Moved helper functions to static methods of JsObject so code can be reused.
git-svn-id: http://yate.null.ro/svn/yate/trunk@5063 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
parent
1667d0bbc3
commit
97c2a7e6bb
|
@ -153,33 +153,6 @@ protected:
|
|||
}; // anonymous namespace
|
||||
|
||||
|
||||
// Helper function that adds an object to a parent
|
||||
static inline void addObject(NamedList& params, const char* name, JsObject* obj)
|
||||
{
|
||||
params.addParam(new NamedPointer(name,obj,obj->toString()));
|
||||
}
|
||||
|
||||
// Helper function that adds a constructor to a parent
|
||||
static inline void addConstructor(NamedList& params, const char* name, JsObject* obj)
|
||||
{
|
||||
JsFunction* ctr = new JsFunction(obj->mutex(),name);
|
||||
ctr->params().addParam(new NamedPointer("prototype",obj,obj->toString()));
|
||||
params.addParam(new NamedPointer(name,ctr,ctr->toString()));
|
||||
}
|
||||
|
||||
// Helper function that pops arguments off a stack to a list in proper order
|
||||
static int extractArgs(JsObject* obj, ObjList& stack, const ExpOperation& oper, GenObject* context, ObjList& arguments)
|
||||
{
|
||||
if (!obj || !oper.number())
|
||||
return 0;
|
||||
for (long int i = oper.number(); i; i--) {
|
||||
ExpOperation* op = obj->popValue(stack,context);
|
||||
arguments.insert(op);
|
||||
}
|
||||
return oper.number();
|
||||
}
|
||||
|
||||
|
||||
JsObject::JsObject(const char* name, Mutex* mtx, bool frozen)
|
||||
: ScriptContext(String("[Object ") + name + "]"),
|
||||
m_frozen(frozen), m_mutex(mtx)
|
||||
|
@ -315,6 +288,32 @@ ExpOperation* JsObject::popValue(ObjList& stack, GenObject* context)
|
|||
return ok ? ExpEvaluator::popOne(stack) : 0;
|
||||
}
|
||||
|
||||
// Static method that adds an object to a parent
|
||||
void JsObject::addObject(NamedList& params, const char* name, JsObject* obj)
|
||||
{
|
||||
params.addParam(new NamedPointer(name,obj,obj->toString()));
|
||||
}
|
||||
|
||||
// Static method that adds a constructor to a parent
|
||||
void JsObject::addConstructor(NamedList& params, const char* name, JsObject* obj)
|
||||
{
|
||||
JsFunction* ctr = new JsFunction(obj->mutex(),name);
|
||||
ctr->params().addParam(new NamedPointer("prototype",obj,obj->toString()));
|
||||
params.addParam(new NamedPointer(name,ctr,ctr->toString()));
|
||||
}
|
||||
|
||||
// Static method that pops arguments off a stack to a list in proper order
|
||||
int JsObject::extractArgs(JsObject* obj, ObjList& stack, const ExpOperation& oper, GenObject* context, ObjList& arguments)
|
||||
{
|
||||
if (!obj || !oper.number())
|
||||
return 0;
|
||||
for (long int i = oper.number(); i; i--) {
|
||||
ExpOperation* op = obj->popValue(stack,context);
|
||||
arguments.insert(op);
|
||||
}
|
||||
return oper.number();
|
||||
}
|
||||
|
||||
// Initialize standard globals in the execution context
|
||||
void JsObject::initialize(ScriptContext* context)
|
||||
{
|
||||
|
@ -856,6 +855,7 @@ bool JsFunction::runDefined(ObjList& stack, const ExpOperation& oper, GenObject*
|
|||
// found prototype, build object
|
||||
JsObject* obj = proto->clone();
|
||||
obj->copyFields(stack,*proto,context);
|
||||
obj->runConstructor(stack,oper,context);
|
||||
ExpEvaluator::pushOne(stack,new ExpWrapper(obj,oper.name()));
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -1415,6 +1415,15 @@ public:
|
|||
inline JsObject* clone() const
|
||||
{ return clone(toString()); }
|
||||
|
||||
/**
|
||||
* Native object constructor
|
||||
* @param stack Evaluation stack in use
|
||||
* @param oper Constructor function to evaluate
|
||||
* @param context Pointer to arbitrary object passed from evaluation methods
|
||||
*/
|
||||
virtual void runConstructor(ObjList& stack, const ExpOperation& oper, GenObject* context)
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Try to evaluate a single method
|
||||
* @param stack Evaluation stack in use, parameters are popped off this stack
|
||||
|
@ -1464,6 +1473,44 @@ public:
|
|||
inline void freeze()
|
||||
{ m_frozen = true; }
|
||||
|
||||
/**
|
||||
* Helper static method that adds an object to a parent
|
||||
* @param params List of parameters where to add the object
|
||||
* @param name Name of the new parameter
|
||||
* @param obj Pointer to the object to add
|
||||
*/
|
||||
static void addObject(NamedList& params, const char* name, JsObject* obj);
|
||||
|
||||
/**
|
||||
* Helper static method that adds a constructor to a parent
|
||||
* @param params List of parameters where to add the constructor
|
||||
* @param name Name of the new parameter
|
||||
* @param obj Pointer to the prototype object to add
|
||||
*/
|
||||
static void addConstructor(NamedList& params, const char* name, JsObject* obj);
|
||||
|
||||
/**
|
||||
* Helper static method that pops arguments off a stack to a list in proper order
|
||||
* @param obj Pointer to the object to use when popping each argument
|
||||
* @param stack Evaluation stack in use, parameters are popped off this stack
|
||||
* @param oper Function that is being evaluated
|
||||
* @param context Pointer to arbitrary object passed from evaluation methods
|
||||
* @param arguments List where the arguments are added in proper order
|
||||
* @return Number of arguments popped off stack
|
||||
*/
|
||||
static int extractArgs(JsObject* obj, ObjList& stack, const ExpOperation& oper, GenObject* context, ObjList& arguments);
|
||||
|
||||
/**
|
||||
* Helper method that pops arguments off a stack to a list in proper order
|
||||
* @param stack Evaluation stack in use, parameters are popped off this stack
|
||||
* @param oper Function that is being evaluated
|
||||
* @param context Pointer to arbitrary object passed from evaluation methods
|
||||
* @param arguments List where the arguments are added in proper order
|
||||
* @return Number of arguments popped off stack
|
||||
*/
|
||||
inline int extractArgs(ObjList& stack, const ExpOperation& oper, GenObject* context, ObjList& arguments)
|
||||
{ return extractArgs(this,stack,oper,context,arguments); }
|
||||
|
||||
/**
|
||||
* Initialize the standard global objects in a context
|
||||
* @param context Script context to initialize
|
||||
|
|
|
@ -130,6 +130,7 @@ public:
|
|||
if (m_owned)
|
||||
TelEngine::destruct(m_message);
|
||||
}
|
||||
virtual void runConstructor(ObjList& stack, const ExpOperation& oper, GenObject* context);
|
||||
inline void clearMsg()
|
||||
{ m_message = 0; m_owned = false; }
|
||||
static void initialize(ScriptContext* context);
|
||||
|
@ -193,13 +194,6 @@ UNLOAD_PLUGIN(unloadNow)
|
|||
}
|
||||
|
||||
|
||||
// Helper function that adds an object to a parent
|
||||
static inline void addObject(NamedList& params, const char* name, JsObject* obj)
|
||||
{
|
||||
params.addParam(new NamedPointer(name,obj,obj->toString()));
|
||||
}
|
||||
|
||||
|
||||
bool JsEngine::runNative(ObjList& stack, const ExpOperation& oper, GenObject* context)
|
||||
{
|
||||
if (oper.name() == YSTRING("output")) {
|
||||
|
@ -257,17 +251,7 @@ void JsEngine::initialize(ScriptContext* context)
|
|||
|
||||
bool JsMessage::runNative(ObjList& stack, const ExpOperation& oper, GenObject* context)
|
||||
{
|
||||
if (oper.name() == YSTRING("constructor")) {
|
||||
if (oper.number() != 1)
|
||||
return false;
|
||||
ExpOperation* op = popValue(stack,context);
|
||||
if (!op)
|
||||
return false;
|
||||
Message* m = new Message(*op);
|
||||
ExpEvaluator::pushOne(stack,new ExpWrapper(new JsMessage(m,mutex(),true)));
|
||||
TelEngine::destruct(op);
|
||||
}
|
||||
else if (oper.name() == YSTRING("broadcast")) {
|
||||
if (oper.name() == YSTRING("broadcast")) {
|
||||
if (oper.number() != 0)
|
||||
return false;
|
||||
ExpEvaluator::pushOne(stack,new ExpOperation(m_message && m_message->broadcast()));
|
||||
|
@ -303,6 +287,18 @@ bool JsMessage::runNative(ObjList& stack, const ExpOperation& oper, GenObject* c
|
|||
return true;
|
||||
}
|
||||
|
||||
void JsMessage::runConstructor(ObjList& stack, const ExpOperation& oper, GenObject* context)
|
||||
{
|
||||
if (oper.number() != 1)
|
||||
return;
|
||||
ExpOperation* op = popValue(stack,context);
|
||||
if (!op)
|
||||
return;
|
||||
Message* m = new Message(*op);
|
||||
ExpEvaluator::pushOne(stack,new ExpWrapper(new JsMessage(m,mutex(),true)));
|
||||
TelEngine::destruct(op);
|
||||
}
|
||||
|
||||
void JsMessage::initialize(ScriptContext* context)
|
||||
{
|
||||
if (!context)
|
||||
|
|
Loading…
Reference in New Issue