silphius/app/astride/scope.js
2024-06-22 08:02:23 -05:00

41 lines
654 B
JavaScript

export default class Scope {
context = {};
parent = null;
constructor(parent) {
this.parent = parent;
}
allocate(key, value) {
this.context[key] = value;
}
get(key) {
let walk = this;
while (walk) {
if (key in walk.context) {
return walk.context[key];
}
walk = walk.parent;
}
return undefined;
}
set(key, value) {
let walk = this;
while (walk) {
if (key in walk.context) {
walk.context[key] = value;
return value;
}
if (!walk.parent) {
walk.context[key] = value;
return value;
}
walk = walk.parent;
}
}
}