scope.go 983 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package parser
  2. import (
  3. "github.com/dop251/goja/ast"
  4. "github.com/dop251/goja/unistring"
  5. )
  6. type _scope struct {
  7. outer *_scope
  8. allowIn bool
  9. allowLet bool
  10. inIteration bool
  11. inSwitch bool
  12. inFunction bool
  13. inAsync bool
  14. allowAwait bool
  15. declarationList []*ast.VariableDeclaration
  16. labels []unistring.String
  17. }
  18. func (self *_parser) openScope() {
  19. self.scope = &_scope{
  20. outer: self.scope,
  21. allowIn: true,
  22. }
  23. }
  24. func (self *_parser) closeScope() {
  25. self.scope = self.scope.outer
  26. }
  27. func (self *_scope) declare(declaration *ast.VariableDeclaration) {
  28. self.declarationList = append(self.declarationList, declaration)
  29. }
  30. func (self *_scope) hasLabel(name unistring.String) bool {
  31. for _, label := range self.labels {
  32. if label == name {
  33. return true
  34. }
  35. }
  36. if self.outer != nil && !self.inFunction {
  37. // Crossing a function boundary to look for a label is verboten
  38. return self.outer.hasLabel(name)
  39. }
  40. return false
  41. }