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