forked from mkaminsky11/codeyourcloud
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfer.js
More file actions
1431 lines (1305 loc) · 47.8 KB
/
Copy pathinfer.js
File metadata and controls
1431 lines (1305 loc) · 47.8 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
// Main type inference engine
// Walks an AST, building up a graph of abstract values and contraints
// that cause types to flow from one node to another. Also defines a
// number of utilities for accessing ASTs and scopes.
// Analysis is done in a context, which is tracked by the dynamically
// bound cx variable. Use withContext to set the current context.
// For memory-saving reasons, individual types export an interface
// similar to abstract values (which can hold multiple types), and can
// thus be used in place abstract values that only ever contain a
// single type.
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
return mod(exports, require("acorn"), require("acorn_loose"), require("walk"),
require("./def"), require("./signal"));
if (typeof define == "function" && define.amd) // AMD
return define(["exports", "acorn", "acorn_loose", "walk", "./def", "./signal"], mod);
mod(self.tern || (self.tern = {}), acorn, acorn, acorn.walk, tern.def, tern.signal); // Plain browser env
})(function(exports, acorn, acorn_loose, walk, def, signal) {
"use strict";
var toString = exports.toString = function(type, maxDepth, parent) {
return !type || type == parent ? "?": type.toString(maxDepth);
};
// A variant of AVal used for unknown, dead-end values. Also serves
// as prototype for AVals, Types, and Constraints because it
// implements 'empty' versions of all the methods that the code
// expects.
var ANull = exports.ANull = signal.mixin({
addType: function() {},
propagate: function() {},
getProp: function() { return ANull; },
forAllProps: function() {},
hasType: function() { return false; },
isEmpty: function() { return true; },
getFunctionType: function() {},
getType: function() {},
gatherProperties: function() {},
propagatesTo: function() {},
typeHint: function() {},
propHint: function() {}
});
function extend(proto, props) {
var obj = Object.create(proto);
if (props) for (var prop in props) obj[prop] = props[prop];
return obj;
}
// ABSTRACT VALUES
var WG_DEFAULT = 100, WG_NEW_INSTANCE = 90, WG_MADEUP_PROTO = 10, WG_MULTI_MEMBER = 5,
WG_CATCH_ERROR = 5, WG_GLOBAL_THIS = 90, WG_SPECULATIVE_THIS = 2;
var AVal = exports.AVal = function() {
this.types = [];
this.forward = null;
this.maxWeight = 0;
};
AVal.prototype = extend(ANull, {
addType: function(type, weight) {
weight = weight || WG_DEFAULT;
if (this.maxWeight < weight) {
this.maxWeight = weight;
if (this.types.length == 1 && this.types[0] == type) return;
this.types.length = 0;
} else if (this.maxWeight > weight || this.types.indexOf(type) > -1) {
return;
}
this.signal("addType", type);
this.types.push(type);
var forward = this.forward;
if (forward) withWorklist(function(add) {
for (var i = 0; i < forward.length; ++i) add(type, forward[i], weight);
});
},
propagate: function(target, weight) {
if (target == ANull || (target instanceof Type)) return;
if (weight && weight < WG_DEFAULT) target = new Muffle(target, weight);
(this.forward || (this.forward = [])).push(target);
var types = this.types;
if (types.length) withWorklist(function(add) {
for (var i = 0; i < types.length; ++i) add(types[i], target, weight);
});
},
getProp: function(prop) {
if (prop == "__proto__" || prop == "✖") return ANull;
var found = (this.props || (this.props = Object.create(null)))[prop];
if (!found) {
found = this.props[prop] = new AVal;
this.propagate(new PropIsSubset(prop, found));
}
return found;
},
forAllProps: function(c) {
this.propagate(new ForAllProps(c));
},
hasType: function(type) {
return this.types.indexOf(type) > -1;
},
isEmpty: function() { return this.types.length == 0; },
getFunctionType: function() {
for (var i = this.types.length - 1; i >= 0; --i)
if (this.types[i] instanceof Fn) return this.types[i];
},
getType: function(guess) {
if (this.types.length == 0 && guess !== false) return this.makeupType();
if (this.types.length == 1) return this.types[0];
return canonicalType(this.types);
},
makeupType: function() {
if (!this.forward) return null;
for (var i = this.forward.length - 1; i >= 0; --i) {
var hint = this.forward[i].typeHint();
if (hint && !hint.isEmpty()) {guessing = true; return hint;}
}
var props = Object.create(null), foundProp = null;
for (var i = 0; i < this.forward.length; ++i) {
var prop = this.forward[i].propHint();
if (prop && prop != "length" && prop != "<i>" && prop != "✖") {
props[prop] = true;
foundProp = prop;
}
}
if (!foundProp) return null;
var objs = objsWithProp(foundProp);
if (objs) {
var matches = [];
search: for (var i = 0; i < objs.length; ++i) {
var obj = objs[i];
for (var prop in props) if (!obj.hasProp(prop)) continue search;
if (obj.hasCtor) obj = getInstance(obj);
matches.push(obj);
}
var canon = canonicalType(matches);
if (canon) {guessing = true; return canon;}
}
},
typeHint: function() { return this.types.length ? this.getType() : null; },
propagatesTo: function() { return this; },
gatherProperties: function(f, depth) {
for (var i = 0; i < this.types.length; ++i)
this.types[i].gatherProperties(f, depth);
},
guessProperties: function(f) {
if (this.forward) for (var i = 0; i < this.forward.length; ++i) {
var prop = this.forward[i].propHint();
if (prop) f(prop, null, 0);
}
}
});
function canonicalType(types) {
var arrays = 0, fns = 0, objs = 0, prim = null;
for (var i = 0; i < types.length; ++i) {
var tp = types[i];
if (tp instanceof Arr) ++arrays;
else if (tp instanceof Fn) ++fns;
else if (tp instanceof Obj) ++objs;
else if (tp instanceof Prim) {
if (prim && tp.name != prim.name) return null;
prim = tp;
}
}
var kinds = (arrays && 1) + (fns && 1) + (objs && 1) + (prim && 1);
if (kinds > 1) return null;
if (prim) return prim;
var maxScore = 0, maxTp = null;
for (var i = 0; i < types.length; ++i) {
var tp = types[i], score = 0;
if (arrays) {
score = tp.getProp("<i>").isEmpty() ? 1 : 2;
} else if (fns) {
score = 1;
for (var j = 0; j < tp.args.length; ++j) if (!tp.args[j].isEmpty()) ++score;
if (!tp.retval.isEmpty()) ++score;
} else if (objs) {
score = tp.name ? 100 : 2;
} else if (prims) {
score = 1;
}
if (score >= maxScore) { maxScore = score; maxTp = tp; }
}
return maxTp;
}
// PROPAGATION STRATEGIES
function Constraint() {}
Constraint.prototype = extend(ANull, {
init: function() { this.origin = cx.curOrigin; }
});
var constraint = exports.constraint = function(props, methods) {
var body = "this.init();";
props = props ? props.split(", ") : [];
for (var i = 0; i < props.length; ++i)
body += "this." + props[i] + " = " + props[i] + ";";
var ctor = Function.apply(null, props.concat([body]));
ctor.prototype = Object.create(Constraint.prototype);
for (var m in methods) if (methods.hasOwnProperty(m)) ctor.prototype[m] = methods[m];
return ctor;
};
var PropIsSubset = constraint("prop, target", {
addType: function(type, weight) {
if (type.getProp)
type.getProp(this.prop).propagate(this.target, weight);
},
propHint: function() { return this.prop; },
propagatesTo: function() {
return {target: this.target, pathExt: "." + this.prop};
}
});
var PropHasSubset = exports.PropHasSubset = constraint("prop, type, originNode", {
addType: function(type, weight) {
if (!(type instanceof Obj)) return;
var prop = type.defProp(this.prop, this.originNode);
prop.origin = this.origin;
this.type.propagate(prop, weight);
},
propHint: function() { return this.prop; }
});
var ForAllProps = constraint("c", {
addType: function(type) {
if (!(type instanceof Obj)) return;
type.forAllProps(this.c);
}
});
function withDisabledComputing(fn, body) {
cx.disabledComputing = {fn: fn, prev: cx.disabledComputing};
try {
return body();
} finally {
cx.disabledComputing = cx.disabledComputing.prev;
}
}
var IsCallee = exports.IsCallee = constraint("self, args, argNodes, retval", {
init: function() {
Constraint.prototype.init();
this.disabled = cx.disabledComputing;
},
addType: function(fn, weight) {
if (!(fn instanceof Fn)) return;
for (var i = 0; i < this.args.length; ++i) {
if (i < fn.args.length) this.args[i].propagate(fn.args[i], weight);
if (fn.arguments) this.args[i].propagate(fn.arguments, weight);
}
this.self.propagate(fn.self, this.self == cx.topScope ? WG_GLOBAL_THIS : weight);
var compute = fn.computeRet;
if (compute) for (var d = this.disabled; d; d = d.prev)
if (d.fn == fn || fn.name && d.fn.name == fn.name) compute = null;
if (compute)
compute(this.self, this.args, this.argNodes).propagate(this.retval, weight);
else
fn.retval.propagate(this.retval, weight);
},
typeHint: function() {
var names = [];
for (var i = 0; i < this.args.length; ++i) names.push("?");
return new Fn(null, this.self, this.args, names, ANull);
},
propagatesTo: function() {
return {target: this.retval, pathExt: ".!ret"};
}
});
var HasMethodCall = constraint("propName, args, argNodes, retval", {
init: function() {
Constraint.prototype.init();
this.disabled = cx.disabledComputing;
},
addType: function(obj, weight) {
var callee = new IsCallee(obj, this.args, this.argNodes, this.retval);
callee.disabled = this.disabled;
obj.getProp(this.propName).propagate(callee, weight);
},
propHint: function() { return this.propName; }
});
var IsCtor = exports.IsCtor = constraint("target, noReuse", {
addType: function(f, weight) {
if (!(f instanceof Fn)) return;
f.getProp("prototype").propagate(new IsProto(this.noReuse ? false : f, this.target), weight);
}
});
var getInstance = exports.getInstance = function(obj, ctor) {
if (ctor === false) return new Obj(obj);
if (!ctor) ctor = obj.hasCtor;
if (!obj.instances) obj.instances = [];
for (var i = 0; i < obj.instances.length; ++i) {
var cur = obj.instances[i];
if (cur.ctor == ctor) return cur.instance;
}
var instance = new Obj(obj, ctor && ctor.name);
instance.origin = obj.origin;
obj.instances.push({ctor: ctor, instance: instance});
return instance;
};
var IsProto = exports.IsProto = constraint("ctor, target", {
addType: function(o, _weight) {
if (!(o instanceof Obj)) return;
if ((this.count = (this.count || 0) + 1) > 8) return;
if (o == cx.protos.Array)
this.target.addType(new Arr);
else
this.target.addType(getInstance(o, this.ctor));
}
});
var FnPrototype = constraint("fn", {
addType: function(o, _weight) {
if (o instanceof Obj && !o.hasCtor) {
o.hasCtor = this.fn;
var adder = new SpeculativeThis(o, this.fn);
adder.addType(this.fn);
o.forAllProps(function(_prop, val, local) {
if (local) val.propagate(adder);
});
}
}
});
var IsAdded = constraint("other, target", {
addType: function(type, weight) {
if (type == cx.str)
this.target.addType(cx.str, weight);
else if (type == cx.num && this.other.hasType(cx.num))
this.target.addType(cx.num, weight);
},
typeHint: function() { return this.other; }
});
var IfObj = constraint("target", {
addType: function(t, weight) {
if (t instanceof Obj) this.target.addType(t, weight);
},
propagatesTo: function() { return this.target; }
});
var SpeculativeThis = constraint("obj, ctor", {
addType: function(tp) {
if (tp instanceof Fn && tp.self && tp.self.isEmpty())
tp.self.addType(getInstance(this.obj, this.ctor), WG_SPECULATIVE_THIS);
}
});
var Muffle = constraint("inner, weight", {
addType: function(tp, weight) {
this.inner.addType(tp, Math.min(weight, this.weight));
},
propagatesTo: function() { return this.inner.propagatesTo(); },
typeHint: function() { return this.inner.typeHint(); },
propHint: function() { return this.inner.propHint(); }
});
// TYPE OBJECTS
var Type = exports.Type = function() {};
Type.prototype = extend(ANull, {
propagate: function(c, w) { c.addType(this, w); },
hasType: function(other) { return other == this; },
isEmpty: function() { return false; },
typeHint: function() { return this; },
getType: function() { return this; }
});
var Prim = exports.Prim = function(proto, name) { this.name = name; this.proto = proto; };
Prim.prototype = extend(Type.prototype, {
toString: function() { return this.name; },
getProp: function(prop) {return this.proto.hasProp(prop) || ANull;},
gatherProperties: function(f, depth) {
if (this.proto) this.proto.gatherProperties(f, depth);
}
});
var Obj = exports.Obj = function(proto, name) {
if (!this.props) this.props = Object.create(null);
this.proto = proto === true ? cx.protos.Object : proto;
if (proto && !name && proto.name && !(this instanceof Fn)) {
var match = /^(.*)\.prototype$/.exec(this.proto.name);
if (match) name = match[1];
}
this.name = name;
this.maybeProps = null;
this.origin = cx.curOrigin;
};
Obj.prototype = extend(Type.prototype, {
toString: function(maxDepth) {
if (!maxDepth && this.name) return this.name;
var props = [], etc = false;
for (var prop in this.props) if (prop != "<i>") {
if (props.length > 5) { etc = true; break; }
if (maxDepth)
props.push(prop + ": " + toString(this.props[prop].getType(), maxDepth - 1));
else
props.push(prop);
}
props.sort();
if (etc) props.push("...");
return "{" + props.join(", ") + "}";
},
hasProp: function(prop, searchProto) {
var found = this.props[prop];
if (searchProto !== false)
for (var p = this.proto; p && !found; p = p.proto) found = p.props[prop];
return found;
},
defProp: function(prop, originNode) {
var found = this.hasProp(prop, false);
if (found) {
if (originNode && !found.originNode) found.originNode = originNode;
return found;
}
if (prop == "__proto__" || prop == "✖") return ANull;
var av = this.maybeProps && this.maybeProps[prop];
if (av) {
delete this.maybeProps[prop];
this.maybeUnregProtoPropHandler();
} else {
av = new AVal;
}
this.props[prop] = av;
av.originNode = originNode;
av.origin = cx.curOrigin;
this.broadcastProp(prop, av, true);
return av;
},
getProp: function(prop) {
var found = this.hasProp(prop, true) || (this.maybeProps && this.maybeProps[prop]);
if (found) return found;
if (prop == "__proto__" || prop == "✖") return ANull;
return this.ensureMaybeProps()[prop] = new AVal;
},
broadcastProp: function(prop, val, local) {
if (local) {
this.signal("addProp", prop, val);
// If this is a scope, it shouldn't be registered
if (!(this instanceof Scope)) registerProp(prop, this);
}
if (this.onNewProp) for (var i = 0; i < this.onNewProp.length; ++i) {
var h = this.onNewProp[i];
h.onProtoProp ? h.onProtoProp(prop, val, local) : h(prop, val, local);
}
},
onProtoProp: function(prop, val, _local) {
var maybe = this.maybeProps && this.maybeProps[prop];
if (maybe) {
delete this.maybeProps[prop];
this.maybeUnregProtoPropHandler();
this.proto.getProp(prop).propagate(maybe);
}
this.broadcastProp(prop, val, false);
},
ensureMaybeProps: function() {
if (!this.maybeProps) {
if (this.proto) this.proto.forAllProps(this);
this.maybeProps = Object.create(null);
}
return this.maybeProps;
},
removeProp: function(prop) {
var av = this.props[prop];
delete this.props[prop];
this.ensureMaybeProps()[prop] = av;
},
forAllProps: function(c) {
if (!this.onNewProp) {
this.onNewProp = [];
if (this.proto) this.proto.forAllProps(this);
}
this.onNewProp.push(c);
for (var o = this; o; o = o.proto) for (var prop in o.props) {
if (c.onProtoProp)
c.onProtoProp(prop, o.props[prop], o == this);
else
c(prop, o.props[prop], o == this);
}
},
maybeUnregProtoPropHandler: function() {
if (this.maybeProps) {
for (var _n in this.maybeProps) return;
this.maybeProps = null;
}
if (!this.proto || this.onNewProp && this.onNewProp.length) return;
this.proto.unregPropHandler(this);
},
unregPropHandler: function(handler) {
for (var i = 0; i < this.onNewProp.length; ++i)
if (this.onNewProp[i] == handler) { this.onNewProp.splice(i, 1); break; }
this.maybeUnregProtoPropHandler();
},
gatherProperties: function(f, depth) {
for (var prop in this.props) if (prop != "<i>")
f(prop, this, depth);
if (this.proto) this.proto.gatherProperties(f, depth + 1);
}
});
var Fn = exports.Fn = function(name, self, args, argNames, retval) {
Obj.call(this, cx.protos.Function, name);
this.self = self;
this.args = args;
this.argNames = argNames;
this.retval = retval;
};
Fn.prototype = extend(Obj.prototype, {
toString: function(maxDepth) {
if (maxDepth) maxDepth--;
var str = "fn(";
for (var i = 0; i < this.args.length; ++i) {
if (i) str += ", ";
var name = this.argNames[i];
if (name && name != "?") str += name + ": ";
str += toString(this.args[i].getType(), maxDepth, this);
}
str += ")";
if (!this.retval.isEmpty())
str += " -> " + toString(this.retval.getType(), maxDepth, this);
return str;
},
getProp: function(prop) {
if (prop == "prototype") {
var known = this.hasProp(prop, false);
if (!known) {
known = this.defProp(prop);
var proto = new Obj(true, this.name && this.name + ".prototype");
proto.origin = this.origin;
known.addType(proto, WG_MADEUP_PROTO);
}
return known;
}
return Obj.prototype.getProp.call(this, prop);
},
defProp: function(prop, originNode) {
if (prop == "prototype") {
var found = this.hasProp(prop, false);
if (found) return found;
found = Obj.prototype.defProp.call(this, prop, originNode);
found.origin = this.origin;
found.propagate(new FnPrototype(this));
return found;
}
return Obj.prototype.defProp.call(this, prop, originNode);
},
getFunctionType: function() { return this; }
});
var Arr = exports.Arr = function(contentType) {
Obj.call(this, cx.protos.Array);
var content = this.defProp("<i>");
if (contentType) contentType.propagate(content);
};
Arr.prototype = extend(Obj.prototype, {
toString: function(maxDepth) {
return "[" + toString(this.getProp("<i>").getType(), maxDepth, this) + "]";
}
});
// THE PROPERTY REGISTRY
function registerProp(prop, obj) {
var data = cx.props[prop] || (cx.props[prop] = []);
data.push(obj);
}
function objsWithProp(prop) {
return cx.props[prop];
}
// INFERENCE CONTEXT
exports.Context = function(defs, parent) {
this.parent = parent;
this.props = Object.create(null);
this.protos = Object.create(null);
this.origins = [];
this.curOrigin = "ecma5";
this.paths = Object.create(null);
this.definitions = Object.create(null);
this.purgeGen = 0;
this.workList = null;
this.disabledComputing = null;
exports.withContext(this, function() {
cx.protos.Object = new Obj(null, "Object.prototype");
cx.topScope = new Scope();
cx.topScope.name = "<top>";
cx.protos.Array = new Obj(true, "Array.prototype");
cx.protos.Function = new Obj(true, "Function.prototype");
cx.protos.RegExp = new Obj(true, "RegExp.prototype");
cx.protos.String = new Obj(true, "String.prototype");
cx.protos.Number = new Obj(true, "Number.prototype");
cx.protos.Boolean = new Obj(true, "Boolean.prototype");
cx.str = new Prim(cx.protos.String, "string");
cx.bool = new Prim(cx.protos.Boolean, "bool");
cx.num = new Prim(cx.protos.Number, "number");
cx.curOrigin = null;
if (defs) for (var i = 0; i < defs.length; ++i)
def.load(defs[i]);
});
};
var cx = null;
exports.cx = function() { return cx; };
exports.withContext = function(context, f) {
var old = cx;
cx = context;
try { return f(); }
finally { cx = old; }
};
exports.addOrigin = function(origin) {
if (cx.origins.indexOf(origin) < 0) cx.origins.push(origin);
};
var baseMaxWorkDepth = 20, reduceMaxWorkDepth = .0001;
function withWorklist(f) {
if (cx.workList) return f(cx.workList);
var list = [], depth = 0;
var add = cx.workList = function(type, target, weight) {
if (depth < baseMaxWorkDepth - reduceMaxWorkDepth * list.length)
list.push(type, target, weight, depth);
};
try {
var ret = f(add);
for (var i = 0; i < list.length; i += 4) {
depth = list[i + 3] + 1;
list[i + 1].addType(list[i], list[i + 2]);
}
return ret;
} finally {
cx.workList = null;
}
}
// SCOPES
var Scope = exports.Scope = function(prev) {
Obj.call(this, prev || true);
this.prev = prev;
};
Scope.prototype = extend(Obj.prototype, {
defVar: function(name, originNode) {
for (var s = this; ; s = s.proto) {
var found = s.props[name];
if (found) return found;
if (!s.prev) return s.defProp(name, originNode);
}
}
});
// RETVAL COMPUTATION HEURISTICS
function maybeInstantiate(scope, score) {
if (scope.fnType)
scope.fnType.instantiateScore = (scope.fnType.instantiateScore || 0) + score;
}
var NotSmaller = {};
function nodeSmallerThan(node, n) {
try {
walk.simple(node, {Expression: function() { if (--n <= 0) throw NotSmaller; }});
return true;
} catch(e) {
if (e == NotSmaller) return false;
throw e;
}
}
function maybeTagAsInstantiated(node, scope) {
var score = scope.fnType.instantiateScore;
if (!cx.disabledComputing && score && scope.fnType.args.length && nodeSmallerThan(node, score * 5)) {
maybeInstantiate(scope.prev, score / 2);
setFunctionInstantiated(node, scope);
return true;
} else {
scope.fnType.instantiateScore = null;
}
}
function setFunctionInstantiated(node, scope) {
var fn = scope.fnType;
// Disconnect the arg avals, so that we can add info to them without side effects
for (var i = 0; i < fn.args.length; ++i) fn.args[i] = new AVal;
fn.self = new AVal;
fn.computeRet = function(self, args) {
// Prevent recursion
return withDisabledComputing(fn, function() {
var oldOrigin = cx.curOrigin;
cx.curOrigin = fn.origin;
var scopeCopy = new Scope(scope.prev);
for (var v in scope.props) {
var local = scopeCopy.defProp(v);
for (var i = 0; i < args.length; ++i) if (fn.argNames[i] == v && i < args.length)
args[i].propagate(local);
}
var argNames = fn.argNames.length != args.length ? fn.argNames.slice(0, args.length) : fn.argNames;
while (argNames.length < args.length) argNames.push("?");
scopeCopy.fnType = new Fn(fn.name, self, args, argNames, ANull);
if (fn.arguments) {
var argset = scopeCopy.fnType.arguments = new AVal;
scopeCopy.defProp("arguments").addType(new Arr(argset));
for (var i = 0; i < args.length; ++i) args[i].propagate(argset);
}
node.body.scope = scopeCopy;
walk.recursive(node.body, scopeCopy, null, scopeGatherer);
walk.recursive(node.body, scopeCopy, null, inferWrapper);
cx.curOrigin = oldOrigin;
return scopeCopy.fnType.retval;
});
};
}
function maybeTagAsGeneric(scope) {
var fn = scope.fnType, target = fn.retval;
if (target == ANull) return;
var targetInner, asArray;
if (!target.isEmpty() && (targetInner = target.getType()) instanceof Arr)
target = asArray = targetInner.getProp("<i>");
function explore(aval, path, depth) {
if (depth > 3 || !aval.forward) return;
for (var i = 0; i < aval.forward.length; ++i) {
var prop = aval.forward[i].propagatesTo();
if (!prop) continue;
var newPath = path, dest;
if (prop instanceof AVal) {
dest = prop;
} else if (prop.target instanceof AVal) {
newPath += prop.pathExt;
dest = prop.target;
} else continue;
if (dest == target) return newPath;
var found = explore(dest, newPath, depth + 1);
if (found) return found;
}
}
var foundPath = explore(fn.self, "!this", 0);
for (var i = 0; !foundPath && i < fn.args.length; ++i)
foundPath = explore(fn.args[i], "!" + i, 0);
if (foundPath) {
if (asArray) foundPath = "[" + foundPath + "]";
var p = new def.TypeParser(foundPath);
fn.computeRet = p.parseRetType();
fn.computeRetSource = foundPath;
return true;
}
}
// SCOPE GATHERING PASS
function addVar(scope, nameNode) {
var val = scope.defProp(nameNode.name, nameNode);
if (val.maybePurge) val.maybePurge = false;
return val;
}
var scopeGatherer = walk.make({
Function: function(node, scope, c) {
var inner = node.body.scope = new Scope(scope);
inner.node = node;
var argVals = [], argNames = [];
for (var i = 0; i < node.params.length; ++i) {
var param = node.params[i];
argNames.push(param.name);
argVals.push(addVar(inner, param));
}
inner.fnType = new Fn(node.id && node.id.name, new AVal, argVals, argNames, ANull);
inner.fnType.originNode = node;
if (node.id) {
var decl = node.type == "FunctionDeclaration";
addVar(decl ? scope : inner, node.id);
}
c(node.body, inner, "ScopeBody");
},
TryStatement: function(node, scope, c) {
c(node.block, scope, "Statement");
if (node.handler) {
var v = addVar(scope, node.handler.param);
c(node.handler.body, scope, "ScopeBody");
var e5 = cx.definitions.ecma5;
if (e5 && v.isEmpty()) getInstance(e5["Error.prototype"]).propagate(v, WG_CATCH_ERROR);
}
if (node.finalizer) c(node.finalizer, scope, "Statement");
},
VariableDeclaration: function(node, scope, c) {
for (var i = 0; i < node.declarations.length; ++i) {
var decl = node.declarations[i];
addVar(scope, decl.id);
if (decl.init) c(decl.init, scope, "Expression");
}
}
});
// CONSTRAINT GATHERING PASS
function propName(node, scope, c) {
var prop = node.property;
if (!node.computed) return prop.name;
if (prop.type == "Literal" && typeof prop.value == "string") return prop.value;
if (c) infer(prop, scope, c, ANull);
return "<i>";
}
function unopResultType(op) {
switch (op) {
case "+": case "-": case "~": return cx.num;
case "!": return cx.bool;
case "typeof": return cx.str;
case "void": case "delete": return ANull;
}
}
function binopIsBoolean(op) {
switch (op) {
case "==": case "!=": case "===": case "!==": case "<": case ">": case ">=": case "<=":
case "in": case "instanceof": return true;
}
}
function literalType(val) {
switch (typeof val) {
case "boolean": return cx.bool;
case "number": return cx.num;
case "string": return cx.str;
case "object":
case "function":
if (!val) return ANull;
return getInstance(cx.protos.RegExp);
}
}
function ret(f) {
return function(node, scope, c, out, name) {
var r = f(node, scope, c, name);
if (out) r.propagate(out);
return r;
};
}
function fill(f) {
return function(node, scope, c, out, name) {
if (!out) out = new AVal;
f(node, scope, c, out, name);
return out;
};
}
var inferExprVisitor = {
ArrayExpression: ret(function(node, scope, c) {
var eltval = new AVal;
for (var i = 0; i < node.elements.length; ++i) {
var elt = node.elements[i];
if (elt) infer(elt, scope, c, eltval);
}
return new Arr(eltval);
}),
ObjectExpression: ret(function(node, scope, c, name) {
var obj = node.objType = new Obj(true, name);
obj.originNode = node;
for (var i = 0; i < node.properties.length; ++i) {
var prop = node.properties[i], key = prop.key, name;
if (key.type == "Identifier") {
name = key.name;
} else if (typeof key.value == "string") {
name = key.value;
} else {
infer(prop.value, scope, c, ANull);
continue;
}
var val = obj.defProp(name, key);
val.initializer = true;
infer(prop.value, scope, c, val, name);
}
return obj;
}),
FunctionExpression: ret(function(node, scope, c, name) {
var inner = node.body.scope, fn = inner.fnType;
if (name && !fn.name) fn.name = name;
c(node.body, scope, "ScopeBody");
maybeTagAsInstantiated(node, inner) || maybeTagAsGeneric(inner);
if (node.id) inner.getProp(node.id.name).addType(fn);
return fn;
}),
SequenceExpression: ret(function(node, scope, c) {
for (var i = 0, l = node.expressions.length - 1; i < l; ++i)
infer(node.expressions[i], scope, c, ANull);
return infer(node.expressions[l], scope, c);
}),
UnaryExpression: ret(function(node, scope, c) {
infer(node.argument, scope, c, ANull);
return unopResultType(node.operator);
}),
UpdateExpression: ret(function(node, scope, c) {
infer(node.argument, scope, c, ANull);
return cx.num;
}),
BinaryExpression: ret(function(node, scope, c) {
if (node.operator == "+") {
var lhs = infer(node.left, scope, c);
var rhs = infer(node.right, scope, c);
if (lhs.hasType(cx.str) || rhs.hasType(cx.str)) return cx.str;
if (lhs.hasType(cx.num) && rhs.hasType(cx.num)) return cx.num;
var result = new AVal;
lhs.propagate(new IsAdded(rhs, result));
rhs.propagate(new IsAdded(lhs, result));
return result;
} else {
infer(node.left, scope, c, ANull);
infer(node.right, scope, c, ANull);
return binopIsBoolean(node.operator) ? cx.bool : cx.num;
}
}),
AssignmentExpression: ret(function(node, scope, c) {
var rhs, name, pName;
if (node.left.type == "MemberExpression") {
pName = propName(node.left, scope, c);
if (node.left.object.type == "Identifier")
name = node.left.object.name + "." + pName;
} else {
name = node.left.name;
}
if (node.operator != "=" && node.operator != "+=") {
infer(node.right, scope, c, ANull);
rhs = cx.num;
} else {
rhs = infer(node.right, scope, c, null, name);
}
if (node.left.type == "MemberExpression") {
var obj = infer(node.left.object, scope, c);
if (pName == "prototype") maybeInstantiate(scope, 20);
if (pName == "<i>") {
// This is a hack to recognize for/in loops that copy
// properties, and do the copying ourselves, insofar as we
// manage, because such loops tend to be relevant for type
// information.
var v = node.left.property.name, local = scope.props[v], over = local && local.iteratesOver;
if (over) {
maybeInstantiate(scope, 20);
var fromRight = node.right.type == "MemberExpression" && node.right.computed && node.right.property.name == v;
over.forAllProps(function(prop, val, local) {
if (local && prop != "prototype" && prop != "<i>")
obj.propagate(new PropHasSubset(prop, fromRight ? val : ANull));
});
return rhs;
}
}
obj.propagate(new PropHasSubset(pName, rhs, node.left.property));
} else { // Identifier
var v = scope.defVar(node.left.name, node.left);
if (v.maybePurge) v.maybePurge = false;
rhs.propagate(v);
}
return rhs;
}),
LogicalExpression: fill(function(node, scope, c, out) {
infer(node.left, scope, c, out);
infer(node.right, scope, c, out);
}),
ConditionalExpression: fill(function(node, scope, c, out) {
infer(node.test, scope, c, ANull);
infer(node.consequent, scope, c, out);
infer(node.alternate, scope, c, out);
}),
NewExpression: fill(function(node, scope, c, out, name) {
if (node.callee.type == "Identifier" && node.callee.name in scope.props)
maybeInstantiate(scope, 20);