1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

LibJS: Add the TypedArray.of() method

This commit is contained in:
Idan Horowitz 2021-06-17 19:22:14 +03:00 committed by Linus Groh
parent b91df26d4a
commit cc5c1df64b
4 changed files with 88 additions and 0 deletions

View file

@ -156,6 +156,7 @@
M(TypedArrayInvalidByteOffset, "Invalid byte offset for {}: must be a multiple of {}, got {}") \
M(TypedArrayOutOfRangeByteOffset, "Typed array byte offset {} is out of range for buffer with length {}") \
M(TypedArrayOutOfRangeByteOffsetOrLength, "Typed array range {}:{} is out of range for buffer with length {}") \
M(TypedArrayFailedSettingIndex, "Failed setting value of index {} of typed array") \
M(UnknownIdentifier, "'{}' is not defined") \
M(URIMalformed, "URI malformed") \
/* LibWeb bindings */ \

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/TypedArrayConstructor.h>
namespace JS {
@ -26,6 +27,9 @@ void TypedArrayConstructor::initialize(GlobalObject& global_object)
define_property(vm.names.prototype, global_object.typed_array_prototype(), 0);
define_property(vm.names.length, Value(0), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.of, of, 0, attr);
define_native_accessor(vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
}
@ -44,6 +48,60 @@ Value TypedArrayConstructor::construct(Function&)
return {};
}
// 23.2.4.2 TypedArrayCreate ( constructor, argumentList ), https://tc39.es/ecma262/#typedarray-create
static TypedArrayBase* typed_array_create(GlobalObject& global_object, Function& constructor, MarkedValueList arguments)
{
auto& vm = global_object.vm();
auto argument_count = arguments.size();
auto first_argument = argument_count > 0 ? arguments[0] : js_undefined();
auto new_typed_array = vm.construct(constructor, constructor, move(arguments));
if (vm.exception())
return nullptr;
if (!new_typed_array.is_object() || !new_typed_array.as_object().is_typed_array()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "TypedArray");
return nullptr;
}
auto& typed_array = static_cast<TypedArrayBase&>(new_typed_array.as_object());
if (typed_array.viewed_array_buffer()->is_detached()) {
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return nullptr;
}
if (argument_count == 1 && first_argument.is_number() && typed_array.array_length() < first_argument.as_double()) {
vm.throw_exception<TypeError>(global_object, ErrorType::InvalidLength, "typed array");
return nullptr;
}
return &typed_array;
}
// 23.2.2.2 %TypedArray%.of ( ...items ), https://tc39.es/ecma262/#sec-%typedarray%.of
JS_DEFINE_NATIVE_FUNCTION(TypedArrayConstructor::of)
{
auto length = vm.argument_count();
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 {};
}
MarkedValueList arguments(vm.heap());
arguments.append(Value(length));
auto new_object = typed_array_create(global_object, constructor.as_function(), move(arguments));
if (vm.exception())
return {};
for (size_t k = 0; k < length; ++k) {
auto success = new_object->put(k, vm.argument(k));
if (vm.exception())
return {};
if (!success) {
vm.throw_exception<TypeError>(global_object, ErrorType::TypedArrayFailedSettingIndex, k);
return {};
}
}
return new_object;
}
// 23.2.2.4 get %TypedArray% [ @@species ], https://tc39.es/ecma262/#sec-get-%typedarray%-@@species
JS_DEFINE_NATIVE_GETTER(TypedArrayConstructor::symbol_species_getter)
{
return vm.this_value(global_object);

View file

@ -25,6 +25,7 @@ public:
private:
virtual bool has_constructor() const override { return true; }
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.of(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.of(1n, 2n, 3n);
expect(newTypedArray[0]).toBe(1n);
expect(newTypedArray[1]).toBe(2n);
expect(newTypedArray[2]).toBe(3n);
});
});