Build environment: macOS 26.5.2
Moddable SDK version:
- Installed via jsvu, so there is no SDK source tree /
git describe.
- Engine reports:
XS 17.9.3, slot 32 bytes, ID 4 bytes
Target device: desktop (host xs)
Description
String.prototype.indexOf reports no match when the searchString argument is omitted. The same behavior occurs with String.prototype.lastIndexOf, String.prototype.includes, String.prototype.startsWith, and String.prototype.endsWith.
An omitted argument supplies undefined. Each of these methods begins with Let searchStr be ? ToString(searchString), and ToString converts undefined to the String "undefined", so they should search for the six-character string undefined and behave as if undefined were passed explicitly.
Steps to Reproduce
$ xst -e "print(String('undefined'.indexOf()))"
-1
All five, on the receiver "undefined":
$ xst -e "for (const f of ['indexOf','lastIndexOf','includes','startsWith','endsWith']) print(f + ': ' + String('undefined'[f]()));"
indexOf: -1
lastIndexOf: -1
includes: false
startsWith: false
endsWith: false
Expected behavior
0, 0, true, true, true.
For comparison, V8 (15.6.20):
$ v8 -e "for (const f of ['indexOf','lastIndexOf','includes','startsWith','endsWith']) print(f + ': ' + String('undefined'[f]()));"
indexOf: 0
lastIndexOf: 0
includes: true
startsWith: true
endsWith: true
Other information
XS already produces the expected result when undefined is passed explicitly:
$ xst -e "for (const f of ['indexOf','lastIndexOf','includes','startsWith','endsWith']) print(f + ': ' + String('undefined'[f](undefined)));"
indexOf: 0
lastIndexOf: 0
includes: true
startsWith: true
endsWith: true
Neighbouring String.prototype methods that also apply ToString to an optional argument are unaffected: search, split, replace, concat, at, padStart, and localeCompare all behave the same whether the argument is omitted or explicitly undefined.
Build environment: macOS 26.5.2
Moddable SDK version:
git describe.XS 17.9.3, slot 32 bytes, ID 4 bytesTarget device: desktop (host
xs)Description
String.prototype.indexOfreports no match when thesearchStringargument is omitted. The same behavior occurs withString.prototype.lastIndexOf,String.prototype.includes,String.prototype.startsWith, andString.prototype.endsWith.An omitted argument supplies
undefined. Each of these methods begins withLet searchStr be ? ToString(searchString), andToStringconvertsundefinedto the String"undefined", so they should search for the six-character stringundefinedand behave as ifundefinedwere passed explicitly.Steps to Reproduce
$ xst -e "print(String('undefined'.indexOf()))" -1All five, on the receiver
"undefined":Expected behavior
0,0,true,true,true.For comparison, V8 (15.6.20):
Other information
XS already produces the expected result when
undefinedis passed explicitly:Neighbouring
String.prototypemethods that also applyToStringto an optional argument are unaffected:search,split,replace,concat,at,padStart, andlocaleCompareall behave the same whether the argument is omitted or explicitlyundefined.