mirror of
https://github.com/servo/servo.git
synced 2025-06-15 03:44:30 +00:00
Use ToJSValConvertible to convert enums to JSVal.
This commit is contained in:
parent
9a909229e3
commit
ccaa46e4a3
3 changed files with 17 additions and 9 deletions
|
@ -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()]))
|
||||
|
||||
|
|
|
@ -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> { Blob::new(&self.window) }
|
||||
pub fn SetInterfaceAttribute(&self, _: &JS<Blob>) {}
|
||||
pub fn AnyAttribute(&self, _: *JSContext) -> JSVal { NullValue() }
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue