Skip to content

Commit 95e25c9

Browse files
committed
Did the following:
- Ensured all function delegates match NULL expectations in function arguments and returns - Converted a ref to an out as an identified output variable
1 parent 1c35c49 commit 95e25c9

File tree

5 files changed

+204
-71
lines changed

5 files changed

+204
-71
lines changed

src/Lua51.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ public static int lua_upvalueindex(int i)
142142

143143
public const int LUA_MINSTACK = 20;
144144

145-
[DllImport(DllName, CallingConvention = Convention)]
146-
public static extern lua_State lua_newstate(lua_Alloc f, voidp ud);
145+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_newstate")]
146+
public static extern lua_State _lua_newstate(charp f, voidp ud);
147+
public static lua_State lua_newstate(lua_Alloc? f, voidp ud)
148+
{
149+
return _lua_newstate(f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
150+
}
147151

148152
[DllImport(DllName, CallingConvention = Convention)]
149153
public static extern void lua_close(lua_State L);
@@ -348,11 +352,19 @@ public static int lua_cpcall(lua_State L, lua_CFunction? func, voidp ud)
348352
return _lua_cpcall(L, func == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(func), ud);
349353
}
350354

351-
[DllImport(DllName, CallingConvention = Convention)]
352-
public static extern int lua_load(lua_State L, lua_Reader reader, voidp dt, string chunkname);
355+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_load")]
356+
public static extern int _lua_load(lua_State L, charp reader, voidp dt, string chunkname);
357+
public static int lua_load(lua_State L, lua_Reader? reader, voidp dt, string chunkname)
358+
{
359+
return _lua_load(L, reader == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname);
360+
}
353361

354-
[DllImport(DllName, CallingConvention = Convention)]
355-
public static extern int lua_dump(lua_State L, lua_Writer writer, voidp data);
362+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_dump")]
363+
public static extern int _lua_dump(lua_State L, charp writer, voidp data);
364+
public static int lua_dump(lua_State L, lua_Writer? writer, voidp data)
365+
{
366+
return _lua_dump(L, writer == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data);
367+
}
356368

357369
[DllImport(DllName, CallingConvention = Convention)]
358370
public static extern int lua_yield(lua_State L, int nresults);
@@ -385,10 +397,14 @@ public static int lua_cpcall(lua_State L, lua_CFunction? func, voidp ud)
385397
public static extern void lua_concat(lua_State L, int n);
386398

387399
[DllImport(DllName, CallingConvention = Convention)]
388-
public static extern lua_Alloc lua_getallocf(lua_State L, ref voidp ud);
400+
public static extern lua_Alloc lua_getallocf(lua_State L, out voidp ud);
389401

390-
[DllImport(DllName, CallingConvention = Convention)]
391-
public static extern void lua_setallocf(lua_State L, lua_Alloc f, voidp ud);
402+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setallocf")]
403+
public static extern void _lua_setallocf(lua_State L, charp f, voidp ud);
404+
public static void lua_setallocf(lua_State L, lua_Alloc? f, voidp ud)
405+
{
406+
_lua_setallocf(L, f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
407+
}
392408

393409
public static void lua_pop(lua_State L, int n)
394410
{
@@ -540,11 +556,20 @@ public static int lua_getgccount(lua_State L)
540556
return Marshal.PtrToStringAnsi(_lua_setupvalue(L, funcindex, n));
541557
}
542558

543-
[DllImport(DllName, CallingConvention = Convention)]
544-
public static extern int lua_sethook(lua_State L, lua_Hook func, int mask, int count);
559+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_sethook")]
560+
public static extern int _lua_sethook(lua_State L, charp func, int mask, int count);
561+
public static int lua_sethook(lua_State L, lua_Hook? func, int mask, int count)
562+
{
563+
return _lua_sethook(L, func == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
564+
}
545565

546-
[DllImport(DllName, CallingConvention = Convention)]
547-
public static extern lua_Hook lua_gethook(lua_State L);
566+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_gethook")]
567+
public static extern charp _lua_gethook(lua_State L);
568+
public static lua_Hook? lua_gethook(lua_State L)
569+
{
570+
charp ret = _lua_gethook(L);
571+
return ret == IntPtr.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
572+
}
548573

549574
[DllImport(DllName, CallingConvention = Convention)]
550575
public static extern int lua_gethookmask(lua_State L);

src/Lua52.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,12 @@ public static int lua_upvalueindex(int i)
165165
public const int LUA_RIDX_GLOBALS = 2;
166166
public const int LUA_RIDX_LAST = LUA_RIDX_GLOBALS;
167167

168-
[DllImport(DllName, CallingConvention = Convention)]
169-
public static extern lua_State lua_newstate(lua_Alloc f, voidp ud);
168+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_newstate")]
169+
public static extern lua_State _lua_newstate(charp f, voidp ud);
170+
public static lua_State lua_newstate(lua_Alloc? f, voidp ud)
171+
{
172+
return _lua_newstate(f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
173+
}
170174

171175
[DllImport(DllName, CallingConvention = Convention)]
172176
public static extern void lua_close(lua_State L);
@@ -442,11 +446,19 @@ public static int lua_pcall(lua_State L, int n, int r, int f)
442446
return lua_pcallk(L, n, r, f, 0, null);
443447
}
444448

445-
[DllImport(DllName, CallingConvention = Convention)]
446-
public static extern int lua_load(lua_State L, lua_Reader reader, voidp dt, string chunkname, string? mode);
449+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_load")]
450+
public static extern int _lua_load(lua_State L, charp reader, voidp dt, string chunkname, string? mode);
451+
public static int lua_load(lua_State L, lua_Reader? reader, voidp dt, string chunkname, string? mode)
452+
{
453+
return _lua_load(L, reader == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname, mode);
454+
}
447455

448-
[DllImport(DllName, CallingConvention = Convention)]
449-
public static extern int lua_dump(lua_State L, lua_Writer writer, voidp data);
456+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_dump")]
457+
public static extern int _lua_dump(lua_State L, charp writer, voidp data);
458+
public static int lua_dump(lua_State L, lua_Writer? writer, voidp data)
459+
{
460+
return _lua_dump(L, writer == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data);
461+
}
450462

451463
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_yieldk")]
452464
public static extern int _lua_yieldk(lua_State L, int nresults, int ctx, charp k);
@@ -497,8 +509,12 @@ public static int lua_yield(lua_State L, int n)
497509
[DllImport(DllName, CallingConvention = Convention)]
498510
public static extern lua_Alloc lua_getallocf(lua_State L, ref voidp ud);
499511

500-
[DllImport(DllName, CallingConvention = Convention)]
501-
public static extern void lua_setallocf(lua_State L, lua_Alloc f, voidp ud);
512+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setallocf")]
513+
public static extern void _lua_setallocf(lua_State L, charp f, voidp ud);
514+
public static void lua_setallocf(lua_State L, lua_Alloc? f, voidp ud)
515+
{
516+
_lua_setallocf(L, f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
517+
}
502518

503519
public static double lua_tonumber(lua_State L, int i)
504520
{
@@ -646,11 +662,20 @@ public static void lua_pushglobaltable(lua_State L)
646662
[DllImport(DllName, CallingConvention = Convention)]
647663
public static extern void lua_upvaluejoin(lua_State L, int fidx1, int n1, int fidx2, int n2);
648664

649-
[DllImport(DllName, CallingConvention = Convention)]
650-
public static extern int lua_sethook(lua_State L, lua_Hook func, int mask, int count);
665+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_sethook")]
666+
public static extern int _lua_sethook(lua_State L, charp func, int mask, int count);
667+
public static int lua_sethook(lua_State L, lua_Hook? func, int mask, int count)
668+
{
669+
return _lua_sethook(L, func == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
670+
}
651671

652-
[DllImport(DllName, CallingConvention = Convention)]
653-
public static extern lua_Hook lua_gethook(lua_State L);
672+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_gethook")]
673+
public static extern charp _lua_gethook(lua_State L);
674+
public static lua_Hook? lua_gethook(lua_State L)
675+
{
676+
charp ret = _lua_gethook(L);
677+
return ret == IntPtr.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
678+
}
654679

655680
[DllImport(DllName, CallingConvention = Convention)]
656681
public static extern int lua_gethookmask(lua_State L);
@@ -798,7 +823,7 @@ public static int luaL_loadfile(lua_State L, string f)
798823
[DllImport(DllName, CallingConvention = Convention)]
799824
public static extern void luaL_traceback(lua_State L, lua_State L1, string msg, int level);
800825

801-
[DllImport(DllName, CallingConvention = Convention)]
826+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "luaL_requiref")]
802827
public static extern void _luaL_requiref(lua_State L, string modname, charp openf, int glb);
803828
public static void luaL_requiref(lua_State L, string modname, lua_CFunction? openf, int glb)
804829
{

src/Lua53.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ public static int lua_upvalueindex(int i)
160160
public const int LUA_RIDX_GLOBALS = 2;
161161
public const int LUA_RIDX_LAST = LUA_RIDX_GLOBALS;
162162

163-
[DllImport(DllName, CallingConvention = Convention)]
164-
public static extern lua_State lua_newstate(lua_Alloc f, voidp ud);
163+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_newstate")]
164+
public static extern lua_State _lua_newstate(charp f, voidp ud);
165+
public static lua_State lua_newstate(lua_Alloc? f, voidp ud)
166+
{
167+
return _lua_newstate(f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
168+
}
165169

166170
[DllImport(DllName, CallingConvention = Convention)]
167171
public static extern void lua_close(lua_State L);
@@ -438,11 +442,19 @@ public static int lua_pcall(lua_State L, int n, int r, int f)
438442
return lua_pcallk(L, n, r, f, null, null);
439443
}
440444

441-
[DllImport(DllName, CallingConvention = Convention)]
442-
public static extern int lua_load(lua_State L, lua_Reader reader, voidp dt, string chunkname, string? mode);
445+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_load")]
446+
public static extern int _lua_load(lua_State L, charp reader, voidp dt, string chunkname, string? mode);
447+
public static int lua_load(lua_State L, lua_Reader? reader, voidp dt, string chunkname, string? mode)
448+
{
449+
return _lua_load(L, reader == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname, mode);
450+
}
443451

444-
[DllImport(DllName, CallingConvention = Convention)]
445-
public static extern int lua_dump(lua_State L, lua_Writer writer, voidp data, int strip);
452+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_dump")]
453+
public static extern int _lua_dump(lua_State L, charp writer, voidp data, int strip);
454+
public static int lua_dump(lua_State L, lua_Writer? writer, voidp data, int strip)
455+
{
456+
return _lua_dump(L, writer == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data, strip);
457+
}
446458

447459
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_yieldk")]
448460
public static extern int _lua_yieldk(lua_State L, int nresults, charp ctx, charp k);
@@ -494,10 +506,14 @@ public static int lua_yield(lua_State L, int n)
494506
public static extern size_t lua_stringtonumber(lua_State L, string s);
495507

496508
[DllImport(DllName, CallingConvention = Convention)]
497-
public static extern lua_Alloc lua_getallocf(lua_State L, ref voidp ud);
509+
public static extern lua_Alloc lua_getallocf(lua_State L, out voidp ud);
498510

499-
[DllImport(DllName, CallingConvention = Convention)]
500-
public static extern void lua_setallocf(lua_State L, lua_Alloc f, voidp ud);
511+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setallocf")]
512+
public static extern void _lua_setallocf(lua_State L, charp f, voidp ud);
513+
public static void lua_setallocf(lua_State L, lua_Alloc? f, voidp ud)
514+
{
515+
_lua_setallocf(L, f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
516+
}
501517

502518
public static voidp lua_getextraspace(lua_State L)
503519
{
@@ -661,11 +677,20 @@ public static void lua_replace(lua_State L, int idx)
661677
[DllImport(DllName, CallingConvention = Convention)]
662678
public static extern void lua_upvaluejoin(lua_State L, int fidx1, int n1, int fidx2, int n2);
663679

664-
[DllImport(DllName, CallingConvention = Convention)]
665-
public static extern void lua_sethook(lua_State L, lua_Hook func, int mask, int count);
680+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_sethook")]
681+
public static extern void _lua_sethook(lua_State L, charp func, int mask, int count);
682+
public static void lua_sethook(lua_State L, lua_Hook? func, int mask, int count)
683+
{
684+
_lua_sethook(L, func == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
685+
}
666686

667-
[DllImport(DllName, CallingConvention = Convention)]
668-
public static extern lua_Hook lua_gethook(lua_State L);
687+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_gethook")]
688+
public static extern charp _lua_gethook(lua_State L);
689+
public static lua_Hook? lua_gethook(lua_State L)
690+
{
691+
charp ret = _lua_gethook(L);
692+
return ret == IntPtr.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
693+
}
669694

670695
[DllImport(DllName, CallingConvention = Convention)]
671696
public static extern int lua_gethookmask(lua_State L);
@@ -812,7 +837,7 @@ public static int luaL_loadfile(lua_State L, string f)
812837
[DllImport(DllName, CallingConvention = Convention)]
813838
public static extern void luaL_traceback(lua_State L, lua_State L1, string msg, int level);
814839

815-
[DllImport(DllName, CallingConvention = Convention)]
840+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "luaL_requiref")]
816841
public static extern void _luaL_requiref(lua_State L, string modname, charp openf, int glb);
817842
public static void luaL_requiref(lua_State L, string modname, lua_CFunction? openf, int glb)
818843
{

src/Lua54.cs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ public static int lua_upvalueindex(int i)
167167
public const int LUA_RIDX_GLOBALS = 2;
168168
public const int LUA_RIDX_LAST = LUA_RIDX_GLOBALS;
169169

170-
[DllImport(DllName, CallingConvention = Convention)]
171-
public static extern lua_State lua_newstate(lua_Alloc f, voidp ud);
170+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_newstate")]
171+
public static extern lua_State _lua_newstate(charp f, voidp ud);
172+
public static lua_State lua_newstate(lua_Alloc? f, voidp ud)
173+
{
174+
return _lua_newstate(f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
175+
}
172176

173177
[DllImport(DllName, CallingConvention = Convention)]
174178
public static extern void lua_close(lua_State L);
@@ -438,11 +442,19 @@ public static int lua_pcall(lua_State L, int n, int r, int f)
438442
return lua_pcallk(L, n, r, f, null, null);
439443
}
440444

441-
[DllImport(DllName, CallingConvention = Convention)]
442-
public static extern int lua_load(lua_State L, lua_Reader reader, voidp dt, string chunkname, string? mode);
445+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_load")]
446+
public static extern int _lua_load(lua_State L, charp reader, voidp dt, string chunkname, string? mode);
447+
public static int lua_load(lua_State L, lua_Reader? reader, voidp dt, string chunkname, string? mode)
448+
{
449+
return _lua_load(L, reader == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname, mode);
450+
}
443451

444-
[DllImport(DllName, CallingConvention = Convention)]
445-
public static extern int lua_dump(lua_State L, lua_Writer writer, voidp data, int strip);
452+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_dump")]
453+
public static extern int _lua_dump(lua_State L, charp writer, voidp data, int strip);
454+
public static int lua_dump(lua_State L, lua_Writer? writer, voidp data, int strip)
455+
{
456+
return _lua_dump(L, writer == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data, strip);
457+
}
446458

447459
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_yieldk")]
448460
public static extern int _lua_yieldk(lua_State L, int nresults, charp ctx, charp k);
@@ -465,8 +477,12 @@ public static int lua_yield(lua_State L, int n)
465477
return lua_yieldk(L, n, null, null);
466478
}
467479

468-
[DllImport(DllName, CallingConvention = Convention)]
469-
public static extern void lua_setwarnf(lua_State L, lua_WarnFunction f, voidp ud);
480+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setwarnf")]
481+
public static extern void _lua_setwarnf(lua_State L, charp f, voidp ud);
482+
public static void lua_setwarnf(lua_State L, lua_WarnFunction? f, voidp ud)
483+
{
484+
_lua_setwarnf(L, f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_WarnFunction>(f), ud);
485+
}
470486

471487
[DllImport(DllName, CallingConvention = Convention)]
472488
public static extern void lua_warning(lua_State L, string msg, int tocont);
@@ -503,10 +519,14 @@ public static int lua_yield(lua_State L, int n)
503519
public static extern size_t lua_stringtonumber(lua_State L, string s);
504520

505521
[DllImport(DllName, CallingConvention = Convention)]
506-
public static extern lua_Alloc lua_getallocf(lua_State L, ref voidp ud);
522+
public static extern lua_Alloc lua_getallocf(lua_State L, out voidp ud);
507523

508-
[DllImport(DllName, CallingConvention = Convention)]
509-
public static extern void lua_setallocf(lua_State L, lua_Alloc f, voidp ud);
524+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_setallocf")]
525+
public static extern void _lua_setallocf(lua_State L, charp f, voidp ud);
526+
public static void lua_setallocf(lua_State L, lua_Alloc? f, voidp ud)
527+
{
528+
_lua_setallocf(L, f == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
529+
}
510530

511531
[DllImport(DllName, CallingConvention = Convention)]
512532
public static extern void lua_toclose(lua_State L, int idx);
@@ -693,11 +713,20 @@ public static int lua_setuservalue(lua_State L, int idx)
693713
[DllImport(DllName, CallingConvention = Convention)]
694714
public static extern void lua_upvaluejoin(lua_State L, int fidx1, int n1, int fidx2, int n2);
695715

696-
[DllImport(DllName, CallingConvention = Convention)]
697-
public static extern void lua_sethook(lua_State L, lua_Hook func, int mask, int count);
716+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_sethook")]
717+
public static extern void _lua_sethook(lua_State L, charp func, int mask, int count);
718+
public static void lua_sethook(lua_State L, lua_Hook? func, int mask, int count)
719+
{
720+
_lua_sethook(L, func == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
721+
}
698722

699-
[DllImport(DllName, CallingConvention = Convention)]
700-
public static extern lua_Hook lua_gethook(lua_State L);
723+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_gethook")]
724+
public static extern charp _lua_gethook(lua_State L);
725+
public static lua_Hook? lua_gethook(lua_State L)
726+
{
727+
charp ret = _lua_gethook(L);
728+
return ret == IntPtr.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
729+
}
701730

702731
[DllImport(DllName, CallingConvention = Convention)]
703732
public static extern int lua_gethookmask(lua_State L);
@@ -855,7 +884,7 @@ public static int luaL_loadfile(lua_State L, string f)
855884
[DllImport(DllName, CallingConvention = Convention)]
856885
public static extern void luaL_traceback(lua_State L, lua_State L1, string msg, int level);
857886

858-
[DllImport(DllName, CallingConvention = Convention)]
887+
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "luaL_requiref")]
859888
public static extern void _luaL_requiref(lua_State L, string modname, charp openf, int glb);
860889
public static void luaL_requiref(lua_State L, string modname, lua_CFunction? openf, int glb)
861890
{

0 commit comments

Comments
 (0)