Beispiel #1
0
bool cPlugin_NewLua::OnPlayerLeftClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_LEFT_CLICK);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_Status);

	if (!CallFunction(6, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #2
0
bool cPlugin_NewLua::OnUpdatedSign(
	cWorld * a_World, 
	int a_BlockX, int a_BlockY, int a_BlockZ, 
	const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4,
	cPlayer * a_Player
)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_UPDATED_SIGN);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushstring  (m_LuaState, a_Line1.c_str());
	tolua_pushstring  (m_LuaState, a_Line2.c_str());
	tolua_pushstring  (m_LuaState, a_Line3.c_str());
	tolua_pushstring  (m_LuaState, a_Line4.c_str());
	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");

	if (!CallFunction(9, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #3
0
bool cPlugin_NewLua::OnPlayerUsedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_USED_BLOCK);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);

	if (!CallFunction(10, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #4
0
bool cPlugin_NewLua::OnBlockToPickups(cWorld * a_World, cEntity * a_Digger, int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, cItems & a_Pickups)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_BLOCK_TO_PICKUPS);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_World, "cWorld");
	tolua_pushusertype(m_LuaState, a_Digger, "cEntity");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);
	tolua_pushusertype(m_LuaState, &a_Pickups, "cItems");

	if (!CallFunction(8, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #5
0
bool cPlugin_NewLua::OnChat(cPlayer * a_Player, AString & a_Message)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHAT);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
	tolua_pushstring  (m_LuaState, a_Message.c_str());

	if (!CallFunction(2, 2, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -2, 0) > 0);
	if (lua_isstring(m_LuaState, -1))
	{
		a_Message = tolua_tostring(m_LuaState, -1, "");
	}
	lua_pop(m_LuaState, 2);
	return bRetVal;
}
Beispiel #6
0
bool cPlugin_NewLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGED);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_NewWeather);

	if (!CallFunction(2, 2, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	if (lua_isnumber(m_LuaState, -2))
	{
		a_NewWeather = (eWeather)lua_tointeger(m_LuaState, -2);
	}
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #7
0
bool cPlugin_NewLua::OnExecuteCommand(cPlayer * a_Player, const AStringVector & a_Split)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_EXECUTE_COMMAND);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");

	// Push the split:
	lua_createtable(m_LuaState, a_Split.size(), 0);
	int newTable = lua_gettop(m_LuaState);
	int index = 1;
	std::vector<std::string>::const_iterator iter = a_Split.begin(), end = a_Split.end();
	while(iter != end)
	{
		tolua_pushstring(m_LuaState, (*iter).c_str());
		lua_rawseti(m_LuaState, newTable, index);
		++iter;
		++index;
	}

	if (!CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #8
0
int cLuaState::CallFunctionWithForeignParams(
	const AString & a_FunctionName,
	cLuaState & a_SrcLuaState,
	int a_SrcParamStart,
	int a_SrcParamEnd
)
{
	ASSERT(IsValid());
	ASSERT(a_SrcLuaState.IsValid());

	// Store the stack position before any changes
	int OldTop = lua_gettop(m_LuaState);

	// Push the function to call, including the error handler:
	if (!PushFunction(a_FunctionName.c_str()))
	{
		LOGWARNING("Function '%s' not found", a_FunctionName.c_str());
		lua_settop(m_LuaState, OldTop);
		return -1;
	}

	// Copy the function parameters to the target state
	if (CopyStackFrom(a_SrcLuaState, a_SrcParamStart, a_SrcParamEnd) < 0)
	{
		// Something went wrong, fix the stack and exit
		lua_settop(m_LuaState, OldTop);
		m_NumCurrentFunctionArgs = -1;
		m_CurrentFunctionName.clear();
		return -1;
	}

	// Call the function, with an error handler:
	int s = lua_pcall(m_LuaState, a_SrcParamEnd - a_SrcParamStart + 1, LUA_MULTRET, OldTop + 1);
	if (ReportErrors(s))
	{
		LOGWARN("Error while calling function '%s' in '%s'", a_FunctionName.c_str(), m_SubsystemName.c_str());
		// Reset the stack:
		lua_settop(m_LuaState, OldTop);

		// Reset the internal checking mechanisms:
		m_NumCurrentFunctionArgs = -1;
		m_CurrentFunctionName.clear();

		// Make Lua think everything is okay and return 0 values, so that plugins continue executing.
		// The failure is indicated by the zero return values.
		return 0;
	}

	// Reset the internal checking mechanisms:
	m_NumCurrentFunctionArgs = -1;
	m_CurrentFunctionName.clear();

	// Remove the error handler from the stack:
	lua_remove(m_LuaState, OldTop + 1);

	// Return the number of return values:
	return lua_gettop(m_LuaState) - OldTop;
}
Beispiel #9
0
void cPlugin_NewLua::OnDisable()
{
	cCSLock Lock(m_CriticalSection);
	if (!PushFunction("OnDisable", false)) // false = don't log error if not found
	{
		return;
	}

	CallFunction(0, 0, "OnDisable");
}
Beispiel #10
0
void cPlugin_NewLua::Tick(float a_Dt)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_TICK);
	if (!PushFunction(FnName))
	{
		return;
	}

	tolua_pushnumber( m_LuaState, a_Dt );

	CallFunction(1, 0, FnName);
}
Beispiel #11
0
bool cPlugin_NewLua::OnUpdatingSign(
	cWorld * a_World, 
	int a_BlockX, int a_BlockY, int a_BlockZ, 
	AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4,
	cPlayer * a_Player
)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_UPDATING_SIGN);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushstring  (m_LuaState, a_Line1.c_str());
	tolua_pushstring  (m_LuaState, a_Line2.c_str());
	tolua_pushstring  (m_LuaState, a_Line3.c_str());
	tolua_pushstring  (m_LuaState, a_Line4.c_str());
	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");

	if (!CallFunction(9, 5, "OnUpdatingSign"))
	{
		return false;
	}

	
	bool bRetVal = (tolua_toboolean(m_LuaState, -5, 0) > 0);
	if (lua_isstring(m_LuaState, -4))
	{
		a_Line1 = tolua_tostring(m_LuaState, -4, "");
	}
	if (lua_isstring(m_LuaState, -3))
	{
		a_Line2 = tolua_tostring(m_LuaState, -3, "");
	}
	if (lua_isstring(m_LuaState, -2))
	{
		a_Line3 = tolua_tostring(m_LuaState, -2, "");
	}
	if (lua_isstring(m_LuaState, -1))
	{
		a_Line4 = tolua_tostring(m_LuaState, -1, "");
	}
	return bRetVal;
}
Beispiel #12
0
Function* Function_Create(lua_State* L, Function* parent)
{

    Function* function = static_cast<Function*>( Gc_AllocateObject( L, LUA_TFUNCTIONP, sizeof(Function) ) );
    Gc* gc = &L->gc;

    if (parent != NULL)
    {
        Gc_IncrementReference(gc, function, parent);
    }
    function->parent            = parent;

    function->parser            = NULL;

    function->numRegisters      = 0;
    function->maxStackSize      = 0;
    function->numParams         = 0;
    function->varArg            = false;

    function->numConstants      = 0;
    function->constants         = NULL;

    function->code              = NULL;
    function->codeSize          = 0;
    function->maxCodeSize       = 0;

    function->sourceLine        = NULL;
    function->maxSourceLines    = 0;
  
    function->numLocals         = 0;
    function->numCommitedLocals = 0;
    function->numUpValues       = 0;

    function->function          = NULL;
    function->numFunctions      = 0;
    function->maxFunctions      = 0;

    // It's possible for creating the table to trigger garbage collection, so
    // make sure there's a reference to our function on the stack before we do
    // that.

    PushFunction(L, function);
    function->constants = Table_Create(L, 0, 0);
    Gc_IncrementReference(&L->gc, function, function->constants);
    Pop(L, 1);

    return function;

}
Beispiel #13
0
bool cPlugin_NewLua::OnWeatherChanged(cWorld & a_World)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGED);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_World, "cWorld");

	if (!CallFunction(1, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #14
0
bool cPlugin_NewLua::OnPlayerTossingItem(cPlayer & a_Player)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_TOSSING_ITEM);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");

	if (!CallFunction(1, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #15
0
bool cPlugin_NewLua::OnHandshake(cClientHandle * a_Client, const AString & a_Username)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_HANDSHAKE);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Client, "cClientHandle");
	tolua_pushstring  (m_LuaState, a_Username.c_str());

	if (!CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #16
0
bool cPlugin_NewLua::OnPlayerRightClickingEntity(cPlayer & a_Player, cEntity & a_Entity)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, &a_Entity, "cEntity");

	if (!CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #17
0
bool cPlugin_NewLua::OnKilling(cEntity & a_Victim, cEntity * a_Killer)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_KILLING);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Victim, "cEntity");
	tolua_pushusertype(m_LuaState, a_Killer,  "cEntity");

	if (!CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #18
0
bool cPlugin_NewLua::OnCollectingPickup(cPlayer * a_Player, cPickup * a_Pickup)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_COLLECTING_PICKUP);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, a_Pickup, "cPickup");

	if (!CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #19
0
bool cPlugin_NewLua::OnDisconnect(cPlayer * a_Player, const AString & a_Reason)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_DISCONNECT);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
	tolua_pushstring  (m_LuaState, a_Reason.c_str());

	if (!CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #20
0
bool cPlugin_NewLua::OnTakeDamage(cEntity & a_Receiver, TakeDamageInfo & a_TDI)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_TAKE_DAMAGE);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Receiver, "cEntity");
	tolua_pushusertype(m_LuaState, &a_TDI,      "TakeDamageInfo");

	if (!CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) != 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #21
0
bool cPlugin_NewLua::OnChunkUnloading(cWorld * a_World, int a_ChunkX, int a_ChunkZ)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADING);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}
	
	tolua_pushusertype(m_LuaState, a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_ChunkX);
	tolua_pushnumber  (m_LuaState, a_ChunkZ);
	
	if (!CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #22
0
bool cPlugin_NewLua::OnCraftingNoRecipe(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CRAFTING_NO_RECIPE);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, (void *)a_Grid,   "cCraftingGrid");
	tolua_pushusertype(m_LuaState, (void *)a_Recipe, "cCraftingRecipe");

	if (!CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
Beispiel #23
0
bool cPlugin_NewLua::OnLogin(cClientHandle * a_Client, int a_ProtocolVersion, const AString & a_Username)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_LOGIN);
	ASSERT(FnName != NULL);
	if (!PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype (m_LuaState, a_Client, "cClientHandle");
	tolua_pushnumber   (m_LuaState, a_ProtocolVersion);
	tolua_pushcppstring(m_LuaState, a_Username);

	if (!CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}
int QueueFlipScreen(void *window){
  assert(window);
  PushFunction(FlipScreen, window);
  return 0;
}
int QueueLine(void *arg){
  PushFunction(Line, arg);
  return 0;
}
Beispiel #26
0
bool gmCodeGenPrivate::GenExprFunction(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
{
  GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_FUNCTION);

  gmptr id = m_hooks->GetFunctionId();
  a_byteCode->EmitPtr(BC_PUSHFN, id);

  // Create the function
  PushFunction();

  // Get a debug function name as the name of the variable the function is assigned to
  if(m_debug && a_node->m_parent && a_node->m_parent->m_type == CTNT_EXPRESSION && a_node->m_parent->m_subType == CTNET_OPERATION &&
     (a_node->m_parent->m_subTypeType == CTNOT_ASSIGN || a_node->m_parent->m_subTypeType == CTNOT_ASSIGN_FIELD) && a_node->m_parent->m_children[1] == a_node)
  {
    const gmCodeTreeNode * debugName = a_node->m_parent->m_children[0];
    if(debugName && debugName->m_type == CTNT_EXPRESSION && debugName->m_subType == CTNET_IDENTIFIER)
    {
    }
    else if(debugName->m_type == CTNT_EXPRESSION && debugName->m_subType == CTNET_OPERATION && 
            debugName->m_subTypeType == CTNOT_DOT)
    {
      debugName = debugName->m_children[1];
    }
    else
    {
      debugName = NULL;
    }

    if(debugName)
    {
      GM_ASSERT(debugName->m_type == CTNT_EXPRESSION && debugName->m_subType == CTNET_IDENTIFIER);
      m_currentFunction->m_debugName = debugName->m_data.m_string;
    }
  }

  // Parameters
  const gmCodeTreeNode * params = a_node->m_children[0];
  int numParams = 0;
  while(params)
  {
    const gmCodeTreeNode * param = params->m_children[0];
    GM_ASSERT(param->m_type == CTNT_EXPRESSION && param->m_subType == CTNET_IDENTIFIER);
    if(m_currentFunction->SetVariableType(param->m_data.m_string, CTVT_LOCAL) != numParams)
    {
      if(m_log) m_log->LogEntry("error (%d) parameter %s already declared", param->m_lineNumber, param->m_data.m_string);
      PopFunction();
      return false;
    }
    ++numParams;
    params = params->m_sibling;
  }

  // Generate the code
  bool res = Generate(a_node->m_children[1], &m_currentFunction->m_byteCode);

  // Generate a return incase the function didnt have one.
  m_currentFunction->m_byteCode.Emit(BC_RET);

  if(res)
  {
    // Create a locals table
    const char ** locals = NULL;
    if(m_debug)
    {
      locals = (const char **) alloca(sizeof(const char *) * m_currentFunction->m_numLocals);
      memset(locals, 0, sizeof(const char *) * m_currentFunction->m_numLocals);

      for(gmuint v = 0; v < m_currentFunction->m_variables.Count(); ++v)
      {
        Variable &variable = m_currentFunction->m_variables[v];
        if(variable.m_offset != -1)
        {
          locals[variable.m_offset] = variable.m_symbol;
        }
      }
    }

    // Add the function to the hooks.

    gmSortDebugLines(m_currentFunction->m_lineInfo);
    
    gmFunctionInfo info;
    info.m_id = id;
    info.m_root = false;
    info.m_byteCode = m_currentFunction->m_byteCode.GetData();
    info.m_byteCodeLength = m_currentFunction->m_byteCode.Tell();
    info.m_numParams = numParams;
    info.m_numLocals = m_currentFunction->m_numLocals - numParams;
    info.m_symbols = locals;
    info.m_maxStackSize = m_currentFunction->m_byteCode.GetMaxTos();
    info.m_lineInfoCount = m_currentFunction->m_lineInfo.Count();
    info.m_lineInfo = m_currentFunction->m_lineInfo.GetData();
    info.m_debugName = m_currentFunction->m_debugName;
    m_hooks->AddFunction(info);

    //gmByteCodePrint(stdout, info.m_byteCode, info.m_byteCodeLength);
  }

  PopFunction();
  
  return res;
}
Beispiel #27
0
int gmCodeGenPrivate::Lock(const gmCodeTreeNode * a_codeTree, gmCodeGenHooks * a_hooks, bool a_debug, gmLog * a_log)
{
  if(m_locked == true) return 1;

  // set up members
  m_errors = 0;
  m_locked = true;
  m_log = a_log;
  m_hooks = a_hooks;
  m_debug = a_debug;

  GM_ASSERT(m_hooks != NULL);

  // set up memory and stacks.
  m_currentLoop = -1;
  m_currentFunction = NULL;
  m_loopStack.Reset();
  m_patches.Reset();

  // set up the stacks for the first procedure.
  m_hooks->Begin(m_debug);

  PushFunction();
  GM_ASSERT(m_currentFunction);

  // generate the byte code for the root procedure
  if(!Generate(a_codeTree, &m_currentFunction->m_byteCode))
  {
    ++m_errors;
  }
  else
  {
    m_currentFunction->m_byteCode.Emit(BC_RET);

    // Create a locals table
    const char ** locals = NULL;
    if(m_debug)
    {
      locals = (const char **) alloca(sizeof(const char *) * m_currentFunction->m_numLocals);
      memset(locals, 0, sizeof(const char *) * m_currentFunction->m_numLocals);
      for(gmuint v = 0; v < m_currentFunction->m_variables.Count(); ++v)
      {
        Variable &variable = m_currentFunction->m_variables[v];
        if(variable.m_offset != -1)
        {
          locals[variable.m_offset] = variable.m_symbol;
        }
      }
    }
    
    // Fill out a function info struct and add the function to the code gen hooks.

    gmSortDebugLines(m_currentFunction->m_lineInfo);

    gmFunctionInfo info;
    info.m_id = m_hooks->GetFunctionId();
    info.m_root = true;
    info.m_byteCode = m_currentFunction->m_byteCode.GetData();
    info.m_byteCodeLength = m_currentFunction->m_byteCode.Tell();
    info.m_numParams = 0;
    info.m_numLocals = m_currentFunction->m_numLocals;
    info.m_symbols = locals;
    info.m_maxStackSize = m_currentFunction->m_byteCode.GetMaxTos();
    info.m_lineInfoCount = m_currentFunction->m_lineInfo.Count();
    info.m_lineInfo = m_currentFunction->m_lineInfo.GetData();
    info.m_debugName = "__main";
    m_hooks->AddFunction(info);

    //gmByteCodePrint(stdout, info.m_byteCode, info.m_byteCodeLength);
  }

  PopFunction();

  m_hooks->End(m_errors);

  return m_errors;
}
Beispiel #28
0
bool cPlugin_NewLua::Initialize(void)
{
	cCSLock Lock(m_CriticalSection);
	if (m_LuaState == NULL)
	{	
		m_LuaState = lua_open();
		luaL_openlibs(m_LuaState);
		tolua_AllToLua_open(m_LuaState);
		ManualBindings::Bind(m_LuaState);
		luaopen_lsqlite3(m_LuaState);
		luaopen_lxp(m_LuaState);
		
		// Inject the identification global variables into the state:
		lua_pushlightuserdata(m_LuaState, this);
		lua_setglobal(m_LuaState, LUA_PLUGIN_INSTANCE_VAR_NAME);
		lua_pushstring(m_LuaState, GetName().c_str());
		lua_setglobal(m_LuaState, LUA_PLUGIN_NAME_VAR_NAME);
	}

	std::string PluginPath = FILE_IO_PREFIX + GetLocalDirectory() + "/";

	// Load all files for this plugin, and execute them
	AStringList Files = GetDirectoryContents(PluginPath.c_str());
	for (AStringList::const_iterator itr = Files.begin(); itr != Files.end(); ++itr)
	{
		if (itr->rfind(".lua") == AString::npos)
		{
			continue;
		}
		AString Path = PluginPath + *itr;
		int s = luaL_loadfile(m_LuaState, Path.c_str() );
		if( report_errors( m_LuaState, s ) )
		{
			LOGERROR("Can't load plugin %s because of an error in file %s", GetLocalDirectory().c_str(), Path.c_str() );
			lua_close( m_LuaState );
			m_LuaState = 0;
			return false;
		}

		s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0);
		if( report_errors( m_LuaState, s ) )
		{
			LOGERROR("Error in plugin %s in file %s", GetLocalDirectory().c_str(), Path.c_str() );
			lua_close( m_LuaState );
			m_LuaState = 0;
			return false;
		}
	}  // for itr - Files[]

	// Call intialize function
	if (!PushFunction("Initialize"))
	{
		lua_close( m_LuaState );
		m_LuaState = 0;
		return false;
	}

	tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
	
	if (!CallFunction(1, 1, "Initialize"))
	{
		lua_close( m_LuaState );
		m_LuaState = 0;
		return false;
	}

	if( !lua_isboolean( m_LuaState, -1 ) )
	{
		LOGWARN("Error in plugin %s Initialize() must return a boolean value!", GetLocalDirectory().c_str() );
		lua_close( m_LuaState );
		m_LuaState = 0;
		return false;
	}

	bool bSuccess = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	return bSuccess;
}