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