Skip to content

Commit b2eb420

Browse files
committed
mcp: give wasm statements their own field
get_function put a wasm row's statement in "native", where a client expects machine assembly, with nothing saying otherwise. It now comes in "statement", and the tool's description says what a wasm function returns for the native view.
1 parent 6ee2fc4 commit b2eb420

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎internal/mcp/export.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ type SourceLineDTO struct {
4747
// function's own. Index counts the spacer rows the
4848
// viewer puts before jump targets, which RefOffset and Related use too.
4949
type AsmLineDTO struct {
50-
Index int `json:"index"`
51-
PC string `json:"pc"`
52-
Go string `json:"go,omitempty"`
53-
Native string `json:"native,omitempty"`
50+
Index int `json:"index"`
51+
PC string `json:"pc"`
52+
Go string `json:"go,omitempty"`
53+
Native string `json:"native,omitempty"`
54+
// Statement is what a wasm row completes, spelled like source, in
55+
// place of native assembly.
56+
Statement string `json:"statement,omitempty"`
5457
Indent int `json:"indent,omitempty"`
5558
File string `json:"file,omitempty"`
5659
Line int `json:"line,omitempty"`
@@ -139,6 +142,9 @@ func BuildFunctionCodeDTO(binary string, code *disasm.Code, store *comments.Stor
139142
}
140143
if opts.NativeAsm {
141144
line.Native = inst.NativeText
145+
if code.Statements {
146+
line.Native, line.Statement = "", inst.NativeText
147+
}
142148
line.NativeComment = store.ForAsm(code.Name, comments.ViewNativeAsm, inst.PC)
143149
}
144150
dto.Asm = append(dto.Asm, line)

‎internal/mcp/export_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mcp
2+
3+
import (
4+
"testing"
5+
6+
"loov.dev/lensm/internal/comments"
7+
"loov.dev/lensm/internal/disasm"
8+
)
9+
10+
// TestBuildFunctionCodeDTOStatements checks that statements come in
11+
// their own field, not as native assembly.
12+
func TestBuildFunctionCodeDTOStatements(t *testing.T) {
13+
code := &disasm.Code{Name: "f", Arch: "wasm", Statements: true, Insts: []disasm.Inst{
14+
{PC: 1, Text: "local.get 0"},
15+
{PC: 2, Text: "local.set 1", NativeText: "l1 = p0"},
16+
}}
17+
dto := BuildFunctionCodeDTO("m.wasm", code, comments.NewMemory("m.wasm"), ExportOptions{GoAsm: true, NativeAsm: true})
18+
if len(dto.Asm) != 2 {
19+
t.Fatalf("got %d rows", len(dto.Asm))
20+
}
21+
if got := dto.Asm[1]; got.Statement != "l1 = p0" || got.Native != "" {
22+
t.Errorf("got statement %q, native %q", got.Statement, got.Native)
23+
}
24+
}

‎internal/mcp/mcp.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ func mcpTools() []mcpTool {
510510
Title: "Get Function Code",
511511
Description: "Return a function's source, its assembly in Go and native syntax, which instructions each source line compiled to, and comments. " +
512512
"Source comes from the debug info and is missing when the files aren't on this machine. " +
513+
"A wasm function has no native assembly; the native view gives each row the statement it completes, such as \"l2 = l2 + i32[p0 + l1]\", in statement, and rows that only feed one have none. " +
513514
"Instructions come in pages of 500; when truncated is set, call again with offset.",
514515
InputSchema: objectSchema(map[string]any{
515516
"name": stringSchema("Exact function name."),

0 commit comments

Comments
 (0)