IteratorObjectEnumerator.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. IteratorObjectEnumerator * IteratorObjectEnumerator::Create(ScriptContext* scriptContext, Var iterator)
  9. {
  10. return RecyclerNew(scriptContext->GetRecycler(), IteratorObjectEnumerator, scriptContext, iterator);
  11. }
  12. IteratorObjectEnumerator::IteratorObjectEnumerator(ScriptContext* scriptContext, Var iterator) :
  13. JavascriptEnumerator(scriptContext),
  14. done(false),
  15. value(nullptr)
  16. {
  17. Assert(JavascriptOperators::IsObject(iterator));
  18. iteratorObject = RecyclableObject::FromVar(iterator);
  19. }
  20. Var IteratorObjectEnumerator::MoveAndGetNext(PropertyId& propertyId, PropertyAttributes* attributes)
  21. {
  22. ScriptContext* scriptContext = GetScriptContext();
  23. Var resultValue = nullptr;
  24. if (JavascriptOperators::IteratorStepAndValue(iteratorObject, scriptContext, &resultValue))
  25. {
  26. this->value = resultValue;
  27. if (attributes != nullptr)
  28. {
  29. *attributes = PropertyEnumerable;
  30. }
  31. Var currentIndex = value;
  32. const PropertyRecord* propertyRecord = nullptr;
  33. if (!TaggedInt::Is(currentIndex) && JavascriptString::Is(currentIndex) &&
  34. VirtualTableInfo<Js::PropertyString>::HasVirtualTable(JavascriptString::FromVar(currentIndex)))
  35. {
  36. propertyRecord = ((PropertyString *)PropertyString::FromVar(currentIndex))->GetPropertyRecord();
  37. }
  38. else if (JavascriptSymbol::Is(currentIndex))
  39. {
  40. propertyRecord = JavascriptSymbol::FromVar(currentIndex)->GetValue();
  41. }
  42. else
  43. {
  44. JavascriptString* propertyName = JavascriptConversion::ToString(currentIndex, scriptContext);
  45. scriptContext->GetOrAddPropertyRecord(propertyName->GetString(), propertyName->GetLength(), &propertyRecord);
  46. }
  47. propertyId = propertyRecord->GetPropertyId();
  48. return currentIndex;
  49. }
  50. return NULL;
  51. }
  52. void IteratorObjectEnumerator::Reset()
  53. {
  54. Assert(FALSE);
  55. }
  56. };