@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.370

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1992 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ #include "runtime_internal.h"
29
+
30
+ #include <math.h>
31
+ #include <stdio.h>
32
+ #include <stdlib.h>
33
+ #include <string.h>
34
+
35
+ /*
36
+ * The JSON namespace and JSON.parse. The parser consumes the already
37
+ * converted UTF-16 string directly, so this data grammar does not compile or
38
+ * execute JavaScript source and does not cross the dynamic-source boundary.
39
+ */
40
+
41
+ typedef struct {
42
+ OseoContext *context;
43
+ OseoValue text;
44
+ size_t cursor;
45
+ /* Build a JSON Parse Record for every value, for a callable reviver. */
46
+ bool records;
47
+ } OseoJsonParser;
48
+
49
+ /*
50
+ * A JSON Parse Record is a four-slot environment: the parsed value, the
51
+ * start and end code-unit offsets of its source text, and its child
52
+ * records. An array's children are an internal array of records in
53
+ * element order. An object's children are a null-prototype object that
54
+ * maps each member name to its record, so a later duplicate name
55
+ * replaces the earlier record, as the last such member also gives the
56
+ * property its value. A primitive has no children.
57
+ */
58
+ enum {
59
+ JSON_RECORD_VALUE = 0u,
60
+ JSON_RECORD_START,
61
+ JSON_RECORD_END,
62
+ JSON_RECORD_CHILDREN,
63
+ JSON_RECORD_SLOT_COUNT,
64
+ };
65
+
66
+ static OseoResult json_record_create(
67
+ OseoContext *context,
68
+ OseoValue value,
69
+ size_t start,
70
+ size_t end,
71
+ OseoValue children
72
+ ) {
73
+ OseoValue slots[3] = {value, children, oseo_undefined()};
74
+ OseoRootFrame frame = {NULL, slots, 3u};
75
+ oseo_roots_push(context, &frame);
76
+ OseoResult result = oseo_environment_create(
77
+ context,
78
+ JSON_RECORD_SLOT_COUNT
79
+ );
80
+ slots[2] = result.value;
81
+ if (result.status == OSEO_STATUS_NORMAL) {
82
+ (void)oseo_environment_set(
83
+ context, slots[2], JSON_RECORD_VALUE, slots[0]);
84
+ (void)oseo_environment_set(
85
+ context, slots[2], JSON_RECORD_START, oseo_number((double)start));
86
+ (void)oseo_environment_set(
87
+ context, slots[2], JSON_RECORD_END, oseo_number((double)end));
88
+ (void)oseo_environment_set(
89
+ context, slots[2], JSON_RECORD_CHILDREN, slots[1]);
90
+ }
91
+ oseo_roots_pop(context, &frame);
92
+ return result;
93
+ }
94
+
95
+ static OseoValue json_record_slot(OseoValue record, size_t index) {
96
+ return environment_object(record)->slots[index];
97
+ }
98
+
99
+ static OseoResult json_syntax_error(OseoJsonParser *parser) {
100
+ return oseo_internal_throw_error(
101
+ parser->context,
102
+ OSEO_ERROR_SYNTAX,
103
+ "Invalid JSON text."
104
+ );
105
+ }
106
+
107
+ static OseoString *json_text(const OseoJsonParser *parser) {
108
+ return string_object(parser->text);
109
+ }
110
+
111
+ static void json_skip_whitespace(OseoJsonParser *parser) {
112
+ OseoString *text = json_text(parser);
113
+ while (parser->cursor < text->length) {
114
+ uint16_t unit = text->units[parser->cursor];
115
+ if (unit != UINT16_C(0x0009) && unit != UINT16_C(0x000a) &&
116
+ unit != UINT16_C(0x000d) && unit != UINT16_C(0x0020)) {
117
+ break;
118
+ }
119
+ parser->cursor += 1u;
120
+ }
121
+ }
122
+
123
+ static bool json_consume(OseoJsonParser *parser, uint16_t unit) {
124
+ OseoString *text = json_text(parser);
125
+ if (parser->cursor >= text->length ||
126
+ text->units[parser->cursor] != unit) {
127
+ return false;
128
+ }
129
+ parser->cursor += 1u;
130
+ return true;
131
+ }
132
+
133
+ static int json_hex_digit(uint16_t unit) {
134
+ if (unit >= UINT16_C('0') && unit <= UINT16_C('9')) {
135
+ return (int)(unit - UINT16_C('0'));
136
+ }
137
+ if (unit >= UINT16_C('a') && unit <= UINT16_C('f')) {
138
+ return (int)(unit - UINT16_C('a')) + 10;
139
+ }
140
+ if (unit >= UINT16_C('A') && unit <= UINT16_C('F')) {
141
+ return (int)(unit - UINT16_C('A')) + 10;
142
+ }
143
+ return -1;
144
+ }
145
+
146
+ static OseoResult json_parse_string(OseoJsonParser *parser) {
147
+ if (!json_consume(parser, UINT16_C('"'))) {
148
+ return json_syntax_error(parser);
149
+ }
150
+ OseoString *text = json_text(parser);
151
+ size_t capacity = text->length - parser->cursor;
152
+ uint16_t *units = NULL;
153
+ if (capacity > 0u) {
154
+ if (capacity > SIZE_MAX / sizeof(*units)) {
155
+ return failure(
156
+ parser->context,
157
+ "OSEO2001",
158
+ "JSON string is too large."
159
+ );
160
+ }
161
+ units = oseo_internal_allocate_work_bytes(
162
+ parser->context,
163
+ capacity * sizeof(*units)
164
+ );
165
+ if (units == NULL) {
166
+ return failure(
167
+ parser->context,
168
+ "OSEO2001",
169
+ "JSON string allocation failed."
170
+ );
171
+ }
172
+ }
173
+ size_t length = 0u;
174
+ while (parser->cursor < json_text(parser)->length) {
175
+ uint16_t unit = json_text(parser)->units[parser->cursor];
176
+ parser->cursor += 1u;
177
+ if (unit == UINT16_C('"')) {
178
+ OseoResult result = oseo_string_from_units(
179
+ parser->context,
180
+ units,
181
+ length
182
+ );
183
+ free(units);
184
+ return result;
185
+ }
186
+ if (unit < UINT16_C(0x0020)) {
187
+ free(units);
188
+ return json_syntax_error(parser);
189
+ }
190
+ if (unit != UINT16_C('\\')) {
191
+ units[length] = unit;
192
+ length += 1u;
193
+ continue;
194
+ }
195
+ text = json_text(parser);
196
+ if (parser->cursor >= text->length) {
197
+ free(units);
198
+ return json_syntax_error(parser);
199
+ }
200
+ uint16_t escape = text->units[parser->cursor];
201
+ parser->cursor += 1u;
202
+ if (escape == UINT16_C('"') || escape == UINT16_C('\\') ||
203
+ escape == UINT16_C('/')) {
204
+ unit = escape;
205
+ } else if (escape == UINT16_C('b')) {
206
+ unit = UINT16_C(0x0008);
207
+ } else if (escape == UINT16_C('f')) {
208
+ unit = UINT16_C(0x000c);
209
+ } else if (escape == UINT16_C('n')) {
210
+ unit = UINT16_C(0x000a);
211
+ } else if (escape == UINT16_C('r')) {
212
+ unit = UINT16_C(0x000d);
213
+ } else if (escape == UINT16_C('t')) {
214
+ unit = UINT16_C(0x0009);
215
+ } else if (escape == UINT16_C('u')) {
216
+ uint16_t decoded = 0u;
217
+ for (size_t index = 0u; index < 4u; index += 1u) {
218
+ text = json_text(parser);
219
+ if (parser->cursor >= text->length) {
220
+ free(units);
221
+ return json_syntax_error(parser);
222
+ }
223
+ int digit = json_hex_digit(text->units[parser->cursor]);
224
+ if (digit < 0) {
225
+ free(units);
226
+ return json_syntax_error(parser);
227
+ }
228
+ parser->cursor += 1u;
229
+ decoded = (uint16_t)(decoded * UINT16_C(16) +
230
+ (uint16_t)digit);
231
+ }
232
+ unit = decoded;
233
+ } else {
234
+ free(units);
235
+ return json_syntax_error(parser);
236
+ }
237
+ units[length] = unit;
238
+ length += 1u;
239
+ }
240
+ free(units);
241
+ return json_syntax_error(parser);
242
+ }
243
+
244
+ static bool json_match_literal(OseoJsonParser *parser, const char *literal) {
245
+ size_t length = strlen(literal);
246
+ OseoString *text = json_text(parser);
247
+ if (length > text->length - parser->cursor) return false;
248
+ for (size_t index = 0u; index < length; index += 1u) {
249
+ if (text->units[parser->cursor + index] !=
250
+ (uint16_t)(unsigned char)literal[index]) {
251
+ return false;
252
+ }
253
+ }
254
+ parser->cursor += length;
255
+ return true;
256
+ }
257
+
258
+ static bool json_decimal_digit(uint16_t unit) {
259
+ return unit >= UINT16_C('0') && unit <= UINT16_C('9');
260
+ }
261
+
262
+ static OseoResult json_parse_number(OseoJsonParser *parser) {
263
+ OseoString *text = json_text(parser);
264
+ size_t start = parser->cursor;
265
+ if (parser->cursor < text->length &&
266
+ text->units[parser->cursor] == UINT16_C('-')) {
267
+ parser->cursor += 1u;
268
+ }
269
+ if (parser->cursor >= text->length) return json_syntax_error(parser);
270
+ uint16_t first = text->units[parser->cursor];
271
+ if (first == UINT16_C('0')) {
272
+ parser->cursor += 1u;
273
+ if (parser->cursor < text->length &&
274
+ json_decimal_digit(text->units[parser->cursor])) {
275
+ return json_syntax_error(parser);
276
+ }
277
+ } else if (first >= UINT16_C('1') && first <= UINT16_C('9')) {
278
+ do {
279
+ parser->cursor += 1u;
280
+ } while (parser->cursor < text->length &&
281
+ json_decimal_digit(text->units[parser->cursor]));
282
+ } else {
283
+ return json_syntax_error(parser);
284
+ }
285
+ if (parser->cursor < text->length &&
286
+ text->units[parser->cursor] == UINT16_C('.')) {
287
+ parser->cursor += 1u;
288
+ if (parser->cursor >= text->length ||
289
+ !json_decimal_digit(text->units[parser->cursor])) {
290
+ return json_syntax_error(parser);
291
+ }
292
+ do {
293
+ parser->cursor += 1u;
294
+ } while (parser->cursor < text->length &&
295
+ json_decimal_digit(text->units[parser->cursor]));
296
+ }
297
+ if (parser->cursor < text->length &&
298
+ (text->units[parser->cursor] == UINT16_C('e') ||
299
+ text->units[parser->cursor] == UINT16_C('E'))) {
300
+ parser->cursor += 1u;
301
+ if (parser->cursor < text->length &&
302
+ (text->units[parser->cursor] == UINT16_C('+') ||
303
+ text->units[parser->cursor] == UINT16_C('-'))) {
304
+ parser->cursor += 1u;
305
+ }
306
+ if (parser->cursor >= text->length ||
307
+ !json_decimal_digit(text->units[parser->cursor])) {
308
+ return json_syntax_error(parser);
309
+ }
310
+ do {
311
+ parser->cursor += 1u;
312
+ } while (parser->cursor < text->length &&
313
+ json_decimal_digit(text->units[parser->cursor]));
314
+ }
315
+ size_t length = parser->cursor - start;
316
+ char *buffer = oseo_internal_allocate_work_bytes(
317
+ parser->context,
318
+ length + 1u
319
+ );
320
+ if (buffer == NULL) {
321
+ return failure(
322
+ parser->context,
323
+ "OSEO2001",
324
+ "JSON number allocation failed."
325
+ );
326
+ }
327
+ text = json_text(parser);
328
+ for (size_t index = 0u; index < length; index += 1u) {
329
+ buffer[index] = (char)text->units[start + index];
330
+ }
331
+ buffer[length] = '\0';
332
+ double number = strtod(buffer, NULL);
333
+ free(buffer);
334
+ return normal(oseo_number(number));
335
+ }
336
+
337
+ static OseoResult json_parse_value(
338
+ OseoJsonParser *parser,
339
+ size_t depth,
340
+ OseoValue *record
341
+ );
342
+
343
+ static OseoResult json_index_key(OseoContext *context, size_t index) {
344
+ char text[32];
345
+ (void)snprintf(text, sizeof(text), "%zu", index);
346
+ return oseo_internal_ascii_string(context, text);
347
+ }
348
+
349
+ /* Stores a container's record once its closing token is consumed. */
350
+ static OseoResult json_container_record(
351
+ OseoJsonParser *parser,
352
+ OseoResult result,
353
+ OseoValue value,
354
+ size_t start,
355
+ OseoValue children,
356
+ OseoValue *record
357
+ ) {
358
+ if (result.status != OSEO_STATUS_NORMAL || !parser->records) {
359
+ return result;
360
+ }
361
+ OseoResult created = json_record_create(
362
+ parser->context,
363
+ value,
364
+ start,
365
+ parser->cursor,
366
+ children
367
+ );
368
+ if (created.status != OSEO_STATUS_NORMAL) return created;
369
+ *record = created.value;
370
+ return result;
371
+ }
372
+
373
+ static OseoResult json_parse_array(
374
+ OseoJsonParser *parser,
375
+ size_t depth,
376
+ OseoValue *record
377
+ ) {
378
+ size_t start = parser->cursor;
379
+ parser->cursor += 1u;
380
+ OseoRootFrame frame = {NULL, NULL, 0u};
381
+ OseoResult result = oseo_roots_allocate(parser->context, &frame, 5u);
382
+ if (result.status != OSEO_STATUS_NORMAL) return result;
383
+ /* 3: the children array, 4: the current element's record. */
384
+ result = oseo_array_create(parser->context, 0u);
385
+ frame.slots[0] = result.value;
386
+ if (result.status == OSEO_STATUS_NORMAL && parser->records) {
387
+ result = oseo_array_create(parser->context, 0u);
388
+ frame.slots[3] = result.value;
389
+ }
390
+ json_skip_whitespace(parser);
391
+ if (result.status == OSEO_STATUS_NORMAL &&
392
+ json_consume(parser, UINT16_C(']'))) {
393
+ result.value = frame.slots[0];
394
+ result = json_container_record(
395
+ parser, result, frame.slots[0], start, frame.slots[3], record);
396
+ oseo_roots_release(parser->context, &frame);
397
+ return result;
398
+ }
399
+ size_t index = 0u;
400
+ while (result.status == OSEO_STATUS_NORMAL) {
401
+ result = json_parse_value(parser, depth + 1u, &frame.slots[4]);
402
+ frame.slots[1] = result.value;
403
+ if (result.status != OSEO_STATUS_NORMAL) break;
404
+ if (parser->records) {
405
+ result = oseo_array_append(
406
+ parser->context,
407
+ frame.slots[3],
408
+ frame.slots[4]
409
+ );
410
+ if (result.status != OSEO_STATUS_NORMAL) break;
411
+ }
412
+ result = json_index_key(parser->context, index);
413
+ frame.slots[2] = result.value;
414
+ if (result.status != OSEO_STATUS_NORMAL) break;
415
+ result = oseo_object_define(
416
+ parser->context,
417
+ frame.slots[0],
418
+ frame.slots[2],
419
+ frame.slots[1],
420
+ (OseoPropertyAttributes){true, true, true, false}
421
+ );
422
+ if (result.status != OSEO_STATUS_NORMAL) break;
423
+ index += 1u;
424
+ json_skip_whitespace(parser);
425
+ if (json_consume(parser, UINT16_C(']'))) break;
426
+ if (!json_consume(parser, UINT16_C(','))) {
427
+ result = json_syntax_error(parser);
428
+ break;
429
+ }
430
+ json_skip_whitespace(parser);
431
+ }
432
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
433
+ result = json_container_record(
434
+ parser, result, frame.slots[0], start, frame.slots[3], record);
435
+ oseo_roots_release(parser->context, &frame);
436
+ return result;
437
+ }
438
+
439
+ static OseoResult json_parse_object(
440
+ OseoJsonParser *parser,
441
+ size_t depth,
442
+ OseoValue *record
443
+ ) {
444
+ size_t start = parser->cursor;
445
+ parser->cursor += 1u;
446
+ OseoRootFrame frame = {NULL, NULL, 0u};
447
+ OseoResult result = oseo_roots_allocate(parser->context, &frame, 5u);
448
+ if (result.status != OSEO_STATUS_NORMAL) return result;
449
+ /* 3: the member-name map of records, 4: the current record. */
450
+ result = oseo_object_literal_create(parser->context);
451
+ frame.slots[0] = result.value;
452
+ if (result.status == OSEO_STATUS_NORMAL && parser->records) {
453
+ result = oseo_object_create(parser->context, oseo_null());
454
+ frame.slots[3] = result.value;
455
+ }
456
+ json_skip_whitespace(parser);
457
+ if (result.status == OSEO_STATUS_NORMAL &&
458
+ json_consume(parser, UINT16_C('}'))) {
459
+ result.value = frame.slots[0];
460
+ result = json_container_record(
461
+ parser, result, frame.slots[0], start, frame.slots[3], record);
462
+ oseo_roots_release(parser->context, &frame);
463
+ return result;
464
+ }
465
+ while (result.status == OSEO_STATUS_NORMAL) {
466
+ result = json_parse_string(parser);
467
+ frame.slots[1] = result.value;
468
+ if (result.status != OSEO_STATUS_NORMAL) break;
469
+ json_skip_whitespace(parser);
470
+ if (!json_consume(parser, UINT16_C(':'))) {
471
+ result = json_syntax_error(parser);
472
+ break;
473
+ }
474
+ json_skip_whitespace(parser);
475
+ result = json_parse_value(parser, depth + 1u, &frame.slots[4]);
476
+ frame.slots[2] = result.value;
477
+ if (result.status != OSEO_STATUS_NORMAL) break;
478
+ result = oseo_object_define(
479
+ parser->context,
480
+ frame.slots[0],
481
+ frame.slots[1],
482
+ frame.slots[2],
483
+ (OseoPropertyAttributes){true, true, true, false}
484
+ );
485
+ if (result.status != OSEO_STATUS_NORMAL) break;
486
+ if (parser->records) {
487
+ result = oseo_object_define(
488
+ parser->context,
489
+ frame.slots[3],
490
+ frame.slots[1],
491
+ frame.slots[4],
492
+ (OseoPropertyAttributes){true, true, true, false}
493
+ );
494
+ if (result.status != OSEO_STATUS_NORMAL) break;
495
+ }
496
+ json_skip_whitespace(parser);
497
+ if (json_consume(parser, UINT16_C('}'))) break;
498
+ if (!json_consume(parser, UINT16_C(','))) {
499
+ result = json_syntax_error(parser);
500
+ break;
501
+ }
502
+ json_skip_whitespace(parser);
503
+ }
504
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
505
+ result = json_container_record(
506
+ parser, result, frame.slots[0], start, frame.slots[3], record);
507
+ oseo_roots_release(parser->context, &frame);
508
+ return result;
509
+ }
510
+
511
+ static OseoResult json_parse_primitive(OseoJsonParser *parser);
512
+
513
+ /*
514
+ * Parses one value. With records requested, `record` receives its JSON
515
+ * Parse Record; it must be a rooted slot.
516
+ */
517
+ static OseoResult json_parse_value(
518
+ OseoJsonParser *parser,
519
+ size_t depth,
520
+ OseoValue *record
521
+ ) {
522
+ if (depth >= OSEO_MAX_CALL_DEPTH) {
523
+ return oseo_internal_throw_error(
524
+ parser->context,
525
+ OSEO_ERROR_RANGE,
526
+ "Maximum JSON nesting depth exceeded."
527
+ );
528
+ }
529
+ json_skip_whitespace(parser);
530
+ OseoString *text = json_text(parser);
531
+ if (parser->cursor >= text->length) return json_syntax_error(parser);
532
+ uint16_t unit = text->units[parser->cursor];
533
+ if (unit == UINT16_C('[')) return json_parse_array(parser, depth, record);
534
+ if (unit == UINT16_C('{')) {
535
+ return json_parse_object(parser, depth, record);
536
+ }
537
+ size_t start = parser->cursor;
538
+ OseoResult result = json_parse_primitive(parser);
539
+ if (result.status != OSEO_STATUS_NORMAL || !parser->records) {
540
+ return result;
541
+ }
542
+ OseoValue slots[1] = {result.value};
543
+ OseoRootFrame frame = {NULL, slots, 1u};
544
+ oseo_roots_push(parser->context, &frame);
545
+ OseoResult created = json_record_create(
546
+ parser->context,
547
+ slots[0],
548
+ start,
549
+ parser->cursor,
550
+ oseo_undefined()
551
+ );
552
+ if (created.status == OSEO_STATUS_NORMAL) {
553
+ *record = created.value;
554
+ created.value = slots[0];
555
+ }
556
+ oseo_roots_pop(parser->context, &frame);
557
+ return created;
558
+ }
559
+
560
+ /* A string, number, or literal name at the cursor. */
561
+ static OseoResult json_parse_primitive(OseoJsonParser *parser) {
562
+ OseoString *text = json_text(parser);
563
+ uint16_t unit = text->units[parser->cursor];
564
+ if (unit == UINT16_C('"')) return json_parse_string(parser);
565
+ if (unit == UINT16_C('-') || json_decimal_digit(unit)) {
566
+ return json_parse_number(parser);
567
+ }
568
+ if (json_match_literal(parser, "null")) return normal(oseo_null());
569
+ if (json_match_literal(parser, "false")) {
570
+ return normal(oseo_boolean(false));
571
+ }
572
+ if (json_match_literal(parser, "true")) {
573
+ return normal(oseo_boolean(true));
574
+ }
575
+ return json_syntax_error(parser);
576
+ }
577
+
578
+ static OseoResult json_parse_text(
579
+ OseoContext *context,
580
+ OseoValue text,
581
+ OseoValue *record
582
+ ) {
583
+ OseoJsonParser parser = {context, text, 0u, record != NULL};
584
+ OseoResult result = json_parse_value(&parser, 0u, record);
585
+ if (result.status != OSEO_STATUS_NORMAL) return result;
586
+ json_skip_whitespace(&parser);
587
+ if (parser.cursor != json_text(&parser)->length) {
588
+ return json_syntax_error(&parser);
589
+ }
590
+ return result;
591
+ }
592
+
593
+ static OseoResult json_define_revived(
594
+ OseoContext *context,
595
+ OseoValue object,
596
+ OseoValue key,
597
+ OseoValue value
598
+ ) {
599
+ const char *refusal = NULL;
600
+ return oseo_internal_define_data_reported(
601
+ context,
602
+ object,
603
+ key,
604
+ value,
605
+ (OseoPropertyAttributes){true, true, true, false},
606
+ true,
607
+ false,
608
+ &refusal
609
+ );
610
+ }
611
+
612
+ static OseoResult json_array_length(
613
+ OseoContext *context,
614
+ OseoValue array,
615
+ size_t *length
616
+ ) {
617
+ *length = 0u;
618
+ if (is_array(array)) {
619
+ *length = ordinary_object(array)->array_length;
620
+ return normal(oseo_undefined());
621
+ }
622
+ OseoValue slots[2] = {array, oseo_undefined()};
623
+ OseoRootFrame frame = {NULL, slots, 2u};
624
+ oseo_roots_push(context, &frame);
625
+ OseoResult result = oseo_internal_ascii_string(context, "length");
626
+ slots[1] = result.value;
627
+ if (result.status == OSEO_STATUS_NORMAL) {
628
+ result = oseo_object_get(context, slots[0], slots[1]);
629
+ slots[1] = result.value;
630
+ }
631
+ if (result.status == OSEO_STATUS_NORMAL) {
632
+ result = oseo_internal_to_number(context, slots[1]);
633
+ }
634
+ if (result.status == OSEO_STATUS_NORMAL) {
635
+ double value = number_value(result.value);
636
+ if (value > 0.0) {
637
+ value = floor(value);
638
+ if (value > OSEO_INDEX_LIMIT) value = OSEO_INDEX_LIMIT;
639
+ if (value > (double)SIZE_MAX) value = (double)SIZE_MAX;
640
+ *length = (size_t)value;
641
+ }
642
+ }
643
+ oseo_roots_pop(context, &frame);
644
+ return result.status == OSEO_STATUS_NORMAL
645
+ ? normal(oseo_number((double)*length))
646
+ : result;
647
+ }
648
+
649
+ static OseoResult json_own_enumerable(
650
+ OseoContext *context,
651
+ OseoValue object,
652
+ OseoValue key,
653
+ bool *enumerable
654
+ ) {
655
+ OseoValue ignored = oseo_undefined();
656
+ OseoValue getter = oseo_undefined();
657
+ OseoValue setter = oseo_undefined();
658
+ OseoPropertyAttributes attributes = {false, false, false, false};
659
+ bool found = false;
660
+ bool numeric = false;
661
+ OseoResult element = oseo_internal_typed_array_own_property(
662
+ context,
663
+ object,
664
+ key,
665
+ &numeric,
666
+ &found
667
+ );
668
+ if (element.status != OSEO_STATUS_NORMAL || numeric) {
669
+ /* A valid TypedArray element is always enumerable. */
670
+ *enumerable = element.status == OSEO_STATUS_NORMAL && found;
671
+ return element;
672
+ }
673
+ OseoResult result = is_proxy(object)
674
+ ? oseo_internal_proxy_get_own_property(
675
+ context,
676
+ object,
677
+ key,
678
+ &found,
679
+ &ignored,
680
+ &attributes,
681
+ &getter,
682
+ &setter
683
+ )
684
+ : normal(oseo_boolean(
685
+ (found = oseo_internal_own_property_descriptor(
686
+ context,
687
+ object,
688
+ key,
689
+ &ignored,
690
+ &attributes,
691
+ &getter,
692
+ &setter
693
+ ))
694
+ ));
695
+ if (result.status == OSEO_STATUS_NORMAL && found) {
696
+ result = oseo_internal_namespace_export_ready(
697
+ context, object, ignored);
698
+ }
699
+ *enumerable = result.status == OSEO_STATUS_NORMAL &&
700
+ found && attributes.enumerable;
701
+ return result;
702
+ }
703
+
704
+ /*
705
+ * The record of one child of a container whose own record still
706
+ * describes it: element `index` of an array's children or the member
707
+ * `key` of an object's map. A child added after parsing has none.
708
+ */
709
+ static OseoResult json_child_record(
710
+ OseoContext *context,
711
+ OseoValue children,
712
+ OseoValue key,
713
+ size_t index,
714
+ bool array
715
+ ) {
716
+ if (!is_object(children)) return normal(oseo_undefined());
717
+ if (array && index >= ordinary_object(children)->array_length) {
718
+ return normal(oseo_undefined());
719
+ }
720
+ /* Both maps are internal own data properties, so no user code runs. */
721
+ return oseo_object_get(context, children, key);
722
+ }
723
+
724
+ /*
725
+ * InternalizeJSONProperty with a JSON Parse Record. The record applies
726
+ * only while the holder's property still has the parsed value: then a
727
+ * primitive's context gets its `source` text and a container's children
728
+ * keep their records. A value a reviver replaced has neither.
729
+ */
730
+ static OseoResult json_internalize(
731
+ OseoContext *context,
732
+ OseoValue holder,
733
+ OseoValue name,
734
+ OseoValue reviver,
735
+ OseoValue record,
736
+ OseoValue source,
737
+ size_t depth
738
+ ) {
739
+ if (depth >= OSEO_MAX_CALL_DEPTH) {
740
+ return oseo_internal_throw_error(
741
+ context,
742
+ OSEO_ERROR_RANGE,
743
+ "Maximum JSON reviver depth exceeded."
744
+ );
745
+ }
746
+ OseoRootFrame frame = {NULL, NULL, 0u};
747
+ OseoResult result = oseo_roots_allocate(context, &frame, 14u);
748
+ if (result.status != OSEO_STATUS_NORMAL) return result;
749
+ /* 9: the record, 10: the source text, 11: the children, 12: the
750
+ * reviver's context object, 13: a child record. */
751
+ frame.slots[0] = holder;
752
+ frame.slots[1] = name;
753
+ frame.slots[2] = reviver;
754
+ frame.slots[9] = record;
755
+ frame.slots[10] = source;
756
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
757
+ frame.slots[3] = result.value;
758
+ if (result.status == OSEO_STATUS_NORMAL) {
759
+ result = oseo_object_literal_create(context);
760
+ frame.slots[12] = result.value;
761
+ }
762
+ if (result.status == OSEO_STATUS_NORMAL &&
763
+ is_environment(frame.slots[9]) &&
764
+ oseo_internal_same_value(
765
+ json_record_slot(frame.slots[9], JSON_RECORD_VALUE),
766
+ frame.slots[3]
767
+ )) {
768
+ frame.slots[11] = json_record_slot(
769
+ frame.slots[9],
770
+ JSON_RECORD_CHILDREN
771
+ );
772
+ if (!is_object(frame.slots[3])) {
773
+ size_t start = (size_t)number_value(
774
+ json_record_slot(frame.slots[9], JSON_RECORD_START));
775
+ size_t end = (size_t)number_value(
776
+ json_record_slot(frame.slots[9], JSON_RECORD_END));
777
+ result = oseo_string_from_units(
778
+ context,
779
+ string_object(frame.slots[10])->units + start,
780
+ end - start
781
+ );
782
+ frame.slots[13] = result.value;
783
+ if (result.status == OSEO_STATUS_NORMAL) {
784
+ result = oseo_internal_ascii_string(context, "source");
785
+ frame.slots[6] = result.value;
786
+ }
787
+ if (result.status == OSEO_STATUS_NORMAL) {
788
+ result = oseo_object_define(
789
+ context,
790
+ frame.slots[12],
791
+ frame.slots[6],
792
+ frame.slots[13],
793
+ (OseoPropertyAttributes){true, true, true, false}
794
+ );
795
+ }
796
+ }
797
+ }
798
+ if (result.status == OSEO_STATUS_NORMAL && is_object(frame.slots[3])) {
799
+ result = oseo_internal_is_array(context, frame.slots[3]);
800
+ bool array = result.status == OSEO_STATUS_NORMAL &&
801
+ oseo_to_boolean(result.value);
802
+ if (result.status == OSEO_STATUS_NORMAL && array) {
803
+ size_t length = 0u;
804
+ result = json_array_length(context, frame.slots[3], &length);
805
+ for (size_t index = 0u;
806
+ result.status == OSEO_STATUS_NORMAL && index < length;
807
+ index += 1u) {
808
+ result = json_index_key(context, index);
809
+ frame.slots[6] = result.value;
810
+ if (result.status != OSEO_STATUS_NORMAL) break;
811
+ result = json_child_record(
812
+ context,
813
+ frame.slots[11],
814
+ frame.slots[6],
815
+ index,
816
+ true
817
+ );
818
+ frame.slots[13] = result.value;
819
+ if (result.status != OSEO_STATUS_NORMAL) break;
820
+ result = json_internalize(
821
+ context,
822
+ frame.slots[3],
823
+ frame.slots[6],
824
+ frame.slots[2],
825
+ frame.slots[13],
826
+ frame.slots[10],
827
+ depth + 1u
828
+ );
829
+ frame.slots[7] = result.value;
830
+ if (result.status != OSEO_STATUS_NORMAL) break;
831
+ if (tag_of(frame.slots[7]) == OSEO_TAG_UNDEFINED) {
832
+ result = oseo_object_delete(
833
+ context,
834
+ frame.slots[3],
835
+ frame.slots[6],
836
+ false
837
+ );
838
+ } else {
839
+ result = json_define_revived(
840
+ context,
841
+ frame.slots[3],
842
+ frame.slots[6],
843
+ frame.slots[7]
844
+ );
845
+ }
846
+ }
847
+ } else if (result.status == OSEO_STATUS_NORMAL) {
848
+ result = oseo_internal_own_key_array(
849
+ context,
850
+ frame.slots[3],
851
+ OSEO_OWN_KEY_STRINGS
852
+ );
853
+ frame.slots[4] = result.value;
854
+ if (result.status == OSEO_STATUS_NORMAL) {
855
+ result = oseo_argument_list_create(context);
856
+ frame.slots[5] = result.value;
857
+ }
858
+ size_t key_count = result.status == OSEO_STATUS_NORMAL
859
+ ? ordinary_object(frame.slots[4])->array_length
860
+ : 0u;
861
+ for (size_t index = 0u;
862
+ result.status == OSEO_STATUS_NORMAL && index < key_count;
863
+ index += 1u) {
864
+ result = json_index_key(context, index);
865
+ frame.slots[6] = result.value;
866
+ if (result.status == OSEO_STATUS_NORMAL) {
867
+ result = oseo_object_get(
868
+ context,
869
+ frame.slots[4],
870
+ frame.slots[6]
871
+ );
872
+ frame.slots[6] = result.value;
873
+ }
874
+ if (result.status != OSEO_STATUS_NORMAL) break;
875
+ bool enumerable = false;
876
+ result = json_own_enumerable(
877
+ context,
878
+ frame.slots[3],
879
+ frame.slots[6],
880
+ &enumerable
881
+ );
882
+ if (result.status == OSEO_STATUS_NORMAL && enumerable) {
883
+ result = oseo_argument_list_append(
884
+ context,
885
+ frame.slots[5],
886
+ frame.slots[6]
887
+ );
888
+ }
889
+ }
890
+ size_t enumerable_count = 0u;
891
+ if (result.status == OSEO_STATUS_NORMAL) {
892
+ const OseoValue *ignored = NULL;
893
+ result = oseo_argument_list_view(
894
+ context,
895
+ frame.slots[5],
896
+ &enumerable_count,
897
+ &ignored
898
+ );
899
+ }
900
+ for (size_t index = 0u;
901
+ result.status == OSEO_STATUS_NORMAL &&
902
+ index < enumerable_count;
903
+ index += 1u) {
904
+ size_t current_count = 0u;
905
+ const OseoValue *keys = NULL;
906
+ result = oseo_argument_list_view(
907
+ context,
908
+ frame.slots[5],
909
+ &current_count,
910
+ &keys
911
+ );
912
+ if (result.status != OSEO_STATUS_NORMAL ||
913
+ index >= current_count) {
914
+ break;
915
+ }
916
+ frame.slots[6] = keys[index];
917
+ result = json_child_record(
918
+ context,
919
+ frame.slots[11],
920
+ frame.slots[6],
921
+ 0u,
922
+ false
923
+ );
924
+ frame.slots[13] = result.value;
925
+ if (result.status != OSEO_STATUS_NORMAL) break;
926
+ result = json_internalize(
927
+ context,
928
+ frame.slots[3],
929
+ frame.slots[6],
930
+ frame.slots[2],
931
+ frame.slots[13],
932
+ frame.slots[10],
933
+ depth + 1u
934
+ );
935
+ frame.slots[7] = result.value;
936
+ if (result.status != OSEO_STATUS_NORMAL) break;
937
+ if (tag_of(frame.slots[7]) == OSEO_TAG_UNDEFINED) {
938
+ result = oseo_object_delete(
939
+ context,
940
+ frame.slots[3],
941
+ frame.slots[6],
942
+ false
943
+ );
944
+ } else {
945
+ result = json_define_revived(
946
+ context,
947
+ frame.slots[3],
948
+ frame.slots[6],
949
+ frame.slots[7]
950
+ );
951
+ }
952
+ }
953
+ }
954
+ }
955
+ if (result.status == OSEO_STATUS_NORMAL) {
956
+ /* name, value, and context occupy slots 7 to 9. */
957
+ frame.slots[7] = frame.slots[1];
958
+ frame.slots[8] = frame.slots[3];
959
+ frame.slots[9] = frame.slots[12];
960
+ result = oseo_call_function(
961
+ context,
962
+ frame.slots[2],
963
+ frame.slots[0],
964
+ 3u,
965
+ &frame.slots[7],
966
+ oseo_undefined()
967
+ );
968
+ }
969
+ oseo_roots_release(context, &frame);
970
+ return result;
971
+ }
972
+
973
+ static OseoValue json_argument(
974
+ size_t argument_count,
975
+ const OseoValue *arguments,
976
+ size_t index
977
+ ) {
978
+ return index < argument_count ? arguments[index] : oseo_undefined();
979
+ }
980
+
981
+ static OseoResult json_parse(
982
+ OseoContext *context,
983
+ size_t argument_count,
984
+ const OseoValue *arguments
985
+ ) {
986
+ OseoRootFrame frame = {NULL, NULL, 0u};
987
+ OseoResult result = oseo_roots_allocate(context, &frame, 7u);
988
+ if (result.status != OSEO_STATUS_NORMAL) return result;
989
+ /* 5: the root's JSON Parse Record, 6: the source text. */
990
+ frame.slots[0] = json_argument(argument_count, arguments, 0u);
991
+ frame.slots[1] = json_argument(argument_count, arguments, 1u);
992
+ /* IsCallable runs no user code, so asking it before parsing decides
993
+ * whether the parser builds records without changing any order. */
994
+ bool reviving = is_callable(frame.slots[1]);
995
+ result = oseo_to_string(context, frame.slots[0]);
996
+ frame.slots[2] = result.value;
997
+ frame.slots[6] = result.value;
998
+ if (result.status == OSEO_STATUS_NORMAL) {
999
+ result = json_parse_text(
1000
+ context,
1001
+ frame.slots[2],
1002
+ reviving ? &frame.slots[5] : NULL
1003
+ );
1004
+ frame.slots[3] = result.value;
1005
+ }
1006
+ if (result.status == OSEO_STATUS_NORMAL && reviving) {
1007
+ result = oseo_object_literal_create(context);
1008
+ frame.slots[4] = result.value;
1009
+ if (result.status == OSEO_STATUS_NORMAL) {
1010
+ result = oseo_internal_ascii_string(context, "");
1011
+ frame.slots[2] = result.value;
1012
+ }
1013
+ if (result.status == OSEO_STATUS_NORMAL) {
1014
+ result = oseo_object_define(
1015
+ context,
1016
+ frame.slots[4],
1017
+ frame.slots[2],
1018
+ frame.slots[3],
1019
+ (OseoPropertyAttributes){true, true, true, false}
1020
+ );
1021
+ }
1022
+ if (result.status == OSEO_STATUS_NORMAL) {
1023
+ result = json_internalize(
1024
+ context,
1025
+ frame.slots[4],
1026
+ frame.slots[2],
1027
+ frame.slots[1],
1028
+ frame.slots[5],
1029
+ frame.slots[6],
1030
+ 0u
1031
+ );
1032
+ }
1033
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1034
+ result.value = frame.slots[3];
1035
+ }
1036
+ oseo_roots_release(context, &frame);
1037
+ return result;
1038
+ }
1039
+
1040
+ typedef struct OseoJsonAncestor OseoJsonAncestor;
1041
+ struct OseoJsonAncestor {
1042
+ OseoValue value;
1043
+ const OseoJsonAncestor *parent;
1044
+ };
1045
+
1046
+ typedef struct {
1047
+ OseoContext *context;
1048
+ OseoValue replacer;
1049
+ OseoValue property_list;
1050
+ OseoValue to_json_key;
1051
+ uint16_t gap[10];
1052
+ size_t gap_length;
1053
+ OseoStringBuilder *builder;
1054
+ } OseoJsonStringifier;
1055
+
1056
+ static bool json_is_property_list(OseoValue value) {
1057
+ return tag_of(value) == OSEO_TAG_HEAP &&
1058
+ heap_object(value)->kind == OSEO_HEAP_ARGUMENT_LIST;
1059
+ }
1060
+
1061
+ static OseoResult json_append_units(OseoJsonStringifier *stringifier,
1062
+ const uint16_t *units,
1063
+ size_t length) {
1064
+ return oseo_internal_string_builder_append(
1065
+ stringifier->context, stringifier->builder, units, length);
1066
+ }
1067
+
1068
+ static OseoResult json_append_ascii(OseoJsonStringifier *stringifier,
1069
+ const char *text) {
1070
+ size_t length = strlen(text);
1071
+ uint16_t units[16];
1072
+ if (length > sizeof(units) / sizeof(units[0])) {
1073
+ return failure(stringifier->context,
1074
+ "OSEO2001",
1075
+ "Internal JSON fragment is too large.");
1076
+ }
1077
+ for (size_t index = 0u; index < length; index += 1u) {
1078
+ units[index] = (uint16_t)(unsigned char)text[index];
1079
+ }
1080
+ return json_append_units(stringifier, units, length);
1081
+ }
1082
+
1083
+ static OseoResult json_quote_string(OseoJsonStringifier *stringifier,
1084
+ OseoValue value) {
1085
+ static const uint16_t hex[] = {'0',
1086
+ '1',
1087
+ '2',
1088
+ '3',
1089
+ '4',
1090
+ '5',
1091
+ '6',
1092
+ '7',
1093
+ '8',
1094
+ '9',
1095
+ 'a',
1096
+ 'b',
1097
+ 'c',
1098
+ 'd',
1099
+ 'e',
1100
+ 'f'};
1101
+ OseoResult result = json_append_ascii(stringifier, "\"");
1102
+ OseoString *string = string_object(value);
1103
+ for (size_t index = 0u;
1104
+ result.status == OSEO_STATUS_NORMAL && index < string->length;
1105
+ index += 1u) {
1106
+ uint16_t unit = string->units[index];
1107
+ const char *escape = NULL;
1108
+ if (unit == UINT16_C('"'))
1109
+ escape = "\\\"";
1110
+ else if (unit == UINT16_C('\\'))
1111
+ escape = "\\\\";
1112
+ else if (unit == UINT16_C(0x0008))
1113
+ escape = "\\b";
1114
+ else if (unit == UINT16_C(0x000c))
1115
+ escape = "\\f";
1116
+ else if (unit == UINT16_C(0x000a))
1117
+ escape = "\\n";
1118
+ else if (unit == UINT16_C(0x000d))
1119
+ escape = "\\r";
1120
+ else if (unit == UINT16_C(0x0009))
1121
+ escape = "\\t";
1122
+ if (escape != NULL) {
1123
+ result = json_append_ascii(stringifier, escape);
1124
+ continue;
1125
+ }
1126
+ bool leading = unit >= UINT16_C(0xd800) && unit <= UINT16_C(0xdbff);
1127
+ bool trailing = unit >= UINT16_C(0xdc00) && unit <= UINT16_C(0xdfff);
1128
+ bool paired = leading && index + 1u < string->length &&
1129
+ string->units[index + 1u] >= UINT16_C(0xdc00) &&
1130
+ string->units[index + 1u] <= UINT16_C(0xdfff);
1131
+ if (unit < UINT16_C(0x0020) || trailing || (leading && !paired)) {
1132
+ uint16_t escaped[] = {'\\',
1133
+ 'u',
1134
+ hex[(unit >> 12u) & 0x0fu],
1135
+ hex[(unit >> 8u) & 0x0fu],
1136
+ hex[(unit >> 4u) & 0x0fu],
1137
+ hex[unit & 0x0fu]};
1138
+ result = json_append_units(stringifier, escaped, 6u);
1139
+ } else {
1140
+ result = json_append_units(stringifier, &unit, 1u);
1141
+ if (result.status == OSEO_STATUS_NORMAL && paired) {
1142
+ index += 1u;
1143
+ unit = string->units[index];
1144
+ result = json_append_units(stringifier, &unit, 1u);
1145
+ }
1146
+ }
1147
+ }
1148
+ if (result.status == OSEO_STATUS_NORMAL) {
1149
+ result = json_append_ascii(stringifier, "\"");
1150
+ }
1151
+ return result;
1152
+ }
1153
+
1154
+ static OseoResult json_append_indent(OseoJsonStringifier *stringifier,
1155
+ size_t depth) {
1156
+ if (stringifier->gap_length == 0u) {
1157
+ return normal(oseo_undefined());
1158
+ }
1159
+ OseoResult result = json_append_ascii(stringifier, "\n");
1160
+ for (size_t index = 0u;
1161
+ result.status == OSEO_STATUS_NORMAL && index < depth;
1162
+ index += 1u) {
1163
+ result = json_append_units(
1164
+ stringifier, stringifier->gap, stringifier->gap_length);
1165
+ }
1166
+ return result;
1167
+ }
1168
+
1169
+ static bool json_has_ancestor(const OseoJsonAncestor *ancestor,
1170
+ OseoValue value) {
1171
+ while (ancestor != NULL) {
1172
+ if (ancestor->value == value) return true;
1173
+ ancestor = ancestor->parent;
1174
+ }
1175
+ return false;
1176
+ }
1177
+
1178
+ static OseoResult json_serialize_property(OseoJsonStringifier *stringifier,
1179
+ OseoValue holder,
1180
+ OseoValue key,
1181
+ const OseoJsonAncestor *ancestor,
1182
+ size_t depth,
1183
+ bool *serialized);
1184
+
1185
+ static OseoResult json_cycle_error(OseoContext *context) {
1186
+ return oseo_internal_throw_error(
1187
+ context, OSEO_ERROR_TYPE, "Converting circular structure to JSON.");
1188
+ }
1189
+
1190
+ static OseoResult json_serialize_array(OseoJsonStringifier *stringifier,
1191
+ OseoValue value,
1192
+ const OseoJsonAncestor *ancestor,
1193
+ size_t depth) {
1194
+ if (json_has_ancestor(ancestor, value)) {
1195
+ return json_cycle_error(stringifier->context);
1196
+ }
1197
+ if (depth >= OSEO_MAX_CALL_DEPTH) {
1198
+ return oseo_internal_throw_error(
1199
+ stringifier->context,
1200
+ OSEO_ERROR_RANGE,
1201
+ "Maximum JSON stringify depth exceeded.");
1202
+ }
1203
+ OseoValue slots[2] = {value, oseo_undefined()};
1204
+ OseoRootFrame frame = {NULL, slots, 2u};
1205
+ oseo_roots_push(stringifier->context, &frame);
1206
+ size_t length = 0u;
1207
+ OseoResult result =
1208
+ json_array_length(stringifier->context, slots[0], &length);
1209
+ OseoJsonAncestor current = {slots[0], ancestor};
1210
+ if (result.status == OSEO_STATUS_NORMAL) {
1211
+ result = json_append_ascii(stringifier, "[");
1212
+ }
1213
+ for (size_t index = 0u;
1214
+ result.status == OSEO_STATUS_NORMAL && index < length;
1215
+ index += 1u) {
1216
+ if (index > 0u) result = json_append_ascii(stringifier, ",");
1217
+ if (result.status == OSEO_STATUS_NORMAL &&
1218
+ stringifier->gap_length > 0u) {
1219
+ result = json_append_indent(stringifier, depth + 1u);
1220
+ }
1221
+ if (result.status != OSEO_STATUS_NORMAL) break;
1222
+ result = json_index_key(stringifier->context, index);
1223
+ slots[1] = result.value;
1224
+ bool element = false;
1225
+ if (result.status == OSEO_STATUS_NORMAL) {
1226
+ result = json_serialize_property(stringifier,
1227
+ slots[0],
1228
+ slots[1],
1229
+ &current,
1230
+ depth + 1u,
1231
+ &element);
1232
+ }
1233
+ if (result.status == OSEO_STATUS_NORMAL && !element) {
1234
+ result = json_append_ascii(stringifier, "null");
1235
+ }
1236
+ }
1237
+ if (result.status == OSEO_STATUS_NORMAL && length > 0u &&
1238
+ stringifier->gap_length > 0u) {
1239
+ result = json_append_indent(stringifier, depth);
1240
+ }
1241
+ if (result.status == OSEO_STATUS_NORMAL) {
1242
+ result = json_append_ascii(stringifier, "]");
1243
+ }
1244
+ oseo_roots_pop(stringifier->context, &frame);
1245
+ return result;
1246
+ }
1247
+
1248
+ static OseoResult json_snapshot_object_keys(OseoJsonStringifier *stringifier,
1249
+ OseoValue object) {
1250
+ if (json_is_property_list(stringifier->property_list)) {
1251
+ return normal(stringifier->property_list);
1252
+ }
1253
+ OseoRootFrame frame = {NULL, NULL, 0u};
1254
+ OseoResult result = oseo_roots_allocate(stringifier->context, &frame, 4u);
1255
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1256
+ frame.slots[0] = object;
1257
+ result = oseo_internal_own_key_array(
1258
+ stringifier->context, frame.slots[0], OSEO_OWN_KEY_STRINGS);
1259
+ frame.slots[1] = result.value;
1260
+ if (result.status == OSEO_STATUS_NORMAL) {
1261
+ result = oseo_argument_list_create(stringifier->context);
1262
+ frame.slots[2] = result.value;
1263
+ }
1264
+ size_t count = result.status == OSEO_STATUS_NORMAL
1265
+ ? ordinary_object(frame.slots[1])->array_length
1266
+ : 0u;
1267
+ for (size_t index = 0u;
1268
+ result.status == OSEO_STATUS_NORMAL && index < count;
1269
+ index += 1u) {
1270
+ result = json_index_key(stringifier->context, index);
1271
+ frame.slots[3] = result.value;
1272
+ if (result.status == OSEO_STATUS_NORMAL) {
1273
+ result = oseo_object_get(
1274
+ stringifier->context, frame.slots[1], frame.slots[3]);
1275
+ frame.slots[3] = result.value;
1276
+ }
1277
+ bool enumerable = false;
1278
+ if (result.status == OSEO_STATUS_NORMAL) {
1279
+ result = json_own_enumerable(stringifier->context,
1280
+ frame.slots[0],
1281
+ frame.slots[3],
1282
+ &enumerable);
1283
+ }
1284
+ if (result.status == OSEO_STATUS_NORMAL && enumerable) {
1285
+ result = oseo_argument_list_append(
1286
+ stringifier->context, frame.slots[2], frame.slots[3]);
1287
+ }
1288
+ }
1289
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[2];
1290
+ oseo_roots_release(stringifier->context, &frame);
1291
+ return result;
1292
+ }
1293
+
1294
+ static OseoResult json_serialize_object(OseoJsonStringifier *stringifier,
1295
+ OseoValue value,
1296
+ const OseoJsonAncestor *ancestor,
1297
+ size_t depth) {
1298
+ if (json_has_ancestor(ancestor, value)) {
1299
+ return json_cycle_error(stringifier->context);
1300
+ }
1301
+ if (depth >= OSEO_MAX_CALL_DEPTH) {
1302
+ return oseo_internal_throw_error(
1303
+ stringifier->context,
1304
+ OSEO_ERROR_RANGE,
1305
+ "Maximum JSON stringify depth exceeded.");
1306
+ }
1307
+ OseoRootFrame frame = {NULL, NULL, 0u};
1308
+ OseoResult result = oseo_roots_allocate(stringifier->context, &frame, 2u);
1309
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1310
+ frame.slots[0] = value;
1311
+ result = json_snapshot_object_keys(stringifier, frame.slots[0]);
1312
+ frame.slots[1] = result.value;
1313
+ OseoArgumentList *keys =
1314
+ result.status == OSEO_STATUS_NORMAL
1315
+ ? (OseoArgumentList *)heap_object(frame.slots[1])
1316
+ : NULL;
1317
+ OseoJsonAncestor current = {frame.slots[0], ancestor};
1318
+ if (result.status == OSEO_STATUS_NORMAL) {
1319
+ result = json_append_ascii(stringifier, "{");
1320
+ }
1321
+ size_t emitted = 0u;
1322
+ for (size_t index = 0u;
1323
+ result.status == OSEO_STATUS_NORMAL && index < keys->length;
1324
+ index += 1u) {
1325
+ OseoStringBuilder member = {NULL, 0u, 0u};
1326
+ OseoStringBuilder *outer = stringifier->builder;
1327
+ stringifier->builder = &member;
1328
+ bool present = false;
1329
+ result = json_serialize_property(stringifier,
1330
+ frame.slots[0],
1331
+ keys->values[index],
1332
+ &current,
1333
+ depth + 1u,
1334
+ &present);
1335
+ stringifier->builder = outer;
1336
+ if (result.status == OSEO_STATUS_NORMAL && present) {
1337
+ if (emitted > 0u) result = json_append_ascii(stringifier, ",");
1338
+ if (result.status == OSEO_STATUS_NORMAL &&
1339
+ stringifier->gap_length > 0u) {
1340
+ result = json_append_indent(stringifier, depth + 1u);
1341
+ }
1342
+ if (result.status == OSEO_STATUS_NORMAL) {
1343
+ result = json_quote_string(stringifier, keys->values[index]);
1344
+ }
1345
+ if (result.status == OSEO_STATUS_NORMAL) {
1346
+ result = json_append_ascii(
1347
+ stringifier, stringifier->gap_length > 0u ? ": " : ":");
1348
+ }
1349
+ if (result.status == OSEO_STATUS_NORMAL) {
1350
+ result =
1351
+ json_append_units(stringifier, member.units, member.length);
1352
+ }
1353
+ emitted += 1u;
1354
+ }
1355
+ oseo_internal_string_builder_release(&member);
1356
+ }
1357
+ if (result.status == OSEO_STATUS_NORMAL && emitted > 0u &&
1358
+ stringifier->gap_length > 0u) {
1359
+ result = json_append_indent(stringifier, depth);
1360
+ }
1361
+ if (result.status == OSEO_STATUS_NORMAL) {
1362
+ result = json_append_ascii(stringifier, "}");
1363
+ }
1364
+ oseo_roots_release(stringifier->context, &frame);
1365
+ return result;
1366
+ }
1367
+
1368
+ static OseoResult json_serialize_property(OseoJsonStringifier *stringifier,
1369
+ OseoValue holder,
1370
+ OseoValue key,
1371
+ const OseoJsonAncestor *ancestor,
1372
+ size_t depth,
1373
+ bool *serialized) {
1374
+ *serialized = false;
1375
+ OseoRootFrame frame = {NULL, NULL, 0u};
1376
+ OseoResult result = oseo_roots_allocate(stringifier->context, &frame, 5u);
1377
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1378
+ frame.slots[0] = holder;
1379
+ frame.slots[1] = key;
1380
+ result =
1381
+ oseo_object_get(stringifier->context, frame.slots[0], frame.slots[1]);
1382
+ frame.slots[2] = result.value;
1383
+ if (result.status == OSEO_STATUS_NORMAL &&
1384
+ (is_object(frame.slots[2]) || is_bigint(frame.slots[2]))) {
1385
+ result = oseo_object_get(
1386
+ stringifier->context, frame.slots[2], stringifier->to_json_key);
1387
+ frame.slots[3] = result.value;
1388
+ if (result.status == OSEO_STATUS_NORMAL &&
1389
+ is_callable(frame.slots[3])) {
1390
+ frame.slots[4] = frame.slots[1];
1391
+ result = oseo_call_function(stringifier->context,
1392
+ frame.slots[3],
1393
+ frame.slots[2],
1394
+ 1u,
1395
+ &frame.slots[4],
1396
+ oseo_undefined());
1397
+ frame.slots[2] = result.value;
1398
+ }
1399
+ }
1400
+ if (result.status == OSEO_STATUS_NORMAL &&
1401
+ is_callable(stringifier->replacer)) {
1402
+ frame.slots[3] = frame.slots[1];
1403
+ frame.slots[4] = frame.slots[2];
1404
+ result = oseo_call_function(stringifier->context,
1405
+ stringifier->replacer,
1406
+ frame.slots[0],
1407
+ 2u,
1408
+ &frame.slots[3],
1409
+ oseo_undefined());
1410
+ frame.slots[2] = result.value;
1411
+ }
1412
+ if (result.status == OSEO_STATUS_NORMAL && is_object(frame.slots[2]) &&
1413
+ heap_object(frame.slots[2])->raw_json) {
1414
+ /* A raw JSON object's frozen own `rawJSON` text is emitted as is;
1415
+ * reading it runs no user code. */
1416
+ result = oseo_internal_ascii_string(stringifier->context, "rawJSON");
1417
+ frame.slots[3] = result.value;
1418
+ if (result.status == OSEO_STATUS_NORMAL) {
1419
+ result = oseo_object_get(
1420
+ stringifier->context, frame.slots[2], frame.slots[3]);
1421
+ frame.slots[3] = result.value;
1422
+ }
1423
+ if (result.status == OSEO_STATUS_NORMAL) {
1424
+ OseoString *text = string_object(frame.slots[3]);
1425
+ result = json_append_units(stringifier, text->units, text->length);
1426
+ *serialized = result.status == OSEO_STATUS_NORMAL;
1427
+ }
1428
+ oseo_roots_release(stringifier->context, &frame);
1429
+ return result;
1430
+ }
1431
+ if (result.status == OSEO_STATUS_NORMAL && is_object(frame.slots[2]) &&
1432
+ !is_proxy(frame.slots[2]) &&
1433
+ heap_object(frame.slots[2])->kind == OSEO_HEAP_OBJECT) {
1434
+ OseoOrdinaryObject *wrapper = ordinary_object(frame.slots[2]);
1435
+ if (wrapper->number_data) {
1436
+ result = oseo_internal_to_number(
1437
+ stringifier->context,
1438
+ frame.slots[2]
1439
+ );
1440
+ frame.slots[2] = result.value;
1441
+ } else if (wrapper->primitive_data) {
1442
+ OseoValue primitive = wrapper->primitive_value;
1443
+ if (is_string(primitive)) {
1444
+ result = oseo_to_string(
1445
+ stringifier->context,
1446
+ frame.slots[2]
1447
+ );
1448
+ frame.slots[2] = result.value;
1449
+ } else if (is_bigint(primitive) ||
1450
+ tag_of(primitive) == OSEO_TAG_BOOLEAN) {
1451
+ frame.slots[2] = primitive;
1452
+ }
1453
+ }
1454
+ }
1455
+ if (result.status == OSEO_STATUS_NORMAL) {
1456
+ uint64_t tag = tag_of(frame.slots[2]);
1457
+ if (tag == OSEO_TAG_NULL) {
1458
+ result = json_append_ascii(stringifier, "null");
1459
+ *serialized = result.status == OSEO_STATUS_NORMAL;
1460
+ } else if (tag == OSEO_TAG_BOOLEAN) {
1461
+ result = json_append_ascii(
1462
+ stringifier,
1463
+ oseo_to_boolean(frame.slots[2]) ? "true" : "false");
1464
+ *serialized = result.status == OSEO_STATUS_NORMAL;
1465
+ } else if (is_string(frame.slots[2])) {
1466
+ result = json_quote_string(stringifier, frame.slots[2]);
1467
+ *serialized = result.status == OSEO_STATUS_NORMAL;
1468
+ } else if (is_number(frame.slots[2])) {
1469
+ double number = number_value(frame.slots[2]);
1470
+ if (!isfinite(number)) {
1471
+ result = json_append_ascii(stringifier, "null");
1472
+ } else {
1473
+ result = oseo_to_string(stringifier->context, frame.slots[2]);
1474
+ frame.slots[3] = result.value;
1475
+ if (result.status == OSEO_STATUS_NORMAL) {
1476
+ OseoString *text = string_object(frame.slots[3]);
1477
+ result = json_append_units(
1478
+ stringifier, text->units, text->length);
1479
+ }
1480
+ }
1481
+ *serialized = result.status == OSEO_STATUS_NORMAL;
1482
+ } else if (is_bigint(frame.slots[2])) {
1483
+ result = oseo_internal_throw_error(
1484
+ stringifier->context,
1485
+ OSEO_ERROR_TYPE,
1486
+ "Do not know how to serialize a BigInt.");
1487
+ } else if (is_object(frame.slots[2]) && !is_callable(frame.slots[2])) {
1488
+ result =
1489
+ oseo_internal_is_array(stringifier->context, frame.slots[2]);
1490
+ if (result.status == OSEO_STATUS_NORMAL) {
1491
+ bool array = oseo_to_boolean(result.value);
1492
+ result =
1493
+ array ? json_serialize_array(
1494
+ stringifier, frame.slots[2], ancestor, depth)
1495
+ : json_serialize_object(
1496
+ stringifier, frame.slots[2], ancestor, depth);
1497
+ *serialized = result.status == OSEO_STATUS_NORMAL;
1498
+ }
1499
+ }
1500
+ }
1501
+ oseo_roots_release(stringifier->context, &frame);
1502
+ return result;
1503
+ }
1504
+
1505
+ static bool json_string_or_number_wrapper(OseoValue value) {
1506
+ if (!is_object(value) || is_proxy(value) ||
1507
+ heap_object(value)->kind != OSEO_HEAP_OBJECT)
1508
+ return false;
1509
+ OseoOrdinaryObject *wrapper = ordinary_object(value);
1510
+ return wrapper->number_data ||
1511
+ (wrapper->primitive_data && is_string(wrapper->primitive_value));
1512
+ }
1513
+
1514
+ static OseoResult json_replacer_property_list(OseoContext *context,
1515
+ OseoValue replacer) {
1516
+ OseoRootFrame frame = {NULL, NULL, 0u};
1517
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1518
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1519
+ frame.slots[0] = replacer;
1520
+ result = oseo_argument_list_create(context);
1521
+ frame.slots[1] = result.value;
1522
+ size_t length = 0u;
1523
+ if (result.status == OSEO_STATUS_NORMAL) {
1524
+ result = json_array_length(context, frame.slots[0], &length);
1525
+ }
1526
+ for (size_t index = 0u;
1527
+ result.status == OSEO_STATUS_NORMAL && index < length;
1528
+ index += 1u) {
1529
+ result = json_index_key(context, index);
1530
+ frame.slots[2] = result.value;
1531
+ if (result.status == OSEO_STATUS_NORMAL) {
1532
+ result = oseo_object_get(context, frame.slots[0], frame.slots[2]);
1533
+ frame.slots[2] = result.value;
1534
+ }
1535
+ if (result.status != OSEO_STATUS_NORMAL) break;
1536
+ if (is_string(frame.slots[2]) || is_number(frame.slots[2]) ||
1537
+ json_string_or_number_wrapper(frame.slots[2])) {
1538
+ result = oseo_to_string(context, frame.slots[2]);
1539
+ frame.slots[2] = result.value;
1540
+ if (result.status != OSEO_STATUS_NORMAL) break;
1541
+ OseoArgumentList *list =
1542
+ (OseoArgumentList *)heap_object(frame.slots[1]);
1543
+ bool duplicate = false;
1544
+ for (size_t item = 0u; item < list->length; item += 1u) {
1545
+ if (oseo_internal_string_equal(list->values[item],
1546
+ frame.slots[2])) {
1547
+ duplicate = true;
1548
+ break;
1549
+ }
1550
+ }
1551
+ if (!duplicate) {
1552
+ result = oseo_argument_list_append(
1553
+ context, frame.slots[1], frame.slots[2]);
1554
+ }
1555
+ }
1556
+ }
1557
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
1558
+ oseo_roots_release(context, &frame);
1559
+ return result;
1560
+ }
1561
+
1562
+ static OseoResult json_stringify(OseoContext *context,
1563
+ size_t argument_count,
1564
+ const OseoValue *arguments) {
1565
+ OseoRootFrame frame = {NULL, NULL, 0u};
1566
+ OseoResult result = oseo_roots_allocate(context, &frame, 8u);
1567
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1568
+ frame.slots[0] = json_argument(argument_count, arguments, 0u);
1569
+ frame.slots[1] = json_argument(argument_count, arguments, 1u);
1570
+ frame.slots[2] = json_argument(argument_count, arguments, 2u);
1571
+ frame.slots[3] =
1572
+ is_callable(frame.slots[1]) ? frame.slots[1] : oseo_undefined();
1573
+ frame.slots[4] = oseo_undefined();
1574
+ if (!is_callable(frame.slots[1])) {
1575
+ result = oseo_internal_is_array(context, frame.slots[1]);
1576
+ if (result.status == OSEO_STATUS_NORMAL &&
1577
+ oseo_to_boolean(result.value)) {
1578
+ result = json_replacer_property_list(context, frame.slots[1]);
1579
+ frame.slots[4] = result.value;
1580
+ }
1581
+ }
1582
+ OseoJsonStringifier stringifier = {context,
1583
+ frame.slots[3],
1584
+ frame.slots[4],
1585
+ oseo_undefined(),
1586
+ {0u},
1587
+ 0u,
1588
+ NULL};
1589
+ if (result.status == OSEO_STATUS_NORMAL &&
1590
+ json_string_or_number_wrapper(frame.slots[2])) {
1591
+ OseoOrdinaryObject *wrapper = ordinary_object(frame.slots[2]);
1592
+ result = wrapper->number_data
1593
+ ? oseo_internal_to_number(context, frame.slots[2])
1594
+ : oseo_to_string(context, frame.slots[2]);
1595
+ frame.slots[2] = result.value;
1596
+ }
1597
+ if (result.status == OSEO_STATUS_NORMAL && is_number(frame.slots[2])) {
1598
+ double width = number_value(frame.slots[2]);
1599
+ if (isnan(width) || width <= 0.0)
1600
+ width = 0.0;
1601
+ else if (width >= 10.0)
1602
+ width = 10.0;
1603
+ else
1604
+ width = floor(width);
1605
+ stringifier.gap_length = (size_t)width;
1606
+ for (size_t index = 0u; index < stringifier.gap_length; index += 1u) {
1607
+ stringifier.gap[index] = UINT16_C(' ');
1608
+ }
1609
+ } else if (result.status == OSEO_STATUS_NORMAL &&
1610
+ is_string(frame.slots[2])) {
1611
+ OseoString *gap = string_object(frame.slots[2]);
1612
+ stringifier.gap_length = gap->length < 10u ? gap->length : 10u;
1613
+ memcpy(stringifier.gap,
1614
+ gap->units,
1615
+ stringifier.gap_length * sizeof(uint16_t));
1616
+ }
1617
+ if (result.status == OSEO_STATUS_NORMAL) {
1618
+ result = oseo_internal_ascii_string(context, "toJSON");
1619
+ frame.slots[5] = result.value;
1620
+ stringifier.to_json_key = frame.slots[5];
1621
+ }
1622
+ if (result.status == OSEO_STATUS_NORMAL) {
1623
+ result = oseo_object_literal_create(context);
1624
+ frame.slots[6] = result.value;
1625
+ }
1626
+ if (result.status == OSEO_STATUS_NORMAL) {
1627
+ result = oseo_internal_ascii_string(context, "");
1628
+ frame.slots[7] = result.value;
1629
+ }
1630
+ if (result.status == OSEO_STATUS_NORMAL) {
1631
+ result = oseo_object_define(
1632
+ context,
1633
+ frame.slots[6],
1634
+ frame.slots[7],
1635
+ frame.slots[0],
1636
+ (OseoPropertyAttributes){true, true, true, false});
1637
+ }
1638
+ OseoStringBuilder builder = {NULL, 0u, 0u};
1639
+ stringifier.builder = &builder;
1640
+ bool serialized = false;
1641
+ if (result.status == OSEO_STATUS_NORMAL) {
1642
+ result = json_serialize_property(&stringifier,
1643
+ frame.slots[6],
1644
+ frame.slots[7],
1645
+ NULL,
1646
+ 0u,
1647
+ &serialized);
1648
+ }
1649
+ if (result.status == OSEO_STATUS_NORMAL && serialized) {
1650
+ result = oseo_string_from_units(context, builder.units, builder.length);
1651
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1652
+ result = normal(oseo_undefined());
1653
+ }
1654
+ oseo_internal_string_builder_release(&builder);
1655
+ oseo_roots_release(context, &frame);
1656
+ return result;
1657
+ }
1658
+
1659
+ static bool json_raw_edge(uint16_t unit) {
1660
+ return unit == UINT16_C(0x09) || unit == UINT16_C(0x0a) ||
1661
+ unit == UINT16_C(0x0d) || unit == UINT16_C(0x20);
1662
+ }
1663
+
1664
+ /*
1665
+ * JSON.rawJSON(text): the text must be one JSON null, boolean, number,
1666
+ * or string with no surrounding whitespace. The result is a frozen
1667
+ * null-prototype object with [[IsRawJSON]] and one `rawJSON` property.
1668
+ */
1669
+ static OseoResult json_raw_json(
1670
+ OseoContext *context,
1671
+ size_t argument_count,
1672
+ const OseoValue *arguments
1673
+ ) {
1674
+ OseoRootFrame frame = {NULL, NULL, 0u};
1675
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1676
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1677
+ result = oseo_to_string(
1678
+ context,
1679
+ json_argument(argument_count, arguments, 0u)
1680
+ );
1681
+ frame.slots[0] = result.value;
1682
+ if (result.status == OSEO_STATUS_NORMAL) {
1683
+ OseoString *text = string_object(frame.slots[0]);
1684
+ if (text->length == 0u || json_raw_edge(text->units[0]) ||
1685
+ json_raw_edge(text->units[text->length - 1u]) ||
1686
+ text->units[0] == UINT16_C('[') ||
1687
+ text->units[0] == UINT16_C('{')) {
1688
+ result = oseo_internal_throw_error(
1689
+ context,
1690
+ OSEO_ERROR_SYNTAX,
1691
+ "Invalid raw JSON text."
1692
+ );
1693
+ }
1694
+ }
1695
+ if (result.status == OSEO_STATUS_NORMAL) {
1696
+ result = json_parse_text(context, frame.slots[0], NULL);
1697
+ }
1698
+ if (result.status == OSEO_STATUS_NORMAL) {
1699
+ result = oseo_object_create(context, oseo_null());
1700
+ frame.slots[1] = result.value;
1701
+ }
1702
+ if (result.status == OSEO_STATUS_NORMAL) {
1703
+ result = oseo_internal_ascii_string(context, "rawJSON");
1704
+ frame.slots[2] = result.value;
1705
+ }
1706
+ if (result.status == OSEO_STATUS_NORMAL) {
1707
+ /* CreateDataPropertyOrThrow and then SetIntegrityLevel frozen. */
1708
+ result = oseo_object_define(
1709
+ context,
1710
+ frame.slots[1],
1711
+ frame.slots[2],
1712
+ frame.slots[0],
1713
+ (OseoPropertyAttributes){false, true, false, false}
1714
+ );
1715
+ }
1716
+ if (result.status == OSEO_STATUS_NORMAL) {
1717
+ ordinary_object(frame.slots[1])->extensible = false;
1718
+ heap_object(frame.slots[1])->raw_json = true;
1719
+ result = normal(frame.slots[1]);
1720
+ }
1721
+ oseo_roots_release(context, &frame);
1722
+ return result;
1723
+ }
1724
+
1725
+ OseoResult oseo_internal_json_builtin_dispatch(OseoContext *context,
1726
+ size_t code_id,
1727
+ OseoValue callee,
1728
+ OseoValue receiver,
1729
+ size_t argument_count,
1730
+ const OseoValue *arguments,
1731
+ OseoValue new_target) {
1732
+ (void)callee;
1733
+ if (code_id == OSEO_JSON_STRINGIFY_CODE_ID) {
1734
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1735
+ return oseo_internal_throw_error(
1736
+ context,
1737
+ OSEO_ERROR_TYPE,
1738
+ "JSON.stringify is not a constructor.");
1739
+ }
1740
+ return json_stringify(context, argument_count, arguments);
1741
+ }
1742
+ (void)receiver;
1743
+ if (code_id == OSEO_JSON_RAW_JSON_CODE_ID ||
1744
+ code_id == OSEO_JSON_IS_RAW_JSON_CODE_ID) {
1745
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1746
+ return oseo_internal_throw_error(
1747
+ context,
1748
+ OSEO_ERROR_TYPE,
1749
+ "JSON built-in is not a constructor.");
1750
+ }
1751
+ if (code_id == OSEO_JSON_RAW_JSON_CODE_ID) {
1752
+ return json_raw_json(context, argument_count, arguments);
1753
+ }
1754
+ OseoValue value = json_argument(argument_count, arguments, 0u);
1755
+ return normal(oseo_boolean(
1756
+ is_object(value) && heap_object(value)->raw_json));
1757
+ }
1758
+ if (code_id != OSEO_JSON_PARSE_CODE_ID) {
1759
+ return oseo_unknown_function(context, code_id);
1760
+ }
1761
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1762
+ return oseo_internal_throw_error(
1763
+ context, OSEO_ERROR_TYPE, "JSON.parse is not a constructor.");
1764
+ }
1765
+ return json_parse(context, argument_count, arguments);
1766
+ }
1767
+
1768
+ static OseoResult json_parse_function(OseoContext *context) {
1769
+ static const uint16_t name[] = {'p', 'a', 'r', 's', 'e'};
1770
+ OseoValue environment = oseo_undefined();
1771
+ OseoRootFrame frame = {NULL, &environment, 1u};
1772
+ oseo_roots_push(context, &frame);
1773
+ OseoResult result = oseo_environment_create(context, 0u);
1774
+ environment = result.value;
1775
+ if (result.status == OSEO_STATUS_NORMAL) {
1776
+ result = oseo_function_create(
1777
+ context,
1778
+ OSEO_JSON_PARSE_CODE_ID,
1779
+ environment,
1780
+ name,
1781
+ sizeof(name) / sizeof(name[0]),
1782
+ 2u,
1783
+ OSEO_FUNCTION_INTERNAL,
1784
+ oseo_undefined(),
1785
+ oseo_undefined(),
1786
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1787
+ );
1788
+ }
1789
+ oseo_roots_pop(context, &frame);
1790
+ return result;
1791
+ }
1792
+
1793
+ static OseoResult json_stringify_function(OseoContext *context) {
1794
+ static const uint16_t name[] = {
1795
+ 's', 't', 'r', 'i', 'n', 'g', 'i', 'f', 'y'
1796
+ };
1797
+ OseoValue environment = oseo_undefined();
1798
+ OseoRootFrame frame = {NULL, &environment, 1u};
1799
+ oseo_roots_push(context, &frame);
1800
+ OseoResult result = oseo_environment_create(context, 0u);
1801
+ environment = result.value;
1802
+ if (result.status == OSEO_STATUS_NORMAL) {
1803
+ result = oseo_function_create(
1804
+ context,
1805
+ OSEO_JSON_STRINGIFY_CODE_ID,
1806
+ environment,
1807
+ name,
1808
+ sizeof(name) / sizeof(name[0]),
1809
+ 3u,
1810
+ OSEO_FUNCTION_INTERNAL,
1811
+ oseo_undefined(),
1812
+ oseo_undefined(),
1813
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1814
+ );
1815
+ }
1816
+ oseo_roots_pop(context, &frame);
1817
+ return result;
1818
+ }
1819
+
1820
+ /* JSON.rawJSON and JSON.isRawJSON own no intrinsic slot. */
1821
+ static OseoResult json_define_method(
1822
+ OseoContext *context,
1823
+ OseoValue namespace_object,
1824
+ size_t code_id,
1825
+ const char *name
1826
+ ) {
1827
+ size_t name_length = strlen(name);
1828
+ uint16_t units[16];
1829
+ for (size_t index = 0u; index < name_length; index += 1u) {
1830
+ units[index] = (uint16_t)(unsigned char)name[index];
1831
+ }
1832
+ OseoValue slots[3] = {namespace_object, oseo_undefined(), oseo_undefined()};
1833
+ OseoRootFrame frame = {NULL, slots, 3u};
1834
+ oseo_roots_push(context, &frame);
1835
+ OseoResult result = oseo_environment_create(context, 0u);
1836
+ slots[1] = result.value;
1837
+ if (result.status == OSEO_STATUS_NORMAL) {
1838
+ result = oseo_function_create(
1839
+ context,
1840
+ code_id,
1841
+ slots[1],
1842
+ units,
1843
+ name_length,
1844
+ 1u,
1845
+ OSEO_FUNCTION_INTERNAL,
1846
+ oseo_undefined(),
1847
+ oseo_undefined(),
1848
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1849
+ );
1850
+ slots[1] = result.value;
1851
+ }
1852
+ if (result.status == OSEO_STATUS_NORMAL) {
1853
+ result = oseo_internal_ascii_string(context, name);
1854
+ slots[2] = result.value;
1855
+ }
1856
+ if (result.status == OSEO_STATUS_NORMAL) {
1857
+ result = oseo_object_define(
1858
+ context,
1859
+ slots[0],
1860
+ slots[2],
1861
+ slots[1],
1862
+ (OseoPropertyAttributes){true, false, true, false}
1863
+ );
1864
+ }
1865
+ oseo_roots_pop(context, &frame);
1866
+ return result;
1867
+ }
1868
+
1869
+ OseoResult oseo_internal_json_intrinsic(OseoContext *context) {
1870
+ OseoValue *slot = &context->realm->intrinsics[OSEO_INTRINSIC_JSON];
1871
+ if (is_object(*slot)) return normal(*slot);
1872
+ size_t entry_allocations = context->allocations;
1873
+ OseoRootFrame frame = {NULL, NULL, 0u};
1874
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
1875
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1876
+ result = oseo_internal_intrinsic(
1877
+ context,
1878
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1879
+ );
1880
+ frame.slots[0] = result.value;
1881
+ if (result.status == OSEO_STATUS_NORMAL) {
1882
+ result = oseo_object_create(context, frame.slots[0]);
1883
+ frame.slots[0] = result.value;
1884
+ }
1885
+ if (result.status == OSEO_STATUS_NORMAL) {
1886
+ *slot = frame.slots[0];
1887
+ result = json_parse_function(context);
1888
+ frame.slots[1] = result.value;
1889
+ }
1890
+ if (result.status == OSEO_STATUS_NORMAL) {
1891
+ context->realm->intrinsics[OSEO_INTRINSIC_JSON_PARSE] = frame.slots[1];
1892
+ result = oseo_internal_ascii_string(context, "parse");
1893
+ frame.slots[2] = result.value;
1894
+ }
1895
+ if (result.status == OSEO_STATUS_NORMAL) {
1896
+ result = oseo_object_define(
1897
+ context,
1898
+ frame.slots[0],
1899
+ frame.slots[2],
1900
+ frame.slots[1],
1901
+ (OseoPropertyAttributes){true, false, true, false}
1902
+ );
1903
+ }
1904
+ if (result.status == OSEO_STATUS_NORMAL) {
1905
+ result = json_stringify_function(context);
1906
+ frame.slots[1] = result.value;
1907
+ }
1908
+ if (result.status == OSEO_STATUS_NORMAL) {
1909
+ result = oseo_internal_ascii_string(context, "stringify");
1910
+ frame.slots[2] = result.value;
1911
+ }
1912
+ if (result.status == OSEO_STATUS_NORMAL) {
1913
+ result = oseo_object_define(
1914
+ context,
1915
+ frame.slots[0],
1916
+ frame.slots[2],
1917
+ frame.slots[1],
1918
+ (OseoPropertyAttributes){true, false, true, false}
1919
+ );
1920
+ }
1921
+ if (result.status == OSEO_STATUS_NORMAL) {
1922
+ result = json_define_method(
1923
+ context,
1924
+ frame.slots[0],
1925
+ OSEO_JSON_RAW_JSON_CODE_ID,
1926
+ "rawJSON"
1927
+ );
1928
+ }
1929
+ if (result.status == OSEO_STATUS_NORMAL) {
1930
+ result = json_define_method(
1931
+ context,
1932
+ frame.slots[0],
1933
+ OSEO_JSON_IS_RAW_JSON_CODE_ID,
1934
+ "isRawJSON"
1935
+ );
1936
+ }
1937
+ if (result.status == OSEO_STATUS_NORMAL) {
1938
+ result = oseo_internal_well_known_symbol(
1939
+ context,
1940
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1941
+ );
1942
+ frame.slots[2] = result.value;
1943
+ }
1944
+ if (result.status == OSEO_STATUS_NORMAL) {
1945
+ result = oseo_internal_ascii_string(context, "JSON");
1946
+ frame.slots[3] = result.value;
1947
+ }
1948
+ if (result.status == OSEO_STATUS_NORMAL) {
1949
+ result = oseo_object_define(
1950
+ context,
1951
+ frame.slots[0],
1952
+ frame.slots[2],
1953
+ frame.slots[3],
1954
+ (OseoPropertyAttributes){true, false, false, false}
1955
+ );
1956
+ }
1957
+ if (result.status != OSEO_STATUS_NORMAL) {
1958
+ *slot = oseo_undefined();
1959
+ context->realm->intrinsics[OSEO_INTRINSIC_JSON_PARSE] =
1960
+ oseo_undefined();
1961
+ } else if (context->observe_specialization) {
1962
+ context->allocations = entry_allocations;
1963
+ }
1964
+ oseo_roots_release(context, &frame);
1965
+ return result.status == OSEO_STATUS_NORMAL ? normal(*slot) : result;
1966
+ }
1967
+
1968
+ OseoResult oseo_internal_install_json_global(
1969
+ OseoContext *context,
1970
+ OseoValue global
1971
+ ) {
1972
+ OseoValue slots[3] = {global, oseo_undefined(), oseo_undefined()};
1973
+ OseoRootFrame frame = {NULL, slots, 3u};
1974
+ oseo_roots_push(context, &frame);
1975
+ OseoResult result = oseo_internal_json_intrinsic(context);
1976
+ slots[1] = result.value;
1977
+ if (result.status == OSEO_STATUS_NORMAL) {
1978
+ result = oseo_internal_ascii_string(context, "JSON");
1979
+ slots[2] = result.value;
1980
+ }
1981
+ if (result.status == OSEO_STATUS_NORMAL) {
1982
+ result = oseo_object_define(
1983
+ context,
1984
+ slots[0],
1985
+ slots[2],
1986
+ slots[1],
1987
+ (OseoPropertyAttributes){true, false, true, false}
1988
+ );
1989
+ }
1990
+ oseo_roots_pop(context, &frame);
1991
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1992
+ }