mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
added catch attribute to the Generator methods, consistent rust keyword name
This commit is contained in:
parent
b797bbc39c
commit
eac2b05b1b
20
src/js.rs
20
src/js.rs
@ -374,25 +374,25 @@ extern "C" {
|
|||||||
extern {
|
extern {
|
||||||
pub type Generator;
|
pub type Generator;
|
||||||
|
|
||||||
/// 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 gen_return(this: &Generator, value: &JsValue) -> JsValue;
|
|
||||||
|
|
||||||
/// The next() method returns an object with two properties done and value.
|
/// 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.
|
/// 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
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/next
|
||||||
#[wasm_bindgen(method, structural)]
|
#[wasm_bindgen(method, structural, catch)]
|
||||||
pub fn next(this: &Generator, value: &JsValue) -> JsValue;
|
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
|
/// 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.
|
/// and returns an object with two properties done and value.
|
||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/throw
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/throw
|
||||||
#[wasm_bindgen(method, structural)]
|
#[wasm_bindgen(method, structural, catch)]
|
||||||
pub fn throw(this: &Generator, error: &Error) -> JsValue;
|
pub fn throw(this: &Generator, error: &Error) -> Result<JsValue, JsValue>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map
|
// Map
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use project;
|
use project;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn gen_return() {
|
fn return_() {
|
||||||
project()
|
project()
|
||||||
.file(
|
.file(
|
||||||
"src/lib.rs",
|
"src/lib.rs",
|
||||||
@ -16,7 +16,7 @@ fn gen_return() {
|
|||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn gen_return(this: &js::Generator, value: &JsValue) -> JsValue {
|
pub fn gen_return(this: &js::Generator, value: &JsValue) -> JsValue {
|
||||||
this.gen_return(value)
|
this.return_(value)
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@ -61,6 +61,13 @@ fn next() {
|
|||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn next(this: &js::Generator, value: &JsValue) -> JsValue {
|
pub fn next(this: &js::Generator, value: &JsValue) -> JsValue {
|
||||||
this.next(value)
|
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()
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@ -84,6 +91,13 @@ fn next() {
|
|||||||
|
|
||||||
const a = wasm.next(gen, 4);
|
const a = wasm.next(gen, 4);
|
||||||
assert.deepEqual(a, { value: true, done: true });
|
assert.deepEqual(a, { value: true, done: true });
|
||||||
|
|
||||||
|
function* brokenGenerator() {
|
||||||
|
throw new Error('Something went wrong');
|
||||||
|
yield 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(wasm.next_throws_error(brokenGenerator(), undefined));
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@ -103,8 +117,8 @@ fn throw() {
|
|||||||
use wasm_bindgen::js;
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn gen_throw(this: &js::Generator, error: &js::Error) -> JsValue {
|
pub fn gen_throws_error(this: &js::Generator, error: &js::Error) -> bool {
|
||||||
this.throw(error)
|
this.throw(error).is_err()
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@ -123,12 +137,7 @@ fn throw() {
|
|||||||
const gen = generator();
|
const gen = generator();
|
||||||
gen.next();
|
gen.next();
|
||||||
|
|
||||||
try {
|
assert(wasm.gen_throws_error(gen, new Error('Something went wrong')));
|
||||||
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 });
|
assert.deepEqual(gen.next(), { value: undefined, done: true });
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
Loading…
Reference in New Issue
Block a user