Clone a JsFunction when first assigned into another variable.

git-svn-id: http://yate.null.ro/svn/yate/trunk@6477 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
oana 2021-04-12 08:55:18 +00:00
parent 871751ecfa
commit fdff04abd5
3 changed files with 32 additions and 2 deletions

View File

@ -3617,6 +3617,17 @@ JsObject* JsFunction::copy(ScriptMutex* mtx, const char* name, const ExpOperatio
return new JsFunction(mtx,name,oper.lineNumber(),&args,label(),m_code);
}
JsFunction* JsFunction::cloneFunction(const ExpOperation& oper, ScriptMutex* mtx)
{
XDebug(DebugInfo,"JsFunction::cloneFunction(mtx=%p) '%s'='%s' (%s) in '%s' [%p]",
mtx,oper.name().c_str(),oper.c_str(),oper.typeOf(),toString().c_str(),this);
if (mutex() && ref()) {
XDebug(DebugAll,"JsFunction::cloneFunction() Function '%s' already cloned [%p]",toString().c_str(),this);
return this;
}
return static_cast<JsFunction*>(copy(mtx,m_func.toString(),oper));
}
void JsFunction::init()
{
params().addParam(new ExpFunction("apply"));

View File

@ -682,8 +682,19 @@ bool JsObject::runAssign(ObjList& stack, const ExpOperation& oper, GenObject* co
ExpWrapper* w = YOBJECT(ExpWrapper,&oper);
if (w) {
JsFunction* jsf = YOBJECT(JsFunction,w->object());
if (jsf)
jsf->firstName(oper.name());
if (jsf) {
ScriptRun* runner = YOBJECT(ScriptRun,context);
JsFunction* clonedFunc = jsf->cloneFunction(oper,runner && runner->context() ? runner->context()->mutex() : 0);
if (clonedFunc) {
clonedFunc->firstName(oper.name());
w = new ExpWrapper(clonedFunc,oper.name(),oper.barrier());
w->lineNumber(oper.lineNumber());
params().setParam(w);
return true;
}
else
jsf->firstName(oper.name());
}
params().setParam(w->clone(oper.name()));
}
else

View File

@ -2571,6 +2571,14 @@ public:
*/
virtual JsObject* copy(ScriptMutex* mtx, const char* name, const ExpOperation& oper) const;
/**
* Clone function
* @param oper ExpOperation that required the clone
* @param mtx Pointer to the mutex that serializes the cloning
* @return A copy of JsFunction if not already cloned, a reference to itself is function is already a clone
*/
virtual JsFunction* cloneFunction(const ExpOperation& oper, ScriptMutex* mtx = 0);
protected:
/**
* Try to evaluate a single native method