on master help sign in
proper globals support
commit19f84e1aa354535d5d6499d105f75e65f05bcb04 authorAmjad Masad <amjad.masad@gmail.com> dateMon 7 Sep 2020, 23:31 (6 years ago) parent83abc21
1 file changed+13−9
languages/nodejs/repl.js+13−9
languages/nodejs/repl.js+13−9
@@ -134,6 +134,7 @@ if (process.env.PRYBAR_CODE) {
134134
135135 process.mainModule = module;
136136 const sandbox = {
137+ console,
137138 module,
138139 require: module.require.bind(module),
139140 __dirname: path.dirname(mainPath),
@@ -145,20 +146,23 @@ if (process.env.PRYBAR_CODE) {
145146 root: global,
146147 };
147148
148− // These properties will show a warning. We can avoid them.
149− for (const prop of Object.getOwnPropertyNames(global)) {
150− if (sandbox.hasOwnProperty(prop)) {
151− continue;
152− }
153−
154− sandbox[prop] = global[prop];
155− }
156−
157149 console.log(
158150 "\u001b[0m\u001b[90mHint: hit control+c anytime to enter REPL.\u001b[0m"
159151 );
160152 const context = vm.createContext(sandbox);
161153
154+ // Adapted from https://bit.ly/2Fc4WjM
155+ const globalBuiltins =
156+ new Set(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)'));
157+ for (const name of Object.getOwnPropertyNames(global)) {
158+ // Only set properties that do not already exist as a global builtin.
159+ if (!globalBuiltins.has(name)) {
160+ Object.defineProperty(context, name,
161+ Object.getOwnPropertyDescriptor(global, name));
162+ }
163+ }
164+ context.global = context;
165+
162166 let script;
163167 try {
164168 script = vm.createScript(main, {