Skip to content

Commit a8c0211

Browse files
committed
Bugfix: require() and include() should work in callbacks.
Removing requireAsync and includeAsync from global scope for now as a temporary fix. Reported by Yuffster.
1 parent 1ae69a6 commit a8c0211

6 files changed

Lines changed: 39 additions & 35 deletions

File tree

doc/api.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ <h3 id="_modules">Modules</h3><div style="clear:left"></div>
535535
<div class="paragraph"><p>Node comes with several libraries which are installed when <tt>"make install"</tt>
536536
is run. These are currently undocumented, but do look them up in your
537537
system.</p></div>
538-
<div class="paragraph"><p>(Functions <tt>require_async()</tt> and <tt>include_async()</tt> also exist.)</p></div>
539538
<h3 id="_timers">Timers</h3><div style="clear:left"></div>
540539
<div class="dlist"><dl>
541540
<dt class="hdlist1">
@@ -2030,7 +2029,7 @@ <h2 id="_extension_api">Extension API</h2>
20302029
<div id="footer">
20312030
<div id="footer-text">
20322031
Version 0.1.12<br />
2033-
Last updated 2009-09-29 09:59:52 CEST
2032+
Last updated 2009-09-29 19:22:27 CEST
20342033
</div>
20352034
</div>
20362035
</body>

doc/api.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,6 @@ Node comes with several libraries which are installed when +"make install"+
332332
is run. These are currently undocumented, but do look them up in your
333333
system.
334334

335-
(Functions +require_async()+ and +include_async()+ also exist.)
336-
337335

338336

339337

doc/api.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ variable (which should be a list of paths, colon separated).</simpara>
554554
<simpara>Node comes with several libraries which are installed when <literal>"make install"</literal>
555555
is run. These are currently undocumented, but do look them up in your
556556
system.</simpara>
557-
<simpara>(Functions <literal>require_async()</literal> and <literal>include_async()</literal> also exist.)</simpara>
558557
</refsect2>
559558
<refsect2 id="_timers">
560559
<title>Timers</title>

doc/node.1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,6 @@ node\.libraryPaths can be modified at runtime by simply unshifting new paths on
457457
.sp
458458
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\.
459459
.sp
460-
(Functions require_async() and include_async() also exist\.)
461-
.sp
462460
.SS "Timers"
463461
.PP
464462
setTimeout(callback, delay)

src/node.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,6 @@ if (ENV["NODE_LIBRARY_PATHS"]) {
6262
ENV["NODE_LIBRARY_PATHS"].split(":").concat(node.libraryPaths);
6363
}
6464

65-
node.loadingModules = [];
66-
67-
function require_async (url) {
68-
var currentModule = node.loadingModules[0];
69-
return currentModule.newChild(url, {});
70-
}
71-
72-
function require (url) {
73-
return require_async(url).wait();
74-
}
75-
76-
function include_async (url) {
77-
var promise = require_async(url)
78-
promise.addCallback(function (t) {
79-
// copy properties into global namespace.
80-
for (var prop in t) {
81-
if (t.hasOwnProperty(prop)) process[prop] = t[prop];
82-
}
83-
});
84-
return promise;
85-
}
86-
87-
function include (url) {
88-
include_async(url).wait();
89-
}
90-
9165
node.Module = function (filename, parent) {
9266
node.assert(filename.charAt(0) == "/");
9367
this.filename = filename;
@@ -223,12 +197,37 @@ node.Module.prototype.loadScript = function (loadPromise) {
223197
// remove shebang
224198
content = content.replace(/^\#\!.*/, '');
225199

200+
node.loadingModules = [];
201+
202+
function requireAsync (url) {
203+
return self.newChild(url, {});
204+
}
205+
206+
function require (url) {
207+
return requireAsync(url).wait();
208+
}
209+
210+
function includeAsync (url) {
211+
var promise = requireAsync(url)
212+
promise.addCallback(function (t) {
213+
// copy properties into global namespace.
214+
for (var prop in t) {
215+
if (t.hasOwnProperty(prop)) process[prop] = t[prop];
216+
}
217+
});
218+
return promise;
219+
}
220+
221+
function include (url) {
222+
includeAsync(url).wait();
223+
}
224+
226225
// create wrapper function
227-
var wrapper = "function (__filename, exports) { " + content + "\n};";
226+
var wrapper = "function (__filename, exports, require, include) { " + content + "\n};";
228227
var compiled_wrapper = node.compile(wrapper, self.filename);
229228

230229
node.loadingModules.unshift(self);
231-
compiled_wrapper.apply(self.target, [self.filename, self.target]);
230+
compiled_wrapper.apply(self.target, [self.filename, self.target, require, include]);
232231
node.loadingModules.shift();
233232

234233
self.waitChildrenLoad(function () {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
include("common.js");
2+
3+
setTimeout(function () {
4+
a = require("fixtures/a.js");
5+
}, 50);
6+
7+
process.addListener("exit", function () {
8+
assertTrue("A" in a);
9+
assertEquals("A", a.A());
10+
assertEquals("D", a.D());
11+
});

0 commit comments

Comments
 (0)