Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run examples with php wasm #1097

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# iCalendar needs CRLF line endings, Git index and working tree will have CRLF
/backend/events/* -text
js/php-web.wasm filter=lfs diff=lfs merge=lfs binary
7 changes: 7 additions & 0 deletions include/footer.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ if (!empty($_SERVER['BASE_PAGE'])
echo '<script src="/cached.php?t=' . @filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
}
?>
<?php
$jsfiles = ["interactive-examples.js"];
foreach ($jsfiles as $filename) {
$path = dirname(__DIR__) . '/js/' . $filename;
echo '<script type="module" src="/cached.php?t=' . @filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
}
?>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe that this deserves only one line?


<a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/[email protected]"></a>

Expand Down
79 changes: 79 additions & 0 deletions js/interactive-examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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 (PHP "+ PHP.version +"):";
container.appendChild(title);
const div = document.createElement("div");
div.classList.add("examplescode");
container.appendChild(div);
const pre = document.createElement("pre");
pre.classList.add("examplescode");
pre.innerText = output;
div.appendChild(pre);
return container;
}

class PHP {
static buffer = [];
static runPhp = null;
static version = '';
static async loadPhp() {
if (PHP.runPhp) {
return PHP.runPhp;
}

const { ccall } = await phpBinary({
print(data) {
if (!data) {
return;
}

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

PHP.version = ccall("phpw_exec", "string", ["string"], ["phpversion();"]),
console.log("PHP wasm %s loaded.", PHP.version);
PHP.runPhp = (code) => ccall("phpw_run", null, ["string"], ["?>" + code]);
return PHP.runPhp;
}
}

async function main() {
let lastOutput = null;

document.querySelectorAll(".example .example-contents").forEach((example) => {
const button = document.createElement("button");
button.setAttribute("type", "button");
const phpcode = example.querySelector(".phpcode");
if (phpcode === null) {
return;
}

const code = phpcode.querySelector("code");
code.setAttribute("contentEditable", true);

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

const runPhp = await PHP.loadPhp();
runPhp(phpcode.innerText);
lastOutput = createOutput(PHP.buffer.join(""));
phpcode.parentNode.appendChild(lastOutput);
PHP.buffer.length = 0;
};

phpcode.before(button);
});
}

main();
16 changes: 16 additions & 0 deletions js/php-web.mjs

Large diffs are not rendered by default.

Binary file added js/php-web.wasm
Binary file not shown.