@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.
Files changed (83) hide show
  1. package/README.md +2 -2
  2. package/dist/analysis/scope.js +33 -4
  3. package/dist/analysis/typeAnalyzer.js +260 -21
  4. package/dist/ast/symbols.js +29 -0
  5. package/dist/cli-utils/args.js +57 -0
  6. package/dist/cli-utils/colors.js +9 -0
  7. package/dist/cli-utils/file-utils.js +20 -0
  8. package/dist/cli-utils/spinner.js +55 -0
  9. package/dist/cli.js +105 -31
  10. package/dist/core/codegen/class-handlers.js +131 -0
  11. package/dist/core/codegen/control-flow-handlers.js +474 -0
  12. package/dist/core/codegen/declaration-handlers.js +36 -15
  13. package/dist/core/codegen/expression-handlers.js +579 -125
  14. package/dist/core/codegen/function-handlers.js +222 -37
  15. package/dist/core/codegen/helpers.js +158 -4
  16. package/dist/core/codegen/index.js +20 -8
  17. package/dist/core/codegen/literal-handlers.js +18 -6
  18. package/dist/core/codegen/statement-handlers.js +171 -228
  19. package/dist/core/codegen/visitor.js +31 -3
  20. package/package.json +3 -3
  21. package/src/prelude/any_value.hpp +510 -633
  22. package/src/prelude/any_value_access.hpp +151 -0
  23. package/src/prelude/any_value_defines.hpp +190 -0
  24. package/src/prelude/any_value_helpers.hpp +139 -225
  25. package/src/prelude/exception.hpp +32 -0
  26. package/src/prelude/exception_helpers.hpp +49 -0
  27. package/src/prelude/index.hpp +25 -9
  28. package/src/prelude/library/array.hpp +190 -0
  29. package/src/prelude/library/console.hpp +14 -13
  30. package/src/prelude/library/error.hpp +113 -0
  31. package/src/prelude/library/function.hpp +10 -0
  32. package/src/prelude/library/global.hpp +35 -4
  33. package/src/prelude/library/math.hpp +308 -0
  34. package/src/prelude/library/object.hpp +288 -0
  35. package/src/prelude/library/performance.hpp +2 -2
  36. package/src/prelude/library/process.hpp +39 -0
  37. package/src/prelude/library/promise.hpp +131 -0
  38. package/src/prelude/library/symbol.hpp +46 -59
  39. package/src/prelude/library/timer.hpp +92 -0
  40. package/src/prelude/scheduler.hpp +145 -0
  41. package/src/prelude/types.hpp +58 -1
  42. package/src/prelude/utils/access.hpp +345 -0
  43. package/src/prelude/utils/assignment_operators.hpp +99 -0
  44. package/src/prelude/utils/log_any_value/array.hpp +245 -0
  45. package/src/prelude/utils/log_any_value/config.hpp +32 -0
  46. package/src/prelude/utils/log_any_value/function.hpp +39 -0
  47. package/src/prelude/utils/log_any_value/fwd.hpp +15 -0
  48. package/src/prelude/utils/log_any_value/helpers.hpp +62 -0
  49. package/src/prelude/utils/log_any_value/log_any_value.hpp +94 -0
  50. package/src/prelude/utils/log_any_value/object.hpp +136 -0
  51. package/src/prelude/utils/log_any_value/primitives.hpp +43 -0
  52. package/src/prelude/utils/operators.hpp +751 -0
  53. package/src/prelude/utils/well_known_symbols.hpp +25 -0
  54. package/src/prelude/values/array.hpp +10 -7
  55. package/src/prelude/{descriptors.hpp → values/descriptors.hpp} +2 -2
  56. package/src/prelude/values/function.hpp +85 -51
  57. package/src/prelude/values/helpers/array.hpp +80 -35
  58. package/src/prelude/values/helpers/function.hpp +110 -77
  59. package/src/prelude/values/helpers/iterator.hpp +16 -10
  60. package/src/prelude/values/helpers/object.hpp +85 -10
  61. package/src/prelude/values/helpers/promise.hpp +181 -0
  62. package/src/prelude/values/helpers/string.hpp +3 -3
  63. package/src/prelude/values/helpers/symbol.hpp +2 -2
  64. package/src/prelude/values/iterator.hpp +14 -6
  65. package/src/prelude/values/object.hpp +14 -3
  66. package/src/prelude/values/promise.hpp +73 -0
  67. package/src/prelude/values/prototypes/array.hpp +855 -16
  68. package/src/prelude/values/prototypes/function.hpp +4 -4
  69. package/src/prelude/values/prototypes/iterator.hpp +11 -10
  70. package/src/prelude/values/prototypes/number.hpp +153 -0
  71. package/src/prelude/values/prototypes/object.hpp +26 -0
  72. package/src/prelude/values/prototypes/promise.hpp +134 -0
  73. package/src/prelude/values/prototypes/string.hpp +29 -29
  74. package/src/prelude/values/prototypes/symbol.hpp +22 -3
  75. package/src/prelude/values/shape.hpp +52 -0
  76. package/src/prelude/values/string.hpp +1 -1
  77. package/src/prelude/values/symbol.hpp +1 -1
  78. package/src/prelude/access.hpp +0 -91
  79. package/src/prelude/error.hpp +0 -31
  80. package/src/prelude/error_helpers.hpp +0 -59
  81. package/src/prelude/log_string.hpp +0 -407
  82. package/src/prelude/operators.hpp +0 -256
  83. package/src/prelude/well_known_symbols.hpp +0 -14
@@ -3,6 +3,7 @@
3
3
  #include "types.hpp"
4
4
  #include "values/symbol.hpp"
5
5
  #include "any_value.hpp"
6
+ #include "utils/well_known_symbols.hpp"
6
7
 
7
8
  namespace jspp
8
9
  {
@@ -11,21 +12,39 @@ namespace jspp
11
12
  inline std::optional<AnyValue> get(const std::string &key, JsSymbol *self)
12
13
  {
13
14
  // --- toString() method ---
14
- if (key == "toString" || key == WellKnownSymbols::toString->key)
15
+ if (key == "toString" || key == WellKnownSymbols::toStringTag->key)
15
16
  {
16
- return AnyValue::make_function([self](const std::vector<AnyValue> &) -> AnyValue
17
+ return AnyValue::make_function([self](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
17
18
  { return AnyValue::make_string(self->to_std_string()); },
18
19
  key);
19
20
  }
20
21
 
22
+ // --- valueOf() method ---
23
+ if (key == "valueOf")
24
+ {
25
+ return AnyValue::make_function([self](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
26
+ { return thisVal; },
27
+ key);
28
+ }
29
+
30
+ // --- [Symbol.toPrimitive] ---
31
+ if (key == WellKnownSymbols::toPrimitive->key)
32
+ {
33
+ return AnyValue::make_function([self](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
34
+ { return thisVal; },
35
+ "[Symbol.toPrimitive]");
36
+ }
37
+
21
38
  // --- description property ---
22
39
  if (key == "description")
23
40
  {
24
41
  return AnyValue::make_accessor_descriptor(
25
- [self](const std::vector<AnyValue> &) -> AnyValue
42
+ [self](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
26
43
  {
27
44
  if (self->description.empty())
45
+ {
28
46
  return AnyValue::make_undefined();
47
+ }
29
48
  return AnyValue::make_string(self->description);
30
49
  },
31
50
  std::nullopt,
@@ -0,0 +1,52 @@
1
+ #pragma once
2
+
3
+ #include <string>
4
+ #include <unordered_map>
5
+ #include <vector>
6
+ #include <memory>
7
+ #include <optional>
8
+ #include <span>
9
+
10
+ namespace jspp {
11
+
12
+ class Shape {
13
+ public:
14
+ // Map property name -> storage index
15
+ std::unordered_map<std::string, uint32_t> property_offsets;
16
+ // Map property name -> next Shape (transitions)
17
+ std::unordered_map<std::string, std::shared_ptr<Shape>> transitions;
18
+
19
+ // For fast enumeration (Object.keys)
20
+ std::vector<std::string> property_names;
21
+
22
+ // Singleton empty shape
23
+ static std::shared_ptr<Shape> empty_shape() {
24
+ static auto shape = std::make_shared<Shape>();
25
+ return shape;
26
+ }
27
+
28
+ std::optional<uint32_t> get_offset(const std::string& name) const {
29
+ auto it = property_offsets.find(name);
30
+ if (it != property_offsets.end()) return it->second;
31
+ return std::nullopt;
32
+ }
33
+
34
+ std::shared_ptr<Shape> transition(const std::string& name) {
35
+ auto it = transitions.find(name);
36
+ if (it != transitions.end()) return it->second;
37
+
38
+ // Create new shape
39
+ auto new_shape = std::make_shared<Shape>();
40
+ new_shape->property_offsets = this->property_offsets;
41
+ new_shape->property_names = this->property_names;
42
+
43
+ uint32_t new_offset = static_cast<uint32_t>(new_shape->property_offsets.size());
44
+ new_shape->property_offsets[name] = new_offset;
45
+ new_shape->property_names.push_back(name);
46
+
47
+ transitions[name] = new_shape;
48
+ return new_shape;
49
+ }
50
+ };
51
+
52
+ }
@@ -19,7 +19,7 @@ namespace jspp
19
19
  std::string to_std_string() const;
20
20
  JsIterator<AnyValue> get_iterator();
21
21
 
22
- AnyValue get_property(const std::string &key);
22
+ AnyValue get_property(const std::string &key, const AnyValue &thisVal);
23
23
  AnyValue get_property(uint32_t idx);
24
24
  };
25
25
  }
@@ -97,6 +97,6 @@ namespace jspp
97
97
 
98
98
  // --- Methods ---
99
99
  std::string to_std_string() const;
100
- AnyValue get_property(const std::string &key);
100
+ AnyValue get_property(const std::string &key, const AnyValue &thisVal);
101
101
  };
102
102
  }
@@ -1,91 +0,0 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "well_known_symbols.hpp"
5
- #include "values/function.hpp"
6
- #include "values/symbol.hpp"
7
- #include "error.hpp"
8
- #include "any_value.hpp"
9
- #include <ranges>
10
-
11
- namespace jspp
12
- {
13
- namespace Access
14
- {
15
- // Helper function to check for TDZ and deref variables
16
- inline const AnyValue &deref(const std::shared_ptr<AnyValue> &var, const std::string &name)
17
- {
18
- if ((*var).is_uninitialized()) [[unlikely]]
19
- {
20
- RuntimeError::throw_uninitialized_reference_error(name);
21
- }
22
- return *var;
23
- }
24
- inline AnyValue &deref(std::shared_ptr<AnyValue> &var, const std::string &name)
25
- {
26
- if ((*var).is_uninitialized()) [[unlikely]]
27
- {
28
- RuntimeError::throw_uninitialized_reference_error(name);
29
- }
30
- return *var;
31
- }
32
- inline const AnyValue &deref(const std::unique_ptr<AnyValue> &var, const std::string &name)
33
- {
34
- if ((*var).is_uninitialized()) [[unlikely]]
35
- {
36
- RuntimeError::throw_uninitialized_reference_error(name);
37
- }
38
- return *var;
39
- }
40
- inline AnyValue &deref(std::unique_ptr<AnyValue> &var, const std::string &name)
41
- {
42
- if ((*var).is_uninitialized()) [[unlikely]]
43
- {
44
- RuntimeError::throw_uninitialized_reference_error(name);
45
- }
46
- return *var;
47
- }
48
-
49
- inline std::vector<std::string> get_object_keys(const AnyValue &obj)
50
- {
51
- std::vector<std::string> keys;
52
-
53
- if (obj.is_object())
54
- {
55
- auto ptr = obj.as_object();
56
- for (const auto &pair : ptr->props)
57
- {
58
- if (!JsSymbol::is_internal_key(pair.first))
59
- keys.push_back(pair.first);
60
- }
61
- }
62
- if (obj.is_function())
63
- {
64
- auto ptr = obj.as_function();
65
- for (const auto &pair : ptr->props)
66
- {
67
- if (!JsSymbol::is_internal_key(pair.first))
68
- keys.push_back(pair.first);
69
- }
70
- }
71
- if (obj.is_array())
72
- {
73
- auto len = obj.as_array()->length;
74
- for (auto i = 0; i < len; ++i)
75
- {
76
- keys.push_back(std::to_string(i));
77
- }
78
- }
79
- if (obj.is_string())
80
- {
81
- auto len = obj.as_string()->value.length();
82
- for (auto i = 0; i < len; ++i)
83
- {
84
- keys.push_back(std::to_string(i));
85
- }
86
- }
87
-
88
- return keys;
89
- }
90
- }
91
- }
@@ -1,31 +0,0 @@
1
-
2
- #pragma once
3
-
4
- #include "types.hpp"
5
-
6
- namespace jspp
7
- {
8
- class AnyValue;
9
-
10
- struct RuntimeError : std::exception
11
- {
12
- std::shared_ptr<AnyValue> data;
13
-
14
- explicit RuntimeError(std::shared_ptr<AnyValue> d)
15
- : data(std::move(d)) {}
16
- explicit RuntimeError(const AnyValue &value)
17
- : data(std::make_shared<AnyValue>(value)) {}
18
- explicit RuntimeError(AnyValue &&value)
19
- : data(std::make_shared<AnyValue>(std::move(value))) {}
20
-
21
- const char *what() const noexcept override;
22
- static RuntimeError make_error(const std::string &message, const std::string &name);
23
- static AnyValue error_to_value(const std::exception &ex);
24
-
25
- // --- THROWERS
26
- static AnyValue throw_unresolved_reference_error(const std::string &var_name);
27
- static AnyValue throw_uninitialized_reference_error(const std::string &var_name);
28
- static AnyValue throw_immutable_assignment_error();
29
- static AnyValue throw_invalid_return_statement_error();
30
- };
31
- }
@@ -1,59 +0,0 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "error.hpp"
5
- #include "any_value.hpp"
6
-
7
- const char *jspp::RuntimeError::what() const noexcept
8
- {
9
- return data->to_std_string().c_str();
10
- }
11
-
12
- jspp::RuntimeError jspp::RuntimeError::make_error(const std::string &message, const std::string &name = "Error")
13
- {
14
- auto errorObj = std::make_shared<AnyValue>(AnyValue::make_object({{"message", AnyValue::make_string(message)}, {"name", AnyValue::make_string(name)}}));
15
- (*errorObj).set_own_property(WellKnownSymbols::toString->key, AnyValue::make_function([errorObj](const std::vector<AnyValue> &) -> AnyValue
16
- {
17
- AnyValue name = (*errorObj).get_own_property("name");
18
- AnyValue message = (*errorObj).get_own_property("message");
19
- std::string str = "";
20
- if (name.is_string())
21
- str = name.to_std_string();
22
- else
23
- str = "Error";
24
- str += ": ";
25
- if (message.is_string())
26
- str += message.to_std_string();
27
- return AnyValue::make_string(str); },
28
- WellKnownSymbols::toString->key));
29
- return RuntimeError(errorObj);
30
- }
31
- jspp::AnyValue jspp::RuntimeError::error_to_value(const std::exception &ex)
32
- {
33
- if (const jspp::RuntimeError *err = dynamic_cast<const jspp::RuntimeError *>(&ex))
34
- {
35
- return (*err->data);
36
- }
37
- else
38
- {
39
- return AnyValue::make_string(ex.what());
40
- }
41
- }
42
-
43
- // --- THROWERS
44
- jspp::AnyValue jspp::RuntimeError::throw_unresolved_reference_error(const std::string &var_name)
45
- {
46
- throw RuntimeError::make_error(var_name + " is not defined", "ReferenceError");
47
- }
48
- jspp::AnyValue jspp::RuntimeError::throw_uninitialized_reference_error(const std::string &var_name)
49
- {
50
- throw RuntimeError::make_error("Cannot access '" + var_name + "' before initialization", "ReferenceError");
51
- }
52
- jspp::AnyValue jspp::RuntimeError::throw_immutable_assignment_error()
53
- {
54
- throw RuntimeError::make_error("Assignment to constant variable.", "TypeError");
55
- }
56
- jspp::AnyValue jspp::RuntimeError::throw_invalid_return_statement_error()
57
- {
58
- throw RuntimeError::make_error("Return statements are only valid inside functions.", "SyntaxError");
59
- }