node-helpers.js 2.3 KB

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