Merge pull request #2847 from thetarnav/js-runtime-patch

Update wasm `runtime.js`
This commit is contained in:
gingerBill
2023-10-14 19:58:28 +01:00
committed by GitHub
+22 -18
View File
@@ -9,6 +9,10 @@ function getElement(name) {
return undefined; return undefined;
} }
function stripNewline(str) {
return str.replace(/\n/, ' ')
}
class WasmMemoryInterface { class WasmMemoryInterface {
constructor() { constructor() {
this.memory = null; this.memory = null;
@@ -47,8 +51,8 @@ class WasmMemoryInterface {
} }
loadU8(addr) { return this.mem.getUint8 (addr, true); } loadU8(addr) { return this.mem.getUint8 (addr); }
loadI8(addr) { return this.mem.getInt8 (addr, true); } loadI8(addr) { return this.mem.getInt8 (addr); }
loadU16(addr) { return this.mem.getUint16 (addr, true); } loadU16(addr) { return this.mem.getUint16 (addr, true); }
loadI16(addr) { return this.mem.getInt16 (addr, true); } loadI16(addr) { return this.mem.getInt16 (addr, true); }
loadU32(addr) { return this.mem.getUint32 (addr, true); } loadU32(addr) { return this.mem.getUint32 (addr, true); }
@@ -77,11 +81,11 @@ class WasmMemoryInterface {
loadString(ptr, len) { loadString(ptr, len) {
const bytes = this.loadBytes(ptr, len); const bytes = this.loadBytes(ptr, len);
return new TextDecoder("utf-8").decode(bytes); return new TextDecoder().decode(bytes);
} }
storeU8(addr, value) { this.mem.setUint8 (addr, value, true); } storeU8(addr, value) { this.mem.setUint8 (addr, value); }
storeI8(addr, value) { this.mem.setInt8 (addr, value, true); } storeI8(addr, value) { this.mem.setInt8 (addr, value); }
storeU16(addr, value) { this.mem.setUint16 (addr, value, true); } storeU16(addr, value) { this.mem.setUint16 (addr, value, true); }
storeI16(addr, value) { this.mem.setInt16 (addr, value, true); } storeI16(addr, value) { this.mem.setInt16 (addr, value, true); }
storeU32(addr, value) { this.mem.setUint32 (addr, value, true); } storeU32(addr, value) { this.mem.setUint32 (addr, value, true); }
@@ -102,7 +106,7 @@ class WasmMemoryInterface {
storeString(addr, value) { storeString(addr, value) {
const bytes = this.loadBytes(addr, value.length); const bytes = this.loadBytes(addr, value.length);
new TextEncoder("utf-8").encodeInto(value, bytes); new TextEncoder().encodeInto(value, bytes);
} }
}; };
@@ -572,7 +576,7 @@ class WebGLInterface {
if (buf_len > 0 && buf_ptr) { if (buf_len > 0 && buf_ptr) {
let n = Math.min(buf_len, log.length); let n = Math.min(buf_len, log.length);
log = log.substring(0, n); log = log.substring(0, n);
this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(log)) this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder().encode(log))
this.mem.storeInt(length_ptr, n); this.mem.storeInt(length_ptr, n);
} }
@@ -585,7 +589,7 @@ class WebGLInterface {
if (buf_len > 0 && buf_ptr) { if (buf_len > 0 && buf_ptr) {
let n = Math.min(buf_len, log.length); let n = Math.min(buf_len, log.length);
log = log.substring(0, n); log = log.substring(0, n);
this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(log)) this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder().encode(log))
this.mem.storeInt(length_ptr, n); this.mem.storeInt(length_ptr, n);
} }
@@ -1151,7 +1155,7 @@ class WebGLInterface {
let n = Math.min(buf_len, name.length); let n = Math.min(buf_len, name.length);
name = name.substring(0, n); name = name.substring(0, n);
this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(name)) this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder().encode(name))
this.mem.storeInt(length_ptr, n); this.mem.storeInt(length_ptr, n);
}, },
UniformBlockBinding: (program, uniformBlockIndex, uniformBlockBinding) => { UniformBlockBinding: (program, uniformBlockIndex, uniformBlockBinding) => {
@@ -1344,14 +1348,14 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
} }
}, },
sqrt: (x) => Math.sqrt(x), sqrt: Math.sqrt,
sin: (x) => Math.sin(x), sin: Math.sin,
cos: (x) => Math.cos(x), cos: Math.cos,
pow: (x, power) => Math.pow(x, power), pow: Math.pow,
fmuladd: (x, y, z) => x*y + z, fmuladd: (x, y, z) => x*y + z,
ln: (x) => Math.log(x), ln: Math.log,
exp: (x) => Math.exp(x), exp: Math.exp,
ldexp: (x) => Math.ldexp(x), ldexp: (x, exp) => x * Math.pow(2, exp),
}, },
"odin_dom": { "odin_dom": {
init_event_raw: (ep) => { init_event_raw: (ep) => {
@@ -1425,7 +1429,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
wmi.storeI16(off(2), e.button); wmi.storeI16(off(2), e.button);
wmi.storeU16(off(2), e.buttons); wmi.storeU16(off(2), e.buttons);
} else if (e instanceof KeyboardEvent) { } else if (e instanceof KeyboardEvent) {
// Note: those strigs are constructed // Note: those strings are constructed
// on the native side from buffers that // on the native side from buffers that
// are filled later, so skip them // are filled later, so skip them
const keyPtr = off(W*2, W); const keyPtr = off(W*2, W);
@@ -1571,7 +1575,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
if (buf_len > 0 && buf_ptr) { if (buf_len > 0 && buf_ptr) {
let n = Math.min(buf_len, str.length); let n = Math.min(buf_len, str.length);
str = str.substring(0, n); str = str.substring(0, n);
this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(str)) this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder().encode(str))
return n; return n;
} }
} }