bindings for Generator.throw()

This commit is contained in:
Alexander Kryvomaz 2018-07-03 23:48:54 +03:00
parent 39d79eb037
commit b797bbc39c
2 changed files with 53 additions and 0 deletions

View File

@ -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

View File

@ -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()
}