index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. let long: Long;
  62. if (isHexLiteral(sourceString)) {
  63. long = Long.fromString(sourceString, false, 16);
  64. } else if (isDecimalExponentLiteral(sourceString)) {
  65. throw new Error("This number literal format is yet to be implemented.");
  66. } else {
  67. long = Long.fromString(sourceString);
  68. }
  69. return {
  70. high: long.high,
  71. low: long.low
  72. };
  73. }
  74. const NAN_WORD = /^\+?-?nan/;
  75. const INF_WORD = /^\+?-?inf/;
  76. export function isInfLiteral(sourceString: string): boolean {
  77. return INF_WORD.test(sourceString.toLowerCase());
  78. }
  79. export function isNanLiteral(sourceString: string): boolean {
  80. return NAN_WORD.test(sourceString.toLowerCase());
  81. }
  82. function isDecimalExponentLiteral(sourceString: string): boolean {
  83. return (
  84. !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E")
  85. );
  86. }
  87. function isHexLiteral(sourceString: string): boolean {
  88. return (
  89. sourceString.substring(0, 2).toUpperCase() === "0X" ||
  90. sourceString.substring(0, 3).toUpperCase() === "-0X"
  91. );
  92. }