@agoric/xsnap 0.14.3-u14.0 → 0.14.3-u16.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 (137) hide show
  1. package/README.md +3 -3
  2. package/api.js +4 -2
  3. package/build.env +1 -1
  4. package/moddable/modules/data/base64/base64.js +28 -0
  5. package/moddable/modules/data/base64/manifest.json +11 -0
  6. package/moddable/modules/data/base64/modBase64.c +188 -0
  7. package/moddable/modules/data/binaryMessage/BinaryMessage.js +106 -0
  8. package/moddable/modules/data/crc/crc.c +205 -0
  9. package/moddable/modules/data/crc/crc.js +36 -0
  10. package/moddable/modules/data/crc/manifest.json +8 -0
  11. package/moddable/modules/data/hex/hex.js +28 -0
  12. package/moddable/modules/data/hex/manifest.json +11 -0
  13. package/moddable/modules/data/hex/modHex.c +139 -0
  14. package/moddable/modules/data/logical/logical.js +32 -0
  15. package/moddable/modules/data/logical/modLogical.c +98 -0
  16. package/moddable/modules/data/qrcode/manifest.json +9 -0
  17. package/moddable/modules/data/qrcode/qrcode.c +93 -0
  18. package/moddable/modules/data/qrcode/qrcode.js +23 -0
  19. package/moddable/modules/data/qrcode/qrcodegen.c +1025 -0
  20. package/moddable/modules/data/qrcode/qrcodegen.h +267 -0
  21. package/moddable/modules/data/text/decoder/manifest.json +8 -0
  22. package/moddable/modules/data/text/decoder/textdecoder.c +480 -0
  23. package/moddable/modules/data/text/decoder/textdecoder.js +27 -0
  24. package/moddable/modules/data/text/encoder/manifest.json +8 -0
  25. package/moddable/modules/data/text/encoder/textencoder.c +232 -0
  26. package/moddable/modules/data/text/encoder/textencoder.js +24 -0
  27. package/moddable/modules/data/tinyint/tinyint.c +150 -0
  28. package/moddable/modules/data/tinyint/tinyint.js +53 -0
  29. package/moddable/modules/data/url/manifest.json +17 -0
  30. package/moddable/modules/data/url/url.c +1959 -0
  31. package/moddable/modules/data/url/url.js +210 -0
  32. package/moddable/modules/data/wavreader/manifest.json +8 -0
  33. package/moddable/modules/data/wavreader/wavreader.js +128 -0
  34. package/moddable/modules/data/zlib/deflate.c +161 -0
  35. package/moddable/modules/data/zlib/deflate.js +63 -0
  36. package/moddable/modules/data/zlib/inflate.c +145 -0
  37. package/moddable/modules/data/zlib/inflate.js +66 -0
  38. package/moddable/modules/data/zlib/manifest_deflate.json +9 -0
  39. package/moddable/modules/data/zlib/manifest_inflate.json +9 -0
  40. package/moddable/modules/data/zlib/miniz.c +4924 -0
  41. package/moddable/xs/includes/xs.d.ts +73 -0
  42. package/moddable/xs/includes/xs.h +1533 -0
  43. package/moddable/xs/includes/xsmc.h +206 -0
  44. package/moddable/xs/makefiles/lin/makefile +33 -0
  45. package/moddable/xs/makefiles/lin/xsc.mk +118 -0
  46. package/moddable/xs/makefiles/lin/xsid.mk +90 -0
  47. package/moddable/xs/makefiles/lin/xsl.mk +168 -0
  48. package/moddable/xs/makefiles/lin/xst.mk +201 -0
  49. package/moddable/xs/makefiles/mac/makefile +33 -0
  50. package/moddable/xs/makefiles/mac/xsc.mk +130 -0
  51. package/moddable/xs/makefiles/mac/xsid.mk +102 -0
  52. package/moddable/xs/makefiles/mac/xsl.mk +177 -0
  53. package/moddable/xs/makefiles/mac/xst.mk +203 -0
  54. package/moddable/xs/makefiles/mac/xst_no_asan.txt +52 -0
  55. package/moddable/xs/makefiles/win/build.bat +26 -0
  56. package/moddable/xs/makefiles/win/xsc.mak +142 -0
  57. package/moddable/xs/makefiles/win/xsid.mak +113 -0
  58. package/moddable/xs/makefiles/win/xsl.mak +186 -0
  59. package/moddable/xs/makefiles/win/xst.mak +195 -0
  60. package/moddable/xs/platforms/lin_xs.h +99 -0
  61. package/moddable/xs/platforms/mac_xs.h +97 -0
  62. package/moddable/xs/platforms/wasm_xs.h +79 -0
  63. package/moddable/xs/platforms/win_xs.h +104 -0
  64. package/moddable/xs/platforms/xsHost.h +63 -0
  65. package/moddable/xs/platforms/xsPlatform.h +618 -0
  66. package/moddable/xs/sources/xsAPI.c +2555 -0
  67. package/moddable/xs/sources/xsAll.c +294 -0
  68. package/moddable/xs/sources/xsAll.h +2741 -0
  69. package/moddable/xs/sources/xsArguments.c +222 -0
  70. package/moddable/xs/sources/xsArray.c +2657 -0
  71. package/moddable/xs/sources/xsAtomics.c +844 -0
  72. package/moddable/xs/sources/xsBigInt.c +1859 -0
  73. package/moddable/xs/sources/xsBoolean.c +109 -0
  74. package/moddable/xs/sources/xsCode.c +4493 -0
  75. package/moddable/xs/sources/xsCommon.c +1710 -0
  76. package/moddable/xs/sources/xsCommon.h +1142 -0
  77. package/moddable/xs/sources/xsDataView.c +2890 -0
  78. package/moddable/xs/sources/xsDate.c +1541 -0
  79. package/moddable/xs/sources/xsDebug.c +2710 -0
  80. package/moddable/xs/sources/xsDefaults.c +134 -0
  81. package/moddable/xs/sources/xsError.c +353 -0
  82. package/moddable/xs/sources/xsFunction.c +776 -0
  83. package/moddable/xs/sources/xsGenerator.c +865 -0
  84. package/moddable/xs/sources/xsGlobal.c +839 -0
  85. package/moddable/xs/sources/xsJSON.c +1091 -0
  86. package/moddable/xs/sources/xsLexical.c +1969 -0
  87. package/moddable/xs/sources/xsLockdown.c +933 -0
  88. package/moddable/xs/sources/xsMapSet.c +1649 -0
  89. package/moddable/xs/sources/xsMarshall.c +1020 -0
  90. package/moddable/xs/sources/xsMath.c +624 -0
  91. package/moddable/xs/sources/xsMemory.c +1941 -0
  92. package/moddable/xs/sources/xsModule.c +3101 -0
  93. package/moddable/xs/sources/xsNumber.c +560 -0
  94. package/moddable/xs/sources/xsObject.c +1102 -0
  95. package/moddable/xs/sources/xsPlatforms.c +480 -0
  96. package/moddable/xs/sources/xsProfile.c +577 -0
  97. package/moddable/xs/sources/xsPromise.c +1199 -0
  98. package/moddable/xs/sources/xsProperty.c +636 -0
  99. package/moddable/xs/sources/xsProxy.c +1014 -0
  100. package/moddable/xs/sources/xsRegExp.c +1168 -0
  101. package/moddable/xs/sources/xsRun.c +4889 -0
  102. package/moddable/xs/sources/xsScope.c +1293 -0
  103. package/moddable/xs/sources/xsScript.c +288 -0
  104. package/moddable/xs/sources/xsScript.h +1186 -0
  105. package/moddable/xs/sources/xsSnapshot.c +2161 -0
  106. package/moddable/xs/sources/xsSnapshot.h +51 -0
  107. package/moddable/xs/sources/xsSourceMap.c +218 -0
  108. package/moddable/xs/sources/xsString.c +3332 -0
  109. package/moddable/xs/sources/xsSymbol.c +503 -0
  110. package/moddable/xs/sources/xsSyntaxical.c +4193 -0
  111. package/moddable/xs/sources/xsTree.c +1893 -0
  112. package/moddable/xs/sources/xsType.c +1488 -0
  113. package/moddable/xs/sources/xsdtoa.c +6672 -0
  114. package/moddable/xs/sources/xsmc.c +340 -0
  115. package/moddable/xs/sources/xsre.c +7578 -0
  116. package/package.json +37 -20
  117. package/scripts/get_xsnap_version.sh +14 -0
  118. package/scripts/test-package.sh +21 -0
  119. package/src/avaAssertXS.js +6 -2
  120. package/src/avaHandler.cjs +2 -5
  121. package/src/avaXS.js +7 -8
  122. package/src/build.js +161 -28
  123. package/src/replay.js +0 -3
  124. package/src/xsnap.js +105 -91
  125. package/src/xsrepl.js +2 -3
  126. package/xsnap-native/xsnap/makefiles/lin/makefile +10 -0
  127. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +156 -0
  128. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +144 -0
  129. package/xsnap-native/xsnap/makefiles/mac/makefile +10 -0
  130. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +165 -0
  131. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +153 -0
  132. package/xsnap-native/xsnap/sources/xsnap-worker.c +1008 -0
  133. package/xsnap-native/xsnap/sources/xsnap.c +717 -0
  134. package/xsnap-native/xsnap/sources/xsnap.h +142 -0
  135. package/xsnap-native/xsnap/sources/xsnapPlatform.c +1501 -0
  136. package/xsnap-native/xsnap/sources/xsnapPlatform.h +105 -0
  137. package/CHANGELOG.md +0 -654
@@ -0,0 +1,560 @@
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
+ }