phpv8 / v8js

V8 Javascript Engine for PHP — This PHP extension embeds the Google V8 Javascript Engine
http://pecl.php.net/package/v8js
MIT License
1.83k stars 200 forks source link

Nested PHP thrown exception handling issue #113

Closed rosmo closed 9 years ago

rosmo commented 9 years ago

Exceptions thrown from a nested V8 seem to get lost and don't interrupt the Javascript execution:

Here's a small test I devised to test this out:

--TEST--
Test V8::executeString() : Test nested exception handling
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php

$js = <<<'EOT'
PHP.cb('test');
PHP.print('foo');
"foo";
EOT;

$js2 = <<<'EOT'
PHP.throwException();
PHP.print('bar');
"bar";
EOT;

$v8 = new V8Js();
$v8->print = function ($msg) {
         echo $msg . "\n";
};
$v8->cb = function ($msg) use ($js2) {
   $v8two = new V8Js();
         $v8two->throwException = function () {
            throw new \Exception('nested');
         };
        try {
                var_dump($v8two->executeString($js2, 'b.js'));
        } catch (\Exception $e) {
          echo 'Exception nested: ' . get_class($e) . ': ' . $e->getMessage() . "\n";
        }
};

try {
  var_dump($v8->executeString($js, 'a.js'));
} catch (\V8JsScriptException $e) {
        echo 'V8JsScriptException: ' . get_class($e) . ': ' . $e->getMessage() . "\n";
} catch (\Exception $e) {
        echo 'Exception: ' . get_class($e) . ': ' . $e->getMessage() . "\n";
}
?>
===EOF===
--EXPECT--
Exception nested: Exception: nested
foo
string(3) "foo"
===EOF===

Output from older version:

Exception nested: Exception: nested
foo
string(3) "foo"

Output from latest master:

Exception nested: V8JsScriptException: b.js:2: TypeError: Object #<V8Js> has no method 'print'
foo
string(3) "foo"

(I rolled back all the way before compile script stuff and it doesn't affect this)

rosmo commented 9 years ago

My bad - I haven't noticed the new report_uncaught_exceptions flag in V8Js construct. That fixes this issue.