File: lua_catches_php_exception.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 (57 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download | duplicates (2)
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
--TEST--
PHP throwing exceptions to be caught by pcall()
--FILE--
<?php
$lua = <<<LUA
	function pcall_test(f)
		local status, msg
		status, msg = pcall(f)
		if not status then
			return "Caught: " .. msg
		else
			return "success"
		end
	end

	function hang_test(f)
		pcall_test(f)
		while true do end
	end
LUA;

function runtime_error() {
	throw new LuaSandboxRuntimeError("runtime error");
}
function fatal_error() {
	throw new LuaSandboxFatalError("fatal error");
}
function plain_exception() {
	throw new Exception("exception");
}

$tests = array(
	'Runtime error' => array( 'pcall_test', 'runtime_error' ),
	'Fatal error' => array( 'hang_test', 'fatal_error' ),
	'Plain Exception' => array( 'hang_test', 'plain_exception' ),
);

foreach ( $tests as $desc => $info ) {
	list( $wrapper, $funcName ) = $info;
	echo "$desc: ";
	try {
		$sandbox = new LuaSandbox;
		$sandbox->loadString( $lua )->call();
		$sandbox->setCPULimit( 0.25 );
		$sandbox->registerLibrary( 'test', array( 'test' => $funcName ) );
		$res = $sandbox->loadString( 'return test.test' )->call();
		print implode("\n",
			$sandbox->callFunction( $wrapper, $res[0] ) ) . "\n";
	} catch ( Exception $e ) {
		echo get_class( $e ) . ': ' . $e->getMessage() . "\n";
	}
}

--EXPECT--
Runtime error: Caught: runtime error
Fatal error: LuaSandboxFatalError: fatal error
Plain Exception: Exception: exception