@ugo-studio/jspp 0.2.8 → 0.3.0

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 (46) hide show
  1. package/dist/analysis/typeAnalyzer.js +42 -27
  2. package/dist/core/codegen/class-handlers.js +6 -6
  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 +43 -23
  11. package/package.json +1 -1
  12. package/scripts/precompile-headers.ts +13 -5
  13. package/src/prelude/any_value.hpp +362 -361
  14. package/src/prelude/any_value_access.hpp +170 -170
  15. package/src/prelude/any_value_defines.hpp +189 -189
  16. package/src/prelude/any_value_helpers.hpp +374 -365
  17. package/src/prelude/library/array.hpp +185 -185
  18. package/src/prelude/library/console.hpp +111 -111
  19. package/src/prelude/library/error.hpp +112 -112
  20. package/src/prelude/library/function.hpp +10 -10
  21. package/src/prelude/library/math.hpp +307 -307
  22. package/src/prelude/library/object.hpp +275 -275
  23. package/src/prelude/library/performance.hpp +1 -1
  24. package/src/prelude/library/process.hpp +39 -39
  25. package/src/prelude/library/promise.hpp +123 -123
  26. package/src/prelude/library/symbol.hpp +52 -52
  27. package/src/prelude/library/timer.hpp +91 -91
  28. package/src/prelude/types.hpp +178 -178
  29. package/src/prelude/utils/access.hpp +411 -393
  30. package/src/prelude/utils/operators.hpp +336 -329
  31. package/src/prelude/values/array.hpp +0 -1
  32. package/src/prelude/values/async_iterator.hpp +83 -81
  33. package/src/prelude/values/function.hpp +82 -82
  34. package/src/prelude/values/helpers/array.hpp +198 -208
  35. package/src/prelude/values/helpers/async_iterator.hpp +275 -271
  36. package/src/prelude/values/helpers/function.hpp +108 -108
  37. package/src/prelude/values/helpers/iterator.hpp +144 -107
  38. package/src/prelude/values/helpers/promise.hpp +253 -253
  39. package/src/prelude/values/helpers/string.hpp +37 -47
  40. package/src/prelude/values/iterator.hpp +32 -5
  41. package/src/prelude/values/promise.hpp +72 -72
  42. package/src/prelude/values/prototypes/array.hpp +54 -42
  43. package/src/prelude/values/prototypes/iterator.hpp +201 -74
  44. package/src/prelude/values/prototypes/promise.hpp +196 -196
  45. package/src/prelude/values/prototypes/string.hpp +564 -542
  46. package/src/prelude/values/string.hpp +25 -26
@@ -1,209 +1,199 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/array.hpp"
5
- #include "exception.hpp"
6
- #include "any_value.hpp"
7
- #include "values/prototypes/array.hpp"
8
-
9
- inline jspp::JsArray::JsArray() : proto(Constants::Null), length(0) {}
10
- inline jspp::JsArray::JsArray(const std::vector<jspp::AnyValue> &items) : dense(items), proto(Constants::Null), length(items.size()) {}
11
- inline jspp::JsArray::JsArray(std::vector<jspp::AnyValue> &&items) : dense(std::move(items)), proto(Constants::Null), length(dense.size()) {}
12
-
13
- inline std::string jspp::JsArray::to_std_string() const
14
- {
15
- if (length == 0)
16
- {
17
- return "";
18
- }
19
-
20
- std::string result = "";
21
- for (uint64_t i = 0; i < length; ++i)
22
- {
23
- AnyValue itemVal = Constants::UNINITIALIZED;
24
- if (i < dense.size())
25
- {
26
- itemVal = dense[i];
27
- }
28
- else
29
- {
30
- auto it = sparse.find(static_cast<uint32_t>(i));
31
- if (it != sparse.end())
32
- {
33
- itemVal = it->second;
34
- }
35
- }
36
-
37
- if (!itemVal.is_uninitialized())
38
- {
39
- if (!itemVal.is_undefined() && !itemVal.is_null())
40
- {
41
- result += itemVal.to_std_string();
42
- }
43
- }
44
-
45
- if (i < length - 1)
46
- {
47
- result += ",";
48
- }
49
- }
50
- return result;
51
- }
52
-
53
- inline jspp::JsIterator<jspp::AnyValue> jspp::JsArray::get_iterator()
54
- {
55
- for (uint64_t idx = 0; idx < length; ++idx)
56
- {
57
- co_yield get_property(static_cast<uint32_t>(idx));
58
- }
59
-
60
- co_return AnyValue::make_undefined();
61
- }
62
-
63
- inline bool jspp::JsArray::has_property(const std::string &key) const
64
- {
65
- if (key == "length")
66
- return true;
67
- if (is_array_index(key))
68
- {
69
- uint32_t idx = static_cast<uint32_t>(std::stoull(key));
70
- if (idx < dense.size())
71
- return true;
72
- if (sparse.find(idx) != sparse.end())
73
- return true;
74
- }
75
- if (props.find(key) != props.end())
76
- return true;
77
-
78
- if (!proto.is_null() && !proto.is_undefined())
79
- {
80
- if (proto.has_property(key))
81
- return true;
82
- }
83
-
84
- if (ArrayPrototypes::get(key).has_value())
85
- return true;
86
- return false;
87
- }
88
-
89
- inline jspp::AnyValue jspp::JsArray::get_property(const std::string &key, const AnyValue &thisVal)
90
- {
91
- if (
92
- !key.empty() && std::isdigit(static_cast<unsigned char>(key[0]))
93
- && is_array_index(key))
94
- {
95
- uint32_t idx = static_cast<uint32_t>(std::stoull(key));
96
- return get_property(idx);
97
- }
98
- else
99
- {
100
- auto it = props.find(key);
101
- if (it == props.end())
102
- {
103
- if (key == "length")
104
- {
105
- auto proto_it = ArrayPrototypes::get(key);
106
- if (proto_it.has_value())
107
- {
108
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
109
- }
110
- }
111
-
112
- if (!proto.is_null() && !proto.is_undefined())
113
- {
114
- if (proto.has_property(key))
115
- {
116
- return proto.get_property_with_receiver(key, thisVal);
117
- }
118
- }
119
-
120
- auto proto_it = ArrayPrototypes::get(key);
121
- if (proto_it.has_value())
122
- {
123
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
124
- }
125
- return Constants::UNDEFINED;
126
- }
127
- return AnyValue::resolve_property_for_read(it->second, thisVal, key);
128
- }
129
- }
130
-
131
- inline jspp::AnyValue jspp::JsArray::get_property(uint32_t idx)
132
- {
133
- if (idx < dense.size())
134
- {
135
- const auto &val = dense[idx];
136
- return val.is_uninitialized() ? Constants::UNDEFINED : val;
137
- }
138
- const auto &it = sparse.find(idx);
139
- if (it != sparse.end())
140
- {
141
- const auto &val = it->second;
142
- return val.is_uninitialized() ? Constants::UNDEFINED : val;
143
- }
144
- return Constants::UNDEFINED;
145
- }
146
-
147
- inline jspp::AnyValue jspp::JsArray::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
148
- {
149
- if (
150
- !key.empty() && std::isdigit(static_cast<unsigned char>(key[0]))
151
- && is_array_index(key))
152
- {
153
- uint32_t idx = static_cast<uint32_t>(std::stoull(key));
154
- return set_property(idx, value);
155
- }
156
- else
157
- {
158
- auto proto_val_opt = ArrayPrototypes::get(key);
159
-
160
- if (proto_val_opt.has_value())
161
- {
162
- auto proto_value = proto_val_opt.value();
163
- if (proto_value.is_accessor_descriptor())
164
- {
165
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
166
- }
167
- if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
168
- {
169
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
170
- }
171
- }
172
-
173
- auto it = props.find(key);
174
- if (it != props.end())
175
- {
176
- return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
177
- }
178
- else
179
- {
180
- props[key] = value;
181
- return value;
182
- }
183
- }
184
- }
185
-
186
- inline jspp::AnyValue jspp::JsArray::set_property(uint32_t idx, const AnyValue &value)
187
- {
188
- uint64_t newLen = static_cast<uint64_t>(idx) + 1;
189
- if (newLen > length)
190
- length = newLen;
191
-
192
- const uint32_t DENSE_GROW_THRESHOLD = 1024;
193
- if (idx < dense.size())
194
- {
195
- dense[idx] = value;
196
- return value;
197
- }
198
- else if (idx <= dense.size() + DENSE_GROW_THRESHOLD)
199
- {
200
- dense.resize(idx + 1, Constants::UNINITIALIZED);
201
- dense[idx] = value;
202
- return value;
203
- }
204
- else
205
- {
206
- sparse[idx] = value;
207
- return value;
208
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "values/array.hpp"
5
+ #include "exception.hpp"
6
+ #include "any_value.hpp"
7
+ #include "values/prototypes/array.hpp"
8
+
9
+ inline jspp::JsArray::JsArray() : proto(Constants::Null), length(0) {}
10
+ inline jspp::JsArray::JsArray(const std::vector<jspp::AnyValue> &items) : dense(items), proto(Constants::Null), length(items.size()) {}
11
+ inline jspp::JsArray::JsArray(std::vector<jspp::AnyValue> &&items) : dense(std::move(items)), proto(Constants::Null), length(dense.size()) {}
12
+
13
+ inline std::string jspp::JsArray::to_std_string() const
14
+ {
15
+ if (length == 0)
16
+ {
17
+ return "";
18
+ }
19
+
20
+ std::string result = "";
21
+ for (uint64_t i = 0; i < length; ++i)
22
+ {
23
+ AnyValue itemVal = Constants::UNINITIALIZED;
24
+ if (i < dense.size())
25
+ {
26
+ itemVal = dense[i];
27
+ }
28
+ else
29
+ {
30
+ auto it = sparse.find(static_cast<uint32_t>(i));
31
+ if (it != sparse.end())
32
+ {
33
+ itemVal = it->second;
34
+ }
35
+ }
36
+
37
+ if (!itemVal.is_uninitialized())
38
+ {
39
+ if (!itemVal.is_undefined() && !itemVal.is_null())
40
+ {
41
+ result += itemVal.to_std_string();
42
+ }
43
+ }
44
+
45
+ if (i < length - 1)
46
+ {
47
+ result += ",";
48
+ }
49
+ }
50
+ return result;
51
+ }
52
+
53
+ inline bool jspp::JsArray::has_property(const std::string &key) const
54
+ {
55
+ if (key == "length")
56
+ return true;
57
+ if (is_array_index(key))
58
+ {
59
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key));
60
+ if (idx < dense.size())
61
+ return !dense[idx].is_uninitialized();
62
+ if (sparse.find(idx) != sparse.end())
63
+ return !sparse.at(idx).is_uninitialized();
64
+ }
65
+ if (props.find(key) != props.end())
66
+ return true;
67
+
68
+ if (!proto.is_null() && !proto.is_undefined())
69
+ {
70
+ if (proto.has_property(key))
71
+ return true;
72
+ }
73
+
74
+ if (ArrayPrototypes::get(key).has_value())
75
+ return true;
76
+ return false;
77
+ }
78
+
79
+ inline jspp::AnyValue jspp::JsArray::get_property(const std::string &key, const AnyValue &thisVal)
80
+ {
81
+ if (
82
+ !key.empty() && std::isdigit(static_cast<unsigned char>(key[0]))
83
+ && is_array_index(key))
84
+ {
85
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key));
86
+ return get_property(idx);
87
+ }
88
+ else
89
+ {
90
+ auto it = props.find(key);
91
+ if (it == props.end())
92
+ {
93
+ if (key == "length")
94
+ {
95
+ auto proto_it = ArrayPrototypes::get(key);
96
+ if (proto_it.has_value())
97
+ {
98
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
99
+ }
100
+ }
101
+
102
+ if (!proto.is_null() && !proto.is_undefined())
103
+ {
104
+ if (proto.has_property(key))
105
+ {
106
+ return proto.get_property_with_receiver(key, thisVal);
107
+ }
108
+ }
109
+
110
+ auto proto_it = ArrayPrototypes::get(key);
111
+ if (proto_it.has_value())
112
+ {
113
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
114
+ }
115
+ return Constants::UNDEFINED;
116
+ }
117
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key);
118
+ }
119
+ }
120
+
121
+ inline jspp::AnyValue jspp::JsArray::get_property(uint32_t idx)
122
+ {
123
+ if (idx < dense.size())
124
+ {
125
+ const auto &val = dense[idx];
126
+ return val.is_uninitialized() ? Constants::UNDEFINED : val;
127
+ }
128
+ const auto &it = sparse.find(idx);
129
+ if (it != sparse.end())
130
+ {
131
+ const auto &val = it->second;
132
+ return val.is_uninitialized() ? Constants::UNDEFINED : val;
133
+ }
134
+ return Constants::UNDEFINED;
135
+ }
136
+
137
+ inline jspp::AnyValue jspp::JsArray::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
138
+ {
139
+ if (
140
+ !key.empty() && std::isdigit(static_cast<unsigned char>(key[0]))
141
+ && is_array_index(key))
142
+ {
143
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key));
144
+ return set_property(idx, value);
145
+ }
146
+ else
147
+ {
148
+ auto proto_val_opt = ArrayPrototypes::get(key);
149
+
150
+ if (proto_val_opt.has_value())
151
+ {
152
+ auto proto_value = proto_val_opt.value();
153
+ if (proto_value.is_accessor_descriptor())
154
+ {
155
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
156
+ }
157
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
158
+ {
159
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
160
+ }
161
+ }
162
+
163
+ auto it = props.find(key);
164
+ if (it != props.end())
165
+ {
166
+ return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
167
+ }
168
+ else
169
+ {
170
+ props[key] = value;
171
+ return value;
172
+ }
173
+ }
174
+ }
175
+
176
+ inline jspp::AnyValue jspp::JsArray::set_property(uint32_t idx, const AnyValue &value)
177
+ {
178
+ uint64_t newLen = static_cast<uint64_t>(idx) + 1;
179
+ if (newLen > length)
180
+ length = newLen;
181
+
182
+ const uint32_t DENSE_GROW_THRESHOLD = 1024;
183
+ if (idx < dense.size())
184
+ {
185
+ dense[idx] = value;
186
+ return value;
187
+ }
188
+ else if (idx <= dense.size() + DENSE_GROW_THRESHOLD)
189
+ {
190
+ dense.resize(idx + 1, Constants::UNINITIALIZED);
191
+ dense[idx] = value;
192
+ return value;
193
+ }
194
+ else
195
+ {
196
+ sparse[idx] = value;
197
+ return value;
198
+ }
209
199
  }