diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index f0dd201bf37..709ced84779 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -79,6 +79,8 @@ libc = "0.2" log = "0.3" num = "0.1.24" rand = "0.3" +phf = "0.7.13" +phf_macros = "0.7.13" ref_slice = "0.1.0" rustc-serialize = "0.3" selectors = {version = "0.5", features = ["heap_size"]} diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 6ae07873dca..c969d3f04db 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -1779,10 +1779,10 @@ class CGDOMJSClass(CGThing): } if self.descriptor.isGlobal(): assert not self.descriptor.weakReferenceable - args["enumerateHook"] = "Some(js::jsapi::JS_EnumerateStandardClasses)" + args["enumerateHook"] = "Some(enumerate_global)" args["flags"] = "JSCLASS_IS_GLOBAL | JSCLASS_DOM_GLOBAL" args["slots"] = "JSCLASS_GLOBAL_SLOT_COUNT + 1" - args["resolveHook"] = "Some(js::jsapi::JS_ResolveStandardClass)" + args["resolveHook"] = "Some(resolve_global)" args["traceHook"] = "js::jsapi::JS_GlobalObjectTraceHook" elif self.descriptor.weakReferenceable: args["slots"] = "2" @@ -2280,11 +2280,8 @@ JS_SetPrototype(cx, obj.handle(), proto.handle()); %(copyUnforgeable)s (*raw).init_reflector(obj.ptr); -let ret = Root::from_ref(&*raw); - -RegisterBindings::Register(cx, obj.handle()); - -ret""" % {'copyUnforgeable': unforgeable, 'createObject': create}) +Root::from_ref(&*raw)\ +""" % {'copyUnforgeable': unforgeable, 'createObject': create}) class CGIDLInterface(CGThing): @@ -5447,12 +5444,12 @@ class CGBindingRoot(CGThing): 'dom::bindings::utils::{DOMClass, DOMJSClass}', 'dom::bindings::utils::{DOM_PROTO_UNFORGEABLE_HOLDER_SLOT, JSCLASS_DOM_GLOBAL}', 'dom::bindings::utils::{ProtoOrIfaceArray, create_dom_global}', - 'dom::bindings::utils::{finalize_global, find_enum_string_index, generic_getter}', - 'dom::bindings::utils::{generic_lenient_getter, generic_lenient_setter}', + 'dom::bindings::utils::{enumerate_global, finalize_global, find_enum_string_index}', + 'dom::bindings::utils::{generic_getter, generic_lenient_getter, generic_lenient_setter}', 'dom::bindings::utils::{generic_method, generic_setter, get_array_index_from_id}', 'dom::bindings::utils::{get_dictionary_property, get_property_on_prototype}', 'dom::bindings::utils::{get_proto_or_iface_array, has_property_on_prototype}', - 'dom::bindings::utils::{is_platform_object, set_dictionary_property}', + 'dom::bindings::utils::{is_platform_object, resolve_global, set_dictionary_property}', 'dom::bindings::utils::{throwing_constructor, trace_global}', 'dom::bindings::trace::{JSTraceable, RootedTraceable}', 'dom::bindings::callback::{CallbackContainer,CallbackInterface,CallbackFunction}', @@ -6095,6 +6092,37 @@ class GlobalGenRoots(): call the appropriate define/declare method. """ + @staticmethod + def InterfaceObjectMap(config): + mods = [ + "dom::bindings::codegen", + "js::jsapi::{HandleObject, JSContext}", + "phf", + ] + imports = CGList([CGGeneric("use %s;" % mod) for mod in mods], "\n") + + pairs = [] + for d in config.getDescriptors(hasInterfaceObject=True): + binding = toBindingNamespace(d.name) + pairs.append((d.name, binding)) + for ctor in d.interface.namedConstructors: + pairs.append((ctor.identifier.name, binding)) + pairs.sort(key=operator.itemgetter(0)) + mappings = [ + CGGeneric('b"%s" => codegen::Bindings::%s::DefineDOMInterface as fn(_, _),' % pair) + for pair in pairs + ] + mapType = "phf::Map<&'static [u8], fn(*mut JSContext, HandleObject)>" + phf = CGWrapper( + CGIndenter(CGList(mappings, "\n")), + pre="pub static MAP: %s = phf_map! {\n" % mapType, + post="\n};\n") + + return CGList([ + CGGeneric(AUTOGENERATED_WARNING_COMMENT), + CGList([imports, phf], "\n\n") + ]) + @staticmethod def PrototypeList(config): # Prototype ID enum. diff --git a/components/script/dom/bindings/codegen/GlobalGen.py b/components/script/dom/bindings/codegen/GlobalGen.py index f4210331e0e..72a2faa71e4 100644 --- a/components/script/dom/bindings/codegen/GlobalGen.py +++ b/components/script/dom/bindings/codegen/GlobalGen.py @@ -62,6 +62,7 @@ def main(): to_generate = [ ('PrototypeList', 'PrototypeList.rs'), ('RegisterBindings', 'RegisterBindings.rs'), + ('InterfaceObjectMap', 'InterfaceObjectMap.rs'), ('InterfaceTypes', 'InterfaceTypes.rs'), ('InheritTypes', 'InheritTypes.rs'), ('Bindings', os.path.join('Bindings', 'mod.rs')), diff --git a/components/script/dom/bindings/mod.rs b/components/script/dom/bindings/mod.rs index 602e24fe2e0..160141f76e4 100644 --- a/components/script/dom/bindings/mod.rs +++ b/components/script/dom/bindings/mod.rs @@ -159,6 +159,9 @@ pub mod codegen { pub mod Bindings { include!(concat!(env!("OUT_DIR"), "/Bindings/mod.rs")); } + pub mod InterfaceObjectMap { + include!(concat!(env!("OUT_DIR"), "/InterfaceObjectMap.rs")); + } pub mod InterfaceTypes { include!(concat!(env!("OUT_DIR"), "/InterfaceTypes.rs")); } diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index d06dc10c8da..2b4e5ee5ae0 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -4,6 +4,7 @@ //! Various utilities to glue JavaScript and the DOM implementation together. +use dom::bindings::codegen::InterfaceObjectMap; use dom::bindings::codegen::PrototypeList; use dom::bindings::codegen::PrototypeList::{MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH}; use dom::bindings::conversions::{DOM_OBJECT_SLOT, is_dom_class}; @@ -18,16 +19,18 @@ use js; use js::error::throw_type_error; use js::glue::{CallJitGetterOp, CallJitMethodOp, CallJitSetterOp, IsWrapper}; use js::glue::{GetCrossCompartmentWrapper, WrapperNew}; -use js::glue::{RUST_FUNCTION_VALUE_TO_JITINFO, RUST_JSID_IS_INT}; -use js::glue::{RUST_JSID_TO_INT, UnwrapObject}; +use js::glue::{RUST_FUNCTION_VALUE_TO_JITINFO, RUST_JSID_IS_INT, RUST_JSID_IS_STRING}; +use js::glue::{RUST_JSID_TO_INT, RUST_JSID_TO_STRING, UnwrapObject}; use js::jsapi::{CallArgs, CompartmentOptions, DOMCallbacks, GetGlobalForObjectCrossCompartment}; use js::jsapi::{HandleId, HandleObject, HandleValue, Heap, JSAutoCompartment, JSClass, JSContext}; use js::jsapi::{JSJitInfo, JSObject, JSTraceOp, JSTracer, JSVersion, JSWrapObjectCallbacks}; -use js::jsapi::{JS_DeletePropertyById1, JS_FireOnNewGlobalObject, JS_ForwardGetPropertyTo}; -use js::jsapi::{JS_GetClass, JS_GetProperty, JS_GetPrototype, JS_GetReservedSlot, JS_HasProperty}; -use js::jsapi::{JS_HasPropertyById, JS_IsExceptionPending, JS_NewGlobalObject}; -use js::jsapi::{JS_ObjectToOuterObject, JS_SetProperty, JS_SetReservedSlot}; -use js::jsapi::{MutableHandleValue, ObjectOpResult, OnNewGlobalHookOption, RootedObject}; +use js::jsapi::{JS_DeletePropertyById1, JS_EnumerateStandardClasses, JS_FireOnNewGlobalObject}; +use js::jsapi::{JS_ForwardGetPropertyTo, JS_GetClass, JS_GetLatin1StringCharsAndLength}; +use js::jsapi::{JS_GetProperty, JS_GetPrototype, JS_GetReservedSlot, JS_HasProperty}; +use js::jsapi::{JS_HasPropertyById, JS_IsExceptionPending, JS_IsGlobalObject, JS_NewGlobalObject}; +use js::jsapi::{JS_ObjectToOuterObject, JS_ResolveStandardClass, JS_SetProperty}; +use js::jsapi::{JS_SetReservedSlot, JS_StringHasLatin1Chars, MutableHandleValue, ObjectOpResult}; +use js::jsapi::{OnNewGlobalHookOption, RootedObject}; use js::jsval::{JSVal}; use js::jsval::{PrivateValue, UndefinedValue}; use js::rust::{GCMethods, ToString}; @@ -37,6 +40,7 @@ use std::default::Default; use std::ffi::CString; use std::os::raw::c_void; use std::ptr; +use std::slice; use util::non_geckolib::jsstring_to_str; /// Proxy handler for a WindowProxy. @@ -364,6 +368,56 @@ pub unsafe fn trace_global(tracer: *mut JSTracer, obj: *mut JSObject) { } } +/// Enumerate lazy properties of a global object. +pub unsafe extern "C" fn enumerate_global(cx: *mut JSContext, obj: HandleObject) -> bool { + assert!(JS_IsGlobalObject(obj.get())); + if !JS_EnumerateStandardClasses(cx, obj) { + return false; + } + for init_fun in InterfaceObjectMap::MAP.values() { + init_fun(cx, obj); + } + true +} + +/// Resolve a lazy global property, for interface objects and named constructors. +pub unsafe extern "C" fn resolve_global( + cx: *mut JSContext, + obj: HandleObject, + id: HandleId, + rval: *mut bool) + -> bool { + assert!(JS_IsGlobalObject(obj.get())); + if !JS_ResolveStandardClass(cx, obj, id, rval) { + return false; + } + if *rval { + return true; + } + if !RUST_JSID_IS_STRING(id) { + *rval = false; + return true; + } + + let string = RUST_JSID_TO_STRING(id); + if !JS_StringHasLatin1Chars(string) { + *rval = false; + return true; + } + let mut length = 0; + let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length); + assert!(!ptr.is_null()); + let bytes = slice::from_raw_parts(ptr, length as usize); + + if let Some(init_fun) = InterfaceObjectMap::MAP.get(bytes) { + init_fun(cx, obj); + *rval = true; + } else { + *rval = false; + } + true +} + unsafe extern "C" fn wrap(cx: *mut JSContext, _existing: HandleObject, obj: HandleObject) diff --git a/components/script/lib.rs b/components/script/lib.rs index dfd11938075..93ba1dba8a4 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -26,6 +26,7 @@ #![doc = "The script crate contains all matters DOM."] #![plugin(heapsize_plugin)] +#![plugin(phf_macros)] #![plugin(plugins)] extern crate angle; @@ -55,6 +56,7 @@ extern crate msg; extern crate net_traits; extern crate num; extern crate offscreen_gl_context; +extern crate phf; #[macro_use] extern crate profile_traits; extern crate rand; diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 2d9b010e7be..23a93b6e0f4 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -1428,6 +1428,15 @@ dependencies = [ "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "phf_macros" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_generator 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_shared 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "phf_shared" version = "0.7.13" @@ -1622,6 +1631,8 @@ dependencies = [ "net_traits 0.0.1", "num 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", "offscreen_gl_context 0.1.0 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)", + "phf 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_macros 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", "profile_traits 0.0.1", "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/cef/Cargo.lock b/ports/cef/Cargo.lock index 1e4c811d9ce..d0ec8af8c64 100644 --- a/ports/cef/Cargo.lock +++ b/ports/cef/Cargo.lock @@ -1332,6 +1332,15 @@ dependencies = [ "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "phf_macros" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_generator 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_shared 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "phf_shared" version = "0.7.13" @@ -1508,6 +1517,8 @@ dependencies = [ "net_traits 0.0.1", "num 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", "offscreen_gl_context 0.1.0 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)", + "phf 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_macros 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", "profile_traits 0.0.1", "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/gonk/Cargo.lock b/ports/gonk/Cargo.lock index fb9d7abb017..f8dffe9ed2b 100644 --- a/ports/gonk/Cargo.lock +++ b/ports/gonk/Cargo.lock @@ -1313,6 +1313,15 @@ dependencies = [ "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "phf_macros" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_generator 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_shared 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "phf_shared" version = "0.7.13" @@ -1489,6 +1498,8 @@ dependencies = [ "net_traits 0.0.1", "num 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", "offscreen_gl_context 0.1.0 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)", + "phf 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_macros 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", "profile_traits 0.0.1", "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/tests/wpt/metadata/MANIFEST.json b/tests/wpt/metadata/MANIFEST.json index c3e65a34e55..6ef0d6a1b71 100644 --- a/tests/wpt/metadata/MANIFEST.json +++ b/tests/wpt/metadata/MANIFEST.json @@ -34494,7 +34494,16 @@ }, "local_changes": { "deleted": [], - "items": {}, + "items": { + "testharness": { + "WebIDL/ecmascript-binding/interface-object.html": [ + { + "path": "WebIDL/ecmascript-binding/interface-object.html", + "url": "/WebIDL/ecmascript-binding/interface-object.html" + } + ] + } + }, "reftest_nodes": {} }, "reftest_nodes": { diff --git a/tests/wpt/web-platform-tests/WebIDL/ecmascript-binding/interface-object.html b/tests/wpt/web-platform-tests/WebIDL/ecmascript-binding/interface-object.html new file mode 100644 index 00000000000..132c61ddaed --- /dev/null +++ b/tests/wpt/web-platform-tests/WebIDL/ecmascript-binding/interface-object.html @@ -0,0 +1,28 @@ + + +Interface objects + + +