2019-11-30 10:31:51 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2019-12-20 10:01:45 -05:00
|
|
|
use crate::isolate::Isolate;
|
2019-11-30 10:31:51 -05:00
|
|
|
use crate::support::Opaque;
|
2019-12-20 10:01:45 -05:00
|
|
|
use crate::value::Value;
|
2019-11-30 10:31:51 -05:00
|
|
|
use crate::Local;
|
|
|
|
|
|
|
|
extern "C" {
|
2019-12-20 10:01:45 -05:00
|
|
|
fn v8__Number__New(isolate: *mut Isolate, value: f64) -> *mut Number;
|
2019-11-30 10:31:51 -05:00
|
|
|
fn v8__Number__Value(this: &Number) -> f64;
|
2019-12-20 10:01:45 -05:00
|
|
|
fn v8__Integer__New(isolate: *mut Isolate, value: i32) -> *mut Integer;
|
2019-11-30 10:31:51 -05:00
|
|
|
fn v8__Integer__NewFromUnsigned(
|
2019-12-20 10:01:45 -05:00
|
|
|
isolate: *mut Isolate,
|
2019-11-30 10:31:51 -05:00
|
|
|
value: u32,
|
2019-12-04 08:12:27 -05:00
|
|
|
) -> *mut Integer;
|
2019-12-20 10:01:45 -05:00
|
|
|
fn v8__Integer__Value(this: *const Integer) -> i64;
|
2019-11-30 10:31:51 -05:00
|
|
|
}
|
|
|
|
|
2019-12-19 08:14:19 -05:00
|
|
|
/// A JavaScript number value (ECMA-262, 4.3.20)
|
2019-11-30 10:31:51 -05:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct Number(Opaque);
|
|
|
|
|
|
|
|
impl Number {
|
2019-12-20 10:01:45 -05:00
|
|
|
pub fn new<'sc>(
|
2019-12-22 09:05:39 -05:00
|
|
|
scope: &mut impl AsMut<Isolate>,
|
2019-12-20 10:01:45 -05:00
|
|
|
value: f64,
|
|
|
|
) -> Local<'sc, Number> {
|
2019-11-30 10:31:51 -05:00
|
|
|
unsafe {
|
2019-12-20 10:01:45 -05:00
|
|
|
let local = v8__Number__New(scope.as_mut(), value);
|
2019-11-30 10:31:51 -05:00
|
|
|
Local::from_raw(local).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value(&self) -> f64 {
|
|
|
|
unsafe { v8__Number__Value(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 21:40:54 -05:00
|
|
|
impl Deref for Number {
|
|
|
|
type Target = Value;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
unsafe { &*(self as *const _ as *const Value) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 08:14:19 -05:00
|
|
|
/// A JavaScript value representing a signed integer.
|
2019-11-30 10:31:51 -05:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct Integer(Opaque);
|
|
|
|
|
|
|
|
impl Integer {
|
2019-12-20 10:01:45 -05:00
|
|
|
pub fn new<'sc>(
|
2019-12-22 09:05:39 -05:00
|
|
|
scope: &mut impl AsMut<Isolate>,
|
2019-12-20 10:01:45 -05:00
|
|
|
value: i32,
|
|
|
|
) -> Local<'sc, Integer> {
|
2019-11-30 10:31:51 -05:00
|
|
|
unsafe {
|
2019-12-20 10:01:45 -05:00
|
|
|
let local = v8__Integer__New(scope.as_mut(), value);
|
2019-11-30 10:31:51 -05:00
|
|
|
Local::from_raw(local).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 10:01:45 -05:00
|
|
|
pub fn new_from_unsigned<'sc>(
|
2019-12-22 09:05:39 -05:00
|
|
|
scope: &mut impl AsMut<Isolate>,
|
2019-12-20 10:01:45 -05:00
|
|
|
value: u32,
|
|
|
|
) -> Local<'sc, Integer> {
|
2019-11-30 10:31:51 -05:00
|
|
|
unsafe {
|
2019-12-20 10:01:45 -05:00
|
|
|
let local = v8__Integer__NewFromUnsigned(scope.as_mut(), value);
|
2019-11-30 10:31:51 -05:00
|
|
|
Local::from_raw(local).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value(&self) -> i64 {
|
|
|
|
unsafe { v8__Integer__Value(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Integer {
|
|
|
|
type Target = Number;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
unsafe { &*(self as *const _ as *const Number) }
|
|
|
|
}
|
|
|
|
}
|