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
use crate::data::Data;
use crate::data::FunctionTemplate;
use crate::data::Name;
use crate::data::ObjectTemplate;
use crate::data::Template;
use crate::isolate::Isolate;
use crate::support::MapFnTo;
use crate::Context;
use crate::Function;
use crate::FunctionCallback;
use crate::Local;
use crate::Object;
use crate::PropertyAttribute;
use crate::String;
use crate::ToLocal;
use crate::NONE;
extern "C" {
fn v8__Template__Set(
this: *const Template,
key: *const Name,
value: *const Data,
attr: PropertyAttribute,
);
fn v8__FunctionTemplate__New(
isolate: *mut Isolate,
callback: FunctionCallback,
) -> *const FunctionTemplate;
fn v8__FunctionTemplate__GetFunction(
this: *const FunctionTemplate,
context: *const Context,
) -> *const Function;
fn v8__FunctionTemplate__SetClassName(
this: *const FunctionTemplate,
name: *const String,
);
fn v8__ObjectTemplate__New(
isolate: *mut Isolate,
templ: *const FunctionTemplate,
) -> *const ObjectTemplate;
fn v8__ObjectTemplate__NewInstance(
this: *const ObjectTemplate,
context: *const Context,
) -> *const Object;
}
impl Template {
pub fn set(&self, key: Local<Name>, value: Local<Data>) {
self.set_with_attr(key, value, NONE)
}
pub fn set_with_attr(
&self,
key: Local<Name>,
value: Local<Data>,
attr: PropertyAttribute,
) {
unsafe { v8__Template__Set(self, &*key, &*value, attr) }
}
}
impl FunctionTemplate {
pub fn new<'sc>(
scope: &mut impl ToLocal<'sc>,
callback: impl MapFnTo<FunctionCallback>,
) -> Local<'sc, FunctionTemplate> {
let ptr = unsafe {
v8__FunctionTemplate__New(scope.isolate(), callback.map_fn_to())
};
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn get_function<'sc>(
&mut self,
scope: &mut impl ToLocal<'sc>,
context: Local<Context>,
) -> Option<Local<'sc, Function>> {
unsafe {
scope.to_local(v8__FunctionTemplate__GetFunction(&*self, &*context))
}
}
pub fn set_class_name(&mut self, name: Local<String>) {
unsafe { v8__FunctionTemplate__SetClassName(&*self, &*name) };
}
}
impl ObjectTemplate {
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, ObjectTemplate> {
let ptr =
unsafe { v8__ObjectTemplate__New(scope.isolate(), std::ptr::null()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn new_from_template<'sc>(
scope: &mut impl ToLocal<'sc>,
templ: Local<FunctionTemplate>,
) -> Local<'sc, ObjectTemplate> {
let ptr = unsafe { v8__ObjectTemplate__New(scope.isolate(), &*templ) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn new_instance<'a>(
&self,
scope: &mut impl ToLocal<'a>,
context: Local<Context>,
) -> Option<Local<'a, Object>> {
let ptr = unsafe { v8__ObjectTemplate__NewInstance(self, &*context) };
unsafe { scope.to_local(ptr) }
}
}