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