Format script component

This commit is contained in:
chansuke 2018-09-18 23:24:15 +09:00 committed by Josh Matthews
parent 2ca7a13473
commit c37a345dc9
357 changed files with 25485 additions and 18076 deletions

View file

@ -52,11 +52,12 @@ unsafe impl Sync for NonCallbackInterfaceObjectClass {}
impl NonCallbackInterfaceObjectClass {
/// Create a new `NonCallbackInterfaceObjectClass` structure.
pub const fn new(constructor_behavior: &'static InterfaceConstructorBehavior,
string_rep: &'static [u8],
proto_id: PrototypeList::ID,
proto_depth: u16)
-> NonCallbackInterfaceObjectClass {
pub const fn new(
constructor_behavior: &'static InterfaceConstructorBehavior,
string_rep: &'static [u8],
proto_id: PrototypeList::ID,
proto_depth: u16,
) -> NonCallbackInterfaceObjectClass {
NonCallbackInterfaceObjectClass {
class: Class {
name: b"Function\0" as *const _ as *const libc::c_char,
@ -74,9 +75,7 @@ impl NonCallbackInterfaceObjectClass {
/// cast own reference to `JSClass` reference
pub fn as_jsclass(&self) -> &JSClass {
unsafe {
&*(self as *const _ as *const JSClass)
}
unsafe { &*(self as *const _ as *const JSClass) }
}
}
@ -124,27 +123,29 @@ impl InterfaceConstructorBehavior {
}
/// A trace hook.
pub type TraceHook =
unsafe extern "C" fn(trc: *mut JSTracer, obj: *mut JSObject);
pub type TraceHook = unsafe extern "C" fn(trc: *mut JSTracer, obj: *mut JSObject);
/// Create a global object with the given class.
pub unsafe fn create_global_object(
cx: *mut JSContext,
class: &'static JSClass,
private: *const libc::c_void,
trace: TraceHook,
mut rval: MutableHandleObject) {
cx: *mut JSContext,
class: &'static JSClass,
private: *const libc::c_void,
trace: TraceHook,
mut rval: MutableHandleObject,
) {
assert!(rval.is_null());
let mut options = CompartmentOptions::default();
options.creationOptions_.traceGlobal_ = Some(trace);
options.creationOptions_.sharedMemoryAndAtomics_ = true;
rval.set(JS_NewGlobalObject(cx,
class,
ptr::null_mut(),
OnNewGlobalHookOption::DontFireOnNewGlobalHook,
&options));
rval.set(JS_NewGlobalObject(
cx,
class,
ptr::null_mut(),
OnNewGlobalHookOption::DontFireOnNewGlobalHook,
&options,
));
assert!(!rval.is_null());
// Initialize the reserved slots before doing anything that can GC, to
@ -162,11 +163,12 @@ pub unsafe fn create_global_object(
/// Create and define the interface object of a callback interface.
pub unsafe fn create_callback_interface_object(
cx: *mut JSContext,
global: HandleObject,
constants: &[Guard<&[ConstantSpec]>],
name: &[u8],
mut rval: MutableHandleObject) {
cx: *mut JSContext,
global: HandleObject,
constants: &[Guard<&[ConstantSpec]>],
name: &[u8],
mut rval: MutableHandleObject,
) {
assert!(!constants.is_empty());
rval.set(JS_NewObject(cx, ptr::null()));
assert!(!rval.is_null());
@ -177,15 +179,24 @@ pub unsafe fn create_callback_interface_object(
/// Create the interface prototype object of a non-callback interface.
pub unsafe fn create_interface_prototype_object(
cx: *mut JSContext,
proto: HandleObject,
class: &'static JSClass,
regular_methods: &[Guard<&'static [JSFunctionSpec]>],
regular_properties: &[Guard<&'static [JSPropertySpec]>],
constants: &[Guard<&[ConstantSpec]>],
unscopable_names: &[&[u8]],
rval: MutableHandleObject) {
create_object(cx, proto, class, regular_methods, regular_properties, constants, rval);
cx: *mut JSContext,
proto: HandleObject,
class: &'static JSClass,
regular_methods: &[Guard<&'static [JSFunctionSpec]>],
regular_properties: &[Guard<&'static [JSPropertySpec]>],
constants: &[Guard<&[ConstantSpec]>],
unscopable_names: &[&[u8]],
rval: MutableHandleObject,
) {
create_object(
cx,
proto,
class,
regular_methods,
regular_properties,
constants,
rval,
);
if !unscopable_names.is_empty() {
rooted!(in(cx) let mut unscopable_obj = ptr::null_mut::<JSObject>());
@ -196,32 +207,43 @@ pub unsafe fn create_interface_prototype_object(
rooted!(in(cx) let unscopable_id = RUST_SYMBOL_TO_JSID(unscopable_symbol));
assert!(JS_DefinePropertyById4(
cx, rval.handle(), unscopable_id.handle(), unscopable_obj.handle(),
JSPROP_READONLY as u32))
cx,
rval.handle(),
unscopable_id.handle(),
unscopable_obj.handle(),
JSPROP_READONLY as u32
))
}
}
/// Create and define the interface object of a non-callback interface.
pub unsafe fn create_noncallback_interface_object(
cx: *mut JSContext,
global: HandleObject,
proto: HandleObject,
class: &'static NonCallbackInterfaceObjectClass,
static_methods: &[Guard<&'static [JSFunctionSpec]>],
static_properties: &[Guard<&'static [JSPropertySpec]>],
constants: &[Guard<&[ConstantSpec]>],
interface_prototype_object: HandleObject,
name: &[u8],
length: u32,
rval: MutableHandleObject) {
create_object(cx,
proto,
class.as_jsclass(),
static_methods,
static_properties,
constants,
rval);
assert!(JS_LinkConstructorAndPrototype(cx, rval.handle(), interface_prototype_object));
cx: *mut JSContext,
global: HandleObject,
proto: HandleObject,
class: &'static NonCallbackInterfaceObjectClass,
static_methods: &[Guard<&'static [JSFunctionSpec]>],
static_properties: &[Guard<&'static [JSPropertySpec]>],
constants: &[Guard<&[ConstantSpec]>],
interface_prototype_object: HandleObject,
name: &[u8],
length: u32,
rval: MutableHandleObject,
) {
create_object(
cx,
proto,
class.as_jsclass(),
static_methods,
static_properties,
constants,
rval,
);
assert!(JS_LinkConstructorAndPrototype(
cx,
rval.handle(),
interface_prototype_object
));
define_name(cx, rval.handle(), name);
define_length(cx, rval.handle(), i32::try_from(length).expect("overflow"));
define_on_global_object(cx, global, name, rval.handle());
@ -229,29 +251,34 @@ pub unsafe fn create_noncallback_interface_object(
/// Create and define the named constructors of a non-callback interface.
pub unsafe fn create_named_constructors(
cx: *mut JSContext,
global: HandleObject,
named_constructors: &[(ConstructorClassHook, &[u8], u32)],
interface_prototype_object: HandleObject) {
cx: *mut JSContext,
global: HandleObject,
named_constructors: &[(ConstructorClassHook, &[u8], u32)],
interface_prototype_object: HandleObject,
) {
rooted!(in(cx) let mut constructor = ptr::null_mut::<JSObject>());
for &(native, name, arity) in named_constructors {
assert_eq!(*name.last().unwrap(), b'\0');
let fun = JS_NewFunction(cx,
Some(native),
arity,
JSFUN_CONSTRUCTOR,
name.as_ptr() as *const libc::c_char);
let fun = JS_NewFunction(
cx,
Some(native),
arity,
JSFUN_CONSTRUCTOR,
name.as_ptr() as *const libc::c_char,
);
assert!(!fun.is_null());
constructor.set(JS_GetFunctionObject(fun));
assert!(!constructor.is_null());
assert!(JS_DefineProperty2(cx,
constructor.handle(),
b"prototype\0".as_ptr() as *const libc::c_char,
interface_prototype_object,
(JSPROP_PERMANENT | JSPROP_READONLY) as u32));
assert!(JS_DefineProperty2(
cx,
constructor.handle(),
b"prototype\0".as_ptr() as *const libc::c_char,
interface_prototype_object,
(JSPROP_PERMANENT | JSPROP_READONLY) as u32
));
define_on_global_object(cx, global, name, constructor.handle());
}
@ -259,13 +286,14 @@ pub unsafe fn create_named_constructors(
/// Create a new object with a unique type.
pub unsafe fn create_object(
cx: *mut JSContext,
proto: HandleObject,
class: &'static JSClass,
methods: &[Guard<&'static [JSFunctionSpec]>],
properties: &[Guard<&'static [JSPropertySpec]>],
constants: &[Guard<&[ConstantSpec]>],
mut rval: MutableHandleObject) {
cx: *mut JSContext,
proto: HandleObject,
class: &'static JSClass,
methods: &[Guard<&'static [JSFunctionSpec]>],
properties: &[Guard<&'static [JSPropertySpec]>],
constants: &[Guard<&[ConstantSpec]>],
mut rval: MutableHandleObject,
) {
rval.set(JS_NewObjectWithUniqueType(cx, class, proto));
assert!(!rval.is_null());
define_guarded_methods(cx, rval.handle(), methods);
@ -275,9 +303,10 @@ pub unsafe fn create_object(
/// Conditionally define constants on an object.
pub unsafe fn define_guarded_constants(
cx: *mut JSContext,
obj: HandleObject,
constants: &[Guard<&[ConstantSpec]>]) {
cx: *mut JSContext,
obj: HandleObject,
constants: &[Guard<&[ConstantSpec]>],
) {
for guard in constants {
if let Some(specs) = guard.expose(cx, obj) {
define_constants(cx, obj, specs);
@ -287,9 +316,10 @@ pub unsafe fn define_guarded_constants(
/// Conditionally define methods on an object.
pub unsafe fn define_guarded_methods(
cx: *mut JSContext,
obj: HandleObject,
methods: &[Guard<&'static [JSFunctionSpec]>]) {
cx: *mut JSContext,
obj: HandleObject,
methods: &[Guard<&'static [JSFunctionSpec]>],
) {
for guard in methods {
if let Some(specs) = guard.expose(cx, obj) {
define_methods(cx, obj, specs).unwrap();
@ -299,9 +329,10 @@ pub unsafe fn define_guarded_methods(
/// Conditionally define properties on an object.
pub unsafe fn define_guarded_properties(
cx: *mut JSContext,
obj: HandleObject,
properties: &[Guard<&'static [JSPropertySpec]>]) {
cx: *mut JSContext,
obj: HandleObject,
properties: &[Guard<&'static [JSPropertySpec]>],
) {
for guard in properties {
if let Some(specs) = guard.expose(cx, obj) {
define_properties(cx, obj, specs).unwrap();
@ -320,16 +351,19 @@ pub unsafe fn is_exposed_in(object: HandleObject, globals: Globals) -> bool {
/// Define a property with a given name on the global object. Should be called
/// through the resolve hook.
pub unsafe fn define_on_global_object(
cx: *mut JSContext,
global: HandleObject,
name: &[u8],
obj: HandleObject) {
cx: *mut JSContext,
global: HandleObject,
name: &[u8],
obj: HandleObject,
) {
assert_eq!(*name.last().unwrap(), b'\0');
assert!(JS_DefineProperty2(cx,
global,
name.as_ptr() as *const libc::c_char,
obj,
JSPROP_RESOLVING));
assert!(JS_DefineProperty2(
cx,
global,
name.as_ptr() as *const libc::c_char,
obj,
JSPROP_RESOLVING
));
}
const OBJECT_OPS: ObjectOps = ObjectOps {
@ -344,10 +378,11 @@ const OBJECT_OPS: ObjectOps = ObjectOps {
funToString: Some(fun_to_string_hook),
};
unsafe extern "C" fn fun_to_string_hook(cx: *mut JSContext,
obj: RawHandleObject,
_is_to_source: bool)
-> *mut JSString {
unsafe extern "C" fn fun_to_string_hook(
cx: *mut JSContext,
obj: RawHandleObject,
_is_to_source: bool,
) -> *mut JSString {
let js_class = get_object_class(obj.get());
assert!(!js_class.is_null());
let repr = (*(js_class as *const NonCallbackInterfaceObjectClass)).representation;
@ -358,17 +393,19 @@ unsafe extern "C" fn fun_to_string_hook(cx: *mut JSContext,
}
/// Hook for instanceof on interface objects.
unsafe extern "C" fn has_instance_hook(cx: *mut JSContext,
obj: RawHandleObject,
value: RawMutableHandleValue,
rval: *mut bool) -> bool {
unsafe extern "C" fn has_instance_hook(
cx: *mut JSContext,
obj: RawHandleObject,
value: RawMutableHandleValue,
rval: *mut bool,
) -> bool {
let obj_raw = HandleObject::from_raw(obj);
let val_raw = HandleValue::from_raw(value.handle());
match has_instance(cx, obj_raw, val_raw) {
Ok(result) => {
*rval = result;
true
}
},
Err(()) => false,
}
}
@ -376,10 +413,10 @@ unsafe extern "C" fn has_instance_hook(cx: *mut JSContext,
/// Return whether a value is an instance of a given prototype.
/// <http://heycam.github.io/webidl/#es-interface-hasinstance>
unsafe fn has_instance(
cx: *mut JSContext,
interface_object: HandleObject,
value: HandleValue)
-> Result<bool, ()> {
cx: *mut JSContext,
interface_object: HandleObject,
value: HandleValue,
) -> Result<bool, ()> {
if !value.is_object() {
// Step 1.
return Ok(false);
@ -391,8 +428,10 @@ unsafe fn has_instance(
let js_class = get_object_class(interface_object.get());
let object_class = &*(js_class as *const NonCallbackInterfaceObjectClass);
if let Ok(dom_class) = get_dom_class(UncheckedUnwrapObject(value.get(),
/* stopAtWindowProxy = */ 0)) {
if let Ok(dom_class) = get_dom_class(UncheckedUnwrapObject(
value.get(),
/* stopAtWindowProxy = */ 0,
)) {
if dom_class.interface_chain[object_class.proto_depth as usize] == object_class.proto_id {
// Step 4.
return Ok(true);
@ -422,9 +461,10 @@ unsafe fn has_instance(
}
unsafe fn create_unscopable_object(
cx: *mut JSContext,
names: &[&[u8]],
mut rval: MutableHandleObject) {
cx: *mut JSContext,
names: &[&[u8]],
mut rval: MutableHandleObject,
) {
assert!(!names.is_empty());
assert!(rval.is_null());
rval.set(JS_NewPlainObject(cx));
@ -445,35 +485,39 @@ unsafe fn define_name(cx: *mut JSContext, obj: HandleObject, name: &[u8]) {
assert_eq!(*name.last().unwrap(), b'\0');
rooted!(in(cx) let name = JS_AtomizeAndPinString(cx, name.as_ptr() as *const libc::c_char));
assert!(!name.is_null());
assert!(JS_DefineProperty3(cx,
obj,
b"name\0".as_ptr() as *const libc::c_char,
name.handle().into(),
JSPROP_READONLY as u32));
assert!(JS_DefineProperty3(
cx,
obj,
b"name\0".as_ptr() as *const libc::c_char,
name.handle().into(),
JSPROP_READONLY as u32
));
}
unsafe fn define_length(cx: *mut JSContext, obj: HandleObject, length: i32) {
assert!(JS_DefineProperty4(cx,
obj,
b"length\0".as_ptr() as *const libc::c_char,
length,
JSPROP_READONLY as u32));
assert!(JS_DefineProperty4(
cx,
obj,
b"length\0".as_ptr() as *const libc::c_char,
length,
JSPROP_READONLY as u32
));
}
unsafe extern "C" fn invalid_constructor(
cx: *mut JSContext,
_argc: libc::c_uint,
_vp: *mut JSVal)
-> bool {
cx: *mut JSContext,
_argc: libc::c_uint,
_vp: *mut JSVal,
) -> bool {
throw_type_error(cx, "Illegal constructor.");
false
}
unsafe extern "C" fn non_new_constructor(
cx: *mut JSContext,
_argc: libc::c_uint,
_vp: *mut JSVal)
-> bool {
cx: *mut JSContext,
_argc: libc::c_uint,
_vp: *mut JSVal,
) -> bool {
throw_type_error(cx, "This constructor needs to be called with `new`.");
false
}