From ccde8185f9d38b7c06fed1f0c2f0c9705fe35613 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 25 Mar 2018 11:51:22 +0800 Subject: [PATCH 1/3] wrap raw jsctxt --- components/script/script_runtime.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs index f758a1131c03..c0d34c189546 100644 --- a/components/script/script_runtime.rs +++ b/components/script/script_runtime.rs @@ -473,3 +473,13 @@ unsafe fn set_gc_zeal_options(rt: *mut JSRuntime) { #[allow(unsafe_code)] #[cfg(not(feature = "debugmozjs"))] unsafe fn set_gc_zeal_options(_: *mut JSRuntime) {} + +pub struct RawJSContext(*mut JSContext); + +#[allow(unsafe_code)] +impl Deref for RawJSContext { + type Target = JSContext; + fn deref(&self) -> &JSContext { + unsafe { &*self.0 } + } +} From 0e5957613080367a247925654f54fffeb148d157 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Wed, 28 Mar 2018 15:52:33 +0800 Subject: [PATCH 2/3] rename import to RawJSContext and impl from_ptr --- components/script/script_runtime.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs index c0d34c189546..6e014bd99119 100644 --- a/components/script/script_runtime.rs +++ b/components/script/script_runtime.rs @@ -16,7 +16,7 @@ use dom::bindings::utils::DOM_CALLBACKS; use dom::globalscope::GlobalScope; use js::glue::CollectServoSizes; use js::jsapi::{DisableIncrementalGC, GCDescription, GCProgress, HandleObject}; -use js::jsapi::{JSContext, JS_GetRuntime, JSRuntime, JSTracer, SetDOMCallbacks, SetGCSliceCallback}; +use js::jsapi::{JSContext as RawJSContext, JS_GetRuntime, JSRuntime, JSTracer, SetDOMCallbacks, SetGCSliceCallback}; use js::jsapi::{JSGCInvocationKind, JSGCStatus, JS_AddExtraGCRootsTracer, JS_SetGCCallback}; use js::jsapi::{JSGCMode, JSGCParamKey, JS_SetGCParameter, JS_SetGlobalJitCompilerOption}; use js::jsapi::{JSJitCompilerOption, JS_SetOffthreadIonCompilationEnabled, JS_SetParallelParsingEnabled}; @@ -109,7 +109,7 @@ pub trait ScriptPort { /// SM callback for promise job resolution. Adds a promise callback to the current /// global's microtask queue. #[allow(unsafe_code)] -unsafe extern "C" fn enqueue_job(cx: *mut JSContext, +unsafe extern "C" fn enqueue_job(cx: *mut RawJSContext, job: HandleObject, _allocation_site: HandleObject, _data: *mut c_void) -> bool { @@ -157,7 +157,7 @@ pub unsafe fn new_rt_and_cx() -> Runtime { SetGCSliceCallback(runtime.rt(), Some(gc_slice_callback)); } - unsafe extern "C" fn empty_wrapper_callback(_: *mut JSContext, _: *mut JSObject) -> bool { true } + unsafe extern "C" fn empty_wrapper_callback(_: *mut RawJSContext, _: *mut JSObject) -> bool { true } SetDOMCallbacks(runtime.rt(), &DOM_CALLBACKS); SetPreserveWrapperCallback(runtime.rt(), Some(empty_wrapper_callback)); // Pre barriers aren't working correctly at the moment @@ -334,7 +334,7 @@ unsafe extern "C" fn get_size(obj: *mut JSObject) -> usize { } #[allow(unsafe_code)] -pub fn get_reports(cx: *mut JSContext, path_seg: String) -> Vec { +pub fn get_reports(cx: *mut RawJSContext, path_seg: String) -> Vec { let mut reports = vec![]; unsafe { @@ -474,12 +474,21 @@ unsafe fn set_gc_zeal_options(rt: *mut JSRuntime) { #[cfg(not(feature = "debugmozjs"))] unsafe fn set_gc_zeal_options(_: *mut JSRuntime) {} -pub struct RawJSContext(*mut JSContext); +pub struct JSContext(*mut RawJSContext); #[allow(unsafe_code)] -impl Deref for RawJSContext { - type Target = JSContext; - fn deref(&self) -> &JSContext { +#[allow(dead_code)] // FIXME: remove in next commit +impl JSContext { + unsafe fn from_ptr(raw_jsctxt: *mut RawJSContext) -> Self { + JSContext(raw_jsctxt) + } +} + +#[allow(unsafe_code)] +impl Deref for JSContext { + type Target = RawJSContext; + fn deref(&self) -> &RawJSContext { unsafe { &*self.0 } - } + } } + From 24c7982d10a5a417d5056eaf302a30a3584bca8b Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Fri, 22 Jun 2018 22:04:57 +0800 Subject: [PATCH 3/3] codegen: raw to JSContext --- .../dom/bindings/codegen/CodegenRust.py | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 01b52d916aea..f05080a99e8d 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -2501,7 +2501,7 @@ class CGConstructorEnabled(CGAbstractMethod): def __init__(self, descriptor): CGAbstractMethod.__init__(self, descriptor, 'ConstructorEnabled', 'bool', - [Argument("*mut JSContext", "aCx"), + [Argument("JSContext", "aCx"), Argument("HandleObject", "aObj")], unsafe=True) @@ -2621,7 +2621,7 @@ class CGWrapMethod(CGAbstractMethod): def __init__(self, descriptor): assert not descriptor.interface.isCallback() assert not descriptor.isGlobal() - args = [Argument('*mut JSContext', 'cx'), + args = [Argument('JSContext', 'cx'), Argument('&GlobalScope', 'scope'), Argument("Box<%s>" % descriptor.concreteType, 'object')] retval = 'DomRoot<%s>' % descriptor.concreteType @@ -2656,7 +2656,7 @@ class CGWrapGlobalMethod(CGAbstractMethod): def __init__(self, descriptor, properties): assert not descriptor.interface.isCallback() assert descriptor.isGlobal() - args = [Argument('*mut JSContext', 'cx'), + args = [Argument('JSContext', 'cx'), Argument("Box<%s>" % descriptor.concreteType, 'object')] retval = 'DomRoot<%s>' % descriptor.concreteType CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args, @@ -2800,7 +2800,7 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod): properties should be a PropertyArrays instance. """ def __init__(self, descriptor, properties, haveUnscopables): - args = [Argument('*mut JSContext', 'cx'), Argument('HandleObject', 'global'), + args = [Argument('JSContext', 'cx'), Argument('HandleObject', 'global'), Argument('*mut ProtoOrIfaceArray', 'cache')] CGAbstractMethod.__init__(self, descriptor, 'CreateInterfaceObjects', 'void', args, unsafe=True) @@ -3042,7 +3042,7 @@ class CGGetPerInterfaceObject(CGAbstractMethod): constructor object). """ def __init__(self, descriptor, name, idPrefix="", pub=False): - args = [Argument('*mut JSContext', 'cx'), + args = [Argument('JSContext', 'cx'), Argument('HandleObject', 'global'), Argument('MutableHandleObject', 'mut rval')] CGAbstractMethod.__init__(self, descriptor, name, @@ -3180,7 +3180,7 @@ class CGDefineDOMInterfaceMethod(CGAbstractMethod): def __init__(self, descriptor): assert descriptor.interface.hasInterfaceObject() args = [ - Argument('*mut JSContext', 'cx'), + Argument('JSContext', 'cx'), Argument('HandleObject', 'global'), ] CGAbstractMethod.__init__(self, descriptor, 'DefineDOMInterface', @@ -3477,7 +3477,7 @@ class CGAbstractStaticBindingMethod(CGAbstractMethod): """ def __init__(self, descriptor, name): args = [ - Argument('*mut JSContext', 'cx'), + Argument('JSContext', 'cx'), Argument('libc::c_uint', 'argc'), Argument('*mut JSVal', 'vp'), ] @@ -3504,7 +3504,7 @@ class CGSpecializedMethod(CGAbstractExternMethod): def __init__(self, descriptor, method): self.method = method name = method.identifier.name - args = [Argument('*mut JSContext', 'cx'), Argument('HandleObject', '_obj'), + args = [Argument('JSContext', 'cx'), Argument('HandleObject', '_obj'), Argument('*const %s' % descriptor.concreteType, 'this'), Argument('*const JSJitMethodCallArgs', 'args')] CGAbstractExternMethod.__init__(self, descriptor, name, 'bool', args) @@ -3552,7 +3552,7 @@ class CGSpecializedGetter(CGAbstractExternMethod): def __init__(self, descriptor, attr): self.attr = attr name = 'get_' + descriptor.internalNameFor(attr.identifier.name) - args = [Argument('*mut JSContext', 'cx'), + args = [Argument('JSContext', 'cx'), Argument('HandleObject', '_obj'), Argument('*const %s' % descriptor.concreteType, 'this'), Argument('JSJitGetterCallArgs', 'args')] @@ -3607,7 +3607,7 @@ class CGSpecializedSetter(CGAbstractExternMethod): def __init__(self, descriptor, attr): self.attr = attr name = 'set_' + descriptor.internalNameFor(attr.identifier.name) - args = [Argument('*mut JSContext', 'cx'), + args = [Argument('JSContext', 'cx'), Argument('HandleObject', 'obj'), Argument('*const %s' % descriptor.concreteType, 'this'), Argument('JSJitSetterCallArgs', 'args')] @@ -4101,7 +4101,7 @@ def __init__(self, enum): } impl ToJSValConvertible for super::${ident} { - unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) { + unsafe fn to_jsval(&self, cx: JSContext, rval: MutableHandleValue) { pairs[*self as usize].0.to_jsval(cx, rval); } } @@ -4244,7 +4244,7 @@ def define(self): } impl ToJSValConvertible for %s { - unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) { + unsafe fn to_jsval(&self, cx: JSContext, rval: MutableHandleValue) { match *self { %s } @@ -4398,7 +4398,7 @@ def getStringOrPrimitiveConversion(memberType): "Err(())" % ", ".join(names))) method = CGWrapper( CGIndenter(CGList(conversions, "\n\n")), - pre="unsafe fn from_jsval(cx: *mut JSContext,\n" + pre="unsafe fn from_jsval(cx: JSContext,\n" " value: HandleValue,\n" " _option: ())\n" " -> Result, ()> {\n" % self.type, @@ -4421,7 +4421,7 @@ def try_method(self, t): return CGWrapper( CGIndenter(jsConversion, 4), - pre="unsafe fn TryConvertTo%s(cx: *mut JSContext, value: HandleValue) -> %s {\n" + pre="unsafe fn TryConvertTo%s(cx: JSContext, value: HandleValue) -> %s {\n" % (t.name, returnType), post="\n}") @@ -4917,7 +4917,7 @@ def definition_body(self): class CGDOMJSProxyHandler_getOwnPropertyDescriptor(CGAbstractExternMethod): def __init__(self, descriptor): - args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), Argument('RawHandleId', 'id'), Argument('RawMutableHandle', 'desc')] CGAbstractExternMethod.__init__(self, descriptor, "getOwnPropertyDescriptor", @@ -5019,7 +5019,7 @@ def definition_body(self): class CGDOMJSProxyHandler_defineProperty(CGAbstractExternMethod): def __init__(self, descriptor): - args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), Argument('RawHandleId', 'id'), Argument('RawHandle', 'desc'), Argument('*mut ObjectOpResult', 'opresult')] @@ -5068,7 +5068,7 @@ def definition_body(self): class CGDOMJSProxyHandler_delete(CGAbstractExternMethod): def __init__(self, descriptor): - args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), Argument('RawHandleId', 'id'), Argument('*mut ObjectOpResult', 'res')] CGAbstractExternMethod.__init__(self, descriptor, "delete", "bool", args) @@ -5090,7 +5090,7 @@ def definition_body(self): class CGDOMJSProxyHandler_ownPropertyKeys(CGAbstractExternMethod): def __init__(self, descriptor): - args = [Argument('*mut JSContext', 'cx'), + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), Argument('*mut AutoIdVector', 'props')] CGAbstractExternMethod.__init__(self, descriptor, "own_property_keys", "bool", args) @@ -5145,7 +5145,7 @@ class CGDOMJSProxyHandler_getOwnEnumerablePropertyKeys(CGAbstractExternMethod): def __init__(self, descriptor): assert (descriptor.operations["IndexedGetter"] and descriptor.interface.getExtendedAttribute("LegacyUnenumerableNamedProperties")) - args = [Argument('*mut JSContext', 'cx'), + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), Argument('*mut AutoIdVector', 'props')] CGAbstractExternMethod.__init__(self, descriptor, @@ -5186,7 +5186,7 @@ def definition_body(self): class CGDOMJSProxyHandler_hasOwn(CGAbstractExternMethod): def __init__(self, descriptor): - args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), Argument('RawHandleId', 'id'), Argument('*mut bool', 'bp')] CGAbstractExternMethod.__init__(self, descriptor, "hasOwn", "bool", args) self.descriptor = descriptor @@ -5248,7 +5248,7 @@ def definition_body(self): class CGDOMJSProxyHandler_get(CGAbstractExternMethod): def __init__(self, descriptor): - args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', 'proxy'), Argument('RawHandleValue', 'receiver'), Argument('RawHandleId', 'id'), Argument('RawMutableHandleValue', 'vp')] CGAbstractExternMethod.__init__(self, descriptor, "get", "bool", args) @@ -5334,7 +5334,7 @@ def definition_body(self): class CGDOMJSProxyHandler_className(CGAbstractExternMethod): def __init__(self, descriptor): - args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', '_proxy')] + args = [Argument('JSContext', 'cx'), Argument('RawHandleObject', '_proxy')] CGAbstractExternMethod.__init__(self, descriptor, "className", "*const i8", args, doesNotPanic=True) self.descriptor = descriptor @@ -5426,7 +5426,7 @@ class CGClassConstructHook(CGAbstractExternMethod): JS-visible constructor for our objects """ def __init__(self, descriptor, constructor=None): - args = [Argument('*mut JSContext', 'cx'), Argument('u32', 'argc'), Argument('*mut JSVal', 'vp')] + args = [Argument('JSContext', 'cx'), Argument('u32', 'argc'), Argument('*mut JSVal', 'vp')] name = CONSTRUCT_HOOK_NAME if constructor: name += "_" + constructor.identifier.name @@ -5553,7 +5553,7 @@ def __init__(self, descriptor): def attribute_arguments(needCx, argument=None): if needCx: - yield "cx", "*mut JSContext" + yield "cx", "JSContext" if argument: yield "value", argument_type(descriptor, argument) @@ -5617,7 +5617,7 @@ def fmt(arguments): def contains_unsafe_arg(arguments): if not arguments or len(arguments) == 0: return False - return reduce((lambda x, y: x or y[1] == '*mut JSContext'), arguments, False) + return reduce((lambda x, y: x or y[1] == 'JSContext'), arguments, False) methods = [] for name, arguments, rettype in members(): @@ -6224,13 +6224,13 @@ def memberInsert(memberInfo): return string.Template( "impl ${selfName} {\n" - " pub unsafe fn empty(cx: *mut JSContext) -> ${actualType} {\n" + " pub unsafe fn empty(cx: JSContext) -> ${actualType} {\n" " match ${selfName}::new(cx, HandleValue::null()) {\n" " Ok(ConversionResult::Success(v)) => v,\n" " _ => unreachable!(),\n" " }\n" " }\n" - " pub unsafe fn new(cx: *mut JSContext, val: HandleValue) \n" + " pub unsafe fn new(cx: JSContext, val: HandleValue) \n" " -> Result, ()> {\n" " let object = if val.get().is_null_or_undefined() {\n" " ptr::null_mut()\n" @@ -6250,14 +6250,14 @@ def memberInsert(memberInfo): "\n" "impl FromJSValConvertible for ${actualType} {\n" " type Config = ();\n" - " unsafe fn from_jsval(cx: *mut JSContext, value: HandleValue, _option: ())\n" + " unsafe fn from_jsval(cx: JSContext, value: HandleValue, _option: ())\n" " -> Result, ()> {\n" " ${selfName}::new(cx, value)\n" " }\n" "}\n" "\n" "impl ToJSValConvertible for ${selfName} {\n" - " unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {\n" + " unsafe fn to_jsval(&self, cx: JSContext, mut rval: MutableHandleValue) {\n" " rooted!(in(cx) let obj = JS_NewObject(cx, ptr::null()));\n" "${insertMembers}" " rval.set(ObjectOrNullValue(obj.get()))\n" @@ -6555,7 +6555,7 @@ def argument_type(descriptorProvider, ty, optional=False, defaultValue=None, var def method_arguments(descriptorProvider, returnType, arguments, passJSBits=True, trailing=None): if needCx(returnType, arguments, passJSBits): - yield "cx", "*mut JSContext" + yield "cx", "JSContext" for argument in arguments: ty = argument_type(descriptorProvider, argument.type, argument.optional, @@ -6635,7 +6635,7 @@ def __init__(self, idlObject, descriptorProvider, baseName, methods): def getConstructors(self): return [ClassConstructor( - [Argument("*mut JSContext", "aCx"), Argument("*mut JSObject", "aCallback")], + [Argument("JSContext", "aCx"), Argument("*mut JSObject", "aCallback")], bodyInHeader=True, visibility="pub", explicit=False, @@ -6648,7 +6648,7 @@ def getMethodImpls(self, method): args = list(method.args) # Strip out the JSContext*/JSObject* args # that got added. - assert args[0].name == "cx" and args[0].argType == "*mut JSContext" + assert args[0].name == "cx" and args[0].argType == "JSContext" assert args[1].name == "aThisObj" and args[1].argType == "HandleObject" args = args[2:] # Record the names of all the arguments, so we can use them when we call @@ -6731,7 +6731,7 @@ class CGCallbackFunctionImpl(CGGeneric): def __init__(self, callback): impl = string.Template("""\ impl CallbackContainer for ${type} { - unsafe fn new(cx: *mut JSContext, callback: *mut JSObject) -> Rc<${type}> { + unsafe fn new(cx: JSContext, callback: *mut JSObject) -> Rc<${type}> { ${type}::new(cx, callback) } @@ -6741,7 +6741,7 @@ def __init__(self, callback): } impl ToJSValConvertible for ${type} { - unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) { + unsafe fn to_jsval(&self, cx: JSContext, rval: MutableHandleValue) { self.callback().to_jsval(cx, rval); } }\ @@ -6935,7 +6935,7 @@ def getArgs(self, returnType, argList): return args # We want to allow the caller to pass in a "this" object, as # well as a JSContext. - return [Argument("*mut JSContext", "cx"), + return [Argument("JSContext", "cx"), Argument("HandleObject", "aThisObj")] + args def getCallSetup(self):