-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat: add more types to polyfill wasmtime in js #3
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use crate::DataHandle; | ||
use crate::Store; | ||
|
||
#[derive(Debug)] | ||
pub struct Caller<T> { | ||
handle: DataHandle<T>, | ||
pub(crate) store: Store<T>, | ||
} | ||
|
||
impl<T> Caller<T> { | ||
pub(crate) fn new(handle: DataHandle<T>) -> Self { | ||
Self { handle } | ||
pub(crate) fn new(store: Store<T>) -> Self { | ||
Self { store } | ||
} | ||
|
||
pub fn data(&self) -> impl Deref<Target = T> + '_ { | ||
self.handle.borrow() | ||
self.store.data() | ||
} | ||
|
||
pub fn data_mut(&mut self) -> impl DerefMut<Target = T> + '_ { | ||
self.handle.borrow_mut() | ||
self.store.data_mut() | ||
} | ||
} | ||
|
||
impl<T> Clone for Caller<T> { | ||
fn clone(&self) -> Self { | ||
Caller::new(self.handle.clone()) | ||
Caller::new(self.store.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use anyhow::bail; | ||
use js_sys::{Function, WebAssembly}; | ||
|
||
use crate::{helpers::map_js_error, AsContextMut, Error, FromJsValue, ToJsValue, Val}; | ||
|
||
pub struct Func { | ||
instance: WebAssembly::Instance, | ||
function: Function, | ||
} | ||
|
||
impl Func { | ||
pub fn new(instance: WebAssembly::Instance, function: Function) -> Self { | ||
Func { instance, function } | ||
} | ||
|
||
pub fn call( | ||
&self, | ||
_: impl AsContextMut, | ||
params: &[Val], | ||
results: &mut [Val], | ||
) -> Result<(), Error> { | ||
let params = params | ||
.iter() | ||
.map(|val| val.to_js_value()) | ||
.collect::<js_sys::Array>(); | ||
let result = self | ||
.function | ||
.apply(&self.function, ¶ms) | ||
.map_err(map_js_error("Exported function threw an exception"))?; | ||
let data = if result.is_array() { | ||
Vec::<Val>::from_js_value(&result)? | ||
} else if result.is_undefined() || result.is_null() { | ||
vec![] | ||
} else { | ||
vec![Val::from_js_value(&result)?] | ||
}; | ||
if data.len() != results.len() { | ||
bail!( | ||
"Exported function {} should have {} arguments, but it has {} instead.", | ||
self.function.name().as_string().unwrap_or_default(), | ||
results.len(), | ||
data.len() | ||
); | ||
} | ||
for (target, source) in results.iter_mut().zip(data.into_iter()) { | ||
*target = source; | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It is probably more "correct" to "propagate" the same engine into the
Caller
like this, but theEngine
struct is completely unused and is only there for compatibility with wasmtime, so I think it would just needlessly bloat the code.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.
Engine is used to construct the store in caller, which leads to impl AsContext and AsContextMut for Caller