Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: expose Module._runInThisContext #6288

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Intentionally empty
17 changes: 17 additions & 0 deletions test/parallel/test-module-run-in-this-context.js
Original file line number Diff line number Diff line change
@@ -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);