Skip to content

Commit 5782ec2

Browse files
addaleaxMyles Borins
authored and
Myles Borins
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: #6899 Ref: #6899 PR-URL: #6907 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent b9dfdfe commit 5782ec2

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/node.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@
877877
this.id = id;
878878
this.exports = {};
879879
this.loaded = false;
880+
this.loading = true;
880881
}
881882

882883
NativeModule._source = process.binding('natives');
@@ -888,7 +889,7 @@
888889
}
889890

890891
var cached = NativeModule.getCached(id);
891-
if (cached) {
892+
if (cached && (cached.loaded || cached.loading)) {
892893
return cached.exports;
893894
}
894895

@@ -952,13 +953,18 @@
952953
var source = NativeModule.getSource(this.id);
953954
source = NativeModule.wrap(source);
954955

955-
var fn = runInThisContext(source, {
956-
filename: this.filename,
957-
lineOffset: 0
958-
});
959-
fn(this.exports, NativeModule.require, this, this.filename);
956+
this.loading = true;
957+
try {
958+
var fn = runInThisContext(source, {
959+
filename: this.filename,
960+
lineOffset: 0
961+
});
962+
fn(this.exports, NativeModule.require, this, this.filename);
960963

961-
this.loaded = true;
964+
this.loaded = true;
965+
} finally {
966+
this.loading = false;
967+
}
962968
};
963969

964970
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)