From 9c64fb5d60352b8c4fd8731d2676e897f3c3ec0f Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 18 Jan 2021 01:17:49 +0800 Subject: [PATCH] feat(eval): optimize eval behavior --- packages/plugin-eval/src/index.ts | 9 +++++++++ packages/plugin-eval/src/vm.ts | 4 +--- packages/plugin-eval/src/worker.ts | 18 +++--------------- packages/plugin-tools/package.json | 1 - 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/plugin-eval/src/index.ts b/packages/plugin-eval/src/index.ts index 13ceda9985..d6718004a9 100644 --- a/packages/plugin-eval/src/index.ts +++ b/packages/plugin-eval/src/index.ts @@ -1,5 +1,6 @@ import { Context } from 'koishi-core' import { CQCode, Logger, defineProperty } from 'koishi-utils' +import { Script } from 'vm' import { EvalWorker, attachTraps, EvalConfig, Config, resolveAccess } from './main' export * from './main' @@ -75,6 +76,14 @@ export function apply(ctx: Context, config: Config = {}) { if (!expr) return '请输入要执行的脚本。' expr = CQCode.unescape(expr) + try { + Reflect.construct(Script, [expr, { filename: 'stdin' }]) + } catch (e) { + if (!(e instanceof SyntaxError)) throw e + const lines = e.stack.split('\n', 5) + return `${lines[4]}\n at ${lines[0]}:${lines[2].length}` + } + return await new Promise((resolve) => { logger.debug(expr) defineProperty(session, '_isEval', true) diff --git a/packages/plugin-eval/src/vm.ts b/packages/plugin-eval/src/vm.ts index e733b3e50f..250e8a216e 100644 --- a/packages/plugin-eval/src/vm.ts +++ b/packages/plugin-eval/src/vm.ts @@ -26,9 +26,7 @@ export class VM { const filename = resolve(__dirname, '../dist/internal.js') const data = readFileSync(filename, 'utf8') - const script = new Script(data, { - filename, - }) + const script = new Script(data, { filename }) script .runInContext(this.context, { displayErrors: false }) diff --git a/packages/plugin-eval/src/worker.ts b/packages/plugin-eval/src/worker.ts index 749ab0ba70..c52aa04456 100644 --- a/packages/plugin-eval/src/worker.ts +++ b/packages/plugin-eval/src/worker.ts @@ -48,16 +48,6 @@ function formatResult(...param: [string, ...any[]]) { export function formatError(error: Error) { if (!(error instanceof Error)) return `Uncaught: ${error}` - if (error.name === 'SyntaxError') { - // syntax error during compilation - const message = 'SyntaxError: ' + error.message - const lines = error.stack.split('\n') - const index = lines.indexOf(message) + 1 - if (lines[index].startsWith(' at new Script')) { - return `${message}\n at ${lines[0]}:${lines[2].length}` - } - } - return error.stack .replace(/\s*.+(Script|MessagePort)[\s\S]*/, '') .split('\n') @@ -139,13 +129,11 @@ export class WorkerAPI { let result: any try { - result = await vm.run(`{ - const { send, exec, user, group } = global[Symbol.for("${key}")]; - delete global[Symbol.for("${key}")]; - \n${source} + result = await vm.run(`with (global[Symbol.for("${key}")]) { + delete global[Symbol.for("${key}")];\n${source} }`, { filename: 'stdin', - lineOffset: -4, + lineOffset: -2, }) await this.sync(ctx) } catch (error) { diff --git a/packages/plugin-tools/package.json b/packages/plugin-tools/package.json index aa78811138..8a0cab421b 100644 --- a/packages/plugin-tools/package.json +++ b/packages/plugin-tools/package.json @@ -21,7 +21,6 @@ }, "homepage": "https://github.com/koishijs/koishi#readme", "devDependencies": { - "@types/cheerio": "^0.22.23", "@types/qrcode": "^1.3.5" }, "peerDependencies": {