1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

LibJS: Add the TypedArray.from() method

This commit is contained in:
Idan Horowitz 2021-07-05 01:25:40 +03:00 committed by Linus Groh
parent 2183d01eb0
commit fac8f9a94d
3 changed files with 114 additions and 0 deletions

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/TypedArrayConstructor.h>
@ -31,6 +32,7 @@ void TypedArrayConstructor::initialize(GlobalObject& global_object)
define_property(vm.names.length, Value(0), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.from, from, 1, attr);
define_native_function(vm.names.of, of, 0, attr);
define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
@ -80,6 +82,89 @@ static TypedArrayBase* typed_array_create(GlobalObject& global_object, FunctionO
return &typed_array;
}
// 23.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-%typedarray%.from
JS_DEFINE_NATIVE_FUNCTION(TypedArrayConstructor::from)
{
auto constructor = vm.this_value(global_object);
if (!constructor.is_constructor()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
return {};
}
FunctionObject* map_fn = nullptr;
if (!vm.argument(1).is_undefined()) {
auto callback = vm.argument(1);
if (!callback.is_function()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
return {};
}
map_fn = &callback.as_function();
}
auto source = vm.argument(0);
auto this_arg = vm.argument(2);
auto using_iterator = source.get_method(global_object, *vm.well_known_symbol_iterator());
if (vm.exception())
return {};
if (using_iterator) {
auto values = iterable_to_list(global_object, source, using_iterator);
if (vm.exception())
return {};
MarkedValueList arguments(vm.heap());
arguments.empend(values.size());
auto target_object = typed_array_create(global_object, constructor.as_function(), move(arguments));
if (vm.exception())
return {};
for (size_t k = 0; k < values.size(); ++k) {
auto k_value = values[k];
Value mapped_value;
if (map_fn) {
mapped_value = vm.call(*map_fn, this_arg, k_value, Value(k));
if (vm.exception())
return {};
} else {
mapped_value = k_value;
}
target_object->set(k, mapped_value, true);
if (vm.exception())
return {};
}
return target_object;
}
auto array_like = source.to_object(global_object);
auto length = length_of_array_like(global_object, *array_like);
if (vm.exception())
return {};
MarkedValueList arguments(vm.heap());
arguments.empend(length);
auto target_object = typed_array_create(global_object, constructor.as_function(), move(arguments));
if (vm.exception())
return {};
for (size_t k = 0; k < length; ++k) {
auto k_value = array_like->get(k);
Value mapped_value;
if (map_fn) {
mapped_value = vm.call(*map_fn, this_arg, k_value, Value(k));
if (vm.exception())
return {};
} else {
mapped_value = k_value;
}
target_object->set(k, mapped_value, true);
if (vm.exception())
return {};
}
return target_object;
}
// 23.2.2.2 %TypedArray%.of ( ...items ), https://tc39.es/ecma262/#sec-%typedarray%.of
JS_DEFINE_NATIVE_FUNCTION(TypedArrayConstructor::of)
{

View file

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

View file

@ -0,0 +1,28 @@
const TYPED_ARRAYS = [
Uint8Array,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
];
const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array];
test("basic functionality", () => {
TYPED_ARRAYS.forEach(T => {
const newTypedArray = T.from([1, 2, 3]);
expect(newTypedArray[0]).toBe(1);
expect(newTypedArray[1]).toBe(2);
expect(newTypedArray[2]).toBe(3);
});
BIGINT_TYPED_ARRAYS.forEach(T => {
const newTypedArray = T.from([1n, 2n, 3n]);
expect(newTypedArray[0]).toBe(1n);
expect(newTypedArray[1]).toBe(2n);
expect(newTypedArray[2]).toBe(3n);
});
});