From f055abcb85f196144cfdae752eaf5c1e3ef2b401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Tainon?= Date: Wed, 28 Aug 2024 15:48:57 +0200 Subject: [PATCH] Exclude defined variables --- frontend/stepper/python/analysis.ts | 15 +++++++++------ frontend/stepper/python/python_runner.ts | 5 ++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/frontend/stepper/python/analysis.ts b/frontend/stepper/python/analysis.ts index ff09fe2b..17a534ce 100644 --- a/frontend/stepper/python/analysis.ts +++ b/frontend/stepper/python/analysis.ts @@ -14,7 +14,7 @@ import log from 'loglevel'; */ const SKULPT_ANALYSIS_DEBUG = 1; -export const convertSkulptStateToAnalysisSnapshot = function (suspensions: readonly any[], lastAnalysis: AnalysisSnapshot, newStepNum: number): AnalysisSnapshot { +export const convertSkulptStateToAnalysisSnapshot = function (suspensions: readonly any[], lastAnalysis: AnalysisSnapshot, newStepNum: number, excludedVariableNames: string[]): AnalysisSnapshot { // @ts-ignore if (SKULPT_ANALYSIS_DEBUG === 2) { log.getLogger('python_runner').debug('[¥¥¥¥¥¥¥] Building analysis'); @@ -57,7 +57,7 @@ export const convertSkulptStateToAnalysisSnapshot = function (suspensions: reado log.getLogger('python_runner').debug('suspension loaded', suspension.$loaded_references); - const scope = analyseSkulptScope(suspension); + const scope = analyseSkulptScope(suspension, excludedVariableNames); stackFrame.scopes.push(scope); stackFrame.name = scope.name; stackFrames.push(stackFrame); @@ -95,7 +95,7 @@ const isProgramSuspension = function(suspension): boolean { /** * Transforms the skulpt scope (one suspension) to something readable with the variables content. */ -export const analyseSkulptScope = function(suspension: any): AnalysisScope { +export const analyseSkulptScope = function(suspension: any, excludedVariableNames: string[]): AnalysisScope { // @ts-ignore if (SKULPT_ANALYSIS_DEBUG === 2) { log.getLogger('python_runner').debug('////// Analyse scope...'); @@ -118,7 +118,7 @@ export const analyseSkulptScope = function(suspension: any): AnalysisScope { suspVariables = suspension.$loc; } - const variableNames = sortArgumentsFirst(filterInternalVariables(Object.keys(suspVariables)), suspension._argnames); + const variableNames = sortArgumentsFirst(filterInternalVariables(Object.keys(suspVariables), excludedVariableNames), suspension._argnames); const loadedReferences = suspension.$loaded_references ? suspension.$loaded_references : {}; for (let variableName of variableNames) { @@ -188,9 +188,12 @@ const variablesBeginWithIgnore = [ * * @returns {Array} */ -const filterInternalVariables = (variableNames: string[]): string[] => { +const filterInternalVariables = (variableNames: string[], excludedVariableNames: string[]): string[] => { return variableNames.filter((name) => { - let ignore = false; + if (-1 !== excludedVariableNames.indexOf(name)) { + return false; + } + for (let variableBeginWithIgnore of variablesBeginWithIgnore) { if (name.indexOf(variableBeginWithIgnore) === 0) { return false; diff --git a/frontend/stepper/python/python_runner.ts b/frontend/stepper/python/python_runner.ts index 463dc16c..57b11d4b 100644 --- a/frontend/stepper/python/python_runner.ts +++ b/frontend/stepper/python/python_runner.ts @@ -58,6 +58,7 @@ export default class PythonRunner extends AbstractRunner { private _stepInProgress = false; private stepMode = false; private _editorMarker = null; + private definedConstants = []; private availableModules = []; private availableBlocks = [] as Block[]; public _isFinished = false; @@ -280,9 +281,11 @@ mod.${className} = Sk.misceval.buildClass(mod, newClass${className}, "${classNam modContents += PythonRunner._skulptifyClassInstance(classInstance, className); } + this.definedConstants = []; for (let block of blocks.filter(block => block.type === BlockType.Constant)) { const {name, value} = block; modContents += PythonRunner._skulptifyConst(name, value); + this.definedConstants.push(name); } modContents += "\nreturn mod;\n};"; @@ -775,7 +778,7 @@ mod.${className} = Sk.misceval.buildClass(mod, newClass${className}, "${classNam stepperState.isFinished = true; } else { log.getLogger('stepper').debug('INCREASE STEP NUM TO ', stepperState.analysis.stepNum + 1); - stepperState.analysis = convertSkulptStateToAnalysisSnapshot(stepperState.suspensions, stepperState.lastAnalysis, stepperState.analysis.stepNum + 1); + stepperState.analysis = convertSkulptStateToAnalysisSnapshot(stepperState.suspensions, stepperState.lastAnalysis, stepperState.analysis.stepNum + 1, this.definedConstants); stepperState.directives = { ordered: parseDirectives(stepperState.analysis), functionCallStackMap: null,