Added an Engine.shared singleton object that exposes in JS the engine shared variables.
git-svn-id: http://yate.null.ro/svn/yate/trunk@5160 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
parent
89210c61b5
commit
d6e57e9f4c
|
@ -126,6 +126,24 @@ private:
|
||||||
static ObjList s_globals;
|
static ObjList s_globals;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class JsShared : public JsObject
|
||||||
|
{
|
||||||
|
YCLASS(JsShared,JsObject)
|
||||||
|
public:
|
||||||
|
inline JsShared(Mutex* mtx)
|
||||||
|
: JsObject("Shared",mtx,true)
|
||||||
|
{
|
||||||
|
params().addParam(new ExpFunction("inc"));
|
||||||
|
params().addParam(new ExpFunction("dec"));
|
||||||
|
params().addParam(new ExpFunction("get"));
|
||||||
|
params().addParam(new ExpFunction("set"));
|
||||||
|
params().addParam(new ExpFunction("clear"));
|
||||||
|
params().addParam(new ExpFunction("exists"));
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
bool runNative(ObjList& stack, const ExpOperation& oper, GenObject* context);
|
||||||
|
};
|
||||||
|
|
||||||
#define MKDEBUG(lvl) params().addParam(new ExpOperation((long int)Debug ## lvl,"Debug" # lvl))
|
#define MKDEBUG(lvl) params().addParam(new ExpOperation((long int)Debug ## lvl,"Debug" # lvl))
|
||||||
class JsEngine : public JsObject
|
class JsEngine : public JsObject
|
||||||
{
|
{
|
||||||
|
@ -153,6 +171,7 @@ public:
|
||||||
params().addParam(new ExpFunction("idle"));
|
params().addParam(new ExpFunction("idle"));
|
||||||
params().addParam(new ExpFunction("dump_r"));
|
params().addParam(new ExpFunction("dump_r"));
|
||||||
params().addParam(new ExpFunction("print_r"));
|
params().addParam(new ExpFunction("print_r"));
|
||||||
|
params().addParam(new ExpWrapper(new JsShared(mtx),"shared"));
|
||||||
}
|
}
|
||||||
static void initialize(ScriptContext* context);
|
static void initialize(ScriptContext* context);
|
||||||
protected:
|
protected:
|
||||||
|
@ -164,6 +183,7 @@ class JsMessage : public JsObject
|
||||||
{
|
{
|
||||||
YCLASS(JsMessage,JsObject)
|
YCLASS(JsMessage,JsObject)
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline JsMessage(Mutex* mtx)
|
inline JsMessage(Mutex* mtx)
|
||||||
: JsObject("Message",mtx,true), m_message(0), m_owned(false)
|
: JsObject("Message",mtx,true), m_message(0), m_owned(false)
|
||||||
{
|
{
|
||||||
|
@ -480,6 +500,99 @@ void JsEngine::initialize(ScriptContext* context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool JsShared::runNative(ObjList& stack, const ExpOperation& oper, GenObject* context)
|
||||||
|
{
|
||||||
|
XDebug(&__plugin,DebugAll,"JsShared::runNative '%s'(%ld)",oper.name().c_str(),oper.number());
|
||||||
|
if (oper.name() == YSTRING("inc")) {
|
||||||
|
ObjList args;
|
||||||
|
switch (extractArgs(stack,oper,context,args)) {
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ExpOperation* param = static_cast<ExpOperation*>(args[0]);
|
||||||
|
ExpOperation* modulo = static_cast<ExpOperation*>(args[1]);
|
||||||
|
int mod = 0;
|
||||||
|
if (modulo && modulo->isInteger())
|
||||||
|
mod = modulo->number();
|
||||||
|
if (mod > 1)
|
||||||
|
mod--;
|
||||||
|
else
|
||||||
|
mod = 0;
|
||||||
|
ExpEvaluator::pushOne(stack,new ExpOperation((long)Engine::sharedVars().inc(*param,mod)));
|
||||||
|
}
|
||||||
|
else if (oper.name() == YSTRING("dec")) {
|
||||||
|
ObjList args;
|
||||||
|
switch (extractArgs(stack,oper,context,args)) {
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ExpOperation* param = static_cast<ExpOperation*>(args[0]);
|
||||||
|
ExpOperation* modulo = static_cast<ExpOperation*>(args[1]);
|
||||||
|
int mod = 0;
|
||||||
|
if (modulo && modulo->isInteger())
|
||||||
|
mod = modulo->number();
|
||||||
|
if (mod > 1)
|
||||||
|
mod--;
|
||||||
|
else
|
||||||
|
mod = 0;
|
||||||
|
ExpEvaluator::pushOne(stack,new ExpOperation((long)Engine::sharedVars().dec(*param,mod)));
|
||||||
|
}
|
||||||
|
else if (oper.name() == YSTRING("get")) {
|
||||||
|
if (oper.number() != 1)
|
||||||
|
return false;
|
||||||
|
ExpOperation* param = popValue(stack,context);
|
||||||
|
if (!param)
|
||||||
|
return false;
|
||||||
|
String buf;
|
||||||
|
Engine::sharedVars().get(*param,buf);
|
||||||
|
TelEngine::destruct(param);
|
||||||
|
ExpEvaluator::pushOne(stack,new ExpOperation(buf));
|
||||||
|
}
|
||||||
|
else if (oper.name() == YSTRING("set")) {
|
||||||
|
if (oper.number() != 2)
|
||||||
|
return false;
|
||||||
|
ExpOperation* val = popValue(stack,context);
|
||||||
|
if (!val)
|
||||||
|
return false;
|
||||||
|
ExpOperation* param = popValue(stack,context);
|
||||||
|
if (!param) {
|
||||||
|
TelEngine::destruct(val);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Engine::sharedVars().set(*param,*val);
|
||||||
|
TelEngine::destruct(param);
|
||||||
|
TelEngine::destruct(val);
|
||||||
|
}
|
||||||
|
else if (oper.name() == YSTRING("clear")) {
|
||||||
|
if (oper.number() != 1)
|
||||||
|
return false;
|
||||||
|
ExpOperation* param = popValue(stack,context);
|
||||||
|
if (!param)
|
||||||
|
return false;
|
||||||
|
Engine::sharedVars().clear(*param);
|
||||||
|
TelEngine::destruct(param);
|
||||||
|
}
|
||||||
|
else if (oper.name() == YSTRING("exists")) {
|
||||||
|
if (oper.number() != 1)
|
||||||
|
return false;
|
||||||
|
ExpOperation* param = popValue(stack,context);
|
||||||
|
if (!param)
|
||||||
|
return false;
|
||||||
|
ExpEvaluator::pushOne(stack,new ExpOperation(Engine::sharedVars().exists(*param)));
|
||||||
|
TelEngine::destruct(param);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return JsObject::runNative(stack,oper,context);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool JsMessage::runAssign(ObjList& stack, const ExpOperation& oper, GenObject* context)
|
bool JsMessage::runAssign(ObjList& stack, const ExpOperation& oper, GenObject* context)
|
||||||
{
|
{
|
||||||
XDebug(&__plugin,DebugAll,"JsMessage::runAssign '%s'='%s'",oper.name().c_str(),oper.c_str());
|
XDebug(&__plugin,DebugAll,"JsMessage::runAssign '%s'='%s'",oper.name().c_str(),oper.c_str());
|
||||||
|
|
Loading…
Reference in New Issue