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