Skip to content

Commit e723438

Browse files
committed
add Value pool for visitor
1 parent 60c9a83 commit e723438

File tree

9 files changed

+1129
-167
lines changed

9 files changed

+1129
-167
lines changed

CMakeLists.txt

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ target_link_libraries(rift fmt GeodeResult)
1414

1515
# Allows an easier conversion of rift::Value to JSON and vice versa
1616
if (RIFT_INCLUDE_MATJSON)
17-
CPMAddPackage("gh:geode-sdk/json#82b1552")
17+
CPMAddPackage("gh:geode-sdk/json#0ea6009")
1818
target_compile_definitions(rift PUBLIC RIFT_INCLUDE_MATJSON)
1919
target_link_libraries(rift mat-json)
2020
endif()
@@ -28,3 +28,16 @@ if (RIFT_BUILD_TESTS)
2828
add_executable(rift_test test/main.cpp)
2929
target_link_libraries(rift_test rift)
3030
endif()
31+
32+
if (RIFT_BUILD_BENCHMARKS)
33+
# for comparison
34+
set(BENCHMARK_ENABLE_TESTING OFF)
35+
CPMAddPackage("gh:google/benchmark#v1.8.5")
36+
add_executable(rift_benchmark test/benchmark.cpp)
37+
target_link_libraries(rift_benchmark rift benchmark::benchmark fmt)
38+
endif()
39+
40+
if (RIFT_BUILD_FUZZERS)
41+
add_executable(rift_fuzzer test/fuzzer.cpp)
42+
target_link_libraries(rift_fuzzer rift)
43+
endif()

include/rift/config.hpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ namespace rift {
2424
|| std::same_as<T, Array>
2525
|| std::same_as<T, Object>;
2626

27+
using FuncArgs = std::span<Value*>;
28+
2729
using RuntimeFuncResult = geode::Result<Value>;
28-
using RuntimeFunction = std::function<RuntimeFuncResult(std::span<Value const>)>;
30+
using RuntimeFunction = std::function<RuntimeFuncResult(FuncArgs)>;
2931
using RuntimeFunctions = std::unordered_map<std::string, RuntimeFunction>;
3032

3133
/// @brief Global configuration for the Rift library.
@@ -78,29 +80,29 @@ namespace rift {
7880

7981
private:
8082
template <size_t I, typename T, typename... Args>
81-
static geode::Result<std::tuple<T, Args...>> unwrapArgsImpl(std::span<Value const> args) {
83+
static geode::Result<std::tuple<T, Args...>> unwrapArgsImpl(FuncArgs args) {
8284
if constexpr (sizeof...(Args) > 0) {
8385
auto rest = unwrapArgsImpl<I + 1, Args...>(args);
8486
if (rest.isErr()) {
8587
return geode::Err(rest.unwrapErr());
8688
}
87-
auto canUnwrap = args[I].is<T>();
89+
auto canUnwrap = args[I]->is<T>();
8890
if (!canUnwrap) {
8991
return geode::Err("Argument type mismatch");
9092
}
91-
return geode::Ok(std::tuple_cat(std::tuple<T>{args[I].to<T>()}, std::move(rest.unwrap())));
93+
return geode::Ok(std::tuple_cat(std::tuple<T>{args[I]->to<T>()}, std::move(rest.unwrap())));
9294
} else {
93-
return geode::Ok(std::tuple<T>(args[I].to<T>()));
95+
return geode::Ok(std::tuple<T>(args[I]->to<T>()));
9496
}
9597
}
9698

9799
template <std::size_t I>
98-
static geode::Result<std::tuple<>> unwrapArgsImpl(std::span<const Value>) {
100+
static geode::Result<std::tuple<>> unwrapArgsImpl(FuncArgs) {
99101
return geode::Ok(std::tuple());
100102
}
101103

102104
template <typename... Args>
103-
static geode::Result<std::tuple<Args...>> unwrapArgs(std::span<Value const> args) {
105+
static geode::Result<std::tuple<Args...>> unwrapArgs(FuncArgs args) {
104106
if constexpr (sizeof...(Args) != 0) {
105107
if (args.size() != sizeof...(Args)) {
106108
return geode::Err("Argument count mismatch");
@@ -120,7 +122,7 @@ namespace rift {
120122
/// @param func the function pointer
121123
template <typename Ret, typename... Args>
122124
void makeFunction(std::string const& name, Ret(*func)(Args...)) noexcept {
123-
m_functions[name] = [func](std::span<Value const> args) -> RuntimeFuncResult {
125+
m_functions[name] = [func](FuncArgs args) -> RuntimeFuncResult {
124126
// Unwrap the arguments with deduced types
125127
auto res = unwrapArgs<Args...>(args);
126128
if (res.isErr()) {

include/rift/value.hpp

+21
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ namespace rift {
199199
Result operator%(const Value& other) const noexcept;
200200
Result operator^(const Value& other) const noexcept;
201201

202+
geode::Result<> operator_add(const Value& other) noexcept;
203+
geode::Result<> operator_sub(const Value& other) noexcept;
204+
geode::Result<> operator_mul(const Value& other) noexcept;
205+
geode::Result<> operator_div(const Value& other) noexcept;
206+
geode::Result<> operator_mod(const Value& other) noexcept;
207+
geode::Result<> operator_pow(const Value& other) noexcept;
208+
202209
// Comparison operators
203210

204211
Value operator==(const Value& other) const noexcept;
@@ -208,17 +215,31 @@ namespace rift {
208215
Value operator<=(const Value& other) const noexcept;
209216
Value operator>=(const Value& other) const noexcept;
210217

218+
void operator_eq(const Value& other) noexcept;
219+
void operator_ne(const Value& other) noexcept;
220+
void operator_lt(const Value& other) noexcept;
221+
void operator_gt(const Value& other) noexcept;
222+
void operator_le(const Value& other) noexcept;
223+
void operator_ge(const Value& other) noexcept;
224+
211225
// Logical operators
212226

213227
Value operator&&(const Value& other) const noexcept;
214228
Value operator||(const Value& other) const noexcept;
215229
Value operator!() const noexcept;
216230

231+
void operator_and(const Value& other) noexcept;
232+
void operator_or(const Value& other) noexcept;
233+
void operator_not() noexcept;
234+
217235
// Special operators
218236

219237
Value operator-() const noexcept;
220238
Value at(const Value& key) const noexcept;
221239

240+
void operator_minus() noexcept;
241+
void operator_plus() noexcept;
242+
222243
// Object/Array access operators
223244

224245
Value operator[](size_t index) const noexcept;

include/rift/visitor.hpp

+20-12
Original file line numberDiff line numberDiff line change
@@ -21,67 +21,75 @@ namespace rift {
2121

2222
class Visitor {
2323
public:
24-
explicit Visitor(Object const& variables) noexcept : m_variables(variables) {}
24+
explicit Visitor(Object const& variables) noexcept;
25+
26+
using VisitorResultImpl = geode::Result<Value&, RuntimeError>;
2527

2628
/// @brief Visit a node and evaluate its value.
2729
/// @param node the node to visit
2830
/// @return the result of the evaluation as a VisitorResult containing the value or an error
2931
[[nodiscard]] VisitorResult visit(Node const& node) noexcept;
3032

33+
private:
34+
/// @brief Visit a node and evaluate its value.
35+
/// @param node the node to visit
36+
/// @return the result of the evaluation as a VisitorResult containing the value or an error
37+
[[nodiscard]] VisitorResultImpl visitImpl(Node const& node) noexcept;
38+
3139
/// @brief Visit a root node and evaluate its value.
3240
/// @param node the root node to visit
3341
/// @return the result of the evaluation as a VisitorResult containing the value
34-
[[nodiscard]] VisitorResult visit(RootNode const& node) noexcept;
42+
[[nodiscard]] VisitorResultImpl visit(RootNode const& node) noexcept;
3543

3644
/// @brief Visit an identifier node and evaluate its value.
3745
/// @param node the identifier node to visit
3846
/// @return the result of the evaluation as a VisitorResult containing the value
39-
[[nodiscard]] VisitorResult visit(IdentifierNode const& node) noexcept;
47+
[[nodiscard]] VisitorResultImpl visit(IdentifierNode const& node) noexcept;
4048

4149
/// @brief Visit a binary node and evaluate its value.
4250
/// @param node the binary node to visit
4351
/// @return the result of the evaluation as a VisitorResult containing the value
44-
[[nodiscard]] VisitorResult visit(BinaryNode const& node) noexcept;
52+
[[nodiscard]] VisitorResultImpl visit(BinaryNode const& node) noexcept;
4553

4654
/// @brief Visit a unary node and evaluate its value.
4755
/// @param node the unary node to visit
4856
/// @return the result of the evaluation as a VisitorResult containing the value
49-
[[nodiscard]] VisitorResult visit(UnaryNode const& node) noexcept;
57+
[[nodiscard]] VisitorResultImpl visit(UnaryNode const& node) noexcept;
5058

5159
/// @brief Visit a ternary node and evaluate its value.
5260
/// @param node the ternary node to visit
5361
/// @return the result of the evaluation as a VisitorResult containing the value
54-
[[nodiscard]] VisitorResult visit(TernaryNode const& node) noexcept;
62+
[[nodiscard]] VisitorResultImpl visit(TernaryNode const& node) noexcept;
5563

5664
/// @brief Visit a call node and evaluate its value.
5765
/// @param node the call node to visit
5866
/// @return the result of the evaluation as a VisitorResult containing the value
59-
[[nodiscard]] VisitorResult visit(CallNode const& node) noexcept;
67+
[[nodiscard]] VisitorResultImpl visit(CallNode const& node) noexcept;
6068

6169
/// @brief Visit an accessor node and evaluate its value.
6270
/// @param node the accessor node to visit
6371
/// @return the result of the evaluation as a VisitorResult containing the value
64-
[[nodiscard]] VisitorResult visit(AccessorNode const& node) noexcept;
72+
[[nodiscard]] VisitorResultImpl visit(AccessorNode const& node) noexcept;
6573

6674
/// @brief Visit an indexer node and evaluate its value.
6775
/// @param node the indexer node to visit
6876
/// @return the result of the evaluation as a VisitorResult containing the value
69-
[[nodiscard]] VisitorResult visit(IndexerNode const& node) noexcept;
77+
[[nodiscard]] VisitorResultImpl visit(IndexerNode const& node) noexcept;
7078

7179
/// @brief Visit a for loop node and evaluate its value.
7280
/// @param node the for loop node to visit
7381
/// @return the result of the evaluation as a VisitorResult containing the value
74-
[[nodiscard]] VisitorResult visit(ForNode const& node) noexcept;
82+
[[nodiscard]] VisitorResultImpl visit(ForNode const& node) noexcept;
7583

7684
/// @brief Visit a script node and evaluate its value.
7785
/// @param node the script node to visit
7886
/// @return the result of the evaluation as a VisitorResult containing the value
79-
[[nodiscard]] VisitorResult visit(ScriptNode const& node) noexcept;
87+
[[nodiscard]] VisitorResultImpl visit(ScriptNode const& node) noexcept;
8088

8189
/// @brief Visit an assign node and evaluate its value.
8290
/// @param node the assign node to visit
8391
/// @return the result of the evaluation as a VisitorResult containing the value
84-
[[nodiscard]] VisitorResult visit(AssignNode const& node) noexcept;
92+
[[nodiscard]] VisitorResultImpl visit(AssignNode const& node) noexcept;
8593

8694
private:
8795
std::reference_wrapper<Object const> m_variables;

0 commit comments

Comments
 (0)