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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +1677 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2594 -0
  10. package/native/runtime_agent.c +1693 -0
  11. package/native/runtime_arguments.c +355 -0
  12. package/native/runtime_array.c +5742 -0
  13. package/native/runtime_array_buffer.c +1724 -0
  14. package/native/runtime_async_generator.c +948 -0
  15. package/native/runtime_atomics.c +1259 -0
  16. package/native/runtime_bigint.c +1363 -0
  17. package/native/runtime_bigint_object.c +602 -0
  18. package/native/runtime_binding.c +1005 -0
  19. package/native/runtime_boolean.c +296 -0
  20. package/native/runtime_clock.c +157 -0
  21. package/native/runtime_clock_posix.c +575 -0
  22. package/native/runtime_core.c +464 -0
  23. package/native/runtime_data_view.c +1111 -0
  24. package/native/runtime_date.c +1973 -0
  25. package/native/runtime_descriptor.c +1080 -0
  26. package/native/runtime_disposal.c +1643 -0
  27. package/native/runtime_enumeration.c +612 -0
  28. package/native/runtime_error.c +973 -0
  29. package/native/runtime_event_loop.c +629 -0
  30. package/native/runtime_function.c +3643 -0
  31. package/native/runtime_generator.c +1096 -0
  32. package/native/runtime_internal.h +5554 -0
  33. package/native/runtime_iterator.c +5355 -0
  34. package/native/runtime_json.c +1968 -0
  35. package/native/runtime_map.c +1539 -0
  36. package/native/runtime_math.c +894 -0
  37. package/native/runtime_memory.c +1341 -0
  38. package/native/runtime_module.c +860 -0
  39. package/native/runtime_number.c +2053 -0
  40. package/native/runtime_object.c +399 -0
  41. package/native/runtime_object_builtin.c +3877 -0
  42. package/native/runtime_primitive.c +2244 -0
  43. package/native/runtime_process.c +720 -0
  44. package/native/runtime_promise.c +3354 -0
  45. package/native/runtime_property.c +791 -0
  46. package/native/runtime_proxy.c +1533 -0
  47. package/native/runtime_realm.c +411 -0
  48. package/native/runtime_reflect.c +624 -0
  49. package/native/runtime_regexp.c +3495 -0
  50. package/native/runtime_regexp_matcher.c +3535 -0
  51. package/native/runtime_regexp_symbol.c +1471 -0
  52. package/native/runtime_set.c +1751 -0
  53. package/native/runtime_string.c +2862 -0
  54. package/native/runtime_string_match.c +1161 -0
  55. package/native/runtime_symbol.c +986 -0
  56. package/native/runtime_typed_array.c +5113 -0
  57. package/native/runtime_unicode_tables.h +14647 -0
  58. package/native/runtime_uri.c +676 -0
  59. package/native/runtime_weak_collection.c +1275 -0
  60. package/package.json +38 -3
@@ -0,0 +1,629 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <math.h>
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /*
12
+ * Timer conversion, timer queues, task checkpoints, top-level
13
+ * await progress, and shutdown.
14
+ *
15
+ * Deadlines are whole milliseconds in the realm's monotonic domain. A
16
+ * task computes every deadline from the scheduler time cached when it
17
+ * started, and a timer turn waits through the clock adapter until the
18
+ * earliest live deadline has elapsed before it caches the new time. The
19
+ * deterministic test adapter's wait advances straight to the deadline,
20
+ * which reproduces the logical clock of ADR 0012 exactly; a platform
21
+ * adapter's wait blocks for the elapsed time instead. Neither changes
22
+ * the order of timers with known deadlines or the microtask checkpoint
23
+ * after each task.
24
+ */
25
+
26
+ static uint64_t timer_delay(OseoValue value) {
27
+ double delay = number_value(value);
28
+ if (!isfinite(delay) || delay <= 0.0) return 0u;
29
+ if (delay >= (double)UINT32_MAX) return UINT32_MAX;
30
+ return (uint64_t)delay;
31
+ }
32
+
33
+ /* Links one published timer into the deadline-ordered queue. */
34
+ static void timer_link(OseoContext *context, OseoValue timer_value) {
35
+ OseoTimer *timer = timer_object(timer_value);
36
+ OseoValue *link = &context->timer_head;
37
+ while (tag_of(*link) != OSEO_TAG_UNDEFINED) {
38
+ OseoTimer *current = timer_object(*link);
39
+ if (current->deadline > timer->deadline ||
40
+ (current->deadline == timer->deadline &&
41
+ current->order > timer->order)) {
42
+ break;
43
+ }
44
+ link = &current->next;
45
+ }
46
+ timer->next = *link;
47
+ *link = timer_value;
48
+ }
49
+
50
+ OseoResult oseo_set_timeout(
51
+ OseoContext *context,
52
+ size_t argument_count,
53
+ const OseoValue *arguments
54
+ ) {
55
+ if (argument_count == 0u || !is_callable(arguments[0])) {
56
+ return oseo_internal_throw_error(
57
+ context,
58
+ OSEO_ERROR_TYPE,
59
+ "The timer callback is not a function."
60
+ );
61
+ }
62
+ OseoValue delay_value = argument_count > 1u
63
+ ? arguments[1]
64
+ : oseo_number(0.0);
65
+ OseoResult result = oseo_internal_to_number(context, delay_value);
66
+ if (result.status != OSEO_STATUS_NORMAL) return result;
67
+ uint64_t delay = timer_delay(result.value);
68
+ result = oseo_internal_clock_start(context);
69
+ if (result.status != OSEO_STATUS_NORMAL) return result;
70
+ size_t callback_argument_count = argument_count > 2u
71
+ ? argument_count - 2u
72
+ : 0u;
73
+ OseoRootFrame frame = {NULL, NULL, 0u};
74
+ result = oseo_roots_allocate(context, &frame, 3u);
75
+ if (result.status != OSEO_STATUS_NORMAL) return result;
76
+ frame.slots[0] = arguments[0];
77
+ result = oseo_environment_create(context, callback_argument_count);
78
+ frame.slots[1] = result.value;
79
+ for (size_t index = 0u;
80
+ result.status == OSEO_STATUS_NORMAL &&
81
+ index < callback_argument_count;
82
+ index += 1u) {
83
+ result = oseo_environment_set(
84
+ context,
85
+ frame.slots[1],
86
+ index,
87
+ arguments[index + 2u]
88
+ );
89
+ }
90
+ OseoTimer *timer = NULL;
91
+ if (result.status == OSEO_STATUS_NORMAL) {
92
+ timer = oseo_internal_allocate_heap_bytes(context, sizeof(*timer));
93
+ if (timer == NULL) {
94
+ result = failure(
95
+ context,
96
+ "OSEO2001",
97
+ "Timer allocation failed."
98
+ );
99
+ }
100
+ }
101
+ if (result.status == OSEO_STATUS_NORMAL && timer != NULL) {
102
+ timer->next = oseo_undefined();
103
+ timer->callback = frame.slots[0];
104
+ timer->arguments = frame.slots[1];
105
+ timer->waiter = oseo_undefined();
106
+ timer->deadline = UINT64_MAX - context->clock_milliseconds < delay
107
+ ? UINT64_MAX
108
+ : context->clock_milliseconds + delay;
109
+ timer->id = context->next_timer_id;
110
+ context->next_timer_id += 1u;
111
+ timer->order = context->next_timer_order;
112
+ context->next_timer_order += 1u;
113
+ timer->argument_count = callback_argument_count;
114
+ timer->canceled = false;
115
+ result = oseo_internal_publish_heap(
116
+ context, &timer->header, OSEO_HEAP_TIMER);
117
+ frame.slots[2] = result.value;
118
+ }
119
+ if (result.status == OSEO_STATUS_NORMAL) {
120
+ timer_link(context, frame.slots[2]);
121
+ result.value = oseo_number((double)timer->id);
122
+ }
123
+ oseo_roots_release(context, &frame);
124
+ return result;
125
+ }
126
+
127
+ /*
128
+ * Enqueues the EnqueueAtomicsWaitAsyncTimeoutJob of one waiter at an
129
+ * absolute monotonic deadline, with the same FIFO tie order as
130
+ * `setTimeout`, so a waiter timeout and a timer due at the same time run
131
+ * in the order they were enqueued. The job is a host timeout job like any
132
+ * other and keeps the event loop running until it is due or canceled.
133
+ */
134
+ OseoResult oseo_internal_atomics_timeout_enqueue(
135
+ OseoContext *context,
136
+ OseoValue waiter,
137
+ uint64_t deadline
138
+ ) {
139
+ OseoValue slots[2] = {waiter, oseo_undefined()};
140
+ OseoRootFrame frame = {NULL, slots, 2u};
141
+ oseo_roots_push(context, &frame);
142
+ OseoTimer *timer = oseo_internal_allocate_heap_bytes(
143
+ context,
144
+ sizeof(*timer)
145
+ );
146
+ if (timer == NULL) {
147
+ oseo_roots_pop(context, &frame);
148
+ return failure(context, "OSEO2001", "Timer allocation failed.");
149
+ }
150
+ timer->next = oseo_undefined();
151
+ timer->callback = oseo_undefined();
152
+ timer->arguments = oseo_undefined();
153
+ timer->waiter = frame.slots[0];
154
+ timer->deadline = deadline;
155
+ timer->id = 0u;
156
+ timer->order = context->next_timer_order;
157
+ context->next_timer_order += 1u;
158
+ timer->argument_count = 0u;
159
+ timer->canceled = false;
160
+ OseoResult result = oseo_internal_publish_heap(
161
+ context,
162
+ &timer->header,
163
+ OSEO_HEAP_TIMER
164
+ );
165
+ frame.slots[1] = result.value;
166
+ if (result.status == OSEO_STATUS_NORMAL) {
167
+ timer_link(context, frame.slots[1]);
168
+ }
169
+ oseo_roots_pop(context, &frame);
170
+ return result;
171
+ }
172
+
173
+ OseoResult oseo_clear_timeout(
174
+ OseoContext *context,
175
+ OseoValue handle
176
+ ) {
177
+ if (!is_number(handle)) return normal(oseo_undefined());
178
+ double requested = number_value(handle);
179
+ OseoValue current = context->timer_head;
180
+ while (tag_of(current) != OSEO_TAG_UNDEFINED) {
181
+ OseoTimer *timer = timer_object(current);
182
+ if (timer->id != 0u && (double)timer->id == requested) {
183
+ timer->canceled = true;
184
+ break;
185
+ }
186
+ current = timer->next;
187
+ }
188
+ return normal(oseo_undefined());
189
+ }
190
+
191
+ /*
192
+ * Waits until the earliest live timer is due and caches that monotonic
193
+ * observation as the scheduler time of the turn that runs it. Canceled
194
+ * timers at the head are unlinked first, so a canceled deadline never
195
+ * extends a wait and a queue of canceled timers alone ends without one.
196
+ * An early wakeup only rereads the clock. The wait is not a safepoint:
197
+ * no allocation happens between reading the head and running it.
198
+ */
199
+ static OseoResult wait_for_due_timer(OseoContext *context) {
200
+ while (tag_of(context->timer_head) != OSEO_TAG_UNDEFINED) {
201
+ OseoTimer *timer = timer_object(context->timer_head);
202
+ if (timer->canceled) {
203
+ context->timer_head = timer->next;
204
+ timer->next = oseo_undefined();
205
+ continue;
206
+ }
207
+ uint64_t now = 0u;
208
+ OseoResult result = oseo_internal_clock_now(context, &now);
209
+ if (result.status != OSEO_STATUS_NORMAL) return result;
210
+ if (timer->deadline <= now) {
211
+ context->clock_milliseconds = now;
212
+ break;
213
+ }
214
+ /* An agent of a multi-agent cluster gives up its turn while it
215
+ * waits, so the other agents keep running. */
216
+ result = context->agent != NULL
217
+ ? oseo_internal_agent_idle(context, true, timer->deadline)
218
+ : oseo_internal_clock_wait_until(context, timer->deadline);
219
+ if (result.status != OSEO_STATUS_NORMAL) return result;
220
+ }
221
+ return normal(oseo_undefined());
222
+ }
223
+
224
+ /*
225
+ * Unlinks canceled timers at the head and reports the earliest live
226
+ * deadline, or false when no live timer remains.
227
+ */
228
+ static bool next_timer_deadline(OseoContext *context, uint64_t *deadline) {
229
+ while (tag_of(context->timer_head) != OSEO_TAG_UNDEFINED) {
230
+ OseoTimer *timer = timer_object(context->timer_head);
231
+ if (!timer->canceled) {
232
+ *deadline = timer->deadline;
233
+ return true;
234
+ }
235
+ context->timer_head = timer->next;
236
+ timer->next = oseo_undefined();
237
+ }
238
+ return false;
239
+ }
240
+
241
+ /*
242
+ * Runs the earliest due timer as a job of its own. The preceding job's
243
+ * KeptAlive set ends before the callback. The outer event loop has already
244
+ * cleared it in its finalization checkpoint, but an internal await drives
245
+ * this turn directly, and its timer callback must not observe a target
246
+ * kept alive only by the WeakRef construction or deref of the job that
247
+ * awaited.
248
+ */
249
+ static OseoResult run_timer_turn(
250
+ OseoContext *context,
251
+ OseoValue awaited_promise
252
+ ) {
253
+ OseoResult result = wait_for_due_timer(context);
254
+ if (result.status != OSEO_STATUS_NORMAL) return result;
255
+ oseo_internal_clear_kept_objects(context);
256
+ OseoRootFrame frame = {NULL, NULL, 0u};
257
+ result = oseo_roots_allocate(context, &frame, 3u);
258
+ if (result.status != OSEO_STATUS_NORMAL) return result;
259
+ while (tag_of(context->timer_head) != OSEO_TAG_UNDEFINED) {
260
+ frame.slots[0] = context->timer_head;
261
+ OseoTimer *timer = timer_object(frame.slots[0]);
262
+ context->timer_head = timer->next;
263
+ timer->next = oseo_undefined();
264
+ if (timer->canceled) {
265
+ frame.slots[0] = oseo_undefined();
266
+ continue;
267
+ }
268
+ if (tag_of(timer->waiter) != OSEO_TAG_UNDEFINED) {
269
+ /* A waiter timeout job runs no user code itself, so it can
270
+ * only fail with a host diagnostic; its promise reactions
271
+ * run in the checkpoint below like a callback's. */
272
+ result = oseo_internal_atomics_waiter_timeout(
273
+ context,
274
+ timer->waiter
275
+ );
276
+ } else {
277
+ frame.slots[1] = timer->arguments;
278
+ result = oseo_call_function(
279
+ context,
280
+ timer->callback,
281
+ oseo_undefined(),
282
+ timer->argument_count,
283
+ environment_object(frame.slots[1])->slots,
284
+ oseo_undefined()
285
+ );
286
+ }
287
+ OseoResult callback_result = result;
288
+ const char *callback_error_code = context->error_code;
289
+ const char *callback_error_message = context->error_message;
290
+ const char *callback_source_id = context->source_id;
291
+ size_t callback_source_id_length = context->source_id_length;
292
+ size_t callback_line = context->line;
293
+ size_t callback_column = context->column;
294
+ bool callback_threw = result.status == OSEO_STATUS_THROW &&
295
+ !context->has_diagnostic;
296
+ if (callback_threw) {
297
+ frame.slots[2] = result.value;
298
+ result = oseo_internal_jobs_drain_until(context, awaited_promise);
299
+ if (result.status == OSEO_STATUS_NORMAL &&
300
+ !oseo_internal_jobs_reached_promise(awaited_promise)) {
301
+ result = oseo_rejection_checkpoint(context);
302
+ }
303
+ if (result.status == OSEO_STATUS_NORMAL ||
304
+ !context->has_diagnostic) {
305
+ context->error_code = callback_error_code;
306
+ context->error_message = callback_error_message;
307
+ context->has_diagnostic = false;
308
+ context->source_id = callback_source_id;
309
+ context->source_id_length = callback_source_id_length;
310
+ context->line = callback_line;
311
+ context->column = callback_column;
312
+ callback_result.value = frame.slots[2];
313
+ result = callback_result;
314
+ }
315
+ }
316
+ if (result.status == OSEO_STATUS_NORMAL) {
317
+ result = oseo_internal_jobs_drain_until(context, awaited_promise);
318
+ }
319
+ if (result.status == OSEO_STATUS_NORMAL &&
320
+ !oseo_internal_jobs_reached_promise(awaited_promise)) {
321
+ result = oseo_rejection_checkpoint(context);
322
+ }
323
+ frame.slots[0] = oseo_undefined();
324
+ frame.slots[1] = oseo_undefined();
325
+ frame.slots[2] = oseo_undefined();
326
+ break;
327
+ }
328
+ oseo_roots_release(context, &frame);
329
+ return result;
330
+ }
331
+
332
+ static OseoResult run_finalization_turns(
333
+ OseoContext *context,
334
+ OseoValue awaited_promise
335
+ );
336
+
337
+ /*
338
+ * An internal compatibility checkpoint for iterator adapter operations that
339
+ * do not yet own a traced frame. Module top-level await and `for await` never
340
+ * use this path. While the awaited promise stays pending, each timer turn it
341
+ * drives follows the finalization cleanup checkpoint, exactly as in the outer
342
+ * event loop, so cleanup jobs a collection queued run before the next timer.
343
+ * A body that can make no further progress reports a host diagnostic naming
344
+ * the stalled operation.
345
+ */
346
+ static OseoResult await_settled_value(
347
+ OseoContext *context,
348
+ OseoValue value,
349
+ const char *stall_message
350
+ ) {
351
+ OseoRootFrame frame = {NULL, NULL, 0u};
352
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
353
+ if (result.status != OSEO_STATUS_NORMAL) return result;
354
+ frame.slots[0] = value;
355
+ result = oseo_promise_resolve(context, frame.slots[0]);
356
+ frame.slots[0] = result.value;
357
+ if (result.status == OSEO_STATUS_NORMAL) {
358
+ result = oseo_promise_then(
359
+ context,
360
+ frame.slots[0],
361
+ oseo_undefined(),
362
+ oseo_undefined()
363
+ );
364
+ frame.slots[1] = result.value;
365
+ }
366
+ if (result.status == OSEO_STATUS_NORMAL) {
367
+ OseoPromise *awaited = promise_object(frame.slots[1]);
368
+ awaited->handled = true;
369
+ awaited->pending_report = false;
370
+ result = oseo_internal_jobs_drain_until(context, frame.slots[1]);
371
+ }
372
+ if (result.status == OSEO_STATUS_NORMAL &&
373
+ promise_object(frame.slots[1])->state == OSEO_PROMISE_PENDING) {
374
+ result = oseo_rejection_checkpoint(context);
375
+ }
376
+ while (result.status == OSEO_STATUS_NORMAL &&
377
+ promise_object(frame.slots[1])->state == OSEO_PROMISE_PENDING) {
378
+ result = run_finalization_turns(context, frame.slots[1]);
379
+ if (result.status != OSEO_STATUS_NORMAL ||
380
+ promise_object(frame.slots[1])->state != OSEO_PROMISE_PENDING) {
381
+ break;
382
+ }
383
+ if (tag_of(context->timer_head) == OSEO_TAG_UNDEFINED) {
384
+ result = failure(context, "OSEO3001", stall_message);
385
+ break;
386
+ }
387
+ result = run_timer_turn(context, frame.slots[1]);
388
+ }
389
+ if (result.status == OSEO_STATUS_NORMAL) {
390
+ OseoPromise *awaited = promise_object(frame.slots[1]);
391
+ result.value = awaited->result;
392
+ if (awaited->state == OSEO_PROMISE_REJECTED) {
393
+ context->source_id = awaited->rejection_source_id;
394
+ context->source_id_length =
395
+ awaited->rejection_source_id_length;
396
+ context->line = awaited->rejection_line;
397
+ context->column = awaited->rejection_column;
398
+ result.status = OSEO_STATUS_THROW;
399
+ }
400
+ }
401
+ oseo_roots_release(context, &frame);
402
+ return result;
403
+ }
404
+
405
+ OseoResult oseo_internal_await_step(OseoContext *context, OseoValue value) {
406
+ return await_settled_value(
407
+ context,
408
+ value,
409
+ "An asynchronous iteration step cannot make progress."
410
+ );
411
+ }
412
+
413
+ OseoResult oseo_entry_task_checkpoint(
414
+ OseoContext *context,
415
+ OseoResult completion
416
+ ) {
417
+ if (completion.status != OSEO_STATUS_THROW ||
418
+ context->has_diagnostic) {
419
+ return completion;
420
+ }
421
+ const char *error_code = context->error_code;
422
+ const char *error_message = context->error_message;
423
+ const char *source_id = context->source_id;
424
+ size_t source_id_length = context->source_id_length;
425
+ size_t line = context->line;
426
+ size_t column = context->column;
427
+ OseoRootFrame frame = {NULL, NULL, 0u};
428
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
429
+ if (result.status != OSEO_STATUS_NORMAL) return result;
430
+ frame.slots[0] = completion.value;
431
+ result = oseo_jobs_drain(context);
432
+ if (result.status == OSEO_STATUS_NORMAL) {
433
+ result = oseo_rejection_checkpoint(context);
434
+ }
435
+ if (result.status == OSEO_STATUS_NORMAL || !context->has_diagnostic) {
436
+ context->error_code = error_code;
437
+ context->error_message = error_message;
438
+ context->has_diagnostic = false;
439
+ context->source_id = source_id;
440
+ context->source_id_length = source_id_length;
441
+ context->line = line;
442
+ context->column = column;
443
+ completion.value = frame.slots[0];
444
+ result = completion;
445
+ }
446
+ oseo_roots_release(context, &frame);
447
+ return result;
448
+ }
449
+
450
+ /*
451
+ * The finalization cleanup checkpoint. It first ends the current job's
452
+ * KeptAlive set, then runs one cleanup job per registry with queued
453
+ * records, ordered by each registry's oldest record. A job calls the
454
+ * callback for all of its registry's records before promise jobs drain,
455
+ * exactly as a timer turn drains them after its callback. An abrupt
456
+ * callback ends the loop with that completion. Under an internal await,
457
+ * `awaited_promise` bounds each drain as it bounds a timer turn's, and no
458
+ * further cleanup job starts once that promise has settled; the outer event
459
+ * loop passes `undefined`.
460
+ */
461
+ static OseoResult run_finalization_turns(
462
+ OseoContext *context,
463
+ OseoValue awaited_promise
464
+ ) {
465
+ OseoRootFrame frame = {NULL, NULL, 0u};
466
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
467
+ if (result.status != OSEO_STATUS_NORMAL) return result;
468
+ oseo_internal_clear_kept_objects(context);
469
+ while (result.status == OSEO_STATUS_NORMAL &&
470
+ !oseo_internal_jobs_reached_promise(awaited_promise)) {
471
+ bool ran = false;
472
+ result = oseo_internal_finalization_cleanup_job(context, &ran);
473
+ if (!ran) break;
474
+ OseoResult callback_result = result;
475
+ const char *callback_error_code = context->error_code;
476
+ const char *callback_error_message = context->error_message;
477
+ const char *callback_source_id = context->source_id;
478
+ size_t callback_source_id_length = context->source_id_length;
479
+ size_t callback_line = context->line;
480
+ size_t callback_column = context->column;
481
+ bool callback_threw = result.status == OSEO_STATUS_THROW &&
482
+ !context->has_diagnostic;
483
+ if (callback_threw) {
484
+ frame.slots[0] = result.value;
485
+ result = normal(oseo_undefined());
486
+ }
487
+ if (result.status == OSEO_STATUS_NORMAL) {
488
+ result = oseo_internal_jobs_drain_until(context, awaited_promise);
489
+ }
490
+ if (result.status == OSEO_STATUS_NORMAL &&
491
+ !oseo_internal_jobs_reached_promise(awaited_promise)) {
492
+ result = oseo_rejection_checkpoint(context);
493
+ }
494
+ oseo_internal_clear_kept_objects(context);
495
+ if (callback_threw &&
496
+ (result.status == OSEO_STATUS_NORMAL ||
497
+ !context->has_diagnostic)) {
498
+ context->error_code = callback_error_code;
499
+ context->error_message = callback_error_message;
500
+ context->has_diagnostic = false;
501
+ context->source_id = callback_source_id;
502
+ context->source_id_length = callback_source_id_length;
503
+ context->line = callback_line;
504
+ context->column = callback_column;
505
+ callback_result.value = frame.slots[0];
506
+ result = callback_result;
507
+ }
508
+ frame.slots[0] = oseo_undefined();
509
+ }
510
+ oseo_roots_release(context, &frame);
511
+ return result.status == OSEO_STATUS_NORMAL
512
+ ? normal(oseo_undefined())
513
+ : result;
514
+ }
515
+
516
+ static OseoResult entry_promise_completion(
517
+ OseoContext *context,
518
+ OseoValue entry_promise
519
+ ) {
520
+ if (!is_promise(entry_promise)) return normal(oseo_undefined());
521
+ OseoPromise *promise = promise_object(entry_promise);
522
+ if (promise->state == OSEO_PROMISE_PENDING) {
523
+ return normal(oseo_undefined());
524
+ }
525
+ if (promise->state == OSEO_PROMISE_FULFILLED) {
526
+ return normal(promise->result);
527
+ }
528
+ context->source_id = promise->rejection_source_id;
529
+ context->source_id_length = promise->rejection_source_id_length;
530
+ context->line = promise->rejection_line;
531
+ context->column = promise->rejection_column;
532
+ return (OseoResult){OSEO_STATUS_THROW, promise->result};
533
+ }
534
+
535
+ /*
536
+ * One step of the event loop of an agent in a multi-agent cluster.
537
+ * Incoming work from other agents comes first: a waitAsync waiter another
538
+ * agent notified resolves, and a retrieved broadcast becomes a due timer
539
+ * callback. Then a due timer runs as usual. With nothing due, the agent
540
+ * gives up its turn until its next deadline or incoming work, and with no
541
+ * timer left it keeps waiting only while the cluster could still hand it
542
+ * work; otherwise `finished` reports that the loop is over.
543
+ */
544
+ static OseoResult run_agent_turn(
545
+ OseoContext *context,
546
+ OseoValue entry,
547
+ bool *finished
548
+ ) {
549
+ bool ran = false;
550
+ OseoResult result = oseo_internal_agent_receive(context, &ran);
551
+ if (result.status == OSEO_STATUS_NORMAL && ran) {
552
+ result = oseo_jobs_drain(context);
553
+ if (result.status == OSEO_STATUS_NORMAL) {
554
+ result = oseo_rejection_checkpoint(context);
555
+ }
556
+ } else if (result.status == OSEO_STATUS_NORMAL) {
557
+ uint64_t deadline = 0u;
558
+ uint64_t now = 0u;
559
+ if (next_timer_deadline(context, &deadline)) {
560
+ result = oseo_internal_clock_now(context, &now);
561
+ if (result.status == OSEO_STATUS_NORMAL) {
562
+ result = now >= deadline
563
+ ? run_timer_turn(context, oseo_undefined())
564
+ : oseo_internal_agent_idle(context, true, deadline);
565
+ }
566
+ } else if (oseo_internal_agent_keeps_alive(context)) {
567
+ result = oseo_internal_agent_idle(context, false, 0u);
568
+ } else {
569
+ *finished = true;
570
+ return normal(oseo_undefined());
571
+ }
572
+ }
573
+ if (result.status == OSEO_STATUS_NORMAL) {
574
+ result = run_finalization_turns(context, oseo_undefined());
575
+ }
576
+ if (result.status == OSEO_STATUS_NORMAL) {
577
+ result = entry_promise_completion(context, entry);
578
+ }
579
+ return result;
580
+ }
581
+
582
+ OseoResult oseo_event_loop_run(
583
+ OseoContext *context,
584
+ OseoValue entry_promise
585
+ ) {
586
+ OseoValue slots[1] = {entry_promise};
587
+ OseoRootFrame frame = {NULL, slots, 1u};
588
+ oseo_roots_push(context, &frame);
589
+ if (is_promise(frame.slots[0])) {
590
+ OseoPromise *promise = promise_object(frame.slots[0]);
591
+ promise->handled = true;
592
+ promise->pending_report = false;
593
+ }
594
+ OseoResult result = oseo_jobs_drain(context);
595
+ if (result.status == OSEO_STATUS_NORMAL) {
596
+ result = oseo_rejection_checkpoint(context);
597
+ }
598
+ if (result.status == OSEO_STATUS_NORMAL) {
599
+ result = run_finalization_turns(context, oseo_undefined());
600
+ }
601
+ if (result.status == OSEO_STATUS_NORMAL) {
602
+ result = entry_promise_completion(context, frame.slots[0]);
603
+ }
604
+ bool finished = context->agent == NULL;
605
+ while (result.status == OSEO_STATUS_NORMAL && !finished) {
606
+ result = run_agent_turn(context, frame.slots[0], &finished);
607
+ }
608
+ while (result.status == OSEO_STATUS_NORMAL &&
609
+ tag_of(context->timer_head) != OSEO_TAG_UNDEFINED) {
610
+ result = run_timer_turn(context, oseo_undefined());
611
+ if (result.status == OSEO_STATUS_NORMAL) {
612
+ result = run_finalization_turns(context, oseo_undefined());
613
+ }
614
+ if (result.status == OSEO_STATUS_NORMAL) {
615
+ result = entry_promise_completion(context, frame.slots[0]);
616
+ }
617
+ }
618
+ if (result.status == OSEO_STATUS_NORMAL &&
619
+ is_promise(frame.slots[0]) &&
620
+ promise_object(frame.slots[0])->state == OSEO_PROMISE_PENDING) {
621
+ result = failure(
622
+ context,
623
+ "OSEO3001",
624
+ "Top-level await cannot make progress."
625
+ );
626
+ }
627
+ oseo_roots_pop(context, &frame);
628
+ return result;
629
+ }