xref: /netbsd/external/mit/lua/dist/src/lvm.h (revision f13f21ab)
1 /*	$NetBSD: lvm.h,v 1.12 2023/06/08 21:12:08 nikita Exp $	*/
2 
3 /*
4 ** Id: lvm.h
5 ** Lua virtual machine
6 ** See Copyright Notice in lua.h
7 */
8 
9 #ifndef lvm_h
10 #define lvm_h
11 
12 
13 #include "ldo.h"
14 #include "lobject.h"
15 #include "ltm.h"
16 
17 
18 #if !defined(LUA_NOCVTN2S)
19 #define cvt2str(o)	ttisnumber(o)
20 #else
21 #define cvt2str(o)	0	/* no conversion from numbers to strings */
22 #endif
23 
24 
25 #if !defined(LUA_NOCVTS2N)
26 #define cvt2num(o)	ttisstring(o)
27 #else
28 #define cvt2num(o)	0	/* no conversion from strings to numbers */
29 #endif
30 
31 
32 /*
33 ** You can define LUA_FLOORN2I if you want to convert floats to integers
34 ** by flooring them (instead of raising an error if they are not
35 ** integral values)
36 */
37 #if !defined(LUA_FLOORN2I)
38 #define LUA_FLOORN2I		F2Ieq
39 #endif
40 
41 
42 /*
43 ** Rounding modes for float->integer coercion
44  */
45 typedef enum {
46   F2Ieq,     /* no rounding; accepts only integral values */
47   F2Ifloor,  /* takes the floor of the number */
48   F2Iceil    /* takes the ceil of the number */
49 } F2Imod;
50 
51 
52 /* convert an object to a float (including string coercion) */
53 #ifndef _KERNEL
54 #define tonumber(o,n) \
55 	(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
56 #else /* _KERNEL */
57 #define tonumber       tointeger
58 #endif /* _KERNEL */
59 
60 
61 /* convert an object to a float (without string coercion) */
62 #define tonumberns(o,n) \
63 	(ttisfloat(o) ? ((n) = fltvalue(o), 1) : \
64 	(ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0))
65 
66 
67 /* convert an object to an integer (including string coercion) */
68 #define tointeger(o,i) \
69   (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
70                           : luaV_tointeger(o,i,LUA_FLOORN2I))
71 
72 
73 /* convert an object to an integer (without string coercion) */
74 #define tointegerns(o,i) \
75   (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
76                           : luaV_tointegerns(o,i,LUA_FLOORN2I))
77 
78 
79 #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
80 
81 #define luaV_rawequalobj(t1,t2)		luaV_equalobj(NULL,t1,t2)
82 
83 
84 /*
85 ** fast track for 'gettable': if 't' is a table and 't[k]' is present,
86 ** return 1 with 'slot' pointing to 't[k]' (position of final result).
87 ** Otherwise, return 0 (meaning it will have to check metamethod)
88 ** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL
89 ** (otherwise). 'f' is the raw get function to use.
90 */
91 #define luaV_fastget(L,t,k,slot,f) \
92   (!ttistable(t)  \
93    ? (slot = NULL, 0)  /* not a table; 'slot' is NULL and result is 0 */  \
94    : (slot = f(hvalue(t), k),  /* else, do raw access */  \
95       !isempty(slot)))  /* result not empty? */
96 
97 
98 /*
99 ** Special case of 'luaV_fastget' for integers, inlining the fast case
100 ** of 'luaH_getint'.
101 */
102 #define luaV_fastgeti(L,t,k,slot) \
103   (!ttistable(t)  \
104    ? (slot = NULL, 0)  /* not a table; 'slot' is NULL and result is 0 */  \
105    : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \
106               ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \
107       !isempty(slot)))  /* result not empty? */
108 
109 
110 /*
111 ** Finish a fast set operation (when fast get succeeds). In that case,
112 ** 'slot' points to the place to put the value.
113 */
114 #define luaV_finishfastset(L,t,slot,v) \
115     { setobj2t(L, cast(TValue *,slot), v); \
116       luaC_barrierback(L, gcvalue(t), v); }
117 
118 
119 /*
120 ** Shift right is the same as shift left with a negative 'y'
121 */
122 #define luaV_shiftr(x,y)	luaV_shiftl(x,intop(-, 0, y))
123 
124 
125 
126 LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
127 LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
128 LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
129 LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
130 LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
131 LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
132                                 F2Imod mode);
133 LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
134 LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
135                                StkId val, const TValue *slot);
136 LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
137                                TValue *val, const TValue *slot);
138 LUAI_FUNC void luaV_finishOp (lua_State *L);
139 LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci);
140 LUAI_FUNC void luaV_concat (lua_State *L, int total);
141 LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y);
142 LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
143 LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y);
144 LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
145 LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
146 
147 #endif
148