From a0bf79692685c07540c9b9dbb9280386d8407578 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 2 Nov 2020 00:57:40 +0100 Subject: [PATCH] autofree: handle SelectorExpr and fix json --- vlib/v/checker/checker.v | 7 ++++++- vlib/v/gen/fn.v | 9 +++++++-- vlib/vweb/vweb.v | 4 +++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3bb9d5eef5..65eb11e38f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1002,7 +1002,12 @@ pub fn (mut c Checker) call_expr(mut call_expr ast.CallExpr) table.Type { if arg.typ != table.string_type { continue } - if arg.expr is ast.Ident || arg.expr is ast.StringLiteral { + if arg.expr is ast.Ident || + arg.expr is ast.StringLiteral || arg.expr is ast.SelectorExpr { + // Simple expressions like variables, string literals, selector expressions + // (`x.field`) can't result in allocations and don't need to be assigned to + // temporary vars. + // Only expressions like `str + 'b'` need to be freed. continue } call_expr.args[i].is_tmp_autofree = true diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 7911fb24f0..3436cb0ceb 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -526,7 +526,9 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { tmp2 = g.new_tmp_var() g.writeln('Option_$typ $tmp2 = $fn_name ($json_obj);') } - g.write('cJSON_Delete($json_obj);') + if !g.pref.autofree { + g.write('cJSON_Delete($json_obj); //del') + } g.write('\n$cur_line') name = '' json_obj = tmp2 @@ -636,6 +638,9 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) { if !free_tmp_arg_vars { return } + if g.is_js_call { + return + } free_tmp_arg_vars = false // set the flag to true only if we have at least one arg to free g.tmp_count2++ mut scope := g.file.scope.innermost(node.pos.pos) @@ -778,7 +783,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) { if is_interface { g.expr(arg.expr) } else if use_tmp_var_autofree { - if arg.is_tmp_autofree { + if arg.is_tmp_autofree { // && !g.is_js_call { // We saved expressions in temp variables so that they can be freed later. // `foo(str + str2) => x := str + str2; foo(x); x.free()` // g.write('_arg_expr_${g.called_fn_name}_$i') diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index f4776def07..b4822cb433 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -350,7 +350,9 @@ fn handle_conn(conn net.Socket, mut app T) { //t := time.ticks() //mut action := '' mut route_words_a := [][]string{} - mut url_words := vals[1][1..].split('/').filter(it != '') + //mut url_words := vals[1][1..].split('/').filter(it != '') + x := vals[1][1..].split('/') + mut url_words := x.filter(it != '') if url_words.len == 0 {