@agoric/xsnap 0.14.3-dev-b047af2.0 → 0.14.3-dev-3eb4230.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/api.js +1 -1
  2. package/package.json +16 -23
  3. package/src/build.js +4 -61
  4. package/moddable/modules/data/base64/base64.js +0 -28
  5. package/moddable/modules/data/base64/manifest.json +0 -11
  6. package/moddable/modules/data/base64/modBase64.c +0 -188
  7. package/moddable/modules/data/binaryMessage/BinaryMessage.js +0 -106
  8. package/moddable/modules/data/crc/crc.c +0 -205
  9. package/moddable/modules/data/crc/crc.js +0 -36
  10. package/moddable/modules/data/crc/manifest.json +0 -8
  11. package/moddable/modules/data/hex/hex.js +0 -28
  12. package/moddable/modules/data/hex/manifest.json +0 -11
  13. package/moddable/modules/data/hex/modHex.c +0 -139
  14. package/moddable/modules/data/logical/logical.js +0 -32
  15. package/moddable/modules/data/logical/modLogical.c +0 -98
  16. package/moddable/modules/data/qrcode/manifest.json +0 -9
  17. package/moddable/modules/data/qrcode/qrcode.c +0 -93
  18. package/moddable/modules/data/qrcode/qrcode.js +0 -23
  19. package/moddable/modules/data/qrcode/qrcodegen.c +0 -1025
  20. package/moddable/modules/data/qrcode/qrcodegen.h +0 -267
  21. package/moddable/modules/data/text/decoder/manifest.json +0 -8
  22. package/moddable/modules/data/text/decoder/textdecoder.c +0 -480
  23. package/moddable/modules/data/text/decoder/textdecoder.js +0 -27
  24. package/moddable/modules/data/text/encoder/manifest.json +0 -8
  25. package/moddable/modules/data/text/encoder/textencoder.c +0 -232
  26. package/moddable/modules/data/text/encoder/textencoder.js +0 -24
  27. package/moddable/modules/data/tinyint/tinyint.c +0 -150
  28. package/moddable/modules/data/tinyint/tinyint.js +0 -53
  29. package/moddable/modules/data/url/manifest.json +0 -17
  30. package/moddable/modules/data/url/url.c +0 -1959
  31. package/moddable/modules/data/url/url.js +0 -210
  32. package/moddable/modules/data/wavreader/manifest.json +0 -8
  33. package/moddable/modules/data/wavreader/wavreader.js +0 -128
  34. package/moddable/modules/data/zlib/deflate.c +0 -161
  35. package/moddable/modules/data/zlib/deflate.js +0 -63
  36. package/moddable/modules/data/zlib/inflate.c +0 -145
  37. package/moddable/modules/data/zlib/inflate.js +0 -66
  38. package/moddable/modules/data/zlib/manifest_deflate.json +0 -9
  39. package/moddable/modules/data/zlib/manifest_inflate.json +0 -9
  40. package/moddable/modules/data/zlib/miniz.c +0 -4924
  41. package/moddable/xs/includes/xs.d.ts +0 -73
  42. package/moddable/xs/includes/xs.h +0 -1533
  43. package/moddable/xs/includes/xsmc.h +0 -206
  44. package/moddable/xs/makefiles/lin/makefile +0 -33
  45. package/moddable/xs/makefiles/lin/xsc.mk +0 -118
  46. package/moddable/xs/makefiles/lin/xsid.mk +0 -90
  47. package/moddable/xs/makefiles/lin/xsl.mk +0 -168
  48. package/moddable/xs/makefiles/lin/xst.mk +0 -201
  49. package/moddable/xs/makefiles/mac/makefile +0 -33
  50. package/moddable/xs/makefiles/mac/xsc.mk +0 -130
  51. package/moddable/xs/makefiles/mac/xsid.mk +0 -102
  52. package/moddable/xs/makefiles/mac/xsl.mk +0 -177
  53. package/moddable/xs/makefiles/mac/xst.mk +0 -203
  54. package/moddable/xs/makefiles/mac/xst_no_asan.txt +0 -52
  55. package/moddable/xs/makefiles/win/build.bat +0 -26
  56. package/moddable/xs/makefiles/win/xsc.mak +0 -142
  57. package/moddable/xs/makefiles/win/xsid.mak +0 -113
  58. package/moddable/xs/makefiles/win/xsl.mak +0 -186
  59. package/moddable/xs/makefiles/win/xst.mak +0 -195
  60. package/moddable/xs/platforms/lin_xs.h +0 -99
  61. package/moddable/xs/platforms/mac_xs.h +0 -97
  62. package/moddable/xs/platforms/wasm_xs.h +0 -79
  63. package/moddable/xs/platforms/win_xs.h +0 -104
  64. package/moddable/xs/platforms/xsHost.h +0 -63
  65. package/moddable/xs/platforms/xsPlatform.h +0 -618
  66. package/moddable/xs/sources/xsAPI.c +0 -2555
  67. package/moddable/xs/sources/xsAll.c +0 -294
  68. package/moddable/xs/sources/xsAll.h +0 -2741
  69. package/moddable/xs/sources/xsArguments.c +0 -222
  70. package/moddable/xs/sources/xsArray.c +0 -2657
  71. package/moddable/xs/sources/xsAtomics.c +0 -844
  72. package/moddable/xs/sources/xsBigInt.c +0 -1859
  73. package/moddable/xs/sources/xsBoolean.c +0 -109
  74. package/moddable/xs/sources/xsCode.c +0 -4493
  75. package/moddable/xs/sources/xsCommon.c +0 -1710
  76. package/moddable/xs/sources/xsCommon.h +0 -1142
  77. package/moddable/xs/sources/xsDataView.c +0 -2890
  78. package/moddable/xs/sources/xsDate.c +0 -1541
  79. package/moddable/xs/sources/xsDebug.c +0 -2710
  80. package/moddable/xs/sources/xsDefaults.c +0 -134
  81. package/moddable/xs/sources/xsError.c +0 -353
  82. package/moddable/xs/sources/xsFunction.c +0 -776
  83. package/moddable/xs/sources/xsGenerator.c +0 -865
  84. package/moddable/xs/sources/xsGlobal.c +0 -839
  85. package/moddable/xs/sources/xsJSON.c +0 -1091
  86. package/moddable/xs/sources/xsLexical.c +0 -1969
  87. package/moddable/xs/sources/xsLockdown.c +0 -933
  88. package/moddable/xs/sources/xsMapSet.c +0 -1649
  89. package/moddable/xs/sources/xsMarshall.c +0 -1020
  90. package/moddable/xs/sources/xsMath.c +0 -624
  91. package/moddable/xs/sources/xsMemory.c +0 -1941
  92. package/moddable/xs/sources/xsModule.c +0 -3101
  93. package/moddable/xs/sources/xsNumber.c +0 -560
  94. package/moddable/xs/sources/xsObject.c +0 -1102
  95. package/moddable/xs/sources/xsPlatforms.c +0 -480
  96. package/moddable/xs/sources/xsProfile.c +0 -577
  97. package/moddable/xs/sources/xsPromise.c +0 -1199
  98. package/moddable/xs/sources/xsProperty.c +0 -636
  99. package/moddable/xs/sources/xsProxy.c +0 -1014
  100. package/moddable/xs/sources/xsRegExp.c +0 -1168
  101. package/moddable/xs/sources/xsRun.c +0 -4889
  102. package/moddable/xs/sources/xsScope.c +0 -1293
  103. package/moddable/xs/sources/xsScript.c +0 -288
  104. package/moddable/xs/sources/xsScript.h +0 -1186
  105. package/moddable/xs/sources/xsSnapshot.c +0 -2161
  106. package/moddable/xs/sources/xsSnapshot.h +0 -51
  107. package/moddable/xs/sources/xsSourceMap.c +0 -218
  108. package/moddable/xs/sources/xsString.c +0 -3332
  109. package/moddable/xs/sources/xsSymbol.c +0 -503
  110. package/moddable/xs/sources/xsSyntaxical.c +0 -4193
  111. package/moddable/xs/sources/xsTree.c +0 -1893
  112. package/moddable/xs/sources/xsType.c +0 -1488
  113. package/moddable/xs/sources/xsdtoa.c +0 -6672
  114. package/moddable/xs/sources/xsmc.c +0 -340
  115. package/moddable/xs/sources/xsre.c +0 -7578
  116. package/xsnap-native/xsnap/makefiles/lin/makefile +0 -10
  117. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +0 -153
  118. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +0 -144
  119. package/xsnap-native/xsnap/makefiles/mac/makefile +0 -10
  120. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +0 -162
  121. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +0 -153
  122. package/xsnap-native/xsnap/sources/xsnap-worker.c +0 -996
  123. package/xsnap-native/xsnap/sources/xsnap.c +0 -716
  124. package/xsnap-native/xsnap/sources/xsnap.h +0 -142
  125. package/xsnap-native/xsnap/sources/xsnapPlatform.c +0 -1496
  126. package/xsnap-native/xsnap/sources/xsnapPlatform.h +0 -105
@@ -1,560 +0,0 @@
1
- /*
2
- * Copyright (c) 2016-2017 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
- {
44
- txSlot* slot;
45
-
46
- mxPushNumber((txNumber)C_INFINITY);
47
- mxPull(mxInfinity);
48
- mxPushNumber((txNumber)C_NAN);
49
- mxPull(mxNaN);
50
-
51
- mxPush(mxObjectPrototype);
52
- slot = fxLastProperty(the, fxNewNumberInstance(the));
53
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toExponential), 1, mxID(_toExponential), XS_DONT_ENUM_FLAG);
54
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toFixed), 1, mxID(_toFixed), XS_DONT_ENUM_FLAG);
55
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toLocaleString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG);
56
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toPrecision), 1, mxID(_toPrecision), XS_DONT_ENUM_FLAG);
57
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_toString), 1, mxID(_toString), XS_DONT_ENUM_FLAG);
58
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG);
59
- mxNumberPrototype = *the->stack;
60
- slot = fxBuildHostConstructor(the, mxCallback(fx_Number), 1, mxID(_Number));
61
- mxNumberConstructor = *the->stack;
62
- slot = fxLastProperty(the, slot);
63
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isFinite), 1, mxID(_isFinite), XS_DONT_ENUM_FLAG);
64
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isInteger), 1, mxID(_isInteger), XS_DONT_ENUM_FLAG);
65
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isNaN), 1, mxID(_isNaN), XS_DONT_ENUM_FLAG);
66
- slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Number_isSafeInteger), 1, mxID(_isSafeInteger), XS_DONT_ENUM_FLAG);
67
- slot = fxNextSlotProperty(the, slot, &mxParseFloatFunction, mxID(_parseFloat), XS_DONT_ENUM_FLAG);
68
- slot = fxNextSlotProperty(the, slot, &mxParseIntFunction, mxID(_parseInt), XS_DONT_ENUM_FLAG);
69
- slot = fxNextNumberProperty(the, slot, C_EPSILON, mxID(_EPSILON), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
70
- 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
- slot = fxNextNumberProperty(the, slot, C_DBL_MAX, mxID(_MAX_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
72
- 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
- slot = fxNextNumberProperty(the, slot, C_DBL_MIN, mxID(_MIN_VALUE), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
74
- slot = fxNextNumberProperty(the, slot, C_NAN, mxID(_NaN), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
75
- slot = fxNextNumberProperty(the, slot, -((txNumber)C_INFINITY), mxID(_NEGATIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
76
- slot = fxNextNumberProperty(the, slot, (txNumber)C_INFINITY, mxID(_POSITIVE_INFINITY), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
77
- mxPop();
78
- }
79
-
80
- txSlot* fxNewNumberInstance(txMachine* the)
81
- {
82
- txSlot* instance;
83
- instance = fxNewObjectInstance(the);
84
- fxNextNumberProperty(the, instance, 0, XS_NO_ID, XS_INTERNAL_FLAG);
85
- return instance;
86
- }
87
-
88
- void fxNumberCoerce(txMachine* the, txSlot* slot)
89
- {
90
- fxToNumber(the, slot);
91
- }
92
-
93
- void fx_isFinite(txMachine* the)
94
- {
95
- int fpclass;
96
- txNumber number = (mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0));
97
- mxResult->kind = XS_BOOLEAN_KIND;
98
- mxResult->value.boolean = 0;
99
- fpclass = c_fpclassify(number);
100
- if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE))
101
- mxResult->value.boolean = 1;
102
- }
103
-
104
- void fx_isNaN(txMachine* the)
105
- {
106
- int fpclass;
107
- txNumber number = (mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0));
108
- mxResult->kind = XS_BOOLEAN_KIND;
109
- mxResult->value.boolean = 0;
110
- fpclass = c_fpclassify(number);
111
- if (fpclass == FP_NAN)
112
- mxResult->value.boolean = 1;
113
- }
114
-
115
- void fx_parseFloat(txMachine* the)
116
- {
117
- if (mxArgc < 1) {
118
- mxResult->value.number = C_NAN;
119
- mxResult->kind = XS_NUMBER_KIND;
120
- return;
121
- }
122
- fxToString(the, mxArgv(0));
123
- mxResult->kind = XS_NUMBER_KIND;
124
- mxResult->value.number = fxStringToNumber(the->dtoa, mxArgv(0)->value.string, 0);
125
- }
126
-
127
- void fx_parseInt(txMachine* the)
128
- {
129
- txInteger aRadix, aDigit;
130
- txNumber aSign, aResult;
131
- txString s, r;
132
- char c;
133
-
134
- if (mxArgc < 1) {
135
- mxResult->value.number = C_NAN;
136
- mxResult->kind = XS_NUMBER_KIND;
137
- return;
138
- }
139
- fxToString(the, mxArgv(0));
140
- if (mxArgc > 1) {
141
- aRadix = fxToInteger(the, mxArgv(1));
142
- if (aRadix) {
143
- if ((aRadix < 2) || (36 < aRadix)) {
144
- mxResult->kind = XS_NUMBER_KIND;
145
- mxResult->value.number = C_NAN;
146
- return;
147
- }
148
- }
149
- }
150
- else
151
- aRadix = 0;
152
- s = fxSkipSpaces(mxArgv(0)->value.string);
153
- c = *s;
154
- aSign = 1;
155
- if (c == '+')
156
- s++;
157
- else if (c == '-') {
158
- s++;
159
- aSign = -1;
160
- }
161
- if ((*s == '0') && ((*(s + 1) == 'x') || (*(s + 1) == 'X'))) {
162
- if ((aRadix == 0) || (aRadix == 16)) {
163
- aRadix = 16;
164
- s += 2;
165
- }
166
- }
167
- /*if (*s == '0') {
168
- if ((aRadix == 0) || (aRadix == 8)) {
169
- aRadix = 8;
170
- }
171
- }*/
172
- if (aRadix == 0)
173
- aRadix = 10;
174
- aResult = 0;
175
- r = s;
176
- while ((c = *s)) {
177
- if (('0' <= c) && (c <= '9'))
178
- aDigit = c - '0';
179
- else if (('a' <= c) && (c <= 'z'))
180
- aDigit = 10 + c - 'a';
181
- else if (('A' <= c) && (c <= 'Z'))
182
- aDigit = 10 + c - 'A';
183
- else
184
- break;
185
- if (aDigit >= aRadix)
186
- break;
187
- aResult = (aResult * aRadix) + aDigit;
188
- s++;
189
- }
190
- if (r == s) {
191
- mxResult->kind = XS_NUMBER_KIND;
192
- mxResult->value.number = C_NAN;
193
- }
194
- else {
195
- aResult *= aSign;
196
- aRadix = (txInteger)aResult;
197
- aSign = aRadix;
198
- if (aSign == aResult) {
199
- mxResult->value.integer = aRadix;
200
- mxResult->kind = XS_INTEGER_KIND;
201
- }
202
- else {
203
- mxResult->value.number = aResult;
204
- mxResult->kind = XS_NUMBER_KIND;
205
- }
206
- }
207
- }
208
-
209
- void fx_Number(txMachine* the)
210
- {
211
- txNumber value = 0;
212
- if (mxArgc > 0) {
213
- txSlot* slot = mxArgv(0);
214
- if (slot->kind == XS_REFERENCE_KIND)
215
- fxToPrimitive(the, slot, XS_NUMBER_HINT);
216
- if ((slot->kind == XS_BIGINT_KIND) || (slot->kind == XS_BIGINT_X_KIND))
217
- value = fxBigIntToNumber(the, slot);
218
- else
219
- value = fxToNumber(the, slot);
220
- }
221
- if (mxIsUndefined(mxTarget)) {
222
- mxResult->kind = XS_NUMBER_KIND;
223
- mxResult->value.number = value;
224
- fx_Math_toInteger(the);
225
- }
226
- else {
227
- txSlot* instance;
228
- mxPushSlot(mxTarget);
229
- fxGetPrototypeFromConstructor(the, &mxNumberPrototype);
230
- instance = fxNewNumberInstance(the);
231
- instance->next->value.number = value;
232
- mxPullSlot(mxResult);
233
- }
234
- }
235
-
236
- void fx_Number_isFinite(txMachine* the)
237
- {
238
- int fpclass;
239
- txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0);
240
- mxResult->kind = XS_BOOLEAN_KIND;
241
- mxResult->value.boolean = 0;
242
- if (slot) {
243
- if (slot->kind == XS_INTEGER_KIND)
244
- mxResult->value.boolean = 1;
245
- else if (slot->kind == XS_NUMBER_KIND) {
246
- fpclass = c_fpclassify(slot->value.number);
247
- if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE))
248
- mxResult->value.boolean = 1;
249
- }
250
- }
251
- }
252
-
253
- void fx_Number_isInteger(txMachine* the)
254
- {
255
- int fpclass;
256
- txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0);
257
- mxResult->kind = XS_BOOLEAN_KIND;
258
- mxResult->value.boolean = 0;
259
- if (slot) {
260
- if (slot->kind == XS_INTEGER_KIND)
261
- mxResult->value.boolean = 1;
262
- else if (slot->kind == XS_NUMBER_KIND) {
263
- fpclass = c_fpclassify(slot->value.number);
264
- if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE)) {
265
- txNumber check = c_trunc(slot->value.number);
266
- if (slot->value.number == check)
267
- mxResult->value.boolean = 1;
268
- }
269
- }
270
- }
271
- }
272
-
273
- void fx_Number_isNaN(txMachine* the)
274
- {
275
- int fpclass;
276
- txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0);
277
- mxResult->kind = XS_BOOLEAN_KIND;
278
- mxResult->value.boolean = 0;
279
- if (slot) {
280
- if (slot->kind == XS_NUMBER_KIND) {
281
- fpclass = c_fpclassify(slot->value.number);
282
- if (fpclass == FP_NAN)
283
- mxResult->value.boolean = 1;
284
- }
285
- }
286
- }
287
-
288
- void fx_Number_isSafeInteger(txMachine* the)
289
- {
290
- int fpclass;
291
- txSlot* slot = (mxArgc < 1) ? C_NULL : mxArgv(0);
292
- mxResult->kind = XS_BOOLEAN_KIND;
293
- mxResult->value.boolean = 0;
294
- if (slot) {
295
- if (slot->kind == XS_INTEGER_KIND)
296
- mxResult->value.boolean = 1;
297
- else if (slot->kind == XS_NUMBER_KIND) {
298
- fpclass = c_fpclassify(slot->value.number);
299
- if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE)) {
300
- txNumber check = c_trunc(slot->value.number);
301
- if (slot->value.number == check) {
302
- if ((C_MIN_SAFE_INTEGER <= check) && (check <= C_MAX_SAFE_INTEGER))
303
- mxResult->value.boolean = 1;
304
- }
305
- }
306
- }
307
- }
308
- }
309
-
310
- void fx_Number_prototype_toExponential(txMachine* the)
311
- {
312
- char buffer[256];
313
- txInteger mode = 0;
314
- txNumber precision = 0;
315
- txNumber value;
316
- txSlot* slot = fxCheckNumber(the, mxThis);
317
- if (!slot) mxTypeError("this is no number");
318
- value = slot->value.number;
319
- if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) {
320
- precision = fxToNumber(the, mxArgv(0));
321
- if (c_isnan(precision))
322
- precision = 0;
323
- else if (c_isfinite(precision))
324
- precision = c_trunc(precision);
325
- }
326
- else
327
- precision = 0;
328
- if (c_isnan(value))
329
- precision = 0;
330
- else if (c_isfinite(value)) {
331
- if ((precision < 0) || (100 < precision))
332
- mxRangeError("invalid fractionDigits");
333
- if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0)))
334
- precision++;
335
- mode = 'e';
336
- }
337
- else
338
- precision = 0;
339
- fxNumberToString(the->dtoa, slot->value.number, buffer, sizeof(buffer), mode, (int)precision);
340
- fxCopyStringC(the, mxResult, buffer);
341
- }
342
-
343
- void fx_Number_prototype_toFixed(txMachine* the)
344
- {
345
- char buffer[256];
346
- txInteger mode = 0;
347
- txNumber precision = 0;
348
- txNumber value;
349
- txSlot* slot = fxCheckNumber(the, mxThis);
350
- if (!slot) mxTypeError("this is no number");
351
- value = slot->value.number;
352
- if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) {
353
- precision = fxToNumber(the, mxArgv(0));
354
- if (c_isnan(precision))
355
- precision = 0;
356
- else if (c_isfinite(precision))
357
- precision = c_trunc(precision);
358
- }
359
- else
360
- precision = 0;
361
- if ((precision < 0) || (100 < precision))
362
- mxRangeError("invalid fractionDigits");
363
- if (c_fabs(value) >= 1e21)
364
- precision = 0;
365
- else
366
- mode = 'f';
367
- fxNumberToString(the->dtoa, value, buffer, sizeof(buffer), mode, (int)precision);
368
- fxCopyStringC(the, mxResult, buffer);
369
- }
370
-
371
- void fx_Number_prototype_toLocaleString(txMachine* the)
372
- {
373
- txSlot* slot = fxCheckNumber(the, mxThis);
374
- if (!slot) mxTypeError("this is no number");
375
- mxResult->kind = slot->kind;
376
- mxResult->value = slot->value;
377
- fxToString(the, mxResult);
378
- }
379
-
380
- void fx_Number_prototype_toPrecision(txMachine* the)
381
- {
382
- char buffer[256];
383
- txInteger mode = 0;
384
- txNumber precision = 0;
385
- txNumber value;
386
- txSlot* slot = fxCheckNumber(the, mxThis);
387
- if (!slot) mxTypeError("this is no number");
388
- value = slot->value.number;
389
- if ((mxArgc > 0) && !mxIsUndefined(mxArgv(0))) {
390
- precision = fxToNumber(the, mxArgv(0));
391
- if (c_isnan(precision))
392
- precision = 0;
393
- else if (c_isfinite(precision))
394
- precision = c_trunc(precision);
395
- if (c_isnan(value))
396
- precision = 0;
397
- else if (c_isfinite(value)) {
398
- if ((precision < 1) || (100 < precision))
399
- mxRangeError("invalid precision");
400
- mode = 'g';
401
- }
402
- else
403
- precision = 0;
404
- }
405
- fxNumberToString(the->dtoa, value, buffer, sizeof(buffer), mode, (int)precision);
406
- fxCopyStringC(the, mxResult, buffer);
407
- }
408
-
409
- void fx_Number_prototype_toString(txMachine* the)
410
- {
411
- txInteger radix;
412
- txSlot* slot = fxCheckNumber(the, mxThis);
413
- if (!slot) mxTypeError("this is no number");
414
- if ((mxArgc > 0) && (!mxIsUndefined(mxArgv(0)))) {
415
- radix = fxToInteger(the, mxArgv(0));
416
- if ((radix < 2) || (36 < radix))
417
- mxRangeError("invalid radix");
418
- }
419
- else
420
- radix = 10;
421
- mxResult->kind = slot->kind;
422
- mxResult->value = slot->value;
423
- if (radix == 10)
424
- fxToString(the, mxResult);
425
- else {
426
- txNumber value = mxResult->value.number;
427
- switch (c_fpclassify(value)) {
428
- case C_FP_INFINITE:
429
- if (value < 0)
430
- fxStringX(the, mxResult, "-Infinity");
431
- else
432
- fxStringX(the, mxResult, "Infinity");
433
- break;
434
- case C_FP_NAN:
435
- fxStringX(the, mxResult, "NaN");
436
- break;
437
- case C_FP_ZERO:
438
- fxStringX(the, mxResult, "0");
439
- break;
440
- default: {
441
- // Thanks Google V8 for the fraction part
442
- static const char gxDigits[] ICACHE_FLASH_ATTR = "0123456789abcdefghijklmnopqrstuvwxyz";
443
- txInteger minus;
444
- txNumber integer;
445
- txNumber fraction;
446
- txNumber next;
447
- txU8* nextCast = (txU8*)&next;
448
- txNumber delta;
449
- txSize length;
450
- txString string;
451
- txNumber modulo;
452
- if (value < 0) {
453
- minus = 1;
454
- value = -value;
455
- }
456
- else
457
- minus = 0;
458
- integer = c_floor(value);
459
- fraction = value - integer;
460
- next = value;
461
- *nextCast = *nextCast + 1;
462
- delta = 0.5 * (next - value);
463
- next = 0;
464
- *nextCast = *nextCast + 1;
465
- if (delta < next)
466
- delta = next;
467
- length = minus + ((integer)? (txSize)c_round(c_log1p(integer) / c_log(radix)) : 0) + 1;
468
- if (fraction >= delta) {
469
- #define mxFractionPartLength 2048
470
- txString dot;
471
- txInteger i = 0;
472
- txInteger digit;
473
- string = mxResult->value.string = fxNewChunk(the, length + 1 + mxFractionPartLength + 1);
474
- dot = string + length;
475
- dot[i++] = '.';
476
- do {
477
- fraction *= radix;
478
- delta *= radix;
479
- digit = (txInteger)fraction;
480
- dot[i++] = c_read8(gxDigits + digit);
481
- fraction -= digit;
482
- if (fraction > 0.5 || (fraction == 0.5 && (digit & 1))) {
483
- if (fraction + delta > 1) {
484
- for (;;) {
485
- char c;
486
- i--;
487
- if (i == 0) {
488
- integer += 1;
489
- break;
490
- }
491
- c = dot[i];
492
- digit = c > '9' ? (c - 'a' + 10) : (c - '0');
493
- if (digit + 1 < radix) {
494
- dot[i++] = c_read8(gxDigits + digit + 1);
495
- break;
496
- }
497
- }
498
- break;
499
- }
500
- }
501
- } while ((fraction >= delta) && (i < mxFractionPartLength));
502
- dot[i++] = 0;
503
- length += i;
504
- string = dot;
505
- }
506
- else {
507
- length += 1;
508
- string = mxResult->value.string = fxNewChunk(the, length);
509
- string += length;
510
- *(--string) = 0;
511
- }
512
- modulo = C_MAX_SAFE_INTEGER * radix;
513
- while (integer > modulo) {
514
- *(--string) = '0';
515
- integer = integer / radix;
516
- }
517
- do {
518
- modulo = c_fmod(integer, radix);
519
- *(--string) = c_read8(gxDigits + (txInteger)modulo);
520
- integer = (integer - modulo) / radix;
521
- } while (integer >= 1);
522
- if (minus) {
523
- *(--string) = '-';
524
- }
525
- minus = (txInteger)(string - mxResult->value.string);
526
- if (minus > 0) {
527
- length -= minus;
528
- c_memmove(mxResult->value.string, string, length);
529
- }
530
- mxResult->value.string = fxRenewChunk(the, mxResult->value.string, length);
531
- mxResult->kind = XS_STRING_KIND;
532
- }}
533
- }
534
- }
535
-
536
- void fx_Number_prototype_valueOf(txMachine* the)
537
- {
538
- txSlot* slot = fxCheckNumber(the, mxThis);
539
- if (!slot) mxTypeError("this is no number");
540
- mxResult->kind = slot->kind;
541
- mxResult->value = slot->value;
542
- }
543
-
544
- txSlot* fxCheckNumber(txMachine* the, txSlot* it)
545
- {
546
- txSlot* result = C_NULL;
547
- if (it->kind == XS_INTEGER_KIND) {
548
- fxToNumber(the, it);
549
- result = it;
550
- }
551
- else if (it->kind == XS_NUMBER_KIND)
552
- result = it;
553
- else if (it->kind == XS_REFERENCE_KIND) {
554
- txSlot* instance = it->value.reference;
555
- it = instance->next;
556
- if ((it) && (it->flag & XS_INTERNAL_FLAG) && (it->kind == XS_NUMBER_KIND))
557
- result = it;
558
- }
559
- return result;
560
- }