Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conversions: Implement FromJSValConvertible for Vec<T> #226

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

[wip] Try to generalise it using ForOfIterator

This actually fails with a trap (at the assertion in `Rooted<T>::drop`).
  • Loading branch information
emilio committed Dec 30, 2015
commit 4c04a596f4b87a21c11fedc4489c16d3b3437cec
@@ -30,11 +30,11 @@
use JSPROP_ENUMERATE;
use error::throw_type_error;
use glue::RUST_JS_NumberValue;
use jsapi::{JSContext, JSObject, JSString, Handle, HandleValue, MutableHandleValue};
use jsapi::{JSContext, JSObject, JSString, HandleValue, MutableHandleValue};
use jsapi::{JS_NewUCStringCopyN, JS_StringHasLatin1Chars, JS_WrapValue};
use jsapi::{JS_GetLatin1StringCharsAndLength, JS_GetTwoByteStringCharsAndLength};
use jsapi::{JS_NewArrayObject1, JS_DefineElement, RootedValue, RootedObject};
use jsapi::{JS_GetArrayLength, JS_GetElement};
use jsapi::{ForOfIterator, NonIterableBehavior};
use jsval::{BooleanValue, Int32Value, NullValue, UInt32Value, UndefinedValue};
use jsval::{JSVal, ObjectValue, ObjectOrNullValue, StringValue};
use rust::{ToBoolean, ToNumber, ToUint16, ToInt32, ToUint32, ToInt64, ToUint64, ToString};
@@ -496,27 +496,33 @@ impl<T: FromJSValConvertible<Config=()>> FromJSValConvertible for Vec<T> {
value: HandleValue,
option: ())
-> Result<Vec<T>, ()> {
let mut length = 0;
let mut iterator = ForOfIterator {
cx_: cx,
iterator: RootedObject::new(cx, ptr::null_mut()),
index: ::std::u32::MAX, // NOT_ARRAY
};

if !value.is_object() {
if !iterator.init(value, NonIterableBehavior::AllowNonIterable) {
return Err(())
}

// HandleValue -> HandleObject
let handle_obj = Handle::from_marked_location(&value.to_object());
if JS_GetArrayLength(cx, handle_obj, &mut length) {
let mut ret = Vec::with_capacity(length as usize);
let mut ret = vec![];

for i in 0..length {
let mut val = RootedValue::new(cx, UndefinedValue());
assert!(JS_GetElement(cx, handle_obj, i, val.handle_mut()));
ret.push(try!(T::from_jsval(cx, val.handle(), option)));
loop {
let mut done = false;
let mut val = RootedValue::new(cx, UndefinedValue());
if !iterator.next(val.handle_mut(), &mut done) {
return Err(())
}

Ok(ret)
} else {
Err(())
if done {
break;
}

ret.push(try!(T::from_jsval(cx, val.handle(), option)));
}

Ok(ret)
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.