Skip to content

Commit

Permalink
Return a ConversionResult::Failure when converting a non-iterable val…
Browse files Browse the repository at this point in the history
…ue to Vec.
  • Loading branch information
Ms2ger authored and jdm committed Dec 18, 2017
1 parent cafdb83 commit f40d7bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion mozjs/js/rust/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,14 @@ impl<C: Clone, T: FromJSValConvertible<Config=C>> FromJSValConvertible for Vec<T
let mut iterator = ForOfIteratorGuard::new(cx, &mut iterator);
let iterator = &mut *iterator.root;

if !iterator.init(value, JS::ForOfIterator_NonIterableBehavior::ThrowOnNonIterable) {
if !iterator.init(value, JS::ForOfIterator_NonIterableBehavior::AllowNonIterable) {
return Err(())
}

if iterator.iterator.ptr.is_null() {
return Ok(ConversionResult::Failure("Value is not iterable".into()));
}

let mut ret = vec![];

loop {
Expand Down
9 changes: 9 additions & 0 deletions mozjs/js/rust/tests/vec_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate js;

use js::ac::AutoCompartment;
use js::conversions::ConversionBehavior;
use js::conversions::ConversionResult;
use js::conversions::FromJSValConvertible;
use js::conversions::ToJSValConvertible;
use js::jsapi::root::JS::CompartmentOptions;
Expand Down Expand Up @@ -67,5 +68,13 @@ fn vec_conversion() {
ConversionBehavior::Default).unwrap();

assert_eq!(&orig_vec, converted.get_success_value().unwrap());

rt.evaluate_script(global, "({})", "test", 1, rval.handle_mut()).unwrap();
let converted = Vec::<i32>::from_jsval(cx, rval.handle(),
ConversionBehavior::Default);
assert!(match converted {
Ok(ConversionResult::Failure(_)) => true,
_ => false,
});
}
}

0 comments on commit f40d7bb

Please sign in to comment.