fix: address Codex PR review findings
This commit is contained in:
1 parent
0f164b6de5
commit
a7974abb08
44 files changed
+3317
-667
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import variant from "@jitl/quickjs-singlefile-cjs-release-sync";
|
||||
import { newQuickJSWASMModuleFromVariant } from "quickjs-emscripten-core";
|
||||
import type { ScriptEvalResult, ScriptEvalLog } from "./types.js";
|
||||
import { transpileMvelSubset } from "./mvel-subset.js";
|
||||
|
||||
const MAX_MEMORY_BYTES = 32 * 1024 * 1024;
|
||||
const MAX_VALUE_BYTES = 1024 * 1024;
|
||||
@@ -127,38 +128,7 @@ export async function executeJavaScriptSandbox(
|
||||
* 转换 MVEL 常用语法为 QuickJS 可执行的 JavaScript,实现轻量仿真
|
||||
*/
|
||||
export function transpileMvelToJs(mvelCode: string): string {
|
||||
let js = mvelCode;
|
||||
|
||||
// 1. 移除 Java 类型声明:如 String x =, int y =, List z =, Map m =, boolean b =, double d =, long l =
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*=)/g, "var $2");
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*;)/g, "var $2");
|
||||
|
||||
// 2. 转换 new HashMap() / new ArrayList()
|
||||
js = js.replace(/new\s+(?:HashMap|Map)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "({})");
|
||||
js = js.replace(/new\s+(?:ArrayList|List)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "([])");
|
||||
|
||||
// 3. 常见 Map/List 方法调用模拟:
|
||||
// .put(k, v) -> [k] = v (包装为安全赋值辅助)
|
||||
// 注入轻量 helper
|
||||
const preamble = [
|
||||
"function _mvel_put(obj, key, val) { if (obj && typeof obj === 'object') { obj[key] = val; } return val; }",
|
||||
"function _mvel_get(obj, key) { return obj ? obj[key] : undefined; }",
|
||||
"function _mvel_size(obj) { if (!obj) return 0; if (Array.isArray(obj)) return obj.length; if (typeof obj === 'object') return Object.keys(obj).length; return 0; }",
|
||||
"function _mvel_add(arr, item) { if (Array.isArray(arr)) arr.push(item); return arr; }",
|
||||
"",
|
||||
].join("\n");
|
||||
|
||||
js = js.replace(/\.put\s*\(\s*([^,]+)\s*,\s*([^)]+)\s*\)/g, ""); // 避免直接正则冲突,使用下面逐行或更精确替换
|
||||
// 恢复并使用 helper
|
||||
js = mvelCode;
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*=)/g, "var $2");
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*;)/g, "var $2");
|
||||
js = js.replace(/new\s+(?:HashMap|Map)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "({})");
|
||||
js = js.replace(/new\s+(?:ArrayList|List)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "([])");
|
||||
js = js.replace(/\.put\s*\(\s*([^,]+)\s*,\s*([^)]+)\s*\)/g, "[$1] = $2");
|
||||
js = js.replace(/\.size\s*\(\s*\)/g, ".length");
|
||||
|
||||
return `${preamble}${js}`;
|
||||
return transpileMvelSubset(mvelCode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,7 +139,12 @@ export async function executeMvelSandbox(
|
||||
inputs: Record<string, unknown> = {},
|
||||
options: { timeoutMs?: number } = {}
|
||||
): Promise<ScriptEvalResult> {
|
||||
const transpiled = transpileMvelToJs(code);
|
||||
const result = await executeJavaScriptSandbox(transpiled, inputs, options);
|
||||
return result;
|
||||
const start = performance.now();
|
||||
try {
|
||||
const transpiled = transpileMvelToJs(code);
|
||||
return await executeJavaScriptSandbox(transpiled, inputs, options);
|
||||
} catch (error) {
|
||||
return { success: false, durationMs: Number((performance.now() - start).toFixed(2)), returnValue: undefined, logs: [],
|
||||
error: { name: "UnsupportedMvelSyntax", message: (error as Error).message }, inputs };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user