MantisBase v0.4.9
Loading...
Searching...
No Matches
scripting_engine.h
Go to the documentation of this file.
1
5
6#ifndef MB_SCRIPTING_ENGINE_H
7#define MB_SCRIPTING_ENGINE_H
8
9#ifdef MB_SCRIPTING_ENABLED
10
11#include <dukglue/dukglue.h>
12#include <mutex>
13#include <string>
15
16struct duk_hthread;
17typedef struct duk_hthread duk_context;
18
19namespace mb {
20 class MantisBase;
21 class MbRequest;
22 class MbResponse;
23
30 class MANTISBASE_API MbScriptingEngine {
31 public:
32 explicit MbScriptingEngine(MantisBase &app);
33 ~MbScriptingEngine();
34
35 MbScriptingEngine(const MbScriptingEngine &) = delete;
36 MbScriptingEngine &operator=(const MbScriptingEngine &) = delete;
37
39 void init();
40
42 void loadStartScript();
43
45 void loadScript(const std::string &relativePath);
46
47 void fireOnServerStart();
48 void fireOnServerShutdown();
49 void fireOnRecordCreated(const std::string &entity, const std::string &recordId);
50 void fireOnRecordUpdated(const std::string &entity, const std::string &recordId);
51
52 [[nodiscard]] duk_context *ctx() const { return m_ctx; }
53 [[nodiscard]] bool isActive() const { return m_ctx != nullptr; }
54
56 [[nodiscard]] static MbScriptingEngine *active();
57
59 template<typename Fn>
60 auto withLock(Fn &&fn) -> decltype(fn()) {
61 std::lock_guard lock(m_mutex);
62 return fn();
63 }
64
65 bool pcallBool(const DukValue &fn, MbRequest &req, MbResponse &res);
66 void pcallVoid(const DukValue &fn, MbRequest &req, MbResponse &res);
67
68 private:
69 void registerBindings() const;
70 void evalFile(const std::string &filePath);
71 void evalString(const std::string &content, const std::string &label);
72 [[nodiscard]] bool isRuntimeDisabled() const;
73
74 MantisBase &m_app;
75 duk_context *m_ctx = nullptr;
76 mutable std::mutex m_mutex;
77 static MbScriptingEngine *s_active;
78 };
79}
80
81#endif // MB_SCRIPTING_ENABLED
82#endif // MB_SCRIPTING_ENGINE_H
MantisBase entry point.
Definition mantisbase.h:54
HTTP request wrapper bound to a MantisBase instance.
Definition http.h:66
HTTP response wrapper bound to a MantisBase instance.
Definition http.h:213
#define MANTISBASE_API
Definition export.h:31
Definition api_keys.h:35
Core type aliases, handler signatures, and the MbAppMixin DI mixin.