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

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