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