Is it possible to create a wasm file for an application that uses the C FFI? #160
-
@brendanfh Perhaps I missed it somewhere in the documentation, but is there a way to build a wasi file that has access to the C FFI? #load "./lib/packages"
use core {*}
use raylib
MAX_BUNNIES := 50000
Bunny :: struct {
position: raylib.Vector2;
speed: raylib.Vector2;
color: raylib.Color
}
main :: () {
raylib.InitWindow(800, 450, "Simple RAYLIB Window!");
raylib.SetTargetFPS(144);
texBunny: raylib.Texture2D;
raylib.LoadTexture("resources/wabbit_alpha.png", &texBunny);
texWidth := cast(f32) texBunny.width
texHeight := cast(f32) texBunny.height
bunnies: [MAX_BUNNIES] Bunny;
bunniesCount: i32 = 0;
while (!raylib.WindowShouldClose()) {
//if raylib.IsKeyPressed(.Q) {
// break;
//}
if raylib.IsMouseButtonDown(raylib.MouseButton.LEFT) {
for i in 0 .. 100 {
if bunniesCount < MAX_BUNNIES {
position: raylib.Vector2;
raylib.GetMousePosition(&position);
bunnies[bunniesCount] = Bunny.{
position = position,
speed = raylib.Vector2.{
x = random.float(-250.0, 250.0)/60,
y = random.float(-250.0, 250.0)/60
},
color = raylib.Color.{
r = cast(u8) random.between(50, 240)
g = cast(u8) random.between(80, 240)
b = cast(u8) random.between(100, 240)
a = 255
}
}
bunniesCount = bunniesCount + 1
}
}
}
for i in 0 .. bunniesCount {
bunnies[i].position.x = bunnies[i].position.x + bunnies[i].speed.x
bunnies[i].position.y = bunnies[i].position.y + bunnies[i].speed.y
if (bunnies[i].position.x + texWidth/2 > 800 || bunnies[i].position.x + texWidth/2 < 0) {
bunnies[i].speed.x *= -1
}
if (bunnies[i].position.y + texWidth/2 > 450 || bunnies[i].position.y + texWidth/2 < 0) {
bunnies[i].speed.y *= -1
}
}
// pos := raylib.GetMousePosition();
bunnyCountText := tprintf("{}\0", bunniesCount);
raylib.BeginDrawing();
raylib.ClearBackground(raylib.RAYWHITE);
for i in 0 .. bunniesCount {
raylib.DrawTexture(
texBunny,
cast(i32) bunnies[i].position.x,
cast(i32) bunnies[i].position.y,
bunnies[i].color
);
}
raylib.DrawRectangleRec(raylib.Rectangle.{0, 0, cast(f32) raylib.GetScreenWidth(), 40}, raylib.BLACK);
raylib.DrawText(bunnyCountText.data, 120, 10, 20, raylib.GREEN);
raylib.DrawFPS(10, 10);
raylib.EndDrawing();
}
raylib.CloseWindow();
}
I can compile without errors doing the following:
However, when I attempt to run with wasmer:
I assume this isn't really possible since onyx embeds wasmer and is doing some kind of magic to make it link properly to a dynamic lib? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yes, you are correct. When you run a program with If you're curious about the implementation you can see some of the important functions here. |
Beta Was this translation helpful? Give feedback.
Yes, you are correct. When you run a program with
onyx run ...
it has extra logic to resolve the native C-FFI functions. This is a rather non-standard feature in the WASM world and it violates some of the isolation/security principles that WASM generally has, so I wouldn't expect any other runtimes to support anything like this. It only exists for Onyx to allow for support of more "useful" native things like Raylib/OpenGL/etc.If you're curious about the implementation you can see some of the important functions here.
wasm_runtime.c#150
wasm_runtime.c#447