Skip to content

Commit d559a62

Browse files
authored
builtin,v.gen.wasm: support -b wasm -d no_imports (#24188)
1 parent 36ec235 commit d559a62

File tree

9 files changed

+26
-5
lines changed

9 files changed

+26
-5
lines changed

vlib/builtin/wasm/alloc.v

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ __global g_heap_base = usize(vwasm_heap_base())
1212
@[unsafe]
1313
pub fn malloc(n isize) &u8 {
1414
if n <= 0 {
15-
panic('malloc(n <= 0)')
15+
$if no_imports ? {
16+
return unsafe { nil }
17+
} $else {
18+
panic('malloc(n <= 0)')
19+
}
1620
}
1721

1822
res := g_heap_base
File renamed without changes.

vlib/builtin/wasm/builtin.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ pub fn vwasm_memory_grow(size int) int {
4343
@[unsafe]
4444
pub fn vcalloc(n isize) &u8 {
4545
if n <= 0 {
46-
panic('vcalloc(n <= 0)')
47-
} else if n == 0 {
48-
return &u8(0)
46+
$if no_imports ? {
47+
return unsafe { nil }
48+
} $else {
49+
panic('valloc(n <= 0)')
50+
}
4951
}
5052

5153
res := unsafe { malloc(n) }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

vlib/v/gen/wasm/comptime.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub fn (mut g Gen) comptime_cond(cond ast.Expr, pkg_exists bool) bool {
4747
ast.ComptimeCall {
4848
return pkg_exists // more documentation needed here...
4949
}
50+
ast.PostfixExpr {
51+
return g.comptime_if_to_ifdef((cond.expr as ast.Ident).name, true)
52+
}
5053
else {}
5154
}
5255
g.w_error('wasm.comptime_cond(): unhandled node: ' + cond.type_name())
@@ -118,6 +121,14 @@ pub fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_option bool) bo
118121
return false
119122
}
120123
else {
124+
// note: this works but there might be some things missing from what I saw in the other platforms
125+
// but it is better than nothing
126+
if name in g.pref.compile_defines {
127+
return true
128+
} else {
129+
return false
130+
}
131+
121132
// taken from JS: what does it do??
122133
/*
123134
if is_comptime_option

vlib/v/help/build/build-wasm.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ For more general build help, see also `v help build`.
1616
Overrides the default stack_top value used by the WebAssembly compiler.
1717
Useful for some platforms with unusual memory requirements.
1818

19+
-d no_imports
20+
Removes the default imports from the builtins. Useful for embedded targets where
21+
you can't control the imports.
22+
1923
-os <browser|wasi>, -target-os <browser|wasi>
2024
Change the target WebAssembly execution environment that V compiles for.
2125

@@ -25,7 +29,7 @@ For more general build help, see also `v help build`.
2529
WASI provides a standardized interface to interact with the host operating system,
2630
allowing WebAssembly modules to perform tasks like file I/O, networking, and more.
2731
The specific version of the WASI specification targeted by V is 'wasi_snapshot_preview1'.
28-
32+
2933
The `browser` target is an experimental environment that compiles for a stripped down
3034
builtin, for use in browsers. The produced WebAssembly module will have functions
3135
exported that are `pub` and inside the `module main`. See `examples/wasm/mandelbrot`

0 commit comments

Comments
 (0)