source: imaps-frontend/node_modules/@webassemblyjs/helper-numbers/src/index.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • 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 // $FlowIgnore
69 let long: Long;
70 if (isHexLiteral(sourceString)) {
71 long = Long.fromString(sourceString, false, 16);
72 } else if (isDecimalExponentLiteral(sourceString)) {
73 throw new Error("This number literal format is yet to be implemented.");
74 } else {
75 long = Long.fromString(sourceString);
76 }
77
78 return {
79 high: long.high,
80 low: long.low,
81 };
82}
83
84const NAN_WORD = /^\+?-?nan/;
85const INF_WORD = /^\+?-?inf/;
86
87export function isInfLiteral(sourceString: string): boolean {
88 return INF_WORD.test(sourceString.toLowerCase());
89}
90
91export function isNanLiteral(sourceString: string): boolean {
92 return NAN_WORD.test(sourceString.toLowerCase());
93}
94
95function isDecimalExponentLiteral(sourceString: string): boolean {
96 return (
97 !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E")
98 );
99}
100
101function isHexLiteral(sourceString: string): boolean {
102 return (
103 sourceString.substring(0, 2).toUpperCase() === "0X" ||
104 sourceString.substring(0, 3).toUpperCase() === "-0X"
105 );
106}
Note: See TracBrowser for help on using the repository browser.