Set.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashTable.h>
  8. #include <LibJS/Runtime/BigInt.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Object.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS {
  13. struct ValueTraits : public Traits<Value> {
  14. static unsigned hash(Value value)
  15. {
  16. VERIFY(!value.is_empty());
  17. if (value.is_string())
  18. return value.as_string().string().hash();
  19. if (value.is_bigint())
  20. return value.as_bigint().big_integer().hash();
  21. return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints?
  22. }
  23. static bool equals(const Value a, const Value b)
  24. {
  25. return same_value_zero(a, b);
  26. }
  27. };
  28. class Set : public Object {
  29. JS_OBJECT(Set, Object);
  30. public:
  31. static Set* create(GlobalObject&);
  32. explicit Set(Object& prototype);
  33. virtual ~Set() override;
  34. static Set* typed_this(VM&, GlobalObject&);
  35. HashTable<Value, ValueTraits> const& values() const { return m_values; };
  36. HashTable<Value, ValueTraits>& values() { return m_values; };
  37. private:
  38. HashTable<Value, ValueTraits> m_values; // FIXME: Replace with a HashTable that maintains a linked list of insertion order for correct iteration order
  39. };
  40. }