diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 8174a37fa81..52e1529f968 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1092,15 +1092,7 @@ def getWrapTemplateForType(type, descriptorProvider, result, successCode, if type.nullable(): raise TypeError("We don't support nullable enumerated return types " "yet") - return ("""assert!((%(result)s as uint) < %(strings)s.len()); -let %(resultStr)s: *JSString = JS_NewStringCopyN(cx, &%(strings)s[%(result)s as u32].value[0] as *i8, %(strings)s[%(result)s as u32].length as libc::size_t); -if %(resultStr)s.is_null() { - return 0; -} -""" % { "result" : result, - "resultStr" : result + "_str", - "strings" : type.inner.identifier.name + "Values::strings" } + - setValue("StringValue(&*(%s_str))" % result), False) + return (setValue("(%s).to_jsval(cx)" % result), True) if type.isCallback(): assert not type.isInterface() @@ -2909,6 +2901,10 @@ class CGEnum(CGThing): def __init__(self, enum): CGThing.__init__(self) inner = """ +use dom::bindings::conversions::ToJSValConvertible; +use js::jsapi::JSContext; +use js::jsval::JSVal; + #[repr(uint)] pub enum valuelist { %s @@ -2917,6 +2913,12 @@ pub enum valuelist { pub static strings: &'static [&'static str] = &[ %s, ]; + +impl ToJSValConvertible for valuelist { + fn to_jsval(&self, cx: *JSContext) -> JSVal { + strings[*self as uint].to_owned().to_jsval(cx) + } +} """ % (",\n ".join(map(getEnumValueName, enum.values())), ",\n ".join(['&"%s"' % val for val in enum.values()])) diff --git a/src/components/script/dom/testbinding.rs b/src/components/script/dom/testbinding.rs index 8d7b4ce5c51..7f05fd5f183 100644 --- a/src/components/script/dom/testbinding.rs +++ b/src/components/script/dom/testbinding.rs @@ -4,6 +4,8 @@ use dom::bindings::js::JS; use dom::bindings::utils::{Reflector, Reflectable}; +use dom::bindings::codegen::TestBindingBinding::TestEnum; +use dom::bindings::codegen::TestBindingBinding::TestEnumValues::_empty; use dom::blob::Blob; use dom::window::Window; use servo_util::str::DOMString; @@ -42,6 +44,7 @@ impl TestBinding { pub fn SetDoubleAttribute(&self, _: f64) {} pub fn StringAttribute(&self) -> DOMString { ~"" } pub fn SetStringAttribute(&self, _: DOMString) {} + pub fn EnumAttribute(&self) -> TestEnum { _empty } pub fn InterfaceAttribute(&self) -> JS { Blob::new(&self.window) } pub fn SetInterfaceAttribute(&self, _: &JS) {} pub fn AnyAttribute(&self, _: *JSContext) -> JSVal { NullValue() } diff --git a/src/components/script/dom/webidls/TestBinding.webidl b/src/components/script/dom/webidls/TestBinding.webidl index 4aefa14728f..04545269dca 100644 --- a/src/components/script/dom/webidls/TestBinding.webidl +++ b/src/components/script/dom/webidls/TestBinding.webidl @@ -2,6 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +enum TestEnum { "", "foo", "bar" }; + interface TestBinding { attribute boolean booleanAttribute; attribute byte byteAttribute; @@ -15,6 +17,7 @@ interface TestBinding { attribute float floatAttribute; attribute double doubleAttribute; attribute DOMString stringAttribute; + readonly attribute TestEnum enumAttribute; attribute Blob interfaceAttribute; attribute any anyAttribute;