js-sys: Add js_sys::try_iter for iterating over any JsValue

Fixes #776
This commit is contained in:
Nick Fitzgerald
2018-09-25 12:30:04 -07:00
parent e3d2ea2628
commit f9cd329b14
4 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,19 @@
exports.get_iterable = () => ["one", "two", "three"];
exports.get_not_iterable = () => new Object;
exports.get_symbol_iterator_throws = () => ({
[Symbol.iterator]: () => { throw new Error("nope"); },
});
exports.get_symbol_iterator_not_function = () => ({
[Symbol.iterator]: 5,
});
exports.get_symbol_iterator_returns_not_object = () => ({
[Symbol.iterator]: () => 5,
});
exports.get_symbol_iterator_returns_object_without_next = () => ({
[Symbol.iterator]: () => new Object,
});

View File

@ -0,0 +1,36 @@
use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[wasm_bindgen(module = "tests/wasm/Iterator.js")]
extern "C" {
fn get_iterable() -> JsValue;
fn get_not_iterable() -> JsValue;
fn get_symbol_iterator_throws() -> JsValue;
fn get_symbol_iterator_not_function() -> JsValue;
fn get_symbol_iterator_returns_not_object() -> JsValue;
fn get_symbol_iterator_returns_object_without_next() -> JsValue;
}
#[wasm_bindgen_test]
fn try_iter_handles_iteration_protocol() {
assert_eq!(
try_iter(&get_iterable())
.unwrap()
.unwrap()
.map(|x| x.unwrap().as_string().unwrap())
.collect::<Vec<_>>(),
vec!["one", "two", "three"]
);
assert!(try_iter(&get_not_iterable()).unwrap().is_none());
assert!(try_iter(&get_symbol_iterator_throws()).is_err());
assert!(try_iter(&get_symbol_iterator_not_function()).unwrap().is_none());
assert!(try_iter(&get_symbol_iterator_returns_not_object()).unwrap().is_none());
assert!(try_iter(&get_symbol_iterator_returns_object_without_next()).unwrap().is_none());
}

View File

@ -19,6 +19,7 @@ pub mod EvalError;
pub mod Function;
pub mod Generator;
pub mod Intl;
pub mod Iterator;
pub mod JsString;
pub mod JSON;
pub mod Map;