register works
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
pkgs.cargo
|
pkgs.cargo
|
||||||
pkgs.rust-analyzer
|
pkgs.rust-analyzer
|
||||||
pkgs.rustfmt
|
pkgs.rustfmt
|
||||||
|
pkgs.binaryen
|
||||||
pkgs.lld
|
pkgs.lld
|
||||||
pkgs.zig
|
pkgs.zig
|
||||||
pkgs.zls
|
pkgs.zls
|
||||||
|
|||||||
@@ -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 release = b.option(bool, "release", "To build a wasm release") orelse false;
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "math",
|
.name = "zig",
|
||||||
.root_module = b.createModule(.{
|
.root_module = b.createModule(.{
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_source_file = b.path("src/main.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"id": "niklas/zig/math",
|
"id": "max/nodarium/zig",
|
||||||
"outputs": [
|
"outputs": [
|
||||||
"float"
|
"float"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const std = @import("std");
|
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 {
|
export fn execute(ptr: *anyopaque, len: c_int) c_int {
|
||||||
_ = ptr; // autofix
|
_ = ptr; // autofix
|
||||||
@@ -19,3 +19,11 @@ export fn __free(ptr: *anyopaque, len: c_int) void {
|
|||||||
const mem: [*]u8 = @ptrCast(@alignCast(ptr));
|
const mem: [*]u8 = @ptrCast(@alignCast(ptr));
|
||||||
std.heap.wasm_allocator.free(mem[0..@intCast(len)]);
|
std.heap.wasm_allocator.free(mem[0..@intCast(len)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export fn getDefinitionPtr() *const anyopaque {
|
||||||
|
return def.ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn getDefinitionLen() usize {
|
||||||
|
return def.len;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ interface NodariumExports extends WebAssembly.Exports {
|
|||||||
execute: (ptr: number, len: number) => number;
|
execute: (ptr: number, len: number) => number;
|
||||||
__free: (ptr: number, len: number) => void;
|
__free: (ptr: number, len: number) => void;
|
||||||
__alloc: (len: number) => number;
|
__alloc: (len: number) => number;
|
||||||
|
getDefinitionPtr: () => number;
|
||||||
|
getDefinitionLen: () => number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createWasmWrapper(buffer: ArrayBuffer) {
|
export function createWasmWrapper(buffer: ArrayBuffer) {
|
||||||
@@ -19,8 +21,8 @@ export function createWasmWrapper(buffer: ArrayBuffer) {
|
|||||||
if (!exports) return;
|
if (!exports) return;
|
||||||
const view = new Uint8Array(exports.memory.buffer, ptr, len);
|
const view = new Uint8Array(exports.memory.buffer, ptr, len);
|
||||||
console.log("RUST:", new TextDecoder().decode(view));
|
console.log("RUST:", new TextDecoder().decode(view));
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const module = new WebAssembly.Module(buffer);
|
const module = new WebAssembly.Module(buffer);
|
||||||
@@ -43,12 +45,22 @@ export function createWasmWrapper(buffer: ArrayBuffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get_definition() {
|
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) {
|
if (sections.length > 0) {
|
||||||
const decoder = new TextDecoder();
|
|
||||||
const jsonString = decoder.decode(sections[0]);
|
const jsonString = decoder.decode(sections[0]);
|
||||||
return JSON.parse(jsonString);
|
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 };
|
return { execute, get_definition };
|
||||||
|
|||||||
Reference in New Issue
Block a user