|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 5 | + <meta name="description" content="Arjun in your browser"> |
| 6 | + <meta name="keywords" content="Arjun, Functional Synthesis, WebAssembly, JavaScript, Emscripten"> |
| 7 | + <meta name="author" content="Mate Soos"> |
| 8 | + <meta charset="UTF-8"> |
| 9 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 10 | + <title>Arjun in your browser</title> |
| 11 | + <style> |
| 12 | + body { |
| 13 | + font-family: Arial, sans-serif; |
| 14 | + max-width: 800px; |
| 15 | + margin: 0 auto; |
| 16 | + padding: 20px; |
| 17 | + line-height: 1.6; |
| 18 | + } |
| 19 | + .container { |
| 20 | + display: flex; |
| 21 | + flex-direction: column; |
| 22 | + gap: 20px; |
| 23 | + } |
| 24 | + textarea { |
| 25 | + width: 100%; |
| 26 | + height: 200px; |
| 27 | + padding: 10px; |
| 28 | + box-sizing: border-box; |
| 29 | + font-family: monospace; |
| 30 | + border: 1px solid #ddd; |
| 31 | + border-radius: 4px; |
| 32 | + } |
| 33 | + button { |
| 34 | + padding: 10px 15px; |
| 35 | + background-color: #4CAF50; |
| 36 | + color: white; |
| 37 | + border: none; |
| 38 | + border-radius: 4px; |
| 39 | + cursor: pointer; |
| 40 | + font-size: 16px; |
| 41 | + transition: background-color 0.3s; |
| 42 | + } |
| 43 | + button:hover { |
| 44 | + background-color: #45a049; |
| 45 | + } |
| 46 | + button:disabled { |
| 47 | + background-color: #cccccc; |
| 48 | + cursor: not-allowed; |
| 49 | + } |
| 50 | + #output { |
| 51 | + border: 1px solid #ddd; |
| 52 | + padding: 15px; |
| 53 | + min-height: 100px; |
| 54 | + white-space: pre-wrap; |
| 55 | + background-color: #f9f9f9; |
| 56 | + border-radius: 4px; |
| 57 | + font-family: monospace; |
| 58 | + overflow-y: auto; |
| 59 | + max-height: 400px; |
| 60 | + } |
| 61 | + .status { |
| 62 | + font-style: italic; |
| 63 | + color: #666; |
| 64 | + margin-left: 10px; |
| 65 | + } |
| 66 | + label { |
| 67 | + font-weight: bold; |
| 68 | + display: block; |
| 69 | + margin-bottom: 5px; |
| 70 | + } |
| 71 | + .progress-container { |
| 72 | + width: 100%; |
| 73 | + background-color: #f1f1f1; |
| 74 | + border-radius: 4px; |
| 75 | + margin-top: 10px; |
| 76 | + } |
| 77 | + #progress-bar { |
| 78 | + height: 10px; |
| 79 | + background-color: #4CAF50; |
| 80 | + border-radius: 4px; |
| 81 | + width: 0%; |
| 82 | + transition: width 0.3s; |
| 83 | + } |
| 84 | + </style> |
| 85 | +</head> |
| 86 | +<body> |
| 87 | + <div class="container"> |
| 88 | + <h1>Arjun Functional Synthesis in Your Browser</h1> |
| 89 | + <div>Copy your CNF formula in the input box below and click "Run |
| 90 | + arjun.js" to run the Functional Synthesis engine in your browser. |
| 91 | + The output will be displayed below the input box. Format |
| 92 | + description is available <a |
| 93 | + href="https://jix.github.io/varisat/manual/0.2.0/formats/dimacs.html">here</a></div> |
| 94 | + You need to have "c p show ... 0" for all the variables that are inputs |
| 95 | + <div> |
| 96 | + <label for="extra-args">Extra command line arguments:</label> |
| 97 | + <input type="text" id="extra-args" |
| 98 | + value='--mstrategy "const(max_repairs=400),const(max_repairs=400,inv_learnt=1),bve"' |
| 99 | + style="width:100%;padding:8px;box-sizing:border-box;font-family:monospace;border:1px solid #ddd;border-radius:4px;"> |
| 100 | + </div> |
| 101 | + |
| 102 | + <div> |
| 103 | + <label for="input-content">CNF:</label> |
| 104 | + <textarea id="input-content">p cnf 3 3 |
| 105 | +-1 -2 3 0 |
| 106 | +1 -3 0 |
| 107 | +2 -3 0 |
| 108 | +c p show 1 2 0 |
| 109 | +</textarea> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div> |
| 113 | + <button id="run-button">Run arjun.js</button> |
| 114 | + <button id="cancel-button">Cancel</button> |
| 115 | + <span id="status" class="status">Loading WebAssembly module...</span> |
| 116 | + <div class="progress-container"> |
| 117 | + <div id="progress-bar"></div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div> |
| 122 | + <label for="output">Output:</label> |
| 123 | + <div id="output">Ready - enter your input and click "Run arjun.js"</div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + |
| 127 | + <script> |
| 128 | + // Main thread Module configuration |
| 129 | + var Module = { |
| 130 | + noInitialRun: true, |
| 131 | + stdin: function() { return null; }, |
| 132 | + onRuntimeInitialized: function() { |
| 133 | + document.getElementById('status').textContent = 'Ready'; |
| 134 | + document.getElementById('run-button').disabled = false; |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + // Create worker with proper WASM loading |
| 139 | + function createEmscriptenWorker() { |
| 140 | + const workerCode = ` |
| 141 | + var Module = { |
| 142 | + noInitialRun: true, |
| 143 | + stdin: function() { return null; }, |
| 144 | + print: function(text) { |
| 145 | + postMessage({ type: 'output', content: text }); |
| 146 | + }, |
| 147 | + printErr: function(text) { |
| 148 | + console.error('Error:', text); |
| 149 | + //postMessage({ type: 'output', content: 'ERROR: ' + text }); |
| 150 | + }, |
| 151 | + onRuntimeInitialized: function() { |
| 152 | + postMessage({ type: 'ready' }); |
| 153 | + }, |
| 154 | + locateFile: function(path) { |
| 155 | + console.log('Locating file:', path); |
| 156 | + // Resolve correct path for WASM file |
| 157 | + if (path.endsWith('.wasm')) { |
| 158 | + return self.workerOptions.wasmPath || 'arjun.wasm'; |
| 159 | + } |
| 160 | + return path; |
| 161 | + } |
| 162 | + }; |
| 163 | +
|
| 164 | + // This will be replaced by the Emscripten-generated code |
| 165 | + var wasmBinary; |
| 166 | +
|
| 167 | + onmessage = function(e) { |
| 168 | + if (e.data.type === 'init') { |
| 169 | + console.log('Initializing worker with options:', e.data.options); |
| 170 | + self.workerOptions = e.data.options; |
| 171 | + importScripts(e.data.options.jsPath); |
| 172 | + } |
| 173 | + else if (e.data.type === 'run') { |
| 174 | + try { |
| 175 | + console.log('Running with args:', e.data.args); |
| 176 | + Module.FS.writeFile('/input.cnf', e.data.input); |
| 177 | + var args = ['--verb', '1', '--synth'].concat(e.data.extraArgs || []).concat(['/input.cnf']); |
| 178 | + Module.callMain(args); |
| 179 | + postMessage({ type: 'done' }); |
| 180 | + } catch (e) { |
| 181 | + postMessage({ type: 'error', content: e.toString() }); |
| 182 | + } |
| 183 | + } |
| 184 | + }; |
| 185 | + `; |
| 186 | + |
| 187 | + const blob = new Blob([workerCode], { type: 'application/javascript' }); |
| 188 | + return URL.createObjectURL(blob); |
| 189 | + } |
| 190 | + |
| 191 | + // Main execution logic |
| 192 | + function parseExtraArgs(str) { |
| 193 | + // Shell-like tokenizer: handles "quoted strings" and unquoted tokens |
| 194 | + const args = []; |
| 195 | + let i = 0; |
| 196 | + while (i < str.length) { |
| 197 | + while (i < str.length && str[i] === ' ') i++; |
| 198 | + if (i >= str.length) break; |
| 199 | + if (str[i] === '"') { |
| 200 | + i++; |
| 201 | + let start = i; |
| 202 | + while (i < str.length && str[i] !== '"') i++; |
| 203 | + args.push(str.slice(start, i)); |
| 204 | + if (i < str.length) i++; // skip closing quote |
| 205 | + } else { |
| 206 | + let start = i; |
| 207 | + while (i < str.length && str[i] !== ' ') i++; |
| 208 | + args.push(str.slice(start, i)); |
| 209 | + } |
| 210 | + } |
| 211 | + return args; |
| 212 | + } |
| 213 | + |
| 214 | + document.getElementById('run-button').addEventListener('click', async function() { |
| 215 | + const inputContent = document.getElementById('input-content').value; |
| 216 | + const outputDiv = document.getElementById('output'); |
| 217 | + const extraArgs = parseExtraArgs(document.getElementById('extra-args').value); |
| 218 | + |
| 219 | + if (!inputContent.trim()) { |
| 220 | + outputDiv.textContent = 'Error: Please enter input content'; |
| 221 | + return; |
| 222 | + } |
| 223 | + |
| 224 | + outputDiv.textContent = 'Initializing...\n'; |
| 225 | + document.getElementById('run-button').disabled = true; |
| 226 | + document.getElementById('status').textContent = 'Running...'; |
| 227 | + |
| 228 | + // Get absolute URL for WASM file |
| 229 | + const wasmPath = new URL('arjun.wasm', window.location.href).href; |
| 230 | + const jsPath = new URL('arjun.js', window.location.href).href; |
| 231 | + const workerUrl = createEmscriptenWorker(); |
| 232 | + const worker = new Worker(workerUrl); |
| 233 | + |
| 234 | + worker.onmessage = function(e) { |
| 235 | + switch (e.data.type) { |
| 236 | + case 'output': |
| 237 | + console.log('Output:', e.data.content); |
| 238 | + outputDiv.textContent += e.data.content + '\n'; |
| 239 | + outputDiv.scrollTop = outputDiv.scrollHeight; |
| 240 | + break; |
| 241 | + case 'ready': |
| 242 | + //console.log('ready. Sending input:', inputContent); |
| 243 | + worker.postMessage({ |
| 244 | + type: 'run', |
| 245 | + input: inputContent, |
| 246 | + extraArgs: extraArgs |
| 247 | + }); |
| 248 | + break; |
| 249 | + case 'done': |
| 250 | + document.getElementById('status').textContent = 'Completed'; |
| 251 | + document.getElementById('run-button').disabled = false; |
| 252 | + worker.terminate(); |
| 253 | + URL.revokeObjectURL(workerUrl); |
| 254 | + break; |
| 255 | + case 'error': |
| 256 | + outputDiv.textContent += 'Error: ' + e.data.content + '\n'; |
| 257 | + document.getElementById('status').textContent = 'Failed'; |
| 258 | + document.getElementById('run-button').disabled = false; |
| 259 | + worker.terminate(); |
| 260 | + URL.revokeObjectURL(workerUrl); |
| 261 | + break; |
| 262 | + } |
| 263 | + }; |
| 264 | + |
| 265 | + // Initialize worker with proper paths |
| 266 | + worker.postMessage({ |
| 267 | + type: 'init', |
| 268 | + options: { |
| 269 | + jsPath: jsPath, |
| 270 | + wasmPath: wasmPath |
| 271 | + } |
| 272 | + }); |
| 273 | + }); |
| 274 | + |
| 275 | + // Load arjun.js in main thread |
| 276 | + const script = document.createElement('script'); |
| 277 | + script.src = 'arjun.js'; |
| 278 | + document.body.appendChild(script); |
| 279 | + </script> |
| 280 | +</body> |
| 281 | +</html> |
0 commit comments