|
@@ -4,6 +4,11 @@ import (
|
|
|
"github.com/dop251/goja/ast"
|
|
|
"github.com/dop251/goja/file"
|
|
|
"github.com/dop251/goja/token"
|
|
|
+ "github.com/go-sourcemap/sourcemap"
|
|
|
+ "encoding/base64"
|
|
|
+ "strings"
|
|
|
+ "os"
|
|
|
+ "io/ioutil"
|
|
|
)
|
|
|
|
|
|
func (self *_parser) parseBlockStatement() *ast.BlockStatement {
|
|
@@ -544,9 +549,47 @@ func (self *_parser) parseProgram() *ast.Program {
|
|
|
Body: self.parseSourceElements(),
|
|
|
DeclarationList: self.scope.declarationList,
|
|
|
File: self.file,
|
|
|
+ SourceMap: self.parseSourceMap(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (self *_parser) parseSourceMap() *sourcemap.Consumer {
|
|
|
+ lastLine := self.str[strings.LastIndexByte(self.str, '\n') + 1:]
|
|
|
+ if strings.HasPrefix(lastLine, "//# sourceMappingURL") {
|
|
|
+ urlIndex := strings.Index(lastLine, "=")
|
|
|
+ url := lastLine[urlIndex+1:]
|
|
|
+
|
|
|
+ var data []byte
|
|
|
+ if strings.HasPrefix(url, "data:application/json;base64") {
|
|
|
+ b64Index := strings.Index(url, ",")
|
|
|
+ b64 := url[b64Index+1:]
|
|
|
+ if d, err := base64.StdEncoding.DecodeString(b64); err == nil {
|
|
|
+ data = d
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if strings.HasPrefix(strings.ToLower(url), "http") {
|
|
|
+ // Not implemented - compile error?
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if f, err := os.Open(url); err == nil {
|
|
|
+ if d, err := ioutil.ReadAll(f); err == nil {
|
|
|
+ data = d
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if data == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if sm, err := sourcemap.Parse(self.file.Name(), data); err == nil {
|
|
|
+ return sm
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func (self *_parser) parseBreakStatement() ast.Statement {
|
|
|
idx := self.expect(token.BREAK)
|
|
|
semicolon := self.implicitSemicolon
|