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

checker, cgen: fix go call fn using map value (#15665)

This commit is contained in:
yuyi 2022-09-05 22:16:28 +08:00 committed by GitHub
parent 90c2c5b8a4
commit d649f5aff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -617,6 +617,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
if elem_sym.info is ast.FnType {
func = elem_sym.info.func
found = true
node.is_fn_var = true
node.fn_var_type = sym.info.elem_type
} else {
c.error('cannot call the element of the array, it is not a function',
node.pos)
@ -626,6 +628,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
if value_sym.info is ast.FnType {
func = value_sym.info.func
found = true
node.is_fn_var = true
node.fn_var_type = sym.info.value_type
} else {
c.error('cannot call the value of the map, it is not a function', node.pos)
}
@ -634,6 +638,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
if elem_sym.info is ast.FnType {
func = elem_sym.info.func
found = true
node.is_fn_var = true
node.fn_var_type = sym.info.elem_type
} else {
c.error('cannot call the element of the array, it is not a function',
node.pos)

View File

@ -1722,6 +1722,17 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
g.gen_anon_fn_decl(mut expr.left)
name = expr.left.decl.name
}
} else if expr.left is ast.IndexExpr {
if expr.is_fn_var {
fn_sym := g.table.sym(expr.fn_var_type)
func := (fn_sym.info as ast.FnType).func
fn_var := g.fn_var_signature(func.return_type, func.params.map(it.typ), tmp_fn)
g.write('\t$fn_var = ')
g.expr(expr.left)
g.writeln(';')
name = fn_sym.cname
use_tmp_fn_var = true
}
}
name = util.no_dots(name)
if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__') {

View File

@ -0,0 +1,16 @@
module main
fn test_go_call_fn_using_map_value() {
sum := fn (x int, y int) int {
return x + y
}
mut fns := map[string]fn (int, int) int{}
fns['sum'] = sum
g := go fns['sum'](2, 3)
x := g.wait()
println('$x')
assert x == 5
}