Partial implementation of metatables.

This commit is contained in:
James Roseborough
2007-06-16 15:31:27 +00:00
parent 7449605d56
commit 5d3c86e552
10 changed files with 108 additions and 14 deletions

View File

@@ -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
View 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

Binary file not shown.