Allow assigned top-level functions to work as callbacks.

Store a JsFunction name in its embedded ExpFunction so they are always in sync.


git-svn-id: http://voip.null.ro/svn/yate@6102 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2016-03-08 12:23:11 +00:00
parent 7982f5b494
commit 0091b832c3
3 changed files with 14 additions and 16 deletions

View File

@ -2910,6 +2910,11 @@ ScriptRun::Status JsRunner::call(const String& name, ObjList& args,
return Invalid;
}
JsFunction* func = c->getGlobalFunction(name);
if (!func) {
JsContext* ctx = YOBJECT(JsContext,context());
if (ctx)
func = YOBJECT(JsFunction,ctx->getField(stack(),name,this));
}
if (!func) {
TelEngine::destruct(thisObj);
TelEngine::destruct(scopeObj);
@ -3063,9 +3068,7 @@ void JsRunner::traceCall(const ExpOperation& oper, const JsFunction& func)
return;
}
const String* name = &func.firstName();
if (TelEngine::null(name))
name = &oper.name();
const String& name = func.getFunc()->name();
JsFuncStats* fs = 0;
if (m_stats) {
m_stats->lock();
@ -3076,7 +3079,7 @@ void JsRunner::traceCall(const ExpOperation& oper, const JsFunction& func)
if (m_callInfo)
m_callInfo->traceLine(m_lastLine,diff);
}
fs = m_stats->getFuncStats(*name,o->lineNumber());
fs = m_stats->getFuncStats(name,o->lineNumber());
m_stats->unlock();
}
#ifdef STATS_TRACE
@ -3084,9 +3087,9 @@ void JsRunner::traceCall(const ExpOperation& oper, const JsFunction& func)
static_cast<const JsCode*>(code())->formatLineNo(caller,m_lastLine);
static_cast<const JsCode*>(code())->formatLineNo(called,o->lineNumber());
Debug(STATS_TRACE,DebugCall,"Call %s %s -> %s, instr=%u",
name->c_str(),caller.c_str(),called.c_str(),m_instr);
name.c_str(),caller.c_str(),called.c_str(),m_instr);
#endif
m_traceStack.insert(m_callInfo = new JsCallInfo(fs,*name,m_lastLine,o->lineNumber(),m_instr,m_totalTime));
m_traceStack.insert(m_callInfo = new JsCallInfo(fs,name,m_lastLine,o->lineNumber(),m_instr,m_totalTime));
}
void JsRunner::traceReturn()
@ -3361,7 +3364,7 @@ JsFunction::JsFunction(Mutex* mtx)
JsFunction::JsFunction(Mutex* mtx, const char* name, ObjList* args, long int lbl, ScriptCode* code)
: JsObject(mtx,String("[function ") + name + "()]",false),
m_label(lbl), m_code(code), m_func(name), m_name(name)
m_label(lbl), m_code(code), m_func(name)
{
init();
if (args) {

View File

@ -453,6 +453,9 @@ int JsObject::extractArgs(JsObject* obj, ObjList& stack, const ExpOperation& ope
return 0;
for (int i = (int)oper.number(); i; i--) {
ExpOperation* op = obj->popValue(stack,context);
JsFunction* jsf = YOBJECT(JsFunction,op);
if (jsf)
jsf->firstName(op->name());
arguments.insert(op);
}
return (int)oper.number();

View File

@ -2204,19 +2204,12 @@ public:
inline const ExpFunction* getFunc() const
{ return &m_func; }
/**
* Retrieve the first name assigned to this function
* @return The name of the property towhich this function was first assigned
*/
inline const String& firstName() const
{ return m_name; }
/**
* Set the name of this function if still empty
* @param name Name to set as first assigned name
*/
inline void firstName(const char* name)
{ if (m_name.null()) m_name = name; }
{ if (m_func.name().null()) const_cast<String&>(m_func.name()) = name; }
/**
* Retrieve the name of the N-th formal argument
@ -2257,7 +2250,6 @@ private:
long int m_label;
ScriptCode* m_code;
ExpFunction m_func;
String m_name;
};
/**