mirror of
https://github.com/servo/servo.git
synced 2025-08-02 12:10:29 +01:00
Move WebIDL constants machinery to bindings::constant
This commit is contained in:
parent
109a297309
commit
51e46b11a3
4 changed files with 77 additions and 67 deletions
|
@ -1136,20 +1136,20 @@ def instantiateJSToNativeConversionTemplate(templateBody, replacements,
|
|||
|
||||
def convertConstIDLValueToJSVal(value):
|
||||
if isinstance(value, IDLNullValue):
|
||||
return "NullVal"
|
||||
return "ConstantVal::NullVal"
|
||||
tag = value.type.tag()
|
||||
if tag in [IDLType.Tags.int8, IDLType.Tags.uint8, IDLType.Tags.int16,
|
||||
IDLType.Tags.uint16, IDLType.Tags.int32]:
|
||||
return "IntVal(%s)" % (value.value)
|
||||
return "ConstantVal::IntVal(%s)" % (value.value)
|
||||
if tag == IDLType.Tags.uint32:
|
||||
return "UintVal(%s)" % (value.value)
|
||||
return "ConstantVal::UintVal(%s)" % (value.value)
|
||||
if tag in [IDLType.Tags.int64, IDLType.Tags.uint64]:
|
||||
return "DoubleVal(%s)" % (value.value)
|
||||
return "ConstantVal::DoubleVal(%s)" % (value.value)
|
||||
if tag == IDLType.Tags.bool:
|
||||
return "BoolVal(true)" if value.value else "BoolVal(false)"
|
||||
return "ConstantVal::BoolVal(true)" if value.value else "ConstantVal::BoolVal(false)"
|
||||
if tag in [IDLType.Tags.unrestricted_float, IDLType.Tags.float,
|
||||
IDLType.Tags.unrestricted_double, IDLType.Tags.double]:
|
||||
return "DoubleVal(%s)" % (value.value)
|
||||
return "ConstantVal::DoubleVal(%s)" % (value.value)
|
||||
raise TypeError("Const value of unhandled type: " + value.type)
|
||||
|
||||
|
||||
|
@ -5429,12 +5429,11 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'dom',
|
||||
'dom::bindings',
|
||||
'dom::bindings::codegen::InterfaceObjectMap',
|
||||
'dom::bindings::constant::ConstantSpec',
|
||||
'dom::bindings::constant::ConstantVal',
|
||||
'dom::bindings::global::GlobalRef',
|
||||
'dom::bindings::global::global_root_from_object',
|
||||
'dom::bindings::global::global_root_from_reflector',
|
||||
'dom::bindings::interface::ConstantSpec',
|
||||
'dom::bindings::interface::ConstantVal::IntVal',
|
||||
'dom::bindings::interface::ConstantVal::UintVal',
|
||||
'dom::bindings::interface::InterfaceConstructorBehavior',
|
||||
'dom::bindings::interface::NonCallbackInterfaceObjectClass',
|
||||
'dom::bindings::interface::NonNullJSNative',
|
||||
|
|
65
components/script/dom/bindings/constant.rs
Normal file
65
components/script/dom/bindings/constant.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* 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/. */
|
||||
|
||||
//! WebIDL constants.
|
||||
|
||||
use js::jsapi::{HandleObject, JSContext, JSPROP_ENUMERATE, JSPROP_PERMANENT};
|
||||
use js::jsapi::{JSPROP_READONLY, JS_DefineProperty};
|
||||
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UInt32Value};
|
||||
use libc;
|
||||
|
||||
/// Representation of an IDL constant.
|
||||
#[derive(Clone)]
|
||||
pub struct ConstantSpec {
|
||||
/// name of the constant.
|
||||
pub name: &'static [u8],
|
||||
/// value of the constant.
|
||||
pub value: ConstantVal,
|
||||
}
|
||||
|
||||
/// Representation of an IDL constant value.
|
||||
#[derive(Clone)]
|
||||
pub enum ConstantVal {
|
||||
/// `long` constant.
|
||||
IntVal(i32),
|
||||
/// `unsigned long` constant.
|
||||
UintVal(u32),
|
||||
/// `double` constant.
|
||||
DoubleVal(f64),
|
||||
/// `boolean` constant.
|
||||
BoolVal(bool),
|
||||
/// `null` constant.
|
||||
NullVal,
|
||||
}
|
||||
|
||||
impl ConstantSpec {
|
||||
/// Returns a `JSVal` that represents the value of this `ConstantSpec`.
|
||||
pub fn get_value(&self) -> JSVal {
|
||||
match self.value {
|
||||
ConstantVal::NullVal => NullValue(),
|
||||
ConstantVal::IntVal(i) => Int32Value(i),
|
||||
ConstantVal::UintVal(u) => UInt32Value(u),
|
||||
ConstantVal::DoubleVal(d) => DoubleValue(d),
|
||||
ConstantVal::BoolVal(b) => BooleanValue(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines constants on `obj`.
|
||||
/// Fails on JSAPI failure.
|
||||
pub unsafe fn define_constants(
|
||||
cx: *mut JSContext,
|
||||
obj: HandleObject,
|
||||
constants: &[ConstantSpec]) {
|
||||
for spec in constants {
|
||||
rooted!(in(cx) let value = spec.get_value());
|
||||
assert!(JS_DefineProperty(cx,
|
||||
obj,
|
||||
spec.name.as_ptr() as *const libc::c_char,
|
||||
value.handle(),
|
||||
JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT,
|
||||
None,
|
||||
None));
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
use dom::bindings::codegen::InterfaceObjectMap::Globals;
|
||||
use dom::bindings::codegen::PrototypeList;
|
||||
use dom::bindings::constant::{ConstantSpec, define_constants};
|
||||
use dom::bindings::conversions::{DOM_OBJECT_SLOT, get_dom_class};
|
||||
use dom::bindings::guard::Guard;
|
||||
use dom::bindings::utils::{DOM_PROTOTYPE_SLOT, ProtoOrIfaceArray, get_proto_or_iface_array};
|
||||
|
@ -14,7 +15,7 @@ use js::glue::{RUST_SYMBOL_TO_JSID, UncheckedUnwrapObject};
|
|||
use js::jsapi::{Class, ClassOps, CompartmentOptions, GetGlobalForObjectCrossCompartment};
|
||||
use js::jsapi::{GetWellKnownSymbol, HandleObject, HandleValue, JSAutoCompartment};
|
||||
use js::jsapi::{JSClass, JSContext, JSFUN_CONSTRUCTOR, JSFunctionSpec, JSObject};
|
||||
use js::jsapi::{JSPROP_ENUMERATE, JSPROP_PERMANENT, JSPROP_READONLY, JSPROP_RESOLVING};
|
||||
use js::jsapi::{JSPROP_PERMANENT, JSPROP_READONLY, JSPROP_RESOLVING};
|
||||
use js::jsapi::{JSPropertySpec, JSString, JSTracer, JSVersion, JS_AtomizeAndPinString};
|
||||
use js::jsapi::{JS_DefineProperty, JS_DefineProperty1, JS_DefineProperty2};
|
||||
use js::jsapi::{JS_DefineProperty4, JS_DefinePropertyById3, JS_FireOnNewGlobalObject};
|
||||
|
@ -24,71 +25,15 @@ use js::jsapi::{JS_NewObject, JS_NewObjectWithUniqueType, JS_NewPlainObject};
|
|||
use js::jsapi::{JS_NewStringCopyN, JS_SetReservedSlot, MutableHandleObject};
|
||||
use js::jsapi::{MutableHandleValue, ObjectOps, OnNewGlobalHookOption, SymbolCode};
|
||||
use js::jsapi::{TrueHandleValue, Value};
|
||||
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue};
|
||||
use js::jsval::{PrivateValue, UInt32Value};
|
||||
use js::jsval::{JSVal, PrivateValue};
|
||||
use js::rust::{define_methods, define_properties};
|
||||
use libc;
|
||||
use std::ptr;
|
||||
|
||||
/// Representation of an IDL constant value.
|
||||
#[derive(Clone)]
|
||||
pub enum ConstantVal {
|
||||
/// `long` constant.
|
||||
IntVal(i32),
|
||||
/// `unsigned long` constant.
|
||||
UintVal(u32),
|
||||
/// `double` constant.
|
||||
DoubleVal(f64),
|
||||
/// `boolean` constant.
|
||||
BoolVal(bool),
|
||||
/// `null` constant.
|
||||
NullVal,
|
||||
}
|
||||
|
||||
/// Representation of an IDL constant.
|
||||
#[derive(Clone)]
|
||||
pub struct ConstantSpec {
|
||||
/// name of the constant.
|
||||
pub name: &'static [u8],
|
||||
/// value of the constant.
|
||||
pub value: ConstantVal,
|
||||
}
|
||||
|
||||
impl ConstantSpec {
|
||||
/// Returns a `JSVal` that represents the value of this `ConstantSpec`.
|
||||
pub fn get_value(&self) -> JSVal {
|
||||
match self.value {
|
||||
ConstantVal::NullVal => NullValue(),
|
||||
ConstantVal::IntVal(i) => Int32Value(i),
|
||||
ConstantVal::UintVal(u) => UInt32Value(u),
|
||||
ConstantVal::DoubleVal(d) => DoubleValue(d),
|
||||
ConstantVal::BoolVal(b) => BooleanValue(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A JSNative that cannot be null.
|
||||
pub type NonNullJSNative =
|
||||
unsafe extern "C" fn (arg1: *mut JSContext, arg2: libc::c_uint, arg3: *mut JSVal) -> bool;
|
||||
|
||||
/// Defines constants on `obj`.
|
||||
/// Fails on JSAPI failure.
|
||||
unsafe fn define_constants(
|
||||
cx: *mut JSContext,
|
||||
obj: HandleObject,
|
||||
constants: &[ConstantSpec]) {
|
||||
for spec in constants {
|
||||
rooted!(in(cx) let value = spec.get_value());
|
||||
assert!(JS_DefineProperty(cx,
|
||||
obj,
|
||||
spec.name.as_ptr() as *const libc::c_char,
|
||||
value.handle(),
|
||||
JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT,
|
||||
None,
|
||||
None));
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn fun_to_string_hook(cx: *mut JSContext,
|
||||
obj: HandleObject,
|
||||
_indent: u32)
|
||||
|
|
|
@ -131,6 +131,7 @@
|
|||
pub use style::domrefcell as cell;
|
||||
|
||||
pub mod callback;
|
||||
pub mod constant;
|
||||
pub mod conversions;
|
||||
pub mod error;
|
||||
pub mod global;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue