mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-03-09 13:38:51 -04:00
Add 'External::new()' and 'External::value()' (#413)
This commit is contained in:
parent
acf6bfe0e4
commit
b981aaceee
4 changed files with 81 additions and 0 deletions
|
@ -765,6 +765,12 @@ const v8::Array* v8__Array__New_with_elements(v8::Isolate* isolate,
|
||||||
|
|
||||||
uint32_t v8__Array__Length(const v8::Array& self) { return self.Length(); }
|
uint32_t v8__Array__Length(const v8::Array& self) { return self.Length(); }
|
||||||
|
|
||||||
|
const v8::External* v8__External__New(v8::Isolate* isolate, void* value) {
|
||||||
|
return local_to_ptr(v8::External::New(isolate, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void* v8__External__Value(const v8::External& self) { return self.Value(); }
|
||||||
|
|
||||||
size_t v8__Map__Size(const v8::Map& self) { return self.Size(); }
|
size_t v8__Map__Size(const v8::Map& self) { return self.Size(); }
|
||||||
|
|
||||||
const v8::Array* v8__Map__As__Array(const v8::Map& self) {
|
const v8::Array* v8__Map__As__Array(const v8::Map& self) {
|
||||||
|
|
32
src/external.rs
Normal file
32
src/external.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
use std::ffi::c_void;
|
||||||
|
|
||||||
|
use crate::External;
|
||||||
|
use crate::HandleScope;
|
||||||
|
use crate::Isolate;
|
||||||
|
use crate::Local;
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
fn v8__External__New(
|
||||||
|
isolate: *mut Isolate,
|
||||||
|
value: *mut c_void,
|
||||||
|
) -> *const External;
|
||||||
|
fn v8__External__Value(this: *const External) -> *mut c_void;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl External {
|
||||||
|
pub fn new<'s>(
|
||||||
|
scope: &mut HandleScope<'s>,
|
||||||
|
value: *mut c_void,
|
||||||
|
) -> Local<'s, Self> {
|
||||||
|
unsafe {
|
||||||
|
scope.cast_local(|sd| v8__External__New(sd.get_isolate_ptr(), value))
|
||||||
|
}
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn value(&self) -> *mut c_void {
|
||||||
|
unsafe { v8__External__Value(self) }
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,6 +38,7 @@ mod array_buffer_view;
|
||||||
mod context;
|
mod context;
|
||||||
mod data;
|
mod data;
|
||||||
mod exception;
|
mod exception;
|
||||||
|
mod external;
|
||||||
mod external_references;
|
mod external_references;
|
||||||
mod function;
|
mod function;
|
||||||
mod handle;
|
mod handle;
|
||||||
|
|
|
@ -549,6 +549,48 @@ fn eval<'s>(
|
||||||
r.map(|v| scope.escape(v))
|
r.map(|v| scope.escape(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external() {
|
||||||
|
let _setup_guard = setup();
|
||||||
|
let isolate = &mut v8::Isolate::new(Default::default());
|
||||||
|
{
|
||||||
|
let scope = &mut v8::HandleScope::new(isolate);
|
||||||
|
let context = v8::Context::new(scope);
|
||||||
|
let scope = &mut v8::ContextScope::new(scope, context);
|
||||||
|
let global = context.global(scope);
|
||||||
|
|
||||||
|
let ex1_value = 1234567usize as *mut std::ffi::c_void;
|
||||||
|
let ex2_value = -1isize as *mut std::ffi::c_void;
|
||||||
|
|
||||||
|
let ex1_handle = v8::External::new(scope, ex1_value);
|
||||||
|
let ex2_handle = v8::External::new(scope, ex2_value);
|
||||||
|
|
||||||
|
assert!(ex1_handle != ex2_handle);
|
||||||
|
assert_ne!(ex1_value, ex2_value);
|
||||||
|
assert_eq!(ex1_handle.value(), ex1_value);
|
||||||
|
assert_eq!(ex2_handle.value(), ex2_value);
|
||||||
|
|
||||||
|
let ex1_key = v8::String::new(scope, "e1").unwrap().into();
|
||||||
|
let ex2_key = v8::String::new(scope, "e2").unwrap().into();
|
||||||
|
|
||||||
|
global.set(scope, ex1_key, ex1_handle.into());
|
||||||
|
global.set(scope, ex2_key, ex2_handle.into());
|
||||||
|
|
||||||
|
let ex1_handle_2: v8::Local<v8::External> =
|
||||||
|
eval(scope, "e1").unwrap().try_into().unwrap();
|
||||||
|
let ex2_handle_2: v8::Local<v8::External> =
|
||||||
|
eval(scope, "e2").unwrap().try_into().unwrap();
|
||||||
|
|
||||||
|
assert!(ex1_handle_2 != ex2_handle_2);
|
||||||
|
assert!(ex1_handle == ex1_handle_2);
|
||||||
|
assert!(ex2_handle == ex2_handle_2);
|
||||||
|
|
||||||
|
assert_ne!(ex1_value, ex2_value);
|
||||||
|
assert_eq!(ex1_handle.value(), ex1_value);
|
||||||
|
assert_eq!(ex2_handle.value(), ex2_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn try_catch() {
|
fn try_catch() {
|
||||||
let _setup_guard = setup();
|
let _setup_guard = setup();
|
||||||
|
|
Loading…
Add table
Reference in a new issue