Truly independent web browser
LibJS: Create no resolving functions in Promise.prototype.then()
Andreas Kling5cf601a5d315
diff --git a/Libraries/LibJS/Rust/src/runtime/promise.rs b/Libraries/LibJS/Rust/src/runtime/promise.rsindex 16f6c32cec8..6aedef2d2ca 100644--- a/Libraries/LibJS/Rust/src/runtime/promise.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise.rs@@ -169,7 +169,10 @@ impl Promise {// 27.2.1.3 CreateResolvingFunctions ( promise ), https://tc39.es/ecma262/#sec-createresolvingfunctionspub fn create_resolving_functions(&self, vm: &Vm) -> ResolvingFunctions {- let realm = vm.current_realm().expect("there is a current realm");+ self.create_resolving_functions_in_realm(vm, vm.current_realm().expect("there is a current realm"))+ }++ pub fn create_resolving_functions_in_realm(&self, vm: &Vm, realm: Gc<Realm>) -> ResolvingFunctions {self.has_resolver.set(true);// 1. Let alreadyResolved be the Record { [[Value]]: false }.diff --git a/Libraries/LibJS/Rust/src/runtime/promise_capability.rs b/Libraries/LibJS/Rust/src/runtime/promise_capability.rsindex 582d023afda..e8bbfdce75e 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_capability.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_capability.rs@@ -11,7 +11,7 @@ use libjs_runtime_macros::Trace;use crate::gc::class::{GcCell, define_cell};use crate::interpreter::vm::Vm;use crate::layout::cell::{CellHeader, Gc};-use crate::layout::member::Member;+use crate::layout::member::{Member, NullableMember};use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::abstract_operations::{construct, ordinary_create_from_constructor_of};@@ -21,6 +21,7 @@ use crate::runtime::error_types::ErrorType;use crate::runtime::function_object::FunctionObject;use crate::runtime::native_function::NativeFunction;use crate::runtime::promise::Promise;+use crate::runtime::realm::Realm;// 27.2.1.1 PromiseCapability Records, https://tc39.es/ecma262/#sec-promisecapability-records#[repr(C)]@@ -28,11 +29,15 @@ use crate::runtime::promise::Promise;pub struct PromiseCapability {header: CellHeader,promise: Member<Object>,- resolve: Member<FunctionObject>,- reject: Member<FunctionObject>,+ resolve: NullableMember<FunctionObject>,+ reject: NullableMember<FunctionObject>,+ /// The realm of the resolving functions, for a capability that has not created them.+ realm: NullableMember<Realm>,+ #[gc(untraced)]+ padding: u32,}-const _: () = assert!(size_of::<PromiseCapability>() == 24);+const _: () = assert!(size_of::<PromiseCapability>() == 32);define_cell!(PromiseCapability, Other);@@ -46,21 +51,46 @@ impl PromiseCapability {vm.heap().allocate(|_| PromiseCapability {header: CellHeader::for_class(Self::CLASS),promise: Member::new(promise),- resolve: Member::new(resolve),- reject: Member::new(reject),+ resolve: NullableMember::new(Some(resolve)),+ reject: NullableMember::new(Some(reject)),+ realm: NullableMember::new(None),+ padding: 0,})}+ /// OPTIMIZATION: A capability whose [[Resolve]] and [[Reject]] no script can reach does not need them to be functions,+ /// which take more time to create than the rest of a reaction together. Whoever uses this capability+ /// settles `promise` directly, once, from within `realm`, or creates the functions there.+ pub fn create_for_promise_without_resolving_functions(+ vm: &Vm,+ realm: Gc<Realm>,+ promise: Gc<Promise>,+ ) -> Gc<PromiseCapability> {+ vm.heap().allocate(|_| PromiseCapability {+ header: CellHeader::for_class(Self::CLASS),+ promise: Member::new(promise.upcast()),+ resolve: NullableMember::new(None),+ reject: NullableMember::new(None),+ realm: NullableMember::new(Some(realm)),+ padding: 0,+ })+ }++ /// The realm to settle the promise from, for a capability that has no resolving functions.+ pub fn realm_without_resolving_functions(&self) -> Option<Gc<Realm>> {+ self.realm.get()+ }+pub fn promise(&self) -> Gc<Object> {self.promise.get()}pub fn resolve(&self) -> Gc<FunctionObject> {- self.resolve.get()+ self.resolve.get().expect("this capability has resolving functions")}pub fn reject(&self) -> Gc<FunctionObject> {- self.reject.get()+ self.reject.get().expect("this capability has resolving functions")}}diff --git a/Libraries/LibJS/Rust/src/runtime/promise_jobs.rs b/Libraries/LibJS/Rust/src/runtime/promise_jobs.rsindex f382cc9665f..c6900415ac2 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_jobs.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_jobs.rs@@ -70,6 +70,36 @@ fn run_reaction_job(vm: &Vm, reaction: Gc<PromiseReaction>, argument: Value) ->// g. Assert: promiseCapability is a PromiseCapability Record.+ if let Some(realm) = promise_capability.realm_without_resolving_functions() {+ let promise = promise_capability+ .promise()+ .downcast::<Promise>()+ .expect("a capability without resolving functions has a Promise");++ // OPTIMIZATION: Nothing else can resolve the promise, so from the realm of its resolving functions, settling it+ // directly does what calling them would. Without a handler, the job runs in no realm at all. The+ // host looks at the running execution context for a rejection without a handler, which has to be+ // that of a resolving function. Only an object can reject the promise as it resolves it.+ let may_reject_without_handler =+ !promise.is_handled() && (handler_result.is_abrupt() || handler_result.value().is_object());+ if vm.current_realm() == Some(realm) && !may_reject_without_handler {+ if handler_result.is_abrupt() {+ promise.reject(vm, handler_result.value());+ } else {+ promise.resolve(vm, handler_result.value());+ }+ return Ok(Value::UNDEFINED);+ }++ let resolving_functions = promise.create_resolving_functions_in_realm(vm, realm);+ let function = if handler_result.is_abrupt() {+ resolving_functions.reject+ } else {+ resolving_functions.resolve+ };+ return call_function_object(vm, function, Value::UNDEFINED, &[handler_result.value()]);+ }+// h. If handlerResult is an abrupt completion, thenif handler_result.is_abrupt() {// i. Return ? Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).diff --git a/Libraries/LibJS/Rust/src/runtime/promise_prototype.rs b/Libraries/LibJS/Rust/src/runtime/promise_prototype.rsindex 308acf1fcb5..db7ee050406 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_prototype.rs@@ -20,7 +20,7 @@ use crate::runtime::native_function::{NativeFunction, RawNativeFunction, RawNatiuse crate::runtime::object::{MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, define_object_class};use crate::runtime::primitive_string::PrimitiveString;use crate::runtime::promise::{Promise, promise_resolve};-use crate::runtime::promise_capability::new_promise_capability;+use crate::runtime::promise_capability::{PromiseCapability, new_promise_capability};use crate::runtime::property_attributes::{Attribute, PropertyAttributes};use crate::runtime::property_key::PropertyKey;use crate::runtime::prototype_object::typed_this_object;@@ -117,7 +117,12 @@ impl PromisePrototype {let constructor = species_constructor(vm, &promise, realm.intrinsics().promise_constructor(vm).upcast())?;// 4. Let resultCapability be ? NewPromiseCapability(C).- let result_capability = new_promise_capability(vm, Value::from_object(constructor))?;+ // OPTIMIZATION: The resolving functions of the intrinsic Promise would never reach any script here.+ let result_capability = if constructor == realm.intrinsics().promise_constructor(vm).upcast() {+ PromiseCapability::create_for_promise_without_resolving_functions(vm, realm, Promise::create(vm, realm))+ } else {+ new_promise_capability(vm, Value::from_object(constructor))?+ };// 5. Return PerformPromiseThen(promise, onFulfilled, onRejected, resultCapability).Ok(promise.perform_then(vm, on_fulfilled, on_rejected, Some(result_capability)))