RootObjectBase.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #pragma once
  6. namespace Js
  7. {
  8. class RootObjectInlineCache
  9. {
  10. public:
  11. RootObjectInlineCache(InlineCacheAllocator * allocator);
  12. uint AddRef() { return ++refCount; }
  13. uint Release() { Assert(refCount != 0); return --refCount; }
  14. Js::InlineCache * GetInlineCache() const { return inlineCache; }
  15. uint GetRefCount() { return refCount; }
  16. private:
  17. Field(uint) refCount;
  18. Field(Js::InlineCache *) inlineCache;
  19. };
  20. class RootObjectBase: public DynamicObject
  21. {
  22. public:
  23. HostObjectBase * GetHostObject() const;
  24. void SetHostObject(HostObjectBase * hostObject);
  25. Js::InlineCache * GetInlineCache(Js::PropertyRecord const* propertyRecord, bool isLoadMethod, bool isStore);
  26. Js::RootObjectInlineCache * GetRootInlineCache(Js::PropertyRecord const* propertyRecord, bool isLoadMethod, bool isStore);
  27. uint ReleaseInlineCache(PropertyId propertyId, bool isLoadMethod, bool isStore, bool isShutdown);
  28. virtual BOOL EnsureProperty(PropertyId propertyId) override;
  29. virtual BOOL EnsureNoRedeclProperty(PropertyId propertyId) override sealed;
  30. virtual BOOL HasOwnPropertyCheckNoRedecl(PropertyId propertyId) override sealed;
  31. void EnsureCanDeclGloFunc(PropertyId propertyId);
  32. // These are special "Root" versions of the property APIs that allow access
  33. // to global let and const variables, which are stored as properties on the
  34. // root object, but are hidden from normal property API access.
  35. virtual BOOL HasRootProperty(PropertyId propertyId);
  36. virtual BOOL GetRootProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext);
  37. virtual BOOL GetRootPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext);
  38. virtual BOOL SetRootProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info);
  39. virtual DescriptorFlags GetRootSetter(PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext);
  40. virtual BOOL DeleteRootProperty(PropertyId propertyId, PropertyOperationFlags flags);
  41. PropertyIndex GetRootPropertyIndex(PropertyId propertyId);
  42. void EnsureNoProperty(PropertyId propertyId);
  43. template <typename Fn>
  44. void MapLetConstGlobals(Fn fn);
  45. #if DBG
  46. bool IsLetConstGlobal(PropertyId propertyId);
  47. #endif
  48. protected:
  49. DEFINE_VTABLE_CTOR(RootObjectBase, DynamicObject);
  50. // We shouldn't create an instance of this object, only derive from it, hide the constructor
  51. RootObjectBase(DynamicType * type);
  52. RootObjectBase(DynamicType * type, ScriptContext* scriptContext);
  53. Field(HostObjectBase *) hostObject;
  54. typedef JsUtil::BaseDictionary<PropertyRecord const *, RootObjectInlineCache *, Recycler> RootObjectInlineCacheMap;
  55. Field(RootObjectInlineCacheMap *) loadInlineCacheMap;
  56. Field(RootObjectInlineCacheMap *) loadMethodInlineCacheMap;
  57. Field(RootObjectInlineCacheMap *) storeInlineCacheMap;
  58. };
  59. template <> inline bool VarIsImpl<RootObjectBase>(RecyclableObject* obj)
  60. {
  61. TypeId id = obj->GetTypeId();
  62. return id == TypeIds_GlobalObject || id == TypeIds_ModuleRoot;
  63. }
  64. template <typename Fn>
  65. void
  66. RootObjectBase::MapLetConstGlobals(Fn fn)
  67. {
  68. int index = 0;
  69. const PropertyRecord* propertyRecord;
  70. Var value;
  71. bool isConst;
  72. while (GetTypeHandler()->NextLetConstGlobal(index, this, &propertyRecord, &value, &isConst))
  73. {
  74. fn(propertyRecord, value, isConst);
  75. }
  76. }
  77. };