Builtins.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2023, Simon Wanner <simon@skyrising.xyz>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <LibJS/Forward.h>
  9. namespace JS::Bytecode {
  10. // TitleCaseName, snake_case_name, base, property, argument_count
  11. #define JS_ENUMERATE_BUILTINS(O) \
  12. O(MathAbs, math_abs, Math, abs, 1) \
  13. O(MathLog, math_log, Math, log, 1) \
  14. O(MathPow, math_pow, Math, pow, 2) \
  15. O(MathCeil, math_ceil, Math, ceil, 1) \
  16. O(MathFloor, math_floor, Math, floor, 1) \
  17. O(MathRound, math_round, Math, round, 1) \
  18. O(MathSqrt, math_sqrt, Math, sqrt, 1)
  19. enum class Builtin {
  20. #define DEFINE_BUILTIN_ENUM(name, ...) name,
  21. JS_ENUMERATE_BUILTINS(DEFINE_BUILTIN_ENUM)
  22. #undef DEFINE_BUILTIN_ENUM
  23. __Count,
  24. };
  25. static StringView builtin_name(Builtin value)
  26. {
  27. switch (value) {
  28. #define DEFINE_BUILTIN_CASE(name, snake_case_name, base, property, ...) \
  29. case Builtin::name: \
  30. return #base "." #property##sv;
  31. JS_ENUMERATE_BUILTINS(DEFINE_BUILTIN_CASE)
  32. #undef DEFINE_BUILTIN_CASE
  33. case Builtin::__Count:
  34. VERIFY_NOT_REACHED();
  35. }
  36. VERIFY_NOT_REACHED();
  37. }
  38. inline size_t builtin_argument_count(Builtin value)
  39. {
  40. switch (value) {
  41. #define DEFINE_BUILTIN_CASE(name, snake_case_name, base, property, arg_count, ...) \
  42. case Builtin::name: \
  43. return arg_count;
  44. JS_ENUMERATE_BUILTINS(DEFINE_BUILTIN_CASE)
  45. #undef DEFINE_BUILTIN_CASE
  46. case Builtin::__Count:
  47. VERIFY_NOT_REACHED();
  48. }
  49. VERIFY_NOT_REACHED();
  50. }
  51. Optional<Builtin> get_builtin(MemberExpression const& expression);
  52. }
  53. namespace AK {
  54. template<>
  55. struct Formatter<JS::Bytecode::Builtin> : Formatter<StringView> {
  56. ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Builtin value)
  57. {
  58. return Formatter<StringView>::format(builder, builtin_name(value));
  59. }
  60. };
  61. }