mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Introduce a NativeProperties struct to store the properties.
This will simplify adding more kinds of properties, such as static attributes.
This commit is contained in:
parent
da28a791e5
commit
886d401ff0
2 changed files with 43 additions and 16 deletions
|
@ -1064,9 +1064,10 @@ class PropertyDefiner:
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def variableName(self):
|
def variableName(self):
|
||||||
if len(self.regular) > 0:
|
return "s" + self.name
|
||||||
return "s" + self.name
|
|
||||||
return "ptr::null()"
|
def length(self):
|
||||||
|
return len(self.regular)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
# We only need to generate id arrays for things that will end
|
# We only need to generate id arrays for things that will end
|
||||||
|
@ -1853,6 +1854,31 @@ class PropertyArrays():
|
||||||
define += str(getattr(self, array))
|
define += str(getattr(self, array))
|
||||||
return define
|
return define
|
||||||
|
|
||||||
|
|
||||||
|
class CGNativeProperties(CGThing):
|
||||||
|
def __init__(self, descriptor, properties):
|
||||||
|
CGThing.__init__(self)
|
||||||
|
self.properties = properties
|
||||||
|
|
||||||
|
def define(self):
|
||||||
|
def getField(array):
|
||||||
|
propertyArray = getattr(self.properties, array)
|
||||||
|
if propertyArray.length() > 0:
|
||||||
|
value = "Some(%s)" % propertyArray.variableName()
|
||||||
|
else:
|
||||||
|
value = "None"
|
||||||
|
|
||||||
|
return CGGeneric(string.Template('${name}: ${value},').substitute({
|
||||||
|
'name': array,
|
||||||
|
'value': value,
|
||||||
|
}))
|
||||||
|
|
||||||
|
nativeProps = CGList([getField(array) for array in self.properties.arrayNames()], '\n')
|
||||||
|
return CGWrapper(CGIndenter(nativeProps),
|
||||||
|
pre="static sNativeProperties: NativeProperties = NativeProperties {\n",
|
||||||
|
post="\n};\n").define()
|
||||||
|
|
||||||
|
|
||||||
class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
|
class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
|
||||||
"""
|
"""
|
||||||
Generate the CreateInterfaceObjects method for an interface descriptor.
|
Generate the CreateInterfaceObjects method for an interface descriptor.
|
||||||
|
@ -1890,12 +1916,6 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
|
||||||
else:
|
else:
|
||||||
domClass = "ptr::null()"
|
domClass = "ptr::null()"
|
||||||
|
|
||||||
def arrayPtr(name):
|
|
||||||
val = ('%(' + name + ')s') % self.properties.variableNames()
|
|
||||||
if val == "ptr::null()":
|
|
||||||
return "None"
|
|
||||||
return "Some(%s)" % val
|
|
||||||
|
|
||||||
if needInterfaceObject:
|
if needInterfaceObject:
|
||||||
if self.descriptor.interface.ctor():
|
if self.descriptor.interface.ctor():
|
||||||
constructHook = CONSTRUCT_HOOK_NAME
|
constructHook = CONSTRUCT_HOOK_NAME
|
||||||
|
@ -1913,14 +1933,12 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
|
||||||
call = """return CreateInterfaceObjects2(aCx, aGlobal, aReceiver, parentProto,
|
call = """return CreateInterfaceObjects2(aCx, aGlobal, aReceiver, parentProto,
|
||||||
&PrototypeClass, %s,
|
&PrototypeClass, %s,
|
||||||
%s,
|
%s,
|
||||||
%s,
|
sNativeProperties.methods,
|
||||||
%s,
|
sNativeProperties.attrs,
|
||||||
%s,
|
sNativeProperties.consts,
|
||||||
%s);""" % (
|
sNativeProperties.staticMethods);""" % (
|
||||||
constructor,
|
constructor,
|
||||||
domClass,
|
domClass)
|
||||||
arrayPtr("methods"), arrayPtr("attrs"),
|
|
||||||
arrayPtr("consts"), arrayPtr("staticMethods"))
|
|
||||||
|
|
||||||
return CGList([
|
return CGList([
|
||||||
CGGeneric(getParentProto),
|
CGGeneric(getParentProto),
|
||||||
|
@ -3863,6 +3881,7 @@ class CGDescriptor(CGThing):
|
||||||
|
|
||||||
properties = PropertyArrays(descriptor)
|
properties = PropertyArrays(descriptor)
|
||||||
cgThings.append(CGGeneric(str(properties)))
|
cgThings.append(CGGeneric(str(properties)))
|
||||||
|
cgThings.append(CGNativeProperties(descriptor, properties))
|
||||||
cgThings.append(CGCreateInterfaceObjectsMethod(descriptor, properties))
|
cgThings.append(CGCreateInterfaceObjectsMethod(descriptor, properties))
|
||||||
|
|
||||||
cgThings.append(CGNamespace.build([descriptor.name + "Constants"],
|
cgThings.append(CGNamespace.build([descriptor.name + "Constants"],
|
||||||
|
@ -4233,6 +4252,7 @@ class CGBindingRoot(CGThing):
|
||||||
'dom::bindings::utils::{ThrowingConstructor, unwrap, unwrap_jsmanaged}',
|
'dom::bindings::utils::{ThrowingConstructor, unwrap, unwrap_jsmanaged}',
|
||||||
'dom::bindings::utils::VoidVal',
|
'dom::bindings::utils::VoidVal',
|
||||||
'dom::bindings::utils::get_dictionary_property',
|
'dom::bindings::utils::get_dictionary_property',
|
||||||
|
'dom::bindings::utils::NativeProperties',
|
||||||
'dom::bindings::trace::JSTraceable',
|
'dom::bindings::trace::JSTraceable',
|
||||||
'dom::bindings::callback::{CallbackContainer,CallbackInterface,CallbackFunction}',
|
'dom::bindings::callback::{CallbackContainer,CallbackInterface,CallbackFunction}',
|
||||||
'dom::bindings::callback::{CallSetup,ExceptionHandling}',
|
'dom::bindings::callback::{CallSetup,ExceptionHandling}',
|
||||||
|
|
|
@ -215,6 +215,13 @@ pub fn GetProtoOrIfaceArray(global: *mut JSObject) -> *mut *mut JSObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct NativeProperties {
|
||||||
|
pub methods: Option<&'static [JSFunctionSpec]>,
|
||||||
|
pub attrs: Option<&'static [JSPropertySpec]>,
|
||||||
|
pub consts: Option<&'static [ConstantSpec]>,
|
||||||
|
pub staticMethods: Option<&'static [JSFunctionSpec]>,
|
||||||
|
}
|
||||||
|
|
||||||
pub type NonNullJSNative =
|
pub type NonNullJSNative =
|
||||||
unsafe extern "C" fn (arg1: *mut JSContext, arg2: c_uint, arg3: *mut JSVal) -> JSBool;
|
unsafe extern "C" fn (arg1: *mut JSContext, arg2: c_uint, arg3: *mut JSVal) -> JSBool;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue