JavascriptSet.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. JavascriptSet::JavascriptSet(DynamicType* type)
  9. : DynamicObject(type)
  10. {
  11. }
  12. JavascriptSet* JavascriptSet::New(ScriptContext* scriptContext)
  13. {
  14. JavascriptSet* set = scriptContext->GetLibrary()->CreateSet();
  15. return set;
  16. }
  17. JavascriptSet::SetDataList::Iterator JavascriptSet::GetIterator()
  18. {
  19. return this->list.GetIterator();
  20. }
  21. Var JavascriptSet::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  22. {
  23. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  24. ARGUMENTS(args, callInfo);
  25. ScriptContext* scriptContext = function->GetScriptContext();
  26. JavascriptLibrary* library = scriptContext->GetLibrary();
  27. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, _u("Set"));
  28. Var newTarget = args.GetNewTarget();
  29. bool isCtorSuperCall = JavascriptOperators::GetAndAssertIsConstructorSuperCall(args);
  30. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Set, scriptContext);
  31. JavascriptSet* setObject = nullptr;
  32. if (callInfo.Flags & CallFlags_New)
  33. {
  34. setObject = library->CreateSet();
  35. }
  36. else
  37. {
  38. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set"), _u("Set"));
  39. }
  40. Assert(setObject != nullptr);
  41. Var iterable = (args.Info.Count > 1) ? args[1] : library->GetUndefined();
  42. // REVIEW: This condition seems impossible?
  43. if (setObject->kind != SetKind::EmptySet)
  44. {
  45. Assert(UNREACHED);
  46. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_ObjectIsAlreadyInitialized, _u("Set"), _u("Set"));
  47. }
  48. RecyclableObject* iter = nullptr;
  49. RecyclableObject* adder = nullptr;
  50. if (JavascriptConversion::CheckObjectCoercible(iterable, scriptContext))
  51. {
  52. iter = JavascriptOperators::GetIterator(iterable, scriptContext);
  53. Var adderVar = JavascriptOperators::GetPropertyNoCache(setObject, PropertyIds::add, scriptContext);
  54. if (!JavascriptConversion::IsCallable(adderVar))
  55. {
  56. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  57. }
  58. adder = VarTo<RecyclableObject>(adderVar);
  59. }
  60. if (iter != nullptr)
  61. {
  62. JavascriptOperators::DoIteratorStepAndValue(iter, scriptContext, [&](Var nextItem) {
  63. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  64. {
  65. CALL_FUNCTION(scriptContext->GetThreadContext(), adder, CallInfo(CallFlags_Value, 2), setObject, nextItem);
  66. }
  67. END_SAFE_REENTRANT_CALL
  68. });
  69. }
  70. return isCtorSuperCall ?
  71. JavascriptOperators::OrdinaryCreateFromConstructor(VarTo<RecyclableObject>(newTarget), setObject, nullptr, scriptContext) :
  72. setObject;
  73. }
  74. Var JavascriptSet::EntryAdd(RecyclableObject* function, CallInfo callInfo, ...)
  75. {
  76. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  77. ARGUMENTS(args, callInfo);
  78. ScriptContext* scriptContext = function->GetScriptContext();
  79. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  80. if (set == nullptr)
  81. {
  82. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.add"), _u("Set"));
  83. }
  84. Var value = (args.Info.Count > 1) ? args[1] : scriptContext->GetLibrary()->GetUndefined();
  85. if (JavascriptNumber::Is(value) && JavascriptNumber::IsNegZero(JavascriptNumber::GetValue(value)))
  86. {
  87. // Normalize -0 to +0
  88. value = JavascriptNumber::New(0.0, scriptContext);
  89. }
  90. set->Add(value);
  91. return set;
  92. }
  93. Var JavascriptSet::EntryClear(RecyclableObject* function, CallInfo callInfo, ...)
  94. {
  95. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  96. ARGUMENTS(args, callInfo);
  97. ScriptContext* scriptContext = function->GetScriptContext();
  98. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  99. if (set == nullptr)
  100. {
  101. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.clear"), _u("Set"));
  102. }
  103. set->Clear();
  104. return scriptContext->GetLibrary()->GetUndefined();
  105. }
  106. Var JavascriptSet::EntryDelete(RecyclableObject* function, CallInfo callInfo, ...)
  107. {
  108. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  109. ARGUMENTS(args, callInfo);
  110. ScriptContext* scriptContext = function->GetScriptContext();
  111. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  112. if (set == nullptr)
  113. {
  114. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.delete"), _u("Set"));
  115. }
  116. Var value = (args.Info.Count > 1) ? args[1] : scriptContext->GetLibrary()->GetUndefined();
  117. bool didDelete = set->Delete(value);
  118. return scriptContext->GetLibrary()->CreateBoolean(didDelete);
  119. }
  120. Var JavascriptSet::EntryForEach(RecyclableObject* function, CallInfo callInfo, ...)
  121. {
  122. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  123. ARGUMENTS(args, callInfo);
  124. ScriptContext* scriptContext = function->GetScriptContext();
  125. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, _u("Set.prototype.forEach"));
  126. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  127. if (set == nullptr)
  128. {
  129. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.forEach"), _u("Set"));
  130. }
  131. if (args.Info.Count < 2 || !JavascriptConversion::IsCallable(args[1]))
  132. {
  133. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Set.prototype.forEach"));
  134. }
  135. RecyclableObject* callBackFn = VarTo<RecyclableObject>(args[1]);
  136. Var thisArg = (args.Info.Count > 2) ? args[2] : scriptContext->GetLibrary()->GetUndefined();
  137. auto iterator = set->GetIterator();
  138. while (iterator.Next())
  139. {
  140. Var value = iterator.Current();
  141. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  142. {
  143. CALL_FUNCTION(scriptContext->GetThreadContext(), callBackFn, CallInfo(CallFlags_Value, 4), thisArg, value, value, args[0]);
  144. }
  145. END_SAFE_REENTRANT_CALL
  146. }
  147. return scriptContext->GetLibrary()->GetUndefined();
  148. }
  149. Var JavascriptSet::EntryHas(RecyclableObject* function, CallInfo callInfo, ...)
  150. {
  151. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  152. ARGUMENTS(args, callInfo);
  153. ScriptContext* scriptContext = function->GetScriptContext();
  154. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  155. if (set == nullptr)
  156. {
  157. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.has"), _u("Set"));
  158. }
  159. Var value = (args.Info.Count > 1) ? args[1] : scriptContext->GetLibrary()->GetUndefined();
  160. bool hasValue = set->Has(value);
  161. return scriptContext->GetLibrary()->CreateBoolean(hasValue);
  162. }
  163. Var JavascriptSet::EntrySizeGetter(RecyclableObject* function, CallInfo callInfo, ...)
  164. {
  165. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  166. ARGUMENTS(args, callInfo);
  167. ScriptContext* scriptContext = function->GetScriptContext();
  168. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  169. if (set == nullptr)
  170. {
  171. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.size"), _u("Set"));
  172. }
  173. int size = set->Size();
  174. return JavascriptNumber::ToVar(size, scriptContext);
  175. }
  176. Var JavascriptSet::EntryEntries(RecyclableObject* function, CallInfo callInfo, ...)
  177. {
  178. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  179. ARGUMENTS(args, callInfo);
  180. ScriptContext* scriptContext = function->GetScriptContext();
  181. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  182. if (set == nullptr)
  183. {
  184. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.entries"), _u("Set"));
  185. }
  186. return scriptContext->GetLibrary()->CreateSetIterator(set, JavascriptSetIteratorKind::KeyAndValue);
  187. }
  188. Var JavascriptSet::EntryValues(RecyclableObject* function, CallInfo callInfo, ...)
  189. {
  190. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  191. ARGUMENTS(args, callInfo);
  192. ScriptContext* scriptContext = function->GetScriptContext();
  193. JavascriptSet* set = JavascriptOperators::TryFromVar<JavascriptSet>(args[0]);
  194. if (set == nullptr)
  195. {
  196. JavascriptError::ThrowTypeErrorVar(scriptContext, JSERR_NeedObjectOfType, _u("Set.prototype.values"), _u("Set"));
  197. }
  198. return scriptContext->GetLibrary()->CreateSetIterator(set, JavascriptSetIteratorKind::Value);
  199. }
  200. Var JavascriptSet::EntryGetterSymbolSpecies(RecyclableObject* function, CallInfo callInfo, ...)
  201. {
  202. ARGUMENTS(args, callInfo);
  203. Assert(args.Info.Count > 0);
  204. return args[0];
  205. }
  206. template <typename T>
  207. T*
  208. JavascriptSet::CreateVarSetFromList(uint initialCapacity)
  209. {
  210. T* varSet = RecyclerNew(this->GetRecycler(), T, this->GetRecycler(), initialCapacity);
  211. JavascriptSet::SetDataList::Iterator iter = this->list.GetIterator();
  212. // TODO: we can use a more efficient Iterator, since we know there will be no side effects
  213. while (iter.Next())
  214. {
  215. varSet->Add(iter.Current(), iter.CurrentNode());
  216. }
  217. return varSet;
  218. }
  219. void
  220. JavascriptSet::PromoteToSimpleVarSet()
  221. {
  222. AssertOrFailFast(this->kind == SetKind::IntSet);
  223. SimpleVarDataSet* newSet = this->CreateVarSetFromList<SimpleVarDataSet>(this->u.intSet->Count() + 1);
  224. this->kind = SetKind::SimpleVarSet;
  225. this->u.simpleVarSet = newSet;
  226. }
  227. void
  228. JavascriptSet::PromoteToComplexVarSet()
  229. {
  230. uint setSize = 0;
  231. if (this->kind == SetKind::IntSet)
  232. {
  233. setSize = this->u.intSet->Count();
  234. }
  235. else
  236. {
  237. AssertOrFailFast(this->kind == SetKind::SimpleVarSet);
  238. setSize = this->u.simpleVarSet->Count();
  239. }
  240. ComplexVarDataSet* newSet = this->CreateVarSetFromList<ComplexVarDataSet>(setSize + 1);
  241. this->kind = SetKind::ComplexVarSet;
  242. this->u.complexVarSet = newSet;
  243. }
  244. void
  245. JavascriptSet::AddToEmptySet(Var value)
  246. {
  247. Assert(this->kind == SetKind::EmptySet);
  248. // We cannot store an int with value -1 inside of a bit vector
  249. Var taggedInt = JavascriptConversion::TryCanonicalizeAsTaggedInt<false /* allowNegOne */, false /* allowLossyConversion */>(value);
  250. if (taggedInt)
  251. {
  252. int32 intVal = TaggedInt::ToInt32(taggedInt);
  253. BVSparse<Recycler>* newIntSet = RecyclerNew(this->GetRecycler(), BVSparse<Recycler>, this->GetRecycler());
  254. newIntSet->Set(intVal);
  255. this->list.Append(taggedInt, this->GetRecycler());
  256. this->u.intSet = newIntSet;
  257. this->kind = SetKind::IntSet;
  258. return;
  259. }
  260. Var simpleVar = JavascriptConversion::TryCanonicalizeAsSimpleVar<false /* allowLossyConversion */>(value);
  261. if (simpleVar)
  262. {
  263. SimpleVarDataSet* newSimpleSet = RecyclerNew(this->GetRecycler(), SimpleVarDataSet, this->GetRecycler());
  264. SetDataNode* node = this->list.Append(simpleVar, this->GetRecycler());
  265. newSimpleSet->Add(simpleVar, node);
  266. this->u.simpleVarSet = newSimpleSet;
  267. this->kind = SetKind::SimpleVarSet;
  268. return;
  269. }
  270. ComplexVarDataSet* newComplexSet = RecyclerNew(this->GetRecycler(), ComplexVarDataSet, this->GetRecycler());
  271. SetDataNode* node = this->list.Append(value, this->GetRecycler());
  272. newComplexSet->Add(value, node);
  273. this->u.complexVarSet = newComplexSet;
  274. this->kind = SetKind::ComplexVarSet;
  275. }
  276. bool
  277. JavascriptSet::TryAddToIntSet(Var value)
  278. {
  279. Assert(this->kind == SetKind::IntSet);
  280. Var taggedInt = JavascriptConversion::TryCanonicalizeAsTaggedInt<false /* allowNegOne */, false /* allowLossyConversion */>(value);
  281. if (!taggedInt)
  282. {
  283. return false;
  284. }
  285. int32 intVal = TaggedInt::ToInt32(taggedInt);
  286. if (!this->u.intSet->TestAndSet(intVal))
  287. {
  288. this->list.Append(taggedInt, this->GetRecycler());
  289. }
  290. return true;
  291. }
  292. bool
  293. JavascriptSet::TryAddToSimpleVarSet(Var value)
  294. {
  295. Assert(this->kind == SetKind::SimpleVarSet);
  296. Var simpleVar = JavascriptConversion::TryCanonicalizeAsSimpleVar<false /* allowLossyConversion */>(value);
  297. if (!simpleVar)
  298. {
  299. return false;
  300. }
  301. if (!this->u.simpleVarSet->ContainsKey(simpleVar))
  302. {
  303. SetDataNode* node = this->list.Append(simpleVar, this->GetRecycler());
  304. this->u.simpleVarSet->Add(simpleVar, node);
  305. }
  306. return true;
  307. }
  308. void
  309. JavascriptSet::AddToComplexVarSet(Var value)
  310. {
  311. Assert(this->kind == SetKind::ComplexVarSet);
  312. if (!this->u.complexVarSet->ContainsKey(value))
  313. {
  314. SetDataNode* node = this->list.Append(value, this->GetRecycler());
  315. this->u.complexVarSet->Add(value, node);
  316. }
  317. }
  318. void
  319. JavascriptSet::Add(Var value)
  320. {
  321. JS_REENTRANCY_LOCK(jsReentLock, this->GetScriptContext()->GetThreadContext());
  322. switch (this->kind)
  323. {
  324. case SetKind::EmptySet:
  325. {
  326. this->AddToEmptySet(value);
  327. return;
  328. }
  329. case SetKind::IntSet:
  330. {
  331. if (this->TryAddToIntSet(value))
  332. {
  333. return;
  334. }
  335. Var simpleVar = JavascriptConversion::TryCanonicalizeAsSimpleVar<false /* allowLossyConversion */>(value);
  336. if (simpleVar)
  337. {
  338. this->PromoteToSimpleVarSet();
  339. this->Add(simpleVar);
  340. return;
  341. }
  342. this->PromoteToComplexVarSet();
  343. this->Add(value);
  344. return;
  345. }
  346. case SetKind::SimpleVarSet:
  347. if (this->TryAddToSimpleVarSet(value))
  348. {
  349. return;
  350. }
  351. this->PromoteToComplexVarSet();
  352. this->Add(value);
  353. return;
  354. case SetKind::ComplexVarSet:
  355. this->AddToComplexVarSet(value);
  356. return;
  357. default:
  358. Assume(UNREACHED);
  359. }
  360. }
  361. void JavascriptSet::Clear()
  362. {
  363. JS_REENTRANCY_LOCK(jsReentLock, this->GetScriptContext()->GetThreadContext());
  364. this->list.Clear();
  365. switch (this->kind)
  366. {
  367. case SetKind::EmptySet:
  368. return;
  369. case SetKind::IntSet:
  370. this->u.intSet->ClearAll();
  371. return;
  372. case SetKind::SimpleVarSet:
  373. this->u.simpleVarSet->Clear();
  374. return;
  375. case SetKind::ComplexVarSet:
  376. this->u.complexVarSet->Clear();
  377. return;
  378. default:
  379. Assume(UNREACHED);
  380. }
  381. }
  382. bool
  383. JavascriptSet::IsInIntSet(Var value)
  384. {
  385. Assert(this->kind == SetKind::IntSet);
  386. Var taggedInt = JavascriptConversion::TryCanonicalizeAsTaggedInt<false /* allowNegOne */, true /* allowLossyConversion */>(value);
  387. if (!taggedInt)
  388. {
  389. return false;
  390. }
  391. int32 intVal = TaggedInt::ToInt32(taggedInt);
  392. return this->u.intSet->Test(intVal);
  393. }
  394. template <bool isComplex>
  395. bool
  396. JavascriptSet::DeleteFromVarSet(Var value)
  397. {
  398. Assert(this->kind == (isComplex ? SetKind::ComplexVarSet : SetKind::SimpleVarSet));
  399. SetDataNode * node = nullptr;
  400. if (isComplex
  401. ? !this->u.complexVarSet->TryGetValueAndRemove(value, &node)
  402. : !this->u.simpleVarSet->TryGetValueAndRemove(value, &node))
  403. {
  404. return false;
  405. }
  406. this->list.Remove(node);
  407. return true;
  408. }
  409. bool
  410. JavascriptSet::DeleteFromSimpleVarSet(Var value)
  411. {
  412. Assert(this->kind == SetKind::SimpleVarSet);
  413. Var simpleVar = JavascriptConversion::TryCanonicalizeAsSimpleVar<true /* allowLossyConversion */>(value);
  414. if (!simpleVar)
  415. {
  416. return false;
  417. }
  418. return this->DeleteFromVarSet<false>(simpleVar);
  419. }
  420. bool JavascriptSet::Delete(Var value)
  421. {
  422. JS_REENTRANCY_LOCK(jsReentLock, this->GetScriptContext()->GetThreadContext());
  423. switch (this->kind)
  424. {
  425. case SetKind::EmptySet:
  426. return false;
  427. case SetKind::IntSet:
  428. if (!IsInIntSet(value))
  429. {
  430. return false;
  431. }
  432. // We don't have the list node pointer readily available, so deletion from int sets would require walking the list
  433. // Because of this, let's just promote to a var set
  434. //
  435. // If this promotion becomes an issue, we can consider options to improve this, e.g. deferring until an iterator is requested
  436. this->PromoteToSimpleVarSet();
  437. return this->Delete(value);
  438. case SetKind::SimpleVarSet:
  439. return this->DeleteFromSimpleVarSet(value);
  440. case SetKind::ComplexVarSet:
  441. return this->DeleteFromVarSet<true /* isComplex */>(value);
  442. default:
  443. Assume(UNREACHED);
  444. return false;
  445. }
  446. }
  447. bool JavascriptSet::Has(Var value)
  448. {
  449. JS_REENTRANCY_LOCK(jsReentLock, this->GetScriptContext()->GetThreadContext());
  450. switch (this->kind)
  451. {
  452. case SetKind::EmptySet:
  453. return false;
  454. case SetKind::IntSet:
  455. {
  456. Var taggedInt = JavascriptConversion::TryCanonicalizeAsTaggedInt<false /* allowNegOne */, true /* allowLossyConversion */ >(value);
  457. if (!taggedInt)
  458. {
  459. return false;
  460. }
  461. int32 intVal = TaggedInt::ToInt32(taggedInt);
  462. return this->u.intSet->Test(intVal);
  463. }
  464. case SetKind::SimpleVarSet:
  465. {
  466. // First check if the value is in the set
  467. if (this->u.simpleVarSet->ContainsKey(value))
  468. {
  469. return true;
  470. }
  471. // If the value isn't in the set, check if the canonical value is
  472. Var simpleVar = JavascriptConversion::TryCanonicalizeAsSimpleVar<true /* allowLossyConversion */>(value);
  473. // If the simple value is the same as the original value, we know it isn't in the set
  474. if (!simpleVar || simpleVar == value)
  475. {
  476. return false;
  477. }
  478. return this->u.simpleVarSet->ContainsKey(simpleVar);
  479. }
  480. case SetKind::ComplexVarSet:
  481. return this->u.complexVarSet->ContainsKey(value);
  482. default:
  483. Assume(UNREACHED);
  484. return false;
  485. }
  486. }
  487. int JavascriptSet::Size()
  488. {
  489. JS_REENTRANCY_LOCK(jsReentLock, this->GetScriptContext()->GetThreadContext());
  490. switch (this->kind)
  491. {
  492. case SetKind::EmptySet:
  493. return 0;
  494. case SetKind::IntSet:
  495. return this->u.intSet->Count();
  496. case SetKind::SimpleVarSet:
  497. return this->u.simpleVarSet->Count();
  498. case SetKind::ComplexVarSet:
  499. return this->u.complexVarSet->Count();
  500. default:
  501. Assume(UNREACHED);
  502. return 0;
  503. }
  504. }
  505. BOOL JavascriptSet::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  506. {
  507. stringBuilder->AppendCppLiteral(_u("Set"));
  508. return TRUE;
  509. }
  510. #if ENABLE_TTD
  511. void JavascriptSet::MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor)
  512. {
  513. auto iterator = this->GetIterator();
  514. while(iterator.Next())
  515. {
  516. extractor->MarkVisitVar(iterator.Current());
  517. }
  518. }
  519. TTD::NSSnapObjects::SnapObjectType JavascriptSet::GetSnapTag_TTD() const
  520. {
  521. return TTD::NSSnapObjects::SnapObjectType::SnapSetObject;
  522. }
  523. void JavascriptSet::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
  524. {
  525. TTD::NSSnapObjects::SnapSetInfo* ssi = alloc.SlabAllocateStruct<TTD::NSSnapObjects::SnapSetInfo>();
  526. ssi->SetSize = 0;
  527. if(this->Size() == 0)
  528. {
  529. ssi->SetValueArray = nullptr;
  530. }
  531. else
  532. {
  533. ssi->SetValueArray = alloc.SlabAllocateArray<TTD::TTDVar>(this->Size());
  534. auto iter = this->GetIterator();
  535. while(iter.Next())
  536. {
  537. ssi->SetValueArray[ssi->SetSize] = iter.Current();
  538. ssi->SetSize++;
  539. }
  540. }
  541. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::NSSnapObjects::SnapSetInfo*, TTD::NSSnapObjects::SnapObjectType::SnapSetObject>(objData, ssi);
  542. }
  543. JavascriptSet* JavascriptSet::CreateForSnapshotRestore(ScriptContext* ctx)
  544. {
  545. JavascriptSet* res = ctx->GetLibrary()->CreateSet();
  546. return res;
  547. }
  548. #endif
  549. }