Skip to content

Commit

Permalink
feat(eval): optimize eval behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 17, 2021
1 parent 74690ee commit 9c64fb5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
9 changes: 9 additions & 0 deletions packages/plugin-eval/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-eval/src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
18 changes: 3 additions & 15 deletions packages/plugin-eval/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
},
"homepage": "https://github.com/koishijs/koishi#readme",
"devDependencies": {
"@types/cheerio": "^0.22.23",
"@types/qrcode": "^1.3.5"
},
"peerDependencies": {
Expand Down

0 comments on commit 9c64fb5

Please sign in to comment.