|
@@ -161,6 +161,7 @@ type Runtime struct {
|
|
|
rand RandSource
|
|
|
now Now
|
|
|
_collator *collate.Collator
|
|
|
+ parserOptions []parser.Option
|
|
|
|
|
|
symbolRegistry map[unistring.String]*Symbol
|
|
|
|
|
@@ -1111,8 +1112,17 @@ func MustCompile(name, src string, strict bool) *Program {
|
|
|
return prg
|
|
|
}
|
|
|
|
|
|
-func compile(name, src string, strict, eval bool) (p *Program, err error) {
|
|
|
- prg, err1 := parser.ParseFile(nil, name, src, 0)
|
|
|
+// Parse takes a source string and produces a parsed AST. Use this function if you want to pass options
|
|
|
+// to the parser, e.g.:
|
|
|
+//
|
|
|
+// p, err := Parse("test.js", "var a = true", parser.WithDisableSourceMaps)
|
|
|
+// if err != nil { /* ... */ }
|
|
|
+// prg, err := CompileAST(p, true)
|
|
|
+// // ...
|
|
|
+//
|
|
|
+// Otherwise use Compile which combines both steps.
|
|
|
+func Parse(name, src string, options ...parser.Option) (prg *js_ast.Program, err error) {
|
|
|
+ prg, err1 := parser.ParseFile(nil, name, src, 0, options...)
|
|
|
if err1 != nil {
|
|
|
switch err1 := err1.(type) {
|
|
|
case parser.ErrorList:
|
|
@@ -1131,12 +1141,17 @@ func compile(name, src string, strict, eval bool) (p *Program, err error) {
|
|
|
Message: err1.Error(),
|
|
|
},
|
|
|
}
|
|
|
- return
|
|
|
}
|
|
|
+ return
|
|
|
+}
|
|
|
|
|
|
- p, err = compileAST(prg, strict, eval)
|
|
|
+func compile(name, src string, strict, eval bool, parserOptions ...parser.Option) (p *Program, err error) {
|
|
|
+ prg, err := Parse(name, src, parserOptions...)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- return
|
|
|
+ return compileAST(prg, strict, eval)
|
|
|
}
|
|
|
|
|
|
func compileAST(prg *js_ast.Program, strict, eval bool) (p *Program, err error) {
|
|
@@ -1162,7 +1177,7 @@ func compileAST(prg *js_ast.Program, strict, eval bool) (p *Program, err error)
|
|
|
}
|
|
|
|
|
|
func (r *Runtime) compile(name, src string, strict, eval bool) (p *Program, err error) {
|
|
|
- p, err = compile(name, src, strict, eval)
|
|
|
+ p, err = compile(name, src, strict, eval, r.parserOptions...)
|
|
|
if err != nil {
|
|
|
switch x1 := err.(type) {
|
|
|
case *CompilerSyntaxError:
|
|
@@ -1185,7 +1200,7 @@ func (r *Runtime) RunString(str string) (Value, error) {
|
|
|
|
|
|
// RunScript executes the given string in the global context.
|
|
|
func (r *Runtime) RunScript(name, src string) (Value, error) {
|
|
|
- p, err := Compile(name, src, false)
|
|
|
+ p, err := r.compile(name, src, false, false)
|
|
|
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
@@ -1984,6 +1999,11 @@ func (r *Runtime) SetTimeSource(now Now) {
|
|
|
r.now = now
|
|
|
}
|
|
|
|
|
|
+// SetParserOptions sets parser options to be used by RunString, RunScript and eval() within the code.
|
|
|
+func (r *Runtime) SetParserOptions(opts ...parser.Option) {
|
|
|
+ r.parserOptions = opts
|
|
|
+}
|
|
|
+
|
|
|
// New is an equivalent of the 'new' operator allowing to call it directly from Go.
|
|
|
func (r *Runtime) New(construct Value, args ...Value) (o *Object, err error) {
|
|
|
err = tryFunc(func() {
|