servo: Merge #10096 - Make all interface objects share the same funToString (from GuillaumeGomez:fun_to_string); r=nox

Fixes #9280

Source-Repo: https://github.com/servo/servo
Source-Revision: e95368df6297e664122da8ae15f25724be557353
This commit is contained in:
Guillaume Gomez 2016-03-21 16:21:44 +05:01
parent 5e8b17341f
commit d5f008cc87
2 changed files with 25 additions and 30 deletions

View File

@ -1893,15 +1893,16 @@ class CGInterfaceObjectJSClass(CGThing):
constructor = CONSTRUCT_HOOK_NAME
else:
constructor = "throwing_constructor"
name = self.descriptor.interface.identifier.name
args = {
"constructor": constructor,
"name": self.descriptor.interface.identifier.name,
"id": self.descriptor.interface.identifier.name,
"id": name,
"representation": str_to_const_array("function %s() {\\n [native code]\\n}" % name),
"depth": self.descriptor.prototypeDepth
}
return """\
static InterfaceObjectClass: NonCallbackInterfaceObjectClass =
NonCallbackInterfaceObjectClass::new(%(constructor)s, fun_to_string,
NonCallbackInterfaceObjectClass::new(%(constructor)s, %(representation)s,
PrototypeList::ID::%(id)s, %(depth)s);
""" % args
@ -4824,21 +4825,6 @@ let args = CallArgs::from_vp(vp, argc);
return CGList([preamble, callGenerator])
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):
"""
A hook for finalize, used to release our native object.
@ -5007,7 +4993,6 @@ class CGDescriptor(CGThing):
cgThings.append(CGClassConstructHook(descriptor, ctor))
if not descriptor.interface.isCallback():
cgThings.append(CGInterfaceObjectJSClass(descriptor))
cgThings.append(CGClassFunToStringHook(descriptor))
if not descriptor.interface.isCallback():
cgThings.append(CGPrototypeJSClass(descriptor))

View File

@ -13,8 +13,9 @@ use js::jsapi::{HandleObject, HandleValue, JSClass, JSContext, JSFunctionSpec};
use js::jsapi::{JSPropertySpec, JSString, JS_DefineProperty1, JS_DefineProperty2};
use js::jsapi::{JS_DefineProperty4, JS_GetClass, JS_GetFunctionObject, JS_GetPrototype};
use js::jsapi::{JS_InternString, JS_LinkConstructorAndPrototype, JS_NewFunction, JS_NewObject};
use js::jsapi::{JS_NewObjectWithUniqueType, JS_DefineProperty, MutableHandleObject};
use js::jsapi::{MutableHandleValue, ObjectOps, RootedObject, RootedString, RootedValue, Value};
use js::jsapi::{JS_NewObjectWithUniqueType, JS_NewStringCopyZ, JS_DefineProperty};
use js::jsapi::{MutableHandleObject, MutableHandleValue, ObjectOps, RootedObject, RootedString};
use js::jsapi::{RootedValue, Value};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UInt32Value};
use js::rust::{define_methods, define_properties};
use js::{JSPROP_ENUMERATE, JSFUN_CONSTRUCTOR, JSPROP_PERMANENT, JSPROP_READONLY};
@ -79,17 +80,23 @@ pub fn define_constants(cx: *mut JSContext, obj: HandleObject, constants: &'stat
}
}
unsafe extern "C" fn fun_to_string_hook(cx: *mut JSContext,
obj: HandleObject,
_indent: u32)
-> *mut JSString {
let js_class = JS_GetClass(obj.get());
assert!(!js_class.is_null());
let object_class = &*(js_class as *const NonCallbackInterfaceObjectClass);
assert!(object_class.representation.last() == Some(&0));
let ret = JS_NewStringCopyZ(cx, object_class.representation.as_ptr() as *const libc::c_char);
assert!(!ret.is_null());
ret
}
/// A constructor class hook.
pub type ConstructorClassHook =
unsafe extern "C" fn(cx: *mut JSContext, argc: u32, vp: *mut Value) -> 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 {
@ -99,6 +106,8 @@ pub struct NonCallbackInterfaceObjectClass {
pub proto_id: PrototypeList::ID,
/// The prototype depth of that interface, used in the hasInstance hook.
pub proto_depth: u16,
/// The string representation of the object (ends with '\0').
pub representation: &'static [u8],
}
unsafe impl Sync for NonCallbackInterfaceObjectClass {}
@ -107,7 +116,7 @@ impl NonCallbackInterfaceObjectClass {
/// Create a new `NonCallbackInterfaceObjectClass` structure.
pub const fn new(
constructor: ConstructorClassHook,
fun_to_string: FunToStringHook,
string_rep: &'static [u8],
proto_id: PrototypeList::ID,
proto_depth: u16)
-> NonCallbackInterfaceObjectClass {
@ -157,11 +166,12 @@ impl NonCallbackInterfaceObjectClass {
getElements: None,
enumerate: None,
thisObject: None,
funToString: Some(fun_to_string),
funToString: Some(fun_to_string_hook),
}
},
proto_id: proto_id,
proto_depth: proto_depth,
representation: string_rep,
}
}