mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 10:10:52 +00:00
bindings for Generator.next()
This commit is contained in:
parent
17fde01243
commit
6e95ba20f1
13
src/js.rs
13
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 {
|
||||
|
51
tests/all/js_globals/Generator.rs
Normal file
51
tests/all/js_globals/Generator.rs
Normal 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()
|
||||
}
|
@ -8,6 +8,7 @@ mod Boolean;
|
||||
mod Date;
|
||||
mod Error;
|
||||
mod Function;
|
||||
mod Generator;
|
||||
mod JsString;
|
||||
mod Map;
|
||||
mod MapIterator;
|
||||
|
Loading…
x
Reference in New Issue
Block a user