Browse Source

Use v8::TryCatch if calling V8Function, refs #127

Stefan Siegl 10 years ago
parent
commit
0023c03280
2 changed files with 33 additions and 0 deletions
  1. 25 0
      tests/issue_127_001.phpt
  2. 8 0
      v8js.cc

+ 25 - 0
tests/issue_127_001.phpt

@@ -0,0 +1,25 @@
+--TEST--
+Test V8Function::__call() : Check v8::TryCatch behaviour
+--SKIPIF--
+<?php
+if(!function_exists('json_encode')) {
+  die('SKIP');
+}
+require_once(dirname(__FILE__) . '/skipif.inc');
+?>
+--FILE--
+<?php
+$sandbox = new V8Js();
+
+$cb = $sandbox->executeString('(function() { return oh; });');
+
+try {
+    $cb();
+} catch(\Exception $e) {
+    echo "caught: ".$e->getMessage()."\n";
+}
+?>
+===EOF===
+--EXPECT--
+caught: V8Js::compileString():1: ReferenceError: oh is not defined
+===EOF===

+ 8 - 0
v8js.cc

@@ -486,6 +486,9 @@ static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /
 		jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(*argv[i], isolate TSRMLS_CC));
 	}
 
+	/* Catch JS exceptions */
+	v8::TryCatch try_catch;
+
 	js_retval = cb->Call(V8JS_GLOBAL(isolate), argc, jsArgv);
 
 	zval_ptr_dtor(&object);
@@ -494,6 +497,11 @@ static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /
 		efree(argv);
 	}
 
+	if (try_catch.HasCaught()) {
+		php_v8js_throw_script_exception(&try_catch TSRMLS_CC);
+		return FAILURE;
+	}
+
 	if (return_value_used) {
 		return v8js_to_zval(js_retval, return_value, obj->flags, isolate TSRMLS_CC);
 	}