ReviewOS

also looking at this

zig-utils/zig-regex

Port to latest zig-master

#2
Closed armagidon-exception wants to merge main into main
53 files +1149 -1017
docs/WASM.mdmodified+70-72
Changes to docs/WASM.md
@@ -29,14 +29,11 @@ This will produce `regex.wasm` which can be loaded in browsers or Node.js.
2929
3030```javascript
3131// Load the WASM module
32const wasmModule = await WebAssembly.instantiateStreaming(
33 fetch('regex.wasm'),
34 {
35 env: {
36 // Provide any required imports
37 }
38 }
39);
32const wasmModule = await WebAssembly.instantiateStreaming(fetch("regex.wasm"), {
33 env: {
34 // Provide any required imports
35 },
36});
4037
4138// Use the exported functions
4239const exports = wasmModule.instance.exports;
@@ -48,7 +45,7 @@ const regex = exports.zig_regex_compile(pattern);
4845// Check for match
4946const input = "test 123";
5047const matches = exports.zig_regex_is_match(regex, input);
51console.log('Matches:', matches === 1);
48console.log("Matches:", matches === 1);
5249
5350// Clean up
5451exports.zig_regex_free(regex);
@@ -57,28 +54,29 @@ exports.zig_regex_free(regex);
5754### Node.js Usage
5855
5956```javascript
60const fs = require('fs');
61const path = require('path');
57const fs = require("fs");
58const path = require("path");
6259
6360// Load WASM file
64const wasmPath = path.join(__dirname, 'regex.wasm');
61const wasmPath = path.join(__dirname, "regex.wasm");
6562const wasmBuffer = fs.readFileSync(wasmPath);
6663
6764// Instantiate
6865WebAssembly.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;
7472
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");
7876
79 console.log('Pattern matches:', matches === 1);
77 console.log("Pattern matches:", matches === 1);
8078
81 zig_regex_free(regex);
79 zig_regex_free(regex);
8280});
8381```
8482
@@ -89,43 +87,43 @@ Create a TypeScript wrapper for type safety:
8987```typescript
9088// regex.d.ts
9189export 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;
9694}
9795
9896export interface Match {
99 text: string;
100 start: number;
101 end: number;
97 text: string;
98 start: number;
99 end: number;
102100}
103101
104102// regex.ts
105103export 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");
116112 }
113 this.handle = handle;
114 }
117115
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 }
121119
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 }
125123
126 free(): void {
127 this.wasm.free(this.handle);
128 }
124 free(): void {
125 this.wasm.free(this.handle);
126 }
129127}
130128```
131129
@@ -141,36 +139,36 @@ The WASM build uses the C allocator, which means:
141139
142140```javascript
143141class 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);
155151 }
152 return handle;
153 }
156154
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 }
160158
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);
166163 }
164 }
167165
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);
173169 }
170 this.regexes.clear();
171 }
174172}
175173```
176174