1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
// abstract class that dispatches assignment / reference in environments.
EnvironmentRedirect {
var <envir;
var <dispatch;
*new { arg envir;
^super.newCopyArgs(envir ?? { Environment.new(32, Environment.new) })
}
*make { arg function;
^this.new.make(function)
}
*use { arg function;
^this.new.use(function)
}
*newFrom { arg aCollection;
var newCollection = this.new;
aCollection.keysValuesDo({ arg k,v, i; newCollection.put(k,v) });
^newCollection
}
// override in subclasses
at { arg key;
^envir.at(key)
}
put { arg key, obj;
envir.put(key, obj);
dispatch.value(key, obj);
}
localPut { arg key, obj;
envir.put(key, obj)
}
removeAt { arg key;
^envir.removeAt(key)
}
// behave like environment
*push {
^this.new.push;
}
*pop {
^Environment.pop;
}
pop {
^Environment.pop
}
push {
if(currentEnvironment !== this) {
Environment.push(this)
} { "this environment is already current".warn }
}
make { arg function;
// pushes the Envir, executes function, returns the Envir
// usually used to create an environment by adding new variables to it.
var result, saveEnvir;
saveEnvir = currentEnvironment;
currentEnvironment = this;
protect {
function.value(this);
}{
currentEnvironment = saveEnvir;
};
}
use { arg function;
// temporarily replaces the currentEnvironment with this,
// executes function, returns the result of the function
var result, saveEnvir;
saveEnvir = currentEnvironment;
currentEnvironment = this;
protect {
result = function.value(this);
}{
currentEnvironment = saveEnvir;
};
^result
}
do { arg function;
envir.do(function)
}
keysValuesDo { arg function;
envir.keysValuesDo(function);
}
keysValuesArrayDo { arg argArray, function;
envir.keysValuesArrayDo(argArray, function);
}
findKeyForValue { arg val;
^envir.findKeyForValue(val)
}
sortedKeysValuesDo { arg function;
envir.sortedKeysValuesDo(function);
}
putAll { arg ... dictionaries;
dictionaries.do {|dict|
dict.keysValuesDo { arg key, value;
this.put(key, value)
}
}
}
add { arg anAssociation;
this.put(anAssociation.key, anAssociation.value);
}
choose {
^envir.choose
}
clear { envir.clear }
keys { arg species(Set);
^envir.keys(species)
}
values {
^envir.values
}
know_ { arg flag; envir.know = flag }
know { ^envir.know }
doesNotUnderstand { arg selector ... args;
var func;
if (this.know) {
if (selector.isSetter) {
selector = selector.asGetter;
^this[selector] = args[0];
};
^this.doFunctionPerform(selector, args)
};
^this.superPerformList(\doesNotUnderstand, selector, args);
}
doFunctionPerform { arg selector, args;
envir[\forward] !? {
if(envir[selector].isNil) {
^envir[\forward].functionPerformList(\value, this, selector, args);
}
};
^this[selector].functionPerformList(\value, this, args);
}
printOn { | stream |
if (stream.atLimit) { ^this };
stream << this.class.name << "[ " ;
envir.printItemsOn(stream);
stream << " ]" ;
}
linkDoc { arg doc;
doc = doc ? Document.current;
doc.envir_(this);
}
unlinkDoc { arg doc;
doc = doc ? Document.current;
if(doc.envir === this) { doc.envir_(nil) };
}
// networking
dispatch_ { arg disp;
dispatch = disp;
if(disp.respondsTo(\envir_)) { disp.envir_(this) };
}
envir_ { arg argEnvir;
envir = argEnvir;
if(dispatch.notNil) { this.dispatch = dispatch };
}
}
LazyEnvir : EnvironmentRedirect {
var <>proxyClass=\Maybe;
makeProxy {
^proxyClass.asClass.new
}
at { arg key;
var proxy;
proxy = super.at(key);
if(proxy.isNil) {
proxy = this.makeProxy(key);
envir.put(key, proxy);
};
^proxy
}
put { arg key, obj;
this.at(key).source_(obj);
dispatch.value(key, obj); // forward to dispatch for networking
}
removeAt { arg key;
var proxy;
proxy = envir.removeAt(key);
if(proxy.notNil) { proxy.clear };
}
localPut { arg key, obj;
this.at(key).source_(obj);
}
copy {
var result = this.class.new;
dispatch !? { result.dispatch = dispatch };
envir.keysValuesDo { |key, val|
result.envir[key] = val.copy;
};
^result
}
storeOn { | stream |
if (stream.atLimit) { ^this };
stream << this.class.name << ".newFrom([" ;
stream <<<* envir.getPairs.collect { |x, i| if(i.even) { x } { x.source } };
stream << "])" ;
}
}
|