Skip to content

Commit 5f5265e

Browse files
authored
Merge branch 'yuin:master' into master
2 parents 59f0a4d + ccacf66 commit 5f5265e

File tree

6 files changed

+99
-12
lines changed

6 files changed

+99
-12
lines changed

README.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,10 @@ Sharing byte code is safe as it is read only and cannot be altered by lua script
595595
596596
// Example shows how to share the compiled byte code from a lua script between multiple VMs.
597597
func Example() {
598-
codeToShare := CompileLua("mylua.lua")
598+
codeToShare, err := CompileLua("mylua.lua")
599+
if err != nil {
600+
panic(err)
601+
}
599602
a := lua.NewState()
600603
b := lua.NewState()
601604
c := lua.NewState()
@@ -847,7 +850,7 @@ Lua has an interpreter called ``lua`` . GopherLua has an interpreter called ``gl
847850
----------------------------------------------------------------
848851
How to Contribute
849852
----------------------------------------------------------------
850-
See `Guidlines for contributors <https://github.com/yuin/gopher-lua/tree/master/.github/CONTRIBUTING.md>`_ .
853+
See `Guidelines for contributors <https://github.com/yuin/gopher-lua/tree/master/.github/CONTRIBUTING.md>`_ .
851854

852855
----------------------------------------------------------------
853856
Libraries for GopherLua
@@ -865,6 +868,7 @@ Libraries for GopherLua
865868
- `gluaxmlpath <https://github.com/ailncode/gluaxmlpath>`_ : An xmlpath module for gopher-lua
866869
- `gmoonscript <https://github.com/rucuriousyet/gmoonscript>`_ : Moonscript Compiler for the Gopher Lua VM
867870
- `loguago <https://github.com/rucuriousyet/loguago>`_ : Zerolog wrapper for Gopher-Lua
871+
- `gluabit32 <https://github.com/PeerDB-io/gluabit32>`_ : [Port of Lua 5.2 bit32](https://www.lua.org/manual/5.2/manual.html#6.7)
868872
- `gluacrypto <https://github.com/tengattack/gluacrypto>`_ : A native Go implementation of crypto library for the GopherLua VM.
869873
- `gluasql <https://github.com/tengattack/gluasql>`_ : A native Go implementation of SQL client for the GopherLua VM.
870874
- `purr <https://github.com/leyafo/purr>`_ : A http mock testing tool.
@@ -873,6 +877,8 @@ Libraries for GopherLua
873877
- `glua-async <https://github.com/CuberL/glua-async>`_ : An async/await implement for gopher-lua.
874878
- `gopherlua-debugger <https://github.com/edolphin-ydf/gopherlua-debugger>`_ : A debugger for gopher-lua
875879
- `gluamahonia <https://github.com/super1207/gluamahonia>`_ : An encoding converter for gopher-lua
880+
- `awesome-gopher-lua <https://github.com/Root-lee/awesome-gopher-lua>`_ : Collections of awesome libraries for GopherLua.
881+
876882
----------------------------------------------------------------
877883
Donation
878884
----------------------------------------------------------------

auxlib_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package lua
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
@@ -300,10 +299,10 @@ func TestOptChannel(t *testing.T) {
300299
}
301300

302301
func TestLoadFileForShebang(t *testing.T) {
303-
tmpFile, err := ioutil.TempFile("", "")
302+
tmpFile, err := os.CreateTemp("", "")
304303
errorIfNotNil(t, err)
305304

306-
err = ioutil.WriteFile(tmpFile.Name(), []byte(`#!/path/to/lua
305+
err = os.WriteFile(tmpFile.Name(), []byte(`#!/path/to/lua
307306
print("hello")
308307
`), 0644)
309308
errorIfNotNil(t, err)
@@ -321,7 +320,7 @@ print("hello")
321320
}
322321

323322
func TestLoadFileForEmptyFile(t *testing.T) {
324-
tmpFile, err := ioutil.TempFile("", "")
323+
tmpFile, err := os.CreateTemp("", "")
325324
errorIfNotNil(t, err)
326325

327326
defer func() {

compile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (cd *codeStore) PropagateMV(top int, save *int, reg *int, inc int) {
237237

238238
func (cd *codeStore) AddLoadNil(a, b, line int) {
239239
last := cd.Last()
240-
if opGetOpCode(last) == OP_LOADNIL && (opGetArgA(last)+opGetArgB(last)) == a {
240+
if opGetOpCode(last) == OP_LOADNIL && (opGetArgB(last)+1) == a {
241241
cd.SetB(cd.LastPC(), b)
242242
} else {
243243
cd.AddABC(OP_LOADNIL, a, b, 0, line)

iolib.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"os"
109
"os/exec"
1110
"syscall"
@@ -373,7 +372,7 @@ func fileReadAux(L *LState, file *lFile, idx int) int {
373372
L.Push(v)
374373
case 'a':
375374
var buf []byte
376-
buf, err = ioutil.ReadAll(file.reader)
375+
buf, err = io.ReadAll(file.reader)
377376
if err == io.EOF {
378377
L.Push(emptyLString)
379378
goto normalreturn
@@ -704,7 +703,7 @@ func ioType(L *LState) int {
704703
}
705704

706705
func ioTmpFile(L *LState) int {
707-
file, err := ioutil.TempFile("", "")
706+
file, err := os.CreateTemp("", "")
708707
if err != nil {
709708
L.Push(LNil)
710709
L.Push(LString(err.Error()))

oslib.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package lua
22

33
import (
4-
"io/ioutil"
54
"os"
65
"strings"
76
"time"
@@ -223,7 +222,7 @@ func osTime(L *LState) int {
223222
}
224223

225224
func osTmpname(L *LState) int {
226-
file, err := ioutil.TempFile("", "")
225+
file, err := os.CreateTemp("", "")
227226
if err != nil {
228227
L.RaiseError("unable to generate a unique filename")
229228
}

script_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"runtime"
7+
"strings"
78
"sync/atomic"
89
"testing"
910
"time"
@@ -146,3 +147,86 @@ func TestGlua(t *testing.T) {
146147
func TestLua(t *testing.T) {
147148
testScriptDir(t, luaTests, "_lua5.1-tests")
148149
}
150+
151+
func TestMergingLoadNilBug(t *testing.T) {
152+
// there was a bug where a multiple load nils were being incorrectly merged, and the following code exposed it
153+
s := `
154+
function test()
155+
local a = 0
156+
local b = 1
157+
local c = 2
158+
local d = 3
159+
local e = 4 -- reg 4
160+
local f = 5
161+
local g = 6
162+
local h = 7
163+
164+
if e == 4 then
165+
e = nil -- should clear reg 4, but clears regs 4-8 by mistake
166+
end
167+
if f == nil then
168+
error("bad f")
169+
end
170+
if g == nil then
171+
error("bad g")
172+
end
173+
if h == nil then
174+
error("bad h")
175+
end
176+
end
177+
178+
test()
179+
`
180+
181+
L := NewState()
182+
defer L.Close()
183+
if err := L.DoString(s); err != nil {
184+
t.Error(err)
185+
}
186+
}
187+
188+
func TestMergingLoadNil(t *testing.T) {
189+
// multiple nil assignments to consecutive registers should be merged
190+
s := `
191+
function test()
192+
local a = 0
193+
local b = 1
194+
local c = 2
195+
196+
-- this should generate just one LOADNIL byte code instruction
197+
a = nil
198+
b = nil
199+
c = nil
200+
201+
print(a,b,c)
202+
end
203+
204+
test()`
205+
206+
chunk, err := parse.Parse(strings.NewReader(s), "test")
207+
if err != nil {
208+
t.Fatal(err)
209+
}
210+
211+
compiled, err := Compile(chunk, "test")
212+
if err != nil {
213+
t.Fatal(err)
214+
}
215+
216+
if len(compiled.FunctionPrototypes) != 1 {
217+
t.Fatal("expected 1 function prototype")
218+
}
219+
220+
// there should be exactly 1 LOADNIL instruction in the byte code generated for the above
221+
// anymore, and the LOADNIL merging is not working correctly
222+
count := 0
223+
for _, instr := range compiled.FunctionPrototypes[0].Code {
224+
if opGetOpCode(instr) == OP_LOADNIL {
225+
count++
226+
}
227+
}
228+
229+
if count != 1 {
230+
t.Fatalf("expected 1 LOADNIL instruction, found %d", count)
231+
}
232+
}

0 commit comments

Comments
 (0)