node-helpers.js 2.9 KB

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