JavascriptMap.cpp 20 KB

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