Skip to content

Commit

Permalink
update-glue-6894
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilliamboswell committed Jul 14, 2024
1 parent 1eb5f11 commit 87900d3
Show file tree
Hide file tree
Showing 16 changed files with 2,746 additions and 383 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ examples/*.wasm

generated-docs/

format.sh
format.sh
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ The intent for this platform is to have some fun, learn more about Roc and platf

### Setup

Clone this repository.
1. Clone this repository.

Make sure you have [roc](https://www.roc-lang.org/install) newer than 2023-1-8, [zig](https://ziglang.org/download/) version 0.11.0, and [w4](https://wasm4.org) in your `PATH` environment variable.
2. Make sure you have the following in your `PATH` environment variable
- [roc](https://www.roc-lang.org/install),
- [zig](https://ziglang.org/download/) version **0.11.0**
- [w4](https://wasm4.org)

### Run

Expand Down
24 changes: 10 additions & 14 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ pub fn build(b: *std.Build) !void {

const build_roc = b.addExecutable(.{
.name = "build_roc",
.root_source_file = b.path("build_roc.zig"),
.root_source_file = .{ .path = "build_roc.zig" },
// Empty means native.
.target = b.graph.host,
.target = .{},
.optimize = .Debug,
});
const run_build_roc = b.addRunArtifact(build_roc);
Expand All @@ -27,10 +27,10 @@ pub fn build(b: *std.Build) !void {
run_build_roc.has_side_effects = true;

if (roc_src) |val| {
run_build_roc.addFileArg(b.path(val));
run_build_roc.addFileArg(.{ .path = val });
} else {
const default_path = "examples/snake.roc";
run_build_roc.addFileArg(b.path(default_path));
run_build_roc.addFileArg(.{ .path = default_path });
}

switch (optimize) {
Expand All @@ -44,32 +44,28 @@ pub fn build(b: *std.Build) !void {
}

// TODO: change to addExecutable with entry disabled when we update to zig 0.12.0.
const lib = b.addExecutable(.{
const lib = b.addSharedLibrary(.{
.name = "cart",
.root_source_file = b.path("platform/host.zig"),
.target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
}),
.root_source_file = .{ .path = "platform/host.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = optimize,
});
const options = b.addOptions();
options.addOption(usize, "mem_size", mem_size);
options.addOption(bool, "zero_on_alloc", zero_on_alloc);
options.addOption(bool, "trace_allocs", trace_allocs);
lib.root_module.addOptions("config", options);
lib.addOptions("config", options);

lib.entry = .disabled;
lib.import_memory = true;
lib.initial_memory = 65536;
lib.max_memory = 65536;
lib.stack_size = 14752;

// Export WASM-4 symbols
lib.root_module.export_symbol_names = &[_][]const u8{ "start", "update" };
lib.export_symbol_names = &[_][]const u8{ "start", "update" };

lib.step.dependOn(&run_build_roc.step);
lib.addObjectFile(b.path("zig-cache/app.o"));
lib.addObjectFile(.{ .path = "zig-cache/app.o" });

b.installArtifact(lib);

Expand Down
4 changes: 2 additions & 2 deletions build_roc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn main() !void {

// Run `roc check`
const roc_check_args = [_][]const u8{ "roc", "check", app_name };
const roc_check = try std.process.Child.run(.{
const roc_check = try std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &roc_check_args,
});
Expand All @@ -53,7 +53,7 @@ pub fn main() !void {
try roc_build_args.append(optimize_flag);
}
try roc_build_args.append(app_name);
const roc_build = try std.process.Child.run(.{
const roc_build = try std.ChildProcess.exec(.{
.allocator = allocator,
.argv = roc_build_args.items,
});
Expand Down
5 changes: 2 additions & 3 deletions examples/snake.roc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ drawGame = \model ->
tertiary: green,
quaternary: blue,
}

Sprite.blit! model.fruitSprite { x: model.fruit.x * 8, y: model.fruit.y * 8 }

# Draw snake body
Expand Down Expand Up @@ -214,7 +214,7 @@ moveSnake = \prev ->
walkBody = \last, remaining, newBody ->
when remaining is
[] -> newBody
[curr, .. as rest] ->
[curr, ..] ->
walkBody curr (List.dropFirst remaining 1) (List.append newBody last)

body = walkBody prev.head prev.body []
Expand Down Expand Up @@ -243,4 +243,3 @@ getRandomFruit = \{ head, body } ->
Step {} |> Task.ok
else
Done fruit |> Task.ok

11 changes: 11 additions & 0 deletions glue.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
app [makeGlue] {
pf: platform "https://github.com/lukewilliamboswell/roc/releases/download/glue-0.2/5bWcGHHGninJr_RP0-Mg5lPEGYGpPeYwYmVCwG_cZG4.tar.br",
glue: "https://github.com/lukewilliamboswell/roc-glue-code-gen/releases/download/0.4.0/E6x8uVSMI0YQ9CgpMww6Cj2g3BlN1lV8H04UEZh_-QA.tar.br",
}

import pf.Types exposing [Types]
import pf.File exposing [File]
import glue.Zig

makeGlue : List Types -> Result (List File) Str
makeGlue = \_ -> Ok Zig.builtins
12 changes: 12 additions & 0 deletions glue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

## This script is used to re-generate the glue type for Roc. For now this is limited to the std library.

# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail

# remove previous generated code
rm -rf platform/roc/

# regenerate builtins
roc glue glue.roc platform/ platform/main-glue.roc
3 changes: 0 additions & 3 deletions platform/glue/main.zig

This file was deleted.

8 changes: 4 additions & 4 deletions platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const config = @import("config");

const w4 = @import("vendored/wasm4.zig");

const str = @import("glue/str.zig");
const str = @import("roc/str.zig");
const RocStr = str.RocStr;

const list = @import("glue/list.zig");
const list = @import("roc/list.zig");
const RocList = list.RocList;

const utils = @import("glue/utils.zig");
const utils = @import("roc/utils.zig");

const ALIGN = @alignOf(u128);
const Range = std.bit_set.Range;
Expand Down Expand Up @@ -303,7 +303,7 @@ export fn roc_fx_diskw(bytes: *RocList) callconv(.C) bool {

export fn roc_fx_diskr() callconv(.C) RocList {
// This is just gonna always get as many bytes as possible.
var out = RocList.allocateExact(@alignOf(u8), MAX_DISK_SIZE, @sizeOf(u8));
var out = RocList.allocateExact(@alignOf(u8), MAX_DISK_SIZE, @sizeOf(u8), false);

const data: [*]u8 = out.elements(u8).?;
const get = w4.diskr(data, MAX_DISK_SIZE);
Expand Down
Loading

0 comments on commit 87900d3

Please sign in to comment.