@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,423 @@
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 <stdlib.h>
31
+
32
+ /*
33
+ * Ordinary object creation and layout: the property vector and its
34
+ * growth, own-property lookup and removal, cell-backed property
35
+ * recognition, shape identifiers and the generated-code property
36
+ * caches, object coercibility checks, and [[SetPrototypeOf]].
37
+ */
38
+
39
+ static OseoResult type_error(OseoContext *context, const char *message) {
40
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
41
+ }
42
+
43
+ /*
44
+ * True when one own property of `object_value` keeps its value in the
45
+ * binding cell `stored` instead of the property slot. A namespace
46
+ * export, a realm global binding, and a mapped arguments index are
47
+ * views of a binding. Namespace metadata such as Symbol.toStringTag is
48
+ * a plain property and therefore does not satisfy the stored-cell
49
+ * check. Every read of a cell-backed property dereferences the cell,
50
+ * and every admitted write goes through it, so the property and its
51
+ * binding remain one storage location rather than two copies.
52
+ */
53
+ bool oseo_internal_cell_backed_property(
54
+ OseoValue object_value,
55
+ OseoValue stored
56
+ ) {
57
+ if (!is_object(object_value) || !is_cell(stored)) return false;
58
+ const OseoOrdinaryObject *object = ordinary_object(object_value);
59
+ return object->module_namespace || object->global_object ||
60
+ object->mapped_arguments;
61
+ }
62
+
63
+ size_t oseo_internal_own_property_index(
64
+ const OseoOrdinaryObject *object,
65
+ OseoValue key
66
+ ) {
67
+ /* Dense arrays commonly store index i in property slot i. Check the
68
+ * current key before using that slot: holes, deletion, named keys,
69
+ * and out-of-order insertion can all break the correspondence.
70
+ * No slot or pointer survives this call, and no safepoint occurs. */
71
+ uint32_t array_index;
72
+ if (object->header.kind == OSEO_HEAP_ARRAY &&
73
+ oseo_internal_array_index(key, &array_index)) {
74
+ /* Array exotic definitions keep every own index below length.
75
+ * An extension therefore has no own descriptor, even when named
76
+ * properties, holes, or out-of-order indices occupy the vector.
77
+ * Callers still check prototypes and all definition restrictions. */
78
+ if (array_index >= object->array_length) return SIZE_MAX;
79
+ if (array_index < object->property_count &&
80
+ oseo_internal_property_key_equal(
81
+ object->properties[array_index].key, key)) {
82
+ return array_index;
83
+ }
84
+ }
85
+ /* The dense run holds every own index, each at its base offset, so
86
+ * an index past the run is absent without a scan. */
87
+ if (object->dense_index_known &&
88
+ oseo_internal_array_index(key, &array_index)) {
89
+ if (array_index >= object->dense_index_count) return SIZE_MAX;
90
+ return object->dense_index_base + array_index;
91
+ }
92
+ for (size_t index = 0u; index < object->property_count; index += 1u) {
93
+ if (oseo_internal_property_key_equal(
94
+ object->properties[index].key, key)) {
95
+ return index;
96
+ }
97
+ }
98
+ return SIZE_MAX;
99
+ }
100
+
101
+ bool oseo_internal_remove_property(OseoOrdinaryObject *object, size_t index) {
102
+ if (!object->properties[index].attributes.configurable) return false;
103
+ for (size_t next = index + 1u; next < object->property_count; next += 1u) {
104
+ object->properties[next - 1u] = object->properties[next];
105
+ }
106
+ object->property_count -= 1u;
107
+ /* A removal before the run shifts it down whole, and removing its
108
+ * last index shortens it; any other index would split it. */
109
+ if (object->dense_index_known && object->dense_index_count > 0u) {
110
+ size_t end = object->dense_index_base + object->dense_index_count;
111
+ if (index < object->dense_index_base) {
112
+ object->dense_index_base -= 1u;
113
+ } else if (index + 1u == end) {
114
+ object->dense_index_count -= 1u;
115
+ if (object->dense_index_count == 0u) object->dense_index_base = 0u;
116
+ } else if (index < end) {
117
+ object->dense_index_known = false;
118
+ }
119
+ }
120
+ return true;
121
+ }
122
+
123
+ void oseo_internal_note_property_inserted(
124
+ OseoOrdinaryObject *object,
125
+ size_t slot
126
+ ) {
127
+ if (!object->dense_index_known) return;
128
+ size_t end = object->dense_index_base + object->dense_index_count;
129
+ uint32_t array_index;
130
+ if (oseo_internal_array_index(object->properties[slot].key, &array_index)) {
131
+ /* Index 0 starts the run wherever it lands, since no other index
132
+ * is stored yet; the next index right after the run extends it. */
133
+ if (object->dense_index_count == 0u && array_index == 0u) {
134
+ object->dense_index_base = slot;
135
+ object->dense_index_count = 1u;
136
+ } else if (object->dense_index_count > 0u && slot == end &&
137
+ array_index == object->dense_index_count) {
138
+ object->dense_index_count += 1u;
139
+ } else {
140
+ object->dense_index_known = false;
141
+ }
142
+ } else if (object->dense_index_count > 0u) {
143
+ /* A named key before the run shifts it up whole. */
144
+ if (slot <= object->dense_index_base) {
145
+ object->dense_index_base += 1u;
146
+ } else if (slot < end) {
147
+ object->dense_index_known = false;
148
+ }
149
+ }
150
+ }
151
+
152
+ static OseoResult object_create(
153
+ OseoContext *context,
154
+ OseoValue prototype
155
+ ) {
156
+ if (tag_of(prototype) != OSEO_TAG_NULL && !is_object(prototype)) {
157
+ return type_error(
158
+ context,
159
+ "Object prototype must be an object or null."
160
+ );
161
+ }
162
+ OseoOrdinaryObject *object =
163
+ oseo_internal_allocate_heap_bytes(context, sizeof(*object));
164
+ if (object == NULL) {
165
+ return failure(context, "OSEO2001", "Object allocation failed.");
166
+ }
167
+ object->prototype = prototype;
168
+ object->properties = NULL;
169
+ object->property_capacity = 0u;
170
+ object->property_count = 0u;
171
+ oseo_internal_init_dense_index(object);
172
+ object->private_elements = NULL;
173
+ object->private_element_capacity = 0u;
174
+ object->private_element_count = 0u;
175
+ object->shape_id = context->next_shape_id;
176
+ context->next_shape_id += 1u;
177
+ object->array_length = 0u;
178
+ object->dictionary = false;
179
+ object->length_writable = false;
180
+ object->extensible = true;
181
+ object->module_namespace = false;
182
+ object->immutable_prototype = false;
183
+ object->global_object = false;
184
+ object->error_data = false;
185
+ object->number_data = false;
186
+ object->number_value = oseo_undefined();
187
+ object->primitive_data = false;
188
+ object->primitive_value = oseo_undefined();
189
+ object->primitive_wrapper_methods_initialized = false;
190
+ object->virtual_string_iterator = false;
191
+ object->virtual_string_iterator_configurable = false;
192
+ object->virtual_string_iterator_enumerable = false;
193
+ object->virtual_string_iterator_writable = false;
194
+ object->iterator_kind = OSEO_ITERATOR_NONE;
195
+ object->iterator_target = oseo_undefined();
196
+ object->iterator_index = 0u;
197
+ object->regexp_string_iterator = false;
198
+ object->regexp_iterator_regexp = oseo_undefined();
199
+ object->regexp_iterator_subject = oseo_undefined();
200
+ object->regexp_iterator_global = false;
201
+ object->regexp_iterator_unicode = false;
202
+ object->regexp_iterator_complete = false;
203
+ object->async_from_sync = false;
204
+ object->async_sync_iterator = oseo_undefined();
205
+ object->wrap_for_valid_iterator = false;
206
+ object->wrapped_iterator = oseo_undefined();
207
+ object->wrapped_next = oseo_undefined();
208
+ object->generator = NULL;
209
+ object->arguments_object = false;
210
+ object->mapped_arguments = false;
211
+ return oseo_internal_publish_heap(
212
+ context, &object->header, OSEO_HEAP_OBJECT);
213
+ }
214
+
215
+ OseoResult oseo_object_create(OseoContext *context, OseoValue prototype) {
216
+ return object_create(context, prototype);
217
+ }
218
+
219
+ OseoResult oseo_object_literal_create(OseoContext *context) {
220
+ OseoResult prototype = oseo_internal_intrinsic(
221
+ context,
222
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
223
+ );
224
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
225
+ return object_create(context, prototype.value);
226
+ }
227
+
228
+ OseoResult oseo_require_object_coercible(
229
+ OseoContext *context,
230
+ OseoValue value
231
+ ) {
232
+ if (is_nullish(value)) {
233
+ return type_error(context, "Cannot destructure a nullish value.");
234
+ }
235
+ return normal(value);
236
+ }
237
+
238
+ OseoResult oseo_require_delete_object_coercible(
239
+ OseoContext *context,
240
+ OseoValue value
241
+ ) {
242
+ if (is_nullish(value)) {
243
+ return type_error(
244
+ context,
245
+ "Cannot delete properties of a nullish value."
246
+ );
247
+ }
248
+ return normal(value);
249
+ }
250
+
251
+ OseoResult oseo_internal_grow_properties(
252
+ OseoContext *context,
253
+ OseoValue object_value
254
+ ) {
255
+ OseoOrdinaryObject *object = ordinary_object(object_value);
256
+ if (object->property_count < object->property_capacity) {
257
+ return normal(object_value);
258
+ }
259
+ size_t capacity = object->property_capacity == 0u
260
+ ? 4u
261
+ : object->property_capacity * 2u;
262
+ if (capacity < object->property_capacity ||
263
+ capacity > SIZE_MAX / sizeof(OseoProperty)) {
264
+ return failure(context, "OSEO2001", "Property storage is too large.");
265
+ }
266
+ if (context->collect_every_safepoint) oseo_collect(context);
267
+ object = ordinary_object(object_value);
268
+ context->allocation_attempts += 1u;
269
+ if (context->fail_allocation_at != 0u &&
270
+ context->allocation_attempts == context->fail_allocation_at) {
271
+ return failure(context, "OSEO2001", "Property allocation failed.");
272
+ }
273
+ OseoProperty *properties = malloc(capacity * sizeof(*properties));
274
+ if (properties == NULL) {
275
+ return failure(context, "OSEO2001", "Property allocation failed.");
276
+ }
277
+ if (object->property_count > 0u) {
278
+ memcpy(
279
+ properties,
280
+ object->properties,
281
+ object->property_count * sizeof(*properties)
282
+ );
283
+ }
284
+ free(object->properties);
285
+ object->properties = properties;
286
+ object->property_capacity = capacity;
287
+ return normal(object_value);
288
+ }
289
+
290
+ bool oseo_value_is_object(OseoValue value) {
291
+ return is_object(value) || is_promise(value);
292
+ }
293
+
294
+ bool oseo_property_cache_matches(
295
+ OseoValue object_value,
296
+ const OseoPropertyCache *cache
297
+ ) {
298
+ if (!is_object(object_value)) return false;
299
+ OseoOrdinaryObject *object = ordinary_object(object_value);
300
+ return !object->dictionary && cache->shape_id != 0u &&
301
+ cache->shape_id == object->shape_id &&
302
+ cache->slot < object->property_count;
303
+ }
304
+
305
+ OseoValue oseo_property_cache_load(
306
+ OseoValue object_value,
307
+ const OseoPropertyCache *cache
308
+ ) {
309
+ if (!is_object(object_value)) return oseo_undefined();
310
+ OseoOrdinaryObject *object = ordinary_object(object_value);
311
+ if (cache->slot >= object->property_count) return oseo_undefined();
312
+ return object->properties[cache->slot].value;
313
+ }
314
+
315
+ void oseo_property_cache_update(
316
+ OseoValue object_value,
317
+ OseoValue key,
318
+ OseoPropertyCache *cache
319
+ ) {
320
+ if (!is_object(object_value)) {
321
+ cache->shape_id = 0u;
322
+ cache->slot = 0u;
323
+ return;
324
+ }
325
+ OseoOrdinaryObject *object = ordinary_object(object_value);
326
+ size_t slot = oseo_internal_own_property_index(object, key);
327
+ /* An accessor slot must always dispatch through the generic getter
328
+ * call, never the direct fixed-slot load. A cell-backed slot holds
329
+ * the binding cell rather than the value, so it is excluded for the
330
+ * same reason: the fixed-slot load would hand generated code the
331
+ * cell. Excluding the slot rather than the whole object keeps the
332
+ * cache working for the global object's ordinary properties. */
333
+ if (!object->dictionary && slot != SIZE_MAX &&
334
+ !object->properties[slot].attributes.accessor &&
335
+ !oseo_internal_cell_backed_property(
336
+ object_value, object->properties[slot].value)) {
337
+ cache->shape_id = object->shape_id;
338
+ cache->slot = slot;
339
+ } else {
340
+ cache->shape_id = 0u;
341
+ cache->slot = 0u;
342
+ }
343
+ }
344
+
345
+ OseoResult oseo_internal_set_prototype_reported(
346
+ OseoContext *context,
347
+ OseoValue object_value,
348
+ OseoValue prototype,
349
+ const char **refusal
350
+ ) {
351
+ *refusal = NULL;
352
+ if (is_proxy(object_value)) {
353
+ return oseo_internal_proxy_set_prototype(
354
+ context, object_value, prototype, refusal);
355
+ }
356
+ OseoOrdinaryObject *object = ordinary_object(object_value);
357
+ if (object->module_namespace || object->immutable_prototype) {
358
+ /* SetImmutablePrototype (10.4.7.1) accepts only the prototype
359
+ * the object already has. Both a module namespace and
360
+ * %Object.prototype% start with a null one. */
361
+ if (!oseo_internal_same_value(prototype, object->prototype)) {
362
+ *refusal = object->module_namespace
363
+ ? "Cannot change a namespace prototype."
364
+ : "Cannot change an immutable prototype.";
365
+ }
366
+ return normal(object_value);
367
+ }
368
+ if (object->prototype == prototype) {
369
+ return normal(object_value);
370
+ }
371
+ if (!object->extensible) {
372
+ *refusal = "Cannot change a non-extensible object's prototype.";
373
+ return normal(object_value);
374
+ }
375
+ OseoValue current = prototype;
376
+ while (is_object(current)) {
377
+ if (current == object_value) {
378
+ *refusal = "Cyclic prototype chains are not allowed.";
379
+ return normal(object_value);
380
+ }
381
+ current = ordinary_object(current)->prototype;
382
+ }
383
+ object->prototype = prototype;
384
+ object->dictionary = true;
385
+ object->shape_id = context->next_shape_id;
386
+ context->next_shape_id += 1u;
387
+ return normal(object_value);
388
+ }
389
+
390
+ OseoResult oseo_object_set_prototype(
391
+ OseoContext *context,
392
+ OseoValue object_value,
393
+ OseoValue prototype
394
+ ) {
395
+ if (!is_object(object_value) ||
396
+ (tag_of(prototype) != OSEO_TAG_NULL && !is_object(prototype))) {
397
+ return type_error(
398
+ context,
399
+ "Object.setPrototypeOf requires an object prototype."
400
+ );
401
+ }
402
+ const char *refusal = NULL;
403
+ OseoResult result = oseo_internal_set_prototype_reported(
404
+ context,
405
+ object_value,
406
+ prototype,
407
+ &refusal
408
+ );
409
+ if (result.status != OSEO_STATUS_NORMAL || refusal == NULL) return result;
410
+ return type_error(context, refusal);
411
+ }
412
+
413
+ OseoResult oseo_object_literal_set_prototype(
414
+ OseoContext *context,
415
+ OseoValue object_value,
416
+ OseoValue prototype
417
+ ) {
418
+ if (!is_object(object_value) || tag_of(prototype) == OSEO_TAG_NULL ||
419
+ is_object(prototype)) {
420
+ return oseo_object_set_prototype(context, object_value, prototype);
421
+ }
422
+ return normal(object_value);
423
+ }