mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-28 14:27:36 +03:00
7cf4213283
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
29 lines
433 B
Rust
29 lines
433 B
Rust
use wasm_bindgen_test::*;
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "tests/wasm/rethrow.js")]
|
|
extern {
|
|
fn call_throw_one();
|
|
fn call_ok();
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn err_works() {
|
|
call_throw_one();
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn throw_one() -> Result<u32, JsValue> {
|
|
Err(1.into())
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn ok_works() {
|
|
call_ok();
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn nothrow() -> Result<u32, JsValue> {
|
|
Ok(1)
|
|
}
|