Skip to content

Commit c1e03e9

Browse files
committed
variable
1 parent d7914d2 commit c1e03e9

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/debugger/AttachDebugSession.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as sb from "smart-buffer";
99
import {
1010
LuaAttachMessage, DMReqInitialize, DebugMessageId, DMMessage, DMLoadScript,
1111
DMAddBreakpoint, DMBreak, StackNodeContainer, StackRootNode, IStackNode,
12-
DMReqEvaluate, DMRespEvaluate, ExprEvaluator
12+
DMReqEvaluate, DMRespEvaluate, ExprEvaluator, LoadedScript, LoadedScriptManager
1313
} from './AttachProtol';
1414
import { ByteArray } from './ByteArray';
1515
import * as path from 'path';
@@ -40,13 +40,7 @@ interface EmmyBreakpoint {
4040
line: number;
4141
}
4242

43-
interface LoadedScript {
44-
path: string;
45-
index: number;
46-
source?: string;
47-
}
48-
49-
export class AttachDebugSession extends LoggingDebugSession implements ExprEvaluator {
43+
export class AttachDebugSession extends LoggingDebugSession implements ExprEvaluator, LoadedScriptManager {
5044

5145
private socket?: net.Socket;
5246
private receiveBuf = new sb.SmartBuffer();
@@ -321,7 +315,7 @@ export class AttachDebugSession extends LoggingDebugSession implements ExprEvalu
321315
}
322316
}
323317

324-
private findScript(path: string): LoadedScript | undefined {
318+
public findScript(path: string): LoadedScript | undefined {
325319
const filePath = this.resolvePath(path);
326320
if (filePath) {
327321
return this.loadedScripts.get(this.normalize(filePath));
@@ -332,7 +326,7 @@ export class AttachDebugSession extends LoggingDebugSession implements ExprEvalu
332326
this.sendEvent(new Event("log", obj));
333327
}
334328

335-
private fundScriptByIndex(index: number): LoadedScript|undefined {
329+
public findScriptByIndex(index: number): LoadedScript | undefined {
336330
for (const iterator of this.loadedScripts) {
337331
if (iterator["1"].index === index) {
338332
return iterator["1"];
@@ -347,7 +341,7 @@ export class AttachDebugSession extends LoggingDebugSession implements ExprEvalu
347341
response.body = {
348342
stackFrames: stacks.children.map(child => {
349343
const root = <StackRootNode> child;
350-
const script = this.fundScriptByIndex(root.scriptIndex);
344+
const script = this.findScriptByIndex(root.scriptIndex);
351345
var source: Source | undefined;
352346
if (script) {
353347
source = new Source(path.basename(script.path), this.resolvePath(script.path));
@@ -377,7 +371,7 @@ export class AttachDebugSession extends LoggingDebugSession implements ExprEvalu
377371
protected variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): void {
378372
if (this.break) {
379373
const node = this.handles.get(args.variablesReference);
380-
const ctx = { evaluator: this, handles: this.handles };
374+
const ctx = { evaluator: this, handles: this.handles, scriptManager: this };
381375
node.computeChildren(ctx).then(vars => {
382376
response.body = { variables: vars.map(node => node.toVariable(ctx)) };
383377
this.sendResponse(response);
@@ -410,7 +404,7 @@ export class AttachDebugSession extends LoggingDebugSession implements ExprEvalu
410404
protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {
411405
const stackId = args.frameId || 0;
412406
this.eval(args.expression, stackId).then(v => {
413-
const ctx = { evaluator: this, handles: this.handles };
407+
const ctx = { evaluator: this, handles: this.handles, scriptManager: this };
414408
const variable = v.resultNode.children[0].toVariable(ctx);
415409
response.body = {
416410
result: variable.name,

src/debugger/AttachProtol.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ export enum StackNodeId {
222222
Error,
223223
}
224224

225+
export interface LoadedScript {
226+
path: string;
227+
index: number;
228+
source?: string;
229+
}
230+
231+
export interface LoadedScriptManager {
232+
findScript(path: string): LoadedScript | undefined;
233+
findScriptByIndex(index: number): LoadedScript | undefined;
234+
}
235+
225236
export interface ExprEvaluator {
226237
eval(expr: string, stack: number): Thenable<DMRespEvaluate>;
227238
}
@@ -233,6 +244,7 @@ interface Context {
233244
interface ComputeContext {
234245
evaluator: ExprEvaluator;
235246
handles: Handles<IStackNode>;
247+
scriptManager: LoadedScriptManager;
236248
}
237249

238250
export interface IStackNode {
@@ -345,7 +357,7 @@ class LuaXTable extends LuaXObjectValue {
345357
}
346358

347359
toVariable(ctx: ComputeContext): DebugProtocol.Variable {
348-
return { name: this.name, value: this.data, variablesReference: ctx.handles.create(this), type:"object" };
360+
return { name: this.name, value: "table", variablesReference: ctx.handles.create(this), type:"object" };
349361
}
350362
}
351363

@@ -371,6 +383,19 @@ class LuaXFunction extends LuaXObjectValue {
371383
this.script = buf.readUint32();
372384
this.line = buf.readUint32();
373385
}
386+
387+
toVariable(ctx: ComputeContext): Variable {
388+
var desc = "native";
389+
if (this.line >= 0 && this.script >= 0) {
390+
const script = ctx.scriptManager.findScriptByIndex(this.script);
391+
if (script) {
392+
desc = `line:${this.line}, script:${script.path}`;
393+
} else {
394+
desc = "unknown source";
395+
}
396+
}
397+
return new Variable(this.name, desc);
398+
}
374399
}
375400

376401
class LuaXUserdata extends LuaXObjectValue {

0 commit comments

Comments
 (0)