Skip to main content

luaur_code_gen/functions/
init_functions.rs

1use crate::records::native_context::NativeContext;
2use crate::type_aliases::lua_state::lua_State;
3use crate::type_aliases::luau_fast_function::luau_fast_function;
4use luaur_vm::type_aliases::stk_id::StkId;
5use luaur_vm::type_aliases::t_value::TValue;
6use luaur_vm::type_aliases::tms::TMS;
7
8extern "C" {
9    #[link_name = "luaur_luaV_lessthan"]
10    pub fn luaV_lessthan(L: *mut lua_State, l: *const TValue, r: *const TValue)
11        -> core::ffi::c_int;
12    #[link_name = "luaur_luaV_lessequal"]
13    pub fn luaV_lessequal(
14        L: *mut lua_State,
15        l: *const TValue,
16        r: *const TValue,
17    ) -> core::ffi::c_int;
18    #[link_name = "luaur_luaV_equalval"]
19    pub fn luaV_equalval(
20        L: *mut lua_State,
21        t1: *const TValue,
22        t2: *const TValue,
23    ) -> core::ffi::c_int;
24
25    #[link_name = "luaur_luaV_doarithimpl_TM_ADD"]
26    pub fn luaV_doarithimpl_TM_ADD(
27        L: *mut lua_State,
28        ra: StkId,
29        rb: *const TValue,
30        rc: *const TValue,
31    );
32    #[link_name = "luaur_luaV_doarithimpl_TM_SUB"]
33    pub fn luaV_doarithimpl_TM_SUB(
34        L: *mut lua_State,
35        ra: StkId,
36        rb: *const TValue,
37        rc: *const TValue,
38    );
39    #[link_name = "luaur_luaV_doarithimpl_TM_MUL"]
40    pub fn luaV_doarithimpl_TM_MUL(
41        L: *mut lua_State,
42        ra: StkId,
43        rb: *const TValue,
44        rc: *const TValue,
45    );
46    #[link_name = "luaur_luaV_doarithimpl_TM_DIV"]
47    pub fn luaV_doarithimpl_TM_DIV(
48        L: *mut lua_State,
49        ra: StkId,
50        rb: *const TValue,
51        rc: *const TValue,
52    );
53    #[link_name = "luaur_luaV_doarithimpl_TM_IDIV"]
54    pub fn luaV_doarithimpl_TM_IDIV(
55        L: *mut lua_State,
56        ra: StkId,
57        rb: *const TValue,
58        rc: *const TValue,
59    );
60    #[link_name = "luaur_luaV_doarithimpl_TM_MOD"]
61    pub fn luaV_doarithimpl_TM_MOD(
62        L: *mut lua_State,
63        ra: StkId,
64        rb: *const TValue,
65        rc: *const TValue,
66    );
67    #[link_name = "luaur_luaV_doarithimpl_TM_POW"]
68    pub fn luaV_doarithimpl_TM_POW(
69        L: *mut lua_State,
70        ra: StkId,
71        rb: *const TValue,
72        rc: *const TValue,
73    );
74    #[link_name = "luaur_luaV_doarithimpl_TM_UNM"]
75    pub fn luaV_doarithimpl_TM_UNM(
76        L: *mut lua_State,
77        ra: StkId,
78        rb: *const TValue,
79        rc: *const TValue,
80    );
81
82    #[link_name = "luaur_luaV_dolen"]
83    pub fn luaV_dolen(L: *mut lua_State, ra: StkId, rb: *const TValue);
84    #[link_name = "luaur_luaV_gettable"]
85    pub fn luaV_gettable(L: *mut lua_State, t: *const TValue, key: *mut TValue, val: StkId);
86    #[link_name = "luaur_luaV_settable"]
87    pub fn luaV_settable(L: *mut lua_State, t: *const TValue, key: *mut TValue, val: StkId);
88    #[link_name = "luaur_luaV_concat"]
89    pub fn luaV_concat(L: *mut lua_State, total: core::ffi::c_int, last: core::ffi::c_int);
90
91    #[link_name = "luaur_luaH_getn"]
92    pub fn luaH_getn(t: *mut core::ffi::c_void) -> core::ffi::c_int;
93    #[link_name = "luaur_luaH_new"]
94    pub fn luaH_new(
95        L: *mut lua_State,
96        narray: core::ffi::c_int,
97        lnhash: core::ffi::c_int,
98    ) -> *mut core::ffi::c_void;
99    #[link_name = "luaur_luaH_clone"]
100    pub fn luaH_clone(L: *mut lua_State, tt: *mut core::ffi::c_void) -> *mut core::ffi::c_void;
101    #[link_name = "luaur_luaH_resizearray"]
102    pub fn luaH_resizearray(L: *mut lua_State, t: *mut core::ffi::c_void, nasize: core::ffi::c_int);
103    #[link_name = "luaur_luaH_setnum"]
104    pub fn luaH_setnum(
105        L: *mut lua_State,
106        t: *mut core::ffi::c_void,
107        key: core::ffi::c_int,
108    ) -> *mut TValue;
109
110    #[link_name = "luaur_luaC_barriertable"]
111    pub fn luaC_barriertable(
112        L: *mut lua_State,
113        t: *mut core::ffi::c_void,
114        v: *mut core::ffi::c_void,
115    );
116    #[link_name = "luaur_luaC_barrierf"]
117    pub fn luaC_barrierf(L: *mut lua_State, o: *mut core::ffi::c_void, v: *mut core::ffi::c_void);
118    #[link_name = "luaur_luaC_barrierback"]
119    pub fn luaC_barrierback(
120        L: *mut lua_State,
121        o: *mut core::ffi::c_void,
122        gclist: *mut *mut core::ffi::c_void,
123    );
124    #[link_name = "luaur_luaC_step"]
125    pub fn luaC_step(L: *mut lua_State, assist: bool) -> usize;
126
127    #[link_name = "luaur_luaF_close"]
128    pub fn luaF_close(L: *mut lua_State, level: StkId);
129    #[link_name = "luaur_luaF_findupval"]
130    pub fn luaF_findupval(L: *mut lua_State, level: StkId) -> *mut core::ffi::c_void;
131    #[link_name = "luaur_luaF_newLclosure"]
132    pub fn luaF_newLclosure(
133        L: *mut lua_State,
134        nelems: core::ffi::c_int,
135        e: *mut core::ffi::c_void,
136        p: *mut core::ffi::c_void,
137    ) -> *mut core::ffi::c_void;
138
139    #[link_name = "luaur_luaT_gettm"]
140    pub fn luaT_gettm(
141        events: *mut core::ffi::c_void,
142        event: TMS,
143        ename: *mut core::ffi::c_void,
144    ) -> *const TValue;
145    #[link_name = "luaur_luaT_objtypenamestr"]
146    pub fn luaT_objtypenamestr(L: *mut lua_State, o: *const TValue) -> *const core::ffi::c_void;
147
148    pub fn exp(x: f64) -> f64;
149    pub fn pow(x: f64, y: f64) -> f64;
150    pub fn fmod(x: f64, y: f64) -> f64;
151    pub fn log(x: f64) -> f64;
152    pub fn log2(x: f64) -> f64;
153    pub fn log10(x: f64) -> f64;
154    pub fn ldexp(x: f64, exp: core::ffi::c_int) -> f64;
155    pub fn round(x: f64) -> f64;
156    pub fn frexp(x: f64, exp: *mut core::ffi::c_int) -> f64;
157    pub fn modf(x: f64, iptr: *mut f64) -> f64;
158
159    pub fn asin(x: f64) -> f64;
160    pub fn sin(x: f64) -> f64;
161    pub fn sinh(x: f64) -> f64;
162    pub fn acos(x: f64) -> f64;
163    pub fn cos(x: f64) -> f64;
164    pub fn cosh(x: f64) -> f64;
165    pub fn atan(x: f64) -> f64;
166    pub fn atan2(y: f64, x: f64) -> f64;
167    pub fn tan(x: f64) -> f64;
168    pub fn tanh(x: f64) -> f64;
169
170    #[link_name = "luaur_forgLoopTableIter"]
171    pub fn forgLoopTableIter(
172        L: *mut lua_State,
173        h: *mut core::ffi::c_void,
174        index: core::ffi::c_int,
175        ra: *mut TValue,
176    ) -> bool;
177    #[link_name = "luaur_forgLoopNodeIter"]
178    pub fn forgLoopNodeIter(
179        L: *mut lua_State,
180        h: *mut core::ffi::c_void,
181        index: core::ffi::c_int,
182        ra: *mut TValue,
183    ) -> bool;
184    #[link_name = "luaur_forgLoopNonTableFallback"]
185    pub fn forgLoopNonTableFallback(
186        L: *mut lua_State,
187        insnA: core::ffi::c_int,
188        aux: core::ffi::c_int,
189    ) -> core::ffi::c_int;
190    #[link_name = "luaur_forgLoopNonTableFallback_DEPRECATED"]
191    pub fn forgLoopNonTableFallback_DEPRECATED(
192        L: *mut lua_State,
193        insnA: core::ffi::c_int,
194        aux: core::ffi::c_int,
195    ) -> bool;
196    #[link_name = "luaur_forgPrepXnextFallback"]
197    pub fn forgPrepXnextFallback(L: *mut lua_State, ra: *mut TValue, pc: core::ffi::c_int);
198    #[link_name = "luaur_callProlog"]
199    pub fn callProlog(
200        L: *mut lua_State,
201        ra: *mut TValue,
202        argtop: StkId,
203        nresults: core::ffi::c_int,
204    ) -> *mut core::ffi::c_void;
205    #[link_name = "luaur_callEpilogC"]
206    pub fn callEpilogC(L: *mut lua_State, nresults: core::ffi::c_int, n: core::ffi::c_int);
207    #[link_name = "luaur_newUserdata"]
208    pub fn newUserdata(
209        L: *mut lua_State,
210        s: usize,
211        tag: core::ffi::c_int,
212    ) -> *mut core::ffi::c_void;
213    #[link_name = "luaur_getImport"]
214    pub fn getImport(L: *mut lua_State, res: StkId, id: core::ffi::c_uint, pc: core::ffi::c_uint);
215
216    #[link_name = "luaur_callFallback"]
217    pub fn callFallback(
218        L: *mut lua_State,
219        ra: StkId,
220        argtop: StkId,
221        nresults: core::ffi::c_int,
222    ) -> *mut core::ffi::c_void;
223
224    #[link_name = "luaur_executeGETGLOBAL"]
225    pub fn executeGETGLOBAL(
226        L: *mut lua_State,
227        pc: *const u32,
228        base: StkId,
229        k: *mut TValue,
230    ) -> *const u32;
231    #[link_name = "luaur_executeSETGLOBAL"]
232    pub fn executeSETGLOBAL(
233        L: *mut lua_State,
234        pc: *const u32,
235        base: StkId,
236        k: *mut TValue,
237    ) -> *const u32;
238    #[link_name = "luaur_executeGETTABLEKS"]
239    pub fn executeGETTABLEKS(
240        L: *mut lua_State,
241        pc: *const u32,
242        base: StkId,
243        k: *mut TValue,
244    ) -> *const u32;
245    #[link_name = "luaur_executeSETTABLEKS"]
246    pub fn executeSETTABLEKS(
247        L: *mut lua_State,
248        pc: *const u32,
249        base: StkId,
250        k: *mut TValue,
251    ) -> *const u32;
252    #[link_name = "luaur_executeNAMECALL"]
253    pub fn executeNAMECALL(
254        L: *mut lua_State,
255        pc: *const u32,
256        base: StkId,
257        k: *mut TValue,
258    ) -> *const u32;
259    #[link_name = "luaur_executeFORGPREP"]
260    pub fn executeFORGPREP(
261        L: *mut lua_State,
262        pc: *const u32,
263        base: StkId,
264        k: *mut TValue,
265    ) -> *const u32;
266    #[link_name = "luaur_executeGETVARARGSMultRet"]
267    pub fn executeGETVARARGSMultRet(
268        L: *mut lua_State,
269        pc: *const u32,
270        base: StkId,
271        rai: core::ffi::c_int,
272    );
273    #[link_name = "luaur_executeGETVARARGSConst"]
274    pub fn executeGETVARARGSConst(
275        L: *mut lua_State,
276        base: StkId,
277        rai: core::ffi::c_int,
278        b: core::ffi::c_int,
279    );
280    #[link_name = "luaur_executeDUPCLOSURE"]
281    pub fn executeDUPCLOSURE(
282        L: *mut lua_State,
283        pc: *const u32,
284        base: StkId,
285        k: *mut TValue,
286    ) -> *const u32;
287    #[link_name = "luaur_executePREPVARARGS"]
288    pub fn executePREPVARARGS(
289        L: *mut lua_State,
290        pc: *const u32,
291        base: StkId,
292        k: *mut TValue,
293    ) -> *const u32;
294    #[link_name = "luaur_executeSETLIST"]
295    pub fn executeSETLIST(
296        L: *mut lua_State,
297        pc: *const u32,
298        base: StkId,
299        k: *mut TValue,
300    ) -> *const u32;
301
302    #[link_name = "luaur_luauF_table"]
303    pub static luauF_table: [luau_fast_function; 256];
304}
305
306pub fn init_functions(context: &mut NativeContext) {
307    unsafe {
308        core::ptr::copy_nonoverlapping(luauF_table.as_ptr(), context.luauF_table.as_mut_ptr(), 256);
309
310        context.luaV_lessthan = Some(luaV_lessthan);
311        context.luaV_lessequal = Some(luaV_lessequal);
312        context.luaV_equalval = Some(luaV_equalval);
313
314        context.luaV_doarithadd = Some(luaV_doarithimpl_TM_ADD);
315        context.luaV_doarithsub = Some(luaV_doarithimpl_TM_SUB);
316        context.luaV_doarithmul = Some(luaV_doarithimpl_TM_MUL);
317        context.luaV_doarithdiv = Some(luaV_doarithimpl_TM_DIV);
318        context.luaV_doarithidiv = Some(luaV_doarithimpl_TM_IDIV);
319        context.luaV_doarithmod = Some(luaV_doarithimpl_TM_MOD);
320        context.luaV_doarithpow = Some(luaV_doarithimpl_TM_POW);
321        context.luaV_doarithunm = Some(luaV_doarithimpl_TM_UNM);
322
323        context.luaV_dolen = Some(luaV_dolen);
324        context.luaV_gettable = Some(luaV_gettable);
325        context.luaV_settable = Some(luaV_settable);
326        context.luaV_concat = Some(luaV_concat);
327
328        context.luaH_getn = Some(luaH_getn);
329        context.luaH_new = Some(luaH_new);
330        context.luaH_clone = Some(luaH_clone);
331        context.luaH_resizearray = Some(luaH_resizearray);
332        context.luaH_setnum = Some(luaH_setnum);
333
334        context.luaC_barriertable = Some(luaC_barriertable);
335        context.luaC_barrierf = Some(luaC_barrierf);
336        context.luaC_barrierback = Some(luaC_barrierback);
337        context.luaC_step = Some(luaC_step);
338
339        context.luaF_close = Some(luaF_close);
340        context.luaF_findupval = Some(luaF_findupval);
341        context.luaF_newLclosure = Some(luaF_newLclosure);
342
343        context.luaT_gettm = Some(luaT_gettm);
344        context.luaT_objtypenamestr = Some(luaT_objtypenamestr);
345
346        context.libm_exp = Some(exp);
347        context.libm_pow = Some(pow);
348        context.libm_fmod = Some(fmod);
349        context.libm_log = Some(log);
350        context.libm_log2 = Some(log2);
351        context.libm_log10 = Some(log10);
352        context.libm_ldexp = Some(ldexp);
353        context.libm_round = Some(round);
354        context.libm_frexp = Some(frexp);
355        context.libm_modf = Some(modf);
356
357        context.libm_asin = Some(asin);
358        context.libm_sin = Some(sin);
359        context.libm_sinh = Some(sinh);
360        context.libm_acos = Some(acos);
361        context.libm_cos = Some(cos);
362        context.libm_cosh = Some(cosh);
363        context.libm_atan = Some(atan);
364        context.libm_atan2 = Some(atan2);
365        context.libm_tan = Some(tan);
366        context.libm_tanh = Some(tanh);
367
368        context.forgLoopTableIter = Some(forgLoopTableIter);
369        context.forgLoopNodeIter = Some(forgLoopNodeIter);
370        context.forgLoopNonTableFallback = Some(forgLoopNonTableFallback);
371        context.forgLoopNonTableFallback_DEPRECATED = Some(forgLoopNonTableFallback_DEPRECATED);
372        context.forgPrepXnextFallback = Some(forgPrepXnextFallback);
373        context.callProlog = Some(callProlog);
374        context.callEpilogC = Some(callEpilogC);
375        context.newUserdata = Some(newUserdata);
376        context.getImport = Some(getImport);
377
378        context.callFallback = Some(callFallback);
379
380        context.executeGETGLOBAL = Some(executeGETGLOBAL);
381        context.executeSETGLOBAL = Some(executeSETGLOBAL);
382        context.executeGETTABLEKS = Some(executeGETTABLEKS);
383        context.executeSETTABLEKS = Some(executeSETTABLEKS);
384
385        context.executeNAMECALL = Some(executeNAMECALL);
386        context.executeFORGPREP = Some(executeFORGPREP);
387        context.executeGETVARARGSMultRet = Some(executeGETVARARGSMultRet);
388        context.executeGETVARARGSConst = Some(executeGETVARARGSConst);
389        context.executeDUPCLOSURE = Some(executeDUPCLOSURE);
390        context.executePREPVARARGS = Some(executePREPVARARGS);
391        context.executeSETLIST = Some(executeSETLIST);
392    }
393}