diff options
author | bors-servo <metajack+bors@gmail.com> | 2014-12-30 09:51:45 -0700 |
---|---|---|
committer | bors-servo <metajack+bors@gmail.com> | 2014-12-30 09:51:45 -0700 |
commit | 37a97f3273c442fa59a3f65e8300a2527b004036 (patch) | |
tree | f67474e5acdb9ac4aab021ea5b082cee3607e74c /components/script/dom/bindings/codegen/CodegenRust.py | |
parent | 5d8ec549597de4af1008ee1395f89adbcf40fdb8 (diff) | |
parent | bb577968e5063c48cd534f2f3fc1f2fba1be26de (diff) | |
download | servo-37a97f3273c442fa59a3f65e8300a2527b004036.tar.gz servo-37a97f3273c442fa59a3f65e8300a2527b004036.zip |
auto merge of #4515 : brunoabinader/servo/codegen-cleanup, r=Ms2ger
Main changes:
- Whitespace (indent) fixes;
- CGIndent-related fixes;
- Removed consecutive empty lines;
- Removed empty lines before closing brackets;
- Codegen style fixes;
Tests:
We don't have a static code style analyzer yet, so I've checked using the following (together with manual lookup at some selected generated bindings):
1. Check for lines with wrong indentation (1 to 3 whitespaces at the beginning)
Command: ```$ pcregrep -r "^[ ]{1,3}[^ ]" components/script/dom/bindings/codegen/Bindings```
Expected: None
Actual: None
2. Check for lines with wrong indentation (5 to 7 whitespaces at the beginning)
Command: ```$ pcregrep -r "^[ ]{5,7}[^ ]" components/script/dom/bindings/codegen/Bindings```
Expected: None
Actual: None
3. Check for lonely semicolons
Command: ```$ pcregrep -r " \{0,\};" components/script/dom/bindings/codegen/Bindings```
Expected: None
Actual: None
4. Check for empty lines before closing brackets
Command: ```$ pcregrep -r -M "^$\n {0,}\}" components/script/dom/bindings/codegen/Bindings```
Expected: None
Actual: None
5. Check for consecutive empty lines
Command: ```$ pcregrep -r -M "^$\n^$\n" components/script/dom/bindings/codegen/Bindings```
Expected: None
Actual:
```components/script/dom/bindings/codegen/Bindings/ChildNodeBinding.rs
components/script/dom/bindings/codegen/Bindings/ElementCSSInlineStyleBinding.rs
components/script/dom/bindings/codegen/Bindings/ParentNodeBinding.rs
components/script/dom/bindings/codegen/Bindings/URLUtilsBinding.rs
components/script/dom/bindings/codegen/Bindings/URLUtilsReadOnlyBinding.rs
```
All of the above are ```[NoInterfaceObject]```, thus providing only imports. We shouldn’t, however, generate empty lines (investigate this later on).
Diffstat (limited to 'components/script/dom/bindings/codegen/CodegenRust.py')
-rw-r--r-- | components/script/dom/bindings/codegen/CodegenRust.py | 887 |
1 files changed, 447 insertions, 440 deletions
diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index a0ea1a0e3f0..814f453c1f7 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -103,16 +103,16 @@ class CastableObjectUnwrapper(): def __init__(self, descriptor, source, codeOnFailure): self.substitution = { "source": source, - "codeOnFailure": CGIndenter(CGGeneric(codeOnFailure), 4).define(), + "codeOnFailure": CGIndenter(CGGeneric(codeOnFailure), 8).define(), } def __str__(self): - return string.Template( -"""match unwrap_jsmanaged(${source}) { - Ok(val) => val, - Err(()) => { + return string.Template("""\ +match unwrap_jsmanaged(${source}) { + Ok(val) => val, + Err(()) => { ${codeOnFailure} - } + } }""").substitute(self.substitution) @@ -194,8 +194,8 @@ class CGMethodCall(CGThing): if requiredArgs > 0: code = ( "if argc < %d {\n" - " throw_type_error(cx, \"Not enough arguments to %s.\");\n" - " return 0;\n" + " throw_type_error(cx, \"Not enough arguments to %s.\");\n" + " return 0;\n" "}" % (requiredArgs, methodName)) self.cgRoot.prepend( CGWrapper(CGGeneric(code), pre="\n", post="\n")) @@ -374,7 +374,7 @@ class CGMethodCall(CGThing): CGSwitch("argcount", argCountCases, CGGeneric("throw_type_error(cx, \"Not enough arguments to %s.\");\n" - "return 0;\n" % methodName))) + "return 0;" % methodName))) #XXXjdm Avoid unreachable statement warnings #overloadCGThings.append( # CGGeneric('panic!("We have an always-returning default case");\n' @@ -405,7 +405,7 @@ def typeIsSequenceOrHasSequenceMember(type): type = type.inner if type.isSequence(): return True - if type.isArray(): + if type.isArray(): elementType = type.inner return typeIsSequenceOrHasSequenceMember(elementType) if type.isDictionary(): @@ -509,9 +509,7 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, return string[0].upper() + string[1:] # Helper functions for dealing with failures due to the JS value being the - # wrong type of value - # Helper functions for dealing with failures due to the JS value being the - # wrong type of value + # wrong type of value. def onFailureNotAnObject(failureCode): return CGWrapper( CGGeneric( @@ -532,9 +530,7 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, CGGeneric( failureCode or ('throw_type_error(cx, \"%s is not callable.\");\n' - '%s' % (firstCap(sourceDescription), exceptionCode))), - post="\n") - + '%s' % (firstCap(sourceDescription), exceptionCode)))) # A helper function for handling null default values. Checks that the # default value, if it exists, is null. @@ -561,12 +557,11 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, if type.nullable(): templateBody += ( "} else if (${val}).is_null_or_undefined() {\n" - " None\n") + " None\n") templateBody += ( "} else {\n" + CGIndenter(onFailureNotAnObject(failureCode)).define() + - "}\n") - + "}") return templateBody assert not (isEnforceRange and isClamp) # These are mutually exclusive @@ -668,8 +663,8 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, conversionCode = ( "match FromJSValConvertible::from_jsval(cx, ${val}, %s) {\n" - " Ok(strval) => strval,\n" - " Err(_) => { %s },\n" + " Ok(strval) => strval,\n" + " Err(_) => { %s },\n" "}" % (nullBehavior, exceptionCode)) if defaultValue is None: @@ -701,8 +696,8 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, conversionCode = ( "match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n" - " Ok(strval) => strval,\n" - " Err(_) => { %s },\n" + " Ok(strval) => strval,\n" + " Err(_) => { %s },\n" "}" % exceptionCode) declType = CGGeneric("ByteString") @@ -725,12 +720,12 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, template = ( "match FindEnumStringIndex(cx, ${val}, %(values)s) {\n" - " Err(_) => { %(exceptionCode)s },\n" - " Ok(None) => { %(handleInvalidEnumValueCode)s },\n" - " Ok(Some(index)) => {\n" - " //XXXjdm need some range checks up in here.\n" - " unsafe { mem::transmute(index) }\n" - " },\n" + " Err(_) => { %(exceptionCode)s },\n" + " Ok(None) => { %(handleInvalidEnumValueCode)s },\n" + " Ok(Some(index)) => {\n" + " //XXXjdm need some range checks up in here.\n" + " unsafe { mem::transmute(index) }\n" + " },\n" "}" % { "values" : enum + "Values::strings", "exceptionCode" : exceptionCode, "handleInvalidEnumValueCode" : handleInvalidEnumValueCode }) @@ -814,8 +809,8 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, typeName = CGDictionary.makeDictionaryName(type.inner) declType = CGGeneric(typeName) template = ("match %s::new(cx, ${val}) {\n" - " Ok(dictionary) => dictionary,\n" - " Err(_) => return 0,\n" + " Ok(dictionary) => dictionary,\n" + " Err(_) => return 0,\n" "}" % typeName) return handleOptional(template, declType, handleDefaultNull("%s::empty()" % typeName)) @@ -840,8 +835,8 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, #XXXjdm support conversionBehavior here template = ( "match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n" - " Ok(v) => v,\n" - " Err(_) => { %s }\n" + " Ok(v) => v,\n" + " Err(_) => { %s }\n" "}" % exceptionCode) if defaultValue is not None: @@ -1017,7 +1012,10 @@ def wrapForType(jsvalRef, result='result', successCode='return 1;'): * 'result': the name of the variable in which the Rust value is stored; * 'successCode': the code to run once we have done the conversion. """ - return "%s = (%s).to_jsval(cx);\n%s" % (jsvalRef, result, successCode) + wrap = "%s = (%s).to_jsval(cx);" % (jsvalRef, result) + if successCode: + wrap += "\n%s" % successCode + return wrap def typeNeedsCx(type, retVal=False): @@ -1149,7 +1147,7 @@ class PropertyDefiner: return (("const %s: &'static [%s] = &[\n" + ",\n".join(specs) + "\n" + - "];\n\n") % (name, specType)) + "];\n") % (name, specType)) # The length of a method is the maximum of the lengths of the # argument lists of all its overloads. @@ -1204,8 +1202,8 @@ class MethodDefiner(PropertyDefiner): decls = ''.join([stringDecl(m) for m in array]) return decls + 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: 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 { 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: 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) @@ -1267,14 +1265,14 @@ class AttrDefiner(PropertyDefiner): def stringDecl(attr): name = attr.identifier.name return "const %s_name: [u8, ..%i] = %s;\n" % (name, len(name) + 1, - str_to_const_array(name)) + str_to_const_array(name)) decls = ''.join([stringDecl(m) for m in array]) return decls + 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: 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 { name: &%s_name 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) @@ -1303,7 +1301,7 @@ class ConstDefiner(PropertyDefiner): return decls + self.generatePrefableArray( array, name, - ' ConstantSpec { name: %s_name, value: %s }', + ' ConstantSpec { name: %s_name, value: %s }', None, 'ConstantSpec', specData) @@ -1317,7 +1315,7 @@ class CGIndenter(CGThing): A class that takes another CGThing and generates code that indents that CGThing by some number of spaces. The default indent is two spaces. """ - def __init__(self, child, indentLevel=2): + def __init__(self, child, indentLevel=4): CGThing.__init__(self) self.child = child self.indent = " " * indentLevel @@ -1400,7 +1398,7 @@ class CGTemplatedType(CGWrapper): class CGNamespace(CGWrapper): def __init__(self, namespace, child, public=False): pre = "%smod %s {\n" % ("pub " if public else "", namespace) - post = "} // mod %s\n" % namespace + post = "} // mod %s" % namespace CGWrapper.__init__(self, child, pre=pre, post=post) @staticmethod @@ -1421,9 +1419,10 @@ def DOMClass(descriptor): # padding. protoList.extend(['PrototypeList::ID::Count'] * (descriptor.config.maxProtoChainLength - len(protoList))) prototypeChainString = ', '.join(protoList) - return """DOMClass { - interface_chain: [ %s ], - native_hooks: &sNativePropertyHooks, + return """\ +DOMClass { + interface_chain: [ %s ], + native_hooks: &sNativePropertyHooks, }""" % prototypeChainString class CGDOMJSClass(CGThing): @@ -1442,72 +1441,72 @@ class CGDOMJSClass(CGThing): else: flags = "0" slots = "1" - return """ + 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, - flags: JSCLASS_IS_DOMJSCLASS | %s | (((%s) & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT as uint), //JSCLASS_HAS_RESERVED_SLOTS(%s), - addProperty: Some(JS_PropertyStub), - delProperty: Some(JS_PropertyStub), - getProperty: Some(JS_PropertyStub), - setProperty: Some(JS_StrictPropertyStub), - enumerate: Some(JS_EnumerateStub), - resolve: Some(JS_ResolveStub), - convert: Some(JS_ConvertStub), - finalize: Some(%s), - checkAccess: None, - call: None, - hasInstance: None, - construct: None, - trace: %s, - - ext: js::ClassExtension { - equality: 0 as *const u8, - outerObject: %s, - innerObject: None, - iteratorObject: 0 as *const u8, - unused: 0 as *const u8, - isWrappedNative: 0 as *const u8, + base: js::Class { + name: &Class_name as *const u8 as *const libc::c_char, + flags: JSCLASS_IS_DOMJSCLASS | %s | (((%s) & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT as uint), //JSCLASS_HAS_RESERVED_SLOTS(%s), + addProperty: Some(JS_PropertyStub), + delProperty: Some(JS_PropertyStub), + getProperty: Some(JS_PropertyStub), + setProperty: Some(JS_StrictPropertyStub), + enumerate: Some(JS_EnumerateStub), + resolve: Some(JS_ResolveStub), + convert: Some(JS_ConvertStub), + finalize: Some(%s), + checkAccess: None, + call: None, + hasInstance: None, + construct: None, + trace: %s, + + ext: js::ClassExtension { + equality: 0 as *const u8, + outerObject: %s, + innerObject: None, + iteratorObject: 0 as *const u8, + unused: 0 as *const u8, + isWrappedNative: 0 as *const u8, + }, + + ops: js::ObjectOps { + lookupGeneric: 0 as *const u8, + lookupProperty: 0 as *const u8, + lookupElement: 0 as *const u8, + lookupSpecial: 0 as *const u8, + defineGeneric: 0 as *const u8, + defineProperty: 0 as *const u8, + defineElement: 0 as *const u8, + defineSpecial: 0 as *const u8, + getGeneric: 0 as *const u8, + getProperty: 0 as *const u8, + getElement: 0 as *const u8, + getElementIfPresent: 0 as *const u8, + getSpecial: 0 as *const u8, + setGeneric: 0 as *const u8, + setProperty: 0 as *const u8, + setElement: 0 as *const u8, + setSpecial: 0 as *const u8, + getGenericAttributes: 0 as *const u8, + getPropertyAttributes: 0 as *const u8, + getElementAttributes: 0 as *const u8, + getSpecialAttributes: 0 as *const u8, + setGenericAttributes: 0 as *const u8, + setPropertyAttributes: 0 as *const u8, + setElementAttributes: 0 as *const u8, + setSpecialAttributes: 0 as *const u8, + deleteProperty: 0 as *const u8, + deleteElement: 0 as *const u8, + deleteSpecial: 0 as *const u8, + + enumerate: 0 as *const u8, + typeOf: 0 as *const u8, + thisObject: %s, + clear: 0 as *const u8, + }, }, - - ops: js::ObjectOps { - lookupGeneric: 0 as *const u8, - lookupProperty: 0 as *const u8, - lookupElement: 0 as *const u8, - lookupSpecial: 0 as *const u8, - defineGeneric: 0 as *const u8, - defineProperty: 0 as *const u8, - defineElement: 0 as *const u8, - defineSpecial: 0 as *const u8, - getGeneric: 0 as *const u8, - getProperty: 0 as *const u8, - getElement: 0 as *const u8, - getElementIfPresent: 0 as *const u8, - getSpecial: 0 as *const u8, - setGeneric: 0 as *const u8, - setProperty: 0 as *const u8, - setElement: 0 as *const u8, - setSpecial: 0 as *const u8, - getGenericAttributes: 0 as *const u8, - getPropertyAttributes: 0 as *const u8, - getElementAttributes: 0 as *const u8, - getSpecialAttributes: 0 as *const u8, - setGenericAttributes: 0 as *const u8, - setPropertyAttributes: 0 as *const u8, - setElementAttributes: 0 as *const u8, - setSpecialAttributes: 0 as *const u8, - deleteProperty: 0 as *const u8, - deleteElement: 0 as *const u8, - deleteSpecial: 0 as *const u8, - - enumerate: 0 as *const u8, - typeOf: 0 as *const u8, - thisObject: %s, - clear: 0 as *const u8, - }, - }, - dom_class: %s + dom_class: %s }; """ % (len(self.descriptor.interface.identifier.name) + 1, str_to_const_array(self.descriptor.interface.identifier.name), @@ -1526,25 +1525,25 @@ class CGPrototypeJSClass(CGThing): self.descriptor = descriptor def define(self): - return """ + return """\ const PrototypeClassName__: [u8, ..%s] = %s; static PrototypeClass: JSClass = JSClass { - name: &PrototypeClassName__ as *const u8 as *const libc::c_char, - flags: (1 & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT as uint, //JSCLASS_HAS_RESERVED_SLOTS(1) - addProperty: Some(JS_PropertyStub), - delProperty: Some(JS_PropertyStub), - getProperty: Some(JS_PropertyStub), - setProperty: Some(JS_StrictPropertyStub), - enumerate: Some(JS_EnumerateStub), - resolve: Some(JS_ResolveStub), - convert: Some(JS_ConvertStub), - finalize: None, - checkAccess: None, - call: None, - hasInstance: None, - construct: None, - trace: None, - reserved: [0 as *mut libc::c_void, ..40] + name: &PrototypeClassName__ as *const u8 as *const libc::c_char, + flags: (1 & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT as uint, //JSCLASS_HAS_RESERVED_SLOTS(1) + addProperty: Some(JS_PropertyStub), + delProperty: Some(JS_PropertyStub), + getProperty: Some(JS_PropertyStub), + setProperty: Some(JS_StrictPropertyStub), + enumerate: Some(JS_EnumerateStub), + resolve: Some(JS_ResolveStub), + convert: Some(JS_ConvertStub), + finalize: None, + checkAccess: None, + call: None, + hasInstance: None, + construct: None, + 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")) @@ -1559,23 +1558,23 @@ class CGInterfaceObjectJSClass(CGThing): return "" ctorname = "0 as *const u8" if not self.descriptor.interface.ctor() else CONSTRUCT_HOOK_NAME hasinstance = HASINSTANCE_HOOK_NAME - return """ + return """\ const InterfaceObjectClass: JSClass = { - %s, 0, - JS_PropertyStub, - JS_PropertyStub, - 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 + %s, 0, + JS_PropertyStub, + JS_PropertyStub, + 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) @@ -1620,9 +1619,10 @@ class CGGeneric(CGThing): class CGCallbackTempRoot(CGGeneric): def __init__(self, name): val = "%s::new(tempRoot)" % name - define = """{ - let tempRoot = ${val}.to_object(); - %s + define = """\ +{ + let tempRoot = ${val}.to_object(); + %s }""" % val CGGeneric.__init__(self, define) @@ -1778,7 +1778,7 @@ class CGAbstractMethod(CGThing): def define(self): body = self.definition_body() if self.unsafe: - body = CGWrapper(body, pre="unsafe {\n", post="\n}") + body = CGWrapper(CGIndenter(body), pre="unsafe {\n", post="\n}") return CGWrapper(CGIndenter(body), pre=self.definition_prologue(), @@ -1800,26 +1800,25 @@ def CreateBindingJSObject(descriptor, parent=None): let handler = RegisterBindings::proxy_handlers[PrototypeList::Proxies::%s as uint]; let mut private = PrivateValue(squirrel_away_unique(aObject) as *const libc::c_void); let obj = with_compartment(aCx, proto, || { - NewProxyObject(aCx, handler, - &private, - proto, %s, - ptr::null_mut(), ptr::null_mut()) + NewProxyObject(aCx, handler, + &private, + proto, %s, + ptr::null_mut(), ptr::null_mut()) }); -assert!(obj.is_not_null()); - +assert!(obj.is_not_null());\ """ % (descriptor.name, parent) else: if descriptor.isGlobal(): create += "let obj = CreateDOMGlobal(aCx, &Class.base as *const js::Class as *const JSClass);\n" else: create += ("let obj = with_compartment(aCx, proto, || {\n" - " JS_NewObject(aCx, &Class.base as *const js::Class as *const JSClass, &*proto, &*%s)\n" + " JS_NewObject(aCx, &Class.base as *const js::Class as *const JSClass, &*proto, &*%s)\n" "});\n" % parent) - create += """assert!(obj.is_not_null()); + create += """\ +assert!(obj.is_not_null()); JS_SetReservedSlot(obj, DOM_OBJECT_SLOT as u32, - PrivateValue(squirrel_away_unique(aObject) as *const libc::c_void)); -""" + PrivateValue(squirrel_away_unique(aObject) as *const libc::c_void));""" return create class CGWrapMethod(CGAbstractMethod): @@ -1857,12 +1856,12 @@ Temporary::new(raw)""" % CreateBindingJSObject(self.descriptor, "scope")) return CGGeneric("""\ %s with_compartment(aCx, obj, || { - let proto = GetProtoObject(aCx, obj, obj); - JS_SetPrototype(aCx, obj, proto); + let proto = GetProtoObject(aCx, obj, obj); + JS_SetPrototype(aCx, obj, proto); - raw.reflector().set_jsobject(obj); + raw.reflector().set_jsobject(obj); - RegisterBindings::Register(aCx, obj); + RegisterBindings::Register(aCx, obj); }); Temporary::new(raw)""" % CreateBindingJSObject(self.descriptor)) @@ -1881,7 +1880,7 @@ class CGIDLInterface(CGThing): 'type': self.descriptor.name, 'depth': self.descriptor.interface.inheritanceDepth(), } - return string.Template(""" + return string.Template("""\ impl IDLInterface for ${type} { fn get_prototype_id(_: Option<${type}>) -> PrototypeList::ID { PrototypeList::ID::${type} @@ -1999,7 +1998,8 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod): else: constructor = 'None' - call = """return CreateInterfaceObjects2(aCx, aGlobal, aReceiver, parentProto, + call = """\ +return CreateInterfaceObjects2(aCx, aGlobal, aReceiver, parentProto, &PrototypeClass, %s, %s, &sNativeProperties);""" % (constructor, domClass) @@ -2035,12 +2035,12 @@ assert!(((*JS_GetClass(aGlobal)).flags & JSCLASS_DOM_GLOBAL) != 0); let protoOrIfaceArray = GetProtoOrIfaceArray(aGlobal); let cachedObject: *mut JSObject = *protoOrIfaceArray.offset(%s as int); if cachedObject.is_null() { - let tmp: *mut JSObject = CreateInterfaceObjects(aCx, aGlobal, aReceiver); - assert!(tmp.is_not_null()); - *protoOrIfaceArray.offset(%s as int) = tmp; - tmp + let tmp: *mut JSObject = CreateInterfaceObjects(aCx, aGlobal, aReceiver); + assert!(tmp.is_not_null()); + *protoOrIfaceArray.offset(%s as int) = tmp; + tmp } else { - cachedObject + cachedObject }""" % (self.id, self.id)) class CGGetProtoObjectMethod(CGGetPerInterfaceObject): @@ -2096,44 +2096,43 @@ class CGDefineProxyHandler(CGAbstractMethod): body = """\ let traps = ProxyTraps { - getPropertyDescriptor: Some(getPropertyDescriptor), - getOwnPropertyDescriptor: Some(getOwnPropertyDescriptor), - defineProperty: Some(%s), - getOwnPropertyNames: Some(getOwnPropertyNames_), - delete_: Some(%s), - enumerate: Some(enumerate_), - - has: None, - hasOwn: Some(hasOwn), - get: Some(get), - set: None, - keys: None, - iterate: None, - - call: None, - construct: None, - nativeCall: ptr::null(), - hasInstance: None, - typeOf: None, - objectClassIs: None, - obj_toString: Some(obj_toString), - fun_toString: None, - //regexp_toShared: ptr::null(), - defaultValue: None, - iteratorNext: None, - finalize: Some(%s), - getElementIfPresent: None, - getPrototypeOf: None, - trace: Some(%s) + getPropertyDescriptor: Some(getPropertyDescriptor), + getOwnPropertyDescriptor: Some(getOwnPropertyDescriptor), + defineProperty: Some(%s), + getOwnPropertyNames: Some(getOwnPropertyNames_), + delete_: Some(%s), + enumerate: Some(enumerate_), + + has: None, + hasOwn: Some(hasOwn), + get: Some(get), + set: None, + keys: None, + iterate: None, + + call: None, + construct: None, + nativeCall: ptr::null(), + hasInstance: None, + typeOf: None, + objectClassIs: None, + obj_toString: Some(obj_toString), + fun_toString: None, + //regexp_toShared: ptr::null(), + defaultValue: None, + iteratorNext: None, + finalize: Some(%s), + getElementIfPresent: None, + getPrototypeOf: None, + trace: Some(%s) }; -CreateProxyHandler(&traps, &Class as *const _ as *const _) +CreateProxyHandler(&traps, &Class as *const _ as *const _)\ """ % (customDefineProperty, customDelete, FINALIZE_HOOK_NAME, TRACE_HOOK_NAME) return CGGeneric(body) - class CGDefineDOMInterfaceMethod(CGAbstractMethod): """ A method for resolve hooks to try to lazily define the interface object for @@ -2226,7 +2225,7 @@ class CGCallGenerator(CGThing): " throw_dom_exception(cx, global.root_ref(), e);\n" " return%s;\n" " },\n" - "};\n" % (glob, errorResult))) + "};" % (glob, errorResult))) if typeRetValNeedsRooting(returnType): self.cgRoot.append(CGGeneric("let result = result.root();")) @@ -2443,7 +2442,7 @@ class CGAbstractBindingMethod(CGAbstractExternMethod): unwrapThis = CGGeneric( "let obj: *mut JSObject = JS_THIS_OBJECT(cx, vp as *mut JSVal);\n" "if obj.is_null() {\n" - " return false as JSBool;\n" + " return false as JSBool;\n" "}\n" "\n" "let this: JS<%s> = %s;\n" % (self.descriptor.concreteType, unwrapThis)) @@ -2552,7 +2551,7 @@ class CGGenericGetter(CGAbstractBindingMethod): def generate_code(self): return CGGeneric( "let info: *const JSJitInfo = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));\n" - "return CallJitPropertyOp(info, cx, obj, this.unsafe_get() as *mut libc::c_void, vp);\n") + "return CallJitPropertyOp(info, cx, obj, this.unsafe_get() as *mut libc::c_void, vp);") class CGSpecializedGetter(CGAbstractExternMethod): """ @@ -2628,7 +2627,7 @@ class CGGenericSetter(CGAbstractBindingMethod): "let argv: *mut JSVal = if argc != 0 { JS_ARGV(cx, vp) } else { &mut undef as *mut JSVal };\n" "let info: *const JSJitInfo = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));\n" "if CallJitPropertyOp(info, cx, obj, this.unsafe_get() as *mut libc::c_void, argv) == 0 {\n" - " return 0;\n" + " return 0;\n" "}\n" "*vp = UndefinedValue();\n" "return 1;") @@ -2675,9 +2674,9 @@ class CGStaticSetter(CGAbstractStaticBindingMethod): checkForArg = CGGeneric( "let argv = JS_ARGV(cx, vp);\n" "if (argc == 0) {\n" - " throw_type_error(cx, \"Not enough arguments to %s setter.\");\n" - " return 0;\n" - "}\n" % self.attr.identifier.name) + " throw_type_error(cx, \"Not enough arguments to %s setter.\");\n" + " return 0;\n" + "}" % self.attr.identifier.name) call = CGSetterCall([], self.attr.type, nativeName, self.descriptor, self.attr) return CGList([checkForArg, call]) @@ -2693,16 +2692,15 @@ class CGMemberJITInfo(CGThing): self.descriptor = descriptor def defineJitInfo(self, infoName, opName, infallible): - protoID = "PrototypeList::ID::%s as u32" % self.descriptor.name + protoID = "PrototypeList::ID::%s as u32" % self.descriptor.name depth = self.descriptor.interface.inheritanceDepth() failstr = "true" if infallible else "false" - return ("\n" - "const %s: JSJitInfo = JSJitInfo {\n" - " op: %s as *const u8,\n" - " protoID: %s,\n" - " depth: %s,\n" - " isInfallible: %s, /* False in setters. */\n" - " isConstant: false /* Only relevant for getters. */\n" + return ("const %s: JSJitInfo = JSJitInfo {\n" + " op: %s as *const u8,\n" + " protoID: %s,\n" + " depth: %s,\n" + " isInfallible: %s, /* False in setters. */\n" + " isConstant: false /* Only relevant for getters. */\n" "};\n" % (infoName, opName, protoID, depth, failstr)) def define(self): @@ -2715,7 +2713,7 @@ class CGMemberJITInfo(CGThing): setterinfo = ("%s_setterinfo" % self.member.identifier.name) setter = ("set_%s" % self.member.identifier.name) # Setters are always fallible, since they have to do a typed unwrap. - result += self.defineJitInfo(setterinfo, setter, False) + result += "\n" + self.defineJitInfo(setterinfo, setter, False) return result if self.member.isMethod(): methodinfo = ("%s_methodinfo" % self.member.identifier.name) @@ -2763,30 +2761,30 @@ class CGEnum(CGThing): def __init__(self, enum): CGThing.__init__(self) - decl = """ + decl = """\ #[repr(uint)] #[deriving(PartialEq)] #[jstraceable] pub enum %s { - %s + %s } -""" % (enum.identifier.name, ",\n ".join(map(getEnumValueName, enum.values()))) +""" % (enum.identifier.name, ",\n ".join(map(getEnumValueName, enum.values()))) - inner = """ + inner = """\ use dom::bindings::conversions::ToJSValConvertible; use js::jsapi::JSContext; use js::jsval::JSVal; pub const strings: &'static [&'static str] = &[ - %s, + %s, ]; impl ToJSValConvertible for super::%s { - fn to_jsval(&self, cx: *mut JSContext) -> JSVal { - strings[*self as uint].into_string().to_jsval(cx) - } + fn to_jsval(&self, cx: *mut JSContext) -> JSVal { + strings[*self as uint].into_string().to_jsval(cx) + } } -""" % (",\n ".join(['"%s"' % val for val in enum.values()]), enum.identifier.name) +""" % (",\n ".join(['"%s"' % val for val in enum.values()]), enum.identifier.name) self.cgRoot = CGList([ CGGeneric(decl), @@ -2890,7 +2888,8 @@ class CGUnionStruct(CGThing): ] # XXXManishearth The following should be #[must_root], # however we currently allow it till #2661 is fixed - return ("""#[allow(unrooted_must_root)] + return ("""\ +#[allow(unrooted_must_root)] pub enum %s { %s } @@ -2900,7 +2899,7 @@ impl ToJSValConvertible for %s { match *self { %s } - } + } } """) % (self.type, "\n".join(enumValues), self.type, "\n".join(enumConversions)) @@ -3147,7 +3146,8 @@ class ClassUsingDeclaration(ClassItem): ClassItem.__init__(self, name, visibility) def declare(self, cgClass): - return string.Template("""using ${baseClass}::${name}; + return string.Template("""\ +using ${baseClass}::${name}; """).substitute({ 'baseClass': self.baseClass, 'name': self.name }) @@ -3210,7 +3210,7 @@ class ClassConstructor(ClassItem): return '' def getBody(self, cgClass): - initializers = [" parent: %s" % str(self.baseConstructors[0])] + initializers = [" parent: %s" % str(self.baseConstructors[0])] return (self.body + ( "%s {\n" "%s\n" @@ -3218,13 +3218,14 @@ class ClassConstructor(ClassItem): def declare(self, cgClass): args = ', '.join([a.declare() for a in self.args]) - body = ' ' + self.getBody(cgClass); - body = stripTrailingWhitespace(body.replace('\n', '\n ')) + body = ' ' + self.getBody(cgClass); + body = stripTrailingWhitespace(body.replace('\n', '\n ')) if len(body) > 0: body += '\n' body = ' {\n' + body + '}' - return string.Template("""pub fn ${decorators}new(${args}) -> ${className}${body} + return string.Template("""\ +pub fn ${decorators}new(${args}) -> ${className}${body} """).substitute({ 'decorators': self.getDecorators(True), 'className': cgClass.getNameString(), 'args': args, @@ -3236,12 +3237,13 @@ class ClassConstructor(ClassItem): args = ', '.join([a.define() for a in self.args]) - body = ' ' + self.getBody() - body = '\n' + stripTrailingWhitespace(body.replace('\n', '\n ')) + body = ' ' + self.getBody() + body = '\n' + stripTrailingWhitespace(body.replace('\n', '\n ')) if len(body) > 0: body += '\n' - return string.Template("""${decorators} + return string.Template("""\ +${decorators} ${className}::${className}(${args})${initializationList} {${body}} """).substitute({ 'decorators': self.getDecorators(False), @@ -3289,15 +3291,16 @@ class ClassDestructor(ClassItem): def declare(self, cgClass): if self.bodyInHeader: - body = ' ' + self.getBody(); - body = stripTrailingWhitespace(body.replace('\n', '\n ')) + body = ' ' + self.getBody(); + body = stripTrailingWhitespace(body.replace('\n', '\n ')) if len(body) > 0: body += '\n' body = '\n{\n' + body + '}' else: body = ';' - return string.Template("""${decorators}~${className}()${body} + return string.Template("""\ +${decorators}~${className}()${body} """).substitute({ 'decorators': self.getDecorators(True), 'className': cgClass.getNameString(), 'body': body }) @@ -3306,12 +3309,13 @@ class ClassDestructor(ClassItem): if self.bodyInHeader: return '' - body = ' ' + self.getBody() - body = '\n' + stripTrailingWhitespace(body.replace('\n', '\n ')) + body = ' ' + self.getBody() + body = '\n' + stripTrailingWhitespace(body.replace('\n', '\n ')) if len(body) > 0: body += '\n' - return string.Template("""${decorators} + return string.Template("""\ +${decorators} ${className}::~${className}() {${body}} """).substitute({ 'decorators': self.getDecorators(False), @@ -3366,7 +3370,7 @@ class ClassEnum(ClassItem): entry = '%s = %s' % (self.entries[i], self.values[i]) entries.append(entry) name = '' if not self.name else ' ' + self.name - return 'enum%s\n{\n %s\n};\n' % (name, ',\n '.join(entries)) + return 'enum%s\n{\n%s\n};\n' % (name, ',\n '.join(entries)) def define(self, cgClass): # Only goes in the header @@ -3378,7 +3382,7 @@ class ClassUnion(ClassItem): ClassItem.__init__(self, name, visibility) def declare(self, cgClass): - return 'union %s\n{\n %s\n};\n' % (self.name, '\n '.join(self.entries)) + return 'union %s\n{\n%s\n};\n' % (self.name, '\n '.join(self.entries)) def define(self, cgClass): # Only goes in the header @@ -3444,7 +3448,7 @@ class CGClass(CGThing): assert len(self.bases) == 1 #XXjdm Can we support multiple inheritance? - result += '{\n%s\n' % self.indent + result += ' {\n' if self.bases: self.members = [ClassMember("parent", self.bases[0].name, "pub")] + self.members @@ -3599,8 +3603,9 @@ class CGProxyUnwrap(CGAbstractMethod): CGAbstractMethod.__init__(self, descriptor, "UnwrapProxy", '*const ' + descriptor.concreteType, args, alwaysInline=True) def definition_body(self): - return CGGeneric("""/*if (xpc::WrapperFactory::IsXrayWrapper(obj)) { - obj = js::UnwrapObject(obj); + return CGGeneric("""\ +/*if (xpc::WrapperFactory::IsXrayWrapper(obj)) { + obj = js::UnwrapObject(obj); }*/ //MOZ_ASSERT(IsProxy(obj)); let box_ = GetProxyPrivate(obj).to_private() as *const %s; @@ -3627,32 +3632,32 @@ class CGDOMJSProxyHandler_getOwnPropertyDescriptor(CGAbstractExternMethod): fillDescriptor = "FillPropertyDescriptor(&mut *desc, proxy, %s);\nreturn true;" % readonly templateValues = {'jsvalRef': '(*desc).value', 'successCode': fillDescriptor} get = ("if index.is_some() {\n" + - " let index = index.unwrap();\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let index = index.unwrap();\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyIndexedGetter(self.descriptor, templateValues)).define() + "\n" + "}\n") if indexedSetter or self.descriptor.operations['NamedSetter']: setOrIndexedGet += "if set {\n" if indexedSetter: - setOrIndexedGet += (" if index.is_some() {\n" + - " let index = index.unwrap();\n") + setOrIndexedGet += (" if index.is_some() {\n" + + " let index = index.unwrap();\n") if not 'IndexedCreator' in self.descriptor.operations: # FIXME need to check that this is a 'supported property index' assert False - setOrIndexedGet += (" FillPropertyDescriptor(&mut *desc, proxy, false);\n" + - " return true;\n" + - " }\n") + setOrIndexedGet += (" FillPropertyDescriptor(&mut *desc, proxy, false);\n" + + " return true;\n" + + " }\n") if self.descriptor.operations['NamedSetter']: - setOrIndexedGet += " if RUST_JSID_IS_STRING(id) != 0 {\n" + setOrIndexedGet += " if RUST_JSID_IS_STRING(id) != 0 {\n" if not 'NamedCreator' in self.descriptor.operations: # FIXME need to check that this is a 'supported property name' assert False - setOrIndexedGet += (" FillPropertyDescriptor(&mut *desc, proxy, false);\n" + - " return true;\n" + - " }\n") + setOrIndexedGet += (" FillPropertyDescriptor(&mut *desc, proxy, false);\n" + + " return true;\n" + + " }\n") setOrIndexedGet += "}" if indexedGetter: setOrIndexedGet += (" else {\n" + @@ -3674,29 +3679,30 @@ class CGDOMJSProxyHandler_getOwnPropertyDescriptor(CGAbstractExternMethod): # properties that shadow prototype properties. namedGet = ("\n" + "if !set && RUST_JSID_IS_STRING(id) != 0 && !HasPropertyOnPrototype(cx, proxy, id) {\n" + - " let name = jsid_to_str(cx, id);\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let name = jsid_to_str(cx, id);\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyNamedGetter(self.descriptor, templateValues)).define() + "\n" + "}\n") else: namedGet = "" - return setOrIndexedGet + """let expando: *mut JSObject = GetExpandoObject(proxy); + return setOrIndexedGet + """\ +let expando: *mut JSObject = GetExpandoObject(proxy); //if (!xpc::WrapperFactory::IsXrayWrapper(proxy) && (expando = GetExpandoObject(proxy))) { if expando.is_not_null() { - let flags = if set { JSRESOLVE_ASSIGNING } else { 0 } | JSRESOLVE_QUALIFIED; - if JS_GetPropertyDescriptorById(cx, expando, id, flags, desc) == 0 { - return false; - } - if (*desc).obj.is_not_null() { - // Pretend the property lives on the wrapper. - (*desc).obj = proxy; - return true; - } + let flags = if set { JSRESOLVE_ASSIGNING } else { 0 } | JSRESOLVE_QUALIFIED; + if JS_GetPropertyDescriptorById(cx, expando, id, flags, desc) == 0 { + return false; + } + if (*desc).obj.is_not_null() { + // Pretend the property lives on the wrapper. + (*desc).obj = proxy; + return true; + } } -""" + namedGet + """ +""" + namedGet + """\ (*desc).obj = ptr::null_mut(); return true;""" @@ -3719,17 +3725,17 @@ class CGDOMJSProxyHandler_defineProperty(CGAbstractExternMethod): raise TypeError("Can't handle creator that's different from the setter") set += ("let index = GetArrayIndexFromId(cx, id);\n" + "if index.is_some() {\n" + - " let index = index.unwrap();\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let index = index.unwrap();\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyIndexedSetter(self.descriptor)).define() + - " return true;\n" + + " return true;\n" + "}\n") elif self.descriptor.operations['IndexedGetter']: set += ("if GetArrayIndexFromId(cx, id).is_some() {\n" + - " return false;\n" + - " //return ThrowErrorMessage(cx, MSG_NO_PROPERTY_SETTER, \"%s\");\n" + + " return false;\n" + + " //return ThrowErrorMessage(cx, MSG_NO_PROPERTY_SETTER, \"%s\");\n" + "}\n") % self.descriptor.name namedSetter = self.descriptor.operations['NamedSetter'] @@ -3737,24 +3743,24 @@ class CGDOMJSProxyHandler_defineProperty(CGAbstractExternMethod): if not self.descriptor.operations['NamedCreator'] is namedSetter: raise TypeError("Can't handle creator that's different from the setter") set += ("if RUST_JSID_IS_STRING(id) != 0 {\n" + - " let name = jsid_to_str(cx, id);\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + - CGIndenter(CGProxyNamedSetter(self.descriptor)).define() + "\n" + + " let name = jsid_to_str(cx, id);\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + + CGIndenter(CGProxyNamedSetter(self.descriptor)).define() + "}\n") elif self.descriptor.operations['NamedGetter']: set += ("if RUST_JSID_IS_STRING(id) != 0 {\n" + - " let name = jsid_to_str(cx, id);\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let name = jsid_to_str(cx, id);\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyNamedGetter(self.descriptor)).define() + - " if (found) {\n" - " return false;\n" + - " //return ThrowErrorMessage(cx, MSG_NO_PROPERTY_SETTER, \"%s\");\n" + - " }\n" + - " return true;\n" + " if (found) {\n" + " return false;\n" + + " //return ThrowErrorMessage(cx, MSG_NO_PROPERTY_SETTER, \"%s\");\n" + + " }\n" + + " return true;\n" "}\n") % (self.descriptor.name) return set + """return proxyhandler::defineProperty_(%s);""" % ", ".join(a.name for a in self.args) @@ -3794,13 +3800,13 @@ class CGDOMJSProxyHandler_hasOwn(CGAbstractExternMethod): if indexedGetter: indexed = ("let index = GetArrayIndexFromId(cx, id);\n" + "if index.is_some() {\n" + - " let index = index.unwrap();\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let index = index.unwrap();\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyIndexedGetter(self.descriptor)).define() + "\n" + - " *bp = found;\n" + - " return true;\n" + + " *bp = found;\n" + + " return true;\n" + "}\n\n") else: indexed = "" @@ -3808,29 +3814,30 @@ class CGDOMJSProxyHandler_hasOwn(CGAbstractExternMethod): namedGetter = self.descriptor.operations['NamedGetter'] if namedGetter: named = ("if RUST_JSID_IS_STRING(id) != 0 && !HasPropertyOnPrototype(cx, proxy, id) {\n" + - " let name = jsid_to_str(cx, id);\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let name = jsid_to_str(cx, id);\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyNamedGetter(self.descriptor)).define() + "\n" + - " *bp = found;\n" - " return true;\n" + " *bp = found;\n" + " return true;\n" "}\n" + "\n") else: named = "" - return indexed + """let expando: *mut JSObject = GetExpandoObject(proxy); + return indexed + """\ +let expando: *mut JSObject = GetExpandoObject(proxy); if expando.is_not_null() { - let mut b: JSBool = 1; - let ok = JS_HasPropertyById(cx, expando, id, &mut b) != 0; - *bp = b != 0; - if !ok || *bp { - return ok; - } + let mut b: JSBool = 1; + let ok = JS_HasPropertyById(cx, expando, id, &mut b) != 0; + *bp = b != 0; + if !ok || *bp { + return ok; + } } - -""" + named + """*bp = false; +""" + named + """\ +*bp = false; return true;""" def definition_body(self): @@ -3844,16 +3851,17 @@ class CGDOMJSProxyHandler_get(CGAbstractExternMethod): CGAbstractExternMethod.__init__(self, descriptor, "get", "bool", args) self.descriptor = descriptor def getBody(self): - getFromExpando = """let expando = GetExpandoObject(proxy); + getFromExpando = """\ +let expando = GetExpandoObject(proxy); if expando.is_not_null() { - let mut hasProp = 0; - if JS_HasPropertyById(cx, expando, id, &mut hasProp) == 0 { - return false; - } + let mut hasProp = 0; + if JS_HasPropertyById(cx, expando, id, &mut hasProp) == 0 { + return false; + } - if hasProp != 0 { - return JS_GetPropertyById(cx, expando, id, vp) != 0; - } + if hasProp != 0 { + return JS_GetPropertyById(cx, expando, id, vp) != 0; + } }""" templateValues = { @@ -3865,44 +3873,45 @@ if expando.is_not_null() { if indexedGetter: getIndexedOrExpando = ("let index = GetArrayIndexFromId(cx, id);\n" + "if index.is_some() {\n" + - " let index = index.unwrap();\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let index = index.unwrap();\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyIndexedGetter(self.descriptor, templateValues)).define()) - getIndexedOrExpando += """ - // Even if we don't have this index, we don't forward the - // get on to our expando object. + getIndexedOrExpando += """\ + // Even if we don't have this index, we don't forward the + // get on to our expando object. } else { - %s + %s } -""" % (stripTrailingWhitespace(getFromExpando.replace('\n', '\n '))) +""" % (stripTrailingWhitespace(getFromExpando.replace('\n', '\n '))) else: getIndexedOrExpando = getFromExpando + "\n" namedGetter = self.descriptor.operations['NamedGetter'] if namedGetter: getNamed = ("if (RUST_JSID_IS_STRING(id) != 0) {\n" + - " let name = jsid_to_str(cx, id);\n" + - " let this = UnwrapProxy(proxy);\n" + - " let this = JS::from_raw(this);\n" + - " let this = this.root();\n" + + " let name = jsid_to_str(cx, id);\n" + + " let this = UnwrapProxy(proxy);\n" + + " let this = JS::from_raw(this);\n" + + " let this = this.root();\n" + CGIndenter(CGProxyNamedGetter(self.descriptor, templateValues)).define() + "}\n") else: getNamed = "" - return """//MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(proxy), - //"Should not have a XrayWrapper here"); + return """\ +//MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(proxy), +//"Should not have a XrayWrapper here"); %s let mut found = false; if !GetPropertyOnPrototype(cx, proxy, id, &mut found, vp) { - return false; + return false; } if found { - return true; + return true; } %s *vp = UndefinedValue(); @@ -3931,8 +3940,7 @@ class CGDOMJSProxyHandler_obj_toString(CGAbstractExternMethod): else: error = None call = CGCallGenerator(error, [], "", returnType, extendedAttributes, self.descriptor, nativeName, False, object="UnwrapProxy(proxy)") - return call.define() + """ - + return call.define() + """\ JSString* jsresult; return xpc_qsStringToJsstring(cx, result, &jsresult) ? jsresult : NULL;""" @@ -3966,9 +3974,10 @@ let this: *const %s = unwrap::<%s>(obj); assert(False) def finalizeHook(descriptor, hookName, context): - release = """let value = unwrap::<%s>(obj); + release = """\ +let value = unwrap::<%s>(obj); let _: Box<%s> = mem::transmute(value); -debug!("%s finalize: {:p}", this); +debug!("%s finalize: {:p}", this);\ """ % (descriptor.concreteType, descriptor.concreteType, descriptor.concreteType) return release @@ -4027,10 +4036,7 @@ class CGDOMJSProxyHandlerDOMClass(CGThing): self.descriptor = descriptor def define(self): - return """ -static Class: DOMClass = """ + DOMClass(self.descriptor) + """; - -""" + return "static Class: DOMClass = " + DOMClass(self.descriptor) + ";\n" class CGInterfaceTrait(CGThing): @@ -4218,7 +4224,6 @@ class CGDescriptor(CGThing): cgThings.append(CGInterfaceTrait(descriptor)) cgThings = CGList(cgThings, "\n") - cgThings = CGWrapper(cgThings, pre='\n', post='\n') #self.cgRoot = CGWrapper(CGNamespace(toBindingNamespace(descriptor.name), # cgThings), # post='\n') @@ -4246,7 +4251,7 @@ class CGNonNamespacedEnum(CGThing): entries.append('Count = ' + str(len(entries))) # Indent. - entries = [' ' + e for e in entries] + entries = [' ' + e for e in entries] # Build the enum body. enumstr = comment + 'pub enum %s {\n%s\n}\n' % (enumName, ',\n'.join(entries)) @@ -4295,11 +4300,11 @@ class CGDictionary(CGThing): def struct(self): d = self.dictionary if d.parent: - inheritance = " pub parent: %s::%s<'a, 'b>,\n" % (self.makeModuleName(d.parent), - self.makeClassName(d.parent)) + inheritance = " pub parent: %s::%s<'a, 'b>,\n" % (self.makeModuleName(d.parent), + self.makeClassName(d.parent)) else: inheritance = "" - memberDecls = [" pub %s: %s," % + memberDecls = [" pub %s: %s," % (self.makeMemberName(m[0].identifier.name), self.getMemberType(m)) for m in self.memberInfo] @@ -4314,8 +4319,8 @@ class CGDictionary(CGThing): d = self.dictionary if d.parent: initParent = ("parent: match %s::%s::new(cx, val) {\n" - " Ok(parent) => parent,\n" - " Err(_) => return Err(()),\n" + " Ok(parent) => parent,\n" + " Err(_) => return Err(()),\n" "},\n") % (self.makeModuleName(d.parent), self.makeClassName(d.parent)) else: @@ -4331,27 +4336,27 @@ class CGDictionary(CGThing): return string.Template( "impl<'a, 'b> ${selfName}<'a, 'b> {\n" - " pub fn empty() -> ${selfName}<'a, 'b> {\n" - " ${selfName}::new(ptr::null_mut(), NullValue()).unwrap()\n" - " }\n" - " pub fn new(cx: *mut JSContext, val: JSVal) -> Result<${selfName}<'a, 'b>, ()> {\n" - " let object = if val.is_null_or_undefined() {\n" - " ptr::null_mut()\n" - " } else if val.is_object() {\n" - " val.to_object()\n" - " } else {\n" - " throw_type_error(cx, \"Value not an object.\");\n" - " return Err(());\n" - " };\n" - " Ok(${selfName} {\n" + " pub fn empty() -> ${selfName}<'a, 'b> {\n" + " ${selfName}::new(ptr::null_mut(), NullValue()).unwrap()\n" + " }\n" + " pub fn new(cx: *mut JSContext, val: JSVal) -> Result<${selfName}<'a, 'b>, ()> {\n" + " let object = if val.is_null_or_undefined() {\n" + " ptr::null_mut()\n" + " } else if val.is_object() {\n" + " val.to_object()\n" + " } else {\n" + " throw_type_error(cx, \"Value not an object.\");\n" + " return Err(());\n" + " };\n" + " Ok(${selfName} {\n" "${initParent}" "${initMembers}" - " })\n" - " }\n" + " })\n" + " }\n" "}").substitute({ "selfName": self.makeClassName(d), - "initParent": CGIndenter(CGGeneric(initParent), indentLevel=6).define(), - "initMembers": CGIndenter(memberInits, indentLevel=6).define(), + "initParent": CGIndenter(CGGeneric(initParent), indentLevel=12).define(), + "initMembers": CGIndenter(memberInits, indentLevel=12).define(), }) @staticmethod @@ -4652,7 +4657,6 @@ class CGNativeMember(ClassMethod): # have a non-void return type, as const. const=(not member.isStatic() and member.isAttr() and not signature[0].isVoid()), - breakAfterReturnDecl=" ", breakAfterSelf=breakAfterSelf, visibility=visibility) @@ -4734,14 +4738,14 @@ class CGCallback(CGClass): setupCall = ("let s = CallSetup::new(self, aExceptionHandling);\n" "if s.GetContext().is_null() {\n" - " return Err(FailureUnknown);\n" + " return Err(FailureUnknown);\n" "}\n") bodyWithThis = string.Template( setupCall+ "let thisObjJS = WrapCallThisObject(s.GetContext(), thisObj);\n" "if thisObjJS.is_null() {\n" - " return Err(FailureUnknown);\n" + " return Err(FailureUnknown);\n" "}\n" "return ${methodName}(${callArgs});").substitute({ "callArgs" : ", ".join(argnamesWithThis), @@ -4785,7 +4789,8 @@ class CGCallbackFunction(CGCallback): class CGCallbackFunctionImpl(CGGeneric): def __init__(self, callback): - impl = string.Template("""impl CallbackContainer for ${type} { + impl = string.Template("""\ +impl CallbackContainer for ${type} { fn new(callback: *mut JSObject) -> ${type} { ${type}::new(callback) } @@ -4799,7 +4804,7 @@ impl ToJSValConvertible for ${type} { fn to_jsval(&self, cx: *mut JSContext) -> JSVal { self.callback().to_jsval(cx) } -} +}\ """).substitute({"type": callback.name}) CGGeneric.__init__(self, impl) @@ -4867,7 +4872,7 @@ class CallbackMember(CGNativeMember): jsObjectsArePtr=True) # We have to do all the generation of our body now, because # the caller relies on us throwing if we can't manage it. - self.exceptionCode= "return Err(FailureUnknown);\n" + self.exceptionCode= "return Err(FailureUnknown);" self.body = self.getImpl() def getImpl(self): @@ -4959,17 +4964,17 @@ class CallbackMember(CGNativeMember): conversion = string.Template( "for idx in range(0, ${arg}.len()) {\n" + CGIndenter(CGGeneric(conversion)).define() + "\n" - "}\n" + "}" ).substitute({ "arg": arg.identifier.name }) elif arg.optional and not arg.defaultValue: conversion = ( CGIfWrapper(CGGeneric(conversion), "%s.is_some()" % arg.identifier.name).define() + " else if (argc == %d) {\n" - " // This is our current trailing argument; reduce argc\n" - " argc -= 1;\n" + " // This is our current trailing argument; reduce argc\n" + " argc -= 1;\n" "} else {\n" - " argv[%d] = UndefinedValue();\n" + " argv[%d] = UndefinedValue();\n" "}" % (i+1, i)) return conversion @@ -5005,7 +5010,7 @@ class CallbackMember(CGNativeMember): "${callSetup}\n" "JSContext* cx = s.GetContext();\n" "if (!cx) {\n" - " return Err(FailureUnknown);\n" + " return Err(FailureUnknown);\n" "}\n").substitute({ "callSetup": callSetup, }) @@ -5048,11 +5053,11 @@ class CallbackMethod(CallbackMember): replacements["argc"] = "0" return string.Template("${getCallable}" "let ok = unsafe {\n" - " JS_CallFunctionValue(cx, ${thisObj}, callable,\n" - " ${argc}, ${argv}, &mut rval)\n" + " JS_CallFunctionValue(cx, ${thisObj}, callable,\n" + " ${argc}, ${argv}, &mut rval)\n" "};\n" "if ok == 0 {\n" - " return Err(FailureUnknown);\n" + " return Err(FailureUnknown);\n" "}\n").substitute(replacements) class CallCallback(CallbackMethod): @@ -5064,7 +5069,7 @@ class CallCallback(CallbackMethod): return "aThisObj" def getCallableDecl(self): - return "let callable = ObjectValue(unsafe {&*self.parent.callback()});\n"; + return "let callable = ObjectValue(unsafe {&*self.parent.callback()});\n" class CallbackOperationBase(CallbackMethod): """ @@ -5089,8 +5094,8 @@ class CallbackOperationBase(CallbackMethod): } getCallableFromProp = string.Template( 'match self.parent.GetCallableProperty(cx, "${methodName}") {\n' - ' Err(_) => return Err(FailureUnknown),\n' - ' Ok(callable) => callable,\n' + ' Err(_) => return Err(FailureUnknown),\n' + ' Ok(callable) => callable,\n' '}').substitute(replacements) if not self.singleOperation: return 'JS::Rooted<JS::Value> callable(cx);\n' + getCallableFromProp @@ -5134,7 +5139,7 @@ class CallbackGetter(CallbackMember): } return string.Template( 'if (!JS_GetProperty(cx, mCallback, "${attrName}", &rval)) {\n' - ' return Err(FailureUnknown);\n' + ' return Err(FailureUnknown);\n' '}\n').substitute(replacements); class CallbackSetter(CallbackMember): @@ -5161,7 +5166,7 @@ class CallbackSetter(CallbackMember): return string.Template( 'MOZ_ASSERT(argv.length() == 1);\n' 'if (!JS_SetProperty(cx, mCallback, "${attrName}", ${argv})) {\n' - ' return Err(FailureUnknown);\n' + ' return Err(FailureUnknown);\n' '}\n').substitute(replacements) def getArgcDecl(self): @@ -5243,68 +5248,70 @@ class GlobalGenRoots(): (name + 'Derived', 'is_' + name.lower()))] for protoName in descriptor.prototypeChain[1:-1]: protoDescriptor = config.getDescriptor(protoName) - delegate = string.Template('''impl ${selfName} for ${baseName} { - #[inline] - fn ${fname}(&self) -> bool { - ${parentName}Cast::from_actual(self).${fname}() - } -} -''').substitute({'fname': 'is_' + name.lower(), + delegate = string.Template("""\ +impl ${selfName} for ${baseName} { + #[inline] + fn ${fname}(&self) -> bool { + ${parentName}Cast::from_actual(self).${fname}() + } +}\ +""").substitute({'fname': 'is_' + name.lower(), 'selfName': name + 'Derived', 'baseName': protoDescriptor.concreteType, 'parentName': protoDescriptor.prototypeChain[-2]}) derived += [CGGeneric(delegate)] derived += [CGGeneric('\n')] - cast = [CGGeneric(string.Template('''pub trait ${castTraitName} { - #[inline(always)] - fn to_ref<'a, T: ${toBound}+Reflectable>(base: JSRef<'a, T>) -> Option<JSRef<'a, Self>> { - match base.${checkFn}() { - true => unsafe { Some(base.transmute()) }, - false => None + cast = [CGGeneric(string.Template("""\ +pub trait ${castTraitName} { + #[inline(always)] + fn to_ref<'a, T: ${toBound}+Reflectable>(base: JSRef<'a, T>) -> Option<JSRef<'a, Self>> { + match base.${checkFn}() { + true => unsafe { Some(base.transmute()) }, + false => None + } } - } - #[inline(always)] - fn to_borrowed_ref<'a, 'b, T: ${toBound}+Reflectable>(base: &'a JSRef<'b, T>) -> Option<&'a JSRef<'b, Self>> { - match base.${checkFn}() { - true => unsafe { Some(base.transmute_borrowed()) }, - false => None - } - } - - #[inline(always)] - #[allow(unrooted_must_root)] - fn to_js<T: ${toBound}+Reflectable>(base: &JS<T>) -> Option<JS<Self>> { - unsafe { - match (*base.unsafe_get()).${checkFn}() { - true => Some(base.transmute_copy()), + #[inline(always)] + fn to_borrowed_ref<'a, 'b, T: ${toBound}+Reflectable>(base: &'a JSRef<'b, T>) -> Option<&'a JSRef<'b, Self>> { + match base.${checkFn}() { + true => unsafe { Some(base.transmute_borrowed()) }, false => None } } - } - - #[inline(always)] - fn from_ref<'a, T: ${fromBound}+Reflectable>(derived: JSRef<'a, T>) -> JSRef<'a, Self> { - unsafe { derived.transmute() } - } - - #[inline(always)] - fn from_borrowed_ref<'a, 'b, T: ${fromBound}+Reflectable>(derived: &'a JSRef<'b, T>) -> &'a JSRef<'b, Self> { - unsafe { derived.transmute_borrowed() } - } - - #[inline(always)] - fn from_temporary<T: ${fromBound}+Reflectable>(derived: Temporary<T>) -> Temporary<Self> { - unsafe { derived.transmute() } - } - - #[inline(always)] - fn from_actual<'a, T: ${fromBound}+Reflectable>(derived: &T) -> &'a Self { - unsafe { mem::transmute(derived) } - } + + #[inline(always)] + #[allow(unrooted_must_root)] + fn to_js<T: ${toBound}+Reflectable>(base: &JS<T>) -> Option<JS<Self>> { + unsafe { + match (*base.unsafe_get()).${checkFn}() { + true => Some(base.transmute_copy()), + false => None + } + } + } + + #[inline(always)] + fn from_ref<'a, T: ${fromBound}+Reflectable>(derived: JSRef<'a, T>) -> JSRef<'a, Self> { + unsafe { derived.transmute() } + } + + #[inline(always)] + fn from_borrowed_ref<'a, 'b, T: ${fromBound}+Reflectable>(derived: &'a JSRef<'b, T>) -> &'a JSRef<'b, Self> { + unsafe { derived.transmute_borrowed() } + } + + #[inline(always)] + fn from_temporary<T: ${fromBound}+Reflectable>(derived: Temporary<T>) -> Temporary<Self> { + unsafe { derived.transmute() } + } + + #[inline(always)] + fn from_actual<'a, T: ${fromBound}+Reflectable>(derived: &T) -> &'a Self { + unsafe { mem::transmute(derived) } + } } -''').substitute({'checkFn': 'is_' + name.lower(), +""").substitute({'checkFn': 'is_' + name.lower(), 'castTraitName': name + 'Cast', 'fromBound': name + 'Base', 'toBound': name + 'Derived'})), |