mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-01-21 13:31:38 -05:00
Add primitive constructors null, undefined, true, false
This commit is contained in:
parent
e3618f6939
commit
f9a29cf56c
6 changed files with 87 additions and 2 deletions
1
BUILD.gn
1
BUILD.gn
|
@ -15,6 +15,7 @@ v8_static_library("rusty_v8") {
|
|||
"src/number.cc",
|
||||
"src/platform/mod.cc",
|
||||
"src/platform/task.cc",
|
||||
"src/primitives.cc",
|
||||
"src/script.cc",
|
||||
"src/string.cc",
|
||||
]
|
||||
|
|
|
@ -17,6 +17,7 @@ mod isolate;
|
|||
mod local;
|
||||
mod locker;
|
||||
mod number;
|
||||
mod primitives;
|
||||
mod script;
|
||||
mod string;
|
||||
mod support;
|
||||
|
@ -35,6 +36,7 @@ pub use isolate::Isolate;
|
|||
pub use local::Local;
|
||||
pub use locker::Locker;
|
||||
pub use number::{Integer, Number};
|
||||
pub use primitives::*;
|
||||
pub use script::{Script, ScriptOrigin};
|
||||
pub use string::NewStringType;
|
||||
pub use string::String;
|
||||
|
|
22
src/primitives.cc
Normal file
22
src/primitives.cc
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include "support.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
using namespace support;
|
||||
|
||||
extern "C" {
|
||||
v8::Primitive *v8__Null(v8::Isolate *isolate) {
|
||||
return local_to_ptr(v8::Null(isolate));
|
||||
}
|
||||
|
||||
v8::Primitive *v8__Undefined(v8::Isolate *isolate) {
|
||||
return local_to_ptr(v8::Undefined(isolate));
|
||||
}
|
||||
|
||||
v8::Boolean *v8__True(v8::Isolate *isolate) {
|
||||
return local_to_ptr(v8::True(isolate));
|
||||
}
|
||||
|
||||
v8::Boolean *v8__False(v8::Isolate *isolate) {
|
||||
return local_to_ptr(v8::False(isolate));
|
||||
}
|
||||
}
|
42
src/primitives.rs
Normal file
42
src/primitives.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use crate::isolate::CxxIsolate;
|
||||
use crate::isolate::LockedIsolate;
|
||||
use crate::support::Opaque;
|
||||
use crate::HandleScope;
|
||||
use crate::Local;
|
||||
|
||||
/// The superclass of primitive values. See ECMA-262 4.3.2.
|
||||
#[repr(C)]
|
||||
pub struct Primitive(Opaque);
|
||||
|
||||
/// A primitive boolean value (ECMA-262, 4.3.14). Either the true
|
||||
/// or false value.
|
||||
#[repr(C)]
|
||||
pub struct Boolean(Opaque);
|
||||
|
||||
extern "C" {
|
||||
fn v8__Null(isolate: *mut CxxIsolate) -> *mut Primitive;
|
||||
|
||||
fn v8__Undefined(isolate: *mut CxxIsolate) -> *mut Primitive;
|
||||
|
||||
fn v8__True(isolate: *mut CxxIsolate) -> *mut Boolean;
|
||||
|
||||
fn v8__False(isolate: *mut CxxIsolate) -> *mut Boolean;
|
||||
}
|
||||
|
||||
pub fn new_null<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Primitive> {
|
||||
unsafe { Local::from_raw(v8__Null(scope.cxx_isolate())) }.unwrap()
|
||||
}
|
||||
|
||||
pub fn new_undefined<'sc>(
|
||||
scope: &mut HandleScope<'sc>,
|
||||
) -> Local<'sc, Primitive> {
|
||||
unsafe { Local::from_raw(v8__Undefined(scope.cxx_isolate())) }.unwrap()
|
||||
}
|
||||
|
||||
pub fn new_true<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Boolean> {
|
||||
unsafe { Local::from_raw(v8__True(scope.cxx_isolate())) }.unwrap()
|
||||
}
|
||||
|
||||
pub fn new_false<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Boolean> {
|
||||
unsafe { Local::from_raw(v8__False(scope.cxx_isolate())) }.unwrap()
|
||||
}
|
|
@ -85,7 +85,8 @@ impl String {
|
|||
unsafe { v8__String__Length(self) as usize }
|
||||
}
|
||||
|
||||
/// Returns the number of bytes in the UTF-8 encoded representation of this string.
|
||||
/// Returns the number of bytes in the UTF-8 encoded representation of this
|
||||
/// string.
|
||||
pub fn utf8_length(&self, isolate: &mut impl LockedIsolate) -> usize {
|
||||
unsafe { v8__String__Utf8Length(self, isolate.cxx_isolate()) as usize }
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ impl Drop for TestGuard {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn setup() -> TestGuard {
|
||||
fn setup() -> TestGuard {
|
||||
let mut g = INIT_LOCK.lock().unwrap();
|
||||
*g += 1;
|
||||
if *g == 1 {
|
||||
|
@ -179,3 +179,20 @@ fn inspector_string_buffer() {
|
|||
assert_eq!(c1, c2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_primitives() {
|
||||
setup();
|
||||
let mut params = v8::Isolate::create_params();
|
||||
params.set_array_buffer_allocator(
|
||||
v8::array_buffer::Allocator::new_default_allocator(),
|
||||
);
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue