-
Notifications
You must be signed in to change notification settings - Fork 72
/
xs6Number.c
429 lines (408 loc) · 12.4 KB
/
xs6Number.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (C) 2010-2015 Marvell International Ltd.
* Copyright (C) 2002-2010 Kinoma, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "xs6All.h"
static void fx_isFinite(txMachine* the);
static void fx_isNaN(txMachine* the);
static void fx_parseFloat(txMachine* the);
static void fx_parseInt(txMachine* the);
static void fx_Number(txMachine* the);
static void fx_Number_isFinite(txMachine* the);
static void fx_Number_isInteger(txMachine* the);
static void fx_Number_isNaN(txMachine* the);
static void fx_Number_isSafeInteger(txMachine* the);
static void fx_Number_prototype_toExponential(txMachine* the);
static void fx_Number_prototype_toFixed(txMachine* the);
static void fx_Number_prototype_toPrecision(txMachine* the);
static void fx_Number_prototype_toString(txMachine* the);
static void fx_Number_prototype_valueOf(txMachine* the);
static txSlot* fxCheckNumber(txMachine* the, txSlot* it);
void fxBuildNumber(txMachine* the)
{
static const txHostFunctionBuilder gx_global_builders[] = {
{ fx_isFinite, 1, _isFinite },
{ fx_isNaN, 1, _isNaN },
{ fx_parseFloat, 1, _parseFloat },
{ fx_parseInt, 2, _parseInt },
{ C_NULL, 0, 0 },
};
static const txHostFunctionBuilder gx_Number_prototype_builders[] = {
{ fx_Number_prototype_toExponential, 1, _toExponential },
{ fx_Number_prototype_toFixed, 1, _toFixed },
{ fx_Number_prototype_toString, 0, _toLocaleString },
{ fx_Number_prototype_toPrecision, 1, _toPrecision },
{ fx_Number_prototype_toString, 1, _toString },
{ fx_Number_prototype_valueOf, 0, _valueOf },
{ C_NULL, 0, 0 },
};
static const txHostFunctionBuilder gx_Number_builders[] = {
{ fx_Number_isFinite, 1, _isFinite },
{ fx_Number_isInteger, 1, _isInteger },
{ fx_Number_isNaN, 1, _isNaN },
{ fx_Number_isSafeInteger, 1, _isSafeInteger },
{ fx_parseFloat, 1, _parseFloat },
{ fx_parseInt, 1, _parseInt },
{ C_NULL, 0, 0 },
};
const txHostFunctionBuilder* builder;
txSlot* slot;
for (builder = gx_global_builders; builder->callback; builder++) {
fxNewHostFunctionGlobal(the, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
the->stack++;
}
slot = fxSetGlobalProperty(the, mxGlobal.value.reference, mxID(_Infinity));
slot->flag = XS_GET_ONLY;
slot->kind = XS_NUMBER_KIND;
slot->value.number = (txNumber)C_INFINITY;
slot = fxSetGlobalProperty(the, mxGlobal.value.reference, mxID(_NaN));
slot->flag = XS_GET_ONLY;
slot->kind = XS_NUMBER_KIND;
slot->value.number = C_NAN;
mxPush(mxObjectPrototype);
slot = fxLastProperty(the, fxNewNumberInstance(the));
for (builder = gx_Number_prototype_builders; builder->callback; builder++)
slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
mxNumberPrototype = *the->stack;
slot = fxLastProperty(the, fxNewHostConstructorGlobal(the, fx_Number, 1, mxID(_Number), XS_DONT_ENUM_FLAG));
for (builder = gx_Number_builders; builder->callback; builder++)
slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
slot = fxNextNumberProperty(the, slot, C_EPSILON, mxID(_EPSILON), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
slot = fxNextNumberProperty(the, slot, C_MAX_SAFE_INTEGER, mxID(_MAX_SAFE_INTEGER), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
slot = fxNextNumberProperty(the, slot, C_DBL_MAX, mxID(_MAX_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
slot = fxNextNumberProperty(the, slot, C_MIN_SAFE_INTEGER, mxID(_MIN_SAFE_INTEGER), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
slot = fxNextNumberProperty(the, slot, C_DBL_MIN, mxID(_MIN_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
slot = fxNextNumberProperty(the, slot, C_NAN, mxID(_NaN), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
slot = fxNextNumberProperty(the, slot, -((txNumber)C_INFINITY), mxID(_NEGATIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
slot = fxNextNumberProperty(the, slot, (txNumber)C_INFINITY, mxID(_POSITIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
the->stack++;
}
txSlot* fxNewNumberInstance(txMachine* the)
{
txSlot* instance;
txSlot* property;
instance = fxNewObjectInstance(the);
property = fxNextNumberProperty(the, instance, 0, XS_NO_ID, XS_INTERNAL_FLAG | XS_GET_ONLY);
return instance;
}
void fx_isFinite(txMachine* the)
{
int fpclass;
txNumber number = (mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0));
mxResult->kind = XS_BOOLEAN_KIND;
mxResult->value.boolean = 0;
fpclass = c_fpclassify(number);
if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE))
mxResult->value.boolean = 1;
}
void fx_isNaN(txMachine* the)
{
int fpclass;
txNumber number = (mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0));
mxResult->kind = XS_BOOLEAN_KIND;
mxResult->value.boolean = 0;
fpclass = c_fpclassify(number);
if (fpclass == FP_NAN)
mxResult->value.boolean = 1;
}
void fx_parseFloat(txMachine* the)
{
if (mxArgc < 1) {
mxResult->value.number = C_NAN;
mxResult->kind = XS_NUMBER_KIND;
return;
}
fxToString(the, mxArgv(0));
mxResult->kind = XS_NUMBER_KIND;
mxResult->value.number = fxStringToNumber(the->dtoa, mxArgv(0)->value.string, 0);
}
void fx_parseInt(txMachine* the)
{
txInteger aRadix, aDigit;
txNumber aSign, aResult;
txString s, r;
char c;
if (mxArgc < 1) {
mxResult->value.number = C_NAN;
mxResult->kind = XS_NUMBER_KIND;
return;
}
fxToString(the, mxArgv(0));
if (mxArgc > 1) {
aRadix = fxToInteger(the, mxArgv(1));
if (aRadix) {
if ((aRadix < 2) || (36 < aRadix)) {
mxResult->kind = XS_NUMBER_KIND;
mxResult->value.number = C_NAN;
return;
}
}
}
else
aRadix = 0;
s = fxSkipSpaces(mxArgv(0)->value.string);
c = *s;
aSign = 1;
if (c == '+')
s++;
else if (c == '-') {
s++;
aSign = -1;
}
if ((*s == '0') && ((*(s + 1) == 'x') || (*(s + 1) == 'X'))) {
if ((aRadix == 0) || (aRadix == 16)) {
aRadix = 16;
s += 2;
}
}
/*if (*s == '0') {
if ((aRadix == 0) || (aRadix == 8)) {
aRadix = 8;
}
}*/
if (aRadix == 0)
aRadix = 10;
aResult = 0;
r = s;
while ((c = *s)) {
if (('0' <= c) && (c <= '9'))
aDigit = c - '0';
else if (('a' <= c) && (c <= 'z'))
aDigit = 10 + c - 'a';
else if (('A' <= c) && (c <= 'Z'))
aDigit = 10 + c - 'A';
else
break;
if (aDigit >= aRadix)
break;
aResult = (aResult * aRadix) + aDigit;
s++;
}
if (r == s) {
mxResult->kind = XS_NUMBER_KIND;
mxResult->value.number = C_NAN;
}
else {
aResult *= aSign;
aRadix = (txInteger)aResult;
aSign = aRadix;
if (aSign == aResult) {
mxResult->value.integer = aRadix;
mxResult->kind = XS_INTEGER_KIND;
}
else {
mxResult->value.number = aResult;
mxResult->kind = XS_NUMBER_KIND;
}
}
}
void fx_Number(txMachine* the)
{
txSlot* slot = fxCheckNumber(the, mxThis);
txNumber value = 0;
if (mxArgc > 0)
value = fxToNumber(the, mxArgv(0));
if (slot)
slot->value.number = value;
else {
txInteger integer = (txInteger)value;
txNumber number = integer;
if (number == value) {
mxResult->kind = XS_INTEGER_KIND;
mxResult->value.integer = integer;
}
else {
mxResult->kind = XS_NUMBER_KIND;
mxResult->value.number = value;
}
}
}
void fx_Number_isFinite(txMachine* the)
{
int fpclass;
txSlot* slot = (mxArgc < 1) ? C_NULL : fxCheckNumber(the, mxArgv(0));
mxResult->kind = XS_BOOLEAN_KIND;
mxResult->value.boolean = 0;
if (slot) {
fpclass = c_fpclassify(slot->value.number);
if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE))
mxResult->value.boolean = 1;
}
}
void fx_Number_isInteger(txMachine* the)
{
int fpclass;
txSlot* slot = (mxArgc < 1) ? C_NULL : fxCheckNumber(the, mxArgv(0));
mxResult->kind = XS_BOOLEAN_KIND;
mxResult->value.boolean = 0;
if (slot) {
fpclass = c_fpclassify(slot->value.number);
if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE)) {
txNumber check = c_trunc(slot->value.number);
if (slot->value.number == check)
mxResult->value.boolean = 1;
}
}
}
void fx_Number_isNaN(txMachine* the)
{
int fpclass;
txSlot* slot = (mxArgc < 1) ? C_NULL : fxCheckNumber(the, mxArgv(0));
mxResult->kind = XS_BOOLEAN_KIND;
mxResult->value.boolean = 0;
if (slot) {
fpclass = c_fpclassify(slot->value.number);
if (fpclass == FP_NAN)
mxResult->value.boolean = 1;
}
}
void fx_Number_isSafeInteger(txMachine* the)
{
int fpclass;
txSlot* slot = (mxArgc < 1) ? C_NULL : fxCheckNumber(the, mxArgv(0));
mxResult->kind = XS_BOOLEAN_KIND;
mxResult->value.boolean = 0;
if (slot) {
fpclass = c_fpclassify(slot->value.number);
if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE)) {
txNumber check = c_trunc(slot->value.number);
if (slot->value.number == check) {
if ((C_MIN_SAFE_INTEGER <= check) && (check <= C_MAX_SAFE_INTEGER))
mxResult->value.boolean = 1;
}
}
}
}
void fx_Number_prototype_toExponential(txMachine* the)
{
char buffer[256];
int precision;
txSlot* slot = fxCheckNumber(the, mxThis);
if (!slot) mxTypeError("this is no number");
if (mxArgc > 0)
precision = fxToInteger(the, mxArgv(0));
else
precision = 0;
fxNumberToString(the->dtoa, slot->value.number, buffer, sizeof(buffer), 'e', precision);
fxCopyStringC(the, mxResult, buffer);
}
void fx_Number_prototype_toFixed(txMachine* the)
{
char buffer[256];
int precision;
txSlot* slot = fxCheckNumber(the, mxThis);
if (!slot) mxTypeError("this is no number");
if (mxArgc > 0)
precision = fxToInteger(the, mxArgv(0));
else
precision = 0;
fxNumberToString(the->dtoa, slot->value.number, buffer, sizeof(buffer), 'f', precision);
fxCopyStringC(the, mxResult, buffer);
}
void fx_Number_prototype_toPrecision(txMachine* the)
{
char buffer[256];
int precision;
txSlot* slot = fxCheckNumber(the, mxThis);
if (!slot) mxTypeError("this is no number");
if (mxArgc > 0)
precision = fxToInteger(the, mxArgv(0));
else
precision = 0;
fxNumberToString(the->dtoa, slot->value.number, buffer, sizeof(buffer), 'g', precision);
fxCopyStringC(the, mxResult, buffer);
}
void fx_Number_prototype_toString(txMachine* the)
{
char buffer[256];
txInteger radix;
txSlot* slot = fxCheckNumber(the, mxThis);
if (!slot) mxTypeError("this is no number");
if (mxArgc > 0) {
radix = fxToInteger(the, mxArgv(0));
if (radix) {
if ((radix < 2) || (36 < radix))
mxRangeError("invalid radix");
}
else
radix = 10;
}
else
radix = 10;
mxResult->kind = slot->kind;
mxResult->value = slot->value;
if (radix == 10)
fxToString(the, mxResult);
else {
static char gxDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
txString string = buffer + sizeof(buffer);
txNumber value;
txBoolean minus;
value = mxResult->value.number;
switch (c_fpclassify(value)) {
case C_FP_INFINITE:
if (value < 0)
fxCopyStringC(the, mxResult, "-Infinity");
else
fxCopyStringC(the, mxResult, "Infinity");
break;
case C_FP_NAN:
fxCopyStringC(the, mxResult, "NaN");
break;
case C_FP_ZERO:
fxCopyStringC(the, mxResult, "0");
break;
default:
*(--string) = 0;
if (value < 0) {
minus = 1;
value = -value;
}
else
minus = 0;
do {
*(--string) = gxDigits[(txInteger)c_fmod(value, radix)];
value = value / radix;
} while (value >= 1);
if (minus)
*(--string) = '-';
fxCopyStringC(the, mxResult, string);
}
}
}
void fx_Number_prototype_valueOf(txMachine* the)
{
txSlot* slot = fxCheckNumber(the, mxThis);
if (!slot) mxTypeError("this is no number");
mxResult->kind = slot->kind;
mxResult->value = slot->value;
}
txSlot* fxCheckNumber(txMachine* the, txSlot* it)
{
txSlot* result = C_NULL;
if (it->kind == XS_INTEGER_KIND) {
fxToNumber(the, it);
result = it;
}
else if (it->kind == XS_NUMBER_KIND)
result = it;
else if (it->kind == XS_REFERENCE_KIND) {
txSlot* instance = it->value.reference;
it = instance->next;
if ((it) && (it->flag & XS_INTERNAL_FLAG) && (it->kind == XS_NUMBER_KIND))
result = it;
}
return result;
}