mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Describe non-callback interface objects with JSClass structures
JS_NewFunction doesn't allow us to set the prototype of the interface objects.
This commit is contained in:
parent
2957a56ad3
commit
e1d3e4df2a
2 changed files with 221 additions and 52 deletions
|
@ -1861,33 +1861,25 @@ static PrototypeClass: JSClass = JSClass {
|
||||||
|
|
||||||
class CGInterfaceObjectJSClass(CGThing):
|
class CGInterfaceObjectJSClass(CGThing):
|
||||||
def __init__(self, descriptor):
|
def __init__(self, descriptor):
|
||||||
|
assert descriptor.interface.hasInterfaceObject() and not descriptor.interface.isCallback()
|
||||||
CGThing.__init__(self)
|
CGThing.__init__(self)
|
||||||
self.descriptor = descriptor
|
self.descriptor = descriptor
|
||||||
|
|
||||||
def define(self):
|
def define(self):
|
||||||
if True:
|
if self.descriptor.interface.ctor():
|
||||||
return ""
|
constructor = CONSTRUCT_HOOK_NAME
|
||||||
ctorname = "0 as *const u8" if not self.descriptor.interface.ctor() else CONSTRUCT_HOOK_NAME
|
else:
|
||||||
hasinstance = HASINSTANCE_HOOK_NAME
|
constructor = "throwing_constructor"
|
||||||
|
args = {
|
||||||
|
"constructor": constructor,
|
||||||
|
"hasInstance": HASINSTANCE_HOOK_NAME,
|
||||||
|
"name": self.descriptor.interface.identifier.name,
|
||||||
|
}
|
||||||
return """\
|
return """\
|
||||||
const InterfaceObjectClass: JSClass = {
|
static InterfaceObjectClass: NonCallbackInterfaceObjectClass =
|
||||||
%s, 0,
|
NonCallbackInterfaceObjectClass::new(%(constructor)s, %(hasInstance)s,
|
||||||
JS_PropertyStub,
|
fun_to_string);
|
||||||
JS_PropertyStub,
|
""" % args
|
||||||
JS_PropertyStub,
|
|
||||||
JS_StrictPropertyStub,
|
|
||||||
JS_EnumerateStub,
|
|
||||||
JS_ResolveStub,
|
|
||||||
JS_ConvertStub,
|
|
||||||
0 as *const u8,
|
|
||||||
0 as *const u8,
|
|
||||||
%s,
|
|
||||||
%s,
|
|
||||||
%s,
|
|
||||||
0 as *const u8,
|
|
||||||
JSCLASS_NO_INTERNAL_MEMBERS
|
|
||||||
};
|
|
||||||
""" % (str_to_const_array("Function"), ctorname, hasinstance, ctorname)
|
|
||||||
|
|
||||||
|
|
||||||
class CGList(CGThing):
|
class CGList(CGThing):
|
||||||
|
@ -2420,12 +2412,14 @@ assert!(!rval.ptr.is_null());""" % properties))
|
||||||
else:
|
else:
|
||||||
properties["constructor"] = "throwing_constructor"
|
properties["constructor"] = "throwing_constructor"
|
||||||
properties["length"] = 0
|
properties["length"] = 0
|
||||||
|
code.append(CGGeneric("""
|
||||||
code.append(CGGeneric("""\
|
let interface_proto = RootedObject::new(cx, JS_GetFunctionPrototype(cx, global));
|
||||||
|
assert!(!interface_proto.ptr.is_null());
|
||||||
|
|
||||||
create_noncallback_interface_object(cx,
|
create_noncallback_interface_object(cx,
|
||||||
receiver,
|
receiver,
|
||||||
%(constructor)s,
|
interface_proto.handle(),
|
||||||
|
&InterfaceObjectClass,
|
||||||
%(static_methods)s,
|
%(static_methods)s,
|
||||||
%(static_attrs)s,
|
%(static_attrs)s,
|
||||||
%(consts)s,
|
%(consts)s,
|
||||||
|
@ -4693,6 +4687,44 @@ let args = CallArgs::from_vp(vp, argc);
|
||||||
return CGList([preamble, callGenerator])
|
return CGList([preamble, callGenerator])
|
||||||
|
|
||||||
|
|
||||||
|
class CGClassHasInstanceHook(CGAbstractExternMethod):
|
||||||
|
def __init__(self, descriptor):
|
||||||
|
args = [Argument('*mut JSContext', 'cx'),
|
||||||
|
Argument('HandleObject', 'obj'),
|
||||||
|
Argument('MutableHandleValue', 'value'),
|
||||||
|
Argument('*mut bool', 'rval')]
|
||||||
|
assert descriptor.interface.hasInterfaceObject() and not descriptor.interface.isCallback()
|
||||||
|
CGAbstractExternMethod.__init__(self, descriptor, HASINSTANCE_HOOK_NAME,
|
||||||
|
'bool', args)
|
||||||
|
|
||||||
|
def definition_body(self):
|
||||||
|
id = "PrototypeList::ID::%s" % self.descriptor.interface.identifier.name
|
||||||
|
return CGGeneric("""\
|
||||||
|
match has_instance(cx, obj, value.handle(), %(id)s, %(index)s) {
|
||||||
|
Ok(result) => {
|
||||||
|
*rval = result;
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Err(()) => false,
|
||||||
|
}
|
||||||
|
""" % {"id": id, "index": self.descriptor.prototypeDepth})
|
||||||
|
|
||||||
|
|
||||||
|
class CGClassFunToStringHook(CGAbstractExternMethod):
|
||||||
|
"""
|
||||||
|
A hook to convert functions to strings.
|
||||||
|
"""
|
||||||
|
def __init__(self, descriptor):
|
||||||
|
args = [Argument('*mut JSContext', 'cx'), Argument('HandleObject', '_obj'),
|
||||||
|
Argument('u32', '_indent')]
|
||||||
|
CGAbstractExternMethod.__init__(self, descriptor, "fun_to_string", '*mut JSString', args)
|
||||||
|
|
||||||
|
def definition_body(self):
|
||||||
|
name = self.descriptor.interface.identifier.name
|
||||||
|
string = str_to_const_array("function %s() {\\n [native code]\\n}" % name)
|
||||||
|
return CGGeneric("JS_NewStringCopyZ(cx, %s as *const _ as *const libc::c_char)" % string)
|
||||||
|
|
||||||
|
|
||||||
class CGClassFinalizeHook(CGAbstractClassHook):
|
class CGClassFinalizeHook(CGAbstractClassHook):
|
||||||
"""
|
"""
|
||||||
A hook for finalize, used to release our native object.
|
A hook for finalize, used to release our native object.
|
||||||
|
@ -4860,7 +4892,10 @@ class CGDescriptor(CGThing):
|
||||||
cgThings.append(CGClassConstructHook(descriptor))
|
cgThings.append(CGClassConstructHook(descriptor))
|
||||||
for ctor in descriptor.interface.namedConstructors:
|
for ctor in descriptor.interface.namedConstructors:
|
||||||
cgThings.append(CGClassConstructHook(descriptor, ctor))
|
cgThings.append(CGClassConstructHook(descriptor, ctor))
|
||||||
|
if not descriptor.interface.isCallback():
|
||||||
cgThings.append(CGInterfaceObjectJSClass(descriptor))
|
cgThings.append(CGInterfaceObjectJSClass(descriptor))
|
||||||
|
cgThings.append(CGClassHasInstanceHook(descriptor))
|
||||||
|
cgThings.append(CGClassFunToStringHook(descriptor))
|
||||||
|
|
||||||
if not descriptor.interface.isCallback():
|
if not descriptor.interface.isCallback():
|
||||||
cgThings.append(CGPrototypeJSClass(descriptor))
|
cgThings.append(CGPrototypeJSClass(descriptor))
|
||||||
|
@ -5258,8 +5293,8 @@ class CGBindingRoot(CGThing):
|
||||||
'js::jsapi::{JS_GetObjectPrototype, JS_GetProperty, JS_GetPropertyById}',
|
'js::jsapi::{JS_GetObjectPrototype, JS_GetProperty, JS_GetPropertyById}',
|
||||||
'js::jsapi::{JS_GetPropertyDescriptorById, JS_GetReservedSlot, JS_HasProperty}',
|
'js::jsapi::{JS_GetPropertyDescriptorById, JS_GetReservedSlot, JS_HasProperty}',
|
||||||
'js::jsapi::{JS_HasPropertyById, JS_InitializePropertiesFromCompatibleNativeObject}',
|
'js::jsapi::{JS_HasPropertyById, JS_InitializePropertiesFromCompatibleNativeObject}',
|
||||||
'js::jsapi::{JS_InternString, JS_IsExceptionPending, JS_NewObject}',
|
'js::jsapi::{JS_InternString, JS_IsExceptionPending, JS_NewObject, JS_NewObjectWithGivenProto}',
|
||||||
'js::jsapi::{JS_NewObjectWithGivenProto, JS_NewObjectWithoutMetadata, JS_SetProperty}',
|
'js::jsapi::{JS_NewObjectWithoutMetadata, JS_NewStringCopyZ, JS_SetProperty}',
|
||||||
'js::jsapi::{JS_SetPrototype, JS_SetReservedSlot, JS_WrapValue, JSAutoCompartment}',
|
'js::jsapi::{JS_SetPrototype, JS_SetReservedSlot, JS_WrapValue, JSAutoCompartment}',
|
||||||
'js::jsapi::{JSAutoRequest, JSContext, JSClass, JSFreeOp, JSFunctionSpec}',
|
'js::jsapi::{JSAutoRequest, JSContext, JSClass, JSFreeOp, JSFunctionSpec}',
|
||||||
'js::jsapi::{JSJitGetterCallArgs, JSJitInfo, JSJitMethodCallArgs, JSJitSetterCallArgs}',
|
'js::jsapi::{JSJitGetterCallArgs, JSJitInfo, JSJitMethodCallArgs, JSJitSetterCallArgs}',
|
||||||
|
@ -5279,8 +5314,9 @@ class CGBindingRoot(CGThing):
|
||||||
'js::rust::{GCMethods, define_methods, define_properties}',
|
'js::rust::{GCMethods, define_methods, define_properties}',
|
||||||
'dom::bindings',
|
'dom::bindings',
|
||||||
'dom::bindings::global::{GlobalRef, global_root_from_object, global_root_from_reflector}',
|
'dom::bindings::global::{GlobalRef, global_root_from_object, global_root_from_reflector}',
|
||||||
'dom::bindings::interface::{create_callback_interface_object, create_interface_prototype_object}',
|
'dom::bindings::interface::{NonCallbackInterfaceObjectClass, create_callback_interface_object}',
|
||||||
'dom::bindings::interface::{create_named_constructors, create_noncallback_interface_object}',
|
'dom::bindings::interface::{create_interface_prototype_object, create_named_constructors}',
|
||||||
|
'dom::bindings::interface::{create_noncallback_interface_object, has_instance}',
|
||||||
'dom::bindings::js::{JS, Root, RootedReference}',
|
'dom::bindings::js::{JS, Root, RootedReference}',
|
||||||
'dom::bindings::js::{OptionalRootedReference}',
|
'dom::bindings::js::{OptionalRootedReference}',
|
||||||
'dom::bindings::reflector::{Reflectable}',
|
'dom::bindings::reflector::{Reflectable}',
|
||||||
|
|
|
@ -4,17 +4,109 @@
|
||||||
|
|
||||||
//! Machinery to initialise interface prototype objects and interface objects.
|
//! Machinery to initialise interface prototype objects and interface objects.
|
||||||
|
|
||||||
|
use dom::bindings::codegen::PrototypeList;
|
||||||
|
use dom::bindings::conversions::get_dom_class;
|
||||||
use dom::bindings::utils::{ConstantSpec, NonNullJSNative, define_constants};
|
use dom::bindings::utils::{ConstantSpec, NonNullJSNative, define_constants};
|
||||||
use js::jsapi::{HandleObject, JSClass, JSContext, JSFunctionSpec};
|
use js::glue::UncheckedUnwrapObject;
|
||||||
use js::jsapi::{JSObject, JSPropertySpec, JS_DefineProperty1, JS_DefineProperty2};
|
use js::jsapi::{Class, ClassExtension, ClassSpec, HandleObject, HandleValue, JSClass};
|
||||||
use js::jsapi::{JS_GetFunctionObject, JS_InternString, JS_LinkConstructorAndPrototype};
|
use js::jsapi::{JSContext, JSFunctionSpec, JSObject, JSPropertySpec, JSString};
|
||||||
use js::jsapi::{JS_NewFunction, JS_NewObject, JS_NewObjectWithUniqueType};
|
use js::jsapi::{JS_DefineProperty1, JS_DefineProperty2, JS_DefineProperty4};
|
||||||
use js::jsapi::{MutableHandleObject, RootedObject, RootedString};
|
use js::jsapi::{JS_GetFunctionObject, JS_GetPrototype, JS_InternString};
|
||||||
|
use js::jsapi::{JS_LinkConstructorAndPrototype, JS_NewFunction, JS_NewObject};
|
||||||
|
use js::jsapi::{JS_NewObjectWithUniqueType, MutableHandleObject, MutableHandleValue};
|
||||||
|
use js::jsapi::{ObjectOps, RootedObject, RootedString, Value};
|
||||||
use js::rust::{define_methods, define_properties};
|
use js::rust::{define_methods, define_properties};
|
||||||
use js::{JSFUN_CONSTRUCTOR, JSPROP_PERMANENT, JSPROP_READONLY};
|
use js::{JSFUN_CONSTRUCTOR, JSPROP_PERMANENT, JSPROP_READONLY};
|
||||||
use libc;
|
use libc;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
|
/// A constructor class hook.
|
||||||
|
pub type ConstructorClassHook =
|
||||||
|
unsafe extern "C" fn(cx: *mut JSContext, argc: u32, vp: *mut Value) -> bool;
|
||||||
|
|
||||||
|
/// A has_instance class hook.
|
||||||
|
pub type HasInstanceClassHook =
|
||||||
|
unsafe extern "C" fn(cx: *mut JSContext,
|
||||||
|
obj: HandleObject,
|
||||||
|
vp: MutableHandleValue,
|
||||||
|
bp: *mut bool)
|
||||||
|
-> bool;
|
||||||
|
|
||||||
|
/// A fun_to_string class hook.
|
||||||
|
pub type FunToStringHook =
|
||||||
|
unsafe extern "C" fn(cx: *mut JSContext,
|
||||||
|
obj: HandleObject,
|
||||||
|
indent: u32)
|
||||||
|
-> *mut JSString;
|
||||||
|
|
||||||
|
/// The class of a non-callback interface object.
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct NonCallbackInterfaceObjectClass {
|
||||||
|
/// The SpiderMonkey Class structure.
|
||||||
|
pub class: Class,
|
||||||
|
}
|
||||||
|
unsafe impl Sync for NonCallbackInterfaceObjectClass {}
|
||||||
|
|
||||||
|
impl NonCallbackInterfaceObjectClass {
|
||||||
|
/// Create a new `NonCallbackInterfaceObjectClass` structure.
|
||||||
|
pub const fn new(
|
||||||
|
constructor: ConstructorClassHook,
|
||||||
|
has_instance: HasInstanceClassHook,
|
||||||
|
fun_to_string: FunToStringHook)
|
||||||
|
-> NonCallbackInterfaceObjectClass {
|
||||||
|
NonCallbackInterfaceObjectClass {
|
||||||
|
class: Class {
|
||||||
|
name: b"Function\0" as *const _ as *const libc::c_char,
|
||||||
|
flags: 0,
|
||||||
|
addProperty: None,
|
||||||
|
delProperty: None,
|
||||||
|
getProperty: None,
|
||||||
|
setProperty: None,
|
||||||
|
enumerate: None,
|
||||||
|
resolve: None,
|
||||||
|
convert: None,
|
||||||
|
finalize: None,
|
||||||
|
call: Some(constructor),
|
||||||
|
hasInstance: Some(has_instance),
|
||||||
|
construct: Some(constructor),
|
||||||
|
trace: None,
|
||||||
|
spec: ClassSpec {
|
||||||
|
createConstructor: None,
|
||||||
|
createPrototype: None,
|
||||||
|
constructorFunctions: ptr::null(),
|
||||||
|
constructorProperties: ptr::null(),
|
||||||
|
prototypeFunctions: ptr::null(),
|
||||||
|
prototypeProperties: ptr::null(),
|
||||||
|
finishInit: None,
|
||||||
|
flags: 0,
|
||||||
|
},
|
||||||
|
ext: ClassExtension {
|
||||||
|
outerObject: None,
|
||||||
|
innerObject: None,
|
||||||
|
isWrappedNative: false,
|
||||||
|
weakmapKeyDelegateOp: None,
|
||||||
|
objectMovedOp: None,
|
||||||
|
},
|
||||||
|
ops: ObjectOps {
|
||||||
|
lookupProperty: None,
|
||||||
|
defineProperty: None,
|
||||||
|
hasProperty: None,
|
||||||
|
getProperty: None,
|
||||||
|
setProperty: None,
|
||||||
|
getOwnPropertyDescriptor: None,
|
||||||
|
deleteProperty: None,
|
||||||
|
watch: None,
|
||||||
|
unwatch: None,
|
||||||
|
getElements: None,
|
||||||
|
enumerate: None,
|
||||||
|
thisObject: None,
|
||||||
|
funToString: Some(fun_to_string),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create and define the interface object of a callback interface.
|
/// Create and define the interface object of a callback interface.
|
||||||
pub unsafe fn create_callback_interface_object(
|
pub unsafe fn create_callback_interface_object(
|
||||||
cx: *mut JSContext,
|
cx: *mut JSContext,
|
||||||
|
@ -45,31 +137,26 @@ pub unsafe fn create_interface_prototype_object(
|
||||||
pub unsafe fn create_noncallback_interface_object(
|
pub unsafe fn create_noncallback_interface_object(
|
||||||
cx: *mut JSContext,
|
cx: *mut JSContext,
|
||||||
receiver: HandleObject,
|
receiver: HandleObject,
|
||||||
constructor_native: NonNullJSNative,
|
proto: HandleObject,
|
||||||
|
class: &'static NonCallbackInterfaceObjectClass,
|
||||||
static_methods: Option<&'static [JSFunctionSpec]>,
|
static_methods: Option<&'static [JSFunctionSpec]>,
|
||||||
static_properties: Option<&'static [JSPropertySpec]>,
|
static_properties: Option<&'static [JSPropertySpec]>,
|
||||||
constants: &'static [ConstantSpec],
|
constants: &'static [ConstantSpec],
|
||||||
interface_prototype_object: HandleObject,
|
interface_prototype_object: HandleObject,
|
||||||
name: &'static [u8],
|
name: &'static [u8],
|
||||||
length: u32) {
|
length: u32) {
|
||||||
assert!(!interface_prototype_object.ptr.is_null());
|
let mut interface_object = RootedObject::new(cx, ptr::null_mut());
|
||||||
|
create_object(cx,
|
||||||
let interface_object =
|
proto,
|
||||||
RootedObject::new(cx, create_constructor(cx, constructor_native, length, name));
|
&*(class as *const _ as *const JSClass),
|
||||||
assert!(!interface_object.ptr.is_null());
|
static_methods,
|
||||||
|
static_properties,
|
||||||
if let Some(static_methods) = static_methods {
|
constants,
|
||||||
define_methods(cx, interface_object.handle(), static_methods).unwrap();
|
interface_object.handle_mut());
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(static_properties) = static_properties {
|
|
||||||
define_properties(cx, interface_object.handle(), static_properties).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
define_constants(cx, interface_object.handle(), constants);
|
|
||||||
|
|
||||||
assert!(JS_LinkConstructorAndPrototype(cx, interface_object.handle(),
|
assert!(JS_LinkConstructorAndPrototype(cx, interface_object.handle(),
|
||||||
interface_prototype_object));
|
interface_prototype_object));
|
||||||
|
define_name(cx, interface_object.handle(), name);
|
||||||
|
define_length(cx, interface_object.handle(), length);
|
||||||
define_on_global_object(cx, receiver, name, interface_object.handle());
|
define_on_global_object(cx, receiver, name, interface_object.handle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,6 +186,43 @@ pub unsafe fn create_named_constructors(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return whether a value is an instance of a given prototype.
|
||||||
|
/// http://heycam.github.io/webidl/#es-interface-hasinstance
|
||||||
|
pub unsafe fn has_instance(
|
||||||
|
cx: *mut JSContext,
|
||||||
|
prototype: HandleObject,
|
||||||
|
value: HandleValue,
|
||||||
|
id: PrototypeList::ID,
|
||||||
|
index: usize)
|
||||||
|
-> Result<bool, ()> {
|
||||||
|
if !value.is_object() {
|
||||||
|
// Step 1.
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
let mut value = RootedObject::new(cx, value.to_object());
|
||||||
|
|
||||||
|
// Steps 2-3 only concern callback interface objects.
|
||||||
|
|
||||||
|
if let Ok(dom_class) = get_dom_class(UncheckedUnwrapObject(value.ptr, /* stopAtOuter = */ 0)) {
|
||||||
|
if dom_class.interface_chain[index] == id {
|
||||||
|
// Step 4.
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while JS_GetPrototype(cx, value.handle(), value.handle_mut()) {
|
||||||
|
if value.ptr.is_null() {
|
||||||
|
// Step 5.2.
|
||||||
|
return Ok(false);
|
||||||
|
} else if value.ptr as *const _ == prototype.ptr {
|
||||||
|
// Step 5.3.
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// JS_GetPrototype threw an exception.
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
|
||||||
unsafe fn create_constructor(
|
unsafe fn create_constructor(
|
||||||
cx: *mut JSContext,
|
cx: *mut JSContext,
|
||||||
constructor_native: NonNullJSNative,
|
constructor_native: NonNullJSNative,
|
||||||
|
@ -152,6 +276,15 @@ unsafe fn define_name(cx: *mut JSContext, obj: HandleObject, name: &'static [u8]
|
||||||
None, None));
|
None, None));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe fn define_length(cx: *mut JSContext, obj: HandleObject, length: u32) {
|
||||||
|
assert!(JS_DefineProperty4(cx,
|
||||||
|
obj,
|
||||||
|
b"length\0".as_ptr() as *const libc::c_char,
|
||||||
|
length,
|
||||||
|
JSPROP_READONLY,
|
||||||
|
None, None));
|
||||||
|
}
|
||||||
|
|
||||||
unsafe fn define_on_global_object(
|
unsafe fn define_on_global_object(
|
||||||
cx: *mut JSContext,
|
cx: *mut JSContext,
|
||||||
receiver: HandleObject,
|
receiver: HandleObject,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue