ClearScript
ClearScript copied to clipboard
Allow "Await Debugger" in V8 at any time, rather than just on startup
In our application we execute a lot of global/system scripts before we get to the "user" scripts, and most of the time its only those later scripts that we care about debugging, and normally only specific methods inside those user scripts, so pausing execution on startup is a little "heavy handed". Currently I am working around this by manually poking the flag in the V8ScriptEngine with reflection:
var field = engine.GetType().GetField("awaitDebuggerAndPause",
BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(engine, true);
This works fine, but thought it would be nice to make this a supported thing to do, so this PR adds a single method to V8ScriptEngine that lets you set this flag on demand. Its a method rather than just setting the property directly because using a property setter feels like the caller has the responsibility to flip it back afterwards, but the script engine does that itself on next execution.
I haven't added any tests for this because I couldn't see how (other than changing the visibility of the field, which felt icky), but its only a 3 line method so hopefully that's ok :)
It also appears that using "Invoke" doesn't check or use the awaitDebuggerAndPause flag, which means I have to "wrap" executing the function and use Execute, from looking at the code I can't see any reason why adding this in would cause problems, I'll do so if you think its ok?
Hi @grumpydev,
Thanks for your contribution!
It also appears that using "Invoke" doesn't check or use the awaitDebuggerAndPause flag
Quite correct. In fact, ClearScript provides many ways to enter V8's interpreter, and most of them do not honor that flag. Exposing it may work in your scenario, but there may be a more robust and generally useful way to provide the behavior you're looking for. We'll take a look.
Thanks again!