@agoric/xsnap 0.14.3-u14.0 → 0.14.3-u16.1

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,624 @@
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
+ void fxBuildMath(txMachine* the)
41
+ {
42
+ txSlot* slot;
43
+ mxPush(mxObjectPrototype);
44
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
45
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_abs), 1, mxID(_abs), XS_DONT_ENUM_FLAG);
46
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acos), 1, mxID(_acos), XS_DONT_ENUM_FLAG);
47
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acosh), 1, mxID(_acosh), XS_DONT_ENUM_FLAG);
48
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asin), 1, mxID(_asin), XS_DONT_ENUM_FLAG);
49
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asinh), 1, mxID(_asinh), XS_DONT_ENUM_FLAG);
50
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan), 1, mxID(_atan), XS_DONT_ENUM_FLAG);
51
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atanh), 1, mxID(_atanh), XS_DONT_ENUM_FLAG);
52
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan2), 2, mxID(_atan2), XS_DONT_ENUM_FLAG);
53
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cbrt), 1, mxID(_cbrt), XS_DONT_ENUM_FLAG);
54
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_ceil), 1, mxID(_ceil), XS_DONT_ENUM_FLAG);
55
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_clz32), 1, mxID(_clz32), XS_DONT_ENUM_FLAG);
56
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cos), 1, mxID(_cos), XS_DONT_ENUM_FLAG);
57
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cosh), 1, mxID(_cosh), XS_DONT_ENUM_FLAG);
58
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_exp), 1, mxID(_exp), XS_DONT_ENUM_FLAG);
59
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_expm1), 1, mxID(_expm1), XS_DONT_ENUM_FLAG);
60
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_floor), 1, mxID(_floor), XS_DONT_ENUM_FLAG);
61
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_fround), 1, mxID(_fround), XS_DONT_ENUM_FLAG);
62
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_hypot), 2, mxID(_hypot_), XS_DONT_ENUM_FLAG);
63
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idiv), 2, mxID(_idiv), XS_DONT_ENUM_FLAG);
64
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idivmod), 2, mxID(_idivmod), XS_DONT_ENUM_FLAG);
65
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imod), 2, mxID(_imod), XS_DONT_ENUM_FLAG);
66
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imul), 2, mxID(_imul), XS_DONT_ENUM_FLAG);
67
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imuldiv), 2, mxID(_imuldiv), XS_DONT_ENUM_FLAG);
68
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irem), 2, mxID(_irem), XS_DONT_ENUM_FLAG);
69
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log), 1, mxID(_log), XS_DONT_ENUM_FLAG);
70
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log1p), 1, mxID(_log1p), XS_DONT_ENUM_FLAG);
71
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log10), 1, mxID(_log10), XS_DONT_ENUM_FLAG);
72
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log2), 1, mxID(_log2), XS_DONT_ENUM_FLAG);
73
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_max), 2, mxID(_max), XS_DONT_ENUM_FLAG);
74
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_min), 2, mxID(_min), XS_DONT_ENUM_FLAG);
75
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_mod), 2, mxID(_mod), XS_DONT_ENUM_FLAG);
76
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_pow), 2, mxID(_pow), XS_DONT_ENUM_FLAG);
77
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_random), 0, mxID(_random), XS_DONT_ENUM_FLAG);
78
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_round), 1, mxID(_round), XS_DONT_ENUM_FLAG);
79
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sign), 1, mxID(_sign), XS_DONT_ENUM_FLAG);
80
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sin), 1, mxID(_sin), XS_DONT_ENUM_FLAG);
81
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sinh), 1, mxID(_sinh), XS_DONT_ENUM_FLAG);
82
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sqrt), 1, mxID(_sqrt), XS_DONT_ENUM_FLAG);
83
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tan), 1, mxID(_tan), XS_DONT_ENUM_FLAG);
84
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tanh), 1, mxID(_tanh), XS_DONT_ENUM_FLAG);
85
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_trunc), 1, mxID(_trunc), XS_DONT_ENUM_FLAG);
86
+ slot = fxNextNumberProperty(the, slot, C_M_E, mxID(_E), XS_GET_ONLY);
87
+ slot = fxNextNumberProperty(the, slot, C_M_LN10, mxID(_LN10), XS_GET_ONLY);
88
+ slot = fxNextNumberProperty(the, slot, C_M_LN2, mxID(_LN2), XS_GET_ONLY);
89
+ slot = fxNextNumberProperty(the, slot, C_M_LOG10E, mxID(_LOG10E), XS_GET_ONLY);
90
+ slot = fxNextNumberProperty(the, slot, C_M_LOG2E, mxID(_LOG2E), XS_GET_ONLY);
91
+ slot = fxNextNumberProperty(the, slot, C_M_PI, mxID(_PI), XS_GET_ONLY);
92
+ slot = fxNextNumberProperty(the, slot, C_M_SQRT1_2, mxID(_SQRT1_2), XS_GET_ONLY);
93
+ slot = fxNextNumberProperty(the, slot, C_M_SQRT2, mxID(_SQRT2), XS_GET_ONLY);
94
+ slot = fxNextStringXProperty(the, slot, "Math", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
95
+ mxPull(mxMathObject);
96
+ //@@ c_srand((unsigned)c_time(0));
97
+ }
98
+
99
+ #define mxNanResultIfNoArg \
100
+ if (mxArgc < 1) { \
101
+ mxResult->kind = XS_NUMBER_KIND; \
102
+ mxResult->value.number = C_NAN; \
103
+ return; \
104
+ }
105
+
106
+ #define mxNanResultIfNoArg2 \
107
+ if (mxArgc < 2) { \
108
+ mxResult->kind = XS_NUMBER_KIND; \
109
+ mxResult->value.number = C_NAN; \
110
+ return; \
111
+ }
112
+
113
+ void fx_Math_abs(txMachine* the)
114
+ {
115
+ mxNanResultIfNoArg;
116
+ fxToNumber(the, mxArgv(0));
117
+ mxResult->kind = XS_NUMBER_KIND;
118
+ mxResult->value.number = c_fabs(mxArgv(0)->value.number);
119
+ }
120
+
121
+ void fx_Math_acos(txMachine* the)
122
+ {
123
+ mxNanResultIfNoArg;
124
+ fxToNumber(the, mxArgv(0));
125
+ mxResult->kind = XS_NUMBER_KIND;
126
+ mxResult->value.number = c_acos(mxArgv(0)->value.number);
127
+ }
128
+
129
+ void fx_Math_acosh(txMachine* the)
130
+ {
131
+ mxNanResultIfNoArg;
132
+ fxToNumber(the, mxArgv(0));
133
+ mxResult->kind = XS_NUMBER_KIND;
134
+ mxResult->value.number = c_acosh(mxArgv(0)->value.number);
135
+ }
136
+
137
+ void fx_Math_asin(txMachine* the)
138
+ {
139
+ mxNanResultIfNoArg;
140
+ fxToNumber(the, mxArgv(0));
141
+ mxResult->kind = XS_NUMBER_KIND;
142
+ mxResult->value.number = c_asin(mxArgv(0)->value.number);
143
+ }
144
+
145
+ void fx_Math_asinh(txMachine* the)
146
+ {
147
+ mxNanResultIfNoArg;
148
+ fxToNumber(the, mxArgv(0));
149
+ mxResult->kind = XS_NUMBER_KIND;
150
+ mxResult->value.number = c_asinh(mxArgv(0)->value.number);
151
+ }
152
+
153
+ void fx_Math_atan(txMachine* the)
154
+ {
155
+ mxNanResultIfNoArg;
156
+ fxToNumber(the, mxArgv(0));
157
+ mxResult->kind = XS_NUMBER_KIND;
158
+ mxResult->value.number = c_atan(mxArgv(0)->value.number);
159
+ }
160
+
161
+ void fx_Math_atanh(txMachine* the)
162
+ {
163
+ mxNanResultIfNoArg;
164
+ fxToNumber(the, mxArgv(0));
165
+ mxResult->kind = XS_NUMBER_KIND;
166
+ mxResult->value.number = c_atanh(mxArgv(0)->value.number);
167
+ }
168
+
169
+ void fx_Math_atan2(txMachine* the)
170
+ {
171
+ mxNanResultIfNoArg2;
172
+ fxToNumber(the, mxArgv(0));
173
+ fxToNumber(the, mxArgv(1));
174
+ mxResult->kind = XS_NUMBER_KIND;
175
+ mxResult->value.number = c_atan2(mxArgv(0)->value.number, mxArgv(1)->value.number);
176
+ }
177
+
178
+ void fx_Math_cbrt(txMachine* the)
179
+ {
180
+ mxNanResultIfNoArg;
181
+ fxToNumber(the, mxArgv(0));
182
+ mxResult->kind = XS_NUMBER_KIND;
183
+ mxResult->value.number = c_cbrt(mxArgv(0)->value.number);
184
+ }
185
+
186
+ void fx_Math_ceil(txMachine* the)
187
+ {
188
+ mxNanResultIfNoArg;
189
+ if (XS_INTEGER_KIND == mxArgv(0)->kind) {
190
+ mxResult->kind = XS_INTEGER_KIND;
191
+ mxResult->value.integer = mxArgv(0)->value.integer;
192
+ return;
193
+ }
194
+ fxToNumber(the, mxArgv(0));
195
+ mxResult->kind = XS_NUMBER_KIND;
196
+ mxResult->value.number = c_ceil(mxArgv(0)->value.number);
197
+ fx_Math_toInteger(the);
198
+ }
199
+
200
+ void fx_Math_clz32(txMachine* the)
201
+ {
202
+ txUnsigned x = (mxArgc > 0) ? fxToUnsigned(the, mxArgv(0)) : 0;
203
+ txInteger r;
204
+ if (x)
205
+ #if mxWindows
206
+ _BitScanForward(&r, x);
207
+ #else
208
+ r = __builtin_clz(x);
209
+ #endif
210
+ else
211
+ r = 32;
212
+ mxResult->kind = XS_INTEGER_KIND;
213
+ mxResult->value.integer = r;
214
+ }
215
+
216
+ void fx_Math_cos(txMachine* the)
217
+ {
218
+ mxNanResultIfNoArg;
219
+ fxToNumber(the, mxArgv(0));
220
+ mxResult->kind = XS_NUMBER_KIND;
221
+ mxResult->value.number = c_cos(mxArgv(0)->value.number);
222
+ }
223
+
224
+ void fx_Math_cosh(txMachine* the)
225
+ {
226
+ mxNanResultIfNoArg;
227
+ fxToNumber(the, mxArgv(0));
228
+ mxResult->kind = XS_NUMBER_KIND;
229
+ mxResult->value.number = c_cosh(mxArgv(0)->value.number);
230
+ }
231
+
232
+ void fx_Math_exp(txMachine* the)
233
+ {
234
+ mxNanResultIfNoArg;
235
+ fxToNumber(the, mxArgv(0));
236
+ mxResult->kind = XS_NUMBER_KIND;
237
+ mxResult->value.number = c_exp(mxArgv(0)->value.number);
238
+ }
239
+
240
+ void fx_Math_expm1(txMachine* the)
241
+ {
242
+ mxNanResultIfNoArg;
243
+ fxToNumber(the, mxArgv(0));
244
+ mxResult->kind = XS_NUMBER_KIND;
245
+ mxResult->value.number = c_expm1(mxArgv(0)->value.number);
246
+ }
247
+
248
+ void fx_Math_floor(txMachine* the)
249
+ {
250
+ mxNanResultIfNoArg;
251
+ if (XS_INTEGER_KIND == mxArgv(0)->kind) {
252
+ mxResult->kind = XS_INTEGER_KIND;
253
+ mxResult->value.integer = mxArgv(0)->value.integer;
254
+ return;
255
+ }
256
+ fxToNumber(the, mxArgv(0));
257
+ mxResult->kind = XS_NUMBER_KIND;
258
+ mxResult->value.number = c_floor(mxArgv(0)->value.number);
259
+ fx_Math_toInteger(the);
260
+ }
261
+
262
+ void fx_Math_fround(txMachine* the)
263
+ {
264
+ float arg;
265
+ mxNanResultIfNoArg;
266
+ if (XS_INTEGER_KIND == mxArgv(0)->kind) {
267
+ mxResult->kind = XS_INTEGER_KIND;
268
+ mxResult->value.integer = mxArgv(0)->value.integer;
269
+ return;
270
+ }
271
+ arg = (float)fxToNumber(the, mxArgv(0));
272
+ mxResult->kind = XS_NUMBER_KIND;
273
+ mxResult->value.number = arg;
274
+ }
275
+
276
+ void fx_Math_hypot(txMachine* the)
277
+ {
278
+ if (mxArgc == 2) {
279
+ fxToNumber(the, mxArgv(0));
280
+ fxToNumber(the, mxArgv(1));
281
+ mxResult->kind = XS_NUMBER_KIND;
282
+ mxResult->value.number = c_hypot(mxArgv(0)->value.number, mxArgv(1)->value.number);
283
+ }
284
+ else {
285
+ txInteger c = mxArgc, i;
286
+ txNumber result = 0;
287
+ for (i = 0; i < c; i++) {
288
+ txNumber argument = fxToNumber(the, mxArgv(i));
289
+ result += argument * argument;
290
+ }
291
+ mxResult->kind = XS_NUMBER_KIND;
292
+ mxResult->value.number = c_sqrt(result);
293
+ }
294
+ }
295
+
296
+ void fx_Math_idiv(txMachine* the)
297
+ {
298
+ txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
299
+ txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
300
+ if (y == 0) {
301
+ mxResult->kind = XS_NUMBER_KIND;
302
+ mxResult->value.number = C_NAN;
303
+ }
304
+ else {
305
+ mxResult->kind = XS_INTEGER_KIND;
306
+ #if mxIntegerDivideOverflowException
307
+ if ((x == (txInteger)0x80000000) && (y == -1))
308
+ mxResult->value.integer = x;
309
+ else
310
+ #endif
311
+ mxResult->value.integer = x / y;
312
+ }
313
+ }
314
+
315
+ void fx_Math_idivmod(txMachine* the)
316
+ {
317
+ txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
318
+ txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
319
+ if (y == 0) {
320
+ mxPushNumber(C_NAN);
321
+ mxPushNumber(C_NAN);
322
+ }
323
+ else {
324
+ #if mxIntegerDivideOverflowException
325
+ if ((x == (txInteger)0x80000000) && (y == -1)) {
326
+ mxPushInteger(x);
327
+ mxPushInteger(0);
328
+ }
329
+ else
330
+ #endif
331
+ {
332
+ mxPushInteger(x / y);
333
+ mxPushInteger((x % y + y) % y);
334
+ }
335
+ }
336
+ fxConstructArrayEntry(the, mxResult);
337
+ }
338
+
339
+ void fx_Math_imod(txMachine* the)
340
+ {
341
+ txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
342
+ txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
343
+ if (y == 0) {
344
+ mxResult->kind = XS_NUMBER_KIND;
345
+ mxResult->value.number = C_NAN;
346
+ }
347
+ else {
348
+ mxResult->kind = XS_INTEGER_KIND;
349
+ #if mxIntegerDivideOverflowException
350
+ if ((x == (txInteger)0x80000000) && (y == -1))
351
+ mxResult->value.integer = 0;
352
+ else
353
+ #endif
354
+ mxResult->value.integer = (x % y + y) % y;
355
+ }
356
+ }
357
+
358
+ void fx_Math_imul(txMachine* the)
359
+ {
360
+ txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
361
+ txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
362
+ mxResult->kind = XS_INTEGER_KIND;
363
+ mxResult->value.integer = x * y;
364
+ }
365
+
366
+ void fx_Math_imuldiv(txMachine* the)
367
+ {
368
+ txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
369
+ txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
370
+ txS8 z = (mxArgc > 2) ? fxToInteger(the, mxArgv(2)) : 0;
371
+ if (z == 0) {
372
+ mxResult->kind = XS_NUMBER_KIND;
373
+ mxResult->value.number = C_NAN;
374
+ }
375
+ else {
376
+ txS8 r = (x * y) / z;
377
+ if ((-2147483648LL <= r) && (r <= 2147483647LL)) {
378
+ mxResult->kind = XS_INTEGER_KIND;
379
+ mxResult->value.integer = (txInteger)r;
380
+ }
381
+ else {
382
+ mxResult->kind = XS_NUMBER_KIND;
383
+ mxResult->value.number = (txNumber)r;
384
+ }
385
+ }
386
+ }
387
+
388
+ void fx_Math_irem(txMachine* the)
389
+ {
390
+ txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
391
+ txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
392
+ if (y == 0) {
393
+ mxResult->kind = XS_NUMBER_KIND;
394
+ mxResult->value.number = C_NAN;
395
+ }
396
+ else {
397
+ mxResult->kind = XS_INTEGER_KIND;
398
+ #if mxIntegerDivideOverflowException
399
+ if ((x == (txInteger)0x80000000) && (y == -1))
400
+ mxResult->value.integer = 0;
401
+ else
402
+ #endif
403
+ mxResult->value.integer = x % y;
404
+ }
405
+ }
406
+
407
+ void fx_Math_log(txMachine* the)
408
+ {
409
+ mxNanResultIfNoArg;
410
+ fxToNumber(the, mxArgv(0));
411
+ mxResult->kind = XS_NUMBER_KIND;
412
+ mxResult->value.number = c_log(mxArgv(0)->value.number);
413
+ }
414
+
415
+ void fx_Math_log1p(txMachine* the)
416
+ {
417
+ mxNanResultIfNoArg;
418
+ fxToNumber(the, mxArgv(0));
419
+ mxResult->kind = XS_NUMBER_KIND;
420
+ mxResult->value.number = c_log1p(mxArgv(0)->value.number);
421
+ }
422
+
423
+ void fx_Math_log10(txMachine* the)
424
+ {
425
+ mxNanResultIfNoArg;
426
+ fxToNumber(the, mxArgv(0));
427
+ mxResult->kind = XS_NUMBER_KIND;
428
+ mxResult->value.number = c_log10(mxArgv(0)->value.number);
429
+ }
430
+
431
+ void fx_Math_log2(txMachine* the)
432
+ {
433
+ mxNanResultIfNoArg;
434
+ fxToNumber(the, mxArgv(0));
435
+ mxResult->kind = XS_NUMBER_KIND;
436
+ #if mxAndroid
437
+ mxResult->value.number = c_log(mxArgv(0)->value.number) / c_log(2);
438
+ #else
439
+ mxResult->value.number = c_log2(mxArgv(0)->value.number);
440
+ #endif
441
+ }
442
+
443
+ void fx_Math_max(txMachine* the)
444
+ {
445
+ txInteger c = mxArgc, i;
446
+ mxResult->kind = XS_NUMBER_KIND;
447
+ mxResult->value.number = -((txNumber)C_INFINITY);
448
+ for (i = 0; i < c; i++) {
449
+ txNumber n = fxToNumber(the, mxArgv(i));
450
+ if (c_isnan(n)) {
451
+ for (; i < c; i++)
452
+ fxToNumber(the, mxArgv(i));
453
+ mxResult->value.number = C_NAN;
454
+ return;
455
+ }
456
+ if (mxResult->value.number < n)
457
+ mxResult->value.number = n;
458
+ else if ((mxResult->value.number == 0) && (n == 0)) {
459
+ if (c_signbit(mxResult->value.number) != c_signbit(n))
460
+ mxResult->value.number = 0;
461
+ }
462
+ }
463
+ }
464
+
465
+ void fx_Math_min(txMachine* the)
466
+ {
467
+ txInteger c = mxArgc, i;
468
+ mxResult->kind = XS_NUMBER_KIND;
469
+ mxResult->value.number = (txNumber)C_INFINITY;
470
+ for (i = 0; i < c; i++) {
471
+ txNumber n = fxToNumber(the, mxArgv(i));
472
+ if (c_isnan(n)) {
473
+ for (; i < c; i++)
474
+ fxToNumber(the, mxArgv(i));
475
+ mxResult->value.number = C_NAN;
476
+ return;
477
+ }
478
+ if (mxResult->value.number > n)
479
+ mxResult->value.number = n;
480
+ else if ((mxResult->value.number == 0) && (n == 0)) {
481
+ if (c_signbit(mxResult->value.number) != c_signbit(n))
482
+ mxResult->value.number = -0.0;
483
+ }
484
+ }
485
+ }
486
+
487
+ void fx_Math_mod(txMachine* the)
488
+ {
489
+ txNumber x = (mxArgc > 0) ? fxToNumber(the, mxArgv(0)) : 0;
490
+ txNumber y = (mxArgc > 1) ? fxToNumber(the, mxArgv(1)) : 0;
491
+ mxResult->kind = XS_NUMBER_KIND;
492
+ mxResult->value.number = c_fmod((c_fmod(x, y) + y), y);
493
+ }
494
+
495
+ txNumber fx_pow(txNumber x, txNumber y)
496
+ {
497
+ if (!c_isfinite(y) && (c_fabs(x) == 1.0))
498
+ return C_NAN;
499
+ return c_pow(x, y);
500
+ }
501
+
502
+ void fx_Math_pow(txMachine* the)
503
+ {
504
+ txNumber x, y;
505
+ mxNanResultIfNoArg2;
506
+ x = fxToNumber(the, mxArgv(0));
507
+ y = fxToNumber(the, mxArgv(1));
508
+ mxResult->kind = XS_NUMBER_KIND;
509
+ mxResult->value.number = fx_pow(x, y);
510
+ }
511
+
512
+ void fx_Math_random(txMachine* the)
513
+ {
514
+ uint32_t result = c_rand();
515
+ while (result == C_RAND_MAX)
516
+ result = c_rand();
517
+ mxResult->kind = XS_NUMBER_KIND;
518
+ mxResult->value.number = (double)result / (double)C_RAND_MAX;
519
+ }
520
+
521
+ void fx_Math_random_secure(txMachine* the)
522
+ {
523
+ mxTypeError("secure mode");
524
+ }
525
+
526
+ void fx_Math_round(txMachine* the)
527
+ {
528
+ txNumber arg;
529
+ mxNanResultIfNoArg;
530
+ if (XS_INTEGER_KIND == mxArgv(0)->kind) {
531
+ mxResult->kind = XS_INTEGER_KIND;
532
+ mxResult->value.integer = mxArgv(0)->value.integer;
533
+ return;
534
+ }
535
+ arg = fxToNumber(the, mxArgv(0));
536
+ if (c_isnormal(arg) && (-4503599627370495 < arg) && (arg < 4503599627370495)) { // 2 ** 52 - 1
537
+ if ((arg < -0.5) || (0.5 <= arg))
538
+ arg = c_floor(arg + 0.5);
539
+ else if (arg < 0)
540
+ arg = -0.0;
541
+ else if (arg > 0)
542
+ arg = 0.0;
543
+ }
544
+ mxResult->kind = XS_NUMBER_KIND;
545
+ mxResult->value.number = arg;
546
+ fx_Math_toInteger(the);
547
+ }
548
+
549
+ void fx_Math_sqrt(txMachine* the)
550
+ {
551
+ mxNanResultIfNoArg;
552
+ fxToNumber(the, mxArgv(0));
553
+ mxResult->kind = XS_NUMBER_KIND;
554
+ mxResult->value.number = c_sqrt(mxArgv(0)->value.number);
555
+ }
556
+
557
+ void fx_Math_sign(txMachine* the)
558
+ {
559
+ txNumber arg;
560
+ mxNanResultIfNoArg;
561
+ arg = fxToNumber(the, mxArgv(0));
562
+ mxResult->kind = XS_NUMBER_KIND;
563
+ if (c_isnan(arg))
564
+ mxResult->value.number = C_NAN;
565
+ else if (arg < 0)
566
+ mxResult->value.number = -1;
567
+ else if (arg > 0)
568
+ mxResult->value.number = 1;
569
+ else
570
+ mxResult->value.number = arg;
571
+ fx_Math_toInteger(the);
572
+ }
573
+
574
+ void fx_Math_sin(txMachine* the)
575
+ {
576
+ mxNanResultIfNoArg;
577
+ fxToNumber(the, mxArgv(0));
578
+ mxResult->kind = XS_NUMBER_KIND;
579
+ mxResult->value.number = c_sin(mxArgv(0)->value.number);
580
+ }
581
+
582
+ void fx_Math_sinh(txMachine* the)
583
+ {
584
+ mxNanResultIfNoArg;
585
+ fxToNumber(the, mxArgv(0));
586
+ mxResult->kind = XS_NUMBER_KIND;
587
+ mxResult->value.number = c_sinh(mxArgv(0)->value.number);
588
+ }
589
+
590
+ void fx_Math_tan(txMachine* the)
591
+ {
592
+ mxNanResultIfNoArg;
593
+ fxToNumber(the, mxArgv(0));
594
+ mxResult->kind = XS_NUMBER_KIND;
595
+ mxResult->value.number = c_tan(mxArgv(0)->value.number);
596
+ }
597
+
598
+ void fx_Math_tanh(txMachine* the)
599
+ {
600
+ mxNanResultIfNoArg;
601
+ fxToNumber(the, mxArgv(0));
602
+ mxResult->kind = XS_NUMBER_KIND;
603
+ mxResult->value.number = c_tanh(mxArgv(0)->value.number);
604
+ }
605
+
606
+ void fx_Math_trunc(txMachine* the)
607
+ {
608
+ mxNanResultIfNoArg;
609
+ fxToNumber(the, mxArgv(0));
610
+ mxResult->kind = XS_NUMBER_KIND;
611
+ mxResult->value.number = c_trunc(mxArgv(0)->value.number);
612
+ fx_Math_toInteger(the);
613
+ }
614
+
615
+ void fx_Math_toInteger(txMachine* the)
616
+ {
617
+ txNumber number = mxResult->value.number;
618
+ txInteger integer = (txInteger)number;
619
+ txNumber check = integer;
620
+ if ((number == check) && (number || !c_signbit(number))) {
621
+ mxResult->value.integer = integer;
622
+ mxResult->kind = XS_INTEGER_KIND;
623
+ }
624
+ }