Skip to content

Commit 9b5316e

Browse files
authored
Merge pull request #7 from zigtools/dev
2 parents 14f11e3 + fd2b0b6 commit 9b5316e

37 files changed

+1826
-10425
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.zig text=auto eol=lf
2+
*.zon text=auto eol=lf

.github/workflows/deploy.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: Deploy
33
on:
44
push:
55
branches: ["main"]
6+
pull_request:
67
workflow_dispatch:
78

89
permissions:
@@ -21,9 +22,17 @@ jobs:
2122
with:
2223
node-version: 22
2324

25+
- uses: mlugg/setup-zig@v2
26+
27+
- name: Install binaryen
28+
run: |
29+
sudo apt-get update
30+
sudo apt-get install binaryen
31+
2432
- run: |
33+
zig build -Drelease -Dwasm-opt
2534
npm ci
26-
npx parcel build 404.html
35+
npm run build
2736
2837
- uses: actions/upload-pages-artifact@v3
2938
with:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
22
dist
3-
.parcel-cache
43
.env
54
repos
5+
.zig-cache
6+
zig-out

.proxyrc.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

404.html

Lines changed: 0 additions & 67 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Zig and ZLS in the browser
1+
# Zig Playground
22

33
Works pretty well in a bunch of browsers, but note the required security headers.
44

@@ -9,31 +9,12 @@ You can either:
99
- Use it online: https://playground.zigtools.org/
1010
- Run it locally:
1111

12+
Requires Zig `0.15.1`.
13+
1214
```bash
15+
zig build
1316
npm install
14-
npm run serve
17+
npm run dev
1518
```
1619

1720
Enjoy!
18-
19-
### Update artifacts
20-
21-
For the time being, the following artifacts have been commited to source control:
22-
23-
- `src/zls.wasm` - A build of [ZLS](https://github.com/zigtools/zls) (ReleaseSmall, wasm32-wasi, VERSION_TBA)
24-
- `src/zig.wasm` - A build of [Zig](https://github.com/ziglang/zig) (ReleaseSmall, wasm32-wasi, 0.14.0 with `./zig.patch` applied)
25-
- `src/zig.tar.gz` - The source code of [Zig](https://github.com/ziglang/zig). Only the `lib/std` subdirectory is needed.
26-
27-
The `./compile.sh` script can be used to create these artifacts:
28-
29-
```bash
30-
./compile zls
31-
./compile zig
32-
./compile zig_tarball
33-
```
34-
35-
Compiling Zig and ZLS may require different Zig compiler versions.
36-
37-
## TODOs
38-
39-
- [ ] Stop using `SharedArrayBuffer`s (they're awesome but a nightmare to deploy)

build.zig

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi });
5+
const optimize: std.builtin.OptimizeMode = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSmall });
6+
7+
const enable_wasm_opt = b.option(bool, "wasm-opt", "Run wasm-opt") orelse false;
8+
9+
const zls_step = b.step("zls", "compile and install ZLS");
10+
const zig_step = b.step("zig", "compile and install Zig");
11+
const tarball_step = b.step("zig_tarball", "compile and install zig.tar.gz");
12+
13+
b.getInstallStep().dependOn(zls_step);
14+
b.getInstallStep().dependOn(zig_step);
15+
b.getInstallStep().dependOn(tarball_step);
16+
17+
const zls_dependency = b.dependency("zls", .{
18+
.target = target,
19+
.optimize = optimize,
20+
// .@"version-string" = @as([]const u8, "0.16.0-dev"),
21+
});
22+
23+
const zls_exe = b.addExecutable(.{
24+
.name = "zls",
25+
.root_module = b.createModule(.{
26+
.root_source_file = b.path("src/zls.zig"),
27+
.target = target,
28+
.optimize = optimize,
29+
.imports = &.{
30+
.{ .name = "zls", .module = zls_dependency.module("zls") },
31+
},
32+
}),
33+
});
34+
zls_exe.entry = .disabled;
35+
zls_exe.rdynamic = true;
36+
zls_step.dependOn(installArtifact(b, zls_exe, enable_wasm_opt));
37+
38+
const zig_dependency = b.dependency("zig", .{
39+
.target = target,
40+
.optimize = optimize,
41+
.@"version-string" = @as([]const u8, "0.15.1"),
42+
.@"no-lib" = true,
43+
.dev = "wasm",
44+
});
45+
zig_step.dependOn(installArtifact(b, zig_dependency.artifact("zig"), enable_wasm_opt));
46+
47+
const run_tar = b.addSystemCommand(&.{ "tar", "-czf" });
48+
const zig_tar_gz = run_tar.addOutputFileArg("zig.tar.gz");
49+
tarball_step.dependOn(&b.addInstallFile(zig_tar_gz, "zig.tar.gz").step);
50+
run_tar.addArg("-C");
51+
run_tar.addDirectoryArg(zig_dependency.path("."));
52+
run_tar.addArg("lib/std");
53+
}
54+
55+
fn installArtifact(b: *std.Build, artifact: *std.Build.Step.Compile, enable_wasm_opt: bool) *std.Build.Step {
56+
if (enable_wasm_opt) {
57+
const wasm_opt = b.addSystemCommand(&.{
58+
"wasm-opt",
59+
"-Oz",
60+
"--enable-bulk-memory",
61+
"--enable-mutable-globals",
62+
"--enable-nontrapping-float-to-int",
63+
"--enable-sign-ext",
64+
});
65+
wasm_opt.addArtifactArg(artifact);
66+
wasm_opt.addArg("-o");
67+
const file_name = b.fmt("{s}.wasm", .{artifact.name});
68+
const exe = wasm_opt.addOutputFileArg(file_name);
69+
return &b.addInstallBinFile(exe, file_name).step;
70+
} else {
71+
return &b.addInstallArtifact(artifact, .{}).step;
72+
}
73+
}

build.zig.zon

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.{
2+
.name = .playground,
3+
.version = "0.0.0",
4+
.fingerprint = 0xdc188848360fd988, // Changing this has security and trust implications.
5+
.minimum_zig_version = "0.15.1",
6+
.dependencies = .{
7+
.zls = .{
8+
.url = "git+https://github.com/zigtools/zls?ref=0.15.0#ce6c8f02c78e622421cfc2405c67c5222819ec03",
9+
.hash = "zls-0.15.0-rmm5fkjqIwDZpmDHyKwxa9K2gcI3FPaGVFPwjYWFBM5B",
10+
},
11+
.zig = .{
12+
.url = "git+https://github.com/zigtools/zig?ref=wasm32-wasi#c019ad27607ad9a78b8fe62a482e2cd3e59c862a",
13+
.hash = "zig-0.0.0-Fp4XJPL7IQ0pokn0NhKHKeNpXS-bMHUs0fi_q1yJHX5G",
14+
},
15+
},
16+
.paths = .{""},
17+
}

compile.sh

Lines changed: 0 additions & 99 deletions
This file was deleted.

index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Zig+ZLS Playground</title>
8+
9+
<link rel="stylesheet" href="style/style.css">
10+
</head>
11+
<body>
12+
<button id="run">Run</button>
13+
14+
<main>
15+
<div id="split-pane" style="--editor-height-percent: 100%;">
16+
<div id="editor"></div>
17+
<div id="resize-bar"></div>
18+
<div id="output"></div>
19+
</div>
20+
</main>
21+
22+
<footer>
23+
<h1><a href="https://playground.zigtools.org">playground</a></h1>
24+
<span><span id="warning">Warning</span> Zig's self-hosted WebAssembly backend is still experimental!</span>
25+
<span>A <a href="https://zigtools.org">zigtools</a> initiative.</span>
26+
</footer>
27+
28+
<script src="/src/editor.ts" type="module"></script>
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)