Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add std.CreatePackage #715

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"strconv"
"time"
"strings"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/tm2/pkg/bech32"
Expand Down Expand Up @@ -483,6 +484,50 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
m.PushValue(res0)
},
)

// CreatePackage creates a new package from an existing path.
// Given a package path and the new path name, with optional trailing arguments for init,
// it creates a new package under the prefix of the current package.
//
// Example: a "template" GRC20 package is created under the std package("std/grc20").
// The init() of grc20 templates takes two arguments: the name of the token and the initial supply.
// To create a new GRC20 package from package "tokenfactory", we can use the following code:
// ```gno
// mytokenPackage := std.CreatePackage("std/grc20", "tokens/mytoken", 10000000000000).(IGRC20);
// ```
// This will create a new package under the current package, with the path "gno.land/r/tokenfactory/tokens/mytoken/grc20".
// The result is an interface{}, exposing all public functions of the package.
// Internally, the interface maps function names to internal native call, `CallPackageFunction`.
pn.DefineNative("CreatePackage",
gno.Flds(
"pkgTemplatePath", "string",
"pkgPath", "string",
"arguments", "...interface{}",
),
gno.Flds(
"pkg", "interface{}",
),
func(m *gno.Machine) {
arg0, arg1, arg2 := m.LastBlock().GetParams3()
pkgTemplatePath := arg0.TV.GetString()
pkgPath := strings.Join([]string{m.Package.PkgPath, arg1.TV.GetString()}, "/")
initArgs := arg2.TV.V.(*gno.SliceValue)

// TODO: validate pkgPath
tpkg := m.Store.Fork().GetMemPackage(pkgTemplatePath)
tpkg.RenamePath(pkgPath)

// TODO: this might be insecure due to shared execution environment.
_, pkg := m.RunMemPackage(tpkg, true)

res0 := gno.Go2GnoValue(
m.Alloc,
m.Store,
reflect.ValueOf(pkg),
)
m.PushValue(res0)
},
)
}
}

Expand Down
4 changes: 4 additions & 0 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type MemPackage struct {
Files []*MemFile
}

func (mempkg *MemPackage) RenamePath(newPath string) {
mempkg.Path = newPath
}

func (mempkg *MemPackage) GetFile(name string) *MemFile {
for _, memFile := range mempkg.Files {
if memFile.Name == name {
Expand Down