Skip to content

Commit

Permalink
Use source name provided by source map. See #235.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Dec 21, 2020
1 parent 10e5c75 commit 6b6d5e2
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 173 deletions.
3 changes: 0 additions & 3 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/dop251/goja/file"
"github.com/dop251/goja/token"
"github.com/dop251/goja/unistring"
"github.com/go-sourcemap/sourcemap"
)

// All nodes implement the Node interface.
Expand Down Expand Up @@ -399,8 +398,6 @@ type Program struct {
DeclarationList []Declaration

File *file.File

SourceMap *sourcemap.Consumer
}

// ==== //
Expand Down
6 changes: 3 additions & 3 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (

type CompilerError struct {
Message string
File *SrcFile
File *file.File
Offset int
}

Expand All @@ -43,7 +43,7 @@ type Program struct {
values []Value

funcName unistring.String
src *SrcFile
src *file.File
srcMap []srcMapItem
}

Expand Down Expand Up @@ -250,7 +250,7 @@ func (c *compiler) markBlockStart() {
}

func (c *compiler) compile(in *ast.Program) {
c.p.src = NewSrcFile(in.File.Name(), in.File.Source(), in.SourceMap)
c.p.src = in.File

if len(in.Body) > 0 {
if !c.scope.strict {
Expand Down
118 changes: 99 additions & 19 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ package file

import (
"fmt"
"strings"
"path"
"sort"
"sync"

"github.com/go-sourcemap/sourcemap"
)

// Idx is a compact encoding of a source position within a file set.
Expand All @@ -16,7 +20,6 @@ type Idx int
// including the filename, line, and column location.
type Position struct {
Filename string // The filename where the error occurred, if any
Offset int // The src offset
Line int // The line number, starting at 1
Column int // The column number, starting at 1 (The character count)

Expand All @@ -35,7 +38,7 @@ func (self *Position) isValid() bool {
// file An invalid position with filename
// - An invalid position without filename
//
func (self *Position) String() string {
func (self Position) String() string {
str := self.Filename
if self.isValid() {
if str != "" {
Expand Down Expand Up @@ -89,29 +92,23 @@ func (self *FileSet) File(idx Idx) *File {
}

// Position converts an Idx in the FileSet into a Position.
func (self *FileSet) Position(idx Idx) *Position {
position := &Position{}
func (self *FileSet) Position(idx Idx) Position {
for _, file := range self.files {
if idx <= Idx(file.base+len(file.src)) {
offset := int(idx) - file.base
src := file.src[:offset]
position.Filename = file.name
position.Offset = offset
position.Line = 1 + strings.Count(src, "\n")
if index := strings.LastIndex(src, "\n"); index >= 0 {
position.Column = offset - index
} else {
position.Column = 1 + len(src)
}
return file.Position(int(idx) - file.base)
}
}
return position
return Position{}
}

type File struct {
name string
src string
base int // This will always be 1 or greater
mu sync.Mutex
name string
src string
base int // This will always be 1 or greater
sourceMap *sourcemap.Consumer
lineOffsets []int
lastScannedOffset int
}

func NewFile(filename, src string, base int) *File {
Expand All @@ -133,3 +130,86 @@ func (fl *File) Source() string {
func (fl *File) Base() int {
return fl.base
}

func (fl *File) SetSourceMap(m *sourcemap.Consumer) {
fl.sourceMap = m
}

func (fl *File) Position(offset int) Position {
var line int
var lineOffsets []int
fl.mu.Lock()
if offset > fl.lastScannedOffset {
line = fl.scanTo(offset)
lineOffsets = fl.lineOffsets
fl.mu.Unlock()
} else {
lineOffsets = fl.lineOffsets
fl.mu.Unlock()
line = sort.Search(len(lineOffsets), func(x int) bool { return lineOffsets[x] > offset }) - 1
}

var lineStart int
if line >= 0 {
lineStart = lineOffsets[line]
}

row := line + 2
col := offset - lineStart + 1

if fl.sourceMap != nil {
if source, _, row, col, ok := fl.sourceMap.Source(row, col); ok {
if !path.IsAbs(source) {
source = path.Join(path.Dir(fl.name), source)
}
return Position{
Filename: source,
Line: row,
Column: col,
}
}
}

return Position{
Filename: fl.name,
Line: row,
Column: col,
}
}

func findNextLineStart(s string) int {
for pos, ch := range s {
switch ch {
case '\r':
if pos < len(s)-1 && s[pos+1] == '\n' {
return pos + 2
}
return pos + 1
case '\n':
return pos + 1
case '\u2028', '\u2029':
return pos + 3
}
}
return -1
}

func (fl *File) scanTo(offset int) int {
o := fl.lastScannedOffset
for o < offset {
p := findNextLineStart(fl.src[o:])
if p == -1 {
fl.lastScannedOffset = len(fl.src)
return len(fl.lineOffsets) - 1
}
o = o + p
fl.lineOffsets = append(fl.lineOffsets, o)
}
fl.lastScannedOffset = o

if o == offset {
return len(fl.lineOffsets) - 1
}

return len(fl.lineOffsets) - 2
}
12 changes: 6 additions & 6 deletions srcfile_test.go → file/file_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package goja
package file

import "testing"

func TestPosition(t *testing.T) {
const SRC = `line1
line2
line3`
f := NewSrcFile("", SRC, nil)
f := NewFile("", SRC, 0)

tests := []struct {
offset int
Expand All @@ -27,17 +27,17 @@ line3`
}

for i, test := range tests {
if p := f.Position(test.offset); p.Line != test.line || p.Col != test.col {
t.Fatalf("%d. Line: %d, col: %d", i, p.Line, p.Col)
if p := f.Position(test.offset); p.Line != test.line || p.Column != test.col {
t.Fatalf("%d. Line: %d, col: %d", i, p.Line, p.Column)
}
}
}

func TestSrcFileConcurrency(t *testing.T) {
func TestFileConcurrency(t *testing.T) {
const SRC = `line1
line2
line3`
f := NewSrcFile("", SRC, nil)
f := NewFile("", SRC, 0)
go func() {
f.Position(12)
}()
Expand Down
38 changes: 1 addition & 37 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,42 +264,6 @@ func (self *_parser) expect(value token.Token) file.Idx {
return idx
}

func lineCount(str string) (int, int) {
line, last := 0, -1
pair := false
for index, chr := range str {
switch chr {
case '\r':
line += 1
last = index
pair = true
continue
case '\n':
if !pair {
line += 1
}
last = index
case '\u2028', '\u2029':
line += 1
last = index + 2
}
pair = false
}
return line, last
}

func (self *_parser) position(idx file.Idx) file.Position {
position := file.Position{}
offset := int(idx) - self.base
str := self.str[:offset]
position.Filename = self.file.Name()
line, last := lineCount(str)
position.Line = 1 + line
if last >= 0 {
position.Column = offset - last
} else {
position.Column = 1 + len(str)
}

return position
return self.file.Position(int(idx) - self.base)
}
7 changes: 4 additions & 3 deletions parser/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,13 @@ func (self *_parser) parseSourceElements() []ast.Statement {
func (self *_parser) parseProgram() *ast.Program {
self.openScope()
defer self.closeScope()
return &ast.Program{
prg := &ast.Program{
Body: self.parseSourceElements(),
DeclarationList: self.scope.declarationList,
File: self.file,
SourceMap: self.parseSourceMap(),
}
self.file.SetSourceMap(self.parseSourceMap())
return prg
}

func extractSourceMapLine(str string) string {
Expand Down Expand Up @@ -624,7 +625,7 @@ func (self *_parser) parseSourceMap() *sourcemap.Consumer {
var smUrl *url.URL
if smUrl, err = url.Parse(urlStr); err == nil {
p := smUrl.Path
if !strings.HasPrefix(p, "/") {
if !path.IsAbs(p) {
baseName := self.file.Name()
baseUrl, err1 := url.Parse(baseName)
if err1 == nil && baseUrl.Scheme != "" {
Expand Down
19 changes: 10 additions & 9 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/dop251/goja/file"
"go/ast"
"hash/maphash"
"math"
Expand Down Expand Up @@ -193,7 +194,7 @@ func (f *StackFrame) SrcName() string {
if f.prg == nil {
return "<native>"
}
return f.prg.src.name
return f.prg.src.Name()
}

func (f *StackFrame) FuncName() string {
Expand All @@ -206,12 +207,9 @@ func (f *StackFrame) FuncName() string {
return f.funcName.String()
}

func (f *StackFrame) Position() Position {
func (f *StackFrame) Position() file.Position {
if f.prg == nil || f.prg.src == nil {
return Position{
0,
0,
}
return file.Position{}
}
return f.prg.src.Position(f.prg.sourceOffset(f.pc))
}
Expand All @@ -222,13 +220,16 @@ func (f *StackFrame) Write(b *bytes.Buffer) {
b.WriteString(n.String())
b.WriteString(" (")
}
if n := f.prg.src.name; n != "" {
b.WriteString(n)
p := f.Position()
if p.Filename != "" {
b.WriteString(p.Filename)
} else {
b.WriteString("<eval>")
}
b.WriteByte(':')
b.WriteString(f.Position().String())
b.WriteString(strconv.Itoa(p.Line))
b.WriteByte(':')
b.WriteString(strconv.Itoa(p.Column))
b.WriteByte('(')
b.WriteString(strconv.Itoa(f.pc))
b.WriteByte(')')
Expand Down
Loading

0 comments on commit 6b6d5e2

Please sign in to comment.