diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 1445a5daa6..3e1914e493 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -1506,6 +1506,7 @@ struct JavascriptEngine::RootObject : public DynamicObject setMethod ("remove", remove); setMethod ("join", join); setMethod ("push", push); + setMethod ("splice", splice); } static Identifier getClassName() { static const Identifier i ("Array"); return i; } @@ -1549,6 +1550,38 @@ struct JavascriptEngine::RootObject : public DynamicObject return var::undefined(); } + + static var splice (Args a) + { + if (Array* 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 (get (a, 1))) + : arraySize - start; + + Array 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(); + } }; //==============================================================================