tiscript 0.0.3

Turing-Incomplete TypeScript as a Configuration 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
use std::{cell::RefCell, error::Error, rc::Rc};

use crate::{
    bytecode::{ByteCode, FnByteCode, FnDef},
    // dprintln,
    instructions::{Instruction, OpCode},
    value::Value,
};

pub enum YieldResult {
    Finished(Value),
    Suspend(Value),
}

pub struct StackFrame {
    fn_def: Rc<FnByteCode>,
    args: usize,
    stack: Vec<Value>,
    ip: usize,
}

impl StackFrame {
    fn new(fn_def: Rc<FnByteCode>, args: Vec<Value>) -> Self {
        Self {
            fn_def,
            args: args.len(),
            stack: args,
            ip: 0,
        }
    }

    fn inst(&self) -> Option<Instruction> {
        let ret = self.fn_def.instructions.get(self.ip)?;
        // dprintln!(
        //   "interpret[{}]: {:?} stack: {:?}",
        //   self.ip,
        //   ret,
        //   self.stack
        // );
        Some(*ret)
    }
}

pub struct Vm {
    bytecode: Rc<ByteCode>,
    stack_frames: Vec<StackFrame>,
    user_data: Box<dyn std::any::Any>,
    pub exports: Value, // must be an object
    debug_output: bool,
}

impl PartialEq for Vm {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(self, other)
    }
}

impl std::fmt::Debug for Vm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "<Vm>")
    }
}

#[cold]
fn err_bin_op(op: &str, lhs: &Value, rhs: &Value) -> Box<dyn Error> {
    format!(
        "Operator {} cannot be applied to types '{}' and '{}'",
        op,
        lhs.kind(),
        rhs.kind()
    )
    .into()
}

fn bin_op_add(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(lhs + rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs + rhs)),
        (Value::Str(lhs), rhs) => Ok(Value::Str(lhs.clone() + &rhs.to_string())),
        (lhs, Value::Str(rhs)) => Ok(Value::Str(lhs.to_string() + rhs)),
        _ => Err(err_bin_op("+", lhs, rhs)),
    }
}

fn bin_op_sub(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(lhs - rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs - rhs)),
        _ => Err(err_bin_op("-", lhs, rhs)),
    }
}

fn bin_op_mul(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(lhs * rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs * rhs)),
        _ => Err(err_bin_op("*", lhs, rhs)),
    }
}

fn bin_op_div(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(lhs / rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs / rhs)),
        _ => Err(err_bin_op("/", lhs, rhs)),
    }
}

fn bin_op_mod(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(lhs % rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs % rhs)),
        _ => Err(err_bin_op("%", lhs, rhs)),
    }
}

fn bin_op_pow(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(lhs.powf(*rhs))),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(
            lhs.saturating_pow((*rhs).min(u32::MAX as i64) as u32),
        )), // when overflowed, returns i64::MAX
        _ => Err(err_bin_op("**", lhs, rhs)),
    }
}

fn bin_op_bw_or(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num((*lhs as i32 | *rhs as i32) as f64)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs | rhs)),
        _ => Err(err_bin_op("|", lhs, rhs)),
    }
}

fn bin_op_bw_and(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num((*lhs as i32 & *rhs as i32) as f64)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs & rhs)),
        _ => Err(err_bin_op("&", lhs, rhs)),
    }
}

fn bin_op_bw_xor(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num((*lhs as i32 ^ *rhs as i32) as f64)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs ^ rhs)),
        _ => Err(err_bin_op("^", lhs, rhs)),
    }
}

fn bin_op_bw_lshift(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(((*lhs as i32) << *rhs as i32) as f64)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs << rhs)),
        _ => Err(err_bin_op("<<", lhs, rhs)),
    }
}

fn bin_op_bw_rshift(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num((*lhs as i32 >> *rhs as i32) as f64)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::Int(lhs >> rhs)),
        _ => Err(err_bin_op(">>", lhs, rhs)),
    }
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift
// a.k.a. Right Shift Zero Fill operator.
fn bin_op_bw_rshift_u(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::Num(
            ((*lhs as i32 as u32) >> (*rhs as i32 as u32)) as f64,
        )),
        _ => Err(err_bin_op(">>>", lhs, rhs)),
    }
}

fn bin_op_lt(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::from(*lhs < *rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs < *rhs)),
        (Value::Num(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs < *rhs as f64)),
        (Value::Int(lhs), Value::Num(rhs)) => Ok(Value::from((*lhs as f64) < *rhs)),
        (Value::Str(lhs), Value::Str(rhs)) => Ok(Value::from(*lhs < *rhs)),
        _ => Err(err_bin_op("<", lhs, rhs)),
    }
}

fn bin_op_le(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::from(*lhs <= *rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs <= *rhs)),
        (Value::Num(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs <= *rhs as f64)),
        (Value::Int(lhs), Value::Num(rhs)) => Ok(Value::from((*lhs as f64) <= *rhs)),
        (Value::Str(lhs), Value::Str(rhs)) => Ok(Value::from(*lhs <= *rhs)),
        _ => Err(err_bin_op("<=", lhs, rhs)),
    }
}

fn bin_op_gt(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::from(*lhs > *rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs > *rhs)),
        (Value::Num(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs > *rhs as f64)),
        (Value::Int(lhs), Value::Num(rhs)) => Ok(Value::from((*lhs as f64) > *rhs)),
        (Value::Str(lhs), Value::Str(rhs)) => Ok(Value::from(*lhs > *rhs)),
        _ => Err(err_bin_op(">", lhs, rhs)),
    }
}

fn bin_op_ge(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    match (lhs, rhs) {
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::from(*lhs >= *rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs >= *rhs)),
        (Value::Num(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs >= *rhs as f64)),
        (Value::Int(lhs), Value::Num(rhs)) => Ok(Value::from((*lhs as f64) >= *rhs)),
        (Value::Str(lhs), Value::Str(rhs)) => Ok(Value::from(*lhs >= *rhs)),
        _ => Err(err_bin_op(">=", lhs, rhs)),
    }
}

fn bin_op_ee(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    // TODO: ECMA-262 compliant equality check
    match (lhs, rhs) {
        (Value::Undefined, Value::Undefined) => Ok(Value::from(true)),
        (Value::Null, Value::Null) => Ok(Value::from(true)),
        (Value::Undefined, Value::Null) => Ok(Value::from(true)),
        (Value::Null, Value::Undefined) => Ok(Value::from(true)),
        (Value::Bool(lhs), Value::Bool(rhs)) => Ok(Value::from(*lhs == *rhs)),
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::from(*lhs == *rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs == *rhs)),
        (Value::Num(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs == *rhs as f64)),
        (Value::Int(lhs), Value::Num(rhs)) => Ok(Value::from((*lhs as f64) >= *rhs)),
        (Value::Str(lhs), Value::Str(rhs)) => Ok(Value::from(*lhs >= *rhs)),
        _ => Ok(Value::from(false)),
    }
}

fn bin_op_ne(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    bin_op_ee(lhs, rhs).map(|v| Value::from(!v.to_bool()))
}

fn bin_op_eee(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    // TODO: ECMA-262 compliant equality check
    match (lhs, rhs) {
        (Value::Undefined, Value::Undefined) => Ok(Value::from(true)),
        (Value::Null, Value::Null) => Ok(Value::from(true)),
        (Value::Bool(lhs), Value::Bool(rhs)) => Ok(Value::from(*lhs == *rhs)),
        (Value::Num(lhs), Value::Num(rhs)) => Ok(Value::from(*lhs == *rhs)),
        (Value::Int(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs == *rhs)),
        (Value::Num(lhs), Value::Int(rhs)) => Ok(Value::from(*lhs == *rhs as f64)),
        (Value::Int(lhs), Value::Num(rhs)) => Ok(Value::from((*lhs as f64) >= *rhs)),
        (Value::Str(lhs), Value::Str(rhs)) => Ok(Value::from(*lhs >= *rhs)),
        (Value::Array(lhs), Value::Array(rhs)) => Ok(Value::from(lhs == rhs)),
        (Value::Object(lhs), Value::Object(rhs)) => Ok(Value::from(lhs == rhs)),
        (Value::Coro(lhs), Value::Coro(rhs)) => Ok(Value::from(lhs == rhs)),
        _ => Ok(Value::from(false)),
    }
}

fn bin_op_nee(lhs: &Value, rhs: &Value) -> Result<Value, Box<dyn Error>> {
    bin_op_eee(lhs, rhs).map(|v| Value::from(!v.to_bool()))
}

#[cold]
fn stack_overflow(ip: usize, instruction: Instruction, stack: &[Value]) -> ! {
    panic!(
        "[BUG] Stack overflow in `[{}] {:?}` where stack.len() is {} (see --disasm to look into the instructions)",
        ip, instruction, stack.len()
    );
}

#[cold]
fn log_instruction(ip: usize, instruction: Instruction, stack: &Vec<Value>) {
    eprintln!("interpret[{ip}]: {instruction:?} stack: {stack:?}",);
}

impl Vm {
    pub fn new(
        bytecode: Rc<ByteCode>,
        user_data: Box<dyn std::any::Any>,
        debug_output: bool,
    ) -> Self {
        Self {
            bytecode,
            stack_frames: Default::default(),
            user_data,
            exports: Value::Object(Default::default()),
            debug_output,
        }
    }

    pub fn top(&self) -> Result<&StackFrame, String> {
        self.stack_frames
            .last()
            .ok_or_else(|| "Stack frame underflow".to_string())
    }

    fn top_mut(&mut self) -> Result<&mut StackFrame, String> {
        self.stack_frames
            .last_mut()
            .ok_or_else(|| "Stack frame underflow".to_string())
    }

    /// A convenience function to run a function without the
    /// ability to suspend execution.
    /// An yield instruction would be an error.
    #[allow(dead_code)]
    fn run_fn(&mut self, fn_name: &str, args: &[Value]) -> Result<Value, Box<dyn Error>> {
        let fn_def = self
            .bytecode
            .funcs
            .get(fn_name)
            .ok_or_else(|| format!("Function {fn_name:?} was not found"))?;
        let fn_def = match fn_def {
            FnDef::User(user) => user.clone(),
            FnDef::Native(n) => return (*n.code)(self.user_data.as_ref(), args),
        };

        self.stack_frames
            .push(StackFrame::new(fn_def, args.to_vec()));

        match self.interpret()? {
            YieldResult::Finished(val) => Ok(val),
            YieldResult::Suspend(_) => Err("Yielded at toplevel".into()),
        }
    }

    pub fn init_fn(&mut self, fn_name: &str, args: &[Value]) -> Result<(), Box<dyn Error>> {
        let fn_def = self
            .bytecode
            .funcs
            .get(fn_name)
            .ok_or_else(|| format!("Function {fn_name:?} was not found"))?;
        let fn_def = match fn_def {
            FnDef::User(user) => user.clone(),
            FnDef::Native(_) => {
                return Err(
                    "Native function cannot be called as a coroutine. Use `run_fn` instead.".into(),
                )
            }
        };

        self.stack_frames
            .push(StackFrame::new(fn_def, args.to_vec()));

        Ok(())
    }

    fn return_fn(&mut self, stack_pos: u8) -> Result<Option<YieldResult>, Box<dyn Error>> {
        let top_frame = self
            .stack_frames
            .pop()
            .ok_or("Stack frame underflow at Ret")?;
        let res = top_frame
            .stack
            .get(top_frame.stack.len() - stack_pos as usize - 1)
            .ok_or("Stack underflow at Ret")?
            .clone();
        let args = top_frame.args;

        if self.stack_frames.is_empty() {
            return Ok(Some(YieldResult::Finished(res)));
        }

        if self.debug_output {
            eprintln!("Returning {}", res);
        }

        let stack = &mut self.top_mut()?.stack;
        stack.resize(stack.len() - args - 1, Value::default());
        stack.push(res);
        self.top_mut()?.ip += 1;
        Ok(None)
    }

    pub fn interpret(&mut self) -> Result<YieldResult, Box<dyn Error>> {
        loop {
            let (instruction, ip) = if let Some(instruction) = self.top()?.inst() {
                (instruction, self.top()?.ip)
            } else {
                if let Some(res) = self.return_fn(0)? {
                    return Ok(res);
                }
                continue;
            };

            if self.debug_output {
                log_instruction(ip, instruction, &self.top()?.stack);
            }

            match instruction.op {
                OpCode::Nop => (),
                OpCode::LoadLit => {
                    let stack_frame = self.top_mut()?;
                    stack_frame
                        .stack
                        .push(stack_frame.fn_def.literals[instruction.arg0 as usize].clone());
                }
                OpCode::Store => {
                    let stack = &mut self.top_mut()?.stack;
                    let top_to_bottom_idx = instruction.arg0 as usize;
                    // stack.len() - top_to_bottom_idx - 1 with overflow check
                    let Some(d) = stack.len().checked_sub(top_to_bottom_idx) else {
                        stack_overflow(ip, instruction, stack);
                    };
                    let Some(idx) = d.checked_sub(1) else {
                        stack_overflow(ip, instruction, stack);
                    };
                    let value = stack.pop().expect("Store needs an argument");
                    stack[idx] = value;
                }
                OpCode::Copy => {
                    let stack = &mut self.top_mut()?.stack;
                    let top_to_bottom_idx = instruction.arg0 as usize;
                    // stack.len() - top_to_bottom_idx - 1 with overflow check
                    let Some(d) = stack.len().checked_sub(top_to_bottom_idx) else {
                        stack_overflow(ip, instruction, stack);
                    };
                    let Some(idx) = d.checked_sub(1) else {
                        stack_overflow(ip, instruction, stack);
                    };
                    stack.push(stack[idx].clone());
                }
                OpCode::Dup => {
                    let stack = &mut self.top_mut()?.stack;
                    let top = stack.last().unwrap().clone();
                    stack.extend((0..instruction.arg0).map(|_| top.clone()));
                }
                OpCode::Not => {
                    let stack = &mut self.top_mut()?.stack;
                    let top = stack.pop().expect("Not needs an argument");
                    stack.push(Value::Bool(!top.to_bool()));
                }
                OpCode::BwNot => {
                    let stack = &mut self.top_mut()?.stack;
                    let top = stack.pop().expect("BwNot needs an argument");
                    match top {
                        Value::Num(n) => stack.push(Value::Num(!(n as i64) as f64)),
                        Value::Int(n) => stack.push(Value::Int(!n)),
                        _ => panic!("BwNot needs an integer"),
                    }
                }
                OpCode::Neg => {
                    let stack = &mut self.top_mut()?.stack;
                    let top = stack.pop().expect("Neg needs an argument");
                    match top {
                        Value::Num(n) => stack.push(Value::Num(-n)),
                        Value::Int(n) => stack.push(Value::Int(-n)),
                        _ => panic!("Neg needs a number"),
                    }
                }
                OpCode::Add => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_add)?,
                OpCode::Sub => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_sub)?,
                OpCode::Mul => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_mul)?,
                OpCode::Div => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_div)?,
                OpCode::Mod => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_mod)?,
                OpCode::Pow => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_pow)?,
                OpCode::BwOr => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_bw_or)?,
                OpCode::BwAnd => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_bw_and)?,
                OpCode::BwXor => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_bw_xor)?,
                OpCode::BwLShift => {
                    Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_bw_lshift)?
                }
                OpCode::BwRShift => {
                    Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_bw_rshift)?
                }
                OpCode::BwRShiftU => {
                    Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_bw_rshift_u)?
                }
                OpCode::Call => {
                    let stack = &self.top()?.stack;
                    let args = &stack[stack.len() - instruction.arg0 as usize..];
                    let fname = &stack[stack.len() - instruction.arg0 as usize - 1];
                    let Value::Str(fname) = fname else {
                        panic!(
                            "Function name shall be a string: {fname:?} in fn {:?}",
                            self.top()?.stack
                        );
                    };
                    let fn_def = self
                        .bytecode
                        .funcs
                        .get(fname)
                        .ok_or_else(|| format!("Function not found: {fname:?}"))?;
                    match fn_def {
                        FnDef::User(user_fn) => {
                            if user_fn.cofn {
                                let mut vm =
                                    Vm::new(self.bytecode.clone(), Box::new(()), self.debug_output);
                                vm.stack_frames
                                    .push(StackFrame::new(user_fn.clone(), args.to_vec()));
                                let stack = &mut self.top_mut()?.stack;
                                stack.resize(
                                    stack.len() - instruction.arg0 as usize - 1,
                                    Value::default(),
                                );
                                stack.push(Value::Coro(Rc::new(RefCell::new(vm))));
                            } else {
                                self.stack_frames
                                    .push(StackFrame::new(user_fn.clone(), args.to_vec()));
                                continue;
                            }
                        }
                        FnDef::Native(native) => {
                            let res = (native.code)(self.user_data.as_ref(), args)?;
                            let stack = &mut self.top_mut()?.stack;
                            stack.resize(
                                stack.len() - instruction.arg0 as usize - 1,
                                Value::default(),
                            );
                            stack.push(res);
                        }
                    }
                }
                OpCode::Jmp => {
                    self.top_mut()?.ip = instruction.arg0 as usize;
                    continue;
                }
                OpCode::Jf => {
                    let stack = &mut self.top_mut()?.stack;
                    let cond = stack.pop().expect("Jf needs an argument");
                    if !cond.to_bool() {
                        self.top_mut()?.ip = instruction.arg0 as usize;
                        continue;
                    }
                }
                OpCode::Lt => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_lt)?,
                OpCode::Le => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_le)?,
                OpCode::Gt => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_gt)?,
                OpCode::Ge => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_ge)?,
                OpCode::Ee => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_ee)?,
                OpCode::Ne => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_ne)?,
                OpCode::Eee => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_eee)?,
                OpCode::Nee => Self::interpret_bin_op(&mut self.top_mut()?.stack, bin_op_nee)?,
                OpCode::Pop => {
                    let stack = &mut self.top_mut()?.stack;
                    stack.resize(stack.len() - instruction.arg0 as usize, Value::default());
                }
                OpCode::Ret => {
                    if let Some(res) = self.return_fn(instruction.arg0)? {
                        return Ok(res);
                    }
                    continue;
                }
                OpCode::Yield => {
                    let top_frame = self.top_mut()?;
                    let res = top_frame
                        .stack
                        .pop()
                        .ok_or_else(|| "Stack underflow".to_string())?;
                    // Increment the ip for the next call
                    top_frame.ip += 1;
                    return Ok(YieldResult::Suspend(res));
                }
                OpCode::Await => {
                    let vms = self
                        .top_mut()?
                        .stack
                        .pop()
                        .ok_or_else(|| "Stack underflow".to_string())?;
                    let Value::Coro(vm) = vms else {
                        return Err("Await keyword applied to a non-coroutine".into());
                    };
                    match vm.borrow_mut().interpret() {
                        Ok(YieldResult::Finished(_)) => (),
                        Ok(YieldResult::Suspend(value)) => {
                            self.top_mut()?.stack.push(value);
                        }
                        Err(e) => {
                            eprintln!("Runtime error: {e:?}");
                        }
                    };
                }
                OpCode::Export => {
                    let stack = &mut self.top_mut()?.stack;

                    let name = stack
                        .pop()
                        .expect("Export needs a name")
                        .must_be_str()
                        .expect("Export name must be a string")
                        .to_string();
                    let value = stack[stack.len() - 1].clone();

                    match self.exports {
                        Value::Object(ref mut map) => {
                            map.insert(name, value);
                        }
                        _ => {
                            panic!("vm.exports must be an object");
                        }
                    };
                }
            }
            self.top_mut()?.ip += 1;
        }
    }

    fn interpret_bin_op(
        stack: &mut Vec<Value>,
        op: impl FnOnce(&Value, &Value) -> Result<Value, Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        let rhs = stack.pop().expect("Stack underflow");
        let lhs = stack.pop().expect("Stack underflow");
        let res = op(&lhs, &rhs)?;
        stack.push(res);
        Ok(())
    }

    #[allow(dead_code)]
    fn back_trace(&self) {
        for (i, frame) in self.stack_frames.iter().rev().enumerate() {
            eprintln!("[{}]: {:?}", i, frame.stack);
        }
    }
}