Skip to content

Commit bf58fe6

Browse files
committed
wrap module loading in zend_try/zend_catch, closes #178
1 parent 39fff23 commit bf58fe6

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

tests/commonjs_fatal_error.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test V8Js::setModuleLoader : Handle fatal errors gracefully
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$v8 = new V8Js();
8+
9+
$v8->setModuleLoader(function() {
10+
trigger_error('some fatal error', E_USER_ERROR);
11+
});
12+
13+
$v8->executeString(' require("foo"); ');
14+
?>
15+
===EOF===
16+
--EXPECTF--
17+
Fatal error: some fatal error in %s%ecommonjs_fatal_error.php on line 5

v8js_methods.cc

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,34 @@ V8JS_METHOD(require)
257257
MAKE_STD_ZVAL(normalised_path_zend);
258258
ZVAL_STRING(normalised_path_zend, normalised_module_id, 1);
259259

260-
zval **params[1] = {&normalised_path_zend};
261-
if (FAILURE == call_user_function_ex(EG(function_table), NULL, c->module_loader, &module_code, 1, params, 0, NULL TSRMLS_CC)) {
260+
int call_result;
261+
262+
zend_try {
263+
{
264+
isolate->Exit();
265+
v8::Unlocker unlocker(isolate);
266+
267+
zval **params[1] = {&normalised_path_zend};
268+
call_result = call_user_function_ex(EG(function_table), NULL, c->module_loader, &module_code, 1, params, 0, NULL TSRMLS_CC);
269+
}
270+
271+
isolate->Enter();
272+
273+
if (call_result == FAILURE) {
274+
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module loader callback failed")));
275+
}
276+
}
277+
zend_catch {
278+
v8js_terminate_execution(isolate);
279+
V8JSG(fatal_error_abort) = 1;
280+
call_result = FAILURE;
281+
}
282+
zend_end_try();
283+
284+
if (call_result == FAILURE) {
262285
zval_ptr_dtor(&normalised_path_zend);
263286
efree(normalised_module_id);
264287
efree(normalised_path);
265-
266-
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module loader callback failed")));
267288
return;
268289
}
269290
zval_ptr_dtor(&normalised_path_zend);

0 commit comments

Comments
 (0)