Skip to content
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

update util.getCallSites() implementation to follow node.js #3381

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/node/internal/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ export function isBoxedPrimitive(
): value is number | string | boolean | bigint | symbol;

export function getBuiltinModule(id: string): any;
export function getCallSite(frames: number): Record<string, string>[];
export function getCallSites(frames?: number): Record<string, string>[];
export function processExitImpl(code: number): void;
export const processPlatform: string;
10 changes: 7 additions & 3 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,12 @@ export function deprecate(
return fn;
}

export function getCallSite(frames: number = 10) {
return utilImpl.getCallSite(frames);
}
// Node.js originally introduced the API with the name `getCallSite()` as an experimental
// API but then renamed it to `getCallSites()` soon after. We had already implemented the
// API with the original name in a release. To avoid the possibility of breaking, we export
// the function using both names.
export const getCallSite = utilImpl.getCallSites.bind(utilImpl);
export const getCallSites = utilImpl.getCallSites.bind(utilImpl);

export function isDeepStrictEqual(a: unknown, b: unknown): boolean {
return _isDeepStrictEqual(a, b);
Expand Down Expand Up @@ -247,6 +250,7 @@ export default {
transferableAbortController,
transferableAbortSignal,
getCallSite,
getCallSites,
isDeepStrictEqual,
isArray,
};
Expand Down
12 changes: 10 additions & 2 deletions src/workerd/api/node/tests/util-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4343,15 +4343,23 @@ export const debuglog = {
},
};

export const getCallSiteTest = {
export const getCallSitesTest = {
test() {
const callSites = util.getCallSite();
const callSites = util.getCallSites();
assert.strictEqual(callSites.length, 1);
const [stack] = callSites;
assert.strictEqual(stack.functionName, 'test');
assert.strictEqual(stack.scriptName, 'worker');
assert.strictEqual(typeof stack.lineNumber, 'number');
assert.strictEqual(typeof stack.columnNumber, 'number');
assert.strictEqual(typeof stack.column, 'number');

// We leave this implementation for compat reasons.
// Node.js originally introduced the API with the name `getCallSite()` as an experimental
// API but then renamed it to `getCallSites()` soon after. We had already implemented the
// API with the original name in a release. To avoid the possibility of breaking, we export
// the function using both names.
assert.strictEqual(typeof util.getCallSite, 'function');
},
};

Expand Down
19 changes: 13 additions & 6 deletions src/workerd/api/node/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,29 @@ jsg::Name UtilModule::getResourceTypeInspect(jsg::Lock& js) {
return js.newApiSymbol("kResourceTypeInspect"_kj);
}

kj::Array<UtilModule::CallSiteEntry> UtilModule::getCallSite(jsg::Lock& js, int frames) {
JSG_REQUIRE(
frames >= 1 && frames <= 200, Error, "Frame count should be between 1 and 200 inclusive."_kj);
auto stack = v8::StackTrace::CurrentStackTrace(js.v8Isolate, frames + 1);
kj::Array<UtilModule::CallSiteEntry> UtilModule::getCallSites(
jsg::Lock& js, jsg::Optional<int> frames) {
KJ_IF_SOME(f, frames) {
JSG_REQUIRE(f >= 1 && f <= 200, Error, "Frame count should be between 1 and 200 inclusive."_kj);
}

auto stack = v8::StackTrace::CurrentStackTrace(js.v8Isolate, frames.orDefault(10) + 1);
const int frameCount = stack->GetFrameCount();
auto objects = kj::Vector<CallSiteEntry>();
objects.reserve(frameCount - 1);

// Frame 0 is node:util. It should be skipped.
for (int i = 1; i < frameCount; ++i) {
for (int i = 0; i < frameCount; ++i) {
auto stack_frame = stack->GetFrame(js.v8Isolate, i);

objects.add(CallSiteEntry{
.functionName = js.toString(stack_frame->GetFunctionName()),
.scriptName = js.toString(stack_frame->GetScriptName()),
.lineNumber = stack_frame->GetLineNumber(),
// Node.js originally implemented the experimental API using the "column" field
// then later renamed it to columnNumber. We had already implemented the API
// using column. To ensure backwards compat without the complexity of a compat
// flag, we just export both.
.columnNumber = stack_frame->GetColumn(),
.column = stack_frame->GetColumn(),
});
}
Expand Down
11 changes: 8 additions & 3 deletions src/workerd/api/node/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,16 @@ class UtilModule final: public jsg::Object {
kj::String functionName;
kj::String scriptName;
int lineNumber;
// Node.js originally introduced the API with the name `getCallSite()` as an experimental
// API but then renamed it to `getCallSites()` soon after. We had already implemented the
// API with the original name in a release. To avoid the possibility of breaking, we export
// the function using both names.
int columnNumber;
int column;

JSG_STRUCT(functionName, scriptName, lineNumber, column);
JSG_STRUCT(functionName, scriptName, lineNumber, columnNumber, column);
};
kj::Array<CallSiteEntry> getCallSite(jsg::Lock& js, int frames);
kj::Array<CallSiteEntry> getCallSites(jsg::Lock& js, jsg::Optional<int> frames);

#define V(Type) bool is##Type(jsg::JsValue value);
JS_UTIL_IS_TYPES(V)
Expand Down Expand Up @@ -257,7 +262,7 @@ class UtilModule final: public jsg::Object {
JSG_METHOD(getProxyDetails);
JSG_METHOD(previewEntries);
JSG_METHOD(getConstructorName);
JSG_METHOD(getCallSite);
JSG_METHOD(getCallSites);

#define V(Type) JSG_METHOD(is##Type);
JS_UTIL_IS_TYPES(V)
Expand Down
Loading