-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Hi.
Once the JsDiagStartDebugging call is done with a certain callback, and a call to JSRun is in the process of the provided code, the provided callback is called with the event JsDiagDebugEventSourceCompile.
That's ok.
But the code provided to JSRun is always runned before taking into account any call to JsDiagSetBreakpoint, or JsDiagRequestAsyncBreak, even if these functions are called inside the callback, or called before JSRun.
And so I always end with the JsErrorDiagNotAtBreak error code.
Furthermore with more complex code, any "echo" in the JS code is done (and visible in the console) when the call to JSRun is completed. So anything done after the JSRun completion seems to be useless as the JS code will be executed.
Can you tell how to start the debugging and stop at the first line ?
P.S.: You can find below, one of my many attempts to get it right.
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "ChakraCore.h"
#define EXCEPTION_MAX_LENGTH 255
class ErrorCode
{
bool withException;
JsErrorCode errCode;
public:
explicit ErrorCode(bool withException = true):withException(withException), errCode(JsNoError){}
explicit ErrorCode(JsErrorCode errCode, bool withException = true):withException(withException)
{
(*this) = errCode;
}
ErrorCode(const ErrorCode& errorCode)
:withException(errorCode.withException), errCode(errorCode.errorCode())
{}
operator bool() const
{
return errCode == JsNoError;
}
JsErrorCode errorCode() const{return errCode;}
ErrorCode& operator()(JsErrorCode errorCode, const char* errorMessage) noexcept (false)
{
this->errCode = errorCode;
if (errorCode == JsNoError)
return *this;
char buffer [EXCEPTION_MAX_LENGTH + 1];
size_t actualLength;
JsErrorCode error;
JsValueRef exception;
if ((error = JsGetAndClearException(&exception)) != JsNoError)
{
printf("Error on Error! Original error code=%d, new error code=%d; extra info=%s\n",errorCode, error, errorMessage);
exit(EXIT_FAILURE);
}
JsPropertyIdRef messageName;
if ((error = JsCreatePropertyId("message", strlen("message"), &messageName)) != JsNoError)
{
printf("Error on Error! Original error code=%d, new error code=%d; extra info=%s\n",errorCode, error, errorMessage);
exit(EXIT_FAILURE);
}
JsValueRef messageValue;
if ((error = JsGetProperty(exception, messageName, &messageValue)) != JsNoError)
{
printf("Error on Error! Original error code=%d, new error code=%d; extra info=%s\n",errorCode, error, errorMessage);
exit(EXIT_FAILURE);
}
if ((error = JsCopyString(messageValue, buffer, EXCEPTION_MAX_LENGTH, &actualLength)) != JsNoError)
{
printf("Error on Error! Original error code=%d, new error code=%d; extra info=%s\n",errorCode, error, errorMessage);
exit(EXIT_FAILURE);
}
buffer[actualLength] = 0;
printf("Error=%s; extra info=%s\n", buffer, errorMessage);
exit(EXIT_FAILURE);
}
ErrorCode& operator=(JsErrorCode errorCode) noexcept (false)
{
return (*this)(errorCode, "");
}
ErrorCode& operator=(const ErrorCode& errorCode) noexcept (true)
{
this->errCode = errorCode.errCode;
this->withException = errorCode.withException;
return *this;
}
};
unsigned retrieveId(JsValueRef value);
static unsigned scriptId ;
static JsRuntimeHandle runtime;
static JsContextRef context;
static JsValueRef globalObject;
static ErrorCode errCode;
static JsValueRef breakpoint;
void debugCallback(JsDiagDebugEvent debugEvent, JsValueRef eventData, void* callbackState)
{
printf("debugCallback called on %d\n", debugEvent);
fflush(stdout);
static bool allBreakDone = false;
scriptId = retrieveId(eventData);
if (!allBreakDone)
{
errCode = JsDiagRequestAsyncBreak(runtime);
errCode = JsDiagSetBreakpoint(scriptId, 0, 0, &breakpoint);
allBreakDone = true;
}
}
int main()
{
errCode = JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);
errCode = JsCreateContext(runtime, &context);
errCode = JsSetCurrentContext(context);
errCode = JsGetGlobalObject(&globalObject);
errCode = JsDiagStartDebugging(runtime, debugCallback, nullptr);
const char code[] = R"END(
var a = 0;
for(var i = 0; i < 10 ; i++)
a += i;
a++;
)END";
unsigned currentSourceContext = 1;
JsValueRef fname;
JsValueRef jsResult;
errCode = JsCreateString("", 0, &fname);
JsValueRef scriptSource;
errCode = JsCreateString(code,strlen(code), &scriptSource);
errCode = JsRun(scriptSource, currentSourceContext++, fname, JsParseScriptAttributeNone, &jsResult);
JsValueRef breakpoint;
errCode = JsDiagSetBreakpoint(scriptId, 0, 0, &breakpoint);
errCode = JsDiagRequestAsyncBreak(runtime);
errCode = JsDiagSetStepType(JsDiagStepTypeContinue);
return EXIT_SUCCESS;
}