MapPrototype.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/KeyedCollections.h>
  10. #include <LibJS/Runtime/MapIterator.h>
  11. #include <LibJS/Runtime/MapPrototype.h>
  12. namespace JS {
  13. GC_DEFINE_ALLOCATOR(MapPrototype);
  14. MapPrototype::MapPrototype(Realm& realm)
  15. : PrototypeObject(realm.intrinsics().object_prototype())
  16. {
  17. }
  18. void MapPrototype::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. Base::initialize(realm);
  22. u8 attr = Attribute::Writable | Attribute::Configurable;
  23. define_native_function(realm, vm.names.clear, clear, 0, attr);
  24. define_native_function(realm, vm.names.delete_, delete_, 1, attr);
  25. define_native_function(realm, vm.names.entries, entries, 0, attr);
  26. define_native_function(realm, vm.names.forEach, for_each, 1, attr);
  27. define_native_function(realm, vm.names.get, get, 1, attr);
  28. define_native_function(realm, vm.names.has, has, 1, attr);
  29. define_native_function(realm, vm.names.keys, keys, 0, attr);
  30. define_native_function(realm, vm.names.set, set, 2, attr);
  31. define_native_function(realm, vm.names.values, values, 0, attr);
  32. define_native_accessor(realm, vm.names.size, size_getter, {}, Attribute::Configurable);
  33. define_direct_property(vm.well_known_symbol_iterator(), get_without_side_effects(vm.names.entries), attr);
  34. define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, vm.names.Map.as_string()), Attribute::Configurable);
  35. }
  36. // 24.1.3.1 Map.prototype.clear ( ), https://tc39.es/ecma262/#sec-map.prototype.clear
  37. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::clear)
  38. {
  39. // 1. Let M be the this value.
  40. // 2. Perform ? RequireInternalSlot(M, [[MapData]]).
  41. auto map = TRY(typed_this_object(vm));
  42. // 3. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
  43. // a. Set p.[[Key]] to empty.
  44. // b. Set p.[[Value]] to empty.
  45. map->map_clear();
  46. // 4. Return undefined.
  47. return js_undefined();
  48. }
  49. // 24.1.3.3 Map.prototype.delete ( key ), https://tc39.es/ecma262/#sec-map.prototype.delete
  50. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::delete_)
  51. {
  52. auto key = vm.argument(0);
  53. // 1. Let M be the this value.
  54. // 2. Perform ? RequireInternalSlot(M, [[MapData]]).
  55. auto map = TRY(typed_this_object(vm));
  56. // 3. Set key to CanonicalizeKeyedCollectionKey(key).
  57. key = canonicalize_keyed_collection_key(key);
  58. // 3. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
  59. // a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
  60. // i. Set p.[[Key]] to empty.
  61. // ii. Set p.[[Value]] to empty.
  62. // iii. Return true.
  63. // 4. Return false.
  64. return Value(map->map_remove(key));
  65. }
  66. // 24.1.3.4 Map.prototype.entries ( ), https://tc39.es/ecma262/#sec-map.prototype.entries
  67. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::entries)
  68. {
  69. auto& realm = *vm.current_realm();
  70. // 1. Let M be the this value.
  71. auto map = TRY(typed_this_object(vm));
  72. // 2. Return ? CreateMapIterator(M, key+value).
  73. return MapIterator::create(realm, *map, Object::PropertyKind::KeyAndValue);
  74. }
  75. // 24.1.3.5 Map.prototype.forEach ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-map.prototype.foreach
  76. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::for_each)
  77. {
  78. auto callbackfn = vm.argument(0);
  79. auto this_arg = vm.argument(1);
  80. // 1. Let M be the this value.
  81. // 2. Perform ? RequireInternalSlot(M, [[MapData]]).
  82. auto map = TRY(typed_this_object(vm));
  83. // 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
  84. if (!callbackfn.is_function())
  85. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, callbackfn.to_string_without_side_effects());
  86. // 4. Let entries be M.[[MapData]].
  87. // 5. Let numEntries be the number of elements in entries.
  88. // 6. Let index be 0.
  89. // 7. Repeat, while index < numEntries,
  90. for (auto& entry : *map) {
  91. // i. Let e be entries[index].
  92. // b. Set index to index + 1.
  93. // c. If e.[[Key]] is not empty, then
  94. // NOTE: This is handled by Map's IteratorImpl.
  95. // i. Perform ? Call(callbackfn, thisArg, « e.[[Value]], e.[[Key]], M »).
  96. TRY(call(vm, callbackfn.as_function(), this_arg, entry.value, entry.key, map));
  97. // ii. NOTE: The number of elements in entries may have increased during execution of callbackfn.
  98. // iii. Set numEntries to the number of elements in entries.
  99. }
  100. // 8. Return undefined.
  101. return js_undefined();
  102. }
  103. // 24.1.3.6 Map.prototype.get ( key ), https://tc39.es/ecma262/#sec-map.prototype.get
  104. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::get)
  105. {
  106. auto key = vm.argument(0);
  107. // 1. Let M be the this value.
  108. // 2. Perform ? RequireInternalSlot(M, [[MapData]]).
  109. auto map = TRY(typed_this_object(vm));
  110. // 3. Set key to CanonicalizeKeyedCollectionKey(key).
  111. key = canonicalize_keyed_collection_key(key);
  112. // 3. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
  113. // a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
  114. if (auto result = map->map_get(key); result.has_value())
  115. return result.release_value();
  116. // 4. Return undefined.
  117. return js_undefined();
  118. }
  119. // 24.1.3.7 Map.prototype.has ( key ), https://tc39.es/ecma262/#sec-map.prototype.has
  120. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::has)
  121. {
  122. auto key = vm.argument(0);
  123. // 1. Let M be the this value.
  124. // 2. Perform ? RequireInternalSlot(M, [[MapData]]).
  125. auto map = TRY(typed_this_object(vm));
  126. // 3. Set key to CanonicalizeKeyedCollectionKey(key).
  127. key = canonicalize_keyed_collection_key(key);
  128. // 3. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
  129. // a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return true.
  130. // 4. Return false.
  131. return map->map_has(key);
  132. }
  133. // 24.1.3.8 Map.prototype.keys ( ), https://tc39.es/ecma262/#sec-map.prototype.keys
  134. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::keys)
  135. {
  136. auto& realm = *vm.current_realm();
  137. // 1. Let M be the this value.
  138. auto map = TRY(typed_this_object(vm));
  139. // 2. Return ? CreateMapIterator(M, key).
  140. return MapIterator::create(realm, *map, Object::PropertyKind::Key);
  141. }
  142. // 24.1.3.9 Map.prototype.set ( key, value ), https://tc39.es/ecma262/#sec-map.prototype.set
  143. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::set)
  144. {
  145. auto key = vm.argument(0);
  146. auto value = vm.argument(1);
  147. // 1. Let M be the this value.
  148. // 2. Perform ? RequireInternalSlot(M, [[MapData]]).
  149. auto map = TRY(typed_this_object(vm));
  150. // 3. Set key to CanonicalizeKeyedCollectionKey(key).
  151. key = canonicalize_keyed_collection_key(key);
  152. // 4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
  153. // a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
  154. // i. Set p.[[Value]] to value.
  155. // ii. Return M.
  156. // 5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
  157. // 6. Append p to M.[[MapData]].
  158. map->map_set(key, value);
  159. // 7. Return M.
  160. return map;
  161. }
  162. // 24.1.3.10 get Map.prototype.size, https://tc39.es/ecma262/#sec-get-map.prototype.size
  163. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::size_getter)
  164. {
  165. // 1. Let M be the this value.
  166. // 2. Perform ? RequireInternalSlot(M, [[MapData]]).
  167. auto map = TRY(typed_this_object(vm));
  168. // 3. Let count be 0.
  169. // 4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
  170. // a. If p.[[Key]] is not empty, set count to count + 1.
  171. auto count = map->map_size();
  172. // 5. Return 𝔽(count).
  173. return Value(count);
  174. }
  175. // 24.1.3.11 Map.prototype.values ( ), https://tc39.es/ecma262/#sec-map.prototype.values
  176. JS_DEFINE_NATIVE_FUNCTION(MapPrototype::values)
  177. {
  178. auto& realm = *vm.current_realm();
  179. // 1. Let M be the this value.
  180. auto map = TRY(typed_this_object(vm));
  181. // 2. Return ? CreateMapIterator(M, value).
  182. return MapIterator::create(realm, *map, Object::PropertyKind::Value);
  183. }
  184. }