2024-06-16 08:01:01 -05:00
|
|
|
export default class Scope {
|
|
|
|
|
|
|
|
context = {};
|
|
|
|
parent = null;
|
|
|
|
|
|
|
|
constructor(parent) {
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
allocate(key, value) {
|
2024-06-30 10:03:50 -05:00
|
|
|
return this.context[key] = value;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|