-
Notifications
You must be signed in to change notification settings - Fork 1
/
ejectorsGuardsTrademarks.js
379 lines (349 loc) · 13.1 KB
/
ejectorsGuardsTrademarks.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (C) 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview
* This is a pure-ES5 implementation of ejectors, guards, and trademarks as
* otherwise provided by ES5/3. It is incorporated into the cajaVM object by
* hookupSESPlus.js.
*
* <p>Assumes ES5. Compatible with ES5-strict.
*
* // provides ses.ejectorsGuardsTrademarks
* @author [email protected]
* @requires cajaVM
* @overrides ses, ejectorsGuardsTrademarksModule
*/
var ses;
(function ejectorsGuardsTrademarksModule(){
"use strict";
ses.ejectorsGuardsTrademarks = function ejectorsGuardsTrademarks() {
/**
* During the call to {@code ejectorsGuardsTrademarks}, {@code
* ejectorsGuardsTrademarks} must not call {@code cajaVM.def},
* since startSES.js has not yet finished cleaning things. See the
* doc-comments on the {@code extensions} parameter of
* startSES.js.
*
* <p>Instead, we define here some conveniences for freezing just
* enough without prematurely freezing primodial objects
* transitively reachable from these.
*/
var freeze = Object.freeze;
var constFunc = cajaVM.constFunc;
/**
* Returns a new object whose only utility is its identity and (for
* diagnostic purposes only) its name.
*/
function Token(name) {
name = '' + name;
return freeze({
toString: constFunc(function tokenToString() {
return name;
})
});
}
////////////////////////////////////////////////////////////////////////
// Ejectors
////////////////////////////////////////////////////////////////////////
/**
* One-arg form is known in scheme as "call with escape
* continuation" (call/ec).
*
* <p>In this analogy, a call to {@code callWithEjector} emulates a
* labeled statement. The ejector passed to the {@code attemptFunc}
* emulates the label part. The {@code attemptFunc} itself emulates
* the statement being labeled. And a call to {@code eject} with
* this ejector emulates the return-to-label statement.
*
* <p>We extend the normal notion of call/ec with an
* {@code opt_failFunc} in order to give more the sense of a
* {@code try/catch} (or similarly, the {@code escape} special
* form in E). The {@code attemptFunc} is like the {@code try}
* clause and the {@code opt_failFunc} is like the {@code catch}
* clause. If omitted, {@code opt_failFunc} defaults to the
* {@code identity} function.
*
* <p>{@code callWithEjector} creates a fresh ejector -- a one
* argument function -- for exiting from this attempt. It then calls
* {@code attemptFunc} passing that ejector as argument. If
* {@code attemptFunc} completes without calling the ejector, then
* this call to {@code callWithEjector} completes
* likewise. Otherwise, if the ejector is called with an argument,
* then {@code opt_failFunc} is called with that argument. The
* completion of {@code opt_failFunc} is then the completion of the
* {@code callWithEjector} as a whole.
*
* <p>The ejector stays live until {@code attemptFunc} is exited,
* at which point the ejector is disabled. Calling a disabled
* ejector throws.
*
* <p>Note that the ejector relies on {@code try..catch}, so
* it's not entirely bulletproof. The {@code attemptFunc} can
* block an {@code eject} with a {@code try..catch} or a
* {@code try..finally} that throws, so you should be careful
* about what code is run in the attemptFunc.
*
* <p>Historic note: This was first invented by John C. Reynolds in
* <a href="http://doi.acm.org/10.1145/800194.805852"
* >Definitional interpreters for higher-order programming
* languages</a>. Reynold's invention was a special form as in E,
* rather than a higher order function as here and in call/ec.
*/
function callWithEjector(attemptFunc, opt_failFunc) {
var failFunc = opt_failFunc || function (x) { return x; };
var disabled = false;
var token = new Token('ejection');
var stash = void 0;
function ejector(result) {
if (disabled) {
throw new Error('ejector disabled');
} else {
// don't disable here.
stash = result;
throw token;
}
}
constFunc(ejector);
try {
try {
return attemptFunc(ejector);
} finally {
disabled = true;
}
} catch (e) {
if (e === token) {
return failFunc(stash);
} else {
throw e;
}
}
}
/**
* Safely invokes {@code opt_ejector} with {@code result}.
* <p>
* If {@code opt_ejector} is falsy, disabled, or returns
* normally, then {@code eject} throws. Under no conditions does
* {@code eject} return normally.
*/
function eject(opt_ejector, result) {
if (opt_ejector) {
opt_ejector(result);
throw new Error('Ejector did not exit: ', opt_ejector);
} else {
throw new Error(result);
}
}
////////////////////////////////////////////////////////////////////////
// Sealing and Unsealing
////////////////////////////////////////////////////////////////////////
function makeSealerUnsealerPair() {
var boxValues = new WeakMap();
function seal(value) {
var box = freeze({});
boxValues.set(box, value);
return box;
}
function optUnseal(box) {
return boxValues.has(box) ? [boxValues.get(box)] : null;
}
function unseal(box) {
var result = optUnseal(box);
if (result === null) {
throw new Error("That wasn't one of my sealed boxes!");
} else {
return result[0];
}
}
return freeze({
seal: constFunc(seal),
unseal: constFunc(unseal),
optUnseal: constFunc(optUnseal)
});
}
////////////////////////////////////////////////////////////////////////
// Trademarks
////////////////////////////////////////////////////////////////////////
var stampers = new WeakMap();
/**
* Internal routine for making a trademark from a table.
*
* <p>To untangle a cycle, the guard made by {@code makeTrademark}
* is not yet stamped. The caller of {@code makeTrademark} must
* stamp it before allowing the guard to escape.
*
* <p>Note that {@code makeTrademark} is called during the call to
* {@code ejectorsGuardsTrademarks}, and so must not call {@code
* cajaVM.def}.
*/
function makeTrademark(typename, table) {
typename = '' + typename;
var stamp = freeze({
toString: constFunc(function() { return typename + 'Stamp'; })
});
stampers.set(stamp, function(obj) {
table.set(obj, true);
return obj;
});
return freeze({
toString: constFunc(function() { return typename + 'Mark'; }),
stamp: stamp,
guard: freeze({
toString: constFunc(function() { return typename + 'T'; }),
coerce: constFunc(function(specimen, opt_ejector) {
if (!table.get(specimen)) {
eject(opt_ejector,
'Specimen does not have the "' + typename + '" trademark');
}
return specimen;
})
})
});
}
/**
* Objects representing guards should be marked as such, so that
* they will pass the {@code GuardT} guard.
* <p>
* {@code GuardT} is generally accessible as
* {@code cajaVM.GuardT}. However, {@code GuardStamp} must not be
* made generally accessible, but rather only given to code trusted
* to use it to deem as guards things that act in a guard-like
* manner: A guard MUST be immutable and SHOULD be idempotent. By
* "idempotent", we mean that<pre>
* var x = g(specimen, ej); // may fail
* // if we're still here, then without further failure
* g(x) === x
* </pre>
*/
var GuardMark = makeTrademark('Guard', new WeakMap());
var GuardT = GuardMark.guard;
var GuardStamp = GuardMark.stamp;
stampers.get(GuardStamp)(GuardT);
/**
* The {@code Trademark} constructor makes a trademark, which is a
* guard/stamp pair, where the stamp marks and freezes unfrozen
* records as carrying that trademark and the corresponding guard
* cerifies objects as carrying that trademark (and therefore as
* having been marked by that stamp).
*
* <p>By convention, a guard representing the type-like concept
* 'Foo' is named 'FooT'. The corresponding stamp is
* 'FooStamp'. And the record holding both is 'FooMark'. Many
* guards also have {@code of} methods for making guards like
* themselves but parameterized by further constraints, which are
* usually other guards. For example, {@code T.ListT} is the guard
* representing frozen array, whereas {@code
* T.ListT.of(cajaVM.GuardT)} represents frozen arrays of guards.
*/
function Trademark(typename) {
var result = makeTrademark(typename, new WeakMap());
stampers.get(GuardStamp)(result.guard);
return result;
};
/**
* Given that {@code stamps} is a list of stamps and
* {@code record} is a non-frozen object, this marks record with
* the trademarks of all of these stamps, and then freezes and
* returns the record.
* <p>
* If any of these conditions do not hold, this throws.
*/
function stamp(stamps, record) {
// TODO: Should nonextensible objects be stampable?
if (Object.isFrozen(record)) {
throw new TypeError("Can't stamp frozen objects: " + record);
}
stamps = Array.prototype.slice.call(stamps, 0);
var numStamps = stamps.length;
// First ensure that we will succeed before applying any stamps to
// the record.
var i;
for (i = 0; i < numStamps; i++) {
if (!stampers.has(stamps[i])) {
throw new TypeError("Can't stamp with a non-stamp: " + stamps[i]);
}
}
for (i = 0; i < numStamps; i++) {
// Only works for real stamps, postponing the need for a
// user-implementable auditing protocol.
stampers.get(stamps[i])(record);
}
return freeze(record);
};
////////////////////////////////////////////////////////////////////////
// Guards
////////////////////////////////////////////////////////////////////////
/**
* First ensures that g is a guard; then does
* {@code g.coerce(specimen, opt_ejector)}.
*/
function guard(g, specimen, opt_ejector) {
g = GuardT.coerce(g); // failure throws rather than ejects
return g.coerce(specimen, opt_ejector);
}
/**
* First ensures that g is a guard; then checks whether the specimen
* passes that guard.
* <p>
* If g is a coercing guard, this only checks that g coerces the
* specimen to something rather than failing. Note that trademark
* guards are non-coercing, so if specimen passes a trademark guard,
* then specimen itself has been marked with that trademark.
*/
function passesGuard(g, specimen) {
g = GuardT.coerce(g); // failure throws rather than ejects
return callWithEjector(
constFunc(function(opt_ejector) {
g.coerce(specimen, opt_ejector);
return true;
}),
constFunc(function(ignored) {
return false;
})
);
}
/**
* Create a guard which passes all objects present in {@code table}.
* This may be used to define trademark-like systems which do not require
* the object to be frozen.
*
* {@code typename} is used for toString and {@code errorMessage} is used
* when an object does not pass the guard.
*/
function makeTableGuard(table, typename, errorMessage) {
var g = {
toString: constFunc(function() { return typename + 'T'; }),
coerce: constFunc(function(specimen, opt_ejector) {
if (Object(specimen) === specimen && table.get(specimen)) {
return specimen;
}
eject(opt_ejector, errorMessage);
})
};
stamp([GuardStamp], g);
return freeze(g);
}
////////////////////////////////////////////////////////////////////////
// Exporting
////////////////////////////////////////////////////////////////////////
return freeze({
makeSealerUnsealerPair: constFunc(makeSealerUnsealerPair),
GuardT: GuardT,
makeTableGuard: constFunc(makeTableGuard),
Trademark: constFunc(Trademark),
guard: constFunc(guard),
passesGuard: constFunc(passesGuard),
stamp: constFunc(stamp)
});
};
})();