Skip to content

Commit

Permalink
[Update] Add parse and run ast (#37 & #41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siubaak committed May 4, 2020
1 parent 0eaece4 commit 83cc851
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getOwnNames, createSandBox, globalObj } from './share/util'
import { getOwnNames, createSandBox, globalObj, assign } from './share/util'
import { version } from '../package.json'
import { parse, Options } from 'acorn'
import { Node, Program } from 'estree'
import Scope from './scope'

import { hoist } from './evaluate_n/helper'
Expand Down Expand Up @@ -58,9 +59,16 @@ class Sval {
}
}

run(code: string) {
const ast = parse(code, this.options) as any
hoist(ast, this.scope)
parse(code: string, parser?: (code: string, options: Options) => Node) {
if (typeof parser === 'function') {
return parser(code, assign({}, this.options))
}
return parse(code, this.options)
}

run(code: string | Node) {
const ast = typeof code === 'string' ? parse(code, this.options) as Node : code
hoist(ast as Program, this.scope)
evaluate(ast, this.scope)
}
}
Expand Down

0 comments on commit 83cc851

Please sign in to comment.