scope.go 899 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. inIteration bool
  10. inSwitch bool
  11. inFunction bool
  12. declarationList []ast.Declaration
  13. labels []unistring.String
  14. }
  15. func (self *_parser) openScope() {
  16. self.scope = &_scope{
  17. outer: self.scope,
  18. allowIn: true,
  19. }
  20. }
  21. func (self *_parser) closeScope() {
  22. self.scope = self.scope.outer
  23. }
  24. func (self *_scope) declare(declaration ast.Declaration) {
  25. self.declarationList = append(self.declarationList, declaration)
  26. }
  27. func (self *_scope) hasLabel(name unistring.String) bool {
  28. for _, label := range self.labels {
  29. if label == name {
  30. return true
  31. }
  32. }
  33. if self.outer != nil && !self.inFunction {
  34. // Crossing a function boundary to look for a label is verboten
  35. return self.outer.hasLabel(name)
  36. }
  37. return false
  38. }