Coverage Report

Created: 2026-09-01 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
25.3k
{
105
25.3k
  txSlot* slot;
106
25.3k
  mxPush(mxObjectPrototype);
107
25.3k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
108
25.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_parse), 2, mxID(_parse), XS_DONT_ENUM_FLAG);
109
25.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_stringify), 3, mxID(_stringify), XS_DONT_ENUM_FLAG);
110
25.3k
#if mxECMAScript2026
111
25.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_isRawJSON), 1, mxID(_isRawJSON), XS_DONT_ENUM_FLAG);
112
25.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_JSON_rawJSON), 1, mxID(_rawJSON), XS_DONT_ENUM_FLAG);
113
25.3k
#endif
114
25.3k
  slot = fxNextStringXProperty(the, slot, "JSON", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
115
25.3k
  mxPull(mxJSONObject);
116
25.3k
}
117
118
#define mxIsRawJSON(THE_SLOT) \
119
34
  ((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
60
{
123
60
  if (mxArgc < 1)
124
0
    mxTypeError("no text");
125
60
  txSlot* slot = mxArgv(0);
126
60
  mxResult->kind = XS_BOOLEAN_KIND;
127
60
  mxResult->value.boolean = (mxIsReference(slot) && mxIsRawJSON(slot->value.reference)) ? 1 : 0;
128
60
}
129
130
void fx_JSON_parse(txMachine* the)
131
46.6k
{
132
46.6k
  volatile txJSONParser aParser = {0};
133
46.6k
  if (mxArgc < 1)
134
1.28k
    mxSyntaxError("no buffer");
135
45.3k
  fxToString(the, mxArgv(0));
136
45.3k
  aParser.slot = mxArgv(0);
137
45.3k
  aParser.offset = 0;
138
45.3k
  mxPush(mxEmptyString);
139
45.3k
  aParser.string = the->stack;
140
45.3k
  aParser.line = 1;
141
45.3k
  if ((mxArgc > 1) && mxIsReference(mxArgv(1))) {
142
1.46k
    if (fxIsArray(the, mxArgv(1)->value.reference))
143
17
      aParser.keys = fxToJSONKeys(the, mxArgv(1));
144
1.44k
    else if (mxIsCallable(mxArgv(1)->value.reference))
145
755
      aParser.sourceFlag = 1;
146
1.46k
  }
147
45.3k
  fxParseJSON(the, (txJSONParser*)&aParser);
148
45.3k
  if (aParser.sourceFlag) {
149
748
    txSlot* valueReference = the->stack + 1;
150
748
    txSlot* sourceReference = the->stack;
151
748
    txSlot* instance;
152
748
    txID id;
153
748
    mxPush(mxObjectPrototype);
154
748
    instance = fxNewObjectInstance(the);
155
748
    id = fxID(the, "");
156
748
    mxBehaviorDefineOwnProperty(the, instance, id, 0, valueReference, XS_GET_ONLY);
157
748
    mxPushSlot(mxArgv(1));
158
748
    mxCall();
159
748
    mxPushUndefined();
160
748
    fxKeyAt(the, id, 0, the->stack);
161
748
    mxPushSlot(valueReference);
162
748
    mxPushSlot(sourceReference);
163
748
    fxReviveJSON(the, (txJSONParser*)&aParser, mxArgv(1));
164
748
  }
165
45.3k
  mxPullSlot(mxResult);
166
45.3k
}
167
168
void fxParseJSON(txMachine* the, txJSONParser* theParser)
169
45.4k
{
170
45.4k
  fxParseJSONToken(the, theParser);
171
45.4k
  fxParseJSONValue(the, theParser);
172
45.4k
  if (theParser->token != XS_JSON_TOKEN_EOF)
173
3.74k
    mxSyntaxError("%ld: missing EOF", theParser->line);
174
45.4k
}
175
176
void fxParseJSONArray(txMachine* the, txJSONParser* theParser)
177
11.6k
{
178
11.6k
  txSlot* sourceArray = C_NULL;
179
11.6k
  txSlot* sourceItem = C_NULL;
180
11.6k
  txSlot* valueArray;
181
11.6k
  txSlot* valueItem;
182
11.6k
  txIndex length;
183
184
11.6k
  mxCheckCStack();
185
11.6k
  fxParseJSONToken(the, theParser);
186
11.6k
  mxPush(mxArrayPrototype);
187
11.6k
  valueArray = fxNewArrayInstance(the);
188
11.6k
  valueItem = fxLastProperty(the, valueArray);
189
11.6k
  if (theParser->sourceFlag) {
190
138
    mxPush(mxArrayPrototype);
191
138
    sourceArray = fxNewArrayInstance(the);
192
138
    sourceItem = fxLastProperty(the, sourceArray);
193
138
  }
194
11.6k
  length = 0;
195
19.6k
  for (;;) {
196
19.6k
    if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACKET)
197
1.63k
      break;
198
18.0k
    if (length) {
199
7.90k
      if (theParser->token == XS_JSON_TOKEN_COMMA)
200
6.41k
        fxParseJSONToken(the, theParser);
201
1.49k
      else
202
1.49k
        mxSyntaxError("%ld: missing ,", theParser->line);  
203
7.90k
    }  
204
16.5k
    fxParseJSONValue(the, theParser);
205
16.5k
    length++;
206
16.5k
    if (sourceItem) {
207
269
      sourceItem->next = fxNewSlot(the);
208
269
      sourceItem = sourceItem->next;
209
269
      sourceItem->kind = the->stack->kind;
210
269
      sourceItem->value = the->stack->value;
211
269
      mxPop();
212
269
    }
213
16.5k
    valueItem->next = fxNewSlot(the);
214
16.5k
    valueItem = valueItem->next;
215
16.5k
    valueItem->kind = the->stack->kind;
216
16.5k
    valueItem->value = the->stack->value;
217
16.5k
    mxPop();
218
16.5k
  }
219
10.1k
  valueArray->next->value.array.length = length;
220
10.1k
  fxCacheArray(the, valueArray);
221
10.1k
  if (sourceItem) {
222
135
    sourceArray->next->value.array.length = length;
223
135
    fxCacheArray(the, sourceArray);
224
135
  }
225
10.1k
  fxParseJSONToken(the, theParser);
226
10.1k
}
227
228
void fxParseJSONToken(txMachine* the, txJSONParser* theParser)
229
112k
{
230
112k
  txInteger character;
231
112k
  txBoolean escaped;
232
112k
  txNumber number;
233
112k
  txSize offset;
234
112k
  txSize size;
235
112k
  txString p, s;
236
237
112k
  theParser->integer = 0;
238
112k
  theParser->number = 0;
239
112k
  theParser->string->value.string = mxEmptyString.value.string;
240
112k
  theParser->string->kind = mxEmptyString.kind;
241
112k
  theParser->token = XS_NO_JSON_TOKEN;
242
112k
  p = theParser->slot->value.string + theParser->offset;
243
255k
  while (theParser->token == XS_NO_JSON_TOKEN) {
244
167k
    switch (*p) {
245
10.2k
    case 0:
246
10.2k
      theParser->token = XS_JSON_TOKEN_EOF;
247
10.2k
      break;
248
32.3k
    case 10:
249
32.3k
      p++;
250
32.3k
      theParser->line++;
251
32.3k
      break;
252
533
    case 13:
253
533
      p++;
254
533
      theParser->line++;
255
533
      if (*p == 10)
256
1
        p++;
257
533
      break;
258
4.27k
    case '\t':
259
22.1k
    case ' ':
260
22.1k
      p++;
261
22.1k
      break;
262
7.71k
    case '-':
263
8.99k
    case '0':
264
10.4k
    case '1':
265
16.0k
    case '2':
266
16.3k
    case '3':
267
17.6k
    case '4':
268
31.4k
    case '5':
269
31.6k
    case '6':
270
34.2k
    case '7':
271
35.1k
    case '8':
272
40.3k
    case '9':
273
40.3k
      s = p;
274
40.3k
      if (*p == '-')
275
7.71k
        p++;
276
40.3k
      if (('0' <= *p) && (*p <= '9')) {
277
39.7k
        if (*p == '0') {
278
6.15k
          p++;
279
6.15k
        }
280
33.6k
        else {
281
33.6k
          p++;
282
242k
          while (('0' <= *p) && (*p <= '9'))
283
208k
            p++;
284
33.6k
        }
285
39.7k
        if (*p == '.') {
286
6.32k
          p++;
287
6.32k
          if (('0' <= *p) && (*p <= '9')) {
288
2.48k
            p++;
289
29.8k
            while (('0' <= *p) && (*p <= '9'))
290
27.3k
              p++;
291
2.48k
          }
292
3.83k
          else
293
3.83k
            goto error;
294
6.32k
        }
295
35.9k
        if ((*p == 'e') || (*p == 'E')) {
296
2.50k
          p++;
297
2.50k
          if ((*p == '+') || (*p == '-'))
298
1.54k
            p++;
299
2.50k
          if (('0' <= *p) && (*p <= '9')) {
300
966
            p++;
301
2.24k
            while (('0' <= *p) && (*p <= '9'))
302
1.27k
              p++;
303
966
          }
304
1.54k
          else
305
1.54k
            goto error;
306
2.50k
        }
307
35.9k
      }
308
565
      else
309
565
        goto error;
310
34.4k
      size = mxPtrDiff(p - s);
311
34.4k
      if (theParser->sourceFlag) {
312
282
        theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string);
313
282
        theParser->sourceSize = size;
314
282
      }
315
34.4k
      if ((size_t)(size + 1) > sizeof(the->nameBuffer))
316
0
        mxSyntaxError("%ld: number overflow", theParser->line);
317
34.4k
      c_memcpy(the->nameBuffer, s, size);
318
34.4k
      the->nameBuffer[size] = 0;
319
34.4k
      theParser->number = fxStringToNumber(the, the->nameBuffer, 0);
320
34.4k
      theParser->integer = (txInteger)theParser->number;
321
34.4k
      number = theParser->integer;
322
34.4k
      if ((theParser->number == number) && (theParser->number != -0))
323
24.1k
        theParser->token = XS_JSON_TOKEN_INTEGER;
324
10.2k
      else
325
10.2k
        theParser->token = XS_JSON_TOKEN_NUMBER;
326
34.4k
      break;
327
6.79k
    case ',':
328
6.79k
      p++;
329
6.79k
      theParser->token = XS_JSON_TOKEN_COMMA;
330
6.79k
      break;  
331
921
    case ':':
332
921
      p++;
333
921
      theParser->token = XS_JSON_TOKEN_COLON;
334
921
      break;  
335
11.8k
    case '[':
336
11.8k
      p++;
337
11.8k
      theParser->token = XS_JSON_TOKEN_LEFT_BRACKET;
338
11.8k
      break;  
339
1.90k
    case ']':
340
1.90k
      p++;
341
1.90k
      theParser->token = XS_JSON_TOKEN_RIGHT_BRACKET;
342
1.90k
      break;  
343
8.78k
    case '{':
344
8.78k
      p++;
345
8.78k
      theParser->token = XS_JSON_TOKEN_LEFT_BRACE;
346
8.78k
      break;  
347
195
    case '}':
348
195
      p++;
349
195
      theParser->token = XS_JSON_TOKEN_RIGHT_BRACE;
350
195
      break;  
351
14.6k
    case '"':
352
14.6k
      s = p;
353
14.6k
      p++;
354
14.6k
      escaped = 0;
355
14.6k
      offset = mxPtrDiff(p - theParser->slot->value.string);
356
14.6k
      size = 0;
357
192k
      for (;;) {
358
192k
        p = mxStringByteDecode(p, &character);
359
192k
        if (character < 32) {
360
1.69k
          goto error;
361
1.69k
        }
362
190k
        else if (character == '"') {
363
10.8k
          break;
364
10.8k
        }
365
180k
        else if (character == '\\') {
366
9.27k
          escaped = 1;
367
9.27k
          switch (*p) {
368
1.19k
          case '"':
369
3.29k
          case '/':
370
3.60k
          case '\\':
371
4.62k
          case 'b':
372
4.98k
          case 'f':
373
5.79k
          case 'n':
374
5.87k
          case 'r':
375
6.07k
          case 't':
376
6.07k
            p++;
377
6.07k
            size++;
378
6.07k
            break;
379
2.74k
          case 'u':
380
2.74k
            p++;
381
2.74k
            if (fxParseUnicodeEscape(&p, &character, 0, '\\'))
382
1.14k
              size += mxStringByteLength(character);
383
1.59k
            else
384
1.59k
              goto error;
385
1.14k
            break;
386
1.14k
          default:
387
454
            goto error;
388
9.27k
          }
389
9.27k
        }
390
170k
        else {
391
170k
          size += mxStringByteLength(character);
392
170k
        }
393
192k
      }
394
10.8k
      if (theParser->sourceFlag) {
395
49
        theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string);
396
49
        theParser->sourceSize = mxPtrDiff(p - s);
397
49
      }
398
10.8k
      s = theParser->string->value.string = fxNewChunk(the, size + 1);
399
10.8k
      theParser->string->kind = XS_STRING_KIND;
400
10.8k
      p = theParser->slot->value.string + offset;
401
10.8k
      if (escaped) {
402
120k
        for (;;) {
403
120k
          if (*p == '"') {
404
6.91k
            p++;
405
6.91k
            *s = 0;
406
6.91k
            break;
407
6.91k
          }
408
113k
          else if (*p == '\\') {
409
7.21k
            p++;
410
7.21k
            switch (*p) {
411
1.19k
            case '"':
412
3.28k
            case '/':
413
3.59k
            case '\\':
414
3.59k
              *s++ = *p++;
415
3.59k
              break;
416
1.02k
            case 'b':
417
1.02k
              p++;
418
1.02k
              *s++ = '\b';
419
1.02k
              break;
420
358
            case 'f':
421
358
              p++;
422
358
              *s++ = '\f';
423
358
              break;
424
809
            case 'n':
425
809
              p++;
426
809
              *s++ = '\n';
427
809
              break;
428
78
            case 'r':
429
78
              p++;
430
78
              *s++ = '\r';
431
78
              break;
432
206
            case 't':
433
206
              p++;
434
206
              *s++ = '\t';
435
206
              break;
436
1.14k
            case 'u':
437
1.14k
              p++;
438
1.14k
              fxParseUnicodeEscape(&p, &character, 0, '\\');
439
1.14k
              s = mxStringByteEncode(s, character);
440
1.14k
              break;
441
7.21k
            }
442
7.21k
          }
443
106k
          else {
444
106k
            *s++ = *p++;
445
106k
          }
446
120k
        }
447
6.91k
      }
448
3.97k
      else {
449
3.97k
        c_memcpy(s, p, size);
450
3.97k
        p += size + 1;
451
3.97k
        s[size] = 0;
452
3.97k
      }
453
10.8k
      theParser->token = XS_JSON_TOKEN_STRING;
454
10.8k
      break;
455
2.98k
    case 'f':
456
2.98k
      s = p;
457
2.98k
      p++;
458
2.98k
      if (*p != 'a') goto error;  
459
2.77k
      p++;
460
2.77k
      if (*p != 'l') goto error;  
461
2.72k
      p++;
462
2.72k
      if (*p != 's') goto error;  
463
1.44k
      p++;
464
1.44k
      if (*p != 'e') goto error;  
465
368
      p++;
466
368
      if (theParser->sourceFlag) {
467
193
        theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string);
468
193
        theParser->sourceSize = mxPtrDiff(p - s);
469
193
      }
470
368
      theParser->token = XS_JSON_TOKEN_FALSE;
471
368
      break;
472
5.77k
    case 'n':
473
5.77k
      s = p;
474
5.77k
      p++;
475
5.77k
      if (*p != 'u') goto error;
476
4.07k
      p++;
477
4.07k
      if (*p != 'l') goto error;
478
2.58k
      p++;
479
2.58k
      if (*p != 'l') goto error;
480
825
      p++;
481
825
      if (theParser->sourceFlag) {
482
257
        theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string);
483
257
        theParser->sourceSize = mxPtrDiff(p - s);
484
257
      }
485
825
      theParser->token = XS_JSON_TOKEN_NULL;
486
825
      break;
487
4.81k
    case 't':
488
4.81k
      s = p;
489
4.81k
      p++;
490
4.81k
      if (*p != 'r') goto error;
491
3.29k
      p++;
492
3.29k
      if (*p != 'u') goto error;
493
2.71k
      p++;
494
2.71k
      if (*p != 'e') goto error;
495
685
      p++;
496
685
      if (theParser->sourceFlag) {
497
188
        theParser->sourceOffset = mxPtrDiff(s - theParser->slot->value.string);
498
188
        theParser->sourceSize = mxPtrDiff(p - s);
499
188
      }
500
685
      theParser->token = XS_JSON_TOKEN_TRUE;
501
685
      break;
502
3.24k
    default:
503
24.6k
    error:
504
24.6k
      mxSyntaxError("%ld: invalid character", theParser->line); 
505
0
      break;
506
167k
    }
507
167k
  }
508
87.8k
  theParser->offset = mxPtrDiff(p - theParser->slot->value.string);
509
87.8k
}
510
511
void fxParseJSONObject(txMachine* the, txJSONParser* theParser)
512
8.77k
{
513
8.77k
  txSlot* sourceObject = NULL;
514
8.77k
  txSlot* valueObject;
515
8.77k
  txBoolean comma = 0;
516
8.77k
  txSlot* at;
517
8.77k
  txIndex index;
518
8.77k
  txID id;
519
8.77k
  txSlot* property;
520
521
8.77k
  mxCheckCStack();
522
8.77k
  fxParseJSONToken(the, theParser);
523
8.77k
  mxPush(mxObjectPrototype);
524
8.77k
  valueObject = fxNewObjectInstance(the);
525
8.77k
  if (theParser->sourceFlag) {
526
14
    mxPush(mxObjectPrototype);
527
14
    sourceObject = fxNewObjectInstance(the);
528
14
  }
529
8.77k
  for (;;) {
530
5.01k
    if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACE)
531
60
      break;
532
4.95k
    if (comma) {
533
226
      if (theParser->token == XS_JSON_TOKEN_COMMA)
534
91
        fxParseJSONToken(the, theParser);
535
135
      else
536
135
        mxSyntaxError("%ld: missing ,", theParser->line);  
537
226
    }  
538
4.82k
    if (theParser->token != XS_JSON_TOKEN_STRING)
539
1.24k
      mxSyntaxError("%ld: missing name", theParser->line);
540
3.57k
    mxPushString(theParser->string->value.string);
541
3.57k
    at = the->stack;
542
3.57k
    index = 0;
543
3.57k
    if (theParser->keys) {
544
33
      at->kind = XS_UNDEFINED_KIND;
545
33
      if (fxStringToIndex(the, at->value.string, &index))
546
5
        id = 0;
547
28
      else
548
28
        id = fxFindName(the, at->value.string);
549
33
      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
33
    }
562
3.54k
    else {
563
3.54k
      if (fxStringToIndex(the, at->value.string, &index))
564
17
        id = 0;
565
3.52k
      else
566
3.52k
        id = fxNewName(the, at);
567
3.54k
      at->value.at.id = id;
568
3.54k
            at->value.at.index = index;
569
3.54k
      at->kind = XS_AT_KIND;
570
3.54k
    }
571
3.57k
    fxParseJSONToken(the, theParser);
572
3.57k
    if (theParser->token != XS_JSON_TOKEN_COLON)
573
3.24k
      mxSyntaxError("%ld: missing :", theParser->line);
574
326
    fxParseJSONToken(the, theParser);
575
326
    fxParseJSONValue(the, theParser);
576
326
    if (theParser->sourceFlag) {
577
47
      property = mxBehaviorSetProperty(the, sourceObject, at->value.at.id, at->value.at.index, XS_OWN);
578
47
      property->kind = the->stack->kind;
579
47
      property->value = the->stack->value;
580
47
      mxPop(); // source
581
47
    }
582
326
    if ((at->kind == XS_AT_KIND) && (the->stack->kind != XS_UNDEFINED_KIND)) {
583
233
      property = mxBehaviorSetProperty(the, valueObject, at->value.at.id, at->value.at.index, XS_OWN);
584
233
      property->kind = the->stack->kind;
585
233
      property->value = the->stack->value;
586
233
    }
587
326
    mxPop(); // value
588
326
    mxPop(); // at
589
326
    comma = 1;
590
326
  }
591
4.14k
  fxParseJSONToken(the, theParser);
592
4.14k
}
593
594
void fxParseJSONValue(txMachine* the, txJSONParser* theParser)
595
55.6k
{
596
55.6k
  if (theParser->token == XS_JSON_TOKEN_LEFT_BRACE)
597
8.77k
    fxParseJSONObject(the, theParser);
598
46.8k
  else if (theParser->token == XS_JSON_TOKEN_LEFT_BRACKET)
599
11.6k
    fxParseJSONArray(the, theParser);
600
35.1k
  else {
601
35.1k
    switch (theParser->token) {
602
235
    case XS_JSON_TOKEN_FALSE:
603
235
      mxPushBoolean(0);
604
235
      break;
605
684
    case XS_JSON_TOKEN_TRUE:
606
684
      mxPushBoolean(1);
607
684
      break;
608
284
    case XS_JSON_TOKEN_NULL:
609
284
      mxPushNull();
610
284
      break;
611
18.9k
    case XS_JSON_TOKEN_INTEGER:
612
18.9k
      mxPushInteger(theParser->integer);
613
18.9k
      break;
614
8.96k
    case XS_JSON_TOKEN_NUMBER:
615
8.96k
      mxPushNumber(theParser->number);
616
8.96k
      break;
617
5.39k
    case XS_JSON_TOKEN_STRING:
618
5.39k
      mxPushString(theParser->string->value.string);
619
5.39k
      break;
620
694
    default:
621
694
      mxPushUndefined();
622
694
      mxSyntaxError("%ld: invalid value", theParser->line);
623
0
      break;
624
35.1k
    }
625
34.4k
    if (theParser->sourceFlag) {
626
919
      txSlot* value = the->stack;
627
919
      txSlot* list;
628
919
      txSlot* slot;
629
919
      mxPushList();
630
919
      list = the->stack;
631
919
      slot = list->value.list.first = fxNewSlot(the);
632
919
      slot->kind = XS_DATA_VIEW_KIND;
633
919
      slot->value.dataView.offset = theParser->sourceOffset;
634
919
      slot->value.dataView.size = theParser->sourceSize;
635
919
      slot = slot->next = list->value.list.last = fxNewSlot(the);
636
919
      slot->kind = value->kind;
637
919
      slot->value = value->value;
638
919
    }
639
34.4k
    fxParseJSONToken(the, theParser);
640
34.4k
  }
641
55.6k
}
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.21k
      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
263
          mxPushSlot(sourceReference);
667
263
          mxGetIndex(index);
668
263
        }
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
29
          mxPushSlot(sourceReference);
695
29
          mxGetAll(at->value.at.id, at->value.at.index);
696
29
        }
697
20.8k
        else
698
20.8k
          mxPushUndefined();
699
20.8k
        fxReviveJSON(the, theParser, reviver);
700
20.8k
        if (mxIsUndefined(the->stack)) {
701
151
          mxBehaviorDeleteProperty(the, valueReference->value.reference, at->value.at.id, at->value.at.index);
702
151
        }
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
769
    txSlot* view = sourceReference->value.list.first;
713
769
    txInteger offset = view->value.dataView.offset;
714
769
    txInteger size = view->value.dataView.size;
715
769
    txSlot* instance;
716
769
    txSlot* source;
717
769
    mxPop();
718
769
    mxPush(mxObjectPrototype);
719
769
    instance = fxNewObjectInstance(the);
720
769
    source = instance->next = fxNewSlot(the);
721
769
    source->value.string = fxNewChunk(the, size + 1);
722
769
    c_memcpy(source->value.string, theParser->slot->value.string + offset, size);
723
769
    source->value.string[size] = 0;
724
769
    source->kind = XS_STRING_KIND;
725
769
    source->ID = mxID(_source);
726
769
  }
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
100
{
737
100
  txSlot* slot;
738
100
  txString string;
739
100
  txSize length;
740
100
  txSlot* instance;
741
100
  txSlot* property;
742
100
  volatile txJSONParser aParser = {0};
743
100
  if (mxArgc > 0)
744
100
    mxPushSlot(mxArgv(0));
745
0
  else
746
0
    mxPushUndefined();
747
100
  slot = the->stack;
748
100
  string = fxToString(the, slot);
749
100
  length = (txSize)c_strlen(string);
750
100
  if (length == 0) 
751
2
    mxSyntaxError("empty string");
752
98
  else {
753
98
    char first = string[0];
754
98
    char last = string[length - 1];
755
98
    if ((first == 0x09) || (first == 0x0A) || (first == 0x0D) || (first == 0x20) || (last == 0x09) || (last == 0x0A) || (last == 0x0D) || (last == 0x20))
756
24
    mxSyntaxError("invalid string");
757
98
  }
758
74
  aParser.slot = slot;
759
74
  aParser.offset = 0;
760
74
  mxPush(mxEmptyString);
761
74
  aParser.string = the->stack;
762
74
  aParser.line = 1;
763
74
  fxParseJSON(the, (txJSONParser*)&aParser);
764
74
  if (mxIsReference(the->stack))
765
0
    mxSyntaxError("invalid string");
766
74
  mxPop();
767
74
  instance = fxNewInstance(the);
768
74
  instance->flag |= XS_EXOTIC_FLAG | XS_DONT_PATCH_FLAG;
769
74
  property = instance->next = fxNewSlot(the);
770
74
  property->flag = XS_INTERNAL_FLAG | XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG;
771
74
  property->kind = XS_RAW_JSON_KIND;
772
74
  property = property->next = fxNewSlot(the);
773
74
  property->ID = mxID(_rawJSON);
774
74
  property->flag = XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG;
775
74
  property->kind = slot->kind;
776
74
  property->value = slot->value;
777
74
  mxPullSlot(mxResult);
778
74
}
779
780
void fx_JSON_stringify(txMachine* the)
781
270k
{
782
270k
  volatile txJSONStringifier aStringifier = {0};
783
270k
  mxTry(the) {
784
270k
    fxStringifyJSON(the, (txJSONStringifier*)&aStringifier);
785
270k
    if (aStringifier.offset) {
786
267k
      fxStringifyJSONChars(the, (txJSONStringifier*)&aStringifier, "\0", 1);
787
267k
      mxResult->value.string = (txString)fxNewChunk(the, aStringifier.offset);
788
267k
      c_memcpy(mxResult->value.string, aStringifier.buffer, aStringifier.offset);
789
267k
      mxResult->kind = XS_STRING_KIND;
790
267k
    }
791
270k
    c_free(aStringifier.buffer);
792
270k
  }
793
270k
  mxCatch(the) {
794
16
    if (aStringifier.buffer)
795
16
      c_free(aStringifier.buffer);
796
16
    fxJump(the);
797
16
  }
798
270k
}
799
800
void fxStringifyJSON(txMachine* the, txJSONStringifier* theStringifier)
801
270k
{
802
270k
  txSlot* aSlot;
803
270k
  txInteger aFlag;
804
270k
  txSlot* instance;
805
  
806
270k
  aSlot = fxGetInstance(the, mxThis);
807
270k
  theStringifier->offset = 0;
808
270k
  theStringifier->size = 1024;
809
270k
  theStringifier->buffer = c_malloc(1024);
810
270k
  if (!theStringifier->buffer)
811
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
812
813
270k
  if (mxArgc > 1) {
814
3.21k
    aSlot = mxArgv(1);
815
3.21k
    if (mxIsReference(aSlot)) {
816
3.05k
      if (fxIsCallable(the, aSlot))
817
2.99k
        theStringifier->replacer = mxArgv(1);
818
53
      else if (fxIsArray(the, fxGetInstance(the, aSlot)))
819
45
        theStringifier->keys = fxToJSONKeys(the, aSlot);
820
3.05k
    }
821
3.21k
  }
822
270k
  if (mxArgc > 2) {
823
154
    aSlot = mxArgv(2);
824
154
    if (mxIsReference(aSlot)) {
825
56
      txSlot* instance = fxGetInstance(the, aSlot);
826
56
      if (mxIsNumber(instance)) {
827
20
        fxToNumber(the, aSlot);
828
20
      }
829
36
      else if (mxIsString(instance)) {
830
5
        fxToString(the, aSlot);
831
5
      }
832
56
    }
833
154
    if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) {
834
72
      txInteger aCount = fxToInteger(the, aSlot), anIndex;
835
72
      if (aCount < 0)
836
11
        aCount = 0;
837
61
      else if (aCount > 10)
838
22
        aCount = 10;
839
457
      for (anIndex = 0; anIndex < aCount; anIndex++)
840
385
        theStringifier->indent[anIndex] = ' ';
841
72
      theStringifier->indentLength = aCount;
842
72
    }
843
82
    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
154
  }
856
857
270k
  theStringifier->stack = the->stack;
858
270k
  mxPush(mxObjectPrototype);
859
270k
  instance = fxNewObjectInstance(the);
860
270k
  aFlag = 0;
861
270k
  if (mxArgc > 0)
862
270k
    mxPushSlot(mxArgv(0));
863
11
  else
864
11
    mxPushUndefined();
865
270k
  fxNextSlotProperty(the, instance, the->stack, mxID(__empty_string_), XS_NO_FLAG);
866
270k
  mxPush(mxEmptyString);
867
270k
  fxStringifyJSONProperty(the, theStringifier, &aFlag);
868
270k
  mxPop();
869
270k
}
870
871
void fxStringifyJSONCharacter(txMachine* the, txJSONStringifier* theStringifier, txInteger character)
872
26.6M
{
873
26.6M
    txSize size = mxStringByteLength(character);
874
26.6M
  if ((theStringifier->offset + size) >= theStringifier->size) {
875
64.4k
    char* aBuffer;
876
64.4k
    theStringifier->size += ((size / 1024) + 1) * 1024;
877
64.4k
    aBuffer = c_realloc(theStringifier->buffer, theStringifier->size);
878
64.4k
    if (!aBuffer)
879
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
880
64.4k
    theStringifier->buffer = aBuffer;
881
64.4k
  }
882
26.6M
  mxStringByteEncode(theStringifier->buffer + theStringifier->offset, character);
883
26.6M
  theStringifier->offset += size;
884
26.6M
}
885
886
void fxStringifyJSONChars(txMachine* the, txJSONStringifier* theStringifier, char* s, txSize theSize)
887
213M
{
888
    //fprintf(stderr, "%s", s);
889
213M
  if ((theStringifier->offset + theSize) >= theStringifier->size) {
890
224k
    char* aBuffer;
891
224k
    theStringifier->size += ((theSize / 1024) + 1) * 1024;
892
224k
    aBuffer = c_realloc(theStringifier->buffer, theStringifier->size);
893
224k
    if (!aBuffer)
894
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
895
224k
    theStringifier->buffer = aBuffer;
896
224k
  }
897
213M
  c_memcpy(theStringifier->buffer + theStringifier->offset, s, theSize);
898
213M
  theStringifier->offset += theSize;
899
213M
}
900
901
void fxStringifyJSONIndent(txMachine* the, txJSONStringifier* theStringifier)
902
29.9M
{
903
29.9M
  txInteger aLevel;
904
29.9M
  if (theStringifier->indent[0]) {
905
1.63k
    fxStringifyJSONChars(the, theStringifier, "\n", 1);
906
5.70k
    for (aLevel = 0; aLevel < theStringifier->level; aLevel++)
907
4.06k
      fxStringifyJSONChars(the, theStringifier, theStringifier->indent, theStringifier->indentLength);
908
1.63k
  }
909
29.9M
}
910
911
void fxStringifyJSONInteger(txMachine* the, txJSONStringifier* theStringifier, txInteger theInteger)
912
29.5M
{
913
29.5M
  char aBuffer[256];
914
29.5M
  fxIntegerToString(the, theInteger, aBuffer, sizeof(aBuffer));
915
29.5M
  fxStringifyJSONChars(the, theStringifier, aBuffer, (txSize)c_strlen(aBuffer));
916
29.5M
}
917
918
void fxStringifyJSONName(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag)
919
30.0M
{
920
30.0M
  txSlot* aSlot = the->stack;
921
30.0M
  if (*theFlag & 1) {
922
29.5M
    fxStringifyJSONChars(the, theStringifier, ",", 1);
923
29.5M
    fxStringifyJSONIndent(the, theStringifier);
924
29.5M
  }
925
484k
  else
926
484k
    *theFlag |= 1;
927
30.0M
  if (*theFlag & 2) {
928
29.7M
    if (aSlot->kind == XS_INTEGER_KIND) {
929
29.5M
      fxStringifyJSONChars(the, theStringifier, "\"", 1);
930
29.5M
      fxStringifyJSONInteger(the, theStringifier, aSlot->value.integer);
931
29.5M
      fxStringifyJSONChars(the, theStringifier, "\"", 1);
932
29.5M
    }
933
224k
    else
934
224k
      fxStringifyJSONString(the, theStringifier, aSlot->value.string);
935
29.7M
    fxStringifyJSONChars(the, theStringifier, ":", 1);
936
29.7M
    if (theStringifier->indent[0])
937
376
      fxStringifyJSONChars(the, theStringifier, " ", 1);
938
29.7M
  }
939
30.0M
  mxPop(); // POP KEY
940
30.0M
}
941
942
void fxStringifyJSONNumber(txMachine* the, txJSONStringifier* theStringifier, txNumber theNumber)
943
170k
{
944
170k
  int fpclass = c_fpclassify(theNumber);
945
170k
  if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE)) {
946
25.3k
    char aBuffer[256];
947
25.3k
    fxNumberToString(the, theNumber, aBuffer, sizeof(aBuffer), 0, 0);
948
25.3k
    fxStringifyJSONChars(the, theStringifier, aBuffer, (txSize)c_strlen(aBuffer));
949
25.3k
  }
950
145k
  else
951
145k
    fxStringifyJSONChars(the, theStringifier, "null", 4);
952
170k
}
953
954
void fxStringifyJSONProperty(txMachine* the, txJSONStringifier* theStringifier, txInteger* theFlag)
955
30.1M
{
956
30.1M
  txSlot* aWrapper = the->stack + 2;
957
30.1M
  txSlot* aValue = the->stack + 1;
958
30.1M
  txSlot* aKey = the->stack;
959
30.1M
  txSlot* anInstance;
960
30.1M
  txSlot* aSlot;
961
30.1M
  txInteger aFlag;
962
30.1M
  txIndex aLength, anIndex;
963
  
964
30.1M
  mxCheckCStack();
965
30.1M
  if (mxIsReference(aValue) || mxIsBigInt(aValue)) {
966
    /* THIS */
967
205k
    mxPushSlot(aValue);
968
    /* FUNCTION */
969
205k
    mxDub();
970
205k
    mxGetID(mxID(_toJSON));
971
205k
    if (mxIsReference(the->stack) && mxIsFunction(the->stack->value.reference))  {
972
6.87k
      mxCall();
973
6.87k
      mxPushSlot(aKey);
974
6.87k
      fxToString(the, the->stack);
975
6.87k
      mxRunCount(1);
976
6.87k
      mxPullSlot(aValue);
977
6.87k
    }
978
205k
    the->stack = aKey;
979
205k
  }
980
30.1M
  if (theStringifier->replacer) {
981
    /* THIS */
982
49.1k
    mxPushSlot(aWrapper);
983
    /* FUNCTION */
984
49.1k
    mxPushSlot(theStringifier->replacer);
985
49.1k
    mxCall();
986
    /* ARGUMENTS */
987
49.1k
    mxPushSlot(aKey);
988
49.1k
    fxToString(the, the->stack);
989
49.1k
    mxPushSlot(aValue);
990
    /* COUNT */
991
49.1k
    mxRunCount(2);
992
49.1k
    mxPullSlot(aValue);
993
49.1k
    the->stack = aKey;
994
49.1k
  }
995
30.1M
  if (mxIsReference(aValue)) {
996
244k
    mxPushSlot(aValue);
997
244k
    anInstance = fxToInstance(the, the->stack);
998
244k
    if (anInstance->flag & XS_LEVEL_FLAG)
999
7
      mxTypeError("cyclic value");
1000
244k
    the->stack = aKey;
1001
244k
    aSlot = anInstance->next;
1002
244k
    if (aSlot && (aSlot->flag & XS_INTERNAL_FLAG)) {
1003
159k
      if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) {
1004
3
        fxToNumber(the, aValue);
1005
3
      }
1006
159k
      else if (mxIsStringPrimitive(aSlot)) {
1007
3
        fxToString(the, aValue);
1008
3
      }
1009
159k
      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
159k
      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
159k
    }
1025
244k
  }
1026
30.1M
  if (aValue->kind == XS_NULL_KIND) {
1027
3.22k
    fxStringifyJSONName(the, theStringifier, theFlag);
1028
3.22k
    fxStringifyJSONChars(the, theStringifier, "null", 4);
1029
3.22k
  }
1030
30.1M
  else if (aValue->kind == XS_BOOLEAN_KIND) {
1031
88.0k
    fxStringifyJSONName(the, theStringifier, theFlag);
1032
88.0k
    if (aValue->value.boolean)
1033
365
      fxStringifyJSONChars(the, theStringifier, "true", 4);
1034
87.6k
    else
1035
87.6k
      fxStringifyJSONChars(the, theStringifier, "false", 5);
1036
88.0k
  }
1037
30.0M
  else if (aValue->kind == XS_INTEGER_KIND) {
1038
1.39k
    fxStringifyJSONName(the, theStringifier, theFlag);
1039
1.39k
    fxStringifyJSONInteger(the, theStringifier, aValue->value.integer);
1040
1.39k
  }
1041
30.0M
  else if (aValue->kind == XS_NUMBER_KIND) {
1042
170k
    fxStringifyJSONName(the, theStringifier, theFlag);
1043
170k
    fxStringifyJSONNumber(the, theStringifier, aValue->value.number);
1044
170k
  }
1045
29.9M
  else if ((aValue->kind == XS_STRING_KIND) || (aValue->kind == XS_STRING_X_KIND)) {
1046
29.5M
    fxStringifyJSONName(the, theStringifier, theFlag);
1047
29.5M
    fxStringifyJSONString(the, theStringifier, aValue->value.string);
1048
29.5M
  }
1049
327k
  else if ((aValue->kind == XS_BIGINT_KIND) || (aValue->kind == XS_BIGINT_X_KIND)) {
1050
8
    mxTypeError("stringify bigint");
1051
8
  }
1052
327k
  else if ((aValue->kind == XS_REFERENCE_KIND) && !fxIsCallable(the, aValue)) {
1053
222k
    mxTry(the) {
1054
222k
      fxStringifyJSONName(the, theStringifier, theFlag);
1055
222k
      if (anInstance->flag & XS_MARK_FLAG)
1056
0
        mxTypeError("read only value");
1057
222k
      anInstance->flag |= XS_LEVEL_FLAG;
1058
222k
      if (fxIsArray(the, anInstance)) {
1059
51.3k
        fxStringifyJSONChars(the, theStringifier, "[", 1);
1060
51.3k
        mxPushReference(anInstance);
1061
51.3k
        mxGetID(mxID(_length));
1062
51.3k
        aLength = fxToInteger(the, the->stack);
1063
51.3k
        if (aLength > 0) {
1064
46.9k
          theStringifier->level++;
1065
46.9k
          fxStringifyJSONIndent(the, theStringifier);
1066
46.9k
          aFlag = 4;
1067
46.9k
          mxPop();
1068
97.3k
          for (anIndex = 0; anIndex < aLength; anIndex++) {
1069
50.4k
            mxPushReference(anInstance);
1070
50.4k
            mxGetIndex(anIndex);
1071
50.4k
            mxPushInteger(anIndex);
1072
50.4k
            fxStringifyJSONProperty(the, theStringifier, &aFlag);
1073
50.4k
          }
1074
46.9k
          theStringifier->level--;
1075
46.9k
          fxStringifyJSONIndent(the, theStringifier);
1076
46.9k
        }
1077
51.3k
        fxStringifyJSONChars(the, theStringifier, "]", 1);
1078
51.3k
      }
1079
170k
      else {
1080
170k
        fxStringifyJSONChars(the, theStringifier, "{", 1);
1081
170k
        {
1082
170k
          txSlot* at;
1083
170k
          txSlot* property;
1084
170k
          if (theStringifier->keys) {
1085
47
            mxPushUndefined();
1086
47
            at = theStringifier->keys->value.reference;
1087
47
          }
1088
170k
          else {
1089
170k
            at = fxNewInstance(the);
1090
170k
            mxBehaviorOwnKeys(the, anInstance, XS_EACH_NAME_FLAG, at);
1091
170k
          }
1092
170k
          if (at->next) {
1093
170k
            theStringifier->level++;
1094
170k
            fxStringifyJSONIndent(the, theStringifier);
1095
170k
            aFlag = 2;
1096
170k
            mxPushUndefined();
1097
170k
            property = the->stack;
1098
170k
            mxPushReference(anInstance);
1099
30.0M
            while ((at = at->next)) {
1100
29.8M
              if (mxBehaviorGetOwnProperty(the, anInstance, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) {
1101
29.8M
                mxPushReference(anInstance);
1102
29.8M
                mxGetAll(at->value.at.id, at->value.at.index);
1103
29.8M
                if (at->value.at.id)
1104
324k
                  fxPushKeyString(the, at->value.at.id, C_NULL);
1105
29.5M
                else
1106
29.5M
                  mxPushInteger((txInteger)at->value.at.index);
1107
29.8M
                fxStringifyJSONProperty(the, theStringifier, &aFlag);
1108
29.8M
              }
1109
29.8M
            }
1110
170k
            mxPop();
1111
170k
            mxPop();
1112
170k
            theStringifier->level--;
1113
170k
            fxStringifyJSONIndent(the, theStringifier);
1114
170k
          }
1115
170k
          mxPop();
1116
170k
        }
1117
170k
        fxStringifyJSONChars(the, theStringifier, "}", 1);
1118
170k
      }
1119
222k
      anInstance->flag &= ~XS_LEVEL_FLAG;
1120
222k
    }
1121
222k
    mxCatch(the) {
1122
17
      if (anInstance->flag & XS_LEVEL_FLAG)
1123
17
        anInstance->flag &= ~XS_LEVEL_FLAG;
1124
17
      fxJump(the);
1125
17
    }
1126
222k
  }
1127
105k
  else {
1128
105k
    if (*theFlag & 4) {
1129
1.51k
      if (*theFlag & 1) {
1130
1.27k
        fxStringifyJSONChars(the, theStringifier, ",", 1);
1131
1.27k
        fxStringifyJSONIndent(the, theStringifier);
1132
1.27k
      }
1133
244
      else
1134
244
        *theFlag |= 1;
1135
1.51k
      fxStringifyJSONChars(the, theStringifier, "null", 4);
1136
1.51k
    }
1137
105k
  }
1138
30.1M
  mxPop(); // POP VALUE
1139
30.1M
}
1140
1141
void fxStringifyJSONString(txMachine* the, txJSONStringifier* theStringifier, txString theString)
1142
29.8M
{
1143
29.8M
  fxStringifyJSONChars(the, theStringifier, "\"", 1);
1144
61.6M
  for (;;) {
1145
61.6M
    txInteger character;  
1146
61.6M
    theString = mxStringByteDecode(theString, &character);
1147
61.6M
    if (character == C_EOF)
1148
29.8M
      break;
1149
31.8M
    if (character < 8)
1150
3.00M
      fxStringifyJSONUnicodeEscape(the, theStringifier, character);
1151
28.8M
    else if (character == 8)
1152
20.0k
      fxStringifyJSONChars(the, theStringifier, "\\b", 2); 
1153
28.8M
    else if (character == 9)
1154
13.2k
      fxStringifyJSONChars(the, theStringifier, "\\t", 2); 
1155
28.8M
    else if (character == 10)
1156
47.4k
      fxStringifyJSONChars(the, theStringifier, "\\n", 2); 
1157
28.7M
    else if (character == 11)
1158
66.0k
      fxStringifyJSONUnicodeEscape(the, theStringifier, character); 
1159
28.6M
    else if (character == 12)
1160
171k
      fxStringifyJSONChars(the, theStringifier, "\\f", 2);
1161
28.5M
    else if (character == 13)
1162
9.78k
      fxStringifyJSONChars(the, theStringifier, "\\r", 2);
1163
28.5M
    else if (character < 32)
1164
525k
      fxStringifyJSONUnicodeEscape(the, theStringifier, character);
1165
27.9M
    else if (character < 34)
1166
284k
      fxStringifyJSONCharacter(the, theStringifier, character);
1167
27.7M
    else if (character == 34)
1168
518k
      fxStringifyJSONChars(the, theStringifier, "\\\"", 2);
1169
27.1M
    else if (character < 92)
1170
3.30M
      fxStringifyJSONCharacter(the, theStringifier, character);
1171
23.8M
    else if (character == 92)
1172
767k
      fxStringifyJSONChars(the, theStringifier, "\\\\", 2);
1173
23.1M
    else if (character < 127)
1174
4.21M
      fxStringifyJSONCharacter(the, theStringifier, character);
1175
18.8M
    else if ((0xD800 <= character) && (character <= 0xDFFF))
1176
95.0k
      fxStringifyJSONUnicodeEscape(the, theStringifier, character);
1177
18.7M
    else
1178
18.7M
      fxStringifyJSONCharacter(the, theStringifier, character);
1179
31.8M
  }
1180
29.8M
  fxStringifyJSONChars(the, theStringifier, "\"", 1);
1181
29.8M
}
1182
1183
void fxStringifyJSONUnicodeEscape(txMachine* the, txJSONStringifier* theStringifier, txInteger character)
1184
3.69M
{
1185
3.69M
  char buffer[16];
1186
3.69M
  txString p = buffer;
1187
3.69M
  *p++ = '\\'; 
1188
3.69M
  *p++ = 'u'; 
1189
3.69M
  p = fxStringifyUnicodeEscape(p, character, '\\');
1190
3.69M
  fxStringifyJSONChars(the, theStringifier, buffer, mxPtrDiff(p - buffer));
1191
3.69M
}
1192
1193
txSlot* fxToJSONKeys(txMachine* the, txSlot* reference)
1194
62
{
1195
62
  txSlot* list = fxNewInstance(the);
1196
62
  txSlot* item = list;
1197
62
  txSlot* slot;
1198
62
  txIndex length, i;
1199
62
  mxPushSlot(reference);
1200
62
  mxGetID(mxID(_length));
1201
62
  length = (txIndex)fxToLength(the, the->stack);
1202
62
  mxPop();
1203
62
  i = 0;
1204
325
  while (i < length) {
1205
263
    txBoolean flag = 0;
1206
263
    txID id = XS_NO_ID;
1207
263
    txIndex index = 0;
1208
263
    mxPushSlot(reference);
1209
263
    mxGetIndex(i);
1210
263
    slot = the->stack;
1211
329
  again:
1212
329
    if ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND)) {
1213
115
      if (fxStringToIndex(the, slot->value.string, &index))
1214
13
        flag = 1;
1215
102
      else {
1216
102
        if (slot->kind == XS_STRING_X_KIND)
1217
0
          id = fxNewNameX(the, slot->value.string);
1218
102
        else
1219
102
          id = fxNewName(the, slot);
1220
102
        flag = 1;
1221
102
      }
1222
115
    }
1223
214
    else if (slot->kind == XS_INTEGER_KIND) {
1224
100
      if (fxIntegerToIndex(the, slot->value.integer, &index))
1225
74
        flag = 1;
1226
26
      else {
1227
26
        fxToString(the, slot);
1228
26
        goto again;
1229
26
      }
1230
100
    }
1231
114
    else if (slot->kind == XS_NUMBER_KIND){
1232
48
      if (fxNumberToIndex(the, slot->value.number, &index))
1233
10
        flag = 1;
1234
38
      else {
1235
38
        fxToString(the, slot);
1236
38
        goto again;
1237
38
      }
1238
48
    }
1239
66
    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
263
    if (flag) {
1247
199
      txSlot* already = list->next;
1248
695
      while (already) {
1249
530
        if ((already->value.at.id == id) && (already->value.at.index == index))
1250
34
          break;
1251
496
        already = already->next;
1252
496
      }
1253
199
      if (!already) {
1254
165
        item = item->next = fxNewSlot(the);
1255
165
        item->value.at.id = id;
1256
165
        item->value.at.index = index;
1257
165
        item->kind = XS_AT_KIND;
1258
165
      }
1259
199
    }
1260
263
    mxPop();
1261
263
    i++;
1262
263
  }
1263
62
  return the->stack;
1264
62
}