From ba1f5b13e04e9c02a837aa92dc59a8509f5906ca Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Tue, 19 Apr 2016 17:50:36 -0400 Subject: [PATCH] module: expose `Module._runInThisContext` `Module._runInThisContext` can be overridden to store/load cached compilation artifacts when embedding node.js . --- lib/module.js | 6 +++++- test/fixtures/empty.js | 1 + .../parallel/test-module-run-in-this-context.js | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-module-run-in-this-context.js diff --git a/lib/module.js b/lib/module.js index 29a23776d790d2..6a63337545562f 100644 --- a/lib/module.js +++ b/lib/module.js @@ -461,6 +461,10 @@ Module.prototype.require = function(path) { var resolvedArgv; +// Overridable method, may be useful for caching compilation results +Module._runInThisContext = runInThisContext; + + // Run the file contents in the correct scope or sandbox. Expose // the correct helper variables (require, module, exports) to // the file. @@ -497,7 +501,7 @@ Module.prototype._compile = function(content, filename) { // create wrapper function var wrapper = Module.wrap(content); - var compiledWrapper = runInThisContext(wrapper, { + var compiledWrapper = Module._runInThisContext(wrapper, { filename: filename, lineOffset: 0, displayErrors: true diff --git a/test/fixtures/empty.js b/test/fixtures/empty.js index e69de29bb2d1d6..5885289003f0a4 100644 --- a/test/fixtures/empty.js +++ b/test/fixtures/empty.js @@ -0,0 +1 @@ +// Intentionally empty diff --git a/test/parallel/test-module-run-in-this-context.js b/test/parallel/test-module-run-in-this-context.js new file mode 100644 index 00000000000000..86ba8f41e210bd --- /dev/null +++ b/test/parallel/test-module-run-in-this-context.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const vm = require('vm'); +const path = require('path'); +const Module = require('module'); + +let counter = 0; + +Module._runInThisContext = function runInThisContext(source, options) { + counter++; + return vm.runInThisContext(source, options); +}; + +const empty = require(path.join(common.fixturesDir, 'empty.js')); + +assert.equal(counter, 1);