Skip to content

HTTP POST to zig.run

Jason Lauman edited this page Feb 26, 2021 · 1 revision

The zig.run web application performs an HTTP POST with a Content-Type of application/json.

The command property value may be run, test, or format. The application/json response has the same structure with the stderr and/or stdout values populated, or the source value populated for the format command.

{
  "command": "run",
  "file_name": "main.zig",
  "source": "//@file_name=main.zig\nconst std = @import(\"std\");\nconst print = std.debug.print;\n\nconst hello = @import(\"./hello.zig\");\n\npub fn main() !void {\n    const howdy = @import(\"./howdy.zig\");\n    const stdout_file = std.io.getStdOut();\n    defer stdout_file.close();\n    const stdout = stdout_file.writer();\n\n    // the buffer is reused for each bprint call, and\n    // strN variables are a slice into the same buffer\n    var buffer: [64]u8 = undefined;\n    const str1 = hello.bprint(&buffer, \"world\");\n    try stdout.print(\"{}\\n\", .{str1});\n    const str2 = howdy.bprint(&buffer, \"world\");\n    try stdout.print(\"{}\\n\", .{str2});\n    // not visible outside of the source file\n    // const str3 = informal.hi(&buffer, \"world\");\n    const str3 = informal.bprint(&buffer, \"world\");\n    try stdout.print(\"{}\\n\", .{str3});\n    // the same buffer...\n    try stdout.print(\"str1? {}\\n\", .{str1});\n}\n\nconst informal = @import(\"./hi.zig\");\n//@file_name=hello.zig\nconst std = @import(\"std\");\n\n/// This is a public function.\npub fn bprint(buffer: []u8, name: []const u8) ![]u8 {\n    const slice = try std.fmt.bufPrint(buffer, \"hello, {}!\", .{name});\n    return slice;\n}\n//@file_name=hi.zig\nconst std = @import(\"std\");\n\n/// This is a public function.\npub fn bprint(buffer: []u8, name: []const u8) ![]u8 {\n    return hi(buffer, name);\n}\n\n// This is a private function.\nfn hi(buffer: []u8, name: []const u8) ![]u8 {\n    const slice = try std.fmt.bufPrint(buffer, \"hi, {}!\", .{name});\n    return slice;\n}\n\n// This is a private function.\nfn hey(buffer: []u8, name: []const u8) ![]u8 {\n    const slice = try std.fmt.bufPrint(buffer, \"hey, {}!\", .{name});\n    return slice;\n}\n//@file_name=howdy.zig\nconst std = @import(\"std\");\n\n// This is a public function.\npub fn bprint(buffer: []u8, name: []const u8) ![]u8 {\n    const slice = try std.fmt.bufPrint(buffer, \"howdy, {}!\", .{name});\n    return slice;\n}",
  "argv": "",
  "stderr": "",
  "stdout": ""
}

The source value is a file archive with each Zig source file prefixed with "//@file_name=<filename>\n".

//@file_name=main.zig
const std = @import("std");
const print = std.debug.print;

const hello = @import("./hello.zig");

pub fn main() !void {
    const howdy = @import("./howdy.zig");
    const stdout_file = std.io.getStdOut();
    defer stdout_file.close();
    const stdout = stdout_file.writer();

    // the buffer is reused for each bprint call, and
    // strN variables are a slice into the same buffer
    var buffer: [64]u8 = undefined;
    const str1 = hello.bprint(&buffer, "world");
    try stdout.print("{}\n", .{str1});
    const str2 = howdy.bprint(&buffer, "world");
    try stdout.print("{}\n", .{str2});
    // not visible outside of the source file
    // const str3 = informal.hi(&buffer, "world");
    const str3 = informal.bprint(&buffer, "world");
    try stdout.print("{}\n", .{str3});
    // the same buffer...
    try stdout.print("str1? {}\n", .{str1});
}

const informal = @import("./hi.zig");
//@file_name=hello.zig
const std = @import("std");

/// This is a public function.
pub fn bprint(buffer: []u8, name: []const u8) ![]u8 {
    const slice = try std.fmt.bufPrint(buffer, "hello, {}!", .{name});
    return slice;
}
//@file_name=hi.zig
const std = @import("std");

/// This is a public function.
pub fn bprint(buffer: []u8, name: []const u8) ![]u8 {
    return hi(buffer, name);
}

// This is a private function.
fn hi(buffer: []u8, name: []const u8) ![]u8 {
    const slice = try std.fmt.bufPrint(buffer, "hi, {}!", .{name});
    return slice;
}

// This is a private function.
fn hey(buffer: []u8, name: []const u8) ![]u8 {
    const slice = try std.fmt.bufPrint(buffer, "hey, {}!", .{name});
    return slice;
}
//@file_name=howdy.zig
const std = @import("std");

// This is a public function.
pub fn bprint(buffer: []u8, name: []const u8) ![]u8 {
    const slice = try std.fmt.bufPrint(buffer, "howdy, {}!", .{name});
    return slice;
}

Clone this wiki locally