diff --git a/src/js.rs b/src/js.rs index e7278b60..a9cdd098 100644 --- a/src/js.rs +++ b/src/js.rs @@ -369,6 +369,19 @@ extern "C" { pub fn to_string(this: &Function) -> JsString; } +// Generator +#[wasm_bindgen] +extern { + pub type Generator; + + /// The next() method returns an object with two properties done and value. + /// You can also provide a parameter to the next method to send a value to the generator. + /// + /// 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; +} + // Map #[wasm_bindgen] extern { diff --git a/tests/all/js_globals/Generator.rs b/tests/all/js_globals/Generator.rs new file mode 100644 index 00000000..f0543f63 --- /dev/null +++ b/tests/all/js_globals/Generator.rs @@ -0,0 +1,51 @@ +#![allow(non_snake_case)] + +use project; + +#[test] +fn next() { + 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 next(this: &js::Generator, value: &JsValue) -> JsValue { + this.next(value) + } + "#, + ) + .file( + "test.ts", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + function* generator() { + const reply = yield '2 * 2'; + + return reply === 4; + } + + const gen = generator(); + + const q = wasm.next(gen, undefined); + + assert.equal(q.value, '2 * 2'); + assert.equal(q.done, false); + + const a = wasm.next(gen, 4); + + assert.equal(a.value, true); + assert.equal(a.done, true); + } + "#, + ) + .test() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 632898e0..8c02811c 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -8,6 +8,7 @@ mod Boolean; mod Date; mod Error; mod Function; +mod Generator; mod JsString; mod Map; mod MapIterator;