Plasma Engine  2.0
Loading...
Searching...
No Matches
DuktapeHelper.h
1#pragma once
2
3#include <Core/CoreDLL.h>
4#include <Foundation/Logging/Log.h>
5#include <Foundation/Strings/String.h>
6
7#ifdef BUILDSYSTEM_ENABLE_DUKTAPE_SUPPORT
8
9struct duk_hthread;
10using duk_context = duk_hthread;
11using duk_c_function = int (*)(duk_context*);
12
13struct plDuktapeTypeMask
14{
15 using StorageType = plUInt32;
16
17 enum Enum
18 {
19 None = PL_BIT(0),
20 Undefined = PL_BIT(1),
21 Null = PL_BIT(2),
22 Bool = PL_BIT(3),
23 Number = PL_BIT(4),
24 String = PL_BIT(5),
25 Object = PL_BIT(6),
26 Buffer = PL_BIT(7),
27 Pointer = PL_BIT(8)
28
29 };
30
31 struct Bits
32 {
33 StorageType None : 1;
34 StorageType Undefined : 1;
35 StorageType Null : 1;
36 StorageType Bool : 1;
37 StorageType Number : 1;
38 StorageType String : 1;
39 StorageType Object : 1;
40 };
41};
42
43PL_DECLARE_FLAGS_OPERATORS(plDuktapeTypeMask);
44
45# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
46
47# define PL_DUK_VERIFY_STACK(duk, ExpectedStackChange) \
48 duk.EnableStackChangeVerification(); \
49 duk.VerifyExpectedStackChange(ExpectedStackChange, PL_SOURCE_FILE, PL_SOURCE_LINE, PL_SOURCE_FUNCTION);
50
51# define PL_DUK_RETURN_AND_VERIFY_STACK(duk, ReturnCode, ExpectedStackChange) \
52 { \
53 auto ret = ReturnCode; \
54 PL_DUK_VERIFY_STACK(duk, ExpectedStackChange); \
55 return ret; \
56 }
57
58# define PL_DUK_RETURN_VOID_AND_VERIFY_STACK(duk, ExpectedStackChange) \
59 PL_DUK_VERIFY_STACK(duk, ExpectedStackChange); \
60 return;
61
62
63# else
64
65# define PL_DUK_VERIFY_STACK(duk, ExpectedStackChange)
66
67# define PL_DUK_RETURN_AND_VERIFY_STACK(duk, ReturnCode, ExpectedStackChange) return ReturnCode;
68
69# define PL_DUK_RETURN_VOID_AND_VERIFY_STACK(duk, ExpectedStackChange) return;
70
71# endif
72
73class PL_CORE_DLL plDuktapeHelper
74{
75public:
76 plDuktapeHelper(duk_context* pContext);
77 plDuktapeHelper(const plDuktapeHelper& rhs);
78 ~plDuktapeHelper();
79 void operator=(const plDuktapeHelper& rhs);
80
83
85 PL_ALWAYS_INLINE duk_context* GetContext() const { return m_pContext; }
86
88 PL_ALWAYS_INLINE operator duk_context*() const { return m_pContext; }
89
90# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
91 void VerifyExpectedStackChange(plInt32 iExpectedStackChange, const char* szFile, plUInt32 uiLine, const char* szFunction) const;
92# endif
93
97
98 void Error(const plFormatString& text);
99
100 void LogStackTrace(plInt32 iErrorObjIdx);
101
102
106
107 void PopStack(plUInt32 n = 1);
108
109 void PushGlobalObject();
110
111 void PushGlobalStash();
112
113 plResult PushLocalObject(const char* szName, plInt32 iParentObjectIndex = -1);
114
118
119 bool HasProperty(const char* szPropertyName, plInt32 iParentObjectIndex = -1) const;
120
121 bool GetBoolProperty(const char* szPropertyName, bool bFallback, plInt32 iParentObjectIndex = -1) const;
122 plInt32 GetIntProperty(const char* szPropertyName, plInt32 iFallback, plInt32 iParentObjectIndex = -1) const;
123 plUInt32 GetUIntProperty(const char* szPropertyName, plUInt32 uiFallback, plInt32 iParentObjectIndex = -1) const;
124 float GetFloatProperty(const char* szPropertyName, float fFallback, plInt32 iParentObjectIndex = -1) const;
125 double GetNumberProperty(const char* szPropertyName, double fFallback, plInt32 iParentObjectIndex = -1) const;
126 const char* GetStringProperty(const char* szPropertyName, const char* szFallback, plInt32 iParentObjectIndex = -1) const;
127
128 void SetBoolProperty(const char* szPropertyName, bool value, plInt32 iParentObjectIndex = -1) const;
129 void SetNumberProperty(const char* szPropertyName, double value, plInt32 iParentObjectIndex = -1) const;
130 void SetStringProperty(const char* szPropertyName, const char* value, plInt32 iParentObjectIndex = -1) const;
131
133 void SetCustomProperty(const char* szPropertyName, plInt32 iParentObjectIndex = -1) const;
134
135
139
140 void StorePointerInStash(const char* szKey, void* pPointer);
141 void* RetrievePointerFromStash(const char* szKey) const;
142
143 void StoreStringInStash(const char* szKey, const char* value);
144 const char* RetrieveStringFromStash(const char* szKey, const char* szFallback = nullptr) const;
145
149
150 bool IsOfType(plBitflags<plDuktapeTypeMask> mask, plInt32 iStackElement = -1) const;
151 bool IsBool(plInt32 iStackElement = -1) const;
152 bool IsNumber(plInt32 iStackElement = -1) const;
153 bool IsString(plInt32 iStackElement = -1) const;
154 bool IsNull(plInt32 iStackElement = -1) const;
155 bool IsUndefined(plInt32 iStackElement = -1) const;
156 bool IsObject(plInt32 iStackElement = -1) const;
157 bool IsBuffer(plInt32 iStackElement = -1) const;
158 bool IsPointer(plInt32 iStackElement = -1) const;
159 bool IsNullOrUndefined(plInt32 iStackElement = -1) const;
160
164
165 void RegisterGlobalFunction(const char* szFunctionName, duk_c_function function, plUInt8 uiNumArguments, plInt16 iMagicValue = 0);
166 void RegisterGlobalFunctionWithVarArgs(const char* szFunctionName, duk_c_function function, plInt16 iMagicValue = 0);
167
168 void RegisterObjectFunction(
169 const char* szFunctionName, duk_c_function function, plUInt8 uiNumArguments, plInt32 iParentObjectIndex = -1, plInt16 iMagicValue = 0);
170
171 plResult PrepareGlobalFunctionCall(const char* szFunctionName);
172 plResult PrepareObjectFunctionCall(const char* szFunctionName, plInt32 iParentObjectIndex = -1);
173 plResult CallPreparedFunction();
174
175 plResult PrepareMethodCall(const char* szMethodName, plInt32 iParentObjectIndex = -1);
176 plResult CallPreparedMethod();
177
178
182
183 void PushInt(plInt32 iParam);
184 void PushUInt(plUInt32 uiParam);
185 void PushBool(bool bParam);
186 void PushNumber(double fParam);
187 void PushString(const plStringView& sParam);
188 void PushNull();
189 void PushUndefined();
190 void PushCustom(plUInt32 uiNum = 1);
191
192 bool GetBoolValue(plInt32 iStackElement, bool bFallback = false) const;
193 plInt32 GetIntValue(plInt32 iStackElement, plInt32 iFallback = 0) const;
194 plUInt32 GetUIntValue(plInt32 iStackElement, plUInt32 uiFallback = 0) const;
195 float GetFloatValue(plInt32 iStackElement, float fFallback = 0) const;
196 double GetNumberValue(plInt32 iStackElement, double fFallback = 0) const;
197 const char* GetStringValue(plInt32 iStackElement, const char* szFallback = "") const;
198
202
203 plResult ExecuteString(const char* szString, const char* szDebugName = "eval");
204
205 plResult ExecuteStream(plStreamReader& inout_stream, const char* szDebugName);
206
207 plResult ExecuteFile(const char* szFile);
208
210
211public:
212# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
213 void EnableStackChangeVerification() const;
214# endif
215
216
217protected:
218 duk_context* m_pContext = nullptr;
219 plInt32 m_iPushedValues = 0;
220
221# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
222 plInt32 m_iStackTopAtStart = -1000;
223 mutable bool m_bVerifyStackChange = false;
224
225# endif
226};
227
228#endif
Implements formating of strings with placeholders and formatting options.
Definition FormatString.h:59
Interface for binary in (read) streams.
Definition Stream.h:22
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54