Skip to content

Commit f3d1806

Browse files
(Why) is this working?
1 parent 358f67d commit f3d1806

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/debugRuntime.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Subject } from './subject';
1515
import { logger } from './logging';
1616

1717
export type LineHandler = (line: string, from: DataSource, isFullLine: boolean) => string;
18-
export type DapHandler = (dap: string) => string;
18+
export type DapHandler = (dap: Buffer) => Buffer;
1919

2020
export type DataSource = 'stdout'|'stderr'|'dapSocket'|'sinkSocket'|'stdin';
2121
export type OutputMode = 'all'|'filtered'|'nothing';
@@ -125,15 +125,15 @@ export class DebugRuntime extends EventEmitter {
125125
const tmpHandleLine: LineHandler = (line: string, from: DataSource, isFullLine: boolean) => {
126126
return this.handleLine(line, from, isFullLine);
127127
};
128-
const tmpHandleDapString: DapHandler = (dap: string) => {
129-
return this.handleDapString(dap);
128+
const tmpHandleDapData: DapHandler = (dap: Buffer) => {
129+
return this.handleDapData(dap);
130130
};
131131
const tmpEchoStdin = (text: string) => {
132132
if(this.outputModes['stdin'] === 'all'){
133133
setTimeout(() => this.writeOutput(text, false, 'stdout'), 0);
134134
}
135135
};
136-
this.rSession = new RSession(tmpHandleLine, tmpHandleDapString, tmpEchoStdin);
136+
this.rSession = new RSession(tmpHandleLine, tmpHandleDapData, tmpEchoStdin);
137137
// check that the child process launched properly
138138
const successTerminal = await this.rSession.startR(rStartupArguments);
139139
if (!successTerminal) {
@@ -413,10 +413,10 @@ export class DebugRuntime extends EventEmitter {
413413
};
414414
this.dispatchRequest(request);
415415
}
416-
417-
protected handleDapString(dap: string): string {
416+
417+
protected handleDapData(dap: Buffer): Buffer {
418418
while(dap.length > 0){
419-
const m = /^Content-Length: (\d+)\r\n\r\n/.exec(dap);
419+
const m = /^Content-Length: (\d+)\r\n\r\n/.exec(dap.toString('utf-8'));
420420
if(!m){
421421
break;
422422
}
@@ -425,10 +425,11 @@ export class DebugRuntime extends EventEmitter {
425425
if(dap.length < headerLength + contentLength){
426426
break;
427427
}
428-
const jsonString = dap.substr(headerLength, contentLength);
428+
const jsonString = dap.slice(headerLength, headerLength + contentLength).toString('utf-8');
429+
// const jsonString = dap.substr(headerLength, contentLength);
429430
const json = <{[key: string]: any}>JSON.parse(jsonString);
430431
this.handleJson(json);
431-
dap = dap.substr(headerLength + contentLength);
432+
dap = dap.slice(headerLength + contentLength);
432433
}
433434
return dap;
434435
}

src/rSession.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
import * as child from 'child_process';
4-
import { LineHandler, DapHandler, DataSource } from'./debugRuntime';
4+
import { LineHandler, DataSource, DapHandler } from'./debugRuntime';
55
import { RStartupArguments } from './debugProtocolModifications';
66
import { config, getPortNumber } from './utils';
77
import * as net from 'net';
@@ -16,9 +16,10 @@ import kill = require('tree-kill');
1616
export class RSession {
1717
private cp?: child.ChildProcessWithoutNullStreams;
1818
private handleLine: LineHandler;
19-
private handleDapString: DapHandler;
19+
private handleDapData: DapHandler;
2020
private echoStdin: (text: string) => void;
2121
private restOfLine: {[k in DataSource]?: string} = {};
22+
private restOfDap: Buffer = Buffer.from('');
2223

2324
public ignoreOutput: boolean = false;
2425

@@ -32,12 +33,12 @@ export class RSession {
3233

3334
constructor(
3435
handleLine: LineHandler,
35-
handleDapString: DapHandler,
36+
handleDapData: DapHandler,
3637
echoStdin?: (text: string) => void
3738
){
3839
// store line/json handlers (are called by this.handleData)
3940
this.handleLine = handleLine;
40-
this.handleDapString = handleDapString;
41+
this.handleDapData = handleDapData;
4142
this.echoStdin = echoStdin || ((text: string) => {/* dummy */});
4243
}
4344

@@ -66,7 +67,7 @@ export class RSession {
6667
this.dapServer = net.createServer((socket) => {
6768
socket.on('data', (data) => {
6869
this.handleData(data, 'dapSocket');
69-
logger.log('dapOut', data);
70+
logger.log('dapIn', data);
7071
});
7172
this.dapSocket = socket;
7273
});
@@ -138,22 +139,24 @@ export class RSession {
138139

139140
public handleData(data: Buffer, from: DataSource): void{
140141

141-
let text: string = data.toString();
142-
// text = text.replace(/\r/g,''); //keep only \n as linebreak
143-
text = (this.restOfLine[from] || '') + text; // append to rest of line from previouse call
144-
const lines = text.split(/\n/); // split into lines
145-
146142
// logger.debug(`data from ${from}: ${text}`);
147143
//
148144
if(from === 'dapSocket'){
149-
const restOfLine = this.handleDapString(text);
150-
this.restOfLine[from] = restOfLine;
145+
data = Buffer.concat([this.restOfDap, data]);
146+
const restOfLine = this.handleDapData(data);
147+
this.restOfDap = restOfLine;
151148
return;
152149
}
153150

151+
152+
let text: string = data.toString();
153+
// text = text.replace(/\r/g,''); //keep only \n as linebreak
154+
text = (this.restOfLine[from] || '') + text; // append to rest of line from previouse call
155+
const lines = text.split(/\n/); // split into lines
156+
154157
for(let i = 0; i<lines.length; i++){
155-
// abort output handling if ignoreOutput has been set to true
156-
// used to avoid handling remaining output after debugging has been stopped
158+
// abort output handling if ignoreOutput has been set to true
159+
// used to avoid handling remaining output after debugging has been stopped
157160
if(this.ignoreOutput){
158161
return;
159162
}

test/R/objects.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# options(error = traceback)
44

5+
# options(vsc.doLogPrint = TRUE)
6+
57
options(vsc.matricesByRow = FALSE)
68
options(vsc.dataFramesByRow = TRUE)
79

@@ -117,6 +119,7 @@ s3 <- "a"
117119
s4 <- 1 + 2i
118120
s5 <- TRUE
119121
s6 <- charToRaw("h")
122+
s7 <- 'öé'
120123

121124
# ...
122125
fun <- function(x, y=x, ...) {

0 commit comments

Comments
 (0)