Use safe JSContext when possible in interface.rs

This commit is contained in:
marmeladema 2019-07-27 18:52:12 +01:00
parent 8b070fef52
commit 78034a90d0
6 changed files with 194 additions and 176 deletions

View file

@ -2651,8 +2651,8 @@ def InitUnforgeablePropertiesOnHolder(descriptor, properties):
"""
unforgeables = []
defineUnforgeableAttrs = "define_guarded_properties(*cx, unforgeable_holder.handle(), %s, global);"
defineUnforgeableMethods = "define_guarded_methods(*cx, unforgeable_holder.handle(), %s, global);"
defineUnforgeableAttrs = "define_guarded_properties(cx, unforgeable_holder.handle(), %s, global);"
defineUnforgeableMethods = "define_guarded_methods(cx, unforgeable_holder.handle(), %s, global);"
unforgeableMembers = [
(defineUnforgeableAttrs, properties.unforgeable_attrs),
@ -2762,7 +2762,7 @@ class CGWrapGlobalMethod(CGAbstractMethod):
("define_guarded_methods", self.properties.methods),
("define_guarded_constants", self.properties.consts)
]
members = ["%s(*cx, obj.handle(), %s, obj.handle());" % (function, array.variableName())
members = ["%s(cx, obj.handle(), %s, obj.handle());" % (function, array.variableName())
for (function, array) in pairs if array.length() > 0]
values["members"] = "\n".join(members)
@ -2772,7 +2772,7 @@ let _rt = RootedTraceable::new(&*raw);
rooted!(in(*cx) let mut obj = ptr::null_mut::<JSObject>());
create_global_object(
*cx,
cx,
&Class.base,
raw as *const libc::c_void,
_trace,
@ -2911,7 +2911,7 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
rooted!(in(*cx) let proto = %(proto)s);
assert!(!proto.is_null());
rooted!(in(*cx) let mut namespace = ptr::null_mut::<JSObject>());
create_namespace_object(*cx, global, proto.handle(), &NAMESPACE_OBJECT_CLASS,
create_namespace_object(cx, global, proto.handle(), &NAMESPACE_OBJECT_CLASS,
%(methods)s, %(name)s, namespace.handle_mut());
assert!(!namespace.is_null());
assert!((*cache)[PrototypeList::Constructor::%(id)s as usize].is_null());
@ -2924,7 +2924,7 @@ assert!((*cache)[PrototypeList::Constructor::%(id)s as usize].is_null());
assert not self.descriptor.interface.ctor() and self.descriptor.interface.hasConstants()
return CGGeneric("""\
rooted!(in(*cx) let mut interface = ptr::null_mut::<JSObject>());
create_callback_interface_object(*cx, global, sConstants, %(name)s, interface.handle_mut());
create_callback_interface_object(cx, global, sConstants, %(name)s, interface.handle_mut());
assert!(!interface.is_null());
assert!((*cache)[PrototypeList::Constructor::%(id)s as usize].is_null());
(*cache)[PrototypeList::Constructor::%(id)s as usize] = interface.get();
@ -2976,7 +2976,7 @@ assert!(!prototype_proto.is_null());""" % getPrototypeProto)]
code.append(CGGeneric("""
rooted!(in(*cx) let mut prototype = ptr::null_mut::<JSObject>());
create_interface_prototype_object(*cx,
create_interface_prototype_object(cx,
global.into(),
prototype_proto.handle().into(),
&PrototypeClass,
@ -3011,7 +3011,7 @@ assert!((*cache)[PrototypeList::ID::%(id)s as usize].is_null());
assert!(!interface_proto.is_null());
rooted!(in(*cx) let mut interface = ptr::null_mut::<JSObject>());
create_noncallback_interface_object(*cx,
create_noncallback_interface_object(cx,
global.into(),
interface_proto.handle(),
&INTERFACE_OBJECT_CLASS,
@ -3093,7 +3093,7 @@ assert!((*cache)[PrototypeList::Constructor::%(id)s as usize].is_null());
specs.append(CGGeneric("(%s as ConstructorClassHook, %s, %d)" % (hook, name, length)))
values = CGIndenter(CGList(specs, "\n"), 4)
code.append(CGWrapper(values, pre="%s = [\n" % decl, post="\n];"))
code.append(CGGeneric("create_named_constructors(*cx, global, &named_constructors, prototype.handle());"))
code.append(CGGeneric("create_named_constructors(cx, global, &named_constructors, prototype.handle());"))
if self.descriptor.hasUnforgeableMembers:
# We want to use the same JSClass and prototype as the object we'll

View file

@ -4,8 +4,9 @@
//! WebIDL constants.
use crate::script_runtime::JSContext;
use js::jsapi::JSPROP_READONLY;
use js::jsapi::{JSContext, JSPROP_ENUMERATE, JSPROP_PERMANENT};
use js::jsapi::{JSPROP_ENUMERATE, JSPROP_PERMANENT};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UInt32Value};
use js::rust::wrappers::JS_DefineProperty;
use js::rust::HandleObject;
@ -50,15 +51,17 @@ impl ConstantSpec {
/// Defines constants on `obj`.
/// Fails on JSAPI failure.
pub unsafe fn define_constants(cx: *mut JSContext, obj: HandleObject, constants: &[ConstantSpec]) {
pub fn define_constants(cx: JSContext, obj: HandleObject, constants: &[ConstantSpec]) {
for spec in constants {
rooted!(in(cx) let value = spec.get_value());
rooted!(in(*cx) let value = spec.get_value());
unsafe {
assert!(JS_DefineProperty(
cx,
*cx,
obj,
spec.name.as_ptr() as *const libc::c_char,
value.handle(),
(JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT) as u32
));
}
}
}

View file

@ -6,7 +6,7 @@
use crate::dom::bindings::codegen::InterfaceObjectMap;
use crate::dom::bindings::interface::is_exposed_in;
use js::jsapi::JSContext;
use crate::script_runtime::JSContext;
use js::rust::HandleObject;
use servo_config::prefs;
@ -28,12 +28,7 @@ impl<T: Clone + Copy> Guard<T> {
/// Expose the value if the condition is satisfied.
///
/// The passed handle is the object on which the value may be exposed.
pub unsafe fn expose(
&self,
cx: *mut JSContext,
obj: HandleObject,
global: HandleObject,
) -> Option<T> {
pub fn expose(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> Option<T> {
if self.condition.is_satisfied(cx, obj, global) {
Some(self.value)
} else {
@ -45,7 +40,7 @@ impl<T: Clone + Copy> Guard<T> {
/// A condition to expose things.
pub enum Condition {
/// The condition is satisfied if the function returns true.
Func(unsafe fn(*mut JSContext, HandleObject) -> bool),
Func(fn(JSContext, HandleObject) -> bool),
/// The condition is satisfied if the preference is set.
Pref(&'static str),
// The condition is satisfied if the interface is exposed in the global.
@ -55,12 +50,7 @@ pub enum Condition {
}
impl Condition {
unsafe fn is_satisfied(
&self,
cx: *mut JSContext,
obj: HandleObject,
global: HandleObject,
) -> bool {
fn is_satisfied(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> bool {
match *self {
Condition::Pref(name) => prefs::pref_map().get(name).as_bool().unwrap_or(false),
Condition::Func(f) => f(cx, obj),

View file

@ -12,6 +12,7 @@ use crate::dom::bindings::guard::Guard;
use crate::dom::bindings::utils::{
get_proto_or_iface_array, ProtoOrIfaceArray, DOM_PROTOTYPE_SLOT,
};
use crate::script_runtime::JSContext as SafeJSContext;
use js::error::throw_type_error;
use js::glue::UncheckedUnwrapObject;
use js::jsapi::HandleObject as RawHandleObject;
@ -129,7 +130,7 @@ 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,
cx: SafeJSContext,
class: &'static JSClass,
private: *const libc::c_void,
trace: TraceHook,
@ -142,7 +143,7 @@ pub unsafe fn create_global_object(
options.creationOptions_.sharedMemoryAndAtomics_ = true;
rval.set(JS_NewGlobalObject(
cx,
*cx,
class,
ptr::null_mut(),
OnNewGlobalHookOption::DontFireOnNewGlobalHook,
@ -159,20 +160,22 @@ pub unsafe fn create_global_object(
let val = PrivateValue(Box::into_raw(proto_array) as *const libc::c_void);
JS_SetReservedSlot(rval.get(), DOM_PROTOTYPE_SLOT, &val);
let _ac = JSAutoRealm::new(cx, rval.get());
JS_FireOnNewGlobalObject(cx, rval.handle());
let _ac = JSAutoRealm::new(*cx, rval.get());
JS_FireOnNewGlobalObject(*cx, rval.handle());
}
/// Create and define the interface object of a callback interface.
pub unsafe fn create_callback_interface_object(
cx: *mut JSContext,
pub fn create_callback_interface_object(
cx: SafeJSContext,
global: HandleObject,
constants: &[Guard<&[ConstantSpec]>],
name: &[u8],
mut rval: MutableHandleObject,
) {
assert!(!constants.is_empty());
rval.set(JS_NewObject(cx, ptr::null()));
unsafe {
rval.set(JS_NewObject(*cx, ptr::null()));
}
assert!(!rval.is_null());
define_guarded_constants(cx, rval.handle(), constants, global);
define_name(cx, rval.handle(), name);
@ -180,8 +183,8 @@ 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,
pub fn create_interface_prototype_object(
cx: SafeJSContext,
global: HandleObject,
proto: HandleObject,
class: &'static JSClass,
@ -203,27 +206,29 @@ pub unsafe fn create_interface_prototype_object(
);
if !unscopable_names.is_empty() {
rooted!(in(cx) let mut unscopable_obj = ptr::null_mut::<JSObject>());
rooted!(in(*cx) let mut unscopable_obj = ptr::null_mut::<JSObject>());
create_unscopable_object(cx, unscopable_names, unscopable_obj.handle_mut());
let unscopable_symbol = GetWellKnownSymbol(cx, SymbolCode::unscopables);
unsafe {
let unscopable_symbol = GetWellKnownSymbol(*cx, SymbolCode::unscopables);
assert!(!unscopable_symbol.is_null());
rooted!(in(cx) let mut unscopable_id: jsid);
rooted!(in(*cx) let mut unscopable_id: jsid);
RUST_SYMBOL_TO_JSID(unscopable_symbol, unscopable_id.handle_mut());
assert!(JS_DefinePropertyById5(
cx,
*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,
pub fn create_noncallback_interface_object(
cx: SafeJSContext,
global: HandleObject,
proto: HandleObject,
class: &'static NonCallbackInterfaceObjectClass,
@ -245,30 +250,33 @@ pub unsafe fn create_noncallback_interface_object(
constants,
rval,
);
unsafe {
assert!(JS_LinkConstructorAndPrototype(
cx,
*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());
}
/// Create and define the named constructors of a non-callback interface.
pub unsafe fn create_named_constructors(
cx: *mut JSContext,
pub fn create_named_constructors(
cx: SafeJSContext,
global: HandleObject,
named_constructors: &[(ConstructorClassHook, &[u8], u32)],
interface_prototype_object: HandleObject,
) {
rooted!(in(cx) let mut constructor = ptr::null_mut::<JSObject>());
rooted!(in(*cx) let mut constructor = ptr::null_mut::<JSObject>());
for &(native, name, arity) in named_constructors {
assert_eq!(*name.last().unwrap(), b'\0');
unsafe {
let fun = JS_NewFunction(
cx,
*cx,
Some(native),
arity,
JSFUN_CONSTRUCTOR,
@ -279,20 +287,21 @@ pub unsafe fn create_named_constructors(
assert!(!constructor.is_null());
assert!(JS_DefineProperty3(
cx,
*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());
}
}
/// Create a new object with a unique type.
pub unsafe fn create_object(
cx: *mut JSContext,
pub fn create_object(
cx: SafeJSContext,
global: HandleObject,
proto: HandleObject,
class: &'static JSClass,
@ -301,7 +310,9 @@ pub unsafe fn create_object(
constants: &[Guard<&[ConstantSpec]>],
mut rval: MutableHandleObject,
) {
rval.set(JS_NewObjectWithUniqueType(cx, class, proto));
unsafe {
rval.set(JS_NewObjectWithUniqueType(*cx, class, proto));
}
assert!(!rval.is_null());
define_guarded_methods(cx, rval.handle(), methods, global);
define_guarded_properties(cx, rval.handle(), properties, global);
@ -309,8 +320,8 @@ pub unsafe fn create_object(
}
/// Conditionally define constants on an object.
pub unsafe fn define_guarded_constants(
cx: *mut JSContext,
pub fn define_guarded_constants(
cx: SafeJSContext,
obj: HandleObject,
constants: &[Guard<&[ConstantSpec]>],
global: HandleObject,
@ -323,57 +334,65 @@ pub unsafe fn define_guarded_constants(
}
/// Conditionally define methods on an object.
pub unsafe fn define_guarded_methods(
cx: *mut JSContext,
pub fn define_guarded_methods(
cx: SafeJSContext,
obj: HandleObject,
methods: &[Guard<&'static [JSFunctionSpec]>],
global: HandleObject,
) {
for guard in methods {
if let Some(specs) = guard.expose(cx, obj, global) {
define_methods(cx, obj, specs).unwrap();
unsafe {
define_methods(*cx, obj, specs).unwrap();
}
}
}
}
/// Conditionally define properties on an object.
pub unsafe fn define_guarded_properties(
cx: *mut JSContext,
pub fn define_guarded_properties(
cx: SafeJSContext,
obj: HandleObject,
properties: &[Guard<&'static [JSPropertySpec]>],
global: HandleObject,
) {
for guard in properties {
if let Some(specs) = guard.expose(cx, obj, global) {
define_properties(cx, obj, specs).unwrap();
unsafe {
define_properties(*cx, obj, specs).unwrap();
}
}
}
}
/// Returns whether an interface with exposure set given by `globals` should
/// be exposed in the global object `obj`.
pub unsafe fn is_exposed_in(object: HandleObject, globals: Globals) -> bool {
pub fn is_exposed_in(object: HandleObject, globals: Globals) -> bool {
unsafe {
let unwrapped = UncheckedUnwrapObject(object.get(), /* stopAtWindowProxy = */ 0);
let dom_class = get_dom_class(unwrapped).unwrap();
globals.contains(dom_class.global)
}
}
/// 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,
pub fn define_on_global_object(
cx: SafeJSContext,
global: HandleObject,
name: &[u8],
obj: HandleObject,
) {
assert_eq!(*name.last().unwrap(), b'\0');
unsafe {
assert!(JS_DefineProperty3(
cx,
*cx,
global,
name.as_ptr() as *const libc::c_char,
obj,
JSPROP_RESOLVING
));
}
}
const OBJECT_OPS: ObjectOps = ObjectOps {
@ -409,6 +428,7 @@ unsafe extern "C" fn has_instance_hook(
value: RawMutableHandleValue,
rval: *mut bool,
) -> bool {
let cx = SafeJSContext::from_ptr(cx);
let obj_raw = HandleObject::from_raw(obj);
let val_raw = HandleValue::from_raw(value.handle());
match has_instance(cx, obj_raw, val_raw) {
@ -422,8 +442,8 @@ unsafe extern "C" fn has_instance_hook(
/// 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,
fn has_instance(
cx: SafeJSContext,
interface_object: HandleObject,
value: HandleValue,
) -> Result<bool, ()> {
@ -432,9 +452,10 @@ unsafe fn has_instance(
return Ok(false);
}
rooted!(in(cx) let mut value_out = value.to_object());
rooted!(in(cx) let mut value = value.to_object());
rooted!(in(*cx) let mut value_out = value.to_object());
rooted!(in(*cx) let mut value = value.to_object());
unsafe {
let js_class = get_object_class(interface_object.get());
let object_class = &*(js_class as *const NonCallbackInterfaceObjectClass);
@ -442,7 +463,8 @@ unsafe fn has_instance(
value.get(),
/* stopAtWindowProxy = */ 0,
)) {
if dom_class.interface_chain[object_class.proto_depth as usize] == object_class.proto_id {
if dom_class.interface_chain[object_class.proto_depth as usize] == object_class.proto_id
{
// Step 4.
return Ok(true);
}
@ -452,11 +474,11 @@ unsafe fn has_instance(
let global = GetNonCCWObjectGlobal(interface_object.get());
assert!(!global.is_null());
let proto_or_iface_array = get_proto_or_iface_array(global);
rooted!(in(cx) let prototype = (*proto_or_iface_array)[object_class.proto_id as usize]);
rooted!(in(*cx) let prototype = (*proto_or_iface_array)[object_class.proto_id as usize]);
assert!(!prototype.is_null());
// Step 3 only concern legacy callback interface objects (i.e. NodeFilter).
while JS_GetPrototype(cx, value.handle(), value_out.handle_mut()) {
while JS_GetPrototype(*cx, value.handle(), value_out.handle_mut()) {
*value = *value_out;
if value.is_null() {
// Step 5.2.
@ -466,52 +488,55 @@ unsafe fn has_instance(
return Ok(true);
}
}
}
// JS_GetPrototype threw an exception.
Err(())
}
unsafe fn create_unscopable_object(
cx: *mut JSContext,
names: &[&[u8]],
mut rval: MutableHandleObject,
) {
fn create_unscopable_object(cx: SafeJSContext, names: &[&[u8]], mut rval: MutableHandleObject) {
assert!(!names.is_empty());
assert!(rval.is_null());
rval.set(JS_NewPlainObject(cx));
unsafe {
rval.set(JS_NewPlainObject(*cx));
assert!(!rval.is_null());
for &name in names {
assert_eq!(*name.last().unwrap(), b'\0');
assert!(JS_DefineProperty(
cx,
*cx,
rval.handle(),
name.as_ptr() as *const libc::c_char,
HandleValue::from_raw(TrueHandleValue),
JSPROP_READONLY as u32,
));
}
}
}
unsafe fn define_name(cx: *mut JSContext, obj: HandleObject, name: &[u8]) {
fn define_name(cx: SafeJSContext, 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));
unsafe {
rooted!(in(*cx) let name = JS_AtomizeAndPinString(*cx, name.as_ptr() as *const libc::c_char));
assert!(!name.is_null());
assert!(JS_DefineProperty4(
cx,
*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) {
fn define_length(cx: SafeJSContext, obj: HandleObject, length: i32) {
unsafe {
assert!(JS_DefineProperty5(
cx,
*cx,
obj,
b"length\0".as_ptr() as *const libc::c_char,
length,
JSPROP_READONLY as u32
));
}
}
unsafe extern "C" fn invalid_constructor(

View file

@ -6,7 +6,8 @@
use crate::dom::bindings::guard::Guard;
use crate::dom::bindings::interface::{create_object, define_on_global_object};
use js::jsapi::{JSClass, JSContext, JSFunctionSpec};
use crate::script_runtime::JSContext;
use js::jsapi::{JSClass, JSFunctionSpec};
use js::rust::{HandleObject, MutableHandleObject};
/// The class of a namespace object.
@ -28,8 +29,8 @@ impl NamespaceObjectClass {
}
/// Create a new namespace object.
pub unsafe fn create_namespace_object(
cx: *mut JSContext,
pub fn create_namespace_object(
cx: JSContext,
global: HandleObject,
proto: HandleObject,
class: &'static NamespaceObjectClass,

View file

@ -1090,12 +1090,11 @@ impl TestBinding {
pub fn FuncControlledStaticMethodEnabled(_: &GlobalScope) {}
}
#[allow(unsafe_code)]
impl TestBinding {
pub unsafe fn condition_satisfied(_: *mut JSContext, _: HandleObject) -> bool {
pub fn condition_satisfied(_: SafeJSContext, _: HandleObject) -> bool {
true
}
pub unsafe fn condition_unsatisfied(_: *mut JSContext, _: HandleObject) -> bool {
pub fn condition_unsatisfied(_: SafeJSContext, _: HandleObject) -> bool {
false
}
}