Click here to Skip to main content
12,350,597 members (22,956 online)
Click here to Skip to main content

Stats

222.3K views
7K downloads
121 bookmarked
Posted

Integrating Lua into C++

, 2 Sep 2005 CPOL
An article about embedding the Lua scripting language with C++ objects.
// ---------------------------------------------------------------------------
// FILE NAME            : LuaDebugger.cpp
// ---------------------------------------------------------------------------
// DESCRIPTION :
//
// Simple debugging routines
// 
// ---------------------------------------------------------------------------
// VERSION              : 1.00
// DATE                 : 1-Sep-2005
// AUTHOR               : Richard Shephard
// ---------------------------------------------------------------------------
// LIBRARY INCLUDE FILES
#include "luadebugger.h"
// ---------------------------------------------------------------------------

// typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
static void LuaHookCall (lua_State *lua)
{
   
   printf ("---- Call Stack ----\n");
//   printf ("[Level] [Function] [# args] [@line] [src]\n");

   lua_Debug ar;
   
   // Look at call stack
   for (int iLevel = 0; lua_getstack (lua, iLevel, &ar) != 0; ++iLevel)
   {
      if (lua_getinfo (lua, "Snlu", &ar) != 0)
      {
         printf ("%d %s %s %d @%d %s\n", iLevel, ar.namewhat, ar.name, ar.nups, ar.linedefined, ar.short_src);
      }
   }
}

static void LuaHookRet (lua_State *lua)
{
   
}

static void LuaHookLine (lua_State *lua)
{
   lua_Debug ar;
   lua_getstack (lua, 0, &ar);
   

   if (lua_getinfo (lua, "Sl", &ar) != 0)
   {
      printf ("exe %s on line %d\n", ar.short_src, ar.currentline);
   }
}

static void LuaHookCount (lua_State *lua)
{
   LuaHookLine (lua);
}

static void LuaHook (lua_State *lua, lua_Debug *ar)
{
   // Handover to the correct hook
   switch (ar->event)
   {
   case LUA_HOOKCALL:
      LuaHookCall (lua);
      break;
   case LUA_HOOKRET:
   case LUA_HOOKTAILRET:
      LuaHookRet (lua);
      break;
   case LUA_HOOKLINE:
      LuaHookLine (lua);
      break;
   case LUA_HOOKCOUNT:
      LuaHookCount (lua);
      break;
   }
}

CLuaDebugger::CLuaDebugger (CLuaVirtualMachine& vm) : m_iCountMask (10), m_vm (vm)
{
   // Clear all current hooks
   if (vm.Ok ())
   {
      vm.AttachDebugger (this);
      lua_sethook ((lua_State *) vm, LuaHook, 0, m_iCountMask);
   }
}

CLuaDebugger::~CLuaDebugger (void)
{
   // Clear all current hooks
   if (m_vm.Ok ())
   {
      lua_sethook ((lua_State *) m_vm, LuaHook, 0, m_iCountMask);
   }
}

void CLuaDebugger::SetHooksFlag (int iMask)
{
   // Set hooks
   lua_sethook ((lua_State *) m_vm, LuaHook, iMask, m_iCountMask);
}

void CLuaDebugger::ErrorRun (int iErrorCode)
{
   switch (iErrorCode)
   {
   case LUA_ERRRUN:
      printf ("LUA_ERRRUN\n");
      break;
   case LUA_ERRMEM:
      printf ("LUA_ERRMEM\n");
      break;
   case LUA_ERRERR:
      printf ("LUA_ERRERR\n");
      break;
   }

   // Get the error string that appears on top of stack when a function
   // fails to run
   printf ("Error: %s\n", lua_tostring ((lua_State *) m_vm, -1));
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RichardS
Software Developer (Senior)
South Africa South Africa
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 2 Sep 2005
Article Copyright 2005 by RichardS
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid