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