Merge pull request #380 from akryvomaz/master

bindings for Generator.next(), Generator.return() and Generator.throw()
This commit is contained in:
Nick Fitzgerald 2018-07-03 16:30:46 -07:00 committed by GitHub
commit fbbd97bbe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 173 additions and 0 deletions

View File

@ -369,6 +369,32 @@ 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, catch)]
pub fn next(this: &Generator, value: &JsValue) -> Result<JsValue, JsValue>;
/// The return() method returns the given value and finishes the generator.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return
#[wasm_bindgen(method, structural, js_name = return)]
pub fn return_(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, catch)]
pub fn throw(this: &Generator, error: &Error) -> Result<JsValue, JsValue>;
}
// Map
#[wasm_bindgen]
extern {

View File

@ -0,0 +1,146 @@
#![allow(non_snake_case)]
use project;
#[test]
fn return_() {
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_return(this: &js::Generator, value: &JsValue) -> JsValue {
this.return_(value)
}
"#,
)
.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();
const res = wasm.gen_return(gen, 42);
assert.deepEqual(res, { value: 42, done: true });
const next = gen.next();
assert.deepEqual(next, { value: undefined, done: true });
}
"#,
)
.test()
}
#[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)
.ok()
.expect("generator throws an error")
}
#[wasm_bindgen]
pub fn next_throws_error(this: &js::Generator, value: &JsValue) -> bool {
this.next(value).is_err()
}
"#,
)
.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.deepEqual(q, { value: '2 * 2', done: false });
const a = wasm.next(gen, 4);
assert.deepEqual(a, { value: true, done: true });
function* brokenGenerator() {
throw new Error('Something went wrong');
yield 1;
}
assert(wasm.next_throws_error(brokenGenerator(), undefined));
}
"#,
)
.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_throws_error(this: &js::Generator, error: &js::Error) -> bool {
this.throw(error).is_err()
}
"#,
)
.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();
assert(wasm.gen_throws_error(gen, new Error('Something went wrong')));
assert.deepEqual(gen.next(), { value: undefined, 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;