@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,3643 @@
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 <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /*
12
+ * Function creation, callable metadata, construction, and
13
+ * generic dispatch.
14
+ */
15
+
16
+ typedef struct {
17
+ size_t first_code_id;
18
+ size_t last_code_id;
19
+ OseoBuiltinDispatcher dispatch;
20
+ } OseoBuiltinDispatchRange;
21
+
22
+ static OseoResult function_prototype_apply(
23
+ OseoContext *context,
24
+ OseoValue target,
25
+ size_t argument_count,
26
+ const OseoValue *arguments
27
+ );
28
+ static OseoResult function_prototype_bind(
29
+ OseoContext *context,
30
+ OseoValue target,
31
+ size_t argument_count,
32
+ const OseoValue *arguments
33
+ );
34
+ static OseoResult function_prototype_call(
35
+ OseoContext *context,
36
+ OseoValue target,
37
+ size_t argument_count,
38
+ const OseoValue *arguments
39
+ );
40
+ static OseoResult function_prototype_to_string(
41
+ OseoContext *context,
42
+ OseoValue target
43
+ );
44
+ OseoResult oseo_internal_ordinary_has_instance(
45
+ OseoContext *context,
46
+ OseoValue target,
47
+ OseoValue value
48
+ );
49
+
50
+ OseoResult oseo_internal_function_builtin_dispatch(
51
+ OseoContext *context,
52
+ size_t code_id,
53
+ OseoValue callee,
54
+ OseoValue receiver,
55
+ size_t argument_count,
56
+ const OseoValue *arguments,
57
+ OseoValue new_target
58
+ ) {
59
+ (void)callee;
60
+ if (code_id == OSEO_FUNCTION_PROTOTYPE_CODE_ID) {
61
+ return normal(oseo_undefined());
62
+ }
63
+ /* ADR 0016 keeps every form that compiles source text at run time
64
+ * outside the profile, so reaching %Function%'s [[Call]] or
65
+ * [[Construct]] reports that boundary, as the other dynamic-source
66
+ * constructors do. */
67
+ if (code_id == OSEO_FUNCTION_CONSTRUCTOR_CODE_ID) {
68
+ return failure(
69
+ context,
70
+ "OSEO1001",
71
+ "Function compiles source text at run time, which is outside "
72
+ "the admitted profile."
73
+ );
74
+ }
75
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
76
+ return oseo_internal_throw_error(
77
+ context,
78
+ OSEO_ERROR_TYPE,
79
+ "Function prototype method is not a constructor."
80
+ );
81
+ }
82
+ if (code_id == OSEO_FUNCTION_APPLY_CODE_ID) {
83
+ return function_prototype_apply(
84
+ context,
85
+ receiver,
86
+ argument_count,
87
+ arguments
88
+ );
89
+ }
90
+ if (code_id == OSEO_FUNCTION_BIND_CODE_ID) {
91
+ return function_prototype_bind(
92
+ context,
93
+ receiver,
94
+ argument_count,
95
+ arguments
96
+ );
97
+ }
98
+ if (code_id == OSEO_FUNCTION_CALL_CODE_ID) {
99
+ return function_prototype_call(
100
+ context,
101
+ receiver,
102
+ argument_count,
103
+ arguments
104
+ );
105
+ }
106
+ if (code_id == OSEO_FUNCTION_TO_STRING_CODE_ID) {
107
+ return function_prototype_to_string(context, receiver);
108
+ }
109
+ if (code_id == OSEO_FUNCTION_HAS_INSTANCE_CODE_ID) {
110
+ OseoValue value = argument_count == 0u
111
+ ? oseo_undefined()
112
+ : arguments[0];
113
+ return oseo_internal_ordinary_has_instance(context, receiver, value);
114
+ }
115
+ return oseo_unknown_function(context, code_id);
116
+ }
117
+
118
+ static const OseoBuiltinDispatchRange builtin_dispatch_ranges[] = {
119
+ {OSEO_PROMISE_CODE_ID_RANGE_FIRST,
120
+ OSEO_PROMISE_CODE_ID_RANGE_LAST,
121
+ oseo_internal_promise_builtin_dispatch},
122
+ {OSEO_ERROR_CODE_ID_RANGE_FIRST,
123
+ OSEO_ERROR_CODE_ID_RANGE_LAST,
124
+ oseo_internal_error_builtin_dispatch},
125
+ {OSEO_SYMBOL_CODE_ID_RANGE_FIRST,
126
+ OSEO_SYMBOL_CODE_ID_RANGE_LAST,
127
+ oseo_internal_symbol_builtin_dispatch},
128
+ {OSEO_ITERATOR_CODE_ID_RANGE_FIRST,
129
+ OSEO_ITERATOR_CODE_ID_RANGE_LAST,
130
+ oseo_internal_iterator_builtin_dispatch},
131
+ {OSEO_GENERATOR_CODE_ID_RANGE_FIRST,
132
+ OSEO_GENERATOR_CODE_ID_RANGE_LAST,
133
+ oseo_internal_generator_builtin_dispatch},
134
+ {OSEO_ASYNC_GENERATOR_CODE_ID_RANGE_FIRST,
135
+ OSEO_ASYNC_GENERATOR_CODE_ID_RANGE_LAST,
136
+ oseo_internal_async_generator_builtin_dispatch},
137
+ {OSEO_ARRAY_CODE_ID_RANGE_FIRST,
138
+ OSEO_ARRAY_CODE_ID_RANGE_LAST,
139
+ oseo_internal_array_builtin_dispatch},
140
+ {OSEO_ARGUMENTS_CODE_ID_RANGE_FIRST,
141
+ OSEO_ARGUMENTS_CODE_ID_RANGE_LAST,
142
+ oseo_internal_arguments_builtin_dispatch},
143
+ {OSEO_FUNCTION_CODE_ID_RANGE_FIRST,
144
+ OSEO_FUNCTION_CODE_ID_RANGE_LAST,
145
+ oseo_internal_function_builtin_dispatch},
146
+ {OSEO_OBJECT_CODE_ID_RANGE_FIRST,
147
+ OSEO_OBJECT_CODE_ID_RANGE_LAST,
148
+ oseo_internal_object_builtin_dispatch},
149
+ {OSEO_NUMBER_CODE_ID_RANGE_FIRST,
150
+ OSEO_NUMBER_CODE_ID_RANGE_LAST,
151
+ oseo_internal_number_builtin_dispatch},
152
+ {OSEO_ARRAY_BUFFER_CODE_ID_RANGE_FIRST,
153
+ OSEO_ARRAY_BUFFER_CODE_ID_RANGE_LAST,
154
+ oseo_internal_array_buffer_builtin_dispatch},
155
+ {OSEO_STRING_CODE_ID_RANGE_FIRST,
156
+ OSEO_STRING_CODE_ID_RANGE_LAST,
157
+ oseo_internal_string_builtin_dispatch},
158
+ {OSEO_MAP_CODE_ID_RANGE_FIRST,
159
+ OSEO_MAP_CODE_ID_RANGE_LAST,
160
+ oseo_internal_map_builtin_dispatch},
161
+ {OSEO_BIGINT_CODE_ID_RANGE_FIRST,
162
+ OSEO_BIGINT_CODE_ID_RANGE_LAST,
163
+ oseo_internal_bigint_builtin_dispatch},
164
+ {OSEO_DATA_VIEW_CODE_ID_RANGE_FIRST,
165
+ OSEO_DATA_VIEW_CODE_ID_RANGE_LAST,
166
+ oseo_internal_data_view_builtin_dispatch},
167
+ {OSEO_REGEXP_CODE_ID_RANGE_FIRST,
168
+ OSEO_REGEXP_CODE_ID_RANGE_LAST,
169
+ oseo_internal_regexp_builtin_dispatch},
170
+ {OSEO_MATH_CODE_ID_RANGE_FIRST,
171
+ OSEO_MATH_CODE_ID_RANGE_LAST,
172
+ oseo_internal_math_builtin_dispatch},
173
+ {OSEO_URI_CODE_ID_RANGE_FIRST,
174
+ OSEO_URI_CODE_ID_RANGE_LAST,
175
+ oseo_internal_uri_builtin_dispatch},
176
+ {OSEO_REFLECT_CODE_ID_RANGE_FIRST,
177
+ OSEO_REFLECT_CODE_ID_RANGE_LAST,
178
+ oseo_internal_reflect_builtin_dispatch},
179
+ {OSEO_PROXY_CODE_ID_RANGE_FIRST,
180
+ OSEO_PROXY_CODE_ID_RANGE_LAST,
181
+ oseo_internal_proxy_builtin_dispatch},
182
+ {OSEO_DATE_CODE_ID_RANGE_FIRST,
183
+ OSEO_DATE_CODE_ID_RANGE_LAST,
184
+ oseo_internal_date_builtin_dispatch},
185
+ {OSEO_JSON_CODE_ID_RANGE_FIRST,
186
+ OSEO_JSON_CODE_ID_RANGE_LAST,
187
+ oseo_internal_json_builtin_dispatch},
188
+ {OSEO_SET_CODE_ID_RANGE_FIRST,
189
+ OSEO_SET_CODE_ID_RANGE_LAST,
190
+ oseo_internal_set_builtin_dispatch},
191
+ {OSEO_TYPED_ARRAY_CODE_ID_RANGE_FIRST,
192
+ OSEO_TYPED_ARRAY_CODE_ID_RANGE_LAST,
193
+ oseo_internal_typed_array_builtin_dispatch},
194
+ {OSEO_ATOMICS_CODE_ID_RANGE_FIRST,
195
+ OSEO_ATOMICS_CODE_ID_RANGE_LAST,
196
+ oseo_internal_atomics_builtin_dispatch},
197
+ {OSEO_WEAK_COLLECTION_CODE_ID_RANGE_FIRST,
198
+ OSEO_WEAK_COLLECTION_CODE_ID_RANGE_LAST,
199
+ oseo_internal_weak_collection_builtin_dispatch},
200
+ {OSEO_BOOLEAN_CODE_ID_RANGE_FIRST,
201
+ OSEO_BOOLEAN_CODE_ID_RANGE_LAST,
202
+ oseo_internal_boolean_builtin_dispatch},
203
+ {OSEO_AGENT_CODE_ID_RANGE_FIRST,
204
+ OSEO_AGENT_CODE_ID_RANGE_LAST,
205
+ oseo_internal_agent_builtin_dispatch},
206
+ {OSEO_REALM_CODE_ID_RANGE_FIRST,
207
+ OSEO_REALM_CODE_ID_RANGE_LAST,
208
+ oseo_internal_realm_builtin_dispatch},
209
+ {OSEO_PROCESS_CODE_ID_RANGE_FIRST,
210
+ OSEO_PROCESS_CODE_ID_RANGE_LAST,
211
+ oseo_internal_process_builtin_dispatch},
212
+ {OSEO_DISPOSAL_CODE_ID_RANGE_FIRST,
213
+ OSEO_DISPOSAL_CODE_ID_RANGE_LAST,
214
+ oseo_internal_disposal_builtin_dispatch},
215
+ };
216
+
217
+ static OseoBuiltinDispatcher builtin_dispatcher(size_t code_id) {
218
+ size_t count =
219
+ sizeof(builtin_dispatch_ranges) / sizeof(builtin_dispatch_ranges[0]);
220
+ for (size_t index = 0u; index < count; index += 1u) {
221
+ const OseoBuiltinDispatchRange *range =
222
+ &builtin_dispatch_ranges[index];
223
+ if (code_id >= range->first_code_id &&
224
+ code_id <= range->last_code_id) {
225
+ return range->dispatch;
226
+ }
227
+ }
228
+ return NULL;
229
+ }
230
+
231
+ bool oseo_internal_builtin_code_id(size_t code_id) {
232
+ return builtin_dispatcher(code_id) != NULL;
233
+ }
234
+
235
+ static bool is_argument_list(OseoValue value) {
236
+ return tag_of(value) == OSEO_TAG_HEAP &&
237
+ heap_object(value)->kind == OSEO_HEAP_ARGUMENT_LIST;
238
+ }
239
+
240
+ static OseoArgumentList *argument_list(OseoValue value) {
241
+ return (OseoArgumentList *)heap_object(value);
242
+ }
243
+
244
+ OseoResult oseo_argument_list_create(OseoContext *context) {
245
+ OseoArgumentList *list =
246
+ oseo_internal_allocate_heap_bytes(context, sizeof(*list));
247
+ if (list == NULL) {
248
+ return failure(context, "OSEO2001", "Argument list allocation failed.");
249
+ }
250
+ list->values = NULL;
251
+ list->length = 0u;
252
+ list->capacity = 0u;
253
+ return oseo_internal_publish_heap(
254
+ context,
255
+ &list->header,
256
+ OSEO_HEAP_ARGUMENT_LIST
257
+ );
258
+ }
259
+
260
+ static OseoResult grow_argument_list(
261
+ OseoContext *context,
262
+ OseoValue list_value
263
+ ) {
264
+ OseoArgumentList *list = argument_list(list_value);
265
+ if (list->length < list->capacity) return normal(list_value);
266
+ size_t capacity = list->capacity == 0u ? 4u : list->capacity * 2u;
267
+ if (capacity < list->capacity ||
268
+ capacity > SIZE_MAX / sizeof(OseoValue)) {
269
+ return failure(
270
+ context,
271
+ "OSEO2001",
272
+ "Argument list storage is too large."
273
+ );
274
+ }
275
+ if (context->collect_every_safepoint) oseo_collect(context);
276
+ list = argument_list(list_value);
277
+ context->allocation_attempts += 1u;
278
+ if (context->fail_allocation_at != 0u &&
279
+ context->allocation_attempts == context->fail_allocation_at) {
280
+ return failure(
281
+ context,
282
+ "OSEO2001",
283
+ "Argument list allocation failed."
284
+ );
285
+ }
286
+ OseoValue *values = malloc(capacity * sizeof(*values));
287
+ if (values == NULL) {
288
+ return failure(
289
+ context,
290
+ "OSEO2001",
291
+ "Argument list allocation failed."
292
+ );
293
+ }
294
+ if (list->length > 0u) {
295
+ memcpy(values, list->values, list->length * sizeof(*values));
296
+ }
297
+ free(list->values);
298
+ list->values = values;
299
+ list->capacity = capacity;
300
+ return normal(list_value);
301
+ }
302
+
303
+ OseoResult oseo_argument_list_append(
304
+ OseoContext *context,
305
+ OseoValue list_value,
306
+ OseoValue value
307
+ ) {
308
+ if (!is_argument_list(list_value)) {
309
+ return oseo_internal_throw_error(
310
+ context,
311
+ OSEO_ERROR_TYPE,
312
+ "Argument list append requires a list."
313
+ );
314
+ }
315
+ OseoResult grown = grow_argument_list(context, list_value);
316
+ if (grown.status != OSEO_STATUS_NORMAL) return grown;
317
+ OseoArgumentList *list = argument_list(list_value);
318
+ list->values[list->length] = value;
319
+ list->length += 1u;
320
+ return normal(list_value);
321
+ }
322
+
323
+ OseoResult oseo_argument_list_view(
324
+ OseoContext *context,
325
+ OseoValue list_value,
326
+ size_t *count,
327
+ const OseoValue **arguments
328
+ ) {
329
+ if (!is_argument_list(list_value)) {
330
+ return oseo_internal_throw_error(
331
+ context,
332
+ OSEO_ERROR_TYPE,
333
+ "Argument list view requires a list."
334
+ );
335
+ }
336
+ OseoArgumentList *list = argument_list(list_value);
337
+ *count = list->length;
338
+ *arguments = list->values;
339
+ return normal(list_value);
340
+ }
341
+
342
+ static OseoResult require_callable(
343
+ OseoContext *context,
344
+ OseoValue value
345
+ ) {
346
+ if (is_callable(value)) return normal(value);
347
+ return oseo_internal_throw_error(
348
+ context,
349
+ OSEO_ERROR_TYPE,
350
+ "Function prototype method requires a callable receiver."
351
+ );
352
+ }
353
+
354
+ static OseoResult array_like_length(
355
+ OseoContext *context,
356
+ OseoValue value,
357
+ double *length
358
+ ) {
359
+ *length = 0.0;
360
+ if (!is_object(value)) {
361
+ return oseo_internal_throw_error(
362
+ context,
363
+ OSEO_ERROR_TYPE,
364
+ "An array-like argument list must be an object."
365
+ );
366
+ }
367
+ if (is_array(value)) {
368
+ *length = (double)ordinary_object(value)->array_length;
369
+ return normal(value);
370
+ }
371
+ OseoValue slots[2] = {value, oseo_undefined()};
372
+ OseoRootFrame frame = {NULL, slots, 2u};
373
+ oseo_roots_push(context, &frame);
374
+ OseoResult result = oseo_internal_ascii_string(context, "length");
375
+ slots[1] = result.value;
376
+ if (result.status == OSEO_STATUS_NORMAL) {
377
+ result = oseo_object_get(context, slots[0], slots[1]);
378
+ }
379
+ if (result.status == OSEO_STATUS_NORMAL) {
380
+ result = oseo_internal_to_number(context, result.value);
381
+ }
382
+ oseo_roots_pop(context, &frame);
383
+ if (result.status != OSEO_STATUS_NORMAL) return result;
384
+ double number = number_value(result.value);
385
+ if (!(number > 0.0)) return normal(value);
386
+ number = floor(number);
387
+ *length = number > 9007199254740991.0
388
+ ? 9007199254740991.0
389
+ : number;
390
+ return normal(value);
391
+ }
392
+
393
+ OseoResult oseo_internal_array_like_list(
394
+ OseoContext *context,
395
+ OseoValue source,
396
+ OseoValue *list
397
+ ) {
398
+ OseoValue slots[3] = {source, oseo_undefined(), oseo_undefined()};
399
+ OseoRootFrame frame = {NULL, slots, 3u};
400
+ oseo_roots_push(context, &frame);
401
+ double length = 0.0;
402
+ OseoResult result = array_like_length(context, slots[0], &length);
403
+ if (result.status == OSEO_STATUS_NORMAL && length > (double)SIZE_MAX) {
404
+ result = failure(
405
+ context,
406
+ "OSEO2001",
407
+ "An array-like argument list is too large."
408
+ );
409
+ }
410
+ if (result.status == OSEO_STATUS_NORMAL) {
411
+ result = oseo_argument_list_create(context);
412
+ slots[1] = result.value;
413
+ }
414
+ for (size_t index = 0u;
415
+ result.status == OSEO_STATUS_NORMAL && (double)index < length;
416
+ index += 1u) {
417
+ result = oseo_property_key(context, oseo_number((double)index));
418
+ slots[2] = result.value;
419
+ if (result.status == OSEO_STATUS_NORMAL) {
420
+ result = oseo_object_get(context, slots[0], slots[2]);
421
+ slots[2] = result.value;
422
+ }
423
+ if (result.status == OSEO_STATUS_NORMAL) {
424
+ result = oseo_argument_list_append(
425
+ context,
426
+ slots[1],
427
+ slots[2]
428
+ );
429
+ }
430
+ }
431
+ if (result.status == OSEO_STATUS_NORMAL) *list = slots[1];
432
+ oseo_roots_pop(context, &frame);
433
+ return result;
434
+ }
435
+
436
+ static OseoResult function_prototype_call(
437
+ OseoContext *context,
438
+ OseoValue target,
439
+ size_t argument_count,
440
+ const OseoValue *arguments
441
+ ) {
442
+ OseoResult callable = require_callable(context, target);
443
+ if (callable.status != OSEO_STATUS_NORMAL) return callable;
444
+ OseoValue receiver = argument_count == 0u
445
+ ? oseo_undefined()
446
+ : arguments[0];
447
+ size_t forwarded_count = argument_count == 0u
448
+ ? 0u
449
+ : argument_count - 1u;
450
+ const OseoValue *forwarded = forwarded_count == 0u
451
+ ? NULL
452
+ : arguments + 1u;
453
+ /* Function.prototype.call step 2 performs PrepareForTailCall. */
454
+ return oseo_internal_tail_call(
455
+ context,
456
+ target,
457
+ receiver,
458
+ forwarded_count,
459
+ forwarded
460
+ );
461
+ }
462
+
463
+ static OseoResult function_prototype_apply(
464
+ OseoContext *context,
465
+ OseoValue target,
466
+ size_t argument_count,
467
+ const OseoValue *arguments
468
+ ) {
469
+ OseoResult callable = require_callable(context, target);
470
+ if (callable.status != OSEO_STATUS_NORMAL) return callable;
471
+ OseoValue receiver = argument_count == 0u
472
+ ? oseo_undefined()
473
+ : arguments[0];
474
+ OseoValue source = argument_count < 2u
475
+ ? oseo_undefined()
476
+ : arguments[1];
477
+ /* Both Function.prototype.apply branches perform PrepareForTailCall
478
+ * once the argument list is complete. */
479
+ if (is_nullish(source)) {
480
+ return oseo_internal_tail_call(context, target, receiver, 0u, NULL);
481
+ }
482
+ OseoValue slots[4] = {
483
+ target,
484
+ receiver,
485
+ source,
486
+ oseo_undefined(),
487
+ };
488
+ OseoRootFrame frame = {NULL, slots, 4u};
489
+ oseo_roots_push(context, &frame);
490
+ OseoResult result = oseo_internal_array_like_list(
491
+ context,
492
+ slots[2],
493
+ &slots[3]
494
+ );
495
+ size_t forwarded_count = 0u;
496
+ const OseoValue *forwarded = NULL;
497
+ if (result.status == OSEO_STATUS_NORMAL) {
498
+ result = oseo_argument_list_view(
499
+ context,
500
+ slots[3],
501
+ &forwarded_count,
502
+ &forwarded
503
+ );
504
+ }
505
+ if (result.status == OSEO_STATUS_NORMAL) {
506
+ result = oseo_internal_tail_call(
507
+ context,
508
+ slots[0],
509
+ slots[1],
510
+ forwarded_count,
511
+ forwarded
512
+ );
513
+ }
514
+ oseo_roots_pop(context, &frame);
515
+ return result;
516
+ }
517
+
518
+ static OseoResult bound_function_name(
519
+ OseoContext *context,
520
+ OseoValue name
521
+ ) {
522
+ size_t base_length = is_string(name) ? string_object(name)->length : 0u;
523
+ if (base_length > SIZE_MAX / sizeof(uint16_t) - 6u) {
524
+ return failure(context, "OSEO2001", "Bound name is too large.");
525
+ }
526
+ size_t length = base_length + 6u;
527
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
528
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
529
+ uint16_t *units = malloc(length * sizeof(*units));
530
+ if (units == NULL) {
531
+ return failure(context, "OSEO2001", "Bound name allocation failed.");
532
+ }
533
+ static const char prefix[] = "bound ";
534
+ for (size_t index = 0u; index < 6u; index += 1u) {
535
+ units[index] = (uint16_t)(unsigned char)prefix[index];
536
+ }
537
+ if (base_length > 0u) {
538
+ memcpy(
539
+ units + 6u,
540
+ string_object(name)->units,
541
+ base_length * sizeof(*units)
542
+ );
543
+ }
544
+ OseoValue rooted = name;
545
+ OseoRootFrame frame = {NULL, &rooted, 1u};
546
+ oseo_roots_push(context, &frame);
547
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
548
+ oseo_roots_pop(context, &frame);
549
+ free(units);
550
+ return result;
551
+ }
552
+
553
+ static OseoResult function_prototype_bind(
554
+ OseoContext *context,
555
+ OseoValue target,
556
+ size_t argument_count,
557
+ const OseoValue *arguments
558
+ ) {
559
+ OseoResult result = require_callable(context, target);
560
+ if (result.status != OSEO_STATUS_NORMAL) return result;
561
+ OseoRootFrame frame = {NULL, NULL, 0u};
562
+ result = oseo_roots_allocate(context, &frame, 8u);
563
+ if (result.status != OSEO_STATUS_NORMAL) return result;
564
+ frame.slots[0] = target;
565
+ frame.slots[1] = argument_count == 0u
566
+ ? oseo_undefined()
567
+ : arguments[0];
568
+ result = oseo_internal_get_prototype(context, frame.slots[0]);
569
+ frame.slots[7] = result.value;
570
+ if (result.status == OSEO_STATUS_NORMAL) {
571
+ result = oseo_argument_list_create(context);
572
+ }
573
+ frame.slots[2] = result.value;
574
+ for (size_t index = 1u;
575
+ result.status == OSEO_STATUS_NORMAL && index < argument_count;
576
+ index += 1u) {
577
+ result = oseo_argument_list_append(
578
+ context,
579
+ frame.slots[2],
580
+ arguments[index]
581
+ );
582
+ }
583
+ OseoValue length_value = oseo_number(0.0);
584
+ if (result.status == OSEO_STATUS_NORMAL) {
585
+ result = oseo_internal_ascii_string(context, "length");
586
+ frame.slots[3] = result.value;
587
+ }
588
+ if (result.status == OSEO_STATUS_NORMAL) {
589
+ result = oseo_object_has_own(context, frame.slots[0], frame.slots[3]);
590
+ }
591
+ if (result.status == OSEO_STATUS_NORMAL && oseo_to_boolean(result.value)) {
592
+ result = oseo_object_get(context, frame.slots[0], frame.slots[3]);
593
+ if (result.status == OSEO_STATUS_NORMAL && is_number(result.value)) {
594
+ double target_length = number_value(result.value);
595
+ double bound_count = argument_count == 0u
596
+ ? 0.0
597
+ : (double)(argument_count - 1u);
598
+ double bound_length;
599
+ if (isinf(target_length) && target_length > 0.0) {
600
+ bound_length = INFINITY;
601
+ } else if (!isfinite(target_length)) {
602
+ bound_length = 0.0;
603
+ } else {
604
+ double integer = trunc(target_length);
605
+ bound_length = integer > bound_count
606
+ ? integer - bound_count
607
+ : 0.0;
608
+ }
609
+ length_value = oseo_number(bound_length);
610
+ }
611
+ }
612
+ if (result.status == OSEO_STATUS_NORMAL) {
613
+ result = oseo_internal_ascii_string(context, "name");
614
+ frame.slots[3] = result.value;
615
+ }
616
+ if (result.status == OSEO_STATUS_NORMAL) {
617
+ result = oseo_object_get(context, frame.slots[0], frame.slots[3]);
618
+ frame.slots[4] = result.value;
619
+ }
620
+ if (result.status == OSEO_STATUS_NORMAL) {
621
+ result = bound_function_name(context, frame.slots[4]);
622
+ frame.slots[4] = result.value;
623
+ }
624
+ if (result.status == OSEO_STATUS_NORMAL) {
625
+ result = oseo_environment_create(context, 0u);
626
+ frame.slots[5] = result.value;
627
+ }
628
+ if (result.status == OSEO_STATUS_NORMAL) {
629
+ OseoString *name = string_object(frame.slots[4]);
630
+ result = oseo_function_create(
631
+ context,
632
+ OSEO_FUNCTION_BIND_CODE_ID,
633
+ frame.slots[5],
634
+ name->units,
635
+ name->length,
636
+ 0u,
637
+ OSEO_FUNCTION_BOUND,
638
+ oseo_undefined(),
639
+ oseo_undefined(),
640
+ OSEO_FUNCTION_NAME_PREFIX_NONE
641
+ );
642
+ frame.slots[6] = result.value;
643
+ }
644
+ if (result.status == OSEO_STATUS_NORMAL) {
645
+ result = oseo_internal_ascii_string(context, "length");
646
+ frame.slots[3] = result.value;
647
+ }
648
+ if (result.status == OSEO_STATUS_NORMAL) {
649
+ const OseoPropertyAttributes attributes = {
650
+ true,
651
+ false,
652
+ false,
653
+ false,
654
+ };
655
+ result = oseo_object_define(
656
+ context,
657
+ frame.slots[6],
658
+ frame.slots[3],
659
+ length_value,
660
+ attributes
661
+ );
662
+ }
663
+ if (result.status == OSEO_STATUS_NORMAL) {
664
+ OseoFunction *bound = function_object(frame.slots[6]);
665
+ bound->ordinary.prototype = frame.slots[7];
666
+ bound->bound_target = frame.slots[0];
667
+ bound->bound_this = frame.slots[1];
668
+ bound->bound_arguments = frame.slots[2];
669
+ result = normal(frame.slots[6]);
670
+ }
671
+ oseo_roots_release(context, &frame);
672
+ return result;
673
+ }
674
+
675
+ static OseoResult native_function_text(
676
+ OseoContext *context,
677
+ OseoValue target
678
+ ) {
679
+ OseoValue slots[2] = {target, oseo_undefined()};
680
+ OseoRootFrame frame = {NULL, slots, 2u};
681
+ oseo_roots_push(context, &frame);
682
+ slots[1] = function_object(slots[0])->initial_name;
683
+ OseoResult result = normal(slots[1]);
684
+ size_t name_length = is_string(slots[1])
685
+ ? string_object(slots[1])->length
686
+ : 0u;
687
+ if (function_object(slots[0])->function_kind == OSEO_FUNCTION_BOUND) {
688
+ name_length = 0u;
689
+ }
690
+ static const char prefix[] = "function ";
691
+ static const char suffix[] = "() { [native code] }";
692
+ size_t length = 9u + name_length + 20u;
693
+ uint16_t *units = NULL;
694
+ if (result.status == OSEO_STATUS_NORMAL) {
695
+ result = oseo_internal_validate_string_length(context, length);
696
+ }
697
+ if (result.status == OSEO_STATUS_NORMAL) {
698
+ units = malloc(length * sizeof(*units));
699
+ if (units == NULL) {
700
+ result = failure(
701
+ context,
702
+ "OSEO2001",
703
+ "Native function text allocation failed."
704
+ );
705
+ }
706
+ }
707
+ if (result.status == OSEO_STATUS_NORMAL) {
708
+ for (size_t index = 0u; index < 9u; index += 1u) {
709
+ units[index] = (uint16_t)(unsigned char)prefix[index];
710
+ }
711
+ if (name_length > 0u) {
712
+ memcpy(
713
+ units + 9u,
714
+ string_object(slots[1])->units,
715
+ name_length * sizeof(*units)
716
+ );
717
+ }
718
+ for (size_t index = 0u; index < 20u; index += 1u) {
719
+ units[9u + name_length + index] =
720
+ (uint16_t)(unsigned char)suffix[index];
721
+ }
722
+ result = oseo_internal_allocate_string(context, units, length);
723
+ }
724
+ free(units);
725
+ oseo_roots_pop(context, &frame);
726
+ return result;
727
+ }
728
+
729
+ static OseoResult function_prototype_to_string(
730
+ OseoContext *context,
731
+ OseoValue target
732
+ ) {
733
+ OseoResult callable = require_callable(context, target);
734
+ if (callable.status != OSEO_STATUS_NORMAL) return callable;
735
+ if (is_proxy(target)) {
736
+ return oseo_internal_ascii_string(
737
+ context, "function () { [native code] }");
738
+ }
739
+ OseoValue source = function_object(target)->source_text;
740
+ if (is_string(source)) return normal(source);
741
+ return native_function_text(context, target);
742
+ }
743
+
744
+ /*
745
+ * SetFunctionName's "get "/"set " prefix, applied to whatever base
746
+ * name the static or computed key path already resolved, mirroring
747
+ * oseo_internal_symbol_name's bracket-wrapping for a symbol key.
748
+ */
749
+ static OseoResult accessor_function_name(
750
+ OseoContext *context,
751
+ OseoValue base_name,
752
+ bool setter
753
+ ) {
754
+ size_t base_length = string_object(base_name)->length;
755
+ if (base_length > SIZE_MAX / sizeof(uint16_t) - 4u) {
756
+ return failure(context, "OSEO2001", "String allocation is too large.");
757
+ }
758
+ size_t length = base_length + 4u;
759
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
760
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
761
+ uint16_t *units = malloc(length * sizeof(uint16_t));
762
+ if (units == NULL) {
763
+ return failure(context, "OSEO2001", "String allocation failed.");
764
+ }
765
+ static const char get_prefix[] = "get ";
766
+ static const char set_prefix[] = "set ";
767
+ const char *prefix = setter ? set_prefix : get_prefix;
768
+ for (size_t index = 0u; index < 4u; index += 1u) {
769
+ units[index] = (uint16_t)(unsigned char)prefix[index];
770
+ }
771
+ if (base_length > 0u) {
772
+ memcpy(
773
+ units + 4u,
774
+ string_object(base_name)->units,
775
+ base_length * sizeof(uint16_t)
776
+ );
777
+ }
778
+ OseoValue slots[1] = {base_name};
779
+ OseoRootFrame frame = {NULL, slots, 1u};
780
+ oseo_roots_push(context, &frame);
781
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
782
+ oseo_roots_pop(context, &frame);
783
+ free(units);
784
+ return result;
785
+ }
786
+
787
+ /*
788
+ * Build the two roots every ordinary intrinsic chain reaches. The
789
+ * bootstrap Function.prototype call skips this helper once, then the
790
+ * completed pair is published together so no partial graph is observable.
791
+ */
792
+ static OseoResult intrinsic_graph_root(OseoContext *context) {
793
+ OseoValue object_prototype =
794
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_PROTOTYPE];
795
+ OseoValue function_prototype =
796
+ context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION_PROTOTYPE];
797
+ if (is_object(object_prototype) && is_function(function_prototype)) {
798
+ return normal(object_prototype);
799
+ }
800
+ size_t entry_allocations = context->allocations;
801
+ OseoRootFrame frame = {NULL, NULL, 0u};
802
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
803
+ if (result.status != OSEO_STATUS_NORMAL) return result;
804
+ result = oseo_object_create(context, oseo_null());
805
+ frame.slots[0] = result.value;
806
+ if (result.status == OSEO_STATUS_NORMAL) {
807
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_PROTOTYPE] =
808
+ frame.slots[0];
809
+ /* %Object.prototype% is an immutable prototype exotic object
810
+ * (10.4.7), so its [[SetPrototypeOf]] accepts only the null it
811
+ * already has. */
812
+ ordinary_object(frame.slots[0])->immutable_prototype = true;
813
+ result = oseo_function_create(
814
+ context,
815
+ OSEO_FUNCTION_PROTOTYPE_CODE_ID,
816
+ oseo_undefined(),
817
+ NULL,
818
+ 0u,
819
+ 0u,
820
+ OSEO_FUNCTION_INTERNAL,
821
+ oseo_undefined(),
822
+ oseo_undefined(),
823
+ OSEO_FUNCTION_NAME_PREFIX_NONE
824
+ );
825
+ frame.slots[1] = result.value;
826
+ }
827
+ if (result.status == OSEO_STATUS_NORMAL) {
828
+ context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION_PROTOTYPE] =
829
+ frame.slots[1];
830
+ result = normal(frame.slots[0]);
831
+ if (context->observe_specialization) {
832
+ context->allocations = entry_allocations;
833
+ }
834
+ } else {
835
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_PROTOTYPE] =
836
+ oseo_undefined();
837
+ }
838
+ oseo_roots_release(context, &frame);
839
+ return result;
840
+ }
841
+
842
+ static OseoResult create_function_builtin(
843
+ OseoContext *context,
844
+ size_t code_id,
845
+ const char *name,
846
+ size_t length,
847
+ OseoFunctionKind kind
848
+ ) {
849
+ size_t name_length = strlen(name);
850
+ if (name_length > 31u) {
851
+ return failure(context, "OSEO2001", "Built-in name is too long.");
852
+ }
853
+ uint16_t units[31];
854
+ for (size_t index = 0u; index < name_length; index += 1u) {
855
+ units[index] = (uint16_t)(unsigned char)name[index];
856
+ }
857
+ OseoValue environment = oseo_undefined();
858
+ OseoRootFrame frame = {NULL, &environment, 1u};
859
+ oseo_roots_push(context, &frame);
860
+ OseoResult result = oseo_environment_create(context, 0u);
861
+ environment = result.value;
862
+ if (result.status == OSEO_STATUS_NORMAL) {
863
+ result = oseo_function_create(
864
+ context,
865
+ code_id,
866
+ environment,
867
+ units,
868
+ name_length,
869
+ length,
870
+ kind,
871
+ oseo_undefined(),
872
+ oseo_undefined(),
873
+ OSEO_FUNCTION_NAME_PREFIX_NONE
874
+ );
875
+ }
876
+ oseo_roots_pop(context, &frame);
877
+ return result;
878
+ }
879
+
880
+ static OseoResult function_prototype_intrinsic(OseoContext *context) {
881
+ OseoValue prototype =
882
+ context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION_PROTOTYPE];
883
+ OseoValue *marker =
884
+ &context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION_HAS_INSTANCE];
885
+ if (is_function(*marker)) return normal(prototype);
886
+ if (is_object(*marker)) return normal(prototype);
887
+ if (!is_function(prototype)) {
888
+ return failure(
889
+ context,
890
+ "OSEO2001",
891
+ "Function prototype is unavailable."
892
+ );
893
+ }
894
+ size_t entry_allocations = context->allocations;
895
+ *marker = prototype;
896
+ OseoRootFrame frame = {NULL, NULL, 0u};
897
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
898
+ if (result.status != OSEO_STATUS_NORMAL) {
899
+ *marker = oseo_undefined();
900
+ return result;
901
+ }
902
+ frame.slots[0] = prototype;
903
+ result = create_function_builtin(
904
+ context,
905
+ OSEO_FUNCTION_CONSTRUCTOR_CODE_ID,
906
+ "Function",
907
+ 1u,
908
+ OSEO_FUNCTION_ORDINARY
909
+ );
910
+ frame.slots[1] = result.value;
911
+ if (result.status == OSEO_STATUS_NORMAL) {
912
+ context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION] = frame.slots[1];
913
+ OseoFunction *constructor = function_object(frame.slots[1]);
914
+ constructor->prototype_object = frame.slots[0];
915
+ constructor->prototype_writable = false;
916
+ }
917
+ static const OseoIntrinsic intrinsics[] = {
918
+ OSEO_INTRINSIC_FUNCTION_APPLY,
919
+ OSEO_INTRINSIC_FUNCTION_BIND,
920
+ OSEO_INTRINSIC_FUNCTION_CALL,
921
+ OSEO_INTRINSIC_FUNCTION_TO_STRING,
922
+ OSEO_INTRINSIC_FUNCTION_HAS_INSTANCE,
923
+ };
924
+ static const size_t codes[] = {
925
+ OSEO_FUNCTION_APPLY_CODE_ID,
926
+ OSEO_FUNCTION_BIND_CODE_ID,
927
+ OSEO_FUNCTION_CALL_CODE_ID,
928
+ OSEO_FUNCTION_TO_STRING_CODE_ID,
929
+ OSEO_FUNCTION_HAS_INSTANCE_CODE_ID,
930
+ };
931
+ static const char *const names[] = {
932
+ "apply",
933
+ "bind",
934
+ "call",
935
+ "toString",
936
+ "[Symbol.hasInstance]",
937
+ };
938
+ static const size_t lengths[] = {2u, 1u, 1u, 0u, 1u};
939
+ for (size_t index = 0u;
940
+ result.status == OSEO_STATUS_NORMAL && index < 5u;
941
+ index += 1u) {
942
+ result = create_function_builtin(
943
+ context,
944
+ codes[index],
945
+ names[index],
946
+ lengths[index],
947
+ OSEO_FUNCTION_INTERNAL
948
+ );
949
+ if (result.status == OSEO_STATUS_NORMAL) {
950
+ context->realm->intrinsics[intrinsics[index]] = result.value;
951
+ }
952
+ }
953
+ if (result.status == OSEO_STATUS_NORMAL) {
954
+ result = oseo_internal_throw_type_error_function(context);
955
+ frame.slots[2] = result.value;
956
+ }
957
+ static const char *const restricted_names[] = {"arguments", "caller"};
958
+ for (size_t index = 0u;
959
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
960
+ index += 1u) {
961
+ result = oseo_internal_ascii_string(
962
+ context,
963
+ restricted_names[index]
964
+ );
965
+ frame.slots[3] = result.value;
966
+ if (result.status == OSEO_STATUS_NORMAL) {
967
+ const OseoPropertyAttributes attributes = {
968
+ true,
969
+ false,
970
+ false,
971
+ true,
972
+ };
973
+ result = oseo_object_define_accessor(
974
+ context,
975
+ frame.slots[0],
976
+ frame.slots[3],
977
+ frame.slots[2],
978
+ frame.slots[2],
979
+ true,
980
+ true,
981
+ attributes
982
+ );
983
+ }
984
+ }
985
+ static const OseoIntrinsic properties[] = {
986
+ OSEO_INTRINSIC_FUNCTION,
987
+ OSEO_INTRINSIC_FUNCTION_APPLY,
988
+ OSEO_INTRINSIC_FUNCTION_BIND,
989
+ OSEO_INTRINSIC_FUNCTION_CALL,
990
+ OSEO_INTRINSIC_FUNCTION_TO_STRING,
991
+ };
992
+ static const char *const property_names[] = {
993
+ "constructor",
994
+ "apply",
995
+ "bind",
996
+ "call",
997
+ "toString",
998
+ };
999
+ for (size_t index = 0u;
1000
+ result.status == OSEO_STATUS_NORMAL && index < 5u;
1001
+ index += 1u) {
1002
+ result = oseo_internal_ascii_string(context, property_names[index]);
1003
+ frame.slots[3] = result.value;
1004
+ if (result.status == OSEO_STATUS_NORMAL) {
1005
+ const OseoPropertyAttributes attributes = {
1006
+ true,
1007
+ false,
1008
+ true,
1009
+ false,
1010
+ };
1011
+ result = oseo_object_define(
1012
+ context,
1013
+ frame.slots[0],
1014
+ frame.slots[3],
1015
+ context->realm->intrinsics[properties[index]],
1016
+ attributes
1017
+ );
1018
+ }
1019
+ }
1020
+ if (result.status == OSEO_STATUS_NORMAL) {
1021
+ result = oseo_internal_well_known_symbol(
1022
+ context,
1023
+ OSEO_WELL_KNOWN_HAS_INSTANCE
1024
+ );
1025
+ frame.slots[3] = result.value;
1026
+ }
1027
+ if (result.status == OSEO_STATUS_NORMAL) {
1028
+ const OseoPropertyAttributes attributes = {
1029
+ false,
1030
+ false,
1031
+ false,
1032
+ false,
1033
+ };
1034
+ result = oseo_object_define(
1035
+ context,
1036
+ frame.slots[0],
1037
+ frame.slots[3],
1038
+ context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION_HAS_INSTANCE],
1039
+ attributes
1040
+ );
1041
+ }
1042
+ if (result.status == OSEO_STATUS_NORMAL) {
1043
+ result = normal(frame.slots[0]);
1044
+ if (context->observe_specialization) {
1045
+ context->allocations = entry_allocations;
1046
+ }
1047
+ } else {
1048
+ *marker = oseo_undefined();
1049
+ }
1050
+ oseo_roots_release(context, &frame);
1051
+ return result;
1052
+ }
1053
+
1054
+ OseoResult oseo_intrinsic(OseoContext *context, OseoIntrinsic intrinsic) {
1055
+ if ((size_t)intrinsic >= OSEO_INTRINSIC_COUNT) {
1056
+ return failure(context, "OSEO2001", "Unknown realm intrinsic.");
1057
+ }
1058
+ OseoResult materialized = normal(oseo_undefined());
1059
+ if (intrinsic == OSEO_INTRINSIC_OBJECT_PROTOTYPE ||
1060
+ (intrinsic >= OSEO_INTRINSIC_OBJECT &&
1061
+ intrinsic <= OSEO_INTRINSIC_OBJECT_SET_PROTOTYPE_OF)) {
1062
+ materialized = intrinsic_graph_root(context);
1063
+ if (materialized.status == OSEO_STATUS_NORMAL) {
1064
+ materialized = oseo_internal_object_prototype(context);
1065
+ }
1066
+ } else if (
1067
+ intrinsic == OSEO_INTRINSIC_FUNCTION_PROTOTYPE ||
1068
+ (intrinsic >= OSEO_INTRINSIC_FUNCTION &&
1069
+ intrinsic <= OSEO_INTRINSIC_FUNCTION_HAS_INSTANCE)
1070
+ ) {
1071
+ materialized = intrinsic_graph_root(context);
1072
+ if (materialized.status == OSEO_STATUS_NORMAL) {
1073
+ materialized = function_prototype_intrinsic(context);
1074
+ }
1075
+ } else if (
1076
+ intrinsic == OSEO_INTRINSIC_ARRAY_PROTOTYPE ||
1077
+ intrinsic == OSEO_INTRINSIC_ARRAY_TO_STRING ||
1078
+ (intrinsic >= OSEO_INTRINSIC_ARRAY &&
1079
+ intrinsic <= OSEO_INTRINSIC_ARRAY_SPECIES_GETTER)
1080
+ ) {
1081
+ materialized = oseo_internal_array_intrinsic(context);
1082
+ } else if (intrinsic >= OSEO_INTRINSIC_NUMBER_PROTOTYPE &&
1083
+ intrinsic <= OSEO_INTRINSIC_NUMBER_PARSE_INT) {
1084
+ materialized = oseo_internal_number_intrinsic(context);
1085
+ } else if (intrinsic == OSEO_INTRINSIC_BOOLEAN_PROTOTYPE ||
1086
+ (intrinsic >= OSEO_INTRINSIC_BOOLEAN &&
1087
+ intrinsic <= OSEO_INTRINSIC_BOOLEAN_VALUE_OF)) {
1088
+ materialized = oseo_internal_boolean_intrinsic(context);
1089
+ } else if (intrinsic == OSEO_INTRINSIC_IS_FINITE ||
1090
+ intrinsic == OSEO_INTRINSIC_IS_NAN) {
1091
+ materialized = oseo_internal_global_numeric_intrinsic(
1092
+ context,
1093
+ intrinsic
1094
+ );
1095
+ } else if (intrinsic == OSEO_INTRINSIC_STRING_PROTOTYPE ||
1096
+ (intrinsic >= OSEO_INTRINSIC_STRING &&
1097
+ intrinsic <= OSEO_INTRINSIC_STRING_RAW)) {
1098
+ materialized = oseo_internal_string_intrinsic(context);
1099
+ } else if (
1100
+ intrinsic == OSEO_INTRINSIC_REGEXP_STRING_ITERATOR_PROTOTYPE ||
1101
+ intrinsic == OSEO_INTRINSIC_REGEXP_STRING_ITERATOR_NEXT
1102
+ ) {
1103
+ materialized = oseo_internal_regexp_string_iterator_prototype(context);
1104
+ } else if (intrinsic == OSEO_INTRINSIC_BIGINT_PROTOTYPE ||
1105
+ (intrinsic >= OSEO_INTRINSIC_BIGINT &&
1106
+ intrinsic <= OSEO_INTRINSIC_BIGINT_VALUE_OF)) {
1107
+ materialized = oseo_internal_bigint_intrinsic(context);
1108
+ } else if (intrinsic == OSEO_INTRINSIC_PROMISE_PROTOTYPE ||
1109
+ (intrinsic >= OSEO_INTRINSIC_PROMISE &&
1110
+ intrinsic <= OSEO_INTRINSIC_PROMISE_SPECIES)) {
1111
+ materialized = oseo_internal_promise_intrinsic(context);
1112
+ } else if (intrinsic >= OSEO_INTRINSIC_MAP_ITERATOR_PROTOTYPE &&
1113
+ intrinsic <= OSEO_INTRINSIC_MAP_SPECIES) {
1114
+ materialized = oseo_internal_map_intrinsic(context);
1115
+ } else if (intrinsic >= OSEO_INTRINSIC_ARRAY_BUFFER_PROTOTYPE &&
1116
+ intrinsic <= OSEO_INTRINSIC_ARRAY_BUFFER_SPECIES) {
1117
+ materialized = oseo_internal_array_buffer_intrinsic(context);
1118
+ } else if (intrinsic >= OSEO_INTRINSIC_DATA_VIEW_PROTOTYPE &&
1119
+ intrinsic <= OSEO_INTRINSIC_DATA_VIEW_SET_BIG_UINT64) {
1120
+ materialized = oseo_internal_data_view_intrinsic(context);
1121
+ } else if (intrinsic == OSEO_INTRINSIC_DATE_PROTOTYPE ||
1122
+ intrinsic == OSEO_INTRINSIC_DATE) {
1123
+ materialized = oseo_internal_date_intrinsic(context);
1124
+ } else if (intrinsic >= OSEO_INTRINSIC_REGEXP_PROTOTYPE &&
1125
+ intrinsic <= OSEO_INTRINSIC_REGEXP_SPECIES) {
1126
+ materialized = oseo_internal_regexp_intrinsic(context);
1127
+ } else if (intrinsic == OSEO_INTRINSIC_MATH) {
1128
+ materialized = oseo_internal_math_intrinsic(context);
1129
+ } else if (intrinsic >= OSEO_INTRINSIC_DECODE_URI &&
1130
+ intrinsic <= OSEO_INTRINSIC_ENCODE_URI_COMPONENT) {
1131
+ materialized = oseo_internal_uri_intrinsic(context, intrinsic);
1132
+ } else if (intrinsic == OSEO_INTRINSIC_ESCAPE ||
1133
+ intrinsic == OSEO_INTRINSIC_UNESCAPE) {
1134
+ materialized = oseo_internal_uri_intrinsic(context, intrinsic);
1135
+ } else if (intrinsic == OSEO_INTRINSIC_REFLECT) {
1136
+ materialized = oseo_internal_reflect_intrinsic(context);
1137
+ } else if (intrinsic == OSEO_INTRINSIC_PROXY) {
1138
+ materialized = oseo_internal_proxy_intrinsic(context);
1139
+ } else if (intrinsic >= OSEO_INTRINSIC_PROCESS &&
1140
+ intrinsic <= OSEO_INTRINSIC_PROCESS_EXIT_CODE_SET) {
1141
+ materialized = oseo_internal_process_intrinsic(context);
1142
+ } else if (intrinsic == OSEO_INTRINSIC_JSON ||
1143
+ intrinsic == OSEO_INTRINSIC_JSON_PARSE) {
1144
+ materialized = oseo_internal_json_intrinsic(context);
1145
+ } else if (intrinsic >= OSEO_INTRINSIC_SET_PROTOTYPE &&
1146
+ intrinsic <= OSEO_INTRINSIC_SET_SPECIES) {
1147
+ materialized = oseo_internal_set_intrinsic(context);
1148
+ } else if ((intrinsic >= OSEO_INTRINSIC_TYPED_ARRAY_PROTOTYPE &&
1149
+ intrinsic <= OSEO_INTRINSIC_BIGUINT64_ARRAY) ||
1150
+ intrinsic == OSEO_INTRINSIC_FLOAT16_ARRAY_PROTOTYPE ||
1151
+ intrinsic == OSEO_INTRINSIC_FLOAT16_ARRAY) {
1152
+ materialized = oseo_internal_typed_array_intrinsic(context);
1153
+ } else if (
1154
+ intrinsic >= OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_PROTOTYPE &&
1155
+ intrinsic <= OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_SPECIES
1156
+ ) {
1157
+ materialized = oseo_internal_shared_array_buffer_intrinsic(context);
1158
+ } else if (intrinsic == OSEO_INTRINSIC_ATOMICS) {
1159
+ materialized = oseo_internal_atomics_intrinsic(context);
1160
+ } else if (intrinsic >= OSEO_INTRINSIC_WEAK_MAP_PROTOTYPE &&
1161
+ intrinsic <= OSEO_INTRINSIC_FINALIZATION_REGISTRY_UNREGISTER) {
1162
+ materialized = oseo_internal_weak_collection_intrinsic(context);
1163
+ } else if (intrinsic == OSEO_INTRINSIC_ITERATOR_PROTOTYPE ||
1164
+ intrinsic == OSEO_INTRINSIC_ARRAY_ITERATOR_PROTOTYPE ||
1165
+ (intrinsic >= OSEO_INTRINSIC_ITERATOR &&
1166
+ intrinsic <= OSEO_INTRINSIC_ITERATOR_TAG_SETTER) ||
1167
+ (intrinsic >= OSEO_INTRINSIC_ITERATOR_MAP &&
1168
+ intrinsic <= OSEO_INTRINSIC_ITERATOR_FLAT_MAP) ||
1169
+ (intrinsic >= OSEO_INTRINSIC_ITERATOR_REDUCE &&
1170
+ intrinsic <= OSEO_INTRINSIC_ITERATOR_FIND)) {
1171
+ materialized = oseo_internal_array_iterator_prototype(context);
1172
+ } else if (intrinsic >= OSEO_INTRINSIC_ITERATOR_HELPER_PROTOTYPE &&
1173
+ intrinsic <= OSEO_INTRINSIC_ITERATOR_HELPER_RETURN) {
1174
+ materialized = oseo_internal_iterator_helper_prototype(context);
1175
+ } else if (intrinsic == OSEO_INTRINSIC_GENERATOR_PROTOTYPE ||
1176
+ intrinsic == OSEO_INTRINSIC_GENERATOR_FUNCTION_PROTOTYPE ||
1177
+ intrinsic == OSEO_INTRINSIC_GENERATOR_FUNCTION) {
1178
+ materialized = oseo_internal_generator_prototype(context);
1179
+ } else if (intrinsic == OSEO_INTRINSIC_ASYNC_FUNCTION_PROTOTYPE ||
1180
+ intrinsic == OSEO_INTRINSIC_ASYNC_FUNCTION) {
1181
+ materialized = oseo_internal_async_function_intrinsic(context);
1182
+ } else if (
1183
+ intrinsic >= OSEO_INTRINSIC_ASYNC_ITERATOR_PROTOTYPE &&
1184
+ intrinsic <= OSEO_INTRINSIC_ASYNC_GENERATOR_FUNCTION
1185
+ ) {
1186
+ materialized = oseo_internal_async_generator_prototype(context);
1187
+ } else if (intrinsic == OSEO_INTRINSIC_SYMBOL_PROTOTYPE ||
1188
+ intrinsic == OSEO_INTRINSIC_SYMBOL ||
1189
+ (intrinsic >= OSEO_INTRINSIC_SYMBOL_FOR &&
1190
+ intrinsic <= OSEO_INTRINSIC_SYMBOL_DESCRIPTION_GETTER)) {
1191
+ materialized = oseo_symbol_intrinsic(context);
1192
+ } else if (intrinsic >= OSEO_INTRINSIC_ERROR_PROTOTYPE &&
1193
+ intrinsic <= OSEO_INTRINSIC_AGGREGATE_ERROR_PROTOTYPE) {
1194
+ OseoErrorKind kind = (OseoErrorKind)(
1195
+ intrinsic - OSEO_INTRINSIC_ERROR_PROTOTYPE
1196
+ );
1197
+ materialized = oseo_internal_error_prototype(context, kind);
1198
+ } else if (intrinsic >= OSEO_INTRINSIC_ERROR &&
1199
+ intrinsic <= OSEO_INTRINSIC_AGGREGATE_ERROR) {
1200
+ OseoErrorKind kind =
1201
+ (OseoErrorKind)(intrinsic - OSEO_INTRINSIC_ERROR);
1202
+ materialized = oseo_error_intrinsic(context, kind);
1203
+ } else if (intrinsic == OSEO_INTRINSIC_SUPPRESSED_ERROR_PROTOTYPE) {
1204
+ materialized = oseo_internal_error_prototype(
1205
+ context,
1206
+ OSEO_ERROR_SUPPRESSED
1207
+ );
1208
+ } else if (intrinsic == OSEO_INTRINSIC_SUPPRESSED_ERROR) {
1209
+ materialized = oseo_error_intrinsic(context, OSEO_ERROR_SUPPRESSED);
1210
+ } else if (intrinsic >= OSEO_INTRINSIC_DISPOSABLE_STACK_PROTOTYPE &&
1211
+ intrinsic <= OSEO_INTRINSIC_ASYNC_DISPOSABLE_STACK) {
1212
+ materialized = oseo_internal_disposal_intrinsic(context);
1213
+ } else if (intrinsic == OSEO_INTRINSIC_EVAL) {
1214
+ materialized = oseo_internal_eval_intrinsic(context);
1215
+ } else if (intrinsic == OSEO_INTRINSIC_THROW_TYPE_ERROR) {
1216
+ materialized = oseo_internal_throw_type_error_function(context);
1217
+ } else if (intrinsic == OSEO_INTRINSIC_ARRAY_PUSH) {
1218
+ materialized = oseo_internal_array_push_function(context);
1219
+ } else if (intrinsic >= OSEO_INTRINSIC_ARRAY_VALUES &&
1220
+ intrinsic <= OSEO_INTRINSIC_GENERATOR_THROW) {
1221
+ static const size_t codes[] = {
1222
+ OSEO_ARRAY_VALUES_CODE_ID,
1223
+ OSEO_ARRAY_ITERATOR_NEXT_CODE_ID,
1224
+ OSEO_ITERATOR_SELF_CODE_ID,
1225
+ OSEO_GENERATOR_NEXT_CODE_ID,
1226
+ OSEO_GENERATOR_RETURN_CODE_ID,
1227
+ OSEO_GENERATOR_THROW_CODE_ID,
1228
+ };
1229
+ size_t index = (size_t)(intrinsic - OSEO_INTRINSIC_ARRAY_VALUES);
1230
+ materialized = oseo_internal_iterator_method(context, codes[index]);
1231
+ } else if (intrinsic == OSEO_INTRINSIC_ARRAY_KEYS) {
1232
+ materialized = oseo_internal_iterator_method(
1233
+ context,
1234
+ OSEO_ARRAY_KEYS_CODE_ID
1235
+ );
1236
+ } else if (intrinsic == OSEO_INTRINSIC_ARRAY_ENTRIES) {
1237
+ materialized = oseo_internal_iterator_method(
1238
+ context,
1239
+ OSEO_ARRAY_ENTRIES_CODE_ID
1240
+ );
1241
+ } else if (intrinsic == OSEO_INTRINSIC_STRING_PROTOTYPE_ITERATOR) {
1242
+ materialized = oseo_internal_iterator_method(
1243
+ context,
1244
+ OSEO_STRING_PROTOTYPE_ITERATOR_CODE_ID
1245
+ );
1246
+ } else if (intrinsic == OSEO_INTRINSIC_STRING_ITERATOR_PROTOTYPE ||
1247
+ intrinsic == OSEO_INTRINSIC_STRING_ITERATOR_NEXT) {
1248
+ materialized = oseo_internal_string_iterator_prototype(context);
1249
+ } else if (intrinsic >= OSEO_INTRINSIC_PROMISE_THEN &&
1250
+ intrinsic <= OSEO_INTRINSIC_PROMISE_FINALLY) {
1251
+ static const char *const names[] = {"then", "catch", "finally"};
1252
+ size_t index = (size_t)(intrinsic - OSEO_INTRINSIC_PROMISE_THEN);
1253
+ materialized = oseo_internal_promise_method_function(
1254
+ context,
1255
+ names[index]
1256
+ );
1257
+ } else if (intrinsic >= OSEO_INTRINSIC_ASYNC_GENERATOR_NEXT &&
1258
+ intrinsic <= OSEO_INTRINSIC_ASYNC_ITERATOR_SELF) {
1259
+ static const size_t codes[] = {
1260
+ OSEO_ASYNC_GENERATOR_NEXT_CODE_ID,
1261
+ OSEO_ASYNC_GENERATOR_RETURN_CODE_ID,
1262
+ OSEO_ASYNC_GENERATOR_THROW_CODE_ID,
1263
+ OSEO_ASYNC_ITERATOR_SELF_CODE_ID,
1264
+ };
1265
+ size_t index =
1266
+ (size_t)(intrinsic - OSEO_INTRINSIC_ASYNC_GENERATOR_NEXT);
1267
+ materialized = oseo_internal_async_generator_method(
1268
+ context,
1269
+ codes[index]
1270
+ );
1271
+ } else {
1272
+ return failure(context, "OSEO2001", "Unknown realm intrinsic.");
1273
+ }
1274
+ if (materialized.status != OSEO_STATUS_NORMAL) return materialized;
1275
+ OseoValue value = context->realm->intrinsics[intrinsic];
1276
+ if (tag_of(value) == OSEO_TAG_UNDEFINED) {
1277
+ return failure(context, "OSEO2001", "Realm intrinsic is unavailable.");
1278
+ }
1279
+ return normal(value);
1280
+ }
1281
+
1282
+ OseoResult oseo_internal_intrinsic(
1283
+ OseoContext *context,
1284
+ OseoIntrinsic intrinsic
1285
+ ) {
1286
+ return oseo_intrinsic(context, intrinsic);
1287
+ }
1288
+
1289
+ OseoResult oseo_function_create(
1290
+ OseoContext *context,
1291
+ size_t code_id,
1292
+ OseoValue environment,
1293
+ const uint16_t *name_units,
1294
+ size_t name_length,
1295
+ size_t parameter_count,
1296
+ OseoFunctionKind function_kind,
1297
+ OseoValue lexical_this,
1298
+ OseoValue inferred_name,
1299
+ OseoFunctionNamePrefix name_prefix
1300
+ ) {
1301
+ bool bootstrapping_function_prototype =
1302
+ code_id == OSEO_FUNCTION_PROTOTYPE_CODE_ID &&
1303
+ tag_of(context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION_PROTOTYPE]) ==
1304
+ OSEO_TAG_UNDEFINED;
1305
+ if (!bootstrapping_function_prototype && !is_environment(environment)) {
1306
+ return failure(context, "OSEO2001", "Invalid function environment.");
1307
+ }
1308
+ OseoRootFrame frame = {NULL, NULL, 0u};
1309
+ switch (function_kind) {
1310
+ case OSEO_FUNCTION_ORDINARY:
1311
+ case OSEO_FUNCTION_ARROW:
1312
+ case OSEO_FUNCTION_ASYNC:
1313
+ case OSEO_FUNCTION_ASYNC_ARROW:
1314
+ case OSEO_FUNCTION_INTERNAL:
1315
+ case OSEO_FUNCTION_METHOD:
1316
+ case OSEO_FUNCTION_GENERATOR:
1317
+ case OSEO_FUNCTION_ASYNC_GENERATOR:
1318
+ case OSEO_FUNCTION_CLASS:
1319
+ case OSEO_FUNCTION_BOUND:
1320
+ break;
1321
+ default:
1322
+ return failure(context, "OSEO2001", "Invalid function kind.");
1323
+ }
1324
+ OseoResult result = oseo_roots_allocate(context, &frame, 10u);
1325
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1326
+ frame.slots[5] = inferred_name;
1327
+ if (bootstrapping_function_prototype) {
1328
+ frame.slots[8] =
1329
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_PROTOTYPE];
1330
+ frame.slots[9] = frame.slots[8];
1331
+ } else {
1332
+ result = oseo_internal_intrinsic(
1333
+ context,
1334
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1335
+ );
1336
+ frame.slots[8] = result.value;
1337
+ if (result.status == OSEO_STATUS_NORMAL) {
1338
+ result = oseo_internal_intrinsic(
1339
+ context,
1340
+ OSEO_INTRINSIC_FUNCTION_PROTOTYPE
1341
+ );
1342
+ frame.slots[9] = result.value;
1343
+ }
1344
+ if (result.status != OSEO_STATUS_NORMAL) {
1345
+ oseo_roots_release(context, &frame);
1346
+ return result;
1347
+ }
1348
+ }
1349
+ frame.slots[7] = function_kind == OSEO_FUNCTION_ARROW ||
1350
+ function_kind == OSEO_FUNCTION_ASYNC_ARROW
1351
+ ? lexical_this
1352
+ : oseo_undefined();
1353
+ if (bootstrapping_function_prototype) {
1354
+ frame.slots[0] = oseo_undefined();
1355
+ } else {
1356
+ result = oseo_environment_clone(context, environment);
1357
+ frame.slots[0] = result.value;
1358
+ if (result.status != OSEO_STATUS_NORMAL) {
1359
+ oseo_roots_release(context, &frame);
1360
+ return result;
1361
+ }
1362
+ }
1363
+ /* A generator function's `prototype` object inherits from
1364
+ * %GeneratorPrototype%, so its generators reach the materialized
1365
+ * `next` and `Symbol.iterator` through the specified lookup order,
1366
+ * and, unlike a constructor's prototype, it has no `constructor`
1367
+ * property. */
1368
+ if (function_kind == OSEO_FUNCTION_GENERATOR ||
1369
+ function_kind == OSEO_FUNCTION_ASYNC_GENERATOR) {
1370
+ result = function_kind == OSEO_FUNCTION_ASYNC_GENERATOR
1371
+ ? oseo_internal_async_generator_prototype(context)
1372
+ : oseo_internal_generator_prototype(context);
1373
+ frame.slots[8] = result.value;
1374
+ if (result.status != OSEO_STATUS_NORMAL) {
1375
+ oseo_roots_release(context, &frame);
1376
+ return result;
1377
+ }
1378
+ }
1379
+ /* A generator, asynchronous generator, or asynchronous function
1380
+ * inherits from the function-prototype object of its own kind, which
1381
+ * is what makes its `constructor` reach %GeneratorFunction%,
1382
+ * %AsyncGeneratorFunction%, or %AsyncFunction% and its
1383
+ * `Symbol.toStringTag` report that kind. An asynchronous arrow
1384
+ * function is an asynchronous function for this purpose. Every other
1385
+ * function inherits from the realm's %Function.prototype%. */
1386
+ if (function_kind == OSEO_FUNCTION_GENERATOR ||
1387
+ function_kind == OSEO_FUNCTION_ASYNC_GENERATOR ||
1388
+ function_kind == OSEO_FUNCTION_ASYNC ||
1389
+ function_kind == OSEO_FUNCTION_ASYNC_ARROW) {
1390
+ result = function_kind == OSEO_FUNCTION_GENERATOR
1391
+ ? oseo_internal_generator_function_intrinsic(context)
1392
+ : function_kind == OSEO_FUNCTION_ASYNC_GENERATOR
1393
+ ? oseo_internal_async_generator_intrinsic(context)
1394
+ : oseo_internal_async_function_intrinsic(context);
1395
+ frame.slots[9] = result.value;
1396
+ if (result.status != OSEO_STATUS_NORMAL) {
1397
+ oseo_roots_release(context, &frame);
1398
+ return result;
1399
+ }
1400
+ }
1401
+ if (bootstrapping_function_prototype) {
1402
+ result = normal(oseo_undefined());
1403
+ } else {
1404
+ result = oseo_object_create(context, frame.slots[8]);
1405
+ }
1406
+ frame.slots[1] = result.value;
1407
+ if (result.status == OSEO_STATUS_NORMAL &&
1408
+ !bootstrapping_function_prototype &&
1409
+ context->observe_specialization && context->allocations > 0u) {
1410
+ context->allocations -= 1u;
1411
+ }
1412
+ if (result.status != OSEO_STATUS_NORMAL) {
1413
+ oseo_roots_release(context, &frame);
1414
+ return result;
1415
+ }
1416
+ OseoFunction *function =
1417
+ oseo_internal_allocate_heap_bytes(context, sizeof(*function));
1418
+ if (function == NULL) {
1419
+ oseo_roots_release(context, &frame);
1420
+ return failure(context, "OSEO2001", "Function allocation failed.");
1421
+ }
1422
+ function->ordinary.prototype = frame.slots[9];
1423
+ function->ordinary.properties = NULL;
1424
+ function->ordinary.property_capacity = 0u;
1425
+ function->ordinary.property_count = 0u;
1426
+ oseo_internal_init_dense_index(&function->ordinary);
1427
+ function->ordinary.private_elements = NULL;
1428
+ function->ordinary.private_element_capacity = 0u;
1429
+ function->ordinary.private_element_count = 0u;
1430
+ function->ordinary.shape_id = context->next_shape_id;
1431
+ context->next_shape_id += 1u;
1432
+ function->ordinary.array_length = 0u;
1433
+ function->ordinary.dictionary = false;
1434
+ function->ordinary.length_writable = false;
1435
+ function->ordinary.extensible = true;
1436
+ function->ordinary.module_namespace = false;
1437
+ function->ordinary.immutable_prototype = false;
1438
+ function->ordinary.global_object = false;
1439
+ function->ordinary.error_data = false;
1440
+ function->ordinary.number_data = false;
1441
+ function->ordinary.number_value = oseo_undefined();
1442
+ function->ordinary.primitive_data = false;
1443
+ function->ordinary.primitive_value = oseo_undefined();
1444
+ function->ordinary.virtual_string_iterator = false;
1445
+ function->ordinary.virtual_string_iterator_configurable = false;
1446
+ function->ordinary.virtual_string_iterator_enumerable = false;
1447
+ function->ordinary.virtual_string_iterator_writable = false;
1448
+ function->ordinary.iterator_kind = OSEO_ITERATOR_NONE;
1449
+ function->ordinary.iterator_target = oseo_undefined();
1450
+ function->ordinary.iterator_index = 0u;
1451
+ function->ordinary.regexp_string_iterator = false;
1452
+ function->ordinary.regexp_iterator_regexp = oseo_undefined();
1453
+ function->ordinary.regexp_iterator_subject = oseo_undefined();
1454
+ function->ordinary.regexp_iterator_global = false;
1455
+ function->ordinary.regexp_iterator_unicode = false;
1456
+ function->ordinary.regexp_iterator_complete = false;
1457
+ function->ordinary.async_from_sync = false;
1458
+ function->ordinary.async_sync_iterator = oseo_undefined();
1459
+ function->ordinary.wrap_for_valid_iterator = false;
1460
+ function->ordinary.wrapped_iterator = oseo_undefined();
1461
+ function->ordinary.wrapped_next = oseo_undefined();
1462
+ function->ordinary.generator = NULL;
1463
+ function->ordinary.arguments_object = false;
1464
+ function->ordinary.mapped_arguments = false;
1465
+ function->environment = frame.slots[0];
1466
+ function->lexical_this = frame.slots[7];
1467
+ function->lexical_new_target = oseo_undefined();
1468
+ function->lexical_super = oseo_undefined();
1469
+ function->prototype_object = frame.slots[1];
1470
+ function->home_object = oseo_undefined();
1471
+ function->initial_name = oseo_undefined();
1472
+ function->source_text = oseo_undefined();
1473
+ function->realm = context->realm_record;
1474
+ function->bound_target = oseo_undefined();
1475
+ function->bound_this = oseo_undefined();
1476
+ function->bound_arguments = oseo_undefined();
1477
+ function->elements = NULL;
1478
+ function->element_count = 0u;
1479
+ function->element_capacity = 0u;
1480
+ function->code_id = code_id;
1481
+ function->function_kind = function_kind;
1482
+ /* ClassDefinitionEvaluation marks a constructor derived only when
1483
+ * the definition has a ClassHeritage, which oseo_class_heritage
1484
+ * records. */
1485
+ function->derived_constructor = false;
1486
+ /* A class's `prototype` is non-writable, non-enumerable, and
1487
+ * non-configurable, unlike an ordinary function's writable one. */
1488
+ function->prototype_writable = function_kind != OSEO_FUNCTION_CLASS;
1489
+ /* MakeConstructor runs after SetFunctionLength and SetFunctionName,
1490
+ * so `prototype` follows the two properties defined just below. */
1491
+ function->prototype_key_position = 2u;
1492
+ function->non_strict = false;
1493
+ function->legacy_live = 0u;
1494
+ function->legacy_arguments_position = 2u;
1495
+ result = oseo_internal_publish_heap(
1496
+ context,
1497
+ &function->ordinary.header,
1498
+ OSEO_HEAP_FUNCTION
1499
+ );
1500
+ frame.slots[2] = result.value;
1501
+ if (result.status == OSEO_STATUS_NORMAL) {
1502
+ static const uint16_t length_name[] = {
1503
+ 'l', 'e', 'n', 'g', 't', 'h',
1504
+ };
1505
+ size_t counted = context->allocations;
1506
+ result = oseo_internal_allocate_string(
1507
+ context,
1508
+ length_name,
1509
+ sizeof(length_name) / sizeof(*length_name)
1510
+ );
1511
+ frame.slots[3] = result.value;
1512
+ context->allocations = counted;
1513
+ }
1514
+ if (result.status == OSEO_STATUS_NORMAL) {
1515
+ OseoPropertyAttributes attributes = {true, false, false, false};
1516
+ result = oseo_object_define(
1517
+ context,
1518
+ frame.slots[2],
1519
+ frame.slots[3],
1520
+ oseo_number(parameter_count),
1521
+ attributes
1522
+ );
1523
+ }
1524
+ if (result.status == OSEO_STATUS_NORMAL) {
1525
+ static const uint16_t name_name[] = {'n', 'a', 'm', 'e'};
1526
+ size_t counted = context->allocations;
1527
+ result = oseo_internal_allocate_string(
1528
+ context,
1529
+ name_name,
1530
+ sizeof(name_name) / sizeof(*name_name)
1531
+ );
1532
+ frame.slots[4] = result.value;
1533
+ context->allocations = counted;
1534
+ }
1535
+ if (result.status == OSEO_STATUS_NORMAL &&
1536
+ tag_of(frame.slots[5]) == OSEO_TAG_UNDEFINED) {
1537
+ size_t counted = context->allocations;
1538
+ result =
1539
+ oseo_internal_allocate_string(context, name_units, name_length);
1540
+ frame.slots[5] = result.value;
1541
+ context->allocations = counted;
1542
+ }
1543
+ /* SetFunctionName wraps a symbol key's description in brackets,
1544
+ * producing "[description]", or an empty name for a
1545
+ * descriptionless symbol. */
1546
+ if (result.status == OSEO_STATUS_NORMAL && is_symbol(frame.slots[5])) {
1547
+ size_t counted = context->allocations;
1548
+ result = oseo_internal_symbol_name(context, frame.slots[5]);
1549
+ frame.slots[5] = result.value;
1550
+ context->allocations = counted;
1551
+ }
1552
+ if (result.status == OSEO_STATUS_NORMAL && !is_string(frame.slots[5])) {
1553
+ result = failure(context, "OSEO2001", "Invalid function name.");
1554
+ }
1555
+ if (result.status == OSEO_STATUS_NORMAL &&
1556
+ name_prefix != OSEO_FUNCTION_NAME_PREFIX_NONE) {
1557
+ size_t counted = context->allocations;
1558
+ result = accessor_function_name(
1559
+ context,
1560
+ frame.slots[5],
1561
+ name_prefix == OSEO_FUNCTION_NAME_PREFIX_SET
1562
+ );
1563
+ frame.slots[5] = result.value;
1564
+ context->allocations = counted;
1565
+ }
1566
+ if (result.status == OSEO_STATUS_NORMAL) {
1567
+ OseoPropertyAttributes attributes = {true, false, false, false};
1568
+ result = oseo_object_define(
1569
+ context,
1570
+ frame.slots[2],
1571
+ frame.slots[4],
1572
+ frame.slots[5],
1573
+ attributes
1574
+ );
1575
+ if (result.status == OSEO_STATUS_NORMAL) {
1576
+ function_object(frame.slots[2])->initial_name = frame.slots[5];
1577
+ }
1578
+ }
1579
+ if (result.status == OSEO_STATUS_NORMAL &&
1580
+ !bootstrapping_function_prototype &&
1581
+ function_kind != OSEO_FUNCTION_GENERATOR &&
1582
+ function_kind != OSEO_FUNCTION_ASYNC_GENERATOR) {
1583
+ static const uint16_t constructor_name[] = {
1584
+ 'c', 'o', 'n', 's', 't', 'r', 'u', 'c', 't', 'o', 'r',
1585
+ };
1586
+ size_t counted = context->allocations;
1587
+ result = oseo_internal_allocate_string(
1588
+ context,
1589
+ constructor_name,
1590
+ sizeof(constructor_name) / sizeof(*constructor_name)
1591
+ );
1592
+ frame.slots[6] = result.value;
1593
+ context->allocations = counted;
1594
+ }
1595
+ if (result.status == OSEO_STATUS_NORMAL &&
1596
+ !bootstrapping_function_prototype &&
1597
+ function_kind != OSEO_FUNCTION_GENERATOR &&
1598
+ function_kind != OSEO_FUNCTION_ASYNC_GENERATOR) {
1599
+ OseoPropertyAttributes attributes = {true, false, true, false};
1600
+ result = oseo_object_define(
1601
+ context,
1602
+ frame.slots[1],
1603
+ frame.slots[6],
1604
+ frame.slots[2],
1605
+ attributes
1606
+ );
1607
+ }
1608
+ if (result.status == OSEO_STATUS_NORMAL) {
1609
+ result.value = frame.slots[2];
1610
+ }
1611
+ oseo_roots_release(context, &frame);
1612
+ return result;
1613
+ }
1614
+
1615
+ OseoResult oseo_function_create_non_strict(
1616
+ OseoContext *context,
1617
+ size_t code_id,
1618
+ OseoValue environment,
1619
+ const uint16_t *name_units,
1620
+ size_t name_length,
1621
+ size_t parameter_count,
1622
+ OseoFunctionKind function_kind,
1623
+ OseoValue lexical_this,
1624
+ OseoValue inferred_name,
1625
+ OseoFunctionNamePrefix name_prefix
1626
+ ) {
1627
+ OseoResult result = oseo_function_create(
1628
+ context,
1629
+ code_id,
1630
+ environment,
1631
+ name_units,
1632
+ name_length,
1633
+ parameter_count,
1634
+ function_kind,
1635
+ lexical_this,
1636
+ inferred_name,
1637
+ name_prefix
1638
+ );
1639
+ if (result.status == OSEO_STATUS_NORMAL) {
1640
+ OseoFunction *function = function_object(result.value);
1641
+ function->non_strict = true;
1642
+ if (function->function_kind == OSEO_FUNCTION_ORDINARY) {
1643
+ function->legacy_live =
1644
+ OSEO_LEGACY_ARGUMENTS_LIVE | OSEO_LEGACY_CALLER_LIVE;
1645
+ function->legacy_arguments_position =
1646
+ function->prototype_key_position;
1647
+ }
1648
+ }
1649
+ return result;
1650
+ }
1651
+
1652
+ OseoResult oseo_internal_legacy_function_keys(OseoContext *context) {
1653
+ static const char *const names[2] = {"arguments", "caller"};
1654
+ for (size_t index = 0u; index < 2u; index += 1u) {
1655
+ if (tag_of(context->legacy_function_keys[index]) !=
1656
+ OSEO_TAG_UNDEFINED) {
1657
+ continue;
1658
+ }
1659
+ OseoResult key = oseo_internal_ascii_string(context, names[index]);
1660
+ if (key.status != OSEO_STATUS_NORMAL) return key;
1661
+ context->legacy_function_keys[index] = key.value;
1662
+ }
1663
+ return normal(oseo_undefined());
1664
+ }
1665
+
1666
+ /* The most recent running activation of `function`, or NULL. */
1667
+ static const OseoActivation *latest_activation(
1668
+ const OseoContext *context,
1669
+ OseoValue function
1670
+ ) {
1671
+ for (const OseoActivation *activation = context->activation;
1672
+ activation != NULL;
1673
+ activation = activation->previous) {
1674
+ if (activation->function == function) return activation;
1675
+ }
1676
+ return NULL;
1677
+ }
1678
+
1679
+ /*
1680
+ * The legacy `caller` answer for one activation: the function that made
1681
+ * the call that started it, when that function is non-strict generated
1682
+ * code, and null otherwise. The record linked below the activation was
1683
+ * the running one when the call started, and stays linked as long as
1684
+ * the activation does.
1685
+ */
1686
+ static OseoValue activation_caller(const OseoActivation *activation) {
1687
+ const OseoActivation *previous = activation->previous;
1688
+ if (previous == NULL) return oseo_null();
1689
+ OseoValue caller = tag_of(previous->function) == OSEO_TAG_UNDEFINED
1690
+ ? previous->caller
1691
+ : previous->function;
1692
+ if (is_function(caller) && function_object(caller)->non_strict) {
1693
+ return caller;
1694
+ }
1695
+ return oseo_null();
1696
+ }
1697
+
1698
+ bool oseo_internal_legacy_descriptor(
1699
+ const OseoContext *context,
1700
+ OseoValue object_value,
1701
+ OseoValue key,
1702
+ OseoValue *value,
1703
+ OseoPropertyAttributes *attributes
1704
+ ) {
1705
+ uint8_t live = oseo_internal_legacy_live_key(object_value, key);
1706
+ if (live == 0u) return false;
1707
+ bool caller = live == OSEO_LEGACY_CALLER_LIVE;
1708
+ *attributes = (OseoPropertyAttributes){true, false, false, false};
1709
+ const OseoActivation *activation =
1710
+ latest_activation(context, object_value);
1711
+ if (activation == NULL) {
1712
+ *value = oseo_null();
1713
+ } else if (caller) {
1714
+ *value = activation_caller(activation);
1715
+ } else {
1716
+ *value = object_value;
1717
+ }
1718
+ return true;
1719
+ }
1720
+
1721
+ bool oseo_internal_legacy_arguments_pending(
1722
+ OseoValue object_value,
1723
+ OseoValue key,
1724
+ OseoValue value
1725
+ ) {
1726
+ return value == object_value &&
1727
+ oseo_internal_legacy_live_key(object_value, key) ==
1728
+ OSEO_LEGACY_ARGUMENTS_LIVE;
1729
+ }
1730
+
1731
+ OseoResult oseo_internal_legacy_value(
1732
+ OseoContext *context,
1733
+ OseoValue object_value,
1734
+ OseoValue key,
1735
+ OseoValue value
1736
+ ) {
1737
+ if (!oseo_internal_legacy_arguments_pending(object_value, key, value)) {
1738
+ return normal(value);
1739
+ }
1740
+ const OseoActivation *activation =
1741
+ latest_activation(context, object_value);
1742
+ if (activation == NULL) return normal(oseo_null());
1743
+ /* A mapped-shape object with no mapped index: `callee` is the
1744
+ * ordinary data property V8 reports, and no element aliases a
1745
+ * parameter. The activation's arguments stay rooted by the frame
1746
+ * that made its call. */
1747
+ OseoResult result = oseo_mapped_arguments_create(
1748
+ context,
1749
+ oseo_undefined(),
1750
+ object_value,
1751
+ activation->argument_count,
1752
+ activation->arguments,
1753
+ NULL,
1754
+ NULL,
1755
+ 0u
1756
+ );
1757
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1758
+ ordinary_object(result.value)->mapped_arguments = false;
1759
+ if (function_object(object_value)->realm == context->realm_record) {
1760
+ return result;
1761
+ }
1762
+ /* V8 takes the prototype from the running realm but the
1763
+ * Symbol.iterator value from the function's own realm (ADR 0034). */
1764
+ OseoRootFrame frame = {NULL, NULL, 0u};
1765
+ OseoResult rooted = oseo_roots_allocate(context, &frame, 4u);
1766
+ if (rooted.status != OSEO_STATUS_NORMAL) return rooted;
1767
+ frame.slots[0] = result.value;
1768
+ frame.slots[3] = object_value;
1769
+ result = oseo_internal_well_known_symbol(context, OSEO_WELL_KNOWN_ITERATOR);
1770
+ frame.slots[1] = result.value;
1771
+ if (result.status == OSEO_STATUS_NORMAL) {
1772
+ OseoRealmScope scope;
1773
+ oseo_internal_realm_enter(
1774
+ context,
1775
+ &scope,
1776
+ function_object(frame.slots[3])->realm
1777
+ );
1778
+ result = oseo_internal_iterator_method(
1779
+ context,
1780
+ OSEO_ARRAY_VALUES_CODE_ID
1781
+ );
1782
+ oseo_internal_realm_leave(context, &scope);
1783
+ frame.slots[2] = result.value;
1784
+ }
1785
+ if (result.status == OSEO_STATUS_NORMAL) {
1786
+ result = oseo_object_define(
1787
+ context,
1788
+ frame.slots[0],
1789
+ frame.slots[1],
1790
+ frame.slots[2],
1791
+ (OseoPropertyAttributes){true, false, true, false}
1792
+ );
1793
+ }
1794
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
1795
+ oseo_roots_release(context, &frame);
1796
+ return result;
1797
+ }
1798
+
1799
+ OseoResult oseo_function_set_source(
1800
+ OseoContext *context,
1801
+ OseoValue function_value,
1802
+ const uint16_t *source_units,
1803
+ size_t source_length
1804
+ ) {
1805
+ if (!is_function(function_value)) {
1806
+ return oseo_internal_throw_error(
1807
+ context,
1808
+ OSEO_ERROR_TYPE,
1809
+ "Function source requires a callable value."
1810
+ );
1811
+ }
1812
+ OseoValue rooted = function_value;
1813
+ OseoRootFrame frame = {NULL, &rooted, 1u};
1814
+ oseo_roots_push(context, &frame);
1815
+ OseoResult result = oseo_internal_allocate_string(
1816
+ context,
1817
+ source_units,
1818
+ source_length
1819
+ );
1820
+ if (result.status == OSEO_STATUS_NORMAL) {
1821
+ function_object(rooted)->source_text = result.value;
1822
+ result = normal(rooted);
1823
+ }
1824
+ oseo_roots_pop(context, &frame);
1825
+ return result;
1826
+ }
1827
+
1828
+ OseoResult oseo_function_environment(
1829
+ OseoContext *context,
1830
+ OseoValue function_value
1831
+ ) {
1832
+ if (!is_function(function_value)) {
1833
+ return oseo_internal_throw_error(
1834
+ context,
1835
+ OSEO_ERROR_TYPE,
1836
+ "Called value is not a function."
1837
+ );
1838
+ }
1839
+ return normal(function_object(function_value)->environment);
1840
+ }
1841
+
1842
+ OseoResult oseo_function_code_id(
1843
+ OseoContext *context,
1844
+ OseoValue function_value,
1845
+ size_t *code_id
1846
+ ) {
1847
+ if (!is_function(function_value)) {
1848
+ return oseo_internal_throw_error(
1849
+ context,
1850
+ OSEO_ERROR_TYPE,
1851
+ "Called value is not a function."
1852
+ );
1853
+ }
1854
+ *code_id = function_object(function_value)->code_id;
1855
+ return normal(function_value);
1856
+ }
1857
+
1858
+ OseoResult oseo_unknown_function(OseoContext *context, size_t code_id) {
1859
+ (void)code_id;
1860
+ return failure(context, "OSEO2001", "Function code identity is invalid.");
1861
+ }
1862
+
1863
+ OseoResult oseo_function_prototype(
1864
+ OseoContext *context,
1865
+ OseoValue function_value
1866
+ ) {
1867
+ if (!function_is_constructible(function_value)) {
1868
+ return oseo_internal_throw_error(
1869
+ context,
1870
+ OSEO_ERROR_TYPE,
1871
+ "Constructed value is not a constructor."
1872
+ );
1873
+ }
1874
+ /* A Proxy owns [[Construct]] and allocates or returns its receiver
1875
+ * there. Returning no prototype makes legacy callers' eager receiver
1876
+ * irrelevant without reading a function record through the Proxy's
1877
+ * shorter heap layout. */
1878
+ while (is_function(function_value) &&
1879
+ function_object(function_value)->function_kind ==
1880
+ OSEO_FUNCTION_BOUND) {
1881
+ function_value = function_object(function_value)->bound_target;
1882
+ }
1883
+ if (is_proxy(function_value)) return normal(oseo_undefined());
1884
+ return normal(function_object(function_value)->prototype_object);
1885
+ }
1886
+
1887
+ OseoResult oseo_internal_ordinary_has_instance(
1888
+ OseoContext *context,
1889
+ OseoValue target,
1890
+ OseoValue value
1891
+ ) {
1892
+ if (!is_callable(target)) return normal(oseo_boolean(false));
1893
+ if (is_function(target) &&
1894
+ function_object(target)->function_kind == OSEO_FUNCTION_BOUND) {
1895
+ return oseo_instanceof(
1896
+ context,
1897
+ value,
1898
+ function_object(target)->bound_target
1899
+ );
1900
+ }
1901
+ if (!is_object(value)) return normal(oseo_boolean(false));
1902
+ OseoValue slots[3] = {target, value, oseo_undefined()};
1903
+ OseoRootFrame frame = {NULL, slots, 3u};
1904
+ oseo_roots_push(context, &frame);
1905
+ OseoResult result = oseo_internal_ascii_string(context, "prototype");
1906
+ slots[2] = result.value;
1907
+ if (result.status == OSEO_STATUS_NORMAL) {
1908
+ result = oseo_object_get(context, slots[0], slots[2]);
1909
+ slots[2] = result.value;
1910
+ }
1911
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[2])) {
1912
+ result = oseo_internal_throw_error(
1913
+ context,
1914
+ OSEO_ERROR_TYPE,
1915
+ "The instanceof prototype must be an object."
1916
+ );
1917
+ }
1918
+ if (result.status == OSEO_STATUS_NORMAL) {
1919
+ OseoResult prototype = oseo_internal_get_prototype(
1920
+ context, slots[1]);
1921
+ OseoValue current = prototype.value;
1922
+ bool found = false;
1923
+ while (prototype.status == OSEO_STATUS_NORMAL && is_object(current)) {
1924
+ if (current == slots[2]) {
1925
+ found = true;
1926
+ break;
1927
+ }
1928
+ prototype = oseo_internal_get_prototype(context, current);
1929
+ current = prototype.value;
1930
+ }
1931
+ result = prototype.status == OSEO_STATUS_NORMAL
1932
+ ? normal(oseo_boolean(found))
1933
+ : prototype;
1934
+ }
1935
+ oseo_roots_pop(context, &frame);
1936
+ return result;
1937
+ }
1938
+
1939
+ OseoResult oseo_constructor_receiver(
1940
+ OseoContext *context,
1941
+ OseoValue prototype
1942
+ ) {
1943
+ OseoResult fallback = normal(prototype);
1944
+ if (!is_object(prototype)) {
1945
+ fallback = oseo_internal_intrinsic(
1946
+ context,
1947
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1948
+ );
1949
+ }
1950
+ if (fallback.status != OSEO_STATUS_NORMAL) return fallback;
1951
+ return oseo_object_create(context, fallback.value);
1952
+ }
1953
+
1954
+ /*
1955
+ * Get(constructor, "prototype"), the observable half of
1956
+ * GetPrototypeFromConstructor. The caller decides the intrinsic default
1957
+ * for a non-object result, because every clause names its own and
1958
+ * several of those prototypes are built lazily.
1959
+ */
1960
+ OseoResult oseo_internal_constructor_prototype(
1961
+ OseoContext *context,
1962
+ OseoValue constructor
1963
+ ) {
1964
+ OseoValue slots[2] = {constructor, oseo_undefined()};
1965
+ OseoRootFrame frame = {NULL, slots, 2u};
1966
+ oseo_roots_push(context, &frame);
1967
+ OseoResult result = oseo_internal_ascii_string(context, "prototype");
1968
+ slots[1] = result.value;
1969
+ if (result.status == OSEO_STATUS_NORMAL) {
1970
+ result = oseo_object_get(context, slots[0], slots[1]);
1971
+ }
1972
+ oseo_roots_pop(context, &frame);
1973
+ return result;
1974
+ }
1975
+
1976
+ /*
1977
+ * OrdinaryCreateFromConstructor on behalf of a caller that performs
1978
+ * `target.[[Construct]]`'s receiver allocation itself, which is what
1979
+ * both `super()` and `Reflect.construct` do.
1980
+ *
1981
+ * The [[Construct]] that will actually run belongs to the innermost
1982
+ * bound target, and a bound function's [[Construct]] replaces the new
1983
+ * target with its own target whenever the two are the same function, so
1984
+ * the effective new target is found by walking the same chain and
1985
+ * unwrapping only while it still names the new target.
1986
+ *
1987
+ * Which receiver that clause needs then follows from the constructor's
1988
+ * kind. A derived class constructor creates none: 10.2.2 skips
1989
+ * OrdinaryCreateFromConstructor for [[ConstructorKind]] derived and
1990
+ * leaves both the read and the allocation to the `super()` inside its
1991
+ * body, so this reports `undefined` and reads nothing. An ordinary or
1992
+ * base class constructor runs OrdinaryCreateFromConstructor before its
1993
+ * body, so reading `prototype` here is at the specified position and
1994
+ * the read is a real Get: a bound function has no own `prototype`, so a
1995
+ * program can define an accessor one on it and observe it. A built-in
1996
+ * constructor also creates its own, because its clause fixes the
1997
+ * position of its GetPrototypeFromConstructor relative to the argument
1998
+ * conversions that precede it, so this reports `undefined` and reads
1999
+ * nothing there as well.
2000
+ */
2001
+ OseoResult oseo_internal_construct_receiver(
2002
+ OseoContext *context,
2003
+ OseoValue target,
2004
+ OseoValue new_target
2005
+ ) {
2006
+ if (is_proxy(target)) return normal(oseo_undefined());
2007
+ OseoValue effective = new_target;
2008
+ OseoValue constructor = target;
2009
+ while (is_function(constructor) &&
2010
+ function_object(constructor)->function_kind ==
2011
+ OSEO_FUNCTION_BOUND) {
2012
+ if (constructor == effective) {
2013
+ effective = function_object(constructor)->bound_target;
2014
+ }
2015
+ constructor = function_object(constructor)->bound_target;
2016
+ }
2017
+ if (is_proxy(constructor)) return normal(oseo_undefined());
2018
+ if (is_function(constructor) &&
2019
+ function_object(constructor)->derived_constructor) {
2020
+ return normal(oseo_undefined());
2021
+ }
2022
+ size_t code_id = 0u;
2023
+ OseoResult identified = oseo_function_code_id(
2024
+ context,
2025
+ constructor,
2026
+ &code_id
2027
+ );
2028
+ if (identified.status != OSEO_STATUS_NORMAL) return identified;
2029
+ if (oseo_internal_builtin_code_id(code_id)) {
2030
+ return normal(oseo_undefined());
2031
+ }
2032
+ OseoValue slots[2] = {effective, oseo_undefined()};
2033
+ OseoRootFrame frame = {NULL, slots, 2u};
2034
+ oseo_roots_push(context, &frame);
2035
+ OseoResult result = oseo_internal_constructor_prototype(
2036
+ context,
2037
+ slots[0]
2038
+ );
2039
+ /* A `prototype` accessor can return a fresh object, so the read
2040
+ * lands in a rooted slot before the receiver allocation. */
2041
+ slots[1] = result.value;
2042
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[1])) {
2043
+ result = oseo_internal_constructor_realm_default(
2044
+ context,
2045
+ slots[0],
2046
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
2047
+ );
2048
+ slots[1] = result.value;
2049
+ }
2050
+ if (result.status == OSEO_STATUS_NORMAL) {
2051
+ result = oseo_constructor_receiver(context, slots[1]);
2052
+ }
2053
+ oseo_roots_pop(context, &frame);
2054
+ return result;
2055
+ }
2056
+
2057
+ /*
2058
+ * SuperCall's receiver allocation. `parent` is the super constructor
2059
+ * SuperCall step 3 read and step 5 checked; the receiver comes from the
2060
+ * running constructor's new target, and stays undefined when the super
2061
+ * constructor is itself derived and creates its own.
2062
+ */
2063
+ OseoResult oseo_super_constructor_receiver(
2064
+ OseoContext *context,
2065
+ OseoValue parent,
2066
+ OseoValue new_target
2067
+ ) {
2068
+ return oseo_internal_construct_receiver(context, parent, new_target);
2069
+ }
2070
+
2071
+ OseoResult oseo_constructor_result(
2072
+ OseoContext *context,
2073
+ OseoValue returned,
2074
+ OseoValue receiver
2075
+ ) {
2076
+ (void)context;
2077
+ return normal(
2078
+ is_object(returned) || is_promise(returned) ? returned : receiver
2079
+ );
2080
+ }
2081
+
2082
+ /*
2083
+ * Replaces one freshly created object's [[Prototype]]. Both objects a
2084
+ * class definition links are allocated by that definition, so no earlier
2085
+ * shape assumption or intrinsic lookup can depend on the old chain; this
2086
+ * therefore keeps the object out of dictionary mode, unlike the
2087
+ * `Object.setPrototypeOf` path that must assume arbitrary history.
2088
+ */
2089
+ static void relink_prototype(
2090
+ OseoContext *context,
2091
+ OseoValue object_value,
2092
+ OseoValue prototype
2093
+ ) {
2094
+ OseoOrdinaryObject *object = ordinary_object(object_value);
2095
+ object->prototype = prototype;
2096
+ object->shape_id = context->next_shape_id;
2097
+ context->next_shape_id += 1u;
2098
+ }
2099
+
2100
+ OseoResult oseo_class_heritage(
2101
+ OseoContext *context,
2102
+ OseoValue constructor,
2103
+ OseoValue heritage
2104
+ ) {
2105
+ if (!is_function(constructor)) {
2106
+ return failure(context, "OSEO2001", "Class heritage needs a class.");
2107
+ }
2108
+ /* A ClassHeritage, `extends null` included, makes the constructor
2109
+ * derived: its [[Construct]] leaves the receiver to `super()`. */
2110
+ function_object(constructor)->derived_constructor = true;
2111
+ if (tag_of(heritage) == OSEO_TAG_NULL) {
2112
+ OseoValue class_prototype =
2113
+ function_object(constructor)->prototype_object;
2114
+ if (is_object(class_prototype)) {
2115
+ relink_prototype(context, class_prototype, oseo_null());
2116
+ }
2117
+ return normal(constructor);
2118
+ }
2119
+ if (!function_is_constructible(heritage)) {
2120
+ return oseo_internal_throw_error(
2121
+ context,
2122
+ OSEO_ERROR_TYPE,
2123
+ "Class extends value is not a constructor or null."
2124
+ );
2125
+ }
2126
+ OseoRootFrame frame = {NULL, NULL, 0u};
2127
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
2128
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2129
+ frame.slots[0] = constructor;
2130
+ frame.slots[1] = heritage;
2131
+ static const uint16_t prototype_name[] = {
2132
+ 'p', 'r', 'o', 't', 'o', 't', 'y', 'p', 'e',
2133
+ };
2134
+ result = oseo_internal_allocate_string(
2135
+ context,
2136
+ prototype_name,
2137
+ sizeof(prototype_name) / sizeof(*prototype_name)
2138
+ );
2139
+ frame.slots[2] = result.value;
2140
+ if (result.status == OSEO_STATUS_NORMAL) {
2141
+ result = oseo_object_get(context, frame.slots[1], frame.slots[2]);
2142
+ }
2143
+ OseoValue parent_prototype = result.value;
2144
+ if (result.status == OSEO_STATUS_NORMAL &&
2145
+ !is_object(parent_prototype) &&
2146
+ tag_of(parent_prototype) != OSEO_TAG_NULL) {
2147
+ result = oseo_internal_throw_error(
2148
+ context,
2149
+ OSEO_ERROR_TYPE,
2150
+ "Class extends value has a non-object prototype."
2151
+ );
2152
+ }
2153
+ if (result.status == OSEO_STATUS_NORMAL) {
2154
+ OseoValue class_prototype =
2155
+ function_object(frame.slots[0])->prototype_object;
2156
+ if (is_object(class_prototype)) {
2157
+ relink_prototype(context, class_prototype, parent_prototype);
2158
+ }
2159
+ relink_prototype(context, frame.slots[0], frame.slots[1]);
2160
+ result = normal(frame.slots[0]);
2161
+ }
2162
+ oseo_roots_release(context, &frame);
2163
+ return result;
2164
+ }
2165
+
2166
+ /*
2167
+ * Reserves room for one more entry in a class constructor's element
2168
+ * list. The list belongs to the constructor rather than to the heap
2169
+ * object graph, so it grows with the same explicit reallocation the
2170
+ * property table uses, and the caller reacquires the function pointer.
2171
+ */
2172
+ static OseoResult reserve_class_element(
2173
+ OseoContext *context,
2174
+ OseoValue constructor
2175
+ ) {
2176
+ OseoFunction *function = function_object(constructor);
2177
+ if (function->element_count != function->element_capacity) {
2178
+ return normal(constructor);
2179
+ }
2180
+ size_t capacity = function->element_capacity == 0u
2181
+ ? 4u
2182
+ : function->element_capacity * 2u;
2183
+ if (capacity < function->element_capacity ||
2184
+ capacity > SIZE_MAX / sizeof(OseoClassElement)) {
2185
+ return failure(
2186
+ context,
2187
+ "OSEO2001",
2188
+ "Class element storage is too large."
2189
+ );
2190
+ }
2191
+ if (context->collect_every_safepoint) oseo_collect(context);
2192
+ context->allocation_attempts += 1u;
2193
+ OseoClassElement *elements =
2194
+ context->fail_allocation_at != 0u &&
2195
+ context->allocation_attempts == context->fail_allocation_at
2196
+ ? NULL
2197
+ : malloc(capacity * sizeof(*elements));
2198
+ if (elements == NULL) {
2199
+ return failure(
2200
+ context,
2201
+ "OSEO2001",
2202
+ "Class element allocation failed."
2203
+ );
2204
+ }
2205
+ function = function_object(constructor);
2206
+ if (function->element_count > 0u) {
2207
+ memcpy(
2208
+ elements,
2209
+ function->elements,
2210
+ function->element_count * sizeof(*elements)
2211
+ );
2212
+ }
2213
+ free(function->elements);
2214
+ function->elements = elements;
2215
+ function->element_capacity = capacity;
2216
+ return normal(constructor);
2217
+ }
2218
+
2219
+ /* Appends one already-complete record to a constructor's element list. */
2220
+ static OseoResult append_class_element(
2221
+ OseoContext *context,
2222
+ OseoValue constructor,
2223
+ OseoClassElement element
2224
+ ) {
2225
+ if (!is_function(constructor)) {
2226
+ return failure(context, "OSEO2001", "Class elements need a class.");
2227
+ }
2228
+ OseoResult result = reserve_class_element(context, constructor);
2229
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2230
+ OseoFunction *function = function_object(constructor);
2231
+ function->elements[function->element_count] = element;
2232
+ function->element_count += 1u;
2233
+ return normal(constructor);
2234
+ }
2235
+
2236
+ OseoResult oseo_class_field_define(
2237
+ OseoContext *context,
2238
+ OseoValue constructor,
2239
+ OseoValue key,
2240
+ OseoValue initializer
2241
+ ) {
2242
+ OseoClassElement element = {
2243
+ key,
2244
+ initializer,
2245
+ oseo_undefined(),
2246
+ oseo_undefined(),
2247
+ OSEO_CLASS_ELEMENT_FIELD,
2248
+ };
2249
+ return append_class_element(context, constructor, element);
2250
+ }
2251
+
2252
+ OseoResult oseo_class_private_field_define(
2253
+ OseoContext *context,
2254
+ OseoValue constructor,
2255
+ OseoValue name,
2256
+ OseoValue initializer
2257
+ ) {
2258
+ if (!is_private_name(name)) {
2259
+ return failure(
2260
+ context,
2261
+ "OSEO2001",
2262
+ "A private field needs a private name."
2263
+ );
2264
+ }
2265
+ OseoClassElement element = {
2266
+ name,
2267
+ initializer,
2268
+ oseo_undefined(),
2269
+ oseo_undefined(),
2270
+ OSEO_CLASS_ELEMENT_PRIVATE_FIELD,
2271
+ };
2272
+ return append_class_element(context, constructor, element);
2273
+ }
2274
+
2275
+ OseoResult oseo_class_private_method_define(
2276
+ OseoContext *context,
2277
+ OseoValue constructor,
2278
+ OseoValue name,
2279
+ OseoValue value,
2280
+ OseoPrivateMethodKind kind
2281
+ ) {
2282
+ if (!is_function(constructor)) {
2283
+ return failure(context, "OSEO2001", "Class elements need a class.");
2284
+ }
2285
+ if (!is_private_name(name)) {
2286
+ return failure(
2287
+ context,
2288
+ "OSEO2001",
2289
+ "A private method needs a private name."
2290
+ );
2291
+ }
2292
+ if (kind == OSEO_PRIVATE_METHOD) {
2293
+ OseoClassElement element = {
2294
+ name,
2295
+ value,
2296
+ oseo_undefined(),
2297
+ oseo_undefined(),
2298
+ OSEO_CLASS_ELEMENT_PRIVATE_METHOD,
2299
+ };
2300
+ return append_class_element(context, constructor, element);
2301
+ }
2302
+ /* A getter and a setter under one private name describe one
2303
+ * accessor element, so the second half fills the record the first
2304
+ * appended instead of adding a second entry the instance would
2305
+ * reject as a duplicate. */
2306
+ OseoFunction *function = function_object(constructor);
2307
+ for (size_t index = 0u; index < function->element_count; index += 1u) {
2308
+ OseoClassElement *element = &function->elements[index];
2309
+ if (element->kind != OSEO_CLASS_ELEMENT_PRIVATE_ACCESSOR) continue;
2310
+ if (element->key != name) continue;
2311
+ if (kind == OSEO_PRIVATE_GETTER) {
2312
+ element->getter = value;
2313
+ } else {
2314
+ element->setter = value;
2315
+ }
2316
+ return normal(constructor);
2317
+ }
2318
+ OseoClassElement element = {
2319
+ name,
2320
+ oseo_undefined(),
2321
+ kind == OSEO_PRIVATE_GETTER ? value : oseo_undefined(),
2322
+ kind == OSEO_PRIVATE_SETTER ? value : oseo_undefined(),
2323
+ OSEO_CLASS_ELEMENT_PRIVATE_ACCESSOR,
2324
+ };
2325
+ return append_class_element(context, constructor, element);
2326
+ }
2327
+
2328
+ /*
2329
+ * Reserves room for one more entry in an object's [[PrivateElements]].
2330
+ * The list is per-instance and grows only while a constructor installs
2331
+ * the elements its class declared.
2332
+ */
2333
+ static OseoResult reserve_private_element(
2334
+ OseoContext *context,
2335
+ OseoValue object
2336
+ ) {
2337
+ OseoOrdinaryObject *target = ordinary_object(object);
2338
+ if (target->private_element_count != target->private_element_capacity) {
2339
+ return normal(object);
2340
+ }
2341
+ size_t capacity = target->private_element_capacity == 0u
2342
+ ? 4u
2343
+ : target->private_element_capacity * 2u;
2344
+ if (capacity < target->private_element_capacity ||
2345
+ capacity > SIZE_MAX / sizeof(OseoPrivateElement)) {
2346
+ return failure(
2347
+ context,
2348
+ "OSEO2001",
2349
+ "Private element storage is too large."
2350
+ );
2351
+ }
2352
+ if (context->collect_every_safepoint) oseo_collect(context);
2353
+ context->allocation_attempts += 1u;
2354
+ OseoPrivateElement *elements =
2355
+ context->fail_allocation_at != 0u &&
2356
+ context->allocation_attempts == context->fail_allocation_at
2357
+ ? NULL
2358
+ : malloc(capacity * sizeof(*elements));
2359
+ if (elements == NULL) {
2360
+ return failure(
2361
+ context,
2362
+ "OSEO2001",
2363
+ "Private element allocation failed."
2364
+ );
2365
+ }
2366
+ target = ordinary_object(object);
2367
+ if (target->private_element_count > 0u) {
2368
+ memcpy(
2369
+ elements,
2370
+ target->private_elements,
2371
+ target->private_element_count * sizeof(*elements)
2372
+ );
2373
+ }
2374
+ free(target->private_elements);
2375
+ target->private_elements = elements;
2376
+ target->private_element_capacity = capacity;
2377
+ return normal(object);
2378
+ }
2379
+
2380
+ /* PrivateElementFind: the entry `object` carries under `name`. */
2381
+ static OseoPrivateElement *find_private_element(
2382
+ OseoValue object,
2383
+ OseoValue name
2384
+ ) {
2385
+ if (!is_object(object)) return NULL;
2386
+ OseoOrdinaryObject *target = ordinary_object(object);
2387
+ for (size_t index = 0u;
2388
+ index < target->private_element_count;
2389
+ index += 1u) {
2390
+ if (target->private_elements[index].key == name) {
2391
+ return &target->private_elements[index];
2392
+ }
2393
+ }
2394
+ return NULL;
2395
+ }
2396
+
2397
+ /* Appends one private element, which PrivateElementFind must not find. */
2398
+ static OseoResult add_private_element(
2399
+ OseoContext *context,
2400
+ OseoValue object,
2401
+ OseoPrivateElement element
2402
+ ) {
2403
+ if (!is_object(object)) {
2404
+ return oseo_internal_throw_error(
2405
+ context,
2406
+ OSEO_ERROR_TYPE,
2407
+ "A private element needs an object."
2408
+ );
2409
+ }
2410
+ if (find_private_element(object, element.key) != NULL) {
2411
+ return oseo_internal_throw_error(
2412
+ context,
2413
+ OSEO_ERROR_TYPE,
2414
+ "This object already carries that private member."
2415
+ );
2416
+ }
2417
+ OseoRootFrame frame = {NULL, NULL, 0u};
2418
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2419
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2420
+ frame.slots[0] = object;
2421
+ frame.slots[1] = element.key;
2422
+ frame.slots[2] = element.value;
2423
+ frame.slots[3] = element.getter;
2424
+ frame.slots[4] = element.setter;
2425
+ result = reserve_private_element(context, frame.slots[0]);
2426
+ if (result.status == OSEO_STATUS_NORMAL) {
2427
+ OseoOrdinaryObject *target = ordinary_object(frame.slots[0]);
2428
+ OseoPrivateElement stored = {
2429
+ frame.slots[1],
2430
+ frame.slots[2],
2431
+ frame.slots[3],
2432
+ frame.slots[4],
2433
+ element.kind,
2434
+ };
2435
+ target->private_elements[target->private_element_count] = stored;
2436
+ target->private_element_count += 1u;
2437
+ result = normal(frame.slots[0]);
2438
+ }
2439
+ oseo_roots_release(context, &frame);
2440
+ return result;
2441
+ }
2442
+
2443
+ OseoResult oseo_class_static_private_method_define(
2444
+ OseoContext *context,
2445
+ OseoValue constructor,
2446
+ OseoValue name,
2447
+ OseoValue value,
2448
+ OseoPrivateMethodKind kind
2449
+ ) {
2450
+ if (!is_function(constructor)) {
2451
+ return failure(context, "OSEO2001", "Class elements need a class.");
2452
+ }
2453
+ if (!is_private_name(name)) {
2454
+ return failure(
2455
+ context,
2456
+ "OSEO2001",
2457
+ "A static private method needs a private name."
2458
+ );
2459
+ }
2460
+ if (kind == OSEO_PRIVATE_METHOD) {
2461
+ OseoPrivateElement added = {
2462
+ name,
2463
+ value,
2464
+ oseo_undefined(),
2465
+ oseo_undefined(),
2466
+ OSEO_PRIVATE_ELEMENT_METHOD,
2467
+ };
2468
+ return add_private_element(context, constructor, added);
2469
+ }
2470
+ if (kind != OSEO_PRIVATE_GETTER && kind != OSEO_PRIVATE_SETTER) {
2471
+ return failure(
2472
+ context,
2473
+ "OSEO2001",
2474
+ "A static private accessor needs a valid half."
2475
+ );
2476
+ }
2477
+ OseoPrivateElement *existing = find_private_element(constructor, name);
2478
+ if (existing != NULL) {
2479
+ if (existing->kind != OSEO_PRIVATE_ELEMENT_ACCESSOR) {
2480
+ return failure(
2481
+ context,
2482
+ "OSEO2001",
2483
+ "A static private accessor conflicts with another element."
2484
+ );
2485
+ }
2486
+ OseoValue *slot =
2487
+ kind == OSEO_PRIVATE_GETTER ? &existing->getter : &existing->setter;
2488
+ if (tag_of(*slot) != OSEO_TAG_UNDEFINED) {
2489
+ return failure(
2490
+ context,
2491
+ "OSEO2001",
2492
+ "A static private accessor half is already defined."
2493
+ );
2494
+ }
2495
+ *slot = value;
2496
+ return normal(constructor);
2497
+ }
2498
+ OseoPrivateElement added = {
2499
+ name,
2500
+ oseo_undefined(),
2501
+ kind == OSEO_PRIVATE_GETTER ? value : oseo_undefined(),
2502
+ kind == OSEO_PRIVATE_SETTER ? value : oseo_undefined(),
2503
+ OSEO_PRIVATE_ELEMENT_ACCESSOR,
2504
+ };
2505
+ return add_private_element(context, constructor, added);
2506
+ }
2507
+
2508
+ OseoResult oseo_private_name_create(OseoContext *context) {
2509
+ OseoPrivateName *name =
2510
+ oseo_internal_allocate_heap_bytes(context, sizeof(*name));
2511
+ if (name == NULL) {
2512
+ return failure(context, "OSEO2001", "Private name allocation failed.");
2513
+ }
2514
+ return oseo_internal_publish_heap(
2515
+ context,
2516
+ &name->header,
2517
+ OSEO_HEAP_PRIVATE_NAME
2518
+ );
2519
+ }
2520
+
2521
+ OseoResult oseo_private_name_bind(
2522
+ OseoContext *context,
2523
+ OseoValue environment,
2524
+ size_t binding_id
2525
+ ) {
2526
+ OseoValue slots[] = {environment, oseo_undefined()};
2527
+ OseoRootFrame frame = {NULL, slots, 2u};
2528
+ oseo_roots_push(context, &frame);
2529
+ OseoResult result = oseo_cell_create(context, oseo_uninitialized());
2530
+ if (result.status == OSEO_STATUS_NORMAL) {
2531
+ slots[1] = result.value;
2532
+ result = oseo_environment_set(
2533
+ context, slots[0], binding_id, slots[1]
2534
+ );
2535
+ }
2536
+ if (result.status == OSEO_STATUS_NORMAL) {
2537
+ result = oseo_private_name_create(context);
2538
+ if (result.status == OSEO_STATUS_NORMAL) {
2539
+ /* Cell initialization cannot allocate or invoke user code. */
2540
+ result = oseo_cell_initialize(context, slots[1], result.value);
2541
+ }
2542
+ }
2543
+ oseo_roots_pop(context, &frame);
2544
+ return result;
2545
+ }
2546
+
2547
+ OseoResult oseo_private_get(
2548
+ OseoContext *context,
2549
+ OseoValue object,
2550
+ OseoValue name
2551
+ ) {
2552
+ if (!is_private_name(name)) {
2553
+ return failure(
2554
+ context,
2555
+ "OSEO2001",
2556
+ "A private reference needs a private name."
2557
+ );
2558
+ }
2559
+ /* GetValue converts the base with ToObject first, and no primitive
2560
+ * wrapper carries a private element, so every non-object base ends
2561
+ * in the same TypeError this reports directly. */
2562
+ OseoPrivateElement *element = find_private_element(object, name);
2563
+ if (element == NULL) {
2564
+ return oseo_internal_throw_error(
2565
+ context,
2566
+ OSEO_ERROR_TYPE,
2567
+ "This object does not carry that private member."
2568
+ );
2569
+ }
2570
+ if (element->kind != OSEO_PRIVATE_ELEMENT_ACCESSOR) {
2571
+ return normal(element->value);
2572
+ }
2573
+ OseoValue getter = element->getter;
2574
+ if (tag_of(getter) == OSEO_TAG_UNDEFINED) {
2575
+ return oseo_internal_throw_error(
2576
+ context,
2577
+ OSEO_ERROR_TYPE,
2578
+ "This private accessor declares no getter."
2579
+ );
2580
+ }
2581
+ return oseo_call_function(
2582
+ context,
2583
+ getter,
2584
+ object,
2585
+ 0u,
2586
+ NULL,
2587
+ oseo_undefined()
2588
+ );
2589
+ }
2590
+
2591
+ OseoResult oseo_private_in(
2592
+ OseoContext *context,
2593
+ OseoValue object,
2594
+ OseoValue name
2595
+ ) {
2596
+ if (!is_private_name(name)) {
2597
+ return failure(
2598
+ context,
2599
+ "OSEO2001",
2600
+ "A private brand check needs a private name."
2601
+ );
2602
+ }
2603
+ if (!is_object(object)) {
2604
+ return oseo_internal_throw_error(
2605
+ context,
2606
+ OSEO_ERROR_TYPE,
2607
+ "A private brand check needs an object."
2608
+ );
2609
+ }
2610
+ return normal(oseo_boolean(find_private_element(object, name) != NULL));
2611
+ }
2612
+
2613
+ OseoResult oseo_private_set(
2614
+ OseoContext *context,
2615
+ OseoValue object,
2616
+ OseoValue name,
2617
+ OseoValue value
2618
+ ) {
2619
+ if (!is_private_name(name)) {
2620
+ return failure(
2621
+ context,
2622
+ "OSEO2001",
2623
+ "A private reference needs a private name."
2624
+ );
2625
+ }
2626
+ OseoPrivateElement *element = find_private_element(object, name);
2627
+ if (element == NULL) {
2628
+ return oseo_internal_throw_error(
2629
+ context,
2630
+ OSEO_ERROR_TYPE,
2631
+ "This object does not carry that private member."
2632
+ );
2633
+ }
2634
+ if (element->kind == OSEO_PRIVATE_ELEMENT_FIELD) {
2635
+ element->value = value;
2636
+ return normal(value);
2637
+ }
2638
+ if (element->kind == OSEO_PRIVATE_ELEMENT_METHOD) {
2639
+ return oseo_internal_throw_error(
2640
+ context,
2641
+ OSEO_ERROR_TYPE,
2642
+ "A private method cannot be assigned."
2643
+ );
2644
+ }
2645
+ OseoValue setter = element->setter;
2646
+ if (tag_of(setter) == OSEO_TAG_UNDEFINED) {
2647
+ return oseo_internal_throw_error(
2648
+ context,
2649
+ OSEO_ERROR_TYPE,
2650
+ "This private accessor declares no setter."
2651
+ );
2652
+ }
2653
+ OseoRootFrame frame = {NULL, NULL, 0u};
2654
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
2655
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2656
+ frame.slots[0] = value;
2657
+ result = oseo_call_function(
2658
+ context,
2659
+ setter,
2660
+ object,
2661
+ 1u,
2662
+ &frame.slots[0],
2663
+ oseo_undefined()
2664
+ );
2665
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[0]);
2666
+ oseo_roots_release(context, &frame);
2667
+ return result;
2668
+ }
2669
+
2670
+ OseoResult oseo_initialize_instance_elements(
2671
+ OseoContext *context,
2672
+ OseoValue constructor,
2673
+ OseoValue instance
2674
+ ) {
2675
+ if (!is_function(constructor)) {
2676
+ return failure(
2677
+ context,
2678
+ "OSEO2001",
2679
+ "Instance elements need a class constructor."
2680
+ );
2681
+ }
2682
+ if (function_object(constructor)->element_count == 0u) {
2683
+ return normal(instance);
2684
+ }
2685
+ OseoRootFrame frame = {NULL, NULL, 0u};
2686
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
2687
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2688
+ frame.slots[0] = constructor;
2689
+ frame.slots[1] = instance;
2690
+ /* Every private method and accessor is installed before any field
2691
+ * initializer runs, so an initializer can already call `this.#m()`
2692
+ * even when the method is declared after it. */
2693
+ for (size_t index = 0u;
2694
+ result.status == OSEO_STATUS_NORMAL &&
2695
+ index < function_object(frame.slots[0])->element_count;
2696
+ index += 1u) {
2697
+ /* The list is complete before any instance exists, so re-reading
2698
+ * it only guards against a reallocation this loop cannot cause. */
2699
+ OseoClassElement element =
2700
+ function_object(frame.slots[0])->elements[index];
2701
+ if (element.kind != OSEO_CLASS_ELEMENT_PRIVATE_METHOD &&
2702
+ element.kind != OSEO_CLASS_ELEMENT_PRIVATE_ACCESSOR) {
2703
+ continue;
2704
+ }
2705
+ frame.slots[2] = element.key;
2706
+ frame.slots[3] = element.value;
2707
+ frame.slots[4] = element.getter;
2708
+ frame.slots[5] = element.setter;
2709
+ OseoPrivateElement installed = {
2710
+ frame.slots[2],
2711
+ frame.slots[3],
2712
+ frame.slots[4],
2713
+ frame.slots[5],
2714
+ element.kind == OSEO_CLASS_ELEMENT_PRIVATE_METHOD
2715
+ ? OSEO_PRIVATE_ELEMENT_METHOD
2716
+ : OSEO_PRIVATE_ELEMENT_ACCESSOR,
2717
+ };
2718
+ result = add_private_element(context, frame.slots[1], installed);
2719
+ }
2720
+ for (size_t index = 0u;
2721
+ result.status == OSEO_STATUS_NORMAL &&
2722
+ index < function_object(frame.slots[0])->element_count;
2723
+ index += 1u) {
2724
+ OseoClassElement element =
2725
+ function_object(frame.slots[0])->elements[index];
2726
+ if (element.kind != OSEO_CLASS_ELEMENT_FIELD &&
2727
+ element.kind != OSEO_CLASS_ELEMENT_PRIVATE_FIELD) {
2728
+ continue;
2729
+ }
2730
+ frame.slots[2] = element.key;
2731
+ frame.slots[3] = element.value;
2732
+ frame.slots[4] = oseo_undefined();
2733
+ if (tag_of(frame.slots[3]) != OSEO_TAG_UNDEFINED) {
2734
+ result = oseo_call_function(
2735
+ context,
2736
+ frame.slots[3],
2737
+ frame.slots[1],
2738
+ 0u,
2739
+ NULL,
2740
+ oseo_undefined()
2741
+ );
2742
+ frame.slots[4] = result.value;
2743
+ if (result.status != OSEO_STATUS_NORMAL) break;
2744
+ }
2745
+ if (element.kind == OSEO_CLASS_ELEMENT_PRIVATE_FIELD) {
2746
+ OseoPrivateElement added = {
2747
+ frame.slots[2],
2748
+ frame.slots[4],
2749
+ oseo_undefined(),
2750
+ oseo_undefined(),
2751
+ OSEO_PRIVATE_ELEMENT_FIELD,
2752
+ };
2753
+ result = add_private_element(context, frame.slots[1], added);
2754
+ continue;
2755
+ }
2756
+ OseoPropertyAttributes attributes = {true, true, true, false};
2757
+ result = oseo_object_define(
2758
+ context,
2759
+ frame.slots[1],
2760
+ frame.slots[2],
2761
+ frame.slots[4],
2762
+ attributes
2763
+ );
2764
+ }
2765
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[1]);
2766
+ oseo_roots_release(context, &frame);
2767
+ return result;
2768
+ }
2769
+
2770
+ /*
2771
+ * DefineField against a class constructor, which is what a `static`
2772
+ * field definition performs once the class is otherwise complete. The
2773
+ * initializer runs with the constructor as its receiver, so `this`
2774
+ * inside it is the class, and its result is installed as a property or
2775
+ * as a private element the constructor carries.
2776
+ */
2777
+ static OseoResult define_static_field(
2778
+ OseoContext *context,
2779
+ OseoValue constructor,
2780
+ OseoValue key,
2781
+ OseoValue initializer,
2782
+ bool private_element
2783
+ ) {
2784
+ if (!is_function(constructor)) {
2785
+ return failure(context, "OSEO2001", "Class elements need a class.");
2786
+ }
2787
+ if (private_element != is_private_name(key)) {
2788
+ return failure(
2789
+ context,
2790
+ "OSEO2001",
2791
+ "A static field key does not match its placement."
2792
+ );
2793
+ }
2794
+ OseoRootFrame frame = {NULL, NULL, 0u};
2795
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
2796
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2797
+ frame.slots[0] = constructor;
2798
+ frame.slots[1] = key;
2799
+ frame.slots[2] = initializer;
2800
+ frame.slots[3] = oseo_undefined();
2801
+ if (tag_of(frame.slots[2]) != OSEO_TAG_UNDEFINED) {
2802
+ result = oseo_call_function(
2803
+ context,
2804
+ frame.slots[2],
2805
+ frame.slots[0],
2806
+ 0u,
2807
+ NULL,
2808
+ oseo_undefined()
2809
+ );
2810
+ frame.slots[3] = result.value;
2811
+ }
2812
+ if (result.status == OSEO_STATUS_NORMAL) {
2813
+ if (private_element) {
2814
+ OseoPrivateElement added = {
2815
+ frame.slots[1],
2816
+ frame.slots[3],
2817
+ oseo_undefined(),
2818
+ oseo_undefined(),
2819
+ OSEO_PRIVATE_ELEMENT_FIELD,
2820
+ };
2821
+ result = add_private_element(context, frame.slots[0], added);
2822
+ } else {
2823
+ OseoPropertyAttributes attributes = {true, true, true, false};
2824
+ result = oseo_object_define(
2825
+ context,
2826
+ frame.slots[0],
2827
+ frame.slots[1],
2828
+ frame.slots[3],
2829
+ attributes
2830
+ );
2831
+ }
2832
+ }
2833
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[0]);
2834
+ oseo_roots_release(context, &frame);
2835
+ return result;
2836
+ }
2837
+
2838
+ OseoResult oseo_class_static_field_define(
2839
+ OseoContext *context,
2840
+ OseoValue constructor,
2841
+ OseoValue key,
2842
+ OseoValue initializer
2843
+ ) {
2844
+ return define_static_field(context, constructor, key, initializer, false);
2845
+ }
2846
+
2847
+ OseoResult oseo_class_static_private_field_define(
2848
+ OseoContext *context,
2849
+ OseoValue constructor,
2850
+ OseoValue name,
2851
+ OseoValue initializer
2852
+ ) {
2853
+ return define_static_field(context, constructor, name, initializer, true);
2854
+ }
2855
+
2856
+ void oseo_bind_home_object(OseoValue function, OseoValue home) {
2857
+ if (!is_function(function)) return;
2858
+ function_object(function)->home_object = home;
2859
+ }
2860
+
2861
+ void oseo_bind_arrow_context(
2862
+ OseoValue function,
2863
+ OseoValue enclosing,
2864
+ OseoValue new_target
2865
+ ) {
2866
+ if (!function_has_lexical_this(function)) return;
2867
+ OseoFunction *arrow = function_object(function);
2868
+ arrow->lexical_new_target = new_target;
2869
+ if (!is_function(enclosing)) return;
2870
+ OseoFunction *outer = function_object(enclosing);
2871
+ arrow->home_object = outer->home_object;
2872
+ arrow->lexical_super = function_has_lexical_this(enclosing)
2873
+ ? outer->lexical_super
2874
+ : enclosing;
2875
+ }
2876
+
2877
+ OseoValue oseo_super_base(OseoValue callee) {
2878
+ if (!is_function(callee)) return oseo_undefined();
2879
+ OseoValue home = function_object(callee)->home_object;
2880
+ if (!is_object(home)) return oseo_undefined();
2881
+ return ordinary_object(home)->prototype;
2882
+ }
2883
+
2884
+ OseoResult oseo_super_property_delete(OseoContext *context) {
2885
+ return oseo_internal_throw_error(
2886
+ context,
2887
+ OSEO_ERROR_REFERENCE,
2888
+ "Cannot delete a super property reference."
2889
+ );
2890
+ }
2891
+
2892
+ OseoResult oseo_super_constructor(OseoContext *context, OseoValue callee) {
2893
+ (void)context;
2894
+ OseoValue context_function = callee;
2895
+ if (function_has_lexical_this(callee)) {
2896
+ context_function = function_object(callee)->lexical_super;
2897
+ }
2898
+ OseoValue parent = is_function(context_function)
2899
+ ? function_object(context_function)->ordinary.prototype
2900
+ : oseo_undefined();
2901
+ return normal(parent);
2902
+ }
2903
+
2904
+ OseoResult oseo_super_constructor_check(
2905
+ OseoContext *context,
2906
+ OseoValue parent
2907
+ ) {
2908
+ if (!function_is_constructible(parent)) {
2909
+ return oseo_internal_throw_error(
2910
+ context,
2911
+ OSEO_ERROR_TYPE,
2912
+ "Super constructor is not a constructor."
2913
+ );
2914
+ }
2915
+ return normal(parent);
2916
+ }
2917
+
2918
+ OseoResult oseo_bind_this(
2919
+ OseoContext *context,
2920
+ OseoValue cell,
2921
+ OseoValue value
2922
+ ) {
2923
+ if (!is_cell(cell)) {
2924
+ return failure(context, "OSEO2001", "Value is not a binding cell.");
2925
+ }
2926
+ if (tag_of(cell_object(cell)->value) != OSEO_TAG_UNINITIALIZED) {
2927
+ return oseo_internal_throw_error(
2928
+ context,
2929
+ OSEO_ERROR_REFERENCE,
2930
+ "Super constructor was already called in this constructor."
2931
+ );
2932
+ }
2933
+ cell_object(cell)->value = value;
2934
+ return normal(value);
2935
+ }
2936
+
2937
+ static OseoResult derived_result_in_realm(
2938
+ OseoContext *context,
2939
+ OseoValue returned,
2940
+ OseoValue cell,
2941
+ OseoValue realm
2942
+ ) {
2943
+ if (is_object(returned) || is_promise(returned)) return normal(returned);
2944
+ /* Construct resumes the caller before interpreting the body's normal
2945
+ * completion. Body throws and this reads still use the callee realm. */
2946
+ OseoRealmScope scope;
2947
+ oseo_internal_realm_enter(context, &scope, realm);
2948
+ OseoResult result;
2949
+ if (tag_of(returned) != OSEO_TAG_UNDEFINED) {
2950
+ result = oseo_internal_throw_error(
2951
+ context,
2952
+ OSEO_ERROR_TYPE,
2953
+ "Derived constructor returned a non-object value."
2954
+ );
2955
+ } else {
2956
+ result = oseo_cell_get(context, cell);
2957
+ }
2958
+ oseo_internal_realm_leave(context, &scope);
2959
+ return result;
2960
+ }
2961
+
2962
+ OseoResult oseo_derived_constructor_result(
2963
+ OseoContext *context,
2964
+ OseoValue returned,
2965
+ OseoValue cell
2966
+ ) {
2967
+ OseoValue realm = context->constructor_scope == NULL
2968
+ ? context->realm_record
2969
+ : context->constructor_scope->realm_record;
2970
+ return derived_result_in_realm(context, returned, cell, realm);
2971
+ }
2972
+
2973
+ static OseoResult call_bound_function(
2974
+ OseoContext *context,
2975
+ OseoValue callee,
2976
+ OseoValue receiver,
2977
+ size_t argument_count,
2978
+ const OseoValue *arguments,
2979
+ OseoValue new_target
2980
+ ) {
2981
+ OseoRootFrame frame = {NULL, NULL, 0u};
2982
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2983
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2984
+ frame.slots[0] = callee;
2985
+ frame.slots[1] = receiver;
2986
+ frame.slots[2] = new_target;
2987
+ frame.slots[3] = function_object(frame.slots[0])->bound_target;
2988
+ frame.slots[4] = function_object(frame.slots[0])->bound_this;
2989
+ OseoValue combined = oseo_undefined();
2990
+ OseoRootFrame combined_frame = {NULL, &combined, 1u};
2991
+ oseo_roots_push(context, &combined_frame);
2992
+ result = oseo_argument_list_create(context);
2993
+ combined = result.value;
2994
+ size_t bound_count = 0u;
2995
+ const OseoValue *bound_values = NULL;
2996
+ if (result.status == OSEO_STATUS_NORMAL) {
2997
+ result = oseo_argument_list_view(
2998
+ context,
2999
+ function_object(frame.slots[0])->bound_arguments,
3000
+ &bound_count,
3001
+ &bound_values
3002
+ );
3003
+ }
3004
+ for (size_t index = 0u;
3005
+ result.status == OSEO_STATUS_NORMAL && index < bound_count;
3006
+ index += 1u) {
3007
+ result = oseo_argument_list_append(
3008
+ context,
3009
+ combined,
3010
+ bound_values[index]
3011
+ );
3012
+ }
3013
+ for (size_t index = 0u;
3014
+ result.status == OSEO_STATUS_NORMAL && index < argument_count;
3015
+ index += 1u) {
3016
+ result = oseo_argument_list_append(
3017
+ context,
3018
+ combined,
3019
+ arguments[index]
3020
+ );
3021
+ }
3022
+ size_t combined_count = 0u;
3023
+ const OseoValue *combined_values = NULL;
3024
+ if (result.status == OSEO_STATUS_NORMAL) {
3025
+ result = oseo_argument_list_view(
3026
+ context,
3027
+ combined,
3028
+ &combined_count,
3029
+ &combined_values
3030
+ );
3031
+ }
3032
+ bool constructing = tag_of(frame.slots[2]) != OSEO_TAG_UNDEFINED;
3033
+ if (result.status == OSEO_STATUS_NORMAL && !constructing) {
3034
+ /* A bound function's [[Call]] creates no execution context and
3035
+ * returns the target's result unchanged, so it forwards through
3036
+ * the trampoline instead of nesting one more native call. */
3037
+ result = oseo_internal_tail_call(
3038
+ context,
3039
+ frame.slots[3],
3040
+ frame.slots[4],
3041
+ combined_count,
3042
+ combined_values
3043
+ );
3044
+ } else if (result.status == OSEO_STATUS_NORMAL) {
3045
+ OseoValue forwarded_target = frame.slots[2] == frame.slots[0]
3046
+ ? frame.slots[3]
3047
+ : frame.slots[2];
3048
+ result = oseo_call_function(
3049
+ context,
3050
+ frame.slots[3],
3051
+ frame.slots[1],
3052
+ combined_count,
3053
+ combined_values,
3054
+ forwarded_target
3055
+ );
3056
+ }
3057
+ oseo_roots_pop(context, &combined_frame);
3058
+ oseo_roots_release(context, &frame);
3059
+ return result;
3060
+ }
3061
+
3062
+ /*
3063
+ * Function.prototype.call, Function.prototype.apply, Reflect.apply, and
3064
+ * Reflect.construct: the built-ins whose call counts as made by the code
3065
+ * that called them.
3066
+ */
3067
+ static bool forwarding_builtin(size_t code_id) {
3068
+ return code_id == OSEO_FUNCTION_CALL_CODE_ID ||
3069
+ code_id == OSEO_FUNCTION_APPLY_CODE_ID ||
3070
+ code_id == OSEO_REFLECT_APPLY_CODE_ID ||
3071
+ code_id == OSEO_REFLECT_CONSTRUCT_CODE_ID;
3072
+ }
3073
+
3074
+ /*
3075
+ * The part of [[Call]] and [[Construct]] that runs once the callee's
3076
+ * realm, if it has one, is the running realm.
3077
+ */
3078
+ static OseoResult call_in_running_realm(
3079
+ OseoContext *context,
3080
+ OseoValue callee,
3081
+ OseoValue receiver,
3082
+ size_t argument_count,
3083
+ const OseoValue *arguments,
3084
+ OseoValue new_target
3085
+ ) {
3086
+ /* A class constructor has [[IsClassConstructor]] true, so [[Call]]
3087
+ * always throws; only [[Construct]], which supplies a new target,
3088
+ * reaches its body. */
3089
+ if (is_function(callee) &&
3090
+ function_object(callee)->function_kind == OSEO_FUNCTION_CLASS &&
3091
+ tag_of(new_target) == OSEO_TAG_UNDEFINED) {
3092
+ return oseo_internal_throw_error(
3093
+ context,
3094
+ OSEO_ERROR_TYPE,
3095
+ "Class constructor cannot be invoked without 'new'."
3096
+ );
3097
+ }
3098
+ if (function_has_lexical_this(callee)) {
3099
+ OseoFunction *function = function_object(callee);
3100
+ receiver = function->lexical_this;
3101
+ new_target = function->lexical_new_target;
3102
+ }
3103
+ if (is_function(callee) &&
3104
+ function_object(callee)->function_kind == OSEO_FUNCTION_BOUND) {
3105
+ OseoResult entered = oseo_call_enter(context);
3106
+ if (entered.status != OSEO_STATUS_NORMAL) return entered;
3107
+ OseoResult result = call_bound_function(
3108
+ context,
3109
+ callee,
3110
+ receiver,
3111
+ argument_count,
3112
+ arguments,
3113
+ new_target
3114
+ );
3115
+ oseo_call_leave(context);
3116
+ return result;
3117
+ }
3118
+ size_t code_id = 0u;
3119
+ OseoResult result = oseo_function_code_id(context, callee, &code_id);
3120
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3121
+ result = oseo_call_enter(context);
3122
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3123
+ OseoBuiltinDispatcher dispatch = builtin_dispatcher(code_id);
3124
+ /* The four forwarding built-ins record no activation, so the call
3125
+ * they make counts as made by their own caller (ADR 0034). Every
3126
+ * other built-in does, and reports null to the functions it calls. */
3127
+ bool tracked = dispatch == NULL || !forwarding_builtin(code_id);
3128
+ OseoActivation activation;
3129
+ if (tracked) {
3130
+ oseo_internal_activation_push(
3131
+ context,
3132
+ &activation,
3133
+ callee,
3134
+ argument_count,
3135
+ arguments
3136
+ );
3137
+ }
3138
+ if (dispatch != NULL) {
3139
+ result = dispatch(
3140
+ context,
3141
+ code_id,
3142
+ callee,
3143
+ receiver,
3144
+ argument_count,
3145
+ arguments,
3146
+ new_target
3147
+ );
3148
+ } else if (context->function_dispatcher == NULL) {
3149
+ result = failure(
3150
+ context,
3151
+ "OSEO2001",
3152
+ "No generated function dispatcher is installed."
3153
+ );
3154
+ } else {
3155
+ result = context->function_dispatcher(
3156
+ context,
3157
+ callee,
3158
+ receiver,
3159
+ argument_count,
3160
+ arguments,
3161
+ new_target
3162
+ );
3163
+ OseoFunctionKind kind = function_object(callee)->function_kind;
3164
+ bool async_function = kind == OSEO_FUNCTION_ASYNC ||
3165
+ kind == OSEO_FUNCTION_ASYNC_ARROW;
3166
+ if (async_function && result.status == OSEO_STATUS_NORMAL) {
3167
+ result = oseo_async_function_start(context, result.value);
3168
+ } else if (async_function &&
3169
+ result.status == OSEO_STATUS_THROW &&
3170
+ !context->has_diagnostic) {
3171
+ OseoValue reason = result.value;
3172
+ oseo_context_clear_language_error(context);
3173
+ result = oseo_async_function_reject(context, reason);
3174
+ } else if ((kind == OSEO_FUNCTION_GENERATOR ||
3175
+ kind == OSEO_FUNCTION_ASYNC_GENERATOR) &&
3176
+ result.status == OSEO_STATUS_NORMAL) {
3177
+ /* EvaluateBody reads the function's `prototype` after
3178
+ * FunctionDeclarationInstantiation, which the prologue has
3179
+ * just finished. */
3180
+ result = oseo_internal_generator_created(
3181
+ context,
3182
+ callee,
3183
+ result.value
3184
+ );
3185
+ }
3186
+ }
3187
+ if (tracked) oseo_internal_activation_pop(context, &activation);
3188
+ oseo_call_leave(context);
3189
+ return result;
3190
+ }
3191
+
3192
+ /*
3193
+ * One [[Call]] or [[Construct]] step. Its result may carry
3194
+ * OSEO_STATUS_TAIL_CALL when the callee, or a bound function, Proxy, or
3195
+ * built-in forwarding on its behalf, ended in a tail call; only the
3196
+ * trampoline below and its public wrappers see that status.
3197
+ */
3198
+ static OseoResult call_once(
3199
+ OseoContext *context,
3200
+ OseoValue callee,
3201
+ OseoValue receiver,
3202
+ size_t argument_count,
3203
+ const OseoValue *arguments,
3204
+ OseoValue new_target
3205
+ ) {
3206
+ if (is_proxy(callee)) {
3207
+ OseoResult entered = oseo_call_enter(context);
3208
+ if (entered.status != OSEO_STATUS_NORMAL) return entered;
3209
+ OseoResult result = oseo_internal_proxy_call(
3210
+ context,
3211
+ callee,
3212
+ receiver,
3213
+ argument_count,
3214
+ arguments,
3215
+ new_target
3216
+ );
3217
+ oseo_call_leave(context);
3218
+ return result;
3219
+ }
3220
+ if (!is_function(callee) ||
3221
+ function_object(callee)->function_kind == OSEO_FUNCTION_BOUND) {
3222
+ return call_in_running_realm(
3223
+ context,
3224
+ callee,
3225
+ receiver,
3226
+ argument_count,
3227
+ arguments,
3228
+ new_target
3229
+ );
3230
+ }
3231
+ /* PrepareForOrdinaryCall and a built-in function's [[Call]] both make
3232
+ * the callee's [[Realm]] the running realm, and the caller's realm is
3233
+ * restored when the callee returns or throws. A bound function or a
3234
+ * Proxy has no realm of its own and never switches. */
3235
+ OseoConstructorScope constructor_scope = {
3236
+ context->realm_record, context->constructor_scope
3237
+ };
3238
+ context->constructor_scope = &constructor_scope;
3239
+ OseoRealmScope scope;
3240
+ oseo_internal_realm_enter(context, &scope, function_object(callee)->realm);
3241
+ OseoResult result = call_in_running_realm(
3242
+ context,
3243
+ callee,
3244
+ receiver,
3245
+ argument_count,
3246
+ arguments,
3247
+ new_target
3248
+ );
3249
+ oseo_internal_realm_leave(context, &scope);
3250
+ context->constructor_scope = constructor_scope.previous;
3251
+ return result;
3252
+ }
3253
+
3254
+ static OseoTailCall *tail_call_record(OseoContext *context) {
3255
+ if (context->tail_call == NULL) {
3256
+ OseoTailCall *record = malloc(sizeof(OseoTailCall));
3257
+ if (record == NULL) return NULL;
3258
+ record->callee = oseo_undefined();
3259
+ record->receiver = oseo_undefined();
3260
+ record->realm_record = oseo_undefined();
3261
+ record->this_cell = oseo_undefined();
3262
+ record->constructor_realm = oseo_undefined();
3263
+ record->caller = oseo_undefined();
3264
+ record->arguments = NULL;
3265
+ record->argument_count = 0u;
3266
+ record->argument_capacity = 0u;
3267
+ record->host = 0u;
3268
+ record->pending = false;
3269
+ record->derived = false;
3270
+ context->tail_call = record;
3271
+ }
3272
+ return context->tail_call;
3273
+ }
3274
+
3275
+ /*
3276
+ * Stores one owed call. Nothing runs between a recording site and the
3277
+ * trampoline that takes the record, so a record that is already pending
3278
+ * is an internal invariant failure rather than a call to overwrite. A
3279
+ * buffer that must grow is replaced, never reallocated in place: the new
3280
+ * buffer is filled from `arguments` before the old one is freed, so an
3281
+ * argument pointer into the old buffer stays valid for the copy.
3282
+ */
3283
+ static OseoResult record_tail_call(
3284
+ OseoContext *context,
3285
+ unsigned host,
3286
+ OseoValue callee,
3287
+ OseoValue receiver,
3288
+ size_t argument_count,
3289
+ const OseoValue *arguments,
3290
+ bool derived,
3291
+ OseoValue this_cell,
3292
+ OseoValue caller
3293
+ ) {
3294
+ OseoTailCall *record = tail_call_record(context);
3295
+ if (record == NULL) {
3296
+ return failure(
3297
+ context,
3298
+ "OSEO2001",
3299
+ "Tail call record allocation failed."
3300
+ );
3301
+ }
3302
+ if (record->pending) {
3303
+ return failure(
3304
+ context,
3305
+ "OSEO2001",
3306
+ "A tail call is already pending."
3307
+ );
3308
+ }
3309
+ if (argument_count > record->argument_capacity) {
3310
+ if (argument_count > SIZE_MAX / sizeof(OseoValue)) {
3311
+ return failure(
3312
+ context,
3313
+ "OSEO2001",
3314
+ "Tail call argument list is too large."
3315
+ );
3316
+ }
3317
+ OseoValue *grown = malloc(argument_count * sizeof(OseoValue));
3318
+ if (grown == NULL) {
3319
+ return failure(
3320
+ context,
3321
+ "OSEO2001",
3322
+ "Tail call argument allocation failed."
3323
+ );
3324
+ }
3325
+ memcpy(grown, arguments, argument_count * sizeof(OseoValue));
3326
+ free(record->arguments);
3327
+ record->arguments = grown;
3328
+ record->argument_capacity = argument_count;
3329
+ } else if (argument_count > 0u) {
3330
+ memmove(
3331
+ record->arguments,
3332
+ arguments,
3333
+ argument_count * sizeof(OseoValue)
3334
+ );
3335
+ }
3336
+ record->host = host;
3337
+ record->callee = callee;
3338
+ record->receiver = receiver;
3339
+ record->realm_record = context->realm_record;
3340
+ record->this_cell = this_cell;
3341
+ record->constructor_realm = derived && context->constructor_scope != NULL
3342
+ ? context->constructor_scope->realm_record
3343
+ : context->realm_record;
3344
+ record->argument_count = argument_count;
3345
+ record->caller = caller;
3346
+ record->derived = derived;
3347
+ record->pending = true;
3348
+ return (OseoResult){OSEO_STATUS_TAIL_CALL, oseo_undefined()};
3349
+ }
3350
+
3351
+ static void clear_tail_call(OseoTailCall *record) {
3352
+ record->host = 0u;
3353
+ record->callee = oseo_undefined();
3354
+ record->receiver = oseo_undefined();
3355
+ record->realm_record = oseo_undefined();
3356
+ record->this_cell = oseo_undefined();
3357
+ record->constructor_realm = oseo_undefined();
3358
+ record->caller = oseo_undefined();
3359
+ record->argument_count = 0u;
3360
+ record->pending = false;
3361
+ record->derived = false;
3362
+ }
3363
+
3364
+ OseoResult oseo_internal_tail_call(
3365
+ OseoContext *context,
3366
+ OseoValue callee,
3367
+ OseoValue receiver,
3368
+ size_t argument_count,
3369
+ const OseoValue *arguments
3370
+ ) {
3371
+ return record_tail_call(
3372
+ context,
3373
+ 0u,
3374
+ callee,
3375
+ receiver,
3376
+ argument_count,
3377
+ arguments,
3378
+ false,
3379
+ oseo_undefined(),
3380
+ oseo_internal_activation_current_caller(context)
3381
+ );
3382
+ }
3383
+
3384
+ void oseo_internal_tail_call_release(OseoContext *context) {
3385
+ OseoTailCall *record = context->tail_call;
3386
+ if (record == NULL) return;
3387
+ free(record->arguments);
3388
+ free(record);
3389
+ context->tail_call = NULL;
3390
+ }
3391
+
3392
+ OseoResult oseo_tail_call(
3393
+ OseoContext *context,
3394
+ OseoValue callee,
3395
+ OseoValue receiver,
3396
+ size_t argument_count,
3397
+ const OseoValue *arguments
3398
+ ) {
3399
+ /* EvaluateCall steps 3 and 4 throw before PrepareForTailCall, so the
3400
+ * ordinary call path raises that TypeError in the calling frame. */
3401
+ if (!is_callable(callee)) {
3402
+ return oseo_call_function(
3403
+ context,
3404
+ callee,
3405
+ receiver,
3406
+ argument_count,
3407
+ arguments,
3408
+ oseo_undefined()
3409
+ );
3410
+ }
3411
+ return record_tail_call(
3412
+ context,
3413
+ 0u,
3414
+ callee,
3415
+ receiver,
3416
+ argument_count,
3417
+ arguments,
3418
+ false,
3419
+ oseo_undefined(),
3420
+ oseo_undefined()
3421
+ );
3422
+ }
3423
+
3424
+ OseoResult oseo_tail_host_call(
3425
+ OseoContext *context,
3426
+ OseoTailHost host,
3427
+ size_t argument_count,
3428
+ const OseoValue *arguments
3429
+ ) {
3430
+ return record_tail_call(
3431
+ context,
3432
+ (unsigned)host,
3433
+ oseo_undefined(),
3434
+ oseo_undefined(),
3435
+ argument_count,
3436
+ arguments,
3437
+ false,
3438
+ oseo_undefined(),
3439
+ oseo_undefined()
3440
+ );
3441
+ }
3442
+
3443
+ OseoResult oseo_tail_host_call_derived(
3444
+ OseoContext *context,
3445
+ OseoTailHost host,
3446
+ size_t argument_count,
3447
+ const OseoValue *arguments,
3448
+ OseoValue this_cell
3449
+ ) {
3450
+ return record_tail_call(
3451
+ context,
3452
+ (unsigned)host,
3453
+ oseo_undefined(),
3454
+ oseo_undefined(),
3455
+ argument_count,
3456
+ arguments,
3457
+ true,
3458
+ this_cell,
3459
+ oseo_undefined()
3460
+ );
3461
+ }
3462
+
3463
+ /* One trampoline step's call: a host intrinsic or [[Call]]. */
3464
+ static OseoResult tail_call_step(
3465
+ OseoContext *context,
3466
+ unsigned host,
3467
+ OseoValue callee,
3468
+ OseoValue receiver,
3469
+ size_t argument_count,
3470
+ const OseoValue *arguments
3471
+ ) {
3472
+ if (host == (unsigned)OSEO_TAIL_HOST_CONSOLE_LOG) {
3473
+ return oseo_console_log(context, argument_count, arguments);
3474
+ }
3475
+ if (host == (unsigned)OSEO_TAIL_HOST_SET_TIMEOUT) {
3476
+ return oseo_set_timeout(context, argument_count, arguments);
3477
+ }
3478
+ if (host == (unsigned)OSEO_TAIL_HOST_CLEAR_TIMEOUT) {
3479
+ return oseo_clear_timeout(
3480
+ context,
3481
+ argument_count == 0u ? oseo_undefined() : arguments[0]
3482
+ );
3483
+ }
3484
+ if (host != 0u) {
3485
+ return failure(context, "OSEO2001", "Tail call host is invalid.");
3486
+ }
3487
+ return call_once(
3488
+ context,
3489
+ callee,
3490
+ receiver,
3491
+ argument_count,
3492
+ arguments,
3493
+ oseo_undefined()
3494
+ );
3495
+ }
3496
+
3497
+ OseoResult oseo_tail_call_derived(
3498
+ OseoContext *context,
3499
+ OseoValue callee,
3500
+ OseoValue receiver,
3501
+ size_t argument_count,
3502
+ const OseoValue *arguments,
3503
+ OseoValue this_cell
3504
+ ) {
3505
+ if (!is_callable(callee)) {
3506
+ return oseo_call_function(
3507
+ context,
3508
+ callee,
3509
+ receiver,
3510
+ argument_count,
3511
+ arguments,
3512
+ oseo_undefined()
3513
+ );
3514
+ }
3515
+ return record_tail_call(
3516
+ context,
3517
+ 0u,
3518
+ callee,
3519
+ receiver,
3520
+ argument_count,
3521
+ arguments,
3522
+ true,
3523
+ this_cell,
3524
+ oseo_undefined()
3525
+ );
3526
+ }
3527
+
3528
+ /*
3529
+ * Performs pending tail calls until one completes without owing another.
3530
+ * Each step moves the record into a root frame of its own before the
3531
+ * call runs, so a nested call or trampoline inside the callee finds the
3532
+ * record free, and the step's frame, realm scope, call depth, and frame
3533
+ * budget are all released before the next step begins: a chain of any
3534
+ * length holds one step's resources. Only the first step can come from a
3535
+ * derived class constructor, since every later step is a [[Call]], which
3536
+ * never evaluates a class constructor body; its `this` cell and realm stay
3537
+ * rooted here until the chain's final value passes the constructor's
3538
+ * result rules.
3539
+ */
3540
+ static OseoResult run_tail_calls(OseoContext *context) {
3541
+ OseoTailCall *record = context->tail_call;
3542
+ OseoValue continuation[2] = {oseo_undefined(), oseo_undefined()};
3543
+ OseoRootFrame continuation_frame = {NULL, continuation, 2u};
3544
+ oseo_roots_push(context, &continuation_frame);
3545
+ bool derived = record->derived;
3546
+ if (derived) {
3547
+ continuation[0] = record->this_cell;
3548
+ continuation[1] = record->constructor_realm;
3549
+ }
3550
+ OseoResult result;
3551
+ for (;;) {
3552
+ OseoRootFrame frame = {NULL, NULL, 0u};
3553
+ size_t argument_count = record->argument_count;
3554
+ result = oseo_roots_allocate(context, &frame, argument_count + 3u);
3555
+ if (result.status != OSEO_STATUS_NORMAL) {
3556
+ clear_tail_call(record);
3557
+ break;
3558
+ }
3559
+ unsigned host = record->host;
3560
+ frame.slots[0] = record->callee;
3561
+ frame.slots[1] = record->receiver;
3562
+ frame.slots[2] = record->realm_record;
3563
+ if (argument_count > 0u) {
3564
+ memcpy(
3565
+ frame.slots + 3u,
3566
+ record->arguments,
3567
+ argument_count * sizeof(OseoValue)
3568
+ );
3569
+ }
3570
+ /* The step's call counts as made by the function that owed it,
3571
+ * not by the frame running this trampoline (ADR 0034). The
3572
+ * boundary record keeps that function linked for the step. */
3573
+ OseoActivation boundary;
3574
+ oseo_internal_activation_push(
3575
+ context,
3576
+ &boundary,
3577
+ oseo_undefined(),
3578
+ 0u,
3579
+ NULL
3580
+ );
3581
+ boundary.caller = record->caller;
3582
+ clear_tail_call(record);
3583
+ OseoRealmScope scope;
3584
+ oseo_internal_realm_enter(context, &scope, frame.slots[2]);
3585
+ result = tail_call_step(
3586
+ context,
3587
+ host,
3588
+ frame.slots[0],
3589
+ frame.slots[1],
3590
+ argument_count,
3591
+ frame.slots + 3u
3592
+ );
3593
+ oseo_internal_realm_leave(context, &scope);
3594
+ oseo_internal_activation_pop(context, &boundary);
3595
+ oseo_roots_release(context, &frame);
3596
+ if (result.status != OSEO_STATUS_TAIL_CALL) break;
3597
+ if (record->derived) {
3598
+ clear_tail_call(record);
3599
+ result = failure(
3600
+ context,
3601
+ "OSEO2001",
3602
+ "A derived constructor tail call followed a [[Call]]."
3603
+ );
3604
+ break;
3605
+ }
3606
+ }
3607
+ if (derived && result.status == OSEO_STATUS_NORMAL) {
3608
+ result = derived_result_in_realm(
3609
+ context,
3610
+ result.value,
3611
+ continuation[0],
3612
+ continuation[1]
3613
+ );
3614
+ }
3615
+ oseo_roots_pop(context, &continuation_frame);
3616
+ return result;
3617
+ }
3618
+
3619
+ OseoResult oseo_call_complete(OseoContext *context, OseoResult result) {
3620
+ if (result.status != OSEO_STATUS_TAIL_CALL) return result;
3621
+ return run_tail_calls(context);
3622
+ }
3623
+
3624
+ OseoResult oseo_call_function(
3625
+ OseoContext *context,
3626
+ OseoValue callee,
3627
+ OseoValue receiver,
3628
+ size_t argument_count,
3629
+ const OseoValue *arguments,
3630
+ OseoValue new_target
3631
+ ) {
3632
+ return oseo_call_complete(
3633
+ context,
3634
+ call_once(
3635
+ context,
3636
+ callee,
3637
+ receiver,
3638
+ argument_count,
3639
+ arguments,
3640
+ new_target
3641
+ )
3642
+ );
3643
+ }