0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-02 17:01:55 -05:00
denoland-rusty-v8/src/primitives.rs

74 lines
1.8 KiB
Rust
Raw Normal View History

use std::ops::Deref;
use crate::isolate::Isolate;
use crate::support::Opaque;
use crate::Local;
use crate::ToLocal;
use crate::Value;
/// 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);
2019-12-09 19:14:07 -05:00
/// A superclass for symbols and strings.
#[repr(C)]
pub struct Name(Opaque);
extern "C" {
2019-12-20 10:01:45 -05:00
fn v8__Null(isolate: *mut Isolate) -> *mut Primitive;
2019-12-20 10:01:45 -05:00
fn v8__Undefined(isolate: *mut Isolate) -> *mut Primitive;
2019-12-20 10:01:45 -05:00
fn v8__True(isolate: *mut Isolate) -> *mut Boolean;
2019-12-20 10:01:45 -05:00
fn v8__False(isolate: *mut Isolate) -> *mut Boolean;
}
pub fn new_null<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Primitive> {
let ptr = unsafe { v8__Null(scope.isolate()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
2019-12-20 10:01:45 -05:00
pub fn new_undefined<'sc>(
scope: &mut impl ToLocal<'sc>,
2019-12-20 10:01:45 -05:00
) -> Local<'sc, Primitive> {
let ptr = unsafe { v8__Undefined(scope.isolate()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn new_true<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Boolean> {
let ptr = unsafe { v8__True(scope.isolate()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn new_false<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Boolean> {
let ptr = unsafe { v8__False(scope.isolate()) };
unsafe { scope.to_local(ptr) }.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) }
}
}
2019-12-09 19:14:07 -05:00
impl Deref for Name {
type Target = Primitive;
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const _ as *const Primitive) }
}
}