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