shape-vm 0.3.1

Stack-based bytecode virtual machine for the Shape programming language
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! VM executor handlers for v2 typed array opcodes.
//!
//! These handlers operate on `TypedArray<T>` raw pointers (`*mut TypedArray<T>`),
//! a v2-raw HeapHeader-equipped flat struct (24-byte `repr(C)`, refcount at
//! offset 0). Pointer bits flow through the kinded API as
//! `NativeKind::Ptr(HeapKind::TypedArray)` — the kind track itself is the
//! carrier discriminator, separating a `*mut TypedArray<T>` pointer from a
//! genuine scalar `u64`. Element kinds:
//!   F64  -> `NativeKind::Float64`
//!   I64  -> `NativeKind::Int64`
//!   I32  -> `NativeKind::Int32`
//!   Bool -> `NativeKind::Bool`
//!
//! ## r5c-2-β-CKPT-C u64-carrier-disambiguation (2026-05-20)
//!
//! The `NewTypedArray*` producers below previously stamped the carrier with
//! the bare `NativeKind::UInt64` kind — the SAME kind that labels a genuine
//! scalar `u64`. That overload made `as_v2_typed_array(bits, kind)`
//! dereference an arbitrary scalar `u64` value (e.g. `u64::MAX`) as a
//! `*const HeapHeader`, SIGSEGV-ing on `print(x)` for `let x: u64 = ...`.
//! The fix migrates the v2-typed-array POINTER carrier to
//! `NativeKind::Ptr(HeapKind::TypedArray)` (ordinal 8, already extant),
//! completing the Family-α struct-field / closure-capture migration. The
//! kind track now discriminates: `Ptr(HeapKind::TypedArray)` is the array
//! carrier, `UInt64` is a scalar. `clone_with_kind` / `drop_with_kind`
//! route the carrier through `retain_v2_typed_array` /
//! `release_v2_typed_array` (the array starts at refcount 1 from
//! `with_capacity`'s `HeapHeader::new`), giving the direct carrier the same
//! RAII discipline as the refcounted struct-field / closure-capture carrier.
//!
//! ADR-006 §2.3 / §2.7.7 / Wave 6.5 cluster C.
//!
//! ## W16.2-J.2 macro-generated per-kind arms (2026-05-22)
//!
//! Per `docs/cluster-audits/v0.3-w16-2-j-audit.md` §2.C + §6.A: the
//! per-kind opcode arms (TypedArrayKind variants × 4 ops:
//! New/Get/Push/Set) are macro-generated via `macro_rules!` rather than
//! hand-written. A single `define_exec_v2_typed_array!` macro takes three
//! repetition groups (scalar rows, then char rows, then heap rows) and
//! emits the entire `impl VirtualMachine { fn exec_v2_typed_array(…) … }`
//! in one non-recursive expansion. The whole-impl shape sidesteps two
//! `macro_rules!` constraints: (a) macros in match-arm position must
//! produce all arms of the match in a single expansion (no chained
//! macro-arm-producers), and (b) TT-muncher recursion blows the
//! recursion limit because each step re-parses the accumulator
//! quadratically.
//!
//! Three row shapes cover the matrix:
//!
//! - `scalar` rows  — `Copy + Sized` element types with explicit
//!   bit↔value encode/decode expressions: F64, I64, I32, Bool, I8, U8,
//!   I16, U16, U32, F32. Same shape as the original hand-written arms;
//!   the macro just unrolls the four-op template per kind.
//!
//! - `char` rows    — Char element, push/set go through
//!   `char::from_u32` (rejects surrogates + out-of-range codepoints)
//!   and surface VMError::RuntimeError on invalid bits.
//!
//! - `heap` rows    — heap-element kinds with per-element refcount
//!   discipline (String/Decimal/TypedObject). Element-read retains the
//!   per-element HeapHeader before pushing; element-write/push transfers
//!   the caller's refcount share to the array; element-set additionally
//!   releases the prior element's share.
//!
//! ADR-006 §2.7.5 stamp-at-compile-time discipline preserved: each arm
//! stamps the element-type metadata at the allocation/push/get/set
//! producer site; the kind is encoded in the opcode name (and the
//! associated macro row), never decoded at runtime. Zero-tag runtime
//! invariant preserved — no `is_tagged()`, no `tag_bits`, no `ValueWord`
//! shim, no dynamic fallback.
//!
//! Semantically equivalent to the pre-macro hand-written arms (same
//! pop/push order, same refcount discipline, same error surfaces).
//! No external proc-macro crate (e.g. `paste`) is used — the OpCode
//! variant names are passed explicitly per macro row so concatenation
//! is never required.

use crate::bytecode::{Instruction, OpCode, Operand};
use crate::executor::vm_impl::stack::drop_with_kind;
use shape_value::heap_value::TypedObjectStorage;
use shape_value::v2::decimal_obj::DecimalObj;
use shape_value::v2::heap_element::HeapElement;
use shape_value::v2::refcount::v2_retain;
use shape_value::v2::string_obj::StringObj;
use shape_value::v2::typed_array::TypedArray;
use shape_value::{HeapKind, NativeKind, VMError};

use super::super::VirtualMachine;
use super::v2_array_detect::{
    ELEM_TYPE_BOOL, ELEM_TYPE_CHAR, ELEM_TYPE_DECIMAL, ELEM_TYPE_F32, ELEM_TYPE_F64, ELEM_TYPE_I16,
    ELEM_TYPE_I32, ELEM_TYPE_I64, ELEM_TYPE_I8, ELEM_TYPE_STRING, ELEM_TYPE_TYPED_OBJECT,
    ELEM_TYPE_U16, ELEM_TYPE_U32, ELEM_TYPE_U8, stamp_elem_type,
};

// ─────────────────────────────────────────────────────────────────────────
// W16.2-J.2 macro — per-kind typed-array opcode arms (audit §2.C / §6.A)
// ─────────────────────────────────────────────────────────────────────────
//
// The macro has a single non-recursive arm that accepts three repetition
// groups in fixed order:
//   1. scalar rows  — emitted first in the outer match
//   2. char rows    — emitted next
//   3. heap rows    — emitted last
//
// All three groups expand inside the same `match` body, producing the
// 56 per-kind arms (14 kinds × 4 ops). Non-repetition opcodes
// (`NewStringV2`, `NewDecimalV2`, `TypedArrayLen`) live in the macro
// body's trailer.
//
// The opcode name encodes the kind statically (ADR-006 §2.7.5 producer-
// side stamp); the kind is NEVER decoded at runtime. NO tagged dispatch,
// NO ValueWord, NO dynamic fallback. ALL arms transfer the array
// pointer through `pop_kinded` / `push_kinded` with
// `NativeKind::Ptr(HeapKind::TypedArray)`, and route the array's RAII
// through `drop_with_kind`.

macro_rules! define_exec_v2_typed_array {
    (
        scalar_rows: [
            $(
                {
                    ops: $s_new:ident / $s_get:ident / $s_push:ident / $s_set:ident,
                    storage: $s_storage:ty,
                    elem_tag: $s_tag:expr,
                    result_kind: $s_kind:expr,
                    decode( $s_vb:ident ) => $s_decode:expr,
                    encode( $s_v:ident : $s_vt:ty ) => $s_encode:expr,
                }
            )+
        ],
        char_rows: [
            $(
                {
                    ops: $c_new:ident / $c_get:ident / $c_push:ident / $c_set:ident,
                    elem_tag: $c_tag:expr,
                    result_kind: $c_kind:expr,
                    push_label: $c_push_label:expr,
                    set_label: $c_set_label:expr,
                }
            )+
        ],
        heap_rows: [
            $(
                {
                    ops: $h_new:ident / $h_get:ident / $h_push:ident / $h_set:ident,
                    heap_obj: $h_obj:ty,
                    elem_kind: $h_kind:expr,
                    elem_tag: $h_tag:expr,
                    err_label: $h_err:expr,
                }
            )+
        ],
    ) => {
        impl VirtualMachine {
            /// Execute a v2 typed array opcode.
            pub(crate) fn exec_v2_typed_array(
                &mut self,
                instruction: &Instruction,
            ) -> Result<(), VMError> {
                match instruction.opcode {
                    // ─── W16.2-J.2 macro-generated per-kind arms ───────
                    //
                    // 14 kinds × 4 ops (New / Get / Push / Set) = 56
                    // arms total:
                    //   - 10 scalar  rows: F64, I64, I32, Bool, I8, U8,
                    //                      I16, U16, U32, F32 (40 arms)
                    //   -  1 char    row : Char (4 arms)
                    //   -  3 heap    rows: String, Decimal, TypedObject
                    //                      (12 arms)
                    //
                    // Sign-extension (I8/I16/I32) and zero-extension
                    // (U8/U16/U32) preserve the value's semantic when the
                    // i64 slot is later decoded by `decode_i64` per
                    // kind. F32 bits ride the low 32 bits of the slot;
                    // Bool collapses any nonzero source-bit to 1. Char
                    // goes through `char::from_u32` on push/set (rejects
                    // surrogates + out-of-range codepoints). Heap-
                    // element kinds (String/Decimal/TypedObject) carry
                    // per-element refcount discipline (retain on Get,
                    // transfer on Push, release-prior on Set).
                    //
                    // U64 typed-array opcode handlers intentionally NOT
                    // minted — see opcode_defs.rs comment block. The
                    // S1.5 sub-cluster re-mints
                    // OpCode::{New,Get,Push,Set}TypedArrayU64 + their
                    // handler bodies once the §2.7.7/Q9 NativeKind
                    // discriminator for "pointer to TypedArray<T>" vs
                    // "scalar u64" lands.
                    $(
                        OpCode::$s_new => {
                            let cap = match instruction.operand {
                                Some(Operand::Count(n)) => n as u32,
                                _ => 0,
                            };
                            let ptr = TypedArray::<$s_storage>::with_capacity(cap);
                            // ADR-006 §2.7.5 producer-side stamp.
                            unsafe { stamp_elem_type(ptr as *mut u8, $s_tag) };
                            self.push_kinded(
                                ptr as usize as u64,
                                NativeKind::Ptr(HeapKind::TypedArray),
                            )?;
                            Ok(())
                        }
                        OpCode::$s_get => {
                            let (idx_bits, _idx_kind) = self.pop_kinded()?;
                            let index = idx_bits as i64 as u32;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr = arr_bits as usize as *const TypedArray<$s_storage>;
                            let len = unsafe { TypedArray::len(arr) };
                            let $s_v: $s_vt = unsafe {
                                TypedArray::get(arr, index).ok_or(
                                    VMError::IndexOutOfBounds {
                                        index: index as i32,
                                        length: len as usize,
                                    },
                                )?
                            };
                            drop_with_kind(arr_bits, arr_kind);
                            // ADR-006 §2.7.5: result kind statically
                            // pinned by the opcode name.
                            self.push_kinded($s_encode, $s_kind)?;
                            Ok(())
                        }
                        OpCode::$s_push => {
                            let ($s_vb, _vk) = self.pop_kinded()?;
                            let val: $s_storage = $s_decode;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr = arr_bits as usize as *mut TypedArray<$s_storage>;
                            unsafe { TypedArray::push(arr, val); }
                            drop_with_kind(arr_bits, arr_kind);
                            Ok(())
                        }
                        OpCode::$s_set => {
                            let ($s_vb, _vk) = self.pop_kinded()?;
                            let val: $s_storage = $s_decode;
                            let (idx_bits, _ik) = self.pop_kinded()?;
                            let index = idx_bits as i64 as u32;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr = arr_bits as usize as *mut TypedArray<$s_storage>;
                            unsafe { TypedArray::set(arr, index, val); }
                            drop_with_kind(arr_bits, arr_kind);
                            Ok(())
                        }
                    )+

                    $(
                        OpCode::$c_new => {
                            let cap = match instruction.operand {
                                Some(Operand::Count(n)) => n as u32,
                                _ => 0,
                            };
                            let ptr = TypedArray::<char>::with_capacity(cap);
                            unsafe { stamp_elem_type(ptr as *mut u8, $c_tag) };
                            self.push_kinded(
                                ptr as usize as u64,
                                NativeKind::Ptr(HeapKind::TypedArray),
                            )?;
                            Ok(())
                        }
                        OpCode::$c_get => {
                            let (idx_bits, _idx_kind) = self.pop_kinded()?;
                            let index = idx_bits as i64 as u32;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr = arr_bits as usize as *const TypedArray<char>;
                            let len = unsafe { TypedArray::len(arr) };
                            let val = unsafe {
                                TypedArray::get(arr, index).ok_or(
                                    VMError::IndexOutOfBounds {
                                        index: index as i32,
                                        length: len as usize,
                                    },
                                )?
                            };
                            drop_with_kind(arr_bits, arr_kind);
                            // Codepoint pushed as inline bits per
                            // §2.7.6/Q8 KindedSlot::from_char shape.
                            self.push_kinded(val as u32 as u64, $c_kind)?;
                            Ok(())
                        }
                        OpCode::$c_push => {
                            let (val_bits, _vk) = self.pop_kinded()?;
                            // Codepoint validity check —
                            // `char::from_u32` rejects surrogates and
                            // out-of-range values; a corrupt slot
                            // surfaces a structured error instead of
                            // panicking on `unwrap`.
                            let val = char::from_u32(val_bits as u32).ok_or_else(|| {
                                VMError::RuntimeError(format!(
                                    "{}: invalid char codepoint 0x{:X}",
                                    $c_push_label, val_bits as u32
                                ))
                            })?;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr = arr_bits as usize as *mut TypedArray<char>;
                            unsafe { TypedArray::push(arr, val); }
                            drop_with_kind(arr_bits, arr_kind);
                            Ok(())
                        }
                        OpCode::$c_set => {
                            let (val_bits, _vk) = self.pop_kinded()?;
                            let val = char::from_u32(val_bits as u32).ok_or_else(|| {
                                VMError::RuntimeError(format!(
                                    "{}: invalid char codepoint 0x{:X}",
                                    $c_set_label, val_bits as u32
                                ))
                            })?;
                            let (idx_bits, _ik) = self.pop_kinded()?;
                            let index = idx_bits as i64 as u32;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr = arr_bits as usize as *mut TypedArray<char>;
                            unsafe { TypedArray::set(arr, index, val); }
                            drop_with_kind(arr_bits, arr_kind);
                            Ok(())
                        }
                    )+

                    $(
                        OpCode::$h_new => {
                            let cap = match instruction.operand {
                                Some(Operand::Count(n)) => n as u32,
                                _ => 0,
                            };
                            let ptr =
                                TypedArray::<*const $h_obj>::with_capacity(cap);
                            unsafe { stamp_elem_type(ptr as *mut u8, $h_tag) };
                            self.push_kinded(
                                ptr as usize as u64,
                                NativeKind::Ptr(HeapKind::TypedArray),
                            )?;
                            Ok(())
                        }
                        OpCode::$h_get => {
                            let (idx_bits, _idx_kind) = self.pop_kinded()?;
                            let index = idx_bits as i64 as u32;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr =
                                arr_bits as usize as *const TypedArray<*const $h_obj>;
                            let len = unsafe { TypedArray::len(arr) };
                            let elem_ptr = unsafe {
                                TypedArray::<*const $h_obj>::get(arr, index).ok_or(
                                    VMError::IndexOutOfBounds {
                                        index: index as i32,
                                        length: len as usize,
                                    },
                                )?
                            };
                            // Retain the per-element header: array
                            // keeps its share, caller gets a fresh
                            // share released via the $h_kind arm in
                            // drop_with_kind.
                            unsafe { v2_retain(&(*elem_ptr).header) };
                            drop_with_kind(arr_bits, arr_kind);
                            self.push_kinded(elem_ptr as u64, $h_kind)?;
                            Ok(())
                        }
                        OpCode::$h_push => {
                            let (val_bits, val_kind) = self.pop_kinded()?;
                            if val_kind != $h_kind {
                                return Err(VMError::RuntimeError(format!(
                                    "{}: expected {:?}, got {:?}",
                                    $h_err, $h_kind, val_kind
                                )));
                            }
                            let val = val_bits as usize as *const $h_obj;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr =
                                arr_bits as usize as *mut TypedArray<*const $h_obj>;
                            // Caller transfers their refcount share to
                            // the array (no retain).
                            unsafe { TypedArray::push(arr, val); }
                            drop_with_kind(arr_bits, arr_kind);
                            Ok(())
                        }
                        OpCode::$h_set => {
                            let (val_bits, val_kind) = self.pop_kinded()?;
                            if val_kind != $h_kind {
                                return Err(VMError::RuntimeError(format!(
                                    "{}: expected {:?}, got {:?}",
                                    $h_err, $h_kind, val_kind
                                )));
                            }
                            let val = val_bits as usize as *const $h_obj;
                            let (idx_bits, _ik) = self.pop_kinded()?;
                            let index = idx_bits as i64 as u32;
                            let (arr_bits, arr_kind) = self.pop_kinded()?;
                            let arr =
                                arr_bits as usize as *mut TypedArray<*const $h_obj>;
                            unsafe {
                                let old_ptr =
                                    TypedArray::<*const $h_obj>::get_unchecked(arr, index);
                                <$h_obj as HeapElement>::release_elem(old_ptr);
                                TypedArray::set(arr, index, val);
                            }
                            drop_with_kind(arr_bits, arr_kind);
                            Ok(())
                        }
                    )+

                    // ── Wave 3 Stabilize Round 1 V3-A2-followup-producer-cascade (2026-05-15) ──
                    //
                    // v2-raw heap-element literal constructors. Read the
                    // source value from the program constant / string
                    // pool, allocate a fresh `StringObj` / `DecimalObj`
                    // (refcount = 1), push the raw pointer bits with
                    // `NativeKind::StringV2` / `NativeKind::DecimalV2`.
                    // The caller's share is then transferred to the
                    // typed array on the subsequent
                    // `TypedArrayPushString` / `TypedArrayPushDecimal`
                    // (matches the per-element refcount discipline of
                    // the heap-row Get/Push/Set arms above).
                    //
                    // Per ADR-006 §2.7.5 stamp-at-compile-time: the
                    // kind is proven at compile-time emission; no
                    // runtime decode/probe at the FFI boundary.

                    OpCode::NewStringV2 => {
                        let str_id = match instruction.operand {
                            Some(Operand::Property(id)) => id as usize,
                            Some(Operand::Const(id)) => id as usize,
                            _ => {
                                return Err(VMError::RuntimeError(
                                    "NewStringV2 requires a Property/Const string-id operand"
                                        .to_string(),
                                ));
                            }
                        };
                        let s = self
                            .program
                            .strings
                            .get(str_id)
                            .ok_or_else(|| {
                                VMError::RuntimeError(format!(
                                    "NewStringV2: string id {} out of bounds (pool len = {})",
                                    str_id,
                                    self.program.strings.len()
                                ))
                            })?
                            .clone();
                        let ptr = StringObj::new(&s);
                        self.push_kinded(ptr as usize as u64, NativeKind::StringV2)?;
                        Ok(())
                    }

                    OpCode::NewDecimalV2 => {
                        let const_id = match instruction.operand {
                            Some(Operand::Const(id)) => id as usize,
                            _ => {
                                return Err(VMError::RuntimeError(
                                    "NewDecimalV2 requires a Const constant-id operand"
                                        .to_string(),
                                ));
                            }
                        };
                        let constant = self.program.constants.get(const_id).ok_or_else(|| {
                            VMError::RuntimeError(format!(
                                "NewDecimalV2: constant id {} out of bounds (pool len = {})",
                                const_id,
                                self.program.constants.len()
                            ))
                        })?;
                        let d = match constant {
                            crate::bytecode::Constant::Decimal(d) => *d,
                            other => {
                                return Err(VMError::RuntimeError(format!(
                                    "NewDecimalV2: expected Constant::Decimal, got {:?}",
                                    other
                                )));
                            }
                        };
                        let ptr = DecimalObj::new(d);
                        self.push_kinded(ptr as usize as u64, NativeKind::DecimalV2)?;
                        Ok(())
                    }

                    // ── Length ────────────────────────────────────────

                    OpCode::TypedArrayLen => {
                        let (arr_bits, arr_kind) = self.pop_kinded()?;
                        let arr = arr_bits as usize as *const TypedArray<u8>;
                        // len field is at a fixed offset regardless of T —
                        // safe to read via any T.
                        let len = unsafe { TypedArray::len(arr) };
                        drop_with_kind(arr_bits, arr_kind);
                        self.push_kinded(len as u64, NativeKind::Int64)?;
                        Ok(())
                    }

                    _ => Err(VMError::NotImplemented(format!(
                        "v2 typed array opcode {:?} not implemented",
                        instruction.opcode
                    ))),
                }
            }
        }
    };
}

// ─────────────────────────────────────────────────────────────────────────
// Macro invocation — emits `impl VirtualMachine { fn exec_v2_typed_array
// … }` with all per-kind arms inlined.
// ─────────────────────────────────────────────────────────────────────────

define_exec_v2_typed_array! {
    scalar_rows: [
        {
            ops: NewTypedArrayF64 / TypedArrayGetF64
                / TypedArrayPushF64 / TypedArraySetF64,
            storage: f64,
            elem_tag: ELEM_TYPE_F64,
            result_kind: NativeKind::Float64,
            decode(vb) => f64::from_bits(vb),
            encode(v: f64) => v.to_bits(),
        }
        {
            ops: NewTypedArrayI64 / TypedArrayGetI64
                / TypedArrayPushI64 / TypedArraySetI64,
            storage: i64,
            elem_tag: ELEM_TYPE_I64,
            result_kind: NativeKind::Int64,
            decode(vb) => vb as i64,
            encode(v: i64) => v as u64,
        }
        {
            ops: NewTypedArrayI32 / TypedArrayGetI32
                / TypedArrayPushI32 / TypedArraySetI32,
            storage: i32,
            elem_tag: ELEM_TYPE_I32,
            result_kind: NativeKind::Int32,
            decode(vb) => vb as i64 as i32,
            encode(v: i32) => v as i64 as u64,
        }
        {
            ops: NewTypedArrayBool / TypedArrayGetBool
                / TypedArrayPushBool / TypedArraySetBool,
            storage: u8,
            elem_tag: ELEM_TYPE_BOOL,
            result_kind: NativeKind::Bool,
            decode(vb) => if vb != 0 { 1u8 } else { 0u8 },
            encode(v: u8) => (v != 0) as u64,
        }
        {
            ops: NewTypedArrayI8 / TypedArrayGetI8
                / TypedArrayPushI8 / TypedArraySetI8,
            storage: i8,
            elem_tag: ELEM_TYPE_I8,
            result_kind: NativeKind::Int8,
            decode(vb) => vb as i64 as i8,
            encode(v: i8) => v as i64 as u64,
        }
        {
            ops: NewTypedArrayU8 / TypedArrayGetU8
                / TypedArrayPushU8 / TypedArraySetU8,
            storage: u8,
            elem_tag: ELEM_TYPE_U8,
            result_kind: NativeKind::UInt8,
            decode(vb) => vb as u8,
            encode(v: u8) => v as u64,
        }
        {
            ops: NewTypedArrayI16 / TypedArrayGetI16
                / TypedArrayPushI16 / TypedArraySetI16,
            storage: i16,
            elem_tag: ELEM_TYPE_I16,
            result_kind: NativeKind::Int16,
            decode(vb) => vb as i64 as i16,
            encode(v: i16) => v as i64 as u64,
        }
        {
            ops: NewTypedArrayU16 / TypedArrayGetU16
                / TypedArrayPushU16 / TypedArraySetU16,
            storage: u16,
            elem_tag: ELEM_TYPE_U16,
            result_kind: NativeKind::UInt16,
            decode(vb) => vb as u16,
            encode(v: u16) => v as u64,
        }
        {
            ops: NewTypedArrayU32 / TypedArrayGetU32
                / TypedArrayPushU32 / TypedArraySetU32,
            storage: u32,
            elem_tag: ELEM_TYPE_U32,
            result_kind: NativeKind::UInt32,
            decode(vb) => vb as u32,
            encode(v: u32) => v as u64,
        }
        {
            ops: NewTypedArrayF32 / TypedArrayGetF32
                / TypedArrayPushF32 / TypedArraySetF32,
            storage: f32,
            elem_tag: ELEM_TYPE_F32,
            result_kind: NativeKind::Float32,
            decode(vb) => f32::from_bits(vb as u32),
            encode(v: f32) => v.to_bits() as u64,
        }
    ],
    char_rows: [
        {
            ops: NewTypedArrayChar / TypedArrayGetChar
                / TypedArrayPushChar / TypedArraySetChar,
            elem_tag: ELEM_TYPE_CHAR,
            result_kind: NativeKind::Char,
            push_label: "TypedArrayPushChar",
            set_label: "TypedArraySetChar",
        }
    ],
    heap_rows: [
        {
            ops: NewTypedArrayString / TypedArrayGetString
                / TypedArrayPushString / TypedArraySetString,
            heap_obj: StringObj,
            elem_kind: NativeKind::StringV2,
            elem_tag: ELEM_TYPE_STRING,
            err_label: "TypedArrayPush/SetString",
        }
        {
            ops: NewTypedArrayDecimal / TypedArrayGetDecimal
                / TypedArrayPushDecimal / TypedArraySetDecimal,
            heap_obj: DecimalObj,
            elem_kind: NativeKind::DecimalV2,
            elem_tag: ELEM_TYPE_DECIMAL,
            err_label: "TypedArrayPush/SetDecimal",
        }
        {
            ops: NewTypedArrayTypedObject / TypedArrayGetTypedObject
                / TypedArrayPushTypedObject / TypedArraySetTypedObject,
            heap_obj: TypedObjectStorage,
            elem_kind: NativeKind::Ptr(HeapKind::TypedObject),
            elem_tag: ELEM_TYPE_TYPED_OBJECT,
            err_label: "TypedArrayPush/SetTypedObject",
        }
    ],
}