Use ToJSValConvertible to convert enums to JSVal.

This commit is contained in:
Ms2ger 2014-04-06 19:40:24 +02:00
parent 9a909229e3
commit ccaa46e4a3
3 changed files with 17 additions and 9 deletions

View file

@ -1092,15 +1092,7 @@ def getWrapTemplateForType(type, descriptorProvider, result, successCode,
if type.nullable(): if type.nullable():
raise TypeError("We don't support nullable enumerated return types " raise TypeError("We don't support nullable enumerated return types "
"yet") "yet")
return ("""assert!((%(result)s as uint) < %(strings)s.len()); return (setValue("(%s).to_jsval(cx)" % result), True)
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)
if type.isCallback(): if type.isCallback():
assert not type.isInterface() assert not type.isInterface()
@ -2909,6 +2901,10 @@ class CGEnum(CGThing):
def __init__(self, enum): def __init__(self, enum):
CGThing.__init__(self) CGThing.__init__(self)
inner = """ inner = """
use dom::bindings::conversions::ToJSValConvertible;
use js::jsapi::JSContext;
use js::jsval::JSVal;
#[repr(uint)] #[repr(uint)]
pub enum valuelist { pub enum valuelist {
%s %s
@ -2917,6 +2913,12 @@ pub enum valuelist {
pub static strings: &'static [&'static str] = &[ pub static strings: &'static [&'static str] = &[
%s, %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(map(getEnumValueName, enum.values())),
",\n ".join(['&"%s"' % val for val in enum.values()])) ",\n ".join(['&"%s"' % val for val in enum.values()]))

View file

@ -4,6 +4,8 @@
use dom::bindings::js::JS; use dom::bindings::js::JS;
use dom::bindings::utils::{Reflector, Reflectable}; 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::blob::Blob;
use dom::window::Window; use dom::window::Window;
use servo_util::str::DOMString; use servo_util::str::DOMString;
@ -42,6 +44,7 @@ impl TestBinding {
pub fn SetDoubleAttribute(&self, _: f64) {} pub fn SetDoubleAttribute(&self, _: f64) {}
pub fn StringAttribute(&self) -> DOMString { ~"" } pub fn StringAttribute(&self) -> DOMString { ~"" }
pub fn SetStringAttribute(&self, _: DOMString) {} pub fn SetStringAttribute(&self, _: DOMString) {}
pub fn EnumAttribute(&self) -> TestEnum { _empty }
pub fn InterfaceAttribute(&self) -> JS<Blob> { Blob::new(&self.window) } pub fn InterfaceAttribute(&self) -> JS<Blob> { Blob::new(&self.window) }
pub fn SetInterfaceAttribute(&self, _: &JS<Blob>) {} pub fn SetInterfaceAttribute(&self, _: &JS<Blob>) {}
pub fn AnyAttribute(&self, _: *JSContext) -> JSVal { NullValue() } pub fn AnyAttribute(&self, _: *JSContext) -> JSVal { NullValue() }

View file

@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
enum TestEnum { "", "foo", "bar" };
interface TestBinding { interface TestBinding {
attribute boolean booleanAttribute; attribute boolean booleanAttribute;
attribute byte byteAttribute; attribute byte byteAttribute;
@ -15,6 +17,7 @@ interface TestBinding {
attribute float floatAttribute; attribute float floatAttribute;
attribute double doubleAttribute; attribute double doubleAttribute;
attribute DOMString stringAttribute; attribute DOMString stringAttribute;
readonly attribute TestEnum enumAttribute;
attribute Blob interfaceAttribute; attribute Blob interfaceAttribute;
attribute any anyAttribute; attribute any anyAttribute;