Single-file standalone Bun executables (#2879)

* Add LIEF

* Compile LIEF

* Implement support for embedding files on macOS

* proof of concept

* Add zstd

* Implement runtime support

* Move some code around

* Update .gitmodules

* Upgrade zig

https://github.com/ziglang/zig/pull/15278

* leftover

* leftover

* delete dead code

* Fix extname

* Revert "Upgrade zig"

This reverts commit dd968f30bf.

* Revert "leftover"

This reverts commit 7664de7686.

* Revert "leftover"

This reverts commit 498005be06.

* various fixes

* it works!

* leftover

* Make `zig build` a little faster

* give up on code signing support

* Support Linux & macOS

* Finish removing LIEF

* few more

* Add zstd to list of deps

* make it pretty

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2023-05-14 06:13:39 -07:00
committed by GitHub
parent 7f25aa9e08
commit 893f70fee4
28 changed files with 1416 additions and 2024 deletions

View File

@@ -361,6 +361,7 @@ pub const VirtualMachine = struct {
pending_unref_counter: i32 = 0,
preload: []const string = &[_][]const u8{},
unhandled_pending_rejection_to_capture: ?*JSC.JSValue = null,
standalone_module_graph: ?*bun.StandaloneModuleGraph = null,
hot_reload: bun.CLI.Command.HotReload = .none,
@@ -723,6 +724,92 @@ pub const VirtualMachine = struct {
return VMHolder.vm != null;
}
pub fn initWithModuleGraph(
allocator: std.mem.Allocator,
log: *logger.Log,
graph: *bun.StandaloneModuleGraph,
) !*VirtualMachine {
VMHolder.vm = try allocator.create(VirtualMachine);
var console = try allocator.create(ZigConsoleClient);
console.* = ZigConsoleClient.init(Output.errorWriter(), Output.writer());
const bundler = try Bundler.init(
allocator,
log,
std.mem.zeroes(Api.TransformOptions),
null,
null,
);
var vm = VMHolder.vm.?;
vm.* = VirtualMachine{
.global = undefined,
.allocator = allocator,
.entry_point = ServerEntryPoint{},
.event_listeners = EventListenerMixin.Map.init(allocator),
.bundler = bundler,
.console = console,
.node_modules = bundler.options.node_modules_bundle,
.log = log,
.flush_list = std.ArrayList(string).init(allocator),
.blobs = null,
.origin = bundler.options.origin,
.saved_source_map_table = SavedSourceMap.HashTable.init(allocator),
.source_mappings = undefined,
.macros = MacroMap.init(allocator),
.macro_entry_points = @TypeOf(vm.macro_entry_points).init(allocator),
.origin_timer = std.time.Timer.start() catch @panic("Please don't mess with timers."),
.origin_timestamp = getOriginTimestamp(),
.ref_strings = JSC.RefString.Map.init(allocator),
.file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator),
.standalone_module_graph = graph,
};
vm.source_mappings = .{ .map = &vm.saved_source_map_table };
vm.regular_event_loop.tasks = EventLoop.Queue.init(
default_allocator,
);
vm.regular_event_loop.tasks.ensureUnusedCapacity(64) catch unreachable;
vm.regular_event_loop.concurrent_tasks = .{};
vm.event_loop = &vm.regular_event_loop;
vm.bundler.macro_context = null;
vm.bundler.resolver.store_fd = false;
vm.bundler.resolver.onWakePackageManager = .{
.context = &vm.modules,
.handler = ModuleLoader.AsyncModule.Queue.onWakeHandler,
.onDependencyError = JSC.ModuleLoader.AsyncModule.Queue.onDependencyError,
};
vm.bundler.resolver.standalone_module_graph = graph;
// Avoid reading from tsconfig.json & package.json when we're in standalone mode
vm.bundler.configureLinkerWithAutoJSX(false);
try vm.bundler.configureFramework(false);
vm.bundler.macro_context = js_ast.Macro.MacroContext.init(&vm.bundler);
var global_classes: [GlobalClasses.len]js.JSClassRef = undefined;
inline for (GlobalClasses, 0..) |Class, i| {
global_classes[i] = Class.get().*;
}
vm.global = ZigGlobalObject.create(
&global_classes,
@intCast(i32, global_classes.len),
vm.console,
);
vm.regular_event_loop.global = vm.global;
vm.regular_event_loop.virtual_machine = vm;
if (source_code_printer == null) {
var writer = try js_printer.BufferWriter.init(allocator);
source_code_printer = allocator.create(js_printer.BufferPrinter) catch unreachable;
source_code_printer.?.* = js_printer.BufferPrinter.init(writer);
source_code_printer.?.ctx.append_null_byte = false;
}
return vm;
}
pub fn init(
allocator: std.mem.Allocator,
_args: Api.TransformOptions,
@@ -1196,6 +1283,7 @@ pub const VirtualMachine = struct {
res.* = ErrorableZigString.ok(ZigString.init(hardcoded.path));
return;
}
var old_log = jsc_vm.log;
var log = logger.Log.init(jsc_vm.allocator);
defer log.deinit();