Skip to content

Commit e37e169

Browse files
committedDec 25, 2017
Issue #154, #156, #157 : make moonscript compiler work
1 parent 3d9c819 commit e37e169

12 files changed

+68
-14
lines changed
 

‎_lua5.1-tests/files.lua

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ assert(io.output():seek("end") == string.len("alo joao"))
2020

2121
assert(io.output():seek("set") == 0)
2222

23-
assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
24-
assert(io.write('çfourth_line'))
23+
assert(io.write('"?lo"', "{a}\n", "second line\n", "third line \n"))
24+
assert(io.write('?fourth_line'))
2525
io.output():close()
2626
io.output(io.stdout)
2727
-- collectgarbage() -- file should be closed by GC
@@ -82,14 +82,14 @@ do -- test error returns
8282
assert(not a and type(b) == "string" and type(c) == "number")
8383
end
8484
assert(io.read(0) == "") -- not eof
85-
assert(io.read(5, '*l') == '"álo"')
85+
assert(io.read(5, '*l') == '"?lo"')
8686
assert(io.read(0) == "")
8787
assert(io.read() == "second line")
8888
local x = io.input():seek()
8989
assert(io.read() == "third line ")
9090
assert(io.input():seek("set", x))
9191
assert(io.read('*l') == "third line ")
92-
assert(io.read(1) == "ç")
92+
assert(io.read(1) == "?")
9393
assert(io.read(string.len"fourth_line") == "fourth_line")
9494
assert(io.input():seek("cur", -string.len"fourth_line"))
9595
assert(io.read() == "fourth_line")
@@ -311,3 +311,4 @@ local s = os.date('%S')
311311
io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a))
312312
io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s))
313313
io.write(string.format('%s\n', _VERSION))
314+
io.write(string.format('%s\n', _GOPHER_LUA_VERSION))

‎_vm.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,12 @@ func init() {
402402
reg.Push(op)
403403
reg.Push(lv)
404404
L.Call(1, 1)
405-
reg.Set(RA, reg.Pop())
405+
ret := reg.Pop()
406+
if ret.Type() == LTNumber {
407+
reg.SetNumber(RA, ret.(LNumber))
408+
} else {
409+
reg.SetNumber(RA, LNumber(0))
410+
}
406411
} else if lv.Type() == LTTable {
407412
reg.SetNumber(RA, LNumber(lv.(*LTable).Len()))
408413
} else {

‎baselib.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
func OpenBase(L *LState) int {
1515
global := L.Get(GlobalsIndex).(*LTable)
1616
L.SetGlobal("_G", global)
17-
L.SetGlobal("_VERSION", LString(PackageName+" "+PackageVersion))
17+
L.SetGlobal("_VERSION", LString(LuaVersion))
18+
L.SetGlobal("_GOPHER_LUA_VERSION", LString(PackageName+" "+PackageVersion))
1819
basemod := L.RegisterModule("_G", baseFuncs)
1920
global.RawSetString("ipairs", L.NewClosure(baseIpairs, L.NewFunction(ipairsaux)))
2021
global.RawSetString("pairs", L.NewClosure(basePairs, L.NewFunction(pairsaux)))
@@ -50,6 +51,8 @@ var baseFuncs = map[string]LGFunction{
5051
// loadlib
5152
"module": loModule,
5253
"require": loRequire,
54+
// hidden features
55+
"newproxy": baseNewProxy,
5356
}
5457

5558
func baseAssert(L *LState) int {
@@ -564,4 +567,20 @@ loopbreak:
564567

565568
/* }}} */
566569

570+
/* hidden features {{{ */
571+
572+
func baseNewProxy(L *LState) int {
573+
ud := L.NewUserData()
574+
L.SetTop(1)
575+
if L.Get(1) == LTrue {
576+
L.SetMetatable(ud, L.NewTable())
577+
} else if d, ok := L.Get(1).(*LUserData); ok {
578+
L.SetMetatable(ud, L.GetMetatable(d))
579+
}
580+
L.Push(ud)
581+
return 1
582+
}
583+
584+
/* }}} */
585+
567586
//

‎compile.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ func compileAssignStmtLeft(context *funcContext, stmt *ast.AssignStmt) (int, []*
529529
case *ast.AttrGetExpr:
530530
ac := &assigncontext{&expcontext{ecTable, regNotDefined, 0}, 0, 0, false, false}
531531
compileExprWithKMVPropagation(context, st.Object, &reg, &ac.ec.reg)
532-
compileExprWithKMVPropagation(context, st.Key, &reg, &ac.keyrk)
532+
ac.keyrk = reg
533+
reg += compileExpr(context, reg, st.Key, ecnone(0))
533534
if _, ok := st.Key.(*ast.StringExpr); ok {
534535
ac.keyks = true
535536
}

‎config.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type LNumber float64
1515

1616
const LNumberBit = 64
1717
const LNumberScanFormat = "%f"
18+
const LuaVersion = "Lua 5.1"
1819

1920
var LuaPath = "LUA_PATH"
2021
var LuaLDir string

‎debuglib.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,20 @@ func debugSetUpvalue(L *LState) int {
151151
}
152152

153153
func debugTraceback(L *LState) int {
154-
msg := L.OptString(1, "")
154+
msg := ""
155155
level := L.OptInt(2, 1)
156-
traceback := strings.TrimSpace(L.stackTrace(level))
156+
ls := L
157+
if L.GetTop() > 0 {
158+
if s, ok := L.Get(1).assertString(); ok {
159+
msg = s
160+
}
161+
if l, ok := L.Get(1).(*LState); ok {
162+
ls = l
163+
msg = ""
164+
}
165+
}
166+
167+
traceback := strings.TrimSpace(ls.stackTrace(level))
157168
if len(msg) > 0 {
158169
traceback = fmt.Sprintf("%s\n%s", msg, traceback)
159170
}

‎package.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ package lua
44
const PackageName = "GopherLua"
55
const PackageVersion = "0.1"
66
const PackageAuthors = "Yusuke Inuzuka"
7-
const PackageCopyRight = PackageName + " " + PackageVersion + " Copyright (C) 2015 " + PackageAuthors
7+
const PackageCopyRight = PackageName + " " + PackageVersion + " Copyright (C) 2015 -2017 " + PackageAuthors

‎stringlib.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,11 @@ func strMatch(L *LState) int {
390390
func strRep(L *LState) int {
391391
str := L.CheckString(1)
392392
n := L.CheckInt(2)
393-
L.Push(LString(strings.Repeat(str, n)))
393+
if n < 0 {
394+
L.Push(LString(""))
395+
} else {
396+
L.Push(LString(strings.Repeat(str, n)))
397+
}
394398
return 1
395399
}
396400

‎table.go

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func (tb *LTable) Len() int {
6464

6565
// Append appends a given LValue to this LTable.
6666
func (tb *LTable) Append(value LValue) {
67+
if value == LNil {
68+
return
69+
}
6770
if tb.array == nil {
6871
tb.array = make([]LValue, 0, defaultArrayCap)
6972
}

‎table_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestTableForEach(t *testing.T) {
166166
errorIfNotEqual(t, LNumber(2), value)
167167
case 3:
168168
errorIfNotEqual(t, LNumber(3), value)
169-
case 5:
169+
case 4:
170170
errorIfNotEqual(t, LNumber(5), value)
171171
default:
172172
t.Fail()

‎tablelib.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ func tableConcat(L *LState) int {
6969
//TODO should flushing?
7070
retbottom := L.GetTop()
7171
for ; i <= j; i++ {
72-
L.Push(tbl.RawGetInt(i))
72+
v := tbl.RawGetInt(i)
73+
if !LVCanConvToString(v) {
74+
L.RaiseError("invalid value (%s) at index %d in table for concat", v.Type().String(), i)
75+
}
76+
L.Push(v)
7377
if i != j {
7478
L.Push(sep)
7579
}

‎vm.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,12 @@ func init() {
444444
reg.Push(op)
445445
reg.Push(lv)
446446
L.Call(1, 1)
447-
reg.Set(RA, reg.Pop())
447+
ret := reg.Pop()
448+
if ret.Type() == LTNumber {
449+
reg.SetNumber(RA, ret.(LNumber))
450+
} else {
451+
reg.SetNumber(RA, LNumber(0))
452+
}
448453
} else if lv.Type() == LTTable {
449454
reg.SetNumber(RA, LNumber(lv.(*LTable).Len()))
450455
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.