Coverage Report

Created: 2026-09-28 06:51

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