Browse Source

use "this = module.exports" for modules

Stefan Siegl 7 years ago
parent
commit
384ec9b1b6
3 changed files with 30 additions and 2 deletions
  1. 28 0
      tests/commonjs_node_compat_001.phpt
  2. 1 1
      tests/commonjs_node_compat_basic.phpt
  3. 1 1
      v8js_methods.cc

+ 28 - 0
tests/commonjs_node_compat_001.phpt

@@ -0,0 +1,28 @@
+--TEST--
+Test V8Js::setModuleLoader : this === module.exports
+--SKIPIF--
+<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
+--FILE--
+<?php
+
+$v8 = new V8Js();
+
+$v8->setModuleLoader(function ($moduleName) {
+    return <<<'EOJS'
+        var_dump(this === global);
+        var_dump(this === module.exports);
+EOJS
+    ;
+});
+
+$v8->executeString(<<<'EOJS'
+    var result = require('foo');
+EOJS
+);
+
+?>
+===EOF===
+--EXPECT--
+bool(false)
+bool(true)
+===EOF===

+ 1 - 1
tests/commonjs_node_compat_basic.phpt

@@ -1,5 +1,5 @@
 --TEST--
-Test V8::executeString() : exports/module.exports behaviour
+Test V8Js::setModuleLoader : exports/module.exports behaviour
 --SKIPIF--
 <?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
 --FILE--

+ 1 - 1
v8js_methods.cc

@@ -462,7 +462,7 @@ V8JS_METHOD(require)
 		jsArgv[1] = module;
 
 		// actually call the module
-		v8::Local<v8::Function>::Cast(module_function)->Call(V8JS_GLOBAL(isolate), 2, jsArgv);
+		v8::Local<v8::Function>::Cast(module_function)->Call(exports, 2, jsArgv);
 	}
 
 	// Remove this module and path from the stack