@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,3378 @@
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
+ /* Built-in bodies the dispatcher below reaches before this translation
36
+ * unit defines them. */
37
+ static OseoResult capability_executor_call(
38
+ OseoContext *context,
39
+ OseoValue callee,
40
+ size_t argument_count,
41
+ const OseoValue *arguments
42
+ );
43
+ typedef enum {
44
+ PROMISE_COMBINE_ALL,
45
+ PROMISE_COMBINE_ALL_SETTLED,
46
+ PROMISE_COMBINE_ANY,
47
+ PROMISE_COMBINE_RACE,
48
+ } PromiseCombineKind;
49
+ static OseoResult promise_combine(
50
+ OseoContext *context,
51
+ OseoValue constructor,
52
+ OseoValue iterable,
53
+ PromiseCombineKind kind
54
+ );
55
+ static OseoResult promise_construct_with_target(
56
+ OseoContext *context,
57
+ OseoValue new_target,
58
+ OseoValue executor
59
+ );
60
+ static OseoResult promise_prototype_then(
61
+ OseoContext *context,
62
+ OseoValue receiver,
63
+ OseoValue on_fulfilled,
64
+ OseoValue on_rejected
65
+ );
66
+ static OseoResult promise_invoke_then_with(
67
+ OseoContext *context,
68
+ OseoValue promise_value,
69
+ size_t argument_count,
70
+ OseoValue on_fulfilled,
71
+ OseoValue on_rejected
72
+ );
73
+ static OseoResult promise_resolve_with(
74
+ OseoContext *context,
75
+ OseoValue constructor,
76
+ OseoValue value
77
+ );
78
+ static OseoResult promise_static_reject(
79
+ OseoContext *context,
80
+ OseoValue constructor,
81
+ OseoValue reason
82
+ );
83
+ static OseoResult promise_static_resolve(
84
+ OseoContext *context,
85
+ OseoValue constructor,
86
+ OseoValue value
87
+ );
88
+ static OseoResult promise_static_try(
89
+ OseoContext *context,
90
+ OseoValue constructor,
91
+ size_t argument_count,
92
+ const OseoValue *arguments
93
+ );
94
+ static OseoResult promise_then_with_capability(
95
+ OseoContext *context,
96
+ OseoValue promise_value,
97
+ OseoValue on_fulfilled,
98
+ OseoValue on_rejected,
99
+ OseoValue capability
100
+ );
101
+ static OseoResult promise_with_resolvers(
102
+ OseoContext *context,
103
+ OseoValue constructor
104
+ );
105
+
106
+ OseoResult oseo_internal_promise_builtin_dispatch(
107
+ OseoContext *context,
108
+ size_t code_id,
109
+ OseoValue callee,
110
+ OseoValue receiver,
111
+ size_t argument_count,
112
+ const OseoValue *arguments,
113
+ OseoValue new_target
114
+ ) {
115
+ if (code_id == OSEO_PROMISE_CONSTRUCTOR_CODE_ID) {
116
+ /* 27.2.3.1 step 1: Promise is not callable without new. */
117
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
118
+ return oseo_internal_throw_error(
119
+ context,
120
+ OSEO_ERROR_TYPE,
121
+ "The Promise constructor requires new."
122
+ );
123
+ }
124
+ return promise_construct_with_target(
125
+ context,
126
+ new_target,
127
+ argument_count > 0u ? arguments[0] : oseo_undefined()
128
+ );
129
+ }
130
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
131
+ /* Every remaining built-in here is a method, accessor, or
132
+ * anonymous closure, and ECMA-262 gives none of them a
133
+ * [[Construct]]. */
134
+ return oseo_internal_throw_error(
135
+ context,
136
+ OSEO_ERROR_TYPE,
137
+ "The Promise built-in function is not a constructor."
138
+ );
139
+ }
140
+ if (code_id == OSEO_PROMISE_CAPABILITY_EXECUTOR_CODE_ID) {
141
+ return capability_executor_call(
142
+ context,
143
+ callee,
144
+ argument_count,
145
+ arguments
146
+ );
147
+ }
148
+ if (code_id == OSEO_PROMISE_SPECIES_CODE_ID) {
149
+ return normal(receiver);
150
+ }
151
+ if (code_id == OSEO_PROMISE_STATIC_RESOLVE_CODE_ID) {
152
+ return promise_static_resolve(
153
+ context,
154
+ receiver,
155
+ argument_count > 0u ? arguments[0] : oseo_undefined()
156
+ );
157
+ }
158
+ if (code_id == OSEO_PROMISE_STATIC_REJECT_CODE_ID) {
159
+ return promise_static_reject(
160
+ context,
161
+ receiver,
162
+ argument_count > 0u ? arguments[0] : oseo_undefined()
163
+ );
164
+ }
165
+ if (code_id == OSEO_PROMISE_WITH_RESOLVERS_CODE_ID) {
166
+ return promise_with_resolvers(context, receiver);
167
+ }
168
+ if (code_id == OSEO_PROMISE_TRY_CODE_ID) {
169
+ return promise_static_try(
170
+ context,
171
+ receiver,
172
+ argument_count,
173
+ arguments
174
+ );
175
+ }
176
+ if (code_id == OSEO_PROMISE_ALL_CODE_ID ||
177
+ code_id == OSEO_PROMISE_ALL_SETTLED_CODE_ID ||
178
+ code_id == OSEO_PROMISE_ANY_CODE_ID ||
179
+ code_id == OSEO_PROMISE_RACE_CODE_ID) {
180
+ PromiseCombineKind kind = PROMISE_COMBINE_ALL;
181
+ if (code_id == OSEO_PROMISE_ALL_SETTLED_CODE_ID) {
182
+ kind = PROMISE_COMBINE_ALL_SETTLED;
183
+ } else if (code_id == OSEO_PROMISE_ANY_CODE_ID) {
184
+ kind = PROMISE_COMBINE_ANY;
185
+ } else if (code_id == OSEO_PROMISE_RACE_CODE_ID) {
186
+ kind = PROMISE_COMBINE_RACE;
187
+ }
188
+ return promise_combine(
189
+ context,
190
+ receiver,
191
+ argument_count > 0u ? arguments[0] : oseo_undefined(),
192
+ kind
193
+ );
194
+ }
195
+ if (code_id == OSEO_PROMISE_RESOLVE_CODE_ID ||
196
+ code_id == OSEO_PROMISE_REJECT_CODE_ID) {
197
+ OseoResult result = oseo_function_environment(context, callee);
198
+ OseoValue environment = result.value;
199
+ if (result.status == OSEO_STATUS_NORMAL) {
200
+ result = oseo_environment_get(context, environment, 1u);
201
+ }
202
+ OseoValue latch = result.value;
203
+ if (result.status == OSEO_STATUS_NORMAL) {
204
+ result = oseo_cell_get(context, latch);
205
+ }
206
+ bool already_resolved = result.status == OSEO_STATUS_NORMAL &&
207
+ oseo_to_boolean(result.value);
208
+ if (already_resolved) {
209
+ result = normal(oseo_undefined());
210
+ } else if (result.status == OSEO_STATUS_NORMAL) {
211
+ result = oseo_cell_set(context, latch, oseo_boolean(true));
212
+ }
213
+ if (result.status == OSEO_STATUS_NORMAL && !already_resolved) {
214
+ result = oseo_environment_get(context, environment, 0u);
215
+ }
216
+ if (result.status == OSEO_STATUS_NORMAL && !already_resolved) {
217
+ OseoValue argument = argument_count > 0u
218
+ ? arguments[0]
219
+ : oseo_undefined();
220
+ result = code_id == OSEO_PROMISE_RESOLVE_CODE_ID
221
+ ? oseo_promise_resolve_into(context, result.value, argument)
222
+ : oseo_promise_reject_into(context, result.value, argument);
223
+ }
224
+ return result;
225
+ }
226
+ if (code_id == OSEO_PROMISE_FINALLY_FULFILL_CODE_ID ||
227
+ code_id == OSEO_PROMISE_FINALLY_REJECT_CODE_ID) {
228
+ OseoRootFrame frame = {NULL, NULL, 0u};
229
+ OseoResult result = oseo_roots_allocate(context, &frame, 7u);
230
+ if (result.status == OSEO_STATUS_NORMAL) {
231
+ frame.slots[0] = callee;
232
+ frame.slots[3] = argument_count > 0u
233
+ ? arguments[0]
234
+ : oseo_undefined();
235
+ result = oseo_function_environment(context, frame.slots[0]);
236
+ frame.slots[1] = result.value;
237
+ }
238
+ if (result.status == OSEO_STATUS_NORMAL) {
239
+ result = oseo_environment_get(context, frame.slots[1], 0u);
240
+ frame.slots[2] = result.value;
241
+ }
242
+ if (result.status == OSEO_STATUS_NORMAL) {
243
+ result = oseo_call_function(
244
+ context,
245
+ frame.slots[2],
246
+ oseo_undefined(),
247
+ 0u,
248
+ NULL,
249
+ oseo_undefined()
250
+ );
251
+ frame.slots[4] = result.value;
252
+ }
253
+ if (result.status == OSEO_STATUS_NORMAL) {
254
+ /* ThenFinally resolves through the constructor the enclosing
255
+ * `finally` captured, which is the SpeciesConstructor result
256
+ * rather than %Promise% whenever the receiver names one. */
257
+ result = oseo_environment_get(context, frame.slots[1], 1u);
258
+ frame.slots[5] = result.value;
259
+ }
260
+ if (result.status == OSEO_STATUS_NORMAL) {
261
+ result = promise_resolve_with(
262
+ context,
263
+ frame.slots[5],
264
+ frame.slots[4]
265
+ );
266
+ frame.slots[5] = result.value;
267
+ }
268
+ if (result.status == OSEO_STATUS_NORMAL) {
269
+ bool fulfilled =
270
+ code_id == OSEO_PROMISE_FINALLY_FULFILL_CODE_ID;
271
+ result = oseo_internal_promise_finally_continuation_create(
272
+ context,
273
+ frame.slots[3],
274
+ fulfilled
275
+ );
276
+ frame.slots[6] = result.value;
277
+ }
278
+ if (result.status == OSEO_STATUS_NORMAL) {
279
+ /* Both closures end with Invoke(p, "then", « thunk »), one
280
+ * argument rather than the two `finally` itself passes. */
281
+ result = promise_invoke_then_with(
282
+ context,
283
+ frame.slots[5],
284
+ 1u,
285
+ frame.slots[6],
286
+ oseo_undefined()
287
+ );
288
+ }
289
+ oseo_roots_release(context, &frame);
290
+ return result;
291
+ }
292
+ if (code_id == OSEO_PROMISE_FINALLY_CONTINUE_CODE_ID) {
293
+ OseoResult result = oseo_function_environment(context, callee);
294
+ OseoValue environment = result.value;
295
+ if (result.status == OSEO_STATUS_NORMAL) {
296
+ result = oseo_environment_get(context, environment, 0u);
297
+ }
298
+ OseoValue preserved = result.value;
299
+ if (result.status == OSEO_STATUS_NORMAL) {
300
+ result = oseo_environment_get(context, environment, 1u);
301
+ }
302
+ if (result.status == OSEO_STATUS_NORMAL &&
303
+ oseo_to_boolean(result.value)) {
304
+ result = normal(preserved);
305
+ } else if (result.status == OSEO_STATUS_NORMAL) {
306
+ oseo_context_clear_language_error(context);
307
+ result = (OseoResult){OSEO_STATUS_THROW, preserved};
308
+ }
309
+ return result;
310
+ }
311
+ if (code_id == OSEO_PROMISE_AGGREGATE_FULFILL_CODE_ID ||
312
+ code_id == OSEO_PROMISE_AGGREGATE_REJECT_CODE_ID) {
313
+ OseoResult result = oseo_function_environment(context, callee);
314
+ OseoValue environment = result.value;
315
+ bool fulfilling =
316
+ code_id == OSEO_PROMISE_AGGREGATE_FULFILL_CODE_ID;
317
+ if (result.status == OSEO_STATUS_NORMAL) {
318
+ result = oseo_environment_get(context, environment, 0u);
319
+ }
320
+ OseoValue reaction = result.value;
321
+ OseoReactionKind kind = OSEO_REACTION_NORMAL;
322
+ if (result.status == OSEO_STATUS_NORMAL) {
323
+ kind = reaction_object(reaction)->kind;
324
+ }
325
+ bool guarded = result.status == OSEO_STATUS_NORMAL &&
326
+ (kind == OSEO_REACTION_ALL ||
327
+ kind == OSEO_REACTION_ALL_SETTLED ||
328
+ kind == OSEO_REACTION_ANY);
329
+ if (result.status == OSEO_STATUS_NORMAL && guarded) {
330
+ result = oseo_environment_get(context, environment, 1u);
331
+ }
332
+ OseoValue already_called_cell = result.value;
333
+ if (result.status == OSEO_STATUS_NORMAL && guarded) {
334
+ result = oseo_cell_get(context, already_called_cell);
335
+ }
336
+ bool already_called = result.status == OSEO_STATUS_NORMAL &&
337
+ guarded && oseo_to_boolean(result.value);
338
+ if (already_called) {
339
+ result = normal(oseo_undefined());
340
+ } else if (result.status == OSEO_STATUS_NORMAL && guarded) {
341
+ result = oseo_cell_set(
342
+ context,
343
+ already_called_cell,
344
+ oseo_boolean(true)
345
+ );
346
+ }
347
+ if (result.status == OSEO_STATUS_NORMAL && !already_called) {
348
+ OseoValue argument = argument_count > 0u
349
+ ? arguments[0]
350
+ : oseo_undefined();
351
+ result = oseo_internal_promise_aggregate_settle(
352
+ context,
353
+ reaction,
354
+ argument,
355
+ fulfilling
356
+ );
357
+ }
358
+ return result;
359
+ }
360
+ if (code_id == OSEO_PROMISE_THEN_CODE_ID ||
361
+ code_id == OSEO_PROMISE_CATCH_CODE_ID ||
362
+ code_id == OSEO_PROMISE_FINALLY_CODE_ID) {
363
+ OseoValue first = argument_count > 0u
364
+ ? arguments[0]
365
+ : oseo_undefined();
366
+ OseoValue second = argument_count > 1u
367
+ ? arguments[1]
368
+ : oseo_undefined();
369
+ if (code_id == OSEO_PROMISE_THEN_CODE_ID) {
370
+ return promise_prototype_then(context, receiver, first, second);
371
+ }
372
+ if (code_id == OSEO_PROMISE_CATCH_CODE_ID) {
373
+ return oseo_internal_promise_invoke_then(
374
+ context,
375
+ receiver,
376
+ oseo_undefined(),
377
+ first
378
+ );
379
+ }
380
+ return oseo_internal_promise_finally_invoke(
381
+ context,
382
+ receiver,
383
+ first
384
+ );
385
+ }
386
+ return oseo_unknown_function(context, code_id);
387
+ }
388
+
389
+ /*
390
+ * Promises, capabilities, reactions, thenable jobs, combinators,
391
+ * and rejection tracking.
392
+ */
393
+
394
+ /*
395
+ * Allocates one pending promise with an explicit [[Prototype]]. The
396
+ * prototype is rooted across the allocation because a subclass supplies
397
+ * an object the intrinsic table does not hold.
398
+ */
399
+ static OseoResult promise_allocate(
400
+ OseoContext *context,
401
+ OseoValue prototype_value
402
+ ) {
403
+ OseoValue rooted = prototype_value;
404
+ OseoRootFrame frame = {NULL, &rooted, 1u};
405
+ oseo_roots_push(context, &frame);
406
+ OseoPromise *promise =
407
+ oseo_internal_allocate_heap_bytes(context, sizeof(*promise));
408
+ if (promise == NULL) {
409
+ oseo_roots_pop(context, &frame);
410
+ return failure(context, "OSEO2001", "Promise allocation failed.");
411
+ }
412
+ promise->ordinary.prototype = rooted;
413
+ promise->ordinary.properties = NULL;
414
+ promise->ordinary.property_capacity = 0u;
415
+ promise->ordinary.property_count = 0u;
416
+ oseo_internal_init_dense_index(&promise->ordinary);
417
+ promise->ordinary.private_elements = NULL;
418
+ promise->ordinary.private_element_capacity = 0u;
419
+ promise->ordinary.private_element_count = 0u;
420
+ promise->ordinary.shape_id = context->next_shape_id;
421
+ context->next_shape_id += 1u;
422
+ promise->ordinary.array_length = 0u;
423
+ promise->ordinary.dictionary = false;
424
+ promise->ordinary.length_writable = false;
425
+ promise->ordinary.extensible = true;
426
+ promise->ordinary.module_namespace = false;
427
+ promise->ordinary.immutable_prototype = false;
428
+ promise->ordinary.global_object = false;
429
+ promise->ordinary.error_data = false;
430
+ promise->ordinary.number_data = false;
431
+ promise->ordinary.number_value = oseo_undefined();
432
+ promise->ordinary.primitive_data = false;
433
+ promise->ordinary.primitive_value = oseo_undefined();
434
+ promise->ordinary.virtual_string_iterator = false;
435
+ promise->ordinary.virtual_string_iterator_configurable = false;
436
+ promise->ordinary.virtual_string_iterator_enumerable = false;
437
+ promise->ordinary.virtual_string_iterator_writable = false;
438
+ promise->ordinary.iterator_kind = OSEO_ITERATOR_NONE;
439
+ promise->ordinary.iterator_target = oseo_undefined();
440
+ promise->ordinary.iterator_index = 0u;
441
+ promise->ordinary.regexp_string_iterator = false;
442
+ promise->ordinary.regexp_iterator_regexp = oseo_undefined();
443
+ promise->ordinary.regexp_iterator_subject = oseo_undefined();
444
+ promise->ordinary.regexp_iterator_global = false;
445
+ promise->ordinary.regexp_iterator_unicode = false;
446
+ promise->ordinary.regexp_iterator_complete = false;
447
+ promise->ordinary.async_from_sync = false;
448
+ promise->ordinary.async_sync_iterator = oseo_undefined();
449
+ promise->ordinary.wrap_for_valid_iterator = false;
450
+ promise->ordinary.wrapped_iterator = oseo_undefined();
451
+ promise->ordinary.wrapped_next = oseo_undefined();
452
+ promise->ordinary.generator = NULL;
453
+ promise->ordinary.arguments_object = false;
454
+ promise->ordinary.mapped_arguments = false;
455
+ promise->result = oseo_undefined();
456
+ promise->reaction_head = oseo_undefined();
457
+ promise->reaction_tail = oseo_undefined();
458
+ promise->unhandled_next = oseo_undefined();
459
+ promise->realm = context->realm_record;
460
+ promise->rejection_source_id = context->source_id;
461
+ promise->rejection_source_id_length = context->source_id_length;
462
+ promise->rejection_line = context->line;
463
+ promise->rejection_column = context->column;
464
+ promise->state = OSEO_PROMISE_PENDING;
465
+ promise->handled = false;
466
+ promise->pending_report = false;
467
+ promise->reported = false;
468
+ OseoResult published = oseo_internal_publish_heap(
469
+ context,
470
+ &promise->ordinary.header,
471
+ OSEO_HEAP_PROMISE
472
+ );
473
+ oseo_roots_pop(context, &frame);
474
+ return published;
475
+ }
476
+
477
+ OseoResult oseo_internal_promise_create(OseoContext *context) {
478
+ OseoResult prototype = oseo_internal_promise_prototype(context);
479
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
480
+ return promise_allocate(context, prototype.value);
481
+ }
482
+
483
+ /*
484
+ * OrdinaryCreateFromConstructor(newTarget, "%Promise.prototype%"). A
485
+ * subclass constructor carries its own `prototype` object, so
486
+ * `new Subclass(executor)` gives its instance that object while a plain
487
+ * `new Promise(executor)` keeps the realm prototype.
488
+ *
489
+ * GetPrototypeFromConstructor is a real Get, not a read of the
490
+ * synthetic `prototype` slot, because a new target this reaches need not
491
+ * own one: `Reflect.construct(Promise, args, boundFunction)` can hand it
492
+ * a bound function whose own `prototype` a program defined as an
493
+ * accessor. This runs at the position 27.2.3.1 gives it, after the
494
+ * executor's callability check, so an abrupt read reports after that
495
+ * TypeError rather than before it.
496
+ */
497
+ static OseoResult promise_create_from_constructor(
498
+ OseoContext *context,
499
+ OseoValue new_target
500
+ ) {
501
+ OseoValue slots[2] = {new_target, oseo_undefined()};
502
+ OseoRootFrame frame = {NULL, slots, 2u};
503
+ oseo_roots_push(context, &frame);
504
+ OseoResult result = normal(oseo_undefined());
505
+ if (is_object(slots[0])) {
506
+ result = oseo_internal_ascii_string(context, "prototype");
507
+ slots[1] = result.value;
508
+ if (result.status == OSEO_STATUS_NORMAL) {
509
+ result = oseo_object_get(context, slots[0], slots[1]);
510
+ slots[1] = result.value;
511
+ }
512
+ }
513
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[1])) {
514
+ result = oseo_internal_constructor_realm_default(
515
+ context,
516
+ slots[0],
517
+ OSEO_INTRINSIC_PROMISE_PROTOTYPE
518
+ );
519
+ slots[1] = result.value;
520
+ }
521
+ if (result.status == OSEO_STATUS_NORMAL) {
522
+ result = promise_allocate(context, slots[1]);
523
+ }
524
+ oseo_roots_pop(context, &frame);
525
+ return result;
526
+ }
527
+
528
+ OseoResult oseo_internal_promise_method_function(
529
+ OseoContext *context,
530
+ const char *name
531
+ ) {
532
+ OseoIntrinsic intrinsic;
533
+ size_t code_id;
534
+ const uint16_t *units;
535
+ size_t length;
536
+ static const uint16_t catch_units[] = {'c', 'a', 't', 'c', 'h'};
537
+ static const uint16_t finally_units[] = {
538
+ 'f', 'i', 'n', 'a', 'l', 'l', 'y'
539
+ };
540
+ static const uint16_t then_units[] = {'t', 'h', 'e', 'n'};
541
+ if (strcmp(name, "then") == 0) {
542
+ intrinsic = OSEO_INTRINSIC_PROMISE_THEN;
543
+ code_id = OSEO_PROMISE_THEN_CODE_ID;
544
+ units = then_units;
545
+ length = sizeof(then_units) / sizeof(*then_units);
546
+ } else if (strcmp(name, "catch") == 0) {
547
+ intrinsic = OSEO_INTRINSIC_PROMISE_CATCH;
548
+ code_id = OSEO_PROMISE_CATCH_CODE_ID;
549
+ units = catch_units;
550
+ length = sizeof(catch_units) / sizeof(*catch_units);
551
+ } else if (strcmp(name, "finally") == 0) {
552
+ intrinsic = OSEO_INTRINSIC_PROMISE_FINALLY;
553
+ code_id = OSEO_PROMISE_FINALLY_CODE_ID;
554
+ units = finally_units;
555
+ length = sizeof(finally_units) / sizeof(*finally_units);
556
+ } else {
557
+ return failure(context, "OSEO2001", "Unknown promise method.");
558
+ }
559
+ OseoValue *cache = &context->realm->intrinsics[intrinsic];
560
+ if (tag_of(*cache) != OSEO_TAG_UNDEFINED) return normal(*cache);
561
+ OseoRootFrame frame = {NULL, NULL, 0u};
562
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
563
+ if (result.status != OSEO_STATUS_NORMAL) return result;
564
+ result = oseo_environment_create(context, 0u);
565
+ frame.slots[0] = result.value;
566
+ if (result.status == OSEO_STATUS_NORMAL) {
567
+ result = oseo_function_create(
568
+ context,
569
+ code_id,
570
+ frame.slots[0],
571
+ units,
572
+ length,
573
+ code_id == OSEO_PROMISE_THEN_CODE_ID ? 2u : 1u,
574
+ OSEO_FUNCTION_INTERNAL,
575
+ oseo_undefined(),
576
+ oseo_undefined(),
577
+ OSEO_FUNCTION_NAME_PREFIX_NONE
578
+ );
579
+ }
580
+ if (result.status == OSEO_STATUS_NORMAL) *cache = result.value;
581
+ oseo_roots_release(context, &frame);
582
+ return result;
583
+ }
584
+
585
+ static OseoResult create_promise_builtin(
586
+ OseoContext *context,
587
+ size_t code_id,
588
+ const char *name,
589
+ size_t length,
590
+ OseoFunctionKind kind,
591
+ OseoFunctionNamePrefix prefix
592
+ ) {
593
+ size_t name_length = strlen(name);
594
+ if (name_length > 31u) {
595
+ return failure(context, "OSEO2001", "Built-in name is too long.");
596
+ }
597
+ uint16_t units[31];
598
+ for (size_t index = 0u; index < name_length; index += 1u) {
599
+ units[index] = (uint16_t)(unsigned char)name[index];
600
+ }
601
+ OseoValue environment = oseo_undefined();
602
+ OseoRootFrame frame = {NULL, &environment, 1u};
603
+ oseo_roots_push(context, &frame);
604
+ OseoResult result = oseo_environment_create(context, 0u);
605
+ environment = result.value;
606
+ if (result.status == OSEO_STATUS_NORMAL) {
607
+ result = oseo_function_create(
608
+ context,
609
+ code_id,
610
+ environment,
611
+ units,
612
+ name_length,
613
+ length,
614
+ kind,
615
+ oseo_undefined(),
616
+ oseo_undefined(),
617
+ prefix
618
+ );
619
+ }
620
+ oseo_roots_pop(context, &frame);
621
+ return result;
622
+ }
623
+
624
+ static OseoResult define_promise_property(
625
+ OseoContext *context,
626
+ OseoValue object,
627
+ const char *name,
628
+ OseoValue value,
629
+ OseoPropertyAttributes attributes
630
+ ) {
631
+ OseoValue slots[3] = {object, value, oseo_undefined()};
632
+ OseoRootFrame frame = {NULL, slots, 3u};
633
+ oseo_roots_push(context, &frame);
634
+ OseoResult result = oseo_internal_ascii_string(context, name);
635
+ slots[2] = result.value;
636
+ if (result.status == OSEO_STATUS_NORMAL) {
637
+ result = oseo_object_define(
638
+ context,
639
+ slots[0],
640
+ slots[2],
641
+ slots[1],
642
+ attributes
643
+ );
644
+ }
645
+ oseo_roots_pop(context, &frame);
646
+ return result;
647
+ }
648
+
649
+ /*
650
+ * Materializes %Promise%, %Promise.prototype%, and every own property
651
+ * ECMA-262 gives them. The whole cluster is built at once so that a
652
+ * promise reached without naming `Promise`, such as the one an
653
+ * asynchronous function returns, still finds `constructor`,
654
+ * `Symbol.toStringTag`, and the statics on its prototype chain.
655
+ *
656
+ * `OSEO_INTRINSIC_PROMISE_SPECIES` is filled last, so it doubles as the
657
+ * completion marker: a partially built cluster leaves it undefined and
658
+ * the failure path clears every slot the attempt filled. The marker
659
+ * holds the uninitialized sentinel while the attempt runs, so an
660
+ * in-progress cluster is distinguishable from an unbuilt one. No
661
+ * operation the build performs materializes `%Promise%`, so reaching
662
+ * that state would mean a new dependency introduced a cycle; the
663
+ * sentinel reports it instead of splitting the constructor and
664
+ * prototype identities across two concurrent attempts.
665
+ */
666
+ static OseoResult promise_intrinsic_build(OseoContext *context) {
667
+ OseoValue *marker =
668
+ &context->realm->intrinsics[OSEO_INTRINSIC_PROMISE_SPECIES];
669
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
670
+ return failure(
671
+ context,
672
+ "OSEO2001",
673
+ "The Promise intrinsic cluster is already being built."
674
+ );
675
+ }
676
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
677
+ size_t entry_allocations = context->allocations;
678
+ OseoRootFrame frame = {NULL, NULL, 0u};
679
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
680
+ if (result.status != OSEO_STATUS_NORMAL) return result;
681
+ *marker = oseo_uninitialized();
682
+ result = oseo_internal_intrinsic(
683
+ context,
684
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
685
+ );
686
+ frame.slots[0] = result.value;
687
+ if (result.status == OSEO_STATUS_NORMAL) {
688
+ result = oseo_object_create(context, frame.slots[0]);
689
+ frame.slots[1] = result.value;
690
+ }
691
+ if (result.status == OSEO_STATUS_NORMAL) {
692
+ context->realm->intrinsics[OSEO_INTRINSIC_PROMISE_PROTOTYPE] =
693
+ frame.slots[1];
694
+ }
695
+ static const char *const method_names[] = {"then", "catch", "finally"};
696
+ const OseoPropertyAttributes method = {true, false, true, false};
697
+ for (size_t index = 0u;
698
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
699
+ index += 1u) {
700
+ result = oseo_internal_promise_method_function(
701
+ context,
702
+ method_names[index]
703
+ );
704
+ frame.slots[2] = result.value;
705
+ if (result.status != OSEO_STATUS_NORMAL) break;
706
+ result = define_promise_property(
707
+ context,
708
+ frame.slots[1],
709
+ method_names[index],
710
+ frame.slots[2],
711
+ method
712
+ );
713
+ }
714
+ if (result.status == OSEO_STATUS_NORMAL) {
715
+ result = create_promise_builtin(
716
+ context,
717
+ OSEO_PROMISE_CONSTRUCTOR_CODE_ID,
718
+ "Promise",
719
+ 1u,
720
+ OSEO_FUNCTION_ORDINARY,
721
+ OSEO_FUNCTION_NAME_PREFIX_NONE
722
+ );
723
+ frame.slots[2] = result.value;
724
+ }
725
+ if (result.status == OSEO_STATUS_NORMAL) {
726
+ context->realm->intrinsics[OSEO_INTRINSIC_PROMISE] = frame.slots[2];
727
+ OseoFunction *constructor = function_object(frame.slots[2]);
728
+ constructor->prototype_object = frame.slots[1];
729
+ constructor->prototype_writable = false;
730
+ result = define_promise_property(
731
+ context,
732
+ frame.slots[1],
733
+ "constructor",
734
+ frame.slots[2],
735
+ method
736
+ );
737
+ }
738
+ if (result.status == OSEO_STATUS_NORMAL) {
739
+ result = oseo_internal_well_known_symbol(
740
+ context,
741
+ OSEO_WELL_KNOWN_TO_STRING_TAG
742
+ );
743
+ frame.slots[3] = result.value;
744
+ }
745
+ if (result.status == OSEO_STATUS_NORMAL) {
746
+ result = oseo_internal_ascii_string(context, "Promise");
747
+ frame.slots[4] = result.value;
748
+ }
749
+ if (result.status == OSEO_STATUS_NORMAL) {
750
+ result = oseo_object_define(
751
+ context,
752
+ frame.slots[1],
753
+ frame.slots[3],
754
+ frame.slots[4],
755
+ (OseoPropertyAttributes){true, false, false, false}
756
+ );
757
+ }
758
+ static const OseoIntrinsic static_intrinsics[] = {
759
+ OSEO_INTRINSIC_PROMISE_ALL,
760
+ OSEO_INTRINSIC_PROMISE_ALL_SETTLED,
761
+ OSEO_INTRINSIC_PROMISE_ANY,
762
+ OSEO_INTRINSIC_PROMISE_RACE,
763
+ OSEO_INTRINSIC_PROMISE_REJECT,
764
+ OSEO_INTRINSIC_PROMISE_RESOLVE,
765
+ OSEO_INTRINSIC_PROMISE_TRY,
766
+ OSEO_INTRINSIC_PROMISE_WITH_RESOLVERS,
767
+ };
768
+ static const size_t static_codes[] = {
769
+ OSEO_PROMISE_ALL_CODE_ID,
770
+ OSEO_PROMISE_ALL_SETTLED_CODE_ID,
771
+ OSEO_PROMISE_ANY_CODE_ID,
772
+ OSEO_PROMISE_RACE_CODE_ID,
773
+ OSEO_PROMISE_STATIC_REJECT_CODE_ID,
774
+ OSEO_PROMISE_STATIC_RESOLVE_CODE_ID,
775
+ OSEO_PROMISE_TRY_CODE_ID,
776
+ OSEO_PROMISE_WITH_RESOLVERS_CODE_ID,
777
+ };
778
+ static const char *const static_names[] = {
779
+ "all",
780
+ "allSettled",
781
+ "any",
782
+ "race",
783
+ "reject",
784
+ "resolve",
785
+ "try",
786
+ "withResolvers",
787
+ };
788
+ static const size_t static_lengths[] = {
789
+ 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u
790
+ };
791
+ for (size_t index = 0u;
792
+ result.status == OSEO_STATUS_NORMAL && index < 8u;
793
+ index += 1u) {
794
+ result = create_promise_builtin(
795
+ context,
796
+ static_codes[index],
797
+ static_names[index],
798
+ static_lengths[index],
799
+ OSEO_FUNCTION_INTERNAL,
800
+ OSEO_FUNCTION_NAME_PREFIX_NONE
801
+ );
802
+ frame.slots[5] = result.value;
803
+ if (result.status != OSEO_STATUS_NORMAL) break;
804
+ context->realm->intrinsics[static_intrinsics[index]] = frame.slots[5];
805
+ result = define_promise_property(
806
+ context,
807
+ frame.slots[2],
808
+ static_names[index],
809
+ frame.slots[5],
810
+ method
811
+ );
812
+ }
813
+ if (result.status == OSEO_STATUS_NORMAL) {
814
+ result = create_promise_builtin(
815
+ context,
816
+ OSEO_PROMISE_SPECIES_CODE_ID,
817
+ "[Symbol.species]",
818
+ 0u,
819
+ OSEO_FUNCTION_INTERNAL,
820
+ OSEO_FUNCTION_NAME_PREFIX_GET
821
+ );
822
+ frame.slots[5] = result.value;
823
+ }
824
+ if (result.status == OSEO_STATUS_NORMAL) {
825
+ result = oseo_internal_well_known_symbol(
826
+ context,
827
+ OSEO_WELL_KNOWN_SPECIES
828
+ );
829
+ frame.slots[3] = result.value;
830
+ }
831
+ if (result.status == OSEO_STATUS_NORMAL) {
832
+ result = oseo_object_define_accessor(
833
+ context,
834
+ frame.slots[2],
835
+ frame.slots[3],
836
+ frame.slots[5],
837
+ oseo_undefined(),
838
+ true,
839
+ false,
840
+ (OseoPropertyAttributes){true, false, false, true}
841
+ );
842
+ }
843
+ if (result.status != OSEO_STATUS_NORMAL) {
844
+ context->realm->intrinsics[OSEO_INTRINSIC_PROMISE_PROTOTYPE] =
845
+ oseo_undefined();
846
+ for (size_t index = OSEO_INTRINSIC_PROMISE;
847
+ index <= OSEO_INTRINSIC_PROMISE_SPECIES;
848
+ index += 1u) {
849
+ context->realm->intrinsics[index] = oseo_undefined();
850
+ }
851
+ oseo_roots_release(context, &frame);
852
+ return result;
853
+ }
854
+ OseoValue species = frame.slots[5];
855
+ context->realm->intrinsics[OSEO_INTRINSIC_PROMISE_SPECIES] = species;
856
+ if (context->observe_specialization) {
857
+ context->allocations = entry_allocations;
858
+ }
859
+ oseo_roots_release(context, &frame);
860
+ return normal(species);
861
+ }
862
+
863
+ OseoResult oseo_internal_promise_prototype(OseoContext *context) {
864
+ OseoValue *cache =
865
+ &context->realm->intrinsics[OSEO_INTRINSIC_PROMISE_PROTOTYPE];
866
+ if (tag_of(*cache) != OSEO_TAG_UNDEFINED) return normal(*cache);
867
+ OseoResult built = promise_intrinsic_build(context);
868
+ if (built.status != OSEO_STATUS_NORMAL) return built;
869
+ return normal(*cache);
870
+ }
871
+
872
+ OseoResult oseo_internal_promise_intrinsic(OseoContext *context) {
873
+ OseoResult built = promise_intrinsic_build(context);
874
+ if (built.status != OSEO_STATUS_NORMAL) return built;
875
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_PROMISE]);
876
+ }
877
+
878
+ OseoResult oseo_internal_install_promise_global(
879
+ OseoContext *context,
880
+ OseoValue global
881
+ ) {
882
+ OseoValue slots[2] = {global, oseo_undefined()};
883
+ OseoRootFrame frame = {NULL, slots, 2u};
884
+ oseo_roots_push(context, &frame);
885
+ OseoResult result = oseo_internal_promise_intrinsic(context);
886
+ slots[1] = result.value;
887
+ if (result.status == OSEO_STATUS_NORMAL) {
888
+ result = define_promise_property(
889
+ context,
890
+ slots[0],
891
+ "Promise",
892
+ slots[1],
893
+ (OseoPropertyAttributes){true, false, true, false}
894
+ );
895
+ }
896
+ oseo_roots_pop(context, &frame);
897
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
898
+ }
899
+
900
+ static OseoResult reaction_create(
901
+ OseoContext *context,
902
+ OseoValue on_fulfilled,
903
+ OseoValue on_rejected,
904
+ OseoValue capability
905
+ ) {
906
+ OseoPromiseReaction *reaction =
907
+ oseo_internal_allocate_heap_bytes(context, sizeof(*reaction));
908
+ if (reaction == NULL) {
909
+ return failure(
910
+ context,
911
+ "OSEO2001",
912
+ "Promise reaction allocation failed."
913
+ );
914
+ }
915
+ reaction->next = oseo_undefined();
916
+ reaction->on_fulfilled = on_fulfilled;
917
+ reaction->on_rejected = on_rejected;
918
+ reaction->capability = capability;
919
+ reaction->aggregate = oseo_undefined();
920
+ reaction->index = 0u;
921
+ reaction->kind = OSEO_REACTION_NORMAL;
922
+ return oseo_internal_publish_heap(
923
+ context,
924
+ &reaction->header,
925
+ OSEO_HEAP_PROMISE_REACTION
926
+ );
927
+ }
928
+
929
+ static OseoResult enqueue_job(
930
+ OseoContext *context,
931
+ OseoJobKind kind,
932
+ OseoValue primary,
933
+ OseoValue secondary,
934
+ OseoValue argument,
935
+ bool fulfilled
936
+ ) {
937
+ OseoJob *job = oseo_internal_allocate_heap_bytes(context, sizeof(*job));
938
+ if (job == NULL) {
939
+ return failure(context, "OSEO2001", "Promise job allocation failed.");
940
+ }
941
+ job->next = oseo_undefined();
942
+ job->primary = primary;
943
+ job->secondary = secondary;
944
+ job->argument = argument;
945
+ if (kind == OSEO_JOB_THENABLE) {
946
+ job->realm = oseo_internal_callee_realm(context, secondary);
947
+ } else {
948
+ const OseoPromiseReaction *reaction = reaction_object(primary);
949
+ job->realm = oseo_internal_callee_realm(
950
+ context,
951
+ fulfilled ? reaction->on_fulfilled : reaction->on_rejected
952
+ );
953
+ }
954
+ job->kind = kind;
955
+ job->fulfilled = fulfilled;
956
+ OseoResult published =
957
+ oseo_internal_publish_heap(context, &job->header, OSEO_HEAP_JOB);
958
+ if (published.status != OSEO_STATUS_NORMAL) return published;
959
+ if (tag_of(context->microtask_tail) == OSEO_TAG_UNDEFINED) {
960
+ context->microtask_head = published.value;
961
+ } else {
962
+ job_object(context->microtask_tail)->next = published.value;
963
+ }
964
+ context->microtask_tail = published.value;
965
+ return normal(oseo_undefined());
966
+ }
967
+
968
+ static OseoResult promise_attach_reaction(
969
+ OseoContext *context,
970
+ OseoValue promise_value,
971
+ OseoValue reaction_value
972
+ ) {
973
+ OseoPromise *promise = promise_object(promise_value);
974
+ if (promise->reported && !promise->handled) {
975
+ context->rejection_handled_count += 1u;
976
+ }
977
+ promise->handled = true;
978
+ promise->pending_report = false;
979
+ if (promise->state == OSEO_PROMISE_PENDING) {
980
+ if (tag_of(promise->reaction_tail) == OSEO_TAG_UNDEFINED) {
981
+ promise->reaction_head = reaction_value;
982
+ } else {
983
+ reaction_object(promise->reaction_tail)->next = reaction_value;
984
+ }
985
+ promise->reaction_tail = reaction_value;
986
+ return normal(oseo_undefined());
987
+ }
988
+ return enqueue_job(
989
+ context,
990
+ OSEO_JOB_REACTION,
991
+ reaction_value,
992
+ promise_value,
993
+ promise->result,
994
+ promise->state == OSEO_PROMISE_FULFILLED
995
+ );
996
+ }
997
+
998
+ static OseoResult enqueue_reactions(
999
+ OseoContext *context,
1000
+ OseoValue promise_value
1001
+ ) {
1002
+ OseoPromise *promise = promise_object(promise_value);
1003
+ OseoValue current = promise->reaction_head;
1004
+ while (tag_of(current) != OSEO_TAG_UNDEFINED) {
1005
+ OseoPromiseReaction *reaction = reaction_object(current);
1006
+ OseoValue next = reaction->next;
1007
+ OseoResult queued = enqueue_job(
1008
+ context,
1009
+ OSEO_JOB_REACTION,
1010
+ current,
1011
+ promise_value,
1012
+ promise_object(promise_value)->result,
1013
+ promise_object(promise_value)->state == OSEO_PROMISE_FULFILLED
1014
+ );
1015
+ if (queued.status != OSEO_STATUS_NORMAL) return queued;
1016
+ current = next;
1017
+ }
1018
+ promise = promise_object(promise_value);
1019
+ promise->reaction_head = oseo_undefined();
1020
+ promise->reaction_tail = oseo_undefined();
1021
+ return normal(oseo_undefined());
1022
+ }
1023
+
1024
+ static OseoResult promise_fulfill(
1025
+ OseoContext *context,
1026
+ OseoValue promise_value,
1027
+ OseoValue value
1028
+ ) {
1029
+ if (!is_promise(promise_value)) {
1030
+ return oseo_internal_throw_error(
1031
+ context,
1032
+ OSEO_ERROR_TYPE,
1033
+ "The settled value is not a promise."
1034
+ );
1035
+ }
1036
+ OseoPromise *promise = promise_object(promise_value);
1037
+ if (promise->state != OSEO_PROMISE_PENDING) {
1038
+ return normal(oseo_undefined());
1039
+ }
1040
+ promise->state = OSEO_PROMISE_FULFILLED;
1041
+ promise->result = value;
1042
+ return enqueue_reactions(context, promise_value);
1043
+ }
1044
+
1045
+ OseoResult oseo_promise_reject_into(
1046
+ OseoContext *context,
1047
+ OseoValue promise_value,
1048
+ OseoValue reason
1049
+ ) {
1050
+ if (!is_promise(promise_value)) return oseo_internal_throw_error(
1051
+ context,
1052
+ OSEO_ERROR_TYPE,
1053
+ "The settled value is not a promise."
1054
+ );
1055
+ OseoPromise *promise = promise_object(promise_value);
1056
+ if (promise->state != OSEO_PROMISE_PENDING) {
1057
+ return normal(oseo_undefined());
1058
+ }
1059
+ promise->state = OSEO_PROMISE_REJECTED;
1060
+ promise->result = reason;
1061
+ promise->rejection_source_id = context->source_id;
1062
+ promise->rejection_source_id_length = context->source_id_length;
1063
+ promise->rejection_line = context->line;
1064
+ promise->rejection_column = context->column;
1065
+ if (!promise->handled) {
1066
+ promise->pending_report = true;
1067
+ if (tag_of(context->pending_rejection_tail) == OSEO_TAG_UNDEFINED) {
1068
+ context->pending_rejections = promise_value;
1069
+ } else {
1070
+ promise_object(context->pending_rejection_tail)->unhandled_next =
1071
+ promise_value;
1072
+ }
1073
+ context->pending_rejection_tail = promise_value;
1074
+ }
1075
+ return enqueue_reactions(context, promise_value);
1076
+ }
1077
+
1078
+ static OseoResult resolving_environment_create(
1079
+ OseoContext *context,
1080
+ OseoValue promise
1081
+ ) {
1082
+ OseoRootFrame frame = {NULL, NULL, 0u};
1083
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1084
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1085
+ frame.slots[0] = promise;
1086
+ result = oseo_environment_create(context, 2u);
1087
+ frame.slots[1] = result.value;
1088
+ if (result.status == OSEO_STATUS_NORMAL) {
1089
+ result = oseo_environment_set(
1090
+ context,
1091
+ frame.slots[1],
1092
+ 0u,
1093
+ frame.slots[0]
1094
+ );
1095
+ }
1096
+ if (result.status == OSEO_STATUS_NORMAL) {
1097
+ result = oseo_cell_create(context, oseo_boolean(false));
1098
+ frame.slots[2] = result.value;
1099
+ }
1100
+ if (result.status == OSEO_STATUS_NORMAL) {
1101
+ result = oseo_environment_set(
1102
+ context,
1103
+ frame.slots[1],
1104
+ 1u,
1105
+ frame.slots[2]
1106
+ );
1107
+ }
1108
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
1109
+ oseo_roots_release(context, &frame);
1110
+ return result;
1111
+ }
1112
+
1113
+ static OseoResult resolving_function_create(
1114
+ OseoContext *context,
1115
+ OseoValue environment,
1116
+ size_t code_id
1117
+ ) {
1118
+ if (!is_environment(environment)) {
1119
+ return failure(context, "OSEO2001", "Invalid resolving environment.");
1120
+ }
1121
+ OseoRootFrame frame = {NULL, NULL, 0u};
1122
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
1123
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1124
+ frame.slots[0] = environment;
1125
+ if (result.status == OSEO_STATUS_NORMAL) {
1126
+ result = oseo_function_create(
1127
+ context,
1128
+ code_id,
1129
+ frame.slots[0],
1130
+ NULL,
1131
+ 0u,
1132
+ 1u,
1133
+ OSEO_FUNCTION_INTERNAL,
1134
+ oseo_undefined(),
1135
+ oseo_undefined(),
1136
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1137
+ );
1138
+ }
1139
+ oseo_roots_release(context, &frame);
1140
+ return result;
1141
+ }
1142
+
1143
+ OseoResult oseo_promise_resolve_into(
1144
+ OseoContext *context,
1145
+ OseoValue promise_value,
1146
+ OseoValue value
1147
+ ) {
1148
+ if (!is_promise(promise_value)) return oseo_internal_throw_error(
1149
+ context,
1150
+ OSEO_ERROR_TYPE,
1151
+ "The settled value is not a promise."
1152
+ );
1153
+ if (promise_object(promise_value)->state != OSEO_PROMISE_PENDING) {
1154
+ return normal(oseo_undefined());
1155
+ }
1156
+ if (promise_value == value) {
1157
+ OseoResult error = oseo_internal_throw_error(
1158
+ context,
1159
+ OSEO_ERROR_TYPE,
1160
+ "A promise cannot resolve to itself."
1161
+ );
1162
+ if (error.status != OSEO_STATUS_THROW || context->has_diagnostic) {
1163
+ return error;
1164
+ }
1165
+ oseo_context_clear_language_error(context);
1166
+ return oseo_promise_reject_into(context, promise_value, error.value);
1167
+ }
1168
+ if (!is_object(value)) {
1169
+ return promise_fulfill(context, promise_value, value);
1170
+ }
1171
+
1172
+ OseoRootFrame frame = {NULL, NULL, 0u};
1173
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1174
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1175
+ frame.slots[0] = promise_value;
1176
+ frame.slots[1] = value;
1177
+ static const uint16_t then_units[] = {'t', 'h', 'e', 'n'};
1178
+ result = oseo_string_from_units(
1179
+ context,
1180
+ then_units,
1181
+ sizeof(then_units) / sizeof(*then_units)
1182
+ );
1183
+ frame.slots[2] = result.value;
1184
+ if (result.status == OSEO_STATUS_NORMAL) {
1185
+ result = oseo_object_get(context, frame.slots[1], frame.slots[2]);
1186
+ frame.slots[2] = result.value;
1187
+ }
1188
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
1189
+ oseo_context_clear_language_error(context);
1190
+ result = oseo_promise_reject_into(
1191
+ context,
1192
+ frame.slots[0],
1193
+ result.value
1194
+ );
1195
+ } else if (result.status == OSEO_STATUS_NORMAL &&
1196
+ is_callable(frame.slots[2])) {
1197
+ result = enqueue_job(
1198
+ context,
1199
+ OSEO_JOB_THENABLE,
1200
+ frame.slots[0],
1201
+ frame.slots[2],
1202
+ frame.slots[1],
1203
+ true
1204
+ );
1205
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1206
+ result = promise_fulfill(context, frame.slots[0], frame.slots[1]);
1207
+ }
1208
+ oseo_roots_release(context, &frame);
1209
+ return result;
1210
+ }
1211
+
1212
+ OseoResult oseo_promise_resolve(
1213
+ OseoContext *context,
1214
+ OseoValue value
1215
+ ) {
1216
+ OseoRootFrame frame = {NULL, NULL, 0u};
1217
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
1218
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1219
+ frame.slots[0] = value;
1220
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_PROMISE);
1221
+ frame.slots[1] = result.value;
1222
+ if (result.status == OSEO_STATUS_NORMAL) {
1223
+ result = promise_resolve_with(
1224
+ context,
1225
+ frame.slots[1],
1226
+ frame.slots[0]
1227
+ );
1228
+ }
1229
+ oseo_roots_release(context, &frame);
1230
+ return result;
1231
+ }
1232
+
1233
+ /*
1234
+ * A PromiseCapability Record. A capability over %Promise% is the native
1235
+ * promise itself, so the common path keeps allocating one object and
1236
+ * settles it directly. A capability over any other constructor is a
1237
+ * three-slot environment holding the constructed promise, its resolve
1238
+ * function, and its reject function, because those functions are the
1239
+ * only way to settle a foreign promise. Both shapes are ordinary
1240
+ * collector-traced values, so one reaction field carries either.
1241
+ */
1242
+ static bool is_foreign_capability(OseoValue capability) {
1243
+ return is_environment(capability);
1244
+ }
1245
+
1246
+ static OseoResult capability_promise(
1247
+ OseoContext *context,
1248
+ OseoValue capability
1249
+ ) {
1250
+ if (!is_foreign_capability(capability)) return normal(capability);
1251
+ return oseo_environment_get(context, capability, 0u);
1252
+ }
1253
+
1254
+ /* Resolves or rejects one capability, whichever `fulfilled` selects. */
1255
+ static OseoResult capability_settle(
1256
+ OseoContext *context,
1257
+ OseoValue capability,
1258
+ OseoValue value,
1259
+ bool fulfilled
1260
+ ) {
1261
+ if (!is_foreign_capability(capability)) {
1262
+ /* The allocation-free capability stands for resolving functions
1263
+ * of the promise's realm, so an error self-resolution creates
1264
+ * belongs to that realm. */
1265
+ OseoRealmScope scope;
1266
+ oseo_internal_realm_enter(
1267
+ context,
1268
+ &scope,
1269
+ promise_object(capability)->realm
1270
+ );
1271
+ OseoResult settled = fulfilled
1272
+ ? oseo_promise_resolve_into(context, capability, value)
1273
+ : oseo_promise_reject_into(context, capability, value);
1274
+ oseo_internal_realm_leave(context, &scope);
1275
+ return settled;
1276
+ }
1277
+ OseoRootFrame frame = {NULL, NULL, 0u};
1278
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1279
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1280
+ frame.slots[0] = capability;
1281
+ frame.slots[1] = value;
1282
+ result = oseo_environment_get(
1283
+ context,
1284
+ frame.slots[0],
1285
+ fulfilled ? 1u : 2u
1286
+ );
1287
+ frame.slots[2] = result.value;
1288
+ if (result.status == OSEO_STATUS_NORMAL) {
1289
+ result = oseo_call_function(
1290
+ context,
1291
+ frame.slots[2],
1292
+ oseo_undefined(),
1293
+ 1u,
1294
+ &frame.slots[1],
1295
+ oseo_undefined()
1296
+ );
1297
+ }
1298
+ oseo_roots_release(context, &frame);
1299
+ return result;
1300
+ }
1301
+
1302
+ /*
1303
+ * NewPromiseCapability. %Promise% keeps its allocation-free
1304
+ * representation; every other constructor is invoked with the
1305
+ * GetCapabilitiesExecutor closure, and the resolving functions it
1306
+ * captured must both be callable afterwards.
1307
+ */
1308
+ static OseoResult new_promise_capability(
1309
+ OseoContext *context,
1310
+ OseoValue constructor
1311
+ ) {
1312
+ if (is_function(constructor) &&
1313
+ constructor == context->realm->intrinsics[OSEO_INTRINSIC_PROMISE]) {
1314
+ return oseo_internal_promise_create(context);
1315
+ }
1316
+ if (!function_is_constructible(constructor)) {
1317
+ return oseo_internal_throw_error(
1318
+ context,
1319
+ OSEO_ERROR_TYPE,
1320
+ "The promise capability needs a constructor."
1321
+ );
1322
+ }
1323
+ OseoRootFrame frame = {NULL, NULL, 0u};
1324
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
1325
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1326
+ frame.slots[0] = constructor;
1327
+ result = oseo_environment_create(context, 3u);
1328
+ frame.slots[1] = result.value;
1329
+ /* Creating a function copies its environment's slots, so the record
1330
+ * itself is reached through one closure slot: the copy still points
1331
+ * at the same record, and the executor's writes stay visible here. */
1332
+ if (result.status == OSEO_STATUS_NORMAL) {
1333
+ result = oseo_environment_create(context, 1u);
1334
+ frame.slots[4] = result.value;
1335
+ }
1336
+ if (result.status == OSEO_STATUS_NORMAL) {
1337
+ result = oseo_environment_set(
1338
+ context,
1339
+ frame.slots[4],
1340
+ 0u,
1341
+ frame.slots[1]
1342
+ );
1343
+ }
1344
+ if (result.status == OSEO_STATUS_NORMAL) {
1345
+ result = oseo_function_create(
1346
+ context,
1347
+ OSEO_PROMISE_CAPABILITY_EXECUTOR_CODE_ID,
1348
+ frame.slots[4],
1349
+ NULL,
1350
+ 0u,
1351
+ 2u,
1352
+ OSEO_FUNCTION_INTERNAL,
1353
+ oseo_undefined(),
1354
+ oseo_undefined(),
1355
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1356
+ );
1357
+ frame.slots[2] = result.value;
1358
+ }
1359
+ if (result.status == OSEO_STATUS_NORMAL) {
1360
+ result = oseo_function_prototype(context, frame.slots[0]);
1361
+ }
1362
+ if (result.status == OSEO_STATUS_NORMAL) {
1363
+ result = oseo_internal_function_receiver(
1364
+ context,
1365
+ frame.slots[0],
1366
+ result.value
1367
+ );
1368
+ frame.slots[3] = result.value;
1369
+ }
1370
+ if (result.status == OSEO_STATUS_NORMAL) {
1371
+ result = oseo_call_function(
1372
+ context,
1373
+ frame.slots[0],
1374
+ frame.slots[3],
1375
+ 1u,
1376
+ &frame.slots[2],
1377
+ frame.slots[0]
1378
+ );
1379
+ }
1380
+ if (result.status == OSEO_STATUS_NORMAL) {
1381
+ result = oseo_constructor_result(
1382
+ context,
1383
+ result.value,
1384
+ frame.slots[3]
1385
+ );
1386
+ }
1387
+ if (result.status == OSEO_STATUS_NORMAL) {
1388
+ result = oseo_environment_set(
1389
+ context,
1390
+ frame.slots[1],
1391
+ 0u,
1392
+ result.value
1393
+ );
1394
+ }
1395
+ for (size_t index = 1u;
1396
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
1397
+ index += 1u) {
1398
+ result = oseo_environment_get(context, frame.slots[1], index);
1399
+ if (result.status == OSEO_STATUS_NORMAL &&
1400
+ !is_callable(result.value)) {
1401
+ result = oseo_internal_throw_error(
1402
+ context,
1403
+ OSEO_ERROR_TYPE,
1404
+ "The promise capability resolving function is not callable."
1405
+ );
1406
+ }
1407
+ }
1408
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
1409
+ oseo_roots_release(context, &frame);
1410
+ return result;
1411
+ }
1412
+
1413
+ /*
1414
+ * GetCapabilitiesExecutor. ECMA-262 rejects a second call rather than
1415
+ * overwriting the captured functions, which is what makes a constructor
1416
+ * that invokes its executor twice observable.
1417
+ */
1418
+ static OseoResult capability_executor_call(
1419
+ OseoContext *context,
1420
+ OseoValue callee,
1421
+ size_t argument_count,
1422
+ const OseoValue *arguments
1423
+ ) {
1424
+ OseoRootFrame frame = {NULL, NULL, 0u};
1425
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1426
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1427
+ frame.slots[1] = argument_count > 0u ? arguments[0] : oseo_undefined();
1428
+ frame.slots[2] = argument_count > 1u ? arguments[1] : oseo_undefined();
1429
+ result = oseo_function_environment(context, callee);
1430
+ frame.slots[0] = result.value;
1431
+ if (result.status == OSEO_STATUS_NORMAL) {
1432
+ result = oseo_environment_get(context, frame.slots[0], 0u);
1433
+ frame.slots[0] = result.value;
1434
+ }
1435
+ for (size_t index = 1u;
1436
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
1437
+ index += 1u) {
1438
+ result = oseo_environment_get(context, frame.slots[0], index);
1439
+ if (result.status == OSEO_STATUS_NORMAL &&
1440
+ tag_of(result.value) != OSEO_TAG_UNDEFINED) {
1441
+ result = oseo_internal_throw_error(
1442
+ context,
1443
+ OSEO_ERROR_TYPE,
1444
+ "The promise capability is already resolved."
1445
+ );
1446
+ }
1447
+ }
1448
+ for (size_t index = 1u;
1449
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
1450
+ index += 1u) {
1451
+ result = oseo_environment_set(
1452
+ context,
1453
+ frame.slots[0],
1454
+ index,
1455
+ frame.slots[index]
1456
+ );
1457
+ }
1458
+ if (result.status == OSEO_STATUS_NORMAL) {
1459
+ result = normal(oseo_undefined());
1460
+ }
1461
+ oseo_roots_release(context, &frame);
1462
+ return result;
1463
+ }
1464
+
1465
+ static OseoResult promise_aggregate_create(
1466
+ OseoContext *context,
1467
+ OseoValue capability,
1468
+ OseoValue values,
1469
+ size_t remaining
1470
+ ) {
1471
+ OseoPromiseAggregate *aggregate =
1472
+ oseo_internal_allocate_heap_bytes(context, sizeof(*aggregate));
1473
+ if (aggregate == NULL) {
1474
+ return failure(
1475
+ context,
1476
+ "OSEO2001",
1477
+ "Promise aggregate allocation failed."
1478
+ );
1479
+ }
1480
+ aggregate->capability = capability;
1481
+ aggregate->values = values;
1482
+ aggregate->remaining = remaining;
1483
+ aggregate->settled = false;
1484
+ return oseo_internal_publish_heap(
1485
+ context,
1486
+ &aggregate->header,
1487
+ OSEO_HEAP_PROMISE_AGGREGATE
1488
+ );
1489
+ }
1490
+
1491
+ static OseoResult promise_aggregate_environment_create(
1492
+ OseoContext *context,
1493
+ OseoValue reaction
1494
+ ) {
1495
+ OseoRootFrame frame = {NULL, NULL, 0u};
1496
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1497
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1498
+ frame.slots[0] = reaction;
1499
+ result = oseo_environment_create(context, 2u);
1500
+ frame.slots[1] = result.value;
1501
+ if (result.status == OSEO_STATUS_NORMAL) {
1502
+ result = oseo_environment_set(
1503
+ context,
1504
+ frame.slots[1],
1505
+ 0u,
1506
+ frame.slots[0]
1507
+ );
1508
+ }
1509
+ if (result.status == OSEO_STATUS_NORMAL) {
1510
+ result = oseo_cell_create(context, oseo_boolean(false));
1511
+ frame.slots[2] = result.value;
1512
+ }
1513
+ if (result.status == OSEO_STATUS_NORMAL) {
1514
+ result = oseo_environment_set(
1515
+ context,
1516
+ frame.slots[1],
1517
+ 1u,
1518
+ frame.slots[2]
1519
+ );
1520
+ }
1521
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
1522
+ oseo_roots_release(context, &frame);
1523
+ return result;
1524
+ }
1525
+
1526
+ static OseoResult promise_aggregate_function_create(
1527
+ OseoContext *context,
1528
+ OseoValue environment,
1529
+ size_t code_id
1530
+ ) {
1531
+ OseoRootFrame frame = {NULL, NULL, 0u};
1532
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
1533
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1534
+ frame.slots[0] = environment;
1535
+ result = oseo_function_create(
1536
+ context,
1537
+ code_id,
1538
+ frame.slots[0],
1539
+ NULL,
1540
+ 0u,
1541
+ 1u,
1542
+ OSEO_FUNCTION_INTERNAL,
1543
+ oseo_undefined(),
1544
+ oseo_undefined(),
1545
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1546
+ );
1547
+ oseo_roots_release(context, &frame);
1548
+ return result;
1549
+ }
1550
+
1551
+ static OseoResult promise_settled_record(
1552
+ OseoContext *context,
1553
+ OseoValue argument,
1554
+ bool fulfilled
1555
+ ) {
1556
+ OseoRootFrame frame = {NULL, NULL, 0u};
1557
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
1558
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1559
+ frame.slots[0] = argument;
1560
+ result = oseo_internal_intrinsic(
1561
+ context,
1562
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1563
+ );
1564
+ if (result.status == OSEO_STATUS_NORMAL) {
1565
+ result = oseo_object_create(context, result.value);
1566
+ frame.slots[1] = result.value;
1567
+ }
1568
+ if (result.status == OSEO_STATUS_NORMAL) {
1569
+ result = oseo_internal_ascii_string(context, "status");
1570
+ frame.slots[2] = result.value;
1571
+ }
1572
+ if (result.status == OSEO_STATUS_NORMAL) {
1573
+ result = oseo_internal_ascii_string(
1574
+ context,
1575
+ fulfilled ? "fulfilled" : "rejected"
1576
+ );
1577
+ frame.slots[3] = result.value;
1578
+ }
1579
+ if (result.status == OSEO_STATUS_NORMAL) {
1580
+ result = oseo_object_define(
1581
+ context,
1582
+ frame.slots[1],
1583
+ frame.slots[2],
1584
+ frame.slots[3],
1585
+ (OseoPropertyAttributes){true, true, true, false}
1586
+ );
1587
+ }
1588
+ if (result.status == OSEO_STATUS_NORMAL) {
1589
+ result = oseo_internal_ascii_string(
1590
+ context,
1591
+ fulfilled ? "value" : "reason"
1592
+ );
1593
+ frame.slots[2] = result.value;
1594
+ }
1595
+ if (result.status == OSEO_STATUS_NORMAL) {
1596
+ result = oseo_object_define(
1597
+ context,
1598
+ frame.slots[1],
1599
+ frame.slots[2],
1600
+ frame.slots[0],
1601
+ (OseoPropertyAttributes){true, true, true, false}
1602
+ );
1603
+ }
1604
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
1605
+ oseo_roots_release(context, &frame);
1606
+ return result;
1607
+ }
1608
+
1609
+ static OseoResult promise_any_error(
1610
+ OseoContext *context,
1611
+ OseoValue errors
1612
+ ) {
1613
+ OseoRootFrame frame = {NULL, NULL, 0u};
1614
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
1615
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1616
+ frame.slots[0] = errors;
1617
+ result = oseo_internal_error_prototype(
1618
+ context,
1619
+ OSEO_ERROR_AGGREGATE
1620
+ );
1621
+ if (result.status == OSEO_STATUS_NORMAL) {
1622
+ result = oseo_object_create(context, result.value);
1623
+ frame.slots[1] = result.value;
1624
+ }
1625
+ if (result.status == OSEO_STATUS_NORMAL) {
1626
+ ordinary_object(frame.slots[1])->error_data = true;
1627
+ result = oseo_internal_ascii_string(
1628
+ context,
1629
+ "All promises were rejected"
1630
+ );
1631
+ frame.slots[3] = result.value;
1632
+ }
1633
+ if (result.status == OSEO_STATUS_NORMAL) {
1634
+ result = oseo_internal_ascii_string(context, "message");
1635
+ frame.slots[2] = result.value;
1636
+ }
1637
+ if (result.status == OSEO_STATUS_NORMAL) {
1638
+ result = oseo_object_define(
1639
+ context,
1640
+ frame.slots[1],
1641
+ frame.slots[2],
1642
+ frame.slots[3],
1643
+ (OseoPropertyAttributes){true, false, true, false}
1644
+ );
1645
+ }
1646
+ if (result.status == OSEO_STATUS_NORMAL) {
1647
+ result = oseo_internal_ascii_string(context, "errors");
1648
+ frame.slots[2] = result.value;
1649
+ }
1650
+ if (result.status == OSEO_STATUS_NORMAL) {
1651
+ result = oseo_object_define(
1652
+ context,
1653
+ frame.slots[1],
1654
+ frame.slots[2],
1655
+ frame.slots[0],
1656
+ (OseoPropertyAttributes){true, false, true, false}
1657
+ );
1658
+ }
1659
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
1660
+ oseo_roots_release(context, &frame);
1661
+ return result;
1662
+ }
1663
+
1664
+ static OseoResult promise_aggregate_complete(
1665
+ OseoContext *context,
1666
+ OseoValue aggregate_value,
1667
+ OseoReactionKind kind
1668
+ ) {
1669
+ OseoRootFrame frame = {NULL, NULL, 0u};
1670
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1671
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1672
+ frame.slots[0] = aggregate_value;
1673
+ OseoPromiseAggregate *aggregate = aggregate_object(frame.slots[0]);
1674
+ frame.slots[1] = aggregate->capability;
1675
+ frame.slots[2] = aggregate->values;
1676
+ bool native_capability = !is_foreign_capability(frame.slots[1]);
1677
+ if (kind == OSEO_REACTION_ANY) {
1678
+ result = promise_any_error(context, frame.slots[2]);
1679
+ frame.slots[2] = result.value;
1680
+ }
1681
+ if (result.status == OSEO_STATUS_NORMAL) {
1682
+ if (native_capability) {
1683
+ aggregate = aggregate_object(frame.slots[0]);
1684
+ aggregate->settled = true;
1685
+ }
1686
+ result = capability_settle(
1687
+ context,
1688
+ frame.slots[1],
1689
+ frame.slots[2],
1690
+ kind != OSEO_REACTION_ANY
1691
+ );
1692
+ }
1693
+ oseo_roots_release(context, &frame);
1694
+ return result;
1695
+ }
1696
+
1697
+ OseoResult oseo_internal_promise_aggregate_settle(
1698
+ OseoContext *context,
1699
+ OseoValue reaction_value,
1700
+ OseoValue argument,
1701
+ bool fulfilled
1702
+ ) {
1703
+ OseoRootFrame frame = {NULL, NULL, 0u};
1704
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
1705
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1706
+ frame.slots[0] = reaction_value;
1707
+ OseoPromiseReaction *reaction = reaction_object(frame.slots[0]);
1708
+ frame.slots[1] = reaction->aggregate;
1709
+ OseoPromiseAggregate *aggregate = aggregate_object(frame.slots[1]);
1710
+ frame.slots[2] = aggregate->capability;
1711
+ frame.slots[3] = aggregate->values;
1712
+ frame.slots[4] = argument;
1713
+ size_t index = reaction->index;
1714
+ OseoReactionKind kind = reaction->kind;
1715
+ bool native_capability = !is_foreign_capability(frame.slots[2]);
1716
+ bool aggregate_already_settled = aggregate->settled;
1717
+ if (native_capability &&
1718
+ (aggregate_already_settled ||
1719
+ promise_object(frame.slots[2])->state != OSEO_PROMISE_PENDING)) {
1720
+ result = normal(oseo_undefined());
1721
+ } else if (kind == OSEO_REACTION_RACE ||
1722
+ (kind == OSEO_REACTION_ALL && !fulfilled) ||
1723
+ (kind == OSEO_REACTION_ANY && fulfilled)) {
1724
+ if (native_capability) aggregate->settled = true;
1725
+ result = capability_settle(
1726
+ context,
1727
+ frame.slots[2],
1728
+ frame.slots[4],
1729
+ fulfilled
1730
+ );
1731
+ } else {
1732
+ if (kind == OSEO_REACTION_ALL_SETTLED) {
1733
+ result = promise_settled_record(
1734
+ context,
1735
+ frame.slots[4],
1736
+ fulfilled
1737
+ );
1738
+ frame.slots[4] = result.value;
1739
+ }
1740
+ if (result.status == OSEO_STATUS_NORMAL) {
1741
+ result = oseo_property_key(
1742
+ context,
1743
+ oseo_number((double)index)
1744
+ );
1745
+ }
1746
+ frame.slots[0] = result.value;
1747
+ if (result.status == OSEO_STATUS_NORMAL) {
1748
+ result = oseo_object_define(
1749
+ context,
1750
+ frame.slots[3],
1751
+ frame.slots[0],
1752
+ frame.slots[4],
1753
+ (OseoPropertyAttributes){true, true, true, false}
1754
+ );
1755
+ }
1756
+ if (result.status == OSEO_STATUS_NORMAL) {
1757
+ aggregate = aggregate_object(frame.slots[1]);
1758
+ aggregate->remaining -= 1u;
1759
+ if (aggregate->remaining == 0u) {
1760
+ result = promise_aggregate_complete(
1761
+ context,
1762
+ frame.slots[1],
1763
+ kind
1764
+ );
1765
+ }
1766
+ }
1767
+ }
1768
+ oseo_roots_release(context, &frame);
1769
+ return result;
1770
+ }
1771
+
1772
+ static OseoResult promise_capability_reject_call(
1773
+ OseoContext *context,
1774
+ OseoValue capability,
1775
+ OseoValue foreign_reject,
1776
+ OseoValue reason
1777
+ ) {
1778
+ if (!is_foreign_capability(capability)) {
1779
+ return capability_settle(context, capability, reason, false);
1780
+ }
1781
+ OseoValue slots[2] = {foreign_reject, reason};
1782
+ OseoRootFrame frame = {NULL, slots, 2u};
1783
+ oseo_roots_push(context, &frame);
1784
+ OseoResult result = oseo_call_function(
1785
+ context,
1786
+ slots[0],
1787
+ oseo_undefined(),
1788
+ 1u,
1789
+ &slots[1],
1790
+ oseo_undefined()
1791
+ );
1792
+ oseo_roots_pop(context, &frame);
1793
+ return result;
1794
+ }
1795
+
1796
+ static OseoResult promise_combine_reject_call(
1797
+ OseoContext *context,
1798
+ OseoValue capability,
1799
+ OseoValue aggregate_value,
1800
+ OseoValue foreign_reject,
1801
+ OseoValue shared_reject,
1802
+ OseoValue reason,
1803
+ PromiseCombineKind kind
1804
+ ) {
1805
+ if (kind != PROMISE_COMBINE_ALL &&
1806
+ kind != PROMISE_COMBINE_RACE) {
1807
+ if (!is_foreign_capability(capability)) {
1808
+ OseoPromiseAggregate *aggregate =
1809
+ aggregate_object(aggregate_value);
1810
+ if (aggregate->settled ||
1811
+ promise_object(capability)->state != OSEO_PROMISE_PENDING) {
1812
+ return normal(oseo_undefined());
1813
+ }
1814
+ aggregate->settled = true;
1815
+ }
1816
+ return promise_capability_reject_call(
1817
+ context,
1818
+ capability,
1819
+ foreign_reject,
1820
+ reason
1821
+ );
1822
+ }
1823
+ OseoValue slots[2] = {shared_reject, reason};
1824
+ OseoRootFrame frame = {NULL, slots, 2u};
1825
+ oseo_roots_push(context, &frame);
1826
+ OseoResult result = oseo_call_function(
1827
+ context,
1828
+ slots[0],
1829
+ oseo_undefined(),
1830
+ 1u,
1831
+ &slots[1],
1832
+ oseo_undefined()
1833
+ );
1834
+ oseo_roots_pop(context, &frame);
1835
+ return result;
1836
+ }
1837
+
1838
+ /* The four iterable combinators share capability, iterator, and abrupt
1839
+ * completion plumbing while their per-element reactions select the
1840
+ * aggregate result. */
1841
+ static OseoResult promise_combine(
1842
+ OseoContext *context,
1843
+ OseoValue constructor,
1844
+ OseoValue iterable,
1845
+ PromiseCombineKind combine_kind
1846
+ ) {
1847
+ OseoRootFrame frame = {NULL, NULL, 0u};
1848
+ OseoResult result = oseo_roots_allocate(context, &frame, 17u);
1849
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1850
+ frame.slots[0] = iterable;
1851
+ frame.slots[14] = constructor;
1852
+ result = new_promise_capability(context, frame.slots[14]);
1853
+ frame.slots[1] = result.value;
1854
+ if (result.status != OSEO_STATUS_NORMAL) {
1855
+ oseo_roots_release(context, &frame);
1856
+ return result;
1857
+ }
1858
+ if (is_foreign_capability(frame.slots[1])) {
1859
+ result = oseo_environment_get(context, frame.slots[1], 2u);
1860
+ frame.slots[16] = result.value;
1861
+ }
1862
+ static const uint16_t resolve_units[] = {
1863
+ 'r', 'e', 's', 'o', 'l', 'v', 'e'
1864
+ };
1865
+ if (result.status == OSEO_STATUS_NORMAL) {
1866
+ result = oseo_string_from_units(context, resolve_units, 7u);
1867
+ frame.slots[15] = result.value;
1868
+ }
1869
+ if (result.status == OSEO_STATUS_NORMAL) {
1870
+ result = oseo_object_get(
1871
+ context,
1872
+ frame.slots[14],
1873
+ frame.slots[15]
1874
+ );
1875
+ frame.slots[15] = result.value;
1876
+ }
1877
+ if (result.status == OSEO_STATUS_NORMAL &&
1878
+ !is_callable(frame.slots[15])) {
1879
+ result = oseo_internal_throw_error(
1880
+ context,
1881
+ OSEO_ERROR_TYPE,
1882
+ "The promise constructor resolve method is not callable."
1883
+ );
1884
+ }
1885
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
1886
+ frame.slots[2] = result.value;
1887
+ oseo_context_clear_language_error(context);
1888
+ result = promise_capability_reject_call(
1889
+ context,
1890
+ frame.slots[1],
1891
+ frame.slots[16],
1892
+ frame.slots[2]
1893
+ );
1894
+ if (result.status == OSEO_STATUS_NORMAL) {
1895
+ result = capability_promise(context, frame.slots[1]);
1896
+ }
1897
+ oseo_roots_release(context, &frame);
1898
+ return result;
1899
+ }
1900
+ if (result.status == OSEO_STATUS_NORMAL) {
1901
+ OseoValue captured_next = oseo_undefined();
1902
+ result = oseo_iterator_get(
1903
+ context,
1904
+ frame.slots[0],
1905
+ &captured_next
1906
+ );
1907
+ frame.slots[12] = result.value;
1908
+ if (result.status == OSEO_STATUS_NORMAL) {
1909
+ frame.slots[13] = captured_next;
1910
+ }
1911
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
1912
+ frame.slots[2] = result.value;
1913
+ oseo_context_clear_language_error(context);
1914
+ result = promise_capability_reject_call(
1915
+ context,
1916
+ frame.slots[1],
1917
+ frame.slots[16],
1918
+ frame.slots[2]
1919
+ );
1920
+ if (result.status == OSEO_STATUS_NORMAL) {
1921
+ result = capability_promise(context, frame.slots[1]);
1922
+ }
1923
+ oseo_roots_release(context, &frame);
1924
+ return result;
1925
+ }
1926
+ }
1927
+ if (result.status == OSEO_STATUS_NORMAL) {
1928
+ result = combine_kind == PROMISE_COMBINE_RACE
1929
+ ? normal(oseo_undefined())
1930
+ : oseo_array_create(context, 0u);
1931
+ frame.slots[2] = result.value;
1932
+ }
1933
+ if (result.status == OSEO_STATUS_NORMAL) {
1934
+ result = promise_aggregate_create(
1935
+ context,
1936
+ frame.slots[1],
1937
+ frame.slots[2],
1938
+ 1u
1939
+ );
1940
+ frame.slots[3] = result.value;
1941
+ }
1942
+ static const uint16_t then_units[] = {'t', 'h', 'e', 'n'};
1943
+ if (result.status == OSEO_STATUS_NORMAL) {
1944
+ result = oseo_string_from_units(context, then_units, 4u);
1945
+ frame.slots[7] = result.value;
1946
+ }
1947
+ /* all shares its reject element, any shares its fulfill element, and
1948
+ * race shares both. A foreign capability exposes the constructor-created
1949
+ * functions directly because their identity is observable. */
1950
+ if (result.status == OSEO_STATUS_NORMAL) {
1951
+ result = reaction_create(
1952
+ context,
1953
+ oseo_undefined(),
1954
+ oseo_undefined(),
1955
+ oseo_undefined()
1956
+ );
1957
+ frame.slots[6] = result.value;
1958
+ }
1959
+ if (result.status == OSEO_STATUS_NORMAL) {
1960
+ OseoPromiseReaction *reaction = reaction_object(frame.slots[6]);
1961
+ reaction->aggregate = frame.slots[3];
1962
+ reaction->index = 0u;
1963
+ if (combine_kind == PROMISE_COMBINE_ALL) {
1964
+ reaction->kind = OSEO_REACTION_RACE;
1965
+ } else if (combine_kind == PROMISE_COMBINE_ALL_SETTLED) {
1966
+ reaction->kind = OSEO_REACTION_ALL_SETTLED;
1967
+ } else if (combine_kind == PROMISE_COMBINE_ANY) {
1968
+ reaction->kind = OSEO_REACTION_ANY;
1969
+ } else {
1970
+ reaction->kind = OSEO_REACTION_RACE;
1971
+ }
1972
+ result = promise_aggregate_environment_create(
1973
+ context,
1974
+ frame.slots[6]
1975
+ );
1976
+ frame.slots[8] = result.value;
1977
+ }
1978
+ if (result.status == OSEO_STATUS_NORMAL &&
1979
+ (combine_kind == PROMISE_COMBINE_RACE ||
1980
+ combine_kind == PROMISE_COMBINE_ANY)) {
1981
+ result = is_foreign_capability(frame.slots[1])
1982
+ ? oseo_environment_get(context, frame.slots[1], 1u)
1983
+ : promise_aggregate_function_create(
1984
+ context,
1985
+ frame.slots[8],
1986
+ OSEO_PROMISE_AGGREGATE_FULFILL_CODE_ID
1987
+ );
1988
+ frame.slots[9] = result.value;
1989
+ }
1990
+ if (result.status == OSEO_STATUS_NORMAL &&
1991
+ (combine_kind == PROMISE_COMBINE_RACE ||
1992
+ combine_kind == PROMISE_COMBINE_ALL)) {
1993
+ result = is_foreign_capability(frame.slots[1])
1994
+ ? normal(frame.slots[16])
1995
+ : promise_aggregate_function_create(
1996
+ context,
1997
+ frame.slots[8],
1998
+ OSEO_PROMISE_AGGREGATE_REJECT_CODE_ID
1999
+ );
2000
+ frame.slots[10] = result.value;
2001
+ }
2002
+ bool iteration_abrupt = false;
2003
+ bool reject_call_abrupt = false;
2004
+ size_t index = 0u;
2005
+ while (result.status == OSEO_STATUS_NORMAL) {
2006
+ OseoValue element = oseo_undefined();
2007
+ bool done = false;
2008
+ result = oseo_iterator_next(
2009
+ context,
2010
+ frame.slots[12],
2011
+ frame.slots[13],
2012
+ &element,
2013
+ &done
2014
+ );
2015
+ /*
2016
+ * A throw from IteratorStep leaves the iterator record done, so
2017
+ * the specification rejects without calling IteratorClose.
2018
+ */
2019
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
2020
+ frame.slots[4] = result.value;
2021
+ oseo_context_clear_language_error(context);
2022
+ iteration_abrupt = true;
2023
+ result = promise_combine_reject_call(
2024
+ context,
2025
+ frame.slots[1],
2026
+ frame.slots[3],
2027
+ frame.slots[16],
2028
+ frame.slots[10],
2029
+ frame.slots[4],
2030
+ combine_kind
2031
+ );
2032
+ reject_call_abrupt = result.status != OSEO_STATUS_NORMAL;
2033
+ break;
2034
+ }
2035
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
2036
+ frame.slots[4] = element;
2037
+ if (combine_kind != PROMISE_COMBINE_RACE) {
2038
+ aggregate_object(frame.slots[3])->remaining += 1u;
2039
+ }
2040
+ {
2041
+ result = oseo_call_function(
2042
+ context,
2043
+ frame.slots[15],
2044
+ frame.slots[14],
2045
+ 1u,
2046
+ &frame.slots[4],
2047
+ oseo_undefined()
2048
+ );
2049
+ frame.slots[5] = result.value;
2050
+ }
2051
+ if (result.status == OSEO_STATUS_NORMAL &&
2052
+ combine_kind != PROMISE_COMBINE_RACE) {
2053
+ result = reaction_create(
2054
+ context,
2055
+ oseo_undefined(),
2056
+ oseo_undefined(),
2057
+ oseo_undefined()
2058
+ );
2059
+ frame.slots[6] = result.value;
2060
+ }
2061
+ if (result.status == OSEO_STATUS_NORMAL &&
2062
+ combine_kind != PROMISE_COMBINE_RACE) {
2063
+ OseoPromiseReaction *reaction =
2064
+ reaction_object(frame.slots[6]);
2065
+ reaction->aggregate = frame.slots[3];
2066
+ reaction->index = index;
2067
+ if (combine_kind == PROMISE_COMBINE_ALL) {
2068
+ reaction->kind = OSEO_REACTION_ALL;
2069
+ } else if (combine_kind == PROMISE_COMBINE_ALL_SETTLED) {
2070
+ reaction->kind = OSEO_REACTION_ALL_SETTLED;
2071
+ } else {
2072
+ reaction->kind = OSEO_REACTION_ANY;
2073
+ }
2074
+ result = promise_aggregate_environment_create(
2075
+ context,
2076
+ frame.slots[6]
2077
+ );
2078
+ frame.slots[8] = result.value;
2079
+ }
2080
+ if (result.status == OSEO_STATUS_NORMAL &&
2081
+ (combine_kind == PROMISE_COMBINE_ALL ||
2082
+ combine_kind == PROMISE_COMBINE_ALL_SETTLED)) {
2083
+ result = promise_aggregate_function_create(
2084
+ context,
2085
+ frame.slots[8],
2086
+ OSEO_PROMISE_AGGREGATE_FULFILL_CODE_ID
2087
+ );
2088
+ frame.slots[9] = result.value;
2089
+ }
2090
+ if (result.status == OSEO_STATUS_NORMAL &&
2091
+ (combine_kind == PROMISE_COMBINE_ANY ||
2092
+ combine_kind == PROMISE_COMBINE_ALL_SETTLED)) {
2093
+ result = promise_aggregate_function_create(
2094
+ context,
2095
+ frame.slots[8],
2096
+ OSEO_PROMISE_AGGREGATE_REJECT_CODE_ID
2097
+ );
2098
+ frame.slots[10] = result.value;
2099
+ }
2100
+ if (result.status == OSEO_STATUS_NORMAL) {
2101
+ result = oseo_object_get(
2102
+ context,
2103
+ frame.slots[5],
2104
+ frame.slots[7]
2105
+ );
2106
+ frame.slots[11] = result.value;
2107
+ }
2108
+ if (result.status == OSEO_STATUS_NORMAL) {
2109
+ result = oseo_call_function(
2110
+ context,
2111
+ frame.slots[11],
2112
+ frame.slots[5],
2113
+ 2u,
2114
+ &frame.slots[9],
2115
+ oseo_undefined()
2116
+ );
2117
+ }
2118
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
2119
+ /*
2120
+ * A throw after a successful step, such as calling a
2121
+ * yielded promise's own throwing then, closes the iterator
2122
+ * before rejecting. A non-catchable diagnostic from the
2123
+ * return method propagates.
2124
+ */
2125
+ frame.slots[4] = result.value;
2126
+ oseo_context_clear_language_error(context);
2127
+ iteration_abrupt = true;
2128
+ OseoResult closed = oseo_iterator_close(
2129
+ context,
2130
+ frame.slots[12],
2131
+ true
2132
+ );
2133
+ if (closed.status != OSEO_STATUS_NORMAL) {
2134
+ result = closed;
2135
+ break;
2136
+ }
2137
+ result = promise_combine_reject_call(
2138
+ context,
2139
+ frame.slots[1],
2140
+ frame.slots[3],
2141
+ frame.slots[16],
2142
+ frame.slots[10],
2143
+ frame.slots[4],
2144
+ combine_kind
2145
+ );
2146
+ reject_call_abrupt = result.status != OSEO_STATUS_NORMAL;
2147
+ if (result.status == OSEO_STATUS_NORMAL) break;
2148
+ }
2149
+ index += 1u;
2150
+ }
2151
+ if (result.status == OSEO_STATUS_NORMAL &&
2152
+ combine_kind != PROMISE_COMBINE_RACE && !iteration_abrupt) {
2153
+ OseoPromiseAggregate *aggregate = aggregate_object(frame.slots[3]);
2154
+ aggregate->remaining -= 1u;
2155
+ if (aggregate->remaining == 0u) {
2156
+ OseoReactionKind reaction_kind = OSEO_REACTION_ALL;
2157
+ if (combine_kind == PROMISE_COMBINE_ALL_SETTLED) {
2158
+ reaction_kind = OSEO_REACTION_ALL_SETTLED;
2159
+ } else if (combine_kind == PROMISE_COMBINE_ANY) {
2160
+ reaction_kind = OSEO_REACTION_ANY;
2161
+ }
2162
+ result = promise_aggregate_complete(
2163
+ context,
2164
+ frame.slots[3],
2165
+ reaction_kind
2166
+ );
2167
+ }
2168
+ }
2169
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic &&
2170
+ !reject_call_abrupt) {
2171
+ frame.slots[4] = result.value;
2172
+ oseo_context_clear_language_error(context);
2173
+ result = promise_combine_reject_call(
2174
+ context,
2175
+ frame.slots[1],
2176
+ frame.slots[3],
2177
+ frame.slots[16],
2178
+ frame.slots[10],
2179
+ frame.slots[4],
2180
+ combine_kind
2181
+ );
2182
+ }
2183
+ if (result.status == OSEO_STATUS_NORMAL) {
2184
+ result = capability_promise(context, frame.slots[1]);
2185
+ }
2186
+ oseo_roots_release(context, &frame);
2187
+ return result;
2188
+ }
2189
+
2190
+ OseoResult oseo_promise_all(
2191
+ OseoContext *context,
2192
+ OseoValue iterable
2193
+ ) {
2194
+ OseoResult constructor =
2195
+ oseo_internal_intrinsic(context, OSEO_INTRINSIC_PROMISE);
2196
+ if (constructor.status != OSEO_STATUS_NORMAL) return constructor;
2197
+ return promise_combine(
2198
+ context,
2199
+ constructor.value,
2200
+ iterable,
2201
+ PROMISE_COMBINE_ALL
2202
+ );
2203
+ }
2204
+
2205
+ /* Invoke(promise, "then", arguments) with the first argument_count of
2206
+ * the two handlers. The count is observable through `arguments.length`,
2207
+ * so a one-argument Invoke must not pass a trailing undefined. */
2208
+ static OseoResult promise_invoke_then_with(
2209
+ OseoContext *context,
2210
+ OseoValue promise_value,
2211
+ size_t argument_count,
2212
+ OseoValue on_fulfilled,
2213
+ OseoValue on_rejected
2214
+ ) {
2215
+ static const uint16_t then_units[] = {'t', 'h', 'e', 'n'};
2216
+ OseoRootFrame frame = {NULL, NULL, 0u};
2217
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2218
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2219
+ frame.slots[0] = promise_value;
2220
+ frame.slots[1] = on_fulfilled;
2221
+ frame.slots[2] = on_rejected;
2222
+ result = oseo_string_from_units(context, then_units, 4u);
2223
+ frame.slots[3] = result.value;
2224
+ if (result.status == OSEO_STATUS_NORMAL) {
2225
+ result = oseo_object_get(
2226
+ context,
2227
+ frame.slots[0],
2228
+ frame.slots[3]
2229
+ );
2230
+ frame.slots[4] = result.value;
2231
+ }
2232
+ if (result.status == OSEO_STATUS_NORMAL) {
2233
+ result = oseo_call_function(
2234
+ context,
2235
+ frame.slots[4],
2236
+ frame.slots[0],
2237
+ argument_count,
2238
+ &frame.slots[1],
2239
+ oseo_undefined()
2240
+ );
2241
+ }
2242
+ oseo_roots_release(context, &frame);
2243
+ return result;
2244
+ }
2245
+
2246
+ OseoResult oseo_internal_promise_invoke_then(
2247
+ OseoContext *context,
2248
+ OseoValue promise_value,
2249
+ OseoValue on_fulfilled,
2250
+ OseoValue on_rejected
2251
+ ) {
2252
+ return promise_invoke_then_with(
2253
+ context,
2254
+ promise_value,
2255
+ 2u,
2256
+ on_fulfilled,
2257
+ on_rejected
2258
+ );
2259
+ }
2260
+
2261
+ static OseoResult promise_finally_function_create(
2262
+ OseoContext *context,
2263
+ OseoValue on_finally,
2264
+ OseoValue constructor,
2265
+ size_t code_id
2266
+ ) {
2267
+ OseoRootFrame frame = {NULL, NULL, 0u};
2268
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
2269
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2270
+ frame.slots[0] = on_finally;
2271
+ frame.slots[2] = constructor;
2272
+ result = oseo_environment_create(context, 2u);
2273
+ frame.slots[1] = result.value;
2274
+ if (result.status == OSEO_STATUS_NORMAL) {
2275
+ result = oseo_environment_set(
2276
+ context,
2277
+ frame.slots[1],
2278
+ 0u,
2279
+ frame.slots[0]
2280
+ );
2281
+ }
2282
+ if (result.status == OSEO_STATUS_NORMAL) {
2283
+ result = oseo_environment_set(
2284
+ context,
2285
+ frame.slots[1],
2286
+ 1u,
2287
+ frame.slots[2]
2288
+ );
2289
+ }
2290
+ if (result.status == OSEO_STATUS_NORMAL) {
2291
+ result = oseo_function_create(
2292
+ context,
2293
+ code_id,
2294
+ frame.slots[1],
2295
+ NULL,
2296
+ 0u,
2297
+ 1u,
2298
+ OSEO_FUNCTION_INTERNAL,
2299
+ oseo_undefined(),
2300
+ oseo_undefined(),
2301
+ OSEO_FUNCTION_NAME_PREFIX_NONE
2302
+ );
2303
+ }
2304
+ oseo_roots_release(context, &frame);
2305
+ return result;
2306
+ }
2307
+
2308
+ OseoResult oseo_internal_promise_finally_continuation_create(
2309
+ OseoContext *context,
2310
+ OseoValue preserved,
2311
+ bool fulfilled
2312
+ ) {
2313
+ OseoRootFrame frame = {NULL, NULL, 0u};
2314
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
2315
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2316
+ frame.slots[0] = preserved;
2317
+ result = oseo_environment_create(context, 2u);
2318
+ frame.slots[1] = result.value;
2319
+ if (result.status == OSEO_STATUS_NORMAL) {
2320
+ result = oseo_environment_set(
2321
+ context,
2322
+ frame.slots[1],
2323
+ 0u,
2324
+ frame.slots[0]
2325
+ );
2326
+ }
2327
+ if (result.status == OSEO_STATUS_NORMAL) {
2328
+ result = oseo_environment_set(
2329
+ context,
2330
+ frame.slots[1],
2331
+ 1u,
2332
+ oseo_boolean(fulfilled)
2333
+ );
2334
+ }
2335
+ if (result.status == OSEO_STATUS_NORMAL) {
2336
+ result = oseo_function_create(
2337
+ context,
2338
+ OSEO_PROMISE_FINALLY_CONTINUE_CODE_ID,
2339
+ frame.slots[1],
2340
+ NULL,
2341
+ 0u,
2342
+ 0u,
2343
+ OSEO_FUNCTION_INTERNAL,
2344
+ oseo_undefined(),
2345
+ oseo_undefined(),
2346
+ OSEO_FUNCTION_NAME_PREFIX_NONE
2347
+ );
2348
+ }
2349
+ oseo_roots_release(context, &frame);
2350
+ return result;
2351
+ }
2352
+
2353
+ /*
2354
+ * SpeciesConstructor(O, %Promise%). An absent `constructor` keeps the
2355
+ * default, a non-object one is a TypeError, and the constructor's
2356
+ * `Symbol.species` selects the result when it is neither null nor
2357
+ * undefined. Every read is observable, so a throwing getter makes the
2358
+ * whole operation abrupt.
2359
+ */
2360
+ static OseoResult promise_species_constructor(
2361
+ OseoContext *context,
2362
+ OseoValue object
2363
+ ) {
2364
+ OseoRootFrame frame = {NULL, NULL, 0u};
2365
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
2366
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2367
+ frame.slots[0] = object;
2368
+ result = oseo_internal_ascii_string(context, "constructor");
2369
+ frame.slots[1] = result.value;
2370
+ if (result.status == OSEO_STATUS_NORMAL) {
2371
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
2372
+ frame.slots[2] = result.value;
2373
+ }
2374
+ if (result.status == OSEO_STATUS_NORMAL &&
2375
+ tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
2376
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_PROMISE);
2377
+ oseo_roots_release(context, &frame);
2378
+ return result;
2379
+ }
2380
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[2])) {
2381
+ result = oseo_internal_throw_error(
2382
+ context,
2383
+ OSEO_ERROR_TYPE,
2384
+ "The promise constructor property is not an object."
2385
+ );
2386
+ }
2387
+ if (result.status == OSEO_STATUS_NORMAL) {
2388
+ result = oseo_internal_well_known_symbol(
2389
+ context,
2390
+ OSEO_WELL_KNOWN_SPECIES
2391
+ );
2392
+ frame.slots[1] = result.value;
2393
+ }
2394
+ if (result.status == OSEO_STATUS_NORMAL) {
2395
+ result = oseo_object_get(context, frame.slots[2], frame.slots[1]);
2396
+ frame.slots[2] = result.value;
2397
+ }
2398
+ if (result.status == OSEO_STATUS_NORMAL &&
2399
+ (tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED ||
2400
+ tag_of(frame.slots[2]) == OSEO_TAG_NULL)) {
2401
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_PROMISE);
2402
+ oseo_roots_release(context, &frame);
2403
+ return result;
2404
+ }
2405
+ if (result.status == OSEO_STATUS_NORMAL) {
2406
+ result = function_is_constructible(frame.slots[2])
2407
+ ? normal(frame.slots[2])
2408
+ : oseo_internal_throw_error(
2409
+ context,
2410
+ OSEO_ERROR_TYPE,
2411
+ "The promise species is not a constructor."
2412
+ );
2413
+ }
2414
+ oseo_roots_release(context, &frame);
2415
+ return result;
2416
+ }
2417
+
2418
+ /* PromiseResolve(C, x), including the observable constructor read for a
2419
+ * native promise. A matching constructor returns x itself; every other
2420
+ * value is resolved through a fresh capability for C. */
2421
+ static OseoResult promise_resolve_with(
2422
+ OseoContext *context,
2423
+ OseoValue constructor,
2424
+ OseoValue value
2425
+ ) {
2426
+ OseoRootFrame frame = {NULL, NULL, 0u};
2427
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2428
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2429
+ frame.slots[0] = constructor;
2430
+ frame.slots[1] = value;
2431
+
2432
+ bool matching_constructor = false;
2433
+ if (is_promise(frame.slots[1])) {
2434
+ static const uint16_t constructor_units[] = {
2435
+ 'c', 'o', 'n', 's', 't', 'r', 'u', 'c', 't', 'o', 'r',
2436
+ };
2437
+ result = oseo_string_from_units(
2438
+ context,
2439
+ constructor_units,
2440
+ sizeof(constructor_units) / sizeof(*constructor_units)
2441
+ );
2442
+ frame.slots[2] = result.value;
2443
+ if (result.status == OSEO_STATUS_NORMAL) {
2444
+ result = oseo_object_get(
2445
+ context,
2446
+ frame.slots[1],
2447
+ frame.slots[2]
2448
+ );
2449
+ frame.slots[3] = result.value;
2450
+ }
2451
+ matching_constructor = result.status == OSEO_STATUS_NORMAL &&
2452
+ frame.slots[3] == frame.slots[0];
2453
+ }
2454
+ if (result.status == OSEO_STATUS_NORMAL && matching_constructor) {
2455
+ result = normal(frame.slots[1]);
2456
+ } else if (result.status == OSEO_STATUS_NORMAL) {
2457
+ result = new_promise_capability(context, frame.slots[0]);
2458
+ frame.slots[4] = result.value;
2459
+ }
2460
+ if (result.status == OSEO_STATUS_NORMAL && !matching_constructor) {
2461
+ result = capability_settle(
2462
+ context,
2463
+ frame.slots[4],
2464
+ frame.slots[1],
2465
+ true
2466
+ );
2467
+ }
2468
+ if (result.status == OSEO_STATUS_NORMAL && !matching_constructor) {
2469
+ result = capability_promise(context, frame.slots[4]);
2470
+ }
2471
+ oseo_roots_release(context, &frame);
2472
+ return result;
2473
+ }
2474
+
2475
+ OseoResult oseo_internal_promise_finally_invoke(
2476
+ OseoContext *context,
2477
+ OseoValue promise_value,
2478
+ OseoValue on_finally
2479
+ ) {
2480
+ if (!is_object(promise_value)) return oseo_internal_throw_error(
2481
+ context,
2482
+ OSEO_ERROR_TYPE,
2483
+ "Promise.prototype.finally requires an object."
2484
+ );
2485
+ OseoRootFrame frame = {NULL, NULL, 0u};
2486
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2487
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2488
+ frame.slots[0] = promise_value;
2489
+ frame.slots[1] = on_finally;
2490
+ /* SpeciesConstructor runs before the callable test, so a throwing
2491
+ * `constructor` or `Symbol.species` getter is observable even when
2492
+ * `onFinally` is not callable. */
2493
+ result = promise_species_constructor(context, frame.slots[0]);
2494
+ frame.slots[4] = result.value;
2495
+ if (result.status == OSEO_STATUS_NORMAL && !is_callable(frame.slots[1])) {
2496
+ result = oseo_internal_promise_invoke_then(
2497
+ context,
2498
+ frame.slots[0],
2499
+ frame.slots[1],
2500
+ frame.slots[1]
2501
+ );
2502
+ oseo_roots_release(context, &frame);
2503
+ return result;
2504
+ }
2505
+ if (result.status == OSEO_STATUS_NORMAL) {
2506
+ result = promise_finally_function_create(
2507
+ context,
2508
+ frame.slots[1],
2509
+ frame.slots[4],
2510
+ OSEO_PROMISE_FINALLY_FULFILL_CODE_ID
2511
+ );
2512
+ frame.slots[2] = result.value;
2513
+ }
2514
+ if (result.status == OSEO_STATUS_NORMAL) {
2515
+ result = promise_finally_function_create(
2516
+ context,
2517
+ frame.slots[1],
2518
+ frame.slots[4],
2519
+ OSEO_PROMISE_FINALLY_REJECT_CODE_ID
2520
+ );
2521
+ frame.slots[3] = result.value;
2522
+ }
2523
+ if (result.status == OSEO_STATUS_NORMAL) {
2524
+ result = oseo_internal_promise_invoke_then(
2525
+ context,
2526
+ frame.slots[0],
2527
+ frame.slots[2],
2528
+ frame.slots[3]
2529
+ );
2530
+ }
2531
+ oseo_roots_release(context, &frame);
2532
+ return result;
2533
+ }
2534
+
2535
+ /*
2536
+ * Promise.prototype.then. The receiver must be a promise, the derived
2537
+ * promise comes from SpeciesConstructor, and PerformPromiseThen then
2538
+ * attaches the reaction to whichever capability that produced.
2539
+ */
2540
+ static OseoResult promise_prototype_then(
2541
+ OseoContext *context,
2542
+ OseoValue receiver,
2543
+ OseoValue on_fulfilled,
2544
+ OseoValue on_rejected
2545
+ ) {
2546
+ if (!is_promise(receiver)) return oseo_internal_throw_error(
2547
+ context,
2548
+ OSEO_ERROR_TYPE,
2549
+ "The receiver is not a promise."
2550
+ );
2551
+ OseoRootFrame frame = {NULL, NULL, 0u};
2552
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
2553
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2554
+ frame.slots[0] = receiver;
2555
+ frame.slots[1] = on_fulfilled;
2556
+ frame.slots[2] = on_rejected;
2557
+ result = promise_species_constructor(context, frame.slots[0]);
2558
+ frame.slots[3] = result.value;
2559
+ if (result.status == OSEO_STATUS_NORMAL) {
2560
+ result = new_promise_capability(context, frame.slots[3]);
2561
+ frame.slots[3] = result.value;
2562
+ }
2563
+ if (result.status == OSEO_STATUS_NORMAL) {
2564
+ result = promise_then_with_capability(
2565
+ context,
2566
+ frame.slots[0],
2567
+ frame.slots[1],
2568
+ frame.slots[2],
2569
+ frame.slots[3]
2570
+ );
2571
+ }
2572
+ oseo_roots_release(context, &frame);
2573
+ return result;
2574
+ }
2575
+
2576
+ /* The `this` value every Promise static requires before it builds a
2577
+ * capability. ECMA-262 states the object test separately from
2578
+ * NewPromiseCapability's constructor test, so a non-object receiver and
2579
+ * a non-constructor object receiver are distinguishable. */
2580
+ static OseoResult promise_static_receiver(
2581
+ OseoContext *context,
2582
+ OseoValue constructor
2583
+ ) {
2584
+ if (is_object(constructor)) return normal(constructor);
2585
+ return oseo_internal_throw_error(
2586
+ context,
2587
+ OSEO_ERROR_TYPE,
2588
+ "The Promise static receiver is not an object."
2589
+ );
2590
+ }
2591
+
2592
+ static OseoResult promise_static_resolve(
2593
+ OseoContext *context,
2594
+ OseoValue constructor,
2595
+ OseoValue value
2596
+ ) {
2597
+ OseoResult receiver = promise_static_receiver(context, constructor);
2598
+ if (receiver.status != OSEO_STATUS_NORMAL) return receiver;
2599
+ return promise_resolve_with(context, constructor, value);
2600
+ }
2601
+
2602
+ static OseoResult promise_static_reject(
2603
+ OseoContext *context,
2604
+ OseoValue constructor,
2605
+ OseoValue reason
2606
+ ) {
2607
+ OseoResult receiver = promise_static_receiver(context, constructor);
2608
+ if (receiver.status != OSEO_STATUS_NORMAL) return receiver;
2609
+ OseoRootFrame frame = {NULL, NULL, 0u};
2610
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
2611
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2612
+ frame.slots[0] = constructor;
2613
+ frame.slots[1] = reason;
2614
+ result = new_promise_capability(context, frame.slots[0]);
2615
+ frame.slots[2] = result.value;
2616
+ if (result.status == OSEO_STATUS_NORMAL) {
2617
+ result = capability_settle(
2618
+ context,
2619
+ frame.slots[2],
2620
+ frame.slots[1],
2621
+ false
2622
+ );
2623
+ }
2624
+ if (result.status == OSEO_STATUS_NORMAL) {
2625
+ result = capability_promise(context, frame.slots[2]);
2626
+ }
2627
+ oseo_roots_release(context, &frame);
2628
+ return result;
2629
+ }
2630
+
2631
+ /*
2632
+ * Promise.withResolvers. The returned object is an ordinary object with
2633
+ * three own enumerable data properties, so a caller can destructure the
2634
+ * capability the static built.
2635
+ */
2636
+ static OseoResult promise_with_resolvers(
2637
+ OseoContext *context,
2638
+ OseoValue constructor
2639
+ ) {
2640
+ OseoResult receiver = promise_static_receiver(context, constructor);
2641
+ if (receiver.status != OSEO_STATUS_NORMAL) return receiver;
2642
+ OseoRootFrame frame = {NULL, NULL, 0u};
2643
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
2644
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2645
+ frame.slots[0] = constructor;
2646
+ result = new_promise_capability(context, frame.slots[0]);
2647
+ frame.slots[1] = result.value;
2648
+ if (result.status == OSEO_STATUS_NORMAL) {
2649
+ result = oseo_internal_intrinsic(
2650
+ context,
2651
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
2652
+ );
2653
+ frame.slots[2] = result.value;
2654
+ }
2655
+ if (result.status == OSEO_STATUS_NORMAL) {
2656
+ result = oseo_object_create(context, frame.slots[2]);
2657
+ frame.slots[2] = result.value;
2658
+ }
2659
+ /* A %Promise% capability stores no resolving functions, so the pair
2660
+ * this static hands back is created here over one shared
2661
+ * already-resolved latch, exactly as CreateResolvingFunctions
2662
+ * does. */
2663
+ if (result.status == OSEO_STATUS_NORMAL &&
2664
+ !is_foreign_capability(frame.slots[1])) {
2665
+ result = resolving_environment_create(context, frame.slots[1]);
2666
+ frame.slots[3] = result.value;
2667
+ }
2668
+ /* ECMA-262 creates `promise`, then `resolve`, then `reject`, and an
2669
+ * own-key enumeration of the result observes that order. The
2670
+ * capability record stores its resolving functions at the matching
2671
+ * slots, so one index selects both. */
2672
+ static const char *const names[] = {"promise", "resolve", "reject"};
2673
+ for (size_t index = 0u;
2674
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
2675
+ index += 1u) {
2676
+ if (index == 0u) {
2677
+ result = capability_promise(context, frame.slots[1]);
2678
+ } else if (is_foreign_capability(frame.slots[1])) {
2679
+ result = oseo_environment_get(context, frame.slots[1], index);
2680
+ } else {
2681
+ result = resolving_function_create(
2682
+ context,
2683
+ frame.slots[3],
2684
+ index == 1u
2685
+ ? OSEO_PROMISE_RESOLVE_CODE_ID
2686
+ : OSEO_PROMISE_REJECT_CODE_ID
2687
+ );
2688
+ }
2689
+ frame.slots[5] = result.value;
2690
+ if (result.status != OSEO_STATUS_NORMAL) break;
2691
+ result = oseo_internal_ascii_string(context, names[index]);
2692
+ frame.slots[4] = result.value;
2693
+ if (result.status != OSEO_STATUS_NORMAL) break;
2694
+ result = oseo_object_define(
2695
+ context,
2696
+ frame.slots[2],
2697
+ frame.slots[4],
2698
+ frame.slots[5],
2699
+ (OseoPropertyAttributes){true, true, true, false}
2700
+ );
2701
+ }
2702
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[2];
2703
+ oseo_roots_release(context, &frame);
2704
+ return result;
2705
+ }
2706
+
2707
+ /*
2708
+ * Promise.try. The callback runs synchronously with the remaining
2709
+ * arguments, and its completion settles the capability, so a throw
2710
+ * becomes a rejection rather than propagating to the caller.
2711
+ */
2712
+ static OseoResult promise_static_try(
2713
+ OseoContext *context,
2714
+ OseoValue constructor,
2715
+ size_t argument_count,
2716
+ const OseoValue *arguments
2717
+ ) {
2718
+ OseoResult receiver = promise_static_receiver(context, constructor);
2719
+ if (receiver.status != OSEO_STATUS_NORMAL) return receiver;
2720
+ OseoRootFrame frame = {NULL, NULL, 0u};
2721
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
2722
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2723
+ frame.slots[0] = constructor;
2724
+ frame.slots[1] = argument_count > 0u ? arguments[0] : oseo_undefined();
2725
+ result = new_promise_capability(context, frame.slots[0]);
2726
+ frame.slots[2] = result.value;
2727
+ if (result.status != OSEO_STATUS_NORMAL) {
2728
+ oseo_roots_release(context, &frame);
2729
+ return result;
2730
+ }
2731
+ result = oseo_call_function(
2732
+ context,
2733
+ frame.slots[1],
2734
+ oseo_undefined(),
2735
+ argument_count > 1u ? argument_count - 1u : 0u,
2736
+ argument_count > 1u ? &arguments[1] : NULL,
2737
+ oseo_undefined()
2738
+ );
2739
+ frame.slots[3] = result.value;
2740
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
2741
+ oseo_context_clear_language_error(context);
2742
+ result = capability_settle(
2743
+ context,
2744
+ frame.slots[2],
2745
+ frame.slots[3],
2746
+ false
2747
+ );
2748
+ } else if (result.status == OSEO_STATUS_NORMAL) {
2749
+ result = capability_settle(
2750
+ context,
2751
+ frame.slots[2],
2752
+ frame.slots[3],
2753
+ true
2754
+ );
2755
+ }
2756
+ if (result.status == OSEO_STATUS_NORMAL) {
2757
+ result = capability_promise(context, frame.slots[2]);
2758
+ }
2759
+ oseo_roots_release(context, &frame);
2760
+ return result;
2761
+ }
2762
+
2763
+ /*
2764
+ * The Promise constructor's [[Construct]]. ECMA-262 rejects a
2765
+ * non-callable executor before it allocates anything, takes the
2766
+ * prototype from the new target, and turns a throwing executor into a
2767
+ * rejection of the promise it already created.
2768
+ */
2769
+ static OseoResult promise_construct_with_target(
2770
+ OseoContext *context,
2771
+ OseoValue new_target,
2772
+ OseoValue executor
2773
+ ) {
2774
+ if (!is_callable(executor)) return oseo_internal_throw_error(
2775
+ context,
2776
+ OSEO_ERROR_TYPE,
2777
+ "The promise executor is not a function."
2778
+ );
2779
+ OseoRootFrame frame = {NULL, NULL, 0u};
2780
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
2781
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2782
+ frame.slots[0] = executor;
2783
+ frame.slots[5] = new_target;
2784
+ result = promise_create_from_constructor(context, frame.slots[5]);
2785
+ frame.slots[1] = result.value;
2786
+ if (result.status == OSEO_STATUS_NORMAL) {
2787
+ result = resolving_environment_create(context, frame.slots[1]);
2788
+ frame.slots[4] = result.value;
2789
+ }
2790
+ if (result.status == OSEO_STATUS_NORMAL) {
2791
+ result = resolving_function_create(
2792
+ context,
2793
+ frame.slots[4],
2794
+ OSEO_PROMISE_RESOLVE_CODE_ID
2795
+ );
2796
+ frame.slots[2] = result.value;
2797
+ }
2798
+ if (result.status == OSEO_STATUS_NORMAL) {
2799
+ result = resolving_function_create(
2800
+ context,
2801
+ frame.slots[4],
2802
+ OSEO_PROMISE_REJECT_CODE_ID
2803
+ );
2804
+ frame.slots[3] = result.value;
2805
+ }
2806
+ if (result.status == OSEO_STATUS_NORMAL) {
2807
+ result = oseo_call_function(
2808
+ context,
2809
+ frame.slots[0],
2810
+ oseo_undefined(),
2811
+ 2u,
2812
+ &frame.slots[2],
2813
+ oseo_undefined()
2814
+ );
2815
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
2816
+ frame.slots[4] = result.value;
2817
+ oseo_context_clear_language_error(context);
2818
+ result = oseo_call_function(
2819
+ context,
2820
+ frame.slots[3],
2821
+ oseo_undefined(),
2822
+ 1u,
2823
+ &frame.slots[4],
2824
+ oseo_undefined()
2825
+ );
2826
+ }
2827
+ }
2828
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
2829
+ oseo_roots_release(context, &frame);
2830
+ return result;
2831
+ }
2832
+
2833
+ static OseoResult promise_then_with_capability(
2834
+ OseoContext *context,
2835
+ OseoValue promise_value,
2836
+ OseoValue on_fulfilled,
2837
+ OseoValue on_rejected,
2838
+ OseoValue capability
2839
+ ) {
2840
+ if (!is_promise(promise_value)) return oseo_internal_throw_error(
2841
+ context,
2842
+ OSEO_ERROR_TYPE,
2843
+ "The receiver is not a promise."
2844
+ );
2845
+ if (tag_of(on_fulfilled) != OSEO_TAG_UNDEFINED &&
2846
+ !is_callable(on_fulfilled)) {
2847
+ on_fulfilled = oseo_undefined();
2848
+ }
2849
+ if (tag_of(on_rejected) != OSEO_TAG_UNDEFINED &&
2850
+ !is_callable(on_rejected)) {
2851
+ on_rejected = oseo_undefined();
2852
+ }
2853
+ OseoRootFrame frame = {NULL, NULL, 0u};
2854
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2855
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2856
+ frame.slots[0] = promise_value;
2857
+ frame.slots[1] = on_fulfilled;
2858
+ frame.slots[2] = on_rejected;
2859
+ frame.slots[3] = capability;
2860
+ if (tag_of(frame.slots[3]) == OSEO_TAG_UNDEFINED) {
2861
+ result = oseo_internal_promise_create(context);
2862
+ frame.slots[3] = result.value;
2863
+ } else if (!is_promise(frame.slots[3]) &&
2864
+ !is_foreign_capability(frame.slots[3])) {
2865
+ result = failure(
2866
+ context,
2867
+ "OSEO2001",
2868
+ "Invalid promise reaction capability."
2869
+ );
2870
+ }
2871
+ if (result.status == OSEO_STATUS_NORMAL) {
2872
+ result = reaction_create(
2873
+ context,
2874
+ frame.slots[1],
2875
+ frame.slots[2],
2876
+ frame.slots[3]
2877
+ );
2878
+ frame.slots[4] = result.value;
2879
+ }
2880
+ if (result.status == OSEO_STATUS_NORMAL) {
2881
+ result = promise_attach_reaction(
2882
+ context,
2883
+ frame.slots[0],
2884
+ frame.slots[4]
2885
+ );
2886
+ }
2887
+ if (result.status == OSEO_STATUS_NORMAL) {
2888
+ result = capability_promise(context, frame.slots[3]);
2889
+ }
2890
+ oseo_roots_release(context, &frame);
2891
+ return result;
2892
+ }
2893
+
2894
+ OseoResult oseo_internal_promise_then_module(
2895
+ OseoContext *context,
2896
+ OseoValue promise_value,
2897
+ OseoValue evaluator,
2898
+ size_t module_index
2899
+ ) {
2900
+ if (!is_promise(promise_value)) {
2901
+ return failure(
2902
+ context,
2903
+ "OSEO2001",
2904
+ "The module evaluation result is not a promise."
2905
+ );
2906
+ }
2907
+ OseoRootFrame frame = {NULL, NULL, 0u};
2908
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
2909
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2910
+ frame.slots[0] = promise_value;
2911
+ frame.slots[1] = evaluator;
2912
+ result = reaction_create(
2913
+ context,
2914
+ frame.slots[1],
2915
+ frame.slots[1],
2916
+ oseo_undefined()
2917
+ );
2918
+ frame.slots[2] = result.value;
2919
+ if (result.status == OSEO_STATUS_NORMAL) {
2920
+ OseoPromiseReaction *reaction = reaction_object(frame.slots[2]);
2921
+ reaction->kind = OSEO_REACTION_MODULE;
2922
+ reaction->index = module_index;
2923
+ result = promise_attach_reaction(
2924
+ context,
2925
+ frame.slots[0],
2926
+ frame.slots[2]
2927
+ );
2928
+ }
2929
+ oseo_roots_release(context, &frame);
2930
+ return result;
2931
+ }
2932
+
2933
+ OseoResult oseo_internal_promise_then_import(
2934
+ OseoContext *context,
2935
+ OseoValue promise_value,
2936
+ OseoValue capability,
2937
+ OseoValue evaluator,
2938
+ size_t module_index,
2939
+ OseoReactionKind kind
2940
+ ) {
2941
+ bool queued_now = tag_of(promise_value) == OSEO_TAG_UNDEFINED;
2942
+ if (!queued_now && !is_promise(promise_value)) {
2943
+ return failure(
2944
+ context,
2945
+ "OSEO2001",
2946
+ "The module evaluation result is not a promise."
2947
+ );
2948
+ }
2949
+ OseoRootFrame frame = {NULL, NULL, 0u};
2950
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
2951
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2952
+ frame.slots[0] = promise_value;
2953
+ frame.slots[1] = evaluator;
2954
+ frame.slots[2] = capability;
2955
+ result = reaction_create(
2956
+ context,
2957
+ frame.slots[1],
2958
+ frame.slots[1],
2959
+ frame.slots[2]
2960
+ );
2961
+ frame.slots[3] = result.value;
2962
+ if (result.status == OSEO_STATUS_NORMAL) {
2963
+ OseoPromiseReaction *reaction = reaction_object(frame.slots[3]);
2964
+ reaction->kind = kind;
2965
+ reaction->index = module_index;
2966
+ result = queued_now
2967
+ ? enqueue_job(
2968
+ context,
2969
+ OSEO_JOB_REACTION,
2970
+ frame.slots[3],
2971
+ oseo_undefined(),
2972
+ oseo_undefined(),
2973
+ true
2974
+ )
2975
+ : promise_attach_reaction(
2976
+ context,
2977
+ frame.slots[0],
2978
+ frame.slots[3]
2979
+ );
2980
+ }
2981
+ oseo_roots_release(context, &frame);
2982
+ return result;
2983
+ }
2984
+
2985
+ OseoResult oseo_promise_then(
2986
+ OseoContext *context,
2987
+ OseoValue promise_value,
2988
+ OseoValue on_fulfilled,
2989
+ OseoValue on_rejected
2990
+ ) {
2991
+ return promise_then_with_capability(
2992
+ context,
2993
+ promise_value,
2994
+ on_fulfilled,
2995
+ on_rejected,
2996
+ oseo_undefined()
2997
+ );
2998
+ }
2999
+
3000
+ OseoResult oseo_promise_await_then(
3001
+ OseoContext *context,
3002
+ OseoValue promise_value,
3003
+ OseoValue on_fulfilled
3004
+ ) {
3005
+ OseoValue capability = context->async_call_capability;
3006
+ context->async_call_capability = oseo_undefined();
3007
+ return promise_then_with_capability(
3008
+ context,
3009
+ promise_value,
3010
+ on_fulfilled,
3011
+ oseo_undefined(),
3012
+ capability
3013
+ );
3014
+ }
3015
+
3016
+ OseoResult oseo_promise_async_call(
3017
+ OseoContext *context,
3018
+ OseoValue execution
3019
+ ) {
3020
+ if (!is_function(execution)) return oseo_internal_throw_error(
3021
+ context,
3022
+ OSEO_ERROR_TYPE,
3023
+ "The asynchronous execution is not a function."
3024
+ );
3025
+ OseoRootFrame frame = {NULL, NULL, 0u};
3026
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
3027
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3028
+ frame.slots[0] = execution;
3029
+ result = oseo_internal_promise_create(context);
3030
+ frame.slots[1] = result.value;
3031
+ if (result.status == OSEO_STATUS_NORMAL) {
3032
+ frame.slots[3] = context->async_call_capability;
3033
+ context->async_call_capability = frame.slots[1];
3034
+ result = oseo_call_function(
3035
+ context,
3036
+ frame.slots[0],
3037
+ oseo_undefined(),
3038
+ 0u,
3039
+ NULL,
3040
+ oseo_undefined()
3041
+ );
3042
+ frame.slots[2] = result.value;
3043
+ context->async_call_capability = frame.slots[3];
3044
+ }
3045
+ if (result.status == OSEO_STATUS_NORMAL &&
3046
+ frame.slots[2] == frame.slots[1]) {
3047
+ result.value = frame.slots[1];
3048
+ } else if (result.status == OSEO_STATUS_NORMAL) {
3049
+ result = oseo_promise_resolve_into(
3050
+ context,
3051
+ frame.slots[1],
3052
+ frame.slots[2]
3053
+ );
3054
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
3055
+ } else if (result.status == OSEO_STATUS_THROW &&
3056
+ !context->has_diagnostic) {
3057
+ oseo_context_clear_language_error(context);
3058
+ result = oseo_promise_reject_into(
3059
+ context,
3060
+ frame.slots[1],
3061
+ frame.slots[2]
3062
+ );
3063
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
3064
+ }
3065
+ oseo_roots_release(context, &frame);
3066
+ return result;
3067
+ }
3068
+
3069
+ OseoResult oseo_promise_finally(
3070
+ OseoContext *context,
3071
+ OseoValue promise_value,
3072
+ OseoValue on_finally
3073
+ ) {
3074
+ return oseo_internal_promise_finally_invoke(
3075
+ context, promise_value, on_finally);
3076
+ }
3077
+
3078
+ OseoPromiseState oseo_promise_state(OseoValue promise) {
3079
+ return is_promise(promise)
3080
+ ? promise_object(promise)->state
3081
+ : OSEO_PROMISE_INVALID;
3082
+ }
3083
+
3084
+ OseoResult oseo_promise_result(
3085
+ OseoContext *context,
3086
+ OseoValue promise
3087
+ ) {
3088
+ if (!is_promise(promise) ||
3089
+ promise_object(promise)->state == OSEO_PROMISE_PENDING) {
3090
+ return oseo_internal_throw_error(
3091
+ context,
3092
+ OSEO_ERROR_TYPE,
3093
+ "The promise has no settled result."
3094
+ );
3095
+ }
3096
+ return normal(promise_object(promise)->result);
3097
+ }
3098
+
3099
+ static OseoResult run_reaction_job(
3100
+ OseoContext *context,
3101
+ OseoValue job_value
3102
+ ) {
3103
+ OseoPromiseReaction *selected = reaction_object(
3104
+ job_object(job_value)->primary
3105
+ );
3106
+ if (selected->kind == OSEO_REACTION_ALL ||
3107
+ selected->kind == OSEO_REACTION_ALL_SETTLED ||
3108
+ selected->kind == OSEO_REACTION_ANY ||
3109
+ selected->kind == OSEO_REACTION_RACE) {
3110
+ OseoJob *job = job_object(job_value);
3111
+ return oseo_internal_promise_aggregate_settle(
3112
+ context,
3113
+ job->primary,
3114
+ job->argument,
3115
+ job->fulfilled
3116
+ );
3117
+ }
3118
+ OseoRootFrame frame = {NULL, NULL, 0u};
3119
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
3120
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3121
+ OseoJob *job = job_object(job_value);
3122
+ OseoPromiseReaction *reaction = reaction_object(job->primary);
3123
+ frame.slots[0] = job->fulfilled
3124
+ ? reaction->on_fulfilled
3125
+ : reaction->on_rejected;
3126
+ frame.slots[1] = reaction->capability;
3127
+ frame.slots[2] = job->argument;
3128
+ bool fulfilled = job->fulfilled;
3129
+ const char *source_id = context->source_id;
3130
+ size_t source_id_length = context->source_id_length;
3131
+ size_t line = context->line;
3132
+ size_t column = context->column;
3133
+ if (!fulfilled && is_promise(job->secondary)) {
3134
+ OseoPromise *source = promise_object(job->secondary);
3135
+ context->source_id = source->rejection_source_id;
3136
+ context->source_id_length = source->rejection_source_id_length;
3137
+ context->line = source->rejection_line;
3138
+ context->column = source->rejection_column;
3139
+ }
3140
+ if (reaction->kind == OSEO_REACTION_MODULE ||
3141
+ reaction->kind == OSEO_REACTION_IMPORT_EVALUATE ||
3142
+ reaction->kind == OSEO_REACTION_IMPORT_SETTLE) {
3143
+ result = reaction->kind == OSEO_REACTION_MODULE
3144
+ ? oseo_internal_module_async_settled(
3145
+ context,
3146
+ reaction->index,
3147
+ frame.slots[2],
3148
+ fulfilled
3149
+ )
3150
+ : oseo_internal_module_import_step(
3151
+ context,
3152
+ reaction->kind,
3153
+ reaction->index,
3154
+ frame.slots[1],
3155
+ frame.slots[2],
3156
+ fulfilled
3157
+ );
3158
+ if (!context->has_diagnostic) {
3159
+ context->source_id = source_id;
3160
+ context->source_id_length = source_id_length;
3161
+ context->line = line;
3162
+ context->column = column;
3163
+ }
3164
+ oseo_roots_release(context, &frame);
3165
+ return result;
3166
+ }
3167
+ if (tag_of(frame.slots[0]) == OSEO_TAG_UNDEFINED) {
3168
+ result = capability_settle(
3169
+ context,
3170
+ frame.slots[1],
3171
+ frame.slots[2],
3172
+ fulfilled
3173
+ );
3174
+ if (!context->has_diagnostic) {
3175
+ context->source_id = source_id;
3176
+ context->source_id_length = source_id_length;
3177
+ context->line = line;
3178
+ context->column = column;
3179
+ }
3180
+ oseo_roots_release(context, &frame);
3181
+ return result;
3182
+ }
3183
+ result = oseo_call_function(
3184
+ context,
3185
+ frame.slots[0],
3186
+ oseo_undefined(),
3187
+ 1u,
3188
+ &frame.slots[2],
3189
+ oseo_undefined()
3190
+ );
3191
+ frame.slots[3] = result.value;
3192
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
3193
+ oseo_context_clear_language_error(context);
3194
+ result = capability_settle(
3195
+ context,
3196
+ frame.slots[1],
3197
+ frame.slots[3],
3198
+ false
3199
+ );
3200
+ } else if (result.status == OSEO_STATUS_NORMAL) {
3201
+ result = capability_settle(
3202
+ context,
3203
+ frame.slots[1],
3204
+ frame.slots[3],
3205
+ true
3206
+ );
3207
+ }
3208
+ if (!context->has_diagnostic) {
3209
+ context->source_id = source_id;
3210
+ context->source_id_length = source_id_length;
3211
+ context->line = line;
3212
+ context->column = column;
3213
+ }
3214
+ oseo_roots_release(context, &frame);
3215
+ return result;
3216
+ }
3217
+
3218
+ static OseoResult run_thenable_job(
3219
+ OseoContext *context,
3220
+ OseoValue job_value
3221
+ ) {
3222
+ OseoRootFrame frame = {NULL, NULL, 0u};
3223
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
3224
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3225
+ OseoJob *job = job_object(job_value);
3226
+ frame.slots[0] = job->primary;
3227
+ frame.slots[1] = job->secondary;
3228
+ frame.slots[2] = job->argument;
3229
+ result = resolving_environment_create(context, frame.slots[0]);
3230
+ frame.slots[5] = result.value;
3231
+ if (result.status == OSEO_STATUS_NORMAL) {
3232
+ result = resolving_function_create(
3233
+ context,
3234
+ frame.slots[5],
3235
+ OSEO_PROMISE_RESOLVE_CODE_ID
3236
+ );
3237
+ frame.slots[3] = result.value;
3238
+ }
3239
+ if (result.status == OSEO_STATUS_NORMAL) {
3240
+ result = resolving_function_create(
3241
+ context,
3242
+ frame.slots[5],
3243
+ OSEO_PROMISE_REJECT_CODE_ID
3244
+ );
3245
+ frame.slots[4] = result.value;
3246
+ }
3247
+ if (result.status == OSEO_STATUS_NORMAL) {
3248
+ result = oseo_call_function(
3249
+ context,
3250
+ frame.slots[1],
3251
+ frame.slots[2],
3252
+ 2u,
3253
+ &frame.slots[3],
3254
+ oseo_undefined()
3255
+ );
3256
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
3257
+ frame.slots[5] = result.value;
3258
+ oseo_context_clear_language_error(context);
3259
+ result = oseo_call_function(
3260
+ context,
3261
+ frame.slots[4],
3262
+ oseo_undefined(),
3263
+ 1u,
3264
+ &frame.slots[5],
3265
+ oseo_undefined()
3266
+ );
3267
+ }
3268
+ }
3269
+ oseo_roots_release(context, &frame);
3270
+ return result;
3271
+ }
3272
+
3273
+ bool oseo_internal_jobs_reached_promise(OseoValue promise) {
3274
+ return is_promise(promise) &&
3275
+ promise_object(promise)->state != OSEO_PROMISE_PENDING;
3276
+ }
3277
+
3278
+ /*
3279
+ * Runs the oldest promise job as a job of its own. The preceding job's
3280
+ * KeptAlive set ends first, whether that job was the script, a timer
3281
+ * callback, a cleanup job, an earlier promise job of the same drain, or the
3282
+ * job suspended by an internal await that drains these jobs directly.
3283
+ */
3284
+ static OseoResult jobs_run_next(OseoContext *context) {
3285
+ OseoRootFrame frame = {NULL, NULL, 0u};
3286
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
3287
+ if (result.status != OSEO_STATUS_NORMAL) return result;
3288
+ if (tag_of(context->microtask_head) != OSEO_TAG_UNDEFINED) {
3289
+ oseo_internal_clear_kept_objects(context);
3290
+ frame.slots[0] = context->microtask_head;
3291
+ OseoValue next = job_object(frame.slots[0])->next;
3292
+ context->microtask_head = next;
3293
+ if (tag_of(next) == OSEO_TAG_UNDEFINED) {
3294
+ context->microtask_tail = oseo_undefined();
3295
+ }
3296
+ OseoRealmScope scope;
3297
+ oseo_internal_realm_enter(
3298
+ context,
3299
+ &scope,
3300
+ job_object(frame.slots[0])->realm
3301
+ );
3302
+ result = job_object(frame.slots[0])->kind == OSEO_JOB_REACTION
3303
+ ? run_reaction_job(context, frame.slots[0])
3304
+ : run_thenable_job(context, frame.slots[0]);
3305
+ oseo_internal_realm_leave(context, &scope);
3306
+ }
3307
+ oseo_roots_release(context, &frame);
3308
+ return result;
3309
+ }
3310
+
3311
+ OseoResult oseo_internal_jobs_drain_until(
3312
+ OseoContext *context,
3313
+ OseoValue promise
3314
+ ) {
3315
+ OseoResult result = normal(oseo_undefined());
3316
+ while (tag_of(context->microtask_head) != OSEO_TAG_UNDEFINED &&
3317
+ !oseo_internal_jobs_reached_promise(promise)) {
3318
+ result = jobs_run_next(context);
3319
+ if (result.status != OSEO_STATUS_NORMAL) break;
3320
+ }
3321
+ return result;
3322
+ }
3323
+
3324
+ OseoResult oseo_jobs_drain(OseoContext *context) {
3325
+ return oseo_internal_jobs_drain_until(context, oseo_undefined());
3326
+ }
3327
+
3328
+ OseoResult oseo_rejection_checkpoint(OseoContext *context) {
3329
+ OseoValue current = context->pending_rejections;
3330
+ OseoValue first = oseo_undefined();
3331
+ const char *first_source_id = context->source_id;
3332
+ size_t first_source_id_length = context->source_id_length;
3333
+ size_t first_line = context->line;
3334
+ size_t first_column = context->column;
3335
+ bool found = false;
3336
+ context->pending_rejections = oseo_undefined();
3337
+ context->pending_rejection_tail = oseo_undefined();
3338
+ while (tag_of(current) != OSEO_TAG_UNDEFINED) {
3339
+ OseoPromise *promise = promise_object(current);
3340
+ OseoValue next = promise->unhandled_next;
3341
+ promise->unhandled_next = oseo_undefined();
3342
+ if (promise->pending_report && !promise->handled &&
3343
+ promise->state == OSEO_PROMISE_REJECTED) {
3344
+ promise->pending_report = false;
3345
+ promise->reported = true;
3346
+ context->unhandled_rejection_count += 1u;
3347
+ if (!found) {
3348
+ first = promise->result;
3349
+ first_source_id = promise->rejection_source_id;
3350
+ first_source_id_length =
3351
+ promise->rejection_source_id_length;
3352
+ first_line = promise->rejection_line;
3353
+ first_column = promise->rejection_column;
3354
+ found = true;
3355
+ }
3356
+ }
3357
+ current = next;
3358
+ }
3359
+ /*
3360
+ * The observe policy has already recorded every report above; it
3361
+ * only declines to end the program. A thrown timer or cleanup
3362
+ * callback is a separate completion its caller keeps, and an internal
3363
+ * await reads its own promise, so neither depends on this decision.
3364
+ */
3365
+ if (!found ||
3366
+ context->unhandled_rejection_policy ==
3367
+ OSEO_UNHANDLED_REJECTION_OBSERVE) {
3368
+ return normal(oseo_undefined());
3369
+ }
3370
+ context->error_code = "OSEO2001";
3371
+ context->error_message = OSEO_UNHANDLED_REJECTION_MESSAGE;
3372
+ context->has_diagnostic = false;
3373
+ context->source_id = first_source_id;
3374
+ context->source_id_length = first_source_id_length;
3375
+ context->line = first_line;
3376
+ context->column = first_column;
3377
+ return (OseoResult){OSEO_STATUS_THROW, first};
3378
+ }