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