source: trip-planner-front/node_modules/@webassemblyjs/helper-numbers/esm/index.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[6a3a178]1import Long from "@xtuc/long";
2import parseHexFloat from "@webassemblyjs/floating-point-hex-parser";
3import { CompileError } from "@webassemblyjs/helper-api-error";
4export 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}
19export 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}
38export 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}
51export 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}
60export function parse64I(sourceString) {
61 var long;
62
63 if (isHexLiteral(sourceString)) {
64 long = Long.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.fromString(sourceString);
69 }
70
71 return {
72 high: long.high,
73 low: long.low
74 };
75}
76var NAN_WORD = /^\+?-?nan/;
77var INF_WORD = /^\+?-?inf/;
78export function isInfLiteral(sourceString) {
79 return INF_WORD.test(sourceString.toLowerCase());
80}
81export function isNanLiteral(sourceString) {
82 return NAN_WORD.test(sourceString.toLowerCase());
83}
84
85function isDecimalExponentLiteral(sourceString) {
86 return !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E");
87}
88
89function isHexLiteral(sourceString) {
90 return sourceString.substring(0, 2).toUpperCase() === "0X" || sourceString.substring(0, 3).toUpperCase() === "-0X";
91}
Note: See TracBrowser for help on using the repository browser.