/src/moddable/xs/sources/xsJSON.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016-2017 Moddable Tech, Inc. |
3 | | * |
4 | | * This file is part of the Moddable SDK Runtime. |
5 | | * |
6 | | * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This file incorporates work covered by the following copyright and |
20 | | * permission notice: |
21 | | * |
22 | | * Copyright (C) 2010-2016 Marvell International Ltd. |
23 | | * Copyright (C) 2002-2010 Kinoma, Inc. |
24 | | * |
25 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
26 | | * you may not use this file except in compliance with the License. |
27 | | * You may obtain a copy of the License at |
28 | | * |
29 | | * http://www.apache.org/licenses/LICENSE-2.0 |
30 | | * |
31 | | * Unless required by applicable law or agreed to in writing, software |
32 | | * distributed under the License is distributed on an "AS IS" BASIS, |
33 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 | | * See the License for the specific language governing permissions and |
35 | | * limitations under the License. |
36 | | */ |
37 | | |
38 | | #include "xsAll.h" |
39 | | |
40 | | enum { |
41 | | XS_NO_JSON_TOKEN, |
42 | | XS_JSON_TOKEN_COLON, |
43 | | XS_JSON_TOKEN_COMMA, |
44 | | XS_JSON_TOKEN_EOF, |
45 | | XS_JSON_TOKEN_FALSE, |
46 | | XS_JSON_TOKEN_INTEGER, |
47 | | XS_JSON_TOKEN_LEFT_BRACE, |
48 | | XS_JSON_TOKEN_LEFT_BRACKET, |
49 | | XS_JSON_TOKEN_NULL, |
50 | | XS_JSON_TOKEN_NUMBER, |
51 | | XS_JSON_TOKEN_RIGHT_BRACE, |
52 | | XS_JSON_TOKEN_RIGHT_BRACKET, |
53 | | XS_JSON_TOKEN_STRING, |
54 | | XS_JSON_TOKEN_TRUE, |
55 | | }; |
56 | | |
57 | | typedef struct { |
58 | | txSlot* slot; |
59 | | txSize offset; |
60 | | txInteger integer; |
61 | | txNumber number; |
62 | | txSlot* string; |
63 | | txInteger token; |
64 | | txSlot* keys; |
65 | | txInteger line; |
66 | | txBoolean sourceFlag; |
67 | | txInteger sourceOffset; |
68 | | txInteger sourceSize; |
69 | | } txJSONParser; |
70 | | |
71 | | typedef struct { |
72 | | txString buffer; |
73 | | char indent[64]; |
74 | | txInteger indentLength; |
75 | | txInteger level; |
76 | | txSize offset; |
77 | | txSize size; |
78 | | txSlot* replacer; |
79 | | txSlot* keys; |
80 | | txSlot* stack; |
81 | | } txJSONStringifier; |
82 | | |
83 | | static void fxParseJSON(txMachine* the, txJSONParser* theParser); |
84 | | static void fxParseJSONArray(txMachine* the, txJSONParser* theParser); |
85 | | static void fxParseJSONObject(txMachine* the, txJSONParser* theParser); |
86 | | static void fxParseJSONToken(txMachine* the, txJSONParser* theParser); |
87 | | static void fxParseJSONValue(txMachine* the, txJSONParser* theParser); |
88 | | static void fxReviveJSON(txMachine* the, txJSONParser* theParser, txSlot* reviver); |
89 | | |
90 | | static void fxStringifyJSON(txMachine* the, txJSONStringifier* theStringifier); |
91 | | static void fxStringifyJSONCharacter(txMachine* the, txJSONStringifier* theStringifier, txInteger character); |
92 | | static void fxStringifyJSONChars(txMachine* the, txJSONStringifier* theStringifier, char* s, txSize theSize); |
93 | | static void fxStringifyJSONIndent(txMachine* the, txJSONStringifier* theStringifier); |
94 | | static void fxStringifyJSONInteger(txMachine* the, txJSONStringifier* theStringifier, txInteger theInteger); |
95 | | static void fxStringifyJSONName(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag); |
96 | | static void fxStringifyJSONNumber(txMachine* the, txJSONStringifier* theStringifier, txNumber theNumber); |
97 | | static void fxStringifyJSONProperty(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag); |
98 | | static void fxStringifyJSONString(txMachine* the, txJSONStringifier* theStringifier, txString theString); |
99 | | static void fxStringifyJSONUnicodeEscape(txMachine* the, txJSONStringifier* theStringifier, txInteger character); |
100 | | |
101 | | static txSlot* fxToJSONKeys(txMachine* the, txSlot* reference); |
102 | | |
103 | | void fxBuildJSON(txMachine* the) |
104 | 24.8k | { |
105 | 24.8k | txSlot* slot; |
106 | 24.8k | mxPush(mxObjectPrototype); |
107 | 24.8k | slot = fxLastProperty(the, fxNewObjectInstance(the)); |
108 | 24.8k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_parse), 2, mxID(_parse), XS_DONT_ENUM_FLAG); |
109 | 24.8k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_stringify), 3, mxID(_stringify), XS_DONT_ENUM_FLAG); |
110 | 24.8k | #if mxECMAScript2026 |
111 | 24.8k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_isRawJSON), 1, mxID(_isRawJSON), XS_DONT_ENUM_FLAG); |
112 | 24.8k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_rawJSON), 1, mxID(_rawJSON), XS_DONT_ENUM_FLAG); |
113 | 24.8k | #endif |
114 | 24.8k | slot = fxNextStringXProperty(the, slot, "JSON", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
115 | 24.8k | mxPull(mxJSONObject); |
116 | 24.8k | } |
117 | | |
118 | | #define mxIsRawJSON(THE_SLOT) \ |
119 | 44 | ((THE_SLOT) && ((THE_SLOT)->next) && ((THE_SLOT)->next->flag & XS_INTERNAL_FLAG) && ((THE_SLOT)->next->kind == XS_RAW_JSON_KIND)) |
120 | | |
121 | | void fx_JSON_isRawJSON(txMachine* the) |
122 | 66 | { |
123 | 66 | if (mxArgc < 1) |
124 | 0 | mxTypeError("no text"); |
125 | 66 | txSlot* slot = mxArgv(0); |
126 | 66 | mxResult->kind = XS_BOOLEAN_KIND; |
127 | 66 | mxResult->value.boolean = (mxIsReference(slot) && mxIsRawJSON(slot->value.reference)) ? 1 : 0; |
128 | 66 | } |
129 | | |
130 | | void fx_JSON_parse(txMachine* the) |
131 | 81.3k | { |
132 | 81.3k | volatile txJSONParser aParser = {0}; |
133 | 81.3k | if (mxArgc < 1) |
134 | 1.54k | mxSyntaxError("no buffer"); |
135 | 79.8k | fxToString(the, mxArgv(0)); |
136 | 79.8k | aParser.slot = mxArgv(0); |
137 | 79.8k | aParser.offset = 0; |
138 | 79.8k | mxPush(mxEmptyString); |
139 | 79.8k | aParser.string = the->stack; |
140 | 79.8k | aParser.line = 1; |
141 | 79.8k | if ((mxArgc > 1) && mxIsReference(mxArgv(1))) { |
142 | 900 | if (fxIsArray(the, mxArgv(1)->value.reference)) |
143 | 15 | aParser.keys = fxToJSONKeys(the, mxArgv(1)); |
144 | 885 | else if (mxIsCallable(mxArgv(1)->value.reference)) |
145 | 773 | aParser.sourceFlag = 1; |
146 | 900 | } |
147 | 79.8k | fxParseJSON(the, (txJSONParser*)&aParser); |
148 | 79.8k | if (aParser.sourceFlag) { |
149 | 765 | txSlot* valueReference = the->stack + 1; |
150 | 765 | txSlot* sourceReference = the->stack; |
151 | 765 | txSlot* instance; |
152 | 765 | txID id; |
153 | 765 | mxPush(mxObjectPrototype); |
154 | 765 | instance = fxNewObjectInstance(the); |
155 | 765 | id = fxID(the, ""); |
156 | 765 | mxBehaviorDefineOwnProperty(the, instance, id, 0, valueReference, XS_GET_ONLY); |
157 | 765 | mxPushSlot(mxArgv(1)); |
158 | 765 | mxCall(); |
159 | 765 | mxPushUndefined(); |
160 | 765 | fxKeyAt(the, id, 0, the->stack); |
161 | 765 | mxPushSlot(valueReference); |
162 | 765 | mxPushSlot(sourceReference); |
163 | 765 | fxReviveJSON(the, (txJSONParser*)&aParser, mxArgv(1)); |
164 | 765 | } |
165 | 79.8k | mxPullSlot(mxResult); |
166 | 79.8k | } |
167 | | |
168 | | void fxParseJSON(txMachine* the, txJSONParser* theParser) |
169 | 79.8k | { |
170 | 79.8k | fxParseJSONToken(the, theParser); |
171 | 79.8k | fxParseJSONValue(the, theParser); |
172 | 79.8k | if (theParser->token != XS_JSON_TOKEN_EOF) |
173 | 3.71k | mxSyntaxError("%ld: missing EOF", theParser->line); |
174 | 79.8k | } |
175 | | |
176 | | void fxParseJSONArray(txMachine* the, txJSONParser* theParser) |
177 | 12.0k | { |
178 | 12.0k | txSlot* sourceArray = C_NULL; |
179 | 12.0k | txSlot* sourceItem = C_NULL; |
180 | 12.0k | txSlot* valueArray; |
181 | 12.0k | txSlot* valueItem; |
182 | 12.0k | txIndex length; |
183 | | |
184 | 12.0k | mxCheckCStack(); |
185 | 12.0k | fxParseJSONToken(the, theParser); |
186 | 12.0k | mxPush(mxArrayPrototype); |
187 | 12.0k | valueArray = fxNewArrayInstance(the); |
188 | 12.0k | valueItem = fxLastProperty(the, valueArray); |
189 | 12.0k | if (theParser->sourceFlag) { |
190 | 136 | mxPush(mxArrayPrototype); |
191 | 136 | sourceArray = fxNewArrayInstance(the); |
192 | 136 | sourceItem = fxLastProperty(the, sourceArray); |
193 | 136 | } |
194 | 12.0k | length = 0; |
195 | 21.2k | for (;;) { |
196 | 21.2k | if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACKET) |
197 | 1.08k | break; |
198 | 20.1k | if (length) { |
199 | 9.16k | if (theParser->token == XS_JSON_TOKEN_COMMA) |
200 | 7.07k | fxParseJSONToken(the, theParser); |
201 | 2.08k | else |
202 | 2.08k | mxSyntaxError("%ld: missing ,", theParser->line); |
203 | 9.16k | } |
204 | 18.0k | fxParseJSONValue(the, theParser); |
205 | 18.0k | length++; |
206 | 18.0k | if (sourceItem) { |
207 | 265 | sourceItem->next = fxNewSlot(the); |
208 | 265 | sourceItem = sourceItem->next; |
209 | 265 | sourceItem->kind = the->stack->kind; |
210 | 265 | sourceItem->value = the->stack->value; |
211 | 265 | mxPop(); |
212 | 265 | } |
213 | 18.0k | valueItem->next = fxNewSlot(the); |
214 | 18.0k | valueItem = valueItem->next; |
215 | 18.0k | valueItem->kind = the->stack->kind; |
216 | 18.0k | valueItem->value = the->stack->value; |
217 | 18.0k | mxPop(); |
218 | 18.0k | } |
219 | 9.98k | valueArray->next->value.array.length = length; |
220 | 9.98k | fxCacheArray(the, valueArray); |
221 | 9.98k | if (sourceItem) { |
222 | 133 | sourceArray->next->value.array.length = length; |
223 | 133 | fxCacheArray(the, sourceArray); |
224 | 133 | } |
225 | 9.98k | fxParseJSONToken(the, theParser); |
226 | 9.98k | } |
227 | | |
228 | | void fxParseJSONToken(txMachine* the, txJSONParser* theParser) |
229 | 146k | { |
230 | 146k | txInteger character; |
231 | 146k | txBoolean escaped; |
232 | 146k | txNumber number; |
233 | 146k | txSize offset; |
234 | 146k | txSize size; |
235 | 146k | txString p, s; |
236 | | |
237 | 146k | theParser->integer = 0; |
238 | 146k | theParser->number = 0; |
239 | 146k | theParser->string->value.string = mxEmptyString.value.string; |
240 | 146k | theParser->string->kind = mxEmptyString.kind; |
241 | 146k | theParser->token = XS_NO_JSON_TOKEN; |
242 | 146k | p = theParser->slot->value.string + theParser->offset; |
243 | 320k | while (theParser->token == XS_NO_JSON_TOKEN) { |
244 | 230k | switch (*p) { |
245 | 12.8k | case 0: |
246 | 12.8k | theParser->token = XS_JSON_TOKEN_EOF; |
247 | 12.8k | break; |
248 | 29.5k | case 10: |
249 | 29.5k | p++; |
250 | 29.5k | theParser->line++; |
251 | 29.5k | break; |
252 | 550 | case 13: |
253 | 550 | p++; |
254 | 550 | theParser->line++; |
255 | 550 | if (*p == 10) |
256 | 1 | p++; |
257 | 550 | break; |
258 | 3.53k | case '\t': |
259 | 53.4k | case ' ': |
260 | 53.4k | p++; |
261 | 53.4k | break; |
262 | 41.8k | case '-': |
263 | 44.8k | case '0': |
264 | 45.9k | case '1': |
265 | 52.2k | case '2': |
266 | 52.6k | case '3': |
267 | 53.7k | case '4': |
268 | 67.4k | case '5': |
269 | 67.5k | case '6': |
270 | 69.5k | case '7': |
271 | 70.4k | case '8': |
272 | 74.1k | case '9': |
273 | 74.1k | s = p; |
274 | 74.1k | if (*p == '-') |
275 | 41.8k | p++; |
276 | 74.1k | if (('0' <= *p) && (*p <= '9')) { |
277 | 38.2k | if (*p == '0') { |
278 | 6.88k | p++; |
279 | 6.88k | } |
280 | 31.3k | else { |
281 | 31.3k | p++; |
282 | 270k | while (('0' <= *p) && (*p <= '9')) |
283 | 238k | p++; |
284 | 31.3k | } |
285 | 38.2k | if (*p == '.') { |
286 | 5.10k | p++; |
287 | 5.10k | if (('0' <= *p) && (*p <= '9')) { |
288 | 2.01k | p++; |
289 | 24.9k | while (('0' <= *p) && (*p <= '9')) |
290 | 22.9k | p++; |
291 | 2.01k | } |
292 | 3.09k | else |
293 | 3.09k | goto error; |
294 | 5.10k | } |
295 | 35.1k | if ((*p == 'e') || (*p == 'E')) { |
296 | 2.61k | p++; |
297 | 2.61k | if ((*p == '+') || (*p == '-')) |
298 | 1.73k | p++; |
299 | 2.61k | if (('0' <= *p) && (*p <= '9')) { |
300 | 771 | p++; |
301 | 1.85k | while (('0' <= *p) && (*p <= '9')) |
302 | 1.08k | p++; |
303 | 771 | } |
304 | 1.83k | else |
305 | 1.83k | goto error; |
306 | 2.61k | } |
307 | 35.1k | } |
308 | 35.8k | else |
309 | 35.8k | goto error; |
310 | 33.2k | size = mxPtrDiff(p - s); |
311 | 33.2k | if (theParser->sourceFlag) { |
312 | 272 | theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string); |
313 | 272 | theParser->sourceSize = size; |
314 | 272 | } |
315 | 33.2k | if ((size_t)(size + 1) > sizeof(the->nameBuffer)) |
316 | 0 | mxSyntaxError("%ld: number overflow", theParser->line); |
317 | 33.2k | c_memcpy(the->nameBuffer, s, size); |
318 | 33.2k | the->nameBuffer[size] = 0; |
319 | 33.2k | theParser->number = fxStringToNumber(the, the->nameBuffer, 0); |
320 | 33.2k | theParser->integer = (txInteger)theParser->number; |
321 | 33.2k | number = theParser->integer; |
322 | 33.2k | if ((theParser->number == number) && (theParser->number != -0)) |
323 | 20.9k | theParser->token = XS_JSON_TOKEN_INTEGER; |
324 | 12.3k | else |
325 | 12.3k | theParser->token = XS_JSON_TOKEN_NUMBER; |
326 | 33.2k | break; |
327 | 7.45k | case ',': |
328 | 7.45k | p++; |
329 | 7.45k | theParser->token = XS_JSON_TOKEN_COMMA; |
330 | 7.45k | break; |
331 | 713 | case ':': |
332 | 713 | p++; |
333 | 713 | theParser->token = XS_JSON_TOKEN_COLON; |
334 | 713 | break; |
335 | 12.0k | case '[': |
336 | 12.0k | p++; |
337 | 12.0k | theParser->token = XS_JSON_TOKEN_LEFT_BRACKET; |
338 | 12.0k | break; |
339 | 1.50k | case ']': |
340 | 1.50k | p++; |
341 | 1.50k | theParser->token = XS_JSON_TOKEN_RIGHT_BRACKET; |
342 | 1.50k | break; |
343 | 7.66k | case '{': |
344 | 7.66k | p++; |
345 | 7.66k | theParser->token = XS_JSON_TOKEN_LEFT_BRACE; |
346 | 7.66k | break; |
347 | 63 | case '}': |
348 | 63 | p++; |
349 | 63 | theParser->token = XS_JSON_TOKEN_RIGHT_BRACE; |
350 | 63 | break; |
351 | 16.7k | case '"': |
352 | 16.7k | s = p; |
353 | 16.7k | p++; |
354 | 16.7k | escaped = 0; |
355 | 16.7k | offset = mxPtrDiff(p - theParser->slot->value.string); |
356 | 16.7k | size = 0; |
357 | 197k | for (;;) { |
358 | 197k | p = mxStringByteDecode(p, &character); |
359 | 197k | if (character < 32) { |
360 | 2.08k | goto error; |
361 | 2.08k | } |
362 | 195k | else if (character == '"') { |
363 | 12.0k | break; |
364 | 12.0k | } |
365 | 183k | else if (character == '\\') { |
366 | 11.6k | escaped = 1; |
367 | 11.6k | switch (*p) { |
368 | 818 | case '"': |
369 | 3.19k | case '/': |
370 | 3.49k | case '\\': |
371 | 5.34k | case 'b': |
372 | 6.20k | case 'f': |
373 | 7.00k | case 'n': |
374 | 7.24k | case 'r': |
375 | 7.45k | case 't': |
376 | 7.45k | p++; |
377 | 7.45k | size++; |
378 | 7.45k | break; |
379 | 3.22k | case 'u': |
380 | 3.22k | p++; |
381 | 3.22k | if (fxParseUnicodeEscape(&p, &character, 0, '\\')) |
382 | 1.63k | size += mxStringByteLength(character); |
383 | 1.59k | else |
384 | 1.59k | goto error; |
385 | 1.63k | break; |
386 | 1.63k | default: |
387 | 983 | goto error; |
388 | 11.6k | } |
389 | 11.6k | } |
390 | 171k | else { |
391 | 171k | size += mxStringByteLength(character); |
392 | 171k | } |
393 | 197k | } |
394 | 12.0k | if (theParser->sourceFlag) { |
395 | 39 | theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string); |
396 | 39 | theParser->sourceSize = mxPtrDiff(p - s); |
397 | 39 | } |
398 | 12.0k | s = theParser->string->value.string = fxNewChunk(the, size + 1); |
399 | 12.0k | theParser->string->kind = XS_STRING_KIND; |
400 | 12.0k | p = theParser->slot->value.string + offset; |
401 | 12.0k | if (escaped) { |
402 | 147k | for (;;) { |
403 | 147k | if (*p == '"') { |
404 | 8.77k | p++; |
405 | 8.77k | *s = 0; |
406 | 8.77k | break; |
407 | 8.77k | } |
408 | 139k | else if (*p == '\\') { |
409 | 9.07k | p++; |
410 | 9.07k | switch (*p) { |
411 | 809 | case '"': |
412 | 3.18k | case '/': |
413 | 3.48k | case '\\': |
414 | 3.48k | *s++ = *p++; |
415 | 3.48k | break; |
416 | 1.84k | case 'b': |
417 | 1.84k | p++; |
418 | 1.84k | *s++ = '\b'; |
419 | 1.84k | break; |
420 | 860 | case 'f': |
421 | 860 | p++; |
422 | 860 | *s++ = '\f'; |
423 | 860 | break; |
424 | 809 | case 'n': |
425 | 809 | p++; |
426 | 809 | *s++ = '\n'; |
427 | 809 | break; |
428 | 240 | case 'r': |
429 | 240 | p++; |
430 | 240 | *s++ = '\r'; |
431 | 240 | break; |
432 | 206 | case 't': |
433 | 206 | p++; |
434 | 206 | *s++ = '\t'; |
435 | 206 | break; |
436 | 1.63k | case 'u': |
437 | 1.63k | p++; |
438 | 1.63k | fxParseUnicodeEscape(&p, &character, 0, '\\'); |
439 | 1.63k | s = mxStringByteEncode(s, character); |
440 | 1.63k | break; |
441 | 9.07k | } |
442 | 9.07k | } |
443 | 130k | else { |
444 | 130k | *s++ = *p++; |
445 | 130k | } |
446 | 147k | } |
447 | 8.77k | } |
448 | 3.30k | else { |
449 | 3.30k | c_memcpy(s, p, size); |
450 | 3.30k | p += size + 1; |
451 | 3.30k | s[size] = 0; |
452 | 3.30k | } |
453 | 12.0k | theParser->token = XS_JSON_TOKEN_STRING; |
454 | 12.0k | break; |
455 | 2.97k | case 'f': |
456 | 2.97k | s = p; |
457 | 2.97k | p++; |
458 | 2.97k | if (*p != 'a') goto error; |
459 | 2.51k | p++; |
460 | 2.51k | if (*p != 'l') goto error; |
461 | 2.46k | p++; |
462 | 2.46k | if (*p != 's') goto error; |
463 | 1.51k | p++; |
464 | 1.51k | if (*p != 'e') goto error; |
465 | 442 | p++; |
466 | 442 | if (theParser->sourceFlag) { |
467 | 193 | theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string); |
468 | 193 | theParser->sourceSize = mxPtrDiff(p - s); |
469 | 193 | } |
470 | 442 | theParser->token = XS_JSON_TOKEN_FALSE; |
471 | 442 | break; |
472 | 4.21k | case 'n': |
473 | 4.21k | s = p; |
474 | 4.21k | p++; |
475 | 4.21k | if (*p != 'u') goto error; |
476 | 3.14k | p++; |
477 | 3.14k | if (*p != 'l') goto error; |
478 | 1.90k | p++; |
479 | 1.90k | if (*p != 'l') goto error; |
480 | 1.07k | p++; |
481 | 1.07k | if (theParser->sourceFlag) { |
482 | 253 | theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string); |
483 | 253 | theParser->sourceSize = mxPtrDiff(p - s); |
484 | 253 | } |
485 | 1.07k | theParser->token = XS_JSON_TOKEN_NULL; |
486 | 1.07k | break; |
487 | 4.02k | case 't': |
488 | 4.02k | s = p; |
489 | 4.02k | p++; |
490 | 4.02k | if (*p != 'r') goto error; |
491 | 3.06k | p++; |
492 | 3.06k | if (*p != 'u') goto error; |
493 | 2.79k | p++; |
494 | 2.79k | if (*p != 'e') goto error; |
495 | 763 | p++; |
496 | 763 | if (theParser->sourceFlag) { |
497 | 208 | theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string); |
498 | 208 | theParser->sourceSize = mxPtrDiff(p - s); |
499 | 208 | } |
500 | 763 | theParser->token = XS_JSON_TOKEN_TRUE; |
501 | 763 | break; |
502 | 2.34k | default: |
503 | 56.7k | error: |
504 | 56.7k | mxSyntaxError("%ld: invalid character", theParser->line); |
505 | 0 | break; |
506 | 230k | } |
507 | 230k | } |
508 | 89.9k | theParser->offset = mxPtrDiff(p - theParser->slot->value.string); |
509 | 89.9k | } |
510 | | |
511 | | void fxParseJSONObject(txMachine* the, txJSONParser* theParser) |
512 | 7.66k | { |
513 | 7.66k | txSlot* sourceObject = NULL; |
514 | 7.66k | txSlot* valueObject; |
515 | 7.66k | txBoolean comma = 0; |
516 | 7.66k | txSlot* at; |
517 | 7.66k | txIndex index; |
518 | 7.66k | txID id; |
519 | 7.66k | txSlot* property; |
520 | | |
521 | 7.66k | mxCheckCStack(); |
522 | 7.66k | fxParseJSONToken(the, theParser); |
523 | 7.66k | mxPush(mxObjectPrototype); |
524 | 7.66k | valueObject = fxNewObjectInstance(the); |
525 | 7.66k | if (theParser->sourceFlag) { |
526 | 14 | mxPush(mxObjectPrototype); |
527 | 14 | sourceObject = fxNewObjectInstance(the); |
528 | 14 | } |
529 | 7.66k | for (;;) { |
530 | 4.14k | if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACE) |
531 | 61 | break; |
532 | 4.08k | if (comma) { |
533 | 218 | if (theParser->token == XS_JSON_TOKEN_COMMA) |
534 | 83 | fxParseJSONToken(the, theParser); |
535 | 135 | else |
536 | 135 | mxSyntaxError("%ld: missing ,", theParser->line); |
537 | 218 | } |
538 | 3.94k | if (theParser->token != XS_JSON_TOKEN_STRING) |
539 | 1.04k | mxSyntaxError("%ld: missing name", theParser->line); |
540 | 2.90k | mxPushString(theParser->string->value.string); |
541 | 2.90k | at = the->stack; |
542 | 2.90k | index = 0; |
543 | 2.90k | if (theParser->keys) { |
544 | 31 | at->kind = XS_UNDEFINED_KIND; |
545 | 31 | if (fxStringToIndex(the, at->value.string, &index)) |
546 | 5 | id = 0; |
547 | 26 | else |
548 | 26 | id = fxFindName(the, at->value.string); |
549 | 31 | if (id != XS_NO_ID) { |
550 | 11 | txSlot* item = theParser->keys->value.reference->next; |
551 | 33 | while (item) { |
552 | 26 | if ((item->value.at.id == id) && (item->value.at.index == index)) { |
553 | 4 | at->value.at.id = id; |
554 | 4 | at->value.at.index = index; |
555 | 4 | at->kind = XS_AT_KIND; |
556 | 4 | break; |
557 | 4 | } |
558 | 22 | item = item->next; |
559 | 22 | } |
560 | 11 | } |
561 | 31 | } |
562 | 2.87k | else { |
563 | 2.87k | if (fxStringToIndex(the, at->value.string, &index)) |
564 | 7 | id = 0; |
565 | 2.86k | else |
566 | 2.86k | id = fxNewName(the, at); |
567 | 2.87k | at->value.at.id = id; |
568 | 2.87k | at->value.at.index = index; |
569 | 2.87k | at->kind = XS_AT_KIND; |
570 | 2.87k | } |
571 | 2.90k | fxParseJSONToken(the, theParser); |
572 | 2.90k | if (theParser->token != XS_JSON_TOKEN_COLON) |
573 | 2.58k | mxSyntaxError("%ld: missing :", theParser->line); |
574 | 320 | fxParseJSONToken(the, theParser); |
575 | 320 | fxParseJSONValue(the, theParser); |
576 | 320 | if (theParser->sourceFlag) { |
577 | 36 | property = mxBehaviorSetProperty(the, sourceObject, at->value.at.id, at->value.at.index, XS_OWN); |
578 | 36 | property->kind = the->stack->kind; |
579 | 36 | property->value = the->stack->value; |
580 | 36 | mxPop(); // source |
581 | 36 | } |
582 | 320 | if ((at->kind == XS_AT_KIND) && (the->stack->kind != XS_UNDEFINED_KIND)) { |
583 | 224 | property = mxBehaviorSetProperty(the, valueObject, at->value.at.id, at->value.at.index, XS_OWN); |
584 | 224 | property->kind = the->stack->kind; |
585 | 224 | property->value = the->stack->value; |
586 | 224 | } |
587 | 320 | mxPop(); // value |
588 | 320 | mxPop(); // at |
589 | 320 | comma = 1; |
590 | 320 | } |
591 | 3.90k | fxParseJSONToken(the, theParser); |
592 | 3.90k | } |
593 | | |
594 | | void fxParseJSONValue(txMachine* the, txJSONParser* theParser) |
595 | 56.3k | { |
596 | 56.3k | if (theParser->token == XS_JSON_TOKEN_LEFT_BRACE) |
597 | 7.66k | fxParseJSONObject(the, theParser); |
598 | 48.6k | else if (theParser->token == XS_JSON_TOKEN_LEFT_BRACKET) |
599 | 12.0k | fxParseJSONArray(the, theParser); |
600 | 36.5k | else { |
601 | 36.5k | switch (theParser->token) { |
602 | 236 | case XS_JSON_TOKEN_FALSE: |
603 | 236 | mxPushBoolean(0); |
604 | 236 | break; |
605 | 762 | case XS_JSON_TOKEN_TRUE: |
606 | 762 | mxPushBoolean(1); |
607 | 762 | break; |
608 | 285 | case XS_JSON_TOKEN_NULL: |
609 | 285 | mxPushNull(); |
610 | 285 | break; |
611 | 16.3k | case XS_JSON_TOKEN_INTEGER: |
612 | 16.3k | mxPushInteger(theParser->integer); |
613 | 16.3k | break; |
614 | 11.1k | case XS_JSON_TOKEN_NUMBER: |
615 | 11.1k | mxPushNumber(theParser->number); |
616 | 11.1k | break; |
617 | 6.77k | case XS_JSON_TOKEN_STRING: |
618 | 6.77k | mxPushString(theParser->string->value.string); |
619 | 6.77k | break; |
620 | 968 | default: |
621 | 968 | mxPushUndefined(); |
622 | 968 | mxSyntaxError("%ld: invalid value", theParser->line); |
623 | 0 | break; |
624 | 36.5k | } |
625 | 35.5k | if (theParser->sourceFlag) { |
626 | 925 | txSlot* value = the->stack; |
627 | 925 | txSlot* list; |
628 | 925 | txSlot* slot; |
629 | 925 | mxPushList(); |
630 | 925 | list = the->stack; |
631 | 925 | slot = list->value.list.first = fxNewSlot(the); |
632 | 925 | slot->kind = XS_DATA_VIEW_KIND; |
633 | 925 | slot->value.dataView.offset = theParser->sourceOffset; |
634 | 925 | slot->value.dataView.size = theParser->sourceSize; |
635 | 925 | slot = slot->next = list->value.list.last = fxNewSlot(the); |
636 | 925 | slot->kind = value->kind; |
637 | 925 | slot->value = value->value; |
638 | 925 | } |
639 | 35.5k | fxParseJSONToken(the, theParser); |
640 | 35.5k | } |
641 | 56.3k | } |
642 | | |
643 | | void fxReviveJSON(txMachine* the, txJSONParser* theParser, txSlot* reviver) |
644 | 26.2k | { |
645 | 26.2k | txSlot* valueReference = the->stack + 1; |
646 | 26.2k | txSlot* sourceReference = the->stack; |
647 | 26.2k | mxCheckCStack(); |
648 | 26.2k | if (mxIsReference(valueReference)) { |
649 | 25.3k | txSlot* instance = valueReference->value.reference; |
650 | 25.3k | if (fxIsArray(the, instance)) { |
651 | 4.59k | txIndex length, index; |
652 | 4.59k | mxPushSlot(valueReference); |
653 | 4.59k | mxGetID(mxID(_length)); |
654 | 4.59k | length = (txIndex)fxToLength(the, the->stack); |
655 | 4.59k | mxPop(); |
656 | 4.59k | index = 0; |
657 | 9.20k | while (index < length) { |
658 | 4.61k | mxPushSlot(valueReference); |
659 | 4.61k | mxPushSlot(reviver); |
660 | 4.61k | mxCall(); |
661 | 4.61k | mxPushUndefined(); |
662 | 4.61k | fxKeyAt(the, 0, index, the->stack); |
663 | 4.61k | mxPushSlot(valueReference); |
664 | 4.61k | mxGetIndex(index); |
665 | 4.61k | if (mxIsReference(sourceReference)) { |
666 | 259 | mxPushSlot(sourceReference); |
667 | 259 | mxGetIndex(index); |
668 | 259 | } |
669 | 4.35k | else |
670 | 4.35k | mxPushUndefined(); |
671 | 4.61k | fxReviveJSON(the, theParser, reviver); |
672 | 4.61k | if (mxIsUndefined(the->stack)) { |
673 | 11 | mxBehaviorDeleteProperty(the, valueReference->value.reference, 0, index); |
674 | 11 | } |
675 | 4.60k | else { |
676 | 4.60k | mxBehaviorDefineOwnProperty(the, valueReference->value.reference, 0, index, the->stack, XS_GET_ONLY); |
677 | 4.60k | } |
678 | 4.61k | mxPop(); |
679 | 4.61k | index++; |
680 | 4.61k | } |
681 | 4.59k | } |
682 | 20.7k | else { |
683 | 20.7k | txSlot* at = fxNewInstance(the); |
684 | 20.7k | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG, at); |
685 | 41.6k | while ((at = at->next)) { |
686 | 20.8k | mxPushSlot(valueReference); |
687 | 20.8k | mxPushSlot(reviver); |
688 | 20.8k | mxCall(); |
689 | 20.8k | mxPushUndefined(); |
690 | 20.8k | fxKeyAt(the, at->value.at.id, at->value.at.index, the->stack); |
691 | 20.8k | mxPushSlot(valueReference); |
692 | 20.8k | mxGetAll(at->value.at.id, at->value.at.index); |
693 | 20.8k | if (mxIsReference(sourceReference)) { |
694 | 25 | mxPushSlot(sourceReference); |
695 | 25 | mxGetAll(at->value.at.id, at->value.at.index); |
696 | 25 | } |
697 | 20.8k | else |
698 | 20.8k | mxPushUndefined(); |
699 | 20.8k | fxReviveJSON(the, theParser, reviver); |
700 | 20.8k | if (mxIsUndefined(the->stack)) { |
701 | 147 | mxBehaviorDeleteProperty(the, valueReference->value.reference, at->value.at.id, at->value.at.index); |
702 | 147 | } |
703 | 20.7k | else { |
704 | 20.7k | mxBehaviorDefineOwnProperty(the, valueReference->value.reference, at->value.at.id, at->value.at.index, the->stack, XS_GET_ONLY); |
705 | 20.7k | } |
706 | 20.8k | mxPop(); |
707 | 20.8k | } |
708 | 20.7k | mxPop(); |
709 | 20.7k | } |
710 | 25.3k | } |
711 | 26.2k | if ((sourceReference->kind == XS_LIST_KIND) && fxIsSameValue(the, valueReference, sourceReference->value.list.last, 0)) { |
712 | 783 | txSlot* view = sourceReference->value.list.first; |
713 | 783 | txInteger offset = view->value.dataView.offset; |
714 | 783 | txInteger size = view->value.dataView.size; |
715 | 783 | txSlot* instance; |
716 | 783 | txSlot* source; |
717 | 783 | mxPop(); |
718 | 783 | mxPush(mxObjectPrototype); |
719 | 783 | instance = fxNewObjectInstance(the); |
720 | 783 | source = instance->next = fxNewSlot(the); |
721 | 783 | source->value.string = fxNewChunk(the, size + 1); |
722 | 783 | c_memcpy(source->value.string, theParser->slot->value.string + offset, size); |
723 | 783 | source->value.string[size] = 0; |
724 | 783 | source->kind = XS_STRING_KIND; |
725 | 783 | source->ID = mxID(_source); |
726 | 783 | } |
727 | 25.4k | else { |
728 | 25.4k | mxPop(); |
729 | 25.4k | mxPush(mxObjectPrototype); |
730 | 25.4k | fxNewObjectInstance(the); |
731 | 25.4k | } |
732 | 26.2k | mxRunCount(3); |
733 | 26.2k | } |
734 | | |
735 | | void fx_JSON_rawJSON(txMachine* the) |
736 | 109 | { |
737 | 109 | txSlot* slot; |
738 | 109 | txString string; |
739 | 109 | txSize length; |
740 | 109 | txSlot* instance; |
741 | 109 | txSlot* property; |
742 | 109 | volatile txJSONParser aParser = {0}; |
743 | 109 | if (mxArgc > 0) |
744 | 109 | mxPushSlot(mxArgv(0)); |
745 | 0 | else |
746 | 0 | mxPushUndefined(); |
747 | 109 | slot = the->stack; |
748 | 109 | string = fxToString(the, slot); |
749 | 109 | length = (txSize)c_strlen(string); |
750 | 109 | if (length == 0) |
751 | 2 | mxSyntaxError("empty string"); |
752 | 107 | else { |
753 | 107 | char first = string[0]; |
754 | 107 | char last = string[length - 1]; |
755 | 107 | if ((first == 0x09) || (first == 0x0A) || (first == 0x0D) || (first == 0x20) || (last == 0x09) || (last == 0x0A) || (last == 0x0D) || (last == 0x20)) |
756 | 24 | mxSyntaxError("invalid string"); |
757 | 107 | } |
758 | 83 | aParser.slot = slot; |
759 | 83 | aParser.offset = 0; |
760 | 83 | mxPush(mxEmptyString); |
761 | 83 | aParser.string = the->stack; |
762 | 83 | aParser.line = 1; |
763 | 83 | fxParseJSON(the, (txJSONParser*)&aParser); |
764 | 83 | if (mxIsReference(the->stack)) |
765 | 0 | mxSyntaxError("invalid string"); |
766 | 83 | mxPop(); |
767 | 83 | instance = fxNewInstance(the); |
768 | 83 | instance->flag |= XS_EXOTIC_FLAG | XS_DONT_PATCH_FLAG; |
769 | 83 | property = instance->next = fxNewSlot(the); |
770 | 83 | property->flag = XS_INTERNAL_FLAG | XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG; |
771 | 83 | property->kind = XS_RAW_JSON_KIND; |
772 | 83 | property = property->next = fxNewSlot(the); |
773 | 83 | property->ID = mxID(_rawJSON); |
774 | 83 | property->flag = XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG; |
775 | 83 | property->kind = slot->kind; |
776 | 83 | property->value = slot->value; |
777 | 83 | mxPullSlot(mxResult); |
778 | 83 | } |
779 | | |
780 | | void fx_JSON_stringify(txMachine* the) |
781 | 274k | { |
782 | 274k | volatile txJSONStringifier aStringifier = {0}; |
783 | 274k | mxTry(the) { |
784 | 274k | fxStringifyJSON(the, (txJSONStringifier*)&aStringifier); |
785 | 274k | if (aStringifier.offset) { |
786 | 270k | fxStringifyJSONChars(the, (txJSONStringifier*)&aStringifier, "\0", 1); |
787 | 270k | mxResult->value.string = (txString)fxNewChunk(the, aStringifier.offset); |
788 | 270k | c_memcpy(mxResult->value.string, aStringifier.buffer, aStringifier.offset); |
789 | 270k | mxResult->kind = XS_STRING_KIND; |
790 | 270k | } |
791 | 274k | c_free(aStringifier.buffer); |
792 | 274k | } |
793 | 274k | mxCatch(the) { |
794 | 16 | if (aStringifier.buffer) |
795 | 16 | c_free(aStringifier.buffer); |
796 | 16 | fxJump(the); |
797 | 16 | } |
798 | 274k | } |
799 | | |
800 | | void fxStringifyJSON(txMachine* the, txJSONStringifier* theStringifier) |
801 | 274k | { |
802 | 274k | txSlot* aSlot; |
803 | 274k | txInteger aFlag; |
804 | 274k | txSlot* instance; |
805 | | |
806 | 274k | aSlot = fxGetInstance(the, mxThis); |
807 | 274k | theStringifier->offset = 0; |
808 | 274k | theStringifier->size = 1024; |
809 | 274k | theStringifier->buffer = c_malloc(1024); |
810 | 274k | if (!theStringifier->buffer) |
811 | 0 | fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT); |
812 | | |
813 | 274k | if (mxArgc > 1) { |
814 | 3.21k | aSlot = mxArgv(1); |
815 | 3.21k | if (mxIsReference(aSlot)) { |
816 | 3.06k | if (fxIsCallable(the, aSlot)) |
817 | 2.99k | theStringifier->replacer = mxArgv(1); |
818 | 64 | else if (fxIsArray(the, fxGetInstance(the, aSlot))) |
819 | 56 | theStringifier->keys = fxToJSONKeys(the, aSlot); |
820 | 3.06k | } |
821 | 3.21k | } |
822 | 274k | if (mxArgc > 2) { |
823 | 151 | aSlot = mxArgv(2); |
824 | 151 | if (mxIsReference(aSlot)) { |
825 | 53 | txSlot* instance = fxGetInstance(the, aSlot); |
826 | 53 | if (mxIsNumber(instance)) { |
827 | 22 | fxToNumber(the, aSlot); |
828 | 22 | } |
829 | 31 | else if (mxIsString(instance)) { |
830 | 5 | fxToString(the, aSlot); |
831 | 5 | } |
832 | 53 | } |
833 | 151 | if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) { |
834 | 77 | txInteger aCount = fxToInteger(the, aSlot), anIndex; |
835 | 77 | if (aCount < 0) |
836 | 14 | aCount = 0; |
837 | 63 | else if (aCount > 10) |
838 | 19 | aCount = 10; |
839 | 442 | for (anIndex = 0; anIndex < aCount; anIndex++) |
840 | 365 | theStringifier->indent[anIndex] = ' '; |
841 | 77 | theStringifier->indentLength = aCount; |
842 | 77 | } |
843 | 74 | else if (mxIsStringPrimitive(aSlot)) { |
844 | 30 | txInteger aCount = fxUnicodeLength(aSlot->value.string, C_NULL); |
845 | 30 | if (aCount > 10) { |
846 | 8 | aCount = fxUnicodeToUTF8Offset(aSlot->value.string, 10); |
847 | 8 | } |
848 | 22 | else { |
849 | 22 | aCount = (txInteger)c_strlen(aSlot->value.string); |
850 | 22 | } |
851 | 30 | c_memcpy(theStringifier->indent, aSlot->value.string, aCount); |
852 | 30 | theStringifier->indent[aCount] = 0; |
853 | 30 | theStringifier->indentLength = aCount; |
854 | 30 | } |
855 | 151 | } |
856 | | |
857 | 274k | theStringifier->stack = the->stack; |
858 | 274k | mxPush(mxObjectPrototype); |
859 | 274k | instance = fxNewObjectInstance(the); |
860 | 274k | aFlag = 0; |
861 | 274k | if (mxArgc > 0) |
862 | 274k | mxPushSlot(mxArgv(0)); |
863 | 8 | else |
864 | 8 | mxPushUndefined(); |
865 | 274k | fxNextSlotProperty(the, instance, the->stack, mxID(__empty_string_), XS_NO_FLAG); |
866 | 274k | mxPush(mxEmptyString); |
867 | 274k | fxStringifyJSONProperty(the, theStringifier, &aFlag); |
868 | 274k | mxPop(); |
869 | 274k | } |
870 | | |
871 | | void fxStringifyJSONCharacter(txMachine* the, txJSONStringifier* theStringifier, txInteger character) |
872 | 26.2M | { |
873 | 26.2M | txSize size = mxStringByteLength(character); |
874 | 26.2M | if ((theStringifier->offset + size) >= theStringifier->size) { |
875 | 55.8k | char* aBuffer; |
876 | 55.8k | theStringifier->size += ((size / 1024) + 1) * 1024; |
877 | 55.8k | aBuffer = c_realloc(theStringifier->buffer, theStringifier->size); |
878 | 55.8k | if (!aBuffer) |
879 | 0 | fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT); |
880 | 55.8k | theStringifier->buffer = aBuffer; |
881 | 55.8k | } |
882 | 26.2M | mxStringByteEncode(theStringifier->buffer + theStringifier->offset, character); |
883 | 26.2M | theStringifier->offset += size; |
884 | 26.2M | } |
885 | | |
886 | | void fxStringifyJSONChars(txMachine* the, txJSONStringifier* theStringifier, char* s, txSize theSize) |
887 | 209M | { |
888 | | //fprintf(stderr, "%s", s); |
889 | 209M | if ((theStringifier->offset + theSize) >= theStringifier->size) { |
890 | 222k | char* aBuffer; |
891 | 222k | theStringifier->size += ((theSize / 1024) + 1) * 1024; |
892 | 222k | aBuffer = c_realloc(theStringifier->buffer, theStringifier->size); |
893 | 222k | if (!aBuffer) |
894 | 0 | fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT); |
895 | 222k | theStringifier->buffer = aBuffer; |
896 | 222k | } |
897 | 209M | c_memcpy(theStringifier->buffer + theStringifier->offset, s, theSize); |
898 | 209M | theStringifier->offset += theSize; |
899 | 209M | } |
900 | | |
901 | | void fxStringifyJSONIndent(txMachine* the, txJSONStringifier* theStringifier) |
902 | 29.2M | { |
903 | 29.2M | txInteger aLevel; |
904 | 29.2M | if (theStringifier->indent[0]) { |
905 | 1.53k | fxStringifyJSONChars(the, theStringifier, "\n", 1); |
906 | 5.33k | for (aLevel = 0; aLevel < theStringifier->level; aLevel++) |
907 | 3.79k | fxStringifyJSONChars(the, theStringifier, theStringifier->indent, theStringifier->indentLength); |
908 | 1.53k | } |
909 | 29.2M | } |
910 | | |
911 | | void fxStringifyJSONInteger(txMachine* the, txJSONStringifier* theStringifier, txInteger theInteger) |
912 | 28.8M | { |
913 | 28.8M | char aBuffer[256]; |
914 | 28.8M | fxIntegerToString(the, theInteger, aBuffer, sizeof(aBuffer)); |
915 | 28.8M | fxStringifyJSONChars(the, theStringifier, aBuffer, (txSize)c_strlen(aBuffer)); |
916 | 28.8M | } |
917 | | |
918 | | void fxStringifyJSONName(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag) |
919 | 29.3M | { |
920 | 29.3M | txSlot* aSlot = the->stack; |
921 | 29.3M | if (*theFlag & 1) { |
922 | 28.8M | fxStringifyJSONChars(the, theStringifier, ",", 1); |
923 | 28.8M | fxStringifyJSONIndent(the, theStringifier); |
924 | 28.8M | } |
925 | 489k | else |
926 | 489k | *theFlag |= 1; |
927 | 29.3M | if (*theFlag & 2) { |
928 | 29.0M | if (aSlot->kind == XS_INTEGER_KIND) { |
929 | 28.8M | fxStringifyJSONChars(the, theStringifier, "\"", 1); |
930 | 28.8M | fxStringifyJSONInteger(the, theStringifier, aSlot->value.integer); |
931 | 28.8M | fxStringifyJSONChars(the, theStringifier, "\"", 1); |
932 | 28.8M | } |
933 | 233k | else |
934 | 233k | fxStringifyJSONString(the, theStringifier, aSlot->value.string); |
935 | 29.0M | fxStringifyJSONChars(the, theStringifier, ":", 1); |
936 | 29.0M | if (theStringifier->indent[0]) |
937 | 370 | fxStringifyJSONChars(the, theStringifier, " ", 1); |
938 | 29.0M | } |
939 | 29.3M | mxPop(); // POP KEY |
940 | 29.3M | } |
941 | | |
942 | | void fxStringifyJSONNumber(txMachine* the, txJSONStringifier* theStringifier, txNumber theNumber) |
943 | 173k | { |
944 | 173k | int fpclass = c_fpclassify(theNumber); |
945 | 173k | if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) { |
946 | 20.6k | char aBuffer[256]; |
947 | 20.6k | fxNumberToString(the, theNumber, aBuffer, sizeof(aBuffer), 0, 0); |
948 | 20.6k | fxStringifyJSONChars(the, theStringifier, aBuffer, (txSize)c_strlen(aBuffer)); |
949 | 20.6k | } |
950 | 152k | else |
951 | 152k | fxStringifyJSONChars(the, theStringifier, "null", 4); |
952 | 173k | } |
953 | | |
954 | | void fxStringifyJSONProperty(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag) |
955 | 29.4M | { |
956 | 29.4M | txSlot* aWrapper = the->stack + 2; |
957 | 29.4M | txSlot* aValue = the->stack + 1; |
958 | 29.4M | txSlot* aKey = the->stack; |
959 | 29.4M | txSlot* anInstance; |
960 | 29.4M | txSlot* aSlot; |
961 | 29.4M | txInteger aFlag; |
962 | 29.4M | txIndex aLength, anIndex; |
963 | | |
964 | 29.4M | mxCheckCStack(); |
965 | 29.4M | if (mxIsReference(aValue) || mxIsBigInt(aValue)) { |
966 | | /* THIS */ |
967 | 200k | mxPushSlot(aValue); |
968 | | /* FUNCTION */ |
969 | 200k | mxDub(); |
970 | 200k | mxGetID(mxID(_toJSON)); |
971 | 200k | if (mxIsReference(the->stack) && mxIsFunction(the->stack->value.reference)) { |
972 | 6.12k | mxCall(); |
973 | 6.12k | mxPushSlot(aKey); |
974 | 6.12k | fxToString(the, the->stack); |
975 | 6.12k | mxRunCount(1); |
976 | 6.12k | mxPullSlot(aValue); |
977 | 6.12k | } |
978 | 200k | the->stack = aKey; |
979 | 200k | } |
980 | 29.4M | if (theStringifier->replacer) { |
981 | | /* THIS */ |
982 | 49.0k | mxPushSlot(aWrapper); |
983 | | /* FUNCTION */ |
984 | 49.0k | mxPushSlot(theStringifier->replacer); |
985 | 49.0k | mxCall(); |
986 | | /* ARGUMENTS */ |
987 | 49.0k | mxPushSlot(aKey); |
988 | 49.0k | fxToString(the, the->stack); |
989 | 49.0k | mxPushSlot(aValue); |
990 | | /* COUNT */ |
991 | 49.0k | mxRunCount(2); |
992 | 49.0k | mxPullSlot(aValue); |
993 | 49.0k | the->stack = aKey; |
994 | 49.0k | } |
995 | 29.4M | if (mxIsReference(aValue)) { |
996 | 240k | mxPushSlot(aValue); |
997 | 240k | anInstance = fxToInstance(the, the->stack); |
998 | 240k | if (anInstance->flag & XS_LEVEL_FLAG) |
999 | 7 | mxTypeError("cyclic value"); |
1000 | 240k | the->stack = aKey; |
1001 | 240k | aSlot = anInstance->next; |
1002 | 240k | if (aSlot && (aSlot->flag & XS_INTERNAL_FLAG)) { |
1003 | 154k | if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) { |
1004 | 3 | fxToNumber(the, aValue); |
1005 | 3 | } |
1006 | 154k | else if (mxIsStringPrimitive(aSlot)) { |
1007 | 3 | fxToString(the, aValue); |
1008 | 3 | } |
1009 | 154k | else if ((aSlot->kind == XS_BOOLEAN_KIND) || (aSlot->kind == XS_BIGINT_KIND) || (aSlot->kind == XS_BIGINT_X_KIND)) { |
1010 | 8 | aValue->kind = aSlot->kind; |
1011 | 8 | aValue->value = aSlot->value; |
1012 | 8 | } |
1013 | 154k | else if (aSlot->kind == XS_RAW_JSON_KIND) { |
1014 | 39 | mxPushSlot(aValue); |
1015 | 39 | mxGetID(mxID(_rawJSON)); |
1016 | 39 | aValue->kind = the->stack->kind; |
1017 | 39 | aValue->value = the->stack->value; |
1018 | 39 | the->stack = aKey; |
1019 | 39 | fxStringifyJSONName(the, theStringifier, theFlag); |
1020 | 39 | fxStringifyJSONChars(the, theStringifier, aValue->value.string, (txSize)c_strlen(aValue->value.string)); |
1021 | 39 | mxPop(); // POP VALUE |
1022 | 39 | return; |
1023 | 39 | } |
1024 | 154k | } |
1025 | 240k | } |
1026 | 29.4M | if (aValue->kind == XS_NULL_KIND) { |
1027 | 1.93k | fxStringifyJSONName(the, theStringifier, theFlag); |
1028 | 1.93k | fxStringifyJSONChars(the, theStringifier, "null", 4); |
1029 | 1.93k | } |
1030 | 29.4M | else if (aValue->kind == XS_BOOLEAN_KIND) { |
1031 | 87.9k | fxStringifyJSONName(the, theStringifier, theFlag); |
1032 | 87.9k | if (aValue->value.boolean) |
1033 | 690 | fxStringifyJSONChars(the, theStringifier, "true", 4); |
1034 | 87.2k | else |
1035 | 87.2k | fxStringifyJSONChars(the, theStringifier, "false", 5); |
1036 | 87.9k | } |
1037 | 29.3M | else if (aValue->kind == XS_INTEGER_KIND) { |
1038 | 1.09k | fxStringifyJSONName(the, theStringifier, theFlag); |
1039 | 1.09k | fxStringifyJSONInteger(the, theStringifier, aValue->value.integer); |
1040 | 1.09k | } |
1041 | 29.3M | else if (aValue->kind == XS_NUMBER_KIND) { |
1042 | 173k | fxStringifyJSONName(the, theStringifier, theFlag); |
1043 | 173k | fxStringifyJSONNumber(the, theStringifier, aValue->value.number); |
1044 | 173k | } |
1045 | 29.2M | else if ((aValue->kind == XS_STRING_KIND) || (aValue->kind == XS_STRING_X_KIND)) { |
1046 | 28.8M | fxStringifyJSONName(the, theStringifier, theFlag); |
1047 | 28.8M | fxStringifyJSONString(the, theStringifier, aValue->value.string); |
1048 | 28.8M | } |
1049 | 323k | else if ((aValue->kind == XS_BIGINT_KIND) || (aValue->kind == XS_BIGINT_X_KIND)) { |
1050 | 8 | mxTypeError("stringify bigint"); |
1051 | 8 | } |
1052 | 323k | else if ((aValue->kind == XS_REFERENCE_KIND) && !fxIsCallable(the, aValue)) { |
1053 | 223k | mxTry(the) { |
1054 | 223k | fxStringifyJSONName(the, theStringifier, theFlag); |
1055 | 223k | if (anInstance->flag & XS_MARK_FLAG) |
1056 | 0 | mxTypeError("read only value"); |
1057 | 223k | anInstance->flag |= XS_LEVEL_FLAG; |
1058 | 223k | if (fxIsArray(the, anInstance)) { |
1059 | 51.2k | fxStringifyJSONChars(the, theStringifier, "[", 1); |
1060 | 51.2k | mxPushReference(anInstance); |
1061 | 51.2k | mxGetID(mxID(_length)); |
1062 | 51.2k | aLength = fxToInteger(the, the->stack); |
1063 | 51.2k | if (aLength > 0) { |
1064 | 46.8k | theStringifier->level++; |
1065 | 46.8k | fxStringifyJSONIndent(the, theStringifier); |
1066 | 46.8k | aFlag = 4; |
1067 | 46.8k | mxPop(); |
1068 | 96.8k | for (anIndex = 0; anIndex < aLength; anIndex++) { |
1069 | 50.0k | mxPushReference(anInstance); |
1070 | 50.0k | mxGetIndex(anIndex); |
1071 | 50.0k | mxPushInteger(anIndex); |
1072 | 50.0k | fxStringifyJSONProperty(the, theStringifier, &aFlag); |
1073 | 50.0k | } |
1074 | 46.8k | theStringifier->level--; |
1075 | 46.8k | fxStringifyJSONIndent(the, theStringifier); |
1076 | 46.8k | } |
1077 | 51.2k | fxStringifyJSONChars(the, theStringifier, "]", 1); |
1078 | 51.2k | } |
1079 | 172k | else { |
1080 | 172k | fxStringifyJSONChars(the, theStringifier, "{", 1); |
1081 | 172k | { |
1082 | 172k | txSlot* at; |
1083 | 172k | txSlot* property; |
1084 | 172k | if (theStringifier->keys) { |
1085 | 58 | mxPushUndefined(); |
1086 | 58 | at = theStringifier->keys->value.reference; |
1087 | 58 | } |
1088 | 172k | else { |
1089 | 172k | at = fxNewInstance(the); |
1090 | 172k | mxBehaviorOwnKeys(the, anInstance, XS_EACH_NAME_FLAG, at); |
1091 | 172k | } |
1092 | 172k | if (at->next) { |
1093 | 172k | theStringifier->level++; |
1094 | 172k | fxStringifyJSONIndent(the, theStringifier); |
1095 | 172k | aFlag = 2; |
1096 | 172k | mxPushUndefined(); |
1097 | 172k | property = the->stack; |
1098 | 172k | mxPushReference(anInstance); |
1099 | 29.3M | while ((at = at->next)) { |
1100 | 29.1M | if (mxBehaviorGetOwnProperty(the, anInstance, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
1101 | 29.1M | mxPushReference(anInstance); |
1102 | 29.1M | mxGetAll(at->value.at.id, at->value.at.index); |
1103 | 29.1M | if (at->value.at.id) |
1104 | 327k | fxPushKeyString(the, at->value.at.id, C_NULL); |
1105 | 28.8M | else |
1106 | 28.8M | mxPushInteger((txInteger)at->value.at.index); |
1107 | 29.1M | fxStringifyJSONProperty(the, theStringifier, &aFlag); |
1108 | 29.1M | } |
1109 | 29.1M | } |
1110 | 172k | mxPop(); |
1111 | 172k | mxPop(); |
1112 | 172k | theStringifier->level--; |
1113 | 172k | fxStringifyJSONIndent(the, theStringifier); |
1114 | 172k | } |
1115 | 172k | mxPop(); |
1116 | 172k | } |
1117 | 172k | fxStringifyJSONChars(the, theStringifier, "}", 1); |
1118 | 172k | } |
1119 | 223k | anInstance->flag &= ~XS_LEVEL_FLAG; |
1120 | 223k | } |
1121 | 223k | mxCatch(the) { |
1122 | 17 | if (anInstance->flag & XS_LEVEL_FLAG) |
1123 | 17 | anInstance->flag &= ~XS_LEVEL_FLAG; |
1124 | 17 | fxJump(the); |
1125 | 17 | } |
1126 | 223k | } |
1127 | 99.8k | else { |
1128 | 99.8k | if (*theFlag & 4) { |
1129 | 1.45k | if (*theFlag & 1) { |
1130 | 1.20k | fxStringifyJSONChars(the, theStringifier, ",", 1); |
1131 | 1.20k | fxStringifyJSONIndent(the, theStringifier); |
1132 | 1.20k | } |
1133 | 244 | else |
1134 | 244 | *theFlag |= 1; |
1135 | 1.45k | fxStringifyJSONChars(the, theStringifier, "null", 4); |
1136 | 1.45k | } |
1137 | 99.8k | } |
1138 | 29.4M | mxPop(); // POP VALUE |
1139 | 29.4M | } |
1140 | | |
1141 | | void fxStringifyJSONString(txMachine* the, txJSONStringifier* theStringifier, txString theString) |
1142 | 29.1M | { |
1143 | 29.1M | fxStringifyJSONChars(the, theStringifier, "\"", 1); |
1144 | 60.8M | for (;;) { |
1145 | 60.8M | txInteger character; |
1146 | 60.8M | theString = mxStringByteDecode(theString, &character); |
1147 | 60.8M | if (character == C_EOF) |
1148 | 29.1M | break; |
1149 | 31.7M | if (character < 8) |
1150 | 2.81M | fxStringifyJSONUnicodeEscape(the, theStringifier, character); |
1151 | 28.9M | else if (character == 8) |
1152 | 16.7k | fxStringifyJSONChars(the, theStringifier, "\\b", 2); |
1153 | 28.9M | else if (character == 9) |
1154 | 12.9k | fxStringifyJSONChars(the, theStringifier, "\\t", 2); |
1155 | 28.9M | else if (character == 10) |
1156 | 41.2k | fxStringifyJSONChars(the, theStringifier, "\\n", 2); |
1157 | 28.8M | else if (character == 11) |
1158 | 71.3k | fxStringifyJSONUnicodeEscape(the, theStringifier, character); |
1159 | 28.8M | else if (character == 12) |
1160 | 152k | fxStringifyJSONChars(the, theStringifier, "\\f", 2); |
1161 | 28.6M | else if (character == 13) |
1162 | 9.78k | fxStringifyJSONChars(the, theStringifier, "\\r", 2); |
1163 | 28.6M | else if (character < 32) |
1164 | 574k | fxStringifyJSONUnicodeEscape(the, theStringifier, character); |
1165 | 28.0M | else if (character < 34) |
1166 | 264k | fxStringifyJSONCharacter(the, theStringifier, character); |
1167 | 27.8M | else if (character == 34) |
1168 | 675k | fxStringifyJSONChars(the, theStringifier, "\\\"", 2); |
1169 | 27.1M | else if (character < 92) |
1170 | 3.34M | fxStringifyJSONCharacter(the, theStringifier, character); |
1171 | 23.7M | else if (character == 92) |
1172 | 1.02M | fxStringifyJSONChars(the, theStringifier, "\\\\", 2); |
1173 | 22.7M | else if (character < 127) |
1174 | 4.01M | fxStringifyJSONCharacter(the, theStringifier, character); |
1175 | 18.7M | else if ((0xD800 <= character) && (character <= 0xDFFF)) |
1176 | 89.5k | fxStringifyJSONUnicodeEscape(the, theStringifier, character); |
1177 | 18.6M | else |
1178 | 18.6M | fxStringifyJSONCharacter(the, theStringifier, character); |
1179 | 31.7M | } |
1180 | 29.1M | fxStringifyJSONChars(the, theStringifier, "\"", 1); |
1181 | 29.1M | } |
1182 | | |
1183 | | void fxStringifyJSONUnicodeEscape(txMachine* the, txJSONStringifier* theStringifier, txInteger character) |
1184 | 3.54M | { |
1185 | 3.54M | char buffer[16]; |
1186 | 3.54M | txString p = buffer; |
1187 | 3.54M | *p++ = '\\'; |
1188 | 3.54M | *p++ = 'u'; |
1189 | 3.54M | p = fxStringifyUnicodeEscape(p, character, '\\'); |
1190 | 3.54M | fxStringifyJSONChars(the, theStringifier, buffer, mxPtrDiff(p - buffer)); |
1191 | 3.54M | } |
1192 | | |
1193 | | txSlot* fxToJSONKeys(txMachine* the, txSlot* reference) |
1194 | 71 | { |
1195 | 71 | txSlot* list = fxNewInstance(the); |
1196 | 71 | txSlot* item = list; |
1197 | 71 | txSlot* slot; |
1198 | 71 | txIndex length, i; |
1199 | 71 | mxPushSlot(reference); |
1200 | 71 | mxGetID(mxID(_length)); |
1201 | 71 | length = (txIndex)fxToLength(the, the->stack); |
1202 | 71 | mxPop(); |
1203 | 71 | i = 0; |
1204 | 365 | while (i < length) { |
1205 | 294 | txBoolean flag = 0; |
1206 | 294 | txID id = XS_NO_ID; |
1207 | 294 | txIndex index = 0; |
1208 | 294 | mxPushSlot(reference); |
1209 | 294 | mxGetIndex(i); |
1210 | 294 | slot = the->stack; |
1211 | 371 | again: |
1212 | 371 | if ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND)) { |
1213 | 129 | if (fxStringToIndex(the, slot->value.string, &index)) |
1214 | 11 | flag = 1; |
1215 | 118 | else { |
1216 | 118 | if (slot->kind == XS_STRING_X_KIND) |
1217 | 0 | id = fxNewNameX(the, slot->value.string); |
1218 | 118 | else |
1219 | 118 | id = fxNewName(the, slot); |
1220 | 118 | flag = 1; |
1221 | 118 | } |
1222 | 129 | } |
1223 | 242 | else if (slot->kind == XS_INTEGER_KIND) { |
1224 | 115 | if (fxIntegerToIndex(the, slot->value.integer, &index)) |
1225 | 88 | flag = 1; |
1226 | 27 | else { |
1227 | 27 | fxToString(the, slot); |
1228 | 27 | goto again; |
1229 | 27 | } |
1230 | 115 | } |
1231 | 127 | else if (slot->kind == XS_NUMBER_KIND){ |
1232 | 62 | if (fxNumberToIndex(the, slot->value.number, &index)) |
1233 | 14 | flag = 1; |
1234 | 48 | else { |
1235 | 48 | fxToString(the, slot); |
1236 | 48 | goto again; |
1237 | 48 | } |
1238 | 62 | } |
1239 | 65 | else if (slot->kind == XS_REFERENCE_KIND) { |
1240 | 6 | txSlot* instance = slot->value.reference; |
1241 | 6 | if (mxIsNumber(instance) || mxIsString(instance)) { |
1242 | 2 | fxToString(the, slot); |
1243 | 2 | goto again; |
1244 | 2 | } |
1245 | 6 | } |
1246 | 294 | if (flag) { |
1247 | 231 | txSlot* already = list->next; |
1248 | 839 | while (already) { |
1249 | 647 | if ((already->value.at.id == id) && (already->value.at.index == index)) |
1250 | 39 | break; |
1251 | 608 | already = already->next; |
1252 | 608 | } |
1253 | 231 | if (!already) { |
1254 | 192 | item = item->next = fxNewSlot(the); |
1255 | 192 | item->value.at.id = id; |
1256 | 192 | item->value.at.index = index; |
1257 | 192 | item->kind = XS_AT_KIND; |
1258 | 192 | } |
1259 | 231 | } |
1260 | 294 | mxPop(); |
1261 | 294 | i++; |
1262 | 294 | } |
1263 | 71 | return the->stack; |
1264 | 71 | } |