1
0
Fork 0

DBG: ExpressionFunctions class to handle function calls in ExpressionParser

This commit is contained in:
mrexodia 2016-07-07 09:02:07 +02:00
parent f49e5ad7f1
commit 5358596997
No known key found for this signature in database
GPG Key ID: D72F9A4FAA0073B4
5 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,55 @@
#include "expressionfunctions.h"
#include "threading.h"
std::unordered_map<String, ExpressionFunctions::Function> ExpressionFunctions::mFunctions;
void ExpressionFunctions::Init()
{
//TODO: register some functions
}
bool ExpressionFunctions::Register(const String & name, int argc, CBEXPRESSIONFUNCTION cbFunction)
{
EXCLUSIVE_ACQUIRE(LockExpressionFunctions);
if(mFunctions.count(name))
return false;
Function f;
f.name = name;
f.argc = argc;
f.cbFunction = cbFunction;
mFunctions[name] = f;
return true;
}
bool ExpressionFunctions::Unregister(const String & name)
{
EXCLUSIVE_ACQUIRE(LockExpressionFunctions);
auto found = mFunctions.find(name);
if(found == mFunctions.end())
return false;
mFunctions.erase(found);
return true;
}
bool ExpressionFunctions::Call(const String & name, const std::vector<duint> & argv, duint & result)
{
SHARED_ACQUIRE(LockExpressionFunctions);
auto found = mFunctions.find(name);
if(found == mFunctions.end())
return false;
const auto & f = found->second;
if(f.argc != int(argv.size()))
return false;
result = f.cbFunction(f.argc, argv.data());
return true;
}
bool ExpressionFunctions::GetArgc(const String & name, int & argc)
{
SHARED_ACQUIRE(LockExpressionFunctions);
auto found = mFunctions.find(name);
if(found == mFunctions.end())
return false;
argc = found->second.argc;
return true;
}

View File

@ -0,0 +1,25 @@
#pragma once
#include "_global.h"
class ExpressionFunctions
{
public:
typedef duint(*CBEXPRESSIONFUNCTION)(int argc, const duint* argv);
static void Init();
static bool Register(const String & name, int argc, CBEXPRESSIONFUNCTION cbFunction);
static bool Unregister(const String & name);
static bool Call(const String & name, const std::vector<duint> & argv, duint & result);
static bool GetArgc(const String & name, int & argc);
private:
struct Function
{
String name;
int argc;
CBEXPRESSIONFUNCTION cbFunction;
};
static std::unordered_map<String, Function> mFunctions;
};

View File

@ -25,6 +25,7 @@
#include "datainst_helper.h"
#include "error.h"
#include "exception.h"
#include "expressionfunctions.h"
static MESSAGE_STACK* gMsgStack = 0;
static HANDLE hCommandLoopThread = 0;
@ -509,6 +510,8 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
dputs("Registering debugger commands...");
registercommands();
dputs("Registering GUI command handler...");
ExpressionFunctions::Init();
dputs("Registering expression functions...");
SCRIPTTYPEINFO info;
strcpy_s(info.name, "Default");
info.id = 0;

View File

@ -43,6 +43,7 @@
<ClCompile Include="encodemap.cpp" />
<ClCompile Include="disasm_fast.cpp" />
<ClCompile Include="disasm_helper.cpp" />
<ClCompile Include="expressionfunctions.cpp" />
<ClCompile Include="handles.cpp" />
<ClCompile Include="error.cpp" />
<ClCompile Include="exception.cpp" />
@ -133,6 +134,7 @@
<ClInclude Include="disasm_fast.h" />
<ClInclude Include="disasm_helper.h" />
<ClInclude Include="dynamicmem.h" />
<ClInclude Include="expressionfunctions.h" />
<ClInclude Include="handles.h" />
<ClInclude Include="error.h" />
<ClInclude Include="exception.h" />

View File

@ -335,6 +335,9 @@
<ClCompile Include="datainst_helper.cpp">
<Filter>Source Files\Utilities</Filter>
</ClCompile>
<ClCompile Include="expressionfunctions.cpp">
<Filter>Source Files\Core</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="x64_dbg.h">
@ -760,5 +763,8 @@
<ClInclude Include="taskthread.h">
<Filter>Header Files\Utilities</Filter>
</ClInclude>
<ClInclude Include="expressionfunctions.h">
<Filter>Header Files\Core</Filter>
</ClInclude>
</ItemGroup>
</Project>