@ugo-studio/jspp 0.2.5 → 0.2.7

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 (54) hide show
  1. package/README.md +51 -36
  2. package/dist/analysis/scope.js +7 -0
  3. package/dist/analysis/typeAnalyzer.js +96 -43
  4. package/dist/ast/symbols.js +34 -24
  5. package/dist/cli/args.js +59 -0
  6. package/dist/cli/colors.js +9 -0
  7. package/dist/cli/file-utils.js +20 -0
  8. package/dist/cli/index.js +160 -0
  9. package/dist/cli/spinner.js +55 -0
  10. package/dist/core/codegen/class-handlers.js +8 -8
  11. package/dist/core/codegen/control-flow-handlers.js +19 -9
  12. package/dist/core/codegen/declaration-handlers.js +30 -10
  13. package/dist/core/codegen/expression-handlers.js +649 -161
  14. package/dist/core/codegen/function-handlers.js +107 -103
  15. package/dist/core/codegen/helpers.js +61 -14
  16. package/dist/core/codegen/index.js +13 -9
  17. package/dist/core/codegen/literal-handlers.js +4 -2
  18. package/dist/core/codegen/statement-handlers.js +147 -55
  19. package/dist/core/codegen/visitor.js +22 -2
  20. package/dist/core/constants.js +16 -0
  21. package/dist/core/error.js +58 -0
  22. package/dist/index.js +6 -3
  23. package/package.json +3 -3
  24. package/src/prelude/any_value.hpp +89 -59
  25. package/src/prelude/any_value_access.hpp +1 -1
  26. package/src/prelude/any_value_helpers.hpp +85 -43
  27. package/src/prelude/index.hpp +1 -0
  28. package/src/prelude/library/array.hpp +3 -2
  29. package/src/prelude/scheduler.hpp +144 -144
  30. package/src/prelude/types.hpp +8 -8
  31. package/src/prelude/utils/access.hpp +62 -6
  32. package/src/prelude/utils/assignment_operators.hpp +14 -14
  33. package/src/prelude/utils/log_any_value/array.hpp +0 -15
  34. package/src/prelude/utils/log_any_value/object.hpp +12 -10
  35. package/src/prelude/utils/log_any_value/primitives.hpp +2 -0
  36. package/src/prelude/utils/operators.hpp +117 -474
  37. package/src/prelude/utils/operators_primitive.hpp +337 -0
  38. package/src/prelude/values/helpers/array.hpp +4 -4
  39. package/src/prelude/values/helpers/async_iterator.hpp +2 -2
  40. package/src/prelude/values/helpers/function.hpp +3 -3
  41. package/src/prelude/values/helpers/iterator.hpp +2 -2
  42. package/src/prelude/values/helpers/object.hpp +3 -3
  43. package/src/prelude/values/helpers/promise.hpp +1 -1
  44. package/src/prelude/values/helpers/string.hpp +1 -1
  45. package/src/prelude/values/helpers/symbol.hpp +1 -1
  46. package/src/prelude/values/prototypes/array.hpp +1125 -853
  47. package/src/prelude/values/prototypes/async_iterator.hpp +32 -14
  48. package/src/prelude/values/prototypes/function.hpp +30 -18
  49. package/src/prelude/values/prototypes/iterator.hpp +40 -17
  50. package/src/prelude/values/prototypes/number.hpp +119 -62
  51. package/src/prelude/values/prototypes/object.hpp +10 -4
  52. package/src/prelude/values/prototypes/promise.hpp +167 -109
  53. package/src/prelude/values/prototypes/string.hpp +407 -231
  54. package/src/prelude/values/prototypes/symbol.hpp +45 -23
@@ -40,74 +40,93 @@ namespace jspp
40
40
  private:
41
41
  uint64_t storage;
42
42
 
43
- static constexpr uint64_t TAG_BASE = 0xFFFC000000000000ULL;
44
- static constexpr uint64_t TAG_POINTER = 0xFFFC000000000000ULL;
45
- static constexpr uint64_t TAG_BOOLEAN = 0xFFFD000000000000ULL;
46
- static constexpr uint64_t TAG_SPECIAL = 0xFFFE000000000000ULL;
47
-
48
- static constexpr uint64_t VAL_UNDEFINED = 0x1ULL;
49
- static constexpr uint64_t VAL_NULL = 0x2ULL;
43
+ static constexpr uint64_t TAG_BASE = 0xFFFC000000000000ULL;
44
+ static constexpr uint64_t TAG_POINTER = 0xFFFC000000000000ULL;
45
+ static constexpr uint64_t TAG_BOOLEAN = 0xFFFD000000000000ULL;
46
+ static constexpr uint64_t TAG_SPECIAL = 0xFFFE000000000000ULL;
47
+
48
+ static constexpr uint64_t VAL_UNDEFINED = 0x1ULL;
49
+ static constexpr uint64_t VAL_NULL = 0x2ULL;
50
50
  static constexpr uint64_t VAL_UNINITIALIZED = 0x3ULL;
51
51
 
52
- static constexpr uint64_t TAG_MASK = 0xFFFF000000000000ULL;
53
- static constexpr uint64_t PAYLOAD_MASK = 0x0000FFFFFFFFFFFFULL;
52
+ static constexpr uint64_t TAG_MASK = 0xFFFF000000000000ULL;
53
+ static constexpr uint64_t PAYLOAD_MASK = 0x0000FFFFFFFFFFFFULL;
54
54
 
55
- inline bool has_tag(uint64_t tag) const noexcept {
55
+ inline bool has_tag(uint64_t tag) const noexcept
56
+ {
56
57
  return (storage & TAG_MASK) == tag;
57
58
  }
58
59
 
59
60
  public:
60
- inline HeapObject* get_ptr() const noexcept {
61
- return reinterpret_cast<HeapObject*>(storage & PAYLOAD_MASK);
61
+ inline HeapObject *get_ptr() const noexcept
62
+ {
63
+ return reinterpret_cast<HeapObject *>(storage & PAYLOAD_MASK);
62
64
  }
63
65
 
64
66
  // default ctor (Undefined)
65
67
  AnyValue() noexcept : storage(TAG_SPECIAL | VAL_UNDEFINED) {}
66
68
 
67
- explicit AnyValue(double d) noexcept {
69
+ explicit AnyValue(double d) noexcept
70
+ {
68
71
  std::memcpy(&storage, &d, 8);
69
- if (storage >= TAG_BASE) [[unlikely]] {
72
+ if (storage >= TAG_BASE) [[unlikely]]
73
+ {
70
74
  storage = 0x7FF8000000000000ULL;
71
75
  }
72
76
  }
73
77
 
74
78
  explicit AnyValue(bool b) noexcept : storage(TAG_BOOLEAN | (b ? 1 : 0)) {}
75
79
 
76
- AnyValue(const AnyValue &other) noexcept : storage(other.storage) {
77
- if (is_heap_object()) get_ptr()->ref();
80
+ AnyValue(const AnyValue &other) noexcept : storage(other.storage)
81
+ {
82
+ if (is_heap_object())
83
+ get_ptr()->ref();
78
84
  }
79
85
 
80
- AnyValue(AnyValue &&other) noexcept : storage(other.storage) {
86
+ AnyValue(AnyValue &&other) noexcept : storage(other.storage)
87
+ {
81
88
  other.storage = TAG_SPECIAL | VAL_UNDEFINED;
82
89
  }
83
90
 
84
- AnyValue &operator=(const AnyValue &other) noexcept {
85
- if (this != &other) {
86
- if (is_heap_object()) get_ptr()->deref();
91
+ AnyValue &operator=(const AnyValue &other) noexcept
92
+ {
93
+ if (this != &other)
94
+ {
95
+ if (is_heap_object())
96
+ get_ptr()->deref();
87
97
  storage = other.storage;
88
- if (is_heap_object()) get_ptr()->ref();
98
+ if (is_heap_object())
99
+ get_ptr()->ref();
89
100
  }
90
101
  return *this;
91
102
  }
92
103
 
93
- AnyValue &operator=(AnyValue &&other) noexcept {
94
- if (this != &other) {
95
- if (is_heap_object()) get_ptr()->deref();
104
+ AnyValue &operator=(AnyValue &&other) noexcept
105
+ {
106
+ if (this != &other)
107
+ {
108
+ if (is_heap_object())
109
+ get_ptr()->deref();
96
110
  storage = other.storage;
97
111
  other.storage = TAG_SPECIAL | VAL_UNDEFINED;
98
112
  }
99
113
  return *this;
100
114
  }
101
115
 
102
- ~AnyValue() {
103
- if (is_heap_object()) get_ptr()->deref();
116
+ ~AnyValue()
117
+ {
118
+ if (is_heap_object())
119
+ get_ptr()->deref();
104
120
  }
105
121
 
106
122
  // Assignments
107
- AnyValue &operator=(double val) noexcept {
108
- if (is_heap_object()) get_ptr()->deref();
123
+ AnyValue &operator=(double val) noexcept
124
+ {
125
+ if (is_heap_object())
126
+ get_ptr()->deref();
109
127
  std::memcpy(&storage, &val, 8);
110
- if (storage >= TAG_BASE) [[unlikely]] {
128
+ if (storage >= TAG_BASE) [[unlikely]]
129
+ {
111
130
  storage = 0x7FF8000000000000ULL;
112
131
  }
113
132
  return *this;
@@ -173,6 +192,7 @@ namespace jspp
173
192
  static AnyValue make_array(std::vector<AnyValue> &&dense) noexcept;
174
193
  static AnyValue make_array_with_proto(std::span<const AnyValue> dense, AnyValue proto) noexcept;
175
194
  static AnyValue make_array_with_proto(const std::vector<AnyValue> &dense, AnyValue proto) noexcept;
195
+ static AnyValue make_array_with_proto(std::vector<AnyValue> &&dense, AnyValue proto) noexcept;
176
196
  static AnyValue make_function(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt, bool is_constructor = true) noexcept;
177
197
  static AnyValue make_class(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt) noexcept;
178
198
  static AnyValue make_generator(const JsFunctionCallable &call, const std::optional<std::string> &name = std::nullopt) noexcept;
@@ -188,12 +208,14 @@ namespace jspp
188
208
 
189
209
  static AnyValue from_symbol(JsSymbol *sym) noexcept;
190
210
  static AnyValue from_string(JsString *str) noexcept;
211
+ static AnyValue from_promise(JsPromise &&promise) noexcept;
191
212
  static AnyValue from_iterator(JsIterator<AnyValue> &&iterator) noexcept;
192
213
  static AnyValue from_iterator_ref(JsIterator<AnyValue> *iterator) noexcept;
193
214
  static AnyValue from_async_iterator(JsAsyncIterator<AnyValue> &&iterator) noexcept;
194
215
 
195
216
  // internal factory for wrapping raw heap object pointers
196
- static AnyValue from_ptr(HeapObject* ptr) noexcept {
217
+ static AnyValue from_ptr(HeapObject *ptr) noexcept
218
+ {
197
219
  AnyValue v;
198
220
  v.storage = TAG_POINTER | reinterpret_cast<uint64_t>(ptr);
199
221
  ptr->ref();
@@ -204,13 +226,20 @@ namespace jspp
204
226
  static AnyValue resolve_property_for_write(AnyValue &val, const AnyValue &thisVal, const AnyValue &new_val, const std::string &propName);
205
227
 
206
228
  // TYPE CHECKERS AND ACCESSORS ---------------------------------------
207
- JsType get_type() const noexcept {
208
- if (is_number()) return JsType::Number;
209
- if (is_heap_object()) return get_ptr()->get_heap_type();
210
- if (is_boolean()) return JsType::Boolean;
211
- if (is_undefined()) return JsType::Undefined;
212
- if (is_null()) return JsType::Null;
213
- if (is_uninitialized()) return JsType::Uninitialized;
229
+ inline JsType get_type() const noexcept
230
+ {
231
+ if (is_number())
232
+ return JsType::Number;
233
+ if (is_heap_object())
234
+ return get_ptr()->get_heap_type();
235
+ if (is_boolean())
236
+ return JsType::Boolean;
237
+ if (is_undefined())
238
+ return JsType::Undefined;
239
+ if (is_null())
240
+ return JsType::Null;
241
+ if (is_uninitialized())
242
+ return JsType::Uninitialized;
214
243
  return JsType::Undefined;
215
244
  }
216
245
 
@@ -230,18 +259,18 @@ namespace jspp
230
259
  inline bool is_data_descriptor() const noexcept { return is_heap_object() && get_ptr()->get_heap_type() == JsType::DataDescriptor; }
231
260
  inline bool is_accessor_descriptor() const noexcept { return is_heap_object() && get_ptr()->get_heap_type() == JsType::AccessorDescriptor; }
232
261
  inline bool is_async_iterator() const noexcept { return is_heap_object() && get_ptr()->get_heap_type() == JsType::AsyncIterator; }
233
-
234
- JsString* as_string() const noexcept;
235
- JsObject* as_object() const noexcept;
236
- JsArray* as_array() const noexcept;
237
- JsFunction* as_function() const noexcept;
238
- JsSymbol* as_symbol() const noexcept;
239
- JsPromise* as_promise() const noexcept;
240
- JsIterator<AnyValue>* as_iterator() const noexcept;
241
- JsAsyncIterator<AnyValue>* as_async_iterator() const noexcept;
242
- DataDescriptor* as_data_descriptor() const noexcept;
243
- AccessorDescriptor* as_accessor_descriptor() const noexcept;
244
-
262
+
263
+ JsString *as_string() const noexcept;
264
+ JsObject *as_object() const noexcept;
265
+ JsArray *as_array() const noexcept;
266
+ JsFunction *as_function() const noexcept;
267
+ JsSymbol *as_symbol() const noexcept;
268
+ JsPromise *as_promise() const noexcept;
269
+ JsIterator<AnyValue> *as_iterator() const noexcept;
270
+ JsAsyncIterator<AnyValue> *as_async_iterator() const noexcept;
271
+ DataDescriptor *as_data_descriptor() const noexcept;
272
+ AccessorDescriptor *as_accessor_descriptor() const noexcept;
273
+
245
274
  bool is_generator() const noexcept;
246
275
 
247
276
  double as_double() const noexcept
@@ -258,50 +287,51 @@ namespace jspp
258
287
  auto operator co_await() const;
259
288
 
260
289
  bool has_property(const std::string &key) const;
261
- bool has_property(const char* key) const { return has_property(std::string(key)); }
290
+ bool has_property(const char *key) const { return has_property(std::string(key)); }
262
291
 
263
292
  AnyValue get_own_property(const std::string &key) const;
264
- AnyValue get_own_property(const char* key) const { return get_own_property(std::string(key)); }
293
+ AnyValue get_own_property(const char *key) const { return get_own_property(std::string(key)); }
265
294
  AnyValue get_own_property(uint32_t idx) const;
266
295
  AnyValue get_own_property(int idx) const { return get_own_property(static_cast<uint32_t>(idx)); }
267
296
  AnyValue get_own_property(const AnyValue &key) const;
268
297
 
269
298
  AnyValue get_property_with_receiver(const std::string &key, const AnyValue &receiver) const;
270
- AnyValue get_property_with_receiver(const char* key, const AnyValue &receiver) const { return get_property_with_receiver(std::string(key), receiver); }
299
+ AnyValue get_property_with_receiver(const char *key, const AnyValue &receiver) const { return get_property_with_receiver(std::string(key), receiver); }
271
300
 
272
301
  AnyValue set_own_property(const std::string &key, const AnyValue &value) const;
273
- AnyValue set_own_property(const char* key, const AnyValue &value) const { return set_own_property(std::string(key), value); }
302
+ AnyValue set_own_property(const char *key, const AnyValue &value) const { return set_own_property(std::string(key), value); }
274
303
  AnyValue set_own_property(uint32_t idx, const AnyValue &value) const;
275
304
  AnyValue set_own_property(int idx, const AnyValue &value) const { return set_own_property(static_cast<uint32_t>(idx), value); }
276
305
  AnyValue set_own_property(const AnyValue &key, const AnyValue &value) const;
277
306
 
278
307
  AnyValue call_own_property(const std::string &key, std::span<const AnyValue> args) const;
279
- AnyValue call_own_property(const char* key, std::span<const AnyValue> args) const { return call_own_property(std::string(key), args); }
308
+ AnyValue call_own_property(const char *key, std::span<const AnyValue> args) const { return call_own_property(std::string(key), args); }
280
309
  AnyValue call_own_property(uint32_t idx, std::span<const AnyValue> args) const;
281
310
  AnyValue call_own_property(int idx, std::span<const AnyValue> args) const { return call_own_property(static_cast<uint32_t>(idx), args); }
282
311
  AnyValue call_own_property(const AnyValue &key, std::span<const AnyValue> args) const;
283
312
 
284
313
  void define_data_property(const std::string &key, const AnyValue &value);
285
- void define_data_property(const char* key, const AnyValue &value) { define_data_property(std::string(key), value); }
314
+ void define_data_property(const char *key, const AnyValue &value) { define_data_property(std::string(key), value); }
286
315
  void define_data_property(const AnyValue &key, const AnyValue &value);
287
316
  void define_data_property(const std::string &key, const AnyValue &value, bool writable, bool enumerable, bool configurable);
288
- void define_data_property(const char* key, const AnyValue &value, bool writable, bool enumerable, bool configurable) { define_data_property(std::string(key), value, writable, enumerable, configurable); }
317
+ void define_data_property(const char *key, const AnyValue &value, bool writable, bool enumerable, bool configurable) { define_data_property(std::string(key), value, writable, enumerable, configurable); }
289
318
 
290
319
  void define_getter(const std::string &key, const AnyValue &getter);
291
- void define_getter(const char* key, const AnyValue &getter) { define_getter(std::string(key), getter); }
320
+ void define_getter(const char *key, const AnyValue &getter) { define_getter(std::string(key), getter); }
292
321
  void define_getter(const AnyValue &key, const AnyValue &getter);
293
322
 
294
323
  void define_setter(const std::string &key, const AnyValue &setter);
295
- void define_setter(const char* key, const AnyValue &setter) { define_setter(std::string(key), setter); }
324
+ void define_setter(const char *key, const AnyValue &setter) { define_setter(std::string(key), setter); }
296
325
  void define_setter(const AnyValue &key, const AnyValue &setter);
297
326
 
298
327
  AnyValue call(const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt) const;
328
+ AnyValue optional_call(const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt) const;
299
329
  AnyValue construct(std::span<const AnyValue> args, const std::optional<std::string> &name = std::nullopt) const;
300
330
  void set_prototype(const AnyValue &proto);
301
331
  std::string to_std_string() const;
302
332
 
303
333
  inline uint64_t get_storage() const noexcept { return storage; }
304
- inline void* get_raw_ptr() const noexcept { return reinterpret_cast<void*>(storage & PAYLOAD_MASK); }
334
+ inline void *get_raw_ptr() const noexcept { return reinterpret_cast<void *>(storage & PAYLOAD_MASK); }
305
335
  };
306
336
 
307
337
  struct AnyValueAwaiter
@@ -86,7 +86,7 @@ namespace jspp
86
86
  return as_string()->get_property(key, receiver);
87
87
  case JsType::Number:
88
88
  {
89
- auto proto_it = NumberPrototypes::get(key, as_double());
89
+ auto proto_it = NumberPrototypes::get(key);
90
90
  if (proto_it.has_value())
91
91
  {
92
92
  return AnyValue::resolve_property_for_read(proto_it.value(), receiver, key);
@@ -16,128 +16,166 @@
16
16
  namespace jspp
17
17
  {
18
18
  // --- AnyValue GETTERS ---
19
- inline JsString* AnyValue::as_string() const noexcept { return static_cast<JsString*>(get_ptr()); }
20
- inline JsObject* AnyValue::as_object() const noexcept { return static_cast<JsObject*>(get_ptr()); }
21
- inline JsArray* AnyValue::as_array() const noexcept { return static_cast<JsArray*>(get_ptr()); }
22
- inline JsFunction* AnyValue::as_function() const noexcept { return static_cast<JsFunction*>(get_ptr()); }
23
- inline JsSymbol* AnyValue::as_symbol() const noexcept { return static_cast<JsSymbol*>(get_ptr()); }
24
- inline JsPromise* AnyValue::as_promise() const noexcept { return static_cast<JsPromise*>(get_ptr()); }
25
- inline JsIterator<AnyValue>* AnyValue::as_iterator() const noexcept { return static_cast<JsIterator<AnyValue>*>(get_ptr()); }
26
- inline JsAsyncIterator<AnyValue>* AnyValue::as_async_iterator() const noexcept { return static_cast<JsAsyncIterator<AnyValue>*>(get_ptr()); }
27
- inline DataDescriptor* AnyValue::as_data_descriptor() const noexcept { return static_cast<DataDescriptor*>(get_ptr()); }
28
- inline AccessorDescriptor* AnyValue::as_accessor_descriptor() const noexcept { return static_cast<AccessorDescriptor*>(get_ptr()); }
29
-
19
+ inline JsString *AnyValue::as_string() const noexcept { return static_cast<JsString *>(get_ptr()); }
20
+ inline JsObject *AnyValue::as_object() const noexcept { return static_cast<JsObject *>(get_ptr()); }
21
+ inline JsArray *AnyValue::as_array() const noexcept { return static_cast<JsArray *>(get_ptr()); }
22
+ inline JsFunction *AnyValue::as_function() const noexcept { return static_cast<JsFunction *>(get_ptr()); }
23
+ inline JsSymbol *AnyValue::as_symbol() const noexcept { return static_cast<JsSymbol *>(get_ptr()); }
24
+ inline JsPromise *AnyValue::as_promise() const noexcept { return static_cast<JsPromise *>(get_ptr()); }
25
+ inline JsIterator<AnyValue> *AnyValue::as_iterator() const noexcept { return static_cast<JsIterator<AnyValue> *>(get_ptr()); }
26
+ inline JsAsyncIterator<AnyValue> *AnyValue::as_async_iterator() const noexcept { return static_cast<JsAsyncIterator<AnyValue> *>(get_ptr()); }
27
+ inline DataDescriptor *AnyValue::as_data_descriptor() const noexcept { return static_cast<DataDescriptor *>(get_ptr()); }
28
+ inline AccessorDescriptor *AnyValue::as_accessor_descriptor() const noexcept { return static_cast<AccessorDescriptor *>(get_ptr()); }
29
+
30
30
  inline bool AnyValue::is_generator() const noexcept { return is_function() && as_function()->is_generator; }
31
31
 
32
32
  // --- AnyValue FACTORIES ---
33
- inline AnyValue AnyValue::make_string(const std::string &raw_s) noexcept {
33
+ inline AnyValue AnyValue::make_string(const std::string &raw_s) noexcept
34
+ {
34
35
  return from_ptr(new JsString(raw_s));
35
36
  }
36
- inline AnyValue AnyValue::make_object(std::initializer_list<std::pair<std::string, AnyValue>> props) noexcept {
37
+ inline AnyValue AnyValue::make_object(std::initializer_list<std::pair<std::string, AnyValue>> props) noexcept
38
+ {
37
39
  return from_ptr(new JsObject(props, make_null()));
38
40
  }
39
- inline AnyValue AnyValue::make_object(const std::map<std::string, AnyValue> &props) noexcept {
41
+ inline AnyValue AnyValue::make_object(const std::map<std::string, AnyValue> &props) noexcept
42
+ {
40
43
  return from_ptr(new JsObject(props, make_null()));
41
44
  }
42
- inline AnyValue AnyValue::make_object_with_proto(std::initializer_list<std::pair<std::string, AnyValue>> props, AnyValue proto) noexcept {
45
+ inline AnyValue AnyValue::make_object_with_proto(std::initializer_list<std::pair<std::string, AnyValue>> props, AnyValue proto) noexcept
46
+ {
43
47
  return from_ptr(new JsObject(props, proto));
44
48
  }
45
- inline AnyValue AnyValue::make_object_with_proto(const std::map<std::string, AnyValue> &props, AnyValue proto) noexcept {
49
+ inline AnyValue AnyValue::make_object_with_proto(const std::map<std::string, AnyValue> &props, AnyValue proto) noexcept
50
+ {
46
51
  return from_ptr(new JsObject(props, proto));
47
52
  }
48
- inline AnyValue AnyValue::make_array(std::span<const AnyValue> dense) noexcept {
53
+ inline AnyValue AnyValue::make_array(std::span<const AnyValue> dense) noexcept
54
+ {
49
55
  std::vector<AnyValue> vec;
50
56
  vec.reserve(dense.size());
51
- for (const auto &item : dense) vec.push_back(item);
57
+ for (const auto &item : dense)
58
+ vec.push_back(item);
52
59
  return from_ptr(new JsArray(std::move(vec)));
53
60
  }
54
- inline AnyValue AnyValue::make_array(const std::vector<AnyValue> &dense) noexcept {
61
+ inline AnyValue AnyValue::make_array(const std::vector<AnyValue> &dense) noexcept
62
+ {
55
63
  return from_ptr(new JsArray(dense));
56
64
  }
57
- inline AnyValue AnyValue::make_array(std::vector<AnyValue> &&dense) noexcept {
65
+ inline AnyValue AnyValue::make_array(std::vector<AnyValue> &&dense) noexcept
66
+ {
58
67
  return from_ptr(new JsArray(std::move(dense)));
59
68
  }
60
- inline AnyValue AnyValue::make_array_with_proto(std::span<const AnyValue> dense, AnyValue proto) noexcept {
69
+ inline AnyValue AnyValue::make_array_with_proto(std::span<const AnyValue> dense, AnyValue proto) noexcept
70
+ {
61
71
  auto arr = new JsArray();
62
72
  arr->dense.reserve(dense.size());
63
- for (const auto &item : dense) arr->dense.push_back(item);
73
+ for (const auto &item : dense)
74
+ arr->dense.push_back(item);
64
75
  arr->length = dense.size();
65
76
  arr->proto = proto;
66
77
  return from_ptr(arr);
67
78
  }
68
- inline AnyValue AnyValue::make_array_with_proto(const std::vector<AnyValue> &dense, AnyValue proto) noexcept {
79
+ inline AnyValue AnyValue::make_array_with_proto(const std::vector<AnyValue> &dense, AnyValue proto) noexcept
80
+ {
69
81
  auto arr = new JsArray(dense);
70
82
  arr->proto = proto;
71
83
  return from_ptr(arr);
72
84
  }
73
- inline AnyValue AnyValue::make_function(const JsFunctionCallable &call, const std::optional<std::string> &name, bool is_constructor) noexcept {
85
+ inline AnyValue AnyValue::make_array_with_proto(std::vector<AnyValue> &&dense, AnyValue proto) noexcept
86
+ {
87
+ auto arr = new JsArray(std::move(dense));
88
+ arr->proto = proto;
89
+ return from_ptr(arr);
90
+ }
91
+ inline AnyValue AnyValue::make_function(const JsFunctionCallable &call, const std::optional<std::string> &name, bool is_constructor) noexcept
92
+ {
74
93
  auto v = from_ptr(new JsFunction(call, name, {}, false, is_constructor));
75
94
  auto proto = make_object({});
76
95
  proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
77
96
  v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
78
97
  return v;
79
98
  }
80
- inline AnyValue AnyValue::make_class(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
99
+ inline AnyValue AnyValue::make_class(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept
100
+ {
81
101
  auto v = from_ptr(new JsFunction(call, name, {}, true));
82
102
  auto proto = make_object({});
83
103
  proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
84
104
  v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
85
105
  return v;
86
106
  }
87
- inline AnyValue AnyValue::make_generator(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
107
+ inline AnyValue AnyValue::make_generator(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept
108
+ {
88
109
  auto v = from_ptr(new JsFunction(call, true, name));
89
110
  auto proto = make_object({});
90
111
  proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
91
112
  v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
92
113
  return v;
93
114
  }
94
- inline AnyValue AnyValue::make_async_function(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
115
+ inline AnyValue AnyValue::make_async_function(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept
116
+ {
95
117
  auto v = from_ptr(new JsFunction(call, false, true, name));
96
118
  auto proto = make_object({});
97
119
  proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
98
120
  v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
99
121
  return v;
100
122
  }
101
- inline AnyValue AnyValue::make_async_generator(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
123
+ inline AnyValue AnyValue::make_async_generator(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept
124
+ {
102
125
  auto v = from_ptr(new JsFunction(call, true, true, name));
103
126
  auto proto = make_object({});
104
127
  proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
105
128
  v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
106
129
  return v;
107
130
  }
108
- inline AnyValue AnyValue::make_symbol(const std::string &description) noexcept {
131
+ inline AnyValue AnyValue::make_symbol(const std::string &description) noexcept
132
+ {
109
133
  return from_ptr(new JsSymbol(description));
110
134
  }
111
- inline AnyValue AnyValue::make_promise(const JsPromise &promise) noexcept {
135
+ inline AnyValue AnyValue::make_promise(const JsPromise &promise) noexcept
136
+ {
112
137
  auto p = new JsPromise();
113
138
  *p = promise;
114
139
  return from_ptr(p);
115
140
  }
116
- inline AnyValue AnyValue::make_data_descriptor(AnyValue value, bool writable, bool enumerable, bool configurable) noexcept {
141
+ inline AnyValue AnyValue::make_data_descriptor(AnyValue value, bool writable, bool enumerable, bool configurable) noexcept
142
+ {
117
143
  return from_ptr(new DataDescriptor(value, writable, enumerable, configurable));
118
144
  }
119
145
  inline AnyValue AnyValue::make_accessor_descriptor(const std::optional<std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>> &get,
120
- const std::optional<std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>> &set,
121
- bool enumerable,
122
- bool configurable) noexcept {
146
+ const std::optional<std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>> &set,
147
+ bool enumerable,
148
+ bool configurable) noexcept
149
+ {
123
150
  return from_ptr(new AccessorDescriptor(get, set, enumerable, configurable));
124
151
  }
125
152
 
126
- inline AnyValue AnyValue::from_symbol(JsSymbol *sym) noexcept {
153
+ inline AnyValue AnyValue::from_symbol(JsSymbol *sym) noexcept
154
+ {
127
155
  return from_ptr(sym);
128
156
  }
129
- inline AnyValue AnyValue::from_string(JsString *str) noexcept {
157
+ inline AnyValue AnyValue::from_string(JsString *str) noexcept
158
+ {
130
159
  return from_ptr(str);
131
160
  }
132
161
 
133
- inline AnyValue AnyValue::from_iterator(JsIterator<AnyValue> &&iterator) noexcept {
162
+ inline AnyValue AnyValue::from_promise(JsPromise &&promise) noexcept
163
+ {
164
+ auto it = new JsPromise(std::move(promise));
165
+ return from_ptr(it);
166
+ }
167
+
168
+ inline AnyValue AnyValue::from_iterator(JsIterator<AnyValue> &&iterator) noexcept
169
+ {
134
170
  auto it = new JsIterator<AnyValue>(std::move(iterator));
135
171
  return from_ptr(it);
136
172
  }
137
- inline AnyValue AnyValue::from_iterator_ref(JsIterator<AnyValue> *iterator) noexcept {
138
- return from_ptr(iterator);
173
+ inline AnyValue AnyValue::from_iterator_ref(JsIterator<AnyValue> *iterator) noexcept
174
+ {
175
+ return from_ptr(iterator);
139
176
  }
140
- inline AnyValue AnyValue::from_async_iterator(JsAsyncIterator<AnyValue> &&iterator) noexcept {
177
+ inline AnyValue AnyValue::from_async_iterator(JsAsyncIterator<AnyValue> &&iterator) noexcept
178
+ {
141
179
  auto it = new JsAsyncIterator<AnyValue>(std::move(iterator));
142
180
  return from_ptr(it);
143
181
  }
@@ -285,10 +323,14 @@ namespace jspp
285
323
  inline AnyValue AnyValue::call(const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &expr) const
286
324
  {
287
325
  if (!is_function())
288
- {
289
326
  throw Exception::make_exception(expr.value_or(to_std_string()) + " is not a function", "TypeError");
290
- }
291
- return as_function()->call(thisVal, args);
327
+ return as_function()->call(thisVal, args);
328
+ }
329
+ inline AnyValue AnyValue::optional_call(const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &expr) const
330
+ {
331
+ if (is_null() || is_undefined())
332
+ return Constants::UNDEFINED;
333
+ return as_function()->call(thisVal, args);
292
334
  }
293
335
 
294
336
  // AnyValue::construct implementation
@@ -44,6 +44,7 @@
44
44
  #include "values/helpers/string.hpp"
45
45
 
46
46
  // utilities
47
+ #include "utils/operators_primitive.hpp"
47
48
  #include "utils/operators.hpp"
48
49
  #include "utils/assignment_operators.hpp"
49
50
  #include "utils/access.hpp"
@@ -13,8 +13,9 @@ inline auto Array = jspp::AnyValue::make_class([](const jspp::AnyValue &thisVal,
13
13
  throw jspp::Exception::make_exception("Invalid array length", "RangeError");
14
14
  }
15
15
  auto arr = jspp::AnyValue::make_array(std::vector<jspp::AnyValue>());
16
- arr.as_array()->length = static_cast<uint64_t>(len);
17
- arr.as_array()->dense.resize(static_cast<size_t>(len), jspp::Constants::UNINITIALIZED);
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);
18
19
  return arr;
19
20
  }
20
21
  std::vector<jspp::AnyValue> elements;