Scope.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 (this->GetScopeType() == ScopeType_FunctionBody && funcInfo && !funcInfo->IsBodyAndParamScopeMerged()
  64. && funcInfo->paramScope && !funcInfo->paramScope->GetIsObject())
  65. {
  66. // If this is split scope then mark the param scope also as an object
  67. funcInfo->paramScope->SetIsObject();
  68. }
  69. }
  70. void Scope::MergeParamAndBodyScopes(ParseNode *pnodeScope)
  71. {
  72. Assert(pnodeScope->sxFnc.funcInfo);
  73. Scope *paramScope = pnodeScope->sxFnc.pnodeScopes->sxBlock.scope;
  74. Scope *bodyScope = pnodeScope->sxFnc.pnodeBodyScope->sxBlock.scope;
  75. if (paramScope->Count() == 0)
  76. {
  77. return;
  78. }
  79. bodyScope->scopeSlotCount = paramScope->scopeSlotCount;
  80. paramScope->ForEachSymbol([&](Symbol * sym)
  81. {
  82. bodyScope->AddNewSymbol(sym);
  83. });
  84. if (paramScope->GetIsObject())
  85. {
  86. bodyScope->SetIsObject();
  87. }
  88. if (paramScope->GetMustInstantiate())
  89. {
  90. bodyScope->SetMustInstantiate(true);
  91. }
  92. if (paramScope->GetHasOwnLocalInClosure())
  93. {
  94. bodyScope->SetHasOwnLocalInClosure(true);
  95. }
  96. }
  97. void Scope::RemoveParamScope(ParseNode *pnodeScope)
  98. {
  99. Assert(pnodeScope->sxFnc.funcInfo);
  100. Scope *paramScope = pnodeScope->sxFnc.pnodeScopes->sxBlock.scope;
  101. Scope *bodyScope = pnodeScope->sxFnc.pnodeBodyScope->sxBlock.scope;
  102. // Once the scopes are merged, there's no reason to instantiate the param scope.
  103. paramScope->SetMustInstantiate(false);
  104. paramScope->m_count = 0;
  105. paramScope->scopeSlotCount = 0;
  106. paramScope->m_symList = nullptr;
  107. // Remove the parameter scope from the scope chain.
  108. bodyScope->SetEnclosingScope(paramScope->GetEnclosingScope());
  109. }