@ugo-studio/jspp 0.3.0 → 0.3.1
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/LICENSE +25 -25
- package/README.md +20 -12
- package/dist/analysis/scope.js +5 -3
- package/dist/analysis/typeAnalyzer.js +21 -25
- package/dist/cli/index.js +14 -4
- package/dist/cli/utils.js +61 -0
- package/dist/core/codegen/control-flow-handlers.js +10 -9
- package/dist/core/codegen/declaration-handlers.js +10 -3
- package/dist/core/codegen/destructuring-handlers.js +9 -4
- package/dist/core/codegen/expression-handlers.js +40 -29
- package/dist/core/codegen/function-handlers.js +78 -12
- package/dist/core/codegen/helpers.js +91 -14
- package/dist/core/codegen/index.js +4 -2
- package/dist/core/codegen/statement-handlers.js +8 -6
- package/package.json +2 -2
- package/scripts/precompile-headers.ts +249 -50
- package/scripts/setup-compiler.ts +63 -63
- package/src/prelude/any_value.cpp +636 -0
- package/src/prelude/any_value.hpp +23 -16
- package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
- package/src/prelude/exception.hpp +27 -27
- package/src/prelude/iterator_instantiations.hpp +10 -0
- package/src/prelude/{index.hpp → jspp.hpp} +10 -16
- package/src/prelude/library/array.cpp +191 -0
- package/src/prelude/library/array.hpp +5 -178
- package/src/prelude/library/console.cpp +125 -0
- package/src/prelude/library/console.hpp +9 -97
- package/src/prelude/library/error.cpp +100 -0
- package/src/prelude/library/error.hpp +8 -108
- package/src/prelude/library/function.cpp +69 -0
- package/src/prelude/library/function.hpp +6 -5
- package/src/prelude/library/global.cpp +96 -0
- package/src/prelude/library/global.hpp +12 -28
- package/src/prelude/library/global_usings.hpp +15 -0
- package/src/prelude/library/math.cpp +258 -0
- package/src/prelude/library/math.hpp +6 -288
- package/src/prelude/library/object.cpp +379 -0
- package/src/prelude/library/object.hpp +5 -267
- package/src/prelude/library/performance.cpp +21 -0
- package/src/prelude/library/performance.hpp +5 -20
- package/src/prelude/library/process.cpp +38 -0
- package/src/prelude/library/process.hpp +3 -31
- package/src/prelude/library/promise.cpp +131 -0
- package/src/prelude/library/promise.hpp +5 -116
- package/src/prelude/library/symbol.cpp +56 -0
- package/src/prelude/library/symbol.hpp +5 -46
- package/src/prelude/library/timer.cpp +88 -0
- package/src/prelude/library/timer.hpp +11 -87
- package/src/prelude/runtime.cpp +19 -0
- package/src/prelude/types.hpp +17 -12
- package/src/prelude/utils/access.hpp +122 -31
- package/src/prelude/utils/assignment_operators.hpp +99 -99
- package/src/prelude/utils/log_any_value/array.hpp +61 -40
- package/src/prelude/utils/log_any_value/function.hpp +39 -39
- package/src/prelude/utils/log_any_value/object.hpp +60 -3
- package/src/prelude/utils/operators.hpp +25 -10
- package/src/prelude/utils/operators_primitive.hpp +336 -336
- package/src/prelude/utils/well_known_symbols.hpp +24 -24
- package/src/prelude/values/array.cpp +1399 -0
- package/src/prelude/values/array.hpp +4 -0
- package/src/prelude/values/async_iterator.cpp +251 -0
- package/src/prelude/values/async_iterator.hpp +60 -32
- package/src/prelude/values/function.cpp +262 -0
- package/src/prelude/values/function.hpp +10 -30
- package/src/prelude/values/iterator.cpp +309 -0
- package/src/prelude/values/iterator.hpp +33 -64
- package/src/prelude/values/number.cpp +176 -0
- package/src/prelude/values/object.cpp +159 -0
- package/src/prelude/values/object.hpp +4 -0
- package/src/prelude/values/promise.cpp +479 -0
- package/src/prelude/values/promise.hpp +9 -2
- package/src/prelude/values/prototypes/array.hpp +46 -1348
- package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
- package/src/prelude/values/prototypes/function.hpp +7 -46
- package/src/prelude/values/prototypes/iterator.hpp +15 -191
- package/src/prelude/values/prototypes/number.hpp +23 -210
- package/src/prelude/values/prototypes/object.hpp +7 -23
- package/src/prelude/values/prototypes/promise.hpp +8 -186
- package/src/prelude/values/prototypes/string.hpp +28 -553
- package/src/prelude/values/prototypes/symbol.hpp +9 -70
- package/src/prelude/values/shape.hpp +52 -52
- package/src/prelude/values/string.cpp +485 -0
- package/src/prelude/values/symbol.cpp +89 -0
- package/src/prelude/values/symbol.hpp +101 -101
- package/src/prelude/any_value_access.hpp +0 -170
- package/src/prelude/any_value_defines.hpp +0 -190
- package/src/prelude/any_value_helpers.hpp +0 -374
- package/src/prelude/values/helpers/array.hpp +0 -199
- package/src/prelude/values/helpers/async_iterator.hpp +0 -275
- package/src/prelude/values/helpers/function.hpp +0 -109
- package/src/prelude/values/helpers/iterator.hpp +0 -145
- package/src/prelude/values/helpers/object.hpp +0 -104
- package/src/prelude/values/helpers/promise.hpp +0 -254
- package/src/prelude/values/helpers/string.hpp +0 -37
- package/src/prelude/values/helpers/symbol.hpp +0 -21
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include <atomic>
|
|
5
|
-
#include <string>
|
|
6
|
-
#include <unordered_map>
|
|
7
|
-
#include <unordered_set>
|
|
8
|
-
#include <optional>
|
|
9
|
-
|
|
10
|
-
namespace jspp
|
|
11
|
-
{
|
|
12
|
-
// Forward declaration of AnyValue
|
|
13
|
-
class AnyValue;
|
|
14
|
-
|
|
15
|
-
struct JsSymbol : HeapObject
|
|
16
|
-
{
|
|
17
|
-
std::string description;
|
|
18
|
-
std::string key; // Internal unique key used for AnyValue property maps
|
|
19
|
-
|
|
20
|
-
JsType get_heap_type() const override { return JsType::Symbol; }
|
|
21
|
-
|
|
22
|
-
// --- Registries ---
|
|
23
|
-
|
|
24
|
-
// 1. Global Symbol Registry (for Symbol.for/keyFor)
|
|
25
|
-
static std::unordered_map<std::string, JsSymbol*> ®istry()
|
|
26
|
-
{
|
|
27
|
-
static std::unordered_map<std::string, JsSymbol*> reg;
|
|
28
|
-
return reg;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 2. All Internal Keys Registry (for robust Object.keys filtering)
|
|
32
|
-
static std::unordered_set<std::string> &internal_keys_registry()
|
|
33
|
-
{
|
|
34
|
-
static std::unordered_set<std::string> keys;
|
|
35
|
-
return keys;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
static bool is_internal_key(const std::string &k)
|
|
39
|
-
{
|
|
40
|
-
return internal_keys_registry().count(k) > 0;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// --- Constructors ---
|
|
44
|
-
|
|
45
|
-
// Standard Constructor (creates unique symbol)
|
|
46
|
-
JsSymbol(const std::string &desc) : description(desc)
|
|
47
|
-
{
|
|
48
|
-
static std::atomic<uint64_t> id_counter{0};
|
|
49
|
-
// Ensure unique internal key for property storage
|
|
50
|
-
key = "__Sym" + std::to_string(id_counter++) + "_" + desc;
|
|
51
|
-
|
|
52
|
-
// Register this key as a valid symbol key
|
|
53
|
-
internal_keys_registry().insert(key);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Constructor for Well-Known Symbols (fixed keys)
|
|
57
|
-
JsSymbol(const std::string &desc, const std::string &fixed_key)
|
|
58
|
-
: description(desc), key(fixed_key)
|
|
59
|
-
{
|
|
60
|
-
// Register this key as a valid symbol key
|
|
61
|
-
internal_keys_registry().insert(key);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// --- Global Registry API ---
|
|
65
|
-
|
|
66
|
-
// Implements Symbol.for(key)
|
|
67
|
-
static JsSymbol* for_global(const std::string ®istryKey)
|
|
68
|
-
{
|
|
69
|
-
auto ® = registry();
|
|
70
|
-
auto it = reg.find(registryKey);
|
|
71
|
-
if (it != reg.end())
|
|
72
|
-
{
|
|
73
|
-
return it->second;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Create new symbol with description = registryKey
|
|
77
|
-
auto newSym = new JsSymbol(registryKey);
|
|
78
|
-
newSym->ref(); // Keep it alive in registry
|
|
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
|
-
if (pair.second == sym)
|
|
90
|
-
{
|
|
91
|
-
return pair.first;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return std::nullopt;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// --- Methods ---
|
|
98
|
-
std::string to_std_string() const;
|
|
99
|
-
AnyValue get_property(const std::string &key, const AnyValue &thisVal);
|
|
100
|
-
};
|
|
101
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include <atomic>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <unordered_map>
|
|
7
|
+
#include <unordered_set>
|
|
8
|
+
#include <optional>
|
|
9
|
+
|
|
10
|
+
namespace jspp
|
|
11
|
+
{
|
|
12
|
+
// Forward declaration of AnyValue
|
|
13
|
+
class AnyValue;
|
|
14
|
+
|
|
15
|
+
struct JsSymbol : HeapObject
|
|
16
|
+
{
|
|
17
|
+
std::string description;
|
|
18
|
+
std::string key; // Internal unique key used for AnyValue property maps
|
|
19
|
+
|
|
20
|
+
JsType get_heap_type() const override { return JsType::Symbol; }
|
|
21
|
+
|
|
22
|
+
// --- Registries ---
|
|
23
|
+
|
|
24
|
+
// 1. Global Symbol Registry (for Symbol.for/keyFor)
|
|
25
|
+
static std::unordered_map<std::string, JsSymbol*> ®istry()
|
|
26
|
+
{
|
|
27
|
+
static std::unordered_map<std::string, JsSymbol*> reg;
|
|
28
|
+
return reg;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 2. All Internal Keys Registry (for robust Object.keys filtering)
|
|
32
|
+
static std::unordered_set<std::string> &internal_keys_registry()
|
|
33
|
+
{
|
|
34
|
+
static std::unordered_set<std::string> keys;
|
|
35
|
+
return keys;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static bool is_internal_key(const std::string &k)
|
|
39
|
+
{
|
|
40
|
+
return internal_keys_registry().count(k) > 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// --- Constructors ---
|
|
44
|
+
|
|
45
|
+
// Standard Constructor (creates unique symbol)
|
|
46
|
+
JsSymbol(const std::string &desc) : description(desc)
|
|
47
|
+
{
|
|
48
|
+
static std::atomic<uint64_t> id_counter{0};
|
|
49
|
+
// Ensure unique internal key for property storage
|
|
50
|
+
key = "__Sym" + std::to_string(id_counter++) + "_" + desc;
|
|
51
|
+
|
|
52
|
+
// Register this key as a valid symbol key
|
|
53
|
+
internal_keys_registry().insert(key);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Constructor for Well-Known Symbols (fixed keys)
|
|
57
|
+
JsSymbol(const std::string &desc, const std::string &fixed_key)
|
|
58
|
+
: description(desc), key(fixed_key)
|
|
59
|
+
{
|
|
60
|
+
// Register this key as a valid symbol key
|
|
61
|
+
internal_keys_registry().insert(key);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// --- Global Registry API ---
|
|
65
|
+
|
|
66
|
+
// Implements Symbol.for(key)
|
|
67
|
+
static JsSymbol* for_global(const std::string ®istryKey)
|
|
68
|
+
{
|
|
69
|
+
auto ® = registry();
|
|
70
|
+
auto it = reg.find(registryKey);
|
|
71
|
+
if (it != reg.end())
|
|
72
|
+
{
|
|
73
|
+
return it->second;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Create new symbol with description = registryKey
|
|
77
|
+
auto newSym = new JsSymbol(registryKey);
|
|
78
|
+
newSym->ref(); // Keep it alive in registry
|
|
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
|
+
if (pair.second == sym)
|
|
90
|
+
{
|
|
91
|
+
return pair.first;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return std::nullopt;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// --- Methods ---
|
|
98
|
+
std::string to_std_string() const;
|
|
99
|
+
AnyValue get_property(const std::string &key, const AnyValue &thisVal);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -1,170 +0,0 @@
|
|
|
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 SymbolPrototypes::get(key).has_value();
|
|
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 StringPrototypes::get(key).has_value();
|
|
39
|
-
case JsType::Number:
|
|
40
|
-
return NumberPrototypes::get(key).has_value();
|
|
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, 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);
|
|
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, 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, 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, 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
|
-
}
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "any_value.hpp"
|
|
5
|
-
#include "values/object.hpp"
|
|
6
|
-
#include "values/function.hpp"
|
|
7
|
-
|
|
8
|
-
namespace jspp
|
|
9
|
-
{
|
|
10
|
-
inline void AnyValue::define_data_property(const std::string &key, AnyValue value)
|
|
11
|
-
{
|
|
12
|
-
if (is_object())
|
|
13
|
-
{
|
|
14
|
-
auto obj = as_object();
|
|
15
|
-
auto offset = obj->shape->get_offset(key);
|
|
16
|
-
if (offset.has_value())
|
|
17
|
-
{
|
|
18
|
-
obj->storage[offset.value()] = value;
|
|
19
|
-
}
|
|
20
|
-
else
|
|
21
|
-
{
|
|
22
|
-
obj->shape = obj->shape->transition(key);
|
|
23
|
-
obj->storage.push_back(value);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
else if (is_function())
|
|
27
|
-
{
|
|
28
|
-
as_function()->props[key] = value;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
inline void AnyValue::define_data_property(const AnyValue &key, AnyValue value)
|
|
33
|
-
{
|
|
34
|
-
if (key.is_symbol())
|
|
35
|
-
define_data_property(key.as_symbol()->key, value);
|
|
36
|
-
else
|
|
37
|
-
define_data_property(key.to_std_string(), value);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
inline void AnyValue::define_data_property(const std::string &key, AnyValue value, bool writable, bool enumerable, bool configurable)
|
|
41
|
-
{
|
|
42
|
-
define_data_property(key, AnyValue::make_data_descriptor(value, writable, enumerable, configurable));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
inline void AnyValue::define_getter(const std::string &key, AnyValue getter)
|
|
46
|
-
{
|
|
47
|
-
if (is_object())
|
|
48
|
-
{
|
|
49
|
-
auto obj = as_object();
|
|
50
|
-
auto offset = obj->shape->get_offset(key);
|
|
51
|
-
|
|
52
|
-
if (offset.has_value())
|
|
53
|
-
{
|
|
54
|
-
auto &val = obj->storage[offset.value()];
|
|
55
|
-
if (val.is_accessor_descriptor())
|
|
56
|
-
{
|
|
57
|
-
auto desc = val.as_accessor_descriptor();
|
|
58
|
-
desc->get = [getter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
59
|
-
{
|
|
60
|
-
return getter.call(thisVal, args);
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
else
|
|
64
|
-
{
|
|
65
|
-
auto getFunc = [getter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
66
|
-
{
|
|
67
|
-
return getter.call(thisVal, args);
|
|
68
|
-
};
|
|
69
|
-
obj->storage[offset.value()] = AnyValue::make_accessor_descriptor(getFunc, std::nullopt, true, true);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
else
|
|
73
|
-
{
|
|
74
|
-
auto getFunc = [getter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
75
|
-
{
|
|
76
|
-
return getter.call(thisVal, args);
|
|
77
|
-
};
|
|
78
|
-
obj->shape = obj->shape->transition(key);
|
|
79
|
-
obj->storage.push_back(AnyValue::make_accessor_descriptor(getFunc, std::nullopt, true, true));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
else if (is_function())
|
|
83
|
-
{
|
|
84
|
-
auto &props = as_function()->props;
|
|
85
|
-
auto it = props.find(key);
|
|
86
|
-
if (it != props.end() && it->second.is_accessor_descriptor())
|
|
87
|
-
{
|
|
88
|
-
auto desc = it->second.as_accessor_descriptor();
|
|
89
|
-
desc->get = [getter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
90
|
-
{
|
|
91
|
-
return getter.call(thisVal, args);
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
else
|
|
95
|
-
{
|
|
96
|
-
auto getFunc = [getter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
97
|
-
{
|
|
98
|
-
return getter.call(thisVal, args);
|
|
99
|
-
};
|
|
100
|
-
props[key] = AnyValue::make_accessor_descriptor(getFunc, std::nullopt, true, true);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
inline void AnyValue::define_getter(const AnyValue &key, AnyValue getter)
|
|
106
|
-
{
|
|
107
|
-
if (key.is_symbol())
|
|
108
|
-
define_getter(key.as_symbol()->key, getter);
|
|
109
|
-
else
|
|
110
|
-
define_getter(key.to_std_string(), getter);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
inline void AnyValue::define_setter(const std::string &key, AnyValue setter)
|
|
114
|
-
{
|
|
115
|
-
if (is_object())
|
|
116
|
-
{
|
|
117
|
-
auto obj = as_object();
|
|
118
|
-
auto offset = obj->shape->get_offset(key);
|
|
119
|
-
|
|
120
|
-
if (offset.has_value())
|
|
121
|
-
{
|
|
122
|
-
auto &val = obj->storage[offset.value()];
|
|
123
|
-
if (val.is_accessor_descriptor())
|
|
124
|
-
{
|
|
125
|
-
auto desc = val.as_accessor_descriptor();
|
|
126
|
-
desc->set = [setter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
127
|
-
{
|
|
128
|
-
if (args.empty())
|
|
129
|
-
return Constants::UNDEFINED;
|
|
130
|
-
return setter.call(thisVal, args);
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
else
|
|
134
|
-
{
|
|
135
|
-
auto setFunc = [setter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
136
|
-
{
|
|
137
|
-
if (args.empty())
|
|
138
|
-
return Constants::UNDEFINED;
|
|
139
|
-
return setter.call(thisVal, args);
|
|
140
|
-
};
|
|
141
|
-
obj->storage[offset.value()] = AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
else
|
|
145
|
-
{
|
|
146
|
-
auto setFunc = [setter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
147
|
-
{
|
|
148
|
-
if (args.empty())
|
|
149
|
-
return Constants::UNDEFINED;
|
|
150
|
-
return setter.call(thisVal, args);
|
|
151
|
-
};
|
|
152
|
-
obj->shape = obj->shape->transition(key);
|
|
153
|
-
obj->storage.push_back(AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true));
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
else if (is_function())
|
|
157
|
-
{
|
|
158
|
-
auto &props = as_function()->props;
|
|
159
|
-
auto it = props.find(key);
|
|
160
|
-
if (it != props.end() && it->second.is_accessor_descriptor())
|
|
161
|
-
{
|
|
162
|
-
auto desc = it->second.as_accessor_descriptor();
|
|
163
|
-
desc->set = [setter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
164
|
-
{
|
|
165
|
-
if (args.empty())
|
|
166
|
-
return Constants::UNDEFINED;
|
|
167
|
-
return setter.call(thisVal, args);
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
else
|
|
171
|
-
{
|
|
172
|
-
auto setFunc = [setter](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
173
|
-
{
|
|
174
|
-
if (args.empty())
|
|
175
|
-
return Constants::UNDEFINED;
|
|
176
|
-
return setter.call(thisVal, args);
|
|
177
|
-
};
|
|
178
|
-
props[key] = AnyValue::make_accessor_descriptor(std::nullopt, setFunc, true, true);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
inline void AnyValue::define_setter(const AnyValue &key, AnyValue setter)
|
|
184
|
-
{
|
|
185
|
-
if (key.is_symbol())
|
|
186
|
-
define_setter(key.as_symbol()->key, setter);
|
|
187
|
-
else
|
|
188
|
-
define_setter(key.to_std_string(), setter);
|
|
189
|
-
}
|
|
190
|
-
}
|