-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathphp-wasm.js
More file actions
70 lines (67 loc) · 2.31 KB
/
php-wasm.js
File metadata and controls
70 lines (67 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 */