diff --git a/.gitignore b/.gitignore index 9bb4e1d..c1eaadb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ dist .env repos .zig-cache +zig-pkg zig-out diff --git a/README.md b/README.md index b4e6172..a5779c5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ You can either: - Use it online: https://playground.zigtools.org/ - Run it locally: -Requires Zig `0.15.2`. Will automatically fetch Zig along with ZLS, compiling both for webassembly. +Requires Zig `0.16.0`. Will automatically fetch Zig along with ZLS, compiling both for webassembly. ```bash zig build -Drelease diff --git a/build.zig b/build.zig index 7adfe6c..b29dfde 100644 --- a/build.zig +++ b/build.zig @@ -8,16 +8,18 @@ pub fn build(b: *std.Build) void { const zls_step = b.step("zls", "compile and install ZLS"); const zig_step = b.step("zig", "compile and install Zig"); + const compiler_rt_step = b.step("zig_compiler_rt", "compile and install compiler_rt"); const tarball_step = b.step("zig_tarball", "compile and install zig.tar.gz"); b.getInstallStep().dependOn(zls_step); b.getInstallStep().dependOn(zig_step); + b.getInstallStep().dependOn(compiler_rt_step); b.getInstallStep().dependOn(tarball_step); const zls_dependency = b.dependency("zls", .{ .target = target, .optimize = optimize, - // .@"version-string" = @as([]const u8, "0.16.0-dev"), + // .@"version-string" = @as([]const u8, "0.17.0-dev"), }); const zls_exe = b.addExecutable(.{ @@ -38,12 +40,23 @@ pub fn build(b: *std.Build) void { const zig_dependency = b.dependency("zig", .{ .target = target, .optimize = optimize, - .@"version-string" = @as([]const u8, "0.15.1"), + .@"version-string" = @as([]const u8, "0.17.0"), .@"no-lib" = true, .dev = "wasm", }); zig_step.dependOn(installArtifact(b, zig_dependency.artifact("zig"), enable_wasm_opt)); + const lib_compiler_rt = b.addLibrary(.{ + .linkage = .static, + .name = "compiler_rt", + .root_module = b.createModule(.{ + .root_source_file = zig_dependency.path("lib/compiler_rt.zig"), + .target = target, + .optimize = optimize, + }), + }); + compiler_rt_step.dependOn(&b.addInstallArtifact(lib_compiler_rt, .{ .dest_dir = .{ .override = .prefix } }).step); + const run_tar = b.addSystemCommand(&.{ "tar", "-czf" }); const zig_tar_gz = run_tar.addOutputFileArg("zig.tar.gz"); tarball_step.dependOn(&b.addInstallFile(zig_tar_gz, "zig.tar.gz").step); diff --git a/build.zig.zon b/build.zig.zon index 59181e0..1d611db 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -2,15 +2,15 @@ .name = .playground, .version = "0.0.0", .fingerprint = 0xdc188848360fd988, // Changing this has security and trust implications. - .minimum_zig_version = "0.15.2", + .minimum_zig_version = "0.16.0", .dependencies = .{ .zls = .{ - .url = "git+https://github.com/zigtools/zls?ref=0.15.1#f91b2e1e305e5d5bd3725aea90f9f9bfb3dce055", - .hash = "zls-0.15.1-rmm5fgQ2JADjDh84Ja3pDBZ1io_kKdHS2YhQyvBYz66r", + .url = "git+https://github.com/zigtools/zls?ref=0.16.0#494486203c3a48927f2383aa3d5ce5fca112186d", + .hash = "zls-0.16.0-rmm5fs_JJgBG3dk5HwYK1FylE2_LMN0BYpX57tPzT-Xc", }, .zig = .{ - .url = "git+https://github.com/zigtools/zig?ref=wasm32-wasi#e123e17142e0576498ba7f14971e24565c242bd4", - .hash = "zig-0.0.0-Fp4XJMPnIg0zUDsFeLUD3kzBiBYbT_YSvJZo0pVB3tnR", + .url = "git+https://github.com/zigtools/zig?ref=wasm32-wasi#1c430bc130989d285723de8b7aa8e3c77297a266", + .hash = "zig-0.0.0-Fp4XJHzK5Q2xL7r676PlbSvBhz_af_mGXkGPVaXvf_Kx", }, }, .paths = .{""}, diff --git a/src/main.zig b/src/main.zig index 3b2b910..27cedd4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,5 @@ const std = @import("std"); -pub fn main() !void { - try std.fs.File.stdout().writeAll("Hello, World!\n"); +pub fn main(init: std.process.Init) !void { + try std.Io.File.stdout().writeStreamingAll(init.io, "Hello, World!\n"); } diff --git a/src/workers/zig.ts b/src/workers/zig.ts index 58efb0f..b95f20d 100644 --- a/src/workers/zig.ts +++ b/src/workers/zig.ts @@ -8,18 +8,15 @@ async function run(source: string) { currentlyRunning = true; const libDirectory = await getLatestZigArchive(); + const libCompilerRt = await fetch(new URL("../../zig-out/libcompiler_rt.a", import.meta.url)); - // -fno-llvm -fno-lld is set explicitly to ensure the native WASM backend is - // used in preference to LLVM. This may be removable once the non-LLVM - // backends become more mature. let args = [ "zig.wasm", "build-exe", "main.zig", - "-fno-llvm", - "-fno-lld", - "-fno-ubsan-rt", - "-fno-entry", // prevent the native webassembly backend from adding a start function to the module + "libcompiler_rt.a", + "-fno-compiler-rt", // manually linked because the self hosted webassembly backend cannot compile it by itself + "-fno-entry", // prevent the native webassembly backend from adding a start function to the module ]; let env = []; let fds = [ @@ -28,6 +25,7 @@ async function run(source: string) { stderrOutput(), // stderr new PreopenDirectory(".", new Map([ ["main.zig", new File(new TextEncoder().encode(source))], + ["libcompiler_rt.a", new File(await libCompilerRt.arrayBuffer())] ])), new PreopenDirectory("/lib", libDirectory.contents), new PreopenDirectory("/cache", new Map()), diff --git a/src/zls.zig b/src/zls.zig index d5771a1..8eb5386 100644 --- a/src/zls.zig +++ b/src/zls.zig @@ -12,7 +12,7 @@ pub const std_options: std.Options = .{ fn logFn( comptime level: std.log.Level, - comptime scope: @Type(.enum_literal), + comptime scope: @EnumLiteral(), comptime format: []const u8, args: anytype, ) void { @@ -20,14 +20,14 @@ fn logFn( var buffer: [4096]u8 = undefined; comptime std.debug.assert(buffer.len >= zls.lsp.minimum_logging_buffer_size); - const lsp_message_type: zls.lsp.types.MessageType = switch (level) { + const lsp_message_type: zls.lsp.types.window.MessageType = switch (level) { .err => .Error, .warn => .Warning, .info => .Info, .debug => .Debug, }; const json_message = zls.lsp.bufPrintLogMessage(&buffer, lsp_message_type, format, args); - transport.writeJsonMessage(json_message) catch {}; + transport.writeJsonMessage(threaded.io(), json_message) catch {}; } var transport: zls.lsp.Transport = .{ @@ -37,15 +37,18 @@ var transport: zls.lsp.Transport = .{ }, }; -fn readJsonMessage(_: *zls.lsp.Transport, _: std.mem.Allocator) (std.mem.Allocator.Error || zls.lsp.Transport.ReadError)![]u8 { +fn readJsonMessage(_: *zls.lsp.Transport, _: std.Io, _: std.mem.Allocator) (std.mem.Allocator.Error || zls.lsp.Transport.ReadError)![]u8 { unreachable; } -fn writeJsonMessage(_: *zls.lsp.Transport, json_message: []const u8) zls.lsp.Transport.WriteError!void { +fn writeJsonMessage(_: *zls.lsp.Transport, _: std.Io, json_message: []const u8) zls.lsp.Transport.WriteError!void { output_message_starts.append(allocator, output_message_bytes.items.len) catch return error.NoSpaceLeft; output_message_bytes.appendSlice(allocator, json_message) catch return error.NoSpaceLeft; } +var threaded: std.Io.Threaded = .init_single_threaded; +var environ_map: std.process.Environ.Map = undefined; +var config_manager: zls.configuration.Manager = undefined; var server: *zls.Server = undefined; var input_bytes: std.ArrayList(u8) = .empty; @@ -54,12 +57,15 @@ var output_message_starts: std.ArrayList(usize) = .empty; var output_message_bytes: std.ArrayList(u8) = .empty; export fn createServer() void { + const io = threaded.io(); + environ_map = .init(allocator); + config_manager = zls.configuration.Manager.init(io, allocator, &environ_map) catch @panic("server creation failed"); server = zls.Server.create(.{ + .io = io, .allocator = allocator, - .transport = null, - .config = null, + .transport = &transport, + .config_manager = &config_manager, }) catch @panic("server creation failed"); - server.setTransport(&transport); } export fn allocMessage(len: usize) [*]const u8 {