@ugo-studio/jspp 0.1.4 → 0.1.5

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 (68) hide show
  1. package/dist/analysis/scope.js +17 -0
  2. package/dist/analysis/typeAnalyzer.js +7 -1
  3. package/dist/ast/symbols.js +29 -0
  4. package/dist/ast/types.js +0 -6
  5. package/dist/cli-utils/args.js +57 -0
  6. package/dist/cli-utils/colors.js +9 -0
  7. package/dist/cli-utils/file-utils.js +20 -0
  8. package/dist/cli-utils/spinner.js +55 -0
  9. package/dist/cli.js +105 -30
  10. package/dist/core/codegen/class-handlers.js +10 -6
  11. package/dist/core/codegen/control-flow-handlers.js +31 -21
  12. package/dist/core/codegen/declaration-handlers.js +10 -6
  13. package/dist/core/codegen/expression-handlers.js +202 -60
  14. package/dist/core/codegen/function-handlers.js +179 -70
  15. package/dist/core/codegen/helpers.js +107 -17
  16. package/dist/core/codegen/index.js +9 -8
  17. package/dist/core/codegen/literal-handlers.js +15 -6
  18. package/dist/core/codegen/statement-handlers.js +67 -53
  19. package/dist/core/codegen/visitor.js +3 -1
  20. package/package.json +1 -1
  21. package/src/prelude/any_value.hpp +195 -342
  22. package/src/prelude/any_value_access.hpp +78 -30
  23. package/src/prelude/any_value_defines.hpp +74 -35
  24. package/src/prelude/any_value_helpers.hpp +73 -180
  25. package/src/prelude/exception.hpp +1 -0
  26. package/src/prelude/exception_helpers.hpp +4 -4
  27. package/src/prelude/index.hpp +9 -2
  28. package/src/prelude/library/array.hpp +190 -0
  29. package/src/prelude/library/console.hpp +6 -5
  30. package/src/prelude/library/error.hpp +10 -8
  31. package/src/prelude/library/function.hpp +10 -0
  32. package/src/prelude/library/global.hpp +20 -0
  33. package/src/prelude/library/math.hpp +308 -0
  34. package/src/prelude/library/object.hpp +288 -0
  35. package/src/prelude/library/performance.hpp +1 -1
  36. package/src/prelude/library/process.hpp +39 -0
  37. package/src/prelude/library/promise.hpp +53 -43
  38. package/src/prelude/library/symbol.hpp +45 -57
  39. package/src/prelude/library/timer.hpp +6 -6
  40. package/src/prelude/types.hpp +48 -0
  41. package/src/prelude/utils/access.hpp +182 -11
  42. package/src/prelude/utils/assignment_operators.hpp +99 -0
  43. package/src/prelude/utils/log_any_value/array.hpp +8 -8
  44. package/src/prelude/utils/log_any_value/function.hpp +6 -4
  45. package/src/prelude/utils/log_any_value/object.hpp +41 -24
  46. package/src/prelude/utils/log_any_value/primitives.hpp +3 -1
  47. package/src/prelude/utils/operators.hpp +750 -274
  48. package/src/prelude/utils/well_known_symbols.hpp +12 -0
  49. package/src/prelude/values/array.hpp +8 -6
  50. package/src/prelude/values/descriptors.hpp +2 -2
  51. package/src/prelude/values/function.hpp +71 -62
  52. package/src/prelude/values/helpers/array.hpp +64 -28
  53. package/src/prelude/values/helpers/function.hpp +77 -92
  54. package/src/prelude/values/helpers/iterator.hpp +3 -3
  55. package/src/prelude/values/helpers/object.hpp +54 -9
  56. package/src/prelude/values/helpers/promise.hpp +3 -3
  57. package/src/prelude/values/iterator.hpp +1 -1
  58. package/src/prelude/values/object.hpp +10 -3
  59. package/src/prelude/values/promise.hpp +3 -3
  60. package/src/prelude/values/prototypes/array.hpp +851 -12
  61. package/src/prelude/values/prototypes/function.hpp +2 -2
  62. package/src/prelude/values/prototypes/iterator.hpp +5 -5
  63. package/src/prelude/values/prototypes/number.hpp +153 -0
  64. package/src/prelude/values/prototypes/object.hpp +2 -2
  65. package/src/prelude/values/prototypes/promise.hpp +40 -30
  66. package/src/prelude/values/prototypes/string.hpp +28 -28
  67. package/src/prelude/values/prototypes/symbol.hpp +20 -3
  68. package/src/prelude/values/shape.hpp +52 -0
@@ -4,24 +4,66 @@
4
4
  #include "values/object.hpp"
5
5
  #include "any_value.hpp"
6
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
+
7
29
  std::string jspp::JsObject::to_std_string() const
8
30
  {
9
31
  return "[Object Object]";
10
32
  }
11
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
+
12
50
  jspp::AnyValue jspp::JsObject::get_property(const std::string &key, const AnyValue &thisVal)
13
51
  {
14
- auto it = props.find(key);
15
- if (it == props.end())
52
+ if (deleted_keys.count(key)) return AnyValue::make_undefined();
53
+
54
+ auto offset = shape->get_offset(key);
55
+ if (!offset.has_value())
16
56
  {
17
57
  // check prototype chain
18
58
  if (proto && !(*proto).is_null() && !(*proto).is_undefined())
19
59
  {
20
- return (*proto).get_property_with_receiver(key, thisVal);
60
+ if ((*proto).has_property(key))
61
+ {
62
+ return (*proto).get_property_with_receiver(key, thisVal);
63
+ }
21
64
  }
22
65
 
23
66
  // check built-in prototype methods (Object.prototype)
24
- // ideally these should be on the root prototype object, but for now we keep this fallback
25
67
  auto proto_it = ObjectPrototypes::get(key, this);
26
68
  if (proto_it.has_value())
27
69
  {
@@ -30,7 +72,7 @@ jspp::AnyValue jspp::JsObject::get_property(const std::string &key, const AnyVal
30
72
  // not found
31
73
  return AnyValue::make_undefined();
32
74
  }
33
- return AnyValue::resolve_property_for_read(it->second, thisVal, key);
75
+ return AnyValue::resolve_property_for_read(storage[offset.value()], thisVal, key);
34
76
  }
35
77
 
36
78
  jspp::AnyValue jspp::JsObject::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
@@ -51,14 +93,17 @@ jspp::AnyValue jspp::JsObject::set_property(const std::string &key, const AnyVal
51
93
  }
52
94
 
53
95
  // set own property
54
- auto it = props.find(key);
55
- if (it != props.end())
96
+ if (deleted_keys.count(key)) deleted_keys.erase(key);
97
+
98
+ auto offset = shape->get_offset(key);
99
+ if (offset.has_value())
56
100
  {
57
- return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
101
+ return AnyValue::resolve_property_for_write(storage[offset.value()], thisVal, value, key);
58
102
  }
59
103
  else
60
104
  {
61
- props[key] = value;
105
+ shape = shape->transition(key);
106
+ storage.push_back(value);
62
107
  return value;
63
108
  }
64
109
  }
@@ -24,13 +24,13 @@ namespace jspp {
24
24
  auto weak_state = std::weak_ptr<PromiseState>(state);
25
25
 
26
26
  p->then(
27
- [weak_state](AnyValue v) {
27
+ [weak_state](const AnyValue& v) {
28
28
  if (auto s = weak_state.lock()) {
29
29
  JsPromise localP; localP.state = s;
30
30
  localP.resolve(v);
31
31
  }
32
32
  },
33
- [weak_state](AnyValue r) {
33
+ [weak_state](const AnyValue& r) {
34
34
  if (auto s = weak_state.lock()) {
35
35
  JsPromise localP; localP.state = s;
36
36
  localP.reject(r);
@@ -71,7 +71,7 @@ namespace jspp {
71
71
  }
72
72
  }
73
73
 
74
- inline void JsPromise::then(std::function<void(AnyValue)> onFulfilled, std::function<void(AnyValue)> onRejected) {
74
+ inline void JsPromise::then(std::function<void(const AnyValue&)> onFulfilled, std::function<void(const AnyValue&)> onRejected) {
75
75
  if (state->status == PromiseStatus::Fulfilled) {
76
76
  if (onFulfilled) {
77
77
  AnyValue val = *(state->result);
@@ -89,7 +89,7 @@ namespace jspp
89
89
 
90
90
  std::string to_std_string() const;
91
91
  NextResult next(const T &val = T());
92
- std::vector<std::optional<T>> to_vector();
92
+ std::vector<T> to_vector();
93
93
  AnyValue get_property(const std::string &key, const AnyValue &thisVal);
94
94
  AnyValue set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal);
95
95
  };
@@ -1,6 +1,9 @@
1
1
  #pragma once
2
2
 
3
3
  #include "types.hpp"
4
+ #include "shape.hpp"
5
+ #include <vector>
6
+ #include <unordered_set>
4
7
 
5
8
  namespace jspp
6
9
  {
@@ -9,13 +12,17 @@ namespace jspp
9
12
 
10
13
  struct JsObject
11
14
  {
12
- std::map<std::string, AnyValue> props;
15
+ std::shared_ptr<Shape> shape;
16
+ std::vector<AnyValue> storage;
13
17
  std::shared_ptr<AnyValue> proto;
18
+ std::unordered_set<std::string> deleted_keys;
14
19
 
15
- JsObject() : proto(nullptr) {}
16
- explicit JsObject(const std::map<std::string, AnyValue> &p, std::shared_ptr<AnyValue> pr = nullptr) : props(p), proto(pr) {}
20
+ JsObject();
21
+ JsObject(std::initializer_list<std::pair<std::string, AnyValue>> p, std::shared_ptr<AnyValue> pr = nullptr);
22
+ JsObject(const std::map<std::string, AnyValue> &p, std::shared_ptr<AnyValue> pr = nullptr);
17
23
 
18
24
  std::string to_std_string() const;
25
+ bool has_property(const std::string &key) const;
19
26
  AnyValue get_property(const std::string &key, const AnyValue &thisVal);
20
27
  AnyValue set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal);
21
28
  };
@@ -20,8 +20,8 @@ namespace jspp
20
20
  {
21
21
  PromiseStatus status = PromiseStatus::Pending;
22
22
  std::shared_ptr<AnyValue> result; // Value if fulfilled, reason if rejected
23
- std::vector<std::function<void(AnyValue)>> onFulfilled;
24
- std::vector<std::function<void(AnyValue)>> onRejected;
23
+ std::vector<std::function<void(const AnyValue&)>> onFulfilled;
24
+ std::vector<std::function<void(const AnyValue&)>> onRejected;
25
25
 
26
26
  PromiseState(); // Defined in helpers
27
27
  };
@@ -40,7 +40,7 @@ namespace jspp
40
40
  // --- Promise Logic ---
41
41
  void resolve(const AnyValue& value);
42
42
  void reject(const AnyValue& reason);
43
- void then(std::function<void(AnyValue)> onFulfilled, std::function<void(AnyValue)> onRejected = nullptr);
43
+ void then(std::function<void(const AnyValue&)> onFulfilled, std::function<void(const AnyValue&)> onRejected = nullptr);
44
44
 
45
45
  // --- Methods ---
46
46
  std::string to_std_string() const;