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

Php wasm #55

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions docs/core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/core.js.map

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions esm/interpreter/php-wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { dedent } from '../utils.js';
import { io, stdio } from './_utils.js';

const type = 'php-wasm';

// TODO: almost nothing is implemented

// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export default {
type,
module: (version = '0.0.3') => `https://cdn.jsdelivr.net/npm/@webreflection/php@${version}/es.js`,
async engine({ PhpWeb }, _, url) {
const { stderr, get } = stdio();
const interpreter = await new Promise(resolve => {
let timer = 0, chunks = [];
const php = new PhpWeb({
print: (message) => {
chunks.push(message);
clearTimeout(timer);
timer = setTimeout(() => {
document.getElementById('target').innerHTML = chunks.splice(0).join('');
});
},
printErr: (message) => {
if (message) stderr(message);
},
locateFile: () => `${url.slice(0, url.lastIndexOf('/'))}/php-web.wasm`
});
php.addEventListener('ready', () => {
resolve(get(php));
});
});
// TODO: to be implemented
// if (config.files) await fetchFiles(this, interpreter, config.files);
// if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
return interpreter;
},
// Fallback to globally defined module fields
registerJSModule: () => {
// TODO: to be implemented
},
run: (interpreter, code, ...args) => {
// TODO: this is always async
try {
return interpreter.run(`<?php ${dedent(code)} ?>`, ...args);
}
catch (error) {
io.get(interpreter).stderr(error);
}
},
runAsync: async (interpreter, code, ...args) => {
try {
return await interpreter.run(`<?php ${dedent(code)} ?>`, ...args);
}
catch (error) {
io.get(interpreter).stderr(error);
}
},
runEvent: async () => {
// TODO: to be implemented
throw new SyntaxError('runEvent are not implemented');
},
transform: (_, value) => value,
writeFile: () => {
// TODO: to be implemented
throw new SyntaxError('writeFile is not implemented');
},
};
/* c8 ignore stop */
3 changes: 2 additions & 1 deletion esm/interpreters.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ const register = (interpreter) => {

//:RUNTIMES
import micropython from './interpreter/micropython.js';
import php_wasm from './interpreter/php-wasm.js';
import pyodide from './interpreter/pyodide.js';
import ruby_wasm_wasi from './interpreter/ruby-wasm-wasi.js';
import wasmoon from './interpreter/wasmoon.js';
for (const interpreter of [micropython, pyodide, ruby_wasm_wasi, wasmoon])
for (const interpreter of [micropython, php_wasm, pyodide, ruby_wasm_wasi, wasmoon])
register(interpreter);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@
"html-escaper": "^3.0.3"
},
"worker": {
"blob": "sha256-ixJNXrBnwM18zoc4l44JmnNzgD+eoNpGaOcZz3dXP94="
"blob": "sha256-i+V1onL/+r/OO5XheOe/tvzQ2olHFpxNdK0KmoQnHF4="
}
}
16 changes: 16 additions & 0 deletions test/php-wasm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>PHP</title>
<link rel="stylesheet" href="style.css" />
<script type="module" src="../core.js"></script>
</head>
<body>
<script type="php-wasm">
phpinfo();
</script>
<div id="target"></div>
</body>
</html>