blob: 4a925ec2239296332305f0b6b107c175383f2d0b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#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);
bool CheckEmptyStack();
struct TExpression {
TString Name;
};
TExpression Compile(TStringBuf expression);
TExpression CompileFunction(TStringBuf expression);
TExpression CompileRaw(TStringBuf body, const TString& name);
TString DumpStack();
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_;
};
|