0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-12-22 14:01:53 +00:00

introduce setExceptionProxyFactory

This commit is contained in:
Stefan Siegl 2022-05-31 14:21:32 +02:00
parent acd9431ec9
commit ac2b1cb238
4 changed files with 109 additions and 8 deletions

View File

@ -0,0 +1,55 @@
--TEST--
Test V8::setExceptionProxyFactory() : Simple test
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
class myv8 extends V8Js
{
public function throwException(string $message) {
throw new Exception($message);
}
}
class ExceptionProxy {
private $ex;
public function __construct(Throwable $ex) {
echo "ExceptionProxy::__construct called!\n";
var_dump($ex->getMessage());
$this->ex = $ex;
}
public function getMessage() {
echo "getMessage called\n";
return $this->ex->getMessage();
}
}
$v8 = new myv8();
$v8->setExceptionProxyFactory(function (Throwable $ex) {
echo "exception proxy factory called.\n";
return new ExceptionProxy($ex);
});
$v8->executeString('
try {
PHP.throwException("Oops");
}
catch (e) {
var_dump(e.getMessage()); // calls ExceptionProxy::getMessage
var_dump(typeof e.getTrace);
}
', null, V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS);
?>
===EOF===
--EXPECT--
exception proxy factory called.
ExceptionProxy::__construct called!
string(4) "Oops"
getMessage called
string(4) "Oops"
string(9) "undefined"
===EOF===

View File

@ -92,6 +92,7 @@ static void v8js_free_storage(zend_object *object) /* {{{ */
zval_ptr_dtor(&c->pending_exception);
zval_ptr_dtor(&c->module_normaliser);
zval_ptr_dtor(&c->module_loader);
zval_ptr_dtor(&c->exception_proxy_factory);
/* Delete PHP global object from JavaScript */
if (!c->context.IsEmpty()) {
@ -400,6 +401,7 @@ static PHP_METHOD(V8Js, __construct)
ZVAL_NULL(&c->module_normaliser);
ZVAL_NULL(&c->module_loader);
ZVAL_NULL(&c->exception_proxy_factory);
/* Include extensions used by this context */
/* Note: Extensions registered with auto_enable do not need to be added separately like this. */
@ -880,6 +882,21 @@ static PHP_METHOD(V8Js, setModuleLoader)
}
/* }}} */
/* {{{ proto void V8Js::setExceptionProxyFactory(callable factory)
*/
static PHP_METHOD(V8Js, setExceptionProxyFactory)
{
zval *callable;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
return;
}
v8js_ctx *c = Z_V8JS_CTX_OBJ_P(getThis());
ZVAL_COPY(&c->exception_proxy_factory, callable);
}
/* }}} */
/* {{{ proto void V8Js::setTimeLimit(int time_limit)
*/
static PHP_METHOD(V8Js, setTimeLimit)
@ -1254,6 +1271,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
ZEND_ARG_INFO(0, callable)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setexceptionproxyfactory, 0, 0, 1)
ZEND_ARG_INFO(0, callable)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setaverageobjectsize, 0, 0, 1)
ZEND_ARG_INFO(0, average_object_size)
ZEND_END_ARG_INFO()
@ -1293,6 +1314,7 @@ const zend_function_entry v8js_methods[] = { /* {{{ */
PHP_ME(V8Js, clearPendingException, arginfo_v8js_clearpendingexception, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
PHP_ME(V8Js, setModuleNormaliser, arginfo_v8js_setmodulenormaliser, ZEND_ACC_PUBLIC)
PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
PHP_ME(V8Js, setExceptionProxyFactory, arginfo_v8js_setexceptionproxyfactory, ZEND_ACC_PUBLIC)
PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC)
PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC)
PHP_ME(V8Js, setAverageObjectSize, arginfo_v8js_setaverageobjectsize, ZEND_ACC_PUBLIC)

View File

@ -55,6 +55,7 @@ struct v8js_ctx {
zval module_normaliser;
zval module_loader;
zval exception_proxy_factory;
std::vector<char *> modules_stack;
std::map<char *, v8js_persistent_value_t, cmp_str> modules_loaded;

View File

@ -34,6 +34,36 @@ extern "C" {
static void v8js_weak_object_callback(const v8::WeakCallbackInfo<zend_object> &data);
v8::Local<v8::Value> v8js_propagate_exception(v8js_ctx *ctx) /* {{{ */
{
v8::Local<v8::Value> return_value = v8::Null(ctx->isolate);
if (!(ctx->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS)) {
v8js_terminate_execution(ctx->isolate);
return return_value;
}
zval tmp_zv;
if (Z_TYPE(ctx->exception_proxy_factory) != IS_NULL) {
zval params[1];
ZVAL_OBJ(&params[0], EG(exception));
Z_ADDREF_P(&params[0]);
zend_clear_exception();
call_user_function(EG(function_table), NULL, &ctx->exception_proxy_factory, &tmp_zv, 1, params);
zval_ptr_dtor(&params[0]);
return_value = ctx->isolate->ThrowException(zval_to_v8js(&tmp_zv, ctx->isolate));
} else {
ZVAL_OBJ(&tmp_zv, EG(exception));
return_value = ctx->isolate->ThrowException(zval_to_v8js(&tmp_zv, ctx->isolate));
zend_clear_exception();
}
return return_value;
}
/* }}} */
/* Callback for PHP methods and functions */
static void v8js_call_php_func(zend_object *object, zend_function *method_ptr, const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
@ -175,14 +205,7 @@ failure:
}
if(EG(exception)) {
if(ctx->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
zval tmp_zv;
ZVAL_OBJ(&tmp_zv, EG(exception));
return_value = isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate));
zend_clear_exception();
} else {
v8js_terminate_execution(isolate);
}
return_value = v8js_propagate_exception(ctx);
} else if (Z_TYPE(retval) == IS_OBJECT && Z_OBJ(retval) == object) {
// special case: "return $this"
return_value = info.Holder();