diff --git a/components/script/dom/testbinding.rs b/components/script/dom/testbinding.rs index 3ac2e7c2c264..26a79a029ff1 100644 --- a/components/script/dom/testbinding.rs +++ b/components/script/dom/testbinding.rs @@ -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; @@ -20,7 +21,7 @@ use dom::bindings::codegen::UnionTypes::{HTMLElementOrUnsignedLongOrStringOrBool 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::global::{GlobalRef, global_root_from_context}; use dom::bindings::js::Root; use dom::bindings::mozmap::MozMap; use dom::bindings::num::Finite; @@ -29,6 +30,7 @@ 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::{JS_NewPlainObject, JS_NewUint8ClampedArray}; @@ -649,8 +651,44 @@ impl TestBindingMethods for TestBinding { fn ReceiveAnyMozMap(&self) -> MozMap { MozMap::new() } #[allow(unrooted_must_root)] - fn ReturnPromise(&self) -> Rc { - Promise::new(self.global().r()) + fn ReturnResolvedPromise(&self, cx: *mut JSContext, v: HandleValue) -> Fallible> { + Promise::Resolve(self.global().r(), cx, v) + } + + #[allow(unrooted_must_root)] + fn ReturnRejectedPromise(&self, cx: *mut JSContext, v: HandleValue) -> Fallible> { + Promise::Reject(self.global().r(), cx, v) + } + + #[allow(unrooted_must_root)] + fn PromiseNativeHandler(&self, + resolve: Option>, + reject: Option>) -> Rc { + let global = self.global(); + let handler = PromiseNativeHandler::new(global.r(), + resolve.map(|r| SimpleHandler::new(r)), + reject.map(|r| SimpleHandler::new(r))); + 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, + } + impl SimpleHandler { + fn new(callback: Rc) -> Box { + 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)] diff --git a/components/script/dom/webidls/TestBinding.webidl b/components/script/dom/webidls/TestBinding.webidl index dc58b9b0645c..3ecfaf4cdef8 100644 --- a/components/script/dom/webidls/TestBinding.webidl +++ b/components/script/dom/webidls/TestBinding.webidl @@ -508,14 +508,20 @@ interface TestBinding { [Func="TestBinding::condition_satisfied"] const unsigned short funcControlledConstEnabled = 0; - Promise returnPromise(); + [Throws] + Promise returnResolvedPromise(any value); + [Throws] + Promise returnRejectedPromise(any value); readonly attribute Promise promiseAttribute; void acceptPromise(Promise string); void acceptNullablePromise(Promise? string); + Promise promiseNativeHandler(SimpleCallback? resolve, SimpleCallback? reject); void panic(); }; +callback SimpleCallback = void(any value); + partial interface TestBinding { [Pref="dom.testable_crash.enabled"] void crashHard(); diff --git a/tests/wpt/mozilla/meta/MANIFEST.json b/tests/wpt/mozilla/meta/MANIFEST.json index ede708b825bd..2401b0543aff 100644 --- a/tests/wpt/mozilla/meta/MANIFEST.json +++ b/tests/wpt/mozilla/meta/MANIFEST.json @@ -7176,6 +7176,12 @@ "url": "/_mozilla/mozilla/preserve_wrapper_callback.html" } ], + "mozilla/promise.html": [ + { + "path": "mozilla/promise.html", + "url": "/_mozilla/mozilla/promise.html" + } + ], "mozilla/prototypes.html": [ { "path": "mozilla/prototypes.html", diff --git a/tests/wpt/mozilla/meta/mozilla/promise.html.ini b/tests/wpt/mozilla/meta/mozilla/promise.html.ini new file mode 100644 index 000000000000..784c666e1b72 --- /dev/null +++ b/tests/wpt/mozilla/meta/mozilla/promise.html.ini @@ -0,0 +1,3 @@ +[promise.html] + type: testharness + prefs: [dom.testbinding.enabled:true] diff --git a/tests/wpt/mozilla/tests/mozilla/promise.html b/tests/wpt/mozilla/tests/mozilla/promise.html new file mode 100644 index 000000000000..db86bcce106a --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/promise.html @@ -0,0 +1,40 @@ + + + + + +