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

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.6 KB
Line 
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 // $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}
77var NAN_WORD = /^\+?-?nan/;
78var INF_WORD = /^\+?-?inf/;
79export function isInfLiteral(sourceString) {
80 return INF_WORD.test(sourceString.toLowerCase());
81}
82export function isNanLiteral(sourceString) {
83 return NAN_WORD.test(sourceString.toLowerCase());
84}
85
86function isDecimalExponentLiteral(sourceString) {
87 return !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E");
88}
89
90function isHexLiteral(sourceString) {
91 return sourceString.substring(0, 2).toUpperCase() === "0X" || sourceString.substring(0, 3).toUpperCase() === "-0X";
92}
Note: See TracBrowser for help on using the repository browser.