diff options
author | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
---|---|---|
committer | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
commit | 06e5c21a835c0e923506c4ff27929f34e00761c2 (patch) | |
tree | 75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/lua/eval.h | |
parent | 03f024c4412e3aa613bb543cf1660176320ba8f4 (diff) | |
download | ydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz |
fix ya.make
Diffstat (limited to 'library/cpp/lua/eval.h')
-rw-r--r-- | library/cpp/lua/eval.h | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/library/cpp/lua/eval.h b/library/cpp/lua/eval.h new file mode 100644 index 0000000000..77c59d7efd --- /dev/null +++ b/library/cpp/lua/eval.h @@ -0,0 +1,65 @@ +#pragma once + +#include "wrapper.h" + +#include <library/cpp/json/json_value.h> + +#include <util/system/mutex.h> + +class TLuaEval { +public: + TLuaEval(); + + template <class C> + inline TLuaEval& SetVars(const C& container) { + for (auto& [k, v] : container) { + SetVariable(k, v); + } + + return *this; + } + + inline TLuaEval& Parse(TStringBuf chunk) { + ParseChunk(chunk); + + return *this; + } + + void SetVariable(TZtStringBuf name, const NJson::TJsonValue& value); + template <typename T> + void SetUserdata(TZtStringBuf name, T&& userdata) { + LuaState_.push_userdata(std::forward<T>(userdata)); + LuaState_.set_global(name.c_str()); + } + TString EvalExpression(TStringBuf expression); + TString EvalRaw(TStringBuf code); + void ParseChunk(TStringBuf code); + TString Preprocess(TStringBuf line); + TString PreprocessOne(TStringBuf line); + + struct TExpression { + TString Name; + }; + + TExpression Compile(TStringBuf expression); + TExpression CompileFunction(TStringBuf expression); + TExpression CompileRaw(TStringBuf body, const TString& name); + TString EvalCompiled(const TExpression& compiled); + void EvalCompiledRaw(const TExpression& compiled); + bool EvalCompiledCondition(const TExpression& compiled); + template <typename TNumber> + TNumber EvalCompiledNumeric(const TExpression& compiled) { + TGuard<TMutex> guard(LuaMutex_); + RunExpressionLocked(guard, compiled); + return LuaState_.pop_number<TNumber>(); + } + +private: + TString GenerateName(); + TString Evaluate(const TString& name, const TString& body); + void RunExpressionLocked(const TGuard<TMutex>& lock, const TExpression& compiled); + + TLuaStateHolder LuaState_; + ui64 FunctionNameCounter_; + TMutex LuaMutex_; +}; |