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 upMade all "free" extern "C" APIs in simpleservo fallible by returning … #23828
Conversation
|
r? @jdm |
| }); | ||
|
|
||
| match _result { | ||
| Ok(_) => return true, |
This comment has been minimized.
This comment has been minimized.
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.
This comment has been minimized.
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()
}|
Thanks for the comments. Can I propose to do the following based on your comments?
|
Yes! Sure! Actually, 1 & 2 are what I thought :D! And 3 & 4 also sounds great to me! Thanks! |
|
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.
This comment has been minimized.
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.
This comment has been minimized.
| 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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
|
@bors-servo r+ |
|
|
…n "C" functions returning bool. Modified "free" extern "C" functions to now be falliable by returning bool value.
ef4d579
to
dde4f71
|
@bors-servo r+ |
|
|
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 retry |
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 -->
|
|
angelortiz1007 commentedJul 22, 2019
•
edited by CYBAI
…null pointr or boolean value. ServoApp will need to be modified to check return values.
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is