/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 | 16.3k | { |
44 | 16.3k | txSlot* slot; |
45 | | |
46 | 16.3k | mxPushNumber((txNumber)C_INFINITY); |
47 | 16.3k | mxPull(mxInfinity); |
48 | 16.3k | mxPushNumber((txNumber)C_NAN); |
49 | 16.3k | mxPull(mxNaN); |
50 | | |
51 | 16.3k | mxPush(mxObjectPrototype); |
52 | 16.3k | slot = fxLastProperty(the, fxNewNumberInstance(the)); |
53 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toExponential), 1, mxID(_toExponential), XS_DONT_ENUM_FLAG); |
54 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toFixed), 1, mxID(_toFixed), XS_DONT_ENUM_FLAG); |
55 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toLocaleString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG); |
56 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toPrecision), 1, mxID(_toPrecision), XS_DONT_ENUM_FLAG); |
57 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toString), 1, mxID(_toString), XS_DONT_ENUM_FLAG); |
58 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG); |
59 | 16.3k | mxNumberPrototype = *the->stack; |
60 | 16.3k | slot = fxBuildHostConstructor(the, mxCallback(fx_Number), 1, mxID(_Number)); |
61 | 16.3k | mxNumberConstructor = *the->stack; |
62 | 16.3k | slot = fxLastProperty(the, slot); |
63 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isFinite), 1, mxID(_isFinite), XS_DONT_ENUM_FLAG); |
64 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isInteger), 1, mxID(_isInteger), XS_DONT_ENUM_FLAG); |
65 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isNaN), 1, mxID(_isNaN), XS_DONT_ENUM_FLAG); |
66 | 16.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isSafeInteger), 1, mxID(_isSafeInteger), XS_DONT_ENUM_FLAG); |
67 | 16.3k | slot = fxNextSlotProperty(the, slot, &mxParseFloatFunction, mxID(_parseFloat), XS_DONT_ENUM_FLAG); |
68 | 16.3k | slot = fxNextSlotProperty(the, slot, &mxParseIntFunction, mxID(_parseInt), XS_DONT_ENUM_FLAG); |
69 | 16.3k | slot = fxNextNumberProperty(the, slot, C_EPSILON, mxID(_EPSILON), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
70 | 16.3k | 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 | 16.3k | slot = fxNextNumberProperty(the, slot, C_DBL_MAX, mxID(_MAX_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
72 | 16.3k | 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 | 16.3k | slot = fxNextNumberProperty(the, slot, C_DBL_MIN, mxID(_MIN_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
74 | 16.3k | slot = fxNextNumberProperty(the, slot, C_NAN, mxID(_NaN), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
75 | 16.3k | slot = fxNextNumberProperty(the, slot, -((txNumber)C_INFINITY), mxID(_NEGATIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
76 | 16.3k | slot = fxNextNumberProperty(the, slot, (txNumber)C_INFINITY, mxID(_POSITIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
77 | 16.3k | mxPop(); |
78 | 16.3k | } |
79 | | |
80 | | txSlot* fxNewNumberInstance(txMachine* the) |
81 | 9.43M | { |
82 | 9.43M | txSlot* instance; |
83 | 9.43M | instance = fxNewObjectInstance(the); |
84 | 9.43M | fxNextNumberProperty(the, instance, 0, XS_NO_ID, XS_INTERNAL_FLAG); |
85 | 9.43M | return instance; |
86 | 9.43M | } |
87 | | |
88 | | void fxNumberCoerce(txMachine* the, txSlot* slot) |
89 | 1.81k | { |
90 | 1.81k | fxToNumber(the, slot); |
91 | 1.81k | } |
92 | | |
93 | | void fx_isFinite(txMachine* the) |
94 | 205 | { |
95 | 205 | int fpclass; |
96 | 205 | txNumber number = (mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0)); |
97 | 205 | mxResult->kind = XS_BOOLEAN_KIND; |
98 | 205 | mxResult->value.boolean = 0; |
99 | 205 | fpclass = c_fpclassify(number); |
100 | 205 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) |
101 | 180 | mxResult->value.boolean = 1; |
102 | 205 | } |
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 | 666 | mxResult->value.boolean = 1; |
113 | 1.66k | } |
114 | | |
115 | | void fx_parseFloat(txMachine* the) |
116 | 41.3k | { |
117 | 41.3k | if (mxArgc < 1) { |
118 | 128 | mxResult->value.number = C_NAN; |
119 | 128 | mxResult->kind = XS_NUMBER_KIND; |
120 | 128 | return; |
121 | 128 | } |
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 | 1.06M | { |
129 | 1.06M | txInteger aRadix, aDigit; |
130 | 1.06M | txNumber aSign, aResult; |
131 | 1.06M | txString s, r; |
132 | 1.06M | char c; |
133 | | |
134 | 1.06M | if (mxArgc < 1) { |
135 | 74 | mxResult->value.number = C_NAN; |
136 | 74 | mxResult->kind = XS_NUMBER_KIND; |
137 | 74 | return; |
138 | 74 | } |
139 | 1.06M | fxToString(the, mxArgv(0)); |
140 | 1.06M | if (mxArgc > 1) { |
141 | 1.05M | aRadix = fxToInteger(the, mxArgv(1)); |
142 | 1.05M | if (aRadix) { |
143 | 903k | if ((aRadix < 2) || (36 < aRadix)) { |
144 | 83.9k | mxResult->kind = XS_NUMBER_KIND; |
145 | 83.9k | mxResult->value.number = C_NAN; |
146 | 83.9k | return; |
147 | 83.9k | } |
148 | 903k | } |
149 | 1.05M | } |
150 | 2.54k | else |
151 | 2.54k | aRadix = 0; |
152 | 978k | s = fxSkipSpaces(mxArgv(0)->value.string); |
153 | 978k | c = *s; |
154 | 978k | aSign = 1; |
155 | 978k | if (c == '+') |
156 | 667 | s++; |
157 | 977k | else if (c == '-') { |
158 | 1.30k | s++; |
159 | 1.30k | aSign = -1; |
160 | 1.30k | } |
161 | 978k | if ((*s == '0') && ((*(s + 1) == 'x') || (*(s + 1) == 'X'))) { |
162 | 217 | if ((aRadix == 0) || (aRadix == 16)) { |
163 | 214 | aRadix = 16; |
164 | 214 | s += 2; |
165 | 214 | } |
166 | 217 | } |
167 | | /*if (*s == '0') { |
168 | | if ((aRadix == 0) || (aRadix == 8)) { |
169 | | aRadix = 8; |
170 | | } |
171 | | }*/ |
172 | 978k | if (aRadix == 0) |
173 | 159k | aRadix = 10; |
174 | 978k | aResult = 0; |
175 | 978k | r = s; |
176 | 1.05M | while ((c = *s)) { |
177 | 175k | if (('0' <= c) && (c <= '9')) |
178 | 43.0k | aDigit = c - '0'; |
179 | 132k | else if (('a' <= c) && (c <= 'z')) |
180 | 1.96k | aDigit = 10 + c - 'a'; |
181 | 130k | else if (('A' <= c) && (c <= 'Z')) |
182 | 29.8k | aDigit = 10 + c - 'A'; |
183 | 101k | else |
184 | 101k | break; |
185 | 74.8k | if (aDigit >= aRadix) |
186 | 1.33k | break; |
187 | 73.4k | aResult = (aResult * aRadix) + aDigit; |
188 | 73.4k | s++; |
189 | 73.4k | } |
190 | 978k | if (r == s) { |
191 | 948k | mxResult->kind = XS_NUMBER_KIND; |
192 | 948k | mxResult->value.number = C_NAN; |
193 | 948k | } |
194 | 30.2k | else { |
195 | 30.2k | aResult *= aSign; |
196 | 30.2k | aRadix = (txInteger)aResult; |
197 | 30.2k | aSign = aRadix; |
198 | 30.2k | if (aSign == aResult) { |
199 | 29.3k | mxResult->value.integer = aRadix; |
200 | 29.3k | mxResult->kind = XS_INTEGER_KIND; |
201 | 29.3k | } |
202 | 866 | else { |
203 | 866 | mxResult->value.number = aResult; |
204 | 866 | mxResult->kind = XS_NUMBER_KIND; |
205 | 866 | } |
206 | 30.2k | } |
207 | 978k | } |
208 | | |
209 | | void fx_Number(txMachine* the) |
210 | 241k | { |
211 | 241k | txNumber value = 0; |
212 | 241k | if (mxArgc > 0) { |
213 | 129k | txSlot* slot = mxArgv(0); |
214 | 129k | if (slot->kind == XS_REFERENCE_KIND) |
215 | 364 | fxToPrimitive(the, slot, XS_NUMBER_HINT); |
216 | 129k | if ((slot->kind == XS_BIGINT_KIND) || (slot->kind == XS_BIGINT_X_KIND)) |
217 | 378 | value = fxBigIntToNumber(the, slot); |
218 | 128k | else |
219 | 128k | value = fxToNumber(the, slot); |
220 | 129k | } |
221 | 241k | if (mxIsUndefined(mxTarget)) { |
222 | 241k | mxResult->kind = XS_NUMBER_KIND; |
223 | 241k | mxResult->value.number = value; |
224 | 241k | fx_Math_toInteger(the); |
225 | 241k | } |
226 | 360 | else { |
227 | 360 | txSlot* instance; |
228 | 360 | mxPushSlot(mxTarget); |
229 | 360 | fxGetPrototypeFromConstructor(the, &mxNumberPrototype); |
230 | 360 | instance = fxNewNumberInstance(the); |
231 | 360 | instance->next->value.number = value; |
232 | 360 | mxPullSlot(mxResult); |
233 | 360 | } |
234 | 241k | } |
235 | | |
236 | | void fx_Number_isFinite(txMachine* the) |
237 | 55 | { |
238 | 55 | int fpclass; |
239 | 55 | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
240 | 55 | mxResult->kind = XS_BOOLEAN_KIND; |
241 | 55 | mxResult->value.boolean = 0; |
242 | 55 | if (slot) { |
243 | 54 | if (slot->kind == XS_INTEGER_KIND) |
244 | 14 | mxResult->value.boolean = 1; |
245 | 40 | else if (slot->kind == XS_NUMBER_KIND) { |
246 | 23 | fpclass = c_fpclassify(slot->value.number); |
247 | 23 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) |
248 | 13 | mxResult->value.boolean = 1; |
249 | 23 | } |
250 | 54 | } |
251 | 55 | } |
252 | | |
253 | | void fx_Number_isInteger(txMachine* the) |
254 | 117 | { |
255 | 117 | int fpclass; |
256 | 117 | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
257 | 117 | mxResult->kind = XS_BOOLEAN_KIND; |
258 | 117 | mxResult->value.boolean = 0; |
259 | 117 | if (slot) { |
260 | 116 | if (slot->kind == XS_INTEGER_KIND) |
261 | 76 | mxResult->value.boolean = 1; |
262 | 40 | else if (slot->kind == XS_NUMBER_KIND) { |
263 | 25 | fpclass = c_fpclassify(slot->value.number); |
264 | 25 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) { |
265 | 21 | txNumber check = c_trunc(slot->value.number); |
266 | 21 | if (slot->value.number == check) |
267 | 11 | mxResult->value.boolean = 1; |
268 | 21 | } |
269 | 25 | } |
270 | 116 | } |
271 | 117 | } |
272 | | |
273 | | void fx_Number_isNaN(txMachine* the) |
274 | 36 | { |
275 | 36 | int fpclass; |
276 | 36 | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
277 | 36 | mxResult->kind = XS_BOOLEAN_KIND; |
278 | 36 | mxResult->value.boolean = 0; |
279 | 36 | if (slot) { |
280 | 35 | if (slot->kind == XS_NUMBER_KIND) { |
281 | 20 | fpclass = c_fpclassify(slot->value.number); |
282 | 20 | if (fpclass == C_FP_NAN) |
283 | 10 | mxResult->value.boolean = 1; |
284 | 20 | } |
285 | 35 | } |
286 | 36 | } |
287 | | |
288 | | void fx_Number_isSafeInteger(txMachine* the) |
289 | 2.97k | { |
290 | 2.97k | int fpclass; |
291 | 2.97k | txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0); |
292 | 2.97k | mxResult->kind = XS_BOOLEAN_KIND; |
293 | 2.97k | mxResult->value.boolean = 0; |
294 | 2.97k | if (slot) { |
295 | 2.97k | if (slot->kind == XS_INTEGER_KIND) |
296 | 4 | mxResult->value.boolean = 1; |
297 | 2.96k | else if (slot->kind == XS_NUMBER_KIND) { |
298 | 25 | fpclass = c_fpclassify(slot->value.number); |
299 | 25 | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) { |
300 | 21 | txNumber check = c_trunc(slot->value.number); |
301 | 21 | if (slot->value.number == check) { |
302 | 11 | if ((C_MIN_SAFE_INTEGER <= check) && (check <= C_MAX_SAFE_INTEGER)) |
303 | 6 | mxResult->value.boolean = 1; |
304 | 11 | } |
305 | 21 | } |
306 | 25 | } |
307 | 2.97k | } |
308 | 2.97k | } |
309 | | |
310 | | void fx_Number_prototype_toExponential(txMachine* the) |
311 | 70.5k | { |
312 | 70.5k | char buffer[256]; |
313 | 70.5k | txInteger mode = 0; |
314 | 70.5k | txNumber precision = 0; |
315 | 70.5k | txNumber value; |
316 | 70.5k | txSlot* slot = fxCheckNumber(the, mxThis); |
317 | 70.5k | if (!slot) mxTypeError("this: not a number"); |
318 | 70.5k | value = slot->value.number; |
319 | 70.5k | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) { |
320 | 70.5k | precision = fxToNumber(the, mxArgv(0)); |
321 | 70.5k | if (c_isnan(precision)) |
322 | 4 | precision = 0; |
323 | 70.5k | else if (c_isfinite(precision)) |
324 | 70.5k | precision = c_trunc(precision); |
325 | 70.5k | } |
326 | 20 | else |
327 | 20 | precision = 0; |
328 | 70.5k | if (c_isnan(value)) |
329 | 7 | precision = 0; |
330 | 70.5k | else if (c_isfinite(value)) { |
331 | 106 | if ((precision < 0) || (100 < precision)) |
332 | 3 | mxRangeError("invalid fractionDigits"); |
333 | 103 | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) |
334 | 83 | precision++; |
335 | 103 | mode = 'e'; |
336 | 103 | } |
337 | 70.4k | else |
338 | 70.4k | precision = 0; |
339 | 70.5k | fxNumberToString(the, slot->value.number, buffer, sizeof(buffer), mode, (int)precision); |
340 | 70.5k | fxCopyStringC(the, mxResult, buffer); |
341 | 70.5k | } |
342 | | |
343 | | void fx_Number_prototype_toFixed(txMachine* the) |
344 | 106k | { |
345 | 106k | char buffer[256]; |
346 | 106k | txInteger mode = 0; |
347 | 106k | txNumber precision = 0; |
348 | 106k | txNumber value; |
349 | 106k | txSlot* slot = fxCheckNumber(the, mxThis); |
350 | 106k | if (!slot) mxTypeError("this: not a number"); |
351 | 106k | value = slot->value.number; |
352 | 106k | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) { |
353 | 104k | precision = fxToNumber(the, mxArgv(0)); |
354 | 104k | if (c_isnan(precision)) |
355 | 7 | precision = 0; |
356 | 104k | else if (c_isfinite(precision)) |
357 | 104k | precision = c_trunc(precision); |
358 | 104k | } |
359 | 2.15k | else |
360 | 2.15k | precision = 0; |
361 | 106k | if ((precision < 0) || (100 < precision)) |
362 | 5 | mxRangeError("invalid fractionDigits"); |
363 | 106k | if (c_fabs(value) >= 1e21) |
364 | 15 | precision = 0; |
365 | 106k | else |
366 | 106k | mode = 'f'; |
367 | 106k | fxNumberToString(the, value, buffer, sizeof(buffer), mode, (int)precision); |
368 | 106k | fxCopyStringC(the, mxResult, buffer); |
369 | 106k | } |
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 | 414k | { |
382 | 414k | char buffer[256]; |
383 | 414k | txInteger mode = 0; |
384 | 414k | txNumber precision = 0; |
385 | 414k | txNumber value; |
386 | 414k | txSlot* slot = fxCheckNumber(the, mxThis); |
387 | 414k | if (!slot) mxTypeError("this: not a number"); |
388 | 414k | value = slot->value.number; |
389 | 414k | if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) { |
390 | 414k | precision = fxToNumber(the, mxArgv(0)); |
391 | 414k | if (c_isnan(precision)) |
392 | 3 | precision = 0; |
393 | 414k | else if (c_isfinite(precision)) |
394 | 414k | precision = c_trunc(precision); |
395 | 414k | if (c_isnan(value)) |
396 | 139k | precision = 0; |
397 | 275k | else if (c_isfinite(value)) { |
398 | 275k | if ((precision < 1) || (100 < precision)) |
399 | 6 | mxRangeError("invalid precision"); |
400 | 275k | mode = 'g'; |
401 | 275k | } |
402 | 174 | else |
403 | 174 | precision = 0; |
404 | 414k | } |
405 | 414k | fxNumberToString(the, value, buffer, sizeof(buffer), mode, (int)precision); |
406 | 414k | fxCopyStringC(the, mxResult, buffer); |
407 | 414k | } |
408 | | |
409 | | void fx_Number_prototype_toString(txMachine* the) |
410 | 686k | { |
411 | 686k | txInteger radix; |
412 | 686k | txSlot* slot = fxCheckNumber(the, mxThis); |
413 | 686k | if (!slot) mxTypeError("this: not a number"); |
414 | 686k | if ((mxArgc > 0) && (!mxIsUndefined(mxArgv(0)))) { |
415 | 686k | radix = fxToInteger(the, mxArgv(0)); |
416 | 686k | if ((radix < 2) || (36 < radix)) |
417 | 311 | mxRangeError("invalid radix"); |
418 | 686k | } |
419 | 236 | else |
420 | 236 | radix = 10; |
421 | 686k | mxResult->kind = slot->kind; |
422 | 686k | mxResult->value = slot->value; |
423 | 686k | if (radix == 10) |
424 | 236 | fxToString(the, mxResult); |
425 | 686k | else { |
426 | 686k | txNumber value = mxResult->value.number; |
427 | 686k | switch (c_fpclassify(value)) { |
428 | 260k | case C_FP_INFINITE: |
429 | 260k | if (value < 0) |
430 | 260k | fxStringX(the, mxResult, "-Infinity"); |
431 | 110 | else |
432 | 110 | fxStringX(the, mxResult, "Infinity"); |
433 | 260k | break; |
434 | 5 | case C_FP_NAN: |
435 | 5 | fxStringX(the, mxResult, "NaN"); |
436 | 5 | break; |
437 | 273k | case C_FP_ZERO: |
438 | 273k | fxStringX(the, mxResult, "0"); |
439 | 273k | break; |
440 | 151k | default: { |
441 | | // Thanks Google V8 for the fraction part |
442 | 151k | static const char gxDigits[] ICACHE_FLASH_ATTR = "0123456789abcdefghijklmnopqrstuvwxyz"; |
443 | 151k | txInteger minus; |
444 | 151k | txNumber integer; |
445 | 151k | txNumber fraction; |
446 | 151k | txNumber next; |
447 | 151k | txU8* nextCast = (txU8*)&next; |
448 | 151k | txNumber delta; |
449 | 151k | txSize length; |
450 | 151k | txString string; |
451 | 151k | txNumber modulo; |
452 | 151k | if (value < 0) { |
453 | 2.34k | minus = 1; |
454 | 2.34k | value = -value; |
455 | 2.34k | } |
456 | 149k | else |
457 | 149k | minus = 0; |
458 | 151k | integer = c_floor(value); |
459 | 151k | fraction = value - integer; |
460 | 151k | next = value; |
461 | 151k | *nextCast = *nextCast + 1; |
462 | 151k | delta = 0.5 * (next - value); |
463 | 151k | next = 0; |
464 | 151k | *nextCast = *nextCast + 1; |
465 | 151k | if (delta < next) |
466 | 0 | delta = next; |
467 | 151k | length = minus + ((integer)? (txSize)c_round(c_log1p(integer) / c_log(radix)) : 0) + 1; |
468 | 151k | if (fraction >= delta) { |
469 | 78.0k | #define mxFractionPartLength 2048 |
470 | 2.19k | txString dot; |
471 | 2.19k | txInteger i = 0; |
472 | 2.19k | txInteger digit; |
473 | 2.19k | string = mxResult->value.string = fxNewChunk(the, length + 1 + mxFractionPartLength + 1); |
474 | 2.19k | dot = string + length; |
475 | 2.19k | dot[i++] = '.'; |
476 | 78.0k | do { |
477 | 78.0k | fraction *= radix; |
478 | 78.0k | delta *= radix; |
479 | 78.0k | digit = (txInteger)fraction; |
480 | 78.0k | dot[i++] = c_read8(gxDigits + digit); |
481 | 78.0k | fraction -= digit; |
482 | 78.0k | if (fraction > 0.5 || (fraction == 0.5 && (digit & 1))) { |
483 | 34.9k | if (fraction + delta > 1) { |
484 | 916 | for (;;) { |
485 | 916 | char c; |
486 | 916 | i--; |
487 | 916 | if (i == 0) { |
488 | 0 | integer += 1; |
489 | 0 | break; |
490 | 0 | } |
491 | 916 | c = dot[i]; |
492 | 916 | digit = c > '9' ? (c - 'a' + 10) : (c - '0'); |
493 | 916 | if (digit + 1 < radix) { |
494 | 886 | dot[i++] = c_read8(gxDigits + digit + 1); |
495 | 886 | break; |
496 | 886 | } |
497 | 916 | } |
498 | 886 | break; |
499 | 886 | } |
500 | 34.9k | } |
501 | 78.0k | } while ((fraction >= delta) && (i < mxFractionPartLength)); |
502 | 2.19k | dot[i++] = 0; |
503 | 2.19k | length += i; |
504 | 2.19k | string = dot; |
505 | 2.19k | } |
506 | 149k | else { |
507 | 149k | length += 1; |
508 | 149k | string = mxResult->value.string = fxNewChunk(the, length); |
509 | 149k | string += length; |
510 | 149k | *(--string) = 0; |
511 | 149k | } |
512 | 151k | modulo = C_MAX_SAFE_INTEGER * radix; |
513 | 51.0M | while (integer > modulo) { |
514 | 50.9M | *(--string) = '0'; |
515 | 50.9M | integer = integer / radix; |
516 | 50.9M | } |
517 | 5.20M | do { |
518 | 5.20M | modulo = c_fmod(integer, radix); |
519 | 5.20M | *(--string) = c_read8(gxDigits + (txInteger)modulo); |
520 | 5.20M | integer = (integer - modulo) / radix; |
521 | 5.20M | } while (integer >= 1); |
522 | 151k | if (minus) { |
523 | 2.34k | *(--string) = '-'; |
524 | 2.34k | } |
525 | 151k | minus = (txInteger)(string - mxResult->value.string); |
526 | 151k | if (minus > 0) { |
527 | 1.86k | length -= minus; |
528 | 1.86k | c_memmove(mxResult->value.string, string, length); |
529 | 1.86k | } |
530 | 151k | mxResult->value.string = fxRenewChunk(the, mxResult->value.string, length); |
531 | 151k | mxResult->kind = XS_STRING_KIND; |
532 | 151k | }} |
533 | 686k | } |
534 | 686k | } |
535 | | |
536 | | void fx_Number_prototype_valueOf(txMachine* the) |
537 | 161k | { |
538 | 161k | txSlot* slot = fxCheckNumber(the, mxThis); |
539 | 161k | if (!slot) mxTypeError("this: not a number"); |
540 | 161k | mxResult->kind = slot->kind; |
541 | 161k | mxResult->value = slot->value; |
542 | 161k | } |
543 | | |
544 | | txSlot* fxCheckNumber(txMachine* the, txSlot* it) |
545 | 1.44M | { |
546 | 1.44M | txSlot* result = C_NULL; |
547 | 1.44M | if (it->kind == XS_INTEGER_KIND) { |
548 | 253k | fxToNumber(the, it); |
549 | 253k | result = it; |
550 | 253k | } |
551 | 1.18M | else if (it->kind == XS_NUMBER_KIND) |
552 | 1.02M | result = it; |
553 | 161k | else if (it->kind == XS_REFERENCE_KIND) { |
554 | 161k | txSlot* instance = it->value.reference; |
555 | 161k | it = instance->next; |
556 | 161k | if ((it) && (it->flag & XS_INTERNAL_FLAG) && (it->kind == XS_NUMBER_KIND)) |
557 | 161k | result = it; |
558 | 161k | } |
559 | 1.44M | return result; |
560 | 1.44M | } |