Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Zig 0.12 #26

Merged
merged 6 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 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 = .{ .path = "build_roc.zig" },
.root_source_file = b.path("build_roc.zig"),
// Empty means native.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment?

.target = .{},
.target = b.graph.host,
.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(.{ .path = val });
run_build_roc.addFileArg(b.path(val));
} else {
const default_path = "examples/snake.roc";
run_build_roc.addFileArg(.{ .path = default_path });
run_build_roc.addFileArg(b.path(default_path));
}

switch (optimize) {
Expand All @@ -44,28 +44,31 @@ 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.addSharedLibrary(.{
const lib = b.addExecutable(.{
.name = "cart",
.root_source_file = .{ .path = "platform/host.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.root_source_file = b.path("platform/host.zig"),
.target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
}),
.optimize = optimize,
});
sandprickle marked this conversation as resolved.
Show resolved Hide resolved
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.addOptions("config", options);
lib.root_module.addOptions("config", options);

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

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

lib.step.dependOn(&run_build_roc.step);
lib.addObjectFile(.{ .path = "zig-cache/app.o" });
lib.addObjectFile(b.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 };
var roc_check = try std.ChildProcess.exec(.{
const roc_check = try std.process.Child.run(.{
.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);
var roc_build = try std.ChildProcess.exec(.{
const roc_build = try std.process.Child.run(.{
.allocator = allocator,
.argv = roc_build_args.items,
});
Expand Down
6 changes: 3 additions & 3 deletions platform/InternalTask.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
interface InternalTask
exposes [Task, fromEffect, toEffect, ok, err]
imports [Effect.{ Effect }]
module [Task, fromEffect, toEffect, ok, err]

import Effect exposing [Effect]

Task ok err := Effect (Result ok err)

Expand Down
8 changes: 5 additions & 3 deletions platform/Sprite.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
interface Sprite
exposes [Sprite, SubRegion, new, blit, sub, subOrCrash]
imports [InternalTask, Task.{ Task }, Effect.{ Effect }]
module [Sprite, SubRegion, new, blit, sub, subOrCrash]

import InternalTask
import Task exposing [Task]
import Effect

## Represents a [sprite](https://en.wikipedia.org/wiki/Sprite_(computer_graphics)) for drawing to the screen.
##
Expand Down
35 changes: 18 additions & 17 deletions platform/Task.roc
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
interface Task
exposes [
Task,
ok,
err,
await,
map,
mapErr,
onErr,
attempt,
forever,
loop,
fromResult,
batch,
result,
]
imports [Effect, InternalTask]
module [
Task,
ok,
err,
await,
map,
mapErr,
onErr,
attempt,
forever,
loop,
fromResult,
batch,
result,
]

import Effect
import InternalTask

## A Task represents an effect; an interaction with state outside your Roc
## program, such as the terminal's standard output, or a file.
Expand Down
89 changes: 45 additions & 44 deletions platform/W4.roc
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,51 @@
##
## Build [wasm4](https://wasm4.org) games using Roc
##
interface W4
exposes [
Palette,
Mouse,
Gamepad,
Netplay,
Player,
Shader,
screenWidth,
screenHeight,
text,
setPalette,
getPalette,
setDrawColors,
getDrawColors,
setPrimaryColor,
setTextColors,
setShapeColors,
getGamepad,
getMouse,
getNetplay,
rect,
oval,
line,
hline,
vline,
seedRand,
rand,
randBetween,
trace,
debug,
saveToDisk,
loadFromDisk,
preserveFrameBuffer,
clearFrameBufferEachUpdate,
hideGamepadOverlay,
showGamepadOverlay,
tone,
getPixel,
setPixel,
runShader,
]
imports [InternalTask, Task.{ Task }, Effect.{ Effect }]
module [
Palette,
Mouse,
Gamepad,
Netplay,
Player,
Shader,
screenWidth,
screenHeight,
text,
setPalette,
getPalette,
setDrawColors,
getDrawColors,
setPrimaryColor,
setTextColors,
setShapeColors,
getGamepad,
getMouse,
getNetplay,
rect,
oval,
line,
hline,
vline,
seedRand,
rand,
randBetween,
trace,
debug,
saveToDisk,
loadFromDisk,
preserveFrameBuffer,
clearFrameBufferEachUpdate,
hideGamepadOverlay,
showGamepadOverlay,
tone,
getPixel,
setPixel,
runShader,
]

import InternalTask
import Task exposing [Task]
import Effect

## The [Palette] consists of four colors. There is also `None` which is used to
## represent a transparent or no change color. Each pixel on the screen will be
Expand Down Expand Up @@ -810,4 +812,3 @@ expect
res = fromColorFlags 0x0042
res == { primary: Color2, secondary: Color4, tertiary: None, quaternary: None }
expect fromColorFlags 0x4321 == { primary: Color1, secondary: Color2, tertiary: Color3, quaternary: Color4 }

14 changes: 7 additions & 7 deletions platform/glue/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub const RocList = extern struct {
return RocList.empty();
}

var list = allocate(@alignOf(T), slice.len, @sizeOf(T));
const list = allocate(@alignOf(T), slice.len, @sizeOf(T));

if (slice.len > 0) {
const dest = list.bytes orelse unreachable;
Expand Down Expand Up @@ -162,7 +162,7 @@ pub const RocList = extern struct {
}

// unfortunately, we have to clone
var new_list = RocList.allocate(alignment, self.length, element_width);
const new_list = RocList.allocate(alignment, self.length, element_width);

var old_bytes: [*]u8 = @as([*]u8, @ptrCast(self.bytes));
var new_bytes: [*]u8 = @as([*]u8, @ptrCast(new_list.bytes));
Expand Down Expand Up @@ -516,7 +516,7 @@ pub fn listReleaseExcessCapacity(
list.decref(alignment);
return RocList.empty();
} else {
var output = RocList.allocateExact(alignment, old_length, element_width);
const output = RocList.allocateExact(alignment, old_length, element_width);
if (list.bytes) |source_ptr| {
const dest_ptr = output.bytes orelse unreachable;

Expand Down Expand Up @@ -844,7 +844,7 @@ fn swap(width_initial: usize, p1: [*]u8, p2: [*]u8) void {
var ptr2 = p2;

var buffer_actual: [threshold]u8 = undefined;
var buffer: [*]u8 = buffer_actual[0..];
const buffer: [*]u8 = buffer_actual[0..];

while (true) {
if (width < threshold) {
Expand All @@ -862,8 +862,8 @@ fn swap(width_initial: usize, p1: [*]u8, p2: [*]u8) void {
}

fn swapElements(source_ptr: [*]u8, element_width: usize, index_1: usize, index_2: usize) void {
var element_at_i = source_ptr + (index_1 * element_width);
var element_at_j = source_ptr + (index_2 * element_width);
const element_at_i = source_ptr + (index_1 * element_width);
const element_at_j = source_ptr + (index_2 * element_width);

return swap(element_width, element_at_i, element_at_j);
}
Expand Down Expand Up @@ -1018,7 +1018,7 @@ pub fn listAllocationPtr(

test "listConcat: non-unique with unique overlapping" {
var nonUnique = RocList.fromSlice(u8, ([_]u8{1})[0..]);
var bytes: [*]u8 = @as([*]u8, @ptrCast(nonUnique.bytes));
const bytes: [*]u8 = @as([*]u8, @ptrCast(nonUnique.bytes));
const ptr_width = @sizeOf(usize);
const refcount_ptr = @as([*]isize, @ptrCast(@as([*]align(ptr_width) u8, @alignCast(bytes)) - ptr_width));
utils.increfRcPtrC(&refcount_ptr[0], 1);
Expand Down
Loading