lj_lib.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. ** Library function support.
  3. ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_LIB_H
  6. #define _LJ_LIB_H
  7. #include "lj_obj.h"
  8. /*
  9. ** A fallback handler is called by the assembler VM if the fast path fails:
  10. **
  11. ** - too few arguments: unrecoverable.
  12. ** - wrong argument type: recoverable, if coercion succeeds.
  13. ** - bad argument value: unrecoverable.
  14. ** - stack overflow: recoverable, if stack reallocation succeeds.
  15. ** - extra handling: recoverable.
  16. **
  17. ** The unrecoverable cases throw an error with lj_err_arg(), lj_err_argtype(),
  18. ** lj_err_caller() or lj_err_callermsg().
  19. ** The recoverable cases return 0 or the number of results + 1.
  20. ** The assembler VM retries the fast path only if 0 is returned.
  21. ** This time the fallback must not be called again or it gets stuck in a loop.
  22. */
  23. /* Return values from fallback handler. */
  24. #define FFH_RETRY 0
  25. #define FFH_UNREACHABLE FFH_RETRY
  26. #define FFH_RES(n) ((n)+1)
  27. #define FFH_TAILCALL (-1)
  28. LJ_FUNC TValue *lj_lib_checkany(lua_State *L, int narg);
  29. LJ_FUNC GCstr *lj_lib_checkstr(lua_State *L, int narg);
  30. LJ_FUNC GCstr *lj_lib_optstr(lua_State *L, int narg);
  31. #if LJ_DUALNUM
  32. LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg);
  33. #else
  34. #define lj_lib_checknumber(L, narg) lj_lib_checknum((L), (narg))
  35. #endif
  36. LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg);
  37. LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg);
  38. LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def);
  39. LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg);
  40. LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg);
  41. LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg);
  42. LJ_FUNC int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst);
  43. /* Avoid including lj_frame.h. */
  44. #define lj_lib_upvalue(L, n) \
  45. (&gcref((L->base-1)->fr.func)->fn.c.upvalue[(n)-1])
  46. #if LJ_TARGET_WINDOWS
  47. #define lj_lib_checkfpu(L) \
  48. do { setnumV(L->top++, (lua_Number)1437217655); \
  49. if (lua_tointeger(L, -1) != 1437217655) lj_err_caller(L, LJ_ERR_BADFPU); \
  50. L->top--; } while (0)
  51. #else
  52. #define lj_lib_checkfpu(L) UNUSED(L)
  53. #endif
  54. /* Push internal function on the stack. */
  55. static LJ_AINLINE GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f,
  56. int id, int n)
  57. {
  58. GCfunc *fn;
  59. lua_pushcclosure(L, f, n);
  60. fn = funcV(L->top-1);
  61. fn->c.ffid = (uint8_t)id;
  62. setmref(fn->c.pc, &G(L)->bc_cfunc_int);
  63. return fn;
  64. }
  65. #define lj_lib_pushcf(L, fn, id) (lj_lib_pushcc(L, (fn), (id), 0))
  66. /* Library function declarations. Scanned by buildvm. */
  67. #define LJLIB_CF(name) static int lj_cf_##name(lua_State *L)
  68. #define LJLIB_ASM(name) static int lj_ffh_##name(lua_State *L)
  69. #define LJLIB_ASM_(name)
  70. #define LJLIB_LUA(name)
  71. #define LJLIB_SET(name)
  72. #define LJLIB_PUSH(arg)
  73. #define LJLIB_REC(handler)
  74. #define LJLIB_NOREGUV
  75. #define LJLIB_NOREG
  76. #define LJ_LIB_REG(L, regname, name) \
  77. lj_lib_register(L, regname, lj_lib_init_##name, lj_lib_cf_##name)
  78. LJ_FUNC void lj_lib_register(lua_State *L, const char *libname,
  79. const uint8_t *init, const lua_CFunction *cf);
  80. LJ_FUNC void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f,
  81. GCtab *env);
  82. /* Library init data tags. */
  83. #define LIBINIT_LENMASK 0x3f
  84. #define LIBINIT_TAGMASK 0xc0
  85. #define LIBINIT_CF 0x00
  86. #define LIBINIT_ASM 0x40
  87. #define LIBINIT_ASM_ 0x80
  88. #define LIBINIT_STRING 0xc0
  89. #define LIBINIT_MAXSTR 0x38
  90. #define LIBINIT_LUA 0xf9
  91. #define LIBINIT_SET 0xfa
  92. #define LIBINIT_NUMBER 0xfb
  93. #define LIBINIT_COPY 0xfc
  94. #define LIBINIT_LASTCL 0xfd
  95. #define LIBINIT_FFID 0xfe
  96. #define LIBINIT_END 0xff
  97. /* Exported library functions. */
  98. typedef struct RandomState RandomState;
  99. LJ_FUNC uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs);
  100. #endif