comptime: fix $(field.name) in $for; vweb: shared fields

pull/10484/head
Alexander Medvednikov 2021-06-16 20:33:30 +03:00
parent b2e2a53f98
commit eacdd0d7e1
5 changed files with 40 additions and 13 deletions

View File

@ -117,6 +117,7 @@ pub:
attrs []string
is_pub bool
is_mut bool
is_shared bool
typ int
}

View File

@ -3973,12 +3973,7 @@ fn (mut c Checker) stmt(node ast.Stmt) {
c.branch_stmt(node)
}
ast.CompFor {
typ := c.unwrap_generic(node.typ)
sym := c.table.get_type_symbol(typ)
if sym.kind == .placeholder || typ.has_flag(.generic) {
c.error('unknown type `$sym.name`', node.typ_pos)
}
c.stmts(node.stmts)
c.comp_for(node)
}
ast.ConstDecl {
c.inside_const = true
@ -4149,6 +4144,18 @@ fn (mut c Checker) for_c_stmt(node ast.ForCStmt) {
c.in_for_count--
}
fn (mut c Checker) comp_for(node ast.CompFor) {
typ := c.unwrap_generic(node.typ)
sym := c.table.get_type_symbol(typ)
if sym.kind == .placeholder || typ.has_flag(.generic) {
c.error('unknown type `$sym.name`', node.typ_pos)
}
if node.kind == .fields {
c.comptime_fields_type[node.val_var] = node.typ
}
c.stmts(node.stmts)
}
fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
c.in_for_count++
prev_loop_label := c.loop_label

View File

@ -500,6 +500,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
g.writeln('\t${node.val_var}.typ = $styp;')
g.writeln('\t${node.val_var}.is_pub = $field.is_pub;')
g.writeln('\t${node.val_var}.is_mut = $field.is_mut;')
g.writeln('\t${node.val_var}.is_shared = ${field.typ.has_flag(.shared_f)};')
g.comptime_var_type_map['${node.val_var}.typ'] = styp
g.stmts(node.stmts)
i++

View File

@ -12,6 +12,11 @@ struct App {
vweb.Context
port int
timeout int
global_config shared Config
}
struct Config {
max_ping int
}
fn exit_after_timeout(timeout_in_ms int) {
@ -30,9 +35,13 @@ fn main() {
assert timeout > 0
go exit_after_timeout(timeout)
//
shared config := &Config{
max_ping: 50
}
app := &App{
port: http_port
timeout: timeout
global_config: config
}
eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.')
// vweb.run<App>(mut app, http_port)
@ -43,6 +52,7 @@ fn main() {
//}
pub fn (mut app App) index() vweb.Result {
assert app.global_config.max_ping == 50
return app.text('Welcome to VWeb')
}

View File

@ -324,6 +324,14 @@ pub fn run<T>(global_app &T, port int) {
} $else {
// println('vweb no db')
}
$for field in T.fields {
// if field.is_shared {
// println(field)
//}
//$if field.typ is string {
request_app.$(field.name) = global_app.$(field.name)
//}
}
request_app.Context = global_app.Context // copy the context ref that contains static files map etc
// request_app.Context = Context{
// conn: 0