3#include <Core/CoreDLL.h>
4#include <Foundation/Logging/Log.h>
5#include <Foundation/Strings/String.h>
7#ifdef BUILDSYSTEM_ENABLE_DUKTAPE_SUPPORT
10using duk_context = duk_hthread;
11using duk_c_function = int (*)(duk_context*);
13struct plDuktapeTypeMask
15 using StorageType = plUInt32;
20 Undefined = PL_BIT(1),
34 StorageType Undefined : 1;
37 StorageType Number : 1;
38 StorageType String : 1;
39 StorageType Object : 1;
43PL_DECLARE_FLAGS_OPERATORS(plDuktapeTypeMask);
45# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
47# define PL_DUK_VERIFY_STACK(duk, ExpectedStackChange) \
48 duk.EnableStackChangeVerification(); \
49 duk.VerifyExpectedStackChange(ExpectedStackChange, PL_SOURCE_FILE, PL_SOURCE_LINE, PL_SOURCE_FUNCTION);
51# define PL_DUK_RETURN_AND_VERIFY_STACK(duk, ReturnCode, ExpectedStackChange) \
53 auto ret = ReturnCode; \
54 PL_DUK_VERIFY_STACK(duk, ExpectedStackChange); \
58# define PL_DUK_RETURN_VOID_AND_VERIFY_STACK(duk, ExpectedStackChange) \
59 PL_DUK_VERIFY_STACK(duk, ExpectedStackChange); \
65# define PL_DUK_VERIFY_STACK(duk, ExpectedStackChange)
67# define PL_DUK_RETURN_AND_VERIFY_STACK(duk, ReturnCode, ExpectedStackChange) return ReturnCode;
69# define PL_DUK_RETURN_VOID_AND_VERIFY_STACK(duk, ExpectedStackChange) return;
73class PL_CORE_DLL plDuktapeHelper
76 plDuktapeHelper(duk_context* pContext);
77 plDuktapeHelper(
const plDuktapeHelper& rhs);
79 void operator=(
const plDuktapeHelper& rhs);
85 PL_ALWAYS_INLINE duk_context* GetContext()
const {
return m_pContext; }
88 PL_ALWAYS_INLINE
operator duk_context*()
const {
return m_pContext; }
90# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
91 void VerifyExpectedStackChange(plInt32 iExpectedStackChange,
const char* szFile, plUInt32 uiLine,
const char* szFunction)
const;
100 void LogStackTrace(plInt32 iErrorObjIdx);
107 void PopStack(plUInt32 n = 1);
109 void PushGlobalObject();
111 void PushGlobalStash();
113 plResult PushLocalObject(
const char* szName, plInt32 iParentObjectIndex = -1);
119 bool HasProperty(
const char* szPropertyName, plInt32 iParentObjectIndex = -1)
const;
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;
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;
133 void SetCustomProperty(
const char* szPropertyName, plInt32 iParentObjectIndex = -1)
const;
140 void StorePointerInStash(
const char* szKey,
void* pPointer);
141 void* RetrievePointerFromStash(
const char* szKey)
const;
143 void StoreStringInStash(
const char* szKey,
const char* value);
144 const char* RetrieveStringFromStash(
const char* szKey,
const char* szFallback =
nullptr)
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;
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);
168 void RegisterObjectFunction(
169 const char* szFunctionName, duk_c_function function, plUInt8 uiNumArguments, plInt32 iParentObjectIndex = -1, plInt16 iMagicValue = 0);
171 plResult PrepareGlobalFunctionCall(
const char* szFunctionName);
172 plResult PrepareObjectFunctionCall(
const char* szFunctionName, plInt32 iParentObjectIndex = -1);
175 plResult PrepareMethodCall(
const char* szMethodName, plInt32 iParentObjectIndex = -1);
183 void PushInt(plInt32 iParam);
184 void PushUInt(plUInt32 uiParam);
185 void PushBool(
bool bParam);
186 void PushNumber(
double fParam);
189 void PushUndefined();
190 void PushCustom(plUInt32 uiNum = 1);
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;
203 plResult ExecuteString(
const char* szString,
const char* szDebugName =
"eval");
207 plResult ExecuteFile(
const char* szFile);
212# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
213 void EnableStackChangeVerification()
const;
218 duk_context* m_pContext =
nullptr;
219 plInt32 m_iPushedValues = 0;
221# if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
222 plInt32 m_iStackTopAtStart = -1000;
223 mutable bool m_bVerifyStackChange =
false;
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