builtin,v.gen.wasm: support -b wasm -d no_imports (#24188)

This commit is contained in:
Xyndra 2025-04-13 11:21:35 +02:00 committed by GitHub
parent 36ec2355b2
commit d559a62cfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 26 additions and 5 deletions

View File

@ -12,7 +12,11 @@ __global g_heap_base = usize(vwasm_heap_base())
@[unsafe]
pub fn malloc(n isize) &u8 {
if n <= 0 {
panic('malloc(n <= 0)')
$if no_imports ? {
return unsafe { nil }
} $else {
panic('malloc(n <= 0)')
}
}
res := g_heap_base

View File

@ -43,9 +43,11 @@ pub fn vwasm_memory_grow(size int) int {
@[unsafe]
pub fn vcalloc(n isize) &u8 {
if n <= 0 {
panic('vcalloc(n <= 0)')
} else if n == 0 {
return &u8(0)
$if no_imports ? {
return unsafe { nil }
} $else {
panic('valloc(n <= 0)')
}
}
res := unsafe { malloc(n) }

View File

@ -47,6 +47,9 @@ pub fn (mut g Gen) comptime_cond(cond ast.Expr, pkg_exists bool) bool {
ast.ComptimeCall {
return pkg_exists // more documentation needed here...
}
ast.PostfixExpr {
return g.comptime_if_to_ifdef((cond.expr as ast.Ident).name, true)
}
else {}
}
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
return false
}
else {
// note: this works but there might be some things missing from what I saw in the other platforms
// but it is better than nothing
if name in g.pref.compile_defines {
return true
} else {
return false
}
// taken from JS: what does it do??
/*
if is_comptime_option

View File

@ -16,6 +16,10 @@ For more general build help, see also `v help build`.
Overrides the default stack_top value used by the WebAssembly compiler.
Useful for some platforms with unusual memory requirements.
-d no_imports
Removes the default imports from the builtins. Useful for embedded targets where
you can't control the imports.
-os <browser|wasi>, -target-os <browser|wasi>
Change the target WebAssembly execution environment that V compiles for.
@ -25,7 +29,7 @@ For more general build help, see also `v help build`.
WASI provides a standardized interface to interact with the host operating system,
allowing WebAssembly modules to perform tasks like file I/O, networking, and more.
The specific version of the WASI specification targeted by V is 'wasi_snapshot_preview1'.
The `browser` target is an experimental environment that compiles for a stripped down
builtin, for use in browsers. The produced WebAssembly module will have functions
exported that are `pub` and inside the `module main`. See `examples/wasm/mandelbrot`