diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-06-09 00:08:47 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-09 11:48:04 +0100 |
commit | 670be04c816b978e6308e18e8b18a0f784bf577c (patch) | |
tree | b3aaa8e9079f27737d2c5c1052afc95ebbdc5e08 /Userland/Libraries/LibJS/Runtime/Set.h | |
parent | b17a282b4b0a1f2831ea133b42aeec9e3d3e1208 (diff) | |
download | serenity-670be04c816b978e6308e18e8b18a0f784bf577c.zip |
LibJS: Add the Set built-in object
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Set.h')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Set.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Set.h b/Userland/Libraries/LibJS/Runtime/Set.h new file mode 100644 index 0000000000..289d361c03 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Set.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/HashTable.h> +#include <LibJS/Runtime/BigInt.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Object.h> +#include <LibJS/Runtime/Value.h> + +namespace JS { + +struct ValueTraits : public Traits<Value> { + static unsigned hash(Value value) + { + VERIFY(!value.is_empty()); + if (value.is_string()) + return value.as_string().string().hash(); + + if (value.is_bigint()) + return value.as_bigint().big_integer().hash(); + + return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints? + } + static bool equals(const Value a, const Value b) + { + return same_value_zero(a, b); + } +}; + +class Set : public Object { + JS_OBJECT(Set, Object); + +public: + static Set* create(GlobalObject&); + + explicit Set(Object& prototype); + virtual ~Set() override; + + static Set* typed_this(VM&, GlobalObject&); + + HashTable<Value, ValueTraits> const& values() const { return m_values; }; + HashTable<Value, ValueTraits>& values() { return m_values; }; + +private: + HashTable<Value, ValueTraits> m_values; // FIXME: Replace with a HashTable that maintains a linked list of insertion order for correct iteration order +}; + +} |