runmat_vm/object/
class_def.rs1use runmat_builtins::{self, Access, ClassDef, MethodDef, PropertyDef};
2use runmat_runtime::RuntimeError;
3
4pub fn register_class(
5 name: String,
6 super_class: Option<String>,
7 properties: Vec<(String, bool, String, String)>,
8 methods: Vec<(String, String, bool, String)>,
9) -> Result<(), RuntimeError> {
10 let mut prop_map = std::collections::HashMap::new();
11 for (p, is_static, get_access, set_access) in properties {
12 let gacc = if get_access.eq_ignore_ascii_case("private") {
13 Access::Private
14 } else {
15 Access::Public
16 };
17 let sacc = if set_access.eq_ignore_ascii_case("private") {
18 Access::Private
19 } else {
20 Access::Public
21 };
22 let (is_dep, clean_name) = if let Some(stripped) = p.strip_prefix("@dep:") {
23 (true, stripped.to_string())
24 } else {
25 (false, p.clone())
26 };
27 prop_map.insert(
28 clean_name.clone(),
29 PropertyDef {
30 name: clean_name,
31 is_static,
32 is_dependent: is_dep,
33 get_access: gacc,
34 set_access: sacc,
35 default_value: None,
36 },
37 );
38 }
39 let mut method_map = std::collections::HashMap::new();
40 for (mname, fname, is_static, access) in methods {
41 let access = if access.eq_ignore_ascii_case("private") {
42 Access::Private
43 } else {
44 Access::Public
45 };
46 method_map.insert(
47 mname.clone(),
48 MethodDef {
49 name: mname,
50 is_static,
51 access,
52 function_name: fname,
53 },
54 );
55 }
56 let def = ClassDef {
57 name: name.clone(),
58 parent: super_class.clone(),
59 properties: prop_map,
60 methods: method_map,
61 };
62 runmat_builtins::register_class(def);
63 Ok(())
64}