@ugo-studio/jspp 0.2.8 → 0.2.9

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 (41) hide show
  1. package/dist/analysis/typeAnalyzer.js +42 -27
  2. package/dist/core/codegen/class-handlers.js +2 -2
  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 +42 -22
  11. package/package.json +1 -1
  12. package/scripts/precompile-headers.ts +13 -5
  13. package/src/prelude/any_value.hpp +26 -25
  14. package/src/prelude/any_value_access.hpp +7 -7
  15. package/src/prelude/any_value_defines.hpp +17 -17
  16. package/src/prelude/any_value_helpers.hpp +16 -7
  17. package/src/prelude/library/array.hpp +7 -7
  18. package/src/prelude/library/console.hpp +5 -5
  19. package/src/prelude/library/error.hpp +3 -3
  20. package/src/prelude/library/function.hpp +1 -1
  21. package/src/prelude/library/math.hpp +38 -38
  22. package/src/prelude/library/object.hpp +16 -16
  23. package/src/prelude/library/performance.hpp +1 -1
  24. package/src/prelude/library/process.hpp +1 -1
  25. package/src/prelude/library/promise.hpp +8 -8
  26. package/src/prelude/library/symbol.hpp +3 -3
  27. package/src/prelude/library/timer.hpp +4 -4
  28. package/src/prelude/types.hpp +4 -4
  29. package/src/prelude/utils/access.hpp +24 -6
  30. package/src/prelude/utils/operators.hpp +7 -0
  31. package/src/prelude/values/async_iterator.hpp +5 -3
  32. package/src/prelude/values/function.hpp +3 -3
  33. package/src/prelude/values/helpers/async_iterator.hpp +7 -3
  34. package/src/prelude/values/helpers/function.hpp +10 -10
  35. package/src/prelude/values/helpers/iterator.hpp +39 -2
  36. package/src/prelude/values/helpers/promise.hpp +9 -9
  37. package/src/prelude/values/helpers/string.hpp +17 -3
  38. package/src/prelude/values/iterator.hpp +32 -5
  39. package/src/prelude/values/promise.hpp +7 -7
  40. package/src/prelude/values/prototypes/array.hpp +41 -41
  41. package/src/prelude/values/prototypes/iterator.hpp +128 -1
@@ -60,84 +60,84 @@ struct MathInit {
60
60
  // --- Methods ---
61
61
 
62
62
  // Math.abs(x)
63
- Math.define_data_property("abs", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
63
+ Math.define_data_property("abs", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
64
64
  return AnyValue::make_number(std::abs(GetArgAsDouble(args, 0)));
65
65
  }, "abs"));
66
66
 
67
67
  // Math.acos(x)
68
- Math.define_data_property("acos", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
68
+ Math.define_data_property("acos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
69
69
  return MathFunc1(args, std::acos);
70
70
  }, "acos"));
71
71
 
72
72
  // Math.acosh(x)
73
- Math.define_data_property("acosh", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
73
+ Math.define_data_property("acosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
74
74
  return MathFunc1(args, std::acosh);
75
75
  }, "acosh"));
76
76
 
77
77
  // Math.asin(x)
78
- Math.define_data_property("asin", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
78
+ Math.define_data_property("asin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
79
79
  return MathFunc1(args, std::asin);
80
80
  }, "asin"));
81
81
 
82
82
  // Math.asinh(x)
83
- Math.define_data_property("asinh", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
83
+ Math.define_data_property("asinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
84
84
  return MathFunc1(args, std::asinh);
85
85
  }, "asinh"));
86
86
 
87
87
  // Math.atan(x)
88
- Math.define_data_property("atan", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
88
+ Math.define_data_property("atan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
89
89
  return MathFunc1(args, std::atan);
90
90
  }, "atan"));
91
91
 
92
92
  // Math.atan2(y, x)
93
- Math.define_data_property("atan2", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
93
+ Math.define_data_property("atan2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
94
94
  return MathFunc2(args, std::atan2);
95
95
  }, "atan2"));
96
96
 
97
97
  // Math.atanh(x)
98
- Math.define_data_property("atanh", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
98
+ Math.define_data_property("atanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
99
99
  return MathFunc1(args, std::atanh);
100
100
  }, "atanh"));
101
101
 
102
102
  // Math.cbrt(x)
103
- Math.define_data_property("cbrt", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
103
+ Math.define_data_property("cbrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
104
104
  return MathFunc1(args, std::cbrt);
105
105
  }, "cbrt"));
106
106
 
107
107
  // Math.ceil(x)
108
- Math.define_data_property("ceil", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
108
+ Math.define_data_property("ceil", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
109
109
  return MathFunc1(args, std::ceil);
110
110
  }, "ceil"));
111
111
 
112
112
  // Math.clz32(x)
113
- Math.define_data_property("clz32", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
113
+ Math.define_data_property("clz32", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
114
114
  uint32_t val = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
115
115
  if (val == 0) return AnyValue::make_number(32);
116
116
  return AnyValue::make_number(std::countl_zero(val));
117
117
  }, "clz32"));
118
118
 
119
119
  // Math.cos(x)
120
- Math.define_data_property("cos", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
120
+ Math.define_data_property("cos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
121
121
  return MathFunc1(args, std::cos);
122
122
  }, "cos"));
123
123
 
124
124
  // Math.cosh(x)
125
- Math.define_data_property("cosh", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
125
+ Math.define_data_property("cosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
126
126
  return MathFunc1(args, std::cosh);
127
127
  }, "cosh"));
128
128
 
129
129
  // Math.exp(x)
130
- Math.define_data_property("exp", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
130
+ Math.define_data_property("exp", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
131
131
  return MathFunc1(args, std::exp);
132
132
  }, "exp"));
133
133
 
134
134
  // Math.expm1(x)
135
- Math.define_data_property("expm1", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
135
+ Math.define_data_property("expm1", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
136
136
  return MathFunc1(args, std::expm1);
137
137
  }, "expm1"));
138
138
 
139
139
  // Math.f16round(x)
140
- Math.define_data_property("f16round", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
140
+ Math.define_data_property("f16round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
141
141
  double d = GetArgAsDouble(args, 0);
142
142
  #if defined(__STDCPP_FLOAT16_T__)
143
143
  return AnyValue::make_number(static_cast<double>(static_cast<std::float16_t>(d)));
@@ -150,18 +150,18 @@ struct MathInit {
150
150
  }, "f16round"));
151
151
 
152
152
  // Math.floor(x)
153
- Math.define_data_property("floor", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
153
+ Math.define_data_property("floor", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
154
154
  return MathFunc1(args, std::floor);
155
155
  }, "floor"));
156
156
 
157
157
  // Math.fround(x)
158
- Math.define_data_property("fround", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
158
+ Math.define_data_property("fround", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
159
159
  double d = GetArgAsDouble(args, 0);
160
160
  return AnyValue::make_number(static_cast<double>(static_cast<float>(d)));
161
161
  }, "fround"));
162
162
 
163
163
  // Math.hypot(...args)
164
- Math.define_data_property("hypot", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
164
+ Math.define_data_property("hypot", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
165
165
  double result = 0;
166
166
  for (const auto& arg : args) {
167
167
  double val = Operators_Private::ToNumber(arg);
@@ -172,34 +172,34 @@ struct MathInit {
172
172
  }, "hypot"));
173
173
 
174
174
  // Math.imul(x, y)
175
- Math.define_data_property("imul", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
175
+ Math.define_data_property("imul", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
176
176
  int32_t a = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
177
177
  int32_t b = Operators_Private::ToInt32(args.size() < 2 ? Constants::UNDEFINED : args[1]);
178
178
  return AnyValue::make_number(a * b);
179
179
  }, "imul"));
180
180
 
181
181
  // Math.log(x)
182
- Math.define_data_property("log", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
182
+ Math.define_data_property("log", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
183
183
  return MathFunc1(args, std::log);
184
184
  }, "log"));
185
185
 
186
186
  // Math.log10(x)
187
- Math.define_data_property("log10", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
187
+ Math.define_data_property("log10", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
188
188
  return MathFunc1(args, std::log10);
189
189
  }, "log10"));
190
190
 
191
191
  // Math.log1p(x)
192
- Math.define_data_property("log1p", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
192
+ Math.define_data_property("log1p", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
193
193
  return MathFunc1(args, std::log1p);
194
194
  }, "log1p"));
195
195
 
196
196
  // Math.log2(x)
197
- Math.define_data_property("log2", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
197
+ Math.define_data_property("log2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
198
198
  return MathFunc1(args, std::log2);
199
199
  }, "log2"));
200
200
 
201
201
  // Math.max(...args)
202
- Math.define_data_property("max", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
202
+ Math.define_data_property("max", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
203
203
  double maxVal = -std::numeric_limits<double>::infinity();
204
204
  for (const auto& arg : args) {
205
205
  double val = Operators_Private::ToNumber(arg);
@@ -210,7 +210,7 @@ struct MathInit {
210
210
  }, "max"));
211
211
 
212
212
  // Math.min(...args)
213
- Math.define_data_property("min", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
213
+ Math.define_data_property("min", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
214
214
  double minVal = std::numeric_limits<double>::infinity();
215
215
  for (const auto& arg : args) {
216
216
  double val = Operators_Private::ToNumber(arg);
@@ -221,24 +221,24 @@ struct MathInit {
221
221
  }, "min"));
222
222
 
223
223
  // Math.pow(base, exponent)
224
- Math.define_data_property("pow", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
224
+ Math.define_data_property("pow", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
225
225
  return MathFunc2(args, std::pow);
226
226
  }, "pow"));
227
227
 
228
228
  // Math.random()
229
- Math.define_data_property("random", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
229
+ Math.define_data_property("random", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
230
230
  return AnyValue::make_number(dis(gen));
231
231
  }, "random"));
232
232
 
233
233
  // Math.round(x)
234
- Math.define_data_property("round", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
234
+ Math.define_data_property("round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
235
235
  double d = GetArgAsDouble(args, 0);
236
236
  if (std::isnan(d)) return AnyValue::make_nan();
237
237
  return AnyValue::make_number(std::floor(d + 0.5));
238
238
  }, "round"));
239
239
 
240
240
  // Math.sign(x)
241
- Math.define_data_property("sign", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
241
+ Math.define_data_property("sign", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
242
242
  double d = GetArgAsDouble(args, 0);
243
243
  if (std::isnan(d)) return AnyValue::make_nan();
244
244
  if (d == 0) return AnyValue::make_number(d);
@@ -246,25 +246,25 @@ struct MathInit {
246
246
  }, "sign"));
247
247
 
248
248
  // Math.sin(x)
249
- Math.define_data_property("sin", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
249
+ Math.define_data_property("sin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
250
250
  return MathFunc1(args, std::sin);
251
251
  }, "sin"));
252
252
 
253
253
  // Math.sinh(x)
254
- Math.define_data_property("sinh", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
254
+ Math.define_data_property("sinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
255
255
  return MathFunc1(args, std::sinh);
256
256
  }, "sinh"));
257
257
 
258
258
  // Math.sqrt(x)
259
- Math.define_data_property("sqrt", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
259
+ Math.define_data_property("sqrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
260
260
  return MathFunc1(args, std::sqrt);
261
261
  }, "sqrt"));
262
262
 
263
263
  // Math.sumPrecise(iterable)
264
- Math.define_data_property("sumPrecise", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
264
+ Math.define_data_property("sumPrecise", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
265
265
  if (args.empty()) throw Exception::make_exception("Math.sumPrecise requires an iterable", "TypeError");
266
266
 
267
- auto iterObj = jspp::Access::get_object_value_iterator(args[0], "iterable");
267
+ auto iterObj = jspp::Access::get_object_iterator(args[0], "iterable");
268
268
  auto nextFunc = iterObj.get_own_property("next");
269
269
 
270
270
  double sum = 0;
@@ -291,17 +291,17 @@ struct MathInit {
291
291
  }, "sumPrecise"));
292
292
 
293
293
  // Math.tan(x)
294
- Math.define_data_property("tan", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
294
+ Math.define_data_property("tan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
295
295
  return MathFunc1(args, std::tan);
296
296
  }, "tan"));
297
297
 
298
298
  // Math.tanh(x)
299
- Math.define_data_property("tanh", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
299
+ Math.define_data_property("tanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
300
300
  return MathFunc1(args, std::tanh);
301
301
  }, "tanh"));
302
302
 
303
303
  // Math.trunc(x)
304
- Math.define_data_property("trunc", AnyValue::make_function([](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
304
+ Math.define_data_property("trunc", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
305
305
  return MathFunc1(args, std::trunc);
306
306
  }, "trunc"));
307
307
  }
@@ -6,7 +6,7 @@
6
6
  #include "exception.hpp"
7
7
 
8
8
  // Define Object constructor
9
- inline auto Object = jspp::AnyValue::make_class([](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
9
+ inline auto Object = jspp::AnyValue::make_class([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
10
10
  {
11
11
  if (args.empty() || args[0].is_undefined() || args[0].is_null()) {
12
12
  return jspp::AnyValue::make_object({});
@@ -23,7 +23,7 @@ struct ObjectInit
23
23
  ObjectInit()
24
24
  {
25
25
  // Object.keys(obj)
26
- Object.define_data_property("keys", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
26
+ Object.define_data_property("keys", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
27
27
  {
28
28
  if (args.empty()) throw jspp::Exception::make_exception("Object.keys called on non-object", "TypeError");
29
29
  auto obj = args[0];
@@ -37,7 +37,7 @@ struct ObjectInit
37
37
  return jspp::AnyValue::make_array(std::move(keyValues)); }, "keys"));
38
38
 
39
39
  // Object.values(obj)
40
- Object.define_data_property("values", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
40
+ Object.define_data_property("values", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
41
41
  {
42
42
  if (args.empty()) throw jspp::Exception::make_exception("Object.values called on non-object", "TypeError");
43
43
  auto obj = args[0];
@@ -51,7 +51,7 @@ struct ObjectInit
51
51
  return jspp::AnyValue::make_array(std::move(values)); }, "values"));
52
52
 
53
53
  // Object.entries(obj)
54
- Object.define_data_property("entries", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
54
+ Object.define_data_property("entries", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
55
55
  {
56
56
  if (args.empty()) throw jspp::Exception::make_exception("Object.entries called on non-object", "TypeError");
57
57
  auto obj = args[0];
@@ -68,7 +68,7 @@ struct ObjectInit
68
68
  return jspp::AnyValue::make_array(std::move(entries)); }, "entries"));
69
69
 
70
70
  // Object.assign(target, ...sources)
71
- Object.define_data_property("assign", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
71
+ Object.define_data_property("assign", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
72
72
  {
73
73
  if (args.empty()) throw jspp::Exception::make_exception("Cannot convert undefined or null to object", "TypeError");
74
74
  auto target = args[0];
@@ -87,7 +87,7 @@ struct ObjectInit
87
87
  return target; }, "assign"));
88
88
 
89
89
  // Object.is(value1, value2)
90
- Object.define_data_property("is", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
90
+ Object.define_data_property("is", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
91
91
  {
92
92
  jspp::AnyValue v1 = args.size() > 0 ? args[0] : jspp::Constants::UNDEFINED;
93
93
  jspp::AnyValue v2 = args.size() > 1 ? args[1] : jspp::Constants::UNDEFINED;
@@ -105,7 +105,7 @@ struct ObjectInit
105
105
  return jspp::is_strictly_equal_to(v1, v2); }, "is"));
106
106
 
107
107
  // Object.getPrototypeOf(obj)
108
- Object.define_data_property("getPrototypeOf", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
108
+ Object.define_data_property("getPrototypeOf", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
109
109
  {
110
110
  if (args.empty()) throw jspp::Exception::make_exception("Object.getPrototypeOf called on non-object", "TypeError");
111
111
  auto obj = args[0];
@@ -123,7 +123,7 @@ struct ObjectInit
123
123
  return jspp::Constants::Null; }, "getPrototypeOf"));
124
124
 
125
125
  // Object.setPrototypeOf(obj, proto)
126
- Object.define_data_property("setPrototypeOf", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
126
+ Object.define_data_property("setPrototypeOf", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
127
127
  {
128
128
  if (args.size() < 2) throw jspp::Exception::make_exception("Object.setPrototypeOf requires at least 2 arguments", "TypeError");
129
129
  auto obj = args[0];
@@ -144,7 +144,7 @@ struct ObjectInit
144
144
  return obj; }, "setPrototypeOf"));
145
145
 
146
146
  // Object.create(proto, [propertiesObject])
147
- Object.define_data_property("create", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
147
+ Object.define_data_property("create", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
148
148
  {
149
149
  if (args.empty()) throw jspp::Exception::make_exception("Object prototype may only be an Object or null", "TypeError");
150
150
  auto proto = args[0];
@@ -162,7 +162,7 @@ struct ObjectInit
162
162
  return newObj; }, "create"));
163
163
 
164
164
  // Object.defineProperty(obj, prop, descriptor)
165
- Object.define_data_property("defineProperty", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
165
+ Object.define_data_property("defineProperty", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
166
166
  {
167
167
  if (args.size() < 3) throw jspp::Exception::make_exception("Object.defineProperty requires 3 arguments", "TypeError");
168
168
  auto obj = args[0];
@@ -202,16 +202,16 @@ struct ObjectInit
202
202
 
203
203
  if (obj.is_object()) {
204
204
  auto o_ptr = obj.as_object();
205
- std::optional<std::function<jspp::AnyValue(const jspp::AnyValue &, std::span<const jspp::AnyValue>)>> getFunc;
206
- std::optional<std::function<jspp::AnyValue(const jspp::AnyValue &, std::span<const jspp::AnyValue>)>> setFunc;
205
+ std::optional<std::function<jspp::AnyValue(jspp::AnyValue, std::span<const jspp::AnyValue>)>> getFunc;
206
+ std::optional<std::function<jspp::AnyValue(jspp::AnyValue, std::span<const jspp::AnyValue>)>> setFunc;
207
207
 
208
208
  if (getter.is_function()) {
209
- getFunc = [getter](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
209
+ getFunc = [getter](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
210
210
  return getter.call(thisVal, args);
211
211
  };
212
212
  }
213
213
  if (setter.is_function()) {
214
- setFunc = [setter](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
214
+ setFunc = [setter](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
215
215
  return setter.call(thisVal, args);
216
216
  };
217
217
  }
@@ -230,7 +230,7 @@ struct ObjectInit
230
230
  return obj; }, "defineProperty"));
231
231
 
232
232
  // Object.hasOwn(obj, prop)
233
- Object.define_data_property("hasOwn", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
233
+ Object.define_data_property("hasOwn", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
234
234
  {
235
235
  if (args.empty()) throw jspp::Exception::make_exception("Object.hasOwn called on non-object", "TypeError");
236
236
  auto obj = args[0];
@@ -255,7 +255,7 @@ struct ObjectInit
255
255
 
256
256
  // Object.prototype.hasOwnProperty
257
257
  auto proto = Object.get_own_property("prototype");
258
- proto.define_data_property("hasOwnProperty", jspp::AnyValue::make_function([](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
258
+ proto.define_data_property("hasOwnProperty", jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
259
259
  {
260
260
  std::string prop = args.size() > 0 ? args[0].to_std_string() : "undefined";
261
261
  if (thisVal.is_object()) return jspp::AnyValue::make_boolean(thisVal.as_object()->shape->get_offset(prop).has_value());
@@ -13,7 +13,7 @@ inline auto performance = jspp::AnyValue::make_object({
13
13
  // [C++14 Feature] Generalized Lambda Capture
14
14
  // We initialize 'startTime' RIGHT HERE inside the [].
15
15
  // It acts like a private variable stored inside this specific function.
16
- [startTime = std::chrono::steady_clock::now()](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue>)
16
+ [startTime = std::chrono::steady_clock::now()](jspp::AnyValue, std::span<const jspp::AnyValue>)
17
17
  {
18
18
  // We calculate the diff against the captured startTime
19
19
  std::chrono::duration<double, std::milli> duration =
@@ -13,7 +13,7 @@ inline auto process = jspp::AnyValue::make_object({
13
13
  {"argv", jspp::AnyValue::make_array(std::vector<jspp::AnyValue>{})},
14
14
  {"env", jspp::AnyValue::make_object({})},
15
15
  {"platform", jspp::AnyValue::make_string(JSPP_PLATFORM)},
16
- {"exit", jspp::AnyValue::make_function([](const jspp::AnyValue&, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
16
+ {"exit", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
17
17
  int code = 0;
18
18
  if (!args.empty() && args[0].is_number()) {
19
19
  code = static_cast<int>(args[0].as_double());
@@ -5,7 +5,7 @@
5
5
  #include "any_value.hpp"
6
6
  #include "exception.hpp"
7
7
 
8
- inline auto Promise = jspp::AnyValue::make_function([](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
8
+ inline auto Promise = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
9
9
  {
10
10
  if (args.empty() || !args[0].is_function())
11
11
  {
@@ -17,14 +17,14 @@ inline auto Promise = jspp::AnyValue::make_function([](const jspp::AnyValue &thi
17
17
  auto state = promise.state; // Share state
18
18
 
19
19
  // resolve function
20
- auto resolveFn = jspp::AnyValue::make_function([state](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
20
+ auto resolveFn = jspp::AnyValue::make_function([state](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
21
21
  {
22
22
  jspp::JsPromise p; p.state = state;
23
23
  p.resolve(args.empty() ? jspp::Constants::UNDEFINED : args[0]);
24
24
  return jspp::Constants::UNDEFINED; }, "resolve");
25
25
 
26
26
  // reject function
27
- auto rejectFn = jspp::AnyValue::make_function([state](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
27
+ auto rejectFn = jspp::AnyValue::make_function([state](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
28
28
  {
29
29
  jspp::JsPromise p; p.state = state;
30
30
  p.reject(args.empty() ? jspp::Constants::UNDEFINED : args[0]);
@@ -52,21 +52,21 @@ struct PromiseInit
52
52
  PromiseInit()
53
53
  {
54
54
  // Promise.resolve(value)
55
- Promise.define_data_property("resolve", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
55
+ Promise.define_data_property("resolve", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
56
56
  {
57
57
  jspp::JsPromise p;
58
58
  p.resolve(args.empty() ? jspp::Constants::UNDEFINED : args[0]);
59
59
  return jspp::AnyValue::make_promise(p); }, "resolve"));
60
60
 
61
61
  // Promise.reject(reason)
62
- Promise.define_data_property("reject", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
62
+ Promise.define_data_property("reject", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
63
63
  {
64
64
  jspp::JsPromise p;
65
65
  p.reject(args.empty() ? jspp::Constants::UNDEFINED : args[0]);
66
66
  return jspp::AnyValue::make_promise(p); }, "reject"));
67
67
 
68
68
  // Promise.all(iterable)
69
- Promise.define_data_property("all", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
69
+ Promise.define_data_property("all", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
70
70
  {
71
71
  // Handle non-array iterable or empty args
72
72
  if (args.empty() || !args[0].is_array()) {
@@ -93,7 +93,7 @@ struct PromiseInit
93
93
  for (size_t i = 0; i < len; ++i) {
94
94
  jspp::AnyValue item = arr->get_property(static_cast<uint32_t>(i));
95
95
 
96
- auto handleResult = [masterState, results, count, i, rejected](const jspp::AnyValue& res) {
96
+ auto handleResult = [masterState, results, count, i, rejected](jspp::AnyValue res) {
97
97
  if (*rejected) return;
98
98
  (*results)[i] = res;
99
99
  (*count)--;
@@ -103,7 +103,7 @@ struct PromiseInit
103
103
  }
104
104
  };
105
105
 
106
- auto handleReject = [masterState, rejected](const jspp::AnyValue& reason) {
106
+ auto handleReject = [masterState, rejected](jspp::AnyValue reason) {
107
107
  if (*rejected) return;
108
108
  *rejected = true;
109
109
  jspp::JsPromise p; p.state = masterState;
@@ -6,7 +6,7 @@
6
6
  #include "any_value.hpp"
7
7
 
8
8
  // Define Symbol as a function
9
- inline auto Symbol = jspp::AnyValue::make_function([](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
9
+ inline auto Symbol = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
10
10
  {
11
11
  std::string description = "";
12
12
  if (!args.empty() && !args[0].is_undefined()) {
@@ -20,13 +20,13 @@ struct SymbolInit
20
20
  SymbolInit()
21
21
  {
22
22
  // Static methods
23
- Symbol.define_data_property("for", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
23
+ Symbol.define_data_property("for", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
24
24
  {
25
25
  std::string key = "";
26
26
  if (!args.empty()) key = args[0].to_std_string();
27
27
  return jspp::AnyValue::from_symbol(jspp::JsSymbol::for_global(key)); }, "for"));
28
28
 
29
- Symbol.define_data_property("keyFor", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
29
+ Symbol.define_data_property("keyFor", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
30
30
  {
31
31
  if (args.empty() || !args[0].is_symbol()) throw jspp::Exception::make_exception("Symbol.keyFor requires a symbol", "TypeError");
32
32
  auto sym = args[0].as_symbol();
@@ -7,7 +7,7 @@
7
7
  #include "exception.hpp"
8
8
 
9
9
  // setTimeout(callback, delay, ...args)
10
- inline auto setTimeout = jspp::AnyValue::make_function([](const jspp::AnyValue& thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
10
+ inline auto setTimeout = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
11
11
  if (args.empty() || !args[0].is_function()) {
12
12
  throw jspp::Exception::make_exception("Callback must be a function", "TypeError");
13
13
  }
@@ -41,7 +41,7 @@ inline auto setTimeout = jspp::AnyValue::make_function([](const jspp::AnyValue&
41
41
  }, "setTimeout");
42
42
 
43
43
  // clearTimeout(id)
44
- inline auto clearTimeout = jspp::AnyValue::make_function([](const jspp::AnyValue& thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
44
+ inline auto clearTimeout = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
45
45
  if (!args.empty() && args[0].is_number()) {
46
46
  size_t id = static_cast<size_t>(args[0].as_double());
47
47
  jspp::Scheduler::instance().clear_timer(id);
@@ -50,7 +50,7 @@ inline auto clearTimeout = jspp::AnyValue::make_function([](const jspp::AnyValue
50
50
  }, "clearTimeout");
51
51
 
52
52
  // setInterval(callback, delay, ...args)
53
- inline auto setInterval = jspp::AnyValue::make_function([](const jspp::AnyValue& thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
53
+ inline auto setInterval = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
54
54
  if (args.empty() || !args[0].is_function()) {
55
55
  throw jspp::Exception::make_exception("Callback must be a function", "TypeError");
56
56
  }
@@ -83,7 +83,7 @@ inline auto setInterval = jspp::AnyValue::make_function([](const jspp::AnyValue&
83
83
  }, "setInterval");
84
84
 
85
85
  // clearInterval(id)
86
- inline auto clearInterval = jspp::AnyValue::make_function([](const jspp::AnyValue& thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
86
+ inline auto clearInterval = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
87
87
  if (!args.empty() && args[0].is_number()) {
88
88
  size_t id = static_cast<size_t>(args[0].as_double());
89
89
  jspp::Scheduler::instance().clear_timer(id);
@@ -89,10 +89,10 @@ namespace jspp
89
89
  class AnyValue;
90
90
 
91
91
  using JsFunctionCallable = std::variant<
92
- std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>,
93
- std::function<JsIterator<AnyValue>(const AnyValue &, std::span<const AnyValue>)>,
94
- std::function<JsPromise(const AnyValue &, std::span<const AnyValue>)>,
95
- std::function<JsAsyncIterator<AnyValue>(const AnyValue &, std::span<const AnyValue>)>>;
92
+ std::function<AnyValue(AnyValue, std::span<const AnyValue>)>,
93
+ std::function<JsIterator<AnyValue>(AnyValue, std::vector<AnyValue>)>,
94
+ std::function<JsPromise(AnyValue, std::vector<AnyValue>)>,
95
+ std::function<JsAsyncIterator<AnyValue>(AnyValue, std::vector<AnyValue>)>>;
96
96
 
97
97
  // Awaiter for AnyValue
98
98
  struct AnyValueAwaiter;
@@ -83,7 +83,7 @@ namespace jspp
83
83
  }
84
84
 
85
85
  // Helper function to get enumerable own property keys/values of an object
86
- inline std::vector<std::string> get_object_keys(const AnyValue &obj)
86
+ inline std::vector<std::string> get_object_keys(const AnyValue &obj, bool include_symbols = false)
87
87
  {
88
88
  std::vector<std::string> keys;
89
89
 
@@ -95,7 +95,7 @@ namespace jspp
95
95
  if (ptr->deleted_keys.count(key))
96
96
  continue;
97
97
 
98
- if (JsSymbol::is_internal_key(key))
98
+ if (!include_symbols && JsSymbol::is_internal_key(key))
99
99
  continue;
100
100
 
101
101
  auto offset_opt = ptr->shape->get_offset(key);
@@ -125,7 +125,7 @@ namespace jspp
125
125
  auto ptr = obj.as_function();
126
126
  for (const auto &pair : ptr->props)
127
127
  {
128
- if (!JsSymbol::is_internal_key(pair.first))
128
+ if (include_symbols || !JsSymbol::is_internal_key(pair.first))
129
129
  {
130
130
  if (!pair.second.is_data_descriptor() && !pair.second.is_accessor_descriptor())
131
131
  keys.push_back(pair.first);
@@ -155,7 +155,7 @@ namespace jspp
155
155
  return keys;
156
156
  }
157
157
 
158
- inline AnyValue get_object_value_iterator(const AnyValue &obj, const std::string &name)
158
+ inline AnyValue get_object_iterator(const AnyValue &obj, const std::string &name)
159
159
  {
160
160
  if (obj.is_iterator())
161
161
  {
@@ -183,7 +183,7 @@ namespace jspp
183
183
  throw jspp::Exception::make_exception(name + " is not iterable", "TypeError");
184
184
  }
185
185
 
186
- inline AnyValue get_async_object_value_iterator(const AnyValue &obj, const std::string &name)
186
+ inline AnyValue get_object_async_iterator(const AnyValue &obj, const std::string &name)
187
187
  {
188
188
  if (obj.is_async_iterator())
189
189
  return obj;
@@ -362,7 +362,7 @@ namespace jspp
362
362
  }
363
363
  else if (source.is_object() || source.is_function() || source.is_iterator())
364
364
  {
365
- auto iter = get_object_value_iterator(source, "spread target");
365
+ auto iter = get_object_iterator(source, "spread target");
366
366
  auto next_fn = iter.get_own_property("next");
367
367
  while (true)
368
368
  {
@@ -390,5 +390,23 @@ namespace jspp
390
390
  }
391
391
  }
392
392
 
393
+ inline AnyValue get_rest_object(const AnyValue &source, const std::vector<std::string> &excluded_keys)
394
+ {
395
+ if (source.is_null() || source.is_undefined())
396
+ return AnyValue::make_object({});
397
+
398
+ auto result = AnyValue::make_object({});
399
+ auto keys = get_object_keys(source, true);
400
+ std::unordered_set<std::string> excluded(excluded_keys.begin(), excluded_keys.end());
401
+
402
+ for (const auto &key : keys)
403
+ {
404
+ if (excluded.find(key) == excluded.end())
405
+ {
406
+ result.set_own_property(key, source.get_property_with_receiver(key, source));
407
+ }
408
+ }
409
+ return result;
410
+ }
393
411
  }
394
412
  }