Skip to content

fix(ext/node): use primordials in ext/node/polyfills/internal_binding/util.ts #30014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions ext/node/polyfills/internal_binding/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@
// - https://github.com/nodejs/node/blob/master/src/util.cc
// - https://github.com/nodejs/node/blob/master/src/util.h

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials

import {
op_node_guess_handle_type,
op_node_view_has_buffer,
} from "ext:core/ops";
import { primordials } from "ext:core/mod.js";
const {
ArrayIsArray,
ArrayBufferIsView,
ArrayPrototypeFilter,
ArrayPrototypePush,
ObjectGetOwnPropertyDescriptor,
ReflectOwnKeys,
StringPrototypeCharCodeAt,
SymbolFor,
} = primordials;

const handleTypes = ["TCP", "TTY", "UDP", "FILE", "PIPE", "UNKNOWN"];
export function guessHandleType(fd: number): string {
Expand Down Expand Up @@ -74,7 +82,7 @@ export function isArrayIndex(value: unknown): value is number | string {
let ch = 0;
let i = 0;
for (; i < length; ++i) {
ch = value.charCodeAt(i);
ch = StringPrototypeCharCodeAt(value, i);
if (
i === 0 && ch === 0x30 && length > 1 /* must not start with 0 */ ||
ch < 0x30 /* 0 */ || ch > 0x39 /* 9 */
Expand All @@ -93,22 +101,23 @@ export function getOwnNonIndexProperties(
obj: object,
filter: number,
): (string | symbol)[] {
let allProperties = [
...Object.getOwnPropertyNames(obj),
...Object.getOwnPropertySymbols(obj),
];
let allProperties = ReflectOwnKeys(obj);

if (Array.isArray(obj) || ArrayBuffer.isView(obj)) {
allProperties = allProperties.filter((k) => !isArrayIndex(k));
if (ArrayIsArray(obj) || ArrayBufferIsView(obj)) {
allProperties = ArrayPrototypeFilter(
allProperties,
(k) => !isArrayIndex(k),
);
}

if (filter === ALL_PROPERTIES) {
return allProperties;
}

const result: (string | symbol)[] = [];
for (const key of allProperties) {
const desc = Object.getOwnPropertyDescriptor(obj, key);
for (let i = 0; i < allProperties.length; ++i) {
const key = allProperties[i];
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
if (desc === undefined) {
continue;
}
Expand All @@ -127,7 +136,7 @@ export function getOwnNonIndexProperties(
if (filter & SKIP_SYMBOLS && typeof key === "symbol") {
continue;
}
result.push(key);
ArrayPrototypePush(result, key);
}
return result;
}
Expand All @@ -138,6 +147,6 @@ export function arrayBufferViewHasBuffer(
return op_node_view_has_buffer(view);
}

export const untransferableSymbol = Symbol.for(
export const untransferableSymbol = SymbolFor(
"nodejs.worker_threads.untransferable",
);
Loading