mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-28 20:21:35 +00:00
Allow returning Result
from functions
This commit adds support for exporting a function defined in Rust that returns a `Result`, translating the `Ok` variant to the actual return value and the `Err` variant to an exception that's thrown in JS. The support for return types and descriptors was rejiggered a bit to be a bit more abstract and more well suited for this purpose. We no longer distinguish between functions with a return value and those without a return value. Additionally a new trait, `ReturnWasmAbi`, is used for converting return values. This trait is an internal implementation detail, however, and shouldn't surface itself to users much (if at all). Closes #841
This commit is contained in:
@ -2,10 +2,12 @@ use core::char;
|
||||
use core::mem::{self, ManuallyDrop};
|
||||
|
||||
use convert::{Stack, FromWasmAbi, IntoWasmAbi, RefFromWasmAbi};
|
||||
use convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
|
||||
use convert::{OptionIntoWasmAbi, OptionFromWasmAbi, ReturnWasmAbi};
|
||||
use convert::traits::WasmAbi;
|
||||
use JsValue;
|
||||
|
||||
unsafe impl WasmAbi for () {}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct WasmOptionalI32 {
|
||||
pub present: u32,
|
||||
@ -370,3 +372,22 @@ impl<T: OptionFromWasmAbi> FromWasmAbi for Option<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoWasmAbi for () {
|
||||
type Abi = ();
|
||||
|
||||
fn into_abi(self, _extra: &mut Stack) -> () {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: IntoWasmAbi> ReturnWasmAbi for Result<T, JsValue> {
|
||||
type Abi = T::Abi;
|
||||
|
||||
fn return_abi(self, extra: &mut Stack) -> Self::Abi {
|
||||
match self {
|
||||
Ok(v) => v.into_abi(extra),
|
||||
Err(e) => ::throw_val(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user