summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Array.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-04-06 21:39:17 +0200
committerAndreas Kling <kling@serenityos.org>2021-04-07 09:05:01 +0200
commit1c3eef531762659ef8acf8160e4f9b55d5644a62 (patch)
tree5b1cad186e4be17a684bbafc528e573ae6d0b1d1 /Userland/Libraries/LibJS/Runtime/Array.cpp
parenta42886d8ffac2ebcb3004584ea122fe81cf3abf2 (diff)
downloadserenity-1c3eef531762659ef8acf8160e4f9b55d5644a62.zip
LibJS: Use MarkedValueList for internal own properties getter functions
Letting these create and return a JS::Array directly is pretty awkward since we then need to go through the indexed properties for iteration. Just use a MarkedValueList (i.e. Vector<Value>) for this and add a new Array::create_from() function to turn the Vector into a returnable Array as we did before. This brings it a lot closer to the spec as well, which uses the CreateArrayFromList abstract operation to do exactly this. There's an optimization opportunity for the future here, since we know the Vector's size we could prepare the newly created Array accordingly, e.g. by switching to generic storage upfront if needed.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Array.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Array.cpp9
1 files changed, 9 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Array.cpp b/Userland/Libraries/LibJS/Runtime/Array.cpp
index c04a65c1d8..27f6c835af 100644
--- a/Userland/Libraries/LibJS/Runtime/Array.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Array.cpp
@@ -36,6 +36,15 @@ Array* Array::create(GlobalObject& global_object)
return global_object.heap().allocate<Array>(global_object, *global_object.array_prototype());
}
+// 7.3.17 CreateArrayFromList, https://tc39.es/ecma262/#sec-createarrayfromlist
+Array* Array::create_from(GlobalObject& global_object, const Vector<Value>& values)
+{
+ auto* array = Array::create(global_object);
+ for (size_t i = 0; i < values.size(); ++i)
+ array->define_property(i, values[i]);
+ return array;
+}
+
Array::Array(Object& prototype)
: Object(prototype)
{