quickjs-capi 0.14.1

C ABI compatibility layer: the QuickJS C API on top of the pure-Rust engine
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! The C structs whose bytes C fills in and we read.
//!
//! `JSClassDef` and `JSCFunctionListEntry` are built by the *caller*'s compiler as static
//! data, then handed to us by pointer. Nothing about them is negotiated at runtime: a field
//! at the wrong offset is a silent misread, so every one of these is asserted against the
//! number `gcc` printed for `offsetof` on this platform (`tests/abi.rs`).
//!
//! Measured on x86-64 Linux with the reference `quickjs.h`:
//!
//! ```text
//! JSCFunctionListEntry 32 bytes: name 0, prop_flags 8, def_type 9, magic 10, u 16
//!   u.func.length 16, u.func.cproto 17, u.func.cfunc 24
//!   u.getset.get 16, u.getset.set 24, u.str/i32/i64/f64 16
//! JSClassDef 40 bytes: class_name 0, finalizer 8, gc_mark 16, call 24, exotic 32
//! ```
//!
//! # Why `JSCFunctionType` is a union of function pointers
//!
//! The reference spells one union for fourteen calling conventions and casts through it. We
//! only ever *read* the pointer out and call it with the convention the surrounding
//! `cproto`/`def_type` names, which is what the reference's own `JS_SetPropertyFunctionList`
//! does too — so the union stays opaque here and [`JSCFunctionType::as_ptr`] is the one
//! accessor.

use core::ffi::{c_char, c_int, c_void};

use crate::jsvalue::JsValue;

/// `JS_CALL_FLAG_CONSTRUCTOR`: the flag `JSClassCall` receives on a `new` call.
pub const JS_CALL_FLAG_CONSTRUCTOR: c_int = 1 << 0;

/// `JSClassID`: a `uint32_t`. `0` is `JS_INVALID_CLASS_ID`.
pub type ClassId = u32;

/// `JS_BOOL`.
pub type Bool = c_int;

/// `JS_PROP_*`: the attribute bits a `JSCFunctionListEntry::prop_flags` carries.
pub mod prop {
    /// `JS_PROP_CONFIGURABLE`.
    pub const CONFIGURABLE: u8 = 1 << 0;
    /// `JS_PROP_WRITABLE`.
    pub const WRITABLE: u8 = 1 << 1;
    /// `JS_PROP_ENUMERABLE`.
    pub const ENUMERABLE: u8 = 1 << 2;
    /// `JS_PROP_HAS_VALUE`, `JS_PROP_HAS_GET`, … — the "this descriptor field is present"
    /// bits. `JS_SetPropertyFunctionList` never sets them; they exist here so the shared
    /// constants are all in one place.
    pub const HAS_GET: u8 = 1 << 5;
    /// `JS_PROP_HAS_SET`.
    pub const HAS_SET: u8 = 1 << 6;
    /// `JS_PROP_C_W_E`, the default for a method.
    pub const C_W_E: u8 = CONFIGURABLE | WRITABLE | ENUMERABLE;
}

/// `JSCFunctionEnum`: which calling convention a `JSCFunctionType` holds.
///
/// Only the ones a `JSCFunctionListEntry` can name are listed; the reference's remaining
/// values (`f_f`, `f_f_f`, `iterator_next`, …) are `JS_CFUNC_SPECIAL_DEF`'s business and
/// arrive with the built-in classes that want them.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum CFunctionEnum {
    /// `JS_CFUNC_generic`: `(ctx, this_val, argc, argv)`.
    Generic = 0,
    /// `JS_CFUNC_generic_magic`: the same plus a trailing `magic`.
    GenericMagic = 1,
    /// `JS_CFUNC_constructor`: `(ctx, new_target, argc, argv)`, and `new` is allowed.
    Constructor = 2,
    /// `JS_CFUNC_constructor_magic`.
    ConstructorMagic = 3,
    /// `JS_CFUNC_constructor_or_func`: `(ctx, this_val, argc, argv)` either way.
    ConstructorOrFunc = 4,
    /// `JS_CFUNC_constructor_or_func_magic`.
    ConstructorOrFuncMagic = 5,
    /// `JS_CFUNC_constructor_or_func_flags`: `(ctx, func_obj, this_val, argc, argv, flags)`.
    ConstructorOrFuncFlags = 6,
    /// `JS_CFUNC_f_f`.
    FF = 7,
    /// `JS_CFUNC_f_f_f`.
    FFF = 8,
    /// `JS_CFUNC_getter`: `(ctx, this_val)`.
    Getter = 9,
    /// `JS_CFUNC_setter`: `(ctx, this_val, val)`.
    Setter = 10,
    /// `JS_CFUNC_getter_magic`.
    GetterMagic = 11,
    /// `JS_CFUNC_setter_magic`.
    SetterMagic = 12,
    /// `JS_CFUNC_iterator_next`.
    IteratorNext = 13,
}

impl CFunctionEnum {
    /// The enum value as C would have written it, or `None` for a value this layer does not
    /// implement — which is a `TypeError` for the caller rather than a guess at a calling
    /// convention.
    pub fn from_raw(raw: u8) -> Option<CFunctionEnum> {
        Some(match raw {
            0 => CFunctionEnum::Generic,
            1 => CFunctionEnum::GenericMagic,
            2 => CFunctionEnum::Constructor,
            3 => CFunctionEnum::ConstructorMagic,
            4 => CFunctionEnum::ConstructorOrFunc,
            5 => CFunctionEnum::ConstructorOrFuncMagic,
            6 => CFunctionEnum::ConstructorOrFuncFlags,
            7 => CFunctionEnum::FF,
            8 => CFunctionEnum::FFF,
            9 => CFunctionEnum::Getter,
            10 => CFunctionEnum::Setter,
            11 => CFunctionEnum::GetterMagic,
            12 => CFunctionEnum::SetterMagic,
            13 => CFunctionEnum::IteratorNext,
            _ => return None,
        })
    }

    /// Whether `new` is allowed on a function with this convention.
    pub fn is_constructor(self) -> bool {
        matches!(
            self,
            CFunctionEnum::Constructor
                | CFunctionEnum::ConstructorMagic
                | CFunctionEnum::ConstructorOrFunc
                | CFunctionEnum::ConstructorOrFuncMagic
                | CFunctionEnum::ConstructorOrFuncFlags
        )
    }
}

/// `JSCFunctionType`: the union of every `JSCFunction*` pointer type.
///
/// Kept as one pointer here, because the union is only ever read through the accessor the
/// surrounding `cproto` selects. Reading any variant is a pointer load; which *signature*
/// to call it with is decided by the enum, not by the union.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct JSCFunctionType {
    /// The pointer, sign- and type-erased.
    pub pointer: *const c_void,
}

impl JSCFunctionType {
    /// The same pointer with no type. Callers transmute it to the signature `cproto` names.
    pub fn as_ptr(self) -> *const c_void {
        self.pointer
    }

    /// Whether the union holds nothing.
    pub fn is_null(self) -> bool {
        self.pointer.is_null()
    }
}

// `JS_DEF_*`: what a `JSCFunctionListEntry` defines.
/// `JS_DEF_CFUNC`: a function, with `u.func` filled in.
pub const JS_DEF_CFUNC: u8 = 0;
/// `JS_DEF_CGETSET`: a getter/setter pair with no `magic`.
pub const JS_DEF_CGETSET: u8 = 1;
/// `JS_DEF_CGETSET_MAGIC`: a getter/setter pair that also receives `magic`.
pub const JS_DEF_CGETSET_MAGIC: u8 = 2;
/// `JS_DEF_PROP_STRING`.
pub const JS_DEF_PROP_STRING: u8 = 3;
/// `JS_DEF_PROP_INT32`.
pub const JS_DEF_PROP_INT32: u8 = 4;
/// `JS_DEF_PROP_INT64`.
pub const JS_DEF_PROP_INT64: u8 = 5;
/// `JS_DEF_PROP_DOUBLE`.
pub const JS_DEF_PROP_DOUBLE: u8 = 6;
/// `JS_DEF_PROP_UNDEFINED`.
pub const JS_DEF_PROP_UNDEFINED: u8 = 7;
/// `JS_DEF_OBJECT`: a nested function-list table.
pub const JS_DEF_OBJECT: u8 = 8;
/// `JS_DEF_ALIAS`.
pub const JS_DEF_ALIAS: u8 = 9;
/// `JS_DEF_PROP_ATOM`.
pub const JS_DEF_PROP_ATOM: u8 = 10;
/// `JS_DEF_PROP_BOOL`.
pub const JS_DEF_PROP_BOOL: u8 = 11;

/// The `u.func` half of [`JSCFunctionListEntry`]: a function plus its arity and convention.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct FunctionEntry {
    /// `length`.
    pub length: u8,
    /// A [`CFunctionEnum`] value.
    pub cproto: u8,
    /// Six bytes of padding, which C inserted to align `cfunc` to 8.
    ///
    /// Written out rather than left to `repr(C)` because the *next* field is a union: a
    /// struct ending in a union has the union's alignment, and the compiler would place
    /// `cfunc` at 8 anyway — but a field that is not spelled is a field whose offset is not
    /// asserted, and this file's whole premise is that every offset is asserted.
    pub _padding: [u8; 6],
    /// The body.
    pub cfunc: JSCFunctionType,
}

/// The `u.getset` half: a getter and a setter, either of which may be null.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct GetSetEntry {
    /// `u.getset.get`.
    pub get: JSCFunctionType,
    /// `u.getset.set`.
    pub set: JSCFunctionType,
}

/// The `u` union of [`JSCFunctionListEntry`].
///
/// A union in C; a struct here, because we read exactly one arm per entry and the arms all
/// start at offset 0 — with the exception of `func`, whose `cfunc` is at 8 rather than 0.
/// Modelling it as a struct of overlapping fields would need `unsafe` reads of a union for
/// no gain: the field we want is chosen by `def_type`, which is checked before anything is
/// read.
#[derive(Clone, Copy)]
#[repr(C)]
pub union FunctionListUnion {
    /// `u.func`.
    pub func: core::mem::ManuallyDrop<FunctionEntry>,
    /// `u.getset`.
    pub getset: core::mem::ManuallyDrop<GetSetEntry>,
    /// `u.alias.name`.
    pub alias_name: *const c_char,
    /// `u.alias.base`.
    pub alias_base: c_int,
    /// `u.prop_list.tab`.
    pub prop_list_tab: *const JSCFunctionListEntry,
    /// `u.prop_list.len`.
    pub prop_list_len: c_int,
    /// `u.str`.
    pub str_: *const c_char,
    /// `u.i32`, and also `u.alias.base`/`u.prop_list.len`'s storage.
    pub i32: i32,
    /// `u.i64`.
    pub i64: i64,
    /// `u.f64`.
    pub f64: f64,
    /// The raw eight bytes, for the cases where the arm is a pair of 4-byte words.
    pub raw: u64,
}

/// `JSCFunctionListEntry`: one row of a static table a C module hands us.
///
/// 32 bytes, `u` at 16. C builds these with `JS_CFUNC_DEF` and friends, so the layout is
/// fixed by the *caller's* compiler and we only read it.
#[repr(C)]
pub struct JSCFunctionListEntry {
    /// The property name.
    pub name: *const c_char,
    /// `JS_PROP_*` attribute bits.
    pub prop_flags: u8,
    /// One of the `JS_DEF_*` values.
    pub def_type: u8,
    /// The `magic` integer the definition carries.
    pub magic: i16,
    /// Four bytes C inserted to align the union to 8.
    pub _padding: [u8; 4],
    /// The payload, whose arm `def_type` selects.
    pub u: FunctionListUnion,
}

/// `JSClassFinalizer`: `void (*)(JSRuntime *rt, JSValue val)`.
pub type ClassFinalizer = Option<unsafe extern "C" fn(*mut c_void, JsValue)>;

/// `JSClassGCMark`: `void (*)(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func)`.
pub type ClassGCMark = Option<unsafe extern "C" fn(*mut c_void, JsValue, *mut c_void)>;

/// `JSClassCall`: `(ctx, func_obj, this_val, argc, argv, flags)`.
pub type ClassCall = Option<
    unsafe extern "C" fn(*mut c_void, JsValue, JsValue, c_int, *const JsValue, c_int) -> JsValue,
>;

/// `JSClassExoticMethods`: the traps a class may override.
///
/// Read field by field with null checks — a module that only wants `get_own_property` leaves
/// the rest zero, which is what `point.c`'s empty `JSClassDef` relies on.
#[repr(C)]
pub struct JSClassExoticMethods {
    /// `[[GetOwnProperty]]`.
    pub get_own_property: Option<
        unsafe extern "C" fn(*mut c_void, *mut JsValue, JsValue, c_int, *const JsValue) -> c_int,
    >,
    /// `[[DefineOwnProperty]]`.
    pub define_own_property: Option<
        unsafe extern "C" fn(
            *mut c_void,
            JsValue,
            c_int,
            *const JsValue,
            JsValue,
            *mut c_int,
        ) -> c_int,
    >,
    /// `[[HasProperty]]`.
    pub has_property:
        Option<unsafe extern "C" fn(*mut c_void, JsValue, c_int, *const JsValue) -> c_int>,
    /// `[[Delete]]`.
    pub delete_property:
        Option<unsafe extern "C" fn(*mut c_void, JsValue, c_int, *const JsValue) -> c_int>,
    /// `[[GetPrototypeOf]]`.
    pub get_prototype: Option<unsafe extern "C" fn(*mut c_void, JsValue) -> JsValue>,
    /// `[[SetPrototypeOf]]`.
    pub set_prototype: Option<unsafe extern "C" fn(*mut c_void, JsValue, JsValue) -> c_int>,
    /// `[[IsExtensible]]`.
    pub is_extensible: Option<unsafe extern "C" fn(*mut c_void, JsValue) -> c_int>,
    /// `[[PreventExtensions]]`.
    pub prevent_extensions: Option<unsafe extern "C" fn(*mut c_void, JsValue) -> c_int>,
}

/// `JSClassDef`: what `JS_NewClass` is handed.
///
/// 40 bytes: `class_name` 0, `finalizer` 8, `gc_mark` 16, `call` 24, `exotic` 32.
#[repr(C)]
pub struct JSClassDef {
    /// The class name, for diagnostics.
    pub class_name: *const c_char,
    /// Run once when an instance is released.
    pub finalizer: ClassFinalizer,
    /// `gc_mark`, for a payload that points at other JavaScript values.
    pub gc_mark: ClassGCMark,
    /// `call`, non-null when instances are callable.
    pub call: ClassCall,
    /// The exotic traps.
    pub exotic: *const JSClassExoticMethods,
}

/// `JSModuleInitFunc`: fills a module's exports in.
pub type ModuleInitFunc =
    Option<unsafe extern "C" fn(*mut crate::runtime::JsContext, *mut JsModuleDef) -> c_int>;

/// `JSCFunctionData`: `(ctx, this_val, argc, argv, magic, func_data)`.
pub type CFunctionData = Option<
    unsafe extern "C" fn(
        *mut crate::runtime::JsContext,
        JsValue,
        c_int,
        *const JsValue,
        c_int,
        *mut JsValue,
    ) -> JsValue,
>;

/// `JSCFunction`: the plain `(ctx, this_val, argc, argv)` shape.
pub type RawFunction = *const c_void;

/// `JSInterruptHandler`: return non-zero to abort the running script.
pub type InterruptHandler =
    Option<unsafe extern "C" fn(*mut crate::runtime::JsRuntime, *mut c_void) -> c_int>;

/// `JSModuleNormalizeFunc`: turn `(base_name, name)` into the module's key.
pub type ModuleNormalizeFunc = Option<
    unsafe extern "C" fn(
        *mut crate::runtime::JsContext,
        *const c_char,
        *const c_char,
        *mut c_void,
    ) -> *mut c_char,
>;

/// `JSModuleLoaderFunc`: load the module a normalised key names.
pub type ModuleLoaderFunc = Option<
    unsafe extern "C" fn(*mut crate::runtime::JsContext, *const c_char, *mut c_void) -> JsValue,
>;

/// `JSModuleLoaderFunc2`: the same, plus the import attributes.
pub type ModuleLoaderFunc2 = Option<
    unsafe extern "C" fn(
        *mut crate::runtime::JsContext,
        *const c_char,
        *mut crate::runtime::JsContext,
        *const c_char,
        *mut c_void,
    ) -> JsValue,
>;

/// `JSModuleCheckSupportedImportAttributes`: refuse an attribute set the host cannot honour.
pub type ModuleCheckAttrs = Option<
    unsafe extern "C" fn(
        *mut crate::runtime::JsContext,
        *mut crate::runtime::JsContext,
        *mut c_void,
    ) -> c_int,
>;

/// `JSJobFunc`: a job the host enqueued with `JS_EnqueueJob`.
pub type JobFunc =
    Option<unsafe extern "C" fn(*mut crate::runtime::JsContext, c_int, *const JsValue) -> JsValue>;

/// `JSHostPromiseRejectionTracker`.
pub type PromiseRejectionTracker = Option<
    unsafe extern "C" fn(*mut crate::runtime::JsContext, JsValue, JsValue, c_int, *mut c_void),
>;

/// `JSMallocFunctions`: a custom allocator for `JS_NewRuntime2`.
#[repr(C)]
pub struct JSMallocFunctions {
    /// `js_malloc`.
    pub js_malloc: Option<unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void>,
    /// `js_free`.
    pub js_free: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>,
    /// `js_realloc`.
    pub js_realloc: Option<unsafe extern "C" fn(*mut c_void, *mut c_void, usize) -> *mut c_void>,
    /// `js_malloc_usable_size`.
    pub js_malloc_usable_size: Option<unsafe extern "C" fn(*const c_void) -> usize>,
}

/// `JSPropertyEnum`: one entry of `JS_GetOwnPropertyNames`' answer.
///
/// 8 bytes, exactly as the header lays it out: `is_enumerable` is a `JS_BOOL` (4 bytes) and
/// `atom` follows at offset 4.
#[repr(C)]
pub struct JSPropertyEnum {
    /// Whether the property is enumerable, filled in when `JS_GPN_SET_ENUM` was asked for.
    pub is_enumerable: crate::cdefs::Bool,
    /// The property's atom.
    pub atom: crate::atoms::Atom,
}

/// `JSPropertyDescriptor`: one complete property, as `JS_GetOwnProperty` reports it.
///
/// 32 bytes: `flags` at 0, then three `JSValue`s at 8/16/24.
#[repr(C)]
pub struct JSPropertyDescriptor {
    /// `JS_PROP_*`: the attributes, plus `JS_PROP_GETSET` for an accessor.
    pub flags: c_int,
    /// The value, for a data property.
    pub value: JsValue,
    /// The getter, for an accessor.
    pub getter: JsValue,
    /// The setter, for an accessor.
    pub setter: JsValue,
}

/// `JSPrintValueOptions`: how `JS_PrintValue` renders a value.
#[repr(C)]
pub struct JSPrintValueOptions {
    /// `show_hidden`: `bool` in the header's bitfield, and this layer reads it as a byte.
    pub show_hidden: u8,
    /// `raw_dump`.
    pub raw_dump: u8,
    /// `max_depth`, 0 for no limit.
    pub max_depth: u32,
    /// `max_string_length`, 0 for no limit.
    pub max_string_length: u32,
    /// `max_item_count`, 0 for no limit.
    pub max_item_count: u32,
}

/// `JSPrintValueWrite`: receives the rendered text in chunks.
pub type PrintValueWrite = Option<unsafe extern "C" fn(*mut c_void, *const c_char, usize)>;

/// `JSMemoryUsage`: the shape `JS_ComputeMemoryUsage` fills in.
///
/// Field-for-field with the header so that a C program reading `s->malloc_count` reads the
/// number we wrote there. Every field is `int64_t` in the reference; the `size_t`-valued
/// ones are widened on the way out.
#[repr(C)]
#[derive(Default, Clone, Copy)]
pub struct JSMemoryUsage {
    /// Bytes the allocator holds.
    pub malloc_size: i64,
    /// Allocations outstanding.
    pub malloc_count: i64,
    /// Bytes the allocator can still hand out (0 when unlimited).
    pub memory_used_size: i64,
    /// Times the allocator was asked for memory.
    pub malloc_limit: i64,
    /// Bytes the allocator has borrowed from the system.
    pub memory_used_count: i64,
    /// Atoms interned.
    pub atom_count: i64,
    /// Bytes those atoms occupy.
    pub atom_size: i64,
    /// Strings.
    pub str_count: i64,
    /// Bytes those strings occupy.
    pub str_size: i64,
    /// Objects.
    pub obj_count: i64,
    /// Bytes those objects occupy.
    pub obj_size: i64,
    /// Properties.
    pub prop_count: i64,
    /// Bytes those properties occupy.
    pub prop_size: i64,
    /// Shapes (hidden classes).
    pub shape_count: i64,
    /// Bytes those shapes occupy.
    pub shape_size: i64,
    /// JavaScript functions.
    pub js_func_count: i64,
    /// Bytes of bytecode.
    pub js_func_size: i64,
    /// Bytecode instructions.
    pub js_func_code_size: i64,
    /// Bytecode instructions, as a running total.
    pub js_func_pc2line_count: i64,
    /// Bytes of the pc-to-line table.
    pub js_func_pc2line_size: i64,
    /// C functions.
    pub c_func_count: i64,
    /// Arrays.
    pub array_count: i64,
    /// Arrays stored as element vectors.
    pub fast_array_count: i64,
    /// Elements in those.
    pub fast_array_elements: i64,
    /// Binary objects.
    pub binary_object_count: i64,
    /// Bytes they occupy.
    pub binary_object_size: i64,
}

/// `JSModuleDef`: a C module's definition, as `JS_NewCModule` answers it.
///
/// The C side only passes it back, so the fields are ours — but it has to be a real
/// allocation with a stable address, because `JS_AddModuleExport` is called with the same
/// pointer the init function was given. It is `repr(C)` for the same reason: nothing on the
/// C side looks inside, and a plain Rust layout is all it needs.
#[repr(C)]
pub struct JsModuleDef {
    /// The module name, as `JS_NewCModule` received it.
    pub name: String,
    /// The exports added with `JS_AddModuleExport`/`JS_AddModuleExportList`.
    pub declared: Vec<String>,
    /// The values set with `JS_SetModuleExport`.
    ///
    /// Engine values, not `JSValue`s: `JS_SetModuleExport` consumes the reference it is
    /// given (as the C API says it does), so what is kept is the engine's own handle. That
    /// keeps the module definition out of the reference-counting business entirely, and a
    /// definition therefore needs no `Drop`.
    pub values: Vec<(String, engine::Value)>,
    /// The function that fills the exports in, run when the module is instantiated.
    pub init: ModuleInitFunc,
    /// `JS_SetModulePrivateValue`'s value, which a host uses to reach its own data from a
    /// module's callbacks.
    pub private: Option<engine::Value>,
}

const _: () = {
    use core::mem::{align_of, offset_of, size_of};
    assert!(size_of::<JSCFunctionListEntry>() == 32);
    assert!(offset_of!(JSCFunctionListEntry, name) == 0);
    assert!(offset_of!(JSCFunctionListEntry, prop_flags) == 8);
    assert!(offset_of!(JSCFunctionListEntry, def_type) == 9);
    assert!(offset_of!(JSCFunctionListEntry, magic) == 10);
    assert!(offset_of!(JSCFunctionListEntry, u) == 16);
    assert!(offset_of!(FunctionEntry, length) == 0);
    assert!(offset_of!(FunctionEntry, cproto) == 1);
    assert!(offset_of!(FunctionEntry, cfunc) == 8);
    assert!(align_of::<JSCFunctionListEntry>() == 8);
    assert!(size_of::<JSClassDef>() == 40);
    assert!(offset_of!(JSClassDef, class_name) == 0);
    assert!(offset_of!(JSClassDef, finalizer) == 8);
    assert!(offset_of!(JSClassDef, gc_mark) == 16);
    assert!(offset_of!(JSClassDef, call) == 24);
    assert!(offset_of!(JSClassDef, exotic) == 32);
    assert!(size_of::<JSCFunctionType>() == 8);
    assert!(size_of::<ClassId>() == 4);
};