@ugo-studio/jspp 0.2.9 → 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 (35) hide show
  1. package/dist/core/codegen/class-handlers.js +6 -6
  2. package/dist/core/codegen/statement-handlers.js +1 -1
  3. package/package.json +1 -1
  4. package/src/prelude/any_value.hpp +362 -362
  5. package/src/prelude/any_value_access.hpp +170 -170
  6. package/src/prelude/any_value_defines.hpp +189 -189
  7. package/src/prelude/any_value_helpers.hpp +374 -374
  8. package/src/prelude/library/array.hpp +185 -185
  9. package/src/prelude/library/console.hpp +111 -111
  10. package/src/prelude/library/error.hpp +112 -112
  11. package/src/prelude/library/function.hpp +10 -10
  12. package/src/prelude/library/math.hpp +307 -307
  13. package/src/prelude/library/object.hpp +275 -275
  14. package/src/prelude/library/process.hpp +39 -39
  15. package/src/prelude/library/promise.hpp +123 -123
  16. package/src/prelude/library/symbol.hpp +52 -52
  17. package/src/prelude/library/timer.hpp +91 -91
  18. package/src/prelude/types.hpp +178 -178
  19. package/src/prelude/utils/access.hpp +411 -411
  20. package/src/prelude/utils/operators.hpp +336 -336
  21. package/src/prelude/values/array.hpp +0 -1
  22. package/src/prelude/values/async_iterator.hpp +83 -83
  23. package/src/prelude/values/function.hpp +82 -82
  24. package/src/prelude/values/helpers/array.hpp +198 -208
  25. package/src/prelude/values/helpers/async_iterator.hpp +275 -275
  26. package/src/prelude/values/helpers/function.hpp +108 -108
  27. package/src/prelude/values/helpers/iterator.hpp +144 -144
  28. package/src/prelude/values/helpers/promise.hpp +253 -253
  29. package/src/prelude/values/helpers/string.hpp +37 -61
  30. package/src/prelude/values/promise.hpp +72 -72
  31. package/src/prelude/values/prototypes/array.hpp +14 -2
  32. package/src/prelude/values/prototypes/iterator.hpp +201 -201
  33. package/src/prelude/values/prototypes/promise.hpp +196 -196
  34. package/src/prelude/values/prototypes/string.hpp +564 -542
  35. package/src/prelude/values/string.hpp +25 -26
@@ -1,109 +1,109 @@
1
- #pragma once
2
-
3
- #include <variant>
4
- #include <optional>
5
-
6
- #include "types.hpp"
7
- #include "values/function.hpp"
8
- #include "any_value.hpp"
9
- #include "values/prototypes/function.hpp"
10
-
11
- namespace jspp
12
- {
13
- inline std::string JsFunction::to_std_string() const
14
- {
15
- std::string type_part = this->is_async ? "async function" : this->is_generator ? "function*"
16
- : "function";
17
- std::string name_part = this->name.value_or("");
18
- return type_part + " " + name_part + "() { [native code] }";
19
- }
20
-
21
- inline AnyValue JsFunction::call(AnyValue thisVal, std::span<const AnyValue> args)
22
- {
23
- if (std::function<AnyValue(AnyValue, std::span<const AnyValue>)> *func = std::get_if<0>(&callable))
24
- {
25
- return (*func)(thisVal, args);
26
- }
27
- else if (std::function<jspp::JsIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<1>(&callable))
28
- {
29
- return AnyValue::from_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
30
- }
31
- else if (std::function<jspp::JsPromise(AnyValue, std::vector<AnyValue>)> *func = std::get_if<2>(&callable))
32
- {
33
- return AnyValue::make_promise((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
34
- }
35
- else if (std::function<jspp::JsAsyncIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<3>(&callable))
36
- {
37
- return AnyValue::from_async_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
38
- }
39
- else
40
- {
41
- return Constants::UNDEFINED;
42
- }
43
- }
44
-
45
- inline bool JsFunction::has_property(const std::string &key) const
46
- {
47
- if (props.find(key) != props.end())
48
- return true;
49
- if (!proto.is_null() && !proto.is_undefined())
50
- {
51
- if (proto.has_property(key))
52
- return true;
53
- }
54
- if (FunctionPrototypes::get(key).has_value())
55
- return true;
56
- return false;
57
- }
58
-
59
- inline AnyValue JsFunction::get_property(const std::string &key, AnyValue thisVal)
60
- {
61
- auto it = props.find(key);
62
- if (it == props.end())
63
- {
64
- if (!proto.is_null() && !proto.is_undefined())
65
- {
66
- if (proto.has_property(key))
67
- {
68
- return proto.get_property_with_receiver(key, thisVal);
69
- }
70
- }
71
-
72
- auto proto_it = FunctionPrototypes::get(key);
73
- if (proto_it.has_value())
74
- {
75
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
76
- }
77
- return Constants::UNDEFINED;
78
- }
79
- return AnyValue::resolve_property_for_read(it->second, thisVal, key);
80
- }
81
-
82
- inline AnyValue JsFunction::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
83
- {
84
- auto proto_it = FunctionPrototypes::get(key);
85
- if (proto_it.has_value())
86
- {
87
- auto proto_value = proto_it.value();
88
- if (proto_value.is_accessor_descriptor())
89
- {
90
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
91
- }
92
- if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
93
- {
94
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
95
- }
96
- }
97
-
98
- auto it = props.find(key);
99
- if (it != props.end())
100
- {
101
- return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
102
- }
103
- else
104
- {
105
- props[key] = value;
106
- return value;
107
- }
108
- }
1
+ #pragma once
2
+
3
+ #include <variant>
4
+ #include <optional>
5
+
6
+ #include "types.hpp"
7
+ #include "values/function.hpp"
8
+ #include "any_value.hpp"
9
+ #include "values/prototypes/function.hpp"
10
+
11
+ namespace jspp
12
+ {
13
+ inline std::string JsFunction::to_std_string() const
14
+ {
15
+ std::string type_part = this->is_async ? "async function" : this->is_generator ? "function*"
16
+ : "function";
17
+ std::string name_part = this->name.value_or("");
18
+ return type_part + " " + name_part + "() { [native code] }";
19
+ }
20
+
21
+ inline AnyValue JsFunction::call(AnyValue thisVal, std::span<const AnyValue> args)
22
+ {
23
+ if (std::function<AnyValue(AnyValue, std::span<const AnyValue>)> *func = std::get_if<0>(&callable))
24
+ {
25
+ return (*func)(thisVal, args);
26
+ }
27
+ else if (std::function<jspp::JsIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<1>(&callable))
28
+ {
29
+ return AnyValue::from_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
30
+ }
31
+ else if (std::function<jspp::JsPromise(AnyValue, std::vector<AnyValue>)> *func = std::get_if<2>(&callable))
32
+ {
33
+ return AnyValue::make_promise((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
34
+ }
35
+ else if (std::function<jspp::JsAsyncIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<3>(&callable))
36
+ {
37
+ return AnyValue::from_async_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
38
+ }
39
+ else
40
+ {
41
+ return Constants::UNDEFINED;
42
+ }
43
+ }
44
+
45
+ inline bool JsFunction::has_property(const std::string &key) const
46
+ {
47
+ if (props.find(key) != props.end())
48
+ return true;
49
+ if (!proto.is_null() && !proto.is_undefined())
50
+ {
51
+ if (proto.has_property(key))
52
+ return true;
53
+ }
54
+ if (FunctionPrototypes::get(key).has_value())
55
+ return true;
56
+ return false;
57
+ }
58
+
59
+ inline AnyValue JsFunction::get_property(const std::string &key, AnyValue thisVal)
60
+ {
61
+ auto it = props.find(key);
62
+ if (it == props.end())
63
+ {
64
+ if (!proto.is_null() && !proto.is_undefined())
65
+ {
66
+ if (proto.has_property(key))
67
+ {
68
+ return proto.get_property_with_receiver(key, thisVal);
69
+ }
70
+ }
71
+
72
+ auto proto_it = FunctionPrototypes::get(key);
73
+ if (proto_it.has_value())
74
+ {
75
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
76
+ }
77
+ return Constants::UNDEFINED;
78
+ }
79
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key);
80
+ }
81
+
82
+ inline AnyValue JsFunction::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
83
+ {
84
+ auto proto_it = FunctionPrototypes::get(key);
85
+ if (proto_it.has_value())
86
+ {
87
+ auto proto_value = proto_it.value();
88
+ if (proto_value.is_accessor_descriptor())
89
+ {
90
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
91
+ }
92
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
93
+ {
94
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
95
+ }
96
+ }
97
+
98
+ auto it = props.find(key);
99
+ if (it != props.end())
100
+ {
101
+ return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
102
+ }
103
+ else
104
+ {
105
+ props[key] = value;
106
+ return value;
107
+ }
108
+ }
109
109
  }
@@ -1,145 +1,145 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/iterator.hpp"
5
- #include "any_value.hpp"
6
- #include "values/prototypes/iterator.hpp"
7
-
8
- template <typename T>
9
- std::string jspp::JsIterator<T>::to_std_string() const
10
- {
11
- return "[object Generator]";
12
- }
13
-
14
- template <typename T>
15
- typename jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::next(const T &val)
16
- {
17
- // If the generator is already finished or invalid, return {undefined, true}
18
- if (!handle || handle.done())
19
- return {std::nullopt, true};
20
-
21
- handle.promise().input_value = val;
22
-
23
- // Resume execution until next co_yield or co_return
24
- handle.resume();
25
-
26
- if (handle.promise().exception_)
27
- {
28
- std::rethrow_exception(handle.promise().exception_);
29
- }
30
-
31
- // If handle.done() is TRUE, we hit co_return (value: X, done: true)
32
- // If handle.done() is FALSE, we hit co_yield (value: X, done: false)
33
- bool is_done = handle.done();
34
-
35
- return {handle.promise().current_value, is_done};
36
- }
37
-
38
- template <typename T>
39
- typename jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::return_(const T &val)
40
- {
41
- if (!handle || handle.done())
42
- return {val, true};
43
-
44
- handle.promise().pending_return = true;
45
- handle.promise().current_value = val;
46
- handle.resume();
47
-
48
- if (handle.promise().exception_)
49
- {
50
- std::rethrow_exception(handle.promise().exception_);
51
- }
52
-
53
- return {handle.promise().current_value, true};
54
- }
55
-
56
- template <typename T>
57
- typename jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::throw_(const AnyValue &err)
58
- {
59
- if (!handle || handle.done()) {
60
- throw Exception(err);
61
- }
62
-
63
- handle.promise().pending_exception = std::make_exception_ptr(Exception(err));
64
- handle.resume();
65
-
66
- if (handle.promise().exception_)
67
- {
68
- std::rethrow_exception(handle.promise().exception_);
69
- }
70
-
71
- bool is_done = handle.done();
72
- return {handle.promise().current_value, is_done};
73
- }
74
-
75
- template <typename T>
76
- std::vector<T> jspp::JsIterator<T>::to_vector()
77
- {
78
- std::vector<T> result;
79
- while (true)
80
- {
81
- auto next_res = this->next();
82
- if (next_res.done)
83
- {
84
- break;
85
- }
86
- result.push_back(next_res.value.value_or(Constants::UNDEFINED));
87
- }
88
- return result;
89
- }
90
-
91
- template <typename T>
92
- jspp::AnyValue jspp::JsIterator<T>::get_property(const std::string &key, AnyValue thisVal)
93
- {
94
- auto it = props.find(key);
95
- if (it == props.end())
96
- {
97
- // check prototype
98
- if constexpr (std::is_same_v<T, AnyValue>)
99
- {
100
- auto proto_it = IteratorPrototypes::get(key);
101
- if (proto_it.has_value())
102
- {
103
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
104
- }
105
- }
106
- // prototype not found
107
- return Constants::UNDEFINED;
108
- }
109
-
110
- return AnyValue::resolve_property_for_read(it->second, thisVal, key);
111
- }
112
-
113
- template <typename T>
114
- jspp::AnyValue jspp::JsIterator<T>::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
115
- {
116
- // set prototype property if accessor descriptor
117
- if constexpr (std::is_same_v<T, AnyValue>)
118
- {
119
- auto proto_it = IteratorPrototypes::get(key);
120
- if (proto_it.has_value())
121
- {
122
- auto proto_value = proto_it.value();
123
- if (proto_value.is_accessor_descriptor())
124
- {
125
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
126
- }
127
- if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
128
- {
129
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
130
- }
131
- }
132
- }
133
-
134
- // set own property
135
- auto it = props.find(key);
136
- if (it != props.end())
137
- {
138
- return jspp::AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
139
- }
140
- else
141
- {
142
- props[key] = value;
143
- return value;
144
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "values/iterator.hpp"
5
+ #include "any_value.hpp"
6
+ #include "values/prototypes/iterator.hpp"
7
+
8
+ template <typename T>
9
+ std::string jspp::JsIterator<T>::to_std_string() const
10
+ {
11
+ return "[object Generator]";
12
+ }
13
+
14
+ template <typename T>
15
+ typename jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::next(const T &val)
16
+ {
17
+ // If the generator is already finished or invalid, return {undefined, true}
18
+ if (!handle || handle.done())
19
+ return {std::nullopt, true};
20
+
21
+ handle.promise().input_value = val;
22
+
23
+ // Resume execution until next co_yield or co_return
24
+ handle.resume();
25
+
26
+ if (handle.promise().exception_)
27
+ {
28
+ std::rethrow_exception(handle.promise().exception_);
29
+ }
30
+
31
+ // If handle.done() is TRUE, we hit co_return (value: X, done: true)
32
+ // If handle.done() is FALSE, we hit co_yield (value: X, done: false)
33
+ bool is_done = handle.done();
34
+
35
+ return {handle.promise().current_value, is_done};
36
+ }
37
+
38
+ template <typename T>
39
+ typename jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::return_(const T &val)
40
+ {
41
+ if (!handle || handle.done())
42
+ return {val, true};
43
+
44
+ handle.promise().pending_return = true;
45
+ handle.promise().current_value = val;
46
+ handle.resume();
47
+
48
+ if (handle.promise().exception_)
49
+ {
50
+ std::rethrow_exception(handle.promise().exception_);
51
+ }
52
+
53
+ return {handle.promise().current_value, true};
54
+ }
55
+
56
+ template <typename T>
57
+ typename jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::throw_(const AnyValue &err)
58
+ {
59
+ if (!handle || handle.done()) {
60
+ throw Exception(err);
61
+ }
62
+
63
+ handle.promise().pending_exception = std::make_exception_ptr(Exception(err));
64
+ handle.resume();
65
+
66
+ if (handle.promise().exception_)
67
+ {
68
+ std::rethrow_exception(handle.promise().exception_);
69
+ }
70
+
71
+ bool is_done = handle.done();
72
+ return {handle.promise().current_value, is_done};
73
+ }
74
+
75
+ template <typename T>
76
+ std::vector<T> jspp::JsIterator<T>::to_vector()
77
+ {
78
+ std::vector<T> result;
79
+ while (true)
80
+ {
81
+ auto next_res = this->next();
82
+ if (next_res.done)
83
+ {
84
+ break;
85
+ }
86
+ result.push_back(next_res.value.value_or(Constants::UNDEFINED));
87
+ }
88
+ return result;
89
+ }
90
+
91
+ template <typename T>
92
+ jspp::AnyValue jspp::JsIterator<T>::get_property(const std::string &key, AnyValue thisVal)
93
+ {
94
+ auto it = props.find(key);
95
+ if (it == props.end())
96
+ {
97
+ // check prototype
98
+ if constexpr (std::is_same_v<T, AnyValue>)
99
+ {
100
+ auto proto_it = IteratorPrototypes::get(key);
101
+ if (proto_it.has_value())
102
+ {
103
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
104
+ }
105
+ }
106
+ // prototype not found
107
+ return Constants::UNDEFINED;
108
+ }
109
+
110
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key);
111
+ }
112
+
113
+ template <typename T>
114
+ jspp::AnyValue jspp::JsIterator<T>::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
115
+ {
116
+ // set prototype property if accessor descriptor
117
+ if constexpr (std::is_same_v<T, AnyValue>)
118
+ {
119
+ auto proto_it = IteratorPrototypes::get(key);
120
+ if (proto_it.has_value())
121
+ {
122
+ auto proto_value = proto_it.value();
123
+ if (proto_value.is_accessor_descriptor())
124
+ {
125
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
126
+ }
127
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
128
+ {
129
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
130
+ }
131
+ }
132
+ }
133
+
134
+ // set own property
135
+ auto it = props.find(key);
136
+ if (it != props.end())
137
+ {
138
+ return jspp::AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
139
+ }
140
+ else
141
+ {
142
+ props[key] = value;
143
+ return value;
144
+ }
145
145
  }