mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 10:10:52 +00:00
bindings for Generator.throw()
This commit is contained in:
parent
39d79eb037
commit
b797bbc39c
@ -386,6 +386,13 @@ extern {
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/next
|
||||
#[wasm_bindgen(method, structural)]
|
||||
pub fn next(this: &Generator, value: &JsValue) -> JsValue;
|
||||
|
||||
/// The throw() method resumes the execution of a generator by throwing an error into it
|
||||
/// and returns an object with two properties done and value.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/throw
|
||||
#[wasm_bindgen(method, structural)]
|
||||
pub fn throw(this: &Generator, error: &Error) -> JsValue;
|
||||
}
|
||||
|
||||
// Map
|
||||
|
@ -89,3 +89,49 @@ fn next() {
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn throw() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn gen_throw(this: &js::Generator, error: &js::Error) -> JsValue {
|
||||
this.throw(error)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.ts",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
function* generator() {
|
||||
yield 1;
|
||||
yield 2;
|
||||
}
|
||||
|
||||
const gen = generator();
|
||||
gen.next();
|
||||
|
||||
try {
|
||||
wasm.gen_throw(gen, new Error('Something went wrong'));
|
||||
} catch(err) {
|
||||
assert.equal(err.message, 'Something went wrong');
|
||||
}
|
||||
|
||||
assert.deepEqual(gen.next(), { value: undefined, done: true });
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user