@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,488 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ #include "runtime_internal.h"
29
+
30
+ #include <math.h>
31
+ #include <stdatomic.h>
32
+ #include <stdio.h>
33
+ #include <stdlib.h>
34
+ #include <string.h>
35
+
36
+ /*
37
+ * Results, context lifecycle, diagnostics, call limits, frames,
38
+ * root-stack operations, and immediate value constructors.
39
+ */
40
+
41
+ /*
42
+ * How many realms this process has initialized. ECMA-262 requires the
43
+ * Math.random of one realm to produce a sequence distinct from every
44
+ * other realm's, and this runtime takes no host entropy, so the realms
45
+ * count themselves: realm N of a run seeds from ordinal N, so a host
46
+ * that initializes its realms in one order observes the same sequences
47
+ * on every run and every target while no two realms share one. The
48
+ * counter is atomic so that a host initializing contexts on separate
49
+ * threads would still read distinct ordinals without a data race, which
50
+ * is all atomicity buys: it does not order those threads, so such a host
51
+ * would keep distinctness and lose the fixed ordinal each realm gets.
52
+ * Relaxed order is enough because an ordinal only has to be distinct,
53
+ * not ordered against any other memory.
54
+ */
55
+ static _Atomic uint64_t realm_ordinal;
56
+
57
+ /*
58
+ * One SplitMix64 output over `state`, which the call advances. The mix
59
+ * is a bijection over the whole 64-bit state, so consecutive ordinals
60
+ * scatter into unrelated and pairwise distinct seed words rather than
61
+ * into adjacent xorshift128+ states, and every multiplication here wraps
62
+ * as unsigned arithmetic defines.
63
+ */
64
+ static uint64_t seed_word(uint64_t *state) {
65
+ uint64_t word = (*state += UINT64_C(0x9e3779b97f4a7c15));
66
+ word = (word ^ (word >> 30u)) * UINT64_C(0xbf58476d1ce4e5b9);
67
+ word = (word ^ (word >> 27u)) * UINT64_C(0x94d049bb133111eb);
68
+ return word ^ (word >> 31u);
69
+ }
70
+
71
+ void oseo_internal_realm_init(OseoRealm *realm) {
72
+ for (size_t index = 0u; index < OSEO_INTRINSIC_COUNT; index += 1u) {
73
+ realm->intrinsics[index] = oseo_undefined();
74
+ }
75
+ realm->global_this = oseo_undefined();
76
+ realm->global_lexicals = oseo_undefined();
77
+ realm->template_cache = NULL;
78
+ realm->template_cache_count = 0u;
79
+ realm->template_cache_capacity = 0u;
80
+ realm->regexp_literal_cache = NULL;
81
+ realm->regexp_literal_cache_count = 0u;
82
+ realm->regexp_literal_cache_capacity = 0u;
83
+ /*
84
+ * The realm's Math.random state, mixed from the realm's own
85
+ * initialization ordinal rather than from host entropy, so distinct
86
+ * realms start from distinct states and draw distinct sequences that
87
+ * are uniform and the same on every run and every target. The low
88
+ * bit of the second word is set because a xorshift128+ state must
89
+ * never be all zero.
90
+ */
91
+ uint64_t seed = atomic_fetch_add_explicit(
92
+ &realm_ordinal,
93
+ UINT64_C(1),
94
+ memory_order_relaxed
95
+ );
96
+ realm->random_state[0] = seed_word(&seed);
97
+ realm->random_state[1] = seed_word(&seed) | UINT64_C(1);
98
+ }
99
+
100
+ void oseo_internal_realm_release(OseoRealm *realm) {
101
+ for (size_t index = 0u; index < OSEO_INTRINSIC_COUNT; index += 1u) {
102
+ realm->intrinsics[index] = oseo_undefined();
103
+ }
104
+ realm->global_this = oseo_undefined();
105
+ realm->global_lexicals = oseo_undefined();
106
+ free(realm->template_cache);
107
+ realm->template_cache = NULL;
108
+ realm->template_cache_count = 0u;
109
+ realm->template_cache_capacity = 0u;
110
+ free(realm->regexp_literal_cache);
111
+ realm->regexp_literal_cache = NULL;
112
+ realm->regexp_literal_cache_count = 0u;
113
+ realm->regexp_literal_cache_capacity = 0u;
114
+ }
115
+
116
+ void oseo_context_init(
117
+ OseoContext *context,
118
+ const char *source_id,
119
+ size_t source_id_length
120
+ ) {
121
+ context->process_argc = 0;
122
+ context->process_argv = NULL;
123
+ context->process_has_exit_code = false;
124
+ context->process_exit_code = 0;
125
+ context->process_stdout_closed = false;
126
+ context->process_stderr_closed = false;
127
+ context->roots = NULL;
128
+ context->objects = NULL;
129
+ context->host_scripts = NULL;
130
+ context->host_script_count = 0u;
131
+ context->function_dispatcher = NULL;
132
+ context->generator_dispatcher = NULL;
133
+ context->async_call_capability = oseo_undefined();
134
+ context->microtask_head = oseo_undefined();
135
+ context->microtask_tail = oseo_undefined();
136
+ context->pending_rejections = oseo_undefined();
137
+ context->pending_rejection_tail = oseo_undefined();
138
+ oseo_internal_realm_init(&context->initial_realm);
139
+ context->realm = &context->initial_realm;
140
+ context->realm_record = oseo_undefined();
141
+ context->constructor_scope = NULL;
142
+ context->activation = NULL;
143
+ context->legacy_function_keys[0] = oseo_undefined();
144
+ context->legacy_function_keys[1] = oseo_undefined();
145
+ for (size_t index = 0u;
146
+ index < OSEO_WELL_KNOWN_SYMBOL_COUNT;
147
+ index += 1u) {
148
+ context->well_known_symbols[index] = oseo_undefined();
149
+ }
150
+ context->registered_symbols = NULL;
151
+ context->registered_symbol_count = 0u;
152
+ context->registered_symbol_capacity = 0u;
153
+ context->array_string_stack = NULL;
154
+ context->timer_head = oseo_undefined();
155
+ context->atomics_waiter_head = oseo_undefined();
156
+ context->atomics_waiter_tail = oseo_undefined();
157
+ context->agent = NULL;
158
+ context->agent_can_block = true;
159
+ context->agent_broadcast_callback = oseo_undefined();
160
+ context->finalization_head = oseo_undefined();
161
+ context->finalization_tail = oseo_undefined();
162
+ context->next_finalization_order = 0u;
163
+ context->finalization_pending_count = 0u;
164
+ context->ephemeron_table_count = 0u;
165
+ context->weak_reference_count = 0u;
166
+ context->finalization_registry_count = 0u;
167
+ context->finalization_cell_count = 0u;
168
+ context->kept_objects = NULL;
169
+ context->kept_object_count = 0u;
170
+ context->kept_object_capacity = 0u;
171
+ context->tail_call = NULL;
172
+ context->string_table = NULL;
173
+ context->module_table = NULL;
174
+ context->source_id = source_id;
175
+ context->source_id_length = source_id_length;
176
+ oseo_context_clear_language_error(context);
177
+ context->active_frame_slots = 0u;
178
+ context->call_depth = 0u;
179
+ context->line = 1u;
180
+ context->column = 1u;
181
+ context->guard_hits = 0u;
182
+ context->guard_misses = 0u;
183
+ context->overflow_misses = 0u;
184
+ context->generic_addition_calls = 0u;
185
+ context->next_shape_id = 1u;
186
+ context->allocations = 0u;
187
+ context->allocation_attempts = 0u;
188
+ context->collections = 0u;
189
+ context->rejection_handled_count = 0u;
190
+ context->unhandled_rejection_count = 0u;
191
+ context->unhandled_rejection_policy = OSEO_UNHANDLED_REJECTION_FATAL;
192
+ context->clock_adapter = NULL;
193
+ context->clock_state = NULL;
194
+ context->clock_origin = 0u;
195
+ context->clock_restrictions = 0u;
196
+ context->clock_started = false;
197
+ context->clock_milliseconds = 0u;
198
+ context->next_timer_id = 1u;
199
+ context->next_timer_order = 0u;
200
+ context->fail_allocation_at = 0u;
201
+ context->observe_specialization = false;
202
+ context->collect_every_safepoint =
203
+ getenv("OSEO_GC_EVERY_SAFEPOINT") != NULL;
204
+ }
205
+
206
+ void oseo_context_set_function_dispatcher(
207
+ OseoContext *context,
208
+ OseoFunctionDispatcher dispatcher
209
+ ) {
210
+ context->function_dispatcher = dispatcher;
211
+ }
212
+
213
+ void oseo_context_set_generator_dispatcher(
214
+ OseoContext *context,
215
+ OseoGeneratorDispatcher dispatcher
216
+ ) {
217
+ context->generator_dispatcher = dispatcher;
218
+ }
219
+
220
+ void oseo_context_fail_allocation_at(OseoContext *context, size_t attempt) {
221
+ context->allocation_attempts = 0u;
222
+ context->fail_allocation_at = attempt;
223
+ }
224
+
225
+ void oseo_context_clear_language_error(OseoContext *context) {
226
+ context->error_code = "OSEO2001";
227
+ context->error_message = OSEO_UNHANDLED_THROW_MESSAGE;
228
+ context->has_diagnostic = false;
229
+ }
230
+
231
+ void oseo_context_destroy(OseoContext *context) {
232
+ oseo_internal_agent_context_destroy(context);
233
+ context->roots = NULL;
234
+ context->async_call_capability = oseo_undefined();
235
+ context->microtask_head = oseo_undefined();
236
+ context->microtask_tail = oseo_undefined();
237
+ context->pending_rejections = oseo_undefined();
238
+ context->pending_rejection_tail = oseo_undefined();
239
+ /*
240
+ * Every created realm is a heap object, so the final collection
241
+ * below frees each one; only the initial realm is released here.
242
+ */
243
+ context->realm = &context->initial_realm;
244
+ context->realm_record = oseo_undefined();
245
+ context->constructor_scope = NULL;
246
+ context->activation = NULL;
247
+ context->legacy_function_keys[0] = oseo_undefined();
248
+ context->legacy_function_keys[1] = oseo_undefined();
249
+ oseo_internal_realm_release(&context->initial_realm);
250
+ for (size_t index = 0u;
251
+ index < OSEO_WELL_KNOWN_SYMBOL_COUNT;
252
+ index += 1u) {
253
+ context->well_known_symbols[index] = oseo_undefined();
254
+ }
255
+ free(context->registered_symbols);
256
+ context->registered_symbols = NULL;
257
+ context->registered_symbol_count = 0u;
258
+ context->registered_symbol_capacity = 0u;
259
+ context->array_string_stack = NULL;
260
+ context->timer_head = oseo_undefined();
261
+ context->atomics_waiter_head = oseo_undefined();
262
+ context->atomics_waiter_tail = oseo_undefined();
263
+ context->agent_broadcast_callback = oseo_undefined();
264
+ context->finalization_head = oseo_undefined();
265
+ context->finalization_tail = oseo_undefined();
266
+ context->finalization_pending_count = 0u;
267
+ free(context->kept_objects);
268
+ context->kept_objects = NULL;
269
+ context->kept_object_count = 0u;
270
+ context->kept_object_capacity = 0u;
271
+ oseo_internal_tail_call_release(context);
272
+ oseo_internal_module_table_release(context);
273
+ oseo_internal_string_table_release(context);
274
+ /*
275
+ * A mark another context set while it rooted one of this context's
276
+ * values is still set here, and no sweep of that context ever clears
277
+ * it, so it has to go before the sweep that decides what this last
278
+ * collection frees.
279
+ */
280
+ oseo_internal_clear_heap_marks(context);
281
+ /*
282
+ * A registered representative is the one value another context may
283
+ * still hold, in a property value, a Map value, or any other value
284
+ * position no localization reaches, so it and its description leave
285
+ * this heap rather than being freed with it.
286
+ */
287
+ oseo_internal_retire_registered_symbols(context);
288
+ oseo_collect(context);
289
+ oseo_internal_clock_destroy(context);
290
+ }
291
+
292
+ void oseo_context_location(
293
+ OseoContext *context,
294
+ size_t line,
295
+ size_t column
296
+ ) {
297
+ context->line = line;
298
+ context->column = column;
299
+ }
300
+
301
+ void oseo_context_source_location(
302
+ OseoContext *context,
303
+ const char *source_id,
304
+ size_t source_id_length,
305
+ size_t line,
306
+ size_t column
307
+ ) {
308
+ context->source_id = source_id;
309
+ context->source_id_length = source_id_length;
310
+ context->line = line;
311
+ context->column = column;
312
+ }
313
+
314
+ void oseo_context_print_error(const OseoContext *context) {
315
+ (void)fwrite(
316
+ context->source_id,
317
+ 1u,
318
+ context->source_id_length,
319
+ stderr
320
+ );
321
+ (void)fprintf(
322
+ stderr,
323
+ ":%zu:%zu: error[%s]: %s\n",
324
+ context->line,
325
+ context->column,
326
+ context->error_code,
327
+ context->error_message
328
+ );
329
+ }
330
+
331
+ void oseo_context_print_observations(const OseoContext *context) {
332
+ (void)fprintf(
333
+ stderr,
334
+ "OSEO_OBSERVATIONS "
335
+ "{\"guardHits\":%zu,\"guardMisses\":%zu,"
336
+ "\"overflowMisses\":%zu,\"genericAdditionCalls\":%zu,"
337
+ "\"allocations\":%zu,\"collections\":%zu}\n",
338
+ context->guard_hits,
339
+ context->guard_misses,
340
+ context->overflow_misses,
341
+ context->generic_addition_calls,
342
+ context->allocations,
343
+ context->collections
344
+ );
345
+ }
346
+
347
+ OseoResult oseo_call_enter(OseoContext *context) {
348
+ if (context->call_depth >= OSEO_MAX_CALL_DEPTH) {
349
+ return failure(
350
+ context,
351
+ "OSEO2001",
352
+ "Maximum call depth exceeded."
353
+ );
354
+ }
355
+ context->call_depth += 1u;
356
+ return normal(oseo_undefined());
357
+ }
358
+
359
+ void oseo_call_leave(OseoContext *context) {
360
+ if (context->call_depth > 0u) context->call_depth -= 1u;
361
+ }
362
+
363
+ OseoResult oseo_frame_enter(OseoContext *context, size_t slot_count) {
364
+ if (slot_count >
365
+ OSEO_MAX_ACTIVE_FRAME_SLOTS - context->active_frame_slots) {
366
+ return failure(
367
+ context,
368
+ "OSEO2001",
369
+ "Maximum active native frame budget exceeded."
370
+ );
371
+ }
372
+ context->active_frame_slots += slot_count;
373
+ return normal(oseo_undefined());
374
+ }
375
+
376
+ void oseo_frame_leave(OseoContext *context, size_t slot_count) {
377
+ if (slot_count <= context->active_frame_slots) {
378
+ context->active_frame_slots -= slot_count;
379
+ } else {
380
+ context->active_frame_slots = 0u;
381
+ }
382
+ }
383
+
384
+ void oseo_roots_push(OseoContext *context, OseoRootFrame *frame) {
385
+ frame->previous = context->roots;
386
+ context->roots = frame;
387
+ }
388
+
389
+ void oseo_roots_pop(OseoContext *context, OseoRootFrame *frame) {
390
+ if (context->roots == frame) context->roots = frame->previous;
391
+ }
392
+
393
+ OseoResult oseo_roots_allocate(
394
+ OseoContext *context,
395
+ OseoRootFrame *frame,
396
+ size_t slot_count
397
+ ) {
398
+ OseoValue *slots = NULL;
399
+ if (slot_count > 0u) {
400
+ if (slot_count > SIZE_MAX / sizeof(OseoValue)) {
401
+ return failure(
402
+ context,
403
+ "OSEO2001",
404
+ "Root frame allocation is too large."
405
+ );
406
+ }
407
+ slots = calloc(slot_count, sizeof(OseoValue));
408
+ if (slots == NULL) {
409
+ return failure(
410
+ context,
411
+ "OSEO2001",
412
+ "Root frame allocation failed."
413
+ );
414
+ }
415
+ }
416
+ frame->previous = NULL;
417
+ frame->slots = slots;
418
+ frame->slot_count = slot_count;
419
+ oseo_roots_push(context, frame);
420
+ return normal(oseo_undefined());
421
+ }
422
+
423
+ void oseo_roots_release(OseoContext *context, OseoRootFrame *frame) {
424
+ oseo_roots_pop(context, frame);
425
+ free(frame->slots);
426
+ frame->previous = NULL;
427
+ frame->slots = NULL;
428
+ frame->slot_count = 0u;
429
+ }
430
+
431
+ OseoValue oseo_undefined(void) {
432
+ return tagged(OSEO_TAG_UNDEFINED, 0u);
433
+ }
434
+
435
+ OseoValue oseo_uninitialized(void) {
436
+ return tagged(OSEO_TAG_UNINITIALIZED, 0u);
437
+ }
438
+
439
+ OseoResult oseo_read_binding(OseoContext *context, OseoValue value) {
440
+ if (tag_of(value) == OSEO_TAG_UNINITIALIZED) {
441
+ return oseo_internal_throw_error(
442
+ context,
443
+ OSEO_ERROR_REFERENCE,
444
+ "Binding is read before initialization."
445
+ );
446
+ }
447
+ return normal(value);
448
+ }
449
+
450
+ OseoResult oseo_write_immutable_binding(OseoContext *context) {
451
+ return oseo_internal_throw_error(
452
+ context,
453
+ OSEO_ERROR_TYPE,
454
+ "Cannot assign to an immutable binding."
455
+ );
456
+ }
457
+
458
+ OseoValue oseo_null(void) {
459
+ return tagged(OSEO_TAG_NULL, 0u);
460
+ }
461
+
462
+ OseoValue oseo_boolean(bool value) {
463
+ return tagged(OSEO_TAG_BOOLEAN, value ? 1u : 0u);
464
+ }
465
+
466
+ OseoValue oseo_number(double value) {
467
+ if (isnan(value)) return OSEO_CANONICAL_NAN;
468
+ if (value == 0.0 && signbit(value)) return double_bits(value);
469
+ if (isfinite(value) && trunc(value) == value &&
470
+ value >= (double)OSEO_SMI_MIN && value <= (double)OSEO_SMI_MAX) {
471
+ int64_t integer = (int64_t)value;
472
+ return tagged(OSEO_TAG_SMI, (uint64_t)integer);
473
+ }
474
+ return double_bits(value);
475
+ }
476
+
477
+ bool oseo_to_boolean(OseoValue value) {
478
+ uint64_t tag = tag_of(value);
479
+ if (tag == OSEO_TAG_UNDEFINED || tag == OSEO_TAG_NULL) return false;
480
+ if (tag == OSEO_TAG_BOOLEAN) return (value & 1u) != 0u;
481
+ if (is_number(value)) {
482
+ double number = number_value(value);
483
+ return number != 0.0 && !isnan(number);
484
+ }
485
+ if (is_bigint(value)) return !oseo_internal_bigint_is_zero(value);
486
+ if (is_string(value)) return string_object(value)->length != 0u;
487
+ return tag == OSEO_TAG_HEAP;
488
+ }