@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,379 @@
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
+ OseoResult oseo_internal_arguments_builtin_dispatch(
31
+ OseoContext *context,
32
+ size_t code_id,
33
+ OseoValue callee,
34
+ OseoValue receiver,
35
+ size_t argument_count,
36
+ const OseoValue *arguments,
37
+ OseoValue new_target
38
+ ) {
39
+ (void)callee;
40
+ (void)receiver;
41
+ (void)argument_count;
42
+ (void)arguments;
43
+ (void)new_target;
44
+ if (code_id != OSEO_THROW_TYPE_ERROR_CODE_ID) {
45
+ return oseo_unknown_function(context, code_id);
46
+ }
47
+ /* %ThrowTypeError%. Both accessor halves throw regardless of their
48
+ * receiver or argument. */
49
+ return oseo_internal_throw_error(
50
+ context,
51
+ OSEO_ERROR_TYPE,
52
+ "'callee' is not available on an unmapped arguments object."
53
+ );
54
+ }
55
+
56
+ /*
57
+ * The arguments exotic objects: the unmapped object 10.2.4 creates, the
58
+ * mapped object 10.4.4 creates from a simple parameter list, the
59
+ * `@@iterator` both shapes define, and the realm's single
60
+ * %ThrowTypeError% intrinsic their `callee` accessor reports.
61
+ */
62
+
63
+ /*
64
+ * %ThrowTypeError% (10.2.4.1). One anonymous, zero-parameter function
65
+ * per realm, so an unmapped arguments object's `callee` accessor reports
66
+ * the same [[Get]] and [[Set]] identity every time it is inspected. Its
67
+ * own body only throws, and the caller's arguments never reach it.
68
+ *
69
+ * 10.2.4.1 also hardens the intrinsic: its `length` and `name` are
70
+ * non-writable and non-configurable, and the function itself is
71
+ * non-extensible, so admitted reflection can neither reshape it nor
72
+ * replace its prototype. `oseo_function_create` leaves both properties
73
+ * configurable, so they are redefined before extensibility is dropped.
74
+ * The `prototype` object every internal function in this profile still
75
+ * carries is a separate boundary the intrinsics stream owns.
76
+ */
77
+ OseoResult oseo_internal_throw_type_error_function(OseoContext *context) {
78
+ OseoValue *cache =
79
+ &context->realm->intrinsics[OSEO_INTRINSIC_THROW_TYPE_ERROR];
80
+ if (tag_of(*cache) != OSEO_TAG_UNDEFINED) {
81
+ return normal(*cache);
82
+ }
83
+ OseoRootFrame frame = {NULL, NULL, 0u};
84
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
85
+ if (result.status != OSEO_STATUS_NORMAL) return result;
86
+ result = oseo_environment_create(context, 0u);
87
+ frame.slots[0] = result.value;
88
+ if (result.status == OSEO_STATUS_NORMAL) {
89
+ result = oseo_function_create(
90
+ context,
91
+ OSEO_THROW_TYPE_ERROR_CODE_ID,
92
+ frame.slots[0],
93
+ NULL,
94
+ 0u,
95
+ 0u,
96
+ OSEO_FUNCTION_INTERNAL,
97
+ oseo_undefined(),
98
+ oseo_undefined(),
99
+ OSEO_FUNCTION_NAME_PREFIX_NONE
100
+ );
101
+ frame.slots[0] = result.value;
102
+ }
103
+ /* Materializing Function.prototype above may have requested and cached
104
+ * this intrinsic while the outer allocation was still in progress. Keep
105
+ * that completed function as the realm identity instead of replacing it
106
+ * with the outer allocation. */
107
+ if (result.status == OSEO_STATUS_NORMAL && is_function(*cache)) {
108
+ result = normal(*cache);
109
+ oseo_roots_release(context, &frame);
110
+ return result;
111
+ }
112
+ const OseoPropertyAttributes frozen =
113
+ (OseoPropertyAttributes){false, false, false, false};
114
+ const char *const hardened[] = {"length", "name"};
115
+ for (size_t index = 0u;
116
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
117
+ index += 1u) {
118
+ result = oseo_internal_ascii_string(context, hardened[index]);
119
+ frame.slots[1] = result.value;
120
+ OseoValue existing = oseo_undefined();
121
+ if (result.status == OSEO_STATUS_NORMAL) {
122
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
123
+ existing = result.value;
124
+ }
125
+ if (result.status == OSEO_STATUS_NORMAL) {
126
+ result = oseo_object_define(
127
+ context,
128
+ frame.slots[0],
129
+ frame.slots[1],
130
+ existing,
131
+ frozen
132
+ );
133
+ }
134
+ }
135
+ if (result.status == OSEO_STATUS_NORMAL) {
136
+ ordinary_object(frame.slots[0])->extensible = false;
137
+ *cache = frame.slots[0];
138
+ result = normal(frame.slots[0]);
139
+ }
140
+ oseo_roots_release(context, &frame);
141
+ return result;
142
+ }
143
+
144
+ /*
145
+ * Both arguments object shapes define `@@iterator` as an ordinary
146
+ * writable, non-enumerable, configurable data property whose value is
147
+ * the same %Array.prototype.values% function an array's own
148
+ * `Symbol.iterator` resolves to, so spreading or iterating an arguments
149
+ * object walks its indices through the same array iterator, which reads
150
+ * a non-array target's `length` the way LengthOfArrayLike does.
151
+ */
152
+ static OseoResult arguments_define_iterator(
153
+ OseoContext *context,
154
+ OseoValue object
155
+ ) {
156
+ OseoRootFrame frame = {NULL, NULL, 0u};
157
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
158
+ if (result.status != OSEO_STATUS_NORMAL) return result;
159
+ frame.slots[0] = object;
160
+ result = oseo_internal_well_known_symbol(context, OSEO_WELL_KNOWN_ITERATOR);
161
+ frame.slots[1] = result.value;
162
+ if (result.status == OSEO_STATUS_NORMAL) {
163
+ result =
164
+ oseo_internal_iterator_method(context, OSEO_ARRAY_VALUES_CODE_ID);
165
+ frame.slots[2] = result.value;
166
+ }
167
+ if (result.status == OSEO_STATUS_NORMAL) {
168
+ const OseoPropertyAttributes attributes =
169
+ (OseoPropertyAttributes){true, false, true, false};
170
+ result = oseo_object_define(
171
+ context,
172
+ frame.slots[0],
173
+ frame.slots[1],
174
+ frame.slots[2],
175
+ attributes
176
+ );
177
+ }
178
+ oseo_roots_release(context, &frame);
179
+ return result;
180
+ }
181
+
182
+ OseoResult oseo_arguments_create(
183
+ OseoContext *context,
184
+ size_t argument_count,
185
+ const OseoValue *arguments
186
+ ) {
187
+ if (argument_count > 0u && arguments == NULL) {
188
+ return failure(context, "OSEO2001", "Arguments are missing.");
189
+ }
190
+ OseoRootFrame frame = {NULL, NULL, 0u};
191
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
192
+ if (result.status != OSEO_STATUS_NORMAL) return result;
193
+ result = oseo_object_literal_create(context);
194
+ frame.slots[0] = result.value;
195
+ if (result.status == OSEO_STATUS_NORMAL) {
196
+ ordinary_object(frame.slots[0])->arguments_object = true;
197
+ }
198
+ const OseoPropertyAttributes indexed =
199
+ (OseoPropertyAttributes){true, true, true, false};
200
+ for (size_t index = 0u;
201
+ result.status == OSEO_STATUS_NORMAL && index < argument_count;
202
+ index += 1u) {
203
+ result = oseo_property_key(context, oseo_number((double)index));
204
+ frame.slots[1] = result.value;
205
+ if (result.status == OSEO_STATUS_NORMAL) {
206
+ result = oseo_object_define(
207
+ context,
208
+ frame.slots[0],
209
+ frame.slots[1],
210
+ arguments[index],
211
+ indexed
212
+ );
213
+ }
214
+ }
215
+ const OseoPropertyAttributes metadata =
216
+ (OseoPropertyAttributes){true, false, true, false};
217
+ if (result.status == OSEO_STATUS_NORMAL) {
218
+ result = oseo_internal_ascii_string(context, "length");
219
+ frame.slots[1] = result.value;
220
+ }
221
+ if (result.status == OSEO_STATUS_NORMAL) {
222
+ result = oseo_object_define(
223
+ context,
224
+ frame.slots[0],
225
+ frame.slots[1],
226
+ oseo_number((double)argument_count),
227
+ metadata
228
+ );
229
+ }
230
+ /*
231
+ * CreateUnmappedArgumentsObject poisons `callee` unconditionally: it
232
+ * is a non-configurable accessor whose [[Get]] and [[Set]] are both
233
+ * %ThrowTypeError%, so the running function never leaks through an
234
+ * unmapped arguments object, whatever the caller's strictness. Only
235
+ * the mapped object still exposes the ordinary data property.
236
+ */
237
+ if (result.status == OSEO_STATUS_NORMAL) {
238
+ result = oseo_internal_throw_type_error_function(context);
239
+ frame.slots[2] = result.value;
240
+ }
241
+ if (result.status == OSEO_STATUS_NORMAL) {
242
+ result = oseo_internal_ascii_string(context, "callee");
243
+ frame.slots[1] = result.value;
244
+ }
245
+ if (result.status == OSEO_STATUS_NORMAL) {
246
+ const OseoPropertyAttributes poisoned =
247
+ (OseoPropertyAttributes){false, false, false, true};
248
+ result = oseo_object_define_accessor(
249
+ context,
250
+ frame.slots[0],
251
+ frame.slots[1],
252
+ frame.slots[2],
253
+ frame.slots[2],
254
+ true,
255
+ true,
256
+ poisoned
257
+ );
258
+ }
259
+ if (result.status == OSEO_STATUS_NORMAL) {
260
+ result = arguments_define_iterator(context, frame.slots[0]);
261
+ }
262
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
263
+ oseo_roots_release(context, &frame);
264
+ return result;
265
+ }
266
+
267
+ /*
268
+ * CreateMappedArgumentsObject (10.4.4.7), admitted only for a non-strict
269
+ * function whose parameter list is simple. `mapped_indices` names, in
270
+ * ascending order, exactly the indices that are the rightmost formal
271
+ * parameter of their name, and `mapped_binding_ids` gives each of those
272
+ * indices its parameter's own environment binding id at the same
273
+ * position; both are empty for a supplied index that is a duplicate
274
+ * name's non-rightmost occurrence, or at or beyond the parameter count,
275
+ * since ECMA-262 maps only the rightmost occurrence of a declared
276
+ * parameter position. Defining a mapped index's property with its
277
+ * parameter's own binding cell as the stored value, rather than the
278
+ * plain snapshot value an unmapped arguments object stores, is what
279
+ * makes oseo_internal_cell_backed_property recognize it afterward: every later
280
+ * [[Get]]/[[Set]]/[[GetOwnProperty]]/[[DefineOwnProperty]]/[[Delete]]
281
+ * on the index then reaches the parameter through the existing
282
+ * cell-backed property machinery with no exotic method table of its
283
+ * own.
284
+ */
285
+ OseoResult oseo_mapped_arguments_create(
286
+ OseoContext *context,
287
+ OseoValue environment,
288
+ OseoValue callee,
289
+ size_t argument_count,
290
+ const OseoValue *arguments,
291
+ const size_t *mapped_indices,
292
+ const size_t *mapped_binding_ids,
293
+ size_t mapped_count
294
+ ) {
295
+ if (argument_count > 0u && arguments == NULL) {
296
+ return failure(context, "OSEO2001", "Arguments are missing.");
297
+ }
298
+ if (mapped_count > 0u &&
299
+ (mapped_indices == NULL || mapped_binding_ids == NULL)) {
300
+ return failure(context, "OSEO2001", "Mapped parameters are missing.");
301
+ }
302
+ OseoRootFrame frame = {NULL, NULL, 0u};
303
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
304
+ if (result.status != OSEO_STATUS_NORMAL) return result;
305
+ frame.slots[2] = callee;
306
+ result = oseo_object_literal_create(context);
307
+ frame.slots[0] = result.value;
308
+ if (result.status == OSEO_STATUS_NORMAL) {
309
+ ordinary_object(frame.slots[0])->arguments_object = true;
310
+ ordinary_object(frame.slots[0])->mapped_arguments = true;
311
+ }
312
+ const OseoPropertyAttributes indexed =
313
+ (OseoPropertyAttributes){true, true, true, false};
314
+ size_t mapped_position = 0u;
315
+ for (size_t index = 0u;
316
+ result.status == OSEO_STATUS_NORMAL && index < argument_count;
317
+ index += 1u) {
318
+ bool is_mapped = mapped_position < mapped_count &&
319
+ mapped_indices[mapped_position] == index;
320
+ if (is_mapped) {
321
+ result = oseo_environment_get(
322
+ context,
323
+ environment,
324
+ mapped_binding_ids[mapped_position]
325
+ );
326
+ mapped_position += 1u;
327
+ } else {
328
+ result = normal(arguments[index]);
329
+ }
330
+ frame.slots[3] = result.value;
331
+ if (result.status == OSEO_STATUS_NORMAL) {
332
+ result = oseo_property_key(context, oseo_number((double)index));
333
+ frame.slots[1] = result.value;
334
+ }
335
+ if (result.status == OSEO_STATUS_NORMAL) {
336
+ result = oseo_object_define(
337
+ context,
338
+ frame.slots[0],
339
+ frame.slots[1],
340
+ frame.slots[3],
341
+ indexed
342
+ );
343
+ }
344
+ }
345
+ const OseoPropertyAttributes metadata =
346
+ (OseoPropertyAttributes){true, false, true, false};
347
+ if (result.status == OSEO_STATUS_NORMAL) {
348
+ result = oseo_internal_ascii_string(context, "length");
349
+ frame.slots[1] = result.value;
350
+ }
351
+ if (result.status == OSEO_STATUS_NORMAL) {
352
+ result = oseo_object_define(
353
+ context,
354
+ frame.slots[0],
355
+ frame.slots[1],
356
+ oseo_number((double)argument_count),
357
+ metadata
358
+ );
359
+ }
360
+ if (result.status == OSEO_STATUS_NORMAL) {
361
+ result = oseo_internal_ascii_string(context, "callee");
362
+ frame.slots[1] = result.value;
363
+ }
364
+ if (result.status == OSEO_STATUS_NORMAL) {
365
+ result = oseo_object_define(
366
+ context,
367
+ frame.slots[0],
368
+ frame.slots[1],
369
+ frame.slots[2],
370
+ metadata
371
+ );
372
+ }
373
+ if (result.status == OSEO_STATUS_NORMAL) {
374
+ result = arguments_define_iterator(context, frame.slots[0]);
375
+ }
376
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
377
+ oseo_roots_release(context, &frame);
378
+ return result;
379
+ }