Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 1 addition & 28 deletions src/js/internal/repl/node-shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePush,

Check failure on line 11 in src/js/internal/repl/node-shims.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

eslint(no-unused-vars)

Variable 'ArrayPrototypePush' is declared but never used. Unused variables should start with a '_'.
ArrayPrototypeSlice,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
Expand Down Expand Up @@ -299,38 +299,11 @@
// ---- internalBinding('util') ----------------------------------------------

const ALL_PROPERTIES = 0;
const ONLY_ENUMERABLE = 2;

Check failure on line 302 in src/js/internal/repl/node-shims.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

eslint(no-unused-vars)

Variable 'ONLY_ENUMERABLE' is declared but never used. Unused variables should start with a '_'.
const SKIP_STRINGS = 8;

Check failure on line 303 in src/js/internal/repl/node-shims.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

eslint(no-unused-vars)

Variable 'SKIP_STRINGS' is declared but never used. Unused variables should start with a '_'.
const SKIP_SYMBOLS = 16;

function getOwnNonIndexProperties(obj, filter = ALL_PROPERTIES) {
const indexRegex = /^(0|[1-9][0-9]*)$/;
const keys = [];
if (!(filter & SKIP_STRINGS)) {
const names = Object.getOwnPropertyNames(obj);
for (let i = 0; i < names.length; i++) {
const key = names[i];
if (RegExpPrototypeExec(indexRegex, key) !== null) continue;
if (filter & ONLY_ENUMERABLE) {
const desc = Object.getOwnPropertyDescriptor(obj, key);
if (!desc?.enumerable) continue;
}
ArrayPrototypePush(keys, key);
}
}
if (!(filter & SKIP_SYMBOLS)) {
const syms = Object.getOwnPropertySymbols(obj);
for (let i = 0; i < syms.length; i++) {
const sym = syms[i];
if (filter & ONLY_ENUMERABLE) {
const desc = Object.getOwnPropertyDescriptor(obj, sym);
if (!desc?.enumerable) continue;
}
ArrayPrototypePush(keys, sym);
}
}
return keys;
}
const getOwnNonIndexProperties = $newCppFunction("UtilInspect.cpp", "jsFunctionGetOwnNonIndexProperties", 2);
Comment thread
robobun marked this conversation as resolved.
Resolved

// ---- process.addUncaughtExceptionCaptureCallback polyfill ----------------
// Bun only implements the single-callback set/clear API; emulate Node's
Expand Down
18 changes: 1 addition & 17 deletions src/js/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@
const NumberPrototypeValueOf = uncurryThis(Number.prototype.valueOf);
const ObjectAssign = Object.assign;
const ObjectDefineProperty = Object.defineProperty;
const ObjectEntries = Object.entries;

Check failure on line 87 in src/js/internal/util/inspect.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

eslint(no-unused-vars)

Variable 'ObjectEntries' is declared but never used. Unused variables should start with a '_'.
const ObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
const ObjectGetOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;

Check failure on line 89 in src/js/internal/util/inspect.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

eslint(no-unused-vars)

Variable 'ObjectGetOwnPropertyDescriptors' is declared but never used. Unused variables should start with a '_'.
const ObjectGetOwnPropertyNames = Object.getOwnPropertyNames;
const ObjectGetOwnPropertySymbols = Object.getOwnPropertySymbols;
const ObjectGetPrototypeOf = Object.getPrototypeOf;
Expand Down Expand Up @@ -2933,23 +2933,7 @@
}

// utils
function getOwnNonIndexProperties(a, filter = ONLY_ENUMERABLE) {
const desc = ObjectGetOwnPropertyDescriptors(a);
const ret = [];
for (const [k, v] of ObjectEntries(desc)) {
if (!RegExpPrototypeTest(/^(0|[1-9][0-9]*)$/, k) || NumberParseInt(k, 10) >= 2 ** 32 - 1) {
// Arrays are limited in size
if (filter === ONLY_ENUMERABLE && !v.enumerable) continue;
else ArrayPrototypePush.$call(ret, k);
}
}
for (const s of ObjectGetOwnPropertySymbols(a)) {
const v = ObjectGetOwnPropertyDescriptor(a, s);
if (filter === ONLY_ENUMERABLE && !v.enumerable) continue;
ArrayPrototypePush.$call(ret, s);
}
return ret;
}
const getOwnNonIndexProperties = $newCppFunction("UtilInspect.cpp", "jsFunctionGetOwnNonIndexProperties", 2);

Check warning on line 2936 in src/js/internal/util/inspect.js

View check run for this annotation

Claude / Claude Code Review

Dead primordial captures left in inspect.js

Same dead-code cleanup applies here: deleting the JS `getOwnNonIndexProperties` body left `ObjectEntries` (L87) and `ObjectGetOwnPropertyDescriptors` (L89) with no remaining reads in this file. Per REVIEW.md ("delete dead code in the same PR that makes it dead … helpers whose last caller you rewired"), drop those two lines. (This is the inspect.js sibling of the node-shims.js finding — different file, different identifiers.)
Comment thread
robobun marked this conversation as resolved.
Resolved
function getPromiseDetails(promise) {
const state = $peekPromiseStatus(promise);
if (state !== 0) {
Expand Down
54 changes: 54 additions & 0 deletions src/jsc/bindings/UtilInspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "JavaScriptCore/JSGlobalObject.h"
#include "ZigGlobalObject.h"
#include "JavaScriptCore/ObjectConstructor.h"
#include "JavaScriptCore/PropertyNameArray.h"
#include "JavaScriptCore/JSArray.h"

namespace Bun {

Expand Down Expand Up @@ -65,4 +67,56 @@ extern "C" JSC::EncodedJSValue JSC__JSValue__callCustomInspectFunction(
RELEASE_AND_RETURN(scope, JSValue::encode(inspectRet));
}

// Port of V8's `internalBinding('util').getOwnNonIndexProperties(object, filter)` used by
// util.inspect and the REPL completer: own keys minus array indices, without materializing
// a name (or descriptor) per element the way Object.getOwnPropertyNames() on an array would.
// `filter` is Node's PropertyFilter bitmask: ONLY_ENUMERABLE = 2, SKIP_STRINGS = 8, SKIP_SYMBOLS = 16.
JSC_DEFINE_HOST_FUNCTION(jsFunctionGetOwnNonIndexProperties, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);

JSObject* object = callFrame->argument(0).toObject(globalObject);
RETURN_IF_EXCEPTION(scope, {});
int32_t filter = callFrame->argument(1).toInt32(globalObject);
RETURN_IF_EXCEPTION(scope, {});
constexpr int32_t ONLY_ENUMERABLE = 1 << 1;
constexpr int32_t SKIP_STRINGS = 1 << 3;
constexpr int32_t SKIP_SYMBOLS = 1 << 4;

PropertyNameMode propertyNameMode = PropertyNameMode::StringsAndSymbols;
if (filter & SKIP_STRINGS)
propertyNameMode = PropertyNameMode::Symbols;
else if (filter & SKIP_SYMBOLS)
propertyNameMode = PropertyNameMode::Strings;
DontEnumPropertiesMode dontEnumMode = (filter & ONLY_ENUMERABLE) ? DontEnumPropertiesMode::Exclude : DontEnumPropertiesMode::Include;

PropertyNameArrayBuilder propertyNames(vm, propertyNameMode, PrivateSymbolMode::Exclude);
// Arrays and typed arrays only add their indices on top of the ordinary path, so skip
// straight to the non-index part. Anything else that customizes [[OwnPropertyKeys]]
// (Proxy, module namespace, String objects, arguments, ...) goes through the method table
// and has index-looking keys filtered out below.
JSC::JSType type = object->type();
bool onlyAddsIndices = type == ArrayType || type == DerivedArrayType || isTypedArrayType(type)
|| object->methodTable()->getOwnPropertyNames == JSObject::getOwnPropertyNames;
if (onlyAddsIndices) {
object->getOwnNonIndexPropertyNames(globalObject, propertyNames, dontEnumMode);
} else {
object->methodTable()->getOwnPropertyNames(object, globalObject, propertyNames, dontEnumMode);
}
RETURN_IF_EXCEPTION(scope, {});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

MarkedArgumentBuffer keys;
for (auto& propertyName : propertyNames) {
if (propertyName.isSymbol()) {
keys.append(Symbol::create(vm, static_cast<SymbolImpl&>(*propertyName.impl())));
continue;
}
if (!onlyAddsIndices && parseIndex(propertyName))
continue;
keys.append(jsOwnedString(vm, propertyName.string()));
}
RELEASE_AND_RETURN(scope, JSValue::encode(constructArray(globalObject, static_cast<ArrayAllocationProfile*>(nullptr), keys)));
Comment thread
robobun marked this conversation as resolved.
Resolved
}

}
5 changes: 5 additions & 0 deletions src/jsc/bindings/UtilInspect.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once

#include "root.h"

namespace Bun {

JSC::Structure* createUtilInspectOptionsStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject);

// internalBinding('util').getOwnNonIndexProperties(object, filter)
JSC_DECLARE_HOST_FUNCTION(jsFunctionGetOwnNonIndexProperties);

}
Loading