index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 _long = _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. var long;
  63. if (isHexLiteral(sourceString)) {
  64. long = _long.default.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.default.fromString(sourceString);
  69. }
  70. return {
  71. high: long.high,
  72. low: long.low
  73. };
  74. }
  75. var NAN_WORD = /^\+?-?nan/;
  76. var INF_WORD = /^\+?-?inf/;
  77. function isInfLiteral(sourceString) {
  78. return INF_WORD.test(sourceString.toLowerCase());
  79. }
  80. function isNanLiteral(sourceString) {
  81. return NAN_WORD.test(sourceString.toLowerCase());
  82. }
  83. function isDecimalExponentLiteral(sourceString) {
  84. return !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E");
  85. }
  86. function isHexLiteral(sourceString) {
  87. return sourceString.substring(0, 2).toUpperCase() === "0X" || sourceString.substring(0, 3).toUpperCase() === "-0X";
  88. }