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 all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -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) {
@@ -63,6 +63,9 @@ pub mod glue;
pub mod jsval;
pub mod rust;

#[cfg(test)]
mod tests;

pub use consts::*;

use heapsize::HeapSizeOf;
@@ -0,0 +1,61 @@

use consts::{JSCLASS_RESERVED_SLOTS_MASK,JSCLASS_RESERVED_SLOTS_SHIFT,JSCLASS_GLOBAL_SLOT_COUNT,JSCLASS_IS_GLOBAL};
use jsapi::JS_GlobalObjectTraceHook;
use conversions::*;
use jsval::*;
use rust::*;
use jsapi::{CallArgs,CompartmentOptions,OnNewGlobalHookOption,Rooted,Value, JS_NewGlobalObject};
use jsapi::{RootedValue, RootedObject, JSAutoRequest, JSAutoCompartment, JS_Init, JSClass};
use std::ptr;

static CLASS: &'static JSClass = &JSClass {
name: b"test\0" as *const u8 as *const _,
flags: JSCLASS_IS_GLOBAL | ((JSCLASS_GLOBAL_SLOT_COUNT & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT),
addProperty: None,
delProperty: None,
getProperty: None,
setProperty: None,
enumerate: None,
resolve: None,
convert: None,
finalize: None,
call: None,
hasInstance: None,
construct: None,
trace: Some(JS_GlobalObjectTraceHook),
reserved: [0 as *mut _; 25]
};


#[test]
fn test_vec_conversion() {
unsafe{
assert!(JS_Init());
}

let rt = Runtime::new();
let cx = rt.cx();

let glob = RootedObject::new(cx, 0 as *mut _);
let _ar = JSAutoRequest::new(cx);

let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = CompartmentOptions::default();
let global = unsafe {
JS_NewGlobalObject(cx, CLASS, ptr::null_mut(), h_option, &c_option)
};
let global_root = Rooted::new(cx, global);
let global = global_root.handle();

let _ac = JSAutoCompartment::new(cx, global.get());

let orig_vec: Vec<f32> = vec![1.0, 2.9, 3.0];
let mut rval = RootedValue::new(cx, UndefinedValue());

let converted = unsafe {
orig_vec.to_jsval(cx, rval.handle_mut());
Vec::<f32>::from_jsval(cx, rval.handle(), ()).unwrap()
};

assert_eq!(orig_vec, converted);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.