@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,1091 @@
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
+ enum {
41
+ XS_NO_JSON_TOKEN,
42
+ XS_JSON_TOKEN_COLON,
43
+ XS_JSON_TOKEN_COMMA,
44
+ XS_JSON_TOKEN_EOF,
45
+ XS_JSON_TOKEN_FALSE,
46
+ XS_JSON_TOKEN_INTEGER,
47
+ XS_JSON_TOKEN_LEFT_BRACE,
48
+ XS_JSON_TOKEN_LEFT_BRACKET,
49
+ XS_JSON_TOKEN_NULL,
50
+ XS_JSON_TOKEN_NUMBER,
51
+ XS_JSON_TOKEN_RIGHT_BRACE,
52
+ XS_JSON_TOKEN_RIGHT_BRACKET,
53
+ XS_JSON_TOKEN_STRING,
54
+ XS_JSON_TOKEN_TRUE,
55
+ };
56
+
57
+ typedef struct {
58
+ txSlot* slot;
59
+ txSize offset;
60
+ txInteger integer;
61
+ txNumber number;
62
+ txSlot* string;
63
+ txInteger token;
64
+ txSlot* keys;
65
+ txInteger line;
66
+ } txJSONParser;
67
+
68
+ typedef struct {
69
+ txString buffer;
70
+ char indent[64];
71
+ txInteger indentLength;
72
+ txInteger level;
73
+ txSize offset;
74
+ txSize size;
75
+ txSlot* replacer;
76
+ txSlot* keys;
77
+ txSlot* stack;
78
+ } txJSONStringifier;
79
+
80
+ static void fxParseJSON(txMachine* the, txJSONParser* theParser);
81
+ static void fxParseJSONArray(txMachine* the, txJSONParser* theParser);
82
+ static void fxParseJSONObject(txMachine* the, txJSONParser* theParser);
83
+ static void fxParseJSONToken(txMachine* the, txJSONParser* theParser);
84
+ static void fxParseJSONValue(txMachine* the, txJSONParser* theParser);
85
+ static void fxReviveJSON(txMachine* the, txSlot* reviver);
86
+
87
+ static void fxStringifyJSON(txMachine* the, txJSONStringifier* theStringifier);
88
+ static void fxStringifyJSONCharacter(txMachine* the, txJSONStringifier* theStringifier, txInteger character);
89
+ static void fxStringifyJSONChars(txMachine* the, txJSONStringifier* theStringifier, char* s, txSize theSize);
90
+ static void fxStringifyJSONIndent(txMachine* the, txJSONStringifier* theStringifier);
91
+ static void fxStringifyJSONInteger(txMachine* the, txJSONStringifier* theStringifier, txInteger theInteger);
92
+ static void fxStringifyJSONName(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag);
93
+ static void fxStringifyJSONNumber(txMachine* the, txJSONStringifier* theStringifier, txNumber theNumber);
94
+ static void fxStringifyJSONProperty(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag);
95
+ static void fxStringifyJSONString(txMachine* the, txJSONStringifier* theStringifier, txString theString);
96
+ static void fxStringifyJSONUnicodeEscape(txMachine* the, txJSONStringifier* theStringifier, txInteger character);
97
+
98
+ static txSlot* fxToJSONKeys(txMachine* the, txSlot* reference);
99
+
100
+ void fxBuildJSON(txMachine* the)
101
+ {
102
+ txSlot* slot;
103
+ mxPush(mxObjectPrototype);
104
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
105
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_parse), 2, mxID(_parse), XS_DONT_ENUM_FLAG);
106
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_stringify), 3, mxID(_stringify), XS_DONT_ENUM_FLAG);
107
+ slot = fxNextStringXProperty(the, slot, "JSON", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
108
+ mxPull(mxJSONObject);
109
+ }
110
+
111
+ void fx_JSON_parse(txMachine* the)
112
+ {
113
+ volatile txJSONParser aParser = {0};
114
+
115
+ if (mxArgc < 1)
116
+ mxSyntaxError("no buffer");
117
+ if ((mxArgc > 1) && mxIsReference(mxArgv(1)) && fxIsArray(the, mxArgv(1)->value.reference))
118
+ aParser.keys = fxToJSONKeys(the, mxArgv(1));
119
+ fxToString(the, mxArgv(0));
120
+ aParser.slot = mxArgv(0);
121
+ aParser.offset = 0;
122
+ fxParseJSON(the, (txJSONParser*)&aParser);
123
+ mxPullSlot(mxResult);
124
+ if (aParser.keys)
125
+ mxPop();
126
+ if ((mxArgc > 1) && mxIsReference(mxArgv(1)) && mxIsCallable(mxArgv(1)->value.reference)) {
127
+ txSlot* instance;
128
+ txID id;
129
+ mxPush(mxObjectPrototype);
130
+ instance = fxNewObjectInstance(the);
131
+ id = fxID(the, "");
132
+ mxBehaviorDefineOwnProperty(the, instance, id, 0, mxResult, XS_GET_ONLY);
133
+ mxPushSlot(mxArgv(1));
134
+ mxCall();
135
+ mxPushUndefined();
136
+ fxKeyAt(the, id, 0, the->stack);
137
+ mxPushSlot(mxResult);
138
+ fxReviveJSON(the, mxArgv(1));
139
+ mxPullSlot(mxResult);
140
+ }
141
+ }
142
+
143
+ void fxParseJSON(txMachine* the, txJSONParser* theParser)
144
+ {
145
+ mxPush(mxEmptyString);
146
+ theParser->string = the->stack;
147
+ theParser->line = 1;
148
+ fxParseJSONToken(the, theParser);
149
+ fxParseJSONValue(the, theParser);
150
+ if (theParser->token != XS_JSON_TOKEN_EOF)
151
+ mxSyntaxError("%ld: missing EOF", theParser->line);
152
+ }
153
+
154
+ void fxParseJSONArray(txMachine* the, txJSONParser* theParser)
155
+ {
156
+ txSlot* anArray;
157
+ txIndex aLength;
158
+ txSlot* anItem;
159
+
160
+ mxCheckCStack();
161
+ fxParseJSONToken(the, theParser);
162
+ mxPush(mxArrayPrototype);
163
+ anArray = fxNewArrayInstance(the);
164
+ aLength = 0;
165
+ anItem = fxLastProperty(the, anArray);
166
+ for (;;) {
167
+ if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACKET)
168
+ break;
169
+ if (aLength) {
170
+ if (theParser->token == XS_JSON_TOKEN_COMMA)
171
+ fxParseJSONToken(the, theParser);
172
+ else
173
+ mxSyntaxError("%ld: missing ,", theParser->line);
174
+ }
175
+ fxParseJSONValue(the, theParser);
176
+ aLength++;
177
+ anItem->next = fxNewSlot(the);
178
+ anItem = anItem->next;
179
+ anItem->kind = the->stack->kind;
180
+ anItem->value = the->stack->value;
181
+ mxPop();
182
+ }
183
+ anArray->next->value.array.length = aLength;
184
+ fxCacheArray(the, anArray);
185
+ fxParseJSONToken(the, theParser);
186
+ }
187
+
188
+ void fxParseJSONToken(txMachine* the, txJSONParser* theParser)
189
+ {
190
+ txInteger character;
191
+ txBoolean escaped;
192
+ txNumber number;
193
+ txSize offset;
194
+ txSize size;
195
+ txString p, s;
196
+
197
+ theParser->integer = 0;
198
+ theParser->number = 0;
199
+ theParser->string->value.string = mxEmptyString.value.string;
200
+ theParser->string->kind = mxEmptyString.kind;
201
+ theParser->token = XS_NO_JSON_TOKEN;
202
+ p = theParser->slot->value.string + theParser->offset;
203
+ while (theParser->token == XS_NO_JSON_TOKEN) {
204
+ switch (*p) {
205
+ case 0:
206
+ theParser->token = XS_JSON_TOKEN_EOF;
207
+ break;
208
+ case 10:
209
+ p++;
210
+ theParser->line++;
211
+ break;
212
+ case 13:
213
+ p++;
214
+ theParser->line++;
215
+ if (*p == 10)
216
+ p++;
217
+ break;
218
+ case '\t':
219
+ case ' ':
220
+ p++;
221
+ break;
222
+ case '-':
223
+ case '0':
224
+ case '1':
225
+ case '2':
226
+ case '3':
227
+ case '4':
228
+ case '5':
229
+ case '6':
230
+ case '7':
231
+ case '8':
232
+ case '9':
233
+ s = p;
234
+ if (*p == '-')
235
+ p++;
236
+ if (('0' <= *p) && (*p <= '9')) {
237
+ if (*p == '0') {
238
+ p++;
239
+ }
240
+ else {
241
+ p++;
242
+ while (('0' <= *p) && (*p <= '9'))
243
+ p++;
244
+ }
245
+ if (*p == '.') {
246
+ p++;
247
+ if (('0' <= *p) && (*p <= '9')) {
248
+ p++;
249
+ while (('0' <= *p) && (*p <= '9'))
250
+ p++;
251
+ }
252
+ else
253
+ goto error;
254
+ }
255
+ if ((*p == 'e') || (*p == 'E')) {
256
+ p++;
257
+ if ((*p == '+') || (*p == '-'))
258
+ p++;
259
+ if (('0' <= *p) && (*p <= '9')) {
260
+ p++;
261
+ while (('0' <= *p) && (*p <= '9'))
262
+ p++;
263
+ }
264
+ else
265
+ goto error;
266
+ }
267
+ }
268
+ else
269
+ goto error;
270
+ size = mxPtrDiff(p - s);
271
+ if ((size_t)(size + 1) > sizeof(the->nameBuffer))
272
+ mxSyntaxError("%ld: number overflow", theParser->line);
273
+ c_memcpy(the->nameBuffer, s, size);
274
+ the->nameBuffer[size] = 0;
275
+ theParser->number = fxStringToNumber(the->dtoa, the->nameBuffer, 0);
276
+ theParser->integer = (txInteger)theParser->number;
277
+ number = theParser->integer;
278
+ if ((theParser->number == number) && (theParser->number != -0))
279
+ theParser->token = XS_JSON_TOKEN_INTEGER;
280
+ else
281
+ theParser->token = XS_JSON_TOKEN_NUMBER;
282
+ break;
283
+ case ',':
284
+ p++;
285
+ theParser->token = XS_JSON_TOKEN_COMMA;
286
+ break;
287
+ case ':':
288
+ p++;
289
+ theParser->token = XS_JSON_TOKEN_COLON;
290
+ break;
291
+ case '[':
292
+ p++;
293
+ theParser->token = XS_JSON_TOKEN_LEFT_BRACKET;
294
+ break;
295
+ case ']':
296
+ p++;
297
+ theParser->token = XS_JSON_TOKEN_RIGHT_BRACKET;
298
+ break;
299
+ case '{':
300
+ p++;
301
+ theParser->token = XS_JSON_TOKEN_LEFT_BRACE;
302
+ break;
303
+ case '}':
304
+ p++;
305
+ theParser->token = XS_JSON_TOKEN_RIGHT_BRACE;
306
+ break;
307
+ case '"':
308
+ p++;
309
+ escaped = 0;
310
+ offset = mxPtrDiff(p - theParser->slot->value.string);
311
+ size = 0;
312
+ for (;;) {
313
+ p = mxStringByteDecode(p, &character);
314
+ if (character < 32) {
315
+ goto error;
316
+ }
317
+ else if (character == '"') {
318
+ break;
319
+ }
320
+ else if (character == '\\') {
321
+ escaped = 1;
322
+ switch (*p) {
323
+ case '"':
324
+ case '/':
325
+ case '\\':
326
+ case 'b':
327
+ case 'f':
328
+ case 'n':
329
+ case 'r':
330
+ case 't':
331
+ p++;
332
+ size++;
333
+ break;
334
+ case 'u':
335
+ p++;
336
+ if (fxParseUnicodeEscape(&p, &character, 0, '\\'))
337
+ size += mxStringByteLength(character);
338
+ else
339
+ goto error;
340
+ break;
341
+ default:
342
+ goto error;
343
+ }
344
+ }
345
+ else {
346
+ size += mxStringByteLength(character);
347
+ }
348
+ }
349
+ s = theParser->string->value.string = fxNewChunk(the, size + 1);
350
+ theParser->string->kind = XS_STRING_KIND;
351
+ p = theParser->slot->value.string + offset;
352
+ if (escaped) {
353
+ for (;;) {
354
+ if (*p == '"') {
355
+ p++;
356
+ *s = 0;
357
+ break;
358
+ }
359
+ else if (*p == '\\') {
360
+ p++;
361
+ switch (*p) {
362
+ case '"':
363
+ case '/':
364
+ case '\\':
365
+ *s++ = *p++;
366
+ break;
367
+ case 'b':
368
+ p++;
369
+ *s++ = '\b';
370
+ break;
371
+ case 'f':
372
+ p++;
373
+ *s++ = '\f';
374
+ break;
375
+ case 'n':
376
+ p++;
377
+ *s++ = '\n';
378
+ break;
379
+ case 'r':
380
+ p++;
381
+ *s++ = '\r';
382
+ break;
383
+ case 't':
384
+ p++;
385
+ *s++ = '\t';
386
+ break;
387
+ case 'u':
388
+ p++;
389
+ fxParseUnicodeEscape(&p, &character, 0, '\\');
390
+ s = mxStringByteEncode(s, character);
391
+ break;
392
+ }
393
+ }
394
+ else {
395
+ *s++ = *p++;
396
+ }
397
+ }
398
+ }
399
+ else {
400
+ c_memcpy(s, p, size);
401
+ p += size + 1;
402
+ s[size] = 0;
403
+ }
404
+ theParser->token = XS_JSON_TOKEN_STRING;
405
+ break;
406
+ case 'f':
407
+ p++;
408
+ if (*p != 'a') goto error;
409
+ p++;
410
+ if (*p != 'l') goto error;
411
+ p++;
412
+ if (*p != 's') goto error;
413
+ p++;
414
+ if (*p != 'e') goto error;
415
+ p++;
416
+ theParser->token = XS_JSON_TOKEN_FALSE;
417
+ break;
418
+ case 'n':
419
+ p++;
420
+ if (*p != 'u') goto error;
421
+ p++;
422
+ if (*p != 'l') goto error;
423
+ p++;
424
+ if (*p != 'l') goto error;
425
+ p++;
426
+ theParser->token = XS_JSON_TOKEN_NULL;
427
+ break;
428
+ case 't':
429
+ p++;
430
+ if (*p != 'r') goto error;
431
+ p++;
432
+ if (*p != 'u') goto error;
433
+ p++;
434
+ if (*p != 'e') goto error;
435
+ p++;
436
+ theParser->token = XS_JSON_TOKEN_TRUE;
437
+ break;
438
+ default:
439
+ error:
440
+ mxSyntaxError("%ld: invalid character", theParser->line);
441
+ break;
442
+ }
443
+ }
444
+ theParser->offset = mxPtrDiff(p - theParser->slot->value.string);
445
+ }
446
+
447
+ void fxParseJSONObject(txMachine* the, txJSONParser* theParser)
448
+ {
449
+ txSlot* anObject;
450
+ txBoolean comma = 0;
451
+ txSlot* at;
452
+ txIndex index;
453
+ txID id;
454
+ txSlot* aProperty;
455
+
456
+ mxCheckCStack();
457
+ fxParseJSONToken(the, theParser);
458
+ mxPush(mxObjectPrototype);
459
+ anObject = fxNewObjectInstance(the);
460
+ for (;;) {
461
+ if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACE)
462
+ break;
463
+ if (comma) {
464
+ if (theParser->token == XS_JSON_TOKEN_COMMA)
465
+ fxParseJSONToken(the, theParser);
466
+ else
467
+ mxSyntaxError("%ld: missing ,", theParser->line);
468
+ }
469
+ if (theParser->token != XS_JSON_TOKEN_STRING)
470
+ mxSyntaxError("%ld: missing name", theParser->line);
471
+ mxPushString(theParser->string->value.string);
472
+ at = the->stack;
473
+ index = 0;
474
+ if (theParser->keys) {
475
+ at->kind = XS_UNDEFINED_KIND;
476
+ if (fxStringToIndex(the->dtoa, at->value.string, &index))
477
+ id = 0;
478
+ else
479
+ id = fxFindName(the, at->value.string);
480
+ if (id != XS_NO_ID) {
481
+ txSlot* item = theParser->keys->value.reference->next;
482
+ while (item) {
483
+ if ((item->value.at.id == id) && (item->value.at.index == index)) {
484
+ at->value.at.id = id;
485
+ at->value.at.index = index;
486
+ at->kind = XS_AT_KIND;
487
+ break;
488
+ }
489
+ item = item->next;
490
+ }
491
+ }
492
+ }
493
+ else {
494
+ if (fxStringToIndex(the->dtoa, at->value.string, &index))
495
+ id = 0;
496
+ else
497
+ id = fxNewName(the, at);
498
+ at->value.at.id = id;
499
+ at->value.at.index = index;
500
+ at->kind = XS_AT_KIND;
501
+ }
502
+ fxParseJSONToken(the, theParser);
503
+ if (theParser->token != XS_JSON_TOKEN_COLON)
504
+ mxSyntaxError("%ld: missing :", theParser->line);
505
+ fxParseJSONToken(the, theParser);
506
+ fxParseJSONValue(the, theParser);
507
+ if ((at->kind == XS_AT_KIND) && (the->stack->kind != XS_UNDEFINED_KIND)) {
508
+ aProperty = mxBehaviorSetProperty(the, anObject, at->value.at.id, at->value.at.index, XS_OWN);
509
+ aProperty->kind = the->stack->kind;
510
+ aProperty->value = the->stack->value;
511
+ }
512
+ mxPop();
513
+ mxPop();
514
+ comma = 1;
515
+ }
516
+ fxParseJSONToken(the, theParser);
517
+ }
518
+
519
+ void fxParseJSONValue(txMachine* the, txJSONParser* theParser)
520
+ {
521
+ switch (theParser->token) {
522
+ case XS_JSON_TOKEN_FALSE:
523
+ mxPushBoolean(0);
524
+ fxParseJSONToken(the, theParser);
525
+ break;
526
+ case XS_JSON_TOKEN_TRUE:
527
+ mxPushBoolean(1);
528
+ fxParseJSONToken(the, theParser);
529
+ break;
530
+ case XS_JSON_TOKEN_NULL:
531
+ mxPushNull();
532
+ fxParseJSONToken(the, theParser);
533
+ break;
534
+ case XS_JSON_TOKEN_INTEGER:
535
+ mxPushInteger(theParser->integer);
536
+ fxParseJSONToken(the, theParser);
537
+ break;
538
+ case XS_JSON_TOKEN_NUMBER:
539
+ mxPushNumber(theParser->number);
540
+ fxParseJSONToken(the, theParser);
541
+ break;
542
+ case XS_JSON_TOKEN_STRING:
543
+ mxPushString(theParser->string->value.string);
544
+ fxParseJSONToken(the, theParser);
545
+ break;
546
+ case XS_JSON_TOKEN_LEFT_BRACE:
547
+ fxParseJSONObject(the, theParser);
548
+ break;
549
+ case XS_JSON_TOKEN_LEFT_BRACKET:
550
+ fxParseJSONArray(the, theParser);
551
+ break;
552
+ default:
553
+ mxPushUndefined();
554
+ mxSyntaxError("%ld: invalid value", theParser->line);
555
+ break;
556
+ }
557
+ }
558
+
559
+ void fxReviveJSON(txMachine* the, txSlot* reviver)
560
+ {
561
+ txSlot* reference = the->stack;
562
+ mxCheckCStack();
563
+ if (mxIsReference(reference)) {
564
+ txSlot* instance = reference->value.reference;
565
+ if (fxIsArray(the, instance)) {
566
+ txIndex length, index;
567
+ mxPushSlot(reference);
568
+ mxGetID(mxID(_length));
569
+ length = (txIndex)fxToLength(the, the->stack);
570
+ mxPop();
571
+ index = 0;
572
+ while (index < length) {
573
+ mxPushSlot(reference);
574
+ mxPushSlot(reviver);
575
+ mxCall();
576
+ mxPushUndefined();
577
+ fxKeyAt(the, 0, index, the->stack);
578
+ mxPushSlot(reference);
579
+ mxGetAll(0, index);
580
+ fxReviveJSON(the, reviver);
581
+ if (mxIsUndefined(the->stack)) {
582
+ mxBehaviorDeleteProperty(the, reference->value.reference, 0, index);
583
+ }
584
+ else {
585
+ mxBehaviorDefineOwnProperty(the, reference->value.reference, 0, index, the->stack, XS_GET_ONLY);
586
+ }
587
+ mxPop();
588
+ index++;
589
+ }
590
+ }
591
+ else {
592
+ txSlot* at = fxNewInstance(the);
593
+ mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG, at);
594
+ while ((at = at->next)) {
595
+ mxPushSlot(reference);
596
+ mxPushSlot(reviver);
597
+ mxCall();
598
+ mxPushUndefined();
599
+ fxKeyAt(the, at->value.at.id, at->value.at.index, the->stack);
600
+ mxPushSlot(reference);
601
+ mxGetAll(at->value.at.id, at->value.at.index);
602
+ fxReviveJSON(the, reviver);
603
+ if (mxIsUndefined(the->stack)) {
604
+ mxBehaviorDeleteProperty(the, reference->value.reference, at->value.at.id, at->value.at.index);
605
+ }
606
+ else {
607
+ mxBehaviorDefineOwnProperty(the, reference->value.reference, at->value.at.id, at->value.at.index, the->stack, XS_GET_ONLY);
608
+ }
609
+ mxPop();
610
+ }
611
+ mxPop();
612
+ }
613
+ }
614
+ mxRunCount(2);
615
+ }
616
+
617
+ void fx_JSON_stringify(txMachine* the)
618
+ {
619
+ volatile txJSONStringifier aStringifier = {0};
620
+ mxTry(the) {
621
+ fxStringifyJSON(the, (txJSONStringifier*)&aStringifier);
622
+ if (aStringifier.offset) {
623
+ fxStringifyJSONChars(the, (txJSONStringifier*)&aStringifier, "\0", 1);
624
+ mxResult->value.string = (txString)fxNewChunk(the, aStringifier.offset);
625
+ c_memcpy(mxResult->value.string, aStringifier.buffer, aStringifier.offset);
626
+ mxResult->kind = XS_STRING_KIND;
627
+ }
628
+ c_free(aStringifier.buffer);
629
+ }
630
+ mxCatch(the) {
631
+ if (aStringifier.buffer)
632
+ c_free(aStringifier.buffer);
633
+ fxJump(the);
634
+ }
635
+ }
636
+
637
+ void fxStringifyJSON(txMachine* the, txJSONStringifier* theStringifier)
638
+ {
639
+ txSlot* aSlot;
640
+ txInteger aFlag;
641
+ txSlot* instance;
642
+
643
+ aSlot = fxGetInstance(the, mxThis);
644
+ theStringifier->offset = 0;
645
+ theStringifier->size = 1024;
646
+ theStringifier->buffer = c_malloc(1024);
647
+ if (!theStringifier->buffer)
648
+ fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
649
+
650
+ if (mxArgc > 1) {
651
+ aSlot = mxArgv(1);
652
+ if (mxIsReference(aSlot)) {
653
+ if (fxIsCallable(the, aSlot))
654
+ theStringifier->replacer = mxArgv(1);
655
+ else if (fxIsArray(the, fxGetInstance(the, aSlot)))
656
+ theStringifier->keys = fxToJSONKeys(the, aSlot);
657
+ }
658
+ }
659
+ if (mxArgc > 2) {
660
+ aSlot = mxArgv(2);
661
+ if (mxIsReference(aSlot)) {
662
+ txSlot* instance = fxGetInstance(the, aSlot);
663
+ if (mxIsNumber(instance)) {
664
+ fxToNumber(the, aSlot);
665
+ }
666
+ else if (mxIsString(instance)) {
667
+ fxToString(the, aSlot);
668
+ }
669
+ }
670
+ if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) {
671
+ txInteger aCount = fxToInteger(the, aSlot), anIndex;
672
+ if (aCount < 0)
673
+ aCount = 0;
674
+ else if (aCount > 10)
675
+ aCount = 10;
676
+ for (anIndex = 0; anIndex < aCount; anIndex++)
677
+ theStringifier->indent[anIndex] = ' ';
678
+ theStringifier->indentLength = aCount;
679
+ }
680
+ else if (mxIsStringPrimitive(aSlot)) {
681
+ txInteger aCount = fxUnicodeLength(aSlot->value.string);
682
+ if (aCount > 10) {
683
+ aCount = fxUnicodeToUTF8Offset(aSlot->value.string, 10);
684
+ }
685
+ else {
686
+ aCount = (txInteger)c_strlen(aSlot->value.string);
687
+ }
688
+ c_memcpy(theStringifier->indent, aSlot->value.string, aCount);
689
+ theStringifier->indent[aCount] = 0;
690
+ theStringifier->indentLength = aCount;
691
+ }
692
+ }
693
+
694
+ theStringifier->stack = the->stack;
695
+ mxPush(mxObjectPrototype);
696
+ instance = fxNewObjectInstance(the);
697
+ aFlag = 0;
698
+ if (mxArgc > 0)
699
+ mxPushSlot(mxArgv(0));
700
+ else
701
+ mxPushUndefined();
702
+ fxNextSlotProperty(the, instance, the->stack, mxID(__empty_string_), XS_NO_FLAG);
703
+ mxPush(mxEmptyString);
704
+ fxStringifyJSONProperty(the, theStringifier, &aFlag);
705
+ mxPop();
706
+ }
707
+
708
+ void fxStringifyJSONCharacter(txMachine* the, txJSONStringifier* theStringifier, txInteger character)
709
+ {
710
+ txSize size = mxStringByteLength(character);
711
+ if ((theStringifier->offset + size) >= theStringifier->size) {
712
+ char* aBuffer;
713
+ theStringifier->size += ((size / 1024) + 1) * 1024;
714
+ aBuffer = c_realloc(theStringifier->buffer, theStringifier->size);
715
+ if (!aBuffer)
716
+ mxUnknownError("out of memory");
717
+ theStringifier->buffer = aBuffer;
718
+ }
719
+ mxStringByteEncode(theStringifier->buffer + theStringifier->offset, character);
720
+ theStringifier->offset += size;
721
+ }
722
+
723
+ void fxStringifyJSONChars(txMachine* the, txJSONStringifier* theStringifier, char* s, txSize theSize)
724
+ {
725
+ //fprintf(stderr, "%s", s);
726
+ if ((theStringifier->offset + theSize) >= theStringifier->size) {
727
+ char* aBuffer;
728
+ theStringifier->size += ((theSize / 1024) + 1) * 1024;
729
+ aBuffer = c_realloc(theStringifier->buffer, theStringifier->size);
730
+ if (!aBuffer)
731
+ mxUnknownError("out of memory");
732
+ theStringifier->buffer = aBuffer;
733
+ }
734
+ c_memcpy(theStringifier->buffer + theStringifier->offset, s, theSize);
735
+ theStringifier->offset += theSize;
736
+ }
737
+
738
+ void fxStringifyJSONIndent(txMachine* the, txJSONStringifier* theStringifier)
739
+ {
740
+ txInteger aLevel;
741
+ if (theStringifier->indent[0]) {
742
+ fxStringifyJSONChars(the, theStringifier, "\n", 1);
743
+ for (aLevel = 0; aLevel < theStringifier->level; aLevel++)
744
+ fxStringifyJSONChars(the, theStringifier, theStringifier->indent, theStringifier->indentLength);
745
+ }
746
+ }
747
+
748
+ void fxStringifyJSONInteger(txMachine* the, txJSONStringifier* theStringifier, txInteger theInteger)
749
+ {
750
+ char aBuffer[256];
751
+ fxIntegerToString(the->dtoa, theInteger, aBuffer, sizeof(aBuffer));
752
+ fxStringifyJSONChars(the, theStringifier, aBuffer, (txSize)c_strlen(aBuffer));
753
+ }
754
+
755
+ void fxStringifyJSONName(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag)
756
+ {
757
+ txSlot* aSlot = the->stack;
758
+ if (*theFlag & 1) {
759
+ fxStringifyJSONChars(the, theStringifier, ",", 1);
760
+ fxStringifyJSONIndent(the, theStringifier);
761
+ }
762
+ else
763
+ *theFlag |= 1;
764
+ if (*theFlag & 2) {
765
+ if (aSlot->kind == XS_INTEGER_KIND) {
766
+ fxStringifyJSONChars(the, theStringifier, "\"", 1);
767
+ fxStringifyJSONInteger(the, theStringifier, aSlot->value.integer);
768
+ fxStringifyJSONChars(the, theStringifier, "\"", 1);
769
+ }
770
+ else
771
+ fxStringifyJSONString(the, theStringifier, aSlot->value.string);
772
+ fxStringifyJSONChars(the, theStringifier, ":", 1);
773
+ if (theStringifier->indent[0])
774
+ fxStringifyJSONChars(the, theStringifier, " ", 1);
775
+ }
776
+ mxPop(); // POP KEY
777
+ }
778
+
779
+ void fxStringifyJSONNumber(txMachine* the, txJSONStringifier* theStringifier, txNumber theNumber)
780
+ {
781
+ int fpclass = c_fpclassify(theNumber);
782
+ if ((fpclass != FP_NAN) && (fpclass != FP_INFINITE)) {
783
+ char aBuffer[256];
784
+ fxNumberToString(the->dtoa, theNumber, aBuffer, sizeof(aBuffer), 0, 0);
785
+ fxStringifyJSONChars(the, theStringifier, aBuffer, (txSize)c_strlen(aBuffer));
786
+ }
787
+ else
788
+ fxStringifyJSONChars(the, theStringifier, "null", 4);
789
+ }
790
+
791
+ void fxStringifyJSONProperty(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag)
792
+ {
793
+ txSlot* aWrapper = the->stack + 2;
794
+ txSlot* aValue = the->stack + 1;
795
+ txSlot* aKey = the->stack;
796
+ txSlot* anInstance;
797
+ txSlot* aSlot;
798
+ txInteger aFlag;
799
+ txIndex aLength, anIndex;
800
+
801
+ mxCheckCStack();
802
+ if (mxIsReference(aValue) || mxIsBigInt(aValue)) {
803
+ /* THIS */
804
+ mxPushSlot(aValue);
805
+ /* FUNCTION */
806
+ mxDub();
807
+ mxGetID(mxID(_toJSON));
808
+ if (mxIsReference(the->stack) && mxIsFunction(the->stack->value.reference)) {
809
+ mxCall();
810
+ mxPushSlot(aKey);
811
+ fxToString(the, the->stack);
812
+ mxRunCount(1);
813
+ mxPullSlot(aValue);
814
+ }
815
+ the->stack = aKey;
816
+ }
817
+ if (theStringifier->replacer) {
818
+ /* THIS */
819
+ mxPushSlot(aWrapper);
820
+ /* FUNCTION */
821
+ mxPushSlot(theStringifier->replacer);
822
+ mxCall();
823
+ /* ARGUMENTS */
824
+ mxPushSlot(aKey);
825
+ fxToString(the, the->stack);
826
+ mxPushSlot(aValue);
827
+ /* COUNT */
828
+ mxRunCount(2);
829
+ mxPullSlot(aValue);
830
+ the->stack = aKey;
831
+ }
832
+ if (mxIsReference(aValue)) {
833
+ mxPushSlot(aValue);
834
+ anInstance = fxToInstance(the, the->stack);
835
+ if (anInstance->flag & XS_LEVEL_FLAG)
836
+ mxTypeError("cyclic value");
837
+ the->stack = aKey;
838
+ aSlot = anInstance->next;
839
+ if (aSlot && (aSlot->flag & XS_INTERNAL_FLAG)) {
840
+ if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) {
841
+ fxToNumber(the, aValue);
842
+ }
843
+ else if (mxIsStringPrimitive(aSlot)) {
844
+ fxToString(the, aValue);
845
+ }
846
+ else if ((aSlot->kind == XS_BOOLEAN_KIND) || (aSlot->kind == XS_BIGINT_KIND) || (aSlot->kind == XS_BIGINT_X_KIND)) {
847
+ aValue->kind = aSlot->kind;
848
+ aValue->value = aSlot->value;
849
+ }
850
+ }
851
+ }
852
+ if (aValue->kind == XS_NULL_KIND) {
853
+ fxStringifyJSONName(the, theStringifier, theFlag);
854
+ fxStringifyJSONChars(the, theStringifier, "null", 4);
855
+ }
856
+ else if (aValue->kind == XS_BOOLEAN_KIND) {
857
+ fxStringifyJSONName(the, theStringifier, theFlag);
858
+ if (aValue->value.boolean)
859
+ fxStringifyJSONChars(the, theStringifier, "true", 4);
860
+ else
861
+ fxStringifyJSONChars(the, theStringifier, "false", 5);
862
+ }
863
+ else if (aValue->kind == XS_INTEGER_KIND) {
864
+ fxStringifyJSONName(the, theStringifier, theFlag);
865
+ fxStringifyJSONInteger(the, theStringifier, aValue->value.integer);
866
+ }
867
+ else if (aValue->kind == XS_NUMBER_KIND) {
868
+ fxStringifyJSONName(the, theStringifier, theFlag);
869
+ fxStringifyJSONNumber(the, theStringifier, aValue->value.number);
870
+ }
871
+ else if ((aValue->kind == XS_STRING_KIND) || (aValue->kind == XS_STRING_X_KIND)) {
872
+ fxStringifyJSONName(the, theStringifier, theFlag);
873
+ fxStringifyJSONString(the, theStringifier, aValue->value.string);
874
+ }
875
+ else if ((aValue->kind == XS_BIGINT_KIND) || (aValue->kind == XS_BIGINT_X_KIND)) {
876
+ mxTypeError("stringify bigint");
877
+ }
878
+ else if ((aValue->kind == XS_REFERENCE_KIND) && !fxIsCallable(the, aValue)) {
879
+ mxTry(the) {
880
+ fxStringifyJSONName(the, theStringifier, theFlag);
881
+ if (anInstance->flag & XS_MARK_FLAG)
882
+ mxTypeError("read only value");
883
+ anInstance->flag |= XS_LEVEL_FLAG;
884
+ if (fxIsArray(the, anInstance)) {
885
+ fxStringifyJSONChars(the, theStringifier, "[", 1);
886
+ theStringifier->level++;
887
+ fxStringifyJSONIndent(the, theStringifier);
888
+ aFlag = 4;
889
+ mxPushReference(anInstance);
890
+ mxGetID(mxID(_length));
891
+ aLength = fxToInteger(the, the->stack);
892
+ mxPop();
893
+ for (anIndex = 0; anIndex < aLength; anIndex++) {
894
+ mxPushReference(anInstance);
895
+ mxGetIndex(anIndex);
896
+ mxPushInteger(anIndex);
897
+ fxStringifyJSONProperty(the, theStringifier, &aFlag);
898
+ }
899
+ theStringifier->level--;
900
+ fxStringifyJSONIndent(the, theStringifier);
901
+ fxStringifyJSONChars(the, theStringifier, "]", 1);
902
+ }
903
+ else {
904
+ fxStringifyJSONChars(the, theStringifier, "{", 1);
905
+ theStringifier->level++;
906
+ fxStringifyJSONIndent(the, theStringifier);
907
+ aFlag = 2;
908
+ {
909
+ txSlot* at;
910
+ txSlot* property;
911
+ if (theStringifier->keys) {
912
+ mxPushUndefined();
913
+ at = theStringifier->keys->value.reference;
914
+ }
915
+ else {
916
+ at = fxNewInstance(the);
917
+ mxBehaviorOwnKeys(the, anInstance, XS_EACH_NAME_FLAG, at);
918
+ }
919
+ mxPushUndefined();
920
+ property = the->stack;
921
+ mxPushReference(anInstance);
922
+ while ((at = at->next)) {
923
+ if (mxBehaviorGetOwnProperty(the, anInstance, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) {
924
+ mxPushReference(anInstance);
925
+ mxGetAll(at->value.at.id, at->value.at.index);
926
+ if (at->value.at.id) {
927
+ txSlot* key = fxGetKey(the, at->value.at.id);
928
+ if (mxGetKeySlotKind(key) == XS_KEY_KIND)
929
+ mxPushString(key->value.key.string);
930
+ else
931
+ mxPushStringX(key->value.key.string);
932
+ }
933
+ else
934
+ mxPushInteger((txInteger)at->value.at.index);
935
+ fxStringifyJSONProperty(the, theStringifier, &aFlag);
936
+ }
937
+ }
938
+ mxPop();
939
+ mxPop();
940
+ mxPop();
941
+ }
942
+ theStringifier->level--;
943
+ fxStringifyJSONIndent(the, theStringifier);
944
+ fxStringifyJSONChars(the, theStringifier, "}", 1);
945
+ }
946
+ anInstance->flag &= ~XS_LEVEL_FLAG;
947
+ }
948
+ mxCatch(the) {
949
+ if (anInstance->flag & XS_LEVEL_FLAG)
950
+ anInstance->flag &= ~XS_LEVEL_FLAG;
951
+ fxJump(the);
952
+ }
953
+ }
954
+ else {
955
+ if (*theFlag & 4) {
956
+ if (*theFlag & 1) {
957
+ fxStringifyJSONChars(the, theStringifier, ",", 1);
958
+ fxStringifyJSONIndent(the, theStringifier);
959
+ }
960
+ else
961
+ *theFlag |= 1;
962
+ fxStringifyJSONChars(the, theStringifier, "null", 4);
963
+ }
964
+ }
965
+ mxPop(); // POP VALUE
966
+ }
967
+
968
+ void fxStringifyJSONString(txMachine* the, txJSONStringifier* theStringifier, txString theString)
969
+ {
970
+ fxStringifyJSONChars(the, theStringifier, "\"", 1);
971
+ for (;;) {
972
+ txInteger character;
973
+ theString = mxStringByteDecode(theString, &character);
974
+ if (character == C_EOF)
975
+ break;
976
+ if (character < 8)
977
+ fxStringifyJSONUnicodeEscape(the, theStringifier, character);
978
+ else if (character == 8)
979
+ fxStringifyJSONChars(the, theStringifier, "\\b", 2);
980
+ else if (character == 9)
981
+ fxStringifyJSONChars(the, theStringifier, "\\t", 2);
982
+ else if (character == 10)
983
+ fxStringifyJSONChars(the, theStringifier, "\\n", 2);
984
+ else if (character == 11)
985
+ fxStringifyJSONUnicodeEscape(the, theStringifier, character);
986
+ else if (character == 12)
987
+ fxStringifyJSONChars(the, theStringifier, "\\f", 2);
988
+ else if (character == 13)
989
+ fxStringifyJSONChars(the, theStringifier, "\\r", 2);
990
+ else if (character < 32)
991
+ fxStringifyJSONUnicodeEscape(the, theStringifier, character);
992
+ else if (character < 34)
993
+ fxStringifyJSONCharacter(the, theStringifier, character);
994
+ else if (character == 34)
995
+ fxStringifyJSONChars(the, theStringifier, "\\\"", 2);
996
+ else if (character < 92)
997
+ fxStringifyJSONCharacter(the, theStringifier, character);
998
+ else if (character == 92)
999
+ fxStringifyJSONChars(the, theStringifier, "\\\\", 2);
1000
+ else if (character < 127)
1001
+ fxStringifyJSONCharacter(the, theStringifier, character);
1002
+ else if ((0xD800 <= character) && (character <= 0xDFFF))
1003
+ fxStringifyJSONUnicodeEscape(the, theStringifier, character);
1004
+ else
1005
+ fxStringifyJSONCharacter(the, theStringifier, character);
1006
+ }
1007
+ fxStringifyJSONChars(the, theStringifier, "\"", 1);
1008
+ }
1009
+
1010
+ void fxStringifyJSONUnicodeEscape(txMachine* the, txJSONStringifier* theStringifier, txInteger character)
1011
+ {
1012
+ char buffer[16];
1013
+ txString p = buffer;
1014
+ *p++ = '\\';
1015
+ *p++ = 'u';
1016
+ p = fxStringifyUnicodeEscape(p, character, '\\');
1017
+ fxStringifyJSONChars(the, theStringifier, buffer, mxPtrDiff(p - buffer));
1018
+ }
1019
+
1020
+ txSlot* fxToJSONKeys(txMachine* the, txSlot* reference)
1021
+ {
1022
+ txSlot* list = fxNewInstance(the);
1023
+ txSlot* item = list;
1024
+ txSlot* slot;
1025
+ txIndex length, i;
1026
+ mxPushSlot(reference);
1027
+ mxGetID(mxID(_length));
1028
+ length = (txIndex)fxToLength(the, the->stack);
1029
+ mxPop();
1030
+ i = 0;
1031
+ while (i < length) {
1032
+ txBoolean flag = 0;
1033
+ txID id = XS_NO_ID;
1034
+ txIndex index = 0;
1035
+ mxPushSlot(reference);
1036
+ mxGetIndex(i);
1037
+ slot = the->stack;
1038
+ again:
1039
+ if ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND)) {
1040
+ if (fxStringToIndex(the->dtoa, slot->value.string, &index))
1041
+ flag = 1;
1042
+ else {
1043
+ if (slot->kind == XS_STRING_X_KIND)
1044
+ id = fxNewNameX(the, slot->value.string);
1045
+ else
1046
+ id = fxNewName(the, slot);
1047
+ flag = 1;
1048
+ }
1049
+ }
1050
+ else if (slot->kind == XS_INTEGER_KIND) {
1051
+ if (fxIntegerToIndex(the->dtoa, slot->value.integer, &index))
1052
+ flag = 1;
1053
+ else {
1054
+ fxToString(the, slot);
1055
+ goto again;
1056
+ }
1057
+ }
1058
+ else if (slot->kind == XS_NUMBER_KIND){
1059
+ if (fxNumberToIndex(the->dtoa, slot->value.number, &index))
1060
+ flag = 1;
1061
+ else {
1062
+ fxToString(the, slot);
1063
+ goto again;
1064
+ }
1065
+ }
1066
+ else if (slot->kind == XS_REFERENCE_KIND) {
1067
+ txSlot* instance = slot->value.reference;
1068
+ if (mxIsNumber(instance) || mxIsString(instance)) {
1069
+ fxToString(the, slot);
1070
+ goto again;
1071
+ }
1072
+ }
1073
+ if (flag) {
1074
+ txSlot* already = list->next;
1075
+ while (already) {
1076
+ if ((already->value.at.id == id) && (already->value.at.index == index))
1077
+ break;
1078
+ already = already->next;
1079
+ }
1080
+ if (!already) {
1081
+ item = item->next = fxNewSlot(the);
1082
+ item->value.at.id = id;
1083
+ item->value.at.index = index;
1084
+ item->kind = XS_AT_KIND;
1085
+ }
1086
+ }
1087
+ mxPop();
1088
+ i++;
1089
+ }
1090
+ return the->stack;
1091
+ }