source: trip-planner-front/node_modules/@webassemblyjs/helper-numbers/src/index.js@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1// @flow
2
3import Long from "@xtuc/long";
4import parseHexFloat from "@webassemblyjs/floating-point-hex-parser";
5import { CompileError } from "@webassemblyjs/helper-api-error";
6
7export function parse32F(sourceString: string): number {
8 if (isHexLiteral(sourceString)) {
9 return parseHexFloat(sourceString);
10 }
11 if (isInfLiteral(sourceString)) {
12 return sourceString[0] === "-" ? -1 : 1;
13 }
14 if (isNanLiteral(sourceString)) {
15 return (
16 (sourceString[0] === "-" ? -1 : 1) *
17 (sourceString.includes(":")
18 ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16)
19 : 0x400000)
20 );
21 }
22 return parseFloat(sourceString);
23}
24
25export function parse64F(sourceString: string): number {
26 if (isHexLiteral(sourceString)) {
27 return parseHexFloat(sourceString);
28 }
29 if (isInfLiteral(sourceString)) {
30 return sourceString[0] === "-" ? -1 : 1;
31 }
32 if (isNanLiteral(sourceString)) {
33 return (
34 (sourceString[0] === "-" ? -1 : 1) *
35 (sourceString.includes(":")
36 ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16)
37 : 0x8000000000000)
38 );
39 }
40 if (isHexLiteral(sourceString)) {
41 return parseHexFloat(sourceString);
42 }
43 return parseFloat(sourceString);
44}
45
46export function parse32I(sourceString: string): number {
47 let value = 0;
48 if (isHexLiteral(sourceString)) {
49 value = ~~parseInt(sourceString, 16);
50 } else if (isDecimalExponentLiteral(sourceString)) {
51 throw new Error("This number literal format is yet to be implemented.");
52 } else {
53 value = parseInt(sourceString, 10);
54 }
55
56 return value;
57}
58
59export function parseU32(sourceString: string): number {
60 const value = parse32I(sourceString);
61 if (value < 0) {
62 throw new CompileError("Illegal value for u32: " + sourceString);
63 }
64 return value;
65}
66
67export function parse64I(sourceString: string): LongNumber {
68 let long: Long;
69 if (isHexLiteral(sourceString)) {
70 long = Long.fromString(sourceString, false, 16);
71 } else if (isDecimalExponentLiteral(sourceString)) {
72 throw new Error("This number literal format is yet to be implemented.");
73 } else {
74 long = Long.fromString(sourceString);
75 }
76
77 return {
78 high: long.high,
79 low: long.low
80 };
81}
82
83const NAN_WORD = /^\+?-?nan/;
84const INF_WORD = /^\+?-?inf/;
85
86export function isInfLiteral(sourceString: string): boolean {
87 return INF_WORD.test(sourceString.toLowerCase());
88}
89
90export function isNanLiteral(sourceString: string): boolean {
91 return NAN_WORD.test(sourceString.toLowerCase());
92}
93
94function isDecimalExponentLiteral(sourceString: string): boolean {
95 return (
96 !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E")
97 );
98}
99
100function isHexLiteral(sourceString: string): boolean {
101 return (
102 sourceString.substring(0, 2).toUpperCase() === "0X" ||
103 sourceString.substring(0, 3).toUpperCase() === "-0X"
104 );
105}
Note: See TracBrowser for help on using the repository browser.