@ugo-studio/jspp 0.2.8 → 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.
- package/dist/analysis/typeAnalyzer.js +42 -27
- package/dist/core/codegen/class-handlers.js +6 -6
- package/dist/core/codegen/control-flow-handlers.js +4 -4
- package/dist/core/codegen/declaration-handlers.js +21 -3
- package/dist/core/codegen/destructuring-handlers.js +187 -0
- package/dist/core/codegen/expression-handlers.js +7 -0
- package/dist/core/codegen/function-handlers.js +58 -36
- package/dist/core/codegen/helpers.js +288 -52
- package/dist/core/codegen/index.js +7 -4
- package/dist/core/codegen/statement-handlers.js +43 -23
- package/package.json +1 -1
- package/scripts/precompile-headers.ts +13 -5
- package/src/prelude/any_value.hpp +362 -361
- package/src/prelude/any_value_access.hpp +170 -170
- package/src/prelude/any_value_defines.hpp +189 -189
- package/src/prelude/any_value_helpers.hpp +374 -365
- package/src/prelude/library/array.hpp +185 -185
- package/src/prelude/library/console.hpp +111 -111
- package/src/prelude/library/error.hpp +112 -112
- package/src/prelude/library/function.hpp +10 -10
- package/src/prelude/library/math.hpp +307 -307
- package/src/prelude/library/object.hpp +275 -275
- package/src/prelude/library/performance.hpp +1 -1
- package/src/prelude/library/process.hpp +39 -39
- package/src/prelude/library/promise.hpp +123 -123
- package/src/prelude/library/symbol.hpp +52 -52
- package/src/prelude/library/timer.hpp +91 -91
- package/src/prelude/types.hpp +178 -178
- package/src/prelude/utils/access.hpp +411 -393
- package/src/prelude/utils/operators.hpp +336 -329
- package/src/prelude/values/array.hpp +0 -1
- package/src/prelude/values/async_iterator.hpp +83 -81
- package/src/prelude/values/function.hpp +82 -82
- package/src/prelude/values/helpers/array.hpp +198 -208
- package/src/prelude/values/helpers/async_iterator.hpp +275 -271
- package/src/prelude/values/helpers/function.hpp +108 -108
- package/src/prelude/values/helpers/iterator.hpp +144 -107
- package/src/prelude/values/helpers/promise.hpp +253 -253
- package/src/prelude/values/helpers/string.hpp +37 -47
- package/src/prelude/values/iterator.hpp +32 -5
- package/src/prelude/values/promise.hpp +72 -72
- package/src/prelude/values/prototypes/array.hpp +54 -42
- package/src/prelude/values/prototypes/iterator.hpp +201 -74
- package/src/prelude/values/prototypes/promise.hpp +196 -196
- package/src/prelude/values/prototypes/string.hpp +564 -542
- 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([](
|
|
28
|
-
{
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
static AnyValue
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
std::string
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
std::string replacement = args[1].to_std_string();
|
|
228
|
-
std::string result = self->value;
|
|
229
|
-
size_t pos = result.find(search);
|
|
230
|
-
|
|
231
|
-
{
|
|
232
|
-
result.replace(pos, search.length(), replacement);
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
pos =
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
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
|
+
}
|