bindings for Generator.next()

This commit is contained in:
Alexander Kryvomaz 2018-07-03 23:42:49 +03:00
parent 17fde01243
commit 6e95ba20f1
3 changed files with 65 additions and 0 deletions

View File

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

View File

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

View File

@ -8,6 +8,7 @@ mod Boolean;
mod Date;
mod Error;
mod Function;
mod Generator;
mod JsString;
mod Map;
mod MapIterator;