Skip to content

Commit 61fe485

Browse files
Add 'Array.slice' function, and its corresponding unit test.
All tests are passing!!!
1 parent df89362 commit 61fe485

File tree

2 files changed

+39
-53
lines changed

2 files changed

+39
-53
lines changed

TinyJS_Functions.cpp

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -254,52 +254,6 @@ Ref<JSValue> scArrayIndexOf(FunctionScope* pScope)
254254
}
255255
return jsInt(-1);
256256
}
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-
*/
303257

304258
Ref<JSValue>scArrayJoin(FunctionScope* pScope)
305259
{
@@ -322,6 +276,33 @@ Ref<JSValue>scArrayJoin(FunctionScope* pScope)
322276
return jsString(output.str());
323277
}
324278

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+
325306
// ----------------------------------------------- Register Functions
326307

327308
Ref<JSObject> createClass(const char* className,
@@ -382,9 +363,8 @@ void registerFunctions(Ref<IScope> scope)
382363
addNative("function parseInt(str)", scIntegerParseInt, scope); // string to int
383364
addNative("function Integer.valueOf(str)", scIntegerValueOf, scope); // value of a single character
384365
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);
388368
addNative("function Array.prototype.join(separator)", scArrayJoin, scope);
389369
addNative("function Array.prototype.push(x)", scArrayPush, scope);
390370
addNative("function Array.prototype.indexOf(searchElement, fromIndex)", scArrayIndexOf, scope);

tests/test029.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
// test for array remove
1+
// test for array slice
22
var a = [1,2,4,5,7];
33

4-
a.remove(2);
5-
a.remove(5);
4+
var b = a.slice(2);
5+
var c = a.slice(1,3);
6+
var d = a.slice(1, -3);
67

7-
result = a.length==3 && a[0]==1 && a[1]==4 && a[2]==7;
8+
9+
assert (b.join() == "4,5,7", "wrong 'b' array: " + b.join());
10+
assert (c.join() == "2,4", "wrong 'c' array: " + c.join());
11+
assert (d.join() == "2,4,5,7", "wrong 'd' array: " + d.join());
12+
13+
result = 1;

0 commit comments

Comments
 (0)