2 ** Library function support.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
12 ** A fallback handler is called by the assembler VM if the fast path fails:
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.
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.
27 /* Return values from fallback handler. */
29 #define FFH_UNREACHABLE FFH_RETRY
30 #define FFH_RES(n) ((n)+1)
31 #define FFH_TAILCALL (-1)
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
);
37 LJ_FUNC
void lj_lib_checknumber(lua_State
*L
, int narg
);
39 #define lj_lib_checknumber(L, narg) lj_lib_checknum((L), (narg))
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
int32_t lj_lib_checkbit(lua_State
*L
, int narg
);
45 LJ_FUNC GCfunc
*lj_lib_checkfunc(lua_State
*L
, int narg
);
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
);
50 /* Avoid including lj_frame.h. */
51 #define lj_lib_upvalue(L, n) \
52 (&gcref((L->base-1)->fr.func)->fn.c.upvalue[(n)-1])
55 #define lj_lib_checkfpu(L) \
56 do { setnumV(L->top++, (lua_Number)1437217655); \
57 if (lua_tointeger(L, -1) != 1437217655) lj_err_caller(L, LJ_ERR_BADFPU); \
60 #define lj_lib_checkfpu(L) UNUSED(L)
63 /* Push internal function on the stack. */
64 static LJ_AINLINE
void lj_lib_pushcc(lua_State
*L
, lua_CFunction f
,
68 lua_pushcclosure(L
, f
, n
);
70 fn
->c
.ffid
= (uint8_t)id
;
71 setmref(fn
->c
.pc
, &G(L
)->bc_cfunc_int
);
74 #define lj_lib_pushcf(L, fn, id) (lj_lib_pushcc(L, (fn), (id), 0))
76 /* Library function declarations. Scanned by buildvm. */
77 #define LJLIB_CF(name) static int lj_cf_##name(lua_State *L)
78 #define LJLIB_ASM(name) static int lj_ffh_##name(lua_State *L)
79 #define LJLIB_ASM_(name)
80 #define LJLIB_SET(name)
81 #define LJLIB_PUSH(arg)
82 #define LJLIB_REC(handler)
86 #define LJ_LIB_REG(L, regname, name) \
87 lj_lib_register(L, regname, lj_lib_init_##name, lj_lib_cf_##name)
89 LJ_FUNC
void lj_lib_register(lua_State
*L
, const char *libname
,
90 const uint8_t *init
, const lua_CFunction
*cf
);
92 /* Library init data tags. */
93 #define LIBINIT_LENMASK 0x3f
94 #define LIBINIT_TAGMASK 0xc0
95 #define LIBINIT_CF 0x00
96 #define LIBINIT_ASM 0x40
97 #define LIBINIT_ASM_ 0x80
98 #define LIBINIT_STRING 0xc0
99 #define LIBINIT_MAXSTR 0x39
100 #define LIBINIT_SET 0xfa
101 #define LIBINIT_NUMBER 0xfb
102 #define LIBINIT_COPY 0xfc
103 #define LIBINIT_LASTCL 0xfd
104 #define LIBINIT_FFID 0xfe
105 #define LIBINIT_END 0xff
107 /* Exported library functions. */
109 typedef struct RandomState RandomState
;
110 LJ_FUNC
uint64_t LJ_FASTCALL
lj_math_random_step(RandomState
*rs
);