Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChakraJsiRuntime debugger refactoring #2526

Merged
merged 3 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions vnext/Chakra/ChakraJsiRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ ChakraJsiRuntime::ChakraJsiRuntime(ChakraJsiRuntimeArgs&& args) noexcept
// Note :: We currently assume that the runtime will be created and exclusively used in a single thread.
JsSetCurrentContext(m_ctx);

#if defined(USE_EDGEMODE_JSRT)
if (args.enableDebugging)
JsStartDebugging();
#endif

startDebuggingIfNeeded();

setupNativePromiseContinuation();

std::call_once(s_runtimeVersionInitFlag, initRuntimeVersion);
}

ChakraJsiRuntime::~ChakraJsiRuntime() noexcept {

stopDebuggingIfNeeded();

JsSetCurrentContext(JS_INVALID_REFERENCE);
JsRelease(m_ctx, nullptr);

JsSetRuntimeMemoryAllocationCallback(m_runtime, nullptr, nullptr);

JsDisposeRuntime(m_runtime);
}

Expand Down Expand Up @@ -1068,4 +1070,9 @@ jsi::Object ChakraJsiRuntime::createHostObjectProxyHandler() noexcept {
return handlerObj;
}

std::unique_ptr<jsi::Runtime> makeChakraJsiRuntime(ChakraJsiRuntimeArgs&& args) noexcept {
return std::make_unique<ChakraJsiRuntime>(std::move(args));
}


}}} // facebook::jsi::chakraruntime
28 changes: 25 additions & 3 deletions vnext/Chakra/ChakraJsiRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
typedef JsValueRef JsWeakRef;
#endif

#if !defined(USE_EDGEMODE_JSRT)
#include "ChakraCoreDebugger.h"
#else
class DebugProtocolHandler {};
class DebugService {};
#endif

namespace facebook {
namespace jsi {
namespace chakraruntime {
Expand All @@ -46,7 +53,7 @@ class ByteArrayBuffer final : public jsi::Buffer {
std::unique_ptr<uint8_t[]> byteArray_;
};

class ChakraJsiRuntime : public jsi::Runtime {
class ChakraJsiRuntime final : public jsi::Runtime {
public:
ChakraJsiRuntime(ChakraJsiRuntimeArgs&& args) noexcept;
~ChakraJsiRuntime() noexcept;
Expand Down Expand Up @@ -301,10 +308,27 @@ class ChakraJsiRuntime : public jsi::Runtime {
inline void checkException(JsErrorCode res);
inline void checkException(JsErrorCode res, const char* msg);

void startDebuggingIfNeeded();
void stopDebuggingIfNeeded();

JsErrorCode enableDebugging(JsRuntimeHandle runtime, std::string const& runtimeName, bool breakOnNextLine, uint16_t port, std::unique_ptr<DebugProtocolHandler>& debugProtocolHandler, std::unique_ptr<DebugService>& debugService);
void ProcessDebuggerCommandQueue();

private:
JsRuntimeHandle m_runtime;
JsContextRef m_ctx;
std::string m_desc;

std::string m_debugRuntimeName;
int m_debugPort{ 0 };
std::unique_ptr<DebugProtocolHandler> m_debugProtocolHandler;
std::unique_ptr<DebugService> m_debugService;

private:
constexpr static char DebuggerDefaultRuntimeName[] = "runtime1";
constexpr static int DebuggerDefaultPort = 9229;

static void CALLBACK ProcessDebuggerCommandQueueCallback(void* callbackState);
static JsValueRef CALLBACK HostFunctionCall(JsValueRef callee, bool isConstructCall, JsValueRef *argumentsIncThis, unsigned short argumentCountIncThis, void *callbackState);

// String helpers
Expand All @@ -313,8 +337,6 @@ class ChakraJsiRuntime : public jsi::Runtime {

static JsValueRef createJSString(const char*data, size_t length);
static JsValueRef createJSPropertyId(const char*data, size_t length);

friend class ChakraJsiRuntimeWithDebugger;
};

}}} // namespace facebook::jsi::chakraruntime
146 changes: 52 additions & 94 deletions vnext/Chakra/ChakraJsiRuntime_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
// This file contains non-edge-mode (or win32) implementations.
#if !defined(USE_EDGEMODE_JSRT)
#include <ChakraCore.h>

#define CHAKRART_HAS_DEBUGGER

#if defined(CHAKRART_HAS_DEBUGGER)
#include "ChakraCoreDebugger.h"
#endif

namespace facebook {
namespace jsi {
Expand Down Expand Up @@ -86,7 +81,7 @@ bool ChakraJsiRuntime::evaluateSerializedScript(const jsi::Buffer& scriptBuffer,
}

std::unique_ptr<const jsi::Buffer> ChakraJsiRuntime::generatePreparedScript(const std::string& sourceURL, const jsi::Buffer& sourceBuffer) noexcept {
const std::wstring scriptUTF16 = facebook::react::unicode::utf8ToUtf16(reinterpret_cast<const char*>(sourceBuffer.data()), sourceBuffer.size());
const std::wstring scriptUTF16 = facebook::react::UnicodeConversion::Utf8ToUtf16(reinterpret_cast<const char*>(sourceBuffer.data()), sourceBuffer.size());

unsigned int bytecodeSize = 0;
if (JsSerializeScript(scriptUTF16.c_str(), nullptr, &bytecodeSize) == JsNoError)
Expand Down Expand Up @@ -229,82 +224,7 @@ struct FileVersionInfoResource {
s_runtimeVersion |= chakraVersionInfo->fixedFileInfo.dwFileVersionLS;
}

#ifdef CHAKRART_HAS_DEBUGGER
class ChakraJsiRuntimeWithDebugger : public ChakraJsiRuntime {
public:
ChakraJsiRuntimeWithDebugger(ChakraJsiRuntimeArgs&& args);
~ChakraJsiRuntimeWithDebugger();

private:
JsErrorCode enableDebugging(JsRuntimeHandle runtime, std::string const& runtimeName, bool breakOnNextLine, uint16_t port, std::unique_ptr<DebugProtocolHandler>& debugProtocolHandler, std::unique_ptr<DebugService>& debugService);
void ProcessDebuggerCommandQueue();

static void CHAKRA_CALLBACK ProcessDebuggerCommandQueueCallback(void* callbackState);

constexpr static char DebuggerDefaultRuntimeName[] = "runtime1";
constexpr static int DebuggerDefaultPort = 9229;

std::string debugRuntimeName_;
int debugPort_ { 0 };
std::unique_ptr<DebugProtocolHandler> debugProtocolHandler_;
std::unique_ptr<DebugService> debugService_;
};

ChakraJsiRuntimeWithDebugger::ChakraJsiRuntimeWithDebugger(ChakraJsiRuntimeArgs&& args)
: ChakraJsiRuntime{ std::move(args) } {
bool enableDebugging = runtimeArgs().enableDebugging;
bool breakOnNextLine = runtimeArgs().debuggerBreakOnNextLine;

if (enableDebugging)
{
std::string runtimeName = runtimeArgs().debuggerRuntimeName;
int port = runtimeArgs().debuggerPort;

if (runtimeName.empty()) {
runtimeName = DebuggerDefaultRuntimeName;
}

if (port == 0) {
port = DebuggerDefaultPort;
}

JsErrorCode result = this->enableDebugging(m_runtime, runtimeName, breakOnNextLine, static_cast<uint16_t>(port), debugProtocolHandler_, debugService_);

if (result == JsNoError) {
debugPort_ = port;
debugRuntimeName_ = runtimeName;
}
}

if (breakOnNextLine && debugProtocolHandler_) {
if(runtimeArgs().loggingCallback)
runtimeArgs().loggingCallback("Waiting for debugger to connect...", facebook::jsi::chakraruntime::LogLevel::Info);

debugProtocolHandler_->WaitForDebugger();

if (runtimeArgs().loggingCallback)
runtimeArgs().loggingCallback("Debugger connected", facebook::jsi::chakraruntime::LogLevel::Info);
}
}

ChakraJsiRuntimeWithDebugger::~ChakraJsiRuntimeWithDebugger() {
if (debugService_)
{
JsErrorCode result = debugService_->Close();

if (result == JsNoError)
{
result = debugService_->UnregisterHandler(debugRuntimeName_);
}
}
debugService_ = nullptr;
debugProtocolHandler_ = nullptr;

// TODO: Does order matter here? Should this be after release context and before disposeruntime?
JsSetRuntimeMemoryAllocationCallback(m_runtime, nullptr, nullptr);
}

JsErrorCode ChakraJsiRuntimeWithDebugger::enableDebugging(JsRuntimeHandle runtime, std::string const& runtimeName, bool breakOnNextLine, uint16_t port,
JsErrorCode ChakraJsiRuntime::enableDebugging(JsRuntimeHandle runtime, std::string const& runtimeName, bool breakOnNextLine, uint16_t port,
std::unique_ptr<DebugProtocolHandler>& debugProtocolHandler, std::unique_ptr<DebugService>& debugService) {
JsErrorCode result = JsNoError;
auto protocolHandler = std::make_unique<DebugProtocolHandler>(runtime);
Expand Down Expand Up @@ -334,32 +254,70 @@ JsErrorCode ChakraJsiRuntimeWithDebugger::enableDebugging(JsRuntimeHandle runtim
return result;
}

/* static */ void ChakraJsiRuntimeWithDebugger::ProcessDebuggerCommandQueueCallback(void* callbackState) {
ChakraJsiRuntimeWithDebugger* runtime= reinterpret_cast<ChakraJsiRuntimeWithDebugger*>(callbackState);
/* static */ void ChakraJsiRuntime::ProcessDebuggerCommandQueueCallback(void* callbackState) {
ChakraJsiRuntime* runtime = reinterpret_cast<ChakraJsiRuntime*>(callbackState);

if (runtime) {
runtime->ProcessDebuggerCommandQueue();
}
}

void ChakraJsiRuntimeWithDebugger::ProcessDebuggerCommandQueue() {
void ChakraJsiRuntime::ProcessDebuggerCommandQueue() {
if (runtimeArgs().jsQueue) {
runtimeArgs().jsQueue->runOnQueue([this]() {
if (debugProtocolHandler_) {
debugProtocolHandler_->ProcessCommandQueue();
if (m_debugProtocolHandler) {
m_debugProtocolHandler->ProcessCommandQueue();
}
});
}
}

#endif
void ChakraJsiRuntime::startDebuggingIfNeeded() {

std::unique_ptr<jsi::Runtime> makeChakraJsiRuntime(ChakraJsiRuntimeArgs&& args) noexcept {
#ifdef CHAKRART_HAS_DEBUGGER
return std::make_unique<ChakraJsiRuntimeWithDebugger>(std::move(args));
#else
return std::make_unique<ChakraJsiRuntime>(std::move(args));
#endif
if (runtimeArgs().enableDebugging)
mganandraj marked this conversation as resolved.
Show resolved Hide resolved
{
std::string runtimeName = runtimeArgs().debuggerRuntimeName;
int port = runtimeArgs().debuggerPort;
mganandraj marked this conversation as resolved.
Show resolved Hide resolved

if (runtimeName.empty()) {
runtimeName = DebuggerDefaultRuntimeName;
}

if (port == 0) {
port = DebuggerDefaultPort;
}
mganandraj marked this conversation as resolved.
Show resolved Hide resolved

JsErrorCode result = this->enableDebugging(m_runtime, runtimeName, runtimeArgs().debuggerBreakOnNextLine, static_cast<uint16_t>(port), m_debugProtocolHandler, m_debugService);
mganandraj marked this conversation as resolved.
Show resolved Hide resolved

if (result == JsNoError) {
m_debugPort = port;
m_debugRuntimeName = runtimeName;
}
}

if (runtimeArgs().debuggerBreakOnNextLine && m_debugProtocolHandler) {
if (runtimeArgs().loggingCallback)
runtimeArgs().loggingCallback("Waiting for debugger to connect...", facebook::jsi::chakraruntime::LogLevel::Info);

m_debugProtocolHandler->WaitForDebugger();

if (runtimeArgs().loggingCallback)
runtimeArgs().loggingCallback("Debugger connected", facebook::jsi::chakraruntime::LogLevel::Info);
}
}

void ChakraJsiRuntime::stopDebuggingIfNeeded() {
if (m_debugService)
{
JsErrorCode result = m_debugService->Close();

if (result == JsNoError)
{
result = m_debugService->UnregisterHandler(m_debugRuntimeName);
}
}
m_debugService = nullptr;
m_debugProtocolHandler = nullptr;
}

}}} // namespace facebook::jsi::chakraruntime
Expand Down
13 changes: 9 additions & 4 deletions vnext/Chakra/ChakraJsiRuntime_edgemode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ namespace facebook {
namespace jsi {
namespace chakraruntime {

void ChakraJsiRuntime::startDebuggingIfNeeded() {
if (runtimeArgs().enableDebugging)
JsStartDebugging();
}

void ChakraJsiRuntime::stopDebuggingIfNeeded() {
// NOP AFAIK
}

JsWeakRef ChakraJsiRuntime::newWeakObjectRef(const jsi::Object& obj) {
return objectRef(obj);
}
Expand Down Expand Up @@ -94,10 +103,6 @@ void ChakraJsiRuntime::initRuntimeVersion() noexcept {
// NOP
}

std::unique_ptr<jsi::Runtime> makeChakraJsiRuntime(ChakraJsiRuntimeArgs&& args) noexcept {
return std::make_unique<ChakraJsiRuntime>(std::move(args));
}

}}}

#endif