소스 검색

Add Node.js-style "global" to global scope

Stefan Siegl 8 년 전
부모
커밋
e4ab07de03
2개의 변경된 파일25개의 추가작업 그리고 4개의 파일을 삭제
  1. 20 0
      tests/global_object_basic.phpt
  2. 5 4
      v8js_class.cc

+ 20 - 0
tests/global_object_basic.phpt

@@ -0,0 +1,20 @@
+--TEST--
+Test V8Js::executeString : Global scope links global object
+--SKIPIF--
+<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
+--FILE--
+<?php
+
+$JS = <<< EOT
+var_dump(typeof global);
+var_dump(global.var_dump === var_dump);
+EOT;
+
+$v8 = new V8Js();
+$v8->executeString($JS);
+?>
+===EOF===
+--EXPECT--
+string(6) "object"
+bool(true)
+===EOF===

+ 5 - 4
v8js_class.cc

@@ -425,14 +425,14 @@ static PHP_METHOD(V8Js, __construct)
 	/* Create global template for global object */
 	// Now we are using multiple isolates this needs to be created for every context
 
-	v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New(c->isolate);
-	c->global_template.Reset(isolate, tpl);
+	v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(c->isolate);
+	c->global_template.Reset(isolate, global_template);
 
 	/* Register builtin methods */
-	v8js_register_methods(tpl, c);
+	v8js_register_methods(global_template, c);
 
 	/* Create context */
-	v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, tpl);
+	v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, global_template);
 
 	if (exts) {
 		v8js_free_ext_strarr(exts, exts_count);
@@ -447,6 +447,7 @@ static PHP_METHOD(V8Js, __construct)
 	}
 
 	context->SetAlignedPointerInEmbedderData(1, c);
+	context->Global()->Set(context, V8JS_SYM("global"), context->Global());
 	c->context.Reset(isolate, context);
 
 	/* Enter context */