js: add draft support for -skip-unused (hello_world.v works)

pull/12170/head
Delyan Angelov 2021-10-12 19:22:16 +03:00
parent 1d2b56d71d
commit 6c728cf389
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 41 additions and 4 deletions

View File

@ -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'

View File

@ -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