index.js 8.2 KB

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