-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.rs
34 lines (26 loc) · 939 Bytes
/
field.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
extern crate rvb as v8;
use std::mem;
use v8::v8::{
prelude::*,
Platform,
Isolate,
};
pub fn main() {
let _platform = Platform::New();
let mut isolate = Isolate::New();
isolate.exec(|ctx| {
let mut objt = ObjectT::New(None);
objt.set_internal_field_count(1);
let mut object = objt.new_instance(ctx).to_local_checked()?;
let count = object.internal_field_count();
let callback: Box<Box<dyn FnMut()>> = Box::new(Box::new(|| {
println!("I am from internal field.");
}));
object.set_internal_field(0, V8External::New(Box::into_raw(callback) as *mut std::ffi::c_void));
let closure_ptr = object.get_internal_field::<V8External>(0).value();
let closure: &mut Box<dyn FnMut()> = unsafe { mem::transmute(closure_ptr) };
println!("object has {} internal field.", count);
closure();
Ok(())
}).unwrap();
}