Skip to content

Commit

Permalink
chore: optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Mar 15, 2022
1 parent 4dd9b3c commit 419e986
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
6 changes: 2 additions & 4 deletions wasmy-vm/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ impl WasmCaller {
/// Call the wasm specified method.
pub fn call<A: Message, R: Message>(&self, method: Method, data: A) -> Result<R> {
let in_args = InArgs::try_new(method, data)?;
instance::with(self.0.clone(), |ins| -> Result<R> {
ins.handle_wasm(method, in_args)?.into()
})
instance::with(self.0.clone(), |ins| -> Result<R> { ins.handle_wasm(in_args)?.into() })
}
/// Carry the context to call the wasm specified method.
pub fn ctx_call<C: Message, A: Message, R: Message>(
Expand All @@ -65,7 +63,7 @@ impl WasmCaller {
) -> Result<R> {
let in_args = InArgs::try_new(method, data)?;
instance::with(self.0.clone(), |ins| -> Result<R> {
ins.ctx_handle_wasm(ctx, method, in_args)?.into()
ins.ctx_handle_wasm(ctx, in_args)?.into()
})
}
// // Execute the raw call to wasm.
Expand Down
6 changes: 3 additions & 3 deletions wasmy-vm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ fn handle(ctx_ptr: usize, args: InArgs) -> OutRets {
pub(crate) struct WasmHandlerApi();

impl WasmHandlerApi {
pub const fn onload_symbol() -> &'static str {
pub(crate) const fn onload_symbol() -> &'static str {
"_wasmy_wasm_onload"
}
pub fn method_to_symbol(method: WasmMethod) -> String {
pub(crate) fn method_to_symbol(method: WasmMethod) -> String {
format!("_wasmy_wasm_handle_{}", method)
}
pub fn symbol_to_method(symbol: &str) -> Option<WasmMethod> {
pub(crate) fn symbol_to_method(symbol: &str) -> Option<WasmMethod> {
if let Some(s) = symbol.strip_prefix("_wasmy_wasm_handle_") { s.parse().ok() } else { None }
}
}
Expand Down
12 changes: 5 additions & 7 deletions wasmy-vm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,30 +333,28 @@ impl Instance {
}
}
#[inline]
pub fn handle_wasm(&self, method: Method, in_args: InArgs) -> Result<OutRets> {
self.inner_handle_wasm(None::<Empty>, method, in_args)
pub(crate) fn handle_wasm(&self, in_args: InArgs) -> Result<OutRets> {
self.inner_handle_wasm(None::<Empty>, in_args)
}
#[inline]
pub fn ctx_handle_wasm<C: Message>(
pub(crate) fn ctx_handle_wasm<C: Message>(
&self,
ctx_value: C,
method: Method,
in_args: InArgs,
) -> Result<OutRets> {
self.inner_handle_wasm(Some(ctx_value), method, in_args)
self.inner_handle_wasm(Some(ctx_value), in_args)
}
#[inline]
fn inner_handle_wasm<C: Message>(
&self,
ctx_value: Option<C>,
method: Method,
in_args: InArgs,
) -> Result<OutRets> {
self.check_loaded()?;
#[cfg(debug_assertions)]
println!("method={}, data={:?}", in_args.get_method(), in_args.get_data());
let sign_name = WasmHandlerApi::method_to_symbol(in_args.get_method());
let (ctx_size, args_size) = self.context.borrow_mut().set_args(ctx_value.as_ref(), in_args);
let sign_name = WasmHandlerApi::method_to_symbol(method);
self.raw_call_wasm(
sign_name.as_str(),
&[Val::I32(ctx_size as i32), Val::I32(args_size as i32)],
Expand Down

0 comments on commit 419e986

Please sign in to comment.