diff --git a/build/browser.esm.js b/build/browser.esm.js index 4c0cfda..3ceee4f 100644 --- a/build/browser.esm.js +++ b/build/browser.esm.js @@ -15677,15 +15677,19 @@ function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -function stringToBase64(str) { - { - return globalThis.btoa(str); +let workerSource; + +const threadStr = `(${"function thread(self) {\n const MAXMEM = 32767;\n let instance;\n let memory;\n\n if (self) {\n self.onmessage = function(e) {\n let data;\n if (e.data) {\n data = e.data;\n } else {\n data = e;\n }\n\n if (data[0].cmd == \"INIT\") {\n init(data[0]).then(function() {\n self.postMessage(data.result);\n });\n } else if (data[0].cmd == \"TERMINATE\") {\n self.close();\n } else {\n const res = runTask(data);\n self.postMessage(res);\n }\n };\n }\n\n async function init(data) {\n const code = new Uint8Array(data.code);\n const wasmModule = await WebAssembly.compile(code);\n memory = new WebAssembly.Memory({initial:data.init, maximum: MAXMEM});\n\n instance = await WebAssembly.instantiate(wasmModule, {\n env: {\n \"memory\": memory\n }\n });\n }\n\n\n\n function alloc(length) {\n const u32 = new Uint32Array(memory.buffer, 0, 1);\n while (u32[0] & 3) u32[0]++; // Return always aligned pointers\n const res = u32[0];\n u32[0] += length;\n if (u32[0] + length > memory.buffer.byteLength) {\n const currentPages = memory.buffer.byteLength / 0x10000;\n let requiredPages = Math.floor((u32[0] + length) / 0x10000)+1;\n if (requiredPages>MAXMEM) requiredPages=MAXMEM;\n memory.grow(requiredPages-currentPages);\n }\n return res;\n }\n\n function allocBuffer(buffer) {\n const p = alloc(buffer.byteLength);\n setBuffer(p, buffer);\n return p;\n }\n\n function getBuffer(pointer, length) {\n const u8 = new Uint8Array(memory.buffer);\n return new Uint8Array(u8.buffer, u8.byteOffset + pointer, length);\n }\n\n function setBuffer(pointer, buffer) {\n const u8 = new Uint8Array(memory.buffer);\n u8.set(new Uint8Array(buffer), pointer);\n }\n\n function runTask(task) {\n if (task[0].cmd == \"INIT\") {\n return init(task[0]);\n }\n const ctx = {\n vars: [],\n out: []\n };\n const u32a = new Uint32Array(memory.buffer, 0, 1);\n const oldAlloc = u32a[0];\n for (let i=0; i memory.buffer.byteLength) {\n const currentPages = memory.buffer.byteLength / 0x10000;\n let requiredPages = Math.floor((u32[0] + length) / 0x10000)+1;\n if (requiredPages>MAXMEM) requiredPages=MAXMEM;\n memory.grow(requiredPages-currentPages);\n }\n return res;\n }\n\n function allocBuffer(buffer) {\n const p = alloc(buffer.byteLength);\n setBuffer(p, buffer);\n return p;\n }\n\n function getBuffer(pointer, length) {\n const u8 = new Uint8Array(memory.buffer);\n return new Uint8Array(u8.buffer, u8.byteOffset + pointer, length);\n }\n\n function setBuffer(pointer, buffer) {\n const u8 = new Uint8Array(memory.buffer);\n u8.set(new Uint8Array(buffer), pointer);\n }\n\n function runTask(task) {\n if (task[0].cmd == \"INIT\") {\n return init(task[0]);\n }\n const ctx = {\n vars: [],\n out: []\n };\n const u32a = new Uint32Array(memory.buffer, 0, 1);\n const oldAlloc = u32a[0];\n for (let i=0; i setTimeout(resolve, ms)); } -function stringToBase64(str) { - if (process.browser) { - return globalThis.btoa(str); +let workerSource; + +const threadStr = `(${thread.toString()})(self)`; +if(process.browser) { + if(globalThis?.Blob) { + const threadBytes= new TextEncoder().encode(threadStr); + const workerBlob = new Blob([threadBytes], { type: "application/javascript" }) ; + workerSource = URL.createObjectURL(workerBlob); } else { - return Buffer.from(str).toString("base64"); + workerSource = "data:application/javascript;base64," + globalThis.btoa(threadStr); } +} else { + workerSource = "data:application/javascript;base64," + Buffer.from(threadStr).toString("base64"); } -const threadSource = stringToBase64("(" + thread.toString() + ")(self)"); -const workerSource = "data:application/javascript;base64," + threadSource; - async function buildThreadManager(wasm, singleThread) { @@ -4431,7 +4435,11 @@ async function buildThreadManager(wasm, singleThread) { "memory": tm.memory } }); - + + if(process.browser && !globalThis?.Worker) { + singleThread = true; + } + tm.singleThread = singleThread; tm.initalPFree = tm.u32[0]; // Save the Pointer to free space. tm.pq = wasm.pq; @@ -4445,7 +4453,6 @@ async function buildThreadManager(wasm, singleThread) { // tm.pTmp0 = tm.alloc(curve.G2.F.n8*3); // tm.pTmp1 = tm.alloc(curve.G2.F.n8*3); - if (singleThread) { tm.code = wasm.code; tm.taskManager = thread(); @@ -4462,11 +4469,11 @@ async function buildThreadManager(wasm, singleThread) { let concurrency = 2; if (process.browser) { - if (typeof navigator === "object" && navigator.hardwareConcurrency) { - concurrency = navigator.hardwareConcurrency; - } + if (typeof navigator === "object" && navigator.hardwareConcurrency) { + concurrency = navigator.hardwareConcurrency; + } } else { - concurrency = os.cpus().length; + concurrency = os.cpus().length; } if(concurrency == 0){ diff --git a/src/threadman.js b/src/threadman.js index ace66d7..f80901b 100644 --- a/src/threadman.js +++ b/src/threadman.js @@ -38,17 +38,21 @@ function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -function stringToBase64(str) { - if (process.browser) { - return globalThis.btoa(str); +let workerSource; + +const threadStr = `(${thread.toString()})(self)`; +if(process.browser) { + if(globalThis?.Blob) { + const threadBytes= new TextEncoder().encode(threadStr); + const workerBlob = new Blob([threadBytes], { type: "application/javascript" }) ; + workerSource = URL.createObjectURL(workerBlob); } else { - return Buffer.from(str).toString("base64"); + workerSource = "data:application/javascript;base64," + globalThis.btoa(threadStr); } +} else { + workerSource = "data:application/javascript;base64," + Buffer.from(threadStr).toString("base64"); } -const threadSource = stringToBase64("(" + thread.toString() + ")(self)"); -const workerSource = "data:application/javascript;base64," + threadSource; - export default async function buildThreadManager(wasm, singleThread) { @@ -65,7 +69,11 @@ export default async function buildThreadManager(wasm, singleThread) { "memory": tm.memory } }); - + + if(process.browser && !globalThis?.Worker) { + singleThread = true; + } + tm.singleThread = singleThread; tm.initalPFree = tm.u32[0]; // Save the Pointer to free space. tm.pq = wasm.pq; @@ -79,7 +87,6 @@ export default async function buildThreadManager(wasm, singleThread) { // tm.pTmp0 = tm.alloc(curve.G2.F.n8*3); // tm.pTmp1 = tm.alloc(curve.G2.F.n8*3); - if (singleThread) { tm.code = wasm.code; tm.taskManager = thread(); @@ -96,11 +103,11 @@ export default async function buildThreadManager(wasm, singleThread) { let concurrency = 2; if (process.browser) { - if (typeof navigator === "object" && navigator.hardwareConcurrency) { - concurrency = navigator.hardwareConcurrency; - } + if (typeof navigator === "object" && navigator.hardwareConcurrency) { + concurrency = navigator.hardwareConcurrency; + } } else { - concurrency = os.cpus().length; + concurrency = os.cpus().length; } if(concurrency == 0){