Skip to content

Add debugger support#118

Open
chris-james-displayr wants to merge 2 commits intoJavascriptNet:masterfrom
chris-james-displayr:debugger
Open

Add debugger support#118
chris-james-displayr wants to merge 2 commits intoJavascriptNet:masterfrom
chris-james-displayr:debugger

Conversation

@chris-james-displayr
Copy link

Adds debugger support for debugging the V8 instance using the Chrome DevTools Protocol:

using var context = new JavascriptContext();
using var debugger = new JavascriptDebugger(context, false);
debugger.MessageReceived += (s, e) => Console.WriteLine(e.Message);

// Queue setup commands before running (processed at start of Run())
debugger.SendCommand("{\"id\":1,\"method\":\"Debugger.enable\"}");
debugger.SendCommand("{\"id\":2,\"method\":\"Debugger.setBreakpointByUrl\",...}");

context.Run(script);

// When paused at a breakpoint, MessageReceived fires with "Debugger.paused"
// Send resume/step commands from any thread via SendCommand()

Copy link

@Braedon-Wooding-Displayr Braedon-Wooding-Displayr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing major, but some cleanup + suggestions.

cli::array<System::Byte>^ bytes = gcnew cli::array<System::Byte>((int)view.length());
pin_ptr<System::Byte> pinned = &bytes[0];
memcpy(pinned, view.characters8(), view.length());
return System::Text::Encoding::UTF8->GetString(bytes);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just do a GetString(view.characters8) we might have to call the variant that takes in a byte* (and cast our uint8_t* -> byte*)

Saves a copy and is a lil cleaner for when we do v8 interop migration

static std::string ManagedStringToUtf8(System::String^ str)
{
cli::array<System::Byte>^ bytes = System::Text::Encoding::UTF8->GetBytes(str);
std::string result(bytes->Length, '\0');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be bytes->Length + 1 since it needs to store the \0 or does std::string take it into account here for the ctor arg?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the content length, not the buffer size, so this should be correct. The null terminator is handled internally.

// V8 inspector 8-bit strings are UTF-8 encoded (CDP JSON is ASCII-safe)
cli::array<System::Byte>^ bytes = gcnew cli::array<System::Byte>((int)view.length());
pin_ptr<System::Byte> pinned = &bytes[0];
memcpy(pinned, view.characters8(), view.length());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this view.length include \0 since we shouldn't copy that byte to C#

{
std::unique_lock<std::mutex> lock(queueMutex);
cv.wait(lock, [this] {
return !pendingCommands.empty() || !waitingForDebugger.load();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check disconnecting here, but we check it below, I would guess that both would want to stop waiting once we are disconnecting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants