0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-23 07:29:56 -05:00

Add Value::{number_value, integer_value, uint32_value, int32_value}

#174
This commit is contained in:
Ry Dahl 2020-01-03 12:17:11 -05:00 committed by GitHub
parent 8adf85ea89
commit a4f519c643
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 0 deletions

View file

@ -500,6 +500,26 @@ v8::Object* v8__Value__ToObject(const v8::Value& self, v8::Context* context) {
return maybe_local_to_ptr(self.ToObject(ptr_to_local(context))); return maybe_local_to_ptr(self.ToObject(ptr_to_local(context)));
} }
void v8__Value__NumberValue(const v8::Value& self, v8::Context* context,
v8::Maybe<double>* out) {
*out = self.NumberValue(ptr_to_local(context));
}
void v8__Value__IntegerValue(const v8::Value& self, v8::Context* context,
v8::Maybe<int64_t>* out) {
*out = self.IntegerValue(ptr_to_local(context));
}
void v8__Value__Uint32Value(const v8::Value& self, v8::Context* context,
v8::Maybe<uint32_t>* out) {
*out = self.Uint32Value(ptr_to_local(context));
}
void v8__Value__Int32Value(const v8::Value& self, v8::Context* context,
v8::Maybe<int32_t>* out) {
*out = self.Int32Value(ptr_to_local(context));
}
v8::Primitive* v8__Null(v8::Isolate* isolate) { v8::Primitive* v8__Null(v8::Isolate* isolate) {
return local_to_ptr(v8::Null(isolate)); return local_to_ptr(v8::Null(isolate));
} }

View file

@ -260,3 +260,20 @@ impl<F> FieldOffset<F> {
.unwrap() .unwrap()
} }
} }
#[repr(C)]
#[derive(Default)]
pub struct Maybe<T> {
has_value: bool,
value: T,
}
impl<T> Into<Option<T>> for Maybe<T> {
fn into(self) -> Option<T> {
if self.has_value {
Some(self.value)
} else {
None
}
}
}

View file

@ -1,3 +1,4 @@
use crate::support::Maybe;
use crate::BigInt; use crate::BigInt;
use crate::Context; use crate::Context;
use crate::Int32; use crate::Int32;
@ -80,6 +81,27 @@ extern "C" {
fn v8__Value__ToInteger(this: &Value, context: *mut Context) -> *mut Integer; fn v8__Value__ToInteger(this: &Value, context: *mut Context) -> *mut Integer;
fn v8__Value__ToUint32(this: &Value, context: *mut Context) -> *mut Uint32; fn v8__Value__ToUint32(this: &Value, context: *mut Context) -> *mut Uint32;
fn v8__Value__ToInt32(this: &Value, context: *mut Context) -> *mut Int32; fn v8__Value__ToInt32(this: &Value, context: *mut Context) -> *mut Int32;
fn v8__Value__NumberValue(
this: &Value,
context: *mut Context,
out: *mut Maybe<f64>,
);
fn v8__Value__IntegerValue(
this: &Value,
context: *mut Context,
out: *mut Maybe<i64>,
);
fn v8__Value__Uint32Value(
this: &Value,
context: *mut Context,
out: *mut Maybe<u32>,
);
fn v8__Value__Int32Value(
this: &Value,
context: *mut Context,
out: *mut Maybe<i32>,
);
} }
impl Value { impl Value {
@ -446,4 +468,45 @@ impl Value {
let mut context = isolate.get_current_context(); let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToInt32(self, &mut *context)) } unsafe { Local::from_raw(v8__Value__ToInt32(self, &mut *context)) }
} }
pub fn number_value<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<f64> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
let mut out = Maybe::<f64>::default();
unsafe { v8__Value__NumberValue(self, &mut *context, &mut out) };
out.into()
}
pub fn integer_value<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<i64> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
let mut out = Maybe::<i64>::default();
unsafe { v8__Value__IntegerValue(self, &mut *context, &mut out) };
out.into()
}
pub fn uint32_value<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<u32> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
let mut out = Maybe::<u32>::default();
unsafe { v8__Value__Uint32Value(self, &mut *context, &mut out) };
out.into()
}
pub fn int32_value<'sc>(&self, scope: &mut impl ToLocal<'sc>) -> Option<i32> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
let mut out = Maybe::<i32>::default();
unsafe { v8__Value__Int32Value(self, &mut *context, &mut out) };
out.into()
}
} }

View file

@ -1835,6 +1835,12 @@ fn value_checker() {
assert_eq!(123, value.to_uint32(scope).unwrap().value()); assert_eq!(123, value.to_uint32(scope).unwrap().value());
assert_eq!(123, value.to_int32(scope).unwrap().value()); assert_eq!(123, value.to_int32(scope).unwrap().value());
assert_eq!(123, value.to_integer(scope).unwrap().value()); assert_eq!(123, value.to_integer(scope).unwrap().value());
assert_eq!(123, value.integer_value(scope).unwrap());
assert_eq!(123, value.uint32_value(scope).unwrap());
assert_eq!(123, value.int32_value(scope).unwrap());
let value = eval(scope, context, "12.3").unwrap();
assert!(12.3 - value.number_value(scope).unwrap() < 0.00001);
let value = eval(scope, context, "-123").unwrap(); let value = eval(scope, context, "-123").unwrap();
assert!(value.is_number()); assert!(value.is_number());