Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
voigt committed Aug 14, 2023
1 parent 1a26de5 commit 3b92a42
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Wasm Workers Server focuses on simplicity. We want you to run workers (written i
| Go | ✅ | No | [#95](https://github.com/vmware-labs/wasm-workers-server/issues/95) |
| Ruby | ✅ | [Yes](https://workers.wasmlabs.dev/docs/languages/ruby#installation) | [#63](https://github.com/vmware-labs/wasm-workers-server/issues/63) |
| Python | ✅ | [Yes](https://workers.wasmlabs.dev/docs/languages/python#installation) | [#63](https://github.com/vmware-labs/wasm-workers-server/issues/63) |
| Zig | 🚧 | No | [#144](https://github.com/vmware-labs/wasm-workers-server/issues/144) |
| Zig | | Yes | [#144](https://github.com/vmware-labs/wasm-workers-server/issues/144) |
| PHP | 🚧 | No | [#100](https://github.com/vmware-labs/wasm-workers-server/issues/100) |
To get more information about multi-language support in Wasm Workers Server, [check our documentation](https://workers.wasmlabs.dev/docs/languages/introduction).
Expand Down
4 changes: 3 additions & 1 deletion kits/zig/worker/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
zig-cache/
zig-out/
zig-out/
examples/json.zig
examples/output.zig
40 changes: 39 additions & 1 deletion kits/zig/worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,48 @@

This folder contains the Zig kit or SDK for Wasm Workers Server. Currently, it uses the regular STDIN / STDOUT approach to receive the request and provide the response.

> *Note: this assumes Zig `0.11.0`*
## build

To build example in ./examples
To build all example in ./examples

```bash
$ zig build -Dtarget="wasm32-wasi"
```

To build a specific example

```bash
$ zig build-exe examples/<example>.zig -target wasm32-wasi
```

## testing

from `./kits/zig/worker` execute

```bash
$ zig build -Dtarget="wasm32-wasi"
$ wws ./zig-out/bin/
```

## sockaddr issue

Using `*http.Server.Response` was unsuccessful and lead to following error:

```
$ worker git:(144_-_add_support_for_zig) ✗ zig build -Dtarget="wasm32-wasi"
zig build-exe main Debug wasm32-wasi: error: the following command failed with 1 compilation errors:
/Users/c.voigt/.asdf/installs/zig/0.11.0/zig build-exe /Users/c.voigt/go/src/github.com/voigt/wasm-workers-server/kits/zig/worker/examples/main.zig --cache-dir /Users/c.voigt/go/src/github.com/voigt/wasm-workers-server/kits/zig/worker/zig-cache --global-cache-dir /Users/c.voigt/.cache/zig --name main -target wasm32-wasi -mcpu generic --mod worker::/Users/c.voigt/go/src/github.com/voigt/wasm-workers-server/kits/zig/worker/src/worker.zig --deps worker --listen=-
Build Summary: 6/9 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install main transitive failure
└─ zig build-exe main Debug wasm32-wasi 1 errors
/Users/c.voigt/.asdf/installs/zig/0.11.0/lib/std/os.zig:182:28: error: root struct of file 'os.wasi' has no member named 'sockaddr'
pub const sockaddr = system.sockaddr;
~~~~~~^~~~~~~~~
referenced by:
Address: /Users/c.voigt/.asdf/installs/zig/0.11.0/lib/std/net.zig:18:12
Address: /Users/c.voigt/.asdf/installs/zig/0.11.0/lib/std/net.zig:17:28
remaining reference traces hidden; use '-freference-trace' to see all reference traces
```
1 change: 1 addition & 0 deletions kits/zig/worker/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const package_name = "worker";
const package_path = "src/worker.zig";

const examples = [2][]const u8{ "main", "basic" };
// const examples = [4][]const u8{ "main", "basic", "json", "output" };

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{ });
Expand Down
24 changes: 20 additions & 4 deletions kits/zig/worker/examples/basic.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
const std = @import("std");
const w = @import("worker");
const io = std.io;
const http = std.http;
const worker = @import("worker");

var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();

// Not working with *http.Server.Response
// fn cool(resp: *http.Server.Response, r: *http.Client.Request) void {
fn requestFn(resp: *worker.Response, r: *http.Client.Request) void {
_ = r;
std.debug.print("Hello from function\n", .{ });

_ = &resp.httpHeader.append("content-type", "text/plain");
_ = &resp.httpHeader.append("x-generated-by", "wasm-workers-server");

_ = &resp.writeAll("Zig World!");
}

pub fn main() !void {
var word = w.GetWord();
std.debug.print("Hello from {s}!\n", .{ word });
}
worker.ServeFunc(requestFn);
}
1 change: 1 addition & 0 deletions kits/zig/worker/examples/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn requestFn(resp: *worker.Response, r: *http.Client.Request) void {
_ = r;
std.debug.print("Hello from function\n", .{ });

// TODO: prepare to read request body and send it back
// std.debug.print("+++ doing payload \n", .{ });
// var payload: []const u8 = "";
// var reqBody = r.reader().readAllAlloc(allocator, 8192) catch unreachable;
Expand Down

0 comments on commit 3b92a42

Please sign in to comment.