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