From 1b9a8b996dafa08d698eda1da0e6459ee122bfe9 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 10 Nov 2024 05:17:12 +0200 Subject: [PATCH] checker,pref: fix `$if wasm32_emscripten {` check, add tests (#22816) --- vlib/v/ast/comptime_valid_idents.v | 5 ++- vlib/v/checker/comptime.v | 14 +++++--- .../platform_wrapper/file_default.c.v | 5 +++ .../testdata/platform_wrapper/file_linux.c.v | 10 ++++++ .../testdata/platform_wrapper/file_macos.c.v | 10 ++++++ .../file_wasm32_emscripten.c.v | 10 ++++++ .../platform_wrapper/file_windows.c.v | 10 ++++++ .../platform_wrapper_emscripten.c.must_have | 9 +++++ .../c/testdata/platform_wrapper_emscripten.vv | 35 +++++++++++++++++++ .../platform_wrapper_non_emscripten.out | 7 ++++ .../platform_wrapper_non_emscripten.vv | 33 +++++++++++++++++ vlib/v/pref/os.v | 13 ++++--- 12 files changed, 150 insertions(+), 11 deletions(-) create mode 100644 vlib/v/gen/c/testdata/platform_wrapper/file_default.c.v create mode 100644 vlib/v/gen/c/testdata/platform_wrapper/file_linux.c.v create mode 100644 vlib/v/gen/c/testdata/platform_wrapper/file_macos.c.v create mode 100644 vlib/v/gen/c/testdata/platform_wrapper/file_wasm32_emscripten.c.v create mode 100644 vlib/v/gen/c/testdata/platform_wrapper/file_windows.c.v create mode 100644 vlib/v/gen/c/testdata/platform_wrapper_emscripten.c.must_have create mode 100644 vlib/v/gen/c/testdata/platform_wrapper_emscripten.vv create mode 100644 vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.out create mode 100644 vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.vv diff --git a/vlib/v/ast/comptime_valid_idents.v b/vlib/v/ast/comptime_valid_idents.v index e7aa530ae..de8c48c9a 100644 --- a/vlib/v/ast/comptime_valid_idents.v +++ b/vlib/v/ast/comptime_valid_idents.v @@ -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'] diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 6d274368c..b8e21ec48 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -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 diff --git a/vlib/v/gen/c/testdata/platform_wrapper/file_default.c.v b/vlib/v/gen/c/testdata/platform_wrapper/file_default.c.v new file mode 100644 index 000000000..26f6bfe9c --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper/file_default.c.v @@ -0,0 +1,5 @@ +module platform_wrapper + +pub fn abc() int { + return 987654321 +} diff --git a/vlib/v/gen/c/testdata/platform_wrapper/file_linux.c.v b/vlib/v/gen/c/testdata/platform_wrapper/file_linux.c.v new file mode 100644 index 000000000..b28b45ca6 --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper/file_linux.c.v @@ -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 +} diff --git a/vlib/v/gen/c/testdata/platform_wrapper/file_macos.c.v b/vlib/v/gen/c/testdata/platform_wrapper/file_macos.c.v new file mode 100644 index 000000000..c59ae3cee --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper/file_macos.c.v @@ -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 +} diff --git a/vlib/v/gen/c/testdata/platform_wrapper/file_wasm32_emscripten.c.v b/vlib/v/gen/c/testdata/platform_wrapper/file_wasm32_emscripten.c.v new file mode 100644 index 000000000..8f9e0fd93 --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper/file_wasm32_emscripten.c.v @@ -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 +} diff --git a/vlib/v/gen/c/testdata/platform_wrapper/file_windows.c.v b/vlib/v/gen/c/testdata/platform_wrapper/file_windows.c.v new file mode 100644 index 000000000..bcdc3cd62 --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper/file_windows.c.v @@ -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 +} diff --git a/vlib/v/gen/c/testdata/platform_wrapper_emscripten.c.must_have b/vlib/v/gen/c/testdata/platform_wrapper_emscripten.c.must_have new file mode 100644 index 000000000..9b699eedd --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper_emscripten.c.must_have @@ -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(); diff --git a/vlib/v/gen/c/testdata/platform_wrapper_emscripten.vv b/vlib/v/gen/c/testdata/platform_wrapper_emscripten.vv new file mode 100644 index 000000000..d20696c8e --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper_emscripten.vv @@ -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') +} diff --git a/vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.out b/vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.out new file mode 100644 index 000000000..72e25ff86 --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.out @@ -0,0 +1,7 @@ +start +--- 1 +> inside else branch of if emscripten +--- 2 +> inside else branch of if wasm32_emscripten +--- 3 +done diff --git a/vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.vv b/vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.vv new file mode 100644 index 000000000..50d1ce2b4 --- /dev/null +++ b/vlib/v/gen/c/testdata/platform_wrapper_non_emscripten.vv @@ -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') +} diff --git a/vlib/v/pref/os.v b/vlib/v/pref/os.v index 3a38f192f..9d46aac30 100644 --- a/vlib/v/pref/os.v +++ b/vlib/v/pref/os.v @@ -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' } @@ -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 } -- 2.39.5