issue #82 fix issue with display of dynamic source code

pull/85/head
Dibyendu Majumdar 8 years ago
parent 955d9aeb0d
commit 345f8779dc

@ -1,9 +1,9 @@
#include "protocol.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* Parse a VSCode JSON message type
*/
@ -124,7 +124,7 @@ static json_value *get_object_value(json_value *js, const char *key,
void vscode_json_stringify(const char *src, char *dest, size_t len) {
char *p = dest;
char *end = dest + len - 1;
int n = (int) strlen(src);
int n = (int)strlen(src);
for (int i = 0; i < n && p < end; i++) {
char ch = 0;
switch (src[i]) {
@ -258,6 +258,13 @@ static int vscode_parse_intarg_request(json_value *js, ProtocolMessage *msg,
return msgtype;
}
/* replace back slashes with front slash
*/
static void fix_path(char *buf) {
for (char *cp = buf; *cp; cp++)
if (*cp == '\\') *cp = '/';
}
/* Parse launch request
*/
static int vscode_parse_launch_request(json_value *js, ProtocolMessage *msg,
@ -273,30 +280,23 @@ static int vscode_parse_launch_request(json_value *js, ProtocolMessage *msg,
if (prog == NULL) return VSCODE_UNKNOWN_REQUEST;
ravi_string_copy(msg->u.Request.u.LaunchRequest.program, prog,
sizeof msg->u.Request.u.LaunchRequest.program);
for (char *cp = msg->u.Request.u.LaunchRequest.program; *cp; cp++) {
/* replace back slashes with front slash as the VSCode front end doesn't
* like
* back slashes even though it sends them!
*/
if (*cp == '\\') *cp = '/';
}
fix_path(msg->u.Request.u.LaunchRequest.program);
fprintf(log, "LAUNCH %s\n", prog);
const char *lua_path = get_string_value(args, "LUA_PATH", log);
if (lua_path != NULL) ravi_string_copy(msg->u.Request.u.LaunchRequest.lua_path, lua_path,
sizeof msg->u.Request.u.LaunchRequest.lua_path);
if (lua_path != NULL)
ravi_string_copy(msg->u.Request.u.LaunchRequest.lua_path, lua_path,
sizeof msg->u.Request.u.LaunchRequest.lua_path);
fix_path(msg->u.Request.u.LaunchRequest.lua_path);
const char *lua_cpath = get_string_value(args, "LUA_CPATH", log);
if (lua_cpath != NULL) ravi_string_copy(msg->u.Request.u.LaunchRequest.lua_cpath, lua_cpath,
sizeof msg->u.Request.u.LaunchRequest.lua_cpath);
if (lua_cpath != NULL)
ravi_string_copy(msg->u.Request.u.LaunchRequest.lua_cpath, lua_cpath,
sizeof msg->u.Request.u.LaunchRequest.lua_cpath);
fix_path(msg->u.Request.u.LaunchRequest.lua_cpath);
const char *cwd = get_string_value(args, "cwd", log);
if (cwd != NULL) ravi_string_copy(msg->u.Request.u.LaunchRequest.cwd, cwd,
sizeof msg->u.Request.u.LaunchRequest.cwd);
for (char *cp = msg->u.Request.u.LaunchRequest.cwd; *cp; cp++) {
/* replace back slashes with front slash as the VSCode front end doesn't
* like
* back slashes even though it sends them!
*/
if (*cp == '\\') *cp = '/';
}
if (cwd != NULL)
ravi_string_copy(msg->u.Request.u.LaunchRequest.cwd, cwd,
sizeof msg->u.Request.u.LaunchRequest.cwd);
fix_path(msg->u.Request.u.LaunchRequest.cwd);
msg->u.Request.request_type = msgtype;
return msgtype;
}
@ -313,10 +313,7 @@ static int vscode_parse_set_breakpoints_request(json_value *js,
if (prog == NULL) return VSCODE_UNKNOWN_REQUEST;
ravi_string_copy(msg->u.Request.u.SetBreakpointsRequest.source.path, prog,
sizeof msg->u.Request.u.SetBreakpointsRequest.source.path);
for (char *cp = msg->u.Request.u.SetBreakpointsRequest.source.path; *cp;
cp++) {
if (*cp == '\\') *cp = '/';
}
fix_path(msg->u.Request.u.SetBreakpointsRequest.source.path);
json_value *breakpoints = get_array_value(args, "breakpoints", log);
if (breakpoints == NULL || breakpoints->type != json_array)
return VSCODE_UNKNOWN_REQUEST;

@ -180,11 +180,11 @@ static void handle_stack_trace_request(ProtocolMessage *req,
// ((int)(((intptr_t)src) << 8) & 0xFFFFFF00) ;
res->u.Response.u.StackTraceResponse.stackFrames[depth]
.source.sourceReference = depth + 1;
ravi_string_copy(
snprintf(
res->u.Response.u.StackTraceResponse.stackFrames[depth].source.name,
"<dynamic function>",
sizeof res->u.Response.u.StackTraceResponse.stackFrames[depth]
.source.name);
.source.name,
"<dynamic function %p>", src);
}
res->u.Response.u.StackTraceResponse.stackFrames[depth].id = depth;
res->u.Response.u.StackTraceResponse.stackFrames[depth].line =
@ -196,29 +196,31 @@ static void handle_stack_trace_request(ProtocolMessage *req,
depth++;
}
res->u.Response.u.StackTraceResponse.totalFrames = depth;
for (int i = depth - 1, j = 0; i >= 0; i--, j++) {
if (res->u.Response.u.StackTraceResponse.stackFrames[i]
.source.sourceReference != 0) {
/* swap depth */
source_references[j] = res->u.Response.u.StackTraceResponse.stackFrames[i]
.source.sourceReference -
1;
res->u.Response.u.StackTraceResponse.stackFrames[i]
.source.sourceReference = j;
}
else {
source_references[j] = 0;
}
}
// for (int i = depth - 1, j = 0; i >= 0; i--, j++) {
// if (res->u.Response.u.StackTraceResponse.stackFrames[i]
// .source.sourceReference != 0) {
// /* swap depth */
// source_references[j] =
// res->u.Response.u.StackTraceResponse.stackFrames[i]
// .source.sourceReference -
// 1;
// res->u.Response.u.StackTraceResponse.stackFrames[i]
// .source.sourceReference = j;
// }
// else {
// source_references[j] = 0;
// }
// }
vscode_send(res, out, my_logger);
}
static void handle_source_request(ProtocolMessage *req, ProtocolMessage *res,
lua_State *L, FILE *out) {
lua_Debug entry;
// int depth = (req->u.Request.u.SourceRequest.sourceReference & 0xFF) - 1;
int i = req->u.Request.u.SourceRequest.sourceReference;
int depth = i >= 0 && i < MAX_STACK_FRAMES ? source_references[i] : -1;
// int depth = (req->u.Request.u.SourceRequest.sourceReference & 0xFF) - 1;
int depth = req->u.Request.u.SourceRequest.sourceReference - 1;
// int i = req->u.Request.u.SourceRequest.sourceReference;
// int depth = i >= 0 && i < MAX_STACK_FRAMES ? source_references[i] : -1;
vscode_make_success_response(req, res, VSCODE_SOURCE_RESPONSE);
if (lua_getstack(L, depth, &entry)) {
int status = lua_getinfo(L, "Sln", &entry);
@ -419,7 +421,8 @@ static int get_value(lua_State *L, int stack_idx, char *buf, size_t len) {
/*
* The VSCode front-end sends a variables request when it wants to
* display variables. Unfortunately a limitation is that only a numberic
* reference field is available named 'variableReference' to identify the variable.
* reference field is available named 'variableReference' to identify the
* variable.
* We need to know various bits about the variable - such as its stack frame
* location, the variable's location. Therefore we have to encode various pieces
* of information in the numeric value.
@ -478,12 +481,11 @@ static void handle_variables_request(ProtocolMessage *req, ProtocolMessage *res,
/* If the variable is a table then we pass pack a reference
that is used by the front end to drill down */
res->u.Response.u.VariablesResponse.variables[v]
.variablesReference = is_table
? MAKENUM(type, depth,
isvararg ? ((char)-n) : n,
0, isvararg)
: 0; /* cast is to ensure that we
save a signed char value */
.variablesReference =
is_table ? MAKENUM(type, depth, isvararg ? ((char)-n) : n, 0,
isvararg)
: 0; /* cast is to ensure that we
save a signed char value */
v++;
}
lua_pop(L, 1); /* pop the value */
@ -512,24 +514,34 @@ static void handle_variables_request(ProtocolMessage *req, ProtocolMessage *res,
int v = 0;
while (lua_next(L, stack_idx) && v < MAX_VARIABLES) {
if (v == MAX_VARIABLES) {
ravi_string_copy(res->u.Response.u.VariablesResponse.variables[v].name, "...",
sizeof res->u.Response.u.VariablesResponse.variables[0].name);
ravi_string_copy(res->u.Response.u.VariablesResponse.variables[v].value, "truncated",
sizeof res->u.Response.u.VariablesResponse.variables[0].value);
ravi_string_copy(
res->u.Response.u.VariablesResponse.variables[v].name,
"...",
sizeof res->u.Response.u.VariablesResponse.variables[0]
.name);
ravi_string_copy(
res->u.Response.u.VariablesResponse.variables[v].value,
"truncated",
sizeof res->u.Response.u.VariablesResponse.variables[0]
.value);
lua_pop(L, 1);
}
else {
// stack now contains: -1 => value; -2 => key
// copy the key so that lua_tostring does not modify the original
// copy the key so that lua_tostring does not modify the
// original
lua_pushvalue(L, -2);
// stack now contains: -1 => key; -2 => value; -3 => key
get_value(
L, -1, res->u.Response.u.VariablesResponse.variables[v].name,
sizeof res->u.Response.u.VariablesResponse.variables[0].name);
get_value(L, -2,
res->u.Response.u.VariablesResponse.variables[v].value,
sizeof res->u.Response.u.VariablesResponse.variables[0]
.value);
L, -1,
res->u.Response.u.VariablesResponse.variables[v].name,
sizeof res->u.Response.u.VariablesResponse.variables[0]
.name);
get_value(
L, -2,
res->u.Response.u.VariablesResponse.variables[v].value,
sizeof res->u.Response.u.VariablesResponse.variables[0]
.value);
/*
* Right now we do not support further drill down
*/
@ -645,10 +657,12 @@ static void debugger(lua_State *L, lua_Debug *ar, FILE *in, FILE *out) {
if (!initialized) break;
if (ar->source[0] == '@') {
/* Only support breakpoints on disk based code */
// fprintf(my_logger,
// "Breakpoint[%d] check %s vs ar.source=%s, %d vs ar.line=%d\n",
// j, breakpoints[j].source.path, ar->source, breakpoints[j].line,
// ar->currentline);
// fprintf(my_logger,
// "Breakpoint[%d] check %s vs ar.source=%s, %d vs
// ar.line=%d\n",
// j, breakpoints[j].source.path, ar->source,
// breakpoints[j].line,
// ar->currentline);
if (strcmp(breakpoints[j].source.path, ar->source + 1) == 0) {
debugger_state = DEBUGGER_PROGRAM_STEPPING;
break;
@ -819,7 +833,7 @@ static void createargtable(lua_State *L, char **argv, int argc, int script) {
* The protocol used is described in protocol.h.
*/
int main(int argc, char **argv) {
/* For debugging purposes we log the interaction */
/* For debugging purposes we log the interaction */
#ifdef _WIN32
my_logger = fopen("/temp/out1.txt", "w");
#else

Loading…
Cancel
Save