NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
1.96k stars 352 forks source link

How to make breakpoint ? #236

Closed codeh2o closed 2 years ago

codeh2o commented 2 years ago
let i = 1
console.log(i)
debugger
i++;

I hope the program can run and pause at the line of the debugger. not one step by step.

How can I do that?

NeilFraser commented 2 years ago

Excellent question. There's a stub for 'debugger' here: https://github.com/NeilFraser/JS-Interpreter/blob/master/interpreter.js#L3865

So what I'd do is create a global (or a new property on Interpreter):

var debugStarted = false;

Then override the debug step:

Interpreter.prototype['stepDebuggerStatement'] = function(stack, state, node) {
  debugStarted = true;
  stack.pop();
};

And write a new run function:

function runToDebug () {
  while (!debugStarted && myInterpreter.step()) {}
  debugStarted = false;
}

This will make 'runToDebug' stop when 'debugger' is reached. Upon which one can either step, or run some more.