def.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. // Type description parser
  2. //
  3. // Type description JSON files (such as ecma5.json and browser.json)
  4. // are used to
  5. //
  6. // A) describe types that come from native code
  7. //
  8. // B) to cheaply load the types for big libraries, or libraries that
  9. // can't be inferred well
  10. (function(mod) {
  11. if (typeof exports == "object" && typeof module == "object") // CommonJS
  12. return exports.init = mod;
  13. if (typeof define == "function" && define.amd) // AMD
  14. return define({init: mod});
  15. tern.def = {init: mod};
  16. })(function(exports, infer) {
  17. "use strict";
  18. function hop(obj, prop) {
  19. return Object.prototype.hasOwnProperty.call(obj, prop);
  20. }
  21. var TypeParser = exports.TypeParser = function(spec, start, base, forceNew) {
  22. this.pos = start || 0;
  23. this.spec = spec;
  24. this.base = base;
  25. this.forceNew = forceNew;
  26. };
  27. function unwrapType(type, self, args) {
  28. return type.call ? type(self, args) : type;
  29. }
  30. function extractProp(type, prop) {
  31. if (prop == "!ret") {
  32. if (type.retval) return type.retval;
  33. var rv = new infer.AVal;
  34. type.propagate(new infer.IsCallee(infer.ANull, [], null, rv));
  35. return rv;
  36. } else {
  37. return type.getProp(prop);
  38. }
  39. }
  40. function computedFunc(args, retType) {
  41. return function(self, cArgs) {
  42. var realArgs = [];
  43. for (var i = 0; i < args.length; i++) realArgs.push(unwrapType(args[i], self, cArgs));
  44. return new infer.Fn(name, infer.ANull, realArgs, unwrapType(retType, self, cArgs));
  45. };
  46. }
  47. function computedUnion(types) {
  48. return function(self, args) {
  49. var union = new infer.AVal;
  50. for (var i = 0; i < types.length; i++) unwrapType(types[i], self, args).propagate(union);
  51. return union;
  52. };
  53. }
  54. function computedArray(inner) {
  55. return function(self, args) {
  56. return new infer.Arr(inner(self, args));
  57. };
  58. }
  59. TypeParser.prototype = {
  60. eat: function(str) {
  61. if (str.length == 1 ? this.spec.charAt(this.pos) == str : this.spec.indexOf(str, this.pos) == this.pos) {
  62. this.pos += str.length;
  63. return true;
  64. }
  65. },
  66. word: function(re) {
  67. var word = "", ch, re = re || /[\w$]/;
  68. while ((ch = this.spec.charAt(this.pos)) && re.test(ch)) { word += ch; ++this.pos; }
  69. return word;
  70. },
  71. error: function() {
  72. throw new Error("Unrecognized type spec: " + this.spec + " (at " + this.pos + ")");
  73. },
  74. parseFnType: function(comp, name, top) {
  75. var args = [], names = [], computed = false;
  76. if (!this.eat(")")) for (var i = 0; ; ++i) {
  77. var colon = this.spec.indexOf(": ", this.pos), argname;
  78. if (colon != -1) {
  79. argname = this.spec.slice(this.pos, colon);
  80. if (/^[$\w?]+$/.test(argname))
  81. this.pos = colon + 2;
  82. else
  83. argname = null;
  84. }
  85. names.push(argname);
  86. var argType = this.parseType(comp);
  87. if (argType.call) computed = true;
  88. args.push(argType);
  89. if (!this.eat(", ")) {
  90. this.eat(")") || this.error();
  91. break;
  92. }
  93. }
  94. var retType, computeRet, computeRetStart, fn;
  95. if (this.eat(" -> ")) {
  96. var retStart = this.pos;
  97. retType = this.parseType(true);
  98. if (retType.call) {
  99. if (top) {
  100. computeRet = retType;
  101. retType = infer.ANull;
  102. computeRetStart = retStart;
  103. } else {
  104. computed = true;
  105. }
  106. }
  107. } else {
  108. retType = infer.ANull;
  109. }
  110. if (computed) return computedFunc(args, retType);
  111. if (top && (fn = this.base))
  112. infer.Fn.call(this.base, name, infer.ANull, args, names, retType);
  113. else
  114. fn = new infer.Fn(name, infer.ANull, args, names, retType);
  115. if (computeRet) fn.computeRet = computeRet;
  116. if (computeRetStart != null) fn.computeRetSource = this.spec.slice(computeRetStart, this.pos);
  117. return fn;
  118. },
  119. parseType: function(comp, name, top) {
  120. var main = this.parseTypeMaybeProp(comp, name, top);
  121. if (!this.eat("|")) return main;
  122. var types = [main], computed = main.call;
  123. for (;;) {
  124. var next = this.parseTypeMaybeProp(comp, name, top);
  125. types.push(next);
  126. if (next.call) computed = true;
  127. if (!this.eat("|")) break;
  128. }
  129. if (computed) return computedUnion(types);
  130. var union = new infer.AVal;
  131. for (var i = 0; i < types.length; i++) types[i].propagate(union);
  132. return union;
  133. },
  134. parseTypeMaybeProp: function(comp, name, top) {
  135. var result = this.parseTypeInner(comp, name, top);
  136. while (comp && this.eat(".")) result = this.extendWithProp(result);
  137. return result;
  138. },
  139. extendWithProp: function(base) {
  140. var propName = this.word(/[\w<>$!]/) || this.error();
  141. if (base.apply) return function(self, args) {
  142. return extractProp(base(self, args), propName);
  143. };
  144. return extractProp(base, propName);
  145. },
  146. parseTypeInner: function(comp, name, top) {
  147. if (this.eat("fn(")) {
  148. return this.parseFnType(comp, name, top);
  149. } else if (this.eat("[")) {
  150. var inner = this.parseType(comp);
  151. this.eat("]") || this.error();
  152. if (inner.call) return computedArray(inner);
  153. if (top && this.base) {
  154. infer.Arr.call(this.base, inner);
  155. return this.base;
  156. }
  157. return new infer.Arr(inner);
  158. } else if (this.eat("+")) {
  159. var path = this.word(/[\w$<>\.!]/);
  160. var base = parsePath(path + ".prototype");
  161. if (!(base instanceof infer.Obj)) base = parsePath(path);
  162. if (!(base instanceof infer.Obj)) return base;
  163. if (comp && this.eat("[")) return this.parsePoly(base);
  164. if (top && this.forceNew) return new infer.Obj(base);
  165. return infer.getInstance(base);
  166. } else if (comp && this.eat("!")) {
  167. var arg = this.word(/\d/);
  168. if (arg) {
  169. arg = Number(arg);
  170. return function(_self, args) {return args[arg] || infer.ANull;};
  171. } else if (this.eat("this")) {
  172. return function(self) {return self;};
  173. } else if (this.eat("custom:")) {
  174. var fname = this.word(/[\w$]/);
  175. return customFunctions[fname] || function() { return infer.ANull; };
  176. } else {
  177. return this.fromWord("!" + this.word(/[\w$<>\.!]/));
  178. }
  179. } else if (this.eat("?")) {
  180. return infer.ANull;
  181. } else {
  182. return this.fromWord(this.word(/[\w$<>\.!`]/));
  183. }
  184. },
  185. fromWord: function(spec) {
  186. var cx = infer.cx();
  187. switch (spec) {
  188. case "number": return cx.num;
  189. case "string": return cx.str;
  190. case "bool": return cx.bool;
  191. case "<top>": return cx.topScope;
  192. }
  193. if (cx.localDefs && spec in cx.localDefs) return cx.localDefs[spec];
  194. return parsePath(spec);
  195. },
  196. parsePoly: function(base) {
  197. var propName = "<i>", match;
  198. if (match = this.spec.slice(this.pos).match(/^\s*(\w+)\s*=\s*/)) {
  199. propName = match[1];
  200. this.pos += match[0].length;
  201. }
  202. var value = this.parseType(true);
  203. if (!this.eat("]")) this.error();
  204. if (value.call) return function(self, args) {
  205. var instance = infer.getInstance(base);
  206. value(self, args).propagate(instance.defProp(propName));
  207. return instance;
  208. };
  209. var instance = infer.getInstance(base);
  210. value.propagate(instance.defProp(propName));
  211. return instance;
  212. }
  213. };
  214. function parseType(spec, name, base, forceNew) {
  215. var type = new TypeParser(spec, null, base, forceNew).parseType(false, name, true);
  216. if (/^fn\(/.test(spec)) for (var i = 0; i < type.args.length; ++i) (function(i) {
  217. var arg = type.args[i];
  218. if (arg instanceof infer.Fn && arg.args && arg.args.length) addEffect(type, function(_self, fArgs) {
  219. var fArg = fArgs[i];
  220. if (fArg) fArg.propagate(new infer.IsCallee(infer.cx().topScope, arg.args, null, infer.ANull));
  221. });
  222. })(i);
  223. return type;
  224. }
  225. function addEffect(fn, handler, replaceRet) {
  226. var oldCmp = fn.computeRet, rv = fn.retval;
  227. fn.computeRet = function(self, args, argNodes) {
  228. var handled = handler(self, args, argNodes);
  229. var old = oldCmp ? oldCmp(self, args, argNodes) : rv;
  230. return replaceRet ? handled : old;
  231. };
  232. }
  233. var parseEffect = exports.parseEffect = function(effect, fn) {
  234. var m;
  235. if (effect.indexOf("propagate ") == 0) {
  236. var p = new TypeParser(effect, 10);
  237. var origin = p.parseType(true);
  238. if (!p.eat(" ")) p.error();
  239. var target = p.parseType(true);
  240. addEffect(fn, function(self, args) {
  241. unwrapType(origin, self, args).propagate(unwrapType(target, self, args));
  242. });
  243. } else if (effect.indexOf("call ") == 0) {
  244. var andRet = effect.indexOf("and return ", 5) == 5;
  245. var p = new TypeParser(effect, andRet ? 16 : 5);
  246. var getCallee = p.parseType(true), getSelf = null, getArgs = [];
  247. if (p.eat(" this=")) getSelf = p.parseType(true);
  248. while (p.eat(" ")) getArgs.push(p.parseType(true));
  249. addEffect(fn, function(self, args) {
  250. var callee = unwrapType(getCallee, self, args);
  251. var slf = getSelf ? unwrapType(getSelf, self, args) : infer.ANull, as = [];
  252. for (var i = 0; i < getArgs.length; ++i) as.push(unwrapType(getArgs[i], self, args));
  253. var result = andRet ? new infer.AVal : infer.ANull;
  254. callee.propagate(new infer.IsCallee(slf, as, null, result));
  255. return result;
  256. }, andRet);
  257. } else if (m = effect.match(/^custom (\S+)\s*(.*)/)) {
  258. var customFunc = customFunctions[m[1]];
  259. if (customFunc) addEffect(fn, m[2] ? customFunc(m[2]) : customFunc);
  260. } else if (effect.indexOf("copy ") == 0) {
  261. var p = new TypeParser(effect, 5);
  262. var getFrom = p.parseType(true);
  263. p.eat(" ");
  264. var getTo = p.parseType(true);
  265. addEffect(fn, function(self, args) {
  266. var from = unwrapType(getFrom, self, args), to = unwrapType(getTo, self, args);
  267. from.forAllProps(function(prop, val, local) {
  268. if (local && prop != "<i>")
  269. to.propagate(new infer.PropHasSubset(prop, val));
  270. });
  271. });
  272. } else {
  273. throw new Error("Unknown effect type: " + effect);
  274. }
  275. };
  276. var currentTopScope;
  277. var parsePath = exports.parsePath = function(path, scope) {
  278. var cx = infer.cx(), cached = cx.paths[path], origPath = path;
  279. if (cached != null) return cached;
  280. cx.paths[path] = infer.ANull;
  281. var base = scope || currentTopScope || cx.topScope;
  282. if (cx.localDefs) for (var name in cx.localDefs) {
  283. if (path.indexOf(name) == 0) {
  284. if (path == name) return cx.paths[path] = cx.localDefs[path];
  285. if (path.charAt(name.length) == ".") {
  286. base = cx.localDefs[name];
  287. path = path.slice(name.length + 1);
  288. break;
  289. }
  290. }
  291. }
  292. var parts = path.split(".");
  293. for (var i = 0; i < parts.length && base != infer.ANull; ++i) {
  294. var prop = parts[i];
  295. if (prop.charAt(0) == "!") {
  296. if (prop == "!proto") {
  297. base = (base instanceof infer.Obj && base.proto) || infer.ANull;
  298. } else {
  299. var fn = base.getFunctionType();
  300. if (!fn) {
  301. base = infer.ANull;
  302. } else if (prop == "!ret") {
  303. base = fn.retval && fn.retval.getType(false) || infer.ANull;
  304. } else {
  305. var arg = fn.args && fn.args[Number(prop.slice(1))];
  306. base = (arg && arg.getType(false)) || infer.ANull;
  307. }
  308. }
  309. } else if (base instanceof infer.Obj) {
  310. var propVal = (prop == "prototype" && base instanceof infer.Fn) ? base.getProp(prop) : base.props[prop];
  311. if (!propVal || propVal.isEmpty())
  312. base = infer.ANull;
  313. else
  314. base = propVal.types[0];
  315. }
  316. }
  317. // Uncomment this to get feedback on your poorly written .json files
  318. // if (base == infer.ANull) console.error("bad path: " + origPath + " (" + cx.curOrigin + ")");
  319. cx.paths[origPath] = base == infer.ANull ? null : base;
  320. return base;
  321. };
  322. function emptyObj(ctor) {
  323. var empty = Object.create(ctor.prototype);
  324. empty.props = Object.create(null);
  325. empty.isShell = true;
  326. return empty;
  327. }
  328. function isSimpleAnnotation(spec) {
  329. if (!spec["!type"] || /^(fn\(|\[)/.test(spec["!type"])) return false;
  330. for (var prop in spec)
  331. if (prop != "!type" && prop != "!doc" && prop != "!url" && prop != "!span" && prop != "!data")
  332. return false;
  333. return true;
  334. }
  335. function passOne(base, spec, path) {
  336. if (!base) {
  337. var tp = spec["!type"];
  338. if (tp) {
  339. if (/^fn\(/.test(tp)) base = emptyObj(infer.Fn);
  340. else if (tp.charAt(0) == "[") base = emptyObj(infer.Arr);
  341. else throw new Error("Invalid !type spec: " + tp);
  342. } else if (spec["!stdProto"]) {
  343. base = infer.cx().protos[spec["!stdProto"]];
  344. } else {
  345. base = emptyObj(infer.Obj);
  346. }
  347. base.name = path;
  348. }
  349. for (var name in spec) if (hop(spec, name) && name.charCodeAt(0) != 33) {
  350. var inner = spec[name];
  351. if (typeof inner == "string" || isSimpleAnnotation(inner)) continue;
  352. var prop = base.defProp(name);
  353. passOne(prop.getObjType(), inner, path ? path + "." + name : name).propagate(prop);
  354. }
  355. return base;
  356. }
  357. function passTwo(base, spec, path) {
  358. if (base.isShell) {
  359. delete base.isShell;
  360. var tp = spec["!type"];
  361. if (tp) {
  362. parseType(tp, path, base);
  363. } else {
  364. var proto = spec["!proto"] && parseType(spec["!proto"]);
  365. infer.Obj.call(base, proto instanceof infer.Obj ? proto : true, path);
  366. }
  367. }
  368. var effects = spec["!effects"];
  369. if (effects && base instanceof infer.Fn) for (var i = 0; i < effects.length; ++i)
  370. parseEffect(effects[i], base);
  371. copyInfo(spec, base);
  372. for (var name in spec) if (hop(spec, name) && name.charCodeAt(0) != 33) {
  373. var inner = spec[name], known = base.defProp(name), innerPath = path ? path + "." + name : name;
  374. if (typeof inner == "string") {
  375. if (known.isEmpty()) parseType(inner, innerPath).propagate(known);
  376. } else {
  377. if (!isSimpleAnnotation(inner))
  378. passTwo(known.getObjType(), inner, innerPath);
  379. else if (known.isEmpty())
  380. parseType(inner["!type"], innerPath, null, true).propagate(known);
  381. else
  382. continue;
  383. if (inner["!doc"]) known.doc = inner["!doc"];
  384. if (inner["!url"]) known.url = inner["!url"];
  385. if (inner["!span"]) known.span = inner["!span"];
  386. }
  387. }
  388. return base;
  389. }
  390. function copyInfo(spec, type) {
  391. if (spec["!doc"]) type.doc = spec["!doc"];
  392. if (spec["!url"]) type.url = spec["!url"];
  393. if (spec["!span"]) type.span = spec["!span"];
  394. if (spec["!data"]) type.metaData = spec["!data"];
  395. }
  396. function runPasses(type, arg) {
  397. var parent = infer.cx().parent, pass = parent && parent.passes && parent.passes[type];
  398. if (pass) for (var i = 0; i < pass.length; i++) pass[i](arg);
  399. }
  400. function doLoadEnvironment(data, scope) {
  401. var cx = infer.cx();
  402. infer.addOrigin(cx.curOrigin = data["!name"] || "env#" + cx.origins.length);
  403. cx.localDefs = cx.definitions[cx.curOrigin] = Object.create(null);
  404. runPasses("preLoadDef", data);
  405. passOne(scope, data);
  406. var def = data["!define"];
  407. if (def) {
  408. for (var name in def) {
  409. var spec = def[name];
  410. cx.localDefs[name] = typeof spec == "string" ? parsePath(spec) : passOne(null, spec, name);
  411. }
  412. for (var name in def) {
  413. var spec = def[name];
  414. if (typeof spec != "string") passTwo(cx.localDefs[name], def[name], name);
  415. }
  416. }
  417. passTwo(scope, data);
  418. runPasses("postLoadDef", data);
  419. cx.curOrigin = cx.localDefs = null;
  420. }
  421. exports.load = function(data, scope) {
  422. if (!scope) scope = infer.cx().topScope;
  423. var oldScope = currentTopScope;
  424. currentTopScope = scope;
  425. try {
  426. doLoadEnvironment(data, scope);
  427. } finally {
  428. currentTopScope = oldScope;
  429. }
  430. };
  431. exports.parse = function(data, origin, path) {
  432. var cx = infer.cx();
  433. if (origin) {
  434. cx.origin = origin;
  435. cx.localDefs = cx.definitions[origin];
  436. }
  437. try {
  438. if (typeof data == "string")
  439. return parseType(data, path);
  440. else
  441. return passTwo(passOne(null, data, path), data, path);
  442. } finally {
  443. if (origin) cx.origin = cx.localDefs = null;
  444. }
  445. };
  446. // Used to register custom logic for more involved effect or type
  447. // computation.
  448. var customFunctions = Object.create(null);
  449. infer.registerFunction = function(name, f) { customFunctions[name] = f; };
  450. var IsCreated = infer.constraint("created, target, spec", {
  451. addType: function(tp) {
  452. if (tp instanceof infer.Obj && this.created++ < 5) {
  453. var derived = new infer.Obj(tp), spec = this.spec;
  454. if (spec instanceof infer.AVal) spec = spec.getObjType(false);
  455. if (spec instanceof infer.Obj) for (var prop in spec.props) {
  456. var cur = spec.props[prop].types[0];
  457. var p = derived.defProp(prop);
  458. if (cur && cur instanceof infer.Obj && cur.props.value) {
  459. var vtp = cur.props.value.getType(false);
  460. if (vtp) p.addType(vtp);
  461. }
  462. }
  463. this.target.addType(derived);
  464. }
  465. }
  466. });
  467. infer.registerFunction("Object_create", function(_self, args, argNodes) {
  468. if (argNodes && argNodes.length && argNodes[0].type == "Literal" && argNodes[0].value == null)
  469. return new infer.Obj();
  470. var result = new infer.AVal;
  471. if (args[0]) args[0].propagate(new IsCreated(0, result, args[1]));
  472. return result;
  473. });
  474. var PropSpec = infer.constraint("target", {
  475. addType: function(tp) {
  476. if (!(tp instanceof infer.Obj)) return;
  477. if (tp.hasProp("value"))
  478. tp.getProp("value").propagate(this.target);
  479. else if (tp.hasProp("get"))
  480. tp.getProp("get").propagate(new infer.IsCallee(infer.ANull, [], null, this.target));
  481. }
  482. });
  483. infer.registerFunction("Object_defineProperty", function(_self, args, argNodes) {
  484. if (argNodes && argNodes.length >= 3 && argNodes[1].type == "Literal" &&
  485. typeof argNodes[1].value == "string") {
  486. var obj = args[0], connect = new infer.AVal;
  487. obj.propagate(new infer.PropHasSubset(argNodes[1].value, connect, argNodes[1]));
  488. args[2].propagate(new PropSpec(connect));
  489. }
  490. return infer.ANull;
  491. });
  492. infer.registerFunction("Object_defineProperties", function(_self, args, argNodes) {
  493. if (args.length >= 2) {
  494. var obj = args[0];
  495. args[1].forAllProps(function(prop, val, local) {
  496. if (!local) return;
  497. var connect = new infer.AVal;
  498. obj.propagate(new infer.PropHasSubset(prop, connect, argNodes && argNodes[1]));
  499. val.propagate(new PropSpec(connect));
  500. });
  501. }
  502. return infer.ANull;
  503. });
  504. var IsBound = infer.constraint("self, args, target", {
  505. addType: function(tp) {
  506. if (!(tp instanceof infer.Fn)) return;
  507. this.target.addType(new infer.Fn(tp.name, infer.ANull, tp.args.slice(this.args.length),
  508. tp.argNames.slice(this.args.length), tp.retval));
  509. this.self.propagate(tp.self);
  510. for (var i = 0; i < Math.min(tp.args.length, this.args.length); ++i)
  511. this.args[i].propagate(tp.args[i]);
  512. }
  513. });
  514. infer.registerFunction("Function_bind", function(self, args) {
  515. if (!args.length) return infer.ANull;
  516. var result = new infer.AVal;
  517. self.propagate(new IsBound(args[0], args.slice(1), result));
  518. return result;
  519. });
  520. infer.registerFunction("Array_ctor", function(_self, args) {
  521. var arr = new infer.Arr;
  522. if (args.length != 1 || !args[0].hasType(infer.cx().num)) {
  523. var content = arr.getProp("<i>");
  524. for (var i = 0; i < args.length; ++i) args[i].propagate(content);
  525. }
  526. return arr;
  527. });
  528. infer.registerFunction("Promise_ctor", function(_self, args, argNodes) {
  529. if (args.length < 1) return infer.ANull;
  530. var self = new infer.Obj(infer.cx().definitions.ecma6["Promise.prototype"]);
  531. var valProp = self.defProp("value", argNodes && argNodes[0]);
  532. var valArg = new infer.AVal;
  533. valArg.propagate(valProp);
  534. var exec = new infer.Fn("execute", infer.ANull, [valArg], ["value"], infer.ANull);
  535. var reject = infer.cx().definitions.ecma6.promiseReject;
  536. args[0].propagate(new infer.IsCallee(infer.ANull, [exec, reject], null, infer.ANull));
  537. return self;
  538. });
  539. return exports;
  540. });