Truly independent web browser
LibJS: Add operation to resolve a promise without running script
Tim Ledbetter024d247fb384
diff --git a/Libraries/LibJS/Runtime/PromiseCapability.cpp b/Libraries/LibJS/Runtime/PromiseCapability.cppindex 1b2fd4aea31..615c35cceee 100644--- a/Libraries/LibJS/Runtime/PromiseCapability.cpp+++ b/Libraries/LibJS/Runtime/PromiseCapability.cpp@@ -44,4 +44,10 @@ ThrowCompletionOr<GC::Ref<PromiseCapability>> new_promise_capability(VM& vm, Valreturn completion_from_abi<GC::Ref<PromiseCapability>>(js_promise_capability_new(vm_to_abi(vm), value_to_abi(constructor)));}+// SafePromiseResolve ( promiseCapability, resolution ), https://tc39.es/proposal-thenable-curtailment/#sec-safe-promise-resolve+ThrowCompletionOr<Value> safe_promise_resolve(VM& vm, PromiseCapability const& promise_capability, Value resolution)+{+ return completion_from_abi<Value>(js_safe_promise_resolve(vm_to_abi(vm), promise_capability_to_abi(promise_capability), value_to_abi(resolution)));+}+}diff --git a/Libraries/LibJS/Runtime/PromiseCapability.h b/Libraries/LibJS/Runtime/PromiseCapability.hindex b9eee05fc4f..717c3549177 100644--- a/Libraries/LibJS/Runtime/PromiseCapability.h+++ b/Libraries/LibJS/Runtime/PromiseCapability.h@@ -56,4 +56,7 @@ public:// 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapabilityJS_API ThrowCompletionOr<GC::Ref<PromiseCapability>> new_promise_capability(VM& vm, Value constructor);+// SafePromiseResolve ( promiseCapability, resolution ), https://tc39.es/proposal-thenable-curtailment/#sec-safe-promise-resolve+JS_API ThrowCompletionOr<Value> safe_promise_resolve(VM&, PromiseCapability const&, Value resolution);+}diff --git a/Libraries/LibJS/Rust/src/api.rs b/Libraries/LibJS/Rust/src/api.rsindex 5e80779578c..7802220401a 100644--- a/Libraries/LibJS/Rust/src/api.rs+++ b/Libraries/LibJS/Rust/src/api.rs@@ -79,7 +79,7 @@ pub use crate::runtime::native_function::{NativeFunction, RawNativeFunction};pub use crate::runtime::number_object::NumberObject;pub use crate::runtime::object::{IntegrityLevel, Object, PropertyKind, ShouldThrowExceptions};pub use crate::runtime::primitive_string::PrimitiveString;-pub use crate::runtime::promise::{Promise, PromiseState, RejectionOperation};+pub use crate::runtime::promise::{Promise, PromiseState, RejectionOperation, safe_promise_resolve};pub use crate::runtime::promise_capability::{PromiseCapability, new_promise_capability};pub use crate::runtime::property_attributes::{Attribute, PropertyAttributes};pub use crate::runtime::property_key::PropertyKey;diff --git a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs b/Libraries/LibJS/Rust/src/embedding/host/host_object.rsindex 1d67f2f338a..6580c5a36d2 100644--- a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs+++ b/Libraries/LibJS/Rust/src/embedding/host/host_object.rs@@ -480,6 +480,16 @@ pub(crate) fn as_host_object(object: &Object) -> &HostObject {cast_ref(object)}+/// HostObjectHasImpureLookupInternalMethods(object): whether a hook of the object's host class looks its properties or+/// its prototype up, which may run script.+pub(crate) fn host_object_has_impure_lookup_internal_methods(object: &Object) -> bool {+ if object.class().id != ClassId::HostObject {+ return false;+ }+ let hooks = hooks_of(object);+ hooks.get_prototype_of.is_some() || hooks.get_own_property.is_some() || hooks.get.is_some()+}+/// host_class_of(): the host class of an object of any host kind, or none for any other object.pub fn host_class_of(object: &Object) -> Option<&'static JSHostClass> {match object.class().id {diff --git a/Libraries/LibJS/Rust/src/embedding/promise.rs b/Libraries/LibJS/Rust/src/embedding/promise.rsindex bdc082e900d..6256d2ee800 100644--- a/Libraries/LibJS/Rust/src/embedding/promise.rs+++ b/Libraries/LibJS/Rust/src/embedding/promise.rs@@ -24,7 +24,7 @@ use crate::gc::heap_function::HeapFunction;use crate::layout::cell::Gc;use crate::layout::host_class::{JSCompletion, JSObject, JSPromiseCapability, JSVM, JSValue};use crate::layout::value::Value;-use crate::runtime::promise::{Promise, PromiseState};+use crate::runtime::promise::{Promise, PromiseState, safe_promise_resolve};use crate::runtime::promise_capability::new_promise_capability;/// [[PromiseState]], in the order of LibJS's JS::Promise::State.@@ -175,3 +175,17 @@ pub unsafe extern "C" fn js_promise_capability_reject(capability: *mut JSPromise// SAFETY: See the module documentation.object_into_abi(unsafe { cell_from_abi::<JSPromiseCapability>(capability) }.reject())}++/// SafePromiseResolve ( promiseCapability, resolution ), which resolves the capability's promise without running+/// script. The capability's [[Promise]] is a Promise, and its [[Resolve]] a resolve function of that promise. Main+/// thread only.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn js_safe_promise_resolve(+ vm: *mut JSVM,+ capability: *mut JSPromiseCapability,+ resolution: JSValue,+) -> JSCompletion {+ // SAFETY: See the module documentation.+ let (vm, capability) = unsafe { (vm_from_abi(vm), cell_from_abi::<JSPromiseCapability>(capability)) };+ completion_into_abi(safe_promise_resolve(vm, capability, Value::from_bits(resolution)))+}diff --git a/Libraries/LibJS/Rust/src/runtime/promise.rs b/Libraries/LibJS/Rust/src/runtime/promise.rsindex ffc1009c7e4..0a711537a77 100644--- a/Libraries/LibJS/Rust/src/runtime/promise.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise.rs@@ -10,22 +10,28 @@ use core::ops::Deref;use ak::Utf16String;use libjs_runtime_macros::Trace;+use crate::embedding::host::host_object::host_object_has_impure_lookup_internal_methods;use crate::gc::class::{Class, ExternalMemorySize, GcCell, define_cell};use crate::gc::defer_gc::DeferGc;use crate::gc::gc_ref_cell::GcRefCell;+use crate::gc::heap_function::create_heap_function;use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::abstract_operations::call_function_object;-use crate::runtime::completion::ThrowCompletionOr;+use crate::runtime::completion::{Must, ThrowCompletionOr};use crate::runtime::error::TypeError;use crate::runtime::function_object::FunctionObject;+use crate::runtime::module_namespace_object::ModuleNamespaceObject;use crate::runtime::object::MayInterfereWithIndexedPropertyAccess;use crate::runtime::promise_capability::{PromiseCapability, new_promise_capability};-use crate::runtime::promise_jobs::{create_promise_reaction_job, create_promise_resolve_thenable_job};+use crate::runtime::promise_jobs::{+ ThenAction, create_promise_reaction_job, create_promise_resolve_thenable_job, perform_promise_resolve_thenable,+};use crate::runtime::promise_reaction::{PromiseReaction, PromiseReactionType};use crate::runtime::promise_resolving_function::PromiseResolvingFunction;+use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;use crate::runtime::special_property_cache::{self, SpecialPropertyKey};use crate::runtime::value::same_value;@@ -65,6 +71,156 @@ pub fn promise_resolve(vm: &Vm, constructor: Gc<Object>, value: Value) -> ThrowCOk(promise_capability.promise())}+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum PropertyAccessKind {+ Any,+ Set,+ Get,+}++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum ThenCallTiming {+ Sync,+ Deferred,+}++// PropertyAccessCouldRunUserCode ( o, propertyKey, kind ), https://tc39.es/proposal-thenable-curtailment/#sec-has-property-which-could-run-user-code+pub fn property_access_could_run_user_code(+ vm: &Vm,+ mut object: Gc<Object>,+ property_key: &PropertyKey,+ kind: PropertyAccessKind,+) -> bool {+ loop {+ // 1. Assert: The following steps do not run user code.++ // 2. If o has the [[GetPrototypeOf]] and [[GetOwnProperty]] internal methods as defined in 10.5, return true.+ if object.is_proxy_object() {+ return true;+ }++ // 3. If o has the [[GetPrototypeOf]] and [[GetOwnProperty]] internal methods as defined in 10.4.6, return true.+ if object.is::<ModuleNamespaceObject>() {+ return true;+ }++ // 4. If HostObjectHasImpureLookupInternalMethods(o) is true, return true.+ if host_object_has_impure_lookup_internal_methods(&object) {+ return true;+ }++ // 5. Let desc be ! o.[[GetOwnProperty]](propertyKey).+ let descriptor = object.internal_get_own_property(vm, property_key).must();++ // 6. If desc is not undefined, then+ if let Some(descriptor) = descriptor {+ // a. If IsAccessorDescriptor(desc) is true, then+ if descriptor.is_accessor_descriptor() {+ // i. If kind is either any or get, and desc.[[Get]] is not undefined, return true.+ if matches!(kind, PropertyAccessKind::Any | PropertyAccessKind::Get)+ && matches!(descriptor.get, Some(Some(_)))+ {+ return true;+ }++ // ii. If kind is either any or set, and desc.[[Set]] is not undefined, return true.+ if matches!(kind, PropertyAccessKind::Any | PropertyAccessKind::Set)+ && matches!(descriptor.set, Some(Some(_)))+ {+ return true;+ }+ }++ // b. Return false.+ return false;+ }++ // 7. Let proto be ! o.[[GetPrototypeOf]]().+ let prototype = object.internal_get_prototype_of(vm).must();++ // 8. If proto is null, return false.+ let Some(prototype) = prototype else {+ return false;+ };++ // 9. Return PropertyAccessCouldRunUserCode(proto, propertyKey, kind).+ object = prototype;+ }+}++// RequiresDeferredPromiseResolution ( value ), https://tc39.es/proposal-thenable-curtailment/#sec-requires-deferred-promise-resolution+fn requires_deferred_promise_resolution(vm: &Vm, value: Value) -> bool {+ // 1. If value is not an Object, return false.+ if !value.is_object() {+ return false;+ }+ let object = value.as_object();++ // OPTIMIZATION: The special property cache only knows what Get gives without running code.+ if let Some(then_value) = special_property_cache::known(&object, SpecialPropertyKey::Then) {+ return then_value.is_function();+ }++ // 2. If PropertyAccessCouldRunUserCode(value, "then", get) is true, return true.+ if property_access_could_run_user_code(vm, object, &vm.names.then, PropertyAccessKind::Get) {+ return true;+ }++ // 3. Let thenValue be ! Get(value, "then").+ // AD-HOC: Get throws when it runs out of native stack. The deferred steps then perform it again.+ let Ok(then_value) = special_property_cache::get(vm, &object, SpecialPropertyKey::Then) else {+ return true;+ };++ // 4. If IsCallable(thenValue) is true, return true.+ // 5. Return false.+ then_value.is_function()+}++// SafePromiseResolve ( promiseCapability, resolution ), https://tc39.es/proposal-thenable-curtailment/#sec-safe-promise-resolve+pub fn safe_promise_resolve(+ vm: &Vm,+ promise_capability: Gc<PromiseCapability>,+ resolution: Value,+) -> ThrowCompletionOr<Value> {+ // 1. If RequiresDeferredPromiseResolution(resolution) is false, then+ if !requires_deferred_promise_resolution(vm, resolution) {+ // a. Return ? Call(promiseCapability.[[Resolve]], undefined, « resolution »).+ return call_function_object(vm, promise_capability.resolve(), Value::UNDEFINED, &[resolution]);+ }++ // 2. Let promise be promiseCapability.[[Promise]].+ let promise = promise_capability+ .promise()+ .downcast::<Promise>()+ .expect("the capability has a Promise");++ // 3. Let deferredSteps be a new Abstract Closure that captures promise and resolution and performs the following steps when called:+ let deferred_steps = create_heap_function(vm, (promise, resolution), |vm, &(promise, resolution)| {+ // a. Perform ? PerformPromiseResolution(promise, resolution, deferred).+ promise.perform_promise_resolution(vm, resolution, ThenCallTiming::Deferred);++ // b. Return undefined.+ Ok(Value::UNDEFINED)+ });++ // 4. Let deferredThen be CreateBuiltinFunction(deferredSteps, 0, "", « »).+ // 5. Let wrapper be OrdinaryObjectCreate(null).+ // 6. Perform ! CreateDataPropertyOrThrow(wrapper, "then", deferredThen).+ // 7. Return ? Call(promiseCapability.[[Resolve]], undefined, « wrapper »).+ // AD-HOC: Resolving promise with wrapper marks promiseCapability as already resolved and enqueues a job that+ // performs deferredSteps. We do both directly, because an embedder may resolve a promise outside of script,+ // where there is no current realm and HostMakeJobCallback has no state to capture.+ let resolve_function = promise_capability+ .resolve()+ .downcast::<PromiseResolvingFunction>()+ .expect("the capability has the resolve function of a Promise");+ if resolve_function.mark_already_resolved() {+ vm.host_enqueue_promise_job()(vm, deferred_steps, Some(promise.shape().realm()));+ }+ Ok(Value::UNDEFINED)+}+#[derive(Clone, Copy, Debug, PartialEq, Eq, Trace)]pub enum PromiseState {Pending,@@ -242,7 +398,8 @@ impl Promise {// 6. Set alreadyResolved.[[Value]] to true.already_resolved.set(true);- promise.resolve_steps(vm, resolution);+ // NB: PerformPromiseResolution performs steps 7 to 16.+ promise.perform_promise_resolution(vm, resolution, ThenCallTiming::Sync);Value::UNDEFINED}@@ -257,15 +414,17 @@ impl Promise {"only a promise that nothing else resolves is resolved directly");self.has_resolver.set(true);- self.resolve_steps(vm, resolution);+ self.perform_promise_resolution(vm, resolution, ThenCallTiming::Sync);}- /// Steps 7 to 16 of the Promise Resolve Functions, which resolve the promise with `resolution`.- fn resolve_steps(&self, vm: &Vm, resolution: Value) {+ // PerformPromiseResolution ( promise, resolution, thenCallTiming ), https://tc39.es/proposal-thenable-curtailment/#sec-perform-promise-resolution+ pub fn perform_promise_resolution(&self, vm: &Vm, resolution: Value, then_call_timing: ThenCallTiming) {let realm = vm.current_realm().expect("there is a current realm");let promise = self.as_gc();- // 7. If SameValue(resolution, promise) is true, then+ // 1. Assert: promise has a [[PromiseState]] internal slot.++ // 2. If SameValue(resolution, promise) is true, thenif resolution.is_object() && resolution.as_object() == promise.upcast() {// a. Let selfResolutionError be a newly created TypeError object.let self_resolution_error =@@ -274,54 +433,64 @@ impl Promise {// b. Perform RejectPromise(promise, selfResolutionError).promise.reject(vm, Value::from_object(self_resolution_error));- // c. Return undefined.+ // c. Return unused.return;}- // 8. If Type(resolution) is not Object, then+ // 3. If resolution is not an Object, thenif !resolution.is_object() {// a. Perform FulfillPromise(promise, resolution).promise.fulfill(vm, resolution);- // b. Return undefined.+ // b. Return unused.return;}- // 9. Let then be Completion(Get(resolution, "then")).+ // 4. Let then be Completion(Get(resolution, "then")).let then = special_property_cache::get(vm, &resolution.as_object(), SpecialPropertyKey::Then);- // 10. If then is an abrupt completion, then+ // 5. If then is an abrupt completion, thenlet then_action = match then {Err(throw) => {// a. Perform RejectPromise(promise, then.[[Value]]).promise.reject(vm, throw.value());- // b. Return undefined.+ // b. Return unused.return;}- // 11. Let thenAction be then.[[Value]].+ // 6. Let thenAction be then.[[Value]].Ok(then_action) => then_action,};- // 12. If IsCallable(thenAction) is false, then+ // 7. If IsCallable(thenAction) is false, thenif !then_action.is_function() {// a. Perform FulfillPromise(promise, resolution).promise.fulfill(vm, resolution);- // b. Return undefined.+ // b. Return unused.+ return;+ }++ // 8. If thenCallTiming is deferred, then+ if then_call_timing == ThenCallTiming::Deferred {+ // a. Perform ! PerformPromiseResolveThenable(promise, resolution, thenAction).+ perform_promise_resolve_thenable(vm, promise, resolution, ThenAction::Function(then_action.as_function()))+ .must();++ // b. Return unused.return;}- // 13. Let thenJobCallback be HostMakeJobCallback(thenAction).+ // 9. Let thenJobCallback be HostMakeJobCallback(thenAction).let then_job_callback = vm.host_make_job_callback()(vm, then_action.as_function());- // 14. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).+ // 10. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).let job = create_promise_resolve_thenable_job(vm, promise, resolution, then_job_callback);- // 15. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).+ // 11. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).vm.host_enqueue_promise_job()(vm, job.job, job.realm);- // 16. Return undefined.+ // 12. Return unused.}// 27.2.1.3.1 Promise Reject Functions, https://tc39.es/ecma262/#sec-promise-reject-functionsdiff --git a/Libraries/LibJS/Rust/src/runtime/promise_jobs.rs b/Libraries/LibJS/Rust/src/runtime/promise_jobs.rsindex c6900415ac2..18604c3469e 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_jobs.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_jobs.rs@@ -11,6 +11,7 @@ use crate::layout::value::Value;use crate::runtime::abstract_operations::{call_function_object, get_function_realm};use crate::runtime::completion::{Completion, CompletionType, ThrowCompletionOr};use crate::runtime::error::InternalError;+use crate::runtime::function_object::FunctionObject;use crate::runtime::job_callback::JobCallback;use crate::runtime::promise::Promise;use crate::runtime::promise_reaction::{PromiseReaction, PromiseReactionType};@@ -155,34 +156,46 @@ pub fn create_promise_reaction_job(vm: &Vm, reaction: Gc<PromiseReaction>, argum}}-// 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob-fn run_resolve_thenable_job(+#[derive(Clone, Copy)]+pub enum ThenAction {+ JobCallback(Gc<JobCallback>),+ Function(Gc<FunctionObject>),+}++// PerformPromiseResolveThenable ( promiseToResolve, thenable, then ), https://tc39.es/proposal-thenable-curtailment/#sec-perform-promise-resolve-thenable+pub fn perform_promise_resolve_thenable(vm: &Vm,promise_to_resolve: Gc<Promise>,thenable: Value,- then: Gc<JobCallback>,+ then: ThenAction,) -> ThrowCompletionOr<Value> {- // a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).+ // 1. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).let resolving_functions = promise_to_resolve.create_resolving_functions(vm);- // b. Let thenCallResult be Completion(HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).let arguments = [Value::from_object(resolving_functions.resolve),Value::from_object(resolving_functions.reject),];- let then_call_result = vm.host_call_job_callback()(vm, then, thenable, &arguments);+ let then_call_result = match then {+ // 2. If then is a function object, then+ // a. Let thenCallResult be Completion(Call(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).+ ThenAction::Function(then) => call_function_object(vm, then, thenable, &arguments),+ // 3. Else,+ // a. Let thenCallResult be Completion(HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).+ ThenAction::JobCallback(then) => vm.host_call_job_callback()(vm, then, thenable, &arguments),+ };- // c. If thenCallResult is an abrupt completion, then+ // 4. If thenCallResult is an abrupt completion, thenif let Err(throw) = then_call_result {- // i. Return ? Call(resolvingFunctions.[[Reject]], undefined, « thenCallResult.[[Value]] »).+ // a. Return ! Call(resolvingFunctions.[[Reject]], undefined, « thenCallResult.[[Value]] »).return call_function_object(vm, resolving_functions.reject, Value::UNDEFINED, &[throw.value()]);}- // d. Return ? thenCallResult.+ // 5. Return ! thenCallResult.then_call_result}-// 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob+// NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/proposal-thenable-curtailment/#sec-newpromiseresolvethenablejobpub fn create_promise_resolve_thenable_job(vm: &Vm,promise_to_resolve: Gc<Promise>,@@ -202,11 +215,13 @@ pub fn create_promise_resolve_thenable_job(// 5. NOTE: thenRealm is never null. When then.[[Callback]] is a revoked Proxy and no code runs, thenRealm is used to create error objects.// 1. Let job be a new Job Abstract Closure with no parameters that captures promiseToResolve, thenable, and then and performs the following steps when called:- // See run_resolve_thenable_job() for "the following steps".let job = create_heap_function(vm,(promise_to_resolve, thenable, then),- |vm, &(promise_to_resolve, thenable, then)| run_resolve_thenable_job(vm, promise_to_resolve, thenable, then),+ |vm, &(promise_to_resolve, thenable, then)| {+ // a. Return ? PerformPromiseResolveThenable(promiseToResolve, thenable, then).+ perform_promise_resolve_thenable(vm, promise_to_resolve, thenable, ThenAction::JobCallback(then))+ },);// 6. Return the Record { [[Job]]: job, [[Realm]]: thenRealm }.diff --git a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rsindex 67d444d68fd..43db5097c7b 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs@@ -147,6 +147,11 @@ impl PromiseResolvingFunction {})}+ /// Sets [[AlreadyResolved]] to true, and returns whether it was false.+ pub fn mark_already_resolved(&self) -> bool {+ !self.already_resolved_owner().already_resolved.replace(true)+ }+/// The function whose [[AlreadyResolved]] this one uses: the resolve function keeps the bit both share.fn already_resolved_owner(&self) -> Gc<PromiseResolvingFunction> {if self.kind == PromiseResolvingFunctionKind::Resolve {