-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,442 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: D | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
runs-on: [ubuntu-latest, macos-latest] | ||
runs-on: ${{ matrix.runs-on }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: korandoru/setup-zig@v1 | ||
with: | ||
zig-version: master | ||
- uses: dlang-community/setup-dlang@v2 | ||
with: | ||
compiler: ldc-master | ||
|
||
# - name: (Dub) Build | ||
# run: dub build | ||
- name: (Zig) Build | ||
run: zig build run-fib32 --summary new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ docs/ | |
|
||
# Code coverage | ||
*.lst | ||
|
||
*zig-*/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
# wasm3-ldc | ||
# wasm3-d [WiP] | ||
|
||
D bindings for Wasm3, the fastest WebAssembly interpreter | ||
|
||
## Requires | ||
|
||
- [LDC](https://github.com/ldc-developers/ldc) version 1.28.0 or later | ||
- [LDC](https://ldc-developers.github.io) version 1.33.0 or latest-CI/nightly | ||
|
||
## References | ||
## Some References | ||
|
||
- https://github.com/alichay/zig-wasm3 | ||
- https://github.com/wasm3/wasm3-rs | ||
- https://github.com/wasm3/wasm3 | ||
| Lang | Project | | ||
| --- | --- | | ||
| C | [wasm3 (official)](https://github.com/wasm3/wasm3) | | ||
| Go | [go-wasm3](https://github.com/matiasinsaurralde/go-wasm3) | | ||
| Rust | [wasm3-rs (official)](https://github.com/wasm3/wasm3-rs) | | ||
| Zig | [zig-wasm3](https://github.com/alichay/zig-wasm3) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const std = @import("std"); | ||
const abs = @import("abs"); | ||
const builtin = @import("builtin"); | ||
|
||
pub fn build(b: *std.Build) !void { | ||
// ldc2/ldmd2 not have mingw-support | ||
const target = b.standardTargetOptions(.{ | ||
.default_target = if (builtin.os.tag == .windows) | ||
try std.Target.Query.parse(.{ | ||
.arch_os_abi = "native-windows-msvc", | ||
}) | ||
else | ||
.{}, | ||
}); | ||
const optimize = b.standardOptimizeOption(.{ | ||
.preferred_optimize_mode = .ReleaseSmall, | ||
}); | ||
|
||
var dflags = std.ArrayList([]const u8).init(b.allocator); | ||
defer dflags.deinit(); | ||
|
||
// local includedir | ||
try dflags.append("-Isource"); | ||
|
||
// common flags | ||
try dflags.append("-w"); | ||
try dflags.append("--preview=all"); | ||
|
||
try buildD(b, .{ | ||
.name = "fib32", | ||
.target = target, | ||
.optimize = optimize, | ||
// .betterC = true, // disable D runtimeGC (default: falsed) | ||
.sources = &.{ | ||
"examples/fib32.d", | ||
}, | ||
.dflags = dflags.items, | ||
.artifact = buildWasm3lib(b, .{ | ||
.target = target, | ||
.optimize = optimize, | ||
}), | ||
}); | ||
} | ||
|
||
fn buildD(b: *std.Build, options: abs.DCompileStep) !void { | ||
const exe = try abs.ldcBuildStep(b, options); | ||
b.default_step.dependOn(&exe.step); | ||
} | ||
|
||
fn buildWasm3lib(b: *std.Build, options: struct { | ||
target: std.Build.ResolvedTarget, | ||
optimize: std.builtin.OptimizeMode, | ||
}) *std.Build.Step.Compile { | ||
const dep = b.dependency("wasm3", .{ | ||
.target = options.target, | ||
.optimize = options.optimize, | ||
.libm3 = true, | ||
}); | ||
return dep.artifact("m3"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.{ | ||
.name = "wasm3-ldc", | ||
.version = "0.0.1", | ||
.dependencies = .{ | ||
.abs = .{ | ||
.url = "git+https://github.com/kassane/anotherBuildStep#4cb71185673f600091b92aa082db33f7f6b0d38c", | ||
.hash = "1220c7e6dc78a53c0504f93a4c9e885b8293873b799b7c755793f7d46a5309d5b6ed", | ||
}, | ||
.wasm3 = .{ | ||
.url = "git+https://github.com/kassane/wasm3#zig-v0.13.0", | ||
.hash = "1220a3c0e52d77b66bb6408e3b53bf49407b8e5a850e5f3a2a502a379e6ffbebbb18", | ||
}, | ||
}, | ||
.paths = .{ | ||
"source", | ||
"LICENSE", | ||
"README.md", | ||
"build.zig", | ||
"build.zig.zon", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
module examples.fib32; | ||
|
||
import wasm3; | ||
import std.file; | ||
import std.stdio; | ||
|
||
struct Wasm3D | ||
{ | ||
@disable this(); | ||
this(uint stackByteSize) | ||
{ | ||
env = m3_NewEnvironment(); | ||
if (env is null) | ||
{ | ||
writeln("Failed to create environment\n"); | ||
return; | ||
} | ||
runtime = m3_NewRuntime(env, stackByteSize, null); | ||
if (runtime is null) | ||
{ | ||
writeln("Failed to init runtime\n"); | ||
return; | ||
} | ||
} | ||
|
||
void loadFile(string filename) | ||
{ | ||
assert(exists(filename), "File does not exist: " ~ filename); | ||
auto f = File(filename, "rb"); | ||
if (!f.isOpen) | ||
{ | ||
writeln("Failed to open file!\n"); | ||
return; | ||
} | ||
buffer.length = 1024 * 512; | ||
while (!f.eof) | ||
{ | ||
wasmBytes = cast(ubyte[]) f.rawRead(buffer); | ||
if (wasmBytes.length == 0) | ||
{ | ||
writeln("Failed to read .wasm file\n"); | ||
return; | ||
} | ||
} | ||
writeln("Wasm file size: ", wasmBytes.length); | ||
f.close(); | ||
result = m3_ParseModule(env, &mod, wasmBytes.ptr, cast(uint) wasmBytes.length); | ||
if (result !is null) | ||
{ | ||
writeln("m3_ParseModule: "); | ||
printf("%s\n", result); | ||
return; | ||
} | ||
} | ||
|
||
void loadContent(const(ubyte)* content, uint len) | ||
{ | ||
result = m3_ParseModule(env, &mod, content, len); | ||
if (result !is null) | ||
{ | ||
writeln("m3_ParseModule: "); | ||
printf("%s\n", result); | ||
return; | ||
} | ||
} | ||
|
||
void run() | ||
{ | ||
result = m3_LoadModule(runtime, mod); | ||
if (result !is null) | ||
{ | ||
writeln("m3_LoadModule: "); | ||
printf("%s\n", result); | ||
return; | ||
} | ||
|
||
result = m3_FindFunction(&func, runtime, "fib"); | ||
if (result !is null) | ||
{ | ||
writeln("m3_FindFunction: "); | ||
printf("%s\n", result); | ||
return; | ||
} | ||
|
||
result = m3_CallV(func, 24); | ||
if (result !is null) | ||
{ | ||
writeln("m3_Call: "); | ||
printf("%s\n", result); | ||
return; | ||
} | ||
|
||
int* value = cast(int*)(runtime.stack); | ||
printf("Result: %d\n", *value); | ||
} | ||
|
||
~this() | ||
{ | ||
m3_FreeRuntime(runtime); | ||
m3_FreeEnvironment(env); | ||
} | ||
|
||
M3Environment* env = null; | ||
M3Runtime* runtime = null; | ||
IM3Function func; | ||
IM3Module mod; | ||
const(char)* result = void; | ||
ubyte[] buffer; | ||
ubyte[] wasmBytes = void; | ||
} | ||
|
||
void main() | ||
{ | ||
|
||
Wasm3D wasm3d = Wasm3D(1024); | ||
if (exists("fib32.wasm")) | ||
{ | ||
wasm3d.loadFile("fib32.wasm"); | ||
} | ||
else | ||
{ | ||
wasm3d.loadContent(fib32_wasm.ptr, fib32_wasm.length); | ||
} | ||
wasm3d.run(); | ||
} | ||
|
||
ubyte[62] fib32_wasm = [ | ||
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, | ||
0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, | ||
0x66, 0x69, 0x62, 0x00, 0x00, 0x0a, 0x1f, 0x01, 0x1d, 0x00, 0x20, 0x00, | ||
0x41, 0x02, 0x49, 0x04, 0x40, 0x20, 0x00, 0x0f, 0x0b, 0x20, 0x00, 0x41, | ||
0x02, 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x10, 0x00, 0x6a, | ||
0x0f, 0x0b | ||
]; |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.