-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLua51.cs
More file actions
873 lines (663 loc) · 29.7 KB
/
Lua51.cs
File metadata and controls
873 lines (663 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
using System.Runtime.InteropServices;
using size_t = System.UInt64;
using lua_Number = System.Double;
using lua_Integer = System.Int64;
namespace LuaNET.Lua51;
public struct lua_State : IEquatable<lua_State>
{
public nuint Handle;
public readonly bool IsNull => Handle == 0;
public readonly bool IsNotNull => Handle != 0;
public static bool operator !(lua_State state) => state.Handle == 0;
public static bool operator ==(lua_State state1, lua_State state2) => state1.Handle == state2.Handle;
public static bool operator ==(lua_State state1, int handle) => state1.Handle == (nuint) handle;
public static bool operator !=(lua_State state1, lua_State state2) => state1.Handle != state2.Handle;
public static bool operator !=(lua_State state1, int handle) => state1.Handle != (nuint) handle;
public readonly bool Equals(lua_State other) => Handle == other.Handle;
public override readonly bool Equals(object? other) => other is lua_State state && Equals(state);
public override readonly int GetHashCode() => Handle.GetHashCode();
}
public static class Lua
{
private const string DllName = "lua515";
private const CallingConvention Convention = CallingConvention.Cdecl;
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct lua_Debug {
public int _event;
public string name;
public string namewhat;
public string what;
public string source;
public int currentline;
public int nups;
public int linedefined;
public int lastlinedefined;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = LUA_IDSIZE)]
public sbyte[] short_src;
public int i_ci;
};
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct luaL_Reg {
public string name;
public nint func;
};
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct luaL_Buffer {
public nint p;
public int lvl;
public lua_State L;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = LUAL_BUFFERSIZE)]
public sbyte[] buffer;
};
public delegate int lua_CFunction(lua_State L);
public delegate nint lua_Reader(lua_State L, nuint ud, ref size_t sz);
public delegate int lua_Writer(lua_State L, nuint p, size_t sz, nuint ud);
public delegate nuint lua_Alloc(nuint ud, nuint ptr, size_t osize, size_t nsize);
public delegate void lua_Hook(lua_State L, lua_Debug ar);
public static unsafe luaL_Reg AsLuaLReg(string name, delegate*unmanaged<lua_State, int> func) => new() { name = name, func = (nint) func };
public const string LUA_PATH = "LUA_PATH";
public const string LUA_CPATH = "LUA_CPATH";
public const string LUA_INIT = "LUA_INIT";
public const string LUA_LDIR = "!\\lua\\";
public const string LUA_CDIR = "!\\";
public const string LUA_PATH_DEFAULT = ".\\?.lua;" + LUA_LDIR + "?.lua;" + LUA_LDIR + "?\\init.lua;" + LUA_CDIR + "?.lua;" + LUA_CDIR + "?\\init.lua";
public const string LUA_CPATH_DEFAULT = ".\\?.dll;" + LUA_CDIR + "?.dll;" + LUA_CDIR + "loadall.dll";
public const string LUA_DIRSEP = "\\";
public const string LUA_PATHSEP = ";";
public const string LUA_PATH_MARK = "?";
public const string LUA_EXECDIR = "!";
public const string LUA_IGMARK = "-";
public static string LUA_QL(string x)
{
return "'" + x + "'";
}
public const string LUA_QS = "'%s'";
public const int LUA_IDSIZE = 60;
public const int LUAI_GCPAUSE = 200;
public const int LUAI_GCMUL = 200;
public const int LUA_COMPAT_LSTR = 1;
public const int LUAI_BITSINT = 32;
public const int LUAI_MAXCALLS = 20000;
public const int LUAI_MAXCSTACK = 8000;
public const int LUAI_MAXCCALLS = 200;
public const int LUAI_MAXVARS = 200;
public const int LUAI_MAXUPVALUES = 60;
public const int LUAL_BUFFERSIZE = 512;
public const int LUA_MAXCAPTURES = 32;
// No lua_popen
// No lua_pclose
public const int LUAI_EXTRASPACE = 0;
public const string LUA_VERSION = "Lua 5.1";
public const string LUA_RELEASE = "Lua 5.1.5";
public const int LUA_VERSION_NUM = 501;
public const string LUA_COPYRIGHT = "Copyright (C) 1994-2012 Lua.org, PUC-Rio";
public const string LUA_AUTHORS = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes";
public const string LUA_SIGNATURE = "\x1bLua";
public const int LUA_MULTRET = -1;
public const int LUA_REGISTRYINDEX = -10000;
public const int LUA_ENVIRONINDEX = -10001;
public const int LUA_GLOBALSINDEX = -10002;
public static int lua_upvalueindex(int i)
{
return LUA_GLOBALSINDEX - i;
}
public const int LUA_YIELD = 1;
public const int LUA_ERRRUN = 2;
public const int LUA_ERRSYNTAX = 3;
public const int LUA_ERRMEM = 4;
public const int LUA_ERRERR = 5;
public const int LUA_TNONE = -1;
public const int LUA_TNIL = 0;
public const int LUA_TBOOLEAN = 1;
public const int LUA_TLIGHTUSERDATA = 2;
public const int LUA_TNUMBER = 3;
public const int LUA_TSTRING = 4;
public const int LUA_TTABLE = 5;
public const int LUA_TFUNCTION = 6;
public const int LUA_TUSERDATA = 7;
public const int LUA_TTHREAD = 8;
public const int LUA_MINSTACK = 20;
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_newstate")]
private static extern lua_State _lua_newstate(nint f, nuint ud);
public static lua_State lua_newstate(lua_Alloc? f, nuint ud)
{
return _lua_newstate(f == null ? 0 : Marshal.GetFunctionPointerForDelegate(f), ud);
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_close(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_State lua_newthread(lua_State L);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_atpanic")]
private static extern nint _lua_atpanic(lua_State L, nint panicf);
public static lua_CFunction? lua_atpanic(lua_State L, lua_CFunction? panicf)
{
nint panic = _lua_atpanic(L, panicf == null ? 0 : Marshal.GetFunctionPointerForDelegate(panicf));
return panic == 0 ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(panic);
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_gettop(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_settop(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushvalue(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_remove(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_insert(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_replace(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_checkstack(lua_State L, int sz);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_xmove(lua_State from, lua_State to, int n);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_isnumber(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_isstring(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_iscfunction(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_isuserdata(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_type(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_typename")]
private static extern nint _lua_typename(lua_State L, int tp);
public static string? lua_typename(lua_State L, int tp)
{
return Marshal.PtrToStringAnsi(_lua_typename(L, tp));
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_equal(lua_State L, int idx1, int idx2);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_rawequal(lua_State L, int idx1, int idx2);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_lessthan(lua_State L, int idx1, int idx2);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_Number lua_tonumber(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_Integer lua_tointegerx(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_toboolean(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_tolstring")]
private static extern nint _lua_tolstring(lua_State L, int idx, ref size_t len);
public static string? lua_tolstring(lua_State L, int idx, ref size_t len)
{
return Marshal.PtrToStringAnsi(_lua_tolstring(L, idx, ref len));
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern size_t lua_objlen(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_tocfunction")]
private static extern nint _lua_tocfunction(lua_State L, int idx);
public static lua_CFunction? lua_tocfunction(lua_State L, int idx)
{
nint ret = _lua_tocfunction(L, idx);
return ret == 0 ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(ret);
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern nuint lua_touserdata(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_State lua_tothread(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern nuint lua_topointer(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushnil(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushnumber(lua_State L, lua_Number n);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushinteger(lua_State L, lua_Integer n);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushlstring(lua_State L, string s, size_t l);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushstring(lua_State L, string s);
// TODO:
// [DllImport(DllName, CallingConvention = Convention)]
// public static extern nint lua_pushvfstring(lua_State L, string fmt, va_list argp);
// TODO:
// [DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_pushfstring")]
// private static extern nint _lua_pushfstring(lua_State L, string fmt, params string[] args);
// public static string? lua_pushfstring(lua_State L, string fmt, params string[] args)
// {
// return Marshal.PtrToStringAnsi(_lua_pushfstring(L, fmt, args));
// }
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_pushcclosure")]
private static extern void _lua_pushcclosure(lua_State L, nint fn, int n);
public static void lua_pushcclosure(lua_State L, lua_CFunction? fn, int n)
{
_lua_pushcclosure(L, fn == null ? 0 : Marshal.GetFunctionPointerForDelegate(fn), n);
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushboolean(lua_State L, int b);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_pushlightuserdata(lua_State L, nuint p);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_pushthread(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_gettable(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_getfield(lua_State L, int idx, string k);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_rawget(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_rawgeti(lua_State L, int idx, int n);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_createtable(lua_State L, int narr, int nrec);
[DllImport(DllName, CallingConvention = Convention)]
public static extern nuint lua_newuserdata(lua_State L, size_t sz);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_getmetatable(lua_State L, int objindex);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_getfenv(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_settable(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_setfield(lua_State L, int idx, string k);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_rawset(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_rawseti(lua_State L, int idx, int n);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_setmetatable(lua_State L, int objindex);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_setfenv(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_call(lua_State L, int nargs, int nresults);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_pcall(lua_State L, int nargs, int nresults, int errfunc);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_cpcall")]
private static extern int _lua_cpcall(lua_State L, nint func, nuint ud);
public static int lua_cpcall(lua_State L, lua_CFunction? func, nuint ud)
{
return _lua_cpcall(L, func == null ? 0 : Marshal.GetFunctionPointerForDelegate(func), ud);
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_load")]
private static extern int _lua_load(lua_State L, nint reader, nuint dt, string chunkname);
public static int lua_load(lua_State L, lua_Reader? reader, nuint dt, string chunkname)
{
return _lua_load(L, reader == null ? 0 : Marshal.GetFunctionPointerForDelegate(reader), dt, chunkname);
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_dump")]
private static extern int _lua_dump(lua_State L, nint writer, nuint data);
public static int lua_dump(lua_State L, lua_Writer? writer, nuint data)
{
return _lua_dump(L, writer == null ? 0 : Marshal.GetFunctionPointerForDelegate(writer), data);
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_yield(lua_State L, int nresults);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_resume(lua_State L, int narg);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_status(lua_State L);
public const int LUA_GCSTOP = 0;
public const int LUA_GCRESTART = 1;
public const int LUA_GCCOLLECT = 2;
public const int LUA_GCCOUNT = 3;
public const int LUA_GCCOUNTB = 4;
public const int LUA_GCSTEP = 5;
public const int LUA_GCSETPAUSE = 6;
public const int LUA_GCSETSTEPMUL = 7;
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_gc(lua_State L, int what, int data);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_error(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_next(lua_State L, int idx);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_concat(lua_State L, int n);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_Alloc lua_getallocf(lua_State L, out nuint ud);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setallocf")]
private static extern void _lua_setallocf(lua_State L, nint f, nuint ud);
public static void lua_setallocf(lua_State L, lua_Alloc? f, nuint ud)
{
_lua_setallocf(L, f == null ? 0 : Marshal.GetFunctionPointerForDelegate(f), ud);
}
public static void lua_pop(lua_State L, int n)
{
lua_settop(L, -n-1);
}
public static void lua_newtable(lua_State L)
{
lua_createtable(L, 0, 0);
}
public static void lua_register(lua_State L, string n, lua_CFunction? f)
{
lua_pushcfunction(L, f);
lua_setglobal(L, n);
}
public static void lua_pushcfunction(lua_State L, lua_CFunction? f)
{
lua_pushcclosure(L, f, 0);
}
public static size_t lua_strlen(lua_State L, int i)
{
return lua_objlen(L, i);
}
public static int lua_isfunction(lua_State L, int n)
{
return (lua_type(L, n) == LUA_TFUNCTION) ? 1 : 0;
}
public static int lua_istable(lua_State L, int n)
{
return (lua_type(L, n) == LUA_TTABLE) ? 1 : 0;
}
public static int lua_islightuserdata(lua_State L, int n)
{
return (lua_type(L, n) == LUA_TLIGHTUSERDATA) ? 1 : 0;
}
public static int lua_isnil(lua_State L, int n)
{
return (lua_type(L, n) == LUA_TNIL) ? 1 : 0;
}
public static int lua_isboolean(lua_State L, int n)
{
return (lua_type(L, n) == LUA_TBOOLEAN) ? 1 : 0;
}
public static int lua_isthread(lua_State L, int n)
{
return (lua_type(L, n) == LUA_TTHREAD) ? 1 : 0;
}
public static int lua_isnone(lua_State L, int n)
{
return (lua_type(L, n) == LUA_TNONE) ? 1 : 0;
}
public static int lua_isnoneornil(lua_State L, int n)
{
return (lua_type(L, n) <= 0) ? 1 : 0;
}
public static void lua_pushliteral(lua_State L, string s)
{
lua_pushlstring(L, s, (ulong) s.Length);
}
public static void lua_setglobal(lua_State L, string s)
{
lua_setfield(L, LUA_GLOBALSINDEX, s);
}
public static void lua_getglobal(lua_State L, string s)
{
lua_getfield(L, LUA_GLOBALSINDEX, s);
}
public static string? lua_tostring(lua_State L, int i)
{
ulong temp = 0; // NOP
return lua_tolstring(L, i, ref temp);
}
public static lua_State lua_open()
{
return luaL_newstate();
}
public static void lua_getregistry(lua_State L)
{
lua_pushvalue(L, LUA_REGISTRYINDEX);
}
public static int lua_getgccount(lua_State L)
{
return lua_gc(L, LUA_GCCOUNT, 0);
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern void lua_setlevel(lua_State from, lua_State to);
public const int LUA_HOOKCALL = 0;
public const int LUA_HOOKRET = 1;
public const int LUA_HOOKLINE = 2;
public const int LUA_HOOKCOUNT = 3;
public const int LUA_HOOKTAILRET = 4;
public const int LUA_MASKCALL = 1 << LUA_HOOKCALL;
public const int LUA_MASKRET = 1 << LUA_HOOKRET;
public const int LUA_MASKLINE = 1 << LUA_HOOKLINE;
public const int LUA_MASKCOUNT = 1 << LUA_HOOKCOUNT;
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_getstack(lua_State L, int level, lua_Debug ar);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_getinfo(lua_State L, string what, lua_Debug ar);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_getlocal")]
private static extern nint _lua_getlocal(lua_State L, lua_Debug ar, int n);
public static string? lua_getlocal(lua_State L, lua_Debug ar, int n)
{
return Marshal.PtrToStringAnsi(_lua_getlocal(L, ar, n));
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setlocal")]
private static extern nint _lua_setlocal(lua_State L, lua_Debug ar, int n);
public static string? lua_setlocal(lua_State L, lua_Debug ar, int n)
{
return Marshal.PtrToStringAnsi(_lua_setlocal(L, ar, n));
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_getupvalue")]
private static extern nint _lua_getupvalue(lua_State L, int funcindex, int n);
public static string? lua_getupvalue(lua_State L, int funcindex, int n)
{
return Marshal.PtrToStringAnsi(_lua_getupvalue(L, funcindex, n));
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setupvalue")]
private static extern nint _lua_setupvalue(lua_State L, int funcindex, int n);
public static string? lua_setupvalue(lua_State L, int funcindex, int n)
{
return Marshal.PtrToStringAnsi(_lua_setupvalue(L, funcindex, n));
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_sethook")]
private static extern int _lua_sethook(lua_State L, nint func, int mask, int count);
public static int lua_sethook(lua_State L, lua_Hook? func, int mask, int count)
{
return _lua_sethook(L, func == null ? 0 : Marshal.GetFunctionPointerForDelegate(func), mask, count);
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_gethook")]
private static extern nint _lua_gethook(lua_State L);
public static lua_Hook? lua_gethook(lua_State L)
{
nint ret = _lua_gethook(L);
return ret == 0 ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_gethookmask(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_gethookcount(lua_State L);
public static ulong luaL_getn(lua_State L, int i)
{
return lua_objlen(L, i);
}
public static void luaL_setn(lua_State L, int i, int j)
{
}
public const int LUA_ERRFILE = LUA_ERRERR + 1;
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_openlib(lua_State L, string libname, luaL_Reg[] l, int nup);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_register(lua_State L, string libname, luaL_Reg[] l);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_getmetafield(lua_State L, int obj, string e);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_callmeta(lua_State L, int obj, string e);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_typerror(lua_State L, int narg, string tname);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_argerror(lua_State L, int numarg, string extramsg);
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "luaL_checklstring")]
private static extern nint _luaL_checklstring(lua_State L, int numArg, ref size_t l);
public static string? luaL_checklstring(lua_State L, int numArg, ref size_t l)
{
return Marshal.PtrToStringAnsi(_luaL_checklstring(L, numArg, ref l));
}
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "luaL_optlstring")]
private static extern nint _luaL_optlstring(lua_State L, int numArg, string def, ref size_t l);
public static string? luaL_optlstring(lua_State L, int numArg, string def, ref size_t l)
{
return Marshal.PtrToStringAnsi(_luaL_optlstring(L, numArg, def, ref l));
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_Number luaL_checknumber(lua_State L, int numArg);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_Number luaL_optnumber(lua_State L, int nArg, lua_Number def);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_Integer luaL_checkinteger(lua_State L, int numArg);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_Integer luaL_optinteger(lua_State L, int nArg, lua_Integer def);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_checkstack(lua_State L, int sz, string msg);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_checktype(lua_State L, int narg, int t);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_checkany(lua_State L, int narg);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_newmetatable(lua_State L, string tname);
[DllImport(DllName, CallingConvention = Convention)]
public static extern nuint luaL_checkudata(lua_State L, int ud, string tname);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_where(lua_State L, int lvl);
// TODO: I dont think params works
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_error(lua_State L, string fmt, params string[] args);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_checkoption(lua_State L, int narg, string def, string[] lst);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_ref(lua_State L, int t);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_unref(lua_State L, int t, int _ref);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_loadfile(lua_State L, string filename);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_loadbuffer(lua_State L, string buff, size_t sz, string name);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaL_loadstring(lua_State L, string s);
[DllImport(DllName, CallingConvention = Convention)]
public static extern lua_State luaL_newstate();
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "luaL_gsub")]
private static extern nint _luaL_gsub(lua_State L, string s, string p, string r);
public static string? luaL_gsub(lua_State L, string s, string p, string r)
{
return Marshal.PtrToStringAnsi(_luaL_gsub(L, s, p, r));
}
[DllImport(DllName, CallingConvention= Convention, EntryPoint = "luaL_findtable")]
private static extern nint _luaL_findtable(lua_State L, int idx, string fname, int szhint);
public static string? luaL_findtable(lua_State L, int idx, string fname, int szhint)
{
return Marshal.PtrToStringAnsi(_luaL_findtable(L, idx, fname, szhint));
}
public static void luaL_argcheck(lua_State L, bool cond, int numarg, string extramsg)
{
if (cond == false)
luaL_argerror(L, numarg, extramsg);
}
public static string? luaL_checkstring(lua_State L, int n)
{
size_t temp = 0; // NOP
return luaL_checklstring(L, n, ref temp);
}
public static string? luaL_optstring(lua_State L, int n, string d)
{
size_t temp = 0; // NOP
return luaL_optlstring(L, n, d, ref temp);
}
public static int luaL_checkint(lua_State L, int n)
{
return (int) luaL_checkinteger(L, n);
}
public static int luaL_optint(lua_State L, int n, lua_Integer d)
{
return (int) luaL_optinteger(L, n, d);
}
public static long luaL_checklong(lua_State L, int n)
{
return luaL_checkinteger(L, n);
}
public static long luaL_optlong(lua_State L, int n, lua_Integer d)
{
return luaL_optinteger(L, n, d);
}
public static string? luaL_typename(lua_State L, int i)
{
return lua_typename(L, lua_type(L, i));
}
public static int luaL_dofile(lua_State L, string fn)
{
int status = luaL_loadfile(L, fn);
if (status > 0)
return status;
return lua_pcall(L, 0, LUA_MULTRET, 0);
}
public static int luaL_dostring(lua_State L, string s)
{
int status = luaL_loadstring(L, s);
if (status > 0)
return status;
return lua_pcall(L, 0, LUA_MULTRET, 0);
}
public static void luaL_getmetatable(lua_State L, string n)
{
lua_getfield(L, LUA_REGISTRYINDEX, n);
}
public delegate T luaL_Function<T>(lua_State L, int n);
public static T luaL_opt<T>(lua_State L, luaL_Function<T> f, int n, T d)
{
return lua_isnoneornil(L, n) > 0 ? d : f(L, n);
}
public static void luaL_addchar(luaL_Buffer B, byte c)
{
if (B.p >= B.buffer.Length + LUAL_BUFFERSIZE)
luaL_prepbuffer(B);
Marshal.WriteByte(B.p, c);
B.p += 1;
}
public static void luaL_putchar(luaL_Buffer B, byte c)
{
luaL_addchar(B, c);
}
public static void luaL_addsize(luaL_Buffer B, int n)
{
B.p += n;
}
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_buffinit(lua_State L, luaL_Buffer B);
[DllImport(DllName, CallingConvention = Convention)]
public static extern nint luaL_prepbuffer(luaL_Buffer B);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_addlstring(luaL_Buffer B, string s, size_t l);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_addstring(luaL_Buffer B, string s);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_addvalue(luaL_Buffer B);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_pushresult(luaL_Buffer B);
public const int LUA_NOREF = -2;
public const int LUA_REFNIL = -1;
public static int lua_ref(lua_State L, bool _lock)
{
if (_lock)
return luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushstring(L, "unlocked references are obsolete");
lua_error(L);
return 0;
}
public static void lua_unref(lua_State L, int _ref)
{
luaL_unref(L, LUA_REGISTRYINDEX, _ref);
}
public static void lua_getref(lua_State L, int _ref)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, _ref);
}
public const string LUA_FILEHANDLE = "FILE*";
public const string LUA_COLIBNAME = "coroutine";
public const string LUA_TABLIBNAME = "table";
public const string IOLIBNAME = "io";
public const string OSLIBNAME = "os";
public const string LUA_STRLIBNAME = "string";
public const string LUA_MATHLIBNAME = "math";
public const string DBLIBNAME = "debug";
public const string LOADLIBNAME = "package";
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_base(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_table(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_io(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_os(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_string(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_math(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_debug(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern int luaopen_package(lua_State L);
[DllImport(DllName, CallingConvention = Convention)]
public static extern void luaL_openlibs(lua_State L);
}