mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-11 17:00:37 +00:00
LibJS: Rewrite String.raw() closer to the specification
This includes not throwing a custom exception and using the length_of_array_like abstract operation where required.
This commit is contained in:
parent
d1ffeaf66d
commit
596324ae9c
Notes:
sideshowbarker
2024-07-18 11:25:34 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/596324ae9cc Pull-request: https://github.com/SerenityOS/serenity/pull/8286 Reviewed-by: https://github.com/linusg
3 changed files with 29 additions and 20 deletions
|
@ -150,7 +150,6 @@
|
|||
M(SpeciesConstructorDidNotCreate, "Species constructor did not create {}") \
|
||||
M(SpeciesConstructorReturned, "Species constructor returned {}") \
|
||||
M(StringMatchAllNonGlobalRegExp, "RegExp argument is non-global") \
|
||||
M(StringRawCannotConvert, "Cannot convert property 'raw' to object from {}") \
|
||||
M(StringRepeatCountMustBe, "repeat count must be a {} number") \
|
||||
M(ThisHasNotBeenInitialized, "|this| has not been initialized") \
|
||||
M(ThisIsAlreadyInitialized, "|this| is already initialized") \
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Utf32View.h>
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
|
@ -68,41 +69,50 @@ Value StringConstructor::construct(FunctionObject&)
|
|||
// 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
|
||||
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
|
||||
{
|
||||
auto* template_object = vm.argument(0).to_object(global_object);
|
||||
auto* cooked = vm.argument(0).to_object(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto raw = template_object->get(vm.names.raw);
|
||||
auto raw_value = cooked->get(vm.names.raw).value_or(js_undefined());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (raw.is_empty() || raw.is_nullish()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::StringRawCannotConvert, raw.is_null() ? "null" : "undefined");
|
||||
|
||||
auto* raw = raw_value.to_object(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
}
|
||||
// FIXME: This should use length_of_array_like() and work with any object
|
||||
if (!raw.is_object() || !raw.as_object().is_array())
|
||||
|
||||
auto literal_segments = length_of_array_like(global_object, *raw);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
if (literal_segments == 0)
|
||||
return js_string(vm, "");
|
||||
|
||||
auto* array = static_cast<Array*>(raw.to_object(global_object));
|
||||
auto& raw_array_elements = array->indexed_properties();
|
||||
StringBuilder builder;
|
||||
const auto number_of_substituions = vm.argument_count() - 1;
|
||||
|
||||
for (size_t i = 0; i < raw_array_elements.array_like_size(); ++i) {
|
||||
auto result = raw_array_elements.get(array, i);
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < literal_segments; ++i) {
|
||||
auto next_key = String::number(i);
|
||||
auto next_segment_value = raw->get(next_key).value_or(js_undefined());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!result.has_value())
|
||||
continue;
|
||||
builder.append(result.value().value.to_string(global_object));
|
||||
auto next_segment = next_segment_value.to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (i + 1 < vm.argument_count() && i < raw_array_elements.array_like_size() - 1) {
|
||||
builder.append(vm.argument(i + 1).to_string(global_object));
|
||||
|
||||
builder.append(next_segment);
|
||||
|
||||
if (i + 1 == literal_segments)
|
||||
break;
|
||||
|
||||
if (i < number_of_substituions) {
|
||||
auto next = vm.argument(i + 1);
|
||||
auto next_sub = next.to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
builder.append(next_sub);
|
||||
}
|
||||
}
|
||||
|
||||
return js_string(vm, builder.build());
|
||||
}
|
||||
|
||||
|
|
|
@ -27,5 +27,5 @@ test("basic functionality", () => {
|
|||
test("passing object with no 'raw' property", () => {
|
||||
expect(() => {
|
||||
String.raw({});
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert property 'raw' to object from undefined");
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue