From 533de49cd5dd918880cdc26ebb4d59b47edff0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Thu, 14 Apr 2022 21:46:06 +0200 Subject: [PATCH] simplify base64 handling --- src/shell/WAForth.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/shell/WAForth.js b/src/shell/WAForth.js index 6ea9ab7..4141c81 100644 --- a/src/shell/WAForth.js +++ b/src/shell/WAForth.js @@ -4,19 +4,22 @@ const isSafari = typeof navigator != "undefined" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent); -class WAForth { - constructor(arrayToBase64) { - this.arrayToBase64 = - arrayToBase64 || - function arrayToBase64(bytes) { +const arrayToBase64 = + typeof "Buffer" === "undefined" + ? function arrayToBase64(bytes) { var binary = ""; var len = bytes.byteLength; for (var i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); + } + : function arrayToBase64(bytes) { + return Buffer.from(s).toString("base64"); }; - } + +class WAForth { + constructor() {} start() { let table; @@ -82,7 +85,7 @@ class WAForth { if (index >= table.length) { table.grow(table.length); // Double size } - // console.log("Load", index, this.arrayToBase64(data)); + // console.log("Load", index, arrayToBase64(data)); var module = new WebAssembly.Module(data); new WebAssembly.Instance(module, { env: { table, memory, tos: -1 },