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: https://github.com/nodejs/node/issues/6899 PR-URL: https://github.com/nodejs/node/pull/6907 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
4bd410bbbe
commit
dab09877b0
9
lib/internal/bootstrap_node.js
vendored
9
lib/internal/bootstrap_node.js
vendored
@ -357,6 +357,7 @@
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
this.exports = {};
|
this.exports = {};
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
|
this.loading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeModule._source = process.binding('natives');
|
NativeModule._source = process.binding('natives');
|
||||||
@ -368,7 +369,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
var cached = NativeModule.getCached(id);
|
var cached = NativeModule.getCached(id);
|
||||||
if (cached) {
|
if (cached && (cached.loaded || cached.loading)) {
|
||||||
return cached.exports;
|
return cached.exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -432,6 +433,9 @@
|
|||||||
var source = NativeModule.getSource(this.id);
|
var source = NativeModule.getSource(this.id);
|
||||||
source = NativeModule.wrap(source);
|
source = NativeModule.wrap(source);
|
||||||
|
|
||||||
|
this.loading = true;
|
||||||
|
|
||||||
|
try {
|
||||||
var fn = runInThisContext(source, {
|
var fn = runInThisContext(source, {
|
||||||
filename: this.filename,
|
filename: this.filename,
|
||||||
lineOffset: 0,
|
lineOffset: 0,
|
||||||
@ -440,6 +444,9 @@
|
|||||||
fn(this.exports, NativeModule.require, this, this.filename);
|
fn(this.exports, NativeModule.require, this, this.filename);
|
||||||
|
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
|
} finally {
|
||||||
|
this.loading = false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
NativeModule.prototype.cache = function() {
|
NativeModule.prototype.cache = function() {
|
||||||
|
22
test/message/console_low_stack_space.js
Normal file
22
test/message/console_low_stack_space.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
'use strict';
|
||||||
|
// copy console accessor because requiring ../common touches it
|
||||||
|
const consoleDescriptor = Object.getOwnPropertyDescriptor(global, 'console');
|
||||||
|
delete global.console;
|
||||||
|
global.console = {};
|
||||||
|
|
||||||
|
require('../common');
|
||||||
|
|
||||||
|
function a() {
|
||||||
|
try {
|
||||||
|
return a();
|
||||||
|
} catch (e) {
|
||||||
|
const console = consoleDescriptor.get();
|
||||||
|
if (console.log) {
|
||||||
|
console.log('Hello, World!');
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a();
|
1
test/message/console_low_stack_space.out
Normal file
1
test/message/console_low_stack_space.out
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello, World!
|
Loading…
x
Reference in New Issue
Block a user