robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.04k stars 584 forks source link

parser.ParseFile difference between []byte and file #439

Closed ttttmr closed 1 year ago

ttttmr commented 2 years ago
body := []byte("xxx...")
var err error
ioutil.WriteFile("test.js", body, 0644)

_, err = parser.ParseFile(nil, "", body, 0)
log.Info(err) // (anonymous): Line 4:3 Unexpected reserved word (and 24 more errors)
_, err = parser.ParseFile(nil, "test.js", "", 0)
log.Info(err) // <nil>

body is correct js code, but their parsing results are different js file

ttttmr commented 2 years ago

I want to parse the body but not create the file, how to do it

stevenh commented 1 year ago

You can do this with parser.ReadSource