Example #1
0
void outputFunc(std::string file, std::string func, const std::string& str, std::vector<any> args)
{
    LuaState ls;
    openUtilExtendLibs(ls.getState());

    int err = ls.parseFile(file);
    if (0 != err)
    {
        printLine(luaGetError(ls.getState(), err));
        return;
    }

    luaGetGlobal(ls.getState(), func);

    luaPushString(ls.getState(), str);
    for (size_t i=0; i<args.size(); ++i)
        luaPushAny(ls.getState(), args[i]);

    err = luaCallFunc(ls.getState(), 1 + args.size() , 0);
    if (0 != err)
    {
        printLine(luaGetError(ls.getState(), err));
        return;
    }

    luaPop(ls.getState(), -1);
}
Example #2
0
void CCallStackDlg::Update()
{
	m_list.DeleteAllItems();

	LuaState* state = theApp.GetDocument()->GetState();

	LuaObject callStackTableObj = state->GetGlobals()[ "CallStack" ];
	if (callStackTableObj.IsTable())
	{
		int index = 1;
		while (true)
		{
			LuaObject callTableObj = callStackTableObj.GetByIndex(index++);
			if (callTableObj.IsNil())
				break;
			LuaObject nameObj = callTableObj.GetByIndex(1);
			LuaObject typeObj = callTableObj.GetByIndex(2);
			LuaObject lineNumberObj = callTableObj.GetByIndex(3);

			CString str;
			if (lineNumberObj.GetInteger() != -1)
				str.Format(_T(" line %d"), lineNumberObj.GetInteger());
			str = nameObj.GetString() + str + _T(" (") + typeObj.GetString() + _T(")");
			m_list.InsertItem(m_list.GetItemCount(), str);
		}
	}
}
Example #3
0
		void LuaTable::toLua(LuaState& lua) const {
			lua.createTable(intKeys.size(), stringKeys.size());

			std::for_each(std::begin(intKeys), std::end(intKeys),
				[&](std::pair<const int, unsigned> intKey)->void {

				lua.pushStack(intKey.first);

				pushAnyValue(lua, types[intKey.second], values[intKey.second]);

				lua.setTable();

			});

			std::for_each(std::begin(stringKeys), std::end(stringKeys),
				[&](std::pair<const std::string, unsigned> stringKey)->void {

				lua.pushStack(stringKey.first);

				pushAnyValue(lua, types[stringKey.second], values[stringKey.second]);

				lua.setTable();

			});

		}
Example #4
0
		void LuaTable::fromLua(LuaState& lua) {
			
			std::for_each(std::begin(intKeys), std::end(intKeys),
				[&](std::pair<const int, unsigned> intKey)->void {

				int x = 0;

				lua.getTable(intKey.first);

				pullAnyValue(lua, types[intKey.second], values[intKey.second]);

				lua.popStack();

			});

			std::for_each(std::begin(stringKeys), std::end(stringKeys),
				[&](std::pair<const std::string, unsigned> stringKey)->void {

				lua.getTable(stringKey.first);

				pullAnyValue(lua, types[stringKey.second], values[stringKey.second]);

				lua.popStack();

			});

		}
Example #5
0
/*static*/ LuaState* LuaState::Create(bool initStandardLibrary)
{
	LuaState* state = LuaState::Create();
	if (initStandardLibrary)
		state->OpenLibs();
	return state;
}
/**
	Go to the next entry in the table.

	\return Returns true if the iteration is done.
**/
bool LuaStackTableIterator::Next()
{
	// This function is only active if Reset() has been called head.
	luaplus_assert( IsValid() );

	// This is a local helper variable so we don't waste space in the class
	// definition.
	LuaState* state = m_tableObj.GetState();

	// Do any stack management operations.
	if ( m_autoStackManagement )
	{
		state->SetTop( m_startStackIndex + 1 );
	}
	else
	{
		// If this luaplus_assert fires, then you left something on the stack.
		luaplus_assert( state->GetTop() == m_startStackIndex + 1 );
	}

	// Do the Lua table iteration.
	if ( state->Next( m_tableObj ) == 0 )
	{
		// Invalidate the iterator.
		m_startStackIndex = -1;
		return false;
	}

	// The iterator is still valid.
	return true;
}
Example #7
0
int LuaState::LuaError(lua_State* luaState)
{
	LuaState* wrapper = LuaState::GetWrapper(luaState);
	assert(wrapper);
	wrapper->OnError();
	return 0;
}
Example #8
0
/*static*/ LuaState* LuaState::CreateMT(bool initStandardLibrary)
{
	lua_Alloc reallocFunc;
	void* data;
	lua_getdefaultallocfunction(&reallocFunc, &data);
	LuaState* state = (LuaState*)(*reallocFunc)(data, NULL, 0, sizeof(LuaState), "LuaState", 0);
	::new(state) LuaState(true);
	state->OpenLibs();
	return state;
}
Example #9
0
int main(int argc, char** argv)
{
	LuaState* pstate = LuaState::Create(true);	

	pstate->GetGlobals().Register("TestFunc", TestFunc);	

	const char* szCmd = "TestFunc('hahhahhahh');";
	pstate->DoString(szCmd);

	LuaState::Destroy(pstate);
	return 0;
}
Example #10
0
LuaState* LuaState::luaForThread( sys::Thread& thread, unsigned int id, const char* init )
{
    sys::LockEnterLeave lock( s_lock );
    
    //
    //  create new lua state for new thread
    //  
    LuaState* lua = Leda::instance()->newLua();
    lua->load( id, false, init );
    thread.setData( lua ); 
    
    return lua;
}
Example #11
0
	/*bool TileType::loadFromDef(JsonValue value)
	{
		if (value.has("fullName", JV_STR))
		{
			mFullName = value["fullName"].getCStr();
		}
		return true;
	}*/
	bool TileType::loadFromDef(LuaState &lua)
	{
		if (!lua_istable(lua, -1))
		{
			return false;
		}
		if (lua.isTableString("fullName"))
		{
			mFullName = lua_tostring(lua, -1);
			lua.pop(1);
		}
		return true;
	}
Example #12
0
	bool TestLuaTileSet::testSimple() {
		LuaState lua;
		
		assert(lua.loadString("TileSet = import(\"TileSet\")\n"
			"set = TileSet.new(\"testTileSet\")\n"
			"function getName()\n"
			"	return set:name()\n"
			"end\n"
			"function getFullName()\n"
			"	return set:full_name()\n"
			"end\n"
			"function setFullName(name)\n"
			"	set:full_name(name)\n"
			"end\n"
			));

		assert(lua.hasGlobalFunction("getName"));
		lua_acall(lua, 0, 1);
		am_equalsStr("testTileSet", lua_tostring(lua, -1));
		lua.pop(1);

		assert(lua.hasGlobalFunction("setFullName"));
		lua.push("New Full Name");
		lua_acall(lua, 1, 0);
		
		assert(lua.hasGlobalFunction("getFullName"));
		lua_acall(lua, 0, 1);
		am_equalsStr("New Full Name", lua_tostring(lua, -1));
		lua.pop(1);

		return true;
	}
Example #13
0
void LuaPlusGCFunction(void* s)
{
	GCState* st = (GCState*)s;
	LuaState* state = (LuaState*)lua_getstateuserdata(st->L);
	if (!state)
		return;

	LuaObject* curObj = state->GetHeadObject()->m_next;
	while (curObj != state->GetTailObject())
	{
		markobject(st, &curObj->m_object);
		curObj = curObj->m_next;		
	}
}
/**
	Start iteration at the beginning of the table.
**/
void LuaStackTableIterator::Reset()
{
	// Start afresh...
	LuaState* state = m_tableObj.GetState();
	m_startStackIndex = state->GetTop();

	// Push the head stack entry.
	state->PushNil();

	// Start the iteration.  If the return value is 0, then the iterator
	// will be invalid.
	if ( state->Next( m_tableObj ) == 0 )
		m_startStackIndex = -1;
}
Example #15
0
extern "C" LUAMODULE_API int luaopen_bit(lua_State* L)
{
	LuaState* state = LuaState::CastState(L);
	LuaObject obj = state->GetGlobals().CreateTable( "bit" );
	obj.Register("bnot", int_bnot);
	obj.Register("band", int_band);
	obj.Register("bor", int_bor);
	obj.Register("bxor", int_bxor);
	obj.Register("lshift", int_lshift);
	obj.Register("rshift", int_rshift);
	obj.Register("arshift", int_arshift);
	obj.Register("mod", int_mod);
	obj.Register("help", LS_help);
	return 0;
}
Example #16
0
TEST_F(Benchmark, LuaLoadString)
{
    LuaState l;
    string error;
    l.LoadLuaCode("function zippylog_load_string(s)\n"
                  "  e = zippylog.envelope.new()\n"
                  "  e:set_string_value(s)\n"
                  "  return e\n"
                  "end", error);

    ::zippylog::lua::LoadStringResult result;
    const string input = "foo bar 2k";
    TIMER_START(10000);
    l.ExecuteLoadString(input, result);
    TIMER_END("zippylog.lua.load_string_return_simple_envelope");
}
Example #17
0
void LuaState::LuaPlusGCFunction(void* s)
{
	lua_State* L = (lua_State*)s;
	LuaState* state = (LuaState*)lua_getstateuserdata(L);
	if (!state)
		return;

    global_State* g = G(L);

	LuaObject* curObj = state->GetHeadObject()->m_next;
	while (curObj != (LuaObject*)state->GetTailObject())
	{
		markvalue(g, curObj->GetTObject());
		curObj = ((MiniLuaObject*)curObj)->m_next;
	}
}
Example #18
0
static void DebugLineHook( lua_State* inState, lua_Debug* ar )
{
	if ( ar->event == LUA_HOOKLINE )
	{
		LuaState* state = LuaState::CastState( inState );

		lua_Debug debugInfo;
		state->GetStack( 0, &debugInfo );
		state->GetInfo( "Sl", &debugInfo );

		if ( debugInfo.source[0] == '@' )
			debugInfo.source++;

		printf("%s[%d]\n", debugInfo.source, debugInfo.currentline - 1);
	}
}
void PositionComponent::registerComponent(LuaState& state)
{
    //Register loading infos
    meta::MetadataStore::registerClass<PositionComponent>("PositionComponent")
        .declareAttribute<float>("x", &PositionComponent::x)
        .declareAttribute<float>("y", &PositionComponent::y)
        .declareAttribute<float>("z", &PositionComponent::z)
        .declareAttribute<float>("width", &PositionComponent::width)
        .declareAttribute<float>("height", &PositionComponent::height);

    EntityHandle::declareComponent<PositionComponent>("position");

    //Register to lua
    state.getState().new_usertype<PositionComponent>("position_component",
        "x", &PositionComponent::x,
        "y", &PositionComponent::y,
        "z", &PositionComponent::z,
        "width", &PositionComponent::width,
        "height", &PositionComponent::height,
        "old_x", sol::readonly(&PositionComponent::oldX),
        "old_y", sol::readonly(&PositionComponent::oldY),
        "old_width", sol::readonly(&PositionComponent::oldWidth),
        "old_height", sol::readonly(&PositionComponent::oldHeight)
    );
    state.declareComponentGetter<PositionComponent>("position");
}
Example #20
0
void LoadGameLibrary(LuaState &state) {
	state.SetGlobal("ChunkWidth", ChunkWidth);
	state.SetGlobal("ChunkHeight", ChunkHeight);
	state.SetGlobal("ChunkLength", ChunkLength);
	
	state.GetGlobalNamespace()
	
	.beginClass<Dimension>("Dimension")
	.addFunction("GetBlock", &Dimension::GetBlockByCoord)
	.addFunction("IsSolidBlock", &Dimension::IsSolidBlock)
	.endClass()
	
	
	.beginClass<Chunk>("Chunk")
	.addFunction("GetBlock", &Chunk::GetBlock)
	.addFunction("IsSolidBlock", &Chunk::IsSolidBlock)
	.addFunction("GetDimension", &Chunk::GetDimension)
	.addFunction("GetX", &Chunk::GetX)
	.addFunction("GetZ", &Chunk::GetZ)
	.endClass()
	
	
	.beginClass<Block>("Block")
	.addFunction("GetX", &Block::GetX)
	.addFunction("GetY", &Block::GetY)
	.addFunction("GetZ", &Block::GetZ)
	.addFunction("GetAbsX", &Block::GetAbsoluteX)
	.addFunction("GetAbsZ", &Block::GetAbsoluteZ)
	.addFunction("IsSolid", &Block::IsSolid)
	.addFunction("GetChunk", &Block::GetChunk)
	.endClass()
	
	.beginClass<Entity>("Entity")
	.addFunction("GetPosition", &Entity::GetPosition)
	.addFunction("GetRotation", &Entity::GetRotation)
	.addFunction("SetPosition", &Entity::SetPosition)
	.addFunction("SetRotation", &Entity::SetRotation)
	.endClass()
	
	
	
	.beginClass<TextureAtlas<TEXTURE_ATLAS_BLOCK>>("BlockTexture")
	.addStaticFunction("GetTexCoord", &TextureAtlas<TEXTURE_ATLAS_BLOCK>::GetTextureCoords)
	;
}
Example #21
0
		void LuaTable::pushAnyValue(LuaState& lua, std::type_index type, void* value) const {

			 if (type == typeid(int)) {
				lua.pushStack(*static_cast<int*>(value));
			}
			else if (type == typeid(std::string)) {
				lua.pushStack(*static_cast<std::string*>(value));
			}
			else if (type == typeid(long)) {
				lua.pushStack(*static_cast<long*>(value));
			}
			else if (type == typeid(bool)) {
				lua.pushStack(*static_cast<bool*>(value));
			}
			else if (type == typeid(float)) {
				lua.pushStack(*static_cast<float*>(value));
			}
			else if (type == typeid(double)) {
				lua.pushStack(*static_cast<double*>(value));
			}
			else if (type == typeid(LuaFunction)) {
				lua.pushStack(*static_cast<LuaFunction*>(value));
			}
			else {
				auto vcast = static_cast<LuaTable*>(value);
				vcast->toLua(lua);
			}

		}
Example #22
0
void LevelState::registerClass(LuaState& luaState)
{
    sol::constructors<> ctor;
    sol::usertype<LevelState> levelLuaClass(ctor,
        "create_new_entity", &LevelState::lua_createNewEntity,
        "get_entities", &LevelState::lua_getEntities
    );
    luaState.getState().set_usertype("level_state", levelLuaClass);
}
Example #23
0
extern "C" LUAMODULE_API int luaopen_vdrive(lua_State* L)
{
	LuaState* state = LuaState::CastState(L);

	LuaObject metaTableObj = state->GetRegistry().CreateTable("VDriveFileHandleMetaTable");
	metaTableObj.Register("__gc",			VirtualFileHandle_gc);

	metaTableObj = state->GetRegistry().CreateTable("VDriveMetaTable");
	metaTableObj.Register("Create",				LS_VirtualDrive_Create);
	metaTableObj.Register("Open",				LS_VirtualDrive_Open);
	metaTableObj.Register("Close",				LS_VirtualDrive_Close);
	metaTableObj.Register("Flush",				LS_VirtualDrive_Flush);
	metaTableObj.Register("FileCreate",			LS_VirtualDrive_FileCreate);
	metaTableObj.Register("FileOpen",			LS_VirtualDrive_FileOpen);
	metaTableObj.Register("FileClose",			LS_VirtualDrive_FileClose);
	metaTableObj.Register("FileCloseAll",		LS_VirtualDrive_FileCloseAll);
	metaTableObj.Register("FileGetFileName",    LS_VirtualDrive_FileGetFileName);
	metaTableObj.Register("FileGetPosition",    LS_VirtualDrive_FileGetPosition);
	metaTableObj.Register("FileSetLength",      LS_VirtualDrive_FileSetLength);
	metaTableObj.Register("FileGetLength",      LS_VirtualDrive_FileGetLength);
	metaTableObj.Register("FileRead",           LS_VirtualDrive_FileRead);
	metaTableObj.Register("FileWrite",          LS_VirtualDrive_FileWrite);
    metaTableObj.Register("FileErase",			LS_VirtualDrive_FileErase);
	metaTableObj.Register("FileRename",			LS_VirtualDrive_FileRename);
	metaTableObj.Register("FileInsert",			LS_VirtualDrive_FileInsert);
	metaTableObj.Register("FileExtract",		LS_VirtualDrive_FileExtract);
	metaTableObj.Register("Pack",				LS_VirtualDrive_Pack);
	metaTableObj.Register("ProcessFileList",	LS_VirtualDrive_ProcessFileList);
	metaTableObj.Register("GetFileName",		LS_VirtualDrive_GetFileName);
	metaTableObj.Register("GetFileEntryCount",	LS_VirtualDrive_GetFileEntryCount);
	metaTableObj.Register("GetFileEntry",		LS_VirtualDrive_GetFileEntry);
	metaTableObj.Register("FindFileEntry",		LS_VirtualDrive_FindFileEntry);
	metaTableObj.Register("GetFileEntryIndex",	LS_VirtualDrive_GetFileEntryIndex);
	metaTableObj.Register("__gc",				VirtualDrive_gc);
	metaTableObj.SetObject("__index",			metaTableObj);

	LuaObject obj = state->GetGlobals().CreateTable( "vdrive" );
	obj.Register("VirtualDrive", LS_VirtualDrive);
	obj.Register("AdjustTime_t", LS_AdjustTime_t);
	obj.Register("help", LS_Help);
	obj.Register("crc32", LS_crc32);
	return 0;
}
Example #24
0
void ScriptFunctionsRegister(struct lua_State* L)
{
	LuaState* state = lua_State_To_LuaState(L);
	LuaObject globalsObj = state->GetGlobals();
	globalsObj.Register("GetTickCount",		LS_GetTickCount);

#if defined(_MSC_VER)  &&  defined(WIN32)  &&  !defined(_XBOX)  &&  !defined(_XBOX_VER)  &&  !defined(PLATFORM_PS3)
#elif defined(__APPLE__)
	char modulePath[MAXPATHLEN + 1];
	unsigned int path_len = MAXPATHLEN;
    _NSGetExecutablePath(modulePath, &path_len);
	char* slashPtr = strrchr(modulePath, '/');
	slashPtr++;
	*slashPtr = 0;
#endif // _MSC_VER

    state->GetGlobals().Register("LuaDumpGlobals", LS_LuaDumpGlobals);
	state->GetGlobals().Register("LuaDumpObject", LS_LuaDumpObject);
	state->GetGlobals().Register("LuaDumpFile", LS_LuaDumpFile);
}
void ColliderComponent::registerComponent(LuaState& state)
{
    meta::MetadataStore::registerClass<ColliderComponent>();

    EntityHandle::declareComponent<ColliderComponent>("collider");

    state.getState().new_usertype<ColliderComponent>("collider_component" //TODO: Replace the name here
        //TODO: Register the properties here
    );
    state.declareComponentGetter<ColliderComponent>("collider");
}
Example #26
0
	SirenObject DeserializeLua(LuaState& state, const ISirenType& type)
	{
		SirenObject obj;
		SirenLuaReader reader(state.GetState());
		SirenObjectDeserializer deserializer(reader);
		if (deserializer.Deserialize(obj, type))
		{
			return obj;
		}
		return SirenObject::Null;
	}
Example #27
0
void executeSlayer()
{
	LuaState L;
	L.init();
	L.dofile("xmasEventCommon.lua");

	LuaTradeEventSlayerItem luaSlayerItem(&L);

	while (1)
	{
		cout << "Input SUM(0 to exit): ";
		int SUM; cin >> SUM;

		if (SUM==0) break;

		luaSlayerItem.setSum(SUM);

		executeSelectItem(luaSlayerItem, "xmasEventSlayer.lua");
	}
}
Example #28
0
void executeVampire()
{
	LuaState L;
	L.init();
	L.dofile("xmasEventCommon.lua");

	LuaTradeEventVampireItem luaVampireItem(&L);

	while (1)
	{
		cout << "Input Level(0 to exit): ";
		int Level; cin >> Level;

		if (Level==0) break;

		luaVampireItem.setLevel(Level);

		executeSelectItem(luaVampireItem, "xmasEventVampire.lua");
	}
}
Example #29
0
extern "C" LUAMODULE_API int luaopen_dotnet(lua_State* L)
{
	LuaState* state = LuaState::CastState(L);
	LuaObject obj = state->GetGlobals()["dotnet"];
	if (obj.IsTable())
		return 0;

	obj = state->GetGlobals().CreateTable( "dotnet" );

	char path[ _MAX_PATH ];
#ifdef _DEBUG
	const char* dllName = "ldotnet.dlld";
#else !_DEBUG
	const char* dllName = "ldotnet.dll";
#endif _DEBUG
	GetModuleFileName(GetModuleHandle(dllName), path, _MAX_PATH);
	char* slashPtr = strrchr( path, '\\' );
	slashPtr++;
	*slashPtr = 0;
#ifdef _DEBUG
	strcat(path, "Debug\\");
#endif _DEBUG
	strcat(path, "dotnetinterface.dll");

	Assembly* assembly = Assembly::LoadFrom(path);
	Type* type = assembly->GetType("LuaInterface.Lua");
	Object* parms[] = new Object*[1];
	parms[0] = __box((IntPtr)state->GetCState());
	Object* lua = Activator::CreateInstance(type, parms);

	LuaObject metaTableObj;
	metaTableObj.AssignNewTable(state);
	metaTableObj.Register("__gc", GCHandle_gc);

	LuaObject dotNetObj = state->NewUserDataBox((void*)(GCHandle::op_Explicit(GCHandle::Alloc(lua))).ToInt32());
	dotNetObj.SetMetaTable(metaTableObj);

	obj.SetObject("__dotNetUserData", dotNetObj);

	return 0;
}
Example #30
0
void WeatherMgr::Init(LuaState &lua)
{
    lua.getState()->new_usertype<WeatherMgr>("WeatherMgr",
                                             "current", sol::property(&WeatherMgr::getCurrent, &WeatherMgr::setCurrent),
                                             "next", sol::property(&WeatherMgr::getNext, &WeatherMgr::setNext),
                                             "transitionFactor", sol::property(&WeatherMgr::getTransition, &WeatherMgr::setTransition),
                                             "updateTime", sol::property(&WeatherMgr::getUpdate, &WeatherMgr::setUpdate),
                                             "copy", &WeatherMgr::copy,
                                             "setWeather", &WeatherMgr::setWeather,
                                             "request", &WeatherMgr::requestWeather
    );
}