@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +1677 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2594 -0
  10. package/native/runtime_agent.c +1693 -0
  11. package/native/runtime_arguments.c +355 -0
  12. package/native/runtime_array.c +5742 -0
  13. package/native/runtime_array_buffer.c +1724 -0
  14. package/native/runtime_async_generator.c +948 -0
  15. package/native/runtime_atomics.c +1259 -0
  16. package/native/runtime_bigint.c +1363 -0
  17. package/native/runtime_bigint_object.c +602 -0
  18. package/native/runtime_binding.c +1005 -0
  19. package/native/runtime_boolean.c +296 -0
  20. package/native/runtime_clock.c +157 -0
  21. package/native/runtime_clock_posix.c +575 -0
  22. package/native/runtime_core.c +464 -0
  23. package/native/runtime_data_view.c +1111 -0
  24. package/native/runtime_date.c +1973 -0
  25. package/native/runtime_descriptor.c +1080 -0
  26. package/native/runtime_disposal.c +1643 -0
  27. package/native/runtime_enumeration.c +612 -0
  28. package/native/runtime_error.c +973 -0
  29. package/native/runtime_event_loop.c +629 -0
  30. package/native/runtime_function.c +3643 -0
  31. package/native/runtime_generator.c +1096 -0
  32. package/native/runtime_internal.h +5554 -0
  33. package/native/runtime_iterator.c +5355 -0
  34. package/native/runtime_json.c +1968 -0
  35. package/native/runtime_map.c +1539 -0
  36. package/native/runtime_math.c +894 -0
  37. package/native/runtime_memory.c +1341 -0
  38. package/native/runtime_module.c +860 -0
  39. package/native/runtime_number.c +2053 -0
  40. package/native/runtime_object.c +399 -0
  41. package/native/runtime_object_builtin.c +3877 -0
  42. package/native/runtime_primitive.c +2244 -0
  43. package/native/runtime_process.c +720 -0
  44. package/native/runtime_promise.c +3354 -0
  45. package/native/runtime_property.c +791 -0
  46. package/native/runtime_proxy.c +1533 -0
  47. package/native/runtime_realm.c +411 -0
  48. package/native/runtime_reflect.c +624 -0
  49. package/native/runtime_regexp.c +3495 -0
  50. package/native/runtime_regexp_matcher.c +3535 -0
  51. package/native/runtime_regexp_symbol.c +1471 -0
  52. package/native/runtime_set.c +1751 -0
  53. package/native/runtime_string.c +2862 -0
  54. package/native/runtime_string_match.c +1161 -0
  55. package/native/runtime_symbol.c +986 -0
  56. package/native/runtime_typed_array.c +5113 -0
  57. package/native/runtime_unicode_tables.h +14647 -0
  58. package/native/runtime_uri.c +676 -0
  59. package/native/runtime_weak_collection.c +1275 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1259 @@
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 <string.h>
8
+
9
+ /*
10
+ * The %Atomics% namespace object: the read-modify-write family, load,
11
+ * store, compareExchange, isLockFree, wait, waitAsync, and notify, over
12
+ * integer TypedArray views of ArrayBuffer and SharedArrayBuffer storage.
13
+ *
14
+ * Agents can suspend when their [[CanBlock]] policy permits it.
15
+ * Agents of one cluster take turns on one
16
+ * executing thread (runtime_agent.c), so every atomic step runs without
17
+ * interference and a critical section is never contended; each operation
18
+ * offers the turn to another ready agent once its step is complete.
19
+ *
20
+ * In an ordinary program this realm is the only agent of its cluster, and
21
+ * the WaiterList store is the context-owned FIFO of `Atomics.waitAsync`
22
+ * waiters. `Atomics.wait` never enters that store: once the agent
23
+ * suspends, no code in the cluster can run to notify it. In a multi-agent
24
+ * cluster the store is the cluster's: blocking waiters of every agent and
25
+ * each agent's waitAsync waiters share one FIFO per Shared Data Block and
26
+ * byte index, while the context list still roots this agent's own pending
27
+ * waiters. A notification or timeout in this agent resolves its own
28
+ * waiter's promise synchronously, as NotifyWaiter specifies for a waiter
29
+ * of the surrounding agent, and another agent's waiter resolves in that
30
+ * agent as a task of its own.
31
+ */
32
+
33
+ typedef struct {
34
+ const char *name;
35
+ size_t length;
36
+ } OseoAtomicsFunction;
37
+
38
+ static const OseoAtomicsFunction
39
+ atomics_functions[OSEO_ATOMICS_OPERATION_COUNT] = {
40
+ {"add", 3u},
41
+ {"and", 3u},
42
+ {"compareExchange", 4u},
43
+ {"exchange", 3u},
44
+ {"isLockFree", 1u},
45
+ {"load", 2u},
46
+ {"notify", 3u},
47
+ {"or", 3u},
48
+ {"store", 3u},
49
+ {"sub", 3u},
50
+ {"wait", 4u},
51
+ {"waitAsync", 4u},
52
+ {"xor", 3u},
53
+ {"pause", 0u},
54
+ };
55
+
56
+ #define ATOMICS_TWO_TO_32 4294967296.0
57
+
58
+ static OseoValue atomics_argument(
59
+ size_t argument_count,
60
+ const OseoValue *arguments,
61
+ size_t index
62
+ ) {
63
+ return index < argument_count ? arguments[index] : oseo_undefined();
64
+ }
65
+
66
+ static bool atomics_bigint_kind(OseoTypedArrayKind kind) {
67
+ return kind == OSEO_TYPED_ARRAY_BIGINT64 ||
68
+ kind == OSEO_TYPED_ARRAY_BIGUINT64;
69
+ }
70
+
71
+ /* ToIntegerOrInfinity, with -0 normalized to +0. */
72
+ static OseoResult atomics_integer_or_infinity(
73
+ OseoContext *context,
74
+ OseoValue value,
75
+ double *integer
76
+ ) {
77
+ OseoResult result = oseo_internal_to_number(context, value);
78
+ if (result.status != OSEO_STATUS_NORMAL) return result;
79
+ double number = number_value(result.value);
80
+ if (isnan(number) || number == 0.0) number = 0.0;
81
+ else if (isfinite(number)) number = trunc(number);
82
+ *integer = number;
83
+ return normal(oseo_undefined());
84
+ }
85
+
86
+ /*
87
+ * The low 32 bits of an integral Number, which is every bit NumericToRawBytes
88
+ * keeps for an integer element of at most four bytes. Reducing modulo 2^32
89
+ * rather than 2^64 keeps every intermediate exactly representable.
90
+ */
91
+ static uint64_t atomics_number_bits(double integer) {
92
+ if (!isfinite(integer)) return UINT64_C(0);
93
+ double wrapped = fmod(integer, ATOMICS_TWO_TO_32);
94
+ if (wrapped < 0.0) wrapped += ATOMICS_TWO_TO_32;
95
+ return (uint64_t)wrapped;
96
+ }
97
+
98
+ static uint64_t atomics_element_mask(size_t size) {
99
+ return size >= 8u ? UINT64_MAX : (UINT64_C(1) << (size * 8u)) - 1u;
100
+ }
101
+
102
+ /*
103
+ * ValidateIntegerTypedArray(typedArray, waitable). It reports
104
+ * TypedArrayLength from the same witness, because ValidateAtomicAccess
105
+ * reads the length before it converts the index.
106
+ */
107
+ static OseoResult atomics_validate_integer(
108
+ OseoContext *context,
109
+ OseoValue value,
110
+ bool waitable,
111
+ size_t *length
112
+ ) {
113
+ OseoResult result =
114
+ oseo_internal_typed_array_validate(context, value, length);
115
+ if (result.status != OSEO_STATUS_NORMAL) return result;
116
+ OseoTypedArrayKind kind = typed_array_object(value)->element_kind;
117
+ bool admitted = waitable
118
+ ? kind == OSEO_TYPED_ARRAY_INT32 ||
119
+ kind == OSEO_TYPED_ARRAY_BIGINT64
120
+ : kind != OSEO_TYPED_ARRAY_UINT8_CLAMPED &&
121
+ kind != OSEO_TYPED_ARRAY_FLOAT16 &&
122
+ kind != OSEO_TYPED_ARRAY_FLOAT32 &&
123
+ kind != OSEO_TYPED_ARRAY_FLOAT64;
124
+ if (!admitted) {
125
+ return oseo_internal_throw_error(
126
+ context,
127
+ OSEO_ERROR_TYPE,
128
+ waitable
129
+ ? "Atomics waiting needs an Int32Array or a BigInt64Array."
130
+ : "Atomics needs an integer TypedArray."
131
+ );
132
+ }
133
+ return normal(value);
134
+ }
135
+
136
+ /*
137
+ * ValidateAtomicAccess against the length read before the conversion,
138
+ * reporting the byte index in the viewed buffer.
139
+ */
140
+ static OseoResult atomics_access(
141
+ OseoContext *context,
142
+ OseoValue view,
143
+ OseoValue request,
144
+ size_t length,
145
+ size_t *byte_index
146
+ ) {
147
+ OseoValue slot = view;
148
+ OseoRootFrame frame = {NULL, &slot, 1u};
149
+ oseo_roots_push(context, &frame);
150
+ double index = 0.0;
151
+ OseoResult result = oseo_internal_to_index(
152
+ context,
153
+ request,
154
+ "Atomics index is outside the admitted index range.",
155
+ &index
156
+ );
157
+ if (result.status == OSEO_STATUS_NORMAL && !(index < (double)length)) {
158
+ result = oseo_internal_throw_error(
159
+ context,
160
+ OSEO_ERROR_RANGE,
161
+ "Atomics index is outside the TypedArray."
162
+ );
163
+ }
164
+ if (result.status == OSEO_STATUS_NORMAL) {
165
+ const OseoTypedArray *typed = typed_array_object(slot);
166
+ *byte_index = (size_t)index *
167
+ oseo_internal_typed_array_element_size(typed->element_kind) +
168
+ typed->byte_offset;
169
+ result = normal(slot);
170
+ }
171
+ oseo_roots_pop(context, &frame);
172
+ return result;
173
+ }
174
+
175
+ /*
176
+ * RevalidateAtomicAccess: argument conversions may have detached,
177
+ * shrunk, or otherwise invalidated the view since the first validation.
178
+ * The specification compares only the element's first byte against the
179
+ * buffer length, so an element of a length-tracking view that a shrink
180
+ * left straddling the new end still passes. Its trailing bytes stay
181
+ * inside the block, because a resizable buffer reserves its maximum
182
+ * length and a shared buffer never shrinks.
183
+ */
184
+ static OseoResult atomics_revalidate(
185
+ OseoContext *context,
186
+ OseoValue view,
187
+ size_t byte_index
188
+ ) {
189
+ size_t buffer_length = 0u;
190
+ if (oseo_internal_typed_array_out_of_bounds(view, &buffer_length)) {
191
+ return oseo_internal_throw_error(
192
+ context,
193
+ OSEO_ERROR_TYPE,
194
+ "The TypedArray is detached or out of bounds."
195
+ );
196
+ }
197
+ if (byte_index >= buffer_length) {
198
+ return oseo_internal_throw_error(
199
+ context,
200
+ OSEO_ERROR_RANGE,
201
+ "Atomics index is outside the TypedArray."
202
+ );
203
+ }
204
+ return normal(view);
205
+ }
206
+
207
+ /* The element's bytes at `byte_index`, zero-extended. */
208
+ static uint64_t atomics_read_raw(OseoValue view, size_t byte_index) {
209
+ const OseoTypedArray *typed = typed_array_object(view);
210
+ const uint8_t *source =
211
+ array_buffer_object(typed->viewed_buffer)->data + byte_index;
212
+ switch (oseo_internal_typed_array_element_size(typed->element_kind)) {
213
+ case 1u: {
214
+ uint8_t stored;
215
+ memcpy(&stored, source, sizeof(stored));
216
+ return stored;
217
+ }
218
+ case 2u: {
219
+ uint16_t stored;
220
+ memcpy(&stored, source, sizeof(stored));
221
+ return stored;
222
+ }
223
+ case 4u: {
224
+ uint32_t stored;
225
+ memcpy(&stored, source, sizeof(stored));
226
+ return stored;
227
+ }
228
+ default: {
229
+ uint64_t stored;
230
+ memcpy(&stored, source, sizeof(stored));
231
+ return stored;
232
+ }
233
+ }
234
+ }
235
+
236
+ static void atomics_write_raw(
237
+ OseoValue view,
238
+ size_t byte_index,
239
+ uint64_t bits
240
+ ) {
241
+ const OseoTypedArray *typed = typed_array_object(view);
242
+ uint8_t *target =
243
+ array_buffer_object(typed->viewed_buffer)->data + byte_index;
244
+ switch (oseo_internal_typed_array_element_size(typed->element_kind)) {
245
+ case 1u: {
246
+ uint8_t stored = (uint8_t)bits;
247
+ memcpy(target, &stored, sizeof(stored));
248
+ break;
249
+ }
250
+ case 2u: {
251
+ uint16_t stored = (uint16_t)bits;
252
+ memcpy(target, &stored, sizeof(stored));
253
+ break;
254
+ }
255
+ case 4u: {
256
+ uint32_t stored = (uint32_t)bits;
257
+ memcpy(target, &stored, sizeof(stored));
258
+ break;
259
+ }
260
+ default:
261
+ memcpy(target, &bits, sizeof(bits));
262
+ break;
263
+ }
264
+ }
265
+
266
+ /* RawBytesToNumeric for an integer element kind. */
267
+ static OseoResult atomics_decode(
268
+ OseoContext *context,
269
+ OseoTypedArrayKind kind,
270
+ uint64_t bits
271
+ ) {
272
+ switch (kind) {
273
+ case OSEO_TYPED_ARRAY_INT8:
274
+ return normal(oseo_number(
275
+ bits >= UINT64_C(0x80) ? (double)bits - 256.0 : (double)bits
276
+ ));
277
+ case OSEO_TYPED_ARRAY_INT16:
278
+ return normal(oseo_number(
279
+ bits >= UINT64_C(0x8000)
280
+ ? (double)bits - 65536.0
281
+ : (double)bits
282
+ ));
283
+ case OSEO_TYPED_ARRAY_INT32:
284
+ return normal(oseo_number(
285
+ bits >= UINT64_C(0x80000000)
286
+ ? (double)bits - ATOMICS_TWO_TO_32
287
+ : (double)bits
288
+ ));
289
+ case OSEO_TYPED_ARRAY_BIGINT64: {
290
+ bool negative = (bits >> 63u) != 0u;
291
+ return oseo_internal_bigint_from_uint64(
292
+ context,
293
+ negative ? UINT64_C(0) - bits : bits,
294
+ negative
295
+ );
296
+ }
297
+ case OSEO_TYPED_ARRAY_BIGUINT64:
298
+ return oseo_internal_bigint_from_uint64(context, bits, false);
299
+ default:
300
+ return normal(oseo_number((double)bits));
301
+ }
302
+ }
303
+
304
+ /*
305
+ * The conversion Atomics applies to a value it stores: ToBigInt for a
306
+ * BigInt view and 𝔽(ToIntegerOrInfinity) otherwise. `result.value` is the
307
+ * converted value `store` returns, and `bits` is its raw element image.
308
+ */
309
+ static OseoResult atomics_operand(
310
+ OseoContext *context,
311
+ OseoTypedArrayKind kind,
312
+ OseoValue value,
313
+ uint64_t *bits
314
+ ) {
315
+ if (atomics_bigint_kind(kind)) {
316
+ OseoResult result = oseo_internal_to_bigint(context, value);
317
+ if (result.status == OSEO_STATUS_NORMAL) {
318
+ *bits = oseo_internal_bigint_to_raw_uint64(result.value);
319
+ }
320
+ return result;
321
+ }
322
+ double integer = 0.0;
323
+ OseoResult result = atomics_integer_or_infinity(context, value, &integer);
324
+ if (result.status != OSEO_STATUS_NORMAL) return result;
325
+ *bits = atomics_number_bits(integer);
326
+ return normal(oseo_number(integer));
327
+ }
328
+
329
+ /*
330
+ * Offers the turn to another ready agent once a completed operation's
331
+ * value is in hand, keeping that value rooted in the operation's first
332
+ * spare slot while other agents run. An abrupt completion is returned
333
+ * unchanged, and a single-agent realm returns at once.
334
+ */
335
+ static OseoResult atomics_offer_turn(
336
+ OseoContext *context,
337
+ OseoRootFrame *frame,
338
+ OseoResult completion
339
+ ) {
340
+ if (context->agent == NULL || completion.status != OSEO_STATUS_NORMAL) {
341
+ return completion;
342
+ }
343
+ frame->slots[frame->slot_count - 1u] = completion.value;
344
+ OseoResult result = oseo_internal_agent_yield(context);
345
+ if (result.status != OSEO_STATUS_NORMAL) return result;
346
+ return normal(frame->slots[frame->slot_count - 1u]);
347
+ }
348
+
349
+ /*
350
+ * AtomicReadModifyWrite together with store and load, selected by
351
+ * `operation`. The view validates and the index converts first, then the
352
+ * value converts, and the view revalidates before any byte is read, so a
353
+ * conversion that detaches or shrinks the buffer throws instead of
354
+ * touching the block.
355
+ */
356
+ static OseoResult atomics_read_modify_write(
357
+ OseoContext *context,
358
+ OseoAtomicsOperation operation,
359
+ size_t argument_count,
360
+ const OseoValue *arguments
361
+ ) {
362
+ OseoRootFrame frame = {NULL, NULL, 0u};
363
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
364
+ if (result.status != OSEO_STATUS_NORMAL) return result;
365
+ frame.slots[0] = atomics_argument(argument_count, arguments, 0u);
366
+ size_t length = 0u;
367
+ size_t byte_index = 0u;
368
+ result = atomics_validate_integer(context, frame.slots[0], false, &length);
369
+ if (result.status == OSEO_STATUS_NORMAL) {
370
+ result = atomics_access(
371
+ context,
372
+ frame.slots[0],
373
+ atomics_argument(argument_count, arguments, 1u),
374
+ length,
375
+ &byte_index
376
+ );
377
+ }
378
+ uint64_t operand = UINT64_C(0);
379
+ if (result.status == OSEO_STATUS_NORMAL &&
380
+ operation != OSEO_ATOMICS_LOAD) {
381
+ result = atomics_operand(
382
+ context,
383
+ typed_array_object(frame.slots[0])->element_kind,
384
+ atomics_argument(argument_count, arguments, 2u),
385
+ &operand
386
+ );
387
+ frame.slots[1] = result.value;
388
+ }
389
+ if (result.status == OSEO_STATUS_NORMAL) {
390
+ result = atomics_revalidate(context, frame.slots[0], byte_index);
391
+ }
392
+ if (result.status == OSEO_STATUS_NORMAL) {
393
+ OseoTypedArrayKind kind =
394
+ typed_array_object(frame.slots[0])->element_kind;
395
+ uint64_t mask = atomics_element_mask(
396
+ oseo_internal_typed_array_element_size(kind)
397
+ );
398
+ uint64_t previous = atomics_read_raw(frame.slots[0], byte_index);
399
+ uint64_t next = previous;
400
+ switch (operation) {
401
+ case OSEO_ATOMICS_ADD:
402
+ next = previous + operand;
403
+ break;
404
+ case OSEO_ATOMICS_AND:
405
+ next = previous & operand;
406
+ break;
407
+ case OSEO_ATOMICS_OR:
408
+ next = previous | operand;
409
+ break;
410
+ case OSEO_ATOMICS_SUB:
411
+ next = previous - operand;
412
+ break;
413
+ case OSEO_ATOMICS_XOR:
414
+ next = previous ^ operand;
415
+ break;
416
+ case OSEO_ATOMICS_EXCHANGE:
417
+ case OSEO_ATOMICS_STORE:
418
+ next = operand;
419
+ break;
420
+ default:
421
+ break;
422
+ }
423
+ if (operation != OSEO_ATOMICS_LOAD) {
424
+ atomics_write_raw(frame.slots[0], byte_index, next & mask);
425
+ }
426
+ result = operation == OSEO_ATOMICS_STORE
427
+ ? normal(frame.slots[1])
428
+ : atomics_decode(context, kind, previous & mask);
429
+ }
430
+ result = atomics_offer_turn(context, &frame, result);
431
+ oseo_roots_release(context, &frame);
432
+ return result;
433
+ }
434
+
435
+ /*
436
+ * Atomics.compareExchange. Both values convert before the revalidation,
437
+ * and the comparison is ByteListEqual over their raw element images, so
438
+ * an expected value that wraps to the stored bits matches.
439
+ */
440
+ static OseoResult atomics_compare_exchange(
441
+ OseoContext *context,
442
+ size_t argument_count,
443
+ const OseoValue *arguments
444
+ ) {
445
+ OseoRootFrame frame = {NULL, NULL, 0u};
446
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
447
+ if (result.status != OSEO_STATUS_NORMAL) return result;
448
+ frame.slots[0] = atomics_argument(argument_count, arguments, 0u);
449
+ size_t length = 0u;
450
+ size_t byte_index = 0u;
451
+ result = atomics_validate_integer(context, frame.slots[0], false, &length);
452
+ if (result.status == OSEO_STATUS_NORMAL) {
453
+ result = atomics_access(
454
+ context,
455
+ frame.slots[0],
456
+ atomics_argument(argument_count, arguments, 1u),
457
+ length,
458
+ &byte_index
459
+ );
460
+ }
461
+ uint64_t expected = UINT64_C(0);
462
+ uint64_t replacement = UINT64_C(0);
463
+ if (result.status == OSEO_STATUS_NORMAL) {
464
+ result = atomics_operand(
465
+ context,
466
+ typed_array_object(frame.slots[0])->element_kind,
467
+ atomics_argument(argument_count, arguments, 2u),
468
+ &expected
469
+ );
470
+ }
471
+ if (result.status == OSEO_STATUS_NORMAL) {
472
+ result = atomics_operand(
473
+ context,
474
+ typed_array_object(frame.slots[0])->element_kind,
475
+ atomics_argument(argument_count, arguments, 3u),
476
+ &replacement
477
+ );
478
+ }
479
+ if (result.status == OSEO_STATUS_NORMAL) {
480
+ result = atomics_revalidate(context, frame.slots[0], byte_index);
481
+ }
482
+ if (result.status == OSEO_STATUS_NORMAL) {
483
+ OseoTypedArrayKind kind =
484
+ typed_array_object(frame.slots[0])->element_kind;
485
+ uint64_t mask = atomics_element_mask(
486
+ oseo_internal_typed_array_element_size(kind)
487
+ );
488
+ uint64_t previous = atomics_read_raw(frame.slots[0], byte_index);
489
+ if ((previous & mask) == (expected & mask)) {
490
+ atomics_write_raw(frame.slots[0], byte_index, replacement & mask);
491
+ }
492
+ result = atomics_decode(context, kind, previous & mask);
493
+ }
494
+ result = atomics_offer_turn(context, &frame, result);
495
+ oseo_roots_release(context, &frame);
496
+ return result;
497
+ }
498
+
499
+ /*
500
+ * Atomics.isLockFree. Every atomic step of this single agent runs
501
+ * without a lock of any size, so the agent record reports true for 1, 2,
502
+ * and 8 bytes as well as for the required 4.
503
+ */
504
+ static OseoResult atomics_is_lock_free(
505
+ OseoContext *context,
506
+ size_t argument_count,
507
+ const OseoValue *arguments
508
+ ) {
509
+ double size = 0.0;
510
+ OseoResult result = atomics_integer_or_infinity(
511
+ context,
512
+ atomics_argument(argument_count, arguments, 0u),
513
+ &size
514
+ );
515
+ if (result.status != OSEO_STATUS_NORMAL) return result;
516
+ return normal(oseo_boolean(
517
+ size == 1.0 || size == 2.0 || size == 4.0 || size == 8.0
518
+ ));
519
+ }
520
+
521
+ /* Unlinks one listed waiter from the context's WaiterList store. */
522
+ static void atomics_unlink_waiter(OseoContext *context, OseoValue waiter) {
523
+ OseoValue previous = oseo_undefined();
524
+ OseoValue current = context->atomics_waiter_head;
525
+ while (tag_of(current) != OSEO_TAG_UNDEFINED && current != waiter) {
526
+ previous = current;
527
+ current = atomics_waiter_object(current)->next;
528
+ }
529
+ if (tag_of(current) == OSEO_TAG_UNDEFINED) return;
530
+ OseoAtomicsWaiter *record = atomics_waiter_object(current);
531
+ if (tag_of(previous) == OSEO_TAG_UNDEFINED) {
532
+ context->atomics_waiter_head = record->next;
533
+ } else {
534
+ atomics_waiter_object(previous)->next = record->next;
535
+ }
536
+ if (context->atomics_waiter_tail == current) {
537
+ context->atomics_waiter_tail = previous;
538
+ }
539
+ record->next = oseo_undefined();
540
+ record->listed = false;
541
+ }
542
+
543
+ /*
544
+ * NotifyWaiter for a waiter of this agent, after its removal: its promise
545
+ * resolves synchronously to `outcome`. Resolving with a string runs no
546
+ * user code, so the waiter needs no further protection here.
547
+ */
548
+ static OseoResult atomics_resolve_waiter(
549
+ OseoContext *context,
550
+ OseoValue waiter,
551
+ const char *outcome
552
+ ) {
553
+ OseoValue slots[2] = {waiter, oseo_undefined()};
554
+ OseoRootFrame frame = {NULL, slots, 2u};
555
+ oseo_roots_push(context, &frame);
556
+ OseoAtomicsWaiter *record = atomics_waiter_object(slots[0]);
557
+ if (tag_of(record->timer) != OSEO_TAG_UNDEFINED) {
558
+ timer_object(record->timer)->canceled = true;
559
+ record->timer = oseo_undefined();
560
+ }
561
+ OseoResult result = oseo_internal_ascii_string(context, outcome);
562
+ slots[1] = result.value;
563
+ if (result.status == OSEO_STATUS_NORMAL) {
564
+ result = oseo_promise_resolve_into(
565
+ context,
566
+ atomics_waiter_object(slots[0])->promise,
567
+ slots[1]
568
+ );
569
+ }
570
+ oseo_roots_pop(context, &frame);
571
+ return result;
572
+ }
573
+
574
+ OseoResult oseo_internal_atomics_waiter_timeout(
575
+ OseoContext *context,
576
+ OseoValue waiter
577
+ ) {
578
+ OseoAtomicsWaiter *record = atomics_waiter_object(waiter);
579
+ if (!record->listed) return normal(oseo_undefined());
580
+ /*
581
+ * In a multi-agent cluster another agent's notification may already
582
+ * have removed the waiter from the store; its "ok" is then on the way
583
+ * to this agent as a task, and the timeout job does nothing.
584
+ */
585
+ if (context->agent != NULL &&
586
+ !oseo_internal_agent_unlist_waiter(context, waiter)) {
587
+ return normal(oseo_undefined());
588
+ }
589
+ /* The job is running, so the waiter no longer owns a pending timer. */
590
+ record->timer = oseo_undefined();
591
+ atomics_unlink_waiter(context, waiter);
592
+ return atomics_resolve_waiter(context, waiter, "timed-out");
593
+ }
594
+
595
+ OseoResult oseo_internal_atomics_waiter_notified(
596
+ OseoContext *context,
597
+ OseoValue waiter
598
+ ) {
599
+ if (!atomics_waiter_object(waiter)->listed) {
600
+ return normal(oseo_undefined());
601
+ }
602
+ atomics_unlink_waiter(context, waiter);
603
+ return atomics_resolve_waiter(context, waiter, "ok");
604
+ }
605
+
606
+ /*
607
+ * Atomics.notify. A buffer that is not shared has no waiters and reports
608
+ * zero after the validation and conversions. Otherwise the first `count`
609
+ * waiters on the same block and byte index leave the store in FIFO order:
610
+ * this agent's own waitAsync waiter resolves to "ok" as it leaves, a
611
+ * blocking waiter of another agent wakes, and another agent's waitAsync
612
+ * waiter resolves in that agent.
613
+ */
614
+ static OseoResult atomics_notify(
615
+ OseoContext *context,
616
+ size_t argument_count,
617
+ const OseoValue *arguments
618
+ ) {
619
+ OseoRootFrame frame = {NULL, NULL, 0u};
620
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
621
+ if (result.status != OSEO_STATUS_NORMAL) return result;
622
+ frame.slots[0] = atomics_argument(argument_count, arguments, 0u);
623
+ size_t length = 0u;
624
+ size_t byte_index = 0u;
625
+ result = atomics_validate_integer(context, frame.slots[0], true, &length);
626
+ if (result.status == OSEO_STATUS_NORMAL) {
627
+ result = atomics_access(
628
+ context,
629
+ frame.slots[0],
630
+ atomics_argument(argument_count, arguments, 1u),
631
+ length,
632
+ &byte_index
633
+ );
634
+ }
635
+ double count = INFINITY;
636
+ OseoValue requested = atomics_argument(argument_count, arguments, 2u);
637
+ if (result.status == OSEO_STATUS_NORMAL &&
638
+ tag_of(requested) != OSEO_TAG_UNDEFINED) {
639
+ result = atomics_integer_or_infinity(context, requested, &count);
640
+ if (count < 0.0) count = 0.0;
641
+ }
642
+ double notified = 0.0;
643
+ if (result.status == OSEO_STATUS_NORMAL) {
644
+ frame.slots[1] = typed_array_object(frame.slots[0])->viewed_buffer;
645
+ }
646
+ if (result.status == OSEO_STATUS_NORMAL &&
647
+ array_buffer_object(frame.slots[1])->shared) {
648
+ const OseoSharedBlock *block =
649
+ array_buffer_object(frame.slots[1])->block;
650
+ while (result.status == OSEO_STATUS_NORMAL && notified < count) {
651
+ OseoValue current = oseo_undefined();
652
+ if (context->agent != NULL) {
653
+ if (!oseo_internal_agent_notify_one(
654
+ context,
655
+ block,
656
+ byte_index,
657
+ &current
658
+ )) {
659
+ break;
660
+ }
661
+ notified += 1.0;
662
+ /* Only this agent's own waitAsync waiter resolves here. */
663
+ if (tag_of(current) == OSEO_TAG_UNDEFINED) continue;
664
+ } else {
665
+ current = context->atomics_waiter_head;
666
+ while (tag_of(current) != OSEO_TAG_UNDEFINED) {
667
+ const OseoAtomicsWaiter *record =
668
+ atomics_waiter_object(current);
669
+ if (record->block == block &&
670
+ record->byte_index == byte_index) {
671
+ break;
672
+ }
673
+ current = record->next;
674
+ }
675
+ if (tag_of(current) == OSEO_TAG_UNDEFINED) break;
676
+ notified += 1.0;
677
+ }
678
+ frame.slots[2] = current;
679
+ atomics_unlink_waiter(context, frame.slots[2]);
680
+ result = atomics_resolve_waiter(context, frame.slots[2], "ok");
681
+ }
682
+ }
683
+ if (result.status == OSEO_STATUS_NORMAL) {
684
+ result = normal(oseo_number(notified));
685
+ }
686
+ result = atomics_offer_turn(context, &frame, result);
687
+ oseo_roots_release(context, &frame);
688
+ return result;
689
+ }
690
+
691
+ /* CreateDataPropertyOrThrow on the fresh waitAsync result object. */
692
+ static OseoResult atomics_define_result(
693
+ OseoContext *context,
694
+ OseoValue object,
695
+ const char *name,
696
+ OseoValue value
697
+ ) {
698
+ OseoValue slots[3] = {object, value, oseo_undefined()};
699
+ OseoRootFrame frame = {NULL, slots, 3u};
700
+ oseo_roots_push(context, &frame);
701
+ OseoResult result = oseo_internal_ascii_string(context, name);
702
+ slots[2] = result.value;
703
+ if (result.status == OSEO_STATUS_NORMAL) {
704
+ result = oseo_object_define(
705
+ context,
706
+ slots[0],
707
+ slots[2],
708
+ slots[1],
709
+ (OseoPropertyAttributes){true, true, true, false}
710
+ );
711
+ }
712
+ oseo_roots_pop(context, &frame);
713
+ return result;
714
+ }
715
+
716
+ /* The { async, value } record waitAsync returns. */
717
+ static OseoResult atomics_wait_result(
718
+ OseoContext *context,
719
+ bool asynchronous,
720
+ OseoValue value
721
+ ) {
722
+ OseoValue slots[2] = {value, oseo_undefined()};
723
+ OseoRootFrame frame = {NULL, slots, 2u};
724
+ oseo_roots_push(context, &frame);
725
+ OseoResult result = oseo_internal_intrinsic(
726
+ context,
727
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
728
+ );
729
+ if (result.status == OSEO_STATUS_NORMAL) {
730
+ result = oseo_object_create(context, result.value);
731
+ slots[1] = result.value;
732
+ }
733
+ if (result.status == OSEO_STATUS_NORMAL) {
734
+ result = atomics_define_result(
735
+ context,
736
+ slots[1],
737
+ "async",
738
+ oseo_boolean(asynchronous)
739
+ );
740
+ }
741
+ if (result.status == OSEO_STATUS_NORMAL) {
742
+ result = atomics_define_result(context, slots[1], "value", slots[0]);
743
+ }
744
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(slots[1]);
745
+ oseo_roots_pop(context, &frame);
746
+ return result;
747
+ }
748
+
749
+ /*
750
+ * The whole-millisecond deadline of a finite timeout `t` after `base`. A
751
+ * fractional timeout rounds up, which is a permitted additionalTimeout.
752
+ */
753
+ static uint64_t atomics_deadline(uint64_t base, double timeout) {
754
+ double rounded = ceil(timeout);
755
+ uint64_t delay = rounded >= 18446744073709549568.0
756
+ ? UINT64_MAX
757
+ : (uint64_t)rounded;
758
+ return UINT64_MAX - base < delay ? UINT64_MAX : base + delay;
759
+ }
760
+
761
+ /*
762
+ * SuspendThisAgent. In a multi-agent cluster the agent lists a blocking
763
+ * waiter and gives up the turn until a notification or its timeout ends
764
+ * the wait. Otherwise no other agent can notify it: the agent blocks
765
+ * through the clock adapter until the deadline passes, or forever for an
766
+ * infinite timeout, and the wait always ends "timed-out".
767
+ */
768
+ static OseoResult atomics_suspend(
769
+ OseoContext *context,
770
+ const OseoSharedBlock *block,
771
+ size_t byte_index,
772
+ double timeout
773
+ ) {
774
+ if (context->agent != NULL) {
775
+ bool notified = false;
776
+ OseoResult suspended = oseo_internal_agent_suspend(
777
+ context,
778
+ block,
779
+ byte_index,
780
+ timeout,
781
+ &notified
782
+ );
783
+ if (suspended.status != OSEO_STATUS_NORMAL) return suspended;
784
+ return oseo_internal_ascii_string(
785
+ context,
786
+ notified ? "ok" : "timed-out"
787
+ );
788
+ }
789
+ OseoResult result = oseo_internal_clock_start(context);
790
+ if (result.status != OSEO_STATUS_NORMAL) return result;
791
+ uint64_t deadline = UINT64_MAX;
792
+ if (isfinite(timeout)) {
793
+ uint64_t now = 0u;
794
+ result = oseo_internal_clock_now(context, &now);
795
+ if (result.status != OSEO_STATUS_NORMAL) return result;
796
+ deadline = atomics_deadline(now, timeout);
797
+ }
798
+ for (;;) {
799
+ uint64_t now = 0u;
800
+ result = oseo_internal_clock_now(context, &now);
801
+ if (result.status != OSEO_STATUS_NORMAL) return result;
802
+ if (isfinite(timeout) && now >= deadline) break;
803
+ result = oseo_internal_clock_wait_until(context, deadline);
804
+ if (result.status != OSEO_STATUS_NORMAL) return result;
805
+ }
806
+ return oseo_internal_ascii_string(context, "timed-out");
807
+ }
808
+
809
+ /*
810
+ * Appends one pending waitAsync waiter to the WaiterList store. DoWait
811
+ * computes the timeout time from the current time, not from the time the
812
+ * task started, so a waiter created after a blocking wait or a long task
813
+ * still waits its whole timeout before its timeout job becomes due.
814
+ */
815
+ static OseoResult atomics_add_waiter(
816
+ OseoContext *context,
817
+ OseoValue buffer,
818
+ size_t byte_index,
819
+ OseoValue promise,
820
+ double timeout
821
+ ) {
822
+ OseoValue slots[3] = {buffer, promise, oseo_undefined()};
823
+ OseoRootFrame frame = {NULL, slots, 3u};
824
+ oseo_roots_push(context, &frame);
825
+ OseoResult result = normal(oseo_undefined());
826
+ uint64_t deadline = UINT64_MAX;
827
+ if (isfinite(timeout)) {
828
+ uint64_t now = 0u;
829
+ result = oseo_internal_clock_now(context, &now);
830
+ deadline = atomics_deadline(now, timeout);
831
+ }
832
+ if (context->agent != NULL && isfinite(timeout) &&
833
+ result.status == OSEO_STATUS_NORMAL && deadline < UINT64_MAX) {
834
+ /* Another agent can measure the wait, so the current whole
835
+ * millisecond counts as already started, as a blocking wait's
836
+ * deadline in a cluster does. */
837
+ deadline += 1u;
838
+ }
839
+ OseoAtomicsWaiter *waiter = NULL;
840
+ if (result.status == OSEO_STATUS_NORMAL) {
841
+ waiter = oseo_internal_allocate_heap_bytes(context, sizeof(*waiter));
842
+ if (waiter == NULL) {
843
+ result = failure(
844
+ context,
845
+ "OSEO2001",
846
+ "Atomics waiter allocation failed."
847
+ );
848
+ }
849
+ }
850
+ if (result.status == OSEO_STATUS_NORMAL) {
851
+ waiter->next = oseo_undefined();
852
+ waiter->buffer = slots[0];
853
+ waiter->promise = slots[1];
854
+ waiter->timer = oseo_undefined();
855
+ waiter->block = array_buffer_object(slots[0])->block;
856
+ waiter->record = NULL;
857
+ waiter->byte_index = byte_index;
858
+ waiter->listed = true;
859
+ result = oseo_internal_publish_heap(
860
+ context,
861
+ &waiter->header,
862
+ OSEO_HEAP_ATOMICS_WAITER
863
+ );
864
+ slots[2] = result.value;
865
+ }
866
+ if (result.status == OSEO_STATUS_NORMAL) {
867
+ if (tag_of(context->atomics_waiter_tail) == OSEO_TAG_UNDEFINED) {
868
+ context->atomics_waiter_head = slots[2];
869
+ } else {
870
+ atomics_waiter_object(context->atomics_waiter_tail)->next =
871
+ slots[2];
872
+ }
873
+ context->atomics_waiter_tail = slots[2];
874
+ if (context->agent != NULL) {
875
+ result = oseo_internal_agent_list_waiter(context, slots[2]);
876
+ if (result.status != OSEO_STATUS_NORMAL) {
877
+ atomics_unlink_waiter(context, slots[2]);
878
+ }
879
+ }
880
+ if (result.status == OSEO_STATUS_NORMAL && isfinite(timeout)) {
881
+ result = oseo_internal_atomics_timeout_enqueue(
882
+ context,
883
+ slots[2],
884
+ deadline
885
+ );
886
+ if (result.status == OSEO_STATUS_NORMAL) {
887
+ atomics_waiter_object(slots[2])->timer = result.value;
888
+ } else {
889
+ if (context->agent != NULL) {
890
+ (void)oseo_internal_agent_unlist_waiter(
891
+ context,
892
+ slots[2]
893
+ );
894
+ }
895
+ atomics_unlink_waiter(context, slots[2]);
896
+ }
897
+ }
898
+ }
899
+ oseo_roots_pop(context, &frame);
900
+ return result;
901
+ }
902
+
903
+ /*
904
+ * DoWait(mode, typedArray, index, value, timeout). `asynchronous` selects
905
+ * waitAsync. The comparison reads the element as the view's own type,
906
+ * so an Int32Array compares ToInt32(value) and a BigInt64Array compares
907
+ * ToBigInt64(value), both as two's-complement bits.
908
+ */
909
+ static OseoResult atomics_wait(
910
+ OseoContext *context,
911
+ bool asynchronous,
912
+ size_t argument_count,
913
+ const OseoValue *arguments
914
+ ) {
915
+ OseoRootFrame frame = {NULL, NULL, 0u};
916
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
917
+ if (result.status != OSEO_STATUS_NORMAL) return result;
918
+ frame.slots[0] = atomics_argument(argument_count, arguments, 0u);
919
+ size_t length = 0u;
920
+ size_t byte_index = 0u;
921
+ result = atomics_validate_integer(context, frame.slots[0], true, &length);
922
+ if (result.status == OSEO_STATUS_NORMAL) {
923
+ frame.slots[1] = typed_array_object(frame.slots[0])->viewed_buffer;
924
+ if (!array_buffer_object(frame.slots[1])->shared) {
925
+ result = oseo_internal_throw_error(
926
+ context,
927
+ OSEO_ERROR_TYPE,
928
+ "Atomics waiting needs a SharedArrayBuffer."
929
+ );
930
+ }
931
+ }
932
+ if (result.status == OSEO_STATUS_NORMAL) {
933
+ result = atomics_access(
934
+ context,
935
+ frame.slots[0],
936
+ atomics_argument(argument_count, arguments, 1u),
937
+ length,
938
+ &byte_index
939
+ );
940
+ }
941
+ bool bigint = false;
942
+ uint64_t expected = UINT64_C(0);
943
+ if (result.status == OSEO_STATUS_NORMAL) {
944
+ bigint = typed_array_object(frame.slots[0])->element_kind ==
945
+ OSEO_TYPED_ARRAY_BIGINT64;
946
+ OseoValue value = atomics_argument(argument_count, arguments, 2u);
947
+ if (bigint) {
948
+ result = oseo_internal_to_bigint(context, value);
949
+ if (result.status == OSEO_STATUS_NORMAL) {
950
+ expected = oseo_internal_bigint_to_raw_uint64(result.value);
951
+ }
952
+ } else {
953
+ result = oseo_internal_to_number(context, value);
954
+ if (result.status == OSEO_STATUS_NORMAL) {
955
+ double number = number_value(result.value);
956
+ expected = atomics_number_bits(
957
+ isfinite(number) ? trunc(number) : 0.0
958
+ );
959
+ }
960
+ }
961
+ }
962
+ double timeout = INFINITY;
963
+ if (result.status == OSEO_STATUS_NORMAL) {
964
+ result = oseo_internal_to_number(
965
+ context,
966
+ atomics_argument(argument_count, arguments, 3u)
967
+ );
968
+ if (result.status == OSEO_STATUS_NORMAL) {
969
+ double number = number_value(result.value);
970
+ timeout = isnan(number) ? INFINITY
971
+ : number == -INFINITY ? 0.0
972
+ : fmax(number, 0.0);
973
+ }
974
+ }
975
+ if (result.status == OSEO_STATUS_NORMAL &&
976
+ !asynchronous && !context->agent_can_block) {
977
+ result = oseo_internal_throw_error(
978
+ context,
979
+ OSEO_ERROR_TYPE,
980
+ "The executing agent cannot suspend."
981
+ );
982
+ }
983
+ if (result.status != OSEO_STATUS_NORMAL) {
984
+ oseo_roots_release(context, &frame);
985
+ return result;
986
+ }
987
+ /*
988
+ * A shared buffer never detaches or shrinks, so the element the
989
+ * access validated is still inside the block after the conversions.
990
+ */
991
+ uint64_t mask = bigint ? UINT64_MAX : UINT64_C(0xffffffff);
992
+ uint64_t stored = atomics_read_raw(frame.slots[0], byte_index);
993
+ if ((stored & mask) != (expected & mask)) {
994
+ result = oseo_internal_ascii_string(context, "not-equal");
995
+ if (asynchronous && result.status == OSEO_STATUS_NORMAL) {
996
+ result = atomics_wait_result(context, false, result.value);
997
+ }
998
+ } else if (asynchronous && timeout == 0.0) {
999
+ result = oseo_internal_ascii_string(context, "timed-out");
1000
+ if (result.status == OSEO_STATUS_NORMAL) {
1001
+ result = atomics_wait_result(context, false, result.value);
1002
+ }
1003
+ } else if (!asynchronous) {
1004
+ result = atomics_suspend(
1005
+ context,
1006
+ array_buffer_object(frame.slots[1])->block,
1007
+ byte_index,
1008
+ timeout
1009
+ );
1010
+ } else {
1011
+ result = oseo_internal_promise_create(context);
1012
+ frame.slots[2] = result.value;
1013
+ if (result.status == OSEO_STATUS_NORMAL) {
1014
+ result = atomics_add_waiter(
1015
+ context,
1016
+ frame.slots[1],
1017
+ byte_index,
1018
+ frame.slots[2],
1019
+ timeout
1020
+ );
1021
+ }
1022
+ if (result.status == OSEO_STATUS_NORMAL) {
1023
+ result = atomics_wait_result(context, true, frame.slots[2]);
1024
+ }
1025
+ }
1026
+ result = atomics_offer_turn(context, &frame, result);
1027
+ oseo_roots_release(context, &frame);
1028
+ return result;
1029
+ }
1030
+
1031
+ /*
1032
+ * The processor hint that Atomics.pause executes: PAUSE on x86-64 and
1033
+ * YIELD on AArch64. The compiler's target descriptions record the same
1034
+ * choice as `atomicsPauseHint`, and any other architecture fails to build
1035
+ * rather than silently dropping the hint.
1036
+ */
1037
+ static void atomics_pause_hint(void) {
1038
+ #if defined(__x86_64__)
1039
+ __asm__ __volatile__("pause");
1040
+ #elif defined(__aarch64__)
1041
+ __asm__ __volatile__("yield");
1042
+ #else
1043
+ #error "Atomics.pause has no processor hint for this target."
1044
+ #endif
1045
+ }
1046
+
1047
+ /*
1048
+ * Atomics.pause(N). N must be undefined or an integral Number, and it is
1049
+ * never coerced. The call executes the processor hint once, whatever the
1050
+ * count, so a large count cannot stall the agent, and returns undefined.
1051
+ */
1052
+ static OseoResult atomics_pause(
1053
+ OseoContext *context,
1054
+ size_t argument_count,
1055
+ const OseoValue *arguments
1056
+ ) {
1057
+ OseoValue count = atomics_argument(argument_count, arguments, 0u);
1058
+ if (tag_of(count) != OSEO_TAG_UNDEFINED) {
1059
+ double number = is_number(count) ? number_value(count) : NAN;
1060
+ if (!isfinite(number) || trunc(number) != number) {
1061
+ return oseo_internal_throw_error(
1062
+ context,
1063
+ OSEO_ERROR_TYPE,
1064
+ "Atomics.pause requires an integral Number or undefined."
1065
+ );
1066
+ }
1067
+ }
1068
+ atomics_pause_hint();
1069
+ return normal(oseo_undefined());
1070
+ }
1071
+
1072
+ OseoResult oseo_internal_atomics_builtin_dispatch(
1073
+ OseoContext *context,
1074
+ size_t code_id,
1075
+ OseoValue callee,
1076
+ OseoValue receiver,
1077
+ size_t argument_count,
1078
+ const OseoValue *arguments,
1079
+ OseoValue new_target
1080
+ ) {
1081
+ (void)callee;
1082
+ (void)receiver;
1083
+ if (code_id < OSEO_ATOMICS_FUNCTION_CODE_ID_FIRST ||
1084
+ code_id > OSEO_ATOMICS_FUNCTION_CODE_ID_LAST) {
1085
+ return oseo_unknown_function(context, code_id);
1086
+ }
1087
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1088
+ return oseo_internal_throw_error(
1089
+ context,
1090
+ OSEO_ERROR_TYPE,
1091
+ "Atomics function is not a constructor."
1092
+ );
1093
+ }
1094
+ OseoAtomicsOperation operation = (OseoAtomicsOperation)(
1095
+ OSEO_ATOMICS_FUNCTION_CODE_ID_LAST - code_id
1096
+ );
1097
+ switch (operation) {
1098
+ case OSEO_ATOMICS_COMPARE_EXCHANGE:
1099
+ return atomics_compare_exchange(context, argument_count, arguments);
1100
+ case OSEO_ATOMICS_IS_LOCK_FREE:
1101
+ return atomics_is_lock_free(context, argument_count, arguments);
1102
+ case OSEO_ATOMICS_NOTIFY:
1103
+ return atomics_notify(context, argument_count, arguments);
1104
+ case OSEO_ATOMICS_WAIT:
1105
+ return atomics_wait(context, false, argument_count, arguments);
1106
+ case OSEO_ATOMICS_WAIT_ASYNC:
1107
+ return atomics_wait(context, true, argument_count, arguments);
1108
+ case OSEO_ATOMICS_PAUSE:
1109
+ return atomics_pause(context, argument_count, arguments);
1110
+ default:
1111
+ return atomics_read_modify_write(
1112
+ context,
1113
+ operation,
1114
+ argument_count,
1115
+ arguments
1116
+ );
1117
+ }
1118
+ }
1119
+
1120
+ static OseoResult create_atomics_function(
1121
+ OseoContext *context,
1122
+ size_t operation
1123
+ ) {
1124
+ const OseoAtomicsFunction *entry = &atomics_functions[operation];
1125
+ size_t name_length = strlen(entry->name);
1126
+ uint16_t units[16];
1127
+ if (name_length > sizeof(units) / sizeof(*units)) {
1128
+ return failure(context, "OSEO2001", "Built-in name is too long.");
1129
+ }
1130
+ for (size_t index = 0u; index < name_length; index += 1u) {
1131
+ units[index] = (uint16_t)(unsigned char)entry->name[index];
1132
+ }
1133
+ OseoValue environment = oseo_undefined();
1134
+ OseoRootFrame frame = {NULL, &environment, 1u};
1135
+ oseo_roots_push(context, &frame);
1136
+ OseoResult result = oseo_environment_create(context, 0u);
1137
+ environment = result.value;
1138
+ if (result.status == OSEO_STATUS_NORMAL) {
1139
+ result = oseo_function_create(
1140
+ context,
1141
+ OSEO_ATOMICS_FUNCTION_CODE_ID_LAST - operation,
1142
+ environment,
1143
+ units,
1144
+ name_length,
1145
+ entry->length,
1146
+ OSEO_FUNCTION_INTERNAL,
1147
+ oseo_undefined(),
1148
+ oseo_undefined(),
1149
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1150
+ );
1151
+ }
1152
+ oseo_roots_pop(context, &frame);
1153
+ return result;
1154
+ }
1155
+
1156
+ static OseoResult define_atomics_property(
1157
+ OseoContext *context,
1158
+ OseoValue object,
1159
+ const char *name,
1160
+ OseoValue value
1161
+ ) {
1162
+ OseoValue slots[3] = {object, value, oseo_undefined()};
1163
+ OseoRootFrame frame = {NULL, slots, 3u};
1164
+ oseo_roots_push(context, &frame);
1165
+ OseoResult result = oseo_internal_ascii_string(context, name);
1166
+ slots[2] = result.value;
1167
+ if (result.status == OSEO_STATUS_NORMAL) {
1168
+ result = oseo_object_define(
1169
+ context,
1170
+ slots[0],
1171
+ slots[2],
1172
+ slots[1],
1173
+ (OseoPropertyAttributes){true, false, true, false}
1174
+ );
1175
+ }
1176
+ oseo_roots_pop(context, &frame);
1177
+ return result;
1178
+ }
1179
+
1180
+ OseoResult oseo_internal_atomics_intrinsic(OseoContext *context) {
1181
+ OseoValue *slot = &context->realm->intrinsics[OSEO_INTRINSIC_ATOMICS];
1182
+ if (is_object(*slot)) return normal(*slot);
1183
+ size_t entry_allocations = context->allocations;
1184
+ OseoRootFrame frame = {NULL, NULL, 0u};
1185
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1186
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1187
+ result = oseo_internal_intrinsic(
1188
+ context,
1189
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1190
+ );
1191
+ frame.slots[0] = result.value;
1192
+ if (result.status == OSEO_STATUS_NORMAL) {
1193
+ result = oseo_object_create(context, frame.slots[0]);
1194
+ frame.slots[0] = result.value;
1195
+ }
1196
+ for (size_t index = 0u;
1197
+ result.status == OSEO_STATUS_NORMAL &&
1198
+ index < OSEO_ATOMICS_OPERATION_COUNT;
1199
+ index += 1u) {
1200
+ result = create_atomics_function(context, index);
1201
+ frame.slots[1] = result.value;
1202
+ if (result.status != OSEO_STATUS_NORMAL) break;
1203
+ result = define_atomics_property(
1204
+ context,
1205
+ frame.slots[0],
1206
+ atomics_functions[index].name,
1207
+ frame.slots[1]
1208
+ );
1209
+ }
1210
+ if (result.status == OSEO_STATUS_NORMAL) {
1211
+ result = oseo_internal_well_known_symbol(
1212
+ context,
1213
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1214
+ );
1215
+ frame.slots[1] = result.value;
1216
+ }
1217
+ if (result.status == OSEO_STATUS_NORMAL) {
1218
+ result = oseo_internal_ascii_string(context, "Atomics");
1219
+ frame.slots[2] = result.value;
1220
+ }
1221
+ if (result.status == OSEO_STATUS_NORMAL) {
1222
+ result = oseo_object_define(
1223
+ context,
1224
+ frame.slots[0],
1225
+ frame.slots[1],
1226
+ frame.slots[2],
1227
+ (OseoPropertyAttributes){true, false, false, false}
1228
+ );
1229
+ }
1230
+ if (result.status == OSEO_STATUS_NORMAL) {
1231
+ *slot = frame.slots[0];
1232
+ if (context->observe_specialization) {
1233
+ context->allocations = entry_allocations;
1234
+ }
1235
+ }
1236
+ oseo_roots_release(context, &frame);
1237
+ return result.status == OSEO_STATUS_NORMAL ? normal(*slot) : result;
1238
+ }
1239
+
1240
+ OseoResult oseo_internal_install_atomics_global(
1241
+ OseoContext *context,
1242
+ OseoValue global
1243
+ ) {
1244
+ OseoValue slots[2] = {global, oseo_undefined()};
1245
+ OseoRootFrame frame = {NULL, slots, 2u};
1246
+ oseo_roots_push(context, &frame);
1247
+ OseoResult result = oseo_internal_atomics_intrinsic(context);
1248
+ slots[1] = result.value;
1249
+ if (result.status == OSEO_STATUS_NORMAL) {
1250
+ result = define_atomics_property(
1251
+ context,
1252
+ slots[0],
1253
+ "Atomics",
1254
+ slots[1]
1255
+ );
1256
+ }
1257
+ oseo_roots_pop(context, &frame);
1258
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1259
+ }