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