Partial implementation of metatables.
This commit is contained in:
@@ -38,7 +38,7 @@ public class LuacRunner {
|
||||
state.push( c );
|
||||
for ( int i=0; i<args.length; i++ )
|
||||
state.push( new LString(args[i]) );
|
||||
state.docall(args.length, false);
|
||||
state.docall(args.length, 0);
|
||||
|
||||
// print result?
|
||||
System.out.println("stack:");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
LUA_HOME=/cygdrive/c/programs/lua5.1
|
||||
TESTS="test1 test2 test3 test4 test5"
|
||||
TESTS="test5"
|
||||
TESTS="test6"
|
||||
for x in $TESTS
|
||||
do
|
||||
echo compiling $x
|
||||
|
||||
44
src/test/res/test6.lua
Normal file
44
src/test/res/test6.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
t = { 11, 22, 33, you='one', me='two' }
|
||||
--[[
|
||||
for a,b in pairs(t) do
|
||||
print( a, b )
|
||||
end
|
||||
print( "t[2]", t[2] )
|
||||
print( "t.me", t.me )
|
||||
print( "t.fred", t.fred )
|
||||
print( "t['me']", t["me"] )
|
||||
print( "t['fred']", t["fred"] )
|
||||
print( "me", me )
|
||||
print( "fred", fred )
|
||||
print( "t[me]", t[me] )
|
||||
print( "t[fred]", t[fred] )
|
||||
--]]
|
||||
|
||||
-- basic metatable setting
|
||||
t = { 11, 22, 33, you='one', me='two' }
|
||||
mt = { __index = print, __newindex = print }
|
||||
setmetatable(t,mt)
|
||||
a = t[11]
|
||||
b = t.one
|
||||
c = t[1]
|
||||
d = t.you
|
||||
t[4] = 'rat'
|
||||
t[1] = 'pipe'
|
||||
print( a, b, c, d )
|
||||
for a,b in pairs(t) do
|
||||
print( a, b )
|
||||
end
|
||||
|
||||
-- delegate to actual metatable
|
||||
s = { }
|
||||
mt = { __newindex = s, __index = _G }
|
||||
setmetatable(t, mt)
|
||||
print( t.you )
|
||||
x = 'wow'
|
||||
print( t.x )
|
||||
me = 'here'
|
||||
print( t.me )
|
||||
t[5] = 99
|
||||
for a,b in pairs(s) do
|
||||
print( a, b )
|
||||
end
|
||||
BIN
src/test/res/test6.luac
Normal file
BIN
src/test/res/test6.luac
Normal file
Binary file not shown.
Reference in New Issue
Block a user