cao-lang 0.2.6

The back-end of cao-lang, a node based visual scripting 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
use std::{convert::TryFrom, ptr::NonNull};

use bytemuck::Pod;
use tracing::{debug, trace};

use crate::{
    bytecode::{decode_str, read_from_bytes},
    collections::handle_table::Handle,
    compiled_program::CaoCompiledProgram,
    procedures::ExecutionErrorPayload,
    traits::MAX_STR_LEN,
    value::Value,
    VariableId,
};

use super::{
    runtime::{
        cao_lang_function::{CaoLangClosure, CaoLangUpvalue},
        cao_lang_object::{CaoLangObject, CaoLangObjectBody},
        CallFrame, RuntimeData,
    },
    Vm,
};

pub fn read_str<'a>(instr_ptr: &mut usize, program: &'a [u8]) -> Option<&'a str> {
    let p = *instr_ptr;
    let limit = program.len().min(p + MAX_STR_LEN);
    let (len, s): (_, &'a str) = decode_str(&program[p..limit])?;
    *instr_ptr += len;
    Some(s)
}

/// # Safety
///
/// Assumes that the underlying data is safely decodable to the given type
///
pub unsafe fn decode_value<T: Pod>(bytes: &[u8], instr_ptr: &mut usize) -> T {
    let (len, val) = read_from_bytes(&bytes[*instr_ptr..]).expect("Failed to read data");
    *instr_ptr += len;
    val
}

type ExecutionResult = Result<(), ExecutionErrorPayload>;

pub fn instr_read_var(
    runtime_data: &mut RuntimeData,
    instr_ptr: &mut usize,
    program: &CaoCompiledProgram,
) -> ExecutionResult {
    let VariableId(varid): VariableId = unsafe { decode_value(&program.bytecode, instr_ptr) };
    let value = runtime_data
        .global_vars
        .get(varid as usize)
        .ok_or_else(|| {
            ExecutionErrorPayload::VarNotFound(
                program
                    .variables
                    .names
                    .get(Handle::from_u32(varid))
                    .map(|x| x.to_string())
                    .unwrap_or_else(|| "<<<Unknown variable>>>".to_string()),
            )
        })?;
    runtime_data
        .value_stack
        .push(*value)
        .map_err(|_| ExecutionErrorPayload::Stackoverflow)?;
    Ok(())
}

pub fn instr_set_var(
    runtime_data: &mut RuntimeData,
    bytecode: &[u8],
    instr_ptr: &mut usize,
) -> ExecutionResult {
    let varname = unsafe { decode_value::<VariableId>(bytecode, instr_ptr) };
    let scalar = runtime_data.value_stack.pop();
    let varid = varname.0 as usize;
    if runtime_data.global_vars.len() <= varid {
        runtime_data.global_vars.resize(varid + 1, Value::Nil);
    }
    runtime_data.global_vars[varid] = scalar;
    Ok(())
}

pub fn instr_len<T>(vm: &mut Vm<T>) -> ExecutionResult {
    let val = vm.stack_pop();
    let len = match val {
        Value::Nil => 0,
        Value::Integer(_) | Value::Real(_) => 1,
        Value::Object(t) => unsafe { t.as_ref().len() as i64 },
    };
    vm.stack_push(len)?;
    Ok(())
}

pub fn instr_string_literal<T>(
    vm: &mut Vm<T>,
    instr_ptr: &mut usize,
    program: &CaoCompiledProgram,
) -> ExecutionResult {
    let handle: u32 = unsafe { decode_value(&program.bytecode, instr_ptr) };
    let payload = read_str(&mut (handle as usize), program.data.as_slice())
        .ok_or(ExecutionErrorPayload::InvalidArgument { context: None })?;

    let ptr = vm.init_string(payload)?;
    vm.stack_push(Value::Object(ptr.0))?;

    Ok(())
}

pub fn push_call_frame(
    arity: usize,
    src_ptr: u32,
    instr_ptr: u32,
    closure: *mut CaoLangClosure,
    runtime_data: &mut RuntimeData,
) -> ExecutionResult {
    // remember the location after this jump
    runtime_data
        .call_stack
        .last_mut()
        .expect("Call stack was empty")
        .dst_instr_ptr = instr_ptr;

    // init the new call frame
    runtime_data
        .call_stack
        .push(CallFrame {
            src_instr_ptr: src_ptr,
            dst_instr_ptr: instr_ptr,
            stack_offset: runtime_data
                .value_stack
                .len()
                .checked_sub(arity)
                .ok_or(ExecutionErrorPayload::MissingArgument)? as u32,
            closure,
        })
        .map_err(|_| ExecutionErrorPayload::CallStackOverflow)?;
    Ok(())
}

pub fn instr_call_function<T>(
    src_ptr: usize,
    instr_ptr: &mut usize,
    program: &CaoCompiledProgram,
    vm: &mut Vm<T>,
) -> ExecutionResult {
    let Value::Object(o) = vm.runtime_data.value_stack.pop() else {
        return Err(ExecutionErrorPayload::invalid_argument(
            "Call instruction expects a function object argument",
        ));
    };
    let arity;
    let label;
    let mut closure = std::ptr::null_mut();
    unsafe {
        match &o.as_ref().body {
            CaoLangObjectBody::Function(f) => {
                arity = f.arity;
                label = f.handle;
            }
            CaoLangObjectBody::Closure(c) => {
                arity = c.function.arity;
                label = c.function.handle;
                closure = (c as *const CaoLangClosure).cast_mut();
            }
            CaoLangObjectBody::NativeFunction(f) => {
                return call_native(vm, f.handle);
            }
            _ => {
                return Err(ExecutionErrorPayload::invalid_argument(format!(
                    "Call instruction expects a function object argument, instead got: {}",
                    o.as_ref().type_name()
                )));
            }
        }
    }

    push_call_frame(
        arity as usize,
        src_ptr as u32,
        *instr_ptr as u32,
        closure,
        &mut vm.runtime_data,
    )?;

    // set the instr_ptr to the new function's beginning
    *instr_ptr = program
        .labels
        .0
        .get(label)
        .ok_or_else(|| ExecutionErrorPayload::ProcedureNotFound(label))?
        .pos as usize;
    Ok(())
}

pub fn execute_call_native<T>(
    vm: &mut Vm<T>,
    instr_ptr: &mut usize,
    bytecode: &[u8],
) -> ExecutionResult {
    let fun_hash: Handle = unsafe { decode_value(bytecode, instr_ptr) };
    call_native(vm, fun_hash)
}

pub fn call_native<T>(vm: &mut Vm<T>, handle: Handle) -> ExecutionResult {
    // Clone the function because in the future native functions may call into the VM and call
    // themselves recursively
    let procedure: crate::procedures::Procedure<T> = vm
        .callables
        .get(handle)
        .ok_or(ExecutionErrorPayload::ProcedureNotFound(handle))?
        .clone();
    let res = procedure
        .fun
        .call(vm)
        .map_err(|err| ExecutionErrorPayload::TaskFailure {
            name: procedure.name().to_string(),
            error: Box::new(err),
        })?;
    vm.stack_push(res)?;
    Ok(())
}

fn write_local_var<T>(vm: &mut Vm<T>, handle: u32, value: Value, offset: usize) -> ExecutionResult {
    vm.runtime_data
        .value_stack
        .set(offset + handle as usize, value)
        .map_err(|err| {
            ExecutionErrorPayload::VarNotFound(format!("Failed to set local variable: {}", err))
        })?;
    Ok(())
}

fn stack_offset<T>(vm: &Vm<T>) -> usize {
    let offset = vm
        .runtime_data
        .call_stack
        .last()
        .expect("Call stack is emtpy")
        .stack_offset;
    offset as usize
}

pub fn set_local<T>(vm: &mut Vm<T>, bytecode: &[u8], instr_ptr: &mut usize) -> ExecutionResult {
    let handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let offset = stack_offset(vm);
    let value = vm.runtime_data.value_stack.pop_w_offset(offset);
    debug!(
        handle = handle,
        offset = offset,
        value = tracing::field::debug(value),
        "writing local variable"
    );
    write_local_var(vm, handle, value, offset)
}

fn read_local_var<T>(vm: &mut Vm<T>, handle: u32) -> Result<Value, ExecutionErrorPayload> {
    let offset = stack_offset(vm);
    let value = vm.runtime_data.value_stack.get(offset + handle as usize);
    debug!(
        handle = handle,
        offset = offset,
        value = tracing::field::debug(value),
        "read local variable"
    );
    Ok(value)
}

pub fn get_local<T>(vm: &mut Vm<T>, bytecode: &[u8], instr_ptr: &mut usize) -> ExecutionResult {
    let handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let value = read_local_var(vm, handle)?;
    vm.stack_push(value)?;
    Ok(())
}

pub fn instr_return<T>(vm: &mut Vm<T>, instr_ptr: &mut usize) -> ExecutionResult {
    // pop the current stack frame
    let value = match vm.runtime_data.call_stack.pop() {
        // return value
        Some(call_frame) => {
            let stack_start_location = unsafe {
                vm.runtime_data
                    .value_stack
                    .as_slice()
                    .as_ptr()
                    .add(call_frame.stack_offset as usize)
            };
            _close_upvalues(vm, stack_start_location)?;

            vm.runtime_data
                .value_stack
                .clear_until(call_frame.stack_offset as usize)
        }
        None => {
            return Err(ExecutionErrorPayload::BadReturn {
                reason: "Call stack is empty".to_string(),
            })
        }
    };

    // read the previous frame
    match vm.runtime_data.call_stack.last_mut() {
        Some(CallFrame {
            dst_instr_ptr: ptr, ..
        }) => {
            *instr_ptr = *ptr as usize;
        }
        None => {
            return Err(ExecutionErrorPayload::BadReturn {
                reason: "Failed to find return address".to_string(),
            });
        }
    }

    // push the return value
    trace!("Return {value:?}");
    vm.stack_push(value)?;
    Ok(())
}

pub fn instr_copy_last<T>(vm: &mut Vm<T>) -> ExecutionResult {
    let val = vm.runtime_data.value_stack.last();
    vm.runtime_data
        .value_stack
        .push(val)
        .map_err(|_| ExecutionErrorPayload::Stackoverflow)?;

    Ok(())
}

/// push `i=0` onto the stack
pub fn begin_for_each<T>(
    vm: &mut Vm<T>,
    bytecode: &[u8],
    instr_ptr: &mut usize,
) -> ExecutionResult {
    let i_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let t_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let item_val = vm.runtime_data.value_stack.last();
    // test if the input is a table
    let item = vm.get_table_mut(item_val)?;
    debug!("Starting for-each on table {:?}", item);
    let offset = stack_offset(vm);
    write_local_var(vm, i_handle, Value::Integer(0), offset)?;
    write_local_var(vm, t_handle, item_val, offset)?;

    // initialize local variables
    //
    // must be initialized at this point, otherwise the scope end cleans up non-existent locals
    // if for-each is called with an empty list
    let i_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let k_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let v_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    write_local_var(vm, v_handle, Value::Nil, offset)?;
    write_local_var(vm, k_handle, Value::Nil, offset)?;
    write_local_var(vm, i_handle, Value::Nil, offset)?;

    Ok(())
}

/// Assumes that [begin_for_each](begin_for_each) was called once to set up the loop
///
/// Pushes the next key and the object onto the stack. Assumes that the function takes these as
/// parameters.
///
/// Pushes should_continue on top of the stack
pub fn for_each<T>(vm: &mut Vm<T>, bytecode: &[u8], instr_ptr: &mut usize) -> ExecutionResult {
    let loop_variable: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let t_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };

    let i_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let k_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };
    let v_handle: u32 = unsafe { decode_value(bytecode, instr_ptr) };

    let offset = stack_offset(vm);
    let i = read_local_var(vm, loop_variable)?;
    let obj_val = read_local_var(vm, t_handle)?;

    let i = i64::try_from(i).map_err(|_| {
        ExecutionErrorPayload::AssertionError("ForEach i must be an integer. This error can be caused by stack corruption. Check your function calls!".to_string())
    })?;
    let obj = vm.get_table(obj_val).map_err(|_| {
        ExecutionErrorPayload::AssertionError("ForEach value is not an object. This error can be caused by stack corruption. Check your function calls!".to_string())
    })?;

    debug_assert!(0 <= i, "for_each overflow");

    let n = obj.len() as i64;

    let should_continue = 0 <= i && i < n;
    if should_continue {
        let key = obj.nth_key(i as usize);
        let val = obj.get(&key).copied().unwrap_or(Value::Nil);

        write_local_var(vm, v_handle, val, offset)?;
        write_local_var(vm, k_handle, key, offset)?;
        write_local_var(vm, i_handle, Value::Integer(i), offset)?;
        // store the loop variable
        write_local_var(vm, loop_variable, Value::Integer(i + 1), offset)?;
    }
    vm.stack_push(should_continue)?;

    Ok(())
}

fn resolve_closure<'a>(closure: Value) -> Result<&'a mut CaoLangClosure, ExecutionErrorPayload> {
    match closure {
        Value::Object(mut o) => unsafe {
            let o = o.as_mut();
            match &mut o.body {
                CaoLangObjectBody::Closure(c) => Ok(c),
                _ => Err(ExecutionErrorPayload::invalid_argument(
                    "Upvalues can only be registered for closures",
                )),
            }
        },
        _ => Err(ExecutionErrorPayload::invalid_argument(
            "Upvalues can only be registered for closures",
        )),
    }
}

fn resolve_upvalue(o: &mut CaoLangObject) -> Result<&mut CaoLangUpvalue, ExecutionErrorPayload> {
    match &mut o.body {
        CaoLangObjectBody::Upvalue(u) => Ok(u),
        _ => Err(ExecutionErrorPayload::invalid_argument(
            "Expected Upvalue object",
        )),
    }
}

/// Registers an upvalue in the closure at the top of the stack
pub fn register_upvalue<T>(
    vm: &mut Vm<T>,
    bytecode: &[u8],
    instr_ptr: &mut usize,
) -> ExecutionResult {
    let index: u8 = unsafe { decode_value(bytecode, instr_ptr) };
    let is_local: u8 = unsafe { decode_value(bytecode, instr_ptr) };
    let is_local = is_local != 0;
    let closure = vm.stack_pop();

    let c = resolve_closure(closure)?;

    if is_local {
        let location = &vm.runtime_data.value_stack.as_slice()[index as usize];
        let location = (location as *const Value).cast_mut();
        unsafe {
            // look for an existing upvalue to the same location
            let mut prev_upvalue = std::ptr::null_mut();
            let mut upvalue = vm.runtime_data.open_upvalues;
            while let Some(u) = upvalue.as_ref().and_then(|o| o.as_upvalue()) {
                if u.location <= location {
                    break;
                }
                prev_upvalue = upvalue;
                upvalue = u.next;
            }
            if upvalue
                .as_ref()
                .and_then(|u| u.as_upvalue())
                .filter(|x| x.location == location)
                .is_some()
            {
                // if there is an existing upvalue to this location reuse that
                c.upvalues.push(NonNull::new_unchecked(upvalue));
            } else {
                let upvalue = vm.init_upvalue(location)?;

                // keep the open upvalues sorted
                match prev_upvalue.as_mut().and_then(|u| u.as_upvalue_mut()) {
                    Some(prev_upvalue) => {
                        prev_upvalue.next = upvalue.0.as_ptr();
                    }
                    None => {
                        vm.runtime_data.open_upvalues = upvalue.0.as_ptr();
                    }
                }

                c.upvalues.push(upvalue.0);
            }
        }
    } else {
        let closure = unsafe {
            vm.runtime_data
                .call_stack
                .last()
                .unwrap()
                .closure
                .as_ref()
                .expect("closure not found for capture")
        };
        let upvalue = closure.upvalues[index as usize];
        c.upvalues.push(upvalue);
    }

    Ok(())
}

pub fn read_upvalue<T>(vm: &mut Vm<T>, bytecode: &[u8], instr_ptr: &mut usize) -> ExecutionResult {
    unsafe {
        let index: u32 = decode_value(bytecode, instr_ptr);
        let c = vm.runtime_data.call_stack.last().unwrap();
        let Some(c) = c.closure.as_mut() else {
            return Err(ExecutionErrorPayload::NotClosure);
        };
        match c.upvalues.get_mut(index as usize) {
            Some(u) => {
                let u = resolve_upvalue(u.as_mut())?;
                debug_assert!(!u.location.is_null());
                let value = *u.location;
                vm.stack_push(value)
            }
            None => Err(ExecutionErrorPayload::InvalidUpvalue),
        }
    }
}

pub fn write_upvalue<T>(vm: &mut Vm<T>, bytecode: &[u8], instr_ptr: &mut usize) -> ExecutionResult {
    unsafe {
        let index: u32 = decode_value(bytecode, instr_ptr);
        let value = vm.stack_pop();
        let c = vm.runtime_data.call_stack.last().unwrap();
        let Some(c) = c.closure.as_mut() else {
            return Err(ExecutionErrorPayload::NotClosure);
        };
        match c.upvalues.get_mut(index as usize) {
            Some(u) => {
                let u = resolve_upvalue(u.as_mut())?;
                debug_assert!(!u.location.is_null());
                std::ptr::write(u.location, value);
                Ok(())
            }
            None => Err(ExecutionErrorPayload::InvalidUpvalue),
        }
    }
}

fn _close_upvalues<T>(vm: &mut Vm<T>, top: *const Value) -> ExecutionResult {
    if top.is_null() {
        return Err(ExecutionErrorPayload::invalid_argument(
            "Can't close upvalues on an empty stack",
        ));
    }

    unsafe {
        while let Some(upvalue) = vm.runtime_data.open_upvalues.as_mut() {
            match upvalue.as_upvalue_mut() {
                Some(upvalue) => {
                    if upvalue.location < top.cast_mut() {
                        break;
                    }
                    upvalue.value = std::ptr::read(upvalue.location);
                    upvalue.location = (&mut upvalue.value) as *mut _;
                    vm.runtime_data.open_upvalues = upvalue.next;
                }
                None => {
                    vm.runtime_data.open_upvalues = std::ptr::null_mut();
                    return Err(ExecutionErrorPayload::InvalidUpvalue);
                }
            }
        }
    }

    Ok(())
}

pub fn close_upvalues<T>(vm: &mut Vm<T>) -> ExecutionResult {
    let top = vm.runtime_data.value_stack.top_location();
    _close_upvalues(vm, top)
}