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