diff --git a/src/binding.cc b/src/binding.cc index fee9962c..69371cfb 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -658,10 +658,9 @@ v8::Object* v8__Object__New(v8::Isolate* isolate) { return local_to_ptr(v8::Object::New(isolate)); } -v8::Object* v8__Object__New2(v8::Isolate* isolate, - v8::Local prototype_or_null, - v8::Local* names, - v8::Local* values, size_t length) { +v8::Object* v8__Object__New__with_prototype_and_properties( + v8::Isolate* isolate, v8::Local prototype_or_null, + v8::Local* names, v8::Local* values, size_t length) { return local_to_ptr( v8::Object::New(isolate, prototype_or_null, names, values, length)); } diff --git a/src/lib.rs b/src/lib.rs index 22923c23..7c62e26c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,7 +162,6 @@ pub use snapshot::OwnedStartupData; pub use snapshot::SnapshotCreator; pub use snapshot::StartupData; pub use string::NewStringType; -pub use support::MaybeBool; pub use support::SharedRef; pub use support::UniquePtr; pub use support::UniqueRef; diff --git a/src/object.rs b/src/object.rs index b89e449e..60550955 100644 --- a/src/object.rs +++ b/src/object.rs @@ -14,42 +14,41 @@ use crate::Value; extern "C" { fn v8__Object__New(isolate: *mut Isolate) -> *mut Object; - fn v8__Object__New2( + fn v8__Object__New__with_prototype_and_properties( isolate: *mut Isolate, - prototype_or_null: *mut Value, - names: *mut *mut Name, - values: *mut *mut Value, + prototype_or_null: Local, + names: *mut Local, + values: *mut Local, length: usize, ) -> *mut Object; fn v8__Object__SetAccessor( self_: &Object, - context: *const Context, - name: *const Name, + context: Local, + name: Local, getter: AccessorNameGetterCallback, ) -> MaybeBool; - fn v8__Object__Get( object: &Object, - context: *const Context, - key: *const Value, + context: Local, + key: Local, ) -> *mut Value; fn v8__Object__Set( object: &Object, - context: *const Context, - key: *const Value, - value: *const Value, + context: Local, + key: Local, + value: Local, ) -> MaybeBool; fn v8__Object__CreateDataProperty( object: &Object, - context: *const Context, - key: *const Name, - value: *const Value, + context: Local, + key: Local, + value: Local, ) -> MaybeBool; fn v8__Object__DefineOwnProperty( object: &Object, - context: *const Context, - key: *const Name, - value: *const Value, + context: Local, + key: Local, + value: Local, attr: PropertyAttribute, ) -> MaybeBool; fn v8__Object__GetIdentityHash(object: &Object) -> int; @@ -70,35 +69,23 @@ impl Object { /// a prototype at all). This is similar to Object.create(). /// All properties will be created as enumerable, configurable /// and writable properties. - pub fn new2<'sc>( + pub fn with_prototype_and_properties<'sc>( scope: &mut impl ToLocal<'sc>, - mut prototype_or_null: Local<'sc, Value>, - names: Vec>, - values: Vec>, + prototype_or_null: Local<'sc, Value>, + names: &[Local], + values: &[Local], ) -> Local<'sc, Object> { - let length = names.len(); - assert_eq!(length, values.len()); - 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); - } - let ptr = unsafe { - v8__Object__New2( + assert_eq!(names.len(), values.len()); + unsafe { + let object = v8__Object__New__with_prototype_and_properties( scope.isolate(), - &mut *prototype_or_null, - names_.as_mut_ptr(), - values_.as_mut_ptr(), - length, - ) - }; - unsafe { scope.to_local(ptr) }.unwrap() + prototype_or_null, + names.as_ptr() as *mut Local, + values.as_ptr() as *mut Local, + names.len(), + ); + scope.to_local(object).unwrap() + } } /// Set only return Just(true) or Empty(), so if it should never fail, use @@ -108,8 +95,8 @@ impl Object { context: Local, key: Local, value: Local, - ) -> MaybeBool { - unsafe { v8__Object__Set(self, &*context, &*key, &*value) } + ) -> Option { + unsafe { v8__Object__Set(self, context, key, value) }.into() } /// Implements CreateDataProperty (ECMA-262, 7.3.4). @@ -124,8 +111,8 @@ impl Object { context: Local, key: Local, value: Local, - ) -> MaybeBool { - unsafe { v8__Object__CreateDataProperty(self, &*context, &*key, &*value) } + ) -> Option { + unsafe { v8__Object__CreateDataProperty(self, context, key, value) }.into() } /// Implements DefineOwnProperty. @@ -140,10 +127,9 @@ impl Object { key: Local, value: Local, attr: PropertyAttribute, - ) -> MaybeBool { - unsafe { - v8__Object__DefineOwnProperty(self, &*context, &*key, &*value, attr) - } + ) -> Option { + unsafe { v8__Object__DefineOwnProperty(self, context, key, value, attr) } + .into() } pub fn get<'a>( @@ -153,7 +139,7 @@ impl Object { key: Local, ) -> Option> { unsafe { - let ptr = v8__Object__Get(self, &*context, &*key); + let ptr = v8__Object__Get(self, context, key); scope.to_local(ptr) } } @@ -164,10 +150,9 @@ impl Object { context: Local, name: Local, getter: impl for<'s> MapFnTo>, - ) -> MaybeBool { - unsafe { - v8__Object__SetAccessor(self, &*context, &*name, getter.map_fn_to()) - } + ) -> Option { + unsafe { v8__Object__SetAccessor(self, context, name, getter.map_fn_to()) } + .into() } /// Returns the identity hash for this object. The current implementation diff --git a/src/support.rs b/src/support.rs index 9581d5d1..f0a03605 100644 --- a/src/support.rs +++ b/src/support.rs @@ -236,7 +236,7 @@ where #[repr(C)] #[derive(Debug, PartialEq)] -pub enum MaybeBool { +pub(crate) enum MaybeBool { JustFalse = 0, JustTrue = 1, Nothing = 2, diff --git a/tests/test_api.rs b/tests/test_api.rs index f052c60e..c0febe08 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1013,15 +1013,16 @@ fn object() { let mut cs = v8::ContextScope::new(scope, context); let scope = cs.enter(); let null: v8::Local = v8::null(scope).into(); - let s1 = v8::String::new(scope, "a").unwrap(); - let s2 = v8::String::new(scope, "b").unwrap(); - let name1 = s1.into(); - let name2 = s2.into(); - let names = vec![name1, name2]; + let n1: v8::Local = v8::String::new(scope, "a").unwrap().into(); + let n2: v8::Local = v8::String::new(scope, "b").unwrap().into(); let v1: v8::Local = v8::Number::new(scope, 1.0).into(); let v2: v8::Local = v8::Number::new(scope, 2.0).into(); - let values = vec![v1, v2]; - let object = v8::Object::new2(scope, null, names, values); + let object = v8::Object::with_prototype_and_properties( + scope, + null, + &[n1, n2], + &[v1, v2], + ); assert!(!object.is_null_or_undefined()); let object_ = v8::Object::new(scope); @@ -1088,18 +1089,14 @@ fn create_data_property() { let obj = obj.to_object(scope).unwrap(); let key = v8_str(scope, "foo"); let value = v8_str(scope, "bar"); - assert_eq!( - obj.create_data_property(context, key.into(), value.into()), - v8::MaybeBool::JustTrue - ); + assert!(obj + .create_data_property(context, key.into(), value.into()) + .unwrap()); let actual = obj.get(scope, context, key.into()).unwrap(); assert!(value.strict_equals(actual)); let key2 = v8_str(scope, "foo2"); - assert_eq!( - obj.set(context, key2.into(), value.into()), - v8::MaybeBool::JustTrue - ); + assert!(obj.set(context, key2.into(), value.into()).unwrap()); let actual = obj.get(scope, context, key2.into()).unwrap(); assert!(value.strict_equals(actual)); } @@ -1893,12 +1890,10 @@ fn shared_array_buffer() { } let global = context.global(scope); - let r = global.create_data_property( - context, - v8_str(scope, "shared").into(), - sab.into(), - ); - assert_eq!(r, v8::MaybeBool::JustTrue); + let r = global + .create_data_property(context, v8_str(scope, "shared").into(), sab.into()) + .unwrap(); + assert!(r); let source = v8::String::new( scope, r"sharedBytes = new Uint8Array(shared);