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

Implement SolidityCallSite.toString() #6088

Merged
merged 1 commit into from
Jan 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export class SolidityError extends Error {
}
}

class SolidityCallSite implements NodeJS.CallSite {
export class SolidityCallSite implements NodeJS.CallSite {
readonly #sourceName: string | undefined;
readonly #contract: string | undefined;
readonly #functionName: string | undefined;
Expand All @@ -335,15 +335,15 @@ class SolidityCallSite implements NodeJS.CallSite {
return undefined;
}

public getFileName() {
public getFileName(): string {
return this.#sourceName ?? "unknown";
}

public getFunction() {
return undefined;
}

public getFunctionName() {
public getFunctionName(): string | null {
// if it's a top-level function, we print its name
if (this.#contract === undefined) {
return this.#functionName ?? null;
Expand All @@ -352,11 +352,11 @@ class SolidityCallSite implements NodeJS.CallSite {
return null;
}

public getLineNumber() {
public getLineNumber(): number | null {
return this.#line !== undefined ? this.#line : null;
}

public getMethodName() {
public getMethodName(): string | null {
if (this.#contract !== undefined) {
return this.#functionName ?? null;
}
Expand All @@ -380,7 +380,7 @@ class SolidityCallSite implements NodeJS.CallSite {
return undefined;
}

public getTypeName() {
public getTypeName(): string | null {
return this.#contract ?? null;
}

Expand Down Expand Up @@ -420,7 +420,41 @@ class SolidityCallSite implements NodeJS.CallSite {
return 0;
}

// Extracted and adapted from source-map-support package, which extracts it from V8
public toString(): string {
return "[SolidityCallSite]";
let fileLocation = this.getFileName();

if (this.getLineNumber() !== null) {
fileLocation += `:${this.getLineNumber()}`;
}

let line = "";
const functionName = this.getFunctionName();
let addSuffix = true;
const isConstructor = this.isConstructor();
const isMethodCall = !(this.isToplevel() || isConstructor);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked the definition of isToplevel(), and it always seems to return false. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these CallSite interface functions are returning fixed values, but probably that's temporary. Since I'm not really sure, I didn't exclude the logic from the toString() function, in case in the future those functions may be of use

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed with keeping the v8 logic just in case

if (isMethodCall) {
const typeName = this.getTypeName();
const methodName = this.getMethodName();
if (functionName !== null) {
if (typeName !== null) {
line += typeName + ".";
}
line += functionName;
} else {
line += typeName + "." + (methodName ?? "<anonymous>");
}
} else if (isConstructor) {
line += "new " + (functionName ?? "<anonymous>");
} else if (functionName !== null) {
line += functionName;
} else {
line += fileLocation;
addSuffix = false;
}
if (addSuffix) {
line += " (" + fileLocation + ")";
}
return line;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { SolidityCallSite } from "../../../../../../src/internal/builtin-plugins/network-manager/edr/stack-traces/stack-trace-solidity-errors.js";

describe("SolidityCallSite", function () {
describe("toString", function () {
it("works when no properties are present", async () => {
const callSite = new SolidityCallSite(
undefined,
undefined,
undefined,
undefined,
);

assert.equal(callSite.toString(), "null.<anonymous> (unknown)");
});

it("works when only the sourcename and the line number are present", async () => {
const callSite = new SolidityCallSite(
"Source.sol",
undefined,
undefined,
1,
);

assert.equal(callSite.toString(), "null.<anonymous> (Source.sol:1)");
});

it("works when all properties are present", async () => {
const callSite = new SolidityCallSite(
"Source.sol",
"Contract",
"functionName",
1,
);

assert.equal(callSite.toString(), "Contract.functionName (Source.sol:1)");
});
});
});
Loading