silphius/app/astride/scope.js

41 lines
654 B
JavaScript
Raw Normal View History

2024-06-16 08:01:01 -05:00
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;
}
}
}