diff --git a/src/binding.cc b/src/binding.cc index 94899756..b226de01 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -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))); } diff --git a/src/value.rs b/src/value.rs index a9e56674..e5ce731f 100644 --- a/src/value.rs +++ b/src/value.rs @@ -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> { + ) -> Option> { 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> { + 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> { + 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> { + 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> { + 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> { + let isolate = scope.isolate(); + let mut context = isolate.get_current_context(); + unsafe { Local::from_raw(v8__Value__ToInt32(self, &mut *context)) } + } } diff --git a/tests/test_api.rs b/tests/test_api.rs index b288acf4..30301ed3 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -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());