From ca38f724c8e4efc26d4aeef175109c6f1927dcda Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Tue, 31 May 2022 16:07:57 +0200 Subject: [PATCH] handle exceptions thrown in proxy factory --- tests/exception_proxy_004.phpt | 37 ++++++++++++++++++++++++++++++++++ v8js_object_export.cc | 7 ++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/exception_proxy_004.phpt diff --git a/tests/exception_proxy_004.phpt b/tests/exception_proxy_004.phpt new file mode 100644 index 0000000..dfa4287 --- /dev/null +++ b/tests/exception_proxy_004.phpt @@ -0,0 +1,37 @@ +--TEST-- +Test V8::setExceptionProxyFactory() : Proxy handling on exception in factory +--SKIPIF-- + +--FILE-- +setExceptionProxyFactory(function (Throwable $ex) { + throw new Exception('moep'); +}); + +try { + $v8->executeString(' + try { + PHP.throwException("Oops"); + print("done\\n"); + } + catch (e) { + print("caught\\n"); + var_dump(e); + } + ', null, V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS); +} catch (Exception $ex) { + echo "caught in php: " . $ex->getMessage() . PHP_EOL; +} +?> +===EOF=== +--EXPECT-- +caught in php: moep +===EOF=== diff --git a/v8js_object_export.cc b/v8js_object_export.cc index d78fd51..e6e8354 100644 --- a/v8js_object_export.cc +++ b/v8js_object_export.cc @@ -53,7 +53,12 @@ v8::Local v8js_propagate_exception(v8js_ctx *ctx) /* {{{ */ call_user_function(EG(function_table), NULL, &ctx->exception_proxy_factory, &tmp_zv, 1, params); zval_ptr_dtor(¶ms[0]); - return_value = ctx->isolate->ThrowException(zval_to_v8js(&tmp_zv, ctx->isolate)); + if(EG(exception)) { + // exception proxy threw exception itself, don't forward, just stop execution. + v8js_terminate_execution(ctx->isolate); + } else { + 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));