From b7dde1b1db5a9d3909ccc3daf38b752e2977ad3b Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 23 Aug 2015 17:40:27 +0200 Subject: [PATCH] Handle thrown PHP objects, that are no exceptions --- tests/php_exceptions_005.phpt | 43 +++++++++++++++++++++++++++++++++++ tests/php_exceptions_006.phpt | 40 ++++++++++++++++++++++++++++++++ v8js_exceptions.cc | 6 ++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/php_exceptions_005.phpt create mode 100644 tests/php_exceptions_006.phpt diff --git a/tests/php_exceptions_005.phpt b/tests/php_exceptions_005.phpt new file mode 100644 index 0000000..94f3c0d --- /dev/null +++ b/tests/php_exceptions_005.phpt @@ -0,0 +1,43 @@ +--TEST-- +Test V8::executeString() : PHP Exception handling (JS throw PHP-exception) +--SKIPIF-- + +--FILE-- +foo = new \Foo(); + +$JS = <<< EOT +var ex = PHP.foo.getException(); +print("after getException\\n"); +throw ex; +print("after throw\\n"); +EOT; + +try { + $v8->executeString($JS, 'php_exceptions_005'); +} +catch(V8JsScriptException $e) { + echo "Got V8JsScriptException\n"; + var_dump($e->getMessage()); + var_dump($e->getPrevious()->getMessage()); +} +?> +===EOF=== +--EXPECTF-- +after getException +Got V8JsScriptException +string(329) "php_exceptions_005:3: exception 'Exception' with message 'Test-Exception' in %s +Stack trace: +#0 [internal function]: Foo->getException() +#1 %s: V8Js->executeString('var ex = PHP.fo...', 'php_exceptions_...') +#2 {main}" +string(14) "Test-Exception" +===EOF=== diff --git a/tests/php_exceptions_006.phpt b/tests/php_exceptions_006.phpt new file mode 100644 index 0000000..9ccc7d5 --- /dev/null +++ b/tests/php_exceptions_006.phpt @@ -0,0 +1,40 @@ +--TEST-- +Test V8::executeString() : PHP Exception handling (JS throws normal PHP-object) +--SKIPIF-- + +--FILE-- +foo = new \Foo(); + +$JS = <<< EOT +var ex = PHP.foo.getNonExceptionObject(); +print("after getNonExceptionObject\\n"); +throw ex; +print("after throw\\n"); +EOT; + +try { + $v8->executeString($JS, 'php_exceptions_006'); +} +catch(V8JsScriptException $e) { + echo "Got V8JsScriptException\n"; + var_dump($e->getMessage()); + // previous exception should be NULL, as it is *not* a php exception + var_dump($e->getPrevious()); +} +?> +===EOF=== +--EXPECTF-- +after getNonExceptionObject +Got V8JsScriptException +string(34) "php_exceptions_006:3: [object Foo]" +NULL +===EOF=== diff --git a/v8js_exceptions.cc b/v8js_exceptions.cc index c2e2379..0b5e6c7 100644 --- a/v8js_exceptions.cc +++ b/v8js_exceptions.cc @@ -89,7 +89,11 @@ void v8js_create_script_exception(zval *return_value, v8::Isolate *isolate, v8:: if(!php_ref.IsEmpty()) { assert(php_ref->IsExternal()); zval *php_exception = reinterpret_cast(v8::External::Cast(*php_ref)->Value()); - zend_exception_set_previous(return_value, php_exception TSRMLS_CC); + + zend_class_entry *exception_ce = zend_exception_get_default(TSRMLS_C); + if (Z_TYPE_P(php_exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(php_exception), exception_ce TSRMLS_CC)) { + zend_exception_set_previous(return_value, php_exception TSRMLS_CC); + } } }