Skip to content

Commit

Permalink
feat: Add support for vm/qdoc
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 committed Jan 24, 2025
1 parent 4eda906 commit 752c34b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/reference/rpc-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ Call with the `/abci_query` to get information via the ABCI Query.
| `bank/balances/{ADDRESS}` | Returns the balance information about the account. |
| `vm/qfuncs` | Returns public facing function signatures as JSON. |
| `vm/qfile` | Returns the file bytes, or list of files if directory. |
| `vm/qdoc` | Returns JSON of the package doc, suitable for printing. |
| `vm/qrender` | Calls `.Render(<path>)` in readonly mode. |
| `vm/qeval` | Evaluates any expression in readonly mode and returns the results. |
| `vm/store` | (not yet supported) Fetches items from the store. |
Expand Down
17 changes: 17 additions & 0 deletions gno.land/pkg/sdk/vm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const (
QueryFuncs = "qfuncs"
QueryEval = "qeval"
QueryFile = "qfile"
QueryDoc = "qdoc"
)

func (vh vmHandler) Query(ctx sdk.Context, req abci.RequestQuery) abci.ResponseQuery {
Expand All @@ -95,6 +96,8 @@ func (vh vmHandler) Query(ctx sdk.Context, req abci.RequestQuery) abci.ResponseQ
res = vh.queryEval(ctx, req)
case QueryFile:
res = vh.queryFile(ctx, req)
case QueryDoc:
res = vh.queryDoc(ctx, req)

Check warning on line 100 in gno.land/pkg/sdk/vm/handler.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/handler.go#L99-L100

Added lines #L99 - L100 were not covered by tests
default:
return sdk.ABCIResponseQueryFromError(
std.ErrUnknownRequest(fmt.Sprintf(
Expand Down Expand Up @@ -197,6 +200,20 @@ func (vh vmHandler) queryFile(ctx sdk.Context, req abci.RequestQuery) (res abci.
return
}

// queryDoc returns the file bytes, or list of files if directory.
// if file, res.Value is []byte("file").
// if dir, res.Value is []byte("dir").
func (vh vmHandler) queryDoc(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
filepath := string(req.Data)
jsonDoc, err := vh.vm.QueryDoc(ctx, filepath)
if err != nil {
res = sdk.ABCIResponseQueryFromError(err)
return
}
res.Data = []byte(jsonDoc.JSON())
return

Check warning on line 214 in gno.land/pkg/sdk/vm/handler.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/handler.go#L206-L214

Added lines #L206 - L214 were not covered by tests
}

// ----------------------------------------
// misc

Expand Down
22 changes: 22 additions & 0 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/gnolang/gno/gnovm"
"github.com/gnolang/gno/gnovm/pkg/doc"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/stdlibs"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand Down Expand Up @@ -843,6 +844,27 @@ func (vm *VMKeeper) QueryFile(ctx sdk.Context, filepath string) (res string, err
}
}

var rePkgPathAndSymbol = regexp.MustCompile(`^(.+)\.([a-zA-Z0-9_]+)$`)

func (vm *VMKeeper) QueryDoc(ctx sdk.Context, input string) (*doc.JSONDocumentation, error) {
store := vm.newGnoTransactionStore(ctx) // throwaway (never committed)
pkgPath := input
symbol := ""
match := rePkgPathAndSymbol.FindStringSubmatch(input)
if len(match) == 3 {
// The input is <pkgpath>[.<symbol>
pkgPath = match[1]
symbol = match[2]
}

Check warning on line 858 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L849-L858

Added lines #L849 - L858 were not covered by tests

memPkg := store.GetMemPackage(pkgPath)
d, err := doc.NewDocumentableFromMemPkg(memPkg, true, symbol, "")
if err != nil {
return nil, err
}
return d.WriteJSONDocumentation()

Check warning on line 865 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L860-L865

Added lines #L860 - L865 were not covered by tests
}

// logTelemetry logs the VM processing telemetry
func logTelemetry(
gasUsed int64,
Expand Down

0 comments on commit 752c34b

Please sign in to comment.