Forráskód Böngészése

Handle exceptions thrown in JS generators well

Stefan Siegl 9 éve
szülő
commit
479d14b5b0
2 módosított fájl, 38 hozzáadás és 0 törlés
  1. 33 0
      tests/generators_from_v8_007.phpt
  2. 5 0
      v8js_v8object_class.cc

+ 33 - 0
tests/generators_from_v8_007.phpt

@@ -0,0 +1,33 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (throw JS)
+--SKIPIF--
+<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  yield 23;
+  throw new Error('blar');
+}
+
+TheGen();
+EOJS;
+
+$v8 = new V8Js();
+$gen = $v8->executeString($js);
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(23)
+
+Fatal error: Uncaught V8JsScriptException: V8Js::compileString():3: Error: blar in %s
+Stack trace:
+#0 %s: V8Generator->next()
+#1 {main}
+  thrown in %s

+ 5 - 0
v8js_v8object_class.cc

@@ -510,6 +510,11 @@ static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */
 
 			v8::Local<v8::Value> result = cb->Call(v8obj, 0, NULL);
 
+			if(result.IsEmpty()) {
+				/* cb->Call probably threw (and already threw a zend exception), just return */
+				return V8JS_NULL;
+			}
+
 			if(!result->IsObject()) {
 				zend_throw_exception(php_ce_v8js_exception,
 					"V8Generator returned non-object on next()", 0);