|
1 | 1 | import phpBinary from "/js/php-web.mjs"; |
2 | 2 |
|
3 | 3 | function createOutput(output) { |
4 | | - const container = document.createElement('div'); |
5 | | - container.classList.add('screen', 'example-contents'); |
6 | | - const title = document.createElement('p'); |
7 | | - title.innerText = 'Output: '; |
8 | | - container.appendChild(title) |
9 | | - const div = document.createElement('div'); |
10 | | - div.classList.add('examplescode'); |
| 4 | + const container = document.createElement("div"); |
| 5 | + container.classList.add("screen", "example-contents"); |
| 6 | + const title = document.createElement("p"); |
| 7 | + title.innerText = "Output: "; |
| 8 | + container.appendChild(title); |
| 9 | + const div = document.createElement("div"); |
| 10 | + div.classList.add("examplescode"); |
11 | 11 | container.appendChild(div); |
12 | | - const pre = document.createElement('pre'); |
13 | | - pre.classList.add('examplescode'); |
| 12 | + const pre = document.createElement("pre"); |
| 13 | + pre.classList.add("examplescode"); |
14 | 14 | pre.innerText = output; |
15 | | - div.appendChild(pre) |
| 15 | + div.appendChild(pre); |
16 | 16 | return container; |
17 | 17 | } |
18 | 18 |
|
19 | | -async function main() { |
20 | | - const buffer = []; |
21 | | - const { ccall } = await phpBinary({ |
22 | | - print(data) { |
23 | | - if (!data) { |
24 | | - return; |
25 | | - } |
26 | | - |
27 | | - if (buffer.length) { |
28 | | - buffer.push('\n'); |
29 | | - } |
30 | | - buffer.push(data); |
| 19 | +class PHP { |
| 20 | + static buffer = []; |
| 21 | + static runPhp = null; |
| 22 | + static async loadPhp() { |
| 23 | + if (PHP.runPhp) { |
| 24 | + return PHP.runPhp; |
31 | 25 | } |
32 | | - }) |
33 | 26 |
|
34 | | - console.log("PHP wasm %s loaded.", ccall("phpw_exec", "string", ["string"], ["phpversion();"])); |
35 | | - let lastOutput = null |
| 27 | + const { ccall } = await phpBinary({ |
| 28 | + print(data) { |
| 29 | + if (!data) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (PHP.buffer.length) { |
| 34 | + PHP.buffer.push("\n"); |
| 35 | + } |
| 36 | + PHP.buffer.push(data); |
| 37 | + }, |
| 38 | + }); |
36 | 39 |
|
37 | | - document.querySelectorAll('.example').forEach((example) => { |
38 | | - const button = document.createElement('button'); |
39 | | - button.setAttribute('type', 'button') |
40 | | - const phpcode = example.querySelector('.phpcode'); |
| 40 | + console.log( |
| 41 | + "PHP wasm %s loaded.", |
| 42 | + ccall("phpw_exec", "string", ["string"], ["phpversion();"]), |
| 43 | + ); |
| 44 | + PHP.runPhp = (code) => ccall("phpw_run", null, ["string"], ["?>" + code]); |
| 45 | + return PHP.runPhp; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +async function main() { |
| 50 | + let lastOutput = null; |
41 | 51 |
|
42 | | - const code = phpcode.querySelector('pre code') |
43 | | - code.setAttribute('contentEditable', true) |
| 52 | + document.querySelectorAll(".example").forEach((example) => { |
| 53 | + const button = document.createElement("button"); |
| 54 | + button.setAttribute("type", "button"); |
| 55 | + const phpcode = example.querySelector(".phpcode"); |
44 | 56 |
|
45 | | - button.innerText = 'Run code'; |
46 | | - button.onclick = function() { |
| 57 | + const code = phpcode.querySelector("code"); |
| 58 | + code.setAttribute("contentEditable", true); |
| 59 | + |
| 60 | + button.innerText = "Run code"; |
| 61 | + button.onclick = async function () { |
47 | 62 | if (lastOutput && lastOutput.parentNode) { |
48 | | - lastOutput.remove() |
| 63 | + lastOutput.remove(); |
49 | 64 | } |
50 | 65 |
|
51 | | - ccall("phpw_run", null, ["string"], ['?>' + phpcode.innerText]); |
52 | | - lastOutput = createOutput(buffer.join('')) |
| 66 | + const runPhp = await PHP.loadPhp(); |
| 67 | + runPhp(phpcode.innerText); |
| 68 | + lastOutput = createOutput(PHP.buffer.join("")); |
53 | 69 | phpcode.parentNode.appendChild(lastOutput); |
54 | | - buffer.length = 0; |
| 70 | + PHP.buffer.length = 0; |
55 | 71 | }; |
56 | 72 |
|
57 | 73 | phpcode.before(button); |
58 | 74 | }); |
59 | | - |
60 | 75 | } |
61 | 76 |
|
62 | | -main() |
| 77 | +main(); |
0 commit comments