Apply module bundling
[platform/framework/web/wrtjs.git] / node_modules / @webassemblyjs / helper-numbers / lib / index.js
1 "use strict";
2
3 Object.defineProperty(exports, "__esModule", {
4   value: true
5 });
6 exports.parse32F = parse32F;
7 exports.parse64F = parse64F;
8 exports.parse32I = parse32I;
9 exports.parseU32 = parseU32;
10 exports.parse64I = parse64I;
11 exports.isInfLiteral = isInfLiteral;
12 exports.isNanLiteral = isNanLiteral;
13
14 var _long = _interopRequireDefault(require("@xtuc/long"));
15
16 var _floatingPointHexParser = _interopRequireDefault(require("@webassemblyjs/floating-point-hex-parser"));
17
18 var _helperApiError = require("@webassemblyjs/helper-api-error");
19
20 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22 function parse32F(sourceString) {
23   if (isHexLiteral(sourceString)) {
24     return (0, _floatingPointHexParser.default)(sourceString);
25   }
26
27   if (isInfLiteral(sourceString)) {
28     return sourceString[0] === "-" ? -1 : 1;
29   }
30
31   if (isNanLiteral(sourceString)) {
32     return (sourceString[0] === "-" ? -1 : 1) * (sourceString.includes(":") ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16) : 0x400000);
33   }
34
35   return parseFloat(sourceString);
36 }
37
38 function parse64F(sourceString) {
39   if (isHexLiteral(sourceString)) {
40     return (0, _floatingPointHexParser.default)(sourceString);
41   }
42
43   if (isInfLiteral(sourceString)) {
44     return sourceString[0] === "-" ? -1 : 1;
45   }
46
47   if (isNanLiteral(sourceString)) {
48     return (sourceString[0] === "-" ? -1 : 1) * (sourceString.includes(":") ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16) : 0x8000000000000);
49   }
50
51   if (isHexLiteral(sourceString)) {
52     return (0, _floatingPointHexParser.default)(sourceString);
53   }
54
55   return parseFloat(sourceString);
56 }
57
58 function parse32I(sourceString) {
59   var value = 0;
60
61   if (isHexLiteral(sourceString)) {
62     value = ~~parseInt(sourceString, 16);
63   } else if (isDecimalExponentLiteral(sourceString)) {
64     throw new Error("This number literal format is yet to be implemented.");
65   } else {
66     value = parseInt(sourceString, 10);
67   }
68
69   return value;
70 }
71
72 function parseU32(sourceString) {
73   var value = parse32I(sourceString);
74
75   if (value < 0) {
76     throw new _helperApiError.CompileError("Illegal value for u32: " + sourceString);
77   }
78
79   return value;
80 }
81
82 function parse64I(sourceString) {
83   var long;
84
85   if (isHexLiteral(sourceString)) {
86     long = _long.default.fromString(sourceString, false, 16);
87   } else if (isDecimalExponentLiteral(sourceString)) {
88     throw new Error("This number literal format is yet to be implemented.");
89   } else {
90     long = _long.default.fromString(sourceString);
91   }
92
93   return {
94     high: long.high,
95     low: long.low
96   };
97 }
98
99 var NAN_WORD = /^\+?-?nan/;
100 var INF_WORD = /^\+?-?inf/;
101
102 function isInfLiteral(sourceString) {
103   return INF_WORD.test(sourceString.toLowerCase());
104 }
105
106 function isNanLiteral(sourceString) {
107   return NAN_WORD.test(sourceString.toLowerCase());
108 }
109
110 function isDecimalExponentLiteral(sourceString) {
111   return !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E");
112 }
113
114 function isHexLiteral(sourceString) {
115   return sourceString.substring(0, 2).toUpperCase() === "0X" || sourceString.substring(0, 3).toUpperCase() === "-0X";
116 }