buzz-language / buzz

👨‍🚀 buzz, A small/lightweight statically typed scripting language
https://buzz-lang.dev
MIT License
1.22k stars 34 forks source link

Panics on FFI unused global type #229

Closed Its-Kenta closed 11 months ago

Its-Kenta commented 11 months ago

Working on utilising FFI with Raylib and whenever I'm trying to run the code for test purposes it panics with following:

:1:0: [W89] Syntax warning: Unused global of type `Vector2`
     1 ╭─ 
       ┆  ╭──────
       ┆  ╰─ Unused global of type `Vector2`

:1:0: [W89] Syntax warning: Unused global of type `Image`
     1 ╭─ 
       ┆  ╭────
       ┆  ╰─ Unused global of type `Image`
thread 171263 panic: reached unreachable code

It seems to be related to global extern structs within FFI call that are not used in the Buzz code. Full code:

import "std";
import "ffi";

zdef("raylib", `
const Vector2 = extern struct {
    width: f64,
    height: f64,
};

const Image = extern struct {
    width: c_int,
    height: c_int,
    mipmaps: c_int,
    format: c_int,
};

fn GetScreenWidth() c_int;
fn InitWindow(width: c_int, height: c_int, title: [*:0]const u8) void;
fn WindowShouldClose() bool;
fn CloseWindow() void;
fn IsWindowReady() bool;
fn IsWindowFullscreen() bool;             
fn IsWindowHidden() bool;
fn IsWindowMinimized() bool;
fn IsWindowMaximized() bool;
fn IsWindowFocused() bool;
fn IsWindowResized() bool;
fn IsWindowState(flag: c_uint) bool;
fn SetWindowState(flag: c_uint) void;
fn ClearWindowState(flag: c_uint) void;
fn ToggleFullscreen() void;
fn MaximizeWindow() void;
fn MinimizeWindow() void;
fn RestoreWindow() void;
fn SetWindowIcon(image: Image) void;
fn SetWindowIcons(images: *Image, count: c_int) void;
fn SetWindowTitle(title: [*:0]const u8) void;
fn SetWindowPosition(x: c_int, y: c_int) void;
fn SetWindowMonitor(monitor: c_int) void;
fn SetWindowMinSize(width: c_int, height: c_int) void;
fn SetWindowSize(width: c_int, height: c_int) void;
fn GetScreenHeight() c_int;
fn GetRenderWidth() c_int;
fn GetRenderHeight() c_int;
fn GetMonitorCount() c_int;
fn GetCurrentMonitor() c_int;
fn GetMonitorPosition(monitor: c_int) Vector2;
fn GetMonitorWidth(monitor: c_int) c_int;
fn GetMonitorHeight(monitor: c_int) c_int;
fn GetMonitorPhysicalWidth(monitor: c_int) c_int;
fn GetMonitorPhysicalHeight(monitor: c_int) c_int;
fn GetMonitorRefreshRate(monitor: c_int) c_int;
fn GetWindowPosition() Vector2;
fn GetWindowScaleDPI() Vector2;
fn GetMonitorName(monitor: c_int) [*:0]const u8;
fn SetClipboardText(text: [*:0]const u8) void;
fn GetClipboardText() [*:0]const u8;
fn EnableEventWaiting() void;
fn DisableEventWaiting() void;
`);

fun main([str] _) > void {
    print("hello world");
}

Removing all instances of Image and Vector2 (including functions) will result in successful printout of "hello world"

giann commented 11 months ago

Do you still have the issue? I don't on my end.

Its-Kenta commented 11 months ago

Can be closed. Looks like its not related to unused global types, but as discussed on Discord its about passing structs around.