JavascriptSetIterator.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. JavascriptSetIterator::JavascriptSetIterator(DynamicType* type, JavascriptSet* set, JavascriptSetIteratorKind kind):
  9. DynamicObject(type),
  10. m_set(set),
  11. m_setIterator(set->GetIterator()),
  12. m_kind(kind)
  13. {
  14. Assert(type->GetTypeId() == TypeIds_SetIterator);
  15. }
  16. Var JavascriptSetIterator::EntryNext(RecyclableObject* function, CallInfo callInfo, ...)
  17. {
  18. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  19. ARGUMENTS(args, callInfo);
  20. ScriptContext* scriptContext = function->GetScriptContext();
  21. JavascriptLibrary* library = scriptContext->GetLibrary();
  22. Assert(!(callInfo.Flags & CallFlags_New));
  23. Var thisObj = args[0];
  24. if (!VarIs<JavascriptSetIterator>(thisObj))
  25. {
  26. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedSetIterator, _u("Set Iterator.prototype.next"));
  27. }
  28. JavascriptSetIterator* iterator = VarTo<JavascriptSetIterator>(thisObj);
  29. JavascriptSet* set = iterator->m_set;
  30. auto& setIterator = iterator->m_setIterator;
  31. if (set == nullptr || !setIterator.Next())
  32. {
  33. iterator->m_set = nullptr;
  34. return library->CreateIteratorResultObjectDone();
  35. }
  36. auto value = setIterator.Current();
  37. Var result;
  38. if (iterator->m_kind == JavascriptSetIteratorKind::KeyAndValue)
  39. {
  40. JavascriptArray* keyValueTuple = library->CreateArray(2);
  41. keyValueTuple->SetItem(0, value, PropertyOperation_None);
  42. keyValueTuple->SetItem(1, value, PropertyOperation_None);
  43. result = keyValueTuple;
  44. }
  45. else
  46. {
  47. Assert(iterator->m_kind == JavascriptSetIteratorKind::Value);
  48. result = value;
  49. }
  50. return library->CreateIteratorResultObject(result);
  51. }
  52. } //namespace Js