Browse Source

Added a Javascript array splice method

tags/2021-05-28
jules 9 years ago
parent
commit
37cffb74ce
1 changed files with 33 additions and 0 deletions
  1. +33
    -0
      modules/juce_core/javascript/juce_Javascript.cpp

+ 33
- 0
modules/juce_core/javascript/juce_Javascript.cpp View File

@@ -1506,6 +1506,7 @@ struct JavascriptEngine::RootObject : public DynamicObject
setMethod ("remove", remove); setMethod ("remove", remove);
setMethod ("join", join); setMethod ("join", join);
setMethod ("push", push); setMethod ("push", push);
setMethod ("splice", splice);
} }
static Identifier getClassName() { static const Identifier i ("Array"); return i; } static Identifier getClassName() { static const Identifier i ("Array"); return i; }
@@ -1549,6 +1550,38 @@ struct JavascriptEngine::RootObject : public DynamicObject
return var::undefined(); return var::undefined();
} }
static var splice (Args a)
{
if (Array<var>* array = a.thisObject.getArray())
{
const int arraySize = array->size();
int start = get (a, 0);
if (start < 0)
start = jmax (0, arraySize + start);
else if (start > arraySize)
start = arraySize;
const int num = a.numArguments > 1 ? jlimit (0, arraySize - start, static_cast<int> (get (a, 1)))
: arraySize - start;
Array<var> itemsRemoved;
itemsRemoved.ensureStorageAllocated (num);
for (int i = 0; i < num; ++i)
itemsRemoved.add (array->getReference (start + i));
array->removeRange (start, num);
for (int i = 2; i < a.numArguments; ++i)
array->insert (start++, get (a, i));
return itemsRemoved;
}
return var::undefined();
}
}; };
//============================================================================== //==============================================================================


Loading…
Cancel
Save