@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,3877 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ /*
11
+ * The `Object` built-ins, the `Object.prototype` methods, and the own-key
12
+ * operations they share with object rest and spread: ToPropertyDescriptor,
13
+ * FromPropertyDescriptor, CopyDataProperties, own-key ordering, and the
14
+ * `Object.create`, `Object.defineProperty`, `Object.defineProperties`,
15
+ * `Object.getOwnPropertyDescriptor`,
16
+ * `Object.getOwnPropertyDescriptors`, `Object.keys`, and
17
+ * `Object.setPrototypeOf` entry points.
18
+ */
19
+
20
+ static OseoResult type_error(OseoContext *context, const char *message) {
21
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
22
+ }
23
+
24
+ /*
25
+ * [[GetOwnProperty]] for a non-Proxy object. A TypedArray answers its
26
+ * canonical numeric keys from the view, whose elements live outside the
27
+ * property vector; every other key reads the stored descriptor. A BigInt
28
+ * element is freshly allocated, so a caller that keeps `*value` across a
29
+ * safepoint roots it.
30
+ */
31
+ static OseoResult own_property_query(
32
+ OseoContext *context,
33
+ OseoValue object_value,
34
+ OseoValue key,
35
+ bool *found,
36
+ OseoValue *value,
37
+ OseoPropertyAttributes *attributes,
38
+ OseoValue *getter,
39
+ OseoValue *setter
40
+ ) {
41
+ bool numeric = false;
42
+ OseoResult result = oseo_internal_typed_array_own_property(
43
+ context,
44
+ object_value,
45
+ key,
46
+ &numeric,
47
+ found
48
+ );
49
+ if (result.status != OSEO_STATUS_NORMAL) return result;
50
+ if (numeric) {
51
+ *value = result.value;
52
+ *attributes = (OseoPropertyAttributes){true, true, true, false};
53
+ *getter = oseo_undefined();
54
+ *setter = oseo_undefined();
55
+ return normal(oseo_undefined());
56
+ }
57
+ *found = oseo_internal_own_property_descriptor(
58
+ context,
59
+ object_value,
60
+ key,
61
+ value,
62
+ attributes,
63
+ getter,
64
+ setter
65
+ );
66
+ if (!*found) return normal(oseo_undefined());
67
+ result = oseo_internal_legacy_value(context, object_value, key, *value);
68
+ if (result.status != OSEO_STATUS_NORMAL) return result;
69
+ *value = result.value;
70
+ return oseo_internal_namespace_export_ready(
71
+ context, object_value, *value);
72
+ }
73
+
74
+ static OseoValue builtin_argument(
75
+ size_t argument_count,
76
+ const OseoValue *arguments,
77
+ size_t index
78
+ ) {
79
+ return index < argument_count ? arguments[index] : oseo_undefined();
80
+ }
81
+
82
+ static OseoResult create_object_prototype_function(
83
+ OseoContext *context,
84
+ size_t code_id,
85
+ const char *name,
86
+ size_t length
87
+ );
88
+ static OseoResult object_get_own_property_descriptors(
89
+ OseoContext *context,
90
+ size_t argument_count,
91
+ const OseoValue *arguments
92
+ );
93
+ static OseoResult object_define_properties(
94
+ OseoContext *context,
95
+ size_t argument_count,
96
+ const OseoValue *arguments
97
+ );
98
+ static OseoResult object_assign(
99
+ OseoContext *context,
100
+ size_t argument_count,
101
+ const OseoValue *arguments
102
+ );
103
+ static OseoResult object_entries(
104
+ OseoContext *context,
105
+ size_t argument_count,
106
+ const OseoValue *arguments
107
+ );
108
+ static OseoResult object_from_entries(
109
+ OseoContext *context,
110
+ size_t argument_count,
111
+ const OseoValue *arguments
112
+ );
113
+ static OseoResult object_get_own_property_names(
114
+ OseoContext *context,
115
+ size_t argument_count,
116
+ const OseoValue *arguments
117
+ );
118
+ static OseoResult object_get_own_property_symbols(
119
+ OseoContext *context,
120
+ size_t argument_count,
121
+ const OseoValue *arguments
122
+ );
123
+ static OseoResult object_group_by(
124
+ OseoContext *context,
125
+ size_t argument_count,
126
+ const OseoValue *arguments
127
+ );
128
+ static OseoResult object_has_own(
129
+ OseoContext *context,
130
+ size_t argument_count,
131
+ const OseoValue *arguments
132
+ );
133
+ static OseoResult object_values(
134
+ OseoContext *context,
135
+ size_t argument_count,
136
+ const OseoValue *arguments
137
+ );
138
+ static OseoResult object_prototype_has_own_property(
139
+ OseoContext *context,
140
+ OseoValue receiver,
141
+ size_t argument_count,
142
+ const OseoValue *arguments
143
+ ) {
144
+ OseoResult key = oseo_property_key(
145
+ context,
146
+ builtin_argument(argument_count, arguments, 0u)
147
+ );
148
+ if (key.status != OSEO_STATUS_NORMAL) return key;
149
+ return oseo_object_has_own(context, receiver, key.value);
150
+ }
151
+
152
+ static OseoResult object_prototype_property_is_enumerable(
153
+ OseoContext *context,
154
+ OseoValue receiver,
155
+ size_t argument_count,
156
+ const OseoValue *arguments
157
+ ) {
158
+ OseoResult key = oseo_property_key(
159
+ context,
160
+ builtin_argument(argument_count, arguments, 0u)
161
+ );
162
+ if (key.status != OSEO_STATUS_NORMAL) return key;
163
+ if (is_nullish(receiver)) {
164
+ return type_error(context, "Cannot convert a nullish value to object.");
165
+ }
166
+ if (is_string(receiver)) {
167
+ bool own = oseo_internal_string_own_property(
168
+ receiver,
169
+ key.value,
170
+ NULL
171
+ );
172
+ bool enumerable = own &&
173
+ !oseo_internal_string_is_ascii(key.value, "length");
174
+ return normal(oseo_boolean(enumerable));
175
+ }
176
+ if (!is_object(receiver)) return normal(oseo_boolean(false));
177
+ OseoValue ignored = oseo_undefined();
178
+ OseoValue ignored_getter = oseo_undefined();
179
+ OseoValue ignored_setter = oseo_undefined();
180
+ OseoPropertyAttributes attributes = {false, false, false, false};
181
+ bool own = false;
182
+ OseoResult result = is_proxy(receiver)
183
+ ? oseo_internal_proxy_get_own_property(
184
+ context,
185
+ receiver,
186
+ key.value,
187
+ &own,
188
+ &ignored,
189
+ &attributes,
190
+ &ignored_getter,
191
+ &ignored_setter
192
+ )
193
+ : own_property_query(
194
+ context,
195
+ receiver,
196
+ key.value,
197
+ &own,
198
+ &ignored,
199
+ &attributes,
200
+ &ignored_getter,
201
+ &ignored_setter
202
+ );
203
+ if (result.status != OSEO_STATUS_NORMAL) return result;
204
+ return normal(oseo_boolean(own && attributes.enumerable));
205
+ }
206
+
207
+ static OseoResult object_prototype_is_prototype_of(
208
+ OseoContext *context,
209
+ OseoValue receiver,
210
+ size_t argument_count,
211
+ const OseoValue *arguments
212
+ ) {
213
+ OseoValue value = builtin_argument(argument_count, arguments, 0u);
214
+ if (!is_object(value)) return normal(oseo_boolean(false));
215
+ if (is_nullish(receiver)) {
216
+ return type_error(context, "Cannot convert a nullish value to object.");
217
+ }
218
+ if (!is_object(receiver)) return normal(oseo_boolean(false));
219
+ OseoResult prototype = oseo_internal_get_prototype(context, value);
220
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
221
+ OseoValue current = prototype.value;
222
+ while (is_object(current)) {
223
+ if (current == receiver) return normal(oseo_boolean(true));
224
+ prototype = oseo_internal_get_prototype(context, current);
225
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
226
+ current = prototype.value;
227
+ }
228
+ return normal(oseo_boolean(false));
229
+ }
230
+
231
+ static const char *object_builtin_tag(OseoValue receiver) {
232
+ if (is_array(receiver)) return "Array";
233
+ if (is_regexp(receiver)) return "RegExp";
234
+ if (is_callable(receiver)) return "Function";
235
+ if (is_object(receiver) && ordinary_object(receiver)->arguments_object) {
236
+ return "Arguments";
237
+ }
238
+ if (is_object(receiver) && ordinary_object(receiver)->error_data) {
239
+ return "Error";
240
+ }
241
+ if (is_object(receiver) && ordinary_object(receiver)->number_data) {
242
+ return "Number";
243
+ }
244
+ if (is_object(receiver) && ordinary_object(receiver)->primitive_data) {
245
+ OseoValue primitive = ordinary_object(receiver)->primitive_value;
246
+ if (is_string(primitive)) return "String";
247
+ if (is_symbol(primitive)) return "Object";
248
+ if (tag_of(primitive) == OSEO_TAG_BOOLEAN) return "Boolean";
249
+ }
250
+ if (is_date(receiver)) return "Date";
251
+ if (is_string(receiver)) return "String";
252
+ if (is_symbol(receiver)) return "Object";
253
+ if (is_number(receiver)) return "Number";
254
+ if (tag_of(receiver) == OSEO_TAG_BOOLEAN) return "Boolean";
255
+ return "Object";
256
+ }
257
+
258
+ static OseoResult object_tag_text(
259
+ OseoContext *context,
260
+ OseoValue tag_value,
261
+ const char *fallback
262
+ ) {
263
+ static const uint16_t prefix[] = {
264
+ '[', 'o', 'b', 'j', 'e', 'c', 't', ' '
265
+ };
266
+ size_t tag_length = is_string(tag_value)
267
+ ? string_object(tag_value)->length
268
+ : strlen(fallback);
269
+ if (tag_length > SIZE_MAX - 9u ||
270
+ tag_length + 9u > SIZE_MAX / sizeof(uint16_t)) {
271
+ return failure(context, "OSEO2001", "Object tag is too long.");
272
+ }
273
+ size_t length = tag_length + 9u;
274
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
275
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
276
+ uint16_t *units = malloc(length * sizeof(*units));
277
+ if (units == NULL) {
278
+ return failure(context, "OSEO2001", "Object tag allocation failed.");
279
+ }
280
+ memcpy(units, prefix, sizeof(prefix));
281
+ if (is_string(tag_value)) {
282
+ memcpy(
283
+ units + 8u,
284
+ string_object(tag_value)->units,
285
+ tag_length * sizeof(*units)
286
+ );
287
+ } else {
288
+ for (size_t index = 0u; index < tag_length; index += 1u) {
289
+ units[index + 8u] = (uint16_t)(unsigned char)fallback[index];
290
+ }
291
+ }
292
+ units[length - 1u] = ']';
293
+ OseoValue slot = tag_value;
294
+ OseoRootFrame frame = {NULL, &slot, 1u};
295
+ oseo_roots_push(context, &frame);
296
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
297
+ oseo_roots_pop(context, &frame);
298
+ free(units);
299
+ return result;
300
+ }
301
+
302
+ static OseoResult object_prototype_to_string(
303
+ OseoContext *context,
304
+ OseoValue receiver
305
+ ) {
306
+ if (tag_of(receiver) == OSEO_TAG_UNDEFINED) {
307
+ return object_tag_text(context, oseo_undefined(), "Undefined");
308
+ }
309
+ if (tag_of(receiver) == OSEO_TAG_NULL) {
310
+ return object_tag_text(context, oseo_undefined(), "Null");
311
+ }
312
+ OseoValue tag_target = receiver;
313
+ bool proxy_array = false;
314
+ while (is_proxy(tag_target)) {
315
+ if (proxy_object(tag_target)->revoked) {
316
+ return type_error(context, "Cannot inspect a revoked Proxy.");
317
+ }
318
+ tag_target = proxy_object(tag_target)->target;
319
+ }
320
+ if (is_proxy(receiver)) proxy_array = is_array(tag_target);
321
+ const char *fallback = proxy_array
322
+ ? "Array"
323
+ : is_callable(receiver)
324
+ ? "Function"
325
+ : object_builtin_tag(is_proxy(receiver) ? receiver : tag_target);
326
+ OseoRootFrame frame = {NULL, NULL, 0u};
327
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
328
+ if (result.status != OSEO_STATUS_NORMAL) return result;
329
+ frame.slots[0] = receiver;
330
+ if (is_object(frame.slots[0])) {
331
+ frame.slots[1] = frame.slots[0];
332
+ } else {
333
+ result = oseo_internal_to_object(context, frame.slots[0]);
334
+ frame.slots[1] = result.value;
335
+ }
336
+ if (result.status == OSEO_STATUS_NORMAL) {
337
+ result = oseo_internal_well_known_symbol(
338
+ context,
339
+ OSEO_WELL_KNOWN_TO_STRING_TAG
340
+ );
341
+ frame.slots[2] = result.value;
342
+ }
343
+ if (result.status == OSEO_STATUS_NORMAL) {
344
+ /* This is Get(O, @@toStringTag) after ToObject, not GetV on the
345
+ * original primitive. A strict getter must receive the box. */
346
+ result = oseo_object_get(context, frame.slots[1], frame.slots[2]);
347
+ frame.slots[3] = result.value;
348
+ }
349
+ if (result.status == OSEO_STATUS_NORMAL) {
350
+ result = object_tag_text(context, frame.slots[3], fallback);
351
+ }
352
+ oseo_roots_release(context, &frame);
353
+ return result;
354
+ }
355
+
356
+ static OseoResult object_prototype_to_locale_string(
357
+ OseoContext *context,
358
+ OseoValue receiver
359
+ ) {
360
+ if (is_nullish(receiver)) {
361
+ return type_error(context, "Cannot convert a nullish value to object.");
362
+ }
363
+ OseoRootFrame frame = {NULL, NULL, 0u};
364
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
365
+ if (result.status != OSEO_STATUS_NORMAL) return result;
366
+ frame.slots[0] = receiver;
367
+ result = oseo_internal_to_object(context, frame.slots[0]);
368
+ frame.slots[1] = result.value;
369
+ if (result.status == OSEO_STATUS_NORMAL) {
370
+ result = oseo_internal_ascii_string(context, "toString");
371
+ }
372
+ frame.slots[2] = result.value;
373
+ if (result.status == OSEO_STATUS_NORMAL) {
374
+ result = oseo_super_get(
375
+ context,
376
+ frame.slots[1],
377
+ frame.slots[2],
378
+ frame.slots[0]
379
+ );
380
+ frame.slots[2] = result.value;
381
+ }
382
+ if (result.status == OSEO_STATUS_NORMAL && !is_callable(frame.slots[2])) {
383
+ result = type_error(context, "The toString property is not callable.");
384
+ }
385
+ if (result.status == OSEO_STATUS_NORMAL) {
386
+ result = oseo_call_function(
387
+ context,
388
+ frame.slots[2],
389
+ frame.slots[0],
390
+ 0u,
391
+ NULL,
392
+ oseo_undefined()
393
+ );
394
+ }
395
+ oseo_roots_release(context, &frame);
396
+ return result;
397
+ }
398
+
399
+ static OseoResult object_prototype_value_of(
400
+ OseoContext *context,
401
+ OseoValue receiver
402
+ ) {
403
+ return oseo_internal_to_object(context, receiver);
404
+ }
405
+
406
+ /* get Object.prototype.__proto__ (20.1.3.8.1). */
407
+ static OseoResult object_prototype_get_proto(
408
+ OseoContext *context,
409
+ OseoValue receiver
410
+ ) {
411
+ OseoResult object = oseo_internal_to_object_for_property(
412
+ context, receiver);
413
+ if (object.status != OSEO_STATUS_NORMAL) return object;
414
+ return oseo_internal_get_prototype(context, object.value);
415
+ }
416
+
417
+ /*
418
+ * set Object.prototype.__proto__ (20.1.3.8.2). A primitive receiver and
419
+ * a prototype that is neither an object nor null leave everything
420
+ * unchanged; a refused [[SetPrototypeOf]] throws the TypeError that
421
+ * oseo_object_set_prototype raises.
422
+ */
423
+ static OseoResult object_prototype_set_proto(
424
+ OseoContext *context,
425
+ OseoValue receiver,
426
+ size_t argument_count,
427
+ const OseoValue *arguments
428
+ ) {
429
+ OseoValue prototype = builtin_argument(argument_count, arguments, 0u);
430
+ if (is_nullish(receiver)) {
431
+ return type_error(
432
+ context,
433
+ "Object.prototype.__proto__ requires a non-nullish receiver."
434
+ );
435
+ }
436
+ if (tag_of(prototype) != OSEO_TAG_NULL && !is_object(prototype)) {
437
+ return normal(oseo_undefined());
438
+ }
439
+ if (!is_object(receiver)) return normal(oseo_undefined());
440
+ OseoResult result = oseo_object_set_prototype(
441
+ context, receiver, prototype);
442
+ if (result.status != OSEO_STATUS_NORMAL) return result;
443
+ return normal(oseo_undefined());
444
+ }
445
+
446
+ /*
447
+ * Object.prototype.__defineGetter__ and __defineSetter__ (20.1.3.9 and
448
+ * 20.1.3.10). The callability check precedes ToPropertyKey, and the
449
+ * definition is DefinePropertyOrThrow of an enumerable, configurable
450
+ * accessor half, so an existing other half survives and a Proxy sees
451
+ * its defineProperty trap.
452
+ */
453
+ static OseoResult object_prototype_define_accessor(
454
+ OseoContext *context,
455
+ OseoValue receiver,
456
+ size_t argument_count,
457
+ const OseoValue *arguments,
458
+ bool setter_half
459
+ ) {
460
+ /* Slots: 0 object, 1 function, 2 key. */
461
+ OseoRootFrame frame = {NULL, NULL, 0u};
462
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
463
+ if (result.status != OSEO_STATUS_NORMAL) return result;
464
+ frame.slots[1] = builtin_argument(argument_count, arguments, 1u);
465
+ result = oseo_internal_to_object(context, receiver);
466
+ frame.slots[0] = result.value;
467
+ if (result.status == OSEO_STATUS_NORMAL && !is_callable(frame.slots[1])) {
468
+ result = type_error(
469
+ context,
470
+ setter_half
471
+ ? "Object.prototype.__defineSetter__ requires a function."
472
+ : "Object.prototype.__defineGetter__ requires a function."
473
+ );
474
+ }
475
+ if (result.status == OSEO_STATUS_NORMAL) {
476
+ result = oseo_property_key(
477
+ context,
478
+ builtin_argument(argument_count, arguments, 0u)
479
+ );
480
+ frame.slots[2] = result.value;
481
+ }
482
+ const char *refusal = NULL;
483
+ if (result.status == OSEO_STATUS_NORMAL) {
484
+ const OseoConvertedDescriptor descriptor = {
485
+ .has_enumerable = true,
486
+ .enumerable = true,
487
+ .has_configurable = true,
488
+ .configurable = true,
489
+ .has_getter = !setter_half,
490
+ .has_setter = setter_half,
491
+ };
492
+ result = oseo_internal_define_converted_property(
493
+ context,
494
+ frame.slots[0],
495
+ frame.slots[2],
496
+ &descriptor,
497
+ oseo_undefined(),
498
+ setter_half ? oseo_undefined() : frame.slots[1],
499
+ setter_half ? frame.slots[1] : oseo_undefined(),
500
+ &refusal
501
+ );
502
+ }
503
+ if (result.status == OSEO_STATUS_NORMAL && refusal != NULL) {
504
+ result = type_error(context, refusal);
505
+ }
506
+ oseo_roots_release(context, &frame);
507
+ if (result.status != OSEO_STATUS_NORMAL) return result;
508
+ return normal(oseo_undefined());
509
+ }
510
+
511
+ /*
512
+ * Object.prototype.__lookupGetter__ and __lookupSetter__ (20.1.3.11 and
513
+ * 20.1.3.12). The walk asks each object's [[GetOwnProperty]] and
514
+ * [[GetPrototypeOf]], so Proxy traps on the chain run in that order, and
515
+ * the first own property found answers even when it is a data property.
516
+ */
517
+ static OseoResult object_prototype_lookup_accessor(
518
+ OseoContext *context,
519
+ OseoValue receiver,
520
+ size_t argument_count,
521
+ const OseoValue *arguments,
522
+ bool setter_half
523
+ ) {
524
+ /* Slots: 0 current object, 1 key. */
525
+ OseoRootFrame frame = {NULL, NULL, 0u};
526
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
527
+ if (result.status != OSEO_STATUS_NORMAL) return result;
528
+ result = oseo_internal_to_object(context, receiver);
529
+ frame.slots[0] = result.value;
530
+ if (result.status == OSEO_STATUS_NORMAL) {
531
+ result = oseo_property_key(
532
+ context,
533
+ builtin_argument(argument_count, arguments, 0u)
534
+ );
535
+ frame.slots[1] = result.value;
536
+ }
537
+ OseoValue answer = oseo_undefined();
538
+ while (result.status == OSEO_STATUS_NORMAL) {
539
+ OseoValue value = oseo_undefined();
540
+ OseoPropertyAttributes attributes = {false, false, false, false};
541
+ OseoValue getter = oseo_undefined();
542
+ OseoValue setter = oseo_undefined();
543
+ bool exists = false;
544
+ result = is_proxy(frame.slots[0])
545
+ ? oseo_internal_proxy_get_own_property(
546
+ context,
547
+ frame.slots[0],
548
+ frame.slots[1],
549
+ &exists,
550
+ &value,
551
+ &attributes,
552
+ &getter,
553
+ &setter
554
+ )
555
+ : own_property_query(
556
+ context,
557
+ frame.slots[0],
558
+ frame.slots[1],
559
+ &exists,
560
+ &value,
561
+ &attributes,
562
+ &getter,
563
+ &setter
564
+ );
565
+ if (result.status != OSEO_STATUS_NORMAL) break;
566
+ if (exists) {
567
+ if (attributes.accessor) answer = setter_half ? setter : getter;
568
+ break;
569
+ }
570
+ result = oseo_internal_get_prototype(context, frame.slots[0]);
571
+ if (result.status != OSEO_STATUS_NORMAL) break;
572
+ if (tag_of(result.value) == OSEO_TAG_NULL) break;
573
+ frame.slots[0] = result.value;
574
+ }
575
+ oseo_roots_release(context, &frame);
576
+ if (result.status != OSEO_STATUS_NORMAL) return result;
577
+ return normal(answer);
578
+ }
579
+
580
+ OseoResult oseo_internal_install_primitive_wrapper_methods(
581
+ OseoContext *context,
582
+ OseoValue prototype,
583
+ bool include_index_of
584
+ ) {
585
+ if (ordinary_object(prototype)->primitive_wrapper_methods_initialized) {
586
+ return normal(prototype);
587
+ }
588
+ static const struct {
589
+ const char *name;
590
+ size_t length;
591
+ } methods[] = {
592
+ {"toString", 0u},
593
+ {"valueOf", 0u},
594
+ {"indexOf", 1u},
595
+ };
596
+ size_t method_count = include_index_of ? 3u : 2u;
597
+ OseoValue slots[3] = {
598
+ prototype,
599
+ oseo_undefined(),
600
+ oseo_undefined(),
601
+ };
602
+ OseoRootFrame frame = {NULL, slots, 3u};
603
+ oseo_roots_push(context, &frame);
604
+ OseoResult result = normal(slots[0]);
605
+ for (size_t index = 0u; index < method_count; index += 1u) {
606
+ result = create_object_prototype_function(
607
+ context,
608
+ OSEO_OBJECT_PRIMITIVE_WRAPPER_METHOD_CODE_ID,
609
+ methods[index].name,
610
+ methods[index].length
611
+ );
612
+ slots[1] = result.value;
613
+ if (result.status == OSEO_STATUS_NORMAL) {
614
+ result = oseo_internal_ascii_string(
615
+ context,
616
+ methods[index].name
617
+ );
618
+ slots[2] = result.value;
619
+ }
620
+ if (result.status == OSEO_STATUS_NORMAL) {
621
+ result = oseo_object_define(
622
+ context,
623
+ slots[0],
624
+ slots[2],
625
+ slots[1],
626
+ (OseoPropertyAttributes){true, false, true, false}
627
+ );
628
+ }
629
+ if (result.status != OSEO_STATUS_NORMAL) break;
630
+ }
631
+ if (result.status == OSEO_STATUS_NORMAL) {
632
+ ordinary_object(slots[0])->primitive_wrapper_methods_initialized =
633
+ true;
634
+ result.value = slots[0];
635
+ }
636
+ oseo_roots_pop(context, &frame);
637
+ return result;
638
+ }
639
+
640
+ OseoResult oseo_internal_primitive_wrapper_prototype(
641
+ OseoContext *context,
642
+ OseoIntrinsic intrinsic
643
+ ) {
644
+ OseoResult result;
645
+ bool created = false;
646
+ /*
647
+ * A prototype whose own constructor node materializes it, including
648
+ * the exotic own properties and the `constructor` property, is
649
+ * reached through the realm intrinsic table rather than built here.
650
+ */
651
+ if (intrinsic == OSEO_INTRINSIC_BOOLEAN_PROTOTYPE ||
652
+ intrinsic == OSEO_INTRINSIC_BIGINT_PROTOTYPE ||
653
+ intrinsic == OSEO_INTRINSIC_NUMBER_PROTOTYPE ||
654
+ intrinsic == OSEO_INTRINSIC_STRING_PROTOTYPE ||
655
+ intrinsic == OSEO_INTRINSIC_SYMBOL_PROTOTYPE) {
656
+ result = oseo_internal_intrinsic(context, intrinsic);
657
+ } else if (is_object(context->realm->intrinsics[intrinsic])) {
658
+ result = normal(context->realm->intrinsics[intrinsic]);
659
+ } else {
660
+ result = oseo_internal_intrinsic(
661
+ context,
662
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
663
+ );
664
+ if (result.status == OSEO_STATUS_NORMAL) {
665
+ result = oseo_object_create(context, result.value);
666
+ }
667
+ if (result.status == OSEO_STATUS_NORMAL) {
668
+ context->realm->intrinsics[intrinsic] = result.value;
669
+ created = true;
670
+ }
671
+ }
672
+ if (result.status != OSEO_STATUS_NORMAL) return result;
673
+ if (intrinsic == OSEO_INTRINSIC_BOOLEAN_PROTOTYPE ||
674
+ intrinsic == OSEO_INTRINSIC_BIGINT_PROTOTYPE ||
675
+ intrinsic == OSEO_INTRINSIC_NUMBER_PROTOTYPE ||
676
+ intrinsic == OSEO_INTRINSIC_STRING_PROTOTYPE ||
677
+ intrinsic == OSEO_INTRINSIC_SYMBOL_PROTOTYPE) {
678
+ return result;
679
+ }
680
+ if (created) {
681
+ OseoOrdinaryObject *prototype = ordinary_object(result.value);
682
+ prototype->primitive_data = true;
683
+ prototype->primitive_value = oseo_boolean(false);
684
+ }
685
+ if (result.status == OSEO_STATUS_NORMAL) {
686
+ result = oseo_internal_install_primitive_wrapper_methods(
687
+ context,
688
+ context->realm->intrinsics[intrinsic],
689
+ false
690
+ );
691
+ }
692
+ if (result.status != OSEO_STATUS_NORMAL && created) {
693
+ context->realm->intrinsics[intrinsic] = oseo_undefined();
694
+ }
695
+ return result;
696
+ }
697
+
698
+ static OseoResult primitive_wrapper_prototype(
699
+ OseoContext *context,
700
+ OseoValue value
701
+ ) {
702
+ OseoIntrinsic intrinsic;
703
+ if (is_number(value)) {
704
+ intrinsic = OSEO_INTRINSIC_NUMBER_PROTOTYPE;
705
+ } else if (is_symbol(value)) {
706
+ intrinsic = OSEO_INTRINSIC_SYMBOL_PROTOTYPE;
707
+ } else if (is_string(value)) {
708
+ intrinsic = OSEO_INTRINSIC_STRING_PROTOTYPE;
709
+ } else if (is_bigint(value)) {
710
+ intrinsic = OSEO_INTRINSIC_BIGINT_PROTOTYPE;
711
+ } else {
712
+ intrinsic = OSEO_INTRINSIC_BOOLEAN_PROTOTYPE;
713
+ }
714
+ return oseo_internal_primitive_wrapper_prototype(context, intrinsic);
715
+ }
716
+
717
+ static OseoResult to_object(
718
+ OseoContext *context,
719
+ OseoValue value,
720
+ bool define_string_properties
721
+ ) {
722
+ if (is_nullish(value)) {
723
+ return type_error(context, "Cannot convert a nullish value to object.");
724
+ }
725
+ if (is_object(value)) return normal(value);
726
+ OseoRootFrame frame = {NULL, NULL, 0u};
727
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
728
+ if (result.status != OSEO_STATUS_NORMAL) return result;
729
+ frame.slots[0] = value;
730
+ result = primitive_wrapper_prototype(context, frame.slots[0]);
731
+ frame.slots[1] = result.value;
732
+ if (result.status == OSEO_STATUS_NORMAL) {
733
+ result = oseo_object_create(context, frame.slots[1]);
734
+ frame.slots[1] = result.value;
735
+ }
736
+ if (result.status == OSEO_STATUS_NORMAL) {
737
+ OseoOrdinaryObject *wrapper = ordinary_object(frame.slots[1]);
738
+ wrapper->primitive_data = true;
739
+ wrapper->primitive_value = frame.slots[0];
740
+ if (is_number(frame.slots[0])) {
741
+ wrapper->number_data = true;
742
+ wrapper->number_value = frame.slots[0];
743
+ }
744
+ if (define_string_properties && is_string(frame.slots[0])) {
745
+ result = oseo_internal_string_wrapper_properties(
746
+ context,
747
+ frame.slots[0],
748
+ frame.slots[1]
749
+ );
750
+ }
751
+ }
752
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
753
+ oseo_roots_release(context, &frame);
754
+ return result;
755
+ }
756
+
757
+ OseoResult oseo_to_object(OseoContext *context, OseoValue value) {
758
+ return to_object(context, value, true);
759
+ }
760
+
761
+ OseoResult oseo_internal_to_object(OseoContext *context, OseoValue value) {
762
+ return to_object(context, value, true);
763
+ }
764
+
765
+ OseoResult oseo_internal_to_object_for_property(
766
+ OseoContext *context,
767
+ OseoValue value
768
+ ) {
769
+ return to_object(context, value, false);
770
+ }
771
+
772
+ /*
773
+ * OrdinaryCreateFromConstructor(newTarget, "%Object.prototype%").
774
+ * GetPrototypeFromConstructor is a real Get rather than a read of the
775
+ * synthetic `prototype` slot, because `Reflect.construct` can hand this
776
+ * a bound function whose own `prototype` a program defined as an
777
+ * accessor. `Object` performs no argument validation before this step,
778
+ * so the read is at the position 20.1.1.1 gives it.
779
+ */
780
+ static OseoResult object_create_from_constructor(
781
+ OseoContext *context,
782
+ OseoValue new_target
783
+ ) {
784
+ OseoValue slots[2] = {new_target, oseo_undefined()};
785
+ OseoRootFrame frame = {NULL, slots, 2u};
786
+ oseo_roots_push(context, &frame);
787
+ OseoResult result = oseo_internal_constructor_prototype(
788
+ context,
789
+ slots[0]
790
+ );
791
+ slots[1] = result.value;
792
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[1])) {
793
+ result = oseo_internal_constructor_realm_default(
794
+ context,
795
+ slots[0],
796
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
797
+ );
798
+ slots[1] = result.value;
799
+ }
800
+ if (result.status == OSEO_STATUS_NORMAL) {
801
+ result = oseo_object_create(context, slots[1]);
802
+ }
803
+ oseo_roots_pop(context, &frame);
804
+ return result;
805
+ }
806
+
807
+ static OseoResult object_constructor(
808
+ OseoContext *context,
809
+ OseoValue callee,
810
+ size_t argument_count,
811
+ const OseoValue *arguments,
812
+ OseoValue new_target
813
+ ) {
814
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED && new_target != callee) {
815
+ return object_create_from_constructor(context, new_target);
816
+ }
817
+ OseoValue value = builtin_argument(argument_count, arguments, 0u);
818
+ if (!is_nullish(value)) return oseo_internal_to_object(context, value);
819
+ /* The new target is absent or the active function itself, so
820
+ * 20.1.1.1 step 2 creates an ordinary object over the realm
821
+ * prototype rather than reading anything. A built-in [[Construct]]
822
+ * never adopts the receiver its caller made, so this creates one
823
+ * even when the caller already had. */
824
+ OseoResult prototype = oseo_internal_intrinsic(
825
+ context,
826
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
827
+ );
828
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
829
+ return oseo_object_create(context, prototype.value);
830
+ }
831
+
832
+ static OseoResult object_get_prototype_of(
833
+ OseoContext *context,
834
+ size_t argument_count,
835
+ const OseoValue *arguments
836
+ ) {
837
+ OseoResult object = oseo_internal_to_object_for_property(
838
+ context,
839
+ builtin_argument(argument_count, arguments, 0u)
840
+ );
841
+ if (object.status != OSEO_STATUS_NORMAL) return object;
842
+ return oseo_internal_get_prototype(context, object.value);
843
+ }
844
+
845
+ static OseoResult object_is(
846
+ size_t argument_count,
847
+ const OseoValue *arguments
848
+ ) {
849
+ OseoValue left = builtin_argument(argument_count, arguments, 0u);
850
+ OseoValue right = builtin_argument(argument_count, arguments, 1u);
851
+ return normal(oseo_boolean(oseo_internal_same_value(left, right)));
852
+ }
853
+
854
+ /*
855
+ * SetIntegrityLevel (7.3.15) over the ordinary and currently admitted exotic
856
+ * object representations. The descriptor component remains authoritative for
857
+ * applying every stored property change, including mapped arguments aliases
858
+ * and module namespace compatibility. Array `length` and function `prototype`
859
+ * are the two own data properties held outside the property vector.
860
+ */
861
+ static OseoResult object_set_integrity_level(
862
+ OseoContext *context,
863
+ OseoValue object_value,
864
+ bool frozen
865
+ ) {
866
+ if (is_proxy(object_value)) {
867
+ OseoValue slots[3] = {
868
+ object_value, oseo_undefined(), oseo_undefined(),
869
+ };
870
+ OseoRootFrame proxy_frame = {NULL, slots, 3u};
871
+ oseo_roots_push(context, &proxy_frame);
872
+ const char *refusal = NULL;
873
+ OseoResult proxy_result = oseo_internal_prevent_extensions_reported(
874
+ context, slots[0], &refusal);
875
+ if (proxy_result.status == OSEO_STATUS_NORMAL && refusal != NULL) {
876
+ proxy_result = type_error(context, refusal);
877
+ }
878
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
879
+ proxy_result = oseo_internal_proxy_own_keys(
880
+ context, slots[0], OSEO_OWN_KEY_ALL);
881
+ slots[1] = proxy_result.value;
882
+ }
883
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
884
+ proxy_result = oseo_internal_array_like_list(
885
+ context, slots[1], &slots[2]);
886
+ }
887
+ size_t key_count = 0u;
888
+ const OseoValue *keys = NULL;
889
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
890
+ proxy_result = oseo_argument_list_view(
891
+ context, slots[2], &key_count, &keys);
892
+ }
893
+ for (size_t index = 0u;
894
+ proxy_result.status == OSEO_STATUS_NORMAL && index < key_count;
895
+ index += 1u) {
896
+ bool found = true;
897
+ OseoValue value = oseo_undefined();
898
+ OseoPropertyAttributes attributes = {false, false, false, false};
899
+ OseoValue getter = oseo_undefined();
900
+ OseoValue setter = oseo_undefined();
901
+ if (frozen) {
902
+ proxy_result = oseo_internal_proxy_get_own_property(
903
+ context, slots[0], keys[index], &found, &value,
904
+ &attributes, &getter, &setter);
905
+ }
906
+ if (proxy_result.status != OSEO_STATUS_NORMAL || !found) continue;
907
+ OseoConvertedDescriptor descriptor = {
908
+ false, false, true, false,
909
+ frozen && !attributes.accessor, false,
910
+ false, false, false,
911
+ };
912
+ refusal = NULL;
913
+ proxy_result = oseo_internal_proxy_define_own_property(
914
+ context, slots[0], keys[index], &descriptor,
915
+ oseo_undefined(), oseo_undefined(), oseo_undefined(),
916
+ &refusal);
917
+ if (proxy_result.status == OSEO_STATUS_NORMAL && refusal != NULL) {
918
+ proxy_result = type_error(context, refusal);
919
+ }
920
+ }
921
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
922
+ proxy_result.value = slots[0];
923
+ }
924
+ oseo_roots_pop(context, &proxy_frame);
925
+ return proxy_result;
926
+ }
927
+ if (is_typed_array(object_value)) {
928
+ /* A view's [[PreventExtensions]] refuses a length that can still
929
+ * change, and each valid index then refuses the non-configurable
930
+ * redefinition DefinePropertyOrThrow asks for. The
931
+ * indices lead the own keys, so no ordinary property changes. */
932
+ const char *refusal = NULL;
933
+ OseoResult prevented = oseo_internal_prevent_extensions_reported(
934
+ context, object_value, &refusal);
935
+ if (prevented.status != OSEO_STATUS_NORMAL) return prevented;
936
+ if (refusal != NULL) return type_error(context, refusal);
937
+ if (oseo_internal_typed_array_index_key_count(object_value) > 0u) {
938
+ return type_error(
939
+ context,
940
+ "A TypedArray element must stay a writable, enumerable, "
941
+ "and configurable data property."
942
+ );
943
+ }
944
+ }
945
+ OseoOrdinaryObject *object = ordinary_object(object_value);
946
+ object->extensible = false;
947
+ if (object->virtual_string_iterator) {
948
+ object->virtual_string_iterator_configurable = false;
949
+ if (frozen) object->virtual_string_iterator_writable = false;
950
+ }
951
+ OseoRootFrame frame = {NULL, NULL, 0u};
952
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
953
+ if (result.status != OSEO_STATUS_NORMAL) return result;
954
+ frame.slots[0] = object_value;
955
+ /* SetIntegrityLevel redefines a live `arguments` or `caller` like
956
+ * any other own property, which fixes its present value. */
957
+ if (is_function(object_value) &&
958
+ function_object(object_value)->legacy_live != 0u) {
959
+ result = oseo_internal_legacy_function_keys(context);
960
+ for (size_t index = 0u;
961
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
962
+ index += 1u) {
963
+ result = oseo_internal_legacy_materialize(
964
+ context,
965
+ frame.slots[0],
966
+ context->legacy_function_keys[index]
967
+ );
968
+ }
969
+ if (result.status != OSEO_STATUS_NORMAL) {
970
+ oseo_roots_release(context, &frame);
971
+ return result;
972
+ }
973
+ object = ordinary_object(frame.slots[0]);
974
+ }
975
+ size_t property_count = object->property_count;
976
+ for (size_t index = 0u;
977
+ result.status == OSEO_STATUS_NORMAL && index < property_count;
978
+ index += 1u) {
979
+ object = ordinary_object(frame.slots[0]);
980
+ OseoProperty property = object->properties[index];
981
+ frame.slots[1] = property.key;
982
+ frame.slots[2] = property.attributes.accessor
983
+ ? property.getter
984
+ : property.value;
985
+ if (!property.attributes.accessor &&
986
+ oseo_internal_cell_backed_property(
987
+ frame.slots[0], frame.slots[2])) {
988
+ result = oseo_cell_get(context, frame.slots[2]);
989
+ frame.slots[2] = result.value;
990
+ }
991
+ OseoPropertyAttributes attributes = property.attributes;
992
+ attributes.configurable = false;
993
+ if (frozen && !attributes.accessor) attributes.writable = false;
994
+ if (result.status == OSEO_STATUS_NORMAL && attributes.accessor) {
995
+ OseoConvertedDescriptor seal = {
996
+ false, false, true, false, false, false, false, false, false,
997
+ };
998
+ const char *refusal = NULL;
999
+ result = oseo_internal_define_converted_property(
1000
+ context, frame.slots[0], frame.slots[1], &seal,
1001
+ oseo_undefined(), oseo_undefined(), oseo_undefined(),
1002
+ &refusal);
1003
+ if (result.status == OSEO_STATUS_NORMAL && refusal != NULL) {
1004
+ result = type_error(context, refusal);
1005
+ }
1006
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1007
+ result = oseo_internal_object_define_data(
1008
+ context,
1009
+ frame.slots[0],
1010
+ frame.slots[1],
1011
+ frame.slots[2],
1012
+ attributes,
1013
+ true
1014
+ );
1015
+ }
1016
+ }
1017
+ if (result.status == OSEO_STATUS_NORMAL && frozen) {
1018
+ object = ordinary_object(frame.slots[0]);
1019
+ if (is_array(frame.slots[0])) object->length_writable = false;
1020
+ if (function_has_prototype_property(frame.slots[0])) {
1021
+ function_object(frame.slots[0])->prototype_writable = false;
1022
+ }
1023
+ }
1024
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
1025
+ oseo_roots_release(context, &frame);
1026
+ return result;
1027
+ }
1028
+
1029
+ /* TestIntegrityLevel (7.3.16), including the two virtual own properties. */
1030
+ static OseoResult object_test_integrity_level(
1031
+ OseoContext *context,
1032
+ OseoValue value,
1033
+ bool frozen
1034
+ ) {
1035
+ if (is_proxy(value)) {
1036
+ OseoValue slots[3] = {value, oseo_undefined(), oseo_undefined()};
1037
+ OseoRootFrame frame = {NULL, slots, 3u};
1038
+ oseo_roots_push(context, &frame);
1039
+ OseoResult result = oseo_internal_is_extensible(context, slots[0]);
1040
+ if (result.status == OSEO_STATUS_NORMAL &&
1041
+ oseo_to_boolean(result.value)) {
1042
+ oseo_roots_pop(context, &frame);
1043
+ return normal(oseo_boolean(false));
1044
+ }
1045
+ if (result.status == OSEO_STATUS_NORMAL) {
1046
+ result = oseo_internal_proxy_own_keys(
1047
+ context, slots[0], OSEO_OWN_KEY_ALL);
1048
+ slots[1] = result.value;
1049
+ }
1050
+ if (result.status == OSEO_STATUS_NORMAL) {
1051
+ result = oseo_internal_array_like_list(
1052
+ context, slots[1], &slots[2]);
1053
+ }
1054
+ size_t key_count = 0u;
1055
+ const OseoValue *keys = NULL;
1056
+ if (result.status == OSEO_STATUS_NORMAL) {
1057
+ result = oseo_argument_list_view(
1058
+ context, slots[2], &key_count, &keys);
1059
+ }
1060
+ bool intact = true;
1061
+ for (size_t index = 0u;
1062
+ result.status == OSEO_STATUS_NORMAL && intact &&
1063
+ index < key_count;
1064
+ index += 1u) {
1065
+ bool found = false;
1066
+ OseoValue property = oseo_undefined();
1067
+ OseoPropertyAttributes attributes = {false, false, false, false};
1068
+ OseoValue getter = oseo_undefined();
1069
+ OseoValue setter = oseo_undefined();
1070
+ result = oseo_internal_proxy_get_own_property(
1071
+ context, slots[0], keys[index], &found, &property, &attributes,
1072
+ &getter, &setter);
1073
+ if (result.status == OSEO_STATUS_NORMAL && found &&
1074
+ (attributes.configurable ||
1075
+ (frozen && !attributes.accessor && attributes.writable))) {
1076
+ intact = false;
1077
+ }
1078
+ }
1079
+ if (result.status == OSEO_STATUS_NORMAL) {
1080
+ result = normal(oseo_boolean(intact));
1081
+ }
1082
+ oseo_roots_pop(context, &frame);
1083
+ return result;
1084
+ }
1085
+ OseoOrdinaryObject *object = ordinary_object(value);
1086
+ if (object->extensible) return normal(oseo_boolean(false));
1087
+ /* Every valid TypedArray element is configurable and writable. */
1088
+ if (oseo_internal_typed_array_index_key_count(value) > 0u) {
1089
+ return normal(oseo_boolean(false));
1090
+ }
1091
+ if (frozen && is_array(value) && object->length_writable) {
1092
+ return normal(oseo_boolean(false));
1093
+ }
1094
+ if (frozen && function_has_prototype_property(value) &&
1095
+ function_object(value)->prototype_writable) {
1096
+ return normal(oseo_boolean(false));
1097
+ }
1098
+ /* A live `arguments` or `caller` is configurable (ADR 0034). */
1099
+ if (is_function(value) && function_object(value)->legacy_live != 0u) {
1100
+ return normal(oseo_boolean(false));
1101
+ }
1102
+ if (object->virtual_string_iterator &&
1103
+ (object->virtual_string_iterator_configurable ||
1104
+ (frozen && object->virtual_string_iterator_writable))) {
1105
+ return normal(oseo_boolean(false));
1106
+ }
1107
+ /* A module namespace lists its exports in [[OwnPropertyKeys]] order,
1108
+ * so this walk reads each export's binding exactly where
1109
+ * TestIntegrityLevel's [[GetOwnProperty]] would. */
1110
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
1111
+ OseoResult ready = oseo_internal_namespace_export_ready(
1112
+ context, value, ordinary_object(value)->properties[index].value);
1113
+ if (ready.status != OSEO_STATUS_NORMAL) return ready;
1114
+ object = ordinary_object(value);
1115
+ OseoPropertyAttributes attributes =
1116
+ object->properties[index].attributes;
1117
+ if (attributes.configurable ||
1118
+ (frozen && !attributes.accessor && attributes.writable)) {
1119
+ return normal(oseo_boolean(false));
1120
+ }
1121
+ }
1122
+ return normal(oseo_boolean(true));
1123
+ }
1124
+
1125
+ static OseoResult object_integrity_transition(
1126
+ OseoContext *context,
1127
+ size_t argument_count,
1128
+ const OseoValue *arguments,
1129
+ bool frozen
1130
+ ) {
1131
+ OseoValue value = builtin_argument(argument_count, arguments, 0u);
1132
+ if (!is_object(value)) return normal(value);
1133
+ return object_set_integrity_level(context, value, frozen);
1134
+ }
1135
+
1136
+ static OseoResult object_integrity_query(
1137
+ OseoContext *context,
1138
+ size_t argument_count,
1139
+ const OseoValue *arguments,
1140
+ bool frozen
1141
+ ) {
1142
+ OseoValue value = builtin_argument(argument_count, arguments, 0u);
1143
+ return !is_object(value)
1144
+ ? normal(oseo_boolean(true))
1145
+ : object_test_integrity_level(context, value, frozen);
1146
+ }
1147
+
1148
+ static OseoResult object_is_extensible(
1149
+ OseoContext *context,
1150
+ size_t argument_count,
1151
+ const OseoValue *arguments
1152
+ ) {
1153
+ OseoValue value = builtin_argument(argument_count, arguments, 0u);
1154
+ return is_object(value)
1155
+ ? oseo_internal_is_extensible(context, value)
1156
+ : normal(oseo_boolean(false));
1157
+ }
1158
+
1159
+ static OseoResult object_prevent_extensions(
1160
+ OseoContext *context,
1161
+ size_t argument_count,
1162
+ const OseoValue *arguments
1163
+ ) {
1164
+ OseoValue value = builtin_argument(argument_count, arguments, 0u);
1165
+ if (!is_object(value)) return normal(value);
1166
+ const char *refusal = NULL;
1167
+ OseoResult result = oseo_internal_prevent_extensions_reported(
1168
+ context, value, &refusal);
1169
+ if (result.status != OSEO_STATUS_NORMAL || refusal == NULL) return result;
1170
+ return type_error(context, refusal);
1171
+ }
1172
+
1173
+ OseoResult oseo_internal_object_builtin_dispatch(
1174
+ OseoContext *context,
1175
+ size_t code_id,
1176
+ OseoValue callee,
1177
+ OseoValue receiver,
1178
+ size_t argument_count,
1179
+ const OseoValue *arguments,
1180
+ OseoValue new_target
1181
+ ) {
1182
+ if (code_id == OSEO_OBJECT_CONSTRUCTOR_CODE_ID) {
1183
+ return object_constructor(
1184
+ context,
1185
+ callee,
1186
+ argument_count,
1187
+ arguments,
1188
+ new_target
1189
+ );
1190
+ }
1191
+ if (code_id == OSEO_OBJECT_PRIMITIVE_WRAPPER_METHOD_CODE_ID) {
1192
+ return failure(
1193
+ context,
1194
+ "OSEO2001",
1195
+ "Primitive wrapper prototype methods are not admitted yet."
1196
+ );
1197
+ }
1198
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1199
+ return type_error(
1200
+ context,
1201
+ "Object static method is not a constructor."
1202
+ );
1203
+ }
1204
+ if (code_id == OSEO_OBJECT_PROTO_GET_CODE_ID) {
1205
+ return object_prototype_get_proto(context, receiver);
1206
+ }
1207
+ if (code_id == OSEO_OBJECT_PROTO_SET_CODE_ID) {
1208
+ return object_prototype_set_proto(
1209
+ context, receiver, argument_count, arguments);
1210
+ }
1211
+ if (code_id == OSEO_OBJECT_DEFINE_GETTER_CODE_ID ||
1212
+ code_id == OSEO_OBJECT_DEFINE_SETTER_CODE_ID) {
1213
+ return object_prototype_define_accessor(
1214
+ context, receiver, argument_count, arguments,
1215
+ code_id == OSEO_OBJECT_DEFINE_SETTER_CODE_ID);
1216
+ }
1217
+ if (code_id == OSEO_OBJECT_LOOKUP_GETTER_CODE_ID ||
1218
+ code_id == OSEO_OBJECT_LOOKUP_SETTER_CODE_ID) {
1219
+ return object_prototype_lookup_accessor(
1220
+ context, receiver, argument_count, arguments,
1221
+ code_id == OSEO_OBJECT_LOOKUP_SETTER_CODE_ID);
1222
+ }
1223
+ if (code_id == OSEO_OBJECT_GET_PROTOTYPE_OF_CODE_ID) {
1224
+ return object_get_prototype_of(context, argument_count, arguments);
1225
+ }
1226
+ if (code_id == OSEO_OBJECT_IS_CODE_ID) {
1227
+ return object_is(argument_count, arguments);
1228
+ }
1229
+ if (code_id == OSEO_OBJECT_SET_PROTOTYPE_OF_CODE_ID) {
1230
+ return oseo_object_builtin_set_prototype_of(
1231
+ context,
1232
+ argument_count,
1233
+ arguments
1234
+ );
1235
+ }
1236
+ if (code_id == OSEO_OBJECT_CREATE_CODE_ID) {
1237
+ return oseo_object_builtin_create(
1238
+ context,
1239
+ argument_count,
1240
+ arguments
1241
+ );
1242
+ }
1243
+ if (code_id == OSEO_OBJECT_DEFINE_PROPERTY_CODE_ID) {
1244
+ return oseo_object_builtin_define_property(
1245
+ context,
1246
+ argument_count,
1247
+ arguments
1248
+ );
1249
+ }
1250
+ if (code_id == OSEO_OBJECT_DEFINE_PROPERTIES_CODE_ID) {
1251
+ return object_define_properties(
1252
+ context,
1253
+ argument_count,
1254
+ arguments
1255
+ );
1256
+ }
1257
+ if (code_id == OSEO_OBJECT_GET_OWN_PROPERTY_DESCRIPTOR_CODE_ID) {
1258
+ return oseo_object_builtin_get_own_property_descriptor(
1259
+ context,
1260
+ argument_count,
1261
+ arguments
1262
+ );
1263
+ }
1264
+ if (code_id == OSEO_OBJECT_GET_OWN_PROPERTY_DESCRIPTORS_CODE_ID) {
1265
+ return object_get_own_property_descriptors(
1266
+ context,
1267
+ argument_count,
1268
+ arguments
1269
+ );
1270
+ }
1271
+ if (code_id == OSEO_OBJECT_FREEZE_CODE_ID) {
1272
+ return object_integrity_transition(
1273
+ context, argument_count, arguments, true);
1274
+ }
1275
+ if (code_id == OSEO_OBJECT_IS_EXTENSIBLE_CODE_ID) {
1276
+ return object_is_extensible(context, argument_count, arguments);
1277
+ }
1278
+ if (code_id == OSEO_OBJECT_IS_FROZEN_CODE_ID) {
1279
+ return object_integrity_query(
1280
+ context, argument_count, arguments, true);
1281
+ }
1282
+ if (code_id == OSEO_OBJECT_IS_SEALED_CODE_ID) {
1283
+ return object_integrity_query(
1284
+ context, argument_count, arguments, false);
1285
+ }
1286
+ if (code_id == OSEO_OBJECT_PREVENT_EXTENSIONS_CODE_ID) {
1287
+ return object_prevent_extensions(context, argument_count, arguments);
1288
+ }
1289
+ if (code_id == OSEO_OBJECT_SEAL_CODE_ID) {
1290
+ return object_integrity_transition(
1291
+ context, argument_count, arguments, false);
1292
+ }
1293
+ if (code_id == OSEO_OBJECT_ASSIGN_CODE_ID) {
1294
+ return object_assign(context, argument_count, arguments);
1295
+ }
1296
+ if (code_id == OSEO_OBJECT_ENTRIES_CODE_ID) {
1297
+ return object_entries(context, argument_count, arguments);
1298
+ }
1299
+ if (code_id == OSEO_OBJECT_FROM_ENTRIES_CODE_ID) {
1300
+ return object_from_entries(context, argument_count, arguments);
1301
+ }
1302
+ if (code_id == OSEO_OBJECT_GET_OWN_PROPERTY_NAMES_CODE_ID) {
1303
+ return object_get_own_property_names(
1304
+ context, argument_count, arguments);
1305
+ }
1306
+ if (code_id == OSEO_OBJECT_GET_OWN_PROPERTY_SYMBOLS_CODE_ID) {
1307
+ return object_get_own_property_symbols(
1308
+ context, argument_count, arguments);
1309
+ }
1310
+ if (code_id == OSEO_OBJECT_GROUP_BY_CODE_ID) {
1311
+ return object_group_by(context, argument_count, arguments);
1312
+ }
1313
+ if (code_id == OSEO_OBJECT_HAS_OWN_CODE_ID) {
1314
+ return object_has_own(context, argument_count, arguments);
1315
+ }
1316
+ if (code_id == OSEO_OBJECT_KEYS_CODE_ID) {
1317
+ return oseo_object_builtin_keys(context, argument_count, arguments);
1318
+ }
1319
+ if (code_id == OSEO_OBJECT_VALUES_CODE_ID) {
1320
+ return object_values(context, argument_count, arguments);
1321
+ }
1322
+ if (code_id == OSEO_OBJECT_HAS_OWN_PROPERTY_CODE_ID) {
1323
+ return object_prototype_has_own_property(
1324
+ context,
1325
+ receiver,
1326
+ argument_count,
1327
+ arguments
1328
+ );
1329
+ }
1330
+ if (code_id == OSEO_OBJECT_IS_PROTOTYPE_OF_CODE_ID) {
1331
+ return object_prototype_is_prototype_of(
1332
+ context,
1333
+ receiver,
1334
+ argument_count,
1335
+ arguments
1336
+ );
1337
+ }
1338
+ if (code_id == OSEO_OBJECT_PROPERTY_IS_ENUMERABLE_CODE_ID) {
1339
+ return object_prototype_property_is_enumerable(
1340
+ context,
1341
+ receiver,
1342
+ argument_count,
1343
+ arguments
1344
+ );
1345
+ }
1346
+ if (code_id == OSEO_OBJECT_TO_STRING_CODE_ID) {
1347
+ return object_prototype_to_string(context, receiver);
1348
+ }
1349
+ if (code_id == OSEO_OBJECT_TO_LOCALE_STRING_CODE_ID) {
1350
+ return object_prototype_to_locale_string(context, receiver);
1351
+ }
1352
+ if (code_id == OSEO_OBJECT_VALUE_OF_CODE_ID) {
1353
+ return object_prototype_value_of(context, receiver);
1354
+ }
1355
+ return oseo_unknown_function(context, code_id);
1356
+ }
1357
+
1358
+ static OseoResult create_object_prototype_function(
1359
+ OseoContext *context,
1360
+ size_t code_id,
1361
+ const char *name,
1362
+ size_t length
1363
+ ) {
1364
+ size_t name_length = strlen(name);
1365
+ if (name_length > 31u) {
1366
+ return failure(context, "OSEO2001", "Built-in name is too long.");
1367
+ }
1368
+ uint16_t units[31];
1369
+ for (size_t index = 0u; index < name_length; index += 1u) {
1370
+ units[index] = (uint16_t)(unsigned char)name[index];
1371
+ }
1372
+ OseoValue environment = oseo_undefined();
1373
+ OseoRootFrame frame = {NULL, &environment, 1u};
1374
+ oseo_roots_push(context, &frame);
1375
+ OseoResult result = oseo_environment_create(context, 0u);
1376
+ environment = result.value;
1377
+ if (result.status == OSEO_STATUS_NORMAL) {
1378
+ result = oseo_function_create(
1379
+ context,
1380
+ code_id,
1381
+ environment,
1382
+ units,
1383
+ name_length,
1384
+ length,
1385
+ OSEO_FUNCTION_INTERNAL,
1386
+ oseo_undefined(),
1387
+ oseo_undefined(),
1388
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1389
+ );
1390
+ }
1391
+ oseo_roots_pop(context, &frame);
1392
+ return result;
1393
+ }
1394
+
1395
+ static OseoResult object_prototype_function(
1396
+ OseoContext *context,
1397
+ OseoIntrinsic intrinsic,
1398
+ size_t code_id,
1399
+ const char *name,
1400
+ size_t length
1401
+ ) {
1402
+ OseoValue *cache = &context->realm->intrinsics[intrinsic];
1403
+ if (is_function(*cache)) return normal(*cache);
1404
+ OseoResult result = create_object_prototype_function(
1405
+ context,
1406
+ code_id,
1407
+ name,
1408
+ length
1409
+ );
1410
+ if (result.status == OSEO_STATUS_NORMAL) *cache = result.value;
1411
+ return result;
1412
+ }
1413
+
1414
+ static OseoResult object_constructor_function(OseoContext *context) {
1415
+ static const uint16_t name[] = {'O', 'b', 'j', 'e', 'c', 't'};
1416
+ OseoValue environment = oseo_undefined();
1417
+ OseoRootFrame frame = {NULL, &environment, 1u};
1418
+ oseo_roots_push(context, &frame);
1419
+ OseoResult result = oseo_environment_create(context, 0u);
1420
+ environment = result.value;
1421
+ if (result.status == OSEO_STATUS_NORMAL) {
1422
+ result = oseo_function_create(
1423
+ context,
1424
+ OSEO_OBJECT_CONSTRUCTOR_CODE_ID,
1425
+ environment,
1426
+ name,
1427
+ sizeof(name) / sizeof(*name),
1428
+ 1u,
1429
+ OSEO_FUNCTION_ORDINARY,
1430
+ oseo_undefined(),
1431
+ oseo_undefined(),
1432
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1433
+ );
1434
+ }
1435
+ oseo_roots_pop(context, &frame);
1436
+ return result;
1437
+ }
1438
+
1439
+ OseoResult oseo_internal_object_prototype(OseoContext *context) {
1440
+ OseoValue prototype =
1441
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_PROTOTYPE];
1442
+ OseoValue *marker =
1443
+ &context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_SET_PROTOTYPE_OF];
1444
+ if (is_function(*marker)) return normal(prototype);
1445
+ if (is_object(*marker)) return normal(prototype);
1446
+ if (!is_object(prototype)) {
1447
+ return failure(context, "OSEO2001", "Object prototype is unavailable.");
1448
+ }
1449
+ size_t entry_allocations = context->allocations;
1450
+ *marker = prototype;
1451
+ OseoResult result = object_constructor_function(context);
1452
+ if (result.status == OSEO_STATUS_NORMAL) {
1453
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT] = result.value;
1454
+ OseoFunction *constructor = function_object(result.value);
1455
+ constructor->prototype_object = prototype;
1456
+ constructor->prototype_writable = false;
1457
+ }
1458
+ static const OseoIntrinsic intrinsics[] = {
1459
+ OSEO_INTRINSIC_OBJECT_HAS_OWN_PROPERTY,
1460
+ OSEO_INTRINSIC_OBJECT_IS_PROTOTYPE_OF,
1461
+ OSEO_INTRINSIC_OBJECT_PROPERTY_IS_ENUMERABLE,
1462
+ OSEO_INTRINSIC_OBJECT_TO_LOCALE_STRING,
1463
+ OSEO_INTRINSIC_OBJECT_TO_STRING,
1464
+ };
1465
+ static const size_t codes[] = {
1466
+ OSEO_OBJECT_HAS_OWN_PROPERTY_CODE_ID,
1467
+ OSEO_OBJECT_IS_PROTOTYPE_OF_CODE_ID,
1468
+ OSEO_OBJECT_PROPERTY_IS_ENUMERABLE_CODE_ID,
1469
+ OSEO_OBJECT_TO_LOCALE_STRING_CODE_ID,
1470
+ OSEO_OBJECT_TO_STRING_CODE_ID,
1471
+ };
1472
+ static const char *const names[] = {
1473
+ "hasOwnProperty",
1474
+ "isPrototypeOf",
1475
+ "propertyIsEnumerable",
1476
+ "toLocaleString",
1477
+ "toString",
1478
+ };
1479
+ static const size_t lengths[] = {1u, 1u, 1u, 0u, 0u};
1480
+ for (size_t index = 0u;
1481
+ result.status == OSEO_STATUS_NORMAL &&
1482
+ index < sizeof(intrinsics) / sizeof(*intrinsics);
1483
+ index += 1u) {
1484
+ result = object_prototype_function(
1485
+ context,
1486
+ intrinsics[index],
1487
+ codes[index],
1488
+ names[index],
1489
+ lengths[index]
1490
+ );
1491
+ }
1492
+ if (result.status == OSEO_STATUS_NORMAL) {
1493
+ result = create_object_prototype_function(
1494
+ context,
1495
+ OSEO_OBJECT_VALUE_OF_CODE_ID,
1496
+ "valueOf",
1497
+ 0u
1498
+ );
1499
+ if (result.status == OSEO_STATUS_NORMAL) {
1500
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_VALUE_OF] =
1501
+ result.value;
1502
+ }
1503
+ }
1504
+ static const OseoIntrinsic properties[] = {
1505
+ OSEO_INTRINSIC_OBJECT,
1506
+ OSEO_INTRINSIC_OBJECT_HAS_OWN_PROPERTY,
1507
+ OSEO_INTRINSIC_OBJECT_IS_PROTOTYPE_OF,
1508
+ OSEO_INTRINSIC_OBJECT_PROPERTY_IS_ENUMERABLE,
1509
+ OSEO_INTRINSIC_OBJECT_TO_LOCALE_STRING,
1510
+ OSEO_INTRINSIC_OBJECT_TO_STRING,
1511
+ OSEO_INTRINSIC_OBJECT_VALUE_OF,
1512
+ };
1513
+ static const char *const property_names[] = {
1514
+ "constructor",
1515
+ "hasOwnProperty",
1516
+ "isPrototypeOf",
1517
+ "propertyIsEnumerable",
1518
+ "toLocaleString",
1519
+ "toString",
1520
+ "valueOf",
1521
+ };
1522
+ const OseoPropertyAttributes attributes = {true, false, true, false};
1523
+ OseoRootFrame frame = {NULL, NULL, 0u};
1524
+ if (result.status == OSEO_STATUS_NORMAL) {
1525
+ result = oseo_roots_allocate(context, &frame, 4u);
1526
+ if (result.status == OSEO_STATUS_NORMAL) {
1527
+ frame.slots[0] = prototype;
1528
+ }
1529
+ }
1530
+ for (size_t index = 0u;
1531
+ result.status == OSEO_STATUS_NORMAL &&
1532
+ index < sizeof(properties) / sizeof(*properties);
1533
+ index += 1u) {
1534
+ result = oseo_internal_ascii_string(context, property_names[index]);
1535
+ frame.slots[1] = result.value;
1536
+ if (result.status == OSEO_STATUS_NORMAL) {
1537
+ result = oseo_object_define(
1538
+ context,
1539
+ frame.slots[0],
1540
+ frame.slots[1],
1541
+ context->realm->intrinsics[properties[index]],
1542
+ attributes
1543
+ );
1544
+ }
1545
+ }
1546
+ static const OseoIntrinsic static_intrinsics[] = {
1547
+ OSEO_INTRINSIC_OBJECT_GET_PROTOTYPE_OF,
1548
+ OSEO_INTRINSIC_OBJECT_IS,
1549
+ OSEO_INTRINSIC_OBJECT_SET_PROTOTYPE_OF,
1550
+ };
1551
+ static const size_t static_codes[] = {
1552
+ OSEO_OBJECT_GET_PROTOTYPE_OF_CODE_ID,
1553
+ OSEO_OBJECT_IS_CODE_ID,
1554
+ OSEO_OBJECT_SET_PROTOTYPE_OF_CODE_ID,
1555
+ };
1556
+ static const char *const static_names[] = {
1557
+ "getPrototypeOf",
1558
+ "is",
1559
+ "setPrototypeOf",
1560
+ };
1561
+ static const size_t static_lengths[] = {1u, 2u, 2u};
1562
+ for (size_t index = 0u;
1563
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
1564
+ index += 1u) {
1565
+ result = object_prototype_function(
1566
+ context,
1567
+ static_intrinsics[index],
1568
+ static_codes[index],
1569
+ static_names[index],
1570
+ static_lengths[index]
1571
+ );
1572
+ if (result.status == OSEO_STATUS_NORMAL) {
1573
+ result = oseo_internal_ascii_string(context, static_names[index]);
1574
+ frame.slots[1] = result.value;
1575
+ }
1576
+ if (result.status == OSEO_STATUS_NORMAL) {
1577
+ result = oseo_object_define(
1578
+ context,
1579
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT],
1580
+ frame.slots[1],
1581
+ context->realm->intrinsics[static_intrinsics[index]],
1582
+ attributes
1583
+ );
1584
+ }
1585
+ }
1586
+ static const size_t owned_static_codes[] = {
1587
+ OSEO_OBJECT_ASSIGN_CODE_ID,
1588
+ OSEO_OBJECT_CREATE_CODE_ID,
1589
+ OSEO_OBJECT_DEFINE_PROPERTIES_CODE_ID,
1590
+ OSEO_OBJECT_DEFINE_PROPERTY_CODE_ID,
1591
+ OSEO_OBJECT_ENTRIES_CODE_ID,
1592
+ OSEO_OBJECT_FREEZE_CODE_ID,
1593
+ OSEO_OBJECT_FROM_ENTRIES_CODE_ID,
1594
+ OSEO_OBJECT_GET_OWN_PROPERTY_DESCRIPTOR_CODE_ID,
1595
+ OSEO_OBJECT_GET_OWN_PROPERTY_DESCRIPTORS_CODE_ID,
1596
+ OSEO_OBJECT_GET_OWN_PROPERTY_NAMES_CODE_ID,
1597
+ OSEO_OBJECT_GET_OWN_PROPERTY_SYMBOLS_CODE_ID,
1598
+ OSEO_OBJECT_GROUP_BY_CODE_ID,
1599
+ OSEO_OBJECT_HAS_OWN_CODE_ID,
1600
+ OSEO_OBJECT_IS_EXTENSIBLE_CODE_ID,
1601
+ OSEO_OBJECT_IS_FROZEN_CODE_ID,
1602
+ OSEO_OBJECT_IS_SEALED_CODE_ID,
1603
+ OSEO_OBJECT_KEYS_CODE_ID,
1604
+ OSEO_OBJECT_PREVENT_EXTENSIONS_CODE_ID,
1605
+ OSEO_OBJECT_SEAL_CODE_ID,
1606
+ OSEO_OBJECT_VALUES_CODE_ID,
1607
+ };
1608
+ static const char *const owned_static_names[] = {
1609
+ "assign",
1610
+ "create",
1611
+ "defineProperties",
1612
+ "defineProperty",
1613
+ "entries",
1614
+ "freeze",
1615
+ "fromEntries",
1616
+ "getOwnPropertyDescriptor",
1617
+ "getOwnPropertyDescriptors",
1618
+ "getOwnPropertyNames",
1619
+ "getOwnPropertySymbols",
1620
+ "groupBy",
1621
+ "hasOwn",
1622
+ "isExtensible",
1623
+ "isFrozen",
1624
+ "isSealed",
1625
+ "keys",
1626
+ "preventExtensions",
1627
+ "seal",
1628
+ "values",
1629
+ };
1630
+ static const size_t owned_static_lengths[] = {
1631
+ 2u, 2u, 2u, 3u, 1u, 1u, 1u, 2u, 1u, 1u, 1u, 2u, 2u,
1632
+ 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1633
+ };
1634
+ for (size_t index = 0u;
1635
+ result.status == OSEO_STATUS_NORMAL &&
1636
+ index < sizeof(owned_static_codes) / sizeof(*owned_static_codes);
1637
+ index += 1u) {
1638
+ result = create_object_prototype_function(
1639
+ context,
1640
+ owned_static_codes[index],
1641
+ owned_static_names[index],
1642
+ owned_static_lengths[index]
1643
+ );
1644
+ frame.slots[2] = result.value;
1645
+ if (result.status == OSEO_STATUS_NORMAL) {
1646
+ result = oseo_internal_ascii_string(
1647
+ context,
1648
+ owned_static_names[index]
1649
+ );
1650
+ frame.slots[1] = result.value;
1651
+ }
1652
+ if (result.status == OSEO_STATUS_NORMAL) {
1653
+ result = oseo_object_define(
1654
+ context,
1655
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT],
1656
+ frame.slots[1],
1657
+ frame.slots[2],
1658
+ attributes
1659
+ );
1660
+ }
1661
+ }
1662
+ /* The Annex B accessor and methods of 20.1.3.8 through 20.1.3.12
1663
+ * follow the seven core properties in that clause order. */
1664
+ if (result.status == OSEO_STATUS_NORMAL) {
1665
+ result = create_object_prototype_function(context,
1666
+ OSEO_OBJECT_PROTO_GET_CODE_ID, "get __proto__", 0u);
1667
+ frame.slots[2] = result.value;
1668
+ }
1669
+ if (result.status == OSEO_STATUS_NORMAL) {
1670
+ result = create_object_prototype_function(context,
1671
+ OSEO_OBJECT_PROTO_SET_CODE_ID, "set __proto__", 1u);
1672
+ frame.slots[3] = result.value;
1673
+ }
1674
+ if (result.status == OSEO_STATUS_NORMAL) {
1675
+ result = oseo_internal_ascii_string(context, "__proto__");
1676
+ frame.slots[1] = result.value;
1677
+ }
1678
+ if (result.status == OSEO_STATUS_NORMAL) {
1679
+ result = oseo_object_define_accessor(context, frame.slots[0],
1680
+ frame.slots[1], frame.slots[2], frame.slots[3], true, true,
1681
+ (OseoPropertyAttributes){true, false, false, true});
1682
+ }
1683
+ static const size_t legacy_codes[] = {
1684
+ OSEO_OBJECT_DEFINE_GETTER_CODE_ID,
1685
+ OSEO_OBJECT_DEFINE_SETTER_CODE_ID,
1686
+ OSEO_OBJECT_LOOKUP_GETTER_CODE_ID,
1687
+ OSEO_OBJECT_LOOKUP_SETTER_CODE_ID,
1688
+ };
1689
+ static const char *const legacy_names[] = {
1690
+ "__defineGetter__",
1691
+ "__defineSetter__",
1692
+ "__lookupGetter__",
1693
+ "__lookupSetter__",
1694
+ };
1695
+ static const size_t legacy_lengths[] = {2u, 2u, 1u, 1u};
1696
+ for (size_t index = 0u;
1697
+ result.status == OSEO_STATUS_NORMAL &&
1698
+ index < sizeof(legacy_codes) / sizeof(*legacy_codes);
1699
+ index += 1u) {
1700
+ result = create_object_prototype_function(context,
1701
+ legacy_codes[index], legacy_names[index], legacy_lengths[index]);
1702
+ frame.slots[2] = result.value;
1703
+ if (result.status == OSEO_STATUS_NORMAL) {
1704
+ result = oseo_internal_ascii_string(context, legacy_names[index]);
1705
+ frame.slots[1] = result.value;
1706
+ }
1707
+ if (result.status == OSEO_STATUS_NORMAL) {
1708
+ result = oseo_object_define(context, frame.slots[0],
1709
+ frame.slots[1], frame.slots[2], attributes);
1710
+ }
1711
+ }
1712
+ if (frame.slots != NULL) oseo_roots_release(context, &frame);
1713
+ if (result.status != OSEO_STATUS_NORMAL) {
1714
+ *marker = oseo_undefined();
1715
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT] = oseo_undefined();
1716
+ return result;
1717
+ }
1718
+ if (context->observe_specialization) {
1719
+ context->allocations = entry_allocations;
1720
+ }
1721
+ return normal(prototype);
1722
+ }
1723
+
1724
+ OseoResult oseo_internal_install_object_global(
1725
+ OseoContext *context,
1726
+ OseoValue global
1727
+ ) {
1728
+ OseoValue slots[3] = {global, oseo_undefined(), oseo_undefined()};
1729
+ OseoRootFrame frame = {NULL, slots, 3u};
1730
+ oseo_roots_push(context, &frame);
1731
+ OseoResult result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_OBJECT);
1732
+ slots[1] = result.value;
1733
+ if (result.status == OSEO_STATUS_NORMAL) {
1734
+ result = oseo_internal_ascii_string(context, "Object");
1735
+ slots[2] = result.value;
1736
+ }
1737
+ if (result.status == OSEO_STATUS_NORMAL) {
1738
+ result = oseo_object_define(
1739
+ context,
1740
+ slots[0],
1741
+ slots[2],
1742
+ slots[1],
1743
+ (OseoPropertyAttributes){true, false, true, false}
1744
+ );
1745
+ }
1746
+ oseo_roots_pop(context, &frame);
1747
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1748
+ }
1749
+
1750
+ /* Mirrors HasProperty followed by Get for one ToPropertyDescriptor field:
1751
+ * an accessor field on the descriptor argument must be invoked with the
1752
+ * original descriptor object as the receiver, and a thrown exception must
1753
+ * propagate instead of being read as a raw stored value. */
1754
+ static OseoResult descriptor_field(
1755
+ OseoContext *context,
1756
+ OseoValue descriptor_value,
1757
+ const char *name,
1758
+ bool *has_field,
1759
+ OseoValue *value
1760
+ ) {
1761
+ *has_field = false;
1762
+ *value = oseo_undefined();
1763
+ OseoRootFrame frame = {NULL, NULL, 0u};
1764
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
1765
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1766
+ frame.slots[0] = descriptor_value;
1767
+ result = oseo_internal_ascii_string(context, name);
1768
+ frame.slots[1] = result.value;
1769
+ if (result.status == OSEO_STATUS_NORMAL) {
1770
+ result = oseo_has_property(
1771
+ context,
1772
+ frame.slots[1],
1773
+ frame.slots[0]
1774
+ );
1775
+ *has_field = result.status == OSEO_STATUS_NORMAL &&
1776
+ oseo_to_boolean(result.value);
1777
+ }
1778
+ if (result.status == OSEO_STATUS_NORMAL && *has_field) {
1779
+ result = oseo_object_get(
1780
+ context,
1781
+ frame.slots[0],
1782
+ frame.slots[1]
1783
+ );
1784
+ *value = result.value;
1785
+ }
1786
+ oseo_roots_release(context, &frame);
1787
+ return result.status == OSEO_STATUS_NORMAL ? normal(*value) : result;
1788
+ }
1789
+
1790
+ static bool rest_key_is_excluded(
1791
+ OseoValue key,
1792
+ size_t excluded_count,
1793
+ const OseoValue *excluded_keys
1794
+ ) {
1795
+ for (size_t index = 0u; index < excluded_count; index += 1u) {
1796
+ if (oseo_internal_property_key_equal(key, excluded_keys[index])) {
1797
+ return true;
1798
+ }
1799
+ }
1800
+ return false;
1801
+ }
1802
+
1803
+ /* Materialize a Proxy [[OwnPropertyKeys]] result in the frame shape used
1804
+ * by the ordinary own-key consumers below. */
1805
+ static OseoResult proxy_own_key_frame(
1806
+ OseoContext *context,
1807
+ OseoValue proxy,
1808
+ OseoRootFrame *frame,
1809
+ size_t *key_count
1810
+ ) {
1811
+ OseoValue slots[3] = {
1812
+ proxy, oseo_undefined(), oseo_undefined(),
1813
+ };
1814
+ OseoRootFrame temporary = {NULL, slots, 3u};
1815
+ oseo_roots_push(context, &temporary);
1816
+ OseoResult result = oseo_internal_proxy_own_keys(
1817
+ context, slots[0], OSEO_OWN_KEY_ALL);
1818
+ slots[1] = result.value;
1819
+ if (result.status == OSEO_STATUS_NORMAL) {
1820
+ result = oseo_internal_array_like_list(
1821
+ context, slots[1], &slots[2]);
1822
+ }
1823
+ const OseoValue *keys = NULL;
1824
+ if (result.status == OSEO_STATUS_NORMAL) {
1825
+ result = oseo_argument_list_view(
1826
+ context, slots[2], key_count, &keys);
1827
+ }
1828
+ if (result.status == OSEO_STATUS_NORMAL &&
1829
+ *key_count > SIZE_MAX - 3u) {
1830
+ result = failure(
1831
+ context, "OSEO2001", "Own-key snapshot is too large.");
1832
+ }
1833
+ if (result.status == OSEO_STATUS_NORMAL) {
1834
+ result = oseo_roots_allocate(context, frame, *key_count + 3u);
1835
+ }
1836
+ if (result.status == OSEO_STATUS_NORMAL) {
1837
+ frame->slots[0] = slots[0];
1838
+ for (size_t index = 0u; index < *key_count; index += 1u) {
1839
+ frame->slots[3u + index] = keys[index];
1840
+ }
1841
+ /* The dynamic frame was pushed above the temporary roots. The
1842
+ * copied key values let it replace those roots in the chain. */
1843
+ frame->previous = temporary.previous;
1844
+ temporary.previous = NULL;
1845
+ } else {
1846
+ oseo_roots_pop(context, &temporary);
1847
+ }
1848
+ return result;
1849
+ }
1850
+
1851
+ /*
1852
+ * The own-key order CopyDataProperties walks. Of the own properties that
1853
+ * live outside the property vector, only the virtual
1854
+ * %String.prototype%[@@iterator] can become enumerable, so this snapshot
1855
+ * places it; an array's `length` and a
1856
+ * function's `prototype` are permanently non-enumerable and the copy
1857
+ * would skip them anyway. A TypedArray's valid integer indices lead its
1858
+ * keys, as they do in the ordinary snapshot. A module namespace keeps
1859
+ * its export names in [[Exports]] order, integer-like names included,
1860
+ * as it does in that snapshot.
1861
+ */
1862
+ static OseoResult snapshot_rest_keys(
1863
+ OseoContext *context,
1864
+ OseoRootFrame *frame,
1865
+ size_t key_count
1866
+ ) {
1867
+ OseoValue source = frame->slots[0];
1868
+ size_t output = 0u;
1869
+ if (is_string(source)) {
1870
+ for (size_t index = 0u; index < key_count; index += 1u) {
1871
+ char key_text[24];
1872
+ (void)snprintf(key_text, sizeof(key_text), "%zu", index);
1873
+ OseoResult key = oseo_internal_ascii_string(context, key_text);
1874
+ if (key.status != OSEO_STATUS_NORMAL) return key;
1875
+ frame->slots[3u + output] = key.value;
1876
+ output += 1u;
1877
+ }
1878
+ return normal(oseo_undefined());
1879
+ }
1880
+ if (!is_object(source)) return normal(oseo_undefined());
1881
+ size_t index_count = oseo_internal_typed_array_index_key_count(source);
1882
+ for (; output < index_count && output < key_count; output += 1u) {
1883
+ OseoResult key = oseo_internal_typed_array_index_key(context, output);
1884
+ if (key.status != OSEO_STATUS_NORMAL) return key;
1885
+ frame->slots[3u + output] = key.value;
1886
+ }
1887
+ bool index_keys_lead = !ordinary_object(source)->module_namespace;
1888
+ uint64_t previous = UINT64_MAX;
1889
+ while (index_keys_lead && output < key_count) {
1890
+ OseoOrdinaryObject *object = ordinary_object(frame->slots[0]);
1891
+ size_t selected = SIZE_MAX;
1892
+ uint32_t selected_number = 0u;
1893
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
1894
+ uint32_t number = 0u;
1895
+ if (!oseo_internal_array_index(
1896
+ object->properties[index].key, &number) ||
1897
+ (previous != UINT64_MAX && number <= previous)) continue;
1898
+ if (selected == SIZE_MAX || number < selected_number) {
1899
+ selected = index;
1900
+ selected_number = number;
1901
+ }
1902
+ }
1903
+ if (selected == SIZE_MAX) break;
1904
+ frame->slots[3u + output] = object->properties[selected].key;
1905
+ output += 1u;
1906
+ previous = selected_number;
1907
+ }
1908
+ OseoOrdinaryObject *object = ordinary_object(frame->slots[0]);
1909
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
1910
+ uint32_t ignored = 0u;
1911
+ OseoValue key = object->properties[index].key;
1912
+ if (is_symbol(key) ||
1913
+ (index_keys_lead &&
1914
+ oseo_internal_array_index(key, &ignored))) continue;
1915
+ frame->slots[3u + output] = key;
1916
+ output += 1u;
1917
+ }
1918
+ if (ordinary_object(frame->slots[0])->virtual_string_iterator) {
1919
+ /* The untouched String iterator leads every symbol a program can
1920
+ * add, exactly as it does in the ordinary own-key snapshot. */
1921
+ OseoResult key = oseo_internal_well_known_symbol(
1922
+ context,
1923
+ OSEO_WELL_KNOWN_ITERATOR
1924
+ );
1925
+ if (key.status != OSEO_STATUS_NORMAL) return key;
1926
+ frame->slots[3u + output] = key.value;
1927
+ output += 1u;
1928
+ }
1929
+ /* The virtual-key lookup above may allocate, so reacquire the object
1930
+ * before reading its property vector. */
1931
+ object = ordinary_object(frame->slots[0]);
1932
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
1933
+ OseoValue key = object->properties[index].key;
1934
+ if (!is_symbol(key)) continue;
1935
+ frame->slots[3u + output] = key;
1936
+ output += 1u;
1937
+ }
1938
+ if (output != key_count) {
1939
+ return failure(context, "OSEO2001", "Own-key snapshot changed.");
1940
+ }
1941
+ return normal(oseo_undefined());
1942
+ }
1943
+
1944
+ /**
1945
+ * CopyDataProperties: adds every own enumerable property of `source` that is
1946
+ * not an excluded key to `target` as a writable, enumerable, configurable
1947
+ * data property, in own-key order. `source` must not be nullish; the two
1948
+ * callers apply the nullish rule their own syntax requires. The result value
1949
+ * is `target` so a caller can return it directly.
1950
+ */
1951
+ static OseoResult copy_data_properties(
1952
+ OseoContext *context,
1953
+ OseoValue target,
1954
+ OseoValue source,
1955
+ size_t excluded_count,
1956
+ const OseoValue *excluded_keys
1957
+ ) {
1958
+ OseoRootFrame frame = {NULL, NULL, 0u};
1959
+ size_t key_count = 0u;
1960
+ OseoResult result = normal(oseo_undefined());
1961
+ if (is_proxy(source)) {
1962
+ result = proxy_own_key_frame(context, source, &frame, &key_count);
1963
+ } else {
1964
+ key_count = is_string(source)
1965
+ ? string_object(source)->length
1966
+ : is_object(source)
1967
+ ? ordinary_object(source)->property_count +
1968
+ (ordinary_object(source)->virtual_string_iterator
1969
+ ? 1u
1970
+ : 0u) +
1971
+ oseo_internal_typed_array_index_key_count(source)
1972
+ : 0u;
1973
+ if (key_count > SIZE_MAX - 3u) {
1974
+ return failure(
1975
+ context, "OSEO2001", "Own-key snapshot is too large.");
1976
+ }
1977
+ result = oseo_roots_allocate(context, &frame, key_count + 3u);
1978
+ }
1979
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1980
+ frame.slots[0] = source;
1981
+ frame.slots[1] = target;
1982
+ if (!is_proxy(frame.slots[0])) {
1983
+ result = snapshot_rest_keys(context, &frame, key_count);
1984
+ }
1985
+ for (size_t index = 0u;
1986
+ result.status == OSEO_STATUS_NORMAL && index < key_count;
1987
+ index += 1u) {
1988
+ OseoValue key = frame.slots[3u + index];
1989
+ if (rest_key_is_excluded(key, excluded_count, excluded_keys)) {
1990
+ continue;
1991
+ }
1992
+ OseoPropertyAttributes attributes = {false, false, false, false};
1993
+ OseoValue ignored = oseo_undefined();
1994
+ OseoValue ignored_getter = oseo_undefined();
1995
+ OseoValue ignored_setter = oseo_undefined();
1996
+ bool exists = false;
1997
+ if (is_string(frame.slots[0])) {
1998
+ exists = oseo_internal_string_own_property(
1999
+ frame.slots[0], key, NULL);
2000
+ } else if (is_proxy(frame.slots[0])) {
2001
+ result = oseo_internal_proxy_get_own_property(
2002
+ context,
2003
+ frame.slots[0],
2004
+ key,
2005
+ &exists,
2006
+ &ignored,
2007
+ &attributes,
2008
+ &ignored_getter,
2009
+ &ignored_setter
2010
+ );
2011
+ if (result.status != OSEO_STATUS_NORMAL) break;
2012
+ } else {
2013
+ result = own_property_query(
2014
+ context,
2015
+ frame.slots[0],
2016
+ key,
2017
+ &exists,
2018
+ &ignored,
2019
+ &attributes,
2020
+ &ignored_getter,
2021
+ &ignored_setter
2022
+ );
2023
+ if (result.status != OSEO_STATUS_NORMAL) break;
2024
+ }
2025
+ bool enumerable = is_string(frame.slots[0])
2026
+ ? exists && !oseo_internal_string_is_ascii(key, "length")
2027
+ : exists && attributes.enumerable;
2028
+ if (!enumerable) continue;
2029
+ result = oseo_object_get(context, frame.slots[0], key);
2030
+ frame.slots[2] = result.value;
2031
+ if (result.status == OSEO_STATUS_NORMAL) {
2032
+ result = oseo_object_define(
2033
+ context,
2034
+ frame.slots[1],
2035
+ key,
2036
+ frame.slots[2],
2037
+ (OseoPropertyAttributes){true, true, true, false}
2038
+ );
2039
+ }
2040
+ }
2041
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
2042
+ oseo_roots_release(context, &frame);
2043
+ return result;
2044
+ }
2045
+
2046
+ OseoResult oseo_object_rest(
2047
+ OseoContext *context,
2048
+ OseoValue source,
2049
+ size_t excluded_count,
2050
+ const OseoValue *excluded_keys
2051
+ ) {
2052
+ if (is_nullish(source)) {
2053
+ return type_error(context, "Cannot destructure a nullish value.");
2054
+ }
2055
+ /* Slot 1 keeps the fresh target alive while Proxy traps can collect. */
2056
+ OseoRootFrame frame = {NULL, NULL, 0u};
2057
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
2058
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2059
+ frame.slots[0] = source;
2060
+ result = oseo_object_literal_create(context);
2061
+ frame.slots[1] = result.value;
2062
+ if (result.status == OSEO_STATUS_NORMAL) {
2063
+ result = copy_data_properties(
2064
+ context,
2065
+ frame.slots[1],
2066
+ frame.slots[0],
2067
+ excluded_count,
2068
+ excluded_keys
2069
+ );
2070
+ }
2071
+ oseo_roots_release(context, &frame);
2072
+ return result;
2073
+ }
2074
+
2075
+ OseoResult oseo_object_spread(
2076
+ OseoContext *context,
2077
+ OseoValue target,
2078
+ OseoValue source
2079
+ ) {
2080
+ /* An object literal spread of null or undefined copies nothing rather
2081
+ * than throwing, unlike an object binding rest. */
2082
+ if (is_nullish(source)) return normal(target);
2083
+ return copy_data_properties(context, target, source, 0u, NULL);
2084
+ }
2085
+
2086
+ static OseoResult define_ascii_value(
2087
+ OseoContext *context,
2088
+ OseoRootFrame *frame,
2089
+ const char *name,
2090
+ OseoValue value
2091
+ ) {
2092
+ OseoResult result = oseo_internal_ascii_string(context, name);
2093
+ frame->slots[1] = result.value;
2094
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2095
+ return oseo_object_define(
2096
+ context,
2097
+ frame->slots[0],
2098
+ frame->slots[1],
2099
+ value,
2100
+ (OseoPropertyAttributes){true, true, true, false}
2101
+ );
2102
+ }
2103
+
2104
+ /*
2105
+ * Object.create (20.1.2.2). The prototype check precedes every read of
2106
+ * the properties argument, so a non-object non-null prototype throws
2107
+ * before a poisoned properties getter can run. An undefined properties
2108
+ * argument returns the fresh object directly; any other value flows
2109
+ * through the ObjectDefineProperties body `Object.defineProperties`
2110
+ * owns, so descriptor collection, ordering, and abrupt completions
2111
+ * behave identically over the freshly created target.
2112
+ */
2113
+ OseoResult oseo_object_builtin_create(
2114
+ OseoContext *context,
2115
+ size_t argument_count,
2116
+ const OseoValue *arguments
2117
+ ) {
2118
+ OseoValue prototype = builtin_argument(argument_count, arguments, 0u);
2119
+ if (tag_of(prototype) != OSEO_TAG_NULL && !is_object(prototype)) {
2120
+ return type_error(
2121
+ context,
2122
+ "Object.create requires an object or null prototype."
2123
+ );
2124
+ }
2125
+ OseoValue properties = builtin_argument(argument_count, arguments, 1u);
2126
+ if (tag_of(properties) == OSEO_TAG_UNDEFINED) {
2127
+ return oseo_object_create(context, prototype);
2128
+ }
2129
+ OseoRootFrame frame = {NULL, NULL, 0u};
2130
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
2131
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2132
+ frame.slots[0] = properties;
2133
+ result = oseo_object_create(context, prototype);
2134
+ if (result.status == OSEO_STATUS_NORMAL) {
2135
+ frame.slots[1] = result.value;
2136
+ const OseoValue define_arguments[2] = {
2137
+ frame.slots[1],
2138
+ frame.slots[0],
2139
+ };
2140
+ result = object_define_properties(context, 2u, define_arguments);
2141
+ }
2142
+ oseo_roots_release(context, &frame);
2143
+ return result;
2144
+ }
2145
+
2146
+ /*
2147
+ * The presence flags and converted attribute fields of one
2148
+ * ToPropertyDescriptor result. The `value`, `get`, and `set` fields are
2149
+ * heap values, so they stay in caller-rooted slots rather than in this
2150
+ * record; ToBoolean runs no user code, so the three attribute fields
2151
+ * hold their converted results directly.
2152
+ */
2153
+ /*
2154
+ * ToPropertyDescriptor (6.2.6.5) over a descriptor object the caller has
2155
+ * already checked and rooted. The fields are read in ECMA-262's fixed
2156
+ * order: enumerable, configurable, value, writable, get, set. Every heap
2157
+ * field is stored into its caller-rooted slot as soon as it is read,
2158
+ * because reading a later field can invoke a descriptor accessor that
2159
+ * allocates and collects; an unrooted C local holding an earlier field's
2160
+ * freshly returned heap value would not survive that collection. An
2161
+ * accessor field that is neither undefined nor callable and a
2162
+ * descriptor mixing accessor and data fields throw the specified
2163
+ * TypeError.
2164
+ */
2165
+ OseoResult oseo_internal_to_property_descriptor(
2166
+ OseoContext *context,
2167
+ OseoValue descriptor_value,
2168
+ OseoValue *value_slot,
2169
+ OseoValue *getter_slot,
2170
+ OseoValue *setter_slot,
2171
+ OseoConvertedDescriptor *descriptor
2172
+ ) {
2173
+ OseoValue attribute = oseo_undefined();
2174
+ OseoResult result = descriptor_field(
2175
+ context, descriptor_value, "enumerable",
2176
+ &descriptor->has_enumerable, &attribute);
2177
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2178
+ descriptor->enumerable =
2179
+ descriptor->has_enumerable && oseo_to_boolean(attribute);
2180
+ result = descriptor_field(
2181
+ context, descriptor_value, "configurable",
2182
+ &descriptor->has_configurable, &attribute);
2183
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2184
+ descriptor->configurable =
2185
+ descriptor->has_configurable && oseo_to_boolean(attribute);
2186
+ result = descriptor_field(
2187
+ context, descriptor_value, "value",
2188
+ &descriptor->has_value, value_slot);
2189
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2190
+ result = descriptor_field(
2191
+ context, descriptor_value, "writable",
2192
+ &descriptor->has_writable, &attribute);
2193
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2194
+ descriptor->writable =
2195
+ descriptor->has_writable && oseo_to_boolean(attribute);
2196
+ result = descriptor_field(
2197
+ context, descriptor_value, "get",
2198
+ &descriptor->has_getter, getter_slot);
2199
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2200
+ if (descriptor->has_getter &&
2201
+ tag_of(*getter_slot) != OSEO_TAG_UNDEFINED &&
2202
+ !is_callable(*getter_slot)) {
2203
+ return type_error(context, "A property descriptor 'get' field must "
2204
+ "be undefined or callable.");
2205
+ }
2206
+ result = descriptor_field(
2207
+ context, descriptor_value, "set",
2208
+ &descriptor->has_setter, setter_slot);
2209
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2210
+ if (descriptor->has_setter &&
2211
+ tag_of(*setter_slot) != OSEO_TAG_UNDEFINED &&
2212
+ !is_callable(*setter_slot)) {
2213
+ return type_error(context, "A property descriptor 'set' field must "
2214
+ "be undefined or callable.");
2215
+ }
2216
+ if ((descriptor->has_getter || descriptor->has_setter) &&
2217
+ (descriptor->has_value || descriptor->has_writable)) {
2218
+ return type_error(
2219
+ context,
2220
+ "A property descriptor cannot mix accessor and data fields."
2221
+ );
2222
+ }
2223
+ return normal(oseo_undefined());
2224
+ }
2225
+
2226
+ /*
2227
+ * DefinePropertyOrThrow (7.3.8) over one converted descriptor. The
2228
+ * caller roots the target, the key, and the three heap descriptor
2229
+ * fields. The current property state supplies every absent field, a
2230
+ * cell-backed current value reads through its binding cell, and the
2231
+ * descriptor component stays authoritative for compatibility and
2232
+ * mutation.
2233
+ */
2234
+ OseoResult oseo_internal_define_converted_property(
2235
+ OseoContext *context,
2236
+ OseoValue object_value,
2237
+ OseoValue key,
2238
+ const OseoConvertedDescriptor *descriptor,
2239
+ OseoValue value,
2240
+ OseoValue getter,
2241
+ OseoValue setter,
2242
+ const char **refusal
2243
+ ) {
2244
+ *refusal = NULL;
2245
+ if (is_proxy(object_value)) {
2246
+ return oseo_internal_proxy_define_own_property(
2247
+ context,
2248
+ object_value,
2249
+ key,
2250
+ descriptor,
2251
+ value,
2252
+ getter,
2253
+ setter,
2254
+ refusal
2255
+ );
2256
+ }
2257
+ bool typed_array_key = false;
2258
+ size_t typed_index = SIZE_MAX;
2259
+ if (is_typed_array(object_value)) {
2260
+ OseoResult classified = oseo_internal_typed_array_numeric_key(
2261
+ context,
2262
+ key,
2263
+ &typed_array_key,
2264
+ &typed_index
2265
+ );
2266
+ if (classified.status != OSEO_STATUS_NORMAL) return classified;
2267
+ }
2268
+ if (typed_array_key) {
2269
+ return oseo_internal_typed_array_define_index(
2270
+ context,
2271
+ object_value,
2272
+ typed_index,
2273
+ descriptor,
2274
+ value,
2275
+ refusal
2276
+ );
2277
+ }
2278
+ /* A live `arguments` or `caller` moves into the property vector with
2279
+ * its present value first, which a descriptor without a value field
2280
+ * then keeps (ADR 0034). */
2281
+ OseoResult legacy =
2282
+ oseo_internal_legacy_materialize(context, object_value, key);
2283
+ if (legacy.status != OSEO_STATUS_NORMAL) return legacy;
2284
+ OseoValue current_value = oseo_undefined();
2285
+ OseoPropertyAttributes current_attributes = {false, false, false, false};
2286
+ OseoValue current_getter = oseo_undefined();
2287
+ OseoValue current_setter = oseo_undefined();
2288
+ bool exists = oseo_internal_own_property_descriptor(
2289
+ context,
2290
+ object_value,
2291
+ key,
2292
+ &current_value,
2293
+ &current_attributes,
2294
+ &current_getter,
2295
+ &current_setter
2296
+ );
2297
+ /* A descriptor with no value field keeps the property's current
2298
+ * value, which a cell-backed property holds in its binding cell. */
2299
+ if (exists &&
2300
+ oseo_internal_cell_backed_property(object_value, current_value)) {
2301
+ OseoResult cell = oseo_cell_get(context, current_value);
2302
+ if (cell.status != OSEO_STATUS_NORMAL) return cell;
2303
+ current_value = cell.value;
2304
+ }
2305
+ if (descriptor->has_getter || descriptor->has_setter ||
2306
+ (!descriptor->has_value && !descriptor->has_writable &&
2307
+ exists && current_attributes.accessor)) {
2308
+ OseoPropertyAttributes attributes = {
2309
+ !descriptor->has_configurable
2310
+ ? exists && current_attributes.configurable
2311
+ : descriptor->configurable,
2312
+ !descriptor->has_enumerable
2313
+ ? exists && current_attributes.enumerable
2314
+ : descriptor->enumerable,
2315
+ false,
2316
+ true,
2317
+ };
2318
+ return oseo_internal_define_accessor_reported(
2319
+ context,
2320
+ object_value,
2321
+ key,
2322
+ descriptor->has_getter ? getter : oseo_undefined(),
2323
+ descriptor->has_setter ? setter : oseo_undefined(),
2324
+ descriptor->has_getter,
2325
+ descriptor->has_setter,
2326
+ attributes,
2327
+ refusal
2328
+ );
2329
+ }
2330
+ OseoPropertyAttributes attributes = {
2331
+ !descriptor->has_configurable
2332
+ ? exists && current_attributes.configurable
2333
+ : descriptor->configurable,
2334
+ !descriptor->has_enumerable
2335
+ ? exists && current_attributes.enumerable
2336
+ : descriptor->enumerable,
2337
+ !descriptor->has_writable
2338
+ ? exists && current_attributes.writable
2339
+ : descriptor->writable,
2340
+ false,
2341
+ };
2342
+ return oseo_internal_define_data_reported(
2343
+ context,
2344
+ object_value,
2345
+ key,
2346
+ descriptor->has_value ? value : current_value,
2347
+ attributes,
2348
+ descriptor->has_value,
2349
+ !descriptor->has_writable,
2350
+ refusal
2351
+ );
2352
+ }
2353
+
2354
+ OseoResult oseo_internal_define_from_descriptor(
2355
+ OseoContext *context,
2356
+ OseoValue object_value,
2357
+ OseoValue key,
2358
+ OseoValue descriptor_value,
2359
+ const char **refusal
2360
+ ) {
2361
+ *refusal = NULL;
2362
+ if (!is_object(descriptor_value)) {
2363
+ return type_error(
2364
+ context,
2365
+ "A property definition requires an object descriptor."
2366
+ );
2367
+ }
2368
+ /* Slots: 0 target, 1 key, 2 value, 3 get, 4 set. */
2369
+ OseoRootFrame frame = {NULL, NULL, 0u};
2370
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2371
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2372
+ frame.slots[0] = object_value;
2373
+ frame.slots[1] = key;
2374
+ OseoConvertedDescriptor descriptor;
2375
+ result = oseo_internal_to_property_descriptor(
2376
+ context,
2377
+ descriptor_value,
2378
+ &frame.slots[2],
2379
+ &frame.slots[3],
2380
+ &frame.slots[4],
2381
+ &descriptor
2382
+ );
2383
+ if (result.status == OSEO_STATUS_NORMAL) {
2384
+ result = oseo_internal_define_converted_property(
2385
+ context,
2386
+ frame.slots[0],
2387
+ frame.slots[1],
2388
+ &descriptor,
2389
+ frame.slots[2],
2390
+ frame.slots[3],
2391
+ frame.slots[4],
2392
+ refusal
2393
+ );
2394
+ }
2395
+ oseo_roots_release(context, &frame);
2396
+ return result;
2397
+ }
2398
+
2399
+ OseoResult oseo_object_builtin_define_property(
2400
+ OseoContext *context,
2401
+ size_t argument_count,
2402
+ const OseoValue *arguments
2403
+ ) {
2404
+ OseoValue object_value = builtin_argument(argument_count, arguments, 0u);
2405
+ OseoValue descriptor_value =
2406
+ builtin_argument(argument_count, arguments, 2u);
2407
+ if (!is_object(object_value)) {
2408
+ return type_error(
2409
+ context,
2410
+ "Object.defineProperty requires an object target."
2411
+ );
2412
+ }
2413
+ /* Slot 0 holds the converted key across the descriptor conversion. */
2414
+ OseoRootFrame frame = {NULL, NULL, 0u};
2415
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
2416
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2417
+ result = oseo_property_key(
2418
+ context,
2419
+ builtin_argument(argument_count, arguments, 1u)
2420
+ );
2421
+ frame.slots[0] = result.value;
2422
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(descriptor_value)) {
2423
+ result = type_error(
2424
+ context,
2425
+ "Object.defineProperty requires an object descriptor."
2426
+ );
2427
+ }
2428
+ const char *refusal = NULL;
2429
+ if (result.status == OSEO_STATUS_NORMAL) {
2430
+ result = oseo_internal_define_from_descriptor(
2431
+ context,
2432
+ object_value,
2433
+ frame.slots[0],
2434
+ descriptor_value,
2435
+ &refusal
2436
+ );
2437
+ }
2438
+ if (result.status == OSEO_STATUS_NORMAL && refusal != NULL) {
2439
+ result = type_error(context, refusal);
2440
+ }
2441
+ if (result.status == OSEO_STATUS_NORMAL) result.value = object_value;
2442
+ oseo_roots_release(context, &frame);
2443
+ return result;
2444
+ }
2445
+
2446
+ /*
2447
+ * FromPropertyDescriptor (6.2.6.4). The caller owns a root frame of at
2448
+ * least four slots and stores the described property's data value or
2449
+ * getter in slot 2 and its setter in slot 3 before calling. The created
2450
+ * object lands in slot 0, slot 1 is the field-name scratch every
2451
+ * define_ascii_value call uses, and the fields are defined in the order
2452
+ * the abstract operation specifies, so an own-key walk of the result
2453
+ * reports `value, writable, enumerable, configurable` for a data
2454
+ * property and `get, set, enumerable, configurable` for an accessor.
2455
+ */
2456
+ static OseoResult from_property_descriptor(
2457
+ OseoContext *context,
2458
+ OseoRootFrame *frame,
2459
+ OseoPropertyAttributes attributes
2460
+ ) {
2461
+ OseoResult result = oseo_object_literal_create(context);
2462
+ frame->slots[0] = result.value;
2463
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2464
+ result = attributes.accessor
2465
+ ? define_ascii_value(context, frame, "get", frame->slots[2])
2466
+ : define_ascii_value(context, frame, "value", frame->slots[2]);
2467
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2468
+ result = attributes.accessor
2469
+ ? define_ascii_value(context, frame, "set", frame->slots[3])
2470
+ : define_ascii_value(
2471
+ context,
2472
+ frame,
2473
+ "writable",
2474
+ oseo_boolean(attributes.writable)
2475
+ );
2476
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2477
+ result = define_ascii_value(
2478
+ context,
2479
+ frame,
2480
+ "enumerable",
2481
+ oseo_boolean(attributes.enumerable)
2482
+ );
2483
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2484
+ result = define_ascii_value(
2485
+ context,
2486
+ frame,
2487
+ "configurable",
2488
+ oseo_boolean(attributes.configurable)
2489
+ );
2490
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2491
+ return normal(frame->slots[0]);
2492
+ }
2493
+
2494
+ OseoResult oseo_object_builtin_get_own_property_descriptor(
2495
+ OseoContext *context,
2496
+ size_t argument_count,
2497
+ const OseoValue *arguments
2498
+ ) {
2499
+ OseoValue object_value = builtin_argument(argument_count, arguments, 0u);
2500
+ if (is_nullish(object_value)) {
2501
+ return type_error(
2502
+ context,
2503
+ "Cannot convert a nullish value to an object."
2504
+ );
2505
+ }
2506
+ OseoRootFrame frame = {NULL, NULL, 0u};
2507
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
2508
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2509
+ result = oseo_property_key(
2510
+ context,
2511
+ builtin_argument(argument_count, arguments, 1u)
2512
+ );
2513
+ frame.slots[1] = result.value;
2514
+ OseoValue value = oseo_undefined();
2515
+ OseoPropertyAttributes attributes = {false, false, false, false};
2516
+ OseoValue getter = oseo_undefined();
2517
+ OseoValue setter = oseo_undefined();
2518
+ bool exists = false;
2519
+ if (result.status == OSEO_STATUS_NORMAL && is_object(object_value)) {
2520
+ result = is_proxy(object_value)
2521
+ ? oseo_internal_proxy_get_own_property(
2522
+ context,
2523
+ object_value,
2524
+ frame.slots[1],
2525
+ &exists,
2526
+ &value,
2527
+ &attributes,
2528
+ &getter,
2529
+ &setter
2530
+ )
2531
+ : own_property_query(
2532
+ context,
2533
+ object_value,
2534
+ frame.slots[1],
2535
+ &exists,
2536
+ &value,
2537
+ &attributes,
2538
+ &getter,
2539
+ &setter
2540
+ );
2541
+ /* The normal value is ignored; only the abrupt completion and
2542
+ * the descriptor outputs are observable here. */
2543
+ (void)result.value;
2544
+ } else if (result.status == OSEO_STATUS_NORMAL &&
2545
+ is_string(object_value)) {
2546
+ if (oseo_internal_string_is_ascii(frame.slots[1], "length")) {
2547
+ value = oseo_number(string_object(object_value)->length);
2548
+ exists = true;
2549
+ } else {
2550
+ uint32_t index = 0u;
2551
+ if (oseo_internal_array_index(frame.slots[1], &index) &&
2552
+ index < string_object(object_value)->length) {
2553
+ uint16_t unit = string_object(object_value)->units[index];
2554
+ result = oseo_internal_allocate_string(context, &unit, 1u);
2555
+ value = result.value;
2556
+ attributes.enumerable = true;
2557
+ exists = result.status == OSEO_STATUS_NORMAL;
2558
+ }
2559
+ }
2560
+ }
2561
+ if (result.status == OSEO_STATUS_NORMAL && exists &&
2562
+ oseo_internal_cell_backed_property(object_value, value)) {
2563
+ result = oseo_cell_get(context, value);
2564
+ value = result.value;
2565
+ }
2566
+ frame.slots[2] = attributes.accessor ? getter : value;
2567
+ frame.slots[3] = setter;
2568
+ if (result.status == OSEO_STATUS_NORMAL && !exists) {
2569
+ oseo_roots_release(context, &frame);
2570
+ return normal(oseo_undefined());
2571
+ }
2572
+ if (result.status == OSEO_STATUS_NORMAL) {
2573
+ result = from_property_descriptor(
2574
+ context, &frame, attributes);
2575
+ }
2576
+ oseo_roots_release(context, &frame);
2577
+ return result;
2578
+ }
2579
+
2580
+ /*
2581
+ * OrdinaryOwnPropertyKeys (10.1.11.1) over the representations this
2582
+ * profile stores. Integer-index keys come first in ascending numeric
2583
+ * order, then the remaining string keys in creation order, then the
2584
+ * symbol keys in creation order. A module namespace's [[OwnPropertyKeys]]
2585
+ * (10.4.6.11) instead lists every export name in the code-unit order its
2586
+ * [[Exports]] list has, integer-like names included; the namespace is
2587
+ * created with its exports in that order, so creation order is that list.
2588
+ *
2589
+ * Three own properties are not in the property vector: an array's
2590
+ * `length`, a function's `prototype`, and the untouched virtual
2591
+ * %String.prototype%[Symbol.iterator]. An array's `length` is created
2592
+ * before any property a program can add, so it leads the array's string
2593
+ * keys. A function's `prototype` follows the leading string keys its
2594
+ * `prototype_key_position` still counts. The untouched String iterator
2595
+ * leads every symbol a program can add. A concrete replacement occupies
2596
+ * that same position in the property vector. Slot 0 of `frame` holds
2597
+ * the object, slot 2 holds whichever synthesized string key this object
2598
+ * needs, and the keys fill the `key_count` slots from index 3.
2599
+ */
2600
+ /*
2601
+ * Writes `prototype`, preceded by `caller` while it is still live. A
2602
+ * live `caller` always sits right in front of `prototype`; a live
2603
+ * `arguments` is written separately at its own rank (ADR 0034).
2604
+ */
2605
+ static size_t place_prototype_keys(
2606
+ const OseoContext *context,
2607
+ OseoRootFrame *frame,
2608
+ size_t output,
2609
+ uint8_t legacy_live
2610
+ ) {
2611
+ if ((legacy_live & OSEO_LEGACY_CALLER_LIVE) != 0u) {
2612
+ frame->slots[3u + output++] = context->legacy_function_keys[1];
2613
+ }
2614
+ frame->slots[3u + output++] = frame->slots[2];
2615
+ return output;
2616
+ }
2617
+
2618
+ static OseoResult snapshot_own_keys(
2619
+ OseoContext *context,
2620
+ OseoRootFrame *frame,
2621
+ size_t key_count
2622
+ ) {
2623
+ /* A TypedArray's valid integer indices lead its keys. */
2624
+ size_t output = 0u;
2625
+ size_t index_count =
2626
+ oseo_internal_typed_array_index_key_count(frame->slots[0]);
2627
+ for (; output < index_count && output < key_count; output += 1u) {
2628
+ OseoResult key = oseo_internal_typed_array_index_key(context, output);
2629
+ if (key.status != OSEO_STATUS_NORMAL) return key;
2630
+ frame->slots[3u + output] = key.value;
2631
+ }
2632
+ bool virtual_length = is_array(frame->slots[0]);
2633
+ bool virtual_prototype =
2634
+ function_has_prototype_property(frame->slots[0]);
2635
+ bool virtual_string_iterator =
2636
+ ordinary_object(frame->slots[0])->virtual_string_iterator;
2637
+ bool index_keys_lead = !ordinary_object(frame->slots[0])->module_namespace;
2638
+ uint64_t previous = UINT64_MAX;
2639
+ while (index_keys_lead && output < key_count) {
2640
+ OseoOrdinaryObject *object = ordinary_object(frame->slots[0]);
2641
+ size_t selected = SIZE_MAX;
2642
+ uint32_t selected_number = 0u;
2643
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
2644
+ uint32_t number = 0u;
2645
+ if (!oseo_internal_array_index(
2646
+ object->properties[index].key, &number) ||
2647
+ (previous != UINT64_MAX && number <= previous)) continue;
2648
+ if (selected == SIZE_MAX || number < selected_number) {
2649
+ selected = index;
2650
+ selected_number = number;
2651
+ }
2652
+ }
2653
+ if (selected == SIZE_MAX) break;
2654
+ frame->slots[3u + output] = object->properties[selected].key;
2655
+ output += 1u;
2656
+ previous = selected_number;
2657
+ }
2658
+ if (virtual_length || virtual_prototype) {
2659
+ OseoResult key = oseo_internal_ascii_string(
2660
+ context,
2661
+ virtual_length ? "length" : "prototype"
2662
+ );
2663
+ if (key.status != OSEO_STATUS_NORMAL) return key;
2664
+ frame->slots[2] = key.value;
2665
+ }
2666
+ if (virtual_length) {
2667
+ frame->slots[3u + output] = frame->slots[2];
2668
+ output += 1u;
2669
+ }
2670
+ uint8_t legacy_live = is_function(frame->slots[0])
2671
+ ? function_object(frame->slots[0])->legacy_live
2672
+ : 0u;
2673
+ if (legacy_live != 0u) {
2674
+ OseoResult keys = oseo_internal_legacy_function_keys(context);
2675
+ if (keys.status != OSEO_STATUS_NORMAL) return keys;
2676
+ }
2677
+ bool pending_arguments =
2678
+ (legacy_live & OSEO_LEGACY_ARGUMENTS_LIVE) != 0u;
2679
+ size_t arguments_position = pending_arguments
2680
+ ? function_object(frame->slots[0])->legacy_arguments_position
2681
+ : 0u;
2682
+ /* No allocation happens through the remaining string-key pass, so
2683
+ * the property vector cannot move under this loop. */
2684
+ bool pending_prototype = virtual_prototype;
2685
+ size_t prototype_position = virtual_prototype
2686
+ ? function_object(frame->slots[0])->prototype_key_position
2687
+ : 0u;
2688
+ size_t string_rank = 0u;
2689
+ OseoOrdinaryObject *object = ordinary_object(frame->slots[0]);
2690
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
2691
+ uint32_t ignored = 0u;
2692
+ OseoValue key = object->properties[index].key;
2693
+ if (is_symbol(key) ||
2694
+ (index_keys_lead &&
2695
+ oseo_internal_array_index(key, &ignored))) continue;
2696
+ if (pending_arguments && string_rank == arguments_position) {
2697
+ frame->slots[3u + output++] = context->legacy_function_keys[0];
2698
+ pending_arguments = false;
2699
+ }
2700
+ if (pending_prototype && string_rank == prototype_position) {
2701
+ output = place_prototype_keys(
2702
+ context, frame, output, legacy_live);
2703
+ pending_prototype = false;
2704
+ }
2705
+ frame->slots[3u + output] = key;
2706
+ output += 1u;
2707
+ string_rank += 1u;
2708
+ }
2709
+ if (pending_arguments) {
2710
+ frame->slots[3u + output++] = context->legacy_function_keys[0];
2711
+ }
2712
+ if (pending_prototype) {
2713
+ output = place_prototype_keys(context, frame, output, legacy_live);
2714
+ }
2715
+ if (virtual_string_iterator) {
2716
+ OseoResult key = oseo_internal_well_known_symbol(
2717
+ context,
2718
+ OSEO_WELL_KNOWN_ITERATOR
2719
+ );
2720
+ if (key.status != OSEO_STATUS_NORMAL) return key;
2721
+ frame->slots[3u + output] = key.value;
2722
+ output += 1u;
2723
+ }
2724
+ /* The virtual-key lookup above may allocate, so reacquire the object
2725
+ * before reading its property vector. No allocation happens during
2726
+ * the stored-symbol pass. */
2727
+ object = ordinary_object(frame->slots[0]);
2728
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
2729
+ OseoValue key = object->properties[index].key;
2730
+ if (!is_symbol(key)) continue;
2731
+ frame->slots[3u + output] = key;
2732
+ output += 1u;
2733
+ }
2734
+ if (output != key_count) {
2735
+ return failure(context, "OSEO2001", "Own-key snapshot changed.");
2736
+ }
2737
+ return normal(oseo_undefined());
2738
+ }
2739
+
2740
+ static size_t own_key_count(OseoValue object_value) {
2741
+ size_t virtual_count = is_array(object_value) ||
2742
+ function_has_prototype_property(object_value) ? 1u : 0u;
2743
+ if (is_function(object_value)) {
2744
+ uint8_t live = function_object(object_value)->legacy_live;
2745
+ if ((live & OSEO_LEGACY_ARGUMENTS_LIVE) != 0u) virtual_count += 1u;
2746
+ if ((live & OSEO_LEGACY_CALLER_LIVE) != 0u) virtual_count += 1u;
2747
+ }
2748
+ if (ordinary_object(object_value)->virtual_string_iterator) {
2749
+ virtual_count += 1u;
2750
+ }
2751
+ /* A view's length is bounded by its buffer's byte length, so adding
2752
+ * it to the property count cannot overflow. */
2753
+ return ordinary_object(object_value)->property_count + virtual_count +
2754
+ oseo_internal_typed_array_index_key_count(object_value);
2755
+ }
2756
+
2757
+ static OseoResult object_close_after_abrupt(
2758
+ OseoContext *context,
2759
+ OseoValue iterator,
2760
+ OseoResult abrupt
2761
+ ) {
2762
+ if (abrupt.status != OSEO_STATUS_THROW || context->has_diagnostic) {
2763
+ return abrupt;
2764
+ }
2765
+ OseoValue slots[2] = {iterator, abrupt.value};
2766
+ OseoRootFrame frame = {NULL, slots, 2u};
2767
+ oseo_roots_push(context, &frame);
2768
+ oseo_context_clear_language_error(context);
2769
+ OseoResult closed = oseo_iterator_close(context, slots[0], true);
2770
+ OseoResult result = closed.status == OSEO_STATUS_NORMAL
2771
+ ? (OseoResult){OSEO_STATUS_THROW, slots[1]}
2772
+ : closed;
2773
+ oseo_roots_pop(context, &frame);
2774
+ return result;
2775
+ }
2776
+
2777
+ OseoResult oseo_internal_own_property_enumerable(
2778
+ OseoContext *context,
2779
+ OseoValue object_value,
2780
+ OseoValue key,
2781
+ bool *enumerable
2782
+ ) {
2783
+ *enumerable = false;
2784
+ OseoValue value = oseo_undefined();
2785
+ OseoValue getter = oseo_undefined();
2786
+ OseoValue setter = oseo_undefined();
2787
+ OseoPropertyAttributes attributes = {false, false, false, false};
2788
+ bool exists = false;
2789
+ OseoResult result = is_proxy(object_value)
2790
+ ? oseo_internal_proxy_get_own_property(
2791
+ context,
2792
+ object_value,
2793
+ key,
2794
+ &exists,
2795
+ &value,
2796
+ &attributes,
2797
+ &getter,
2798
+ &setter
2799
+ )
2800
+ : own_property_query(
2801
+ context,
2802
+ object_value,
2803
+ key,
2804
+ &exists,
2805
+ &value,
2806
+ &attributes,
2807
+ &getter,
2808
+ &setter
2809
+ );
2810
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2811
+ *enumerable = exists && attributes.enumerable;
2812
+ return normal(oseo_undefined());
2813
+ }
2814
+
2815
+ OseoResult oseo_internal_own_key_array(
2816
+ OseoContext *context,
2817
+ OseoValue object_value,
2818
+ OseoOwnKeyFilter filter
2819
+ ) {
2820
+ if (is_proxy(object_value)) {
2821
+ return oseo_internal_proxy_own_keys(context, object_value, filter);
2822
+ }
2823
+ OseoValue rooted = object_value;
2824
+ OseoRootFrame root = {NULL, &rooted, 1u};
2825
+ oseo_roots_push(context, &root);
2826
+ size_t key_count = own_key_count(rooted);
2827
+ if (key_count > SIZE_MAX - 3u) {
2828
+ oseo_roots_pop(context, &root);
2829
+ return failure(context, "OSEO2001", "Own-key snapshot is too large.");
2830
+ }
2831
+ OseoRootFrame frame = {NULL, NULL, 0u};
2832
+ OseoResult result = oseo_roots_allocate(context, &frame, key_count + 3u);
2833
+ if (result.status == OSEO_STATUS_NORMAL) {
2834
+ frame.slots[0] = rooted;
2835
+ result = snapshot_own_keys(context, &frame, key_count);
2836
+ }
2837
+ if (result.status == OSEO_STATUS_NORMAL) {
2838
+ result = oseo_array_create(context, 0u);
2839
+ frame.slots[1] = result.value;
2840
+ }
2841
+ for (size_t index = 0u;
2842
+ result.status == OSEO_STATUS_NORMAL && index < key_count;
2843
+ index += 1u) {
2844
+ OseoValue key = frame.slots[3u + index];
2845
+ if ((filter == OSEO_OWN_KEY_STRINGS && is_symbol(key)) ||
2846
+ (filter == OSEO_OWN_KEY_SYMBOLS && !is_symbol(key))) {
2847
+ continue;
2848
+ }
2849
+ result = oseo_array_append(context, frame.slots[1], key);
2850
+ }
2851
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
2852
+ oseo_roots_release(context, &frame);
2853
+ oseo_roots_pop(context, &root);
2854
+ return result;
2855
+ }
2856
+
2857
+ static OseoResult object_filtered_own_keys(
2858
+ OseoContext *context,
2859
+ size_t argument_count,
2860
+ const OseoValue *arguments,
2861
+ bool symbols
2862
+ ) {
2863
+ OseoResult converted = oseo_internal_to_object(
2864
+ context,
2865
+ builtin_argument(argument_count, arguments, 0u)
2866
+ );
2867
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
2868
+ return oseo_internal_own_key_array(
2869
+ context,
2870
+ converted.value,
2871
+ symbols ? OSEO_OWN_KEY_SYMBOLS : OSEO_OWN_KEY_STRINGS
2872
+ );
2873
+ }
2874
+
2875
+ typedef enum {
2876
+ OSEO_ENUMERABLE_KEYS,
2877
+ OSEO_ENUMERABLE_VALUES,
2878
+ OSEO_ENUMERABLE_ENTRIES,
2879
+ } OseoEnumerableOwnPropertyKind;
2880
+
2881
+ static OseoResult object_enumerable_own_properties(
2882
+ OseoContext *context,
2883
+ size_t argument_count,
2884
+ const OseoValue *arguments,
2885
+ OseoEnumerableOwnPropertyKind kind
2886
+ ) {
2887
+ OseoResult converted = oseo_internal_to_object(
2888
+ context,
2889
+ builtin_argument(argument_count, arguments, 0u)
2890
+ );
2891
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
2892
+ if (is_proxy(converted.value)) {
2893
+ OseoValue slots[5] = {
2894
+ converted.value,
2895
+ oseo_undefined(),
2896
+ oseo_undefined(),
2897
+ oseo_undefined(),
2898
+ oseo_undefined(),
2899
+ };
2900
+ OseoRootFrame proxy_frame = {NULL, slots, 5u};
2901
+ oseo_roots_push(context, &proxy_frame);
2902
+ OseoResult proxy_result = oseo_internal_proxy_own_keys(
2903
+ context, slots[0], OSEO_OWN_KEY_STRINGS);
2904
+ slots[1] = proxy_result.value;
2905
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
2906
+ proxy_result = oseo_internal_array_like_list(
2907
+ context, slots[1], &slots[2]);
2908
+ }
2909
+ size_t proxy_count = 0u;
2910
+ const OseoValue *proxy_keys = NULL;
2911
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
2912
+ proxy_result = oseo_argument_list_view(
2913
+ context, slots[2], &proxy_count, &proxy_keys);
2914
+ }
2915
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
2916
+ proxy_result = oseo_array_create(context, 0u);
2917
+ slots[3] = proxy_result.value;
2918
+ }
2919
+ for (size_t index = 0u;
2920
+ proxy_result.status == OSEO_STATUS_NORMAL &&
2921
+ index < proxy_count;
2922
+ index += 1u) {
2923
+ bool found = false;
2924
+ OseoValue descriptor_value = oseo_undefined();
2925
+ OseoValue descriptor_getter = oseo_undefined();
2926
+ OseoValue descriptor_setter = oseo_undefined();
2927
+ OseoPropertyAttributes descriptor = {false, false, false, false};
2928
+ proxy_result = oseo_internal_proxy_get_own_property(
2929
+ context,
2930
+ slots[0],
2931
+ proxy_keys[index],
2932
+ &found,
2933
+ &descriptor_value,
2934
+ &descriptor,
2935
+ &descriptor_getter,
2936
+ &descriptor_setter
2937
+ );
2938
+ if (proxy_result.status != OSEO_STATUS_NORMAL ||
2939
+ !found || !descriptor.enumerable) continue;
2940
+ if (kind == OSEO_ENUMERABLE_KEYS) {
2941
+ proxy_result = oseo_array_append(
2942
+ context, slots[3], proxy_keys[index]);
2943
+ continue;
2944
+ }
2945
+ proxy_result = oseo_object_get(
2946
+ context, slots[0], proxy_keys[index]);
2947
+ slots[4] = proxy_result.value;
2948
+ if (proxy_result.status != OSEO_STATUS_NORMAL) break;
2949
+ if (kind == OSEO_ENUMERABLE_VALUES) {
2950
+ proxy_result = oseo_array_append(
2951
+ context, slots[3], slots[4]);
2952
+ continue;
2953
+ }
2954
+ OseoResult pair = oseo_array_create(context, 0u);
2955
+ slots[1] = pair.value;
2956
+ if (pair.status == OSEO_STATUS_NORMAL) {
2957
+ pair = oseo_array_append(
2958
+ context, slots[1], proxy_keys[index]);
2959
+ }
2960
+ if (pair.status == OSEO_STATUS_NORMAL) {
2961
+ pair = oseo_array_append(context, slots[1], slots[4]);
2962
+ }
2963
+ if (pair.status == OSEO_STATUS_NORMAL) {
2964
+ pair = oseo_array_append(context, slots[3], slots[1]);
2965
+ }
2966
+ proxy_result = pair;
2967
+ }
2968
+ if (proxy_result.status == OSEO_STATUS_NORMAL) {
2969
+ proxy_result.value = slots[3];
2970
+ }
2971
+ oseo_roots_pop(context, &proxy_frame);
2972
+ return proxy_result;
2973
+ }
2974
+ OseoValue rooted = converted.value;
2975
+ OseoRootFrame root = {NULL, &rooted, 1u};
2976
+ oseo_roots_push(context, &root);
2977
+ size_t key_count = own_key_count(rooted);
2978
+ if (key_count > SIZE_MAX - 3u) {
2979
+ oseo_roots_pop(context, &root);
2980
+ return failure(context, "OSEO2001", "Own-key snapshot is too large.");
2981
+ }
2982
+ OseoRootFrame frame = {NULL, NULL, 0u};
2983
+ OseoResult result = oseo_roots_allocate(context, &frame, key_count + 3u);
2984
+ if (result.status == OSEO_STATUS_NORMAL) {
2985
+ frame.slots[0] = rooted;
2986
+ result = snapshot_own_keys(context, &frame, key_count);
2987
+ }
2988
+ if (result.status == OSEO_STATUS_NORMAL) {
2989
+ result = oseo_array_create(context, 0u);
2990
+ frame.slots[1] = result.value;
2991
+ }
2992
+ OseoValue scratch_slots[2] = {oseo_undefined(), oseo_undefined()};
2993
+ OseoRootFrame scratch = {NULL, scratch_slots, 2u};
2994
+ oseo_roots_push(context, &scratch);
2995
+ for (size_t index = 0u;
2996
+ result.status == OSEO_STATUS_NORMAL && index < key_count;
2997
+ index += 1u) {
2998
+ OseoValue key = frame.slots[3u + index];
2999
+ if (is_symbol(key)) continue;
3000
+ OseoValue ignored = oseo_undefined();
3001
+ OseoValue ignored_getter = oseo_undefined();
3002
+ OseoValue ignored_setter = oseo_undefined();
3003
+ OseoPropertyAttributes attributes = {false, false, false, false};
3004
+ bool exists = false;
3005
+ result = own_property_query(
3006
+ context,
3007
+ frame.slots[0],
3008
+ key,
3009
+ &exists,
3010
+ &ignored,
3011
+ &attributes,
3012
+ &ignored_getter,
3013
+ &ignored_setter
3014
+ );
3015
+ if (result.status != OSEO_STATUS_NORMAL) break;
3016
+ if (!exists || !attributes.enumerable) continue;
3017
+ if (kind == OSEO_ENUMERABLE_KEYS) {
3018
+ result = oseo_array_append(context, frame.slots[1], key);
3019
+ continue;
3020
+ }
3021
+ result = oseo_object_get(context, frame.slots[0], key);
3022
+ scratch_slots[0] = result.value;
3023
+ if (result.status != OSEO_STATUS_NORMAL) break;
3024
+ if (kind == OSEO_ENUMERABLE_VALUES) {
3025
+ result = oseo_array_append(
3026
+ context, frame.slots[1], scratch_slots[0]);
3027
+ continue;
3028
+ }
3029
+ result = oseo_array_create(context, 0u);
3030
+ scratch_slots[1] = result.value;
3031
+ if (result.status == OSEO_STATUS_NORMAL) {
3032
+ result = oseo_array_append(context, scratch_slots[1], key);
3033
+ }
3034
+ if (result.status == OSEO_STATUS_NORMAL) {
3035
+ result = oseo_array_append(
3036
+ context, scratch_slots[1], scratch_slots[0]);
3037
+ }
3038
+ if (result.status == OSEO_STATUS_NORMAL) {
3039
+ result = oseo_array_append(
3040
+ context, frame.slots[1], scratch_slots[1]);
3041
+ }
3042
+ }
3043
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
3044
+ oseo_roots_pop(context, &scratch);
3045
+ oseo_roots_release(context, &frame);
3046
+ oseo_roots_pop(context, &root);
3047
+ return result;
3048
+ }
3049
+
3050
+ static OseoResult object_assign(
3051
+ OseoContext *context,
3052
+ size_t argument_count,
3053
+ const OseoValue *arguments
3054
+ ) {
3055
+ OseoResult converted = oseo_internal_to_object(
3056
+ context,
3057
+ builtin_argument(argument_count, arguments, 0u)
3058
+ );
3059
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
3060
+ OseoValue target = converted.value;
3061
+ OseoRootFrame target_root = {NULL, &target, 1u};
3062
+ oseo_roots_push(context, &target_root);
3063
+ OseoResult result = normal(target);
3064
+ for (size_t source_index = 1u;
3065
+ result.status == OSEO_STATUS_NORMAL && source_index < argument_count;
3066
+ source_index += 1u) {
3067
+ if (is_nullish(arguments[source_index])) continue;
3068
+ converted = oseo_internal_to_object(context, arguments[source_index]);
3069
+ if (converted.status != OSEO_STATUS_NORMAL) {
3070
+ result = converted;
3071
+ break;
3072
+ }
3073
+ OseoValue source = converted.value;
3074
+ OseoRootFrame source_root = {NULL, &source, 1u};
3075
+ oseo_roots_push(context, &source_root);
3076
+ size_t key_count = 0u;
3077
+ OseoRootFrame frame = {NULL, NULL, 0u};
3078
+ if (is_proxy(source)) {
3079
+ result = proxy_own_key_frame(
3080
+ context, source, &frame, &key_count);
3081
+ } else {
3082
+ key_count = own_key_count(source);
3083
+ if (key_count > SIZE_MAX - 3u) {
3084
+ result = failure(
3085
+ context, "OSEO2001", "Own-key snapshot is too large.");
3086
+ }
3087
+ }
3088
+ if (result.status == OSEO_STATUS_NORMAL && !is_proxy(source)) {
3089
+ result = oseo_roots_allocate(context, &frame, key_count + 3u);
3090
+ }
3091
+ if (result.status == OSEO_STATUS_NORMAL) {
3092
+ frame.slots[0] = source;
3093
+ frame.slots[1] = target;
3094
+ if (!is_proxy(frame.slots[0])) {
3095
+ result = snapshot_own_keys(context, &frame, key_count);
3096
+ }
3097
+ }
3098
+ for (size_t index = 0u;
3099
+ result.status == OSEO_STATUS_NORMAL && index < key_count;
3100
+ index += 1u) {
3101
+ OseoValue key = frame.slots[3u + index];
3102
+ OseoValue ignored = oseo_undefined();
3103
+ OseoValue ignored_getter = oseo_undefined();
3104
+ OseoValue ignored_setter = oseo_undefined();
3105
+ OseoPropertyAttributes attributes = {false, false, false, false};
3106
+ bool exists = false;
3107
+ if (is_proxy(frame.slots[0])) {
3108
+ result = oseo_internal_proxy_get_own_property(
3109
+ context,
3110
+ frame.slots[0],
3111
+ key,
3112
+ &exists,
3113
+ &ignored,
3114
+ &attributes,
3115
+ &ignored_getter,
3116
+ &ignored_setter
3117
+ );
3118
+ if (result.status != OSEO_STATUS_NORMAL) break;
3119
+ } else {
3120
+ result = own_property_query(
3121
+ context,
3122
+ frame.slots[0],
3123
+ key,
3124
+ &exists,
3125
+ &ignored,
3126
+ &attributes,
3127
+ &ignored_getter,
3128
+ &ignored_setter
3129
+ );
3130
+ if (result.status != OSEO_STATUS_NORMAL) break;
3131
+ }
3132
+ if (!exists || !attributes.enumerable) {
3133
+ continue;
3134
+ }
3135
+ result = oseo_object_get(context, frame.slots[0], key);
3136
+ frame.slots[2] = result.value;
3137
+ if (result.status == OSEO_STATUS_NORMAL) {
3138
+ result = oseo_object_set(
3139
+ context,
3140
+ frame.slots[1],
3141
+ key,
3142
+ frame.slots[2],
3143
+ true
3144
+ );
3145
+ }
3146
+ }
3147
+ oseo_roots_release(context, &frame);
3148
+ oseo_roots_pop(context, &source_root);
3149
+ }
3150
+ if (result.status == OSEO_STATUS_NORMAL) result.value = target;
3151
+ oseo_roots_pop(context, &target_root);
3152
+ return result;
3153
+ }
3154
+
3155
+ static OseoResult object_entries(
3156
+ OseoContext *context,
3157
+ size_t argument_count,
3158
+ const OseoValue *arguments
3159
+ ) {
3160
+ return object_enumerable_own_properties(
3161
+ context, argument_count, arguments, OSEO_ENUMERABLE_ENTRIES);
3162
+ }
3163
+
3164
+ static OseoResult object_values(
3165
+ OseoContext *context,
3166
+ size_t argument_count,
3167
+ const OseoValue *arguments
3168
+ ) {
3169
+ return object_enumerable_own_properties(
3170
+ context, argument_count, arguments, OSEO_ENUMERABLE_VALUES);
3171
+ }
3172
+
3173
+ static OseoResult object_get_own_property_names(
3174
+ OseoContext *context,
3175
+ size_t argument_count,
3176
+ const OseoValue *arguments
3177
+ ) {
3178
+ return object_filtered_own_keys(context, argument_count, arguments, false);
3179
+ }
3180
+
3181
+ static OseoResult object_get_own_property_symbols(
3182
+ OseoContext *context,
3183
+ size_t argument_count,
3184
+ const OseoValue *arguments
3185
+ ) {
3186
+ return object_filtered_own_keys(context, argument_count, arguments, true);
3187
+ }
3188
+
3189
+ static OseoResult object_has_own(
3190
+ OseoContext *context,
3191
+ size_t argument_count,
3192
+ const OseoValue *arguments
3193
+ ) {
3194
+ OseoResult converted = oseo_internal_to_object(
3195
+ context,
3196
+ builtin_argument(argument_count, arguments, 0u)
3197
+ );
3198
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
3199
+ OseoValue slots[2] = {
3200
+ converted.value,
3201
+ builtin_argument(argument_count, arguments, 1u),
3202
+ };
3203
+ OseoRootFrame frame = {NULL, slots, 2u};
3204
+ oseo_roots_push(context, &frame);
3205
+ OseoResult result = oseo_property_key(context, slots[1]);
3206
+ slots[1] = result.value;
3207
+ if (result.status == OSEO_STATUS_NORMAL) {
3208
+ result = oseo_object_has_own(context, slots[0], slots[1]);
3209
+ }
3210
+ oseo_roots_pop(context, &frame);
3211
+ return result;
3212
+ }
3213
+
3214
+ /* Recover the String value a primitive or [[StringData]] wrapper stores. */
3215
+ static bool object_string_value(OseoValue source, OseoValue *string_value) {
3216
+ if (is_string(source)) {
3217
+ *string_value = source;
3218
+ return true;
3219
+ }
3220
+ if (!oseo_internal_string_data(source)) return false;
3221
+ *string_value = ordinary_object(source)->primitive_value;
3222
+ return true;
3223
+ }
3224
+
3225
+ /*
3226
+ * Decide whether a value iterates as a String through the realm's
3227
+ * untouched virtual %String.prototype%[Symbol.iterator] bootstrap slot. A
3228
+ * primitive String or a [[StringData]] wrapper that still reaches that
3229
+ * slot has no iterator object to acquire, so the caller walks code points
3230
+ * itself. Once the String iterator intrinsic materializes the property,
3231
+ * the ordinary own slot makes this branch dormant.
3232
+ * Anything else, including an own, inherited, replaced, or deleted
3233
+ * iterator, reports false and goes through observable iterator
3234
+ * acquisition. Both intrinsic lookups can allocate, so the frame roots
3235
+ * the source and its [[StringData]] rather than relying on the caller to
3236
+ * keep them reachable, and `string_value` is written after the last
3237
+ * allocation so the caller's rooted slot receives a live value.
3238
+ */
3239
+ static OseoResult object_virtual_string_iteration(
3240
+ OseoContext *context,
3241
+ OseoValue source,
3242
+ OseoValue *string_value,
3243
+ bool *direct
3244
+ ) {
3245
+ *direct = false;
3246
+ OseoValue candidate = oseo_undefined();
3247
+ if (!object_string_value(source, &candidate)) {
3248
+ return normal(oseo_undefined());
3249
+ }
3250
+ OseoValue slots[3] = {source, candidate, oseo_undefined()};
3251
+ OseoRootFrame frame = {NULL, slots, 3u};
3252
+ oseo_roots_push(context, &frame);
3253
+ OseoResult result = oseo_internal_well_known_symbol(
3254
+ context,
3255
+ OSEO_WELL_KNOWN_ITERATOR
3256
+ );
3257
+ slots[2] = result.value;
3258
+ if (result.status == OSEO_STATUS_NORMAL) {
3259
+ result = oseo_internal_primitive_wrapper_prototype(
3260
+ context,
3261
+ OSEO_INTRINSIC_STRING_PROTOTYPE
3262
+ );
3263
+ }
3264
+ if (result.status == OSEO_STATUS_NORMAL) {
3265
+ /* The prototype is used before the next allocation, so it needs
3266
+ * no slot of its own. */
3267
+ *direct = oseo_internal_uses_virtual_string_iterator(
3268
+ slots[0],
3269
+ result.value,
3270
+ slots[2]
3271
+ );
3272
+ *string_value = slots[1];
3273
+ }
3274
+ oseo_roots_pop(context, &frame);
3275
+ return result;
3276
+ }
3277
+
3278
+ /*
3279
+ * Code-unit length of the code point the default String iterator yields
3280
+ * at `offset`, pairing a leading surrogate with a following trailing one
3281
+ * and reporting a lone surrogate as a single unit. Each caller reacquires
3282
+ * its OseoString from a rooted slot on every step, so the interior
3283
+ * `units` pointer it then hands to oseo_string_from_units stays valid:
3284
+ * the collector sweeps unreachable objects in place and never relocates a
3285
+ * reachable one.
3286
+ */
3287
+ static size_t object_string_element_length(
3288
+ const OseoString *source,
3289
+ size_t offset
3290
+ ) {
3291
+ uint16_t first = source->units[offset];
3292
+ if (first < UINT16_C(0xd800) || first > UINT16_C(0xdbff)) return 1u;
3293
+ if (offset + 1u >= source->length) return 1u;
3294
+ uint16_t second = source->units[offset + 1u];
3295
+ if (second < UINT16_C(0xdc00) || second > UINT16_C(0xdfff)) return 1u;
3296
+ return 2u;
3297
+ }
3298
+
3299
+ /*
3300
+ * CreateDataPropertyOnObject (20.1.2.7 step 5.c onward) for the entry now
3301
+ * in slots[4], defining its "0" key and "1" value on the target object in
3302
+ * slots[3]. slots[5] through slots[7] are the caller's rooted scratch.
3303
+ */
3304
+ static OseoResult object_define_entry(OseoContext *context, OseoValue *slots) {
3305
+ if (!is_object(slots[4])) {
3306
+ return type_error(context, "Iterator value is not an entry object.");
3307
+ }
3308
+ OseoResult result = oseo_internal_ascii_string(context, "0");
3309
+ slots[5] = result.value;
3310
+ if (result.status == OSEO_STATUS_NORMAL) {
3311
+ result = oseo_object_get(context, slots[4], slots[5]);
3312
+ slots[5] = result.value;
3313
+ }
3314
+ if (result.status == OSEO_STATUS_NORMAL) {
3315
+ result = oseo_internal_ascii_string(context, "1");
3316
+ slots[6] = result.value;
3317
+ }
3318
+ if (result.status == OSEO_STATUS_NORMAL) {
3319
+ result = oseo_object_get(context, slots[4], slots[6]);
3320
+ slots[6] = result.value;
3321
+ }
3322
+ if (result.status == OSEO_STATUS_NORMAL) {
3323
+ result = oseo_property_key(context, slots[5]);
3324
+ slots[7] = result.value;
3325
+ }
3326
+ if (result.status == OSEO_STATUS_NORMAL) {
3327
+ result = oseo_object_define(
3328
+ context,
3329
+ slots[3],
3330
+ slots[7],
3331
+ slots[6],
3332
+ (OseoPropertyAttributes){true, true, true, false}
3333
+ );
3334
+ }
3335
+ return result;
3336
+ }
3337
+
3338
+ static OseoResult object_from_entries(
3339
+ OseoContext *context,
3340
+ size_t argument_count,
3341
+ const OseoValue *arguments
3342
+ ) {
3343
+ OseoValue slots[9] = {
3344
+ builtin_argument(argument_count, arguments, 0u),
3345
+ oseo_undefined(),
3346
+ oseo_undefined(),
3347
+ oseo_undefined(),
3348
+ oseo_undefined(),
3349
+ oseo_undefined(),
3350
+ oseo_undefined(),
3351
+ oseo_undefined(),
3352
+ oseo_undefined(),
3353
+ };
3354
+ OseoRootFrame frame = {NULL, slots, 9u};
3355
+ oseo_roots_push(context, &frame);
3356
+ bool direct_string = false;
3357
+ OseoResult result = object_virtual_string_iteration(
3358
+ context,
3359
+ slots[0],
3360
+ &slots[8],
3361
+ &direct_string
3362
+ );
3363
+ if (result.status == OSEO_STATUS_NORMAL) {
3364
+ result = oseo_object_literal_create(context);
3365
+ slots[3] = result.value;
3366
+ }
3367
+ if (result.status == OSEO_STATUS_NORMAL && !direct_string) {
3368
+ result = oseo_iterator_get(context, slots[0], &slots[2]);
3369
+ slots[1] = result.value;
3370
+ }
3371
+ if (result.status == OSEO_STATUS_NORMAL && direct_string) {
3372
+ /*
3373
+ * The bootstrap virtual default has no iterator object, so walk
3374
+ * the default code-point sequence here as Object.groupBy and
3375
+ * Array.from do. An empty String
3376
+ * yields nothing and produces an empty object; every element a
3377
+ * non-empty String yields is a primitive, so the first one
3378
+ * reaches the entry-object TypeError below. There is no iterator
3379
+ * object to close on that abrupt completion.
3380
+ */
3381
+ size_t offset = 0u;
3382
+ while (result.status == OSEO_STATUS_NORMAL) {
3383
+ OseoString *source = string_object(slots[8]);
3384
+ if (offset >= source->length) break;
3385
+ size_t element_length =
3386
+ object_string_element_length(source, offset);
3387
+ result = oseo_string_from_units(
3388
+ context,
3389
+ &source->units[offset],
3390
+ element_length
3391
+ );
3392
+ slots[4] = result.value;
3393
+ if (result.status == OSEO_STATUS_NORMAL) {
3394
+ result = object_define_entry(context, slots);
3395
+ }
3396
+ offset += element_length;
3397
+ }
3398
+ }
3399
+ bool done = false;
3400
+ while (result.status == OSEO_STATUS_NORMAL && !direct_string && !done) {
3401
+ result = oseo_iterator_next(
3402
+ context, slots[1], slots[2], &slots[4], &done);
3403
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
3404
+ result = object_define_entry(context, slots);
3405
+ if (result.status != OSEO_STATUS_NORMAL) {
3406
+ result = object_close_after_abrupt(context, slots[1], result);
3407
+ }
3408
+ }
3409
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[3];
3410
+ oseo_roots_pop(context, &frame);
3411
+ return result;
3412
+ }
3413
+
3414
+ static OseoResult object_add_grouped_value(
3415
+ OseoContext *context,
3416
+ OseoValue *slots,
3417
+ double index
3418
+ ) {
3419
+ OseoValue callback_arguments[2] = {
3420
+ slots[5],
3421
+ oseo_number(index),
3422
+ };
3423
+ OseoResult result = oseo_call_function(
3424
+ context,
3425
+ slots[1],
3426
+ oseo_undefined(),
3427
+ 2u,
3428
+ callback_arguments,
3429
+ oseo_undefined()
3430
+ );
3431
+ slots[6] = result.value;
3432
+ if (result.status == OSEO_STATUS_NORMAL) {
3433
+ result = oseo_property_key(context, slots[6]);
3434
+ slots[6] = result.value;
3435
+ }
3436
+ if (result.status == OSEO_STATUS_NORMAL) {
3437
+ result = oseo_object_has_own(context, slots[4], slots[6]);
3438
+ }
3439
+ bool exists = result.status == OSEO_STATUS_NORMAL &&
3440
+ oseo_to_boolean(result.value);
3441
+ if (result.status == OSEO_STATUS_NORMAL && exists) {
3442
+ result = oseo_object_get(context, slots[4], slots[6]);
3443
+ slots[7] = result.value;
3444
+ } else if (result.status == OSEO_STATUS_NORMAL) {
3445
+ result = oseo_array_create(context, 0u);
3446
+ slots[7] = result.value;
3447
+ if (result.status == OSEO_STATUS_NORMAL) {
3448
+ result = oseo_object_define(
3449
+ context,
3450
+ slots[4],
3451
+ slots[6],
3452
+ slots[7],
3453
+ (OseoPropertyAttributes){true, true, true, false}
3454
+ );
3455
+ }
3456
+ }
3457
+ if (result.status == OSEO_STATUS_NORMAL) {
3458
+ result = oseo_array_append(context, slots[7], slots[5]);
3459
+ }
3460
+ return result;
3461
+ }
3462
+
3463
+ static OseoResult object_group_by(
3464
+ OseoContext *context,
3465
+ size_t argument_count,
3466
+ const OseoValue *arguments
3467
+ ) {
3468
+ OseoValue slots[9] = {
3469
+ builtin_argument(argument_count, arguments, 0u),
3470
+ builtin_argument(argument_count, arguments, 1u),
3471
+ oseo_undefined(),
3472
+ oseo_undefined(),
3473
+ oseo_undefined(),
3474
+ oseo_undefined(),
3475
+ oseo_undefined(),
3476
+ oseo_undefined(),
3477
+ oseo_undefined(),
3478
+ };
3479
+ OseoRootFrame frame = {NULL, slots, 9u};
3480
+ oseo_roots_push(context, &frame);
3481
+ OseoResult result = normal(oseo_undefined());
3482
+ if (is_nullish(slots[0])) {
3483
+ result = type_error(context, "Object.groupBy requires an iterable.");
3484
+ } else if (!is_callable(slots[1])) {
3485
+ result = type_error(
3486
+ context,
3487
+ "Object.groupBy callback is not callable."
3488
+ );
3489
+ }
3490
+ bool direct_string = false;
3491
+ if (result.status == OSEO_STATUS_NORMAL) {
3492
+ result = object_virtual_string_iteration(
3493
+ context,
3494
+ slots[0],
3495
+ &slots[8],
3496
+ &direct_string
3497
+ );
3498
+ }
3499
+ if (result.status == OSEO_STATUS_NORMAL && !direct_string) {
3500
+ result = oseo_iterator_get(context, slots[0], &slots[3]);
3501
+ slots[2] = result.value;
3502
+ }
3503
+ if (result.status == OSEO_STATUS_NORMAL) {
3504
+ result = oseo_object_create(context, oseo_null());
3505
+ slots[4] = result.value;
3506
+ }
3507
+ double index = 0.0;
3508
+ if (result.status == OSEO_STATUS_NORMAL && direct_string) {
3509
+ /*
3510
+ * The bootstrap virtual default has no iterator object. Preserve
3511
+ * its default code-point iteration here, just as Array.from does.
3512
+ * The materialized String iterator makes this branch dormant.
3513
+ */
3514
+ size_t offset = 0u;
3515
+ while (result.status == OSEO_STATUS_NORMAL) {
3516
+ OseoString *source = string_object(slots[8]);
3517
+ if (offset >= source->length) break;
3518
+ size_t element_length =
3519
+ object_string_element_length(source, offset);
3520
+ result = oseo_string_from_units(
3521
+ context,
3522
+ &source->units[offset],
3523
+ element_length
3524
+ );
3525
+ slots[5] = result.value;
3526
+ if (result.status == OSEO_STATUS_NORMAL) {
3527
+ result = object_add_grouped_value(context, slots, index);
3528
+ }
3529
+ offset += element_length;
3530
+ index += 1.0;
3531
+ }
3532
+ }
3533
+ bool done = false;
3534
+ while (result.status == OSEO_STATUS_NORMAL && !direct_string && !done) {
3535
+ result = oseo_iterator_next(
3536
+ context, slots[2], slots[3], &slots[5], &done);
3537
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
3538
+ if (index >= 9007199254740991.0) {
3539
+ result = oseo_internal_throw_error(
3540
+ context,
3541
+ OSEO_ERROR_RANGE,
3542
+ "Object.groupBy index is too large."
3543
+ );
3544
+ }
3545
+ if (result.status == OSEO_STATUS_NORMAL) {
3546
+ result = object_add_grouped_value(context, slots, index);
3547
+ }
3548
+ if (result.status != OSEO_STATUS_NORMAL) {
3549
+ result = object_close_after_abrupt(context, slots[2], result);
3550
+ }
3551
+ index += 1.0;
3552
+ }
3553
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[4];
3554
+ oseo_roots_pop(context, &frame);
3555
+ return result;
3556
+ }
3557
+
3558
+ /*
3559
+ * Object.getOwnPropertyDescriptors (20.1.2.9). ToObject runs before
3560
+ * anything else, so a nullish argument throws and a primitive is
3561
+ * reported through the wrapper object it converts to, including a
3562
+ * String wrapper's index and `length` properties. Every own key the
3563
+ * conversion result reports contributes one FromPropertyDescriptor
3564
+ * object, created as a writable, enumerable, configurable data property
3565
+ * of an ordinary object.
3566
+ */
3567
+ static OseoResult object_get_own_property_descriptors(
3568
+ OseoContext *context,
3569
+ size_t argument_count,
3570
+ const OseoValue *arguments
3571
+ ) {
3572
+ OseoValue value = builtin_argument(argument_count, arguments, 0u);
3573
+ if (is_nullish(value)) {
3574
+ return type_error(
3575
+ context,
3576
+ "Cannot convert a nullish value to an object."
3577
+ );
3578
+ }
3579
+ OseoResult converted = oseo_internal_to_object(context, value);
3580
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
3581
+ size_t key_count = 0u;
3582
+ /* The key frame roots the conversion result, the reported object,
3583
+ * the one synthesized key string, and the whole key snapshot. The
3584
+ * descriptor frame is the four-slot scratch every
3585
+ * FromPropertyDescriptor call reuses. */
3586
+ OseoRootFrame frame = {NULL, NULL, 0u};
3587
+ OseoResult result = normal(oseo_undefined());
3588
+ if (is_proxy(converted.value)) {
3589
+ result = proxy_own_key_frame(
3590
+ context, converted.value, &frame, &key_count);
3591
+ } else {
3592
+ key_count = own_key_count(converted.value);
3593
+ if (key_count > SIZE_MAX - 3u) {
3594
+ return failure(
3595
+ context, "OSEO2001", "Own-key snapshot is too large.");
3596
+ }
3597
+ result = oseo_roots_allocate(context, &frame, key_count + 3u);
3598
+ }
3599
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3600
+ frame.slots[0] = converted.value;
3601
+ OseoRootFrame descriptor = {NULL, NULL, 0u};
3602
+ result = oseo_roots_allocate(context, &descriptor, 4u);
3603
+ if (result.status != OSEO_STATUS_NORMAL) {
3604
+ oseo_roots_release(context, &frame);
3605
+ return result;
3606
+ }
3607
+ if (!is_proxy(frame.slots[0])) {
3608
+ result = snapshot_own_keys(context, &frame, key_count);
3609
+ }
3610
+ if (result.status == OSEO_STATUS_NORMAL) {
3611
+ result = oseo_object_literal_create(context);
3612
+ frame.slots[1] = result.value;
3613
+ }
3614
+ for (size_t index = 0u;
3615
+ result.status == OSEO_STATUS_NORMAL && index < key_count;
3616
+ index += 1u) {
3617
+ OseoValue key = frame.slots[3u + index];
3618
+ OseoValue own = oseo_undefined();
3619
+ OseoPropertyAttributes attributes = {false, false, false, false};
3620
+ OseoValue getter = oseo_undefined();
3621
+ OseoValue setter = oseo_undefined();
3622
+ bool exists = false;
3623
+ if (is_proxy(frame.slots[0])) {
3624
+ result = oseo_internal_proxy_get_own_property(
3625
+ context,
3626
+ frame.slots[0],
3627
+ key,
3628
+ &exists,
3629
+ &own,
3630
+ &attributes,
3631
+ &getter,
3632
+ &setter
3633
+ );
3634
+ if (result.status != OSEO_STATUS_NORMAL) break;
3635
+ } else {
3636
+ result = own_property_query(
3637
+ context,
3638
+ frame.slots[0],
3639
+ key,
3640
+ &exists,
3641
+ &own,
3642
+ &attributes,
3643
+ &getter,
3644
+ &setter
3645
+ );
3646
+ if (result.status != OSEO_STATUS_NORMAL) break;
3647
+ }
3648
+ if (!exists) continue;
3649
+ descriptor.slots[2] = attributes.accessor ? getter : own;
3650
+ descriptor.slots[3] = setter;
3651
+ if (oseo_internal_cell_backed_property(frame.slots[0], own)) {
3652
+ result = oseo_cell_get(context, own);
3653
+ descriptor.slots[2] = result.value;
3654
+ if (result.status != OSEO_STATUS_NORMAL) break;
3655
+ }
3656
+ result = from_property_descriptor(context, &descriptor,
3657
+ attributes);
3658
+ if (result.status != OSEO_STATUS_NORMAL) break;
3659
+ result = oseo_object_define(
3660
+ context,
3661
+ frame.slots[1],
3662
+ key,
3663
+ descriptor.slots[0],
3664
+ (OseoPropertyAttributes){true, true, true, false}
3665
+ );
3666
+ }
3667
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
3668
+ oseo_roots_release(context, &descriptor);
3669
+ oseo_roots_release(context, &frame);
3670
+ return result;
3671
+ }
3672
+
3673
+ /*
3674
+ * Object.defineProperties (20.1.2.3) over ObjectDefineProperties
3675
+ * (20.1.2.3.1). The target check precedes every read of the properties
3676
+ * argument, and ToObject runs before the own-key walk, so a nullish
3677
+ * properties argument throws and a primitive is read through the
3678
+ * wrapper it converts to, including a String wrapper's index
3679
+ * properties. The walk visits ordinary own keys in order and keeps the
3680
+ * own enumerable ones, reading each descriptor with Get so an accessor
3681
+ * runs, and converts every descriptor through one ToPropertyDescriptor
3682
+ * body before the first definition mutates the target. An abrupt
3683
+ * completion while collecting therefore leaves the target untouched,
3684
+ * while an abrupt definition keeps every definition that preceded it.
3685
+ */
3686
+ static OseoResult object_define_properties(
3687
+ OseoContext *context,
3688
+ size_t argument_count,
3689
+ const OseoValue *arguments
3690
+ ) {
3691
+ OseoValue object_value = builtin_argument(argument_count, arguments, 0u);
3692
+ if (!is_object(object_value)) {
3693
+ return type_error(
3694
+ context,
3695
+ "Object.defineProperties requires an object target."
3696
+ );
3697
+ }
3698
+ OseoResult converted = oseo_internal_to_object(
3699
+ context,
3700
+ builtin_argument(argument_count, arguments, 1u)
3701
+ );
3702
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
3703
+ size_t key_count = 0u;
3704
+ /* The key frame roots the properties object, the target, the one
3705
+ * synthesized key string, and the whole key snapshot. The collected
3706
+ * frame holds four slots per collected descriptor, in the order
3707
+ * key, value, getter, setter, and one final slot that roots each
3708
+ * descriptor object while its fields are read. */
3709
+ OseoRootFrame frame = {NULL, NULL, 0u};
3710
+ OseoResult result = normal(oseo_undefined());
3711
+ if (is_proxy(converted.value)) {
3712
+ result = proxy_own_key_frame(
3713
+ context, converted.value, &frame, &key_count);
3714
+ } else {
3715
+ key_count = own_key_count(converted.value);
3716
+ if (key_count > SIZE_MAX - 3u) {
3717
+ return failure(
3718
+ context, "OSEO2001", "Own-key snapshot is too large.");
3719
+ }
3720
+ result = oseo_roots_allocate(context, &frame, key_count + 3u);
3721
+ }
3722
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3723
+ if (key_count > (SIZE_MAX - 1u) / 4u) {
3724
+ oseo_roots_release(context, &frame);
3725
+ return failure(
3726
+ context, "OSEO2001", "Own-key snapshot is too large.");
3727
+ }
3728
+ frame.slots[0] = converted.value;
3729
+ frame.slots[1] = object_value;
3730
+ OseoRootFrame collected_frame = {NULL, NULL, 0u};
3731
+ result = oseo_roots_allocate(
3732
+ context,
3733
+ &collected_frame,
3734
+ 4u * key_count + 1u
3735
+ );
3736
+ if (result.status != OSEO_STATUS_NORMAL) {
3737
+ oseo_roots_release(context, &frame);
3738
+ return result;
3739
+ }
3740
+ OseoConvertedDescriptor *records = NULL;
3741
+ if (key_count > 0u) {
3742
+ records = malloc(key_count * sizeof(*records));
3743
+ if (records == NULL) {
3744
+ result = failure(
3745
+ context,
3746
+ "OSEO2001",
3747
+ "Descriptor collection allocation failed."
3748
+ );
3749
+ }
3750
+ }
3751
+ if (result.status == OSEO_STATUS_NORMAL &&
3752
+ !is_proxy(frame.slots[0])) {
3753
+ result = snapshot_own_keys(context, &frame, key_count);
3754
+ }
3755
+ size_t collected = 0u;
3756
+ for (size_t index = 0u;
3757
+ result.status == OSEO_STATUS_NORMAL && index < key_count;
3758
+ index += 1u) {
3759
+ OseoValue key = frame.slots[3u + index];
3760
+ OseoValue ignored = oseo_undefined();
3761
+ OseoPropertyAttributes attributes = {false, false, false, false};
3762
+ OseoValue ignored_getter = oseo_undefined();
3763
+ OseoValue ignored_setter = oseo_undefined();
3764
+ /* A getter an earlier key ran may have removed this key or made
3765
+ * it non-enumerable, so the descriptor is re-read per key. */
3766
+ bool exists = false;
3767
+ if (is_proxy(frame.slots[0])) {
3768
+ result = oseo_internal_proxy_get_own_property(
3769
+ context,
3770
+ frame.slots[0],
3771
+ key,
3772
+ &exists,
3773
+ &ignored,
3774
+ &attributes,
3775
+ &ignored_getter,
3776
+ &ignored_setter
3777
+ );
3778
+ if (result.status != OSEO_STATUS_NORMAL) break;
3779
+ } else {
3780
+ result = own_property_query(
3781
+ context,
3782
+ frame.slots[0],
3783
+ key,
3784
+ &exists,
3785
+ &ignored,
3786
+ &attributes,
3787
+ &ignored_getter,
3788
+ &ignored_setter
3789
+ );
3790
+ if (result.status != OSEO_STATUS_NORMAL) break;
3791
+ }
3792
+ if (!exists || !attributes.enumerable) {
3793
+ continue;
3794
+ }
3795
+ result = oseo_object_get(context, frame.slots[0], key);
3796
+ collected_frame.slots[4u * key_count] = result.value;
3797
+ if (result.status != OSEO_STATUS_NORMAL) break;
3798
+ if (!is_object(result.value)) {
3799
+ result = type_error(
3800
+ context,
3801
+ "A property descriptor must be an object."
3802
+ );
3803
+ break;
3804
+ }
3805
+ OseoValue *slots = &collected_frame.slots[4u * collected];
3806
+ slots[0] = key;
3807
+ result = oseo_internal_to_property_descriptor(
3808
+ context,
3809
+ collected_frame.slots[4u * key_count],
3810
+ &slots[1],
3811
+ &slots[2],
3812
+ &slots[3],
3813
+ &records[collected]
3814
+ );
3815
+ if (result.status != OSEO_STATUS_NORMAL) break;
3816
+ collected += 1u;
3817
+ }
3818
+ const char *refusal = NULL;
3819
+ for (size_t index = 0u;
3820
+ result.status == OSEO_STATUS_NORMAL && index < collected;
3821
+ index += 1u) {
3822
+ OseoValue *slots = &collected_frame.slots[4u * index];
3823
+ result = oseo_internal_define_converted_property(
3824
+ context,
3825
+ frame.slots[1],
3826
+ slots[0],
3827
+ &records[index],
3828
+ slots[1],
3829
+ slots[2],
3830
+ slots[3],
3831
+ &refusal
3832
+ );
3833
+ if (result.status == OSEO_STATUS_NORMAL && refusal != NULL) {
3834
+ result = type_error(context, refusal);
3835
+ }
3836
+ }
3837
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
3838
+ free(records);
3839
+ oseo_roots_release(context, &collected_frame);
3840
+ oseo_roots_release(context, &frame);
3841
+ return result;
3842
+ }
3843
+
3844
+ OseoResult oseo_object_builtin_keys(
3845
+ OseoContext *context,
3846
+ size_t argument_count,
3847
+ const OseoValue *arguments
3848
+ ) {
3849
+ return object_enumerable_own_properties(
3850
+ context, argument_count, arguments, OSEO_ENUMERABLE_KEYS);
3851
+ }
3852
+
3853
+ OseoResult oseo_object_builtin_set_prototype_of(
3854
+ OseoContext *context,
3855
+ size_t argument_count,
3856
+ const OseoValue *arguments
3857
+ ) {
3858
+ OseoValue object_value = builtin_argument(
3859
+ argument_count,
3860
+ arguments,
3861
+ 0u
3862
+ );
3863
+ OseoValue prototype = builtin_argument(argument_count, arguments, 1u);
3864
+ if (is_nullish(object_value) ||
3865
+ (tag_of(prototype) != OSEO_TAG_NULL && !is_object(prototype))) {
3866
+ return type_error(
3867
+ context,
3868
+ "Object.setPrototypeOf requires an object prototype."
3869
+ );
3870
+ }
3871
+ if (!is_object(object_value)) return normal(object_value);
3872
+ return oseo_object_set_prototype(
3873
+ context,
3874
+ object_value,
3875
+ prototype
3876
+ );
3877
+ }