2
0

CrossSiteEnumerator.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // for enumerators, the scriptContext of the enumerator is different
  9. // from the scriptContext of the object.
  10. #if !defined(USED_IN_STATIC_LIB)
  11. #define DEFINE_MARSHAL_ENUMERATOR_TO_SCRIPT_CONTEXT(T) \
  12. friend class Js::CrossSiteEnumerator<T>; \
  13. virtual void MarshalToScriptContext(Js::ScriptContext * scriptContext) override \
  14. { \
  15. Assert(GetScriptContext() == scriptContext); \
  16. AssertMsg(VirtualTableInfo<T>::HasVirtualTable(this) || VirtualTableInfo<Js::CrossSiteEnumerator<T>>::HasVirtualTable(this), "Derived class need to define marshal to script context"); \
  17. VirtualTableInfo<Js::CrossSiteEnumerator<T>>::SetVirtualTable(this); \
  18. }
  19. #else
  20. #define DEFINE_MARSHAL_ENUMERATOR_TO_SCRIPT_CONTEXT(T) \
  21. virtual void MarshalToScriptContext(Js::ScriptContext * scriptContext) override {Assert(FALSE);}
  22. #endif
  23. template <typename T>
  24. class CrossSiteEnumerator : public T
  25. {
  26. private:
  27. DEFINE_VTABLE_CTOR(CrossSiteEnumerator<T>, T);
  28. public:
  29. virtual Var GetCurrentIndex() override;
  30. virtual Var GetCurrentValue() override;
  31. virtual void Reset() override;
  32. virtual BOOL MoveNext(PropertyAttributes* attributes = nullptr) override;
  33. virtual Var GetCurrentAndMoveNext(PropertyId& propertyId, PropertyAttributes* attributes = nullptr) override;
  34. virtual BOOL IsCrossSiteEnumerator() override
  35. {
  36. return true;
  37. }
  38. };
  39. template<typename T>
  40. Var CrossSiteEnumerator<T>::GetCurrentIndex()
  41. {
  42. Var result = __super::GetCurrentIndex();
  43. if (result)
  44. {
  45. result = CrossSite::MarshalVar(GetScriptContext(), result);
  46. }
  47. return result;
  48. }
  49. template <typename T>
  50. Var CrossSiteEnumerator<T>::GetCurrentValue()
  51. {
  52. Var result = __super::GetCurrentValue();
  53. if (result)
  54. {
  55. result = CrossSite::MarshalVar(GetScriptContext(), result);
  56. }
  57. return result;
  58. }
  59. template <typename T>
  60. BOOL CrossSiteEnumerator<T>::MoveNext(PropertyAttributes* attributes)
  61. {
  62. return __super::MoveNext(attributes);
  63. }
  64. template <typename T>
  65. void CrossSiteEnumerator<T>::Reset()
  66. {
  67. __super::Reset();
  68. }
  69. template <typename T>
  70. Var CrossSiteEnumerator<T>::GetCurrentAndMoveNext(PropertyId& propertyId, PropertyAttributes* attributes)
  71. {
  72. Var result = __super::GetCurrentAndMoveNext(propertyId, attributes);
  73. if (result)
  74. {
  75. result = CrossSite::MarshalVar(GetScriptContext(), result);
  76. }
  77. return result;
  78. }
  79. };