0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-11-09 15:18:41 +00:00

Add v8js.compat_php_exceptions INI switch

This commit is contained in:
Stefan Siegl 2015-09-23 19:19:11 +02:00
parent 5f6d9aee2d
commit 8934db6dec
4 changed files with 53 additions and 1 deletions

View File

@ -121,6 +121,7 @@ ZEND_BEGIN_MODULE_GLOBALS(v8js)
char *v8_flags; /* V8 command line flags */
bool use_date; /* Generate JS Date objects instead of PHP DateTime */
bool use_array_access; /* Convert ArrayAccess, Countable objects to array-like objects */
bool compat_php_exceptions; /* Don't stop JS execution on PHP exception */
// Timer thread globals
std::deque<v8js_timer_ctx *> timer_stack;

33
tests/issue_156_001.phpt Normal file
View File

@ -0,0 +1,33 @@
--TEST--
Test V8::executeString() : Backwards compatibility for issue #156
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.compat_php_exceptions = 1
--FILE--
<?php
$v8 = new V8Js();
$v8->throwPHPException = function () {
echo "throwing PHP exception now ...\n";
throw new \Exception('foo');
};
$JS = <<< EOT
PHP.throwPHPException();
print("... old behaviour was to not stop JS execution on PHP exceptions\\n");
EOT;
try {
$v8->executeString($JS, 'issue_156_001.js');
} catch(Exception $e) {
var_dump($e->getMessage());
}
?>
===EOF===
--EXPECT--
throwing PHP exception now ...
... old behaviour was to not stop JS execution on PHP exceptions
string(3) "foo"
===EOF===

18
v8js.cc
View File

@ -82,10 +82,28 @@ static ZEND_INI_MH(v8js_OnUpdateUseArrayAccess) /* {{{ */
}
/* }}} */
static ZEND_INI_MH(v8js_OnUpdateCompatExceptions) /* {{{ */
{
bool value;
if (new_value_length==2 && strcasecmp("on", new_value)==0) {
value = (bool) 1;
} else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
value = (bool) 1;
} else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
value = (bool) 1;
} else {
value = (bool) atoi(new_value);
}
V8JSG(compat_php_exceptions) = value;
return SUCCESS;
}
/* }}} */
ZEND_INI_BEGIN() /* {{{ */
ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
ZEND_INI_ENTRY("v8js.use_array_access", "0", ZEND_INI_ALL, v8js_OnUpdateUseArrayAccess)
ZEND_INI_ENTRY("v8js.compat_php_exceptions", "0", ZEND_INI_ALL, v8js_OnUpdateCompatExceptions)
ZEND_INI_END()
/* }}} */

View File

@ -149,7 +149,7 @@ failure:
efree(fci.params);
}
if(EG(exception)) {
if(EG(exception) && !V8JSG(compat_php_exceptions)) {
if(ctx->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
return_value = isolate->ThrowException(zval_to_v8js(EG(exception), isolate TSRMLS_CC));
zend_clear_exception(TSRMLS_C);