feat: first working version of new allocator
This commit is contained in:
@@ -9,6 +9,10 @@ description = "A collection of utilities for Nodarium"
|
||||
[lib]
|
||||
crate-type = ["rlib"]
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
|
||||
[dependencies]
|
||||
glam = "0.30.10"
|
||||
noise = "0.9.0"
|
||||
|
||||
@@ -10,6 +10,55 @@ pub fn decode_float(bits: i32) -> f32 {
|
||||
f32::from_bits(bits)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_i32(ptr: *const i32) -> i32 {
|
||||
*ptr
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_f32(ptr: *const i32) -> f32 {
|
||||
f32::from_bits(*ptr as u32)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_bool(ptr: *const i32) -> bool {
|
||||
*ptr != 0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_vec3(ptr: *const i32) -> [f32; 3] {
|
||||
let p = ptr as *const f32;
|
||||
[p.read(), p.add(1).read(), p.add(2).read()]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_i32_slice(ptr: *const i32, len: usize) -> Vec<i32> {
|
||||
std::slice::from_raw_parts(ptr, len).to_vec()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_f32_slice(ptr: *const i32, len: usize) -> Vec<f32> {
|
||||
std::slice::from_raw_parts(ptr as *const f32, len).to_vec()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_f32_default(ptr: *const i32, default: f32) -> f32 {
|
||||
if ptr.is_null() {
|
||||
default
|
||||
} else {
|
||||
read_f32(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn read_i32_default(ptr: *const i32, default: i32) -> i32 {
|
||||
if ptr.is_null() {
|
||||
default
|
||||
} else {
|
||||
read_i32(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -8,30 +8,30 @@ pub mod geometry;
|
||||
|
||||
extern "C" {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub fn host_log(ptr: *const u8, len: usize);
|
||||
pub fn __nodarium_log(ptr: *const u8, len: usize);
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
// #[cfg(debug_assertions)]
|
||||
#[macro_export]
|
||||
macro_rules! log {
|
||||
($($t:tt)*) => {{
|
||||
let msg = std::format!($($t)*);
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
unsafe {
|
||||
$crate::host_log(msg.as_ptr(), msg.len());
|
||||
$crate::__nodarium_log(msg.as_ptr(), msg.len());
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
println!("{}", msg);
|
||||
}}
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
#[macro_export]
|
||||
macro_rules! log {
|
||||
($($arg:tt)*) => {{
|
||||
// This will expand to nothing in release builds
|
||||
}};
|
||||
}
|
||||
// #[cfg(not(debug_assertions))]
|
||||
// #[macro_export]
|
||||
// macro_rules! log {
|
||||
// ($($arg:tt)*) => {{
|
||||
// // This will expand to nothing in release builds
|
||||
// }};
|
||||
// }
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[rustfmt::skip]
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
interface NodariumExports extends WebAssembly.Exports {
|
||||
memory: WebAssembly.Memory;
|
||||
execute: (ptr: number, len: number) => number;
|
||||
__free: (ptr: number, len: number) => void;
|
||||
__alloc: (len: number) => number;
|
||||
execute: (outputPos: number, ...args: number[]) => number;
|
||||
}
|
||||
|
||||
export function createWasmWrapper(buffer: ArrayBuffer) {
|
||||
export function createWasmWrapper(buffer: ArrayBuffer, memory: WebAssembly.Memory) {
|
||||
let exports: NodariumExports;
|
||||
|
||||
const importObject = {
|
||||
env: {
|
||||
host_log_panic: (ptr: number, len: number) => {
|
||||
memory: memory,
|
||||
__nodarium_log_panic: (ptr: number, len: number) => {
|
||||
if (!exports) return;
|
||||
const view = new Uint8Array(exports.memory.buffer, ptr, len);
|
||||
console.error("RUST PANIC:", new TextDecoder().decode(view));
|
||||
const view = new Uint8Array(memory.buffer, ptr, len);
|
||||
console.error('WASM PANIC:', new TextDecoder().decode(view));
|
||||
},
|
||||
host_log: (ptr: number, len: number) => {
|
||||
__nodarium_log: (ptr: number, len: number) => {
|
||||
if (!exports) return;
|
||||
const view = new Uint8Array(exports.memory.buffer, ptr, len);
|
||||
console.log("RUST:", new TextDecoder().decode(view));
|
||||
const view = new Uint8Array(memory.buffer, ptr, len);
|
||||
console.log('WASM:', new TextDecoder().decode(view));
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -27,23 +26,13 @@ export function createWasmWrapper(buffer: ArrayBuffer) {
|
||||
const instance = new WebAssembly.Instance(module, importObject);
|
||||
exports = instance.exports as NodariumExports;
|
||||
|
||||
function execute(args: Int32Array) {
|
||||
const inPtr = exports.__alloc(args.length);
|
||||
new Int32Array(exports.memory.buffer).set(args, inPtr / 4);
|
||||
|
||||
const outPtr = exports.execute(inPtr, args.length);
|
||||
|
||||
const i32Result = new Int32Array(exports.memory.buffer);
|
||||
const outLen = i32Result[outPtr / 4];
|
||||
const out = i32Result.slice(outPtr / 4 + 1, outPtr / 4 + 1 + outLen);
|
||||
|
||||
exports.__free(inPtr, args.length);
|
||||
|
||||
return out;
|
||||
function execute(outputPos: number, args: number[]): number {
|
||||
console.log('WASM_WRAPPER', { outputPos, args });
|
||||
return exports.execute(outputPos, ...args);
|
||||
}
|
||||
|
||||
function get_definition() {
|
||||
const sections = WebAssembly.Module.customSections(module, "nodarium_definition");
|
||||
const sections = WebAssembly.Module.customSections(module, 'nodarium_definition');
|
||||
if (sections.length > 0) {
|
||||
const decoder = new TextDecoder();
|
||||
const jsonString = decoder.decode(sections[0]);
|
||||
|
||||
Reference in New Issue
Block a user