Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping unsafe raw JSContext pointers in a safe struct. Part 1. #23816

Merged
merged 11 commits into from Jul 24, 2019

Conversation

@marmeladema
Copy link
Contributor

marmeladema commented Jul 20, 2019

Wrapping unsafe raw JSContext pointers in a safe struct. Issue #20377.


  • create a struct JSContext(*mut jsapi::JSContext) type
  • implement Deref for this new type so it's easy to obtain the inner context pointer
  • add an unsafe from_ptr static method that returns a new instance of the type
  • start by changing various Argument uses in CodegenRust.py for python classes that inherit from CGAbstractMethod
  • convert the python code that interacts with CGClass (CGCallback, CGCallbackFunction, CGCallbackFunctionImpl, CGCallbackInterface) and any rust code that interacts with it
  • update CGDictionary and any rust code that interacts with it
  • make the code generator declare trait methods that accept the new type instead of *mut JSContext (CGInterfaceTrait)
  • modify GlobalScope::get_cx to return a SafeJSContext

This change is Reviewable

@highfive
Copy link

highfive commented Jul 20, 2019

Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @emilio (or someone else) soon.

@highfive
Copy link

highfive commented Jul 20, 2019

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/bindings/codegen/CodegenRust.py, components/script/dom/node.rs, components/script/dom/testworkletglobalscope.rs, components/script/dom/dedicatedworkerglobalscope.rs, components/script/dom/bindings/reflector.rs and 6 more
  • @KiChjang: components/script/dom/bindings/codegen/CodegenRust.py, components/script/dom/node.rs, components/script/dom/testworkletglobalscope.rs, components/script/dom/dedicatedworkerglobalscope.rs, components/script/dom/bindings/reflector.rs and 6 more
@highfive
Copy link

highfive commented Jul 20, 2019

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!
  • These commits modify script code, but no tests are modified. Please consider adding a test!
@marmeladema marmeladema force-pushed the marmeladema:issue-20377 branch from cd42989 to 12b8967 Jul 20, 2019
components/script/script_runtime.rs Outdated Show resolved Hide resolved
components/script/script_runtime.rs Outdated Show resolved Hide resolved
@jdm jdm assigned jdm and unassigned emilio Jul 21, 2019
@marmeladema marmeladema force-pushed the marmeladema:issue-20377 branch from 8620cf4 to b82c2bf Jul 21, 2019
@marmeladema
Copy link
Contributor Author

marmeladema commented Jul 21, 2019

@jdm i updated the code according to your comments. I am heading toward the right direction here?

@jdm
Copy link
Member

jdm commented Jul 21, 2019

Yes! This looks like what I expected to see :D

@marmeladema
Copy link
Contributor Author

marmeladema commented Jul 21, 2019

I am having trouble modifying CGAbstractStaticBindingMethod because i end up having a bunch of errors like

error[E0308]: mismatched types
   --> /home/adema/code/servo/target/debug/build/script-537c159507a8c0bf/out/Bindings/ConsoleBinding.rs:808:42
    |
808 |         call: JSNativeWrapper { op: Some(time), info: 0 as *const JSJitInfo },
    |                                          ^^^^ expected *-ptr, found struct `script_runtime::JSContext`
    |
    = note: expected type `unsafe extern "C" fn(*mut js::jsapi::JSContext, u32, *mut js::jsapi::Value) -> bool`
               found type `unsafe extern "C" fn(script_runtime::JSContext, u32, *mut js::jsapi::Value) -> bool {dom::bindings::codegen::Bindings::ConsoleBinding::ConsoleBinding::time}`

which i don't know how to solve.

@jdm
Copy link
Member

jdm commented Jul 21, 2019

Ah, indeed. Those will need to continue to have the raw pointer argument because the C function pointers can't be changed. We will want to declare a let cx = SafeJSContext::from_ptr(cx) as the first line of code in them instead.

@jdm
Copy link
Member

jdm commented Jul 21, 2019

Looking good so far!

@marmeladema marmeladema force-pushed the marmeladema:issue-20377 branch 2 times, most recently from 8c5ec29 to c70cd71 Jul 22, 2019
@marmeladema
Copy link
Contributor Author

marmeladema commented Jul 22, 2019

@jdm i've followed the steps you described in the other PR #23657 (comment)
We do you think are the next steps here?

Also taskcluster is often failing but it does not look it has anything to do with my changes. I think it fails when downloading some rust components.

@jdm
Copy link
Member

jdm commented Jul 22, 2019

The failure does come from your changes. If you run./mach test-tidy you should reproduce it.

@jdm
Copy link
Member

jdm commented Jul 22, 2019

I think the next big step would be modifying GlobalScope::get_cx to return a SafeJSContext and store one internally as well. That should remove a bunch of uses of from_ptr uses right now. At that point it would probably make sense to merge the changes and file follow-ups for increasing the usage in hand-written Rust code.

@marmeladema marmeladema force-pushed the marmeladema:issue-20377 branch from c70cd71 to 9360d10 Jul 22, 2019
@marmeladema
Copy link
Contributor Author

marmeladema commented Jul 22, 2019

The failure does comefromyourchanges. If you run/Mach test-tidy you should reproduce it.

Ok it should be fixed now.

I think the next big step would be modifying GlobalScope::get_cx to return a SafeJSContext and store one internally as well. That should remove a bunch of uses of from_ptr uses right now. At that point it would probably make sense to merge the changes and file follow-ups for increasing the usage in hand-written Rust code.

I'll look into it then 👍

@marmeladema
Copy link
Contributor Author

marmeladema commented Jul 22, 2019

@jdm I made all methods *::get_cx return a safe JSContext but I did not store one inside GlobalScope since it does not store one itself and uses Runtime::get() instead.

@marmeladema marmeladema changed the title [WIP] Wrapping unsafe raw JSContext pointers in a safe struct. Issue #20377. Wrapping unsafe raw JSContext pointers in a safe struct. Part 1. Jul 22, 2019
Copy link
Member

jdm left a comment

Great work! Let's fix these two nits and merge it!

components/script/dom/bindings/htmlconstructor.rs Outdated Show resolved Hide resolved
components/script/dom/bindings/reflector.rs Outdated Show resolved Hide resolved
@bors-servo
Copy link
Contributor

bors-servo commented Jul 24, 2019

🔒 Merge conflict

@marmeladema marmeladema force-pushed the marmeladema:issue-20377 branch from 0de646f to 0195159 Jul 24, 2019
@marmeladema marmeladema force-pushed the marmeladema:issue-20377 branch from 0195159 to 88cacfb Jul 24, 2019
@marmeladema
Copy link
Contributor Author

marmeladema commented Jul 24, 2019

@jdm i rebased and fixed the conflicts but appveyor failed and i don't really understand what is the problem. I don't know what to look for in the build log.

@jdm
Copy link
Member

jdm commented Jul 24, 2019

It hit the time limit for the build and it's very bad at making that clear. We can ignore it.

@jdm
Copy link
Member

jdm commented Jul 24, 2019

@bors-servo
Copy link
Contributor

bors-servo commented Jul 24, 2019

📌 Commit 88cacfb has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Jul 24, 2019

Testing commit 88cacfb with merge 9b14bbd...

bors-servo added a commit that referenced this pull request Jul 24, 2019
Wrapping unsafe raw JSContext pointers in a safe struct. Part 1.

Wrapping unsafe raw JSContext pointers in a safe struct. Issue #20377.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] create a struct JSContext(*mut jsapi::JSContext) type
- [x] implement Deref for this new type so it's easy to obtain the inner context pointer
- [x] add an unsafe from_ptr static method that returns a new instance of the type
- [x] start by changing various Argument uses in CodegenRust.py for python classes that inherit from CGAbstractMethod
- [x] convert the python code that interacts with CGClass (CGCallback, CGCallbackFunction, CGCallbackFunctionImpl, CGCallbackInterface) and any rust code that interacts with it
- [x] update CGDictionary and any rust code that interacts with it
- [x] make the code generator declare trait methods that accept the new type instead of *mut JSContext (CGInterfaceTrait)
- [x] modify GlobalScope::get_cx to return a SafeJSContext

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23816)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Jul 24, 2019

☀️ Test successful - linux-rel-css, linux-rel-wpt, status-taskcluster
Approved by: jdm
Pushing 9b14bbd to master...

@bors-servo bors-servo merged commit 88cacfb into servo:master Jul 24, 2019
2 of 3 checks passed
2 of 3 checks passed
continuous-integration/appveyor/pr AppVeyor build failed
Details
Taskcluster (pull_request) TaskGroup: success
Details
homu Test successful
Details
@marmeladema marmeladema deleted the marmeladema:issue-20377 branch Jul 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants
You can’t perform that action at this time.