Ver Fonte

Merge pull request #9 from slime73/master

Fix compilation on compilers without full C99/C++11 support, fix Lua stack for require("enet")
leaf há 12 anos atrás
pai
commit
25c22ef6fc
1 ficheiros alterados com 4 adições e 25 exclusões
  1. 4 25
      enet.c

+ 4 - 25
enet.c

@@ -251,22 +251,10 @@ static int host_create(lua_State *l) {
 }
 
 static int linked_version(lua_State *l) {
-	char* enet_version_str = (char*) malloc (sizeof(char) * 32);
-
-	if (enet_version_str == NULL) {
-		luaL_error(l, "Error allocating memory for version string.");
-	}
-
-	snprintf (enet_version_str, sizeof(char) * 32, "%d.%d.%d",
+	lua_pushfstring(l, "%d.%d.%d",
 			ENET_VERSION_GET_MAJOR(enet_linked_version()),
 			ENET_VERSION_GET_MINOR(enet_linked_version()),
-			ENET_VERSION_GET_PATCH(enet_linked_version())
-			);
-
-	lua_pushstring (l, enet_version_str);
-
-	free (enet_version_str);
-
+			ENET_VERSION_GET_PATCH(enet_linked_version()));
 	return 1;
 }
 
@@ -398,22 +386,13 @@ static int host_socket_get_address(lua_State *l) {
 	ENetAddress address;
 	enet_socket_get_address (host->socket, &address);
 
-	char* address_str = (char *) malloc(sizeof(char) * 64);
-	if (address_str == NULL) {
-		luaL_error(l, "Error allocating memory for address string.");
-	}
-
-	snprintf (address_str, sizeof(char) * 64, "%d.%d.%d.%d:%d",
+	lua_pushfstring(l, "%d.%d.%d.%d:%d",
 			((address.host) & 0xFF),
 			((address.host >> 8) & 0xFF),
 			((address.host >> 16) & 0xFF),
 			(address.host >> 24& 0xFF),
 			address.port);
 
-	lua_pushstring(l, address_str);
-
-	free (address_str);
-
 	return 1;
 }
 static int host_total_sent_data(lua_State *l) {
@@ -769,5 +748,5 @@ int luaopen_enet(lua_State *l) {
 	lua_setfield(l, LUA_REGISTRYINDEX, "enet_peers");
 
 	luaL_register(l, "enet", enet_funcs);
-	return 0;
+	return 1;
 }