Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
Auto merge of #306 - servo:vec-convert, r=jdm
Browse files Browse the repository at this point in the history
Return a ConversionResult::Failure when converting a non-iterable value to Vec.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/306)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Sep 19, 2016
2 parents 223b907 + 3366341 commit 4f25903
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,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, ForOfIterator_NonIterableBehavior::ThrowOnNonIterable) {
if !iterator.init(value, 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 tests/vec_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
extern crate js;

use js::conversions::ConversionBehavior;
use js::conversions::ConversionResult;
use js::conversions::FromJSValConvertible;
use js::conversions::ToJSValConvertible;
use js::jsapi::CompartmentOptions;
Expand Down Expand Up @@ -57,5 +58,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 4f25903

Please sign in to comment.