-
Notifications
You must be signed in to change notification settings - Fork 38
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
Rust bindings (unsafe execution) #566
Conversation
dc15f97
to
49ef6bb
Compare
Codecov Report
@@ Coverage Diff @@
## master #566 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 69 69
Lines 9712 9712
=======================================
Hits 9555 9555
Misses 157 157
Flags with carried forward coverage won't be shown. Click here to find out more. |
bindings/rust/src/lib.rs
Outdated
|
||
impl Module { | ||
// TODO: support imported functions{ | ||
pub fn instantiate(self) -> Option<Instance> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably return a Result, as failure to instantiate is more of an error condition. Also, question mark operator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was wondering if I could avoid defining error types (given nothing is returned), but the question mark operator is a good point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to have custom error type otherwise question mark operator won't work 😞
@@ -4,8 +4,123 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conventional file layout is:
<use statements>
<module declarations>
<trait declarations>
<struct declarations>
<struct methods>
<trait impls>
bindings/rust/src/lib.rs
Outdated
pub type ExecutionResult = sys::FizzyExecutionResult; | ||
|
||
impl Instance { | ||
pub fn execute(&mut self, func_idx: u32, args: &[Value]) -> ExecutionResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ergonomics could be better if func_idx
was replaced with the name. Users will probably be looking to call an export by name anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The C API does not yet offer anything like that and this is a direct translation as of now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added function to look it up (now that the underlying API has it). Since this function is called unsafe_execute
, I think it is fine to keep func_idx
as it already has unsafe inputs.
Will eventually have a properly wrapped safe execute function, similar to what it is in wasmi and others.
dfdbfbf
to
6525b9d
Compare
f38f7e5
to
59419bd
Compare
54e4c4e
to
d2e6a1f
Compare
bindings/rust/src/lib.rs
Outdated
|
||
/// The optional return value. Only a single return value is allowed in WebAssembly 1.0. | ||
pub fn value(&self) -> Option<Value> { | ||
if !self.0.trapped && self.0.has_value { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation says otherwise, but this is too defensive. It is guaranteed that has_value
if false
if trapped
is true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gumb0 suggested this. Where is it guaranteed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation says otherwise, but this is too defensive. It is guaranteed that
has_value
iffalse
iftrapped
istrue
.
Here it's the opposite - when trapped
is false
, we have to check has_value
.
What I suggested was changing if has_value
-> if !trapped && has_value
Yeah I see what you mean, perhaps could be changed back, with a change to C API documentation and some new asserts in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I suggested was changing if has_value -> if !trapped && has_value
This is what the code has now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ExecuteResult
constructor guarantees it. If you want to keep it, swap the checks: has_value && !trapped
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can also add the trapped as an assertion, which is only present in debug builds:
pub fn value(&self) -> Option<Value> {
if self.0.has_value {
assert!(!self.0.trapped);
Some(self.0.value)
} else {
None
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ExecuteResult constructor guarantees it.
The C API has no constructor guarantee for it though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ExecuteResult constructor guarantees it.
The C API has no constructor guarantee for it though?
No constructor guarantees but FizzyExecutionResult wrap(const fizzy::ExecutionResult&)
just copies all three fields always.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can also add the trapped as an assertion, which is only present in debug builds:
This variant looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with changing trapped
check to assertion.
e45448f
to
4eb205c
Compare
bindings/rust/src/lib.rs
Outdated
|
||
/// The optional return value. Only a single return value is allowed in WebAssembly 1.0. | ||
pub fn value(&self) -> Option<Value> { | ||
if !self.0.trapped && self.0.has_value { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with changing trapped
check to assertion.
TODO:
Fizzy error enum(will do this in a new PR)