Builtins.h 1.8 KB

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