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

Fix various bugs with function names and source mappings involving eval and node:vm #16212

Merged
merged 9 commits into from
Jan 8, 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
1 change: 0 additions & 1 deletion src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ const JSPromise = bun.JSC.JSPromise;
const JSInternalPromise = bun.JSC.JSInternalPromise;
const JSModuleLoader = bun.JSC.JSModuleLoader;
const JSPromiseRejectionOperation = bun.JSC.JSPromiseRejectionOperation;
const Exception = bun.JSC.Exception;
const ErrorableZigString = bun.JSC.ErrorableZigString;
const ZigGlobalObject = bun.JSC.ZigGlobalObject;
const VM = bun.JSC.VM;
Expand Down
1 change: 0 additions & 1 deletion src/bun.js/api/ffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const JSPromise = bun.JSC.JSPromise;
const JSInternalPromise = bun.JSC.JSInternalPromise;
const JSModuleLoader = bun.JSC.JSModuleLoader;
const JSPromiseRejectionOperation = bun.JSC.JSPromiseRejectionOperation;
const Exception = bun.JSC.Exception;
const ErrorableZigString = bun.JSC.ErrorableZigString;
const ZigGlobalObject = bun.JSC.ZigGlobalObject;
const VM = bun.JSC.VM;
Expand Down
1 change: 0 additions & 1 deletion src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ const JSPromise = bun.JSC.JSPromise;
const JSInternalPromise = bun.JSC.JSInternalPromise;
const JSModuleLoader = bun.JSC.JSModuleLoader;
const JSPromiseRejectionOperation = bun.JSC.JSPromiseRejectionOperation;
const Exception = bun.JSC.Exception;
const ErrorableZigString = bun.JSC.ErrorableZigString;
const ZigGlobalObject = bun.JSC.ZigGlobalObject;
const VM = bun.JSC.VM;
Expand Down
88 changes: 61 additions & 27 deletions src/bun.js/bindings/CallSite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#include "JavaScriptCore/CallData.h"
#include "helpers.h"
#include "wtf/text/OrdinalNumber.h"

#include <JavaScriptCore/JSCInlines.h>
#include <optional>

using namespace JSC;
using namespace WebCore;
Expand All @@ -30,8 +32,8 @@ void CallSite::finishCreation(VM& vm, JSC::JSGlobalObject* globalObject, JSCStac
* Thus, if we've already encountered a strict frame, we'll treat our frame as strict too. */

bool isStrictFrame = encounteredStrictFrame;
JSC::CodeBlock* codeBlock = stackFrame.codeBlock();
if (!isStrictFrame) {
JSC::CodeBlock* codeBlock = stackFrame.codeBlock();
if (codeBlock) {
isStrictFrame = codeBlock->ownerExecutable()->isInStrictContext();
}
Expand Down Expand Up @@ -65,6 +67,8 @@ void CallSite::finishCreation(VM& vm, JSC::JSGlobalObject* globalObject, JSCStac

if (stackFrame.isEval()) {
m_flags |= static_cast<unsigned int>(Flags::IsEval);
} else if (stackFrame.isFunctionOrEval()) {
m_flags |= static_cast<unsigned int>(Flags::IsFunction);
}
if (stackFrame.isConstructor()) {
m_flags |= static_cast<unsigned int>(Flags::IsConstructor);
Expand Down Expand Up @@ -102,49 +106,79 @@ JSValue createNativeFrameForTesting(Zig::GlobalObject* globalObject)

void CallSite::formatAsString(JSC::VM& vm, JSC::JSGlobalObject* globalObject, WTF::StringBuilder& sb)
{
JSString* myFunctionName = functionName().toString(globalObject);
JSString* mySourceURL = sourceURL().toString(globalObject);
JSValue thisValue = jsUndefined();
if (m_thisValue) {
thisValue = m_thisValue.get();
}

JSString* myFunctionName = functionName().toStringOrNull(globalObject);
JSString* mySourceURL = sourceURL().toStringOrNull(globalObject);

String functionName;
if (myFunctionName && myFunctionName->length() > 0) {
functionName = myFunctionName->getString(globalObject);
} else if (m_flags & (static_cast<unsigned int>(Flags::IsFunction) | static_cast<unsigned int>(Flags::IsEval))) {
functionName = "<anonymous>"_s;
}

JSString* myColumnNumber = columnNumber().zeroBasedInt() >= 0 ? JSValue(columnNumber().oneBasedInt()).toString(globalObject) : jsEmptyString(vm);
JSString* myLineNumber = lineNumber().zeroBasedInt() >= 0 ? JSValue(lineNumber().oneBasedInt()).toString(globalObject) : jsEmptyString(vm);
std::optional<OrdinalNumber> column = columnNumber().zeroBasedInt() >= 0 ? std::optional(columnNumber()) : std::nullopt;
std::optional<OrdinalNumber> line = lineNumber().zeroBasedInt() >= 0 ? std::optional(lineNumber()) : std::nullopt;

bool myIsConstructor = isConstructor();
if (functionName.length() > 0) {

if (myFunctionName->length() > 0) {
if (myIsConstructor) {
if (isConstructor()) {
sb.append("new "_s);
} else {
// TODO: print type or class name if available
// sb.append(myTypeName->getString(globalObject));
// sb.append(" "_s);
}
sb.append(myFunctionName->getString(globalObject));
} else {
sb.append("<anonymous>"_s);

if (auto* object = thisValue.getObject()) {
auto catchScope = DECLARE_CATCH_SCOPE(vm);
auto className = object->calculatedClassName(object);
if (catchScope.exception()) {
catchScope.clearException();
}

if (className.length() > 0) {
sb.append(className);
sb.append("."_s);
}
}

sb.append(functionName);
}
sb.append(" ("_s);

if (isNative()) {
if (functionName.length() > 0) {
sb.append(" ("_s);
}
sb.append("native"_s);
if (functionName.length() > 0) {
sb.append(")"_s);
}
} else {
if (mySourceURL->length() == 0) {
if (functionName.length() > 0) {
sb.append(" ("_s);
}
if (!mySourceURL || mySourceURL->length() == 0) {
sb.append("unknown"_s);
} else {
sb.append(mySourceURL->getString(globalObject));
}

if (myLineNumber->length() > 0 && myColumnNumber->length() > 0) {
sb.append(":"_s);
sb.append(myLineNumber->getString(globalObject));
sb.append(":"_s);
sb.append(myColumnNumber->getString(globalObject));
} else if (myLineNumber->length() > 0) {
sb.append(":"_s);
sb.append(myLineNumber->getString(globalObject));
if (line && column) {
sb.append(':');
sb.append(line.value().oneBasedInt());
sb.append(':');
sb.append(column.value().oneBasedInt());
} else if (line) {
sb.append(':');
sb.append(line.value().oneBasedInt());
}

if (functionName.length() > 0) {
sb.append(')');
}
}
sb.append(")"_s);
}

DEFINE_VISIT_CHILDREN(CallSite);

}
2 changes: 2 additions & 0 deletions src/bun.js/bindings/CallSite.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class CallSite final : public JSC::JSNonFinalObject {
IsEval = 2,
IsConstructor = 4,
IsNative = 8,
IsWasm = 16,
IsFunction = 32,
};

private:
Expand Down
Loading
Loading