Skip to content

Commit

Permalink
lookupFunc: rm (T).name support
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Mar 15, 2024
1 parent b070611 commit bf64ace
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 27 deletions.
8 changes: 0 additions & 8 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,6 @@ func TestCheckTypeMethod(t *testing.T) {

func TestLookupFunc(t *testing.T) {
scope := types.NewScope(nil, 0, 0, "")
func() {
defer func() {
if e := recover(); e != "lookupFunc: (T not a valid method, use `(T).method` please\n" {
t.Fatal("TestLookupFunc:", e)
}
}()
lookupFunc(scope, "(T", "")
}()
func() {
defer func() {
if e := recover(); e != "lookupFunc: notFound not found\n" {
Expand Down
25 changes: 8 additions & 17 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func InitThisGopPkg(pkg *types.Package) {
for i, name := range names {
if name == "" {
if m.typ != nil {
name = "(" + tname + ")."
name = "."
}
name += m.name + "__" + indexTable[i:i+1]
}
Expand Down Expand Up @@ -169,24 +169,13 @@ func InitThisGopPkg(pkg *types.Package) {

// name
// .name
// (T).name
func lookupFunc(scope *types.Scope, name, tname string) types.Object {
first := name[0]
if first == '.' || first == '(' {
if first == '.' {
name = name[1:]
} else {
next := name[1:]
pos := strings.Index(next, ").")
if pos <= 0 {
log.Panicf("lookupFunc: %v not a valid method, use `(T).method` please\n", name)
}
tname, name = next[:pos], next[pos+2:]
}
if name[0] == '.' {
name = name[1:]
tobj := scope.Lookup(tname)
if tobj != nil {
if tn, ok := tobj.(*types.TypeName); ok {
if o, ok := tn.Type().(*types.Named); ok { // TODO: interface support
if o, ok := tn.Type().(*types.Named); ok { // TODO(xsw): interface support
for i, n := 0, o.NumMethods(); i < n; i++ {
method := o.Method(i)
if method.Name() == name {
Expand All @@ -208,8 +197,10 @@ type omthd struct {
name string
}

// TypeName_Method
// _TypeName__Method
// Func (no _ func name)
// _Func (with _ func name)
// TypeName_Method (no _ method name)
// _TypeName__Method (with _ method name)
func checkTypeMethod(scope *types.Scope, name string) (omthd, string) {
if pos := strings.IndexByte(name, '_'); pos >= 0 {
nsep := 1
Expand Down
4 changes: 2 additions & 2 deletions internal/overload/overload.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (p *Game) RunString(a string) {}
func RunGame(*Game, string) {}

const Gopo__Game__Run = ".RunInt,.RunString"
const Gopo_Game_Run2 = ",(Game).RunString"
const Gopo_Game_Run3 = "(Game).RunInt,RunGame"
const Gopo_Game_Run2 = ",.RunString"
const Gopo_Game_Run3 = ".RunInt,RunGame"

// -----------------------------------------------------------------------------

0 comments on commit bf64ace

Please sign in to comment.