ladybird/Userland/Libraries/LibJS/Runtime/ValueTraits.h
Andreas Kling cc4b3cbacc
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-14, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
Meta: Update my e-mail address everywhere
2024-10-04 13:19:50 +02:00

45 lines
1.5 KiB
C++

/*
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2020-2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/PrimitiveString.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()) {
// FIXME: Propagate this error.
return value.as_string().byte_string().hash();
}
if (value.is_bigint())
return value.as_bigint().big_integer().hash();
// In the IEEE 754 standard a NaN value is encoded as any value from 0x7ff0000000000001 to 0x7fffffffffffffff,
// with the least significant bits (referred to as the 'payload') carrying some kind of diagnostic information
// indicating the source of the NaN. Since ECMA262 does not differentiate between different kinds of NaN values,
// Sets and Maps must not differentiate between them either.
// This is achieved by replacing any NaN value by a canonical qNaN.
if (value.is_nan())
value = js_nan();
return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints?
}
static bool equals(Value const a, Value const b)
{
return same_value(a, b);
}
};
}