mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Implement FromJSValConvertible for union types.
This commit is contained in:
parent
adcc7e694f
commit
9cc5460dd6
1 changed files with 18 additions and 9 deletions
|
@ -581,10 +581,10 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
|
||||||
declType = CGWrapper(declType, pre="Option<", post=" >")
|
declType = CGWrapper(declType, pre="Option<", post=" >")
|
||||||
value = CGWrapper(value, pre="Some(", post=")")
|
value = CGWrapper(value, pre="Some(", post=")")
|
||||||
|
|
||||||
templateBody = CGGeneric("match %s::from_value(cx, ${val}) {\n"
|
templateBody = CGGeneric("match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
|
||||||
" Err(()) => { %s },\n"
|
" Err(()) => { %s },\n"
|
||||||
" Ok(value) => %s,\n"
|
" Ok(value) => %s,\n"
|
||||||
"}" % (type.name, exceptionCode, value.define()))
|
"}" % (exceptionCode, value.define()))
|
||||||
|
|
||||||
if type.nullable():
|
if type.nullable():
|
||||||
templateBody = CGIfElseWrapper(
|
templateBody = CGIfElseWrapper(
|
||||||
|
@ -2847,7 +2847,7 @@ class CGUnionConversionStruct(CGThing):
|
||||||
self.type = type
|
self.type = type
|
||||||
self.descriptorProvider = descriptorProvider
|
self.descriptorProvider = descriptorProvider
|
||||||
|
|
||||||
def from_value_method(self):
|
def from_jsval(self):
|
||||||
memberTypes = self.type.flatMemberTypes
|
memberTypes = self.type.flatMemberTypes
|
||||||
names = []
|
names = []
|
||||||
conversions = []
|
conversions = []
|
||||||
|
@ -2941,9 +2941,13 @@ class CGUnionConversionStruct(CGThing):
|
||||||
conversions.append(CGGeneric(
|
conversions.append(CGGeneric(
|
||||||
"throw_not_in_union(cx, \"%s\");\n"
|
"throw_not_in_union(cx, \"%s\");\n"
|
||||||
"Err(())" % ", ".join(names)))
|
"Err(())" % ", ".join(names)))
|
||||||
return CGWrapper(
|
method = CGWrapper(
|
||||||
CGIndenter(CGList(conversions, "\n\n")),
|
CGIndenter(CGList(conversions, "\n\n")),
|
||||||
pre="pub fn from_value(cx: *JSContext, value: JSVal) -> Result<%s, ()> {\n" % self.type,
|
pre="fn from_jsval(cx: *JSContext, value: JSVal, _option: ()) -> Result<%s, ()> {\n" % self.type,
|
||||||
|
post="\n}")
|
||||||
|
return CGWrapper(
|
||||||
|
CGIndenter(method),
|
||||||
|
pre="impl FromJSValConvertible<()> for %s {\n" % self.type,
|
||||||
post="\n}")
|
post="\n}")
|
||||||
|
|
||||||
def try_method(self, t):
|
def try_method(self, t):
|
||||||
|
@ -2953,16 +2957,21 @@ class CGUnionConversionStruct(CGThing):
|
||||||
|
|
||||||
return CGWrapper(
|
return CGWrapper(
|
||||||
CGIndenter(jsConversion, 4),
|
CGIndenter(jsConversion, 4),
|
||||||
pre="pub fn TryConvertTo%s(cx: *JSContext, value: JSVal) -> %s {\n" % (t.name, returnType),
|
pre="fn TryConvertTo%s(cx: *JSContext, value: JSVal) -> %s {\n" % (t.name, returnType),
|
||||||
post="\n}")
|
post="\n}")
|
||||||
|
|
||||||
def define(self):
|
def define(self):
|
||||||
methods = [self.from_value_method()]
|
from_jsval = self.from_jsval()
|
||||||
methods.extend(self.try_method(t) for t in self.type.flatMemberTypes)
|
methods = CGIndenter(CGList([
|
||||||
|
self.try_method(t) for t in self.type.flatMemberTypes
|
||||||
|
], "\n\n"))
|
||||||
return """
|
return """
|
||||||
|
%s
|
||||||
|
|
||||||
impl %s {
|
impl %s {
|
||||||
%s
|
%s
|
||||||
}""" % (self.type, CGIndenter(CGList(methods, "\n\n")).define())
|
}
|
||||||
|
""" % (from_jsval.define(), self.type, methods.define())
|
||||||
|
|
||||||
|
|
||||||
class ClassItem:
|
class ClassItem:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue