Use JS_NewStringCopyN for the representation of interface objects

This removes the need for the final null byte and we can make
NonCallbackInterfaceObjectClass::new safe again I guess.
This commit is contained in:
Anthony Ramine 2016-05-15 01:19:52 +02:00
parent 2c674d0397
commit 4af3e9028d
2 changed files with 12 additions and 13 deletions

View file

@ -1993,17 +1993,16 @@ class CGInterfaceObjectJSClass(CGThing):
args = { args = {
"constructorBehavior": constructorBehavior, "constructorBehavior": constructorBehavior,
"id": name, "id": name,
"representation": str_to_const_array("function %s() {\\n [native code]\\n}" % name), "representation": 'b"function %s() {\\n [native code]\\n}"' % name,
"depth": self.descriptor.prototypeDepth "depth": self.descriptor.prototypeDepth
} }
return """\ return """\
static InterfaceObjectClass: NonCallbackInterfaceObjectClass = unsafe { static InterfaceObjectClass: NonCallbackInterfaceObjectClass =
NonCallbackInterfaceObjectClass::new( NonCallbackInterfaceObjectClass::new(
%(constructorBehavior)s, %(constructorBehavior)s,
%(representation)s, %(representation)s,
PrototypeList::ID::%(id)s, PrototypeList::ID::%(id)s,
%(depth)s) %(depth)s);
};
""" % args """ % args

View file

@ -12,10 +12,10 @@ use js::glue::UncheckedUnwrapObject;
use js::jsapi::{Class, ClassExtension, ClassSpec, GetGlobalForObjectCrossCompartment}; use js::jsapi::{Class, ClassExtension, ClassSpec, GetGlobalForObjectCrossCompartment};
use js::jsapi::{HandleObject, HandleValue, JSClass, JSContext, JSFunctionSpec}; use js::jsapi::{HandleObject, HandleValue, JSClass, JSContext, JSFunctionSpec};
use js::jsapi::{JSNative, JSFUN_CONSTRUCTOR, JSPROP_ENUMERATE, JSPROP_PERMANENT, JSPROP_READONLY}; use js::jsapi::{JSNative, JSFUN_CONSTRUCTOR, JSPROP_ENUMERATE, JSPROP_PERMANENT, JSPROP_READONLY};
use js::jsapi::{JSPROP_RESOLVING, JSPropertySpec, JSString, JS_DefineProperty1, JS_DefineProperty2}; use js::jsapi::{JSPROP_RESOLVING, JSPropertySpec, JSString, JS_AtomizeAndPinString};
use js::jsapi::{JS_AtomizeAndPinString, JS_DefineProperty4, JS_GetClass, JS_GetFunctionObject}; use js::jsapi::{JS_DefineProperty, JS_DefineProperty1, JS_DefineProperty2, JS_DefineProperty4};
use js::jsapi::{JS_GetPrototype, JS_LinkConstructorAndPrototype, JS_NewFunction, JS_NewObject}; use js::jsapi::{JS_GetClass, JS_GetFunctionObject, JS_GetPrototype, JS_LinkConstructorAndPrototype};
use js::jsapi::{JS_NewObjectWithUniqueType, JS_NewStringCopyZ, JS_DefineProperty}; use js::jsapi::{JS_NewFunction, JS_NewObject, JS_NewObjectWithUniqueType, JS_NewStringCopyN};
use js::jsapi::{MutableHandleObject, MutableHandleValue, ObjectOps, RootedObject, RootedString}; use js::jsapi::{MutableHandleObject, MutableHandleValue, ObjectOps, RootedObject, RootedString};
use js::jsapi::{RootedValue, Value}; use js::jsapi::{RootedValue, Value};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UInt32Value}; use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UInt32Value};
@ -87,9 +87,9 @@ unsafe extern "C" fn fun_to_string_hook(cx: *mut JSContext,
-> *mut JSString { -> *mut JSString {
let js_class = JS_GetClass(obj.get()); let js_class = JS_GetClass(obj.get());
assert!(!js_class.is_null()); assert!(!js_class.is_null());
let object_class = &*(js_class as *const NonCallbackInterfaceObjectClass); let repr = (*(js_class as *const NonCallbackInterfaceObjectClass)).representation;
assert!(object_class.representation.last() == Some(&0)); assert!(!repr.is_empty());
let ret = JS_NewStringCopyZ(cx, object_class.representation.as_ptr() as *const libc::c_char); let ret = JS_NewStringCopyN(cx, repr.as_ptr() as *const libc::c_char, repr.len());
assert!(!ret.is_null()); assert!(!ret.is_null());
ret ret
} }
@ -103,7 +103,7 @@ pub struct NonCallbackInterfaceObjectClass {
pub proto_id: PrototypeList::ID, pub proto_id: PrototypeList::ID,
/// The prototype depth of that interface, used in the hasInstance hook. /// The prototype depth of that interface, used in the hasInstance hook.
pub proto_depth: u16, pub proto_depth: u16,
/// The string representation of the object (ends with '\0'). /// The string representation of the object.
pub representation: &'static [u8], pub representation: &'static [u8],
} }
@ -111,7 +111,7 @@ unsafe impl Sync for NonCallbackInterfaceObjectClass {}
impl NonCallbackInterfaceObjectClass { impl NonCallbackInterfaceObjectClass {
/// Create a new `NonCallbackInterfaceObjectClass` structure. /// Create a new `NonCallbackInterfaceObjectClass` structure.
pub const unsafe fn new( pub const fn new(
constructor_behavior: InterfaceConstructorBehavior, constructor_behavior: InterfaceConstructorBehavior,
string_rep: &'static [u8], string_rep: &'static [u8],
proto_id: PrototypeList::ID, proto_id: PrototypeList::ID,