index.js 2.6 KB

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