0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-11 08:34:01 -05:00

add to_uint32, to_int32, to_integer, to_detail_string, to_big_int (#173)

This commit is contained in:
Ry Dahl 2020-01-03 11:14:50 -05:00 committed by GitHub
parent 4ffbb7a00b
commit 8adf85ea89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 7 deletions

View file

@ -467,10 +467,31 @@ bool v8__Value__SameValue(const v8::Value& self, v8::Value* that) {
return self.SameValue(ptr_to_local(that));
}
v8::Uint32* v8__Value__ToUint32(const v8::Value& self, v8::Context* context) {
return maybe_local_to_ptr(self.ToUint32(ptr_to_local(context)));
}
v8::Int32* v8__Value__ToInt32(const v8::Value& self, v8::Context* context) {
return maybe_local_to_ptr(self.ToInt32(ptr_to_local(context)));
}
v8::Integer* v8__Value__ToInteger(const v8::Value& self, v8::Context* context) {
return maybe_local_to_ptr(self.ToInteger(ptr_to_local(context)));
}
v8::BigInt* v8__Value__ToBigInt(const v8::Value& self, v8::Context* context) {
return maybe_local_to_ptr(self.ToBigInt(ptr_to_local(context)));
}
v8::String* v8__Value__ToString(const v8::Value& self, v8::Context* context) {
return maybe_local_to_ptr(self.ToString(ptr_to_local(context)));
}
v8::String* v8__Value__ToDetailString(const v8::Value& self,
v8::Context* context) {
return maybe_local_to_ptr(self.ToDetailString(ptr_to_local(context)));
}
v8::Number* v8__Value__ToNumber(const v8::Value& self, v8::Context* context) {
return maybe_local_to_ptr(self.ToNumber(ptr_to_local(context)));
}

View file

@ -1,9 +1,13 @@
use crate::BigInt;
use crate::Context;
use crate::Int32;
use crate::Integer;
use crate::Local;
use crate::Number;
use crate::Object;
use crate::String;
use crate::ToLocal;
use crate::Uint32;
use crate::Value;
extern "C" {
@ -64,9 +68,18 @@ extern "C" {
fn v8__Value__IsModuleNamespaceObject(this: &Value) -> bool;
fn v8__Value__StrictEquals(this: &Value, that: &Value) -> bool;
fn v8__Value__SameValue(this: &Value, that: &Value) -> bool;
fn v8__Value__ToString(this: &Value, context: *mut Context) -> *mut String;
fn v8__Value__ToBigInt(this: &Value, context: *mut Context) -> *mut BigInt;
fn v8__Value__ToNumber(this: &Value, context: *mut Context) -> *mut Number;
fn v8__Value__ToString(this: &Value, context: *mut Context) -> *mut String;
fn v8__Value__ToDetailString(
this: &Value,
context: *mut Context,
) -> *mut String;
fn v8__Value__ToObject(this: &Value, context: *mut Context) -> *mut Object;
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__ToInt32(this: &Value, context: *mut Context) -> *mut Int32;
}
impl Value {
@ -362,13 +375,13 @@ impl Value {
unsafe { v8__Value__SameValue(self, &that) }
}
pub fn to_string<'sc>(
pub fn to_big_int<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, String>> {
) -> Option<Local<'sc, BigInt>> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToString(self, &mut *context)) }
unsafe { Local::from_raw(v8__Value__ToBigInt(self, &mut *context)) }
}
pub fn to_number<'sc>(
@ -380,6 +393,24 @@ impl Value {
unsafe { Local::from_raw(v8__Value__ToNumber(self, &mut *context)) }
}
pub fn to_string<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, String>> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToString(self, &mut *context)) }
}
pub fn to_detail_string<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, String>> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToDetailString(self, &mut *context)) }
}
pub fn to_object<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
@ -388,4 +419,31 @@ impl Value {
let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToObject(self, &mut *context)) }
}
pub fn to_integer<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, Integer>> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToInteger(self, &mut *context)) }
}
pub fn to_uint32<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, Uint32>> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToUint32(self, &mut *context)) }
}
pub fn to_int32<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, Int32>> {
let isolate = scope.isolate();
let mut context = isolate.get_current_context();
unsafe { Local::from_raw(v8__Value__ToInt32(self, &mut *context)) }
}
}

View file

@ -1823,14 +1823,18 @@ fn value_checker() {
let value = eval(scope, context, "BigInt('9007199254740995')").unwrap();
assert!(value.is_big_int());
let value = eval(scope, context, "123").unwrap();
assert!(value.is_number());
assert!(value.to_big_int(scope).is_some());
let detail_string = value.to_detail_string(scope).unwrap();
let detail_string = detail_string.to_rust_string_lossy(scope);
assert_eq!("9007199254740995", detail_string);
let value = eval(scope, context, "123").unwrap();
assert!(value.is_number());
assert!(value.is_int32());
assert!(value.is_uint32());
assert_eq!(123, value.to_uint32(scope).unwrap().value());
assert_eq!(123, value.to_int32(scope).unwrap().value());
assert_eq!(123, value.to_integer(scope).unwrap().value());
let value = eval(scope, context, "-123").unwrap();
assert!(value.is_number());