aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-polyfills/lib/zighash/index.mjs
diff options
context:
space:
mode:
authorGravatar jhmaster <32803471+jhmaster2000@users.noreply.github.com> 2023-08-10 16:17:39 -0300
committerGravatar GitHub <noreply@github.com> 2023-08-10 12:17:39 -0700
commit40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7 (patch)
tree1860719f46b760aac8a874ea1fb2c24fcce43fb7 /packages/bun-polyfills/lib/zighash/index.mjs
parent6718950a39614939560ed885b828025e9c45243f (diff)
downloadbun-40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7.tar.gz
bun-40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7.tar.zst
bun-40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7.zip
Sync bun-polyfills branch (#4081)
* bun-polyfills: initial impl. & baseline refactor * move @types/ws dep from root to /test/ * bun-types: remove ReadableStream.forEach method (this does not exist, probably added by mistake) * bun-polyfills: remove extraneous stream utils * bun-polyfills: add types syncing file * bun-polyfills: re-arrange global polyfills * bun-polyfills: fix FileBlob streams types again * bun-polyfills: sync all of @types/node * bun-polyfills: typeguard all current polyfills * bun-polyfills: fix import paths * bun-polyfills: switch to wasm impl. of farmhash * bun-polyfills: support default import of bun obj * bun-polyfills: transpiler placeholder file * bun-polyfills: loaderless import.meta polyfill * bun-polyfills: refactor import.meta polyfill * bun-polyfills: repl entrypoint & todo list index * bun-types: Add null to return type of Bun.which * bun-types: match Bun.sha with Bun.hash.SHA512_256 * bun-polyfills: new "repl" package.json script * bun-polyfills: full refactor of toplevel hashes * bun-polyfills: these are fixed * bun-types: NODE_ENV is optional * bun-polyfills: fix Bun.env types * bun-types+polyfills: fix HeapSnapshot.version type * bun-polyfills: fix some web streams type conflicts * bun-polyfills: update internal FileBlob.slice * bun-polyfills: fix subproc stdin conversions * bun-polyfills: better internal fileblob types * bun-polyfills: try to sync global performance type * bun-polyfills: working zig wasm polyfills setup * bun-polyfills: update scripts * bun-polyfills: fix wasm file location resolution * bun-polyfills: goodbye farmhash (replaced by zig) * bun-polyfills: move all Bun.hash polyfills to zig * bun-polyfills: reimpl. seeding of seeded hashes * bun-polyfills: impl. undocumented murmur32v2 * bun-polyfills: switch zighash from jsdoc to .d.ts * bun-types: partial fix of Hash types * bun-polyfills: documented Hash.murmur32v2 * bun-polyfills: misc updates * bun-polyfills: enable sourcemaps * bun-polyfills: handle empty inputs to hash funcs * bun-types: narrow down hash func types * bun-polyfills: remove unnecessary bigint casts * bun-polyfills: impl. Bun.isMainThread * bun-polyfills: impl. Bun.sleep and fix sleepSync * bun-polyfills: impl. indexOfLine * bun-polyfills: impl. Bun.peek.status * bun-types: fix hashing test --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Diffstat (limited to 'packages/bun-polyfills/lib/zighash/index.mjs')
-rw-r--r--packages/bun-polyfills/lib/zighash/index.mjs95
1 files changed, 95 insertions, 0 deletions
diff --git a/packages/bun-polyfills/lib/zighash/index.mjs b/packages/bun-polyfills/lib/zighash/index.mjs
new file mode 100644
index 000000000..f92ffb6d0
--- /dev/null
+++ b/packages/bun-polyfills/lib/zighash/index.mjs
@@ -0,0 +1,95 @@
+// @ts-check
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const { instance } = /** @type {ZighashInstance} */(
+ await WebAssembly.instantiate(
+ fs.readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), 'zighash.wasm')),
+ {
+ env: {
+ /** @param {any} x */
+ print(x) { console.log(x); },
+ },
+ }
+ )
+);
+const exports = instance.exports;
+const mem = exports.memory;
+const memview = {
+ get u8() { return new Uint8Array(mem.buffer); },
+ get u16() { return new Uint16Array(mem.buffer); },
+ get u32() { return new Uint32Array(mem.buffer); },
+ get u64() { return new BigUint64Array(mem.buffer); },
+ get i8() { return new Int8Array(mem.buffer); },
+ get i16() { return new Int16Array(mem.buffer); },
+ get i32() { return new Int32Array(mem.buffer); },
+ get i64() { return new BigInt64Array(mem.buffer); },
+ get f32() { return new Float32Array(mem.buffer); },
+ get f64() { return new Float64Array(mem.buffer); },
+};
+
+const nullptr = { ptr: -1, size: 0 };
+const encoder = new TextEncoder();
+
+const allocBuffer = (
+ /** @type {ArrayBufferView | ArrayBuffer | SharedArrayBuffer} */ buf,
+ /** @type {boolean=} */ nullTerminate = false,
+) => {
+ const size = buf.byteLength + +nullTerminate;
+ if (size === 0) return nullptr;
+ const ptr = exports.alloc(size);
+ if (ptr === -1) throw new Error('WASM memory allocation failed');
+ const u8heap = memview.u8;
+ u8heap.set(new Uint8Array(ArrayBuffer.isView(buf) ? buf.buffer : buf), ptr);
+ if (nullTerminate) u8heap[ptr + buf.byteLength] = 0;
+ return { ptr, size };
+};
+const allocString = (
+ /** @type {string} */ str,
+ /** @type {boolean=} */ nullTerminate = true,
+) => {
+ const strbuf = encoder.encode(str);
+ return allocBuffer(strbuf, nullTerminate);
+};
+
+/** @type {JSSeededHash64Function} */
+export function wyhash(input = '', seed = 0n) {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return BigInt.asUintN(64, exports.wyhash(ptr, size, seed));
+}
+/** @type {JSHash32Function} */
+export function adler32(input = '') {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return exports.adler32(ptr, size) >>> 0;
+}
+/** @type {JSHash32Function} */
+export function crc32(input = '') {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return exports.crc32(ptr, size) >>> 0;
+}
+/** @type {JSHash32Function} */
+export function cityhash32(input = '') {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return exports.cityhash32(ptr, size) >>> 0;
+}
+/** @type {JSSeededHash64Function} */
+export function cityhash64(input = '', seed = 0n) {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return BigInt.asUintN(64, exports.cityhash64(ptr, size, seed));
+}
+/** @type {JSSeededHash32Function} */
+export function murmur32v3(input = '', seed = 0) {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return exports.murmur32v3(ptr, size, seed); //! Bun doesn't unsigned-cast this one, likely unintended but for now we'll do the same
+}
+/** @type {JSSeededHash32Function} */
+export function murmur32v2(input = '', seed = 0) {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return exports.murmur32v2(ptr, size, seed); //! Bun doesn't unsigned-cast this one, likely unintended but for now we'll do the same
+}
+/** @type {JSSeededHash64Function} */
+export function murmur64v2(input = '', seed = 0n) {
+ const { ptr, size } = typeof input === 'string' ? allocString(input, false) : allocBuffer(input);
+ return BigInt.asUintN(64, exports.murmur64v2(ptr, size, seed));
+}