Skip to content

Commit

Permalink
JS: mark for initializers to distinguish between declaration in body
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Sep 4, 2020
1 parent 09d9f68 commit 989baab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions js/ast.go
Expand Up @@ -113,6 +113,7 @@ type Scope struct {
Declared VarArray // Link in Var are always nil
Undeclared VarArray
NumVarDecls uint16 // number of variable declaration statements in a function scope
NumForInit uint16 // offset into Declared to mark variables used in for initializer
NumArguments uint16 // offset into Undeclared to mark variables used in arguments
IsGlobalOrFunc bool
HasWith bool
Expand Down Expand Up @@ -213,6 +214,11 @@ func (s *Scope) findUndeclared(name []byte) *Var {
return nil
}

func (s *Scope) MarkForInit() {
// set the offset for variables declared in for initializer to distinguish from declarations in body
s.NumForInit = uint16(len(s.Declared))
}

func (s *Scope) MarkArguments() {
// set the offset for variables used for arguments, to ensure different b's for: function f(a=b){var b}
s.NumArguments = uint16(len(s.Undeclared))
Expand Down
3 changes: 3 additions & 0 deletions js/parse.go
Expand Up @@ -333,6 +333,7 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
if !p.consume("for statement", CloseParenToken) {
return
}
p.scope.MarkForInit()
if p.tt == OpenBraceToken {
body.List = p.parseStmtList("")
} else if p.tt != SemicolonToken {
Expand All @@ -352,6 +353,7 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
if !p.consume("for statement", CloseParenToken) {
return
}
p.scope.MarkForInit()
if p.tt == OpenBraceToken {
body.List = p.parseStmtList("")
} else if p.tt != SemicolonToken {
Expand All @@ -368,6 +370,7 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
if !p.consume("for statement", CloseParenToken) {
return
}
p.scope.MarkForInit()
if p.tt == OpenBraceToken {
body.List = p.parseStmtList("")
} else if p.tt != SemicolonToken {
Expand Down

0 comments on commit 989baab

Please sign in to comment.