From ebbe286333da3b37a8e8f4912c4c5b7fc77e429e Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 25 Feb 2025 21:41:28 +0100 Subject: [PATCH] deplace internal/os_test-> os --- examples/gno.land/p/demo/flow/io_test.gno | 2 +- .../p/demo/tamagotchi/z0_filetest.gno | 2 +- examples/no_cycles_test.go | 2 +- gnovm/pkg/gnolang/frame.go | 21 +---- gnovm/pkg/gnolang/gonative.go | 88 ++----------------- gnovm/pkg/gnolang/machine.go | 4 - gnovm/pkg/gnolang/op_call.go | 14 --- gnovm/pkg/gnolang/values_conversions.go | 4 +- gnovm/stdlibs/testing/testing.gno | 15 ++-- gnovm/tests/stdlibs/fmt/fmt.gno | 29 +++--- gnovm/tests/stdlibs/generated.go | 47 +++++----- .../tests/stdlibs/internal/os_test/sleep.gno | 7 -- gnovm/tests/stdlibs/internal/os_test/sleep.go | 24 ----- gnovm/tests/stdlibs/os/os.gno | 16 ++-- gnovm/tests/stdlibs/os/os.go | 18 ++++ 15 files changed, 89 insertions(+), 204 deletions(-) delete mode 100644 gnovm/tests/stdlibs/internal/os_test/sleep.gno delete mode 100644 gnovm/tests/stdlibs/internal/os_test/sleep.go diff --git a/examples/gno.land/p/demo/flow/io_test.gno b/examples/gno.land/p/demo/flow/io_test.gno index 6b64a29431f..fc5afd66c96 100644 --- a/examples/gno.land/p/demo/flow/io_test.gno +++ b/examples/gno.land/p/demo/flow/io_test.gno @@ -9,7 +9,7 @@ import ( "testing" "time" - ios_test "os_test" + ios_test "os" ) // XXX ugh, I can't even sleep milliseconds. diff --git a/examples/gno.land/p/demo/tamagotchi/z0_filetest.gno b/examples/gno.land/p/demo/tamagotchi/z0_filetest.gno index 2aebd9d4aae..bd3090bb03e 100644 --- a/examples/gno.land/p/demo/tamagotchi/z0_filetest.gno +++ b/examples/gno.land/p/demo/tamagotchi/z0_filetest.gno @@ -1,7 +1,7 @@ package main import ( - "os_test" + "os" "time" "gno.land/p/demo/tamagotchi" diff --git a/examples/no_cycles_test.go b/examples/no_cycles_test.go index 4c89c112178..b458c62e5c5 100644 --- a/examples/no_cycles_test.go +++ b/examples/no_cycles_test.go @@ -19,7 +19,7 @@ import ( // XXX: move this into `gno lint` -var injectedTestingLibs = []string{"encoding/json", "fmt", "os", "os_test"} +var injectedTestingLibs = []string{"encoding/json", "fmt", "os"} // TestNoCycles checks that there is no import cycles in stdlibs and non-draft examples func TestNoCycles(t *testing.T) { diff --git a/gnovm/pkg/gnolang/frame.go b/gnovm/pkg/gnolang/frame.go index 70db5531745..32b9f2404cd 100644 --- a/gnovm/pkg/gnolang/frame.go +++ b/gnovm/pkg/gnolang/frame.go @@ -22,8 +22,7 @@ type Frame struct { NumBlocks int // number of blocks in stack // call frame - Func *FuncValue // function value - //GoFunc *NativeValue // go function value + Func *FuncValue // function value Receiver TypedValue // if bound method NumArgs int // number of arguments in call IsVarg bool // is form fncall(???, vargs...) @@ -47,17 +46,6 @@ func (fr Frame) String() string { fr.NumBlocks, fr.LastPackage.PkgPath, fr.LastRealm) - /*} else if fr.GoFunc != nil { - return fmt.Sprintf("[FRAME GOFUNC:%v RECV:%s (%d args) %d/%d/%d/%d/%d]", - fr.GoFunc.Value, - fr.Receiver, - fr.NumArgs, - fr.NumOps, - fr.NumValues, - fr.NumExprs, - fr.NumStmts, - fr.NumBlocks) - */ } else { return fmt.Sprintf("[FRAME LABEL: %s %d/%d/%d/%d/%d]", fr.Label, @@ -70,7 +58,7 @@ func (fr Frame) String() string { } func (fr *Frame) IsCall() bool { - return fr.Func != nil //|| fr.GoFunc != nil + return fr.Func != nil } func (fr *Frame) PushDefer(dfr Defer) { @@ -91,7 +79,7 @@ func (fr *Frame) PopDefer() (res Defer, ok bool) { type Defer struct { Func *FuncValue // function value - //GoFunc *NativeValue // go function value + // GoFunc *NativeValue // go function value Args []TypedValue // arguments Source *DeferStmt // source Parent *Block @@ -128,9 +116,6 @@ func (s Stacktrace) String() string { case call.Frame.Func != nil: fmt.Fprintf(&builder, "%s\n", toExprTrace(cx)) fmt.Fprintf(&builder, " %s/%s:%d\n", call.Frame.Func.PkgPath, call.Frame.Func.FileName, call.Stmt.GetLine()) - //case call.Frame.GoFunc != nil: - // fmt.Fprintf(&builder, "%s\n", toExprTrace(cx)) - // fmt.Fprintf(&builder, " gofunction:%s\n", call.Frame.GoFunc.Value.Type()) default: panic("StacktraceCall has a non-call Frame") } diff --git a/gnovm/pkg/gnolang/gonative.go b/gnovm/pkg/gnolang/gonative.go index 59f98deb945..f406087b128 100644 --- a/gnovm/pkg/gnolang/gonative.go +++ b/gnovm/pkg/gnolang/gonative.go @@ -60,15 +60,13 @@ func go2GnoBaseType(rt reflect.Type) Type { return Float32Type case reflect.Float64: return Float64Type - //case reflect.Array: case reflect.Slice: return &SliceType{ Elt: go2GnoType(rt.Elem()), Vrd: false, } - // return &NativeType{Type: rt} - //case reflect.Chan: - // return &NativeType{Type: rt} + case reflect.Chan: + panic("not yet implemented") case reflect.Func: return getFuncType(rt) case reflect.Interface: @@ -86,11 +84,11 @@ func go2GnoBaseType(rt reflect.Type) Type { it.Methods = fs fmt.Println("here") return it - //case reflect.Map: + // case reflect.Map: // return &NativeType{Type: rt} - //case reflect.Ptr: + // case reflect.Ptr: // return &NativeType{Type: rt} - //case reflect.Struct: + // case reflect.Struct: // return &NativeType{Type: rt} case reflect.UnsafePointer: panic("not yet implemented") @@ -253,6 +251,7 @@ func (ds *defaultStore) go2GnoFuncType(rt reflect.Type) *FuncType { return ft } + func getFuncType(rt reflect.Type) *FuncType { // predefine func type ft := &FuncType{} @@ -364,7 +363,7 @@ func go2GnoValue(alloc *Allocator, rv reflect.Value) (tv TypedValue) { case reflect.Func: panic("not yet implemented") case reflect.Interface: - panic("not yet implemented") + panic("should not happen") case reflect.Map: panic("not yet implemented") case reflect.Ptr: @@ -700,22 +699,9 @@ func go2GnoValue2(alloc *Allocator, store Store, rv reflect.Value, recursive boo case reflect.Chan: panic("not yet implemented") case reflect.Func: - // NOTE: the type may be a full gno type, either a - // *FuncType or *DeclaredType. The value may still be a - // *NativeValue though, and the function can be called - // regardless. - //tv.V = alloc.New(rv) + panic("not yet implemented") case reflect.Interface: - // special case for errors, which are very often used especially in - // native bindings - //if rv.Type() == tError { - // tv.T = gErrorType - // if !rv.IsNil() { - // tv.V = alloc.NewNative(rv.Elem()) - // } - // return - //} - panic(rv.Type()) + panic("not yet implemented") case reflect.Map: panic("not yet implemented") case reflect.Ptr: @@ -1194,7 +1180,6 @@ func gno2GoValue(tv *TypedValue, rv reflect.Value) (ret reflect.Value) { // PackageNode methods func (x *PackageNode) DefineGoNativeValue(name Name, nv interface{}) { - fmt.Println("DefineGoNativeValue", name, nv) x.defineGoNativeValue(false, name, nv) } @@ -1215,61 +1200,6 @@ func (x *PackageNode) defineGoNativeValue(isConst bool, n Name, nv interface{}) x.Define2(isConst, n, tv.T, tv) } -// ---------------------------------------- -// Machine methods - -// NOTE: Unlike doOpCall(), doOpCallGoNative() also handles -// conversions, similarly to doOpConvert(). -/*func (m *Machine) doOpCallGoNative() { - fr := m.LastFrame() - fv := fr.GoFunc - ft := fv.Value.Type() - hasVarg := ft.IsVariadic() - numParams := ft.NumIn() - isVarg := fr.IsVarg - // pop and convert params. - ptvs := m.PopCopyValues(fr.NumArgs) - m.PopValue() // func value - prvs := make([]reflect.Value, 0, len(ptvs)) - for i := 0; i < fr.NumArgs; i++ { - ptv := &ptvs[i] - var it reflect.Type - if hasVarg && numParams-1 <= i && !isVarg { - it = ft.In(numParams - 1) - it = it.Elem() - } else { - it = ft.In(i) - } - erv := reflect.New(it).Elem() - prvs = append(prvs, gno2GoValue(ptv, erv)) - } - // call and get results. - rrvs := []reflect.Value(nil) - if isVarg { - rrvs = fv.Value.CallSlice(prvs) - } else { - rrvs = fv.Value.Call(prvs) - } - // convert and push results. - for _, rvs := range rrvs { - // TODO instead of this shallow conversion, - // look at expected Gno type and convert appropriately. - rtv := go2GnoValue(m.Alloc, rvs) - m.PushValue(rtv) - } - // carry writes to params if needed. - for i := 0; i < fr.NumArgs; i++ { - ptv := &ptvs[i] - prv := prvs[i] - if !ptv.IsUndefined() { - go2GnoValueUpdate(m.Alloc, m.Realm, 0, ptv, prv) - } - } - // cleanup - m.NumResults = fv.Value.Type().NumOut() - m.PopFrame() -}*/ - // ---------------------------------------- // misc diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index 6f06fec5478..e730d623bed 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -1398,10 +1398,6 @@ func (m *Machine) Run() { case OpConvert: m.incrCPU(OpCPUConvert) m.doOpConvert() - /* GoNative Operators */ - //case OpCallGoNative: - // m.incrCPU(OpCPUCallGoNative) - // m.doOpCallGoNative() /* Type operators */ case OpFieldType: m.incrCPU(OpCPUFieldType) diff --git a/gnovm/pkg/gnolang/op_call.go b/gnovm/pkg/gnolang/op_call.go index 557956280d6..b34fc0eeda3 100644 --- a/gnovm/pkg/gnolang/op_call.go +++ b/gnovm/pkg/gnolang/op_call.go @@ -355,20 +355,6 @@ func (m *Machine) doOpReturnCallDefers() { } } copy(b.Values, dfr.Args) - /*} else if dfr.GoFunc != nil { - fv := dfr.GoFunc - ptvs := dfr.Args - prvs := make([]reflect.Value, len(ptvs)) - for i := 0; i < len(prvs); i++ { - // TODO consider when declared types can be - // converted, e.g. fmt.Println. See GoValue. - prvs[i] = gno2GoValue(&ptvs[i], reflect.Value{}) - } - // Call and ignore results. - fv.Value.Call(prvs) - // Cleanup. - m.NumResults = 0 - */ } else { panic("should not happen") } diff --git a/gnovm/pkg/gnolang/values_conversions.go b/gnovm/pkg/gnolang/values_conversions.go index b2ca13d9907..3f9fd287509 100644 --- a/gnovm/pkg/gnolang/values_conversions.go +++ b/gnovm/pkg/gnolang/values_conversions.go @@ -31,9 +31,7 @@ func ConvertTo(alloc *Allocator, store Store, tv *TypedValue, t Type, isConst bo // special case for interface target if t.Kind() == InterfaceKind { if tv.IsUndefined() && tv.T == nil { - if _, ok := t.(*NativeType); !ok { // no support for native now - tv.T = t - } + tv.T = t } return } diff --git a/gnovm/stdlibs/testing/testing.gno b/gnovm/stdlibs/testing/testing.gno index 2644e138050..eafd0a6933f 100644 --- a/gnovm/stdlibs/testing/testing.gno +++ b/gnovm/stdlibs/testing/testing.gno @@ -216,16 +216,17 @@ type Report struct { Failed bool Skipped bool } -func (r *Report) marshal() string{ - failed:="false" + +func (r *Report) marshal() string { + failed := "false" skipped := "false" - if r.Failed{ - failed="true" + if r.Failed { + failed = "true" } - if r.Skipped{ - skipped="true" + if r.Skipped { + skipped = "true" } - return `{"Failed":`+failed+`,"Skipped":`+skipped+`}` + return `{"Failed":` + failed + `,"Skipped":` + skipped + `}` } func (t *T) report() Report { diff --git a/gnovm/tests/stdlibs/fmt/fmt.gno b/gnovm/tests/stdlibs/fmt/fmt.gno index 1bd621eba1b..3aa580fc0b1 100644 --- a/gnovm/tests/stdlibs/fmt/fmt.gno +++ b/gnovm/tests/stdlibs/fmt/fmt.gno @@ -1,17 +1,15 @@ package fmt - - -func Println(a ...interface{}) (int, error){ - return 0,nil +func Println(a ...interface{}) (int, error) { + return 0, nil } -func Printf(format string, a ...interface{}) (int, error){ - return 0,nil +func Printf(format string, a ...interface{}) (int, error) { + return 0, nil } -func Print(a ...interface{}) (int, error){ - return 0,nil +func Print(a ...interface{}) (int, error) { + return 0, nil } func Sprint(a ...interface{}) string { @@ -31,28 +29,29 @@ func Sscanf(str string, format string, a ...interface{}) (n int, err error) { } func Errorf(format string, a ...interface{}) error { - return &errorFmt{err:format} + return &errorFmt{err: format} } func Fprintln(w Writer, a ...interface{}) (n int, err error) { return 0, nil } - - + func Fprintf(w Writer, format string, a ...interface{}) (n int, err error) { return 0, nil } - + func Fprint(w Writer, a ...interface{}) (n int, err error) { return 0, nil } -type errorFmt struct{ +type errorFmt struct { err string } -func (e *errorFmt) Error()string{ + +func (e *errorFmt) Error() string { return e.err } + type Writer interface { Write(p []byte) (n int, err error) -} \ No newline at end of file +} diff --git a/gnovm/tests/stdlibs/generated.go b/gnovm/tests/stdlibs/generated.go index 708726d441a..a69805f61c0 100644 --- a/gnovm/tests/stdlibs/generated.go +++ b/gnovm/tests/stdlibs/generated.go @@ -7,7 +7,6 @@ import ( "reflect" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" - testlibs_internal_os_test "github.com/gnolang/gno/gnovm/tests/stdlibs/internal/os_test" testlibs_os "github.com/gnolang/gno/gnovm/tests/stdlibs/os" testlibs_std "github.com/gnolang/gno/gnovm/tests/stdlibs/std" testlibs_testing "github.com/gnolang/gno/gnovm/tests/stdlibs/testing" @@ -33,28 +32,6 @@ func (n *NativeFunc) HasMachineParam() bool { } var nativeFuncs = [...]NativeFunc{ - { - "internal/os_test", - "sleep", - []gno.FieldTypeExpr{ - {Name: gno.N("p0"), Type: gno.X("int64")}, - }, - []gno.FieldTypeExpr{}, - true, - func(m *gno.Machine) { - b := m.LastBlock() - var ( - p0 int64 - rp0 = reflect.ValueOf(&p0).Elem() - ) - - gno.Gno2GoValue(b.GetPointerTo(nil, gno.NewValuePathBlock(1, 0, "")).TV, rp0) - - testlibs_internal_os_test.X_sleep( - m, - p0) - }, - }, { "os", "writeStderr", @@ -127,6 +104,28 @@ var nativeFuncs = [...]NativeFunc{ )) }, }, + { + "os", + "sleep", + []gno.FieldTypeExpr{ + {Name: gno.N("p0"), Type: gno.X("int64")}, + }, + []gno.FieldTypeExpr{}, + true, + func(m *gno.Machine) { + b := m.LastBlock() + var ( + p0 int64 + rp0 = reflect.ValueOf(&p0).Elem() + ) + + gno.Gno2GoValue(b.GetPointerTo(nil, gno.NewValuePathBlock(1, 0, "")).TV, rp0) + + testlibs_os.X_sleep( + m, + p0) + }, + }, { "std", "AssertOriginCall", @@ -562,9 +561,7 @@ var nativeFuncs = [...]NativeFunc{ } var initOrder = [...]string{ - "encoding/json", "fmt", - "internal/os_test", "os", "std", "testing", diff --git a/gnovm/tests/stdlibs/internal/os_test/sleep.gno b/gnovm/tests/stdlibs/internal/os_test/sleep.gno deleted file mode 100644 index 67f53756d79..00000000000 --- a/gnovm/tests/stdlibs/internal/os_test/sleep.gno +++ /dev/null @@ -1,7 +0,0 @@ -package os_test - -func Sleep(duration int64) { - return unmarshal(data, dest) -} - -func sleep(duration int64) \ No newline at end of file diff --git a/gnovm/tests/stdlibs/internal/os_test/sleep.go b/gnovm/tests/stdlibs/internal/os_test/sleep.go deleted file mode 100644 index ef782014fad..00000000000 --- a/gnovm/tests/stdlibs/internal/os_test/sleep.go +++ /dev/null @@ -1,24 +0,0 @@ -package os_test - -import ( - "time" - - gno "github.com/gnolang/gno/gnovm/pkg/gnolang" - teststd "github.com/gnolang/gno/gnovm/tests/stdlibs/std" -) - -func X_sleep(m *gno.Machine, duration int64) { - 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 -} diff --git a/gnovm/tests/stdlibs/os/os.gno b/gnovm/tests/stdlibs/os/os.gno index 7ae4054d728..1dbacfd0e75 100644 --- a/gnovm/tests/stdlibs/os/os.gno +++ b/gnovm/tests/stdlibs/os/os.gno @@ -3,17 +3,23 @@ package os type stdoutWriter struct{} type stderrWriter struct{} -var Stdout = &stdoutWriter{} -var Stderr = &stderrWriter{} +var Stdout = &stdoutWriter{} +var Stderr = &stderrWriter{} -func (w* stdoutWriter)Write(p []byte) (n int, err error){ +func (w *stdoutWriter) Write(p []byte) (n int, err error) { return writeStdout(p) } -func (w* stderrWriter)Write(p []byte) (n int, err error){ +func (w *stderrWriter) Write(p []byte) (n int, err error) { return writeStderr(p) } +func Sleep(duration int64) { + return unmarshal(data, dest) +} + + // native bindings func writeStderr(p []byte) (n int, err error) -func writeStdout(p []byte) (n int, err error) \ No newline at end of file +func writeStdout(p []byte) (n int, err error) +func sleep(duration int64) diff --git a/gnovm/tests/stdlibs/os/os.go b/gnovm/tests/stdlibs/os/os.go index 5e420b8ef13..d407b17abde 100644 --- a/gnovm/tests/stdlibs/os/os.go +++ b/gnovm/tests/stdlibs/os/os.go @@ -3,8 +3,10 @@ package os import ( "fmt" "os" + "time" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" + teststd "github.com/gnolang/gno/gnovm/tests/stdlibs/std" ) func X_writeStdout(m *gno.Machine, p []byte) (int, error) { @@ -14,3 +16,19 @@ func X_writeStdout(m *gno.Machine, p []byte) (int, error) { func X_writeStderr(m *gno.Machine, p []byte) (int, error) { return fmt.Fprint(os.Stderr, p) } + +func X_sleep(m *gno.Machine, duration int64) { + 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 +}