Skip to content

Commit

Permalink
gogensig:patch avoid keyword & reassign test
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Nov 6, 2024
1 parent 54ab0e5 commit 6869500
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
25 changes: 25 additions & 0 deletions chore/gogensig/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,31 @@ func Resetthread(L *State) c.Int
`, nil)
}

func TestAvoidKeyword(t *testing.T) {
cmptest.RunTest(t, "avoid", false, []config.SymbolEntry{
{MangleName: "lua_sethook", CppName: "lua_sethook", GoName: "Sethook"},
}, &cppgtypes.Config{}, `
typedef struct lua_State lua_State;
typedef void (*lua_Hook)(lua_State *L, lua_Debug *ar);
void(lua_sethook)(lua_State *L, lua_Hook func, int mask, int count);
`, `
package avoid
import (
"github.com/goplus/llgo/c"
_ "unsafe"
)
type Lua_State struct {
Unused [8]uint8
}
// llgo:type C
type Lua_Hook func(*Lua_State, *c.Int)
//go:linkname Sethook C.lua_sethook
func Sethook(L *Lua_State, func_ Lua_Hook, mask c.Int, count c.Int)
`, nil)
}

// todo(zzy): https://github.com/luoliwoshang/llgo/issues/78 error in linux
// Test if it can properly skip types from packages that have already been confirmed to be mapped
// The _int8_t, _int16_t, _int32_t, _int64_t below are types that have already been confirmed to be mapped (macos).
Expand Down
40 changes: 40 additions & 0 deletions chore/gogensig/convert/type_builtin_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package convert

import (
"go/token"
"go/types"
"testing"

"github.com/goplus/gogen"
"github.com/goplus/llgo/chore/gogensig/config"
"github.com/goplus/llgo/chore/llcppg/ast"
cppgtypes "github.com/goplus/llgo/chore/llcppg/types"
)

func TestIdentRef(t *testing.T) {
Expand All @@ -21,3 +26,38 @@ func TestLookupSymbolError(t *testing.T) {
t.Fatal("Expect Error")
}
}

func TestSubstObj(t *testing.T) {
pkg := NewPackage(&PackageConfig{
PkgPath: ".",
Name: "testpkg",
GenConf: &gogen.Config{},
OutputDir: "",
SymbolTable: &config.SymbolTable{},
CppgConf: &cppgtypes.Config{},
})
if pkg == nil {
t.Fatal("NewPackage failed")
}

corg := types.NewNamed(types.NewTypeName(token.NoPos, nil, "origin", nil), types.Typ[types.Int], nil)
corg2 := types.NewNamed(types.NewTypeName(token.NoPos, nil, "origin2", nil), types.Typ[types.Int], nil)
substObj(pkg.p.Types, pkg.p.Types.Scope(), "GoPub", corg.Obj())
name := gogen.Lookup(pkg.p.Types.Scope(), "GoPub")
if name == nil {
t.Fatal("Lookup failed")
}
if name.Type().String() != corg.String() {
t.Fatal("Type not equal")
}

// reassign
substObj(pkg.p.Types, pkg.p.Types.Scope(), "GoPub", corg2.Obj())
name2 := gogen.Lookup(pkg.p.Types.Scope(), "GoPub")
if name2 == nil {
t.Fatal("Lookup failed")
}
if name2.Type().String() != corg2.String() {
t.Fatal("Type not equal")
}
}

0 comments on commit 6869500

Please sign in to comment.