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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -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 +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1748 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ #include "runtime_internal.h"
29
+
30
+ #include <math.h>
31
+ #include <stdlib.h>
32
+ #include <string.h>
33
+
34
+ /*
35
+ * The ArrayBuffer and SharedArrayBuffer constructors, the Data Blocks
36
+ * their instances own, the prototype accessors, resize, grow, transfer,
37
+ * transferToFixedLength, slice, and the species accessors. Both buffer
38
+ * kinds share one record and one allocation path; `shared` is the
39
+ * IsSharedArrayBuffer distinction every brand check below reads.
40
+ */
41
+
42
+ /*
43
+ * The largest Data Block this runtime creates, in bytes. CreateByteDataBlock
44
+ * throws a RangeError when a block of the requested size cannot be created
45
+ * and leaves the threshold to the implementation. Stating it here keeps the
46
+ * boundary the same on every supported target instead of inheriting whatever
47
+ * size the host allocator happens to refuse.
48
+ */
49
+ #define OSEO_ARRAY_BUFFER_DATA_LIMIT 8589934592.0
50
+
51
+ /*
52
+ * Converts one admitted integer index to a host size. The caller has
53
+ * already run ToIndex, so `length` is an integer in [0, 2^53 - 1]; this
54
+ * additionally rejects a request past the Data Block limit or past what
55
+ * a host size_t can express.
56
+ */
57
+ static bool array_buffer_size(double length, size_t *size) {
58
+ if (!(length >= 0.0)) return false;
59
+ if (length > OSEO_ARRAY_BUFFER_DATA_LIMIT) return false;
60
+ if (length > (double)SIZE_MAX) return false;
61
+ *size = (size_t)length;
62
+ return true;
63
+ }
64
+
65
+ /* ToIndex(value) over a length, reported as the integer it admits. */
66
+ static OseoResult array_buffer_to_index(
67
+ OseoContext *context,
68
+ OseoValue value,
69
+ double *index
70
+ ) {
71
+ return oseo_internal_to_index(
72
+ context,
73
+ value,
74
+ "ArrayBuffer length is outside the admitted index range.",
75
+ index
76
+ );
77
+ }
78
+
79
+ /*
80
+ * RequireInternalSlot(this, [[ArrayBufferData]]) followed by the
81
+ * IsSharedArrayBuffer rejection every ArrayBuffer.prototype member
82
+ * performs. Both are the same TypeError, so one check reports them.
83
+ */
84
+ static OseoResult array_buffer_receiver(
85
+ OseoContext *context,
86
+ OseoValue receiver
87
+ ) {
88
+ if (!is_array_buffer(receiver) || array_buffer_object(receiver)->shared) {
89
+ return oseo_internal_throw_error(
90
+ context,
91
+ OSEO_ERROR_TYPE,
92
+ "ArrayBuffer method receiver is not an ArrayBuffer."
93
+ );
94
+ }
95
+ return normal(receiver);
96
+ }
97
+
98
+ void oseo_internal_array_buffer_release(OseoHeapObject *object) {
99
+ OseoArrayBuffer *buffer = (OseoArrayBuffer *)object;
100
+ if (buffer->block != NULL) {
101
+ oseo_internal_shared_block_release(buffer->block);
102
+ buffer->block = NULL;
103
+ } else {
104
+ free(buffer->data);
105
+ }
106
+ buffer->data = NULL;
107
+ buffer->byte_length = 0u;
108
+ buffer->detached = true;
109
+ }
110
+
111
+ void oseo_internal_shared_block_retain(OseoSharedBlock *block) {
112
+ atomic_fetch_add_explicit(&block->references, 1u, memory_order_relaxed);
113
+ }
114
+
115
+ void oseo_internal_shared_block_release(OseoSharedBlock *block) {
116
+ if (atomic_fetch_sub_explicit(
117
+ &block->references,
118
+ 1u,
119
+ memory_order_acq_rel
120
+ ) == 1u) {
121
+ free(block);
122
+ }
123
+ }
124
+
125
+ /*
126
+ * Initializes one fresh buffer record around `prototype`, with no block.
127
+ * The caller publishes it.
128
+ */
129
+ static void array_buffer_initialize(
130
+ OseoContext *context,
131
+ OseoArrayBuffer *buffer,
132
+ OseoValue prototype
133
+ ) {
134
+ buffer->ordinary.prototype = prototype;
135
+ buffer->ordinary.properties = NULL;
136
+ buffer->ordinary.property_capacity = 0u;
137
+ buffer->ordinary.property_count = 0u;
138
+ oseo_internal_init_dense_index(&buffer->ordinary);
139
+ buffer->ordinary.private_elements = NULL;
140
+ buffer->ordinary.private_element_capacity = 0u;
141
+ buffer->ordinary.private_element_count = 0u;
142
+ buffer->ordinary.shape_id = context->next_shape_id;
143
+ context->next_shape_id += 1u;
144
+ buffer->ordinary.array_length = 0u;
145
+ buffer->ordinary.dictionary = false;
146
+ buffer->ordinary.length_writable = false;
147
+ buffer->ordinary.extensible = true;
148
+ buffer->ordinary.module_namespace = false;
149
+ buffer->ordinary.immutable_prototype = false;
150
+ buffer->ordinary.global_object = false;
151
+ buffer->ordinary.error_data = false;
152
+ buffer->ordinary.number_data = false;
153
+ buffer->ordinary.number_value = oseo_undefined();
154
+ buffer->ordinary.primitive_data = false;
155
+ buffer->ordinary.primitive_value = oseo_undefined();
156
+ buffer->ordinary.primitive_wrapper_methods_initialized = false;
157
+ buffer->ordinary.virtual_string_iterator = false;
158
+ buffer->ordinary.virtual_string_iterator_configurable = false;
159
+ buffer->ordinary.virtual_string_iterator_enumerable = false;
160
+ buffer->ordinary.virtual_string_iterator_writable = false;
161
+ buffer->ordinary.iterator_kind = OSEO_ITERATOR_NONE;
162
+ buffer->ordinary.iterator_target = oseo_undefined();
163
+ buffer->ordinary.iterator_index = 0u;
164
+ buffer->ordinary.regexp_string_iterator = false;
165
+ buffer->ordinary.regexp_iterator_regexp = oseo_undefined();
166
+ buffer->ordinary.regexp_iterator_subject = oseo_undefined();
167
+ buffer->ordinary.regexp_iterator_global = false;
168
+ buffer->ordinary.regexp_iterator_unicode = false;
169
+ buffer->ordinary.regexp_iterator_complete = false;
170
+ buffer->ordinary.async_from_sync = false;
171
+ buffer->ordinary.async_sync_iterator = oseo_undefined();
172
+ buffer->ordinary.wrap_for_valid_iterator = false;
173
+ buffer->ordinary.wrapped_iterator = oseo_undefined();
174
+ buffer->ordinary.wrapped_next = oseo_undefined();
175
+ buffer->ordinary.generator = NULL;
176
+ buffer->ordinary.arguments_object = false;
177
+ buffer->ordinary.mapped_arguments = false;
178
+ buffer->data = NULL;
179
+ buffer->byte_length = 0u;
180
+ buffer->max_byte_length = 0u;
181
+ buffer->resizable = false;
182
+ buffer->detached = false;
183
+ buffer->shared = false;
184
+ buffer->block = NULL;
185
+ }
186
+
187
+ /*
188
+ * AllocateArrayBuffer(constructor, byteLength, maxByteLength). The
189
+ * object exists before its Data Block does, so a block the host refuses
190
+ * leaves an already-created instance behind exactly as the specification
191
+ * describes. A resizable buffer reserves its whole maximum up front, so
192
+ * `resize` never moves the block and no pointer into it can go stale.
193
+ * AllocateSharedArrayBuffer takes the same steps, and its Shared Data
194
+ * Block carries a reference count and the length every agent reads, so
195
+ * even an empty shared buffer has a block to name its identity.
196
+ */
197
+ static OseoResult array_buffer_allocate(
198
+ OseoContext *context,
199
+ OseoValue prototype,
200
+ double byte_length,
201
+ double max_byte_length,
202
+ bool resizable,
203
+ bool shared
204
+ ) {
205
+ if (resizable && byte_length > max_byte_length) {
206
+ return oseo_internal_throw_error(
207
+ context,
208
+ OSEO_ERROR_RANGE,
209
+ "ArrayBuffer length exceeds its maximum byte length."
210
+ );
211
+ }
212
+ double requested = resizable ? max_byte_length : byte_length;
213
+ size_t allocation = 0u;
214
+ size_t length = 0u;
215
+ size_t maximum = 0u;
216
+ if (!array_buffer_size(requested, &allocation) ||
217
+ !array_buffer_size(byte_length, &length) ||
218
+ (resizable && !array_buffer_size(max_byte_length, &maximum))) {
219
+ return oseo_internal_throw_error(
220
+ context,
221
+ OSEO_ERROR_RANGE,
222
+ "ArrayBuffer data block is too large."
223
+ );
224
+ }
225
+ OseoValue slot = prototype;
226
+ OseoRootFrame frame = {NULL, &slot, 1u};
227
+ oseo_roots_push(context, &frame);
228
+ OseoArrayBuffer *buffer =
229
+ oseo_internal_allocate_heap_bytes(context, sizeof(*buffer));
230
+ if (buffer == NULL) {
231
+ oseo_roots_pop(context, &frame);
232
+ return failure(
233
+ context,
234
+ "OSEO2001",
235
+ "ArrayBuffer allocation failed."
236
+ );
237
+ }
238
+ array_buffer_initialize(context, buffer, slot);
239
+ buffer->max_byte_length = maximum;
240
+ buffer->resizable = resizable;
241
+ buffer->shared = shared;
242
+ OseoResult published = oseo_internal_publish_heap(
243
+ context,
244
+ &buffer->ordinary.header,
245
+ OSEO_HEAP_ARRAY_BUFFER
246
+ );
247
+ oseo_roots_pop(context, &frame);
248
+ if (published.status != OSEO_STATUS_NORMAL) return published;
249
+ if (allocation == 0u && !shared) return published;
250
+ if (shared && allocation > SIZE_MAX - sizeof(OseoSharedBlock)) {
251
+ return oseo_internal_throw_error(
252
+ context,
253
+ OSEO_ERROR_RANGE,
254
+ "ArrayBuffer data block is too large."
255
+ );
256
+ }
257
+ OseoValue created = published.value;
258
+ OseoRootFrame created_frame = {NULL, &created, 1u};
259
+ oseo_roots_push(context, &created_frame);
260
+ /* A safepoint, so the block is requested while the instance is
261
+ * rooted and the record is reacquired from that root afterwards. */
262
+ void *block = oseo_internal_allocate_heap_bytes(
263
+ context,
264
+ shared ? sizeof(OseoSharedBlock) + allocation : allocation
265
+ );
266
+ oseo_roots_pop(context, &created_frame);
267
+ if (block == NULL) {
268
+ return oseo_internal_throw_error(
269
+ context,
270
+ OSEO_ERROR_RANGE,
271
+ "ArrayBuffer data block allocation failed."
272
+ );
273
+ }
274
+ buffer = array_buffer_object(created);
275
+ if (shared) {
276
+ OseoSharedBlock *shared_block = block;
277
+ atomic_init(&shared_block->references, 1u);
278
+ shared_block->byte_length = length;
279
+ shared_block->max_byte_length = maximum;
280
+ shared_block->growable = resizable;
281
+ memset(shared_block->bytes, 0, allocation);
282
+ buffer->block = shared_block;
283
+ buffer->data = allocation == 0u ? NULL : shared_block->bytes;
284
+ } else {
285
+ memset(block, 0, allocation);
286
+ buffer->data = block;
287
+ }
288
+ buffer->byte_length = length;
289
+ return normal(created);
290
+ }
291
+
292
+ OseoResult oseo_internal_shared_array_buffer_from_block(
293
+ OseoContext *context,
294
+ OseoSharedBlock *block
295
+ ) {
296
+ OseoResult result = oseo_internal_intrinsic(
297
+ context,
298
+ OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_PROTOTYPE
299
+ );
300
+ if (result.status != OSEO_STATUS_NORMAL) return result;
301
+ OseoValue slot = result.value;
302
+ OseoRootFrame frame = {NULL, &slot, 1u};
303
+ oseo_roots_push(context, &frame);
304
+ OseoArrayBuffer *buffer =
305
+ oseo_internal_allocate_heap_bytes(context, sizeof(*buffer));
306
+ if (buffer == NULL) {
307
+ oseo_roots_pop(context, &frame);
308
+ return failure(
309
+ context,
310
+ "OSEO2001",
311
+ "SharedArrayBuffer allocation failed."
312
+ );
313
+ }
314
+ array_buffer_initialize(context, buffer, slot);
315
+ buffer->max_byte_length = block->max_byte_length;
316
+ buffer->resizable = block->growable;
317
+ buffer->shared = true;
318
+ buffer->byte_length = block->byte_length;
319
+ uint8_t *data = block->growable
320
+ ? (block->max_byte_length == 0u ? NULL : block->bytes)
321
+ : (block->byte_length == 0u ? NULL : block->bytes);
322
+ result = oseo_internal_publish_heap(
323
+ context,
324
+ &buffer->ordinary.header,
325
+ OSEO_HEAP_ARRAY_BUFFER
326
+ );
327
+ oseo_roots_pop(context, &frame);
328
+ /* The reference is taken only once the record is published, because
329
+ * a refused publication frees the record without releasing it. */
330
+ if (result.status == OSEO_STATUS_NORMAL) {
331
+ oseo_internal_shared_block_retain(block);
332
+ buffer->block = block;
333
+ buffer->data = data;
334
+ }
335
+ return result;
336
+ }
337
+
338
+ /* One fresh %ArrayBuffer%-prototyped buffer, which is what
339
+ * AllocateArrayBuffer(%ArrayBuffer%, ...) produces for transfer and for
340
+ * the default species path. */
341
+ OseoResult oseo_internal_array_buffer_create(
342
+ OseoContext *context,
343
+ double byte_length,
344
+ double max_byte_length,
345
+ bool resizable
346
+ ) {
347
+ OseoResult prototype = oseo_internal_intrinsic(
348
+ context,
349
+ OSEO_INTRINSIC_ARRAY_BUFFER_PROTOTYPE
350
+ );
351
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
352
+ return array_buffer_allocate(
353
+ context,
354
+ prototype.value,
355
+ byte_length,
356
+ max_byte_length,
357
+ resizable,
358
+ false
359
+ );
360
+ }
361
+
362
+ /*
363
+ * OrdinaryCreateFromConstructor(newTarget, fallback), where the fallback
364
+ * is %ArrayBuffer.prototype% or %SharedArrayBuffer.prototype%.
365
+ * The `prototype` read is the specified Get, so a new target whose
366
+ * property is an accessor runs it, and a non-object result falls back to
367
+ * the realm prototype.
368
+ */
369
+ static OseoResult array_buffer_prototype_from_target(
370
+ OseoContext *context,
371
+ OseoValue new_target,
372
+ OseoIntrinsic fallback,
373
+ OseoValue *prototype
374
+ ) {
375
+ OseoRootFrame frame = {NULL, NULL, 0u};
376
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
377
+ if (result.status != OSEO_STATUS_NORMAL) return result;
378
+ frame.slots[0] = new_target;
379
+ result = oseo_internal_ascii_string(context, "prototype");
380
+ frame.slots[1] = result.value;
381
+ if (result.status == OSEO_STATUS_NORMAL) {
382
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
383
+ frame.slots[1] = result.value;
384
+ }
385
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
386
+ result = oseo_internal_constructor_realm_default(
387
+ context,
388
+ frame.slots[0],
389
+ fallback
390
+ );
391
+ frame.slots[1] = result.value;
392
+ }
393
+ if (result.status == OSEO_STATUS_NORMAL) *prototype = frame.slots[1];
394
+ oseo_roots_release(context, &frame);
395
+ return result;
396
+ }
397
+
398
+ /* GetArrayBufferMaxByteLengthOption(options). */
399
+ static OseoResult array_buffer_max_option(
400
+ OseoContext *context,
401
+ OseoValue options,
402
+ double *maximum,
403
+ bool *present
404
+ ) {
405
+ *present = false;
406
+ if (!is_object(options)) return normal(oseo_undefined());
407
+ OseoRootFrame frame = {NULL, NULL, 0u};
408
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
409
+ if (result.status != OSEO_STATUS_NORMAL) return result;
410
+ frame.slots[0] = options;
411
+ result = oseo_internal_ascii_string(context, "maxByteLength");
412
+ frame.slots[1] = result.value;
413
+ if (result.status == OSEO_STATUS_NORMAL) {
414
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
415
+ frame.slots[1] = result.value;
416
+ }
417
+ if (result.status == OSEO_STATUS_NORMAL &&
418
+ tag_of(frame.slots[1]) != OSEO_TAG_UNDEFINED) {
419
+ result = array_buffer_to_index(context, frame.slots[1], maximum);
420
+ if (result.status == OSEO_STATUS_NORMAL) *present = true;
421
+ }
422
+ oseo_roots_release(context, &frame);
423
+ return result;
424
+ }
425
+
426
+ /*
427
+ * The ArrayBuffer and SharedArrayBuffer constructors. They differ only in
428
+ * the fallback prototype and in the kind of Data Block they allocate, and
429
+ * AllocateArrayBuffer and AllocateSharedArrayBuffer order their length
430
+ * check, prototype read, and block creation identically.
431
+ */
432
+ static OseoResult array_buffer_construct(
433
+ OseoContext *context,
434
+ size_t argument_count,
435
+ const OseoValue *arguments,
436
+ OseoValue new_target,
437
+ bool shared
438
+ ) {
439
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
440
+ return oseo_internal_throw_error(
441
+ context,
442
+ OSEO_ERROR_TYPE,
443
+ shared
444
+ ? "SharedArrayBuffer requires new."
445
+ : "ArrayBuffer requires new."
446
+ );
447
+ }
448
+ OseoRootFrame frame = {NULL, NULL, 0u};
449
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
450
+ if (result.status != OSEO_STATUS_NORMAL) return result;
451
+ frame.slots[0] = new_target;
452
+ frame.slots[1] = argument_count == 0u ? oseo_undefined() : arguments[0];
453
+ frame.slots[2] = argument_count < 2u ? oseo_undefined() : arguments[1];
454
+ double byte_length = 0.0;
455
+ result = array_buffer_to_index(context, frame.slots[1], &byte_length);
456
+ double maximum = 0.0;
457
+ bool resizable = false;
458
+ if (result.status == OSEO_STATUS_NORMAL) {
459
+ result = array_buffer_max_option(
460
+ context,
461
+ frame.slots[2],
462
+ &maximum,
463
+ &resizable
464
+ );
465
+ }
466
+ /* AllocateArrayBuffer compares the two lengths before it creates the
467
+ * instance, so an over-long request never runs the new target's
468
+ * `prototype` getter. */
469
+ if (result.status == OSEO_STATUS_NORMAL && resizable &&
470
+ byte_length > maximum) {
471
+ result = oseo_internal_throw_error(
472
+ context,
473
+ OSEO_ERROR_RANGE,
474
+ "ArrayBuffer length exceeds its maximum byte length."
475
+ );
476
+ }
477
+ if (result.status == OSEO_STATUS_NORMAL) {
478
+ result = array_buffer_prototype_from_target(
479
+ context,
480
+ frame.slots[0],
481
+ shared
482
+ ? OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_PROTOTYPE
483
+ : OSEO_INTRINSIC_ARRAY_BUFFER_PROTOTYPE,
484
+ &frame.slots[1]
485
+ );
486
+ }
487
+ if (result.status == OSEO_STATUS_NORMAL) {
488
+ result = array_buffer_allocate(
489
+ context,
490
+ frame.slots[1],
491
+ byte_length,
492
+ maximum,
493
+ resizable,
494
+ shared
495
+ );
496
+ }
497
+ oseo_roots_release(context, &frame);
498
+ return result;
499
+ }
500
+
501
+ /*
502
+ * ArrayBuffer.isView(arg). DataView and TypedArray are the admitted values
503
+ * that carry [[ViewedArrayBuffer]].
504
+ */
505
+ static OseoResult array_buffer_is_view(
506
+ size_t argument_count,
507
+ const OseoValue *arguments
508
+ ) {
509
+ if (argument_count == 0u) return normal(oseo_boolean(false));
510
+ return normal(oseo_boolean(
511
+ is_data_view(arguments[0]) || is_typed_array(arguments[0])
512
+ ));
513
+ }
514
+
515
+ static OseoResult array_buffer_byte_length(
516
+ OseoContext *context,
517
+ OseoValue receiver
518
+ ) {
519
+ OseoResult checked = array_buffer_receiver(context, receiver);
520
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
521
+ const OseoArrayBuffer *buffer = array_buffer_object(receiver);
522
+ if (buffer->detached) return normal(oseo_number(0.0));
523
+ return normal(oseo_number((double)buffer->byte_length));
524
+ }
525
+
526
+ static OseoResult array_buffer_max_byte_length(
527
+ OseoContext *context,
528
+ OseoValue receiver
529
+ ) {
530
+ OseoResult checked = array_buffer_receiver(context, receiver);
531
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
532
+ const OseoArrayBuffer *buffer = array_buffer_object(receiver);
533
+ if (buffer->detached) return normal(oseo_number(0.0));
534
+ return normal(oseo_number(
535
+ (double)(buffer->resizable ? buffer->max_byte_length
536
+ : buffer->byte_length)
537
+ ));
538
+ }
539
+
540
+ static OseoResult array_buffer_resizable(
541
+ OseoContext *context,
542
+ OseoValue receiver
543
+ ) {
544
+ OseoResult checked = array_buffer_receiver(context, receiver);
545
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
546
+ return normal(oseo_boolean(array_buffer_object(receiver)->resizable));
547
+ }
548
+
549
+ static OseoResult array_buffer_detached(
550
+ OseoContext *context,
551
+ OseoValue receiver
552
+ ) {
553
+ OseoResult checked = array_buffer_receiver(context, receiver);
554
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
555
+ return normal(oseo_boolean(array_buffer_object(receiver)->detached));
556
+ }
557
+
558
+ /*
559
+ * ArrayBuffer.prototype.resize(newLength). The resizability brand is
560
+ * checked before the length conversion, the detached state after it, so
561
+ * an argument whose conversion detaches the buffer still observes the
562
+ * specified TypeError. The reserved block never moves, and the bytes
563
+ * outside the new length are cleared so a later grow cannot reveal what
564
+ * a shrink discarded.
565
+ */
566
+ static OseoResult array_buffer_resize(
567
+ OseoContext *context,
568
+ OseoValue receiver,
569
+ size_t argument_count,
570
+ const OseoValue *arguments
571
+ ) {
572
+ OseoResult checked = array_buffer_receiver(context, receiver);
573
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
574
+ if (!array_buffer_object(receiver)->resizable) {
575
+ return oseo_internal_throw_error(
576
+ context,
577
+ OSEO_ERROR_TYPE,
578
+ "ArrayBuffer.prototype.resize needs a resizable ArrayBuffer."
579
+ );
580
+ }
581
+ OseoValue slot = receiver;
582
+ OseoRootFrame frame = {NULL, &slot, 1u};
583
+ oseo_roots_push(context, &frame);
584
+ double requested = 0.0;
585
+ OseoResult result = array_buffer_to_index(
586
+ context,
587
+ argument_count == 0u ? oseo_undefined() : arguments[0],
588
+ &requested
589
+ );
590
+ /* The conversion is a safepoint, so the record is reacquired from
591
+ * the rooted receiver rather than from a pointer taken before it. */
592
+ OseoArrayBuffer *buffer = array_buffer_object(slot);
593
+ size_t length = 0u;
594
+ if (result.status == OSEO_STATUS_NORMAL && buffer->detached) {
595
+ result = oseo_internal_throw_error(
596
+ context,
597
+ OSEO_ERROR_TYPE,
598
+ "ArrayBuffer.prototype.resize needs an attached ArrayBuffer."
599
+ );
600
+ } else if (result.status == OSEO_STATUS_NORMAL &&
601
+ (!array_buffer_size(requested, &length) ||
602
+ length > buffer->max_byte_length)) {
603
+ result = oseo_internal_throw_error(
604
+ context,
605
+ OSEO_ERROR_RANGE,
606
+ "ArrayBuffer resize length exceeds its maximum byte length."
607
+ );
608
+ } else if (result.status == OSEO_STATUS_NORMAL) {
609
+ buffer = array_buffer_object(slot);
610
+ size_t previous = buffer->byte_length;
611
+ size_t lower = length < previous ? length : previous;
612
+ size_t upper = length < previous ? previous : length;
613
+ if (upper > lower && buffer->data != NULL) {
614
+ memset(buffer->data + lower, 0, upper - lower);
615
+ }
616
+ buffer->byte_length = length;
617
+ result = normal(oseo_undefined());
618
+ }
619
+ oseo_roots_pop(context, &frame);
620
+ return result;
621
+ }
622
+
623
+ /*
624
+ * ArrayBufferCopyAndDetach(this, newLength, preserveResizability). The
625
+ * destination owns a second Data Block rather than the source's, so the
626
+ * source is detached only after the copy succeeded and no block is ever
627
+ * reachable from two buffers.
628
+ */
629
+ static OseoResult array_buffer_copy_and_detach(
630
+ OseoContext *context,
631
+ OseoValue receiver,
632
+ size_t argument_count,
633
+ const OseoValue *arguments,
634
+ bool preserve_resizability
635
+ ) {
636
+ OseoResult checked = array_buffer_receiver(context, receiver);
637
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
638
+ OseoValue slots[2] = {receiver, oseo_undefined()};
639
+ OseoRootFrame frame = {NULL, slots, 2u};
640
+ oseo_roots_push(context, &frame);
641
+ OseoValue requested = argument_count == 0u
642
+ ? oseo_undefined()
643
+ : arguments[0];
644
+ double length = (double)array_buffer_object(slots[0])->byte_length;
645
+ OseoResult result = normal(oseo_undefined());
646
+ if (tag_of(requested) != OSEO_TAG_UNDEFINED) {
647
+ result = array_buffer_to_index(context, requested, &length);
648
+ }
649
+ if (result.status != OSEO_STATUS_NORMAL) {
650
+ oseo_roots_pop(context, &frame);
651
+ return result;
652
+ }
653
+ const OseoArrayBuffer *source = array_buffer_object(slots[0]);
654
+ if (source->detached) {
655
+ oseo_roots_pop(context, &frame);
656
+ return oseo_internal_throw_error(
657
+ context,
658
+ OSEO_ERROR_TYPE,
659
+ "ArrayBuffer transfer needs an attached ArrayBuffer."
660
+ );
661
+ }
662
+ bool resizable = preserve_resizability && source->resizable;
663
+ double maximum = resizable ? (double)source->max_byte_length : 0.0;
664
+ result = oseo_internal_array_buffer_create(
665
+ context,
666
+ length,
667
+ maximum,
668
+ resizable
669
+ );
670
+ slots[1] = result.value;
671
+ if (result.status == OSEO_STATUS_NORMAL) {
672
+ /* Both records are reacquired after the allocation safepoint. */
673
+ OseoArrayBuffer *from = array_buffer_object(slots[0]);
674
+ OseoArrayBuffer *to = array_buffer_object(slots[1]);
675
+ size_t copied = to->byte_length < from->byte_length
676
+ ? to->byte_length
677
+ : from->byte_length;
678
+ if (copied > 0u && from->data != NULL && to->data != NULL) {
679
+ memcpy(to->data, from->data, copied);
680
+ }
681
+ oseo_internal_array_buffer_release(&from->ordinary.header);
682
+ /* A detached buffer keeps its resizability brand, so only the
683
+ * block, the length, and the detached flag change. */
684
+ result = normal(slots[1]);
685
+ }
686
+ oseo_roots_pop(context, &frame);
687
+ return result;
688
+ }
689
+
690
+ static OseoResult array_buffer_slice(
691
+ OseoContext *context,
692
+ OseoValue receiver,
693
+ size_t argument_count,
694
+ const OseoValue *arguments,
695
+ bool shared
696
+ );
697
+
698
+ /*
699
+ * RequireInternalSlot(this, [[ArrayBufferData]]) followed by the
700
+ * IsSharedArrayBuffer requirement of every SharedArrayBuffer.prototype
701
+ * member.
702
+ */
703
+ static OseoResult shared_array_buffer_receiver(
704
+ OseoContext *context,
705
+ OseoValue receiver
706
+ ) {
707
+ if (!is_array_buffer(receiver) ||
708
+ !array_buffer_object(receiver)->shared) {
709
+ return oseo_internal_throw_error(
710
+ context,
711
+ OSEO_ERROR_TYPE,
712
+ "SharedArrayBuffer method receiver is not a SharedArrayBuffer."
713
+ );
714
+ }
715
+ return normal(receiver);
716
+ }
717
+
718
+ /*
719
+ * SharedArrayBuffer.prototype.grow(newLength). Only a growable shared
720
+ * buffer carries [[ArrayBufferMaxByteLength]] together with a shared
721
+ * block, so both brand checks throw before the length converts. Agents of
722
+ * one cluster take turns on one executing thread, and the conversion is
723
+ * the last point that can hand the turn to another agent, so the
724
+ * compare-and-exchange loop the specification describes never observes a
725
+ * racing grow and reduces to one comparison against the block's current
726
+ * length, which is also where another agent's grow is visible. The block
727
+ * was reserved at its maximum and cleared at creation, so the grown bytes
728
+ * already read zero.
729
+ */
730
+ static OseoResult shared_array_buffer_grow(
731
+ OseoContext *context,
732
+ OseoValue receiver,
733
+ size_t argument_count,
734
+ const OseoValue *arguments
735
+ ) {
736
+ if (!is_array_buffer(receiver) ||
737
+ !array_buffer_object(receiver)->shared ||
738
+ !array_buffer_object(receiver)->resizable) {
739
+ return oseo_internal_throw_error(
740
+ context,
741
+ OSEO_ERROR_TYPE,
742
+ "SharedArrayBuffer.prototype.grow needs a growable "
743
+ "SharedArrayBuffer."
744
+ );
745
+ }
746
+ OseoValue slot = receiver;
747
+ OseoRootFrame frame = {NULL, &slot, 1u};
748
+ oseo_roots_push(context, &frame);
749
+ double requested = 0.0;
750
+ OseoResult result = oseo_internal_to_index(
751
+ context,
752
+ argument_count == 0u ? oseo_undefined() : arguments[0],
753
+ "SharedArrayBuffer length is outside the admitted index range.",
754
+ &requested
755
+ );
756
+ if (result.status == OSEO_STATUS_NORMAL) {
757
+ OseoArrayBuffer *buffer = array_buffer_object(slot);
758
+ size_t current = array_buffer_current_length(buffer);
759
+ size_t length = 0u;
760
+ if (requested == (double)current) {
761
+ result = normal(oseo_undefined());
762
+ } else if (requested < (double)current ||
763
+ !array_buffer_size(requested, &length) ||
764
+ length > buffer->max_byte_length) {
765
+ result = oseo_internal_throw_error(
766
+ context,
767
+ OSEO_ERROR_RANGE,
768
+ "SharedArrayBuffer grow length is outside its current and "
769
+ "maximum byte lengths."
770
+ );
771
+ } else {
772
+ buffer->byte_length = length;
773
+ buffer->block->byte_length = length;
774
+ result = normal(oseo_undefined());
775
+ }
776
+ }
777
+ oseo_roots_pop(context, &frame);
778
+ return result;
779
+ }
780
+
781
+ /* The SharedArrayBuffer.prototype accessors and methods. */
782
+ static OseoResult shared_array_buffer_member(
783
+ OseoContext *context,
784
+ size_t code_id,
785
+ OseoValue receiver,
786
+ size_t argument_count,
787
+ const OseoValue *arguments
788
+ ) {
789
+ if (code_id == OSEO_SHARED_ARRAY_BUFFER_GROW_CODE_ID) {
790
+ return shared_array_buffer_grow(
791
+ context,
792
+ receiver,
793
+ argument_count,
794
+ arguments
795
+ );
796
+ }
797
+ if (code_id == OSEO_SHARED_ARRAY_BUFFER_SLICE_CODE_ID) {
798
+ return array_buffer_slice(
799
+ context,
800
+ receiver,
801
+ argument_count,
802
+ arguments,
803
+ true
804
+ );
805
+ }
806
+ OseoResult checked = shared_array_buffer_receiver(context, receiver);
807
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
808
+ const OseoArrayBuffer *buffer = array_buffer_object(receiver);
809
+ if (code_id == OSEO_SHARED_ARRAY_BUFFER_BYTE_LENGTH_CODE_ID) {
810
+ return normal(oseo_number((double)array_buffer_current_length(buffer)));
811
+ }
812
+ if (code_id == OSEO_SHARED_ARRAY_BUFFER_GROWABLE_CODE_ID) {
813
+ return normal(oseo_boolean(buffer->resizable));
814
+ }
815
+ if (code_id == OSEO_SHARED_ARRAY_BUFFER_MAX_BYTE_LENGTH_CODE_ID) {
816
+ return normal(oseo_number(
817
+ (double)(buffer->resizable ? buffer->max_byte_length
818
+ : array_buffer_current_length(buffer))
819
+ ));
820
+ }
821
+ return oseo_unknown_function(context, code_id);
822
+ }
823
+
824
+ /* SpeciesConstructor(O, defaultConstructor). */
825
+ static OseoResult array_buffer_species_constructor(
826
+ OseoContext *context,
827
+ OseoValue object,
828
+ OseoIntrinsic default_constructor
829
+ ) {
830
+ OseoRootFrame frame = {NULL, NULL, 0u};
831
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
832
+ if (result.status != OSEO_STATUS_NORMAL) return result;
833
+ frame.slots[0] = object;
834
+ result = oseo_internal_ascii_string(context, "constructor");
835
+ frame.slots[1] = result.value;
836
+ if (result.status == OSEO_STATUS_NORMAL) {
837
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
838
+ frame.slots[2] = result.value;
839
+ }
840
+ if (result.status == OSEO_STATUS_NORMAL &&
841
+ tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
842
+ result = oseo_internal_intrinsic(context, default_constructor);
843
+ oseo_roots_release(context, &frame);
844
+ return result;
845
+ }
846
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[2])) {
847
+ result = oseo_internal_throw_error(
848
+ context,
849
+ OSEO_ERROR_TYPE,
850
+ "The ArrayBuffer constructor property is not an object."
851
+ );
852
+ }
853
+ if (result.status == OSEO_STATUS_NORMAL) {
854
+ result = oseo_internal_well_known_symbol(
855
+ context,
856
+ OSEO_WELL_KNOWN_SPECIES
857
+ );
858
+ frame.slots[1] = result.value;
859
+ }
860
+ if (result.status == OSEO_STATUS_NORMAL) {
861
+ result = oseo_object_get(context, frame.slots[2], frame.slots[1]);
862
+ frame.slots[2] = result.value;
863
+ }
864
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(frame.slots[2])) {
865
+ result = oseo_internal_intrinsic(context, default_constructor);
866
+ oseo_roots_release(context, &frame);
867
+ return result;
868
+ }
869
+ if (result.status == OSEO_STATUS_NORMAL) {
870
+ result = function_is_constructible(frame.slots[2])
871
+ ? normal(frame.slots[2])
872
+ : oseo_internal_throw_error(
873
+ context,
874
+ OSEO_ERROR_TYPE,
875
+ "The ArrayBuffer species is not a constructor."
876
+ );
877
+ }
878
+ oseo_roots_release(context, &frame);
879
+ return result;
880
+ }
881
+
882
+ /* Construct(species, «newLength»). */
883
+ static OseoResult array_buffer_construct_species(
884
+ OseoContext *context,
885
+ OseoValue constructor,
886
+ double length
887
+ ) {
888
+ OseoRootFrame frame = {NULL, NULL, 0u};
889
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
890
+ if (result.status != OSEO_STATUS_NORMAL) return result;
891
+ frame.slots[0] = constructor;
892
+ frame.slots[1] = oseo_number(length);
893
+ result = oseo_function_prototype(context, frame.slots[0]);
894
+ if (result.status == OSEO_STATUS_NORMAL) {
895
+ result = oseo_internal_function_receiver(
896
+ context,
897
+ frame.slots[0],
898
+ result.value
899
+ );
900
+ frame.slots[2] = result.value;
901
+ }
902
+ if (result.status == OSEO_STATUS_NORMAL) {
903
+ result = oseo_call_function(
904
+ context,
905
+ frame.slots[0],
906
+ frame.slots[2],
907
+ 1u,
908
+ &frame.slots[1],
909
+ frame.slots[0]
910
+ );
911
+ }
912
+ if (result.status == OSEO_STATUS_NORMAL) {
913
+ result = oseo_constructor_result(
914
+ context,
915
+ result.value,
916
+ frame.slots[2]
917
+ );
918
+ }
919
+ oseo_roots_release(context, &frame);
920
+ return result;
921
+ }
922
+
923
+ /*
924
+ * ArrayBuffer.prototype.slice(start, end) and, when `shared` is true,
925
+ * SharedArrayBuffer.prototype.slice(start, end). The two algorithms share
926
+ * their bounds, species construction, and copy; they differ in the brand
927
+ * each requires of the receiver and of the species result, and a shared
928
+ * buffer is never detached, so the detachment checks cannot fire for it.
929
+ */
930
+ static OseoResult array_buffer_slice(
931
+ OseoContext *context,
932
+ OseoValue receiver,
933
+ size_t argument_count,
934
+ const OseoValue *arguments,
935
+ bool shared
936
+ ) {
937
+ OseoResult checked = shared
938
+ ? shared_array_buffer_receiver(context, receiver)
939
+ : array_buffer_receiver(context, receiver);
940
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
941
+ if (array_buffer_object(receiver)->detached) {
942
+ return oseo_internal_throw_error(
943
+ context,
944
+ OSEO_ERROR_TYPE,
945
+ "ArrayBuffer.prototype.slice needs an attached ArrayBuffer."
946
+ );
947
+ }
948
+ OseoValue slots[3] = {receiver, oseo_undefined(), oseo_undefined()};
949
+ OseoRootFrame frame = {NULL, slots, 3u};
950
+ oseo_roots_push(context, &frame);
951
+ double length = (double)array_buffer_current_length(
952
+ array_buffer_object(slots[0])
953
+ );
954
+ OseoResult result = oseo_internal_to_number(
955
+ context,
956
+ argument_count == 0u ? oseo_undefined() : arguments[0]
957
+ );
958
+ double first = 0.0;
959
+ if (result.status == OSEO_STATUS_NORMAL) {
960
+ double relative = number_value(result.value);
961
+ relative = isnan(relative) ? 0.0
962
+ : isfinite(relative) ? trunc(relative)
963
+ : relative;
964
+ first = relative < 0.0 ? (length + relative > 0.0 ? length + relative
965
+ : 0.0)
966
+ : (relative < length ? relative : length);
967
+ }
968
+ double final = length;
969
+ OseoValue end = argument_count < 2u ? oseo_undefined() : arguments[1];
970
+ if (result.status == OSEO_STATUS_NORMAL &&
971
+ tag_of(end) != OSEO_TAG_UNDEFINED) {
972
+ result = oseo_internal_to_number(context, end);
973
+ if (result.status == OSEO_STATUS_NORMAL) {
974
+ double relative = number_value(result.value);
975
+ relative = isnan(relative) ? 0.0
976
+ : isfinite(relative) ? trunc(relative)
977
+ : relative;
978
+ final = relative < 0.0
979
+ ? (length + relative > 0.0 ? length + relative : 0.0)
980
+ : (relative < length ? relative : length);
981
+ }
982
+ }
983
+ double new_length = final - first > 0.0 ? final - first : 0.0;
984
+ if (result.status == OSEO_STATUS_NORMAL) {
985
+ result = array_buffer_species_constructor(
986
+ context,
987
+ slots[0],
988
+ shared
989
+ ? OSEO_INTRINSIC_SHARED_ARRAY_BUFFER
990
+ : OSEO_INTRINSIC_ARRAY_BUFFER
991
+ );
992
+ slots[1] = result.value;
993
+ }
994
+ if (result.status == OSEO_STATUS_NORMAL) {
995
+ result = array_buffer_construct_species(
996
+ context,
997
+ slots[1],
998
+ new_length
999
+ );
1000
+ slots[2] = result.value;
1001
+ }
1002
+ if (result.status == OSEO_STATUS_NORMAL && !is_array_buffer(slots[2])) {
1003
+ result = oseo_internal_throw_error(
1004
+ context,
1005
+ OSEO_ERROR_TYPE,
1006
+ "The ArrayBuffer species did not return an ArrayBuffer."
1007
+ );
1008
+ }
1009
+ if (result.status == OSEO_STATUS_NORMAL &&
1010
+ array_buffer_object(slots[2])->shared != shared) {
1011
+ result = oseo_internal_throw_error(
1012
+ context,
1013
+ OSEO_ERROR_TYPE,
1014
+ shared
1015
+ ? "The SharedArrayBuffer species did not return a "
1016
+ "SharedArrayBuffer."
1017
+ : "The ArrayBuffer species returned a SharedArrayBuffer."
1018
+ );
1019
+ }
1020
+ if (result.status == OSEO_STATUS_NORMAL &&
1021
+ array_buffer_object(slots[2])->detached) {
1022
+ result = oseo_internal_throw_error(
1023
+ context,
1024
+ OSEO_ERROR_TYPE,
1025
+ "The ArrayBuffer species returned a detached ArrayBuffer."
1026
+ );
1027
+ }
1028
+ /* SharedArrayBuffer.prototype.slice compares [[ArrayBufferData]],
1029
+ * which two buffer objects of one agent share when one Shared Data
1030
+ * Block was broadcast to it twice. */
1031
+ if (result.status == OSEO_STATUS_NORMAL &&
1032
+ (slots[2] == slots[0] ||
1033
+ (shared &&
1034
+ array_buffer_object(slots[2])->block ==
1035
+ array_buffer_object(slots[0])->block))) {
1036
+ result = oseo_internal_throw_error(
1037
+ context,
1038
+ OSEO_ERROR_TYPE,
1039
+ "The ArrayBuffer species returned the source ArrayBuffer."
1040
+ );
1041
+ }
1042
+ if (result.status == OSEO_STATUS_NORMAL &&
1043
+ (double)array_buffer_current_length(array_buffer_object(slots[2])) <
1044
+ new_length) {
1045
+ result = oseo_internal_throw_error(
1046
+ context,
1047
+ OSEO_ERROR_TYPE,
1048
+ "The ArrayBuffer species returned a buffer that is too small."
1049
+ );
1050
+ }
1051
+ /* The species constructor is arbitrary code, so the source may have
1052
+ * been detached while it ran. */
1053
+ if (result.status == OSEO_STATUS_NORMAL &&
1054
+ array_buffer_object(slots[0])->detached) {
1055
+ result = oseo_internal_throw_error(
1056
+ context,
1057
+ OSEO_ERROR_TYPE,
1058
+ "ArrayBuffer.prototype.slice source became detached."
1059
+ );
1060
+ }
1061
+ if (result.status == OSEO_STATUS_NORMAL) {
1062
+ const OseoArrayBuffer *from = array_buffer_object(slots[0]);
1063
+ OseoArrayBuffer *to = array_buffer_object(slots[2]);
1064
+ size_t start = (size_t)first;
1065
+ size_t copied = (size_t)new_length;
1066
+ /*
1067
+ * The species constructor is arbitrary code, so it may have
1068
+ * shrunk a resizable source between the bounds and this copy.
1069
+ * The reservation keeps that range inside the allocated block
1070
+ * either way, and the clamp keeps the read in bounds without
1071
+ * depending on the reservation policy.
1072
+ */
1073
+ size_t available = array_buffer_current_length(from) > start
1074
+ ? array_buffer_current_length(from) - start
1075
+ : 0u;
1076
+ if (copied > available) copied = available;
1077
+ if (copied > 0u && from->data != NULL && to->data != NULL) {
1078
+ memcpy(to->data, from->data + start, copied);
1079
+ }
1080
+ result = normal(slots[2]);
1081
+ }
1082
+ oseo_roots_pop(context, &frame);
1083
+ return result;
1084
+ }
1085
+
1086
+ OseoResult oseo_internal_array_buffer_builtin_dispatch(
1087
+ OseoContext *context,
1088
+ size_t code_id,
1089
+ OseoValue callee,
1090
+ OseoValue receiver,
1091
+ size_t argument_count,
1092
+ const OseoValue *arguments,
1093
+ OseoValue new_target
1094
+ ) {
1095
+ (void)callee;
1096
+ if (code_id == OSEO_ARRAY_BUFFER_CONSTRUCTOR_CODE_ID ||
1097
+ code_id == OSEO_SHARED_ARRAY_BUFFER_CONSTRUCTOR_CODE_ID) {
1098
+ return array_buffer_construct(
1099
+ context,
1100
+ argument_count,
1101
+ arguments,
1102
+ new_target,
1103
+ code_id == OSEO_SHARED_ARRAY_BUFFER_CONSTRUCTOR_CODE_ID
1104
+ );
1105
+ }
1106
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1107
+ return oseo_internal_throw_error(
1108
+ context,
1109
+ OSEO_ERROR_TYPE,
1110
+ "ArrayBuffer method is not a constructor."
1111
+ );
1112
+ }
1113
+ if (code_id == OSEO_ARRAY_BUFFER_IS_VIEW_CODE_ID) {
1114
+ return array_buffer_is_view(argument_count, arguments);
1115
+ }
1116
+ if (code_id == OSEO_ARRAY_BUFFER_SPECIES_CODE_ID ||
1117
+ code_id == OSEO_SHARED_ARRAY_BUFFER_SPECIES_CODE_ID) {
1118
+ return normal(receiver);
1119
+ }
1120
+ if (code_id >= OSEO_SHARED_ARRAY_BUFFER_SLICE_CODE_ID &&
1121
+ code_id <= OSEO_SHARED_ARRAY_BUFFER_BYTE_LENGTH_CODE_ID) {
1122
+ return shared_array_buffer_member(
1123
+ context,
1124
+ code_id,
1125
+ receiver,
1126
+ argument_count,
1127
+ arguments
1128
+ );
1129
+ }
1130
+ if (code_id == OSEO_ARRAY_BUFFER_BYTE_LENGTH_CODE_ID) {
1131
+ return array_buffer_byte_length(context, receiver);
1132
+ }
1133
+ if (code_id == OSEO_ARRAY_BUFFER_DETACHED_CODE_ID) {
1134
+ return array_buffer_detached(context, receiver);
1135
+ }
1136
+ if (code_id == OSEO_ARRAY_BUFFER_MAX_BYTE_LENGTH_CODE_ID) {
1137
+ return array_buffer_max_byte_length(context, receiver);
1138
+ }
1139
+ if (code_id == OSEO_ARRAY_BUFFER_RESIZABLE_CODE_ID) {
1140
+ return array_buffer_resizable(context, receiver);
1141
+ }
1142
+ if (code_id == OSEO_ARRAY_BUFFER_RESIZE_CODE_ID) {
1143
+ return array_buffer_resize(
1144
+ context,
1145
+ receiver,
1146
+ argument_count,
1147
+ arguments
1148
+ );
1149
+ }
1150
+ if (code_id == OSEO_ARRAY_BUFFER_SLICE_CODE_ID) {
1151
+ return array_buffer_slice(
1152
+ context,
1153
+ receiver,
1154
+ argument_count,
1155
+ arguments,
1156
+ false
1157
+ );
1158
+ }
1159
+ if (code_id == OSEO_ARRAY_BUFFER_TRANSFER_CODE_ID) {
1160
+ return array_buffer_copy_and_detach(
1161
+ context,
1162
+ receiver,
1163
+ argument_count,
1164
+ arguments,
1165
+ true
1166
+ );
1167
+ }
1168
+ if (code_id == OSEO_ARRAY_BUFFER_TRANSFER_TO_FIXED_LENGTH_CODE_ID) {
1169
+ return array_buffer_copy_and_detach(
1170
+ context,
1171
+ receiver,
1172
+ argument_count,
1173
+ arguments,
1174
+ false
1175
+ );
1176
+ }
1177
+ return oseo_unknown_function(context, code_id);
1178
+ }
1179
+
1180
+ static OseoResult create_array_buffer_builtin(
1181
+ OseoContext *context,
1182
+ size_t code_id,
1183
+ const char *name,
1184
+ size_t length,
1185
+ OseoFunctionKind kind,
1186
+ OseoFunctionNamePrefix prefix
1187
+ ) {
1188
+ size_t name_length = strlen(name);
1189
+ uint16_t units[32];
1190
+ if (name_length > sizeof(units) / sizeof(*units)) {
1191
+ return failure(context, "OSEO2001", "Built-in name is too long.");
1192
+ }
1193
+ for (size_t index = 0u; index < name_length; index += 1u) {
1194
+ units[index] = (uint16_t)(unsigned char)name[index];
1195
+ }
1196
+ OseoValue environment = oseo_undefined();
1197
+ OseoRootFrame frame = {NULL, &environment, 1u};
1198
+ oseo_roots_push(context, &frame);
1199
+ OseoResult result = oseo_environment_create(context, 0u);
1200
+ environment = result.value;
1201
+ if (result.status == OSEO_STATUS_NORMAL) {
1202
+ result = oseo_function_create(
1203
+ context,
1204
+ code_id,
1205
+ environment,
1206
+ units,
1207
+ name_length,
1208
+ length,
1209
+ kind,
1210
+ oseo_undefined(),
1211
+ oseo_undefined(),
1212
+ prefix
1213
+ );
1214
+ }
1215
+ oseo_roots_pop(context, &frame);
1216
+ return result;
1217
+ }
1218
+
1219
+ static OseoResult define_array_buffer_property(
1220
+ OseoContext *context,
1221
+ OseoValue object,
1222
+ const char *name,
1223
+ OseoValue value,
1224
+ OseoPropertyAttributes attributes
1225
+ ) {
1226
+ OseoValue slots[3] = {object, value, oseo_undefined()};
1227
+ OseoRootFrame frame = {NULL, slots, 3u};
1228
+ oseo_roots_push(context, &frame);
1229
+ OseoResult result = oseo_internal_ascii_string(context, name);
1230
+ slots[2] = result.value;
1231
+ if (result.status == OSEO_STATUS_NORMAL) {
1232
+ result = oseo_object_define(
1233
+ context,
1234
+ slots[0],
1235
+ slots[2],
1236
+ slots[1],
1237
+ attributes
1238
+ );
1239
+ }
1240
+ oseo_roots_pop(context, &frame);
1241
+ return result;
1242
+ }
1243
+
1244
+ /* One getter-only own accessor of %ArrayBuffer.prototype%. */
1245
+ static OseoResult define_array_buffer_accessor(
1246
+ OseoContext *context,
1247
+ OseoValue object,
1248
+ const char *name,
1249
+ OseoValue getter
1250
+ ) {
1251
+ OseoValue slots[3] = {object, getter, oseo_undefined()};
1252
+ OseoRootFrame frame = {NULL, slots, 3u};
1253
+ oseo_roots_push(context, &frame);
1254
+ OseoResult result = oseo_internal_ascii_string(context, name);
1255
+ slots[2] = result.value;
1256
+ if (result.status == OSEO_STATUS_NORMAL) {
1257
+ result = oseo_object_define_accessor(
1258
+ context,
1259
+ slots[0],
1260
+ slots[2],
1261
+ slots[1],
1262
+ oseo_undefined(),
1263
+ true,
1264
+ false,
1265
+ (OseoPropertyAttributes){true, false, false, true}
1266
+ );
1267
+ }
1268
+ oseo_roots_pop(context, &frame);
1269
+ return result;
1270
+ }
1271
+
1272
+ /*
1273
+ * Materializes %ArrayBuffer%, %ArrayBuffer.prototype%, and every own
1274
+ * property ECMA-262 gives them.
1275
+ * `OSEO_INTRINSIC_ARRAY_BUFFER_SPECIES` is filled last, so it doubles as
1276
+ * the completion marker: a partially built cluster leaves it undefined
1277
+ * and the failure path clears every slot the attempt filled. The marker
1278
+ * holds the uninitialized sentinel while the attempt runs, so a
1279
+ * dependency that reentered the build reports that instead of splitting
1280
+ * the constructor and prototype identities across two attempts.
1281
+ */
1282
+ static OseoResult array_buffer_intrinsic_build(OseoContext *context) {
1283
+ OseoValue *marker =
1284
+ &context->realm->intrinsics[OSEO_INTRINSIC_ARRAY_BUFFER_SPECIES];
1285
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
1286
+ return failure(
1287
+ context,
1288
+ "OSEO2001",
1289
+ "The ArrayBuffer intrinsic cluster is already being built."
1290
+ );
1291
+ }
1292
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
1293
+ size_t entry_allocations = context->allocations;
1294
+ OseoRootFrame frame = {NULL, NULL, 0u};
1295
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
1296
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1297
+ *marker = oseo_uninitialized();
1298
+ result = oseo_internal_intrinsic(
1299
+ context,
1300
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1301
+ );
1302
+ frame.slots[0] = result.value;
1303
+ if (result.status == OSEO_STATUS_NORMAL) {
1304
+ result = oseo_object_create(context, frame.slots[0]);
1305
+ frame.slots[0] = result.value;
1306
+ }
1307
+ if (result.status == OSEO_STATUS_NORMAL) {
1308
+ context->realm->intrinsics[OSEO_INTRINSIC_ARRAY_BUFFER_PROTOTYPE] =
1309
+ frame.slots[0];
1310
+ result = create_array_buffer_builtin(
1311
+ context,
1312
+ OSEO_ARRAY_BUFFER_CONSTRUCTOR_CODE_ID,
1313
+ "ArrayBuffer",
1314
+ 1u,
1315
+ OSEO_FUNCTION_ORDINARY,
1316
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1317
+ );
1318
+ frame.slots[1] = result.value;
1319
+ }
1320
+ const OseoPropertyAttributes method = {true, false, true, false};
1321
+ if (result.status == OSEO_STATUS_NORMAL) {
1322
+ context->realm->intrinsics[OSEO_INTRINSIC_ARRAY_BUFFER] =
1323
+ frame.slots[1];
1324
+ OseoFunction *constructor = function_object(frame.slots[1]);
1325
+ constructor->prototype_object = frame.slots[0];
1326
+ constructor->prototype_writable = false;
1327
+ result = define_array_buffer_property(
1328
+ context,
1329
+ frame.slots[0],
1330
+ "constructor",
1331
+ frame.slots[1],
1332
+ method
1333
+ );
1334
+ }
1335
+ static const OseoIntrinsic accessor_intrinsics[] = {
1336
+ OSEO_INTRINSIC_ARRAY_BUFFER_BYTE_LENGTH,
1337
+ OSEO_INTRINSIC_ARRAY_BUFFER_DETACHED,
1338
+ OSEO_INTRINSIC_ARRAY_BUFFER_MAX_BYTE_LENGTH,
1339
+ OSEO_INTRINSIC_ARRAY_BUFFER_RESIZABLE,
1340
+ };
1341
+ static const size_t accessor_codes[] = {
1342
+ OSEO_ARRAY_BUFFER_BYTE_LENGTH_CODE_ID,
1343
+ OSEO_ARRAY_BUFFER_DETACHED_CODE_ID,
1344
+ OSEO_ARRAY_BUFFER_MAX_BYTE_LENGTH_CODE_ID,
1345
+ OSEO_ARRAY_BUFFER_RESIZABLE_CODE_ID,
1346
+ };
1347
+ static const char *const accessor_names[] = {
1348
+ "byteLength",
1349
+ "detached",
1350
+ "maxByteLength",
1351
+ "resizable",
1352
+ };
1353
+ for (size_t index = 0u;
1354
+ result.status == OSEO_STATUS_NORMAL && index < 4u;
1355
+ index += 1u) {
1356
+ result = create_array_buffer_builtin(
1357
+ context,
1358
+ accessor_codes[index],
1359
+ accessor_names[index],
1360
+ 0u,
1361
+ OSEO_FUNCTION_INTERNAL,
1362
+ OSEO_FUNCTION_NAME_PREFIX_GET
1363
+ );
1364
+ frame.slots[2] = result.value;
1365
+ if (result.status != OSEO_STATUS_NORMAL) break;
1366
+ context->realm->intrinsics[accessor_intrinsics[index]] = frame.slots[2];
1367
+ result = define_array_buffer_accessor(
1368
+ context,
1369
+ frame.slots[0],
1370
+ accessor_names[index],
1371
+ frame.slots[2]
1372
+ );
1373
+ }
1374
+ static const OseoIntrinsic method_intrinsics[] = {
1375
+ OSEO_INTRINSIC_ARRAY_BUFFER_RESIZE,
1376
+ OSEO_INTRINSIC_ARRAY_BUFFER_SLICE,
1377
+ OSEO_INTRINSIC_ARRAY_BUFFER_TRANSFER,
1378
+ OSEO_INTRINSIC_ARRAY_BUFFER_TRANSFER_TO_FIXED_LENGTH,
1379
+ };
1380
+ static const size_t method_codes[] = {
1381
+ OSEO_ARRAY_BUFFER_RESIZE_CODE_ID,
1382
+ OSEO_ARRAY_BUFFER_SLICE_CODE_ID,
1383
+ OSEO_ARRAY_BUFFER_TRANSFER_CODE_ID,
1384
+ OSEO_ARRAY_BUFFER_TRANSFER_TO_FIXED_LENGTH_CODE_ID,
1385
+ };
1386
+ static const char *const method_names[] = {
1387
+ "resize",
1388
+ "slice",
1389
+ "transfer",
1390
+ "transferToFixedLength",
1391
+ };
1392
+ static const size_t method_lengths[] = {1u, 2u, 0u, 0u};
1393
+ for (size_t index = 0u;
1394
+ result.status == OSEO_STATUS_NORMAL && index < 4u;
1395
+ index += 1u) {
1396
+ result = create_array_buffer_builtin(
1397
+ context,
1398
+ method_codes[index],
1399
+ method_names[index],
1400
+ method_lengths[index],
1401
+ OSEO_FUNCTION_INTERNAL,
1402
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1403
+ );
1404
+ frame.slots[2] = result.value;
1405
+ if (result.status != OSEO_STATUS_NORMAL) break;
1406
+ context->realm->intrinsics[method_intrinsics[index]] = frame.slots[2];
1407
+ result = define_array_buffer_property(
1408
+ context,
1409
+ frame.slots[0],
1410
+ method_names[index],
1411
+ frame.slots[2],
1412
+ method
1413
+ );
1414
+ }
1415
+ if (result.status == OSEO_STATUS_NORMAL) {
1416
+ result = oseo_internal_well_known_symbol(
1417
+ context,
1418
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1419
+ );
1420
+ frame.slots[3] = result.value;
1421
+ }
1422
+ if (result.status == OSEO_STATUS_NORMAL) {
1423
+ result = oseo_internal_ascii_string(context, "ArrayBuffer");
1424
+ frame.slots[4] = result.value;
1425
+ }
1426
+ if (result.status == OSEO_STATUS_NORMAL) {
1427
+ result = oseo_object_define(
1428
+ context,
1429
+ frame.slots[0],
1430
+ frame.slots[3],
1431
+ frame.slots[4],
1432
+ (OseoPropertyAttributes){true, false, false, false}
1433
+ );
1434
+ }
1435
+ if (result.status == OSEO_STATUS_NORMAL) {
1436
+ result = create_array_buffer_builtin(
1437
+ context,
1438
+ OSEO_ARRAY_BUFFER_IS_VIEW_CODE_ID,
1439
+ "isView",
1440
+ 1u,
1441
+ OSEO_FUNCTION_INTERNAL,
1442
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1443
+ );
1444
+ frame.slots[2] = result.value;
1445
+ }
1446
+ if (result.status == OSEO_STATUS_NORMAL) {
1447
+ context->realm->intrinsics[OSEO_INTRINSIC_ARRAY_BUFFER_IS_VIEW] =
1448
+ frame.slots[2];
1449
+ result = define_array_buffer_property(
1450
+ context,
1451
+ frame.slots[1],
1452
+ "isView",
1453
+ frame.slots[2],
1454
+ method
1455
+ );
1456
+ }
1457
+ if (result.status == OSEO_STATUS_NORMAL) {
1458
+ result = create_array_buffer_builtin(
1459
+ context,
1460
+ OSEO_ARRAY_BUFFER_SPECIES_CODE_ID,
1461
+ "[Symbol.species]",
1462
+ 0u,
1463
+ OSEO_FUNCTION_INTERNAL,
1464
+ OSEO_FUNCTION_NAME_PREFIX_GET
1465
+ );
1466
+ frame.slots[2] = result.value;
1467
+ }
1468
+ if (result.status == OSEO_STATUS_NORMAL) {
1469
+ result = oseo_internal_well_known_symbol(
1470
+ context,
1471
+ OSEO_WELL_KNOWN_SPECIES
1472
+ );
1473
+ frame.slots[3] = result.value;
1474
+ }
1475
+ if (result.status == OSEO_STATUS_NORMAL) {
1476
+ result = oseo_object_define_accessor(
1477
+ context,
1478
+ frame.slots[1],
1479
+ frame.slots[3],
1480
+ frame.slots[2],
1481
+ oseo_undefined(),
1482
+ true,
1483
+ false,
1484
+ (OseoPropertyAttributes){true, false, false, true}
1485
+ );
1486
+ }
1487
+ if (result.status != OSEO_STATUS_NORMAL) {
1488
+ for (size_t index = OSEO_INTRINSIC_ARRAY_BUFFER_PROTOTYPE;
1489
+ index <= OSEO_INTRINSIC_ARRAY_BUFFER_SPECIES;
1490
+ index += 1u) {
1491
+ context->realm->intrinsics[index] = oseo_undefined();
1492
+ }
1493
+ oseo_roots_release(context, &frame);
1494
+ return result;
1495
+ }
1496
+ OseoValue species = frame.slots[2];
1497
+ context->realm->intrinsics[OSEO_INTRINSIC_ARRAY_BUFFER_SPECIES] = species;
1498
+ if (context->observe_specialization) {
1499
+ context->allocations = entry_allocations;
1500
+ }
1501
+ oseo_roots_release(context, &frame);
1502
+ return normal(species);
1503
+ }
1504
+
1505
+ OseoResult oseo_internal_array_buffer_intrinsic(OseoContext *context) {
1506
+ OseoResult built = array_buffer_intrinsic_build(context);
1507
+ if (built.status != OSEO_STATUS_NORMAL) return built;
1508
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_ARRAY_BUFFER]);
1509
+ }
1510
+
1511
+ OseoResult oseo_internal_install_array_buffer_global(
1512
+ OseoContext *context,
1513
+ OseoValue global
1514
+ ) {
1515
+ OseoValue slots[2] = {global, oseo_undefined()};
1516
+ OseoRootFrame frame = {NULL, slots, 2u};
1517
+ oseo_roots_push(context, &frame);
1518
+ OseoResult result = oseo_internal_array_buffer_intrinsic(context);
1519
+ slots[1] = result.value;
1520
+ if (result.status == OSEO_STATUS_NORMAL) {
1521
+ result = define_array_buffer_property(
1522
+ context,
1523
+ slots[0],
1524
+ "ArrayBuffer",
1525
+ slots[1],
1526
+ (OseoPropertyAttributes){true, false, true, false}
1527
+ );
1528
+ }
1529
+ oseo_roots_pop(context, &frame);
1530
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1531
+ }
1532
+
1533
+ /*
1534
+ * Materializes %SharedArrayBuffer%, %SharedArrayBuffer.prototype%, and
1535
+ * every own property ECMA-262 gives them. The cluster follows the
1536
+ * ArrayBuffer build: `OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_SPECIES` is
1537
+ * filled last and doubles as the completion marker, holds the
1538
+ * uninitialized sentinel while the attempt runs, and a failed attempt
1539
+ * clears every slot it filled.
1540
+ */
1541
+ static OseoResult shared_array_buffer_intrinsic_build(OseoContext *context) {
1542
+ OseoValue *marker =
1543
+ &context->realm->intrinsics[OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_SPECIES];
1544
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
1545
+ return failure(
1546
+ context,
1547
+ "OSEO2001",
1548
+ "The SharedArrayBuffer intrinsic cluster is already being built."
1549
+ );
1550
+ }
1551
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
1552
+ size_t entry_allocations = context->allocations;
1553
+ OseoRootFrame frame = {NULL, NULL, 0u};
1554
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
1555
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1556
+ *marker = oseo_uninitialized();
1557
+ result = oseo_internal_intrinsic(
1558
+ context,
1559
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1560
+ );
1561
+ frame.slots[0] = result.value;
1562
+ if (result.status == OSEO_STATUS_NORMAL) {
1563
+ result = oseo_object_create(context, frame.slots[0]);
1564
+ frame.slots[0] = result.value;
1565
+ }
1566
+ if (result.status == OSEO_STATUS_NORMAL) {
1567
+ OseoValue *intrinsics = context->realm->intrinsics;
1568
+ intrinsics[OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_PROTOTYPE] =
1569
+ frame.slots[0];
1570
+ result = create_array_buffer_builtin(
1571
+ context,
1572
+ OSEO_SHARED_ARRAY_BUFFER_CONSTRUCTOR_CODE_ID,
1573
+ "SharedArrayBuffer",
1574
+ 1u,
1575
+ OSEO_FUNCTION_ORDINARY,
1576
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1577
+ );
1578
+ frame.slots[1] = result.value;
1579
+ }
1580
+ const OseoPropertyAttributes method = {true, false, true, false};
1581
+ if (result.status == OSEO_STATUS_NORMAL) {
1582
+ context->realm->intrinsics[OSEO_INTRINSIC_SHARED_ARRAY_BUFFER] =
1583
+ frame.slots[1];
1584
+ OseoFunction *constructor = function_object(frame.slots[1]);
1585
+ constructor->prototype_object = frame.slots[0];
1586
+ constructor->prototype_writable = false;
1587
+ result = define_array_buffer_property(
1588
+ context,
1589
+ frame.slots[0],
1590
+ "constructor",
1591
+ frame.slots[1],
1592
+ method
1593
+ );
1594
+ }
1595
+ static const size_t accessor_codes[] = {
1596
+ OSEO_SHARED_ARRAY_BUFFER_BYTE_LENGTH_CODE_ID,
1597
+ OSEO_SHARED_ARRAY_BUFFER_GROWABLE_CODE_ID,
1598
+ OSEO_SHARED_ARRAY_BUFFER_MAX_BYTE_LENGTH_CODE_ID,
1599
+ };
1600
+ static const char *const accessor_names[] = {
1601
+ "byteLength",
1602
+ "growable",
1603
+ "maxByteLength",
1604
+ };
1605
+ for (size_t index = 0u;
1606
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
1607
+ index += 1u) {
1608
+ result = create_array_buffer_builtin(
1609
+ context,
1610
+ accessor_codes[index],
1611
+ accessor_names[index],
1612
+ 0u,
1613
+ OSEO_FUNCTION_INTERNAL,
1614
+ OSEO_FUNCTION_NAME_PREFIX_GET
1615
+ );
1616
+ frame.slots[2] = result.value;
1617
+ if (result.status != OSEO_STATUS_NORMAL) break;
1618
+ result = define_array_buffer_accessor(
1619
+ context,
1620
+ frame.slots[0],
1621
+ accessor_names[index],
1622
+ frame.slots[2]
1623
+ );
1624
+ }
1625
+ static const size_t method_codes[] = {
1626
+ OSEO_SHARED_ARRAY_BUFFER_GROW_CODE_ID,
1627
+ OSEO_SHARED_ARRAY_BUFFER_SLICE_CODE_ID,
1628
+ };
1629
+ static const char *const method_names[] = {"grow", "slice"};
1630
+ static const size_t method_lengths[] = {1u, 2u};
1631
+ for (size_t index = 0u;
1632
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
1633
+ index += 1u) {
1634
+ result = create_array_buffer_builtin(
1635
+ context,
1636
+ method_codes[index],
1637
+ method_names[index],
1638
+ method_lengths[index],
1639
+ OSEO_FUNCTION_INTERNAL,
1640
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1641
+ );
1642
+ frame.slots[2] = result.value;
1643
+ if (result.status != OSEO_STATUS_NORMAL) break;
1644
+ result = define_array_buffer_property(
1645
+ context,
1646
+ frame.slots[0],
1647
+ method_names[index],
1648
+ frame.slots[2],
1649
+ method
1650
+ );
1651
+ }
1652
+ if (result.status == OSEO_STATUS_NORMAL) {
1653
+ result = oseo_internal_well_known_symbol(
1654
+ context,
1655
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1656
+ );
1657
+ frame.slots[3] = result.value;
1658
+ }
1659
+ if (result.status == OSEO_STATUS_NORMAL) {
1660
+ result = oseo_internal_ascii_string(context, "SharedArrayBuffer");
1661
+ frame.slots[4] = result.value;
1662
+ }
1663
+ if (result.status == OSEO_STATUS_NORMAL) {
1664
+ result = oseo_object_define(
1665
+ context,
1666
+ frame.slots[0],
1667
+ frame.slots[3],
1668
+ frame.slots[4],
1669
+ (OseoPropertyAttributes){true, false, false, false}
1670
+ );
1671
+ }
1672
+ if (result.status == OSEO_STATUS_NORMAL) {
1673
+ result = create_array_buffer_builtin(
1674
+ context,
1675
+ OSEO_SHARED_ARRAY_BUFFER_SPECIES_CODE_ID,
1676
+ "[Symbol.species]",
1677
+ 0u,
1678
+ OSEO_FUNCTION_INTERNAL,
1679
+ OSEO_FUNCTION_NAME_PREFIX_GET
1680
+ );
1681
+ frame.slots[2] = result.value;
1682
+ }
1683
+ if (result.status == OSEO_STATUS_NORMAL) {
1684
+ result = oseo_internal_well_known_symbol(
1685
+ context,
1686
+ OSEO_WELL_KNOWN_SPECIES
1687
+ );
1688
+ frame.slots[3] = result.value;
1689
+ }
1690
+ if (result.status == OSEO_STATUS_NORMAL) {
1691
+ result = oseo_object_define_accessor(
1692
+ context,
1693
+ frame.slots[1],
1694
+ frame.slots[3],
1695
+ frame.slots[2],
1696
+ oseo_undefined(),
1697
+ true,
1698
+ false,
1699
+ (OseoPropertyAttributes){true, false, false, true}
1700
+ );
1701
+ }
1702
+ if (result.status != OSEO_STATUS_NORMAL) {
1703
+ for (size_t index = OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_PROTOTYPE;
1704
+ index <= OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_SPECIES;
1705
+ index += 1u) {
1706
+ context->realm->intrinsics[index] = oseo_undefined();
1707
+ }
1708
+ oseo_roots_release(context, &frame);
1709
+ return result;
1710
+ }
1711
+ OseoValue species = frame.slots[2];
1712
+ context->realm->intrinsics[OSEO_INTRINSIC_SHARED_ARRAY_BUFFER_SPECIES] =
1713
+ species;
1714
+ if (context->observe_specialization) {
1715
+ context->allocations = entry_allocations;
1716
+ }
1717
+ oseo_roots_release(context, &frame);
1718
+ return normal(species);
1719
+ }
1720
+
1721
+ OseoResult oseo_internal_shared_array_buffer_intrinsic(OseoContext *context) {
1722
+ OseoResult built = shared_array_buffer_intrinsic_build(context);
1723
+ if (built.status != OSEO_STATUS_NORMAL) return built;
1724
+ const OseoValue *intrinsics = context->realm->intrinsics;
1725
+ return normal(intrinsics[OSEO_INTRINSIC_SHARED_ARRAY_BUFFER]);
1726
+ }
1727
+
1728
+ OseoResult oseo_internal_install_shared_array_buffer_global(
1729
+ OseoContext *context,
1730
+ OseoValue global
1731
+ ) {
1732
+ OseoValue slots[2] = {global, oseo_undefined()};
1733
+ OseoRootFrame frame = {NULL, slots, 2u};
1734
+ oseo_roots_push(context, &frame);
1735
+ OseoResult result = oseo_internal_shared_array_buffer_intrinsic(context);
1736
+ slots[1] = result.value;
1737
+ if (result.status == OSEO_STATUS_NORMAL) {
1738
+ result = define_array_buffer_property(
1739
+ context,
1740
+ slots[0],
1741
+ "SharedArrayBuffer",
1742
+ slots[1],
1743
+ (OseoPropertyAttributes){true, false, true, false}
1744
+ );
1745
+ }
1746
+ oseo_roots_pop(context, &frame);
1747
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1748
+ }