@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.
- package/dist/analysis/typeAnalyzer.js +42 -27
- package/dist/core/codegen/class-handlers.js +6 -6
- package/dist/core/codegen/control-flow-handlers.js +4 -4
- package/dist/core/codegen/declaration-handlers.js +21 -3
- package/dist/core/codegen/destructuring-handlers.js +187 -0
- package/dist/core/codegen/expression-handlers.js +7 -0
- package/dist/core/codegen/function-handlers.js +58 -36
- package/dist/core/codegen/helpers.js +288 -52
- package/dist/core/codegen/index.js +7 -4
- package/dist/core/codegen/statement-handlers.js +43 -23
- package/package.json +1 -1
- package/scripts/precompile-headers.ts +13 -5
- package/src/prelude/any_value.hpp +362 -361
- package/src/prelude/any_value_access.hpp +170 -170
- package/src/prelude/any_value_defines.hpp +189 -189
- package/src/prelude/any_value_helpers.hpp +374 -365
- package/src/prelude/library/array.hpp +185 -185
- package/src/prelude/library/console.hpp +111 -111
- package/src/prelude/library/error.hpp +112 -112
- package/src/prelude/library/function.hpp +10 -10
- package/src/prelude/library/math.hpp +307 -307
- package/src/prelude/library/object.hpp +275 -275
- package/src/prelude/library/performance.hpp +1 -1
- package/src/prelude/library/process.hpp +39 -39
- package/src/prelude/library/promise.hpp +123 -123
- package/src/prelude/library/symbol.hpp +52 -52
- package/src/prelude/library/timer.hpp +91 -91
- package/src/prelude/types.hpp +178 -178
- package/src/prelude/utils/access.hpp +411 -393
- package/src/prelude/utils/operators.hpp +336 -329
- package/src/prelude/values/array.hpp +0 -1
- package/src/prelude/values/async_iterator.hpp +83 -81
- package/src/prelude/values/function.hpp +82 -82
- package/src/prelude/values/helpers/array.hpp +198 -208
- package/src/prelude/values/helpers/async_iterator.hpp +275 -271
- package/src/prelude/values/helpers/function.hpp +108 -108
- package/src/prelude/values/helpers/iterator.hpp +144 -107
- package/src/prelude/values/helpers/promise.hpp +253 -253
- package/src/prelude/values/helpers/string.hpp +37 -47
- package/src/prelude/values/iterator.hpp +32 -5
- package/src/prelude/values/promise.hpp +72 -72
- package/src/prelude/values/prototypes/array.hpp +54 -42
- package/src/prelude/values/prototypes/iterator.hpp +201 -74
- package/src/prelude/values/prototypes/promise.hpp +196 -196
- package/src/prelude/values/prototypes/string.hpp +564 -542
- package/src/prelude/values/string.hpp +25 -26
|
@@ -1,186 +1,186 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "any_value.hpp"
|
|
5
|
-
#include "utils/operators.hpp"
|
|
6
|
-
#include "utils/access.hpp"
|
|
7
|
-
|
|
8
|
-
inline auto Array = jspp::AnyValue::make_class([](
|
|
9
|
-
{
|
|
10
|
-
if (args.size() == 1 && args[0].is_number()) {
|
|
11
|
-
double len = args[0].as_double();
|
|
12
|
-
if (len < 0 || len > 4294967295.0) { // Max uint32
|
|
13
|
-
throw jspp::Exception::make_exception("Invalid array length", "RangeError");
|
|
14
|
-
}
|
|
15
|
-
auto arr = jspp::AnyValue::make_array(std::vector<jspp::AnyValue>());
|
|
16
|
-
auto arr_ptr = arr.as_array();
|
|
17
|
-
arr_ptr->length = static_cast<uint64_t>(len);
|
|
18
|
-
arr_ptr->dense.resize(static_cast<size_t>(len), jspp::Constants::UNINITIALIZED);
|
|
19
|
-
return arr;
|
|
20
|
-
}
|
|
21
|
-
std::vector<jspp::AnyValue> elements;
|
|
22
|
-
for(const auto& arg : args) {
|
|
23
|
-
elements.push_back(arg);
|
|
24
|
-
}
|
|
25
|
-
return jspp::AnyValue::make_array(std::move(elements)); }, "Array");
|
|
26
|
-
|
|
27
|
-
struct ArrayInit
|
|
28
|
-
{
|
|
29
|
-
ArrayInit()
|
|
30
|
-
{
|
|
31
|
-
// Array.isArray(value)
|
|
32
|
-
Array.define_data_property("isArray", jspp::AnyValue::make_function([](
|
|
33
|
-
{
|
|
34
|
-
if (args.empty()) return jspp::Constants::FALSE;
|
|
35
|
-
return jspp::AnyValue::make_boolean(args[0].is_array()); }, "isArray"));
|
|
36
|
-
|
|
37
|
-
// Array.of(...elements)
|
|
38
|
-
Array.define_data_property("of", jspp::AnyValue::make_function([](
|
|
39
|
-
{
|
|
40
|
-
std::vector<jspp::AnyValue> elements;
|
|
41
|
-
for(const auto& arg : args) {
|
|
42
|
-
elements.push_back(arg);
|
|
43
|
-
}
|
|
44
|
-
return jspp::AnyValue::make_array(std::move(elements)); }, "of"));
|
|
45
|
-
|
|
46
|
-
// Array.from(arrayLike, mapFn?, thisArg?)
|
|
47
|
-
Array.define_data_property("from", jspp::AnyValue::make_function([](
|
|
48
|
-
{
|
|
49
|
-
if (args.empty() || args[0].is_null() || args[0].is_undefined()) {
|
|
50
|
-
throw jspp::Exception::make_exception("Array.from requires an array-like object", "TypeError");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const auto& items = args[0];
|
|
54
|
-
const auto& mapFn = (args.size() > 1 && args[1].is_function()) ? args[1] : jspp::Constants::UNDEFINED;
|
|
55
|
-
const auto& thisArg = (args.size() > 2) ? args[2] : jspp::Constants::UNDEFINED;
|
|
56
|
-
|
|
57
|
-
std::vector<jspp::AnyValue> result;
|
|
58
|
-
|
|
59
|
-
auto iteratorSym = jspp::WellKnownSymbols::iterator;
|
|
60
|
-
if (items.has_property(iteratorSym->key)) {
|
|
61
|
-
auto iter = jspp::Access::
|
|
62
|
-
auto nextFn = iter.get_own_property("next");
|
|
63
|
-
|
|
64
|
-
size_t k = 0;
|
|
65
|
-
while (true) {
|
|
66
|
-
auto nextRes = nextFn.call(iter, std::span<const jspp::AnyValue>{}, "next");
|
|
67
|
-
if (jspp::is_truthy(nextRes.get_own_property("done"))) break;
|
|
68
|
-
|
|
69
|
-
auto val = nextRes.get_own_property("value");
|
|
70
|
-
if (mapFn.is_function()) {
|
|
71
|
-
jspp::AnyValue kVal = jspp::AnyValue::make_number(k);
|
|
72
|
-
const jspp::AnyValue mapArgs[] = {val, kVal};
|
|
73
|
-
val = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
74
|
-
}
|
|
75
|
-
result.push_back(val);
|
|
76
|
-
k++;
|
|
77
|
-
}
|
|
78
|
-
} else {
|
|
79
|
-
// Array-like (length property)
|
|
80
|
-
auto lenVal = items.get_property_with_receiver("length", items);
|
|
81
|
-
size_t len = static_cast<size_t>(jspp::Operators_Private::ToUint32(lenVal));
|
|
82
|
-
|
|
83
|
-
for (size_t k = 0; k < len; ++k) {
|
|
84
|
-
auto kVal = items.get_property_with_receiver(std::to_string(k), items);
|
|
85
|
-
if (mapFn.is_function()) {
|
|
86
|
-
jspp::AnyValue kNum = jspp::AnyValue::make_number(k);
|
|
87
|
-
const jspp::AnyValue mapArgs[] = {kVal, kNum};
|
|
88
|
-
kVal = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
89
|
-
}
|
|
90
|
-
result.push_back(kVal);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return jspp::AnyValue::make_array(std::move(result)); }, "from"));
|
|
95
|
-
|
|
96
|
-
// Array.fromAsync(iterableOrArrayLike, mapFn?, thisArg?)
|
|
97
|
-
Array.define_data_property("fromAsync", jspp::AnyValue::make_async_function([](
|
|
98
|
-
{
|
|
99
|
-
if (args.empty() || args[0].is_null() || args[0].is_undefined()) {
|
|
100
|
-
throw jspp::Exception::make_exception("Array.fromAsync requires an iterable or array-like object", "TypeError");
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const auto& items = args[0];
|
|
104
|
-
const auto& mapFn = (args.size() > 1 && args[1].is_function()) ? args[1] : jspp::Constants::UNDEFINED;
|
|
105
|
-
const auto& thisArg = (args.size() > 2) ? args[2] : jspp::Constants::UNDEFINED;
|
|
106
|
-
|
|
107
|
-
std::vector<jspp::AnyValue> result;
|
|
108
|
-
|
|
109
|
-
bool isAsync = false;
|
|
110
|
-
jspp::AnyValue iter = jspp::Constants::UNDEFINED;
|
|
111
|
-
jspp::AnyValue nextFn = jspp::Constants::UNDEFINED;
|
|
112
|
-
|
|
113
|
-
if (items.has_property(jspp::WellKnownSymbols::asyncIterator->key)) {
|
|
114
|
-
auto method = items.get_property_with_receiver(jspp::WellKnownSymbols::asyncIterator->key, items);
|
|
115
|
-
if (method.is_function()) {
|
|
116
|
-
iter = method.call(items, {});
|
|
117
|
-
nextFn = iter.get_own_property("next");
|
|
118
|
-
isAsync = true;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (!isAsync && items.has_property(jspp::WellKnownSymbols::iterator->key)) {
|
|
123
|
-
auto method = items.get_property_with_receiver(jspp::WellKnownSymbols::iterator->key, items);
|
|
124
|
-
if (method.is_function()) {
|
|
125
|
-
iter = method.call(items, {});
|
|
126
|
-
nextFn = iter.get_own_property("next");
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (!iter.is_undefined()) {
|
|
131
|
-
size_t k = 0;
|
|
132
|
-
while (true) {
|
|
133
|
-
auto nextRes = nextFn.call(iter, {});
|
|
134
|
-
|
|
135
|
-
if (nextRes.is_promise()) {
|
|
136
|
-
nextRes = co_await nextRes;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (jspp::is_truthy(nextRes.get_own_property("done"))) break;
|
|
140
|
-
|
|
141
|
-
auto val = nextRes.get_own_property("value");
|
|
142
|
-
|
|
143
|
-
if (mapFn.is_function()) {
|
|
144
|
-
jspp::AnyValue kVal = jspp::AnyValue::make_number(k);
|
|
145
|
-
const jspp::AnyValue mapArgs[] = {val, kVal};
|
|
146
|
-
auto mapRes = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
147
|
-
if (mapRes.is_promise()) {
|
|
148
|
-
val = co_await mapRes;
|
|
149
|
-
} else {
|
|
150
|
-
val = mapRes;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
result.push_back(val);
|
|
154
|
-
k++;
|
|
155
|
-
}
|
|
156
|
-
} else {
|
|
157
|
-
auto lenVal = items.get_property_with_receiver("length", items);
|
|
158
|
-
size_t len = static_cast<size_t>(jspp::Operators_Private::ToUint32(lenVal));
|
|
159
|
-
|
|
160
|
-
for (size_t k = 0; k < len; ++k) {
|
|
161
|
-
auto kVal = items.get_property_with_receiver(std::to_string(k), items);
|
|
162
|
-
if (kVal.is_promise()) {
|
|
163
|
-
kVal = co_await kVal;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (mapFn.is_function()) {
|
|
167
|
-
jspp::AnyValue kNum = jspp::AnyValue::make_number(k);
|
|
168
|
-
const jspp::AnyValue mapArgs[] = {kVal, kNum};
|
|
169
|
-
auto mapRes = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
170
|
-
if (mapRes.is_promise()) {
|
|
171
|
-
kVal = co_await mapRes;
|
|
172
|
-
} else {
|
|
173
|
-
kVal = mapRes;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
result.push_back(kVal);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
co_return jspp::AnyValue::make_array(std::move(result)); }, "fromAsync"));
|
|
181
|
-
|
|
182
|
-
// Array[Symbol.species]
|
|
183
|
-
Array.define_getter(jspp::AnyValue::from_symbol(jspp::WellKnownSymbols::species), jspp::AnyValue::make_function([](
|
|
184
|
-
{ return thisVal; }, "get [Symbol.species]"));
|
|
185
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "any_value.hpp"
|
|
5
|
+
#include "utils/operators.hpp"
|
|
6
|
+
#include "utils/access.hpp"
|
|
7
|
+
|
|
8
|
+
inline auto Array = jspp::AnyValue::make_class([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
9
|
+
{
|
|
10
|
+
if (args.size() == 1 && args[0].is_number()) {
|
|
11
|
+
double len = args[0].as_double();
|
|
12
|
+
if (len < 0 || len > 4294967295.0) { // Max uint32
|
|
13
|
+
throw jspp::Exception::make_exception("Invalid array length", "RangeError");
|
|
14
|
+
}
|
|
15
|
+
auto arr = jspp::AnyValue::make_array(std::vector<jspp::AnyValue>());
|
|
16
|
+
auto arr_ptr = arr.as_array();
|
|
17
|
+
arr_ptr->length = static_cast<uint64_t>(len);
|
|
18
|
+
arr_ptr->dense.resize(static_cast<size_t>(len), jspp::Constants::UNINITIALIZED);
|
|
19
|
+
return arr;
|
|
20
|
+
}
|
|
21
|
+
std::vector<jspp::AnyValue> elements;
|
|
22
|
+
for(const auto& arg : args) {
|
|
23
|
+
elements.push_back(arg);
|
|
24
|
+
}
|
|
25
|
+
return jspp::AnyValue::make_array(std::move(elements)); }, "Array");
|
|
26
|
+
|
|
27
|
+
struct ArrayInit
|
|
28
|
+
{
|
|
29
|
+
ArrayInit()
|
|
30
|
+
{
|
|
31
|
+
// Array.isArray(value)
|
|
32
|
+
Array.define_data_property("isArray", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
33
|
+
{
|
|
34
|
+
if (args.empty()) return jspp::Constants::FALSE;
|
|
35
|
+
return jspp::AnyValue::make_boolean(args[0].is_array()); }, "isArray"));
|
|
36
|
+
|
|
37
|
+
// Array.of(...elements)
|
|
38
|
+
Array.define_data_property("of", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
39
|
+
{
|
|
40
|
+
std::vector<jspp::AnyValue> elements;
|
|
41
|
+
for(const auto& arg : args) {
|
|
42
|
+
elements.push_back(arg);
|
|
43
|
+
}
|
|
44
|
+
return jspp::AnyValue::make_array(std::move(elements)); }, "of"));
|
|
45
|
+
|
|
46
|
+
// Array.from(arrayLike, mapFn?, thisArg?)
|
|
47
|
+
Array.define_data_property("from", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
48
|
+
{
|
|
49
|
+
if (args.empty() || args[0].is_null() || args[0].is_undefined()) {
|
|
50
|
+
throw jspp::Exception::make_exception("Array.from requires an array-like object", "TypeError");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const auto& items = args[0];
|
|
54
|
+
const auto& mapFn = (args.size() > 1 && args[1].is_function()) ? args[1] : jspp::Constants::UNDEFINED;
|
|
55
|
+
const auto& thisArg = (args.size() > 2) ? args[2] : jspp::Constants::UNDEFINED;
|
|
56
|
+
|
|
57
|
+
std::vector<jspp::AnyValue> result;
|
|
58
|
+
|
|
59
|
+
auto iteratorSym = jspp::WellKnownSymbols::iterator;
|
|
60
|
+
if (items.has_property(iteratorSym->key)) {
|
|
61
|
+
auto iter = jspp::Access::get_object_iterator(items, "Array.from source");
|
|
62
|
+
auto nextFn = iter.get_own_property("next");
|
|
63
|
+
|
|
64
|
+
size_t k = 0;
|
|
65
|
+
while (true) {
|
|
66
|
+
auto nextRes = nextFn.call(iter, std::span<const jspp::AnyValue>{}, "next");
|
|
67
|
+
if (jspp::is_truthy(nextRes.get_own_property("done"))) break;
|
|
68
|
+
|
|
69
|
+
auto val = nextRes.get_own_property("value");
|
|
70
|
+
if (mapFn.is_function()) {
|
|
71
|
+
jspp::AnyValue kVal = jspp::AnyValue::make_number(k);
|
|
72
|
+
const jspp::AnyValue mapArgs[] = {val, kVal};
|
|
73
|
+
val = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
74
|
+
}
|
|
75
|
+
result.push_back(val);
|
|
76
|
+
k++;
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
// Array-like (length property)
|
|
80
|
+
auto lenVal = items.get_property_with_receiver("length", items);
|
|
81
|
+
size_t len = static_cast<size_t>(jspp::Operators_Private::ToUint32(lenVal));
|
|
82
|
+
|
|
83
|
+
for (size_t k = 0; k < len; ++k) {
|
|
84
|
+
auto kVal = items.get_property_with_receiver(std::to_string(k), items);
|
|
85
|
+
if (mapFn.is_function()) {
|
|
86
|
+
jspp::AnyValue kNum = jspp::AnyValue::make_number(k);
|
|
87
|
+
const jspp::AnyValue mapArgs[] = {kVal, kNum};
|
|
88
|
+
kVal = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
89
|
+
}
|
|
90
|
+
result.push_back(kVal);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return jspp::AnyValue::make_array(std::move(result)); }, "from"));
|
|
95
|
+
|
|
96
|
+
// Array.fromAsync(iterableOrArrayLike, mapFn?, thisArg?)
|
|
97
|
+
Array.define_data_property("fromAsync", jspp::AnyValue::make_async_function([](jspp::AnyValue, std::vector<jspp::AnyValue> args) -> jspp::JsPromise
|
|
98
|
+
{
|
|
99
|
+
if (args.empty() || args[0].is_null() || args[0].is_undefined()) {
|
|
100
|
+
throw jspp::Exception::make_exception("Array.fromAsync requires an iterable or array-like object", "TypeError");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const auto& items = args[0];
|
|
104
|
+
const auto& mapFn = (args.size() > 1 && args[1].is_function()) ? args[1] : jspp::Constants::UNDEFINED;
|
|
105
|
+
const auto& thisArg = (args.size() > 2) ? args[2] : jspp::Constants::UNDEFINED;
|
|
106
|
+
|
|
107
|
+
std::vector<jspp::AnyValue> result;
|
|
108
|
+
|
|
109
|
+
bool isAsync = false;
|
|
110
|
+
jspp::AnyValue iter = jspp::Constants::UNDEFINED;
|
|
111
|
+
jspp::AnyValue nextFn = jspp::Constants::UNDEFINED;
|
|
112
|
+
|
|
113
|
+
if (items.has_property(jspp::WellKnownSymbols::asyncIterator->key)) {
|
|
114
|
+
auto method = items.get_property_with_receiver(jspp::WellKnownSymbols::asyncIterator->key, items);
|
|
115
|
+
if (method.is_function()) {
|
|
116
|
+
iter = method.call(items, {});
|
|
117
|
+
nextFn = iter.get_own_property("next");
|
|
118
|
+
isAsync = true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!isAsync && items.has_property(jspp::WellKnownSymbols::iterator->key)) {
|
|
123
|
+
auto method = items.get_property_with_receiver(jspp::WellKnownSymbols::iterator->key, items);
|
|
124
|
+
if (method.is_function()) {
|
|
125
|
+
iter = method.call(items, {});
|
|
126
|
+
nextFn = iter.get_own_property("next");
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!iter.is_undefined()) {
|
|
131
|
+
size_t k = 0;
|
|
132
|
+
while (true) {
|
|
133
|
+
auto nextRes = nextFn.call(iter, {});
|
|
134
|
+
|
|
135
|
+
if (nextRes.is_promise()) {
|
|
136
|
+
nextRes = co_await nextRes;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (jspp::is_truthy(nextRes.get_own_property("done"))) break;
|
|
140
|
+
|
|
141
|
+
auto val = nextRes.get_own_property("value");
|
|
142
|
+
|
|
143
|
+
if (mapFn.is_function()) {
|
|
144
|
+
jspp::AnyValue kVal = jspp::AnyValue::make_number(k);
|
|
145
|
+
const jspp::AnyValue mapArgs[] = {val, kVal};
|
|
146
|
+
auto mapRes = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
147
|
+
if (mapRes.is_promise()) {
|
|
148
|
+
val = co_await mapRes;
|
|
149
|
+
} else {
|
|
150
|
+
val = mapRes;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
result.push_back(val);
|
|
154
|
+
k++;
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
auto lenVal = items.get_property_with_receiver("length", items);
|
|
158
|
+
size_t len = static_cast<size_t>(jspp::Operators_Private::ToUint32(lenVal));
|
|
159
|
+
|
|
160
|
+
for (size_t k = 0; k < len; ++k) {
|
|
161
|
+
auto kVal = items.get_property_with_receiver(std::to_string(k), items);
|
|
162
|
+
if (kVal.is_promise()) {
|
|
163
|
+
kVal = co_await kVal;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (mapFn.is_function()) {
|
|
167
|
+
jspp::AnyValue kNum = jspp::AnyValue::make_number(k);
|
|
168
|
+
const jspp::AnyValue mapArgs[] = {kVal, kNum};
|
|
169
|
+
auto mapRes = mapFn.call(thisArg, std::span<const jspp::AnyValue>(mapArgs, 2));
|
|
170
|
+
if (mapRes.is_promise()) {
|
|
171
|
+
kVal = co_await mapRes;
|
|
172
|
+
} else {
|
|
173
|
+
kVal = mapRes;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
result.push_back(kVal);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
co_return jspp::AnyValue::make_array(std::move(result)); }, "fromAsync"));
|
|
181
|
+
|
|
182
|
+
// Array[Symbol.species]
|
|
183
|
+
Array.define_getter(jspp::AnyValue::from_symbol(jspp::WellKnownSymbols::species), jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
184
|
+
{ return thisVal; }, "get [Symbol.species]"));
|
|
185
|
+
}
|
|
186
186
|
} arrayInit;
|
|
@@ -1,112 +1,112 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <chrono>
|
|
4
|
-
#include "types.hpp"
|
|
5
|
-
#include "values/non_values.hpp"
|
|
6
|
-
#include "values/object.hpp"
|
|
7
|
-
#include "values/function.hpp"
|
|
8
|
-
#include "utils/operators.hpp"
|
|
9
|
-
#include "exception.hpp"
|
|
10
|
-
#include "utils/log_any_value/log_any_value.hpp"
|
|
11
|
-
|
|
12
|
-
#include <cmath>
|
|
13
|
-
#include <sstream>
|
|
14
|
-
#include <iomanip>
|
|
15
|
-
|
|
16
|
-
static std::map<std::string, std::chrono::steady_clock::time_point> timers = {};
|
|
17
|
-
|
|
18
|
-
auto logFn = jspp::AnyValue::make_function([](
|
|
19
|
-
{
|
|
20
|
-
for (size_t i = 0; i < args.size(); ++i)
|
|
21
|
-
{
|
|
22
|
-
std::cout << jspp::LogAnyValue::to_log_string(args[i]);
|
|
23
|
-
if (i < args.size() - 1)
|
|
24
|
-
std::cout << " ";
|
|
25
|
-
}
|
|
26
|
-
std::cout << "\n" << std::flush;
|
|
27
|
-
return jspp::Constants::UNDEFINED; }, "log");
|
|
28
|
-
auto warnFn = jspp::AnyValue::make_function([](
|
|
29
|
-
{
|
|
30
|
-
std::cerr << "\033[33m";
|
|
31
|
-
for (size_t i = 0; i < args.size(); ++i)
|
|
32
|
-
{
|
|
33
|
-
std::cout << jspp::LogAnyValue::to_log_string(args[i]);
|
|
34
|
-
if (i < args.size() - 1)
|
|
35
|
-
std::cout << " ";
|
|
36
|
-
}
|
|
37
|
-
std::cerr << "\033[0m" << "\n" << std::flush; // reset
|
|
38
|
-
return jspp::Constants::UNDEFINED; }, "warn");
|
|
39
|
-
auto errorFn = jspp::AnyValue::make_function([](
|
|
40
|
-
{
|
|
41
|
-
std::cerr << "\033[31m";
|
|
42
|
-
for (size_t i = 0; i < args.size(); ++i)
|
|
43
|
-
{
|
|
44
|
-
std::cout << jspp::LogAnyValue::to_log_string(args[i]);
|
|
45
|
-
if (i < args.size() - 1)
|
|
46
|
-
std::cout << " ";
|
|
47
|
-
}
|
|
48
|
-
std::cerr << "\033[0m" << "\n" << std::flush; // reset
|
|
49
|
-
return jspp::Constants::UNDEFINED; }, "error");
|
|
50
|
-
auto timeFn = jspp::AnyValue::make_function([](
|
|
51
|
-
{
|
|
52
|
-
auto start = std::chrono::steady_clock::now(); // capture immediately
|
|
53
|
-
auto key_str = args.size() > 0 ? args[0].to_std_string() : "default";
|
|
54
|
-
timers[key_str] = start;
|
|
55
|
-
return jspp::Constants::UNDEFINED; }, "time");
|
|
56
|
-
|
|
57
|
-
// helper to format duration in ms -> ms/s/m/h with nice precision
|
|
58
|
-
static auto format_duration = [](double ms) -> std::string
|
|
59
|
-
{
|
|
60
|
-
std::ostringstream ss;
|
|
61
|
-
if (ms < 1000.0)
|
|
62
|
-
{
|
|
63
|
-
ss << std::fixed << std::setprecision(4) << ms << "ms";
|
|
64
|
-
return ss.str();
|
|
65
|
-
}
|
|
66
|
-
double total_s = ms / 1000.0;
|
|
67
|
-
if (ms < 60000.0)
|
|
68
|
-
{ // less than a minute -> show seconds
|
|
69
|
-
ss << std::fixed << std::setprecision(4) << total_s << "s";
|
|
70
|
-
return ss.str();
|
|
71
|
-
}
|
|
72
|
-
if (ms < 3600000.0)
|
|
73
|
-
{ // less than an hour -> show minutes + seconds
|
|
74
|
-
int minutes = static_cast<int>(ms / 60000.0);
|
|
75
|
-
double seconds = (ms - minutes * 60000.0) / 1000.0;
|
|
76
|
-
ss << minutes << "m " << std::fixed << std::setprecision(4) << seconds << "s";
|
|
77
|
-
return ss.str();
|
|
78
|
-
}
|
|
79
|
-
// hours, minutes, seconds
|
|
80
|
-
int hours = static_cast<int>(ms / 3600000.0);
|
|
81
|
-
int minutes = static_cast<int>((ms - hours * 3600000.0) / 60000.0);
|
|
82
|
-
double seconds = (ms - hours * 3600000.0 - minutes * 60000.0) / 1000.0;
|
|
83
|
-
ss << hours << "h " << minutes << "m " << std::fixed << std::setprecision(4) << seconds << "s";
|
|
84
|
-
return ss.str();
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
auto timeEndFn = jspp::AnyValue::make_function([](
|
|
88
|
-
{
|
|
89
|
-
auto end = std::chrono::steady_clock::now(); // capture immediately
|
|
90
|
-
auto key_str = args.size() > 0 ? args[0].to_std_string() : "default";
|
|
91
|
-
auto it = timers.find(key_str);
|
|
92
|
-
if (it != timers.end())
|
|
93
|
-
{
|
|
94
|
-
std::chrono::duration<double, std::milli> duration = end - it->second;
|
|
95
|
-
double ms = duration.count();
|
|
96
|
-
std::string formatted = format_duration(ms);
|
|
97
|
-
std::cout << "\033[90m" << "[" << format_duration(ms) << "] " << "\033[0m" << key_str << "\n";
|
|
98
|
-
timers.erase(it); // remove the timer after ending it
|
|
99
|
-
}
|
|
100
|
-
else
|
|
101
|
-
{
|
|
102
|
-
std::cout << "Timer '" << key_str << "' does not exist." << "\n";
|
|
103
|
-
}
|
|
104
|
-
return jspp::Constants::UNDEFINED; }, "timeEnd");
|
|
105
|
-
|
|
106
|
-
inline auto console = jspp::AnyValue::make_object({
|
|
107
|
-
{"log", logFn},
|
|
108
|
-
{"warn", warnFn},
|
|
109
|
-
{"error", errorFn},
|
|
110
|
-
{"time", timeFn},
|
|
111
|
-
{"timeEnd", timeEndFn},
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <chrono>
|
|
4
|
+
#include "types.hpp"
|
|
5
|
+
#include "values/non_values.hpp"
|
|
6
|
+
#include "values/object.hpp"
|
|
7
|
+
#include "values/function.hpp"
|
|
8
|
+
#include "utils/operators.hpp"
|
|
9
|
+
#include "exception.hpp"
|
|
10
|
+
#include "utils/log_any_value/log_any_value.hpp"
|
|
11
|
+
|
|
12
|
+
#include <cmath>
|
|
13
|
+
#include <sstream>
|
|
14
|
+
#include <iomanip>
|
|
15
|
+
|
|
16
|
+
static std::map<std::string, std::chrono::steady_clock::time_point> timers = {};
|
|
17
|
+
|
|
18
|
+
auto logFn = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args)
|
|
19
|
+
{
|
|
20
|
+
for (size_t i = 0; i < args.size(); ++i)
|
|
21
|
+
{
|
|
22
|
+
std::cout << jspp::LogAnyValue::to_log_string(args[i]);
|
|
23
|
+
if (i < args.size() - 1)
|
|
24
|
+
std::cout << " ";
|
|
25
|
+
}
|
|
26
|
+
std::cout << "\n" << std::flush;
|
|
27
|
+
return jspp::Constants::UNDEFINED; }, "log");
|
|
28
|
+
auto warnFn = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args)
|
|
29
|
+
{
|
|
30
|
+
std::cerr << "\033[33m";
|
|
31
|
+
for (size_t i = 0; i < args.size(); ++i)
|
|
32
|
+
{
|
|
33
|
+
std::cout << jspp::LogAnyValue::to_log_string(args[i]);
|
|
34
|
+
if (i < args.size() - 1)
|
|
35
|
+
std::cout << " ";
|
|
36
|
+
}
|
|
37
|
+
std::cerr << "\033[0m" << "\n" << std::flush; // reset
|
|
38
|
+
return jspp::Constants::UNDEFINED; }, "warn");
|
|
39
|
+
auto errorFn = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args)
|
|
40
|
+
{
|
|
41
|
+
std::cerr << "\033[31m";
|
|
42
|
+
for (size_t i = 0; i < args.size(); ++i)
|
|
43
|
+
{
|
|
44
|
+
std::cout << jspp::LogAnyValue::to_log_string(args[i]);
|
|
45
|
+
if (i < args.size() - 1)
|
|
46
|
+
std::cout << " ";
|
|
47
|
+
}
|
|
48
|
+
std::cerr << "\033[0m" << "\n" << std::flush; // reset
|
|
49
|
+
return jspp::Constants::UNDEFINED; }, "error");
|
|
50
|
+
auto timeFn = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args)
|
|
51
|
+
{
|
|
52
|
+
auto start = std::chrono::steady_clock::now(); // capture immediately
|
|
53
|
+
auto key_str = args.size() > 0 ? args[0].to_std_string() : "default";
|
|
54
|
+
timers[key_str] = start;
|
|
55
|
+
return jspp::Constants::UNDEFINED; }, "time");
|
|
56
|
+
|
|
57
|
+
// helper to format duration in ms -> ms/s/m/h with nice precision
|
|
58
|
+
static auto format_duration = [](double ms) -> std::string
|
|
59
|
+
{
|
|
60
|
+
std::ostringstream ss;
|
|
61
|
+
if (ms < 1000.0)
|
|
62
|
+
{
|
|
63
|
+
ss << std::fixed << std::setprecision(4) << ms << "ms";
|
|
64
|
+
return ss.str();
|
|
65
|
+
}
|
|
66
|
+
double total_s = ms / 1000.0;
|
|
67
|
+
if (ms < 60000.0)
|
|
68
|
+
{ // less than a minute -> show seconds
|
|
69
|
+
ss << std::fixed << std::setprecision(4) << total_s << "s";
|
|
70
|
+
return ss.str();
|
|
71
|
+
}
|
|
72
|
+
if (ms < 3600000.0)
|
|
73
|
+
{ // less than an hour -> show minutes + seconds
|
|
74
|
+
int minutes = static_cast<int>(ms / 60000.0);
|
|
75
|
+
double seconds = (ms - minutes * 60000.0) / 1000.0;
|
|
76
|
+
ss << minutes << "m " << std::fixed << std::setprecision(4) << seconds << "s";
|
|
77
|
+
return ss.str();
|
|
78
|
+
}
|
|
79
|
+
// hours, minutes, seconds
|
|
80
|
+
int hours = static_cast<int>(ms / 3600000.0);
|
|
81
|
+
int minutes = static_cast<int>((ms - hours * 3600000.0) / 60000.0);
|
|
82
|
+
double seconds = (ms - hours * 3600000.0 - minutes * 60000.0) / 1000.0;
|
|
83
|
+
ss << hours << "h " << minutes << "m " << std::fixed << std::setprecision(4) << seconds << "s";
|
|
84
|
+
return ss.str();
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
auto timeEndFn = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args)
|
|
88
|
+
{
|
|
89
|
+
auto end = std::chrono::steady_clock::now(); // capture immediately
|
|
90
|
+
auto key_str = args.size() > 0 ? args[0].to_std_string() : "default";
|
|
91
|
+
auto it = timers.find(key_str);
|
|
92
|
+
if (it != timers.end())
|
|
93
|
+
{
|
|
94
|
+
std::chrono::duration<double, std::milli> duration = end - it->second;
|
|
95
|
+
double ms = duration.count();
|
|
96
|
+
std::string formatted = format_duration(ms);
|
|
97
|
+
std::cout << "\033[90m" << "[" << format_duration(ms) << "] " << "\033[0m" << key_str << "\n";
|
|
98
|
+
timers.erase(it); // remove the timer after ending it
|
|
99
|
+
}
|
|
100
|
+
else
|
|
101
|
+
{
|
|
102
|
+
std::cout << "Timer '" << key_str << "' does not exist." << "\n";
|
|
103
|
+
}
|
|
104
|
+
return jspp::Constants::UNDEFINED; }, "timeEnd");
|
|
105
|
+
|
|
106
|
+
inline auto console = jspp::AnyValue::make_object({
|
|
107
|
+
{"log", logFn},
|
|
108
|
+
{"warn", warnFn},
|
|
109
|
+
{"error", errorFn},
|
|
110
|
+
{"time", timeFn},
|
|
111
|
+
{"timeEnd", timeEndFn},
|
|
112
112
|
});
|