improved bindings

This commit is contained in:
Jorijn van der Graaf 2025-02-12 22:22:06 +01:00
commit aae349f803
8 changed files with 69 additions and 82 deletions

View file

@ -18,86 +18,29 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
function setInnerHTML(id, idLenght, html, htmlLenght) {
const decoder = new TextDecoder();
document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght))).innerHTML = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, html, htmlLenght));
const decoder = new TextDecoder();
let memorycounter = -1;
const jsmemory = new Map();
function freeJs(ptr) {
jsmemory.delete(ptr);
}
function strokeRect(id, x, y, width, height, color, colorLenght) {
const decoder = new TextDecoder();
const canvas = document.getElementById(id.toString());
const ctx = canvas.getContext("2d");
ctx.strokeStyle = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, color, colorLenght));
ctx.strokeRect(x, y, width, height);
function getElementById(id, idLenght) {
const obj = document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght)));
jsmemory.set(++memorycounter, obj);
return memorycounter;
}
function putImageData(id, buffer, width, height) {
const decoder = new TextDecoder();
const canvas = document.getElementById(id.toString());
const ctx = canvas.getContext("2d");
const view = new Uint8ClampedArray(window.crafter_webbuild_wasi.instance.exports.memory.buffer, buffer, 640*360*4);
const imageData = new ImageData(view, 640, 360);
ctx.putImageData(imageData, 0, 0);
function setInnerHTML(ptr, html, htmlLenght) {
jsmemory.get(ptr).innerHTML = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, html, htmlLenght));
}
function draw(id, x, y, text, textLenght){
const decoder = new TextDecoder();
const canvas = document.getElementById("1");
let ctx = canvas.getContext("2d")
ctx.fillStyle = "red";
ctx.font = '20px sans-serif';
var textString = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, text, textLenght))
let textWidth = ctx.measureText(textString).width;
ctx.fillText(textString , x - (textWidth / 2), y);
}
function setTimeoutt(obj, time, callback) {
setTimeout(() => {
window.crafter_webbuild_wasi.instance.exports.__indirect_function_table.get(callback)(obj);
}, time);
}
function addEventListener(id, eventId, listener) {
}
function fetchh(obj, url, urlLenght, buffer, callback) {
const decoder = new TextDecoder();
const view = new Uint8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, buffer);
fetch(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, url, urlLenght)), {
// Adding Get request
method: "GET",
// Setting headers
headers: {
'Content-Type': 'application/octet-stream',
},
// Setting response type to arraybuffer
responseType: "arraybuffer"
}).then(x => x.arrayBuffer()).then(
y => {
const outputBytes = new Uint8Array(y);
for(let i = 0; i < outputBytes.length; i++) {
view[i] = outputBytes[i]
}
window.crafter_webbuild_wasi.instance.exports.__indirect_function_table.get(callback)(obj, outputBytes.length, buffer);
}
);
return 1;
}
let env = {
freeJs:freeJs,
getElementById:getElementById,
setInnerHTML:setInnerHTML,
addEventListener:addEventListener,
strokeRect:strokeRect,
fetch:fetchh,
putImageData:putImageData,
setTimeout:setTimeoutt,
draw:draw
}
if(window.crafter_webbuild_env){