You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node-helpers.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.numberLiteralFromRaw = numberLiteralFromRaw;
  6. exports.instruction = instruction;
  7. exports.objectInstruction = objectInstruction;
  8. exports.withLoc = withLoc;
  9. exports.withRaw = withRaw;
  10. exports.funcParam = funcParam;
  11. exports.indexLiteral = indexLiteral;
  12. exports.memIndexLiteral = memIndexLiteral;
  13. var _wastParser = require("@webassemblyjs/wast-parser");
  14. var _nodes = require("./nodes");
  15. function numberLiteralFromRaw(rawValue) {
  16. var instructionType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "i32";
  17. var original = rawValue; // Remove numeric separators _
  18. if (typeof rawValue === "string") {
  19. rawValue = rawValue.replace(/_/g, "");
  20. }
  21. if (typeof rawValue === "number") {
  22. return (0, _nodes.numberLiteral)(rawValue, String(original));
  23. } else {
  24. switch (instructionType) {
  25. case "i32":
  26. {
  27. return (0, _nodes.numberLiteral)((0, _wastParser.parse32I)(rawValue), String(original));
  28. }
  29. case "u32":
  30. {
  31. return (0, _nodes.numberLiteral)((0, _wastParser.parseU32)(rawValue), String(original));
  32. }
  33. case "i64":
  34. {
  35. return (0, _nodes.longNumberLiteral)((0, _wastParser.parse64I)(rawValue), String(original));
  36. }
  37. case "f32":
  38. {
  39. return (0, _nodes.floatLiteral)((0, _wastParser.parse32F)(rawValue), (0, _wastParser.isNanLiteral)(rawValue), (0, _wastParser.isInfLiteral)(rawValue), String(original));
  40. }
  41. // f64
  42. default:
  43. {
  44. return (0, _nodes.floatLiteral)((0, _wastParser.parse64F)(rawValue), (0, _wastParser.isNanLiteral)(rawValue), (0, _wastParser.isInfLiteral)(rawValue), String(original));
  45. }
  46. }
  47. }
  48. }
  49. function instruction(id) {
  50. var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  51. var namedArgs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  52. return (0, _nodes.instr)(id, undefined, args, namedArgs);
  53. }
  54. function objectInstruction(id, object) {
  55. var args = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  56. var namedArgs = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  57. return (0, _nodes.instr)(id, object, args, namedArgs);
  58. }
  59. /**
  60. * Decorators
  61. */
  62. function withLoc(n, end, start) {
  63. var loc = {
  64. start: start,
  65. end: end
  66. };
  67. n.loc = loc;
  68. return n;
  69. }
  70. function withRaw(n, raw) {
  71. n.raw = raw;
  72. return n;
  73. }
  74. function funcParam(valtype, id) {
  75. return {
  76. id: id,
  77. valtype: valtype
  78. };
  79. }
  80. function indexLiteral(value) {
  81. // $FlowIgnore
  82. var x = numberLiteralFromRaw(value, "u32");
  83. return x;
  84. }
  85. function memIndexLiteral(value) {
  86. // $FlowIgnore
  87. var x = numberLiteralFromRaw(value, "u32");
  88. return x;
  89. }