Add a zig wasm function.

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-04-28 00:09:59 -07:00
parent 60da4fb943
commit 0fcd360470
7 changed files with 80 additions and 2 deletions

2
.gitignore vendored
View file

@ -1,6 +1,6 @@
# Envrc
.direnv
.zig-cache
# ---> Go
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore

17
flake.lock generated
View file

@ -36,10 +36,27 @@
"type": "github"
}
},
"nixpkgszig": {
"locked": {
"lastModified": 1744502386,
"narHash": "sha256-QAd1L37eU7ktL2WeLLLTmI6P9moz9+a/ONO8qNBYJgM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f6db44a8daa59c40ae41ba6e5823ec77fe0d2124",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f6db44a8daa59c40ae41ba6e5823ec77fe0d2124",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"nixpkgszig": "nixpkgszig",
"systems": "systems"
}
},

View file

@ -2,6 +2,7 @@
description = "A dev shell flake for go";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/de0fe301211c267807afd11b12613f5511ff7433"; # Go 1.24.1
nixpkgszig.url = "github:NixOS/nixpkgs/f6db44a8daa59c40ae41ba6e5823ec77fe0d2124"; # Zig 0.14.0
systems.url = "github:nix-systems/default";
flake-utils = {
url = "github:numtide/flake-utils";
@ -10,11 +11,12 @@
};
outputs =
{ nixpkgs, flake-utils, ... }:
{ nixpkgs, nixpkgszig, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
pkgszig = nixpkgszig.legacyPackages.${system};
in
{
devShells.default = pkgs.mkShell {
@ -22,6 +24,8 @@
go
gotools
gopls
pkgszig.zig
pkgszig.zls
];
};
}

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module code.jmug.me/jmug/sqlite-wasm
go 1.23.6

15
testdata/addupto.zig vendored Normal file
View file

@ -0,0 +1,15 @@
const expect = @import("std").testing.expect;
pub export fn addUpTo(to: u32) u32 {
var res: u32 = 0;
for (0..to) |i| {
res += @intCast(i);
}
return res;
}
test "addUpTo" {
const expected: u32 = 15;
const actual = addUpTo(6);
try expect(actual == expected);
}

39
testdata/build.zig vendored Normal file
View file

@ -0,0 +1,39 @@
const std = @import("std");
const CrossTarget = std.zig.CrossTarget;
pub fn build(b: *std.Build) void {
const nativeTarget = b.standardTargetOptions(.{});
const wasmTarget = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
const add_up_to_mod = b.createModule(.{
.root_source_file = b.path("addupto.zig"),
.target = wasmTarget,
.optimize = b.standardOptimizeOption(.{}),
});
const add_up_to_native_mod = b.createModule(.{
.root_source_file = b.path("addupto.zig"),
.target = nativeTarget,
});
const add_up_to = b.addExecutable(.{
.name = "addUpTo",
.root_module = add_up_to_mod,
});
add_up_to.entry = .disabled;
add_up_to.rdynamic = true;
b.installArtifact(add_up_to);
const add_up_to_uts = b.addTest(.{
.root_module = add_up_to_native_mod,
});
const run_add_up_to_uts = b.addRunArtifact(add_up_to_uts);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_add_up_to_uts.step);
}

BIN
testdata/zig-out/bin/addUpTo.wasm vendored Executable file

Binary file not shown.