-
Notifications
You must be signed in to change notification settings - Fork 542
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
soyuka
wants to merge
7
commits into
php:master
Choose a base branch
from
soyuka:examples-php-wasm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51dbc7b
Run examples with php wasm
soyuka 0dedf8e
make examples editable
soyuka 3624847
review
soyuka ab85c31
load wasm only when running code
soyuka 6e65750
add version
soyuka 2e50fbc
handle empty code
soyuka 0708497
add gitattributes
soyuka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,13 @@ if (!empty($_SERVER['BASE_PAGE']) | |
echo '<script src="/cached.php?t=' . @filemtime($path) . '&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) . '&f=/js/' . $filename . '"></script>' . "\n"; | ||
} | ||
?> | ||
|
||
<a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/[email protected]"></a> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?