@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,435 @@
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
+ /*
31
+ * Realms beyond the initial realm (ADR 0027): realm records and the
32
+ * running realm, GetFunctionRealm and the realm-aware defaults of
33
+ * GetPrototypeFromConstructor, %eval%, and the test262 host's
34
+ * `$262.createRealm`.
35
+ *
36
+ * A context is one agent. Its initial realm lives in the context, and
37
+ * every later realm is a collected heap record. All realms of a context
38
+ * share the heap, the collector, the job queues, the well-known symbols,
39
+ * and the symbol registry; each owns its intrinsic graph, global object,
40
+ * literal caches, and Math.random state. ADR 0029 adds the finite
41
+ * ahead-of-time host Script set: its evaluators and newly created
42
+ * closures run in the realm of the host evalScript function.
43
+ */
44
+
45
+ OseoResult oseo_internal_function_realm(
46
+ OseoContext *context,
47
+ OseoValue constructor
48
+ ) {
49
+ OseoValue current = constructor;
50
+ while (true) {
51
+ if (is_proxy(current)) {
52
+ if (proxy_object(current)->revoked) {
53
+ return oseo_internal_throw_error(
54
+ context,
55
+ OSEO_ERROR_TYPE,
56
+ "Cannot get the realm of a revoked Proxy."
57
+ );
58
+ }
59
+ current = proxy_object(current)->target;
60
+ continue;
61
+ }
62
+ if (!is_function(current)) return normal(context->realm_record);
63
+ if (function_object(current)->function_kind == OSEO_FUNCTION_BOUND) {
64
+ current = function_object(current)->bound_target;
65
+ continue;
66
+ }
67
+ return normal(function_object(current)->realm);
68
+ }
69
+ }
70
+
71
+ OseoValue oseo_internal_callee_realm(OseoContext *context, OseoValue function) {
72
+ OseoValue current = function;
73
+ while (is_callable(current)) {
74
+ if (is_proxy(current)) {
75
+ if (proxy_object(current)->revoked) break;
76
+ current = proxy_object(current)->target;
77
+ continue;
78
+ }
79
+ if (!is_function(current)) break;
80
+ if (function_object(current)->function_kind == OSEO_FUNCTION_BOUND) {
81
+ current = function_object(current)->bound_target;
82
+ continue;
83
+ }
84
+ return function_object(current)->realm;
85
+ }
86
+ return context->realm_record;
87
+ }
88
+
89
+ OseoRealm *oseo_internal_realm_state(
90
+ OseoContext *context,
91
+ OseoValue realm_value
92
+ ) {
93
+ return is_realm_record(realm_value)
94
+ ? &realm_record_object(realm_value)->realm
95
+ : &context->initial_realm;
96
+ }
97
+
98
+ void oseo_internal_realm_enter(
99
+ OseoContext *context,
100
+ OseoRealmScope *scope,
101
+ OseoValue realm_value
102
+ ) {
103
+ scope->realm = context->realm;
104
+ scope->record = context->realm_record;
105
+ scope->frame.previous = NULL;
106
+ scope->frame.slots = &scope->record;
107
+ scope->frame.slot_count = 1u;
108
+ scope->entered = realm_value != context->realm_record;
109
+ if (!scope->entered) return;
110
+ oseo_roots_push(context, &scope->frame);
111
+ context->realm = oseo_internal_realm_state(context, realm_value);
112
+ context->realm_record = realm_value;
113
+ }
114
+
115
+ void oseo_internal_realm_leave(OseoContext *context, OseoRealmScope *scope) {
116
+ if (!scope->entered) return;
117
+ context->realm = oseo_internal_realm_state(context, scope->record);
118
+ context->realm_record = scope->record;
119
+ oseo_roots_pop(context, &scope->frame);
120
+ }
121
+
122
+ OseoResult oseo_internal_realm_intrinsic(
123
+ OseoContext *context,
124
+ OseoValue realm_value,
125
+ OseoIntrinsic intrinsic
126
+ ) {
127
+ OseoRealmScope scope;
128
+ oseo_internal_realm_enter(context, &scope, realm_value);
129
+ OseoResult result = oseo_internal_intrinsic(context, intrinsic);
130
+ oseo_internal_realm_leave(context, &scope);
131
+ return result;
132
+ }
133
+
134
+ OseoResult oseo_internal_constructor_realm_default(
135
+ OseoContext *context,
136
+ OseoValue constructor,
137
+ OseoIntrinsic intrinsic
138
+ ) {
139
+ OseoResult result = oseo_internal_function_realm(context, constructor);
140
+ if (result.status != OSEO_STATUS_NORMAL) return result;
141
+ return oseo_internal_realm_intrinsic(context, result.value, intrinsic);
142
+ }
143
+
144
+ OseoResult oseo_internal_function_receiver(
145
+ OseoContext *context,
146
+ OseoValue constructor,
147
+ OseoValue prototype
148
+ ) {
149
+ if (is_object(prototype) || is_proxy(constructor)) {
150
+ return oseo_constructor_receiver(context, prototype);
151
+ }
152
+ OseoValue slots[1] = {oseo_undefined()};
153
+ OseoRootFrame frame = {NULL, slots, 1u};
154
+ oseo_roots_push(context, &frame);
155
+ OseoResult result = oseo_internal_constructor_realm_default(
156
+ context,
157
+ constructor,
158
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
159
+ );
160
+ slots[0] = result.value;
161
+ if (result.status == OSEO_STATUS_NORMAL) {
162
+ result = oseo_object_create(context, slots[0]);
163
+ }
164
+ oseo_roots_pop(context, &frame);
165
+ return result;
166
+ }
167
+ /* A built-in function of the running realm with one fixed ASCII name. */
168
+ static OseoResult realm_function(
169
+ OseoContext *context,
170
+ size_t code_id,
171
+ const char *name,
172
+ size_t length
173
+ ) {
174
+ size_t name_length = strlen(name);
175
+ uint16_t units[16];
176
+ if (name_length > sizeof(units) / sizeof(*units)) {
177
+ return failure(context, "OSEO2001", "Built-in name is too long.");
178
+ }
179
+ for (size_t index = 0u; index < name_length; index += 1u) {
180
+ units[index] = (uint16_t)(unsigned char)name[index];
181
+ }
182
+ OseoValue environment = oseo_undefined();
183
+ OseoRootFrame frame = {NULL, &environment, 1u};
184
+ oseo_roots_push(context, &frame);
185
+ OseoResult result = oseo_environment_create(context, 0u);
186
+ environment = result.value;
187
+ if (result.status == OSEO_STATUS_NORMAL) {
188
+ result = oseo_function_create(
189
+ context,
190
+ code_id,
191
+ environment,
192
+ units,
193
+ name_length,
194
+ length,
195
+ OSEO_FUNCTION_INTERNAL,
196
+ oseo_undefined(),
197
+ oseo_undefined(),
198
+ OSEO_FUNCTION_NAME_PREFIX_NONE
199
+ );
200
+ }
201
+ oseo_roots_pop(context, &frame);
202
+ return result;
203
+ }
204
+
205
+ OseoResult oseo_internal_eval_intrinsic(OseoContext *context) {
206
+ OseoValue *cache = &context->realm->intrinsics[OSEO_INTRINSIC_EVAL];
207
+ if (tag_of(*cache) != OSEO_TAG_UNDEFINED) return normal(*cache);
208
+ OseoResult result = realm_function(context, OSEO_EVAL_CODE_ID, "eval", 1u);
209
+ if (result.status == OSEO_STATUS_NORMAL) *cache = result.value;
210
+ return result;
211
+ }
212
+
213
+ /* Defines one writable, non-enumerable, configurable data property. */
214
+ static OseoResult define_realm_property(
215
+ OseoContext *context,
216
+ OseoValue object,
217
+ const char *name,
218
+ OseoValue value
219
+ ) {
220
+ OseoValue slots[3] = {object, value, oseo_undefined()};
221
+ OseoRootFrame frame = {NULL, slots, 3u};
222
+ oseo_roots_push(context, &frame);
223
+ OseoResult result = oseo_internal_ascii_string(context, name);
224
+ slots[2] = result.value;
225
+ if (result.status == OSEO_STATUS_NORMAL) {
226
+ result = oseo_object_define(
227
+ context,
228
+ slots[0],
229
+ slots[2],
230
+ slots[1],
231
+ (OseoPropertyAttributes){true, false, true, false}
232
+ );
233
+ }
234
+ oseo_roots_pop(context, &frame);
235
+ return result;
236
+ }
237
+
238
+ OseoResult oseo_internal_realm_global(
239
+ OseoContext *context,
240
+ OseoValue realm_value
241
+ ) {
242
+ OseoRealmScope scope;
243
+ oseo_internal_realm_enter(context, &scope, realm_value);
244
+ OseoResult result = oseo_this_value(context, oseo_undefined());
245
+ oseo_internal_realm_leave(context, &scope);
246
+ return result;
247
+ }
248
+
249
+ OseoResult oseo_internal_realm_create(OseoContext *context) {
250
+ OseoRealmRecord *record =
251
+ oseo_internal_allocate_heap_bytes(context, sizeof(*record));
252
+ if (record == NULL) {
253
+ return failure(context, "OSEO2001", "Realm allocation failed.");
254
+ }
255
+ oseo_internal_realm_init(&record->realm);
256
+ OseoResult result = oseo_internal_publish_heap(
257
+ context,
258
+ &record->header,
259
+ OSEO_HEAP_REALM
260
+ );
261
+ if (result.status != OSEO_STATUS_NORMAL) return result;
262
+ OseoValue realm = result.value;
263
+ OseoRootFrame frame = {NULL, &realm, 1u};
264
+ oseo_roots_push(context, &frame);
265
+ /* SetDefaultGlobalBindings runs as part of creating the realm, so a
266
+ * realm never exists without its global object. */
267
+ result = oseo_internal_realm_global(context, realm);
268
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(realm);
269
+ oseo_roots_pop(context, &frame);
270
+ return result;
271
+ }
272
+
273
+ OseoResult oseo_internal_realm_host_install(
274
+ OseoContext *context,
275
+ OseoValue host
276
+ ) {
277
+ OseoValue slots[2] = {host, oseo_undefined()};
278
+ OseoRootFrame frame = {NULL, slots, 2u};
279
+ oseo_roots_push(context, &frame);
280
+ OseoResult result = realm_function(
281
+ context,
282
+ OSEO_CREATE_REALM_CODE_ID,
283
+ "createRealm",
284
+ 0u
285
+ );
286
+ slots[1] = result.value;
287
+ if (result.status == OSEO_STATUS_NORMAL) {
288
+ result = define_realm_property(
289
+ context,
290
+ slots[0],
291
+ "createRealm",
292
+ slots[1]
293
+ );
294
+ }
295
+ if (result.status == OSEO_STATUS_NORMAL) {
296
+ result = realm_function(context, OSEO_HOST_EVAL_SCRIPT_CODE_ID,
297
+ "evalScript", 1u);
298
+ slots[1] = result.value;
299
+ }
300
+ if (result.status == OSEO_STATUS_NORMAL) {
301
+ result = define_realm_property(
302
+ context, slots[0], "evalScript", slots[1]);
303
+ }
304
+ if (result.status == OSEO_STATUS_NORMAL) {
305
+ result = oseo_this_value(context, oseo_undefined());
306
+ slots[1] = result.value;
307
+ }
308
+ if (result.status == OSEO_STATUS_NORMAL) {
309
+ result = define_realm_property(context, slots[0], "global", slots[1]);
310
+ }
311
+ if (result.status == OSEO_STATUS_NORMAL) {
312
+ result = define_realm_property(context, context->realm->global_this,
313
+ "$262", slots[0]);
314
+ }
315
+ if (result.status == OSEO_STATUS_NORMAL) {
316
+ result = realm_function(context, OSEO_HOST_PRINT_CODE_ID, "print", 1u);
317
+ slots[1] = result.value;
318
+ }
319
+ if (result.status == OSEO_STATUS_NORMAL) {
320
+ result = define_realm_property(context, context->realm->global_this,
321
+ "print", slots[1]);
322
+ }
323
+ oseo_roots_pop(context, &frame);
324
+ return result;
325
+ }
326
+
327
+ /*
328
+ * `$262.createRealm()`: a new realm and, created inside it, that realm's
329
+ * own host object, whose `global` is the new global object and whose
330
+ * `createRealm` belongs to the new realm. The host object carries no
331
+ * `agent`, because agents belong to the cluster's main host object.
332
+ */
333
+ static OseoResult create_realm(OseoContext *context) {
334
+ OseoResult result = oseo_internal_realm_create(context);
335
+ if (result.status != OSEO_STATUS_NORMAL) return result;
336
+ OseoValue slots[2] = {result.value, oseo_undefined()};
337
+ OseoRootFrame frame = {NULL, slots, 2u};
338
+ oseo_roots_push(context, &frame);
339
+ OseoRealmScope scope;
340
+ oseo_internal_realm_enter(context, &scope, slots[0]);
341
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_OBJECT_PROTOTYPE);
342
+ if (result.status == OSEO_STATUS_NORMAL) {
343
+ result = oseo_object_create(context, result.value);
344
+ slots[1] = result.value;
345
+ }
346
+ if (result.status == OSEO_STATUS_NORMAL) {
347
+ result = oseo_internal_realm_host_install(context, slots[1]);
348
+ }
349
+ oseo_internal_realm_leave(context, &scope);
350
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(slots[1]);
351
+ oseo_roots_pop(context, &frame);
352
+ return result;
353
+ }
354
+
355
+ /* Source selection is exact UTF-16 equality, never runtime compilation. */
356
+ static OseoResult host_eval_script(OseoContext *context, OseoValue source) {
357
+ if (!is_string(source)) {
358
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE,
359
+ "evalScript requires a String source.");
360
+ }
361
+ const OseoString *string = string_object(source);
362
+ for (size_t index = 0u; index < context->host_script_count; index += 1u) {
363
+ const OseoHostScript *script = &context->host_scripts[index];
364
+ if (string->length != script->source_length ||
365
+ (string->length > 0u && memcmp(string->units, script->source,
366
+ string->length * sizeof(uint16_t)) != 0)) continue;
367
+ if (script->evaluate == NULL) {
368
+ if (script->syntax_error) {
369
+ return oseo_internal_throw_error(context, OSEO_ERROR_SYNTAX,
370
+ script->message == NULL
371
+ ? "Invalid Script." : script->message);
372
+ }
373
+ return failure(context, "OSEO1001", script->message == NULL ?
374
+ "Host Script syntax is outside the admitted profile." :
375
+ script->message);
376
+ }
377
+ OseoResult result = oseo_frame_enter(context, script->frame_cost);
378
+ if (result.status != OSEO_STATUS_NORMAL) return result;
379
+ result = script->evaluate(context, oseo_undefined(), oseo_undefined(),
380
+ 0u, NULL, oseo_undefined());
381
+ oseo_frame_leave(context, script->frame_cost);
382
+ return result;
383
+ }
384
+ return failure(context, "OSEO1001",
385
+ "Host Script source outside the ahead-of-time Script set "
386
+ "is not admitted.");
387
+ }
388
+
389
+ OseoResult oseo_internal_realm_builtin_dispatch(
390
+ OseoContext *context,
391
+ size_t code_id,
392
+ OseoValue callee,
393
+ OseoValue receiver,
394
+ size_t argument_count,
395
+ const OseoValue *arguments,
396
+ OseoValue new_target
397
+ ) {
398
+ (void)callee;
399
+ (void)receiver;
400
+ (void)new_target;
401
+ /* ADR 0016 keeps every form that compiles source text at run time
402
+ * outside the profile. %eval% is a complete function value in every
403
+ * realm. PerformEval returns a non-String argument unchanged without
404
+ * parsing anything, and only a String, which it would parse as a
405
+ * Script, reports the boundary. */
406
+ if (code_id == OSEO_EVAL_CODE_ID) {
407
+ OseoValue source = argument_count > 0u
408
+ ? arguments[0]
409
+ : oseo_undefined();
410
+ if (!is_string(source)) return normal(source);
411
+ return failure(
412
+ context,
413
+ "OSEO1001",
414
+ "eval compiles source text at run time, which is outside the "
415
+ "admitted profile."
416
+ );
417
+ }
418
+ if (code_id == OSEO_CREATE_REALM_CODE_ID) return create_realm(context);
419
+ if (code_id == OSEO_HOST_EVAL_SCRIPT_CODE_ID) {
420
+ return host_eval_script(context,
421
+ argument_count > 0u ? arguments[0] : oseo_undefined());
422
+ }
423
+ if (code_id == OSEO_HOST_PRINT_CODE_ID) {
424
+ OseoResult result = oseo_internal_value_string(context,
425
+ argument_count > 0u ? arguments[0] : oseo_undefined());
426
+ if (result.status != OSEO_STATUS_NORMAL) return result;
427
+ OseoValue string = result.value;
428
+ OseoRootFrame frame = {NULL, &string, 1u};
429
+ oseo_roots_push(context, &frame);
430
+ result = oseo_console_log(context, 1u, &string);
431
+ oseo_roots_pop(context, &frame);
432
+ return result;
433
+ }
434
+ return oseo_unknown_function(context, code_id);
435
+ }