Skip to content

jsgen: a pointer function cannot be called directly (fix #24417) #24459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion vlib/v/gen/js/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) {
name = g.generic_fn_name(node.concrete_types, name)
g.expr(it.left)

g.write('${name}(')
g.write(name)
if it.fn_var_type.is_ptr() {
g.write('.val')
}
g.write('(')
for i, arg in it.args {
g.expr(arg.expr)
if i != it.args.len - 1 {
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/gen/js/tests/function.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const hello = 'hello world!'

fn main() {
// calls a function directly
cb1 := fn () string {
return hello
}
assert cb1() == hello

// calls a function indirectly
cb2 := cb1
assert cb2() == hello

// calls a pointer function
cb3 := &cb1
assert cb3() == hello
Comment on lines +14 to +16
Copy link
Member

@spytheman spytheman May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A function is already a pointer to its start/body.
Why should this work (taking the address of a pointer, and then calling the function through that double pointer)?

Copy link
Member

@spytheman spytheman May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also does not work with the C backend, leading to a cgen error.


// calls a pointer function of a pointer function
cb4 := &cb3
assert cb4() == hello
}
Loading