Skip to content

Commit 2738d44

Browse files
Update script.js
1 parent 4a7a5ce commit 2738d44

1 file changed

Lines changed: 31 additions & 49 deletions

File tree

script.js

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -396,63 +396,45 @@ runBtn.addEventListener('click', async () => {
396396
runBtn.disabled = true;
397397
runBtn.innerHTML = '<div class="spinner"></div> Running...';
398398

399-
renderOutput('Running script...', true);
399+
renderOutput('Initializing execution engine...', true);
400400

401401
try {
402-
const response = await fetch(PISTON_API, {
403-
method: 'POST',
404-
headers: { 'Content-Type': 'application/json' },
405-
body: JSON.stringify({
406-
language: 'php',
407-
version: '8.2.3',
408-
files: [{ content: currentCode }],
409-
}),
410-
});
411-
412-
const text = await response.text();
413-
let result;
414-
try {
415-
result = JSON.parse(text);
416-
} catch {
417-
renderOutput(
418-
`Run failed: invalid response (${response.status})\n${text.slice(0, 500)}`,
419-
true
420-
);
421-
runBtn.disabled = false;
422-
runBtn.innerHTML = '<ion-icon name="play"></ion-icon> Run Code';
423-
return;
402+
if (!window.phpWasm) {
403+
renderOutput('Loading PHP WebAssembly engine (this may take a few seconds on first run)...', true);
404+
const module = await import('https://cdn.jsdelivr.net/npm/php-wasm/PhpWeb.mjs');
405+
window.phpWasm = new module.PhpWeb();
424406
}
425407

426-
if (!response.ok) {
427-
const msg = result.message || result.error || text || `HTTP ${response.status}`;
428-
renderOutput(`Run failed:\n${msg}`, true);
429-
runBtn.disabled = false;
430-
runBtn.innerHTML = '<ion-icon name="play"></ion-icon> Run Code';
431-
return;
432-
}
408+
let outputBuffer = '';
409+
let errorBuffer = '';
433410

434-
if (result.compile && result.compile.stderr) {
435-
renderOutput(`[Compile]\n${result.compile.stderr}`, true);
436-
}
411+
const outHandler = (e) => { outputBuffer += e.detail; };
412+
const errHandler = (e) => { errorBuffer += e.detail; };
437413

438-
if (result.run) {
439-
let output = result.run.stdout || '';
440-
if (result.run.stderr) {
441-
output += `${output ? '\n\n' : ''}[PHP stderr]:\n${result.run.stderr}`;
442-
}
443-
if (result.run.code !== 0 && result.run.code != null) {
444-
output += `${output ? '\n\n' : ''}[Exit code: ${result.run.code}]`;
445-
}
446-
if (!output.trim()) {
447-
output = 'Script finished (no output). Tip: session/cookie/forms need a real PHP server, not Piston.';
448-
}
449-
renderOutput(output);
450-
} else {
451-
const msg = result.message ? String(result.message) : JSON.stringify(result, null, 2);
452-
renderOutput(`Could not run:\n${msg}`, true);
414+
// Ensure engine is ready if not already
415+
renderOutput('Running script...', true);
416+
417+
window.phpWasm.addEventListener('output', outHandler);
418+
window.phpWasm.addEventListener('error', errHandler);
419+
420+
const exitCode = await window.phpWasm.run(currentCode);
421+
422+
window.phpWasm.removeEventListener('output', outHandler);
423+
window.phpWasm.removeEventListener('error', errHandler);
424+
425+
let output = outputBuffer || '';
426+
if (errorBuffer) {
427+
output += `${output ? '\n\n' : ''}[PHP stderr]:\n${errorBuffer}`;
428+
}
429+
if (exitCode !== 0 && exitCode != null && exitCode !== 255) {
430+
output += `${output ? '\n\n' : ''}[Exit code: ${exitCode}]`;
431+
}
432+
if (!output.trim()) {
433+
output = 'Script finished (no output). Tip: session/cookie/forms might need a full PHP local server setup.';
453434
}
435+
renderOutput(output);
454436
} catch (error) {
455-
renderOutput(`Execution API Error: ${error.message}`, true);
437+
renderOutput(`Execution Engine Error: ${error.message}`, true);
456438
}
457439

458440
runBtn.disabled = false;

0 commit comments

Comments
 (0)