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

run exceptions thrown in require() through proxy factory as well

This commit is contained in:
Stefan Siegl 2022-05-31 15:58:47 +02:00
parent 456814703c
commit 7422ef2383
4 changed files with 71 additions and 18 deletions

View File

@ -0,0 +1,32 @@
--TEST--
Test V8::setExceptionProxyFactory() : Proxy handling on exception in setModuleLoader
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$v8 = new V8Js();
$v8->setModuleLoader(function ($path) {
throw new Error('moep');
});
$v8->setExceptionProxyFactory(function (Throwable $ex) {
echo "exception proxy factory called.\n";
return $ex->getMessage();
});
$v8->executeString('
try {
require("file");
} catch(e) {
var_dump(e);
}
', null, V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS);
?>
===EOF===
--EXPECT--
exception proxy factory called.
string(4) "moep"
===EOF===

View File

@ -0,0 +1,34 @@
--TEST--
Test V8::setExceptionProxyFactory() : Proxy handling on exception in setModuleNormaliser
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$v8 = new V8Js();
$v8->setModuleNormaliser(function ($path) {
throw new Error('blarg');
});
$v8->setModuleLoader(function ($path) {
throw new Error('moep');
});
$v8->setExceptionProxyFactory(function (Throwable $ex) {
echo "exception proxy factory called.\n";
return $ex->getMessage();
});
$v8->executeString('
try {
require("file");
} catch(e) {
var_dump(e);
}
', null, V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS);
?>
===EOF===
--EXPECT--
exception proxy factory called.
string(5) "blarg"
===EOF===

View File

@ -19,6 +19,7 @@
#include "php_v8js_macros.h"
#include "v8js_commonjs.h"
#include "v8js_exceptions.h"
#include "v8js_object_export.h"
extern "C" {
#include "zend_exceptions.h"
@ -337,14 +338,7 @@ V8JS_METHOD(require)
// Check if an exception was thrown
if (EG(exception)) {
if (c->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
zval tmp_zv;
ZVAL_OBJ(&tmp_zv, EG(exception));
info.GetReturnValue().Set(isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate)));
zend_clear_exception();
} else {
v8js_terminate_execution(isolate);
}
info.GetReturnValue().Set(v8js_propagate_exception(c));
return;
}
@ -466,15 +460,7 @@ V8JS_METHOD(require)
efree(normalised_module_id);
efree(normalised_path);
if (c->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
zval tmp_zv;
ZVAL_OBJ(&tmp_zv, EG(exception));
info.GetReturnValue().Set(isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate)));
zend_clear_exception();
} else {
v8js_terminate_execution(isolate);
}
info.GetReturnValue().Set(v8js_propagate_exception(c));
return;
}

View File

@ -15,6 +15,7 @@
#define V8JS_OBJECT_EXPORT_H
v8::Local<v8::Value> v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate);
v8::Local<v8::Value> v8js_propagate_exception(v8js_ctx *ctx);
typedef enum {