张金坤 9 éve
szülő
commit
5439ad3270

+ 9 - 1
common/src/github.com/yuin/gopher-lua/.github/ISSUE_TEMPLATE.md

@@ -1,4 +1,12 @@
 - [ ] GopherLua is a Lua5.1 implementation. You should be familiar with Lua programming language. Have you read [Lua 5.1 reference manual](http://www.lua.org/manual/5.1/) carefully?
-- [ ] GopherLua is a Lua5.1 implementation.  In Lua, to keep it simple, **it is more important to remove functionalities rather than to add functionalities unlike other languages** . If you are going to introduce some new cool functionalities into the GopherLua code base and the funcionalities can be implemented by existing APIs, It should be implemented as a library. 
+- [ ] GopherLua is a Lua5.1 implementation.  In Lua, to keep it simple, **it is more important to remove functionalities rather than to add functionalities unlike other languages** . If you are going to introduce some new cool functionalities into the GopherLua code base and the functionalities can be implemented by existing APIs, It should be implemented as a library. 
 
+Please answer the following before submitting your issue:
+
+1. What version of GopherLua are you using? : 
+2. What version of Go are you using? : 
+3. What operating system and processor architecture are you using? :
+4. What did you do? :
+5. What did you expect to see? :
+6. What did you see instead? :
 

+ 3 - 0
common/src/github.com/yuin/gopher-lua/_glua-tests/db.lua

@@ -81,3 +81,6 @@ local ok, msg = pcall(function()
   debug.setlocal(10, 1, 1)
 end)
 assert(not ok and string.find(msg, "level out of range"))
+
+assert(debug.getinfo(100) == nil)
+assert(debug.getinfo(1, "a") == nil)

+ 12 - 0
common/src/github.com/yuin/gopher-lua/_glua-tests/math.lua

@@ -0,0 +1,12 @@
+assert(math.fmod(13.5, 2) == 1.5)
+assert(math.pow(7, 2) == 49)
+
+local ok, msg = pcall(function()
+  math.max()
+end)
+assert(not ok and string.find(msg, "wrong number of arguments"))
+
+local ok, msg = pcall(function()
+  math.min()
+end)
+assert(not ok and string.find(msg, "wrong number of arguments"))

+ 32 - 0
common/src/github.com/yuin/gopher-lua/_glua-tests/strings.lua

@@ -0,0 +1,32 @@
+
+local ok, msg = pcall(function()
+  string.dump()
+end)
+assert(not ok and string.find(msg, "GopherLua does not support the string.dump"))
+assert(string.find("","aaa") == nil)
+assert(string.gsub("hello world", "(%w+)", "%1 %1 %c") == "hello hello %c world world %c")
+local ok, msg = pcall(function()
+  string.gsub("aaa", "]", "")
+end)
+assert(not ok and string.find(msg, "invalid '%]'"))
+
+local ok, msg = pcall(function()
+  string.match("aaa", "]", -10)
+end)
+assert(not ok and string.find(msg, "invalid '%]'"))
+
+local ok, msg = pcall(function()
+  string.find("aaa", "]")
+end)
+assert(not ok and string.find(msg, "invalid '%]'"))
+
+local ok, msg = pcall(function()
+  string.gmatch("aaa", "]")
+end)
+assert(not ok and string.find(msg, "invalid '%]'"))
+
+local ret1, ret2, ret3, ret4 = string.find("aaa bbb", "(%w+())")
+assert(ret1 == 1)
+assert(ret2 == 3)
+assert(ret3 == "aaa")
+assert(ret4 == 4)

+ 25 - 0
common/src/github.com/yuin/gopher-lua/_glua-tests/table.lua

@@ -10,3 +10,28 @@ local ok, msg = pcall(function()
   table.insert(a)
 end)
 assert(not ok and string.find(msg, "wrong number of arguments"))
+
+a = {}
+a["key0"] = "0"
+a["key1"] = "1"
+a[1] = 1
+a[2] = 2
+a[true] = "true"
+a[false] = "false"
+for k, v in pairs(a) do
+  if k == "key0" then
+    assert(v == "0")
+  elseif k == "key1" then
+    assert(v == "1")
+  elseif k == 1 then
+    assert(v == 1)
+  elseif k == 2 then
+    assert(v == 2)
+  elseif k == true then
+    assert(v == "true")
+  elseif k == false then
+    assert(v == "false")
+  else
+    error("unexpected key:" .. tostring(k))
+  end
+end

+ 54 - 0
common/src/github.com/yuin/gopher-lua/_glua-tests/vm.lua

@@ -80,3 +80,57 @@ local bigtable = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                   1,1,1,1,1,1,1,1,1,1,1,1,10}
 assert(bigtable[601] == 10)
+
+local ok, msg = loadstring([[
+  function main(
+     a1,  a2,  a3,  a4,  a5,  a6,  a7,  a8,
+     a9,  a10,  a11,  a12,  a13,  a14,  a15,  a16,
+     a17,  a18,  a19,  a20,  a21,  a22,  a23,  a24,
+     a25,  a26,  a27,  a28,  a29,  a30,  a31,  a32,
+     a33,  a34,  a35,  a36,  a37,  a38,  a39,  a40,
+     a41,  a42,  a43,  a44,  a45,  a46,  a47,  a48,
+     a49,  a50,  a51,  a52,  a53,  a54,  a55,  a56,
+     a57,  a58,  a59,  a60,  a61,  a62,  a63,  a64,
+     a65,  a66,  a67,  a68,  a69,  a70,  a71,  a72,
+     a73,  a74,  a75,  a76,  a77,  a78,  a79,  a80,
+     a81,  a82,  a83,  a84,  a85,  a86,  a87,  a88,
+     a89,  a90,  a91,  a92,  a93,  a94,  a95,  a96,
+     a97,  a98,  a99,  a100,  a101,  a102,  a103,
+     a104,  a105,  a106,  a107,  a108,  a109,  a110,
+     a111,  a112,  a113,  a114,  a115,  a116,  a117,
+     a118,  a119,  a120,  a121,  a122,  a123,  a124,
+     a125,  a126,  a127,  a128,  a129,  a130,  a131,
+     a132,  a133,  a134,  a135,  a136,  a137,  a138,
+     a139,  a140,  a141,  a142,  a143,  a144,  a145,
+     a146,  a147,  a148,  a149,  a150,  a151,  a152,
+     a153,  a154,  a155,  a156,  a157,  a158,  a159,
+     a160,  a161,  a162,  a163,  a164,  a165,  a166,
+     a167,  a168,  a169,  a170,  a171,  a172,  a173,
+     a174,  a175,  a176,  a177,  a178,  a179,  a180,
+     a181,  a182,  a183,  a184,  a185,  a186,  a187,
+     a188,  a189,  a190,  a191,  a192,  a193,  a194,
+     a195,  a196,  a197,  a198,  a199,  a200,  a201,
+     a202,  a203,  a204,  a205,  a206,  a207,  a208,
+     a209,  a210,  a211,  a212,  a213,  a214,  a215,
+     a216,  a217,  a218,  a219,  a220,  a221,  a222,
+     a223,  a224,  a225,  a226,  a227,  a228,  a229,
+     a230,  a231,  a232,  a233,  a234,  a235,  a236,
+     a237,  a238,  a239,  a240,  a241,  a242,  a243,
+     a244,  a245,  a246,  a247,  a248,  a249,  a250,
+     a251,  a252,  a253,  a254,  a255,  a256,  a257,
+     a258,  a259,  a260,  a261,  a262,  a263,  a264,
+     a265,  a266,  a267,  a268,  a269,  a270,  a271,
+     a272,  a273,  a274,  a275,  a276,  a277,  a278,
+     a279,  a280,  a281,  a282,  a283,  a284,  a285,
+     a286,  a287,  a288,  a289,  a290,  a291,  a292,
+     a293,  a294,  a295,  a296,  a297,  a298,  a299,
+     a300,  a301,  a302) end
+]]) 
+assert(not ok and string.find(msg, "register overflow"))
+
+local ok, msg = loadstring([[
+   function main()
+     local a = {...}
+   end
+]])
+assert(not ok and string.find(msg, "cannot use '...' outside a vararg function"))

+ 10 - 10
common/src/github.com/yuin/gopher-lua/ast/ast.go

@@ -1,29 +1,29 @@
 package ast
 
 type PositionHolder interface {
-  Line() int
-  SetLine(int)
-  LastLine() int
-  SetLastLine(int)
+	Line() int
+	SetLine(int)
+	LastLine() int
+	SetLastLine(int)
 }
 
 type Node struct {
-  line int
-  lastline int
+	line     int
+	lastline int
 }
 
 func (self *Node) Line() int {
-  return self.line
+	return self.line
 }
 
 func (self *Node) SetLine(line int) {
-  self.line = line
+	self.line = line
 }
 
 func (self *Node) LastLine() int {
-  return self.lastline
+	return self.lastline
 }
 
 func (self *Node) SetLastLine(line int) {
-  self.lastline = line
+	self.lastline = line
 }

+ 2 - 2
common/src/github.com/yuin/gopher-lua/ast/misc.go

@@ -6,8 +6,8 @@ type Field struct {
 }
 
 type ParList struct {
-  HasVargs bool
-  Names    []string
+	HasVargs bool
+	Names    []string
 }
 
 type FuncName struct {

+ 27 - 3
common/src/github.com/yuin/gopher-lua/auxlib.go

@@ -1,6 +1,7 @@
 package lua
 
 import (
+	"bufio"
 	"fmt"
 	"io"
 	"os"
@@ -343,18 +344,41 @@ func (ls *LState) CallMeta(obj LValue, event string) LValue {
 
 func (ls *LState) LoadFile(path string) (*LFunction, error) {
 	var file *os.File
-	var reader io.Reader
 	var err error
 	if len(path) == 0 {
-		reader = os.Stdin
+		file = os.Stdin
 	} else {
 		file, err = os.Open(path)
 		defer file.Close()
 		if err != nil {
 			return nil, newApiErrorE(ApiErrorFile, err)
 		}
-		reader = file
 	}
+
+	reader := bufio.NewReader(file)
+	// get the first character.
+	c, err := reader.ReadByte()
+	if err != nil && err != io.EOF {
+		return nil, newApiErrorE(ApiErrorFile, err)
+	}
+	if c == byte('#') {
+		// Unix exec. file?
+		// skip first line
+		_, err, _ = readBufioLine(reader)
+		if err != nil {
+			return nil, newApiErrorE(ApiErrorFile, err)
+		}
+	}
+
+	if err != io.EOF {
+		// if the file is not empty,
+		// unread the first character of the file or newline character(readBufioLine's last byte).
+		err = reader.UnreadByte()
+		if err != nil {
+			return nil, newApiErrorE(ApiErrorFile, err)
+		}
+	}
+
 	return ls.Load(reader, path)
 }
 

+ 39 - 0
common/src/github.com/yuin/gopher-lua/auxlib_test.go

@@ -1,6 +1,8 @@
 package lua
 
 import (
+	"io/ioutil"
+	"os"
 	"testing"
 )
 
@@ -292,3 +294,40 @@ func TestOptChannel(t *testing.T) {
 		return 0
 	}, "channel expected, got string")
 }
+
+func TestLoadFileForShebang(t *testing.T) {
+	tmpFile, err := ioutil.TempFile("", "")
+	errorIfNotNil(t, err)
+
+	err = ioutil.WriteFile(tmpFile.Name(), []byte(`#!/path/to/lua
+print("hello")
+`), 0644)
+	errorIfNotNil(t, err)
+
+	defer func() {
+		tmpFile.Close()
+		os.Remove(tmpFile.Name())
+	}()
+
+	L := NewState()
+	defer L.Close()
+
+	_, err = L.LoadFile(tmpFile.Name())
+	errorIfNotNil(t, err)
+}
+
+func TestLoadFileForEmptyFile(t *testing.T) {
+	tmpFile, err := ioutil.TempFile("", "")
+	errorIfNotNil(t, err)
+
+	defer func() {
+		tmpFile.Close()
+		os.Remove(tmpFile.Name())
+	}()
+
+	L := NewState()
+	defer L.Close()
+
+	_, err = L.LoadFile(tmpFile.Name())
+	errorIfNotNil(t, err)
+}

+ 6 - 2
common/src/github.com/yuin/gopher-lua/baselib.go

@@ -68,9 +68,13 @@ func baseCollectGarbage(L *LState) int {
 func baseDoFile(L *LState) int {
 	src := L.ToString(1)
 	top := L.GetTop()
-	if err := L.DoFile(src); err != nil {
-		L.RaiseError(err.Error())
+	fn, err := L.LoadFile(src)
+	if err != nil {
+		L.Push(LString(err.Error()))
+		L.Panic(L)
 	}
+	L.Push(fn)
+	L.Call(0, MultRet)
 	return L.GetTop() - top
 }
 

+ 0 - 1
common/src/github.com/yuin/gopher-lua/parse/parser.go

@@ -112,7 +112,6 @@ const yyErrCode = 2
 const yyMaxDepth = 200
 
 //line parser.go.y:514
-
 func TokenName(c int) string {
 	if c >= TAnd && c-TAnd < len(yyToknames) {
 		if yyToknames[c-TAnd] != "" {

+ 2 - 0
common/src/github.com/yuin/gopher-lua/script_test.go

@@ -17,6 +17,8 @@ var gluaTests []string = []string{
 	"os.lua",
 	"table.lua",
 	"vm.lua",
+	"math.lua",
+	"strings.lua",
 }
 
 var luaTests []string = []string{

+ 0 - 4
common/src/github.com/yuin/gopher-lua/stringlib.go

@@ -99,10 +99,6 @@ func strFind(L *LState) int {
 	if L.GetTop() == 4 {
 		plain = LVAsBool(L.Get(4))
 	}
-	if len(str) == 0 && len(pattern) == 0 {
-		L.Push(LNumber(1))
-		return 1
-	}
 
 	if plain {
 		pos := strings.Index(str[init:], pattern)

+ 61 - 0
common/src/github.com/yuin/gopher-lua/table_test.go

@@ -52,6 +52,10 @@ func TestTableInsert(t *testing.T) {
 	errorIfNotEqual(t, LNumber(3), tbl.RawGetInt(4))
 	errorIfNotEqual(t, 4, tbl.Len())
 
+	tbl = newLTable(0, 0)
+	tbl.Insert(5, LNumber(10))
+	errorIfNotEqual(t, LNumber(10), tbl.RawGetInt(5))
+
 }
 
 func TestTableMaxN(t *testing.T) {
@@ -63,6 +67,25 @@ func TestTableMaxN(t *testing.T) {
 
 	tbl = newLTable(0, 0)
 	errorIfNotEqual(t, 0, tbl.MaxN())
+
+	tbl = newLTable(10, 0)
+	errorIfNotEqual(t, 0, tbl.MaxN())
+}
+
+func TestTableRemove(t *testing.T) {
+	tbl := newLTable(0, 0)
+	errorIfNotEqual(t, LNil, tbl.Remove(10))
+	tbl.Append(LTrue)
+	errorIfNotEqual(t, LNil, tbl.Remove(10))
+
+	tbl.Append(LFalse)
+	tbl.Append(LTrue)
+	errorIfNotEqual(t, LFalse, tbl.Remove(2))
+	errorIfNotEqual(t, 2, tbl.MaxN())
+	tbl.Append(LFalse)
+	errorIfNotEqual(t, LFalse, tbl.Remove(-1))
+	errorIfNotEqual(t, 2, tbl.MaxN())
+
 }
 
 func TestTableRawSetInt(t *testing.T) {
@@ -83,6 +106,32 @@ func TestTableRawSetInt(t *testing.T) {
 	errorIfNotEqual(t, LTrue, tbl.RawGetInt(3))
 }
 
+func TestTableRawSetH(t *testing.T) {
+	tbl := newLTable(0, 0)
+	tbl.RawSetH(LString("key"), LTrue)
+	tbl.RawSetH(LString("key"), LNil)
+	_, found := tbl.dict[LString("key")]
+	errorIfNotEqual(t, false, found)
+
+	tbl.RawSetH(LTrue, LTrue)
+	tbl.RawSetH(LTrue, LNil)
+	_, foundb := tbl.dict[LTrue]
+	errorIfNotEqual(t, false, foundb)
+}
+
+func TestTableRawGetH(t *testing.T) {
+	tbl := newLTable(0, 0)
+	errorIfNotEqual(t, LNil, tbl.RawGetH(LNumber(1)))
+	errorIfNotEqual(t, LNil, tbl.RawGetH(LString("key0")))
+	tbl.RawSetH(LString("key0"), LTrue)
+	tbl.RawSetH(LString("key1"), LFalse)
+	tbl.RawSetH(LNumber(1), LTrue)
+	errorIfNotEqual(t, LTrue, tbl.RawGetH(LString("key0")))
+	errorIfNotEqual(t, LTrue, tbl.RawGetH(LNumber(1)))
+	errorIfNotEqual(t, LNil, tbl.RawGetH(LString("notexist")))
+	errorIfNotEqual(t, LNil, tbl.RawGetH(LTrue))
+}
+
 func TestTableForEach(t *testing.T) {
 	tbl := newLTable(0, 0)
 	tbl.Append(LNumber(1))
@@ -95,8 +144,20 @@ func TestTableForEach(t *testing.T) {
 	tbl.RawSetH(LString("b"), LString("b"))
 	tbl.RawSetH(LString("c"), LString("c"))
 
+	tbl.RawSetH(LTrue, LString("true"))
+	tbl.RawSetH(LFalse, LString("false"))
+
 	tbl.ForEach(func(key, value LValue) {
 		switch k := key.(type) {
+		case LBool:
+			switch bool(k) {
+			case true:
+				errorIfNotEqual(t, LString("true"), value)
+			case false:
+				errorIfNotEqual(t, LString("false"), value)
+			default:
+				t.Fail()
+			}
 		case LNumber:
 			switch int(k) {
 			case 1:

+ 36 - 0
common/src/golang.org/x/main.go

@@ -0,0 +1,36 @@
+package main
+
+import (
+	"bytes"
+	"io/ioutil"
+	"log"
+	"net/url"
+
+	"golang.org/x/text/encoding/simplifiedchinese"
+	"golang.org/x/text/transform"
+)
+
+func main() {
+	key := "企业名称"
+	msg := "http://a.com/?"
+	l, _ := url.Parse(msg + key)
+	str := l.Query().Encode()
+	if len(str) > 1 {
+		str = str[0 : len(str)-1]
+	}
+	log.Println("UTF8的url编码", str)
+	str, _ = url.QueryUnescape(str)
+	log.Println("UTF8的url解码", str)
+
+	data, _ := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(key)), simplifiedchinese.GBK.NewEncoder()))
+	l2, _ := url.Parse(msg + string(data))
+	str = l2.Query().Encode()
+	if len(str) > 1 {
+		str = str[0 : len(str)-1]
+	}
+	log.Println("GBK的url编码", str)
+	str, _ = url.QueryUnescape("%C6%F3%D2%B5%C3%FB%B3%C6")
+
+	log.Println("GBK的url解码", str)
+
+}