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