mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-02-02 04:37:35 -05:00
add v8::Array::new() (#165)
This commit is contained in:
parent
53fd83a6fa
commit
7b139afbc4
3 changed files with 52 additions and 0 deletions
|
@ -603,6 +603,10 @@ v8::Isolate* v8__Object__GetIsolate(v8::Object& self) {
|
|||
return self.GetIsolate();
|
||||
}
|
||||
|
||||
v8::Array* v8__Array__New(v8::Isolate* isolate, int length) {
|
||||
return local_to_ptr(v8::Array::New(isolate, length));
|
||||
}
|
||||
|
||||
v8::Number* v8__Number__New(v8::Isolate* isolate, double value) {
|
||||
return *v8::Number::New(isolate, value);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use crate::isolate::Isolate;
|
||||
use crate::support::int;
|
||||
use crate::support::MaybeBool;
|
||||
use crate::Array;
|
||||
use crate::Context;
|
||||
use crate::Local;
|
||||
use crate::Name;
|
||||
|
@ -35,6 +37,8 @@ extern "C" {
|
|||
key: *const Name,
|
||||
value: *const Value,
|
||||
) -> MaybeBool;
|
||||
|
||||
fn v8__Array__New(isolate: *mut Isolate, length: int) -> *mut Array;
|
||||
}
|
||||
|
||||
impl Object {
|
||||
|
@ -125,3 +129,15 @@ impl Object {
|
|||
unsafe { v8__Object__GetIsolate(self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Array {
|
||||
/// Creates a JavaScript array with the given length. If the length
|
||||
/// is negative the returned array will have length 0.
|
||||
pub fn new<'sc>(
|
||||
scope: &mut impl ToLocal<'sc>,
|
||||
length: i32,
|
||||
) -> Local<'sc, Array> {
|
||||
let ptr = unsafe { v8__Array__New(scope.isolate(), length) };
|
||||
unsafe { scope.to_local(ptr) }.unwrap()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -833,6 +833,38 @@ fn object() {
|
|||
drop(locker);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array() {
|
||||
setup();
|
||||
let mut params = v8::Isolate::create_params();
|
||||
params.set_array_buffer_allocator(v8::new_default_allocator());
|
||||
let isolate = v8::Isolate::new(params);
|
||||
let mut locker = v8::Locker::new(&isolate);
|
||||
{
|
||||
let mut hs = v8::HandleScope::new(&mut locker);
|
||||
let s = hs.enter();
|
||||
let mut context = v8::Context::new(s);
|
||||
context.enter();
|
||||
let s1 = v8::String::new(s, "a").unwrap();
|
||||
let index1 = v8::Integer::new(s, 0);
|
||||
let s2 = v8::String::new(s, "b").unwrap();
|
||||
let index2 = v8::Integer::new(s, 1);
|
||||
let array = v8::Array::new(s, 2);
|
||||
array.set(context, index1.into(), s1.into());
|
||||
array.set(context, index2.into(), s2.into());
|
||||
|
||||
let maybe_v1 = array.get(s, context, index1.into());
|
||||
assert!(maybe_v1.is_some());
|
||||
assert!(maybe_v1.unwrap().same_value(s1.into()));
|
||||
let maybe_v2 = array.get(s, context, index2.into());
|
||||
assert!(maybe_v2.is_some());
|
||||
assert!(maybe_v2.unwrap().same_value(s2.into()));
|
||||
|
||||
context.exit();
|
||||
}
|
||||
drop(locker);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_data_property() {
|
||||
setup();
|
||||
|
|
Loading…
Add table
Reference in a new issue