From: Chris Duncan Date: Tue, 3 Dec 2024 23:36:22 +0000 (-0800) Subject: Add and fix typings. Throw if any shader issues arise. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=c92f162021e0371e5f331505133fd5b6d44a58ba;p=libnemo.git Add and fix typings. Throw if any shader issues arise. --- diff --git a/src/lib/pow.ts b/src/lib/pow.ts index 547a758..f7b975f 100644 --- a/src/lib/pow.ts +++ b/src/lib/pow.ts @@ -23,8 +23,8 @@ const p = () => { } }) - async function find (hash: string, threshold: string = SEND_THRESHOLD): Promise { - return new Promise(resolve => { + async function find (hash: string, threshold: string = SEND_THRESHOLD): Promise { + return new Promise(resolve => { calculate(hash, resolve, undefined, threshold) }) } @@ -67,7 +67,7 @@ const p = () => { return out } - function calculate (hashHex: string, callback: (nonce: string) => any, progressCallback: (frames: number) => any, threshold: number | string = '0xFFFFFFF8'): void { + function calculate (hashHex: string, callback: (nonce: string | PromiseLike) => any, progressCallback?: (frames: number) => any, threshold: number | string = '0xFFFFFFF8'): void { if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16) const canvas = new OffscreenCanvas(webglWidth, webglHeight) @@ -260,6 +260,8 @@ const p = () => { }` const vertexShader = gl.createShader(gl.VERTEX_SHADER) + if (vertexShader == null) + throw 'error creating vertex shader' gl.shaderSource(vertexShader, vsSource) gl.compileShader(vertexShader) @@ -267,6 +269,8 @@ const p = () => { throw gl.getShaderInfoLog(vertexShader) const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER) + if (fragmentShader == null) + throw 'error creating fragment shader' gl.shaderSource(fragmentShader, fsSource) gl.compileShader(fragmentShader) @@ -274,6 +278,8 @@ const p = () => { throw gl.getShaderInfoLog(fragmentShader) const program = gl.createProgram() + if (program == null) + throw 'error creating program' gl.attachShader(program, vertexShader) gl.attachShader(program, fragmentShader) gl.linkProgram(program) @@ -316,7 +322,7 @@ const p = () => { const work1 = new Uint8Array(4) let n = 0 - function draw () { + const draw = (): void => { n++ crypto.getRandomValues(work0) crypto.getRandomValues(work1) @@ -338,14 +344,12 @@ const p = () => { if (pixels[i] !== 0) { // Return the work value with the custom bits typeof callback === 'function' && - callback( - hexify(work1) + - hexify([ - pixels[i + 2], - pixels[i + 3], - work0[2] ^ (pixels[i] - 1), - work0[3] ^ (pixels[i + 1] - 1) - ]), n) + callback(hexify(work1) + hexify([ + pixels[i + 2], + pixels[i + 3], + work0[2] ^ (pixels[i] - 1), + work0[3] ^ (pixels[i + 1] - 1) + ])) return } }