Skip to content

Commit

Permalink
markused: add -skip-unused for programs that import x.vweb too (d…
Browse files Browse the repository at this point in the history
…o not skip unused routing methods)
  • Loading branch information
spytheman committed Dec 30, 2023
1 parent 923b410 commit 9694024
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/fn.v
Expand Up @@ -2489,7 +2489,7 @@ fn (mut c Checker) post_process_generic_fns() ! {
for concrete_types in gtypes {
c.table.cur_concrete_types = concrete_types
c.fn_decl(mut node)
if node.name in ['vweb.run', 'vweb.run_at'] {
if node.name in ['x.vweb.run', 'x.vweb.run_at', 'vweb.run', 'vweb.run_at'] {
for ct in concrete_types {
if ct !in c.vweb_gen_types {
c.vweb_gen_types << ct
Expand Down
58 changes: 31 additions & 27 deletions vlib/v/markused/markused.v
Expand Up @@ -304,33 +304,8 @@ pub fn mark_used(mut table ast.Table, pref_ &pref.Preferences, ast_files []&ast.
}
}

// handle vweb magic router methods:
typ_vweb_result := table.find_type_idx('vweb.Result')
if typ_vweb_result != 0 {
all_fn_root_names << 'vweb.filter'
typ_vweb_context := ast.Type(table.find_type_idx('vweb.Context')).set_nr_muls(1)
all_fn_root_names << '${int(typ_vweb_context)}.html'
for vgt in table.used_vweb_types {
sym_app := table.sym(vgt)
for m in sym_app.methods {
mut skip := true
if m.name == 'before_request' {
// TODO: handle expansion of method calls in generic functions in a more universal way
skip = false
}
if m.return_type == typ_vweb_result {
skip = false
}
//
if skip {
continue
}
pvgt := vgt.set_nr_muls(1)
// eprintln('vgt: $vgt | pvgt: $pvgt | sym_app.name: $sym_app.name | m.name: $m.name')
all_fn_root_names << '${int(pvgt)}.${m.name}'
}
}
}
handle_vweb(mut table, mut all_fn_root_names, 'vweb.Result', 'vweb.filter', 'vweb.Context')
handle_vweb(mut table, mut all_fn_root_names, 'x.vweb.Result', 'x.vweb.filter', 'x.vweb.Context')

// handle ORM drivers:
orm_connection_implementations := table.iface_types['orm.Connection'] or { []ast.Type{} }
Expand Down Expand Up @@ -464,3 +439,32 @@ fn all_fn_const_and_global(ast_files []&ast.File) (map[string]ast.FnDecl, map[st
}
return all_fns, all_consts, all_globals
}

fn handle_vweb(mut table ast.Table, mut all_fn_root_names []string, result_name string, filter_name string, context_name string) {
// handle vweb magic router methods:
result_type_idx := table.find_type_idx(result_name)
if result_type_idx != 0 {
all_fn_root_names << filter_name
typ_vweb_context := ast.Type(table.find_type_idx(context_name)).set_nr_muls(1)
all_fn_root_names << '${int(typ_vweb_context)}.html'
for vgt in table.used_vweb_types {
sym_app := table.sym(vgt)
for m in sym_app.methods {
mut skip := true
if m.name == 'before_request' {
// TODO: handle expansion of method calls in generic functions in a more universal way
skip = false
}
if m.return_type == result_type_idx {
skip = false
}
if skip {
continue
}
pvgt := vgt.set_nr_muls(1)
// eprintln('vgt: $vgt | pvgt: $pvgt | sym_app.name: $sym_app.name | m.name: $m.name')
all_fn_root_names << '${int(pvgt)}.${m.name}'
}
}
}
}
2 changes: 2 additions & 0 deletions vlib/v/tests/skip_unused/x_vweb_run_at.run.out
@@ -0,0 +1,2 @@
[Vweb] Running app on http://localhost:38090/
done
2 changes: 2 additions & 0 deletions vlib/v/tests/skip_unused/x_vweb_run_at.skip_unused.run.out
@@ -0,0 +1,2 @@
[Vweb] Running app on http://localhost:38090/
done
23 changes: 23 additions & 0 deletions vlib/v/tests/skip_unused/x_vweb_run_at.vv
@@ -0,0 +1,23 @@
import x.vweb
import time

pub struct App {}

pub struct Context {
vweb.Context
}

fn main() {
spawn fn () {
time.sleep(100 * time.millisecond)
println('done')
exit(0)
}()
mut app := &App{}
vweb.run_at[App, Context](mut app, port: 38090)!
}

@['/']
pub fn (app &App) index(mut ctx Context) vweb.Result {
return ctx.text('Hello World')
}

0 comments on commit 9694024

Please sign in to comment.