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

Made all "free" extern "C" APIs in simpleservo fallible by returning … #23828

Merged
merged 1 commit into from Jul 25, 2019

Conversation

@angelortiz1007
Copy link
Contributor

angelortiz1007 commented Jul 22, 2019

…null pointr or boolean value. ServoApp will need to be modified to check return values.


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes fix #23514 (GitHub issue number if applicable)
  • There are tests for these changes OR
  • These changes do not require tests because ServoApp will test/verify._

This change is Reviewable

@jdm
Copy link
Member

jdm commented Jul 22, 2019

r? @jdm

});

match _result {
Ok(_) => return true,

This comment has been minimized.

@CYBAI

CYBAI Jul 23, 2019

Collaborator

nit: we can just use result.is_ok() (Same nit for other pattern matchings with return boolean below)

btw, maybe we don't need underscore prefix for result? 👀


match _result {
Ok(_) => return _ptr,
Err(_) => return std::ptr::null(),

This comment has been minimized.

@CYBAI

CYBAI Jul 23, 2019

Collaborator

maybe no need to return explicitly while it's in the end of function 👀

match result {
  Ok(_) => ptr
  Err(_) => std::ptr::null()
}
@angelortiz1007
Copy link
Contributor Author

angelortiz1007 commented Jul 23, 2019

Thanks for the comments. Can I propose to do the following based on your comments?

  1. remove the "_" from the variable "_result".
  2. in the match{} statements remove the explicit "returns"
  3. After 1&2, I commit changes. I can squash the changes with the previous commit as well.
  4. run mach test-tidy and mach fmt if needed.
@CYBAI
Copy link
Collaborator

CYBAI commented Jul 23, 2019

Thanks for the comments. Can I propose to do the following based on your comments?

1. remove the "_" from the variable "_result".

2. in the match{} statements remove the explicit "returns"

3. After 1&2, I commit changes.  I can squash the changes with the previous commit as well.

4. run mach test-tidy and mach fmt if needed.

Yes! Sure! Actually, 1 & 2 are what I thought :D!

And 3 & 4 also sounds great to me! Thanks!

Copy link
Member

jdm left a comment

Good start!

let _result = panic::catch_unwind(AssertUnwindSafe(|| {
let v = simpleservo::servo_version();
let text = CString::new(v).expect("Can't create string");
_ptr = text.as_ptr();

This comment has been minimized.

@jdm

jdm Jul 23, 2019

Member

Rather than using a mutable variable, we can do the following:

let result = panic::catch_unwind(AssertUnwindSafe(|| {
    let v = simpleservo::servo_version();
    let text = CString::new(v).expect("Can't create string");
    let ptr = text.as_ptr();
    mem::forget(text)
    ptr
}));

match result {
    Ok(ptr) => ptr,
    Err(_) => std::ptr::null(),
}

The return value of catch_unwind contains the return value of the closure that is invoked by catch_unwind, so we don't need to do any special communication with the environment outside of the closure to extract it.

This comment has been minimized.

@angelortiz1007

angelortiz1007 Jul 23, 2019

Author Contributor

Thank you. Good suggestion and better understanding.

let gl = gl_glue::gl::init().unwrap();
unsafe { init(opts, gl, None, None, wakeup, callbacks) }
) -> bool {
let _result = panic::catch_unwind(|| {

This comment has been minimized.

@jdm

jdm Jul 23, 2019

Member

Given how frequently we repeat this pattern of catch_unwind followed by checking its return value, we can make a helper to reduce some duplication:

fn catch_any_panic<F: FnOnce() + UnwindSafe>(function: F) -> bool {
    panic::catch_unwind(function).is_ok()
}

Then we can write the calling code like:

extern "C" fn perform_updates() -> bool {
    catch_any_panic(|| {
        debug!("perform_updates");
        call(|s| s.perform_updates());
    })
}

This will make it easier to maintain in the future if we want to add additional behaviour for logging information about panics, for example.

This comment has been minimized.

@jdm

jdm Jul 23, 2019

Member

I modeled this idea after this code we wrote a few years ago for a similar purpose.

This comment has been minimized.

@angelortiz1007

angelortiz1007 Jul 23, 2019

Author Contributor

OK. I will make the changes. I was under the impression/misunderstanding on .is_ok() will return true when there is no panic, but was wondering how you return false if no explicit statement to do so.

This comment has been minimized.

@jdm

jdm Jul 23, 2019

Member

Result::is_ok() returns true if the value is Ok, and false if the value is Err. panic::catch_unwind returns Result<???> where ??? is the return type of the closure that it invokes, so Ok if there is no panic and Err otherwise. Therefore, catch_any_panic returns true if no panic occurred inside the closure passed to panic::catch_unwind.

This comment has been minimized.

@angelortiz1007

angelortiz1007 Jul 23, 2019

Author Contributor

Can the generic function "catch_any_panic()" further be generalized to return a type based on the function signature?

I typed this up, doesn't build, but feel I'm close.

fn catch_any_panic<F: FnOnce() + UnwindSafe>(function: F) -> Result<T, E> {
let result = panic::catch_unwind(function);

match result {
    OK(T) => T,
    Err(E) => E,
}

}

This comment has been minimized.

@jdm

jdm Jul 23, 2019

Member

It could (that's what this code does), but then we end up having all of the callers that need a boolean return value have to do extra processing. It's not clear to me what the value of this change would be.

This comment has been minimized.

@angelortiz1007

angelortiz1007 Jul 23, 2019

Author Contributor

I was thinking about the function pub extern "C" fn servo_version() -> *const c_char {} which returns a *const c_char. I was trying to fit the catch_any_panic() to servo_version() too.

This comment has been minimized.

@angelortiz1007

angelortiz1007 Jul 23, 2019

Author Contributor

I've got all the suggestions incorporated into my repository. I will run a few tests, add, commit and push.

My question can be deferred.

@jdm
Copy link
Member

jdm commented Jul 24, 2019

@bors-servo r+
Thanks!

@bors-servo
Copy link
Contributor

bors-servo commented Jul 24, 2019

📌 Commit ef4d579 has been approved by jdm

…n "C" functions returning bool. Modified "free" extern "C" functions to now be falliable by returning bool value.
@angelortiz1007 angelortiz1007 force-pushed the angelortiz1007:winrt-servo branch from ef4d579 to dde4f71 Jul 24, 2019
@jdm
Copy link
Member

jdm commented Jul 24, 2019

@bors-servo
Copy link
Contributor

bors-servo commented Jul 24, 2019

📌 Commit dde4f71 has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Jul 24, 2019

Testing commit dde4f71 with merge 55983c3...

bors-servo added a commit that referenced this pull request Jul 24, 2019
Made all "free" extern "C" APIs in simpleservo fallible by returning …

…null pointr or boolean value.  ServoApp will need to be modified to check return values.

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #23514 (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because _ServoApp will test/verify.__

<!-- 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/23828)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Jul 25, 2019

💔 Test failed - status-taskcluster

@jdm
Copy link
Member

jdm commented Jul 25, 2019

@bors-servo
Copy link
Contributor

bors-servo commented Jul 25, 2019

Testing commit dde4f71 with merge d93b652...

bors-servo added a commit that referenced this pull request Jul 25, 2019
Made all "free" extern "C" APIs in simpleservo fallible by returning …

…null pointr or boolean value.  ServoApp will need to be modified to check return values.

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #23514 (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because _ServoApp will test/verify.__

<!-- 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/23828)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Jul 25, 2019

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

@bors-servo bors-servo merged commit dde4f71 into servo:master Jul 25, 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
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.

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