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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// zigir/emit/builtins/string.rs
// String builtin method emission.
use crate::zigir::emit::helpers::EmitterHelpers;
use crate::zigir::emit::Emitter;
impl Emitter {
/// Emit an argument coerced to i64, handling int, float, and JsAny inputs.
/// Used by fromCharCode/fromCodePoint (take `[]const i64`) and Date setters.
/// - JsAny expressions: `expr.asI64()` (extract i64 from union)
/// - Float expressions: `@as(i64, @intFromFloat(expr))` (truncation)
/// - Int expressions/literals: emit directly (already i64)
pub(crate) fn emit_i64_coerced(&mut self, arg: &crate::zigir::types::IrExpr) {
use crate::zigir::types::IrExpr;
match arg {
// JsAny-typed: use .asI64() to extract the integer value
IrExpr::TypedIdent {
ty: crate::types::ZigType::JsAny,
..
} => {
self.emit_expr(arg);
self.write(".asI64()");
}
// JsAny-returning builtins → .asI64()
IrExpr::BuiltinCall(bc) if bc.return_type == crate::types::ZigType::JsAny => {
self.emit_expr(arg);
self.write(".asI64()");
}
// Float-typed: @intFromFloat
IrExpr::TypedIdent {
ty: crate::types::ZigType::F64,
..
} => {
self.write("@as(i64, @intFromFloat(");
self.emit_expr(arg);
self.write("))");
}
// FloatLiteral → @intFromFloat
IrExpr::FloatLiteral(_) => {
self.write("@as(i64, @intFromFloat(");
self.emit_expr(arg);
self.write("))");
}
// Division/Rem/Pow produce f64 in JS → @intFromFloat
IrExpr::DivExpr { .. } | IrExpr::RemExpr { .. } | IrExpr::PowExpr { .. } => {
self.write("@as(i64, @intFromFloat(");
self.emit_expr(arg);
self.write("))");
}
// Float-producing builtins → @intFromFloat
IrExpr::BuiltinCall(bc) if bc.return_type == crate::types::ZigType::F64 => {
self.write("@as(i64, @intFromFloat(");
self.emit_expr(arg);
self.write("))");
}
// Already i64 or unknown — pass through
_ => {
self.emit_expr(arg);
}
}
}
pub(super) fn emit_string_builtin(
&mut self,
method: &str,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
regex_info: Option<&crate::zigir::types::IrRegexInfo>,
) {
// ── Regex-dependent methods: match / matchAll / search ──
match method {
"match" => {
self.emit_string_match(obj, args, regex_info);
return;
}
"matchAll" => {
self.emit_string_match_all(obj, regex_info);
return;
}
"search" => {
self.emit_string_search(obj, args, regex_info);
return;
}
// R8-P1-23: replace/replaceAll branch on regex_info for RegExp routing
"replace" => {
self.emit_string_replace(obj, args, regex_info);
return;
}
"replaceAll" => {
self.emit_string_replace_all(obj, args, regex_info);
return;
}
_ => {}
}
// ── Static variadic calls: String.fromCharCode / String.fromCodePoint ──
// These have signature `fn(Allocator, []const i64) ![]const u8`, so the
// emitter must prepend the allocator and pack all JS arguments into an
// `&[_]i64{ ... }` slice literal. (obj is None for these — they are
// static methods on the `String` constructor, not instance methods.)
match method {
"fromCharCode" => {
self.write("js_string.fromCharCode(js_allocator.allocator(), &[_]i64{");
for (i, arg) in args.iter().enumerate() {
if i > 0 {
self.write(", ");
}
self.emit_i64_coerced(arg);
}
self.write("}) catch @panic(\"OOM: string method\")");
return;
}
"fromCodePoint" => {
// R8-P1-18: invalid code points (cp < 0 or cp > 0x10FFFF)
// produce `error.RangeError` from the runtime. For now we
// route it to a hard panic; full JS-throw routing (marking
// the enclosing fn `can_throw` and returning `error.JsThrow`)
// is tracked as a future improvement.
self.write("js_string.fromCodePoint(js_allocator.allocator(), &[_]i64{");
for (i, arg) in args.iter().enumerate() {
if i > 0 {
self.write(", ");
}
self.emit_i64_coerced(arg);
}
self.write(
"}) catch |err| switch (err) { \
error.RangeError => @panic(\"fromCodePoint: invalid code point\"), \
else => @panic(\"OOM: string method\") }",
);
return;
}
_ => {}
}
// Method dispatch: JS name → Zig runtime name + allocator + fallible.
// ICU-dependent methods (localeCompare, normalize, toLocaleUpperCase,
// toLocaleLowerCase) are routed through js_string_icu instead of
// js_string. This module provides simplified implementations by default,
// and ICU4X-based implementations when needs_icu=true.
let (zig_method, needs_allocator, is_fallible, min_args, max_args, opt_defaults, module): (
&str,
bool,
bool,
usize,
usize,
&[&str],
&str, // "js_string" or "js_string_icu"
) = match method {
// ── No allocator, 0 args, non-fallible ──
"trim" => ("trim", false, false, 0, 0, &[], "js_string"),
"trimStart" => ("trimStart", false, false, 0, 0, &[], "js_string"),
"trimEnd" => ("trimEnd", false, false, 0, 0, &[], "js_string"),
// ── No allocator, 1 arg, non-fallible ──
"includes" => ("includes", false, false, 1, 1, &[], "js_string"),
"startsWith" => ("startsWith", false, false, 1, 1, &[], "js_string"),
"endsWith" => ("endsWith", false, false, 1, 1, &[], "js_string"),
"charCodeAt" => ("charCodeAt", false, false, 1, 1, &[], "js_string"),
"codePointAt" => ("codePointAt", false, false, 1, 1, &[], "js_string"),
// ── R8-P1-19: indexOf/lastIndexOf support optional fromIndex ──
// JS `indexOf(searchString, fromIndex)` defaults fromIndex=0
// (search from the start). JS `lastIndexOf(searchString, fromIndex)`
// defaults fromIndex=+∞ (search from the end); the runtime clamps
// any value ≥ len to len. We use `std.math.maxInt(i64)` as a Zig
// sentinel guaranteed ≥ any valid string length. Sign convention
// matches `slice`/`substring`.
"indexOf" => ("indexOf", false, false, 1, 2, &["0"], "js_string"),
"lastIndexOf" => (
"lastIndexOf",
false,
false,
1,
2,
&["std.math.maxInt(i64)"],
"js_string",
),
// ── No allocator, 1-2 args, non-fallible ──
"slice" => (
"slice",
false,
false,
1,
2,
&["std.math.maxInt(i64)"],
"js_string",
),
"substring" => (
"substring",
false,
false,
1,
2,
&["std.math.maxInt(i64)"],
"js_string",
),
// ── ICU-dependent: No allocator, 0-1 arg, non-fallible ──
"localeCompare" => ("localeCompare", false, false, 0, 1, &[], "js_string_icu"),
// ── With allocator, 0 args, fallible ──
"toUpperCase" => ("toUpper", true, true, 0, 0, &[], "js_string"),
// ── ICU-dependent: With allocator, 0 args, fallible ──
"toLocaleUpperCase" => ("toLocaleUpper", true, true, 0, 0, &[], "js_string_icu"),
"toLowerCase" => ("toLower", true, true, 0, 0, &[], "js_string"),
"toLocaleLowerCase" => ("toLocaleLower", true, true, 0, 0, &[], "js_string_icu"),
// ── With allocator, 1 arg, fallible ──
"charAt" => ("charAt", true, true, 1, 1, &[], "js_string"),
"at" => ("at", true, true, 1, 1, &[], "js_string"),
"concat" => ("concat", true, true, 1, 1, &[], "js_string"),
"repeat" => ("repeat", true, true, 1, 1, &[], "js_string"),
// ── With allocator, 1 arg, fallible (returns ![][]const u8) ──
"split" => ("split", true, true, 1, 1, &[], "js_string"),
// ── With allocator, 2 args, fallible ──
"padStart" => ("padStart", true, true, 2, 2, &[], "js_string"),
"padEnd" => ("padEnd", true, true, 2, 2, &[], "js_string"),
// ── ICU-dependent: With allocator, 0-1 arg, fallible ──
"normalize" => ("normalize", true, true, 0, 1, &["\"NFC\""], "js_string_icu"),
// ── Fallback ──
_ => {
// Unknown string method — naive emission
self.emit_module_call("js_string", method, args);
return;
}
};
// Emit: module.zig_method([js_allocator.allocator(), ]obj[, arg1[, arg2...]])[ catch @panic("OOM: string method")]
// Fallible string methods use catch @panic instead of try, consistent with
// regex methods (match/matchAll/search), BigInt init, and Map/Set operations.
// try would require the enclosing function to return an error union, which
// the transpiler does not currently propagate from builtin calls.
self.write(&format!("{}.{}(", module, zig_method));
if needs_allocator {
self.write("js_allocator.allocator(), ");
}
// Receiver object
if let Some(name) = obj {
self.write(name);
}
// Arguments (fill to max_args with opt_defaults for missing slots)
let n_args = args.len();
let total_slots = max_args;
for slot in 0..total_slots {
if slot < n_args {
self.write(", ");
self.emit_expr(&args[slot]);
} else {
let opt_idx = slot - min_args;
if let Some(default) = opt_defaults.get(opt_idx)
&& !default.is_empty()
{
self.write(&format!(", {}", default));
}
}
}
if is_fallible {
self.write(") catch @panic(\"OOM: string method\")");
} else {
self.write(")");
}
}
/// Shared by match/matchAll: emit `js_string_regex.{method}(js_allocator.allocator(), receiver, pattern_expr) catch @panic(...)`
/// for the is_var_ref and literal branches. The fallback branch uses js_string_regex.{method}(receiver).
fn emit_string_match_like(
&mut self,
obj: Option<&str>,
regex_info: Option<&crate::zigir::types::IrRegexInfo>,
method: &str,
) {
let receiver = obj.unwrap_or("\"\"");
match regex_info {
Some(ri) if ri.is_var_ref => {
if let Some(var) = &ri.var_name {
self.write(&format!(
"js_string_regex.{}(js_allocator.allocator(), {}, {}.pattern) catch @panic(\"OOM: allocation\")",
method, receiver, var
));
}
}
Some(ri) => {
if let Some(pattern) = &ri.pattern {
self.write(&format!(
"js_string_regex.{}(js_allocator.allocator(), {}, \"{}\") catch @panic(\"OOM: allocation\")",
method, receiver, pattern
));
}
}
None => {
self.write(&format!("js_string_regex.{}({})", method, receiver));
}
}
}
// ── String.match() ──────────────────────────────────
pub(super) fn emit_string_match(
&mut self,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
regex_info: Option<&crate::zigir::types::IrRegexInfo>,
) {
// match has an extra global-flag branch → matchStringGlobal
if let Some(ri) = regex_info
&& !ri.is_var_ref
&& ri.has_global
&& let Some(pattern) = &ri.pattern
{
let receiver = obj.unwrap_or("\"\"");
self.write(&format!(
"js_string_regex.matchStringGlobal(js_allocator.allocator(), {}, \"{}\") catch @panic(\"OOM: allocation\")",
receiver, pattern
));
return;
}
// Variable regex: runtime branch on .global to select matchStringGlobal vs matchString
if let Some(ri) = regex_info
&& ri.is_var_ref
&& let Some(var) = &ri.var_name
{
let receiver = obj.unwrap_or("\"\"");
self.write(&format!(
"(if ({}.global) js_string_regex.matchStringGlobal(js_allocator.allocator(), {}, {}.pattern) catch @panic(\"OOM: allocation\") else js_string_regex.matchString(js_allocator.allocator(), {}, {}.pattern) catch @panic(\"OOM: allocation\"))",
var, receiver, var, receiver, var
));
return;
}
// Non-RegExp argument (string literal, expression) or no regex_info.
// Only reach here when regex_info is None (non-RegExp argument).
// Use the first argument as the pattern string, or empty string if no args.
// JS spec: str.match(x) converts x to RegExp via new RegExp(x), which
// uses String(x) as the pattern. We pass the arg directly as the pattern.
if regex_info.is_none() {
let receiver = obj.unwrap_or("\"\"");
let pattern = match args.first() {
Some(arg) => {
// Render the arg expression to a string for inline embedding
let mut buf = String::new();
std::mem::swap(&mut self.output, &mut buf);
self.emit_expr(arg);
std::mem::swap(&mut self.output, &mut buf);
buf.trim().to_string()
}
None => "\"\"".to_string(),
};
self.write(&format!(
"js_string_regex.matchString(js_allocator.allocator(), {}, {}) catch @panic(\"OOM: allocation\")",
receiver, pattern
));
return;
}
// Literal RegExp without global flag — delegate to shared helper
self.emit_string_match_like(obj, regex_info, "matchString");
}
// ── String.matchAll() ───────────────────────────────
/// R8-P1-25: JS spec requires matchAll to throw TypeError if the RegExp
/// does not have the /g flag. Literal RegExp without /g is caught at
/// lower time (IrExpr::CompileError). Variable RegExp needs a runtime guard.
pub(super) fn emit_string_match_all(
&mut self,
obj: Option<&str>,
regex_info: Option<&crate::zigir::types::IrRegexInfo>,
) {
let receiver = obj.unwrap_or("\"\"");
match regex_info {
// No regex info — no RegExp argument, skip validation
None => {
self.write(&format!("js_string_regex.matchAllString({})", receiver));
}
// Literal RegExp with /g — valid, emit matchAllString
// (Literal without /g is caught at lower time — won't reach here)
Some(ri) if !ri.is_var_ref => {
if let Some(pattern) = &ri.pattern {
self.write(&format!(
"js_string_regex.matchAllString(js_allocator.allocator(), {}, \"{}\") catch @panic(\"OOM: allocation\")",
receiver, pattern
));
}
}
// Variable RegExp — runtime guard on .global field
Some(ri) if ri.is_var_ref => {
if let Some(var) = &ri.var_name {
self.write(&format!(
"(if (!{}.global) @panic(\"TypeError: String.prototype.matchAll called with a non-global RegExp argument\") else js_string_regex.matchAllString(js_allocator.allocator(), {}, {}.pattern) catch @panic(\"OOM: allocation\"))",
var, receiver, var
));
}
}
// Fallback (shouldn't reach)
Some(_) => {
self.emit_string_match_like(obj, regex_info, "matchAllString");
}
}
}
// ── String.search() ─────────────────────────────────
pub(super) fn emit_string_search(
&mut self,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
regex_info: Option<&crate::zigir::types::IrRegexInfo>,
) {
let receiver = obj.unwrap_or("\"\"");
match regex_info {
Some(ri) if ri.is_var_ref => {
if let Some(var) = &ri.var_name {
self.write(&format!(
"host_regex.regex_search({}.pattern, {})",
var, receiver
));
}
}
Some(ri) => {
if let Some(pattern) = &ri.pattern {
self.write(&format!(
"host_regex.regex_search(\"{}\", {})",
pattern, receiver
));
}
}
None => {
// Non-regex argument: render the first arg as a string pattern
if let Some(arg) = args.first() {
let (pattern, new_counter) =
Self::emit_expr_inline_with_label_offset(arg, self.label_counter);
self.label_counter = new_counter;
self.write(&format!(
"host_regex.regex_search({}, {})",
pattern, receiver
));
} else {
self.write(&format!("host_regex.regex_search(\"\", {})", receiver));
}
}
}
}
// ── String.replace() ──────────────────────────────────
/// R8-P1-23: Branch on regex_info for RegExp vs plain-string routing.
/// - None → js_string.replace(alloc, s, arg1, arg2) (plain-string, first-only)
/// - Some(ri) literal → js_string_regex.replaceRegex(alloc, s, "pattern", arg2)
/// - Some(ri) variable → js_string_regex.replaceRegex(alloc, s, var.pattern, arg2)
pub(super) fn emit_string_replace(
&mut self,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
regex_info: Option<&crate::zigir::types::IrRegexInfo>,
) {
let receiver = obj.unwrap_or("\"\"");
match regex_info {
// Variable RegExp → runtime regex replace with var.pattern
Some(ri) if ri.is_var_ref => {
if let Some(var) = &ri.var_name {
self.write(&format!(
"js_string_regex.replaceRegex(js_allocator.allocator(), {}, {}.pattern, ",
receiver, var
));
if let Some(arg) = args.get(1) {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(") catch @panic(\"OOM: string method\")");
}
}
// Literal RegExp → compile-time known pattern
Some(ri) => {
if let Some(pattern) = &ri.pattern {
self.write(&format!(
"js_string_regex.replaceRegex(js_allocator.allocator(), {}, \"{}\", ",
receiver, pattern
));
if let Some(arg) = args.get(1) {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(") catch @panic(\"OOM: string method\")");
}
}
// No regex info → plain-string replace (first occurrence only)
None => {
self.write("js_string.replace(js_allocator.allocator(), ");
self.write(receiver);
self.write(", ");
if let Some(arg) = args.first() {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(", ");
if let Some(arg) = args.get(1) {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(") catch @panic(\"OOM: string method\")");
}
}
}
// ── String.replaceAll() ───────────────────────────────
/// R8-P1-23: Branch on regex_info for RegExp vs plain-string routing.
///
/// - None → js_string.replaceAll(alloc, s, arg1, arg2) (plain-string)
/// - Some(ri) literal with /g → js_string_regex.replaceAllRegex(alloc, s, "pattern", arg2)
/// - Some(ri) variable with /g → js_string_regex.replaceAllRegex(alloc, s, var.pattern, arg2)
/// (runtime guard on .global for variable RegExp)
///
/// R8-P1-25: Literal RegExp without /g is caught at lower time (CompileError).
pub(super) fn emit_string_replace_all(
&mut self,
obj: Option<&str>,
args: &[crate::zigir::types::IrExpr],
regex_info: Option<&crate::zigir::types::IrRegexInfo>,
) {
let receiver = obj.unwrap_or("\"\"");
match regex_info {
// Variable RegExp → runtime guard on .global + regex replaceAll
Some(ri) if ri.is_var_ref => {
if let Some(var) = &ri.var_name {
self.write(&format!(
"(if (!{}.global) @panic(\"TypeError: String.prototype.replaceAll called with a non-global RegExp argument\") else js_string_regex.replaceAllRegex(js_allocator.allocator(), {}, {}.pattern, ",
var, receiver, var
));
if let Some(arg) = args.get(1) {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(") catch @panic(\"OOM: string method\"))");
}
}
// Literal RegExp with /g → compile-time known pattern
// (Literal without /g is caught at lower time — won't reach here)
Some(ri) => {
if let Some(pattern) = &ri.pattern {
self.write(&format!(
"js_string_regex.replaceAllRegex(js_allocator.allocator(), {}, \"{}\", ",
receiver, pattern
));
if let Some(arg) = args.get(1) {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(") catch @panic(\"OOM: string method\")");
}
}
// No regex info → plain-string replaceAll
None => {
self.write("js_string.replaceAll(js_allocator.allocator(), ");
self.write(receiver);
self.write(", ");
if let Some(arg) = args.first() {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(", ");
if let Some(arg) = args.get(1) {
self.emit_expr(arg);
} else {
self.write("\"\"");
}
self.write(") catch @panic(\"OOM: string method\")");
}
}
}
}