forked from denoland/rusty_v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slots.rs
345 lines (279 loc) · 9.51 KB
/
slots.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// These tests mock out an organizational pattern that we hope to use in Deno.
// There we want to wrap v8::Isolate to provide extra functionality at multiple
// layers: v8::Isolate -> CoreIsolate -> EsIsolate
// This demonstrates how this can be done in a safe way.
use std::ops::Deref;
use std::ops::DerefMut;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Once;
fn setup() {
static START: Once = Once::new();
START.call_once(|| {
v8::V8::set_flags_from_string("--expose_gc");
v8::V8::initialize_platform(
v8::new_unprotected_default_platform(0, false).make_shared(),
);
v8::V8::initialize();
});
}
struct CoreIsolate(v8::OwnedIsolate);
struct CoreIsolateState {
drop_count: Rc<AtomicUsize>,
i: usize,
}
impl Drop for CoreIsolateState {
fn drop(&mut self) {
self.drop_count.fetch_add(1, Ordering::SeqCst);
}
}
impl CoreIsolate {
fn new(drop_count: Rc<AtomicUsize>) -> CoreIsolate {
setup();
let mut isolate = v8::Isolate::new(Default::default());
let state = CoreIsolateState { drop_count, i: 0 };
isolate.set_slot(state);
CoreIsolate(isolate)
}
// Returns false if there was an error.
fn execute(&mut self, code: &str) -> bool {
let scope = &mut v8::HandleScope::new(&mut self.0);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let source = v8::String::new(scope, code).unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
let r = script.run(scope);
r.is_some()
}
fn get_i(&self) -> usize {
let s = self.0.get_slot::<CoreIsolateState>().unwrap();
s.i
}
fn set_i(&mut self, i: usize) {
let s = self.0.get_slot_mut::<CoreIsolateState>().unwrap();
s.i = i;
}
}
impl Deref for CoreIsolate {
type Target = v8::Isolate;
fn deref(&self) -> &v8::Isolate {
&self.0
}
}
impl DerefMut for CoreIsolate {
fn deref_mut(&mut self) -> &mut v8::Isolate {
&mut self.0
}
}
struct EsIsolate(CoreIsolate);
struct EsIsolateState {
drop_count: Rc<AtomicUsize>,
x: bool,
}
impl Drop for EsIsolateState {
fn drop(&mut self) {
self.drop_count.fetch_add(1, Ordering::SeqCst);
}
}
impl EsIsolate {
fn new(drop_count: Rc<AtomicUsize>) -> Self {
let mut core_isolate = CoreIsolate::new(drop_count.clone());
let state = EsIsolateState {
drop_count,
x: false,
};
core_isolate.set_slot(state);
EsIsolate(core_isolate)
}
fn get_x(&self) -> bool {
let state = self.0.get_slot::<EsIsolateState>().unwrap();
state.x
}
fn set_x(&mut self, x: bool) {
let state = self.0.get_slot_mut::<EsIsolateState>().unwrap();
state.x = x;
}
}
impl Deref for EsIsolate {
type Target = CoreIsolate;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for EsIsolate {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[test]
fn slots_layer1() {
let drop_count = Rc::new(AtomicUsize::new(0));
let mut core_isolate = CoreIsolate::new(drop_count.clone());
// The existence of a IsolateHandle that outlives the isolate should not
// inhibit dropping of slot contents.
let isolate_handle = core_isolate.thread_safe_handle();
assert!(core_isolate.execute("1 + 1"));
assert!(!core_isolate.execute("throw 'foo'"));
assert_eq!(0, core_isolate.get_i());
core_isolate.set_i(123);
assert_eq!(123, core_isolate.get_i());
assert_eq!(drop_count.load(Ordering::SeqCst), 0);
// Check that we can deref CoreIsolate by running a random v8::Isolate method
core_isolate.perform_microtask_checkpoint();
drop(core_isolate);
assert_eq!(drop_count.load(Ordering::SeqCst), 1);
drop(isolate_handle);
}
#[test]
fn slots_layer2() {
let drop_count = Rc::new(AtomicUsize::new(0));
let mut es_isolate = EsIsolate::new(drop_count.clone());
// We can deref to CoreIsolate and use execute...
assert!(es_isolate.execute("1 + 1"));
assert!(!es_isolate.execute("throw 'bar'"));
// We can use get_x set_x
assert!(!es_isolate.get_x());
es_isolate.set_x(true);
assert!(es_isolate.get_x());
// Check that we can deref all the way to a v8::Isolate method
es_isolate.perform_microtask_checkpoint();
// When we drop, both CoreIsolateState and EsIsolateState should be dropped.
assert_eq!(drop_count.load(Ordering::SeqCst), 0);
drop(es_isolate);
assert_eq!(drop_count.load(Ordering::SeqCst), 2);
}
// General test for the slots system, not specific for the Deno pattern.
struct TestState(i32);
#[test]
fn slots_general_1() {
let mut core_isolate = CoreIsolate::new(Rc::new(AtomicUsize::new(0)));
// Set a value in the slots system.
let first_add = core_isolate.set_slot::<TestState>(TestState(0));
// Verify that this was the first time a value of this type was added.
assert!(first_add);
let second_add = core_isolate.set_slot::<TestState>(TestState(1));
// Verify that the set operation cause an existing value to be replaced and dropped.
assert!(!second_add);
// Increase the value stored.
core_isolate.get_slot_mut::<TestState>().unwrap().0 += 5;
// Verify the value has changed,
// and that it was really replaced (if it were not, the result would be 5).
assert_eq!(core_isolate.get_slot::<TestState>().unwrap().0, 6);
// Remove the value out of the slot.
let value = core_isolate.remove_slot::<TestState>().unwrap();
// Verify that we got the proper value.
assert_eq!(value.0, 6);
// Verify that the slot is empty now.
assert!(core_isolate.remove_slot::<TestState>().is_none());
}
#[test]
fn slots_general_2() {
let drop_count = Rc::new(AtomicUsize::new(0));
let mut core_isolate = CoreIsolate::new(drop_count.clone());
let state: CoreIsolateState =
core_isolate.remove_slot::<CoreIsolateState>().unwrap();
drop(core_isolate);
// The state should not be dropped with the isolate because we own it now.
assert_eq!(drop_count.load(Ordering::SeqCst), 0);
// We're dropping it now on purpose.
drop(state);
assert_eq!(drop_count.load(Ordering::SeqCst), 1);
}
// This struct is too large to be stored in the slot by value, so it should
// automatically and transparently get boxed and unboxed.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
struct TestData([u64; 4]);
#[test]
fn slots_auto_boxing() {
let mut core_isolate = CoreIsolate::new(Default::default());
// Create a slot which contains `TestData` by value.
let value1 = TestData([1, 2, 3, 4]);
assert!(core_isolate.set_slot(value1));
// Create another slot which contains a `Box<TestData>`. This should not
// overwrite or conflict with the unboxed slot created above.
let value2 = Box::new(TestData([5, 6, 7, 8]));
assert!(core_isolate.set_slot(value2.clone()));
// Verify that the `Testdata` slot exists and contains the expected value.
assert_eq!(Some(&value1), core_isolate.get_slot::<TestData>());
assert_eq!(Some(value1), core_isolate.remove_slot::<TestData>());
assert_eq!(None, core_isolate.get_slot::<TestData>());
// Verify the contents of the `Box<Testdata>` slot.
assert_eq!(Some(&value2), core_isolate.get_slot::<Box<TestData>>());
assert_eq!(Some(value2), core_isolate.remove_slot::<Box<TestData>>());
assert_eq!(None, core_isolate.get_slot::<Box<TestData>>());
}
#[test]
fn context_slots() {
setup();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
assert!(context.set_slot(scope, TestState(0)));
assert!(!context.set_slot(scope, TestState(1)));
context.get_slot_mut::<TestState>(scope).unwrap().0 += 5;
assert_eq!(context.get_slot::<TestState>(scope).unwrap().0, 6);
let value = context.remove_slot::<TestState>(scope).unwrap();
assert_eq!(value.0, 6);
assert!(context.remove_slot::<TestState>(scope).is_none());
}
#[test]
fn dropped_context_slots() {
// Test that context slots are dropped when the context is GC'd.
use std::cell::Cell;
struct DropMarker(Rc<Cell<bool>>);
impl Drop for DropMarker {
fn drop(&mut self) {
println!("Dropping the drop marker");
self.0.set(true);
}
}
let mut isolate = CoreIsolate::new(Default::default());
let dropped = Rc::new(Cell::new(false));
{
let scope = &mut v8::HandleScope::new(isolate.deref_mut());
let context = v8::Context::new(scope);
context.set_slot(scope, DropMarker(dropped.clone()));
}
assert!(isolate.execute("gc()"));
assert!(dropped.get());
}
#[test]
fn dropped_context_slots_on_kept_context() {
use std::cell::Cell;
struct DropMarker(Rc<Cell<bool>>);
impl Drop for DropMarker {
fn drop(&mut self) {
println!("Dropping the drop marker");
self.0.set(true);
}
}
let mut isolate = CoreIsolate::new(Default::default());
let dropped = Rc::new(Cell::new(false));
let _global_context;
{
let scope = &mut v8::HandleScope::new(isolate.deref_mut());
let context = v8::Context::new(scope);
context.set_slot(scope, DropMarker(dropped.clone()));
_global_context = v8::Global::new(scope, context);
}
drop(isolate);
assert!(dropped.get());
}
#[test]
fn clear_all_context_slots() {
setup();
let mut snapshot_creator = v8::Isolate::snapshot_creator(None);
{
let scope = &mut v8::HandleScope::new(&mut snapshot_creator);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
context.set_slot(scope, TestState(0));
context.clear_all_slots(scope);
assert!(context.get_slot::<TestState>(scope).is_none());
scope.set_default_context(context);
}
snapshot_creator
.create_blob(v8::FunctionCodeHandling::Keep)
.unwrap();
}