/src/moddable/xs/sources/xsNumber.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016-2025 Moddable Tech, Inc. |
3 | | * |
4 | | * This file is part of the Moddable SDK Runtime. |
5 | | * |
6 | | * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This file incorporates work covered by the following copyright and |
20 | | * permission notice: |
21 | | * |
22 | | * Copyright (C) 2010-2016 Marvell International Ltd. |
23 | | * Copyright (C) 2002-2010 Kinoma, Inc. |
24 | | * |
25 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
26 | | * you may not use this file except in compliance with the License. |
27 | | * You may obtain a copy of the License at |
28 | | * |
29 | | * http://www.apache.org/licenses/LICENSE-2.0 |
30 | | * |
31 | | * Unless required by applicable law or agreed to in writing, software |
32 | | * distributed under the License is distributed on an "AS IS" BASIS, |
33 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 | | * See the License for the specific language governing permissions and |
35 | | * limitations under the License. |
36 | | */ |
37 | | |
38 | | #include "xsAll.h" |
39 | | |
40 | | static txSlot* fxCheckNumber(txMachine* the, txSlot* it); |
41 | | |
42 | | void fxBuildNumber(txMachine* the) |
43 | 25.5k | { |
44 | 25.5k | txSlot* slot; |
45 | | |
46 | 25.5k | mxPushNumber((txNumber)C_INFINITY); |
47 | 25.5k | mxPull(mxInfinity); |
48 | 25.5k | mxPushNumber((txNumber)C_NAN); |
49 | 25.5k | mxPull(mxNaN); |
50 | | |
51 | 25.5k | mxPush(mxObjectPrototype); |
52 | 25.5k | slot = fxLastProperty(the, fxNewNumberInstance(the)); |
53 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toExponential), 1, mxID(_toExponential), XS_DONT_ENUM_FLAG); |
54 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toFixed), 1, mxID(_toFixed), XS_DONT_ENUM_FLAG); |
55 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toLocaleString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG); |
56 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toPrecision), 1, mxID(_toPrecision), XS_DONT_ENUM_FLAG); |
57 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toString), 1, mxID(_toString), XS_DONT_ENUM_FLAG); |
58 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG); |
59 | 25.5k | mxNumberPrototype = *the->stack; |
60 | 25.5k | slot = fxBuildHostConstructor(the, mxCallback(fx_Number), 1, mxID(_Number)); |
61 | 25.5k | mxNumberConstructor = *the->stack; |
62 | 25.5k | slot = fxLastProperty(the, slot); |
63 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isFinite), 1, mxID(_isFinite), XS_DONT_ENUM_FLAG); |
64 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isInteger), 1, mxID(_isInteger), XS_DONT_ENUM_FLAG); |
65 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isNaN), 1, mxID(_isNaN), XS_DONT_ENUM_FLAG); |
66 | 25.5k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isSafeInteger), 1, mxID(_isSafeInteger), XS_DONT_ENUM_FLAG); |
67 | 25.5k | slot = fxNextSlotProperty(the, slot, &mxParseFloatFunction, mxID(_parseFloat), XS_DONT_ENUM_FLAG); |
68 | 25.5k | slot = fxNextSlotProperty(the, slot, &mxParseIntFunction, mxID(_parseInt), XS_DONT_ENUM_FLAG); |
69 | 25.5k | slot = fxNextNumberProperty(the, slot, C_EPSILON, mxID(_EPSILON), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
70 | 25.5k | slot = fxNextNumberProperty(the, slot, C_MAX_SAFE_INTEGER, mxID(_MAX_SAFE_INTEGER), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
71 | 25.5k | slot = fxNextNumberProperty(the, slot, C_DBL_MAX, mxID(_MAX_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
72 | 25.5k | slot = fxNextNumberProperty(the, slot, C_MIN_SAFE_INTEGER, mxID(_MIN_SAFE_INTEGER), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
73 | 25.5k | slot = fxNextNumberProperty(the, slot, C_DBL_MIN, mxID(_MIN_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
74 | 25.5k | slot = fxNextNumberProperty(the, slot, C_NAN, mxID(_NaN), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
75 | 25.5k | slot = fxNextNumberProperty(the, slot, -((txNumber)C_INFINITY), mxID(_NEGATIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
76 | 25.5k | slot = fxNextNumberProperty(the, slot, (txNumber)C_INFINITY, mxID(_POSITIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
77 | 25.5k | mxPop(); |
78 | 25.5k | } |
79 | | |
80 | | txSlot* fxNewNumberInstance(txMachine* the) |
81 | 11.8M | { |
82 | 11.8M | txSlot* instance; |
83 | 11.8M | instance = fxNewObjectInstance(the); |
84 | 11.8M | fxNextNumberProperty(the, instance, 0, XS_NO_ID, XS_INTERNAL_FLAG); |
85 | 11.8M | return instance; |
86 | 11.8M | } |
87 | | |
88 | | void fxNumberCoerce(txMachine* the, txSlot* slot) |
89 | 2.56k | { |
90 | 2.56k | fxToNumber(the, slot); |
91 | 2.56k | } |
92 | | |
93 | | void fx_isFinite(txMachine* the) |
94 | 997 | { |
95 | 997 | int fpclass; |
96 | 997 | txNumber number = (mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0)); |
97 | 997 | mxResult->kind = XS_BOOLEAN_KIND; |
98 | 997 | mxResult->value.boolean = 0; |
99 | 997 | fpclass = c_fpclassify(number); |
100 | 997 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) |
101 | 718 | mxResult->value.boolean = 1; |
102 | 997 | } |
103 | | |
104 | | void fx_isNaN(txMachine* the) |
105 | 1.66k | { |
106 | 1.66k | int fpclass; |
107 | 1.66k | txNumber number = (mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0)); |
108 | 1.66k | mxResult->kind = XS_BOOLEAN_KIND; |
109 | 1.66k | mxResult->value.boolean = 0; |
110 | 1.66k | fpclass = c_fpclassify(number); |
111 | 1.66k | if (fpclass == C_FP_NAN) |
112 | 295 | mxResult->value.boolean = 1; |
113 | 1.66k | } |
114 | | |
115 | | void fx_parseFloat(txMachine* the) |
116 | 41.8k | { |
117 | 41.8k | if (mxArgc < 1) { |
118 | 614 | mxResult->value.number = C_NAN; |
119 | 614 | mxResult->kind = XS_NUMBER_KIND; |
120 | 614 | return; |
121 | 614 | } |
122 | 41.2k | fxToString(the, mxArgv(0)); |
123 | 41.2k | mxResult->kind = XS_NUMBER_KIND; |
124 | 41.2k | mxResult->value.number = fxStringToNumber(the, mxArgv(0)->value.string, 0); |
125 | 41.2k | } |
126 | | |
127 | | void fx_parseInt(txMachine* the) |
128 | 561k | { |
129 | 561k | txInteger aRadix, aDigit; |
130 | 561k | txNumber aSign, aResult; |
131 | 561k | txString s, r; |
132 | 561k | char c; |
133 | | |
134 | 561k | if (mxArgc < 1) { |
135 | 767 | mxResult->value.number = C_NAN; |
136 | 767 | mxResult->kind = XS_NUMBER_KIND; |
137 | 767 | return; |
138 | 767 | } |
139 | 560k | fxToString(the, mxArgv(0)); |
140 | 560k | if (mxArgc > 1) { |
141 | 552k | aRadix = fxToInteger(the, mxArgv(1)); |
142 | 552k | if (aRadix) { |
143 | 430k | if ((aRadix < 2) || (36 < aRadix)) { |
144 | 131k | mxResult->kind = XS_NUMBER_KIND; |
145 | 131k | mxResult->value.number = C_NAN; |
146 | 131k | return; |
147 | 131k | } |
148 | 430k | } |
149 | 552k | } |
150 | 7.82k | else |
151 | 7.82k | aRadix = 0; |
152 | 429k | s = fxSkipSpaces(mxArgv(0)->value.string); |
153 | 429k | c = *s; |
154 | 429k | aSign = 1; |
155 | 429k | if (c == '+') |
156 | 570 | s++; |
157 | 428k | else if (c == '-') { |
158 | 6.06k | s++; |
159 | 6.06k | aSign = -1; |
160 | 6.06k | } |
161 | 429k | if ((*s == '0') && ((*(s + 1) == 'x') || (*(s + 1) == 'X'))) { |
162 | 246 | if ((aRadix == 0) || (aRadix == 16)) { |
163 | 228 | aRadix = 16; |
164 | 228 | s += 2; |
165 | 228 | } |
166 | 246 | } |
167 | | /*if (*s == '0') { |
168 | | if ((aRadix == 0) || (aRadix == 8)) { |
169 | | aRadix = 8; |
170 | | } |
171 | | }*/ |
172 | 429k | if (aRadix == 0) |
173 | 130k | aRadix = 10; |
174 | 429k | aResult = 0; |
175 | 429k | r = s; |
176 | 450k | while ((c = *s)) { |
177 | 100k | if (('0' <= c) && (c <= '9')) |
178 | 18.3k | aDigit = c - '0'; |
179 | 82.6k | else if (('a' <= c) && (c <= 'z')) |
180 | 1.93k | aDigit = 10 + c - 'a'; |
181 | 80.6k | else if (('A' <= c) && (c <= 'Z')) |
182 | 2.04k | aDigit = 10 + c - 'A'; |
183 | 78.6k | else |
184 | 78.6k | break; |
185 | 22.2k | if (aDigit >= aRadix) |
186 | 1.30k | break; |
187 | 20.9k | aResult = (aResult * aRadix) + aDigit; |
188 | 20.9k | s++; |
189 | 20.9k | } |
190 | 429k | if (r == s) { |
191 | 426k | mxResult->kind = XS_NUMBER_KIND; |
192 | 426k | mxResult->value.number = C_NAN; |
193 | 426k | } |
194 | 2.91k | else { |
195 | 2.91k | aResult *= aSign; |
196 | 2.91k | aRadix = (txInteger)aResult; |
197 | 2.91k | aSign = aRadix; |
198 | 2.91k | if (aSign == aResult) { |
199 | 1.92k | mxResult->value.integer = aRadix; |
200 | 1.92k | mxResult->kind = XS_INTEGER_KIND; |
201 | 1.92k | } |
202 | 983 | else { |
203 | 983 | mxResult->value.number = aResult; |
204 | 983 | mxResult->kind = XS_NUMBER_KIND; |
205 | 983 | } |
206 | 2.91k | } |
207 | 429k | } |
208 | | |
209 | | void fx_Number(txMachine* the) |
210 | 303k | { |
211 | 303k | txNumber value = 0; |
212 | 303k | if (mxArgc > 0) { |
213 | 248k | txSlot* slot = mxArgv(0); |
214 | 248k | if (slot->kind == XS_REFERENCE_KIND) |
215 | 2.71k | fxToPrimitive(the, slot, XS_NUMBER_HINT); |
216 | 248k | if ((slot->kind == XS_BIGINT_KIND) || (slot->kind == XS_BIGINT_X_KIND)) |
217 | 571 | value = gxTypeBigInt.toNumber(the, slot); |
218 | 248k | else |
219 | 248k | value = fxToNumber(the, slot); |
220 | 248k | } |
221 | 303k | if (!mxHasTarget) { |
222 | 302k | mxResult->kind = XS_NUMBER_KIND; |
223 | 302k | mxResult->value.number = value; |
224 | 302k | fx_Math_toInteger(the); |
225 | 302k | } |
226 | 1.31k | else { |
227 | 1.31k | txSlot* instance; |
228 | 1.31k | mxPushSlot(mxTarget); |
229 | 1.31k | fxGetPrototypeFromConstructor(the, &mxNumberPrototype); |
230 | 1.31k | instance = fxNewNumberInstance(the); |
231 | 1.31k | instance->next->value.number = value; |
232 | 1.31k | mxPullSlot(mxResult); |
233 | 1.31k | } |
234 | 303k | } |
235 | | |
236 | | void fx_Number_isFinite(txMachine* the) |
237 | 57 | { |
238 | 57 | int fpclass; |
239 | 57 | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
240 | 57 | mxResult->kind = XS_BOOLEAN_KIND; |
241 | 57 | mxResult->value.boolean = 0; |
242 | 57 | if (slot) { |
243 | 56 | if (slot->kind == XS_INTEGER_KIND) |
244 | 15 | mxResult->value.boolean = 1; |
245 | 41 | else if (slot->kind == XS_NUMBER_KIND) { |
246 | 25 | fpclass = c_fpclassify(slot->value.number); |
247 | 25 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) |
248 | 15 | mxResult->value.boolean = 1; |
249 | 25 | } |
250 | 56 | } |
251 | 57 | } |
252 | | |
253 | | void fx_Number_isInteger(txMachine* the) |
254 | 676 | { |
255 | 676 | int fpclass; |
256 | 676 | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
257 | 676 | mxResult->kind = XS_BOOLEAN_KIND; |
258 | 676 | mxResult->value.boolean = 0; |
259 | 676 | if (slot) { |
260 | 417 | if (slot->kind == XS_INTEGER_KIND) |
261 | 248 | mxResult->value.boolean = 1; |
262 | 169 | else if (slot->kind == XS_NUMBER_KIND) { |
263 | 151 | fpclass = c_fpclassify(slot->value.number); |
264 | 151 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) { |
265 | 147 | txNumber check = c_trunc(slot->value.number); |
266 | 147 | if (slot->value.number == check) |
267 | 12 | mxResult->value.boolean = 1; |
268 | 147 | } |
269 | 151 | } |
270 | 417 | } |
271 | 676 | } |
272 | | |
273 | | void fx_Number_isNaN(txMachine* the) |
274 | 52 | { |
275 | 52 | int fpclass; |
276 | 52 | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
277 | 52 | mxResult->kind = XS_BOOLEAN_KIND; |
278 | 52 | mxResult->value.boolean = 0; |
279 | 52 | if (slot) { |
280 | 51 | if (slot->kind == XS_NUMBER_KIND) { |
281 | 25 | fpclass = c_fpclassify(slot->value.number); |
282 | 25 | if (fpclass == C_FP_NAN) |
283 | 10 | mxResult->value.boolean = 1; |
284 | 25 | } |
285 | 51 | } |
286 | 52 | } |
287 | | |
288 | | void fx_Number_isSafeInteger(txMachine* the) |
289 | 3.19k | { |
290 | 3.19k | int fpclass; |
291 | 3.19k | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
292 | 3.19k | mxResult->kind = XS_BOOLEAN_KIND; |
293 | 3.19k | mxResult->value.boolean = 0; |
294 | 3.19k | if (slot) { |
295 | 3.19k | if (slot->kind == XS_INTEGER_KIND) |
296 | 6 | mxResult->value.boolean = 1; |
297 | 3.18k | else if (slot->kind == XS_NUMBER_KIND) { |
298 | 23 | fpclass = c_fpclassify(slot->value.number); |
299 | 23 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) { |
300 | 19 | txNumber check = c_trunc(slot->value.number); |
301 | 19 | if (slot->value.number == check) { |
302 | 9 | if ((C_MIN_SAFE_INTEGER <= check) && (check <= C_MAX_SAFE_INTEGER)) |
303 | 6 | mxResult->value.boolean = 1; |
304 | 9 | } |
305 | 19 | } |
306 | 23 | } |
307 | 3.19k | } |
308 | 3.19k | } |
309 | | |
310 | | void fx_Number_prototype_toExponential(txMachine* the) |
311 | 71.4k | { |
312 | 71.4k | char buffer[256]; |
313 | 71.4k | txInteger mode = 0; |
314 | 71.4k | txNumber precision = 0; |
315 | 71.4k | txNumber value; |
316 | 71.4k | txSlot* slot = fxCheckNumber(the, mxThis); |
317 | 71.4k | if (!slot) mxTypeError("this: not a number"); |
318 | 71.4k | value = slot->value.number; |
319 | 71.4k | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) { |
320 | 71.4k | precision = fxToNumber(the, mxArgv(0)); |
321 | 71.4k | if (c_isnan(precision)) |
322 | 4 | precision = 0; |
323 | 71.4k | else if (c_isfinite(precision)) |
324 | 71.4k | precision = c_trunc(precision); |
325 | 71.4k | } |
326 | 33 | else |
327 | 33 | precision = 0; |
328 | 71.4k | if (c_isnan(value)) |
329 | 6 | precision = 0; |
330 | 71.4k | else if (c_isfinite(value)) { |
331 | 236 | if ((precision < 0) || (100 < precision)) |
332 | 5 | mxRangeError("invalid fractionDigits"); |
333 | 231 | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) |
334 | 198 | precision++; |
335 | 231 | mode = 'e'; |
336 | 231 | } |
337 | 71.2k | else |
338 | 71.2k | precision = 0; |
339 | 71.4k | fxNumberToString(the, slot->value.number, buffer, sizeof(buffer), mode, (int)precision); |
340 | 71.4k | fxCopyStringC(the, mxResult, buffer); |
341 | 71.4k | } |
342 | | |
343 | | void fx_Number_prototype_toFixed(txMachine* the) |
344 | 188k | { |
345 | 188k | char buffer[256]; |
346 | 188k | txInteger mode = 0; |
347 | 188k | txNumber precision = 0; |
348 | 188k | txNumber value; |
349 | 188k | txSlot* slot = fxCheckNumber(the, mxThis); |
350 | 188k | if (!slot) mxTypeError("this: not a number"); |
351 | 188k | value = slot->value.number; |
352 | 188k | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) { |
353 | 183k | precision = fxToNumber(the, mxArgv(0)); |
354 | 183k | if (c_isnan(precision)) |
355 | 4.94k | precision = 0; |
356 | 178k | else if (c_isfinite(precision)) |
357 | 178k | precision = c_trunc(precision); |
358 | 183k | } |
359 | 5.15k | else |
360 | 5.15k | precision = 0; |
361 | 188k | if ((precision < 0) || (100 < precision)) |
362 | 8 | mxRangeError("invalid fractionDigits"); |
363 | 188k | if (c_fabs(value) >= 1e21) |
364 | 27.8k | precision = 0; |
365 | 160k | else |
366 | 160k | mode = 'f'; |
367 | 188k | fxNumberToString(the, value, buffer, sizeof(buffer), mode, (int)precision); |
368 | 188k | fxCopyStringC(the, mxResult, buffer); |
369 | 188k | } |
370 | | |
371 | | void fx_Number_prototype_toLocaleString(txMachine* the) |
372 | 6 | { |
373 | 6 | txSlot* slot = fxCheckNumber(the, mxThis); |
374 | 6 | if (!slot) mxTypeError("this: not a number"); |
375 | 6 | mxResult->kind = slot->kind; |
376 | 6 | mxResult->value = slot->value; |
377 | 6 | fxToString(the, mxResult); |
378 | 6 | } |
379 | | |
380 | | void fx_Number_prototype_toPrecision(txMachine* the) |
381 | 663k | { |
382 | 663k | char buffer[256]; |
383 | 663k | txInteger mode = 0; |
384 | 663k | txNumber precision = 0; |
385 | 663k | txNumber value; |
386 | 663k | txSlot* slot = fxCheckNumber(the, mxThis); |
387 | 663k | if (!slot) mxTypeError("this: not a number"); |
388 | 663k | value = slot->value.number; |
389 | 663k | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) { |
390 | 622k | precision = fxToNumber(the, mxArgv(0)); |
391 | 622k | if (c_isnan(precision)) |
392 | 6 | precision = 0; |
393 | 622k | else if (c_isfinite(precision)) |
394 | 622k | precision = c_trunc(precision); |
395 | 622k | if (c_isnan(value)) |
396 | 53.3k | precision = 0; |
397 | 568k | else if (c_isfinite(value)) { |
398 | 456k | if ((precision < 1) || (100 < precision)) |
399 | 17 | mxRangeError("invalid precision"); |
400 | 456k | mode = 'g'; |
401 | 456k | } |
402 | 112k | else |
403 | 112k | precision = 0; |
404 | 622k | } |
405 | 663k | fxNumberToString(the, value, buffer, sizeof(buffer), mode, (int)precision); |
406 | 663k | fxCopyStringC(the, mxResult, buffer); |
407 | 663k | } |
408 | | |
409 | | void fx_Number_prototype_toString(txMachine* the) |
410 | 691k | { |
411 | 691k | txInteger radix; |
412 | 691k | txSlot* slot = fxCheckNumber(the, mxThis); |
413 | 691k | if (!slot) mxTypeError("this: not a number"); |
414 | 691k | if ((mxArgc > 0) && (!mxIsUndefined(mxArgv(0)))) { |
415 | 690k | radix = fxToInteger(the, mxArgv(0)); |
416 | 690k | if ((radix < 2) || (36 < radix)) |
417 | 318 | mxRangeError("invalid radix"); |
418 | 690k | } |
419 | 329 | else |
420 | 329 | radix = 10; |
421 | 690k | mxResult->kind = slot->kind; |
422 | 690k | mxResult->value = slot->value; |
423 | 690k | if (radix == 10) |
424 | 329 | fxToString(the, mxResult); |
425 | 690k | else { |
426 | 690k | txNumber value = mxResult->value.number; |
427 | 690k | switch (c_fpclassify(value)) { |
428 | 260k | case C_FP_INFINITE: |
429 | 260k | if (value < 0) |
430 | 260k | fxStringX(the, mxResult, "-Infinity"); |
431 | 245 | else |
432 | 245 | fxStringX(the, mxResult, "Infinity"); |
433 | 260k | break; |
434 | 80.7k | case C_FP_NAN: |
435 | 80.7k | fxStringX(the, mxResult, "NaN"); |
436 | 80.7k | break; |
437 | 274k | case C_FP_ZERO: |
438 | 274k | fxStringX(the, mxResult, "0"); |
439 | 274k | break; |
440 | 74.5k | default: { |
441 | | // Thanks Google V8 for the fraction part |
442 | 74.5k | static const char gxDigits[] ICACHE_FLASH_ATTR = "0123456789abcdefghijklmnopqrstuvwxyz"; |
443 | 74.5k | txInteger minus; |
444 | 74.5k | txNumber integer; |
445 | 74.5k | txNumber fraction; |
446 | 74.5k | txNumber next; |
447 | 74.5k | txU8* nextCast = (txU8*)&next; |
448 | 74.5k | txNumber delta; |
449 | 74.5k | txSize length; |
450 | 74.5k | txString string; |
451 | 74.5k | txNumber modulo; |
452 | 74.5k | if (value < 0) { |
453 | 4.33k | minus = 1; |
454 | 4.33k | value = -value; |
455 | 4.33k | } |
456 | 70.2k | else |
457 | 70.2k | minus = 0; |
458 | 74.5k | integer = c_floor(value); |
459 | 74.5k | fraction = value - integer; |
460 | 74.5k | next = value; |
461 | 74.5k | *nextCast = *nextCast + 1; |
462 | 74.5k | delta = 0.5 * (next - value); |
463 | 74.5k | next = 0; |
464 | 74.5k | *nextCast = *nextCast + 1; |
465 | 74.5k | if (delta < next) |
466 | 0 | delta = next; |
467 | 74.5k | length = minus + ((integer)? (txSize)c_round(c_log1p(integer) / c_log(radix)) : 0) + 1; |
468 | 74.5k | if (fraction >= delta) { |
469 | 248k | #define mxFractionPartLength 2048 |
470 | 6.89k | txString dot; |
471 | 6.89k | txInteger i = 0; |
472 | 6.89k | txInteger digit; |
473 | 6.89k | string = mxResult->value.string = fxNewChunk(the, length + 1 + mxFractionPartLength + 1); |
474 | 6.89k | dot = string + length; |
475 | 6.89k | dot[i++] = '.'; |
476 | 248k | do { |
477 | 248k | fraction *= radix; |
478 | 248k | delta *= radix; |
479 | 248k | digit = (txInteger)fraction; |
480 | 248k | dot[i++] = c_read8(gxDigits + digit); |
481 | 248k | fraction -= digit; |
482 | 248k | if (fraction > 0.5 || (fraction == 0.5 && (digit & 1))) { |
483 | 130k | if (fraction + delta > 1) { |
484 | 1.93k | for (;;) { |
485 | 1.93k | char c; |
486 | 1.93k | i--; |
487 | 1.93k | if (i == 0) { |
488 | 0 | integer += 1; |
489 | 0 | break; |
490 | 0 | } |
491 | 1.93k | c = dot[i]; |
492 | 1.93k | digit = c > '9' ? (c - 'a' + 10) : (c - '0'); |
493 | 1.93k | if (digit + 1 < radix) { |
494 | 1.64k | dot[i++] = c_read8(gxDigits + digit + 1); |
495 | 1.64k | break; |
496 | 1.64k | } |
497 | 1.93k | } |
498 | 1.64k | break; |
499 | 1.64k | } |
500 | 130k | } |
501 | 248k | } while ((fraction >= delta) && (i < mxFractionPartLength)); |
502 | 6.89k | dot[i++] = 0; |
503 | 6.89k | length += i; |
504 | 6.89k | string = dot; |
505 | 6.89k | } |
506 | 67.6k | else { |
507 | 67.6k | length += 1; |
508 | 67.6k | string = mxResult->value.string = fxNewChunk(the, length); |
509 | 67.6k | string += length; |
510 | 67.6k | *(--string) = 0; |
511 | 67.6k | } |
512 | 74.5k | modulo = C_MAX_SAFE_INTEGER * radix; |
513 | 9.23M | while (integer > modulo) { |
514 | 9.16M | *(--string) = '0'; |
515 | 9.16M | integer = integer / radix; |
516 | 9.16M | } |
517 | 2.30M | do { |
518 | 2.30M | modulo = c_fmod(integer, radix); |
519 | 2.30M | *(--string) = c_read8(gxDigits + (txInteger)modulo); |
520 | 2.30M | integer = (integer - modulo) / radix; |
521 | 2.30M | } while (integer >= 1); |
522 | 74.5k | if (minus) { |
523 | 4.33k | *(--string) = '-'; |
524 | 4.33k | } |
525 | 74.5k | minus = (txInteger)(string - mxResult->value.string); |
526 | 74.5k | if (minus > 0) { |
527 | 69.1k | length -= minus; |
528 | 69.1k | c_memmove(mxResult->value.string, string, length); |
529 | 69.1k | } |
530 | 74.5k | mxResult->value.string = fxRenewChunk(the, mxResult->value.string, length); |
531 | 74.5k | mxResult->kind = XS_STRING_KIND; |
532 | 74.5k | }} |
533 | 690k | } |
534 | 690k | } |
535 | | |
536 | | void fx_Number_prototype_valueOf(txMachine* the) |
537 | 434k | { |
538 | 434k | txSlot* slot = fxCheckNumber(the, mxThis); |
539 | 434k | if (!slot) mxTypeError("this: not a number"); |
540 | 434k | mxResult->kind = slot->kind; |
541 | 434k | mxResult->value = slot->value; |
542 | 434k | } |
543 | | |
544 | | txSlot* fxCheckNumber(txMachine* the, txSlot* it) |
545 | 2.04M | { |
546 | 2.04M | txSlot* result = C_NULL; |
547 | 2.04M | if (it->kind == XS_INTEGER_KIND) { |
548 | 253k | fxToNumber(the, it); |
549 | 253k | result = it; |
550 | 253k | } |
551 | 1.79M | else if (it->kind == XS_NUMBER_KIND) |
552 | 1.36M | result = it; |
553 | 433k | else if (it->kind == XS_REFERENCE_KIND) { |
554 | 433k | txSlot* instance = it->value.reference; |
555 | 433k | it = instance->next; |
556 | 433k | if ((it) && (it->flag & XS_INTERNAL_FLAG) && (it->kind == XS_NUMBER_KIND)) |
557 | 433k | result = it; |
558 | 433k | } |
559 | 2.04M | return result; |
560 | 2.04M | } |