@ugo-studio/jspp 0.2.6 → 0.2.7

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 (42) hide show
  1. package/dist/analysis/typeAnalyzer.js +56 -24
  2. package/dist/ast/symbols.js +28 -19
  3. package/dist/cli/index.js +1 -1
  4. package/dist/core/codegen/class-handlers.js +8 -8
  5. package/dist/core/codegen/control-flow-handlers.js +17 -7
  6. package/dist/core/codegen/declaration-handlers.js +21 -9
  7. package/dist/core/codegen/expression-handlers.js +558 -126
  8. package/dist/core/codegen/function-handlers.js +101 -108
  9. package/dist/core/codegen/helpers.js +28 -7
  10. package/dist/core/codegen/index.js +6 -4
  11. package/dist/core/codegen/literal-handlers.js +4 -2
  12. package/dist/core/codegen/statement-handlers.js +39 -19
  13. package/package.json +1 -1
  14. package/src/prelude/any_value.hpp +89 -59
  15. package/src/prelude/any_value_access.hpp +1 -1
  16. package/src/prelude/any_value_helpers.hpp +85 -43
  17. package/src/prelude/index.hpp +1 -0
  18. package/src/prelude/library/array.hpp +3 -2
  19. package/src/prelude/types.hpp +8 -8
  20. package/src/prelude/utils/access.hpp +62 -6
  21. package/src/prelude/utils/assignment_operators.hpp +14 -14
  22. package/src/prelude/utils/log_any_value/array.hpp +0 -15
  23. package/src/prelude/utils/log_any_value/primitives.hpp +2 -0
  24. package/src/prelude/utils/operators.hpp +117 -474
  25. package/src/prelude/utils/operators_primitive.hpp +337 -0
  26. package/src/prelude/values/helpers/array.hpp +4 -4
  27. package/src/prelude/values/helpers/async_iterator.hpp +2 -2
  28. package/src/prelude/values/helpers/function.hpp +3 -3
  29. package/src/prelude/values/helpers/iterator.hpp +2 -2
  30. package/src/prelude/values/helpers/object.hpp +3 -3
  31. package/src/prelude/values/helpers/promise.hpp +1 -1
  32. package/src/prelude/values/helpers/string.hpp +1 -1
  33. package/src/prelude/values/helpers/symbol.hpp +1 -1
  34. package/src/prelude/values/prototypes/array.hpp +1125 -853
  35. package/src/prelude/values/prototypes/async_iterator.hpp +32 -14
  36. package/src/prelude/values/prototypes/function.hpp +30 -18
  37. package/src/prelude/values/prototypes/iterator.hpp +40 -17
  38. package/src/prelude/values/prototypes/number.hpp +119 -62
  39. package/src/prelude/values/prototypes/object.hpp +10 -4
  40. package/src/prelude/values/prototypes/promise.hpp +167 -109
  41. package/src/prelude/values/prototypes/string.hpp +407 -231
  42. package/src/prelude/values/prototypes/symbol.hpp +45 -23
@@ -306,32 +306,88 @@ namespace jspp
306
306
  return Constants::TRUE;
307
307
  }
308
308
 
309
- inline AnyValue optional_get_property(const AnyValue &obj, const std::string &key)
309
+ inline AnyValue get_optional_property(const AnyValue &obj, const std::string &key)
310
310
  {
311
311
  if (obj.is_null() || obj.is_undefined())
312
312
  return Constants::UNDEFINED;
313
313
  return obj.get_own_property(key);
314
314
  }
315
315
 
316
- inline AnyValue optional_get_element(const AnyValue &obj, const AnyValue &key)
316
+ inline AnyValue get_optional_element(const AnyValue &obj, const AnyValue &key)
317
317
  {
318
318
  if (obj.is_null() || obj.is_undefined())
319
319
  return Constants::UNDEFINED;
320
320
  return obj.get_own_property(key);
321
321
  }
322
322
 
323
- inline AnyValue optional_get_element(const AnyValue &obj, const double &key)
323
+ inline AnyValue get_optional_element(const AnyValue &obj, const double &key)
324
324
  {
325
325
  if (obj.is_null() || obj.is_undefined())
326
326
  return Constants::UNDEFINED;
327
327
  return obj.get_own_property(static_cast<uint32_t>(key));
328
328
  }
329
329
 
330
- inline AnyValue optional_call(const AnyValue &fn, const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &name = std::nullopt)
330
+ inline AnyValue call_optional_property(const AnyValue &obj, const std::string &key, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt)
331
331
  {
332
- if (fn.is_null() || fn.is_undefined())
332
+ if (obj.is_null() || obj.is_undefined())
333
333
  return Constants::UNDEFINED;
334
- return fn.call(thisVal, args, name);
334
+ return obj.get_own_property(key).call(obj, args, expr);
335
+ }
336
+ inline AnyValue call_optional_property_with_optional_call(const AnyValue &obj, const std::string &key, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt)
337
+ {
338
+ if (obj.is_null() || obj.is_undefined())
339
+ return Constants::UNDEFINED;
340
+ return obj.get_own_property(key).optional_call(obj, args, expr);
341
+ }
342
+
343
+ inline void spread_array(std::vector<AnyValue> &target, const AnyValue &source)
344
+ {
345
+ if (source.is_array())
346
+ {
347
+ auto arr = source.as_array();
348
+ target.reserve(target.size() + arr->length);
349
+ for (uint64_t i = 0; i < arr->length; ++i)
350
+ {
351
+ target.push_back(arr->get_property(static_cast<uint32_t>(i)));
352
+ }
353
+ }
354
+ else if (source.is_string())
355
+ {
356
+ auto s = source.as_string();
357
+ target.reserve(target.size() + s->value.length());
358
+ for (char c : s->value)
359
+ {
360
+ target.push_back(AnyValue::make_string(std::string(1, c)));
361
+ }
362
+ }
363
+ else if (source.is_object() || source.is_function() || source.is_iterator())
364
+ {
365
+ auto iter = get_object_value_iterator(source, "spread target");
366
+ auto next_fn = iter.get_own_property("next");
367
+ while (true)
368
+ {
369
+ auto next_res = next_fn.call(iter, {});
370
+ if (is_truthy(next_res.get_own_property("done")))
371
+ break;
372
+ target.push_back(next_res.get_own_property("value"));
373
+ }
374
+ }
375
+ else
376
+ {
377
+ throw jspp::Exception::make_exception("Spread syntax requires an iterable object", "TypeError");
378
+ }
379
+ }
380
+
381
+ inline void spread_object(AnyValue &target, const AnyValue &source)
382
+ {
383
+ if (source.is_null() || source.is_undefined())
384
+ return;
385
+
386
+ auto keys = get_object_keys(source);
387
+ for (const auto &key : keys)
388
+ {
389
+ target.set_own_property(key, source.get_property_with_receiver(key, source));
390
+ }
335
391
  }
336
392
 
337
393
  }
@@ -9,56 +9,56 @@ namespace jspp {
9
9
  // --- FRIEND IMPLEMENTATIONS ---
10
10
 
11
11
  inline AnyValue &operator+=(AnyValue &lhs, const AnyValue &rhs) {
12
- lhs = lhs + rhs;
12
+ lhs = jspp::add(lhs, rhs);
13
13
  return lhs;
14
14
  }
15
15
 
16
16
  inline AnyValue &operator-=(AnyValue &lhs, const AnyValue &rhs) {
17
- lhs = lhs - rhs;
17
+ lhs = jspp::sub(lhs, rhs);
18
18
  return lhs;
19
19
  }
20
20
 
21
21
  inline AnyValue &operator*=(AnyValue &lhs, const AnyValue &rhs) {
22
- lhs = lhs * rhs;
22
+ lhs = jspp::mul(lhs, rhs);
23
23
  return lhs;
24
24
  }
25
25
 
26
26
  inline AnyValue &operator/=(AnyValue &lhs, const AnyValue &rhs) {
27
- lhs = lhs / rhs;
27
+ lhs = jspp::div(lhs, rhs);
28
28
  return lhs;
29
29
  }
30
30
 
31
31
  inline AnyValue &operator%=(AnyValue &lhs, const AnyValue &rhs) {
32
- lhs = lhs % rhs;
32
+ lhs = jspp::mod(lhs, rhs);
33
33
  return lhs;
34
34
  }
35
35
 
36
36
  inline AnyValue &operator++(AnyValue &val) {
37
- val = val + 1.0;
37
+ val = jspp::add(val, 1.0);
38
38
  return val;
39
39
  }
40
40
 
41
41
  inline AnyValue operator++(AnyValue &val, int) {
42
42
  AnyValue temp = val;
43
- val = val + 1.0;
43
+ val = jspp::add(val, 1.0);
44
44
  return temp;
45
45
  }
46
46
 
47
47
  inline AnyValue &operator--(AnyValue &val) {
48
- val = val - 1.0;
48
+ val = jspp::sub(val, 1.0);
49
49
  return val;
50
50
  }
51
51
 
52
52
  inline AnyValue operator--(AnyValue &val, int) {
53
53
  AnyValue temp = val;
54
- val = val - 1.0;
54
+ val = jspp::sub(val, 1.0);
55
55
  return temp;
56
56
  }
57
57
 
58
58
  // --- OVERLOADS FOR PRIMITIVES ---
59
59
 
60
60
  inline AnyValue &operator+=(AnyValue &lhs, const double &rhs) {
61
- lhs = lhs + rhs;
61
+ lhs = jspp::add(lhs, rhs);
62
62
  return lhs;
63
63
  }
64
64
  inline AnyValue &operator+=(AnyValue &lhs, const int &rhs) {
@@ -66,7 +66,7 @@ namespace jspp {
66
66
  }
67
67
 
68
68
  inline AnyValue &operator-=(AnyValue &lhs, const double &rhs) {
69
- lhs = lhs - rhs;
69
+ lhs = jspp::sub(lhs, rhs);
70
70
  return lhs;
71
71
  }
72
72
  inline AnyValue &operator-=(AnyValue &lhs, const int &rhs) {
@@ -74,7 +74,7 @@ namespace jspp {
74
74
  }
75
75
 
76
76
  inline AnyValue &operator*=(AnyValue &lhs, const double &rhs) {
77
- lhs = lhs * rhs;
77
+ lhs = jspp::mul(lhs, rhs);
78
78
  return lhs;
79
79
  }
80
80
  inline AnyValue &operator*=(AnyValue &lhs, const int &rhs) {
@@ -82,7 +82,7 @@ namespace jspp {
82
82
  }
83
83
 
84
84
  inline AnyValue &operator/=(AnyValue &lhs, const double &rhs) {
85
- lhs = lhs / rhs;
85
+ lhs = jspp::div(lhs, rhs);
86
86
  return lhs;
87
87
  }
88
88
  inline AnyValue &operator/=(AnyValue &lhs, const int &rhs) {
@@ -90,7 +90,7 @@ namespace jspp {
90
90
  }
91
91
 
92
92
  inline AnyValue &operator%=(AnyValue &lhs, const double &rhs) {
93
- lhs = lhs % rhs;
93
+ lhs = jspp::mod(lhs, rhs);
94
94
  return lhs;
95
95
  }
96
96
  inline AnyValue &operator%=(AnyValue &lhs, const int &rhs) {
@@ -20,21 +20,6 @@ namespace jspp
20
20
  size_t item_count = static_cast<size_t>(arr->length);
21
21
  size_t prop_count = arr->props.size();
22
22
 
23
- // // If custom toString exists on the object, prefer it
24
- // auto itToString = arr->props.find("toString");
25
- // if (depth > 0 && itToString != arr->props.end() && itToString->second.is_function())
26
- // {
27
- // try
28
- // {
29
- // auto result = itToString->second.as_function()->call(itToString->second, {});
30
- // return to_log_string(result, visited, depth);
31
- // }
32
- // catch (...)
33
- // {
34
- // // ignore and fallback to manual formatting
35
- // }
36
- // }
37
-
38
23
  std::string indent(depth * 2, ' ');
39
24
  std::string next_indent((depth + 1) * 2, ' ');
40
25
  std::stringstream ss;
@@ -14,8 +14,10 @@ namespace jspp
14
14
  inline std::optional<std::string> format_primitive(const AnyValue &val, int depth)
15
15
  {
16
16
  if (val.is_uninitialized())
17
+ {
17
18
  // THROW
18
19
  Exception::throw_uninitialized_reference("#<Object>");
20
+ }
19
21
  if (val.is_undefined())
20
22
  return Color::BRIGHT_BLACK + std::string("undefined") + Color::RESET;
21
23
  if (val.is_null())