@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,1533 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <math.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ /* Proxy exotic objects and their thirteen essential internal methods. */
11
+
12
+ static OseoResult type_error(OseoContext *context, const char *message) {
13
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
14
+ }
15
+
16
+ static OseoValue argument(
17
+ size_t argument_count,
18
+ const OseoValue *arguments,
19
+ size_t index
20
+ ) {
21
+ return index < argument_count ? arguments[index] : oseo_undefined();
22
+ }
23
+
24
+ static OseoResult revoked(OseoContext *context, OseoValue proxy) {
25
+ if (!proxy_object(proxy)->revoked) {
26
+ return normal(proxy);
27
+ }
28
+ return type_error(
29
+ context, "Cannot perform an operation on a revoked Proxy.");
30
+ }
31
+
32
+ static OseoResult proxy_trap(
33
+ OseoContext *context,
34
+ OseoValue proxy,
35
+ const char *name
36
+ ) {
37
+ OseoResult live = revoked(context, proxy);
38
+ if (live.status != OSEO_STATUS_NORMAL) return live;
39
+ OseoValue slots[2] = {proxy, oseo_undefined()};
40
+ OseoRootFrame frame = {NULL, slots, 2u};
41
+ oseo_roots_push(context, &frame);
42
+ OseoResult result = oseo_internal_ascii_string(context, name);
43
+ slots[1] = result.value;
44
+ if (result.status == OSEO_STATUS_NORMAL) {
45
+ result = oseo_object_get(
46
+ context,
47
+ proxy_object(slots[0])->handler,
48
+ slots[1]
49
+ );
50
+ }
51
+ if (result.status == OSEO_STATUS_NORMAL &&
52
+ !is_nullish(result.value) && !is_callable(result.value)) {
53
+ result = type_error(context, "A Proxy trap must be callable.");
54
+ }
55
+ oseo_roots_pop(context, &frame);
56
+ return result;
57
+ }
58
+
59
+ static OseoResult proxy_complete(
60
+ OseoContext *context,
61
+ OseoRootFrame *frame,
62
+ OseoResult result
63
+ ) {
64
+ oseo_roots_pop(context, frame);
65
+ oseo_call_leave(context);
66
+ return result;
67
+ }
68
+
69
+ static void initialize_ordinary(
70
+ OseoContext *context,
71
+ OseoOrdinaryObject *object
72
+ ) {
73
+ object->prototype = oseo_null();
74
+ object->properties = NULL;
75
+ object->property_capacity = 0u;
76
+ object->property_count = 0u;
77
+ oseo_internal_init_dense_index(object);
78
+ object->private_elements = NULL;
79
+ object->private_element_capacity = 0u;
80
+ object->private_element_count = 0u;
81
+ object->shape_id = context->next_shape_id++;
82
+ object->array_length = 0u;
83
+ object->dictionary = true;
84
+ object->length_writable = false;
85
+ object->extensible = true;
86
+ object->module_namespace = false;
87
+ object->immutable_prototype = false;
88
+ object->global_object = false;
89
+ object->error_data = false;
90
+ object->number_data = false;
91
+ object->number_value = oseo_undefined();
92
+ object->primitive_data = false;
93
+ object->primitive_value = oseo_undefined();
94
+ object->primitive_wrapper_methods_initialized = false;
95
+ object->virtual_string_iterator = false;
96
+ object->virtual_string_iterator_configurable = false;
97
+ object->virtual_string_iterator_enumerable = false;
98
+ object->virtual_string_iterator_writable = false;
99
+ object->iterator_kind = OSEO_ITERATOR_NONE;
100
+ object->iterator_target = oseo_undefined();
101
+ object->iterator_index = 0u;
102
+ object->regexp_string_iterator = false;
103
+ object->regexp_iterator_regexp = oseo_undefined();
104
+ object->regexp_iterator_subject = oseo_undefined();
105
+ object->regexp_iterator_global = false;
106
+ object->regexp_iterator_unicode = false;
107
+ object->regexp_iterator_complete = false;
108
+ object->async_from_sync = false;
109
+ object->async_sync_iterator = oseo_undefined();
110
+ object->wrap_for_valid_iterator = false;
111
+ object->wrapped_iterator = oseo_undefined();
112
+ object->wrapped_next = oseo_undefined();
113
+ object->arguments_object = false;
114
+ object->mapped_arguments = false;
115
+ object->generator = NULL;
116
+ }
117
+
118
+ static OseoResult proxy_create(
119
+ OseoContext *context,
120
+ OseoValue target,
121
+ OseoValue handler
122
+ ) {
123
+ if (!is_object(target) || !is_object(handler)) {
124
+ return type_error(context, "Proxy requires object target and handler.");
125
+ }
126
+ OseoProxy *proxy =
127
+ oseo_internal_allocate_heap_bytes(context, sizeof(*proxy));
128
+ if (proxy == NULL) {
129
+ return failure(context, "OSEO2001", "Proxy allocation failed.");
130
+ }
131
+ initialize_ordinary(context, &proxy->ordinary);
132
+ proxy->target = target;
133
+ proxy->handler = handler;
134
+ proxy->callable = is_callable(target);
135
+ proxy->constructible = function_is_constructible(target);
136
+ proxy->revoked = false;
137
+ return oseo_internal_publish_heap(
138
+ context, &proxy->ordinary.header, OSEO_HEAP_PROXY);
139
+ }
140
+
141
+ static OseoResult call_trap(
142
+ OseoContext *context,
143
+ OseoValue proxy,
144
+ OseoValue trap,
145
+ size_t argument_count,
146
+ const OseoValue *arguments
147
+ ) {
148
+ return oseo_call_function(
149
+ context,
150
+ trap,
151
+ proxy_object(proxy)->handler,
152
+ argument_count,
153
+ arguments,
154
+ oseo_undefined()
155
+ );
156
+ }
157
+
158
+ static OseoResult target_own_property(
159
+ OseoContext *context,
160
+ OseoValue target,
161
+ OseoValue key,
162
+ bool *found,
163
+ OseoValue *value,
164
+ OseoPropertyAttributes *attributes,
165
+ OseoValue *getter,
166
+ OseoValue *setter
167
+ ) {
168
+ if (is_proxy(target)) {
169
+ return oseo_internal_proxy_get_own_property(
170
+ context, target, key, found, value, attributes,
171
+ getter, setter);
172
+ }
173
+ if (is_typed_array(target)) {
174
+ bool numeric = false;
175
+ bool present = false;
176
+ OseoResult loaded = oseo_internal_typed_array_own_property(
177
+ context, target, key, &numeric, &present);
178
+ if (loaded.status != OSEO_STATUS_NORMAL) return loaded;
179
+ if (numeric) {
180
+ *found = present;
181
+ *value = loaded.value;
182
+ *getter = oseo_undefined();
183
+ *setter = oseo_undefined();
184
+ /* Always configurable and writable, so no trap-invariant
185
+ * check ever compares this value after a later safepoint
186
+ * could have collected a freshly loaded BigInt element. */
187
+ *attributes = (OseoPropertyAttributes){true, true, true, false};
188
+ return normal(oseo_undefined());
189
+ }
190
+ }
191
+ *found = oseo_internal_own_property_descriptor(
192
+ context, target, key, value, attributes, getter, setter);
193
+ /* A trap result, invariant check, or forwarded descriptor sees the
194
+ * property's value, never the binding cell that a namespace export,
195
+ * global binding, or mapped arguments index stores. Reading the
196
+ * cell is the [[Get]] a namespace's [[GetOwnProperty]] performs, so
197
+ * an uninitialized export throws its ReferenceError here. */
198
+ if (*found && oseo_internal_cell_backed_property(target, *value)) {
199
+ OseoResult read = oseo_cell_get(context, *value);
200
+ if (read.status != OSEO_STATUS_NORMAL) return read;
201
+ *value = read.value;
202
+ }
203
+ /* An active function's legacy `arguments` is a new object, which a
204
+ * trap result can never match; the checks compare it at once. */
205
+ if (*found) {
206
+ OseoResult read =
207
+ oseo_internal_legacy_value(context, target, key, *value);
208
+ if (read.status != OSEO_STATUS_NORMAL) return read;
209
+ *value = read.value;
210
+ }
211
+ return normal(oseo_undefined());
212
+ }
213
+
214
+ static OseoResult target_is_extensible(
215
+ OseoContext *context,
216
+ OseoValue target
217
+ ) {
218
+ return oseo_internal_is_extensible(context, target);
219
+ }
220
+
221
+ OseoResult oseo_internal_proxy_get(
222
+ OseoContext *context,
223
+ OseoValue proxy,
224
+ OseoValue key,
225
+ OseoValue receiver
226
+ ) {
227
+ OseoResult entry = oseo_call_enter(context);
228
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
229
+ OseoValue slots[5] = {
230
+ proxy, key, receiver, oseo_undefined(), oseo_undefined(),
231
+ };
232
+ OseoRootFrame frame = {NULL, slots, 5u};
233
+ oseo_roots_push(context, &frame);
234
+ OseoResult result = proxy_trap(context, slots[0], "get");
235
+ slots[3] = result.value;
236
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[3])) {
237
+ result = oseo_super_get(
238
+ context,
239
+ proxy_object(slots[0])->target,
240
+ slots[1],
241
+ slots[2]
242
+ );
243
+ return proxy_complete(context, &frame, result);
244
+ }
245
+ OseoValue trap_arguments[3] = {
246
+ proxy_object(slots[0])->target, slots[1], slots[2],
247
+ };
248
+ if (result.status == OSEO_STATUS_NORMAL) {
249
+ result = call_trap(context, slots[0], slots[3], 3u, trap_arguments);
250
+ slots[4] = result.value;
251
+ }
252
+ OseoValue target_value = oseo_undefined();
253
+ OseoPropertyAttributes attributes = {false, false, false, false};
254
+ OseoValue getter = oseo_undefined();
255
+ OseoValue setter = oseo_undefined();
256
+ bool found = false;
257
+ if (result.status == OSEO_STATUS_NORMAL) {
258
+ result = target_own_property(
259
+ context,
260
+ proxy_object(slots[0])->target,
261
+ slots[1],
262
+ &found,
263
+ &target_value,
264
+ &attributes,
265
+ &getter,
266
+ &setter);
267
+ }
268
+ if (result.status == OSEO_STATUS_NORMAL && found &&
269
+ !attributes.configurable) {
270
+ if (!attributes.accessor && !attributes.writable &&
271
+ !oseo_internal_same_value(slots[4], target_value)) {
272
+ result = type_error(
273
+ context, "Proxy get trap violated an invariant.");
274
+ } else if (attributes.accessor &&
275
+ tag_of(getter) == OSEO_TAG_UNDEFINED &&
276
+ tag_of(slots[4]) != OSEO_TAG_UNDEFINED) {
277
+ result = type_error(
278
+ context, "Proxy get trap violated an invariant.");
279
+ }
280
+ }
281
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[4];
282
+ return proxy_complete(context, &frame, result);
283
+ }
284
+
285
+ OseoResult oseo_internal_proxy_set(
286
+ OseoContext *context,
287
+ OseoValue proxy,
288
+ OseoValue key,
289
+ OseoValue value,
290
+ OseoValue receiver,
291
+ const char **refusal
292
+ ) {
293
+ *refusal = NULL;
294
+ OseoResult entry = oseo_call_enter(context);
295
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
296
+ OseoValue slots[5] = {
297
+ proxy, key, value, receiver, oseo_undefined(),
298
+ };
299
+ OseoRootFrame frame = {NULL, slots, 5u};
300
+ oseo_roots_push(context, &frame);
301
+ OseoResult result = proxy_trap(context, slots[0], "set");
302
+ slots[4] = result.value;
303
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[4])) {
304
+ result = oseo_internal_set_with_receiver(
305
+ context,
306
+ proxy_object(slots[0])->target,
307
+ slots[1],
308
+ slots[2],
309
+ slots[3],
310
+ refusal
311
+ );
312
+ return proxy_complete(context, &frame, result);
313
+ }
314
+ OseoValue trap_arguments[4] = {
315
+ proxy_object(slots[0])->target, slots[1], slots[2], slots[3],
316
+ };
317
+ if (result.status == OSEO_STATUS_NORMAL) {
318
+ result = call_trap(context, slots[0], slots[4], 4u, trap_arguments);
319
+ }
320
+ bool accepted = result.status == OSEO_STATUS_NORMAL &&
321
+ oseo_to_boolean(result.value);
322
+ OseoValue target_value = oseo_undefined();
323
+ OseoPropertyAttributes attributes = {false, false, false, false};
324
+ OseoValue getter = oseo_undefined();
325
+ OseoValue setter = oseo_undefined();
326
+ bool found = false;
327
+ if (accepted) {
328
+ result = target_own_property(
329
+ context,
330
+ proxy_object(slots[0])->target,
331
+ slots[1],
332
+ &found,
333
+ &target_value,
334
+ &attributes,
335
+ &getter,
336
+ &setter);
337
+ }
338
+ if (result.status == OSEO_STATUS_NORMAL && accepted && found &&
339
+ !attributes.configurable) {
340
+ if (!attributes.accessor && !attributes.writable &&
341
+ !oseo_internal_same_value(slots[2], target_value)) {
342
+ result = type_error(
343
+ context, "Proxy set trap violated an invariant.");
344
+ } else if (attributes.accessor &&
345
+ tag_of(setter) == OSEO_TAG_UNDEFINED) {
346
+ result = type_error(
347
+ context, "Proxy set trap violated an invariant.");
348
+ }
349
+ }
350
+ if (result.status == OSEO_STATUS_NORMAL && !accepted) {
351
+ *refusal = "Proxy set trap returned false.";
352
+ }
353
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[2];
354
+ return proxy_complete(context, &frame, result);
355
+ }
356
+
357
+ OseoResult oseo_internal_proxy_has(
358
+ OseoContext *context,
359
+ OseoValue proxy,
360
+ OseoValue key
361
+ ) {
362
+ OseoResult entry = oseo_call_enter(context);
363
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
364
+ OseoValue slots[3] = {proxy, key, oseo_undefined()};
365
+ OseoRootFrame frame = {NULL, slots, 3u};
366
+ oseo_roots_push(context, &frame);
367
+ OseoResult result = proxy_trap(context, slots[0], "has");
368
+ slots[2] = result.value;
369
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[2])) {
370
+ result = oseo_has_property(
371
+ context, slots[1], proxy_object(slots[0])->target);
372
+ return proxy_complete(context, &frame, result);
373
+ }
374
+ OseoValue trap_arguments[2] = {
375
+ proxy_object(slots[0])->target, slots[1],
376
+ };
377
+ if (result.status == OSEO_STATUS_NORMAL) {
378
+ result = call_trap(context, slots[0], slots[2], 2u, trap_arguments);
379
+ }
380
+ bool present = result.status == OSEO_STATUS_NORMAL &&
381
+ oseo_to_boolean(result.value);
382
+ OseoValue target_value = oseo_undefined();
383
+ OseoPropertyAttributes attributes = {false, false, false, false};
384
+ OseoValue getter = oseo_undefined();
385
+ OseoValue setter = oseo_undefined();
386
+ bool found = false;
387
+ if (result.status == OSEO_STATUS_NORMAL && !present) {
388
+ result = target_own_property(
389
+ context,
390
+ proxy_object(slots[0])->target,
391
+ slots[1],
392
+ &found,
393
+ &target_value,
394
+ &attributes,
395
+ &getter,
396
+ &setter
397
+ );
398
+ }
399
+ if (result.status == OSEO_STATUS_NORMAL && found) {
400
+ if (!attributes.configurable) {
401
+ result = type_error(
402
+ context, "Proxy has trap violated an invariant.");
403
+ } else {
404
+ OseoResult extensible = target_is_extensible(
405
+ context, proxy_object(slots[0])->target);
406
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
407
+ else if (!oseo_to_boolean(extensible.value)) {
408
+ result = type_error(
409
+ context, "Proxy has trap violated an invariant.");
410
+ }
411
+ }
412
+ }
413
+ if (result.status == OSEO_STATUS_NORMAL) {
414
+ result = normal(oseo_boolean(present));
415
+ }
416
+ return proxy_complete(context, &frame, result);
417
+ }
418
+
419
+ OseoResult oseo_internal_get_prototype(
420
+ OseoContext *context,
421
+ OseoValue object_value
422
+ ) {
423
+ if (!is_proxy(object_value)) {
424
+ return normal(ordinary_object(object_value)->prototype);
425
+ }
426
+ OseoResult entry = oseo_call_enter(context);
427
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
428
+ OseoValue slots[3] = {object_value, oseo_undefined(), oseo_undefined()};
429
+ OseoRootFrame frame = {NULL, slots, 3u};
430
+ oseo_roots_push(context, &frame);
431
+ OseoResult result = proxy_trap(context, slots[0], "getPrototypeOf");
432
+ slots[1] = result.value;
433
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[1])) {
434
+ result = oseo_internal_get_prototype(
435
+ context, proxy_object(slots[0])->target);
436
+ return proxy_complete(context, &frame, result);
437
+ }
438
+ OseoValue trap_arguments[1] = {proxy_object(slots[0])->target};
439
+ if (result.status == OSEO_STATUS_NORMAL) {
440
+ result = call_trap(context, slots[0], slots[1], 1u, trap_arguments);
441
+ slots[2] = result.value;
442
+ }
443
+ if (result.status == OSEO_STATUS_NORMAL &&
444
+ tag_of(slots[2]) != OSEO_TAG_NULL && !is_object(slots[2])) {
445
+ result = type_error(
446
+ context, "Proxy getPrototypeOf trap returned a non-object.");
447
+ }
448
+ if (result.status == OSEO_STATUS_NORMAL) {
449
+ OseoResult extensible = target_is_extensible(
450
+ context, proxy_object(slots[0])->target);
451
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
452
+ else if (!oseo_to_boolean(extensible.value)) {
453
+ OseoResult actual = oseo_internal_get_prototype(
454
+ context, proxy_object(slots[0])->target);
455
+ if (actual.status != OSEO_STATUS_NORMAL) result = actual;
456
+ else if (!oseo_internal_same_value(actual.value, slots[2])) {
457
+ result = type_error(
458
+ context,
459
+ "Proxy getPrototypeOf trap violated an invariant.");
460
+ }
461
+ }
462
+ }
463
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[2];
464
+ return proxy_complete(context, &frame, result);
465
+ }
466
+
467
+ OseoResult oseo_internal_proxy_set_prototype(
468
+ OseoContext *context,
469
+ OseoValue proxy,
470
+ OseoValue prototype,
471
+ const char **refusal
472
+ ) {
473
+ *refusal = NULL;
474
+ OseoResult entry = oseo_call_enter(context);
475
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
476
+ OseoValue slots[3] = {proxy, prototype, oseo_undefined()};
477
+ OseoRootFrame frame = {NULL, slots, 3u};
478
+ oseo_roots_push(context, &frame);
479
+ OseoResult result = proxy_trap(context, slots[0], "setPrototypeOf");
480
+ slots[2] = result.value;
481
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[2])) {
482
+ result = oseo_internal_set_prototype_reported(
483
+ context,
484
+ proxy_object(slots[0])->target,
485
+ slots[1],
486
+ refusal
487
+ );
488
+ return proxy_complete(context, &frame, result);
489
+ }
490
+ OseoValue trap_arguments[2] = {
491
+ proxy_object(slots[0])->target, slots[1],
492
+ };
493
+ if (result.status == OSEO_STATUS_NORMAL) {
494
+ result = call_trap(context, slots[0], slots[2], 2u, trap_arguments);
495
+ }
496
+ bool accepted = result.status == OSEO_STATUS_NORMAL &&
497
+ oseo_to_boolean(result.value);
498
+ if (result.status == OSEO_STATUS_NORMAL && accepted) {
499
+ OseoResult extensible = target_is_extensible(
500
+ context, proxy_object(slots[0])->target);
501
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
502
+ else if (!oseo_to_boolean(extensible.value)) {
503
+ OseoResult actual = oseo_internal_get_prototype(
504
+ context, proxy_object(slots[0])->target);
505
+ if (actual.status != OSEO_STATUS_NORMAL) result = actual;
506
+ else if (!oseo_internal_same_value(actual.value, slots[1])) {
507
+ result = type_error(
508
+ context,
509
+ "Proxy setPrototypeOf trap violated an invariant.");
510
+ }
511
+ }
512
+ }
513
+ if (result.status == OSEO_STATUS_NORMAL && !accepted) {
514
+ *refusal = "Proxy setPrototypeOf trap returned false.";
515
+ }
516
+ if (result.status == OSEO_STATUS_NORMAL) result.value = proxy;
517
+ return proxy_complete(context, &frame, result);
518
+ }
519
+
520
+ OseoResult oseo_internal_is_extensible(
521
+ OseoContext *context,
522
+ OseoValue object_value
523
+ ) {
524
+ if (!is_proxy(object_value)) {
525
+ return normal(oseo_boolean(ordinary_object(object_value)->extensible));
526
+ }
527
+ OseoResult entry = oseo_call_enter(context);
528
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
529
+ OseoValue slots[2] = {object_value, oseo_undefined()};
530
+ OseoRootFrame frame = {NULL, slots, 2u};
531
+ oseo_roots_push(context, &frame);
532
+ OseoResult result = proxy_trap(context, slots[0], "isExtensible");
533
+ slots[1] = result.value;
534
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[1])) {
535
+ result = oseo_internal_is_extensible(
536
+ context, proxy_object(slots[0])->target);
537
+ return proxy_complete(context, &frame, result);
538
+ }
539
+ OseoValue trap_arguments[1] = {proxy_object(slots[0])->target};
540
+ if (result.status == OSEO_STATUS_NORMAL) {
541
+ result = call_trap(context, slots[0], slots[1], 1u, trap_arguments);
542
+ }
543
+ bool answer = result.status == OSEO_STATUS_NORMAL &&
544
+ oseo_to_boolean(result.value);
545
+ if (result.status == OSEO_STATUS_NORMAL) {
546
+ OseoResult actual = oseo_internal_is_extensible(
547
+ context, proxy_object(slots[0])->target);
548
+ if (actual.status != OSEO_STATUS_NORMAL) result = actual;
549
+ else if (answer != oseo_to_boolean(actual.value)) {
550
+ result = type_error(
551
+ context, "Proxy isExtensible trap violated an invariant.");
552
+ }
553
+ }
554
+ if (result.status == OSEO_STATUS_NORMAL) {
555
+ result = normal(oseo_boolean(answer));
556
+ }
557
+ return proxy_complete(context, &frame, result);
558
+ }
559
+
560
+ OseoResult oseo_internal_prevent_extensions_reported(
561
+ OseoContext *context,
562
+ OseoValue object_value,
563
+ const char **refusal
564
+ ) {
565
+ *refusal = NULL;
566
+ if (is_typed_array(object_value) &&
567
+ !oseo_internal_typed_array_fixed_length(object_value)) {
568
+ /* TypedArray [[PreventExtensions]]: a view whose
569
+ * integer indices can appear as its buffer grows stays
570
+ * extensible. */
571
+ *refusal =
572
+ "Cannot prevent extensions of a TypedArray whose length can "
573
+ "change.";
574
+ return normal(object_value);
575
+ }
576
+ if (!is_proxy(object_value)) {
577
+ ordinary_object(object_value)->extensible = false;
578
+ return normal(object_value);
579
+ }
580
+ OseoResult entry = oseo_call_enter(context);
581
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
582
+ OseoValue slots[2] = {object_value, oseo_undefined()};
583
+ OseoRootFrame frame = {NULL, slots, 2u};
584
+ oseo_roots_push(context, &frame);
585
+ OseoResult result = proxy_trap(context, slots[0], "preventExtensions");
586
+ slots[1] = result.value;
587
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[1])) {
588
+ result = oseo_internal_prevent_extensions_reported(
589
+ context, proxy_object(slots[0])->target, refusal);
590
+ return proxy_complete(context, &frame, result);
591
+ }
592
+ OseoValue trap_arguments[1] = {proxy_object(slots[0])->target};
593
+ if (result.status == OSEO_STATUS_NORMAL) {
594
+ result = call_trap(context, slots[0], slots[1], 1u, trap_arguments);
595
+ }
596
+ bool accepted = result.status == OSEO_STATUS_NORMAL &&
597
+ oseo_to_boolean(result.value);
598
+ if (result.status == OSEO_STATUS_NORMAL && accepted) {
599
+ OseoResult extensible = oseo_internal_is_extensible(
600
+ context, proxy_object(slots[0])->target);
601
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
602
+ else if (oseo_to_boolean(extensible.value)) {
603
+ result = type_error(
604
+ context, "Proxy preventExtensions trap violated an invariant.");
605
+ }
606
+ }
607
+ if (result.status == OSEO_STATUS_NORMAL && !accepted) {
608
+ *refusal = "Proxy preventExtensions trap returned false.";
609
+ }
610
+ if (result.status == OSEO_STATUS_NORMAL) result.value = object_value;
611
+ return proxy_complete(context, &frame, result);
612
+ }
613
+
614
+ static bool compatible_descriptor(
615
+ const OseoConvertedDescriptor *descriptor,
616
+ OseoValue value,
617
+ OseoValue getter,
618
+ OseoValue setter,
619
+ bool current_exists,
620
+ OseoValue current_value,
621
+ OseoPropertyAttributes current,
622
+ OseoValue current_getter,
623
+ OseoValue current_setter,
624
+ bool extensible
625
+ ) {
626
+ if (!current_exists) return extensible;
627
+ if (current.configurable) return true;
628
+ if (descriptor->has_configurable && descriptor->configurable) return false;
629
+ if (descriptor->has_enumerable &&
630
+ descriptor->enumerable != current.enumerable) return false;
631
+ bool descriptor_accessor = descriptor->has_getter || descriptor->has_setter;
632
+ bool descriptor_data = descriptor->has_value || descriptor->has_writable;
633
+ if ((descriptor_accessor && !current.accessor) ||
634
+ (descriptor_data && current.accessor)) return false;
635
+ if (current.accessor) {
636
+ if (descriptor->has_getter &&
637
+ !oseo_internal_same_value(getter, current_getter)) return false;
638
+ if (descriptor->has_setter &&
639
+ !oseo_internal_same_value(setter, current_setter)) return false;
640
+ return true;
641
+ }
642
+ if (!current.writable) {
643
+ if (descriptor->has_writable && descriptor->writable) return false;
644
+ if (descriptor->has_value &&
645
+ !oseo_internal_same_value(value, current_value)) return false;
646
+ }
647
+ return true;
648
+ }
649
+
650
+ OseoResult oseo_internal_proxy_get_own_property(
651
+ OseoContext *context,
652
+ OseoValue proxy,
653
+ OseoValue key,
654
+ bool *found,
655
+ OseoValue *value,
656
+ OseoPropertyAttributes *attributes,
657
+ OseoValue *getter,
658
+ OseoValue *setter
659
+ ) {
660
+ *found = false;
661
+ OseoResult entry = oseo_call_enter(context);
662
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
663
+ OseoValue slots[6] = {
664
+ proxy, key, oseo_undefined(), oseo_undefined(), oseo_undefined(),
665
+ oseo_undefined(),
666
+ };
667
+ OseoRootFrame frame = {NULL, slots, 6u};
668
+ oseo_roots_push(context, &frame);
669
+ OseoResult result = proxy_trap(
670
+ context, slots[0], "getOwnPropertyDescriptor");
671
+ slots[2] = result.value;
672
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[2])) {
673
+ result = target_own_property(
674
+ context,
675
+ proxy_object(slots[0])->target,
676
+ slots[1],
677
+ found,
678
+ value,
679
+ attributes,
680
+ getter,
681
+ setter);
682
+ return proxy_complete(context, &frame, result);
683
+ }
684
+ OseoValue trap_arguments[2] = {
685
+ proxy_object(slots[0])->target, slots[1],
686
+ };
687
+ if (result.status == OSEO_STATUS_NORMAL) {
688
+ result = call_trap(context, slots[0], slots[2], 2u, trap_arguments);
689
+ slots[2] = result.value;
690
+ }
691
+ if (result.status == OSEO_STATUS_NORMAL &&
692
+ tag_of(slots[2]) != OSEO_TAG_UNDEFINED &&
693
+ !is_object(slots[2])) {
694
+ result = type_error(
695
+ context,
696
+ "Proxy getOwnPropertyDescriptor trap returned an invalid value.");
697
+ }
698
+ /* Identity is compared only for non-configurable immutable halves.
699
+ * Those values stay owned by the rooted target; configurable snapshots
700
+ * need attributes only. */
701
+ bool target_found = false;
702
+ OseoValue target_value = oseo_undefined();
703
+ OseoValue target_getter = oseo_undefined();
704
+ OseoValue target_setter = oseo_undefined();
705
+ OseoPropertyAttributes target_attributes = {false, false, false, false};
706
+ if (result.status == OSEO_STATUS_NORMAL) {
707
+ result = target_own_property(
708
+ context,
709
+ proxy_object(slots[0])->target,
710
+ slots[1],
711
+ &target_found,
712
+ &target_value,
713
+ &target_attributes,
714
+ &target_getter,
715
+ &target_setter);
716
+ }
717
+ OseoResult extensible = normal(oseo_boolean(false));
718
+ if (result.status == OSEO_STATUS_NORMAL &&
719
+ tag_of(slots[2]) == OSEO_TAG_UNDEFINED) {
720
+ if (!target_found) {
721
+ return proxy_complete(
722
+ context, &frame, normal(oseo_undefined()));
723
+ }
724
+ if (!target_attributes.configurable) {
725
+ result = type_error(
726
+ context,
727
+ "Proxy getOwnPropertyDescriptor trap violated an invariant.");
728
+ return proxy_complete(context, &frame, result);
729
+ }
730
+ extensible = target_is_extensible(
731
+ context, proxy_object(slots[0])->target);
732
+ if (extensible.status != OSEO_STATUS_NORMAL) {
733
+ return proxy_complete(context, &frame, extensible);
734
+ }
735
+ if (!oseo_to_boolean(extensible.value)) {
736
+ result = type_error(
737
+ context,
738
+ "Proxy getOwnPropertyDescriptor trap violated an invariant.");
739
+ }
740
+ result = result.status == OSEO_STATUS_NORMAL
741
+ ? normal(oseo_undefined())
742
+ : result;
743
+ return proxy_complete(context, &frame, result);
744
+ }
745
+ if (result.status == OSEO_STATUS_NORMAL) {
746
+ extensible = target_is_extensible(
747
+ context, proxy_object(slots[0])->target);
748
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
749
+ }
750
+ OseoConvertedDescriptor descriptor = {0};
751
+ if (result.status == OSEO_STATUS_NORMAL) {
752
+ result = oseo_internal_to_property_descriptor(
753
+ context,
754
+ slots[2],
755
+ &slots[3],
756
+ &slots[4],
757
+ &slots[5],
758
+ &descriptor
759
+ );
760
+ }
761
+ if (result.status == OSEO_STATUS_NORMAL) {
762
+ bool accessor = descriptor.has_getter || descriptor.has_setter;
763
+ bool data = descriptor.has_value || descriptor.has_writable;
764
+ descriptor.has_enumerable = true;
765
+ descriptor.has_configurable = true;
766
+ if (!accessor) {
767
+ descriptor.has_value = true;
768
+ descriptor.has_writable = true;
769
+ if (!data) slots[3] = oseo_undefined();
770
+ } else {
771
+ descriptor.has_getter = true;
772
+ descriptor.has_setter = true;
773
+ }
774
+ }
775
+ if (result.status == OSEO_STATUS_NORMAL) {
776
+ bool compatible = compatible_descriptor(
777
+ &descriptor,
778
+ slots[3],
779
+ slots[4],
780
+ slots[5],
781
+ target_found,
782
+ target_value,
783
+ target_attributes,
784
+ target_getter,
785
+ target_setter,
786
+ oseo_to_boolean(extensible.value));
787
+ if (!compatible) {
788
+ result = type_error(context,
789
+ "Proxy getOwnPropertyDescriptor trap violated an invariant.");
790
+ }
791
+ }
792
+ if (result.status == OSEO_STATUS_NORMAL &&
793
+ descriptor.has_configurable && !descriptor.configurable &&
794
+ (!target_found || target_attributes.configurable)) {
795
+ result = type_error(
796
+ context,
797
+ "Proxy getOwnPropertyDescriptor trap violated an invariant.");
798
+ }
799
+ if (result.status == OSEO_STATUS_NORMAL && target_found &&
800
+ !descriptor.configurable && !target_attributes.configurable &&
801
+ !target_attributes.accessor && target_attributes.writable &&
802
+ descriptor.has_writable && !descriptor.writable) {
803
+ result = type_error(
804
+ context,
805
+ "Proxy getOwnPropertyDescriptor trap violated an invariant.");
806
+ }
807
+ if (result.status == OSEO_STATUS_NORMAL) {
808
+ bool accessor = descriptor.has_getter || descriptor.has_setter;
809
+ *found = true;
810
+ *value = descriptor.has_value ? slots[3] : oseo_undefined();
811
+ *getter = descriptor.has_getter ? slots[4] : oseo_undefined();
812
+ *setter = descriptor.has_setter ? slots[5] : oseo_undefined();
813
+ *attributes = (OseoPropertyAttributes){
814
+ descriptor.has_configurable && descriptor.configurable,
815
+ descriptor.has_enumerable && descriptor.enumerable,
816
+ descriptor.has_writable && descriptor.writable,
817
+ accessor,
818
+ };
819
+ }
820
+ return proxy_complete(context, &frame, result);
821
+ }
822
+
823
+ static OseoResult define_descriptor_field(
824
+ OseoContext *context,
825
+ OseoValue object,
826
+ const char *name,
827
+ OseoValue value
828
+ ) {
829
+ OseoValue slots[3] = {object, value, oseo_undefined()};
830
+ OseoRootFrame frame = {NULL, slots, 3u};
831
+ oseo_roots_push(context, &frame);
832
+ OseoResult result = oseo_internal_ascii_string(context, name);
833
+ slots[2] = result.value;
834
+ if (result.status == OSEO_STATUS_NORMAL) {
835
+ const OseoPropertyAttributes attrs = {true, true, true, false};
836
+ result = oseo_object_define(
837
+ context, slots[0], slots[2], slots[1], attrs);
838
+ }
839
+ oseo_roots_pop(context, &frame);
840
+ return result;
841
+ }
842
+
843
+ static OseoResult descriptor_object(
844
+ OseoContext *context,
845
+ const OseoConvertedDescriptor *descriptor,
846
+ OseoValue value,
847
+ OseoValue getter,
848
+ OseoValue setter
849
+ ) {
850
+ OseoValue slots[4] = {value, getter, setter, oseo_undefined()};
851
+ OseoRootFrame frame = {NULL, slots, 4u};
852
+ oseo_roots_push(context, &frame);
853
+ OseoResult result = oseo_object_literal_create(context);
854
+ slots[3] = result.value;
855
+ #define DEFINE_DESCRIPTOR_FIELD(condition, name, field_value) \
856
+ if (result.status == OSEO_STATUS_NORMAL && (condition)) { \
857
+ result = define_descriptor_field( \
858
+ context, slots[3], (name), (field_value)); \
859
+ }
860
+ DEFINE_DESCRIPTOR_FIELD(
861
+ descriptor->has_value, "value", slots[0]);
862
+ DEFINE_DESCRIPTOR_FIELD(
863
+ descriptor->has_writable, "writable",
864
+ oseo_boolean(descriptor->writable));
865
+ DEFINE_DESCRIPTOR_FIELD(
866
+ descriptor->has_getter, "get", slots[1]);
867
+ DEFINE_DESCRIPTOR_FIELD(
868
+ descriptor->has_setter, "set", slots[2]);
869
+ DEFINE_DESCRIPTOR_FIELD(
870
+ descriptor->has_enumerable, "enumerable",
871
+ oseo_boolean(descriptor->enumerable));
872
+ DEFINE_DESCRIPTOR_FIELD(
873
+ descriptor->has_configurable, "configurable",
874
+ oseo_boolean(descriptor->configurable));
875
+ #undef DEFINE_DESCRIPTOR_FIELD
876
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[3];
877
+ oseo_roots_pop(context, &frame);
878
+ return result;
879
+ }
880
+
881
+ OseoResult oseo_internal_proxy_define_own_property(
882
+ OseoContext *context,
883
+ OseoValue proxy,
884
+ OseoValue key,
885
+ const OseoConvertedDescriptor *descriptor,
886
+ OseoValue value,
887
+ OseoValue getter,
888
+ OseoValue setter,
889
+ const char **refusal
890
+ ) {
891
+ *refusal = NULL;
892
+ OseoResult entry = oseo_call_enter(context);
893
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
894
+ OseoValue slots[7] = {
895
+ proxy, key, value, getter, setter, oseo_undefined(), oseo_undefined(),
896
+ };
897
+ OseoRootFrame frame = {NULL, slots, 7u};
898
+ oseo_roots_push(context, &frame);
899
+ OseoResult result = proxy_trap(context, slots[0], "defineProperty");
900
+ slots[5] = result.value;
901
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[5])) {
902
+ OseoValue target = proxy_object(slots[0])->target;
903
+ result = oseo_internal_define_converted_property(
904
+ context,
905
+ target,
906
+ slots[1],
907
+ descriptor,
908
+ slots[2],
909
+ slots[3],
910
+ slots[4],
911
+ refusal
912
+ );
913
+ return proxy_complete(context, &frame, result);
914
+ }
915
+ if (result.status == OSEO_STATUS_NORMAL) {
916
+ result = descriptor_object(
917
+ context, descriptor, slots[2], slots[3], slots[4]);
918
+ slots[6] = result.value;
919
+ }
920
+ OseoValue trap_arguments[3] = {
921
+ proxy_object(slots[0])->target, slots[1], slots[6],
922
+ };
923
+ if (result.status == OSEO_STATUS_NORMAL) {
924
+ result = call_trap(context, slots[0], slots[5], 3u, trap_arguments);
925
+ }
926
+ bool accepted = result.status == OSEO_STATUS_NORMAL &&
927
+ oseo_to_boolean(result.value);
928
+ if (result.status == OSEO_STATUS_NORMAL && !accepted) {
929
+ *refusal = "Proxy defineProperty trap returned false.";
930
+ }
931
+ /* Identity is compared only for non-configurable immutable halves.
932
+ * Those values stay owned by the rooted target; configurable snapshots
933
+ * need attributes only. */
934
+ bool target_found = false;
935
+ OseoValue target_value = oseo_undefined();
936
+ OseoValue target_getter = oseo_undefined();
937
+ OseoValue target_setter = oseo_undefined();
938
+ OseoPropertyAttributes target_attributes = {false, false, false, false};
939
+ if (result.status == OSEO_STATUS_NORMAL && accepted) {
940
+ result = target_own_property(
941
+ context,
942
+ proxy_object(slots[0])->target,
943
+ slots[1],
944
+ &target_found,
945
+ &target_value,
946
+ &target_attributes,
947
+ &target_getter,
948
+ &target_setter);
949
+ }
950
+ OseoResult extensible = normal(oseo_boolean(false));
951
+ if (result.status == OSEO_STATUS_NORMAL && accepted) {
952
+ extensible = target_is_extensible(
953
+ context, proxy_object(slots[0])->target);
954
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
955
+ }
956
+ if (result.status == OSEO_STATUS_NORMAL && accepted) {
957
+ bool compatible = compatible_descriptor(
958
+ descriptor,
959
+ slots[2],
960
+ slots[3],
961
+ slots[4],
962
+ target_found,
963
+ target_value,
964
+ target_attributes,
965
+ target_getter,
966
+ target_setter,
967
+ oseo_to_boolean(extensible.value));
968
+ if (!compatible) {
969
+ result = type_error(context,
970
+ "Proxy defineProperty trap violated an invariant.");
971
+ }
972
+ }
973
+ if (result.status == OSEO_STATUS_NORMAL && accepted &&
974
+ descriptor->has_configurable && !descriptor->configurable &&
975
+ (!target_found || target_attributes.configurable)) {
976
+ result = type_error(
977
+ context, "Proxy defineProperty trap violated an invariant.");
978
+ }
979
+ if (result.status == OSEO_STATUS_NORMAL && accepted && target_found &&
980
+ !target_attributes.configurable && !target_attributes.accessor &&
981
+ target_attributes.writable && descriptor->has_writable &&
982
+ !descriptor->writable) {
983
+ result = type_error(
984
+ context, "Proxy defineProperty trap violated an invariant.");
985
+ }
986
+ if (result.status == OSEO_STATUS_NORMAL) result.value = proxy;
987
+ return proxy_complete(context, &frame, result);
988
+ }
989
+
990
+ OseoResult oseo_internal_proxy_delete(
991
+ OseoContext *context,
992
+ OseoValue proxy,
993
+ OseoValue key,
994
+ bool strict
995
+ ) {
996
+ OseoResult entry = oseo_call_enter(context);
997
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
998
+ OseoValue slots[3] = {proxy, key, oseo_undefined()};
999
+ OseoRootFrame frame = {NULL, slots, 3u};
1000
+ oseo_roots_push(context, &frame);
1001
+ OseoResult result = proxy_trap(context, slots[0], "deleteProperty");
1002
+ slots[2] = result.value;
1003
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[2])) {
1004
+ result = oseo_object_delete(
1005
+ context, proxy_object(slots[0])->target, slots[1], strict);
1006
+ return proxy_complete(context, &frame, result);
1007
+ }
1008
+ OseoValue trap_arguments[2] = {
1009
+ proxy_object(slots[0])->target, slots[1],
1010
+ };
1011
+ if (result.status == OSEO_STATUS_NORMAL) {
1012
+ result = call_trap(context, slots[0], slots[2], 2u, trap_arguments);
1013
+ }
1014
+ bool accepted = result.status == OSEO_STATUS_NORMAL &&
1015
+ oseo_to_boolean(result.value);
1016
+ bool target_found = false;
1017
+ OseoValue target_value = oseo_undefined();
1018
+ OseoPropertyAttributes target_attributes = {false, false, false, false};
1019
+ OseoValue getter = oseo_undefined();
1020
+ OseoValue setter = oseo_undefined();
1021
+ if (result.status == OSEO_STATUS_NORMAL && accepted) {
1022
+ result = target_own_property(
1023
+ context,
1024
+ proxy_object(slots[0])->target,
1025
+ slots[1],
1026
+ &target_found,
1027
+ &target_value,
1028
+ &target_attributes,
1029
+ &getter,
1030
+ &setter
1031
+ );
1032
+ }
1033
+ if (result.status == OSEO_STATUS_NORMAL && accepted && target_found) {
1034
+ if (!target_attributes.configurable) {
1035
+ result = type_error(
1036
+ context, "Proxy deleteProperty trap violated an invariant.");
1037
+ } else {
1038
+ OseoResult extensible = target_is_extensible(
1039
+ context, proxy_object(slots[0])->target);
1040
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
1041
+ else if (!oseo_to_boolean(extensible.value)) {
1042
+ result = type_error(
1043
+ context,
1044
+ "Proxy deleteProperty trap violated an invariant.");
1045
+ }
1046
+ }
1047
+ }
1048
+ if (result.status == OSEO_STATUS_NORMAL && !accepted && strict) {
1049
+ result = type_error(
1050
+ context, "Proxy deleteProperty trap returned false.");
1051
+ }
1052
+ if (result.status == OSEO_STATUS_NORMAL) {
1053
+ result = normal(oseo_boolean(accepted));
1054
+ }
1055
+ return proxy_complete(context, &frame, result);
1056
+ }
1057
+
1058
+ static bool list_contains(
1059
+ const OseoValue *values,
1060
+ size_t count,
1061
+ OseoValue key
1062
+ ) {
1063
+ for (size_t index = 0u; index < count; index += 1u) {
1064
+ if (oseo_internal_property_key_equal(values[index], key)) return true;
1065
+ }
1066
+ return false;
1067
+ }
1068
+
1069
+ /* CreateListFromArrayLike with the ownKeys element-type check at each Get. */
1070
+ static OseoResult property_key_list(
1071
+ OseoContext *context,
1072
+ OseoValue source,
1073
+ OseoValue *list
1074
+ ) {
1075
+ if (!is_object(source)) {
1076
+ return type_error(context, "Proxy ownKeys trap must return an object.");
1077
+ }
1078
+ OseoValue slots[4] = {
1079
+ source, oseo_undefined(), oseo_undefined(), oseo_undefined(),
1080
+ };
1081
+ OseoRootFrame frame = {NULL, slots, 4u};
1082
+ oseo_roots_push(context, &frame);
1083
+ double length = 0.0;
1084
+ OseoResult result = normal(slots[0]);
1085
+ if (is_array(slots[0])) {
1086
+ length = (double)ordinary_object(slots[0])->array_length;
1087
+ } else {
1088
+ result = oseo_internal_ascii_string(context, "length");
1089
+ slots[2] = result.value;
1090
+ if (result.status == OSEO_STATUS_NORMAL) {
1091
+ result = oseo_object_get(context, slots[0], slots[2]);
1092
+ }
1093
+ if (result.status == OSEO_STATUS_NORMAL) {
1094
+ result = oseo_internal_to_number(context, result.value);
1095
+ }
1096
+ if (result.status == OSEO_STATUS_NORMAL) {
1097
+ double number = number_value(result.value);
1098
+ if (number > 0.0) {
1099
+ length = floor(number);
1100
+ if (length > 9007199254740991.0) {
1101
+ length = 9007199254740991.0;
1102
+ }
1103
+ }
1104
+ }
1105
+ }
1106
+ if (result.status == OSEO_STATUS_NORMAL && length > (double)SIZE_MAX) {
1107
+ result = failure(
1108
+ context, "OSEO2001", "A Proxy ownKeys result is too large.");
1109
+ }
1110
+ if (result.status == OSEO_STATUS_NORMAL) {
1111
+ result = oseo_argument_list_create(context);
1112
+ slots[1] = result.value;
1113
+ }
1114
+ for (size_t index = 0u;
1115
+ result.status == OSEO_STATUS_NORMAL && (double)index < length;
1116
+ index += 1u) {
1117
+ result = oseo_property_key(context, oseo_number((double)index));
1118
+ slots[2] = result.value;
1119
+ if (result.status == OSEO_STATUS_NORMAL) {
1120
+ result = oseo_object_get(context, slots[0], slots[2]);
1121
+ slots[3] = result.value;
1122
+ }
1123
+ if (result.status == OSEO_STATUS_NORMAL &&
1124
+ !is_string(slots[3]) && !is_symbol(slots[3])) {
1125
+ result = type_error(
1126
+ context, "Proxy ownKeys trap returned an invalid key.");
1127
+ }
1128
+ if (result.status == OSEO_STATUS_NORMAL) {
1129
+ result = oseo_argument_list_append(
1130
+ context, slots[1], slots[3]);
1131
+ }
1132
+ }
1133
+ if (result.status == OSEO_STATUS_NORMAL) *list = slots[1];
1134
+ oseo_roots_pop(context, &frame);
1135
+ return result;
1136
+ }
1137
+
1138
+ OseoResult oseo_internal_proxy_own_keys(
1139
+ OseoContext *context,
1140
+ OseoValue proxy,
1141
+ OseoOwnKeyFilter filter
1142
+ ) {
1143
+ OseoResult entry = oseo_call_enter(context);
1144
+ if (entry.status != OSEO_STATUS_NORMAL) return entry;
1145
+ OseoValue slots[6] = {
1146
+ proxy, oseo_undefined(), oseo_undefined(), oseo_undefined(),
1147
+ oseo_undefined(), oseo_undefined(),
1148
+ };
1149
+ OseoRootFrame frame = {NULL, slots, 6u};
1150
+ oseo_roots_push(context, &frame);
1151
+ OseoResult result = proxy_trap(context, slots[0], "ownKeys");
1152
+ slots[1] = result.value;
1153
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[1])) {
1154
+ result = oseo_internal_own_key_array(
1155
+ context, proxy_object(slots[0])->target, filter);
1156
+ return proxy_complete(context, &frame, result);
1157
+ }
1158
+ OseoValue trap_arguments[1] = {proxy_object(slots[0])->target};
1159
+ if (result.status == OSEO_STATUS_NORMAL) {
1160
+ result = call_trap(context, slots[0], slots[1], 1u, trap_arguments);
1161
+ slots[2] = result.value;
1162
+ }
1163
+ if (result.status == OSEO_STATUS_NORMAL) {
1164
+ result = property_key_list(context, slots[2], &slots[3]);
1165
+ }
1166
+ size_t trap_count = 0u;
1167
+ const OseoValue *trap_values = NULL;
1168
+ if (result.status == OSEO_STATUS_NORMAL) {
1169
+ result = oseo_argument_list_view(
1170
+ context, slots[3], &trap_count, &trap_values);
1171
+ }
1172
+ for (size_t index = 0u;
1173
+ result.status == OSEO_STATUS_NORMAL && index < trap_count;
1174
+ index += 1u) {
1175
+ if (list_contains(trap_values, index, trap_values[index])) {
1176
+ result = type_error(
1177
+ context, "Proxy ownKeys trap returned invalid keys.");
1178
+ }
1179
+ }
1180
+ if (result.status == OSEO_STATUS_NORMAL) {
1181
+ result = oseo_internal_own_key_array(
1182
+ context, proxy_object(slots[0])->target, OSEO_OWN_KEY_ALL);
1183
+ slots[4] = result.value;
1184
+ }
1185
+ if (result.status == OSEO_STATUS_NORMAL) {
1186
+ result = oseo_internal_array_like_list(context, slots[4], &slots[5]);
1187
+ }
1188
+ size_t target_count = 0u;
1189
+ const OseoValue *target_values = NULL;
1190
+ if (result.status == OSEO_STATUS_NORMAL) {
1191
+ result = oseo_argument_list_view(
1192
+ context, slots[5], &target_count, &target_values);
1193
+ }
1194
+ OseoResult extensible = normal(oseo_boolean(false));
1195
+ if (result.status == OSEO_STATUS_NORMAL) {
1196
+ extensible = target_is_extensible(
1197
+ context, proxy_object(slots[0])->target);
1198
+ if (extensible.status != OSEO_STATUS_NORMAL) result = extensible;
1199
+ }
1200
+ for (size_t index = 0u;
1201
+ result.status == OSEO_STATUS_NORMAL && index < target_count;
1202
+ index += 1u) {
1203
+ bool found = false;
1204
+ OseoValue value = oseo_undefined();
1205
+ OseoPropertyAttributes attributes = {false, false, false, false};
1206
+ OseoValue getter = oseo_undefined();
1207
+ OseoValue setter = oseo_undefined();
1208
+ result = target_own_property(
1209
+ context,
1210
+ proxy_object(slots[0])->target,
1211
+ target_values[index],
1212
+ &found,
1213
+ &value,
1214
+ &attributes,
1215
+ &getter,
1216
+ &setter
1217
+ );
1218
+ if (result.status == OSEO_STATUS_NORMAL && found &&
1219
+ (!attributes.configurable || !oseo_to_boolean(extensible.value)) &&
1220
+ !list_contains(trap_values, trap_count, target_values[index])) {
1221
+ result = type_error(
1222
+ context, "Proxy ownKeys trap omitted a required key.");
1223
+ }
1224
+ }
1225
+ if (result.status == OSEO_STATUS_NORMAL &&
1226
+ !oseo_to_boolean(extensible.value) && trap_count != target_count) {
1227
+ result = type_error(context, "Proxy ownKeys trap added an extra key.");
1228
+ }
1229
+ if (result.status == OSEO_STATUS_NORMAL) {
1230
+ result = oseo_array_create(context, 0u);
1231
+ slots[1] = result.value;
1232
+ }
1233
+ for (size_t index = 0u;
1234
+ result.status == OSEO_STATUS_NORMAL && index < trap_count;
1235
+ index += 1u) {
1236
+ OseoValue key = trap_values[index];
1237
+ if ((filter == OSEO_OWN_KEY_STRINGS && is_symbol(key)) ||
1238
+ (filter == OSEO_OWN_KEY_SYMBOLS && !is_symbol(key))) continue;
1239
+ result = oseo_array_append(context, slots[1], key);
1240
+ }
1241
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[1];
1242
+ return proxy_complete(context, &frame, result);
1243
+ }
1244
+
1245
+ static OseoResult array_from_arguments(
1246
+ OseoContext *context,
1247
+ size_t argument_count,
1248
+ const OseoValue *arguments
1249
+ ) {
1250
+ OseoResult result = oseo_array_create(context, 0u);
1251
+ OseoValue array = result.value;
1252
+ OseoRootFrame frame = {NULL, &array, 1u};
1253
+ oseo_roots_push(context, &frame);
1254
+ for (size_t index = 0u;
1255
+ result.status == OSEO_STATUS_NORMAL && index < argument_count;
1256
+ index += 1u) {
1257
+ result = oseo_array_append(context, array, arguments[index]);
1258
+ }
1259
+ if (result.status == OSEO_STATUS_NORMAL) result.value = array;
1260
+ oseo_roots_pop(context, &frame);
1261
+ return result;
1262
+ }
1263
+
1264
+ OseoResult oseo_internal_is_array(
1265
+ OseoContext *context,
1266
+ OseoValue value
1267
+ ) {
1268
+ while (is_proxy(value)) {
1269
+ if (proxy_object(value)->revoked) {
1270
+ return type_error(context, "Cannot inspect a revoked Proxy.");
1271
+ }
1272
+ value = proxy_object(value)->target;
1273
+ }
1274
+ return normal(oseo_boolean(is_array(value)));
1275
+ }
1276
+
1277
+ OseoResult oseo_internal_proxy_call(
1278
+ OseoContext *context,
1279
+ OseoValue proxy,
1280
+ OseoValue receiver,
1281
+ size_t argument_count,
1282
+ const OseoValue *arguments,
1283
+ OseoValue new_target
1284
+ ) {
1285
+ bool constructing = tag_of(new_target) != OSEO_TAG_UNDEFINED;
1286
+ if ((!constructing && !proxy_object(proxy)->callable) ||
1287
+ (constructing && !proxy_object(proxy)->constructible)) {
1288
+ return type_error(context, "A Proxy target is not callable.");
1289
+ }
1290
+ const char *name = constructing ? "construct" : "apply";
1291
+ OseoValue slots[5] = {
1292
+ proxy, receiver, new_target, oseo_undefined(), oseo_undefined(),
1293
+ };
1294
+ OseoRootFrame frame = {NULL, slots, 5u};
1295
+ oseo_roots_push(context, &frame);
1296
+ OseoResult result = proxy_trap(context, slots[0], name);
1297
+ slots[3] = result.value;
1298
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[3])) {
1299
+ OseoValue target = proxy_object(slots[0])->target;
1300
+ if (!constructing) {
1301
+ /* [[Call]] returns the target's result unchanged and creates
1302
+ * no execution context, so it forwards through the
1303
+ * trampoline. */
1304
+ result = oseo_internal_tail_call(
1305
+ context, target, slots[1], argument_count, arguments);
1306
+ } else {
1307
+ result = oseo_internal_construct_receiver(
1308
+ context, target, slots[2]);
1309
+ slots[4] = result.value;
1310
+ if (result.status == OSEO_STATUS_NORMAL) {
1311
+ result = oseo_call_function(
1312
+ context, target, slots[4], argument_count, arguments,
1313
+ slots[2]);
1314
+ }
1315
+ if (result.status == OSEO_STATUS_NORMAL) {
1316
+ result = oseo_constructor_result(
1317
+ context, result.value, slots[4]);
1318
+ }
1319
+ }
1320
+ oseo_roots_pop(context, &frame);
1321
+ return result;
1322
+ }
1323
+ if (result.status == OSEO_STATUS_NORMAL) {
1324
+ result = array_from_arguments(context, argument_count, arguments);
1325
+ slots[4] = result.value;
1326
+ }
1327
+ OseoValue trap_arguments[3] = {
1328
+ proxy_object(slots[0])->target,
1329
+ constructing ? slots[4] : slots[1],
1330
+ constructing ? slots[2] : slots[4],
1331
+ };
1332
+ size_t trap_count = constructing ? 3u : 3u;
1333
+ if (!constructing) {
1334
+ trap_arguments[1] = slots[1];
1335
+ trap_arguments[2] = slots[4];
1336
+ }
1337
+ if (result.status == OSEO_STATUS_NORMAL && !constructing) {
1338
+ /* The apply trap's result is the [[Call]] result unchanged. */
1339
+ result = oseo_internal_tail_call(
1340
+ context,
1341
+ slots[3],
1342
+ proxy_object(slots[0])->handler,
1343
+ trap_count,
1344
+ trap_arguments
1345
+ );
1346
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1347
+ result = call_trap(
1348
+ context, slots[0], slots[3], trap_count, trap_arguments);
1349
+ }
1350
+ if (result.status == OSEO_STATUS_NORMAL && constructing &&
1351
+ !is_object(result.value)) {
1352
+ result = type_error(
1353
+ context, "Proxy construct trap must return an object.");
1354
+ }
1355
+ oseo_roots_pop(context, &frame);
1356
+ return result;
1357
+ }
1358
+
1359
+ static OseoResult create_builtin(
1360
+ OseoContext *context,
1361
+ const char *name,
1362
+ size_t length,
1363
+ size_t code_id,
1364
+ OseoValue environment
1365
+ ) {
1366
+ size_t name_length = strlen(name);
1367
+ if (name_length > 16u) {
1368
+ return failure(context, "OSEO2001", "Built-in name is too long.");
1369
+ }
1370
+ uint16_t units[16];
1371
+ for (size_t index = 0u; index < name_length; index += 1u) {
1372
+ units[index] = (uint16_t)(unsigned char)name[index];
1373
+ }
1374
+ return oseo_function_create(
1375
+ context, code_id, environment, units, name_length, length,
1376
+ OSEO_FUNCTION_INTERNAL, oseo_undefined(), oseo_undefined(),
1377
+ OSEO_FUNCTION_NAME_PREFIX_NONE);
1378
+ }
1379
+
1380
+ static OseoResult proxy_revocable(
1381
+ OseoContext *context,
1382
+ size_t argument_count,
1383
+ const OseoValue *arguments
1384
+ ) {
1385
+ OseoValue slots[6] = {
1386
+ argument(argument_count, arguments, 0u),
1387
+ argument(argument_count, arguments, 1u),
1388
+ oseo_undefined(), oseo_undefined(), oseo_undefined(), oseo_undefined(),
1389
+ };
1390
+ OseoRootFrame frame = {NULL, slots, 6u};
1391
+ oseo_roots_push(context, &frame);
1392
+ OseoResult result = proxy_create(context, slots[0], slots[1]);
1393
+ slots[2] = result.value;
1394
+ if (result.status == OSEO_STATUS_NORMAL) {
1395
+ result = oseo_environment_create(context, 1u);
1396
+ slots[3] = result.value;
1397
+ }
1398
+ if (result.status == OSEO_STATUS_NORMAL) {
1399
+ result = oseo_environment_set(context, slots[3], 0u, slots[2]);
1400
+ }
1401
+ if (result.status == OSEO_STATUS_NORMAL) {
1402
+ result = create_builtin(
1403
+ context, "", 0u, OSEO_PROXY_REVOKE_CODE_ID, slots[3]);
1404
+ slots[4] = result.value;
1405
+ }
1406
+ if (result.status == OSEO_STATUS_NORMAL) {
1407
+ result = oseo_object_literal_create(context);
1408
+ slots[5] = result.value;
1409
+ }
1410
+ static const OseoPropertyAttributes attrs = {true, true, true, false};
1411
+ if (result.status == OSEO_STATUS_NORMAL) {
1412
+ result = oseo_internal_ascii_string(context, "proxy");
1413
+ slots[3] = result.value;
1414
+ }
1415
+ if (result.status == OSEO_STATUS_NORMAL) {
1416
+ result = oseo_object_define(
1417
+ context, slots[5], slots[3], slots[2], attrs);
1418
+ }
1419
+ if (result.status == OSEO_STATUS_NORMAL) {
1420
+ result = oseo_internal_ascii_string(context, "revoke");
1421
+ slots[3] = result.value;
1422
+ }
1423
+ if (result.status == OSEO_STATUS_NORMAL) {
1424
+ result = oseo_object_define(
1425
+ context, slots[5], slots[3], slots[4], attrs);
1426
+ }
1427
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[5];
1428
+ oseo_roots_pop(context, &frame);
1429
+ return result;
1430
+ }
1431
+
1432
+ OseoResult oseo_internal_proxy_builtin_dispatch(
1433
+ OseoContext *context,
1434
+ size_t code_id,
1435
+ OseoValue callee,
1436
+ OseoValue receiver,
1437
+ size_t argument_count,
1438
+ const OseoValue *arguments,
1439
+ OseoValue new_target
1440
+ ) {
1441
+ (void)receiver;
1442
+ if (code_id == OSEO_PROXY_REVOKE_CODE_ID) {
1443
+ OseoResult environment = oseo_function_environment(context, callee);
1444
+ if (environment.status != OSEO_STATUS_NORMAL) return environment;
1445
+ OseoResult stored = oseo_environment_get(
1446
+ context, environment.value, 0u);
1447
+ if (stored.status != OSEO_STATUS_NORMAL) return stored;
1448
+ if (tag_of(stored.value) == OSEO_TAG_NULL) {
1449
+ return normal(oseo_undefined());
1450
+ }
1451
+ OseoValue proxy = stored.value;
1452
+ OseoResult cleared = oseo_environment_set(
1453
+ context, environment.value, 0u, oseo_null());
1454
+ if (cleared.status != OSEO_STATUS_NORMAL) return cleared;
1455
+ proxy_object(proxy)->revoked = true;
1456
+ return normal(oseo_undefined());
1457
+ }
1458
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1459
+ if (code_id != OSEO_PROXY_CONSTRUCTOR_CODE_ID) {
1460
+ return type_error(context, "Proxy.revocable is not a constructor.");
1461
+ }
1462
+ return proxy_create(
1463
+ context,
1464
+ argument(argument_count, arguments, 0u),
1465
+ argument(argument_count, arguments, 1u)
1466
+ );
1467
+ }
1468
+ if (code_id == OSEO_PROXY_CONSTRUCTOR_CODE_ID) {
1469
+ return type_error(context, "Proxy must be called with new.");
1470
+ }
1471
+ if (code_id == OSEO_PROXY_REVOCABLE_CODE_ID) {
1472
+ return proxy_revocable(context, argument_count, arguments);
1473
+ }
1474
+ return oseo_unknown_function(context, code_id);
1475
+ }
1476
+
1477
+ OseoResult oseo_internal_proxy_intrinsic(OseoContext *context) {
1478
+ OseoValue *slot = &context->realm->intrinsics[OSEO_INTRINSIC_PROXY];
1479
+ if (is_function(*slot)) return normal(*slot);
1480
+ OseoValue slots[4] = {
1481
+ oseo_undefined(), oseo_undefined(), oseo_undefined(), oseo_undefined(),
1482
+ };
1483
+ OseoRootFrame frame = {NULL, slots, 4u};
1484
+ oseo_roots_push(context, &frame);
1485
+ OseoResult result = oseo_environment_create(context, 0u);
1486
+ slots[0] = result.value;
1487
+ if (result.status == OSEO_STATUS_NORMAL) {
1488
+ result = create_builtin(
1489
+ context, "Proxy", 2u, OSEO_PROXY_CONSTRUCTOR_CODE_ID, slots[0]);
1490
+ slots[1] = result.value;
1491
+ if (result.status == OSEO_STATUS_NORMAL) *slot = slots[1];
1492
+ }
1493
+ if (result.status == OSEO_STATUS_NORMAL) {
1494
+ result = create_builtin(
1495
+ context, "revocable", 2u, OSEO_PROXY_REVOCABLE_CODE_ID, slots[0]);
1496
+ slots[2] = result.value;
1497
+ }
1498
+ if (result.status == OSEO_STATUS_NORMAL) {
1499
+ result = oseo_internal_ascii_string(context, "revocable");
1500
+ slots[3] = result.value;
1501
+ }
1502
+ if (result.status == OSEO_STATUS_NORMAL) {
1503
+ const OseoPropertyAttributes attrs = {true, false, true, false};
1504
+ result = oseo_object_define(
1505
+ context, slots[1], slots[3], slots[2], attrs);
1506
+ }
1507
+ if (result.status != OSEO_STATUS_NORMAL) *slot = oseo_undefined();
1508
+ else result.value = slots[1];
1509
+ oseo_roots_pop(context, &frame);
1510
+ return result;
1511
+ }
1512
+
1513
+ OseoResult oseo_internal_install_proxy_global(
1514
+ OseoContext *context,
1515
+ OseoValue global
1516
+ ) {
1517
+ OseoValue slots[3] = {global, oseo_undefined(), oseo_undefined()};
1518
+ OseoRootFrame frame = {NULL, slots, 3u};
1519
+ oseo_roots_push(context, &frame);
1520
+ OseoResult result = oseo_internal_proxy_intrinsic(context);
1521
+ slots[1] = result.value;
1522
+ if (result.status == OSEO_STATUS_NORMAL) {
1523
+ result = oseo_internal_ascii_string(context, "Proxy");
1524
+ slots[2] = result.value;
1525
+ }
1526
+ if (result.status == OSEO_STATUS_NORMAL) {
1527
+ const OseoPropertyAttributes attrs = {true, false, true, false};
1528
+ result = oseo_object_define(
1529
+ context, slots[0], slots[2], slots[1], attrs);
1530
+ }
1531
+ oseo_roots_pop(context, &frame);
1532
+ return result;
1533
+ }