⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tolua_map.c

📁 tolua++是一个对lua进行封装调用的工具
💻 C
📖 第 1 页 / 共 2 页
字号:
	return clone;}/* Default collect function*/TOLUA_API int tolua_default_collect (lua_State* tolua_S){ void* self = tolua_tousertype(tolua_S,1,0); free(self); return 0;}/* Do clone*/TOLUA_API int tolua_register_gc (lua_State* L, int lo){ int success = 1; void *value = *(void **)lua_touserdata(L,lo); lua_pushstring(L,"tolua_gc"); lua_rawget(L,LUA_REGISTRYINDEX);	lua_pushlightuserdata(L,value);	lua_rawget(L,-2);	if (!lua_isnil(L,-1)) /* make sure that object is not already owned */		success = 0;	else	{		lua_pushlightuserdata(L,value);		lua_getmetatable(L,lo);		lua_rawset(L,-4);	}	lua_pop(L,2);	return success;}/* Register a usertype	* It creates the correspoding metatable in the registry, for both 'type' and 'const type'.	* It maps 'const type' as being also a 'type'*/TOLUA_API void tolua_usertype (lua_State* L, char* type){ char ctype[128] = "const "; strncat(ctype,type,120);	/* create both metatables */ if (tolua_newmetatable(L,ctype) && tolua_newmetatable(L,type))	 mapsuper(L,type,ctype);             /* 'type' is also a 'const type' */}/* Begin module	* It pushes the module (or class) table on the stack*/TOLUA_API void tolua_beginmodule (lua_State* L, char* name){	if (name)	{	 lua_pushstring(L,name);		lua_rawget(L,-2);	}	else	 lua_pushvalue(L,LUA_GLOBALSINDEX);}/* End module	* It pops the module (or class) from the stack*/TOLUA_API void tolua_endmodule (lua_State* L){	lua_pop(L,1);}/* Map module	* It creates a new module*/#if 1TOLUA_API void tolua_module (lua_State* L, char* name, int hasvar){	if (name)	{		/* tolua module */		lua_pushstring(L,name);		lua_rawget(L,-2);		if (!lua_istable(L,-1))  /* check if module already exists */		{			lua_pop(L,1);		 lua_newtable(L);		 lua_pushstring(L,name);			lua_pushvalue(L,-2);		 lua_rawset(L,-4);       /* assing module into module */		}	}	else	{		/* global table */		lua_pushvalue(L,LUA_GLOBALSINDEX);	}	if (hasvar)	{		if (!tolua_ismodulemetatable(L))  /* check if it already has a module metatable */		{			/* create metatable to get/set C/C++ variable */			lua_newtable(L);			tolua_moduleevents(L);			if (lua_getmetatable(L,-2))				lua_setmetatable(L,-2);  /* set old metatable as metatable of metatable */			lua_setmetatable(L,-2);		}	}	lua_pop(L,1);               /* pop module */}#elseTOLUA_API void tolua_module (lua_State* L, char* name, int hasvar){	if (name)	{		/* tolua module */		lua_pushstring(L,name);		lua_newtable(L);	}	else	{		/* global table */		lua_pushvalue(L,LUA_GLOBALSINDEX);	}	if (hasvar)	{		/* create metatable to get/set C/C++ variable */		lua_newtable(L);		tolua_moduleevents(L);		if (lua_getmetatable(L,-2))			lua_setmetatable(L,-2);  /* set old metatable as metatable of metatable */		lua_setmetatable(L,-2);	}	if (name)		lua_rawset(L,-3);       /* assing module into module */	else		lua_pop(L,1);           /* pop global table */}#endifstatic void push_collector(lua_State* L, const char* type, lua_CFunction col) {	/* push collector function, but only if it's not NULL, or if there's no	   collector already */	if (!col) return;	luaL_getmetatable(L,type);	lua_pushstring(L,".collector");	/*	if (!col) {		lua_pushvalue(L, -1);		lua_rawget(L, -3);		if (!lua_isnil(L, -1)) {			lua_pop(L, 3);			return;		};		lua_pop(L, 1);	};	//	*/	lua_pushcfunction(L,col);	lua_rawset(L,-3);	lua_pop(L, 1);};/* Map C class	* It maps a C class, setting the appropriate inheritance and super classes.*/TOLUA_API void tolua_cclass (lua_State* L, char* lname, char* name, char* base, lua_CFunction col){	char cname[128] = "const ";	char cbase[128] = "const ";	strncat(cname,name,120);	strncat(cbase,base,120);	mapinheritance(L,name,base);	mapinheritance(L,cname,name);	mapsuper(L,cname,cbase);	mapsuper(L,name,base);	lua_pushstring(L,lname);		push_collector(L, name, col);	/*	luaL_getmetatable(L,name);	lua_pushstring(L,".collector");	lua_pushcfunction(L,col);	lua_rawset(L,-3);	*/		luaL_getmetatable(L,name);	lua_rawset(L,-3);              /* assign class metatable to module */	/* now we also need to store the collector table for the const	   instances of the class */	push_collector(L, cname, col);	/*	luaL_getmetatable(L,cname);	lua_pushstring(L,".collector");	lua_pushcfunction(L,col);	lua_rawset(L,-3);	lua_pop(L,1);	*/	}/* Add base	* It adds additional base classes to a class (for multiple inheritance)	* (not for now)TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base) {	char cname[128] = "const ";	char cbase[128] = "const ";	strncat(cname,name,120);	strncat(cbase,base,120);	mapsuper(L,cname,cbase);	mapsuper(L,name,base);};*//* Map function	* It assigns a function into the current module (or class)*/TOLUA_API void tolua_function (lua_State* L, char* name, lua_CFunction func){ lua_pushstring(L,name); lua_pushcfunction(L,func);	lua_rawset(L,-3);}/* sets the __call event for the class (expects the class' main table on top) *//*	never really worked :(TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type) {	lua_getmetatable(L, -1);	//luaL_getmetatable(L, type);	lua_pushstring(L,"__call");	lua_pushcfunction(L,func);	lua_rawset(L,-3);	lua_pop(L, 1);};*//* Map constant number	* It assigns a constant number into the current module (or class)*/TOLUA_API void tolua_constant (lua_State* L, char* name, double value){	lua_pushstring(L,name);	tolua_pushnumber(L,value);	lua_rawset(L,-3);}/* Map variable	* It assigns a variable into the current module (or class)*/TOLUA_API void tolua_variable (lua_State* L, char* name, lua_CFunction get, lua_CFunction set){	/* get func */	lua_pushstring(L,".get");	lua_rawget(L,-2);	if (!lua_istable(L,-1))	{		/* create .get table, leaving it at the top */		lua_pop(L,1);		lua_newtable(L);	 lua_pushstring(L,".get");		lua_pushvalue(L,-2);		lua_rawset(L,-4);	}	lua_pushstring(L,name);	lua_pushcfunction(L,get); lua_rawset(L,-3);                  /* store variable */	lua_pop(L,1);                      /* pop .get table */	/* set func */	if (set)	{		lua_pushstring(L,".set");		lua_rawget(L,-2);		if (!lua_istable(L,-1))		{			/* create .set table, leaving it at the top */			lua_pop(L,1);			lua_newtable(L);			lua_pushstring(L,".set");			lua_pushvalue(L,-2);			lua_rawset(L,-4);		}		lua_pushstring(L,name);		lua_pushcfunction(L,set);		lua_rawset(L,-3);                  /* store variable */		lua_pop(L,1);                      /* pop .set table */	}}/* Access const array	* It reports an error when trying to write into a const array*/static int const_array (lua_State* L){ luaL_error(L,"value of const array cannot be changed"); return 0;}/* Map an array	* It assigns an array into the current module (or class)*/TOLUA_API void tolua_array (lua_State* L, char* name, lua_CFunction get, lua_CFunction set){	lua_pushstring(L,".get");	lua_rawget(L,-2);	if (!lua_istable(L,-1))	{		/* create .get table, leaving it at the top */		lua_pop(L,1);		lua_newtable(L);	 lua_pushstring(L,".get");		lua_pushvalue(L,-2);		lua_rawset(L,-4);	}	lua_pushstring(L,name); lua_newtable(L);           /* create array metatable */ lua_pushvalue(L,-1);	lua_setmetatable(L,-2);    /* set the own table as metatable (for modules) */ lua_pushstring(L,"__index"); lua_pushcfunction(L,get);	lua_rawset(L,-3); lua_pushstring(L,"__newindex"); lua_pushcfunction(L,set?set:const_array);	lua_rawset(L,-3); lua_rawset(L,-3);                  /* store variable */	lua_pop(L,1);                      /* pop .get table */}TOLUA_API void tolua_dobuffer(lua_State* L, char* B, unsigned int size, const char* name) { #ifdef LUA_VERSION_NUM /* lua 5.1 */ luaL_loadbuffer(L, B, size, name) || lua_pcall(L, 0, 0, 0); #else lua_dobuffer(L, B, size, name); #endif};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -