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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// zigir/emit/builtins/collections.rs
// Date, Map/Set, TypedArray, URI, BigInt, Runtime builtin method emission.
use crate::zigir::emit::helpers::EmitterHelpers;
use crate::zigir::emit::Emitter;
impl Emitter {
pub(super) fn emit_date_builtin(
&mut self,
method: &str,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
) {
match method {
// Instance methods: receiver.method() — not js_date.method()
"getTime" | "getFullYear" | "getMonth" | "getDate" | "getDay" | "getHours"
| "getMinutes" | "getSeconds" | "getMilliseconds" | "getTimezoneOffset"
| "getUTCFullYear" | "getUTCMonth" | "getUTCDate" | "getUTCDay" | "getUTCHours"
| "getUTCMinutes" | "getUTCSeconds" | "getUTCMilliseconds" | "setDate"
| "setMilliseconds" | "setUTCDate" | "setUTCMilliseconds" | "setTime" | "valueOf" => {
if method.starts_with("set") {
self.emit_date_setter_simple(obj, method, args);
} else {
self.emit_receiver_or_module_call(obj, "js_date", method, args);
}
}
// Date setters with optional params — pad missing args with null:
// setFullYear(year, month?, date?) → 3 slots
"setFullYear" | "setUTCFullYear" => {
if let Some(name) = obj {
self.write(&format!("{}.{}(", name, method));
} else {
self.write(&format!("js_date.{}(", method));
}
let defaults = ["0", "null", "null"];
self.emit_args_with_defaults_i64(args, 3, &defaults);
self.write(")");
}
// setMonth(month, date?) → 2 slots
"setMonth" | "setUTCMonth" => {
if let Some(name) = obj {
self.write(&format!("{}.{}(", name, method));
} else {
self.write(&format!("js_date.{}(", method));
}
let defaults = ["0", "null"];
self.emit_args_with_defaults_i64(args, 2, &defaults);
self.write(")");
}
// setHours(hours, min?, sec?, ms?) → 4 slots
"setHours" | "setUTCHours" => {
if let Some(name) = obj {
self.write(&format!("{}.{}(", name, method));
} else {
self.write(&format!("js_date.{}(", method));
}
let defaults = ["0", "null", "null", "null"];
self.emit_args_with_defaults_i64(args, 4, &defaults);
self.write(")");
}
// setMinutes(min, sec?, ms?) → 3 slots
"setMinutes" | "setUTCMinutes" => {
if let Some(name) = obj {
self.write(&format!("{}.{}(", name, method));
} else {
self.write(&format!("js_date.{}(", method));
}
let defaults = ["0", "null", "null"];
self.emit_args_with_defaults_i64(args, 3, &defaults);
self.write(")");
}
// setSeconds(sec, ms?) → 2 slots
"setSeconds" | "setUTCSeconds" => {
if let Some(name) = obj {
self.write(&format!("{}.{}(", name, method));
} else {
self.write(&format!("js_date.{}(", method));
}
let defaults = ["0", "null"];
self.emit_args_with_defaults_i64(args, 2, &defaults);
self.write(")");
}
// Instance methods that need allocator
"toISOString" | "toString" | "toDateString" | "toTimeString" | "toJSON"
| "toLocaleString" | "toLocaleDateString" | "toLocaleTimeString" | "toUTCString" => {
if let Some(name) = obj {
self.write(&format!(
"try {}.{}(js_allocator.allocator())",
name, method
));
} else {
self.write(&format!("try js_date.{}(js_allocator.allocator())", method));
}
}
// UTC with default argument filling:
// Date.UTC(y) → js_date.utc(y, 0, 1, 0, 0, 0, 0)
// Date.UTC(y, m) → js_date.utc(y, m, 1, 0, 0, 0, 0)
// etc.
"utc" => {
self.write("js_date.utc(");
// Defaults: [y=1970, m=0, d=1, h=0, min=0, s=0, ms=0]
let defaults = ["1970", "0", "1", "0", "0", "0", "0"];
self.emit_args_with_defaults_i64(args, 7, &defaults);
self.write(")");
}
_ => {
// Static methods: js_date.method(args)
self.emit_module_call("js_date", method, args);
}
}
}
/// Emit a simple Date setter (single i64 argument): `obj.method(arg)`.
/// Coerces the argument to i64 to handle JsAny/F64 variables.
fn emit_date_setter_simple(
&mut self,
obj: Option<&str>,
method: &str,
args: &[crate::zigir::types::IrExpr],
) {
if let Some(name) = obj {
self.write(&format!("{}.{}(", name, method));
} else {
self.write(&format!("js_date.{}(", method));
}
if let Some(arg) = args.first() {
self.emit_i64_coerced(arg);
} else {
self.write("0");
}
self.write(")");
}
pub(super) fn emit_typedarray_builtin(
&mut self,
method: &str,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
ta_type_suffix: Option<&str>,
) {
if let Some(suffix) = ta_type_suffix {
self.write(&format!("js_runtime.js_typedarray.{}{}(", method, suffix));
if let Some(name) = obj {
self.write(name);
for arg in args.iter() {
self.write(", ");
self.emit_expr(arg);
}
} else {
for (i, arg) in args.iter().enumerate() {
if i > 0 {
self.write(", ");
}
self.emit_expr(arg);
}
}
self.write(")");
} else {
// No type suffix: use js_runtime.js_typedarray prefix (js_typedarray
// is not imported as a standalone module).
self.emit_module_call("js_runtime.js_typedarray", method, args);
}
}
pub(super) fn emit_uri_builtin(
&mut self,
method: &str,
_obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
) {
// URI methods: all need js_allocator.allocator() and specific catch patterns.
// encodeURI/encodeURIComponent: catch @panic("OOM: ...")
// decodeURI/decodeURIComponent: catch "" (outside try block)
match method {
"encodeURI" | "encodeURIComponent" => {
self.write(&format!("js_uri.{}(js_allocator.allocator(), ", method));
self.emit_inline_args(args);
self.write(&format!(") catch @panic(\"OOM: {}\")", method));
}
"decodeURI" | "decodeURIComponent" => {
self.write(&format!("js_uri.{}(js_allocator.allocator(), ", method));
self.emit_inline_args(args);
self.write(")");
if let Some(label) = &self.inside_try_block {
// Inside a try block: propagate the original error so
// the catch dispatch can map it to the correct JsError name
self.write(&format!(
" catch |err| break :{} @as(anyerror!void, err)",
label
));
} else {
// Not inside a try block: swallow error
self.write(" catch \"\"");
}
}
"parseInt" => {
self.write("js_uri.parseInt(");
self.emit_inline_args(args);
if args.len() < 2 {
self.write(", null");
}
self.write(")");
}
_ => {
self.emit_module_call("js_uri", method, args);
}
}
}
pub(super) fn emit_bigint_builtin(
&mut self,
method: &str,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
) {
match method {
// BigInt(x) → js_bigint.JsBigInt.fromValue(allocator, x) catch @panic("OOM: BigInt fromValue")
// fromValue uses comptime dispatch: string → init(), i64 → fromI64()
"bigIntConstructor" => {
self.write("(js_bigint.JsBigInt.fromValue(js_allocator.allocator(), ");
self.emit_inline_args(args);
self.write(") catch @panic(\"OOM: BigInt fromValue\"))");
}
"bigIntNeg" | "bigIntBitwiseNot" => {
// (-expr).method(allocator) catch @panic(...)
let (zig_method, panic_ctx) = if method == "bigIntNeg" {
("neg", "BigInt neg OOM")
} else {
("bitwiseNot", "BigInt bitwiseNot OOM")
};
self.write("(");
if let Some(o) = obj {
self.write(o);
}
self.write(&format!(
".{}(js_allocator.allocator()) catch @panic(\"{}\"))",
zig_method, panic_ctx
));
}
"toString" => {
// bigint.toString([radix]) → bigint.toString(allocator, radix) catch @panic(...)
// R8-P1-4: radix defaults to 10 per ECMA-262. Previously the
// radix argument was silently dropped. Fallible builtin call
// convention: catch @panic coerces the error union to []u8.
self.write("(");
if let Some(o) = obj {
self.write(o);
}
self.write(".toString(js_allocator.allocator(), ");
if !args.is_empty() {
self.emit_expr(&args[0]);
} else {
self.write("10");
}
self.write(") catch @panic(\"OOM: BigInt toString\"))");
}
"toLocaleString" => {
// toLocaleString() ignores any radix and is equivalent to
// toString(10) per JS spec (no radix parameter).
self.write("(");
if let Some(o) = obj {
self.write(o);
}
self.write(".toString(js_allocator.allocator(), 10) catch @panic(\"OOM: BigInt toString\"))");
}
"valueOf" => {
// bigint.valueOf() → returns self (identity)
if let Some(o) = obj {
self.write(o);
} else if !args.is_empty() {
self.emit_expr(&args[0]);
}
}
"asIntN" | "asUintN" => {
// BigInt.asIntN(width, bigint) / BigInt.asUintN(width, bigint)
// → js_bigint.asIntN(width, &bigint, allocator) / js_bigint.asUintN(...)
let zig_method = if method == "asIntN" {
"asIntN"
} else {
"asUintN"
};
self.write(&format!("(js_bigint.{}(", zig_method));
if args.len() >= 2 {
self.emit_expr(&args[0]);
self.write(", &");
self.emit_expr(&args[1]);
}
self.write(", js_allocator.allocator()) catch @panic(\"OOM: BigInt ");
self.write(zig_method);
self.write("\"))");
}
_ => {
self.emit_module_call("js_bigint", method, args);
}
}
}
pub(super) fn emit_collections_builtin(
&mut self,
method: &str,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
) {
match method {
// ── Map instance methods (use receiver) ──
"set" => {
// map.set(key, val) → (blk_N: { map.set(JsAny.from(key), JsAny.from(val)) catch @panic("OOM: allocation"); break :blk_N map })
// Returns the map object for chaining (JS semantics).
let blk = self.next_label();
if let Some(name) = obj {
self.write(&format!("({blk}: {{ {}.set(", name));
} else {
self.write(&format!("({blk}: {{ js_collections.set("));
}
if let Some(key) = args.first() {
self.write("JsAny.from(");
self.emit_expr(key);
self.write(")");
}
if args.len() >= 2 {
self.write(", JsAny.from(");
self.emit_expr(&args[1]);
self.write(")");
}
self.write(") catch @panic(\"OOM: allocation\"); ");
if let Some(name) = obj {
self.write(&format!("break :{blk} {}; }})", name));
} else {
// Degenerate path: no receiver name, cannot return the map.
// Emit as a non-chaining call (result ignored).
self.write("})");
}
}
"get" | "has" => {
// map.get(key)/map.has(key) → map.method(JsAny.from(key))
if let Some(name) = obj {
self.write(&format!("{}.{}(", name, method));
} else {
self.write(&format!("js_collections.{}(", method));
}
if let Some(key) = args.first() {
self.write("JsAny.from(");
self.emit_expr(key);
self.write(")");
}
self.write(")");
}
"delete" => {
// map.delete(key) → map.delete(js_allocator.allocator(), JsAny.from(key))
if let Some(name) = obj {
self.write(&format!("{}.delete(js_allocator.allocator(), ", name));
} else {
self.write("js_collections.delete(js_allocator.allocator(), ");
}
if let Some(key) = args.first() {
self.write("JsAny.from(");
self.emit_expr(key);
self.write(")");
}
self.write(")");
}
"clear" => {
if let Some(name) = obj {
self.write(&format!("{}.clear(js_allocator.allocator())", name));
} else {
self.write("js_collections.clear(js_allocator.allocator())");
}
}
"keys" | "values" | "entries" => {
// map.keys() etc. — need allocator
if let Some(name) = obj {
self.write(&format!(
"{}.{}(js_allocator.allocator()) catch @panic(\"OOM: allocation\")",
name, method
));
} else {
self.write(&format!("js_collections.{}(js_allocator.allocator()) catch @panic(\"OOM: allocation\")", method));
}
}
// ── Set instance methods ──
"add" => {
// set.add(val) → (blk_N: { set.add(JsAny.from(val)) catch @panic("OOM: allocation"); break :blk_N set })
// Returns the set object for chaining (JS semantics).
let blk = self.next_label();
if let Some(name) = obj {
self.write(&format!("({blk}: {{ {}.add(", name));
} else {
self.write(&format!("({blk}: {{ js_collections.add("));
}
if let Some(val) = args.first() {
self.write("JsAny.from(");
self.emit_expr(val);
self.write(")");
}
self.write(") catch @panic(\"OOM: allocation\"); ");
if let Some(name) = obj {
self.write(&format!("break :{blk} {}; }})", name));
} else {
// Degenerate path: no receiver name, cannot return the set.
self.write("})");
}
}
// ── forEach — handled by IrArrayCallbackInline, not here ──
"forEach" => {
// Fallback: should be handled by callback inline, not here
self.emit_module_call("js_collections", method, args);
}
// ── Default ──
_ => {
self.emit_module_call("js_collections", method, args);
}
}
}
pub(super) fn emit_runtime_builtin(
&mut self,
method: &str,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
) {
match method {
"deleteKey" => {
// obj.deleteKey("prop") → true
let blk = self.next_label();
self.write(&format!("{blk}: {{ _ = "));
if let Some(name) = obj {
self.write(name);
}
self.write(".deleteKey(");
for (i, arg) in args.iter().enumerate() {
if i > 0 {
self.write(", ");
}
self.emit_expr(arg);
}
self.write(&format!("); break :{blk} true; }}"));
}
"deleteByKey" => {
// obj.deleteByKey(expr, alloc) → true
// JsCollection.delete(alloc, JsAny.from(key)) returns bool
let blk = self.next_label();
self.write(&format!("{blk}: {{ const _dk = "));
if let Some(arg) = args.first() {
self.emit_expr(arg);
}
self.write("; _ = ");
if let Some(name) = obj {
self.write(name);
}
self.write(&format!(
".delete(js_allocator.allocator(), JsAny.from(_dk)); break :{blk} true; }}"
));
}
"instanceOf" => {
// instanceOf(value, "TypeName") → js_runtime.instanceOf(value, "TypeName")
// The value comes from obj_expr (not obj_name), already resolved by
// emit_builtin_call as `obj`. Fall through to emit_module_call with
// the receiver prepended as the first argument.
self.write("js_runtime.instanceOf(");
if let Some(name) = obj {
self.write(name);
self.write(", ");
}
self.emit_inline_args(args);
self.write(")");
}
_ => {
// Default: js_runtime.method(args)
self.emit_module_call("js_runtime", method, args);
}
}
}
}