Skip to content

Commit 06e6a34

Browse files
authored
Merge pull request #153 from iden3/feat/extention-fix
2 parents fdd388c + 471a821 commit 06e6a34

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

build/browser.esm.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -15677,15 +15677,19 @@ function sleep(ms) {
1567715677
return new Promise(resolve => setTimeout(resolve, ms));
1567815678
}
1567915679

15680-
function stringToBase64(str) {
15681-
{
15682-
return globalThis.btoa(str);
15680+
let workerSource;
15681+
15682+
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)`;
15683+
{
15684+
if(globalThis?.Blob) {
15685+
const threadBytes= new TextEncoder().encode(threadStr);
15686+
const workerBlob = new Blob([threadBytes], { type: "application/javascript" }) ;
15687+
workerSource = URL.createObjectURL(workerBlob);
15688+
} else {
15689+
workerSource = "data:application/javascript;base64," + globalThis.btoa(threadStr);
1568315690
}
1568415691
}
1568515692

15686-
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)");
15687-
const workerSource = "data:application/javascript;base64," + threadSource;
15688-
1568915693

1569015694

1569115695
async function buildThreadManager(wasm, singleThread) {
@@ -15702,7 +15706,11 @@ async function buildThreadManager(wasm, singleThread) {
1570215706
"memory": tm.memory
1570315707
}
1570415708
});
15705-
15709+
15710+
if(!globalThis?.Worker) {
15711+
singleThread = true;
15712+
}
15713+
1570615714
tm.singleThread = singleThread;
1570715715
tm.initalPFree = tm.u32[0]; // Save the Pointer to free space.
1570815716
tm.pq = wasm.pq;
@@ -15716,7 +15724,6 @@ async function buildThreadManager(wasm, singleThread) {
1571615724
// tm.pTmp0 = tm.alloc(curve.G2.F.n8*3);
1571715725
// tm.pTmp1 = tm.alloc(curve.G2.F.n8*3);
1571815726

15719-
1572015727
if (singleThread) {
1572115728
tm.code = wasm.code;
1572215729
tm.taskManager = thread();
@@ -15733,9 +15740,9 @@ async function buildThreadManager(wasm, singleThread) {
1573315740

1573415741
let concurrency = 2;
1573515742
{
15736-
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
15737-
concurrency = navigator.hardwareConcurrency;
15738-
}
15743+
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
15744+
concurrency = navigator.hardwareConcurrency;
15745+
}
1573915746
}
1574015747

1574115748
if(concurrency == 0){

build/main.cjs

+20-13
Original file line numberDiff line numberDiff line change
@@ -4404,17 +4404,21 @@ function sleep(ms) {
44044404
return new Promise(resolve => setTimeout(resolve, ms));
44054405
}
44064406

4407-
function stringToBase64(str) {
4408-
if (process.browser) {
4409-
return globalThis.btoa(str);
4407+
let workerSource;
4408+
4409+
const threadStr = `(${thread.toString()})(self)`;
4410+
if(process.browser) {
4411+
if(globalThis?.Blob) {
4412+
const threadBytes= new TextEncoder().encode(threadStr);
4413+
const workerBlob = new Blob([threadBytes], { type: "application/javascript" }) ;
4414+
workerSource = URL.createObjectURL(workerBlob);
44104415
} else {
4411-
return Buffer.from(str).toString("base64");
4416+
workerSource = "data:application/javascript;base64," + globalThis.btoa(threadStr);
44124417
}
4418+
} else {
4419+
workerSource = "data:application/javascript;base64," + Buffer.from(threadStr).toString("base64");
44134420
}
44144421

4415-
const threadSource = stringToBase64("(" + thread.toString() + ")(self)");
4416-
const workerSource = "data:application/javascript;base64," + threadSource;
4417-
44184422

44194423

44204424
async function buildThreadManager(wasm, singleThread) {
@@ -4431,7 +4435,11 @@ async function buildThreadManager(wasm, singleThread) {
44314435
"memory": tm.memory
44324436
}
44334437
});
4434-
4438+
4439+
if(process.browser && !globalThis?.Worker) {
4440+
singleThread = true;
4441+
}
4442+
44354443
tm.singleThread = singleThread;
44364444
tm.initalPFree = tm.u32[0]; // Save the Pointer to free space.
44374445
tm.pq = wasm.pq;
@@ -4445,7 +4453,6 @@ async function buildThreadManager(wasm, singleThread) {
44454453
// tm.pTmp0 = tm.alloc(curve.G2.F.n8*3);
44464454
// tm.pTmp1 = tm.alloc(curve.G2.F.n8*3);
44474455

4448-
44494456
if (singleThread) {
44504457
tm.code = wasm.code;
44514458
tm.taskManager = thread();
@@ -4462,11 +4469,11 @@ async function buildThreadManager(wasm, singleThread) {
44624469

44634470
let concurrency = 2;
44644471
if (process.browser) {
4465-
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
4466-
concurrency = navigator.hardwareConcurrency;
4467-
}
4472+
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
4473+
concurrency = navigator.hardwareConcurrency;
4474+
}
44684475
} else {
4469-
concurrency = os.cpus().length;
4476+
concurrency = os.cpus().length;
44704477
}
44714478

44724479
if(concurrency == 0){

src/threadman.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@ function sleep(ms) {
3838
return new Promise(resolve => setTimeout(resolve, ms));
3939
}
4040

41-
function stringToBase64(str) {
42-
if (process.browser) {
43-
return globalThis.btoa(str);
41+
let workerSource;
42+
43+
const threadStr = `(${thread.toString()})(self)`;
44+
if(process.browser) {
45+
if(globalThis?.Blob) {
46+
const threadBytes= new TextEncoder().encode(threadStr);
47+
const workerBlob = new Blob([threadBytes], { type: "application/javascript" }) ;
48+
workerSource = URL.createObjectURL(workerBlob);
4449
} else {
45-
return Buffer.from(str).toString("base64");
50+
workerSource = "data:application/javascript;base64," + globalThis.btoa(threadStr);
4651
}
52+
} else {
53+
workerSource = "data:application/javascript;base64," + Buffer.from(threadStr).toString("base64");
4754
}
4855

49-
const threadSource = stringToBase64("(" + thread.toString() + ")(self)");
50-
const workerSource = "data:application/javascript;base64," + threadSource;
51-
5256

5357

5458
export default async function buildThreadManager(wasm, singleThread) {
@@ -65,7 +69,11 @@ export default async function buildThreadManager(wasm, singleThread) {
6569
"memory": tm.memory
6670
}
6771
});
68-
72+
73+
if(process.browser && !globalThis?.Worker) {
74+
singleThread = true;
75+
}
76+
6977
tm.singleThread = singleThread;
7078
tm.initalPFree = tm.u32[0]; // Save the Pointer to free space.
7179
tm.pq = wasm.pq;
@@ -79,7 +87,6 @@ export default async function buildThreadManager(wasm, singleThread) {
7987
// tm.pTmp0 = tm.alloc(curve.G2.F.n8*3);
8088
// tm.pTmp1 = tm.alloc(curve.G2.F.n8*3);
8189

82-
8390
if (singleThread) {
8491
tm.code = wasm.code;
8592
tm.taskManager = thread();
@@ -96,11 +103,11 @@ export default async function buildThreadManager(wasm, singleThread) {
96103

97104
let concurrency = 2;
98105
if (process.browser) {
99-
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
100-
concurrency = navigator.hardwareConcurrency;
101-
}
106+
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
107+
concurrency = navigator.hardwareConcurrency;
108+
}
102109
} else {
103-
concurrency = os.cpus().length;
110+
concurrency = os.cpus().length;
104111
}
105112

106113
if(concurrency == 0){

0 commit comments

Comments
 (0)