Add test for combination of print() and tostring(), test case when tostring is changed in global environment.

This commit is contained in:
James Roseborough
2008-07-23 21:08:09 +00:00
parent 989164405d
commit 5da65f7d41
4 changed files with 42 additions and 2 deletions

View File

@@ -122,10 +122,17 @@ public class BaseLib extends LFunction {
switch ( id ) { switch ( id ) {
case PRINT: { case PRINT: {
int n = vm.gettop(); int n = vm.gettop();
vm.getglobal("tostring");
for ( int i=2; i<=n; i++ ) { for ( int i=2; i<=n; i++ ) {
vm.pushvalue(-1);
vm.pushvalue(i);
vm.call(1, 1);
if ( vm.type(-1) != Lua.LUA_TSTRING )
vm.error( "'tostring' must return a string to 'print'" );
if ( i > 2 ) if ( i > 2 )
STDOUT.print( "\t" ); STDOUT.print( "\t" );
STDOUT.print( vm.tostring(i) ); STDOUT.print( vm.tostring(-1) );
vm.poplvalue();
} }
STDOUT.println(); STDOUT.println();
vm.resettop(); vm.resettop();

View File

@@ -321,7 +321,10 @@ public class LValue {
baos.write(b,0,b.length); baos.write(b,0,b.length);
} }
/** Return true if this is a LString */ /** Return true if this is a lua string, meaning it is
* either a LString or LNumber,since all numbers are
* convertible to strings in lua
*/
public boolean isString() { public boolean isString() {
return false; return false;
} }

View File

@@ -112,6 +112,10 @@ public class CompatibiltyTest extends ScriptDrivenTest {
runTest("pcalls"); runTest("pcalls");
} }
public void testPrint() throws IOException, InterruptedException {
runTest("print");
}
public void testRequire() throws IOException, InterruptedException { public void testRequire() throws IOException, InterruptedException {
runTest("require"); runTest("require");
} }

26
src/test/res/print.lua Normal file
View File

@@ -0,0 +1,26 @@
-- print uses tostring under-the-hood!
local function f()
print()
print('abc')
print(123)
print(true)
print('abc',123,true)
end
local function g()
local fenv = {tostring=function(x)
return '*'..type(x)..'*'
end}
package.seeall(fenv)
f()
print('setfenv', pcall(setfenv, 0, fenv), {}, f )
f()
end
local s,c = pcall( coroutine.create, g )
print('create', s, s and type(c) or c)
print('resume', pcall( coroutine.resume, c ) )
f()