Skip to content

Commit 7dcebdd

Browse files
committed
forward exceptions thrown in module loader
1 parent 4161008 commit 7dcebdd

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

tests/commonjs_exception_001.phpt

+35
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

v8js_methods.cc

+9-3
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,15 @@ V8JS_METHOD(require)
390390
efree(normalised_module_id);
391391
efree(normalised_path);
392392

393-
// Clear the PHP exception and throw it in V8 instead
394-
zend_clear_exception(TSRMLS_C);
395-
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module loader callback exception")));
393+
if (c->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
394+
zval tmp_zv;
395+
ZVAL_OBJ(&tmp_zv, EG(exception));
396+
info.GetReturnValue().Set(isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate)));
397+
zend_clear_exception();
398+
} else {
399+
v8js_terminate_execution(isolate);
400+
}
401+
396402
return;
397403
}
398404

0 commit comments

Comments
 (0)