Skip to main content

aver/types/
vector.rs

1/// Vector namespace — fixed-size indexed sequence helpers.
2///
3/// Methods:
4///   Vector.new(size, default)     → Vector<T>
5///   Vector.get(vec, idx)          → Option<T>
6///   Vector.set(vec, idx, val)     → Option<Vector<T>>
7///   Vector.len(vec)               → Int
8///   Vector.fromList(xs)           → Vector<T>
9///   Vector.toList(vec)            → List<T>
10///
11/// No effects required.
12use std::collections::HashMap;
13use std::sync::Arc as Rc;
14
15use aver_rt::AverVector;
16
17use crate::nan_value::{Arena, NanValue};
18use crate::value::{RuntimeError, Value, list_from_vec, list_view};
19
20pub fn register(global: &mut HashMap<String, Value>) {
21    let mut members = HashMap::new();
22    for method in &["new", "get", "set", "len", "fromList", "toList"] {
23        members.insert(
24            method.to_string(),
25            Value::Builtin(format!("Vector.{}", method)),
26        );
27    }
28    global.insert(
29        "Vector".to_string(),
30        Value::Namespace {
31            name: "Vector".to_string(),
32            members,
33        },
34    );
35}
36
37pub fn effects(_name: &str) -> &'static [&'static str] {
38    &[]
39}
40
41pub fn call(name: &str, args: &[Value]) -> Option<Result<Value, RuntimeError>> {
42    match name {
43        "Vector.new" => Some(vec_new(args)),
44        "Vector.get" => Some(vec_get(args)),
45        "Vector.set" => Some(vec_set(args)),
46        "Vector.len" => Some(vec_len(args)),
47        "Vector.fromList" => Some(vec_from_list(args)),
48        "Vector.toList" => Some(vec_to_list(args)),
49        _ => None,
50    }
51}
52
53fn vec_new(args: &[Value]) -> Result<Value, RuntimeError> {
54    if args.len() != 2 {
55        return Err(RuntimeError::Error(format!(
56            "Vector.new() takes 2 arguments (size, default), got {}",
57            args.len()
58        )));
59    }
60    let Value::Int(size) = &args[0] else {
61        return Err(RuntimeError::Error(
62            "Vector.new: size must be an Int".to_string(),
63        ));
64    };
65    if *size < 0 {
66        return Err(RuntimeError::Error(
67            "Vector.new: size must not be negative".to_string(),
68        ));
69    }
70    Ok(Value::Vector(AverVector::new(
71        *size as usize,
72        args[1].clone(),
73    )))
74}
75
76fn vec_get(args: &[Value]) -> Result<Value, RuntimeError> {
77    if args.len() != 2 {
78        return Err(RuntimeError::Error(format!(
79            "Vector.get() takes 2 arguments, got {}",
80            args.len()
81        )));
82    }
83    let Value::Vector(vec) = &args[0] else {
84        return Err(RuntimeError::Error(
85            "Vector.get: first argument must be a Vector".to_string(),
86        ));
87    };
88    let Value::Int(idx) = &args[1] else {
89        return Err(RuntimeError::Error(
90            "Vector.get: index must be an Int".to_string(),
91        ));
92    };
93    if *idx < 0 {
94        return Ok(Value::None);
95    }
96    Ok(match vec.get(*idx as usize) {
97        Some(v) => Value::Some(Box::new(v.clone())),
98        None => Value::None,
99    })
100}
101
102fn vec_set(args: &[Value]) -> Result<Value, RuntimeError> {
103    if args.len() != 3 {
104        return Err(RuntimeError::Error(format!(
105            "Vector.set() takes 3 arguments, got {}",
106            args.len()
107        )));
108    }
109    let Value::Vector(vec) = &args[0] else {
110        return Err(RuntimeError::Error(
111            "Vector.set: first argument must be a Vector".to_string(),
112        ));
113    };
114    let Value::Int(idx) = &args[1] else {
115        return Err(RuntimeError::Error(
116            "Vector.set: index must be an Int".to_string(),
117        ));
118    };
119    if *idx < 0 {
120        return Ok(Value::None);
121    }
122    Ok(match vec.set(*idx as usize, args[2].clone()) {
123        Some(new_vec) => Value::Some(Box::new(Value::Vector(new_vec))),
124        None => Value::None,
125    })
126}
127
128fn vec_len(args: &[Value]) -> Result<Value, RuntimeError> {
129    if args.len() != 1 {
130        return Err(RuntimeError::Error(format!(
131            "Vector.len() takes 1 argument, got {}",
132            args.len()
133        )));
134    }
135    let Value::Vector(vec) = &args[0] else {
136        return Err(RuntimeError::Error(
137            "Vector.len: argument must be a Vector".to_string(),
138        ));
139    };
140    Ok(Value::Int(vec.len() as i64))
141}
142
143fn vec_from_list(args: &[Value]) -> Result<Value, RuntimeError> {
144    if args.len() != 1 {
145        return Err(RuntimeError::Error(format!(
146            "Vector.fromList() takes 1 argument, got {}",
147            args.len()
148        )));
149    }
150    let items = list_view(&args[0]).ok_or_else(|| {
151        RuntimeError::Error("Vector.fromList: argument must be a List".to_string())
152    })?;
153    Ok(Value::Vector(AverVector::from_list(items)))
154}
155
156fn vec_to_list(args: &[Value]) -> Result<Value, RuntimeError> {
157    if args.len() != 1 {
158        return Err(RuntimeError::Error(format!(
159            "Vector.toList() takes 1 argument, got {}",
160            args.len()
161        )));
162    }
163    let Value::Vector(vec) = &args[0] else {
164        return Err(RuntimeError::Error(
165            "Vector.toList: argument must be a Vector".to_string(),
166        ));
167    };
168    Ok(list_from_vec(vec.to_vec()))
169}
170
171// ─── NanValue-native API ─────────────────────────────────────────────────────
172
173pub fn register_nv(global: &mut HashMap<String, NanValue>, arena: &mut Arena) {
174    let methods = &["new", "get", "set", "len", "fromList", "toList"];
175    let mut members: Vec<(Rc<str>, NanValue)> = Vec::with_capacity(methods.len());
176    for method in methods {
177        let idx = arena.push_builtin(&format!("Vector.{}", method));
178        members.push((Rc::from(*method), NanValue::new_builtin(idx)));
179    }
180    let ns_idx = arena.push(crate::nan_value::ArenaEntry::Namespace {
181        name: Rc::from("Vector"),
182        members,
183    });
184    global.insert("Vector".to_string(), NanValue::new_namespace(ns_idx));
185}
186
187pub fn call_nv(
188    name: &str,
189    args: &[NanValue],
190    arena: &mut Arena,
191) -> Option<Result<NanValue, RuntimeError>> {
192    match name {
193        "Vector.new" => Some(vec_new_nv(args, arena)),
194        "Vector.get" => Some(vec_get_nv(args, arena)),
195        "Vector.set" => Some(vec_set_nv(args, arena)),
196        "Vector.len" => Some(vec_len_nv(args, arena)),
197        "Vector.fromList" => Some(vec_from_list_nv(args, arena)),
198        "Vector.toList" => Some(vec_to_list_nv(args, arena)),
199        _ => None,
200    }
201}
202
203fn vec_new_nv(args: &[NanValue], arena: &mut Arena) -> Result<NanValue, RuntimeError> {
204    if args.len() != 2 {
205        return Err(RuntimeError::Error(format!(
206            "Vector.new() takes 2 arguments (size, default), got {}",
207            args.len()
208        )));
209    }
210    if !args[0].is_int() {
211        return Err(RuntimeError::Error(
212            "Vector.new: size must be an Int".to_string(),
213        ));
214    }
215    let size = args[0].as_int(arena);
216    if size < 0 {
217        return Err(RuntimeError::Error(
218            "Vector.new: size must not be negative".to_string(),
219        ));
220    }
221    let items = vec![args[1]; size as usize];
222    if items.is_empty() {
223        Ok(NanValue::EMPTY_VECTOR)
224    } else {
225        Ok(NanValue::new_vector(arena.push_vector(items)))
226    }
227}
228
229fn vec_get_nv(args: &[NanValue], arena: &mut Arena) -> Result<NanValue, RuntimeError> {
230    if args.len() != 2 {
231        return Err(RuntimeError::Error(format!(
232            "Vector.get() takes 2 arguments, got {}",
233            args.len()
234        )));
235    }
236    if !args[0].is_vector() {
237        return Err(RuntimeError::Error(
238            "Vector.get: first argument must be a Vector".to_string(),
239        ));
240    }
241    if !args[1].is_int() {
242        return Err(RuntimeError::Error(
243            "Vector.get: index must be an Int".to_string(),
244        ));
245    }
246    let idx = args[1].as_int(arena);
247    if idx < 0 {
248        return Ok(NanValue::NONE);
249    }
250    let items = arena.vector_ref_value(args[0]);
251    match items.get(idx as usize) {
252        Some(&v) => Ok(NanValue::new_some_value(v, arena)),
253        None => Ok(NanValue::NONE),
254    }
255}
256
257/// Vector.set with sole-owned first argument — takes instead of cloning.
258pub fn vec_set_nv_owned(args: &[NanValue], arena: &mut Arena) -> Result<NanValue, RuntimeError> {
259    if args.len() != 3 {
260        return Err(RuntimeError::Error(format!(
261            "Vector.set() takes 3 arguments, got {}",
262            args.len()
263        )));
264    }
265    if !args[0].is_vector() {
266        return Err(RuntimeError::Error(
267            "Vector.set: first argument must be a Vector".to_string(),
268        ));
269    }
270    if !args[1].is_int() {
271        return Err(RuntimeError::Error(
272            "Vector.set: index must be an Int".to_string(),
273        ));
274    }
275    let idx = args[1].as_int(arena);
276    if idx < 0 {
277        return Ok(NanValue::NONE);
278    }
279    let mut items = arena.take_vector_value(args[0]);
280    let uidx = idx as usize;
281    if uidx >= items.len() {
282        return Ok(NanValue::NONE);
283    }
284    items[uidx] = args[2];
285    let new_vec_idx = arena.push_vector(items);
286    Ok(NanValue::new_some_value(
287        NanValue::new_vector(new_vec_idx),
288        arena,
289    ))
290}
291
292fn vec_set_nv(args: &[NanValue], arena: &mut Arena) -> Result<NanValue, RuntimeError> {
293    if args.len() != 3 {
294        return Err(RuntimeError::Error(format!(
295            "Vector.set() takes 3 arguments, got {}",
296            args.len()
297        )));
298    }
299    if !args[0].is_vector() {
300        return Err(RuntimeError::Error(
301            "Vector.set: first argument must be a Vector".to_string(),
302        ));
303    }
304    if !args[1].is_int() {
305        return Err(RuntimeError::Error(
306            "Vector.set: index must be an Int".to_string(),
307        ));
308    }
309    let idx = args[1].as_int(arena);
310    if idx < 0 {
311        return Ok(NanValue::NONE);
312    }
313    let mut items = arena.clone_vector_value(args[0]);
314    let uidx = idx as usize;
315    if uidx >= items.len() {
316        return Ok(NanValue::NONE);
317    }
318    items[uidx] = args[2];
319    let new_vec_idx = arena.push_vector(items);
320    Ok(NanValue::new_some_value(
321        NanValue::new_vector(new_vec_idx),
322        arena,
323    ))
324}
325
326fn vec_len_nv(args: &[NanValue], arena: &mut Arena) -> Result<NanValue, RuntimeError> {
327    if args.len() != 1 {
328        return Err(RuntimeError::Error(format!(
329            "Vector.len() takes 1 argument, got {}",
330            args.len()
331        )));
332    }
333    if !args[0].is_vector() {
334        return Err(RuntimeError::Error(
335            "Vector.len: argument must be a Vector".to_string(),
336        ));
337    }
338    let items = arena.vector_ref_value(args[0]);
339    Ok(NanValue::new_int(items.len() as i64, arena))
340}
341
342fn vec_from_list_nv(args: &[NanValue], arena: &mut Arena) -> Result<NanValue, RuntimeError> {
343    if args.len() != 1 {
344        return Err(RuntimeError::Error(format!(
345            "Vector.fromList() takes 1 argument, got {}",
346            args.len()
347        )));
348    }
349    if !args[0].is_list() {
350        return Err(RuntimeError::Error(
351            "Vector.fromList: argument must be a List".to_string(),
352        ));
353    }
354    let items = arena.list_to_vec_value(args[0]);
355    if items.is_empty() {
356        Ok(NanValue::EMPTY_VECTOR)
357    } else {
358        Ok(NanValue::new_vector(arena.push_vector(items)))
359    }
360}
361
362fn vec_to_list_nv(args: &[NanValue], arena: &mut Arena) -> Result<NanValue, RuntimeError> {
363    if args.len() != 1 {
364        return Err(RuntimeError::Error(format!(
365            "Vector.toList() takes 1 argument, got {}",
366            args.len()
367        )));
368    }
369    if !args[0].is_vector() {
370        return Err(RuntimeError::Error(
371            "Vector.toList: argument must be a Vector".to_string(),
372        ));
373    }
374    let items = arena.clone_vector_value(args[0]);
375    if items.is_empty() {
376        Ok(NanValue::EMPTY_LIST)
377    } else {
378        let list_idx = arena.push_list(items);
379        Ok(NanValue::new_list(list_idx))
380    }
381}