aboutsummaryrefslogtreecommitdiff
path: root/src/linker.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-12-28 00:51:22 -0800
committerGravatar GitHub <noreply@github.com> 2022-12-28 00:51:22 -0800
commitc0dd2841362b67fdb5ede262b19688004a4eb9a4 (patch)
treea3cd6b353c25c30d66489de61b394af7d5568fed /src/linker.zig
parent504972fa5deb92f2d0510d42f11b085351915a32 (diff)
downloadbun-c0dd2841362b67fdb5ede262b19688004a4eb9a4.tar.gz
bun-c0dd2841362b67fdb5ede262b19688004a4eb9a4.tar.zst
bun-c0dd2841362b67fdb5ede262b19688004a4eb9a4.zip
Upgrade to latest Zig (#1610)
* @min and @max * builtins and some trivial ones * Most of them * more * more! * More Progress * wip * Update tagged_pointer.zig * Update http_client_async.zig * Most of the iterable dir changes * alright * Remove usages of deprecated formatters * :camera: * fmt * Update shimmer.zig * wip * wip * wip * progress * more * Latest * stuck on error * latest * workaround stage2 * wip * Update string_immutable.zig * wip * Migrate `Dirent` and `require("fs')` to use JSC<>Zig bindings * Fix build errors * Fixup most of the test failures * Fix `make headers` * Fix "outside package path" error * Fixup aligned alloc * Add missing file * linux * More linux fixes * use latest peechy * Fix transpiler test failure * Forgot about these * Fixup test failure * Update node-timers.test.ts * [node:htt] Fix `undefined is not an object` error Fixes https://github.com/oven-sh/bun/issues/1618 * Update http.exports.js * Make this test less flaky * fix hashes * Fix hex formatting and zls issues * Download zig version * Update Dockerfile * Update Dockerfile * Update uws * Update Dockerfile * Set llvm version * Update README.md * Update uws * Update Dockerfile * Update io_linux.zig * Update bun.zig * Log output * workaround strange @cInclude error * Make ffi tests better * Don't use cImport * Update c.zig * Update c-bindings.cpp * call setOutputDir * Update Dockerfile * Use a longer name * latest * Update serve.test.ts Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: Veikka Tuominen <git@vexu.eu>
Diffstat (limited to 'src/linker.zig')
-rw-r--r--src/linker.zig62
1 files changed, 37 insertions, 25 deletions
diff --git a/src/linker.zig b/src/linker.zig
index f41fd1a3c..b9ce293cd 100644
--- a/src/linker.zig
+++ b/src/linker.zig
@@ -41,7 +41,7 @@ const JSC = @import("bun").JSC;
const PluginRunner = @import("./bundler.zig").PluginRunner;
pub const CSSResolveError = error{ResolveError};
-pub const OnImportCallback = fn (resolve_result: *const Resolver.Result, import_record: *ImportRecord, origin: URL) void;
+pub const OnImportCallback = *const fn (resolve_result: *const Resolver.Result, import_record: *ImportRecord, origin: URL) void;
pub const Linker = struct {
const HashedFileNameMap = std.AutoHashMap(u64, string);
@@ -369,7 +369,7 @@ pub const Linker = struct {
}
if (package_name.len != text.len) {
if (node_modules_bundle.getPackage(package_name)) |pkg| {
- const import_path = text[@minimum(text.len, package_name.len + 1)..];
+ const import_path = text[@min(text.len, package_name.len + 1)..];
if (node_modules_bundle.findModuleIDInPackageIgnoringExtension(pkg, import_path)) |found_module| {
import_record.is_bundled = true;
node_module_bundle_import_path = node_module_bundle_import_path orelse
@@ -386,7 +386,7 @@ pub const Linker = struct {
}
}
- var resolved_import_ = brk: {
+ var resolved_import_: anyerror!Resolver.Result = brk: {
switch (import_record.tag) {
else => {},
// for fast refresh, attempt to read the version directly from the bundle instead of resolving it
@@ -397,7 +397,7 @@ pub const Linker = struct {
const package_name = runtime[0 .. strings.indexOfChar(runtime, '/') orelse runtime.len];
if (node_modules_bundle.getPackage(package_name)) |pkg| {
- const import_path = runtime[@minimum(runtime.len, package_name.len + 1)..];
+ const import_path = runtime[@min(runtime.len, package_name.len + 1)..];
if (node_modules_bundle.findModuleInPackage(pkg, import_path)) |found_module| {
import_record.is_bundled = true;
node_module_bundle_import_path = node_module_bundle_import_path orelse
@@ -444,13 +444,14 @@ pub const Linker = struct {
.failure => |err| {
break :brk err;
},
- .pending => |*pending| {
+ .pending => |pending1| {
+ var pending = pending1;
if (!linker.resolver.opts.global_cache.canInstall()) {
break :brk error.InstallationPending;
}
pending.import_record_id = record_i;
- try result.pending_imports.append(linker.allocator, pending.*);
+ try result.pending_imports.append(linker.allocator, pending);
continue;
},
.not_found => break :brk error.ModuleNotFound,
@@ -795,9 +796,9 @@ pub const Linker = struct {
else => {},
}
if (had_resolve_errors) return error.ResolveError;
- result.ast.externals = externals.toOwnedSlice();
+ result.ast.externals = try externals.toOwnedSlice();
- if (result.ast.needs_runtime and result.ast.runtime_import_record_id == null) {
+ if (result.ast.needs_runtime and (result.ast.runtime_import_record_id == null or import_records.len == 0)) {
var new_import_records = try linker.allocator.alloc(ImportRecord, import_records.len + 1);
std.mem.copy(ImportRecord, new_import_records, import_records);
@@ -823,27 +824,39 @@ pub const Linker = struct {
// Since they definitely aren't using require, we don't have to worry about the symbol being renamed.
if (needs_require and !result.ast.uses_require_ref) {
result.ast.uses_require_ref = true;
- require_part_import_clauses[0] = js_ast.ClauseItem{
- .alias = require_alias,
- .original_name = "",
- .alias_loc = logger.Loc.Empty,
- .name = js_ast.LocRef{
- .loc = logger.Loc.Empty,
- .ref = result.ast.require_ref,
+ const PrependPart = struct {
+ stmts: [1]js_ast.Stmt,
+ import_statement: js_ast.S.Import,
+ clause_items: [1]js_ast.ClauseItem,
+ };
+ var prepend = linker.allocator.create(PrependPart) catch unreachable;
+
+ prepend.* = .{
+ .clause_items = .{
+ .{
+ .alias = require_alias,
+ .original_name = "",
+ .alias_loc = logger.Loc.Empty,
+ .name = js_ast.LocRef{
+ .loc = logger.Loc.Empty,
+ .ref = result.ast.require_ref,
+ },
+ },
+ },
+ .import_statement = .{
+ .namespace_ref = Ref.None,
+ .items = &prepend.clause_items,
+ .import_record_index = result.ast.runtime_import_record_id.?,
},
+ .stmts = undefined,
};
- require_part_import_statement = js_ast.S.Import{
- .namespace_ref = Ref.None,
- .items = std.mem.span(&require_part_import_clauses),
- .import_record_index = result.ast.runtime_import_record_id.?,
- };
- require_part_stmts[0] = js_ast.Stmt{
- .data = .{ .s_import = &require_part_import_statement },
+ prepend.stmts[0] = .{
+ .data = .{ .s_import = &prepend.import_statement },
.loc = logger.Loc.Empty,
};
- result.ast.prepend_part = js_ast.Part{ .stmts = std.mem.span(&require_part_stmts) };
+ result.ast.prepend_part = js_ast.Part{ .stmts = &prepend.stmts };
}
}
@@ -933,10 +946,9 @@ pub const Linker = struct {
if (linker.options.serve) {
var hash_buf: [64]u8 = undefined;
const modkey = try linker.getModKey(basepath, null);
-
return Fs.Path.init(try origin.joinAlloc(
linker.allocator,
- std.fmt.bufPrint(&hash_buf, "hash:{x}/", .{modkey.hash()}) catch unreachable,
+ std.fmt.bufPrint(&hash_buf, "hash:{any}/", .{bun.fmt.hexIntLower(modkey.hash())}) catch unreachable,
dirname,
basename,
absolute_pathname.ext,