fix weird programMemory Map issue

This commit is contained in:
Kurt Hutten Irev-Dev
2024-08-16 11:41:09 +10:00
parent bc82575912
commit 6c387bdf62

View File

@ -139,9 +139,7 @@ export const parse = (code: string | Error): Program | Error => {
export type PathToNode = [string | number, string][] export type PathToNode = [string | number, string][]
interface Memory { type Memory = Map<string, KclValue>
[key: string]: KclValue
}
type EnvironmentRef = number type EnvironmentRef = number
@ -153,7 +151,7 @@ interface Environment {
} }
function emptyEnvironment(): Environment { function emptyEnvironment(): Environment {
return { bindings: {}, parent: null } return { bindings: new Map(), parent: null }
} }
interface RawProgramMemory { interface RawProgramMemory {
@ -168,7 +166,7 @@ interface RawProgramMemory {
* in the future. * in the future.
*/ */
export class ProgramMemory { export class ProgramMemory {
private environments: Environment[] environments: Environment[]
private currentEnv: EnvironmentRef private currentEnv: EnvironmentRef
private return: KclValue | null private return: KclValue | null
@ -219,9 +217,9 @@ export class ProgramMemory {
let envRef = this.currentEnv let envRef = this.currentEnv
while (true) { while (true) {
const env = this.environments[envRef] const env = this.environments[envRef]
if (env.bindings.hasOwnProperty(name)) { const val = env.bindings.get(name)
return env.bindings[name] if (val) return val
}
if (!env.parent) { if (!env.parent) {
break break
} }
@ -235,7 +233,7 @@ export class ProgramMemory {
return new Error('No environment to set memory in') return new Error('No environment to set memory in')
} }
const env = this.environments[this.currentEnv] const env = this.environments[this.currentEnv]
env.bindings[name] = value env.bindings.set(name, value)
return null return null
} }
@ -268,7 +266,7 @@ export class ProgramMemory {
continue continue
} }
// Deep copy. // Deep copy.
bindings[name] = structuredClone(value) bindings.set(name, structuredClone(value))
} }
environments.push({ bindings, parent: env.parent }) environments.push({ bindings, parent: env.parent })
} }