scope.go 939 B

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