From c3df0b57beaa4c0df82f046862a73fb57884fcbe Mon Sep 17 00:00:00 2001 From: oroce Date: Thu, 5 Sep 2013 15:33:17 +0200 Subject: [PATCH] Set `module.parent` correctly inside sandboxed modules. --- lib/sandboxed_module.js | 4 ++-- test/fixture/parent.js | 1 + test/integration/test-parent.js | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 test/fixture/parent.js create mode 100644 test/integration/test-parent.js diff --git a/lib/sandboxed_module.js b/lib/sandboxed_module.js index e641cd5..1545cee 100644 --- a/lib/sandboxed_module.js +++ b/lib/sandboxed_module.js @@ -4,7 +4,7 @@ var Module = require('module'); var fs = require('fs'); var vm = require('vm'); var path = require('path'); - +var parent = module.parent; module.exports = SandboxedModule; function SandboxedModule() { this.id = null; @@ -45,7 +45,7 @@ SandboxedModule.prototype._init = function(moduleId, trace, options) { this._options = options || {}; - var module = new Module(this.filename); + var module = new Module(this.filename, parent); module.filename = this.filename; this.module = module; diff --git a/test/fixture/parent.js b/test/fixture/parent.js new file mode 100644 index 0000000..eb1963d --- /dev/null +++ b/test/fixture/parent.js @@ -0,0 +1 @@ +exports.myParent = module.parent; diff --git a/test/integration/test-parent.js b/test/integration/test-parent.js new file mode 100644 index 0000000..23164ce --- /dev/null +++ b/test/integration/test-parent.js @@ -0,0 +1,6 @@ +var assert = require('assert'); +var SandboxedModule = require('../..'); + +var fixture = SandboxedModule.require('../fixture/parent'); +assert.strictEqual(fixture.myParent.filename, module.filename); +assert.strictEqual(fixture.myParent, module);