@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,1096 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
/* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
|
|
3
|
+
|
|
4
|
+
#include "runtime_internal.h"
|
|
5
|
+
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
OseoResult oseo_internal_generator_builtin_dispatch(
|
|
10
|
+
OseoContext *context,
|
|
11
|
+
size_t code_id,
|
|
12
|
+
OseoValue callee,
|
|
13
|
+
OseoValue receiver,
|
|
14
|
+
size_t argument_count,
|
|
15
|
+
const OseoValue *arguments,
|
|
16
|
+
OseoValue new_target
|
|
17
|
+
) {
|
|
18
|
+
(void)callee;
|
|
19
|
+
(void)new_target;
|
|
20
|
+
OseoValue argument = argument_count > 0u
|
|
21
|
+
? arguments[0]
|
|
22
|
+
: oseo_undefined();
|
|
23
|
+
if (code_id == OSEO_GENERATOR_NEXT_CODE_ID) {
|
|
24
|
+
return oseo_generator_next(context, receiver, argument);
|
|
25
|
+
}
|
|
26
|
+
if (code_id == OSEO_GENERATOR_RETURN_CODE_ID) {
|
|
27
|
+
return oseo_generator_return(context, receiver, argument);
|
|
28
|
+
}
|
|
29
|
+
if (code_id == OSEO_GENERATOR_THROW_CODE_ID) {
|
|
30
|
+
return oseo_generator_throw(context, receiver, argument);
|
|
31
|
+
}
|
|
32
|
+
if (code_id == OSEO_ASYNC_ITERATOR_SELF_CODE_ID) {
|
|
33
|
+
return normal(receiver);
|
|
34
|
+
}
|
|
35
|
+
/* ADR 0016 keeps every form that compiles source text at run time
|
|
36
|
+
* outside the profile. Each constructor is a materialized intrinsic
|
|
37
|
+
* value, so the chains above it are complete; only reaching its
|
|
38
|
+
* [[Call]] or [[Construct]] reports this boundary. */
|
|
39
|
+
if (code_id == OSEO_ASYNC_GENERATOR_FUNCTION_CODE_ID) {
|
|
40
|
+
return failure(
|
|
41
|
+
context,
|
|
42
|
+
"OSEO1001",
|
|
43
|
+
"AsyncGeneratorFunction compiles source text at run time, "
|
|
44
|
+
"which is outside the admitted profile."
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
if (code_id == OSEO_GENERATOR_FUNCTION_CODE_ID) {
|
|
48
|
+
return failure(
|
|
49
|
+
context,
|
|
50
|
+
"OSEO1001",
|
|
51
|
+
"GeneratorFunction compiles source text at run time, "
|
|
52
|
+
"which is outside the admitted profile."
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
if (code_id == OSEO_ASYNC_FUNCTION_CODE_ID) {
|
|
56
|
+
return failure(
|
|
57
|
+
context,
|
|
58
|
+
"OSEO1001",
|
|
59
|
+
"AsyncFunction compiles source text at run time, "
|
|
60
|
+
"which is outside the admitted profile."
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return oseo_unknown_function(context, code_id);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
* Generator objects: the suspended body frame shared by both generator
|
|
68
|
+
* kinds, its collector-traced root slots and saved completion records,
|
|
69
|
+
* and the %GeneratorPrototype% resumptions that drive a synchronous one.
|
|
70
|
+
* The asynchronous driver lives in *runtime_async_generator.c* and
|
|
71
|
+
* reuses the same record.
|
|
72
|
+
*
|
|
73
|
+
* A generator's roots live in the generator record rather than on the
|
|
74
|
+
* native root stack, so no C frame of a suspended body stays alive. The
|
|
75
|
+
* record is allocated once, is never resized, and the collector never
|
|
76
|
+
* moves it, so generated code can hold `roots` across every safepoint
|
|
77
|
+
* in the resumed body.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
static OseoGenerator *generator_state(OseoValue value) {
|
|
81
|
+
return ordinary_object(value)->generator;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static OseoResult ascii_generator_string(
|
|
85
|
+
OseoContext *context,
|
|
86
|
+
const char *text
|
|
87
|
+
) {
|
|
88
|
+
size_t length = strlen(text);
|
|
89
|
+
uint16_t *units = NULL;
|
|
90
|
+
if (length > 0u) {
|
|
91
|
+
units = malloc(length * sizeof(uint16_t));
|
|
92
|
+
if (units == NULL) {
|
|
93
|
+
return failure(context, "OSEO2001", "String allocation failed.");
|
|
94
|
+
}
|
|
95
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
96
|
+
units[index] = (uint16_t)(unsigned char)text[index];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
OseoResult result = oseo_internal_allocate_string(context, units, length);
|
|
100
|
+
free(units);
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* Define one ASCII-named data property; object and value stay rooted. */
|
|
105
|
+
static OseoResult define_ascii_property(
|
|
106
|
+
OseoContext *context,
|
|
107
|
+
OseoValue object,
|
|
108
|
+
const char *name,
|
|
109
|
+
OseoValue value,
|
|
110
|
+
OseoPropertyAttributes attributes
|
|
111
|
+
) {
|
|
112
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
113
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
114
|
+
oseo_roots_push(context, &frame);
|
|
115
|
+
OseoResult result = ascii_generator_string(context, name);
|
|
116
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
117
|
+
slots[2] = result.value;
|
|
118
|
+
result = oseo_object_define(
|
|
119
|
+
context,
|
|
120
|
+
slots[0],
|
|
121
|
+
slots[2],
|
|
122
|
+
slots[1],
|
|
123
|
+
attributes
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
oseo_roots_pop(context, &frame);
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* The same for a well-known symbol key; object and value stay rooted. */
|
|
131
|
+
static OseoResult define_symbol_property(
|
|
132
|
+
OseoContext *context,
|
|
133
|
+
OseoValue object,
|
|
134
|
+
size_t well_known,
|
|
135
|
+
OseoValue value,
|
|
136
|
+
OseoPropertyAttributes attributes
|
|
137
|
+
) {
|
|
138
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
139
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
140
|
+
oseo_roots_push(context, &frame);
|
|
141
|
+
OseoResult result =
|
|
142
|
+
oseo_internal_well_known_symbol(context, well_known);
|
|
143
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
144
|
+
slots[2] = result.value;
|
|
145
|
+
result = oseo_object_define(
|
|
146
|
+
context,
|
|
147
|
+
slots[0],
|
|
148
|
+
slots[2],
|
|
149
|
+
slots[1],
|
|
150
|
+
attributes
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
oseo_roots_pop(context, &frame);
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* The same for `Symbol.toStringTag` and an ASCII tag value. */
|
|
158
|
+
static OseoResult define_to_string_tag(
|
|
159
|
+
OseoContext *context,
|
|
160
|
+
OseoValue object,
|
|
161
|
+
const char *tag,
|
|
162
|
+
OseoPropertyAttributes attributes
|
|
163
|
+
) {
|
|
164
|
+
OseoValue slots[1] = {object};
|
|
165
|
+
OseoRootFrame frame = {NULL, slots, 1u};
|
|
166
|
+
oseo_roots_push(context, &frame);
|
|
167
|
+
OseoResult result = ascii_generator_string(context, tag);
|
|
168
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
169
|
+
result = define_symbol_property(
|
|
170
|
+
context,
|
|
171
|
+
slots[0],
|
|
172
|
+
OSEO_WELL_KNOWN_TO_STRING_TAG,
|
|
173
|
+
result.value,
|
|
174
|
+
attributes
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
oseo_roots_pop(context, &frame);
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/*
|
|
182
|
+
* Install one built-in method. Every method is cached on the context, so
|
|
183
|
+
* the value this defines is permanently rooted and two generators created
|
|
184
|
+
* from different functions share one method identity.
|
|
185
|
+
*/
|
|
186
|
+
static OseoResult define_method(
|
|
187
|
+
OseoContext *context,
|
|
188
|
+
OseoValue object,
|
|
189
|
+
const char *name,
|
|
190
|
+
size_t code_id
|
|
191
|
+
) {
|
|
192
|
+
/* A built-in method is writable, non-enumerable, and configurable. */
|
|
193
|
+
const OseoPropertyAttributes method = {true, false, true, false};
|
|
194
|
+
OseoValue slots[1] = {object};
|
|
195
|
+
OseoRootFrame frame = {NULL, slots, 1u};
|
|
196
|
+
oseo_roots_push(context, &frame);
|
|
197
|
+
OseoResult result =
|
|
198
|
+
oseo_internal_async_generator_method(context, code_id);
|
|
199
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
200
|
+
result = define_ascii_property(
|
|
201
|
+
context,
|
|
202
|
+
slots[0],
|
|
203
|
+
name,
|
|
204
|
+
result.value,
|
|
205
|
+
method
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
oseo_roots_pop(context, &frame);
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/*
|
|
213
|
+
* The constructor half of one dynamic-source function intrinsic:
|
|
214
|
+
* %GeneratorFunction%, %AsyncGeneratorFunction%, or %AsyncFunction%.
|
|
215
|
+
* Each is an ordinary built-in constructor whose [[Prototype]] is
|
|
216
|
+
* %Function%, so the chain a reflected function exposes reaches the
|
|
217
|
+
* same `call`, `bind`, and `apply` the specification places above it.
|
|
218
|
+
* Only its [[Call]] and [[Construct]] stay outside the profile.
|
|
219
|
+
*/
|
|
220
|
+
static OseoResult dynamic_source_constructor(
|
|
221
|
+
OseoContext *context,
|
|
222
|
+
size_t code_id,
|
|
223
|
+
const uint16_t *name_units,
|
|
224
|
+
size_t name_length,
|
|
225
|
+
OseoValue prototype_property
|
|
226
|
+
) {
|
|
227
|
+
OseoValue slots[3] = {prototype_property, oseo_undefined(),
|
|
228
|
+
oseo_undefined()};
|
|
229
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
230
|
+
oseo_roots_push(context, &frame);
|
|
231
|
+
OseoResult result = oseo_internal_intrinsic(
|
|
232
|
+
context,
|
|
233
|
+
OSEO_INTRINSIC_FUNCTION
|
|
234
|
+
);
|
|
235
|
+
slots[1] = result.value;
|
|
236
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
237
|
+
result = oseo_environment_create(context, 0u);
|
|
238
|
+
slots[2] = result.value;
|
|
239
|
+
}
|
|
240
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
241
|
+
result = oseo_function_create(
|
|
242
|
+
context,
|
|
243
|
+
code_id,
|
|
244
|
+
slots[2],
|
|
245
|
+
name_units,
|
|
246
|
+
name_length,
|
|
247
|
+
1u,
|
|
248
|
+
OSEO_FUNCTION_ORDINARY,
|
|
249
|
+
oseo_undefined(),
|
|
250
|
+
oseo_undefined(),
|
|
251
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
252
|
+
);
|
|
253
|
+
slots[2] = result.value;
|
|
254
|
+
}
|
|
255
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
256
|
+
OseoFunction *constructor = function_object(slots[2]);
|
|
257
|
+
/* The constructor was allocated one statement ago and no read has
|
|
258
|
+
* reached it, so relinking its [[Prototype]] cannot invalidate a
|
|
259
|
+
* shape assumption and keeps it out of dictionary mode, the way a
|
|
260
|
+
* class definition relinks the two objects it allocates. */
|
|
261
|
+
constructor->ordinary.prototype = slots[1];
|
|
262
|
+
constructor->ordinary.shape_id = context->next_shape_id;
|
|
263
|
+
context->next_shape_id += 1u;
|
|
264
|
+
constructor->prototype_object = slots[0];
|
|
265
|
+
constructor->prototype_writable = false;
|
|
266
|
+
result = normal(slots[2]);
|
|
267
|
+
}
|
|
268
|
+
oseo_roots_pop(context, &frame);
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/*
|
|
273
|
+
* The synchronous generator intrinsics, created as one cluster for the
|
|
274
|
+
* reason the asynchronous cluster below is: %GeneratorFunction% names
|
|
275
|
+
* %GeneratorFunction.prototype% as its `prototype`, that object names
|
|
276
|
+
* the constructor back and %GeneratorPrototype% as its own `prototype`,
|
|
277
|
+
* and %GeneratorPrototype% names it as `constructor`. Splitting them
|
|
278
|
+
* would let a program observe a %GeneratorPrototype% whose `constructor`
|
|
279
|
+
* and `Symbol.toStringTag` are still missing.
|
|
280
|
+
*
|
|
281
|
+
* The cluster reaches the context only after every property is defined,
|
|
282
|
+
* so a failed allocation leaves no partially wired intrinsic behind, and
|
|
283
|
+
* its allocations are restored because an intrinsic created on first use
|
|
284
|
+
* is not one the observed program performed.
|
|
285
|
+
*
|
|
286
|
+
* OrdinaryOwnPropertyKeys reports non-index string keys in the order
|
|
287
|
+
* they were created, and every key here is one, so creation order is
|
|
288
|
+
* observable as soon as a program makes a configurable link enumerable.
|
|
289
|
+
* Each object therefore defines its properties in the order the reference
|
|
290
|
+
* hosts expose them:
|
|
291
|
+
* %GeneratorFunction.prototype% takes `prototype` before `constructor`,
|
|
292
|
+
* and %GeneratorPrototype% takes `constructor` before `next`, `return`,
|
|
293
|
+
* and `throw`. The allocation order stays free, because only a
|
|
294
|
+
* definition creates a key.
|
|
295
|
+
*/
|
|
296
|
+
OseoResult oseo_internal_generator_prototype(OseoContext *context) {
|
|
297
|
+
OseoValue cached =
|
|
298
|
+
context->realm->intrinsics[OSEO_INTRINSIC_GENERATOR_PROTOTYPE];
|
|
299
|
+
if (tag_of(cached) != OSEO_TAG_UNDEFINED) return normal(cached);
|
|
300
|
+
size_t entry_allocations = context->allocations;
|
|
301
|
+
/* A built-in `constructor`, `prototype`, or `Symbol.toStringTag` is
|
|
302
|
+
* non-writable, non-enumerable, and configurable. */
|
|
303
|
+
const OseoPropertyAttributes hidden = {true, false, false, false};
|
|
304
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
305
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 4u);
|
|
306
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
307
|
+
result = oseo_internal_array_iterator_prototype(context);
|
|
308
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
309
|
+
result = oseo_object_create(
|
|
310
|
+
context,
|
|
311
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ITERATOR_PROTOTYPE]
|
|
312
|
+
);
|
|
313
|
+
frame.slots[0] = result.value;
|
|
314
|
+
}
|
|
315
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
316
|
+
result = oseo_internal_intrinsic(
|
|
317
|
+
context,
|
|
318
|
+
OSEO_INTRINSIC_FUNCTION_PROTOTYPE
|
|
319
|
+
);
|
|
320
|
+
frame.slots[1] = result.value;
|
|
321
|
+
}
|
|
322
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
323
|
+
result = oseo_object_create(context, frame.slots[1]);
|
|
324
|
+
frame.slots[2] = result.value;
|
|
325
|
+
}
|
|
326
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
327
|
+
static const uint16_t constructor_units[] = {
|
|
328
|
+
'G', 'e', 'n', 'e', 'r', 'a', 't', 'o', 'r',
|
|
329
|
+
'F', 'u', 'n', 'c', 't', 'i', 'o', 'n'
|
|
330
|
+
};
|
|
331
|
+
result = dynamic_source_constructor(
|
|
332
|
+
context,
|
|
333
|
+
OSEO_GENERATOR_FUNCTION_CODE_ID,
|
|
334
|
+
constructor_units,
|
|
335
|
+
sizeof(constructor_units) / sizeof(*constructor_units),
|
|
336
|
+
frame.slots[2]
|
|
337
|
+
);
|
|
338
|
+
frame.slots[3] = result.value;
|
|
339
|
+
}
|
|
340
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
341
|
+
result = define_ascii_property(
|
|
342
|
+
context,
|
|
343
|
+
frame.slots[2],
|
|
344
|
+
"prototype",
|
|
345
|
+
frame.slots[0],
|
|
346
|
+
hidden
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
350
|
+
result = define_ascii_property(
|
|
351
|
+
context,
|
|
352
|
+
frame.slots[2],
|
|
353
|
+
"constructor",
|
|
354
|
+
frame.slots[3],
|
|
355
|
+
hidden
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
359
|
+
result = define_to_string_tag(
|
|
360
|
+
context,
|
|
361
|
+
frame.slots[2],
|
|
362
|
+
"GeneratorFunction",
|
|
363
|
+
hidden
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
367
|
+
result = define_ascii_property(
|
|
368
|
+
context,
|
|
369
|
+
frame.slots[0],
|
|
370
|
+
"constructor",
|
|
371
|
+
frame.slots[2],
|
|
372
|
+
hidden
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
static const char *const names[] = {"next", "return", "throw"};
|
|
376
|
+
static const size_t codes[] = {
|
|
377
|
+
OSEO_GENERATOR_NEXT_CODE_ID,
|
|
378
|
+
OSEO_GENERATOR_RETURN_CODE_ID,
|
|
379
|
+
OSEO_GENERATOR_THROW_CODE_ID,
|
|
380
|
+
};
|
|
381
|
+
const OseoPropertyAttributes method = {true, false, true, false};
|
|
382
|
+
for (size_t index = 0u;
|
|
383
|
+
result.status == OSEO_STATUS_NORMAL && index < 3u;
|
|
384
|
+
index += 1u) {
|
|
385
|
+
result = oseo_internal_iterator_method(context, codes[index]);
|
|
386
|
+
frame.slots[1] = result.value;
|
|
387
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
388
|
+
result = define_ascii_property(
|
|
389
|
+
context,
|
|
390
|
+
frame.slots[0],
|
|
391
|
+
names[index],
|
|
392
|
+
frame.slots[1],
|
|
393
|
+
method
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
397
|
+
result = define_to_string_tag(
|
|
398
|
+
context,
|
|
399
|
+
frame.slots[0],
|
|
400
|
+
"Generator",
|
|
401
|
+
hidden
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
405
|
+
context->realm->intrinsics[OSEO_INTRINSIC_GENERATOR_PROTOTYPE] =
|
|
406
|
+
frame.slots[0];
|
|
407
|
+
context->realm->intrinsics[
|
|
408
|
+
OSEO_INTRINSIC_GENERATOR_FUNCTION_PROTOTYPE
|
|
409
|
+
] = frame.slots[2];
|
|
410
|
+
context->realm->intrinsics[OSEO_INTRINSIC_GENERATOR_FUNCTION] =
|
|
411
|
+
frame.slots[3];
|
|
412
|
+
result.value = frame.slots[0];
|
|
413
|
+
if (context->observe_specialization) {
|
|
414
|
+
context->allocations = entry_allocations;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
oseo_roots_release(context, &frame);
|
|
418
|
+
return result;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
OseoResult oseo_internal_generator_function_intrinsic(OseoContext *context) {
|
|
422
|
+
OseoResult result = oseo_internal_generator_prototype(context);
|
|
423
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
424
|
+
return normal(context->realm->intrinsics[
|
|
425
|
+
OSEO_INTRINSIC_GENERATOR_FUNCTION_PROTOTYPE
|
|
426
|
+
]);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/*
|
|
430
|
+
* The asynchronous function intrinsics. %AsyncFunction.prototype% is an
|
|
431
|
+
* ordinary object rather than a callable one, and, unlike the two
|
|
432
|
+
* generator function prototypes, it carries no `prototype` property:
|
|
433
|
+
* an asynchronous function is not a constructor and creates no
|
|
434
|
+
* instances. `constructor` is therefore its only string key, and the
|
|
435
|
+
* creation order the two generator clusters have to arrange is already
|
|
436
|
+
* the one this object exposes.
|
|
437
|
+
*/
|
|
438
|
+
OseoResult oseo_internal_async_function_intrinsic(OseoContext *context) {
|
|
439
|
+
OseoValue cached =
|
|
440
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_FUNCTION_PROTOTYPE];
|
|
441
|
+
if (tag_of(cached) != OSEO_TAG_UNDEFINED) return normal(cached);
|
|
442
|
+
size_t entry_allocations = context->allocations;
|
|
443
|
+
const OseoPropertyAttributes hidden = {true, false, false, false};
|
|
444
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
445
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
446
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
447
|
+
result = oseo_internal_intrinsic(
|
|
448
|
+
context,
|
|
449
|
+
OSEO_INTRINSIC_FUNCTION_PROTOTYPE
|
|
450
|
+
);
|
|
451
|
+
frame.slots[0] = result.value;
|
|
452
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
453
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
454
|
+
frame.slots[1] = result.value;
|
|
455
|
+
}
|
|
456
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
457
|
+
static const uint16_t constructor_units[] = {
|
|
458
|
+
'A', 's', 'y', 'n', 'c', 'F', 'u', 'n', 'c', 't', 'i', 'o', 'n'
|
|
459
|
+
};
|
|
460
|
+
result = dynamic_source_constructor(
|
|
461
|
+
context,
|
|
462
|
+
OSEO_ASYNC_FUNCTION_CODE_ID,
|
|
463
|
+
constructor_units,
|
|
464
|
+
sizeof(constructor_units) / sizeof(*constructor_units),
|
|
465
|
+
frame.slots[1]
|
|
466
|
+
);
|
|
467
|
+
frame.slots[2] = result.value;
|
|
468
|
+
}
|
|
469
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
470
|
+
result = define_ascii_property(
|
|
471
|
+
context,
|
|
472
|
+
frame.slots[1],
|
|
473
|
+
"constructor",
|
|
474
|
+
frame.slots[2],
|
|
475
|
+
hidden
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
479
|
+
result = define_to_string_tag(
|
|
480
|
+
context,
|
|
481
|
+
frame.slots[1],
|
|
482
|
+
"AsyncFunction",
|
|
483
|
+
hidden
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
487
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_FUNCTION_PROTOTYPE] =
|
|
488
|
+
frame.slots[1];
|
|
489
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_FUNCTION] =
|
|
490
|
+
frame.slots[2];
|
|
491
|
+
result.value = frame.slots[1];
|
|
492
|
+
if (context->observe_specialization) {
|
|
493
|
+
context->allocations = entry_allocations;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
oseo_roots_release(context, &frame);
|
|
497
|
+
return result;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/*
|
|
501
|
+
* The asynchronous generator intrinsics, created as one cluster because
|
|
502
|
+
* their links are circular: %AsyncGeneratorFunction% names
|
|
503
|
+
* %AsyncGeneratorFunction.prototype% as its `prototype`, that object
|
|
504
|
+
* names the constructor back and %AsyncGeneratorPrototype% as its own
|
|
505
|
+
* `prototype`, and %AsyncGeneratorPrototype% names it as `constructor`.
|
|
506
|
+
* Every method is an ordinary own property of the object the
|
|
507
|
+
* specification places it on rather than a brand the property read
|
|
508
|
+
* synthesizes, so a descriptor read, a deletion, and an assignment all
|
|
509
|
+
* observe the same set a read does, and a generator function whose
|
|
510
|
+
* `prototype` is replaced reaches exactly the methods the replacement's
|
|
511
|
+
* chain still retains.
|
|
512
|
+
*
|
|
513
|
+
* The cluster reaches the context only after every property is defined,
|
|
514
|
+
* so a failed allocation leaves no partially wired intrinsic behind and
|
|
515
|
+
* the next reference rebuilds it. Its allocations are restored the way
|
|
516
|
+
* the error intrinsics restore theirs, because an intrinsic created on
|
|
517
|
+
* first use is not one the observed program performed.
|
|
518
|
+
*
|
|
519
|
+
* Every rootward [[Prototype]] link uses the realm-owned object or
|
|
520
|
+
* function prototype rather than a parallel null-terminated chain.
|
|
521
|
+
*
|
|
522
|
+
* Property creation order follows the synchronous cluster's, for the
|
|
523
|
+
* same observability reason: %AsyncGeneratorFunction.prototype% takes
|
|
524
|
+
* `prototype` before `constructor`, and %AsyncGeneratorPrototype% takes
|
|
525
|
+
* `constructor` before `next`, `return`, and `throw`.
|
|
526
|
+
*/
|
|
527
|
+
static OseoResult async_generator_intrinsics(OseoContext *context) {
|
|
528
|
+
OseoValue cached =
|
|
529
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_GENERATOR_PROTOTYPE];
|
|
530
|
+
if (tag_of(cached) != OSEO_TAG_UNDEFINED) {
|
|
531
|
+
return normal(cached);
|
|
532
|
+
}
|
|
533
|
+
size_t entry_allocations = context->allocations;
|
|
534
|
+
const OseoPropertyAttributes hidden = {true, false, false, false};
|
|
535
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
536
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 6u);
|
|
537
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
538
|
+
result = oseo_internal_intrinsic(
|
|
539
|
+
context,
|
|
540
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
541
|
+
);
|
|
542
|
+
frame.slots[4] = result.value;
|
|
543
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
544
|
+
result = oseo_internal_intrinsic(
|
|
545
|
+
context,
|
|
546
|
+
OSEO_INTRINSIC_FUNCTION_PROTOTYPE
|
|
547
|
+
);
|
|
548
|
+
frame.slots[5] = result.value;
|
|
549
|
+
}
|
|
550
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
551
|
+
result = oseo_object_create(context, frame.slots[4]);
|
|
552
|
+
frame.slots[0] = result.value;
|
|
553
|
+
}
|
|
554
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
555
|
+
result = oseo_internal_async_generator_method(
|
|
556
|
+
context,
|
|
557
|
+
OSEO_ASYNC_ITERATOR_SELF_CODE_ID
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
561
|
+
const OseoPropertyAttributes method = {true, false, true, false};
|
|
562
|
+
result = define_symbol_property(
|
|
563
|
+
context,
|
|
564
|
+
frame.slots[0],
|
|
565
|
+
OSEO_WELL_KNOWN_ASYNC_ITERATOR,
|
|
566
|
+
result.value,
|
|
567
|
+
method
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
571
|
+
result = oseo_internal_iterator_dispose_function(
|
|
572
|
+
context,
|
|
573
|
+
OSEO_ASYNC_ITERATOR_ASYNC_DISPOSE_CODE_ID
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
577
|
+
const OseoPropertyAttributes method = {true, false, true, false};
|
|
578
|
+
result = define_symbol_property(
|
|
579
|
+
context,
|
|
580
|
+
frame.slots[0],
|
|
581
|
+
OSEO_WELL_KNOWN_ASYNC_DISPOSE,
|
|
582
|
+
result.value,
|
|
583
|
+
method
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
587
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
588
|
+
frame.slots[1] = result.value;
|
|
589
|
+
}
|
|
590
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
591
|
+
result = oseo_object_create(context, frame.slots[5]);
|
|
592
|
+
frame.slots[2] = result.value;
|
|
593
|
+
}
|
|
594
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
595
|
+
static const uint16_t constructor_units[] = {
|
|
596
|
+
'A', 's', 'y', 'n', 'c', 'G', 'e', 'n', 'e', 'r', 'a', 't',
|
|
597
|
+
'o', 'r', 'F', 'u', 'n', 'c', 't', 'i', 'o', 'n'
|
|
598
|
+
};
|
|
599
|
+
result = dynamic_source_constructor(
|
|
600
|
+
context,
|
|
601
|
+
OSEO_ASYNC_GENERATOR_FUNCTION_CODE_ID,
|
|
602
|
+
constructor_units,
|
|
603
|
+
sizeof(constructor_units) / sizeof(*constructor_units),
|
|
604
|
+
frame.slots[2]
|
|
605
|
+
);
|
|
606
|
+
frame.slots[3] = result.value;
|
|
607
|
+
}
|
|
608
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
609
|
+
result = define_ascii_property(
|
|
610
|
+
context,
|
|
611
|
+
frame.slots[2],
|
|
612
|
+
"prototype",
|
|
613
|
+
frame.slots[1],
|
|
614
|
+
hidden
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
618
|
+
result = define_ascii_property(
|
|
619
|
+
context,
|
|
620
|
+
frame.slots[2],
|
|
621
|
+
"constructor",
|
|
622
|
+
frame.slots[3],
|
|
623
|
+
hidden
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
627
|
+
result = define_to_string_tag(
|
|
628
|
+
context,
|
|
629
|
+
frame.slots[2],
|
|
630
|
+
"AsyncGeneratorFunction",
|
|
631
|
+
hidden
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
635
|
+
result = define_ascii_property(
|
|
636
|
+
context,
|
|
637
|
+
frame.slots[1],
|
|
638
|
+
"constructor",
|
|
639
|
+
frame.slots[2],
|
|
640
|
+
hidden
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
644
|
+
result = define_method(
|
|
645
|
+
context,
|
|
646
|
+
frame.slots[1],
|
|
647
|
+
"next",
|
|
648
|
+
OSEO_ASYNC_GENERATOR_NEXT_CODE_ID
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
652
|
+
result = define_method(
|
|
653
|
+
context,
|
|
654
|
+
frame.slots[1],
|
|
655
|
+
"return",
|
|
656
|
+
OSEO_ASYNC_GENERATOR_RETURN_CODE_ID
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
660
|
+
result = define_method(
|
|
661
|
+
context,
|
|
662
|
+
frame.slots[1],
|
|
663
|
+
"throw",
|
|
664
|
+
OSEO_ASYNC_GENERATOR_THROW_CODE_ID
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
668
|
+
result = define_to_string_tag(
|
|
669
|
+
context,
|
|
670
|
+
frame.slots[1],
|
|
671
|
+
"AsyncGenerator",
|
|
672
|
+
hidden
|
|
673
|
+
);
|
|
674
|
+
}
|
|
675
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
676
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_ITERATOR_PROTOTYPE] =
|
|
677
|
+
frame.slots[0];
|
|
678
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_GENERATOR_PROTOTYPE] =
|
|
679
|
+
frame.slots[1];
|
|
680
|
+
context->realm->intrinsics[
|
|
681
|
+
OSEO_INTRINSIC_ASYNC_GENERATOR_FUNCTION_PROTOTYPE
|
|
682
|
+
] = frame.slots[2];
|
|
683
|
+
context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_GENERATOR_FUNCTION] =
|
|
684
|
+
frame.slots[3];
|
|
685
|
+
result.value = frame.slots[1];
|
|
686
|
+
if (context->observe_specialization) {
|
|
687
|
+
context->allocations = entry_allocations;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
oseo_roots_release(context, &frame);
|
|
691
|
+
return result;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
OseoResult oseo_internal_async_generator_prototype(OseoContext *context) {
|
|
695
|
+
OseoResult result = async_generator_intrinsics(context);
|
|
696
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
697
|
+
return normal(context->realm->intrinsics[
|
|
698
|
+
OSEO_INTRINSIC_ASYNC_GENERATOR_PROTOTYPE
|
|
699
|
+
]);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
OseoResult oseo_internal_async_generator_intrinsic(OseoContext *context) {
|
|
703
|
+
OseoResult result = async_generator_intrinsics(context);
|
|
704
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
705
|
+
return normal(context->realm->intrinsics[
|
|
706
|
+
OSEO_INTRINSIC_ASYNC_GENERATOR_FUNCTION_PROTOTYPE
|
|
707
|
+
]);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
OseoResult oseo_internal_generator_created(
|
|
711
|
+
OseoContext *context,
|
|
712
|
+
OseoValue callee,
|
|
713
|
+
OseoValue generator
|
|
714
|
+
) {
|
|
715
|
+
/* Only the record this call produced is relinked, so a prologue that
|
|
716
|
+
* returned something else leaves the result untouched. */
|
|
717
|
+
if (!is_generator(generator)) return normal(generator);
|
|
718
|
+
OseoGenerator *state = ordinary_object(generator)->generator;
|
|
719
|
+
if (state->callee != callee) return normal(generator);
|
|
720
|
+
OseoValue slots[2] = {callee, generator};
|
|
721
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
722
|
+
oseo_roots_push(context, &frame);
|
|
723
|
+
OseoResult result = normal(function_object(slots[0])->prototype_object);
|
|
724
|
+
/* GetPrototypeFromConstructor falls back to the intrinsic whenever
|
|
725
|
+
* the function's `prototype` is not an object, exactly as the
|
|
726
|
+
* creation path does. */
|
|
727
|
+
if (!is_object(result.value)) {
|
|
728
|
+
result = function_object(slots[0])->function_kind ==
|
|
729
|
+
OSEO_FUNCTION_ASYNC_GENERATOR
|
|
730
|
+
? oseo_internal_async_generator_prototype(context)
|
|
731
|
+
: oseo_internal_generator_prototype(context);
|
|
732
|
+
}
|
|
733
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
734
|
+
OseoOrdinaryObject *object = ordinary_object(slots[1]);
|
|
735
|
+
if (object->prototype != result.value) {
|
|
736
|
+
/* The object has not reached the program yet, so no shape
|
|
737
|
+
* assumption can depend on the chain being replaced. */
|
|
738
|
+
object->prototype = result.value;
|
|
739
|
+
object->shape_id = context->next_shape_id;
|
|
740
|
+
context->next_shape_id += 1u;
|
|
741
|
+
}
|
|
742
|
+
result = normal(slots[1]);
|
|
743
|
+
}
|
|
744
|
+
oseo_roots_pop(context, &frame);
|
|
745
|
+
return result;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
OseoResult oseo_generator_create(
|
|
749
|
+
OseoContext *context,
|
|
750
|
+
OseoValue callee,
|
|
751
|
+
OseoValue receiver,
|
|
752
|
+
size_t slot_count,
|
|
753
|
+
size_t completion_count
|
|
754
|
+
) {
|
|
755
|
+
if (!is_function(callee)) {
|
|
756
|
+
return oseo_internal_throw_error(
|
|
757
|
+
context,
|
|
758
|
+
OSEO_ERROR_TYPE,
|
|
759
|
+
"A suspension frame requires an asynchronous or generator "
|
|
760
|
+
"function."
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
OseoFunctionKind function_kind = function_object(callee)->function_kind;
|
|
764
|
+
if (function_kind != OSEO_FUNCTION_GENERATOR &&
|
|
765
|
+
function_kind != OSEO_FUNCTION_ASYNC_GENERATOR &&
|
|
766
|
+
function_kind != OSEO_FUNCTION_ASYNC &&
|
|
767
|
+
function_kind != OSEO_FUNCTION_ASYNC_ARROW) {
|
|
768
|
+
return oseo_internal_throw_error(
|
|
769
|
+
context,
|
|
770
|
+
OSEO_ERROR_TYPE,
|
|
771
|
+
"A suspension frame requires an asynchronous or generator "
|
|
772
|
+
"function."
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
bool async_function = function_kind == OSEO_FUNCTION_ASYNC ||
|
|
776
|
+
function_kind == OSEO_FUNCTION_ASYNC_ARROW;
|
|
777
|
+
bool asynchronous =
|
|
778
|
+
function_kind == OSEO_FUNCTION_ASYNC_GENERATOR || async_function;
|
|
779
|
+
if (slot_count > (SIZE_MAX - sizeof(OseoGenerator)) / sizeof(OseoValue)) {
|
|
780
|
+
return failure(context, "OSEO2001", "Generator frame is too large.");
|
|
781
|
+
}
|
|
782
|
+
size_t slot_bytes = slot_count * sizeof(OseoValue);
|
|
783
|
+
size_t remaining = SIZE_MAX - sizeof(OseoGenerator) - slot_bytes;
|
|
784
|
+
if (completion_count > remaining / sizeof(OseoCompletionRecord)) {
|
|
785
|
+
return failure(context, "OSEO2001", "Generator frame is too large.");
|
|
786
|
+
}
|
|
787
|
+
size_t completion_bytes = completion_count * sizeof(OseoCompletionRecord);
|
|
788
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
789
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
790
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
791
|
+
frame.slots[0] = callee;
|
|
792
|
+
frame.slots[1] = receiver;
|
|
793
|
+
frame.slots[2] = async_function
|
|
794
|
+
? oseo_null()
|
|
795
|
+
: function_object(callee)->prototype_object;
|
|
796
|
+
/* GetPrototypeFromConstructor falls back to the intrinsic whenever
|
|
797
|
+
* the function's `prototype` is not an object. */
|
|
798
|
+
if (!async_function && !is_object(frame.slots[2])) {
|
|
799
|
+
result = asynchronous
|
|
800
|
+
? oseo_internal_async_generator_prototype(context)
|
|
801
|
+
: oseo_internal_generator_prototype(context);
|
|
802
|
+
frame.slots[2] = result.value;
|
|
803
|
+
}
|
|
804
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
805
|
+
result = oseo_object_create(context, frame.slots[2]);
|
|
806
|
+
}
|
|
807
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
808
|
+
oseo_roots_release(context, &frame);
|
|
809
|
+
return result;
|
|
810
|
+
}
|
|
811
|
+
frame.slots[2] = result.value;
|
|
812
|
+
OseoValue generator = frame.slots[2];
|
|
813
|
+
/* The record is published only after it is fully initialized, so a
|
|
814
|
+
* collection triggered by this allocation never traces half a
|
|
815
|
+
* frame. */
|
|
816
|
+
OseoGenerator *state = malloc(
|
|
817
|
+
sizeof(OseoGenerator) + slot_bytes + completion_bytes
|
|
818
|
+
);
|
|
819
|
+
if (state == NULL) {
|
|
820
|
+
oseo_roots_release(context, &frame);
|
|
821
|
+
return failure(context, "OSEO2001", "Generator allocation failed.");
|
|
822
|
+
}
|
|
823
|
+
state->callee = frame.slots[0];
|
|
824
|
+
state->receiver = frame.slots[1];
|
|
825
|
+
state->sent = oseo_undefined();
|
|
826
|
+
state->async_function_capability = oseo_undefined();
|
|
827
|
+
state->request_head = oseo_undefined();
|
|
828
|
+
state->request_tail = oseo_undefined();
|
|
829
|
+
state->slots = (OseoValue *)(void *)(state + 1);
|
|
830
|
+
state->completions = (OseoCompletionRecord *)(void *)
|
|
831
|
+
((unsigned char *)state->slots + slot_bytes);
|
|
832
|
+
state->slot_count = slot_count;
|
|
833
|
+
state->completion_count = completion_count;
|
|
834
|
+
state->resume_point = 0u;
|
|
835
|
+
state->resume_kind = OSEO_GENERATOR_RESUME_NEXT;
|
|
836
|
+
state->suspend_reason = OSEO_GENERATOR_SUSPEND_YIELD;
|
|
837
|
+
state->state = OSEO_GENERATOR_SUSPENDED_START;
|
|
838
|
+
state->asynchronous = asynchronous;
|
|
839
|
+
state->async_function = async_function;
|
|
840
|
+
state->awaiting_return = false;
|
|
841
|
+
state->yielded_result_object = false;
|
|
842
|
+
for (size_t index = 0u; index < slot_count; index += 1u) {
|
|
843
|
+
state->slots[index] = oseo_undefined();
|
|
844
|
+
}
|
|
845
|
+
if (completion_bytes > 0u) memset(state->completions, 0, completion_bytes);
|
|
846
|
+
OseoOrdinaryObject *object = ordinary_object(generator);
|
|
847
|
+
object->generator = state;
|
|
848
|
+
oseo_roots_release(context, &frame);
|
|
849
|
+
return normal(generator);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
OseoValue *oseo_generator_slots(OseoValue generator) {
|
|
853
|
+
return generator_state(generator)->slots;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
OseoCompletionRecord *oseo_generator_completions(OseoValue generator) {
|
|
857
|
+
return generator_state(generator)->completions;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
OseoValue oseo_generator_callee(OseoValue generator) {
|
|
861
|
+
return generator_state(generator)->callee;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
OseoValue oseo_generator_receiver(OseoValue generator) {
|
|
865
|
+
return generator_state(generator)->receiver;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
OseoValue oseo_generator_sent(OseoValue generator) {
|
|
869
|
+
return generator_state(generator)->sent;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
size_t oseo_generator_resume_point(OseoValue generator) {
|
|
873
|
+
return generator_state(generator)->resume_point;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
size_t oseo_generator_resume_kind(OseoValue generator) {
|
|
877
|
+
return generator_state(generator)->resume_kind;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
void oseo_generator_suspend(
|
|
881
|
+
OseoContext *context,
|
|
882
|
+
OseoValue generator,
|
|
883
|
+
size_t resume_point,
|
|
884
|
+
bool result_object,
|
|
885
|
+
size_t suspend_reason
|
|
886
|
+
) {
|
|
887
|
+
(void)context;
|
|
888
|
+
/* Both suspension reasons leave the body resumable at `resume_point`;
|
|
889
|
+
* the asynchronous driver is the only caller that distinguishes them,
|
|
890
|
+
* and it parks an awaiting body itself once it has the operand. */
|
|
891
|
+
OseoGenerator *state = generator_state(generator);
|
|
892
|
+
state->resume_point = resume_point;
|
|
893
|
+
state->state = OSEO_GENERATOR_SUSPENDED_YIELD;
|
|
894
|
+
state->suspend_reason = suspend_reason;
|
|
895
|
+
state->yielded_result_object = result_object;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
/*
|
|
899
|
+
* Discard [[GeneratorContext]] once a generator completes. A completed
|
|
900
|
+
* generator can never be resumed, so retaining its frame would keep the
|
|
901
|
+
* whole suspended object graph reachable for as long as the generator
|
|
902
|
+
* itself is.
|
|
903
|
+
*/
|
|
904
|
+
void oseo_internal_generator_complete(OseoValue generator) {
|
|
905
|
+
OseoGenerator *state = generator_state(generator);
|
|
906
|
+
state->state = OSEO_GENERATOR_COMPLETED;
|
|
907
|
+
state->callee = oseo_undefined();
|
|
908
|
+
state->receiver = oseo_undefined();
|
|
909
|
+
state->sent = oseo_undefined();
|
|
910
|
+
/* A completed asynchronous generator still reports the requests it
|
|
911
|
+
* already accepted, so only the frame is discarded here; the driver
|
|
912
|
+
* drains the queue itself. */
|
|
913
|
+
for (size_t index = 0u; index < state->slot_count; index += 1u) {
|
|
914
|
+
state->slots[index] = oseo_undefined();
|
|
915
|
+
}
|
|
916
|
+
if (state->completion_count > 0u) {
|
|
917
|
+
memset(
|
|
918
|
+
state->completions,
|
|
919
|
+
0,
|
|
920
|
+
state->completion_count * sizeof(OseoCompletionRecord)
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/*
|
|
926
|
+
* GeneratorResume and GeneratorResumeAbrupt over a return completion.
|
|
927
|
+
* Both deliver `sent` to the pending suspension and differ only in the
|
|
928
|
+
* resume kind the body observes and in what an unstarted or completed
|
|
929
|
+
* generator reports without entering the body.
|
|
930
|
+
*/
|
|
931
|
+
static OseoResult generator_resume(
|
|
932
|
+
OseoContext *context,
|
|
933
|
+
OseoValue generator,
|
|
934
|
+
OseoValue sent,
|
|
935
|
+
size_t resume_kind
|
|
936
|
+
) {
|
|
937
|
+
bool returning = resume_kind == OSEO_GENERATOR_RESUME_RETURN;
|
|
938
|
+
bool throwing = resume_kind == OSEO_GENERATOR_RESUME_THROW;
|
|
939
|
+
/* An asynchronous generator reports its steps through promises, so
|
|
940
|
+
* the synchronous resumption never drives one even when a caller
|
|
941
|
+
* reaches this method with one as the receiver. */
|
|
942
|
+
if (!is_generator(generator) || is_async_generator(generator)) {
|
|
943
|
+
return oseo_internal_throw_error(
|
|
944
|
+
context,
|
|
945
|
+
OSEO_ERROR_TYPE,
|
|
946
|
+
returning
|
|
947
|
+
? "Generator return requires a generator receiver."
|
|
948
|
+
: throwing
|
|
949
|
+
? "Generator throw requires a generator receiver."
|
|
950
|
+
: "Generator next requires a generator receiver."
|
|
951
|
+
);
|
|
952
|
+
}
|
|
953
|
+
if (generator_state(generator)->state == OSEO_GENERATOR_EXECUTING) {
|
|
954
|
+
return oseo_internal_throw_error(
|
|
955
|
+
context,
|
|
956
|
+
OSEO_ERROR_TYPE,
|
|
957
|
+
"Cannot resume an already running generator."
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
961
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
962
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
963
|
+
frame.slots[0] = generator;
|
|
964
|
+
frame.slots[1] = sent;
|
|
965
|
+
OseoGeneratorState current = generator_state(frame.slots[0])->state;
|
|
966
|
+
/* A return completion delivered before the body ever ran completes the
|
|
967
|
+
* generator without entering it, so no `finally` in a body that never
|
|
968
|
+
* started runs, and it reports the requested value. A completed
|
|
969
|
+
* generator reports it too, while `next` reports undefined. A throw
|
|
970
|
+
* completion re-throws the value. */
|
|
971
|
+
bool unstarted =
|
|
972
|
+
(returning || throwing) && current == OSEO_GENERATOR_SUSPENDED_START;
|
|
973
|
+
if (current == OSEO_GENERATOR_COMPLETED || unstarted) {
|
|
974
|
+
if (unstarted) oseo_internal_generator_complete(frame.slots[0]);
|
|
975
|
+
if (throwing) {
|
|
976
|
+
result = (OseoResult){OSEO_STATUS_THROW, frame.slots[1]};
|
|
977
|
+
oseo_roots_release(context, &frame);
|
|
978
|
+
return result;
|
|
979
|
+
}
|
|
980
|
+
result = oseo_internal_iterator_result(
|
|
981
|
+
context,
|
|
982
|
+
returning ? frame.slots[1] : oseo_undefined(),
|
|
983
|
+
true
|
|
984
|
+
);
|
|
985
|
+
oseo_roots_release(context, &frame);
|
|
986
|
+
return result;
|
|
987
|
+
}
|
|
988
|
+
if (context->generator_dispatcher == NULL) {
|
|
989
|
+
oseo_roots_release(context, &frame);
|
|
990
|
+
return failure(
|
|
991
|
+
context,
|
|
992
|
+
"OSEO2001",
|
|
993
|
+
"No generated generator dispatcher is installed."
|
|
994
|
+
);
|
|
995
|
+
}
|
|
996
|
+
OseoGenerator *state = generator_state(frame.slots[0]);
|
|
997
|
+
state->sent = frame.slots[1];
|
|
998
|
+
state->resume_kind = resume_kind;
|
|
999
|
+
state->state = OSEO_GENERATOR_EXECUTING;
|
|
1000
|
+
result = oseo_call_enter(context);
|
|
1001
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1002
|
+
oseo_internal_generator_complete(frame.slots[0]);
|
|
1003
|
+
oseo_roots_release(context, &frame);
|
|
1004
|
+
return result;
|
|
1005
|
+
}
|
|
1006
|
+
OseoRealmScope realm_scope;
|
|
1007
|
+
oseo_internal_realm_enter(
|
|
1008
|
+
context,
|
|
1009
|
+
&realm_scope,
|
|
1010
|
+
oseo_internal_callee_realm(
|
|
1011
|
+
context,
|
|
1012
|
+
generator_state(frame.slots[0])->callee
|
|
1013
|
+
)
|
|
1014
|
+
);
|
|
1015
|
+
/* A resumed body is the running function of the calls it makes
|
|
1016
|
+
* (ADR 0034); its generator record roots the callee. */
|
|
1017
|
+
OseoActivation activation;
|
|
1018
|
+
oseo_internal_activation_push(
|
|
1019
|
+
context,
|
|
1020
|
+
&activation,
|
|
1021
|
+
generator_state(frame.slots[0])->callee,
|
|
1022
|
+
0u,
|
|
1023
|
+
NULL
|
|
1024
|
+
);
|
|
1025
|
+
result = context->generator_dispatcher(context, frame.slots[0]);
|
|
1026
|
+
oseo_internal_activation_pop(context, &activation);
|
|
1027
|
+
oseo_internal_realm_leave(context, &realm_scope);
|
|
1028
|
+
oseo_call_leave(context);
|
|
1029
|
+
state = generator_state(frame.slots[0]);
|
|
1030
|
+
state->sent = oseo_undefined();
|
|
1031
|
+
/* The kind describes one resumption only; the next suspension is
|
|
1032
|
+
* resumed normally unless that resumption says otherwise. */
|
|
1033
|
+
state->resume_kind = OSEO_GENERATOR_RESUME_NEXT;
|
|
1034
|
+
/* The completion value only lives in `result` until it is rooted,
|
|
1035
|
+
* and discarding the frame drops the slot it came from. */
|
|
1036
|
+
frame.slots[1] = result.value;
|
|
1037
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1038
|
+
oseo_internal_generator_complete(frame.slots[0]);
|
|
1039
|
+
result.value = frame.slots[1];
|
|
1040
|
+
oseo_roots_release(context, &frame);
|
|
1041
|
+
return result;
|
|
1042
|
+
}
|
|
1043
|
+
bool done = state->state != OSEO_GENERATOR_SUSPENDED_YIELD;
|
|
1044
|
+
if (done) {
|
|
1045
|
+
oseo_internal_generator_complete(frame.slots[0]);
|
|
1046
|
+
} else if (state->yielded_result_object) {
|
|
1047
|
+
/* GeneratorYield receives an already-built iterator result
|
|
1048
|
+
* object from `yield*`, so it reaches the resuming consumer
|
|
1049
|
+
* unchanged, including a result that omits `done`. */
|
|
1050
|
+
OseoValue yielded = frame.slots[1];
|
|
1051
|
+
oseo_roots_release(context, &frame);
|
|
1052
|
+
return normal(yielded);
|
|
1053
|
+
}
|
|
1054
|
+
result = oseo_internal_iterator_result(context, frame.slots[1], done);
|
|
1055
|
+
oseo_roots_release(context, &frame);
|
|
1056
|
+
return result;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
OseoResult oseo_generator_next(
|
|
1060
|
+
OseoContext *context,
|
|
1061
|
+
OseoValue generator,
|
|
1062
|
+
OseoValue sent
|
|
1063
|
+
) {
|
|
1064
|
+
return generator_resume(
|
|
1065
|
+
context,
|
|
1066
|
+
generator,
|
|
1067
|
+
sent,
|
|
1068
|
+
OSEO_GENERATOR_RESUME_NEXT
|
|
1069
|
+
);
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
OseoResult oseo_generator_return(
|
|
1073
|
+
OseoContext *context,
|
|
1074
|
+
OseoValue generator,
|
|
1075
|
+
OseoValue value
|
|
1076
|
+
) {
|
|
1077
|
+
return generator_resume(
|
|
1078
|
+
context,
|
|
1079
|
+
generator,
|
|
1080
|
+
value,
|
|
1081
|
+
OSEO_GENERATOR_RESUME_RETURN
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
OseoResult oseo_generator_throw(
|
|
1086
|
+
OseoContext *context,
|
|
1087
|
+
OseoValue generator,
|
|
1088
|
+
OseoValue value
|
|
1089
|
+
) {
|
|
1090
|
+
return generator_resume(
|
|
1091
|
+
context,
|
|
1092
|
+
generator,
|
|
1093
|
+
value,
|
|
1094
|
+
OSEO_GENERATOR_RESUME_THROW
|
|
1095
|
+
);
|
|
1096
|
+
}
|