From a8c0211e73371a6783e5fccc90b494c6cdc8b88c Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 29 Sep 2009 19:28:54 +0200 Subject: [PATCH] Bugfix: require() and include() should work in callbacks. Removing requireAsync and includeAsync from global scope for now as a temporary fix. Reported by Yuffster. --- doc/api.html | 3 +- doc/api.txt | 2 - doc/api.xml | 1 - doc/node.1 | 2 - src/node.js | 55 ++++++++++++++-------------- test/mjsunit/test-delayed-require.js | 11 ++++++ 6 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 test/mjsunit/test-delayed-require.js diff --git a/doc/api.html b/doc/api.html index 10de5657bdb..da792d2597f 100644 --- a/doc/api.html +++ b/doc/api.html @@ -535,7 +535,6 @@ variable (which should be a list of paths, colon separated).

Node comes with several libraries which are installed when "make install" is run. These are currently undocumented, but do look them up in your system.

-

(Functions require_async() and include_async() also exist.)

Timers

@@ -2030,7 +2029,7 @@ init (Handle<Object> target) diff --git a/doc/api.txt b/doc/api.txt index 1da911ad2c0..c84519f5c08 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -332,8 +332,6 @@ Node comes with several libraries which are installed when +"make install"+ is run. These are currently undocumented, but do look them up in your system. -(Functions +require_async()+ and +include_async()+ also exist.) - diff --git a/doc/api.xml b/doc/api.xml index e3b8399c5a9..9dc8793695a 100644 --- a/doc/api.xml +++ b/doc/api.xml @@ -554,7 +554,6 @@ variable (which should be a list of paths, colon separated). Node comes with several libraries which are installed when "make install" is run. These are currently undocumented, but do look them up in your system. -(Functions require_async() and include_async() also exist.) Timers diff --git a/doc/node.1 b/doc/node.1 index 286e156bc7b..6f123effcda 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -457,8 +457,6 @@ node\.libraryPaths can be modified at runtime by simply unshifting new paths on .sp Node comes with several libraries which are installed when "make install" is run\. These are currently undocumented, but do look them up in your system\. .sp -(Functions require_async() and include_async() also exist\.) -.sp .SS "Timers" .PP setTimeout(callback, delay) diff --git a/src/node.js b/src/node.js index bb55e3aec26..3da5f7942a6 100644 --- a/src/node.js +++ b/src/node.js @@ -62,32 +62,6 @@ if (ENV["NODE_LIBRARY_PATHS"]) { ENV["NODE_LIBRARY_PATHS"].split(":").concat(node.libraryPaths); } -node.loadingModules = []; - -function require_async (url) { - var currentModule = node.loadingModules[0]; - return currentModule.newChild(url, {}); -} - -function require (url) { - return require_async(url).wait(); -} - -function include_async (url) { - var promise = require_async(url) - promise.addCallback(function (t) { - // copy properties into global namespace. - for (var prop in t) { - if (t.hasOwnProperty(prop)) process[prop] = t[prop]; - } - }); - return promise; -} - -function include (url) { - include_async(url).wait(); -} - node.Module = function (filename, parent) { node.assert(filename.charAt(0) == "/"); this.filename = filename; @@ -223,12 +197,37 @@ node.Module.prototype.loadScript = function (loadPromise) { // remove shebang content = content.replace(/^\#\!.*/, ''); + node.loadingModules = []; + + function requireAsync (url) { + return self.newChild(url, {}); + } + + function require (url) { + return requireAsync(url).wait(); + } + + function includeAsync (url) { + var promise = requireAsync(url) + promise.addCallback(function (t) { + // copy properties into global namespace. + for (var prop in t) { + if (t.hasOwnProperty(prop)) process[prop] = t[prop]; + } + }); + return promise; + } + + function include (url) { + includeAsync(url).wait(); + } + // create wrapper function - var wrapper = "function (__filename, exports) { " + content + "\n};"; + var wrapper = "function (__filename, exports, require, include) { " + content + "\n};"; var compiled_wrapper = node.compile(wrapper, self.filename); node.loadingModules.unshift(self); - compiled_wrapper.apply(self.target, [self.filename, self.target]); + compiled_wrapper.apply(self.target, [self.filename, self.target, require, include]); node.loadingModules.shift(); self.waitChildrenLoad(function () { diff --git a/test/mjsunit/test-delayed-require.js b/test/mjsunit/test-delayed-require.js new file mode 100644 index 00000000000..1070516074e --- /dev/null +++ b/test/mjsunit/test-delayed-require.js @@ -0,0 +1,11 @@ +include("common.js"); + +setTimeout(function () { + a = require("fixtures/a.js"); +}, 50); + +process.addListener("exit", function () { + assertTrue("A" in a); + assertEquals("A", a.A()); + assertEquals("D", a.D()); +});