Skip to content

Commit

Permalink
Merge pull request #254 from MichaelS11/struct
Browse files Browse the repository at this point in the history
Added sync and fixed no member for struct
  • Loading branch information
mattn authored Jun 16, 2018
2 parents 9d63b75 + c4a1ee1 commit 38225cb
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 11 deletions.
5 changes: 2 additions & 3 deletions packages/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
func TestBytes(t *testing.T) {
os.Setenv("ANKO_DEBUG", "1")
tests := []testlib.Test{
// TOFIX: no member named 'WriteString' for struct
// {Script: `bytes = import("bytes"); a = make(bytes.Buffer); n, err = a.WriteString("b")`, RunOutput: []interface{}{1, nil}},
// {Script: `bytes = import("bytes"); a = make(bytes.Buffer); n, err = a.WriteString("b"); a.String()`, RunOutput: "b"},
{Script: `bytes = import("bytes"); a = make(bytes.Buffer); n, err = a.WriteString("a"); if err != nil { return err }; n`, RunOutput: 1},
{Script: `bytes = import("bytes"); a = make(bytes.Buffer); n, err = a.WriteString("a"); if err != nil { return err }; a.String()`, RunOutput: "a"},
}
testlib.Run(t, tests, &testlib.Options{EnvSetupFunc: &testPackagesEnvSetupFunc})
}
20 changes: 20 additions & 0 deletions packages/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package packages

import (
"sync"
)

func init() {
Packages["sync"] = map[string]interface{}{
"NewCond": sync.NewCond,
}
PackageTypes["sync"] = map[string]interface{}{
"Cond": sync.Cond{},
"Mutex": sync.Mutex{},
"Once": sync.Once{},
"Pool": sync.Pool{},
"RWMutex": sync.RWMutex{},
"WaitGroup": sync.WaitGroup{},
}
syncGo19()
}
11 changes: 11 additions & 0 deletions packages/syncGo19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build go1.9

package packages

import (
"sync"
)

func syncGo19() {
PackageTypes["sync"]["Map"] = sync.Map{}
}
5 changes: 5 additions & 0 deletions packages/syncNotGo19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !go1.9

package packages

func syncGo19() {}
17 changes: 17 additions & 0 deletions packages/sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package packages

import (
"os"
"testing"

"github.com/mattn/anko/internal/testlib"
)

func TestSync(t *testing.T) {
os.Setenv("ANKO_DEBUG", "1")
tests := []testlib.Test{
{Script: `sync = import("sync"); once = make(sync.Once); a = []; func add() { a += "a" }; once.Do(add); once.Do(add); a`, RunOutput: []interface{}{"a"}, Output: map[string]interface{}{"a": []interface{}{"a"}}},
{Script: `sync = import("sync"); waitGroup = make(sync.WaitGroup); waitGroup.Add(2); func done() { waitGroup.Done() }; go done(); go done(); waitGroup.Wait(); "a"`, RunOutput: "a"},
}
testlib.Run(t, tests, &testlib.Options{EnvSetupFunc: &testPackagesEnvSetupFunc})
}
2 changes: 1 addition & 1 deletion packages/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestTime(t *testing.T) {
os.Setenv("ANKO_DEBUG", "1")
tests := []testlib.Test{
{Script: `time = import("time"); a = make(time.Time); a.IsZero()`, EnvSetupFunc: &testPackagesEnvSetupFunc, RunOutput: true},
{Script: `time = import("time"); a = make(time.Time); a.IsZero()`, RunOutput: true},
}
testlib.Run(t, tests, &testlib.Options{EnvSetupFunc: &testPackagesEnvSetupFunc})
}
13 changes: 10 additions & 3 deletions vm/vmExpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,17 @@ func invokeExpr(expr ast.Expr, env *Env) (reflect.Value, error) {
switch v.Kind() {
case reflect.Struct:
field, found := v.Type().FieldByName(e.Name)
if !found {
return nilValue, newStringError(e, "no member named '"+e.Name+"' for struct")
if found {
return v.FieldByIndex(field.Index), nil
}
if v.CanAddr() {
v = v.Addr()
method, found := v.Type().MethodByName(e.Name)
if found {
return v.Method(method.Index), nil
}
}
return v.FieldByIndex(field.Index), nil
return nilValue, newStringError(e, "no member named '"+e.Name+"' for struct")
case reflect.Map:
v = getMapIndex(reflect.ValueOf(e.Name), v)
// Note if the map is of reflect.Value, it will incorrectly return nil when zero value
Expand Down
7 changes: 3 additions & 4 deletions vm/vmFunctions_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package vm

import (
// "bytes"
"bytes"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -364,9 +364,8 @@ func TestFunctions(t *testing.T) {

{Script: `a = make(Time); a.IsZero()`, Types: map[string]interface{}{"Time": time.Time{}}, RunOutput: true},

// TOFIX:
// {Script: `a = make(Buffer); n, err = a.WriteString("b")`, Types: map[string]interface{}{"Buffer": bytes.Buffer{}}, RunOutput: []interface{}{1, nil}},
// {Script: `a = make(Buffer); n, err = a.WriteString("b"); a.String()`, Types: map[string]interface{}{"Buffer": bytes.Buffer{}}, RunOutput: "b"},
{Script: `a = make(Buffer); n, err = a.WriteString("a"); if err != nil { return err }; n`, Types: map[string]interface{}{"Buffer": bytes.Buffer{}}, RunOutput: 1},
{Script: `a = make(Buffer); n, err = a.WriteString("a"); if err != nil { return err }; a.String()`, Types: map[string]interface{}{"Buffer": bytes.Buffer{}}, RunOutput: "a"},
}
testlib.Run(t, tests, nil)
}
Expand Down

0 comments on commit 38225cb

Please sign in to comment.