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

feat: load sourcemaps at runtime when using a bun build --target=bun bundle #10998

Merged
merged 24 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
16 changes: 11 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ option(USE_CUSTOM_TINYCC "Use Bun's recommended version of tinycc" ON)
option(USE_CUSTOM_LIBUV "Use Bun's recommended version of libuv (Windows only)" ON)
option(USE_CUSTOM_LSHPACK "Use Bun's recommended version of ls-hpack" ON)
option(USE_BASELINE_BUILD "Build Bun for baseline (older) CPUs" OFF)
option(USE_SYSTEM_ICU "Use the system-provided libicu. May fix startup crashes when building WebKit yourself." OFF)

option(USE_VALGRIND "Build Bun with Valgrind support (Linux only)" OFF)

Expand All @@ -320,7 +321,6 @@ option(USE_LTO "Enable Link-Time Optimization" ${DEFAULT_LTO})
option(BUN_TIDY_ONLY "Only run clang-tidy" OFF)
option(BUN_TIDY_ONLY_EXTRA " Only run clang-tidy, with extra checks for local development" OFF)


if(NOT ZIG_LIB_DIR)
cmake_path(SET ZIG_LIB_DIR NORMALIZE "${CMAKE_CURRENT_SOURCE_DIR}/src/deps/zig/lib")
endif()
Expand Down Expand Up @@ -1179,9 +1179,15 @@ if(UNIX AND NOT APPLE)
target_link_libraries(${bun} PRIVATE "libatomic.a")
endif()

target_link_libraries(${bun} PRIVATE "${WEBKIT_LIB_DIR}/libicudata.a")
target_link_libraries(${bun} PRIVATE "${WEBKIT_LIB_DIR}/libicui18n.a")
target_link_libraries(${bun} PRIVATE "${WEBKIT_LIB_DIR}/libicuuc.a")
if(USE_SYSTEM_ICU)
target_link_libraries(${bun} PRIVATE "libicudata.a")
target_link_libraries(${bun} PRIVATE "libicui18n.a")
target_link_libraries(${bun} PRIVATE "libicuuc.a")
else()
target_link_libraries(${bun} PRIVATE "${WEBKIT_LIB_DIR}/libicudata.a")
target_link_libraries(${bun} PRIVATE "${WEBKIT_LIB_DIR}/libicui18n.a")
target_link_libraries(${bun} PRIVATE "${WEBKIT_LIB_DIR}/libicuuc.a")
endif()

set_target_properties(${bun} PROPERTIES LINK_DEPENDS "${BUN_SRC}/linker.lds")
set_target_properties(${bun} PROPERTIES LINK_DEPENDS "${BUN_SRC}/symbols.dyn")
Expand Down Expand Up @@ -1446,7 +1452,7 @@ if(BUN_TIDY_ONLY)
set_target_properties(${bun} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
endif()

if (BUN_TIDY_ONLY_EXTRA)
if(BUN_TIDY_ONLY_EXTRA)
find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
set(CLANG_TIDY_COMMAND "${CLANG_TIDY_EXE}" "-checks=-*,clang-analyzer-*,performance-*,-clang-analyzer-webkit.UncountedLambdaCapturesChecker" "--fix" "--fix-errors" "--format-style=webkit" "--warnings-as-errors=*")
set_target_properties(${bun} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
Expand Down
2 changes: 1 addition & 1 deletion packages/bun-internal-test/src/runner.node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function checkSlowTests() {
);
proc?.stdout?.destroy?.();
proc?.stderr?.destroy?.();
proc?.kill?.();
proc?.kill?.(9);
} else if (now - start > SHORT_TIMEOUT_DURATION) {
console.error(
`\x1b[33mwarning\x1b[0;2m:\x1b[0m Test ${JSON.stringify(path)} has been running for ${Math.ceil(
Expand Down
11 changes: 10 additions & 1 deletion src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,18 @@ pub fn writeTrace(comptime Writer: type, writer: Writer, global: *JSGlobalObject
defer holder.deinit(vm);
const exception = holder.zigException();

var source_code_slice: ?ZigString.Slice = null;
defer if (source_code_slice) |slice| slice.deinit();

var err = ZigString.init("trace output").toErrorInstance(global);
err.toZigException(global, exception);
vm.remapZigException(exception, err, null, &holder.need_to_clear_parser_arena_on_deinit);
vm.remapZigException(
exception,
err,
null,
&holder.need_to_clear_parser_arena_on_deinit,
&source_code_slice,
);

if (Output.enable_ansi_colors_stderr)
VirtualMachine.printStackTrace(
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/WebKit
Submodule WebKit updated 1691 files
10 changes: 9 additions & 1 deletion src/bun.js/bindings/BunString.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include "helpers.h"
#include "root.h"
#include "headers-handwritten.h"
#include <JavaScriptCore/JSCJSValueInlines.h>
Expand Down Expand Up @@ -196,6 +197,13 @@ BunString toStringRef(WTF::StringImpl* wtfString)
return { BunStringTag::WTFStringImpl, { .wtf = wtfString } };
}

BunString toStringView(StringView view) {
return {
BunStringTag::ZigString,
{ .zig = toZigString(view) }
};
}

}

extern "C" JSC::EncodedJSValue BunString__toJS(JSC::JSGlobalObject* globalObject, const BunString* bunString)
Expand Down Expand Up @@ -582,4 +590,4 @@ extern "C" void JSC__JSValue__putBunString(
WTF::String str = key->tag == BunStringTag::Empty ? WTF::String(""_s) : key->toWTFString();
Identifier id = Identifier::fromString(vm, str);
target->putDirect(vm, id, value, 0);
}
}
2 changes: 1 addition & 1 deletion src/bun.js/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ WTF::String Bun::formatStackTrace(JSC::VM& vm, JSC::JSGlobalObject* globalObject

sb.append(" at <parse> ("_s);

sb.append(sourceURLForFrame);
sb.append(remappedFrame.source_url.toWTFString());

if (remappedFrame.remapped) {
errorInstance->putDirect(vm, Identifier::fromString(vm, "originalLine"_s), jsNumber(originalLine.oneBasedInt()), 0);
Expand Down
34 changes: 27 additions & 7 deletions src/bun.js/bindings/ZigSourceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ JSC::SourceID sourceIDForSourceURL(const WTF::String& sourceURL)
}

extern "C" bool BunTest__shouldGenerateCodeCoverage(BunString sourceURL);

Ref<SourceProvider> SourceProvider::create(Zig::GlobalObject* globalObject, ResolvedSource& resolvedSource, JSC::SourceProviderSourceType sourceType, bool isBuiltin)
{

extern "C" void Bun__addSourceProviderSourceMap(void* bun_vm, SourceProvider* opaque_source_provider, BunString* specifier);
extern "C" void Bun__removeSourceProviderSourceMap(void* bun_vm, SourceProvider* opaque_source_provider, BunString* specifier);

Ref<SourceProvider> SourceProvider::create(
Zig::GlobalObject* globalObject,
ResolvedSource& resolvedSource,
JSC::SourceProviderSourceType sourceType,
bool isBuiltin
) {
auto string = resolvedSource.source_code.toWTFString(BunString::ZeroCopy);
auto sourceURLString = resolvedSource.source_url.toWTFString(BunString::ZeroCopy);

Expand Down Expand Up @@ -99,9 +104,20 @@ Ref<SourceProvider> SourceProvider::create(Zig::GlobalObject* globalObject, Reso
ByteRangeMapping__generate(Bun::toString(provider->sourceURL()), Bun::toString(provider->source().toStringWithoutCopying()), provider->asID());
}

if (resolvedSource.already_bundled) {
Bun__addSourceProviderSourceMap(globalObject->bunVM(), provider.ptr(), &resolvedSource.source_url);
}

return provider;
}

SourceProvider::~SourceProvider() {
if(m_resolvedSource.already_bundled) {
BunString str = Bun::toString(sourceURL());
Bun__removeSourceProviderSourceMap(m_globalObject->bunVM(), this, &str);
}
}

unsigned SourceProvider::hash() const
{
if (m_hash) {
Expand Down Expand Up @@ -139,9 +155,8 @@ void SourceProvider::cacheBytecode(const BytecodeCacheGenerator& generator)
if (update)
m_cachedBytecode->addGlobalUpdate(*update);
}
SourceProvider::~SourceProvider()
{
}


void SourceProvider::commitCachedBytecode()
{
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode || !m_cachedBytecode->hasUpdates())
Expand Down Expand Up @@ -229,4 +244,9 @@ int SourceProvider::readCache(JSC::VM& vm, const JSC::SourceCode& sourceCode)
// return 0;
// }
}

extern "C" BunString ZigSourceProvider__getSourceSlice(SourceProvider* provider) {
return Bun::toStringView(provider->source());
}

}; // namespace Zig
20 changes: 9 additions & 11 deletions src/bun.js/bindings/ZigSourceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ class SourceProvider final : public JSC::SourceProvider {
using SourceOrigin = JSC::SourceOrigin;

public:
static Ref<SourceProvider> create(Zig::GlobalObject*, ResolvedSource& resolvedSource, JSC::SourceProviderSourceType sourceType = JSC::SourceProviderSourceType::Module, bool isBuiltIn = false);
static Ref<SourceProvider> create(
Zig::GlobalObject*,
ResolvedSource& resolvedSource,
JSC::SourceProviderSourceType sourceType = JSC::SourceProviderSourceType::Module,
bool isBuiltIn = false);
~SourceProvider();
unsigned hash() const override;
StringView source() const override { return StringView(m_source.get()); }

RefPtr<JSC::CachedBytecode> cachedBytecode()
{
// if (m_resolvedSource.bytecodecache_fd == 0) {
return nullptr;
// }

// return m_cachedBytecode;
};

void updateCache(const UnlinkedFunctionExecutable* executable, const SourceCode&,
Expand All @@ -65,19 +66,16 @@ class SourceProvider final : public JSC::SourceProvider {
const SourceOrigin& sourceOrigin, WTF::String&& sourceURL,
const TextPosition& startPosition, JSC::SourceProviderSourceType sourceType)
: Base(sourceOrigin, WTFMove(sourceURL), String(), taintedness, startPosition, sourceType)
, m_globalObject(globalObject)
, m_source(sourceImpl)
{

m_resolvedSource = resolvedSource;
}

Zig::GlobalObject* m_globalObject;
RefPtr<JSC::CachedBytecode> m_cachedBytecode;
Ref<WTF::StringImpl> m_source;
bool did_free_source_code = false;
Zig::GlobalObject* m_globalObjectForSourceProviderMap;
unsigned m_hash = 0;

// JSC::SourceCodeKey key;
};

} // namespace Zig
} // namespace Zig
1 change: 1 addition & 0 deletions src/bun.js/bindings/exports.zig
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pub const ResolvedSource = extern struct {

/// This is for source_code
source_code_needs_deref: bool = true,
already_bundled: bool = false,

pub const Tag = @import("ResolvedSourceTag").ResolvedSourceTag;
};
Expand Down
5 changes: 5 additions & 0 deletions src/bun.js/bindings/headers-handwritten.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ typedef struct ResolvedSource {
JSC::EncodedJSValue jsvalue_for_export;
uint32_t tag;
bool needsDeref;
bool already_bundled;
} ResolvedSource;
static const uint32_t ResolvedSourceTagPackageJSONTypeModule = 1;
typedef union ErrorableResolvedSourceResult {
Expand Down Expand Up @@ -278,6 +279,10 @@ BunString toStringRef(JSC::JSGlobalObject* globalObject, JSC::JSValue value);
BunString toStringRef(WTF::String& wtfString);
BunString toStringRef(const WTF::String& wtfString);
BunString toStringRef(WTF::StringImpl* wtfString);

// This creates a detached string view, which cannot be ref/unref.
// Be very careful using this, and ensure the memory owner does not get destroyed.
BunString toStringView(WTF::StringView view);
}

using Uint8Array_alias = JSC::JSUint8Array;
Expand Down
Loading
Loading