@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,107 +1,108 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/iterator.hpp"
5
- #include "any_value.hpp"
6
-
7
- template <typename T>
8
- std::string jspp::JsIterator<T>::to_std_string() const
9
- {
10
- return "[object Generator]";
11
- }
12
-
13
- template <typename T>
14
- jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::next(const T &val)
15
- {
16
- // If the generator is already finished or invalid, return {undefined, true}
17
- if (!handle || handle.done())
18
- return {std::nullopt, true};
19
-
20
- handle.promise().input_value = val;
21
-
22
- // Resume execution until next co_yield or co_return
23
- handle.resume();
24
-
25
- if (handle.promise().exception_)
26
- {
27
- std::rethrow_exception(handle.promise().exception_);
28
- }
29
-
30
- // If handle.done() is TRUE, we hit co_return (value: X, done: true)
31
- // If handle.done() is FALSE, we hit co_yield (value: X, done: false)
32
- bool is_done = handle.done();
33
-
34
- return {handle.promise().current_value, is_done};
35
- }
36
-
37
- template <typename T>
38
- std::vector<T> jspp::JsIterator<T>::to_vector()
39
- {
40
- std::vector<T> result;
41
- while (true)
42
- {
43
- auto next = this->next();
44
- if (next.done)
45
- {
46
- break;
47
- }
48
- result.push_back(next.value.value_or(AnyValue::make_undefined()));
49
- }
50
- return result;
51
- }
52
-
53
- template <typename T>
54
- jspp::AnyValue jspp::JsIterator<T>::get_property(const std::string &key, const AnyValue &thisVal)
55
- {
56
- auto it = props.find(key);
57
- if (it == props.end())
58
- {
59
- // check prototype
60
- if constexpr (std::is_same_v<T, AnyValue>)
61
- {
62
- auto proto_it = IteratorPrototypes::get(key, this);
63
- if (proto_it.has_value())
64
- {
65
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
66
- }
67
- }
68
- // prototype not found
69
- return jspp::AnyValue::make_undefined();
70
- }
71
-
72
- return jspp::AnyValue::resolve_property_for_read(it->second, thisVal, key);
73
- }
74
-
75
- template <typename T>
76
- jspp::AnyValue jspp::JsIterator<T>::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
77
- {
78
- // set prototype property if accessor descriptor
79
- if constexpr (std::is_same_v<T, AnyValue>)
80
- {
81
- auto proto_it = IteratorPrototypes::get(key, this);
82
- if (proto_it.has_value())
83
- {
84
- auto proto_value = proto_it.value();
85
- if (proto_value.is_accessor_descriptor())
86
- {
87
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
88
- }
89
- if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
90
- {
91
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
92
- }
93
- }
94
- }
95
-
96
- // set own property
97
- auto it = props.find(key);
98
- if (it != props.end())
99
- {
100
- return jspp::AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
101
- }
102
- else
103
- {
104
- props[key] = value;
105
- return value;
106
- }
107
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "values/iterator.hpp"
5
+ #include "any_value.hpp"
6
+ #include "values/prototypes/iterator.hpp"
7
+
8
+ template <typename T>
9
+ std::string jspp::JsIterator<T>::to_std_string() const
10
+ {
11
+ return "[object Generator]";
12
+ }
13
+
14
+ template <typename T>
15
+ typename jspp::JsIterator<T>::NextResult jspp::JsIterator<T>::next(const T &val)
16
+ {
17
+ // If the generator is already finished or invalid, return {undefined, true}
18
+ if (!handle || handle.done())
19
+ return {std::nullopt, true};
20
+
21
+ handle.promise().input_value = val;
22
+
23
+ // Resume execution until next co_yield or co_return
24
+ handle.resume();
25
+
26
+ if (handle.promise().exception_)
27
+ {
28
+ std::rethrow_exception(handle.promise().exception_);
29
+ }
30
+
31
+ // If handle.done() is TRUE, we hit co_return (value: X, done: true)
32
+ // If handle.done() is FALSE, we hit co_yield (value: X, done: false)
33
+ bool is_done = handle.done();
34
+
35
+ return {handle.promise().current_value, is_done};
36
+ }
37
+
38
+ template <typename T>
39
+ std::vector<T> jspp::JsIterator<T>::to_vector()
40
+ {
41
+ std::vector<T> result;
42
+ while (true)
43
+ {
44
+ auto next_res = this->next();
45
+ if (next_res.done)
46
+ {
47
+ break;
48
+ }
49
+ result.push_back(next_res.value.value_or(Constants::UNDEFINED));
50
+ }
51
+ return result;
52
+ }
53
+
54
+ template <typename T>
55
+ jspp::AnyValue jspp::JsIterator<T>::get_property(const std::string &key, const AnyValue &thisVal)
56
+ {
57
+ auto it = props.find(key);
58
+ if (it == props.end())
59
+ {
60
+ // check prototype
61
+ if constexpr (std::is_same_v<T, AnyValue>)
62
+ {
63
+ auto proto_it = IteratorPrototypes::get(key, this);
64
+ if (proto_it.has_value())
65
+ {
66
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
67
+ }
68
+ }
69
+ // prototype not found
70
+ return Constants::UNDEFINED;
71
+ }
72
+
73
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key);
74
+ }
75
+
76
+ template <typename T>
77
+ jspp::AnyValue jspp::JsIterator<T>::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
78
+ {
79
+ // set prototype property if accessor descriptor
80
+ if constexpr (std::is_same_v<T, AnyValue>)
81
+ {
82
+ auto proto_it = IteratorPrototypes::get(key, this);
83
+ if (proto_it.has_value())
84
+ {
85
+ auto proto_value = proto_it.value();
86
+ if (proto_value.is_accessor_descriptor())
87
+ {
88
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
89
+ }
90
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
91
+ {
92
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
93
+ }
94
+ }
95
+ }
96
+
97
+ // set own property
98
+ auto it = props.find(key);
99
+ if (it != props.end())
100
+ {
101
+ return jspp::AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
102
+ }
103
+ else
104
+ {
105
+ props[key] = value;
106
+ return value;
107
+ }
108
+ }
@@ -1,109 +1,104 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/object.hpp"
5
- #include "any_value.hpp"
6
-
7
- namespace jspp {
8
- JsObject::JsObject() : shape(Shape::empty_shape()), proto(nullptr) {}
9
-
10
- JsObject::JsObject(std::initializer_list<std::pair<std::string, AnyValue>> p, std::shared_ptr<AnyValue> pr) : proto(pr) {
11
- shape = Shape::empty_shape();
12
- storage.reserve(p.size());
13
- for (const auto& pair : p) {
14
- shape = shape->transition(pair.first);
15
- storage.push_back(pair.second);
16
- }
17
- }
18
-
19
- JsObject::JsObject(const std::map<std::string, AnyValue> &p, std::shared_ptr<AnyValue> pr) : proto(pr) {
20
- shape = Shape::empty_shape();
21
- storage.reserve(p.size());
22
- for (const auto& pair : p) {
23
- shape = shape->transition(pair.first);
24
- storage.push_back(pair.second);
25
- }
26
- }
27
- }
28
-
29
- std::string jspp::JsObject::to_std_string() const
30
- {
31
- return "[Object Object]";
32
- }
33
-
34
- bool jspp::JsObject::has_property(const std::string &key) const
35
- {
36
- if (deleted_keys.count(key)) return false;
37
-
38
- if (shape->get_offset(key).has_value())
39
- return true;
40
- if (proto && !(*proto).is_null() && !(*proto).is_undefined())
41
- {
42
- if ((*proto).has_property(key))
43
- return true;
44
- }
45
- if (ObjectPrototypes::get(key, const_cast<JsObject *>(this)).has_value())
46
- return true;
47
- return false;
48
- }
49
-
50
- jspp::AnyValue jspp::JsObject::get_property(const std::string &key, const AnyValue &thisVal)
51
- {
52
- if (deleted_keys.count(key)) return AnyValue::make_undefined();
53
-
54
- auto offset = shape->get_offset(key);
55
- if (!offset.has_value())
56
- {
57
- // check prototype chain
58
- if (proto && !(*proto).is_null() && !(*proto).is_undefined())
59
- {
60
- if ((*proto).has_property(key))
61
- {
62
- return (*proto).get_property_with_receiver(key, thisVal);
63
- }
64
- }
65
-
66
- // check built-in prototype methods (Object.prototype)
67
- auto proto_it = ObjectPrototypes::get(key, this);
68
- if (proto_it.has_value())
69
- {
70
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
71
- }
72
- // not found
73
- return AnyValue::make_undefined();
74
- }
75
- return AnyValue::resolve_property_for_read(storage[offset.value()], thisVal, key);
76
- }
77
-
78
- jspp::AnyValue jspp::JsObject::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
79
- {
80
- // set prototype property if accessor descriptor
81
- auto proto_it = ObjectPrototypes::get(key, this);
82
- if (proto_it.has_value())
83
- {
84
- auto proto_value = proto_it.value();
85
- if (proto_value.is_accessor_descriptor())
86
- {
87
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
88
- }
89
- if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
90
- {
91
- return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
92
- }
93
- }
94
-
95
- // set own property
96
- if (deleted_keys.count(key)) deleted_keys.erase(key);
97
-
98
- auto offset = shape->get_offset(key);
99
- if (offset.has_value())
100
- {
101
- return AnyValue::resolve_property_for_write(storage[offset.value()], thisVal, value, key);
102
- }
103
- else
104
- {
105
- shape = shape->transition(key);
106
- storage.push_back(value);
107
- return value;
108
- }
109
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "values/object.hpp"
5
+ #include "any_value.hpp"
6
+
7
+ namespace jspp {
8
+ inline JsObject::JsObject() : shape(Shape::empty_shape()), proto(Constants::Null) {}
9
+
10
+ inline JsObject::JsObject(std::initializer_list<std::pair<std::string, AnyValue>> p, AnyValue pr) : proto(pr) {
11
+ shape = Shape::empty_shape();
12
+ storage.reserve(p.size());
13
+ for (const auto& pair : p) {
14
+ shape = shape->transition(pair.first);
15
+ storage.push_back(pair.second);
16
+ }
17
+ }
18
+
19
+ inline JsObject::JsObject(const std::map<std::string, AnyValue> &p, AnyValue pr) : proto(pr) {
20
+ shape = Shape::empty_shape();
21
+ storage.reserve(p.size());
22
+ for (const auto& pair : p) {
23
+ shape = shape->transition(pair.first);
24
+ storage.push_back(pair.second);
25
+ }
26
+ }
27
+ }
28
+
29
+ inline std::string jspp::JsObject::to_std_string() const
30
+ {
31
+ return "[Object Object]";
32
+ }
33
+
34
+ inline bool jspp::JsObject::has_property(const std::string &key) const
35
+ {
36
+ if (deleted_keys.count(key)) return false;
37
+
38
+ if (shape->get_offset(key).has_value())
39
+ return true;
40
+ if (!proto.is_null() && !proto.is_undefined())
41
+ {
42
+ if (proto.has_property(key))
43
+ return true;
44
+ }
45
+ if (ObjectPrototypes::get(key, const_cast<JsObject *>(this)).has_value())
46
+ return true;
47
+ return false;
48
+ }
49
+
50
+ inline jspp::AnyValue jspp::JsObject::get_property(const std::string &key, const AnyValue &thisVal)
51
+ {
52
+ if (deleted_keys.count(key)) return Constants::UNDEFINED;
53
+
54
+ auto offset = shape->get_offset(key);
55
+ if (!offset.has_value())
56
+ {
57
+ if (!proto.is_null() && !proto.is_undefined())
58
+ {
59
+ if (proto.has_property(key))
60
+ {
61
+ return proto.get_property_with_receiver(key, thisVal);
62
+ }
63
+ }
64
+
65
+ auto proto_it = ObjectPrototypes::get(key, this);
66
+ if (proto_it.has_value())
67
+ {
68
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
69
+ }
70
+ return Constants::UNDEFINED;
71
+ }
72
+ return AnyValue::resolve_property_for_read(storage[offset.value()], thisVal, key);
73
+ }
74
+
75
+ inline jspp::AnyValue jspp::JsObject::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
76
+ {
77
+ auto proto_it = ObjectPrototypes::get(key, this);
78
+ if (proto_it.has_value())
79
+ {
80
+ auto proto_value = proto_it.value();
81
+ if (proto_value.is_accessor_descriptor())
82
+ {
83
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
84
+ }
85
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
86
+ {
87
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
88
+ }
89
+ }
90
+
91
+ if (deleted_keys.count(key)) deleted_keys.erase(key);
92
+
93
+ auto offset = shape->get_offset(key);
94
+ if (offset.has_value())
95
+ {
96
+ return AnyValue::resolve_property_for_write(storage[offset.value()], thisVal, value, key);
97
+ }
98
+ else
99
+ {
100
+ shape = shape->transition(key);
101
+ storage.push_back(value);
102
+ return value;
103
+ }
104
+ }