1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:27:45 +00:00

LibJS: Convert TypedArrayConstructor functions to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-23 03:25:05 +03:00 committed by Andreas Kling
parent 92b25cacd1
commit f7bafea661
2 changed files with 29 additions and 37 deletions

View file

@ -30,10 +30,10 @@ void TypedArrayConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.typed_array_prototype(), 0); define_direct_property(vm.names.prototype, global_object.typed_array_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable; u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.from, from, 1, attr); define_native_function(vm.names.from, from, 1, attr);
define_old_native_function(vm.names.of, of, 0, attr); define_native_function(vm.names.of, of, 0, attr);
define_old_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable); define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
define_direct_property(vm.names.length, Value(0), Attribute::Configurable); define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
} }
@ -55,92 +55,84 @@ ThrowCompletionOr<Object*> TypedArrayConstructor::construct(FunctionObject&)
} }
// 23.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-%typedarray%.from // 23.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-%typedarray%.from
JS_DEFINE_OLD_NATIVE_FUNCTION(TypedArrayConstructor::from) JS_DEFINE_NATIVE_FUNCTION(TypedArrayConstructor::from)
{ {
auto constructor = vm.this_value(global_object); auto constructor = vm.this_value(global_object);
if (!constructor.is_constructor()) { if (!constructor.is_constructor())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
return {};
}
FunctionObject* map_fn = nullptr; FunctionObject* map_fn = nullptr;
if (!vm.argument(1).is_undefined()) { if (!vm.argument(1).is_undefined()) {
auto callback = vm.argument(1); auto callback = vm.argument(1);
if (!callback.is_function()) { if (!callback.is_function())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
return {};
}
map_fn = &callback.as_function(); map_fn = &callback.as_function();
} }
auto source = vm.argument(0); auto source = vm.argument(0);
auto this_arg = vm.argument(2); auto this_arg = vm.argument(2);
auto using_iterator = TRY_OR_DISCARD(source.get_method(global_object, *vm.well_known_symbol_iterator())); auto using_iterator = TRY(source.get_method(global_object, *vm.well_known_symbol_iterator()));
if (using_iterator) { if (using_iterator) {
auto values = TRY_OR_DISCARD(iterable_to_list(global_object, source, using_iterator)); auto values = TRY(iterable_to_list(global_object, source, using_iterator));
MarkedValueList arguments(vm.heap()); MarkedValueList arguments(vm.heap());
arguments.empend(values.size()); arguments.empend(values.size());
auto target_object = TRY_OR_DISCARD(typed_array_create(global_object, constructor.as_function(), move(arguments))); auto target_object = TRY(typed_array_create(global_object, constructor.as_function(), move(arguments)));
for (size_t k = 0; k < values.size(); ++k) { for (size_t k = 0; k < values.size(); ++k) {
auto k_value = values[k]; auto k_value = values[k];
Value mapped_value; Value mapped_value;
if (map_fn) if (map_fn)
mapped_value = TRY_OR_DISCARD(vm.call(*map_fn, this_arg, k_value, Value(k))); mapped_value = TRY(vm.call(*map_fn, this_arg, k_value, Value(k)));
else else
mapped_value = k_value; mapped_value = k_value;
TRY_OR_DISCARD(target_object->set(k, mapped_value, Object::ShouldThrowExceptions::Yes)); TRY(target_object->set(k, mapped_value, Object::ShouldThrowExceptions::Yes));
} }
return target_object; return target_object;
} }
auto array_like = MUST(source.to_object(global_object)); auto array_like = MUST(source.to_object(global_object));
auto length = TRY_OR_DISCARD(length_of_array_like(global_object, *array_like)); auto length = TRY(length_of_array_like(global_object, *array_like));
MarkedValueList arguments(vm.heap()); MarkedValueList arguments(vm.heap());
arguments.empend(length); arguments.empend(length);
auto target_object = TRY_OR_DISCARD(typed_array_create(global_object, constructor.as_function(), move(arguments))); auto target_object = TRY(typed_array_create(global_object, constructor.as_function(), move(arguments)));
for (size_t k = 0; k < length; ++k) { for (size_t k = 0; k < length; ++k) {
auto k_value = TRY_OR_DISCARD(array_like->get(k)); auto k_value = TRY(array_like->get(k));
Value mapped_value; Value mapped_value;
if (map_fn) if (map_fn)
mapped_value = TRY_OR_DISCARD(vm.call(*map_fn, this_arg, k_value, Value(k))); mapped_value = TRY(vm.call(*map_fn, this_arg, k_value, Value(k)));
else else
mapped_value = k_value; mapped_value = k_value;
TRY_OR_DISCARD(target_object->set(k, mapped_value, Object::ShouldThrowExceptions::Yes)); TRY(target_object->set(k, mapped_value, Object::ShouldThrowExceptions::Yes));
} }
return target_object; return target_object;
} }
// 23.2.2.2 %TypedArray%.of ( ...items ), https://tc39.es/ecma262/#sec-%typedarray%.of // 23.2.2.2 %TypedArray%.of ( ...items ), https://tc39.es/ecma262/#sec-%typedarray%.of
JS_DEFINE_OLD_NATIVE_FUNCTION(TypedArrayConstructor::of) JS_DEFINE_NATIVE_FUNCTION(TypedArrayConstructor::of)
{ {
auto length = vm.argument_count(); auto length = vm.argument_count();
auto constructor = vm.this_value(global_object); auto constructor = vm.this_value(global_object);
if (!constructor.is_constructor()) { if (!constructor.is_constructor())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
return {};
}
MarkedValueList arguments(vm.heap()); MarkedValueList arguments(vm.heap());
arguments.append(Value(length)); arguments.append(Value(length));
auto new_object = TRY_OR_DISCARD(typed_array_create(global_object, constructor.as_function(), move(arguments))); auto new_object = TRY(typed_array_create(global_object, constructor.as_function(), move(arguments)));
for (size_t k = 0; k < length; ++k) { for (size_t k = 0; k < length; ++k) {
auto success = TRY_OR_DISCARD(new_object->set(k, vm.argument(k), Object::ShouldThrowExceptions::Yes)); auto success = TRY(new_object->set(k, vm.argument(k), Object::ShouldThrowExceptions::Yes));
if (!success) { if (!success)
vm.throw_exception<TypeError>(global_object, ErrorType::TypedArrayFailedSettingIndex, k); return vm.throw_completion<TypeError>(global_object, ErrorType::TypedArrayFailedSettingIndex, k);
return {};
}
} }
return new_object; return new_object;
} }
// 23.2.2.4 get %TypedArray% [ @@species ], https://tc39.es/ecma262/#sec-get-%typedarray%-@@species // 23.2.2.4 get %TypedArray% [ @@species ], https://tc39.es/ecma262/#sec-get-%typedarray%-@@species
JS_DEFINE_OLD_NATIVE_FUNCTION(TypedArrayConstructor::symbol_species_getter) JS_DEFINE_NATIVE_FUNCTION(TypedArrayConstructor::symbol_species_getter)
{ {
return vm.this_value(global_object); return vm.this_value(global_object);
} }

View file

@ -25,9 +25,9 @@ public:
private: private:
virtual bool has_constructor() const override { return true; } virtual bool has_constructor() const override { return true; }
JS_DECLARE_OLD_NATIVE_FUNCTION(from); JS_DECLARE_NATIVE_FUNCTION(from);
JS_DECLARE_OLD_NATIVE_FUNCTION(of); JS_DECLARE_NATIVE_FUNCTION(of);
JS_DECLARE_OLD_NATIVE_FUNCTION(symbol_species_getter); JS_DECLARE_NATIVE_FUNCTION(symbol_species_getter);
}; };
} }