@ugo-studio/jspp 0.2.9 → 0.3.1

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 (97) hide show
  1. package/LICENSE +25 -25
  2. package/README.md +20 -12
  3. package/dist/analysis/scope.js +5 -3
  4. package/dist/analysis/typeAnalyzer.js +21 -25
  5. package/dist/cli/index.js +14 -4
  6. package/dist/cli/utils.js +61 -0
  7. package/dist/core/codegen/class-handlers.js +6 -6
  8. package/dist/core/codegen/control-flow-handlers.js +10 -9
  9. package/dist/core/codegen/declaration-handlers.js +10 -3
  10. package/dist/core/codegen/destructuring-handlers.js +9 -4
  11. package/dist/core/codegen/expression-handlers.js +40 -29
  12. package/dist/core/codegen/function-handlers.js +78 -12
  13. package/dist/core/codegen/helpers.js +91 -14
  14. package/dist/core/codegen/index.js +4 -2
  15. package/dist/core/codegen/statement-handlers.js +9 -7
  16. package/package.json +2 -2
  17. package/scripts/precompile-headers.ts +249 -50
  18. package/scripts/setup-compiler.ts +63 -63
  19. package/src/prelude/any_value.cpp +636 -0
  20. package/src/prelude/any_value.hpp +369 -362
  21. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  22. package/src/prelude/exception.hpp +27 -27
  23. package/src/prelude/iterator_instantiations.hpp +10 -0
  24. package/src/prelude/{index.hpp → jspp.hpp} +10 -16
  25. package/src/prelude/library/array.cpp +191 -0
  26. package/src/prelude/library/array.hpp +13 -186
  27. package/src/prelude/library/console.cpp +125 -0
  28. package/src/prelude/library/console.hpp +24 -112
  29. package/src/prelude/library/error.cpp +100 -0
  30. package/src/prelude/library/error.hpp +13 -113
  31. package/src/prelude/library/function.cpp +69 -0
  32. package/src/prelude/library/function.hpp +11 -10
  33. package/src/prelude/library/global.cpp +96 -0
  34. package/src/prelude/library/global.hpp +12 -28
  35. package/src/prelude/library/global_usings.hpp +15 -0
  36. package/src/prelude/library/math.cpp +258 -0
  37. package/src/prelude/library/math.hpp +26 -308
  38. package/src/prelude/library/object.cpp +379 -0
  39. package/src/prelude/library/object.hpp +14 -276
  40. package/src/prelude/library/performance.cpp +21 -0
  41. package/src/prelude/library/performance.hpp +5 -20
  42. package/src/prelude/library/process.cpp +38 -0
  43. package/src/prelude/library/process.hpp +11 -39
  44. package/src/prelude/library/promise.cpp +131 -0
  45. package/src/prelude/library/promise.hpp +12 -123
  46. package/src/prelude/library/symbol.cpp +56 -0
  47. package/src/prelude/library/symbol.hpp +11 -52
  48. package/src/prelude/library/timer.cpp +88 -0
  49. package/src/prelude/library/timer.hpp +16 -92
  50. package/src/prelude/runtime.cpp +19 -0
  51. package/src/prelude/types.hpp +184 -179
  52. package/src/prelude/utils/access.hpp +502 -411
  53. package/src/prelude/utils/assignment_operators.hpp +99 -99
  54. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  55. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  56. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  57. package/src/prelude/utils/operators.hpp +351 -336
  58. package/src/prelude/utils/operators_primitive.hpp +336 -336
  59. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  60. package/src/prelude/values/array.cpp +1399 -0
  61. package/src/prelude/values/array.hpp +4 -1
  62. package/src/prelude/values/async_iterator.cpp +251 -0
  63. package/src/prelude/values/async_iterator.hpp +111 -83
  64. package/src/prelude/values/function.cpp +262 -0
  65. package/src/prelude/values/function.hpp +62 -82
  66. package/src/prelude/values/iterator.cpp +309 -0
  67. package/src/prelude/values/iterator.hpp +33 -64
  68. package/src/prelude/values/number.cpp +176 -0
  69. package/src/prelude/values/object.cpp +159 -0
  70. package/src/prelude/values/object.hpp +4 -0
  71. package/src/prelude/values/promise.cpp +479 -0
  72. package/src/prelude/values/promise.hpp +79 -72
  73. package/src/prelude/values/prototypes/array.hpp +46 -1336
  74. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  75. package/src/prelude/values/prototypes/function.hpp +7 -46
  76. package/src/prelude/values/prototypes/iterator.hpp +25 -201
  77. package/src/prelude/values/prototypes/number.hpp +23 -210
  78. package/src/prelude/values/prototypes/object.hpp +7 -23
  79. package/src/prelude/values/prototypes/promise.hpp +18 -196
  80. package/src/prelude/values/prototypes/string.hpp +39 -542
  81. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  82. package/src/prelude/values/shape.hpp +52 -52
  83. package/src/prelude/values/string.cpp +485 -0
  84. package/src/prelude/values/string.hpp +25 -26
  85. package/src/prelude/values/symbol.cpp +89 -0
  86. package/src/prelude/values/symbol.hpp +101 -101
  87. package/src/prelude/any_value_access.hpp +0 -170
  88. package/src/prelude/any_value_defines.hpp +0 -190
  89. package/src/prelude/any_value_helpers.hpp +0 -374
  90. package/src/prelude/values/helpers/array.hpp +0 -209
  91. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  92. package/src/prelude/values/helpers/function.hpp +0 -109
  93. package/src/prelude/values/helpers/iterator.hpp +0 -145
  94. package/src/prelude/values/helpers/object.hpp +0 -104
  95. package/src/prelude/values/helpers/promise.hpp +0 -254
  96. package/src/prelude/values/helpers/string.hpp +0 -61
  97. package/src/prelude/values/helpers/symbol.hpp +0 -21
@@ -0,0 +1,262 @@
1
+ #include "jspp.hpp"
2
+ #include "values/function.hpp"
3
+ #include "values/prototypes/function.hpp"
4
+
5
+ namespace jspp {
6
+
7
+ // --- JsFunction Implementation ---
8
+
9
+ JsFunction::JsFunction(const JsFunctionCallable &c,
10
+ std::optional<std::string> n,
11
+ std::unordered_map<std::string, AnyValue> p,
12
+ std::map<AnyValue, AnyValue> sp,
13
+ bool is_cls,
14
+ bool is_ctor)
15
+ : callable(c),
16
+ name(std::move(n)),
17
+ props(std::move(p)),
18
+ symbol_props(std::move(sp)),
19
+ proto(Constants::Null),
20
+ is_generator(callable.index() == 1),
21
+ is_async(callable.index() == 2),
22
+ is_class(is_cls),
23
+ is_constructor(is_ctor && !is_generator && !is_async)
24
+ {
25
+ }
26
+
27
+ JsFunction::JsFunction(const JsFunctionCallable &c,
28
+ bool is_gen,
29
+ std::optional<std::string> n,
30
+ std::unordered_map<std::string, AnyValue> p,
31
+ std::map<AnyValue, AnyValue> sp,
32
+ bool is_cls,
33
+ bool is_ctor)
34
+ : callable(c),
35
+ name(std::move(n)),
36
+ props(std::move(p)),
37
+ symbol_props(std::move(sp)),
38
+ proto(Constants::Null),
39
+ is_generator(is_gen),
40
+ is_async(callable.index() == 2),
41
+ is_class(is_cls),
42
+ is_constructor(is_ctor && !is_gen && !is_async)
43
+ {
44
+ }
45
+
46
+ JsFunction::JsFunction(const JsFunctionCallable &c,
47
+ bool is_gen,
48
+ bool is_async_func,
49
+ std::optional<std::string> n,
50
+ std::unordered_map<std::string, AnyValue> p,
51
+ std::map<AnyValue, AnyValue> sp,
52
+ bool is_cls,
53
+ bool is_ctor)
54
+ : callable(c),
55
+ name(std::move(n)),
56
+ props(std::move(p)),
57
+ symbol_props(std::move(sp)),
58
+ proto(Constants::Null),
59
+ is_generator(is_gen),
60
+ is_async(is_async_func),
61
+ is_class(is_cls),
62
+ is_constructor(is_ctor && !is_gen && !is_async_func)
63
+ {
64
+ }
65
+
66
+ std::string JsFunction::to_std_string() const
67
+ {
68
+ std::string type_part = this->is_async ? "async function" : this->is_generator ? "function*"
69
+ : "function";
70
+ std::string name_part = this->name.value_or("");
71
+ return type_part + " " + name_part + "() { [native code] }";
72
+ }
73
+
74
+ AnyValue JsFunction::call(AnyValue thisVal, std::span<const AnyValue> args)
75
+ {
76
+ if (std::function<AnyValue(AnyValue, std::span<const AnyValue>)> *func = std::get_if<0>(&callable))
77
+ {
78
+ return (*func)(thisVal, args);
79
+ }
80
+ else if (std::function<jspp::JsIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<1>(&callable))
81
+ {
82
+ return AnyValue::from_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
83
+ }
84
+ else if (std::function<jspp::JsPromise(AnyValue, std::vector<AnyValue>)> *func = std::get_if<2>(&callable))
85
+ {
86
+ return AnyValue::from_promise((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
87
+ }
88
+ else if (std::function<jspp::JsAsyncIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<3>(&callable))
89
+ {
90
+ return AnyValue::from_async_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
91
+ }
92
+ else
93
+ {
94
+ return Constants::UNDEFINED;
95
+ }
96
+ }
97
+
98
+ bool JsFunction::has_property(const std::string &key) const
99
+ {
100
+ if (props.find(key) != props.end())
101
+ return true;
102
+ if (!proto.is_null() && !proto.is_undefined())
103
+ {
104
+ if (proto.has_property(key))
105
+ return true;
106
+ }
107
+ if (FunctionPrototypes::get(key).has_value())
108
+ return true;
109
+ return false;
110
+ }
111
+
112
+ bool JsFunction::has_symbol_property(const AnyValue &key) const
113
+ {
114
+ if (symbol_props.count(key))
115
+ return true;
116
+ if (!proto.is_null() && !proto.is_undefined())
117
+ {
118
+ if (proto.has_property(key))
119
+ return true;
120
+ }
121
+ if (FunctionPrototypes::get(key).has_value())
122
+ return true;
123
+ return false;
124
+ }
125
+
126
+ AnyValue JsFunction::get_property(const std::string &key, AnyValue thisVal)
127
+ {
128
+ auto it = props.find(key);
129
+ if (it == props.end())
130
+ {
131
+ if (!proto.is_null() && !proto.is_undefined())
132
+ {
133
+ if (proto.has_property(key))
134
+ {
135
+ return proto.get_property_with_receiver(key, thisVal);
136
+ }
137
+ }
138
+
139
+ auto proto_it = FunctionPrototypes::get(key);
140
+ if (proto_it.has_value())
141
+ {
142
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
143
+ }
144
+ return Constants::UNDEFINED;
145
+ }
146
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key);
147
+ }
148
+
149
+ AnyValue JsFunction::get_symbol_property(const AnyValue &key, AnyValue thisVal)
150
+ {
151
+ auto it = symbol_props.find(key);
152
+ if (it == symbol_props.end())
153
+ {
154
+ if (!proto.is_null() && !proto.is_undefined())
155
+ {
156
+ auto res = proto.get_symbol_property_with_receiver(key, thisVal);
157
+ if (!res.is_undefined())
158
+ return res;
159
+ }
160
+
161
+ auto proto_it = FunctionPrototypes::get(key);
162
+ if (proto_it.has_value())
163
+ {
164
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key.to_std_string());
165
+ }
166
+ return Constants::UNDEFINED;
167
+ }
168
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key.to_std_string());
169
+ }
170
+
171
+ AnyValue JsFunction::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
172
+ {
173
+ auto proto_it = FunctionPrototypes::get(key);
174
+ if (proto_it.has_value())
175
+ {
176
+ auto proto_value = proto_it.value();
177
+ if (proto_value.is_accessor_descriptor())
178
+ {
179
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
180
+ }
181
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
182
+ {
183
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
184
+ }
185
+ }
186
+
187
+ auto it = props.find(key);
188
+ if (it != props.end())
189
+ {
190
+ return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
191
+ }
192
+ else
193
+ {
194
+ props[key] = value;
195
+ return value;
196
+ }
197
+ }
198
+
199
+ AnyValue JsFunction::set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal)
200
+ {
201
+ auto it = symbol_props.find(key);
202
+ if (it != symbol_props.end())
203
+ {
204
+ return AnyValue::resolve_property_for_write(it->second, thisVal, value, key.to_std_string());
205
+ }
206
+ else
207
+ {
208
+ symbol_props[key] = value;
209
+ return value;
210
+ }
211
+ }
212
+
213
+ // --- FunctionPrototypes Implementation ---
214
+
215
+ namespace FunctionPrototypes {
216
+
217
+ AnyValue &get_toString_fn()
218
+ {
219
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> _) -> AnyValue
220
+ { return AnyValue::make_string(thisVal.as_function()->to_std_string()); },
221
+ "toString");
222
+ return fn;
223
+ }
224
+
225
+ AnyValue &get_call_fn()
226
+ {
227
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
228
+ {
229
+ AnyValue thisArg = Constants::UNDEFINED;
230
+ std::span<const AnyValue> fnArgs;
231
+
232
+ if (!args.empty())
233
+ {
234
+ thisArg = args[0];
235
+ fnArgs = args.subspan(1);
236
+ }
237
+
238
+ return thisVal.call(thisArg, fnArgs); },
239
+ "call");
240
+ return fn;
241
+ }
242
+
243
+ std::optional<AnyValue> get(const std::string &key)
244
+ {
245
+ if (key == "toString") return get_toString_fn();
246
+ if (key == "call") return get_call_fn();
247
+ return std::nullopt;
248
+ }
249
+
250
+ std::optional<AnyValue> get(const AnyValue &key)
251
+ {
252
+ if (key.is_string())
253
+ return get(key.as_string()->value);
254
+
255
+ if (key == AnyValue::from_symbol(WellKnownSymbols::toStringTag)) return get_toString_fn();
256
+ if (key == "call") return get_call_fn();
257
+ return std::nullopt;
258
+ }
259
+
260
+ } // namespace FunctionPrototypes
261
+
262
+ } // namespace jspp
@@ -1,82 +1,62 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include <optional>
5
-
6
- namespace jspp
7
- {
8
- // Forward declaration of AnyValue
9
- class AnyValue;
10
-
11
- struct JsFunction : HeapObject
12
- {
13
- JsFunctionCallable callable;
14
- std::optional<std::string> name;
15
- std::unordered_map<std::string, AnyValue> props;
16
- AnyValue proto;
17
- bool is_generator;
18
- bool is_async;
19
- bool is_class;
20
- bool is_constructor;
21
-
22
- // ---- Constructor A: infer flags ----
23
- JsFunction(const JsFunctionCallable &c,
24
- std::optional<std::string> n = std::nullopt,
25
- std::unordered_map<std::string, AnyValue> p = {},
26
- bool is_cls = false,
27
- bool is_ctor = true)
28
- : callable(c),
29
- name(std::move(n)),
30
- props(std::move(p)),
31
- is_generator(callable.index() == 1),
32
- is_async(callable.index() == 2),
33
- is_class(is_cls),
34
- is_constructor(is_ctor && !is_generator && !is_async) // Generators and asyncs are never constructors
35
- {
36
- }
37
-
38
- // ---- Constructor B: explicit generator flag (backward compat) ----
39
- JsFunction(const JsFunctionCallable &c,
40
- bool is_gen,
41
- std::optional<std::string> n = std::nullopt,
42
- std::unordered_map<std::string, AnyValue> p = {},
43
- bool is_cls = false,
44
- bool is_ctor = true)
45
- : callable(c),
46
- name(std::move(n)),
47
- props(std::move(p)),
48
- is_generator(is_gen),
49
- is_async(callable.index() == 2),
50
- is_class(is_cls),
51
- is_constructor(is_ctor && !is_gen && !is_async)
52
- {
53
- }
54
-
55
- // ---- Constructor C: explicit async flag ----
56
- JsFunction(const JsFunctionCallable &c,
57
- bool is_gen,
58
- bool is_async_func,
59
- std::optional<std::string> n = std::nullopt,
60
- std::unordered_map<std::string, AnyValue> p = {},
61
- bool is_cls = false,
62
- bool is_ctor = true)
63
- : callable(c),
64
- name(std::move(n)),
65
- props(std::move(p)),
66
- is_generator(is_gen),
67
- is_async(is_async_func),
68
- is_class(is_cls),
69
- is_constructor(is_ctor && !is_gen && !is_async_func)
70
- {
71
- }
72
-
73
- JsType get_heap_type() const override { return JsType::Function; }
74
-
75
- std::string to_std_string() const;
76
- AnyValue call(AnyValue thisVal, std::span<const AnyValue> args);
77
-
78
- bool has_property(const std::string &key) const;
79
- AnyValue get_property(const std::string &key, AnyValue thisVal);
80
- AnyValue set_property(const std::string &key, AnyValue value, AnyValue thisVal);
81
- };
82
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include <optional>
5
+
6
+ namespace jspp
7
+ {
8
+ // Forward declaration of AnyValue
9
+ class AnyValue;
10
+
11
+ struct JsFunction : HeapObject
12
+ {
13
+ JsFunctionCallable callable;
14
+ std::optional<std::string> name;
15
+ std::unordered_map<std::string, AnyValue> props;
16
+ std::map<AnyValue, AnyValue> symbol_props;
17
+ AnyValue proto;
18
+ bool is_generator;
19
+ bool is_async;
20
+ bool is_class;
21
+ bool is_constructor;
22
+
23
+ // ---- Constructor A: infer flags ----
24
+ JsFunction(const JsFunctionCallable &c,
25
+ std::optional<std::string> n = std::nullopt,
26
+ std::unordered_map<std::string, AnyValue> p = {},
27
+ std::map<AnyValue, AnyValue> sp = {},
28
+ bool is_cls = false,
29
+ bool is_ctor = true);
30
+
31
+ // ---- Constructor B: explicit generator flag (backward compat) ----
32
+ JsFunction(const JsFunctionCallable &c,
33
+ bool is_gen,
34
+ std::optional<std::string> n = std::nullopt,
35
+ std::unordered_map<std::string, AnyValue> p = {},
36
+ std::map<AnyValue, AnyValue> sp = {},
37
+ bool is_cls = false,
38
+ bool is_ctor = true);
39
+
40
+ // ---- Constructor C: explicit async flag ----
41
+ JsFunction(const JsFunctionCallable &c,
42
+ bool is_gen,
43
+ bool is_async_func,
44
+ std::optional<std::string> n = std::nullopt,
45
+ std::unordered_map<std::string, AnyValue> p = {},
46
+ std::map<AnyValue, AnyValue> sp = {},
47
+ bool is_cls = false,
48
+ bool is_ctor = true);
49
+
50
+ JsType get_heap_type() const override { return JsType::Function; }
51
+
52
+ std::string to_std_string() const;
53
+ AnyValue call(AnyValue thisVal, std::span<const AnyValue> args);
54
+
55
+ bool has_property(const std::string &key) const;
56
+ bool has_symbol_property(const AnyValue &key) const;
57
+ AnyValue get_property(const std::string &key, AnyValue thisVal);
58
+ AnyValue get_symbol_property(const AnyValue &key, AnyValue thisVal);
59
+ AnyValue set_property(const std::string &key, AnyValue value, AnyValue thisVal);
60
+ AnyValue set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal);
61
+ };
62
+ }