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

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