Add ES6 Array.prototype.fill (fix #317)

This commit is contained in:
Gordon Williams 2014-04-28 18:26:44 +01:00
parent a756ccd574
commit 2160e07e4c
5 changed files with 63 additions and 1 deletions

View File

@ -24,6 +24,7 @@
Fix deleting the last element in array (fix #346)
More helpful I2C error messages (fix #10)
Fix overriding built-in functions (fix #202)
Add ES6 Array.prototype.fill (fix #317)
1v61 : Fix toString crash for large numbers
Support floating-point literals without a leading 0 - eg '.5' (fix #296)

View File

@ -563,3 +563,36 @@ JsVar *jswrap_array_concat(JsVar *parent, JsVar *args) {
jsvArrayIteratorFree(&argsIt);
return result;
}
/*JSON{ "type":"method", "class": "Array", "name" : "fill",
"description" : "Fill this array with the given value, for every index `>= start` and `< end`",
"generate" : "jswrap_array_fill",
"params" : [ [ "value", "JsVar", "The value to fill the array with" ],
[ "start", "int", "Optional. The index to start from (or 0). If start is negative, it is treated as length+start where length is the length of the array" ],
[ "end", "JsVar", "Optional. The index to end at (or the array length). If end is negative, it is treated as length+end." ] ],
"return" : ["JsVar", "This array"]
}*/
JsVar *jswrap_array_fill(JsVar *parent, JsVar *value, JsVarInt start, JsVar *endVar) {
if (!jsvIsIterable(parent)) return 0;
JsVarInt length = jsvGetLength(parent);
if (start < 0) start = start + length;
if (start < 0) return 0;
JsVarInt end = jsvIsNumeric(endVar) ? jsvGetInteger(endVar) : length;
if (end < 0) end = end + length;
if (end < 0) return 0;
JsvIterator it;
jsvIteratorNew(&it, parent);
while (jsvIteratorHasElement(&it)) {
JsVarInt idx = jsvGetIntegerAndUnLock(jsvIteratorGetKey(&it));
if (idx>=start && idx<end) {
jsvIteratorSetValue(&it, value);
}
jsvIteratorNext(&it);
}
jsvIteratorFree(&it);
return jsvLockAgain(parent);
}

View File

@ -27,3 +27,4 @@ void jswrap_array_forEach(JsVar *parent, JsVar *funcVar, JsVar *thisVar);
JsVar *jswrap_array_reduce(JsVar *parent, JsVar *funcVar, JsVar *initialValue);
JsVar *jswrap_array_sort (JsVar *array, JsVar *compareFn);
JsVar *jswrap_array_concat(JsVar *parent, JsVar *args);
JsVar *jswrap_array_fill(JsVar *parent, JsVar *value, JsVarInt start, JsVar *endVar);

View File

@ -385,4 +385,11 @@ JsVar *jswrap_arraybufferview_map(JsVar *parent, JsVar *funcVar, JsVar *thisVar)
[ "initialValue", "JsVar", "if specified, the initial value to pass to the function"] ],
"return" : ["JsVar", "The value returned by the last function called"]
}*/
/*JSON{ "type":"method", "class": "ArrayBufferView", "name" : "fill",
"description" : "Fill this array with the given value, for every index `>= start` and `< end`",
"generate" : "jswrap_array_fill",
"params" : [ [ "value", "JsVar", "The value to fill the array with" ],
[ "start", "int", "Optional. The index to start from (or 0). If start is negative, it is treated as length+start where length is the length of the array" ],
[ "end", "JsVar", "Optional. The index to end at (or the array length). If end is negative, it is treated as length+end." ] ],
"return" : ["JsVar", "This array"]
}*/

20
tests/test_array_fill.js Normal file
View File

@ -0,0 +1,20 @@
// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.fill
var r = [];
var a = [0,0,0,0,0,0,0,0,0,0];
a.fill(2);
r.push(a.join(",") == "2,2,2,2,2,2,2,2,2,2");
a.fill(3,5);
r.push(a.join(",") == "2,2,2,2,2,3,3,3,3,3");
a.fill(4,6,7);
r.push(a.join(",") == "2,2,2,2,2,3,4,3,3,3");
a.fill(5,-3);
r.push(a.join(",") == "2,2,2,2,2,3,4,5,5,5");
a.fill(6,-2,-1);
r.push(a.join(",") == "2,2,2,2,2,3,4,5,6,5");
var pass = 0;
r.forEach(function(n) { if (n) pass++; });
result = pass == r.length;