Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Skip rarely used Windows API functions
- #define WIN32_LEAN_AND_MEAN
- // Headers
- #include <GMLuaModule.h>
- // Module definition
- GMOD_MODULE( Init, Shutdown );
- // Globals
- ILuaInterface* g_Lua;
- // Typedefs
- typedef unsigned int uint;
- typedef unsigned char byte;
- // Functions
- LUA_FUNCTION( GMAllocate ) {
- // Check argument types
- g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
- // Read size
- uint size = uint( g_Lua->GetInteger( 1 ) );
- // Allocate it
- byte* ptrMem = new byte[ size ];
- // Push the result
- g_Lua->Push( float( int( ptrMem ) ) );
- // We're done
- return 1;
- }
- LUA_FUNCTION( GMDeallocate ) {
- // Check argument types
- g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
- // Read pointer
- byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
- // Deallocate
- delete [] ptrMem;
- // We're done
- return 0;
- }
- LUA_FUNCTION( GMSet ) {
- // Check argument types
- g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
- g_Lua->CheckType( 2, GLua::TYPE_NUMBER );
- g_Lua->CheckType( 3, GLua::TYPE_NUMBER );
- // Read pointer
- byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
- // Read size and value
- byte val = byte( g_Lua->GetInteger( 2 ) );
- uint size = uint( g_Lua->GetInteger( 3 ) );
- // Set
- memset( ptrMem, val, size );
- // We're done
- return 0;
- }
- LUA_FUNCTION( GMCopy ) {
- // Check argument types
- g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
- g_Lua->CheckType( 2, GLua::TYPE_NUMBER );
- g_Lua->CheckType( 3, GLua::TYPE_NUMBER );
- // Read pointers
- byte* ptrMemSrc = (byte*)g_Lua->GetInteger( 1 );
- byte* ptrMemDst = (byte*)g_Lua->GetInteger( 2 );
- // Read size
- uint size = uint( g_Lua->GetInteger( 3 ) );
- // Copy
- memcpy( ptrMemDst, ptrMemSrc, size );
- // We're done
- return 0;
- }
- LUA_FUNCTION( GMSetByte ) {
- // Check argument types
- g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
- g_Lua->CheckType( 2, GLua::TYPE_NUMBER );
- // Read pointer
- byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
- // Read size and value
- byte val = byte( g_Lua->GetInteger( 2 ) );
- // Set
- memset( ptrMem, val, sizeof( byte ) );
- // We're done
- return 0;
- }
- LUA_FUNCTION( GMGetByte ) {
- // Check argument types
- g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
- // Read pointer
- byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
- // Push the result
- g_Lua->Push( float( *ptrMem ) );
- // We're done
- return 1;
- }
- // Initialization
- int Init( lua_State *L )
- {
- // Get the lua instance
- g_Lua = Lua();
- // Register module
- g_Lua->NewGlobalTable( "heap" );
- ILuaObject* module = g_Lua->GetGlobal( "heap" );
- // Register functions
- module->SetMember( "Allocate", GMAllocate );
- module->SetMember( "Deallocate", GMDeallocate );
- module->SetMember( "Set", GMSet );
- module->SetMember( "SetByte", GMSetByte );
- module->SetMember( "GetByte", GMGetByte );
- module->SetMember( "Copy", GMCopy );
- // Clean up
- module->UnReference();
- // Why hello there
- g_Lua->Msg( "[HEAP] Module loaded!\n" );
- // Return
- return 0;
- }
- // Shutdown
- int Shutdown( lua_State *L )
- {
- // Good bye world
- g_Lua->Msg( "[HEAP] Module unloaded!\n" );
- // Return
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement