cao-lang 0.1.67

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
//! Cao-Lang back-end
//!
//! Interprets the compiled output produced by the Cao-Lang compiler
mod instr_execution;
pub mod runtime;

#[cfg(test)]
mod tests;

use self::runtime::CallFrame;
use crate::{
    collections::key_map::{Handle, KeyMap},
    instruction::instruction_span,
    instruction::Instruction,
    prelude::*,
    value::Value,
    VariableId,
};
use runtime::RuntimeData;
use std::{mem::transmute, str::FromStr};
use tracing::debug;

/// Cao-Lang bytecode interpreter.
/// `Aux` is an auxiliary runtime structure passed to custom functions.
pub struct Vm<'a, Aux = ()>
where
    Aux: 'a,
{
    pub auxiliary_data: Aux,
    /// Number of instructions `run` will execute before returning Timeout
    pub max_instr: u64,

    pub runtime_data: RuntimeData,

    callables: KeyMap<Procedure<Aux>>,
    _m: std::marker::PhantomData<&'a ()>,
}

impl<'a, Aux> Vm<'a, Aux> {
    pub fn new(auxiliary_data: Aux) -> Result<Self, ExecutionErrorPayload> {
        Ok(Self {
            auxiliary_data,
            callables: KeyMap::default(),
            runtime_data: RuntimeData::new(400 * 1024, 256, 256)?,
            max_instr: 1000,
            _m: Default::default(),
        })
    }

    pub fn clear(&mut self) {
        self.runtime_data.clear();
    }

    pub fn read_var_by_name(&self, name: &str, vars: &Variables) -> Option<Value> {
        let varid = vars.ids.get(Handle::from_str(name).ok()?)?;
        self.read_var(*varid)
    }

    #[inline]
    pub fn read_var(&self, name: VariableId) -> Option<Value> {
        self.runtime_data.global_vars.get(name.0 as usize).cloned()
    }

    #[must_use]
    pub fn with_max_iter(mut self, max_iter: u64) -> Self {
        self.max_instr = max_iter;
        self
    }

    #[inline]
    pub fn get_aux(&self) -> &Aux {
        &self.auxiliary_data
    }

    #[inline]
    pub fn get_aux_mut(&mut self) -> &mut Aux {
        &mut self.auxiliary_data
    }

    #[inline]
    pub fn unwrap_aux(self) -> Aux {
        self.auxiliary_data
    }

    /// Register a native function for use by Cao-Lang programs
    ///
    pub fn register_function<S, C>(&mut self, name: S, f: C)
    where
        S: Into<String>,
        C: VmFunction<Aux> + 'static,
    {
        let name = name.into();
        let key = Handle::from_str(name.as_str()).unwrap();
        self.callables
            .insert(key, Procedure::new(name, f))
            .expect("failed to insert new function");
    }

    #[inline]
    pub fn stack_push<S>(&mut self, value: S) -> Result<(), ExecutionErrorPayload>
    where
        S: Into<Value>,
    {
        self.runtime_data
            .stack
            .push(value.into())
            .map_err(|_| ExecutionErrorPayload::Stackoverflow)?;
        Ok(())
    }

    #[inline]
    pub fn stack_pop(&mut self) -> Value {
        self.runtime_data.stack.pop()
    }

    pub fn get_table(&self, value: Value) -> Result<&FieldTable, ExecutionErrorPayload> {
        let res = match value {
            Value::Object(o) => unsafe { &*o },
            _ => {
                debug!("Got {:?} instead of object", value);
                return Err(ExecutionErrorPayload::invalid_argument(
                    "Input must be an Object".to_string(),
                ));
            }
        };
        Ok(res)
    }

    pub fn get_table_mut(&self, value: Value) -> Result<&mut FieldTable, ExecutionErrorPayload> {
        let res = match value {
            Value::Object(o) => unsafe { &mut *o },
            _ => {
                debug!("Got {:?} instead of object", value);
                return Err(ExecutionErrorPayload::invalid_argument(
                    "GetProperty input must be an Object".to_string(),
                ));
            }
        };
        Ok(res)
    }

    /// Initializes a new FieldTable in this VM instance
    #[inline]
    pub fn init_table(&mut self) -> Result<std::ptr::NonNull<FieldTable>, ExecutionErrorPayload> {
        self.runtime_data.init_table()
    }

    /// Initializes a new string owned by this VM instance
    pub fn init_string(&mut self, payload: &str) -> Result<StrPointer, ExecutionErrorPayload> {
        unsafe {
            let layout = std::alloc::Layout::from_size_align(4 + payload.len(), 4).unwrap();
            let mut ptr = self
                .runtime_data
                .memory
                .alloc(layout)
                .map_err(|_| ExecutionErrorPayload::OutOfMemory)?;

            let result: *mut u8 = ptr.as_mut();
            std::ptr::write(result as *mut u32, payload.len() as u32);
            std::ptr::copy(payload.as_ptr(), result.add(4), payload.len());

            Ok(StrPointer(ptr.as_ptr()))
        }
    }

    /// This mostly assumes that program is valid, produced by the compiler.
    /// As such running non-compiler emitted programs is very un-safe
    #[inline(never)]
    pub fn run(&mut self, program: &CaoCompiledProgram) -> ExecutionResult<()> {
        self.runtime_data
            .call_stack
            .push(CallFrame {
                instr_ptr: 0,
                stack_offset: 0,
            })
            .map_err(|_| ExecutionErrorPayload::CallStackOverflow)
            .map_err(|pl| ExecutionError::new(pl, TraceEntry::default()))?;

        let len = program.bytecode.len();
        let mut remaining_iters = self.max_instr;
        let mut instr_ptr = 0;
        let bytecode_ptr = program.bytecode.as_ptr();

        let payload_to_error = |err, instr_ptr| {
            ExecutionError::new(
                err,
                program.trace.get(&instr_ptr).cloned().unwrap_or_default(),
            )
        };

        while instr_ptr < len {
            remaining_iters -= 1;
            if remaining_iters == 0 {
                return Err(payload_to_error(ExecutionErrorPayload::Timeout, instr_ptr));
            }
            let instr: u8 = unsafe { *bytecode_ptr.add(instr_ptr) };
            let instr: Instruction = unsafe { transmute(instr) };
            instr_ptr += 1;
            debug!(
                "Executing: {:?} instr_ptr: {} Stack: {}",
                instr, instr_ptr, self.runtime_data.stack
            );
            match instr {
                Instruction::InitTable => {
                    let res = self
                        .init_table()
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                    self.stack_push(Value::Object(res.as_ptr()))
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::GetProperty => {
                    let handle = self
                        .pop_key()
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                    let instance = self.stack_pop();
                    let table = self
                        .get_table(instance)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                    let result = table.get_value(handle).unwrap_or(Value::Nil);
                    self.stack_push(result)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::SetProperty => {
                    let [value, key, instance] = self.runtime_data.stack.pop_n::<3>();
                    let table = self
                        .get_table_mut(instance)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                    table
                        .insert(key, value)
                        .map_err(|err| {
                            debug!("Failed to insert value {:?}", err);
                            ExecutionErrorPayload::OutOfMemory
                        })
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::BeginRepeat => {
                    instr_execution::begin_repeat(self)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::Repeat => {
                    if instr_execution::repeat(self)
                        .map_err(|err| payload_to_error(err, instr_ptr))?
                    {
                        instr_execution::instr_jump(
                            &mut instr_ptr,
                            program,
                            &mut self.runtime_data,
                        )
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                    } else {
                        self.stack_push(false)
                            .map_err(|err| payload_to_error(err, instr_ptr))?; // assumes that the next instruction is GotoIfTrue
                        instr_ptr += instruction_span(Instruction::CallLane) as usize - 1;
                    }
                }
                Instruction::BeginForEach => {
                    instr_execution::begin_for_each(self)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::ForEach => {
                    if instr_execution::for_each(self)
                        .map_err(|err| payload_to_error(err, instr_ptr))?
                    {
                        instr_execution::instr_jump(
                            &mut instr_ptr,
                            program,
                            &mut self.runtime_data,
                        )
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                    } else {
                        self.stack_push(false)
                            .map_err(|err| payload_to_error(err, instr_ptr))?; // assumes that the next instruction is GotoIfTrue
                                                                               // add the span of the jump instruction metadata to the instr_ptr
                                                                               // to skip this instruction
                        instr_ptr += instruction_span(Instruction::CallLane) as usize - 1;
                    }
                }
                Instruction::GotoIfTrue => {
                    let condition = self.runtime_data.stack.pop();
                    let pos: i32 =
                        unsafe { instr_execution::decode_value(&program.bytecode, &mut instr_ptr) };
                    debug_assert!(pos >= 0);
                    if condition.as_bool() {
                        instr_ptr = pos as usize;
                    }
                }
                Instruction::GotoIfFalse => {
                    let condition = self.runtime_data.stack.pop();
                    let pos: i32 =
                        unsafe { instr_execution::decode_value(&program.bytecode, &mut instr_ptr) };
                    debug_assert!(pos >= 0);
                    if !condition.as_bool() {
                        instr_ptr = pos as usize;
                    }
                }
                Instruction::Goto => {
                    let pos: i32 =
                        unsafe { instr_execution::decode_value(&program.bytecode, &mut instr_ptr) };
                    debug_assert!(pos >= 0);
                    instr_ptr = pos as usize;
                }
                Instruction::SwapLast => {
                    let b = self.stack_pop();
                    let a = self.stack_pop();
                    // we popped two values, we know that the stack has capacity for 2 ..
                    self.stack_push(b).unwrap();
                    self.stack_push(a).unwrap();
                }
                Instruction::ScalarNil => self
                    .stack_push(Value::Nil)
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::ClearStack => {
                    self.runtime_data.stack.clear_until(
                        self.runtime_data
                            .call_stack
                            .last()
                            .expect("No callframe available")
                            .stack_offset,
                    );
                }
                Instruction::SetLocalVar => {
                    instr_execution::set_local(self, &program.bytecode, &mut instr_ptr)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::ReadLocalVar => {
                    instr_execution::get_local(self, &program.bytecode, &mut instr_ptr)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::SetGlobalVar => {
                    instr_execution::instr_set_var(
                        &mut self.runtime_data,
                        &program.bytecode,
                        &mut instr_ptr,
                    )
                    .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::ReadGlobalVar => {
                    instr_execution::instr_read_var(
                        &mut self.runtime_data,
                        &mut instr_ptr,
                        program,
                    )
                    .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::Pop => {
                    self.stack_pop();
                }
                Instruction::CallLane => {
                    instr_execution::instr_jump(&mut instr_ptr, program, &mut self.runtime_data)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::Return => {
                    instr_execution::instr_return(self, &mut instr_ptr)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::Exit => return Ok(()),
                Instruction::CopyLast => {
                    instr_execution::instr_copy_last(self)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::Pass => {}
                Instruction::ScalarInt => {
                    self.runtime_data
                        .stack
                        .push(Value::Integer(unsafe {
                            instr_execution::decode_value(&program.bytecode, &mut instr_ptr)
                        }))
                        .map_err(|_| ExecutionErrorPayload::Stackoverflow)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::ScalarFloat => {
                    self.runtime_data
                        .stack
                        .push(Value::Floating(unsafe {
                            instr_execution::decode_value(&program.bytecode, &mut instr_ptr)
                        }))
                        .map_err(|_| ExecutionErrorPayload::Stackoverflow)
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::Not => {
                    let value = self.stack_pop();
                    let value = !value.as_bool();
                    self.stack_push(Value::Integer(value as i64))
                        .map_err(|err| payload_to_error(err, instr_ptr))?;
                }
                Instruction::And => self
                    .binary_op(|a, b| Value::from(a.as_bool() && b.as_bool()))
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Or => self
                    .binary_op(|a, b| Value::from(a.as_bool() || b.as_bool()))
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Xor => self
                    .binary_op(|a, b| Value::from(a.as_bool() ^ b.as_bool()))
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Add => self
                    .binary_op(|a, b| a + b)
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Sub => self
                    .binary_op(|a, b| a - b)
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Mul => self
                    .binary_op(|a, b| a * b)
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Div => self
                    .binary_op(|a, b| a / b)
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Equals => self
                    .binary_op(|a, b| (a == b).into())
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::NotEquals => self
                    .binary_op(|a, b| (a != b).into())
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::Less => self
                    .binary_op(|a, b| (a < b).into())
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::LessOrEq => self
                    .binary_op(|a, b| (a <= b).into())
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
                Instruction::StringLiteral => {
                    instr_execution::instr_string_literal(self, &mut instr_ptr, program)
                        .map_err(|err| payload_to_error(err, instr_ptr))?
                }
                Instruction::Call => {
                    instr_execution::execute_call(self, &mut instr_ptr, &program.bytecode)
                        .map_err(|err| payload_to_error(err, instr_ptr))?
                }
                Instruction::Len => instr_execution::instr_len(self)
                    .map_err(|err| payload_to_error(err, instr_ptr))?,
            }
        }

        Err(payload_to_error(
            ExecutionErrorPayload::UnexpectedEndOfInput,
            instr_ptr,
        ))
    }

    #[inline]
    fn binary_op(&mut self, op: fn(Value, Value) -> Value) -> Result<(), ExecutionErrorPayload> {
        let b = self.stack_pop();
        let a = self.stack_pop();

        self.runtime_data
            .stack
            .push(op(a, b))
            .map_err(|_| ExecutionErrorPayload::Stackoverflow)?;
        Ok(())
    }

    fn pop_key(&mut self) -> Result<Handle, ExecutionErrorPayload> {
        let handle = self.stack_pop();
        let handle = match handle {
            Value::Nil => Handle::default(),
            Value::String(s) => {
                let s = unsafe {
                    s.get_str().ok_or_else(|| {
                        ExecutionErrorPayload::invalid_argument("String not found".to_string())
                    })?
                };
                Handle::from_str(s).unwrap()
            }
            Value::Integer(i) => Handle::from(i),
            Value::Floating(_) | Value::Object(_) => return Err(ExecutionErrorPayload::Unhashable),
        };
        Ok(handle)
    }
}