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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! v1.3 Phase SS-B — `SendVm` newtype wrapper for cross-thread embedding.
//!
//! Gated behind `#[cfg(feature = "send")]`. Embedders opt in via
//! `cargo add luna-core --features send` and use [`SendVm`] in place
//! of [`crate::vm::Vm`] when they need to hold a Lua state across
//! `.await` boundaries on a multi-threaded executor or move it between
//! OS threads.
//!
//! # Shape
//!
//! ```ignore
//! pub struct SendVm {
//! inner: Arc<UnsafeCell<Vm>>,
//! lock: Arc<RwLock<()>>,
//! }
//! ```
//!
//! - `Arc<UnsafeCell<Vm>>` — the wrapped Vm. `UnsafeCell` because the
//! API surface presents `&self` (so `SendVm` can be cloned and the
//! handle copied into multiple threads), but every method internally
//! reaches a `&mut Vm` once the lock is held.
//! - `Arc<RwLock<()>>` — access serializer. All operations take
//! `lock.write()` (effectively a `Mutex` — see the SAFETY notes
//! below for why the read/write split in the audit is purely a
//! conceptual classification, not a mechanically distinct path).
//!
//! `unsafe impl Send for SendVm {}` — safe because:
//!
//! 1. `Vm` is `!Send` only because its [`crate::runtime::heap::Heap`]
//! holds raw `*mut GcHeader` pointers and a thread-local-ish
//! `JitState`. The pointers themselves are address values; what
//! forbids `Send` is the *single-mutator* invariant the dispatcher
//! relies on. Once we serialize all access through the lock, only
//! one OS thread at a time materializes the `&mut Vm` and runs the
//! dispatcher — the same single-mutator invariant the bare `Vm`
//! holds, just established via the lock rather than via type-system
//! `!Send`.
//! 2. The interp-only restriction below means `JitState` stays
//! `NullJitBackend` and the `JIT_VM` TLS pointer is never
//! populated; the only thread-local state the Vm touches under
//! `SendVm` is the std-library RNG seed at construction time
//! (`Vm::new_minimal` → `rng_auto_seed`), which runs once on the
//! constructing thread and is never re-read TLS-wise afterwards.
//!
//! `SendVm` is **not** `Sync`. Cross-thread share of `&SendVm` is
//! forbidden — only move/clone-and-move. Clones share the same
//! underlying Vm via the inner `Arc`; concurrent calls block on the
//! lock.
//!
//! # Interp-only constraint
//!
//! Per `.dev/rfcs/v1.3-audit-send-vm-design.md` §3.3, the v1.3 ship
//! of `SendVm` does **not** install a JIT backend. `SendVm::new`
//! calls [`Vm::new_minimal`] which leaves `JitState` at
//! `NullJitBackend`; the dispatcher always falls back to the
//! interpreter. JIT-aware `SendVm` is a post-v1.3 polish item (the
//! `Proto::traces: RefCell<Vec<Rc<CompiledTrace>>>` cross-cutting
//! concern intersects with `Send` and is scoped out of v1.3).
//!
//! This is a documented contract, not a defer — embedders who need
//! both Send semantics and JIT today should run one bare `Vm` per OS
//! thread and exchange data via channels.
//!
//! # When to use `SendVm` vs `Vm`
//!
//! - **`Vm`** — single-thread scripting (game engine main thread,
//! CLI tool, REPL). The fast path; zero overhead vs the v1.2
//! baseline.
//! - **`SendVm`** — multi-threaded host (tokio `multi_thread`,
//! request-per-script web server, worker-pool embedding). Pays
//! the lock acquire cost (~30-50 ns per method call) and gives
//! up the JIT.
//!
//! See `docs/threading.md` for the canonical embedding patterns.
use UnsafeCell;
use ;
use crateValue;
use crateLuaVersion;
use crateLuaError;
use crateVm;
use crate;
use crateLuaUserdata;
/// Cross-thread-capable Lua VM handle.
///
/// See the [module docs](self) for the design, safety contract, and
/// the v1.3 interp-only restriction.
///
/// Clone the handle and move clones into threads / tasks to share one
/// underlying `Vm` across workers; concurrent method calls block on
/// the inner lock. For genuine parallelism (multiple scripts running
/// at once), construct one `SendVm` per worker — the type's value is
/// that you can hold it across `.await` on a multi-thread executor,
/// not that two threads execute the same script simultaneously.
// SAFETY: see module-level docs. `Vm`'s `!Send` derives from a
// single-mutator invariant the dispatcher relies on; the `RwLock`
// established alongside the `UnsafeCell` re-establishes that
// invariant at runtime (only one thread holds the write guard at a
// time, and every method takes the write guard before materializing
// `&mut Vm`). Interp-only constraint (NullJitBackend, no `JIT_VM`
// TLS) plus the construction-time-only RNG seed mean no per-call
// thread-local state escapes the move/clone-and-move semantics.
//
// SendVm is intentionally not Sync — cross-thread `&SendVm` would
// allow two threads to lock-and-eval concurrently; the lock would
// serialize them but the *handle* still needs to move/clone (not
// shared by reference) to discourage misuse.
unsafe