Skip to content

Commit

Permalink
update binding
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jun 15, 2024
1 parent 25e8ade commit 6375ff6
Show file tree
Hide file tree
Showing 4 changed files with 1,270 additions and 1,198 deletions.
6 changes: 3 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.{
.name = "wasm3-d",
.version = "0.0.1",
.version = "0.1.0",
.dependencies = .{
.abs = .{
.url = "git+https://github.com/kassane/anotherBuildStep#4cb71185673f600091b92aa082db33f7f6b0d38c",
.hash = "1220c7e6dc78a53c0504f93a4c9e885b8293873b799b7c755793f7d46a5309d5b6ed",
.url = "git+https://github.com/kassane/anotherBuildStep#8a17b838646c289ff1dda2823192bbf558417b87",
.hash = "1220d1cca874f2c665b2d5b2f99612e8d71c486f60785b065e24fd667701e1fe6f30",
},
.wasm3 = .{
.url = "git+https://github.com/kassane/wasm3#fd2117454ba389bcecb379d7797937e971011723",
Expand Down
46 changes: 21 additions & 25 deletions examples/fib32.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import std.stdio : writeln, printf, File;
import std.algorithm : endsWith;
import wasm3;

struct Wasm3
struct Fib
{
@disable this();
this(uint stackByteSize)
{
env = m3_NewEnvironment();
env = Environment(null);
if (env is null)
{
writeln("Failed to create environment\n");
return;
}
runtime = m3_NewRuntime(env, stackByteSize, null);
if (runtime is null)
rt = env.newRuntime(stackByteSize);
if (rt.runtime is null)
{
writeln("Failed to init runtime\n");
return;
Expand Down Expand Up @@ -45,37 +45,39 @@ struct Wasm3
}
writeln("Wasm file size: ", wasmBytes.length);
f.close();
result = m3_ParseModule(env, &mod, wasmBytes.ptr, cast(uint) wasmBytes.length);
mod = env.parseModule(wasmBytes, wasmBytes.length);
result = env.result;
if (result !is null)
{
writeln("m3_ParseModule: ");
writeln("env.parseModule: ");
printf("%s\n", result);
return;
}
}

void loadContent(const(ubyte)* content, uint len)
void loadContent(ubyte[] content, uint len)
{
result = m3_ParseModule(env, &mod, content, len);
mod = env.parseModule(content, len);
result = env.result;
if (result !is null)
{
writeln("m3_ParseModule: ");
writeln("env.parseModule: ");
printf("%s\n", result);
return;
}
}

void run()
{
result = m3_LoadModule(runtime, mod);
result = m3_LoadModule(rt.runtime, mod.m_module);
if (result !is null)
{
writeln("m3_LoadModule: ");
printf("%s\n", result);
return;
}

result = m3_FindFunction(&func, runtime, "fib");
result = m3_FindFunction(&func, rt.runtime, "fib");
if (result !is null)
{
writeln("m3_FindFunction: ");
Expand All @@ -91,20 +93,14 @@ struct Wasm3
return;
}

int* value = cast(int*)(runtime.stack);
int* value = cast(int*)(rt.runtime.stack);
printf("Result: %d\n", *value);
}

~this()
{
m3_FreeRuntime(runtime);
m3_FreeEnvironment(env);
}

M3Environment* env = null;
M3Runtime* runtime = null;
IM3Function func;
IM3Module mod;
Environment env;
Runtime rt;
Module mod;
const(char)* result = void;
ubyte[] buffer;
ubyte[] wasmBytes = void;
Expand All @@ -113,16 +109,16 @@ struct Wasm3
void main(string[] args)
{

Wasm3 Wasm3 = Wasm3(1024);
Fib f = Fib(1024);
if (args.length > 1 && args[1].endsWith(".wasm"))
{
Wasm3.loadFile(args[1]);
f.loadFile(args[1]);
}
else
{
Wasm3.loadContent(fib32_wasm.ptr, fib32_wasm.length);
f.loadContent(fib32_wasm, fib32_wasm.length);
}
Wasm3.run();
f.run();
}

ubyte[62] fib32_wasm = [
Expand Down
Loading

0 comments on commit 6375ff6

Please sign in to comment.