js: fix callbacks in structure parameters (fix #24260) (#24324)

This commit is contained in:
Gonzalo Chumillas 2025-04-26 16:16:33 +01:00 committed by GitHub
parent b98ca31e26
commit 8f2528528f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View File

@ -391,7 +391,7 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) {
if it.should_be_skipped {
return
}
if it.is_method && g.table.sym(it.receiver_type).name.starts_with('JS.') {
if it.is_method && (it.is_field || g.table.sym(it.receiver_type).name.starts_with('JS.')) {
g.js_method_call(it)
return
} else if it.name.starts_with('JS.') {

View File

@ -0,0 +1,38 @@
struct FooParams {
name string
update fn (name string) @[required]
}
fn foo(params FooParams) {
params.update(params.name)
}
fn main() {
// anonymous function callback
foo(
name: 'item 1'
update: fn (name string) {
println('update ${name}')
}
)
// lambda function callback
update := fn (name string) {
println('update ${name}')
}
foo(name: 'item 2', update: update)
// anonymous function field
item_3 := FooParams{
update: fn (name string) {
println('update ${name}')
}
}
item_3.update('item 3')
// lambda function field
item_4 := FooParams{
update: update
}
item_4.update('item 4')
}