Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist
.env
repos
.zig-cache
zig-pkg
zig-out
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(.{
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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 = .{""},
Expand Down
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -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");
}
12 changes: 5 additions & 7 deletions src/workers/zig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -28,6 +25,7 @@ async function run(source: string) {
stderrOutput(), // stderr
new PreopenDirectory(".", new Map<string, Inode>([
["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()),
Expand Down
22 changes: 14 additions & 8 deletions src/zls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ 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 {
_ = scope;
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 = .{
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
Loading