-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathimports.go
300 lines (282 loc) · 9.63 KB
/
imports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package test
import (
"encoding/json"
"errors"
"fmt"
"go/token"
"io"
"math/big"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
"github.com/gnolang/gno/gnovm"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/packages"
teststdlibs "github.com/gnolang/gno/gnovm/tests/stdlibs"
teststd "github.com/gnolang/gno/gnovm/tests/stdlibs/std"
"github.com/gnolang/gno/tm2/pkg/db/memdb"
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
storetypes "github.com/gnolang/gno/tm2/pkg/store/types"
)
type StoreOptions struct {
// WithExtern interprets imports of packages under "github.com/gnolang/gno/_test/"
// as imports under the directory in gnovm/tests/files/extern.
// This should only be used for GnoVM internal filetests (gnovm/tests/files).
WithExtern bool
// PreprocessOnly instructs the PackageGetter to run the imported files using
// [gno.Machine.PreprocessFiles]. It avoids executing code for contexts
// which only intend to perform a type check, ie. `gno lint`.
PreprocessOnly bool
}
// NOTE: this isn't safe, should only be used for testing.
func Store(
rootDir string,
stdin io.Reader,
stdout, stderr io.Writer,
) (
baseStore storetypes.CommitStore,
resStore gno.Store,
) {
return StoreWithOptions(rootDir, stdin, stdout, stderr, StoreOptions{})
}
// StoreWithOptions is a variant of [Store] which additionally accepts a
// [StoreOptions] argument.
func StoreWithOptions(
rootDir string,
stdin io.Reader,
stdout, stderr io.Writer,
opts StoreOptions,
) (
baseStore storetypes.CommitStore,
resStore gno.Store,
) {
processMemPackage := func(m *gno.Machine, memPkg *gnovm.MemPackage, save bool) (*gno.PackageNode, *gno.PackageValue) {
return m.RunMemPackage(memPkg, save)
}
if opts.PreprocessOnly {
processMemPackage = func(m *gno.Machine, memPkg *gnovm.MemPackage, save bool) (*gno.PackageNode, *gno.PackageValue) {
m.Store.AddMemPackage(memPkg)
return m.PreprocessFiles(memPkg.Name, memPkg.Path, gno.ParseMemPackage(memPkg), save, false)
}
}
getPackage := func(pkgPath string, store gno.Store) (pn *gno.PackageNode, pv *gno.PackageValue) {
if pkgPath == "" {
panic(fmt.Sprintf("invalid zero package path in testStore().pkgGetter"))
}
if opts.WithExtern {
// if _test package...
const testPath = "github.com/gnolang/gno/_test/"
if strings.HasPrefix(pkgPath, testPath) {
baseDir := filepath.Join(rootDir, "gnovm", "tests", "files", "extern", pkgPath[len(testPath):])
memPkg := gno.MustReadMemPackage(baseDir, pkgPath)
send := std.Coins{}
ctx := Context(pkgPath, send)
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Store: store,
Context: ctx,
})
return processMemPackage(m2, memPkg, true)
}
}
// gonative exceptions.
// These are values available using gonative; eventually they should all be removed.
switch pkgPath {
case "os":
pkg := gno.NewPackageNode("os", pkgPath, nil)
pkg.DefineGoNativeValue("Stdin", stdin)
pkg.DefineGoNativeValue("Stdout", stdout)
pkg.DefineGoNativeValue("Stderr", stderr)
return pkg, pkg.NewPackage()
case "fmt":
pkg := gno.NewPackageNode("fmt", pkgPath, nil)
pkg.DefineGoNativeValue("Println", func(a ...interface{}) (n int, err error) {
// NOTE: uncomment to debug long running tests
// fmt.Println(a...)
res := fmt.Sprintln(a...)
return stdout.Write([]byte(res))
})
pkg.DefineGoNativeValue("Printf", func(format string, a ...interface{}) (n int, err error) {
res := fmt.Sprintf(format, a...)
return stdout.Write([]byte(res))
})
pkg.DefineGoNativeValue("Print", func(a ...interface{}) (n int, err error) {
res := fmt.Sprint(a...)
return stdout.Write([]byte(res))
})
pkg.DefineGoNativeValue("Sprint", fmt.Sprint)
pkg.DefineGoNativeValue("Sprintf", fmt.Sprintf)
pkg.DefineGoNativeValue("Sprintln", fmt.Sprintln)
pkg.DefineGoNativeValue("Sscanf", fmt.Sscanf)
pkg.DefineGoNativeValue("Errorf", fmt.Errorf)
pkg.DefineGoNativeValue("Fprintln", fmt.Fprintln)
pkg.DefineGoNativeValue("Fprintf", fmt.Fprintf)
pkg.DefineGoNativeValue("Fprint", fmt.Fprint)
return pkg, pkg.NewPackage()
case "encoding/json":
pkg := gno.NewPackageNode("json", pkgPath, nil)
pkg.DefineGoNativeValue("Unmarshal", json.Unmarshal)
pkg.DefineGoNativeValue("Marshal", json.Marshal)
return pkg, pkg.NewPackage()
case "os_test":
pkg := gno.NewPackageNode("os_test", pkgPath, nil)
pkg.DefineNative("Sleep",
gno.Flds( // params
"d", gno.AnyT(), // NOTE: should be time.Duration
),
gno.Flds( // results
),
func(m *gno.Machine) {
// For testing purposes here, nanoseconds are separately kept track.
arg0 := m.LastBlock().GetParams1().TV
d := arg0.GetInt64()
sec := d / int64(time.Second)
nano := d % int64(time.Second)
ctx := m.Context.(*teststd.TestExecContext)
ctx.Timestamp += sec
ctx.TimestampNano += nano
if ctx.TimestampNano >= int64(time.Second) {
ctx.Timestamp += 1
ctx.TimestampNano -= int64(time.Second)
}
m.Context = ctx
},
)
return pkg, pkg.NewPackage()
case "math/big":
pkg := gno.NewPackageNode("big", pkgPath, nil)
pkg.DefineGoNativeValue("NewInt", big.NewInt)
return pkg, pkg.NewPackage()
}
// Load normal stdlib.
pn, pv = loadStdlib(rootDir, pkgPath, store, stdout, opts.PreprocessOnly)
if pn != nil {
return
}
// if examples package...
examplePath := filepath.Join(rootDir, "examples", pkgPath)
if osm.DirExists(examplePath) {
memPkg := gno.MustReadMemPackage(examplePath, pkgPath)
if memPkg.IsEmpty() {
panic(fmt.Sprintf("found an empty package %q", pkgPath))
}
send := std.Coins{}
ctx := Context(pkgPath, send)
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Store: store,
Context: ctx,
})
return processMemPackage(m2, memPkg, true)
}
return nil, nil
}
db := memdb.NewMemDB()
baseStore = dbadapter.StoreConstructor(db, storetypes.StoreOptions{})
// Make a new store.
resStore = gno.NewStore(nil, baseStore, baseStore)
resStore.SetPackageGetter(getPackage)
resStore.SetNativeResolver(teststdlibs.NativeResolver)
return
}
func loadStdlib(rootDir, pkgPath string, store gno.Store, stdout io.Writer, preprocessOnly bool) (*gno.PackageNode, *gno.PackageValue) {
dirs := [...]string{
// Normal stdlib path.
filepath.Join(rootDir, "gnovm", "stdlibs", pkgPath),
// Override path. Definitions here override the previous if duplicate.
filepath.Join(rootDir, "gnovm", "tests", "stdlibs", pkgPath),
}
files := make([]string, 0, 32) // pre-alloc 32 as a likely high number of files
for _, path := range dirs {
dl, err := os.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
continue
}
panic(fmt.Errorf("could not access dir %q: %w", path, err))
}
for _, f := range dl {
// NOTE: RunMemPackage has other rules; those should be mostly useful
// for on-chain packages (ie. include README and gno.mod).
if !f.IsDir() && strings.HasSuffix(f.Name(), ".gno") {
files = append(files, filepath.Join(path, f.Name()))
}
}
}
if len(files) == 0 {
return nil, nil
}
memPkg := gno.MustReadMemPackageFromList(files, pkgPath)
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
// NOTE: see also pkgs/sdk/vm/builtins.go
// Needs PkgPath != its name because TestStore.getPackage is the package
// getter for the store, which calls loadStdlib, so it would be recursively called.
PkgPath: "stdlibload",
Output: stdout,
Store: store,
})
if preprocessOnly {
m2.Store.AddMemPackage(memPkg)
return m2.PreprocessFiles(memPkg.Name, memPkg.Path, gno.ParseMemPackage(memPkg), true, true)
}
// TODO: make this work when using gno lint.
return m2.RunMemPackageWithOverrides(memPkg, true)
}
type stackWrappedError struct {
err error
stack []byte
}
func (e *stackWrappedError) Error() string { return e.err.Error() }
func (e *stackWrappedError) Unwrap() error { return e.err }
func (e *stackWrappedError) String() string {
return fmt.Sprintf("%v\nstack:\n%v", e.err, string(e.stack))
}
// LoadImports parses the given MemPackage and attempts to retrieve all pure packages
// from the store. This is mostly useful for "eager import loading", whereby all
// imports are pre-loaded in a permanent store, so that the tests can use
// ephemeral transaction stores.
func LoadImports(store gno.Store, memPkg *gnovm.MemPackage) (err error) {
defer func() {
// This is slightly different from other similar error handling; we do not have a
// machine to work with, as this comes from an import; so we need
// "machine-less" alternatives. (like v.String instead of v.Sprint)
if r := recover(); r != nil {
switch v := r.(type) {
case *gno.TypedValue:
err = errors.New(v.String())
case *gno.PreprocessError:
err = v.Unwrap()
case gno.UnhandledPanicError:
err = v
case error:
err = &stackWrappedError{v, debug.Stack()}
default:
err = &stackWrappedError{fmt.Errorf("%v", v), debug.Stack()}
}
}
}()
fset := token.NewFileSet()
importsMap, err := packages.Imports(memPkg, fset)
if err != nil {
return err
}
imports := importsMap.Merge(packages.FileKindPackageSource, packages.FileKindTest, packages.FileKindXTest)
for _, imp := range imports {
if gno.IsRealmPath(imp.PkgPath) {
// Don't eagerly load realms.
// Realms persist state and can change the state of other realms in initialization.
continue
}
pkg := store.GetPackage(imp.PkgPath, true)
if pkg == nil {
return fmt.Errorf("%v: unknown import path %v", fset.Position(imp.Spec.Pos()).String(), imp.PkgPath)
}
}
return nil
}