diff --git a/vlib/builtin/wasm/alloc.v b/vlib/builtin/wasm/alloc.v index 3b73156bbc..4a9d3287fd 100644 --- a/vlib/builtin/wasm/alloc.v +++ b/vlib/builtin/wasm/alloc.v @@ -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 diff --git a/vlib/builtin/wasm/browser/builtin.v b/vlib/builtin/wasm/browser/builtin_notd_no_imports.v similarity index 100% rename from vlib/builtin/wasm/browser/builtin.v rename to vlib/builtin/wasm/browser/builtin_notd_no_imports.v diff --git a/vlib/builtin/wasm/builtin.v b/vlib/builtin/wasm/builtin.v index c2650929c5..9c9942667a 100644 --- a/vlib/builtin/wasm/builtin.v +++ b/vlib/builtin/wasm/builtin.v @@ -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) } diff --git a/vlib/builtin/wasm/wasi/builtin.v b/vlib/builtin/wasm/wasi/builtin_notd_no_imports.v similarity index 100% rename from vlib/builtin/wasm/wasi/builtin.v rename to vlib/builtin/wasm/wasi/builtin_notd_no_imports.v diff --git a/vlib/builtin/wasm/wasi/int.v b/vlib/builtin/wasm/wasi/int_notd_no_imports.v similarity index 100% rename from vlib/builtin/wasm/wasi/int.v rename to vlib/builtin/wasm/wasi/int_notd_no_imports.v diff --git a/vlib/builtin/wasm/wasi/string.v b/vlib/builtin/wasm/wasi/string_notd_no_imports.v similarity index 100% rename from vlib/builtin/wasm/wasi/string.v rename to vlib/builtin/wasm/wasi/string_notd_no_imports.v diff --git a/vlib/builtin/wasm/wasi/wasi.v b/vlib/builtin/wasm/wasi/wasi_notd_no_imports.v similarity index 100% rename from vlib/builtin/wasm/wasi/wasi.v rename to vlib/builtin/wasm/wasi/wasi_notd_no_imports.v diff --git a/vlib/v/gen/wasm/comptime.v b/vlib/v/gen/wasm/comptime.v index 2e662f7736..5266c1ed46 100644 --- a/vlib/v/gen/wasm/comptime.v +++ b/vlib/v/gen/wasm/comptime.v @@ -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 diff --git a/vlib/v/help/build/build-wasm.txt b/vlib/v/help/build/build-wasm.txt index 08a7cc10cd..d7431b3ac9 100644 --- a/vlib/v/help/build/build-wasm.txt +++ b/vlib/v/help/build/build-wasm.txt @@ -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 , -target-os 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`