#struct 'os.system__struct_2773' has no member named 'fd_t'

1 messages · Page 1 of 1 (latest)

fair robin
#
const std = @import("std");
const fmt = std.fmt;

extern fn draw(ptr: [*]u8, sz: u32) void;

extern fn jslog(message: [*]const u8, length: u32) void;

extern fn getWidth() u32;
extern fn getHeight() u32;

var canvas: []u8 = undefined;
var height: u32 = 0;
var width: u32 = 0;
var sz: u32 = 0;

pub fn Log(s: []const u8) void {
    jslog(s.ptr, s.len);
}

pub fn PrintLog(comptime s: []const u8, args: anytype) void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    var allocator = gpa.allocator();
    const str = buildString(
        allocator,
        s,
        args,
    );
    defer allocator.free(str);
    Log(str);
}

pub fn main() !void {
    Log("Init");

    height = getHeight(); // 1280
    width = getWidth(); // 720
    sz = width * height * 4;
    const webAllocator = std.heap.wasm_allocator;

    canvas = try webAllocator.alloc(u8, sz);
}

export fn onAnimationFrame(timestep: i32) void {
    PrintLog("timestep {}", .{timestep});
    draw(@ptrCast(&canvas), sz);
}

pub fn buildString(allocator: std.mem.Allocator, comptime s: []const u8, args: anytype) []const u8 {
    const str = if (fmt.allocPrint(allocator, s, args)) |str| {
        return str;
    } else |err| switch (err) {
        error.OutOfMemory => {
            return "Out Of Memory: " ++ s;
        },
    };
    return str;
}

/snap/zig/8241/lib/std/os.zig:153:24: error: struct 'os.system__struct_2773' has no member named 'fd_t'
pub const fd_t = system.fd_t;
                 ~~~~~~^~~~~
/snap/zig/8241/lib/std/os.zig:73:13: note: struct declared here
    else => struct {},
            ^~~~~~~~~
referenced by:
    Handle: /snap/zig/8241/lib/std/fs/file.zig:31:26
    File: /snap/zig/8241/lib/std/fs/file.zig:15:13
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
canvas = try webAllocator.alloc(u8, sz * 4);

removing this line removes the compile error

zig build-exe -target wasm32-freestanding -OReleaseSmall -rdynamic --name test ./src/main.zig

zig version
0.11.0

uname -a
Linux Ubuntu-2204-jammy-amd64-base 6.2.0-37-generic #38~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 2 18:01:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

tight jungle
#

i believe its because of the GPA

fair robin
#

compiles fine when i remove the line i mentioned

#

removing GPA does not solve it

tight jungle
#

oh oops

#

oh i think its because zigs default entry point catches errors from main and prints them out to stderr

#

removing that alloc line removes the only place main can error so it doesnt compile in that handling

#

if you change it to canvas = webAllocator.alloc(u8, sz) catch return; it compiles

fair robin
#

root@Ubuntu-2204-jammy-amd64-base ~/Zig/ComplyX # ./build.sh
src/main.zig:41:49: error: expected error union type, found '[]u8'
canvas = try webAllocator.alloc(u8, sz * 4) catch return;
~~~~~~~~~~~~~~~~~~~~~~~~~^~

tight jungle
#

try and catch are exclusive to each other

#

remove try

fair robin
#

ups

#

yea that solved it, tho why that error message?

#

shouldent try work?

tight jungle
#

zig owns the actual entry point to the program which runs your main, if your main returns an error it catches the error and prints to stderr but freestanding has no stderr

#

the reason it gives that exact error is because stdout/stderr/stdin are files and fd_t is a file descriptor

fair robin
#

could def be worded differently

tight jungle
#

it definitely could be improved

#

though im questing why youre trying to create a freestanding wasm exe like that

fair robin
#

using with js?

tight jungle
#

i mean the fact that your zig code has the entry point

#

wasm with js is more like a library, not an executable

fair robin
#

i need things to be initialized upon loading of wasm

tight jungle
#

ive never tried what youre trying but im not sure its going to do that

fair robin
#

its working as it should now

#

quick question tho is it possible to cast a 1d array to 3d array? the 1d array is alocated with the size of the 3d array

tight jungle
#

i dont think that can really be done at runtime since slices hold a pointer and a length but you should be able to index the 1d array as a 3d array