File: datatypes-unsupported.phpt

package info (click to toggle)
php-luasandbox 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: ansic: 3,700; xml: 2,152; php: 64; makefile: 13; sh: 3
file content (80 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download | duplicates (3)
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
--TEST--
Handling of unsupported datatypes
--FILE--
<?php

function doTest( $test, $data ) {
	printf( "%s ", "$test (call PHP->Lua):" );
	$sandbox = new LuaSandbox;
	$sandbox->setMemoryLimit( 100000 );
	$sandbox->setCPULimit( 0.1 );
	try {
		$ret = $sandbox->loadString( 'return 1' )->call( $data );
		printf( "%s\n", preg_replace( '/\s+/', ' ', var_export( $ret, 1 ) ) );
	} catch ( LuaSandboxError $e ) {
		printf( "EXCEPTION: %s\n", $e->getMessage() );
	}

	printf( "%s ", "$test (return PHP->Lua):" );
	$sandbox = new LuaSandbox;
	$sandbox->setMemoryLimit( 100000 );
	$sandbox->setCPULimit( 0.1 );
	$f = $sandbox->wrapPhpFunction( function () use ( $data ) {
		return [ $data ];
	} );
	try {
		$sandbox->loadString( 'local f = ...; f()' )->call( $f );
		printf( "%s\n", preg_replace( '/\s+/', ' ', var_export( $ret, 1 ) ) );
	} catch ( LuaSandboxError $e ) {
		printf( "EXCEPTION: %s\n", $e->getMessage() );
	}
}

function doTest2( $test, $lua ) {
	printf( "%s ", "$test (call Lua->PHP):" );
	$sandbox = new LuaSandbox;
	$sandbox->setMemoryLimit( 100000 );
	$sandbox->setCPULimit( 0.1 );
	$f = $sandbox->wrapPhpFunction( function ( $val ) {
		echo "PHP received " . preg_replace( '/\s+/', ' ', var_export( $val, 1 ) ) . "\n";
	} );
	try {
		$sandbox->loadString( "local f = ...\n$lua\nf(v)" )->call( $f );
	} catch ( LuaSandboxError $e ) {
		printf( "EXCEPTION: %s\n", $e->getMessage() );
	}

	printf( "%s ", "$test (return Lua->PHP):" );
	$sandbox = new LuaSandbox;
	$sandbox->setMemoryLimit( 100000 );
	$sandbox->setCPULimit( 0.1 );
	try {
		$ret = $sandbox->loadString( "$lua\nreturn v" )->call();
		printf( "%s\n", preg_replace( '/\s+/', ' ', var_export( $ret, 1 ) ) );
	} catch ( LuaSandboxError $e ) {
		printf( "EXCEPTION: %s\n", $e->getMessage() );
	}
}

$test = array();
$test['foo'] = &$test;
doTest( 'recursive array', $test );

$test = new stdClass;
doTest( 'object', $test );

doTest2( 'recursive table', 'v = {}; v.v = v' );

--EXPECTF--
recursive array (call PHP->Lua): %AWarning: LuaSandboxFunction::call(): Cannot pass circular reference to Lua in %s on line %d
%AWarning: LuaSandboxFunction::call(): unable to convert argument 1 to a lua value in %s on line %d
false
recursive array (return PHP->Lua): %AWarning: LuaSandboxFunction::call(): Cannot pass circular reference to Lua in %s on line %d
false
object (call PHP->Lua): %AWarning: LuaSandboxFunction::call(): Unable to convert object of type stdClass in %s on line %d
%AWarning: LuaSandboxFunction::call(): unable to convert argument 1 to a lua value in %s on line %d
false
object (return PHP->Lua): %AWarning: LuaSandboxFunction::call(): Unable to convert object of type stdClass in %s on line %d
false
recursive table (call Lua->PHP): EXCEPTION: Cannot pass circular reference to PHP
recursive table (return Lua->PHP): EXCEPTION: Cannot pass circular reference to PHP