2026-05-14 17:12:01 +08:00
|
|
|
// src/core.js
|
|
|
|
|
let _disableRunCodeError = false;
|
|
|
|
|
|
|
|
|
|
export function setDisableRunCodeError(value) {
|
2026-05-17 16:48:29 +08:00
|
|
|
_disableRunCodeError = value;
|
2026-05-14 17:12:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 17:49:33 +08:00
|
|
|
const _fnCache = new Map();
|
|
|
|
|
|
2026-05-14 17:12:01 +08:00
|
|
|
export function _runCode(code, vars, thisObj, extendVars) {
|
2026-05-17 16:48:29 +08:00
|
|
|
const argKeys = [...Object.keys(extendVars || {}), ...Object.keys(vars || {})];
|
|
|
|
|
const argValues = [...Object.values(extendVars || {}), ...Object.values(vars || {})];
|
2026-05-17 17:49:33 +08:00
|
|
|
const cacheKey = code + argKeys.join(',');
|
2026-05-17 16:48:29 +08:00
|
|
|
try {
|
2026-05-17 17:49:33 +08:00
|
|
|
let fn = _fnCache.get(cacheKey);
|
|
|
|
|
if (!fn) {
|
|
|
|
|
fn = new Function(...argKeys, code);
|
|
|
|
|
_fnCache.set(cacheKey, fn);
|
|
|
|
|
}
|
|
|
|
|
return fn.apply(thisObj, argValues);
|
2026-05-17 16:48:29 +08:00
|
|
|
} catch (e) {
|
|
|
|
|
if (!_disableRunCodeError) console.error(e, extendVars, [code, extendVars, vars, thisObj]);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-05-14 17:12:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function _returnCode(code, vars, thisObj, extendVars) {
|
2026-05-17 16:48:29 +08:00
|
|
|
if (code.includes('${')) return _runCode('return `' + code + '`', vars, thisObj, extendVars);
|
|
|
|
|
else return _runCode('return ' + code, vars, thisObj, extendVars);
|
2026-05-14 17:12:01 +08:00
|
|
|
}
|