Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gnolang/gno into feat/configure-a…
Browse files Browse the repository at this point in the history
…ddress-deployer-genesis
  • Loading branch information
Villaquiranm committed Nov 24, 2024
2 parents a1c0ff7 + c6f8dd4 commit 41f8bab
Show file tree
Hide file tree
Showing 20 changed files with 822 additions and 36 deletions.
7 changes: 7 additions & 0 deletions gnovm/pkg/gnolang/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,20 @@ func (alloc *Allocator) NewString(s string) StringValue {
}

func (alloc *Allocator) NewListArray(n int) *ArrayValue {
if n < 0 {
panic(&Exception{Value: typedString("len out of range")})
}
alloc.AllocateListArray(int64(n))
return &ArrayValue{
List: make([]TypedValue, n),
}
}

func (alloc *Allocator) NewDataArray(n int) *ArrayValue {
if n < 0 {
panic(&Exception{Value: typedString("len out of range")})
}

alloc.AllocateDataArray(int64(n))
return &ArrayValue{
Data: make([]byte, n),
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestDebug(t *testing.T) {
{in: "up xxx", out: `"xxx": invalid syntax`},
{in: "b 37\nc\np b\n", out: "(3 int)"},
{in: "b 27\nc\np b\n", out: `("!zero" string)`},
{in: "b 22\nc\np t.A[3]\n", out: "Command failed: slice index out of bounds: 3 (len=3)"},
{in: "b 22\nc\np t.A[3]\n", out: "Command failed: &{(\"slice index out of bounds: 3 (len=3)\" string) <nil> }"},
{in: "b 43\nc\nc\nc\np i\ndetach\n", out: "(1 int)"},
})

Expand Down
130 changes: 130 additions & 0 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,136 @@ func TestBuiltinIdentifiersShadowing(t *testing.T) {
}
}

func TestConvertTo(t *testing.T) {
t.Parallel()

testFunc := func(source, msg string) {
defer func() {
if len(msg) == 0 {
return
}

r := recover()

if r == nil {
t.Fail()
}

err := r.(*PreprocessError)
c := strings.Contains(err.Error(), msg)
if !c {
t.Fatalf(`expected "%s", got "%s"`, msg, r)
}
}()

m := NewMachine("test", nil)

n := MustParseFile("main.go", source)
m.RunFiles(n)
m.RunMain()
}

type cases struct {
source string
msg string
}

tests := []cases{
{
`package test
func main() {
const a int = -1
println(uint(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to UintKind`,
},
{
`package test
func main() {
const a int = -1
println(uint8(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint8Kind`,
},
{
`package test
func main() {
const a int = -1
println(uint16(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint16Kind`,
},
{
`package test
func main() {
const a int = -1
println(uint32(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint32Kind`,
},
{
`package test
func main() {
const a int = -1
println(uint64(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint64Kind`,
},
{
`package test
func main() {
const a float32 = 1.5
println(int32(a))
}`,
`test/main.go:5:13: cannot convert constant of type Float32Kind to Int32Kind`,
},
{
`package test
func main() {
println(int32(1.5))
}`,
`test/main.go:4:13: cannot convert (const (1.5 <untyped> bigdec)) to integer type`,
},
{
`package test
func main() {
const a float64 = 1.5
println(int64(a))
}`,
`test/main.go:5:13: cannot convert constant of type Float64Kind to Int64Kind`,
},
{
`package test
func main() {
println(int64(1.5))
}`,
`test/main.go:4:13: cannot convert (const (1.5 <untyped> bigdec)) to integer type`,
},
{
`package test
func main() {
const f = float64(1.0)
println(int64(f))
}`,
``,
},
}

for _, tc := range tests {
testFunc(tc.source, tc.msg)
}
}

// run empty main().
func TestRunEmptyMain(t *testing.T) {
t.Parallel()
Expand Down
18 changes: 17 additions & 1 deletion gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,9 @@ func (m *Machine) RunFunc(fn Name) {

func (m *Machine) RunMain() {
defer func() {
if r := recover(); r != nil {
r := recover()

if r != nil {
switch r := r.(type) {
case UnhandledPanicError:
fmt.Printf("Machine.RunMain() panic: %s\nStacktrace: %s\n",
Expand Down Expand Up @@ -1280,6 +1282,20 @@ const (
// main run loop.

func (m *Machine) Run() {
defer func() {
r := recover()

if r != nil {
switch r := r.(type) {
case *Exception:
m.Panic(r.Value)
m.Run()
default:
panic(r)
}
}
}()

for {
if m.Debugger.enabled {
m.Debug()
Expand Down
12 changes: 10 additions & 2 deletions gnovm/pkg/gnolang/op_assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func (m *Machine) doOpQuoAssign() {
}
}
// lv /= rv
quoAssign(lv.TV, rv)
err := quoAssign(lv.TV, rv)
if err != nil {
panic(err)
}

if lv.Base != nil {
m.Realm.DidUpdate(lv.Base.(Object), nil, nil)
}
Expand All @@ -154,7 +158,11 @@ func (m *Machine) doOpRemAssign() {
}
}
// lv %= rv
remAssign(lv.TV, rv)
err := remAssign(lv.TV, rv)
if err != nil {
panic(err)
}

if lv.Base != nil {
m.Realm.DidUpdate(lv.Base.(Object), nil, nil)
}
Expand Down
Loading

0 comments on commit 41f8bab

Please sign in to comment.