index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // @flow
  2. import Long from "@xtuc/long";
  3. import parseHexFloat from "@webassemblyjs/floating-point-hex-parser";
  4. import { CompileError } from "@webassemblyjs/helper-api-error";
  5. export function parse32F(sourceString: string): number {
  6. if (isHexLiteral(sourceString)) {
  7. return parseHexFloat(sourceString);
  8. }
  9. if (isInfLiteral(sourceString)) {
  10. return sourceString[0] === "-" ? -1 : 1;
  11. }
  12. if (isNanLiteral(sourceString)) {
  13. return (
  14. (sourceString[0] === "-" ? -1 : 1) *
  15. (sourceString.includes(":")
  16. ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16)
  17. : 0x400000)
  18. );
  19. }
  20. return parseFloat(sourceString);
  21. }
  22. export function parse64F(sourceString: string): number {
  23. if (isHexLiteral(sourceString)) {
  24. return parseHexFloat(sourceString);
  25. }
  26. if (isInfLiteral(sourceString)) {
  27. return sourceString[0] === "-" ? -1 : 1;
  28. }
  29. if (isNanLiteral(sourceString)) {
  30. return (
  31. (sourceString[0] === "-" ? -1 : 1) *
  32. (sourceString.includes(":")
  33. ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16)
  34. : 0x8000000000000)
  35. );
  36. }
  37. if (isHexLiteral(sourceString)) {
  38. return parseHexFloat(sourceString);
  39. }
  40. return parseFloat(sourceString);
  41. }
  42. export function parse32I(sourceString: string): number {
  43. let value = 0;
  44. if (isHexLiteral(sourceString)) {
  45. value = ~~parseInt(sourceString, 16);
  46. } else if (isDecimalExponentLiteral(sourceString)) {
  47. throw new Error("This number literal format is yet to be implemented.");
  48. } else {
  49. value = parseInt(sourceString, 10);
  50. }
  51. return value;
  52. }
  53. export function parseU32(sourceString: string): number {
  54. const value = parse32I(sourceString);
  55. if (value < 0) {
  56. throw new CompileError("Illegal value for u32: " + sourceString);
  57. }
  58. return value;
  59. }
  60. export function parse64I(sourceString: string): LongNumber {
  61. // $FlowIgnore
  62. let long: Long;
  63. if (isHexLiteral(sourceString)) {
  64. long = Long.fromString(sourceString, false, 16);
  65. } else if (isDecimalExponentLiteral(sourceString)) {
  66. throw new Error("This number literal format is yet to be implemented.");
  67. } else {
  68. long = Long.fromString(sourceString);
  69. }
  70. return {
  71. high: long.high,
  72. low: long.low,
  73. };
  74. }
  75. const NAN_WORD = /^\+?-?nan/;
  76. const INF_WORD = /^\+?-?inf/;
  77. export function isInfLiteral(sourceString: string): boolean {
  78. return INF_WORD.test(sourceString.toLowerCase());
  79. }
  80. export function isNanLiteral(sourceString: string): boolean {
  81. return NAN_WORD.test(sourceString.toLowerCase());
  82. }
  83. function isDecimalExponentLiteral(sourceString: string): boolean {
  84. return (
  85. !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E")
  86. );
  87. }
  88. function isHexLiteral(sourceString: string): boolean {
  89. return (
  90. sourceString.substring(0, 2).toUpperCase() === "0X" ||
  91. sourceString.substring(0, 3).toUpperCase() === "-0X"
  92. );
  93. }