summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-07-05 01:25:40 +0300
committerLinus Groh <mail@linusgroh.de>2021-07-05 00:27:03 +0100
commitfac8f9a94d98baa435cc6ad4ee6fe9bab93ae87f (patch)
tree1a1429019434a06a677e8dc1a90484bb24cbaa75 /Userland/Libraries
parent2183d01eb0c2ee9fd5db9af33b4f201586e5cd30 (diff)
downloadserenity-fac8f9a94d98baa435cc6ad4ee6fe9bab93ae87f.zip
LibJS: Add the TypedArray.from() method
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp85
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.from.js28
3 files changed, 114 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp
index 3646bbb4dd..14a5d8c764 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/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)
{
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.h
index 5b2c3ff9ae..c113b255e1 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(from);
JS_DECLARE_NATIVE_FUNCTION(of);
JS_DECLARE_NATIVE_GETTER(symbol_species_getter);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.from.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.from.js
new file mode 100644
index 0000000000..074d6e605c
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.from.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.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);
+ });
+});