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
// zigir/lower/expr/container.rs
// Array, object, template literal lowering.
use oxc_ast::ast::*;
use super::Lowerer;
use crate::zigir::lower::helpers;
use crate::zigir::source_span::SourceSpan;
impl Lowerer {
/// Lower an array expression.
pub(super) fn lower_array_expr(&mut self, ae: &ArrayExpression) -> crate::zigir::types::IrExpr {
let mut elements = Vec::new();
let mut spread_indices = Vec::new();
for (i, elem) in ae.elements.iter().enumerate() {
match elem {
ArrayExpressionElement::SpreadElement(se) => {
spread_indices.push(i);
elements.push(crate::zigir::types::IrExpr::Spread(Box::new(
self.lower_expr(&se.argument),
)));
}
ArrayExpressionElement::Elision(_) => {
elements.push(crate::zigir::types::IrExpr::Null);
}
_ => {
if let Some(expr) = elem.as_expression() {
elements.push(self.lower_expr(expr));
}
}
}
}
crate::zigir::types::IrExpr::ArrayLiteral(crate::zigir::types::IrArrayLiteral {
elements,
spread_indices,
})
}
/// Lower an object expression.
pub(super) fn lower_object_expr(
&mut self,
oe: &ObjectExpression,
) -> crate::zigir::types::IrExpr {
use crate::zigir::types::IrObjectItem;
let mut items = Vec::new();
for prop in oe.properties.iter() {
match prop {
ObjectPropertyKind::ObjectProperty(op) => {
let (key, is_computed) = match &op.key {
PropertyKey::StaticIdentifier(id) => (id.name.to_string(), false),
PropertyKey::StringLiteral(sl) => (sl.value.to_string(), false),
PropertyKey::NumericLiteral(nl) => (nl.value.to_string(), false),
_ => ("__computed__".to_string(), true),
};
match op.kind {
PropertyKind::Init => {
let value = self.lower_expr(&op.value);
items.push(IrObjectItem::Field(crate::zigir::types::IrObjectField {
key,
value,
is_computed,
}));
}
PropertyKind::Get => {
// Getter: extract return expression from function body
// { get x() { return expr; } } → .x = expr
// Only single-return getters are inlined; complex ones get @compileError
if let Expression::FunctionExpression(func) = &op.value
&& let Some(body) = &func.body
&& let Some(return_expr) = Self::extract_return_expr_from_body(body)
{
let value = self.lower_expr(return_expr);
items.push(IrObjectItem::Field(
crate::zigir::types::IrObjectField {
key,
value,
is_computed,
},
));
} else {
// Complex getter (multiple statements) — @compileError
let span = self.span_to_source_span(op.span);
self.add_error(span, "getter with complex body is not supported (only single-return getters are inlined)");
items.push(IrObjectItem::Field(
crate::zigir::types::IrObjectField {
key,
value: crate::zigir::types::IrExpr::CompileError {
span: SourceSpan::default(),
msg: "complex getter not supported".to_string(),
},
is_computed,
},
));
}
}
PropertyKind::Set => {
// Setter: @compileError — Zig structs don't support setters
let span = self.span_to_source_span(op.span);
self.add_error(
span,
"setter property is not supported (Zig structs have no setters)",
);
items.push(IrObjectItem::Field(crate::zigir::types::IrObjectField {
key,
value: crate::zigir::types::IrExpr::CompileError {
span: SourceSpan::default(),
msg: "setter not supported".to_string(),
},
is_computed,
}));
}
}
}
ObjectPropertyKind::SpreadProperty(sp) => {
items.push(IrObjectItem::Spread(self.lower_expr(&sp.argument)));
}
}
}
crate::zigir::types::IrExpr::ObjectLiteral(crate::zigir::types::IrObjectLiteral { items })
}
/// Extract the return expression from a function body with a single return statement.
/// Used by getter property lowering: `{ get x() { return expr; } }` → `.x = expr`.
pub(super) fn extract_return_expr_from_body<'a>(
body: &'a FunctionBody<'a>,
) -> Option<&'a Expression<'a>> {
if body.statements.len() == 1
&& let Statement::ReturnStatement(ret) = &body.statements[0]
{
return ret.argument.as_ref();
}
None
}
/// Lower a template literal.
pub(super) fn lower_template_literal(
&mut self,
tl: &TemplateLiteral,
) -> crate::zigir::types::IrExpr {
// Use the COOKED value (post-escape-interpretation), not `raw`.
// `raw` is the literal source text (e.g. `\\n` as two bytes 0x5C 0x6E);
// `cooked` is the interpreted string (a real 0x0A newline byte). When
// the emitter wraps a quasi in a Zig string literal via
// `escape_zig_format_string`, the cooked value's real control bytes
// get re-escaped to Zig escapes (e.g. `\n`) which Zig then correctly
// interprets at runtime. Using `raw` instead produced double-escaped
// output ("hello\\nworld" → runtime "hello\nworld" with literal
// backslash-n). Note: `cooked` is `None` for invalid escape sequences
// (e.g. `\u` not followed by valid hex); fall back to `raw` only then
// (R6-3).
let parts: Vec<String> = tl
.quasis
.iter()
.map(|q| {
q.value
.cooked
.as_ref()
.map(|c| c.to_string())
.unwrap_or_else(|| q.value.raw.to_string())
})
.collect();
let exprs: Vec<crate::zigir::types::IrExpr> =
tl.expressions.iter().map(|e| self.lower_expr(e)).collect();
// Determine the Zig format specifier for each interpolation expression.
// This must match the Emitter's logic:
// Str→{s}, I64/F64→{d}, Bool→{}, other→expr_is_string?{s}:{}
let format_specs: Vec<String> = tl
.expressions
.iter()
.map(|expr| {
if self.expr_is_string(expr) {
"{s}".to_string()
} else {
match self.infer_expr_type(expr) {
Some(ty) => helpers::format_specifier_for_type(&ty).to_string(),
None => "{any}".to_string(),
}
}
})
.collect();
crate::zigir::types::IrExpr::TemplateLiteral {
parts,
exprs,
format_specs,
}
}
}