aboutsummaryrefslogtreecommitdiffhomepage
path: root/worker/php/exec.mjs
blob: 21bd93f3c8952477ce8dee18b556b03f6ced8f06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import PHPWasm from "./php-wasm.js";
import { buildResult, createIOCallbacks, preprocessCode } from "./lib.mjs";

process.once("message", async ({ code: originalCode, input }) => {
	const code = preprocessCode(originalCode);
	const io = createIOCallbacks(input);

	const { ccall } = await PHPWasm({
		stdin: io.stdin,
		stdout: io.stdout,
		stderr: io.stderr,
	});

	let err;
	let result;
	try {
		result = ccall("php_wasm_run", "number", ["string"], [code]);
	} catch (e) {
		err = e;
	}

	process.send(buildResult(err, result, io.getStdout, io.getStderr));
});