@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,296 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <string.h>
7
+
8
+ /* Boolean constructor, prototype methods, and branded wrapper objects. */
9
+
10
+ static OseoResult boolean_this_value(
11
+ OseoContext *context,
12
+ OseoValue receiver
13
+ ) {
14
+ if (tag_of(receiver) == OSEO_TAG_BOOLEAN) return normal(receiver);
15
+ if (is_object(receiver) &&
16
+ ordinary_object(receiver)->primitive_data &&
17
+ tag_of(ordinary_object(receiver)->primitive_value) ==
18
+ OSEO_TAG_BOOLEAN) {
19
+ return normal(ordinary_object(receiver)->primitive_value);
20
+ }
21
+ return oseo_internal_throw_error(
22
+ context,
23
+ OSEO_ERROR_TYPE,
24
+ "Boolean method requires a Boolean receiver."
25
+ );
26
+ }
27
+
28
+ static OseoResult boolean_construct(
29
+ OseoContext *context,
30
+ OseoValue new_target,
31
+ size_t argument_count,
32
+ const OseoValue *arguments,
33
+ bool constructing
34
+ ) {
35
+ OseoValue data = oseo_boolean(
36
+ argument_count > 0u && oseo_to_boolean(arguments[0])
37
+ );
38
+ if (!constructing) return normal(data);
39
+
40
+ OseoValue slots[2] = {new_target, oseo_undefined()};
41
+ OseoRootFrame frame = {NULL, slots, 2u};
42
+ oseo_roots_push(context, &frame);
43
+ OseoResult result = oseo_internal_constructor_prototype(
44
+ context,
45
+ slots[0]
46
+ );
47
+ slots[1] = result.value;
48
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[1])) {
49
+ result = oseo_internal_constructor_realm_default(
50
+ context,
51
+ slots[0],
52
+ OSEO_INTRINSIC_BOOLEAN_PROTOTYPE
53
+ );
54
+ slots[1] = result.value;
55
+ }
56
+ if (result.status == OSEO_STATUS_NORMAL) {
57
+ result = oseo_object_create(context, slots[1]);
58
+ }
59
+ if (result.status == OSEO_STATUS_NORMAL) {
60
+ OseoOrdinaryObject *object = ordinary_object(result.value);
61
+ object->primitive_data = true;
62
+ object->primitive_value = data;
63
+ }
64
+ oseo_roots_pop(context, &frame);
65
+ return result;
66
+ }
67
+
68
+ static OseoResult boolean_to_string(
69
+ OseoContext *context,
70
+ OseoValue receiver
71
+ ) {
72
+ OseoResult result = boolean_this_value(context, receiver);
73
+ if (result.status != OSEO_STATUS_NORMAL) return result;
74
+ const char *text = oseo_to_boolean(result.value) ? "true" : "false";
75
+ return oseo_internal_ascii_string(context, text);
76
+ }
77
+
78
+ OseoResult oseo_internal_boolean_builtin_dispatch(
79
+ OseoContext *context,
80
+ size_t code_id,
81
+ OseoValue callee,
82
+ OseoValue receiver,
83
+ size_t argument_count,
84
+ const OseoValue *arguments,
85
+ OseoValue new_target
86
+ ) {
87
+ (void)callee;
88
+ if (code_id == OSEO_BOOLEAN_CONSTRUCTOR_CODE_ID) {
89
+ return boolean_construct(
90
+ context,
91
+ new_target,
92
+ argument_count,
93
+ arguments,
94
+ tag_of(new_target) != OSEO_TAG_UNDEFINED
95
+ );
96
+ }
97
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
98
+ return oseo_internal_throw_error(
99
+ context,
100
+ OSEO_ERROR_TYPE,
101
+ "Boolean method is not a constructor."
102
+ );
103
+ }
104
+ if (code_id == OSEO_BOOLEAN_TO_STRING_CODE_ID) {
105
+ return boolean_to_string(context, receiver);
106
+ }
107
+ if (code_id == OSEO_BOOLEAN_VALUE_OF_CODE_ID) {
108
+ return boolean_this_value(context, receiver);
109
+ }
110
+ return oseo_unknown_function(context, code_id);
111
+ }
112
+
113
+ static OseoResult create_boolean_function(
114
+ OseoContext *context,
115
+ size_t code_id,
116
+ const char *name,
117
+ size_t length,
118
+ OseoFunctionKind kind
119
+ ) {
120
+ size_t name_length = strlen(name);
121
+ uint16_t units[16];
122
+ if (name_length > sizeof(units) / sizeof(*units)) {
123
+ return failure(context, "OSEO2001", "Boolean function name is long.");
124
+ }
125
+ for (size_t index = 0u; index < name_length; index += 1u) {
126
+ units[index] = (uint16_t)(unsigned char)name[index];
127
+ }
128
+ OseoValue environment = oseo_undefined();
129
+ OseoRootFrame frame = {NULL, &environment, 1u};
130
+ oseo_roots_push(context, &frame);
131
+ OseoResult result = oseo_environment_create(context, 0u);
132
+ environment = result.value;
133
+ if (result.status == OSEO_STATUS_NORMAL) {
134
+ result = oseo_function_create(
135
+ context,
136
+ code_id,
137
+ environment,
138
+ units,
139
+ name_length,
140
+ length,
141
+ kind,
142
+ oseo_undefined(),
143
+ oseo_undefined(),
144
+ OSEO_FUNCTION_NAME_PREFIX_NONE
145
+ );
146
+ }
147
+ oseo_roots_pop(context, &frame);
148
+ return result;
149
+ }
150
+
151
+ static OseoResult define_boolean_property(
152
+ OseoContext *context,
153
+ OseoValue object,
154
+ const char *name,
155
+ OseoValue value,
156
+ OseoPropertyAttributes attributes
157
+ ) {
158
+ OseoValue slots[3] = {object, value, oseo_undefined()};
159
+ OseoRootFrame frame = {NULL, slots, 3u};
160
+ oseo_roots_push(context, &frame);
161
+ OseoResult result = oseo_internal_ascii_string(context, name);
162
+ slots[2] = result.value;
163
+ if (result.status == OSEO_STATUS_NORMAL) {
164
+ result = oseo_object_define(
165
+ context,
166
+ slots[0],
167
+ slots[2],
168
+ slots[1],
169
+ attributes
170
+ );
171
+ }
172
+ oseo_roots_pop(context, &frame);
173
+ return result;
174
+ }
175
+
176
+ OseoResult oseo_internal_boolean_intrinsic(OseoContext *context) {
177
+ OseoValue *marker =
178
+ &context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_VALUE_OF];
179
+ if (is_function(*marker)) {
180
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN]);
181
+ }
182
+ size_t entry_allocations = context->allocations;
183
+ OseoRootFrame frame = {NULL, NULL, 0u};
184
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
185
+ if (result.status != OSEO_STATUS_NORMAL) return result;
186
+
187
+ result = oseo_internal_intrinsic(
188
+ context,
189
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
190
+ );
191
+ frame.slots[0] = result.value;
192
+ if (result.status == OSEO_STATUS_NORMAL) {
193
+ result = oseo_object_create(context, frame.slots[0]);
194
+ frame.slots[0] = result.value;
195
+ }
196
+ if (result.status == OSEO_STATUS_NORMAL) {
197
+ OseoOrdinaryObject *prototype = ordinary_object(frame.slots[0]);
198
+ prototype->primitive_data = true;
199
+ prototype->primitive_value = oseo_boolean(false);
200
+ context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_PROTOTYPE] =
201
+ frame.slots[0];
202
+ result = create_boolean_function(
203
+ context,
204
+ OSEO_BOOLEAN_CONSTRUCTOR_CODE_ID,
205
+ "Boolean",
206
+ 1u,
207
+ OSEO_FUNCTION_ORDINARY
208
+ );
209
+ frame.slots[1] = result.value;
210
+ }
211
+ if (result.status == OSEO_STATUS_NORMAL) {
212
+ context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN] = frame.slots[1];
213
+ OseoFunction *constructor = function_object(frame.slots[1]);
214
+ constructor->prototype_object = frame.slots[0];
215
+ constructor->prototype_writable = false;
216
+ result = define_boolean_property(
217
+ context,
218
+ frame.slots[0],
219
+ "constructor",
220
+ frame.slots[1],
221
+ (OseoPropertyAttributes){true, false, true, false}
222
+ );
223
+ }
224
+ static const OseoIntrinsic intrinsics[] = {
225
+ OSEO_INTRINSIC_BOOLEAN_TO_STRING,
226
+ OSEO_INTRINSIC_BOOLEAN_VALUE_OF,
227
+ };
228
+ static const size_t codes[] = {
229
+ OSEO_BOOLEAN_TO_STRING_CODE_ID,
230
+ OSEO_BOOLEAN_VALUE_OF_CODE_ID,
231
+ };
232
+ static const char *const names[] = {"toString", "valueOf"};
233
+ for (size_t index = 0u;
234
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
235
+ index += 1u) {
236
+ result = create_boolean_function(
237
+ context,
238
+ codes[index],
239
+ names[index],
240
+ 0u,
241
+ OSEO_FUNCTION_INTERNAL
242
+ );
243
+ frame.slots[2] = result.value;
244
+ if (result.status == OSEO_STATUS_NORMAL) {
245
+ context->realm->intrinsics[intrinsics[index]] = frame.slots[2];
246
+ result = define_boolean_property(
247
+ context,
248
+ frame.slots[0],
249
+ names[index],
250
+ frame.slots[2],
251
+ (OseoPropertyAttributes){true, false, true, false}
252
+ );
253
+ }
254
+ }
255
+ if (result.status == OSEO_STATUS_NORMAL) {
256
+ ordinary_object(frame.slots[0])
257
+ ->primitive_wrapper_methods_initialized = true;
258
+ if (context->observe_specialization) {
259
+ context->allocations = entry_allocations;
260
+ }
261
+ } else {
262
+ context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_PROTOTYPE] =
263
+ oseo_undefined();
264
+ context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN] = oseo_undefined();
265
+ context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_TO_STRING] =
266
+ oseo_undefined();
267
+ context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_VALUE_OF] =
268
+ oseo_undefined();
269
+ }
270
+ oseo_roots_release(context, &frame);
271
+ return result.status == OSEO_STATUS_NORMAL
272
+ ? normal(context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN])
273
+ : result;
274
+ }
275
+
276
+ OseoResult oseo_internal_install_boolean_global(
277
+ OseoContext *context,
278
+ OseoValue global
279
+ ) {
280
+ OseoValue slots[2] = {global, oseo_undefined()};
281
+ OseoRootFrame frame = {NULL, slots, 2u};
282
+ oseo_roots_push(context, &frame);
283
+ OseoResult result = oseo_internal_boolean_intrinsic(context);
284
+ slots[1] = result.value;
285
+ if (result.status == OSEO_STATUS_NORMAL) {
286
+ result = define_boolean_property(
287
+ context,
288
+ slots[0],
289
+ "Boolean",
290
+ slots[1],
291
+ (OseoPropertyAttributes){true, false, true, false}
292
+ );
293
+ }
294
+ oseo_roots_pop(context, &frame);
295
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
296
+ }
@@ -0,0 +1,157 @@
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
+
8
+ /*
9
+ * The platform-neutral clock and wakeup boundary of ADR 0025: adapter
10
+ * installation and selection, the realm's monotonic origin and cached
11
+ * scheduler time, deadline waits in whole milliseconds, epoch real-time
12
+ * reads, and cross-thread wakeup.
13
+ *
14
+ * This component never names an operating-system facility. The platform
15
+ * adapter in runtime_clock_posix.c and any embedder-installed adapter
16
+ * reach the scheduler only through OseoClockAdapter, and the scheduler
17
+ * reaches them only through the functions here. Real time and the
18
+ * monotonic domain never meet: no deadline is derived from an epoch
19
+ * reading, and no epoch reading is derived from the monotonic origin.
20
+ */
21
+
22
+ #define OSEO_NANOSECONDS_PER_MILLISECOND UINT64_C(1000000)
23
+
24
+ static void clock_open(OseoContext *context) {
25
+ if (context->clock_adapter != NULL) return;
26
+ context->clock_adapter = oseo_internal_platform_clock_open(
27
+ context->clock_restrictions,
28
+ &context->clock_state
29
+ );
30
+ }
31
+
32
+ static void clock_close(OseoContext *context) {
33
+ const OseoClockAdapter *adapter = context->clock_adapter;
34
+ if (adapter != NULL) adapter->close(context->clock_state);
35
+ context->clock_adapter = NULL;
36
+ context->clock_state = NULL;
37
+ context->clock_started = false;
38
+ context->clock_origin = 0u;
39
+ }
40
+
41
+ void oseo_internal_clock_destroy(OseoContext *context) {
42
+ clock_close(context);
43
+ }
44
+
45
+ void oseo_context_set_clock(
46
+ OseoContext *context,
47
+ const OseoClockAdapter *adapter,
48
+ void *state
49
+ ) {
50
+ clock_close(context);
51
+ context->clock_adapter = adapter;
52
+ context->clock_state = state;
53
+ }
54
+
55
+ void oseo_context_restrict_clock(
56
+ OseoContext *context,
57
+ unsigned restrictions
58
+ ) {
59
+ context->clock_restrictions = restrictions;
60
+ }
61
+
62
+ bool oseo_clock_open(OseoContext *context) {
63
+ OseoClockCapabilities capabilities;
64
+ oseo_clock_capabilities(context, &capabilities);
65
+ return capabilities.wakeup != NULL;
66
+ }
67
+
68
+ bool oseo_clock_wake(OseoContext *context) {
69
+ const OseoClockAdapter *adapter = context->clock_adapter;
70
+ return adapter != NULL && adapter->wake(context->clock_state);
71
+ }
72
+
73
+ bool oseo_clock_real_time(OseoContext *context, double *milliseconds) {
74
+ clock_open(context);
75
+ double value = 0.0;
76
+ if (!context->clock_adapter->real_time(context->clock_state, &value) ||
77
+ !isfinite(value)) {
78
+ return false;
79
+ }
80
+ *milliseconds = value;
81
+ return true;
82
+ }
83
+
84
+ void oseo_clock_capabilities(
85
+ OseoContext *context,
86
+ OseoClockCapabilities *capabilities
87
+ ) {
88
+ clock_open(context);
89
+ context->clock_adapter->describe(context->clock_state, capabilities);
90
+ }
91
+
92
+ static OseoResult monotonic_failure(OseoContext *context) {
93
+ return failure(
94
+ context,
95
+ "OSEO2001",
96
+ "The host monotonic clock is unavailable."
97
+ );
98
+ }
99
+
100
+ OseoResult oseo_internal_clock_start(OseoContext *context) {
101
+ if (context->clock_started) return normal(oseo_undefined());
102
+ clock_open(context);
103
+ uint64_t origin = 0u;
104
+ if (!context->clock_adapter->monotonic(context->clock_state, &origin)) {
105
+ return monotonic_failure(context);
106
+ }
107
+ context->clock_origin = origin;
108
+ context->clock_started = true;
109
+ context->clock_milliseconds = 0u;
110
+ return normal(oseo_undefined());
111
+ }
112
+
113
+ OseoResult oseo_internal_clock_now(
114
+ OseoContext *context,
115
+ uint64_t *milliseconds
116
+ ) {
117
+ OseoResult result = oseo_internal_clock_start(context);
118
+ if (result.status != OSEO_STATUS_NORMAL) return result;
119
+ uint64_t now = 0u;
120
+ if (!context->clock_adapter->monotonic(context->clock_state, &now)) {
121
+ return monotonic_failure(context);
122
+ }
123
+ uint64_t elapsed = now > context->clock_origin
124
+ ? (now - context->clock_origin) / OSEO_NANOSECONDS_PER_MILLISECOND
125
+ : 0u;
126
+ /*
127
+ * An adapter that reports a smaller reading than an earlier one
128
+ * breaks its own contract; the scheduler still never moves backward.
129
+ */
130
+ *milliseconds = elapsed < context->clock_milliseconds
131
+ ? context->clock_milliseconds
132
+ : elapsed;
133
+ return normal(oseo_undefined());
134
+ }
135
+
136
+ OseoResult oseo_internal_clock_wait_until(
137
+ OseoContext *context,
138
+ uint64_t deadline
139
+ ) {
140
+ OseoResult result = oseo_internal_clock_start(context);
141
+ if (result.status != OSEO_STATUS_NORMAL) return result;
142
+ uint64_t span = deadline > UINT64_MAX / OSEO_NANOSECONDS_PER_MILLISECOND
143
+ ? UINT64_MAX
144
+ : deadline * OSEO_NANOSECONDS_PER_MILLISECOND;
145
+ uint64_t target = UINT64_MAX - context->clock_origin < span
146
+ ? UINT64_MAX
147
+ : context->clock_origin + span;
148
+ OseoClockWaitResult outcome = context->clock_adapter->wait(
149
+ context->clock_state,
150
+ true,
151
+ target
152
+ );
153
+ if (outcome == OSEO_CLOCK_WAIT_FAILED) {
154
+ return failure(context, "OSEO2001", "The host clock wait failed.");
155
+ }
156
+ return normal(oseo_undefined());
157
+ }