summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-06-17 19:22:14 +0300
committerLinus Groh <mail@linusgroh.de>2021-06-17 18:13:20 +0100
commitcc5c1df64b41247a1bdf7f1c4c9bc6aa2f3e5dcc (patch)
tree0576f76e2221e895cae1a0fe55321506489c1dd6 /Userland
parentb91df26d4a14c2721d1920e76552012864c97790 (diff)
downloadserenity-cc5c1df64b41247a1bdf7f1c4c9bc6aa2f3e5dcc.zip
LibJS: Add the TypedArray.of() method
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/ErrorTypes.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp58
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.of.js28
4 files changed, 88 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
index bbecddace1..6c6b5fa374 100644
--- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
+++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
@@ -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 */ \
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp
index 799f400aa8..b26274b6d2 100644
--- a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp
@@ -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);
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h
index 17d3efeab4..0057f546c6 100644
--- a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h
+++ b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h
@@ -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);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.of.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.of.js
new file mode 100644
index 0000000000..107191b5e3
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.of.js
@@ -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);
+ });
+});