Truly independent web browser
LibJS: Look then and constructor up through the special property cache
Andreas Klingbb00c4f1fb04
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rsindex 9ac2bc35f8a..e2c0e9f5df6 100644--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs@@ -742,6 +742,8 @@ define_static_property_lookup_cache_sites! {ArraySpeciesCreateConstructor,ArraySpeciesCreateSpecies,JSONSerializeToJSON,+ PromiseResolveConstructor,+ PromiseResolveFunctionThen,StringPrototypeMatchMatcher,StringPrototypeMatchAllMatcher,StringPrototypeReplaceReplacer,diff --git a/Libraries/LibJS/Rust/src/runtime/promise.rs b/Libraries/LibJS/Rust/src/runtime/promise.rsindex f2db24c62c3..3b85205d17d 100644--- a/Libraries/LibJS/Rust/src/runtime/promise.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise.rs@@ -26,6 +26,7 @@ use crate::runtime::promise_jobs::{create_promise_reaction_job, create_promise_ruse crate::runtime::promise_reaction::{PromiseReaction, PromiseReactionType};use crate::runtime::promise_resolving_function::PromiseResolvingFunction;use crate::runtime::realm::Realm;+use crate::runtime::special_property_cache::{self, SpecialPropertyKey};use crate::runtime::value::same_value;// 27.2.4.7.1 PromiseResolve ( C, x ), https://tc39.es/ecma262/#sec-promise-resolve@@ -37,7 +38,7 @@ pub fn promise_resolve(vm: &Vm, constructor: Gc<Object>, value: Value) -> ThrowC&& let Some(promise) = value.as_object().downcast::<Promise>(){// a. Let xConstructor be ? Get(x, "constructor").- let value_constructor = value.as_object().get(vm, &vm.names.constructor)?;+ let value_constructor = special_property_cache::get(vm, &value.as_object(), SpecialPropertyKey::Constructor)?;// b. If SameValue(xConstructor, C) is true, return x.if same_value(value_constructor, Value::from_object(constructor)) {@@ -237,7 +238,7 @@ impl Promise {}// 9. Let then be Completion(Get(resolution, "then")).- let then = resolution.as_object().get(vm, &vm.names.then);+ let then = special_property_cache::get(vm, &resolution.as_object(), SpecialPropertyKey::Then);// 10. If then is an abrupt completion, thenlet then_action = match then {diff --git a/Libraries/LibJS/Rust/src/runtime/special_property_cache.rs b/Libraries/LibJS/Rust/src/runtime/special_property_cache.rsindex f489ec3c5bb..84546849390 100644--- a/Libraries/LibJS/Rust/src/runtime/special_property_cache.rs+++ b/Libraries/LibJS/Rust/src/runtime/special_property_cache.rs@@ -43,9 +43,11 @@ pub enum SpecialPropertyKey {ValueOf,ToStringTag,ToJson,+ Then,+ Constructor,}-const SPECIAL_PROPERTY_KEY_COUNT: usize = 5;+const SPECIAL_PROPERTY_KEY_COUNT: usize = 7;impl SpecialPropertyKey {fn property_key(self, vm: &Vm) -> PropertyKey {@@ -55,6 +57,8 @@ impl SpecialPropertyKey {Self::ValueOf => vm.names.valueOf.clone(),Self::ToStringTag => PropertyKey::from(vm.well_known_symbols().to_string_tag),Self::ToJson => vm.names.toJSON.clone(),+ Self::Then => vm.names.then.clone(),+ Self::Constructor => vm.names.constructor.clone(),}}@@ -65,6 +69,8 @@ impl SpecialPropertyKey {Self::ValueOf => StaticPropertyLookupCacheSite::ObjectOrdinaryToPrimitiveValueOf,Self::ToStringTag => StaticPropertyLookupCacheSite::ObjectPrototypeToStringToStringTag,Self::ToJson => StaticPropertyLookupCacheSite::JSONSerializeToJSON,+ Self::Then => StaticPropertyLookupCacheSite::PromiseResolveFunctionThen,+ Self::Constructor => StaticPropertyLookupCacheSite::PromiseResolveConstructor,}}}diff --git a/Tests/LibJS/Runtime/special-property-cache.js b/Tests/LibJS/Runtime/special-property-cache.jsindex 33b7b78a0c7..ee88d08632b 100644--- a/Tests/LibJS/Runtime/special-property-cache.js+++ b/Tests/LibJS/Runtime/special-property-cache.js@@ -273,3 +273,65 @@ test("@@toStringTag getter that changes @@toStringTag", () => {expect(Object.prototype.toString.call(second)).toBe("[object X]");expect(`${first}`).toBe("[object X]");});++describe("then and constructor in promise resolution", () => {+ const resolveOften = (makeValue, count = 50) => {+ const results = [];+ for (let i = 0; i < count; ++i)+ Promise.resolve()+ .then(() => makeValue())+ .then(value => results.push(value));+ runQueuedPromiseJobs();+ return results;+ };++ test("then added to Object.prototype", () => {+ const results = resolveOften(() => ({ a: 1 }));+ expect(results).toHaveLength(50);+ expect(results[0]).toEqual({ a: 1 });+ Object.prototype.then = function (resolve) {+ resolve("from then");+ };+ try {+ expect(resolveOften(() => ({ a: 1 }), 1)).toEqual(["from then"]);+ } finally {+ delete Object.prototype.then;+ }+ expect(resolveOften(() => ({ a: 1 }), 1)).toEqual([{ a: 1 }]);+ });++ test("Promise.prototype.then replaced", () => {+ resolveOften(() => Promise.resolve(1));+ const originalThen = Promise.prototype.then;+ let calls = 0;+ Promise.prototype.then = function (...args) {+ ++calls;+ return originalThen.apply(this, args);+ };+ try {+ let result;+ originalThen.call(+ Promise.resolve().then(() => Promise.resolve(2)),+ value => (result = value)+ );+ runQueuedPromiseJobs();+ expect(result).toBe(2);+ expect(calls).toBeGreaterThan(0);+ } finally {+ Promise.prototype.then = originalThen;+ }+ });++ test("Promise.prototype.constructor replaced", () => {+ const promise = Promise.resolve(3);+ for (let i = 0; i < 50; ++i) expect(Promise.resolve(promise)).toBe(promise);+ const originalConstructor = Promise.prototype.constructor;+ Promise.prototype.constructor = function () {};+ try {+ expect(Promise.resolve(promise)).not.toBe(promise);+ } finally {+ Promise.prototype.constructor = originalConstructor;+ }+ expect(Promise.resolve(promise)).toBe(promise);+ });+});