on master help sign in
Merge pull request #62 from replit/fm-fix-globals-cross-modules

Fix cross module node globals

commite652058ccf983d99f2f7e719b2320b4b1b99bc66 authorFaris Masad <farismasad@gmail.com> dateWed 27 Jan 2021, 15:11 (6 years ago) parent2c6459f82bec40
9 files changed+62−54
languages/nodejs/repl.js+9−30 run_tests+1−1 test_files/global_bug.js+7−2 test_files/global_require.js+4 test_files/module_patching.js+7 tests/nodejs/err.exp+7−7 tests/nodejs/global_bug.exp+5−5 tests/nodejs/module_patching.exp+10 tests/nodejs/run_file.exp+12−9
languages/nodejs/repl.js+9−30
@@ -9,7 +9,6 @@ const rl = require(path.join(
99 "nodejs",
1010 "readline-sync.js"
1111));
12−const tty = require("tty");
1312const Module = require("module");
1413
1514let r;
@@ -74,8 +73,8 @@ function handleError(e) {
7473function start(context) {
7574 r = repl.start({
7675 prompt: process.env.PRYBAR_PS1,
76+ useGlobal: true,
7777 });
78− if (context) r.context = context;
7978
8079 // remove the internal error and ours for red etc.
8180 r._domain.removeListener("error", r._domain.listeners("error")[0]);
@@ -133,35 +132,15 @@ if (process.env.PRYBAR_CODE) {
133132 module.paths = Module._nodeModulePaths(path.dirname(mainPath));
134133
135134 process.mainModule = module;
136− const sandbox = {
137− console,
138− module,
139− require: module.require.bind(module),
140− __dirname: path.dirname(mainPath),
141− __filename: mainPath,
142− };
135+
136+ global.module = module;
137+ global.require = module.require.bind(module);
138+ global.__dirname = path.dirname(mainPath);
139+ global.__filename = mainPath;
143140
144141 console.log(
145142 "\u001b[0m\u001b[90mHint: hit control+c anytime to enter REPL.\u001b[0m"
146143 );
147− const context = vm.createContext(sandbox);
148−
149− // Adapted from https://bit.ly/2Fc4WjM
150− const globalBuiltins = new Set(
151− vm.runInContext("Object.getOwnPropertyNames(globalThis)", context)
152− );
153−
154− for (const name of Object.getOwnPropertyNames(global)) {
155− // Only set properties that do not already exist as a global builtin.
156− if (!globalBuiltins.has(name)) {
157− Object.defineProperty(
158− context,
159− name,
160− Object.getOwnPropertyDescriptor(global, name)
161− );
162− }
163− }
164− context.global = context;
165144
166145 let script;
167146 try {
@@ -176,7 +155,7 @@ if (process.env.PRYBAR_CODE) {
176155 if (script) {
177156 let res;
178157 try {
179− res = script.runInContext(context, {
158+ res = script.runInThisContext({
180159 displayErrors: false,
181160 });
182161 } catch (e) {
@@ -191,8 +170,8 @@ if (process.env.PRYBAR_CODE) {
191170 }
192171
193172 if (process.env.PRYBAR_INTERACTIVE) {
194− process.once("SIGINT", () => start(context));
195− process.once("beforeExit", () => start(context));
173+ process.once("SIGINT", () => start());
174+ process.once("beforeExit", () => start());
196175 }
197176} else if (process.env.PRYBAR_INTERACTIVE) {
198177 start();
run_tests+1−1
@@ -1,7 +1,7 @@
11#!/usr/bin/env bash
22
33stty cols 80
4−stty row 20
4+stty rows 20
55
66tests=$(find ./tests -printf '%P\n')
77
test_files/global_bug.js+7−2
@@ -1,4 +1,5 @@
1−console.log([] instanceof global.Array);
1+console.assert([] instanceof global.Array);
2+console.assert([] instanceof Array);
23console.assert(process);
34console.assert(URL);
45console.assert(URLSearchParams);
@@ -9,4 +10,8 @@ console.assert(setImmediate);
910console.assert(setInterval);
1011console.assert(setTimeout);
1112console.assert(Buffer);
12−console.assert(globalThis === global);
13+console.assert(globalThis === global);
14+
15+const { Object: importedObject, global: importedGlobal} = require('./global_require')
16+console.assert(importedObject === Object);
17+console.assert(global === importedGlobal)
test_files/global_require.jsnew file+4
@@ -0,0 +1,4 @@
1+module.exports = {
2+ global,
3+ Object,
4+}
test_files/module_patching.jsnew file+7
@@ -0,0 +1,7 @@
1+console.assert(module.id === '.')
2+console.assert(Object.keys(module.exports).length === 0)
3+console.assert(module.parent === null)
4+console.assert(module.loaded === false)
5+console.assert(module.children.length === 0)
6+console.assert(module.filename === __filename)
7+console.assert(module.filename.endsWith('/test_files/module_patching.js'));
tests/nodejs/err.exp+7−7
@@ -6,16 +6,16 @@ match_max 100000
66expect -exact "\[1G\[0J--> \[5G"
77send -- "x\r"
88expect -exact "x\r\r
9−\u001b\[0m\u001b\[31mReferenceError: x is not defined\r
10− at repl:1:1\r
11− at Script.runInContext (vm.js:130:18)\r
12− at REPLServer.defaultEval (repl.js:435:29)\r
9+\[0m\[31mReferenceError: x is not defined\r
10+ at REPL1:1:1\r
11+ at Script.runInThisContext (vm.js:120:18)\r
12+ at REPLServer.defaultEval (repl.js:442:29)\r
1313 at bound (domain.js:427:14)\r
1414 at REPLServer.runBound \[as eval\] (domain.js:440:12)\r
15− at REPLServer.onLine (repl.js:760:10)\r
16− at REPLServer.emit (events.js:315:20)\r
15+ at REPLServer.onLine (repl.js:777:10)\r
16+ at REPLServer.emit (events.js:314:20)\r
1717 at REPLServer.EventEmitter.emit (domain.js:483:12)\r
1818 at REPLServer.Interface._onLine (readline.js:329:10)\r
19− at REPLServer.Interface._line (readline.js:658:8)\u001b\[0m\u001b\[1G\u001b\[0J--> \u001b\[5G"
19+ at REPLServer.Interface._line (readline.js:658:8)\[0m\[1G\[0J--> \[5G"
2020send -- ""
2121expect eof
tests/nodejs/global_bug.exp+5−5
@@ -1,10 +1,10 @@
11#!/usr/bin/expect -f
22
33set timeout -1
4−spawn ./prybar-nodejs ./test_files/global_bug.js
4+spawn ./prybar-nodejs -i ./test_files/global_bug.js
55match_max 100000
6−expect -exact "Node v12.18.3 on linux\r
7−\u001b\[0m\u001b\[90mHint: hit control+c anytime to enter REPL.\[0m\r
8−\u001b\[33mtrue\u001b\[39m\r
9−"
6+expect -re {Node v\d+\.\d+.\d+ on linux}
7+expect -exact "\[0m\[90mHint: hit control+c anytime to enter REPL.\[0m\r
8+\[1G\[0J--> \[5G"
9+send -- ""
1010expect eof
tests/nodejs/module_patching.expnew file+10
@@ -0,0 +1,10 @@
1+#!/usr/bin/expect -f
2+
3+set timeout -1
4+spawn ./prybar-nodejs -i ./test_files/global_bug.js
5+match_max 100000
6+expect -re {Node v\d+\.\d+.\d+ on linux}
7+expect -exact "\[0m\[90mHint: hit control+c anytime to enter REPL.\[0m\r
8+\[1G\[0J--> \[5G"
9+send -- ""
10+expect eof
tests/nodejs/run_file.exp+12−9
@@ -1,16 +1,19 @@
1−#!/usr/bin/expect -d
1+#!/usr/bin/expect -f
22
33set timeout -1
44spawn ./prybar-nodejs -i ./test_files/hello.js
55match_max 100000
6−expect -exact "Node v12.18.3 on linux\r
7−\u001b\[0m\u001b\[90mHint: hit control+c anytime to enter REPL.\[0m\r
6+expect -re {Node v\d+\.\d+.\d+ on linux}
7+expect -exact "\[0m\[90mHint: hit control+c anytime to enter REPL.\[0m\r
88hello\r
9−\u001b\[32m'hello'\u001b\[39m\r
10−\u001b\[1G\u001b\[0J--> \u001b\[5G"
11−send -- "a\r"
12−expect -exact "a\r\r
13−\u001b\[32m'hello'\u001b\[39m\r
14−\u001b\[1G\u001b\[0J--> \u001b\[5G"
9+\[32m'hello'\[39m\r
10+\[1G\[0J--> \[5G"
11+send -- "a"
12+expect -exact "\r
13+\[90m'hello'\[39m\[6G\[1A"
14+send -- "\r"
15+expect -exact "\[1B\[2K\[1A\r\r
16+\[32m'hello'\[39m\r
17+\[1G\[0J--> \[5G"
1518send -- ""
1619expect eof