From 6aa4f45c42cd9cb405452e951cdfd1febee9616b Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 8 Feb 2023 12:04:05 +0100 Subject: [PATCH] interp: wrap source functions when used as input parameters. It allows to use interpreter functions as parameters in the runtime, even for defered callbacks, or when passed as empty interfaces, as for runtime.SetFinalizer. Fixes #1503 --- _test/fun28.go | 25 +++++++++++++++++++++++++ interp/run.go | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 _test/fun28.go diff --git a/_test/fun28.go b/_test/fun28.go new file mode 100644 index 000000000..c502b4c9e --- /dev/null +++ b/_test/fun28.go @@ -0,0 +1,25 @@ +package main + +import ( + "runtime" +) + +type T struct { + name string +} + +func finalize(t *T) { println("finalize") } + +func newT() *T { + t := new(T) + runtime.SetFinalizer(t, finalize) + return t +} + +func main() { + t := newT() + println(t != nil) +} + +// Output: +// true diff --git a/interp/run.go b/interp/run.go index c2fb5689c..a8cc80b7b 100644 --- a/interp/run.go +++ b/interp/run.go @@ -1561,6 +1561,8 @@ func callBin(n *node) { values = append(values, genValue(c)) case isInterfaceSrc(c.typ): values = append(values, genValueInterfaceValue(c)) + case isFuncSrc(c.typ): + values = append(values, genFunctionWrapper(c)) case c.typ.cat == arrayT || c.typ.cat == variadicT: if isEmptyInterface(c.typ.val) { values = append(values, genValueArray(c))