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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use crate::support::{int, Opaque};
use crate::Context;
use crate::Function;
use crate::FunctionTemplate;
use crate::InIsolate;
use crate::Isolate;
use crate::Local;
use crate::ToLocal;
use crate::Value;
pub type FunctionCallback = extern "C" fn(&FunctionCallbackInfo);
extern "C" {
fn v8__Function__New(
context: *mut Context,
callback: FunctionCallback,
) -> *mut Function;
fn v8__Function__Call(
function: *mut Function,
context: *mut Context,
recv: *mut Value,
argc: int,
argv: *mut *mut Value,
) -> *mut Value;
fn v8__FunctionTemplate__New(
isolate: &Isolate,
callback: FunctionCallback,
) -> *mut FunctionTemplate;
fn v8__FunctionTemplate__GetFunction(
fn_template: *mut FunctionTemplate,
context: *mut Context,
) -> *mut Function;
fn v8__FunctionCallbackInfo__GetIsolate(
info: &FunctionCallbackInfo,
) -> &mut Isolate;
fn v8__FunctionCallbackInfo__Length(info: &FunctionCallbackInfo) -> int;
fn v8__FunctionCallbackInfo__GetReturnValue(
info: &FunctionCallbackInfo,
out: *mut ReturnValue,
);
fn v8__FunctionCallbackInfo__GetArgument(
info: &FunctionCallbackInfo,
i: int,
) -> *mut Value;
fn v8__ReturnValue__Set(rv: &mut ReturnValue, value: *mut Value);
fn v8__ReturnValue__Get(rv: &ReturnValue) -> *mut Value;
fn v8__ReturnValue__GetIsolate(rv: &ReturnValue) -> *mut Isolate;
}
#[repr(C)]
pub struct ReturnValue<'cb>(*mut Opaque, PhantomData<&'cb ()>);
impl<'cb> ReturnValue<'cb> {
pub fn set(&mut self, mut value: Local<Value>) {
unsafe { v8__ReturnValue__Set(&mut *self, &mut *value) }
}
pub fn get_isolate(&mut self) -> &mut Isolate {
unsafe { &mut *v8__ReturnValue__GetIsolate(self) }
}
pub fn get<'sc>(
&mut self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__ReturnValue__Get(self)) }.unwrap()
}
}
#[repr(C)]
pub struct FunctionCallbackInfo(Opaque);
impl InIsolate for FunctionCallbackInfo {
fn isolate(&mut self) -> &mut Isolate {
self.get_isolate()
}
}
impl<'s> ToLocal<'s> for FunctionCallbackInfo {}
impl FunctionCallbackInfo {
pub fn get_return_value(&self) -> ReturnValue {
let mut rv = MaybeUninit::<ReturnValue>::uninit();
unsafe {
v8__FunctionCallbackInfo__GetReturnValue(self, rv.as_mut_ptr());
rv.assume_init()
}
}
#[allow(clippy::mut_from_ref)]
pub fn get_isolate(&mut self) -> &mut Isolate {
unsafe { v8__FunctionCallbackInfo__GetIsolate(self) }
}
pub fn length(&self) -> int {
unsafe { v8__FunctionCallbackInfo__Length(self) }
}
pub fn get_argument<'sc>(&mut self, i: int) -> Local<'sc, Value> {
unsafe {
Local::from_raw(v8__FunctionCallbackInfo__GetArgument(self, i)).unwrap()
}
}
}
impl FunctionTemplate {
pub fn new<'sc>(
scope: &mut impl ToLocal<'sc>,
callback: FunctionCallback,
) -> Local<'sc, FunctionTemplate> {
let ptr = unsafe { v8__FunctionTemplate__New(scope.isolate(), callback) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn get_function<'sc>(
&mut self,
scope: &mut impl ToLocal<'sc>,
mut context: Local<Context>,
) -> Option<Local<'sc, Function>> {
unsafe {
scope
.to_local(v8__FunctionTemplate__GetFunction(&mut *self, &mut *context))
}
}
}
impl Function {
pub fn new<'sc>(
scope: &mut impl ToLocal<'sc>,
mut context: Local<Context>,
callback: FunctionCallback,
) -> Option<Local<'sc, Function>> {
unsafe { scope.to_local(v8__Function__New(&mut *context, callback)) }
}
pub fn call<'sc>(
&mut self,
scope: &mut impl ToLocal<'sc>,
mut context: Local<Context>,
mut recv: Local<Value>,
arc: i32,
argv: Vec<Local<Value>>,
) -> Option<Local<'sc, Value>> {
let mut argv_: Vec<*mut Value> = vec![];
for mut arg in argv {
argv_.push(&mut *arg);
}
unsafe {
scope.to_local(v8__Function__Call(
&mut *self,
&mut *context,
&mut *recv,
arc,
argv_.as_mut_ptr(),
))
}
}
}