0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

Add Uint32 / Int32 Value method bindings (#1030)

This commit is contained in:
Aapo Alasuutari 2022-07-13 07:03:55 +03:00 committed by GitHub
parent d2db387448
commit d8480fc7ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View file

@ -1418,6 +1418,10 @@ const v8::Integer* v8__Integer__NewFromUnsigned(v8::Isolate* isolate,
int64_t v8__Integer__Value(const v8::Integer& self) { return self.Value(); }
uint32_t v8__Uint32__Value(const v8::Uint32& self) { return self.Value(); }
int32_t v8__Int32__Value(const v8::Int32& self) { return self.Value(); }
const v8::BigInt* v8__BigInt__New(v8::Isolate* isolate, int64_t value) {
return local_to_ptr(v8::BigInt::New(isolate, value));
}

View file

@ -2,10 +2,12 @@ use std::alloc::Layout;
use std::ptr::NonNull;
use crate::HandleScope;
use crate::Int32;
use crate::Integer;
use crate::Isolate;
use crate::Local;
use crate::Number;
use crate::Uint32;
extern "C" {
fn v8__Number__New(isolate: *mut Isolate, value: f64) -> *const Number;
@ -16,6 +18,8 @@ extern "C" {
value: u32,
) -> *const Integer;
fn v8__Integer__Value(this: *const Integer) -> i64;
fn v8__Uint32__Value(this: *const Uint32) -> u32;
fn v8__Int32__Value(this: *const Int32) -> i32;
}
impl Number {
@ -78,3 +82,15 @@ impl Integer {
zero_local
}
}
impl Uint32 {
pub fn value(&self) -> u32 {
unsafe { v8__Uint32__Value(self) }
}
}
impl Int32 {
pub fn value(&self) -> i32 {
unsafe { v8__Int32__Value(self) }
}
}

View file

@ -71,9 +71,13 @@ fn handle_scope_numbers() {
{
let scope2 = &mut v8::HandleScope::new(scope1);
let l3 = v8::Number::new(scope2, 78.9);
let l4 = v8::Local::<v8::Int32>::try_from(l1).unwrap();
let l5 = v8::Local::<v8::Uint32>::try_from(l2).unwrap();
assert_eq!(l1.value(), -123);
assert_eq!(l2.value(), 456);
assert_eq!(l3.value(), 78.9);
assert_eq!(l4.value(), -123);
assert_eq!(l5.value(), 456);
assert_eq!(v8::Number::value(&l1), -123f64);
assert_eq!(v8::Number::value(&l2), 456f64);
}