-
Notifications
You must be signed in to change notification settings - Fork 537
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
base: master
Are you sure you want to change the base?
Conversation
🚀 Regression report for commit 08cc46b is at https://web-php-regression-report-pr-1097.preview.thephp.foundation |
🚀 Preview for commit 08cc46b can be found at https://web-php-pr-1097.preview.thephp.foundation |
321a169
to
0dedf8e
Compare
This comment was marked as duplicate.
This comment was marked as duplicate.
js/interactive-examples.js
Outdated
|
||
async function main() { | ||
const buffer = []; | ||
const {ccall} = await phpBinary({ |
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.
const {ccall} = await phpBinary({ | |
const {ccall} = await phpBinary({ |
(mixed tabs and spaces)
js/interactive-examples.js
Outdated
document.querySelectorAll('.example').forEach((example) => { | ||
const button = document.createElement('button'); | ||
const phpcode = example.querySelector('.phpcode'); |
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.
document.querySelectorAll('.example').forEach((example) => { | |
const button = document.createElement('button'); | |
const phpcode = example.querySelector('.phpcode'); | |
document.querySelectorAll('.example').forEach((example) => { | |
const button = document.createElement('button'); | |
const phpcode = example.querySelector('.phpcode'); |
js/interactive-examples.js
Outdated
button.innerText = 'Run code'; | ||
button.onclick = function() { |
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.
button.innerText = 'Run code'; | |
button.onclick = function() { | |
button.innerText = 'Run code'; | |
button.onclick = function() { |
js/interactive-examples.js
Outdated
}; | ||
|
||
phpcode.parentNode.insertBefore(button, phpcode); | ||
}); |
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.
}; | |
phpcode.parentNode.insertBefore(button, phpcode); | |
}); | |
}; | |
phpcode.parentNode.insertBefore(button, phpcode); | |
}); |
js/interactive-examples.js
Outdated
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'); | ||
pre.innerText = output; | ||
div.appendChild(pre) | ||
return container; |
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.
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'); | |
pre.innerText = output; | |
div.appendChild(pre) | |
return container; | |
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'); | |
pre.innerText = output; | |
div.appendChild(pre); | |
return container; |
CS
js/interactive-examples.js
Outdated
button.innerText = 'Run code'; | ||
button.onclick = function() { | ||
if (lastOutput && lastOutput.parentNode) { | ||
lastOutput.parentNode.removeChild(lastOutput) |
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.
much simpler: lastOutput.remove()
(there is no reason to care about unmaintained browsers not supporting the remove
method when you are adding a feature requiring WebAssembly to work)
js/interactive-examples.js
Outdated
let lastOutput = null | ||
|
||
document.querySelectorAll('.example').forEach((example) => { | ||
const button = document.createElement('button'); |
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.
the created button should use the button
type instead of the default submit
type, to avoid issues in case this ends up being used in a place being inside a form.
js/interactive-examples.js
Outdated
lastOutput.parentNode.removeChild(lastOutput) | ||
} | ||
|
||
ccall("phpw_run", null, ["string"], ['?>' + phpcode.innerText]); |
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.
should this use .innerText
or .textContent
here ? innerText
is impacted by the styles of the page
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.
no idea 🤷
js/interactive-examples.js
Outdated
buffer.length = 0; | ||
}; | ||
|
||
phpcode.parentNode.insertBefore(button, phpcode); |
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.
use phpcode.before()
instead
Disabling many extensions as you did in soyuka/php-wasm#7 is likely to make many examples non working though (in your build, even JSON functions are not available) |
Definitely, there are a few things to discuss, I can enable most extensions, some will require additional libraries (for example dom or xml require libxml) and it'll add weight to the build (now it's "only" 3.6 MB). |
$path = dirname(__DIR__) . '/js/' . $filename; | ||
echo '<script type="module" src="/cached.php?t=' . @filemtime($path) . '&f=/js/' . $filename . '"></script>' . "\n"; | ||
} | ||
?> |
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?
As I understand, php binary is always loaded now, right? If yes, does it make sense to load it only when the user clicks "Run" for the first time? |
done |
Note that we should use brotli or gzip though, I'm unfamiliar with the server architecture behind the php docs website, should I add this directly to the cached.php file? |
@derickr is likely the person that would know this. |
It looks like php.net isn't particularly well set-up for encoding content. You can see how CSS/JS/etc is served up by the I'm not sure how much of the encoding happens on our server vs. what Myra does on the edge, but there's probably room for improvement there even outside of encoding the WASM bundle appropriately. The current web configuration isn't in the systems repo yet, though. |
If the modified code can still be highlighted, that would be even better. |
@lwlwilliam this would require bringing an actual code editor (Codemirror or Monaco) instead of just using |
@lwlwilliam, @stof, it makes sense for sure, but perhaps as step two in a separate PR? Just to keep this one smaller and delivered sooner. I'm pretty sure some unforeseen bugs will be discovered when this is deployed, and it'll be easier to fix them with smaller steps. |
@soyuka, what do you think about adding the PHP version in the output label? |
Myra already caches
I can ask whether they can do on the fly-compression, but I am not sure whether they do. To answer @jimwins, the config isn't in the systems repo, as nothing on php-web4 has been "php-ified" yet. I think Sascha set it up with nginx in his own way. There is no I believe adding it to |
I gave a trydiff --git a/cached.php b/cached.php
index 766afb2e..c8055ef7 100644
--- a/cached.php
+++ b/cached.php
@@ -46,6 +46,14 @@
header("Content-Type: application/javascript");
} elseif (substr($abs, -4) == ".css") {
header("Content-Type: text/css");
+} elseif (substr($abs, -4) == ".wasm") {
+ header("Content-Type: application/wasm");
+}
+
+$gzipped = $abs . '.gz';
+if (file_exists($gzipped) && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
+ $abs = $gzipped;
+ header("Content-Encoding: gzip");
}
readfile($abs); But as the Javascript is requiring the @pronskiy added version: |
Co-authored-by: lwlinux <[email protected]>
I don't quite understand whether your "try" was succesful or not. I can only see an image. The javascript should surely be calling And we deploy on nginx, so you should test with that, and not with something else. I have just added the nginx configuration to the systems repository: https://github.com/php/systems/tree/master/php-web4/nginx |
Should there be only one version or all of the supported in a dropdown/select? |
@shaedrich, for the sake of simplicity of this first step, it's better to stick to just one version. The dropdown sounds like a good idea for the next step. |
Probably want to add js/php-web.wasm filter=lfs diff=lfs merge=lfs binary to |
Adds a PHP Wasm binary to run documentation examples in the browser. We discussed this with @pronskiy (
fixes php/doc-en#3259).
php-doc-2.mp4
I've builtin
PHP wasm 8.4.0-dev
and I'm not sure what would be preferred to keep this up to date. If you want to reproduce the build see:soyuka/php-wasm#7
Clone that branch and execute:
Work sponsored by Les-Tilleuls.coop inline with what we built at the API Platform Playground more informations on a blog post