-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathruntime.js
More file actions
6019 lines (5581 loc) · 233 KB
/
Copy pathruntime.js
File metadata and controls
6019 lines (5581 loc) · 233 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
define("pyret-base/js/runtime",
["pyret-base/js/namespace",
"pyret-base/js/js-numbers",
"pyret-base/js/codePoint",
"pyret-base/js/runtime-util",
"pyret-base/js/exn-stack-parser",
"pyret-base/js/secure-loader",
"seedrandom"],
function (Namespace, jsnums, codePoint, util, exnStackParser, loader, seedrandom) {
Error.stackTraceLimit = Infinity;
var require = requirejs;
var AsciiTable;
var codePointAt = codePoint.codePointAt;
var fromCodePoint = codePoint.fromCodePoint;
/**
Creates a Pyret runtime
@param {{stdout : function(string), initialGas : number}}
@return {Object} that contains all the necessary components of a runtime
*/
function makeRuntime(theOutsideWorld) {
var CONSOLE = theOutsideWorld.console || console;
/**
Extends an object with the new fields in fields
If all the fields are new, the brands are kept,
otherwise, the extended object has no brands
The original object is not mutated, instead it is cloned and the clone
is mutated
@param {!Object.<string, !PBase>} fields: a PObj whose fields will be added to the Pyret base
If any of the fields exist, they will be overwritten with the new value
@return {!PBase} the extended object
*/
function extendWith(fields) {
/**@type {!Object}*/
var newDict = Object.create(this.dict);
/**@type {!boolean}*/
var allNewFields = true;
for(var field in fields) {
if(hasProperty(this.dict, field)) {
allNewFields = false;
if(isRef(this.dict[field])) {
thisRuntime.ffi.throwMessageException("Cannot update ref field " + field);
}
}
newDict[field] = fields[field];
}
var newObj = this.updateDict(newDict, allNewFields);
return newObj;
}
function brandClone(newObj, obj, b) {
newObj.dict = obj.dict;
if (b in obj.brands) {
newObj.brands = obj.brands;
}
else {
newObj.brands = Object.create(obj.brands);
newObj.brands[b] = true;
newObj.brands.brandCount++;
}
return newObj;
}
var noBrands = { brandCount: 0 };
/**
The base of all pyret values
@constructor
*/
function PBase() {
/**@type {!Object.<string, Boolean>}*/
this.brands = noBrands;
/**@type {!Object.<string, !PBase>}*/
this.dict = emptyDict;
}
/**@type {!Object.<string, !PBase>}*/
PBase.prototype.dict = emptyDict;
/**@type {!function(!Object.<string, !PBase>) : !PBase}*/
PBase.prototype.extendWith = extendWith;
/**
Sets up Inheritance with a function call
This needs to be done socompiler recognizes it
@param {Function} sub the class that will become a subclass
@param {Function} from the class that sub will subclass
*/
function inherits(sub, from) {
sub.prototype = Object.create(from.prototype);
}
//Set up heirarchy
//We need to set it up before all the other classes
inherits(PNothing, PBase);
inherits(PObject, PBase);
inherits(PFunction, PBase);
inherits(PMethod, PBase);
inherits(POpaque, PBase);
inherits(PTuple, PBase);
/**
Tests whether a JS Object has a property
@param {!Object} obj the object to test
@param {!string} p the property to look for
@return {boolean} true if obj has property p, false otherwise
*/
function hasProperty(obj, p) {
return p in obj;
}
/**
Tests whether a JS Object has a property, but not on
any of its prototypes.
Useful for objects that lack the .hasOwnProperty method
@param {!Object} obj the object to test
@param {!string} p the property to look for
@return {boolean} true if obj has property p, false otherwise
*/
function hasOwnProperty(obj, p) {
return Object.prototype.hasOwnProperty.call(obj, p);
}
var parameters = Object.create(null);
function getParam(param) {
if(hasOwnProperty(parameters, param)) {
return parameters[param];
}
else {
throw new Error("Parameter " + param + " not defined");
}
}
function setParam(param, val) {
parameters[param] = val;
}
function hasParam(param) {
return param in parameters;
}
function getParamOrSetDefault(param, defVal) {
if (hasParam(param))
return getParam(param);
setParam(param, defVal);
return defVal;
}
function clearParam(param) {
delete parameters[param];
}
/**
Get the brands on an object
@param {!PBase} obj the object to get the brands of
@return {Object.<string,!Boolean>}
*/
function getBrands(obj) {
return obj.brands;
}
var getProto = Object.getPrototypeOf;
/**
Get the fields in an object.
@param {!PBase} obj the object to get the fields of
@return {Array.<string>}
*/
function getFields(obj) {
var fieldsObj = Object.create(null);
var fields = [];
var currentProto = obj.dict;
while(currentProto !== null) {
var keys = Object.keys(currentProto);
for (var i = 0; i < keys.length; i++)
fieldsObj[keys[i]] = true;
currentProto = getProto(currentProto);
}
fields = Object.keys(fieldsObj)
return fields;
}
var emptyDict = Object.create(null);
/**Tests whether an object is a PBase
@param {Object} obj the item to test
@return {boolean} true if object is a PBase
*/
function isBase(obj) { return obj instanceof PBase; }
function renderValueSkeleton(val, values) {
if (thisRuntime.ffi.isVSValue(val)) { return values.pop(); } // double-check order!
else if (thisRuntime.ffi.isVSStr(val)) { return thisRuntime.unwrap(thisRuntime.getField(val, "s")); }
else if (thisRuntime.ffi.isVSCollection(val)) {
var name = thisRuntime.unwrap(thisRuntime.getField(val, "name"));
var items = thisRuntime.ffi.toArray(thisRuntime.getField(val, "items"));
var s = "[" + name + ": ";
for (var i = 0; i < items.length; i++) {
if (i > 0) { s += ", "; }
s += renderValueSkeleton(items[i], values);
}
return s + "]";
} else if (thisRuntime.ffi.isVSConstr(val)) {
var name = thisRuntime.unwrap(thisRuntime.getField(val, "name"));
var items = thisRuntime.ffi.toArray(thisRuntime.getField(val, "args"));
var s = name + "(";
for (var i = 0; i < items.length; i++) {
if (i > 0) { s += ", "; }
s += renderValueSkeleton(items[i], values);
}
return s + ")";
} else if (thisRuntime.ffi.isVSSeq(val)) {
var items = thisRuntime.ffi.toArray(thisRuntime.getField(val, "items"));
var s = "";
for (var i = 0; i < items.length; i++) {
s += renderValueSkeleton(items[i], values);
}
return s;
} else if (thisRuntime.ffi.isVSTable(val)) {
// Do this for now until we decide on a string
// representation
if (util.isBrowser()) {
return "<table>";
}
if (!AsciiTable){
AsciiTable = require("ascii-table");
}
var headers = thisRuntime.getField(val, "headers");
var rowSkel = thisRuntime.getField(val, "rows");
headers = headers.map(function(h){ return renderValueSkeleton(h, []); });
var rows = [];
for (var i = 0; i < rowSkel.length; i++) {
var curRow = [];
for (var j = 0; j < headers.length; j++) {
var v = renderValueSkeleton(rowSkel[i][j], values);
if (v.length > 40) {
curRow.push(v.substr(0, 35) + "[...]");
} else {
curRow.push(v);
}
}
rows.push(curRow);
}
return new AsciiTable().fromJSON({
heading: headers,
rows: rows
}).toString();
}
}
var DefaultReprMethods = {
"string": String,
"number": String,
"boolean": String,
"nothing": function(val) { return "nothing"; },
"function": function(val) { return "<function>"; },
"method": function(val) { return "<method>"; },
"cyclic": function(val) { return val; },
"opaque": function(val) {
// if (thisRuntime.imageLib.isImage(val.val)) {
// return "<image (" + String(val.val.getWidth()) + "x" + String(val.val.getHeight()) + ")>";
// } else {
return "<internal value>";
// }
},
"tuple": function(t, pushTodo) {
pushTodo(undefined, undefined, undefined, Array.prototype.slice.call(t.vals), "render-tuple");
},
"render-tuple": function(top) {
var s = "{ ";
for(var i = top.done.length - 1; i >= 0; i--) {
if(i < top.done.length - 1) { s += "; "; }
s += top.done[i];
}
s += " }";
return s;
},
"object": function(val, pushTodo) {
var keys = [];
var vals = [];
for (var field in val.dict) {
keys.push(field); // NOTE: this is reversed order from the values,
vals.unshift(val.dict[field]); // because processing will reverse them back
}
pushTodo(undefined, val, undefined, vals, "render-object", { keys: keys });
},
"render-object": function(top) {
var s = "{";
for (var i = 0; i < top.extra.keys.length; i++) {
if (i > 0) { s += ", "; }
s += top.extra.keys[i] + ": " + top.done[i];
}
s += "}";
return s;
},
"ref": function(val, implicit, pushTodo) {
pushTodo(undefined, undefined, val, [getRef(val)], "render-ref", { implicit: implicit });
},
"render-ref": function(top) {
var s = "";
if (top.extra.implicit) {
s += top.done[0];
} else {
s += "ref(" + top.done[0] + ")";
}
return s;
},
"data": function(val, pushTodo) {
var vals = [];
if (val.$constructor.$fieldNames) {
for (var i = 0; i < val.$constructor.$fieldNames.length; i++) {
vals[i] = val.dict[val.$constructor.$fieldNames[i]];
}
}
pushTodo(undefined, val, undefined, vals, "render-data",
{ arity: val.$arity, implicitRefs: val.$mut_fields_mask,
fields: val.$constructor.$fieldNames, constructorName: val.$name });
},
"render-data": function(top) {
var s = top.extra.constructorName;
// Sentinel value for singleton constructors
if(top.extra.arity !== -1) {
s += "(";
for(var i = top.done.length - 1; i >= 0; i--) {
if(i < top.done.length - 1) { s += ", "; }
s += top.done[i];
}
s += ")";
}
return s;
},
"array": function(val, pushTodo) {
pushTodo(val, undefined, undefined, Array.prototype.slice.call(val), "render-array");
},
"render-array": function(top) {
var s = "[raw-array: ";
for(var i = top.done.length - 1; i >= 0; i--) {
if(i < top.done.length - 1) { s += ", "; }
s += top.done[i];
}
s += "]";
return s;
},
"valueskeleton": function(val, output, pushTodo) {
// NOTE: this is the eager version;
// a lazy version would skip getting the skeleton values altogether
// console.trace();
// throw new Error("How did we get here?");
var values = thisRuntime.ffi.skeletonValues(output);
pushTodo(undefined, val, undefined, values, "render-valueskeleton",
{ skeleton: output });
},
"render-valueskeleton": function(top) {
var skel = top.extra.skeleton;
top.extra.skeleton = undefined;
return renderValueSkeleton(skel, top.done);
}
};
var ReprMethods = {};
ReprMethods["_torepr"] = Object.create(DefaultReprMethods);
ReprMethods["_torepr"]["string"] = function(str) {
return '"' + replaceUnprintableStringChars(String(str)) + '"';
};
ReprMethods["_tostring"] = Object.create(DefaultReprMethods);
ReprMethods["$cli"] = Object.create(DefaultReprMethods);
ReprMethods["$cli"]["function"] = function(val) { return "<function:" + val.name + ">"; }
ReprMethods["$cli"]["method"] = function(val) { return "<method:" + val.name + ">"; }
ReprMethods.createNewRenderer = function createNewRenderer(name, base) {
if (ReprMethods[name]) { return false; }
ReprMethods[name] = Object.create(base);
return true;
}
/********************
Getting Fields
********************/
/**
Gets the field from an object of the given name
-If field is a method, it binds self correctly and returns a function
-If field is a placeholder, it calls get on the placeholder
-If field is a mutable -> error
-If field undefined -> error
-Otherwise, returns field value
@param {PBase} val
@param {string} field
@return {!PBase}
**/
function getFieldLocInternal(val, field, loc, isBang) {
if(val === undefined) {
if (thisRuntime.ffi === undefined || thisRuntime.ffi.throwInternalError === undefined) {
// console.trace();
throw Error("FFI or thisRuntime.ffi.throwInternalError is not yet defined, and lookup of field " + field + " on undefined failed at location " + JSON.stringify(loc));
} else {
thisRuntime.ffi.throwInternalError("Field lookup on undefined ", thisRuntime.ffi.makeList([field]));
}
}
if(!isObject(val)) {
if (val.$constrFor !== undefined) {
thisRuntime.ffi.throwLookupConstructorNotObject(makeSrcloc(loc), val.$constrFor, field);
}
thisRuntime.ffi.throwLookupNonObject(makeSrcloc(loc), val, field);
}
var fieldVal = val.dict[field];
if(fieldVal === undefined) {
if (thisRuntime.ffi === undefined || thisRuntime.ffi.throwFieldNotFound === undefined) {
throw Error("FFI or thisRuntime.ffi.throwFieldNotFound is not yet defined, and lookup of field " + field + " on " + toReprJS(val, ReprMethods._torepr) + " failed at location " + JSON.stringify(loc));
} else {
throw thisRuntime.ffi.throwFieldNotFound(makeSrcloc(loc), val, field);
}
}
else if(isRef(fieldVal)){
if(!isBang) {
return fieldVal;
// NOTE(joe Aug 8 2014): This is a design decision whether we
// want this to be an error or not
// thisRuntime.ffi.throwMessageException("Got ref in dot lookup");
}
return getRef(fieldVal);
}
else if(isMethod(fieldVal)){
var curried = fieldVal['meth'](val);
return makeFunction(curried, field);
}
else {
return fieldVal;
}
}
function getFieldLoc(obj, field, loc) {
return getFieldLocInternal(obj, field, loc, false);
}
function getFieldRef(obj, field, loc) {
return getFieldLocInternal(obj, field, loc, true);
}
function getField(obj, field) {
return thisRuntime.getFieldLoc(obj, field, ["runtime"]);
}
function getMaker(obj, makerField, exprLoc, constrLoc) {
var maker = isObject(obj) && obj.dict[makerField];
if (!isFunction(maker)) {
if (thisRuntime.ffi === undefined || thisRuntime.ffi.throwFieldNotFound === undefined) {
throw Error("FFI or thisRuntime.ffi., val, field is not yet defined, and lookup of field " + makeField + " on " + toReprJS(val, ReprMethods._torepr) + " failed at location " + JSON.stringify(constrLoc));
} else {
throw thisRuntime.ffi.throwConstructorSyntaxNonConstructor(makeSrcloc(exprLoc), makeSrcloc(constrLoc));
}
}
return maker;
}
function extendObj(loc, val, extension) {
if (!isObject(val)) { thisRuntime.ffi.throwExtendNonObject(makeSrcloc(loc), val); }
return val.extendWith(extension);
}
/**
Gets the field from an object of the given name
-Returns the raw field value
@param {!PBase} val
@param {string} field
@return {!PBase}
**/
function getColonField(val, field) {
return getColonFieldLoc(val, field, ["runtime"]);
}
function getColonFieldLoc(val, field, loc) {
if(val === undefined) { thisRuntime.ffi.throwInternalError("Field lookup on undefined ", [field]); }
if(!isObject(val)) { thisRuntime.ffi.throwLookupNonObject(makeSrcloc(loc), val, field); }
var fieldVal = val.dict[field];
if(fieldVal === undefined) {
thisRuntime.ffi.throwFieldNotFound(makeSrcloc(loc), val, field);
}
else {
return fieldVal;
}
}
/**
@constructor
*/
function POpaque(val, equals) {
this.val = val;
this.equals = equals;
/**@type {!Object.<string, Boolean>}*/
this.brands = noBrands;
}
POpaque.prototype = Object.create(PBase.prototype);
function makeOpaque(val, equals) { return new POpaque(val, equals); }
function isOpaque(val) { return val instanceof POpaque; }
/*********************
Nothing
**********************/
/**
Pyret Nothing
Represents the 'nothing' value in pyret
@constructor
@extends {PBase}
**/
function PNothing() {
/**@type {!Object.<string, !PBase>}*/
this.dict = emptyDict;
/**@type {!Object.<string, Boolean>}*/
this.brands = noBrands;
}
/**Clones the nothing
@param {!String} b The brand
@return {!PNothing} With same dict
*/
PNothing.prototype.brand = function(b) {
var newNoth = makeNothing();
return brandClone(newNoth, this, b);
};
/**Tests whether an object is a PNothing
@param {Object} obj the item to test
@return {boolean} true if object is a PNothing
*/
function isNothing(obj) { return obj instanceof PNothing; }
/**Makes a nothing
@return {!PNothing}
*/
function makeNothing() {return new PNothing();}
var nothing = makeNothing();
/*********************
Number
**********************/
/**Tests whether an object is a PNumber
@param {Object} obj the item to test
@return {boolean} true if object is a PNumber
*/
var isNumber = jsnums.isPyretNumber;
function isJSNumber(obj) {
return typeof obj === "number";
}
/**Makes a PNumber using the given bignum
@param {Bignum} n the number the PNumber will contain
@return {!PNumber} with value n
*/
function makeNumberBig(n) {
return n;
}
/**Makes a PNumber using the given JSNum
@param {number} n the number the PNumber will contain
@return {!PNumber} with value n
*/
function makeNumber(n) {
return jsnums.fromFixnum(n, NumberErrbacks);
}
/**Makes a PNumber using the given string
@param {string} s
@return {!PNumber} with value n
*/
function makeNumberFromString(s) {
var result = jsnums.fromString(s, NumberErrbacks);
if(result === false) {
thisRuntime.ffi.throwMessageException("Could not create number from: " + s);
}
return result;
}
/*********************
String
**********************/
/**Tests whether an object is a PString
@param {Object} obj the item to test
@return {boolean} true if object is a PString
*/
function isString(obj) {
return typeof obj === 'string';
}
/**Makes a PString using the given s
@param {string} s the string the PString will contain
@return {!PString} with value s
*/
function makeString(s) {
if(typeof s !== "string") { throw Error("Non-string given to makeString " + JSON.stringify(s)); }
return s;
}
/*********************
Boolean
**********************/
//Boolean Singletons
var pyretTrue = true;
var pyretFalse = false;
/**Makes a PBoolean using the given s
@param {boolean} b the Boolean the PBoolean will contain
@return {!PBoolean} with value b
*/
function makeBoolean(b) {
return b;
}
function isBoolean(b) {
return b === !!b;
}
/**Tests whether the boolean is equal to the singleton true value
@param {PBoolean} b
@return {boolean}
*/
function isPyretTrue(b) {
return b === pyretTrue;
}
function isPyretFalse(b) {
return b === pyretFalse;
}
function checkPyretTrue(b) {
checkBoolean(b);
return b === pyretTrue;
}
/*********************
Function
**********************/
/**The representation of a function
@constructor
@extends {PBase}
@param {Function} fun the function body
*/
function PFunction(fun, arity, name) {
/**@type {Function}*/
this.app = fun;
/**@type {string}*/
this.name = name || "<anonymous function>";
}
/**Clones the function
@param {!string} b The brand to add
@return {!PFunction} With same app and dict
*/
PFunction.prototype.brand = function(b) {
var newFun = makeFunction(this.app, this.name);
return brandClone(newFun, this, b);
};
/**Tests whether an object is a PFunction
@param {Object} obj the item to test
@return {boolean} true if object is a PFunction
*/
function isFunction(obj) {return obj instanceof PFunction; }
/**Makes a PFunction using the given n
@param {Function} fun The JS function that represents the body of the function, must contain at least one arg, which represents self
@return {!PFunction} with app of fun
*/
function makeFunction(fun, name) {
return new PFunction(fun, fun.length, name);
}
function makeFunctionArity(fun, arity, name) {
return new PFunction(fun, arity, name);
}
/*********************
Method
**********************/
/**The representation of a method
@constructor
@param {Function} meth
@param {Function} full_meth
@extends {PBase}
*/
function PMethod(meth, full_meth, name) {
/**@type {Function}*/
this['meth'] = meth;
/**@type {Function}*/
this['full_meth'] = full_meth;
/**@type {string}*/
this.name = name || "<anonymous method>";
}
/**Clones the method
@param {!string} b The brand to add
@return {!PMethod} With same meth and dict
*/
PMethod.prototype.brand = function(b) {
var newMeth = makeMethod(this['meth'], this['full_meth'], this['name']);
return brandClone(newMeth, this, b);
};
/**Tests whether an object is a PMethod
@param {Object} obj the item to test
@return {boolean} true if object is a PMethod
*/
function isMethod(obj) { return obj instanceof PMethod; }
/**Makes a PMethod using the given function
The function first argument should be self
@param {Function} meth The Curried JS function that represents the body of the method
@param {Function} full_meth The Full JS function that represents the body of the method
@return {!PMethod} with app of fun
*/
function makeMethod(meth, full_meth, name) {
return new PMethod(meth, full_meth, name);
}
var appN = function(obj) {
var that = this;
return function() { return that.full_meth(obj, ...arguments); }
}
function maybeMethodCall0(obj, fieldname, loc) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app();
}
}
function maybeMethodCall1(obj, fieldname, loc, arg) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, arg);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(arg);
}
}
function maybeMethodCall2(obj, fieldname, loc, arg1, arg2) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, arg1, arg2);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(arg1, arg2);
}
}
function maybeMethodCall3(obj, fieldname, loc, arg1, arg2, arg3) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, arg1, arg2, arg3);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(arg1, arg2, arg3);
}
}
function maybeMethodCall4(obj, fieldname, loc, arg1, arg2, arg3, arg4) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, arg1, arg2, arg3, arg4);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(arg1, arg2, arg3, arg4);
}
}
function maybeMethodCall5(obj, fieldname, loc, arg1, arg2, arg3, arg4, arg5) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, arg1, arg2, arg3, arg4, arg5);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(arg1, arg2, arg3, arg4, arg5);
}
}
function maybeMethodCall6(obj, fieldname, loc, arg1, arg2, arg3, arg4, arg5, arg6) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, arg1, arg2, arg3, arg4, arg5, arg6);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(arg1, arg2, arg3, arg4, arg5, arg6);
}
}
function maybeMethodCall7(obj, fieldname, loc, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
}
function maybeMethodCall(obj, fieldname, loc, ...args) {
var R = thisRuntime;
var field = R.getColonFieldLoc(obj,fieldname,loc);
if(thisRuntime.isMethod(field)) {
return field.full_meth(obj, ...args);
}
else {
if(!(R.isFunction(field))) {
R.ffi.throwNonFunApp(loc,field);
}
return field.app(...args);
}
}
function makeMethodFromFun(meth, name) {
return new PMethod(appN, meth, name);
}
var makeMethodN = makeMethodFromFun;
var makeMethod0 = makeMethodN;
var makeMethod1 = makeMethodN;
var makeMethod2 = makeMethodN;
var makeMethod3 = makeMethodN;
var makeMethod4 = makeMethodN;
var makeMethod5 = makeMethodN;
var makeMethod6 = makeMethodN;
var makeMethod7 = makeMethodN;
var makeMethod8 = makeMethodN;
function callIfPossible0(L, fun, obj) {
if (isMethod(fun)) {
return fun.full_meth(obj);
} else if (isFunction(fun)) {
return fun.app();
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible1(L, fun, obj, v1) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1);
} else if (isFunction(fun)) {
return fun.app(v1);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible2(L, fun, obj, v1, v2) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1, v2);
} else if (isFunction(fun)) {
return fun.app(v1, v2);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible3(L, fun, obj, v1, v2, v3) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1, v2, v3);
} else if (isFunction(fun)) {
return fun.app(v1, v2, v3);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible4(L, fun, obj, v1, v2, v3, v4) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1, v2, v3, v4);
} else if (isFunction(fun)) {
return fun.app(v1, v2, v3, v4);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible5(L, fun, obj, v1, v2, v3, v4, v5) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1, v2, v3, v4, v5);
} else if (isFunction(fun)) {
return fun.app(v1, v2, v3, v4, v5);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible6(L, fun, obj, v1, v2, v3, v4, v5, v6) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1, v2, v3, v4, v5, v6);
} else if (isFunction(fun)) {
return fun.app(v1, v2, v3, v4, v5, v6);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible7(L, fun, obj, v1, v2, v3, v4, v5, v6, v7) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1, v2, v3, v4, v5, v6, v7);
} else if (isFunction(fun)) {
return fun.app(v1, v2, v3, v4, v5, v6, v7);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
function callIfPossible8(L, fun, obj, v1, v2, v3, v4, v5, v6, v7, v8) {
if (isMethod(fun)) {
return fun.full_meth(obj, v1, v2, v3, v4, v5, v6, v7, v8);
} else if (isFunction(fun)) {
return fun.app(v1, v2, v3, v4, v5, v6, v7, v8);
} else {
thisRuntime.ffi.throwNonFunApp(L, fun);
}
}
var GRAPHABLE = 0;
var UNGRAPHABLE = 1;
var SET = 2;
var FROZEN = 3;
function PRef() {
this.state = GRAPHABLE;
this.anns = makePAnnList([]);
this.value = undefined;
}
function makeGraphableRef() {
return new PRef();
}
function makeRef(ann, loc) {
var loc = typeof loc === "undefined" ? ["references"] : loc;
var r = new PRef();
addRefAnn(r, ann, loc);
r.state = UNGRAPHABLE;
return r;
}
function makeUnsafeSetRef(ann, value, loc) {
var r = new PRef();
r.state = SET;
r.anns = makePAnnList([{ann: ann, loc: loc}]);
r.value = value;
return r;