Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up[WIP] Added JSContext struct with implementation #23657
Conversation
highfive
commented
Jun 28, 2019
|
Heads up! This PR modifies the following files:
|
highfive
commented
Jun 28, 2019
| @@ -388,7 +389,7 @@ pub unsafe extern "C" fn resolve_global( | |||
| let bytes = slice::from_raw_parts(ptr, length as usize); | |||
|
|
|||
| if let Some(init_fun) = InterfaceObjectMap::MAP.get(bytes) { | |||
| init_fun(cx, Handle::from_raw(obj)); | |||
| init_fun(*cx, Handle::from_raw(obj)); | |||
This comment has been minimized.
This comment has been minimized.
oneturkmen
Jul 9, 2019
Author
Contributor
@jdm is this what you meant (in terms of function call changes)?
This comment has been minimized.
This comment has been minimized.
jdm
Jul 9, 2019
Member
In this case, you modified MAP to store functions that accept script_runtime::JSContext. enumerate_global and resolve_global either need to accept cx: script_runtime::JSContext (and pass it unmodified to init_fun) or cx: *mut JSContext (and pass it to init_fun by creating a new script_runtime::JSContext based on cx).
| @@ -47,7 +47,7 @@ fn main() { | |||
| let mut phf = File::create(&phf).unwrap(); | |||
| write!( | |||
| &mut phf, | |||
| "pub static MAP: phf::Map<&'static [u8], unsafe fn(*mut JSContext, HandleObject)> = " | |||
| "pub static MAP: phf::Map<&'static [u8], /* unsafe */ fn(/* *mut */ JSContext, HandleObject)> = " | |||
This comment has been minimized.
This comment has been minimized.
oneturkmen
Jul 9, 2019
Author
Contributor
@jdm same here. I commented unsafe and *mut for now since we accept safe wrapped JSContext now.
(sorry for the mess, I should've removed those)
| @@ -15,7 +15,8 @@ use crate::dom::globalscope::GlobalScope; | |||
| use crate::dom::window::Window; | |||
| use js::jsapi::Heap; | |||
| use js::jsapi::JSAutoRealm; | |||
| use js::jsapi::{AddRawValueRoot, IsCallable, JSContext, JSObject}; | |||
| use js::jsapi::{AddRawValueRoot, IsCallable, /*JSContext, */JSObject}; | |||
| use crate::script_runtime::JSContext; | |||
This comment has been minimized.
This comment has been minimized.
oneturkmen
Jul 9, 2019
Author
Contributor
@jdm and I also tried to re-import a new, safe JSContext. I am not sure if this helps since the compiler just spits out a lot of errors.
This comment has been minimized.
This comment has been minimized.
jdm
Jul 9, 2019
Member
Yeah, I would expect this to cause a lot of errors because now there are many functions accepting *mut script_runtime::JSContext. I would recommend undoing the changes to most of these files. The only time we should be changing code from cx to *cx is when cx is of type script_runtime::JSContext and the receiving code expects *mut jsapi::JSContext.
|
Rather than changing all of the other files in components/script/dom to import script_runtime::JSContext, I think the best path forward is to only modify CodegenRust.py and get the result compiling by passing |
|
@jdm yes, thank you! |
fee35dd
to
972699c
| @@ -3314,7 +3314,7 @@ def __init__(self, errorResult, arguments, argsPre, returnType, | |||
| needsCx = needCx(returnType, (a for (a, _) in arguments), True) | |||
|
|
|||
| if "cx" not in argsPre and needsCx: | |||
| args.prepend(CGGeneric("cx")) | |||
| args.prepend(CGGeneric("JSContext::from_ptr(cx)")) | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
oneturkmen
Jul 10, 2019
•
Author
Contributor
Hmm did not we need to adjust this line? i.e.:
- adjust generated code to pass JSContext::from_ptr(cx) when necessary (here)
| @@ -6692,7 +6692,7 @@ def argument_type(descriptorProvider, ty, optional=False, defaultValue=None, var | |||
|
|
|||
| def method_arguments(descriptorProvider, returnType, arguments, passJSBits=True, trailing=None, inCompartment=False): | |||
| if needCx(returnType, arguments, passJSBits): | |||
| yield "cx", "*mut JSContext" | |||
| yield "cx", "JSContext" | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
oneturkmen
Jul 10, 2019
Author
Contributor
Same as above. I thought we needed to change the type:
- make the code generator declare trait methods that accept the new type instead of *mut JSContext (here and here; both the type )
| @@ -7134,7 +7134,7 @@ def getCall(self): | |||
| "${getCallable}" | |||
| "rooted!(in(cx) let rootedThis = ${thisObj});\n" | |||
| "let ok = ${callGuard}JS_CallFunctionValue(\n" | |||
| " cx, rootedThis.handle(), callable.handle(),\n" | |||
| " *cx, rootedThis.handle(), callable.handle(),\n" | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
oneturkmen
Jul 10, 2019
Author
Contributor
Don't JS_* functions expect *mut JSContext of js::jsapi? By dereferencing, I think we provide them with the type they want. Or did I miss something?
|
All of my comments were made under the assumption that we are not blindly replacing the |
|
I would really like to have checkpoints where Servo compiles with a small subset of the code that has been modified to use the new time. |
|
Ok, let me rollback and I will start integrating safe JSContext use statements in |
|
Let's say I am starting completely from scratch. I implement the new, safe JSContext in Further, do we proceed with the following subtasks (points 4 and 5 in #20377)? i.e.
as well as what you mentioned with
Or, do I directly go and start integrating new Sorry for being bothersome, I am a bit confused about the steps to get this issue done. |
|
Sorry that it seems like I've been giving contradictory suggestions! Here's how I think we should go about it, where each step is a separate commit that should build:
At that point it should be much more clear where the remaining work lies. |
39c8f71
to
921133d
|
Closing due to lack of available time by the author. |
oneturkmen commentedJun 28, 2019
•
edited
Wrapping unsafe raw JSContext pointers in a safe struct. Issue #20377.
Checklist:
struct JSContext(*mut jsapi::JSContext)typeDereffor this new type so it's easy to obtain the inner context pointerfrom_ptrstatic method that returns a new instance of the type*mut JSContext(here and here; both the type )JSContext::from_ptr(cx)when necessary (here)components/script/domthat need to be changed#[allow(unsafe_code)]on methods that no longer accept raw pointersThanks to @csmoe as I used some code from his branch.
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is