@@ -254,52 +254,6 @@ Ref<JSValue> scArrayIndexOf(FunctionScope* pScope)
254
254
}
255
255
return jsInt (-1 );
256
256
}
257
- /* Ref<JSValue> scArrayContains(FunctionScope* pScope) {
258
- Ref<JSValue> obj = pScope->getParam("obj");
259
- Ref<JSValue> arr = pScope->getThis();
260
-
261
- if (!arr->isArray())
262
- return jsFalse();
263
-
264
- Ref<JSArray> v = arr.staticCast();
265
-
266
- while (v) {
267
- if (v->var->equals(obj)) {
268
- contains = true;
269
- break;
270
- }
271
- v = v->nextSibling;
272
- }
273
-
274
- return jsFalse();
275
- }
276
-
277
- Ref<JSValue> scArrayRemove(FunctionScope* pScope) {
278
- CScriptVar *obj = pScope->getParam("obj");
279
- vector<int> removedIndices;
280
- CScriptVarLink *v;
281
- // remove
282
- v = pScope->getThis()->firstChild;
283
- while (v) {
284
- if (v->var->equals(obj)) {
285
- removedIndices.push_back(v->getIntName());
286
- }
287
- v = v->nextSibling;
288
- }
289
- // renumber
290
- v = pScope->getThis()->firstChild;
291
- while (v) {
292
- int n = v->getIntName();
293
- int newn = n;
294
- for (size_t i=0;i<removedIndices.size();i++)
295
- if (n>=removedIndices[i])
296
- newn--;
297
- if (newn!=n)
298
- v->setIntName(newn);
299
- v = v->nextSibling;
300
- }
301
- }
302
- */
303
257
304
258
Ref<JSValue>scArrayJoin (FunctionScope* pScope)
305
259
{
@@ -322,6 +276,33 @@ Ref<JSValue>scArrayJoin(FunctionScope* pScope)
322
276
return jsString (output.str ());
323
277
}
324
278
279
+ /* *
280
+ * Creates a 'alice' of the array. A contiguous subset of array elements
281
+ * defined by a initial index (included) and a final index (not included)
282
+ * @param pScope
283
+ * @return
284
+ */
285
+ Ref<JSValue>scArraySlice (FunctionScope* pScope)
286
+ {
287
+ Ref<JSArray> arr = pScope->getThis ().staticCast <JSArray>();
288
+ auto begin = pScope->getParam (" begin" );
289
+ auto end = pScope->getParam (" end" );
290
+ const size_t iBegin = begin->toInt32 ();
291
+ size_t iEnd = arr->length ();
292
+
293
+ if (!end->isNull () && end->toInt32 () >= 0 )
294
+ iEnd = end->toInt32 ();
295
+
296
+ iEnd = max (iEnd, iBegin);
297
+
298
+ auto result = JSArray::create ();
299
+
300
+ for (size_t i = iBegin; i < iEnd; ++i)
301
+ result->push (arr->getAt (i));
302
+
303
+ return result;
304
+ }
305
+
325
306
// ----------------------------------------------- Register Functions
326
307
327
308
Ref<JSObject> createClass (const char * className,
@@ -382,9 +363,8 @@ void registerFunctions(Ref<IScope> scope)
382
363
addNative (" function parseInt(str)" , scIntegerParseInt, scope); // string to int
383
364
addNative (" function Integer.valueOf(str)" , scIntegerValueOf, scope); // value of a single character
384
365
addNative (" function JSON.stringify(obj, replacer)" , scJSONStringify, scope); // convert to JSON. replacer is ignored at the moment
385
- // JSON.parse is left out as you can (unsafely!) use eval instead
386
- // addNative("function Array.contains(obj)", scArrayContains, scope);
387
- // addNative("function Array.remove(obj)", scArrayRemove, scope);
366
+ // TODO: Add JSON.parse()
367
+ addNative (" function Array.prototype.slice(begin, end)" , scArraySlice, scope);
388
368
addNative (" function Array.prototype.join(separator)" , scArrayJoin, scope);
389
369
addNative (" function Array.prototype.push(x)" , scArrayPush, scope);
390
370
addNative (" function Array.prototype.indexOf(searchElement, fromIndex)" , scArrayIndexOf, scope);
0 commit comments