Skip to content

XS: String search methods ignore an omitted search argument #1708

Description

@d01c2

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions