mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-22 15:07:00 -05:00
Add Object::create_data_property, Object::get, Value::is_array, Value::is_object, Value::is_function (#129)
This commit is contained in:
parent
b00ef1ea3c
commit
d65c604f3a
6 changed files with 119 additions and 1 deletions
|
@ -216,6 +216,12 @@ bool v8__Value__IsString(const v8::Value& self) { return self.IsString(); }
|
|||
|
||||
bool v8__Value__IsNumber(const v8::Value& self) { return self.IsNumber(); }
|
||||
|
||||
bool v8__Value__IsObject(const v8::Value& self) { return self.IsObject(); }
|
||||
|
||||
bool v8__Value__IsArray(const v8::Value& self) { return self.IsArray(); }
|
||||
|
||||
bool v8__Value__IsFunction(const v8::Value& self) { return self.IsFunction(); }
|
||||
|
||||
bool v8__Value__StrictEquals(const v8::Value& self, v8::Value* that) {
|
||||
return self.StrictEquals(ptr_to_local(that));
|
||||
}
|
||||
|
@ -301,6 +307,18 @@ v8::Object* v8__Object__New(v8::Isolate* isolate,
|
|||
v8::Object::New(isolate, prototype_or_null, names, values, length));
|
||||
}
|
||||
|
||||
v8::Value* v8__Object__Get(v8::Object& self, v8::Local<v8::Context> context,
|
||||
v8::Local<v8::Value> key) {
|
||||
return maybe_local_to_ptr(self.Get(context, key));
|
||||
}
|
||||
|
||||
MaybeBool v8__Object__CreateDataProperty(v8::Object& self,
|
||||
v8::Local<v8::Context> context,
|
||||
v8::Local<v8::Name> key,
|
||||
v8::Local<v8::Value> value) {
|
||||
return maybe_to_maybe_bool(self.CreateDataProperty(context, key, value));
|
||||
}
|
||||
|
||||
v8::Isolate* v8__Object__GetIsolate(v8::Object& self) {
|
||||
return self.GetIsolate();
|
||||
}
|
||||
|
|
|
@ -72,5 +72,7 @@ pub use script_or_module::ScriptOrModule;
|
|||
pub use snapshot::{FunctionCodeHandling, SnapshotCreator, StartupData};
|
||||
pub use string::NewStringType;
|
||||
pub use string::String;
|
||||
pub use support::MaybeBool;
|
||||
pub use support::UniqueRef;
|
||||
pub use try_catch::{TryCatch, TryCatchScope};
|
||||
pub use value::Value;
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
use crate::isolate::Isolate;
|
||||
use crate::support::MaybeBool;
|
||||
use crate::support::Opaque;
|
||||
use crate::Context;
|
||||
use crate::HandleScope;
|
||||
use crate::Local;
|
||||
use crate::Name;
|
||||
|
@ -20,6 +22,19 @@ extern "C" {
|
|||
length: usize,
|
||||
) -> *mut Object;
|
||||
fn v8__Object__GetIsolate(object: &Object) -> &mut Isolate;
|
||||
|
||||
fn v8__Object__Get(
|
||||
object: &Object,
|
||||
context: *const Context,
|
||||
key: *const Value,
|
||||
) -> *mut Value;
|
||||
|
||||
fn v8__Object__CreateDataProperty(
|
||||
object: &Object,
|
||||
context: *const Context,
|
||||
key: *const Name,
|
||||
value: *const Value,
|
||||
) -> MaybeBool;
|
||||
}
|
||||
|
||||
impl Object {
|
||||
|
@ -59,6 +74,37 @@ impl Object {
|
|||
}
|
||||
}
|
||||
|
||||
/// Implements CreateDataProperty (ECMA-262, 7.3.4).
|
||||
///
|
||||
/// Defines a configurable, writable, enumerable property with the given value
|
||||
/// on the object unless the property already exists and is not configurable
|
||||
/// or the object is not extensible.
|
||||
///
|
||||
/// Returns true on success.
|
||||
pub fn create_data_property(
|
||||
&self,
|
||||
context: Local<Context>,
|
||||
key: Local<Name>,
|
||||
value: Local<Value>,
|
||||
) -> MaybeBool {
|
||||
unsafe { v8__Object__CreateDataProperty(self, &*context, &*key, &*value) }
|
||||
}
|
||||
|
||||
pub fn get<'a>(
|
||||
&self,
|
||||
context: Local<Context>,
|
||||
key: Local<Value>,
|
||||
) -> Option<Local<'a, Value>> {
|
||||
unsafe {
|
||||
let ptr = v8__Object__Get(self, &*context, &*key);
|
||||
if ptr.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(Local::from_raw(ptr).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the isolate to which the Object belongs to.
|
||||
pub fn get_isolate(&self) -> &Isolate {
|
||||
unsafe { v8__Object__GetIsolate(self) }
|
||||
|
|
|
@ -138,7 +138,7 @@ where
|
|||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum MaybeBool {
|
||||
JustFalse = 0,
|
||||
JustTrue = 1,
|
||||
|
|
19
src/value.rs
19
src/value.rs
|
@ -7,6 +7,9 @@ extern "C" {
|
|||
fn v8__Value__IsNullOrUndefined(this: &Value) -> bool;
|
||||
fn v8__Value__IsString(this: &Value) -> bool;
|
||||
fn v8__Value__IsNumber(this: &Value) -> bool;
|
||||
fn v8__Value__IsArray(this: &Value) -> bool;
|
||||
fn v8__Value__IsFunction(this: &Value) -> bool;
|
||||
fn v8__Value__IsObject(this: &Value) -> bool;
|
||||
fn v8__Value__StrictEquals(this: &Value, that: &Value) -> bool;
|
||||
fn v8__Value__SameValue(this: &Value, that: &Value) -> bool;
|
||||
}
|
||||
|
@ -38,6 +41,22 @@ impl Value {
|
|||
unsafe { v8__Value__IsString(self) }
|
||||
}
|
||||
|
||||
/// Returns true if this value is an array. Note that it will return false for
|
||||
/// an Proxy for an array.
|
||||
pub fn is_array(&self) -> bool {
|
||||
unsafe { v8__Value__IsArray(self) }
|
||||
}
|
||||
|
||||
/// Returns true if this value is a function.
|
||||
pub fn is_function(&self) -> bool {
|
||||
unsafe { v8__Value__IsFunction(self) }
|
||||
}
|
||||
|
||||
/// Returns true if this value is an object.
|
||||
pub fn is_object(&self) -> bool {
|
||||
unsafe { v8__Value__IsObject(self) }
|
||||
}
|
||||
|
||||
/// Returns true if this value is a number.
|
||||
pub fn is_number(&self) -> bool {
|
||||
unsafe { v8__Value__IsNumber(self) }
|
||||
|
|
|
@ -577,6 +577,39 @@ fn object() {
|
|||
drop(locker);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_data_property() {
|
||||
setup();
|
||||
let mut params = v8::Isolate::create_params();
|
||||
params.set_array_buffer_allocator(v8::Allocator::new_default_allocator());
|
||||
let isolate = v8::Isolate::new(params);
|
||||
let mut locker = v8::Locker::new(&isolate);
|
||||
v8::HandleScope::enter(&mut locker, |scope| {
|
||||
let mut context = v8::Context::new(scope);
|
||||
context.enter();
|
||||
|
||||
eval(scope, context, "var a = {};");
|
||||
|
||||
let obj = context
|
||||
.global()
|
||||
.get(context, v8_str(scope, "a").into())
|
||||
.unwrap();
|
||||
assert!(obj.is_object());
|
||||
let obj: Local<v8::Object> = cast(obj);
|
||||
let key = v8_str(scope, "foo");
|
||||
let value = v8_str(scope, "bar");
|
||||
assert_eq!(
|
||||
obj.create_data_property(context, cast(key), cast(value)),
|
||||
v8::MaybeBool::JustTrue
|
||||
);
|
||||
let actual = obj.get(context, cast(key)).unwrap();
|
||||
assert!(value.strict_equals(actual));
|
||||
|
||||
context.exit();
|
||||
});
|
||||
drop(locker);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn promise_resolved() {
|
||||
setup();
|
||||
|
|
Loading…
Reference in a new issue