Skip to content

Commit

Permalink
Use byte string instead of handcrafted byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
aopicier committed May 25, 2015
1 parent 4a95bce commit ab4059c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 40 deletions.
50 changes: 14 additions & 36 deletions components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -1287,16 +1287,11 @@ def specData(m):
accessor = m.get("nativeName", m["name"])
if accessor[0:3] != 'JS_':
accessor = "%s as NonNullJSNative" % accessor
return (m["name"], accessor, jitinfo, m["length"], m["flags"])
return (str_to_const_array(m["name"]), accessor, jitinfo, m["length"], m["flags"])

def stringDecl(m):
return "const %s_name: [u8; %i] = %s;\n" % (m["name"], len(m["name"]) + 1,
str_to_const_array(m["name"]))

decls = ''.join([stringDecl(m) for m in array])
return decls + self.generatePrefableArray(
return self.generatePrefableArray(
array, name,
' JSFunctionSpec { name: &%s_name as *const u8 as *const libc::c_char, call: JSNativeWrapper {op: Some(%s), info: %s}, nargs: %s, flags: %s as u16, selfHostedName: 0 as *const libc::c_char }',
' JSFunctionSpec { name: %s as *const u8 as *const libc::c_char, call: JSNativeWrapper {op: Some(%s), info: %s}, nargs: %s, flags: %s as u16, selfHostedName: 0 as *const libc::c_char }',
' JSFunctionSpec { name: 0 as *const libc::c_char, call: JSNativeWrapper {op: None, info: 0 as *const JSJitInfo}, nargs: 0, flags: 0, selfHostedName: 0 as *const libc::c_char }',
'JSFunctionSpec',
specData)
Expand Down Expand Up @@ -1353,19 +1348,12 @@ def setter(attr):
"native" : accessor})

def specData(attr):
return (attr.identifier.name, flags(attr), getter(attr),
return (str_to_const_array(attr.identifier.name), flags(attr), getter(attr),
setter(attr))

def stringDecl(attr):
name = attr.identifier.name
return "const %s_name: [u8; %i] = %s;\n" % (name, len(name) + 1,
str_to_const_array(name))

decls = ''.join([stringDecl(m) for m in array])

return decls + self.generatePrefableArray(
return self.generatePrefableArray(
array, name,
' JSPropertySpec { name: &%s_name as *const u8 as *const libc::c_char, tinyid: 0, flags: ((%s) & 0xFF) as u8, getter: %s, setter: %s }',
' JSPropertySpec { name: %s as *const u8 as *const libc::c_char, tinyid: 0, flags: ((%s) & 0xFF) as u8, getter: %s, setter: %s }',
' JSPropertySpec { name: 0 as *const libc::c_char, tinyid: 0, flags: 0, getter: JSPropertyOpWrapper {op: None, info: 0 as *const JSJitInfo}, setter: JSStrictPropertyOpWrapper {op: None, info: 0 as *const JSJitInfo} }',
'JSPropertySpec',
specData)
Expand All @@ -1384,18 +1372,12 @@ def generateArray(self, array, name):
return ""

def specData(const):
return (const.identifier.name,
return (str_to_const_array(const.identifier.name),
convertConstIDLValueToJSVal(const.value))

def stringDecl(const):
name = const.identifier.name
return "const %s_name: &'static [u8] = &%s;\n" % (name, str_to_const_array(name))

decls = ''.join([stringDecl(m) for m in array])

return decls + self.generatePrefableArray(
return self.generatePrefableArray(
array, name,
' ConstantSpec { name: %s_name, value: %s }',
' ConstantSpec { name: %s, value: %s }',
None,
'ConstantSpec',
specData)
Expand Down Expand Up @@ -1583,10 +1565,9 @@ def define(self):
flags = "0"
slots = "1"
return """\
const Class_name: [u8; %i] = %s;
static Class: DOMJSClass = DOMJSClass {
base: js::Class {
name: &Class_name as *const u8 as *const libc::c_char,
name: %s as *const u8 as *const libc::c_char,
flags: JSCLASS_IS_DOMJSCLASS | %s | (((%s) & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT), //JSCLASS_HAS_RESERVED_SLOTS(%s),
addProperty: Some(JS_PropertyStub),
delProperty: Some(JS_PropertyStub),
Expand Down Expand Up @@ -1649,16 +1630,15 @@ def define(self):
},
dom_class: %s
};
""" % (len(self.descriptor.interface.identifier.name) + 1,
str_to_const_array(self.descriptor.interface.identifier.name),
""" % (str_to_const_array(self.descriptor.interface.identifier.name),
flags, slots, slots,
FINALIZE_HOOK_NAME, traceHook,
self.descriptor.outerObjectHook,
self.descriptor.outerObjectHook,
CGIndenter(CGGeneric(DOMClass(self.descriptor))).define())

def str_to_const_array(s):
return "[" + (", ".join(map(lambda x: "'" + x + "' as u8", list(s)) + ['0 as u8'])) + "]"
return "b\"%s\\0\"" % s

class CGPrototypeJSClass(CGThing):
def __init__(self, descriptor):
Expand All @@ -1667,9 +1647,8 @@ def __init__(self, descriptor):

def define(self):
return """\
const PrototypeClassName__: [u8; %s] = %s;
static PrototypeClass: JSClass = JSClass {
name: &PrototypeClassName__ as *const u8 as *const libc::c_char,
name: %s as *const u8 as *const libc::c_char,
flags: (1 & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT, //JSCLASS_HAS_RESERVED_SLOTS(1)
addProperty: Some(JS_PropertyStub),
delProperty: Some(JS_PropertyStub),
Expand All @@ -1686,8 +1665,7 @@ def define(self):
trace: None,
reserved: [0 as *mut libc::c_void; 40]
};
""" % (len(self.descriptor.interface.identifier.name + "Prototype") + 1,
str_to_const_array(self.descriptor.interface.identifier.name + "Prototype"))
""" % str_to_const_array(self.descriptor.interface.identifier.name + "Prototype")

class CGInterfaceObjectJSClass(CGThing):
def __init__(self, descriptor):
Expand Down
6 changes: 2 additions & 4 deletions components/script/dom/eventtarget.rs
Expand Up @@ -203,17 +203,15 @@ impl<'a> EventTargetHelpers for JSRef<'a, EventTarget> {
let lineno = 0; //XXXjdm need to get a real number here

let nargs = 1; //XXXjdm not true for onerror
const ARG_NAME: [c_char; 6] =
['e' as c_char, 'v' as c_char, 'e' as c_char, 'n' as c_char, 't' as c_char, 0];
static mut ARG_NAMES: [*const c_char; 1] = [&ARG_NAME as *const c_char];
static mut ARG_NAMES: [*const c_char; 1] = [b"event\0" as *const u8 as *const c_char];

let source: Vec<u16> = source.utf16_units().collect();
let handler = unsafe {
JS_CompileUCFunction(cx,
ptr::null_mut(),
name.as_ptr(),
nargs,
&ARG_NAMES as *const *const c_char as *mut *const c_char,
ARG_NAMES.as_mut_ptr(),
source.as_ptr(),
source.len() as size_t,
url.as_ptr(),
Expand Down

0 comments on commit ab4059c

Please sign in to comment.