1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

autofree: handle SelectorExpr and fix json

This commit is contained in:
Alexander Medvednikov 2020-11-02 00:57:40 +01:00
parent f0c98fb5c2
commit a0bf796926
3 changed files with 16 additions and 4 deletions

View File

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

View File

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

View File

@ -350,7 +350,9 @@ fn handle_conn<T>(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 {