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

Initial implementation for sequence arguments #229

Merged
merged 2 commits into from Dec 30, 2015
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

conversions: Implement FromJSValConvertible for Vec<T>

This will allow us to finally implement WebIDL sequence arguments
(servo/servo#544).
  • Loading branch information
emilio committed Dec 23, 2015
commit bba2c800bcebc5dd90cd7d644d51e31a8d643fff
@@ -30,10 +30,11 @@
use JSPROP_ENUMERATE;
use error::throw_type_error;
use glue::RUST_JS_NumberValue;
use jsapi::{JSContext, JSObject, JSString, HandleValue, MutableHandleValue};
use jsapi::{JSContext, JSObject, JSString, Handle, 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 jsval::{BooleanValue, Int32Value, NullValue, UInt32Value, UndefinedValue};
use jsval::{JSVal, ObjectValue, ObjectOrNullValue, StringValue};
use rust::{ToBoolean, ToNumber, ToUint16, ToInt32, ToUint32, ToInt64, ToUint64, ToString};
@@ -488,6 +489,37 @@ impl<T: ToJSValConvertible> ToJSValConvertible for Vec<T> {
}
}

impl<T: FromJSValConvertible<Config=()>> FromJSValConvertible for Vec<T> {
type Config = ();

unsafe fn from_jsval(cx: *mut JSContext,
value: HandleValue,
option: ())
-> Result<Vec<T>, ()> {
let mut length = 0;

if !value.is_object() {
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);

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)));
}

Ok(ret)
} else {
Err(())
}
}
}

// https://heycam.github.io/webidl/#es-object
impl ToJSValConvertible for *mut JSObject {
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.