RootObjectBase.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "RuntimeLibraryPch.h"
  6. namespace Js
  7. {
  8. RootObjectInlineCache::RootObjectInlineCache(InlineCacheAllocator * allocator):
  9. inlineCache(nullptr), refCount(1)
  10. {
  11. inlineCache = AllocatorNewZ(InlineCacheAllocator,
  12. allocator, Js::InlineCache);
  13. }
  14. RootObjectBase::RootObjectBase(DynamicType * type) :
  15. DynamicObject(type), hostObject(nullptr), loadInlineCacheMap(nullptr), loadMethodInlineCacheMap(nullptr), storeInlineCacheMap(nullptr)
  16. {}
  17. RootObjectBase::RootObjectBase(DynamicType * type, ScriptContext* scriptContext) :
  18. DynamicObject(type, scriptContext), hostObject(nullptr), loadInlineCacheMap(nullptr), loadMethodInlineCacheMap(nullptr), storeInlineCacheMap(nullptr)
  19. {}
  20. bool RootObjectBase::Is(Var var)
  21. {
  22. return RecyclableObject::Is(var) && RootObjectBase::Is(RecyclableObject::FromVar(var));
  23. }
  24. bool RootObjectBase::Is(RecyclableObject* obj)
  25. {
  26. TypeId id = obj->GetTypeId();
  27. return id == TypeIds_GlobalObject || id == TypeIds_ModuleRoot;
  28. }
  29. RootObjectBase * RootObjectBase::FromVar(Var var)
  30. {
  31. Assert(RootObjectBase::Is(var));
  32. return static_cast<Js::RootObjectBase *>(var);
  33. }
  34. HostObjectBase * RootObjectBase::GetHostObject() const
  35. {
  36. Assert(hostObject == nullptr || Js::JavascriptOperators::GetTypeId(hostObject) == TypeIds_HostObject);
  37. return this->hostObject;
  38. }
  39. void RootObjectBase::SetHostObject(HostObjectBase * hostObject)
  40. {
  41. Assert(hostObject == nullptr || Js::JavascriptOperators::GetTypeId(hostObject) == TypeIds_HostObject);
  42. this->hostObject = hostObject;
  43. }
  44. Js::InlineCache *
  45. RootObjectBase::GetInlineCache(Js::PropertyRecord const* propertyRecord, bool isLoadMethod, bool isStore)
  46. {
  47. Js::RootObjectInlineCache * rootObjectInlineCache = GetRootInlineCache(propertyRecord, isLoadMethod, isStore);
  48. return rootObjectInlineCache->GetInlineCache();
  49. }
  50. Js::RootObjectInlineCache*
  51. RootObjectBase::GetRootInlineCache(Js::PropertyRecord const* propertyRecord, bool isLoadMethod, bool isStore)
  52. {
  53. RootObjectInlineCacheMap * inlineCacheMap = isStore ? storeInlineCacheMap :
  54. isLoadMethod ? loadMethodInlineCacheMap : loadInlineCacheMap;
  55. Js::RootObjectInlineCache * rootObjectInlineCache;
  56. if (inlineCacheMap == nullptr)
  57. {
  58. Recycler * recycler = this->GetLibrary()->GetRecycler();
  59. inlineCacheMap = RecyclerNew(recycler, RootObjectInlineCacheMap, recycler);
  60. if (isStore)
  61. {
  62. this->storeInlineCacheMap = inlineCacheMap;
  63. }
  64. else if (isLoadMethod)
  65. {
  66. this->loadMethodInlineCacheMap = inlineCacheMap;
  67. }
  68. else
  69. {
  70. this->loadInlineCacheMap = inlineCacheMap;
  71. }
  72. }
  73. else if (inlineCacheMap->TryGetValue(propertyRecord, &rootObjectInlineCache))
  74. {
  75. rootObjectInlineCache->AddRef();
  76. return rootObjectInlineCache;
  77. }
  78. Recycler * recycler = this->GetLibrary()->GetRecycler();
  79. rootObjectInlineCache = RecyclerNewLeaf(recycler, RootObjectInlineCache, this->GetScriptContext()->GetInlineCacheAllocator());
  80. inlineCacheMap->Add(propertyRecord, rootObjectInlineCache);
  81. return rootObjectInlineCache;
  82. }
  83. // TODO: Switch to take PropertyRecord instead once we clean up the function body to hold onto propertyRecord
  84. // instead of propertyId.
  85. void
  86. RootObjectBase::ReleaseInlineCache(Js::PropertyId propertyId, bool isLoadMethod, bool isStore, bool isShutdown)
  87. {
  88. uint unregisteredInlineCacheCount = 0;
  89. RootObjectInlineCacheMap * inlineCacheMap = isStore ? storeInlineCacheMap :
  90. isLoadMethod ? loadMethodInlineCacheMap : loadInlineCacheMap;
  91. bool found = false;
  92. inlineCacheMap->RemoveIfWithKey(propertyId,
  93. [this, isShutdown, &unregisteredInlineCacheCount, &found](PropertyRecord const * propertyRecord, RootObjectInlineCache * rootObjectInlineCache)
  94. {
  95. found = true;
  96. if (rootObjectInlineCache->Release() == 0)
  97. {
  98. // If we're not shutting down, we need to remove this cache from thread context's invalidation list (if any),
  99. // and release memory back to the arena. During script context shutdown, we leave everything in place, because
  100. // the inline cache arena will stay alive until script context is destroyed (as in destructor called as opposed to
  101. // Close called) and thus the invalidation lists are safe to keep references to caches from this script context.
  102. if (!isShutdown)
  103. {
  104. if (rootObjectInlineCache->GetInlineCache()->RemoveFromInvalidationList())
  105. {
  106. unregisteredInlineCacheCount++;
  107. }
  108. AllocatorDelete(InlineCacheAllocator, this->GetScriptContext()->GetInlineCacheAllocator(), rootObjectInlineCache->GetInlineCache());
  109. }
  110. return true; // Remove from the map
  111. }
  112. return false; // don't remove from the map
  113. }
  114. );
  115. Assert(found);
  116. if (unregisteredInlineCacheCount > 0)
  117. {
  118. this->GetScriptContext()->GetThreadContext()->NotifyInlineCacheBatchUnregistered(unregisteredInlineCacheCount);
  119. }
  120. }
  121. BOOL
  122. RootObjectBase::EnsureProperty(PropertyId propertyId)
  123. {
  124. if (!RootObjectBase::HasOwnPropertyCheckNoRedecl(propertyId))
  125. {
  126. this->InitProperty(propertyId, this->GetLibrary()->GetUndefined(),
  127. static_cast<Js::PropertyOperationFlags>(PropertyOperation_PreInit | PropertyOperation_SpecialValue));
  128. }
  129. return true;
  130. }
  131. BOOL
  132. RootObjectBase::EnsureNoRedeclProperty(PropertyId propertyId)
  133. {
  134. RootObjectBase::HasOwnPropertyCheckNoRedecl(propertyId);
  135. return true;
  136. }
  137. BOOL
  138. RootObjectBase::HasOwnPropertyCheckNoRedecl(PropertyId propertyId)
  139. {
  140. bool noRedecl = false;
  141. if (!GetTypeHandler()->HasRootProperty(this, propertyId, &noRedecl))
  142. {
  143. return FALSE;
  144. }
  145. else if (noRedecl)
  146. {
  147. JavascriptError::ThrowReferenceError(GetScriptContext(), ERRRedeclaration);
  148. }
  149. return true;
  150. }
  151. BOOL
  152. RootObjectBase::HasRootProperty(PropertyId propertyId)
  153. {
  154. Assert(!Js::IsInternalPropertyId(propertyId));
  155. return GetTypeHandler()->HasRootProperty(this, propertyId);
  156. }
  157. BOOL
  158. RootObjectBase::GetRootProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  159. {
  160. Assert(!Js::IsInternalPropertyId(propertyId));
  161. return GetTypeHandler()->GetRootProperty(this, originalInstance, propertyId, value, info, requestContext);
  162. }
  163. BOOL
  164. RootObjectBase::GetRootPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  165. {
  166. Assert(!Js::IsInternalPropertyId(propertyId));
  167. return GetTypeHandler()->GetRootProperty(this, originalInstance, propertyId, value, info, requestContext);
  168. }
  169. BOOL
  170. RootObjectBase::SetRootProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  171. {
  172. Assert(!Js::IsInternalPropertyId(propertyId));
  173. return GetTypeHandler()->SetRootProperty(this, propertyId, value, flags, info);
  174. }
  175. DescriptorFlags
  176. RootObjectBase::GetRootSetter(PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  177. {
  178. Assert(!Js::IsInternalPropertyId(propertyId));
  179. return GetTypeHandler()->GetRootSetter(this, propertyId, setterValue, info, requestContext);
  180. }
  181. BOOL
  182. RootObjectBase::DeleteRootProperty(PropertyId propertyId, PropertyOperationFlags flags)
  183. {
  184. Assert(!Js::IsInternalPropertyId(propertyId));
  185. return GetTypeHandler()->DeleteRootProperty(this, propertyId, flags);
  186. }
  187. PropertyIndex
  188. RootObjectBase::GetRootPropertyIndex(PropertyId propertyId)
  189. {
  190. Assert(!Js::IsInternalPropertyId(propertyId));
  191. Assert(propertyId != Constants::NoProperty);
  192. return GetTypeHandler()->GetRootPropertyIndex(this->GetScriptContext()->GetPropertyName(propertyId));
  193. }
  194. void
  195. RootObjectBase::EnsureNoProperty(PropertyId propertyId)
  196. {
  197. Assert(!Js::IsInternalPropertyId(propertyId));
  198. bool isDeclared = false;
  199. bool isNonconfigurable = false;
  200. if (GetTypeHandler()->HasRootProperty(this, propertyId, nullptr, &isDeclared, &isNonconfigurable) &&
  201. (isDeclared || isNonconfigurable))
  202. {
  203. JavascriptError::ThrowReferenceError(this->GetScriptContext(), ERRRedeclaration);
  204. }
  205. }
  206. #if DBG
  207. bool
  208. RootObjectBase::IsLetConstGlobal(PropertyId propertyId)
  209. {
  210. Assert(!Js::IsInternalPropertyId(propertyId));
  211. return GetTypeHandler()->IsLetConstGlobal(this, propertyId);
  212. }
  213. #endif
  214. }