Skip to content

Commit 2db2757

Browse files
committed
wasm: make integer/string builtins available to the -os browser target
`int.str()` (and the other int→string helpers), the `min_int`/`max_int` builtin constants, and `tos()` live in `int_notd_no_imports.v` / `string_notd_no_imports.v`, which were only compiled for `-os wasi`. So on `-os browser`, `println(int)`, `'${int}'` interpolation, integer concatenation, and `min_int`/`max_int` all failed (checker: "does not have a .str() function" / "undefined ident: max_int"; or at the encoder, "called function int.str does not exist"). These helpers are pure (no imports, no wasi/JS-specific calls), so move them from `vlib/builtin/wasm/wasi/` to the shared `vlib/builtin/wasm/` dir; both browser and wasi builds pick them up. No symbol conflicts — neither the browser nor the wasi builtin defines `tos` or `int.str`. Follow-up to #27450 (which fixed the browser `eprintln` panic). Adds vlib/v/gen/wasm/tests/browser_int_builtins_test.v.
1 parent 1704098 commit 2db2757

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

File renamed without changes.
File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
3+
// On the `-os browser` target, integer-to-string (`int.str()`), `println(int)`,
4+
// string interpolation/concat of integers, and the `min_int`/`max_int` builtin
5+
// constants used to fail — their implementations live in
6+
// `vlib/builtin/wasm/int_notd_no_imports.v` / `string_notd_no_imports.v`, which
7+
// were only compiled for `-os wasi`. They are now in the shared
8+
// `vlib/builtin/wasm/` dir, so browser builds pick them up too.
9+
fn test_wasm_browser_target_can_format_integers() {
10+
vexe := os.quoted_path(@VEXE)
11+
wrkdir := os.join_path(os.vtmp_dir(), 'wasm_browser_int_tests')
12+
os.mkdir_all(wrkdir)!
13+
defer {
14+
os.rmdir_all(wrkdir) or {}
15+
}
16+
17+
source_path := os.join_path(wrkdir, 'ints.v')
18+
output_path := os.join_path(wrkdir, 'ints.wasm')
19+
source := [
20+
'pub fn main() {',
21+
'\tprintln(42)', // println(int) needs int.str()
22+
'\tn := 7',
23+
"\tprintln('value=' + n.str())", // explicit int.str() + string concat
24+
'\t_ = max_int', // min_int/max_int builtin consts
25+
'\t_ = min_int',
26+
'}',
27+
].join_lines()
28+
os.write_file(source_path, source)!
29+
30+
res :=
31+
os.execute('${vexe} -b wasm -os browser -o ${os.quoted_path(output_path)} ${os.quoted_path(source_path)}')
32+
assert res.exit_code == 0, 'browser integer formatting failed to compile: ${res.output}'
33+
assert os.exists(output_path), 'missing browser wasm output'
34+
}

0 commit comments

Comments
 (0)