ValueInlines.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibJS/Runtime/Value.h>
  9. namespace JS {
  10. inline bool Value::to_boolean() const
  11. {
  12. // OPTIMIZATION: Fast path for when this value is already a boolean.
  13. if (is_boolean())
  14. return as_bool();
  15. return to_boolean_slow_case();
  16. }
  17. inline ThrowCompletionOr<Value> Value::to_number(VM& vm) const
  18. {
  19. // OPTIMIZATION: Fast path for when this value is already a number.
  20. if (is_number())
  21. return *this;
  22. return to_number_slow_case(vm);
  23. }
  24. inline ThrowCompletionOr<Value> Value::to_numeric(VM& vm) const
  25. {
  26. // OPTIMIZATION: Fast path for when this value is already a number.
  27. if (is_number())
  28. return *this;
  29. return to_numeric_slow_case(vm);
  30. }
  31. inline ThrowCompletionOr<Value> Value::to_primitive(VM& vm, PreferredType preferred_type) const
  32. {
  33. if (!is_object())
  34. return *this;
  35. return to_primitive_slow_case(vm, preferred_type);
  36. }
  37. }