OpenClonk
C4AulExec Class Reference

#include <C4AulExec.h>

Public Member Functions

 C4AulExec ()
 
C4Value Exec (C4AulScriptFunc *pSFunc, C4PropList *p, C4Value pPars[], bool fPassErrors)
 
C4Value DirectExec (C4PropList *p, const char *szScript, const char *szContext, bool fPassErrors=false, C4AulScriptContext *context=nullptr, bool parse_function=false)
 
void StartTrace ()
 
void StartDirectExec ()
 
void StopDirectExec ()
 
int GetContextDepth () const
 
C4AulScriptContextGetContext (int iLevel)
 
void LogCallStack ()
 
void ClearPointers (C4Object *)
 

Static Public Member Functions

static C4StringFnTranslate (C4PropList *_this, C4String *text)
 
static bool FnLogCallStack (C4PropList *_this)
 

Friends

class C4AulProfiler
 

Detailed Description

Definition at line 54 of file C4AulExec.h.

Constructor & Destructor Documentation

◆ C4AulExec()

C4AulExec::C4AulExec ( )
inline

Definition at line 58 of file C4AulExec.h.

References MAX_CONTEXT_STACK, and MAX_VALUE_STACK.

59  : pCurCtx(Contexts - 1), pCurVal(Values - 1)
60  { }

Member Function Documentation

◆ ClearPointers()

void C4AulExec::ClearPointers ( C4Object obj)

Definition at line 136 of file C4AulExec.cpp.

References C4Value::_getArray(), C4Value::_getInt(), C4Value::_getPropList(), C4Value::_getStr(), AB_ARRAY_SLICE, AB_ARRAY_SLICE_SET, AB_ARRAYA, AB_ARRAYA_SET, AB_BitAnd, AB_BitNot, AB_BitOr, AB_BitXOr, AB_BOOL, AB_CALL, AB_CALLFS, AB_CARRAY, AB_CFUNCTION, AB_COND, AB_CONDN, AB_CPROPLIST, AB_DEBUG, AB_Dec, AB_Div, AB_DUP, AB_DUP_CONTEXT, AB_EOFN, AB_Equal, AB_ERR, AB_FOREACH_NEXT, AB_FUNC, AB_GLOBALN, AB_GLOBALN_SET, AB_GreaterThan, AB_GreaterThanEqual, AB_Inc, AB_INT, AB_JUMP, AB_JUMPAND, AB_JUMPNNIL, AB_JUMPOR, AB_LeftShift, AB_LessThan, AB_LessThanEqual, AB_LOCALN, AB_LOCALN_SET, AB_Mod, AB_Mul, AB_Neg, AB_NEW_ARRAY, AB_NEW_PROPLIST, AB_NIL, AB_Not, AB_NotEqual, AB_PAR, AB_POP_TO, AB_Pow, AB_PROP, AB_PROP_SET, AB_RETURN, AB_RightShift, AB_STACK, AB_STACK_SET, AB_STRING, AB_Sub, AB_Sum, AB_THIS, AddDbgRec(), StdStrBuf::Append(), StdStrBuf::AppendChar(), StdStrBuf::AppendChars(), StdStrBuf::AppendFormat(), C4V_Data::Array, C4AulBCC::bccType, C4AUL_MAX_Par, C4AulExecError::C4AulExecError(), C4V_Array, C4V_Int, C4V_Nil, C4V_PropList, C4V_String, C4VNull, C4ScriptGuiWindowActionID::Call, C4Value::CheckConversion(), C4AulFunc::CheckParTypes(), Config, C4AulScriptContext::CPos, C4ConfigGeneral::DebugRec, DEBUGREC_SCRIPT, C4AulFunc::Exec(), Exec(), FormatString(), C4AulScriptContext::Func, C4Config::General, C4AulScriptFunc::GetCode(), GetContext(), GetContextDepth(), C4String::GetData(), C4Value::GetData(), StdStrBuf::getData(), C4Value::GetDataString(), C4AulDebug::GetDebugger(), C4AulScriptEngine::GetErrorHandler(), C4PropList::GetFunc(), C4ValueArray::GetItem(), C4ValueMapData::GetItem(), StdStrBuf::getLength(), C4AulFunc::GetName(), C4PropList::GetObject(), C4AulFunc::GetParCount(), C4AulScriptFunc::GetParCount(), C4ValueArray::GetSize(), C4ValueArray::GetSlice(), C4Value::getStr(), C4Value::GetType(), C4Value::GetTypeName(), C4AulScriptEngine::GlobalNamed, C4ValueArray::IsFrozen(), C4PropList::IsFrozen(), C4Value::IsIdenticalTo(), LogF(), C4PropList::New(), C4PropListNumbered::Number, C4AulScriptContext::Obj, C4AulErrorHandler::OnError(), C4AulBCC::Par, C4AulScriptContext::Pars, Pow(), RCT_AulFunc, C4AulScriptContext::Return, s, ScriptEngine, C4Value::Set(), C4Value::Set0(), C4Value::SetArray(), C4Value::SetBool(), C4Value::SetInt(), C4PropList::SetPropertyByS(), C4ValueArray::SetSlice(), C4AulFunc::SFunc(), C4PropList::Status, C4AulScriptContext::tTime, and C4AulError::what().

Referenced by C4Game::ClearPointers().

137 {
138  for (C4AulScriptContext *pCtx = pCurCtx; pCtx >= Contexts; pCtx--)
139  {
140  if (pCtx->Obj == obj)
141  pCtx->Obj = nullptr;
142  }
143 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DirectExec()

C4Value C4AulExec::DirectExec ( C4PropList p,
const char *  szScript,
const char *  szContext,
bool  fPassErrors = false,
C4AulScriptContext context = nullptr,
bool  parse_function = false 
)

Definition at line 1013 of file C4AulExec.cpp.

References AddDbgRec(), C4VNull, C4AulScriptEngine::Child0, Config, C4ConfigGeneral::DebugRec, DEBUGREC_SCRIPT, C4PropList::EnumerateOwnFuncs(), GameScript, C4Config::General, C4PropList::GetDef(), C4AulScriptEngine::GetErrorHandler(), C4PropList::GetFunc(), C4AulScriptEngine::GetPropList(), C4GameScriptHost::GetPropList(), C4PropList::GetPropListNumbered(), C4PropList::IsStatic(), C4ScriptHost::Next, C4PropListNumbered::Number, C4AulErrorHandler::OnError(), C4AulParSet::Par, C4AulScriptFunc::ParseDirectExecFunc(), C4AulScriptFunc::ParseDirectExecStatement(), RCT_DirectExec, ScriptEngine, C4AulFunc::SFunc(), C4AulScriptFunc::tProfileTime, and C4AulError::what().

Referenced by C4Control::CompileFunc(), C4ObjectMenu::MenuCommand(), C4Object::MenuCommand(), C4EditCursor::PerformDuplication(), C4PropertyPath::ResolveRoot(), and C4PropertyPath::ResolveValue().

1014 {
1016  {
1017  AddDbgRec(RCT_DirectExec, szScript, strlen(szScript)+1);
1018  int32_t iObjNumber = p && p->GetPropListNumbered() ? p->GetPropListNumbered()->Number : -1;
1019  AddDbgRec(RCT_DirectExec, &iObjNumber, sizeof(int32_t));
1020  }
1021  // profiler
1022  StartDirectExec();
1024  if (p && p->IsStatic())
1025  script = p->IsStatic();
1026  else if (p && p->GetDef())
1027  script = p->GetDef();
1028  // Add a new function
1029  auto pFunc = std::make_unique<C4AulScriptFunc>(script, nullptr, nullptr, szScript);
1030  // Parse function
1031  try
1032  {
1033  if (parse_function)
1034  {
1035  // Expect a full function (e.g. "func foo() { return bar(); }")
1036  pFunc->ParseDirectExecFunc(&::ScriptEngine, context);
1037  }
1038  else
1039  {
1040  // Expect a single statement (e.g. "bar()")
1041  pFunc->ParseDirectExecStatement(&::ScriptEngine, context);
1042  }
1043  C4AulParSet Pars;
1044  C4Value vRetVal(Exec(pFunc.get(), p, Pars.Par, fPassErrors));
1045  // profiler
1046  StopDirectExec();
1047  return vRetVal;
1048  }
1049  catch (C4AulError &ex)
1050  {
1051  if(fPassErrors)
1052  throw;
1054  LogCallStack();
1055  StopDirectExec();
1056  return C4VNull;
1057  }
1058 }
C4Config Config
Definition: C4Config.cpp:833
C4Value Exec(C4AulScriptFunc *pSFunc, C4PropList *p, C4Value pPars[], bool fPassErrors)
C4GameScriptHost GameScript
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
constexpr bool DEBUGREC_SCRIPT
Definition: C4Include.h:30
C4ConfigGeneral General
Definition: C4Config.h:251
void StopDirectExec()
Definition: C4AulExec.h:85
void LogCallStack()
Definition: C4AulExec.cpp:94
virtual class C4PropListStatic * IsStatic()
Definition: C4PropList.h:85
C4Value Par[C4AUL_MAX_Par]
Definition: C4AulFunc.h:30
C4AulErrorHandler * GetErrorHandler() const
Definition: C4Aul.h:173
C4PropListStatic * GetPropList() override
const char * what() const noexcept override
Definition: C4Aul.cpp:59
void AddDbgRec(C4RecordChunkType eType, const void *pData, int iSize)
Definition: C4Record.cpp:32
virtual void OnError(const char *msg)=0
const C4Value C4VNull
Definition: C4Value.cpp:30
virtual C4Def const * GetDef() const
Definition: C4PropList.cpp:685
int32_t DebugRec
Definition: C4Config.h:60
virtual C4PropListNumbered * GetPropListNumbered()
Definition: C4PropList.cpp:703
void StartDirectExec()
Definition: C4AulExec.h:84
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Exec()

C4Value C4AulExec::Exec ( C4AulScriptFunc pSFunc,
C4PropList p,
C4Value  pPars[],
bool  fPassErrors 
)

Referenced by ClearPointers(), and C4AulScriptFunc::Exec().

Here is the caller graph for this function:

◆ FnLogCallStack()

bool C4AulExec::FnLogCallStack ( C4PropList _this)
static

Definition at line 130 of file C4AulExec.cpp.

References LogCallStack().

Referenced by InitCoreFunctionMap().

131 {
133  return true;
134 }
void LogCallStack()
Definition: C4AulExec.cpp:94
C4AulExec AulExec
Definition: C4AulExec.cpp:29
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FnTranslate()

C4String * C4AulExec::FnTranslate ( C4PropList _this,
C4String text 
)
static

Definition at line 100 of file C4AulExec.cpp.

References DebugLogF(), C4AulScriptContext::Func, C4String::GetCStr(), C4String::GetData(), C4PropList::GetDef(), StdStrBuf::isNull(), LogCallStack(), C4AulScriptFunc::pOrgScript, ReturnIfTranslationAvailable, C4Def::Script, and C4ScriptGuiWindowPropertyName::text.

Referenced by InitCoreFunctionMap().

101 {
102 #define ReturnIfTranslationAvailable(script, key) do \
103 { \
104  const auto &s = script; \
105  const auto &k = key; \
106  if (s) \
107  { \
108  try \
109  { \
110  return ::Strings.RegString(s->Translate(k).c_str()); \
111  } \
112  catch (C4LangStringTable::NoSuchTranslation &) {} \
113  } \
114 } while(0)
115 
116  if (!text || text->GetData().isNull()) return nullptr;
117  // Find correct script: translations of the context if possible, containing script as fallback
118  if (_this && _this->GetDef())
119  ReturnIfTranslationAvailable(&(_this->GetDef()->Script), text->GetCStr());
121 
122  // No translation available, log
123  DebugLogF(R"(WARNING: Translate: no translation for string "%s")", text->GetCStr());
124  // Trace
126  return text;
127 #undef ReturnIfTranslationAvailable
128 }
StdStrBuf GetData() const
Definition: C4StringTable.h:50
void LogCallStack()
Definition: C4AulExec.cpp:94
C4ScriptHost * pOrgScript
C4AulExec AulExec
Definition: C4AulExec.cpp:29
C4AulScriptFunc * Func
Definition: C4AulExec.h:46
const char * GetCStr() const
Definition: C4StringTable.h:49
bool isNull() const
Definition: StdBuf.h:441
#define ReturnIfTranslationAvailable(script, key)
virtual C4Def const * GetDef() const
Definition: C4PropList.cpp:685
C4DefScriptHost Script
Definition: C4Def.h:181
bool DebugLogF(const char *strMessage ...)
Definition: C4Log.cpp:288
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetContext()

C4AulScriptContext* C4AulExec::GetContext ( int  iLevel)
inline

Definition at line 88 of file C4AulExec.h.

References ALWAYS_INLINE, AulExec, b, C4V_Array, C4V_Int, C4V_PropList, C4V_String, C4ScriptGuiWindowActionID::Call, C4Value::CheckConversion(), C4Value::CheckParConversion(), FormatString(), C4AulScriptContext::Func, GetC4VName(), C4AulScriptFunc::GetParCount(), C4Value::GetTypeName(), iCnt, C4ValueMapNames::iSize, MAX_VALUE_STACK, C4AulScriptContext::Pars, C4ScriptGuiWindowPropertyName::text, and C4AulScriptFunc::VarNamed.

Referenced by ClearPointers(), C4Control::CompileFunc(), and C4AulDebug::OnLog().

88 { return iLevel >= 0 && iLevel < GetContextDepth() ? Contexts + iLevel : nullptr; }
int GetContextDepth() const
Definition: C4AulExec.h:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetContextDepth()

int C4AulExec::GetContextDepth ( ) const
inline

Definition at line 87 of file C4AulExec.h.

Referenced by ClearPointers(), C4Control::CompileFunc(), and C4AulDebug::OnLog().

87 { return pCurCtx - Contexts + 1; }
Here is the caller graph for this function:

◆ LogCallStack()

void C4AulExec::LogCallStack ( )

Definition at line 94 of file C4AulExec.cpp.

Referenced by FnLogCallStack(), FnTranslate(), and C4Value::GetTypeEx().

95 {
96  for (C4AulScriptContext *pCtx = pCurCtx; pCtx >= Contexts; pCtx--)
97  pCtx->dump(StdStrBuf(" by: "));
98 }
Here is the caller graph for this function:

◆ StartDirectExec()

void C4AulExec::StartDirectExec ( )
inline

Definition at line 84 of file C4AulExec.h.

References C4TimeMilliseconds::Now().

84 { if (fProfiling) tDirectExecStart = C4TimeMilliseconds::Now(); }
static C4TimeMilliseconds Now()
Here is the call graph for this function:

◆ StartTrace()

void C4AulExec::StartTrace ( )

Definition at line 905 of file C4AulExec.cpp.

References StdStrBuf::AppendChars(), C4AulExecError::C4AulExecError(), MAX_CONTEXT_STACK, and C4TimeMilliseconds::Now().

906 {
907  if (iTraceStart < 0)
908  iTraceStart = ContextStackSize();
909 }
Here is the call graph for this function:

◆ StopDirectExec()

void C4AulExec::StopDirectExec ( )
inline

Definition at line 85 of file C4AulExec.h.

References C4TimeMilliseconds::Now().

85 { if (fProfiling) tDirectExecTotal += C4TimeMilliseconds::Now() - tDirectExecStart; }
static C4TimeMilliseconds Now()
Here is the call graph for this function:

Friends And Related Function Documentation

◆ C4AulProfiler

friend class C4AulProfiler
friend

Definition at line 78 of file C4AulExec.h.


The documentation for this class was generated from the following files: