diff --git a/cmd/tools/vtest-all.v b/cmd/tools/vtest-all.v index ae3ea5b5a0..aaf74da119 100644 --- a/cmd/tools/vtest-all.v +++ b/cmd/tools/vtest-all.v @@ -67,10 +67,20 @@ fn get_all_commands() []Command { line: '$vexe -o - examples/hello_world.v | grep "#define V_COMMIT_HASH" > /dev/null' okmsg: 'V prints the generated source code to stdout with `-o -` .' } - } - res << Command{ - line: '$vexe run examples/v_script.vsh' - okmsg: 'V can run the .VSH script file examples/v_script.vsh' + res << Command{ + line: '$vexe run examples/v_script.vsh > /dev/null' + okmsg: 'V can run the .VSH script file examples/v_script.vsh' + } + res << Command{ + line: '$vexe -b js -o hw.js examples/hello_world.v' + okmsg: 'V compiles hello_world.v on the JS backend' + rmfile: 'hw.js' + } + res << Command{ + line: '$vexe -skip-unused -b js -o hw_skip_unused.js examples/hello_world.v' + okmsg: 'V compiles hello_world.v on the JS backend, with -skip-unused' + rmfile: 'hw_skip_unused.js' + } } res << Command{ line: '$vexe -o vtmp cmd/v' diff --git a/vlib/v/gen/js/fn.v b/vlib/v/gen/js/fn.v index 734042d207..f6edb996d8 100644 --- a/vlib/v/gen/js/fn.v +++ b/vlib/v/gen/js/fn.v @@ -381,6 +381,27 @@ fn (g &JsGen) fn_gen_type(it &ast.FnDecl) FnGenType { } } +fn (mut g JsGen) is_used_by_main(node ast.FnDecl) bool { + mut is_used_by_main := true + if g.pref.skip_unused { + fkey := node.fkey() + is_used_by_main = g.table.used_fns[fkey] + $if trace_skip_unused_fns ? { + println('> is_used_by_main: $is_used_by_main | node.name: $node.name | fkey: $fkey | node.is_method: $node.is_method') + } + if !is_used_by_main { + $if trace_skip_unused_fns_in_js_code ? { + g.writeln('// trace_skip_unused_fns_in_js_code, $node.name, fkey: $fkey') + } + } + } else { + $if trace_skip_unused_fns_in_js_code ? { + g.writeln('// trace_skip_unused_fns_in_js_code, $node.name, fkey: $node.fkey()') + } + } + return is_used_by_main +} + fn (mut g JsGen) gen_fn_decl(it ast.FnDecl) { res := g.fn_gen_type(it) if it.language == .js { @@ -389,6 +410,12 @@ fn (mut g JsGen) gen_fn_decl(it ast.FnDecl) { if g.inside_builtin { g.builtin_fns << it.name } + if !g.is_used_by_main(it) { + return + } + if it.should_be_skipped { + return + } cur_fn_decl := g.fn_decl g.gen_method_decl(it, res) g.fn_decl = cur_fn_decl