@ugo-studio/jspp 0.1.2 → 0.1.4
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.
- package/README.md +5 -3
- package/dist/analysis/scope.js +38 -15
- package/dist/analysis/typeAnalyzer.js +257 -23
- package/dist/ast/types.js +6 -0
- package/dist/cli.js +3 -4
- package/dist/core/codegen/class-handlers.js +127 -0
- package/dist/core/codegen/control-flow-handlers.js +464 -0
- package/dist/core/codegen/declaration-handlers.js +31 -14
- package/dist/core/codegen/expression-handlers.js +432 -116
- package/dist/core/codegen/function-handlers.js +110 -13
- package/dist/core/codegen/helpers.js +76 -8
- package/dist/core/codegen/index.js +18 -5
- package/dist/core/codegen/literal-handlers.js +3 -0
- package/dist/core/codegen/statement-handlers.js +152 -186
- package/dist/core/codegen/visitor.js +35 -3
- package/package.json +3 -3
- package/src/prelude/any_value.hpp +658 -734
- package/src/prelude/any_value_access.hpp +103 -0
- package/src/prelude/any_value_defines.hpp +151 -0
- package/src/prelude/any_value_helpers.hpp +246 -0
- package/src/prelude/exception.hpp +31 -0
- package/src/prelude/exception_helpers.hpp +49 -0
- package/src/prelude/index.hpp +35 -12
- package/src/prelude/library/console.hpp +20 -20
- package/src/prelude/library/error.hpp +111 -0
- package/src/prelude/library/global.hpp +15 -4
- package/src/prelude/library/performance.hpp +25 -0
- package/src/prelude/library/promise.hpp +121 -0
- package/src/prelude/library/symbol.hpp +60 -4
- package/src/prelude/library/timer.hpp +92 -0
- package/src/prelude/scheduler.hpp +145 -0
- package/src/prelude/types.hpp +33 -6
- package/src/prelude/utils/access.hpp +174 -0
- package/src/prelude/utils/log_any_value/array.hpp +245 -0
- package/src/prelude/utils/log_any_value/config.hpp +32 -0
- package/src/prelude/utils/log_any_value/function.hpp +37 -0
- package/src/prelude/utils/log_any_value/fwd.hpp +15 -0
- package/src/prelude/utils/log_any_value/helpers.hpp +62 -0
- package/src/prelude/utils/log_any_value/log_any_value.hpp +94 -0
- package/src/prelude/utils/log_any_value/object.hpp +119 -0
- package/src/prelude/utils/log_any_value/primitives.hpp +41 -0
- package/src/prelude/{operators.hpp → utils/operators.hpp} +31 -12
- package/src/prelude/utils/well_known_symbols.hpp +13 -0
- package/src/prelude/values/array.hpp +5 -2
- package/src/prelude/{descriptors.hpp → values/descriptors.hpp} +2 -2
- package/src/prelude/values/function.hpp +76 -19
- package/src/prelude/values/{operators → helpers}/array.hpp +30 -14
- package/src/prelude/values/helpers/function.hpp +125 -0
- package/src/prelude/values/helpers/iterator.hpp +107 -0
- package/src/prelude/values/helpers/object.hpp +64 -0
- package/src/prelude/values/helpers/promise.hpp +181 -0
- package/src/prelude/values/helpers/string.hpp +50 -0
- package/src/prelude/values/helpers/symbol.hpp +23 -0
- package/src/prelude/values/iterator.hpp +96 -0
- package/src/prelude/values/object.hpp +8 -3
- package/src/prelude/values/promise.hpp +73 -0
- package/src/prelude/values/prototypes/array.hpp +23 -16
- package/src/prelude/values/prototypes/function.hpp +26 -0
- package/src/prelude/values/prototypes/iterator.hpp +58 -0
- package/src/prelude/values/prototypes/object.hpp +26 -0
- package/src/prelude/values/prototypes/promise.hpp +124 -0
- package/src/prelude/values/prototypes/string.hpp +366 -357
- package/src/prelude/values/prototypes/symbol.hpp +41 -0
- package/src/prelude/values/string.hpp +25 -0
- package/src/prelude/values/symbol.hpp +102 -0
- package/src/prelude/access.hpp +0 -86
- package/src/prelude/error.hpp +0 -31
- package/src/prelude/error_helpers.hpp +0 -59
- package/src/prelude/log_string.hpp +0 -403
- package/src/prelude/values/operators/function.hpp +0 -34
- package/src/prelude/values/operators/object.hpp +0 -34
- package/src/prelude/well_known_symbols.hpp +0 -10
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "values/symbol.hpp"
|
|
5
|
+
#include "any_value.hpp"
|
|
6
|
+
|
|
7
|
+
namespace jspp
|
|
8
|
+
{
|
|
9
|
+
namespace SymbolPrototypes
|
|
10
|
+
{
|
|
11
|
+
inline std::optional<AnyValue> get(const std::string &key, JsSymbol *self)
|
|
12
|
+
{
|
|
13
|
+
// --- toString() method ---
|
|
14
|
+
if (key == "toString" )
|
|
15
|
+
{
|
|
16
|
+
return AnyValue::make_function([self](const AnyValue &thisVal, const std::vector<AnyValue> &) -> AnyValue
|
|
17
|
+
{ return AnyValue::make_string(self->to_std_string()); },
|
|
18
|
+
key);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// --- description property ---
|
|
22
|
+
if (key == "description")
|
|
23
|
+
{
|
|
24
|
+
return AnyValue::make_accessor_descriptor(
|
|
25
|
+
[self](const AnyValue &thisVal, const std::vector<AnyValue> &) -> AnyValue
|
|
26
|
+
{
|
|
27
|
+
if (self->description.empty())
|
|
28
|
+
{
|
|
29
|
+
return AnyValue::make_undefined();
|
|
30
|
+
}
|
|
31
|
+
return AnyValue::make_string(self->description);
|
|
32
|
+
},
|
|
33
|
+
std::nullopt,
|
|
34
|
+
false,
|
|
35
|
+
true);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return std::nullopt;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <unordered_map>
|
|
6
|
+
|
|
7
|
+
namespace jspp
|
|
8
|
+
{
|
|
9
|
+
// Forward declaration of AnyValue
|
|
10
|
+
class AnyValue;
|
|
11
|
+
|
|
12
|
+
struct JsString
|
|
13
|
+
{
|
|
14
|
+
std::string value;
|
|
15
|
+
|
|
16
|
+
JsString() = default;
|
|
17
|
+
explicit JsString(const std::string &s) : value(s) {}
|
|
18
|
+
|
|
19
|
+
std::string to_std_string() const;
|
|
20
|
+
JsIterator<AnyValue> get_iterator();
|
|
21
|
+
|
|
22
|
+
AnyValue get_property(const std::string &key, const AnyValue &thisVal);
|
|
23
|
+
AnyValue get_property(uint32_t idx);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include <atomic>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <unordered_map>
|
|
7
|
+
#include <unordered_set>
|
|
8
|
+
#include <memory>
|
|
9
|
+
#include <optional>
|
|
10
|
+
|
|
11
|
+
namespace jspp
|
|
12
|
+
{
|
|
13
|
+
// Forward declaration of AnyValue
|
|
14
|
+
class AnyValue;
|
|
15
|
+
|
|
16
|
+
struct JsSymbol
|
|
17
|
+
{
|
|
18
|
+
std::string description;
|
|
19
|
+
std::string key; // Internal unique key used for AnyValue property maps
|
|
20
|
+
|
|
21
|
+
// --- Registries ---
|
|
22
|
+
|
|
23
|
+
// 1. Global Symbol Registry (for Symbol.for/keyFor)
|
|
24
|
+
static std::unordered_map<std::string, std::shared_ptr<JsSymbol>> ®istry()
|
|
25
|
+
{
|
|
26
|
+
static std::unordered_map<std::string, std::shared_ptr<JsSymbol>> reg;
|
|
27
|
+
return reg;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 2. All Internal Keys Registry (for robust Object.keys filtering)
|
|
31
|
+
// Keeps track of every key string ever generated by a JsSymbol to distinguish
|
|
32
|
+
// them from user strings that might coincidentally look like internal keys.
|
|
33
|
+
static std::unordered_set<std::string> &internal_keys_registry()
|
|
34
|
+
{
|
|
35
|
+
static std::unordered_set<std::string> keys;
|
|
36
|
+
return keys;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static bool is_internal_key(const std::string &k)
|
|
40
|
+
{
|
|
41
|
+
return internal_keys_registry().count(k) > 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// --- Constructors ---
|
|
45
|
+
|
|
46
|
+
// Standard Constructor (creates unique symbol)
|
|
47
|
+
JsSymbol(const std::string &desc) : description(desc)
|
|
48
|
+
{
|
|
49
|
+
static std::atomic<uint64_t> id_counter{0};
|
|
50
|
+
// Ensure unique internal key for property storage
|
|
51
|
+
key = "__Sym" + std::to_string(id_counter++) + "_" + desc;
|
|
52
|
+
|
|
53
|
+
// Register this key as a valid symbol key
|
|
54
|
+
internal_keys_registry().insert(key);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Constructor for Well-Known Symbols (fixed keys)
|
|
58
|
+
JsSymbol(const std::string &desc, const std::string &fixed_key)
|
|
59
|
+
: description(desc), key(fixed_key)
|
|
60
|
+
{
|
|
61
|
+
// Register this key as a valid symbol key
|
|
62
|
+
internal_keys_registry().insert(key);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// --- Global Registry API ---
|
|
66
|
+
|
|
67
|
+
// Implements Symbol.for(key)
|
|
68
|
+
static std::shared_ptr<JsSymbol> for_global(const std::string ®istryKey)
|
|
69
|
+
{
|
|
70
|
+
auto ® = registry();
|
|
71
|
+
auto it = reg.find(registryKey);
|
|
72
|
+
if (it != reg.end())
|
|
73
|
+
{
|
|
74
|
+
return it->second;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Create new symbol with description = registryKey
|
|
78
|
+
auto newSym = std::make_shared<JsSymbol>(registryKey);
|
|
79
|
+
reg[registryKey] = newSym;
|
|
80
|
+
return newSym;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Implements Symbol.keyFor(sym)
|
|
84
|
+
static std::optional<std::string> key_for(const JsSymbol *sym)
|
|
85
|
+
{
|
|
86
|
+
auto ® = registry();
|
|
87
|
+
for (const auto &pair : reg)
|
|
88
|
+
{
|
|
89
|
+
// Compare raw pointers
|
|
90
|
+
if (pair.second.get() == sym)
|
|
91
|
+
{
|
|
92
|
+
return pair.first;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return std::nullopt;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// --- Methods ---
|
|
99
|
+
std::string to_std_string() const;
|
|
100
|
+
AnyValue get_property(const std::string &key, const AnyValue &thisVal);
|
|
101
|
+
};
|
|
102
|
+
}
|
package/src/prelude/access.hpp
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "values/function.hpp"
|
|
5
|
-
#include "error.hpp"
|
|
6
|
-
#include <ranges>
|
|
7
|
-
|
|
8
|
-
namespace jspp
|
|
9
|
-
{
|
|
10
|
-
namespace Access
|
|
11
|
-
{
|
|
12
|
-
// Helper function to check for TDZ and deref variables
|
|
13
|
-
inline const AnyValue &deref(const std::shared_ptr<AnyValue> &var, const std::string &name)
|
|
14
|
-
{
|
|
15
|
-
if ((*var).is_uninitialized()) [[unlikely]]
|
|
16
|
-
{
|
|
17
|
-
RuntimeError::throw_uninitialized_reference_error(name);
|
|
18
|
-
}
|
|
19
|
-
return *var;
|
|
20
|
-
}
|
|
21
|
-
inline AnyValue &deref(std::shared_ptr<AnyValue> &var, const std::string &name)
|
|
22
|
-
{
|
|
23
|
-
if ((*var).is_uninitialized()) [[unlikely]]
|
|
24
|
-
{
|
|
25
|
-
RuntimeError::throw_uninitialized_reference_error(name);
|
|
26
|
-
}
|
|
27
|
-
return *var;
|
|
28
|
-
}
|
|
29
|
-
inline const AnyValue &deref(const std::unique_ptr<AnyValue> &var, const std::string &name)
|
|
30
|
-
{
|
|
31
|
-
if ((*var).is_uninitialized()) [[unlikely]]
|
|
32
|
-
{
|
|
33
|
-
RuntimeError::throw_uninitialized_reference_error(name);
|
|
34
|
-
}
|
|
35
|
-
return *var;
|
|
36
|
-
}
|
|
37
|
-
inline AnyValue &deref(std::unique_ptr<AnyValue> &var, const std::string &name)
|
|
38
|
-
{
|
|
39
|
-
if ((*var).is_uninitialized()) [[unlikely]]
|
|
40
|
-
{
|
|
41
|
-
RuntimeError::throw_uninitialized_reference_error(name);
|
|
42
|
-
}
|
|
43
|
-
return *var;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
inline std::vector<std::string> get_object_keys(const AnyValue &obj)
|
|
47
|
-
{
|
|
48
|
-
std::vector<std::string> keys;
|
|
49
|
-
|
|
50
|
-
if (obj.is_object())
|
|
51
|
-
{
|
|
52
|
-
auto ptr = obj.as_object();
|
|
53
|
-
for (const auto &pair : ptr->props)
|
|
54
|
-
{
|
|
55
|
-
keys.push_back(pair.first);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (obj.is_function())
|
|
59
|
-
{
|
|
60
|
-
auto ptr = obj.as_function();
|
|
61
|
-
for (const auto &pair : ptr->props)
|
|
62
|
-
{
|
|
63
|
-
keys.push_back(pair.first);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (obj.is_array())
|
|
67
|
-
{
|
|
68
|
-
auto len = obj.as_array()->length;
|
|
69
|
-
for (auto i = 0; i < len; ++i)
|
|
70
|
-
{
|
|
71
|
-
keys.push_back(std::to_string(i));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (obj.is_string())
|
|
75
|
-
{
|
|
76
|
-
auto len = obj.as_string()->length();
|
|
77
|
-
for (auto i = 0; i < len; ++i)
|
|
78
|
-
{
|
|
79
|
-
keys.push_back(std::to_string(i));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return keys;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
package/src/prelude/error.hpp
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
#pragma once
|
|
3
|
-
|
|
4
|
-
#include "types.hpp"
|
|
5
|
-
|
|
6
|
-
namespace jspp
|
|
7
|
-
{
|
|
8
|
-
class AnyValue;
|
|
9
|
-
|
|
10
|
-
struct RuntimeError : std::exception
|
|
11
|
-
{
|
|
12
|
-
std::shared_ptr<AnyValue> data;
|
|
13
|
-
|
|
14
|
-
explicit RuntimeError(std::shared_ptr<AnyValue> d)
|
|
15
|
-
: data(std::move(d)) {}
|
|
16
|
-
explicit RuntimeError(const AnyValue &value)
|
|
17
|
-
: data(std::make_shared<AnyValue>(value)) {}
|
|
18
|
-
explicit RuntimeError(AnyValue &&value)
|
|
19
|
-
: data(std::make_shared<AnyValue>(std::move(value))) {}
|
|
20
|
-
|
|
21
|
-
const char *what() const noexcept override;
|
|
22
|
-
static RuntimeError make_error(const std::string &message, const std::string &name);
|
|
23
|
-
static AnyValue error_to_value(const std::exception &ex);
|
|
24
|
-
|
|
25
|
-
// --- THROWERS
|
|
26
|
-
static AnyValue throw_unresolved_reference_error(const std::string &var_name);
|
|
27
|
-
static AnyValue throw_uninitialized_reference_error(const std::string &var_name);
|
|
28
|
-
static AnyValue throw_immutable_assignment_error();
|
|
29
|
-
static AnyValue throw_invalid_return_statement_error();
|
|
30
|
-
};
|
|
31
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "error.hpp"
|
|
5
|
-
#include "any_value.hpp"
|
|
6
|
-
|
|
7
|
-
const char *jspp::RuntimeError::what() const noexcept
|
|
8
|
-
{
|
|
9
|
-
return data->to_std_string().c_str();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
jspp::RuntimeError jspp::RuntimeError::make_error(const std::string &message, const std::string &name = "Error")
|
|
13
|
-
{
|
|
14
|
-
auto errorObj = std::make_shared<AnyValue>(AnyValue::make_object({{"message", AnyValue::make_string(message)}, {"name", AnyValue::make_string(name)}}));
|
|
15
|
-
(*errorObj).set_own_property(WellKnownSymbols::toString, AnyValue::make_function([errorObj](const std::vector<AnyValue> &) -> AnyValue
|
|
16
|
-
{
|
|
17
|
-
AnyValue name = (*errorObj).get_own_property("name");
|
|
18
|
-
AnyValue message = (*errorObj).get_own_property("message");
|
|
19
|
-
std::string str = "";
|
|
20
|
-
if (name.is_string())
|
|
21
|
-
str = name.to_std_string();
|
|
22
|
-
else
|
|
23
|
-
str = "Error";
|
|
24
|
-
str += ": ";
|
|
25
|
-
if (message.is_string())
|
|
26
|
-
str += message.to_std_string();
|
|
27
|
-
return AnyValue::make_string(str); },
|
|
28
|
-
WellKnownSymbols::toString));
|
|
29
|
-
return RuntimeError(errorObj);
|
|
30
|
-
}
|
|
31
|
-
jspp::AnyValue jspp::RuntimeError::error_to_value(const std::exception &ex)
|
|
32
|
-
{
|
|
33
|
-
if (const jspp::RuntimeError *err = dynamic_cast<const jspp::RuntimeError *>(&ex))
|
|
34
|
-
{
|
|
35
|
-
return (*err->data);
|
|
36
|
-
}
|
|
37
|
-
else
|
|
38
|
-
{
|
|
39
|
-
return AnyValue::make_string(ex.what());
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// --- THROWERS
|
|
44
|
-
jspp::AnyValue jspp::RuntimeError::throw_unresolved_reference_error(const std::string &var_name)
|
|
45
|
-
{
|
|
46
|
-
throw RuntimeError::make_error(var_name + " is not defined", "ReferenceError");
|
|
47
|
-
}
|
|
48
|
-
jspp::AnyValue jspp::RuntimeError::throw_uninitialized_reference_error(const std::string &var_name)
|
|
49
|
-
{
|
|
50
|
-
throw RuntimeError::make_error("Cannot access '" + var_name + "' before initialization", "ReferenceError");
|
|
51
|
-
}
|
|
52
|
-
jspp::AnyValue jspp::RuntimeError::throw_immutable_assignment_error()
|
|
53
|
-
{
|
|
54
|
-
throw RuntimeError::make_error("Assignment to constant variable.", "TypeError");
|
|
55
|
-
}
|
|
56
|
-
jspp::AnyValue jspp::RuntimeError::throw_invalid_return_statement_error()
|
|
57
|
-
{
|
|
58
|
-
throw RuntimeError::make_error("Return statements are only valid inside functions.", "SyntaxError");
|
|
59
|
-
}
|