add lists & VM memory limits

This commit is contained in:
りき萌 2024-08-20 23:00:39 +02:00
parent 1c0fa7197c
commit 50094c3872
9 changed files with 54 additions and 6 deletions

View file

@ -99,8 +99,10 @@ impl Display for ChunkError {
impl Error for ChunkError {}
pub mod fns {
use alloc::vec::Vec;
use crate::{
value::{Ref, Rgba, Scribble, Shape, Stroke, Value, Vec4},
value::{List, Ref, Rgba, Scribble, Shape, Stroke, Value, Vec4},
vm::{Exception, FnArgs, Vm},
};
@ -133,6 +135,8 @@ pub mod fns {
0x88 ".b" => rgba_b,
0x89 ".a" => rgba_a,
0x90 "list" => list,
0xc0 "to-shape" => to_shape_f,
0xc1 "line" => line,
0xe0 "stroke" => stroke,
@ -389,6 +393,13 @@ pub mod fns {
Ok(Value::Number(rgba.r))
}
pub fn list(vm: &mut Vm, args: FnArgs) -> Result<Value, Exception> {
let elements: Vec<_> = (0..args.num()).map(|i| args.get(vm, i)).collect();
vm.track_array(&elements)?;
let id = vm.create_ref(Ref::List(List { elements }))?;
Ok(Value::Ref(id))
}
fn to_shape(value: Value, vm: &Vm) -> Option<Shape> {
match value {
Value::Nil | Value::False | Value::True | Value::Number(_) | Value::Rgba(_) => None,