mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Auto merge of #12830 - jdm:promises, r=Ms2ger
Implement promise bindings This implements support for using Promises in WebIDL, executing promise callbacks in batches (equivalent to running all enqueued jobs from a setTimeout(0)), and attaching native callbacks to promise objects. This is the combined work of myself, @dati91, and @mmatyas based on the following prior work in Gecko: * Codegen.py * Promise.webidl * Promise.cpp/Promise.h * PromiseNativeHandler.h This does not implement microtasks per #4283. This allows us to make progress on testing code that requires the use of Promises right now; the microtasks work is more complicated, but also largely orthogonal to implement. Requires https://github.com/servo/mozjs/pull/89, https://github.com/servo/rust-mozjs/pull/287, and https://github.com/servo/rust-mozjs/pull/294. --- - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #4282 - [X] There are tests for these changes <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12830) <!-- Reviewable:end -->
This commit is contained in:
commit
2b1a39c2ae
133 changed files with 9477 additions and 263 deletions
|
@ -41,7 +41,7 @@ hyper = "0.9.9"
|
|||
hyper_serde = "0.1.4"
|
||||
image = "0.10"
|
||||
ipc-channel = "0.5"
|
||||
js = {git = "https://github.com/servo/rust-mozjs"}
|
||||
js = {git = "https://github.com/servo/rust-mozjs", features = ["promises"]}
|
||||
libc = "0.2"
|
||||
log = "0.3.5"
|
||||
mime = "0.2.1"
|
||||
|
|
|
@ -14,6 +14,10 @@
|
|||
|
||||
DOMInterfaces = {
|
||||
|
||||
'Promise': {
|
||||
'spiderMonkeyInterface': True,
|
||||
},
|
||||
|
||||
'Range': {
|
||||
'weakReferenceable': True,
|
||||
},
|
||||
|
|
|
@ -658,7 +658,7 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
|
|||
# failureCode will prevent pending exceptions from being set in cases when
|
||||
# they really should be!
|
||||
if exceptionCode is None:
|
||||
exceptionCode = "return false;"
|
||||
exceptionCode = "return false;\n"
|
||||
|
||||
if failureCode is None:
|
||||
failOrPropagate = "throw_type_error(cx, &error);\n%s" % exceptionCode
|
||||
|
@ -797,25 +797,69 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
|
|||
descriptorType = descriptor.argumentType
|
||||
|
||||
templateBody = ""
|
||||
if descriptor.interface.isConsequential():
|
||||
raise TypeError("Consequential interface %s being used as an "
|
||||
"argument" % descriptor.interface.identifier.name)
|
||||
isPromise = descriptor.interface.identifier.name == "Promise"
|
||||
if isPromise:
|
||||
# Per spec, what we're supposed to do is take the original
|
||||
# Promise.resolve and call it with the original Promise as this
|
||||
# value to make a Promise out of whatever value we actually have
|
||||
# here. The question is which global we should use. There are
|
||||
# a couple cases to consider:
|
||||
#
|
||||
# 1) Normal call to API with a Promise argument. This is a case the
|
||||
# spec covers, and we should be using the current Realm's
|
||||
# Promise. That means the current compartment.
|
||||
# 2) Promise return value from a callback or callback interface.
|
||||
# This is in theory a case the spec covers but in practice it
|
||||
# really doesn't define behavior here because it doesn't define
|
||||
# what Realm we're in after the callback returns, which is when
|
||||
# the argument conversion happens. We will use the current
|
||||
# compartment, which is the compartment of the callable (which
|
||||
# may itself be a cross-compartment wrapper itself), which makes
|
||||
# as much sense as anything else. In practice, such an API would
|
||||
# once again be providing a Promise to signal completion of an
|
||||
# operation, which would then not be exposed to anyone other than
|
||||
# our own implementation code.
|
||||
templateBody = fill(
|
||||
"""
|
||||
{ // Scope for our JSAutoCompartment.
|
||||
|
||||
if failureCode is None:
|
||||
substitutions = {
|
||||
"sourceDescription": sourceDescription,
|
||||
"interface": descriptor.interface.identifier.name,
|
||||
"exceptionCode": exceptionCode,
|
||||
}
|
||||
unwrapFailureCode = string.Template(
|
||||
'throw_type_error(cx, "${sourceDescription} does not '
|
||||
'implement interface ${interface}.");\n'
|
||||
'${exceptionCode}').substitute(substitutions)
|
||||
rooted!(in(cx) let globalObj = CurrentGlobalOrNull(cx));
|
||||
let promiseGlobal = global_root_from_object_maybe_wrapped(globalObj.handle().get());
|
||||
|
||||
rooted!(in(cx) let mut valueToResolve = $${val}.get());
|
||||
if !JS_WrapValue(cx, valueToResolve.handle_mut()) {
|
||||
$*{exceptionCode}
|
||||
}
|
||||
match Promise::Resolve(promiseGlobal.r(), cx, valueToResolve.handle()) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
throw_dom_exception(cx, promiseGlobal.r(), error);
|
||||
$*{exceptionCode}
|
||||
}
|
||||
}
|
||||
}
|
||||
""",
|
||||
exceptionCode=exceptionCode)
|
||||
else:
|
||||
unwrapFailureCode = failureCode
|
||||
if descriptor.interface.isConsequential():
|
||||
raise TypeError("Consequential interface %s being used as an "
|
||||
"argument" % descriptor.interface.identifier.name)
|
||||
|
||||
templateBody = unwrapCastableObject(
|
||||
descriptor, "${val}", unwrapFailureCode, conversionFunction)
|
||||
if failureCode is None:
|
||||
substitutions = {
|
||||
"sourceDescription": sourceDescription,
|
||||
"interface": descriptor.interface.identifier.name,
|
||||
"exceptionCode": exceptionCode,
|
||||
}
|
||||
unwrapFailureCode = string.Template(
|
||||
'throw_type_error(cx, "${sourceDescription} does not '
|
||||
'implement interface ${interface}.");\n'
|
||||
'${exceptionCode}').substitute(substitutions)
|
||||
else:
|
||||
unwrapFailureCode = failureCode
|
||||
|
||||
templateBody = unwrapCastableObject(
|
||||
descriptor, "${val}", unwrapFailureCode, conversionFunction)
|
||||
|
||||
declType = CGGeneric(descriptorType)
|
||||
if type.nullable():
|
||||
|
@ -5372,6 +5416,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'js::jsapi::AutoIdVector',
|
||||
'js::jsapi::Call',
|
||||
'js::jsapi::CallArgs',
|
||||
'js::jsapi::CurrentGlobalOrNull',
|
||||
'js::jsapi::FreeOp',
|
||||
'js::jsapi::GetPropertyKeys',
|
||||
'js::jsapi::GetWellKnownSymbol',
|
||||
|
@ -5437,12 +5482,15 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'js::jsapi::JS_SetProperty',
|
||||
'js::jsapi::JS_SetReservedSlot',
|
||||
'js::jsapi::JS_SplicePrototype',
|
||||
'js::jsapi::JS_WrapValue',
|
||||
'js::jsapi::MutableHandle',
|
||||
'js::jsapi::MutableHandleObject',
|
||||
'js::jsapi::MutableHandleValue',
|
||||
'js::jsapi::ObjectOpResult',
|
||||
'js::jsapi::PropertyDescriptor',
|
||||
'js::jsapi::RootedId',
|
||||
'js::jsapi::RootedObject',
|
||||
'js::jsapi::RootedString',
|
||||
'js::jsapi::SymbolCode',
|
||||
'js::jsapi::jsid',
|
||||
'js::jsval::JSVal',
|
||||
|
@ -5472,6 +5520,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'dom::bindings::constant::ConstantVal',
|
||||
'dom::bindings::global::GlobalRef',
|
||||
'dom::bindings::global::global_root_from_object',
|
||||
'dom::bindings::global::global_root_from_object_maybe_wrapped',
|
||||
'dom::bindings::global::global_root_from_reflector',
|
||||
'dom::bindings::interface::ConstructorClassHook',
|
||||
'dom::bindings::interface::InterfaceConstructorBehavior',
|
||||
|
@ -5490,6 +5539,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'dom::bindings::js::JS',
|
||||
'dom::bindings::js::OptionalRootedReference',
|
||||
'dom::bindings::js::Root',
|
||||
'dom::bindings::js::RootedRcReference',
|
||||
'dom::bindings::js::RootedReference',
|
||||
'dom::bindings::namespace::NamespaceObjectClass',
|
||||
'dom::bindings::namespace::create_namespace_object',
|
||||
|
|
|
@ -194,9 +194,17 @@ class Descriptor(DescriptorProvider):
|
|||
|
||||
typeName = desc.get('nativeType', nativeTypeDefault)
|
||||
|
||||
# Callback types do not use JS smart pointers, so we should not use the
|
||||
spiderMonkeyInterface = desc.get('spiderMonkeyInterface', False)
|
||||
|
||||
# Callback and SpiderMonkey types do not use JS smart pointers, so we should not use the
|
||||
# built-in rooting mechanisms for them.
|
||||
if self.interface.isCallback():
|
||||
if spiderMonkeyInterface:
|
||||
self.needsRooting = False
|
||||
self.returnType = 'Rc<%s>' % typeName
|
||||
self.argumentType = '&%s' % typeName
|
||||
self.nativeType = typeName
|
||||
pathDefault = 'dom::types::%s' % typeName
|
||||
elif self.interface.isCallback():
|
||||
self.needsRooting = False
|
||||
ty = 'dom::bindings::codegen::Bindings::%sBinding::%s' % (ifaceName, ifaceName)
|
||||
pathDefault = ty
|
||||
|
@ -225,7 +233,8 @@ class Descriptor(DescriptorProvider):
|
|||
# them as having a concrete descendant.
|
||||
self.concrete = (not self.interface.isCallback() and
|
||||
not self.interface.isNamespace() and
|
||||
not self.interface.getExtendedAttribute("Abstract"))
|
||||
not self.interface.getExtendedAttribute("Abstract") and
|
||||
not spiderMonkeyInterface)
|
||||
self.hasUnforgeableMembers = (self.concrete and
|
||||
any(MemberIsUnforgeable(m, self) for m in
|
||||
self.interface.members))
|
||||
|
|
|
@ -19,6 +19,7 @@ use js::jsapi::JS_ErrorFromException;
|
|||
use js::jsapi::JS_GetPendingException;
|
||||
use js::jsapi::JS_IsExceptionPending;
|
||||
use js::jsapi::JS_SetPendingException;
|
||||
use js::jsapi::MutableHandleValue;
|
||||
use js::jsval::UndefinedValue;
|
||||
use libc::c_uint;
|
||||
use std::slice::from_raw_parts;
|
||||
|
@ -266,3 +267,14 @@ pub unsafe fn throw_invalid_this(cx: *mut JSContext, proto_id: u16) {
|
|||
proto_id_to_name(proto_id));
|
||||
throw_type_error(cx, &error);
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Convert this error value to a JS value, consuming it in the process.
|
||||
pub unsafe fn to_jsval(self, cx: *mut JSContext, global: GlobalRef, rval: MutableHandleValue) {
|
||||
assert!(!JS_IsExceptionPending(cx));
|
||||
throw_dom_exception(cx, global, self);
|
||||
assert!(JS_IsExceptionPending(cx));
|
||||
assert!(JS_GetPendingException(cx, rval));
|
||||
JS_ClearPendingException(cx);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,14 @@ use dom::window::{self, ScriptHelpers};
|
|||
use dom::workerglobalscope::WorkerGlobalScope;
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use js::{JSCLASS_IS_DOMJSCLASS, JSCLASS_IS_GLOBAL};
|
||||
use js::glue::{IsWrapper, UnwrapObject};
|
||||
use js::jsapi::{CurrentGlobalOrNull, GetGlobalForObjectCrossCompartment};
|
||||
use js::jsapi::{JSContext, JSObject, JS_GetClass, MutableHandleValue};
|
||||
use js::jsapi::HandleValue;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use net_traits::{CoreResourceThread, IpcSend, ResourceThreads};
|
||||
use profile_traits::{mem, time};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, EnqueuedPromiseCallback};
|
||||
use script_thread::{MainThreadScriptChan, RunnableWrapper, ScriptThread};
|
||||
use script_traits::{MsDuration, ScriptMsg as ConstellationMsg, TimerEventRequest};
|
||||
use task_source::dom_manipulation::DOMManipulationTaskSource;
|
||||
|
@ -289,6 +290,23 @@ impl<'a> GlobalRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Enqueue a promise callback for subsequent execution.
|
||||
pub fn enqueue_promise_job(&self, job: EnqueuedPromiseCallback) {
|
||||
match *self {
|
||||
GlobalRef::Window(_) => ScriptThread::enqueue_promise_job(job, *self),
|
||||
GlobalRef::Worker(ref worker) => worker.enqueue_promise_job(job),
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the process of executing the pending promise callbacks. They will be invoked
|
||||
/// in FIFO order, synchronously, at some point in the future.
|
||||
pub fn flush_promise_jobs(&self) {
|
||||
match *self {
|
||||
GlobalRef::Window(_) => ScriptThread::flush_promise_jobs(*self),
|
||||
GlobalRef::Worker(ref worker) => worker.flush_promise_jobs(),
|
||||
}
|
||||
}
|
||||
|
||||
/// https://html.spec.whatwg.org/multipage/#report-the-error
|
||||
pub fn report_an_error(&self, error_info: ErrorInfo, value: HandleValue) {
|
||||
match *self {
|
||||
|
@ -356,3 +374,13 @@ pub unsafe fn global_root_from_context(cx: *mut JSContext) -> GlobalRoot {
|
|||
let global = CurrentGlobalOrNull(cx);
|
||||
global_root_from_global(global)
|
||||
}
|
||||
|
||||
/// Returns the global object of the realm that the given JS object was created in,
|
||||
/// after unwrapping any wrappers.
|
||||
pub unsafe fn global_root_from_object_maybe_wrapped(mut obj: *mut JSObject) -> GlobalRoot {
|
||||
if IsWrapper(obj) {
|
||||
obj = UnwrapObject(obj, /* stopAtWindowProxy = */ 0);
|
||||
assert!(!obj.is_null());
|
||||
}
|
||||
global_root_from_object(obj)
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ use std::intrinsics::type_name;
|
|||
use std::mem;
|
||||
use std::ops::Deref;
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
use style::thread_state;
|
||||
|
||||
/// A traced reference to a DOM object
|
||||
|
@ -269,6 +270,12 @@ impl MutHeapJSVal {
|
|||
debug_assert!(thread_state::get().is_script());
|
||||
unsafe { (*self.val.get()).get() }
|
||||
}
|
||||
|
||||
/// Get the underlying unsafe pointer to the contained value.
|
||||
pub unsafe fn get_unsafe(&self) -> *mut JSVal {
|
||||
debug_assert!(thread_state::get().is_script());
|
||||
(*self.val.get()).get_unsafe()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -439,6 +446,18 @@ impl<T: Reflectable> LayoutJS<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Get an `&T` out of a `Rc<T>`
|
||||
pub trait RootedRcReference<T> {
|
||||
/// Obtain a safe reference to the wrapped non-JS owned value.
|
||||
fn r(&self) -> &T;
|
||||
}
|
||||
|
||||
impl<T: Reflectable> RootedRcReference<T> for Rc<T> {
|
||||
fn r(&self) -> &T {
|
||||
&*self
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an `Option<&T>` out of an `Option<Root<T>>`
|
||||
pub trait RootedReference<T> {
|
||||
/// Obtain a safe optional reference to the wrapped JS owned-value that
|
||||
|
@ -446,6 +465,12 @@ pub trait RootedReference<T> {
|
|||
fn r(&self) -> Option<&T>;
|
||||
}
|
||||
|
||||
impl<T: Reflectable> RootedReference<T> for Option<Rc<T>> {
|
||||
fn r(&self) -> Option<&T> {
|
||||
self.as_ref().map(|root| &**root)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Reflectable> RootedReference<T> for Option<Root<T>> {
|
||||
fn r(&self) -> Option<&T> {
|
||||
self.as_ref().map(|root| root.r())
|
||||
|
|
|
@ -26,6 +26,7 @@ use core::nonzero::NonZero;
|
|||
use dom::bindings::js::Root;
|
||||
use dom::bindings::reflector::{Reflectable, Reflector};
|
||||
use dom::bindings::trace::trace_reflector;
|
||||
use dom::promise::Promise;
|
||||
use js::jsapi::JSTracer;
|
||||
use libc;
|
||||
use std::cell::RefCell;
|
||||
|
@ -34,6 +35,7 @@ use std::collections::hash_map::HashMap;
|
|||
use std::hash::Hash;
|
||||
use std::marker::PhantomData;
|
||||
use std::os;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
|
||||
|
@ -58,6 +60,63 @@ impl TrustedReference {
|
|||
}
|
||||
}
|
||||
|
||||
/// A safe wrapper around a DOM Promise object that can be shared among threads for use
|
||||
/// in asynchronous operations. The underlying DOM object is guaranteed to live at least
|
||||
/// as long as the last outstanding `TrustedPromise` instance. These values cannot be cloned,
|
||||
/// only created from existing Rc<Promise> values.
|
||||
pub struct TrustedPromise {
|
||||
dom_object: *const Promise,
|
||||
owner_thread: *const libc::c_void,
|
||||
}
|
||||
|
||||
unsafe impl Send for TrustedPromise {}
|
||||
|
||||
impl TrustedPromise {
|
||||
/// Create a new `TrustedPromise` instance from an existing DOM object. The object will
|
||||
/// be prevented from being GCed for the duration of the resulting `TrustedPromise` object's
|
||||
/// lifetime.
|
||||
#[allow(unrooted_must_root)]
|
||||
pub fn new(promise: Rc<Promise>) -> TrustedPromise {
|
||||
LIVE_REFERENCES.with(|ref r| {
|
||||
let r = r.borrow();
|
||||
let live_references = r.as_ref().unwrap();
|
||||
let ptr = &*promise as *const Promise;
|
||||
live_references.addref_promise(promise);
|
||||
TrustedPromise {
|
||||
dom_object: ptr,
|
||||
owner_thread: (&*live_references) as *const _ as *const libc::c_void,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Obtain a usable DOM Promise from a pinned `TrustedPromise` value. Fails if used on
|
||||
/// a different thread than the original value from which this `TrustedPromise` was
|
||||
/// obtained.
|
||||
#[allow(unrooted_must_root)]
|
||||
pub fn root(self) -> Rc<Promise> {
|
||||
LIVE_REFERENCES.with(|ref r| {
|
||||
let r = r.borrow();
|
||||
let live_references = r.as_ref().unwrap();
|
||||
assert!(self.owner_thread == (&*live_references) as *const _ as *const libc::c_void);
|
||||
// Borrow-check error requires the redundant `let promise = ...; promise` here.
|
||||
let promise = match live_references.promise_table.borrow_mut().entry(self.dom_object) {
|
||||
Occupied(mut entry) => {
|
||||
let promise = {
|
||||
let promises = entry.get_mut();
|
||||
promises.pop().expect("rooted promise list unexpectedly empty")
|
||||
};
|
||||
if entry.get().is_empty() {
|
||||
entry.remove();
|
||||
}
|
||||
promise
|
||||
}
|
||||
Vacant(_) => unreachable!(),
|
||||
};
|
||||
promise
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A safe wrapper around a raw pointer to a DOM object that can be
|
||||
/// shared among threads for use in asynchronous operations. The underlying
|
||||
/// DOM object is guaranteed to live at least as long as the last outstanding
|
||||
|
@ -117,9 +176,11 @@ impl<T: Reflectable> Clone for Trusted<T> {
|
|||
|
||||
/// The set of live, pinned DOM objects that are currently prevented
|
||||
/// from being garbage collected due to outstanding references.
|
||||
#[allow(unrooted_must_root)]
|
||||
pub struct LiveDOMReferences {
|
||||
// keyed on pointer to Rust DOM object
|
||||
table: RefCell<HashMap<*const libc::c_void, Weak<TrustedReference>>>,
|
||||
reflectable_table: RefCell<HashMap<*const libc::c_void, Weak<TrustedReference>>>,
|
||||
promise_table: RefCell<HashMap<*const Promise, Vec<Rc<Promise>>>>,
|
||||
}
|
||||
|
||||
impl LiveDOMReferences {
|
||||
|
@ -127,13 +188,20 @@ impl LiveDOMReferences {
|
|||
pub fn initialize() {
|
||||
LIVE_REFERENCES.with(|ref r| {
|
||||
*r.borrow_mut() = Some(LiveDOMReferences {
|
||||
table: RefCell::new(HashMap::new()),
|
||||
reflectable_table: RefCell::new(HashMap::new()),
|
||||
promise_table: RefCell::new(HashMap::new()),
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
fn addref_promise(&self, promise: Rc<Promise>) {
|
||||
let mut table = self.promise_table.borrow_mut();
|
||||
table.entry(&*promise).or_insert(vec![]).push(promise)
|
||||
}
|
||||
|
||||
fn addref<T: Reflectable>(&self, ptr: *const T) -> Arc<TrustedReference> {
|
||||
let mut table = self.table.borrow_mut();
|
||||
let mut table = self.reflectable_table.borrow_mut();
|
||||
let capacity = table.capacity();
|
||||
let len = table.len();
|
||||
if (0 < capacity) && (capacity <= len) {
|
||||
|
@ -173,17 +241,27 @@ fn remove_nulls<K: Eq + Hash + Clone, V> (table: &mut HashMap<K, Weak<V>>) {
|
|||
}
|
||||
|
||||
/// A JSTraceDataOp for tracing reflectors held in LIVE_REFERENCES
|
||||
#[allow(unrooted_must_root)]
|
||||
pub unsafe extern "C" fn trace_refcounted_objects(tracer: *mut JSTracer,
|
||||
_data: *mut os::raw::c_void) {
|
||||
info!("tracing live refcounted references");
|
||||
LIVE_REFERENCES.with(|ref r| {
|
||||
let r = r.borrow();
|
||||
let live_references = r.as_ref().unwrap();
|
||||
let mut table = live_references.table.borrow_mut();
|
||||
remove_nulls(&mut table);
|
||||
for obj in table.keys() {
|
||||
let reflectable = &*(*obj as *const Reflector);
|
||||
trace_reflector(tracer, "refcounted", reflectable);
|
||||
{
|
||||
let mut table = live_references.reflectable_table.borrow_mut();
|
||||
remove_nulls(&mut table);
|
||||
for obj in table.keys() {
|
||||
let reflectable = &*(*obj as *const Reflector);
|
||||
trace_reflector(tracer, "refcounted", reflectable);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let table = live_references.promise_table.borrow_mut();
|
||||
for promise in table.keys() {
|
||||
trace_reflector(tracer, "refcounted", (**promise).reflector());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ use devtools_traits::CSSError;
|
|||
use devtools_traits::WorkerId;
|
||||
use dom::abstractworker::SharedRt;
|
||||
use dom::bindings::js::{JS, Root};
|
||||
use dom::bindings::refcounted::Trusted;
|
||||
use dom::bindings::refcounted::{Trusted, TrustedPromise};
|
||||
use dom::bindings::reflector::{Reflectable, Reflector};
|
||||
use dom::bindings::str::{DOMString, USVString};
|
||||
use dom::bindings::utils::WindowProxyHandler;
|
||||
|
@ -156,7 +156,7 @@ impl<T: JSTraceable> JSTraceable for Rc<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: JSTraceable> JSTraceable for Box<T> {
|
||||
impl<T: JSTraceable + ?Sized> JSTraceable for Box<T> {
|
||||
fn trace(&self, trc: *mut JSTracer) {
|
||||
(**self).trace(trc)
|
||||
}
|
||||
|
@ -303,6 +303,7 @@ no_jsmanaged_fields!(Metadata);
|
|||
no_jsmanaged_fields!(NetworkError);
|
||||
no_jsmanaged_fields!(Atom, Namespace, QualName);
|
||||
no_jsmanaged_fields!(Trusted<T: Reflectable>);
|
||||
no_jsmanaged_fields!(TrustedPromise);
|
||||
no_jsmanaged_fields!(PropertyDeclarationBlock);
|
||||
no_jsmanaged_fields!(HashSet<T>);
|
||||
// These three are interdependent, if you plan to put jsmanaged data
|
||||
|
|
|
@ -373,6 +373,8 @@ pub mod pluginarray;
|
|||
pub mod popstateevent;
|
||||
pub mod processinginstruction;
|
||||
pub mod progressevent;
|
||||
pub mod promise;
|
||||
pub mod promisenativehandler;
|
||||
pub mod radionodelist;
|
||||
pub mod range;
|
||||
pub mod request;
|
||||
|
|
288
components/script/dom/promise.rs
Normal file
288
components/script/dom/promise.rs
Normal file
|
@ -0,0 +1,288 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
//! Native representation of JS Promise values.
|
||||
//!
|
||||
//! This implementation differs from the traditional Rust DOM object, because the reflector
|
||||
//! is provided by SpiderMonkey and has no knowledge of an associated native representation
|
||||
//! (ie. dom::Promise). This means that native instances use native reference counting (Rc)
|
||||
//! to ensure that no memory is leaked, which means that there can be multiple instances of
|
||||
//! native Promise values that refer to the same JS value yet are distinct native objects
|
||||
//! (ie. address equality for the native objects is meaningless).
|
||||
|
||||
use dom::bindings::callback::CallbackContainer;
|
||||
use dom::bindings::codegen::Bindings::PromiseBinding::AnyCallback;
|
||||
use dom::bindings::conversions::root_from_object;
|
||||
use dom::bindings::error::{Error, Fallible};
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::js::MutHeapJSVal;
|
||||
use dom::bindings::reflector::{Reflectable, MutReflectable, Reflector};
|
||||
use dom::promisenativehandler::PromiseNativeHandler;
|
||||
use js::conversions::ToJSValConvertible;
|
||||
use js::jsapi::{CallOriginalPromiseResolve, CallOriginalPromiseReject, CallOriginalPromiseThen};
|
||||
use js::jsapi::{JSAutoCompartment, CallArgs, JS_GetFunctionObject, JS_NewFunction};
|
||||
use js::jsapi::{JSContext, HandleValue, HandleObject, IsPromiseObject, GetFunctionNativeReserved};
|
||||
use js::jsapi::{JS_ClearPendingException, JSObject, AddRawValueRoot, RemoveRawValueRoot};
|
||||
use js::jsapi::{MutableHandleObject, NewPromiseObject, ResolvePromise, RejectPromise};
|
||||
use js::jsapi::{SetFunctionNativeReserved, NewFunctionWithReserved, AddPromiseReactions};
|
||||
use js::jsval::{JSVal, UndefinedValue, ObjectValue, Int32Value};
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct Promise {
|
||||
reflector: Reflector,
|
||||
/// Since Promise values are natively reference counted without the knowledge of
|
||||
/// the SpiderMonkey GC, an explicit root for the reflector is stored while any
|
||||
/// native instance exists. This ensures that the reflector will never be GCed
|
||||
/// while native code could still interact with its native representation.
|
||||
#[ignore_heap_size_of = "SM handles JS values"]
|
||||
permanent_js_root: MutHeapJSVal,
|
||||
}
|
||||
|
||||
/// Private helper to enable adding new methods to Rc<Promise>.
|
||||
trait PromiseHelper {
|
||||
#[allow(unsafe_code)]
|
||||
unsafe fn initialize(&self, cx: *mut JSContext);
|
||||
}
|
||||
|
||||
impl PromiseHelper for Rc<Promise> {
|
||||
#[allow(unsafe_code)]
|
||||
unsafe fn initialize(&self, cx: *mut JSContext) {
|
||||
let obj = self.reflector().get_jsobject();
|
||||
self.permanent_js_root.set(ObjectValue(&**obj));
|
||||
assert!(AddRawValueRoot(cx,
|
||||
self.permanent_js_root.get_unsafe(),
|
||||
b"Promise::root\0" as *const _ as *const _));
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Promise {
|
||||
#[allow(unsafe_code)]
|
||||
fn drop(&mut self) {
|
||||
let cx = self.global().r().get_cx();
|
||||
unsafe {
|
||||
RemoveRawValueRoot(cx, self.permanent_js_root.get_unsafe());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Promise {
|
||||
#[allow(unsafe_code)]
|
||||
pub fn new(global: GlobalRef) -> Rc<Promise> {
|
||||
let cx = global.get_cx();
|
||||
rooted!(in(cx) let mut obj = ptr::null_mut());
|
||||
unsafe {
|
||||
Promise::create_js_promise(cx, HandleObject::null(), obj.handle_mut());
|
||||
Promise::new_with_js_promise(obj.handle(), cx)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code, unrooted_must_root)]
|
||||
pub fn duplicate(&self) -> Rc<Promise> {
|
||||
let cx = self.global().r().get_cx();
|
||||
unsafe {
|
||||
Promise::new_with_js_promise(self.reflector().get_jsobject(), cx)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code, unrooted_must_root)]
|
||||
unsafe fn new_with_js_promise(obj: HandleObject, cx: *mut JSContext) -> Rc<Promise> {
|
||||
assert!(IsPromiseObject(obj));
|
||||
let mut promise = Promise {
|
||||
reflector: Reflector::new(),
|
||||
permanent_js_root: MutHeapJSVal::new(),
|
||||
};
|
||||
promise.init_reflector(obj.get());
|
||||
let promise = Rc::new(promise);
|
||||
promise.initialize(cx);
|
||||
promise
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
unsafe fn create_js_promise(cx: *mut JSContext, proto: HandleObject, obj: MutableHandleObject) {
|
||||
let do_nothing_func = JS_NewFunction(cx, Some(do_nothing_promise_executor), /* nargs = */ 2,
|
||||
/* flags = */ 0, ptr::null());
|
||||
assert!(!do_nothing_func.is_null());
|
||||
rooted!(in(cx) let do_nothing_obj = JS_GetFunctionObject(do_nothing_func));
|
||||
assert!(!do_nothing_obj.is_null());
|
||||
obj.set(NewPromiseObject(cx, do_nothing_obj.handle(), proto));
|
||||
assert!(!obj.is_null());
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root, unsafe_code)]
|
||||
pub fn Resolve(global: GlobalRef,
|
||||
cx: *mut JSContext,
|
||||
value: HandleValue) -> Fallible<Rc<Promise>> {
|
||||
let _ac = JSAutoCompartment::new(cx, global.reflector().get_jsobject().get());
|
||||
rooted!(in(cx) let p = unsafe { CallOriginalPromiseResolve(cx, value) });
|
||||
assert!(!p.handle().is_null());
|
||||
unsafe {
|
||||
Ok(Promise::new_with_js_promise(p.handle(), cx))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root, unsafe_code)]
|
||||
pub fn Reject(global: GlobalRef,
|
||||
cx: *mut JSContext,
|
||||
value: HandleValue) -> Fallible<Rc<Promise>> {
|
||||
let _ac = JSAutoCompartment::new(cx, global.reflector().get_jsobject().get());
|
||||
rooted!(in(cx) let p = unsafe { CallOriginalPromiseReject(cx, value) });
|
||||
assert!(!p.handle().is_null());
|
||||
unsafe {
|
||||
Ok(Promise::new_with_js_promise(p.handle(), cx))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn resolve_native<T>(&self, cx: *mut JSContext, val: &T) where T: ToJSValConvertible {
|
||||
rooted!(in(cx) let mut v = UndefinedValue());
|
||||
unsafe {
|
||||
val.to_jsval(cx, v.handle_mut());
|
||||
}
|
||||
self.resolve(cx, v.handle());
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root, unsafe_code)]
|
||||
pub fn resolve(&self, cx: *mut JSContext, value: HandleValue) {
|
||||
unsafe {
|
||||
if !ResolvePromise(cx, self.promise_obj(), value) {
|
||||
JS_ClearPendingException(cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn reject_native<T>(&self, cx: *mut JSContext, val: &T) where T: ToJSValConvertible {
|
||||
rooted!(in(cx) let mut v = UndefinedValue());
|
||||
unsafe {
|
||||
val.to_jsval(cx, v.handle_mut());
|
||||
}
|
||||
self.reject(cx, v.handle());
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn reject_error(&self, cx: *mut JSContext, error: Error) {
|
||||
rooted!(in(cx) let mut v = UndefinedValue());
|
||||
unsafe {
|
||||
error.to_jsval(cx, self.global().r(), v.handle_mut());
|
||||
}
|
||||
self.reject(cx, v.handle());
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root, unsafe_code)]
|
||||
pub fn reject(&self,
|
||||
cx: *mut JSContext,
|
||||
value: HandleValue) {
|
||||
unsafe {
|
||||
if !RejectPromise(cx, self.promise_obj(), value) {
|
||||
JS_ClearPendingException(cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root, unsafe_code)]
|
||||
pub fn then(&self,
|
||||
cx: *mut JSContext,
|
||||
_callee: HandleObject,
|
||||
cb_resolve: AnyCallback,
|
||||
cb_reject: AnyCallback,
|
||||
result: MutableHandleObject) {
|
||||
let promise = self.promise_obj();
|
||||
rooted!(in(cx) let resolve = cb_resolve.callback());
|
||||
rooted!(in(cx) let reject = cb_reject.callback());
|
||||
unsafe {
|
||||
rooted!(in(cx) let res =
|
||||
CallOriginalPromiseThen(cx, promise, resolve.handle(), reject.handle()));
|
||||
result.set(*res);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
fn promise_obj(&self) -> HandleObject {
|
||||
let obj = self.reflector().get_jsobject();
|
||||
unsafe {
|
||||
assert!(IsPromiseObject(obj));
|
||||
}
|
||||
obj
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn append_native_handler(&self, handler: &PromiseNativeHandler) {
|
||||
let global = self.global();
|
||||
let cx = global.r().get_cx();
|
||||
rooted!(in(cx) let resolve_func =
|
||||
create_native_handler_function(cx,
|
||||
handler.reflector().get_jsobject(),
|
||||
NativeHandlerTask::Resolve));
|
||||
|
||||
rooted!(in(cx) let reject_func =
|
||||
create_native_handler_function(cx,
|
||||
handler.reflector().get_jsobject(),
|
||||
NativeHandlerTask::Reject));
|
||||
|
||||
unsafe {
|
||||
let ok = AddPromiseReactions(cx,
|
||||
self.promise_obj(),
|
||||
resolve_func.handle(),
|
||||
reject_func.handle());
|
||||
assert!(ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
unsafe extern fn do_nothing_promise_executor(_cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
|
||||
let args = CallArgs::from_vp(vp, argc);
|
||||
*args.rval() = UndefinedValue();
|
||||
true
|
||||
}
|
||||
|
||||
const SLOT_NATIVEHANDLER: usize = 0;
|
||||
const SLOT_NATIVEHANDLER_TASK: usize = 1;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum NativeHandlerTask {
|
||||
Resolve = 0,
|
||||
Reject = 1,
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
unsafe extern fn native_handler_callback(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
|
||||
let args = CallArgs::from_vp(vp, argc);
|
||||
rooted!(in(cx) let v = *GetFunctionNativeReserved(args.callee(), SLOT_NATIVEHANDLER));
|
||||
assert!(v.get().is_object());
|
||||
|
||||
let handler = root_from_object::<PromiseNativeHandler>(v.to_object())
|
||||
.ok().expect("unexpected value for native handler in promise native handler callback");
|
||||
|
||||
rooted!(in(cx) let v = *GetFunctionNativeReserved(args.callee(), SLOT_NATIVEHANDLER_TASK));
|
||||
match v.to_int32() {
|
||||
v if v == NativeHandlerTask::Resolve as i32 => handler.resolved_callback(cx, args.get(0)),
|
||||
v if v == NativeHandlerTask::Reject as i32 => handler.rejected_callback(cx, args.get(0)),
|
||||
_ => panic!("unexpected native handler task value"),
|
||||
};
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
fn create_native_handler_function(cx: *mut JSContext,
|
||||
holder: HandleObject,
|
||||
task: NativeHandlerTask) -> *mut JSObject {
|
||||
unsafe {
|
||||
let func = NewFunctionWithReserved(cx, Some(native_handler_callback), 1, 0, ptr::null());
|
||||
assert!(!func.is_null());
|
||||
|
||||
rooted!(in(cx) let obj = JS_GetFunctionObject(func));
|
||||
assert!(!obj.is_null());
|
||||
SetFunctionNativeReserved(obj.get(),
|
||||
SLOT_NATIVEHANDLER,
|
||||
&ObjectValue(&**holder));
|
||||
SetFunctionNativeReserved(obj.get(),
|
||||
SLOT_NATIVEHANDLER_TASK,
|
||||
&Int32Value(task as i32));
|
||||
obj.get()
|
||||
}
|
||||
}
|
49
components/script/dom/promisenativehandler.rs
Normal file
49
components/script/dom/promisenativehandler.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::codegen::Bindings::PromiseNativeHandlerBinding;
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::js::Root;
|
||||
use dom::bindings::reflector::{Reflector, reflect_dom_object};
|
||||
use dom::bindings::trace::JSTraceable;
|
||||
use heapsize::HeapSizeOf;
|
||||
use js::jsapi::{JSContext, HandleValue};
|
||||
|
||||
pub trait Callback: JSTraceable + HeapSizeOf {
|
||||
fn callback(&self, cx: *mut JSContext, v: HandleValue);
|
||||
}
|
||||
|
||||
#[dom_struct]
|
||||
pub struct PromiseNativeHandler {
|
||||
reflector: Reflector,
|
||||
resolve: Option<Box<Callback>>,
|
||||
reject: Option<Box<Callback>>,
|
||||
}
|
||||
|
||||
impl PromiseNativeHandler {
|
||||
pub fn new(global: GlobalRef,
|
||||
resolve: Option<Box<Callback>>,
|
||||
reject: Option<Box<Callback>>)
|
||||
-> Root<PromiseNativeHandler> {
|
||||
reflect_dom_object(box PromiseNativeHandler {
|
||||
reflector: Reflector::new(),
|
||||
resolve: resolve,
|
||||
reject: reject,
|
||||
}, global, PromiseNativeHandlerBinding::Wrap)
|
||||
}
|
||||
|
||||
fn callback(callback: &Option<Box<Callback>>, cx: *mut JSContext, v: HandleValue) {
|
||||
if let Some(ref callback) = *callback {
|
||||
callback.callback(cx, v)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolved_callback(&self, cx: *mut JSContext, v: HandleValue) {
|
||||
PromiseNativeHandler::callback(&self.resolve, cx, v)
|
||||
}
|
||||
|
||||
pub fn rejected_callback(&self, cx: *mut JSContext, v: HandleValue) {
|
||||
PromiseNativeHandler::callback(&self.reject, cx, v)
|
||||
}
|
||||
}
|
|
@ -5,9 +5,10 @@
|
|||
// check-tidy: no specs after this line
|
||||
|
||||
use core::nonzero::NonZero;
|
||||
use dom::bindings::callback::ExceptionHandling;
|
||||
use dom::bindings::codegen::Bindings::EventListenerBinding::EventListener;
|
||||
use dom::bindings::codegen::Bindings::FunctionBinding::Function;
|
||||
use dom::bindings::codegen::Bindings::TestBindingBinding;
|
||||
use dom::bindings::codegen::Bindings::TestBindingBinding::{self, SimpleCallback};
|
||||
use dom::bindings::codegen::Bindings::TestBindingBinding::{TestBindingMethods, TestDictionary};
|
||||
use dom::bindings::codegen::Bindings::TestBindingBinding::{TestDictionaryDefaults, TestEnum};
|
||||
use dom::bindings::codegen::UnionTypes;
|
||||
|
@ -19,22 +20,27 @@ use dom::bindings::codegen::UnionTypes::{EventOrUSVString, HTMLElementOrLong, Lo
|
|||
use dom::bindings::codegen::UnionTypes::{HTMLElementOrUnsignedLongOrStringOrBoolean, LongSequenceOrBoolean};
|
||||
use dom::bindings::codegen::UnionTypes::{StringOrLongSequence, StringOrStringSequence, StringSequenceOrUnsignedLong};
|
||||
use dom::bindings::codegen::UnionTypes::{StringOrUnsignedLong, StringOrBoolean, UnsignedLongOrBoolean};
|
||||
use dom::bindings::error::Fallible;
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::error::{Error, Fallible};
|
||||
use dom::bindings::global::{GlobalRef, global_root_from_context};
|
||||
use dom::bindings::js::Root;
|
||||
use dom::bindings::mozmap::MozMap;
|
||||
use dom::bindings::num::Finite;
|
||||
use dom::bindings::refcounted::TrustedPromise;
|
||||
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
||||
use dom::bindings::str::{ByteString, DOMString, USVString};
|
||||
use dom::bindings::weakref::MutableWeakRef;
|
||||
use dom::blob::{Blob, BlobImpl};
|
||||
use dom::promise::Promise;
|
||||
use dom::promisenativehandler::{PromiseNativeHandler, Callback};
|
||||
use dom::url::URL;
|
||||
use js::jsapi::{HandleObject, HandleValue, JSContext, JSObject};
|
||||
use js::jsapi::{HandleObject, HandleValue, JSContext, JSObject, JSAutoCompartment};
|
||||
use js::jsapi::{JS_NewPlainObject, JS_NewUint8ClampedArray};
|
||||
use js::jsval::{JSVal, NullValue};
|
||||
use script_traits::MsDuration;
|
||||
use std::borrow::ToOwned;
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
use timers::OneshotTimerCallback;
|
||||
use util::prefs::PREFS;
|
||||
|
||||
#[dom_struct]
|
||||
|
@ -647,6 +653,81 @@ impl TestBindingMethods for TestBinding {
|
|||
fn ReceiveMozMapOfMozMaps(&self) -> MozMap<MozMap<i32>> { MozMap::new() }
|
||||
fn ReceiveAnyMozMap(&self) -> MozMap<JSVal> { MozMap::new() }
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
fn ReturnResolvedPromise(&self, cx: *mut JSContext, v: HandleValue) -> Fallible<Rc<Promise>> {
|
||||
Promise::Resolve(self.global().r(), cx, v)
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
fn ReturnRejectedPromise(&self, cx: *mut JSContext, v: HandleValue) -> Fallible<Rc<Promise>> {
|
||||
Promise::Reject(self.global().r(), cx, v)
|
||||
}
|
||||
|
||||
fn PromiseResolveNative(&self, cx: *mut JSContext, p: &Promise, v: HandleValue) {
|
||||
p.resolve(cx, v);
|
||||
}
|
||||
|
||||
fn PromiseRejectNative(&self, cx: *mut JSContext, p: &Promise, v: HandleValue) {
|
||||
p.reject(cx, v);
|
||||
}
|
||||
|
||||
fn PromiseRejectWithTypeError(&self, p: &Promise, s: USVString) {
|
||||
p.reject_error(self.global().r().get_cx(), Error::Type(s.0));
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
fn ResolvePromiseDelayed(&self, p: &Promise, value: DOMString, delay: u64) {
|
||||
let promise = p.duplicate();
|
||||
let cb = TestBindingCallback {
|
||||
promise: TrustedPromise::new(promise),
|
||||
value: value,
|
||||
};
|
||||
let _ = self.global().r().schedule_callback(OneshotTimerCallback::TestBindingCallback(cb),
|
||||
MsDuration::new(delay));
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
fn PromiseNativeHandler(&self,
|
||||
resolve: Option<Rc<SimpleCallback>>,
|
||||
reject: Option<Rc<SimpleCallback>>) -> Rc<Promise> {
|
||||
let global = self.global();
|
||||
let handler = PromiseNativeHandler::new(global.r(),
|
||||
resolve.map(SimpleHandler::new),
|
||||
reject.map(SimpleHandler::new));
|
||||
let p = Promise::new(global.r());
|
||||
p.append_native_handler(&handler);
|
||||
return p;
|
||||
|
||||
#[derive(JSTraceable, HeapSizeOf)]
|
||||
struct SimpleHandler {
|
||||
#[ignore_heap_size_of = "Rc has unclear ownership semantics"]
|
||||
handler: Rc<SimpleCallback>,
|
||||
}
|
||||
impl SimpleHandler {
|
||||
fn new(callback: Rc<SimpleCallback>) -> Box<Callback> {
|
||||
box SimpleHandler { handler: callback }
|
||||
}
|
||||
}
|
||||
impl Callback for SimpleHandler {
|
||||
#[allow(unsafe_code)]
|
||||
fn callback(&self, cx: *mut JSContext, v: HandleValue) {
|
||||
let global = unsafe { global_root_from_context(cx) };
|
||||
let _ = self.handler.Call_(&global.r(), v, ExceptionHandling::Report);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
fn PromiseAttribute(&self) -> Rc<Promise> {
|
||||
Promise::new(self.global().r())
|
||||
}
|
||||
|
||||
fn AcceptPromise(&self, _promise: &Promise) {
|
||||
}
|
||||
|
||||
fn AcceptNullablePromise(&self, _promise: Option<&Promise>) {
|
||||
}
|
||||
|
||||
fn PassSequenceSequence(&self, _seq: Vec<Vec<i32>>) {}
|
||||
fn ReturnSequenceSequence(&self) -> Vec<Vec<i32>> { vec![] }
|
||||
fn PassUnionSequenceSequence(&self, seq: LongOrLongSequenceSequence) {
|
||||
|
@ -693,3 +774,20 @@ impl TestBinding {
|
|||
pub unsafe fn condition_satisfied(_: *mut JSContext, _: HandleObject) -> bool { true }
|
||||
pub unsafe fn condition_unsatisfied(_: *mut JSContext, _: HandleObject) -> bool { false }
|
||||
}
|
||||
|
||||
#[derive(JSTraceable, HeapSizeOf)]
|
||||
pub struct TestBindingCallback {
|
||||
#[ignore_heap_size_of = "unclear ownership semantics"]
|
||||
promise: TrustedPromise,
|
||||
value: DOMString,
|
||||
}
|
||||
|
||||
impl TestBindingCallback {
|
||||
#[allow(unrooted_must_root)]
|
||||
pub fn invoke(self) {
|
||||
let p = self.promise.root();
|
||||
let cx = p.global().r().get_cx();
|
||||
let _ac = JSAutoCompartment::new(cx, p.reflector().get_jsobject().get());
|
||||
p.resolve_native(cx, &self.value);
|
||||
}
|
||||
}
|
||||
|
|
16
components/script/dom/webidls/Promise.webidl
Normal file
16
components/script/dom/webidls/Promise.webidl
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// This interface is entirely internal to Servo, and should not be accessible to
|
||||
// web pages.
|
||||
|
||||
callback PromiseJobCallback = void();
|
||||
|
||||
[TreatNonCallableAsNull]
|
||||
callback AnyCallback = any (any value);
|
||||
|
||||
[NoInterfaceObject, Exposed=(Window,Worker)]
|
||||
// Need to escape "Promise" so it's treated as an identifier.
|
||||
interface _Promise {
|
||||
};
|
13
components/script/dom/webidls/PromiseNativeHandler.webidl
Normal file
13
components/script/dom/webidls/PromiseNativeHandler.webidl
Normal file
|
@ -0,0 +1,13 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// This interface is entirely internal to Servo, and should not be accessible to
|
||||
// web pages.
|
||||
|
||||
// Hack to allow us to have JS owning and properly tracing/CCing/etc a
|
||||
// PromiseNativeHandler.
|
||||
[NoInterfaceObject,
|
||||
Exposed=(Window,Worker)]
|
||||
interface PromiseNativeHandler {
|
||||
};
|
|
@ -508,9 +508,24 @@ interface TestBinding {
|
|||
[Func="TestBinding::condition_satisfied"]
|
||||
const unsigned short funcControlledConstEnabled = 0;
|
||||
|
||||
[Throws]
|
||||
Promise<any> returnResolvedPromise(any value);
|
||||
[Throws]
|
||||
Promise<any> returnRejectedPromise(any value);
|
||||
readonly attribute Promise<boolean> promiseAttribute;
|
||||
void acceptPromise(Promise<DOMString> string);
|
||||
void acceptNullablePromise(Promise<DOMString>? string);
|
||||
Promise<any> promiseNativeHandler(SimpleCallback? resolve, SimpleCallback? reject);
|
||||
void promiseResolveNative(Promise<any> p, any value);
|
||||
void promiseRejectNative(Promise<any> p, any value);
|
||||
void promiseRejectWithTypeError(Promise<any> p, USVString message);
|
||||
void resolvePromiseDelayed(Promise<any> p, DOMString value, unsigned long long ms);
|
||||
|
||||
void panic();
|
||||
};
|
||||
|
||||
callback SimpleCallback = void(any value);
|
||||
|
||||
partial interface TestBinding {
|
||||
[Pref="dom.testable_crash.enabled"]
|
||||
void crashHard();
|
||||
|
|
|
@ -7,9 +7,10 @@ use dom::bindings::codegen::Bindings::EventHandlerBinding::OnErrorEventHandlerNo
|
|||
use dom::bindings::codegen::Bindings::FunctionBinding::Function;
|
||||
use dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
|
||||
use dom::bindings::error::{Error, ErrorResult, Fallible, report_pending_exception, ErrorInfo};
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::global::{GlobalRef, GlobalRoot};
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::js::{JS, MutNullableHeap, Root};
|
||||
use dom::bindings::refcounted::Trusted;
|
||||
use dom::bindings::reflector::Reflectable;
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::console::TimerSet;
|
||||
|
@ -29,7 +30,8 @@ use net_traits::{IpcSend, LoadOrigin};
|
|||
use net_traits::{LoadContext, ResourceThreads, load_whole_resource};
|
||||
use profile_traits::{mem, time};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, maybe_take_panic_result};
|
||||
use script_thread::RunnableWrapper;
|
||||
use script_runtime::{ScriptThreadEventCategory, PromiseJobQueue, EnqueuedPromiseCallback};
|
||||
use script_thread::{Runnable, RunnableWrapper};
|
||||
use script_traits::{MsDuration, TimerEvent, TimerEventId, TimerEventRequest, TimerSource};
|
||||
use script_traits::ScriptMsg as ConstellationMsg;
|
||||
use script_traits::WorkerGlobalScopeInit;
|
||||
|
@ -112,6 +114,8 @@ pub struct WorkerGlobalScope {
|
|||
|
||||
/// Timers used by the Console API.
|
||||
console_timers: TimerSet,
|
||||
|
||||
promise_job_queue: PromiseJobQueue,
|
||||
}
|
||||
|
||||
impl WorkerGlobalScope {
|
||||
|
@ -143,6 +147,7 @@ impl WorkerGlobalScope {
|
|||
constellation_chan: init.constellation_chan,
|
||||
scheduler_chan: init.scheduler_chan,
|
||||
console_timers: TimerSet::new(),
|
||||
promise_job_queue: PromiseJobQueue::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -228,6 +233,25 @@ impl WorkerGlobalScope {
|
|||
cancelled: self.closing.clone().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enqueue_promise_job(&self, job: EnqueuedPromiseCallback) {
|
||||
self.promise_job_queue.enqueue(job, GlobalRef::Worker(self));
|
||||
}
|
||||
|
||||
pub fn flush_promise_jobs(&self) {
|
||||
self.script_chan().send(CommonScriptMsg::RunnableMsg(
|
||||
ScriptThreadEventCategory::WorkerEvent,
|
||||
box FlushPromiseJobs {
|
||||
global: Trusted::new(self),
|
||||
})).unwrap();
|
||||
}
|
||||
|
||||
fn do_flush_promise_jobs(&self) {
|
||||
self.promise_job_queue.flush_promise_jobs(|id| {
|
||||
assert_eq!(self.pipeline_id(), id);
|
||||
Some(GlobalRoot::Worker(Root::from_ref(self)))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadOrigin for WorkerGlobalScope {
|
||||
|
@ -466,3 +490,14 @@ impl WorkerGlobalScope {
|
|||
.report_an_error(error_info, value);
|
||||
}
|
||||
}
|
||||
|
||||
struct FlushPromiseJobs {
|
||||
global: Trusted<WorkerGlobalScope>,
|
||||
}
|
||||
|
||||
impl Runnable for FlushPromiseJobs {
|
||||
fn handler(self: Box<FlushPromiseJobs>) {
|
||||
let global = self.global.root();
|
||||
global.do_flush_promise_jobs();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,18 +5,23 @@
|
|||
//! The script runtime contains common traits and structs commonly used by the
|
||||
//! script thread, the dom, and the worker threads.
|
||||
|
||||
use dom::bindings::callback::ExceptionHandling;
|
||||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::codegen::Bindings::PromiseBinding::PromiseJobCallback;
|
||||
use dom::bindings::global::{global_root_from_object, GlobalRoot, GlobalRef};
|
||||
use dom::bindings::js::{RootCollection, RootCollectionPtr, trace_roots};
|
||||
use dom::bindings::refcounted::{LiveDOMReferences, trace_refcounted_objects};
|
||||
use dom::bindings::trace::trace_traceables;
|
||||
use dom::bindings::utils::DOM_CALLBACKS;
|
||||
use js::glue::CollectServoSizes;
|
||||
use js::jsapi::{DisableIncrementalGC, GCDescription, GCProgress};
|
||||
use js::jsapi::{DisableIncrementalGC, GCDescription, GCProgress, HandleObject};
|
||||
use js::jsapi::{JSContext, JS_GetRuntime, JSRuntime, JSTracer, SetDOMCallbacks, SetGCSliceCallback};
|
||||
use js::jsapi::{JSGCInvocationKind, JSGCStatus, JS_AddExtraGCRootsTracer, JS_SetGCCallback};
|
||||
use js::jsapi::{JSGCMode, JSGCParamKey, JS_SetGCParameter, JS_SetGlobalJitCompilerOption};
|
||||
use js::jsapi::{JSJitCompilerOption, JS_SetOffthreadIonCompilationEnabled, JS_SetParallelParsingEnabled};
|
||||
use js::jsapi::{JSObject, RuntimeOptionsRef, SetPreserveWrapperCallback};
|
||||
use js::jsapi::{JSObject, RuntimeOptionsRef, SetPreserveWrapperCallback, SetEnqueuePromiseJobCallback};
|
||||
use js::rust::Runtime;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use profile_traits::mem::{Report, ReportKind, ReportsChan};
|
||||
use script_thread::{Runnable, STACK_ROOTS, trace_thread};
|
||||
use std::any::Any;
|
||||
|
@ -24,7 +29,10 @@ use std::cell::{RefCell, Cell};
|
|||
use std::io::{Write, stdout};
|
||||
use std::marker::PhantomData;
|
||||
use std::os;
|
||||
use std::os::raw::c_void;
|
||||
use std::panic::{self, AssertUnwindSafe};
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
use style::thread_state;
|
||||
use time::{Tm, now};
|
||||
use util::opts;
|
||||
|
@ -95,6 +103,97 @@ impl<'a> Drop for StackRootTLS<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// A promise callback scheduled to run during the next microtask checkpoint (#4283).
|
||||
#[derive(JSTraceable, HeapSizeOf)]
|
||||
pub struct EnqueuedPromiseCallback {
|
||||
#[ignore_heap_size_of = "Rc has unclear ownership"]
|
||||
callback: Rc<PromiseJobCallback>,
|
||||
pipeline: PipelineId,
|
||||
}
|
||||
|
||||
/// A collection of promise callbacks in FIFO order.
|
||||
#[derive(JSTraceable, HeapSizeOf)]
|
||||
pub struct PromiseJobQueue {
|
||||
/// A snapshot of `promise_job_queue` that was taken at the start of the microtask checkpoint.
|
||||
/// Used to work around mutability errors when appending new promise jobs while performing
|
||||
/// a microtask checkpoint.
|
||||
flushing_job_queue: DOMRefCell<Vec<EnqueuedPromiseCallback>>,
|
||||
/// The list of enqueued promise callbacks that will be invoked at the next microtask checkpoint.
|
||||
promise_job_queue: DOMRefCell<Vec<EnqueuedPromiseCallback>>,
|
||||
/// True if there is an outstanding runnable responsible for evaluating the promise job queue.
|
||||
/// This prevents runnables flooding the event queue needlessly, since the first one will
|
||||
/// execute all pending runnables.
|
||||
pending_promise_job_runnable: Cell<bool>,
|
||||
}
|
||||
|
||||
impl PromiseJobQueue {
|
||||
/// Create a new PromiseJobQueue instance.
|
||||
pub fn new() -> PromiseJobQueue {
|
||||
PromiseJobQueue {
|
||||
promise_job_queue: DOMRefCell::new(vec![]),
|
||||
flushing_job_queue: DOMRefCell::new(vec![]),
|
||||
pending_promise_job_runnable: Cell::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a new promise job callback to this queue. It will be invoked as part of the next
|
||||
/// microtask checkpoint.
|
||||
pub fn enqueue(&self, job: EnqueuedPromiseCallback, global: GlobalRef) {
|
||||
self.promise_job_queue.borrow_mut().push(job);
|
||||
if !self.pending_promise_job_runnable.get() {
|
||||
self.pending_promise_job_runnable.set(true);
|
||||
global.flush_promise_jobs();
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform a microtask checkpoint, by invoking all of the pending promise job callbacks in
|
||||
/// FIFO order (#4283).
|
||||
pub fn flush_promise_jobs<F>(&self, target_provider: F)
|
||||
where F: Fn(PipelineId) -> Option<GlobalRoot>
|
||||
{
|
||||
self.pending_promise_job_runnable.set(false);
|
||||
{
|
||||
let mut pending_queue = self.promise_job_queue.borrow_mut();
|
||||
*self.flushing_job_queue.borrow_mut() = pending_queue.drain(..).collect();
|
||||
}
|
||||
// N.B. borrowing this vector is safe w.r.t. mutability, since any promise job that
|
||||
// is enqueued while invoking these callbacks will be placed in `pending_queue`;
|
||||
// `flushing_queue` is a static snapshot during this checkpoint.
|
||||
for job in &*self.flushing_job_queue.borrow() {
|
||||
if let Some(target) = target_provider(job.pipeline) {
|
||||
let _ = job.callback.Call_(&target.r(), ExceptionHandling::Report);
|
||||
}
|
||||
}
|
||||
self.flushing_job_queue.borrow_mut().clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// SM callback for promise job resolution. Adds a promise callback to the current global's
|
||||
/// promise job queue, and enqueues a runnable to perform a microtask checkpoint if one
|
||||
/// is not already pending.
|
||||
#[allow(unsafe_code)]
|
||||
unsafe extern "C" fn enqueue_job(_cx: *mut JSContext,
|
||||
job: HandleObject,
|
||||
_allocation_site: HandleObject,
|
||||
_data: *mut c_void) -> bool {
|
||||
let result = panic::catch_unwind(AssertUnwindSafe(|| {
|
||||
let global = global_root_from_object(job.get());
|
||||
let pipeline = global.r().pipeline_id();
|
||||
global.r().enqueue_promise_job(EnqueuedPromiseCallback {
|
||||
callback: PromiseJobCallback::new(job.get()),
|
||||
pipeline: pipeline,
|
||||
});
|
||||
true
|
||||
}));
|
||||
match result {
|
||||
Ok(result) => result,
|
||||
Err(error) => {
|
||||
store_panic_result(error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub unsafe fn new_rt_and_cx() -> Runtime {
|
||||
LiveDOMReferences::initialize();
|
||||
|
@ -118,6 +217,8 @@ pub unsafe fn new_rt_and_cx() -> Runtime {
|
|||
// Pre barriers aren't working correctly at the moment
|
||||
DisableIncrementalGC(runtime.rt());
|
||||
|
||||
SetEnqueuePromiseJobCallback(runtime.rt(), Some(enqueue_job), ptr::null_mut());
|
||||
|
||||
set_gc_zeal_options(runtime.rt());
|
||||
|
||||
// Enable or disable the JITs.
|
||||
|
|
|
@ -27,7 +27,7 @@ use dom::bindings::codegen::Bindings::DocumentBinding::{DocumentMethods, Documen
|
|||
use dom::bindings::codegen::Bindings::LocationBinding::LocationMethods;
|
||||
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
|
||||
use dom::bindings::conversions::{ConversionResult, FromJSValConvertible, StringificationBehavior};
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::global::{GlobalRef, GlobalRoot};
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::js::{JS, MutNullableHeap, Root, RootCollection};
|
||||
use dom::bindings::js::{RootCollectionPtr, RootedReference};
|
||||
|
@ -77,8 +77,8 @@ use parse::xml::{self, parse_xml};
|
|||
use profile_traits::mem::{self, OpaqueSender, Report, ReportKind, ReportsChan};
|
||||
use profile_traits::time::{self, ProfilerCategory, profile};
|
||||
use script_layout_interface::message::{self, NewLayoutThreadInfo, ReflowQueryType};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory};
|
||||
use script_runtime::{ScriptPort, StackRootTLS, get_reports, new_rt_and_cx};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory, EnqueuedPromiseCallback};
|
||||
use script_runtime::{ScriptPort, StackRootTLS, get_reports, new_rt_and_cx, PromiseJobQueue};
|
||||
use script_traits::{CompositorEvent, ConstellationControlMsg, EventResult};
|
||||
use script_traits::{InitialScriptState, MouseButton, MouseEventType, MozBrowserEvent};
|
||||
use script_traits::{NewLayoutInfo, ScriptMsg as ConstellationMsg};
|
||||
|
@ -394,6 +394,8 @@ pub struct ScriptThread {
|
|||
timer_event_port: Receiver<TimerEvent>,
|
||||
|
||||
content_process_shutdown_chan: IpcSender<()>,
|
||||
|
||||
promise_job_queue: PromiseJobQueue,
|
||||
}
|
||||
|
||||
/// In the event of thread panic, all data on the stack runs its destructor. However, there
|
||||
|
@ -599,6 +601,8 @@ impl ScriptThread {
|
|||
timer_event_port: timer_event_port,
|
||||
|
||||
content_process_shutdown_chan: state.content_process_shutdown_chan,
|
||||
|
||||
promise_job_queue: PromiseJobQueue::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2174,6 +2178,33 @@ impl ScriptThread {
|
|||
location.Reload();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enqueue_promise_job(job: EnqueuedPromiseCallback, global: GlobalRef) {
|
||||
SCRIPT_THREAD_ROOT.with(|root| {
|
||||
let script_thread = unsafe { &*root.get().unwrap() };
|
||||
script_thread.promise_job_queue.enqueue(job, global);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn flush_promise_jobs(global: GlobalRef) {
|
||||
SCRIPT_THREAD_ROOT.with(|root| {
|
||||
let script_thread = unsafe { &*root.get().unwrap() };
|
||||
let _ = script_thread.dom_manipulation_task_source.queue(box FlushPromiseJobs, global);
|
||||
})
|
||||
}
|
||||
|
||||
fn do_flush_promise_jobs(&self) {
|
||||
self.promise_job_queue.flush_promise_jobs(|id| {
|
||||
self.find_child_context(id).map(|context| GlobalRoot::Window(context.active_window()))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct FlushPromiseJobs;
|
||||
impl Runnable for FlushPromiseJobs {
|
||||
fn main_thread_handler(self: Box<FlushPromiseJobs>, script_thread: &ScriptThread) {
|
||||
script_thread.do_flush_promise_jobs();
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ScriptThread {
|
||||
|
|
|
@ -8,6 +8,7 @@ use dom::bindings::codegen::Bindings::FunctionBinding::Function;
|
|||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::reflector::Reflectable;
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::testbinding::TestBindingCallback;
|
||||
use dom::window::ScriptHelpers;
|
||||
use dom::xmlhttprequest::XHRTimeoutCallback;
|
||||
use euclid::length::Length;
|
||||
|
@ -68,6 +69,7 @@ struct OneshotTimer {
|
|||
pub enum OneshotTimerCallback {
|
||||
XhrTimeout(XHRTimeoutCallback),
|
||||
JsTimer(JsTimerTask),
|
||||
TestBindingCallback(TestBindingCallback),
|
||||
}
|
||||
|
||||
impl OneshotTimerCallback {
|
||||
|
@ -75,6 +77,7 @@ impl OneshotTimerCallback {
|
|||
match self {
|
||||
OneshotTimerCallback::XhrTimeout(callback) => callback.invoke(),
|
||||
OneshotTimerCallback::JsTimer(task) => task.invoke(this, js_timers),
|
||||
OneshotTimerCallback::TestBindingCallback(callback) => callback.invoke(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue