Skip to content

Truly independent web browser

LibJS: Add values directly in the Set and Map constructors

Andreas Kling1134e5e369d1
diff --git a/Libraries/LibJS/Rust/src/runtime/map_constructor.rs b/Libraries/LibJS/Rust/src/runtime/map_constructor.rs
index 343b6fca1f4..184010b822f 100644
--- a/Libraries/LibJS/Rust/src/runtime/map_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/map_constructor.rs
@@ -26,10 +26,12 @@ use crate::runtime::intrinsics::Intrinsics;
use crate::runtime::iterator::get_iterator_values;
use crate::runtime::keyed_collections::canonicalize_keyed_collection_key;
use crate::runtime::map::Map;
+use crate::runtime::map_prototype;
use crate::runtime::native_function::{NativeFunction, define_native_function_class, raw_native};
use crate::runtime::property_attributes::{Attribute, PropertyAttributes};
use crate::runtime::property_key::PropertyKey;
use crate::runtime::realm::Realm;
+use crate::runtime::regexp_constructor::is_raw_native_function_running;
use crate::runtime::value::same_value;
use crate::runtime::value_traits::value_traits_hash;
use crate::utf16::utf16_formatted;
@@ -198,6 +200,8 @@ impl MapConstructor {
);
}
let adder = adder.as_function();
+ // OPTIMIZATION: Calling %Map.prototype.set% with the map as its this value only sets the entry.
+ let adder_is_intrinsic = is_raw_native_function_running(vm, adder, map_prototype::SET_FUNCTION);
let add_entry = |iterator_value: Value| -> ThrowCompletionOr<()> {
if !iterator_value.is_object() {
@@ -210,7 +214,11 @@ impl MapConstructor {
let key = iterator_value.as_object().get(vm, &PropertyKey::from(0))?;
let value = iterator_value.as_object().get(vm, &PropertyKey::from(1))?;
- call_function_object(vm, adder, Value::from_object(map), &[key, value])?;
+ if adder_is_intrinsic {
+ map.map_set(vm, canonicalize_keyed_collection_key(key), value);
+ } else {
+ call_function_object(vm, adder, Value::from_object(map), &[key, value])?;
+ }
Ok(())
};
diff --git a/Libraries/LibJS/Rust/src/runtime/map_prototype.rs b/Libraries/LibJS/Rust/src/runtime/map_prototype.rs
index c5e8a4cc90c..f84027f6779 100644
--- a/Libraries/LibJS/Rust/src/runtime/map_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/map_prototype.rs
@@ -13,6 +13,7 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
+use crate::layout_forward::RawNativeFunctionPointer;
use crate::runtime::abstract_operations::call;
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::error::ErrorKind;
@@ -31,6 +32,9 @@ use crate::runtime::property_key::PropertyKey;
use crate::runtime::prototype_object::typed_this_object;
use crate::runtime::realm::Realm;
+/// %Map.prototype.set%, which the Map constructor calls directly when it finds it as the adder.
+pub(crate) static SET_FUNCTION: RawNativeFunctionPointer = raw_native!(MapPrototype::set);
+
/// %Map.prototype%.
#[repr(C)]
#[derive(Trace)]
@@ -98,7 +102,7 @@ impl MapPrototype {
);
define_native_function(&names.has, raw_native!(MapPrototype::has), 1);
define_native_function(&names.keys, raw_native!(MapPrototype::keys), 0);
- define_native_function(&names.set, raw_native!(MapPrototype::set), 2);
+ define_native_function(&names.set, SET_FUNCTION, 2);
define_native_function(&names.values, raw_native!(MapPrototype::values), 0);
object.define_native_accessor(
diff --git a/Libraries/LibJS/Rust/src/runtime/set_constructor.rs b/Libraries/LibJS/Rust/src/runtime/set_constructor.rs
index ddf1249a4c6..1f5e10b8939 100644
--- a/Libraries/LibJS/Rust/src/runtime/set_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/set_constructor.rs
@@ -18,11 +18,14 @@ use crate::runtime::error_types::ErrorType;
use crate::runtime::function_object::FunctionObject;
use crate::runtime::intrinsics::Intrinsics;
use crate::runtime::iterator::get_iterator_values;
+use crate::runtime::keyed_collections::canonicalize_keyed_collection_key;
use crate::runtime::native_function::{NativeFunction, define_native_function_class, raw_native};
use crate::runtime::property_attributes::{Attribute, PropertyAttributes};
use crate::runtime::property_key::PropertyKey;
use crate::runtime::realm::Realm;
+use crate::runtime::regexp_constructor::is_raw_native_function_running;
use crate::runtime::set::Set;
+use crate::runtime::set_prototype;
#[repr(C)]
#[derive(Trace)]
@@ -115,6 +118,8 @@ impl SetConstructor {
);
}
let adder = adder.as_function();
+ // OPTIMIZATION: Calling %Set.prototype.add% with the set as its this value only adds the value.
+ let adder_is_intrinsic = is_raw_native_function_running(vm, adder, set_prototype::ADD_FUNCTION);
// 7. Let iteratorRecord be ? GetIterator(iterable, sync).
// 8. Repeat,
@@ -123,6 +128,10 @@ impl SetConstructor {
// b. If next is DONE, return set.
// c. Let status be Completion(Call(adder, set, « nextValue »)).
// d. IfAbruptCloseIterator(status, iteratorRecord).
+ if adder_is_intrinsic {
+ set.set_add(vm, canonicalize_keyed_collection_key(next));
+ return None;
+ }
call_function_object(vm, adder, Value::from_object(set), &[next])
.err()
.map(Completion::from)
diff --git a/Libraries/LibJS/Rust/src/runtime/set_prototype.rs b/Libraries/LibJS/Rust/src/runtime/set_prototype.rs
index d053c064a23..77c7f36b1b1 100644
--- a/Libraries/LibJS/Rust/src/runtime/set_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/set_prototype.rs
@@ -14,6 +14,7 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
+use crate::layout_forward::RawNativeFunctionPointer;
use crate::runtime::abstract_operations::{call, call_function_object};
use crate::runtime::completion::{Completion, ThrowCompletionOr};
use crate::runtime::error::ErrorKind;
@@ -33,6 +34,9 @@ use crate::runtime::realm::Realm;
use crate::runtime::set::{Set, get_set_record, set_data_has};
use crate::runtime::set_iterator::SetIterator;
+/// %Set.prototype.add%, which the Set constructor calls directly when it finds it as the adder.
+pub(crate) static ADD_FUNCTION: RawNativeFunctionPointer = raw_native!(SetPrototype::add);
+
/// %Set.prototype%.
#[repr(C)]
#[derive(Trace)]
@@ -85,7 +89,7 @@ impl SetPrototype {
object.define_native_function(vm, realm, name, function, length, attr, None);
};
- define_native_function(&names.add, raw_native!(SetPrototype::add), 1);
+ define_native_function(&names.add, ADD_FUNCTION, 1);
define_native_function(&names.clear, raw_native!(SetPrototype::clear), 0);
define_native_function(&names.delete_, raw_native!(SetPrototype::delete_), 1);
define_native_function(&names.difference, raw_native!(SetPrototype::difference), 1);
diff --git a/Tests/LibJS/Runtime/builtins/Set/Set-and-Map-constructor-adders.js b/Tests/LibJS/Runtime/builtins/Set/Set-and-Map-constructor-adders.js
new file mode 100644
index 00000000000..1c3efade53d
--- /dev/null
+++ b/Tests/LibJS/Runtime/builtins/Set/Set-and-Map-constructor-adders.js
@@ -0,0 +1,38 @@
+test("replaced adders are called", () => {
+ const originalAdd = Set.prototype.add;
+ const originalSet = Map.prototype.set;
+ const added = [];
+ Set.prototype.add = function (value) {
+ added.push(value);
+ return originalAdd.call(this, value);
+ };
+ Map.prototype.set = function (key, value) {
+ added.push([key, value]);
+ return originalSet.call(this, key, value);
+ };
+ try {
+ new Set([1, 2]);
+ new Map([[3, 4]]);
+ expect(added).toEqual([1, 2, [3, 4]]);
+ } finally {
+ Set.prototype.add = originalAdd;
+ Map.prototype.set = originalSet;
+ }
+});
+
+test("-0 is added as +0", () => {
+ const set = new Set([-0]);
+ expect(Object.is([...set][0], 0)).toBeTrue();
+ const map = new Map([[-0, "zero"]]);
+ expect(Object.is([...map.keys()][0], 0)).toBeTrue();
+});
+
+test("builtin adders add the values of any iterable", () => {
+ function* values() {
+ yield 1;
+ yield 1;
+ yield 2;
+ }
+ expect([...new Set(values())]).toEqual([1, 2]);
+ expect([...new Map(new Set([[1, "a"]]))]).toEqual([[1, "a"]]);
+});