Bug fix: properly set prototype property on JS Array objects.

Added JsObject::setPrototype() in order to set an objects prototype from given context.
Added new JsArray constructor which set its own prototype. Replaced use of constructor which returned Array prototype with this contructor.



git-svn-id: http://yate.null.ro/svn/yate/trunk@5796 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
oana 2014-03-13 13:10:56 +00:00
parent c13299b2bf
commit 6d2de9dfc8
4 changed files with 48 additions and 8 deletions

View File

@ -729,7 +729,7 @@ bool JsContext::runStringFunction(GenObject* obj, const String& name, ObjList& s
ok = buf.matches(r);
}
if (ok) {
JsArray* jsa = new JsArray(mutex());
JsArray* jsa = new JsArray(context,mutex());
for (int i = 0; i <= buf.matchCount(); i++)
jsa->push(new ExpOperation(buf.matchString(i)));
jsa->params().addParam(new ExpOperation((int64_t)buf.matchOffset(),"index"));
@ -808,7 +808,7 @@ bool JsContext::runStringFunction(GenObject* obj, const String& name, ObjList& s
} while (false);
if (name == YSTRING("split")) {
ObjList args;
JsArray* array = new JsArray(mutex());
JsArray* array = new JsArray(context,mutex());
if (!(extractArgs(stack,oper,context,args) && args.skipNull()))
SPLIT_EMPTY();
String* s = static_cast<String*>(args[0]);

View File

@ -230,6 +230,22 @@ void JsObject::printRecursive(const GenObject* obj)
Output("%s",buf.c_str());
}
void JsObject::setPrototype(GenObject* context, const String& objName)
{
ScriptContext* ctxt = YOBJECT(ScriptContext,context);
if (!ctxt) {
ScriptRun* sr = static_cast<ScriptRun*>(context);
if (!(sr && (ctxt = YOBJECT(ScriptContext,sr->context()))))
return;
}
JsObject* objCtr = YOBJECT(JsObject,ctxt->params().getParam(objName));
if (objCtr) {
JsObject* proto = YOBJECT(JsObject,objCtr->params().getParam(YSTRING("prototype")));
if (proto && proto->ref())
params().addParam(new ExpWrapper(proto,protoName()));
}
}
JsObject* JsObject::buildCallContext(Mutex* mtx, JsObject* thisObj)
{
JsObject* ctxt = new JsObject(mtx,"()");
@ -490,6 +506,12 @@ JsArray::JsArray(Mutex* mtx)
params().addParam("length","0");
}
JsArray::JsArray(GenObject* context, Mutex* mtx)
: JsObject(mtx,"[object Array]"), m_length(0)
{
setPrototype(context,YSTRING("Array"));
}
JsObject* JsArray::copy(Mutex* mtx) const
{
JsArray* jsa = new JsArray(mtx,toString(),frozen());

View File

@ -1881,6 +1881,13 @@ public:
inline JsObject* clone() const
{ return clone(toString()); }
/**
* Set the object prototype
* @param context Pointer to arbitrary object passed from evaluation methods
* @param objName Name of the object prototype to set the this object
*/
void setPrototype(GenObject* context, const String& objName);
/**
* Deep copy method
* @param mtx Pointer to the mutex that serializes the copied object
@ -2187,13 +2194,16 @@ private:
*/
class YSCRIPT_API JsArray : public JsObject
{
friend class JsObject;
YCLASS(JsArray,JsObject)
public:
/**
* Constructor
* Constructor for an empty array with prototype
* @param context Script context from which Array prototype is obtainend
* @param mtx Pointer to the mutex that serializes this object
*/
JsArray(Mutex* mtx = 0);
JsArray(GenObject* context, Mutex* mtx = 0);
/**
* Constructor for an empty array
@ -2261,6 +2271,7 @@ public:
virtual JsObject* runConstructor(ObjList& stack, const ExpOperation& oper, GenObject* context);
protected:
/**
* Clone and rename method
* @param name Name of the cloned object
@ -2280,6 +2291,13 @@ protected:
bool runNative(ObjList& stack, const ExpOperation& oper, GenObject* context);
private:
/**
* Private constructor
* @param mtx Pointer to the mutex that serializes this object
*/
JsArray(Mutex* mtx = 0);
bool runNativeSlice(ObjList& stack, const ExpOperation& oper, GenObject* context);
bool runNativeSplice(ObjList& stack, const ExpOperation& oper, GenObject* context);
bool runNativeSort(ObjList& stack, const ExpOperation& oper, GenObject* context);

View File

@ -1314,7 +1314,7 @@ void JsMessage::getColumn(ObjList& stack, const ExpOperation* col, GenObject* co
}
}
if (idx >= 0 && idx < cols) {
JsArray* jsa = new JsArray(mutex());
JsArray* jsa = new JsArray(context,mutex());
for (int r = 1; r <= rows; r++) {
GenObject* o = arr->get(idx,r);
if (o)
@ -1333,7 +1333,7 @@ void JsMessage::getColumn(ObjList& stack, const ExpOperation* col, GenObject* co
const String* name = YOBJECT(String,arr->get(c,0));
if (TelEngine::null(name))
continue;
JsArray* jsa = new JsArray(mutex());
JsArray* jsa = new JsArray(context,mutex());
for (int r = 1; r <= rows; r++) {
GenObject* o = arr->get(c,r);
if (o)
@ -1379,7 +1379,7 @@ void JsMessage::getRow(ObjList& stack, const ExpOperation* row, GenObject* conte
}
else {
// [ { col1: val11, col2: val12 }, { col1: val21, col2: val22 } ]
JsArray* jsa = new JsArray(mutex());
JsArray* jsa = new JsArray(context,mutex());
for (int r = 1; r <= rows; r++) {
JsObject* jso = new JsObject("Object",mutex());
for (int c = 0; c < cols; c++) {
@ -1817,7 +1817,7 @@ bool JsXML::runNative(ObjList& stack, const ExpOperation& oper, GenObject* conte
if (m_xml)
xml = m_xml->findFirstChild(name,ns);
if (xml) {
JsArray* jsa = new JsArray(mutex());
JsArray* jsa = new JsArray(context,mutex());
while (xml) {
jsa->push(new ExpWrapper(new JsXML(mutex(),xml,owner())));
xml = m_xml->findNextChild(xml,name,ns);