aboutsummaryrefslogtreecommitdiffhomepage
path: root/worker/php/exec.mjs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-02-16 22:49:07 +0900
committernsfisis <nsfisis@gmail.com>2026-02-16 22:49:07 +0900
commitefe05c1444963c046ab91bf54fa51a794bda58c0 (patch)
tree509b48f27d2e888740bea6bfd6f50895705c7472 /worker/php/exec.mjs
parentdb87f85aa7055e597800481b8cc6d006c70bcc88 (diff)
downloadphperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.tar.gz
phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.tar.zst
phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.zip
test(worker): add unit tests for php and swift workers
Extract testable logic from exec.mjs into lib.mjs (preprocessCode, createIOCallbacks, buildResult) and add vitest tests. Add Go tests for models, exec helpers, and handlers in worker/swift. Update justfiles to include test tasks for local dev and CI. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'worker/php/exec.mjs')
-rw-r--r--worker/php/exec.mjs78
1 files changed, 8 insertions, 70 deletions
diff --git a/worker/php/exec.mjs b/worker/php/exec.mjs
index d8ca899..21bd93f 100644
--- a/worker/php/exec.mjs
+++ b/worker/php/exec.mjs
@@ -1,65 +1,14 @@
import PHPWasm from "./php-wasm.js";
+import { buildResult, createIOCallbacks, preprocessCode } from "./lib.mjs";
process.once("message", async ({ code: originalCode, input }) => {
- const PRELUDE = `
- define('STDIN', fopen('php://stdin', 'r'));
- define('STDOUT', fopen('php://stdout', 'r'));
- define('STDERR', fopen('php://stderr', 'r'));
-
- error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
-
- `;
-
- // remove php tag
- let code;
- if (originalCode.startsWith("<?php")) {
- code = PRELUDE + originalCode.slice(5);
- } else if (originalCode.startsWith("<?")) {
- code = PRELUDE + originalCode.slice(2);
- } else {
- code = PRELUDE + originalCode;
- }
-
- const BUFFER_MAX = 10 * 1024;
-
- let stdinPos = 0; // bytewise
- const stdinBuf = Buffer.from(input);
- let stdoutPos = 0; // bytewise
- const stdoutBuf = Buffer.alloc(BUFFER_MAX);
- let stderrPos = 0; // bytewise
- const stderrBuf = Buffer.alloc(BUFFER_MAX);
+ const code = preprocessCode(originalCode);
+ const io = createIOCallbacks(input);
const { ccall } = await PHPWasm({
- stdin: () => {
- if (stdinBuf.length <= stdinPos) {
- return null;
- }
- return stdinBuf.readUInt8(stdinPos++);
- },
- stdout: (asciiCode) => {
- if (asciiCode === null) {
- return; // flush
- }
- if (BUFFER_MAX <= stdoutPos) {
- return; // ignore
- }
- stdoutBuf.writeUInt8(
- asciiCode < 0 ? asciiCode + 256 : asciiCode,
- stdoutPos++,
- );
- },
- stderr: (asciiCode) => {
- if (asciiCode === null) {
- return; // flush
- }
- if (BUFFER_MAX <= stderrPos) {
- return; // ignore
- }
- stderrBuf.writeUInt8(
- asciiCode < 0 ? asciiCode + 256 : asciiCode,
- stderrPos++,
- );
- },
+ stdin: io.stdin,
+ stdout: io.stdout,
+ stderr: io.stderr,
});
let err;
@@ -69,17 +18,6 @@ process.once("message", async ({ code: originalCode, input }) => {
} catch (e) {
err = e;
}
- if (err) {
- process.send({
- status: "runtime_error",
- stdout: stdoutBuf.subarray(0, stdoutPos).toString(),
- stderr: `${stderrBuf.subarray(0, stderrPos).toString()}\n${err.toString()}`,
- });
- } else {
- process.send({
- status: result === 0 ? "success" : "runtime_error",
- stdout: stdoutBuf.subarray(0, stdoutPos).toString(),
- stderr: stderrBuf.subarray(0, stderrPos).toString(),
- });
- }
+
+ process.send(buildResult(err, result, io.getStdout, io.getStderr));
});