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