From 612fad706f66d8f830ef9a6efbc33f49a1343ea5 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 18 Apr 2022 20:51:10 -0700 Subject: [PATCH] fix wasm build take 2 (one more thing is broken) --- .gitignore | 5 + Makefile | 13 ++ bun.lockb | Bin 7689 -> 7689 bytes package.json | 4 +- packages/bun-types/types.d.ts | 2 +- packages/bun-wasm/bun.lockb | Bin 0 -> 6328 bytes packages/bun-wasm/index.ts | 346 +++++++++++++++++++++++++++++ packages/bun-wasm/package.json | 30 +++ packages/bun-wasm/test/node.mjs | 17 ++ packages/bun-wasm/tsconfig.json | 21 ++ src/api/bun.lockb | Bin 0 -> 5938 bytes src/api/package.json | 7 + src/api/schema.d.ts | 383 ++++++++++++++++---------------- src/api/tsconfig.json | 6 + src/main_wasm.zig | 2 +- 15 files changed, 641 insertions(+), 195 deletions(-) create mode 100755 packages/bun-wasm/bun.lockb create mode 100644 packages/bun-wasm/index.ts create mode 100644 packages/bun-wasm/package.json create mode 100644 packages/bun-wasm/test/node.mjs create mode 100644 packages/bun-wasm/tsconfig.json create mode 100755 src/api/bun.lockb create mode 100644 src/api/package.json create mode 100644 src/api/tsconfig.json diff --git a/.gitignore b/.gitignore index 8012f5ee4a..d369eab41c 100644 --- a/.gitignore +++ b/.gitignore @@ -90,3 +90,8 @@ src/deps/PLCrashReporter/ *.dSYM *.crash misctools/sha +packages/bun-wasm/*.mjs +packages/bun-wasm/*.cjs +packages/bun-wasm/*.map +packages/bun-wasm/*.js +packages/bun-wasm/*.d.ts diff --git a/Makefile b/Makefile index 018bedae94..d0c52e53be 100644 --- a/Makefile +++ b/Makefile @@ -465,6 +465,19 @@ build-obj-wasm-small: -o packages/bun-freestanding-wasm32/bun-wasm.wasm cp packages/bun-freestanding-wasm32/bun-wasm.wasm src/api/demo/public/bun-wasm.wasm +wasm: api build-obj-wasm-small + @rm -rf packages/bun-wasm/*.{d.ts,js,wasm,cjs,mjs,tsbuildinfo} + @cp packages/bun-freestanding-wasm32/bun-wasm.wasm packages/bun-wasm/bun.wasm + @cp src/api/schema.d.ts packages/bun-wasm/schema.d.ts + @cp src/api/schema.js packages/bun-wasm/schema.js + @cd packages/bun-wasm && $(NPM_CLIENT) run tsc -- -p . + @esbuild --sourcemap=external --external:fs --define:process.env.NODE_ENV="production" --outdir=packages/bun-wasm --target=esnext --bundle packages/bun-wasm/index.ts --format=esm --minify > /dev/null + @mv packages/bun-wasm/index.js packages/bun-wasm/index.mjs + @mv packages/bun-wasm/index.js.map packages/bun-wasm/index.mjs.map + @esbuild --sourcemap=external --external:fs --define:process.env.NODE_ENV="production" --outdir=packages/bun-wasm --target=esnext --bundle packages/bun-wasm/index.ts --format=cjs --minify --platform=node > /dev/null + @mv packages/bun-wasm/index.js packages/bun-wasm/index.cjs + @mv packages/bun-wasm/index.js.map packages/bun-wasm/index.cjs.map + @rm -rf packages/bun-wasm/*.tsbuildinfo build-obj-safe: $(ZIG) build obj -Drelease-safe diff --git a/bun.lockb b/bun.lockb index a45acac0402d5280f477de38ad6817ca0294027c..4e2475280d2f03bea253d7496719f1b241af8e0c 100755 GIT binary patch delta 262 zcmeCQ>9m=kr!Yh5b;&-n?VKtzB;GS^4TzB3=Fw*6rnmW6BWw4J(0c!kew!K16Q1s6 zYzSXdw8xo+k%56hXwS=rqFbyC1#1d&L3|i6fbt`rKiSL3kQq1Gg6RXJ!sJ4>^vx5P z|FSvci#V*%+IP)Zm9h41-Z`r!dkSWhcX6Gy%U!-|f7~gnlMZn+-4>Uwuz7k;;XU(| zAGPkmd)Iwd{_^kj&Ex(nl)oEnKFejoW&?IiK!Kx9ToTCQPb-;W_A`8_DgryuV?{wO bOb}U&X>y>T@a6!)PpnMFMw?y4kFo&(tLR9m=kr%<}7_ULlQ_Mci4zcSao`?0>lbC!9{g;5beOg$-uzGz;J+(9ZWZ{LTI2An9sl<4yGqtFnwT@m|Vz~zIg)kUp9w5 zJMOb|h{?@(-(+@RXXclrZ2k?|@87@S+3lH`v?lFd_+{TYH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@2o25(jBxdxgh`lxpx(1wzOmXMgv3b8PshSu^ARu$$MuK~P)z%KX= ze!~1fTo6CR3-Lib;6L~a{(%XwA5Ge_Bp2jg*+j#;)*NhqtWEG0`~nPq0S5bksWk8n z`oABG`w-*r$76jE3h*Faz&tXhpWx2{0y@jn+{!z|8l4oYo;fx9=aYhS6|P69D3}^s z8e5o?8VEv$D=jMcuWnBb2-jQ`6?`m>&5V(Z4p>DpxGK`iF+d#TrwrFo5Xa0InIZ^W z>A{s1#Ifk+;F=Py;q-Fskv6o#GF+WO9P@4&Tz|s#o?aOj(l=b$>E+;B6t4Yrak}F1 zLo#R=$#8Y3S0)H?&|Z__I#MqOSHy4ys+WUnX1GR$IN&w<+t7EUearM6Y2PxD#r^;gsXLmNQ;o8 literal 0 HcmV?d00001 diff --git a/packages/bun-wasm/index.ts b/packages/bun-wasm/index.ts new file mode 100644 index 0000000000..78582c84b9 --- /dev/null +++ b/packages/bun-wasm/index.ts @@ -0,0 +1,346 @@ +import { ByteBuffer } from "peechy/bb"; +import { + decodeScanResult, + decodeTransformResponse, + encodeScan, + encodeTransform, + Loader as BunLoader, + type ScanResult, + type TransformResponse, +} from "./schema"; +export enum Loader { + jsx = BunLoader.jsx, + js = BunLoader.js, + tsx = BunLoader.tsx, + ts = BunLoader.ts, +} + +export type { ScanResult, TransformResponse }; + +function normalizeLoader(file_name: string, loader?: Loader): BunLoader { + return ( + (loader + ? { + jsx: BunLoader.jsx, + tsx: BunLoader.tsx, + ts: BunLoader.ts, + js: BunLoader.js, + }[loader] + : null) || + { + ".jsx": BunLoader.jsx, + ".tsx": BunLoader.tsx, + ".ts": BunLoader.ts, + ".js": BunLoader.js, + }[file_name.substring(file_name.lastIndexOf("."))] || + BunLoader.js + ); +} + +interface WebAssemblyModule { + init(): number; + transform(a: number): number; + bun_malloc(a: number): number; + bun_free(a: number): number; + scan(a: number): number; +} + +const wasm_imports_sym: symbol | string = + //@ts-ignore + process.env.NODE_ENV === "development" + ? "wasm_imports" + : Symbol("wasm_imports"); + +const ptr_converter = new ArrayBuffer(16); +const ptr_float = new BigUint64Array(ptr_converter); +const slice = new Uint32Array(ptr_converter); + +const Wasi = { + clock_time_get(clk_id, tp) { + return Date.now(); + }, + environ_sizes_get() { + debugger; + return 0; + }, + environ_get(__environ, environ_buf) { + debugger; + return 0; + }, + + fd_close(fd) { + debugger; + return 0; + }, + proc_exit() {}, + + fd_seek(fd, offset_bigint, whence, newOffset) { + debugger; + }, + fd_write(fd, iov, iovcnt, pnum) { + debugger; + }, +}; + +var scratch: Uint8Array; +var scratch2: Uint8Array; + +export class Bun { + static has_initialized = false; + // @ts-ignore-line + static wasm_source: WebAssembly.WebAssemblyInstantiatedSource = null; + static get wasm_exports(): WebAssemblyModule { + return Bun.wasm_source.instance.exports as any; + } + // @ts-ignore-line + static get memory(): WebAssembly.Memory { + return Bun.wasm_source.instance.exports.memory as any; + } + + static memory_array: Uint8Array; + + static _decoder: TextDecoder; + + static _wasmPtrToSlice(offset: number | bigint) { + ptr_float[0] = typeof offset === "number" ? BigInt(offset) : offset; + return new Uint8Array(Bun.memory.buffer, slice[0], slice[1]); + } + + static _wasmPtrLenToString(slice: number) { + if (!Bun._decoder) { + Bun._decoder = new TextDecoder("utf8"); + } + + const region = Bun._wasmPtrToSlice(slice); + return Bun._decoder.decode(region); + } + + // We don't want people to be calling these manually + // @ts-ignore-line + static [wasm_imports_sym as symbol] = { + console_log(slice: number) { + console.log(Bun._wasmPtrLenToString(slice)); + }, + console_error(slice: number) { + console.error(Bun._wasmPtrLenToString(slice)); + }, + console_warn(slice: number) { + console.warn(Bun._wasmPtrLenToString(slice)); + }, + console_info(slice: number) { + console.info(Bun._wasmPtrLenToString(slice)); + }, + // @ts-ignore-line + __indirect_function_table: new WebAssembly.Table({ + initial: 0, + element: "anyfunc", + }), + // @ts-ignore-line + __stack_pointer: new WebAssembly.Global({ + mutable: true, + value: "i32", + }), + __multi3(one: number, two: number) { + return Math.imul(one | 0, two | 0); + }, + fmod(one: number, two: number) { + return one % two; + }, + memset(ptr: number, value: number, len: number) { + Bun.memory_array.fill(value, ptr, ptr + len); + }, + memcpy(ptr: number, value: number, len: number) { + Bun.memory_array.copyWithin(ptr, value, value + len); + }, + // These functions convert a to an unsigned long long, rounding toward zero. Negative values all become zero. + __fixunsdfti(a: number) { + return Math.floor(a); + }, + // These functions return the remainder of the unsigned division of a and b. + __umodti3(a: number, b: number) { + return (a | 0) % (b | 0); + }, + // These functions return the quotient of the unsigned division of a and b. + __udivti3(a: number, b: number) { + return (a | 0) / (b | 0); + }, + // These functions return the result of shifting a left by b bits. + __ashlti3(a: number, b: number) { + return (a | 0) >> (b | 0); + }, + /* Returns: convert a to a double, rounding toward even. */ + __floatuntidf(a: number) { + const mod = a % 2; + if (mod === 0) { + return Math.ceil(a); + } else if (mod === 1) { + return Math.floor(a); + } + }, + emscripten_notify_memory_growth() {}, + }; + + static async init(url, fetch = globalThis.fetch) { + // globalThis.sucraseTransform = sucraseTransform; + scratch = new Uint8Array(8096); + + if (Bun.has_initialized) { + return; + } + + if (globalThis?.WebAssembly?.instantiateStreaming) { + Bun.wasm_source = await globalThis.WebAssembly.instantiateStreaming( + fetch(url), + { env: Bun[wasm_imports_sym], wasi_snapshot_preview1: Wasi } + ); + } else if (typeof window !== "undefined") { + const resp = await fetch(url); + Bun.wasm_source = await globalThis.WebAssembly.instantiate( + await resp.arrayBuffer(), + { + env: Bun[wasm_imports_sym], + wasi_snapshot_preview1: Wasi, + } + ); + // is it node? + } else { + //@ts-ignore + const fs = await import("fs"); + + Bun.wasm_source = await globalThis.WebAssembly.instantiate( + fs.readFileSync(url), + { + env: Bun[wasm_imports_sym], + wasi_snapshot_preview1: Wasi, + } + ); + } + + const res = Bun.wasm_exports.init(); + if (res < 0) { + throw `[Bun] Failed to initialize WASM module: code ${res}`; + } + + Bun.has_initialized = true; + } + + static transformSync( + content: Uint8Array | string, + file_name: string, + loader?: Loader + ): TransformResponse { + if (!Bun.has_initialized) { + throw "Please run await Bun.init(wasm_url) before using this."; + } + + // if (process.env.NODE_ENV === "development") { + // console.time("[Bun] Transform " + file_name); + // } + + const bb = new ByteBuffer(scratch); + bb.length = 0; + bb.index = 0; + var contents_buffer; + if (typeof content === "string") { + if (!scratch2) { + scratch2 = new Uint8Array(content.length * 2); + } + + let i = 0; + for (; i < content.length; i++) { + if (i > scratch2.length) { + var scratch3 = new Uint8Array(scratch2.length * 2); + scratch3.set(scratch2); + scratch2 = scratch3; + } + scratch2[i] = content.charCodeAt(i); + } + contents_buffer = scratch2.subarray(0, i); + } else { + contents_buffer = content; + } + encodeTransform( + { + contents: contents_buffer, + path: file_name, + // @ts-ignore + loader: normalizeLoader(file_name, loader), + }, + bb + ); + const data = bb.toUint8Array(); + + const input_ptr = Bun.wasm_exports.bun_malloc(data.length); + var buffer = Bun._wasmPtrToSlice(input_ptr); + buffer.set(data); + + const resp_ptr = Bun.wasm_exports.transform(input_ptr); + + var _bb = new ByteBuffer(Bun._wasmPtrToSlice(resp_ptr)); + + const response = decodeTransformResponse(_bb); + Bun.wasm_exports.bun_free(input_ptr); + scratch = bb.data; + return response; + } + + static scan( + content: Uint8Array | string, + file_name: string, + loader?: Loader + ): ScanResult { + if (!Bun.has_initialized) { + throw "Please run await Bun.init(wasm_url) before using this."; + } + + // if (process.env.NODE_ENV === "development") { + // console.time("[Bun] Transform " + file_name); + // } + scratch.fill(0); + const bb = new ByteBuffer(scratch); + bb.length = 0; + bb.index = 0; + var contents_buffer; + if (typeof content === "string") { + if (!scratch2) { + scratch2 = new Uint8Array(content.length * 2); + } + const encode_into = new TextEncoder().encodeInto(content, scratch2); + contents_buffer = scratch2.subarray(0, encode_into.written); + } else { + contents_buffer = content; + } + + encodeScan( + { + contents: contents_buffer, + path: file_name, + // @ts-ignore + loader: normalizeLoader(file_name, loader), + }, + bb + ); + const data = bb.toUint8Array(); + + const input_ptr = Bun.wasm_exports.bun_malloc(data.length); + var buffer = Bun._wasmPtrToSlice(input_ptr); + buffer.set(data); + + const resp_ptr = Bun.wasm_exports.scan(input_ptr); + var _bb = new ByteBuffer(Bun._wasmPtrToSlice(resp_ptr)); + const response = decodeScanResult(_bb); + Bun.wasm_exports.bun_free(input_ptr); + scratch = bb.data; + return response; + } +} + +export const transformSync = Bun.transformSync; +export const scan = Bun.scan; +export const init = Bun.init; +export default Bun; + +if ("window" in globalThis && !("Bun" in globalThis)) { + // @ts-ignore-line + globalThis.Bun = Bun; +} diff --git a/packages/bun-wasm/package.json b/packages/bun-wasm/package.json new file mode 100644 index 0000000000..5f3234e408 --- /dev/null +++ b/packages/bun-wasm/package.json @@ -0,0 +1,30 @@ +{ + "name": "bun-wasm", + "version": "0.0.79", + "devDependencies": { + "peechy": "0.4.32", + "typescript": "latest" + }, + "files": [ + "bun.wasm", + "schema.d.ts", + "index.mjs", + "index.cjs", + "index.mjs.map", + "index.cjs.map", + "index.d.ts", + "schema.js" + ], + "type": "module", + "types": "index.d.ts", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.cjs", + "default": "./index.mjs" + }, + "./schema": "./schema.js", + "./wasm": "./bun.wasm", + "./bun.wasm": "./bun.wasm" + } +} diff --git a/packages/bun-wasm/test/node.mjs b/packages/bun-wasm/test/node.mjs new file mode 100644 index 0000000000..832b3f67bf --- /dev/null +++ b/packages/bun-wasm/test/node.mjs @@ -0,0 +1,17 @@ +import * as Bun from "../index.mjs"; + +await Bun.init(new URL("../bun.wasm", import.meta.url)); + +const hey = Bun.transformSync( + ` + +export function hi() { + return true; +} + +`, + "hi.js", + "js" +); + +console.log(JSON.stringify(hey, null, 2)); diff --git a/packages/bun-wasm/tsconfig.json b/packages/bun-wasm/tsconfig.json new file mode 100644 index 0000000000..cb966c6845 --- /dev/null +++ b/packages/bun-wasm/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "target": "esnext", + "module": "esnext", + "declaration": true, + "esModuleInterop": true, + "isolatedModules": false, + "skipLibCheck": true, + "emitDeclarationOnly": true, + "outDir": ".", + "baseUrl": "." + }, + "include": [ + "./node_modules/peechy", + "./schema.d.ts", + "./index.ts", + "./schema.js" + ], + "exclude": [] +} diff --git a/src/api/bun.lockb b/src/api/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..90a1dd4c9f1409bb76d620e22b5ef560654f5839 GIT binary patch literal 5938 zcmeHLd010d7JooQh>Ac_M4^I!(y#^?RGKKtVx@{30wc&6Ado-^NW$WR#yz8sEr{U4 zDvP+HC8DAzE*(?^MMPv%>jFa+5CxZlIQN8i<$X-)sGYI%$DHqb`MrDYJ->VIcJ9kD zG_Z^mMOgarg_c}ll)hi2kd7X6L70C?AYZ^W4-AV4<%rFrtmp~^L9BbOch_@|T~_;@ zn9;_Y^bS3f=9wH3bZXC^F5P$QZJ^=qk*E<8&0Zi>NE}R}gv4*u$QJ@Stq+(E-u(l6`EJ%B06mdu#omrl$Dy zGFdEpNKz$3>spb*lE*~~#6o7!XkVBP!j98Br-Pt?yov*~>#xRk&|V2Wzvfea8z^HtumOR7KScg0J3Jp4|DCVD*H7FJ#{YJI zcoqsX!jLa2xH52#1tSP~5oLVF_;Cm;kK6ZAlP5bO&e=>AxLdWQFaBmZGhAbNSJ z)U;XYKDDD71Xr!1xp8F*O+J@pMybX+y2{r>r1lTk&qK_nMWwPl7>gLHnN8IV>jE+| zT19os%ELGly1HngLzu`%%!}s|+@^(^dDRVJ+t)qNKXzPm{AAPo*?)IU@7`6B?DMTG zi~Wzpyv^g7TTb{}%TjJQZ0{h>Ce}?)474&;iyjp0#yrJ)O5w%(S?}+-_ z$g1K&`{TUtt6y_=8&-O$t;T;s+vv{qDb^HTd~eD9W%L9wyp~@X-Eb(RJtif!$voBB zHDLZ06>o{1f_e9KT7KP`*4Czr=an8j+9_xVHy%}eI4PE>p7U5%0%oyYy_gnZ+~al^xwJ3D^13T)5qV!OuOjXyif16c?8ckC}^|9~Ol0 z*35aazHnaAVq@KE*H`6;2hRikasg^u%bOEPA_wiNw29d#jGEIP*FMW$l+<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