mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:27:35 +00:00
LibJS: Use create_list_from_array_like() in Reflect.{apply,construct}()
This commit is contained in:
parent
ad7aa05cc6
commit
f932da095e
4 changed files with 4 additions and 26 deletions
|
@ -136,7 +136,6 @@
|
||||||
M(ReferenceUnresolvable, "Unresolvable reference") \
|
M(ReferenceUnresolvable, "Unresolvable reference") \
|
||||||
M(ReflectArgumentMustBeAFunction, "First argument of Reflect.{}() must be a function") \
|
M(ReflectArgumentMustBeAFunction, "First argument of Reflect.{}() must be a function") \
|
||||||
M(ReflectArgumentMustBeAnObject, "First argument of Reflect.{}() must be an object") \
|
M(ReflectArgumentMustBeAnObject, "First argument of Reflect.{}() must be an object") \
|
||||||
M(ReflectBadArgumentsList, "Arguments list must be an object") \
|
|
||||||
M(ReflectBadNewTarget, "Optional third argument of Reflect.construct() must be a constructor") \
|
M(ReflectBadNewTarget, "Optional third argument of Reflect.construct() must be a constructor") \
|
||||||
M(ReflectBadDescriptorArgument, "Descriptor argument is not an object") \
|
M(ReflectBadDescriptorArgument, "Descriptor argument is not an object") \
|
||||||
M(RegExpCompileError, "RegExp compile error: {}") \
|
M(RegExpCompileError, "RegExp compile error: {}") \
|
||||||
|
|
|
@ -36,25 +36,6 @@ static Function* get_target_function_from(GlobalObject& global_object, const Str
|
||||||
return &target.as_function();
|
return &target.as_function();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void prepare_arguments_list(GlobalObject& global_object, Value value, MarkedValueList& arguments)
|
|
||||||
{
|
|
||||||
auto& vm = global_object.vm();
|
|
||||||
if (!value.is_object()) {
|
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadArgumentsList);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
auto& arguments_list = value.as_object();
|
|
||||||
auto length = length_of_array_like(global_object, arguments_list);
|
|
||||||
if (vm.exception())
|
|
||||||
return;
|
|
||||||
for (size_t i = 0; i < length; ++i) {
|
|
||||||
auto element = arguments_list.get(String::number(i));
|
|
||||||
if (vm.exception())
|
|
||||||
return;
|
|
||||||
arguments.append(element.value_or(js_undefined()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ReflectObject::ReflectObject(GlobalObject& global_object)
|
ReflectObject::ReflectObject(GlobalObject& global_object)
|
||||||
: Object(*global_object.object_prototype())
|
: Object(*global_object.object_prototype())
|
||||||
{
|
{
|
||||||
|
@ -91,8 +72,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
|
||||||
if (!target)
|
if (!target)
|
||||||
return {};
|
return {};
|
||||||
auto this_arg = vm.argument(1);
|
auto this_arg = vm.argument(1);
|
||||||
MarkedValueList arguments(vm.heap());
|
auto arguments = create_list_from_array_like(global_object, vm.argument(2));
|
||||||
prepare_arguments_list(global_object, vm.argument(2), arguments);
|
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
return vm.call(*target, this_arg, move(arguments));
|
return vm.call(*target, this_arg, move(arguments));
|
||||||
|
@ -103,8 +83,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
|
||||||
auto* target = get_target_function_from(global_object, "construct");
|
auto* target = get_target_function_from(global_object, "construct");
|
||||||
if (!target)
|
if (!target)
|
||||||
return {};
|
return {};
|
||||||
MarkedValueList arguments(vm.heap());
|
auto arguments = create_list_from_array_like(global_object, vm.argument(1));
|
||||||
prepare_arguments_list(global_object, vm.argument(1), arguments);
|
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
auto* new_target = target;
|
auto* new_target = target;
|
||||||
|
|
|
@ -18,7 +18,7 @@ describe("errors", () => {
|
||||||
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
|
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Reflect.apply(() => {}, undefined, value);
|
Reflect.apply(() => {}, undefined, value);
|
||||||
}).toThrowWithMessage(TypeError, "Arguments list must be an object");
|
}).toThrowWithMessage(TypeError, `${value} is not an object`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,7 +18,7 @@ describe("errors", () => {
|
||||||
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
|
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Reflect.construct(() => {}, value);
|
Reflect.construct(() => {}, value);
|
||||||
}).toThrowWithMessage(TypeError, "Arguments list must be an object");
|
}).toThrowWithMessage(TypeError, `${value} is not an object`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue