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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,815 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ #include "runtime_internal.h"
29
+
30
+ /*
31
+ * The generic property access paths: [[Get]] and its `super` form,
32
+ * [[Set]] and its `super` form, and HasOwnProperty.
33
+ */
34
+
35
+ static OseoResult type_error(OseoContext *context, const char *message) {
36
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
37
+ }
38
+
39
+ /*
40
+ * The TypedArray [[Set]] prelude for a view reached on
41
+ * a set walk, whether the view is the receiver itself or a prototype of
42
+ * an ordinary receiver. `*answered` reports that the view settled the
43
+ * write: a canonical numeric key performs TypedArraySetElement when the
44
+ * view is the receiver, and an invalid index with a foreign receiver
45
+ * reports true without writing anywhere. `*element` reports a valid
46
+ * index with a foreign receiver, whose ordinary descriptor is always a
47
+ * writable data property, so the caller stops the walk and writes to the
48
+ * receiver. Any other key leaves both flags clear and the walk continues
49
+ * ordinarily.
50
+ */
51
+ static OseoResult typed_array_set_step(
52
+ OseoContext *context,
53
+ OseoValue view,
54
+ OseoValue key,
55
+ OseoValue value,
56
+ OseoValue receiver,
57
+ bool *answered,
58
+ bool *element
59
+ ) {
60
+ *answered = false;
61
+ *element = false;
62
+ bool numeric = false;
63
+ size_t typed_index = SIZE_MAX;
64
+ OseoResult classified = oseo_internal_typed_array_numeric_key(
65
+ context,
66
+ key,
67
+ &numeric,
68
+ &typed_index
69
+ );
70
+ if (classified.status != OSEO_STATUS_NORMAL || !numeric) {
71
+ return classified.status == OSEO_STATUS_NORMAL
72
+ ? normal(value)
73
+ : classified;
74
+ }
75
+ if (view == receiver) {
76
+ bool present = false;
77
+ OseoResult stored = oseo_internal_typed_array_set_index(
78
+ context,
79
+ view,
80
+ typed_index,
81
+ value,
82
+ &present
83
+ );
84
+ if (stored.status != OSEO_STATUS_NORMAL) return stored;
85
+ *answered = true;
86
+ return normal(value);
87
+ }
88
+ if (!oseo_internal_typed_array_has_index(view, typed_index)) {
89
+ *answered = true;
90
+ return normal(value);
91
+ }
92
+ *element = true;
93
+ return normal(value);
94
+ }
95
+
96
+ OseoResult oseo_internal_require_property_key(
97
+ OseoContext *context,
98
+ OseoValue key
99
+ ) {
100
+ if (!is_string(key) && !is_symbol(key)) {
101
+ return failure(
102
+ context,
103
+ "OSEO2001",
104
+ "Property key is not a string or symbol."
105
+ );
106
+ }
107
+ return normal(key);
108
+ }
109
+
110
+ /*
111
+ * The shared body of Get and its `super` form. `object_value` is where
112
+ * the lookup starts and `receiver` is what a getter receives as `this`;
113
+ * the two differ only for a `super` reference.
114
+ */
115
+
116
+ static OseoResult object_get(
117
+ OseoContext *context,
118
+ OseoValue object_value,
119
+ OseoValue key,
120
+ OseoValue receiver
121
+ ) {
122
+ OseoResult valid = oseo_internal_require_property_key(context, key);
123
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
124
+ if (!is_object(object_value)) {
125
+ if (is_nullish(object_value)) {
126
+ return type_error(
127
+ context,
128
+ "Cannot read properties of a nullish value."
129
+ );
130
+ }
131
+ if (is_string(object_value) &&
132
+ oseo_internal_string_is_ascii(key, "length")) {
133
+ return normal(oseo_number(string_object(object_value)->length));
134
+ }
135
+ uint32_t index = 0u;
136
+ if (oseo_internal_string_own_property(object_value, key, &index)) {
137
+ uint16_t unit = string_object(object_value)->units[index];
138
+ return oseo_internal_allocate_string(context, &unit, 1u);
139
+ }
140
+ OseoValue slots[3] = {object_value, key, receiver};
141
+ OseoRootFrame frame = {NULL, slots, 3u};
142
+ oseo_roots_push(context, &frame);
143
+ OseoResult boxed = oseo_internal_to_object_for_property(
144
+ context,
145
+ slots[0]
146
+ );
147
+ slots[0] = boxed.value;
148
+ if (boxed.status == OSEO_STATUS_NORMAL) {
149
+ boxed = object_get(context, slots[0], slots[1], slots[2]);
150
+ }
151
+ oseo_roots_pop(context, &frame);
152
+ return boxed;
153
+ }
154
+ OseoValue current = object_value;
155
+ while (is_object(current)) {
156
+ if (is_proxy(current)) {
157
+ return oseo_internal_proxy_get(
158
+ context, current, key, receiver);
159
+ }
160
+ if (is_typed_array(current)) {
161
+ /* A view's [[Get]] answers every canonical numeric
162
+ * key itself, so an invalid index never reaches the prototype. */
163
+ bool numeric = false;
164
+ bool present = false;
165
+ OseoResult element = oseo_internal_typed_array_own_property(
166
+ context,
167
+ current,
168
+ key,
169
+ &numeric,
170
+ &present
171
+ );
172
+ if (element.status != OSEO_STATUS_NORMAL || numeric) {
173
+ return element;
174
+ }
175
+ }
176
+ OseoOrdinaryObject *object = ordinary_object(current);
177
+ OseoValue value = oseo_undefined();
178
+ OseoPropertyAttributes attributes = {false, false, false, false};
179
+ OseoValue getter = oseo_undefined();
180
+ OseoValue setter = oseo_undefined();
181
+ if (oseo_internal_own_property_descriptor(
182
+ context, current, key, &value, &attributes,
183
+ &getter, &setter)) {
184
+ if (attributes.accessor) {
185
+ if (!is_callable(getter)) return normal(oseo_undefined());
186
+ OseoRootFrame frame = {NULL, NULL, 0u};
187
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
188
+ if (result.status != OSEO_STATUS_NORMAL) return result;
189
+ frame.slots[0] = getter;
190
+ result = oseo_call_function(
191
+ context,
192
+ frame.slots[0],
193
+ receiver,
194
+ 0u,
195
+ NULL,
196
+ oseo_undefined()
197
+ );
198
+ oseo_roots_release(context, &frame);
199
+ return result;
200
+ }
201
+ if (oseo_internal_cell_backed_property(current, value)) {
202
+ return oseo_cell_get(context, value);
203
+ }
204
+ return oseo_internal_legacy_value(context, current, key, value);
205
+ }
206
+ current = object->prototype;
207
+ }
208
+ return normal(oseo_undefined());
209
+ }
210
+
211
+ OseoResult oseo_object_get(
212
+ OseoContext *context,
213
+ OseoValue object_value,
214
+ OseoValue key
215
+ ) {
216
+ return object_get(context, object_value, key, object_value);
217
+ }
218
+
219
+ OseoResult oseo_super_get(
220
+ OseoContext *context,
221
+ OseoValue base,
222
+ OseoValue key,
223
+ OseoValue receiver
224
+ ) {
225
+ return object_get(context, base, key, receiver);
226
+ }
227
+
228
+ OseoResult oseo_object_has_own(
229
+ OseoContext *context,
230
+ OseoValue object_value,
231
+ OseoValue key
232
+ ) {
233
+ OseoResult valid = oseo_internal_require_property_key(context, key);
234
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
235
+ if (!is_object(object_value)) {
236
+ if (is_nullish(object_value)) {
237
+ return type_error(
238
+ context,
239
+ "Cannot convert a nullish value to an object."
240
+ );
241
+ }
242
+ return normal(oseo_boolean(oseo_internal_string_own_property(
243
+ object_value,
244
+ key,
245
+ NULL
246
+ )));
247
+ }
248
+ if (is_typed_array(object_value)) {
249
+ bool numeric = false;
250
+ size_t index = SIZE_MAX;
251
+ OseoResult classified = oseo_internal_typed_array_numeric_key(
252
+ context,
253
+ key,
254
+ &numeric,
255
+ &index
256
+ );
257
+ if (classified.status != OSEO_STATUS_NORMAL) return classified;
258
+ if (numeric) {
259
+ return normal(oseo_boolean(
260
+ oseo_internal_typed_array_has_index(object_value, index)
261
+ ));
262
+ }
263
+ }
264
+ /* The shared descriptor primitive already reports a function's
265
+ * `prototype`, an array's `length`, and the virtual String iterator,
266
+ * so the ownership answer never needs a separate synthetic case. */
267
+ OseoValue value = oseo_undefined();
268
+ OseoPropertyAttributes attributes = {false, false, false, false};
269
+ OseoValue getter = oseo_undefined();
270
+ OseoValue setter = oseo_undefined();
271
+ if (is_proxy(object_value)) {
272
+ bool found = false;
273
+ OseoResult result = oseo_internal_proxy_get_own_property(
274
+ context,
275
+ object_value,
276
+ key,
277
+ &found,
278
+ &value,
279
+ &attributes,
280
+ &getter,
281
+ &setter
282
+ );
283
+ if (result.status != OSEO_STATUS_NORMAL) return result;
284
+ return normal(oseo_boolean(found));
285
+ }
286
+ bool exists = oseo_internal_own_property_descriptor(
287
+ context,
288
+ object_value,
289
+ key,
290
+ &value,
291
+ &attributes,
292
+ &getter,
293
+ &setter
294
+ );
295
+ if (!exists) return normal(oseo_boolean(false));
296
+ OseoResult ready = oseo_internal_namespace_export_ready(
297
+ context, object_value, value);
298
+ if (ready.status != OSEO_STATUS_NORMAL) return ready;
299
+ return normal(oseo_boolean(true));
300
+ }
301
+
302
+ OseoResult oseo_object_set(
303
+ OseoContext *context,
304
+ OseoValue object_value,
305
+ OseoValue key,
306
+ OseoValue value,
307
+ bool strict
308
+ ) {
309
+ if (object_value == context->realm->global_lexicals &&
310
+ is_object(object_value)) {
311
+ size_t index = oseo_internal_own_property_index(
312
+ ordinary_object(object_value), key);
313
+ if (index != SIZE_MAX) {
314
+ OseoProperty *property =
315
+ &ordinary_object(object_value)->properties[index];
316
+ OseoResult read = oseo_cell_get(context, property->value);
317
+ if (read.status != OSEO_STATUS_NORMAL) return read;
318
+ if (!property->attributes.writable) {
319
+ return type_error(context, "Assignment to a constant binding.");
320
+ }
321
+ return oseo_cell_set(context, property->value, value);
322
+ }
323
+ }
324
+ OseoResult valid = oseo_internal_require_property_key(context, key);
325
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
326
+ if (!is_object(object_value)) {
327
+ if (is_nullish(object_value)) {
328
+ return type_error(
329
+ context,
330
+ "Cannot set properties of a nullish or primitive value."
331
+ );
332
+ }
333
+ /* PutValue uses ToObject only for lookup. Keep GetThisValue's
334
+ * primitive receiver, including across Proxy dispatch and a
335
+ * setter's call into its own realm. Full String wrappers retain
336
+ * the non-writable own index and length descriptors. */
337
+ OseoValue slots[4] = {object_value, key, value, oseo_undefined()};
338
+ OseoRootFrame frame = {NULL, slots, 4u};
339
+ oseo_roots_push(context, &frame);
340
+ OseoResult result = oseo_internal_to_object(context, slots[0]);
341
+ slots[3] = result.value;
342
+ const char *refusal = NULL;
343
+ if (result.status == OSEO_STATUS_NORMAL) {
344
+ result = oseo_internal_set_with_receiver(
345
+ context, slots[3], slots[1], slots[2], slots[0], &refusal
346
+ );
347
+ }
348
+ if (result.status == OSEO_STATUS_NORMAL) {
349
+ result = strict && refusal != NULL
350
+ ? type_error(context, refusal)
351
+ : normal(slots[2]);
352
+ }
353
+ oseo_roots_pop(context, &frame);
354
+ return result;
355
+ }
356
+ if (is_proxy(object_value)) {
357
+ const char *refusal = NULL;
358
+ OseoResult result = oseo_internal_proxy_set(
359
+ context, object_value, key, value, object_value, &refusal);
360
+ if (result.status != OSEO_STATUS_NORMAL || refusal == NULL) {
361
+ return result;
362
+ }
363
+ return strict ? type_error(context, refusal) : normal(value);
364
+ }
365
+ if (ordinary_object(object_value)->module_namespace) {
366
+ if (strict) {
367
+ return type_error(
368
+ context,
369
+ "Cannot assign to a module namespace property."
370
+ );
371
+ }
372
+ return normal(value);
373
+ }
374
+ if (function_has_prototype_property(object_value) &&
375
+ oseo_internal_string_is_ascii(key, "prototype")) {
376
+ OseoFunction *function = function_object(object_value);
377
+ if (!function->prototype_writable) {
378
+ if (strict) {
379
+ return type_error(
380
+ context,
381
+ "Cannot assign to the read-only prototype property."
382
+ );
383
+ }
384
+ return normal(value);
385
+ }
386
+ function->prototype_object = value;
387
+ return normal(value);
388
+ }
389
+ OseoOrdinaryObject *receiver = ordinary_object(object_value);
390
+ if (is_array(object_value) &&
391
+ oseo_internal_string_is_ascii(key, "length")) {
392
+ return oseo_internal_set_array_length(
393
+ context,
394
+ receiver,
395
+ value,
396
+ strict,
397
+ false,
398
+ NULL
399
+ );
400
+ }
401
+ uint32_t receiver_index = 0u;
402
+ bool extends_array = is_array(object_value) &&
403
+ oseo_internal_array_index(key, &receiver_index) &&
404
+ receiver_index >= receiver->array_length;
405
+ OseoValue current = object_value;
406
+ while (is_object(current)) {
407
+ if (is_proxy(current)) {
408
+ const char *refusal = NULL;
409
+ OseoResult result = oseo_internal_proxy_set(
410
+ context,
411
+ current,
412
+ key,
413
+ value,
414
+ object_value,
415
+ &refusal
416
+ );
417
+ if (result.status != OSEO_STATUS_NORMAL || refusal == NULL) {
418
+ return result;
419
+ }
420
+ return strict ? type_error(context, refusal) : normal(value);
421
+ }
422
+ OseoOrdinaryObject *owner = ordinary_object(current);
423
+ if (owner->module_namespace) {
424
+ /* The receiver itself was answered above; a namespace
425
+ * reached later on the walk owns the [[Set]] the absent own
426
+ * property hands the parent, and it refuses every key. */
427
+ if (strict) {
428
+ return type_error(
429
+ context,
430
+ "Cannot assign to a module namespace property."
431
+ );
432
+ }
433
+ return normal(value);
434
+ }
435
+ if (is_typed_array(current)) {
436
+ /* A view anywhere on the walk owns the [[Set]] for a
437
+ * canonical numeric key, so an ordinary receiver inheriting
438
+ * from a TypedArray never materializes an invalid index as
439
+ * its own property. */
440
+ bool answered = false;
441
+ bool element = false;
442
+ OseoResult stepped = typed_array_set_step(
443
+ context,
444
+ current,
445
+ key,
446
+ value,
447
+ object_value,
448
+ &answered,
449
+ &element
450
+ );
451
+ if (stepped.status != OSEO_STATUS_NORMAL || answered) {
452
+ return stepped;
453
+ }
454
+ if (element) break;
455
+ }
456
+ OseoValue own_value = oseo_undefined();
457
+ OseoPropertyAttributes attributes = {false, false, false, false};
458
+ OseoValue getter = oseo_undefined();
459
+ OseoValue setter = oseo_undefined();
460
+ if (oseo_internal_own_property_descriptor(
461
+ context, current, key, &own_value, &attributes, &getter,
462
+ &setter)) {
463
+ if (attributes.accessor) {
464
+ if (!is_callable(setter)) {
465
+ if (strict) {
466
+ return type_error(
467
+ context,
468
+ "Cannot set a property that has only a getter."
469
+ );
470
+ }
471
+ return normal(value);
472
+ }
473
+ OseoRootFrame frame = {NULL, NULL, 0u};
474
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
475
+ if (result.status != OSEO_STATUS_NORMAL) return result;
476
+ frame.slots[0] = setter;
477
+ frame.slots[1] = value;
478
+ result = oseo_call_function(
479
+ context,
480
+ frame.slots[0],
481
+ object_value,
482
+ 1u,
483
+ &frame.slots[1],
484
+ oseo_undefined()
485
+ );
486
+ oseo_roots_release(context, &frame);
487
+ if (result.status != OSEO_STATUS_NORMAL) return result;
488
+ return normal(value);
489
+ }
490
+ if (!attributes.writable) {
491
+ if (strict) {
492
+ return type_error(
493
+ context,
494
+ "Cannot assign to a read-only property."
495
+ );
496
+ }
497
+ return normal(value);
498
+ }
499
+ size_t index = oseo_internal_own_property_index(owner, key);
500
+ if (index == SIZE_MAX) {
501
+ /* An own property outside the property vector is either a
502
+ * synthetic descriptor a dedicated branch above already
503
+ * answered or the virtual String iterator, which an
504
+ * assignment to its own object replaces in place. */
505
+ OseoPropertyAttributes virtual_attributes = attributes;
506
+ if (current == object_value &&
507
+ oseo_internal_virtual_string_iterator_descriptor(
508
+ context,
509
+ current,
510
+ key,
511
+ &virtual_attributes
512
+ )) {
513
+ OseoResult assigned = oseo_object_define(
514
+ context,
515
+ object_value,
516
+ key,
517
+ value,
518
+ virtual_attributes
519
+ );
520
+ if (assigned.status != OSEO_STATUS_NORMAL) {
521
+ return assigned;
522
+ }
523
+ return normal(value);
524
+ }
525
+ break;
526
+ }
527
+ OseoProperty *property = &owner->properties[index];
528
+ if (current == object_value) {
529
+ /* A cell-backed property is a view of a binding, so the
530
+ * assignment updates the binding rather than replacing
531
+ * the view with a plain value. */
532
+ if (oseo_internal_cell_backed_property(
533
+ current, property->value)) {
534
+ return oseo_cell_set(context, property->value, value);
535
+ }
536
+ property->value = value;
537
+ if (extends_array) receiver->array_length = receiver_index + 1u;
538
+ return normal(value);
539
+ }
540
+ break;
541
+ }
542
+ current = owner->prototype;
543
+ }
544
+ if (!receiver->extensible) {
545
+ if (strict) {
546
+ return type_error(
547
+ context,
548
+ "Cannot add a property to a non-extensible object."
549
+ );
550
+ }
551
+ return normal(value);
552
+ }
553
+ if (extends_array && !receiver->length_writable) {
554
+ if (strict) {
555
+ return type_error(
556
+ context,
557
+ "Cannot extend an array with a read-only length."
558
+ );
559
+ }
560
+ return normal(value);
561
+ }
562
+ /* A stored key is this realm's own representative of a registered
563
+ * symbol, so the property vector never holds a value another realm
564
+ * owns. The caller already roots `key`, and the representative is
565
+ * rooted by `registered_symbols`. */
566
+ OseoResult local_key = oseo_internal_local_symbol(context, key);
567
+ if (local_key.status != OSEO_STATUS_NORMAL) return local_key;
568
+ key = local_key.value;
569
+ OseoResult grown = oseo_internal_grow_properties(context, object_value);
570
+ if (grown.status != OSEO_STATUS_NORMAL) return grown;
571
+ OseoOrdinaryObject *object = ordinary_object(object_value);
572
+ OseoProperty *property = &object->properties[object->property_count];
573
+ property->attributes = (OseoPropertyAttributes){true, true, true, false};
574
+ property->key = key;
575
+ property->value = value;
576
+ property->getter = oseo_undefined();
577
+ property->setter = oseo_undefined();
578
+ object->property_count += 1u;
579
+ oseo_internal_note_property_inserted(object, object->property_count - 1u);
580
+ object->shape_id = context->next_shape_id;
581
+ context->next_shape_id += 1u;
582
+ if (extends_array) object->array_length = receiver_index + 1u;
583
+ return normal(value);
584
+ }
585
+
586
+ OseoResult oseo_internal_set_with_receiver(
587
+ OseoContext *context,
588
+ OseoValue base,
589
+ OseoValue key,
590
+ OseoValue value,
591
+ OseoValue receiver,
592
+ const char **refusal
593
+ ) {
594
+ *refusal = NULL;
595
+ OseoResult valid = oseo_internal_require_property_key(context, key);
596
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
597
+ /* The lookup walks `base` only to decide whether a setter runs.
598
+ * Every other outcome, including a data property found on the walk,
599
+ * leaves the write to the receiver. */
600
+ OseoValue current = base;
601
+ while (is_object(current)) {
602
+ if (is_proxy(current)) {
603
+ return oseo_internal_proxy_set(
604
+ context, current, key, value, receiver, refusal);
605
+ }
606
+ if (ordinary_object(current)->module_namespace) {
607
+ /* OrdinarySetWithOwnDescriptor hands an absent own property
608
+ * to the parent's own [[Set]], so a module namespace
609
+ * anywhere on the walk answers with its exotic [[Set]]
610
+ * (10.4.6.9) rather than with this ordinary continuation.
611
+ * That reports false for every key and every receiver, so
612
+ * no write reaches either object. */
613
+ *refusal = "Cannot assign to a module namespace property.";
614
+ return normal(value);
615
+ }
616
+ if (is_typed_array(current)) {
617
+ bool answered = false;
618
+ bool element = false;
619
+ OseoResult stepped = typed_array_set_step(
620
+ context,
621
+ current,
622
+ key,
623
+ value,
624
+ receiver,
625
+ &answered,
626
+ &element
627
+ );
628
+ if (stepped.status != OSEO_STATUS_NORMAL || answered) {
629
+ return stepped;
630
+ }
631
+ if (element) break;
632
+ }
633
+ OseoValue own_value = oseo_undefined();
634
+ OseoPropertyAttributes attributes = {false, false, false, false};
635
+ OseoValue getter = oseo_undefined();
636
+ OseoValue setter = oseo_undefined();
637
+ if (oseo_internal_own_property_descriptor(
638
+ context, current, key, &own_value, &attributes, &getter,
639
+ &setter)) {
640
+ if (attributes.accessor) {
641
+ if (!is_callable(setter)) {
642
+ *refusal = "Cannot set a property that has only a getter.";
643
+ return normal(value);
644
+ }
645
+ OseoRootFrame frame = {NULL, NULL, 0u};
646
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
647
+ if (result.status != OSEO_STATUS_NORMAL) return result;
648
+ frame.slots[0] = setter;
649
+ frame.slots[1] = value;
650
+ result = oseo_call_function(
651
+ context,
652
+ frame.slots[0],
653
+ receiver,
654
+ 1u,
655
+ &frame.slots[1],
656
+ oseo_undefined()
657
+ );
658
+ oseo_roots_release(context, &frame);
659
+ if (result.status != OSEO_STATUS_NORMAL) return result;
660
+ return normal(value);
661
+ }
662
+ if (!attributes.writable) {
663
+ *refusal = "Cannot assign to a read-only property.";
664
+ return normal(value);
665
+ }
666
+ break;
667
+ }
668
+ current = ordinary_object(current)->prototype;
669
+ }
670
+ if (!is_object(receiver)) {
671
+ *refusal = "Cannot set properties of a nullish or primitive value.";
672
+ return normal(value);
673
+ }
674
+ OseoValue receiver_value = oseo_undefined();
675
+ OseoPropertyAttributes receiver_attributes = {false, false, false, false};
676
+ OseoValue receiver_getter = oseo_undefined();
677
+ OseoValue receiver_setter = oseo_undefined();
678
+ bool receiver_found = false;
679
+ OseoResult receiver_result = is_proxy(receiver)
680
+ ? oseo_internal_proxy_get_own_property(
681
+ context,
682
+ receiver,
683
+ key,
684
+ &receiver_found,
685
+ &receiver_value,
686
+ &receiver_attributes,
687
+ &receiver_getter,
688
+ &receiver_setter
689
+ )
690
+ : normal(oseo_boolean(
691
+ (receiver_found = oseo_internal_own_property_descriptor(
692
+ context,
693
+ receiver,
694
+ key,
695
+ &receiver_value,
696
+ &receiver_attributes,
697
+ &receiver_getter,
698
+ &receiver_setter
699
+ ))
700
+ ));
701
+ if (receiver_result.status == OSEO_STATUS_NORMAL && receiver_found) {
702
+ receiver_result = oseo_internal_namespace_export_ready(
703
+ context, receiver, receiver_value);
704
+ }
705
+ if (receiver_result.status != OSEO_STATUS_NORMAL) return receiver_result;
706
+ if (receiver_found) {
707
+ if (receiver_attributes.accessor) {
708
+ *refusal = "Cannot assign to an accessor property of the receiver.";
709
+ return normal(value);
710
+ }
711
+ if (!receiver_attributes.writable) {
712
+ *refusal = "Cannot assign to a read-only property.";
713
+ return normal(value);
714
+ }
715
+ /* OrdinarySetWithOwnDescriptor finishes with
716
+ * Receiver.[[DefineOwnProperty]](P, { [[Value]]: V }), which
717
+ * keeps every other attribute the property already has and
718
+ * routes an array `length` write through ArraySetLength. That
719
+ * definition can still report a refusal, because coercing the
720
+ * written value runs user code that may make the property
721
+ * non-writable. */
722
+ OseoPropertyAttributes attributes = receiver_attributes;
723
+ attributes.accessor = false;
724
+ OseoResult assigned = oseo_internal_define_data_reported(
725
+ context,
726
+ receiver,
727
+ key,
728
+ value,
729
+ attributes,
730
+ true,
731
+ true,
732
+ refusal
733
+ );
734
+ if (assigned.status != OSEO_STATUS_NORMAL) return assigned;
735
+ return normal(value);
736
+ }
737
+ /* CreateDataProperty on the receiver, which reports false rather
738
+ * than throwing when the receiver refuses a new own property. */
739
+ OseoResult created = oseo_internal_define_data_reported(
740
+ context,
741
+ receiver,
742
+ key,
743
+ value,
744
+ (OseoPropertyAttributes){true, true, true, false},
745
+ true,
746
+ false,
747
+ refusal
748
+ );
749
+ if (created.status != OSEO_STATUS_NORMAL) return created;
750
+ return normal(value);
751
+ }
752
+
753
+ OseoResult oseo_super_set(
754
+ OseoContext *context,
755
+ OseoValue base,
756
+ OseoValue key,
757
+ OseoValue value,
758
+ OseoValue receiver,
759
+ bool strict
760
+ ) {
761
+ OseoResult valid = oseo_internal_require_property_key(context, key);
762
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
763
+ if (!is_object(base)) {
764
+ return type_error(
765
+ context,
766
+ "Cannot set properties of a nullish or primitive value."
767
+ );
768
+ }
769
+ const char *refusal = NULL;
770
+ OseoResult result = oseo_internal_set_with_receiver(
771
+ context,
772
+ base,
773
+ key,
774
+ value,
775
+ receiver,
776
+ &refusal
777
+ );
778
+ if (result.status != OSEO_STATUS_NORMAL || refusal == NULL) return result;
779
+ return strict ? type_error(context, refusal) : normal(value);
780
+ }
781
+
782
+ /* Inputs remain in the generated frame until the operation completes. */
783
+ void oseo_generated_property_key(
784
+ OseoContext *context, OseoValue input,
785
+ OseoResult *result, OseoValue *output
786
+ ) {
787
+ *result = oseo_property_key(context, input);
788
+ *output = result->value;
789
+ }
790
+
791
+ void oseo_generated_property_get(
792
+ OseoContext *context, OseoValue object, OseoValue key,
793
+ OseoResult *result, OseoValue *output
794
+ ) {
795
+ *result = oseo_object_get(context, object, key);
796
+ *output = result->value;
797
+ }
798
+
799
+ void oseo_generated_property_key_at(
800
+ OseoContext *context, OseoValue *roots,
801
+ const OseoGeneratedOperation *operation, OseoResult *result
802
+ ) {
803
+ oseo_generated_operation_location(context, operation);
804
+ oseo_generated_property_key(context, roots[operation->first],
805
+ result, &roots[operation->output]);
806
+ }
807
+
808
+ void oseo_generated_property_get_at(
809
+ OseoContext *context, OseoValue *roots,
810
+ const OseoGeneratedOperation *operation, OseoResult *result
811
+ ) {
812
+ oseo_generated_operation_location(context, operation);
813
+ oseo_generated_property_get(context, roots[operation->first],
814
+ roots[operation->second], result, &roots[operation->output]);
815
+ }