Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Bugfix: require() and include() should work in callbacks.
Browse files Browse the repository at this point in the history
Removing requireAsync and includeAsync from global scope for now as a
temporary fix.  Reported by Yuffster.
  • Loading branch information
ry committed Sep 29, 2009
1 parent 1ae69a6 commit a8c0211
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
3 changes: 1 addition & 2 deletions doc/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ <h3 id="_modules">Modules</h3><div style="clear:left"></div>
<div class="paragraph"><p>Node comes with several libraries which are installed when <tt>"make install"</tt>
is run. These are currently undocumented, but do look them up in your
system.</p></div>
<div class="paragraph"><p>(Functions <tt>require_async()</tt> and <tt>include_async()</tt> also exist.)</p></div>
<h3 id="_timers">Timers</h3><div style="clear:left"></div>
<div class="dlist"><dl>
<dt class="hdlist1">
Expand Down Expand Up @@ -2030,7 +2029,7 @@ <h2 id="_extension_api">Extension API</h2>
<div id="footer">
<div id="footer-text">
Version 0.1.12<br />
Last updated 2009-09-29 09:59:52 CEST
Last updated 2009-09-29 19:22:27 CEST
</div>
</div>
</body>
Expand Down
2 changes: 0 additions & 2 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.)




Expand Down
1 change: 0 additions & 1 deletion doc/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ variable (which should be a list of paths, colon separated).</simpara>
<simpara>Node comes with several libraries which are installed when <literal>"make install"</literal>
is run. These are currently undocumented, but do look them up in your
system.</simpara>
<simpara>(Functions <literal>require_async()</literal> and <literal>include_async()</literal> also exist.)</simpara>
</refsect2>
<refsect2 id="_timers">
<title>Timers</title>
Expand Down
2 changes: 0 additions & 2 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 27 additions & 28 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down
11 changes: 11 additions & 0 deletions test/mjsunit/test-delayed-require.js
Original file line number Diff line number Diff line change
@@ -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());
});

0 comments on commit a8c0211

Please sign in to comment.