Skip to content

Commit

Permalink
implement Array.includes to align to specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nename0 authored and gbrail committed Jun 28, 2019
1 parent ea80b70 commit 300d6df
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
55 changes: 54 additions & 1 deletion src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
return js_lastIndexOf(cx, scope, thisObj, args);

case Id_includes:
return Boolean.valueOf(((long) js_indexOf(cx, scope, thisObj, args)) > -1);
return js_includes(cx, scope, thisObj, args);

case Id_fill:
return js_fill(cx, scope, thisObj, args);
Expand Down Expand Up @@ -1711,6 +1711,59 @@ else if (start < 0)
return NEGATIVE_ONE;
}

/*
See ECMA-262 22.1.3.13
*/
private static Boolean js_includes(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object compareTo = args.length > 0 ? args[0] : Undefined.instance;

Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj);
long len = ScriptRuntime.toLength(new Object[]{getProperty(thisObj, "length")}, 0);
if (len == 0) return Boolean.FALSE;

long k;
if (args.length < 2) {
k = 0;
} else {
k = (long) ScriptRuntime.toInteger(args[1]);
if (k < 0) {
k += len;
if (k < 0)
k = 0;
}
if (k > len - 1) return Boolean.FALSE;
}
if (o instanceof NativeArray) {
NativeArray na = (NativeArray) o;
if (na.denseOnly) {
Scriptable proto = na.getPrototype();
for (int i = (int) k; i < len; i++) {
Object elementK = na.dense[i];
if (elementK == NOT_FOUND && proto != null) {
elementK = ScriptableObject.getProperty(proto, i);
}
if (elementK == NOT_FOUND) {
elementK = Undefined.instance;
}
if (ScriptRuntime.sameZero(elementK, compareTo)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
}
for (; k < len; k++) {
Object elementK = getRawElem(o, k);
if (elementK == NOT_FOUND) {
elementK = Undefined.instance;
}
if (ScriptRuntime.sameZero(elementK, compareTo)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}

private static Object js_fill(Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
{
Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj);
Expand Down
16 changes: 0 additions & 16 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,6 @@ built-ins/Array
! prototype/Symbol.unscopables/value.js
# Not throwing properly on unwritable
! prototype/copyWithin/return-abrupt-from-delete-target.js
# No "valueOf" yet.
! prototype/includes/fromIndex-equal-or-greater-length-returns-false.js
! prototype/includes/fromIndex-infinity.js
! prototype/includes/fromIndex-minus-zero.js
! prototype/includes/get-prop.js
! prototype/includes/length-boundaries.js
! prototype/includes/length-zero-returns-false.js
! prototype/includes/no-arg.js
! prototype/includes/samevaluezero.js
! prototype/includes/search-found-returns-true.js
! prototype/includes/search-not-found-returns-false.js
! prototype/includes/sparse.js
! prototype/includes/tointeger-fromindex.js
! prototype/includes/tolength-length.js
! prototype/includes/using-fromindex.js
! prototype/includes/values-are-not-cached.js

# Expects a particular string value
! prototype/Symbol.iterator.js
Expand Down

0 comments on commit 300d6df

Please sign in to comment.