Skip to content

Commit b42039d

Browse files
committed
Fix prototypes of interface objects (fixes #2665)
Callback interface objects' (i.e. NodeFilter's) prototype is now Object instead of Function and non-callback interface objects' their proper ancestor, starting with the Function prototype. The function do_create_interface_objects is removed in favour of 4 functions: create_callback_interface_object, create_interface_prototype_object, create_noncallback_interface_object and create_named_constructors. While this increases the amount of codegen'd code, this greatly improves the readability of the code involved in this part of DOM, instead of having one function doing 4 different things. We can always find a more adequate abstraction later. NativeProperties and everything related to the interface objects have been removed from the utils module.
1 parent 8bab1cd commit b42039d

File tree

9 files changed

+391
-698
lines changed

9 files changed

+391
-698
lines changed

components/script/dom/bindings/codegen/CodegenRust.py

+190-203
Large diffs are not rendered by default.
+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
//! Machinery to initialise interface prototype objects and interface objects.
6+
7+
use dom::bindings::codegen::PrototypeList;
8+
use dom::bindings::conversions::get_dom_class;
9+
use dom::bindings::utils::{ConstantSpec, NonNullJSNative, define_constants};
10+
use js::jsapi::{HandleObject, HandleValue, JS_DefineProperty1, JS_DefineProperty2};
11+
use js::jsapi::{JS_DefineProperty4, JS_GetFunctionObject, JS_GetPrototype, JS_InternString};
12+
use js::jsapi::{JS_LinkConstructorAndPrototype, JS_NewFunction, JS_NewObject};
13+
use js::jsapi::{JS_NewObjectWithUniqueType, JSClass, JSContext, JSFunctionSpec};
14+
use js::jsapi::{JSPropertySpec, MutableHandleObject, RootedObject, RootedString};
15+
use js::rust::{define_methods, define_properties};
16+
use js::{JSFUN_CONSTRUCTOR, JSPROP_PERMANENT, JSPROP_READONLY};
17+
use libc;
18+
use std::ptr;
19+
20+
/// Create and define the interface object of a callback interface.
21+
pub unsafe fn create_callback_interface_object(cx: *mut JSContext,
22+
receiver: HandleObject,
23+
constants: &'static [ConstantSpec],
24+
name: &'static [u8]) {
25+
assert!(!constants.is_empty());
26+
let obj = RootedObject::new(cx, JS_NewObject(cx, ptr::null()));
27+
assert!(!obj.ptr.is_null());
28+
define_constants(cx, obj.handle(), constants);
29+
define_name(cx, obj.handle(), name);
30+
define_global_object(cx, receiver, name, obj.handle());
31+
}
32+
33+
/// Create the interface prototype object of a non-callback interface.
34+
pub unsafe fn create_interface_prototype_object(
35+
cx: *mut JSContext,
36+
proto: HandleObject,
37+
class: &'static JSClass,
38+
regular_methods: Option<&'static [JSFunctionSpec]>,
39+
regular_properties: Option<&'static [JSPropertySpec]>,
40+
constants: &'static [ConstantSpec],
41+
rval: MutableHandleObject) {
42+
create_object(cx, proto, class, regular_methods, regular_properties, constants, rval);
43+
}
44+
45+
/// Create and define the interface object of a non-callback interface.
46+
pub unsafe fn create_noncallback_interface_object(
47+
cx: *mut JSContext,
48+
receiver: HandleObject,
49+
proto: HandleObject,
50+
class: &'static JSClass,
51+
static_methods: Option<&'static [JSFunctionSpec]>,
52+
static_properties: Option<&'static [JSPropertySpec]>,
53+
constants: &'static [ConstantSpec],
54+
interface_prototype_object: HandleObject,
55+
name: &'static [u8],
56+
length: u32,
57+
rval: MutableHandleObject) {
58+
create_object(cx, proto, class, static_methods, static_properties, constants, rval);
59+
assert!(JS_LinkConstructorAndPrototype(cx, rval.handle(), interface_prototype_object));
60+
define_name(cx, rval.handle(), name);
61+
define_length(cx, rval.handle(), length);
62+
define_global_object(cx, receiver, name, rval.handle());
63+
}
64+
65+
/// Create and define the named constructors of a non-callback interface.
66+
pub unsafe fn create_named_constructors(
67+
cx: *mut JSContext,
68+
receiver: HandleObject,
69+
named_constructors: &[(NonNullJSNative, &'static [u8], u32)],
70+
interface_prototype_object: HandleObject) {
71+
let mut constructor = RootedObject::new(cx, ptr::null_mut());
72+
73+
for &(native, name, arity) in named_constructors {
74+
assert!(*name.last().unwrap() == b'\0');
75+
76+
let fun = JS_NewFunction(cx,
77+
Some(native),
78+
arity,
79+
JSFUN_CONSTRUCTOR,
80+
name.as_ptr() as *const libc::c_char);
81+
assert!(!fun.is_null());
82+
constructor.ptr = JS_GetFunctionObject(fun);
83+
assert!(!constructor.ptr.is_null());
84+
85+
assert!(JS_DefineProperty1(cx,
86+
constructor.handle(),
87+
b"prototype\0".as_ptr() as *const libc::c_char,
88+
interface_prototype_object,
89+
JSPROP_PERMANENT | JSPROP_READONLY,
90+
None,
91+
None));
92+
93+
define_global_object(cx, receiver, name, constructor.handle());
94+
}
95+
}
96+
97+
/// Return whether a value is an instance of a given prototype.
98+
/// http://heycam.github.io/webidl/#es-interface-hasinstance
99+
pub unsafe fn has_instance(
100+
cx: *mut JSContext,
101+
prototype: HandleObject,
102+
value: HandleValue,
103+
id: PrototypeList::ID,
104+
index: usize)
105+
-> bool {
106+
if !value.is_object() {
107+
// Step 1.
108+
return false;
109+
}
110+
let mut value = RootedObject::new(cx, value.to_object());
111+
112+
// Steps 2-3 only concern callback interface objects.
113+
114+
if let Ok(dom_class) = get_dom_class(value.ptr) {
115+
if dom_class.interface_chain[index] == id {
116+
// Step 4.
117+
return true;
118+
}
119+
}
120+
121+
while JS_GetPrototype(cx, value.handle(), value.handle_mut()) {
122+
if value.ptr as *const _ == prototype.ptr {
123+
// Step 5.3.
124+
return true;
125+
}
126+
}
127+
// Step 5.2.
128+
false
129+
}
130+
131+
unsafe fn create_object(
132+
cx: *mut JSContext,
133+
proto: HandleObject,
134+
class: &'static JSClass,
135+
methods: Option<&'static [JSFunctionSpec]>,
136+
properties: Option<&'static [JSPropertySpec]>,
137+
constants: &'static [ConstantSpec],
138+
rval: MutableHandleObject) {
139+
rval.set(JS_NewObjectWithUniqueType(cx, class, proto));
140+
assert!(!rval.ptr.is_null());
141+
if let Some(methods) = methods {
142+
define_methods(cx, rval.handle(), methods).unwrap();
143+
}
144+
if let Some(properties) = properties {
145+
define_properties(cx, rval.handle(), properties).unwrap();
146+
}
147+
define_constants(cx, rval.handle(), constants);
148+
}
149+
150+
unsafe fn define_name(cx: *mut JSContext, obj: HandleObject, name: &'static [u8]) {
151+
assert!(*name.last().unwrap() == b'\0');
152+
let name =
153+
RootedString::new(cx, JS_InternString(cx, name.as_ptr() as *const libc::c_char));
154+
assert!(!name.ptr.is_null());
155+
assert!(JS_DefineProperty2(cx,
156+
obj,
157+
b"name\0".as_ptr() as *const libc::c_char,
158+
name.handle(),
159+
JSPROP_READONLY,
160+
None, None));
161+
}
162+
163+
unsafe fn define_length(cx: *mut JSContext, obj: HandleObject, length: u32) {
164+
assert!(JS_DefineProperty4(cx,
165+
obj,
166+
b"length\0".as_ptr() as *const libc::c_char,
167+
length,
168+
JSPROP_READONLY,
169+
None, None));
170+
}
171+
172+
unsafe fn define_global_object(
173+
cx: *mut JSContext,
174+
receiver: HandleObject,
175+
name: &'static [u8],
176+
obj: HandleObject) {
177+
assert!(JS_DefineProperty1(cx,
178+
receiver,
179+
name.as_ptr() as *const libc::c_char,
180+
obj,
181+
0,
182+
None, None));
183+
}

components/script/dom/bindings/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub mod conversions;
138138
pub mod error;
139139
pub mod global;
140140
pub mod inheritance;
141+
pub mod interface;
141142
pub mod js;
142143
pub mod num;
143144
pub mod proxyhandler;

0 commit comments

Comments
 (0)