Skip to content

Commit

Permalink
feat: Add Zig example that only makes dynamic allocations if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-grass committed Feb 23, 2024
1 parent 3bd939b commit 3c9b560
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/zig-examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const examples = &[_]Example{
.name = "no-alloc-kv",
.root_source_file = .{ .path = "src/no-alloc-kv.zig" },
},
.{
.name = "mixed-alloc-kv",
.root_source_file = .{ .path = "src/mixed-alloc-kv.zig" },
},
.{
.name = "mount",
.root_source_file = .{ .path = "src/mount.zig" },
Expand Down
68 changes: 68 additions & 0 deletions examples/zig-examples/src/mixed-alloc-kv.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const std = @import("std");
const wws = @import("wws");

const tmpl =
\\<!DOCTYPE html>
\\<head>
\\<title>
\\Wasm Workers Server - KV example</title>
\\<meta name="viewport" content="width=device-width,initial-scale=1">
\\<meta charset="UTF-8">
\\</head>
\\<body>
\\<h1>Key / Value store in Zig</h1>
\\<p>Counter: {d}</p>
\\<p>This page was generated by a Zig⚡️ file running in WebAssembly.</p>
\\</body>
;
var tmpl_buf: [tmpl.len + 8]u8 = undefined;

fn handle(arena: std.mem.Allocator, request: wws.Request) !wws.Response {
const value = request.kv.map.get("counter") orelse "0";
var counter = std.fmt.parseInt(i32, value, 10) catch 0;

counter += 1;

const body = try std.fmt.bufPrint(&tmpl_buf, tmpl, .{counter});

var response = wws.Response{
.data = body,
};

const num_s = try std.fmt.allocPrint(arena, "{d}", .{counter});
try response.kv.map.put(arena, "counter", num_s);

try response.headers.map.put(arena, "x-generated-by", "wasm-workers-server");

return response;
}

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();

var arena = std.heap.ArenaAllocator.init(gpa.allocator());
defer arena.deinit();

var sfa = std.heap.stackFallback(4096 * 2, arena.allocator());

const allocator = sfa.get();

const parse_result = wws.parseStream(allocator, .{}) catch {
std.debug.print("Failed to read request\n", .{});
return error.ReadRequestFailed;
};
defer parse_result.deinit();

const request = parse_result.value;
const response = handle(allocator, request) catch {
std.debug.print("Failed to handle request\n", .{});
return error.HandleRequestFailed;
};

const stdout = std.io.getStdOut();
wws.writeResponse(response, stdout.writer()) catch {
std.debug.print("Failed to write response\n", .{});
return error.WriteResponseFailed;
};
}

0 comments on commit 3c9b560

Please sign in to comment.