@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
@@ -0,0 +1,151 @@
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "any_value.hpp"
5
+
6
+ jspp::AnyValue jspp::AnyValue::get_own_property(const std::string &key) const
7
+ {
8
+ return get_property_with_receiver(key, *this);
9
+ }
10
+ bool jspp::AnyValue::has_property(const std::string &key) const
11
+ {
12
+ switch (get_type())
13
+ {
14
+ case JsType::Object:
15
+ return std::get<std::shared_ptr<JsObject>>(storage)->has_property(key);
16
+ case JsType::Array:
17
+ return std::get<std::shared_ptr<JsArray>>(storage)->has_property(key);
18
+ case JsType::Function:
19
+ return std::get<std::shared_ptr<JsFunction>>(storage)->has_property(key);
20
+ case JsType::Promise:
21
+ // Promises don't have their own props usually, but could.
22
+ return false;
23
+ case JsType::Iterator:
24
+ return false;
25
+ case JsType::Symbol:
26
+ return false;
27
+ case JsType::String:
28
+ if (key == "length")
29
+ return true;
30
+ if (JsArray::is_array_index(key))
31
+ {
32
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key));
33
+ return idx < std::get<std::shared_ptr<JsString>>(storage)->value.length();
34
+ }
35
+ return false;
36
+ case JsType::Number:
37
+ return false;
38
+ case JsType::Uninitialized:
39
+ Exception::throw_uninitialized_reference("#<Object>");
40
+ return false;
41
+ default:
42
+ return false;
43
+ }
44
+ }
45
+ jspp::AnyValue jspp::AnyValue::get_own_property(uint32_t idx) const
46
+ {
47
+ switch (storage.index())
48
+ {
49
+ case 7: // Array
50
+ return std::get<std::shared_ptr<JsArray>>(storage)->get_property(idx);
51
+ case 5: // String
52
+ return std::get<std::shared_ptr<JsString>>(storage)->get_property(idx);
53
+ case 4: // Number
54
+ return get_own_property(std::to_string(idx));
55
+ default:
56
+ return get_own_property(std::to_string(idx));
57
+ }
58
+ }
59
+ jspp::AnyValue jspp::AnyValue::get_own_property(const AnyValue &key) const
60
+ {
61
+ if (key.is_number() && is_array())
62
+ return std::get<std::shared_ptr<JsArray>>(storage)->get_property(key.as_double());
63
+ if (key.is_number() && is_string())
64
+ return std::get<std::shared_ptr<JsString>>(storage)->get_property(key.as_double());
65
+
66
+ // If the key is a Symbol, use its internal key string
67
+ if (key.is_symbol())
68
+ return get_own_property(key.as_symbol()->key);
69
+
70
+ return get_own_property(key.to_std_string());
71
+ }
72
+
73
+ jspp::AnyValue jspp::AnyValue::get_property_with_receiver(const std::string &key, const AnyValue &receiver) const
74
+ {
75
+ switch (get_type())
76
+ {
77
+ case JsType::Object:
78
+ return std::get<std::shared_ptr<JsObject>>(storage)->get_property(key, receiver);
79
+ case JsType::Array:
80
+ return std::get<std::shared_ptr<JsArray>>(storage)->get_property(key, receiver);
81
+ case JsType::Function:
82
+ return std::get<std::shared_ptr<JsFunction>>(storage)->get_property(key, receiver);
83
+ case JsType::Promise:
84
+ return std::get<std::shared_ptr<JsPromise>>(storage)->get_property(key, receiver);
85
+ case JsType::Iterator:
86
+ return std::get<std::shared_ptr<JsIterator<AnyValue>>>(storage)->get_property(key, receiver);
87
+ case JsType::Symbol:
88
+ return std::get<std::shared_ptr<JsSymbol>>(storage)->get_property(key, receiver);
89
+ case JsType::String:
90
+ return std::get<std::shared_ptr<JsString>>(storage)->get_property(key, receiver);
91
+ case JsType::Number:
92
+ {
93
+ auto proto_it = NumberPrototypes::get(key, std::get<double>(storage));
94
+ if (proto_it.has_value())
95
+ {
96
+ return AnyValue::resolve_property_for_read(proto_it.value(), receiver, key);
97
+ }
98
+ return AnyValue::make_undefined();
99
+ }
100
+ case JsType::Undefined:
101
+ throw Exception::make_exception("Cannot read properties of undefined (reading '" + key + "')", "TypeError");
102
+ case JsType::Null:
103
+ throw Exception::make_exception("Cannot read properties of null (reading '" + key + "')", "TypeError");
104
+ case JsType::Uninitialized:
105
+ Exception::throw_uninitialized_reference("#<Object>");
106
+ default:
107
+ return AnyValue::make_undefined();
108
+ }
109
+ }
110
+
111
+ jspp::AnyValue jspp::AnyValue::set_own_property(const std::string &key, const AnyValue &value) const
112
+ {
113
+ switch (get_type())
114
+ {
115
+ case JsType::Object:
116
+ return std::get<std::shared_ptr<JsObject>>(storage)->set_property(key, value, *this);
117
+ case JsType::Array:
118
+ return std::get<std::shared_ptr<JsArray>>(storage)->set_property(key, value, *this);
119
+ case JsType::Function:
120
+ return std::get<std::shared_ptr<JsFunction>>(storage)->set_property(key, value, *this);
121
+ case JsType::Promise:
122
+ return std::get<std::shared_ptr<JsPromise>>(storage)->set_property(key, value, *this);
123
+ case JsType::Undefined:
124
+ throw Exception::make_exception("Cannot set properties of undefined (setting '" + key + "')", "TypeError");
125
+ case JsType::Null:
126
+ throw Exception::make_exception("Cannot set properties of null (setting '" + key + "')", "TypeError");
127
+ default:
128
+ return value;
129
+ }
130
+ }
131
+ jspp::AnyValue jspp::AnyValue::set_own_property(uint32_t idx, const AnyValue &value) const
132
+ {
133
+ if (is_array())
134
+ {
135
+ return std::get<std::shared_ptr<JsArray>>(storage)->set_property(idx, value);
136
+ }
137
+ return set_own_property(std::to_string(idx), value);
138
+ }
139
+ jspp::AnyValue jspp::AnyValue::set_own_property(const AnyValue &key, const AnyValue &value) const
140
+ {
141
+ if (key.is_number() && is_array())
142
+ {
143
+ return std::get<std::shared_ptr<JsArray>>(storage)->set_property(key.as_double(), value);
144
+ }
145
+
146
+ // If the key is a Symbol, use its internal key string
147
+ if (key.is_symbol())
148
+ return set_own_property(key.as_symbol()->key, value);
149
+
150
+ return set_own_property(key.to_std_string(), value);
151
+ }
@@ -0,0 +1,190 @@
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "any_value.hpp"
5
+ #include "values/object.hpp"
6
+ #include "values/function.hpp"
7
+
8
+ namespace jspp
9
+ {
10
+ inline void AnyValue::define_data_property(const std::string &key, const AnyValue &value)
11
+ {
12
+ if (is_object())
13
+ {
14
+ auto obj = std::get<std::shared_ptr<JsObject>>(storage);
15
+ auto offset = obj->shape->get_offset(key);
16
+ if (offset.has_value())
17
+ {
18
+ obj->storage[offset.value()] = value;
19
+ }
20
+ else
21
+ {
22
+ obj->shape = obj->shape->transition(key);
23
+ obj->storage.push_back(value);
24
+ }
25
+ }
26
+ else if (is_function())
27
+ {
28
+ std::get<std::shared_ptr<JsFunction>>(storage)->props[key] = value;
29
+ }
30
+ }
31
+
32
+ inline void AnyValue::define_data_property(const AnyValue &key, const AnyValue &value)
33
+ {
34
+ if (key.is_symbol())
35
+ define_data_property(key.as_symbol()->key, value);
36
+ else
37
+ define_data_property(key.to_std_string(), value);
38
+ }
39
+
40
+ inline void AnyValue::define_data_property(const std::string &key, const AnyValue &value, bool writable, bool enumerable, bool configurable)
41
+ {
42
+ define_data_property(key, AnyValue::make_data_descriptor(value, writable, enumerable, configurable));
43
+ }
44
+
45
+ inline void AnyValue::define_getter(const std::string &key, const AnyValue &getter)
46
+ {
47
+ if (is_object())
48
+ {
49
+ auto obj = std::get<std::shared_ptr<JsObject>>(storage);
50
+ auto offset = obj->shape->get_offset(key);
51
+
52
+ if (offset.has_value())
53
+ {
54
+ auto &val = obj->storage[offset.value()];
55
+ if (val.is_accessor_descriptor())
56
+ {
57
+ auto desc = val.as_accessor_descriptor();
58
+ desc->get = [getter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
59
+ {
60
+ return getter.call(thisVal, args);
61
+ };
62
+ }
63
+ else
64
+ {
65
+ auto getFunc = [getter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
66
+ {
67
+ return getter.call(thisVal, args);
68
+ };
69
+ obj->storage[offset.value()] = AnyValue::make_accessor_descriptor(getFunc, std::nullopt, true, true);
70
+ }
71
+ }
72
+ else
73
+ {
74
+ auto getFunc = [getter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
75
+ {
76
+ return getter.call(thisVal, args);
77
+ };
78
+ obj->shape = obj->shape->transition(key);
79
+ obj->storage.push_back(AnyValue::make_accessor_descriptor(getFunc, std::nullopt, true, true));
80
+ }
81
+ }
82
+ else if (is_function())
83
+ {
84
+ auto &props = std::get<std::shared_ptr<JsFunction>>(storage)->props;
85
+ auto it = props.find(key);
86
+ if (it != props.end() && it->second.is_accessor_descriptor())
87
+ {
88
+ auto desc = it->second.as_accessor_descriptor();
89
+ desc->get = [getter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
90
+ {
91
+ return getter.call(thisVal, args);
92
+ };
93
+ }
94
+ else
95
+ {
96
+ auto getFunc = [getter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
97
+ {
98
+ return getter.call(thisVal, args);
99
+ };
100
+ props[key] = AnyValue::make_accessor_descriptor(getFunc, std::nullopt, true, true);
101
+ }
102
+ }
103
+ }
104
+
105
+ inline void AnyValue::define_getter(const AnyValue &key, const AnyValue &getter)
106
+ {
107
+ if (key.is_symbol())
108
+ define_getter(key.as_symbol()->key, getter);
109
+ else
110
+ define_getter(key.to_std_string(), getter);
111
+ }
112
+
113
+ inline void AnyValue::define_setter(const std::string &key, const AnyValue &setter)
114
+ {
115
+ if (is_object())
116
+ {
117
+ auto obj = std::get<std::shared_ptr<JsObject>>(storage);
118
+ auto offset = obj->shape->get_offset(key);
119
+
120
+ if (offset.has_value())
121
+ {
122
+ auto &val = obj->storage[offset.value()];
123
+ if (val.is_accessor_descriptor())
124
+ {
125
+ auto desc = val.as_accessor_descriptor();
126
+ desc->set = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
127
+ {
128
+ if (args.empty())
129
+ return AnyValue::make_undefined();
130
+ return setter.call(thisVal, args);
131
+ };
132
+ }
133
+ else
134
+ {
135
+ auto setFunc = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
136
+ {
137
+ if (args.empty())
138
+ return AnyValue::make_undefined();
139
+ return setter.call(thisVal, args);
140
+ };
141
+ obj->storage[offset.value()] = AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true);
142
+ }
143
+ }
144
+ else
145
+ {
146
+ auto setFunc = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
147
+ {
148
+ if (args.empty())
149
+ return AnyValue::make_undefined();
150
+ return setter.call(thisVal, args);
151
+ };
152
+ obj->shape = obj->shape->transition(key);
153
+ obj->storage.push_back(AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true));
154
+ }
155
+ }
156
+ else if (is_function())
157
+ {
158
+ auto &props = std::get<std::shared_ptr<JsFunction>>(storage)->props;
159
+ auto it = props.find(key);
160
+ if (it != props.end() && it->second.is_accessor_descriptor())
161
+ {
162
+ auto desc = it->second.as_accessor_descriptor();
163
+ desc->set = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
164
+ {
165
+ if (args.empty())
166
+ return AnyValue::make_undefined();
167
+ return setter.call(thisVal, args);
168
+ };
169
+ }
170
+ else
171
+ {
172
+ auto setFunc = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
173
+ {
174
+ if (args.empty())
175
+ return AnyValue::make_undefined();
176
+ return setter.call(thisVal, args);
177
+ };
178
+ props[key] = AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true);
179
+ }
180
+ }
181
+ }
182
+
183
+ inline void AnyValue::define_setter(const AnyValue &key, const AnyValue &setter)
184
+ {
185
+ if (key.is_symbol())
186
+ define_setter(key.as_symbol()->key, setter);
187
+ else
188
+ define_setter(key.to_std_string(), setter);
189
+ }
190
+ }