Coverage Report

Created: 2026-12-01 06:31

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