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
|
--TEST--
Data type round-tripping
--FILE--
<?php
function doTest( $test, $data ) {
printf( "%-25s ", "$test:" );
$sandbox = new LuaSandbox;
$sandbox->setMemoryLimit( 100000 );
$sandbox->setCPULimit( 0.1 );
try {
$ret = $sandbox->loadString( 'return ...' )->call( $data );
if ( is_array( $ret[0] ) ) {
ksort( $ret[0], SORT_STRING );
}
printf( "%s\n", preg_replace( '/\s+/', ' ', var_export( $ret[0], 1 ) ) );
} catch ( LuaSandboxError $e ) {
printf( "EXCEPTION: %s\n", $e->getMessage() );
}
}
doTest( 'null', null );
doTest( 'int', 123 );
if ( is_int( 17179869184 ) ) {
doTest( 'long', 17179869184 );
} else {
// Fake it for 32-bit systems
printf( "%-25s %s\n", "long:", "17179869184" );
}
doTest( 'double', 3.125 );
doTest( 'NAN', NAN );
doTest( 'INF', INF );
doTest( 'true', true );
doTest( 'false', false );
doTest( 'string', 'foobar' );
doTest( 'empty string', '' );
doTest( 'string containing NULs', "foo\0bar" );
doTest( 'array', array( 'foo', 'bar' ) );
doTest( 'associative array', array( 'foo', 'bar' => 'baz' ) );
$var = 42;
doTest( 'array with reference', [ &$var ] );
$sandbox = new LuaSandbox;
$sandbox->setMemoryLimit( 100000 );
$sandbox->setCPULimit( 0.1 );
$func = $sandbox->wrapPhpFunction( function ( $x ) { return [ "FUNC: $x" ]; } );
try {
$ret = $sandbox->loadString( 'return ...' )->call( $func );
$ret2 = $ret[0]->call( "ok" );
printf( "%-25s %s\n", "function, pass-through:", $ret2[0] );
$ret = $sandbox->loadString( 'f = ...; return f( "ok" )' )->call( $func );
printf( "%-25s %s\n", "function, called:", $ret[0] );
$ret = $sandbox->loadString( 'return function ( x ) return "FUNC: " .. x end' )->call();
$ret2 = $ret[0]->call( "ok" );
printf( "%-25s %s\n", "function, returned:", $ret2[0] );
} catch ( LuaSandboxError $e ) {
printf( "EXCEPTION: %s\n", $e->getMessage() );
}
--EXPECT--
null: NULL
int: 123
long: 17179869184
double: 3.125
NAN: NAN
INF: INF
true: true
false: false
string: 'foobar'
empty string: ''
string containing NULs: 'foo' . "\0" . 'bar'
array: array ( 0 => 'foo', 1 => 'bar', )
associative array: array ( 0 => 'foo', 'bar' => 'baz', )
array with reference: array ( 0 => 42, )
function, pass-through: FUNC: ok
function, called: FUNC: ok
function, returned: FUNC: ok
|