@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,1168 @@
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
+ #if mxRegExp
40
+ static txNumber fxAdvanceStringIndex(txString string, txNumber index, txBoolean unicodeFlag);
41
+ static txSlot* fxCheckRegExpInstance(txMachine* the, txSlot* slot);
42
+ static void fxExecuteRegExp(txMachine* the, txSlot* regexp, txSlot* argument);
43
+ #endif
44
+
45
+ static void fx_RegExp_prototype_get_flag(txMachine* the, txU4 flag);
46
+ static void fx_RegExp_prototype_split_aux(txMachine* the, txSlot* string, txIndex start, txIndex stop, txSlot* item);
47
+
48
+ void fxBuildRegExp(txMachine* the)
49
+ {
50
+ txSlot* slot;
51
+ mxPush(mxObjectPrototype);
52
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
53
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_compile), 0, mxID(_compile), XS_DONT_ENUM_FLAG);
54
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_exec), 1, mxID(_exec), XS_DONT_ENUM_FLAG);
55
+ mxExecuteRegExpFunction.kind = slot->kind;
56
+ mxExecuteRegExpFunction.value = slot->value;
57
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_match), 1, mxID(_Symbol_match), XS_DONT_ENUM_FLAG);
58
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_matchAll), 1, mxID(_Symbol_matchAll), XS_DONT_ENUM_FLAG);
59
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_replace), 2, mxID(_Symbol_replace), XS_DONT_ENUM_FLAG);
60
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_search), 1, mxID(_Symbol_search), XS_DONT_ENUM_FLAG);
61
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_split), 2, mxID(_Symbol_split), XS_DONT_ENUM_FLAG);
62
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_test), 1, mxID(_test), XS_DONT_ENUM_FLAG);
63
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
64
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_dotAll), C_NULL, mxID(_dotAll), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
65
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_flags), C_NULL, mxID(_flags), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
66
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_global), C_NULL, mxID(_global), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
67
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_hasIndices), C_NULL, mxID(_hasIndices), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
68
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_ignoreCase), C_NULL, mxID(_ignoreCase), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
69
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_multiline), C_NULL, mxID(_multiline), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
70
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_source), C_NULL, mxID(_source), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
71
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_sticky), C_NULL, mxID(_sticky), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
72
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_unicode), C_NULL, mxID(_unicode), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
73
+ mxRegExpPrototype = *the->stack;
74
+ slot = fxBuildHostConstructor(the, mxCallback(fx_RegExp), 2, mxID(_RegExp));
75
+ mxRegExpConstructor = *the->stack;
76
+ slot = fxLastProperty(the, slot);
77
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG);
78
+ mxPop();
79
+ fxNewHostFunction(the, mxCallback(fxInitializeRegExp), 2, XS_NO_ID, XS_NO_ID);
80
+ mxInitializeRegExpFunction = *the->stack;
81
+
82
+ mxPush(mxIteratorPrototype);
83
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
84
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_matchAll_next), 0, mxID(_next), XS_DONT_ENUM_FLAG);
85
+ slot = fxNextStringXProperty(the, slot, "RegExp String Iterator", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
86
+ mxPull(mxRegExpStringIteratorPrototype);
87
+
88
+ mxPop();
89
+ }
90
+
91
+ #if mxRegExp
92
+ txNumber fxAdvanceStringIndex(txString string, txNumber index, txBoolean unicodeFlag)
93
+ {
94
+ #if mxCESU8
95
+ if (unicodeFlag) {
96
+ txInteger character;
97
+ txSize offset = fxUnicodeLength(string);
98
+ if (index >= offset)
99
+ return index + 1;
100
+ string += fxUnicodeToUTF8Offset(string, (txInteger)index);
101
+ offset = mxPtrDiff(mxStringByteDecode(string, &character) - string);
102
+ if (character == C_EOF)
103
+ return index + 1;
104
+ return index + fxUTF8ToUnicodeOffset(string, offset);
105
+ }
106
+ #endif
107
+ return index + 1;
108
+ }
109
+
110
+ txSlot* fxCheckRegExpInstance(txMachine* the, txSlot* slot)
111
+ {
112
+ if (slot->kind == XS_REFERENCE_KIND) {
113
+ slot = slot->value.reference;
114
+ if ((slot->next) && (slot->next->flag & XS_INTERNAL_FLAG) && (slot->next->kind == XS_REGEXP_KIND))
115
+ return slot;
116
+ }
117
+ mxTypeError("this is no RegExp instance");
118
+ return C_NULL;
119
+ }
120
+
121
+ void fxExecuteRegExp(txMachine* the, txSlot* regexp, txSlot* argument)
122
+ {
123
+ mxPushSlot(regexp);
124
+ mxDub();
125
+ mxGetID(mxID(_exec));
126
+ if ((the->stack->kind != XS_REFERENCE_KIND) || (!mxIsFunction(the->stack->value.reference))) {
127
+ the->stack->kind = mxExecuteRegExpFunction.kind;
128
+ the->stack->value = mxExecuteRegExpFunction.value;
129
+ }
130
+ mxCall();
131
+ mxPushSlot(argument);
132
+ mxRunCount(1);
133
+ if ((the->stack->kind != XS_NULL_KIND) && (the->stack->kind != XS_REFERENCE_KIND))
134
+ mxTypeError("invalid exec result");
135
+ }
136
+ #endif
137
+
138
+ void fxInitializeRegExp(txMachine* the)
139
+ {
140
+ #if mxRegExp
141
+ txSlot* instance = fxToInstance(the, mxThis);
142
+ txSlot* regexp = instance->next;
143
+ txSlot* key = regexp->next;
144
+ txString pattern;
145
+ txString modifier;
146
+
147
+ if (mxArgv(0)->kind == XS_UNDEFINED_KIND)
148
+ *mxArgv(0) = mxEmptyString;
149
+ else
150
+ fxToString(the, mxArgv(0));
151
+ if (mxArgv(1)->kind == XS_UNDEFINED_KIND)
152
+ *mxArgv(1) = mxEmptyString;
153
+ else
154
+ fxToString(the, mxArgv(1));
155
+ key->kind = (mxArgv(0)->kind == XS_STRING_X_KIND) ? XS_KEY_X_KIND : XS_KEY_KIND;
156
+ pattern = key->value.key.string = mxArgv(0)->value.string;
157
+ modifier = mxArgv(1)->value.string;
158
+ if (!fxCompileRegExp(the, pattern, modifier, &regexp->value.regexp.code, &regexp->value.regexp.data, the->nameBuffer, sizeof(the->nameBuffer)))
159
+ mxSyntaxError("invalid regular expression: %s", the->nameBuffer);
160
+ *mxResult = *mxThis;
161
+ #endif
162
+ }
163
+
164
+ txBoolean fxIsRegExp(txMachine* the, txSlot* slot)
165
+ {
166
+ #if mxRegExp
167
+ if (mxIsReference(slot)) {
168
+ txSlot* instance = slot->value.reference;
169
+ mxPushSlot(slot);
170
+ mxGetID(mxID(_Symbol_match));
171
+ if (the->stack->kind != XS_UNDEFINED_KIND)
172
+ return fxToBoolean(the, the->stack++);
173
+ mxPop();
174
+ if ((instance->next) && (instance->next->flag & XS_INTERNAL_FLAG) && (instance->next->kind == XS_REGEXP_KIND))
175
+ return 1;
176
+ }
177
+ #endif
178
+ return 0;
179
+ }
180
+
181
+ txSlot* fxNewRegExpInstance(txMachine* the)
182
+ {
183
+ #if mxRegExp
184
+ txSlot* instance;
185
+ txSlot* property;
186
+ instance = fxNewObjectInstance(the);
187
+
188
+ property = instance->next = fxNewSlot(the);
189
+ property->flag = XS_INTERNAL_FLAG;
190
+ property->kind = XS_REGEXP_KIND;
191
+ property->value.regexp.code = C_NULL;
192
+ property->value.regexp.data = C_NULL;
193
+
194
+ property = property->next = fxNewSlot(the);
195
+ property->flag = XS_INTERNAL_FLAG;
196
+ property->kind = (mxEmptyString.kind == XS_STRING_X_KIND) ? XS_KEY_X_KIND : XS_KEY_KIND;
197
+ property->value.key.string = mxEmptyString.value.string;
198
+ property->value.key.sum = 0;
199
+
200
+ property = fxNextIntegerProperty(the, property, 0, mxID(_lastIndex), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
201
+
202
+ return instance;
203
+ #else
204
+ return NULL;
205
+ #endif
206
+ }
207
+
208
+ void fx_RegExp(txMachine* the)
209
+ {
210
+ #if mxRegExp
211
+ txSlot* pattern = ((mxArgc > 0) && (mxArgv(0)->kind != XS_UNDEFINED_KIND)) ? mxArgv(0) : C_NULL;
212
+ txSlot* flags = ((mxArgc > 1) && (mxArgv(1)->kind != XS_UNDEFINED_KIND)) ? mxArgv(1) : C_NULL;
213
+ txBoolean patternIsRegExp = (pattern && fxIsRegExp(the, pattern)) ? 1 : 0;
214
+
215
+ if (mxTarget->kind == XS_UNDEFINED_KIND) {
216
+ if (patternIsRegExp && !flags) {
217
+ mxPushSlot(pattern);
218
+ mxGetID(mxID(_constructor));
219
+ if ((the->stack->kind == mxFunction->kind) && (the->stack->value.reference == mxFunction->value.reference)) {
220
+ mxPop();
221
+ *mxResult = *pattern;
222
+ return;
223
+ }
224
+ mxPop();
225
+ }
226
+ mxPushSlot(mxFunction);
227
+ }
228
+ else {
229
+ mxPushSlot(mxTarget);
230
+ }
231
+ fxGetPrototypeFromConstructor(the, &mxRegExpPrototype);
232
+ fxNewRegExpInstance(the);
233
+ mxPush(mxInitializeRegExpFunction);
234
+ mxCall();
235
+ if (patternIsRegExp) {
236
+ mxPushSlot(pattern);
237
+ mxGetID(mxID(_source));
238
+ if (flags)
239
+ mxPushSlot(flags);
240
+ else {
241
+ mxPushSlot(pattern);
242
+ mxGetID(mxID(_flags));
243
+ }
244
+ }
245
+ else {
246
+ if (pattern)
247
+ mxPushSlot(pattern);
248
+ else
249
+ mxPushUndefined();
250
+ if (flags)
251
+ mxPushSlot(flags);
252
+ else
253
+ mxPushUndefined();
254
+ }
255
+ mxRunCount(2);
256
+ mxPullSlot(mxResult);
257
+ #else
258
+ mxUnknownError("not built-in");
259
+ #endif
260
+ }
261
+
262
+ void fx_RegExp_prototype_get_flag(txMachine* the, txU4 flag)
263
+ {
264
+ #if mxRegExp
265
+ txSlot* slot = mxThis;
266
+ if (slot->kind == XS_REFERENCE_KIND) {
267
+ slot = slot->value.reference;
268
+ if (slot == mxRegExpPrototype.value.reference)
269
+ return;
270
+ slot = slot->next;
271
+ if ((slot) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_REGEXP_KIND)) {
272
+ txInteger flags = slot->value.regexp.code[0];
273
+ mxResult->value.boolean = (flags & flag) ? 1 : 0;
274
+ mxResult->kind = XS_BOOLEAN_KIND;
275
+ return;
276
+ }
277
+ }
278
+ mxTypeError("this is no object");
279
+ #endif
280
+ }
281
+
282
+ void fx_RegExp_prototype_get_flags(txMachine* the)
283
+ {
284
+ #if mxRegExp
285
+ char flags[8];
286
+ txIndex index = 0;
287
+ if (mxThis->kind != XS_REFERENCE_KIND)
288
+ mxTypeError("this is no object");
289
+ mxPushSlot(mxThis);
290
+ mxGetID(mxID(_hasIndices));
291
+ if (fxToBoolean(the, the->stack++))
292
+ flags[index++] = 'd';
293
+ mxPushSlot(mxThis);
294
+ mxGetID(mxID(_global));
295
+ if (fxToBoolean(the, the->stack++))
296
+ flags[index++] = 'g';
297
+ mxPushSlot(mxThis);
298
+ mxGetID(mxID(_ignoreCase));
299
+ if (fxToBoolean(the, the->stack++))
300
+ flags[index++] = 'i';
301
+ mxPushSlot(mxThis);
302
+ mxGetID(mxID(_multiline));
303
+ if (fxToBoolean(the, the->stack++))
304
+ flags[index++] = 'm';
305
+ mxPushSlot(mxThis);
306
+ mxGetID(mxID(_dotAll));
307
+ if (fxToBoolean(the, the->stack++))
308
+ flags[index++] = 's';
309
+ mxPushSlot(mxThis);
310
+ mxGetID(mxID(_unicode));
311
+ if (fxToBoolean(the, the->stack++))
312
+ flags[index++] = 'u';
313
+ mxPushSlot(mxThis);
314
+ mxGetID(mxID(_sticky));
315
+ if (fxToBoolean(the, the->stack++))
316
+ flags[index++] = 'y';
317
+ flags[index] = 0;
318
+ fxCopyStringC(the, mxResult, flags);
319
+ #endif
320
+ }
321
+
322
+ void fx_RegExp_prototype_get_dotAll(txMachine* the)
323
+ {
324
+ #if mxRegExp
325
+ fx_RegExp_prototype_get_flag(the, XS_REGEXP_S);
326
+ #endif
327
+ }
328
+
329
+ void fx_RegExp_prototype_get_global(txMachine* the)
330
+ {
331
+ #if mxRegExp
332
+ fx_RegExp_prototype_get_flag(the, XS_REGEXP_G);
333
+ #endif
334
+ }
335
+
336
+ void fx_RegExp_prototype_get_hasIndices(txMachine* the)
337
+ {
338
+ #if mxRegExp
339
+ fx_RegExp_prototype_get_flag(the, XS_REGEXP_D);
340
+ #endif
341
+ }
342
+
343
+ void fx_RegExp_prototype_get_ignoreCase(txMachine* the)
344
+ {
345
+ #if mxRegExp
346
+ fx_RegExp_prototype_get_flag(the, XS_REGEXP_I);
347
+ #endif
348
+ }
349
+
350
+ void fx_RegExp_prototype_get_multiline(txMachine* the)
351
+ {
352
+ #if mxRegExp
353
+ fx_RegExp_prototype_get_flag(the, XS_REGEXP_M);
354
+ #endif
355
+ }
356
+
357
+ void fx_RegExp_prototype_get_source(txMachine* the)
358
+ {
359
+ #if mxRegExp
360
+ txSlot* slot = mxThis;
361
+ if (slot->kind == XS_REFERENCE_KIND) {
362
+ slot = slot->value.reference;
363
+ if (slot == mxRegExpPrototype.value.reference) {
364
+ *mxResult = mxEmptyRegExp;
365
+ return;
366
+ }
367
+ slot = slot->next;
368
+ if ((slot) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_REGEXP_KIND)) {
369
+ txString pattern;
370
+ txInteger escape = 0;
371
+ txInteger count = 0;
372
+ txU1 c, *s, *r;
373
+ slot = slot->next;
374
+ pattern = slot->value.key.string;
375
+ if (*pattern == 0) {
376
+ *mxResult = mxEmptyRegExp;
377
+ return;
378
+ }
379
+ s = (txU1*)pattern;
380
+ while ((c = *s++)) {
381
+ if ((c == 10) || (c == 13) || (c == '/'))
382
+ escape++;
383
+ else if ((c == 0xE2) && (s[0] == 0x80) && ((s[1] == 0xA8) || (s[1] == 0xA9))) /* LS || PS */
384
+ escape += 3;
385
+ count++;
386
+ }
387
+ if (escape) {
388
+ mxResult->value.string = fxNewChunk(the, count + escape + 1);
389
+ mxResult->kind = XS_STRING_KIND;
390
+ s = (txU1*)slot->value.key.string;
391
+ r = (txU1*)mxResult->value.string;
392
+ while ((c = *s++)) {
393
+ if (c == 10) {
394
+ *r++ = '\\'; *r++ = 'n';
395
+ }
396
+ else if (c == 13) {
397
+ *r++ = '\\'; *r++ = 'r';
398
+ }
399
+ else if (c == '/') {
400
+ *r++ = '\\'; *r++ = '/';
401
+ }
402
+ else if ((c == 0xE2) && (s[0] == 0x80) && (s[1] == 0xA8)) {
403
+ *r++ = '\\'; *r++ = 'u'; *r++ = '2'; *r++ = '0'; *r++ = '2'; *r++ = '8';
404
+ s += 2;
405
+ }
406
+ else if ((c == 0xE2) && (s[0] == 0x80) && (s[1] == 0xA9)) {
407
+ *r++ = '\\'; *r++ = 'u'; *r++ = '2'; *r++ = '0'; *r++ = '2'; *r++ = '9';
408
+ s += 2;
409
+ }
410
+ else {
411
+ *r++ = c;
412
+ }
413
+ }
414
+ *r = 0;
415
+ }
416
+ else {
417
+ mxResult->value.string = slot->value.string;
418
+ mxResult->kind = (slot->kind == XS_KEY_X_KIND) ? XS_STRING_X_KIND : XS_STRING_KIND;
419
+ }
420
+ return;
421
+ }
422
+ }
423
+ mxTypeError("this is no RegExp instance");
424
+ #endif
425
+ }
426
+
427
+ void fx_RegExp_prototype_get_sticky(txMachine* the)
428
+ {
429
+ #if mxRegExp
430
+ fx_RegExp_prototype_get_flag(the, XS_REGEXP_Y);
431
+ #endif
432
+ }
433
+
434
+ void fx_RegExp_prototype_get_unicode(txMachine* the)
435
+ {
436
+ #if mxRegExp
437
+ fx_RegExp_prototype_get_flag(the, XS_REGEXP_U);
438
+ #endif
439
+ }
440
+
441
+ void fx_RegExp_prototype_compile(txMachine* the)
442
+ {
443
+ #if mxRegExp
444
+ *mxResult = *mxThis;
445
+ #endif
446
+ }
447
+
448
+ void fx_RegExp_prototype_exec(txMachine* the)
449
+ {
450
+ #if mxRegExp
451
+ txSlot* instance = fxCheckRegExpInstance(the, mxThis);
452
+ txSlot* regexp = instance->next;
453
+ txSlot* argument;
454
+ txSlot* temporary;
455
+ txNumber lastIndex;
456
+ txInteger flags;
457
+ txBoolean globalFlag;
458
+ txBoolean namedFlag;
459
+ txBoolean stickyFlag;
460
+ txBoolean hasIndicesFlag;
461
+ txInteger offset;
462
+
463
+ if (mxArgc > 0)
464
+ mxPushSlot(mxArgv(0));
465
+ else
466
+ mxPushUndefined();
467
+ fxToString(the, the->stack);
468
+ argument = the->stack;
469
+
470
+ if (regexp->value.regexp.data == C_NULL) {
471
+ mxTemporary(temporary);
472
+ temporary->value.regexp.code = C_NULL;
473
+ temporary->value.regexp.data = fxAllocateRegExpData(the, regexp->value.regexp.code);
474
+ temporary->kind = XS_REGEXP_KIND;
475
+ }
476
+ else
477
+ temporary = regexp;
478
+
479
+ mxPushSlot(mxThis);
480
+ mxGetID(mxID(_lastIndex));
481
+ lastIndex = fxToLength(the, the->stack);
482
+ mxPop();
483
+ if (lastIndex > 0x7FFFFFFF)
484
+ lastIndex = 0x7FFFFFFF;
485
+
486
+ flags = regexp->value.regexp.code[0];
487
+ globalFlag = (flags & XS_REGEXP_G) ? 1 : 0;
488
+ namedFlag = (flags & XS_REGEXP_N) ? 1 : 0;
489
+ stickyFlag = (flags & XS_REGEXP_Y) ? 1 : 0;
490
+ hasIndicesFlag = (flags & XS_REGEXP_D) ? 1 : 0;
491
+ offset = (globalFlag || stickyFlag) ? fxUnicodeToUTF8Offset(argument->value.string, (txInteger)lastIndex) : 0;
492
+ if ((offset >= 0) && fxMatchRegExp(the, regexp->value.regexp.code, temporary->value.regexp.data, argument->value.string, offset)) {
493
+ txSlot* resultArray;
494
+ txSlot* resultItem;
495
+ txSlot* indicesArray;
496
+ txSlot* indicesItem;
497
+ txSlot* resultObject;
498
+ txSlot* resultProperty;
499
+ txSlot* indicesObject;
500
+ txSlot* indicesProperty;
501
+ txInteger count;
502
+ txInteger index;
503
+ txInteger length;
504
+ if (globalFlag || stickyFlag) {
505
+ index = fxUTF8ToUnicodeOffset(argument->value.string, temporary->value.regexp.data[1]);
506
+ mxPushInteger(index);
507
+ mxPushSlot(mxThis);
508
+ mxSetID(mxID(_lastIndex));
509
+ mxPop();
510
+ }
511
+ mxPush(mxArrayPrototype);
512
+ resultArray = fxNewArrayInstance(the);
513
+ resultItem = fxLastProperty(the, resultArray);
514
+ if (hasIndicesFlag) {
515
+ mxPush(mxArrayPrototype);
516
+ indicesArray = fxNewArrayInstance(the);
517
+ indicesItem = fxLastProperty(the, indicesArray);
518
+ }
519
+ if (namedFlag) {
520
+ resultObject = fxNewInstance(the);
521
+ resultProperty = fxLastProperty(the, resultObject);
522
+ if (hasIndicesFlag) {
523
+ indicesObject = fxNewInstance(the);
524
+ indicesProperty = fxLastProperty(the, indicesObject);
525
+ }
526
+ }
527
+ count = regexp->value.regexp.code[1];
528
+ for (index = 0; index < count; index++) {
529
+ txInteger start = temporary->value.regexp.data[2 * index];
530
+ resultItem = resultItem->next = fxNewSlot(the);
531
+ if (hasIndicesFlag)
532
+ indicesItem = indicesItem->next = fxNewSlot(the);
533
+ if (start >= 0) {
534
+ txInteger end = temporary->value.regexp.data[(2 * index) + 1];
535
+ length = end - start;
536
+ resultItem->value.string = (txString)fxNewChunk(the, length + 1);
537
+ c_memcpy(resultItem->value.string, argument->value.string + start, length);
538
+ resultItem->value.string[length] = 0;
539
+ resultItem->kind = XS_STRING_KIND;
540
+ if (hasIndicesFlag) {
541
+ start = fxUTF8ToUnicodeOffset(argument->value.string, start);
542
+ end = start + fxUTF8ToUnicodeOffset(argument->value.string + start, length);
543
+ mxPushInteger(start);
544
+ mxPushInteger(end);
545
+ fxConstructArrayEntry(the, indicesItem);
546
+ }
547
+ }
548
+ if (namedFlag) {
549
+ txInteger tmp = regexp->value.regexp.code[2 + index];
550
+ txID name = (txID)tmp;
551
+ if (name != XS_NO_ID) {
552
+ resultProperty = resultProperty->next = fxNewSlot(the);
553
+ resultProperty->value = resultItem->value;
554
+ resultProperty->kind = resultItem->kind;
555
+ resultProperty->ID = name;
556
+ if (hasIndicesFlag) {
557
+ indicesProperty = indicesProperty->next = fxNewSlot(the);
558
+ indicesProperty->value = indicesItem->value;
559
+ indicesProperty->kind = indicesItem->kind;
560
+ indicesProperty->ID = name;
561
+ }
562
+ }
563
+ }
564
+ resultArray->next->value.array.length++;
565
+ if (hasIndicesFlag)
566
+ indicesArray->next->value.array.length++;
567
+ }
568
+ if (hasIndicesFlag) {
569
+ fxCacheArray(the, indicesArray);
570
+ indicesItem = fxLastProperty(the, indicesArray);
571
+ indicesItem = indicesItem->next = fxNewSlot(the);
572
+ indicesItem->ID = mxID(_groups);
573
+ if (namedFlag) {
574
+ indicesItem->value.reference = indicesObject;
575
+ indicesItem->kind = XS_REFERENCE_KIND;
576
+ mxPop();
577
+ }
578
+ }
579
+ fxCacheArray(the, resultArray);
580
+ resultItem = fxLastProperty(the, resultArray);
581
+ resultItem = resultItem->next = fxNewSlot(the);
582
+ resultItem->ID = mxID(_index);
583
+ resultItem->kind = XS_INTEGER_KIND;
584
+ resultItem->value.integer = fxUTF8ToUnicodeOffset(argument->value.string, temporary->value.regexp.data[0]);
585
+ resultItem = resultItem->next = fxNewSlot(the);
586
+ resultItem->ID = mxID(_input);
587
+ resultItem->value.string = argument->value.string;
588
+ resultItem->kind = argument->kind;
589
+ resultItem = resultItem->next = fxNewSlot(the);
590
+ resultItem->ID = mxID(_groups);
591
+ if (namedFlag) {
592
+ resultItem->value.reference = resultObject;
593
+ resultItem->kind = XS_REFERENCE_KIND;
594
+ mxPop();
595
+ }
596
+ if (hasIndicesFlag) {
597
+ resultItem = resultItem->next = fxNewSlot(the);
598
+ resultItem->ID = mxID(_indices);
599
+ resultItem->value.reference = indicesArray;
600
+ resultItem->kind = XS_REFERENCE_KIND;
601
+ mxPop();
602
+ }
603
+ }
604
+ else {
605
+ if (globalFlag || stickyFlag) {
606
+ mxPushInteger(0);
607
+ mxPushSlot(mxThis);
608
+ mxSetID(mxID(_lastIndex));
609
+ mxPop();
610
+ }
611
+ mxPushNull();
612
+ }
613
+ mxPullSlot(mxResult);
614
+ #endif
615
+ }
616
+
617
+ void fx_RegExp_prototype_match(txMachine* the)
618
+ {
619
+ #if mxRegExp
620
+ txSlot* argument;
621
+ fxToInstance(the, mxThis);
622
+ if (mxArgc == 0)
623
+ mxPushUndefined();
624
+ else
625
+ mxPushSlot(mxArgv(0));
626
+ argument = the->stack;
627
+ fxToString(the, argument);
628
+ mxPushSlot(mxThis);
629
+ mxGetID(mxID(_global));
630
+ if (fxToBoolean(the, the->stack++)) {
631
+ txIndex count = 0;
632
+ txBoolean unicodeFlag;
633
+ mxPushSlot(mxThis);
634
+ mxGetID(mxID(_unicode));
635
+ unicodeFlag = fxToBoolean(the, the->stack);
636
+ mxPop();
637
+ mxPushInteger(0);
638
+ mxPushSlot(mxThis);
639
+ mxSetID(mxID(_lastIndex));
640
+ mxPop();
641
+ mxPush(mxArrayPrototype);
642
+ fxNewArrayInstance(the);
643
+ mxPullSlot(mxResult);
644
+ for (;;) {
645
+ fxExecuteRegExp(the, mxThis, argument);
646
+ if (the->stack->kind == XS_NULL_KIND) {
647
+ if (count == 0)
648
+ mxPullSlot(mxResult);
649
+ else
650
+ mxPop();
651
+ break;
652
+ }
653
+ mxGetIndex(0);
654
+ fxToString(the, the->stack);
655
+ mxPushSlot(mxResult);
656
+ mxDefineIndex(count, 0, XS_GET_ONLY);
657
+ if (c_isEmpty(the->stack->value.string)) {
658
+ mxPushSlot(mxThis);
659
+ mxGetID(mxID(_lastIndex));
660
+ fxToLength(the, the->stack);
661
+ the->stack->value.number = fxAdvanceStringIndex(argument->value.string, the->stack->value.number, unicodeFlag);
662
+ mxPushSlot(mxThis);
663
+ mxSetID(mxID(_lastIndex));
664
+ mxPop();
665
+ }
666
+ mxPop();
667
+ count++;
668
+ }
669
+ }
670
+ else {
671
+ fxExecuteRegExp(the, mxThis, argument);
672
+ mxPullSlot(mxResult);
673
+ }
674
+ mxPop();
675
+ #endif
676
+ }
677
+
678
+ void fx_RegExp_prototype_matchAll(txMachine* the)
679
+ {
680
+ #if mxRegExp
681
+ txSlot* argument;
682
+ txBoolean globalFlag = 0;
683
+ txBoolean unicodeFlag = 0;
684
+ txSlot* matcher;
685
+ txSlot* iterator;
686
+ txSlot* property;
687
+
688
+ if (!mxIsReference(mxThis))
689
+ mxTypeError("this is no object");
690
+ if (mxArgc == 0)
691
+ mxPushUndefined();
692
+ else
693
+ mxPushSlot(mxArgv(0));
694
+ argument = the->stack;
695
+ fxToString(the, argument);
696
+
697
+ mxPushSlot(mxThis);
698
+ mxGetID(mxID(_constructor));
699
+ fxToSpeciesConstructor(the, &mxRegExpConstructor);
700
+ mxNew();
701
+ mxPushSlot(mxThis);
702
+ mxPushSlot(mxThis);
703
+ mxGetID(mxID(_flags));
704
+ if (c_strchr(fxToString(the, the->stack), 'g'))
705
+ globalFlag = 1;
706
+ if (c_strchr(fxToString(the, the->stack), 'u'))
707
+ unicodeFlag = 1;
708
+ mxRunCount(2);
709
+ matcher = the->stack;
710
+
711
+ mxPushSlot(mxThis);
712
+ mxGetID(mxID(_lastIndex));
713
+ fxToInteger(the, the->stack);
714
+ mxPushSlot(matcher);
715
+ mxSetID(mxID(_lastIndex));
716
+
717
+ mxPush(mxRegExpStringIteratorPrototype);
718
+ iterator = fxNewIteratorInstance(the, matcher, mxID(_RegExp));
719
+ property = fxLastProperty(the, iterator);
720
+ property->kind = argument->kind;
721
+ property->value = argument->value;
722
+ property = fxNextBooleanProperty(the, property, globalFlag, XS_NO_ID, XS_INTERNAL_FLAG);
723
+ property = fxNextBooleanProperty(the, property, unicodeFlag, XS_NO_ID, XS_INTERNAL_FLAG);
724
+ property = fxNextBooleanProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG);
725
+ mxPullSlot(mxResult);
726
+ #endif
727
+ }
728
+
729
+ void fx_RegExp_prototype_matchAll_next(txMachine* the)
730
+ {
731
+ #if mxRegExp
732
+ txSlot* iterator = fxCheckIteratorInstance(the, mxThis, mxID(_RegExp));
733
+ txSlot* result = iterator->next;
734
+ txSlot* value = fxCheckIteratorResult(the, result);
735
+ txSlot* done = value->next;
736
+ txSlot* matcher = result->next;
737
+ txSlot* argument = matcher->next;
738
+ txSlot* global = argument->next;
739
+ txSlot* unicode = global->next;
740
+ txSlot* complete = unicode->next;
741
+ txSlot* match;
742
+ if (complete->value.boolean) {
743
+ value->kind = XS_UNDEFINED_KIND;
744
+ done->value.boolean = 1;
745
+ }
746
+ else {
747
+ fxExecuteRegExp(the, matcher, argument);
748
+ match = the->stack;
749
+ if (match->kind == XS_NULL_KIND) {
750
+ value->kind = XS_UNDEFINED_KIND;
751
+ done->value.boolean = 1;
752
+ complete->value.boolean = 1;
753
+ }
754
+ else if (global->value.boolean) {
755
+ mxPushSlot(match);
756
+ mxGetIndex(0);
757
+ fxToString(the, the->stack);
758
+ if (c_isEmpty(the->stack->value.string)) {
759
+ mxPushSlot(matcher);
760
+ mxGetID(mxID(_lastIndex));
761
+ fxToLength(the, the->stack);
762
+ the->stack->value.number = fxAdvanceStringIndex(argument->value.string, the->stack->value.number, unicode->value.boolean);
763
+ mxPushSlot(matcher);
764
+ mxSetID(mxID(_lastIndex));
765
+ mxPop();
766
+ }
767
+ mxPop();
768
+ value->kind = match->kind;
769
+ value->value = match->value;
770
+ done->value.boolean = 0;
771
+ }
772
+ else {
773
+ value->kind = match->kind;
774
+ value->value = match->value;
775
+ done->value.boolean = 0;
776
+ complete->value.boolean = 1;
777
+ }
778
+ mxPop();
779
+ }
780
+ mxResult->kind = result->kind;
781
+ mxResult->value = result->value;
782
+ #endif
783
+ }
784
+
785
+ void fx_RegExp_prototype_replace(txMachine* the)
786
+ {
787
+ #if mxRegExp
788
+ txSlot* argument;
789
+ txSlot* function = C_NULL;;
790
+ txSlot* replacement = C_NULL;;
791
+ txBoolean globalFlag;
792
+ txBoolean unicodeFlag = 0;
793
+ txSlot* list;
794
+ txSlot* item;
795
+ txSize size;
796
+ txSize utf8Size;
797
+ txInteger former;
798
+ txSlot* result;
799
+ txSlot* matched;
800
+ txSize matchLength;
801
+ txInteger c, i;
802
+ txInteger position;
803
+ fxToInstance(the, mxThis);
804
+ if (mxArgc <= 0)
805
+ mxPushUndefined();
806
+ else
807
+ mxPushSlot(mxArgv(0));
808
+ argument = the->stack;
809
+ fxToString(the, argument);
810
+ if (mxArgc <= 1)
811
+ mxPushUndefined();
812
+ else
813
+ mxPushSlot(mxArgv(1));
814
+ if (mxIsReference(the->stack) && mxIsFunction(the->stack->value.reference))
815
+ function = the->stack;
816
+ else {
817
+ replacement = the->stack;
818
+ fxToString(the, replacement);
819
+ }
820
+ mxPushSlot(mxThis);
821
+ mxGetID(mxID(_global));
822
+ globalFlag = fxToBoolean(the, the->stack++);
823
+ if (globalFlag) {
824
+ mxPushSlot(mxThis);
825
+ mxGetID(mxID(_unicode));
826
+ unicodeFlag = fxToBoolean(the, the->stack++);
827
+
828
+ mxPushInteger(0);
829
+ mxPushSlot(mxThis);
830
+ mxSetID(mxID(_lastIndex));
831
+ mxPop();
832
+ }
833
+ list = item = fxNewInstance(the);
834
+ mxPushSlot(list);
835
+ size = fxUnicodeLength(argument->value.string);
836
+ utf8Size = mxStringLength(argument->value.string);
837
+ former = 0;
838
+ for (;;) {
839
+ fxExecuteRegExp(the, mxThis, argument);
840
+ if (the->stack->kind == XS_NULL_KIND) {
841
+ mxPop();
842
+ break;
843
+ }
844
+ result = the->stack;
845
+
846
+ mxPushSlot(result);
847
+ mxGetID(mxID(_index));
848
+ position = fxToInteger(the, the->stack);
849
+ mxPop();
850
+ if (position < 0) position = 0;
851
+ else if (position > size) position = size;
852
+
853
+ if (former < position) {
854
+ item = item->next = fxNewSlot(the);
855
+ fx_RegExp_prototype_split_aux(the, argument, former, position, item);
856
+ }
857
+
858
+ if (former <= position) {
859
+ mxPushSlot(result);
860
+ mxGetIndex(0);
861
+ fxToString(the, the->stack);
862
+ matched = the->stack;
863
+ matchLength = fxUnicodeLength(matched->value.string);
864
+
865
+ mxPushSlot(result);
866
+ mxGetID(mxID(_length));
867
+ c = fxToInteger(the, the->stack);
868
+ mxPop();
869
+
870
+ if (function) {
871
+ mxPushUndefined();
872
+ mxPushSlot(function);
873
+ mxCall();
874
+ }
875
+ mxPushSlot(matched);
876
+ for (i = 1; i < c; i++) {
877
+ mxPushSlot(result);
878
+ mxGetIndex(i);
879
+ if (the->stack->kind != XS_UNDEFINED_KIND)
880
+ fxToString(the, the->stack);
881
+ }
882
+ if (function) {
883
+ mxPushInteger(position);
884
+ mxPushSlot(argument);
885
+ mxPushSlot(result);
886
+ mxGetID(mxID(_groups));
887
+ if (mxIsUndefined(the->stack)) {
888
+ mxPop();
889
+ mxRunCount(3 + i - 1);
890
+ }
891
+ else {
892
+ mxRunCount(4 + i - 1);
893
+ }
894
+ fxToString(the, the->stack);
895
+ item = item->next = fxNewSlot(the);
896
+ mxPullSlot(item);
897
+ }
898
+ else {
899
+ mxPushSlot(result);
900
+ mxGetID(mxID(_groups));
901
+ if (!mxIsUndefined(the->stack))
902
+ fxToInstance(the, the->stack);
903
+ fxPushSubstitutionString(the, argument, utf8Size, fxUnicodeToUTF8Offset(argument->value.string, position), matched, mxStringLength(matched->value.string), i - 1, the->stack + 1, the->stack, replacement);
904
+ item = item->next = fxNewSlot(the);
905
+ mxPullSlot(item);
906
+ the->stack += 1 + i;
907
+ }
908
+ former = position + matchLength;
909
+
910
+ mxPop(); // matched
911
+ }
912
+ else
913
+ matchLength = 0;
914
+
915
+ if (!globalFlag)
916
+ break;
917
+
918
+ if (0 == matchLength) {
919
+ mxPushSlot(mxThis);
920
+ mxGetID(mxID(_lastIndex));
921
+ fxToLength(the, the->stack);
922
+ the->stack->value.number = fxAdvanceStringIndex(argument->value.string, the->stack->value.number, unicodeFlag);
923
+ mxPushSlot(mxThis);
924
+ mxSetID(mxID(_lastIndex));
925
+ mxPop();
926
+ }
927
+ mxPop();
928
+ }
929
+ if (former < size) {
930
+ item = item->next = fxNewSlot(the);
931
+ fx_RegExp_prototype_split_aux(the, argument, former, size, item);
932
+ }
933
+ size = 0;
934
+ item = list->next;
935
+ while (item) {
936
+ item->value.key.sum = mxStringLength(item->value.string);
937
+ size = fxAddChunkSizes(the, size, item->value.key.sum);
938
+ item = item->next;
939
+ }
940
+ size = fxAddChunkSizes(the, size, 1);
941
+ mxResult->value.string = (txString)fxNewChunk(the, size);
942
+ size = 0;
943
+ item = list->next;
944
+ while (item) {
945
+ c_memcpy(mxResult->value.string + size, item->value.string, item->value.key.sum);
946
+ size += item->value.key.sum;
947
+ item = item->next;
948
+ }
949
+ mxResult->value.string[size] = 0;
950
+ mxResult->kind = XS_STRING_KIND;
951
+ mxPop();
952
+ mxPop();
953
+ #endif
954
+ }
955
+
956
+ void fx_RegExp_prototype_search(txMachine* the)
957
+ {
958
+ #if mxRegExp
959
+ txSlot* argument;
960
+ txSlot* lastIndex;
961
+ fxToInstance(the, mxThis);
962
+ if (mxArgc == 0)
963
+ mxPushUndefined();
964
+ else
965
+ mxPushSlot(mxArgv(0));
966
+ argument = the->stack;
967
+ fxToString(the, argument);
968
+ mxPushSlot(mxThis);
969
+ mxGetID(mxID(_lastIndex));
970
+ lastIndex = the->stack;
971
+
972
+ mxPushInteger(0);
973
+ if (!fxIsSameValue(the, the->stack, lastIndex, 0)) {
974
+ mxPushInteger(0);
975
+ mxPushSlot(mxThis);
976
+ mxSetID(mxID(_lastIndex));
977
+ mxPop();
978
+ }
979
+ mxPop();
980
+
981
+ fxExecuteRegExp(the, mxThis, argument);
982
+ if (the->stack->kind == XS_NULL_KIND) {
983
+ the->stack->kind = XS_INTEGER_KIND;
984
+ the->stack->value.integer = -1;
985
+ }
986
+ else
987
+ mxGetID(mxID(_index));
988
+ mxPullSlot(mxResult);
989
+
990
+ mxPushSlot(mxThis);
991
+ mxGetID(mxID(_lastIndex));
992
+ if (!fxIsSameValue(the, the->stack, lastIndex, 0)) {
993
+ mxPushSlot(lastIndex);
994
+ mxPushSlot(mxThis);
995
+ mxSetID(mxID(_lastIndex));
996
+ mxPop();
997
+ }
998
+ mxPop();
999
+
1000
+ mxPop();
1001
+ #endif
1002
+ }
1003
+
1004
+ void fx_RegExp_prototype_split(txMachine* the)
1005
+ {
1006
+ #if mxRegExp
1007
+ txSlot* argument;
1008
+ txBoolean unicodeFlag = 0;
1009
+ txIndex limit;
1010
+ txSlot* splitter;
1011
+ txSlot* array;
1012
+ txSlot* item;
1013
+ txInteger size, p, q, e, c, i;
1014
+
1015
+ if (!mxIsReference(mxThis))
1016
+ mxTypeError("this is no object");
1017
+ if (mxArgc == 0)
1018
+ mxPushUndefined();
1019
+ else
1020
+ mxPushSlot(mxArgv(0));
1021
+ argument = the->stack;
1022
+ fxToString(the, argument);
1023
+ limit = ((mxArgc > 1) && (!mxIsUndefined(mxArgv(1)))) ? fxToUnsigned(the, mxArgv(1)) : 0xFFFFFFFF;
1024
+
1025
+ mxPushSlot(mxThis);
1026
+ mxGetID(mxID(_constructor));
1027
+ fxToSpeciesConstructor(the, &mxRegExpConstructor);
1028
+ mxNew();
1029
+ mxPushSlot(mxThis);
1030
+ mxPushSlot(mxThis);
1031
+ mxGetID(mxID(_flags));
1032
+ if (!c_strchr(fxToString(the, the->stack), 'y'))
1033
+ fxConcatStringC(the, the->stack, "y");
1034
+ if (c_strchr(fxToString(the, the->stack), 'u'))
1035
+ unicodeFlag = 1;
1036
+ mxRunCount(2);
1037
+ splitter = the->stack;
1038
+
1039
+ mxPush(mxArrayPrototype);
1040
+ array = fxNewArrayInstance(the);
1041
+ mxPullSlot(mxResult);
1042
+ item = fxLastProperty(the, array);
1043
+ if (!limit)
1044
+ goto bail;
1045
+ size = fxUnicodeLength(argument->value.string);
1046
+ if (size == 0) {
1047
+ fxExecuteRegExp(the, splitter, argument);
1048
+ if (the->stack->kind == XS_NULL_KIND) {
1049
+ item = item->next = fxNewSlot(the);
1050
+ item->value.string = mxEmptyString.value.string;
1051
+ item->kind = mxEmptyString.kind;
1052
+ array->next->value.array.length++;
1053
+ }
1054
+ mxPop();
1055
+ goto bail;
1056
+ }
1057
+ p = q = 0;
1058
+ while (q < size) {
1059
+ mxPushInteger(q);
1060
+ mxPushSlot(splitter);
1061
+ mxSetID(mxID(_lastIndex));
1062
+ mxPop();
1063
+ fxExecuteRegExp(the, splitter, argument);
1064
+ if (the->stack->kind == XS_NULL_KIND) {
1065
+ q = (txInteger)fxAdvanceStringIndex(argument->value.string, q, unicodeFlag);
1066
+ }
1067
+ else {
1068
+ mxPushSlot(splitter);
1069
+ mxGetID(mxID(_lastIndex));
1070
+ e = fxToInteger(the, the->stack);
1071
+ mxPop();
1072
+ if (e == p) {
1073
+ q = (txInteger)fxAdvanceStringIndex(argument->value.string, q, unicodeFlag);
1074
+ }
1075
+ else {
1076
+ txSlot* result = the->stack;
1077
+ item = item->next = fxNewSlot(the);
1078
+ fx_RegExp_prototype_split_aux(the, argument, p, q, item);
1079
+ array->next->value.array.length++;
1080
+ if (array->next->value.array.length == limit)
1081
+ goto bail;
1082
+ p = e;
1083
+ mxPushSlot(result);
1084
+ mxGetID(mxID(_length));
1085
+ c = fxToInteger(the, the->stack);
1086
+ mxPop();
1087
+ c--; if (c < 0) c = 0;
1088
+ i = 1;
1089
+ while (i <= c) {
1090
+ mxPushSlot(result);
1091
+ mxGetIndex(i);
1092
+ item = item->next = fxNewSlot(the);
1093
+ mxPullSlot(item);
1094
+ array->next->value.array.length++;
1095
+ if (array->next->value.array.length == limit)
1096
+ goto bail;
1097
+ i++;
1098
+ }
1099
+ q = p;
1100
+ }
1101
+ }
1102
+ mxPop();
1103
+ }
1104
+ //if (p < 0)
1105
+ // p = 0;
1106
+ item = item->next = fxNewSlot(the);
1107
+ fx_RegExp_prototype_split_aux(the, argument, p, size, item);
1108
+ array->next->value.array.length++;
1109
+ bail:
1110
+ fxCacheArray(the, array);
1111
+ mxPop();
1112
+ mxPop();
1113
+ mxPop();
1114
+ #endif
1115
+ }
1116
+
1117
+ void fx_RegExp_prototype_split_aux(txMachine* the, txSlot* string, txIndex start, txIndex stop, txSlot* item)
1118
+ {
1119
+ #if mxRegExp
1120
+ txInteger offset = fxUnicodeToUTF8Offset(string->value.string, start);
1121
+ txInteger length = fxUnicodeToUTF8Offset(string->value.string + offset, stop - start);
1122
+ if ((offset >= 0) && (length > 0)) {
1123
+ item->value.string = (txString)fxNewChunk(the, length + 1);
1124
+ c_memcpy(item->value.string, string->value.string + offset, length);
1125
+ item->value.string[length] = 0;
1126
+ item->kind = XS_STRING_KIND;
1127
+ }
1128
+ else {
1129
+ item->value.string = mxEmptyString.value.string;
1130
+ item->kind = mxEmptyString.kind;
1131
+ }
1132
+ #endif
1133
+ }
1134
+
1135
+ void fx_RegExp_prototype_test(txMachine* the)
1136
+ {
1137
+ #if mxRegExp
1138
+ fxToInstance(the, mxThis);
1139
+ if (mxArgc > 0)
1140
+ mxPushSlot(mxArgv(0));
1141
+ else
1142
+ mxPushUndefined();
1143
+ fxToString(the, the->stack);
1144
+ fxExecuteRegExp(the, mxThis, the->stack);
1145
+ mxResult->value.boolean = (the->stack->kind != XS_NULL_KIND) ? 1 : 0;
1146
+ mxResult->kind = XS_BOOLEAN_KIND;
1147
+ mxPop();
1148
+ #endif
1149
+ }
1150
+
1151
+ void fx_RegExp_prototype_toString(txMachine* the)
1152
+ {
1153
+ #if mxRegExp
1154
+ fxToInstance(the, mxThis);
1155
+ fxStringX(the, mxResult, "/");
1156
+ mxPushSlot(mxThis);
1157
+ mxGetID(mxID(_source));
1158
+ fxToString(the, the->stack);
1159
+ fxConcatString(the, mxResult, the->stack);
1160
+ mxPop();
1161
+ fxConcatStringC(the, mxResult, "/");
1162
+ mxPushSlot(mxThis);
1163
+ mxGetID(mxID(_flags));
1164
+ fxToString(the, the->stack);
1165
+ fxConcatString(the, mxResult, the->stack);
1166
+ mxPop();
1167
+ #endif
1168
+ }