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