Skip to content

Commit

Permalink
load wasm only when running code
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 17, 2024
1 parent 3624847 commit ab85c31
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions js/interactive-examples.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,77 @@
import phpBinary from "/js/php-web.mjs";

function createOutput(output) {
const container = document.createElement('div');
container.classList.add('screen', 'example-contents');
const title = document.createElement('p');
title.innerText = 'Output: ';
container.appendChild(title)
const div = document.createElement('div');
div.classList.add('examplescode');
const container = document.createElement("div");
container.classList.add("screen", "example-contents");
const title = document.createElement("p");
title.innerText = "Output: ";
container.appendChild(title);
const div = document.createElement("div");
div.classList.add("examplescode");
container.appendChild(div);
const pre = document.createElement('pre');
pre.classList.add('examplescode');
const pre = document.createElement("pre");
pre.classList.add("examplescode");
pre.innerText = output;
div.appendChild(pre)
div.appendChild(pre);
return container;
}

async function main() {
const buffer = [];
const { ccall } = await phpBinary({
print(data) {
if (!data) {
return;
}

if (buffer.length) {
buffer.push('\n');
}
buffer.push(data);
class PHP {
static buffer = [];
static runPhp = null;
static async loadPhp() {
if (PHP.runPhp) {
return PHP.runPhp;
}
})

console.log("PHP wasm %s loaded.", ccall("phpw_exec", "string", ["string"], ["phpversion();"]));
let lastOutput = null
const { ccall } = await phpBinary({
print(data) {
if (!data) {
return;
}

if (PHP.buffer.length) {
PHP.buffer.push("\n");
}
PHP.buffer.push(data);
},
});

document.querySelectorAll('.example').forEach((example) => {
const button = document.createElement('button');
button.setAttribute('type', 'button')
const phpcode = example.querySelector('.phpcode');
console.log(
"PHP wasm %s loaded.",
ccall("phpw_exec", "string", ["string"], ["phpversion();"]),
);
PHP.runPhp = (code) => ccall("phpw_run", null, ["string"], ["?>" + code]);
return PHP.runPhp;
}
}

async function main() {
let lastOutput = null;

const code = phpcode.querySelector('pre code')
code.setAttribute('contentEditable', true)
document.querySelectorAll(".example").forEach((example) => {
const button = document.createElement("button");
button.setAttribute("type", "button");
const phpcode = example.querySelector(".phpcode");

button.innerText = 'Run code';
button.onclick = function() {
const code = phpcode.querySelector("code");
code.setAttribute("contentEditable", true);

button.innerText = "Run code";
button.onclick = async function () {
if (lastOutput && lastOutput.parentNode) {
lastOutput.remove()
lastOutput.remove();
}

ccall("phpw_run", null, ["string"], ['?>' + phpcode.innerText]);
lastOutput = createOutput(buffer.join(''))
const runPhp = await PHP.loadPhp();
runPhp(phpcode.innerText);
lastOutput = createOutput(PHP.buffer.join(""));
phpcode.parentNode.appendChild(lastOutput);
buffer.length = 0;
PHP.buffer.length = 0;
};

phpcode.before(button);
});

}

main()
main();

0 comments on commit ab85c31

Please sign in to comment.