@ugo-studio/jspp 0.1.3 → 0.1.5
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/README.md +2 -2
- package/dist/analysis/scope.js +33 -4
- package/dist/analysis/typeAnalyzer.js +260 -21
- package/dist/ast/symbols.js +29 -0
- package/dist/cli-utils/args.js +57 -0
- package/dist/cli-utils/colors.js +9 -0
- package/dist/cli-utils/file-utils.js +20 -0
- package/dist/cli-utils/spinner.js +55 -0
- package/dist/cli.js +105 -31
- package/dist/core/codegen/class-handlers.js +131 -0
- package/dist/core/codegen/control-flow-handlers.js +474 -0
- package/dist/core/codegen/declaration-handlers.js +36 -15
- package/dist/core/codegen/expression-handlers.js +579 -125
- package/dist/core/codegen/function-handlers.js +222 -37
- package/dist/core/codegen/helpers.js +158 -4
- package/dist/core/codegen/index.js +20 -8
- package/dist/core/codegen/literal-handlers.js +18 -6
- package/dist/core/codegen/statement-handlers.js +171 -228
- package/dist/core/codegen/visitor.js +31 -3
- package/package.json +3 -3
- package/src/prelude/any_value.hpp +510 -633
- package/src/prelude/any_value_access.hpp +151 -0
- package/src/prelude/any_value_defines.hpp +190 -0
- package/src/prelude/any_value_helpers.hpp +139 -225
- package/src/prelude/exception.hpp +32 -0
- package/src/prelude/exception_helpers.hpp +49 -0
- package/src/prelude/index.hpp +25 -9
- package/src/prelude/library/array.hpp +190 -0
- package/src/prelude/library/console.hpp +14 -13
- package/src/prelude/library/error.hpp +113 -0
- package/src/prelude/library/function.hpp +10 -0
- package/src/prelude/library/global.hpp +35 -4
- package/src/prelude/library/math.hpp +308 -0
- package/src/prelude/library/object.hpp +288 -0
- package/src/prelude/library/performance.hpp +2 -2
- package/src/prelude/library/process.hpp +39 -0
- package/src/prelude/library/promise.hpp +131 -0
- package/src/prelude/library/symbol.hpp +46 -59
- package/src/prelude/library/timer.hpp +92 -0
- package/src/prelude/scheduler.hpp +145 -0
- package/src/prelude/types.hpp +58 -1
- package/src/prelude/utils/access.hpp +345 -0
- package/src/prelude/utils/assignment_operators.hpp +99 -0
- package/src/prelude/utils/log_any_value/array.hpp +245 -0
- package/src/prelude/utils/log_any_value/config.hpp +32 -0
- package/src/prelude/utils/log_any_value/function.hpp +39 -0
- package/src/prelude/utils/log_any_value/fwd.hpp +15 -0
- package/src/prelude/utils/log_any_value/helpers.hpp +62 -0
- package/src/prelude/utils/log_any_value/log_any_value.hpp +94 -0
- package/src/prelude/utils/log_any_value/object.hpp +136 -0
- package/src/prelude/utils/log_any_value/primitives.hpp +43 -0
- package/src/prelude/utils/operators.hpp +751 -0
- package/src/prelude/utils/well_known_symbols.hpp +25 -0
- package/src/prelude/values/array.hpp +10 -7
- package/src/prelude/{descriptors.hpp → values/descriptors.hpp} +2 -2
- package/src/prelude/values/function.hpp +85 -51
- package/src/prelude/values/helpers/array.hpp +80 -35
- package/src/prelude/values/helpers/function.hpp +110 -77
- package/src/prelude/values/helpers/iterator.hpp +16 -10
- package/src/prelude/values/helpers/object.hpp +85 -10
- package/src/prelude/values/helpers/promise.hpp +181 -0
- package/src/prelude/values/helpers/string.hpp +3 -3
- package/src/prelude/values/helpers/symbol.hpp +2 -2
- package/src/prelude/values/iterator.hpp +14 -6
- package/src/prelude/values/object.hpp +14 -3
- package/src/prelude/values/promise.hpp +73 -0
- package/src/prelude/values/prototypes/array.hpp +855 -16
- package/src/prelude/values/prototypes/function.hpp +4 -4
- package/src/prelude/values/prototypes/iterator.hpp +11 -10
- package/src/prelude/values/prototypes/number.hpp +153 -0
- package/src/prelude/values/prototypes/object.hpp +26 -0
- package/src/prelude/values/prototypes/promise.hpp +134 -0
- package/src/prelude/values/prototypes/string.hpp +29 -29
- package/src/prelude/values/prototypes/symbol.hpp +22 -3
- package/src/prelude/values/shape.hpp +52 -0
- package/src/prelude/values/string.hpp +1 -1
- package/src/prelude/values/symbol.hpp +1 -1
- package/src/prelude/access.hpp +0 -91
- package/src/prelude/error.hpp +0 -31
- package/src/prelude/error_helpers.hpp +0 -59
- package/src/prelude/log_string.hpp +0 -407
- package/src/prelude/operators.hpp +0 -256
- package/src/prelude/well_known_symbols.hpp +0 -14
|
@@ -1,634 +1,511 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <cassert>
|
|
4
|
-
#include <cstdint>
|
|
5
|
-
#include <cstring>
|
|
6
|
-
#include <limits>
|
|
7
|
-
#include <new>
|
|
8
|
-
#include <sstream>
|
|
9
|
-
#include <iomanip>
|
|
10
|
-
#include <type_traits>
|
|
11
|
-
#include <memory>
|
|
12
|
-
#include <utility>
|
|
13
|
-
#include <string>
|
|
14
|
-
#include <map>
|
|
15
|
-
#include <vector>
|
|
16
|
-
#include <functional>
|
|
17
|
-
#include <cmath>
|
|
18
|
-
#include <optional>
|
|
19
|
-
|
|
20
|
-
#include
|
|
21
|
-
|
|
22
|
-
#include "
|
|
23
|
-
#include "values/
|
|
24
|
-
#include "values/
|
|
25
|
-
#include "values/
|
|
26
|
-
#include "values/
|
|
27
|
-
#include "values/
|
|
28
|
-
#include "
|
|
29
|
-
#include "
|
|
30
|
-
#include "
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
AnyValue &
|
|
257
|
-
{
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
v
|
|
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
|
-
|
|
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
|
-
static AnyValue
|
|
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
|
-
bool
|
|
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
|
-
return storage.symbol.get();
|
|
512
|
-
}
|
|
513
|
-
std::shared_ptr<JsIterator<AnyValue>> as_iterator() const
|
|
514
|
-
{
|
|
515
|
-
assert(is_iterator());
|
|
516
|
-
return storage.iterator; // Returns the shared_ptr, incrementing ref count
|
|
517
|
-
}
|
|
518
|
-
DataDescriptor *as_data_descriptor() const noexcept
|
|
519
|
-
{
|
|
520
|
-
assert(is_data_descriptor());
|
|
521
|
-
return storage.data_desc.get();
|
|
522
|
-
}
|
|
523
|
-
AccessorDescriptor *as_accessor_descriptor() const noexcept
|
|
524
|
-
{
|
|
525
|
-
assert(is_accessor_descriptor());
|
|
526
|
-
return storage.accessor_desc.get();
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
// --- PROPERTY ACCESS OPERATORS
|
|
530
|
-
AnyValue get_own_property(const std::string &key) const
|
|
531
|
-
{
|
|
532
|
-
switch (storage.type)
|
|
533
|
-
{
|
|
534
|
-
case JsType::Object:
|
|
535
|
-
return storage.object->get_property(key);
|
|
536
|
-
case JsType::Array:
|
|
537
|
-
return storage.array->get_property(key);
|
|
538
|
-
case JsType::Function:
|
|
539
|
-
return storage.function->get_property(key);
|
|
540
|
-
case JsType::Iterator:
|
|
541
|
-
return storage.iterator->get_property(key);
|
|
542
|
-
case JsType::Symbol:
|
|
543
|
-
return storage.symbol->get_property(key);
|
|
544
|
-
case JsType::String:
|
|
545
|
-
return storage.str->get_property(key);
|
|
546
|
-
case JsType::Undefined:
|
|
547
|
-
throw RuntimeError::make_error("Cannot read properties of undefined (reading '" + key + "')", "TypeError");
|
|
548
|
-
case JsType::Null:
|
|
549
|
-
throw RuntimeError::make_error("Cannot read properties of null (reading '" + key + "')", "TypeError");
|
|
550
|
-
default:
|
|
551
|
-
return AnyValue::make_undefined();
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
AnyValue get_own_property(uint32_t idx) const noexcept
|
|
555
|
-
{
|
|
556
|
-
switch (storage.type)
|
|
557
|
-
{
|
|
558
|
-
case JsType::Array:
|
|
559
|
-
return storage.array->get_property(idx);
|
|
560
|
-
case JsType::String:
|
|
561
|
-
return storage.str->get_property(idx);
|
|
562
|
-
default:
|
|
563
|
-
return get_own_property(std::to_string(idx));
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
AnyValue get_own_property(const AnyValue &key) const noexcept
|
|
567
|
-
{
|
|
568
|
-
if (key.storage.type == JsType::Number && storage.type == JsType::Array)
|
|
569
|
-
return storage.array->get_property(key.storage.number);
|
|
570
|
-
if (key.storage.type == JsType::Number && storage.type == JsType::String)
|
|
571
|
-
return storage.str->get_property(key.storage.number);
|
|
572
|
-
|
|
573
|
-
// If the key is a Symbol, use its internal key string
|
|
574
|
-
if (key.storage.type == JsType::Symbol)
|
|
575
|
-
return get_own_property(key.storage.symbol->key);
|
|
576
|
-
|
|
577
|
-
return get_own_property(key.to_std_string());
|
|
578
|
-
}
|
|
579
|
-
// for setting values
|
|
580
|
-
AnyValue set_own_property(const std::string &key, const AnyValue &value) const
|
|
581
|
-
{
|
|
582
|
-
switch (storage.type)
|
|
583
|
-
{
|
|
584
|
-
case JsType::Object:
|
|
585
|
-
return storage.object->set_property(key, value);
|
|
586
|
-
case JsType::Array:
|
|
587
|
-
return storage.array->set_property(key, value);
|
|
588
|
-
case JsType::Function:
|
|
589
|
-
return storage.function->set_property(key, value);
|
|
590
|
-
case JsType::Undefined:
|
|
591
|
-
throw RuntimeError::make_error("Cannot set properties of undefined (setting '" + key + "')", "TypeError");
|
|
592
|
-
case JsType::Null:
|
|
593
|
-
throw RuntimeError::make_error("Cannot set properties of null (setting '" + key + "')", "TypeError");
|
|
594
|
-
default:
|
|
595
|
-
return value;
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
AnyValue set_own_property(uint32_t idx, const AnyValue &value) const
|
|
599
|
-
{
|
|
600
|
-
if (storage.type == JsType::Array)
|
|
601
|
-
{
|
|
602
|
-
return storage.array->set_property(idx, value);
|
|
603
|
-
}
|
|
604
|
-
return set_own_property(std::to_string(idx), value);
|
|
605
|
-
}
|
|
606
|
-
AnyValue set_own_property(const AnyValue &key, const AnyValue &value) const
|
|
607
|
-
{
|
|
608
|
-
if (key.storage.type == JsType::Number && storage.type == JsType::Array)
|
|
609
|
-
{
|
|
610
|
-
return storage.array->set_property(key.storage.number, value);
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
// If the key is a Symbol, use its internal key string
|
|
614
|
-
if (key.storage.type == JsType::Symbol)
|
|
615
|
-
return set_own_property(key.storage.symbol->key, value);
|
|
616
|
-
|
|
617
|
-
return set_own_property(key.to_std_string(), value);
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
// --- HELPERS
|
|
621
|
-
const bool is_truthy() const noexcept;
|
|
622
|
-
|
|
623
|
-
const bool is_strictly_equal_to(const AnyValue &other) const noexcept;
|
|
624
|
-
const bool is_equal_to(const AnyValue &other) const noexcept;
|
|
625
|
-
|
|
626
|
-
const AnyValue is_strictly_equal_to_primitive(const AnyValue &other) const noexcept;
|
|
627
|
-
const AnyValue is_equal_to_primitive(const AnyValue &other) const noexcept;
|
|
628
|
-
|
|
629
|
-
const AnyValue not_strictly_equal_to_primitive(const AnyValue &other) const noexcept;
|
|
630
|
-
const AnyValue not_equal_to_primitive(const AnyValue &other) const noexcept;
|
|
631
|
-
|
|
632
|
-
const std::string to_std_string() const noexcept;
|
|
633
|
-
};
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cassert>
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <cstring>
|
|
6
|
+
#include <limits>
|
|
7
|
+
#include <new>
|
|
8
|
+
#include <sstream>
|
|
9
|
+
#include <iomanip>
|
|
10
|
+
#include <type_traits>
|
|
11
|
+
#include <memory>
|
|
12
|
+
#include <utility>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <map>
|
|
15
|
+
#include <vector>
|
|
16
|
+
#include <functional>
|
|
17
|
+
#include <cmath>
|
|
18
|
+
#include <optional>
|
|
19
|
+
#include <coroutine>
|
|
20
|
+
#include <variant>
|
|
21
|
+
|
|
22
|
+
#include "types.hpp"
|
|
23
|
+
#include "values/non_values.hpp"
|
|
24
|
+
#include "values/object.hpp"
|
|
25
|
+
#include "values/array.hpp"
|
|
26
|
+
#include "values/function.hpp"
|
|
27
|
+
#include "values/iterator.hpp"
|
|
28
|
+
#include "values/promise.hpp"
|
|
29
|
+
#include "values/symbol.hpp"
|
|
30
|
+
#include "values/string.hpp"
|
|
31
|
+
#include "values/descriptors.hpp"
|
|
32
|
+
#include "exception.hpp"
|
|
33
|
+
#include "utils/well_known_symbols.hpp"
|
|
34
|
+
|
|
35
|
+
namespace jspp
|
|
36
|
+
{
|
|
37
|
+
enum class JsType : uint8_t
|
|
38
|
+
{
|
|
39
|
+
Undefined = 0,
|
|
40
|
+
Null = 1,
|
|
41
|
+
Uninitialized = 2,
|
|
42
|
+
Boolean = 3,
|
|
43
|
+
Number = 4,
|
|
44
|
+
String = 5,
|
|
45
|
+
Object = 6,
|
|
46
|
+
Array = 7,
|
|
47
|
+
Function = 8,
|
|
48
|
+
Iterator = 9,
|
|
49
|
+
Symbol = 10,
|
|
50
|
+
Promise = 11,
|
|
51
|
+
DataDescriptor = 12,
|
|
52
|
+
AccessorDescriptor = 13,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// The variant order MUST match JsType
|
|
56
|
+
using AnyValueVariant = std::variant<
|
|
57
|
+
JsUndefined,
|
|
58
|
+
JsNull,
|
|
59
|
+
JsUninitialized,
|
|
60
|
+
bool,
|
|
61
|
+
double,
|
|
62
|
+
std::shared_ptr<JsString>,
|
|
63
|
+
std::shared_ptr<JsObject>,
|
|
64
|
+
std::shared_ptr<JsArray>,
|
|
65
|
+
std::shared_ptr<JsFunction>,
|
|
66
|
+
std::shared_ptr<JsIterator<AnyValue>>,
|
|
67
|
+
std::shared_ptr<JsSymbol>,
|
|
68
|
+
std::shared_ptr<JsPromise>,
|
|
69
|
+
std::shared_ptr<DataDescriptor>,
|
|
70
|
+
std::shared_ptr<AccessorDescriptor>>;
|
|
71
|
+
|
|
72
|
+
class AnyValue
|
|
73
|
+
{
|
|
74
|
+
private:
|
|
75
|
+
AnyValueVariant storage;
|
|
76
|
+
|
|
77
|
+
public:
|
|
78
|
+
// default ctor (Undefined)
|
|
79
|
+
AnyValue() noexcept = default;
|
|
80
|
+
|
|
81
|
+
// Copy/Move handled by std::variant
|
|
82
|
+
AnyValue(const AnyValue &) = default;
|
|
83
|
+
AnyValue(AnyValue &&) noexcept = default;
|
|
84
|
+
AnyValue &operator=(const AnyValue &) = default;
|
|
85
|
+
AnyValue &operator=(AnyValue &&) noexcept = default;
|
|
86
|
+
|
|
87
|
+
~AnyValue() = default;
|
|
88
|
+
|
|
89
|
+
// Assignments
|
|
90
|
+
AnyValue &operator=(const double &val)
|
|
91
|
+
{
|
|
92
|
+
storage = val;
|
|
93
|
+
return *this;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
friend void swap(AnyValue &a, AnyValue &b) noexcept
|
|
97
|
+
{
|
|
98
|
+
std::swap(a.storage, b.storage);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// --- FRIENDS for Optimized Operators
|
|
102
|
+
friend AnyValue &operator+=(AnyValue &lhs, const AnyValue &rhs);
|
|
103
|
+
friend AnyValue &operator-=(AnyValue &lhs, const AnyValue &rhs);
|
|
104
|
+
friend AnyValue &operator*=(AnyValue &lhs, const AnyValue &rhs);
|
|
105
|
+
friend AnyValue &operator/=(AnyValue &lhs, const AnyValue &rhs);
|
|
106
|
+
friend AnyValue &operator%=(AnyValue &lhs, const AnyValue &rhs);
|
|
107
|
+
friend AnyValue &operator++(AnyValue &val);
|
|
108
|
+
friend AnyValue operator++(AnyValue &val, int);
|
|
109
|
+
friend AnyValue &operator--(AnyValue &val);
|
|
110
|
+
friend AnyValue operator--(AnyValue &val, int);
|
|
111
|
+
|
|
112
|
+
// factories -------------------------------------------------------
|
|
113
|
+
static AnyValue make_number(double d) noexcept
|
|
114
|
+
{
|
|
115
|
+
AnyValue v;
|
|
116
|
+
v.storage = d;
|
|
117
|
+
return v;
|
|
118
|
+
}
|
|
119
|
+
static AnyValue make_nan() noexcept
|
|
120
|
+
{
|
|
121
|
+
AnyValue v;
|
|
122
|
+
v.storage = std::numeric_limits<double>::quiet_NaN();
|
|
123
|
+
return v;
|
|
124
|
+
}
|
|
125
|
+
static AnyValue make_uninitialized() noexcept
|
|
126
|
+
{
|
|
127
|
+
AnyValue v;
|
|
128
|
+
v.storage = JsUninitialized{};
|
|
129
|
+
return v;
|
|
130
|
+
}
|
|
131
|
+
static AnyValue make_undefined() noexcept
|
|
132
|
+
{
|
|
133
|
+
AnyValue v;
|
|
134
|
+
v.storage = JsUndefined{};
|
|
135
|
+
return v;
|
|
136
|
+
}
|
|
137
|
+
static AnyValue make_null() noexcept
|
|
138
|
+
{
|
|
139
|
+
AnyValue v;
|
|
140
|
+
v.storage = JsNull{};
|
|
141
|
+
return v;
|
|
142
|
+
}
|
|
143
|
+
static AnyValue make_boolean(bool b) noexcept
|
|
144
|
+
{
|
|
145
|
+
AnyValue v;
|
|
146
|
+
v.storage = b;
|
|
147
|
+
return v;
|
|
148
|
+
}
|
|
149
|
+
static AnyValue make_string(const std::string &raw_s) noexcept
|
|
150
|
+
{
|
|
151
|
+
AnyValue v;
|
|
152
|
+
v.storage = std::make_shared<JsString>(raw_s);
|
|
153
|
+
return v;
|
|
154
|
+
}
|
|
155
|
+
static AnyValue make_object(std::initializer_list<std::pair<std::string, AnyValue>> props) noexcept
|
|
156
|
+
{
|
|
157
|
+
AnyValue v;
|
|
158
|
+
v.storage = std::make_shared<JsObject>(props);
|
|
159
|
+
return v;
|
|
160
|
+
}
|
|
161
|
+
static AnyValue make_object(const std::map<std::string, AnyValue> &props) noexcept
|
|
162
|
+
{
|
|
163
|
+
AnyValue v;
|
|
164
|
+
v.storage = std::make_shared<JsObject>(props);
|
|
165
|
+
return v;
|
|
166
|
+
}
|
|
167
|
+
static AnyValue make_object_with_proto(std::initializer_list<std::pair<std::string, AnyValue>> props, const AnyValue &proto) noexcept
|
|
168
|
+
{
|
|
169
|
+
AnyValue v;
|
|
170
|
+
auto protoPtr = std::make_shared<AnyValue>(proto);
|
|
171
|
+
v.storage = std::make_shared<JsObject>(props, protoPtr);
|
|
172
|
+
return v;
|
|
173
|
+
}
|
|
174
|
+
static AnyValue make_object_with_proto(const std::map<std::string, AnyValue> &props, const AnyValue &proto) noexcept
|
|
175
|
+
{
|
|
176
|
+
AnyValue v;
|
|
177
|
+
auto protoPtr = std::make_shared<AnyValue>(proto);
|
|
178
|
+
v.storage = std::make_shared<JsObject>(props, protoPtr);
|
|
179
|
+
return v;
|
|
180
|
+
}
|
|
181
|
+
static AnyValue make_array(std::span<const AnyValue> dense) noexcept
|
|
182
|
+
{
|
|
183
|
+
AnyValue v;
|
|
184
|
+
std::vector<AnyValue> vec;
|
|
185
|
+
vec.reserve(dense.size());
|
|
186
|
+
for (const auto &item : dense)
|
|
187
|
+
vec.push_back(item);
|
|
188
|
+
v.storage = std::make_shared<JsArray>(std::move(vec));
|
|
189
|
+
return v;
|
|
190
|
+
}
|
|
191
|
+
static AnyValue make_array(const std::vector<AnyValue> &dense) noexcept
|
|
192
|
+
{
|
|
193
|
+
AnyValue v;
|
|
194
|
+
v.storage = std::make_shared<JsArray>(dense);
|
|
195
|
+
return v;
|
|
196
|
+
}
|
|
197
|
+
static AnyValue make_array(std::vector<AnyValue> &&dense) noexcept
|
|
198
|
+
{
|
|
199
|
+
AnyValue v;
|
|
200
|
+
v.storage = std::make_shared<JsArray>(std::move(dense));
|
|
201
|
+
return v;
|
|
202
|
+
}
|
|
203
|
+
static AnyValue make_array_with_proto(std::span<const AnyValue> dense, const AnyValue &proto) noexcept
|
|
204
|
+
{
|
|
205
|
+
AnyValue v;
|
|
206
|
+
std::vector<AnyValue> vec;
|
|
207
|
+
vec.reserve(dense.size());
|
|
208
|
+
for (const auto &item : dense)
|
|
209
|
+
vec.push_back(item);
|
|
210
|
+
v.storage = std::make_shared<JsArray>(std::move(vec));
|
|
211
|
+
std::get<std::shared_ptr<JsArray>>(v.storage)->proto = std::make_shared<AnyValue>(proto);
|
|
212
|
+
return v;
|
|
213
|
+
}
|
|
214
|
+
static AnyValue make_array_with_proto(const std::vector<AnyValue> &dense, const AnyValue &proto) noexcept
|
|
215
|
+
{
|
|
216
|
+
AnyValue v;
|
|
217
|
+
v.storage = std::make_shared<JsArray>(dense);
|
|
218
|
+
std::get<std::shared_ptr<JsArray>>(v.storage)->proto = std::make_shared<AnyValue>(proto);
|
|
219
|
+
return v;
|
|
220
|
+
}
|
|
221
|
+
static AnyValue make_function(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt, bool is_constructor = true) noexcept
|
|
222
|
+
{
|
|
223
|
+
AnyValue v;
|
|
224
|
+
v.storage = std::make_shared<JsFunction>(call, name, std::unordered_map<std::string, AnyValue>{}, false, is_constructor);
|
|
225
|
+
|
|
226
|
+
auto proto = make_object({});
|
|
227
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
228
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
229
|
+
|
|
230
|
+
return v;
|
|
231
|
+
}
|
|
232
|
+
static AnyValue make_class(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt) noexcept
|
|
233
|
+
{
|
|
234
|
+
AnyValue v;
|
|
235
|
+
// use Constructor A with is_cls = true
|
|
236
|
+
v.storage = std::make_shared<JsFunction>(call, name, std::unordered_map<std::string, AnyValue>{}, true);
|
|
237
|
+
|
|
238
|
+
auto proto = make_object({});
|
|
239
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
240
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
241
|
+
|
|
242
|
+
return v;
|
|
243
|
+
}
|
|
244
|
+
static AnyValue make_generator(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt) noexcept
|
|
245
|
+
{
|
|
246
|
+
AnyValue v;
|
|
247
|
+
// use Constructor B with is_gen = true
|
|
248
|
+
v.storage = std::make_shared<JsFunction>(call, true, name);
|
|
249
|
+
|
|
250
|
+
auto proto = make_object({});
|
|
251
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
252
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
253
|
+
|
|
254
|
+
return v;
|
|
255
|
+
}
|
|
256
|
+
static AnyValue make_async_function(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt) noexcept
|
|
257
|
+
{
|
|
258
|
+
AnyValue v;
|
|
259
|
+
// use Constructor C with is_async_func = true
|
|
260
|
+
v.storage = std::make_shared<JsFunction>(call, false, true, name);
|
|
261
|
+
|
|
262
|
+
auto proto = make_object({});
|
|
263
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
264
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
265
|
+
|
|
266
|
+
return v;
|
|
267
|
+
}
|
|
268
|
+
static AnyValue make_async_generator(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt) noexcept
|
|
269
|
+
{
|
|
270
|
+
AnyValue v;
|
|
271
|
+
// use Constructor C with is_gen = true and is_async_func = true
|
|
272
|
+
v.storage = std::make_shared<JsFunction>(call, true, true, name);
|
|
273
|
+
|
|
274
|
+
auto proto = make_object({});
|
|
275
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
276
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
277
|
+
|
|
278
|
+
return v;
|
|
279
|
+
}
|
|
280
|
+
static AnyValue make_symbol(const std::string &description = "") noexcept
|
|
281
|
+
{
|
|
282
|
+
AnyValue v;
|
|
283
|
+
v.storage = std::make_shared<JsSymbol>(description);
|
|
284
|
+
return v;
|
|
285
|
+
}
|
|
286
|
+
static AnyValue make_promise(const JsPromise &promise) noexcept
|
|
287
|
+
{
|
|
288
|
+
AnyValue v;
|
|
289
|
+
v.storage = std::make_shared<JsPromise>(promise);
|
|
290
|
+
return v;
|
|
291
|
+
}
|
|
292
|
+
static AnyValue make_data_descriptor(const AnyValue &value, bool writable, bool enumerable, bool configurable) noexcept
|
|
293
|
+
{
|
|
294
|
+
AnyValue v;
|
|
295
|
+
v.storage = std::make_shared<DataDescriptor>(std::make_shared<AnyValue>(value), writable, enumerable, configurable);
|
|
296
|
+
return v;
|
|
297
|
+
}
|
|
298
|
+
static AnyValue make_accessor_descriptor(const std::optional<std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>> &get,
|
|
299
|
+
const std::optional<std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>> &set,
|
|
300
|
+
bool enumerable,
|
|
301
|
+
bool configurable) noexcept
|
|
302
|
+
{
|
|
303
|
+
AnyValue v;
|
|
304
|
+
v.storage = std::make_shared<AccessorDescriptor>(get, set, enumerable, configurable);
|
|
305
|
+
return v;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
static AnyValue from_symbol(std::shared_ptr<JsSymbol> sym) noexcept
|
|
309
|
+
{
|
|
310
|
+
AnyValue v;
|
|
311
|
+
v.storage = std::move(sym);
|
|
312
|
+
return v;
|
|
313
|
+
}
|
|
314
|
+
static AnyValue from_string(std::shared_ptr<JsString> str) noexcept
|
|
315
|
+
{
|
|
316
|
+
AnyValue v;
|
|
317
|
+
v.storage = std::move(str);
|
|
318
|
+
return v;
|
|
319
|
+
}
|
|
320
|
+
static AnyValue from_iterator(JsIterator<AnyValue> &&iterator) noexcept
|
|
321
|
+
{
|
|
322
|
+
AnyValue v;
|
|
323
|
+
v.storage = std::make_shared<JsIterator<AnyValue>>(std::move(iterator));
|
|
324
|
+
return v;
|
|
325
|
+
}
|
|
326
|
+
static AnyValue from_iterator_ref(JsIterator<AnyValue> *iterator) noexcept
|
|
327
|
+
{
|
|
328
|
+
AnyValue v;
|
|
329
|
+
v.storage = std::shared_ptr<JsIterator<AnyValue>>(iterator, [](JsIterator<AnyValue> *) {});
|
|
330
|
+
return v;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// PROPERTY RESOLUTION HELPERS ---------------------------------------
|
|
334
|
+
static AnyValue resolve_property_for_read(const AnyValue &val, const AnyValue &thisVal, const std::string &propName) noexcept
|
|
335
|
+
{
|
|
336
|
+
switch (val.get_type())
|
|
337
|
+
{
|
|
338
|
+
case JsType::DataDescriptor:
|
|
339
|
+
{
|
|
340
|
+
return *(val.as_data_descriptor()->value);
|
|
341
|
+
}
|
|
342
|
+
case JsType::AccessorDescriptor:
|
|
343
|
+
{
|
|
344
|
+
const auto &accessor = val.as_accessor_descriptor();
|
|
345
|
+
if (accessor->get.has_value())
|
|
346
|
+
return accessor->get.value()(thisVal, std::span<const AnyValue>{});
|
|
347
|
+
else
|
|
348
|
+
{
|
|
349
|
+
static AnyValue undefined = AnyValue{};
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
default:
|
|
354
|
+
{
|
|
355
|
+
return val;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
static AnyValue resolve_property_for_write(AnyValue &val, const AnyValue &thisVal, const AnyValue &new_val, const std::string &propName)
|
|
360
|
+
{
|
|
361
|
+
switch (val.get_type())
|
|
362
|
+
{
|
|
363
|
+
case JsType::DataDescriptor:
|
|
364
|
+
{
|
|
365
|
+
const auto &data = val.as_data_descriptor();
|
|
366
|
+
if (data->writable)
|
|
367
|
+
{
|
|
368
|
+
*(data->value) = new_val;
|
|
369
|
+
return new_val;
|
|
370
|
+
}
|
|
371
|
+
else
|
|
372
|
+
{
|
|
373
|
+
throw Exception::make_exception("Cannot assign to read only property '" + propName + "' of object '#<Object>'", "TypeError");
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
case JsType::AccessorDescriptor:
|
|
377
|
+
{
|
|
378
|
+
const auto &accessor = val.as_accessor_descriptor();
|
|
379
|
+
if (accessor->set.has_value())
|
|
380
|
+
{
|
|
381
|
+
const AnyValue args[] = {new_val};
|
|
382
|
+
accessor->set.value()(thisVal, args);
|
|
383
|
+
return new_val;
|
|
384
|
+
}
|
|
385
|
+
else
|
|
386
|
+
{
|
|
387
|
+
throw Exception::make_exception("Cannot set property of #<Object> which has only a getter", "TypeError");
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
default:
|
|
391
|
+
{
|
|
392
|
+
val = new_val;
|
|
393
|
+
return new_val;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// TYPE CHECKERS AND ACCESSORS ---------------------------------------
|
|
399
|
+
JsType get_type() const noexcept { return static_cast<JsType>(storage.index()); }
|
|
400
|
+
bool is_number() const noexcept { return storage.index() == 4; }
|
|
401
|
+
bool is_string() const noexcept { return storage.index() == 5; }
|
|
402
|
+
bool is_object() const noexcept { return storage.index() == 6; }
|
|
403
|
+
bool is_array() const noexcept { return storage.index() == 7; }
|
|
404
|
+
bool is_function() const noexcept { return storage.index() == 8; }
|
|
405
|
+
bool is_iterator() const noexcept { return storage.index() == 9; }
|
|
406
|
+
bool is_boolean() const noexcept { return storage.index() == 3; }
|
|
407
|
+
bool is_symbol() const noexcept { return storage.index() == 10; }
|
|
408
|
+
bool is_promise() const noexcept { return storage.index() == 11; }
|
|
409
|
+
bool is_null() const noexcept { return storage.index() == 1; }
|
|
410
|
+
bool is_undefined() const noexcept { return storage.index() == 0; }
|
|
411
|
+
bool is_uninitialized() const noexcept { return storage.index() == 2; }
|
|
412
|
+
bool is_data_descriptor() const noexcept { return storage.index() == 12; }
|
|
413
|
+
bool is_accessor_descriptor() const noexcept { return storage.index() == 13; }
|
|
414
|
+
bool is_generator() const noexcept { return is_function() && as_function()->is_generator; }
|
|
415
|
+
|
|
416
|
+
// --- TYPE CASTERS
|
|
417
|
+
double as_double() const noexcept
|
|
418
|
+
{
|
|
419
|
+
return std::get<double>(storage);
|
|
420
|
+
}
|
|
421
|
+
bool as_boolean() const noexcept
|
|
422
|
+
{
|
|
423
|
+
return std::get<bool>(storage);
|
|
424
|
+
}
|
|
425
|
+
JsString *as_string() const noexcept
|
|
426
|
+
{
|
|
427
|
+
return std::get<std::shared_ptr<JsString>>(storage).get();
|
|
428
|
+
}
|
|
429
|
+
JsObject *as_object() const noexcept
|
|
430
|
+
{
|
|
431
|
+
return std::get<std::shared_ptr<JsObject>>(storage).get();
|
|
432
|
+
}
|
|
433
|
+
JsArray *as_array() const noexcept
|
|
434
|
+
{
|
|
435
|
+
return std::get<std::shared_ptr<JsArray>>(storage).get();
|
|
436
|
+
}
|
|
437
|
+
JsFunction *as_function() const noexcept
|
|
438
|
+
{
|
|
439
|
+
return std::get<std::shared_ptr<JsFunction>>(storage).get();
|
|
440
|
+
}
|
|
441
|
+
JsSymbol *as_symbol() const noexcept
|
|
442
|
+
{
|
|
443
|
+
return std::get<std::shared_ptr<JsSymbol>>(storage).get();
|
|
444
|
+
}
|
|
445
|
+
JsPromise *as_promise() const noexcept
|
|
446
|
+
{
|
|
447
|
+
return std::get<std::shared_ptr<JsPromise>>(storage).get();
|
|
448
|
+
}
|
|
449
|
+
std::shared_ptr<JsIterator<AnyValue>> as_iterator() const
|
|
450
|
+
{
|
|
451
|
+
return std::get<std::shared_ptr<JsIterator<AnyValue>>>(storage);
|
|
452
|
+
}
|
|
453
|
+
DataDescriptor *as_data_descriptor() const noexcept
|
|
454
|
+
{
|
|
455
|
+
return std::get<std::shared_ptr<DataDescriptor>>(storage).get();
|
|
456
|
+
}
|
|
457
|
+
AccessorDescriptor *as_accessor_descriptor() const noexcept
|
|
458
|
+
{
|
|
459
|
+
return std::get<std::shared_ptr<AccessorDescriptor>>(storage).get();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// --- CO_AWAIT Operator ---
|
|
463
|
+
auto operator co_await() const;
|
|
464
|
+
|
|
465
|
+
// --- PROPERTY ACCESS OPERATORS
|
|
466
|
+
bool has_property(const std::string &key) const;
|
|
467
|
+
AnyValue get_own_property(const std::string &key) const;
|
|
468
|
+
AnyValue get_own_property(uint32_t idx) const;
|
|
469
|
+
AnyValue get_own_property(const AnyValue &key) const;
|
|
470
|
+
// for getting values with a specific receiver (used in inheritance chains)
|
|
471
|
+
AnyValue get_property_with_receiver(const std::string &key, const AnyValue &receiver) const;
|
|
472
|
+
// for setting values
|
|
473
|
+
AnyValue set_own_property(const std::string &key, const AnyValue &value) const;
|
|
474
|
+
AnyValue set_own_property(uint32_t idx, const AnyValue &value) const;
|
|
475
|
+
AnyValue set_own_property(const AnyValue &key, const AnyValue &value) const;
|
|
476
|
+
|
|
477
|
+
// --- DEFINERS (Object.defineProperty semantics)
|
|
478
|
+
void define_data_property(const std::string &key, const AnyValue &value);
|
|
479
|
+
void define_data_property(const AnyValue &key, const AnyValue &value);
|
|
480
|
+
void define_data_property(const std::string &key, const AnyValue &value, bool writable, bool enumerable, bool configurable);
|
|
481
|
+
void define_getter(const std::string &key, const AnyValue &getter);
|
|
482
|
+
void define_getter(const AnyValue &key, const AnyValue &getter);
|
|
483
|
+
void define_setter(const std::string &key, const AnyValue &setter);
|
|
484
|
+
void define_setter(const AnyValue &key, const AnyValue &setter);
|
|
485
|
+
|
|
486
|
+
// --- HELPERS
|
|
487
|
+
const AnyValue call(const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &expr) const;
|
|
488
|
+
const AnyValue construct(std::span<const AnyValue> args, const std::optional<std::string> &name) const;
|
|
489
|
+
void set_prototype(const AnyValue &proto);
|
|
490
|
+
std::string to_std_string() const;
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
// Inline implementation of operator co_await
|
|
494
|
+
inline auto AnyValue::operator co_await() const
|
|
495
|
+
{
|
|
496
|
+
return AnyValueAwaiter{*this};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Global Constants for Optimization
|
|
500
|
+
namespace Constants
|
|
501
|
+
{
|
|
502
|
+
inline const AnyValue UNINITIALIZED = AnyValue::make_uninitialized();
|
|
503
|
+
inline const AnyValue UNDEFINED = AnyValue::make_undefined();
|
|
504
|
+
inline const AnyValue Null = AnyValue::make_null();
|
|
505
|
+
inline const AnyValue NaN = AnyValue::make_nan();
|
|
506
|
+
inline const AnyValue TRUE = AnyValue::make_boolean(true);
|
|
507
|
+
inline const AnyValue FALSE = AnyValue::make_boolean(false);
|
|
508
|
+
inline const AnyValue ZERO = AnyValue::make_number(0.0);
|
|
509
|
+
inline const AnyValue ONE = AnyValue::make_number(1.0);
|
|
510
|
+
}
|
|
634
511
|
}
|