diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 9ccf4d78e5..ec6ebc813a 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -113,11 +113,12 @@ pub: // FieldData holds information about a field. Fields reside on structs. pub struct FieldData { pub: - name string - attrs []string - is_pub bool - is_mut bool - typ int + name string + attrs []string + is_pub bool + is_mut bool + is_shared bool + typ int } enum AttributeKind { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ee9266c9c3..552b4a57bf 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 16fe9394b1..54462a4998 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -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++ diff --git a/vlib/vweb/tests/vweb_test_server.v b/vlib/vweb/tests/vweb_test_server.v index f4bc61314f..68d8cb27fa 100644 --- a/vlib/vweb/tests/vweb_test_server.v +++ b/vlib/vweb/tests/vweb_test_server.v @@ -10,8 +10,13 @@ const ( struct App { vweb.Context - port int - timeout int + 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(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') } diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 762f2cde69..b673f11b89 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -324,6 +324,14 @@ pub fn run(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