Skip to content

Commit

Permalink
Used sort.Search() in sourceOffset() (fixes dop251#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed May 22, 2017
1 parent d382686 commit 9e958ed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 8 additions & 9 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/dop251/goja/ast"
"github.com/dop251/goja/file"
"sort"
"strconv"
)

Expand Down Expand Up @@ -158,16 +159,14 @@ func (p *Program) _dumpCode(indent string, logger func(format string, args ...in
}

func (p *Program) sourceOffset(pc int) int {
// TODO use sort.Search()
for i, item := range p.srcMap {
if item.pc > pc {
if i > 0 {
return p.srcMap[i-1].srcPos
}
return 0
}
i := sort.Search(len(p.srcMap), func(idx int) bool {
return p.srcMap[idx].pc > pc
}) - 1
if i >= 0 {
return p.srcMap[i].srcPos
}
return p.srcMap[len(p.srcMap)-1].srcPos

return 0
}

func (s *scope) isFunction() bool {
Expand Down
10 changes: 10 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,16 @@ func TestObjectLiteral__Proto__(t *testing.T) {
testScript1(SCRIPT, _null, t)
}

func TestEmptyCodeError(t *testing.T) {
if _, err := New().RunString(`i`); err == nil {
t.Fatal("Expected an error")
} else {
if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
t.Fatalf("Unexpected error: '%s'", e)
}
}
}

// FIXME
/*
func TestDummyCompile(t *testing.T) {
Expand Down

0 comments on commit 9e958ed

Please sign in to comment.