@ugo-studio/jspp 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +1 -1
  2. package/dist/cli-utils/args.js +2 -0
  3. package/dist/cli.js +1 -1
  4. package/package.json +3 -2
  5. package/scripts/precompile-headers.ts +110 -0
  6. package/scripts/setup-compiler.ts +63 -0
  7. package/src/prelude/any_value.hpp +185 -391
  8. package/src/prelude/any_value_access.hpp +170 -190
  9. package/src/prelude/any_value_defines.hpp +12 -12
  10. package/src/prelude/any_value_helpers.hpp +208 -26
  11. package/src/prelude/exception.hpp +27 -31
  12. package/src/prelude/exception_helpers.hpp +53 -49
  13. package/src/prelude/index.hpp +9 -4
  14. package/src/prelude/library/array.hpp +4 -9
  15. package/src/prelude/library/console.hpp +112 -112
  16. package/src/prelude/library/error.hpp +8 -8
  17. package/src/prelude/library/math.hpp +3 -3
  18. package/src/prelude/library/object.hpp +12 -24
  19. package/src/prelude/library/promise.hpp +1 -1
  20. package/src/prelude/library/symbol.hpp +1 -1
  21. package/src/prelude/library/timer.hpp +3 -3
  22. package/src/prelude/types.hpp +178 -130
  23. package/src/prelude/utils/access.hpp +338 -378
  24. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  25. package/src/prelude/utils/log_any_value/log_any_value.hpp +1 -1
  26. package/src/prelude/utils/operators.hpp +20 -82
  27. package/src/prelude/utils/well_known_symbols.hpp +14 -15
  28. package/src/prelude/values/array.hpp +5 -3
  29. package/src/prelude/values/async_iterator.hpp +3 -1
  30. package/src/prelude/values/descriptors.hpp +15 -3
  31. package/src/prelude/values/function.hpp +5 -9
  32. package/src/prelude/values/helpers/array.hpp +208 -219
  33. package/src/prelude/values/helpers/async_iterator.hpp +7 -11
  34. package/src/prelude/values/helpers/function.hpp +12 -17
  35. package/src/prelude/values/helpers/iterator.hpp +108 -107
  36. package/src/prelude/values/helpers/object.hpp +104 -109
  37. package/src/prelude/values/helpers/promise.hpp +185 -119
  38. package/src/prelude/values/helpers/string.hpp +7 -10
  39. package/src/prelude/values/helpers/symbol.hpp +21 -23
  40. package/src/prelude/values/iterator.hpp +4 -1
  41. package/src/prelude/values/object.hpp +6 -4
  42. package/src/prelude/values/promise.hpp +5 -2
  43. package/src/prelude/values/prototypes/array.hpp +22 -22
  44. package/src/prelude/values/prototypes/async_iterator.hpp +3 -10
  45. package/src/prelude/values/prototypes/iterator.hpp +51 -58
  46. package/src/prelude/values/prototypes/promise.hpp +32 -28
  47. package/src/prelude/values/prototypes/string.hpp +5 -5
  48. package/src/prelude/values/prototypes/symbol.hpp +1 -1
  49. package/src/prelude/values/string.hpp +3 -1
  50. package/src/prelude/values/symbol.hpp +101 -102
@@ -1,190 +1,170 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "any_value.hpp"
5
-
6
- namespace jspp
7
- {
8
- AnyValue AnyValue::get_own_property(const std::string &key) const
9
- {
10
- return get_property_with_receiver(key, *this);
11
- }
12
- bool AnyValue::has_property(const std::string &key) const
13
- {
14
- switch (get_type())
15
- {
16
- case JsType::Object:
17
- return std::get<std::shared_ptr<JsObject>>(storage)->has_property(key);
18
- case JsType::Array:
19
- return std::get<std::shared_ptr<JsArray>>(storage)->has_property(key);
20
- case JsType::Function:
21
- return std::get<std::shared_ptr<JsFunction>>(storage)->has_property(key);
22
- case JsType::Promise:
23
- // Promises don't have their own props usually, but could.
24
- return false;
25
- case JsType::Iterator:
26
- return false;
27
- case JsType::AsyncIterator:
28
- return false;
29
- case JsType::Symbol:
30
- return false;
31
- case JsType::String:
32
- if (key == "length")
33
- return true;
34
- if (JsArray::is_array_index(key))
35
- {
36
- uint32_t idx = static_cast<uint32_t>(std::stoull(key));
37
- return idx < std::get<std::shared_ptr<JsString>>(storage)->value.length();
38
- }
39
- return false;
40
- case JsType::Number:
41
- return false;
42
- case JsType::Uninitialized:
43
- Exception::throw_uninitialized_reference("#<Object>");
44
- return false;
45
- default:
46
- return false;
47
- }
48
- }
49
- AnyValue AnyValue::get_own_property(uint32_t idx) const
50
- {
51
- switch (storage.index())
52
- {
53
- case 7: // Array
54
- return std::get<std::shared_ptr<JsArray>>(storage)->get_property(idx);
55
- case 5: // String
56
- return std::get<std::shared_ptr<JsString>>(storage)->get_property(idx);
57
- case 4: // Number
58
- return get_own_property(std::to_string(idx));
59
- default:
60
- return get_own_property(std::to_string(idx));
61
- }
62
- }
63
- AnyValue AnyValue::get_own_property(const AnyValue &key) const
64
- {
65
- if (key.is_number() && is_array())
66
- return std::get<std::shared_ptr<JsArray>>(storage)->get_property(key.as_double());
67
- if (key.is_number() && is_string())
68
- return std::get<std::shared_ptr<JsString>>(storage)->get_property(key.as_double());
69
-
70
- // If the key is a Symbol, use its internal key string
71
- if (key.is_symbol())
72
- return get_own_property(key.as_symbol()->key);
73
-
74
- return get_own_property(key.to_std_string());
75
- }
76
-
77
- AnyValue AnyValue::get_property_with_receiver(const std::string &key, const AnyValue &receiver) const
78
- {
79
- switch (get_type())
80
- {
81
- case JsType::Object:
82
- return std::get<std::shared_ptr<JsObject>>(storage)->get_property(key, receiver);
83
- case JsType::Array:
84
- return std::get<std::shared_ptr<JsArray>>(storage)->get_property(key, receiver);
85
- case JsType::Function:
86
- return std::get<std::shared_ptr<JsFunction>>(storage)->get_property(key, receiver);
87
- case JsType::Promise:
88
- return std::get<std::shared_ptr<JsPromise>>(storage)->get_property(key, receiver);
89
- case JsType::Iterator:
90
- return std::get<std::shared_ptr<JsIterator<AnyValue>>>(storage)->get_property(key, receiver);
91
- case JsType::AsyncIterator:
92
- return std::get<std::shared_ptr<JsAsyncIterator<AnyValue>>>(storage)->get_property(key, receiver);
93
- case JsType::Symbol:
94
- return std::get<std::shared_ptr<JsSymbol>>(storage)->get_property(key, receiver);
95
- case JsType::String:
96
- return std::get<std::shared_ptr<JsString>>(storage)->get_property(key, receiver);
97
- case JsType::Number:
98
- {
99
- auto proto_it = NumberPrototypes::get(key, std::get<double>(storage));
100
- if (proto_it.has_value())
101
- {
102
- return AnyValue::resolve_property_for_read(proto_it.value(), receiver, key);
103
- }
104
- return AnyValue::make_undefined();
105
- }
106
- case JsType::Undefined:
107
- throw Exception::make_exception("Cannot read properties of undefined (reading '" + key + "')", "TypeError");
108
- case JsType::Null:
109
- throw Exception::make_exception("Cannot read properties of null (reading '" + key + "')", "TypeError");
110
- case JsType::Uninitialized:
111
- Exception::throw_uninitialized_reference("#<Object>");
112
- default:
113
- return AnyValue::make_undefined();
114
- }
115
- }
116
-
117
- AnyValue AnyValue::set_own_property(const std::string &key, const AnyValue &value) const
118
- {
119
- switch (get_type())
120
- {
121
- case JsType::Object:
122
- return std::get<std::shared_ptr<JsObject>>(storage)->set_property(key, value, *this);
123
- case JsType::Array:
124
- return std::get<std::shared_ptr<JsArray>>(storage)->set_property(key, value, *this);
125
- case JsType::Function:
126
- return std::get<std::shared_ptr<JsFunction>>(storage)->set_property(key, value, *this);
127
- case JsType::Promise:
128
- return std::get<std::shared_ptr<JsPromise>>(storage)->set_property(key, value, *this);
129
- case JsType::Undefined:
130
- throw Exception::make_exception("Cannot set properties of undefined (setting '" + key + "')", "TypeError");
131
- case JsType::Null:
132
- throw Exception::make_exception("Cannot set properties of null (setting '" + key + "')", "TypeError");
133
- default:
134
- return value;
135
- }
136
- }
137
- AnyValue AnyValue::set_own_property(uint32_t idx, const AnyValue &value) const
138
- {
139
- if (is_array())
140
- {
141
- return std::get<std::shared_ptr<JsArray>>(storage)->set_property(idx, value);
142
- }
143
- return set_own_property(std::to_string(idx), value);
144
- }
145
- AnyValue AnyValue::set_own_property(const AnyValue &key, const AnyValue &value) const
146
- {
147
- if (key.is_number() && is_array())
148
- {
149
- return std::get<std::shared_ptr<JsArray>>(storage)->set_property(key.as_double(), value);
150
- }
151
-
152
- // If the key is a Symbol, use its internal key string
153
- if (key.is_symbol())
154
- return set_own_property(key.as_symbol()->key, value);
155
-
156
- return set_own_property(key.to_std_string(), value);
157
- }
158
-
159
- AnyValue AnyValue::call_own_property(const std::string &key, std::span<const AnyValue> args) const
160
- {
161
- return get_own_property(key).call((*this), args, key);
162
- }
163
- AnyValue AnyValue::call_own_property(uint32_t idx, std::span<const AnyValue> args) const
164
- {
165
- switch (storage.index())
166
- {
167
- case 7: // Array
168
- return std::get<std::shared_ptr<JsArray>>(storage)->get_property(idx).call((*this), args, "[" + std::to_string(idx) + "]");
169
- case 5: // String
170
- return std::get<std::shared_ptr<JsString>>(storage)->get_property(idx).call((*this), args, "[" + std::to_string(idx) + "]");
171
- case 4: // Number
172
- return call_own_property(std::to_string(idx), args);
173
- default:
174
- return call_own_property(std::to_string(idx), args);
175
- }
176
- }
177
- AnyValue AnyValue::call_own_property(const AnyValue &key, std::span<const AnyValue> args) const
178
- {
179
- if (key.is_number() && is_array())
180
- return std::get<std::shared_ptr<JsArray>>(storage)->get_property(key.as_double()).call((*this), args, "[" + key.to_std_string() + "]");
181
- if (key.is_number() && is_string())
182
- return std::get<std::shared_ptr<JsString>>(storage)->get_property(key.as_double()).call((*this), args, "[" + key.to_std_string() + "]");
183
-
184
- // If the key is a Symbol, use its internal key string
185
- if (key.is_symbol())
186
- return call_own_property(key.as_symbol()->key, args);
187
-
188
- return call_own_property(key.to_std_string(), args);
189
- }
190
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "any_value.hpp"
5
+
6
+ namespace jspp
7
+ {
8
+ inline AnyValue AnyValue::get_own_property(const std::string &key) const
9
+ {
10
+ return get_property_with_receiver(key, *this);
11
+ }
12
+ inline bool AnyValue::has_property(const std::string &key) const
13
+ {
14
+ switch (get_type())
15
+ {
16
+ case JsType::Object:
17
+ return as_object()->has_property(key);
18
+ case JsType::Array:
19
+ return as_array()->has_property(key);
20
+ case JsType::Function:
21
+ return as_function()->has_property(key);
22
+ case JsType::Promise:
23
+ return false;
24
+ case JsType::Iterator:
25
+ return false;
26
+ case JsType::AsyncIterator:
27
+ return false;
28
+ case JsType::Symbol:
29
+ return false;
30
+ case JsType::String:
31
+ if (key == "length")
32
+ return true;
33
+ if (JsArray::is_array_index(key))
34
+ {
35
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key));
36
+ return idx < as_string()->value.length();
37
+ }
38
+ return false;
39
+ case JsType::Number:
40
+ return false;
41
+ case JsType::Uninitialized:
42
+ Exception::throw_uninitialized_reference("#<Object>");
43
+ return false;
44
+ default:
45
+ return false;
46
+ }
47
+ }
48
+ inline AnyValue AnyValue::get_own_property(uint32_t idx) const
49
+ {
50
+ if (is_array()) return as_array()->get_property(idx);
51
+ if (is_string()) return as_string()->get_property(idx);
52
+ return get_own_property(std::to_string(idx));
53
+ }
54
+ inline AnyValue AnyValue::get_own_property(const AnyValue &key) const
55
+ {
56
+ if (key.is_number() && is_array())
57
+ return as_array()->get_property(key.as_double());
58
+ if (key.is_number() && is_string())
59
+ return as_string()->get_property(key.as_double());
60
+
61
+ if (key.is_symbol())
62
+ return get_own_property(key.as_symbol()->key);
63
+
64
+ return get_own_property(key.to_std_string());
65
+ }
66
+
67
+ inline AnyValue AnyValue::get_property_with_receiver(const std::string &key, const AnyValue &receiver) const
68
+ {
69
+ switch (get_type())
70
+ {
71
+ case JsType::Object:
72
+ return as_object()->get_property(key, receiver);
73
+ case JsType::Array:
74
+ return as_array()->get_property(key, receiver);
75
+ case JsType::Function:
76
+ return as_function()->get_property(key, receiver);
77
+ case JsType::Promise:
78
+ return as_promise()->get_property(key, receiver);
79
+ case JsType::Iterator:
80
+ return static_cast<JsIterator<AnyValue>*>(get_ptr())->get_property(key, receiver);
81
+ case JsType::AsyncIterator:
82
+ return static_cast<JsAsyncIterator<AnyValue>*>(get_ptr())->get_property(key, receiver);
83
+ case JsType::Symbol:
84
+ return as_symbol()->get_property(key, receiver);
85
+ case JsType::String:
86
+ return as_string()->get_property(key, receiver);
87
+ case JsType::Number:
88
+ {
89
+ auto proto_it = NumberPrototypes::get(key, as_double());
90
+ if (proto_it.has_value())
91
+ {
92
+ return AnyValue::resolve_property_for_read(proto_it.value(), receiver, key);
93
+ }
94
+ return Constants::UNDEFINED;
95
+ }
96
+ case JsType::Undefined:
97
+ throw Exception::make_exception("Cannot read properties of undefined (reading '" + key + "')", "TypeError");
98
+ case JsType::Null:
99
+ throw Exception::make_exception("Cannot read properties of null (reading '" + key + "')", "TypeError");
100
+ case JsType::Uninitialized:
101
+ Exception::throw_uninitialized_reference("#<Object>");
102
+ default:
103
+ return Constants::UNDEFINED;
104
+ }
105
+ }
106
+
107
+ inline AnyValue AnyValue::set_own_property(const std::string &key, const AnyValue &value) const
108
+ {
109
+ switch (get_type())
110
+ {
111
+ case JsType::Object:
112
+ return as_object()->set_property(key, value, *this);
113
+ case JsType::Array:
114
+ return as_array()->set_property(key, value, *this);
115
+ case JsType::Function:
116
+ return as_function()->set_property(key, value, *this);
117
+ case JsType::Promise:
118
+ return as_promise()->set_property(key, value, *this);
119
+ case JsType::Undefined:
120
+ throw Exception::make_exception("Cannot set properties of undefined (setting '" + key + "')", "TypeError");
121
+ case JsType::Null:
122
+ throw Exception::make_exception("Cannot set properties of null (setting '" + key + "')", "TypeError");
123
+ default:
124
+ return value;
125
+ }
126
+ }
127
+ inline AnyValue AnyValue::set_own_property(uint32_t idx, const AnyValue &value) const
128
+ {
129
+ if (is_array())
130
+ {
131
+ return as_array()->set_property(idx, value);
132
+ }
133
+ return set_own_property(std::to_string(idx), value);
134
+ }
135
+ inline AnyValue AnyValue::set_own_property(const AnyValue &key, const AnyValue &value) const
136
+ {
137
+ if (key.is_number() && is_array())
138
+ {
139
+ return as_array()->set_property(key.as_double(), value);
140
+ }
141
+
142
+ if (key.is_symbol())
143
+ return set_own_property(key.as_symbol()->key, value);
144
+
145
+ return set_own_property(key.to_std_string(), value);
146
+ }
147
+
148
+ inline AnyValue AnyValue::call_own_property(const std::string &key, std::span<const AnyValue> args) const
149
+ {
150
+ return get_own_property(key).call((*this), args, key);
151
+ }
152
+ inline AnyValue AnyValue::call_own_property(uint32_t idx, std::span<const AnyValue> args) const
153
+ {
154
+ if (is_array()) return as_array()->get_property(idx).call((*this), args, "[" + std::to_string(idx) + "]");
155
+ if (is_string()) return as_string()->get_property(idx).call((*this), args, "[" + std::to_string(idx) + "]");
156
+ return call_own_property(std::to_string(idx), args);
157
+ }
158
+ inline AnyValue AnyValue::call_own_property(const AnyValue &key, std::span<const AnyValue> args) const
159
+ {
160
+ if (key.is_number() && is_array())
161
+ return as_array()->get_property(key.as_double()).call((*this), args, "[" + key.to_std_string() + "]");
162
+ if (key.is_number() && is_string())
163
+ return as_string()->get_property(key.as_double()).call((*this), args, "[" + key.to_std_string() + "]");
164
+
165
+ if (key.is_symbol())
166
+ return call_own_property(key.as_symbol()->key, args);
167
+
168
+ return call_own_property(key.to_std_string(), args);
169
+ }
170
+ }
@@ -11,7 +11,7 @@ namespace jspp
11
11
  {
12
12
  if (is_object())
13
13
  {
14
- auto obj = std::get<std::shared_ptr<JsObject>>(storage);
14
+ auto obj = as_object();
15
15
  auto offset = obj->shape->get_offset(key);
16
16
  if (offset.has_value())
17
17
  {
@@ -25,7 +25,7 @@ namespace jspp
25
25
  }
26
26
  else if (is_function())
27
27
  {
28
- std::get<std::shared_ptr<JsFunction>>(storage)->props[key] = value;
28
+ as_function()->props[key] = value;
29
29
  }
30
30
  }
31
31
 
@@ -46,7 +46,7 @@ namespace jspp
46
46
  {
47
47
  if (is_object())
48
48
  {
49
- auto obj = std::get<std::shared_ptr<JsObject>>(storage);
49
+ auto obj = as_object();
50
50
  auto offset = obj->shape->get_offset(key);
51
51
 
52
52
  if (offset.has_value())
@@ -81,7 +81,7 @@ namespace jspp
81
81
  }
82
82
  else if (is_function())
83
83
  {
84
- auto &props = std::get<std::shared_ptr<JsFunction>>(storage)->props;
84
+ auto &props = as_function()->props;
85
85
  auto it = props.find(key);
86
86
  if (it != props.end() && it->second.is_accessor_descriptor())
87
87
  {
@@ -114,7 +114,7 @@ namespace jspp
114
114
  {
115
115
  if (is_object())
116
116
  {
117
- auto obj = std::get<std::shared_ptr<JsObject>>(storage);
117
+ auto obj = as_object();
118
118
  auto offset = obj->shape->get_offset(key);
119
119
 
120
120
  if (offset.has_value())
@@ -126,7 +126,7 @@ namespace jspp
126
126
  desc->set = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
127
127
  {
128
128
  if (args.empty())
129
- return AnyValue::make_undefined();
129
+ return Constants::UNDEFINED;
130
130
  return setter.call(thisVal, args);
131
131
  };
132
132
  }
@@ -135,7 +135,7 @@ namespace jspp
135
135
  auto setFunc = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
136
136
  {
137
137
  if (args.empty())
138
- return AnyValue::make_undefined();
138
+ return Constants::UNDEFINED;
139
139
  return setter.call(thisVal, args);
140
140
  };
141
141
  obj->storage[offset.value()] = AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true);
@@ -146,7 +146,7 @@ namespace jspp
146
146
  auto setFunc = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
147
147
  {
148
148
  if (args.empty())
149
- return AnyValue::make_undefined();
149
+ return Constants::UNDEFINED;
150
150
  return setter.call(thisVal, args);
151
151
  };
152
152
  obj->shape = obj->shape->transition(key);
@@ -155,7 +155,7 @@ namespace jspp
155
155
  }
156
156
  else if (is_function())
157
157
  {
158
- auto &props = std::get<std::shared_ptr<JsFunction>>(storage)->props;
158
+ auto &props = as_function()->props;
159
159
  auto it = props.find(key);
160
160
  if (it != props.end() && it->second.is_accessor_descriptor())
161
161
  {
@@ -163,7 +163,7 @@ namespace jspp
163
163
  desc->set = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
164
164
  {
165
165
  if (args.empty())
166
- return AnyValue::make_undefined();
166
+ return Constants::UNDEFINED;
167
167
  return setter.call(thisVal, args);
168
168
  };
169
169
  }
@@ -172,7 +172,7 @@ namespace jspp
172
172
  auto setFunc = [setter](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
173
173
  {
174
174
  if (args.empty())
175
- return AnyValue::make_undefined();
175
+ return Constants::UNDEFINED;
176
176
  return setter.call(thisVal, args);
177
177
  };
178
178
  props[key] = AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true);
@@ -187,4 +187,4 @@ namespace jspp
187
187
  else
188
188
  define_setter(key.to_std_string(), setter);
189
189
  }
190
- }
190
+ }