Skip to content

Commit

Permalink
Merge pull request #153 from iden3/feat/extention-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OBrezhniev authored Apr 11, 2024
2 parents fdd388c + 471a821 commit 06e6a34
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
29 changes: 18 additions & 11 deletions build/browser.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<task.length; i++) {\n switch (task[i].cmd) {\n case \"ALLOCSET\":\n ctx.vars[task[i].var] = allocBuffer(task[i].buff);\n break;\n case \"ALLOC\":\n ctx.vars[task[i].var] = alloc(task[i].len);\n break;\n case \"SET\":\n setBuffer(ctx.vars[task[i].var], task[i].buff);\n break;\n case \"CALL\": {\n const params = [];\n for (let j=0; j<task[i].params.length; j++) {\n const p = task[i].params[j];\n if (typeof p.var !== \"undefined\") {\n params.push(ctx.vars[p.var] + (p.offset || 0));\n } else if (typeof p.val != \"undefined\") {\n params.push(p.val);\n }\n }\n instance.exports[task[i].fnName](...params);\n break;\n }\n case \"GET\":\n ctx.out[task[i].out] = getBuffer(ctx.vars[task[i].var], task[i].len).slice();\n break;\n default:\n throw new Error(\"Invalid cmd\");\n }\n }\n const u32b = new Uint32Array(memory.buffer, 0, 1);\n u32b[0] = oldAlloc;\n return ctx.out;\n }\n\n\n return runTask;\n}"})(self)`;
{
if(globalThis?.Blob) {
const threadBytes= new TextEncoder().encode(threadStr);
const workerBlob = new Blob([threadBytes], { type: "application/javascript" }) ;
workerSource = URL.createObjectURL(workerBlob);
} else {
workerSource = "data:application/javascript;base64," + globalThis.btoa(threadStr);
}
}

const threadSource = stringToBase64("(" + "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<task.length; i++) {\n switch (task[i].cmd) {\n case \"ALLOCSET\":\n ctx.vars[task[i].var] = allocBuffer(task[i].buff);\n break;\n case \"ALLOC\":\n ctx.vars[task[i].var] = alloc(task[i].len);\n break;\n case \"SET\":\n setBuffer(ctx.vars[task[i].var], task[i].buff);\n break;\n case \"CALL\": {\n const params = [];\n for (let j=0; j<task[i].params.length; j++) {\n const p = task[i].params[j];\n if (typeof p.var !== \"undefined\") {\n params.push(ctx.vars[p.var] + (p.offset || 0));\n } else if (typeof p.val != \"undefined\") {\n params.push(p.val);\n }\n }\n instance.exports[task[i].fnName](...params);\n break;\n }\n case \"GET\":\n ctx.out[task[i].out] = getBuffer(ctx.vars[task[i].var], task[i].len).slice();\n break;\n default:\n throw new Error(\"Invalid cmd\");\n }\n }\n const u32b = new Uint32Array(memory.buffer, 0, 1);\n u32b[0] = oldAlloc;\n return ctx.out;\n }\n\n\n return runTask;\n}" + ")(self)");
const workerSource = "data:application/javascript;base64," + threadSource;



async function buildThreadManager(wasm, singleThread) {
Expand All @@ -15702,7 +15706,11 @@ async function buildThreadManager(wasm, singleThread) {
"memory": tm.memory
}
});


if(!globalThis?.Worker) {
singleThread = true;
}

tm.singleThread = singleThread;
tm.initalPFree = tm.u32[0]; // Save the Pointer to free space.
tm.pq = wasm.pq;
Expand All @@ -15716,7 +15724,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();
Expand All @@ -15733,9 +15740,9 @@ async function buildThreadManager(wasm, singleThread) {

let concurrency = 2;
{
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
concurrency = navigator.hardwareConcurrency;
}
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
concurrency = navigator.hardwareConcurrency;
}
}

if(concurrency == 0){
Expand Down
33 changes: 20 additions & 13 deletions build/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4404,17 +4404,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;



async function buildThreadManager(wasm, singleThread) {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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){
Expand Down
33 changes: 20 additions & 13 deletions src/threadman.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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){
Expand Down

0 comments on commit 06e6a34

Please sign in to comment.