mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 13:51:47 +00:00
Compare commits
4 Commits
claude/fix
...
claude/ref
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4aa25a754 | ||
|
|
4d7a01064f | ||
|
|
65b5670913 | ||
|
|
56b1ea2474 |
@@ -725,8 +725,8 @@ fn addInternalImports(b: *Build, mod: *Module, opts: *BunBuildOptions) void {
|
||||
inline for (.{
|
||||
.{ .file = "ZigGeneratedClasses.zig", .import = "ZigGeneratedClasses" },
|
||||
.{ .file = "bindgen_generated.zig", .import = "bindgen_generated" },
|
||||
.{ .file = "ResolvedSourceTag.zig", .import = "ResolvedSourceTag" },
|
||||
.{ .file = "ErrorCode.zig", .import = "ErrorCode" },
|
||||
.{ .file = "ResolvedSourceTag.zig", .import = "ResolvedSourceTag" },
|
||||
.{ .file = "runtime.out.js", .enable = opts.shouldEmbedCode() },
|
||||
.{ .file = "bake.client.js", .import = "bake-codegen/bake.client.js", .enable = opts.shouldEmbedCode() },
|
||||
.{ .file = "bake.error.js", .import = "bake-codegen/bake.error.js", .enable = opts.shouldEmbedCode() },
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1446,31 +1446,11 @@ pub fn clearRefString(_: *anyopaque, ref_string: *jsc.RefString) void {
|
||||
_ = VirtualMachine.get().ref_strings.remove(ref_string.hash);
|
||||
}
|
||||
|
||||
pub fn refCountedResolvedSource(this: *VirtualMachine, code: []const u8, specifier: bun.String, source_url: []const u8, hash_: ?u32, comptime add_double_ref: bool) ResolvedSource {
|
||||
// refCountedString will panic if the code is empty
|
||||
if (code.len == 0) {
|
||||
return ResolvedSource{
|
||||
.source_code = bun.String.init(""),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier.createIfDifferent(source_url),
|
||||
.allocator = null,
|
||||
.source_code_needs_deref = false,
|
||||
};
|
||||
}
|
||||
var source = this.refCountedString(code, hash_, !add_double_ref);
|
||||
if (add_double_ref) {
|
||||
source.ref();
|
||||
source.ref();
|
||||
}
|
||||
|
||||
return ResolvedSource{
|
||||
.source_code = bun.String.init(source.impl),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier.createIfDifferent(source_url),
|
||||
.allocator = source,
|
||||
.source_code_needs_deref = false,
|
||||
};
|
||||
}
|
||||
// TODO: This function is deprecated - update watcher code to work with ModuleResult
|
||||
// pub fn refCountedResolvedSource(this: *VirtualMachine, code: []const u8, specifier: bun.String, source_url: []const u8, hash_: ?u32, comptime add_double_ref: bool) ResolvedSource {
|
||||
// _ = this; _ = code; _ = specifier; _ = source_url; _ = hash_; _ = add_double_ref;
|
||||
// @compileError("refCountedResolvedSource is deprecated - use ModuleResult instead");
|
||||
// }
|
||||
|
||||
fn refCountedStringWithWasNew(this: *VirtualMachine, new: *bool, input_: []const u8, hash_: ?u32, comptime dupe: bool) *jsc.RefString {
|
||||
jsc.markBinding(@src());
|
||||
@@ -1519,7 +1499,7 @@ pub fn fetchWithoutOnLoadPlugins(
|
||||
referrer: String,
|
||||
log: *logger.Log,
|
||||
comptime flags: FetchFlags,
|
||||
) anyerror!ResolvedSource {
|
||||
) anyerror!ModuleResult {
|
||||
bun.assert(VirtualMachine.isLoaded());
|
||||
|
||||
if (try ModuleLoader.fetchBuiltinModule(jsc_vm, _specifier)) |builtin| {
|
||||
@@ -1863,7 +1843,7 @@ pub fn drainMicrotasks(this: *VirtualMachine) void {
|
||||
this.eventLoop().drainMicrotasks() catch {}; // TODO: properly propagate exception upwards
|
||||
}
|
||||
|
||||
pub fn processFetchLog(globalThis: *JSGlobalObject, specifier: bun.String, referrer: bun.String, log: *logger.Log, ret: *ErrorableResolvedSource, err: anyerror) void {
|
||||
pub fn processFetchLog(globalThis: *JSGlobalObject, specifier: bun.String, referrer: bun.String, log: *logger.Log, ret: *ModuleResult, err: anyerror) void {
|
||||
switch (log.msgs.items.len) {
|
||||
0 => {
|
||||
const msg: logger.Msg = brk: {
|
||||
@@ -1882,22 +1862,32 @@ pub fn processFetchLog(globalThis: *JSGlobalObject, specifier: bun.String, refer
|
||||
};
|
||||
};
|
||||
{
|
||||
ret.* = ErrorableResolvedSource.err(err, (bun.api.BuildMessage.create(globalThis, globalThis.allocator(), msg) catch |e| globalThis.takeException(e)));
|
||||
ret.* = ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = ErrorResult{
|
||||
.exception = (bun.api.BuildMessage.create(globalThis, globalThis.allocator(), msg) catch |e| globalThis.takeException(e)),
|
||||
} },
|
||||
};
|
||||
}
|
||||
return;
|
||||
},
|
||||
|
||||
1 => {
|
||||
const msg = log.msgs.items[0];
|
||||
ret.* = ErrorableResolvedSource.err(err, switch (msg.metadata) {
|
||||
.build => (bun.api.BuildMessage.create(globalThis, globalThis.allocator(), msg) catch |e| globalThis.takeException(e)),
|
||||
.resolve => (bun.api.ResolveMessage.create(
|
||||
globalThis,
|
||||
globalThis.allocator(),
|
||||
msg,
|
||||
referrer.toUTF8(bun.default_allocator).slice(),
|
||||
) catch |e| globalThis.takeException(e)),
|
||||
});
|
||||
ret.* = ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = ErrorResult{
|
||||
.exception = switch (msg.metadata) {
|
||||
.build => (bun.api.BuildMessage.create(globalThis, globalThis.allocator(), msg) catch |e| globalThis.takeException(e)),
|
||||
.resolve => (bun.api.ResolveMessage.create(
|
||||
globalThis,
|
||||
globalThis.allocator(),
|
||||
msg,
|
||||
referrer.toUTF8(bun.default_allocator).slice(),
|
||||
) catch |e| globalThis.takeException(e)),
|
||||
},
|
||||
} },
|
||||
};
|
||||
return;
|
||||
},
|
||||
else => {
|
||||
@@ -1919,18 +1909,20 @@ pub fn processFetchLog(globalThis: *JSGlobalObject, specifier: bun.String, refer
|
||||
};
|
||||
}
|
||||
|
||||
ret.* = ErrorableResolvedSource.err(
|
||||
err,
|
||||
globalThis.createAggregateError(
|
||||
errors,
|
||||
&ZigString.init(
|
||||
std.fmt.allocPrint(globalThis.allocator(), "{d} errors building \"{}\"", .{
|
||||
errors.len,
|
||||
specifier,
|
||||
}) catch unreachable,
|
||||
),
|
||||
) catch |e| globalThis.takeException(e),
|
||||
);
|
||||
ret.* = ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = ErrorResult{
|
||||
.exception = globalThis.createAggregateError(
|
||||
errors,
|
||||
&ZigString.init(
|
||||
std.fmt.allocPrint(globalThis.allocator(), "{d} errors building \"{}\"", .{
|
||||
errors.len,
|
||||
specifier,
|
||||
}) catch unreachable,
|
||||
),
|
||||
) catch |e| globalThis.takeException(e),
|
||||
} },
|
||||
};
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -2797,9 +2789,15 @@ pub fn remapZigException(
|
||||
var log = logger.Log.init(bun.default_allocator);
|
||||
defer log.deinit();
|
||||
|
||||
var original_source = fetchWithoutOnLoadPlugins(this, this.global, top.source_url, bun.String.empty, &log, .print_source) catch return;
|
||||
const module_result = fetchWithoutOnLoadPlugins(this, this.global, top.source_url, bun.String.empty, &log, .print_source) catch return;
|
||||
must_reset_parser_arena_later.* = true;
|
||||
break :code original_source.source_code.toUTF8(bun.default_allocator);
|
||||
|
||||
// For error printing, we only care about transpiled sources
|
||||
if (module_result.tag != .transpiled) {
|
||||
break :code ZigString.Slice.empty;
|
||||
}
|
||||
|
||||
break :code module_result.value.transpiled.source_code.toUTF8(bun.default_allocator);
|
||||
};
|
||||
|
||||
if (enable_source_code_preview and code.len == 0) {
|
||||
@@ -3703,6 +3701,9 @@ const PackageManager = @import("../install/install.zig").PackageManager;
|
||||
const URL = @import("../url.zig").URL;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const ErrorResult = @import("./bindings/ModuleResult.zig").ErrorResult;
|
||||
const ModuleResult = @import("./bindings/ModuleResult.zig").ModuleResult;
|
||||
|
||||
const bun = @import("bun");
|
||||
const Async = bun.Async;
|
||||
const DotEnv = bun.DotEnv;
|
||||
@@ -3729,7 +3730,6 @@ const DNSResolver = bun.api.dns.Resolver;
|
||||
|
||||
const jsc = bun.jsc;
|
||||
const ConsoleObject = jsc.ConsoleObject;
|
||||
const ErrorableResolvedSource = jsc.ErrorableResolvedSource;
|
||||
const ErrorableString = jsc.ErrorableString;
|
||||
const EventLoop = jsc.EventLoop;
|
||||
const Exception = jsc.Exception;
|
||||
@@ -3738,7 +3738,6 @@ const JSInternalPromise = jsc.JSInternalPromise;
|
||||
const JSModuleLoader = jsc.JSModuleLoader;
|
||||
const JSValue = jsc.JSValue;
|
||||
const Node = jsc.Node;
|
||||
const ResolvedSource = jsc.ResolvedSource;
|
||||
const SavedSourceMap = jsc.SavedSourceMap;
|
||||
const VM = jsc.VM;
|
||||
const ZigException = jsc.ZigException;
|
||||
|
||||
285
src/bun.js/bindings/BunSourceProvider.cpp
Normal file
285
src/bun.js/bindings/BunSourceProvider.cpp
Normal file
@@ -0,0 +1,285 @@
|
||||
#include "BunSourceProvider.h"
|
||||
#include "ZigGlobalObject.h"
|
||||
#include "helpers.h"
|
||||
#include <JavaScriptCore/SourceCode.h>
|
||||
#include <JavaScriptCore/CodeCache.h>
|
||||
#include <JavaScriptCore/Completion.h>
|
||||
#include <JavaScriptCore/BytecodeCacheError.h>
|
||||
#include <JavaScriptCore/ParserError.h>
|
||||
#include <JavaScriptCore/UnlinkedSourceCode.h>
|
||||
#include <wtf/text/StringHash.h>
|
||||
#include <wtf/text/WTFString.h>
|
||||
#include <mimalloc.h>
|
||||
|
||||
extern "C" void Bun__addSourceProviderSourceMap(void* vm, JSC::SourceProvider* provider, BunString* specifier);
|
||||
extern "C" void Bun__removeSourceProviderSourceMap(void* vm, JSC::SourceProvider* provider, BunString* specifier);
|
||||
|
||||
namespace Zig {
|
||||
|
||||
BunSourceProvider::BunSourceProvider(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType)
|
||||
: JSC::SourceProvider(origin, String(sourceURL), String(), JSC::SourceTaintedOrigin::Untainted, TextPosition(), sourceType)
|
||||
, m_source(WTFMove(source))
|
||||
, m_cachedBytecode(WTFMove(bytecode))
|
||||
{
|
||||
}
|
||||
|
||||
Ref<BunSourceProvider> BunSourceProvider::create(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType)
|
||||
{
|
||||
return adoptRef(*new BunSourceProvider(
|
||||
globalObject,
|
||||
WTFMove(source),
|
||||
origin,
|
||||
WTFMove(sourceURL),
|
||||
WTFMove(bytecode),
|
||||
sourceType));
|
||||
}
|
||||
|
||||
BunSourceProvider::~BunSourceProvider()
|
||||
{
|
||||
// Note: We cannot unregister the sourcemap here because we don't have access
|
||||
// to the globalObject or VM at destruction time. The VM's source_mappings
|
||||
// will clean up when the VM is destroyed.
|
||||
}
|
||||
|
||||
StringView BunSourceProvider::source() const
|
||||
{
|
||||
return StringView(m_source.get());
|
||||
}
|
||||
|
||||
unsigned BunSourceProvider::hash() const
|
||||
{
|
||||
return m_source->hash();
|
||||
}
|
||||
|
||||
RefPtr<JSC::CachedBytecode> BunSourceProvider::cachedBytecode() const
|
||||
{
|
||||
return m_cachedBytecode;
|
||||
}
|
||||
|
||||
extern "C" void* ByteRangeMapping__find(BunString);
|
||||
extern "C" JSC::SourceID ByteRangeMapping__getSourceID(void*, BunString);
|
||||
|
||||
JSC::SourceID sourceIDForSourceURL(const WTF::String& sourceURL)
|
||||
{
|
||||
void* mappings = ByteRangeMapping__find(Bun::toString(sourceURL));
|
||||
if (!mappings) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ByteRangeMapping__getSourceID(mappings, Bun::toString(sourceURL));
|
||||
}
|
||||
|
||||
} // namespace Zig
|
||||
|
||||
// C bridge function
|
||||
extern "C" JSC::SourceProvider* Bun__createSourceProvider(
|
||||
Zig::GlobalObject* globalObject,
|
||||
const TranspiledSource* source)
|
||||
{
|
||||
auto sourceString = source->source_code.toWTFString(BunString::ZeroCopy);
|
||||
auto sourceURL = source->source_url.toWTFString(BunString::ZeroCopy);
|
||||
|
||||
// Extract is_commonjs flag (bit 0 of flags uint32_t)
|
||||
bool isCommonJS = (source->flags & 0x1) != 0;
|
||||
auto sourceType = isCommonJS ? JSC::SourceProviderSourceType::Program : JSC::SourceProviderSourceType::Module;
|
||||
|
||||
// Handle bytecode if present
|
||||
RefPtr<JSC::CachedBytecode> bytecode;
|
||||
if (source->bytecode_cache) {
|
||||
bytecode = JSC::CachedBytecode::create(
|
||||
std::span<uint8_t>(source->bytecode_cache, source->bytecode_cache_len),
|
||||
[](const void* ptr) { mi_free(const_cast<void*>(ptr)); },
|
||||
{});
|
||||
}
|
||||
|
||||
// Create SourceOrigin
|
||||
auto origin = JSC::SourceOrigin(WTF::URL(sourceURL));
|
||||
|
||||
auto provider = Zig::BunSourceProvider::create(
|
||||
globalObject,
|
||||
Ref<WTF::StringImpl>(*sourceString.impl()),
|
||||
origin,
|
||||
WTFMove(sourceURL),
|
||||
WTFMove(bytecode),
|
||||
sourceType);
|
||||
|
||||
// Register sourcemap with VM only if already_bundled (bit 1 of flags)
|
||||
if (source->flags & 0x2) {
|
||||
Bun__addSourceProviderSourceMap(
|
||||
reinterpret_cast<void*>(globalObject->bunVM()),
|
||||
&provider.get(),
|
||||
const_cast<BunString*>(&source->source_url));
|
||||
}
|
||||
|
||||
return &provider.leakRef();
|
||||
}
|
||||
|
||||
// Bytecode cache utilities
|
||||
extern "C" void CachedBytecode__deref(JSC::CachedBytecode* cachedBytecode)
|
||||
{
|
||||
cachedBytecode->deref();
|
||||
}
|
||||
|
||||
static JSC::VM& getVMForBytecodeCache()
|
||||
{
|
||||
static thread_local JSC::VM* vmForBytecodeCache = nullptr;
|
||||
if (!vmForBytecodeCache) {
|
||||
const auto heapSize = JSC::HeapType::Small;
|
||||
auto& vm = JSC::VM::create(heapSize).leakRef();
|
||||
vmForBytecodeCache = &vm;
|
||||
}
|
||||
return *vmForBytecodeCache;
|
||||
}
|
||||
|
||||
extern "C" bool generateCachedModuleByteCodeFromSourceCode(
|
||||
BunString* sourceProviderURL,
|
||||
const Latin1Character* inputSourceCode,
|
||||
size_t inputSourceCodeSize,
|
||||
const uint8_t** outputByteCode,
|
||||
size_t* outputByteCodeSize,
|
||||
JSC::CachedBytecode** cachedBytecodePtr,
|
||||
int32_t* errorLoc,
|
||||
BunString* errorMessage)
|
||||
{
|
||||
std::span<const Latin1Character> sourceCodeSpan(inputSourceCode, inputSourceCodeSize);
|
||||
JSC::SourceCode sourceCode = JSC::makeSource(
|
||||
WTF::String(sourceCodeSpan),
|
||||
JSC::SourceOrigin(WTF::URL(sourceProviderURL->toWTFString())),
|
||||
JSC::SourceTaintedOrigin::Untainted);
|
||||
|
||||
JSC::VM& vm = getVMForBytecodeCache();
|
||||
JSC::JSLockHolder locker(vm);
|
||||
|
||||
JSC::LexicallyScopedFeatures lexicallyScopedFeatures = JSC::StrictModeLexicallyScopedFeature;
|
||||
JSC::JSParserScriptMode scriptMode = JSC::JSParserScriptMode::Module;
|
||||
JSC::EvalContextType evalContextType = JSC::EvalContextType::None;
|
||||
|
||||
JSC::ParserError parserError;
|
||||
JSC::UnlinkedModuleProgramCodeBlock* unlinkedCodeBlock = JSC::recursivelyGenerateUnlinkedCodeBlockForModuleProgram(
|
||||
vm, sourceCode, lexicallyScopedFeatures, scriptMode, {}, parserError, evalContextType);
|
||||
|
||||
if (parserError.isValid()) {
|
||||
if (errorLoc) {
|
||||
*errorLoc = parserError.token().m_startPosition.offset;
|
||||
}
|
||||
if (errorMessage) {
|
||||
*errorMessage = Bun::toStringRef(parserError.message());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
auto sourceCodeKey = JSC::SourceCodeKey(
|
||||
sourceCode,
|
||||
sourceCode.provider()->sourceURL(),
|
||||
JSC::SourceCodeType::ModuleType,
|
||||
lexicallyScopedFeatures,
|
||||
scriptMode,
|
||||
JSC::DerivedContextType::None,
|
||||
evalContextType,
|
||||
false, // isArrowFunctionContext
|
||||
{}, // empty CodeGenerationMode
|
||||
std::nullopt // functionConstructorParametersEndPosition
|
||||
);
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeCodeBlock(vm, sourceCodeKey, unlinkedCodeBlock);
|
||||
if (!cachedBytecode) {
|
||||
if (errorLoc) {
|
||||
*errorLoc = -1;
|
||||
}
|
||||
if (errorMessage) {
|
||||
WTF::String errMsg = "Failed to encode bytecode"_s;
|
||||
*errorMessage = Bun::toStringRef(errMsg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
cachedBytecode->ref();
|
||||
*cachedBytecodePtr = cachedBytecode.get();
|
||||
*outputByteCode = cachedBytecode->span().data();
|
||||
*outputByteCodeSize = cachedBytecode->span().size();
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool generateCachedCommonJSProgramByteCodeFromSourceCode(
|
||||
BunString* sourceProviderURL,
|
||||
const Latin1Character* inputSourceCode,
|
||||
size_t inputSourceCodeSize,
|
||||
const uint8_t** outputByteCode,
|
||||
size_t* outputByteCodeSize,
|
||||
JSC::CachedBytecode** cachedBytecodePtr,
|
||||
int32_t* errorLoc,
|
||||
BunString* errorMessage)
|
||||
{
|
||||
std::span<const Latin1Character> sourceCodeSpan(inputSourceCode, inputSourceCodeSize);
|
||||
JSC::SourceCode sourceCode = JSC::makeSource(
|
||||
WTF::String(sourceCodeSpan),
|
||||
JSC::SourceOrigin(WTF::URL(sourceProviderURL->toWTFString())),
|
||||
JSC::SourceTaintedOrigin::Untainted);
|
||||
|
||||
JSC::VM& vm = getVMForBytecodeCache();
|
||||
JSC::JSLockHolder locker(vm);
|
||||
|
||||
JSC::LexicallyScopedFeatures lexicallyScopedFeatures = JSC::NoLexicallyScopedFeatures;
|
||||
JSC::JSParserScriptMode scriptMode = JSC::JSParserScriptMode::Classic;
|
||||
JSC::EvalContextType evalContextType = JSC::EvalContextType::None;
|
||||
|
||||
JSC::ParserError parserError;
|
||||
JSC::UnlinkedProgramCodeBlock* unlinkedCodeBlock = JSC::recursivelyGenerateUnlinkedCodeBlockForProgram(
|
||||
vm, sourceCode, lexicallyScopedFeatures, scriptMode, {}, parserError, evalContextType);
|
||||
|
||||
if (parserError.isValid()) {
|
||||
if (errorLoc) {
|
||||
*errorLoc = parserError.token().m_startPosition.offset;
|
||||
}
|
||||
if (errorMessage) {
|
||||
*errorMessage = Bun::toStringRef(parserError.message());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
auto sourceCodeKey = JSC::SourceCodeKey(
|
||||
sourceCode,
|
||||
sourceCode.provider()->sourceURL(),
|
||||
JSC::SourceCodeType::ProgramType,
|
||||
lexicallyScopedFeatures,
|
||||
scriptMode,
|
||||
JSC::DerivedContextType::None,
|
||||
evalContextType,
|
||||
false, // isArrowFunctionContext
|
||||
{}, // empty CodeGenerationMode
|
||||
std::nullopt // functionConstructorParametersEndPosition
|
||||
);
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeCodeBlock(vm, sourceCodeKey, unlinkedCodeBlock);
|
||||
if (!cachedBytecode) {
|
||||
if (errorLoc) {
|
||||
*errorLoc = -1;
|
||||
}
|
||||
if (errorMessage) {
|
||||
WTF::String errMsg = "Failed to encode bytecode"_s;
|
||||
*errorMessage = Bun::toStringRef(errMsg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
cachedBytecode->ref();
|
||||
*cachedBytecodePtr = cachedBytecode.get();
|
||||
*outputByteCode = cachedBytecode->span().data();
|
||||
*outputByteCodeSize = cachedBytecode->span().size();
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" BunString ZigSourceProvider__getSourceSlice(JSC::SourceProvider* provider)
|
||||
{
|
||||
return Bun::toStringView(provider->source());
|
||||
}
|
||||
55
src/bun.js/bindings/BunSourceProvider.h
Normal file
55
src/bun.js/bindings/BunSourceProvider.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include "root.h"
|
||||
#include "JavaScriptCore/SourceProvider.h"
|
||||
#include "JavaScriptCore/CachedBytecode.h"
|
||||
#include "JavaScriptCore/SourceOrigin.h"
|
||||
#include <wtf/text/WTFString.h>
|
||||
|
||||
// Forward declaration - full definition in headers-handwritten.h
|
||||
struct TranspiledSource;
|
||||
|
||||
namespace Zig {
|
||||
|
||||
class GlobalObject;
|
||||
|
||||
class BunSourceProvider final : public JSC::SourceProvider {
|
||||
public:
|
||||
static Ref<BunSourceProvider> create(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType);
|
||||
|
||||
~BunSourceProvider() final;
|
||||
|
||||
// Required overrides
|
||||
StringView source() const final;
|
||||
unsigned hash() const final;
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode() const final;
|
||||
|
||||
private:
|
||||
BunSourceProvider(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType);
|
||||
|
||||
// Simplified members (vs ZigSourceProvider)
|
||||
Ref<WTF::StringImpl> m_source;
|
||||
RefPtr<JSC::CachedBytecode> m_cachedBytecode;
|
||||
|
||||
// REMOVED: No more m_resolvedSource member!
|
||||
};
|
||||
|
||||
JSC::SourceID sourceIDForSourceURL(const WTF::String& sourceURL);
|
||||
|
||||
} // namespace Zig
|
||||
|
||||
extern "C" JSC::SourceProvider* Bun__createSourceProvider(
|
||||
Zig::GlobalObject* globalObject,
|
||||
const TranspiledSource* source);
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "root.h"
|
||||
#include "ZigSourceProvider.h"
|
||||
#include <JavaScriptCore/ControlFlowProfiler.h>
|
||||
|
||||
using namespace JSC;
|
||||
|
||||
@@ -260,9 +260,8 @@ JSC::EncodedJSValue builtinLoader(JSC::JSGlobalObject* globalObject, JSC::CallFr
|
||||
BunString specifierBunString = Bun::toString(specifierWtfString);
|
||||
BunString empty = BunStringEmpty;
|
||||
JSC::VM& vm = globalObject->vm();
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
|
||||
JSValue result = fetchCommonJSModuleNonBuiltin<true>(
|
||||
global->bunVM(),
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
|
||||
#include <JavaScriptCore/JSMapInlines.h>
|
||||
#include <JavaScriptCore/GetterSetter.h>
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
#include <JavaScriptCore/FunctionPrototype.h>
|
||||
#include "JSCommonJSModule.h"
|
||||
#include <JavaScriptCore/JSModuleNamespaceObject.h>
|
||||
@@ -1311,14 +1311,13 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionRequireNativeModule, (JSGlobalObject * lexica
|
||||
JSValue specifierValue = callframe->argument(0);
|
||||
WTF::String specifier = specifierValue.toWTFString(globalObject);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
BunString specifierStr = Bun::toString(specifier);
|
||||
auto result = fetchBuiltinModuleWithoutResolution(globalObject, &specifierStr, &res);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
if (result) {
|
||||
if (res.success)
|
||||
if (res.tag != ModuleResult::Tag::err)
|
||||
return JSC::JSValue::encode(result);
|
||||
}
|
||||
throwScope.assertNoExceptionExceptTermination();
|
||||
@@ -1337,7 +1336,7 @@ void RequireResolveFunctionPrototype::finishCreation(JSC::VM& vm)
|
||||
void JSCommonJSModule::evaluate(
|
||||
Zig::GlobalObject* globalObject,
|
||||
const WTF::String& key,
|
||||
ResolvedSource& source,
|
||||
TranspiledSource& source,
|
||||
bool isBuiltIn)
|
||||
{
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
@@ -1346,26 +1345,22 @@ void JSCommonJSModule::evaluate(
|
||||
auto string = source.source_code.toWTFString(BunString::ZeroCopy);
|
||||
auto trimStart = string.find('\n');
|
||||
if (trimStart != WTF::notFound) {
|
||||
if (source.needsDeref && !isBuiltIn) {
|
||||
source.needsDeref = false;
|
||||
source.source_code.deref();
|
||||
}
|
||||
auto wrapperStart = globalObject->m_moduleWrapperStart;
|
||||
auto wrapperEnd = globalObject->m_moduleWrapperEnd;
|
||||
source.source_code = Bun::toStringRef(makeString(
|
||||
wrapperStart,
|
||||
string.substring(trimStart, string.length() - trimStart - 4),
|
||||
wrapperEnd));
|
||||
source.needsDeref = true;
|
||||
}
|
||||
}
|
||||
|
||||
auto sourceProvider = Zig::SourceProvider::create(jsCast<Zig::GlobalObject*>(globalObject), source, JSC::SourceProviderSourceType::Program, isBuiltIn);
|
||||
this->ignoreESModuleAnnotation = source.tag == ResolvedSourceTagPackageJSONTypeModule;
|
||||
auto* sourceProvider = Bun__createSourceProvider(globalObject, &source);
|
||||
// Note: ignoreESModuleAnnotation was removed from TranspiledSource
|
||||
// The package.json type:module handling is now done in the transpiler
|
||||
if (this->hasEvaluated)
|
||||
return;
|
||||
|
||||
this->sourceCode = JSC::SourceCode(WTFMove(sourceProvider));
|
||||
this->sourceCode = JSC::SourceCode(adoptRef(*sourceProvider));
|
||||
|
||||
evaluateCommonJSModuleOnce(vm, globalObject, this, this->m_dirname.get(), this->m_filename.get());
|
||||
}
|
||||
@@ -1374,7 +1369,7 @@ void JSCommonJSModule::evaluateWithPotentiallyOverriddenCompile(
|
||||
Zig::GlobalObject* globalObject,
|
||||
const WTF::String& key,
|
||||
JSValue keyJSString,
|
||||
ResolvedSource& source)
|
||||
TranspiledSource& source)
|
||||
{
|
||||
if (JSValue compileFunction = this->m_overriddenCompile.get()) {
|
||||
auto& vm = globalObject->vm();
|
||||
@@ -1390,10 +1385,7 @@ void JSCommonJSModule::evaluateWithPotentiallyOverriddenCompile(
|
||||
}
|
||||
WTF::String sourceString = source.source_code.toWTFString(BunString::ZeroCopy);
|
||||
RETURN_IF_EXCEPTION(scope, );
|
||||
if (source.needsDeref) {
|
||||
source.needsDeref = false;
|
||||
source.source_code.deref();
|
||||
}
|
||||
|
||||
// Remove the wrapper from the source string, since the transpiler has added it.
|
||||
auto trimStart = sourceString.find('\n');
|
||||
WTF::String sourceStringWithoutWrapper;
|
||||
@@ -1420,7 +1412,7 @@ void JSCommonJSModule::evaluateWithPotentiallyOverriddenCompile(
|
||||
std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSString* requireMapKey,
|
||||
ResolvedSource& source,
|
||||
TranspiledSource& source,
|
||||
bool isBuiltIn)
|
||||
{
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
@@ -1430,7 +1422,6 @@ std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
|
||||
JSValue entry = globalObject->requireMap()->get(globalObject, requireMapKey);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
bool ignoreESModuleAnnotation = source.tag == ResolvedSourceTagPackageJSONTypeModule;
|
||||
SourceOrigin sourceOrigin;
|
||||
|
||||
if (entry) {
|
||||
@@ -1457,16 +1448,15 @@ std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
globalObject->m_moduleWrapperStart,
|
||||
source.source_code.toWTFString(BunString::ZeroCopy),
|
||||
globalObject->m_moduleWrapperEnd);
|
||||
source.source_code.deref();
|
||||
source.source_code = Bun::toStringRef(concat);
|
||||
}
|
||||
|
||||
auto sourceProvider = Zig::SourceProvider::create(jsCast<Zig::GlobalObject*>(globalObject), source, JSC::SourceProviderSourceType::Program, isBuiltIn);
|
||||
auto* sourceProvider = Bun__createSourceProvider(globalObject, &source);
|
||||
sourceOrigin = sourceProvider->sourceOrigin();
|
||||
moduleObject = JSCommonJSModule::create(
|
||||
vm,
|
||||
globalObject->CommonJSModuleObjectStructure(),
|
||||
requireMapKey, filename, dirname, WTFMove(JSC::SourceCode(WTFMove(sourceProvider))));
|
||||
requireMapKey, filename, dirname, WTFMove(JSC::SourceCode(adoptRef(*sourceProvider))));
|
||||
|
||||
moduleObject->putDirect(vm,
|
||||
WebCore::clientData(vm)->builtinNames().exportsPublicName(),
|
||||
@@ -1475,10 +1465,10 @@ std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
requireMap->set(globalObject, filename, moduleObject);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
} else {
|
||||
sourceOrigin = Zig::toSourceOrigin(sourceURL, isBuiltIn);
|
||||
sourceOrigin = JSC::SourceOrigin(URL(sourceURL));
|
||||
}
|
||||
|
||||
moduleObject->ignoreESModuleAnnotation = ignoreESModuleAnnotation;
|
||||
// Note: ignoreESModuleAnnotation field removed - now handled by transpiler
|
||||
|
||||
return JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(
|
||||
|
||||
@@ -87,9 +87,9 @@ public:
|
||||
|
||||
static JSC::Structure* createStructure(JSC::JSGlobalObject* globalObject);
|
||||
|
||||
void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, ResolvedSource& resolvedSource, bool isBuiltIn);
|
||||
void evaluateWithPotentiallyOverriddenCompile(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, JSValue keyJSString, ResolvedSource& resolvedSource);
|
||||
inline void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, ResolvedSource& resolvedSource)
|
||||
void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, TranspiledSource& resolvedSource, bool isBuiltIn);
|
||||
void evaluateWithPotentiallyOverriddenCompile(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, JSValue keyJSString, TranspiledSource& resolvedSource);
|
||||
inline void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, TranspiledSource& resolvedSource)
|
||||
{
|
||||
return evaluate(globalObject, sourceURL, resolvedSource, false);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ public:
|
||||
static JSCommonJSModule* create(
|
||||
Zig::GlobalObject* globalObject,
|
||||
const WTF::String& key,
|
||||
ResolvedSource resolvedSource);
|
||||
TranspiledSource& resolvedSource);
|
||||
|
||||
static JSObject* createBoundRequireFunction(VM& vm, JSGlobalObject* lexicalGlobalObject, const WTF::String& pathString);
|
||||
|
||||
@@ -166,13 +166,13 @@ JSC::Structure* createCommonJSModuleStructure(
|
||||
std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierValue,
|
||||
ResolvedSource& source,
|
||||
TranspiledSource& source,
|
||||
bool isBuiltIn);
|
||||
|
||||
inline std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierValue,
|
||||
ResolvedSource& source)
|
||||
TranspiledSource& source)
|
||||
{
|
||||
return createCommonJSModule(globalObject, specifierValue, source, false);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <JavaScriptCore/JSInternalPromise.h>
|
||||
#include <JavaScriptCore/JSInternalFieldObjectImpl.h>
|
||||
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
|
||||
#include <JavaScriptCore/JSSourceCode.h>
|
||||
#include <JavaScriptCore/JSString.h>
|
||||
@@ -46,24 +46,6 @@ using namespace JSC;
|
||||
using namespace Zig;
|
||||
using namespace WebCore;
|
||||
|
||||
class ResolvedSourceCodeHolder {
|
||||
public:
|
||||
ResolvedSourceCodeHolder(ErrorableResolvedSource* res_)
|
||||
: res(res_)
|
||||
{
|
||||
}
|
||||
|
||||
~ResolvedSourceCodeHolder()
|
||||
{
|
||||
if (res->success && res->result.value.source_code.tag == BunStringTag::WTFStringImpl && res->result.value.needsDeref) {
|
||||
res->result.value.needsDeref = false;
|
||||
res->result.value.source_code.impl.wtf->deref();
|
||||
}
|
||||
}
|
||||
|
||||
ErrorableResolvedSource* res;
|
||||
};
|
||||
|
||||
extern "C" BunLoaderType Bun__getDefaultLoader(JSC::JSGlobalObject*, BunString* specifier);
|
||||
|
||||
static JSC::JSInternalPromise* rejectedInternalPromise(JSC::JSGlobalObject* globalObject, JSC::JSValue value)
|
||||
@@ -331,7 +313,7 @@ template<bool allowPromise>
|
||||
static JSValue handleVirtualModuleResult(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSValue virtualModuleResult,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
bool wasModuleMock = false,
|
||||
@@ -341,7 +323,6 @@ static JSValue handleVirtualModuleResult(
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
auto onLoadResult = handleOnLoadResult(globalObject, virtualModuleResult, specifier, wasModuleMock);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
|
||||
const auto reject = [&](JSC::JSValue exception) -> JSValue {
|
||||
if constexpr (allowPromise) {
|
||||
@@ -353,7 +334,6 @@ static JSValue handleVirtualModuleResult(
|
||||
};
|
||||
|
||||
const auto resolve = [&](JSValue code) -> JSValue {
|
||||
res->success = true;
|
||||
if constexpr (allowPromise) {
|
||||
scope.release();
|
||||
return resolvedInternalPromise(globalObject, code);
|
||||
@@ -372,8 +352,6 @@ static JSValue handleVirtualModuleResult(
|
||||
}
|
||||
}
|
||||
|
||||
res->success = true;
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
scope.release();
|
||||
return resolvedInternalPromise(globalObject, code);
|
||||
@@ -385,12 +363,12 @@ static JSValue handleVirtualModuleResult(
|
||||
switch (onLoadResult.type) {
|
||||
case OnLoadResultTypeCode: {
|
||||
Bun__transpileVirtualModule(globalObject, specifier, referrer, &onLoadResult.value.sourceText.string, onLoadResult.value.sourceText.loader, res);
|
||||
if (!res->success) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSValue::decode(res->result.err.value)));
|
||||
if (res->tag == ModuleResult::Tag::err) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSValue::decode(res->value.err.exception)));
|
||||
}
|
||||
|
||||
auto provider = Zig::SourceProvider::create(globalObject, res->result.value);
|
||||
return resolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(provider)));
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &res->value.transpiled);
|
||||
return resolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider))));
|
||||
}
|
||||
case OnLoadResultTypeError: {
|
||||
RELEASE_AND_RETURN(scope, reject(onLoadResult.value.error));
|
||||
@@ -457,17 +435,16 @@ static JSValue handleVirtualModuleResult(
|
||||
extern "C" void Bun__onFulfillAsyncModule(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::EncodedJSValue encodedPromiseValue,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer)
|
||||
{
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
JSC::JSInternalPromise* promise = jsCast<JSC::JSInternalPromise*>(JSC::JSValue::decode(encodedPromiseValue));
|
||||
|
||||
if (!res->success) {
|
||||
RELEASE_AND_RETURN(scope, promise->reject(globalObject, JSValue::decode(res->result.err.value)));
|
||||
if (res->tag == ModuleResult::Tag::err) {
|
||||
RELEASE_AND_RETURN(scope, promise->reject(globalObject, JSValue::decode(res->value.err.exception)));
|
||||
}
|
||||
|
||||
auto specifierValue = Bun::toJS(globalObject, *specifier);
|
||||
@@ -492,8 +469,8 @@ extern "C" void Bun__onFulfillAsyncModule(
|
||||
}
|
||||
}
|
||||
|
||||
if (res->result.value.isCommonJSModule) {
|
||||
auto created = Bun::createCommonJSModule(jsCast<Zig::GlobalObject*>(globalObject), specifierValue, res->result.value);
|
||||
if (res->tag == ModuleResult::Tag::transpiled && (res->value.transpiled.flags & 0x1)) {
|
||||
auto created = Bun::createCommonJSModule(jsCast<Zig::GlobalObject*>(globalObject), specifierValue, res->value.transpiled);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
JSSourceCode* code = JSSourceCode::create(vm, WTFMove(created.value()));
|
||||
@@ -507,11 +484,12 @@ extern "C" void Bun__onFulfillAsyncModule(
|
||||
scope.assertNoExceptionExceptTermination();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
auto&& provider = Zig::SourceProvider::create(jsDynamicCast<Zig::GlobalObject*>(globalObject), res->result.value);
|
||||
promise->resolve(globalObject, JSC::JSSourceCode::create(vm, JSC::SourceCode(provider)));
|
||||
} else if (res->tag == ModuleResult::Tag::transpiled) {
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &res->value.transpiled);
|
||||
promise->resolve(globalObject, JSC::JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider))));
|
||||
scope.assertNoExceptionExceptTermination();
|
||||
}
|
||||
// NOTE: builtin and special tags shouldn't reach here in async path
|
||||
} else {
|
||||
// the module has since been deleted from the registry.
|
||||
// let's not keep it forever for no reason.
|
||||
@@ -521,48 +499,35 @@ extern "C" void Bun__onFulfillAsyncModule(
|
||||
JSValue fetchBuiltinModuleWithoutResolution(
|
||||
Zig::GlobalObject* globalObject,
|
||||
BunString* specifier,
|
||||
ErrorableResolvedSource* res)
|
||||
ModuleResult* res)
|
||||
{
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
BunString referrer = BunStringEmpty;
|
||||
if (Bun__fetchBuiltinModule(bunVM, globalObject, specifier, &referrer, res)) {
|
||||
if (!res->success) {
|
||||
switch (res->tag) {
|
||||
case ModuleResult::Tag::err: {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto tag = res->result.value.tag;
|
||||
switch (tag) {
|
||||
// require("bun")
|
||||
case SyntheticModuleType::BunObject: {
|
||||
return globalObject->bunObject();
|
||||
}
|
||||
// require("module"), require("node:module")
|
||||
case SyntheticModuleType::NodeModule: {
|
||||
return globalObject->m_nodeModuleConstructor.getInitializedOnMainThread(globalObject);
|
||||
}
|
||||
// require("process"), require("node:process")
|
||||
case SyntheticModuleType::NodeProcess: {
|
||||
return globalObject->processObject();
|
||||
case ModuleResult::Tag::special: {
|
||||
// Special modules return JSValue directly (bun, process, module, etc.)
|
||||
return JSValue::decode(res->value.special.jsvalue);
|
||||
}
|
||||
|
||||
case SyntheticModuleType::ESM: {
|
||||
res->success = false;
|
||||
case ModuleResult::Tag::builtin: {
|
||||
// InternalModuleRegistry items
|
||||
auto result = globalObject->internalModuleRegistry()->requireId(
|
||||
globalObject, vm, static_cast<InternalModuleRegistry::Field>(res->value.builtin_id));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
return result;
|
||||
}
|
||||
|
||||
case ModuleResult::Tag::transpiled: {
|
||||
// ESM or transpiled code - signal to caller this needs further processing
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
}
|
||||
|
||||
default: {
|
||||
if (tag & SyntheticModuleType::InternalModuleRegistryFlag) {
|
||||
constexpr auto mask = (SyntheticModuleType::InternalModuleRegistryFlag - 1);
|
||||
auto result = globalObject->internalModuleRegistry()->requireId(globalObject, vm, static_cast<InternalModuleRegistry::Field>(tag & mask));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
return result;
|
||||
} else {
|
||||
res->success = false;
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
@@ -575,39 +540,25 @@ JSValue resolveAndFetchBuiltinModule(
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
if (Bun__resolveAndFetchBuiltinModule(bunVM, specifier, &res)) {
|
||||
ASSERT(res.success);
|
||||
|
||||
auto tag = res.result.value.tag;
|
||||
switch (tag) {
|
||||
// require("bun")
|
||||
case SyntheticModuleType::BunObject: {
|
||||
return globalObject->bunObject();
|
||||
}
|
||||
// require("module"), require("node:module")
|
||||
case SyntheticModuleType::NodeModule: {
|
||||
return globalObject->m_nodeModuleConstructor.getInitializedOnMainThread(globalObject);
|
||||
}
|
||||
// require("process"), require("node:process")
|
||||
case SyntheticModuleType::NodeProcess: {
|
||||
return globalObject->processObject();
|
||||
switch (res.tag) {
|
||||
case ModuleResult::Tag::special: {
|
||||
// Special modules return JSValue directly (bun, process, module, etc.)
|
||||
return JSValue::decode(res.value.special.jsvalue);
|
||||
}
|
||||
|
||||
case SyntheticModuleType::ESM: {
|
||||
return {};
|
||||
case ModuleResult::Tag::builtin: {
|
||||
// InternalModuleRegistry items
|
||||
auto result = globalObject->internalModuleRegistry()->requireId(
|
||||
globalObject, vm, static_cast<InternalModuleRegistry::Field>(res.value.builtin_id));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
return result;
|
||||
}
|
||||
|
||||
default: {
|
||||
if (tag & SyntheticModuleType::InternalModuleRegistryFlag) {
|
||||
constexpr auto mask = (SyntheticModuleType::InternalModuleRegistryFlag - 1);
|
||||
auto result = globalObject->internalModuleRegistry()->requireId(globalObject, vm, static_cast<InternalModuleRegistry::Field>(tag & mask));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
return result;
|
||||
}
|
||||
|
||||
case ModuleResult::Tag::transpiled:
|
||||
case ModuleResult::Tag::err: {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -651,12 +602,10 @@ JSValue fetchCommonJSModule(
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
ErrorableResolvedSource resValue;
|
||||
resValue.success = false;
|
||||
memset(&resValue.result, 0, sizeof resValue.result);
|
||||
ModuleResult resValue;
|
||||
memset(&resValue, 0, sizeof resValue);
|
||||
|
||||
ErrorableResolvedSource* res = &resValue;
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
ModuleResult* res = &resValue;
|
||||
|
||||
BunString specifier = Bun::toString(specifierWtfString);
|
||||
|
||||
@@ -688,8 +637,8 @@ JSValue fetchCommonJSModule(
|
||||
RELEASE_AND_RETURN(scope, JSValue {});
|
||||
}
|
||||
case JSPromise::Status::Fulfilled: {
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
if (res->tag == ModuleResult::Tag::err) {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
if (!wasModuleMock) {
|
||||
@@ -706,7 +655,7 @@ JSValue fetchCommonJSModule(
|
||||
auto builtin = fetchBuiltinModuleWithoutResolution(globalObject, &specifier, res);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
if (builtin) {
|
||||
if (!res->success) {
|
||||
if (res->tag == ModuleResult::Tag::err) {
|
||||
RELEASE_AND_RETURN(scope, builtin);
|
||||
}
|
||||
target->setExportsObject(builtin);
|
||||
@@ -739,8 +688,8 @@ JSValue fetchCommonJSModule(
|
||||
RELEASE_AND_RETURN(scope, JSValue {});
|
||||
}
|
||||
case JSPromise::Status::Fulfilled: {
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
if (res->tag == ModuleResult::Tag::err) {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
if (!wasModuleMock) {
|
||||
@@ -787,75 +736,73 @@ JSValue fetchCommonJSModuleNonBuiltin(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
JSC::ThrowScope& scope)
|
||||
{
|
||||
Bun__transpileFile(bunVM, globalObject, specifier, referrer, typeAttribute, res, false, !isExtension, forceLoaderType);
|
||||
if (res->success && res->result.value.isCommonJSModule) {
|
||||
if constexpr (isExtension) {
|
||||
target->evaluateWithPotentiallyOverriddenCompile(globalObject, specifierWtfString, specifierValue, res->result.value);
|
||||
} else {
|
||||
target->evaluate(globalObject, specifierWtfString, res->result.value);
|
||||
}
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
}
|
||||
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
switch (res->tag) {
|
||||
case ModuleResult::Tag::err: {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
|
||||
// The JSONForObjectLoader tag is source code returned from Bun that needs
|
||||
// to go through the JSON parser in JSC.
|
||||
//
|
||||
// We don't use JSON.parse directly in JS because we want the top-level keys of the JSON
|
||||
// object to be accessible as named imports.
|
||||
//
|
||||
// We don't use Bun's JSON parser because JSON.parse is faster and
|
||||
// handles stack overflow better.
|
||||
//
|
||||
// When parsing tsconfig.*.json or jsconfig.*.json, we go through Bun's JSON
|
||||
// parser instead to support comments and trailing commas.
|
||||
if (res->result.value.tag == SyntheticModuleType::JSONForObjectLoader) {
|
||||
WTF::String jsonSource = res->result.value.source_code.toWTFString(BunString::NonNull);
|
||||
JSC::JSValue value = JSC::JSONParseWithException(globalObject, jsonSource);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
|
||||
target->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), value, 0);
|
||||
target->hasEvaluated = true;
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
|
||||
}
|
||||
// TOML and JSONC may go through here
|
||||
else if (res->result.value.tag == SyntheticModuleType::ExportsObject || res->result.value.tag == SyntheticModuleType::ExportDefaultObject) {
|
||||
JSC::JSValue value = JSC::JSValue::decode(res->result.value.jsvalue_for_export);
|
||||
if (!value) {
|
||||
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Failed to parse Object"_s));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
case ModuleResult::Tag::transpiled: {
|
||||
if (res->value.transpiled.flags & 0x1) {
|
||||
if constexpr (isExtension) {
|
||||
target->evaluateWithPotentiallyOverriddenCompile(globalObject, specifierWtfString, specifierValue, res->value.transpiled);
|
||||
} else {
|
||||
target->evaluate(globalObject, specifierWtfString, res->value.transpiled);
|
||||
}
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
}
|
||||
|
||||
target->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), value, 0);
|
||||
target->hasEvaluated = true;
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
} else if (res->result.value.tag == SyntheticModuleType::CommonJSCustomExtension) {
|
||||
if constexpr (isExtension) {
|
||||
ASSERT_NOT_REACHED();
|
||||
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Recursive extension. This is a bug in Bun"_s));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
evaluateCommonJSCustomExtension(globalObject, target, specifierWtfString, specifierValue, JSC::JSValue::decode(res->result.value.cjsCustomExtension));
|
||||
// ESM - provide fetch and signal to caller
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &res->value.transpiled);
|
||||
globalObject->moduleLoader()->provideFetch(globalObject, specifierValue, JSC::SourceCode(adoptRef(*provider)));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
}
|
||||
|
||||
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value);
|
||||
globalObject->moduleLoader()->provideFetch(globalObject, specifierValue, JSC::SourceCode(provider));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
case ModuleResult::Tag::special: {
|
||||
auto& special = res->value.special;
|
||||
switch (special.tag) {
|
||||
case SpecialModule::Tag::exports_object:
|
||||
case SpecialModule::Tag::export_default_object: {
|
||||
JSC::JSValue value = JSC::JSValue::decode(special.jsvalue);
|
||||
if (!value) {
|
||||
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Failed to parse Object"_s));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
|
||||
target->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), value, 0);
|
||||
target->hasEvaluated = true;
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
}
|
||||
|
||||
case SpecialModule::Tag::custom_extension: {
|
||||
if constexpr (isExtension) {
|
||||
ASSERT_NOT_REACHED();
|
||||
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Recursive extension. This is a bug in Bun"_s));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
evaluateCommonJSCustomExtension(globalObject, target, specifierWtfString, specifierValue, JSC::JSValue::decode(special.jsvalue));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case ModuleResult::Tag::builtin: {
|
||||
// Shouldn't reach here for CommonJS
|
||||
ASSERT_NOT_REACHED();
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Explicit instantiations of fetchCommonJSModuleNonBuiltin
|
||||
@@ -867,7 +814,7 @@ template JSValue fetchCommonJSModuleNonBuiltin<true>(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
@@ -880,7 +827,7 @@ template JSValue fetchCommonJSModuleNonBuiltin<false>(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
@@ -892,7 +839,7 @@ template<bool allowPromise>
|
||||
static JSValue fetchESMSourceCode(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute)
|
||||
@@ -900,7 +847,6 @@ static JSValue fetchESMSourceCode(
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
|
||||
const auto reject = [&](JSC::JSValue exception) -> JSValue {
|
||||
if constexpr (allowPromise) {
|
||||
@@ -944,58 +890,53 @@ static JSValue fetchESMSourceCode(
|
||||
}
|
||||
|
||||
if (Bun__fetchBuiltinModule(bunVM, globalObject, specifier, referrer, res)) {
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
switch (res->tag) {
|
||||
case ModuleResult::Tag::err: {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, reject(exception));
|
||||
}
|
||||
|
||||
// This can happen if it's a `bun build --compile`'d CommonJS file
|
||||
if (res->result.value.isCommonJSModule) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, res->result.value);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
case ModuleResult::Tag::transpiled: {
|
||||
auto& transpiled = res->value.transpiled;
|
||||
// This can happen if it's a `bun build --compile`'d CommonJS file
|
||||
if (transpiled.flags & 0x1) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, transpiled);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
// ESM builtin
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &transpiled);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider)))));
|
||||
}
|
||||
|
||||
auto moduleKey = specifier->toWTFString(BunString::ZeroCopy);
|
||||
|
||||
auto tag = res->result.value.tag;
|
||||
switch (tag) {
|
||||
case SyntheticModuleType::ESM: {
|
||||
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value, JSC::SourceProviderSourceType::Module, true);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, JSC::SourceCode(provider))));
|
||||
case ModuleResult::Tag::builtin: {
|
||||
// InternalModuleRegistry item - generate synthetic source
|
||||
auto moduleKey = specifier->toWTFString(BunString::ZeroCopy);
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(
|
||||
generateInternalModuleSourceCode(globalObject, static_cast<InternalModuleRegistry::Field>(res->value.builtin_id)),
|
||||
JSC::SourceOrigin(URL(makeString("builtins://"_s, moduleKey))),
|
||||
moduleKey));
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))));
|
||||
}
|
||||
|
||||
#define CASE(str, name) \
|
||||
case (SyntheticModuleType::name): { \
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(generateNativeModule_##name, JSC::SourceOrigin(), WTFMove(moduleKey))); \
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source)))); \
|
||||
}
|
||||
BUN_FOREACH_ESM_NATIVE_MODULE(CASE)
|
||||
#undef CASE
|
||||
|
||||
// CommonJS modules from src/js/*
|
||||
default: {
|
||||
if (tag & SyntheticModuleType::InternalModuleRegistryFlag) {
|
||||
constexpr auto mask = (SyntheticModuleType::InternalModuleRegistryFlag - 1);
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(generateInternalModuleSourceCode(globalObject, static_cast<InternalModuleRegistry::Field>(tag & mask)), JSC::SourceOrigin(URL(makeString("builtins://"_s, moduleKey))), moduleKey));
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))));
|
||||
} else {
|
||||
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value, JSC::SourceProviderSourceType::Module, true);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(provider))));
|
||||
}
|
||||
case ModuleResult::Tag::special: {
|
||||
// Special modules shouldn't reach ESM import path
|
||||
ASSERT_NOT_REACHED();
|
||||
RELEASE_AND_RETURN(scope, reject(createError(globalObject, "Unexpected special module in ESM import"_s)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1018,100 +959,85 @@ static JSValue fetchESMSourceCode(
|
||||
Bun__transpileFile(bunVM, globalObject, specifier, referrer, typeAttribute, res, false, false, BunLoaderTypeNone);
|
||||
}
|
||||
|
||||
if (res->success && res->result.value.isCommonJSModule) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, res->result.value);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
switch (res->tag) {
|
||||
case ModuleResult::Tag::err: {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, reject(exception));
|
||||
}
|
||||
|
||||
// The JSONForObjectLoader tag is source code returned from Bun that needs
|
||||
// to go through the JSON parser in JSC.
|
||||
//
|
||||
// We don't use JSON.parse directly in JS because we want the top-level keys of the JSON
|
||||
// object to be accessible as named imports.
|
||||
//
|
||||
// We don't use Bun's JSON parser because JSON.parse is faster and
|
||||
// handles stack overflow better.
|
||||
//
|
||||
// When parsing tsconfig.*.json or jsconfig.*.json, we go through Bun's JSON
|
||||
// parser instead to support comments and trailing commas.
|
||||
if (res->result.value.tag == SyntheticModuleType::JSONForObjectLoader) {
|
||||
WTF::String jsonSource = res->result.value.source_code.toWTFString(BunString::NonNull);
|
||||
JSC::JSValue value = JSC::JSONParseWithException(globalObject, jsonSource);
|
||||
if (scope.exception()) [[unlikely]] {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, reject(exception));
|
||||
case ModuleResult::Tag::transpiled: {
|
||||
auto& transpiled = res->value.transpiled;
|
||||
if (transpiled.flags & 0x1) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, transpiled);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
// JSON can become strings, null, numbers, booleans so we must handle "export default 123"
|
||||
auto function = generateJSValueModuleSourceCode(
|
||||
globalObject,
|
||||
value);
|
||||
auto source = JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(function),
|
||||
JSC::SourceOrigin(), specifier->toWTFString(BunString::ZeroCopy)));
|
||||
JSC::ensureStillAliveHere(value);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))));
|
||||
// Regular ESM
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &transpiled);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider)))));
|
||||
}
|
||||
// TOML and JSONC may go through here
|
||||
else if (res->result.value.tag == SyntheticModuleType::ExportsObject) {
|
||||
JSC::JSValue value = JSC::JSValue::decode(res->result.value.jsvalue_for_export);
|
||||
|
||||
case ModuleResult::Tag::special: {
|
||||
auto& special = res->value.special;
|
||||
JSC::JSValue value = JSC::JSValue::decode(special.jsvalue);
|
||||
if (!value) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSC::createSyntaxError(globalObject, "Failed to parse Object"_s)));
|
||||
}
|
||||
|
||||
// JSON can become strings, null, numbers, booleans so we must handle "export default 123"
|
||||
auto function = generateJSValueModuleSourceCode(
|
||||
globalObject,
|
||||
value);
|
||||
auto source = JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(function),
|
||||
JSC::SourceOrigin(), specifier->toWTFString(BunString::ZeroCopy)));
|
||||
JSC::ensureStillAliveHere(value);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))));
|
||||
} else if (res->result.value.tag == SyntheticModuleType::ExportDefaultObject) {
|
||||
JSC::JSValue value = JSC::JSValue::decode(res->result.value.jsvalue_for_export);
|
||||
if (!value) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSC::createSyntaxError(globalObject, "Failed to parse Object"_s)));
|
||||
// Generate synthetic source based on special module type
|
||||
JSC::SyntheticSourceProvider::SyntheticSourceGenerator generator;
|
||||
switch (special.tag) {
|
||||
case SpecialModule::Tag::exports_object: {
|
||||
generator = generateJSValueModuleSourceCode(globalObject, value);
|
||||
break;
|
||||
}
|
||||
case SpecialModule::Tag::export_default_object: {
|
||||
generator = generateJSValueExportDefaultObjectSourceCode(globalObject, value);
|
||||
break;
|
||||
}
|
||||
case SpecialModule::Tag::custom_extension: {
|
||||
// Shouldn't reach here for ESM
|
||||
RELEASE_AND_RETURN(scope, reject(createError(globalObject, "Unexpected custom extension in ESM import"_s)));
|
||||
}
|
||||
}
|
||||
|
||||
// JSON can become strings, null, numbers, booleans so we must handle "export default 123"
|
||||
auto function = generateJSValueExportDefaultObjectSourceCode(
|
||||
globalObject,
|
||||
value);
|
||||
auto source = JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(function),
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(generator),
|
||||
JSC::SourceOrigin(), specifier->toWTFString(BunString::ZeroCopy)));
|
||||
JSC::ensureStillAliveHere(value);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))));
|
||||
}
|
||||
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(Zig::SourceProvider::create(globalObject, res->result.value)))));
|
||||
case ModuleResult::Tag::builtin: {
|
||||
// InternalModuleRegistry - generate synthetic source
|
||||
auto moduleKey = specifier->toWTFString(BunString::ZeroCopy);
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(
|
||||
generateInternalModuleSourceCode(globalObject, static_cast<InternalModuleRegistry::Field>(res->value.builtin_id)),
|
||||
JSC::SourceOrigin(URL(makeString("builtins://"_s, moduleKey))),
|
||||
moduleKey));
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JSValue fetchESMSourceCodeSync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute)
|
||||
@@ -1122,7 +1048,7 @@ JSValue fetchESMSourceCodeSync(
|
||||
JSValue fetchESMSourceCodeAsync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute)
|
||||
@@ -1136,9 +1062,8 @@ using namespace Bun;
|
||||
BUN_DEFINE_HOST_FUNCTION(jsFunctionOnLoadObjectResultResolve, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
|
||||
{
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
JSC::JSValue objectResult = callFrame->argument(0);
|
||||
PendingVirtualModuleResult* pendingModule = JSC::jsCast<PendingVirtualModuleResult*>(callFrame->argument(1));
|
||||
JSC::JSValue specifierString = pendingModule->internalField(0).get();
|
||||
@@ -1154,8 +1079,8 @@ BUN_DEFINE_HOST_FUNCTION(jsFunctionOnLoadObjectResultResolve, (JSC::JSGlobalObje
|
||||
bool wasModuleMock = pendingModule->wasModuleMock;
|
||||
|
||||
JSC::JSValue result = handleVirtualModuleResult<false>(static_cast<Zig::GlobalObject*>(globalObject), objectResult, &res, &specifier, &referrer, wasModuleMock);
|
||||
if (!scope.exception() && !res.success) [[unlikely]] {
|
||||
throwException(globalObject, scope, result);
|
||||
if (!scope.exception() && res.tag == ModuleResult::Tag::err) [[unlikely]] {
|
||||
throwException(globalObject, scope, JSValue::decode(res.value.err.exception));
|
||||
}
|
||||
if (scope.exception()) [[unlikely]] {
|
||||
auto retValue = JSValue::encode(promise->rejectWithCaughtException(globalObject, scope));
|
||||
|
||||
@@ -93,7 +93,7 @@ public:
|
||||
JSValue fetchESMSourceCodeSync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSString* spceifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute);
|
||||
@@ -101,7 +101,7 @@ JSValue fetchESMSourceCodeSync(
|
||||
JSValue fetchESMSourceCodeAsync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSString* spceifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute);
|
||||
@@ -123,7 +123,7 @@ JSValue fetchCommonJSModuleNonBuiltin(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
@@ -136,6 +136,6 @@ JSValue resolveAndFetchBuiltinModule(
|
||||
JSValue fetchBuiltinModuleWithoutResolution(
|
||||
Zig::GlobalObject* globalObject,
|
||||
BunString* specifier,
|
||||
ErrorableResolvedSource* res);
|
||||
ModuleResult* res);
|
||||
|
||||
} // namespace Bun
|
||||
|
||||
32
src/bun.js/bindings/ModuleResult.zig
Normal file
32
src/bun.js/bindings/ModuleResult.zig
Normal file
@@ -0,0 +1,32 @@
|
||||
/// Error information from transpilation/module loading
|
||||
pub const ErrorResult = extern struct {
|
||||
/// Error value (JSValue) - already an exception
|
||||
exception: JSValue,
|
||||
};
|
||||
|
||||
/// Tagged union return type from transpiler
|
||||
/// src/bun.js/bindings/ModuleResult.zig
|
||||
pub const ModuleResult = extern struct {
|
||||
tag: Tag,
|
||||
value: extern union {
|
||||
transpiled: TranspiledSource,
|
||||
special: SpecialModule,
|
||||
builtin_id: u32,
|
||||
err: ErrorResult,
|
||||
},
|
||||
|
||||
pub const Tag = enum(u8) {
|
||||
transpiled,
|
||||
special,
|
||||
builtin,
|
||||
err,
|
||||
};
|
||||
|
||||
// Re-export ErrorResult for convenience
|
||||
pub const Error = ErrorResult;
|
||||
};
|
||||
|
||||
const bun = @import("bun");
|
||||
const SpecialModule = @import("./SpecialModule.zig").SpecialModule;
|
||||
const TranspiledSource = @import("./TranspiledSource.zig").TranspiledSource;
|
||||
const JSValue = bun.jsc.JSValue;
|
||||
18
src/bun.js/bindings/SpecialModule.zig
Normal file
18
src/bun.js/bindings/SpecialModule.zig
Normal file
@@ -0,0 +1,18 @@
|
||||
/// For special cases that need JSValue handling
|
||||
/// src/bun.js/bindings/SpecialModule.zig
|
||||
pub const SpecialModule = extern struct {
|
||||
tag: Tag,
|
||||
jsvalue: JSValue,
|
||||
|
||||
pub const Tag = enum(u8) {
|
||||
/// Return exports object directly
|
||||
exports_object,
|
||||
/// Return default export only
|
||||
export_default_object,
|
||||
/// Call custom require.extensions handler
|
||||
custom_extension,
|
||||
};
|
||||
};
|
||||
|
||||
const bun = @import("bun");
|
||||
const JSValue = bun.jsc.JSValue;
|
||||
26
src/bun.js/bindings/TranspiledSource.zig
Normal file
26
src/bun.js/bindings/TranspiledSource.zig
Normal file
@@ -0,0 +1,26 @@
|
||||
/// Minimal POD struct for transpiled source code
|
||||
/// Can be safely created on worker threads
|
||||
/// src/bun.js/bindings/TranspiledSource.zig
|
||||
pub const TranspiledSource = extern struct {
|
||||
/// Transpiled source code (Latin1 or UTF16)
|
||||
/// Ownership transfers to C++ on return
|
||||
source_code: bun.String,
|
||||
|
||||
/// Module specifier for debugging/sourcemaps
|
||||
source_url: bun.String,
|
||||
|
||||
/// Optional bytecode cache (for bun build --compile)
|
||||
bytecode_cache: ?[*]u8 = null,
|
||||
bytecode_cache_len: usize = 0,
|
||||
|
||||
/// Packed flags
|
||||
flags: Flags = .{},
|
||||
|
||||
pub const Flags = packed struct(u32) {
|
||||
is_commonjs: bool = false,
|
||||
is_already_bundled: bool = false,
|
||||
_padding: u30 = 0,
|
||||
};
|
||||
};
|
||||
|
||||
const bun = @import("bun");
|
||||
@@ -153,7 +153,7 @@
|
||||
#include "webcrypto/JSCryptoKey.h"
|
||||
#include "webcrypto/JSSubtleCrypto.h"
|
||||
#include "ZigGeneratedClasses.h"
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
#include "UtilInspect.h"
|
||||
#include "Base64Helpers.h"
|
||||
#include "wtf/text/OrdinalNumber.h"
|
||||
@@ -577,11 +577,9 @@ JSC_DEFINE_HOST_FUNCTION(functionFulfillModuleSync,
|
||||
}
|
||||
|
||||
auto specifier = Bun::toString(moduleKey);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
// zero-initialize entire result union. zeroed BunString has BunStringTag::Dead, and zeroed
|
||||
// EncodedJSValues are empty, which our code should be handling
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
// zero-initialize entire result union
|
||||
memset(&res.value, 0, sizeof res.value);
|
||||
|
||||
JSValue result = Bun::fetchESMSourceCodeSync(
|
||||
globalObject,
|
||||
@@ -3309,11 +3307,9 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderFetch(JSGlobalObject* globalOb
|
||||
|
||||
auto source = Bun::toString(sourceString);
|
||||
auto typeAttribute = Bun::toString(typeAttributeString);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
// zero-initialize entire result union. zeroed BunString has BunStringTag::Dead, and zeroed
|
||||
// EncodedJSValues are empty, which our code should be handling
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
// zero-initialize entire result union
|
||||
memset(&res.value, 0, sizeof res.value);
|
||||
|
||||
JSValue result = Bun::fetchESMSourceCodeAsync(
|
||||
static_cast<Zig::GlobalObject*>(globalObject),
|
||||
|
||||
@@ -1,368 +0,0 @@
|
||||
#include "root.h"
|
||||
|
||||
#include "helpers.h"
|
||||
|
||||
#include "ZigSourceProvider.h"
|
||||
|
||||
#include <JavaScriptCore/BytecodeCacheError.h>
|
||||
#include "ZigGlobalObject.h"
|
||||
#include "wtf/Assertions.h"
|
||||
|
||||
#include <JavaScriptCore/Completion.h>
|
||||
#include <wtf/Scope.h>
|
||||
#include <wtf/text/StringHash.h>
|
||||
#include <sys/stat.h>
|
||||
#include <JavaScriptCore/SourceCodeKey.h>
|
||||
#include <mimalloc.h>
|
||||
#include <JavaScriptCore/CodeCache.h>
|
||||
|
||||
namespace Zig {
|
||||
|
||||
using Base = JSC::SourceProvider;
|
||||
using BytecodeCacheGenerator = JSC::BytecodeCacheGenerator;
|
||||
using UnlinkedFunctionExecutable = JSC::UnlinkedFunctionExecutable;
|
||||
using CachedBytecode = JSC::CachedBytecode;
|
||||
using UnlinkedFunctionCodeBlock = JSC::UnlinkedFunctionCodeBlock;
|
||||
using SourceCode = JSC::SourceCode;
|
||||
using CodeSpecializationKind = JSC::CodeSpecializationKind;
|
||||
using SourceOrigin = JSC::SourceOrigin;
|
||||
using String = WTF::String;
|
||||
using SourceProviderSourceType = JSC::SourceProviderSourceType;
|
||||
|
||||
SourceOrigin toSourceOrigin(const String& sourceURL, bool isBuiltin)
|
||||
{
|
||||
|
||||
ASSERT_WITH_MESSAGE(!sourceURL.startsWith("file://"_s), "specifier should not already be a file URL");
|
||||
|
||||
if (isBuiltin) {
|
||||
if (sourceURL.startsWith("node:"_s)) {
|
||||
return SourceOrigin(WTF::URL(makeString("builtin://node/"_s, sourceURL.substring(5))));
|
||||
} else if (sourceURL.startsWith("bun:"_s)) {
|
||||
return SourceOrigin(WTF::URL(makeString("builtin://bun/"_s, sourceURL.substring(4))));
|
||||
} else {
|
||||
return SourceOrigin(WTF::URL(makeString("builtin://"_s, sourceURL)));
|
||||
}
|
||||
}
|
||||
return SourceOrigin(WTF::URL::fileURLWithFileSystemPath(sourceURL));
|
||||
}
|
||||
|
||||
extern "C" int ByteRangeMapping__getSourceID(void* mappings, BunString sourceURL);
|
||||
extern "C" void* ByteRangeMapping__find(BunString sourceURL);
|
||||
void* sourceMappingForSourceURL(const WTF::String& sourceURL)
|
||||
{
|
||||
return ByteRangeMapping__find(Bun::toString(sourceURL));
|
||||
}
|
||||
|
||||
extern "C" void ByteRangeMapping__generate(BunString sourceURL, BunString code, int sourceID);
|
||||
|
||||
JSC::SourceID sourceIDForSourceURL(const WTF::String& sourceURL)
|
||||
{
|
||||
void* mappings = ByteRangeMapping__find(Bun::toString(sourceURL));
|
||||
if (!mappings) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ByteRangeMapping__getSourceID(mappings, Bun::toString(sourceURL));
|
||||
}
|
||||
|
||||
extern "C" bool BunTest__shouldGenerateCodeCoverage(BunString sourceURL);
|
||||
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);
|
||||
|
||||
bool isCodeCoverageEnabled = !!globalObject->vm().controlFlowProfiler();
|
||||
|
||||
bool shouldGenerateCodeCoverage = isCodeCoverageEnabled && !isBuiltin && BunTest__shouldGenerateCodeCoverage(resolvedSource.source_url);
|
||||
|
||||
if (resolvedSource.needsDeref && !isBuiltin) {
|
||||
resolvedSource.needsDeref = false;
|
||||
resolvedSource.source_code.deref();
|
||||
// Do not deref either source_url or specifier
|
||||
// Specifier's lifetime is the JSValue, mostly
|
||||
// source_url is owned by the string above
|
||||
// https://github.com/oven-sh/bun/issues/9521
|
||||
}
|
||||
|
||||
const auto getProvider = [&]() -> Ref<SourceProvider> {
|
||||
if (resolvedSource.bytecode_cache != nullptr) {
|
||||
const auto destructorPtr = [](const void* ptr) {
|
||||
mi_free(const_cast<void*>(ptr));
|
||||
};
|
||||
const auto destructorNoOp = [](const void* ptr) {
|
||||
// no-op, for bun build --compile.
|
||||
};
|
||||
const auto destructor = resolvedSource.needsDeref ? destructorPtr : destructorNoOp;
|
||||
|
||||
Ref<JSC::CachedBytecode> bytecode = JSC::CachedBytecode::create(std::span<uint8_t>(resolvedSource.bytecode_cache, resolvedSource.bytecode_cache_size), destructor, {});
|
||||
auto provider = adoptRef(*new SourceProvider(
|
||||
globalObject->isThreadLocalDefaultGlobalObject ? globalObject : nullptr,
|
||||
resolvedSource,
|
||||
string.isNull() ? *StringImpl::empty() : *string.impl(),
|
||||
JSC::SourceTaintedOrigin::Untainted,
|
||||
toSourceOrigin(sourceURLString, isBuiltin),
|
||||
sourceURLString.impl(), TextPosition(),
|
||||
sourceType));
|
||||
provider->m_cachedBytecode = WTFMove(bytecode);
|
||||
return provider;
|
||||
}
|
||||
|
||||
return adoptRef(*new SourceProvider(
|
||||
globalObject->isThreadLocalDefaultGlobalObject ? globalObject : nullptr,
|
||||
resolvedSource,
|
||||
string.isNull() ? *StringImpl::empty() : *string.impl(),
|
||||
JSC::SourceTaintedOrigin::Untainted,
|
||||
toSourceOrigin(sourceURLString, isBuiltin),
|
||||
sourceURLString.impl(), TextPosition(),
|
||||
sourceType));
|
||||
};
|
||||
|
||||
auto provider = getProvider();
|
||||
|
||||
if (shouldGenerateCodeCoverage) {
|
||||
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;
|
||||
}
|
||||
|
||||
StringView SourceProvider::source() const
|
||||
{
|
||||
return StringView(m_source.get());
|
||||
}
|
||||
|
||||
SourceProvider::~SourceProvider()
|
||||
{
|
||||
if (m_resolvedSource.already_bundled) {
|
||||
BunString str = Bun::toString(sourceURL());
|
||||
Bun__removeSourceProviderSourceMap(m_globalObject->bunVM(), this, &str);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void CachedBytecode__deref(JSC::CachedBytecode* cachedBytecode)
|
||||
{
|
||||
cachedBytecode->deref();
|
||||
}
|
||||
|
||||
static JSC::VM& getVMForBytecodeCache()
|
||||
{
|
||||
static thread_local JSC::VM* vmForBytecodeCache = nullptr;
|
||||
if (!vmForBytecodeCache) {
|
||||
const auto heapSize = JSC::HeapType::Small;
|
||||
auto vmPtr = JSC::VM::tryCreate(heapSize);
|
||||
vmPtr->refSuppressingSaferCPPChecking();
|
||||
vmForBytecodeCache = vmPtr.get();
|
||||
vmPtr->heap.acquireAccess();
|
||||
}
|
||||
return *vmForBytecodeCache;
|
||||
}
|
||||
|
||||
extern "C" bool generateCachedModuleByteCodeFromSourceCode(BunString* sourceProviderURL, const Latin1Character* inputSourceCode, size_t inputSourceCodeSize, const uint8_t** outputByteCode, size_t* outputByteCodeSize, JSC::CachedBytecode** cachedBytecodePtr)
|
||||
{
|
||||
std::span<const Latin1Character> sourceCodeSpan(inputSourceCode, inputSourceCodeSize);
|
||||
JSC::SourceCode sourceCode = JSC::makeSource(WTF::String(sourceCodeSpan), toSourceOrigin(sourceProviderURL->toWTFString(), false), JSC::SourceTaintedOrigin::Untainted);
|
||||
|
||||
JSC::VM& vm = getVMForBytecodeCache();
|
||||
|
||||
JSC::JSLockHolder locker(vm);
|
||||
LexicallyScopedFeatures lexicallyScopedFeatures = StrictModeLexicallyScopedFeature;
|
||||
JSParserScriptMode scriptMode = JSParserScriptMode::Module;
|
||||
EvalContextType evalContextType = EvalContextType::None;
|
||||
|
||||
ParserError parserError;
|
||||
UnlinkedModuleProgramCodeBlock* unlinkedCodeBlock = JSC::recursivelyGenerateUnlinkedCodeBlockForModuleProgram(vm, sourceCode, lexicallyScopedFeatures, scriptMode, {}, parserError, evalContextType);
|
||||
if (parserError.isValid())
|
||||
return false;
|
||||
if (!unlinkedCodeBlock)
|
||||
return false;
|
||||
|
||||
auto key = JSC::sourceCodeKeyForSerializedModule(vm, sourceCode);
|
||||
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeCodeBlock(vm, key, unlinkedCodeBlock);
|
||||
if (!cachedBytecode)
|
||||
return false;
|
||||
|
||||
cachedBytecode->ref();
|
||||
*cachedBytecodePtr = cachedBytecode.get();
|
||||
*outputByteCode = cachedBytecode->span().data();
|
||||
*outputByteCodeSize = cachedBytecode->span().size();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool generateCachedCommonJSProgramByteCodeFromSourceCode(BunString* sourceProviderURL, const Latin1Character* inputSourceCode, size_t inputSourceCodeSize, const uint8_t** outputByteCode, size_t* outputByteCodeSize, JSC::CachedBytecode** cachedBytecodePtr)
|
||||
{
|
||||
std::span<const Latin1Character> sourceCodeSpan(inputSourceCode, inputSourceCodeSize);
|
||||
|
||||
JSC::SourceCode sourceCode = JSC::makeSource(WTF::String(sourceCodeSpan), toSourceOrigin(sourceProviderURL->toWTFString(), false), JSC::SourceTaintedOrigin::Untainted);
|
||||
JSC::VM& vm = getVMForBytecodeCache();
|
||||
|
||||
JSC::JSLockHolder locker(vm);
|
||||
LexicallyScopedFeatures lexicallyScopedFeatures = NoLexicallyScopedFeatures;
|
||||
JSParserScriptMode scriptMode = JSParserScriptMode::Classic;
|
||||
EvalContextType evalContextType = EvalContextType::None;
|
||||
|
||||
ParserError parserError;
|
||||
UnlinkedProgramCodeBlock* unlinkedCodeBlock = JSC::recursivelyGenerateUnlinkedCodeBlockForProgram(vm, sourceCode, lexicallyScopedFeatures, scriptMode, {}, parserError, evalContextType);
|
||||
if (parserError.isValid())
|
||||
return false;
|
||||
if (!unlinkedCodeBlock)
|
||||
return false;
|
||||
|
||||
auto key = JSC::sourceCodeKeyForSerializedProgram(vm, sourceCode);
|
||||
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeCodeBlock(vm, key, unlinkedCodeBlock);
|
||||
if (!cachedBytecode)
|
||||
return false;
|
||||
|
||||
cachedBytecode->ref();
|
||||
*cachedBytecodePtr = cachedBytecode.get();
|
||||
*outputByteCode = cachedBytecode->span().data();
|
||||
*outputByteCodeSize = cachedBytecode->span().size();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned SourceProvider::hash() const
|
||||
{
|
||||
if (m_hash) {
|
||||
return m_hash;
|
||||
}
|
||||
|
||||
return m_source->hash();
|
||||
}
|
||||
|
||||
void SourceProvider::freeSourceCode()
|
||||
{
|
||||
}
|
||||
|
||||
void SourceProvider::updateCache(const UnlinkedFunctionExecutable* executable, const SourceCode&,
|
||||
CodeSpecializationKind kind,
|
||||
const UnlinkedFunctionCodeBlock* codeBlock)
|
||||
{
|
||||
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode)
|
||||
return;
|
||||
|
||||
JSC::BytecodeCacheError error;
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeFunctionCodeBlock(executable->vm(), codeBlock, error);
|
||||
if (cachedBytecode && !error.isValid())
|
||||
m_cachedBytecode->addFunctionUpdate(executable, kind, *cachedBytecode);
|
||||
}
|
||||
|
||||
void SourceProvider::cacheBytecode(const BytecodeCacheGenerator& generator)
|
||||
{
|
||||
// if (!m_resolvedSource.bytecodecache_fd)
|
||||
return;
|
||||
|
||||
if (!m_cachedBytecode)
|
||||
m_cachedBytecode = JSC::CachedBytecode::create();
|
||||
auto update = generator();
|
||||
if (update)
|
||||
m_cachedBytecode->addGlobalUpdate(*update);
|
||||
}
|
||||
|
||||
void SourceProvider::commitCachedBytecode()
|
||||
{
|
||||
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode || !m_cachedBytecode->hasUpdates())
|
||||
return;
|
||||
|
||||
// auto clearBytecode = WTF::makeScopeExit([&] { m_cachedBytecode = nullptr; });
|
||||
// const auto fd = m_resolvedSource.bytecodecache_fd;
|
||||
|
||||
// auto fileSize = FileSystem::fileSize(fd);
|
||||
// if (!fileSize)
|
||||
// return;
|
||||
|
||||
// size_t cacheFileSize;
|
||||
// if (!WTF::convertSafely(*fileSize, cacheFileSize) || cacheFileSize != m_cachedBytecode->size()) {
|
||||
// // The bytecode cache has already been updated
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (!FileSystem::truncateFile(fd, m_cachedBytecode->sizeForUpdate()))
|
||||
// return;
|
||||
|
||||
// m_cachedBytecode->commitUpdates([&](off_t offset, const void* data, size_t size) {
|
||||
// long long result = FileSystem::seekFile(fd, offset, FileSystem::FileSeekOrigin::Beginning);
|
||||
// ASSERT_UNUSED(result, result != -1);
|
||||
// size_t bytesWritten = static_cast<size_t>(FileSystem::writeToFile(fd, data, size));
|
||||
// ASSERT_UNUSED(bytesWritten, bytesWritten == size);
|
||||
// });
|
||||
}
|
||||
|
||||
bool SourceProvider::isBytecodeCacheEnabled() const
|
||||
{
|
||||
// return m_resolvedSource.bytecodecache_fd > 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
void SourceProvider::readOrGenerateByteCodeCache(JSC::VM& vm, const JSC::SourceCode& sourceCode)
|
||||
{
|
||||
// auto status = this->readCache(vm, sourceCode);
|
||||
// switch (status) {
|
||||
// case -1: {
|
||||
// m_resolvedSource.bytecodecache_fd = 0;
|
||||
// break;
|
||||
// }
|
||||
// case 0: {
|
||||
// JSC::BytecodeCacheError err;
|
||||
// m_cachedBytecode = JSC::generateModuleBytecode(vm, sourceCode, m_resolvedSource.bytecodecache_fd, err);
|
||||
|
||||
// if (err.isValid()) {
|
||||
// m_resolvedSource.bytecodecache_fd = 0;
|
||||
// m_cachedBytecode = JSC::CachedBytecode::create();
|
||||
// }
|
||||
// }
|
||||
// // TODO: read the bytecode into a JSC::SourceCode object here
|
||||
// case 1: {
|
||||
// }
|
||||
// }
|
||||
}
|
||||
int SourceProvider::readCache(JSC::VM& vm, const JSC::SourceCode& sourceCode)
|
||||
{
|
||||
return -1;
|
||||
// if (m_resolvedSource.bytecodecache_fd == 0)
|
||||
// return -1;
|
||||
// if (!FileSystem::isHandleValid(m_resolvedSource.bytecodecache_fd))
|
||||
// return -1;
|
||||
// const auto fd = m_resolvedSource.bytecodecache_fd;
|
||||
|
||||
// bool success;
|
||||
// FileSystem::MappedFileData mappedFile(fd, FileSystem::MappedFileMode::Shared, success);
|
||||
// if (!success)
|
||||
// return -1;
|
||||
|
||||
// const uint8_t* fileData = reinterpret_cast<const uint8_t*>(mappedFile.data());
|
||||
// unsigned fileTotalSize = mappedFile.size();
|
||||
// if (fileTotalSize == 0)
|
||||
// return 0;
|
||||
|
||||
// Ref<JSC::CachedBytecode> cachedBytecode = JSC::CachedBytecode::create(WTFMove(mappedFile));
|
||||
// // auto key = JSC::sourceCodeKeyForSerializedModule(vm, sourceCode);
|
||||
// // if (isCachedBytecodeStillValid(vm, cachedBytecode.copyRef(), key,
|
||||
// // JSC::SourceCodeType::ModuleType)) {
|
||||
// m_cachedBytecode = WTFMove(cachedBytecode);
|
||||
// return 1;
|
||||
// } else {
|
||||
// FileSystem::truncateFile(fd, 0);
|
||||
// return 0;
|
||||
// }
|
||||
}
|
||||
|
||||
extern "C" BunString ZigSourceProvider__getSourceSlice(SourceProvider* provider)
|
||||
{
|
||||
return Bun::toStringView(provider->source());
|
||||
}
|
||||
|
||||
}; // namespace Zig
|
||||
@@ -1,80 +0,0 @@
|
||||
#include "headers.h"
|
||||
#include "root.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace JSC {
|
||||
class Structure;
|
||||
class Identifier;
|
||||
class SourceCodeKey;
|
||||
class SourceProvider;
|
||||
} // namespace JSC
|
||||
|
||||
#include <JavaScriptCore/CachedBytecode.h>
|
||||
#include <JavaScriptCore/JSGlobalObject.h>
|
||||
#include <JavaScriptCore/JSTypeInfo.h>
|
||||
#include <JavaScriptCore/SourceProvider.h>
|
||||
#include <JavaScriptCore/Structure.h>
|
||||
|
||||
namespace Zig {
|
||||
|
||||
class GlobalObject;
|
||||
|
||||
void forEachSourceProvider(WTF::Function<void(JSC::SourceID)>);
|
||||
JSC::SourceID sourceIDForSourceURL(const WTF::String& sourceURL);
|
||||
void* sourceMappingForSourceURL(const WTF::String& sourceURL);
|
||||
JSC::SourceOrigin toSourceOrigin(const String& sourceURL, bool isBuiltin);
|
||||
class SourceProvider final : public JSC::SourceProvider {
|
||||
WTF_DEPRECATED_MAKE_FAST_ALLOCATED(SourceProvider);
|
||||
using Base = JSC::SourceProvider;
|
||||
using BytecodeCacheGenerator = JSC::BytecodeCacheGenerator;
|
||||
using UnlinkedFunctionExecutable = JSC::UnlinkedFunctionExecutable;
|
||||
using CachedBytecode = JSC::CachedBytecode;
|
||||
using UnlinkedFunctionCodeBlock = JSC::UnlinkedFunctionCodeBlock;
|
||||
using SourceCode = JSC::SourceCode;
|
||||
using CodeSpecializationKind = JSC::CodeSpecializationKind;
|
||||
using SourceOrigin = JSC::SourceOrigin;
|
||||
|
||||
public:
|
||||
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;
|
||||
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode() const final
|
||||
{
|
||||
return m_cachedBytecode.copyRef();
|
||||
};
|
||||
|
||||
void updateCache(const UnlinkedFunctionExecutable* executable, const SourceCode&, CodeSpecializationKind kind, const UnlinkedFunctionCodeBlock* codeBlock);
|
||||
void cacheBytecode(const BytecodeCacheGenerator& generator);
|
||||
void commitCachedBytecode();
|
||||
bool isBytecodeCacheEnabled() const;
|
||||
void readOrGenerateByteCodeCache(JSC::VM& vm, const JSC::SourceCode& sourceCode);
|
||||
ResolvedSource m_resolvedSource;
|
||||
int readCache(JSC::VM& vm, const JSC::SourceCode& sourceCode);
|
||||
void freeSourceCode();
|
||||
|
||||
private:
|
||||
SourceProvider(Zig::GlobalObject* globalObject, ResolvedSource resolvedSource, Ref<WTF::StringImpl>&& sourceImpl,
|
||||
JSC::SourceTaintedOrigin taintedness,
|
||||
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;
|
||||
unsigned m_hash = 0;
|
||||
};
|
||||
|
||||
} // namespace Zig
|
||||
@@ -105,29 +105,49 @@ typedef struct ErrorableString {
|
||||
ErrorableStringResult result;
|
||||
bool success;
|
||||
} ErrorableString;
|
||||
typedef struct ResolvedSource {
|
||||
BunString specifier;
|
||||
|
||||
// Forward declarations for new module loading types
|
||||
typedef struct TranspiledSource TranspiledSource;
|
||||
typedef struct SpecialModule SpecialModule;
|
||||
typedef struct ModuleResult ModuleResult;
|
||||
|
||||
// Full definitions for C++ interop
|
||||
// Corresponds to src/bun.js/bindings/TranspiledSource.zig
|
||||
struct TranspiledSource {
|
||||
BunString source_code;
|
||||
BunString source_url;
|
||||
bool isCommonJSModule;
|
||||
JSC::EncodedJSValue cjsCustomExtension;
|
||||
void* allocator;
|
||||
JSC::EncodedJSValue jsvalue_for_export;
|
||||
uint32_t tag;
|
||||
bool needsDeref;
|
||||
bool already_bundled;
|
||||
uint8_t* bytecode_cache;
|
||||
size_t bytecode_cache_size;
|
||||
} ResolvedSource;
|
||||
static const uint32_t ResolvedSourceTagPackageJSONTypeModule = 1;
|
||||
typedef union ErrorableResolvedSourceResult {
|
||||
ResolvedSource value;
|
||||
ZigErrorType err;
|
||||
} ErrorableResolvedSourceResult;
|
||||
typedef struct ErrorableResolvedSource {
|
||||
ErrorableResolvedSourceResult result;
|
||||
bool success;
|
||||
} ErrorableResolvedSource;
|
||||
size_t bytecode_cache_len;
|
||||
uint32_t flags; // Packed struct(u32) in Zig = uint32_t in C++
|
||||
};
|
||||
|
||||
// Corresponds to src/bun.js/bindings/SpecialModule.zig
|
||||
struct SpecialModule {
|
||||
enum Tag : uint8_t {
|
||||
exports_object,
|
||||
export_default_object,
|
||||
custom_extension,
|
||||
} tag;
|
||||
JSC::EncodedJSValue jsvalue;
|
||||
};
|
||||
|
||||
// Corresponds to src/bun.js/bindings/ModuleResult.zig
|
||||
struct ModuleResult {
|
||||
enum Tag : uint8_t {
|
||||
transpiled,
|
||||
special,
|
||||
builtin,
|
||||
err,
|
||||
} tag;
|
||||
union {
|
||||
TranspiledSource transpiled;
|
||||
SpecialModule special;
|
||||
uint32_t builtin_id;
|
||||
struct {
|
||||
JSC::EncodedJSValue exception;
|
||||
} err;
|
||||
} value;
|
||||
};
|
||||
|
||||
typedef struct SystemError {
|
||||
int errno_;
|
||||
@@ -354,7 +374,7 @@ extern "C" bool Bun__transpileVirtualModule(
|
||||
const BunString* referrer,
|
||||
ZigString* sourceCode,
|
||||
BunLoaderType loader,
|
||||
ErrorableResolvedSource* result);
|
||||
ModuleResult* result);
|
||||
|
||||
extern "C" JSC::EncodedJSValue Bun__runVirtualModule(
|
||||
JSC::JSGlobalObject* global,
|
||||
@@ -366,7 +386,7 @@ extern "C" JSC::JSInternalPromise* Bun__transpileFile(
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
const BunString* typeAttribute,
|
||||
ErrorableResolvedSource* result,
|
||||
ModuleResult* result,
|
||||
bool allowPromise,
|
||||
bool isCommonJSRequire,
|
||||
BunLoaderType forceLoaderType);
|
||||
@@ -376,11 +396,11 @@ extern "C" bool Bun__fetchBuiltinModule(
|
||||
JSC::JSGlobalObject* global,
|
||||
const BunString* specifier,
|
||||
const BunString* referrer,
|
||||
ErrorableResolvedSource* result);
|
||||
ModuleResult* result);
|
||||
extern "C" bool Bun__resolveAndFetchBuiltinModule(
|
||||
void* bunVM,
|
||||
const BunString* specifier,
|
||||
ErrorableResolvedSource* result);
|
||||
ModuleResult* result);
|
||||
|
||||
// Used in process.version
|
||||
extern "C" const char* Bun__version;
|
||||
|
||||
2
src/bun.js/bindings/headers.h
generated
2
src/bun.js/bindings/headers.h
generated
@@ -431,7 +431,7 @@ CPP_DECL bool Zig__GlobalObject__resetModuleRegistryMap(JSC::JSGlobalObject* arg
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
ZIG_DECL void Zig__GlobalObject__fetch(ErrorableResolvedSource* arg0, JSC::JSGlobalObject* arg1, BunString* arg2, BunString* arg3);
|
||||
// Removed in Phase 0: ZIG_DECL void Zig__GlobalObject__fetch(ErrorableResolvedSource* arg0, JSC::JSGlobalObject* arg1, BunString* arg2, BunString* arg3);
|
||||
ZIG_DECL void Zig__GlobalObject__onCrash();
|
||||
ZIG_DECL JSC::EncodedJSValue Zig__GlobalObject__promiseRejectionTracker(JSC::JSGlobalObject* arg0, JSC::JSPromise* arg1, uint32_t JSPromiseRejectionOperation2);
|
||||
ZIG_DECL JSC::EncodedJSValue Zig__GlobalObject__reportUncaughtException(JSC::JSGlobalObject* arg0, JSC::Exception* arg1);
|
||||
|
||||
@@ -89,7 +89,6 @@ pub const RegularExpression = @import("./bindings/RegularExpression.zig").Regula
|
||||
|
||||
// JavaScript-related
|
||||
pub const Errorable = @import("./bindings/Errorable.zig").Errorable;
|
||||
pub const ResolvedSource = @import("./bindings/ResolvedSource.zig").ResolvedSource;
|
||||
pub const ErrorCode = @import("./bindings/ErrorCode.zig").ErrorCode;
|
||||
pub const JSErrorCode = @import("./bindings/JSErrorCode.zig").JSErrorCode;
|
||||
pub const ZigErrorType = @import("./bindings/ZigErrorType.zig").ZigErrorType;
|
||||
@@ -102,7 +101,11 @@ pub const EventType = @import("./bindings/EventType.zig").EventType;
|
||||
pub const JSRuntimeType = @import("./bindings/JSRuntimeType.zig").JSRuntimeType;
|
||||
pub const ZigStackFrameCode = @import("./bindings/ZigStackFrameCode.zig").ZigStackFrameCode;
|
||||
|
||||
pub const ErrorableResolvedSource = Errorable(ResolvedSource);
|
||||
// New module result types (Phase 1)
|
||||
pub const ModuleResult = @import("./bindings/ModuleResult.zig").ModuleResult;
|
||||
pub const TranspiledSource = @import("./bindings/TranspiledSource.zig").TranspiledSource;
|
||||
pub const SpecialModule = @import("./bindings/SpecialModule.zig").SpecialModule;
|
||||
pub const ErrorResult = @import("./bindings/ModuleResult.zig").ErrorResult;
|
||||
pub const ErrorableZigString = Errorable(ZigString);
|
||||
pub const ErrorableJSValue = Errorable(JSValue);
|
||||
pub const ErrorableString = Errorable(bun.String);
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
#endif
|
||||
|
||||
#include "JSDOMConvertBase.h"
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
#include "mimalloc.h"
|
||||
|
||||
#include <JavaScriptCore/ControlFlowProfiler.h>
|
||||
|
||||
Reference in New Issue
Block a user