From 1e4077beb9bb6240850147e5861be54a06b8f7fe Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Jan 2024 15:36:07 +0800 Subject: [PATCH 1/5] mv testcases --- cl/compile_gop_test.go | 582 +++++++++++++++++++++++++++++++++ cl/compile_spx_test.go | 134 ++++++++ cl/compile_test.go | 717 ----------------------------------------- 3 files changed, 716 insertions(+), 717 deletions(-) diff --git a/cl/compile_gop_test.go b/cl/compile_gop_test.go index e3ff38b4f..b09eaf658 100644 --- a/cl/compile_gop_test.go +++ b/cl/compile_gop_test.go @@ -255,6 +255,588 @@ func main() { `) } +func TestOverload(t *testing.T) { + gopClTest(t, ` +import "github.com/goplus/gop/cl/internal/overload/foo" + +type Mesh struct { +} + +func (p *Mesh) Name() string { + return "hello" +} + +var ( + m1 = &Mesh{} + m2 = &Mesh{} +) + +foo.onKey "hello", => { +} +foo.onKey "hello", key => { +} +foo.onKey ["1"], => { +} +foo.onKey ["2"], key => { +} +foo.onKey [m1, m2], => { +} +foo.onKey [m1, m2], key => { +} +foo.onKey ["a"], ["b"], key => { +} +foo.onKey ["a"], [m1, m2], key => { +} +foo.onKey ["a"], nil, key => { +} +foo.onKey 100, 200 + +n := &foo.N{} +n.onKey "hello", => { +} +n.onKey "hello", key => { +} +n.onKey ["1"], => { +} +n.onKey ["2"], key => { +} +n.onKey [m1, m2], => { +} +n.onKey [m1, m2], key => { +} +n.onKey ["a"], ["b"], key => { +} +n.onKey ["a"], [m1, m2], key => { +} +n.onKey ["a"], nil, key => { +} +n.onKey 100, 200 +`, `package main + +import "github.com/goplus/gop/cl/internal/overload/foo" + +type Mesh struct { +} + +func (p *Mesh) Name() string { + return "hello" +} + +var m1 = &Mesh{} +var m2 = &Mesh{} + +func main() { + foo.OnKey__0("hello", func() { + }) + foo.OnKey__1("hello", func(key string) { + }) + foo.OnKey__2([]string{"1"}, func() { + }) + foo.OnKey__3([]string{"2"}, func(key string) { + }) + foo.OnKey__4([]foo.Mesher{m1, m2}, func() { + }) + foo.OnKey__5([]foo.Mesher{m1, m2}, func(key foo.Mesher) { + }) + foo.OnKey__6([]string{"a"}, []string{"b"}, func(key string) { + }) + foo.OnKey__7([]string{"a"}, []foo.Mesher{m1, m2}, func(key string) { + }) + foo.OnKey__6([]string{"a"}, nil, func(key string) { + }) + foo.OnKey__8(100, 200) + n := &foo.N{} + n.OnKey__0("hello", func() { + }) + n.OnKey__1("hello", func(key string) { + }) + n.OnKey__2([]string{"1"}, func() { + }) + n.OnKey__3([]string{"2"}, func(key string) { + }) + n.OnKey__4([]foo.Mesher{m1, m2}, func() { + }) + n.OnKey__5([]foo.Mesher{m1, m2}, func(key foo.Mesher) { + }) + n.OnKey__6([]string{"a"}, []string{"b"}, func(key string) { + }) + n.OnKey__7([]string{"a"}, []foo.Mesher{m1, m2}, func(key string) { + }) + n.OnKey__6([]string{"a"}, nil, func(key string) { + }) + n.OnKey__8(100, 200) +} +`) +} + +func TestMixedOverload(t *testing.T) { + gopMixedClTest(t, "main", ` +package main + +type Mesher interface { + Name() string +} + +type N struct { +} + +func (m *N) OnKey__0(a string, fn func()) { +} + +func (m *N) OnKey__1(a string, fn func(key string)) { +} + +func (m *N) OnKey__2(a []string, fn func()) { +} + +func (m *N) OnKey__3(a []string, fn func(key string)) { +} + +func (m *N) OnKey__4(a []Mesher, fn func()) { +} + +func (m *N) OnKey__5(a []Mesher, fn func(key Mesher)) { +} + +func (m *N) OnKey__6(a []string, b []string, fn func(key string)) { +} + +func (m *N) OnKey__7(a []string, b []Mesher, fn func(key string)) { +} + +func (m *N) OnKey__8(x int, y int) { +} + + +func OnKey__0(a string, fn func()) { +} + +func OnKey__1(a string, fn func(key string)) { +} + +func OnKey__2(a []string, fn func()) { +} + +func OnKey__3(a []string, fn func(key string)) { +} + +func OnKey__4(a []Mesher, fn func()) { +} + +func OnKey__5(a []Mesher, fn func(key Mesher)) { +} + +func OnKey__6(a []string, b []string, fn func(key string)) { +} + +func OnKey__7(a []string, b []Mesher, fn func(key string)) { +} + +func OnKey__8(x int, y int) { +} + +func OnKey__9(a, b string, fn ...func(x int) int) { +} + +func OnKey__a(a, b string, v ...int) { +} +`, ` +type Mesh struct { +} + +func (p *Mesh) Name() string { + return "hello" +} + +var ( + m1 = &Mesh{} + m2 = &Mesh{} +) + +OnKey "hello", => { +} +OnKey "hello", key => { +} +OnKey ["1"], => { +} +OnKey ["2"], key => { +} +OnKey [m1, m2], => { +} +OnKey [m1, m2], key => { +} +OnKey ["a"], ["b"], key => { +} +OnKey ["a"], [m1, m2], key => { +} +OnKey ["a"], nil, key => { +} +OnKey 100, 200 +OnKey "a", "b", x => x * x, x => { + return x * 2 +} +OnKey "a", "b", 1, 2, 3 +OnKey("a", "b", [1, 2, 3]...) + +n := &N{} +n.onKey "hello", => { +} +n.onKey "hello", key => { +} +n.onKey ["1"], => { +} +n.onKey ["2"], key => { +} +n.onKey [m1, m2], => { +} +n.onKey [m1, m2], key => { +} +n.onKey ["a"], ["b"], key => { +} +n.onKey ["a"], [m1, m2], key => { +} +n.onKey ["a"], nil, key => { +} +n.onKey 100, 200 +`, `package main + +type Mesh struct { +} + +func (p *Mesh) Name() string { + return "hello" +} + +var m1 = &Mesh{} +var m2 = &Mesh{} + +func main() { + OnKey__0("hello", func() { + }) + OnKey__1("hello", func(key string) { + }) + OnKey__2([]string{"1"}, func() { + }) + OnKey__3([]string{"2"}, func(key string) { + }) + OnKey__4([]Mesher{m1, m2}, func() { + }) + OnKey__5([]Mesher{m1, m2}, func(key Mesher) { + }) + OnKey__6([]string{"a"}, []string{"b"}, func(key string) { + }) + OnKey__7([]string{"a"}, []Mesher{m1, m2}, func(key string) { + }) + OnKey__6([]string{"a"}, nil, func(key string) { + }) + OnKey__8(100, 200) + OnKey__9("a", "b", func(x int) int { + return x * x + }, func(x int) int { + return x * 2 + }) + OnKey__a("a", "b", 1, 2, 3) + OnKey__a("a", "b", []int{1, 2, 3}...) + n := &N{} + n.OnKey__0("hello", func() { + }) + n.OnKey__1("hello", func(key string) { + }) + n.OnKey__2([]string{"1"}, func() { + }) + n.OnKey__3([]string{"2"}, func(key string) { + }) + n.OnKey__4([]Mesher{m1, m2}, func() { + }) + n.OnKey__5([]Mesher{m1, m2}, func(key Mesher) { + }) + n.OnKey__6([]string{"a"}, []string{"b"}, func(key string) { + }) + n.OnKey__7([]string{"a"}, []Mesher{m1, m2}, func(key string) { + }) + n.OnKey__6([]string{"a"}, nil, func(key string) { + }) + n.OnKey__8(100, 200) +} +`) +} + +func TestMixedOverloadOp(t *testing.T) { + gopMixedClTest(t, "main", `package main + +import "fmt" + +type foo struct { +} + +func (a *foo) Gop_Add(b *foo) *foo { + fmt.Println("a + b") + return &foo{} +} +func (a foo) Gop_Sub(b foo) foo { + fmt.Println("a - b") + return foo{} +} +func (a foo) Gop_NE(b foo) bool { + fmt.Println("a!=b") + return true +} +func (a foo) Gop_Neg() *foo { + fmt.Println("-a") + return &foo{} +} +func (a foo) Gop_Inc() { + fmt.Println("a++") +} +`, ` +var a, b foo +var c = a - b +var d = -a +var e = a!=b +`, `package main + +var a, b foo +var c = (foo).Gop_Sub(a, b) +var d = a.Gop_Neg() +var e = (foo).Gop_NE(a, b) +`) +} + +func TestMixedVector3(t *testing.T) { + gopMixedClTest(t, "main", `package main +type Vector3 struct { + x, y, z float64 +} +func (a Vector3) Gop_Add__0(n int) Vector3 { + return Vector3{} +} +func (a Vector3) Gop_Add__1(n float64) Vector3 { + return Vector3{} +} +func (a Vector3) Gop_Add__2(n Vector3) Vector3 { + return Vector3{} +} +func (a *Vector3) Gop_AddAssign(n Vector3) { +} + +func (a Vector3) Gop_Rcast__0() int { + return 0 +} +func (a Vector3) Gop_Rcast__1() float64 { + return 0 +} + +func Vector3_Cast__0(x int) Vector3 { + return Vector3{} +} +func Vector3_Cast__1(x float64) Vector3 { + return Vector3{} +} +func Vector3_Init__0(x int) Vector3 { + return Vector3{} +} +func Vector3_Init__1(x float64) Vector3 { + return Vector3{} +} +`, ` +var a Vector3 +var b int +var c float64 +_ = a+b +_ = a+100 +_ = a+c +_ = 100+a +_ = Vector3(b)+a +_ = b+int(a) +a += b +a += c +`, `package main + +var a Vector3 +var b int +var c float64 + +func main() { + _ = (Vector3).Gop_Add__0(a, b) + _ = (Vector3).Gop_Add__0(a, 100) + _ = (Vector3).Gop_Add__1(a, c) + _ = (Vector3).Gop_Add__2(Vector3_Init__0(100), a) + _ = (Vector3).Gop_Add__2(Vector3_Cast__0(b), a) + _ = b + a.Gop_Rcast__0() + a.Gop_AddAssign(Vector3_Init__0(b)) + a.Gop_AddAssign(Vector3_Init__1(c)) +} +`) +} + +func TestMixedInterfaceOverload(t *testing.T) { + gopMixedClTest(t, "main", ` +package main + +type N[T any] struct { + v T +} + +func (m *N[T]) OnKey__0(a string, fn func()) { +} + +func (m *N[T]) OnKey__1(a string, fn func(key string)) { +} + +func (m *N[T]) OnKey__2(a []string, fn func()) { +} + +func (m *N[T]) OnKey__3(a []string, fn func(key string)) { +} + +type I interface { + OnKey__0(a string, fn func()) + OnKey__1(a string, fn func(key string)) + OnKey__2(a []string, fn func()) + OnKey__3(a []string, fn func(key string)) +} +`, ` +n := &N[int]{} +n.onKey "1", => { +} +keys := ["1","2"] +n.onKey keys, key => { + println key +} +n.onKey keys, => { + println keys +} + +var i I = n +i.onKey "1", key => { + println key +} +i.onKey ["1","2"], key => { + println key +} +`, `package main + +import "fmt" + +func main() { + n := &N[int]{} + n.OnKey__0("1", func() { + }) + keys := []string{"1", "2"} + n.OnKey__3(keys, func(key string) { + fmt.Println(key) + }) + n.OnKey__2(keys, func() { + fmt.Println(keys) + }) + var i I = n + i.OnKey__1("1", func(key string) { + fmt.Println(key) + }) + i.OnKey__3([]string{"1", "2"}, func(key string) { + fmt.Println(key) + }) +} +`) +} + +func TestMixedOverloadCommand(t *testing.T) { + gopMixedClTest(t, "main", `package main + +func Test__0() { +} +func Test__1(n int) { +} +type N struct { +} +func (p *N) Test__0() { +} +func (p *N) Test__1(n int) { +}`, ` +Test +Test 100 +var n N +n.test +n.test 100 +`, `package main + +func main() { + Test__0() + Test__1(100) + var n N + n.Test__0() + n.Test__1(100) +} +`) +} + +func TestOverloadNamed(t *testing.T) { + gopClTest(t, ` +import "github.com/goplus/gop/cl/internal/overload/bar" + +var a bar.Var[int] +var b bar.Var[bar.M] +c := bar.Var(string) +d := bar.Var(bar.M) +`, `package main + +import "github.com/goplus/gop/cl/internal/overload/bar" + +var a bar.Var__0[int] +var b bar.Var__1[map[string]any] + +func main() { + c := bar.Gopx_Var_Cast__0[string]() + d := bar.Gopx_Var_Cast__1[map[string]any]() +} +`) +} + +func TestMixedOverloadNamed(t *testing.T) { + gopMixedClTest(t, "main", `package main + +const GopPackage = true + +type M = map[string]any + +type basetype interface { + string | int | bool | float64 +} + +type Var__0[T basetype] struct { + val T +} + +type Var__1[T map[string]any] struct { + val T +} + +func Gopx_Var_Cast__0[T basetype]() *Var__0[T] { + return new(Var__0[T]) +} + +func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] { + return new(Var__1[T]) +} +`, ` +var a Var[int] +var b Var[M] +c := Var(string) +d := Var(M) +`, `package main + +var a Var__0[int] +var b Var__1[map[string]interface { +}] + +func main() { + c := Gopx_Var_Cast__0[string]() + d := Gopx_Var_Cast__1[map[string]interface { + }]() +} +`) +} + func TestStringLitBasic(t *testing.T) { gopClTest(t, `echo "$$"`, `package main diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go index 03f289e47..3f2e21843 100644 --- a/cl/compile_spx_test.go +++ b/cl/compile_spx_test.go @@ -1015,3 +1015,137 @@ func Test_foo(t *testing.T) { } `, "main.gox", "foo_xtest.gox", "_test") } + +func TestClassFileGopx(t *testing.T) { + gopClTestFile(t, ` +var ( + BaseClass + Width, Height float64 + *AggClass +) + +type BaseClass struct{ + x int + y int +} +type AggClass struct{} + +func Area() float64 { + return Width * Height +} +`, `package main + +type BaseClass struct { + x int + y int +} +type AggClass struct { +} +type Rect struct { + BaseClass + Width float64 + Height float64 + *AggClass +} + +func (this *Rect) Area() float64 { + return this.Width * this.Height +} +`, "Rect.gox") + gopClTestFile(t, ` +import "bytes" +var ( + bytes.Buffer +) +func test(){} +`, `package main + +import "bytes" + +type Rect struct { + bytes.Buffer +} + +func (this *Rect) test() { +} +`, "Rect.gox") + gopClTestFile(t, ` +import "bytes" +var ( + *bytes.Buffer +) +func test(){} +`, `package main + +import "bytes" + +type Rect struct { + *bytes.Buffer +} + +func (this *Rect) test() { +} +`, "Rect.gox") + gopClTestFile(t, ` +import "bytes" +var ( + *bytes.Buffer "spec:\"buffer\"" + a int "json:\"a\"" + b int +) +func test(){} +`, `package main + +import "bytes" + +type Rect struct { + *bytes.Buffer `+"`spec:\"buffer\"`"+` + a int `+"`json:\"a\"`"+` + b int +} + +func (this *Rect) test() { +} +`, "Rect.gox") +} + +func TestClassFileMember(t *testing.T) { + gopClTestFile(t, `type Engine struct { +} + +func (e *Engine) EnterPointerLock() { +} + +func (e *Engine) SetEnable(b bool) { +} + +func Engine() *Engine { + return &Engine{} +} + +func Test() { + engine.setEnable true + engine.enterPointerLock +} +`, `package main + +type Engine struct { +} + +func (e *Engine) EnterPointerLock() { +} +func (e *Engine) SetEnable(b bool) { +} + +type Rect struct { +} + +func (this *Rect) Engine() *Engine { + return &Engine{} +} +func (this *Rect) Test() { + this.Engine().SetEnable(true) + this.Engine().EnterPointerLock() +} +`, "Rect.gox") +} diff --git a/cl/compile_test.go b/cl/compile_test.go index 8ddf572a2..9c17a0b5c 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -4113,255 +4113,6 @@ func main() { `) } -func TestClassFileGopx(t *testing.T) { - gopClTestFile(t, ` -var ( - BaseClass - Width, Height float64 - *AggClass -) - -type BaseClass struct{ - x int - y int -} -type AggClass struct{} - -func Area() float64 { - return Width * Height -} -`, `package main - -type BaseClass struct { - x int - y int -} -type AggClass struct { -} -type Rect struct { - BaseClass - Width float64 - Height float64 - *AggClass -} - -func (this *Rect) Area() float64 { - return this.Width * this.Height -} -`, "Rect.gox") - gopClTestFile(t, ` -import "bytes" -var ( - bytes.Buffer -) -func test(){} -`, `package main - -import "bytes" - -type Rect struct { - bytes.Buffer -} - -func (this *Rect) test() { -} -`, "Rect.gox") - gopClTestFile(t, ` -import "bytes" -var ( - *bytes.Buffer -) -func test(){} -`, `package main - -import "bytes" - -type Rect struct { - *bytes.Buffer -} - -func (this *Rect) test() { -} -`, "Rect.gox") - gopClTestFile(t, ` -import "bytes" -var ( - *bytes.Buffer "spec:\"buffer\"" - a int "json:\"a\"" - b int -) -func test(){} -`, `package main - -import "bytes" - -type Rect struct { - *bytes.Buffer `+"`spec:\"buffer\"`"+` - a int `+"`json:\"a\"`"+` - b int -} - -func (this *Rect) test() { -} -`, "Rect.gox") -} - -func TestOverload(t *testing.T) { - gopClTest(t, ` -import "github.com/goplus/gop/cl/internal/overload/foo" - -type Mesh struct { -} - -func (p *Mesh) Name() string { - return "hello" -} - -var ( - m1 = &Mesh{} - m2 = &Mesh{} -) - -foo.onKey "hello", => { -} -foo.onKey "hello", key => { -} -foo.onKey ["1"], => { -} -foo.onKey ["2"], key => { -} -foo.onKey [m1, m2], => { -} -foo.onKey [m1, m2], key => { -} -foo.onKey ["a"], ["b"], key => { -} -foo.onKey ["a"], [m1, m2], key => { -} -foo.onKey ["a"], nil, key => { -} -foo.onKey 100, 200 - -n := &foo.N{} -n.onKey "hello", => { -} -n.onKey "hello", key => { -} -n.onKey ["1"], => { -} -n.onKey ["2"], key => { -} -n.onKey [m1, m2], => { -} -n.onKey [m1, m2], key => { -} -n.onKey ["a"], ["b"], key => { -} -n.onKey ["a"], [m1, m2], key => { -} -n.onKey ["a"], nil, key => { -} -n.onKey 100, 200 -`, `package main - -import "github.com/goplus/gop/cl/internal/overload/foo" - -type Mesh struct { -} - -func (p *Mesh) Name() string { - return "hello" -} - -var m1 = &Mesh{} -var m2 = &Mesh{} - -func main() { - foo.OnKey__0("hello", func() { - }) - foo.OnKey__1("hello", func(key string) { - }) - foo.OnKey__2([]string{"1"}, func() { - }) - foo.OnKey__3([]string{"2"}, func(key string) { - }) - foo.OnKey__4([]foo.Mesher{m1, m2}, func() { - }) - foo.OnKey__5([]foo.Mesher{m1, m2}, func(key foo.Mesher) { - }) - foo.OnKey__6([]string{"a"}, []string{"b"}, func(key string) { - }) - foo.OnKey__7([]string{"a"}, []foo.Mesher{m1, m2}, func(key string) { - }) - foo.OnKey__6([]string{"a"}, nil, func(key string) { - }) - foo.OnKey__8(100, 200) - n := &foo.N{} - n.OnKey__0("hello", func() { - }) - n.OnKey__1("hello", func(key string) { - }) - n.OnKey__2([]string{"1"}, func() { - }) - n.OnKey__3([]string{"2"}, func(key string) { - }) - n.OnKey__4([]foo.Mesher{m1, m2}, func() { - }) - n.OnKey__5([]foo.Mesher{m1, m2}, func(key foo.Mesher) { - }) - n.OnKey__6([]string{"a"}, []string{"b"}, func(key string) { - }) - n.OnKey__7([]string{"a"}, []foo.Mesher{m1, m2}, func(key string) { - }) - n.OnKey__6([]string{"a"}, nil, func(key string) { - }) - n.OnKey__8(100, 200) -} -`) -} - -func TestClassFileMember(t *testing.T) { - gopClTestFile(t, `type Engine struct { -} - -func (e *Engine) EnterPointerLock() { -} - -func (e *Engine) SetEnable(b bool) { -} - - -func Engine() *Engine { - return &Engine{} -} - -func Test() { - engine.setEnable true - engine.enterPointerLock -} -`, `package main - -type Engine struct { -} - -func (e *Engine) EnterPointerLock() { -} -func (e *Engine) SetEnable(b bool) { -} - -type Rect struct { -} - -func (this *Rect) Engine() *Engine { - return &Engine{} -} -func (this *Rect) Test() { - this.Engine().SetEnable(true) - this.Engine().EnterPointerLock() -} -`, "Rect.gox") -} - func TestCommentLine(t *testing.T) { gopClTestEx(t, gblConfLine, "main", ` type Point struct { @@ -4468,471 +4219,3 @@ func main() { ` gopClTestEx(t, &conf, "main", src, expected) } - -func TestMixedOverload(t *testing.T) { - gopMixedClTest(t, "main", ` -package main - -type Mesher interface { - Name() string -} - -type N struct { -} - -func (m *N) OnKey__0(a string, fn func()) { -} - -func (m *N) OnKey__1(a string, fn func(key string)) { -} - -func (m *N) OnKey__2(a []string, fn func()) { -} - -func (m *N) OnKey__3(a []string, fn func(key string)) { -} - -func (m *N) OnKey__4(a []Mesher, fn func()) { -} - -func (m *N) OnKey__5(a []Mesher, fn func(key Mesher)) { -} - -func (m *N) OnKey__6(a []string, b []string, fn func(key string)) { -} - -func (m *N) OnKey__7(a []string, b []Mesher, fn func(key string)) { -} - -func (m *N) OnKey__8(x int, y int) { -} - - -func OnKey__0(a string, fn func()) { -} - -func OnKey__1(a string, fn func(key string)) { -} - -func OnKey__2(a []string, fn func()) { -} - -func OnKey__3(a []string, fn func(key string)) { -} - -func OnKey__4(a []Mesher, fn func()) { -} - -func OnKey__5(a []Mesher, fn func(key Mesher)) { -} - -func OnKey__6(a []string, b []string, fn func(key string)) { -} - -func OnKey__7(a []string, b []Mesher, fn func(key string)) { -} - -func OnKey__8(x int, y int) { -} - -func OnKey__9(a, b string, fn ...func(x int) int) { -} - -func OnKey__a(a, b string, v ...int) { -} -`, ` -type Mesh struct { -} - -func (p *Mesh) Name() string { - return "hello" -} - -var ( - m1 = &Mesh{} - m2 = &Mesh{} -) - -OnKey "hello", => { -} -OnKey "hello", key => { -} -OnKey ["1"], => { -} -OnKey ["2"], key => { -} -OnKey [m1, m2], => { -} -OnKey [m1, m2], key => { -} -OnKey ["a"], ["b"], key => { -} -OnKey ["a"], [m1, m2], key => { -} -OnKey ["a"], nil, key => { -} -OnKey 100, 200 -OnKey "a", "b", x => x * x, x => { - return x * 2 -} -OnKey "a", "b", 1, 2, 3 -OnKey("a", "b", [1, 2, 3]...) - -n := &N{} -n.onKey "hello", => { -} -n.onKey "hello", key => { -} -n.onKey ["1"], => { -} -n.onKey ["2"], key => { -} -n.onKey [m1, m2], => { -} -n.onKey [m1, m2], key => { -} -n.onKey ["a"], ["b"], key => { -} -n.onKey ["a"], [m1, m2], key => { -} -n.onKey ["a"], nil, key => { -} -n.onKey 100, 200 -`, `package main - -type Mesh struct { -} - -func (p *Mesh) Name() string { - return "hello" -} - -var m1 = &Mesh{} -var m2 = &Mesh{} - -func main() { - OnKey__0("hello", func() { - }) - OnKey__1("hello", func(key string) { - }) - OnKey__2([]string{"1"}, func() { - }) - OnKey__3([]string{"2"}, func(key string) { - }) - OnKey__4([]Mesher{m1, m2}, func() { - }) - OnKey__5([]Mesher{m1, m2}, func(key Mesher) { - }) - OnKey__6([]string{"a"}, []string{"b"}, func(key string) { - }) - OnKey__7([]string{"a"}, []Mesher{m1, m2}, func(key string) { - }) - OnKey__6([]string{"a"}, nil, func(key string) { - }) - OnKey__8(100, 200) - OnKey__9("a", "b", func(x int) int { - return x * x - }, func(x int) int { - return x * 2 - }) - OnKey__a("a", "b", 1, 2, 3) - OnKey__a("a", "b", []int{1, 2, 3}...) - n := &N{} - n.OnKey__0("hello", func() { - }) - n.OnKey__1("hello", func(key string) { - }) - n.OnKey__2([]string{"1"}, func() { - }) - n.OnKey__3([]string{"2"}, func(key string) { - }) - n.OnKey__4([]Mesher{m1, m2}, func() { - }) - n.OnKey__5([]Mesher{m1, m2}, func(key Mesher) { - }) - n.OnKey__6([]string{"a"}, []string{"b"}, func(key string) { - }) - n.OnKey__7([]string{"a"}, []Mesher{m1, m2}, func(key string) { - }) - n.OnKey__6([]string{"a"}, nil, func(key string) { - }) - n.OnKey__8(100, 200) -} -`) -} - -func TestMixedOverloadOp(t *testing.T) { - gopMixedClTest(t, "main", `package main - -import "fmt" - -type foo struct { -} - -func (a *foo) Gop_Add(b *foo) *foo { - fmt.Println("a + b") - return &foo{} -} -func (a foo) Gop_Sub(b foo) foo { - fmt.Println("a - b") - return foo{} -} -func (a foo) Gop_NE(b foo) bool { - fmt.Println("a!=b") - return true -} -func (a foo) Gop_Neg() *foo { - fmt.Println("-a") - return &foo{} -} -func (a foo) Gop_Inc() { - fmt.Println("a++") -} -`, ` -var a, b foo -var c = a - b -var d = -a -var e = a!=b -`, `package main - -var a, b foo -var c = (foo).Gop_Sub(a, b) -var d = a.Gop_Neg() -var e = (foo).Gop_NE(a, b) -`) -} - -func TestMixedVector3(t *testing.T) { - gopMixedClTest(t, "main", `package main -type Vector3 struct { - x, y, z float64 -} -func (a Vector3) Gop_Add__0(n int) Vector3 { - return Vector3{} -} -func (a Vector3) Gop_Add__1(n float64) Vector3 { - return Vector3{} -} -func (a Vector3) Gop_Add__2(n Vector3) Vector3 { - return Vector3{} -} -func (a *Vector3) Gop_AddAssign(n Vector3) { -} - -func (a Vector3) Gop_Rcast__0() int { - return 0 -} -func (a Vector3) Gop_Rcast__1() float64 { - return 0 -} - -func Vector3_Cast__0(x int) Vector3 { - return Vector3{} -} -func Vector3_Cast__1(x float64) Vector3 { - return Vector3{} -} -func Vector3_Init__0(x int) Vector3 { - return Vector3{} -} -func Vector3_Init__1(x float64) Vector3 { - return Vector3{} -} -`, ` -var a Vector3 -var b int -var c float64 -_ = a+b -_ = a+100 -_ = a+c -_ = 100+a -_ = Vector3(b)+a -_ = b+int(a) -a += b -a += c -`, `package main - -var a Vector3 -var b int -var c float64 - -func main() { - _ = (Vector3).Gop_Add__0(a, b) - _ = (Vector3).Gop_Add__0(a, 100) - _ = (Vector3).Gop_Add__1(a, c) - _ = (Vector3).Gop_Add__2(Vector3_Init__0(100), a) - _ = (Vector3).Gop_Add__2(Vector3_Cast__0(b), a) - _ = b + a.Gop_Rcast__0() - a.Gop_AddAssign(Vector3_Init__0(b)) - a.Gop_AddAssign(Vector3_Init__1(c)) -} -`) -} - -func TestMixedInterfaceOverload(t *testing.T) { - gopMixedClTest(t, "main", ` -package main - -type N[T any] struct { - v T -} - -func (m *N[T]) OnKey__0(a string, fn func()) { -} - -func (m *N[T]) OnKey__1(a string, fn func(key string)) { -} - -func (m *N[T]) OnKey__2(a []string, fn func()) { -} - -func (m *N[T]) OnKey__3(a []string, fn func(key string)) { -} - -type I interface { - OnKey__0(a string, fn func()) - OnKey__1(a string, fn func(key string)) - OnKey__2(a []string, fn func()) - OnKey__3(a []string, fn func(key string)) -} -`, ` -n := &N[int]{} -n.onKey "1", => { -} -keys := ["1","2"] -n.onKey keys, key => { - println key -} -n.onKey keys, => { - println keys -} - -var i I = n -i.onKey "1", key => { - println key -} -i.onKey ["1","2"], key => { - println key -} -`, `package main - -import "fmt" - -func main() { - n := &N[int]{} - n.OnKey__0("1", func() { - }) - keys := []string{"1", "2"} - n.OnKey__3(keys, func(key string) { - fmt.Println(key) - }) - n.OnKey__2(keys, func() { - fmt.Println(keys) - }) - var i I = n - i.OnKey__1("1", func(key string) { - fmt.Println(key) - }) - i.OnKey__3([]string{"1", "2"}, func(key string) { - fmt.Println(key) - }) -} -`) -} - -func TestMixedOverloadCommand(t *testing.T) { - gopMixedClTest(t, "main", `package main - -func Test__0() { -} -func Test__1(n int) { -} -type N struct { -} -func (p *N) Test__0() { -} -func (p *N) Test__1(n int) { -}`, ` -Test -Test 100 -var n N -n.test -n.test 100 -`, `package main - -func main() { - Test__0() - Test__1(100) - var n N - n.Test__0() - n.Test__1(100) -} -`) -} - -func TestOverloadNamed(t *testing.T) { - gopClTest(t, ` -import "github.com/goplus/gop/cl/internal/overload/bar" - -var a bar.Var[int] -var b bar.Var[bar.M] -c := bar.Var(string) -d := bar.Var(bar.M) -`, `package main - -import "github.com/goplus/gop/cl/internal/overload/bar" - -var a bar.Var__0[int] -var b bar.Var__1[map[string]any] - -func main() { - c := bar.Gopx_Var_Cast__0[string]() - d := bar.Gopx_Var_Cast__1[map[string]any]() -} -`) -} - -func TestMixedOverloadNamed(t *testing.T) { - gopMixedClTest(t, "main", `package main - -const GopPackage = true - -type M = map[string]any - -type basetype interface { - string | int | bool | float64 -} - -type Var__0[T basetype] struct { - val T -} - -type Var__1[T map[string]any] struct { - val T -} - -func Gopx_Var_Cast__0[T basetype]() *Var__0[T] { - return new(Var__0[T]) -} - -func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] { - return new(Var__1[T]) -} -`, ` -var a Var[int] -var b Var[M] -c := Var(string) -d := Var(M) -`, `package main - -var a Var__0[int] -var b Var__1[map[string]interface { -}] - -func main() { - c := Gopx_Var_Cast__0[string]() - d := Gopx_Var_Cast__1[map[string]interface { - }]() -} -`) -} From bd1a2d117f6709ef0383de289826fe46a498e460 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Jan 2024 16:04:12 +0800 Subject: [PATCH 2/5] TestGopxNoFunc --- cl/compile.go | 4 ++++ cl/compile_spx_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/cl/compile.go b/cl/compile.go index 507612745..486d8f4e5 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -832,6 +832,10 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con } parent.tylds = append(parent.tylds, ld) } + + // bugfix: see TestGopxNoFunc + parent.inits = append(parent.inits, ld.load) + ctx.classRecv = &ast.FieldList{List: []*ast.Field{{ Names: []*ast.Ident{ {Name: "this"}, diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go index 3f2e21843..d7ec4bcf4 100644 --- a/cl/compile_spx_test.go +++ b/cl/compile_spx_test.go @@ -239,6 +239,11 @@ func (this *index) onInit() { this.bar() fmt.Println("Hi") } + +type bar struct { + spx.Sprite + *index +} `) } @@ -537,6 +542,11 @@ func (this *Game) MainEntry() { func main() { new(Game).Main() } + +type Kai struct { + spx2.Sprite + *Game +} `, "Game.t2gmx", "Kai.t2spx", "") gopSpxTestExConf(t, "OnlyGmx", &conf, ` var ( @@ -1016,6 +1026,19 @@ func Test_foo(t *testing.T) { `, "main.gox", "foo_xtest.gox", "_test") } +func TestGopxNoFunc(t *testing.T) { + gopClTestFile(t, ` +var ( + a int +) +`, `package main + +type foo struct { + a int +} +`, "foo.gox") +} + func TestClassFileGopx(t *testing.T) { gopClTestFile(t, ` var ( From 0fb451b537ec07211dd89211ebe707c35358348b Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Jan 2024 16:49:31 +0800 Subject: [PATCH 3/5] compileIdent bugfix: clCommandWithoutArgs --- cl/compile.go | 7 +- cl/compile_spx_test.go | 173 ++++++++++++++++++---------------------- cl/expr.go | 2 +- cl/internal/spx/game.go | 3 + 4 files changed, 84 insertions(+), 101 deletions(-) diff --git a/cl/compile.go b/cl/compile.go index 486d8f4e5..d38cf780c 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -630,9 +630,6 @@ func isOverloadFunc(name string) bool { return n > 3 && name[n-3:n-1] == "__" } -//go:linkname initThisGopPkg github.com/goplus/gox.initThisGopPkg -func initThisGopPkg(pkg *types.Package) - func initGopPkg(ctx *pkgCtx, pkg *gox.Package, gopSyms map[string]bool) { for name, f := range ctx.syms { if gopSyms[name] { @@ -654,7 +651,7 @@ func initGopPkg(ctx *pkgCtx, pkg *gox.Package, gopSyms map[string]bool) { if pkg.Types.Scope().Lookup(gopPackage) == nil { pkg.Types.Scope().Insert(types.NewConst(token.NoPos, pkg.Types, gopPackage, types.Typ[types.UntypedBool], constant.MakeBool(true))) } - initThisGopPkg(pkg.Types) + gox.InitThisGopPkg(pkg.Types) } func getEntrypoint(f *ast.File) string { @@ -834,7 +831,7 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con } // bugfix: see TestGopxNoFunc - parent.inits = append(parent.inits, ld.load) + parent.lbinames = append(parent.lbinames, classType) ctx.classRecv = &ast.FieldList{List: []*ast.Field{{ Names: []*ast.Ident{ diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go index d7ec4bcf4..7d0dbef7d 100644 --- a/cl/compile_spx_test.go +++ b/cl/compile_spx_test.go @@ -168,6 +168,8 @@ func onInit() { for { } } + +initGameApp `, ` func onMsg(msg string) { for { @@ -181,18 +183,22 @@ import "github.com/goplus/gop/cl/internal/spx" type Game struct { *spx.MyGame } +type Kai struct { + spx.Sprite + *Game +} func (this *Game) onInit() { for { spx.SchedNow() } } - -type Kai struct { - spx.Sprite - *Game +func (this *Game) MainEntry() { + this.InitGameApp() +} +func main() { + spx.Gopt_MyGame_Main(new(Game)) } - func (this *Kai) onMsg(msg string) { for { spx.Sched() @@ -229,6 +235,10 @@ import ( const Foo = 1 +type bar struct { + spx.Sprite + *index +} type index struct { *spx.MyGame } @@ -239,11 +249,6 @@ func (this *index) onInit() { this.bar() fmt.Println("Hi") } - -type bar struct { - spx.Sprite - *index -} `) } @@ -272,6 +277,10 @@ import ( type Game struct { *spx.MyGame } +type bar struct { + spx.Sprite + *Game +} func (this *Game) onInit() { spx.Sched() @@ -279,12 +288,6 @@ func (this *Game) onInit() { spx.TestIntValue = 1 x := math.Round(1.2) } - -type bar struct { - spx.Sprite - *Game -} - func (this *bar) onInit() { this.SetCostume("kai-a") this.Play("recordingWhere") @@ -320,15 +323,15 @@ func onCloned() { import "github.com/goplus/gop/cl/internal/spx" +type Game struct { + *spx.MyGame + Kai Kai +} type Kai struct { spx.Sprite *Game a int } -type Game struct { - *spx.MyGame - Kai Kai -} func (this *Game) onInit() { spx.Gopt_Sprite_Clone__0(this.Kai) @@ -366,15 +369,14 @@ type Kai struct { spx.Sprite *index } - -var x float64 = spx.Rand__1(1.2) - type index struct { *spx.MyGame Kai Kai t spx.Sound } +var x float64 = spx.Rand__1(1.2) + func (this *index) MainEntry() { spx.Gopt_MyGame_Run(this, "hzip://open.qiniu.us/weather/res.zip") } @@ -440,6 +442,10 @@ import ( type Game struct { spx2.Game } +type Kai struct { + spx2.Sprite + *Game +} func (this *Game) MainEntry() { fmt.Println("Hi") @@ -447,12 +453,6 @@ func (this *Game) MainEntry() { func main() { new(Game).Main() } - -type Kai struct { - spx2.Sprite - *Game -} - func (this *Kai) onMsg(msg string) { } `, "Game.t2gmx", "Kai.t2spx") @@ -474,6 +474,10 @@ import ( type Game struct { spx2.Game } +type Kai struct { + spx2.Sprite2 + *Game +} func (this *Game) MainEntry() { fmt.Println("Hi, Sprite2") @@ -481,12 +485,6 @@ func (this *Game) MainEntry() { func main() { new(Game).Main() } - -type Kai struct { - spx2.Sprite2 - *Game -} - func (this *Kai) onMsg(msg string) { } `, "Game.t2gmx", "Kai.t2spx2") @@ -508,15 +506,13 @@ import ( type Dog struct { spx2.Sprite } - -func (this *Dog) Main() { - fmt.Println("Hi, Sprite") -} - type Kai struct { spx2.Sprite2 } +func (this *Dog) Main() { + fmt.Println("Hi, Sprite") +} func (this *Kai) onMsg(msg string) { } `, "Dog_t3spx.gox", "Kai.t3spx2") @@ -536,17 +532,16 @@ import "github.com/goplus/gop/cl/internal/spx2" type Game struct { spx2.Game } +type Kai struct { + spx2.Sprite + *Game +} func (this *Game) MainEntry() { } func main() { new(Game).Main() } - -type Kai struct { - spx2.Sprite - *Game -} `, "Game.t2gmx", "Kai.t2spx", "") gopSpxTestExConf(t, "OnlyGmx", &conf, ` var ( @@ -557,14 +552,14 @@ var ( import "github.com/goplus/gop/cl/internal/spx2" -type Kai struct { - spx2.Sprite - *Game -} type Game struct { spx2.Game Kai Kai } +type Kai struct { + spx2.Sprite + *Game +} func (this *Game) MainEntry() { } @@ -593,14 +588,14 @@ import ( "github.com/goplus/gop/cl/internal/spx2" ) -type Kai struct { - spx2.Sprite - *Game -} type Game struct { spx2.Game Kai Kai } +type Kai struct { + spx2.Sprite + *Game +} func (this *Game) MainEntry() { fmt.Println("Hi") @@ -635,18 +630,16 @@ import "github.com/goplus/gop/cl/internal/spx" type Game struct { *spx.MyGame } +type Kai struct { + spx.Sprite + *Game +} func (this *Game) onInit() { for { spx.SchedNow() } } - -type Kai struct { - spx.Sprite - *Game -} - func (this *Kai) onMsg(msg string) { for { spx.Sched() @@ -694,15 +687,15 @@ type info struct { x int y int } +type Game struct { + *spx.MyGame + Kai Kai +} type Kai struct { spx.Sprite *Game a int } -type Game struct { - *spx.MyGame - Kai Kai -} func (this *Game) onInit() { spx.Gopt_Sprite_Clone__0(this.Kai) @@ -741,6 +734,10 @@ import "github.com/goplus/gop/cl/internal/spx" type Game struct { *spx.MyGame } +type Kai struct { + spx.Sprite + *Game +} func (this *Game) MainEntry() { this.SendMessage("Hi") @@ -748,12 +745,6 @@ func (this *Game) MainEntry() { func main() { spx.Gopt_MyGame_Main(new(Game)) } - -type Kai struct { - spx.Sprite - *Game -} - func (this *Kai) onMsg(msg string) { } `, "Game.tgmx", "Kai.tspx") @@ -776,6 +767,10 @@ import ( type Game struct { *spx.MyGame } +type Kai struct { + spx.Sprite + *Game +} func (this *Game) MainEntry() { fmt.Println("Hi") @@ -783,12 +778,6 @@ func (this *Game) MainEntry() { func main() { spx.Gopt_MyGame_Main(new(Game)) } - -type Kai struct { - spx.Sprite - *Game -} - func (this *Kai) onMsg(msg string) { this.Position().Add__0(100, 200) } @@ -825,6 +814,10 @@ import ( type Game struct { *spx.MyGame } +type Kai struct { + spx.Sprite + *Game +} func (this *Game) MainEntry() { fmt.Println("hi") @@ -832,12 +825,6 @@ func (this *Game) MainEntry() { func main() { spx.Gopt_MyGame_Main(new(Game)) } - -type Kai struct { - spx.Sprite - *Game -} - func (this *Kai) onMsg(msg string) { fmt.Println(msg) this.Position().Add__0(100, 200) @@ -910,15 +897,15 @@ import "github.com/goplus/gop/cl/internal/spx" type Mesh struct { } +type Game struct { + *spx.MyGame + Kai Kai +} type Kai struct { spx.Sprite *Game a int } -type Game struct { - *spx.MyGame - Kai Kai -} func (this *Game) onInit() { spx.Gopt_Sprite_OnKey__1(this.Kai, "hello", func(key string) { @@ -973,6 +960,10 @@ import ( "testing" ) +type caseFoo struct { + test.Case + *App +} type App struct { test.App } @@ -980,12 +971,6 @@ type App struct { func (this *App) MainEntry() { fmt.Println("Hi") } - -type caseFoo struct { - test.Case - *App -} - func (this *caseFoo) Main() { this.T().Log("Hi") this.T().Run("a test", func(t *testing.T) { @@ -1154,15 +1139,13 @@ func Test() { type Engine struct { } +type Rect struct { +} func (e *Engine) EnterPointerLock() { } func (e *Engine) SetEnable(b bool) { } - -type Rect struct { -} - func (this *Rect) Engine() *Engine { return &Engine{} } diff --git a/cl/expr.go b/cl/expr.go index fb6cc143b..c712e9d90 100644 --- a/cl/expr.go +++ b/cl/expr.go @@ -99,7 +99,7 @@ func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (pkg gox.PkgRef, k sig := fn.Ancestor().Type().(*types.Signature) if recv := sig.Recv(); recv != nil { ctx.cb.Val(recv) - chkFlag := flags &^ clCommandWithoutArgs + chkFlag := flags // &^ clCommandWithoutArgs (TODO: why?) if chkFlag&clIdentSelectorExpr != 0 { chkFlag = clIdentCanAutoCall } diff --git a/cl/internal/spx/game.go b/cl/internal/spx/game.go index 9367eae70..e7b1f1c1e 100644 --- a/cl/internal/spx/game.go +++ b/cl/internal/spx/game.go @@ -29,6 +29,9 @@ type MyGame struct { func Gopt_MyGame_Main(game interface{}) { } +func (p *MyGame) InitGameApp(args ...string) { +} + func (p *MyGame) Broadcast__0(msg string) { } From f3679f784eeb45a2343149bfcfa35313e2aefd56 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Jan 2024 16:51:43 +0800 Subject: [PATCH 4/5] update gox --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index bb02a687b..cc237ce2d 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/fsnotify/fsnotify v1.7.0 github.com/goplus/c2go v0.7.20 - github.com/goplus/gox v1.14.1 + github.com/goplus/gox v1.14.1-0.20240130050415-3bd8cee57731 github.com/goplus/mod v0.12.3 github.com/qiniu/x v1.13.2 golang.org/x/tools v0.17.0 diff --git a/go.sum b/go.sum index f4de21021..5b2b55729 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/goplus/c2go v0.7.20 h1:90HiO7tuX8hHfEnl7HZRmLsAPH9q2wsYIvHiJMpwtU8= github.com/goplus/c2go v0.7.20/go.mod h1:hToieb5V4BMY3c431DQ7DEg13JQ+q9qGmls4+GgymrA= github.com/goplus/gox v1.14.0/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0= -github.com/goplus/gox v1.14.1 h1:ppqY4xzwc6UaeProYf4pn5fMa4xSDe80PsSsBfCYZ3U= -github.com/goplus/gox v1.14.1/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0= +github.com/goplus/gox v1.14.1-0.20240130050415-3bd8cee57731 h1:ZjWvtPmuf15J8dmcl1ILOmxi400Bf1y6mAMsVPUbg7I= +github.com/goplus/gox v1.14.1-0.20240130050415-3bd8cee57731/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0= github.com/goplus/mod v0.12.2/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs= github.com/goplus/mod v0.12.3 h1:qLU5/F27CzUTQhCQRN1WruiCepUn5GdLKQTB41OsYfk= github.com/goplus/mod v0.12.3/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs= From 22904bae6108d9ec28ee9674be57864bbe96b527 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Jan 2024 16:55:04 +0800 Subject: [PATCH 5/5] TestSpx --- x/build/build_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/x/build/build_test.go b/x/build/build_test.go index b43a127ff..8e2f621f0 100644 --- a/x/build/build_test.go +++ b/x/build/build_test.go @@ -391,14 +391,13 @@ import ( "github.com/goplus/gop/cl/internal/spx" ) -func main() { - spx.Gopt_MyGame_Main(new(spx.MyGame)) -} - type Cat struct { spx.Sprite } +func main() { + spx.Gopt_MyGame_Main(new(spx.MyGame)) +} func (this *Cat) Main() { fmt.Println("hi") }