forked from elavoie/photon-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
1758 lines (1544 loc) · 52.2 KB
/
core.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
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
// TODO:
// * __str__ protocol should handle circular data structures
// * __str__ of function should return the source code or [function native]
// - serialize at a low-level such that the resulting file is sufficient to start the system on v8
// - serialize at a high-level such that the underlying representation of objects is abstracted
// - print a textual representation of the object at a low-level for debugging the underlying representation
// - print a textual representation of the object at a high-level for debugging the application behavior
//
// * Properties on maps should have a flag to specify if they are enumerable
var options = {
verbose:false,
use_ic:false,
trace_ic:false,
trace_ic_tracker:false
}
var root = {
arguments:{},
array:{},
cell:{},
function:{},
global:{},
map:{},
mapMap:{},
object:{},
primitive:{},
regexp:{},
boolean:null,
number:null,
string:null,
date:null,
reservedProperty:{},
defaultCall:null
};
var global = this;
[
"__clone__",
"__delete__",
"__get__",
"__in__",
"__instanceof__",
"__itr__",
"__new__",
"__set__",
"__str__",
"__type__",
"__typeof__",
"__not_understood__",
"hasOwnProperty",
"valueOf",
"toString",
"forEach",
"indexOf",
"lastIndexOf",
"map",
"reverse",
"shift",
"splice",
"unshift"
].forEach(function (p) { root.reservedProperty[p] = true; });
function copy(obj)
{
if (typeof obj === "object" && obj instanceof Array)
{
return obj.slice(0);
} else
{
var newObj = {};
for (var p in obj)
{
newObj[p] = obj[p];
}
return newObj;
}
}
function assert(bool, msg)
{
if (msg === undefined)
msg = "Error";
if (bool !== true)
{
error(msg);
}
}
function error(msg)
{
throw new Error(msg);
}
function isPrimitive(x)
{
return typeof x === "number" || typeof x === "string" || x === undefined || x === null || x === true || x === false;
}
function getProp(obj, name)
{
return obj.values[obj.map.payload.properties[name]];
}
function isMap(obj) {
return !isPrimitive(obj) && obj.map === obj.map.map;
}
function bind(msg, rcv) {
if (isPrimitive(rcv)) {
return bind(msg, root.primitive);
} else if (rcv === rcv.map && msg === "lookup") {
return getProp(root.map, "lookup");
}
while (rcv !== null)
{
if (Object.prototype.hasOwnProperty.call(rcv.map.payload.properties, msg))
var offset = rcv.map.payload.properties[msg];
else
var offset = undefined;
if (offset !== undefined )
{
if (typeof offset === "number" && typeof rcv.values[offset].payload.code === "function")
{
return rcv.values[offset];
} else if (typeof offset === "object" && typeof (offset.payload.code) === "function" )
{
return offset;
}
}
rcv = rcv.prototype;
}
return null;
}
function send(rcv, msg) {
var method = bind(msg, rcv);
var args = Array.prototype.slice.call(arguments, 2).map(function (x) {
if (isPrimitive(x) || x.prototype !== undefined) {
return x;
} else if (x instanceof Error) {
return x.toString();
} else {
error("Invalid object type '" + typeof x + "' for object '" + x + "' in send '" + String(msg) + "'");
}
});
if (method === null) {
return send.call(null, rcv, "__not_understood__", msg, arr(args));
}
if (typeof method.payload.code !== "function") {
error("Invalid method implementation");
}
//var args = [rcv, method].concat(args);
//return method.payload.code.apply(null, args);
if (rcv === getProp(root.function, "call") && msg === "call") {
//print("---> Base case occurred for msg '" + msg + "'");
return rcv.payload.code.apply(null, [args[0], rcv].concat(args.slice(1)));
} else if (isMap(rcv) && msg === "lookup") {
//print("---> Base case occurred for msg '" + msg + "'");
return method.payload.code.apply(null, [rcv, method].concat(args));
} else {
//print("Regular case for msg '" + msg + "'");
//return method.payload.code.apply(null, args);
args = [method, "call", rcv].concat(args);
return send.apply(null, args);
}
}
// Does not reify the call protocol (opaque to modification of the function calling protocol)
function baseSend(rcv, msg) {
var method = bind(msg, rcv);
var args = Array.prototype.slice.call(arguments, 2).map(function (x) {
if (isPrimitive(x) || x.prototype !== undefined) {
return x;
} else if (x instanceof Error) {
return x.toString();
} else {
error("Invalid object type '" + typeof x + "' for object '" + x + "' in send '" + String(msg) + "'");
}
});
if (method === null) {
return send.call(null, rcv, "__not_understood__", msg, arr(args));
}
if (typeof method.payload.code !== "function") {
error("Invalid method implementation");
}
return method.payload.code.apply(null, [rcv, method].concat(args));
}
// ------------------------- Cache invalidation support -----------------------
var tracker;
(function () {
// Use objects as hash tables
var objMsg2Cache = {};
var cache2ObjMsg = {};
var counter = 0;
var verbose = options.trace_ic_tracker;
function hash(obj) {
if (obj.hash === undefined) {
obj.hash = counter++;
}
return obj.hash;
}
tracker = {
addCacheLink:function (obj, msg, cacheId, cacheData) {
while (obj !== null) {
if (Object.prototype.hasOwnProperty.call(obj.map.payload.properties, msg)) {
break;
}
obj = obj.prototype;
}
if ((msg === "call" || msg === "__memoize__") && obj === root.function) {
if (verbose) print("Ignoring tracking information for message " + msg + " because it was found on root.function");
return;
}
var objHash = hash(obj);
if (verbose) print("Adding tuple (" + objHash + "," + msg + "," + cacheId + ")");
if (objMsg2Cache[objHash] === undefined) {
objMsg2Cache[objHash] = {};
}
if (objMsg2Cache[objHash][msg] === undefined) {
objMsg2Cache[objHash][msg] = {};
}
objMsg2Cache[objHash][msg][cacheId] = cacheData;
// Remember the (objHash,msg) container for faster reverse lookup
if (cache2ObjMsg[cacheId] === undefined) {
cache2ObjMsg[cacheId] = {};
}
cache2ObjMsg[cacheId][objHash+","+msg] = objMsg2Cache[objHash][msg];
},
flushCaches:function (obj, msg) {
if ((msg === "call" || msg === "__memoize__") && obj === root.function) {
if (verbose) print("Flushing all caches");
var cacheIds = {};
for (var objHash in objMsg2Cache) {
for (var msg in objMsg2Cache[objHash]) {
for (var cacheId in objMsg2Cache[objHash][msg]) {
cacheIds[cacheId] = true;
}
}
}
var keys = [];
for (var cacheId in cacheIds) {
keys.push(cacheId);
}
} else {
var objHash = hash(obj);
var keys = [];
if (objMsg2Cache[objHash] !== undefined && objMsg2Cache[objHash][msg] !== undefined) {
for (var cacheId in objMsg2Cache[objHash][msg]) {
keys.push(cacheId);
}
}
}
for (var i = 0; i < keys.length; ++i) {
var cacheId = keys[i];
this.removeCacheLinks(cacheId);
if (verbose) print("Resetting " + cacheId);
global[cacheId] = initState;
}
},
removeCacheLinks:function (cacheId) {
var keys = [];
for (var objHashMsg in cache2ObjMsg[cacheId]) {
if (verbose) print("Removing tuple (" + objHashMsg + "," + cacheId + ")");
keys.push(objHashMsg);
}
// cacheData should be the same for all entries, so we should
// reset it only once
if (keys.length > 0) {
var container = cache2ObjMsg[cacheId][keys[0]];
var cacheData = container[cacheId];
global["dataCache"+cacheData[0]] = cacheData;
global["codeCache"+cacheData[0]] = initState;
}
for (var i = 0; i < keys.length; ++i) {
var k = keys[i];
var container = cache2ObjMsg[cacheId][k];
delete cache2ObjMsg[cacheId][k];
delete container[cacheId];
}
},
setVerbosity:function (bool) {
verbose = bool;
}
};
})();
function initState(rcv, cacheData) {
var verbose = options.trace_ic;
var codeCacheName = "codeCache"+cacheData[0];
var dataCacheName = "dataCache"+cacheData[0];
var args = Array.prototype.slice.call(arguments, 2).map(function (x) {
if (isPrimitive(x) || x.prototype !== undefined) {
return x;
} else if (x instanceof Error) {
return x.toString();
} else {
error("Invalid object type '" + typeof x + "' for object '" + x + "' in send '" + String(msg) + "'");
}
});
var msg = cacheData[1];
var method = bind(msg, rcv);
if (method === null || isPrimitive(rcv)) {
// No caching can done, retry with a regular send
return send.apply(null, [rcv, msg].concat(args));
}
var callFn = bind("call", method);
var memoizedCallFn = baseSend(callFn, "__memoize__", method, callFn, arr([rcv].concat(args)), arr(cacheData));
tracker.addCacheLink(rcv, msg, codeCacheName, cacheData);
tracker.addCacheLink(method, "call", codeCacheName, cacheData);
tracker.addCacheLink(callFn, "__memoize__", codeCacheName, cacheData);
if (memoizedCallFn === root.defaultCall) {
var memoizedMethod = baseSend(method, "__memoize__", rcv, method, arr(args), arr(cacheData));
tracker.addCacheLink(method, "__memoize__", codeCacheName, cacheData);
if (rcv === root_global) {
if (verbose) print("-- caching global function call " + msg + " at " + codeCacheName);
global[codeCacheName] = memoizedMethod.payload.code;
global[dataCacheName] = memoizedMethod;
} else {
if (verbose) print("-- caching method call " + msg + " at " + codeCacheName);
//global[codeCacheName] = variableRcv(args.length);
global[codeCacheName] = memoizedMethod.payload.code;
cacheData[2] = rcv.map;
cacheData[3] = memoizedMethod;
if (memoizedMethod === undefined) {
throw Error("Invalid memoized method");
}
}
} else {
if (verbose) print("-- caching call function");
if (args.length !== 1) throw Error("Unhandled arguments number of " + args.length + " when caching call method");
global[codeCacheName] = callState1;
cacheData[2] = rcv.map
cacheData[3] = method;
cacheData[4] = memoizedCallFn;
}
return callFn.payload.code.apply(null, [method, callFn, rcv].concat(args));
}
function bailout(rcv, cacheData) {
if (rcv === undefined || rcv === null ) {
throw new Error("Invalid message for '" + rcv + "'");
}
// Remove cache from invalidation set(s) and reset data cache
tracker.removeCacheLinks("codeCache"+cacheData[0]);
cacheData.length = 2;
// Setup cache
return initState.apply(null, [rcv, cacheData].concat(Array.prototype.slice.call(arguments, 2)));
}
(function () {
var store = [];
variableRcv = function (argNb) {
if (store[argNb] !== undefined) return store[argNb];
var argList = [];
for (var i = 0; i < argNb; ++i) {
argList.push("arg" + i);
}
var argStr = ["rcv", "cacheData"].concat(argList).join(", ");
var methodArgStr = ["rcv", "method"].concat(argList).join(", ");
var code = "" +
" if ( rcv.map === cacheData[2]) {\n" +
" var method = cacheData[3];\n" +
" return method.payload.code(" + methodArgStr + ");\n" +
" } else {\n" +
" return bailout(" + argStr + ");\n" +
" }\n";
store[argNb] = Function.apply(null, ["rcv", "cacheData"].concat(argList,[code]));
return store[argNb];
}
})();
function callState1(rcv, cacheData, arg0) {
if (rcv !== undefined && rcv !== null && rcv.map === cacheData[2]) {
return cacheData[4].payload.code(cacheData[3], cacheData[4], rcv, arg0);
} else {
return bailout(rcv, cacheData, arg0);
}
}
var wrapper_prototype = {
// To integrate correctly with the automatic conversion
toString:function () { return send(this, "__str__"); },
valueOf:function () { return send(this, "valueOf"); }
};
function wrap(o) {
o.__proto__ = wrapper_prototype;
return o;
};
function obj(proto, payload, props)
{
var map = objMap(props);
var values = [];
var offsets = map.payload.properties;
for (var p in offsets)
{
var v = props[p];
// Perform conversion of native values to
// the simulated object model
if (typeof v === "function")
{
values[offsets[p]] = bs_clos((function(p, v)
{
return function ($this, $closure)
{
return v.apply($this, Array.prototype.slice.call(arguments, 2));
};
})(p, v));
} else if (isPrimitive(v))
{
values[offsets[p]] = v;
} else if (typeof v === "object" && v.prototype !== undefined)
{
values[offsets[p]] = v;
} else
{
error("invalid property value");
}
}
return wrap({
// User-defined properties
values:values,
// Header values
prototype:proto,
map:map,
// Object payload
payload:payload
});
}
function arr(payload)
{
var a = obj(root.array, payload);
a.type = "array";
return a;
}
function indentStr(current)
{
var indent = "";
for (var i = 0; i < current; ++i)
{
indent += " ";
}
return indent;
}
function objMap(props)
{
var count = 0;
var nextOffset = 0;
var properties = {};
for (var p in props)
{
count++;
properties[p] = nextOffset++;
}
return newMap = {
// User-defined properties
values:[],
// Header values
prototype:root.map,
map:root.mapMap,
// Object payload
payload:{
count:count,
nextOffset:nextOffset,
properties:properties
}
};
}
function bs_clos(f)
{
return obj(root.function, {code:f, cells:[]});
}
function clos(f, id)
{
var g = bs_clos(f);
if (id !== undefined)
send(g, "__set__", "__id__", id);
return g;
}
function regexp(e)
{
return obj(root.regexp, {code:RegExp(e)});
}
function extend(obj, props)
{
for (var p in props)
{
obj[p] = props[p];
}
}
function unimplemented(name)
{
throw "Unimplemented '" + name + "' operation";
}
function __$apply__(fn, obj, args) {
if (args.type === "arguments") {
args = args.payload.map(function (x) {
return x.payload;
});
} else if (args.type === "array") {
args = args.payload;
} else {
error("Invalid args type '" + args.type + "'");
}
return fn.payload.code.apply(null, [obj, fn].concat(args));
}
extend(root.object, obj(null, null, {
"__clone__":function () {
var clone = obj(this, null);
clone.map = this.map;
clone.prototype = this.prototype;
clone.values = copy(this.values);
return clone;
},
"__delete__":function (name) {
var offset = send(this.map, "lookup", name);
if (offset === undefined)
{
return false;
} else
{
tracker.flushCaches(this, name);
this.map = send(this.map, "remove", name);
if (offset < this.values.length - 1)
this.values[offset] = this.values[this.values.length - 1];
this.values.length--;
return true;
}
},
"__get__":function (name) {
if (name === "__map__")
return this.map;
var offset;
var obj = this;
while (obj !== null)
{
offset = send(obj.map, "lookup", name);
if (offset !== undefined)
return obj.values[offset];
else
obj = obj.prototype;
}
return undefined;
},
"__in__":function (name) {
var it = send(this, "__itr__");
if (typeof name === "string")
var strName = name;
else if (typeof name === "number")
var strName = String(name);
else
error("Invalid name type");
while (send(it, "valid")) {
if (send(it, "get") === strName) {
return true;
}
send(it, "next");
}
return false;
},
"__instanceof__":bs_clos(function ($this, $closure, Fct) {
if (isPrimitive($this)) return false;
var prototype = send(Fct, "__get__", "prototype");
var obj = $this;
while (obj !== null) {
if (obj === prototype)
return true;
obj = obj.prototype;
}
return false;
}),
"__itr__":function () {
var _obj = this;
var _visited = {};
var _itr = send(_obj.map, "prop_itr");
return send(obj(root.object, null, {
"get":function () {
return send(_itr, "get");
},
"init":function () {
send(this, "next");
return this;
},
"next":function () {
var r = null;
while (r === null && send(this, "valid")) {
// If the object had a property added or deleted during
// iteration, its map will have changed so let's continue
// with the new map instead
if (_obj.map !== send(_itr, "__get__", "map")) {
_itr = send(_obj.map, "prop_itr");
}
// If the object's map has been traversed, let's move to the prototype
if (!send(_itr, "valid")) {
_obj = _obj.prototype;
if (_obj !== null)
_itr = send(_obj.map, "prop_itr");
}
// Let's find a property in the current map that has not been visited
// yet
while (r === null && send(_itr, "valid")) {
var p = send(_itr, "get");
if (_visited[p] !== true && root.reservedProperty[p] !== true) {
_visited[p] = true;
r = p;
} else {
send(_itr, "next");
}
}
}
},
"valid":function () {
return _obj !== null;
}
}), "init");
},
"__new__":function () {
return obj(this, null);
},
"__not_understood__":bs_clos(function ($this, $closure, msg, args)
{
throw new Error("Object " + $this + " has no method '" + msg + "'");
}),
"__set__":function (name, value) {
var offset = send(this.map, "lookup", name);
if (offset !== undefined)
{
tracker.flushCaches(this, name);
return this.values[offset] = value;
}
else
{
// Flush caches linked to parent's property name because
// this new property can mask the parent's property on the lookup chain
var obj = this.prototype;
while (obj !== null) {
if (send(obj.map, "lookup", name) !== undefined) {
tracker.flushCaches(obj, name);
break;
}
obj = obj.prototype;
}
this.map = send(this.map, "create", name);
return this.values[send(this.map, "lookup", name)] = value;
}
},
"__type__":function () {
return "object";
},
"__typeof__":function () {
return "object";
},
"__str__":function () {
return "[object Object]";
},
"hasOwnProperty":function (name) {
return send(this.map, "lookup", name) !== undefined || (this.type === "array" && name >= 0 && name < this.payload.length);
},
"valueOf":bs_clos(function ($this) {
print(send($this, "__type__"));
throw new Error("Unimplemented valueOf method");
}),
"toString":function () {
throw new Error("Unimplemented toString method");
}
}));
extend(root.mapMap, objMap());
extend(root.map, obj(root.object, {}, {
"__delete__":function (name) {
if (this.map === this)
{
return false;
} else
{
return send(send(root.object, "__get__", "__delete__"), "call", this, name);
}
},
"__new__":function () {
return objMap();
},
"prop_itr":function () {
var props = [];
var idx = 0;
for (var p in this.payload.properties)
{
if (Object.prototype.hasOwnProperty.call(this.payload.properties, p))
{
props.push(p);
}
}
return obj(root.object, null, {
"map":this,
"get":function () {
return props[idx];
},
"next":function () {
idx++;
},
"valid":function () {
return idx < props.length;
}
});
},
"__set__":function (name, value) {
var offset = send(this.map, "lookup", name);
if (this.map === this && offset === undefined)
{
return false;
} else
{
return send(send(root.object, "__get__", "__set__"), "call", this, name, value);
}
},
"__type__":function ()
{
return "map";
},
"create":function (name) {
var newMap = send(this, "__clone__");
newMap.payload = {
count:this.payload.count,
nextOffset:this.payload.nextOffset,
properties:copy(this.payload.properties)
};
newMap.payload.count++;
newMap.payload.properties[name] = newMap.payload.nextOffset++;
// Preserve circularity of map definition
if (this.map === this)
{
newMap.map = newMap;
}
return newMap;
},
"lookup":function (name) {
if (Object.prototype.hasOwnProperty.call(this.payload.properties, name))
return this.payload.properties[name];
else
return undefined;
},
"remove":function (name) {
var newMap = send(this, "__clone__");
newMap.payload = {
count:this.payload.count - 1,
nextOffset:this.payload.nextOffset - 1,
properties:copy(this.payload.properties)
};
// Move the last property in the
// slot of the delete property to preserve
// compactness
var lastOffset = newMap.payload.nextOffset;
var properties = newMap.payload.properties;
var lastName;
for (var p in properties)
{
if (properties[p] === lastOffset)
{
lastName = p;
}
}
stringName = name;
properties[lastName] = properties[stringName];
delete properties[stringName];
// Preserve circularity of map definition
if (this.map === this)
{
newMap.map = newMap;
}
return newMap;
}
}));
root.map.payload = root.map.map.payload;
root.map.map = root.map;
extend(root.function, obj(root.object, {code:function () {}, cells:[]}, {
"__ctor__":bs_clos(function ($this, $closure) {
var o = send(send($this, "__get__", "prototype"), "__new__");
var r = $this.payload.code.apply(o, [o, $this].concat(Array.prototype.slice.call(arguments, 2)));
if (isPrimitive(r))
return o;
else
return r;
}),
"__get__":function (name) {
// Lazy creation of the prototype property
if (name === "prototype" && !send(this, "hasOwnProperty", "prototype"))
{
var o = send(root.object, "__new__");
return send(this, "__set__", name, o);
} else if (name === "length") {
return this.payload.code.length - 2;
} else
{
return send(send(root.object, "__get__", "__get__"), "call", this, name);
}
},
"__memoize__":function () {
return this;
},
"__new__":function () {
return obj(root.function, {code:null, cells:[]});
},
"__str__":function () {
return String(this.payload.code);
},
"__type__":function () {
return "function";
},
"__typeof__":function () {
return "function";
},
"valueOf":function () {
return this;
},
"apply":function (rcv, args) {
assert((typeof this.payload.code) === "function");
if (args.type === "array")
args = args.payload;
else if (args.type === "arguments")
args = args.payload.map(function (x) { return x.payload; });
else
throw new Error("Invalid args argument for apply");
return this.payload.code.apply(null, [rcv, this].concat(args));
},
"call":function () {
assert(typeof this.payload.code === "function");
return this.payload.code.apply(null, [arguments[0], this].concat(Array.prototype.slice.call(arguments, 1)));
}
}));
extend(root.primitive, obj(root.object, null, {
"__get__":bs_clos(function ($this, $closure, name) {
if (typeof $this === "string")
{
if (name === "length")
return $this.length;
else if (typeof name === "number")
return $this[name];
else
return send(root.string, "__get__", name);
} else if (typeof $this === "number") {
return send(root.number, "__get__", name);
}
error("TypeError: Invalid __get__(" + name + ") operation on primitive value '" + $this + "' of type " + typeof $this);
}),
"toString":bs_clos(function ($this, $closure, x) {
return $this.toString(x);
}),
"valueOf":bs_clos(function ($this) {
return $this.valueOf();
}),
"__not_understood__":bs_clos(function ($this, $closure, msg, args) {
var rcv = $this;
if (typeof rcv === "string" || typeof rcv === "number" || typeof rcv === "boolean")
{
// Check the prototype object to see if a method was defined
var m = send(root[typeof rcv], "__get__", msg);
if (m !== undefined && send(m, "__type__") === "function") {
return m.payload.code.apply(null, [$this, m].concat(args.payload));
}
// Otherwise delegate to the underlying implementation
var m = rcv[msg];
if (m !== undefined && typeof m === "function") {
var r = m.apply(rcv, args.payload.map(function (obj) {
if (isPrimitive(obj))
return obj;
else if (send(obj, "__type__") === "regexp")
return obj.payload.code;
else if (send(obj, "__type__") === "function") {
return (function (f) {
// Wrap implementation function to match native calling conventions
return (function () {
var args = Array.prototype.slice.call(arguments, 0);
return f.payload.code.apply(null, [this, f].concat(args));
});
})(obj);
} else {
error("Unsupported object " + obj + " for primitive operation '" + msg + "'");
}
}));
if (isPrimitive(r))
return r;
else if (r instanceof Array) {
return arr(r.map(function (obj) {
assert(isPrimitive(obj), "Non-primitive return value " + obj);
return obj;