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

Commit 4f25903

Browse files
author
bors-servo
authored
Auto merge of #306 - servo:vec-convert, r=jdm
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 -->
2 parents 223b907 + 3366341 commit 4f25903

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/conversions.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,14 @@ impl<C: Clone, T: FromJSValConvertible<Config=C>> FromJSValConvertible for Vec<T
585585
let mut iterator = ForOfIteratorGuard::new(cx, &mut iterator);
586586
let iterator = &mut *iterator.root;
587587

588-
if !iterator.init(value, ForOfIterator_NonIterableBehavior::ThrowOnNonIterable) {
588+
if !iterator.init(value, ForOfIterator_NonIterableBehavior::AllowNonIterable) {
589589
return Err(())
590590
}
591591

592+
if iterator.iterator.ptr.is_null() {
593+
return Ok(ConversionResult::Failure("Value is not iterable".into()));
594+
}
595+
592596
let mut ret = vec![];
593597

594598
loop {

tests/vec_conversion.rs

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
extern crate js;
77

88
use js::conversions::ConversionBehavior;
9+
use js::conversions::ConversionResult;
910
use js::conversions::FromJSValConvertible;
1011
use js::conversions::ToJSValConvertible;
1112
use js::jsapi::CompartmentOptions;
@@ -57,5 +58,13 @@ fn vec_conversion() {
5758
ConversionBehavior::Default).unwrap();
5859

5960
assert_eq!(&orig_vec, converted.get_success_value().unwrap());
61+
62+
rt.evaluate_script(global, "({})", "test", 1, rval.handle_mut()).unwrap();
63+
let converted = Vec::<i32>::from_jsval(cx, rval.handle(),
64+
ConversionBehavior::Default);
65+
assert!(match converted {
66+
Ok(ConversionResult::Failure(_)) => true,
67+
_ => false,
68+
});
6069
}
6170
}

0 commit comments

Comments
 (0)