[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Retrieve name of called function ?
- From: Markus Ewald <Markus_Ewald@...>
- Date: Sun, 10 Mar 2002 15:21:36 +0100
Hello!
Warning, longer text approaching :)
To better understand my problem, here's a little preface about my
scripting design.
My application supports multiple scripting languages through a simple
abstraction layer. This layer manages C++ method publishing to scripts
by assigning each method a unique MethodID which is then called through
sort of an "Invoke" function in a base class all scripting-enabled
classes have to implement:
class CPublishedClass :
public CScript::CCallable {
void Foo(int bar) {} // A method which is callable by the script
//
// CScript::CCallable implementation
//
OnScriptRegister(CScript *pScript) {
pScript->BeginClass("CPublishedClass", this);
m_midFoo = pScript->RegisterMethod("Foo");
pScript->EndClass();
}
OnScriptInvoke(CScript *pScript, MethodID mid) {
switch(mid) {
case m_midFoo: {
Foo(pScript->PopInt());
break;
}
}
}
};
As you can see, the CScript (from which CLuaScript, CTCLScript or
anything could be derived) records the class in BeginClass() and assigns
each method that gets added to it a unique MethodID. When the script
calls a method, the script has to look up the class pointer internally
and then invoke the method through the class' CCallable::Invoke() method.
The CLuaScript then works like this (sorry, this still only exists in my
head for now):
static int ScriptDispatcher(void) {
// Get global value for this-pointer of CLuaScript instance
int mid = pLuaScript->FindMethodID(/* >>> MY NAME <<< */);
pLuaScript->GetClassFromMethodID(mid)->Invoke(pLuaScript, mid);
}
class CLuaScript :
public CScript {
public:
CLuaScript(void) {
m_pLuaState = lua_Open(0);
// Set a global value for the script to
// retrieve the this pointer out of the lua_State
}
virtual ~CLuaScript(void) {
}
void RegisterMethod(const std::string &sName) {
lua_register(sName, ScriptDispatcher);
}
private:
lua_State *m_pState;
}
Ok, the big >>> MY NAME <<< above is the problem:
As you see each function registered to lua is associated with one and
the same C function which will then dispatch the method calls to the
registered classes. I don't know how I should instantiate a proxy
function for each script function I add, so the problem is that the
static dispatcher function doesn't know as what it was just called, was
it Foo() or maybe Moo() ?
Is the function name from the call still somewhere in the lua stack or
can I somehow else retrieve the function name under which it was called ?
Or do you have a better design for my scripting layer even ;) ?
Thanks,
-Markus-
----> Short version <----
int Foo(void) {
printf("Am I Foo() or Moo( ?)");
}
void Register(void) {
lua_Register(LuaState, Foo, "Foo");
lua_Register(LuaState, Foo, "Moo");
}