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 8 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
18 changes: 17 additions & 1 deletion src/bun.js/bindings/BunString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ BunString toStringRef(WTF::StringImpl* wtfString)
return { BunStringTag::WTFStringImpl, { .wtf = wtfString } };
}

BunString toStringView(StringView view) {
view.is8Bit();
paperdave marked this conversation as resolved.
Show resolved Hide resolved
return {
BunStringTag::StaticZigString,
{ .zig = {
.ptr = (const LChar*)(view.is8Bit()
? (size_t)view.rawCharacters()
: ((size_t)view.rawCharacters() + (static_cast<uint64_t>(1) << 63))
),
.len = view.length()
} }
};
}
paperdave marked this conversation as resolved.
Show resolved Hide resolved



}

extern "C" JSC::EncodedJSValue BunString__toJS(JSC::JSGlobalObject* globalObject, const BunString* bunString)
Expand Down Expand Up @@ -582,4 +598,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 @@ -413,7 +413,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
22 changes: 11 additions & 11 deletions src/bun.js/bindings/ZigSourceProvider.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "headers.h"
#include "root.h"
#include <_types/_uint64_t.h>
#include <limits>

#pragma once

Expand Down Expand Up @@ -36,17 +38,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 +68,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
10 changes: 10 additions & 0 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@ pub const ZigString = extern struct {
};
}

/// Creates a `Slice` from bytes without an allocator, aka a string view.
/// This slice is only valid for the lifetime of the given input, and
/// will never be freed by this slice.
pub fn initStatic(input: []const u8) Slice {
paperdave marked this conversation as resolved.
Show resolved Hide resolved
return .{
.ptr = input.ptr,
.len = @as(u32, @truncate(input.len)),
};
}

pub fn toZigString(this: Slice) ZigString {
if (this.isAllocated())
return ZigString.initUTF8(this.ptr[0..this.len]);
Expand Down
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