0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-28 16:21:04 -05:00

Simplify Object constructor (#149)

This commit is contained in:
Ry Dahl 2019-12-30 12:14:06 -05:00 committed by GitHub
parent 00d592cd4d
commit 43b3438cb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 14 deletions

View file

@ -335,9 +335,7 @@ two_pointers_t v8__ArrayBuffer__GetBackingStore(v8::ArrayBuffer& self) {
return make_pod<two_pointers_t>(self.GetBackingStore()); return make_pod<two_pointers_t>(self.GetBackingStore());
} }
void* v8__BackingStore__Data(v8::BackingStore& self) { void* v8__BackingStore__Data(v8::BackingStore& self) { return self.Data(); }
return self.Data();
}
size_t v8__BackingStore__ByteLength(v8::BackingStore& self) { size_t v8__BackingStore__ByteLength(v8::BackingStore& self) {
return self.ByteLength(); return self.ByteLength();
@ -382,10 +380,14 @@ int v8__String__WriteUtf8(const v8::String& self, v8::Isolate* isolate,
return self.WriteUtf8(isolate, buffer, length, nchars_ref, options); return self.WriteUtf8(isolate, buffer, length, nchars_ref, options);
} }
v8::Object* v8__Object__New(v8::Isolate* isolate, v8::Object* v8__Object__New(v8::Isolate* isolate) {
v8::Local<v8::Value> prototype_or_null, return local_to_ptr(v8::Object::New(isolate));
v8::Local<v8::Name>* names, }
v8::Local<v8::Value>* values, size_t length) {
v8::Object* v8__Object__New2(v8::Isolate* isolate,
v8::Local<v8::Value> prototype_or_null,
v8::Local<v8::Name>* names,
v8::Local<v8::Value>* values, size_t length) {
return local_to_ptr( return local_to_ptr(
v8::Object::New(isolate, prototype_or_null, names, values, length)); v8::Object::New(isolate, prototype_or_null, names, values, length));
} }
@ -686,7 +688,8 @@ v8::PrimitiveArray* v8__ScriptOrModule__GetHostDefinedOptions(
return local_to_ptr(self.GetHostDefinedOptions()); return local_to_ptr(self.GetHostDefinedOptions());
} }
v8::SharedArrayBuffer* v8__SharedArrayBuffer__New(v8::Isolate* isolate, size_t byte_length) { v8::SharedArrayBuffer* v8__SharedArrayBuffer__New(v8::Isolate* isolate,
size_t byte_length) {
return local_to_ptr(v8::SharedArrayBuffer::New(isolate, byte_length)); return local_to_ptr(v8::SharedArrayBuffer::New(isolate, byte_length));
} }
@ -694,7 +697,8 @@ size_t v8__SharedArrayBuffer__ByteLength(v8::SharedArrayBuffer& self) {
return self.ByteLength(); return self.ByteLength();
} }
two_pointers_t v8__SharedArrayBuffer__GetBackingStore(v8::SharedArrayBuffer& self) { two_pointers_t v8__SharedArrayBuffer__GetBackingStore(
v8::SharedArrayBuffer& self) {
return make_pod<two_pointers_t>(self.GetBackingStore()); return make_pod<two_pointers_t>(self.GetBackingStore());
} }

View file

@ -14,7 +14,8 @@ use crate::Value;
pub struct Object(Opaque); pub struct Object(Opaque);
extern "C" { extern "C" {
fn v8__Object__New( fn v8__Object__New(isolate: *mut Isolate) -> *mut Object;
fn v8__Object__New2(
isolate: *mut Isolate, isolate: *mut Isolate,
prototype_or_null: *mut Value, prototype_or_null: *mut Value,
names: *mut *mut Name, names: *mut *mut Name,
@ -43,19 +44,26 @@ extern "C" {
} }
impl Object { impl Object {
/// Creates an empty object.
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Object> {
let ptr = unsafe { v8__Object__New(scope.isolate()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
/// Creates a JavaScript object with the given properties, and /// Creates a JavaScript object with the given properties, and
/// a the given prototype_or_null (which can be any JavaScript /// a the given prototype_or_null (which can be any JavaScript
/// value, and if it's null, the newly created object won't have /// value, and if it's null, the newly created object won't have
/// a prototype at all). This is similar to Object.create(). /// a prototype at all). This is similar to Object.create().
/// All properties will be created as enumerable, configurable /// All properties will be created as enumerable, configurable
/// and writable properties. /// and writable properties.
pub fn new<'sc>( pub fn new2<'sc>(
scope: &mut impl ToLocal<'sc>, scope: &mut impl ToLocal<'sc>,
mut prototype_or_null: Local<'sc, Value>, mut prototype_or_null: Local<'sc, Value>,
names: Vec<Local<'sc, Name>>, names: Vec<Local<'sc, Name>>,
values: Vec<Local<'sc, Value>>, values: Vec<Local<'sc, Value>>,
length: usize,
) -> Local<'sc, Object> { ) -> Local<'sc, Object> {
let length = names.len();
assert_eq!(length, values.len());
let mut names_: Vec<*mut Name> = vec![]; let mut names_: Vec<*mut Name> = vec![];
for mut name in names { for mut name in names {
let n = &mut *name; let n = &mut *name;
@ -68,7 +76,7 @@ impl Object {
values_.push(n); values_.push(n);
} }
let ptr = unsafe { let ptr = unsafe {
v8__Object__New( v8__Object__New2(
scope.isolate(), scope.isolate(),
&mut *prototype_or_null, &mut *prototype_or_null,
names_.as_mut_ptr(), names_.as_mut_ptr(),

View file

@ -725,8 +725,12 @@ fn object() {
let v1: v8::Local<v8::Value> = v8::Number::new(scope, 1.0).into(); let v1: v8::Local<v8::Value> = v8::Number::new(scope, 1.0).into();
let v2: v8::Local<v8::Value> = v8::Number::new(scope, 2.0).into(); let v2: v8::Local<v8::Value> = v8::Number::new(scope, 2.0).into();
let values = vec![v1, v2]; let values = vec![v1, v2];
let object = v8::Object::new(scope, null, names, values, 2); let object = v8::Object::new2(scope, null, names, values);
assert!(!object.is_null_or_undefined()); assert!(!object.is_null_or_undefined());
let object_ = v8::Object::new(scope);
assert!(!object_.is_null_or_undefined());
context.exit(); context.exit();
} }
drop(locker); drop(locker);