add lua5.2 support for no particular reason
This commit is contained in:
parent
f8782338d7
commit
925ff1da3c
12
SConscript
12
SConscript
@ -248,13 +248,17 @@ def findLibs(env, conf):
|
|||||||
|
|
||||||
if not GetOption('nolua') and not GetOption('renderer'):
|
if not GetOption('nolua') and not GetOption('renderer'):
|
||||||
#Look for Lua
|
#Look for Lua
|
||||||
|
luaver = "lua5.1"
|
||||||
if not conf.CheckLib(['lua5.1', 'lua-5.1', 'lua51', 'lua']):
|
if not conf.CheckLib(['lua5.1', 'lua-5.1', 'lua51', 'lua']):
|
||||||
if platform != "Darwin" or not conf.CheckFramework("Lua"):
|
if conf.CheckLib(['lua5.2', 'lua-5.2', 'lua52']):
|
||||||
|
env.Append(CPPDEFINES=["LUA_COMPAT_ALL"])
|
||||||
|
luaver = "lua5.2"
|
||||||
|
elif platform != "Darwin" or not conf.CheckFramework("Lua"):
|
||||||
FatalError("lua5.1 development library not found or not installed")
|
FatalError("lua5.1 development library not found or not installed")
|
||||||
if platform == "Linux":
|
if platform == "Linux":
|
||||||
try:
|
try:
|
||||||
env.ParseConfig('pkg-config --cflags lua5.1')
|
env.ParseConfig("pkg-config --cflags {0}".format(luaver))
|
||||||
env.ParseConfig('pkg-config --libs lua5.1')
|
env.ParseConfig("pkg-config --libs {0}".format(luaver))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -479,7 +483,7 @@ if GetOption('beta'):
|
|||||||
#Generate list of sources to compile
|
#Generate list of sources to compile
|
||||||
sources = Glob("src/*.cpp") + Glob("src/*/*.cpp") + Glob("src/*/*/*.cpp") + Glob("generated/*.cpp")
|
sources = Glob("src/*.cpp") + Glob("src/*/*.cpp") + Glob("src/*/*/*.cpp") + Glob("generated/*.cpp")
|
||||||
if not GetOption('nolua') and not GetOption('renderer'):
|
if not GetOption('nolua') and not GetOption('renderer'):
|
||||||
sources += Glob("src/lua/socket/*.c")
|
sources += Glob("src/lua/socket/*.c") + Glob("src/lua/LuaCompat.c")
|
||||||
|
|
||||||
if platform == "Windows" and not msvc:
|
if platform == "Windows" and not msvc:
|
||||||
sources += env.RES('resources/powder-res.rc')
|
sources += env.RES('resources/powder-res.rc')
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
|
|
||||||
#define LUA_BITOP_VERSION "1.0.2"
|
#define LUA_BITOP_VERSION "1.0.2"
|
||||||
|
|
||||||
#include "luainc.h"
|
#include "LuaCompat.h"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
|
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
|
||||||
@ -178,11 +178,11 @@ int luaopen_bit(lua_State *L)
|
|||||||
msg = "arithmetic right-shift broken";
|
msg = "arithmetic right-shift broken";
|
||||||
luaL_error(L, "bit library self-test failed (%s)", msg);
|
luaL_error(L, "bit library self-test failed (%s)", msg);
|
||||||
}
|
}
|
||||||
#if LUA_VERSION_NUM < 502
|
//#if LUA_VERSION_NUM < 502
|
||||||
luaL_register(L, "bit", bit_funcs);
|
luaL_register(L, "bit", bit_funcs);
|
||||||
#else
|
//#else
|
||||||
luaL_newlib(L, bit_funcs);
|
// luaL_newlib(L, bit_funcs);
|
||||||
#endif
|
//#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
11
src/lua/LuaCompat.c
Normal file
11
src/lua/LuaCompat.c
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "LuaCompat.h"
|
||||||
|
|
||||||
|
#if LUA_VERSION_NUM >= 502
|
||||||
|
//implement missing luaL_typerror function
|
||||||
|
int luaL_typerror (lua_State *L, int narg, const char *tname)
|
||||||
|
{
|
||||||
|
const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
|
||||||
|
return luaL_argerror(L, narg, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -16,6 +16,13 @@ extern "C"
|
|||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if LUA_VERSION_NUM >= 502
|
||||||
|
#define luaL_getn(L,i) lua_rawlen(L, (i))
|
||||||
|
#define LUA_GLOBALSINDEX LUA_RIDX_GLOBALS
|
||||||
|
|
||||||
|
LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
//http://lua-users.org/wiki/SimplerCppBinding
|
//http://lua-users.org/wiki/SimplerCppBinding
|
||||||
|
|
||||||
#include "luainc.h"
|
#include "LuaCompat.h"
|
||||||
|
|
||||||
template <typename T> class Luna
|
template <typename T> class Luna
|
||||||
{
|
{
|
||||||
|
@ -139,7 +139,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m):
|
|||||||
int i = 0, j;
|
int i = 0, j;
|
||||||
char tmpname[12];
|
char tmpname[12];
|
||||||
int currentElementMeta, currentElement;
|
int currentElementMeta, currentElement;
|
||||||
const static struct luaL_reg tptluaapi [] = {
|
const static struct luaL_Reg tptluaapi [] = {
|
||||||
{"test", &luatpt_test},
|
{"test", &luatpt_test},
|
||||||
{"drawtext", &luatpt_drawtext},
|
{"drawtext", &luatpt_drawtext},
|
||||||
{"create", &luatpt_create},
|
{"create", &luatpt_create},
|
||||||
@ -442,7 +442,7 @@ int LuaScriptInterface::tpt_newIndex(lua_State *l)
|
|||||||
|
|
||||||
void LuaScriptInterface::initInterfaceAPI()
|
void LuaScriptInterface::initInterfaceAPI()
|
||||||
{
|
{
|
||||||
struct luaL_reg interfaceAPIMethods [] = {
|
struct luaL_Reg interfaceAPIMethods [] = {
|
||||||
{"showWindow", interface_showWindow},
|
{"showWindow", interface_showWindow},
|
||||||
{"closeWindow", interface_closeWindow},
|
{"closeWindow", interface_closeWindow},
|
||||||
{"addComponent", interface_addComponent},
|
{"addComponent", interface_addComponent},
|
||||||
@ -535,7 +535,7 @@ int LuaScriptInterface::particlePropertiesCount;
|
|||||||
void LuaScriptInterface::initSimulationAPI()
|
void LuaScriptInterface::initSimulationAPI()
|
||||||
{
|
{
|
||||||
//Methods
|
//Methods
|
||||||
struct luaL_reg simulationAPIMethods [] = {
|
struct luaL_Reg simulationAPIMethods [] = {
|
||||||
{"partNeighbours", simulation_partNeighbours},
|
{"partNeighbours", simulation_partNeighbours},
|
||||||
{"partNeighbors", simulation_partNeighbours},
|
{"partNeighbors", simulation_partNeighbours},
|
||||||
{"partChangeType", simulation_partChangeType},
|
{"partChangeType", simulation_partChangeType},
|
||||||
@ -1765,7 +1765,7 @@ int LuaScriptInterface::simulation_neighbours(lua_State * l)
|
|||||||
void LuaScriptInterface::initRendererAPI()
|
void LuaScriptInterface::initRendererAPI()
|
||||||
{
|
{
|
||||||
//Methods
|
//Methods
|
||||||
struct luaL_reg rendererAPIMethods [] = {
|
struct luaL_Reg rendererAPIMethods [] = {
|
||||||
{"renderModes", renderer_renderModes},
|
{"renderModes", renderer_renderModes},
|
||||||
{"displayModes", renderer_displayModes},
|
{"displayModes", renderer_displayModes},
|
||||||
{"colourMode", renderer_colourMode},
|
{"colourMode", renderer_colourMode},
|
||||||
@ -1957,7 +1957,7 @@ int LuaScriptInterface::renderer_debugHUD(lua_State * l)
|
|||||||
void LuaScriptInterface::initElementsAPI()
|
void LuaScriptInterface::initElementsAPI()
|
||||||
{
|
{
|
||||||
//Methods
|
//Methods
|
||||||
struct luaL_reg elementsAPIMethods [] = {
|
struct luaL_Reg elementsAPIMethods [] = {
|
||||||
{"allocate", elements_allocate},
|
{"allocate", elements_allocate},
|
||||||
{"element", elements_element},
|
{"element", elements_element},
|
||||||
{"property", elements_property},
|
{"property", elements_property},
|
||||||
@ -2474,7 +2474,7 @@ int LuaScriptInterface::elements_free(lua_State * l)
|
|||||||
void LuaScriptInterface::initGraphicsAPI()
|
void LuaScriptInterface::initGraphicsAPI()
|
||||||
{
|
{
|
||||||
//Methods
|
//Methods
|
||||||
struct luaL_reg graphicsAPIMethods [] = {
|
struct luaL_Reg graphicsAPIMethods [] = {
|
||||||
{"textSize", graphics_textSize},
|
{"textSize", graphics_textSize},
|
||||||
{"drawText", graphics_drawText},
|
{"drawText", graphics_drawText},
|
||||||
{"drawLine", graphics_drawLine},
|
{"drawLine", graphics_drawLine},
|
||||||
@ -2683,7 +2683,7 @@ int LuaScriptInterface::graphics_getHexColor(lua_State * l)
|
|||||||
void LuaScriptInterface::initFileSystemAPI()
|
void LuaScriptInterface::initFileSystemAPI()
|
||||||
{
|
{
|
||||||
//Methods
|
//Methods
|
||||||
struct luaL_reg fileSystemAPIMethods [] = {
|
struct luaL_Reg fileSystemAPIMethods [] = {
|
||||||
{"list", fileSystem_list},
|
{"list", fileSystem_list},
|
||||||
{"exists", fileSystem_exists},
|
{"exists", fileSystem_exists},
|
||||||
{"isFile", fileSystem_isFile},
|
{"isFile", fileSystem_isFile},
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef LUASCRIPTINTERFACE_H_
|
#ifndef LUASCRIPTINTERFACE_H_
|
||||||
#define LUASCRIPTINTERFACE_H_
|
#define LUASCRIPTINTERFACE_H_
|
||||||
|
|
||||||
#include "luainc.h"
|
#include "LuaCompat.h"
|
||||||
|
|
||||||
#include "CommandInterface.h"
|
#include "CommandInterface.h"
|
||||||
#include "simulation/Simulation.h"
|
#include "simulation/Simulation.h"
|
||||||
|
@ -24,7 +24,7 @@ int auxiliar_open(lua_State *L) {
|
|||||||
* Creates a new class with given methods
|
* Creates a new class with given methods
|
||||||
* Methods whose names start with __ are passed directly to the metatable.
|
* Methods whose names start with __ are passed directly to the metatable.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func) {
|
void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) {
|
||||||
luaL_newmetatable(L, classname); /* mt */
|
luaL_newmetatable(L, classname); /* mt */
|
||||||
/* create __index table to place methods */
|
/* create __index table to place methods */
|
||||||
lua_pushstring(L, "__index"); /* mt,"__index" */
|
lua_pushstring(L, "__index"); /* mt,"__index" */
|
||||||
|
@ -31,10 +31,10 @@
|
|||||||
* RCS ID: $Id: auxiliar.h,v 1.9 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: auxiliar.h,v 1.9 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
int auxiliar_open(lua_State *L);
|
int auxiliar_open(lua_State *L);
|
||||||
void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func);
|
void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func);
|
||||||
void auxiliar_add2group(lua_State *L, const char *classname, const char *group);
|
void auxiliar_add2group(lua_State *L, const char *classname, const char *group);
|
||||||
void auxiliar_setclass(lua_State *L, const char *classname, int objidx);
|
void auxiliar_setclass(lua_State *L, const char *classname, int objidx);
|
||||||
void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx);
|
void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx);
|
||||||
|
@ -222,7 +222,7 @@ static int recvline(p_buffer buf, luaL_Buffer *b) {
|
|||||||
pos = 0;
|
pos = 0;
|
||||||
while (pos < count && data[pos] != '\n') {
|
while (pos < count && data[pos] != '\n') {
|
||||||
/* we ignore all \r's */
|
/* we ignore all \r's */
|
||||||
if (data[pos] != '\r') luaL_putchar(b, data[pos]);
|
if (data[pos] != '\r') luaL_addchar(b, data[pos]);
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
if (pos < count) { /* found '\n' */
|
if (pos < count) { /* found '\n' */
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
*
|
*
|
||||||
* RCS ID: $Id: buffer.h,v 1.12 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: buffer.h,v 1.12 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
@ -18,7 +18,7 @@ static int finalize(lua_State *L);
|
|||||||
static int do_nothing(lua_State *L);
|
static int do_nothing(lua_State *L);
|
||||||
|
|
||||||
/* except functions */
|
/* except functions */
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{"newtry", global_newtry},
|
{"newtry", global_newtry},
|
||||||
{"protect", global_protect},
|
{"protect", global_protect},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
* RCS ID: $Id: except.h,v 1.2 2005/09/29 06:11:41 diego Exp $
|
* RCS ID: $Id: except.h,v 1.2 2005/09/29 06:11:41 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
int except_open(lua_State *L);
|
int except_open(lua_State *L);
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp);
|
|||||||
static int inet_global_gethostname(lua_State *L);
|
static int inet_global_gethostname(lua_State *L);
|
||||||
|
|
||||||
/* DNS functions */
|
/* DNS functions */
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{ "toip", inet_global_toip },
|
{ "toip", inet_global_toip },
|
||||||
{ "tohostname", inet_global_tohostname },
|
{ "tohostname", inet_global_tohostname },
|
||||||
{ "gethostname", inet_global_gethostname},
|
{ "gethostname", inet_global_gethostname},
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
*
|
*
|
||||||
* RCS ID: $Id: inet.h,v 1.16 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: inet.h,v 1.16 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
* RCS ID: $Id: io.h,v 1.11 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: io.h,v 1.11 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ static int base_open(lua_State *L);
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Modules and functions
|
* Modules and functions
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
static const luaL_reg mod[] = {
|
static const luaL_Reg mod[] = {
|
||||||
{"auxiliar", auxiliar_open},
|
{"auxiliar", auxiliar_open},
|
||||||
{"except", except_open},
|
{"except", except_open},
|
||||||
{"timeout", timeout_open},
|
{"timeout", timeout_open},
|
||||||
@ -49,7 +49,7 @@ static const luaL_reg mod[] = {
|
|||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{"skip", global_skip},
|
{"skip", global_skip},
|
||||||
{"__unload", global_unload},
|
{"__unload", global_unload},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
* RCS ID: $Id: luasocket.h,v 1.25 2007/06/11 23:44:54 diego Exp $
|
* RCS ID: $Id: luasocket.h,v 1.25 2007/06/11 23:44:54 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Current socket library version
|
* Current socket library version
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
* RCS ID: $Id: options.h,v 1.4 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: options.h,v 1.4 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
/* option registry */
|
/* option registry */
|
||||||
|
@ -24,7 +24,7 @@ static void make_assoc(lua_State *L, int tab);
|
|||||||
static int global_select(lua_State *L);
|
static int global_select(lua_State *L);
|
||||||
|
|
||||||
/* functions in library namespace */
|
/* functions in library namespace */
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{"select", global_select},
|
{"select", global_select},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#ifdef LUACONSOLE
|
#ifdef LUACONSOLE
|
||||||
// socket.lua from luasocket compiled into a cpp file
|
// socket.lua from luasocket compiled into a cpp file
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
void luaopen_socket(lua_State *l){
|
void luaopen_socket(lua_State *l){
|
||||||
int socket_luac_sz=4061;
|
int socket_luac_sz=4061;
|
||||||
const char* socket_luac="-----------------------------------------------------------------------------\012-- LuaSocket helper module\012-- Author: Diego Nehab\012-- RCS ID: $Id: socket.lua,v 1.22 2005/11/22 08:33:29 diego Exp $\012-----------------------------------------------------------------------------\012\012-----------------------------------------------------------------------------\012-- Declare module and import dependencies\012-----------------------------------------------------------------------------\012local base = _G\012local string = require(\042string\042)\012local math = require(\042math\042)\012local socket = require(\042socket.core\042)\012module(\042socket\042)\012\012-----------------------------------------------------------------------------\012-- Exported auxiliar functions\012-----------------------------------------------------------------------------\012function connect(address, port, laddress, lport)\012 local sock, err = socket.tcp()\012 if not sock then return nil, err end\012 if laddress then\012 local res, err = sock:bind(laddress, lport, -1)\012 if not res then return nil, err end\012 end\012 local res, err = sock:connect(address, port)\012 if not res then return nil, err end\012 return sock\012end\012\012function bind(host, port, backlog)\012 local sock, err = socket.tcp()\012 if not sock then return nil, err end\012 sock:setoption(\042reuseaddr\042, true)\012 local res, err = sock:bind(host, port)\012 if not res then return nil, err end\012 res, err = sock:listen(backlog)\012 if not res then return nil, err end\012 return sock\012end\012\012try = newtry()\012\012function choose(table)\012 return function(name, opt1, opt2)\012 if base.type(name) ~= \042string\042 then\012 name, opt1, opt2 = \042default\042, name, opt1\012 end\012 local f = table[name or \042nil\042]\012 if not f then base.error(\042unknown key (\042.. base.tostring(name) ..\042)\042, 3)\012 else return f(opt1, opt2) end\012 end\012end\012\012-----------------------------------------------------------------------------\012-- Socket sources and sinks, conforming to LTN12\012-----------------------------------------------------------------------------\012-- create namespaces inside LuaSocket namespace\012sourcet = {}\012sinkt = {}\012\012BLOCKSIZE = 2048\012\012sinkt[\042close-when-done\042] = function(sock)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function(self, chunk, err)\012 if not chunk then\012 sock:close()\012 return 1\012 else return sock:send(chunk) end\012 end\012 })\012end\012\012sinkt[\042keep-open\042] = function(sock)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function(self, chunk, err)\012 if chunk then return sock:send(chunk)\012 else return 1 end\012 end\012 })\012end\012\012sinkt[\042default\042] = sinkt[\042keep-open\042]\012\012sink = choose(sinkt)\012\012sourcet[\042by-length\042] = function(sock, length)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function()\012 if length <= 0 then return nil end\012 local size = math.min(socket.BLOCKSIZE, length)\012 local chunk, err = sock:receive(size)\012 if err then return nil, err end\012 length = length - string.len(chunk)\012 return chunk\012 end\012 })\012end\012\012sourcet[\042until-closed\042] = function(sock)\012 local done\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function()\012 if done then return nil end\012 local chunk, err, partial = sock:receive(socket.BLOCKSIZE)\012 if not err then return chunk\012 elseif err == \042closed\042 then\012 sock:close()\012 done = 1\012 return partial\012 else return nil, err end\012 end\012 })\012end\012\012\012sourcet[\042default\042] = sourcet[\042until-closed\042]\012\012source = choose(sourcet)\012\012";
|
const char* socket_luac="-----------------------------------------------------------------------------\012-- LuaSocket helper module\012-- Author: Diego Nehab\012-- RCS ID: $Id: socket.lua,v 1.22 2005/11/22 08:33:29 diego Exp $\012-----------------------------------------------------------------------------\012\012-----------------------------------------------------------------------------\012-- Declare module and import dependencies\012-----------------------------------------------------------------------------\012local base = _G\012local string = require(\042string\042)\012local math = require(\042math\042)\012local socket = require(\042socket.core\042)\012module(\042socket\042)\012\012-----------------------------------------------------------------------------\012-- Exported auxiliar functions\012-----------------------------------------------------------------------------\012function connect(address, port, laddress, lport)\012 local sock, err = socket.tcp()\012 if not sock then return nil, err end\012 if laddress then\012 local res, err = sock:bind(laddress, lport, -1)\012 if not res then return nil, err end\012 end\012 local res, err = sock:connect(address, port)\012 if not res then return nil, err end\012 return sock\012end\012\012function bind(host, port, backlog)\012 local sock, err = socket.tcp()\012 if not sock then return nil, err end\012 sock:setoption(\042reuseaddr\042, true)\012 local res, err = sock:bind(host, port)\012 if not res then return nil, err end\012 res, err = sock:listen(backlog)\012 if not res then return nil, err end\012 return sock\012end\012\012try = newtry()\012\012function choose(table)\012 return function(name, opt1, opt2)\012 if base.type(name) ~= \042string\042 then\012 name, opt1, opt2 = \042default\042, name, opt1\012 end\012 local f = table[name or \042nil\042]\012 if not f then base.error(\042unknown key (\042.. base.tostring(name) ..\042)\042, 3)\012 else return f(opt1, opt2) end\012 end\012end\012\012-----------------------------------------------------------------------------\012-- Socket sources and sinks, conforming to LTN12\012-----------------------------------------------------------------------------\012-- create namespaces inside LuaSocket namespace\012sourcet = {}\012sinkt = {}\012\012BLOCKSIZE = 2048\012\012sinkt[\042close-when-done\042] = function(sock)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function(self, chunk, err)\012 if not chunk then\012 sock:close()\012 return 1\012 else return sock:send(chunk) end\012 end\012 })\012end\012\012sinkt[\042keep-open\042] = function(sock)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function(self, chunk, err)\012 if chunk then return sock:send(chunk)\012 else return 1 end\012 end\012 })\012end\012\012sinkt[\042default\042] = sinkt[\042keep-open\042]\012\012sink = choose(sinkt)\012\012sourcet[\042by-length\042] = function(sock, length)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function()\012 if length <= 0 then return nil end\012 local size = math.min(socket.BLOCKSIZE, length)\012 local chunk, err = sock:receive(size)\012 if err then return nil, err end\012 length = length - string.len(chunk)\012 return chunk\012 end\012 })\012end\012\012sourcet[\042until-closed\042] = function(sock)\012 local done\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function()\012 if done then return nil end\012 local chunk, err, partial = sock:receive(socket.BLOCKSIZE)\012 if not err then return chunk\012 elseif err == \042closed\042 then\012 sock:close()\012 done = 1\012 return partial\012 else return nil, err end\012 end\012 })\012end\012\012\012sourcet[\042default\042] = sourcet[\042until-closed\042]\012\012source = choose(sourcet)\012\012";
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
void luaopen_socket(lua_State *l);
|
void luaopen_socket(lua_State *l);
|
||||||
|
@ -35,7 +35,7 @@ static int meth_setfd(lua_State *L);
|
|||||||
static int meth_dirty(lua_State *L);
|
static int meth_dirty(lua_State *L);
|
||||||
|
|
||||||
/* tcp object methods */
|
/* tcp object methods */
|
||||||
static luaL_reg tcp[] = {
|
static luaL_Reg tcp[] = {
|
||||||
{"__gc", meth_close},
|
{"__gc", meth_close},
|
||||||
{"__tostring", auxiliar_tostring},
|
{"__tostring", auxiliar_tostring},
|
||||||
{"accept", meth_accept},
|
{"accept", meth_accept},
|
||||||
@ -70,7 +70,7 @@ static t_opt opt[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* functions in library namespace */
|
/* functions in library namespace */
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{"tcp", global_create},
|
{"tcp", global_create},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
*
|
*
|
||||||
* RCS ID: $Id: tcp.h,v 1.7 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: tcp.h,v 1.7 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
static int timeout_lua_gettime(lua_State *L);
|
static int timeout_lua_gettime(lua_State *L);
|
||||||
static int timeout_lua_sleep(lua_State *L);
|
static int timeout_lua_sleep(lua_State *L);
|
||||||
|
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{ "gettime", timeout_lua_gettime },
|
{ "gettime", timeout_lua_gettime },
|
||||||
{ "sleep", timeout_lua_sleep },
|
{ "sleep", timeout_lua_sleep },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
*
|
*
|
||||||
* RCS ID: $Id: timeout.h,v 1.14 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: timeout.h,v 1.14 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
/* timeout control structure */
|
/* timeout control structure */
|
||||||
typedef struct t_timeout_ {
|
typedef struct t_timeout_ {
|
||||||
|
@ -40,7 +40,7 @@ static int meth_setfd(lua_State *L);
|
|||||||
static int meth_dirty(lua_State *L);
|
static int meth_dirty(lua_State *L);
|
||||||
|
|
||||||
/* udp object methods */
|
/* udp object methods */
|
||||||
static luaL_reg udp[] = {
|
static luaL_Reg udp[] = {
|
||||||
{"__gc", meth_close},
|
{"__gc", meth_close},
|
||||||
{"__tostring", auxiliar_tostring},
|
{"__tostring", auxiliar_tostring},
|
||||||
{"close", meth_close},
|
{"close", meth_close},
|
||||||
@ -73,7 +73,7 @@ static t_opt opt[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* functions in library namespace */
|
/* functions in library namespace */
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{"udp", global_create},
|
{"udp", global_create},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
*
|
*
|
||||||
* RCS ID: $Id: udp.h,v 1.10 2005/10/07 04:40:59 diego Exp $
|
* RCS ID: $Id: udp.h,v 1.10 2005/10/07 04:40:59 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
@ -37,7 +37,7 @@ static const char *unix_tryconnect(p_unix un, const char *path);
|
|||||||
static const char *unix_trybind(p_unix un, const char *path);
|
static const char *unix_trybind(p_unix un, const char *path);
|
||||||
|
|
||||||
/* unix object methods */
|
/* unix object methods */
|
||||||
static luaL_reg un[] = {
|
static luaL_Reg un[] = {
|
||||||
{"__gc", meth_close},
|
{"__gc", meth_close},
|
||||||
{"__tostring", auxiliar_tostring},
|
{"__tostring", auxiliar_tostring},
|
||||||
{"accept", meth_accept},
|
{"accept", meth_accept},
|
||||||
@ -69,7 +69,7 @@ static t_opt opt[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* our socket creation function */
|
/* our socket creation function */
|
||||||
static luaL_reg func[] = {
|
static luaL_Reg func[] = {
|
||||||
{"unix", global_create},
|
{"unix", global_create},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
*
|
*
|
||||||
* RCS ID: $Id: unix.h,v 1.9 2006/03/13 07:16:39 diego Exp $
|
* RCS ID: $Id: unix.h,v 1.9 2006/03/13 07:16:39 diego Exp $
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "../luainc.h"
|
#include "../LuaCompat.h"
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
Reference in New Issue
Block a user