Fixing issue with uniontypes not created with primitive types

refer to #9531
This commit is contained in:
zakorgyula 2016-02-09 09:20:19 +01:00
parent 07cb6599d0
commit 6d6f23a69d
3 changed files with 135 additions and 20 deletions

View file

@ -3745,25 +3745,50 @@ class CGUnionConversionStruct(CGThing):
if arrayObject:
templateBody.append(arrayObject)
conversions.append(CGIfWrapper("value.get().is_object()", templateBody))
stringTypes = [t for t in memberTypes if t.isString() or t.isEnum()]
numericTypes = [t for t in memberTypes if t.isNumeric()]
booleanTypes = [t for t in memberTypes if t.isBoolean()]
if stringTypes or numericTypes or booleanTypes:
assert len(stringTypes) <= 1
assert len(numericTypes) <= 1
assert len(booleanTypes) <= 1
otherMemberTypes = [
t for t in memberTypes if t.isPrimitive() or t.isString() or t.isEnum()
]
if len(otherMemberTypes) > 0:
assert len(otherMemberTypes) == 1
memberType = otherMemberTypes[0]
if memberType.isEnum():
name = memberType.inner.identifier.name
def getStringOrPrimitiveConversion(memberType):
typename = get_name(memberType)
return CGGeneric(get_match(typename))
other = []
stringConversion = map(getStringOrPrimitiveConversion, stringTypes)
numericConversion = map(getStringOrPrimitiveConversion, numericTypes)
booleanConversion = map(getStringOrPrimitiveConversion, booleanTypes)
if stringConversion:
if booleanConversion:
other.append(CGIfWrapper("value.get().is_boolean()", booleanConversion[0]))
if numericConversion:
other.append(CGIfWrapper("value.get().is_number()", numericConversion[0]))
other.append(stringConversion[0])
elif numericConversion:
if booleanConversion:
other.append(CGIfWrapper("value.get().is_boolean()", booleanConversion[0]))
other.append(numericConversion[0])
else:
name = memberType.name
match = (
"match %s::TryConvertTo%s(cx, value) {\n"
" Err(_) => return Err(()),\n"
" Ok(Some(value)) => return Ok(%s::%s(value)),\n"
" Ok(None) => (),\n"
"}\n") % (self.type, name, self.type, name)
conversions.append(CGGeneric(match))
names.append(name)
assert booleanConversion
other.append(booleanConversion[0])
conversions.append(CGList(other, "\n\n"))
conversions.append(CGGeneric(
"throw_not_in_union(cx, \"%s\");\n"
"Err(())" % ", ".join(names)))
method = CGWrapper(
CGIndenter(CGList(conversions, "\n\n")),
pre="unsafe fn from_jsval(cx: *mut JSContext,\n"
" value: HandleValue, _option: ()) -> Result<%s, ()> {\n" % self.type,
post="\n}")
return CGWrapper(
CGIndenter(CGList([
CGGeneric("type Config = ();"),
method,
], "\n")),
pre="impl FromJSValConvertible for %s {\n" % self.type,
post="\n}")
conversions.append(CGGeneric(
"throw_not_in_union(cx, \"%s\");\n"

View file

@ -7,10 +7,12 @@
use dom::bindings::codegen::Bindings::EventListenerBinding::EventListener;
use dom::bindings::codegen::Bindings::FunctionBinding::Function;
use dom::bindings::codegen::Bindings::TestBindingBinding::{self, TestBindingMethods, TestEnum};
use dom::bindings::codegen::UnionTypes::{BlobOrBlobSequence};
use dom::bindings::codegen::UnionTypes::{BlobOrString, EventOrString};
use dom::bindings::codegen::UnionTypes::{BlobOrBoolean, BlobOrBlobSequence};
use dom::bindings::codegen::UnionTypes::{BlobOrString, BlobOrUnsignedLong, EventOrString};
use dom::bindings::codegen::UnionTypes::{EventOrUSVString, HTMLElementOrLong};
use dom::bindings::codegen::UnionTypes::{StringOrLongSequence, StringOrStringSequence};
use dom::bindings::codegen::UnionTypes::{HTMLElementOrUnsignedLongOrStringOrBoolean, LongSequenceOrBoolean};
use dom::bindings::codegen::UnionTypes::{StringOrLongSequence, StringOrStringSequence, StringSequenceOrUnsignedLong};
use dom::bindings::codegen::UnionTypes::{StringOrUnsignedLong, StringOrBoolean, UnsignedLongOrBoolean};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
@ -98,6 +100,26 @@ impl TestBindingMethods for TestBinding {
EventOrUSVString::USVString(USVString("".to_owned()))
}
fn SetUnion3Attribute(&self, _: EventOrUSVString) {}
fn Union4Attribute(&self) -> StringOrUnsignedLong {
StringOrUnsignedLong::UnsignedLong(0u32)
}
fn SetUnion4Attribute(&self, _: StringOrUnsignedLong) {}
fn Union5Attribute(&self) -> StringOrBoolean {
StringOrBoolean::Boolean(true)
}
fn SetUnion5Attribute(&self, _: StringOrBoolean) {}
fn Union6Attribute(&self) -> UnsignedLongOrBoolean {
UnsignedLongOrBoolean::Boolean(true)
}
fn SetUnion6Attribute(&self, _: UnsignedLongOrBoolean) {}
fn Union7Attribute(&self) -> BlobOrBoolean {
BlobOrBoolean::Boolean(true)
}
fn SetUnion7Attribute(&self, _: BlobOrBoolean) {}
fn Union8Attribute(&self) -> BlobOrUnsignedLong {
BlobOrUnsignedLong::UnsignedLong(0u32)
}
fn SetUnion8Attribute(&self, _: BlobOrUnsignedLong) {}
fn ArrayAttribute(&self, _: *mut JSContext) -> *mut JSObject { NullValue().to_object_or_null() }
fn AnyAttribute(&self, _: *mut JSContext) -> JSVal { NullValue() }
fn SetAnyAttribute(&self, _: *mut JSContext, _: HandleValue) {}
@ -164,6 +186,18 @@ impl TestBindingMethods for TestBinding {
Some(EventOrString::String(DOMString::new()))
}
fn SetUnion2AttributeNullable(&self, _: Option<EventOrString>) {}
fn GetUnion3AttributeNullable(&self) -> Option<BlobOrBoolean> {
Some(BlobOrBoolean::Boolean(true))
}
fn SetUnion3AttributeNullable(&self, _: Option<BlobOrBoolean>) {}
fn GetUnion4AttributeNullable(&self) -> Option<UnsignedLongOrBoolean> {
Some(UnsignedLongOrBoolean::Boolean(true))
}
fn SetUnion4AttributeNullable(&self, _: Option<UnsignedLongOrBoolean>) {}
fn GetUnion5AttributeNullable(&self) -> Option<StringOrBoolean> {
Some(StringOrBoolean::Boolean(true))
}
fn SetUnion5AttributeNullable(&self, _: Option<StringOrBoolean>) {}
fn BinaryRenamedMethod(&self) -> () {}
fn ReceiveVoid(&self) -> () {}
fn ReceiveBoolean(&self) -> bool { false }
@ -193,6 +227,12 @@ impl TestBindingMethods for TestBinding {
fn ReceiveUnion3(&self) -> StringOrLongSequence { StringOrLongSequence::LongSequence(vec![]) }
fn ReceiveUnion4(&self) -> StringOrStringSequence { StringOrStringSequence::StringSequence(vec![]) }
fn ReceiveUnion5(&self) -> BlobOrBlobSequence { BlobOrBlobSequence::BlobSequence(vec![]) }
fn ReceiveUnion6(&self) -> StringOrUnsignedLong { StringOrUnsignedLong::String(DOMString::new()) }
fn ReceiveUnion7(&self) -> StringOrBoolean { StringOrBoolean::Boolean(true) }
fn ReceiveUnion8(&self) -> UnsignedLongOrBoolean { UnsignedLongOrBoolean::UnsignedLong(0u32) }
fn ReceiveUnion9(&self) -> HTMLElementOrUnsignedLongOrStringOrBoolean {
HTMLElementOrUnsignedLongOrStringOrBoolean::Boolean(true)
}
fn ReceiveSequence(&self) -> Vec<i32> { vec![1] }
fn ReceiveInterfaceSequence(&self) -> Vec<Root<Blob>> {
vec![Blob::new(self.global().r(), Vec::new(), "")]
@ -228,6 +268,12 @@ impl TestBindingMethods for TestBinding {
fn ReceiveNullableUnion3(&self) -> Option<StringOrLongSequence> {
Some(StringOrLongSequence::String(DOMString::new()))
}
fn ReceiveNullableUnion4(&self) -> Option<LongSequenceOrBoolean> {
Some(LongSequenceOrBoolean::Boolean(true))
}
fn ReceiveNullableUnion5(&self) -> Option<UnsignedLongOrBoolean> {
Some(UnsignedLongOrBoolean::UnsignedLong(0u32))
}
fn ReceiveNullableSequence(&self) -> Option<Vec<i32>> { Some(vec![1]) }
fn PassBoolean(&self, _: bool) {}
@ -252,6 +298,9 @@ impl TestBindingMethods for TestBinding {
fn PassUnion2(&self, _: EventOrString) {}
fn PassUnion3(&self, _: BlobOrString) {}
fn PassUnion4(&self, _: StringOrStringSequence) {}
fn PassUnion5(&self, _: StringOrBoolean) {}
fn PassUnion6(&self, _: UnsignedLongOrBoolean) {}
fn PassUnion7(&self, _: StringSequenceOrUnsignedLong) {}
fn PassAny(&self, _: *mut JSContext, _: HandleValue) {}
fn PassObject(&self, _: *mut JSContext, _: *mut JSObject) {}
fn PassCallbackFunction(&self, _: Rc<Function>) {}
@ -281,6 +330,9 @@ impl TestBindingMethods for TestBinding {
fn PassNullableObject(&self, _: *mut JSContext, _: *mut JSObject) {}
fn PassNullableUnion(&self, _: Option<HTMLElementOrLong>) {}
fn PassNullableUnion2(&self, _: Option<EventOrString>) {}
fn PassNullableUnion3(&self, _: Option<StringOrLongSequence>) {}
fn PassNullableUnion4(&self, _: Option<LongSequenceOrBoolean>) {}
fn PassNullableUnion5(&self, _: Option<UnsignedLongOrBoolean>) {}
fn PassNullableCallbackFunction(&self, _: Option<Rc<Function>>) {}
fn PassNullableCallbackInterface(&self, _: Option<Rc<EventListener>>) {}
fn PassNullableSequence(&self, _: Option<Vec<i32>>) {}
@ -305,6 +357,9 @@ impl TestBindingMethods for TestBinding {
fn PassOptionalInterface(&self, _: Option<&Blob>) {}
fn PassOptionalUnion(&self, _: Option<HTMLElementOrLong>) {}
fn PassOptionalUnion2(&self, _: Option<EventOrString>) {}
fn PassOptionalUnion3(&self, _: Option<StringOrLongSequence>) {}
fn PassOptionalUnion4(&self, _: Option<LongSequenceOrBoolean>) {}
fn PassOptionalUnion5(&self, _: Option<UnsignedLongOrBoolean>) {}
fn PassOptionalAny(&self, _: *mut JSContext, _: HandleValue) {}
fn PassOptionalObject(&self, _: *mut JSContext, _: Option<*mut JSObject>) {}
fn PassOptionalCallbackFunction(&self, _: Option<Rc<Function>>) {}
@ -332,6 +387,9 @@ impl TestBindingMethods for TestBinding {
fn PassOptionalNullableObject(&self, _: *mut JSContext, _: Option<*mut JSObject>) {}
fn PassOptionalNullableUnion(&self, _: Option<Option<HTMLElementOrLong>>) {}
fn PassOptionalNullableUnion2(&self, _: Option<Option<EventOrString>>) {}
fn PassOptionalNullableUnion3(&self, _: Option<Option<StringOrLongSequence>>) {}
fn PassOptionalNullableUnion4(&self, _: Option<Option<LongSequenceOrBoolean>>) {}
fn PassOptionalNullableUnion5(&self, _: Option<Option<UnsignedLongOrBoolean>>) {}
fn PassOptionalNullableCallbackFunction(&self, _: Option<Option<Rc<Function>>>) {}
fn PassOptionalNullableCallbackInterface(&self, _: Option<Option<Rc<EventListener>>>) {}
fn PassOptionalNullableSequence(&self, _: Option<Option<Vec<i32>>>) {}
@ -413,6 +471,9 @@ impl TestBindingMethods for TestBinding {
fn PassVariadicUnion(&self, _: Vec<HTMLElementOrLong>) {}
fn PassVariadicUnion2(&self, _: Vec<EventOrString>) {}
fn PassVariadicUnion3(&self, _: Vec<BlobOrString>) {}
fn PassVariadicUnion4(&self, _: Vec<BlobOrBoolean>) {}
fn PassVariadicUnion5(&self, _: Vec<StringOrUnsignedLong>) {}
fn PassVariadicUnion6(&self, _: Vec<UnsignedLongOrBoolean>) {}
fn PassVariadicAny(&self, _: *mut JSContext, _: Vec<HandleValue>) {}
fn PassVariadicObject(&self, _: *mut JSContext, _: Vec<*mut JSObject>) {}
}

View file

@ -92,6 +92,11 @@ interface TestBinding {
attribute (HTMLElement or long) unionAttribute;
attribute (Event or DOMString) union2Attribute;
attribute (Event or USVString) union3Attribute;
attribute (DOMString or unsigned long) union4Attribute;
attribute (DOMString or boolean) union5Attribute;
attribute (unsigned long or boolean) union6Attribute;
attribute (Blob or boolean) union7Attribute;
attribute (Blob or unsigned long) union8Attribute;
readonly attribute Uint8ClampedArray arrayAttribute;
attribute any anyAttribute;
attribute object objectAttribute;
@ -118,6 +123,9 @@ interface TestBinding {
attribute object? objectAttributeNullable;
attribute (HTMLElement or long)? unionAttributeNullable;
attribute (Event or DOMString)? union2AttributeNullable;
attribute (Blob or boolean)? union3AttributeNullable;
attribute (unsigned long or boolean)? union4AttributeNullable;
attribute (DOMString or boolean)? union5AttributeNullable;
[BinaryName="BinaryRenamedAttribute"] attribute DOMString attrToBinaryRename;
[BinaryName="BinaryRenamedAttribute2"] attribute DOMString attr-to-binary-rename;
attribute DOMString attr-to-automatically-rename;
@ -152,6 +160,10 @@ interface TestBinding {
(DOMString or sequence<long>) receiveUnion3();
(DOMString or sequence<DOMString>) receiveUnion4();
(Blob or sequence<Blob>) receiveUnion5();
(DOMString or unsigned long) receiveUnion6();
(DOMString or boolean) receiveUnion7();
(unsigned long or boolean) receiveUnion8();
(HTMLElement or unsigned long or DOMString or boolean) receiveUnion9();
sequence<long> receiveSequence();
sequence<Blob> receiveInterfaceSequence();
@ -177,6 +189,8 @@ interface TestBinding {
(HTMLElement or long)? receiveNullableUnion();
(Event or DOMString)? receiveNullableUnion2();
(DOMString or sequence<long>)? receiveNullableUnion3();
(sequence<long> or boolean)? receiveNullableUnion4();
(unsigned long or boolean)? receiveNullableUnion5();
sequence<long>? receiveNullableSequence();
void passBoolean(boolean arg);
@ -201,6 +215,9 @@ interface TestBinding {
void passUnion2((Event or DOMString) data);
void passUnion3((Blob or DOMString) data);
void passUnion4((DOMString or sequence<DOMString>) seq);
void passUnion5((DOMString or boolean) data);
void passUnion6((unsigned long or boolean) bool);
void passUnion7((sequence<DOMString> or unsigned long) arg);
void passAny(any arg);
void passObject(object arg);
void passCallbackFunction(Function fun);
@ -230,6 +247,9 @@ interface TestBinding {
void passNullableObject(object? arg);
void passNullableUnion((HTMLElement or long)? arg);
void passNullableUnion2((Event or DOMString)? data);
void passNullableUnion3((DOMString or sequence<long>)? data);
void passNullableUnion4((sequence<long> or boolean)? bool);
void passNullableUnion5((unsigned long or boolean)? arg);
void passNullableCallbackFunction(Function? fun);
void passNullableCallbackInterface(EventListener? listener);
void passNullableSequence(sequence<long>? seq);
@ -254,6 +274,9 @@ interface TestBinding {
void passOptionalInterface(optional Blob arg);
void passOptionalUnion(optional (HTMLElement or long) arg);
void passOptionalUnion2(optional (Event or DOMString) data);
void passOptionalUnion3(optional (DOMString or sequence<long>) arg);
void passOptionalUnion4(optional (sequence<long> or boolean) data);
void passOptionalUnion5(optional (unsigned long or boolean) bool);
void passOptionalAny(optional any arg);
void passOptionalObject(optional object arg);
void passOptionalCallbackFunction(optional Function fun);
@ -281,6 +304,9 @@ interface TestBinding {
void passOptionalNullableObject(optional object? arg);
void passOptionalNullableUnion(optional (HTMLElement or long)? arg);
void passOptionalNullableUnion2(optional (Event or DOMString)? data);
void passOptionalNullableUnion3(optional (DOMString or sequence<long>)? arg);
void passOptionalNullableUnion4(optional (sequence<long> or boolean)? data);
void passOptionalNullableUnion5(optional (unsigned long or boolean)? bool);
void passOptionalNullableCallbackFunction(optional Function? fun);
void passOptionalNullableCallbackInterface(optional EventListener? listener);
void passOptionalNullableSequence(optional sequence<long>? seq);
@ -362,6 +388,9 @@ interface TestBinding {
void passVariadicUnion((HTMLElement or long)... args);
void passVariadicUnion2((Event or DOMString)... args);
void passVariadicUnion3((Blob or DOMString)... args);
void passVariadicUnion4((Blob or boolean)... args);
void passVariadicUnion5((DOMString or unsigned long)... args);
void passVariadicUnion6((unsigned long or boolean)... args);
void passVariadicAny(any... args);
void passVariadicObject(object... args);