/** * 运行策略 V2 - 整合后端支持 * 支持本地运行(JS/Python)和远程运行(其他语言) */ import type { RunResult, RunnerStrategy, TestCase } from "../types"; import { BackendRunner } from "../core/BackendRunner"; // 创建后端运行器实例(可配置) let backendRunner: BackendRunner | null = null; /** * 配置后端运行器 */ export function configureBackendRunner(config: { apiUrl?: string; timeout?: number; debug?: boolean; }) { backendRunner = new BackendRunner(config); } /** * 获取或创建后端运行器 */ function getBackendRunner(): BackendRunner { if (!backendRunner) { backendRunner = new BackendRunner({ apiUrl: process.env.VITE_API_URL || "http://192.168.60.98:8080", debug: process.env.NODE_ENV === "development", }); } return backendRunner; } /** * JavaScript/TypeScript 本地运行策略(改进版) */ async function runJavaScriptLocal( code: string, testCases?: TestCase[], ): Promise { const startTime = performance.now(); try { // 1. 无测试用例:Console 模式 if (!testCases || testCases.length === 0) { const logs: string[] = []; const customConsole = { log: (...args: any[]) => { logs.push( args .map((arg) => { if (arg === undefined) return "undefined"; if (arg === null) return "null"; return typeof arg === "object" ? JSON.stringify(arg, null, 2) : String(arg); }) .join(" "), ); }, error: (...args: any[]) => logs.push("ERROR: " + args.join(" ")), warn: (...args: any[]) => logs.push("WARN: " + args.join(" ")), }; const fn = new Function("console", code); fn(customConsole); return { output: logs.join("\n") || "(无输出)", executionTime: performance.now() - startTime, status: "success", }; } // 2. 有测试用例:通过后端运行(获得更好的隔离和错误处理) const backend = getBackendRunner(); return await backend.run("javascript", code, testCases); } catch (error: any) { return { output: "", error: error.message || String(error), executionTime: performance.now() - startTime, status: "error", }; } } /** * Python 运行策略(使用后端) */ async function runPython( code: string, testCases?: TestCase[], ): Promise { try { const backend = getBackendRunner(); return await backend.run("python", code, testCases); } catch (error: any) { return { output: "", error: `Python 运行失败: ${error.message}`, executionTime: 0, status: "error", }; } } /** * 远程运行策略(通过后端) */ async function runRemote( code: string, language: string, testCases?: TestCase[], ): Promise { try { const backend = getBackendRunner(); return await backend.run(language, code, testCases); } catch (error: any) { return { output: "", error: `${language} 运行失败: ${error.message}`, executionTime: 0, status: "error", }; } } /** * 运行策略映射 V2 */ export const RUNNER_STRATEGIES_V2: Record = { // 本地运行 javascript: runJavaScriptLocal, typescript: runJavaScriptLocal, // 后端运行 python: runPython, java: (code, testCases) => runRemote(code, "java", testCases), cpp: (code, testCases) => runRemote(code, "cpp", testCases), c: (code, testCases) => runRemote(code, "c", testCases), go: (code, testCases) => runRemote(code, "go", testCases), rust: (code, testCases) => runRemote(code, "rust", testCases), }; /** * 获取运行策略 V2 */ export function getRunnerStrategyV2( language: string, ): RunnerStrategy | undefined { return RUNNER_STRATEGIES_V2[language]; } /** * 检查语言是否支持 */ export function isLanguageSupported(language: string): boolean { return language in RUNNER_STRATEGIES_V2; } /** * 获取支持的语言列表 */ export function getSupportedLanguages(): string[] { return Object.keys(RUNNER_STRATEGIES_V2); } /** * 检查后端健康状态 */ export async function checkBackendHealth(): Promise { try { const backend = getBackendRunner(); return await backend.healthCheck(); } catch { return false; } }