Skip to content

Commit cd97cd7

Browse files
authored
injecting envornment globals in debugger (microsoft#6872)
* injecting envornment globals in debugger * lint
1 parent 3da41c4 commit cd97cd7

4 files changed

Lines changed: 40 additions & 10 deletions

File tree

localtypings/pxtparts.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ declare namespace pxsim {
179179
export interface DebuggerBreakpointMessage extends DebuggerMessage {
180180
breakpointId: number;
181181
globals: Variables;
182+
environmentGlobals?: Variables;
182183
stackframes: StackFrameInfo[];
183184
exceptionMessage?: string;
184185
exceptionStack?: string;

pxtsim/debugger.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ namespace pxsim {
177177
return stackFrame.retval;
178178
}
179179

180+
export function injectEnvironmentGlobals(msg: DebuggerBreakpointMessage, heap: Map<any>) {
181+
const environmentGlobals = runtime.environmentGlobals;
182+
const keys = Object.keys(environmentGlobals);
183+
if (!keys.length)
184+
return;
185+
186+
const envVars: Variables = msg.environmentGlobals = {};
187+
Object.keys(environmentGlobals)
188+
.forEach(n => envVars[n] = valToJSON(runtime.environmentGlobals[n], heap))
189+
}
190+
180191
export function getBreakpointMsg(s: pxsim.StackFrame, brkId: number, userGlobals?: string[]): { msg: DebuggerBreakpointMessage, heap: Map<any> } {
181192
const heap: pxsim.Map<any> = {};
182193

pxtsim/runtime.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ namespace pxsim {
633633
lastPauseTimestamp = 0;
634634
id: string;
635635
globals: any = {};
636+
environmentGlobals: any = {};
636637
currFrame: StackFrame;
637638
otherFrames: StackFrame[] = [];
638639
entry: LabelFn;
@@ -1135,6 +1136,7 @@ namespace pxsim {
11351136

11361137
const { msg, heap } = getBreakpointMsg(s, brkId, userGlobals);
11371138
dbgHeap = heap;
1139+
injectEnvironmentGlobals(msg, heap);
11381140
Runtime.postMessage(msg)
11391141
breakpoints[0] = 0;
11401142
breakFrame = null;
@@ -1276,7 +1278,8 @@ namespace pxsim {
12761278
__this.errorHandler(e)
12771279
else {
12781280
console.error("Simulator crashed, no error handler", e.stack)
1279-
const { msg } = getBreakpointMsg(p, p.lastBrkId, userGlobals)
1281+
const { msg, heap } = getBreakpointMsg(p, p.lastBrkId, userGlobals)
1282+
injectEnvironmentGlobals(msg, heap);
12801283
msg.exceptionMessage = e.message
12811284
msg.exceptionStack = e.stack
12821285
Runtime.postMessage(msg)

webapp/src/debuggerVariables.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface Variable {
1919
children?: Variable[];
2020
}
2121

22-
interface DebuggerVariablesProps {
22+
interface DebuggerVariablesProps {
2323
apis: pxt.Map<pxtc.SymbolInfo>;
2424
sequence: number;
2525
breakpoint?: pxsim.DebuggerBreakpointMessage;
@@ -67,7 +67,7 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
6767
componentDidUpdate(prevProps: DebuggerVariablesProps) {
6868
if (this.props.breakpoint) {
6969
if (this.props.sequence != this.state.renderedSequence) {
70-
this.updateVariables(this.props.breakpoint.globals, this.props.breakpoint.stackframes, this.props.filters);
70+
this.updateVariables(this.props.breakpoint, this.props.filters);
7171
}
7272
}
7373
else if (!this.state.frozen) {
@@ -108,7 +108,7 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
108108
}
109109

110110
return <DebuggerTable header={variableTableHeader} placeholderText={placeholderText}>
111-
{ tableRows }
111+
{tableRows}
112112
</DebuggerTable>
113113
}
114114

@@ -137,8 +137,12 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
137137
return result;
138138
}
139139

140-
updateVariables(globals: pxsim.Variables, stackFrames: pxsim.StackFrameInfo[], filters?: string[]) {
141-
if (!globals) {
140+
updateVariables(
141+
breakpoint: pxsim.DebuggerBreakpointMessage,
142+
filters?: string[]
143+
) {
144+
const { globals, environmentGlobals, stackframes } = breakpoint;
145+
if (!globals && !environmentGlobals) {
142146
// freeze the ui
143147
this.update(true)
144148
return;
@@ -150,14 +154,17 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
150154
if (filters) {
151155
updatedGlobals.variables = updatedGlobals.variables.filter(v => filters.indexOf(v.name) !== -1)
152156
}
157+
// inject unfiltered environment variables
158+
if (environmentGlobals)
159+
updatedGlobals.variables = updatedGlobals.variables.concat(variablesToVariableList(environmentGlobals));
153160

154161
assignVarIds(updatedGlobals.variables);
155162

156163
let updatedFrames: ScopeVariables[];
157-
if (stackFrames) {
164+
if (stackframes) {
158165
const oldFrames = this.state.stackFrames;
159166

160-
updatedFrames = stackFrames.map((sf, index) => {
167+
updatedFrames = stackframes.map((sf, index) => {
161168
const key = sf.breakpointId + "_" + index;
162169

163170
for (const frame of oldFrames) {
@@ -219,7 +226,7 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
219226

220227
return result;
221228

222-
function collectVariables(vars: Variable[]) {
229+
function collectVariables(vars: Variable[]) {
223230
vars.forEach(v => {
224231
result.push(v);
225232
if (v.children) {
@@ -261,8 +268,16 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
261268
}
262269
}
263270

271+
function variablesToVariableList(newVars: pxsim.Variables) {
272+
const current = Object.keys(newVars).map(varName => ({
273+
name: fixVarName(varName),
274+
value: newVars[varName]
275+
}));
276+
return current;
277+
}
278+
264279
function updateScope(lastScope: ScopeVariables, newVars: pxsim.Variables, params?: Variable[]): ScopeVariables {
265-
let current = Object.keys(newVars).map(varName => ({ name: fixVarName(varName), value: newVars[varName] }));
280+
let current = variablesToVariableList(newVars);
266281

267282
if (params) {
268283
current = params.concat(current);

0 commit comments

Comments
 (0)