Skip to main content

js2zig_core/zigir/
kinds.rs

1// zigir/kinds.rs
2// Classification enums for member access, calls, and computed keys.
3
4/// How a field access (`obj.field`) should be emitted in Zig.
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub enum FieldKind {
7    /// Direct struct field: `obj.field`
8    StructField,
9    /// Namespace/module access: `std.math.pi` (chained field access on import)
10    Namespace,
11    /// ArrayList length: `arr.items.len`
12    ArrayListLen,
13    /// String length: `str.len`
14    StringLen,
15    /// Map/Set size: `map.size()` or `set.size()`  (method call, not field)
16    MapSetSize,
17    /// Math constant: `std.math.pi`, etc.
18    MathConstant(String),
19    /// Number constant: `std.math.floatMax(f64)`, etc.
20    NumberConstant(String),
21    /// Well-known Symbol: `js_symbol.symbolIterator()`, etc.
22    SymbolWellKnown(String),
23    /// TypedArray property: `.buffer`, `.byteLength`, `.byteOffset`
24    /// `type_suffix` carries the element type suffix (e.g., "I32" for Int32Array)
25    /// to emit `js_runtime.js_typedarray.bufferI32(arr)`.
26    TypedArrayProp {
27        prop: String,
28        type_suffix: Option<String>,
29    },
30    /// Private class field: `self.field` (from #field syntax)
31    Private,
32    /// Pointer dereference field: `obj.field.*` (captured mutable closure var)
33    PointerDeref,
34}
35
36/// How an index access (`obj[idx]`) should be emitted in Zig.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum IndexKind {
39    /// ArrayList item: `arr.items[n]`
40    ArrayListItem,
41    /// Slice index: `arr[n]`
42    SliceIndex,
43}
44
45/// How a computed member access (`obj[key]`) should be emitted.
46#[derive(Debug, Clone, PartialEq, Eq, Hash)]
47pub enum ComputedKeyKind {
48    /// Struct field via @field: `@field(obj, key)`
49    StructField,
50    /// Map get: `obj.get(key)`
51    MapGet,
52    /// JsAny dynamic: `obj.getByKey(key, alloc)`
53    JsAnyGetByKey,
54    /// ArrayList item: `arr.items[key]`
55    ArrayListItem,
56    /// Unsupported dynamic access → generates @compileError
57    CompileError(String),
58}
59
60/// What kind of function call this is.
61#[derive(Debug, Clone, PartialEq, Eq, Hash)]
62pub enum CallKind {
63    /// Direct function call: `fn(args)`
64    Direct,
65    /// Method call on a known object type: `obj.method(args)`
66    Method { object_type: MethodObjectKind },
67    /// Closure call: `closure_instance(.call)(args)`
68    Closure,
69}
70
71/// Known object types for method call dispatch.
72#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub enum MethodObjectKind {
74    ArrayList,
75    String,
76    Map,
77    Set,
78    Date,
79    /// User-defined class method: carries the class name.
80    Class(String),
81    JsAny,
82    Unknown,
83}
84
85/// What kind of constructor `new X(...)` targets.
86#[derive(Debug, Clone, PartialEq, Eq, Hash)]
87pub enum NewConstructor {
88    Map,
89    Set,
90    Date(DateConstructorKind),
91    RegExp,
92    TypedArray(TypedArrayKind),
93    /// User-defined class constructor.
94    Class(String),
95    /// Error constructor: `new Error(msg)`.
96    Error(String),
97    /// Unsupported constructor → generates @compileError.
98    Unsupported(String),
99}
100
101/// How `new Date(...)` was called.
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
103pub enum DateConstructorKind {
104    /// `new Date()` → current time
105    Now,
106    /// `new Date(millis)` → from milliseconds
107    FromMillis,
108    /// `new Date(string)` → from string
109    FromString,
110    /// `new Date(y,m,d,h,min,s,ms)` → from components
111    FromComponents,
112}
113
114/// TypedArray element types.
115#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
116pub enum TypedArrayKind {
117    Int8Array,
118    Uint8Array,
119    Uint8ClampedArray,
120    Int16Array,
121    Uint16Array,
122    Int32Array,
123    Uint32Array,
124    Float32Array,
125    Float64Array,
126    BigInt64Array,
127    BigUint64Array,
128}
129
130#[cfg(test)]
131mod tests {
132    use super::*;
133
134    #[test]
135    fn test_field_kind_variants() {
136        let kinds = [
137            FieldKind::StructField,
138            FieldKind::ArrayListLen,
139            FieldKind::StringLen,
140            FieldKind::MapSetSize,
141            FieldKind::MathConstant("pi".to_string()),
142            FieldKind::NumberConstant("floatMax".to_string()),
143            FieldKind::SymbolWellKnown("iterator".to_string()),
144            FieldKind::TypedArrayProp {
145                prop: "buffer".to_string(),
146                type_suffix: None,
147            },
148        ];
149        assert_eq!(kinds.len(), 8);
150    }
151
152    #[test]
153    fn test_call_kind() {
154        let direct = CallKind::Direct;
155        let method = CallKind::Method {
156            object_type: MethodObjectKind::ArrayList,
157        };
158        let closure = CallKind::Closure;
159        assert_ne!(direct, method);
160        assert_ne!(method, closure);
161    }
162
163    #[test]
164    fn test_new_constructor() {
165        let date_now = NewConstructor::Date(DateConstructorKind::Now);
166        let class = NewConstructor::Class("Foo".to_string());
167        assert!(matches!(
168            date_now,
169            NewConstructor::Date(DateConstructorKind::Now)
170        ));
171        assert!(matches!(class, NewConstructor::Class(_)));
172    }
173
174    #[test]
175    fn test_typed_array_kinds() {
176        let all = [
177            TypedArrayKind::Int8Array,
178            TypedArrayKind::Uint8Array,
179            TypedArrayKind::Uint8ClampedArray,
180            TypedArrayKind::Int16Array,
181            TypedArrayKind::Uint16Array,
182            TypedArrayKind::Int32Array,
183            TypedArrayKind::Uint32Array,
184            TypedArrayKind::Float32Array,
185            TypedArrayKind::Float64Array,
186            TypedArrayKind::BigInt64Array,
187            TypedArrayKind::BigUint64Array,
188        ];
189        assert_eq!(all.len(), 11);
190    }
191}