Skip to content

Commit 7ad588e

Browse files
committed
module: don't cache uninitialized builtins
Don't cache the exported values of fully uninitialized builtins. This works by adding an additional `loading` flag that is only active during initial loading of an internal module and checking that either the module is fully loaded or is in that state before using its cached value. This has the effect that builtins modules which could not be loaded (e.g. because compilation failed due to missing stack space) can be loaded at a later point. Fixes: nodejs#6899
1 parent 5116d98 commit 7ad588e

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lib/internal/bootstrap_node.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
this.id = id;
358358
this.exports = {};
359359
this.loaded = false;
360+
this.loading = false;
360361
}
361362

362363
NativeModule._source = process.binding('natives');
@@ -368,7 +369,7 @@
368369
}
369370

370371
var cached = NativeModule.getCached(id);
371-
if (cached) {
372+
if (cached && (cached.loaded || cached.loading)) {
372373
return cached.exports;
373374
}
374375

@@ -432,14 +433,20 @@
432433
var source = NativeModule.getSource(this.id);
433434
source = NativeModule.wrap(source);
434435

435-
var fn = runInThisContext(source, {
436-
filename: this.filename,
437-
lineOffset: 0,
438-
displayErrors: true
439-
});
440-
fn(this.exports, NativeModule.require, this, this.filename);
436+
this.loading = true;
437+
438+
try {
439+
var fn = runInThisContext(source, {
440+
filename: this.filename,
441+
lineOffset: 0,
442+
displayErrors: true
443+
});
444+
fn(this.exports, NativeModule.require, this, this.filename);
441445

442-
this.loaded = true;
446+
this.loaded = true;
447+
} finally {
448+
this.loading = false;
449+
}
443450
};
444451

445452
NativeModule.prototype.cache = function() {
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
// copy console accessor because requiring ../common touches it
3+
const consoleDescriptor = Object.getOwnPropertyDescriptor(global, 'console');
4+
delete global.console;
5+
global.console = {};
6+
7+
require('../common');
8+
9+
function a() {
10+
try {
11+
return a();
12+
} catch (e) {
13+
const console = consoleDescriptor.get();
14+
if (console.log) {
15+
console.log('Hello, World!');
16+
} else {
17+
throw e;
18+
}
19+
}
20+
}
21+
22+
a();
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, World!

0 commit comments

Comments
 (0)