@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,1080 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <math.h>
7
+
8
+ /*
9
+ * Property descriptors: [[GetOwnProperty]], [[DefineOwnProperty]] for
10
+ * data and accessor descriptors, [[Delete]], and the SameValue
11
+ * comparison ValidateAndApplyPropertyDescriptor performs.
12
+ */
13
+
14
+ static OseoResult type_error(OseoContext *context, const char *message) {
15
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
16
+ }
17
+
18
+ /*
19
+ * Routes a canonical numeric key of a TypedArray to the view's
20
+ * [[DefineOwnProperty]]. `*handled` stays false for every
21
+ * other key and object, which keeps the ordinary definition below.
22
+ */
23
+ static OseoResult define_typed_array_key(
24
+ OseoContext *context,
25
+ OseoValue object_value,
26
+ OseoValue key,
27
+ const OseoConvertedDescriptor *descriptor,
28
+ OseoValue value,
29
+ bool *handled,
30
+ const char **refusal
31
+ ) {
32
+ *handled = false;
33
+ if (!is_typed_array(object_value)) return normal(object_value);
34
+ size_t index = SIZE_MAX;
35
+ OseoResult result = oseo_internal_typed_array_numeric_key(
36
+ context,
37
+ key,
38
+ handled,
39
+ &index
40
+ );
41
+ if (result.status != OSEO_STATUS_NORMAL) return result;
42
+ if (!*handled) return normal(object_value);
43
+ return oseo_internal_typed_array_define_index(
44
+ context,
45
+ object_value,
46
+ index,
47
+ descriptor,
48
+ value,
49
+ refusal
50
+ );
51
+ }
52
+
53
+ /* A concrete descriptor replacing the virtual String iterator keeps the
54
+ * virtual property's original position before every user-created symbol.
55
+ * The property vector already owns creation order, so inserting the
56
+ * replacement at its first symbol slot lets ordinary deletion and later
57
+ * redefinition recover the usual append-at-creation behavior. */
58
+ static OseoProperty *append_property_slot(
59
+ OseoOrdinaryObject *object,
60
+ bool replaces_virtual
61
+ ) {
62
+ size_t insertion = object->property_count;
63
+ if (replaces_virtual) {
64
+ for (size_t index = 0u;
65
+ index < object->property_count;
66
+ index += 1u) {
67
+ if (is_symbol(object->properties[index].key)) {
68
+ insertion = index;
69
+ break;
70
+ }
71
+ }
72
+ }
73
+ for (size_t index = object->property_count;
74
+ index > insertion;
75
+ index -= 1u) {
76
+ object->properties[index] = object->properties[index - 1u];
77
+ }
78
+ object->property_count += 1u;
79
+ return &object->properties[insertion];
80
+ }
81
+ OseoResult oseo_internal_legacy_materialize(
82
+ OseoContext *context,
83
+ OseoValue object_value,
84
+ OseoValue key
85
+ ) {
86
+ uint8_t live = oseo_internal_legacy_live_key(object_value, key);
87
+ if (live == 0u) return normal(oseo_undefined());
88
+ OseoValue current = oseo_undefined();
89
+ OseoPropertyAttributes attributes = {false, false, false, false};
90
+ (void)oseo_internal_legacy_descriptor(
91
+ context, object_value, key, &current, &attributes);
92
+ OseoRootFrame frame = {NULL, NULL, 0u};
93
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
94
+ if (result.status != OSEO_STATUS_NORMAL) return result;
95
+ frame.slots[0] = object_value;
96
+ frame.slots[1] = key;
97
+ result = oseo_internal_legacy_value(context, object_value, key, current);
98
+ if (result.status == OSEO_STATUS_NORMAL) {
99
+ frame.slots[2] = result.value;
100
+ result = oseo_internal_grow_properties(context, frame.slots[0]);
101
+ }
102
+ if (result.status != OSEO_STATUS_NORMAL) {
103
+ oseo_roots_release(context, &frame);
104
+ return result;
105
+ }
106
+ OseoFunction *function = function_object(frame.slots[0]);
107
+ OseoOrdinaryObject *object = ordinary_object(frame.slots[0]);
108
+ size_t target = live == OSEO_LEGACY_ARGUMENTS_LIVE
109
+ ? function->legacy_arguments_position
110
+ : function->prototype_key_position;
111
+ size_t insertion = object->property_count;
112
+ size_t rank = 0u;
113
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
114
+ uint32_t ignored = 0u;
115
+ OseoValue scanned = object->properties[index].key;
116
+ if (is_symbol(scanned) ||
117
+ oseo_internal_array_index(scanned, &ignored)) continue;
118
+ if (rank == target) {
119
+ insertion = index;
120
+ break;
121
+ }
122
+ rank += 1u;
123
+ }
124
+ for (size_t index = object->property_count;
125
+ index > insertion;
126
+ index -= 1u) {
127
+ object->properties[index] = object->properties[index - 1u];
128
+ }
129
+ object->property_count += 1u;
130
+ OseoProperty *property = &object->properties[insertion];
131
+ property->attributes = (OseoPropertyAttributes){true, false, false, false};
132
+ property->key = frame.slots[1];
133
+ property->value = frame.slots[2];
134
+ property->getter = oseo_undefined();
135
+ property->setter = oseo_undefined();
136
+ oseo_internal_note_property_inserted(object, insertion);
137
+ /* Both live keys sit in front of `prototype`, so the new slot always
138
+ * moves it one place later; a live `arguments` stays in front of a
139
+ * moved `caller` at its unchanged rank. */
140
+ function->prototype_key_position += 1u;
141
+ function->legacy_live &= (uint8_t)~live;
142
+ object->dictionary = true;
143
+ object->shape_id = context->next_shape_id;
144
+ context->next_shape_id += 1u;
145
+ oseo_roots_release(context, &frame);
146
+ return normal(oseo_undefined());
147
+ }
148
+
149
+ bool oseo_internal_same_value(OseoValue left, OseoValue right) {
150
+ if (is_number(left) && is_number(right)) {
151
+ double left_number = number_value(left);
152
+ double right_number = number_value(right);
153
+ if (isnan(left_number) && isnan(right_number)) return true;
154
+ if (left_number == 0.0 && right_number == 0.0) {
155
+ return signbit(left_number) == signbit(right_number);
156
+ }
157
+ return left_number == right_number;
158
+ }
159
+ if (is_string(left) && is_string(right)) {
160
+ return oseo_internal_string_equal(left, right);
161
+ }
162
+ if (is_bigint(left) && is_bigint(right)) {
163
+ return oseo_internal_bigint_equal(left, right);
164
+ }
165
+ return left == right || same_registered_symbol(left, right);
166
+ }
167
+
168
+ bool oseo_internal_same_value_zero(OseoValue left, OseoValue right) {
169
+ if (is_number(left) && is_number(right)) {
170
+ double left_number = number_value(left);
171
+ double right_number = number_value(right);
172
+ if (isnan(left_number) && isnan(right_number)) return true;
173
+ return left_number == right_number;
174
+ }
175
+ if (is_string(left) && is_string(right)) {
176
+ return oseo_internal_string_equal(left, right);
177
+ }
178
+ if (is_bigint(left) && is_bigint(right)) {
179
+ return oseo_internal_bigint_equal(left, right);
180
+ }
181
+ return left == right || same_registered_symbol(left, right);
182
+ }
183
+
184
+ bool oseo_internal_virtual_string_iterator_descriptor(
185
+ OseoContext *context,
186
+ OseoValue object_value,
187
+ OseoValue key,
188
+ OseoPropertyAttributes *attributes
189
+ ) {
190
+ if (!is_object(object_value) ||
191
+ object_value !=
192
+ context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE] ||
193
+ key != context->well_known_symbols[OSEO_WELL_KNOWN_ITERATOR]) {
194
+ return false;
195
+ }
196
+ OseoOrdinaryObject *object = ordinary_object(object_value);
197
+ if (!object->virtual_string_iterator) return false;
198
+ *attributes = (OseoPropertyAttributes){
199
+ object->virtual_string_iterator_configurable,
200
+ object->virtual_string_iterator_enumerable,
201
+ object->virtual_string_iterator_writable,
202
+ false,
203
+ };
204
+ return true;
205
+ }
206
+
207
+ bool oseo_internal_own_descriptor(
208
+ OseoValue object_value,
209
+ OseoValue key,
210
+ OseoValue *value,
211
+ OseoPropertyAttributes *attributes,
212
+ OseoValue *getter,
213
+ OseoValue *setter
214
+ ) {
215
+ *getter = oseo_undefined();
216
+ *setter = oseo_undefined();
217
+ /* Without the context, the legacy `caller` and `arguments` values
218
+ * are unknown; oseo_internal_own_property_descriptor answers them.
219
+ * Every context-free caller reads only presence and attributes. */
220
+ if (oseo_internal_legacy_live_key(object_value, key) != 0u) {
221
+ *value = oseo_null();
222
+ *attributes = (OseoPropertyAttributes){true, false, false, false};
223
+ return true;
224
+ }
225
+ if (function_has_prototype_property(object_value) &&
226
+ oseo_internal_string_is_ascii(key, "prototype")) {
227
+ *value = function_object(object_value)->prototype_object;
228
+ *attributes = (OseoPropertyAttributes){
229
+ false,
230
+ false,
231
+ function_object(object_value)->prototype_writable,
232
+ false,
233
+ };
234
+ return true;
235
+ }
236
+ if (is_array(object_value) &&
237
+ oseo_internal_string_is_ascii(key, "length")) {
238
+ OseoOrdinaryObject *array = ordinary_object(object_value);
239
+ *value = oseo_number(array->array_length);
240
+ *attributes = (OseoPropertyAttributes){
241
+ false,
242
+ false,
243
+ array->length_writable,
244
+ false,
245
+ };
246
+ return true;
247
+ }
248
+ OseoOrdinaryObject *object = ordinary_object(object_value);
249
+ size_t index = oseo_internal_own_property_index(object, key);
250
+ if (index == SIZE_MAX) return false;
251
+ OseoProperty *property = &object->properties[index];
252
+ *attributes = property->attributes;
253
+ if (property->attributes.accessor) {
254
+ *value = oseo_undefined();
255
+ *getter = property->getter;
256
+ *setter = property->setter;
257
+ } else {
258
+ *value = property->value;
259
+ }
260
+ return true;
261
+ }
262
+
263
+ bool oseo_internal_own_property_descriptor(
264
+ OseoContext *context,
265
+ OseoValue object_value,
266
+ OseoValue key,
267
+ OseoValue *value,
268
+ OseoPropertyAttributes *attributes,
269
+ OseoValue *getter,
270
+ OseoValue *setter
271
+ ) {
272
+ if (oseo_internal_legacy_descriptor(
273
+ context, object_value, key, value, attributes)) {
274
+ *getter = oseo_undefined();
275
+ *setter = oseo_undefined();
276
+ return true;
277
+ }
278
+ if (oseo_internal_own_descriptor(
279
+ object_value,
280
+ key,
281
+ value,
282
+ attributes,
283
+ getter,
284
+ setter
285
+ )) {
286
+ return true;
287
+ }
288
+ *value = oseo_undefined();
289
+ *getter = oseo_undefined();
290
+ *setter = oseo_undefined();
291
+ return oseo_internal_virtual_string_iterator_descriptor(
292
+ context,
293
+ object_value,
294
+ key,
295
+ attributes
296
+ );
297
+ }
298
+
299
+ OseoResult oseo_internal_namespace_export_ready(
300
+ OseoContext *context,
301
+ OseoValue object_value,
302
+ OseoValue stored
303
+ ) {
304
+ if (!is_object(object_value) ||
305
+ !ordinary_object(object_value)->module_namespace ||
306
+ !is_cell(stored)) {
307
+ return normal(oseo_undefined());
308
+ }
309
+ OseoResult read = oseo_cell_get(context, stored);
310
+ if (read.status != OSEO_STATUS_NORMAL) return read;
311
+ return normal(oseo_undefined());
312
+ }
313
+
314
+ static OseoResult define_data_property(
315
+ OseoContext *context,
316
+ OseoValue object_value,
317
+ OseoValue key,
318
+ OseoValue value,
319
+ OseoPropertyAttributes attributes,
320
+ bool has_value,
321
+ bool absent_writable,
322
+ const char **refusal
323
+ ) {
324
+ *refusal = NULL;
325
+ OseoResult valid = oseo_internal_require_property_key(context, key);
326
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
327
+ if (!is_object(object_value)) {
328
+ return type_error(
329
+ context,
330
+ "Object.defineProperty requires an object."
331
+ );
332
+ }
333
+ const OseoConvertedDescriptor typed_descriptor = {
334
+ !absent_writable,
335
+ attributes.enumerable,
336
+ !absent_writable,
337
+ attributes.configurable,
338
+ !absent_writable,
339
+ attributes.writable,
340
+ has_value,
341
+ false,
342
+ false,
343
+ };
344
+ bool typed_array_key = false;
345
+ OseoResult typed_array_result = define_typed_array_key(
346
+ context,
347
+ object_value,
348
+ key,
349
+ &typed_descriptor,
350
+ value,
351
+ &typed_array_key,
352
+ refusal
353
+ );
354
+ if (typed_array_result.status != OSEO_STATUS_NORMAL || typed_array_key) {
355
+ return typed_array_result;
356
+ }
357
+ OseoResult legacy = oseo_internal_legacy_materialize(
358
+ context, object_value, key);
359
+ if (legacy.status != OSEO_STATUS_NORMAL) return legacy;
360
+ if (function_has_prototype_property(object_value) &&
361
+ oseo_internal_string_is_ascii(key, "prototype")) {
362
+ if (attributes.configurable || attributes.enumerable) {
363
+ *refusal =
364
+ "Cannot redefine the prototype property.";
365
+ return normal(object_value);
366
+ }
367
+ OseoFunction *function = function_object(object_value);
368
+ if (!function->prototype_writable &&
369
+ (attributes.writable ||
370
+ !oseo_internal_same_value(function->prototype_object, value))) {
371
+ *refusal =
372
+ "Cannot redefine the prototype property.";
373
+ return normal(object_value);
374
+ }
375
+ function->prototype_object = value;
376
+ function->prototype_writable = attributes.writable;
377
+ return normal(object_value);
378
+ }
379
+ OseoOrdinaryObject *object = ordinary_object(object_value);
380
+ OseoPropertyAttributes virtual_attributes = {false, false, false, false};
381
+ bool replaces_virtual =
382
+ oseo_internal_virtual_string_iterator_descriptor(
383
+ context,
384
+ object_value,
385
+ key,
386
+ &virtual_attributes
387
+ );
388
+ if (replaces_virtual) {
389
+ /* During intrinsic bootstrap the virtual property models an
390
+ * undefined value. The successful String initializer replaces it
391
+ * with the concrete iterator before user code can observe it. */
392
+ if (!virtual_attributes.configurable &&
393
+ (attributes.configurable ||
394
+ attributes.enumerable != virtual_attributes.enumerable ||
395
+ (!virtual_attributes.writable && attributes.writable) ||
396
+ (!virtual_attributes.writable && has_value &&
397
+ !oseo_internal_same_value(value, oseo_undefined())))) {
398
+ *refusal =
399
+ "Cannot redefine a non-configurable property.";
400
+ return normal(object_value);
401
+ }
402
+ /* A non-configurable, non-writable bootstrap property cannot change:
403
+ * validation rejected every descriptor that would alter it, and a
404
+ * SameValue undefined leaves the placeholder representation intact. */
405
+ bool read_only_no_op = !virtual_attributes.configurable &&
406
+ !virtual_attributes.writable;
407
+ if (!has_value || read_only_no_op) {
408
+ object->virtual_string_iterator_configurable =
409
+ attributes.configurable;
410
+ object->virtual_string_iterator_enumerable =
411
+ attributes.enumerable;
412
+ object->virtual_string_iterator_writable = attributes.writable;
413
+ object->dictionary = true;
414
+ object->shape_id = context->next_shape_id;
415
+ context->next_shape_id += 1u;
416
+ return normal(object_value);
417
+ }
418
+ }
419
+ if (is_array(object_value) &&
420
+ oseo_internal_string_is_ascii(key, "length")) {
421
+ uint32_t requested = object->array_length;
422
+ if (has_value) {
423
+ OseoResult converted = oseo_internal_to_array_length(
424
+ context,
425
+ value,
426
+ &requested
427
+ );
428
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
429
+ }
430
+ if (absent_writable) {
431
+ /* A descriptor without a [[Writable]] field keeps the
432
+ * property's current one, and ToArrayLength above is the
433
+ * one step of a definition that runs user code, so that
434
+ * field is read here rather than by the caller. Array
435
+ * `length` is always non-configurable and non-enumerable,
436
+ * so no other field can have gone stale. */
437
+ attributes.writable = object->length_writable;
438
+ }
439
+ if (attributes.configurable || attributes.enumerable) {
440
+ *refusal =
441
+ "Cannot redefine the array length property.";
442
+ return normal(object_value);
443
+ }
444
+ if (!object->length_writable && attributes.writable) {
445
+ *refusal =
446
+ "Cannot redefine the array length property.";
447
+ return normal(object_value);
448
+ }
449
+ if (!has_value) {
450
+ object->length_writable = attributes.writable;
451
+ return normal(object_value);
452
+ }
453
+ /* ArraySetLength runs with the already converted length, so it
454
+ * reaches no user code and cannot report a conversion error.
455
+ * Its non-strict form leaves the array length above the
456
+ * requested one exactly when the specification returns false,
457
+ * which is the refusal this reports and the partial truncation
458
+ * it keeps. */
459
+ bool valid_length = false;
460
+ OseoResult changed = oseo_internal_set_array_length(
461
+ context,
462
+ object,
463
+ oseo_number(requested),
464
+ false,
465
+ true,
466
+ &valid_length
467
+ );
468
+ if (changed.status != OSEO_STATUS_NORMAL) return changed;
469
+ object = ordinary_object(object_value);
470
+ if (object->array_length != requested) {
471
+ const char *cause = object->length_writable
472
+ ? "Cannot truncate past a non-configurable element."
473
+ : "Cannot redefine the array length property.";
474
+ if (valid_length && !attributes.writable) {
475
+ object->length_writable = false;
476
+ }
477
+ *refusal = cause;
478
+ return normal(object_value);
479
+ }
480
+ object->length_writable = attributes.writable;
481
+ return normal(object_value);
482
+ }
483
+ uint32_t defined_index = 0u;
484
+ bool extends_array = is_array(object_value) &&
485
+ oseo_internal_array_index(key, &defined_index) &&
486
+ defined_index >= object->array_length;
487
+ if (extends_array && !object->length_writable) {
488
+ *refusal =
489
+ "Cannot extend an array with a read-only length.";
490
+ return normal(object_value);
491
+ }
492
+ size_t index = oseo_internal_own_property_index(object, key);
493
+ if (object->module_namespace) {
494
+ if (index == SIZE_MAX) {
495
+ *refusal =
496
+ "Cannot define a module namespace property.";
497
+ return normal(object_value);
498
+ }
499
+ OseoProperty *property = &object->properties[index];
500
+ OseoValue current_value = property->value;
501
+ if (oseo_internal_cell_backed_property(
502
+ object_value,
503
+ current_value
504
+ )) {
505
+ OseoResult current = oseo_cell_get(context, current_value);
506
+ if (current.status != OSEO_STATUS_NORMAL) return current;
507
+ current_value = current.value;
508
+ }
509
+ if (attributes.accessor || attributes.configurable ||
510
+ attributes.enumerable != property->attributes.enumerable ||
511
+ attributes.writable != property->attributes.writable ||
512
+ !oseo_internal_same_value(current_value, value)) {
513
+ *refusal =
514
+ "Cannot define a module namespace property.";
515
+ return normal(object_value);
516
+ }
517
+ return normal(object_value);
518
+ }
519
+ if (index != SIZE_MAX) {
520
+ OseoProperty *property = &object->properties[index];
521
+ /* ValidateAndApplyPropertyDescriptor compares the property's
522
+ * current value, which a cell-backed property keeps in its
523
+ * binding cell. Reading it allocates nothing on the normal path,
524
+ * so the property pointer stays valid. */
525
+ bool cell_backed = oseo_internal_cell_backed_property(
526
+ object_value, property->value);
527
+ OseoValue current_value = property->value;
528
+ if (cell_backed) {
529
+ OseoResult read = oseo_cell_get(context, property->value);
530
+ if (read.status != OSEO_STATUS_NORMAL) return read;
531
+ current_value = read.value;
532
+ }
533
+ if (!property->attributes.configurable &&
534
+ (property->attributes.accessor ||
535
+ attributes.configurable ||
536
+ attributes.enumerable != property->attributes.enumerable ||
537
+ (!property->attributes.writable && attributes.writable) ||
538
+ (!property->attributes.writable &&
539
+ !oseo_internal_same_value(current_value, value)))) {
540
+ *refusal =
541
+ "Cannot redefine a non-configurable property.";
542
+ return normal(object_value);
543
+ }
544
+ if (cell_backed) {
545
+ /* The binding stays the one storage location the property
546
+ * views, so the accepted redefinition writes through the
547
+ * cell. A global or namespace binding also records the new
548
+ * [[Writable]] on the cell, so a later binding assignment
549
+ * fails exactly where a property assignment would. */
550
+ OseoValue cell = property->value;
551
+ OseoResult written = oseo_cell_set(context, cell, value);
552
+ if (written.status != OSEO_STATUS_NORMAL) return written;
553
+ property->attributes = attributes;
554
+ if (object->mapped_arguments) {
555
+ /* CreateMappedArgumentsObject's [[DefineOwnProperty]]
556
+ * (10.4.4.2) severs the alias exactly when the accepted
557
+ * descriptor is an explicit non-writable data
558
+ * descriptor: this property stops forwarding to the
559
+ * cell and keeps the value just written as its own
560
+ * plain snapshot, while the parameter itself keeps its
561
+ * own cell and stays an ordinary mutable binding. */
562
+ if (!attributes.writable) property->value = value;
563
+ } else {
564
+ cell_object(cell)->writable = attributes.writable;
565
+ }
566
+ object->dictionary = true;
567
+ object->shape_id = context->next_shape_id;
568
+ context->next_shape_id += 1u;
569
+ return normal(object_value);
570
+ }
571
+ property->attributes = attributes;
572
+ property->value = value;
573
+ property->getter = oseo_undefined();
574
+ property->setter = oseo_undefined();
575
+ object->dictionary = true;
576
+ object->shape_id = context->next_shape_id;
577
+ context->next_shape_id += 1u;
578
+ if (extends_array) object->array_length = defined_index + 1u;
579
+ return normal(object_value);
580
+ }
581
+ if (!object->extensible && !replaces_virtual) {
582
+ *refusal =
583
+ "Cannot define a property on a non-extensible object.";
584
+ return normal(object_value);
585
+ }
586
+ /* A stored key is this realm's own representative of a registered
587
+ * symbol, so the property vector never holds a value another realm
588
+ * owns. The caller already roots `key`, and the representative is
589
+ * rooted by `registered_symbols`. */
590
+ OseoResult local_key = oseo_internal_local_symbol(context, key);
591
+ if (local_key.status != OSEO_STATUS_NORMAL) return local_key;
592
+ key = local_key.value;
593
+ OseoResult grown = oseo_internal_grow_properties(context, object_value);
594
+ if (grown.status != OSEO_STATUS_NORMAL) return grown;
595
+ object = ordinary_object(object_value);
596
+ if (replaces_virtual) object->virtual_string_iterator = false;
597
+ OseoProperty *property = append_property_slot(object, replaces_virtual);
598
+ property->attributes = attributes;
599
+ property->key = key;
600
+ property->value = value;
601
+ property->getter = oseo_undefined();
602
+ property->setter = oseo_undefined();
603
+ oseo_internal_note_property_inserted(
604
+ object, (size_t)(property - object->properties));
605
+ object->shape_id = context->next_shape_id;
606
+ context->next_shape_id += 1u;
607
+ if (extends_array) object->array_length = defined_index + 1u;
608
+ return normal(object_value);
609
+ }
610
+
611
+ OseoResult oseo_internal_append_own_property(
612
+ OseoContext *context,
613
+ OseoValue object_value,
614
+ OseoValue key,
615
+ OseoValue value,
616
+ OseoPropertyAttributes attributes
617
+ ) {
618
+ /* The growth step is a safepoint, so key and value must already be
619
+ * rooted by the caller, exactly as they must be for a definition
620
+ * that reaches the same step through define_data_property. A stored
621
+ * key is this realm's own representative of a registered symbol, so
622
+ * the property vector never holds a value another realm owns; the
623
+ * representative itself is rooted by `registered_symbols`. */
624
+ OseoResult local_key = oseo_internal_local_symbol(context, key);
625
+ if (local_key.status != OSEO_STATUS_NORMAL) return local_key;
626
+ key = local_key.value;
627
+ OseoResult grown = oseo_internal_grow_properties(context, object_value);
628
+ if (grown.status != OSEO_STATUS_NORMAL) return grown;
629
+ OseoOrdinaryObject *object = ordinary_object(object_value);
630
+ OseoProperty *property = &object->properties[object->property_count];
631
+ property->attributes = attributes;
632
+ property->key = key;
633
+ property->value = value;
634
+ property->getter = oseo_undefined();
635
+ property->setter = oseo_undefined();
636
+ object->property_count += 1u;
637
+ oseo_internal_note_property_inserted(object, object->property_count - 1u);
638
+ object->shape_id = context->next_shape_id;
639
+ context->next_shape_id += 1u;
640
+ return normal(object_value);
641
+ }
642
+
643
+ OseoResult oseo_object_define(
644
+ OseoContext *context,
645
+ OseoValue object_value,
646
+ OseoValue key,
647
+ OseoValue value,
648
+ OseoPropertyAttributes attributes
649
+ ) {
650
+ return oseo_internal_object_define_data(
651
+ context,
652
+ object_value,
653
+ key,
654
+ value,
655
+ attributes,
656
+ true
657
+ );
658
+ }
659
+
660
+ OseoResult oseo_internal_object_define_data(
661
+ OseoContext *context,
662
+ OseoValue object_value,
663
+ OseoValue key,
664
+ OseoValue value,
665
+ OseoPropertyAttributes attributes,
666
+ bool has_value
667
+ ) {
668
+ const char *refusal = NULL;
669
+ OseoResult result = oseo_internal_define_data_reported(
670
+ context,
671
+ object_value,
672
+ key,
673
+ value,
674
+ attributes,
675
+ has_value,
676
+ false,
677
+ &refusal
678
+ );
679
+ if (result.status != OSEO_STATUS_NORMAL || refusal == NULL) return result;
680
+ return type_error(context, refusal);
681
+ }
682
+
683
+ OseoResult oseo_internal_define_data_reported(
684
+ OseoContext *context,
685
+ OseoValue object_value,
686
+ OseoValue key,
687
+ OseoValue value,
688
+ OseoPropertyAttributes attributes,
689
+ bool has_value,
690
+ bool absent_writable,
691
+ const char **refusal
692
+ ) {
693
+ if (is_proxy(object_value)) {
694
+ OseoConvertedDescriptor descriptor = {
695
+ !absent_writable,
696
+ attributes.enumerable,
697
+ !absent_writable,
698
+ attributes.configurable,
699
+ !absent_writable,
700
+ attributes.writable,
701
+ has_value,
702
+ false,
703
+ false,
704
+ };
705
+ return oseo_internal_proxy_define_own_property(
706
+ context,
707
+ object_value,
708
+ key,
709
+ &descriptor,
710
+ value,
711
+ oseo_undefined(),
712
+ oseo_undefined(),
713
+ refusal
714
+ );
715
+ }
716
+ return define_data_property(
717
+ context,
718
+ object_value,
719
+ key,
720
+ value,
721
+ attributes,
722
+ has_value,
723
+ absent_writable,
724
+ refusal
725
+ );
726
+ }
727
+
728
+ static OseoResult define_accessor_property(
729
+ OseoContext *context,
730
+ OseoValue object_value,
731
+ OseoValue key,
732
+ OseoValue getter,
733
+ OseoValue setter,
734
+ bool has_getter,
735
+ bool has_setter,
736
+ OseoPropertyAttributes attributes,
737
+ const char **refusal
738
+ ) {
739
+ *refusal = NULL;
740
+ OseoResult valid = oseo_internal_require_property_key(context, key);
741
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
742
+ if (!is_object(object_value)) {
743
+ return type_error(
744
+ context,
745
+ "Cannot define an accessor property on a non-object."
746
+ );
747
+ }
748
+ const OseoConvertedDescriptor typed_descriptor = {
749
+ true,
750
+ attributes.enumerable,
751
+ true,
752
+ attributes.configurable,
753
+ false,
754
+ false,
755
+ false,
756
+ has_getter,
757
+ has_setter,
758
+ };
759
+ bool typed_array_key = false;
760
+ OseoResult typed_array_result = define_typed_array_key(
761
+ context,
762
+ object_value,
763
+ key,
764
+ &typed_descriptor,
765
+ oseo_undefined(),
766
+ &typed_array_key,
767
+ refusal
768
+ );
769
+ if (typed_array_result.status != OSEO_STATUS_NORMAL || typed_array_key) {
770
+ return typed_array_result;
771
+ }
772
+ if (ordinary_object(object_value)->module_namespace) {
773
+ *refusal =
774
+ "Cannot define a module namespace property.";
775
+ return normal(object_value);
776
+ }
777
+ if (function_has_prototype_property(object_value) &&
778
+ oseo_internal_string_is_ascii(key, "prototype")) {
779
+ *refusal =
780
+ "Cannot redefine the prototype property.";
781
+ return normal(object_value);
782
+ }
783
+ OseoResult legacy = oseo_internal_legacy_materialize(
784
+ context, object_value, key);
785
+ if (legacy.status != OSEO_STATUS_NORMAL) return legacy;
786
+ OseoOrdinaryObject *object = ordinary_object(object_value);
787
+ OseoPropertyAttributes virtual_attributes = {false, false, false, false};
788
+ bool replaces_virtual =
789
+ oseo_internal_virtual_string_iterator_descriptor(
790
+ context,
791
+ object_value,
792
+ key,
793
+ &virtual_attributes
794
+ );
795
+ if (replaces_virtual) {
796
+ if (!virtual_attributes.configurable) {
797
+ *refusal =
798
+ "Cannot redefine a non-configurable property.";
799
+ return normal(object_value);
800
+ }
801
+ }
802
+ if (is_array(object_value) &&
803
+ oseo_internal_string_is_ascii(key, "length")) {
804
+ *refusal =
805
+ "Cannot redefine the array length property.";
806
+ return normal(object_value);
807
+ }
808
+ uint32_t defined_index = 0u;
809
+ bool extends_array = is_array(object_value) &&
810
+ oseo_internal_array_index(key, &defined_index) &&
811
+ defined_index >= object->array_length;
812
+ if (extends_array && !object->length_writable) {
813
+ *refusal =
814
+ "Cannot extend an array with a read-only length.";
815
+ return normal(object_value);
816
+ }
817
+ size_t index = oseo_internal_own_property_index(object, key);
818
+ OseoValue new_getter = has_getter ? getter : oseo_undefined();
819
+ OseoValue new_setter = has_setter ? setter : oseo_undefined();
820
+ if (index != SIZE_MAX) {
821
+ OseoProperty *property = &object->properties[index];
822
+ OseoValue existing_getter = property->attributes.accessor
823
+ ? property->getter : oseo_undefined();
824
+ OseoValue existing_setter = property->attributes.accessor
825
+ ? property->setter : oseo_undefined();
826
+ if (!has_getter) new_getter = existing_getter;
827
+ if (!has_setter) new_setter = existing_setter;
828
+ if (!property->attributes.configurable &&
829
+ (!property->attributes.accessor ||
830
+ attributes.configurable ||
831
+ attributes.enumerable != property->attributes.enumerable ||
832
+ (has_getter &&
833
+ !oseo_internal_same_value(existing_getter, new_getter)) ||
834
+ (has_setter &&
835
+ !oseo_internal_same_value(existing_setter, new_setter)))) {
836
+ *refusal =
837
+ "Cannot redefine a non-configurable property.";
838
+ return normal(object_value);
839
+ }
840
+ property->attributes = attributes;
841
+ property->value = oseo_undefined();
842
+ property->getter = new_getter;
843
+ property->setter = new_setter;
844
+ object->dictionary = true;
845
+ object->shape_id = context->next_shape_id;
846
+ context->next_shape_id += 1u;
847
+ if (extends_array) object->array_length = defined_index + 1u;
848
+ return normal(object_value);
849
+ }
850
+ if (!object->extensible && !replaces_virtual) {
851
+ *refusal =
852
+ "Cannot define a property on a non-extensible object.";
853
+ return normal(object_value);
854
+ }
855
+ /* A stored key is this realm's own representative of a registered
856
+ * symbol, so the property vector never holds a value another realm
857
+ * owns. The caller already roots `key`, and the representative is
858
+ * rooted by `registered_symbols`. */
859
+ OseoResult local_key = oseo_internal_local_symbol(context, key);
860
+ if (local_key.status != OSEO_STATUS_NORMAL) return local_key;
861
+ key = local_key.value;
862
+ OseoResult grown = oseo_internal_grow_properties(context, object_value);
863
+ if (grown.status != OSEO_STATUS_NORMAL) return grown;
864
+ object = ordinary_object(object_value);
865
+ if (replaces_virtual) object->virtual_string_iterator = false;
866
+ OseoProperty *property = append_property_slot(object, replaces_virtual);
867
+ property->attributes = attributes;
868
+ property->key = key;
869
+ property->value = oseo_undefined();
870
+ property->getter = new_getter;
871
+ property->setter = new_setter;
872
+ oseo_internal_note_property_inserted(
873
+ object, (size_t)(property - object->properties));
874
+ object->shape_id = context->next_shape_id;
875
+ context->next_shape_id += 1u;
876
+ if (extends_array) object->array_length = defined_index + 1u;
877
+ return normal(object_value);
878
+ }
879
+
880
+ OseoResult oseo_object_define_accessor(
881
+ OseoContext *context,
882
+ OseoValue object_value,
883
+ OseoValue key,
884
+ OseoValue getter,
885
+ OseoValue setter,
886
+ bool has_getter,
887
+ bool has_setter,
888
+ OseoPropertyAttributes attributes
889
+ ) {
890
+ const char *refusal = NULL;
891
+ OseoResult result = oseo_internal_define_accessor_reported(
892
+ context,
893
+ object_value,
894
+ key,
895
+ getter,
896
+ setter,
897
+ has_getter,
898
+ has_setter,
899
+ attributes,
900
+ &refusal
901
+ );
902
+ if (result.status != OSEO_STATUS_NORMAL || refusal == NULL) return result;
903
+ return type_error(context, refusal);
904
+ }
905
+
906
+ OseoResult oseo_internal_define_accessor_reported(
907
+ OseoContext *context,
908
+ OseoValue object_value,
909
+ OseoValue key,
910
+ OseoValue getter,
911
+ OseoValue setter,
912
+ bool has_getter,
913
+ bool has_setter,
914
+ OseoPropertyAttributes attributes,
915
+ const char **refusal
916
+ ) {
917
+ if (is_proxy(object_value)) {
918
+ OseoConvertedDescriptor descriptor = {
919
+ true,
920
+ attributes.enumerable,
921
+ true,
922
+ attributes.configurable,
923
+ false,
924
+ false,
925
+ false,
926
+ has_getter,
927
+ has_setter,
928
+ };
929
+ return oseo_internal_proxy_define_own_property(
930
+ context,
931
+ object_value,
932
+ key,
933
+ &descriptor,
934
+ oseo_undefined(),
935
+ getter,
936
+ setter,
937
+ refusal
938
+ );
939
+ }
940
+ return define_accessor_property(
941
+ context,
942
+ object_value,
943
+ key,
944
+ getter,
945
+ setter,
946
+ has_getter,
947
+ has_setter,
948
+ attributes,
949
+ refusal
950
+ );
951
+ }
952
+
953
+ OseoResult oseo_object_delete(
954
+ OseoContext *context,
955
+ OseoValue object_value,
956
+ OseoValue key,
957
+ bool strict
958
+ ) {
959
+ OseoResult valid = oseo_internal_require_property_key(context, key);
960
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
961
+ if (!is_object(object_value)) {
962
+ if (is_nullish(object_value)) {
963
+ return type_error(
964
+ context,
965
+ "Cannot delete properties of a nullish value."
966
+ );
967
+ }
968
+ if (oseo_internal_string_own_property(object_value, key, NULL)) {
969
+ return strict
970
+ ? type_error(context, "Cannot delete a string index property.")
971
+ : normal(oseo_boolean(false));
972
+ }
973
+ return normal(oseo_boolean(true));
974
+ }
975
+ if (is_proxy(object_value)) {
976
+ return oseo_internal_proxy_delete(
977
+ context, object_value, key, strict);
978
+ }
979
+ if (is_typed_array(object_value)) {
980
+ /* A view's [[Delete]] refuses exactly the valid
981
+ * indices and succeeds for every other numeric key. */
982
+ bool numeric = false;
983
+ size_t typed_index = SIZE_MAX;
984
+ OseoResult classified = oseo_internal_typed_array_numeric_key(
985
+ context,
986
+ key,
987
+ &numeric,
988
+ &typed_index
989
+ );
990
+ if (classified.status != OSEO_STATUS_NORMAL) return classified;
991
+ if (numeric &&
992
+ oseo_internal_typed_array_has_index(object_value, typed_index)) {
993
+ return strict
994
+ ? type_error(context, "Cannot delete a TypedArray element.")
995
+ : normal(oseo_boolean(false));
996
+ }
997
+ if (numeric) return normal(oseo_boolean(true));
998
+ }
999
+ if (function_has_prototype_property(object_value) &&
1000
+ oseo_internal_string_is_ascii(key, "prototype")) {
1001
+ return strict
1002
+ ? type_error(context, "Cannot delete the prototype property.")
1003
+ : normal(oseo_boolean(false));
1004
+ }
1005
+ uint8_t legacy_live = oseo_internal_legacy_live_key(object_value, key);
1006
+ if (legacy_live != 0u) {
1007
+ /* A live `arguments` or `caller` is configurable (ADR 0034). */
1008
+ function_object(object_value)->legacy_live &=
1009
+ (uint8_t)~legacy_live;
1010
+ ordinary_object(object_value)->dictionary = true;
1011
+ ordinary_object(object_value)->shape_id = context->next_shape_id;
1012
+ context->next_shape_id += 1u;
1013
+ return normal(oseo_boolean(true));
1014
+ }
1015
+ if (is_array(object_value) &&
1016
+ oseo_internal_string_is_ascii(key, "length")) {
1017
+ return strict
1018
+ ? type_error(context, "Cannot delete the array length property.")
1019
+ : normal(oseo_boolean(false));
1020
+ }
1021
+ OseoOrdinaryObject *object = ordinary_object(object_value);
1022
+ bool string_iterator =
1023
+ object_value ==
1024
+ context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE] &&
1025
+ key == context->well_known_symbols[OSEO_WELL_KNOWN_ITERATOR];
1026
+ size_t index = oseo_internal_own_property_index(object, key);
1027
+ if (index == SIZE_MAX) {
1028
+ if (string_iterator && object->virtual_string_iterator) {
1029
+ if (!object->virtual_string_iterator_configurable) {
1030
+ return strict
1031
+ ? type_error(
1032
+ context,
1033
+ "Cannot delete a non-configurable property."
1034
+ )
1035
+ : normal(oseo_boolean(false));
1036
+ }
1037
+ object->virtual_string_iterator = false;
1038
+ object->dictionary = true;
1039
+ object->shape_id = context->next_shape_id;
1040
+ context->next_shape_id += 1u;
1041
+ }
1042
+ return normal(oseo_boolean(true));
1043
+ }
1044
+ if (!object->properties[index].attributes.configurable) {
1045
+ return strict
1046
+ ? type_error(context, "Cannot delete a non-configurable property.")
1047
+ : normal(oseo_boolean(false));
1048
+ }
1049
+ /* Removing one of the properties a function was created with moves
1050
+ * the synthetic `prototype` one place earlier in own-key order, and
1051
+ * removal alone can tell the original from a property later
1052
+ * recreated under the same name: only an original still sits inside
1053
+ * the leading run the position counts. */
1054
+ if (function_has_prototype_property(object_value)) {
1055
+ OseoFunction *function = function_object(object_value);
1056
+ size_t rank = 0u;
1057
+ for (size_t scan = 0u; scan < index; scan += 1u) {
1058
+ uint32_t ignored = 0u;
1059
+ OseoValue scanned = object->properties[scan].key;
1060
+ if (is_symbol(scanned) ||
1061
+ oseo_internal_array_index(scanned, &ignored)) continue;
1062
+ rank += 1u;
1063
+ }
1064
+ uint32_t ignored = 0u;
1065
+ if (!is_symbol(key) && !oseo_internal_array_index(key, &ignored) &&
1066
+ rank < function->prototype_key_position) {
1067
+ function->prototype_key_position -= 1u;
1068
+ }
1069
+ if (!is_symbol(key) && !oseo_internal_array_index(key, &ignored) &&
1070
+ rank < function->legacy_arguments_position) {
1071
+ function->legacy_arguments_position -= 1u;
1072
+ }
1073
+ }
1074
+ (void)oseo_internal_remove_property(object, index);
1075
+ if (string_iterator) object->virtual_string_iterator = false;
1076
+ object->dictionary = true;
1077
+ object->shape_id = context->next_shape_id;
1078
+ context->next_shape_id += 1u;
1079
+ return normal(oseo_boolean(true));
1080
+ }