Skip to content

Commit

Permalink
checker,pref: fix $if wasm32_emscripten { check, add tests (vlang#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman authored Nov 10, 2024
1 parent c7acf27 commit 1b9a8b9
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 11 deletions.
5 changes: 2 additions & 3 deletions vlib/v/ast/comptime_valid_idents.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ module ast

pub const valid_comptime_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu', 'qnx',
'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux', 'solaris',
'haiku', 'serenity', 'vinix', 'plan9']
'haiku', 'serenity', 'vinix', 'plan9', 'wasm32_emscripten']
pub const valid_comptime_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
pub const valid_comptime_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64',
'rv32']
pub const valid_comptime_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian']
pub const valid_comptime_if_other = ['apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding',
'interpreter', 'es5', 'profile', 'wasm32', 'wasm32_emscripten', 'wasm32_wasi', 'fast_math',
'native', 'autofree']
'interpreter', 'es5', 'profile', 'wasm32', 'wasm32_wasi', 'fast_math', 'native', 'autofree']
pub const valid_comptime_not_user_defined = all_valid_comptime_idents()
pub const valid_comptime_compression_types = ['none', 'zlib']

Expand Down
14 changes: 10 additions & 4 deletions vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -895,12 +895,18 @@ fn (mut c Checker) comptime_if_cond(mut cond ast.Expr, pos token.Pos) ComptimeBr
is_user_ident = false
ident_name = cname
if cname in ast.valid_comptime_if_os {
mut is_os_target_equal := true
mut ident_result := ComptimeBranchSkipState.skip
if !c.pref.output_cross_c {
target_os := c.pref.os.str().to_lower_ascii()
is_os_target_equal = cname == target_os
if cname_enum_val := pref.os_from_string(cname) {
if cname_enum_val == c.pref.os {
ident_result = .eval
}
}
}
$if trace_comptime_os_checks ? {
eprintln('>>> ident_name: ${ident_name} | c.pref.os: ${c.pref.os} | ident_result: ${ident_result}')
}
return if is_os_target_equal { .eval } else { .skip }
return ident_result
} else if cname in ast.valid_comptime_if_compilers {
return if pref.cc_from_string(cname) == c.pref.ccompiler_type {
.eval
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper/file_default.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module platform_wrapper

pub fn abc() int {
return 987654321
}
10 changes: 10 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper/file_linux.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module platform_wrapper

pub fn abc() int {
return 987654321
}

pub fn fn_defined_on_linux() int {
// println('hi from ${@FN}, in file_linux.c.v')
return 456
}
10 changes: 10 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper/file_macos.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module platform_wrapper

pub fn abc() int {
return 987654321
}

pub fn fn_defined_on_macos() int {
// println('hi from ${@FN}, in file_macos.c.v')
return 789
}
10 changes: 10 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper/file_wasm32_emscripten.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module platform_wrapper

pub fn abc() int {
return 987654321
}

pub fn fn_defined_in_wasm32_emscripten() int {
println('hi from ${@FN}, defined in file_wasm32_emscripten.c.v')
return 12345
}
10 changes: 10 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper/file_windows.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module platform_wrapper

pub fn abc() int {
return 987654321
}

pub fn fn_defined_on_windows() int {
// println('hi from ${@FN}, in file_windows.c.v')
return 123
}
9 changes: 9 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper_emscripten.c.must_have
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#if defined(CUSTOM_DEFINE_emscripten)
println(_SLIT("> inside then branch of if emscripten"));

#if defined(__EMSCRIPTEN__)
println(_SLIT("> inside then branch of if wasm32_emscripten"));

v__gen__c__testdata__platform_wrapper__fn_defined_in_wasm32_emscripten();

v__gen__c__testdata__platform_wrapper__abc();
35 changes: 35 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper_emscripten.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module main

// vtest vflags: -os wasm32_emscripten

import platform_wrapper

fn main() {
println('start')
$if windows {
assert platform_wrapper.fn_defined_on_windows() == 123
}
$if linux {
assert platform_wrapper.fn_defined_on_linux() == 456
}
$if macos {
assert platform_wrapper.fn_defined_on_macos() == 789
}
println('--- 1')
$if emscripten ? {
println('> inside then branch of if emscripten')
assert platform_wrapper.fn_defined_in_wasm32_emscripten() == 12345
} $else {
println('> inside else branch of if emscripten')
}
println('--- 2')
$if wasm32_emscripten {
println('> inside then branch of if wasm32_emscripten')
assert platform_wrapper.fn_defined_in_wasm32_emscripten() == 12345
} $else {
println('> inside else branch of if wasm32_emscripten')
}
println('--- 3')
platform_wrapper.abc()
println('done')
}
7 changes: 7 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
start
--- 1
> inside else branch of if emscripten
--- 2
> inside else branch of if wasm32_emscripten
--- 3
done
33 changes: 33 additions & 0 deletions vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module main

import platform_wrapper

fn main() {
println('start')
$if windows {
assert platform_wrapper.fn_defined_on_windows() == 123
}
$if linux {
assert platform_wrapper.fn_defined_on_linux() == 456
}
$if macos {
assert platform_wrapper.fn_defined_on_macos() == 789
}
println('--- 1')
$if emscripten ? {
println('> inside then branch of if emscripten')
assert platform_wrapper.fn_defined_in_wasm32_emscripten() == 12345
} $else {
println('> inside else branch of if emscripten')
}
println('--- 2')
$if wasm32_emscripten {
println('> inside then branch of if wasm32_emscripten')
assert platform_wrapper.fn_defined_in_wasm32_emscripten() == 12345
} $else {
println('> inside else branch of if wasm32_emscripten')
}
println('--- 3')
platform_wrapper.abc()
println('done')
}
13 changes: 9 additions & 4 deletions vlib/v/pref/os.v
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ pub fn os_from_string(os_str string) !OS {
}

pub fn (o OS) str() string {
// TODO: check more thoroughly, why this method needs to exist at all,
// and why should it override the default autogenerated .str() method,
// instead of being named something like .label() ...
// It seems to serve only display purposes on the surface, but it is used
// internally by the compiler for comptime comparisons, which seems very
// error prone. It bugged the interpretation of `$if wasm32_emscripten {` for example.
match o {
._auto { return 'RESERVED: AUTO' }
.ios { return 'iOS' }
Expand Down Expand Up @@ -173,10 +179,9 @@ pub fn get_host_os() OS {
$if emscripten ? {
return .wasm32_emscripten
}
// TODO: make this work:
// $if wasm32_emscripten {
// return .wasm32_emscripten
// }
$if wasm32_emscripten {
return .wasm32_emscripten
}
$if linux {
return .linux
}
Expand Down

0 comments on commit 1b9a8b9

Please sign in to comment.