@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,860 @@
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 <stdint.h>
8
+ #include <stdlib.h>
9
+
10
+ /*
11
+ * The runtime module evaluation driver: Evaluate, InnerModuleEvaluation,
12
+ * ExecuteAsyncModule, GatherAvailableAncestors,
13
+ * AsyncModuleExecutionFulfilled, and AsyncModuleExecutionRejected of
14
+ * ECMA-262 16th edition clause 16.2.1.6, over the module table the build
15
+ * emits. The driver dispatches compiled evaluators and runs no source.
16
+ *
17
+ * Every record's evaluator, top-level capability promise, and evaluation
18
+ * error stay reachable through the context's table, so a value stored in
19
+ * a record is rooted from that point on. Indices are plain integers that
20
+ * name table entries; the stack and the ancestor lists hold no values.
21
+ * Internal reactions use PerformPromiseThen directly and never read
22
+ * `then`, call `Promise.all`, or consult the global `Promise` binding.
23
+ */
24
+
25
+ static OseoModuleTable *module_table(OseoContext *context) {
26
+ return (OseoModuleTable *)context->module_table;
27
+ }
28
+
29
+ static OseoModuleRecord *module_record(OseoContext *context, size_t index) {
30
+ return &module_table(context)->records[index];
31
+ }
32
+
33
+ /* A record's [[CycleRoot]], or the record itself while it has none. */
34
+ static size_t module_cycle_root(OseoContext *context, size_t index) {
35
+ size_t root = module_record(context, index)->cycle_root;
36
+ return root == OSEO_MODULE_NONE ? index : root;
37
+ }
38
+
39
+ static bool module_async_pending(const OseoModuleRecord *record) {
40
+ return record->async_evaluation_order != OSEO_MODULE_ASYNC_UNSET &&
41
+ record->async_evaluation_order != OSEO_MODULE_ASYNC_DONE;
42
+ }
43
+
44
+ static OseoResult module_allocation_failure(OseoContext *context) {
45
+ return failure(
46
+ context,
47
+ "OSEO2001",
48
+ "Module evaluation state allocation failed."
49
+ );
50
+ }
51
+
52
+ /* A ThrowCompletion of a recorded [[EvaluationError]]. */
53
+ static OseoResult module_throw(OseoContext *context, OseoValue error) {
54
+ oseo_context_clear_language_error(context);
55
+ return (OseoResult){OSEO_STATUS_THROW, error};
56
+ }
57
+
58
+ static OseoResult module_add_async_parent(
59
+ OseoContext *context,
60
+ size_t required,
61
+ size_t parent
62
+ ) {
63
+ OseoModuleRecord *record = module_record(context, required);
64
+ if (record->async_parent_count == record->async_parent_capacity) {
65
+ size_t capacity = record->async_parent_capacity == 0u
66
+ ? 4u
67
+ : record->async_parent_capacity * 2u;
68
+ size_t *parents = realloc(
69
+ record->async_parents,
70
+ capacity * sizeof(*parents)
71
+ );
72
+ if (parents == NULL) return module_allocation_failure(context);
73
+ record->async_parents = parents;
74
+ record->async_parent_capacity = capacity;
75
+ }
76
+ record->async_parents[record->async_parent_count] = parent;
77
+ record->async_parent_count += 1u;
78
+ return normal(oseo_undefined());
79
+ }
80
+
81
+ /* Calls a module's compiled evaluator with no arguments. */
82
+ static OseoResult module_call_evaluator(OseoContext *context, size_t index) {
83
+ return oseo_call_function(
84
+ context,
85
+ module_record(context, index)->evaluator,
86
+ oseo_undefined(),
87
+ 0u,
88
+ NULL,
89
+ oseo_undefined()
90
+ );
91
+ }
92
+
93
+ /*
94
+ * ExecuteAsyncModule. The asynchronous evaluator's own result promise is
95
+ * the capability the body settles: it runs to its first await before the
96
+ * call returns, and a body that never suspends has already settled it,
97
+ * so attaching the reaction afterwards queues the same job at the same
98
+ * point as attaching it first.
99
+ */
100
+ static OseoResult module_execute_async(OseoContext *context, size_t index) {
101
+ OseoRootFrame frame = {NULL, NULL, 0u};
102
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
103
+ if (result.status != OSEO_STATUS_NORMAL) return result;
104
+ frame.slots[1] = module_record(context, index)->evaluator;
105
+ result = module_call_evaluator(context, index);
106
+ frame.slots[0] = result.value;
107
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
108
+ /*
109
+ * An abrupt call, such as an exhausted native stack, rejects the
110
+ * capability the way an abrupt body would.
111
+ */
112
+ oseo_context_clear_language_error(context);
113
+ result = oseo_internal_promise_create(context);
114
+ if (result.status == OSEO_STATUS_NORMAL) {
115
+ OseoValue error = frame.slots[0];
116
+ frame.slots[0] = result.value;
117
+ frame.slots[1] = error;
118
+ result = oseo_promise_reject_into(context, frame.slots[0], error);
119
+ frame.slots[1] = module_record(context, index)->evaluator;
120
+ }
121
+ }
122
+ if (result.status == OSEO_STATUS_NORMAL) {
123
+ result = oseo_internal_promise_then_module(
124
+ context,
125
+ frame.slots[0],
126
+ frame.slots[1],
127
+ index
128
+ );
129
+ }
130
+ oseo_roots_release(context, &frame);
131
+ return result;
132
+ }
133
+
134
+ /* The outcome of entering one module in InnerModuleEvaluation. */
135
+ typedef enum {
136
+ OSEO_MODULE_VISIT_ENTERED,
137
+ OSEO_MODULE_VISIT_SKIPPED,
138
+ } OseoModuleVisit;
139
+
140
+ /*
141
+ * Steps 2 through 6 of InnerModuleEvaluation: a module already evaluated
142
+ * or on the stack is skipped, an errored one throws its recorded error,
143
+ * and a linked one is pushed with a fresh frame.
144
+ */
145
+ static OseoResult module_enter(
146
+ OseoContext *context,
147
+ size_t index,
148
+ size_t *dfs_index,
149
+ OseoModuleVisit *visit
150
+ ) {
151
+ OseoModuleRecord *record = module_record(context, index);
152
+ *visit = OSEO_MODULE_VISIT_SKIPPED;
153
+ if (record->status == OSEO_MODULE_EVALUATING_ASYNC ||
154
+ record->status == OSEO_MODULE_EVALUATED) {
155
+ return record->errored
156
+ ? module_throw(context, record->evaluation_error)
157
+ : normal(oseo_undefined());
158
+ }
159
+ if (record->status == OSEO_MODULE_EVALUATING) {
160
+ return normal(oseo_undefined());
161
+ }
162
+ record->status = OSEO_MODULE_EVALUATING;
163
+ record->dfs_index = *dfs_index;
164
+ record->dfs_ancestor_index = *dfs_index;
165
+ record->pending_async_dependencies = 0u;
166
+ *dfs_index += 1u;
167
+ OseoModuleTable *table = module_table(context);
168
+ table->stack[table->stack_count] = index;
169
+ table->stack_count += 1u;
170
+ table->frames[table->frame_count] =
171
+ (OseoModuleFrame){index, 0u, false};
172
+ table->frame_count += 1u;
173
+ *visit = OSEO_MODULE_VISIT_ENTERED;
174
+ return normal(oseo_undefined());
175
+ }
176
+
177
+ /* Step 7.b of InnerModuleEvaluation for one evaluated request. */
178
+ static OseoResult module_after_request(
179
+ OseoContext *context,
180
+ size_t index,
181
+ size_t required
182
+ ) {
183
+ OseoModuleRecord *record = module_record(context, index);
184
+ OseoModuleRecord *required_record = module_record(context, required);
185
+ if (required_record->status == OSEO_MODULE_EVALUATING) {
186
+ if (required_record->dfs_ancestor_index <
187
+ record->dfs_ancestor_index) {
188
+ record->dfs_ancestor_index = required_record->dfs_ancestor_index;
189
+ }
190
+ } else {
191
+ required = module_cycle_root(context, required);
192
+ required_record = module_record(context, required);
193
+ if (required_record->errored) {
194
+ return module_throw(context, required_record->evaluation_error);
195
+ }
196
+ }
197
+ if (module_async_pending(required_record)) {
198
+ record->pending_async_dependencies += 1u;
199
+ return module_add_async_parent(context, required, index);
200
+ }
201
+ return normal(oseo_undefined());
202
+ }
203
+
204
+ /* Steps 8 through 12 of InnerModuleEvaluation, after every request. */
205
+ static OseoResult module_after_requests(OseoContext *context, size_t index) {
206
+ OseoModuleTable *table = module_table(context);
207
+ OseoModuleRecord *record = module_record(context, index);
208
+ if (record->pending_async_dependencies > 0u ||
209
+ record->has_top_level_await) {
210
+ table->async_evaluation_count += 1u;
211
+ record->async_evaluation_order = table->async_evaluation_count;
212
+ if (record->pending_async_dependencies == 0u) {
213
+ OseoResult result = module_execute_async(context, index);
214
+ if (result.status != OSEO_STATUS_NORMAL) return result;
215
+ }
216
+ } else {
217
+ OseoResult result = module_call_evaluator(context, index);
218
+ if (result.status != OSEO_STATUS_NORMAL) return result;
219
+ }
220
+ record = module_record(context, index);
221
+ if (record->dfs_ancestor_index == record->dfs_index) {
222
+ size_t member;
223
+ do {
224
+ table->stack_count -= 1u;
225
+ member = table->stack[table->stack_count];
226
+ OseoModuleRecord *popped = module_record(context, member);
227
+ popped->status =
228
+ popped->async_evaluation_order == OSEO_MODULE_ASYNC_UNSET
229
+ ? OSEO_MODULE_EVALUATED
230
+ : OSEO_MODULE_EVALUATING_ASYNC;
231
+ popped->cycle_root = index;
232
+ } while (member != index);
233
+ }
234
+ return normal(oseo_undefined());
235
+ }
236
+
237
+ /*
238
+ * InnerModuleEvaluation over an explicit frame stack, so graph depth never
239
+ * consumes native stack. Each frame names a module and its next request; a
240
+ * frame whose request entered a module resumes at step 7.b once that
241
+ * module's frame is gone. A THROW without a diagnostic is the abrupt
242
+ * completion Evaluate records; one with a diagnostic ends the program.
243
+ */
244
+ static OseoResult module_inner_evaluation(
245
+ OseoContext *context,
246
+ size_t index,
247
+ size_t *dfs_index
248
+ ) {
249
+ OseoModuleTable *table = module_table(context);
250
+ table->frame_count = 0u;
251
+ OseoModuleVisit visit;
252
+ OseoResult result = module_enter(context, index, dfs_index, &visit);
253
+ while (result.status == OSEO_STATUS_NORMAL && table->frame_count > 0u) {
254
+ OseoModuleFrame *frame = &table->frames[table->frame_count - 1u];
255
+ size_t module = frame->module;
256
+ OseoModuleRecord *record = module_record(context, module);
257
+ if (frame->position == record->requested_count) {
258
+ table->frame_count -= 1u;
259
+ result = module_after_requests(context, module);
260
+ continue;
261
+ }
262
+ size_t required = record->requested[frame->position];
263
+ if (!frame->entered_request) {
264
+ result = module_enter(context, required, dfs_index, &visit);
265
+ if (result.status != OSEO_STATUS_NORMAL) break;
266
+ if (visit == OSEO_MODULE_VISIT_ENTERED) {
267
+ /* Resume this frame at step 7.b after the pushed one. */
268
+ frame->entered_request = true;
269
+ continue;
270
+ }
271
+ }
272
+ frame->entered_request = false;
273
+ frame->position += 1u;
274
+ result = module_after_request(context, module, required);
275
+ }
276
+ table->frame_count = 0u;
277
+ return result;
278
+ }
279
+
280
+ /* Settles a record's top-level capability when it has one. */
281
+ static OseoResult module_settle_capability(
282
+ OseoContext *context,
283
+ size_t index,
284
+ bool fulfilled
285
+ ) {
286
+ OseoModuleRecord *record = module_record(context, index);
287
+ if (!is_promise(record->top_level_capability)) {
288
+ return normal(oseo_undefined());
289
+ }
290
+ return fulfilled
291
+ ? oseo_promise_resolve_into(
292
+ context,
293
+ record->top_level_capability,
294
+ oseo_undefined()
295
+ )
296
+ : oseo_promise_reject_into(
297
+ context,
298
+ record->top_level_capability,
299
+ record->evaluation_error
300
+ );
301
+ }
302
+
303
+ /* Evaluate. Returns the cycle root's top-level capability promise. */
304
+ static OseoResult module_evaluate(OseoContext *context, size_t index) {
305
+ OseoModuleRecord *record = module_record(context, index);
306
+ if (record->status == OSEO_MODULE_EVALUATING_ASYNC ||
307
+ record->status == OSEO_MODULE_EVALUATED) {
308
+ index = module_cycle_root(context, index);
309
+ record = module_record(context, index);
310
+ }
311
+ if (is_promise(record->top_level_capability)) {
312
+ return normal(record->top_level_capability);
313
+ }
314
+ OseoModuleTable *table = module_table(context);
315
+ if (table->evaluating) {
316
+ /*
317
+ * Only a job drained inside a running evaluator, as an
318
+ * asynchronous iteration step in synchronous code does, can
319
+ * reach Evaluate while another Evaluate is on the stack.
320
+ */
321
+ return failure(
322
+ context,
323
+ "OSEO2001",
324
+ "A module evaluation started inside another module evaluation."
325
+ );
326
+ }
327
+ OseoResult result = oseo_internal_promise_create(context);
328
+ if (result.status != OSEO_STATUS_NORMAL) return result;
329
+ module_record(context, index)->top_level_capability = result.value;
330
+ table->stack_count = 0u;
331
+ size_t dfs_index = 0u;
332
+ table->evaluating = true;
333
+ result = module_inner_evaluation(context, index, &dfs_index);
334
+ table->evaluating = false;
335
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
336
+ OseoValue error = result.value;
337
+ oseo_context_clear_language_error(context);
338
+ for (size_t position = 0u;
339
+ position < table->stack_count;
340
+ position += 1u) {
341
+ OseoModuleRecord *member =
342
+ module_record(context, table->stack[position]);
343
+ member->status = OSEO_MODULE_EVALUATED;
344
+ member->errored = true;
345
+ member->evaluation_error = error;
346
+ }
347
+ table->stack_count = 0u;
348
+ record = module_record(context, index);
349
+ if (!record->errored) {
350
+ record->errored = true;
351
+ record->evaluation_error = error;
352
+ }
353
+ result = module_settle_capability(context, index, false);
354
+ } else if (result.status == OSEO_STATUS_NORMAL &&
355
+ module_record(context, index)->status ==
356
+ OSEO_MODULE_EVALUATED) {
357
+ /*
358
+ * A root that finished synchronously, or whose asynchronous
359
+ * evaluation completed before this capability existed, settles
360
+ * the new capability now; nothing else would.
361
+ */
362
+ result = module_settle_capability(context, index, true);
363
+ }
364
+ if (result.status != OSEO_STATUS_NORMAL) return result;
365
+ return normal(module_record(context, index)->top_level_capability);
366
+ }
367
+
368
+ typedef struct {
369
+ size_t *items;
370
+ size_t count;
371
+ size_t capacity;
372
+ } OseoModuleList;
373
+
374
+ static bool module_list_append(OseoModuleList *list, size_t index) {
375
+ if (list->count == list->capacity) {
376
+ size_t capacity = list->capacity == 0u ? 8u : list->capacity * 2u;
377
+ size_t *items = realloc(list->items, capacity * sizeof(*items));
378
+ if (items == NULL) return false;
379
+ list->items = items;
380
+ list->capacity = capacity;
381
+ }
382
+ list->items[list->count] = index;
383
+ list->count += 1u;
384
+ return true;
385
+ }
386
+
387
+ /*
388
+ * GatherAvailableAncestors over a worklist instead of recursion. The
389
+ * edition's recursion only decides which ancestors join the list, and
390
+ * the caller sorts the list by [[AsyncEvaluationOrder]], so visiting the
391
+ * same modules in worklist order yields the same sorted list. Returns
392
+ * false when a list cannot grow.
393
+ */
394
+ static bool module_gather_available_ancestors(
395
+ OseoContext *context,
396
+ size_t index,
397
+ OseoModuleList *list
398
+ ) {
399
+ OseoModuleList work = {NULL, 0u, 0u};
400
+ bool grown = module_list_append(&work, index);
401
+ while (grown && work.count > 0u) {
402
+ work.count -= 1u;
403
+ size_t module = work.items[work.count];
404
+ for (size_t position = 0u;
405
+ grown &&
406
+ position < module_record(context, module)->async_parent_count;
407
+ position += 1u) {
408
+ size_t parent =
409
+ module_record(context, module)->async_parents[position];
410
+ size_t root = module_cycle_root(context, parent);
411
+ if (module_record(context, parent)->gathered ||
412
+ module_record(context, root)->errored) {
413
+ continue;
414
+ }
415
+ OseoModuleRecord *record = module_record(context, parent);
416
+ record->pending_async_dependencies -= 1u;
417
+ if (record->pending_async_dependencies == 0u) {
418
+ grown = module_list_append(list, parent);
419
+ if (grown) record->gathered = true;
420
+ if (grown && !record->has_top_level_await) {
421
+ grown = module_list_append(&work, parent);
422
+ }
423
+ }
424
+ }
425
+ }
426
+ free(work.items);
427
+ for (size_t position = 0u; position < list->count; position += 1u) {
428
+ module_record(context, list->items[position])->gathered = false;
429
+ }
430
+ return grown;
431
+ }
432
+
433
+ /*
434
+ * AsyncModuleExecutionRejected with an explicit stack of async-parent
435
+ * cursors. Each module records the error and rejects its capability when
436
+ * first reached, then its parents follow in list order before the next
437
+ * sibling, which is the edition's depth-first order of capability
438
+ * rejections. The error is rooted by the caller and then by each record.
439
+ */
440
+ static OseoResult module_async_rejected(
441
+ OseoContext *context,
442
+ size_t index,
443
+ OseoValue error
444
+ ) {
445
+ OseoModuleList modules = {NULL, 0u, 0u};
446
+ OseoModuleList positions = {NULL, 0u, 0u};
447
+ OseoResult result = normal(oseo_undefined());
448
+ size_t next = index;
449
+ bool reached = true;
450
+ while (result.status == OSEO_STATUS_NORMAL) {
451
+ if (reached) {
452
+ OseoModuleRecord *record = module_record(context, next);
453
+ if (record->status != OSEO_MODULE_EVALUATED) {
454
+ record->errored = true;
455
+ record->evaluation_error = error;
456
+ record->status = OSEO_MODULE_EVALUATED;
457
+ record->async_evaluation_order = OSEO_MODULE_ASYNC_DONE;
458
+ result = module_settle_capability(context, next, false);
459
+ if (result.status != OSEO_STATUS_NORMAL) break;
460
+ if (!module_list_append(&modules, next) ||
461
+ !module_list_append(&positions, 0u)) {
462
+ result = module_allocation_failure(context);
463
+ break;
464
+ }
465
+ }
466
+ reached = false;
467
+ }
468
+ if (modules.count == 0u) break;
469
+ size_t top = modules.items[modules.count - 1u];
470
+ size_t *position = &positions.items[positions.count - 1u];
471
+ OseoModuleRecord *record = module_record(context, top);
472
+ if (*position == record->async_parent_count) {
473
+ modules.count -= 1u;
474
+ positions.count -= 1u;
475
+ continue;
476
+ }
477
+ next = record->async_parents[*position];
478
+ *position += 1u;
479
+ reached = true;
480
+ }
481
+ free(modules.items);
482
+ free(positions.items);
483
+ return result;
484
+ }
485
+
486
+ /* One gathered module and its [[AsyncEvaluationOrder]] for sorting. */
487
+ typedef struct {
488
+ uint64_t order;
489
+ size_t index;
490
+ } OseoModuleOrder;
491
+
492
+ static int module_order_compare(const void *left, const void *right) {
493
+ uint64_t left_order = ((const OseoModuleOrder *)left)->order;
494
+ uint64_t right_order = ((const OseoModuleOrder *)right)->order;
495
+ return left_order < right_order ? -1 : left_order > right_order;
496
+ }
497
+
498
+ /* AsyncModuleExecutionFulfilled. */
499
+ static OseoResult module_async_fulfilled(OseoContext *context, size_t index) {
500
+ OseoModuleRecord *record = module_record(context, index);
501
+ if (record->status == OSEO_MODULE_EVALUATED) {
502
+ return normal(oseo_undefined());
503
+ }
504
+ record->async_evaluation_order = OSEO_MODULE_ASYNC_DONE;
505
+ record->status = OSEO_MODULE_EVALUATED;
506
+ OseoResult result = module_settle_capability(context, index, true);
507
+ if (result.status != OSEO_STATUS_NORMAL) return result;
508
+ OseoModuleList list = {NULL, 0u, 0u};
509
+ if (!module_gather_available_ancestors(context, index, &list)) {
510
+ free(list.items);
511
+ return module_allocation_failure(context);
512
+ }
513
+ /*
514
+ * Sort by [[AsyncEvaluationOrder]]. Every listed record is still
515
+ * pending, so its order is an integer and the orders are distinct.
516
+ */
517
+ OseoModuleOrder *sorted = NULL;
518
+ if (list.count > 0u) {
519
+ sorted = malloc(list.count * sizeof(*sorted));
520
+ if (sorted == NULL) {
521
+ free(list.items);
522
+ return module_allocation_failure(context);
523
+ }
524
+ }
525
+ for (size_t position = 0u; position < list.count; position += 1u) {
526
+ size_t item = list.items[position];
527
+ sorted[position] = (OseoModuleOrder){
528
+ module_record(context, item)->async_evaluation_order,
529
+ item,
530
+ };
531
+ }
532
+ free(list.items);
533
+ if (list.count > 1u) {
534
+ qsort(sorted, list.count, sizeof(*sorted), module_order_compare);
535
+ }
536
+ for (size_t position = 0u;
537
+ result.status == OSEO_STATUS_NORMAL && position < list.count;
538
+ position += 1u) {
539
+ size_t member = sorted[position].index;
540
+ record = module_record(context, member);
541
+ if (record->status == OSEO_MODULE_EVALUATED) continue;
542
+ if (record->has_top_level_await) {
543
+ result = module_execute_async(context, member);
544
+ continue;
545
+ }
546
+ result = module_call_evaluator(context, member);
547
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
548
+ OseoValue slots[1] = {result.value};
549
+ OseoRootFrame frame = {NULL, slots, 1u};
550
+ oseo_roots_push(context, &frame);
551
+ oseo_context_clear_language_error(context);
552
+ result = module_async_rejected(context, member, frame.slots[0]);
553
+ oseo_roots_pop(context, &frame);
554
+ } else if (result.status == OSEO_STATUS_NORMAL) {
555
+ record = module_record(context, member);
556
+ record->async_evaluation_order = OSEO_MODULE_ASYNC_DONE;
557
+ record->status = OSEO_MODULE_EVALUATED;
558
+ result = module_settle_capability(context, member, true);
559
+ }
560
+ }
561
+ free(sorted);
562
+ return result.status == OSEO_STATUS_NORMAL
563
+ ? normal(oseo_undefined())
564
+ : result;
565
+ }
566
+
567
+ OseoResult oseo_module_evaluate(OseoContext *context, size_t module_index) {
568
+ OseoModuleTable *table = module_table(context);
569
+ if (table == NULL || module_index >= table->count) {
570
+ return failure(
571
+ context,
572
+ "OSEO2001",
573
+ "The module evaluation names no module table entry."
574
+ );
575
+ }
576
+ return module_evaluate(context, module_index);
577
+ }
578
+
579
+ OseoResult oseo_internal_module_async_settled(
580
+ OseoContext *context,
581
+ size_t module_index,
582
+ OseoValue argument,
583
+ bool fulfilled
584
+ ) {
585
+ OseoModuleTable *table = module_table(context);
586
+ if (table == NULL || module_index >= table->count) {
587
+ return failure(
588
+ context,
589
+ "OSEO2001",
590
+ "The module reaction names no module table entry."
591
+ );
592
+ }
593
+ if (fulfilled) return module_async_fulfilled(context, module_index);
594
+ OseoValue slots[1] = {argument};
595
+ OseoRootFrame frame = {NULL, slots, 1u};
596
+ oseo_roots_push(context, &frame);
597
+ OseoResult result =
598
+ module_async_rejected(context, module_index, frame.slots[0]);
599
+ oseo_roots_pop(context, &frame);
600
+ return result;
601
+ }
602
+
603
+ void oseo_internal_module_table_release(OseoContext *context) {
604
+ OseoModuleTable *table = module_table(context);
605
+ if (table == NULL) return;
606
+ for (size_t index = 0u; index < table->count; index += 1u) {
607
+ free(table->records[index].async_parents);
608
+ free(table->records[index].requested);
609
+ }
610
+ free(table->records);
611
+ free(table->stack);
612
+ free(table->frames);
613
+ free(table);
614
+ context->module_table = NULL;
615
+ }
616
+
617
+ /* Reads one non-negative integer table field below `limit`. */
618
+ static bool module_table_index(
619
+ const OseoValue *arguments,
620
+ size_t argument_count,
621
+ size_t *position,
622
+ size_t limit,
623
+ size_t *out
624
+ ) {
625
+ if (*position >= argument_count) return false;
626
+ OseoValue value = arguments[*position];
627
+ *position += 1u;
628
+ if (!is_number(value)) return false;
629
+ double number = number_value(value);
630
+ if (!(number >= 0.0) || number >= (double)limit ||
631
+ floor(number) != number) {
632
+ return false;
633
+ }
634
+ *out = (size_t)number;
635
+ return true;
636
+ }
637
+
638
+ static OseoResult module_table_invalid(OseoContext *context) {
639
+ oseo_internal_module_table_release(context);
640
+ return failure(context, "OSEO2001", "The module table is malformed.");
641
+ }
642
+
643
+ OseoResult oseo_module_graph_evaluate(
644
+ OseoContext *context,
645
+ size_t argument_count,
646
+ const OseoValue *arguments
647
+ ) {
648
+ if (context->module_table != NULL) {
649
+ return failure(
650
+ context,
651
+ "OSEO2001",
652
+ "The module table is already installed."
653
+ );
654
+ }
655
+ size_t position = 0u;
656
+ size_t entry = 0u;
657
+ size_t count = 0u;
658
+ if (!module_table_index(
659
+ arguments, argument_count, &position, SIZE_MAX, &entry) ||
660
+ !module_table_index(
661
+ arguments, argument_count, &position, argument_count, &count) ||
662
+ count == 0u || entry > count) {
663
+ return failure(context, "OSEO2001", "The module table is malformed.");
664
+ }
665
+ OseoModuleTable *table = calloc(1u, sizeof(*table));
666
+ if (table == NULL) return module_allocation_failure(context);
667
+ context->module_table = table;
668
+ table->records = calloc(count, sizeof(*table->records));
669
+ table->stack = calloc(count, sizeof(*table->stack));
670
+ table->frames = calloc(count, sizeof(*table->frames));
671
+ if (table->records == NULL || table->stack == NULL ||
672
+ table->frames == NULL) {
673
+ oseo_internal_module_table_release(context);
674
+ return module_allocation_failure(context);
675
+ }
676
+ table->count = count;
677
+ for (size_t index = 0u; index < count; index += 1u) {
678
+ OseoModuleRecord *record = &table->records[index];
679
+ record->evaluator = oseo_undefined();
680
+ record->namespace_object = oseo_undefined();
681
+ record->top_level_capability = oseo_undefined();
682
+ record->evaluation_error = oseo_undefined();
683
+ record->status = OSEO_MODULE_LINKED;
684
+ record->dfs_index = OSEO_MODULE_NONE;
685
+ record->dfs_ancestor_index = OSEO_MODULE_NONE;
686
+ record->cycle_root = OSEO_MODULE_NONE;
687
+ record->async_evaluation_order = OSEO_MODULE_ASYNC_UNSET;
688
+ }
689
+ for (size_t index = 0u; index < count; index += 1u) {
690
+ OseoModuleRecord *record = &table->records[index];
691
+ if (position >= argument_count ||
692
+ !is_function(arguments[position])) {
693
+ return module_table_invalid(context);
694
+ }
695
+ record->evaluator = arguments[position];
696
+ position += 1u;
697
+ size_t flag = 0u;
698
+ size_t requested_count = 0u;
699
+ if (!module_table_index(
700
+ arguments, argument_count, &position, 2u, &flag) ||
701
+ position >= argument_count ||
702
+ (tag_of(arguments[position]) != OSEO_TAG_UNDEFINED &&
703
+ !is_object(arguments[position]))) {
704
+ return module_table_invalid(context);
705
+ }
706
+ record->namespace_object = arguments[position];
707
+ position += 1u;
708
+ if (!module_table_index(
709
+ arguments,
710
+ argument_count,
711
+ &position,
712
+ argument_count,
713
+ &requested_count
714
+ )) {
715
+ return module_table_invalid(context);
716
+ }
717
+ record->has_top_level_await = flag == 1u;
718
+ if (requested_count > 0u) {
719
+ record->requested =
720
+ calloc(requested_count, sizeof(*record->requested));
721
+ if (record->requested == NULL) {
722
+ oseo_internal_module_table_release(context);
723
+ return module_allocation_failure(context);
724
+ }
725
+ }
726
+ record->requested_count = requested_count;
727
+ for (size_t request = 0u; request < requested_count; request += 1u) {
728
+ if (!module_table_index(
729
+ arguments,
730
+ argument_count,
731
+ &position,
732
+ count,
733
+ &record->requested[request]
734
+ )) {
735
+ return module_table_invalid(context);
736
+ }
737
+ }
738
+ }
739
+ if (position != argument_count) return module_table_invalid(context);
740
+ if (entry == count) return normal(oseo_undefined());
741
+ return module_evaluate(context, entry);
742
+ }
743
+
744
+ /* The module table entry an `import()` names, or a failure. */
745
+ static OseoResult module_import_target(
746
+ OseoContext *context,
747
+ size_t module_index,
748
+ OseoModuleRecord **record
749
+ ) {
750
+ OseoModuleTable *table = module_table(context);
751
+ if (table == NULL || module_index >= table->count ||
752
+ !is_object(table->records[module_index].namespace_object)) {
753
+ return failure(
754
+ context,
755
+ "OSEO2001",
756
+ "The import() call names no module table entry."
757
+ );
758
+ }
759
+ *record = &table->records[module_index];
760
+ return normal(oseo_undefined());
761
+ }
762
+
763
+ /*
764
+ * EvaluateImportCall with the build-time request. The specifier literal
765
+ * has no conversion hooks, and the host completes HostLoadImportedModule
766
+ * synchronously from the table, so ContinueDynamicImport runs at once:
767
+ * LoadRequestedModules is already fulfilled, and its reaction job is
768
+ * queued before the call returns the new promise.
769
+ */
770
+ OseoResult oseo_module_import(
771
+ OseoContext *context,
772
+ size_t argument_count,
773
+ const OseoValue *arguments
774
+ ) {
775
+ OseoModuleTable *table = module_table(context);
776
+ size_t position = 0u;
777
+ size_t index = 0u;
778
+ if (table == NULL || argument_count != 1u ||
779
+ !module_table_index(
780
+ arguments, argument_count, &position, table->count, &index)) {
781
+ return failure(
782
+ context,
783
+ "OSEO2001",
784
+ "The import() call names no module table entry."
785
+ );
786
+ }
787
+ OseoModuleRecord *record = NULL;
788
+ OseoResult result = module_import_target(context, index, &record);
789
+ if (result.status != OSEO_STATUS_NORMAL) return result;
790
+ OseoRootFrame frame = {NULL, NULL, 0u};
791
+ result = oseo_roots_allocate(context, &frame, 1u);
792
+ if (result.status != OSEO_STATUS_NORMAL) return result;
793
+ result = oseo_internal_promise_create(context);
794
+ frame.slots[0] = result.value;
795
+ if (result.status == OSEO_STATUS_NORMAL) {
796
+ result = oseo_internal_promise_then_import(
797
+ context,
798
+ oseo_undefined(),
799
+ frame.slots[0],
800
+ module_record(context, index)->evaluator,
801
+ index,
802
+ OSEO_REACTION_IMPORT_EVALUATE
803
+ );
804
+ }
805
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
806
+ oseo_roots_release(context, &frame);
807
+ return result;
808
+ }
809
+
810
+ /*
811
+ * The two reaction steps of ContinueDynamicImport. Link has no work,
812
+ * because the build linked and instantiated every module. Evaluate's
813
+ * promise then settles the call's promise: fulfillment resolves it with
814
+ * the namespace through ordinary promise resolution, so an exported
815
+ * `then` is honored, and rejection passes the evaluation error on.
816
+ */
817
+ OseoResult oseo_internal_module_import_step(
818
+ OseoContext *context,
819
+ OseoReactionKind kind,
820
+ size_t module_index,
821
+ OseoValue capability,
822
+ OseoValue argument,
823
+ bool fulfilled
824
+ ) {
825
+ OseoModuleRecord *record = NULL;
826
+ OseoResult result = module_import_target(context, module_index, &record);
827
+ if (result.status != OSEO_STATUS_NORMAL) return result;
828
+ OseoValue slots[2] = {capability, argument};
829
+ OseoRootFrame frame = {NULL, slots, 2u};
830
+ oseo_roots_push(context, &frame);
831
+ if (kind == OSEO_REACTION_IMPORT_EVALUATE) {
832
+ result = module_evaluate(context, module_index);
833
+ if (result.status == OSEO_STATUS_NORMAL) {
834
+ result = oseo_internal_promise_then_import(
835
+ context,
836
+ result.value,
837
+ frame.slots[0],
838
+ module_record(context, module_index)->evaluator,
839
+ module_index,
840
+ OSEO_REACTION_IMPORT_SETTLE
841
+ );
842
+ }
843
+ } else if (fulfilled) {
844
+ result = oseo_promise_resolve_into(
845
+ context,
846
+ frame.slots[0],
847
+ module_record(context, module_index)->namespace_object
848
+ );
849
+ } else {
850
+ result = oseo_promise_reject_into(
851
+ context,
852
+ frame.slots[0],
853
+ frame.slots[1]
854
+ );
855
+ }
856
+ oseo_roots_pop(context, &frame);
857
+ return result.status == OSEO_STATUS_NORMAL
858
+ ? normal(oseo_undefined())
859
+ : result;
860
+ }