also looking at this
Port to latest zig-master
#2
53 files
+1149
-1017
| @@ -29,14 +29,11 @@ This will produce `regex.wasm` which can be loaded in browsers or Node.js. | ||
| 29 | 29 | |
| 30 | 30 | ```javascript |
| 31 | 31 | // Load the WASM module |
| 32 | const wasmModule = await WebAssembly.instantiateStreaming( | |
| 33 | fetch('regex.wasm'), | |
| 34 | { | |
| 35 | env: { | |
| 36 | // Provide any required imports | |
| 37 | } | |
| 38 | } | |
| 39 | ); | |
| 32 | const wasmModule = await WebAssembly.instantiateStreaming(fetch("regex.wasm"), { | |
| 33 | env: { | |
| 34 | // Provide any required imports | |
| 35 | }, | |
| 36 | }); | |
| 40 | 37 | |
| 41 | 38 | // Use the exported functions |
| 42 | 39 | const exports = wasmModule.instance.exports; |
| @@ -48,7 +45,7 @@ const regex = exports.zig_regex_compile(pattern); | ||
| 48 | 45 | // Check for match |
| 49 | 46 | const input = "test 123"; |
| 50 | 47 | const matches = exports.zig_regex_is_match(regex, input); |
| 51 | console.log('Matches:', matches === 1); | |
| 48 | console.log("Matches:", matches === 1); | |
| 52 | 49 | |
| 53 | 50 | // Clean up |
| 54 | 51 | exports.zig_regex_free(regex); |
| @@ -57,28 +54,29 @@ exports.zig_regex_free(regex); | ||
| 57 | 54 | ### Node.js Usage |
| 58 | 55 | |
| 59 | 56 | ```javascript |
| 60 | const fs = require('fs'); | |
| 61 | const path = require('path'); | |
| 57 | const fs = require("fs"); | |
| 58 | const path = require("path"); | |
| 62 | 59 | |
| 63 | 60 | // Load WASM file |
| 64 | const wasmPath = path.join(__dirname, 'regex.wasm'); | |
| 61 | const wasmPath = path.join(__dirname, "regex.wasm"); | |
| 65 | 62 | const wasmBuffer = fs.readFileSync(wasmPath); |
| 66 | 63 | |
| 67 | 64 | // Instantiate |
| 68 | 65 | WebAssembly.instantiate(wasmBuffer, { |
| 69 | env: { | |
| 70 | // Provide imports if needed | |
| 71 | } | |
| 72 | }).then(result => { | |
| 73 | const { zig_regex_compile, zig_regex_is_match, zig_regex_free } = result.instance.exports; | |
| 66 | env: { | |
| 67 | // Provide imports if needed | |
| 68 | }, | |
| 69 | }).then((result) => { | |
| 70 | const { zig_regex_compile, zig_regex_is_match, zig_regex_free } = | |
| 71 | result.instance.exports; | |
| 74 | 72 | |
| 75 | // Use the library | |
| 76 | const regex = zig_regex_compile("hello.*world"); | |
| 77 | const matches = zig_regex_is_match(regex, "hello beautiful world"); | |
| 73 | // Use the library | |
| 74 | const regex = zig_regex_compile("hello.*world"); | |
| 75 | const matches = zig_regex_is_match(regex, "hello beautiful world"); | |
| 78 | 76 | |
| 79 | console.log('Pattern matches:', matches === 1); | |
| 77 | console.log("Pattern matches:", matches === 1); | |
| 80 | 78 | |
| 81 | zig_regex_free(regex); | |
| 79 | zig_regex_free(regex); | |
| 82 | 80 | }); |
| 83 | 81 | ``` |
| 84 | 82 | |
| @@ -89,43 +87,43 @@ Create a TypeScript wrapper for type safety: | ||
| 89 | 87 | ```typescript |
| 90 | 88 | // regex.d.ts |
| 91 | 89 | export interface WasmRegex { |
| 92 | compile(pattern: string): number | null; | |
| 93 | isMatch(regex: number, input: string): boolean; | |
| 94 | find(regex: number, input: string): Match | null; | |
| 95 | free(regex: number): void; | |
| 90 | compile(pattern: string): number | null; | |
| 91 | isMatch(regex: number, input: string): boolean; | |
| 92 | find(regex: number, input: string): Match | null; | |
| 93 | free(regex: number): void; | |
| 96 | 94 | } |
| 97 | 95 | |
| 98 | 96 | export interface Match { |
| 99 | text: string; | |
| 100 | start: number; | |
| 101 | end: number; | |
| 97 | text: string; | |
| 98 | start: number; | |
| 99 | end: number; | |
| 102 | 100 | } |
| 103 | 101 | |
| 104 | 102 | // regex.ts |
| 105 | 103 | export class Regex { |
| 106 | private wasm: WasmRegex; | |
| 107 | private handle: number; | |
| 108 | ||
| 109 | constructor(wasm: WasmRegex, pattern: string) { | |
| 110 | this.wasm = wasm; | |
| 111 | const handle = wasm.compile(pattern); | |
| 112 | if (handle === null) { | |
| 113 | throw new Error('Failed to compile regex pattern'); | |
| 114 | } | |
| 115 | this.handle = handle; | |
| 104 | private wasm: WasmRegex; | |
| 105 | private handle: number; | |
| 106 | ||
| 107 | constructor(wasm: WasmRegex, pattern: string) { | |
| 108 | this.wasm = wasm; | |
| 109 | const handle = wasm.compile(pattern); | |
| 110 | if (handle === null) { | |
| 111 | throw new Error("Failed to compile regex pattern"); | |
| 116 | 112 | } |
| 113 | this.handle = handle; | |
| 114 | } | |
| 117 | 115 | |
| 118 | isMatch(input: string): boolean { | |
| 119 | return this.wasm.isMatch(this.handle, input); | |
| 120 | } | |
| 116 | isMatch(input: string): boolean { | |
| 117 | return this.wasm.isMatch(this.handle, input); | |
| 118 | } | |
| 121 | 119 | |
| 122 | find(input: string): Match | null { | |
| 123 | return this.wasm.find(this.handle, input); | |
| 124 | } | |
| 120 | find(input: string): Match | null { | |
| 121 | return this.wasm.find(this.handle, input); | |
| 122 | } | |
| 125 | 123 | |
| 126 | free(): void { | |
| 127 | this.wasm.free(this.handle); | |
| 128 | } | |
| 124 | free(): void { | |
| 125 | this.wasm.free(this.handle); | |
| 126 | } | |
| 129 | 127 | } |
| 130 | 128 | ``` |
| 131 | 129 | |
| @@ -141,36 +139,36 @@ The WASM build uses the C allocator, which means: | ||
| 141 | 139 | |
| 142 | 140 | ```javascript |
| 143 | 141 | class RegexManager { |
| 144 | constructor(wasmExports) { | |
| 145 | this.exports = wasmExports; | |
| 146 | this.regexes = new Map(); | |
| 147 | } | |
| 148 | ||
| 149 | compile(pattern) { | |
| 150 | const handle = this.exports.zig_regex_compile(pattern); | |
| 151 | if (handle) { | |
| 152 | this.regexes.set(handle, pattern); | |
| 153 | } | |
| 154 | return handle; | |
| 142 | constructor(wasmExports) { | |
| 143 | this.exports = wasmExports; | |
| 144 | this.regexes = new Map(); | |
| 145 | } | |
| 146 | ||
| 147 | compile(pattern) { | |
| 148 | const handle = this.exports.zig_regex_compile(pattern); | |
| 149 | if (handle) { | |
| 150 | this.regexes.set(handle, pattern); | |
| 155 | 151 | } |
| 152 | return handle; | |
| 153 | } | |
| 156 | 154 | |
| 157 | isMatch(handle, input) { | |
| 158 | return this.exports.zig_regex_is_match(handle, input) === 1; | |
| 159 | } | |
| 155 | isMatch(handle, input) { | |
| 156 | return this.exports.zig_regex_is_match(handle, input) === 1; | |
| 157 | } | |
| 160 | 158 | |
| 161 | free(handle) { | |
| 162 | if (this.regexes.has(handle)) { | |
| 163 | this.exports.zig_regex_free(handle); | |
| 164 | this.regexes.delete(handle); | |
| 165 | } | |
| 159 | free(handle) { | |
| 160 | if (this.regexes.has(handle)) { | |
| 161 | this.exports.zig_regex_free(handle); | |
| 162 | this.regexes.delete(handle); | |
| 166 | 163 | } |
| 164 | } | |
| 167 | 165 | |
| 168 | freeAll() { | |
| 169 | for (const handle of this.regexes.keys()) { | |
| 170 | this.exports.zig_regex_free(handle); | |
| 171 | } | |
| 172 | this.regexes.clear(); | |
| 166 | freeAll() { | |
| 167 | for (const handle of this.regexes.keys()) { | |
| 168 | this.exports.zig_regex_free(handle); | |
| 173 | 169 | } |
| 170 | this.regexes.clear(); | |
| 171 | } | |
| 174 | 172 | } |
| 175 | 173 | ``` |
| 176 | 174 | |