2022-02-01 03:40:40 +08:00
|
|
|
// Copyright 2022, Roman Gershman. All rights reserved.
|
|
|
|
// See LICENSE for licensing terms.
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-03 06:31:26 +08:00
|
|
|
#include <absl/types/span.h>
|
|
|
|
|
|
|
|
#include <functional>
|
2022-02-01 03:40:40 +08:00
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
typedef struct lua_State lua_State;
|
|
|
|
|
|
|
|
namespace dfly {
|
|
|
|
|
2022-02-01 19:15:19 +08:00
|
|
|
class ObjectExplorer {
|
2022-02-03 06:31:26 +08:00
|
|
|
public:
|
|
|
|
virtual ~ObjectExplorer() {
|
|
|
|
}
|
2022-02-01 19:15:19 +08:00
|
|
|
|
|
|
|
virtual void OnBool(bool b) = 0;
|
|
|
|
virtual void OnString(std::string_view str) = 0;
|
|
|
|
virtual void OnDouble(double d) = 0;
|
|
|
|
virtual void OnInt(int64_t val) = 0;
|
|
|
|
virtual void OnArrayStart(unsigned len) = 0;
|
|
|
|
virtual void OnArrayEnd() = 0;
|
|
|
|
virtual void OnNil() = 0;
|
|
|
|
virtual void OnStatus(std::string_view str) = 0;
|
|
|
|
virtual void OnError(std::string_view str) = 0;
|
|
|
|
};
|
|
|
|
|
2022-02-01 03:40:40 +08:00
|
|
|
class Interpreter {
|
|
|
|
public:
|
2022-02-04 15:38:50 +08:00
|
|
|
using MutableSlice = absl::Span<char>;
|
|
|
|
using MutSliceSpan = absl::Span<MutableSlice>;
|
|
|
|
using RedisFunc = std::function<void(MutSliceSpan, ObjectExplorer*)>;
|
|
|
|
|
2022-02-01 03:40:40 +08:00
|
|
|
Interpreter();
|
|
|
|
~Interpreter();
|
|
|
|
|
|
|
|
Interpreter(const Interpreter&) = delete;
|
|
|
|
void operator=(const Interpreter&) = delete;
|
|
|
|
|
|
|
|
// Note: We leak the state for now.
|
|
|
|
// Production code should not access this method.
|
|
|
|
lua_State* lua() {
|
|
|
|
return lua_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns false if an error happenned, sets error string into result.
|
|
|
|
// otherwise, returns true and sets result to function id.
|
|
|
|
bool AddFunction(std::string_view body, std::string* result);
|
|
|
|
|
|
|
|
// Runs already added function f_id returned by a successful call to AddFunction().
|
|
|
|
// Returns: true if the call succeeded, otherwise fills error and returns false.
|
|
|
|
bool RunFunction(const char* f_id, std::string* err);
|
|
|
|
|
2022-02-04 15:38:50 +08:00
|
|
|
void SetGlobalArray(const char* name, MutSliceSpan args);
|
|
|
|
|
2022-02-01 19:15:19 +08:00
|
|
|
bool Execute(std::string_view body, char f_id[43], std::string* err);
|
|
|
|
bool Serialize(ObjectExplorer* serializer, std::string* err);
|
|
|
|
|
2022-02-01 03:40:40 +08:00
|
|
|
// fp must point to buffer with at least 43 chars.
|
|
|
|
// fp[42] will be set to '\0'.
|
|
|
|
static void Fingerprint(std::string_view body, char* fp);
|
|
|
|
|
2022-02-04 15:38:50 +08:00
|
|
|
template <typename U> void SetRedisFunc(U&& u) {
|
2022-02-03 06:31:26 +08:00
|
|
|
redis_func_ = std::forward<U>(u);
|
|
|
|
}
|
|
|
|
|
2022-02-01 03:40:40 +08:00
|
|
|
private:
|
2022-02-01 19:15:19 +08:00
|
|
|
bool AddInternal(const char* f_id, std::string_view body, std::string* result);
|
|
|
|
|
2022-02-02 01:02:43 +08:00
|
|
|
int RedisGenericCommand(bool raise_error);
|
|
|
|
|
2022-02-03 06:31:26 +08:00
|
|
|
static int RedisCallCommand(lua_State* lua);
|
|
|
|
static int RedisPCallCommand(lua_State* lua);
|
2022-02-02 01:02:43 +08:00
|
|
|
|
2022-02-01 03:40:40 +08:00
|
|
|
lua_State* lua_;
|
2022-02-02 01:02:43 +08:00
|
|
|
unsigned cmd_depth_ = 0;
|
2022-02-03 06:31:26 +08:00
|
|
|
RedisFunc redis_func_;
|
2022-02-01 03:40:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dfly
|