register works

This commit is contained in:
2026-01-23 11:23:07 +01:00
parent aa4d7f73a8
commit 9b94159f8e
5 changed files with 28 additions and 7 deletions

View File

@@ -24,6 +24,7 @@
pkgs.cargo
pkgs.rust-analyzer
pkgs.rustfmt
pkgs.binaryen
pkgs.lld
pkgs.zig
pkgs.zls

View File

@@ -5,7 +5,7 @@ pub fn build(b: *std.Build) void {
const release = b.option(bool, "release", "To build a wasm release") orelse false;
const exe = b.addExecutable(.{
.name = "math",
.name = "zig",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,

View File

@@ -1,5 +1,5 @@
{
"id": "niklas/zig/math",
"id": "max/nodarium/zig",
"outputs": [
"float"
],

View File

@@ -1,6 +1,6 @@
const std = @import("std");
const def linksection("nodarium_definition") = @embedFile("input.json");
const def = @embedFile("input.json");
export fn execute(ptr: *anyopaque, len: c_int) c_int {
_ = ptr; // autofix
@@ -19,3 +19,11 @@ export fn __free(ptr: *anyopaque, len: c_int) void {
const mem: [*]u8 = @ptrCast(@alignCast(ptr));
std.heap.wasm_allocator.free(mem[0..@intCast(len)]);
}
export fn getDefinitionPtr() *const anyopaque {
return def.ptr;
}
export fn getDefinitionLen() usize {
return def.len;
}

View File

@@ -3,6 +3,8 @@ interface NodariumExports extends WebAssembly.Exports {
execute: (ptr: number, len: number) => number;
__free: (ptr: number, len: number) => void;
__alloc: (len: number) => number;
getDefinitionPtr: () => number;
getDefinitionLen: () => number;
}
export function createWasmWrapper(buffer: ArrayBuffer) {
@@ -19,8 +21,8 @@ export function createWasmWrapper(buffer: ArrayBuffer) {
if (!exports) return;
const view = new Uint8Array(exports.memory.buffer, ptr, len);
console.log("RUST:", new TextDecoder().decode(view));
}
}
},
},
};
const module = new WebAssembly.Module(buffer);
@@ -43,12 +45,22 @@ export function createWasmWrapper(buffer: ArrayBuffer) {
}
function get_definition() {
const sections = WebAssembly.Module.customSections(module, "nodarium_definition");
const decoder = new TextDecoder();
const sections = WebAssembly.Module.customSections(
module,
"nodarium_definition",
);
if (sections.length > 0) {
const decoder = new TextDecoder();
const jsonString = decoder.decode(sections[0]);
return JSON.parse(jsonString);
}
const ptr = exports.getDefinitionPtr();
const len = exports.getDefinitionLen();
const view = new Uint8Array(exports.memory.buffer, ptr, len);
const jsonString = decoder.decode(view);
return JSON.parse(jsonString);
}
return { execute, get_definition };