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
// zigir/emit/stmt/destructure_assign.rs
// Destructuring declaration and assignment statement emission.
use crate::zigir::emit::Emitter;
use crate::zigir::emit::helpers::EmitterHelpers;
use crate::zigir::ops::AssignOp;
use crate::zigir::types::{IrAssignTarget, IrDestructureDecl};
impl Emitter {
pub(super) fn emit_destructure_decl(&mut self, data: &IrDestructureDecl) {
use crate::zigir::types::{IrDestructureAccess, IrDestructureKind};
// Step 1: Emit temp variable if needed
if let Some(temp) = &data.temp_name {
self.write_indent();
self.write("const ");
self.write(temp);
self.write(" = ");
self.emit_expr(&data.init);
self.writeln(";");
}
// Step 2: Emit each binding declaration
for binding in &data.bindings {
let kw = if binding.is_const { "const" } else { "var" };
self.write_indent();
self.write(&format!("{} {} = ", kw, binding.name.zig_name));
match &binding.access {
IrDestructureAccess::ObjectField {
source,
key,
is_struct_field,
} => {
match &data.kind {
IrDestructureKind::Object { is_struct, .. } => {
if *is_struct && *is_struct_field {
// Struct with known field: direct field access
self.write(&format!("{}.{}", source, key));
} else if *is_struct && !is_struct_field {
// Struct but field not found
if let Some(default) = &binding.default {
// Use default directly
self.emit_expr(default);
} else {
// Error: field not in struct and no default
self.write(&format!(
"/* error: key '{}' not in struct */",
key
));
}
} else {
// HashMap / unknown: use .get("key")
if let Some(default) = &binding.default {
// Type-aware conversion: .asBool(), .asI64(), .value.string
let conv = self.infer_hashmap_conv(default);
self.write(&format!(
"if ({}.get(\"{}\")) |v| v{} else ",
source, key, conv
));
self.emit_expr(default);
} else {
// No default: raw .get() returns ?JsAny
self.write(&format!("{}.get(\"{}\")", source, key));
}
}
}
IrDestructureKind::Array { .. } => {
// Shouldn't happen — object access in array destructure
self.write(&format!("{}.{}", source, key));
}
}
}
IrDestructureAccess::ArrayIndex { source, index } => {
match &data.kind {
IrDestructureKind::Array { is_arraylist } => {
if *is_arraylist {
// ArrayList: bounds-safe .items[i] access
if let Some(default) = &binding.default {
self.write(&format!(
"if ({}.items.len > {}) {}.items[{}] else ",
source, index, source, index
));
self.emit_expr(default);
} else {
self.write(&format!("{}.items[{}]", source, index));
}
} else {
// Slice/array: direct [i] access
self.write(&format!("{}[{}]", source, index));
if let Some(default) = &binding.default {
self.write(" orelse ");
self.emit_expr(default);
}
}
}
IrDestructureKind::Object { .. } => {
self.write(&format!("{}[{}]", source, index));
}
}
}
}
self.writeln(";");
}
}
/// Infer the HashMap value conversion method based on the default expression type.
/// Returns ".asBool()" for bool defaults, ".value.string" for string defaults,
/// ".asF64()" for float defaults, and ".asI64()" for integer defaults.
pub(super) fn infer_hashmap_conv(&self, default: &crate::zigir::types::IrExpr) -> &'static str {
use crate::types::ZigType;
use crate::zigir::ops::BinOp;
use crate::zigir::types::IrExpr;
match default {
IrExpr::BoolLiteral(_) => ".asBool()",
IrExpr::StringLiteral(_) => ".value.string",
IrExpr::FloatLiteral(_) => ".asF64()",
// TypedIdent with F64 → float extraction
IrExpr::TypedIdent {
ty: ZigType::F64, ..
} => ".asF64()",
// TypedIdent with JsAny → assume i64 (most common JS number default)
IrExpr::TypedIdent {
ty: ZigType::JsAny, ..
} => ".asI64()",
// Division always produces f64 in JS semantics
IrExpr::Binary {
op,
left_type,
right_type,
..
} if *op == BinOp::Div
|| left_type.as_ref() == Some(&ZigType::F64)
|| right_type.as_ref() == Some(&ZigType::F64) =>
{
".asF64()"
}
IrExpr::Binary { .. } => ".asI64()",
_ => ".asI64()",
}
}
pub(super) fn emit_assign_stmt(
&mut self,
target: &IrAssignTarget,
op: AssignOp,
value: &crate::zigir::types::IrExpr,
) {
self.write_indent();
self.emit_assign_inline(target, op, value);
self.write(";\n");
}
/// Emit assignment inline (no indent, no semicolon, no newline).
/// Used in while continuation blocks where the assignment is part of
/// a single-line expression like `: ({ i += 1; })`.
pub(super) fn emit_assign_inline(
&mut self,
target: &IrAssignTarget,
op: AssignOp,
value: &crate::zigir::types::IrExpr,
) {
self.emit_compound_assign(target, op, value);
}
}