Fixed implementation of JS Array concat() and reverse() methods.
git-svn-id: http://yate.null.ro/svn/yate/trunk@5759 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
parent
5efdfb17dc
commit
632070e047
|
@ -590,39 +590,38 @@ bool JsArray::runNative(ObjList& stack, const ExpOperation& oper, GenObject* con
|
||||||
// creates array ["a", "b", "c", 1, 2, 3], leaving alpha unchanged
|
// creates array ["a", "b", "c", 1, 2, 3], leaving alpha unchanged
|
||||||
// var alphaNumeric = alpha.concat(1, [2, 3]);
|
// var alphaNumeric = alpha.concat(1, [2, 3]);
|
||||||
|
|
||||||
// TODO: fix it, it's broken for non-strings
|
ObjList args;
|
||||||
if (!oper.number())
|
extractArgs(this,stack,oper,context,args);
|
||||||
return false;
|
|
||||||
JsArray* array = new JsArray(mutex());
|
JsArray* array = new JsArray(mutex());
|
||||||
// copy this array
|
// copy this array - only numerically indexed elements!
|
||||||
for (int i = 0; i < m_length; i++)
|
for (int i = 0; i < m_length; i++) {
|
||||||
array->params().addParam(params().getParam(String(i)));
|
NamedString* ns = params().getParam(String(i));
|
||||||
|
ExpOperation* op = YOBJECT(ExpOperation,ns);
|
||||||
|
op = op ? op->clone() : new ExpOperation(*ns,ns->name(),true);
|
||||||
|
array->params().addParam(op);
|
||||||
|
}
|
||||||
array->setLength(length());
|
array->setLength(length());
|
||||||
// add parameters (JsArray of JsObject)
|
// add parameters - either basic types or elements of Array
|
||||||
for (int i = (int)oper.number(); i; i--) {
|
while (ExpOperation* op = static_cast<ExpOperation*>(args.remove(false))) {
|
||||||
ExpOperation* op = popValue(stack,context);
|
JsArray* ja = YOBJECT(JsArray,op);
|
||||||
ExpWrapper* obj = YOBJECT(ExpWrapper,op);
|
|
||||||
if (!obj)
|
|
||||||
continue;
|
|
||||||
JsArray* ja = (JsArray*)obj->getObject(YATOM("JsArray"));
|
|
||||||
if (ja) {
|
if (ja) {
|
||||||
for (int j = 0; j < ja->length(); j++)
|
int len = ja->length();
|
||||||
array->params().addParam(String(j + array->length()),ja->params().getValue(String(j)));
|
for (int i = 0; i < len; i++) {
|
||||||
array->setLength(array->length() + ja->length());
|
NamedString* ns = ja->params().getParam(String(i));
|
||||||
|
op = YOBJECT(ExpOperation,ns);
|
||||||
|
op = op ? op->clone() : new ExpOperation(*ns,0,true);
|
||||||
|
const_cast<String&>(op->name()) = (unsigned int)array->m_length++;
|
||||||
|
array->params().addParam(op);
|
||||||
|
}
|
||||||
|
TelEngine::destruct(ja);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
JsObject* jo = (JsObject*)obj->getObject(YATOM("JsObject"));
|
const_cast<String&>(op->name()) = (unsigned int)array->m_length++;
|
||||||
if (jo) {
|
array->params().addParam(op);
|
||||||
array->params().addParam(new NamedPointer(String(array->length()),jo));
|
|
||||||
array->setLength(array->length() + 1);
|
|
||||||
jo->ref();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
TelEngine::destruct(op);
|
|
||||||
}
|
}
|
||||||
ExpEvaluator::pushOne(stack,new ExpWrapper(array,0));
|
ExpEvaluator::pushOne(stack,new ExpWrapper(array));
|
||||||
}
|
}
|
||||||
else if (oper.name() == YSTRING("join")) {
|
else if (oper.name() == YSTRING("join")) {
|
||||||
// Joins all elements of an array into a string
|
// Joins all elements of an array into a string
|
||||||
|
@ -645,16 +644,22 @@ bool JsArray::runNative(ObjList& stack, const ExpOperation& oper, GenObject* con
|
||||||
// Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
|
// Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
|
||||||
// var myArray = ["one", "two", "three"];
|
// var myArray = ["one", "two", "three"];
|
||||||
// myArray.reverse(); => three, two, one
|
// myArray.reverse(); => three, two, one
|
||||||
|
if (oper.number())
|
||||||
// TODO: fix it, it's broken for non-strings
|
return false;
|
||||||
NamedList reversed("");
|
int i1 = 0;
|
||||||
String separator = ",";
|
int i2 = length() - 1;
|
||||||
String toCopy;
|
for (; i1 < i2; i1++, i2--) {
|
||||||
for (int32_t i = 0; i < length(); i++)
|
String s1(i1);
|
||||||
toCopy.append(params()[String(i)],separator);
|
String s2(i2);
|
||||||
reversed.copyParams(params(),toCopy);
|
NamedString* n1 = params().getParam(s1);
|
||||||
for (int32_t i = length(); i; i--)
|
NamedString* n2 = params().getParam(s2);
|
||||||
params().setParam(String(length() - i),reversed.getValue(String(i - 1)));
|
if (n1)
|
||||||
|
const_cast<String&>(n1->name()) = s2;
|
||||||
|
if (n2)
|
||||||
|
const_cast<String&>(n2->name()) = s1;
|
||||||
|
}
|
||||||
|
ref();
|
||||||
|
ExpEvaluator::pushOne(stack,new ExpWrapper(this));
|
||||||
}
|
}
|
||||||
else if (oper.name() == YSTRING("shift")) {
|
else if (oper.name() == YSTRING("shift")) {
|
||||||
// Removes the first element from an array and returns that element
|
// Removes the first element from an array and returns that element
|
||||||
|
|
Loading…
Reference in New Issue