Skip to content

Commit 24a12ca

Browse files
committed
made ipairs respect __index metamethod
1 parent ccacf66 commit 24a12ca

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

README.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ GopherLua: VM and compiler for Lua in Go.
1919
|
2020
2121

22-
GopherLua is a Lua5.1(+ `goto` statement in Lua5.2) VM and compiler written in Go. GopherLua has a same goal
22+
GopherLua is a Lua 5.1 (+ `goto` statement from Lua 5.2 and `ipairs` as in Lua 5.3)
23+
VM and compiler written in Go. GopherLua has a same goal
2324
with Lua: **Be a scripting language with extensible semantics** . It provides
2425
Go APIs that allow you to easily embed a scripting language to your Go host
2526
programs.
@@ -835,6 +836,7 @@ Miscellaneous notes
835836
- GopherLua has a function to set an environment variable : ``os.setenv(name, value)``
836837
- GopherLua support ``goto`` and ``::label::`` statement in Lua5.2.
837838
- `goto` is a keyword and not a valid variable name.
839+
- GopherLua respects the `__index` metamethod in `ipairs` as in Lua 5.3.
838840

839841
----------------------------------------------------------------
840842
Standalone interpreter

_glua-tests/vm.lua

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ assert(#tbl == 10)
1515
setmetatable(tbl, nil)
1616
assert(#tbl == 3)
1717

18+
setmetatable(tbl, {__index = function(t, i)
19+
return i <= 5 and -i or nil
20+
end})
21+
local s = 0
22+
for _, x in ipairs(tbl) do
23+
s = s+x
24+
end
25+
assert(s == -3) -- == 1+2+3-4-5
26+
1827
local ok, msg = pcall(function()
1928
return 1 < "hoge"
2029
end)

baselib.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,17 @@ func baseGetMetatable(L *LState) int {
133133
}
134134

135135
func ipairsaux(L *LState) int {
136-
tb := L.CheckTable(1)
136+
tb := L.Get(1)
137137
i := L.CheckInt(2)
138138
i++
139-
v := tb.RawGetInt(i)
139+
li := LNumber(i)
140+
v := L.getField(tb, li)
140141
if v == LNil {
141142
return 0
142143
} else {
143144
L.Pop(1)
144-
L.Push(LNumber(i))
145-
L.Push(LNumber(i))
145+
L.Push(li)
146+
L.Push(li)
146147
L.Push(v)
147148
return 2
148149
}

0 commit comments

Comments
 (0)