From 7843b7b31750073a3850f4397e87b281d1b2ae7b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 7 Jun 2014 23:44:21 +0530 Subject: [PATCH] Allow union types to be used as return values --- .../script/dom/bindings/codegen/CodegenRust.py | 5 +++++ .../dom/bindings/codegen/parser/WebIDL.py | 2 +- src/components/script/dom/testbinding.rs | 17 ++++++++++++++--- .../script/dom/webidls/TestBinding.webidl | 12 ++++++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 14d558958dc..7ddf982166b 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1002,6 +1002,11 @@ def getRetvalDeclarationForType(returnType, descriptorProvider): if returnType.nullable(): result = CGWrapper(result, pre="Option<", post=">") return result + if returnType.isUnion(): + result = CGGeneric('%s::%s' % (returnType.unroll().name, returnType.unroll().name)) + if returnType.nullable(): + result = CGWrapper(result, pre="Option<", post=">") + return result if returnType.isAny(): return CGGeneric("JSVal") if returnType.isObject() or returnType.isSpiderMonkeyInterface(): diff --git a/src/components/script/dom/bindings/codegen/parser/WebIDL.py b/src/components/script/dom/bindings/codegen/parser/WebIDL.py index 5740a21ca52..8b99b08c69c 100644 --- a/src/components/script/dom/bindings/codegen/parser/WebIDL.py +++ b/src/components/script/dom/bindings/codegen/parser/WebIDL.py @@ -2133,7 +2133,7 @@ class IDLAttribute(IDLInterfaceMember): raise WebIDLError("An attribute cannot be of a sequence type", [self.location]) if self.type.isUnion(): - for f in self.type.flatMemberTypes: + for f in self.type.unroll().flatMemberTypes: if f.isDictionary(): raise WebIDLError("An attribute cannot be of a union " "type if one of its member types (or " diff --git a/src/components/script/dom/testbinding.rs b/src/components/script/dom/testbinding.rs index 852b7443418..6349bcec43c 100644 --- a/src/components/script/dom/testbinding.rs +++ b/src/components/script/dom/testbinding.rs @@ -6,8 +6,8 @@ use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::codegen::Bindings::TestBindingBinding::TestEnum; use dom::bindings::codegen::Bindings::TestBindingBinding::TestEnumValues::_empty; use dom::bindings::codegen::UnionTypes::BlobOrString::BlobOrString; -use dom::bindings::codegen::UnionTypes::EventOrString::EventOrString; -use dom::bindings::codegen::UnionTypes::HTMLElementOrLong::HTMLElementOrLong; +use dom::bindings::codegen::UnionTypes::EventOrString::{EventOrString, eString}; +use dom::bindings::codegen::UnionTypes::HTMLElementOrLong::{HTMLElementOrLong, eLong}; use dom::bindings::str::ByteString; use dom::bindings::utils::{Reflector, Reflectable}; use dom::blob::Blob; @@ -56,6 +56,10 @@ pub trait TestBindingMethods { fn SetEnumAttribute(&self, _: TestEnum) {} fn InterfaceAttribute(&self) -> Temporary; fn SetInterfaceAttribute(&self, _: &JSRef) {} + fn UnionAttribute(&self) -> HTMLElementOrLong { eLong(0) } + fn SetUnionAttribute(&self, _: HTMLElementOrLong) {} + fn Union2Attribute(&self) -> EventOrString { eString("".to_string()) } + fn SetUnion2Attribute(&self, _: EventOrString) {} fn AnyAttribute(&self, _: *mut JSContext) -> JSVal { NullValue() } fn SetAnyAttribute(&self, _: *mut JSContext, _: JSVal) {} @@ -88,7 +92,10 @@ pub trait TestBindingMethods { fn GetEnumAttributeNullable(&self) -> Option { Some(_empty) } fn GetInterfaceAttributeNullable(&self) -> Option>; fn SetInterfaceAttributeNullable(&self, _: Option>) {} - + fn GetUnionAttributeNullable(&self) -> Option { Some(eLong(0)) } + fn SetUnionAttributeNullable(&self, _: Option) {} + fn GetUnion2AttributeNullable(&self) -> Option { Some(eString("".to_string())) } + fn SetUnion2AttributeNullable(&self, _: Option) {} fn ReceiveVoid(&self) -> () {} fn ReceiveBoolean(&self) -> bool { false } fn ReceiveByte(&self) -> i8 { 0 } @@ -106,6 +113,8 @@ pub trait TestBindingMethods { fn ReceiveEnum(&self) -> TestEnum { _empty } fn ReceiveInterface(&self) -> Temporary; fn ReceiveAny(&self, _: *mut JSContext) -> JSVal { NullValue() } + fn ReceiveUnion(&self) -> HTMLElementOrLong { eLong(0) } + fn ReceiveUnion2(&self) -> EventOrString { eString("".to_string()) } fn ReceiveNullableBoolean(&self) -> Option { Some(false) } fn ReceiveNullableByte(&self) -> Option { Some(0) } @@ -123,6 +132,8 @@ pub trait TestBindingMethods { fn ReceiveNullableEnum(&self) -> Option { Some(_empty) } fn ReceiveNullableInterface(&self) -> Option>; fn ReceiveNullableAny(&self, _: *mut JSContext) -> Option { Some(NullValue()) } + fn ReceiveNullableUnion(&self) -> Option { Some(eLong(0)) } + fn ReceiveNullableUnion2(&self) -> Option { Some(eString("".to_string())) } fn PassBoolean(&self, _: bool) {} fn PassByte(&self, _: i8) {} diff --git a/src/components/script/dom/webidls/TestBinding.webidl b/src/components/script/dom/webidls/TestBinding.webidl index 0a93b01fa15..6d2137cdd60 100644 --- a/src/components/script/dom/webidls/TestBinding.webidl +++ b/src/components/script/dom/webidls/TestBinding.webidl @@ -69,8 +69,8 @@ interface TestBinding { attribute ByteString byteStringAttribute; attribute TestEnum enumAttribute; attribute Blob interfaceAttribute; - // attribute (HTMLElement or long) unionAttribute; - // attribute (Event or DOMString) union2Attribute; + attribute (HTMLElement or long) unionAttribute; + attribute (Event or DOMString) union2Attribute; attribute any anyAttribute; attribute boolean? booleanAttributeNullable; @@ -88,8 +88,8 @@ interface TestBinding { attribute ByteString? byteStringAttributeNullable; readonly attribute TestEnum? enumAttributeNullable; attribute Blob? interfaceAttributeNullable; - // attribute (HTMLElement or long)? unionAttributeNullable; - // attribute (Event or DOMString)? union2AttributeNullable; + attribute (HTMLElement or long)? unionAttributeNullable; + attribute (Event or DOMString)? union2AttributeNullable; void receiveVoid(); boolean receiveBoolean(); @@ -106,6 +106,8 @@ interface TestBinding { TestEnum receiveEnum(); Blob receiveInterface(); any receiveAny(); + (HTMLElement or long) receiveUnion(); + (Event or DOMString) receiveUnion2(); byte? receiveNullableByte(); boolean? receiveNullableBoolean(); @@ -120,6 +122,8 @@ interface TestBinding { ByteString? receiveNullableByteString(); TestEnum? receiveNullableEnum(); Blob? receiveNullableInterface(); + (HTMLElement or long)? receiveNullableUnion(); + (Event or DOMString)? receiveNullableUnion2(); void passBoolean(boolean arg); void passByte(byte arg);