Add Value::{is_undefined, is_null, is_null_or_undefined}

This commit is contained in:
Ryan Dahl 2019-12-06 09:36:34 -05:00 committed by Ry Dahl
parent f9a29cf56c
commit 7a233e277e
6 changed files with 84 additions and 7 deletions

View File

@ -18,6 +18,7 @@ v8_static_library("rusty_v8") {
"src/primitives.cc",
"src/script.cc",
"src/string.cc",
"src/value.cc",
]
deps = [
":v8",

View File

@ -21,6 +21,7 @@ mod primitives;
mod script;
mod string;
mod support;
mod value;
pub mod array_buffer;
pub mod inspector;
@ -40,6 +41,4 @@ pub use primitives::*;
pub use script::{Script, ScriptOrigin};
pub use string::NewStringType;
pub use string::String;
#[repr(C)]
pub struct Value(support::Opaque);
pub use value::Value;

View File

@ -1,8 +1,11 @@
use std::ops::Deref;
use crate::isolate::CxxIsolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Local;
use crate::Value;
/// The superclass of primitive values. See ECMA-262 4.3.2.
#[repr(C)]
@ -40,3 +43,17 @@ pub fn new_true<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Boolean> {
pub fn new_false<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Boolean> {
unsafe { Local::from_raw(v8__False(scope.cxx_isolate())) }.unwrap()
}
impl Deref for Primitive {
type Target = Value;
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const _ as *const Value) }
}
}
impl Deref for Boolean {
type Target = Primitive;
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const _ as *const Primitive) }
}
}

17
src/value.cc Normal file
View File

@ -0,0 +1,17 @@
#include <cstdint>
#include "support.h"
#include "v8/include/v8.h"
using namespace v8;
using namespace support;
extern "C" {
bool v8__Value__IsUndefined(const Value &self) { return self.IsUndefined(); }
bool v8__Value__IsNull(const Value &self) { return self.IsNull(); }
bool v8__Value__IsNullOrUndefined(const Value &self) {
return self.IsNullOrUndefined();
}
}

28
src/value.rs Normal file
View File

@ -0,0 +1,28 @@
use crate::support;
extern "C" {
fn v8__Value__IsUndefined(this: &Value) -> bool;
fn v8__Value__IsNull(this: &Value) -> bool;
fn v8__Value__IsNullOrUndefined(this: &Value) -> bool;
}
#[repr(C)]
pub struct Value(support::Opaque);
impl Value {
/// Returns true if this value is the undefined value. See ECMA-262 4.3.10.
pub fn is_undefined(&self) -> bool {
unsafe { v8__Value__IsUndefined(self) }
}
/// Returns true if this value is the null value. See ECMA-262 4.3.11.
pub fn is_null(&self) -> bool {
unsafe { v8__Value__IsNull(self) }
}
/// Returns true if this value is either the null or the undefined value.
/// See ECMA-262 4.3.11. and 4.3.12
pub fn is_null_or_undefined(&self) -> bool {
unsafe { v8__Value__IsNullOrUndefined(self) }
}
}

View File

@ -190,9 +190,24 @@ fn test_primitives() {
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let _null = v8::new_null(scope);
let _undefined = v8::new_undefined(scope);
let _true = v8::new_true(scope);
let _false = v8::new_false(scope);
let null = v8::new_null(scope);
assert!(!null.is_undefined());
assert!(null.is_null());
assert!(null.is_null_or_undefined());
let undefined = v8::new_undefined(scope);
assert!(undefined.is_undefined());
assert!(!undefined.is_null());
assert!(undefined.is_null_or_undefined());
let true_ = v8::new_true(scope);
assert!(!true_.is_undefined());
assert!(!true_.is_null());
assert!(!true_.is_null_or_undefined());
let false_ = v8::new_false(scope);
assert!(!false_.is_undefined());
assert!(!false_.is_null());
assert!(!false_.is_null_or_undefined());
});
}