@ugo-studio/jspp 0.2.8 → 0.2.9

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 (41) hide show
  1. package/dist/analysis/typeAnalyzer.js +42 -27
  2. package/dist/core/codegen/class-handlers.js +2 -2
  3. package/dist/core/codegen/control-flow-handlers.js +4 -4
  4. package/dist/core/codegen/declaration-handlers.js +21 -3
  5. package/dist/core/codegen/destructuring-handlers.js +187 -0
  6. package/dist/core/codegen/expression-handlers.js +7 -0
  7. package/dist/core/codegen/function-handlers.js +58 -36
  8. package/dist/core/codegen/helpers.js +288 -52
  9. package/dist/core/codegen/index.js +7 -4
  10. package/dist/core/codegen/statement-handlers.js +42 -22
  11. package/package.json +1 -1
  12. package/scripts/precompile-headers.ts +13 -5
  13. package/src/prelude/any_value.hpp +26 -25
  14. package/src/prelude/any_value_access.hpp +7 -7
  15. package/src/prelude/any_value_defines.hpp +17 -17
  16. package/src/prelude/any_value_helpers.hpp +16 -7
  17. package/src/prelude/library/array.hpp +7 -7
  18. package/src/prelude/library/console.hpp +5 -5
  19. package/src/prelude/library/error.hpp +3 -3
  20. package/src/prelude/library/function.hpp +1 -1
  21. package/src/prelude/library/math.hpp +38 -38
  22. package/src/prelude/library/object.hpp +16 -16
  23. package/src/prelude/library/performance.hpp +1 -1
  24. package/src/prelude/library/process.hpp +1 -1
  25. package/src/prelude/library/promise.hpp +8 -8
  26. package/src/prelude/library/symbol.hpp +3 -3
  27. package/src/prelude/library/timer.hpp +4 -4
  28. package/src/prelude/types.hpp +4 -4
  29. package/src/prelude/utils/access.hpp +24 -6
  30. package/src/prelude/utils/operators.hpp +7 -0
  31. package/src/prelude/values/async_iterator.hpp +5 -3
  32. package/src/prelude/values/function.hpp +3 -3
  33. package/src/prelude/values/helpers/async_iterator.hpp +7 -3
  34. package/src/prelude/values/helpers/function.hpp +10 -10
  35. package/src/prelude/values/helpers/iterator.hpp +39 -2
  36. package/src/prelude/values/helpers/promise.hpp +9 -9
  37. package/src/prelude/values/helpers/string.hpp +17 -3
  38. package/src/prelude/values/iterator.hpp +32 -5
  39. package/src/prelude/values/promise.hpp +7 -7
  40. package/src/prelude/values/prototypes/array.hpp +41 -41
  41. package/src/prelude/values/prototypes/iterator.hpp +128 -1
@@ -22,20 +22,20 @@ namespace jspp
22
22
 
23
23
  inline AnyValue &get_iterator_fn()
24
24
  {
25
- static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> _) -> AnyValue
26
- { return AnyValue::from_iterator(thisVal.as_array()->get_iterator()); },
25
+ static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
26
+ { return thisVal.as_array()->get_iterator(); },
27
27
  "Symbol.iterator");
28
28
  return fn;
29
29
  }
30
30
 
31
31
  inline AnyValue &get_length_desc()
32
32
  {
33
- static auto getter = [](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
33
+ static auto getter = [](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
34
34
  {
35
35
  return AnyValue::make_number(thisVal.as_array()->length);
36
36
  };
37
37
 
38
- static auto setter = [](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
38
+ static auto setter = [](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
39
39
  {
40
40
  if (args.empty())
41
41
  {
@@ -84,7 +84,7 @@ namespace jspp
84
84
 
85
85
  inline AnyValue &get_push_fn()
86
86
  {
87
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
87
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
88
88
  {
89
89
  auto self = thisVal.as_array();
90
90
  for (const auto &arg : args)
@@ -98,7 +98,7 @@ namespace jspp
98
98
 
99
99
  inline AnyValue &get_pop_fn()
100
100
  {
101
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
101
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
102
102
  {
103
103
  auto self = thisVal.as_array();
104
104
  if (self->length == 0)
@@ -124,7 +124,7 @@ namespace jspp
124
124
 
125
125
  inline AnyValue &get_shift_fn()
126
126
  {
127
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
127
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
128
128
  {
129
129
  auto self = thisVal.as_array();
130
130
  if (self->length == 0)
@@ -156,7 +156,7 @@ namespace jspp
156
156
 
157
157
  inline AnyValue &get_unshift_fn()
158
158
  {
159
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
159
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
160
160
  {
161
161
  auto self = thisVal.as_array();
162
162
  size_t args_count = args.size();
@@ -184,7 +184,7 @@ namespace jspp
184
184
 
185
185
  inline AnyValue &get_join_fn()
186
186
  {
187
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
187
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
188
188
  {
189
189
  auto self = thisVal.as_array();
190
190
  std::string sep = ",";
@@ -213,7 +213,7 @@ namespace jspp
213
213
 
214
214
  inline AnyValue &get_forEach_fn()
215
215
  {
216
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
216
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
217
217
  {
218
218
  auto self = thisVal.as_array();
219
219
  if (args.empty() || !args[0].is_function())
@@ -238,7 +238,7 @@ namespace jspp
238
238
 
239
239
  inline AnyValue &get_at_fn()
240
240
  {
241
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
241
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
242
242
  {
243
243
  auto self = thisVal.as_array();
244
244
  double len = static_cast<double>(self->length);
@@ -254,7 +254,7 @@ namespace jspp
254
254
 
255
255
  inline AnyValue &get_includes_fn()
256
256
  {
257
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
257
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
258
258
  {
259
259
  auto self = thisVal.as_array();
260
260
  AnyValue searchElement = args.empty() ? Constants::UNDEFINED : args[0];
@@ -280,7 +280,7 @@ namespace jspp
280
280
 
281
281
  inline AnyValue &get_indexOf_fn()
282
282
  {
283
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
283
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
284
284
  {
285
285
  auto self = thisVal.as_array();
286
286
  AnyValue searchElement = args.empty() ? Constants::UNDEFINED : args[0];
@@ -306,7 +306,7 @@ namespace jspp
306
306
 
307
307
  inline AnyValue &get_lastIndexOf_fn()
308
308
  {
309
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
309
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
310
310
  {
311
311
  auto self = thisVal.as_array();
312
312
  AnyValue searchElement = args.empty() ? Constants::UNDEFINED : args[0];
@@ -333,7 +333,7 @@ namespace jspp
333
333
 
334
334
  inline AnyValue &get_find_fn()
335
335
  {
336
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
336
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
337
337
  {
338
338
  auto self = thisVal.as_array();
339
339
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -357,7 +357,7 @@ namespace jspp
357
357
 
358
358
  inline AnyValue &get_findIndex_fn()
359
359
  {
360
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
360
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
361
361
  {
362
362
  auto self = thisVal.as_array();
363
363
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -381,7 +381,7 @@ namespace jspp
381
381
 
382
382
  inline AnyValue &get_findLast_fn()
383
383
  {
384
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
384
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
385
385
  {
386
386
  auto self = thisVal.as_array();
387
387
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -405,7 +405,7 @@ namespace jspp
405
405
 
406
406
  inline AnyValue &get_findLastIndex_fn()
407
407
  {
408
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
408
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
409
409
  {
410
410
  auto self = thisVal.as_array();
411
411
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -429,7 +429,7 @@ namespace jspp
429
429
 
430
430
  inline AnyValue &get_values_fn()
431
431
  {
432
- static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
432
+ static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
433
433
  { return thisVal.as_array()->get_iterator(); },
434
434
  "values");
435
435
  return fn;
@@ -437,7 +437,7 @@ namespace jspp
437
437
 
438
438
  inline AnyValue &get_keys_fn()
439
439
  {
440
- static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
440
+ static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
441
441
  {
442
442
  auto self = thisVal.as_array();
443
443
  for (uint64_t i = 0; i < self->length; ++i) {
@@ -450,7 +450,7 @@ namespace jspp
450
450
 
451
451
  inline AnyValue &get_entries_fn()
452
452
  {
453
- static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
453
+ static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> _) -> jspp::JsIterator<jspp::AnyValue>
454
454
  {
455
455
  auto self = thisVal.as_array();
456
456
  for (uint64_t i = 0; i < self->length; ++i) {
@@ -466,7 +466,7 @@ namespace jspp
466
466
 
467
467
  inline AnyValue &get_map_fn()
468
468
  {
469
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
469
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
470
470
  {
471
471
  auto self = thisVal.as_array();
472
472
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -493,7 +493,7 @@ namespace jspp
493
493
 
494
494
  inline AnyValue &get_filter_fn()
495
495
  {
496
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
496
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
497
497
  {
498
498
  auto self = thisVal.as_array();
499
499
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -519,7 +519,7 @@ namespace jspp
519
519
 
520
520
  inline AnyValue &get_every_fn()
521
521
  {
522
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
522
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
523
523
  {
524
524
  auto self = thisVal.as_array();
525
525
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -543,7 +543,7 @@ namespace jspp
543
543
 
544
544
  inline AnyValue &get_some_fn()
545
545
  {
546
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
546
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
547
547
  {
548
548
  auto self = thisVal.as_array();
549
549
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -567,7 +567,7 @@ namespace jspp
567
567
 
568
568
  inline AnyValue &get_reduce_fn()
569
569
  {
570
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
570
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
571
571
  {
572
572
  auto self = thisVal.as_array();
573
573
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -607,7 +607,7 @@ namespace jspp
607
607
 
608
608
  inline AnyValue &get_reduceRight_fn()
609
609
  {
610
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
610
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
611
611
  {
612
612
  auto self = thisVal.as_array();
613
613
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -647,7 +647,7 @@ namespace jspp
647
647
 
648
648
  inline AnyValue &get_flat_fn()
649
649
  {
650
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
650
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
651
651
  {
652
652
  auto self = thisVal.as_array();
653
653
  double depthVal = (args.size() > 0 && !args[0].is_undefined()) ? Operators_Private::ToNumber(args[0]) : 1;
@@ -681,7 +681,7 @@ namespace jspp
681
681
 
682
682
  inline AnyValue &get_flatMap_fn()
683
683
  {
684
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
684
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
685
685
  {
686
686
  auto self = thisVal.as_array();
687
687
  if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
@@ -716,7 +716,7 @@ namespace jspp
716
716
 
717
717
  inline AnyValue &get_fill_fn()
718
718
  {
719
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
719
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
720
720
  {
721
721
  auto self = thisVal.as_array();
722
722
  AnyValue value = args.empty() ? Constants::UNDEFINED : args[0];
@@ -742,7 +742,7 @@ namespace jspp
742
742
 
743
743
  inline AnyValue &get_reverse_fn()
744
744
  {
745
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
745
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
746
746
  {
747
747
  auto self = thisVal.as_array();
748
748
  uint64_t len = self->length;
@@ -775,7 +775,7 @@ namespace jspp
775
775
 
776
776
  inline AnyValue &get_sort_fn()
777
777
  {
778
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
778
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
779
779
  {
780
780
  auto self = thisVal.as_array();
781
781
  AnyValue compareFn = args.empty() ? Constants::UNDEFINED : args[0];
@@ -818,7 +818,7 @@ namespace jspp
818
818
 
819
819
  inline AnyValue &get_splice_fn()
820
820
  {
821
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
821
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
822
822
  {
823
823
  auto self = thisVal.as_array();
824
824
  double len = static_cast<double>(self->length);
@@ -890,7 +890,7 @@ namespace jspp
890
890
 
891
891
  inline AnyValue &get_copyWithin_fn()
892
892
  {
893
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
893
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
894
894
  {
895
895
  auto self = thisVal.as_array();
896
896
  double len = static_cast<double>(self->length);
@@ -942,7 +942,7 @@ namespace jspp
942
942
 
943
943
  inline AnyValue &get_concat_fn()
944
944
  {
945
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
945
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
946
946
  {
947
947
  auto self = thisVal.as_array();
948
948
  std::vector<AnyValue> result;
@@ -989,7 +989,7 @@ namespace jspp
989
989
 
990
990
  inline AnyValue &get_slice_fn()
991
991
  {
992
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
992
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
993
993
  {
994
994
  auto self = thisVal.as_array();
995
995
  double len = static_cast<double>(self->length);
@@ -1021,7 +1021,7 @@ namespace jspp
1021
1021
 
1022
1022
  inline AnyValue &get_toReversed_fn()
1023
1023
  {
1024
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
1024
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
1025
1025
  {
1026
1026
  auto copy = thisVal.get_property_with_receiver("slice", thisVal).call(thisVal, {});
1027
1027
  copy.get_own_property("reverse").call(copy, {});
@@ -1032,7 +1032,7 @@ namespace jspp
1032
1032
 
1033
1033
  inline AnyValue &get_toSorted_fn()
1034
1034
  {
1035
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
1035
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
1036
1036
  {
1037
1037
  auto copy = thisVal.get_property_with_receiver("slice", thisVal).call(thisVal, {});
1038
1038
  copy.get_own_property("sort").call(copy, args);
@@ -1043,7 +1043,7 @@ namespace jspp
1043
1043
 
1044
1044
  inline AnyValue &get_toSpliced_fn()
1045
1045
  {
1046
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
1046
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
1047
1047
  {
1048
1048
  auto copy = thisVal.get_property_with_receiver("slice", thisVal).call(thisVal, {});
1049
1049
  copy.get_own_property("splice").call(copy, args);
@@ -1054,7 +1054,7 @@ namespace jspp
1054
1054
 
1055
1055
  inline AnyValue &get_with_fn()
1056
1056
  {
1057
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
1057
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
1058
1058
  {
1059
1059
  auto self = thisVal.as_array();
1060
1060
  auto copy = thisVal.get_property_with_receiver("slice", thisVal).call(thisVal, {});
@@ -1075,7 +1075,7 @@ namespace jspp
1075
1075
 
1076
1076
  inline AnyValue &get_toLocaleString_fn()
1077
1077
  {
1078
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
1078
+ static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
1079
1079
  {
1080
1080
  auto self = thisVal.as_array();
1081
1081
  std::string result = "";
@@ -37,11 +37,113 @@ namespace jspp
37
37
  return fn;
38
38
  }
39
39
 
40
+ inline AnyValue &get_return_fn()
41
+ {
42
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
43
+ {
44
+ AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
45
+ auto res = thisVal.as_iterator()->return_(val);
46
+ return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
47
+ "return");
48
+ return fn;
49
+ }
50
+
51
+ inline AnyValue &get_throw_fn()
52
+ {
53
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
54
+ {
55
+ AnyValue err = args.empty() ? Constants::UNDEFINED : args[0];
56
+ auto res = thisVal.as_iterator()->throw_(err);
57
+ return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
58
+ "throw");
59
+ return fn;
60
+ }
61
+
40
62
  inline AnyValue &get_toArray_fn()
41
63
  {
42
64
  static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
43
65
  { return AnyValue::make_array(thisVal.as_iterator()->to_vector()); },
44
- "toArray");
66
+ "drop");
67
+ return fn;
68
+ }
69
+
70
+ inline AnyValue &get_drop_fn()
71
+ {
72
+ static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> args) -> JsIterator<AnyValue>
73
+ {
74
+ auto self = thisVal.as_iterator();
75
+ size_t skip_count = 0;
76
+ if (!args.empty()) {
77
+ skip_count = static_cast<size_t>(args[0].as_double());
78
+ }
79
+ size_t skipped = 0;
80
+ while (true)
81
+ {
82
+ auto next_res = self->next();
83
+ if (next_res.done) { break; }
84
+ if (skipped < skip_count)
85
+ {
86
+ skipped++;
87
+ continue;
88
+ }
89
+ co_yield next_res.value.value_or(Constants::UNDEFINED);
90
+ }
91
+ co_return jspp::Constants::UNDEFINED; },
92
+ "drop");
93
+ return fn;
94
+ }
95
+
96
+ inline AnyValue &get_take_fn()
97
+ {
98
+ static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> args) -> JsIterator<AnyValue>
99
+ {
100
+ auto self = thisVal.as_iterator();
101
+ size_t take_count = 0;
102
+ if (!args.empty()) {
103
+ take_count = static_cast<size_t>(args[0].as_double());
104
+ }
105
+ size_t taken = 0;
106
+ while (true)
107
+ {
108
+ auto next_res = self->next();
109
+ if (next_res.done) { break; }
110
+ if (taken < take_count)
111
+ {
112
+ taken++;
113
+ co_yield next_res.value.value_or(Constants::UNDEFINED);
114
+ }
115
+ if (taken >= take_count)
116
+ {
117
+ // Call the iterator's return() method for early cleanup of resources
118
+ self->return_();
119
+ break;
120
+ }
121
+ }
122
+ co_return jspp::Constants::UNDEFINED; },
123
+ "take");
124
+ return fn;
125
+ }
126
+
127
+ inline AnyValue &get_some_fn()
128
+ {
129
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
130
+ {
131
+ auto self = thisVal.as_iterator();
132
+ if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
133
+ auto callback = args[0].as_function();
134
+ while (true)
135
+ {
136
+ auto next_res = self->next();
137
+ if (next_res.done) { break; }
138
+ if (is_truthy(callback->call(thisVal, std::span<const AnyValue>((const jspp::AnyValue[]){next_res.value.value_or(Constants::UNDEFINED)}, 1))))
139
+ {
140
+ // Call the iterator's return() method for early cleanup of resources
141
+ self->return_();
142
+ return Constants::TRUE;
143
+ }
144
+ }
145
+ return jspp::Constants::FALSE; },
146
+ "some");
45
147
  return fn;
46
148
  }
47
149
 
@@ -62,11 +164,36 @@ namespace jspp
62
164
  {
63
165
  return get_next_fn();
64
166
  }
167
+ // --- return() method ---
168
+ if (key == "return")
169
+ {
170
+ return get_return_fn();
171
+ }
172
+ // --- throw() method ---
173
+ if (key == "throw")
174
+ {
175
+ return get_throw_fn();
176
+ }
65
177
  // --- toArray() method ---
66
178
  if (key == "toArray")
67
179
  {
68
180
  return get_toArray_fn();
69
181
  }
182
+ // --- drop() method ---
183
+ if (key == "drop")
184
+ {
185
+ return get_drop_fn();
186
+ }
187
+ // --- take() method ---
188
+ if (key == "take")
189
+ {
190
+ return get_take_fn();
191
+ }
192
+ // --- some() method ---
193
+ if (key == "some")
194
+ {
195
+ return get_some_fn();
196
+ }
70
197
 
71
198
  return std::nullopt;
72
199
  }