-
Notifications
You must be signed in to change notification settings - Fork 86
/
runtime.js
464 lines (423 loc) · 11 KB
/
runtime.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
var True = true;
var False = false;
/*******************************************************************************
* Thunks.
*/
// Force a thunk (if it is a thunk) until WHNF.
function _(thunkish,nocache){
while (thunkish instanceof $) {
thunkish = thunkish.force(nocache);
}
return thunkish;
}
// Apply a function to arguments (see method2 in Fay.hs).
function __(){
var f = arguments[0];
for (var i = 1, len = arguments.length; i < len; i++) {
f = (f instanceof $? _(f) : f)(arguments[i]);
}
return f;
}
// Thunk object.
function $(value){
this.forced = false;
this.value = value;
}
// Force the thunk.
$.prototype.force = function(nocache) {
return nocache ?
this.value() :
(this.forced ?
this.value :
(this.value = this.value(), this.forced = true, this.value));
};
/*******************************************************************************
* Monad.
*/
function Fay$$Monad(value){
this.value = value;
}
// This is used directly from Fay, but can be rebound or shadowed. See primOps in Types.hs.
// >>
function Fay$$then(a){
return function(b){
return Fay$$bind(a)(function(_){
return b;
});
};
}
// >>=
// This is used directly from Fay, but can be rebound or shadowed. See primOps in Types.hs.
function Fay$$bind(m){
return function(f){
return new $(function(){
var monad = _(m,true);
if(monad.cont) {
return _(monad.cont(f));
}
else {
return f(monad.value);
}
});
};
}
// This is used directly from Fay, but can be rebound or shadowed.
function Fay$$$_return(a){
return new Fay$$Monad(a);
}
// This is used directly from Fay, but can be rebound or shadowed. See primOps in Types.hs.
function Fay$$sync($p1){
return new $(function(){
return { cont: $p1 };
});
};
// Unit: ().
var Fay$$unit = null;
/*******************************************************************************
* Serialization.
* Fay <-> JS. Should be bijective.
*/
// Serialize a Fay object to JS.
function Fay$$fayToJs(type,fayObj){
var base = type[0];
var args = type[1];
var jsObj;
switch(base){
case "action": {
// A nullary monadic action. Should become a nullary JS function.
// Fay () -> function(){ return ... }
jsObj = function(){
return Fay$$fayToJs(args[0],_(fayObj,true).value);
};
break;
}
case "function": {
// A proper function.
jsObj = function(){
var fayFunc = fayObj;
var return_type = args[args.length-1];
var len = args.length;
// If some arguments.
if (len > 1) {
// Apply to all the arguments.
fayFunc = _(fayFunc,true);
// TODO: Perhaps we should throw an error when JS
// passes more arguments than Haskell accepts.
for (var i = 0, len = len; i < len - 1 && fayFunc instanceof Function; i++) {
// Unserialize the JS values to Fay for the Fay callback.
fayFunc = _(fayFunc(Fay$$jsToFay(args[i],arguments[i])),true);
}
// Finally, serialize the Fay return value back to JS.
var return_base = return_type[0];
var return_args = return_type[1];
// If it's a monadic return value, get the value instead.
if(return_base == "action") {
return Fay$$fayToJs(return_args[0],fayFunc.value);
}
// Otherwise just serialize the value direct.
else {
return Fay$$fayToJs(return_type,fayFunc);
}
} else {
throw new Error("Nullary function?");
}
};
break;
}
case "string": {
// Serialize Fay string to JavaScript string.
var str = "";
fayObj = _(fayObj);
while(fayObj instanceof Fay$$Cons) {
str += fayObj.car;
fayObj = _(fayObj.cdr);
}
jsObj = str;
break;
}
case "list": {
// Serialize Fay list to JavaScript array.
var arr = [];
fayObj = _(fayObj);
while(fayObj instanceof Fay$$Cons) {
arr.push(Fay$$fayToJs(args[0],fayObj.car));
fayObj = _(fayObj.cdr);
}
jsObj = arr;
break;
}
case "double": {
// Serialize double, just force the argument. Doubles are unboxed.
jsObj = _(fayObj);
break;
}
case "int": {
// Serialize int, just force the argument. Ints are unboxed.
jsObj = _(fayObj);
break;
}
case "bool": {
// Bools are unboxed.
jsObj = _(fayObj);
break;
}
case "unknown":
case "user": {
if(fayObj instanceof $)
fayObj = _(fayObj);
jsObj = Fay$$fayToJsUserDefined(type,fayObj);
break;
}
default: throw new Error("Unhandled Fay->JS translation type: " + base);
}
return jsObj;
}
// Unserialize an object from JS to Fay.
function Fay$$jsToFay(type,jsObj){
var base = type[0];
var args = type[1];
var fayObj;
switch(base){
case "action": {
// Unserialize a "monadic" JavaScript return value into a monadic value.
fayObj = new Fay$$Monad(Fay$$jsToFay(args[0],jsObj));
break;
}
case "string": {
// Unserialize a JS string into Fay list (String).
fayObj = Fay$$list(jsObj);
break;
}
case "list": {
// Unserialize a JS array into a Fay list ([a]).
var serializedList = [];
for (var i = 0, len = jsObj.length; i < len; i++) {
// Unserialize each JS value into a Fay value, too.
serializedList.push(Fay$$jsToFay(args[0],jsObj[i]));
}
// Pop it all in a Fay list.
fayObj = Fay$$list(serializedList);
break;
}
case "double": {
// Doubles are unboxed, so there's nothing to do.
fayObj = jsObj;
break;
}
case "int": {
// Int are unboxed, so there's no forcing to do.
// But we can do validation that the int has no decimal places.
// E.g. Math.round(x)!=x? throw "NOT AN INTEGER, GET OUT!"
fayObj = Math.round(jsObj);
if(fayObj!==jsObj) throw "Argument " + jsObj + " is not an integer!";
break;
}
case "bool": {
// Bools are unboxed.
fayObj = jsObj;
break;
}
case "unknown":
case "user": {
if (jsObj && jsObj['instance']) {
fayObj = Fay$$jsToFayUserDefined(type,jsObj);
}
else
fayObj = jsObj;
break;
}
default: throw new Error("Unhandled JS->Fay translation type: " + base);
}
return fayObj;
}
/*******************************************************************************
* Lists.
*/
// Cons object.
function Fay$$Cons(car,cdr){
this.car = car;
this.cdr = cdr;
}
// Make a list.
function Fay$$list(xs){
var out = null;
for(var i=xs.length-1; i>=0;i--)
out = new Fay$$Cons(xs[i],out);
return out;
}
// Built-in list cons.
function Fay$$cons(x){
return function(y){
return new Fay$$Cons(x,y);
};
}
// List index.
function Fay$$index(index){
return function(list){
for(var i = 0; i < index; i++) {
list = _(list).cdr;
}
return list.car;
};
}
/*******************************************************************************
* Numbers.
*/
// Built-in *.
function Fay$$mult(x){
return function(y){
return new $(function(){
return _(x) * _(y);
});
};
}
// Built-in +.
function Fay$$add(x){
return function(y){
return new $(function(){
return _(x) + _(y);
});
};
}
// Built-in -.
function Fay$$sub(x){
return function(y){
return new $(function(){
return _(x) - _(y);
});
};
}
// Built-in /.
function Fay$$div(x){
return function(y){
return new $(function(){
return _(x) / _(y);
});
};
}
/*******************************************************************************
* Booleans.
*/
// Are two values equal?
function Fay$$equal(lit1, lit2) {
// Simple case
lit1 = _(lit1);
lit2 = _(lit2);
if (lit1 === lit2) {
return true;
}
// General case
if (lit1 instanceof Array) {
if (lit1.length != lit2.length) return false;
for (var len = lit1.length, i = 0; i < len; i++) {
if (!Fay$$equal(lit1[i], lit2[i])) return false;
}
return true;
} else if (lit1 instanceof Fay$$Cons && lit2 instanceof Fay$$Cons) {
do {
if (!Fay$$equal(lit1.car,lit2.car))
return false;
lit1 = _(lit1.cdr), lit2 = _(lit2.cdr);
if (lit1 === null || lit2 === null)
return lit1 === lit2;
} while (true);
} else if (typeof lit1 == 'object' && typeof lit2 == 'object' && lit1 && lit2 &&
lit1.constructor === lit2.constructor) {
for(var x in lit1) {
if(!(lit1.hasOwnProperty(x) && lit2.hasOwnProperty(x) &&
Fay$$equal(lit1[x],lit2[x])))
return false;
}
return true;
} else {
return false;
}
}
// Built-in ==.
function Fay$$eq(x){
return function(y){
return new $(function(){
return Fay$$equal(x,y);
});
};
}
// Built-in /=.
function Fay$$neq(x){
return function(y){
return new $(function(){
return !(Fay$$equal(x,y));
});
};
}
// Built-in >.
function Fay$$gt(x){
return function(y){
return new $(function(){
return _(x) > _(y);
});
};
}
// Built-in <.
function Fay$$lt(x){
return function(y){
return new $(function(){
return _(x) < _(y);
});
};
}
// Built-in >=.
function Fay$$gte(x){
return function(y){
return new $(function(){
return _(x) >= _(y);
});
};
}
// Built-in <=.
function Fay$$lte(x){
return function(y){
return new $(function(){
return _(x) <= _(y);
});
};
}
// Built-in &&.
function Fay$$and(x){
return function(y){
return new $(function(){
return _(x) && _(y);
});
};
}
// Built-in ||.
function Fay$$or(x){
return function(y){
return new $(function(){
return _(x) || _(y);
});
};
}
/*******************************************************************************
* Mutable references.
*/
// Make a new mutable reference.
function Fay$$Ref(x){
this.value = x;
}
// Write to the ref.
function Fay$$writeRef(ref,x){
ref.value = x;
}
// Get the value from the ref.
function Fay$$readRef(ref,x){
return ref.value;
}
/*******************************************************************************
* Dates.
*/
function Fay$$date(str){
return window.Date.parse(str);
}
/*******************************************************************************
* Application code.
*/