aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bindings
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2016-06-21 19:35:36 -0400
committerJosh Matthews <josh@joshmatthews.net>2016-09-22 16:16:48 -0400
commita1091772ec9258d4e2c4184e07edab730e4d559c (patch)
treef9a92d79c1d844a288f64c20260dabe5675047a5 /components/script/dom/bindings
parent73b296350927bad6d526cce21434ce68a75216fa (diff)
downloadservo-a1091772ec9258d4e2c4184e07edab730e4d559c.tar.gz
servo-a1091772ec9258d4e2c4184e07edab730e4d559c.zip
Implement binding support for returning and accepting Promises in WebIDL.
Diffstat (limited to 'components/script/dom/bindings')
-rw-r--r--components/script/dom/bindings/codegen/Bindings.conf4
-rw-r--r--components/script/dom/bindings/codegen/CodegenRust.py85
-rw-r--r--components/script/dom/bindings/codegen/Configuration.py12
-rw-r--r--components/script/dom/bindings/global.rs11
-rw-r--r--components/script/dom/bindings/js.rs19
5 files changed, 111 insertions, 20 deletions
diff --git a/components/script/dom/bindings/codegen/Bindings.conf b/components/script/dom/bindings/codegen/Bindings.conf
index 44d835589c4..434450d2bcc 100644
--- a/components/script/dom/bindings/codegen/Bindings.conf
+++ b/components/script/dom/bindings/codegen/Bindings.conf
@@ -14,6 +14,10 @@
DOMInterfaces = {
+'Promise': {
+ 'spiderMonkeyInterface': True,
+},
+
'Range': {
'weakReferenceable': True,
},
diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py
index 7dc3e5eb3f0..9e8d6514327 100644
--- a/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/components/script/dom/bindings/codegen/CodegenRust.py
@@ -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)
-
- 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)
+ 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.
+
+ rooted!(in(cx) let globalObj = CurrentGlobalOrNull(cx));
+ let promiseGlobal = global_root_from_object_maybe_wrapped(globalObj.handle().get());
+
+ let mut valueToResolve = RootedValue::new(cx, $${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)
+
+ 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)
+ 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',
@@ -5442,7 +5487,9 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'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 +5519,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 +5538,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',
diff --git a/components/script/dom/bindings/codegen/Configuration.py b/components/script/dom/bindings/codegen/Configuration.py
index 6e71bd4bd00..835bcbe4774 100644
--- a/components/script/dom/bindings/codegen/Configuration.py
+++ b/components/script/dom/bindings/codegen/Configuration.py
@@ -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
diff --git a/components/script/dom/bindings/global.rs b/components/script/dom/bindings/global.rs
index 9452972f774..baec394b7fb 100644
--- a/components/script/dom/bindings/global.rs
+++ b/components/script/dom/bindings/global.rs
@@ -18,6 +18,7 @@ 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;
@@ -356,3 +357,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)
+}
diff --git a/components/script/dom/bindings/js.rs b/components/script/dom/bindings/js.rs
index a7ecc975129..50cae7e1f04 100644
--- a/components/script/dom/bindings/js.rs
+++ b/components/script/dom/bindings/js.rs
@@ -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
@@ -439,6 +440,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 +459,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())