aboutsummaryrefslogtreecommitdiffhomepage
path: root/worker/php/exec.mjs
blob: 650d4dfd05300bdd0cb92dd699fcdb06088f4078 (plain)
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
import {
  buildResult,
  createIOCallbacks,
  preprocessCode,
  validateCode,
} from "./lib.mjs";
import PHPWasm from "./php-wasm.js";

process.once("message", async ({ code: originalCode, input }) => {
  const validationError = validateCode(originalCode);
  if (validationError) {
    process.send({
      status: "runtime_error",
      stdout: "",
      stderr: validationError,
    });
    return;
  }

  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));
});