Repository: denoland/deno
Pull request: fix(ext/node): use primordials in `ext/node/polyfills/path/_win32.ts`

Towards #24236

Changed files:
filename: ext/node/polyfills/path/_win32.ts
patch: @@ -2,9 +2,6 @@
 // Ported from https://github.com/browserify/path-browserify/
 // Copyright 2018-2025 the Deno authors. MIT license.
 
-// TODO(petamoriken): enable prefer-primordials for node polyfills
-// deno-lint-ignore-file prefer-primordials
-
 import type {
   FormatInputPathObject,
   ParsedPath,
@@ -25,6 +22,14 @@ import {
   normalizeString,
 } from "ext:deno_node/path/_util.ts";
 import { assert } from "ext:deno_node/_util/asserts.ts";
+import { primordials } from "ext:core/mod.js";
+
+const {
+  StringPrototypeCharCodeAt,
+  StringPrototypeToLowerCase,
+  StringPrototypeSlice,
+  TypeError,
+} = primordials;
 
 export const sep = "\\";
 export const delimiter = ";";
@@ -61,7 +66,8 @@ export function resolve(...pathSegments: string[]): string {
       // to our drive. If not, default to the drive's root.
       if (
         path === undefined ||
-        path.slice(0, 3).toLowerCase() !== `${resolvedDevice.toLowerCase()}\\`
+        StringPrototypeToLowerCase(StringPrototypeSlice(path, 0, 3)) !==
+          `${StringPrototypeToLowerCase(resolvedDevice)}\\`
       ) {
         path = `${resolvedDevice}\\`;
       }
@@ -77,7 +83,7 @@ export function resolve(...pathSegments: string[]): string {
     let rootEnd = 0;
     let device = "";
     let isAbsolute = false;
-    const code = path.charCodeAt(0);
+    const code = StringPrototypeCharCodeAt(path, 0);
 
     // Try to match a root
     if (len > 1) {
@@ -88,37 +94,41 @@ export function resolve(...pathSegments: string[]): string {
         // absolute path of some kind (UNC or otherwise)
         isAbsolute = true;
 
-        if (isPathSeparator(path.charCodeAt(1))) {
+        if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) {
           // Matched double path separator at beginning
           let j = 2;
           let last = j;
           // Match 1 or more non-path separators
           for (; j < len; ++j) {
-            if (isPathSeparator(path.charCodeAt(j))) break;
+            if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
           }
           if (j < len && j !== last) {
-            const firstPart = path.slice(last, j);
+            const firstPart = StringPrototypeSlice(path, last, j);
             // Matched!
             last = j;
             // Match 1 or more path separators
             for (; j < len; ++j) {
-              if (!isPathSeparator(path.charCodeAt(j))) break;
+              if (!isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
             }
             if (j < len && j !== last) {
               // Matched!
               last = j;
               // Match 1 or more non-path separators
               for (; j < len; ++j) {
-                if (isPathSeparator(path.charCodeAt(j))) break;
+                if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
               }
               if (j === len) {
                 // We matched a UNC root only
-                device = `\\\\${firstPart}\\${path.slice(last)}`;
+                device = `\\\\${firstPart}\\${
+                  StringPrototypeSlice(path, last)
+                }`;
                 rootEnd = j;
               } else if (j !== last) {
                 // We matched a UNC root with leftovers
 
-                device = `\\\\${firstPart}\\${path.slice(last, j)}`;
+                device = `\\\\${firstPart}\\${
+                  StringPrototypeSlice(path, last, j)
+                }`;
                 rootEnd = j;
               }
             }
@@ -129,11 +139,11 @@ export function resolve(...pathSegments: string[]): string {
       } else if (isWindowsDeviceRoot(code)) {
         // Possible device root
 
-        if (path.charCodeAt(1) === CHAR_COLON) {
-          device = path.slice(0, 2);
+        if (StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) {
+          device = StringPrototypeSlice(path, 0, 2);
           rootEnd = 2;
           if (len > 2) {
-            if (isPathSeparator(path.charCodeAt(2))) {
+            if (isPathSeparator(StringPrototypeCharCodeAt(path, 2))) {
               // Treat separator following drive name as an absolute path
               // indicator
               isAbsolute = true;
@@ -151,7 +161,8 @@ export function resolve(...pathSegments: string[]): string {
     if (
       device.length > 0 &&
       resolvedDevice.length > 0 &&
-      device.toLowerCase() !== resolvedDevice.toLowerCase()
+      StringPrototypeToLowerCase(device) !==
+        StringPrototypeToLowerCase(resolvedDevice)
     ) {
       // This path points to another device so it is not applicable
       continue;
@@ -161,7 +172,7 @@ export function resolve(...pathSegments: string[]): string {
       resolvedDevice = device;
     }
     if (!resolvedAbsolute) {
-      resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`;
+      resolvedTail = `${StringPrototypeSlice(path, rootEnd)}\\${resolvedTail}`;
       resolvedAbsolute = isAbsolute;
     }
 
@@ -194,7 +205,7 @@ export function normalize(path: string): string {
   let rootEnd = 0;
   let device: string | undefined;
   let isAbsolute = false;
-  const code = path.charCodeAt(0);
+  const code = StringPrototypeCharCodeAt(path, 0);
 
   // Try to match a root
   if (len > 1) {
@@ -205,39 +216,41 @@ export function normalize(path: string): string {
       // path of some kind (UNC or otherwise)
       isAbsolute = true;
 
-      if (isPathSeparator(path.charCodeAt(1))) {
+      if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) {
         // Matched double path separator at beginning
         let j = 2;
         let last = j;
         // Match 1 or more non-path separators
         for (; j < len; ++j) {
-          if (isPathSeparator(path.charCodeAt(j))) break;
+          if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
         }
         if (j < len && j !== last) {
-          const firstPart = path.slice(last, j);
+          const firstPart = StringPrototypeSlice(path, last, j);
           // Matched!
           last = j;
           // Match 1 or more path separators
           for (; j < len; ++j) {
-            if (!isPathSeparator(path.charCodeAt(j))) break;
+            if (!isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
           }
           if (j < len && j !== last) {
             // Matched!
             last = j;
             // Match 1 or more non-path separators
             for (; j < len; ++j) {
-              if (isPathSeparator(path.charCodeAt(j))) break;
+              if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
             }
             if (j === len) {
               // We matched a UNC root only
               // Return the normalized version of the UNC root since there
               // is nothing left to process
 
-              return `\\\\${firstPart}\\${path.slice(last)}\\`;
+              return `\\\\${firstPart}\\${StringPrototypeSlice(path, last)}\\`;
             } else if (j !== last) {
               // We matched a UNC root with leftovers
 
-              device = `\\\\${firstPart}\\${path.slice(last, j)}`;
+              device = `\\\\${firstPart}\\${
+                StringPrototypeSlice(path, last, j)
+              }`;
               rootEnd = j;
             }
           }
@@ -248,11 +261,11 @@ export function normalize(path: string): string {
     } else if (isWindowsDeviceRoot(code)) {
       // Possible device root
 
-      if (path.charCodeAt(1) === CHAR_COLON) {
-        device = path.slice(0, 2);
+      if (StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) {
+        device = StringPrototypeSlice(path, 0, 2);
         rootEnd = 2;
         if (len > 2) {
-          if (isPathSeparator(path.charCodeAt(2))) {
+          if (isPathSeparator(StringPrototypeCharCodeAt(path, 2))) {
             // Treat separator following drive name as an absolute path
             // indicator
             isAbsolute = true;
@@ -270,7 +283,7 @@ export function normalize(path: string): string {
   let tail: string;
   if (rootEnd < len) {
     tail = normalizeString(
-      path.slice(rootEnd),
+      StringPrototypeSlice(path, rootEnd),
       !isAbsolute,
       "\\",
       isPathSeparator,
@@ -279,7 +292,9 @@ export function normalize(path: string): string {
     tail = "";
   }
   if (tail.length === 0 && !isAbsolute) tail = ".";
-  if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) {
+  if (
+    tail.length > 0 && isPathSeparator(StringPrototypeCharCodeAt(path, len - 1))
+  ) {
     tail += "\\";
   }
   if (device === undefined) {
@@ -310,14 +325,14 @@ export function isAbsolute(path: string): boolean {
   const len = path.length;
   if (len === 0) return false;
 
-  const code = path.charCodeAt(0);
+  const code = StringPrototypeCharCodeAt(path, 0);
   if (isPathSeparator(code)) {
     return true;
   } else if (isWindowsDeviceRoot(code)) {
     // Possible device root
 
-    if (len > 2 && path.charCodeAt(1) === CHAR_COLON) {
-      if (isPathSeparator(path.charCodeAt(2))) return true;
+    if (len > 2 && StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) {
+      if (isPathSeparator(StringPrototypeCharCodeAt(path, 2))) return true;
     }
   }
   return false;
@@ -360,15 +375,16 @@ export function join(...paths: string[]): string {
   let needsReplace = true;
   let slashCount = 0;
   assert(firstPart != null);
-  if (isPathSeparator(firstPart.charCodeAt(0))) {
+  if (isPathSeparator(StringPrototypeCharCodeAt(firstPart, 0))) {
     ++slashCount;
     const firstLen = firstPart.length;
     if (firstLen > 1) {
-      if (isPathSeparator(firstPart.charCodeAt(1))) {
+      if (isPathSeparator(StringPrototypeCharCodeAt(firstPart, 1))) {
         ++slashCount;
         if (firstLen > 2) {
-          if (isPathSeparator(firstPart.charCodeAt(2))) ++slashCount;
-          else {
+          if (isPathSeparator(StringPrototypeCharCodeAt(firstPart, 2))) {
+            ++slashCount;
+          } else {
             // We matched a UNC path in the first part
             needsReplace = false;
           }
@@ -379,11 +395,15 @@ export function join(...paths: string[]): string {
   if (needsReplace) {
     // Find any more consecutive slashes we need to replace
     for (; slashCount < joined.length; ++slashCount) {
-      if (!isPathSeparator(joined.charCodeAt(slashCount))) break;
+      if (!isPathSeparator(StringPrototypeCharCodeAt(joined, slashCount))) {
+        break;
+      }
     }
 
     // Replace the slashes if needed
-    if (slashCount >= 2) joined = `\\${joined.slice(slashCount)}`;
+    if (slashCount >= 2) {
+      joined = `\\${StringPrototypeSlice(joined, slashCount)}`;
+    }
   }
 
   return normalize(joined);
@@ -408,32 +428,36 @@ export function relative(from: string, to: string): string {
 
   if (fromOrig === toOrig) return "";
 
-  from = fromOrig.toLowerCase();
-  to = toOrig.toLowerCase();
+  from = StringPrototypeToLowerCase(fromOrig);
+  to = StringPrototypeToLowerCase(toOrig);
 
   if (from === to) return "";
 
   // Trim any leading backslashes
   let fromStart = 0;
   let fromEnd = from.length;
   for (; fromStart < fromEnd; ++fromStart) {
-    if (from.charCodeAt(fromStart) !== CHAR_BACKWARD_SLASH) break;
+    if (StringPrototypeCharCodeAt(from, fromStart) !== CHAR_BACKWARD_SLASH) {
+      break;
+    }
   }
   // Trim trailing backslashes (applicable to UNC paths only)
   for (; fromEnd - 1 > fromStart; --fromEnd) {
-    if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH) break;
+    if (StringPrototypeCharCodeAt(from, fromEnd - 1) !== CHAR_BACKWARD_SLASH) {
+      break;
+    }
   }
   const fromLen = fromEnd - fromStart;
 
   // Trim any leading backslashes
   let toStart = 0;
   let toEnd = to.length;
   for (; toStart < toEnd; ++toStart) {
-    if (to.charCodeAt(toStart) !== CHAR_BACKWARD_SLASH) break;
+    if (StringPrototypeCharCodeAt(to, toStart) !== CHAR_BACKWARD_SLASH) break;
   }
   // Trim trailing backslashes (applicable to UNC paths only)
   for (; toEnd - 1 > toStart; --toEnd) {
-    if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH) break;
+    if (StringPrototypeCharCodeAt(to, toEnd - 1) !== CHAR_BACKWARD_SLASH) break;
   }
   const toLen = toEnd - toStart;
 
@@ -444,18 +468,22 @@ export function relative(from: string, to: string): string {
   for (; i <= length; ++i) {
     if (i === length) {
       if (toLen > length) {
-        if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
+        if (
+          StringPrototypeCharCodeAt(to, toStart + i) === CHAR_BACKWARD_SLASH
+        ) {
           // We get here if `from` is the exact base path for `to`.
           // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
-          return toOrig.slice(toStart + i + 1);
+          return StringPrototypeSlice(toOrig, toStart + i + 1);
         } else if (i === 2) {
           // We get here if `from` is the device root.
           // For example: from='C:\\'; to='C:\\foo'
-          return toOrig.slice(toStart + i);
+          return StringPrototypeSlice(toOrig, toStart + i);
         }
       }
       if (fromLen > length) {
-        if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
+        if (
+          StringPrototypeCharCodeAt(from, fromStart + i) === CHAR_BACKWARD_SLASH
+        ) {
           // We get here if `to` is the exact base path for `from`.
           // For example: from='C:\\foo\\bar'; to='C:\\foo'
           lastCommonSep = i;
@@ -467,8 +495,8 @@ export function relative(from: string, to: string): string {
       }
       break;
     }
-    const fromCode = from.charCodeAt(fromStart + i);
-    const toCode = to.charCodeAt(toStart + i);
+    const fromCode = StringPrototypeCharCodeAt(from, fromStart + i);
+    const toCode = StringPrototypeCharCodeAt(to, toStart + i);
     if (fromCode !== toCode) break;
     else if (fromCode === CHAR_BACKWARD_SLASH) lastCommonSep = i;
   }
@@ -484,7 +512,10 @@ export function relative(from: string, to: string): string {
   // Generate the relative path based on the path difference between `to` and
   // `from`
   for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
-    if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
+    if (
+      i === fromEnd ||
+      StringPrototypeCharCodeAt(from, i) === CHAR_BACKWARD_SLASH
+    ) {
       if (out.length === 0) out += "..";
       else out += "\\..";
     }
@@ -493,11 +524,13 @@ export function relative(from: string, to: string): string {
   // Lastly, append the rest of the destination (`to`) path that comes after
   // the common path parts
   if (out.length > 0) {
-    return out + toOrig.slice(toStart + lastCommonSep, toEnd);
+    return out + StringPrototypeSlice(toOrig, toStart + lastCommonSep, toEnd);
   } else {
     toStart += lastCommonSep;
-    if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) ++toStart;
-    return toOrig.slice(toStart, toEnd);
+    if (StringPrototypeCharCodeAt(toOrig, toStart) === CHAR_BACKWARD_SLASH) {
+      ++toStart;
+    }
+    return StringPrototypeSlice(toOrig, toStart, toEnd);
   }
 }
 
@@ -513,22 +546,24 @@ export function toNamespacedPath(path: string): string {
   const resolvedPath = resolve(path);
 
   if (resolvedPath.length >= 3) {
-    if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
+    if (StringPrototypeCharCodeAt(resolvedPath, 0) === CHAR_BACKWARD_SLASH) {
       // Possible UNC root
 
-      if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
-        const code = resolvedPath.charCodeAt(2);
+      if (StringPrototypeCharCodeAt(resolvedPath, 1) === CHAR_BACKWARD_SLASH) {
+        const code = StringPrototypeCharCodeAt(resolvedPath, 2);
         if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
           // Matched non-long UNC root, convert the path to a long UNC path
-          return `\\\\?\\UNC\\${resolvedPath.slice(2)}`;
+          return `\\\\?\\UNC\\${StringPrototypeSlice(resolvedPath, 2)}`;
         }
       }
-    } else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0))) {
+    } else if (
+      isWindowsDeviceRoot(StringPrototypeCharCodeAt(resolvedPath, 0))
+    ) {
       // Possible device root
 
       if (
-        resolvedPath.charCodeAt(1) === CHAR_COLON &&
-        resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH
+        StringPrototypeCharCodeAt(resolvedPath, 1) === CHAR_COLON &&
+        StringPrototypeCharCodeAt(resolvedPath, 2) === CHAR_BACKWARD_SLASH
       ) {
         // Matched device root, convert the path to a long UNC path
         return `\\\\?\\${resolvedPath}`;
@@ -551,7 +586,7 @@ export function dirname(path: string): string {
   let end = -1;
   let matchedSlash = true;
   let offset = 0;
-  const code = path.charCodeAt(0);
+  const code = StringPrototypeCharCodeAt(path, 0);
 
   // Try to match a root
   if (len > 1) {
@@ -560,27 +595,27 @@ export function dirname(path: string): string {
 
       rootEnd = offset = 1;
 
-      if (isPathSeparator(path.charCodeAt(1))) {
+      if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) {
         // Matched double path separator at beginning
         let j = 2;
         let last = j;
         // Match 1 or more non-path separators
         for (; j < len; ++j) {
-          if (isPathSeparator(path.charCodeAt(j))) break;
+          if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
         }
         if (j < len && j !== last) {
           // Matched!
           last = j;
           // Match 1 or more path separators
           for (; j < len; ++j) {
-            if (!isPathSeparator(path.charCodeAt(j))) break;
+            if (!isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
           }
           if (j < len && j !== last) {
             // Matched!
             last = j;
             // Match 1 or more non-path separators
             for (; j < len; ++j) {
-              if (isPathSeparator(path.charCodeAt(j))) break;
+              if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
             }
             if (j === len) {
               // We matched a UNC root only
@@ -599,10 +634,12 @@ export function dirname(path: string): string {
     } else if (isWindowsDeviceRoot(code)) {
       // Possible device root
 
-      if (path.charCodeAt(1) === CHAR_COLON) {
+      if (StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) {
         rootEnd = offset = 2;
         if (len > 2) {
-          if (isPathSeparator(path.charCodeAt(2))) rootEnd = offset = 3;
+          if (isPathSeparator(StringPrototypeCharCodeAt(path, 2))) {
+            rootEnd = offset = 3;
+          }
         }
       }
     }
@@ -613,7 +650,7 @@ export function dirname(path: string): string {
   }
 
   for (let i = len - 1; i >= offset; --i) {
-    if (isPathSeparator(path.charCodeAt(i))) {
+    if (isPathSeparator(StringPrototypeCharCodeAt(path, i))) {
       if (!matchedSlash) {
         end = i;
         break;
@@ -628,7 +665,7 @@ export function dirname(path: string): string {
     if (rootEnd === -1) return ".";
     else end = rootEnd;
   }
-  return path.slice(0, end);
+  return StringPrototypeSlice(path, 0, end);
 }
 
 /**
@@ -652,9 +689,9 @@ export function basename(path: string, ext = ""): string {
   // path separator as an extra separator at the end of the path that can be
   // disregarded
   if (path.length >= 2) {
-    const drive = path.charCodeAt(0);
+    const drive = StringPrototypeCharCodeAt(path, 0);
     if (isWindowsDeviceRoot(drive)) {
-      if (path.charCodeAt(1) === CHAR_COLON) start = 2;
+      if (StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) start = 2;
     }
   }
 
@@ -663,7 +700,7 @@ export function basename(path: string, ext = ""): string {
     let extIdx = ext.length - 1;
     let firstNonSlashEnd = -1;
     for (i = path.length - 1; i >= start; --i) {
-      const code = path.charCodeAt(i);
+      const code = StringPrototypeCharCodeAt(path, i);
       if (isPathSeparator(code)) {
         // If we reached a path separator that was not part of a set of path
         // separators at the end of the string, stop now
@@ -680,7 +717,7 @@ export function basename(path: string, ext = ""): string {
         }
         if (extIdx >= 0) {
           // Try to match the explicit extension
-          if (code === ext.charCodeAt(extIdx)) {
+          if (code === StringPrototypeCharCodeAt(ext, extIdx)) {
             if (--extIdx === -1) {
               // We matched the extension, so mark this as the end of our path
               // component
@@ -698,10 +735,10 @@ export function basename(path: string, ext = ""): string {
 
     if (start === end) end = firstNonSlashEnd;
     else if (end === -1) end = path.length;
-    return path.slice(start, end);
+    return StringPrototypeSlice(path, start, end);
   } else {
     for (i = path.length - 1; i >= start; --i) {
-      if (isPathSeparator(path.charCodeAt(i))) {
+      if (isPathSeparator(StringPrototypeCharCodeAt(path, i))) {
         // If we reached a path separator that was not part of a set of path
         // separators at the end of the string, stop now
         if (!matchedSlash) {
@@ -717,7 +754,7 @@ export function basename(path: string, ext = ""): string {
     }
 
     if (end === -1) return "";
-    return path.slice(start, end);
+    return StringPrototypeSlice(path, start, end);
   }
 }
 
@@ -742,14 +779,14 @@ export function extname(path: string): string {
 
   if (
     path.length >= 2 &&
-    path.charCodeAt(1) === CHAR_COLON &&
-    isWindowsDeviceRoot(path.charCodeAt(0))
+    StringPrototypeCharCodeAt(path, 1) === CHAR_COLON &&
+    isWindowsDeviceRoot(StringPrototypeCharCodeAt(path, 0))
   ) {
     start = startPart = 2;
   }
 
   for (let i = path.length - 1; i >= start; --i) {
-    const code = path.charCodeAt(i);
+    const code = StringPrototypeCharCodeAt(path, i);
     if (isPathSeparator(code)) {
       // If we reached a path separator that was not part of a set of path
       // separators at the end of the string, stop now
@@ -786,7 +823,7 @@ export function extname(path: string): string {
   ) {
     return "";
   }
-  return path.slice(startDot, end);
+  return StringPrototypeSlice(path, startDot, end);
 }
 
 /**
@@ -813,35 +850,35 @@ export function parse(path: string): ParsedPath {
   if (len === 0) return ret;
 
   let rootEnd = 0;
-  let code = path.charCodeAt(0);
+  let code = StringPrototypeCharCodeAt(path, 0);
 
   // Try to match a root
   if (len > 1) {
     if (isPathSeparator(code)) {
       // Possible UNC root
 
       rootEnd = 1;
-      if (isPathSeparator(path.charCodeAt(1))) {
+      if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) {
         // Matched double path separator at beginning
         let j = 2;
         let last = j;
         // Match 1 or more non-path separators
         for (; j < len; ++j) {
-          if (isPathSeparator(path.charCodeAt(j))) break;
+          if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
         }
         if (j < len && j !== last) {
           // Matched!
           last = j;
           // Match 1 or more path separators
           for (; j < len; ++j) {
-            if (!isPathSeparator(path.charCodeAt(j))) break;
+            if (!isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
           }
           if (j < len && j !== last) {
             // Matched!
             last = j;
             // Match 1 or more non-path separators
             for (; j < len; ++j) {
-              if (isPathSeparator(path.charCodeAt(j))) break;
+              if (isPathSeparator(StringPrototypeCharCodeAt(path, j))) break;
             }
             if (j === len) {
               // We matched a UNC root only
@@ -858,10 +895,10 @@ export function parse(path: string): ParsedPath {
     } else if (isWindowsDeviceRoot(code)) {
       // Possible device root
 
-      if (path.charCodeAt(1) === CHAR_COLON) {
+      if (StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) {
         rootEnd = 2;
         if (len > 2) {
-          if (isPathSeparator(path.charCodeAt(2))) {
+          if (isPathSeparator(StringPrototypeCharCodeAt(path, 2))) {
             if (len === 3) {
               // `path` contains just a drive root, exit early to avoid
               // unnecessary work
@@ -885,7 +922,7 @@ export function parse(path: string): ParsedPath {
     return ret;
   }
 
-  if (rootEnd > 0) ret.root = path.slice(0, rootEnd);
+  if (rootEnd > 0) ret.root = StringPrototypeSlice(path, 0, rootEnd);
 
   let startDot = -1;
   let startPart = rootEnd;
@@ -899,7 +936,7 @@ export function parse(path: string): ParsedPath {
 
   // Get non-dir info
   for (; i >= rootEnd; --i) {
-    code = path.charCodeAt(i);
+    code = StringPrototypeCharCodeAt(path, i);
     if (isPathSeparator(code)) {
       // If we reached a path separator that was not part of a set of path
       // separators at the end of the string, stop now
@@ -935,19 +972,19 @@ export function parse(path: string): ParsedPath {
     (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
   ) {
     if (end !== -1) {
-      ret.base = ret.name = path.slice(startPart, end);
+      ret.base = ret.name = StringPrototypeSlice(path, startPart, end);
     }
   } else {
-    ret.name = path.slice(startPart, startDot);
-    ret.base = path.slice(startPart, end);
-    ret.ext = path.slice(startDot, end);
+    ret.name = StringPrototypeSlice(path, startPart, startDot);
+    ret.base = StringPrototypeSlice(path, startPart, end);
+    ret.ext = StringPrototypeSlice(path, startDot, end);
   }
 
   // If the directory is the root, use the entire root as the `dir` including
   // the trailing slash if any (`C:\abc` -> `C:\`). Otherwise, strip out the
   // trailing slash (`C:\abc\def` -> `C:\abc`).
   if (startPart > 0 && startPart !== rootEnd) {
-    ret.dir = path.slice(0, startPart - 1);
+    ret.dir = StringPrototypeSlice(path, 0, startPart - 1);
   } else ret.dir = ret.root;
 
   return ret;