@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,676 @@
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
+ /*
9
+ * The four URI handling functions of 19.2.6: encodeURI,
10
+ * encodeURIComponent, decodeURI, and decodeURIComponent, together with
11
+ * the Encode and Decode operations they share and the UTF-8
12
+ * transformation both directions run, and the Annex B escape and
13
+ * unescape functions of B.2.1. Each function is an ordinary built-in
14
+ * function that is not a constructor, and the global object binds each
15
+ * as a writable, non-enumerable, configurable property.
16
+ */
17
+
18
+ /*
19
+ * One global string function. The array order is the order the global
20
+ * installation creates its properties in and the order the code IDs
21
+ * count down from `OSEO_URI_FUNCTION_CODE_ID_LAST`, so an entry's index
22
+ * is the only identity the builder and the dispatcher share.
23
+ */
24
+ typedef enum {
25
+ OSEO_URI_DECODE_URI = 0,
26
+ OSEO_URI_DECODE_URI_COMPONENT = 1,
27
+ OSEO_URI_ENCODE_URI = 2,
28
+ OSEO_URI_ENCODE_URI_COMPONENT = 3,
29
+ OSEO_URI_ESCAPE = 4,
30
+ OSEO_URI_UNESCAPE = 5,
31
+ } OseoUriOperation;
32
+
33
+ typedef struct {
34
+ const char *name;
35
+ OseoIntrinsic intrinsic;
36
+ } OseoUriFunction;
37
+
38
+ static const OseoUriFunction uri_functions[] = {
39
+ {"decodeURI", OSEO_INTRINSIC_DECODE_URI},
40
+ {"decodeURIComponent", OSEO_INTRINSIC_DECODE_URI_COMPONENT},
41
+ {"encodeURI", OSEO_INTRINSIC_ENCODE_URI},
42
+ {"encodeURIComponent", OSEO_INTRINSIC_ENCODE_URI_COMPONENT},
43
+ {"escape", OSEO_INTRINSIC_ESCAPE},
44
+ {"unescape", OSEO_INTRINSIC_UNESCAPE},
45
+ };
46
+
47
+ _Static_assert(
48
+ sizeof(uri_functions) / sizeof(uri_functions[0]) ==
49
+ OSEO_URI_FUNCTION_COUNT,
50
+ "The URI function table must match its reviewed code-ID range."
51
+ );
52
+
53
+ /*
54
+ * The reserved code units both `encodeURI` and `decodeURI` name. Encode
55
+ * leaves them unescaped and Decode preserves their escape sequences, so
56
+ * one round trip through the pair is the identity on them.
57
+ */
58
+ static const char uri_reserved_units[] = ";/?:@&=+$,#";
59
+
60
+ /*
61
+ * The eight marks 19.2.6.5 leaves unescaped beside the ASCII word
62
+ * characters, which are the letters, the digits, and the underscore.
63
+ */
64
+ static const char uri_unreserved_marks[] = "-.!~*'()";
65
+
66
+ /*
67
+ * True when `unit` is one of the ASCII units `set` lists. A unit outside
68
+ * ASCII can never be one, so the comparison never truncates a code unit
69
+ * into a char.
70
+ */
71
+ static bool uri_member(const char *set, uint16_t unit) {
72
+ if (unit == 0u || unit > 0x7fu) return false;
73
+ for (const char *entry = set; *entry != '\0'; entry += 1) {
74
+ if ((uint16_t)(unsigned char)*entry == unit) return true;
75
+ }
76
+ return false;
77
+ }
78
+
79
+ /*
80
+ * The unreserved set every Encode leaves alone: the ASCII word
81
+ * characters and the eight marks 19.2.6.5 names. `encodeURIComponent`
82
+ * escapes everything else.
83
+ */
84
+ static bool uri_always_unescaped(uint16_t unit) {
85
+ if (unit >= (uint16_t)'A' && unit <= (uint16_t)'Z') return true;
86
+ if (unit >= (uint16_t)'a' && unit <= (uint16_t)'z') return true;
87
+ if (unit >= (uint16_t)'0' && unit <= (uint16_t)'9') return true;
88
+ if (unit == (uint16_t)'_') return true;
89
+ return uri_member(uri_unreserved_marks, unit);
90
+ }
91
+
92
+ /* True when `unit` is one of the reserved units listed above. */
93
+ static bool uri_reserved(uint16_t unit) {
94
+ return uri_member(uri_reserved_units, unit);
95
+ }
96
+
97
+ /* The URIError every malformed operand raises, 19.2.6.5 and 19.2.6.6. */
98
+ static OseoResult uri_error(OseoContext *context, const char *message) {
99
+ return oseo_internal_throw_error(context, OSEO_ERROR_URI, message);
100
+ }
101
+
102
+ /*
103
+ * The UTF-8 octets of one Unicode code point, written into `octets` and
104
+ * counted by the return value. The code point is already known to be a
105
+ * scalar value, so the encoding is between one and four octets.
106
+ */
107
+ static size_t uri_utf8_octets(uint32_t code_point, uint8_t octets[4]) {
108
+ if (code_point <= 0x7fu) {
109
+ octets[0] = (uint8_t)code_point;
110
+ return 1u;
111
+ }
112
+ if (code_point <= 0x7ffu) {
113
+ octets[0] = (uint8_t)(0xc0u | (code_point >> 6u));
114
+ octets[1] = (uint8_t)(0x80u | (code_point & 0x3fu));
115
+ return 2u;
116
+ }
117
+ if (code_point <= 0xffffu) {
118
+ octets[0] = (uint8_t)(0xe0u | (code_point >> 12u));
119
+ octets[1] = (uint8_t)(0x80u | ((code_point >> 6u) & 0x3fu));
120
+ octets[2] = (uint8_t)(0x80u | (code_point & 0x3fu));
121
+ return 3u;
122
+ }
123
+ octets[0] = (uint8_t)(0xf0u | (code_point >> 18u));
124
+ octets[1] = (uint8_t)(0x80u | ((code_point >> 12u) & 0x3fu));
125
+ octets[2] = (uint8_t)(0x80u | ((code_point >> 6u) & 0x3fu));
126
+ octets[3] = (uint8_t)(0x80u | (code_point & 0x3fu));
127
+ return 4u;
128
+ }
129
+
130
+ /*
131
+ * Decode one well-formed UTF-8 sequence of `count` octets into
132
+ * `code_point`, reporting false for every sequence the Unicode standard
133
+ * does not admit. That rejects a continuation octet out of the 0x80 to
134
+ * 0xBF range, an overlong encoding, a surrogate code point, and a value
135
+ * past U+10FFFF, which is what "a valid UTF-8 encoding of a Unicode code
136
+ * point" means in step 4.c.viii.6 of Decode.
137
+ */
138
+ static bool uri_utf8_code_point(
139
+ const uint8_t *octets,
140
+ size_t count,
141
+ uint32_t *code_point
142
+ ) {
143
+ for (size_t index = 1u; index < count; index += 1u) {
144
+ if (octets[index] < 0x80u || octets[index] > 0xbfu) return false;
145
+ }
146
+ uint32_t value;
147
+ if (count == 2u) {
148
+ value = ((uint32_t)(octets[0] & 0x1fu) << 6u) |
149
+ (uint32_t)(octets[1] & 0x3fu);
150
+ if (value < 0x80u) return false;
151
+ } else if (count == 3u) {
152
+ value = ((uint32_t)(octets[0] & 0x0fu) << 12u) |
153
+ ((uint32_t)(octets[1] & 0x3fu) << 6u) |
154
+ (uint32_t)(octets[2] & 0x3fu);
155
+ if (value < 0x800u) return false;
156
+ if (value >= 0xd800u && value <= 0xdfffu) return false;
157
+ } else {
158
+ value = ((uint32_t)(octets[0] & 0x07u) << 18u) |
159
+ ((uint32_t)(octets[1] & 0x3fu) << 12u) |
160
+ ((uint32_t)(octets[2] & 0x3fu) << 6u) |
161
+ (uint32_t)(octets[3] & 0x3fu);
162
+ if (value < 0x10000u || value > 0x10ffffu) return false;
163
+ }
164
+ *code_point = value;
165
+ return true;
166
+ }
167
+
168
+ /* The value of one hexadecimal digit, or 16 for every other unit. */
169
+ static unsigned uri_hex_digit(uint16_t unit) {
170
+ if (unit >= (uint16_t)'0' && unit <= (uint16_t)'9') {
171
+ return (unsigned)(unit - (uint16_t)'0');
172
+ }
173
+ if (unit >= (uint16_t)'A' && unit <= (uint16_t)'F') {
174
+ return (unsigned)(unit - (uint16_t)'A') + 10u;
175
+ }
176
+ if (unit >= (uint16_t)'a' && unit <= (uint16_t)'f') {
177
+ return (unsigned)(unit - (uint16_t)'a') + 10u;
178
+ }
179
+ return 16u;
180
+ }
181
+
182
+ /*
183
+ * ParseHexOctet(string, position), 19.2.6.4. The caller has already
184
+ * checked that two units follow `position`. A unit pair the HexDigits
185
+ * grammar does not accept reports false rather than an octet.
186
+ */
187
+ static bool uri_hex_octet(
188
+ const uint16_t *units,
189
+ size_t position,
190
+ uint8_t *octet
191
+ ) {
192
+ unsigned high = uri_hex_digit(units[position]);
193
+ unsigned low = uri_hex_digit(units[position + 1u]);
194
+ if (high > 15u || low > 15u) return false;
195
+ *octet = (uint8_t)(high * 16u + low);
196
+ return true;
197
+ }
198
+
199
+ /* Encode(string, extraUnescaped), 19.2.6.5, appended into `builder`. */
200
+ static OseoResult uri_encode(
201
+ OseoContext *context,
202
+ OseoValue string_value,
203
+ bool escape_reserved,
204
+ OseoStringBuilder *builder
205
+ ) {
206
+ static const char hex_digits[] = "0123456789ABCDEF";
207
+ const OseoString *string = string_object(string_value);
208
+ size_t length = string->length;
209
+ size_t index = 0u;
210
+ OseoResult result = normal(oseo_undefined());
211
+ while (index < length) {
212
+ uint16_t unit = string->units[index];
213
+ if (uri_always_unescaped(unit) ||
214
+ (!escape_reserved && uri_reserved(unit))) {
215
+ result = oseo_internal_string_builder_append(
216
+ context,
217
+ builder,
218
+ &unit,
219
+ 1u
220
+ );
221
+ if (result.status != OSEO_STATUS_NORMAL) return result;
222
+ index += 1u;
223
+ continue;
224
+ }
225
+ /* CodePointAt(string, index), 11.1.4. */
226
+ uint32_t code_point = (uint32_t)unit;
227
+ size_t consumed = 1u;
228
+ if (unit >= 0xd800u && unit <= 0xdbffu && index + 1u < length &&
229
+ string->units[index + 1u] >= 0xdc00u &&
230
+ string->units[index + 1u] <= 0xdfffu) {
231
+ code_point = 0x10000u +
232
+ (((uint32_t)unit - 0xd800u) << 10u) +
233
+ ((uint32_t)string->units[index + 1u] - 0xdc00u);
234
+ consumed = 2u;
235
+ } else if (unit >= 0xd800u && unit <= 0xdfffu) {
236
+ return uri_error(
237
+ context,
238
+ "URI encoding received an unpaired surrogate."
239
+ );
240
+ }
241
+ index += consumed;
242
+ uint8_t octets[4];
243
+ size_t octet_count = uri_utf8_octets(code_point, octets);
244
+ for (size_t octet = 0u; octet < octet_count; octet += 1u) {
245
+ uint16_t escape[3] = {
246
+ (uint16_t)'%',
247
+ (uint16_t)hex_digits[octets[octet] >> 4u],
248
+ (uint16_t)hex_digits[octets[octet] & 0x0fu],
249
+ };
250
+ result = oseo_internal_string_builder_append(
251
+ context,
252
+ builder,
253
+ escape,
254
+ 3u
255
+ );
256
+ if (result.status != OSEO_STATUS_NORMAL) return result;
257
+ }
258
+ }
259
+ return result;
260
+ }
261
+
262
+ /* Decode(string, preserveEscapeSet), 19.2.6.6, appended into `builder`. */
263
+ static OseoResult uri_decode(
264
+ OseoContext *context,
265
+ OseoValue string_value,
266
+ bool preserve_reserved,
267
+ OseoStringBuilder *builder
268
+ ) {
269
+ const OseoString *string = string_object(string_value);
270
+ size_t length = string->length;
271
+ size_t index = 0u;
272
+ OseoResult result = normal(oseo_undefined());
273
+ while (index < length) {
274
+ uint16_t unit = string->units[index];
275
+ if (unit != (uint16_t)'%') {
276
+ result = oseo_internal_string_builder_append(
277
+ context,
278
+ builder,
279
+ &unit,
280
+ 1u
281
+ );
282
+ if (result.status != OSEO_STATUS_NORMAL) return result;
283
+ index += 1u;
284
+ continue;
285
+ }
286
+ size_t escape_start = index;
287
+ uint8_t octets[4];
288
+ if (index + 3u > length ||
289
+ !uri_hex_octet(string->units, index + 1u, &octets[0])) {
290
+ return uri_error(
291
+ context,
292
+ "URI decoding received a malformed escape sequence."
293
+ );
294
+ }
295
+ index += 2u;
296
+ /* The number of leading 1 bits in the first octet. */
297
+ size_t sequence_length = 0u;
298
+ for (uint8_t probe = octets[0]; (probe & 0x80u) != 0u; probe <<= 1u) {
299
+ sequence_length += 1u;
300
+ }
301
+ if (sequence_length == 0u) {
302
+ uint16_t decoded = (uint16_t)octets[0];
303
+ if (preserve_reserved && uri_reserved(decoded)) {
304
+ result = oseo_internal_string_builder_append(
305
+ context,
306
+ builder,
307
+ string->units + escape_start,
308
+ 3u
309
+ );
310
+ } else {
311
+ result = oseo_internal_string_builder_append(
312
+ context,
313
+ builder,
314
+ &decoded,
315
+ 1u
316
+ );
317
+ }
318
+ if (result.status != OSEO_STATUS_NORMAL) return result;
319
+ index += 1u;
320
+ continue;
321
+ }
322
+ if (sequence_length == 1u || sequence_length > 4u) {
323
+ return uri_error(
324
+ context,
325
+ "URI decoding received a malformed UTF-8 sequence."
326
+ );
327
+ }
328
+ for (size_t octet = 1u; octet < sequence_length; octet += 1u) {
329
+ index += 1u;
330
+ if (index + 3u > length ||
331
+ string->units[index] != (uint16_t)'%' ||
332
+ !uri_hex_octet(string->units, index + 1u, &octets[octet])) {
333
+ return uri_error(
334
+ context,
335
+ "URI decoding received a malformed escape sequence."
336
+ );
337
+ }
338
+ index += 2u;
339
+ }
340
+ uint32_t code_point = 0u;
341
+ if (!uri_utf8_code_point(octets, sequence_length, &code_point)) {
342
+ return uri_error(
343
+ context,
344
+ "URI decoding received a malformed UTF-8 sequence."
345
+ );
346
+ }
347
+ /* UTF16EncodeCodePoint(code_point), 11.1.1. */
348
+ if (code_point <= 0xffffu) {
349
+ uint16_t decoded = (uint16_t)code_point;
350
+ result = oseo_internal_string_builder_append(
351
+ context,
352
+ builder,
353
+ &decoded,
354
+ 1u
355
+ );
356
+ } else {
357
+ uint32_t remainder = code_point - 0x10000u;
358
+ uint16_t pair[2] = {
359
+ (uint16_t)(0xd800u + (remainder >> 10u)),
360
+ (uint16_t)(0xdc00u + (remainder & 0x3ffu)),
361
+ };
362
+ result = oseo_internal_string_builder_append(
363
+ context,
364
+ builder,
365
+ pair,
366
+ 2u
367
+ );
368
+ }
369
+ if (result.status != OSEO_STATUS_NORMAL) return result;
370
+ index += 1u;
371
+ }
372
+ return result;
373
+ }
374
+
375
+ /*
376
+ * The code units escape(string) leaves unescaped, B.2.1.1 step 4: the
377
+ * ASCII word characters and the six marks `@*+-./`. Unlike Encode, the
378
+ * set is fixed and every other code unit is escaped one unit at a time.
379
+ */
380
+ static bool escape_unescaped(uint16_t unit) {
381
+ if (unit >= (uint16_t)'A' && unit <= (uint16_t)'Z') return true;
382
+ if (unit >= (uint16_t)'a' && unit <= (uint16_t)'z') return true;
383
+ if (unit >= (uint16_t)'0' && unit <= (uint16_t)'9') return true;
384
+ if (unit == (uint16_t)'_') return true;
385
+ return uri_member("@*+-./", unit);
386
+ }
387
+
388
+ /*
389
+ * escape(string), B.2.1.1, appended into `builder`. A code unit below
390
+ * 256 becomes `%XX` and every other unit `%uXXXX`, both with uppercase
391
+ * hexadecimal digits. Surrogates are code units like any other, so a
392
+ * lone surrogate is escaped rather than rejected.
393
+ */
394
+ static OseoResult escape_string(
395
+ OseoContext *context,
396
+ OseoValue string_value,
397
+ OseoStringBuilder *builder
398
+ ) {
399
+ static const char hex_digits[] = "0123456789ABCDEF";
400
+ const OseoString *string = string_object(string_value);
401
+ OseoResult result = normal(oseo_undefined());
402
+ for (size_t index = 0u; index < string->length; index += 1u) {
403
+ uint16_t unit = string->units[index];
404
+ uint16_t escaped[6];
405
+ size_t count = 0u;
406
+ if (escape_unescaped(unit)) {
407
+ escaped[count++] = unit;
408
+ } else if (unit < 256u) {
409
+ escaped[count++] = (uint16_t)'%';
410
+ escaped[count++] = (uint16_t)hex_digits[unit >> 4u];
411
+ escaped[count++] = (uint16_t)hex_digits[unit & 0x0fu];
412
+ } else {
413
+ escaped[count++] = (uint16_t)'%';
414
+ escaped[count++] = (uint16_t)'u';
415
+ escaped[count++] = (uint16_t)hex_digits[unit >> 12u];
416
+ escaped[count++] = (uint16_t)hex_digits[(unit >> 8u) & 0x0fu];
417
+ escaped[count++] = (uint16_t)hex_digits[(unit >> 4u) & 0x0fu];
418
+ escaped[count++] = (uint16_t)hex_digits[unit & 0x0fu];
419
+ }
420
+ result = oseo_internal_string_builder_append(
421
+ context,
422
+ builder,
423
+ escaped,
424
+ count
425
+ );
426
+ if (result.status != OSEO_STATUS_NORMAL) return result;
427
+ }
428
+ return result;
429
+ }
430
+
431
+ /*
432
+ * The value of `count` hexadecimal digits starting at `position`, or
433
+ * false when one of them is not a hexadecimal digit. The caller has
434
+ * already checked that the units exist.
435
+ */
436
+ static bool unescape_hex_value(
437
+ const uint16_t *units,
438
+ size_t position,
439
+ size_t count,
440
+ uint16_t *value
441
+ ) {
442
+ unsigned accumulated = 0u;
443
+ for (size_t index = 0u; index < count; index += 1u) {
444
+ unsigned digit = uri_hex_digit(units[position + index]);
445
+ if (digit > 15u) return false;
446
+ accumulated = accumulated * 16u + digit;
447
+ }
448
+ *value = (uint16_t)accumulated;
449
+ return true;
450
+ }
451
+
452
+ /*
453
+ * unescape(string), B.2.1.2, appended into `builder`. A `%uXXXX` form
454
+ * is tried before a `%XX` form at the same position, and a `%` that
455
+ * starts neither stays in the result unchanged, so unescape never
456
+ * throws after its conversion.
457
+ */
458
+ static OseoResult unescape_string(
459
+ OseoContext *context,
460
+ OseoValue string_value,
461
+ OseoStringBuilder *builder
462
+ ) {
463
+ const OseoString *string = string_object(string_value);
464
+ size_t length = string->length;
465
+ OseoResult result = normal(oseo_undefined());
466
+ size_t index = 0u;
467
+ while (index < length) {
468
+ uint16_t unit = string->units[index];
469
+ size_t consumed = 1u;
470
+ if (unit == (uint16_t)'%') {
471
+ uint16_t value = 0u;
472
+ if (index + 6u <= length &&
473
+ string->units[index + 1u] == (uint16_t)'u' &&
474
+ unescape_hex_value(string->units, index + 2u, 4u, &value)) {
475
+ unit = value;
476
+ consumed = 6u;
477
+ } else if (index + 3u <= length &&
478
+ unescape_hex_value(
479
+ string->units,
480
+ index + 1u,
481
+ 2u,
482
+ &value
483
+ )) {
484
+ unit = value;
485
+ consumed = 3u;
486
+ }
487
+ }
488
+ result = oseo_internal_string_builder_append(
489
+ context,
490
+ builder,
491
+ &unit,
492
+ 1u
493
+ );
494
+ if (result.status != OSEO_STATUS_NORMAL) return result;
495
+ index += consumed;
496
+ }
497
+ return result;
498
+ }
499
+
500
+ /*
501
+ * One global string function call. Every operation converts its single
502
+ * argument with ToString first, so an abrupt conversion is observed
503
+ * before any escape sequence is inspected.
504
+ */
505
+ static OseoResult uri_call(
506
+ OseoContext *context,
507
+ size_t operation,
508
+ size_t argument_count,
509
+ const OseoValue *arguments
510
+ ) {
511
+ if (argument_count > 0u && arguments == NULL) {
512
+ return failure(context, "OSEO2001", "URI arguments are missing.");
513
+ }
514
+ OseoValue slots[1] = {
515
+ argument_count > 0u ? arguments[0] : oseo_undefined(),
516
+ };
517
+ OseoRootFrame frame = {NULL, slots, 1u};
518
+ oseo_roots_push(context, &frame);
519
+ OseoStringBuilder builder = {NULL, 0u, 0u};
520
+ OseoResult result = oseo_internal_value_string(context, slots[0]);
521
+ slots[0] = result.value;
522
+ if (result.status == OSEO_STATUS_NORMAL) {
523
+ if (operation == OSEO_URI_ESCAPE) {
524
+ result = escape_string(context, slots[0], &builder);
525
+ } else if (operation == OSEO_URI_UNESCAPE) {
526
+ result = unescape_string(context, slots[0], &builder);
527
+ } else if (operation == OSEO_URI_ENCODE_URI ||
528
+ operation == OSEO_URI_ENCODE_URI_COMPONENT) {
529
+ result = uri_encode(
530
+ context,
531
+ slots[0],
532
+ operation == OSEO_URI_ENCODE_URI_COMPONENT,
533
+ &builder
534
+ );
535
+ } else {
536
+ result = uri_decode(
537
+ context,
538
+ slots[0],
539
+ operation == OSEO_URI_DECODE_URI,
540
+ &builder
541
+ );
542
+ }
543
+ }
544
+ if (result.status == OSEO_STATUS_NORMAL) {
545
+ result = oseo_internal_allocate_string(
546
+ context,
547
+ builder.units,
548
+ builder.length
549
+ );
550
+ }
551
+ oseo_internal_string_builder_release(&builder);
552
+ oseo_roots_pop(context, &frame);
553
+ return result;
554
+ }
555
+
556
+ OseoResult oseo_internal_uri_builtin_dispatch(
557
+ OseoContext *context,
558
+ size_t code_id,
559
+ OseoValue callee,
560
+ OseoValue receiver,
561
+ size_t argument_count,
562
+ const OseoValue *arguments,
563
+ OseoValue new_target
564
+ ) {
565
+ (void)callee;
566
+ (void)receiver;
567
+ if (code_id < OSEO_URI_FUNCTION_CODE_ID_FIRST ||
568
+ code_id > OSEO_URI_FUNCTION_CODE_ID_LAST) {
569
+ return oseo_unknown_function(context, code_id);
570
+ }
571
+ size_t operation = OSEO_URI_FUNCTION_CODE_ID_LAST - code_id;
572
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
573
+ bool annex_b = operation == OSEO_URI_ESCAPE ||
574
+ operation == OSEO_URI_UNESCAPE;
575
+ return oseo_internal_throw_error(
576
+ context,
577
+ OSEO_ERROR_TYPE,
578
+ annex_b
579
+ ? "Escape function is not a constructor."
580
+ : "URI handling function is not a constructor."
581
+ );
582
+ }
583
+ return uri_call(context, operation, argument_count, arguments);
584
+ }
585
+
586
+ /*
587
+ * The table index of one global string function intrinsic. The URI four
588
+ * are dense from OSEO_INTRINSIC_DECODE_URI; the Annex B pair took the
589
+ * next free realm slots and follows them in the table.
590
+ */
591
+ static size_t uri_operation(OseoIntrinsic intrinsic) {
592
+ if (intrinsic == OSEO_INTRINSIC_ESCAPE) return OSEO_URI_ESCAPE;
593
+ if (intrinsic == OSEO_INTRINSIC_UNESCAPE) return OSEO_URI_UNESCAPE;
594
+ return (size_t)(intrinsic - OSEO_INTRINSIC_DECODE_URI);
595
+ }
596
+
597
+ OseoResult oseo_internal_uri_intrinsic(
598
+ OseoContext *context,
599
+ OseoIntrinsic intrinsic
600
+ ) {
601
+ OseoValue *slot = &context->realm->intrinsics[intrinsic];
602
+ if (is_object(*slot)) return normal(*slot);
603
+ size_t operation = uri_operation(intrinsic);
604
+ const OseoUriFunction *entry = &uri_functions[operation];
605
+ size_t name_length = strlen(entry->name);
606
+ uint16_t units[24];
607
+ if (name_length > sizeof(units) / sizeof(*units)) {
608
+ return failure(context, "OSEO2001", "Built-in name is too long.");
609
+ }
610
+ for (size_t index = 0u; index < name_length; index += 1u) {
611
+ units[index] = (uint16_t)(unsigned char)entry->name[index];
612
+ }
613
+ size_t entry_allocations = context->allocations;
614
+ OseoValue environment = oseo_undefined();
615
+ OseoRootFrame frame = {NULL, &environment, 1u};
616
+ oseo_roots_push(context, &frame);
617
+ OseoResult result = oseo_environment_create(context, 0u);
618
+ environment = result.value;
619
+ if (result.status == OSEO_STATUS_NORMAL) {
620
+ result = oseo_function_create(
621
+ context,
622
+ OSEO_URI_FUNCTION_CODE_ID_LAST - operation,
623
+ environment,
624
+ units,
625
+ name_length,
626
+ 1u,
627
+ OSEO_FUNCTION_INTERNAL,
628
+ oseo_undefined(),
629
+ oseo_undefined(),
630
+ OSEO_FUNCTION_NAME_PREFIX_NONE
631
+ );
632
+ }
633
+ oseo_roots_pop(context, &frame);
634
+ if (result.status != OSEO_STATUS_NORMAL) return result;
635
+ *slot = result.value;
636
+ if (context->observe_specialization) {
637
+ context->allocations = entry_allocations;
638
+ }
639
+ return normal(*slot);
640
+ }
641
+
642
+ OseoResult oseo_internal_install_uri_global(
643
+ OseoContext *context,
644
+ OseoValue global
645
+ ) {
646
+ OseoValue slots[3] = {global, oseo_undefined(), oseo_undefined()};
647
+ OseoRootFrame frame = {NULL, slots, 3u};
648
+ oseo_roots_push(context, &frame);
649
+ OseoResult result = normal(slots[0]);
650
+ for (size_t index = 0u;
651
+ result.status == OSEO_STATUS_NORMAL &&
652
+ index < OSEO_URI_FUNCTION_COUNT;
653
+ index += 1u) {
654
+ result = oseo_internal_uri_intrinsic(
655
+ context,
656
+ uri_functions[index].intrinsic
657
+ );
658
+ slots[1] = result.value;
659
+ if (result.status != OSEO_STATUS_NORMAL) break;
660
+ result = oseo_internal_ascii_string(
661
+ context,
662
+ uri_functions[index].name
663
+ );
664
+ slots[2] = result.value;
665
+ if (result.status != OSEO_STATUS_NORMAL) break;
666
+ result = oseo_object_define(
667
+ context,
668
+ slots[0],
669
+ slots[2],
670
+ slots[1],
671
+ (OseoPropertyAttributes){true, false, true, false}
672
+ );
673
+ }
674
+ oseo_roots_pop(context, &frame);
675
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
676
+ }