@ugo-studio/jspp 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/core/codegen/class-handlers.js +6 -6
  2. package/dist/core/codegen/statement-handlers.js +1 -1
  3. package/package.json +1 -1
  4. package/src/prelude/any_value.hpp +362 -362
  5. package/src/prelude/any_value_access.hpp +170 -170
  6. package/src/prelude/any_value_defines.hpp +189 -189
  7. package/src/prelude/any_value_helpers.hpp +374 -374
  8. package/src/prelude/library/array.hpp +185 -185
  9. package/src/prelude/library/console.hpp +111 -111
  10. package/src/prelude/library/error.hpp +112 -112
  11. package/src/prelude/library/function.hpp +10 -10
  12. package/src/prelude/library/math.hpp +307 -307
  13. package/src/prelude/library/object.hpp +275 -275
  14. package/src/prelude/library/process.hpp +39 -39
  15. package/src/prelude/library/promise.hpp +123 -123
  16. package/src/prelude/library/symbol.hpp +52 -52
  17. package/src/prelude/library/timer.hpp +91 -91
  18. package/src/prelude/types.hpp +178 -178
  19. package/src/prelude/utils/access.hpp +411 -411
  20. package/src/prelude/utils/operators.hpp +336 -336
  21. package/src/prelude/values/array.hpp +0 -1
  22. package/src/prelude/values/async_iterator.hpp +83 -83
  23. package/src/prelude/values/function.hpp +82 -82
  24. package/src/prelude/values/helpers/array.hpp +198 -208
  25. package/src/prelude/values/helpers/async_iterator.hpp +275 -275
  26. package/src/prelude/values/helpers/function.hpp +108 -108
  27. package/src/prelude/values/helpers/iterator.hpp +144 -144
  28. package/src/prelude/values/helpers/promise.hpp +253 -253
  29. package/src/prelude/values/helpers/string.hpp +37 -61
  30. package/src/prelude/values/promise.hpp +72 -72
  31. package/src/prelude/values/prototypes/array.hpp +14 -2
  32. package/src/prelude/values/prototypes/iterator.hpp +201 -201
  33. package/src/prelude/values/prototypes/promise.hpp +196 -196
  34. package/src/prelude/values/prototypes/string.hpp +564 -542
  35. package/src/prelude/values/string.hpp +25 -26
@@ -1,542 +1,564 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/string.hpp" // Make sure this is included
5
- #include "any_value.hpp"
6
- #include "utils/operators.hpp"
7
- #include <optional>
8
- #include <string>
9
- #include <vector>
10
- #include <algorithm>
11
- #include <cctype>
12
-
13
- namespace jspp
14
- {
15
- namespace StringPrototypes
16
- {
17
- inline AnyValue &get_toString_fn()
18
- {
19
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
20
- { return AnyValue::make_string(thisVal.as_string()->value); },
21
- "toString");
22
- return fn;
23
- }
24
-
25
- inline AnyValue &get_iterator_fn()
26
- {
27
- static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> _) -> AnyValue
28
- { return AnyValue::from_iterator(thisVal.as_string()->get_iterator()); },
29
- "Symbol.iterator");
30
- return fn;
31
- }
32
-
33
- inline AnyValue &get_length_desc()
34
- {
35
- static auto getter = [](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
36
- { return AnyValue::make_number(thisVal.as_string()->value.length()); };
37
- static auto setter = [](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
38
- { return Constants::UNDEFINED; };
39
- static AnyValue desc = AnyValue::make_accessor_descriptor(getter, setter, false, false);
40
- return desc;
41
- }
42
-
43
- inline AnyValue &get_charAt_fn()
44
- {
45
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
46
- {
47
- auto self = thisVal.as_string();
48
- double pos = args.empty() ? 0 : Operators_Private::ToNumber(args[0]);
49
- int index = static_cast<int>(pos);
50
- if (index < 0 || index >= self->value.length())
51
- {
52
- return AnyValue::make_string("");
53
- }
54
- return AnyValue::make_string(std::string(1, self->value[index])); },
55
- "charAt");
56
- return fn;
57
- }
58
-
59
- inline AnyValue &get_concat_fn()
60
- {
61
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
62
- {
63
- std::string result = thisVal.as_string()->value;
64
- for (const auto &arg : args)
65
- {
66
- result += arg.to_std_string();
67
- }
68
- return AnyValue::make_string(result); },
69
- "concat");
70
- return fn;
71
- }
72
-
73
- inline AnyValue &get_endsWith_fn()
74
- {
75
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
76
- {
77
- auto self = thisVal.as_string();
78
- if (args.empty())
79
- return Constants::FALSE;
80
- std::string search = args[0].to_std_string();
81
- size_t end_pos = (args.size() > 1 && !args[1].is_undefined()) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : self->value.length();
82
-
83
- if (end_pos > self->value.length())
84
- end_pos = self->value.length();
85
- if (search.length() > end_pos)
86
- return Constants::FALSE;
87
-
88
- return AnyValue::make_boolean(self->value.substr(end_pos - search.length(), search.length()) == search); },
89
- "endsWith");
90
- return fn;
91
- }
92
-
93
- inline AnyValue &get_includes_fn()
94
- {
95
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
96
- {
97
- auto self = thisVal.as_string();
98
- if (args.empty())
99
- return Constants::FALSE;
100
- std::string search = args[0].to_std_string();
101
- size_t pos = (args.size() > 1) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : 0;
102
-
103
- return AnyValue::make_boolean(self->value.find(search, pos) != std::string::npos); },
104
- "includes");
105
- return fn;
106
- }
107
-
108
- inline AnyValue &get_indexOf_fn()
109
- {
110
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
111
- {
112
- auto self = thisVal.as_string();
113
- if (args.empty())
114
- return AnyValue::make_number(-1);
115
- std::string search = args[0].to_std_string();
116
- size_t pos = (args.size() > 1) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : 0;
117
- size_t result = self->value.find(search, pos);
118
- return result == std::string::npos ? AnyValue::make_number(-1) : AnyValue::make_number(result); },
119
- "indexOf");
120
- return fn;
121
- }
122
-
123
- inline AnyValue &get_lastIndexOf_fn()
124
- {
125
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
126
- {
127
- auto self = thisVal.as_string();
128
- if (args.empty())
129
- return AnyValue::make_number(-1);
130
- std::string search = args[0].to_std_string();
131
- size_t pos = (args.size() > 1 && !args[1].is_undefined()) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : std::string::npos;
132
- size_t result = self->value.rfind(search, pos);
133
- return result == std::string::npos ? AnyValue::make_number(-1) : AnyValue::make_number(result); },
134
- "lastIndexOf");
135
- return fn;
136
- }
137
-
138
- inline AnyValue &get_padEnd_fn()
139
- {
140
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
141
- {
142
- auto self = thisVal.as_string();
143
- size_t target_length = args.empty() ? 0 : static_cast<size_t>(Operators_Private::ToNumber(args[0]));
144
- if (self->value.length() >= target_length)
145
- return AnyValue::make_string(self->value);
146
- std::string pad_string = (args.size() > 1 && !args[1].is_undefined() && !args[1].to_std_string().empty()) ? args[1].to_std_string() : " ";
147
- std::string result = self->value;
148
- while (result.length() < target_length)
149
- {
150
- result += pad_string;
151
- }
152
- return AnyValue::make_string(result.substr(0, target_length)); },
153
- "padEnd");
154
- return fn;
155
- }
156
-
157
- inline AnyValue &get_padStart_fn()
158
- {
159
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
160
- {
161
- auto self = thisVal.as_string();
162
- size_t target_length = args.empty() ? 0 : static_cast<size_t>(Operators_Private::ToNumber(args[0]));
163
- if (self->value.length() >= target_length)
164
- return AnyValue::make_string(self->value);
165
- std::string pad_string = (args.size() > 1 && !args[1].is_undefined() && !args[1].to_std_string().empty()) ? args[1].to_std_string() : " ";
166
- std::string padding;
167
- while (padding.length() < target_length - self->value.length())
168
- {
169
- padding += pad_string;
170
- }
171
- return AnyValue::make_string(padding.substr(0, target_length - self->value.length()) + self->value); },
172
- "padStart");
173
- return fn;
174
- }
175
-
176
- inline AnyValue &get_repeat_fn()
177
- {
178
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
179
- {
180
- auto self = thisVal.as_string();
181
- double count = args.empty() ? 0 : Operators_Private::ToNumber(args[0]);
182
- if (count < 0)
183
- {
184
- // In a real implementation, this should throw a RangeError.
185
- return AnyValue::make_string("");
186
- }
187
- std::string result = "";
188
- for (int i = 0; i < count; ++i)
189
- {
190
- result += self->value;
191
- }
192
- return AnyValue::make_string(result); },
193
- "repeat");
194
- return fn;
195
- }
196
-
197
- inline AnyValue &get_replace_fn()
198
- {
199
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
200
- {
201
- auto self = thisVal.as_string();
202
- if (args.size() < 2)
203
- return AnyValue::make_string(self->value);
204
- std::string search = args[0].to_std_string();
205
- std::string replacement = args[1].to_std_string();
206
- std::string result = self->value;
207
- size_t pos = result.find(search);
208
- if (pos != std::string::npos)
209
- {
210
- result.replace(pos, search.length(), replacement);
211
- }
212
- return AnyValue::make_string(result); },
213
- "replace");
214
- return fn;
215
- }
216
-
217
- inline AnyValue &get_replaceAll_fn()
218
- {
219
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
220
- {
221
- auto self = thisVal.as_string();
222
- if (args.size() < 2)
223
- return AnyValue::make_string(self->value);
224
- std::string search = args[0].to_std_string();
225
- if (search.empty())
226
- return AnyValue::make_string(self->value);
227
- std::string replacement = args[1].to_std_string();
228
- std::string result = self->value;
229
- size_t pos = result.find(search);
230
- while (pos != std::string::npos)
231
- {
232
- result.replace(pos, search.length(), replacement);
233
- pos = result.find(search, pos + replacement.length());
234
- }
235
- return AnyValue::make_string(result); },
236
- "replaceAll");
237
- return fn;
238
- }
239
-
240
- inline AnyValue &get_slice_fn()
241
- {
242
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
243
- {
244
- auto self = thisVal.as_string();
245
- int len = self->value.length();
246
- int start = args.empty() ? 0 : Operators_Private::ToInt32(args[0]);
247
- int end = (args.size() < 2 || args[1].is_undefined()) ? len : Operators_Private::ToInt32(args[1]);
248
-
249
- if (start < 0)
250
- start += len;
251
- if (end < 0)
252
- end += len;
253
-
254
- start = std::max(0, std::min(len, start));
255
- end = std::max(0, std::min(len, end));
256
-
257
- if (start >= end)
258
- return AnyValue::make_string("");
259
- return AnyValue::make_string(self->value.substr(start, end - start)); },
260
- "slice");
261
- return fn;
262
- }
263
-
264
- inline AnyValue &get_split_fn()
265
- {
266
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
267
- {
268
- auto self = thisVal.as_string();
269
- std::string separator = (args.empty() || args[0].is_undefined()) ? "" : args[0].to_std_string();
270
- std::vector<jspp::AnyValue> result_vec;
271
-
272
- if (separator.empty())
273
- {
274
- for (char c : (self->value))
275
- {
276
- result_vec.push_back(AnyValue::make_string(std::string(1, c)));
277
- }
278
- }
279
- else
280
- {
281
- std::string temp = (self->value);
282
- size_t pos = 0;
283
- while ((pos = temp.find(separator)) != std::string::npos)
284
- {
285
- result_vec.push_back(AnyValue::make_string(temp.substr(0, pos)));
286
- temp.erase(0, pos + separator.length());
287
- }
288
- result_vec.push_back(AnyValue::make_string(temp));
289
- }
290
- return AnyValue::make_array(std::move(result_vec)); },
291
- "split");
292
- return fn;
293
- }
294
-
295
- inline AnyValue &get_startsWith_fn()
296
- {
297
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
298
- {
299
- auto self = thisVal.as_string();
300
- if (args.empty())
301
- return Constants::FALSE;
302
- std::string search = args[0].to_std_string();
303
- size_t pos = (args.size() > 1) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : 0;
304
- if (pos > self->value.length())
305
- pos = self->value.length();
306
-
307
- return AnyValue::make_boolean(self->value.rfind(search, pos) == pos); },
308
- "startsWith");
309
- return fn;
310
- }
311
-
312
- inline AnyValue &get_substring_fn()
313
- {
314
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
315
- {
316
- auto self = thisVal.as_string();
317
- int len = self->value.length();
318
- int start = args.empty() ? 0 : Operators_Private::ToInt32(args[0]);
319
- int end = (args.size() < 2 || args[1].is_undefined()) ? len : Operators_Private::ToInt32(args[1]);
320
-
321
- start = std::max(0, start);
322
- end = std::max(0, end);
323
-
324
- if (start > end)
325
- std::swap(start, end);
326
-
327
- start = std::min(len, start);
328
- end = std::min(len, end);
329
-
330
- return AnyValue::make_string(self->value.substr(start, end - start)); },
331
- "substring");
332
- return fn;
333
- }
334
-
335
- inline AnyValue &get_toLowerCase_fn()
336
- {
337
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
338
- {
339
- std::string result = thisVal.as_string()->value;
340
- std::transform(result.begin(), result.end(), result.begin(),
341
- [](unsigned char c)
342
- { return std::tolower(c); });
343
- return AnyValue::make_string(result); },
344
- "toLowerCase");
345
- return fn;
346
- }
347
-
348
- inline AnyValue &get_toUpperCase_fn()
349
- {
350
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
351
- {
352
- std::string result = thisVal.as_string()->value;
353
- std::transform(result.begin(), result.end(), result.begin(),
354
- [](unsigned char c)
355
- { return std::toupper(c); });
356
- return AnyValue::make_string(result); },
357
- "toUpperCase");
358
- return fn;
359
- }
360
-
361
- inline AnyValue &get_trim_fn()
362
- {
363
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
364
- {
365
- const char *whitespace = " \t\n\r\f\v";
366
- std::string result = thisVal.as_string()->value;
367
- result.erase(0, result.find_first_not_of(whitespace));
368
- result.erase(result.find_last_not_of(whitespace) + 1);
369
- return AnyValue::make_string(result); },
370
- "trim");
371
- return fn;
372
- }
373
-
374
- inline AnyValue &get_trimEnd_fn()
375
- {
376
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
377
- {
378
- const char *whitespace = " \t\n\r\f\v";
379
- std::string result = thisVal.as_string()->value;
380
- result.erase(result.find_last_not_of(whitespace) + 1);
381
- return AnyValue::make_string(result); },
382
- "trimEnd");
383
- return fn;
384
- }
385
-
386
- inline AnyValue &get_trimStart_fn()
387
- {
388
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
389
- {
390
- const char *whitespace = " \t\n\r\f\v";
391
- std::string result = thisVal.as_string()->value;
392
- result.erase(0, result.find_first_not_of(whitespace));
393
- return AnyValue::make_string(result); },
394
- "trimStart");
395
- return fn;
396
- }
397
-
398
- // This function retrieves a prototype method for a given string instance.
399
- inline std::optional<AnyValue> get(const std::string &key)
400
- {
401
- // --- toString() & valueOf() ---
402
- if (key == "toString" || key == "valueOf" || key == WellKnownSymbols::toStringTag->key)
403
- {
404
- return get_toString_fn();
405
- }
406
-
407
- // --- [Symbol.iterator]() method ---
408
- if (key == WellKnownSymbols::iterator->key)
409
- {
410
- return get_iterator_fn();
411
- }
412
-
413
- // --- length property ---
414
- if (key == "length")
415
- {
416
- return get_length_desc();
417
- }
418
-
419
- // --- charAt(pos) ---
420
- if (key == "charAt")
421
- {
422
- return get_charAt_fn();
423
- }
424
-
425
- // --- concat(str1, str2, ...) ---
426
- if (key == "concat")
427
- {
428
- return get_concat_fn();
429
- }
430
-
431
- // --- endsWith(searchString, endPosition) ---
432
- if (key == "endsWith")
433
- {
434
- return get_endsWith_fn();
435
- }
436
-
437
- // --- includes(searchString, position) ---
438
- if (key == "includes")
439
- {
440
- return get_includes_fn();
441
- }
442
-
443
- // --- indexOf(searchString, position) ---
444
- if (key == "indexOf")
445
- {
446
- return get_indexOf_fn();
447
- }
448
-
449
- // --- lastIndexOf(searchString, position) ---
450
- if (key == "lastIndexOf")
451
- {
452
- return get_lastIndexOf_fn();
453
- }
454
-
455
- // --- padEnd(targetLength, padString) ---
456
- if (key == "padEnd")
457
- {
458
- return get_padEnd_fn();
459
- }
460
-
461
- // --- padStart(targetLength, padString) ---
462
- if (key == "padStart")
463
- {
464
- return get_padStart_fn();
465
- }
466
-
467
- // --- repeat(count) ---
468
- if (key == "repeat")
469
- {
470
- return get_repeat_fn();
471
- }
472
-
473
- // --- replace(substr, newSubstr) ---
474
- if (key == "replace")
475
- {
476
- return get_replace_fn();
477
- }
478
-
479
- // --- replaceAll(substr, newSubstr) ---
480
- if (key == "replaceAll")
481
- {
482
- return get_replaceAll_fn();
483
- }
484
-
485
- // --- slice(beginIndex, endIndex) ---
486
- if (key == "slice")
487
- {
488
- return get_slice_fn();
489
- }
490
-
491
- // --- split(separator) ---
492
- if (key == "split")
493
- {
494
- return get_split_fn();
495
- }
496
-
497
- // --- startsWith(searchString, position) ---
498
- if (key == "startsWith")
499
- {
500
- return get_startsWith_fn();
501
- }
502
-
503
- // --- substring(indexStart, indexEnd) ---
504
- if (key == "substring")
505
- {
506
- return get_substring_fn();
507
- }
508
-
509
- // --- toLowerCase() ---
510
- if (key == "toLowerCase")
511
- {
512
- return get_toLowerCase_fn();
513
- }
514
-
515
- // --- toUpperCase() ---
516
- if (key == "toUpperCase")
517
- {
518
- return get_toUpperCase_fn();
519
- }
520
-
521
- // --- trim() ---
522
- if (key == "trim")
523
- {
524
- return get_trim_fn();
525
- }
526
-
527
- // --- trimEnd() ---
528
- if (key == "trimEnd")
529
- {
530
- return get_trimEnd_fn();
531
- }
532
-
533
- // --- trimStart() ---
534
- if (key == "trimStart")
535
- {
536
- return get_trimStart_fn();
537
- }
538
-
539
- return std::nullopt;
540
- }
541
- }
542
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "values/string.hpp" // Make sure this is included
5
+ #include "any_value.hpp"
6
+ #include "utils/operators.hpp"
7
+ #include <optional>
8
+ #include <string>
9
+ #include <vector>
10
+ #include <algorithm>
11
+ #include <cctype>
12
+
13
+ namespace jspp
14
+ {
15
+ namespace StringPrototypes
16
+ {
17
+ inline AnyValue &get_toString_fn()
18
+ {
19
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
20
+ { return AnyValue::make_string(thisVal.as_string()->value); },
21
+ "toString");
22
+ return fn;
23
+ }
24
+
25
+ inline AnyValue &get_iterator_fn()
26
+ {
27
+ static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
28
+ {
29
+ auto self = thisVal.as_string();
30
+ const std::string &value = self->value;
31
+ for (size_t i = 0; i < value.length();)
32
+ {
33
+ unsigned char c = static_cast<unsigned char>(value[i]);
34
+ size_t len = 1;
35
+ if ((c & 0x80) == 0)
36
+ len = 1;
37
+ else if ((c & 0xE0) == 0xC0)
38
+ len = 2;
39
+ else if ((c & 0xF0) == 0xE0)
40
+ len = 3;
41
+ else if ((c & 0xF8) == 0xF0)
42
+ len = 4;
43
+
44
+ if (i + len > value.length())
45
+ len = value.length() - i;
46
+
47
+ co_yield AnyValue::make_string(value.substr(i, len));
48
+ i += len;
49
+ }
50
+ co_return AnyValue::make_undefined(); },
51
+ "Symbol.iterator");
52
+ return fn;
53
+ }
54
+
55
+ inline AnyValue &get_length_desc()
56
+ {
57
+ static auto getter = [](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
58
+ { return AnyValue::make_number(thisVal.as_string()->value.length()); };
59
+ static auto setter = [](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
60
+ { return Constants::UNDEFINED; };
61
+ static AnyValue desc = AnyValue::make_accessor_descriptor(getter, setter, false, false);
62
+ return desc;
63
+ }
64
+
65
+ inline AnyValue &get_charAt_fn()
66
+ {
67
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
68
+ {
69
+ auto self = thisVal.as_string();
70
+ double pos = args.empty() ? 0 : Operators_Private::ToNumber(args[0]);
71
+ int index = static_cast<int>(pos);
72
+ if (index < 0 || index >= self->value.length())
73
+ {
74
+ return AnyValue::make_string("");
75
+ }
76
+ return AnyValue::make_string(std::string(1, self->value[index])); },
77
+ "charAt");
78
+ return fn;
79
+ }
80
+
81
+ inline AnyValue &get_concat_fn()
82
+ {
83
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
84
+ {
85
+ std::string result = thisVal.as_string()->value;
86
+ for (const auto &arg : args)
87
+ {
88
+ result += arg.to_std_string();
89
+ }
90
+ return AnyValue::make_string(result); },
91
+ "concat");
92
+ return fn;
93
+ }
94
+
95
+ inline AnyValue &get_endsWith_fn()
96
+ {
97
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
98
+ {
99
+ auto self = thisVal.as_string();
100
+ if (args.empty())
101
+ return Constants::FALSE;
102
+ std::string search = args[0].to_std_string();
103
+ size_t end_pos = (args.size() > 1 && !args[1].is_undefined()) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : self->value.length();
104
+
105
+ if (end_pos > self->value.length())
106
+ end_pos = self->value.length();
107
+ if (search.length() > end_pos)
108
+ return Constants::FALSE;
109
+
110
+ return AnyValue::make_boolean(self->value.substr(end_pos - search.length(), search.length()) == search); },
111
+ "endsWith");
112
+ return fn;
113
+ }
114
+
115
+ inline AnyValue &get_includes_fn()
116
+ {
117
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
118
+ {
119
+ auto self = thisVal.as_string();
120
+ if (args.empty())
121
+ return Constants::FALSE;
122
+ std::string search = args[0].to_std_string();
123
+ size_t pos = (args.size() > 1) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : 0;
124
+
125
+ return AnyValue::make_boolean(self->value.find(search, pos) != std::string::npos); },
126
+ "includes");
127
+ return fn;
128
+ }
129
+
130
+ inline AnyValue &get_indexOf_fn()
131
+ {
132
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
133
+ {
134
+ auto self = thisVal.as_string();
135
+ if (args.empty())
136
+ return AnyValue::make_number(-1);
137
+ std::string search = args[0].to_std_string();
138
+ size_t pos = (args.size() > 1) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : 0;
139
+ size_t result = self->value.find(search, pos);
140
+ return result == std::string::npos ? AnyValue::make_number(-1) : AnyValue::make_number(result); },
141
+ "indexOf");
142
+ return fn;
143
+ }
144
+
145
+ inline AnyValue &get_lastIndexOf_fn()
146
+ {
147
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
148
+ {
149
+ auto self = thisVal.as_string();
150
+ if (args.empty())
151
+ return AnyValue::make_number(-1);
152
+ std::string search = args[0].to_std_string();
153
+ size_t pos = (args.size() > 1 && !args[1].is_undefined()) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : std::string::npos;
154
+ size_t result = self->value.rfind(search, pos);
155
+ return result == std::string::npos ? AnyValue::make_number(-1) : AnyValue::make_number(result); },
156
+ "lastIndexOf");
157
+ return fn;
158
+ }
159
+
160
+ inline AnyValue &get_padEnd_fn()
161
+ {
162
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
163
+ {
164
+ auto self = thisVal.as_string();
165
+ size_t target_length = args.empty() ? 0 : static_cast<size_t>(Operators_Private::ToNumber(args[0]));
166
+ if (self->value.length() >= target_length)
167
+ return AnyValue::make_string(self->value);
168
+ std::string pad_string = (args.size() > 1 && !args[1].is_undefined() && !args[1].to_std_string().empty()) ? args[1].to_std_string() : " ";
169
+ std::string result = self->value;
170
+ while (result.length() < target_length)
171
+ {
172
+ result += pad_string;
173
+ }
174
+ return AnyValue::make_string(result.substr(0, target_length)); },
175
+ "padEnd");
176
+ return fn;
177
+ }
178
+
179
+ inline AnyValue &get_padStart_fn()
180
+ {
181
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
182
+ {
183
+ auto self = thisVal.as_string();
184
+ size_t target_length = args.empty() ? 0 : static_cast<size_t>(Operators_Private::ToNumber(args[0]));
185
+ if (self->value.length() >= target_length)
186
+ return AnyValue::make_string(self->value);
187
+ std::string pad_string = (args.size() > 1 && !args[1].is_undefined() && !args[1].to_std_string().empty()) ? args[1].to_std_string() : " ";
188
+ std::string padding;
189
+ while (padding.length() < target_length - self->value.length())
190
+ {
191
+ padding += pad_string;
192
+ }
193
+ return AnyValue::make_string(padding.substr(0, target_length - self->value.length()) + self->value); },
194
+ "padStart");
195
+ return fn;
196
+ }
197
+
198
+ inline AnyValue &get_repeat_fn()
199
+ {
200
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
201
+ {
202
+ auto self = thisVal.as_string();
203
+ double count = args.empty() ? 0 : Operators_Private::ToNumber(args[0]);
204
+ if (count < 0)
205
+ {
206
+ // In a real implementation, this should throw a RangeError.
207
+ return AnyValue::make_string("");
208
+ }
209
+ std::string result = "";
210
+ for (int i = 0; i < count; ++i)
211
+ {
212
+ result += self->value;
213
+ }
214
+ return AnyValue::make_string(result); },
215
+ "repeat");
216
+ return fn;
217
+ }
218
+
219
+ inline AnyValue &get_replace_fn()
220
+ {
221
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
222
+ {
223
+ auto self = thisVal.as_string();
224
+ if (args.size() < 2)
225
+ return AnyValue::make_string(self->value);
226
+ std::string search = args[0].to_std_string();
227
+ std::string replacement = args[1].to_std_string();
228
+ std::string result = self->value;
229
+ size_t pos = result.find(search);
230
+ if (pos != std::string::npos)
231
+ {
232
+ result.replace(pos, search.length(), replacement);
233
+ }
234
+ return AnyValue::make_string(result); },
235
+ "replace");
236
+ return fn;
237
+ }
238
+
239
+ inline AnyValue &get_replaceAll_fn()
240
+ {
241
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
242
+ {
243
+ auto self = thisVal.as_string();
244
+ if (args.size() < 2)
245
+ return AnyValue::make_string(self->value);
246
+ std::string search = args[0].to_std_string();
247
+ if (search.empty())
248
+ return AnyValue::make_string(self->value);
249
+ std::string replacement = args[1].to_std_string();
250
+ std::string result = self->value;
251
+ size_t pos = result.find(search);
252
+ while (pos != std::string::npos)
253
+ {
254
+ result.replace(pos, search.length(), replacement);
255
+ pos = result.find(search, pos + replacement.length());
256
+ }
257
+ return AnyValue::make_string(result); },
258
+ "replaceAll");
259
+ return fn;
260
+ }
261
+
262
+ inline AnyValue &get_slice_fn()
263
+ {
264
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
265
+ {
266
+ auto self = thisVal.as_string();
267
+ int len = self->value.length();
268
+ int start = args.empty() ? 0 : Operators_Private::ToInt32(args[0]);
269
+ int end = (args.size() < 2 || args[1].is_undefined()) ? len : Operators_Private::ToInt32(args[1]);
270
+
271
+ if (start < 0)
272
+ start += len;
273
+ if (end < 0)
274
+ end += len;
275
+
276
+ start = std::max(0, std::min(len, start));
277
+ end = std::max(0, std::min(len, end));
278
+
279
+ if (start >= end)
280
+ return AnyValue::make_string("");
281
+ return AnyValue::make_string(self->value.substr(start, end - start)); },
282
+ "slice");
283
+ return fn;
284
+ }
285
+
286
+ inline AnyValue &get_split_fn()
287
+ {
288
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
289
+ {
290
+ auto self = thisVal.as_string();
291
+ std::string separator = (args.empty() || args[0].is_undefined()) ? "" : args[0].to_std_string();
292
+ std::vector<jspp::AnyValue> result_vec;
293
+
294
+ if (separator.empty())
295
+ {
296
+ for (char c : (self->value))
297
+ {
298
+ result_vec.push_back(AnyValue::make_string(std::string(1, c)));
299
+ }
300
+ }
301
+ else
302
+ {
303
+ std::string temp = (self->value);
304
+ size_t pos = 0;
305
+ while ((pos = temp.find(separator)) != std::string::npos)
306
+ {
307
+ result_vec.push_back(AnyValue::make_string(temp.substr(0, pos)));
308
+ temp.erase(0, pos + separator.length());
309
+ }
310
+ result_vec.push_back(AnyValue::make_string(temp));
311
+ }
312
+ return AnyValue::make_array(std::move(result_vec)); },
313
+ "split");
314
+ return fn;
315
+ }
316
+
317
+ inline AnyValue &get_startsWith_fn()
318
+ {
319
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
320
+ {
321
+ auto self = thisVal.as_string();
322
+ if (args.empty())
323
+ return Constants::FALSE;
324
+ std::string search = args[0].to_std_string();
325
+ size_t pos = (args.size() > 1) ? static_cast<size_t>(Operators_Private::ToNumber(args[1])) : 0;
326
+ if (pos > self->value.length())
327
+ pos = self->value.length();
328
+
329
+ return AnyValue::make_boolean(self->value.rfind(search, pos) == pos); },
330
+ "startsWith");
331
+ return fn;
332
+ }
333
+
334
+ inline AnyValue &get_substring_fn()
335
+ {
336
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
337
+ {
338
+ auto self = thisVal.as_string();
339
+ int len = self->value.length();
340
+ int start = args.empty() ? 0 : Operators_Private::ToInt32(args[0]);
341
+ int end = (args.size() < 2 || args[1].is_undefined()) ? len : Operators_Private::ToInt32(args[1]);
342
+
343
+ start = std::max(0, start);
344
+ end = std::max(0, end);
345
+
346
+ if (start > end)
347
+ std::swap(start, end);
348
+
349
+ start = std::min(len, start);
350
+ end = std::min(len, end);
351
+
352
+ return AnyValue::make_string(self->value.substr(start, end - start)); },
353
+ "substring");
354
+ return fn;
355
+ }
356
+
357
+ inline AnyValue &get_toLowerCase_fn()
358
+ {
359
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
360
+ {
361
+ std::string result = thisVal.as_string()->value;
362
+ std::transform(result.begin(), result.end(), result.begin(),
363
+ [](unsigned char c)
364
+ { return std::tolower(c); });
365
+ return AnyValue::make_string(result); },
366
+ "toLowerCase");
367
+ return fn;
368
+ }
369
+
370
+ inline AnyValue &get_toUpperCase_fn()
371
+ {
372
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
373
+ {
374
+ std::string result = thisVal.as_string()->value;
375
+ std::transform(result.begin(), result.end(), result.begin(),
376
+ [](unsigned char c)
377
+ { return std::toupper(c); });
378
+ return AnyValue::make_string(result); },
379
+ "toUpperCase");
380
+ return fn;
381
+ }
382
+
383
+ inline AnyValue &get_trim_fn()
384
+ {
385
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
386
+ {
387
+ const char *whitespace = " \t\n\r\f\v";
388
+ std::string result = thisVal.as_string()->value;
389
+ result.erase(0, result.find_first_not_of(whitespace));
390
+ result.erase(result.find_last_not_of(whitespace) + 1);
391
+ return AnyValue::make_string(result); },
392
+ "trim");
393
+ return fn;
394
+ }
395
+
396
+ inline AnyValue &get_trimEnd_fn()
397
+ {
398
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
399
+ {
400
+ const char *whitespace = " \t\n\r\f\v";
401
+ std::string result = thisVal.as_string()->value;
402
+ result.erase(result.find_last_not_of(whitespace) + 1);
403
+ return AnyValue::make_string(result); },
404
+ "trimEnd");
405
+ return fn;
406
+ }
407
+
408
+ inline AnyValue &get_trimStart_fn()
409
+ {
410
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
411
+ {
412
+ const char *whitespace = " \t\n\r\f\v";
413
+ std::string result = thisVal.as_string()->value;
414
+ result.erase(0, result.find_first_not_of(whitespace));
415
+ return AnyValue::make_string(result); },
416
+ "trimStart");
417
+ return fn;
418
+ }
419
+
420
+ // This function retrieves a prototype method for a given string instance.
421
+ inline std::optional<AnyValue> get(const std::string &key)
422
+ {
423
+ // --- toString() & valueOf() ---
424
+ if (key == "toString" || key == "valueOf" || key == WellKnownSymbols::toStringTag->key)
425
+ {
426
+ return get_toString_fn();
427
+ }
428
+
429
+ // --- [Symbol.iterator]() method ---
430
+ if (key == WellKnownSymbols::iterator->key)
431
+ {
432
+ return get_iterator_fn();
433
+ }
434
+
435
+ // --- length property ---
436
+ if (key == "length")
437
+ {
438
+ return get_length_desc();
439
+ }
440
+
441
+ // --- charAt(pos) ---
442
+ if (key == "charAt")
443
+ {
444
+ return get_charAt_fn();
445
+ }
446
+
447
+ // --- concat(str1, str2, ...) ---
448
+ if (key == "concat")
449
+ {
450
+ return get_concat_fn();
451
+ }
452
+
453
+ // --- endsWith(searchString, endPosition) ---
454
+ if (key == "endsWith")
455
+ {
456
+ return get_endsWith_fn();
457
+ }
458
+
459
+ // --- includes(searchString, position) ---
460
+ if (key == "includes")
461
+ {
462
+ return get_includes_fn();
463
+ }
464
+
465
+ // --- indexOf(searchString, position) ---
466
+ if (key == "indexOf")
467
+ {
468
+ return get_indexOf_fn();
469
+ }
470
+
471
+ // --- lastIndexOf(searchString, position) ---
472
+ if (key == "lastIndexOf")
473
+ {
474
+ return get_lastIndexOf_fn();
475
+ }
476
+
477
+ // --- padEnd(targetLength, padString) ---
478
+ if (key == "padEnd")
479
+ {
480
+ return get_padEnd_fn();
481
+ }
482
+
483
+ // --- padStart(targetLength, padString) ---
484
+ if (key == "padStart")
485
+ {
486
+ return get_padStart_fn();
487
+ }
488
+
489
+ // --- repeat(count) ---
490
+ if (key == "repeat")
491
+ {
492
+ return get_repeat_fn();
493
+ }
494
+
495
+ // --- replace(substr, newSubstr) ---
496
+ if (key == "replace")
497
+ {
498
+ return get_replace_fn();
499
+ }
500
+
501
+ // --- replaceAll(substr, newSubstr) ---
502
+ if (key == "replaceAll")
503
+ {
504
+ return get_replaceAll_fn();
505
+ }
506
+
507
+ // --- slice(beginIndex, endIndex) ---
508
+ if (key == "slice")
509
+ {
510
+ return get_slice_fn();
511
+ }
512
+
513
+ // --- split(separator) ---
514
+ if (key == "split")
515
+ {
516
+ return get_split_fn();
517
+ }
518
+
519
+ // --- startsWith(searchString, position) ---
520
+ if (key == "startsWith")
521
+ {
522
+ return get_startsWith_fn();
523
+ }
524
+
525
+ // --- substring(indexStart, indexEnd) ---
526
+ if (key == "substring")
527
+ {
528
+ return get_substring_fn();
529
+ }
530
+
531
+ // --- toLowerCase() ---
532
+ if (key == "toLowerCase")
533
+ {
534
+ return get_toLowerCase_fn();
535
+ }
536
+
537
+ // --- toUpperCase() ---
538
+ if (key == "toUpperCase")
539
+ {
540
+ return get_toUpperCase_fn();
541
+ }
542
+
543
+ // --- trim() ---
544
+ if (key == "trim")
545
+ {
546
+ return get_trim_fn();
547
+ }
548
+
549
+ // --- trimEnd() ---
550
+ if (key == "trimEnd")
551
+ {
552
+ return get_trimEnd_fn();
553
+ }
554
+
555
+ // --- trimStart() ---
556
+ if (key == "trimStart")
557
+ {
558
+ return get_trimStart_fn();
559
+ }
560
+
561
+ return std::nullopt;
562
+ }
563
+ }
564
+ }