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
//! `quickjs-capi` — the QuickJS C API on top of the pure-Rust engine.
//!
//! The library this crate produces is `libquickjs.so`, and it is meant to be a drop-in
//! replacement for the reference implementation's at link time: same exported symbols,
//! same `JSValue` bit layout, same struct offsets. `docs/m15-plan.md` is the reasoning;
//! this is the code.
//!
//! # How the two representations meet
//!
//! The engine's `Value` is a 16-byte enum. C's `JSValue` is 8 bytes of NaN boxing. The
//! mapping has to be *bit*-compatible rather than a conversion at the boundary, because
//! `quickjs.h` ships 119 of its "functions" as `static inline` and they run in the
//! caller's translation unit — `JS_DupValue` increments a count through a raw pointer that
//! only exists because we put it there.
//!
//! * [`jsvalue`] is the bit half: tags, encode, decode. No `unsafe`.
//! * [`alloc`] is the memory half: the header that puts `ref_count` four bytes before the
//! payload, and the boxes that hold engine values.
//!
//! # `unsafe` is the whole point of this crate
//!
//! Everything a C caller can do to a `JSValue` — duplicate it, free it, read a field of a
//! struct we handed over — happens outside Rust's type system. `scripts/check-unsafe.sh`
//! lists this crate as a deliberate exception for that reason, and each `unsafe` block
//! below carries the argument for why it is sound.
//!
//! # Status
//!
//! M15's S2: the value/allocator layer plus the `js_malloc` family of exports. The
//! remaining symbols are S3–S7 in `docs/m15-plan.md` §5. This crate is `publish = false`
//! until that surface is complete — a `libquickjs` missing half its symbols is worse than
//! no `libquickjs`.
use c_void;
pub use JsValue;
pub use ;
// `release` and `argument` are the two halves of the ownership rule the whole API follows
// (a returned value is a new reference; an argument is borrowed), so they are reachable
// from a Rust-side caller as well as from this crate's own exports.
pub use ;
// ---------------------------------------------------------------------------
// js_malloc and friends
//
// Signatures and nullptr behaviour are the reference's: a size that cannot be represented
// gives a null pointer rather than an abort, so a script asking for 2^60 bytes gets a
// RangeError instead of a dead process.
// ---------------------------------------------------------------------------
/// `js_malloc`: `size` bytes owned by the runtime, zeroed by nobody.
///
/// # Safety
///
/// The answer must be released with `js_free`/`js_free_rt` (or `js_realloc`), and `size`
/// bytes from it may be written in the meantime.
pub unsafe extern "C"
/// `js_mallocz`: [`js_malloc`], zeroed.
///
/// # Safety
///
/// Same as [`js_malloc`].
pub unsafe extern "C"
/// `js_malloc_rt`: the runtime-level spelling of [`js_malloc`].
///
/// # Safety
///
/// Same as [`js_malloc`].
pub unsafe extern "C"
/// `js_mallocz_rt`: the runtime-level spelling of [`js_mallocz`].
///
/// # Safety
///
/// Same as [`js_malloc`].
pub unsafe extern "C"
/// `js_free`: release a block from [`js_malloc`], or do nothing for a null pointer.
///
/// # Safety
///
/// `ptr` must be null or a live pointer from the `js_malloc` family, and must not be used
/// afterwards.
pub unsafe extern "C"
/// `js_free_rt`: the runtime-level spelling of [`js_free`].
///
/// # Safety
///
/// Same as [`js_free`].
pub unsafe extern "C"
/// `js_realloc`: resize a block from [`js_malloc`], keeping its contents.
///
/// # Safety
///
/// `ptr` must be null or a live pointer from the `js_malloc` family with `size` bytes, and
/// must not be used again afterwards.
pub unsafe extern "C"
/// `js_realloc_rt`: the runtime-level spelling of [`js_realloc`].
///
/// # Safety
///
/// Same as [`js_realloc`].
pub unsafe extern "C"
/// `js_realloc2`: [`js_realloc`] that also reports the slack.
///
/// The reference answers `NULL` without reallocating when growing would not be worth it;
/// ours always reallocates and reports the difference. `*pslack` is only written when the
/// pointer is non-null, which is what the callers in `quickjs-libc.c` rely on.
///
/// # Safety
///
/// Same as [`js_realloc`]; `pslack` must be null or writable.
pub unsafe extern "C"
/// `js_malloc_usable_size`: how big a block actually is.
///
/// Ours records the requested size rather than the allocation's, so this answers what was
/// asked for. The reference answers the same number for its small blocks and a rounded one
/// for large ones; nothing in the C API depends on the difference — `quickjs-libc.c` uses
/// it only to size a read.
///
/// # Safety
///
/// `ptr` must be null or a live pointer from the `js_malloc` family.
pub unsafe extern "C"
/// `js_malloc_usable_size_rt`: the runtime-level spelling of
/// [`js_malloc_usable_size`].
///
/// # Safety
///
/// Same as [`js_malloc_usable_size`].
pub unsafe extern "C"
// ---------------------------------------------------------------------------
// Reference counting
// ---------------------------------------------------------------------------
/// `__JS_FreeValue`: drop a value whose count just reached zero.
///
/// Never called directly by C: the inline `JS_FreeValue` decrements first and calls this
/// only on the transition to zero. A value that carries no reference count is a no-op,
/// which the inline already filtered out — the check is here too so that a caller which
/// gets it wrong leaks nothing and crashes nothing.
///
/// # Safety
///
/// `value` must be a payload from this crate whose count has reached zero, and must not be
/// used again.
pub unsafe extern "C"
/// `__JS_FreeValueRT`: the runtime-level spelling of [`__JS_FreeValue`].
///
/// # Safety
///
/// Same as [`__JS_FreeValue`].
pub unsafe extern "C"