Skip to content

Commit 8da68ae

Browse files
committed
feat: Add support for vm/qdoc
Signed-off-by: Jeff Thompson <[email protected]>
1 parent 9740a79 commit 8da68ae

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

docs/reference/rpc-endpoints.md

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ Call with the `/abci_query` to get information via the ABCI Query.
459459
| `bank/balances/{ADDRESS}` | Returns the balance information about the account. |
460460
| `vm/qfuncs` | Returns public facing function signatures as JSON. |
461461
| `vm/qfile` | Returns the file bytes, or list of files if directory. |
462+
| `vm/qdoc` | Returns the doc of a symbol, or list of symbols if a package. |
462463
| `vm/qrender` | Calls `.Render(<path>)` in readonly mode. |
463464
| `vm/qeval` | Evaluates any expression in readonly mode and returns the results. |
464465
| `vm/store` | (not yet supported) Fetches items from the store. |

gno.land/pkg/sdk/vm/handler.go

+17
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const (
7474
QueryFuncs = "qfuncs"
7575
QueryEval = "qeval"
7676
QueryFile = "qfile"
77+
QueryDoc = "qdoc"
7778
)
7879

7980
func (vh vmHandler) Query(ctx sdk.Context, req abci.RequestQuery) abci.ResponseQuery {
@@ -95,6 +96,8 @@ func (vh vmHandler) Query(ctx sdk.Context, req abci.RequestQuery) abci.ResponseQ
9596
res = vh.queryEval(ctx, req)
9697
case QueryFile:
9798
res = vh.queryFile(ctx, req)
99+
case QueryDoc:
100+
res = vh.queryDoc(ctx, req)
98101
default:
99102
return sdk.ABCIResponseQueryFromError(
100103
std.ErrUnknownRequest(fmt.Sprintf(
@@ -197,6 +200,20 @@ func (vh vmHandler) queryFile(ctx sdk.Context, req abci.RequestQuery) (res abci.
197200
return
198201
}
199202

203+
// queryDoc returns the file bytes, or list of files if directory.
204+
// if file, res.Value is []byte("file").
205+
// if dir, res.Value is []byte("dir").
206+
func (vh vmHandler) queryDoc(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
207+
filepath := string(req.Data)
208+
result, err := vh.vm.QueryDoc(ctx, filepath)
209+
if err != nil {
210+
res = sdk.ABCIResponseQueryFromError(err)
211+
return
212+
}
213+
res.Data = []byte(result)
214+
return
215+
}
216+
200217
// ----------------------------------------
201218
// misc
202219

gno.land/pkg/sdk/vm/keeper.go

+26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/gnolang/gno/gnovm"
19+
"github.com/gnolang/gno/gnovm/pkg/doc"
1920
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
2021
"github.com/gnolang/gno/gnovm/stdlibs"
2122
"github.com/gnolang/gno/tm2/pkg/crypto"
@@ -843,6 +844,31 @@ func (vm *VMKeeper) QueryFile(ctx sdk.Context, filepath string) (res string, err
843844
}
844845
}
845846

847+
var rePkgPathAndSymbol = regexp.MustCompile(`^(.+)\.([a-zA-Z0-9_]+)$`)
848+
849+
func (vm *VMKeeper) QueryDoc(ctx sdk.Context, input string) (res string, err error) {
850+
store := vm.newGnoTransactionStore(ctx) // throwaway (never committed)
851+
pkgPath := input
852+
symbol := ""
853+
match := rePkgPathAndSymbol.FindStringSubmatch(input)
854+
if len(match) == 3 {
855+
// The input is <pkgpath>[.<symbol>
856+
pkgPath = match[1]
857+
symbol = match[2]
858+
}
859+
860+
memPkg := store.GetMemPackage(pkgPath)
861+
doc, err := doc.NewDocumentableFromMemPkg(memPkg, true, symbol, "")
862+
if err != nil {
863+
return "", err
864+
}
865+
buf := &bytes.Buffer{}
866+
if err = doc.WriteDocumentation(buf, nil); err != nil {
867+
return "", err
868+
}
869+
return buf.String(), nil
870+
}
871+
846872
// logTelemetry logs the VM processing telemetry
847873
func logTelemetry(
848874
gasUsed int64,

0 commit comments

Comments
 (0)