1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
--TEST--
LuaSandboxFunction::call
--FILE--
<?php
$sandbox = new LuaSandbox;
var_dump( $sandbox->loadString( 'return 1' )->call() );
echo "Proper handling of circular tables returned by Lua: ";
$sandbox = new LuaSandbox;
try {
$ret = $sandbox->loadString( 'local t = {}; t.t = t; return t' )->call();
echo var_export( $ret, 1 ) . "\n";
} catch ( Exception $ex ) {
echo "Exception: " . $ex->getMessage() . "\n";
}
echo "Proper handling of circular tables in Lua→PHP call: ";
$sandbox = new LuaSandbox;
$f = $sandbox->wrapPhpFunction( function () {
echo func_num_args() . " args ok\n";
} );
try {
$sandbox->loadString( 'local f = ...; local t = {}; t.t = t; f( t )' )->call( $f );
} catch ( Exception $ex ) {
echo "Exception: " . $ex->getMessage() . "\n";
}
echo "Passing lots of arguments PHP->Lua doesn't cause a crash: ";
$sandbox = new LuaSandbox;
$ret = call_user_func_array(
array( $sandbox->loadString( 'return select( "#", ... )' ), 'call' ),
array_fill( 0, 500, '' )
);
echo "$ret[0] args ok\n";
echo "Passing lots of arguments Lua->PHP doesn't cause a crash: ";
$sandbox = new LuaSandbox;
$f = $sandbox->wrapPhpFunction( function () {
echo func_num_args() . " args ok\n";
} );
$sandbox->loadString( 'local f = ...; f( string.byte( string.rep( "x", 500 ), 1, -1 ) )' )->call( $f );
echo "Returning lots of values PHP->Lua doesn't cause a crash: ";
$sandbox = new LuaSandbox;
$f = $sandbox->wrapPhpFunction( function () {
return array_fill( 0, 500, '' );
} );
$ret = $sandbox->loadString( 'local f = ...; return select( "#", f() )' )->call( $f );
echo "$ret[0] values ok\n";
echo "Returning lots of values Lua->PHP doesn't cause a crash: ";
$sandbox = new LuaSandbox;
$ret = $sandbox->loadString( 'return string.byte( string.rep( "x", 500 ), 1, -1 )' )->call();
echo count( $ret ) . " values ok\n";
echo "Passing deeply-nested arrays PHP->Lua doesn't cause a crash: ";
$sandbox = new LuaSandbox;
$v = 1;
for ( $i = 0; $i < 500; $i++ ) {
$v = array( $v );
}
$lua = <<<LUA
local ct, t = 0, ...
while type( t ) == "table" do
_, t = next( t )
ct = ct + 1
end
return ct
LUA;
$ret = $sandbox->loadString( $lua )->call( $v );
echo "$ret[0] levels ok\n";
echo "Passing deeply-nested tables Lua->PHP doesn't cause a crash: ";
$sandbox = new LuaSandbox;
$ret = $sandbox->loadString( 'local t = 1; for i = 1, 500 do t = { t } end; return t' )->call();
$ct = 0;
$v = $ret[0];
while ( is_array( $v ) ) {
$v = reset( $v );
$ct++;
}
echo "$ct levels ok\n";
echo "Proper handling of invalid keys in Lua→PHP conversion (table): ";
$sandbox = new LuaSandbox;
try {
$ret = $sandbox->loadString( 'return { [{}] = 1 }' )->call();
echo var_export( $ret[0], 1 ) . "\n";
} catch ( Exception $ex ) {
echo "Exception: " . $ex->getMessage() . "\n";
}
echo "Proper handling of invalid keys in Lua→PHP conversion (bool): ";
$sandbox = new LuaSandbox;
try {
$ret = $sandbox->loadString( 'return { [true] = 1 }' )->call();
echo var_export( $ret[0], 1 ) . "\n";
} catch ( Exception $ex ) {
echo "Exception: " . $ex->getMessage() . "\n";
}
echo "Proper handling of invalid keys in Lua→PHP conversion (function): ";
$sandbox = new LuaSandbox;
try {
$ret = $sandbox->loadString( 'return { [tostring] = 1 }' )->call();
echo var_export( $ret[0], 1 ) . "\n";
} catch ( Exception $ex ) {
echo "Exception: " . $ex->getMessage() . "\n";
}
echo "Proper handling of unusual keys in Lua→PHP conversion (float): ";
$sandbox = new LuaSandbox;
try {
$ret = $sandbox->loadString( 'return { [1.5] = 1 }' )->call();
echo var_export( $ret[0], 1 ) . "\n";
} catch ( Exception $ex ) {
echo "Exception: " . $ex->getMessage() . "\n";
}
echo "Proper handling of unusual keys in Lua→PHP conversion (inf): ";
$sandbox = new LuaSandbox;
try {
$ret = $sandbox->loadString( 'return { [math.huge] = 1 }' )->call();
echo var_export( $ret[0], 1 ) . "\n";
} catch ( Exception $ex ) {
echo "Exception: " . $ex->getMessage() . "\n";
}
--EXPECT--
array(1) {
[0]=>
int(1)
}
Proper handling of circular tables returned by Lua: Exception: Cannot pass circular reference to PHP
Proper handling of circular tables in Lua→PHP call: Exception: Cannot pass circular reference to PHP
Passing lots of arguments PHP->Lua doesn't cause a crash: 500 args ok
Passing lots of arguments Lua->PHP doesn't cause a crash: 500 args ok
Returning lots of values PHP->Lua doesn't cause a crash: 500 values ok
Returning lots of values Lua->PHP doesn't cause a crash: 500 values ok
Passing deeply-nested arrays PHP->Lua doesn't cause a crash: 500 levels ok
Passing deeply-nested tables Lua->PHP doesn't cause a crash: 500 levels ok
Proper handling of invalid keys in Lua→PHP conversion (table): Exception: Cannot use table as an array key when passing data from Lua to PHP
Proper handling of invalid keys in Lua→PHP conversion (bool): Exception: Cannot use boolean as an array key when passing data from Lua to PHP
Proper handling of invalid keys in Lua→PHP conversion (function): Exception: Cannot use function as an array key when passing data from Lua to PHP
Proper handling of unusual keys in Lua→PHP conversion (float): array (
'1.5' => 1,
)
Proper handling of unusual keys in Lua→PHP conversion (inf): array (
'inf' => 1,
)
|