Builtins.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(MathExp, math_exp, Math, exp, 1) \
  16. O(MathCeil, math_ceil, Math, ceil, 1) \
  17. O(MathFloor, math_floor, Math, floor, 1) \
  18. O(MathRound, math_round, Math, round, 1) \
  19. O(MathSqrt, math_sqrt, Math, sqrt, 1)
  20. enum class Builtin {
  21. #define DEFINE_BUILTIN_ENUM(name, ...) name,
  22. JS_ENUMERATE_BUILTINS(DEFINE_BUILTIN_ENUM)
  23. #undef DEFINE_BUILTIN_ENUM
  24. __Count,
  25. };
  26. static StringView builtin_name(Builtin value)
  27. {
  28. switch (value) {
  29. #define DEFINE_BUILTIN_CASE(name, snake_case_name, base, property, ...) \
  30. case Builtin::name: \
  31. return #base "." #property##sv;
  32. JS_ENUMERATE_BUILTINS(DEFINE_BUILTIN_CASE)
  33. #undef DEFINE_BUILTIN_CASE
  34. case Builtin::__Count:
  35. VERIFY_NOT_REACHED();
  36. }
  37. VERIFY_NOT_REACHED();
  38. }
  39. inline size_t builtin_argument_count(Builtin value)
  40. {
  41. switch (value) {
  42. #define DEFINE_BUILTIN_CASE(name, snake_case_name, base, property, arg_count, ...) \
  43. case Builtin::name: \
  44. return arg_count;
  45. JS_ENUMERATE_BUILTINS(DEFINE_BUILTIN_CASE)
  46. #undef DEFINE_BUILTIN_CASE
  47. case Builtin::__Count:
  48. VERIFY_NOT_REACHED();
  49. }
  50. VERIFY_NOT_REACHED();
  51. }
  52. Optional<Builtin> get_builtin(MemberExpression const& expression);
  53. }
  54. namespace AK {
  55. template<>
  56. struct Formatter<JS::Bytecode::Builtin> : Formatter<StringView> {
  57. ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Builtin value)
  58. {
  59. return Formatter<StringView>::format(builder, builtin_name(value));
  60. }
  61. };
  62. }