| 1 |
use wasm_bindgen::prelude::*; |
| 2 |
use wasm_bindgen::JsError; |
| 3 |
use wasm_bindgen_test::*; |
| 4 |
|
| 5 |
use std::fmt; |
| 6 |
|
| 7 |
#[derive(Clone, Debug)] |
| 8 |
enum MyError { |
| 9 |
Variant, |
| 10 |
InflightShouldBeFalse, |
| 11 |
} |
| 12 |
// shouldn't technically need this, surely |
| 13 |
impl std::error::Error for MyError {} |
| 14 |
impl fmt::Display for MyError { |
| 15 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 16 |
write!(f, "MyError::{:?}", self) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
macro_rules! call_test { |
| 21 |
($test_fn:ident, $js_fn:ident) => { |
| 22 |
#[wasm_bindgen_test] |
| 23 |
fn $test_fn() { |
| 24 |
#[wasm_bindgen(module = "tests/wasm/result_jserror.js")] |
| 25 |
extern "C" { |
| 26 |
fn $js_fn(); |
| 27 |
} |
| 28 |
$js_fn(); |
| 29 |
} |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
#[wasm_bindgen] |
| 34 |
pub fn return_err() -> Result<i32, JsError> { |
| 35 |
let e = Err(MyError::Variant)?; |
| 36 |
Ok(e) |
| 37 |
} |
| 38 |
call_test!(test_err, call_err); |
| 39 |
|
| 40 |
#[wasm_bindgen] |
| 41 |
pub fn return_ok() -> Result<i32, JsError> { |
| 42 |
Ok(5) |
| 43 |
} |
| 44 |
call_test!(test_ok, call_ok); |
| 45 |
|
| 46 |
#[wasm_bindgen] |
| 47 |
pub fn make_an_error() -> JsError { |
| 48 |
JsError::new("un-thrown error") |
| 49 |
} |
| 50 |
call_test!(test_make_an_error, call_make_an_error); |
| 51 |
|
| 52 |
#[wasm_bindgen] |
| 53 |
pub struct MyStruct { |
| 54 |
inflight: bool, |
| 55 |
} |
| 56 |
|
| 57 |
call_test!(test_struct, all_struct_methods); |
| 58 |
|
| 59 |
struct ResetOnDrop<'a> { |
| 60 |
flag: &'a mut bool, |
| 61 |
} |
| 62 |
|
| 63 |
impl<'a> Drop for ResetOnDrop<'a> { |
| 64 |
fn drop(&mut self) { |
| 65 |
*self.flag = false; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl<'a> ResetOnDrop<'a> { |
| 70 |
fn new(flag: &'a mut bool) -> Result<Self, MyError> { |
| 71 |
if *flag { |
| 72 |
return Err(MyError::InflightShouldBeFalse); |
| 73 |
} |
| 74 |
Ok(Self { flag }) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[wasm_bindgen] |
| 79 |
impl MyStruct { |
| 80 |
#[wasm_bindgen] |
| 81 |
pub fn new() -> Result<MyStruct, JsError> { |
| 82 |
Ok(MyStruct { inflight: false }) |
| 83 |
} |
| 84 |
|
| 85 |
#[wasm_bindgen] |
| 86 |
pub fn new_err() -> Result<MyStruct, JsError> { |
| 87 |
Err(MyError::Variant.into()) |
| 88 |
} |
| 89 |
|
| 90 |
#[wasm_bindgen] |
| 91 |
pub fn return_ok(&mut self) -> Result<i32, JsError> { |
| 92 |
let _guard = ResetOnDrop::new(&mut self.inflight)?; |
| 93 |
Ok(5) |
| 94 |
} |
| 95 |
|
| 96 |
#[wasm_bindgen] |
| 97 |
pub fn return_err(&mut self) -> Result<i32, JsError> { |
| 98 |
let guard = ResetOnDrop::new(&mut self.inflight)?; |
| 99 |
let err = Err(MyError::Variant); |
| 100 |
let nope = err?; |
| 101 |
// we are checking both for the flag being reset (from js, via is_inflight) |
| 102 |
// and for the running of drop code |
| 103 |
drop(guard); |
| 104 |
Ok(nope) |
| 105 |
} |
| 106 |
|
| 107 |
#[wasm_bindgen] |
| 108 |
pub fn is_inflight(&self) -> bool { |
| 109 |
self.inflight |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// check some more Ok types |
| 114 |
#[wasm_bindgen] |
| 115 |
pub fn jserror_return_string() -> Result<String, JsError> { |
| 116 |
Ok("string here".into()) |
| 117 |
} |
| 118 |
call_test!(test_return_string, call_return_string); |