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