Scope.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeByteCodePch.h"
  6. bool Scope::IsGlobalEvalBlockScope() const
  7. {
  8. return this->scopeType == ScopeType_GlobalEvalBlock;
  9. }
  10. bool Scope::IsBlockScope(FuncInfo *funcInfo)
  11. {
  12. return this != funcInfo->GetBodyScope() && this != funcInfo->GetParamScope();
  13. }
  14. int Scope::AddScopeSlot()
  15. {
  16. int slot = scopeSlotCount++;
  17. if (scopeSlotCount == Js::ScopeSlots::MaxEncodedSlotCount)
  18. {
  19. this->GetEnclosingFunc()->SetHasMaybeEscapedNestedFunc(DebugOnly(_u("TooManySlots")));
  20. }
  21. return slot;
  22. }
  23. void Scope::ForceAllSymbolNonLocalReference(ByteCodeGenerator *byteCodeGenerator)
  24. {
  25. this->ForEachSymbol([this, byteCodeGenerator](Symbol *const sym)
  26. {
  27. if (!sym->IsArguments() && !sym->IsSpecialSymbol())
  28. {
  29. sym->SetHasNonLocalReference();
  30. byteCodeGenerator->ProcessCapturedSym(sym);
  31. this->GetFunc()->SetHasLocalInClosure(true);
  32. }
  33. });
  34. }
  35. bool Scope::IsEmpty() const
  36. {
  37. return Count() == 0;
  38. }
  39. void Scope::SetIsObject()
  40. {
  41. if (this->isObject)
  42. {
  43. return;
  44. }
  45. this->isObject = true;
  46. // We might set the scope to be object after we have process the symbol
  47. // (e.g. "With" scope referencing a symbol in an outer scope).
  48. // If we have func assignment, we need to mark the function to not do stack nested function
  49. // as these are now assigned to a scope object.
  50. FuncInfo * funcInfo = this->GetFunc();
  51. if (funcInfo && !funcInfo->HasMaybeEscapedNestedFunc())
  52. {
  53. this->ForEachSymbolUntil([funcInfo](Symbol * const sym)
  54. {
  55. if (sym->GetHasFuncAssignment())
  56. {
  57. funcInfo->SetHasMaybeEscapedNestedFunc(DebugOnly(_u("DelayedObjectScopeAssignment")));
  58. return true;
  59. }
  60. return false;
  61. });
  62. }
  63. // If the scope is split (there exists a body and param scope), then it is required that the
  64. // body and param scope are marked as both requiring either a scope object or a scope slot.
  65. if ((this->GetScopeType() == ScopeType_FunctionBody || this->GetScopeType() == ScopeType_Parameter)
  66. && funcInfo && !funcInfo->IsBodyAndParamScopeMerged())
  67. {
  68. // The scope is split and one of the scopes (param or body) is being set
  69. // as an object, therefore set both the param and body scopes as objects.
  70. Assert(funcInfo->paramScope);
  71. funcInfo->paramScope->SetIsObject();
  72. Assert(funcInfo->bodyScope);
  73. funcInfo->bodyScope->SetIsObject();
  74. }
  75. }
  76. void Scope::MergeParamAndBodyScopes(ParseNodeFnc *pnodeScope)
  77. {
  78. Assert(pnodeScope->funcInfo);
  79. Scope *paramScope = pnodeScope->pnodeScopes->scope;
  80. Scope *bodyScope = pnodeScope->pnodeBodyScope->scope;
  81. if (paramScope->Count() == 0)
  82. {
  83. return;
  84. }
  85. bodyScope->scopeSlotCount = paramScope->scopeSlotCount;
  86. paramScope->ForEachSymbol([&](Symbol * sym)
  87. {
  88. bodyScope->AddNewSymbol(sym);
  89. });
  90. if (paramScope->GetIsObject())
  91. {
  92. bodyScope->SetIsObject();
  93. }
  94. if (paramScope->GetMustInstantiate())
  95. {
  96. bodyScope->SetMustInstantiate(true);
  97. }
  98. if (paramScope->GetHasOwnLocalInClosure())
  99. {
  100. bodyScope->SetHasOwnLocalInClosure(true);
  101. }
  102. }
  103. void Scope::RemoveParamScope(ParseNodeFnc *pnodeScope)
  104. {
  105. Assert(pnodeScope->funcInfo);
  106. Scope *paramScope = pnodeScope->pnodeScopes->scope;
  107. Scope *bodyScope = pnodeScope->pnodeBodyScope->scope;
  108. // Once the scopes are merged, there's no reason to instantiate the param scope.
  109. paramScope->SetMustInstantiate(false);
  110. paramScope->m_count = 0;
  111. paramScope->scopeSlotCount = 0;
  112. paramScope->m_symList = nullptr;
  113. // Remove the parameter scope from the scope chain.
  114. bodyScope->SetEnclosingScope(paramScope->GetEnclosingScope());
  115. }