From c06d46eae30c9468535fb6af5e9b822acadbbdb6 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 8 Mar 2025 10:51:01 +0900 Subject: trim php tag before execution --- backend/game/hub.go | 2 +- worker/exec.mjs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/game/hub.go b/backend/game/hub.go index 0aca96c..46cd321 100644 --- a/backend/game/hub.go +++ b/backend/game/hub.go @@ -38,7 +38,7 @@ func (hub *Hub) Run() { func (hub *Hub) CalcCodeSize(code string) int { re := regexp.MustCompile(`\s+`) - return len(re.ReplaceAllString(code, "")) + return len(strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(re.ReplaceAllString(code, ""), "")) } func (hub *Hub) EnqueueTestTasks(ctx context.Context, submissionID, gameID, userID int, code string) error { diff --git a/worker/exec.mjs b/worker/exec.mjs index 7d64cc3..fc0accd 100644 --- a/worker/exec.mjs +++ b/worker/exec.mjs @@ -1,12 +1,23 @@ import PHPWasm from "./php-wasm.js"; -process.once("message", async ({ code, input }) => { +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')); `; + + // remove php tag + let code; + if (originalCode.startsWith(" { let err; let result; try { - result = ccall("php_wasm_run", "number", ["string"], [PRELUDE + code]); + result = ccall("php_wasm_run", "number", ["string"], [code]); } catch (e) { err = e; } -- cgit v1.2.3-70-g09d2