Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

injecting envornment globals in debugger #6872

Merged
merged 2 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions localtypings/pxtparts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ declare namespace pxsim {
export interface DebuggerBreakpointMessage extends DebuggerMessage {
breakpointId: number;
globals: Variables;
environmentGlobals?: Variables;
stackframes: StackFrameInfo[];
exceptionMessage?: string;
exceptionStack?: string;
Expand Down
11 changes: 11 additions & 0 deletions pxtsim/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ namespace pxsim {
return stackFrame.retval;
}

export function injectEnvironmentGlobals(msg: DebuggerBreakpointMessage, heap: Map<any>) {
const environmentGlobals = runtime.environmentGlobals;
const keys = Object.keys(environmentGlobals);
if (!keys.length)
return;

const envVars: Variables = msg.environmentGlobals = {};
Object.keys(environmentGlobals)
.forEach(n => envVars[n] = valToJSON(runtime.environmentGlobals[n], heap))
}

export function getBreakpointMsg(s: pxsim.StackFrame, brkId: number, userGlobals?: string[]): { msg: DebuggerBreakpointMessage, heap: Map<any> } {
const heap: pxsim.Map<any> = {};

Expand Down
5 changes: 4 additions & 1 deletion pxtsim/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ namespace pxsim {
lastPauseTimestamp = 0;
id: string;
globals: any = {};
environmentGlobals: any = {};
currFrame: StackFrame;
otherFrames: StackFrame[] = [];
entry: LabelFn;
Expand Down Expand Up @@ -1135,6 +1136,7 @@ namespace pxsim {

const { msg, heap } = getBreakpointMsg(s, brkId, userGlobals);
dbgHeap = heap;
injectEnvironmentGlobals(msg, heap);
Runtime.postMessage(msg)
breakpoints[0] = 0;
breakFrame = null;
Expand Down Expand Up @@ -1276,7 +1278,8 @@ namespace pxsim {
__this.errorHandler(e)
else {
console.error("Simulator crashed, no error handler", e.stack)
const { msg } = getBreakpointMsg(p, p.lastBrkId, userGlobals)
const { msg, heap } = getBreakpointMsg(p, p.lastBrkId, userGlobals)
injectEnvironmentGlobals(msg, heap);
msg.exceptionMessage = e.message
msg.exceptionStack = e.stack
Runtime.postMessage(msg)
Expand Down
33 changes: 24 additions & 9 deletions webapp/src/debuggerVariables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Variable {
children?: Variable[];
}

interface DebuggerVariablesProps {
interface DebuggerVariablesProps {
apis: pxt.Map<pxtc.SymbolInfo>;
sequence: number;
breakpoint?: pxsim.DebuggerBreakpointMessage;
Expand Down Expand Up @@ -67,7 +67,7 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
componentDidUpdate(prevProps: DebuggerVariablesProps) {
if (this.props.breakpoint) {
if (this.props.sequence != this.state.renderedSequence) {
this.updateVariables(this.props.breakpoint.globals, this.props.breakpoint.stackframes, this.props.filters);
this.updateVariables(this.props.breakpoint, this.props.filters);
}
}
else if (!this.state.frozen) {
Expand Down Expand Up @@ -108,7 +108,7 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
}

return <DebuggerTable header={variableTableHeader} placeholderText={placeholderText}>
{ tableRows }
{tableRows}
</DebuggerTable>
}

Expand Down Expand Up @@ -137,8 +137,12 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
return result;
}

updateVariables(globals: pxsim.Variables, stackFrames: pxsim.StackFrameInfo[], filters?: string[]) {
if (!globals) {
updateVariables(
breakpoint: pxsim.DebuggerBreakpointMessage,
filters?: string[]
) {
const { globals, environmentGlobals, stackframes } = breakpoint;
if (!globals && !environmentGlobals) {
// freeze the ui
this.update(true)
return;
Expand All @@ -150,14 +154,17 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
if (filters) {
updatedGlobals.variables = updatedGlobals.variables.filter(v => filters.indexOf(v.name) !== -1)
}
// inject unfiltered environment variables
if (environmentGlobals)
updatedGlobals.variables = updatedGlobals.variables.concat(variablesToVariableList(environmentGlobals));

assignVarIds(updatedGlobals.variables);

let updatedFrames: ScopeVariables[];
if (stackFrames) {
if (stackframes) {
const oldFrames = this.state.stackFrames;

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

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

return result;

function collectVariables(vars: Variable[]) {
function collectVariables(vars: Variable[]) {
vars.forEach(v => {
result.push(v);
if (v.children) {
Expand Down Expand Up @@ -261,8 +268,16 @@ export class DebuggerVariables extends data.Component<DebuggerVariablesProps, De
}
}

function variablesToVariableList(newVars: pxsim.Variables) {
const current = Object.keys(newVars).map(varName => ({
name: fixVarName(varName),
value: newVars[varName]
}));
return current;
}

function updateScope(lastScope: ScopeVariables, newVars: pxsim.Variables, params?: Variable[]): ScopeVariables {
let current = Object.keys(newVars).map(varName => ({ name: fixVarName(varName), value: newVars[varName] }));
let current = variablesToVariableList(newVars);

if (params) {
current = params.concat(current);
Expand Down