0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-31 19:44:16 -05:00
denoland-rusty-v8/src/object.rs

75 lines
1.9 KiB
Rust
Raw Normal View History

2019-12-09 19:14:07 -05:00
use std::ops::Deref;
use crate::isolate::CxxIsolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Local;
use crate::Name;
use crate::Value;
/// A JavaScript object (ECMA-262, 4.3.3)
#[repr(C)]
pub struct Object(Opaque);
extern "C" {
fn v8__Object__New(
isolate: *mut CxxIsolate,
prototype_or_null: *mut Value,
names: *mut *mut Name,
values: *mut *mut Value,
length: usize,
) -> *mut Object;
2019-12-19 08:13:33 -05:00
fn v8__Object__GetIsolate(object: &Object) -> &mut CxxIsolate;
2019-12-09 19:14:07 -05:00
}
impl Object {
/// Creates a JavaScript object with the given properties, and
/// a the given prototype_or_null (which can be any JavaScript
/// value, and if it's null, the newly created object won't have
/// a prototype at all). This is similar to Object.create().
/// All properties will be created as enumerable, configurable
/// and writable properties.
pub fn new<'sc>(
scope: &mut HandleScope<'sc>,
mut prototype_or_null: Local<'sc, Value>,
names: Vec<Local<'sc, Name>>,
values: Vec<Local<'sc, Value>>,
length: usize,
) -> Local<'sc, Object> {
let mut names_: Vec<*mut Name> = vec![];
for mut name in names {
let n = &mut *name;
names_.push(n);
}
let mut values_: Vec<*mut Value> = vec![];
for mut value in values {
let n = &mut *value;
values_.push(n);
}
unsafe {
Local::from_raw(v8__Object__New(
scope.cxx_isolate(),
&mut *prototype_or_null,
names_.as_mut_ptr(),
values_.as_mut_ptr(),
length,
))
.unwrap()
}
}
2019-12-19 08:13:33 -05:00
/// Return the isolate to which the Object belongs to.
pub fn get_isolate(&self) -> &mut CxxIsolate {
unsafe { v8__Object__GetIsolate(self) }
}
2019-12-09 19:14:07 -05:00
}
impl Deref for Object {
type Target = Value;
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const _ as *const Value) }
}
}