Skip to content

Commit f3943bf

Browse files
authored
fix(ext/node): use primordials in ext/node/polyfills/internal/idna.ts (#29085)
Towards #24236. This PR replaces JS built-ins, `String` and `Array`, with the primordial versions.
1 parent cf8b977 commit f3943bf

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

ext/node/polyfills/internal/idna.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343

4444
// Adapted from https://github.com/mathiasbynens/punycode.js
4545

46-
// TODO(petamoriken): enable prefer-primordials for node polyfills
47-
// deno-lint-ignore-file prefer-primordials
48-
4946
// TODO(cmorten): migrate punycode logic to "icu" internal binding and/or "url"
5047
// internal module so there can be re-use within the "url" module etc.
5148

@@ -55,6 +52,13 @@ import {
5552
op_node_idna_domain_to_ascii,
5653
op_node_idna_domain_to_unicode,
5754
} from "ext:core/ops";
55+
import { primordials } from "ext:core/mod.js";
56+
const {
57+
ArrayPrototypePush,
58+
SafeArrayIterator,
59+
StringFromCodePoint,
60+
StringPrototypeCharCodeAt,
61+
} = primordials;
5862

5963
/**
6064
* Creates an array containing the numeric code points of each Unicode
@@ -72,22 +76,25 @@ function ucs2decode(str: string) {
7276
const length = str.length;
7377

7478
while (counter < length) {
75-
const value = str.charCodeAt(counter++);
79+
const value = StringPrototypeCharCodeAt(str, counter++);
7680

7781
if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
7882
// It's a high surrogate, and there is a next character.
79-
const extra = str.charCodeAt(counter++);
83+
const extra = StringPrototypeCharCodeAt(str, counter++);
8084

8185
if ((extra & 0xFC00) == 0xDC00) { // Low surrogate.
82-
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
86+
ArrayPrototypePush(
87+
output,
88+
((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000,
89+
);
8390
} else {
8491
// It's an unmatched surrogate; only append this code unit, in case the
8592
// next code unit is the high surrogate of a surrogate pair.
86-
output.push(value);
93+
ArrayPrototypePush(output, value);
8794
counter--;
8895
}
8996
} else {
90-
output.push(value);
97+
ArrayPrototypePush(output, value);
9198
}
9299
}
93100

@@ -103,7 +110,7 @@ function ucs2decode(str: string) {
103110
* @returns The new Unicode string (UCS-2).
104111
*/
105112
function ucs2encode(array: number[]) {
106-
return String.fromCodePoint(...array);
113+
return StringFromCodePoint(...new SafeArrayIterator(array));
107114
}
108115

109116
export const ucs2 = {

0 commit comments

Comments
 (0)