Skip to content

Commit efc91ed

Browse files
committed
fix(vm): show func for safe calls in disassembly
Previously, safe builtin functions (OpCallSafe) were pushed to the stack as constants (OpPush), causing the disassembler/debugger to display their memory address (e.g., 0x140000...) instead of their name (e.g., concat). This change updates the compiler to store the function name in debugInfo when emitting OpCallSafe. The VM's disassembler now checks debugInfo for OpPush arguments and displays the stored name if available. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
1 parent ad49544 commit efc91ed

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

compiler/compiler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,9 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) {
11031103
if f.Fast != nil {
11041104
c.emit(OpCallBuiltin1, id)
11051105
} else if f.Safe != nil {
1106-
c.emit(OpPush, c.addConstant(f.Safe))
1106+
id := c.addConstant(f.Safe)
1107+
c.emit(OpPush, id)
1108+
c.debugInfo[fmt.Sprintf("const_%d", id)] = node.Name
11071109
c.emit(OpCallSafe, len(node.Arguments))
11081110
} else if f.Func != nil {
11091111
c.emitFunction(f, len(node.Arguments))

test/issues/567/issue_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package expr_test
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
8+
"github.com/expr-lang/expr"
9+
"github.com/expr-lang/expr/internal/testify/require"
10+
)
11+
12+
func TestIssue567(t *testing.T) {
13+
program, err := expr.Compile("concat(1..2, 3..4)")
14+
require.NoError(t, err)
15+
16+
var buf bytes.Buffer
17+
program.DisassembleWriter(&buf)
18+
output := buf.String()
19+
20+
// Check if "concat" is mentioned in the output
21+
require.True(t, strings.Contains(output, "concat"), "expected 'concat' in disassembly output")
22+
23+
// It should appear as a pushed constant
24+
require.True(t, strings.Contains(output, "OpPush\t<4>\tconcat"), "expected 'OpPush <4> concat' in disassembly output")
25+
}

vm/program.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func (program *Program) DisassembleWriter(w io.Writer) {
112112
} else {
113113
c = "out of range"
114114
}
115+
if name, ok := program.debugInfo[fmt.Sprintf("const_%d", arg)]; ok {
116+
c = name
117+
}
115118
if r, ok := c.(*regexp.Regexp); ok {
116119
c = r.String()
117120
}

0 commit comments

Comments
 (0)