index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  3. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  4. // TODO(sven): add flow in here
  5. export function moduleContextFromModuleAST(m) {
  6. var moduleContext = new ModuleContext();
  7. if (!(m.type === "Module")) {
  8. throw new Error('m.type === "Module"' + " error: " + (undefined || "unknown"));
  9. }
  10. m.fields.forEach(function (field) {
  11. switch (field.type) {
  12. case "Start":
  13. {
  14. moduleContext.setStart(field.index);
  15. }
  16. case "Func":
  17. {
  18. moduleContext.addFunction(field);
  19. break;
  20. }
  21. case "Global":
  22. {
  23. moduleContext.defineGlobal(field);
  24. break;
  25. }
  26. case "ModuleImport":
  27. {
  28. switch (field.descr.type) {
  29. case "GlobalType":
  30. {
  31. moduleContext.importGlobal(field.descr.valtype, field.descr.mutability);
  32. break;
  33. }
  34. case "Memory":
  35. {
  36. moduleContext.addMemory(field.descr.limits.min, field.descr.limits.max);
  37. break;
  38. }
  39. case "FuncImportDescr":
  40. {
  41. moduleContext.importFunction(field.descr);
  42. break;
  43. }
  44. case "Table":
  45. {
  46. // FIXME(sven): not implemented yet
  47. break;
  48. }
  49. default:
  50. throw new Error("Unsupported ModuleImport of type " + JSON.stringify(field.descr.type));
  51. }
  52. break;
  53. }
  54. case "Memory":
  55. {
  56. moduleContext.addMemory(field.limits.min, field.limits.max);
  57. break;
  58. }
  59. }
  60. });
  61. return moduleContext;
  62. }
  63. /**
  64. * Module context for type checking
  65. */
  66. export var ModuleContext =
  67. /*#__PURE__*/
  68. function () {
  69. function ModuleContext() {
  70. _classCallCheck(this, ModuleContext);
  71. this.funcs = [];
  72. this.funcsOffsetByIdentifier = [];
  73. this.globals = [];
  74. this.globalsOffsetByIdentifier = [];
  75. this.mems = []; // Current stack frame
  76. this.locals = [];
  77. this.labels = [];
  78. this.return = [];
  79. this.debugName = "unknown";
  80. this.start = null;
  81. }
  82. /**
  83. * Set start segment
  84. */
  85. _createClass(ModuleContext, [{
  86. key: "setStart",
  87. value: function setStart(index) {
  88. this.start = index.value;
  89. }
  90. /**
  91. * Get start function
  92. */
  93. }, {
  94. key: "getStart",
  95. value: function getStart() {
  96. return this.start;
  97. }
  98. /**
  99. * Reset the active stack frame
  100. */
  101. }, {
  102. key: "newContext",
  103. value: function newContext(debugName, expectedResult) {
  104. this.locals = [];
  105. this.labels = [expectedResult];
  106. this.return = expectedResult;
  107. this.debugName = debugName;
  108. }
  109. /**
  110. * Functions
  111. */
  112. }, {
  113. key: "addFunction",
  114. value: function addFunction(func
  115. /*: Func*/
  116. ) {
  117. // eslint-disable-next-line prefer-const
  118. var _ref = func.signature || {},
  119. _ref$params = _ref.params,
  120. args = _ref$params === void 0 ? [] : _ref$params,
  121. _ref$results = _ref.results,
  122. result = _ref$results === void 0 ? [] : _ref$results;
  123. args = args.map(function (arg) {
  124. return arg.valtype;
  125. });
  126. this.funcs.push({
  127. args: args,
  128. result: result
  129. });
  130. if (typeof func.name !== "undefined") {
  131. this.funcsOffsetByIdentifier[func.name.value] = this.funcs.length - 1;
  132. }
  133. }
  134. }, {
  135. key: "importFunction",
  136. value: function importFunction(funcimport) {
  137. // eslint-disable-next-line prefer-const
  138. var _funcimport$signature = funcimport.signature,
  139. args = _funcimport$signature.params,
  140. result = _funcimport$signature.results;
  141. args = args.map(function (arg) {
  142. return arg.valtype;
  143. });
  144. this.funcs.push({
  145. args: args,
  146. result: result
  147. });
  148. if (typeof funcimport.id !== "undefined") {
  149. // imports are first, we can assume their index in the array
  150. this.funcsOffsetByIdentifier[funcimport.id.value] = this.funcs.length - 1;
  151. }
  152. }
  153. }, {
  154. key: "hasFunction",
  155. value: function hasFunction(index) {
  156. return typeof this.getFunction(index) !== "undefined";
  157. }
  158. }, {
  159. key: "getFunction",
  160. value: function getFunction(index) {
  161. if (typeof index !== "number") {
  162. throw new Error("getFunction only supported for number index");
  163. }
  164. return this.funcs[index];
  165. }
  166. }, {
  167. key: "getFunctionOffsetByIdentifier",
  168. value: function getFunctionOffsetByIdentifier(name) {
  169. if (!(typeof name === "string")) {
  170. throw new Error('typeof name === "string"' + " error: " + (undefined || "unknown"));
  171. }
  172. return this.funcsOffsetByIdentifier[name];
  173. }
  174. /**
  175. * Labels
  176. */
  177. }, {
  178. key: "addLabel",
  179. value: function addLabel(result) {
  180. this.labels.unshift(result);
  181. }
  182. }, {
  183. key: "hasLabel",
  184. value: function hasLabel(index) {
  185. return this.labels.length > index && index >= 0;
  186. }
  187. }, {
  188. key: "getLabel",
  189. value: function getLabel(index) {
  190. return this.labels[index];
  191. }
  192. }, {
  193. key: "popLabel",
  194. value: function popLabel() {
  195. this.labels.shift();
  196. }
  197. /**
  198. * Locals
  199. */
  200. }, {
  201. key: "hasLocal",
  202. value: function hasLocal(index) {
  203. return typeof this.getLocal(index) !== "undefined";
  204. }
  205. }, {
  206. key: "getLocal",
  207. value: function getLocal(index) {
  208. return this.locals[index];
  209. }
  210. }, {
  211. key: "addLocal",
  212. value: function addLocal(type) {
  213. this.locals.push(type);
  214. }
  215. /**
  216. * Globals
  217. */
  218. }, {
  219. key: "hasGlobal",
  220. value: function hasGlobal(index) {
  221. return this.globals.length > index && index >= 0;
  222. }
  223. }, {
  224. key: "getGlobal",
  225. value: function getGlobal(index) {
  226. return this.globals[index].type;
  227. }
  228. }, {
  229. key: "getGlobalOffsetByIdentifier",
  230. value: function getGlobalOffsetByIdentifier(name) {
  231. if (!(typeof name === "string")) {
  232. throw new Error('typeof name === "string"' + " error: " + (undefined || "unknown"));
  233. }
  234. return this.globalsOffsetByIdentifier[name];
  235. }
  236. }, {
  237. key: "defineGlobal",
  238. value: function defineGlobal(global
  239. /*: Global*/
  240. ) {
  241. var type = global.globalType.valtype;
  242. var mutability = global.globalType.mutability;
  243. this.globals.push({
  244. type: type,
  245. mutability: mutability
  246. });
  247. if (typeof global.name !== "undefined") {
  248. this.globalsOffsetByIdentifier[global.name.value] = this.globals.length - 1;
  249. }
  250. }
  251. }, {
  252. key: "importGlobal",
  253. value: function importGlobal(type, mutability) {
  254. this.globals.push({
  255. type: type,
  256. mutability: mutability
  257. });
  258. }
  259. }, {
  260. key: "isMutableGlobal",
  261. value: function isMutableGlobal(index) {
  262. return this.globals[index].mutability === "var";
  263. }
  264. }, {
  265. key: "isImmutableGlobal",
  266. value: function isImmutableGlobal(index) {
  267. return this.globals[index].mutability === "const";
  268. }
  269. /**
  270. * Memories
  271. */
  272. }, {
  273. key: "hasMemory",
  274. value: function hasMemory(index) {
  275. return this.mems.length > index && index >= 0;
  276. }
  277. }, {
  278. key: "addMemory",
  279. value: function addMemory(min, max) {
  280. this.mems.push({
  281. min: min,
  282. max: max
  283. });
  284. }
  285. }, {
  286. key: "getMemory",
  287. value: function getMemory(index) {
  288. return this.mems[index];
  289. }
  290. }]);
  291. return ModuleContext;
  292. }();