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