@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,37 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include "types.hpp"
|
|
3
|
+
#include "any_value.hpp"
|
|
4
|
+
#include "utils/log_any_value/config.hpp"
|
|
5
|
+
#include <string>
|
|
6
|
+
|
|
7
|
+
namespace jspp
|
|
8
|
+
{
|
|
9
|
+
namespace LogAnyValue
|
|
10
|
+
{
|
|
11
|
+
inline std::string format_function(const AnyValue &val)
|
|
12
|
+
{
|
|
13
|
+
auto fn = val.as_function();
|
|
14
|
+
|
|
15
|
+
if (fn->is_class)
|
|
16
|
+
{
|
|
17
|
+
std::string extends_part = "";
|
|
18
|
+
if (fn->proto && !fn->proto->is_uninitialized() && !fn->proto->is_undefined() && !fn->proto->is_null())
|
|
19
|
+
{
|
|
20
|
+
if (fn->proto->is_function())
|
|
21
|
+
{
|
|
22
|
+
auto parent = fn->proto->as_function();
|
|
23
|
+
if (!parent->name.empty())
|
|
24
|
+
{
|
|
25
|
+
extends_part = " extends " + parent->name;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return Color::CYAN + std::string("[class ") + (fn->name.empty() ? "(anonymous)" : fn->name) + extends_part + "]" + Color::RESET;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
auto type_part = fn->is_generator ? "GeneratorFunction" : "Function";
|
|
33
|
+
auto name_part = fn->name.size() > 0 ? ": " + fn->name : " (anonymous)";
|
|
34
|
+
return Color::CYAN + "[" + type_part + name_part + "]" + Color::RESET;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <unordered_set>
|
|
5
|
+
#include "any_value.hpp"
|
|
6
|
+
|
|
7
|
+
namespace jspp
|
|
8
|
+
{
|
|
9
|
+
namespace LogAnyValue
|
|
10
|
+
{
|
|
11
|
+
// Forward declarations
|
|
12
|
+
inline std::string to_log_string(const AnyValue &val);
|
|
13
|
+
inline std::string to_log_string(const AnyValue &val, std::unordered_set<const void *> &visited, int depth);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "any_value.hpp"
|
|
5
|
+
#include "utils/log_any_value/config.hpp"
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <cctype>
|
|
8
|
+
|
|
9
|
+
namespace jspp
|
|
10
|
+
{
|
|
11
|
+
namespace LogAnyValue
|
|
12
|
+
{
|
|
13
|
+
inline bool is_valid_js_identifier(const std::string &s)
|
|
14
|
+
{
|
|
15
|
+
if (s.empty())
|
|
16
|
+
{
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
if (!std::isalpha(s[0]) && s[0] != '_' && s[0] != '$')
|
|
20
|
+
{
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
for (size_t i = 1; i < s.length(); ++i)
|
|
24
|
+
{
|
|
25
|
+
if (!std::isalnum(s[i]) && s[i] != '_' && s[i] != '$')
|
|
26
|
+
{
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
inline bool is_simple_value(const AnyValue &val)
|
|
34
|
+
{
|
|
35
|
+
return val.is_undefined() || val.is_null() || val.is_uninitialized() ||
|
|
36
|
+
val.is_boolean() || val.is_number() || val.is_string();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
inline bool is_enumerable_property(const AnyValue &val)
|
|
40
|
+
{
|
|
41
|
+
if (val.is_data_descriptor())
|
|
42
|
+
{
|
|
43
|
+
return val.as_data_descriptor()->enumerable;
|
|
44
|
+
}
|
|
45
|
+
else if (
|
|
46
|
+
val.is_accessor_descriptor())
|
|
47
|
+
{
|
|
48
|
+
return val.as_accessor_descriptor()->enumerable;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
inline std::string truncate_string(const std::string &str)
|
|
54
|
+
{
|
|
55
|
+
if (str.length() > MAX_STRING_LENGTH)
|
|
56
|
+
{
|
|
57
|
+
return str.substr(0, MAX_STRING_LENGTH) + "...";
|
|
58
|
+
}
|
|
59
|
+
return str;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "utils/well_known_symbols.hpp"
|
|
5
|
+
#include "any_value.hpp"
|
|
6
|
+
#include "utils/log_any_value/config.hpp"
|
|
7
|
+
#include "utils/log_any_value/fwd.hpp"
|
|
8
|
+
#include "utils/log_any_value/helpers.hpp"
|
|
9
|
+
#include "utils/log_any_value/primitives.hpp"
|
|
10
|
+
#include "utils/log_any_value/function.hpp"
|
|
11
|
+
#include "utils/log_any_value/object.hpp"
|
|
12
|
+
#include "utils/log_any_value/array.hpp"
|
|
13
|
+
|
|
14
|
+
#include <unordered_set>
|
|
15
|
+
|
|
16
|
+
namespace jspp
|
|
17
|
+
{
|
|
18
|
+
namespace LogAnyValue
|
|
19
|
+
{
|
|
20
|
+
inline std::string to_log_string(const AnyValue &val)
|
|
21
|
+
{
|
|
22
|
+
std::unordered_set<const void *> visited;
|
|
23
|
+
return to_log_string(val, visited, 0);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
inline std::string to_log_string(const AnyValue &val, std::unordered_set<const void *> &visited, int depth)
|
|
27
|
+
{
|
|
28
|
+
// 1. Try Primitives
|
|
29
|
+
auto primitiveStr = format_primitive(val, depth);
|
|
30
|
+
if (primitiveStr.has_value())
|
|
31
|
+
{
|
|
32
|
+
return primitiveStr.value();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 2. Functions
|
|
36
|
+
if (val.is_function())
|
|
37
|
+
{
|
|
38
|
+
return format_function(val);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 3. Depth limit
|
|
42
|
+
if (depth > MAX_DEPTH)
|
|
43
|
+
{
|
|
44
|
+
if (val.is_object())
|
|
45
|
+
return Color::CYAN + std::string("[Object]") + Color::RESET;
|
|
46
|
+
if (val.is_array())
|
|
47
|
+
return Color::CYAN + std::string("[Array]") + Color::RESET;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 4. Circular reference detection
|
|
51
|
+
const void *ptr_address = nullptr;
|
|
52
|
+
if (val.is_object())
|
|
53
|
+
ptr_address = val.as_object();
|
|
54
|
+
else if (val.is_array())
|
|
55
|
+
ptr_address = val.as_array();
|
|
56
|
+
|
|
57
|
+
if (ptr_address)
|
|
58
|
+
{
|
|
59
|
+
if (visited.count(ptr_address))
|
|
60
|
+
return Color::CYAN + std::string("[Circular]") + Color::RESET;
|
|
61
|
+
visited.insert(ptr_address);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 5. Complex Types (Objects & Arrays)
|
|
65
|
+
if (val.is_object())
|
|
66
|
+
{
|
|
67
|
+
return format_object(val, visited, depth);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (val.is_array())
|
|
71
|
+
{
|
|
72
|
+
return format_array(val, visited, depth);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 6. DataDescriptor
|
|
76
|
+
if (val.is_data_descriptor())
|
|
77
|
+
{
|
|
78
|
+
auto desc = val.as_data_descriptor();
|
|
79
|
+
if (desc->enumerable)
|
|
80
|
+
{
|
|
81
|
+
return to_log_string((*desc->value), visited, depth);
|
|
82
|
+
}
|
|
83
|
+
else
|
|
84
|
+
{
|
|
85
|
+
// Will not be printed if the method works well
|
|
86
|
+
return Color::BRIGHT_BLACK + "<non-enumerable>" + Color::RESET;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Fallback
|
|
91
|
+
return val.to_std_string();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include "types.hpp"
|
|
3
|
+
#include "any_value.hpp"
|
|
4
|
+
#include "library/error.hpp"
|
|
5
|
+
#include "utils/log_any_value/config.hpp"
|
|
6
|
+
#include "utils/log_any_value/helpers.hpp"
|
|
7
|
+
#include "utils/log_any_value/fwd.hpp" // Required for recursive to_log_string call
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <sstream>
|
|
10
|
+
#include <unordered_set>
|
|
11
|
+
|
|
12
|
+
namespace jspp
|
|
13
|
+
{
|
|
14
|
+
namespace LogAnyValue
|
|
15
|
+
{
|
|
16
|
+
inline std::string format_object(const AnyValue &val, std::unordered_set<const void *> &visited, int depth)
|
|
17
|
+
{
|
|
18
|
+
auto obj = val.as_object();
|
|
19
|
+
|
|
20
|
+
size_t prop_count = obj->props.size();
|
|
21
|
+
bool use_horizontal_layout = prop_count > 0 && prop_count <= HORIZONTAL_OBJECT_MAX_PROPS;
|
|
22
|
+
|
|
23
|
+
if (use_horizontal_layout)
|
|
24
|
+
{
|
|
25
|
+
for (const auto &pair : obj->props)
|
|
26
|
+
{
|
|
27
|
+
if (!is_simple_value(pair.second))
|
|
28
|
+
{
|
|
29
|
+
use_horizontal_layout = false;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
std::string indent(depth * 2, ' ');
|
|
36
|
+
std::string next_indent((depth + 1) * 2, ' ');
|
|
37
|
+
std::stringstream ss;
|
|
38
|
+
|
|
39
|
+
// Special handling for Error objects
|
|
40
|
+
try
|
|
41
|
+
{
|
|
42
|
+
auto is_error = isErrorFn.as_function()->call(isErrorFn, {val}).is_truthy();
|
|
43
|
+
if (is_error)
|
|
44
|
+
{
|
|
45
|
+
auto result = errorToStringFn.as_function()->call(val, {});
|
|
46
|
+
if (result.is_string())
|
|
47
|
+
{
|
|
48
|
+
ss << result.to_std_string() << " ";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (...)
|
|
53
|
+
{
|
|
54
|
+
// ignore
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (use_horizontal_layout)
|
|
58
|
+
{
|
|
59
|
+
ss << "{ ";
|
|
60
|
+
size_t current_prop = 0;
|
|
61
|
+
for (const auto &pair : obj->props)
|
|
62
|
+
{
|
|
63
|
+
if (!is_enumerable_property(pair.second))
|
|
64
|
+
continue;
|
|
65
|
+
|
|
66
|
+
if (is_valid_js_identifier(pair.first))
|
|
67
|
+
{
|
|
68
|
+
ss << pair.first;
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
{
|
|
72
|
+
ss << "\"" << pair.first << "\"";
|
|
73
|
+
}
|
|
74
|
+
ss << ": " << to_log_string(pair.second, visited, depth + 1);
|
|
75
|
+
if (++current_prop < prop_count)
|
|
76
|
+
ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
|
|
77
|
+
}
|
|
78
|
+
ss << " }";
|
|
79
|
+
}
|
|
80
|
+
else
|
|
81
|
+
{
|
|
82
|
+
ss << "{";
|
|
83
|
+
if (prop_count > 0)
|
|
84
|
+
{
|
|
85
|
+
ss << "\n";
|
|
86
|
+
size_t props_shown = 0;
|
|
87
|
+
for (const auto &pair : obj->props)
|
|
88
|
+
{
|
|
89
|
+
if (props_shown >= MAX_OBJECT_PROPS)
|
|
90
|
+
break;
|
|
91
|
+
if (props_shown > 0)
|
|
92
|
+
ss << ",\n";
|
|
93
|
+
if (!is_enumerable_property(pair.second))
|
|
94
|
+
continue;
|
|
95
|
+
|
|
96
|
+
ss << next_indent;
|
|
97
|
+
if (is_valid_js_identifier(pair.first))
|
|
98
|
+
{
|
|
99
|
+
ss << pair.first;
|
|
100
|
+
}
|
|
101
|
+
else
|
|
102
|
+
{
|
|
103
|
+
ss << "\"" << pair.first << "\"";
|
|
104
|
+
}
|
|
105
|
+
ss << ": " << to_log_string(pair.second, visited, depth + 1);
|
|
106
|
+
props_shown++;
|
|
107
|
+
}
|
|
108
|
+
if (prop_count > MAX_OBJECT_PROPS)
|
|
109
|
+
ss << ",\n"
|
|
110
|
+
<< next_indent << Color::BRIGHT_BLACK << "... " << (prop_count - MAX_OBJECT_PROPS) << " more properties" << Color::RESET;
|
|
111
|
+
ss << "\n"
|
|
112
|
+
<< indent;
|
|
113
|
+
}
|
|
114
|
+
ss << "}";
|
|
115
|
+
}
|
|
116
|
+
return ss.str();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include "types.hpp"
|
|
3
|
+
#include "any_value.hpp"
|
|
4
|
+
#include "utils/log_any_value/config.hpp"
|
|
5
|
+
#include "utils/log_any_value/helpers.hpp"
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <optional>
|
|
8
|
+
|
|
9
|
+
namespace jspp
|
|
10
|
+
{
|
|
11
|
+
namespace LogAnyValue
|
|
12
|
+
{
|
|
13
|
+
inline std::optional<std::string> format_primitive(const AnyValue &val, int depth)
|
|
14
|
+
{
|
|
15
|
+
if (val.is_uninitialized())
|
|
16
|
+
return Color::BRIGHT_BLACK + std::string("<uninitialized>") + Color::RESET;
|
|
17
|
+
if (val.is_undefined())
|
|
18
|
+
return Color::BRIGHT_BLACK + std::string("undefined") + Color::RESET;
|
|
19
|
+
if (val.is_null())
|
|
20
|
+
return Color::MAGENTA + std::string("null") + Color::RESET;
|
|
21
|
+
if (val.is_boolean())
|
|
22
|
+
return Color::YELLOW + std::string(val.as_boolean() ? "true" : "false") + Color::RESET;
|
|
23
|
+
if (val.is_number())
|
|
24
|
+
return Color::YELLOW + val.to_std_string() + Color::RESET;
|
|
25
|
+
if (val.is_symbol())
|
|
26
|
+
return Color::BLUE + val.to_std_string() + Color::RESET;
|
|
27
|
+
if (val.is_accessor_descriptor())
|
|
28
|
+
return Color::BLUE + std::string("[Getter/Setter]") + Color::RESET;
|
|
29
|
+
|
|
30
|
+
if (val.is_string())
|
|
31
|
+
{
|
|
32
|
+
const std::string &s = val.as_string()->value;
|
|
33
|
+
if (depth == 0)
|
|
34
|
+
return truncate_string(s);
|
|
35
|
+
return Color::GREEN + std::string("\"") + truncate_string(s) + "\"" + Color::RESET;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return std::nullopt; // Not a primitive
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -13,17 +13,20 @@ namespace jspp
|
|
|
13
13
|
// Implements the ToNumber abstract operation from ECMA-262.
|
|
14
14
|
inline double ToNumber(const AnyValue &val)
|
|
15
15
|
{
|
|
16
|
-
|
|
16
|
+
switch (val.get_type())
|
|
17
|
+
{
|
|
18
|
+
case JsType::Number:
|
|
17
19
|
return val.as_double();
|
|
18
|
-
|
|
20
|
+
case JsType::Null:
|
|
19
21
|
return 0.0;
|
|
20
|
-
|
|
22
|
+
case JsType::Uninitialized:
|
|
23
|
+
case JsType::Undefined:
|
|
21
24
|
return std::numeric_limits<double>::quiet_NaN();
|
|
22
|
-
|
|
25
|
+
case JsType::Boolean:
|
|
23
26
|
return val.as_boolean() ? 1.0 : 0.0;
|
|
24
|
-
|
|
27
|
+
case JsType::String:
|
|
25
28
|
{
|
|
26
|
-
const std::string &s =
|
|
29
|
+
const std::string &s = val.as_string()->value;
|
|
27
30
|
// JS considers empty or whitespace-only strings as 0.
|
|
28
31
|
if (s.empty() || std::all_of(s.begin(), s.end(), isspace))
|
|
29
32
|
return 0.0;
|
|
@@ -43,9 +46,11 @@ namespace jspp
|
|
|
43
46
|
return std::numeric_limits<double>::quiet_NaN();
|
|
44
47
|
}
|
|
45
48
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
default:
|
|
50
|
+
// In a full engine, objects would be converted via valueOf/toString.
|
|
51
|
+
// Here we simplify and return NaN.
|
|
52
|
+
return std::numeric_limits<double>::quiet_NaN();
|
|
53
|
+
}
|
|
49
54
|
}
|
|
50
55
|
// Implements the ToInt32 abstract operation from ECMA-262.
|
|
51
56
|
inline int32_t ToInt32(const AnyValue &val)
|
|
@@ -116,7 +121,7 @@ namespace jspp
|
|
|
116
121
|
{
|
|
117
122
|
// Simplified Abstract Relational Comparison
|
|
118
123
|
if (lhs.is_string() && rhs.is_string())
|
|
119
|
-
return AnyValue::make_boolean(
|
|
124
|
+
return AnyValue::make_boolean(lhs.as_string()->value < rhs.as_string()->value);
|
|
120
125
|
|
|
121
126
|
double l = Operators_Private::ToNumber(lhs);
|
|
122
127
|
double r = Operators_Private::ToNumber(rhs);
|
|
@@ -144,11 +149,25 @@ namespace jspp
|
|
|
144
149
|
}
|
|
145
150
|
inline AnyValue operator==(const AnyValue &lhs, const AnyValue &rhs)
|
|
146
151
|
{
|
|
147
|
-
return AnyValue::make_boolean(lhs.
|
|
152
|
+
return AnyValue::make_boolean(lhs.is_equal_to_primitive(rhs));
|
|
148
153
|
}
|
|
149
154
|
inline AnyValue operator!=(const AnyValue &lhs, const AnyValue &rhs)
|
|
150
155
|
{
|
|
151
|
-
return AnyValue::make_boolean(!lhs.
|
|
156
|
+
return AnyValue::make_boolean(!lhs.is_equal_to_primitive(rhs));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// --- LOGICAL OPERATORS
|
|
160
|
+
inline AnyValue operator||(const AnyValue &lhs, const AnyValue &rhs)
|
|
161
|
+
{
|
|
162
|
+
if (lhs.is_truthy())
|
|
163
|
+
return lhs;
|
|
164
|
+
return rhs;
|
|
165
|
+
}
|
|
166
|
+
inline AnyValue operator&&(const AnyValue &lhs, const AnyValue &rhs)
|
|
167
|
+
{
|
|
168
|
+
if (!lhs.is_truthy())
|
|
169
|
+
return lhs;
|
|
170
|
+
return rhs;
|
|
152
171
|
}
|
|
153
172
|
|
|
154
173
|
// --- BITWISE OPERATORS
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "values/symbol.hpp"
|
|
4
|
+
#include <memory>
|
|
5
|
+
|
|
6
|
+
namespace jspp
|
|
7
|
+
{
|
|
8
|
+
namespace WellKnownSymbols
|
|
9
|
+
{
|
|
10
|
+
// We use a specific prefix "@@" for well-known symbols to distinguish them from user symbols
|
|
11
|
+
inline std::shared_ptr<JsSymbol> iterator = std::make_shared<JsSymbol>("Symbol.iterator", "@@iterator");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
namespace jspp
|
|
7
7
|
{
|
|
8
|
+
// Forward declaration of AnyValue
|
|
8
9
|
class AnyValue;
|
|
9
10
|
|
|
10
11
|
struct JsArray
|
|
@@ -16,12 +17,14 @@ namespace jspp
|
|
|
16
17
|
|
|
17
18
|
JsArray() = default;
|
|
18
19
|
explicit JsArray(const std::vector<std::optional<AnyValue>> &items) : dense(items), length(items.size()) {}
|
|
20
|
+
explicit JsArray(std::vector<std::optional<AnyValue>> &&items) : dense(std::move(items)), length(dense.size()) {}
|
|
19
21
|
|
|
20
22
|
std::string to_std_string() const;
|
|
23
|
+
JsIterator<AnyValue> get_iterator();
|
|
21
24
|
|
|
22
|
-
AnyValue get_property(const std::string &key);
|
|
25
|
+
AnyValue get_property(const std::string &key, const AnyValue &thisVal);
|
|
23
26
|
AnyValue get_property(uint32_t idx);
|
|
24
|
-
AnyValue set_property(const std::string &key, const AnyValue &value);
|
|
27
|
+
AnyValue set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal);
|
|
25
28
|
AnyValue set_property(uint32_t idx, const AnyValue &value);
|
|
26
29
|
|
|
27
30
|
static bool is_array_index(const std::string &s)
|
|
@@ -17,8 +17,8 @@ namespace jspp
|
|
|
17
17
|
|
|
18
18
|
struct AccessorDescriptor
|
|
19
19
|
{
|
|
20
|
-
std::optional<std::function<AnyValue(const std::vector<AnyValue> &)>> get; // getter = function or undefined
|
|
21
|
-
std::optional<std::function<AnyValue(const std::vector<AnyValue> &)>> set; // setter = function or undefined
|
|
20
|
+
std::optional<std::function<AnyValue(const AnyValue &, const std::vector<AnyValue> &)>> get; // getter = function or undefined
|
|
21
|
+
std::optional<std::function<AnyValue(const AnyValue &, const std::vector<AnyValue> &)>> set; // setter = function or undefined
|
|
22
22
|
bool enumerable = false;
|
|
23
23
|
bool configurable = true;
|
|
24
24
|
};
|
|
@@ -1,19 +1,76 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include <variant> // Ensure variant is included
|
|
5
|
+
|
|
6
|
+
namespace jspp
|
|
7
|
+
{
|
|
8
|
+
// Forward declaration of AnyValue
|
|
9
|
+
class AnyValue;
|
|
10
|
+
|
|
11
|
+
using JsFunctionCallable = std::variant<std::function<AnyValue(const AnyValue &, const std::vector<AnyValue> &)>, // 0: Normal
|
|
12
|
+
std::function<jspp::JsIterator<jspp::AnyValue>(const AnyValue &, const std::vector<jspp::AnyValue> &)>, // 1: Generator
|
|
13
|
+
std::function<jspp::JsPromise(const AnyValue &, const std::vector<jspp::AnyValue> &)>>; // 2: Async
|
|
14
|
+
|
|
15
|
+
struct JsFunction
|
|
16
|
+
{
|
|
17
|
+
JsFunctionCallable callable;
|
|
18
|
+
std::string name;
|
|
19
|
+
std::unordered_map<std::string, AnyValue> props;
|
|
20
|
+
std::shared_ptr<AnyValue> proto = nullptr;
|
|
21
|
+
bool is_generator;
|
|
22
|
+
bool is_async;
|
|
23
|
+
bool is_class;
|
|
24
|
+
|
|
25
|
+
// ---- Constructor A: infer flags ----
|
|
26
|
+
JsFunction(const JsFunctionCallable &c,
|
|
27
|
+
std::string n = {},
|
|
28
|
+
std::unordered_map<std::string, AnyValue> p = {},
|
|
29
|
+
bool is_cls = false)
|
|
30
|
+
: callable(c),
|
|
31
|
+
name(std::move(n)),
|
|
32
|
+
props(std::move(p)),
|
|
33
|
+
is_generator(callable.index() == 1),
|
|
34
|
+
is_async(callable.index() == 2),
|
|
35
|
+
is_class(is_cls)
|
|
36
|
+
{
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ---- Constructor B: explicit generator flag (backward compat) ----
|
|
40
|
+
JsFunction(const JsFunctionCallable &c,
|
|
41
|
+
bool is_gen,
|
|
42
|
+
std::string n = {},
|
|
43
|
+
std::unordered_map<std::string, AnyValue> p = {},
|
|
44
|
+
bool is_cls = false)
|
|
45
|
+
: callable(c),
|
|
46
|
+
name(std::move(n)),
|
|
47
|
+
props(std::move(p)),
|
|
48
|
+
is_generator(is_gen),
|
|
49
|
+
is_async(callable.index() == 2),
|
|
50
|
+
is_class(is_cls)
|
|
51
|
+
{
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ---- Constructor C: explicit async flag ----
|
|
55
|
+
JsFunction(const JsFunctionCallable &c,
|
|
56
|
+
bool is_gen,
|
|
57
|
+
bool is_async_func,
|
|
58
|
+
std::string n = {},
|
|
59
|
+
std::unordered_map<std::string, AnyValue> p = {},
|
|
60
|
+
bool is_cls = false)
|
|
61
|
+
: callable(c),
|
|
62
|
+
name(std::move(n)),
|
|
63
|
+
props(std::move(p)),
|
|
64
|
+
is_generator(is_gen),
|
|
65
|
+
is_async(is_async_func),
|
|
66
|
+
is_class(is_cls)
|
|
67
|
+
{
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
std::string to_std_string() const;
|
|
71
|
+
AnyValue call(const AnyValue &thisVal, const std::vector<AnyValue> &args);
|
|
72
|
+
|
|
73
|
+
AnyValue get_property(const std::string &key, const AnyValue &thisVal);
|
|
74
|
+
AnyValue set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal);
|
|
75
|
+
};
|
|
76
|
+
}
|