JavascriptMap.cpp 21 KB

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