phasm/examples/include.js
2025-06-05 19:49:07 +02:00

29 lines
680 B
JavaScript

/***
* Allocates the given string in the given application's memory
*/
function alloc_bytes(app, data)
{
let stdlib_types___alloc_bytes__ = app.instance.exports['stdlib.types.__alloc_bytes__']
if( typeof data == 'string' ) {
// TODO: Unicode
data = Uint8Array.from(data.split('').map(x => x.charCodeAt()));
}
let offset = stdlib_types___alloc_bytes__(data.length);
let i8arr = new Uint8Array(app.instance.exports.memory.buffer, offset + 4, data.length);
i8arr.set(data);
return offset;
}
/**
* WebAssembly's interface only gets you signed integers
*
* Getting unsigned values out requires some work.
*/
function i32_to_u32(n)
{
return n >>> 0;
}