index.js 3.1 KB

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