Skip to content

Commit b12a2fb

Browse files
committed
Merge remote-tracking branch 'stesie/require-exception-handling' into php7
2 parents 278b4fb + 9fd201e commit b12a2fb

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

tests/commonjs_exception_001.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Test V8Js::setModuleLoader : Forward exceptions
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
8+
$JS = <<< EOT
9+
var foo = require("./test");
10+
EOT;
11+
12+
$v8 = new V8Js();
13+
$v8->setModuleLoader(function($module) {
14+
throw new Exception('some exception');
15+
});
16+
17+
$v8->executeString($JS, 'module.js', V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS);
18+
?>
19+
===EOF===
20+
--EXPECTF--
21+
Fatal error: Uncaught Exception: some exception in %s%ecommonjs_exception_001.php:9
22+
Stack trace:
23+
#0 [internal function]: {closure}('test')
24+
#1 %s%ecommonjs_exception_001.php(12): V8Js->executeString('var foo = requi...', 'module.js', 4)
25+
#2 {main}
26+
27+
Next V8JsScriptException: module.js:1: Exception: some exception in %s%ecommonjs_exception_001.php:9
28+
Stack trace:
29+
#0 [internal function]: {closure}('test')
30+
#1 %s%ecommonjs_exception_001.php(12): V8Js->executeString('var foo = requi...', 'module.js', 4)
31+
#2 {main} in %s%ecommonjs_exception_001.php:12
32+
Stack trace:
33+
#0 %s%ecommonjs_exception_001.php(12): V8Js->executeString('var foo = requi...', 'module.js', 4)
34+
#1 {main}
35+
thrown in %s%ecommonjs_exception_001.php on line 12

tests/commonjs_exception_002.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Test V8Js::setModuleNormaliser : Forward exceptions
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
8+
$JS = <<< EOT
9+
var foo = require("./test");
10+
EOT;
11+
12+
$v8 = new V8Js();
13+
$v8->setModuleNormaliser(function($module) {
14+
throw new Exception('some exception');
15+
});
16+
$v8->setModuleLoader(function($module) {
17+
echo 'dummy ...';
18+
});
19+
20+
$v8->executeString($JS, 'module.js', V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS);
21+
?>
22+
===EOF===
23+
--EXPECTF--
24+
Fatal error: Uncaught Exception: some exception in %s%ecommonjs_exception_002.php:9
25+
Stack trace:
26+
#0 [internal function]: {closure}('', './test')
27+
#1 %s%ecommonjs_exception_002.php(15): V8Js->executeString('var foo = requi...', 'module.js', 4)
28+
#2 {main}
29+
30+
Next V8JsScriptException: module.js:1: Exception: some exception in %s%ecommonjs_exception_002.php:9
31+
Stack trace:
32+
#0 [internal function]: {closure}('', './test')
33+
#1 %s%ecommonjs_exception_002.php(15): V8Js->executeString('var foo = requi...', 'module.js', 4)
34+
#2 {main} in %s%ecommonjs_exception_002.php:15
35+
Stack trace:
36+
#0 %s%ecommonjs_exception_002.php(15): V8Js->executeString('var foo = requi...', 'module.js', 4)
37+
#1 {main}
38+
thrown in %s%ecommonjs_exception_002.php on line 15

v8js_methods.cc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,14 @@ V8JS_METHOD(require)
267267

268268
// Check if an exception was thrown
269269
if (EG(exception)) {
270-
// Clear the PHP exception and throw it in V8 instead
271-
zend_clear_exception();
272-
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module normaliser callback exception")));
270+
if (c->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
271+
zval tmp_zv;
272+
ZVAL_OBJ(&tmp_zv, EG(exception));
273+
info.GetReturnValue().Set(isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate)));
274+
zend_clear_exception();
275+
} else {
276+
v8js_terminate_execution(isolate);
277+
}
273278
return;
274279
}
275280

@@ -389,9 +394,15 @@ V8JS_METHOD(require)
389394
efree(normalised_module_id);
390395
efree(normalised_path);
391396

392-
// Clear the PHP exception and throw it in V8 instead
393-
zend_clear_exception();
394-
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module loader callback exception")));
397+
if (c->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
398+
zval tmp_zv;
399+
ZVAL_OBJ(&tmp_zv, EG(exception));
400+
info.GetReturnValue().Set(isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate)));
401+
zend_clear_exception();
402+
} else {
403+
v8js_terminate_execution(isolate);
404+
}
405+
395406
return;
396407
}
397408

0 commit comments

Comments
 (0)