@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.
- package/LICENSE +674 -0
- package/MIT-LICENSE.txt +21 -0
- package/README.md +28 -2
- package/UNICODE-LICENSE.txt +39 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +269 -0
- package/dist/index.js.map +1 -0
- package/native/oseo_runtime.h +2618 -0
- package/native/runtime_agent.c +1717 -0
- package/native/runtime_arguments.c +379 -0
- package/native/runtime_array.c +5766 -0
- package/native/runtime_array_buffer.c +1748 -0
- package/native/runtime_async_generator.c +972 -0
- package/native/runtime_atomics.c +1283 -0
- package/native/runtime_bigint.c +1387 -0
- package/native/runtime_bigint_object.c +626 -0
- package/native/runtime_binding.c +1029 -0
- package/native/runtime_boolean.c +320 -0
- package/native/runtime_clock.c +181 -0
- package/native/runtime_clock_posix.c +599 -0
- package/native/runtime_core.c +488 -0
- package/native/runtime_data_view.c +1135 -0
- package/native/runtime_date.c +1997 -0
- package/native/runtime_descriptor.c +1104 -0
- package/native/runtime_disposal.c +1667 -0
- package/native/runtime_enumeration.c +636 -0
- package/native/runtime_error.c +997 -0
- package/native/runtime_event_loop.c +653 -0
- package/native/runtime_function.c +3667 -0
- package/native/runtime_generator.c +1120 -0
- package/native/runtime_internal.h +5578 -0
- package/native/runtime_iterator.c +5379 -0
- package/native/runtime_json.c +1992 -0
- package/native/runtime_map.c +1563 -0
- package/native/runtime_math.c +918 -0
- package/native/runtime_memory.c +1365 -0
- package/native/runtime_module.c +884 -0
- package/native/runtime_number.c +2077 -0
- package/native/runtime_object.c +423 -0
- package/native/runtime_object_builtin.c +3901 -0
- package/native/runtime_primitive.c +2268 -0
- package/native/runtime_process.c +744 -0
- package/native/runtime_promise.c +3378 -0
- package/native/runtime_property.c +815 -0
- package/native/runtime_proxy.c +1557 -0
- package/native/runtime_realm.c +435 -0
- package/native/runtime_reflect.c +648 -0
- package/native/runtime_regexp.c +3519 -0
- package/native/runtime_regexp_matcher.c +3559 -0
- package/native/runtime_regexp_symbol.c +1495 -0
- package/native/runtime_set.c +1775 -0
- package/native/runtime_string.c +2886 -0
- package/native/runtime_string_match.c +1185 -0
- package/native/runtime_symbol.c +1010 -0
- package/native/runtime_typed_array.c +5137 -0
- package/native/runtime_unicode_tables.h +14670 -0
- package/native/runtime_uri.c +700 -0
- package/native/runtime_weak_collection.c +1299 -0
- package/package.json +38 -3
|
@@ -0,0 +1,2886 @@
|
|
|
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
|
+
#include "runtime_unicode_tables.h"
|
|
30
|
+
|
|
31
|
+
#include <math.h>
|
|
32
|
+
#include <stdio.h>
|
|
33
|
+
#include <stdlib.h>
|
|
34
|
+
#include <string.h>
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* String values and the string half of property keys: allocation,
|
|
38
|
+
* content equality, ASCII name matching, canonical array-index
|
|
39
|
+
* recognition, and the own properties a String exotic object exposes.
|
|
40
|
+
* Every component that compares, builds, or classifies a property key
|
|
41
|
+
* name reaches this unit through the internal header. The same unit
|
|
42
|
+
* owns the realm's `String` constructor, %String.prototype%, the String
|
|
43
|
+
* exotic objects `new String` creates, and the `fromCharCode`,
|
|
44
|
+
* `fromCodePoint`, and `raw` statics. Case conversion, trimming,
|
|
45
|
+
* normalization, and deterministic locale comparison read only the pinned
|
|
46
|
+
* tables in the generated runtime Unicode header.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
OseoResult oseo_internal_validate_string_length(
|
|
50
|
+
OseoContext *context,
|
|
51
|
+
size_t length
|
|
52
|
+
) {
|
|
53
|
+
if (length > OSEO_MAX_STRING_LENGTH) {
|
|
54
|
+
return oseo_internal_throw_error(
|
|
55
|
+
context,
|
|
56
|
+
OSEO_ERROR_RANGE,
|
|
57
|
+
"Invalid string length."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return normal(oseo_undefined());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* FNV-1a over the code units, for the string table's index. */
|
|
64
|
+
static uint32_t string_table_hash(const uint16_t *units, size_t length) {
|
|
65
|
+
uint32_t hash = UINT32_C(2166136261);
|
|
66
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
67
|
+
hash = (hash ^ (uint32_t)units[index]) * UINT32_C(16777619);
|
|
68
|
+
}
|
|
69
|
+
return hash;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/* Returns the shared string with exactly these units, or zero. */
|
|
73
|
+
static OseoValue string_table_find(
|
|
74
|
+
const OseoStringTable *table,
|
|
75
|
+
uint32_t hash,
|
|
76
|
+
const uint16_t *units,
|
|
77
|
+
size_t length
|
|
78
|
+
) {
|
|
79
|
+
if (table == NULL || table->slot_capacity == 0u) return 0u;
|
|
80
|
+
size_t mask = table->slot_capacity - 1u;
|
|
81
|
+
for (size_t slot = hash & mask;; slot = (slot + 1u) & mask) {
|
|
82
|
+
uint32_t position = table->slots[slot];
|
|
83
|
+
if (position == 0u) return 0u;
|
|
84
|
+
OseoValue entry = table->entries[position - 1u];
|
|
85
|
+
const OseoString *string = string_object(entry);
|
|
86
|
+
if (string->length == length &&
|
|
87
|
+
(length == 0u ||
|
|
88
|
+
memcmp(string->units, units, length * sizeof(uint16_t)) == 0)) {
|
|
89
|
+
return entry;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* Places entry `position` in an index with at least one empty slot. */
|
|
95
|
+
static void string_table_index(OseoStringTable *table, size_t position) {
|
|
96
|
+
size_t mask = table->slot_capacity - 1u;
|
|
97
|
+
size_t slot = table->hashes[position] & mask;
|
|
98
|
+
while (table->slots[slot] != 0u) slot = (slot + 1u) & mask;
|
|
99
|
+
table->slots[slot] = (uint32_t)(position + 1u);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* Rebuilds the index of a table whose entries moved. */
|
|
103
|
+
static void string_table_reindex(OseoStringTable *table) {
|
|
104
|
+
memset(table->slots, 0, table->slot_capacity * sizeof(*table->slots));
|
|
105
|
+
for (size_t position = 0u; position < table->count; position += 1u) {
|
|
106
|
+
string_table_index(table, position);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/*
|
|
111
|
+
* Shares a freshly published string. Growth uses the ordinary allocator
|
|
112
|
+
* without a safepoint, and a failed growth only leaves the string
|
|
113
|
+
* unshared, so neither path changes what the caller observes.
|
|
114
|
+
*/
|
|
115
|
+
static void string_table_insert(
|
|
116
|
+
OseoContext *context,
|
|
117
|
+
OseoValue value,
|
|
118
|
+
uint32_t hash
|
|
119
|
+
) {
|
|
120
|
+
OseoStringTable *table = context->string_table;
|
|
121
|
+
if (table == NULL) {
|
|
122
|
+
table = calloc(1u, sizeof(*table));
|
|
123
|
+
if (table == NULL) return;
|
|
124
|
+
context->string_table = table;
|
|
125
|
+
}
|
|
126
|
+
if (table->count == UINT32_MAX - 1u) return;
|
|
127
|
+
if (table->count == table->capacity) {
|
|
128
|
+
size_t capacity = table->capacity == 0u ? 64u : table->capacity * 2u;
|
|
129
|
+
if (capacity > SIZE_MAX / sizeof(OseoValue)) return;
|
|
130
|
+
OseoValue *entries =
|
|
131
|
+
realloc(table->entries, capacity * sizeof(*entries));
|
|
132
|
+
if (entries == NULL) return;
|
|
133
|
+
table->entries = entries;
|
|
134
|
+
uint32_t *hashes = realloc(table->hashes, capacity * sizeof(*hashes));
|
|
135
|
+
if (hashes == NULL) return;
|
|
136
|
+
table->hashes = hashes;
|
|
137
|
+
table->capacity = capacity;
|
|
138
|
+
}
|
|
139
|
+
if ((table->count + 1u) * 2u > table->slot_capacity) {
|
|
140
|
+
size_t slot_capacity =
|
|
141
|
+
table->slot_capacity == 0u ? 128u : table->slot_capacity * 2u;
|
|
142
|
+
if (slot_capacity > SIZE_MAX / sizeof(uint32_t)) return;
|
|
143
|
+
uint32_t *slots = calloc(slot_capacity, sizeof(*slots));
|
|
144
|
+
if (slots == NULL) return;
|
|
145
|
+
free(table->slots);
|
|
146
|
+
table->slots = slots;
|
|
147
|
+
table->slot_capacity = slot_capacity;
|
|
148
|
+
string_table_reindex(table);
|
|
149
|
+
}
|
|
150
|
+
table->entries[table->count] = value;
|
|
151
|
+
table->hashes[table->count] = hash;
|
|
152
|
+
string_table_index(table, table->count);
|
|
153
|
+
table->count += 1u;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
void oseo_internal_string_table_sweep(OseoContext *context) {
|
|
157
|
+
OseoStringTable *table = context->string_table;
|
|
158
|
+
if (table == NULL) return;
|
|
159
|
+
size_t kept = 0u;
|
|
160
|
+
for (size_t position = 0u; position < table->count; position += 1u) {
|
|
161
|
+
if (!heap_object(table->entries[position])->marked) continue;
|
|
162
|
+
table->entries[kept] = table->entries[position];
|
|
163
|
+
table->hashes[kept] = table->hashes[position];
|
|
164
|
+
kept += 1u;
|
|
165
|
+
}
|
|
166
|
+
if (kept == table->count) return;
|
|
167
|
+
table->count = kept;
|
|
168
|
+
string_table_reindex(table);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
void oseo_internal_string_table_release(OseoContext *context) {
|
|
172
|
+
OseoStringTable *table = context->string_table;
|
|
173
|
+
if (table == NULL) return;
|
|
174
|
+
free(table->entries);
|
|
175
|
+
free(table->hashes);
|
|
176
|
+
free(table->slots);
|
|
177
|
+
free(table);
|
|
178
|
+
context->string_table = NULL;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static OseoResult allocate_string(
|
|
182
|
+
OseoContext *context,
|
|
183
|
+
const uint16_t *units,
|
|
184
|
+
size_t length,
|
|
185
|
+
bool intern
|
|
186
|
+
) {
|
|
187
|
+
OseoResult valid = oseo_internal_validate_string_length(context, length);
|
|
188
|
+
if (valid.status != OSEO_STATUS_NORMAL) return valid;
|
|
189
|
+
bool shareable = length <= OSEO_STRING_TABLE_MAX_UNITS;
|
|
190
|
+
uint32_t hash = shareable ? string_table_hash(units, length) : 0u;
|
|
191
|
+
if (shareable) {
|
|
192
|
+
OseoValue shared =
|
|
193
|
+
string_table_find(context->string_table, hash, units, length);
|
|
194
|
+
if (shared != 0u) return normal(shared);
|
|
195
|
+
}
|
|
196
|
+
if (length > (SIZE_MAX - sizeof(OseoString)) / sizeof(uint16_t)) {
|
|
197
|
+
return failure(context, "OSEO2001", "String allocation is too large.");
|
|
198
|
+
}
|
|
199
|
+
size_t size = sizeof(OseoString) + length * sizeof(uint16_t);
|
|
200
|
+
OseoString *object = oseo_internal_allocate_heap_bytes(context, size);
|
|
201
|
+
if (object == NULL) {
|
|
202
|
+
return failure(context, "OSEO2001", "String allocation failed.");
|
|
203
|
+
}
|
|
204
|
+
object->length = length;
|
|
205
|
+
if (length > 0u) memcpy(object->units, units, length * sizeof(uint16_t));
|
|
206
|
+
OseoResult published = oseo_internal_publish_heap(
|
|
207
|
+
context, &object->header, OSEO_HEAP_STRING);
|
|
208
|
+
if (published.status == OSEO_STATUS_NORMAL && shareable && intern) {
|
|
209
|
+
string_table_insert(context, published.value, hash);
|
|
210
|
+
}
|
|
211
|
+
return published;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
OseoResult oseo_internal_allocate_string(
|
|
215
|
+
OseoContext *context,
|
|
216
|
+
const uint16_t *units,
|
|
217
|
+
size_t length
|
|
218
|
+
) {
|
|
219
|
+
return allocate_string(context, units, length, true);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
OseoResult oseo_internal_allocate_uninterned_string(
|
|
223
|
+
OseoContext *context,
|
|
224
|
+
const uint16_t *units,
|
|
225
|
+
size_t length
|
|
226
|
+
) {
|
|
227
|
+
return allocate_string(context, units, length, false);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
OseoResult oseo_string_from_units(
|
|
231
|
+
OseoContext *context,
|
|
232
|
+
const uint16_t *units,
|
|
233
|
+
size_t length
|
|
234
|
+
) {
|
|
235
|
+
return oseo_internal_allocate_string(context, units, length);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
OseoResult oseo_internal_ascii_string(OseoContext *context, const char *text) {
|
|
239
|
+
uint16_t units[32];
|
|
240
|
+
size_t length = strlen(text);
|
|
241
|
+
if (length > sizeof(units) / sizeof(*units)) {
|
|
242
|
+
return failure(context, "OSEO2001", "Internal property name is long.");
|
|
243
|
+
}
|
|
244
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
245
|
+
units[index] = (uint16_t)(unsigned char)text[index];
|
|
246
|
+
}
|
|
247
|
+
return oseo_internal_allocate_string(context, units, length);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
bool oseo_internal_string_is_ascii(OseoValue value, const char *text) {
|
|
251
|
+
if (!is_string(value)) return false;
|
|
252
|
+
OseoString *string = string_object(value);
|
|
253
|
+
size_t length = strlen(text);
|
|
254
|
+
if (string->length != length) return false;
|
|
255
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
256
|
+
if (string->units[index] !=
|
|
257
|
+
(uint16_t)(unsigned char)text[index]) return false;
|
|
258
|
+
}
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
bool oseo_internal_string_equal(OseoValue left, OseoValue right) {
|
|
263
|
+
if (!is_string(left) || !is_string(right)) return false;
|
|
264
|
+
OseoString *left_string = string_object(left);
|
|
265
|
+
OseoString *right_string = string_object(right);
|
|
266
|
+
return left_string->length == right_string->length &&
|
|
267
|
+
memcmp(
|
|
268
|
+
left_string->units,
|
|
269
|
+
right_string->units,
|
|
270
|
+
left_string->length * sizeof(uint16_t)
|
|
271
|
+
) == 0;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/*
|
|
275
|
+
* Property keys are strings compared by content or symbols by identity,
|
|
276
|
+
* where two representatives of one registry entry name one symbol.
|
|
277
|
+
*/
|
|
278
|
+
bool oseo_internal_property_key_equal(OseoValue left, OseoValue right) {
|
|
279
|
+
if (is_string(left) && is_string(right)) {
|
|
280
|
+
return oseo_internal_string_equal(left, right);
|
|
281
|
+
}
|
|
282
|
+
return left == right || same_registered_symbol(left, right);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
bool oseo_internal_array_index(OseoValue key, uint32_t *result) {
|
|
286
|
+
if (!is_string(key)) return false;
|
|
287
|
+
OseoString *string = string_object(key);
|
|
288
|
+
if (string->length == 0u || string->length > 10u) return false;
|
|
289
|
+
if (string->length > 1u && string->units[0] == UINT16_C(0x30)) {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
uint64_t value = 0u;
|
|
293
|
+
for (size_t index = 0u; index < string->length; index += 1u) {
|
|
294
|
+
uint16_t unit = string->units[index];
|
|
295
|
+
if (unit < UINT16_C(0x30) || unit > UINT16_C(0x39)) return false;
|
|
296
|
+
value = value * UINT64_C(10) + (uint64_t)(unit - UINT16_C(0x30));
|
|
297
|
+
if (value > UINT64_C(4294967294)) return false;
|
|
298
|
+
}
|
|
299
|
+
*result = (uint32_t)value;
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
bool oseo_internal_string_own_property(
|
|
304
|
+
OseoValue string_value,
|
|
305
|
+
OseoValue key,
|
|
306
|
+
uint32_t *index
|
|
307
|
+
) {
|
|
308
|
+
if (!is_string(string_value)) return false;
|
|
309
|
+
if (oseo_internal_string_is_ascii(key, "length")) return true;
|
|
310
|
+
uint32_t candidate = 0u;
|
|
311
|
+
if (!oseo_internal_array_index(key, &candidate) ||
|
|
312
|
+
candidate >= string_object(string_value)->length) return false;
|
|
313
|
+
if (index != NULL) *index = candidate;
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
bool oseo_internal_string_data(OseoValue value) {
|
|
318
|
+
return is_object(value) && ordinary_object(value)->primitive_data &&
|
|
319
|
+
is_string(ordinary_object(value)->primitive_value);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/*
|
|
323
|
+
* True when the whole run of wrapper definitions may append without a
|
|
324
|
+
* duplicate-key scan. Every key the run produces is a distinct
|
|
325
|
+
* canonical index or `length`, and no user code runs between two of
|
|
326
|
+
* them, so an empty extensible ordinary object stays free of the
|
|
327
|
+
* collisions [[DefineOwnProperty]] exists to detect. A wrapper that
|
|
328
|
+
* arrives with any own property, or that is not an ordinary object,
|
|
329
|
+
* takes the ordinary definition path instead.
|
|
330
|
+
*/
|
|
331
|
+
static bool string_wrapper_may_append(OseoValue wrapper) {
|
|
332
|
+
if (!is_object(wrapper) || is_array(wrapper) || is_function(wrapper)) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
const OseoOrdinaryObject *object = ordinary_object(wrapper);
|
|
336
|
+
return object->extensible && !object->module_namespace &&
|
|
337
|
+
object->property_count == 0u;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
static OseoResult define_wrapper_property(
|
|
341
|
+
OseoContext *context,
|
|
342
|
+
bool append,
|
|
343
|
+
OseoValue wrapper,
|
|
344
|
+
OseoValue key,
|
|
345
|
+
OseoValue value,
|
|
346
|
+
OseoPropertyAttributes attributes
|
|
347
|
+
) {
|
|
348
|
+
return append
|
|
349
|
+
? oseo_internal_append_own_property(
|
|
350
|
+
context, wrapper, key, value, attributes)
|
|
351
|
+
: oseo_object_define(context, wrapper, key, value, attributes);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
OseoResult oseo_internal_string_wrapper_properties(
|
|
355
|
+
OseoContext *context,
|
|
356
|
+
OseoValue string_value,
|
|
357
|
+
OseoValue wrapper
|
|
358
|
+
) {
|
|
359
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
360
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 4u);
|
|
361
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
362
|
+
frame.slots[0] = string_value;
|
|
363
|
+
frame.slots[1] = wrapper;
|
|
364
|
+
bool append = string_wrapper_may_append(frame.slots[1]);
|
|
365
|
+
size_t length = string_object(frame.slots[0])->length;
|
|
366
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
367
|
+
char key_text[24];
|
|
368
|
+
(void)snprintf(key_text, sizeof(key_text), "%zu", index);
|
|
369
|
+
result = oseo_internal_ascii_string(context, key_text);
|
|
370
|
+
frame.slots[2] = result.value;
|
|
371
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
372
|
+
uint16_t unit = string_object(frame.slots[0])->units[index];
|
|
373
|
+
result = oseo_internal_allocate_string(context, &unit, 1u);
|
|
374
|
+
frame.slots[3] = result.value;
|
|
375
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
376
|
+
result = define_wrapper_property(
|
|
377
|
+
context,
|
|
378
|
+
append,
|
|
379
|
+
frame.slots[1],
|
|
380
|
+
frame.slots[2],
|
|
381
|
+
frame.slots[3],
|
|
382
|
+
(OseoPropertyAttributes){false, true, false, false}
|
|
383
|
+
);
|
|
384
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
385
|
+
}
|
|
386
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
387
|
+
result = oseo_internal_ascii_string(context, "length");
|
|
388
|
+
frame.slots[2] = result.value;
|
|
389
|
+
}
|
|
390
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
391
|
+
result = define_wrapper_property(
|
|
392
|
+
context,
|
|
393
|
+
append,
|
|
394
|
+
frame.slots[1],
|
|
395
|
+
frame.slots[2],
|
|
396
|
+
oseo_number((double)string_object(frame.slots[0])->length),
|
|
397
|
+
(OseoPropertyAttributes){false, false, false, false}
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[1]);
|
|
401
|
+
oseo_roots_release(context, &frame);
|
|
402
|
+
return result;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
static OseoResult string_builder_reserve(
|
|
406
|
+
OseoContext *context,
|
|
407
|
+
OseoStringBuilder *builder,
|
|
408
|
+
size_t additional
|
|
409
|
+
) {
|
|
410
|
+
if (additional > SIZE_MAX - builder->length) {
|
|
411
|
+
return failure(context, "OSEO2001", "String allocation is too large.");
|
|
412
|
+
}
|
|
413
|
+
size_t required = builder->length + additional;
|
|
414
|
+
OseoResult valid = oseo_internal_validate_string_length(
|
|
415
|
+
context,
|
|
416
|
+
required
|
|
417
|
+
);
|
|
418
|
+
if (valid.status != OSEO_STATUS_NORMAL) return valid;
|
|
419
|
+
if (required <= builder->capacity) return normal(oseo_undefined());
|
|
420
|
+
size_t capacity = builder->capacity == 0u ? 16u : builder->capacity;
|
|
421
|
+
while (capacity < required) {
|
|
422
|
+
if (capacity > OSEO_MAX_STRING_LENGTH / 2u) {
|
|
423
|
+
capacity = required;
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
capacity *= 2u;
|
|
427
|
+
}
|
|
428
|
+
uint16_t *units = realloc(builder->units, capacity * sizeof(uint16_t));
|
|
429
|
+
if (units == NULL) {
|
|
430
|
+
return failure(context, "OSEO2001", "String allocation failed.");
|
|
431
|
+
}
|
|
432
|
+
builder->units = units;
|
|
433
|
+
builder->capacity = capacity;
|
|
434
|
+
return normal(oseo_undefined());
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
static OseoResult string_builder_append_unit(
|
|
438
|
+
OseoContext *context,
|
|
439
|
+
OseoStringBuilder *builder,
|
|
440
|
+
uint16_t unit
|
|
441
|
+
) {
|
|
442
|
+
OseoResult result = string_builder_reserve(context, builder, 1u);
|
|
443
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
444
|
+
builder->units[builder->length] = unit;
|
|
445
|
+
builder->length += 1u;
|
|
446
|
+
return result;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
OseoResult oseo_internal_string_builder_append(
|
|
450
|
+
OseoContext *context,
|
|
451
|
+
OseoStringBuilder *builder,
|
|
452
|
+
const uint16_t *units,
|
|
453
|
+
size_t length
|
|
454
|
+
) {
|
|
455
|
+
if (length == 0u) return normal(oseo_undefined());
|
|
456
|
+
OseoResult result = string_builder_reserve(context, builder, length);
|
|
457
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
458
|
+
memcpy(
|
|
459
|
+
builder->units + builder->length,
|
|
460
|
+
units,
|
|
461
|
+
length * sizeof(uint16_t)
|
|
462
|
+
);
|
|
463
|
+
builder->length += length;
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
void oseo_internal_string_builder_release(OseoStringBuilder *builder) {
|
|
468
|
+
free(builder->units);
|
|
469
|
+
builder->units = NULL;
|
|
470
|
+
builder->length = 0u;
|
|
471
|
+
builder->capacity = 0u;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
typedef struct {
|
|
475
|
+
uint32_t *values;
|
|
476
|
+
size_t length;
|
|
477
|
+
size_t capacity;
|
|
478
|
+
} OseoCodePointBuilder;
|
|
479
|
+
|
|
480
|
+
static OseoValue string_builtin_argument(
|
|
481
|
+
size_t argument_count,
|
|
482
|
+
const OseoValue *arguments,
|
|
483
|
+
size_t index
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
static OseoResult string_method_subject(
|
|
487
|
+
OseoContext *context,
|
|
488
|
+
OseoValue receiver
|
|
489
|
+
);
|
|
490
|
+
|
|
491
|
+
static OseoResult code_point_builder_reserve(
|
|
492
|
+
OseoContext *context,
|
|
493
|
+
OseoCodePointBuilder *builder,
|
|
494
|
+
size_t additional
|
|
495
|
+
) {
|
|
496
|
+
if (additional > SIZE_MAX - builder->length) {
|
|
497
|
+
return failure(context, "OSEO2001", "String allocation is too large.");
|
|
498
|
+
}
|
|
499
|
+
size_t required = builder->length + additional;
|
|
500
|
+
if (required <= builder->capacity) return normal(oseo_undefined());
|
|
501
|
+
size_t capacity = builder->capacity == 0u ? 16u : builder->capacity;
|
|
502
|
+
while (capacity < required) {
|
|
503
|
+
if (capacity > SIZE_MAX / 2u) {
|
|
504
|
+
capacity = required;
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
capacity *= 2u;
|
|
508
|
+
}
|
|
509
|
+
if (capacity > SIZE_MAX / sizeof(uint32_t)) {
|
|
510
|
+
return failure(context, "OSEO2001", "String allocation is too large.");
|
|
511
|
+
}
|
|
512
|
+
uint32_t *values = realloc(
|
|
513
|
+
builder->values,
|
|
514
|
+
capacity * sizeof(uint32_t)
|
|
515
|
+
);
|
|
516
|
+
if (values == NULL) {
|
|
517
|
+
return failure(context, "OSEO2001", "String allocation failed.");
|
|
518
|
+
}
|
|
519
|
+
builder->values = values;
|
|
520
|
+
builder->capacity = capacity;
|
|
521
|
+
return normal(oseo_undefined());
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
static OseoResult code_point_builder_append(
|
|
525
|
+
OseoContext *context,
|
|
526
|
+
OseoCodePointBuilder *builder,
|
|
527
|
+
uint32_t value
|
|
528
|
+
) {
|
|
529
|
+
OseoResult result = code_point_builder_reserve(context, builder, 1u);
|
|
530
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
531
|
+
builder->values[builder->length] = value;
|
|
532
|
+
builder->length += 1u;
|
|
533
|
+
return result;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
static void code_point_builder_release(OseoCodePointBuilder *builder) {
|
|
537
|
+
free(builder->values);
|
|
538
|
+
builder->values = NULL;
|
|
539
|
+
builder->length = 0u;
|
|
540
|
+
builder->capacity = 0u;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
static uint32_t string_next_code_point(
|
|
544
|
+
const OseoString *string,
|
|
545
|
+
size_t *cursor
|
|
546
|
+
) {
|
|
547
|
+
uint16_t first = string->units[*cursor];
|
|
548
|
+
*cursor += 1u;
|
|
549
|
+
if (first < UINT16_C(0xd800) || first > UINT16_C(0xdbff) ||
|
|
550
|
+
*cursor >= string->length) return first;
|
|
551
|
+
uint16_t second = string->units[*cursor];
|
|
552
|
+
if (second < UINT16_C(0xdc00) || second > UINT16_C(0xdfff)) return first;
|
|
553
|
+
*cursor += 1u;
|
|
554
|
+
return UINT32_C(0x10000) +
|
|
555
|
+
((uint32_t)(first - UINT16_C(0xd800)) << 10u) +
|
|
556
|
+
(uint32_t)(second - UINT16_C(0xdc00));
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
static OseoResult code_points_from_string(
|
|
560
|
+
OseoContext *context,
|
|
561
|
+
const OseoString *string,
|
|
562
|
+
OseoCodePointBuilder *builder
|
|
563
|
+
) {
|
|
564
|
+
OseoResult result = code_point_builder_reserve(
|
|
565
|
+
context,
|
|
566
|
+
builder,
|
|
567
|
+
string->length
|
|
568
|
+
);
|
|
569
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
570
|
+
size_t cursor = 0u;
|
|
571
|
+
while (cursor < string->length) {
|
|
572
|
+
builder->values[builder->length] = string_next_code_point(
|
|
573
|
+
string,
|
|
574
|
+
&cursor
|
|
575
|
+
);
|
|
576
|
+
builder->length += 1u;
|
|
577
|
+
}
|
|
578
|
+
return result;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
static OseoResult string_from_code_points(
|
|
582
|
+
OseoContext *context,
|
|
583
|
+
const OseoCodePointBuilder *builder
|
|
584
|
+
) {
|
|
585
|
+
OseoStringBuilder units = {NULL, 0u, 0u};
|
|
586
|
+
OseoResult result = normal(oseo_undefined());
|
|
587
|
+
for (size_t index = 0u;
|
|
588
|
+
result.status == OSEO_STATUS_NORMAL && index < builder->length;
|
|
589
|
+
index += 1u) {
|
|
590
|
+
uint32_t code_point = builder->values[index];
|
|
591
|
+
if (code_point <= UINT32_C(0xffff)) {
|
|
592
|
+
result = string_builder_append_unit(
|
|
593
|
+
context,
|
|
594
|
+
&units,
|
|
595
|
+
(uint16_t)code_point
|
|
596
|
+
);
|
|
597
|
+
} else {
|
|
598
|
+
uint32_t value = code_point - UINT32_C(0x10000);
|
|
599
|
+
result = string_builder_append_unit(
|
|
600
|
+
context,
|
|
601
|
+
&units,
|
|
602
|
+
(uint16_t)(UINT32_C(0xd800) + (value >> 10u))
|
|
603
|
+
);
|
|
604
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
605
|
+
result = string_builder_append_unit(
|
|
606
|
+
context,
|
|
607
|
+
&units,
|
|
608
|
+
(uint16_t)(UINT32_C(0xdc00) +
|
|
609
|
+
(value & UINT32_C(0x3ff)))
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
615
|
+
result = oseo_internal_allocate_string(
|
|
616
|
+
context,
|
|
617
|
+
units.units,
|
|
618
|
+
units.length
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
oseo_internal_string_builder_release(&units);
|
|
622
|
+
return result;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
static const OseoUnicodeMapping *unicode_mapping_find(
|
|
626
|
+
const OseoUnicodeMapping *mappings,
|
|
627
|
+
size_t count,
|
|
628
|
+
uint32_t code_point
|
|
629
|
+
) {
|
|
630
|
+
size_t low = 0u;
|
|
631
|
+
size_t high = count;
|
|
632
|
+
while (low < high) {
|
|
633
|
+
size_t middle = low + (high - low) / 2u;
|
|
634
|
+
uint32_t candidate = mappings[middle].code_point;
|
|
635
|
+
if (candidate < code_point) low = middle + 1u;
|
|
636
|
+
else high = middle;
|
|
637
|
+
}
|
|
638
|
+
return low < count && mappings[low].code_point == code_point
|
|
639
|
+
? &mappings[low]
|
|
640
|
+
: NULL;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
static uint8_t unicode_combining_class(uint32_t code_point) {
|
|
644
|
+
size_t count = sizeof(oseo_unicode_combining_classes) /
|
|
645
|
+
sizeof(oseo_unicode_combining_classes[0]);
|
|
646
|
+
size_t low = 0u;
|
|
647
|
+
size_t high = count;
|
|
648
|
+
while (low < high) {
|
|
649
|
+
size_t middle = low + (high - low) / 2u;
|
|
650
|
+
if (oseo_unicode_combining_classes[middle].start <= code_point) {
|
|
651
|
+
low = middle + 1u;
|
|
652
|
+
} else {
|
|
653
|
+
high = middle;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return low == 0u ? 0u : oseo_unicode_combining_classes[low - 1u].value;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
static bool unicode_set_has(
|
|
660
|
+
const uint32_t *boundaries,
|
|
661
|
+
size_t count,
|
|
662
|
+
uint32_t code_point
|
|
663
|
+
) {
|
|
664
|
+
size_t low = 0u;
|
|
665
|
+
size_t high = count;
|
|
666
|
+
while (low < high) {
|
|
667
|
+
size_t middle = low + (high - low) / 2u;
|
|
668
|
+
if (boundaries[middle] <= code_point) low = middle + 1u;
|
|
669
|
+
else high = middle;
|
|
670
|
+
}
|
|
671
|
+
return (low & 1u) != 0u;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
static bool unicode_cased(uint32_t code_point) {
|
|
675
|
+
return unicode_set_has(
|
|
676
|
+
oseo_unicode_cased,
|
|
677
|
+
sizeof(oseo_unicode_cased) / sizeof(oseo_unicode_cased[0]),
|
|
678
|
+
code_point
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
static bool unicode_case_ignorable(uint32_t code_point) {
|
|
683
|
+
return unicode_set_has(
|
|
684
|
+
oseo_unicode_case_ignorable,
|
|
685
|
+
sizeof(oseo_unicode_case_ignorable) /
|
|
686
|
+
sizeof(oseo_unicode_case_ignorable[0]),
|
|
687
|
+
code_point
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
bool oseo_internal_unicode_id_start(uint32_t code_point) {
|
|
692
|
+
return unicode_set_has(
|
|
693
|
+
oseo_unicode_id_start,
|
|
694
|
+
sizeof(oseo_unicode_id_start) / sizeof(oseo_unicode_id_start[0]),
|
|
695
|
+
code_point
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
bool oseo_internal_unicode_id_continue(uint32_t code_point) {
|
|
700
|
+
return unicode_set_has(
|
|
701
|
+
oseo_unicode_id_continue,
|
|
702
|
+
sizeof(oseo_unicode_id_continue) /
|
|
703
|
+
sizeof(oseo_unicode_id_continue[0]),
|
|
704
|
+
code_point
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
static bool unicode_final_sigma(
|
|
709
|
+
const OseoCodePointBuilder *subject,
|
|
710
|
+
size_t index
|
|
711
|
+
) {
|
|
712
|
+
bool preceded = false;
|
|
713
|
+
size_t cursor = index;
|
|
714
|
+
while (cursor > 0u) {
|
|
715
|
+
cursor -= 1u;
|
|
716
|
+
uint32_t code_point = subject->values[cursor];
|
|
717
|
+
if (unicode_case_ignorable(code_point)) continue;
|
|
718
|
+
preceded = unicode_cased(code_point);
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
if (!preceded) return false;
|
|
722
|
+
for (cursor = index + 1u; cursor < subject->length; cursor += 1u) {
|
|
723
|
+
uint32_t code_point = subject->values[cursor];
|
|
724
|
+
if (unicode_case_ignorable(code_point)) continue;
|
|
725
|
+
return !unicode_cased(code_point);
|
|
726
|
+
}
|
|
727
|
+
return true;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
static OseoResult string_convert_case(
|
|
731
|
+
OseoContext *context,
|
|
732
|
+
OseoValue receiver,
|
|
733
|
+
bool uppercase
|
|
734
|
+
) {
|
|
735
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
736
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
737
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
738
|
+
frame.slots[0] = receiver;
|
|
739
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
740
|
+
frame.slots[1] = result.value;
|
|
741
|
+
OseoCodePointBuilder input = {NULL, 0u, 0u};
|
|
742
|
+
OseoCodePointBuilder output = {NULL, 0u, 0u};
|
|
743
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
744
|
+
result = code_points_from_string(
|
|
745
|
+
context,
|
|
746
|
+
string_object(frame.slots[1]),
|
|
747
|
+
&input
|
|
748
|
+
);
|
|
749
|
+
}
|
|
750
|
+
const OseoUnicodeMapping *mappings = uppercase
|
|
751
|
+
? oseo_unicode_uppercase
|
|
752
|
+
: oseo_unicode_lowercase;
|
|
753
|
+
size_t mapping_count = uppercase
|
|
754
|
+
? sizeof(oseo_unicode_uppercase) /
|
|
755
|
+
sizeof(oseo_unicode_uppercase[0])
|
|
756
|
+
: sizeof(oseo_unicode_lowercase) /
|
|
757
|
+
sizeof(oseo_unicode_lowercase[0]);
|
|
758
|
+
for (size_t index = 0u;
|
|
759
|
+
result.status == OSEO_STATUS_NORMAL && index < input.length;
|
|
760
|
+
index += 1u) {
|
|
761
|
+
uint32_t code_point = input.values[index];
|
|
762
|
+
if (!uppercase && code_point == UINT32_C(0x3a3) &&
|
|
763
|
+
unicode_final_sigma(&input, index)) {
|
|
764
|
+
result = code_point_builder_append(
|
|
765
|
+
context,
|
|
766
|
+
&output,
|
|
767
|
+
UINT32_C(0x3c2)
|
|
768
|
+
);
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
const OseoUnicodeMapping *mapping = unicode_mapping_find(
|
|
772
|
+
mappings,
|
|
773
|
+
mapping_count,
|
|
774
|
+
code_point
|
|
775
|
+
);
|
|
776
|
+
if (mapping == NULL) {
|
|
777
|
+
result = code_point_builder_append(context, &output, code_point);
|
|
778
|
+
continue;
|
|
779
|
+
}
|
|
780
|
+
for (size_t item = 0u;
|
|
781
|
+
result.status == OSEO_STATUS_NORMAL && item < mapping->length;
|
|
782
|
+
item += 1u) {
|
|
783
|
+
result = code_point_builder_append(
|
|
784
|
+
context,
|
|
785
|
+
&output,
|
|
786
|
+
oseo_unicode_mapping_values[mapping->offset + item]
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
791
|
+
result = string_from_code_points(context, &output);
|
|
792
|
+
}
|
|
793
|
+
code_point_builder_release(&input);
|
|
794
|
+
code_point_builder_release(&output);
|
|
795
|
+
oseo_roots_release(context, &frame);
|
|
796
|
+
return result;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
static bool string_trim_code_unit(uint16_t unit) {
|
|
800
|
+
return unit == UINT16_C(0x0009) || unit == UINT16_C(0x000a) ||
|
|
801
|
+
unit == UINT16_C(0x000b) || unit == UINT16_C(0x000c) ||
|
|
802
|
+
unit == UINT16_C(0x000d) || unit == UINT16_C(0x0020) ||
|
|
803
|
+
unit == UINT16_C(0x00a0) || unit == UINT16_C(0x1680) ||
|
|
804
|
+
(unit >= UINT16_C(0x2000) && unit <= UINT16_C(0x200a)) ||
|
|
805
|
+
unit == UINT16_C(0x2028) || unit == UINT16_C(0x2029) ||
|
|
806
|
+
unit == UINT16_C(0x202f) || unit == UINT16_C(0x205f) ||
|
|
807
|
+
unit == UINT16_C(0x3000) || unit == UINT16_C(0xfeff);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
static OseoResult string_trim(
|
|
811
|
+
OseoContext *context,
|
|
812
|
+
OseoValue receiver,
|
|
813
|
+
bool trim_start,
|
|
814
|
+
bool trim_end
|
|
815
|
+
) {
|
|
816
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
817
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
818
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
819
|
+
frame.slots[0] = receiver;
|
|
820
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
821
|
+
frame.slots[1] = result.value;
|
|
822
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
823
|
+
OseoString *subject = string_object(frame.slots[1]);
|
|
824
|
+
size_t start = 0u;
|
|
825
|
+
size_t end = subject->length;
|
|
826
|
+
if (trim_start) {
|
|
827
|
+
while (start < end && string_trim_code_unit(
|
|
828
|
+
subject->units[start])) start += 1u;
|
|
829
|
+
}
|
|
830
|
+
if (trim_end) {
|
|
831
|
+
while (end > start && string_trim_code_unit(
|
|
832
|
+
subject->units[end - 1u])) end -= 1u;
|
|
833
|
+
}
|
|
834
|
+
result = oseo_internal_allocate_string(
|
|
835
|
+
context,
|
|
836
|
+
start == 0u ? subject->units : subject->units + start,
|
|
837
|
+
end - start
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
oseo_roots_release(context, &frame);
|
|
841
|
+
return result;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
static OseoResult normalize_append_decomposed(
|
|
845
|
+
OseoContext *context,
|
|
846
|
+
OseoCodePointBuilder *output,
|
|
847
|
+
uint32_t code_point
|
|
848
|
+
) {
|
|
849
|
+
return code_point_builder_append(context, output, code_point);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/*
|
|
853
|
+
* Stably order each non-starter run after decomposition. Counting the fixed
|
|
854
|
+
* canonical-combining-class range keeps one descending run linear instead of
|
|
855
|
+
* shifting its already appended suffix once for every code point.
|
|
856
|
+
*/
|
|
857
|
+
static OseoResult normalize_order(
|
|
858
|
+
OseoContext *context,
|
|
859
|
+
OseoCodePointBuilder *output
|
|
860
|
+
) {
|
|
861
|
+
if (output->length < 2u) return normal(oseo_undefined());
|
|
862
|
+
uint8_t prior = unicode_combining_class(output->values[0]);
|
|
863
|
+
bool requires_ordering = false;
|
|
864
|
+
for (size_t index = 1u; index < output->length; index += 1u) {
|
|
865
|
+
uint8_t combining = unicode_combining_class(output->values[index]);
|
|
866
|
+
if (combining != 0u && prior > combining) {
|
|
867
|
+
requires_ordering = true;
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
prior = combining;
|
|
871
|
+
}
|
|
872
|
+
if (!requires_ordering) return normal(oseo_undefined());
|
|
873
|
+
OseoCodePointBuilder ordered = {NULL, 0u, 0u};
|
|
874
|
+
OseoResult result = code_point_builder_reserve(
|
|
875
|
+
context,
|
|
876
|
+
&ordered,
|
|
877
|
+
output->length
|
|
878
|
+
);
|
|
879
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
880
|
+
ordered.length = output->length;
|
|
881
|
+
size_t run_start = 0u;
|
|
882
|
+
while (run_start < output->length) {
|
|
883
|
+
if (unicode_combining_class(output->values[run_start]) == 0u) {
|
|
884
|
+
ordered.values[run_start] = output->values[run_start];
|
|
885
|
+
run_start += 1u;
|
|
886
|
+
continue;
|
|
887
|
+
}
|
|
888
|
+
size_t counts[256] = {0u};
|
|
889
|
+
size_t run_end = run_start;
|
|
890
|
+
while (run_end < output->length) {
|
|
891
|
+
uint8_t combining = unicode_combining_class(
|
|
892
|
+
output->values[run_end]
|
|
893
|
+
);
|
|
894
|
+
if (combining == 0u) break;
|
|
895
|
+
counts[combining] += 1u;
|
|
896
|
+
run_end += 1u;
|
|
897
|
+
}
|
|
898
|
+
size_t offset = run_start;
|
|
899
|
+
for (size_t combining = 1u; combining < 256u; combining += 1u) {
|
|
900
|
+
size_t count = counts[combining];
|
|
901
|
+
counts[combining] = offset;
|
|
902
|
+
offset += count;
|
|
903
|
+
}
|
|
904
|
+
for (size_t index = run_start; index < run_end; index += 1u) {
|
|
905
|
+
uint8_t combining = unicode_combining_class(
|
|
906
|
+
output->values[index]
|
|
907
|
+
);
|
|
908
|
+
ordered.values[counts[combining]] = output->values[index];
|
|
909
|
+
counts[combining] += 1u;
|
|
910
|
+
}
|
|
911
|
+
run_start = run_end;
|
|
912
|
+
}
|
|
913
|
+
memcpy(
|
|
914
|
+
output->values,
|
|
915
|
+
ordered.values,
|
|
916
|
+
output->length * sizeof(*output->values)
|
|
917
|
+
);
|
|
918
|
+
code_point_builder_release(&ordered);
|
|
919
|
+
return normal(oseo_undefined());
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
static OseoResult normalize_decompose(
|
|
923
|
+
OseoContext *context,
|
|
924
|
+
OseoCodePointBuilder *output,
|
|
925
|
+
uint32_t code_point,
|
|
926
|
+
bool compatibility
|
|
927
|
+
) {
|
|
928
|
+
const uint32_t s_base = UINT32_C(0xac00);
|
|
929
|
+
const uint32_t l_base = UINT32_C(0x1100);
|
|
930
|
+
const uint32_t v_base = UINT32_C(0x1161);
|
|
931
|
+
const uint32_t t_base = UINT32_C(0x11a7);
|
|
932
|
+
const uint32_t l_count = UINT32_C(19);
|
|
933
|
+
const uint32_t v_count = UINT32_C(21);
|
|
934
|
+
const uint32_t t_count = UINT32_C(28);
|
|
935
|
+
const uint32_t n_count = v_count * t_count;
|
|
936
|
+
const uint32_t s_count = l_count * n_count;
|
|
937
|
+
if (code_point >= s_base && code_point < s_base + s_count) {
|
|
938
|
+
uint32_t index = code_point - s_base;
|
|
939
|
+
OseoResult result = normalize_append_decomposed(
|
|
940
|
+
context,
|
|
941
|
+
output,
|
|
942
|
+
l_base + index / n_count
|
|
943
|
+
);
|
|
944
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
945
|
+
result = normalize_append_decomposed(
|
|
946
|
+
context,
|
|
947
|
+
output,
|
|
948
|
+
v_base + (index % n_count) / t_count
|
|
949
|
+
);
|
|
950
|
+
}
|
|
951
|
+
if (result.status == OSEO_STATUS_NORMAL && index % t_count != 0u) {
|
|
952
|
+
result = normalize_append_decomposed(
|
|
953
|
+
context,
|
|
954
|
+
output,
|
|
955
|
+
t_base + index % t_count
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
return result;
|
|
959
|
+
}
|
|
960
|
+
const OseoUnicodeMapping *mapping = NULL;
|
|
961
|
+
if (compatibility) {
|
|
962
|
+
mapping = unicode_mapping_find(
|
|
963
|
+
oseo_unicode_compat_decomposition,
|
|
964
|
+
sizeof(oseo_unicode_compat_decomposition) /
|
|
965
|
+
sizeof(oseo_unicode_compat_decomposition[0]),
|
|
966
|
+
code_point
|
|
967
|
+
);
|
|
968
|
+
}
|
|
969
|
+
if (mapping == NULL) {
|
|
970
|
+
mapping = unicode_mapping_find(
|
|
971
|
+
oseo_unicode_canonical_decomposition,
|
|
972
|
+
sizeof(oseo_unicode_canonical_decomposition) /
|
|
973
|
+
sizeof(oseo_unicode_canonical_decomposition[0]),
|
|
974
|
+
code_point
|
|
975
|
+
);
|
|
976
|
+
}
|
|
977
|
+
if (mapping == NULL) {
|
|
978
|
+
return normalize_append_decomposed(context, output, code_point);
|
|
979
|
+
}
|
|
980
|
+
OseoResult result = normal(oseo_undefined());
|
|
981
|
+
for (size_t index = 0u;
|
|
982
|
+
result.status == OSEO_STATUS_NORMAL && index < mapping->length;
|
|
983
|
+
index += 1u) {
|
|
984
|
+
result = normalize_decompose(
|
|
985
|
+
context,
|
|
986
|
+
output,
|
|
987
|
+
oseo_unicode_mapping_values[mapping->offset + index],
|
|
988
|
+
compatibility
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
return result;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
static uint32_t normalize_compose_pair(uint32_t first, uint32_t second) {
|
|
995
|
+
const uint32_t s_base = UINT32_C(0xac00);
|
|
996
|
+
const uint32_t l_base = UINT32_C(0x1100);
|
|
997
|
+
const uint32_t v_base = UINT32_C(0x1161);
|
|
998
|
+
const uint32_t t_base = UINT32_C(0x11a7);
|
|
999
|
+
const uint32_t l_count = UINT32_C(19);
|
|
1000
|
+
const uint32_t v_count = UINT32_C(21);
|
|
1001
|
+
const uint32_t t_count = UINT32_C(28);
|
|
1002
|
+
const uint32_t n_count = v_count * t_count;
|
|
1003
|
+
const uint32_t s_count = l_count * n_count;
|
|
1004
|
+
if (first >= l_base && first < l_base + l_count &&
|
|
1005
|
+
second >= v_base && second < v_base + v_count) {
|
|
1006
|
+
return s_base + ((first - l_base) * v_count +
|
|
1007
|
+
(second - v_base)) * t_count;
|
|
1008
|
+
}
|
|
1009
|
+
if (first >= s_base && first < s_base + s_count &&
|
|
1010
|
+
(first - s_base) % t_count == 0u && second > t_base &&
|
|
1011
|
+
second < t_base + t_count) {
|
|
1012
|
+
return first + second - t_base;
|
|
1013
|
+
}
|
|
1014
|
+
size_t count = sizeof(oseo_unicode_compositions) /
|
|
1015
|
+
sizeof(oseo_unicode_compositions[0]);
|
|
1016
|
+
size_t low = 0u;
|
|
1017
|
+
size_t high = count;
|
|
1018
|
+
while (low < high) {
|
|
1019
|
+
size_t middle = low + (high - low) / 2u;
|
|
1020
|
+
OseoUnicodeComposition candidate = oseo_unicode_compositions[middle];
|
|
1021
|
+
if (candidate.first < first ||
|
|
1022
|
+
(candidate.first == first && candidate.second < second)) {
|
|
1023
|
+
low = middle + 1u;
|
|
1024
|
+
} else {
|
|
1025
|
+
high = middle;
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
if (low < count && oseo_unicode_compositions[low].first == first &&
|
|
1029
|
+
oseo_unicode_compositions[low].second == second) {
|
|
1030
|
+
return oseo_unicode_compositions[low].composite;
|
|
1031
|
+
}
|
|
1032
|
+
return 0u;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
static void normalize_compose(OseoCodePointBuilder *output) {
|
|
1036
|
+
if (output->length < 2u) return;
|
|
1037
|
+
size_t result_length = 1u;
|
|
1038
|
+
size_t starter_index = 0u;
|
|
1039
|
+
uint32_t starter = output->values[0];
|
|
1040
|
+
uint8_t prior_combining = unicode_combining_class(starter);
|
|
1041
|
+
for (size_t index = 1u; index < output->length; index += 1u) {
|
|
1042
|
+
uint32_t code_point = output->values[index];
|
|
1043
|
+
uint8_t combining = unicode_combining_class(code_point);
|
|
1044
|
+
uint32_t composite = 0u;
|
|
1045
|
+
if (prior_combining < combining || prior_combining == 0u) {
|
|
1046
|
+
composite = normalize_compose_pair(starter, code_point);
|
|
1047
|
+
}
|
|
1048
|
+
if (composite != 0u) {
|
|
1049
|
+
output->values[starter_index] = composite;
|
|
1050
|
+
starter = composite;
|
|
1051
|
+
continue;
|
|
1052
|
+
}
|
|
1053
|
+
output->values[result_length] = code_point;
|
|
1054
|
+
if (combining == 0u) {
|
|
1055
|
+
starter_index = result_length;
|
|
1056
|
+
starter = code_point;
|
|
1057
|
+
}
|
|
1058
|
+
result_length += 1u;
|
|
1059
|
+
prior_combining = combining;
|
|
1060
|
+
}
|
|
1061
|
+
output->length = result_length;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
static OseoResult normalize_subject(
|
|
1065
|
+
OseoContext *context,
|
|
1066
|
+
OseoValue subject,
|
|
1067
|
+
bool compatibility,
|
|
1068
|
+
bool compose
|
|
1069
|
+
) {
|
|
1070
|
+
OseoCodePointBuilder input = {NULL, 0u, 0u};
|
|
1071
|
+
OseoCodePointBuilder output = {NULL, 0u, 0u};
|
|
1072
|
+
OseoResult result = code_points_from_string(
|
|
1073
|
+
context,
|
|
1074
|
+
string_object(subject),
|
|
1075
|
+
&input
|
|
1076
|
+
);
|
|
1077
|
+
for (size_t index = 0u;
|
|
1078
|
+
result.status == OSEO_STATUS_NORMAL && index < input.length;
|
|
1079
|
+
index += 1u) {
|
|
1080
|
+
result = normalize_decompose(
|
|
1081
|
+
context,
|
|
1082
|
+
&output,
|
|
1083
|
+
input.values[index],
|
|
1084
|
+
compatibility
|
|
1085
|
+
);
|
|
1086
|
+
}
|
|
1087
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1088
|
+
result = normalize_order(context, &output);
|
|
1089
|
+
}
|
|
1090
|
+
if (result.status == OSEO_STATUS_NORMAL && compose) {
|
|
1091
|
+
normalize_compose(&output);
|
|
1092
|
+
}
|
|
1093
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1094
|
+
result = string_from_code_points(context, &output);
|
|
1095
|
+
}
|
|
1096
|
+
code_point_builder_release(&input);
|
|
1097
|
+
code_point_builder_release(&output);
|
|
1098
|
+
return result;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
static OseoResult string_normalize(
|
|
1102
|
+
OseoContext *context,
|
|
1103
|
+
OseoValue receiver,
|
|
1104
|
+
size_t argument_count,
|
|
1105
|
+
const OseoValue *arguments
|
|
1106
|
+
) {
|
|
1107
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1108
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
1109
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1110
|
+
frame.slots[0] = receiver;
|
|
1111
|
+
frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
|
|
1112
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
1113
|
+
frame.slots[2] = result.value;
|
|
1114
|
+
bool compatibility = false;
|
|
1115
|
+
bool compose = true;
|
|
1116
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1117
|
+
tag_of(frame.slots[1]) != OSEO_TAG_UNDEFINED) {
|
|
1118
|
+
result = oseo_internal_value_string(context, frame.slots[1]);
|
|
1119
|
+
frame.slots[1] = result.value;
|
|
1120
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1121
|
+
if (oseo_internal_string_is_ascii(frame.slots[1], "NFC")) {
|
|
1122
|
+
compatibility = false;
|
|
1123
|
+
compose = true;
|
|
1124
|
+
} else if (oseo_internal_string_is_ascii(
|
|
1125
|
+
frame.slots[1], "NFD")) {
|
|
1126
|
+
compatibility = false;
|
|
1127
|
+
compose = false;
|
|
1128
|
+
} else if (oseo_internal_string_is_ascii(
|
|
1129
|
+
frame.slots[1], "NFKC")) {
|
|
1130
|
+
compatibility = true;
|
|
1131
|
+
compose = true;
|
|
1132
|
+
} else if (oseo_internal_string_is_ascii(
|
|
1133
|
+
frame.slots[1], "NFKD")) {
|
|
1134
|
+
compatibility = true;
|
|
1135
|
+
compose = false;
|
|
1136
|
+
} else {
|
|
1137
|
+
result = oseo_internal_throw_error(
|
|
1138
|
+
context,
|
|
1139
|
+
OSEO_ERROR_RANGE,
|
|
1140
|
+
"Invalid Unicode normalization form."
|
|
1141
|
+
);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1146
|
+
result = normalize_subject(
|
|
1147
|
+
context,
|
|
1148
|
+
frame.slots[2],
|
|
1149
|
+
compatibility,
|
|
1150
|
+
compose
|
|
1151
|
+
);
|
|
1152
|
+
}
|
|
1153
|
+
oseo_roots_release(context, &frame);
|
|
1154
|
+
return result;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
static OseoResult string_locale_compare(
|
|
1158
|
+
OseoContext *context,
|
|
1159
|
+
OseoValue receiver,
|
|
1160
|
+
size_t argument_count,
|
|
1161
|
+
const OseoValue *arguments
|
|
1162
|
+
) {
|
|
1163
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1164
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 4u);
|
|
1165
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1166
|
+
frame.slots[0] = receiver;
|
|
1167
|
+
frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
|
|
1168
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
1169
|
+
frame.slots[2] = result.value;
|
|
1170
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1171
|
+
result = oseo_internal_value_string(context, frame.slots[1]);
|
|
1172
|
+
frame.slots[3] = result.value;
|
|
1173
|
+
}
|
|
1174
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1175
|
+
result = normalize_subject(context, frame.slots[2], false, false);
|
|
1176
|
+
frame.slots[2] = result.value;
|
|
1177
|
+
}
|
|
1178
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1179
|
+
result = normalize_subject(context, frame.slots[3], false, false);
|
|
1180
|
+
frame.slots[3] = result.value;
|
|
1181
|
+
}
|
|
1182
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1183
|
+
const OseoString *left = string_object(frame.slots[2]);
|
|
1184
|
+
const OseoString *right = string_object(frame.slots[3]);
|
|
1185
|
+
size_t common = left->length < right->length
|
|
1186
|
+
? left->length
|
|
1187
|
+
: right->length;
|
|
1188
|
+
int comparison = 0;
|
|
1189
|
+
for (size_t index = 0u; index < common; index += 1u) {
|
|
1190
|
+
if (left->units[index] < right->units[index]) {
|
|
1191
|
+
comparison = -1;
|
|
1192
|
+
break;
|
|
1193
|
+
}
|
|
1194
|
+
if (left->units[index] > right->units[index]) {
|
|
1195
|
+
comparison = 1;
|
|
1196
|
+
break;
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
double order = comparison < 0 ||
|
|
1200
|
+
(comparison == 0 && left->length < right->length)
|
|
1201
|
+
? -1.0
|
|
1202
|
+
: comparison > 0 ||
|
|
1203
|
+
(comparison == 0 && left->length > right->length)
|
|
1204
|
+
? 1.0
|
|
1205
|
+
: 0.0;
|
|
1206
|
+
result = normal(oseo_number(order));
|
|
1207
|
+
}
|
|
1208
|
+
oseo_roots_release(context, &frame);
|
|
1209
|
+
return result;
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/* ToUint16 (7.1.7) over an already converted Number. */
|
|
1213
|
+
static uint16_t string_to_uint16(double value) {
|
|
1214
|
+
if (!isfinite(value) || value == 0.0) return 0u;
|
|
1215
|
+
double modulo = fmod(trunc(value), 65536.0);
|
|
1216
|
+
if (modulo < 0.0) modulo += 65536.0;
|
|
1217
|
+
return (uint16_t)modulo;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
static OseoValue string_builtin_argument(
|
|
1221
|
+
size_t argument_count,
|
|
1222
|
+
const OseoValue *arguments,
|
|
1223
|
+
size_t index
|
|
1224
|
+
) {
|
|
1225
|
+
return index < argument_count ? arguments[index] : oseo_undefined();
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
/* ToIntegerOrInfinity over one already rooted input value. */
|
|
1229
|
+
static OseoResult string_integer_or_infinity(
|
|
1230
|
+
OseoContext *context,
|
|
1231
|
+
OseoValue value
|
|
1232
|
+
) {
|
|
1233
|
+
OseoResult result = oseo_internal_to_number(context, value);
|
|
1234
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1235
|
+
double number = number_value(result.value);
|
|
1236
|
+
if (isnan(number) || number == 0.0) number = 0.0;
|
|
1237
|
+
else if (isfinite(number)) number = trunc(number);
|
|
1238
|
+
return normal(oseo_number(number));
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
static OseoResult string_method_subject(
|
|
1242
|
+
OseoContext *context,
|
|
1243
|
+
OseoValue receiver
|
|
1244
|
+
) {
|
|
1245
|
+
if (is_nullish(receiver)) {
|
|
1246
|
+
return oseo_internal_throw_error(
|
|
1247
|
+
context,
|
|
1248
|
+
OSEO_ERROR_TYPE,
|
|
1249
|
+
"String method receiver is nullish."
|
|
1250
|
+
);
|
|
1251
|
+
}
|
|
1252
|
+
return oseo_internal_value_string(context, receiver);
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
static OseoResult string_repeat(
|
|
1256
|
+
OseoContext *context,
|
|
1257
|
+
OseoValue receiver,
|
|
1258
|
+
size_t argument_count,
|
|
1259
|
+
const OseoValue *arguments
|
|
1260
|
+
) {
|
|
1261
|
+
OseoValue slots[3] = {
|
|
1262
|
+
receiver,
|
|
1263
|
+
string_builtin_argument(argument_count, arguments, 0u),
|
|
1264
|
+
oseo_undefined(),
|
|
1265
|
+
};
|
|
1266
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1267
|
+
oseo_roots_push(context, &frame);
|
|
1268
|
+
OseoResult result = string_method_subject(context, slots[0]);
|
|
1269
|
+
slots[2] = result.value;
|
|
1270
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1271
|
+
result = string_integer_or_infinity(context, slots[1]);
|
|
1272
|
+
}
|
|
1273
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1274
|
+
oseo_roots_pop(context, &frame);
|
|
1275
|
+
return result;
|
|
1276
|
+
}
|
|
1277
|
+
double count_number = number_value(result.value);
|
|
1278
|
+
if (count_number < 0.0 || count_number == INFINITY) {
|
|
1279
|
+
result = oseo_internal_throw_error(
|
|
1280
|
+
context,
|
|
1281
|
+
OSEO_ERROR_RANGE,
|
|
1282
|
+
"Invalid repeat count."
|
|
1283
|
+
);
|
|
1284
|
+
oseo_roots_pop(context, &frame);
|
|
1285
|
+
return result;
|
|
1286
|
+
}
|
|
1287
|
+
const OseoString *subject = string_object(slots[2]);
|
|
1288
|
+
if (subject->length == 0u || count_number == 0.0) {
|
|
1289
|
+
result = oseo_internal_allocate_string(context, NULL, 0u);
|
|
1290
|
+
oseo_roots_pop(context, &frame);
|
|
1291
|
+
return result;
|
|
1292
|
+
}
|
|
1293
|
+
if (count_number > (double)(OSEO_MAX_STRING_LENGTH / subject->length)) {
|
|
1294
|
+
result = oseo_internal_throw_error(
|
|
1295
|
+
context,
|
|
1296
|
+
OSEO_ERROR_RANGE,
|
|
1297
|
+
"Invalid string length."
|
|
1298
|
+
);
|
|
1299
|
+
oseo_roots_pop(context, &frame);
|
|
1300
|
+
return result;
|
|
1301
|
+
}
|
|
1302
|
+
size_t count = (size_t)count_number;
|
|
1303
|
+
size_t length = subject->length * count;
|
|
1304
|
+
uint16_t *units = malloc(length * sizeof(uint16_t));
|
|
1305
|
+
if (units == NULL) {
|
|
1306
|
+
result = failure(context, "OSEO2001", "String allocation failed.");
|
|
1307
|
+
oseo_roots_pop(context, &frame);
|
|
1308
|
+
return result;
|
|
1309
|
+
}
|
|
1310
|
+
memcpy(units, subject->units, subject->length * sizeof(uint16_t));
|
|
1311
|
+
size_t copied = subject->length;
|
|
1312
|
+
while (copied < length) {
|
|
1313
|
+
size_t chunk = copied < length - copied ? copied : length - copied;
|
|
1314
|
+
memcpy(units + copied, units, chunk * sizeof(uint16_t));
|
|
1315
|
+
copied += chunk;
|
|
1316
|
+
}
|
|
1317
|
+
result = oseo_internal_allocate_string(context, units, length);
|
|
1318
|
+
free(units);
|
|
1319
|
+
oseo_roots_pop(context, &frame);
|
|
1320
|
+
return result;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
static OseoResult string_pad(
|
|
1324
|
+
OseoContext *context,
|
|
1325
|
+
OseoValue receiver,
|
|
1326
|
+
size_t argument_count,
|
|
1327
|
+
const OseoValue *arguments,
|
|
1328
|
+
bool at_start
|
|
1329
|
+
) {
|
|
1330
|
+
OseoValue slots[5] = {
|
|
1331
|
+
receiver,
|
|
1332
|
+
string_builtin_argument(argument_count, arguments, 0u),
|
|
1333
|
+
string_builtin_argument(argument_count, arguments, 1u),
|
|
1334
|
+
oseo_undefined(),
|
|
1335
|
+
oseo_undefined(),
|
|
1336
|
+
};
|
|
1337
|
+
OseoRootFrame frame = {NULL, slots, 5u};
|
|
1338
|
+
oseo_roots_push(context, &frame);
|
|
1339
|
+
OseoResult result = string_method_subject(context, slots[0]);
|
|
1340
|
+
slots[3] = result.value;
|
|
1341
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1342
|
+
result = string_integer_or_infinity(context, slots[1]);
|
|
1343
|
+
}
|
|
1344
|
+
double target_number = 0.0;
|
|
1345
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1346
|
+
target_number = number_value(result.value);
|
|
1347
|
+
if (!(target_number > 0.0)) target_number = 0.0;
|
|
1348
|
+
else if (target_number == INFINITY ||
|
|
1349
|
+
target_number > 9007199254740991.0) {
|
|
1350
|
+
target_number = 9007199254740991.0;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1354
|
+
oseo_roots_pop(context, &frame);
|
|
1355
|
+
return result;
|
|
1356
|
+
}
|
|
1357
|
+
const OseoString *subject = string_object(slots[3]);
|
|
1358
|
+
if (target_number <= (double)subject->length) {
|
|
1359
|
+
result = normal(slots[3]);
|
|
1360
|
+
oseo_roots_pop(context, &frame);
|
|
1361
|
+
return result;
|
|
1362
|
+
}
|
|
1363
|
+
if (tag_of(slots[2]) == OSEO_TAG_UNDEFINED) {
|
|
1364
|
+
result = oseo_internal_ascii_string(context, " ");
|
|
1365
|
+
slots[4] = result.value;
|
|
1366
|
+
} else {
|
|
1367
|
+
result = oseo_internal_value_string(context, slots[2]);
|
|
1368
|
+
slots[4] = result.value;
|
|
1369
|
+
}
|
|
1370
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1371
|
+
oseo_roots_pop(context, &frame);
|
|
1372
|
+
return result;
|
|
1373
|
+
}
|
|
1374
|
+
const OseoString *filler = string_object(slots[4]);
|
|
1375
|
+
if (filler->length == 0u) {
|
|
1376
|
+
result = normal(slots[3]);
|
|
1377
|
+
oseo_roots_pop(context, &frame);
|
|
1378
|
+
return result;
|
|
1379
|
+
}
|
|
1380
|
+
if (target_number > (double)OSEO_MAX_STRING_LENGTH) {
|
|
1381
|
+
result = oseo_internal_throw_error(
|
|
1382
|
+
context,
|
|
1383
|
+
OSEO_ERROR_RANGE,
|
|
1384
|
+
"Invalid string length."
|
|
1385
|
+
);
|
|
1386
|
+
oseo_roots_pop(context, &frame);
|
|
1387
|
+
return result;
|
|
1388
|
+
}
|
|
1389
|
+
size_t target = (size_t)target_number;
|
|
1390
|
+
size_t fill_length = target - subject->length;
|
|
1391
|
+
uint16_t *units = malloc(target * sizeof(uint16_t));
|
|
1392
|
+
if (units == NULL) {
|
|
1393
|
+
result = failure(context, "OSEO2001", "String allocation failed.");
|
|
1394
|
+
oseo_roots_pop(context, &frame);
|
|
1395
|
+
return result;
|
|
1396
|
+
}
|
|
1397
|
+
size_t fill_offset = at_start ? 0u : subject->length;
|
|
1398
|
+
for (size_t index = 0u; index < fill_length; index += 1u) {
|
|
1399
|
+
units[fill_offset + index] = filler->units[index % filler->length];
|
|
1400
|
+
}
|
|
1401
|
+
size_t subject_offset = at_start ? fill_length : 0u;
|
|
1402
|
+
memcpy(
|
|
1403
|
+
units + subject_offset,
|
|
1404
|
+
subject->units,
|
|
1405
|
+
subject->length * sizeof(uint16_t)
|
|
1406
|
+
);
|
|
1407
|
+
result = oseo_internal_allocate_string(context, units, target);
|
|
1408
|
+
free(units);
|
|
1409
|
+
oseo_roots_pop(context, &frame);
|
|
1410
|
+
return result;
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
static bool string_is_leading_surrogate(uint16_t unit) {
|
|
1414
|
+
return unit >= UINT16_C(0xd800) && unit <= UINT16_C(0xdbff);
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
static bool string_is_trailing_surrogate(uint16_t unit) {
|
|
1418
|
+
return unit >= UINT16_C(0xdc00) && unit <= UINT16_C(0xdfff);
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
static bool string_is_well_formed_value(const OseoString *subject) {
|
|
1422
|
+
for (size_t index = 0u; index < subject->length; index += 1u) {
|
|
1423
|
+
uint16_t unit = subject->units[index];
|
|
1424
|
+
if (string_is_leading_surrogate(unit)) {
|
|
1425
|
+
if (index + 1u >= subject->length ||
|
|
1426
|
+
!string_is_trailing_surrogate(subject->units[index + 1u])) {
|
|
1427
|
+
return false;
|
|
1428
|
+
}
|
|
1429
|
+
index += 1u;
|
|
1430
|
+
} else if (string_is_trailing_surrogate(unit)) {
|
|
1431
|
+
return false;
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
return true;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
static OseoResult string_well_formed(
|
|
1438
|
+
OseoContext *context,
|
|
1439
|
+
OseoValue receiver,
|
|
1440
|
+
bool repair
|
|
1441
|
+
) {
|
|
1442
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
1443
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1444
|
+
oseo_roots_push(context, &frame);
|
|
1445
|
+
OseoResult result = string_method_subject(context, slots[0]);
|
|
1446
|
+
slots[1] = result.value;
|
|
1447
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1448
|
+
oseo_roots_pop(context, &frame);
|
|
1449
|
+
return result;
|
|
1450
|
+
}
|
|
1451
|
+
const OseoString *subject = string_object(slots[1]);
|
|
1452
|
+
bool well_formed = string_is_well_formed_value(subject);
|
|
1453
|
+
if (!repair) {
|
|
1454
|
+
result = normal(oseo_boolean(well_formed));
|
|
1455
|
+
oseo_roots_pop(context, &frame);
|
|
1456
|
+
return result;
|
|
1457
|
+
}
|
|
1458
|
+
if (well_formed) {
|
|
1459
|
+
result = normal(slots[1]);
|
|
1460
|
+
oseo_roots_pop(context, &frame);
|
|
1461
|
+
return result;
|
|
1462
|
+
}
|
|
1463
|
+
uint16_t *units = malloc(subject->length * sizeof(uint16_t));
|
|
1464
|
+
if (units == NULL) {
|
|
1465
|
+
result = failure(context, "OSEO2001", "String allocation failed.");
|
|
1466
|
+
oseo_roots_pop(context, &frame);
|
|
1467
|
+
return result;
|
|
1468
|
+
}
|
|
1469
|
+
for (size_t index = 0u; index < subject->length; index += 1u) {
|
|
1470
|
+
uint16_t unit = subject->units[index];
|
|
1471
|
+
bool paired_lead = string_is_leading_surrogate(unit) &&
|
|
1472
|
+
index + 1u < subject->length &&
|
|
1473
|
+
string_is_trailing_surrogate(subject->units[index + 1u]);
|
|
1474
|
+
units[index] = string_is_trailing_surrogate(unit) ||
|
|
1475
|
+
(string_is_leading_surrogate(unit) && !paired_lead)
|
|
1476
|
+
? UINT16_C(0xfffd)
|
|
1477
|
+
: unit;
|
|
1478
|
+
if (paired_lead) {
|
|
1479
|
+
index += 1u;
|
|
1480
|
+
units[index] = subject->units[index];
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
result = oseo_internal_allocate_string(
|
|
1484
|
+
context,
|
|
1485
|
+
units,
|
|
1486
|
+
subject->length
|
|
1487
|
+
);
|
|
1488
|
+
free(units);
|
|
1489
|
+
oseo_roots_pop(context, &frame);
|
|
1490
|
+
return result;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
static OseoResult string_at_index(
|
|
1494
|
+
OseoContext *context,
|
|
1495
|
+
size_t code_id,
|
|
1496
|
+
OseoValue receiver,
|
|
1497
|
+
size_t argument_count,
|
|
1498
|
+
const OseoValue *arguments
|
|
1499
|
+
) {
|
|
1500
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1501
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
1502
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1503
|
+
frame.slots[0] = receiver;
|
|
1504
|
+
frame.slots[1] = string_builtin_argument(
|
|
1505
|
+
argument_count,
|
|
1506
|
+
arguments,
|
|
1507
|
+
0u
|
|
1508
|
+
);
|
|
1509
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
1510
|
+
frame.slots[2] = result.value;
|
|
1511
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1512
|
+
result = string_integer_or_infinity(context, frame.slots[1]);
|
|
1513
|
+
}
|
|
1514
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1515
|
+
oseo_roots_release(context, &frame);
|
|
1516
|
+
return result;
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
double position = number_value(result.value);
|
|
1520
|
+
size_t length = string_object(frame.slots[2])->length;
|
|
1521
|
+
if (code_id == OSEO_STRING_AT_CODE_ID && position < 0.0) {
|
|
1522
|
+
position += (double)length;
|
|
1523
|
+
}
|
|
1524
|
+
if (position < 0.0 || !(position < (double)length)) {
|
|
1525
|
+
if (code_id == OSEO_STRING_CHAR_AT_CODE_ID) {
|
|
1526
|
+
result = oseo_internal_allocate_string(context, NULL, 0u);
|
|
1527
|
+
} else if (code_id == OSEO_STRING_CHAR_CODE_AT_CODE_ID) {
|
|
1528
|
+
result = normal(oseo_number(NAN));
|
|
1529
|
+
} else {
|
|
1530
|
+
result = normal(oseo_undefined());
|
|
1531
|
+
}
|
|
1532
|
+
oseo_roots_release(context, &frame);
|
|
1533
|
+
return result;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
size_t index = (size_t)position;
|
|
1537
|
+
uint16_t first = string_object(frame.slots[2])->units[index];
|
|
1538
|
+
if (code_id == OSEO_STRING_CHAR_CODE_AT_CODE_ID) {
|
|
1539
|
+
result = normal(oseo_number((double)first));
|
|
1540
|
+
} else if (code_id == OSEO_STRING_CODE_POINT_AT_CODE_ID &&
|
|
1541
|
+
first >= UINT16_C(0xd800) &&
|
|
1542
|
+
first <= UINT16_C(0xdbff) && index + 1u < length) {
|
|
1543
|
+
uint16_t second = string_object(frame.slots[2])->units[index + 1u];
|
|
1544
|
+
if (second >= UINT16_C(0xdc00) && second <= UINT16_C(0xdfff)) {
|
|
1545
|
+
uint32_t code_point = UINT32_C(0x10000) +
|
|
1546
|
+
((uint32_t)(first - UINT16_C(0xd800)) << 10u) +
|
|
1547
|
+
(uint32_t)(second - UINT16_C(0xdc00));
|
|
1548
|
+
result = normal(oseo_number((double)code_point));
|
|
1549
|
+
} else {
|
|
1550
|
+
result = normal(oseo_number((double)first));
|
|
1551
|
+
}
|
|
1552
|
+
} else if (code_id == OSEO_STRING_CODE_POINT_AT_CODE_ID) {
|
|
1553
|
+
result = normal(oseo_number((double)first));
|
|
1554
|
+
} else {
|
|
1555
|
+
result = oseo_internal_allocate_string(context, &first, 1u);
|
|
1556
|
+
}
|
|
1557
|
+
oseo_roots_release(context, &frame);
|
|
1558
|
+
return result;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
static OseoResult string_this_value(
|
|
1562
|
+
OseoContext *context,
|
|
1563
|
+
OseoValue receiver
|
|
1564
|
+
) {
|
|
1565
|
+
if (is_string(receiver)) return normal(receiver);
|
|
1566
|
+
if (oseo_internal_string_data(receiver)) {
|
|
1567
|
+
return normal(ordinary_object(receiver)->primitive_value);
|
|
1568
|
+
}
|
|
1569
|
+
return oseo_internal_throw_error(
|
|
1570
|
+
context,
|
|
1571
|
+
OSEO_ERROR_TYPE,
|
|
1572
|
+
"String.prototype method requires a String receiver."
|
|
1573
|
+
);
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
static bool string_matches_at(
|
|
1577
|
+
const OseoString *subject,
|
|
1578
|
+
const OseoString *search,
|
|
1579
|
+
size_t position
|
|
1580
|
+
) {
|
|
1581
|
+
if (position > subject->length ||
|
|
1582
|
+
search->length > subject->length - position) return false;
|
|
1583
|
+
return search->length == 0u ||
|
|
1584
|
+
memcmp(
|
|
1585
|
+
subject->units + position,
|
|
1586
|
+
search->units,
|
|
1587
|
+
search->length * sizeof(uint16_t)
|
|
1588
|
+
) == 0;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
static size_t string_clamped_position(double position, size_t length) {
|
|
1592
|
+
if (!(position > 0.0)) return 0u;
|
|
1593
|
+
if (!(position < (double)length)) return length;
|
|
1594
|
+
return (size_t)position;
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
static size_t string_relative_position(double position, size_t length) {
|
|
1598
|
+
if (position == -INFINITY) return 0u;
|
|
1599
|
+
if (position < 0.0) {
|
|
1600
|
+
double relative = (double)length + position;
|
|
1601
|
+
return relative > 0.0 ? (size_t)relative : 0u;
|
|
1602
|
+
}
|
|
1603
|
+
return string_clamped_position(position, length);
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
static OseoResult string_is_regexp(
|
|
1607
|
+
OseoContext *context,
|
|
1608
|
+
OseoValue value,
|
|
1609
|
+
bool *regexp
|
|
1610
|
+
) {
|
|
1611
|
+
return oseo_internal_is_regexp(context, value, regexp);
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
static OseoResult string_concat(
|
|
1615
|
+
OseoContext *context,
|
|
1616
|
+
OseoValue receiver,
|
|
1617
|
+
size_t argument_count,
|
|
1618
|
+
const OseoValue *arguments
|
|
1619
|
+
) {
|
|
1620
|
+
OseoStringBuilder builder = {NULL, 0u, 0u};
|
|
1621
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1622
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
1623
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1624
|
+
frame.slots[0] = receiver;
|
|
1625
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
1626
|
+
frame.slots[1] = result.value;
|
|
1627
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1628
|
+
result = oseo_internal_string_builder_append(
|
|
1629
|
+
context,
|
|
1630
|
+
&builder,
|
|
1631
|
+
string_object(frame.slots[1])->units,
|
|
1632
|
+
string_object(frame.slots[1])->length
|
|
1633
|
+
);
|
|
1634
|
+
}
|
|
1635
|
+
for (size_t index = 0u;
|
|
1636
|
+
result.status == OSEO_STATUS_NORMAL && index < argument_count;
|
|
1637
|
+
index += 1u) {
|
|
1638
|
+
result = oseo_internal_value_string(context, arguments[index]);
|
|
1639
|
+
frame.slots[1] = result.value;
|
|
1640
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1641
|
+
result = oseo_internal_string_builder_append(
|
|
1642
|
+
context,
|
|
1643
|
+
&builder,
|
|
1644
|
+
string_object(frame.slots[1])->units,
|
|
1645
|
+
string_object(frame.slots[1])->length
|
|
1646
|
+
);
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1650
|
+
result = oseo_internal_allocate_string(
|
|
1651
|
+
context,
|
|
1652
|
+
builder.units,
|
|
1653
|
+
builder.length
|
|
1654
|
+
);
|
|
1655
|
+
}
|
|
1656
|
+
oseo_internal_string_builder_release(&builder);
|
|
1657
|
+
oseo_roots_release(context, &frame);
|
|
1658
|
+
return result;
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
static OseoResult string_search(
|
|
1662
|
+
OseoContext *context,
|
|
1663
|
+
size_t code_id,
|
|
1664
|
+
OseoValue receiver,
|
|
1665
|
+
size_t argument_count,
|
|
1666
|
+
const OseoValue *arguments
|
|
1667
|
+
) {
|
|
1668
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1669
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 5u);
|
|
1670
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1671
|
+
frame.slots[0] = receiver;
|
|
1672
|
+
frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
|
|
1673
|
+
frame.slots[2] = string_builtin_argument(argument_count, arguments, 1u);
|
|
1674
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
1675
|
+
frame.slots[3] = result.value;
|
|
1676
|
+
|
|
1677
|
+
bool rejects_regexp = code_id == OSEO_STRING_INCLUDES_CODE_ID ||
|
|
1678
|
+
code_id == OSEO_STRING_STARTS_WITH_CODE_ID ||
|
|
1679
|
+
code_id == OSEO_STRING_ENDS_WITH_CODE_ID;
|
|
1680
|
+
bool regexp = false;
|
|
1681
|
+
if (result.status == OSEO_STATUS_NORMAL && rejects_regexp) {
|
|
1682
|
+
result = string_is_regexp(context, frame.slots[1], ®exp);
|
|
1683
|
+
}
|
|
1684
|
+
if (result.status == OSEO_STATUS_NORMAL && regexp) {
|
|
1685
|
+
result = oseo_internal_throw_error(
|
|
1686
|
+
context,
|
|
1687
|
+
OSEO_ERROR_TYPE,
|
|
1688
|
+
"String search argument must not be a RegExp."
|
|
1689
|
+
);
|
|
1690
|
+
}
|
|
1691
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1692
|
+
result = oseo_internal_value_string(context, frame.slots[1]);
|
|
1693
|
+
frame.slots[4] = result.value;
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
double position = 0.0;
|
|
1697
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1698
|
+
code_id == OSEO_STRING_LAST_INDEX_OF_CODE_ID) {
|
|
1699
|
+
result = oseo_internal_to_number(context, frame.slots[2]);
|
|
1700
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1701
|
+
position = number_value(result.value);
|
|
1702
|
+
if (isnan(position)) position = INFINITY;
|
|
1703
|
+
else if (isfinite(position)) position = trunc(position);
|
|
1704
|
+
}
|
|
1705
|
+
} else if (result.status == OSEO_STATUS_NORMAL &&
|
|
1706
|
+
code_id == OSEO_STRING_ENDS_WITH_CODE_ID &&
|
|
1707
|
+
tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
|
|
1708
|
+
position = (double)string_object(frame.slots[3])->length;
|
|
1709
|
+
} else if (result.status == OSEO_STATUS_NORMAL) {
|
|
1710
|
+
result = string_integer_or_infinity(context, frame.slots[2]);
|
|
1711
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1712
|
+
position = number_value(result.value);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1716
|
+
oseo_roots_release(context, &frame);
|
|
1717
|
+
return result;
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
const OseoString *subject = string_object(frame.slots[3]);
|
|
1721
|
+
const OseoString *search = string_object(frame.slots[4]);
|
|
1722
|
+
size_t start = string_clamped_position(position, subject->length);
|
|
1723
|
+
if (code_id == OSEO_STRING_STARTS_WITH_CODE_ID) {
|
|
1724
|
+
result = normal(oseo_boolean(string_matches_at(
|
|
1725
|
+
subject,
|
|
1726
|
+
search,
|
|
1727
|
+
start
|
|
1728
|
+
)));
|
|
1729
|
+
} else if (code_id == OSEO_STRING_ENDS_WITH_CODE_ID) {
|
|
1730
|
+
bool matches = search->length <= start &&
|
|
1731
|
+
string_matches_at(subject, search, start - search->length);
|
|
1732
|
+
result = normal(oseo_boolean(matches));
|
|
1733
|
+
} else if (code_id == OSEO_STRING_INDEX_OF_CODE_ID ||
|
|
1734
|
+
code_id == OSEO_STRING_INCLUDES_CODE_ID) {
|
|
1735
|
+
size_t found = SIZE_MAX;
|
|
1736
|
+
if (search->length <= subject->length) {
|
|
1737
|
+
size_t limit = subject->length - search->length;
|
|
1738
|
+
for (size_t index = start; index <= limit; index += 1u) {
|
|
1739
|
+
if (string_matches_at(subject, search, index)) {
|
|
1740
|
+
found = index;
|
|
1741
|
+
break;
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
result = code_id == OSEO_STRING_INCLUDES_CODE_ID
|
|
1746
|
+
? normal(oseo_boolean(found != SIZE_MAX))
|
|
1747
|
+
: normal(oseo_number(
|
|
1748
|
+
found == SIZE_MAX ? -1.0 : (double)found));
|
|
1749
|
+
} else {
|
|
1750
|
+
size_t found = SIZE_MAX;
|
|
1751
|
+
if (search->length <= subject->length) {
|
|
1752
|
+
size_t latest = subject->length - search->length;
|
|
1753
|
+
if (start < latest) latest = start;
|
|
1754
|
+
for (size_t index = latest;; index -= 1u) {
|
|
1755
|
+
if (string_matches_at(subject, search, index)) {
|
|
1756
|
+
found = index;
|
|
1757
|
+
break;
|
|
1758
|
+
}
|
|
1759
|
+
if (index == 0u) break;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
result = normal(oseo_number(
|
|
1763
|
+
found == SIZE_MAX ? -1.0 : (double)found));
|
|
1764
|
+
}
|
|
1765
|
+
oseo_roots_release(context, &frame);
|
|
1766
|
+
return result;
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
static OseoResult string_slice(
|
|
1770
|
+
OseoContext *context,
|
|
1771
|
+
size_t code_id,
|
|
1772
|
+
OseoValue receiver,
|
|
1773
|
+
size_t argument_count,
|
|
1774
|
+
const OseoValue *arguments
|
|
1775
|
+
) {
|
|
1776
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1777
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 4u);
|
|
1778
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1779
|
+
frame.slots[0] = receiver;
|
|
1780
|
+
frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
|
|
1781
|
+
frame.slots[2] = string_builtin_argument(argument_count, arguments, 1u);
|
|
1782
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
1783
|
+
frame.slots[3] = result.value;
|
|
1784
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1785
|
+
result = string_integer_or_infinity(context, frame.slots[1]);
|
|
1786
|
+
}
|
|
1787
|
+
double start = result.status == OSEO_STATUS_NORMAL
|
|
1788
|
+
? number_value(result.value)
|
|
1789
|
+
: 0.0;
|
|
1790
|
+
double end = 0.0;
|
|
1791
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1792
|
+
tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
|
|
1793
|
+
end = (double)string_object(frame.slots[3])->length;
|
|
1794
|
+
} else if (result.status == OSEO_STATUS_NORMAL) {
|
|
1795
|
+
result = string_integer_or_infinity(context, frame.slots[2]);
|
|
1796
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1797
|
+
end = number_value(result.value);
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1801
|
+
oseo_roots_release(context, &frame);
|
|
1802
|
+
return result;
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
const OseoString *subject = string_object(frame.slots[3]);
|
|
1806
|
+
size_t from;
|
|
1807
|
+
size_t to;
|
|
1808
|
+
if (code_id == OSEO_STRING_SLICE_CODE_ID) {
|
|
1809
|
+
from = string_relative_position(start, subject->length);
|
|
1810
|
+
to = string_relative_position(end, subject->length);
|
|
1811
|
+
if (to < from) to = from;
|
|
1812
|
+
} else {
|
|
1813
|
+
from = string_clamped_position(start, subject->length);
|
|
1814
|
+
to = string_clamped_position(end, subject->length);
|
|
1815
|
+
if (from > to) {
|
|
1816
|
+
size_t swap = from;
|
|
1817
|
+
from = to;
|
|
1818
|
+
to = swap;
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
result = oseo_internal_allocate_string(
|
|
1822
|
+
context,
|
|
1823
|
+
subject->units + from,
|
|
1824
|
+
to - from
|
|
1825
|
+
);
|
|
1826
|
+
oseo_roots_release(context, &frame);
|
|
1827
|
+
return result;
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
/*
|
|
1831
|
+
* String.prototype.substr(start, length), B.2.2.1. The start is relative
|
|
1832
|
+
* to the end when negative, the length is clamped to what remains, and
|
|
1833
|
+
* an absent length takes the rest of the string. Both conversions run
|
|
1834
|
+
* after the receiver's ToString, start before length.
|
|
1835
|
+
*/
|
|
1836
|
+
static OseoResult string_substr(
|
|
1837
|
+
OseoContext *context,
|
|
1838
|
+
OseoValue receiver,
|
|
1839
|
+
size_t argument_count,
|
|
1840
|
+
const OseoValue *arguments
|
|
1841
|
+
) {
|
|
1842
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1843
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 4u);
|
|
1844
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1845
|
+
frame.slots[0] = receiver;
|
|
1846
|
+
frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
|
|
1847
|
+
frame.slots[2] = string_builtin_argument(argument_count, arguments, 1u);
|
|
1848
|
+
result = string_method_subject(context, frame.slots[0]);
|
|
1849
|
+
frame.slots[3] = result.value;
|
|
1850
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1851
|
+
result = string_integer_or_infinity(context, frame.slots[1]);
|
|
1852
|
+
}
|
|
1853
|
+
double start = result.status == OSEO_STATUS_NORMAL
|
|
1854
|
+
? number_value(result.value)
|
|
1855
|
+
: 0.0;
|
|
1856
|
+
double length = 0.0;
|
|
1857
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1858
|
+
tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
|
|
1859
|
+
length = (double)string_object(frame.slots[3])->length;
|
|
1860
|
+
} else if (result.status == OSEO_STATUS_NORMAL) {
|
|
1861
|
+
result = string_integer_or_infinity(context, frame.slots[2]);
|
|
1862
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1863
|
+
length = number_value(result.value);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1867
|
+
oseo_roots_release(context, &frame);
|
|
1868
|
+
return result;
|
|
1869
|
+
}
|
|
1870
|
+
const OseoString *subject = string_object(frame.slots[3]);
|
|
1871
|
+
size_t from = string_relative_position(start, subject->length);
|
|
1872
|
+
size_t count = string_clamped_position(length, subject->length - from);
|
|
1873
|
+
result = oseo_internal_allocate_string(
|
|
1874
|
+
context,
|
|
1875
|
+
subject->units + from,
|
|
1876
|
+
count
|
|
1877
|
+
);
|
|
1878
|
+
oseo_roots_release(context, &frame);
|
|
1879
|
+
return result;
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
/*
|
|
1883
|
+
* One Annex B HTML method of B.2.2.2 through B.2.2.14: the tag it wraps
|
|
1884
|
+
* the string in and the attribute it writes, or an empty attribute for
|
|
1885
|
+
* the eight methods that take no argument.
|
|
1886
|
+
*/
|
|
1887
|
+
typedef struct {
|
|
1888
|
+
const char *name;
|
|
1889
|
+
const char *tag;
|
|
1890
|
+
const char *attribute;
|
|
1891
|
+
} OseoStringHtmlMethod;
|
|
1892
|
+
|
|
1893
|
+
static const OseoStringHtmlMethod string_html_methods[] = {
|
|
1894
|
+
{"anchor", "a", "name"},
|
|
1895
|
+
{"big", "big", ""},
|
|
1896
|
+
{"blink", "blink", ""},
|
|
1897
|
+
{"bold", "b", ""},
|
|
1898
|
+
{"fixed", "tt", ""},
|
|
1899
|
+
{"fontcolor", "font", "color"},
|
|
1900
|
+
{"fontsize", "font", "size"},
|
|
1901
|
+
{"italics", "i", ""},
|
|
1902
|
+
{"link", "a", "href"},
|
|
1903
|
+
{"small", "small", ""},
|
|
1904
|
+
{"strike", "strike", ""},
|
|
1905
|
+
{"sub", "sub", ""},
|
|
1906
|
+
{"sup", "sup", ""},
|
|
1907
|
+
};
|
|
1908
|
+
|
|
1909
|
+
_Static_assert(
|
|
1910
|
+
sizeof(string_html_methods) / sizeof(string_html_methods[0]) ==
|
|
1911
|
+
OSEO_STRING_HTML_METHOD_COUNT,
|
|
1912
|
+
"The HTML method table must match its reviewed code-ID range."
|
|
1913
|
+
);
|
|
1914
|
+
|
|
1915
|
+
/* Append the ASCII text `text` to `builder`. */
|
|
1916
|
+
static OseoResult string_builder_append_ascii(
|
|
1917
|
+
OseoContext *context,
|
|
1918
|
+
OseoStringBuilder *builder,
|
|
1919
|
+
const char *text
|
|
1920
|
+
) {
|
|
1921
|
+
OseoResult result = normal(oseo_undefined());
|
|
1922
|
+
for (const char *unit = text;
|
|
1923
|
+
result.status == OSEO_STATUS_NORMAL && *unit != '\0';
|
|
1924
|
+
unit += 1) {
|
|
1925
|
+
result = string_builder_append_unit(
|
|
1926
|
+
context,
|
|
1927
|
+
builder,
|
|
1928
|
+
(uint16_t)(unsigned char)*unit
|
|
1929
|
+
);
|
|
1930
|
+
}
|
|
1931
|
+
return result;
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
/*
|
|
1935
|
+
* CreateHTML(string, tag, attribute, value), B.2.2.2.1. The receiver's
|
|
1936
|
+
* ToString runs before the attribute value's, and every quotation mark
|
|
1937
|
+
* in the attribute value becomes `"`; no other unit is escaped.
|
|
1938
|
+
*/
|
|
1939
|
+
static OseoResult string_create_html(
|
|
1940
|
+
OseoContext *context,
|
|
1941
|
+
const OseoStringHtmlMethod *method,
|
|
1942
|
+
OseoValue receiver,
|
|
1943
|
+
size_t argument_count,
|
|
1944
|
+
const OseoValue *arguments
|
|
1945
|
+
) {
|
|
1946
|
+
OseoValue slots[3] = {
|
|
1947
|
+
receiver,
|
|
1948
|
+
string_builtin_argument(argument_count, arguments, 0u),
|
|
1949
|
+
oseo_undefined(),
|
|
1950
|
+
};
|
|
1951
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1952
|
+
oseo_roots_push(context, &frame);
|
|
1953
|
+
OseoStringBuilder builder = {NULL, 0u, 0u};
|
|
1954
|
+
bool attributed = method->attribute[0] != '\0';
|
|
1955
|
+
OseoResult result = string_method_subject(context, slots[0]);
|
|
1956
|
+
slots[0] = result.value;
|
|
1957
|
+
if (result.status == OSEO_STATUS_NORMAL && attributed) {
|
|
1958
|
+
result = oseo_internal_value_string(context, slots[1]);
|
|
1959
|
+
slots[2] = result.value;
|
|
1960
|
+
}
|
|
1961
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1962
|
+
result = string_builder_append_ascii(context, &builder, "<");
|
|
1963
|
+
}
|
|
1964
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1965
|
+
result = string_builder_append_ascii(context, &builder, method->tag);
|
|
1966
|
+
}
|
|
1967
|
+
if (result.status == OSEO_STATUS_NORMAL && attributed) {
|
|
1968
|
+
result = string_builder_append_ascii(context, &builder, " ");
|
|
1969
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1970
|
+
result = string_builder_append_ascii(
|
|
1971
|
+
context,
|
|
1972
|
+
&builder,
|
|
1973
|
+
method->attribute
|
|
1974
|
+
);
|
|
1975
|
+
}
|
|
1976
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1977
|
+
result = string_builder_append_ascii(context, &builder, "=\"");
|
|
1978
|
+
}
|
|
1979
|
+
const OseoString *value = result.status == OSEO_STATUS_NORMAL
|
|
1980
|
+
? string_object(slots[2])
|
|
1981
|
+
: NULL;
|
|
1982
|
+
for (size_t index = 0u;
|
|
1983
|
+
result.status == OSEO_STATUS_NORMAL && index < value->length;
|
|
1984
|
+
index += 1u) {
|
|
1985
|
+
uint16_t unit = value->units[index];
|
|
1986
|
+
result = unit == (uint16_t)'"'
|
|
1987
|
+
? string_builder_append_ascii(context, &builder, """)
|
|
1988
|
+
: string_builder_append_unit(context, &builder, unit);
|
|
1989
|
+
}
|
|
1990
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1991
|
+
result = string_builder_append_ascii(context, &builder, "\"");
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1995
|
+
result = string_builder_append_ascii(context, &builder, ">");
|
|
1996
|
+
}
|
|
1997
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1998
|
+
const OseoString *subject = string_object(slots[0]);
|
|
1999
|
+
result = oseo_internal_string_builder_append(
|
|
2000
|
+
context,
|
|
2001
|
+
&builder,
|
|
2002
|
+
subject->units,
|
|
2003
|
+
subject->length
|
|
2004
|
+
);
|
|
2005
|
+
}
|
|
2006
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2007
|
+
result = string_builder_append_ascii(context, &builder, "</");
|
|
2008
|
+
}
|
|
2009
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2010
|
+
result = string_builder_append_ascii(context, &builder, method->tag);
|
|
2011
|
+
}
|
|
2012
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2013
|
+
result = string_builder_append_ascii(context, &builder, ">");
|
|
2014
|
+
}
|
|
2015
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2016
|
+
result = oseo_internal_allocate_string(
|
|
2017
|
+
context,
|
|
2018
|
+
builder.units,
|
|
2019
|
+
builder.length
|
|
2020
|
+
);
|
|
2021
|
+
}
|
|
2022
|
+
oseo_internal_string_builder_release(&builder);
|
|
2023
|
+
oseo_roots_pop(context, &frame);
|
|
2024
|
+
return result;
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
static OseoResult string_from_char_code(
|
|
2028
|
+
OseoContext *context,
|
|
2029
|
+
size_t argument_count,
|
|
2030
|
+
const OseoValue *arguments
|
|
2031
|
+
) {
|
|
2032
|
+
OseoStringBuilder builder = {NULL, 0u, 0u};
|
|
2033
|
+
OseoResult result = normal(oseo_undefined());
|
|
2034
|
+
for (size_t index = 0u; index < argument_count; index += 1u) {
|
|
2035
|
+
result = oseo_internal_to_number(context, arguments[index]);
|
|
2036
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2037
|
+
result = string_builder_append_unit(
|
|
2038
|
+
context,
|
|
2039
|
+
&builder,
|
|
2040
|
+
string_to_uint16(number_value(result.value))
|
|
2041
|
+
);
|
|
2042
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
2043
|
+
break;
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2047
|
+
result = oseo_internal_allocate_string(
|
|
2048
|
+
context,
|
|
2049
|
+
builder.units,
|
|
2050
|
+
builder.length
|
|
2051
|
+
);
|
|
2052
|
+
}
|
|
2053
|
+
oseo_internal_string_builder_release(&builder);
|
|
2054
|
+
return result;
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
static OseoResult string_from_code_point(
|
|
2058
|
+
OseoContext *context,
|
|
2059
|
+
size_t argument_count,
|
|
2060
|
+
const OseoValue *arguments
|
|
2061
|
+
) {
|
|
2062
|
+
OseoStringBuilder builder = {NULL, 0u, 0u};
|
|
2063
|
+
OseoResult result = normal(oseo_undefined());
|
|
2064
|
+
for (size_t index = 0u; index < argument_count; index += 1u) {
|
|
2065
|
+
result = oseo_internal_to_number(context, arguments[index]);
|
|
2066
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2067
|
+
double next = number_value(result.value);
|
|
2068
|
+
if (!isfinite(next) || trunc(next) != next || next < 0.0 ||
|
|
2069
|
+
next > 1114111.0) {
|
|
2070
|
+
result = oseo_internal_throw_error(
|
|
2071
|
+
context,
|
|
2072
|
+
OSEO_ERROR_RANGE,
|
|
2073
|
+
"String.fromCodePoint received an invalid code point."
|
|
2074
|
+
);
|
|
2075
|
+
break;
|
|
2076
|
+
}
|
|
2077
|
+
uint32_t code_point = (uint32_t)next;
|
|
2078
|
+
if (code_point <= UINT32_C(0xffff)) {
|
|
2079
|
+
result = string_builder_append_unit(
|
|
2080
|
+
context,
|
|
2081
|
+
&builder,
|
|
2082
|
+
(uint16_t)code_point
|
|
2083
|
+
);
|
|
2084
|
+
} else {
|
|
2085
|
+
uint32_t rest = code_point - UINT32_C(0x10000);
|
|
2086
|
+
uint16_t pair[] = {
|
|
2087
|
+
(uint16_t)(UINT32_C(0xd800) + (rest >> 10u)),
|
|
2088
|
+
(uint16_t)(UINT32_C(0xdc00) + (rest & UINT32_C(0x3ff))),
|
|
2089
|
+
};
|
|
2090
|
+
result = oseo_internal_string_builder_append(
|
|
2091
|
+
context,
|
|
2092
|
+
&builder,
|
|
2093
|
+
pair,
|
|
2094
|
+
2u
|
|
2095
|
+
);
|
|
2096
|
+
}
|
|
2097
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2098
|
+
}
|
|
2099
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2100
|
+
result = oseo_internal_allocate_string(
|
|
2101
|
+
context,
|
|
2102
|
+
builder.units,
|
|
2103
|
+
builder.length
|
|
2104
|
+
);
|
|
2105
|
+
}
|
|
2106
|
+
oseo_internal_string_builder_release(&builder);
|
|
2107
|
+
return result;
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
/* LengthOfArrayLike (7.3.18) reported as an exact non-negative double. */
|
|
2111
|
+
static OseoResult string_length_of_array_like(
|
|
2112
|
+
OseoContext *context,
|
|
2113
|
+
OseoValue object_value,
|
|
2114
|
+
double *length
|
|
2115
|
+
) {
|
|
2116
|
+
OseoValue slots[2] = {object_value, oseo_undefined()};
|
|
2117
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
2118
|
+
oseo_roots_push(context, &frame);
|
|
2119
|
+
OseoResult result = oseo_internal_ascii_string(context, "length");
|
|
2120
|
+
slots[1] = result.value;
|
|
2121
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2122
|
+
result = oseo_object_get(context, slots[0], slots[1]);
|
|
2123
|
+
}
|
|
2124
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2125
|
+
result = oseo_internal_to_number(context, result.value);
|
|
2126
|
+
}
|
|
2127
|
+
oseo_roots_pop(context, &frame);
|
|
2128
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2129
|
+
double value = number_value(result.value);
|
|
2130
|
+
if (isnan(value)) value = 0.0;
|
|
2131
|
+
else value = trunc(value);
|
|
2132
|
+
if (!(value > 0.0)) value = 0.0;
|
|
2133
|
+
else if (value > 9007199254740991.0) value = 9007199254740991.0;
|
|
2134
|
+
*length = value;
|
|
2135
|
+
return normal(oseo_number(value));
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
static OseoResult string_raw(
|
|
2139
|
+
OseoContext *context,
|
|
2140
|
+
size_t argument_count,
|
|
2141
|
+
const OseoValue *arguments
|
|
2142
|
+
) {
|
|
2143
|
+
size_t substitution_count = argument_count == 0u
|
|
2144
|
+
? 0u
|
|
2145
|
+
: argument_count - 1u;
|
|
2146
|
+
OseoStringBuilder builder = {NULL, 0u, 0u};
|
|
2147
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
2148
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
2149
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2150
|
+
result = oseo_internal_to_object(
|
|
2151
|
+
context,
|
|
2152
|
+
argument_count == 0u ? oseo_undefined() : arguments[0]
|
|
2153
|
+
);
|
|
2154
|
+
frame.slots[0] = result.value;
|
|
2155
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2156
|
+
result = oseo_internal_ascii_string(context, "raw");
|
|
2157
|
+
frame.slots[1] = result.value;
|
|
2158
|
+
}
|
|
2159
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2160
|
+
result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
|
|
2161
|
+
frame.slots[1] = result.value;
|
|
2162
|
+
}
|
|
2163
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2164
|
+
result = oseo_internal_to_object(context, frame.slots[1]);
|
|
2165
|
+
frame.slots[1] = result.value;
|
|
2166
|
+
}
|
|
2167
|
+
double literal_count = 0.0;
|
|
2168
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2169
|
+
result = string_length_of_array_like(
|
|
2170
|
+
context,
|
|
2171
|
+
frame.slots[1],
|
|
2172
|
+
&literal_count
|
|
2173
|
+
);
|
|
2174
|
+
}
|
|
2175
|
+
for (double next = 0.0;
|
|
2176
|
+
result.status == OSEO_STATUS_NORMAL && next < literal_count;
|
|
2177
|
+
next += 1.0) {
|
|
2178
|
+
char key_text[32];
|
|
2179
|
+
(void)snprintf(key_text, sizeof(key_text), "%.0f", next);
|
|
2180
|
+
result = oseo_internal_ascii_string(context, key_text);
|
|
2181
|
+
frame.slots[2] = result.value;
|
|
2182
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2183
|
+
result = oseo_object_get(context, frame.slots[1], frame.slots[2]);
|
|
2184
|
+
frame.slots[2] = result.value;
|
|
2185
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2186
|
+
result = oseo_internal_value_string(context, frame.slots[2]);
|
|
2187
|
+
frame.slots[2] = result.value;
|
|
2188
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2189
|
+
result = oseo_internal_string_builder_append(
|
|
2190
|
+
context,
|
|
2191
|
+
&builder,
|
|
2192
|
+
string_object(frame.slots[2])->units,
|
|
2193
|
+
string_object(frame.slots[2])->length
|
|
2194
|
+
);
|
|
2195
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
2196
|
+
break;
|
|
2197
|
+
}
|
|
2198
|
+
if (next + 1.0 >= literal_count) break;
|
|
2199
|
+
if (next >= (double)substitution_count) continue;
|
|
2200
|
+
result = oseo_internal_value_string(
|
|
2201
|
+
context,
|
|
2202
|
+
arguments[(size_t)next + 1u]
|
|
2203
|
+
);
|
|
2204
|
+
frame.slots[2] = result.value;
|
|
2205
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2206
|
+
result = oseo_internal_string_builder_append(
|
|
2207
|
+
context,
|
|
2208
|
+
&builder,
|
|
2209
|
+
string_object(frame.slots[2])->units,
|
|
2210
|
+
string_object(frame.slots[2])->length
|
|
2211
|
+
);
|
|
2212
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
2213
|
+
break;
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2217
|
+
result = oseo_internal_allocate_string(
|
|
2218
|
+
context,
|
|
2219
|
+
builder.units,
|
|
2220
|
+
builder.length
|
|
2221
|
+
);
|
|
2222
|
+
}
|
|
2223
|
+
oseo_internal_string_builder_release(&builder);
|
|
2224
|
+
oseo_roots_release(context, &frame);
|
|
2225
|
+
return result;
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
/*
|
|
2229
|
+
* The String constructor (22.1.1.1). A call with a Symbol argument
|
|
2230
|
+
* renders SymbolDescriptiveString instead of throwing, which is the one
|
|
2231
|
+
* conversion difference between calling and constructing. Construction
|
|
2232
|
+
* runs StringCreate, which takes its prototype from
|
|
2233
|
+
* GetPrototypeFromConstructor(NewTarget, "%String.prototype%") after
|
|
2234
|
+
* the argument's ToString, then brands the object it creates with
|
|
2235
|
+
* [[StringData]] and gives it the exotic own properties. A new target
|
|
2236
|
+
* whose `prototype` is an accessor therefore observes that order, and
|
|
2237
|
+
* an abrupt getter reports after the conversion. The read is a real Get
|
|
2238
|
+
* because `Reflect.construct` can name a bound function, which owns no
|
|
2239
|
+
* synthetic `prototype` slot.
|
|
2240
|
+
*/
|
|
2241
|
+
static OseoResult string_construct(
|
|
2242
|
+
OseoContext *context,
|
|
2243
|
+
OseoValue new_target,
|
|
2244
|
+
size_t argument_count,
|
|
2245
|
+
const OseoValue *arguments,
|
|
2246
|
+
bool constructing
|
|
2247
|
+
) {
|
|
2248
|
+
/* Slots: 0 the new target, 1 the converted string, 2 the wrapper.
|
|
2249
|
+
* The conversion is a safepoint and can run arbitrary code, so the
|
|
2250
|
+
* new target is rooted across it. */
|
|
2251
|
+
OseoValue slots[3] = {new_target, oseo_undefined(), oseo_undefined()};
|
|
2252
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
2253
|
+
oseo_roots_push(context, &frame);
|
|
2254
|
+
bool descriptive =
|
|
2255
|
+
argument_count > 0u && !constructing && is_symbol(arguments[0]);
|
|
2256
|
+
OseoResult result;
|
|
2257
|
+
if (descriptive) {
|
|
2258
|
+
result = oseo_internal_symbol_text(context, arguments[0]);
|
|
2259
|
+
} else if (argument_count == 0u) {
|
|
2260
|
+
result = oseo_internal_allocate_string(context, NULL, 0u);
|
|
2261
|
+
} else {
|
|
2262
|
+
result = oseo_internal_value_string(context, arguments[0]);
|
|
2263
|
+
}
|
|
2264
|
+
slots[1] = result.value;
|
|
2265
|
+
if (!descriptive && result.status == OSEO_STATUS_NORMAL && constructing) {
|
|
2266
|
+
result = oseo_internal_constructor_prototype(context, slots[0]);
|
|
2267
|
+
slots[2] = result.value;
|
|
2268
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[2])) {
|
|
2269
|
+
result = oseo_internal_constructor_realm_default(
|
|
2270
|
+
context,
|
|
2271
|
+
slots[0],
|
|
2272
|
+
OSEO_INTRINSIC_STRING_PROTOTYPE
|
|
2273
|
+
);
|
|
2274
|
+
slots[2] = result.value;
|
|
2275
|
+
}
|
|
2276
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2277
|
+
result = oseo_object_create(context, slots[2]);
|
|
2278
|
+
slots[2] = result.value;
|
|
2279
|
+
}
|
|
2280
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2281
|
+
OseoOrdinaryObject *wrapper = ordinary_object(slots[2]);
|
|
2282
|
+
wrapper->primitive_data = true;
|
|
2283
|
+
wrapper->primitive_value = slots[1];
|
|
2284
|
+
result = oseo_internal_string_wrapper_properties(
|
|
2285
|
+
context,
|
|
2286
|
+
slots[1],
|
|
2287
|
+
slots[2]
|
|
2288
|
+
);
|
|
2289
|
+
}
|
|
2290
|
+
if (result.status == OSEO_STATUS_NORMAL) result = normal(slots[2]);
|
|
2291
|
+
}
|
|
2292
|
+
oseo_roots_pop(context, &frame);
|
|
2293
|
+
return result;
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
OseoResult oseo_internal_string_builtin_dispatch(
|
|
2297
|
+
OseoContext *context,
|
|
2298
|
+
size_t code_id,
|
|
2299
|
+
OseoValue callee,
|
|
2300
|
+
OseoValue receiver,
|
|
2301
|
+
size_t argument_count,
|
|
2302
|
+
const OseoValue *arguments,
|
|
2303
|
+
OseoValue new_target
|
|
2304
|
+
) {
|
|
2305
|
+
(void)callee;
|
|
2306
|
+
if (code_id == OSEO_STRING_CONSTRUCTOR_CODE_ID) {
|
|
2307
|
+
return string_construct(
|
|
2308
|
+
context,
|
|
2309
|
+
new_target,
|
|
2310
|
+
argument_count,
|
|
2311
|
+
arguments,
|
|
2312
|
+
tag_of(new_target) != OSEO_TAG_UNDEFINED
|
|
2313
|
+
);
|
|
2314
|
+
}
|
|
2315
|
+
if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
|
|
2316
|
+
return oseo_internal_throw_error(
|
|
2317
|
+
context,
|
|
2318
|
+
OSEO_ERROR_TYPE,
|
|
2319
|
+
"String method is not a constructor."
|
|
2320
|
+
);
|
|
2321
|
+
}
|
|
2322
|
+
if (code_id == OSEO_STRING_AT_CODE_ID ||
|
|
2323
|
+
code_id == OSEO_STRING_CHAR_AT_CODE_ID ||
|
|
2324
|
+
code_id == OSEO_STRING_CHAR_CODE_AT_CODE_ID ||
|
|
2325
|
+
code_id == OSEO_STRING_CODE_POINT_AT_CODE_ID) {
|
|
2326
|
+
return string_at_index(
|
|
2327
|
+
context,
|
|
2328
|
+
code_id,
|
|
2329
|
+
receiver,
|
|
2330
|
+
argument_count,
|
|
2331
|
+
arguments
|
|
2332
|
+
);
|
|
2333
|
+
}
|
|
2334
|
+
if (code_id == OSEO_STRING_TO_STRING_CODE_ID ||
|
|
2335
|
+
code_id == OSEO_STRING_VALUE_OF_CODE_ID) {
|
|
2336
|
+
return string_this_value(context, receiver);
|
|
2337
|
+
}
|
|
2338
|
+
if (code_id == OSEO_STRING_CONCAT_CODE_ID) {
|
|
2339
|
+
return string_concat(context, receiver, argument_count, arguments);
|
|
2340
|
+
}
|
|
2341
|
+
if (code_id == OSEO_STRING_INDEX_OF_CODE_ID ||
|
|
2342
|
+
code_id == OSEO_STRING_LAST_INDEX_OF_CODE_ID ||
|
|
2343
|
+
code_id == OSEO_STRING_INCLUDES_CODE_ID ||
|
|
2344
|
+
code_id == OSEO_STRING_STARTS_WITH_CODE_ID ||
|
|
2345
|
+
code_id == OSEO_STRING_ENDS_WITH_CODE_ID) {
|
|
2346
|
+
return string_search(
|
|
2347
|
+
context,
|
|
2348
|
+
code_id,
|
|
2349
|
+
receiver,
|
|
2350
|
+
argument_count,
|
|
2351
|
+
arguments
|
|
2352
|
+
);
|
|
2353
|
+
}
|
|
2354
|
+
if (code_id == OSEO_STRING_SLICE_CODE_ID ||
|
|
2355
|
+
code_id == OSEO_STRING_SUBSTRING_CODE_ID) {
|
|
2356
|
+
return string_slice(
|
|
2357
|
+
context,
|
|
2358
|
+
code_id,
|
|
2359
|
+
receiver,
|
|
2360
|
+
argument_count,
|
|
2361
|
+
arguments
|
|
2362
|
+
);
|
|
2363
|
+
}
|
|
2364
|
+
if (code_id == OSEO_STRING_SUBSTR_CODE_ID) {
|
|
2365
|
+
return string_substr(context, receiver, argument_count, arguments);
|
|
2366
|
+
}
|
|
2367
|
+
if (code_id >= OSEO_STRING_HTML_CODE_ID_FIRST &&
|
|
2368
|
+
code_id <= OSEO_STRING_HTML_CODE_ID_LAST) {
|
|
2369
|
+
return string_create_html(
|
|
2370
|
+
context,
|
|
2371
|
+
&string_html_methods[code_id - OSEO_STRING_HTML_CODE_ID_FIRST],
|
|
2372
|
+
receiver,
|
|
2373
|
+
argument_count,
|
|
2374
|
+
arguments
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2377
|
+
if (code_id == OSEO_STRING_LOWERCASE_CODE_ID ||
|
|
2378
|
+
code_id == OSEO_STRING_UPPERCASE_CODE_ID) {
|
|
2379
|
+
return string_convert_case(
|
|
2380
|
+
context,
|
|
2381
|
+
receiver,
|
|
2382
|
+
code_id == OSEO_STRING_UPPERCASE_CODE_ID
|
|
2383
|
+
);
|
|
2384
|
+
}
|
|
2385
|
+
if (code_id == OSEO_STRING_TRIM_CODE_ID ||
|
|
2386
|
+
code_id == OSEO_STRING_TRIM_START_CODE_ID ||
|
|
2387
|
+
code_id == OSEO_STRING_TRIM_END_CODE_ID) {
|
|
2388
|
+
return string_trim(
|
|
2389
|
+
context,
|
|
2390
|
+
receiver,
|
|
2391
|
+
code_id != OSEO_STRING_TRIM_END_CODE_ID,
|
|
2392
|
+
code_id != OSEO_STRING_TRIM_START_CODE_ID
|
|
2393
|
+
);
|
|
2394
|
+
}
|
|
2395
|
+
if (code_id == OSEO_STRING_NORMALIZE_CODE_ID) {
|
|
2396
|
+
return string_normalize(
|
|
2397
|
+
context,
|
|
2398
|
+
receiver,
|
|
2399
|
+
argument_count,
|
|
2400
|
+
arguments
|
|
2401
|
+
);
|
|
2402
|
+
}
|
|
2403
|
+
if (code_id == OSEO_STRING_LOCALE_COMPARE_CODE_ID) {
|
|
2404
|
+
return string_locale_compare(
|
|
2405
|
+
context,
|
|
2406
|
+
receiver,
|
|
2407
|
+
argument_count,
|
|
2408
|
+
arguments
|
|
2409
|
+
);
|
|
2410
|
+
}
|
|
2411
|
+
if (code_id == OSEO_STRING_REPEAT_CODE_ID) {
|
|
2412
|
+
return string_repeat(context, receiver, argument_count, arguments);
|
|
2413
|
+
}
|
|
2414
|
+
if (code_id == OSEO_STRING_PAD_START_CODE_ID ||
|
|
2415
|
+
code_id == OSEO_STRING_PAD_END_CODE_ID) {
|
|
2416
|
+
return string_pad(
|
|
2417
|
+
context,
|
|
2418
|
+
receiver,
|
|
2419
|
+
argument_count,
|
|
2420
|
+
arguments,
|
|
2421
|
+
code_id == OSEO_STRING_PAD_START_CODE_ID
|
|
2422
|
+
);
|
|
2423
|
+
}
|
|
2424
|
+
if (code_id == OSEO_STRING_IS_WELL_FORMED_CODE_ID ||
|
|
2425
|
+
code_id == OSEO_STRING_TO_WELL_FORMED_CODE_ID) {
|
|
2426
|
+
return string_well_formed(
|
|
2427
|
+
context,
|
|
2428
|
+
receiver,
|
|
2429
|
+
code_id == OSEO_STRING_TO_WELL_FORMED_CODE_ID
|
|
2430
|
+
);
|
|
2431
|
+
}
|
|
2432
|
+
if (code_id == OSEO_STRING_MATCH_CODE_ID ||
|
|
2433
|
+
code_id == OSEO_STRING_MATCH_ALL_CODE_ID ||
|
|
2434
|
+
code_id == OSEO_STRING_SEARCH_CODE_ID ||
|
|
2435
|
+
code_id == OSEO_STRING_SPLIT_CODE_ID ||
|
|
2436
|
+
code_id == OSEO_STRING_REPLACE_CODE_ID ||
|
|
2437
|
+
code_id == OSEO_STRING_REPLACE_ALL_CODE_ID) {
|
|
2438
|
+
return oseo_internal_string_protocol_dispatch(
|
|
2439
|
+
context,
|
|
2440
|
+
code_id,
|
|
2441
|
+
receiver,
|
|
2442
|
+
argument_count,
|
|
2443
|
+
arguments
|
|
2444
|
+
);
|
|
2445
|
+
}
|
|
2446
|
+
if (code_id == OSEO_REGEXP_STRING_ITERATOR_NEXT_CODE_ID) {
|
|
2447
|
+
return oseo_internal_string_protocol_dispatch(
|
|
2448
|
+
context,
|
|
2449
|
+
code_id,
|
|
2450
|
+
receiver,
|
|
2451
|
+
argument_count,
|
|
2452
|
+
arguments
|
|
2453
|
+
);
|
|
2454
|
+
}
|
|
2455
|
+
if (code_id == OSEO_STRING_FROM_CHAR_CODE_CODE_ID) {
|
|
2456
|
+
return string_from_char_code(context, argument_count, arguments);
|
|
2457
|
+
}
|
|
2458
|
+
if (code_id == OSEO_STRING_FROM_CODE_POINT_CODE_ID) {
|
|
2459
|
+
return string_from_code_point(context, argument_count, arguments);
|
|
2460
|
+
}
|
|
2461
|
+
if (code_id == OSEO_STRING_RAW_CODE_ID) {
|
|
2462
|
+
return string_raw(context, argument_count, arguments);
|
|
2463
|
+
}
|
|
2464
|
+
if (code_id == OSEO_STRING_UNADMITTED_METHOD_CODE_ID) {
|
|
2465
|
+
return failure(
|
|
2466
|
+
context,
|
|
2467
|
+
"OSEO2001",
|
|
2468
|
+
"String prototype method is not admitted in this M5b node."
|
|
2469
|
+
);
|
|
2470
|
+
}
|
|
2471
|
+
return oseo_unknown_function(context, code_id);
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2474
|
+
static OseoResult create_string_function(
|
|
2475
|
+
OseoContext *context,
|
|
2476
|
+
size_t code_id,
|
|
2477
|
+
const char *name,
|
|
2478
|
+
size_t length,
|
|
2479
|
+
OseoFunctionKind kind
|
|
2480
|
+
) {
|
|
2481
|
+
size_t name_length = strlen(name);
|
|
2482
|
+
uint16_t units[31];
|
|
2483
|
+
if (name_length > sizeof(units) / sizeof(*units)) {
|
|
2484
|
+
return failure(context, "OSEO2001", "Built-in name is too long.");
|
|
2485
|
+
}
|
|
2486
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
2487
|
+
units[index] = (uint16_t)(unsigned char)name[index];
|
|
2488
|
+
}
|
|
2489
|
+
OseoValue environment = oseo_undefined();
|
|
2490
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
2491
|
+
oseo_roots_push(context, &frame);
|
|
2492
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
2493
|
+
environment = result.value;
|
|
2494
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2495
|
+
result = oseo_function_create(
|
|
2496
|
+
context,
|
|
2497
|
+
code_id,
|
|
2498
|
+
environment,
|
|
2499
|
+
units,
|
|
2500
|
+
name_length,
|
|
2501
|
+
length,
|
|
2502
|
+
kind,
|
|
2503
|
+
oseo_undefined(),
|
|
2504
|
+
oseo_undefined(),
|
|
2505
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
2506
|
+
);
|
|
2507
|
+
}
|
|
2508
|
+
oseo_roots_pop(context, &frame);
|
|
2509
|
+
return result;
|
|
2510
|
+
}
|
|
2511
|
+
|
|
2512
|
+
static OseoResult define_string_property(
|
|
2513
|
+
OseoContext *context,
|
|
2514
|
+
OseoValue object,
|
|
2515
|
+
const char *name,
|
|
2516
|
+
OseoValue value,
|
|
2517
|
+
OseoPropertyAttributes attributes
|
|
2518
|
+
) {
|
|
2519
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
2520
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
2521
|
+
oseo_roots_push(context, &frame);
|
|
2522
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
2523
|
+
slots[2] = result.value;
|
|
2524
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2525
|
+
result = oseo_object_define(
|
|
2526
|
+
context,
|
|
2527
|
+
slots[0],
|
|
2528
|
+
slots[2],
|
|
2529
|
+
slots[1],
|
|
2530
|
+
attributes
|
|
2531
|
+
);
|
|
2532
|
+
}
|
|
2533
|
+
oseo_roots_pop(context, &frame);
|
|
2534
|
+
return result;
|
|
2535
|
+
}
|
|
2536
|
+
|
|
2537
|
+
OseoResult oseo_internal_string_intrinsic(OseoContext *context) {
|
|
2538
|
+
OseoValue *marker = &context->realm->intrinsics[OSEO_INTRINSIC_STRING_RAW];
|
|
2539
|
+
if (tag_of(*marker) != OSEO_TAG_UNDEFINED) {
|
|
2540
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_STRING]);
|
|
2541
|
+
}
|
|
2542
|
+
size_t entry_allocations = context->allocations;
|
|
2543
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
2544
|
+
/*
|
|
2545
|
+
* Slots 3 and 4 keep trimStart and trimEnd so that the Annex B
|
|
2546
|
+
* trimLeft and trimRight properties can name the same functions.
|
|
2547
|
+
*/
|
|
2548
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 5u);
|
|
2549
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2550
|
+
result = oseo_internal_intrinsic(
|
|
2551
|
+
context,
|
|
2552
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
2553
|
+
);
|
|
2554
|
+
frame.slots[0] = result.value;
|
|
2555
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2556
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
2557
|
+
frame.slots[0] = result.value;
|
|
2558
|
+
}
|
|
2559
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2560
|
+
result = oseo_internal_allocate_string(context, NULL, 0u);
|
|
2561
|
+
frame.slots[1] = result.value;
|
|
2562
|
+
}
|
|
2563
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2564
|
+
/*
|
|
2565
|
+
* %String.prototype% is itself a String exotic object whose
|
|
2566
|
+
* [[StringData]] is the empty String, so it carries the same
|
|
2567
|
+
* brand and the same non-writable `length` an instance does.
|
|
2568
|
+
*/
|
|
2569
|
+
OseoOrdinaryObject *prototype = ordinary_object(frame.slots[0]);
|
|
2570
|
+
prototype->primitive_data = true;
|
|
2571
|
+
prototype->primitive_value = frame.slots[1];
|
|
2572
|
+
prototype->virtual_string_iterator = true;
|
|
2573
|
+
prototype->virtual_string_iterator_configurable = true;
|
|
2574
|
+
prototype->virtual_string_iterator_enumerable = false;
|
|
2575
|
+
prototype->virtual_string_iterator_writable = true;
|
|
2576
|
+
context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE] =
|
|
2577
|
+
frame.slots[0];
|
|
2578
|
+
/*
|
|
2579
|
+
* The marker slot is claimed before the remaining allocations so
|
|
2580
|
+
* a nested materialization cannot start a second construction;
|
|
2581
|
+
* the failure path below clears every slot again.
|
|
2582
|
+
*/
|
|
2583
|
+
*marker = frame.slots[0];
|
|
2584
|
+
result = oseo_internal_string_wrapper_properties(
|
|
2585
|
+
context,
|
|
2586
|
+
frame.slots[1],
|
|
2587
|
+
frame.slots[0]
|
|
2588
|
+
);
|
|
2589
|
+
}
|
|
2590
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2591
|
+
result = oseo_internal_install_primitive_wrapper_methods(
|
|
2592
|
+
context,
|
|
2593
|
+
frame.slots[0],
|
|
2594
|
+
true
|
|
2595
|
+
);
|
|
2596
|
+
}
|
|
2597
|
+
static const size_t access_codes[] = {
|
|
2598
|
+
OSEO_STRING_AT_CODE_ID,
|
|
2599
|
+
OSEO_STRING_CHAR_AT_CODE_ID,
|
|
2600
|
+
OSEO_STRING_CHAR_CODE_AT_CODE_ID,
|
|
2601
|
+
OSEO_STRING_CODE_POINT_AT_CODE_ID,
|
|
2602
|
+
OSEO_STRING_TO_STRING_CODE_ID,
|
|
2603
|
+
OSEO_STRING_VALUE_OF_CODE_ID,
|
|
2604
|
+
OSEO_STRING_CONCAT_CODE_ID,
|
|
2605
|
+
OSEO_STRING_INDEX_OF_CODE_ID,
|
|
2606
|
+
OSEO_STRING_LAST_INDEX_OF_CODE_ID,
|
|
2607
|
+
OSEO_STRING_INCLUDES_CODE_ID,
|
|
2608
|
+
OSEO_STRING_STARTS_WITH_CODE_ID,
|
|
2609
|
+
OSEO_STRING_ENDS_WITH_CODE_ID,
|
|
2610
|
+
OSEO_STRING_SLICE_CODE_ID,
|
|
2611
|
+
OSEO_STRING_SUBSTRING_CODE_ID,
|
|
2612
|
+
OSEO_STRING_MATCH_CODE_ID,
|
|
2613
|
+
OSEO_STRING_MATCH_ALL_CODE_ID,
|
|
2614
|
+
OSEO_STRING_SEARCH_CODE_ID,
|
|
2615
|
+
OSEO_STRING_SPLIT_CODE_ID,
|
|
2616
|
+
OSEO_STRING_REPLACE_CODE_ID,
|
|
2617
|
+
OSEO_STRING_REPLACE_ALL_CODE_ID,
|
|
2618
|
+
OSEO_STRING_LOCALE_COMPARE_CODE_ID,
|
|
2619
|
+
OSEO_STRING_LOWERCASE_CODE_ID,
|
|
2620
|
+
OSEO_STRING_UPPERCASE_CODE_ID,
|
|
2621
|
+
OSEO_STRING_LOWERCASE_CODE_ID,
|
|
2622
|
+
OSEO_STRING_UPPERCASE_CODE_ID,
|
|
2623
|
+
OSEO_STRING_TRIM_CODE_ID,
|
|
2624
|
+
OSEO_STRING_TRIM_START_CODE_ID,
|
|
2625
|
+
OSEO_STRING_TRIM_END_CODE_ID,
|
|
2626
|
+
OSEO_STRING_NORMALIZE_CODE_ID,
|
|
2627
|
+
OSEO_STRING_REPEAT_CODE_ID,
|
|
2628
|
+
OSEO_STRING_PAD_START_CODE_ID,
|
|
2629
|
+
OSEO_STRING_PAD_END_CODE_ID,
|
|
2630
|
+
OSEO_STRING_IS_WELL_FORMED_CODE_ID,
|
|
2631
|
+
OSEO_STRING_TO_WELL_FORMED_CODE_ID,
|
|
2632
|
+
OSEO_STRING_SUBSTR_CODE_ID,
|
|
2633
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 0u,
|
|
2634
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 1u,
|
|
2635
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 2u,
|
|
2636
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 3u,
|
|
2637
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 4u,
|
|
2638
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 5u,
|
|
2639
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 6u,
|
|
2640
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 7u,
|
|
2641
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 8u,
|
|
2642
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 9u,
|
|
2643
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 10u,
|
|
2644
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 11u,
|
|
2645
|
+
OSEO_STRING_HTML_CODE_ID_FIRST + 12u,
|
|
2646
|
+
};
|
|
2647
|
+
static const char *const access_names[] = {
|
|
2648
|
+
"at",
|
|
2649
|
+
"charAt",
|
|
2650
|
+
"charCodeAt",
|
|
2651
|
+
"codePointAt",
|
|
2652
|
+
"toString",
|
|
2653
|
+
"valueOf",
|
|
2654
|
+
"concat",
|
|
2655
|
+
"indexOf",
|
|
2656
|
+
"lastIndexOf",
|
|
2657
|
+
"includes",
|
|
2658
|
+
"startsWith",
|
|
2659
|
+
"endsWith",
|
|
2660
|
+
"slice",
|
|
2661
|
+
"substring",
|
|
2662
|
+
"match",
|
|
2663
|
+
"matchAll",
|
|
2664
|
+
"search",
|
|
2665
|
+
"split",
|
|
2666
|
+
"replace",
|
|
2667
|
+
"replaceAll",
|
|
2668
|
+
"localeCompare",
|
|
2669
|
+
"toLocaleLowerCase",
|
|
2670
|
+
"toLocaleUpperCase",
|
|
2671
|
+
"toLowerCase",
|
|
2672
|
+
"toUpperCase",
|
|
2673
|
+
"trim",
|
|
2674
|
+
"trimStart",
|
|
2675
|
+
"trimEnd",
|
|
2676
|
+
"normalize",
|
|
2677
|
+
"repeat",
|
|
2678
|
+
"padStart",
|
|
2679
|
+
"padEnd",
|
|
2680
|
+
"isWellFormed",
|
|
2681
|
+
"toWellFormed",
|
|
2682
|
+
"substr",
|
|
2683
|
+
"anchor",
|
|
2684
|
+
"big",
|
|
2685
|
+
"blink",
|
|
2686
|
+
"bold",
|
|
2687
|
+
"fixed",
|
|
2688
|
+
"fontcolor",
|
|
2689
|
+
"fontsize",
|
|
2690
|
+
"italics",
|
|
2691
|
+
"link",
|
|
2692
|
+
"small",
|
|
2693
|
+
"strike",
|
|
2694
|
+
"sub",
|
|
2695
|
+
"sup",
|
|
2696
|
+
};
|
|
2697
|
+
static const size_t access_lengths[] = {
|
|
2698
|
+
1u, 1u, 1u, 1u, 0u, 0u, 1u,
|
|
2699
|
+
1u, 1u, 1u, 1u, 1u, 2u, 2u,
|
|
2700
|
+
1u, 1u, 1u, 2u, 2u, 2u,
|
|
2701
|
+
1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
|
2702
|
+
1u, 1u, 1u, 0u, 0u,
|
|
2703
|
+
2u, 1u, 0u, 0u, 0u, 0u, 1u, 1u, 0u, 1u, 0u, 0u, 0u, 0u,
|
|
2704
|
+
};
|
|
2705
|
+
_Static_assert(
|
|
2706
|
+
sizeof(access_codes) / sizeof(access_codes[0]) ==
|
|
2707
|
+
sizeof(access_names) / sizeof(access_names[0]) &&
|
|
2708
|
+
sizeof(access_codes) / sizeof(access_codes[0]) ==
|
|
2709
|
+
sizeof(access_lengths) / sizeof(access_lengths[0]),
|
|
2710
|
+
"Every String.prototype method needs a name and a length."
|
|
2711
|
+
);
|
|
2712
|
+
for (size_t index = 0u;
|
|
2713
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
2714
|
+
index < sizeof(access_codes) / sizeof(access_codes[0]);
|
|
2715
|
+
index += 1u) {
|
|
2716
|
+
result = create_string_function(
|
|
2717
|
+
context,
|
|
2718
|
+
access_codes[index],
|
|
2719
|
+
access_names[index],
|
|
2720
|
+
access_lengths[index],
|
|
2721
|
+
OSEO_FUNCTION_INTERNAL
|
|
2722
|
+
);
|
|
2723
|
+
frame.slots[1] = result.value;
|
|
2724
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2725
|
+
result = define_string_property(
|
|
2726
|
+
context,
|
|
2727
|
+
frame.slots[0],
|
|
2728
|
+
access_names[index],
|
|
2729
|
+
frame.slots[1],
|
|
2730
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2731
|
+
);
|
|
2732
|
+
}
|
|
2733
|
+
if (access_codes[index] == OSEO_STRING_TRIM_START_CODE_ID) {
|
|
2734
|
+
frame.slots[3] = frame.slots[1];
|
|
2735
|
+
} else if (access_codes[index] == OSEO_STRING_TRIM_END_CODE_ID) {
|
|
2736
|
+
frame.slots[4] = frame.slots[1];
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
/*
|
|
2740
|
+
* B.2.2.15 and B.2.2.16: the initial values of trimLeft and trimRight
|
|
2741
|
+
* are the trimStart and trimEnd function objects themselves, so the
|
|
2742
|
+
* aliases keep those names.
|
|
2743
|
+
*/
|
|
2744
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2745
|
+
result = define_string_property(
|
|
2746
|
+
context,
|
|
2747
|
+
frame.slots[0],
|
|
2748
|
+
"trimLeft",
|
|
2749
|
+
frame.slots[3],
|
|
2750
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2751
|
+
);
|
|
2752
|
+
}
|
|
2753
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2754
|
+
result = define_string_property(
|
|
2755
|
+
context,
|
|
2756
|
+
frame.slots[0],
|
|
2757
|
+
"trimRight",
|
|
2758
|
+
frame.slots[4],
|
|
2759
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2760
|
+
);
|
|
2761
|
+
}
|
|
2762
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2763
|
+
result = oseo_internal_iterator_method(
|
|
2764
|
+
context,
|
|
2765
|
+
OSEO_STRING_PROTOTYPE_ITERATOR_CODE_ID
|
|
2766
|
+
);
|
|
2767
|
+
frame.slots[1] = result.value;
|
|
2768
|
+
}
|
|
2769
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2770
|
+
result = oseo_internal_well_known_symbol(
|
|
2771
|
+
context,
|
|
2772
|
+
OSEO_WELL_KNOWN_ITERATOR
|
|
2773
|
+
);
|
|
2774
|
+
frame.slots[2] = result.value;
|
|
2775
|
+
}
|
|
2776
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2777
|
+
result = oseo_object_define(
|
|
2778
|
+
context,
|
|
2779
|
+
frame.slots[0],
|
|
2780
|
+
frame.slots[2],
|
|
2781
|
+
frame.slots[1],
|
|
2782
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2783
|
+
);
|
|
2784
|
+
}
|
|
2785
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2786
|
+
result = create_string_function(
|
|
2787
|
+
context,
|
|
2788
|
+
OSEO_STRING_CONSTRUCTOR_CODE_ID,
|
|
2789
|
+
"String",
|
|
2790
|
+
1u,
|
|
2791
|
+
OSEO_FUNCTION_ORDINARY
|
|
2792
|
+
);
|
|
2793
|
+
frame.slots[1] = result.value;
|
|
2794
|
+
}
|
|
2795
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2796
|
+
context->realm->intrinsics[OSEO_INTRINSIC_STRING] = frame.slots[1];
|
|
2797
|
+
OseoFunction *constructor = function_object(frame.slots[1]);
|
|
2798
|
+
constructor->prototype_object = frame.slots[0];
|
|
2799
|
+
constructor->prototype_writable = false;
|
|
2800
|
+
result = define_string_property(
|
|
2801
|
+
context,
|
|
2802
|
+
frame.slots[0],
|
|
2803
|
+
"constructor",
|
|
2804
|
+
frame.slots[1],
|
|
2805
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2806
|
+
);
|
|
2807
|
+
}
|
|
2808
|
+
static const OseoIntrinsic static_intrinsics[] = {
|
|
2809
|
+
OSEO_INTRINSIC_STRING_FROM_CHAR_CODE,
|
|
2810
|
+
OSEO_INTRINSIC_STRING_FROM_CODE_POINT,
|
|
2811
|
+
OSEO_INTRINSIC_STRING_RAW,
|
|
2812
|
+
};
|
|
2813
|
+
static const size_t static_codes[] = {
|
|
2814
|
+
OSEO_STRING_FROM_CHAR_CODE_CODE_ID,
|
|
2815
|
+
OSEO_STRING_FROM_CODE_POINT_CODE_ID,
|
|
2816
|
+
OSEO_STRING_RAW_CODE_ID,
|
|
2817
|
+
};
|
|
2818
|
+
static const char *const static_names[] = {
|
|
2819
|
+
"fromCharCode",
|
|
2820
|
+
"fromCodePoint",
|
|
2821
|
+
"raw",
|
|
2822
|
+
};
|
|
2823
|
+
for (size_t index = 0u;
|
|
2824
|
+
result.status == OSEO_STATUS_NORMAL && index < 3u;
|
|
2825
|
+
index += 1u) {
|
|
2826
|
+
result = create_string_function(
|
|
2827
|
+
context,
|
|
2828
|
+
static_codes[index],
|
|
2829
|
+
static_names[index],
|
|
2830
|
+
1u,
|
|
2831
|
+
OSEO_FUNCTION_INTERNAL
|
|
2832
|
+
);
|
|
2833
|
+
frame.slots[2] = result.value;
|
|
2834
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2835
|
+
context->realm->intrinsics[static_intrinsics[index]] = frame.slots[2];
|
|
2836
|
+
result = define_string_property(
|
|
2837
|
+
context,
|
|
2838
|
+
frame.slots[1],
|
|
2839
|
+
static_names[index],
|
|
2840
|
+
frame.slots[2],
|
|
2841
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2842
|
+
);
|
|
2843
|
+
}
|
|
2844
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
2845
|
+
context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE] =
|
|
2846
|
+
oseo_undefined();
|
|
2847
|
+
for (size_t index = OSEO_INTRINSIC_STRING;
|
|
2848
|
+
index <= OSEO_INTRINSIC_STRING_RAW;
|
|
2849
|
+
index += 1u) {
|
|
2850
|
+
context->realm->intrinsics[index] = oseo_undefined();
|
|
2851
|
+
}
|
|
2852
|
+
context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE_ITERATOR] =
|
|
2853
|
+
oseo_undefined();
|
|
2854
|
+
} else if (context->observe_specialization) {
|
|
2855
|
+
context->allocations = entry_allocations;
|
|
2856
|
+
}
|
|
2857
|
+
oseo_roots_release(context, &frame);
|
|
2858
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
2859
|
+
? normal(context->realm->intrinsics[OSEO_INTRINSIC_STRING])
|
|
2860
|
+
: result;
|
|
2861
|
+
}
|
|
2862
|
+
|
|
2863
|
+
OseoResult oseo_internal_install_string_global(
|
|
2864
|
+
OseoContext *context,
|
|
2865
|
+
OseoValue global
|
|
2866
|
+
) {
|
|
2867
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
2868
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
2869
|
+
oseo_roots_push(context, &frame);
|
|
2870
|
+
OseoResult result = oseo_internal_string_intrinsic(context);
|
|
2871
|
+
slots[1] = result.value;
|
|
2872
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2873
|
+
result = define_string_property(
|
|
2874
|
+
context,
|
|
2875
|
+
slots[0],
|
|
2876
|
+
"String",
|
|
2877
|
+
slots[1],
|
|
2878
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2879
|
+
);
|
|
2880
|
+
}
|
|
2881
|
+
OseoValue global_value = slots[0];
|
|
2882
|
+
oseo_roots_pop(context, &frame);
|
|
2883
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
2884
|
+
? normal(global_value)
|
|
2885
|
+
: result;
|
|
2886
|
+
}
|