Pārlūkot izejas kodu

Fix handling of failed context creation, closes #136

Stefan Siegl 10 gadi atpakaļ
vecāks
revīzija
e67f1f4c9c
2 mainītis faili ar 29 papildinājumiem un 3 dzēšanām
  1. 25 0
      tests/extensions_error.phpt
  2. 4 3
      v8js_class.cc

+ 25 - 0
tests/extensions_error.phpt

@@ -0,0 +1,25 @@
+--TEST--
+Test V8::registerExtension() : Register extension with errors
+--SKIPIF--
+<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
+--FILE--
+<?php
+
+$handlebarsJs = "var root = typeof global !== 'undefined' ? global : window, \$Handlebars = 'test';";
+echo "-- registerExtension --\n";
+V8Js::registerExtension('handlebars', $handlebarsJs, [], true);
+echo "-- creating V8Js object --\n";
+$v8 = new V8Js();
+var_dump($v8);
+?>
+===EOF===
+--EXPECTF--
+-- registerExtension --
+-- creating V8Js object --
+Exception thrown during bootstrapping
+Extension or internal compilation error in handlebars at line 1.
+Error installing extension 'handlebars'.
+
+Warning: V8Js::__construct(): Failed to create V8 context. Check that registered extensions do not have errors. in %s on line %d
+NULL
+===EOF===

+ 4 - 3
v8js_class.cc

@@ -352,20 +352,21 @@ static PHP_METHOD(V8Js, __construct)
 
 	/* Create context */
 	v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, tpl->InstanceTemplate());
-	context->SetAlignedPointerInEmbedderData(1, c);
-	c->context.Reset(isolate, context);
 
 	if (exts) {
 		v8js_free_ext_strarr(exts, exts_count);
 	}
 
 	/* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
-	if (c->context.IsEmpty()) {
+	if (context.IsEmpty()) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 context. Check that registered extensions do not have errors.");
 		ZVAL_NULL(getThis());
 		return;
 	}
 
+	context->SetAlignedPointerInEmbedderData(1, c);
+	c->context.Reset(isolate, context);
+
 	/* Enter context */
 	v8::Context::Scope context_scope(context);