on master help sign in
[nodejs] don't run code twice on error (#88)
commit74f34a6f79279a587d5202241e0969e971943f0d authorWade Bourne <47748690+AllAwesome497@users.noreply.github.com> dateWed 13 Apr 2022, 17:37 (5 years ago) parentfc9ff1e
1 file changed+13−8
prybar_assets/nodejs/module-context-hook.js+13−8
prybar_assets/nodejs/module-context-hook.js+13−8
@@ -166,8 +166,8 @@ function runModule(moduleName, isInteractive) {
166166 let compiled;
167167
168168 try {
169− hookedFileSource = code;
170− compiled = compile(code + interactiveFooterSnippet, fileName);
169+ // validate that the module has valid syntax without executing the code
170+ vm.compileFunction(code, moduleVariables);
171171 } catch (err) {
172172 // this error is probably unrelated to our added code
173173 // and should fail to compile again.
@@ -180,10 +180,15 @@ function runModule(moduleName, isInteractive) {
180180 console.warn(
181181 'running without interp',
182182 err,
183− 'please submit a bug report.',
183+ 'please submit a bug report.'
184184 );
185+
186+ return compiled;
185187 }
186188
189+ hookedFileSource = code;
190+ compiled = compile(code + interactiveFooterSnippet, fileName);
191+
187192 return compiled;
188193 });
189194 }
@@ -214,7 +219,7 @@ function runCode(code, isInterractive) {
214219 fileSource,
215220 // lie to the `compile` function about the file we're compiling
216221 // so calls to `require` will be resolved from the current directory.
217− fakeFileName,
222+ fakeFileName
218223 );
219224 });
220225
@@ -303,9 +308,9 @@ function getRepl() {
303308 new Set(
304309 Reflect.ownKeys(global).concat(
305310 Reflect.ownKeys(locals),
306− Reflect.ownKeys(ctx),
307− ),
308− ),
311+ Reflect.ownKeys(ctx)
312+ )
313+ )
309314 );
310315 },
311316 // Trap for the [[GetOwnProperty]] (used by the Object.getOwnPropertyDescriptor) function
@@ -407,7 +412,7 @@ function getRepl() {
407412 }
408413
409414 const kContextId = Object.getOwnPropertySymbols(repl).find(
410− (v) => v.description === 'contextId',
415+ (v) => v.description === 'contextId'
411416 );
412417
413418 repl.context.repl = repl;