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
//! HashMap method tests — bytecode-level integration tests for all HashMap methods.
//!
//! Tests use the legacy stack-based CallMethod convention:
//!   push receiver, push args..., push method_name, push arg_count, CallMethod

use super::*;
use shape_value::{ValueWord, ValueWordExt};
use std::sync::Arc;

fn nb_str(s: &str) -> ValueWord {
    ValueWord::from_string(Arc::new(s.to_string()))
}

/// Build a test HashMap: {"a": 1, "b": 2, "c": 3}
fn test_hashmap() -> ValueWord {
    let keys = vec![nb_str("a"), nb_str("b"), nb_str("c")];
    let values = vec![
        ValueWord::from_i64(1),
        ValueWord::from_i64(2),
        ValueWord::from_i64(3),
    ];
    ValueWord::from_hashmap_pairs(keys, values)
}

// ===== get =====

#[test]
fn test_hashmap_get_existing_key() {
    // {"a":1, "b":2, "c":3}.get("b") => 2
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // HashMap
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "b"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // "get"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // 1 arg
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("b".to_string()),
        Constant::String("get".to_string()),
        Constant::Number(1.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(2));
}

#[test]
fn test_hashmap_get_missing_key() {
    // {"a":1, "b":2, "c":3}.get("z") => None
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "z"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))),
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("z".to_string()),
        Constant::String("get".to_string()),
        Constant::Number(1.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert!(result.is_none());
}

// ===== set =====

#[test]
fn test_hashmap_set_new_key() {
    // {"a":1, "b":2, "c":3}.set("d", 4).len() => 4
    let instructions = vec![
        // set("d", 4)
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // HashMap
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "d"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 4
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // "set"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(4))), // 2 args
        Instruction::simple(OpCode::CallMethod),
        // .len()
        Instruction::new(OpCode::PushConst, Some(Operand::Const(5))), // "len"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(6))), // 0 args
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("d".to_string()),
        Constant::Number(4.0),
        Constant::String("set".to_string()),
        Constant::Number(2.0),
        Constant::String("len".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(4));
}

#[test]
fn test_hashmap_set_existing_key() {
    // {"a":1, "b":2, "c":3}.set("b", 99).get("b") => 99
    let instructions = vec![
        // set("b", 99)
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "b"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 99
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // "set"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(4))), // 2 args
        Instruction::simple(OpCode::CallMethod),
        // .get("b")
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "b"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(5))), // "get"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(6))), // 1 arg
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("b".to_string()),
        Constant::Number(99.0),
        Constant::String("set".to_string()),
        Constant::Number(2.0),
        Constant::String("get".to_string()),
        Constant::Number(1.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.to_number().unwrap(), 99.0);
}

// ===== has =====

#[test]
fn test_hashmap_has_existing() {
    // {"a":1, "b":2, "c":3}.has("a") => true
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "a"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))),
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("a".to_string()),
        Constant::String("has".to_string()),
        Constant::Number(1.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_bool(), Some(true));
}

#[test]
fn test_hashmap_has_missing() {
    // {"a":1}.has("z") => false
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "z"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))),
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("z".to_string()),
        Constant::String("has".to_string()),
        Constant::Number(1.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_bool(), Some(false));
}

// ===== delete =====

#[test]
fn test_hashmap_delete() {
    // {"a":1, "b":2, "c":3}.delete("b").len() => 2
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "b"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // "delete"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // 1 arg
        Instruction::simple(OpCode::CallMethod),
        // .len()
        Instruction::new(OpCode::PushConst, Some(Operand::Const(4))), // "len"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(5))), // 0 args
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("b".to_string()),
        Constant::String("delete".to_string()),
        Constant::Number(1.0),
        Constant::String("len".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(2));
}

#[test]
fn test_hashmap_delete_missing_key() {
    // {"a":1, "b":2, "c":3}.delete("z").len() => 3 (unchanged)
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "z"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))),
        Instruction::simple(OpCode::CallMethod),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(4))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(5))),
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("z".to_string()),
        Constant::String("delete".to_string()),
        Constant::Number(1.0),
        Constant::String("len".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(3));
}

// ===== keys =====

#[test]
fn test_hashmap_keys() {
    // {"a":1, "b":2, "c":3}.keys().length => 3
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "keys"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 0 args
        Instruction::simple(OpCode::CallMethod),
        // .length
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // "length"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 0 args
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("keys".to_string()),
        Constant::Number(0.0),
        Constant::String("length".to_string()),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(3));
}

// ===== values =====

#[test]
fn test_hashmap_values() {
    // {"a":1, "b":2, "c":3}.values().length => 3
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "values"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 0 args
        Instruction::simple(OpCode::CallMethod),
        // .length
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // "length"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 0 args
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("values".to_string()),
        Constant::Number(0.0),
        Constant::String("length".to_string()),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(3));
}

// ===== entries =====

#[test]
fn test_hashmap_entries() {
    // {"a":1, "b":2, "c":3}.entries().length => 3
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "entries"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 0 args
        Instruction::simple(OpCode::CallMethod),
        // .length
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // "length"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 0 args
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("entries".to_string()),
        Constant::Number(0.0),
        Constant::String("length".to_string()),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(3));
}

// ===== len =====

#[test]
fn test_hashmap_len() {
    // {"a":1, "b":2, "c":3}.len() => 3
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "len"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 0 args
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("len".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(3));
}

#[test]
fn test_hashmap_len_empty() {
    // HashMap().len() => 0
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(ValueWord::empty_hashmap()),
        Constant::String("len".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(0));
}

// ===== isEmpty =====

#[test]
fn test_hashmap_is_empty_true() {
    // HashMap().isEmpty() => true
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(ValueWord::empty_hashmap()),
        Constant::String("isEmpty".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_bool(), Some(true));
}

#[test]
fn test_hashmap_is_empty_false() {
    // {"a":1}.isEmpty() => false
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("isEmpty".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_bool(), Some(false));
}

// ===== Method chaining =====

#[test]
fn test_hashmap_set_then_get() {
    // HashMap().set("x", 42).get("x") => 42
    let instructions = vec![
        // set("x", 42)
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // empty HashMap
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "x"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 42
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // "set"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(4))), // 2 args
        Instruction::simple(OpCode::CallMethod),
        // .get("x")
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "x"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(5))), // "get"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(6))), // 1 arg
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(ValueWord::empty_hashmap()),
        Constant::String("x".to_string()),
        Constant::Number(42.0),
        Constant::String("set".to_string()),
        Constant::Number(2.0),
        Constant::String("get".to_string()),
        Constant::Number(1.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.to_number().unwrap(), 42.0);
}

#[test]
fn test_hashmap_delete_then_has() {
    // {"a":1, "b":2, "c":3}.delete("a").has("a") => false
    let instructions = vec![
        // .delete("a")
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "a"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // "delete"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // 1 arg
        Instruction::simple(OpCode::CallMethod),
        // .has("a")
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "a"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(4))), // "has"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // 1 arg
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("a".to_string()),
        Constant::String("delete".to_string()),
        Constant::Number(1.0),
        Constant::String("has".to_string()),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_bool(), Some(false));
}

// ===== Integer keys =====

#[test]
fn test_hashmap_integer_keys() {
    // HashMap with integer keys: {1: "one", 2: "two"}
    let keys = vec![ValueWord::from_i64(1), ValueWord::from_i64(2)];
    let values = vec![nb_str("one"), nb_str("two")];
    let hm = ValueWord::from_hashmap_pairs(keys, values);

    // .get(2) => "two"
    let instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // 2
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // "get"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // 1 arg
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(hm),
        Constant::Int(2),
        Constant::String("get".to_string()),
        Constant::Number(1.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_str().unwrap(), "two");
}

// ===== B5: per-VM shape table =====

/// Each VirtualMachine owns its own ShapeTableHandle; shape transitions
/// triggered during HashMap operations land in *that* VM's table, not
/// in a shared global. Verifies the per-VM isolation promise of the
/// post-B5 architecture.
#[test]
fn test_hashmap_shape_table_is_per_vm() {
    use crate::VMConfig;
    use crate::executor::VirtualMachine;
    use shape_value::{SyncShapeTableScope, hash_property_name, shape_transition};

    let vm_a = VirtualMachine::new(VMConfig::default());
    let vm_b = VirtualMachine::new(VMConfig::default());

    // Each VM starts with a fresh root-only transition table.
    assert_eq!(vm_a.shape_table.table().lock().unwrap().shape_count(), 1);
    assert_eq!(vm_b.shape_table.table().lock().unwrap().shape_count(), 1);

    // Install VM A's handle as the ambient shape table and drive a
    // transition through the public free-function API. This is the
    // exact code path that HashMapData::compute_shape,
    // HashMapData::shape_get, and the JIT FFI's jit_v2_map_set_str_i64
    // go through at runtime.
    {
        let _scope = SyncShapeTableScope::enter(vm_a.shape_table.clone());
        let root = shape_value::ShapeTransitionTable::root();
        let _ = shape_transition(root, hash_property_name("x"));
        let _ = shape_transition(root, hash_property_name("y"));
    }

    // VM A's table advanced; VM B's is untouched.
    let a_count = vm_a.shape_table.table().lock().unwrap().shape_count();
    let b_count = vm_b.shape_table.table().lock().unwrap().shape_count();
    assert!(
        a_count >= 3, // root + x + y
        "vm_a shape table should have grown via the ambient handle (count={a_count})",
    );
    assert_eq!(
        b_count, 1,
        "vm_b shape table should be unaffected by transitions made under \
         vm_a's scope (count={b_count})",
    );
}

// ===== Immutability =====

#[test]
fn test_hashmap_set_is_immutable() {
    // Original HashMap should not be modified by set()
    // hm = {"a":1, "b":2, "c":3}
    // hm.set("d", 4)  -- returns new, doesn't modify original
    // hm.len() => 3 (original unchanged)
    let instructions = vec![
        // Push original, then set (discards result)
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // hm
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // "d"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 4
        Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // "set"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(4))), // 2 args
        Instruction::simple(OpCode::CallMethod),
        Instruction::simple(OpCode::Pop), // discard the new HashMap
        // Now check original: hm.len() should still be 3
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // original hm
        Instruction::new(OpCode::PushConst, Some(Operand::Const(5))), // "len"
        Instruction::new(OpCode::PushConst, Some(Operand::Const(6))), // 0 args
        Instruction::simple(OpCode::CallMethod),
    ];
    let constants = vec![
        Constant::Value(test_hashmap()),
        Constant::String("d".to_string()),
        Constant::Number(4.0),
        Constant::String("set".to_string()),
        Constant::Number(2.0),
        Constant::String("len".to_string()),
        Constant::Number(0.0),
    ];
    let result = execute_bytecode(instructions, constants).unwrap();
    assert_eq!(result.as_i64(), Some(3));
}