string-substring.tq 1.06 KB
Newer Older
1 2 3 4
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
namespace string {
6 7 8

  // ES6 #sec-string.prototype.substring
  transitioning javascript builtin StringPrototypeSubstring(
9 10
      js-implicit context: NativeContext,
      receiver: JSAny)(...arguments): String {
11 12
    // Check that {receiver} is coercible to Object and convert it to a String.
    const string: String = ToThisString(receiver, 'String.prototype.substring');
13
    const length: uintptr = string.length_uintptr;
14 15

    // Conversion and bounds-checks for {start}.
16 17 18
    const arg0 = arguments[0];
    let start: uintptr =
        arg0 != Undefined ? ClampToIndexRange(arg0, length) : 0;
19 20

    // Conversion and bounds-checks for {end}.
21 22 23
    const arg1 = arguments[1];
    let end: uintptr =
        arg1 != Undefined ? ClampToIndexRange(arg1, length) : length;
24
    if (end < start) {
25
      const tmp: uintptr = end;
26 27 28
      end = start;
      start = tmp;
    }
29
    return SubString(string, start, end);
30 31
  }
}