Fix TypedArray.indexOf (fix #1468)

This commit is contained in:
Gordon Williams 2018-06-13 09:52:04 +01:00
parent 51ad06ba8c
commit 6264fa0ee4
4 changed files with 22 additions and 12 deletions

View File

@ -15,6 +15,7 @@
Fix issue that caused 'dump()' not to report variables/functions on Pixl.js
Add E.lookupNoCase to allow searching case-insensitively for Object keys
Fix HTTP Chunked transfers when the server uses lowercase headers (fix #1458)
Fix TypedArray.indexOf (fix #1468)
1v99 : Increase jslMatch error buffer size to handle "UNFINISHED TEMPLATE LITERAL" string (#1426)
nRF5x: Make FlashWrite cope with flash writes > 4k

View File

@ -3025,27 +3025,28 @@ void jsvGetArrayItems(JsVar *arr, unsigned int itemCount, JsVar **itemPtr) {
itemPtr[i++] = 0; // just ensure we don't end up with bad data
}
/// Get the index of the value in the array (matchExact==use pointer not equality check, matchIntegerIndices = don't check non-integers)
/// Get the index of the value in the iterable var (matchExact==use pointer not equality check, matchIntegerIndices = don't check non-integers)
JsVar *jsvGetIndexOfFull(JsVar *arr, JsVar *value, bool matchExact, bool matchIntegerIndices, int startIdx) {
JsVarRef indexref;
assert(jsvIsArray(arr) || jsvIsObject(arr));
indexref = jsvGetFirstChild(arr);
while (indexref) {
JsVar *childIndex = jsvLock(indexref);
if (!jsvIsIterable(arr)) return 0;
JsvIterator it;
jsvIteratorNew(&it, arr, JSIF_DEFINED_ARRAY_ElEMENTS);
while (jsvIteratorHasElement(&it)) {
JsVar *childIndex = jsvIteratorGetKey(&it);
if (!matchIntegerIndices ||
(jsvIsInt(childIndex) && jsvGetInteger(childIndex)>=startIdx)) {
assert(jsvIsName(childIndex));
JsVar *childValue = jsvSkipName(childIndex);
JsVar *childValue = jsvIteratorGetValue(&it);
if (childValue==value ||
(!matchExact && jsvMathsOpTypeEqual(childValue, value))) {
jsvUnLock(childValue);
jsvIteratorFree(&it);
return childIndex;
}
jsvUnLock(childValue);
}
indexref = jsvGetNextSibling(childIndex);
jsvUnLock(childIndex);
jsvIteratorNext(&it);
}
jsvIteratorFree(&it);
return 0; // undefined
}

View File

@ -107,9 +107,7 @@ JsVar *jswrap_array_indexOf(JsVar *parent, JsVar *value, JsVarInt startIdx) {
JsVar *idxName = jsvGetIndexOfFull(parent, value, false/*not exact*/, true/*integer indices only*/, startIdx);
// but this is the name - we must turn it into a var
if (idxName == 0) return jsvNewFromInteger(-1); // not found!
JsVar *idx = jsvCopyNameOnly(idxName, false/* no children */, false/* Make sure this is not a name*/);
jsvUnLock(idxName);
return idx;
return jsvNewFromInteger(jsvGetIntegerAndUnLock(idxName));
}
/*JSON{

View File

@ -0,0 +1,10 @@
// https://github.com/espruino/Espruino/issues/1468
var b = new ArrayBuffer(8);
var arr=new Array(8);
var u8 = new Uint8Array(b);
arr.fill(128,3,6);
u8.fill(128,3,6);
console.log(arr.indexOf(128));//return 3
console.log(u8,r=u8.indexOf(128));//return -1
result = r == 3;