Add tests for __index, __newindex

This commit is contained in:
James Roseborough
2010-08-18 23:05:20 +00:00
parent 2066e41ecd
commit 0d50184e8f
2 changed files with 23 additions and 4 deletions

View File

@@ -3,8 +3,7 @@
<head> <head>
<title>Getting Started with LuaJ</title> <title>Getting Started with LuaJ</title>
<link rel="stylesheet" type="text/css" href="http://sourceforge.net/dbimage.php?id=196140"> <link rel="stylesheet" type="text/css" href="http://www.lua.org/lua.css">
<link rel="stylesheet" type="text/css" href="http://sourceforge.net/dbimage.php?id=196141">
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1"> <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
</head> </head>
@@ -53,7 +52,7 @@ Freely available under the terms of the
Luaj is a lua interpreter based on the 5.1.x version of lua with the following goals in mind: Luaj is a lua interpreter based on the 5.1.x version of lua with the following goals in mind:
<ul> <ul>
<li>Java-centric implementation of lua vm built to leverage standard Java features. <li>Java-centric implementation of lua vm built to leverage standard Java features.
<li>Lightweight to allow for small, fast interpretation of lua bytecode. <li>Lightweight, high performance execution of lua.
<li>Multi-platform to be able to run on JME, JSE, or JEE environments. <li>Multi-platform to be able to run on JME, JSE, or JEE environments.
<li>Complete set of libraries and tools for integration into real-world projects. <li>Complete set of libraries and tools for integration into real-world projects.
<li>Dependable due to sufficient unit testing of vm and library features. <li>Dependable due to sufficient unit testing of vm and library features.
@@ -71,7 +70,7 @@ at improving on the 1.0 vm in the following aspects.
<li>Improved class and package naming conventions. <li>Improved class and package naming conventions.
<li>Improved unit tests of core classes. <li>Improved unit tests of core classes.
<li>Improved quality due to major redesign and rewrite of core elements. <li>Improved quality due to major redesign and rewrite of core elements.
<li>Improved weak table support, including weak keys. <li>More complete implementation including weak keys and values, and all metatags.
</ul> </ul>
<h2>Performance</h2> <h2>Performance</h2>

View File

@@ -43,6 +43,8 @@ local mt = {
return 'mt.__tostring('..type(a)..','..type(b)..')' return 'mt.__tostring('..type(a)..','..type(b)..')'
end, end,
__metatable={}, __metatable={},
__index=buildop('index'),
__newindex=buildop('newindex'),
} }
-- pcall a function and check for a pattern in the error string -- pcall a function and check for a pattern in the error string
@@ -178,3 +180,21 @@ for i=1,#values do
print( debug.setmetatable( a, nil ) ) print( debug.setmetatable( a, nil ) )
end end
print( '---- __index, __newindex' )
values = { aboolean, anumber, afunction, athread }
for i=1,#values do
local a = values[i]
print( type(a), 'before', ecall( 'attempt to index', function() return a.foo end ) )
print( type(a), 'before', ecall( 'attempt to index', function() return a[123] end ) )
print( type(a), 'before', ecall( 'index', function() a.foo = 'bar' end ) )
print( type(a), 'before', ecall( 'index', function() a[123] = 'bar' end ) )
print( type(a), 'before', ecall( 'attempt to index', function() return a:foo() end ) )
print( debug.setmetatable( a, mt ) )
print( type(a), 'after', pcall( function() return a.foo end ) )
print( type(a), 'after', pcall( function() return a[123] end ) )
print( type(a), 'after', pcall( function() a.foo = 'bar' end ) )
print( type(a), 'after', pcall( function() a[123] = 'bar' end ) )
print( type(a), 'after', ecall( 'attempt to call', function() return a:foo() end ) )
print( debug.setmetatable( a, nil ) )
end