From 90cd9673331c61769ce524244d016be7c21e03cb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 22 May 2014 11:24:36 +0800 Subject: [PATCH 001/729] concurrence bugfix --- skynet-src/skynet_mq.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index c5d80f0b..d146fd56 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -34,7 +34,6 @@ struct global_queue { struct message_queue ** queue; // We use a separated flag array to ensure the mq is pushed. // See the comments below. - bool * flag; struct message_queue *list; }; @@ -49,7 +48,10 @@ static void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; - if (q->flag[GP(q->tail)]) { + uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1)); + + // only one thread can set the slot (change q->queue[tail] from NULL to queue) + if (!__sync_bool_compare_and_swap(&q->queue[tail], NULL, queue)) { // The queue may full seldom, save queue in list assert(queue->next == NULL); struct message_queue * last; @@ -60,14 +62,6 @@ skynet_globalmq_push(struct message_queue * queue) { return; } - - uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1)); - // The thread would suspend here, and the q->queue[tail] is last version , - // but the queue tail is increased. - // So we set q->flag[tail] after changing q->queue[tail]. - q->queue[tail] = queue; - __sync_synchronize(); - q->flag[tail] = true; } struct message_queue * @@ -93,18 +87,14 @@ skynet_globalmq_pop() { } } - // Check the flag first, if the flag is false, the pushing may not complete. - if(!q->flag[head_ptr]) { - return NULL; - } - - __sync_synchronize(); - struct message_queue * mq = q->queue[head_ptr]; if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) { return NULL; } - q->flag[head_ptr] = false; + // only one thread can get the slot (change q->queue[head_ptr] to NULL) + if (!__sync_bool_compare_and_swap(&q->queue[head_ptr], mq, NULL)) { + return NULL; + } return mq; } @@ -220,8 +210,7 @@ skynet_mq_init() { struct global_queue *q = skynet_malloc(sizeof(*q)); memset(q,0,sizeof(*q)); q->queue = skynet_malloc(MAX_GLOBAL_MQ * sizeof(struct message_queue *)); - q->flag = skynet_malloc(MAX_GLOBAL_MQ * sizeof(bool)); - memset(q->flag, 0, sizeof(bool) * MAX_GLOBAL_MQ); + memset(q->queue, 0, sizeof(struct message_queue *) * MAX_GLOBAL_MQ); Q=q; } From 88700c2c9095a74100754e13ffabaf321168db43 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 22 May 2014 21:04:32 +0800 Subject: [PATCH 002/729] cluster support --- HISTORY.md | 1 + Makefile | 7 +- examples/cluster1.lua | 8 ++ examples/cluster2.lua | 6 ++ examples/clustername.lua | 1 + examples/config.c1 | 12 +++ examples/config.c2 | 12 +++ lualib-src/lua-cluster.c | 205 +++++++++++++++++++++++++++++++++++++++ lualib-src/lua-socket.c | 20 +++- lualib/cluster.lua | 19 ++++ service/clusterd.lua | 69 +++++++++++++ 11 files changed, 357 insertions(+), 3 deletions(-) create mode 100644 examples/cluster1.lua create mode 100644 examples/cluster2.lua create mode 100644 examples/clustername.lua create mode 100644 examples/config.c1 create mode 100644 examples/config.c2 create mode 100644 lualib-src/lua-cluster.c create mode 100644 lualib/cluster.lua create mode 100644 service/clusterd.lua diff --git a/HISTORY.md b/HISTORY.md index 07a4c501..aa8ebfa0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ Dev version ----------- * Bugfix: update lua-bson (signed 32bit int bug) +* cluster support v0.2.1 (2014-5-19) ----------- diff --git a/Makefile b/Makefile index d7bb53cf..e6943b3f 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,9 @@ jemalloc : $(MALLOC_STATICLIB) # skynet CSERVICE = snlua logger gate master harbor -LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack cjson clientsocket memory profile multicast +LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ + cjson clientsocket memory profile multicast \ + cluster SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -105,6 +107,9 @@ $(LUA_CLIB_PATH)/profile.so : lualib-src/lua-profile.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ +$(LUA_CLIB_PATH)/cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ + clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so diff --git a/examples/cluster1.lua b/examples/cluster1.lua new file mode 100644 index 00000000..8a67ebc6 --- /dev/null +++ b/examples/cluster1.lua @@ -0,0 +1,8 @@ +local skynet = require "skynet" +local cluster = require "cluster" + +skynet.start(function() + skynet.newservice("simpledb") + skynet.call("SIMPLEDB", "lua", "SET", "a", "foobar") + cluster.open(2528) +end) diff --git a/examples/cluster2.lua b/examples/cluster2.lua new file mode 100644 index 00000000..a61ff5c4 --- /dev/null +++ b/examples/cluster2.lua @@ -0,0 +1,6 @@ +local skynet = require "skynet" +local cluster = require "cluster" + +skynet.start(function() + print(cluster.call("db", "SIMPLEDB", "GET", "a")) +end) diff --git a/examples/clustername.lua b/examples/clustername.lua new file mode 100644 index 00000000..c5ed5e69 --- /dev/null +++ b/examples/clustername.lua @@ -0,0 +1 @@ +db = "127.0.0.1:2528" diff --git a/examples/config.c1 b/examples/config.c1 new file mode 100644 index 00000000..3e1307bd --- /dev/null +++ b/examples/config.c1 @@ -0,0 +1,12 @@ +thread = 8 +logger = nil +harbor = 1 +address = "127.0.0.1:2526" +master = "127.0.0.1:2013" +start = "cluster1" +bootstrap = "snlua bootstrap" -- The service for bootstrap +standalone = "0.0.0.0:2013" +luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" +lualoader = "lualib/loader.lua" +cpath = "./cservice/?.so" +cluster = "./examples/clustername.lua" \ No newline at end of file diff --git a/examples/config.c2 b/examples/config.c2 new file mode 100644 index 00000000..080ca137 --- /dev/null +++ b/examples/config.c2 @@ -0,0 +1,12 @@ +thread = 8 +logger = nil +harbor = 1 +address = "127.0.0.1:2527" +master = "127.0.0.1:2014" +start = "cluster2" +bootstrap = "snlua bootstrap" -- The service for bootstrap +standalone = "0.0.0.0:2014" +luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" +lualoader = "lualib/loader.lua" +cpath = "./cservice/?.so" +cluster = "./examples/clustername.lua" \ No newline at end of file diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c new file mode 100644 index 00000000..b3b8a9d1 --- /dev/null +++ b/lualib-src/lua-cluster.c @@ -0,0 +1,205 @@ +#include +#include +#include + +#include "skynet.h" + +/* + uint32_t/string addr + uint32_t/session session + lightuserdata msg + uint32_t sz + + return + string request + uint32_t next_session + */ + +#define TEMP_LENGTH 0x10002 + +static void +fill_uint32(uint8_t * buf, uint32_t n) { + buf[0] = n & 0xff; + buf[1] = (n >> 8) & 0xff; + buf[2] = (n >> 16) & 0xff; + buf[3] = (n >> 24) & 0xff; +} + +static void +fill_header(lua_State *L, uint8_t *buf, int sz, void *msg) { + if (sz >= 0x10000) { + skynet_free(msg); + luaL_error(L, "request message is too long %d", sz); + } + buf[0] = (sz >> 8) & 0xff; + buf[1] = sz & 0xff; +} + +static void +packreq_number(lua_State *L, int session, void * msg, size_t sz) { + uint32_t addr = lua_tounsigned(L,1); + uint8_t buf[TEMP_LENGTH]; + fill_header(L, buf, sz+9, msg); + buf[2] = 0; + fill_uint32(buf+3, addr); + fill_uint32(buf+7, (uint32_t)session); + memcpy(buf+11,msg,sz); + + lua_pushlstring(L, (const char *)buf, sz+11); +} + +static void +packreq_string(lua_State *L, int session, void * msg, size_t sz) { + size_t namelen = 0; + const char *name = lua_tolstring(L, 1, &namelen); + if (name == NULL || namelen < 1 || namelen > 255) { + skynet_free(msg); + luaL_error(L, "name is too long %s", name); + } + + uint8_t buf[TEMP_LENGTH]; + fill_header(L, buf, sz+5+namelen, msg); + buf[2] = (uint8_t)namelen; + memcpy(buf+3, name, namelen); + fill_uint32(buf+3+namelen, (uint32_t)session); + memcpy(buf+7+namelen,msg,sz); + + lua_pushlstring(L, (const char *)buf, sz+7+namelen); +} + +static int +lpackrequest(lua_State *L) { + void *msg = lua_touserdata(L,3); + if (msg == NULL) { + return luaL_error(L, "Invalid request message"); + } + size_t sz = luaL_checkunsigned(L,4); + int session = luaL_checkinteger(L,2); + if (session <= 0) { + return luaL_error(L, "Invalid request session %d", session); + } + int addr_type = lua_type(L,1); + if (addr_type == LUA_TNUMBER) { + packreq_number(L, session, msg, sz); + } else { + packreq_string(L, session, msg, sz); + } + if (++session < 0) { + session = 1; + } + skynet_free(msg); + lua_pushinteger(L, session); + return 2; +} + +/* + string packed message + return + uint32_t or string addr + int session + string msg + */ + +static inline uint32_t +unpack_uint32(const uint8_t * buf) { + return buf[0] | buf[1]<<8 | buf[2]<<16 | buf[3]<<24; +} + +static int +unpackreq_number(lua_State *L, const uint8_t * buf, size_t sz) { + if (sz < 9) { + return luaL_error(L, "Invalid cluster message"); + } + uint32_t address = unpack_uint32(buf+1); + uint32_t session = unpack_uint32(buf+5); + lua_pushunsigned(L, address); + lua_pushunsigned(L, session); + lua_pushlstring(L, (const char *)buf+9, sz-9); + + return 3; +} + +static int +unpackreq_string(lua_State *L, const uint8_t * buf, size_t sz) { + size_t namesz = buf[0]; + if (sz < namesz + 5) { + return luaL_error(L, "Invalid cluster message"); + } + lua_pushlstring(L, (const char *)buf+1, namesz); + uint32_t session = unpack_uint32(buf + namesz + 1); + lua_pushunsigned(L, session); + lua_pushlstring(L, (const char *)buf+1+namesz+4, sz - namesz - 5); + + return 3; +} + +static int +lunpackrequest(lua_State *L) { + size_t sz; + const char *msg = luaL_checklstring(L,1,&sz); + if (msg[0] == 0) { + return unpackreq_number(L, (const uint8_t *)msg, sz); + } else { + return unpackreq_string(L, (const uint8_t *)msg, sz); + } +} + +/* + int session + lightuserdata msg + int sz + return string response + */ +static int +lpackresponse(lua_State *L) { + uint32_t session = luaL_checkunsigned(L,1); + void * msg = lua_touserdata(L,2); + size_t sz = luaL_checkunsigned(L, 3); + + uint8_t buf[TEMP_LENGTH]; + fill_header(L, buf, sz+4, msg); + fill_uint32(buf+2, session); + memcpy(buf+6,msg,sz); + + skynet_free(msg); + + lua_pushlstring(L, (const char *)buf, sz+6); + + return 1; +} + +/* + string packed response + return integer session + boolean ok + string msg + */ +static int +lunpackresponse(lua_State *L) { + size_t sz; + const char * buf = luaL_checklstring(L, 1, &sz); + if (sz < 4) { + return 0; + } + uint32_t session = unpack_uint32((const uint8_t *)buf); + lua_pushunsigned(L, session); + lua_pushboolean(L, 1); + lua_pushlstring(L, buf+4, sz-4); + + return 3; +} + +int +luaopen_cluster_c(lua_State *L) { + luaL_Reg l[] = { + { "packrequest", lpackrequest }, + { "unpackrequest", lunpackrequest }, + { "packresponse", lpackresponse }, + { "unpackresponse", lunpackresponse }, + { NULL, NULL }, + }; + luaL_checkversion(L); + luaL_newlib(L,l); + + return 1; +} diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 3f22956c..2428914e 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -191,7 +191,7 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) { /* userdata send_buffer table pool - integer sz + integer sz or string (big endian) */ static int lpopbuffer(lua_State *L) { @@ -200,7 +200,23 @@ lpopbuffer(lua_State *L) { return luaL_error(L, "Need buffer object at param 1"); } luaL_checktype(L,2,LUA_TTABLE); - int sz = luaL_checkinteger(L,3); + int type = lua_type(L,3); + int sz; + if (type == LUA_TNUMBER) { + sz = lua_tointeger(L,3); + } else { + size_t len; + const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 3, &len); + if (len > 4 || len < 1) { + return luaL_error(L, "Invalid read %s", s); + } + int i; + sz = 0; + for (i=0;i<(int)len;i++) { + sz <<= 8; + sz |= s[i]; + } + } if (sb->size < sz || sz == 0) { lua_pushnil(L); } else { diff --git a/lualib/cluster.lua b/lualib/cluster.lua new file mode 100644 index 00000000..e4f6cfa2 --- /dev/null +++ b/lualib/cluster.lua @@ -0,0 +1,19 @@ +local skynet = require "skynet" + +local clusterd +local cluster = {} + +function cluster.call(node, address, ...) + -- skynet.pack(...) will free by cluster.c.packrequest + return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...)) +end + +function cluster.open(port) + skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) +end + +skynet.init(function() + clusterd = skynet.uniqueservice("clusterd") +end) + +return cluster diff --git a/service/clusterd.lua b/service/clusterd.lua new file mode 100644 index 00000000..ef6bff68 --- /dev/null +++ b/service/clusterd.lua @@ -0,0 +1,69 @@ +local skynet = require "skynet" +local sc = require "socketchannel" +local socket = require "socket" +local cluster = require "cluster.c" + +local config_name = skynet.getenv "cluster" +local node_address = {} +assert(loadfile(config_name, "t", node_address))() +local node_session = {} +local command = {} + +local function read_response(sock) + local sz = sock:read(2) + local msg = sock:read(sz) + return cluster.unpackresponse(msg) -- session, ok, data +end + +local function open_channel(t, key) + local host, port = string.match(node_address[key], "([^:]+):(.*)$") + local c = sc.channel { + host = host, + port = tonumber(port), + response = read_response, + } + assert(c:connect(true)) + t[key] = c + node_session[key] = 1 + return c +end + +local node_channel = setmetatable({}, { __index = open_channel }) + +function command.listen(source, addr, port) + local gate = skynet.newservice("gate") + skynet.call(gate, "lua", "open", { address = addr, port = port }) + skynet.ret(skynet.pack(nil)) +end + +function command.req(source, node, addr, msg, sz) + local request + local c = node_channel[node] + local session = node_session[node] + -- msg is a local pointer, cluster.packrequest will free it + request, node_session[node] = cluster.packrequest(addr, session , msg, sz) + skynet.ret(c:request(request, session)) +end + +local request_fd = {} + +function command.socket(source, subcmd, fd, msg) + if subcmd == "data" then + local addr, session, msg = cluster.unpackrequest(msg) + local msg, sz = skynet.rawcall(addr, "lua", msg) + local response = cluster.packresponse(session, msg, sz) + socket.write(fd, response) + elseif subcmd == "open" then + skynet.error(string.format("socket accept from %s", msg)) + skynet.call(source, "lua", "accept", fd) + else + skynet.error(string.format("socket %s %d : %s", subcmd, fd, msg)) + end +end + +skynet.start(function() + skynet.dispatch("lua", function(_, source, cmd, ...) + local f = assert(command[cmd]) + f(source, ...) + end) +end) From 37ea7a025c026c13a4e6d1986b18d2712468f12c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 22 May 2014 22:44:20 +0800 Subject: [PATCH 003/729] single node mode --- Makefile | 2 +- examples/cluster1.lua | 3 +- examples/config.c1 | 5 +- examples/config.c2 | 5 +- service-src/service_dummy.c | 292 +++++++++++++++++++++++++++++++++++ service-src/service_logger.c | 2 +- service/bootstrap.lua | 23 ++- skynet-src/skynet_harbor.c | 8 +- 8 files changed, 318 insertions(+), 22 deletions(-) create mode 100644 service-src/service_dummy.c diff --git a/Makefile b/Makefile index e6943b3f..35f6626d 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ jemalloc : $(MALLOC_STATICLIB) # skynet -CSERVICE = snlua logger gate master harbor +CSERVICE = snlua logger gate master harbor dummy LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ cjson clientsocket memory profile multicast \ cluster diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 8a67ebc6..73fad761 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -3,6 +3,7 @@ local cluster = require "cluster" skynet.start(function() skynet.newservice("simpledb") - skynet.call("SIMPLEDB", "lua", "SET", "a", "foobar") + print(skynet.call("SIMPLEDB", "lua", "SET", "a", "foobar")) + print(skynet.call("SIMPLEDB", "lua", "GET", "a")) cluster.open(2528) end) diff --git a/examples/config.c1 b/examples/config.c1 index 3e1307bd..d1fdf165 100644 --- a/examples/config.c1 +++ b/examples/config.c1 @@ -1,11 +1,8 @@ thread = 8 logger = nil -harbor = 1 -address = "127.0.0.1:2526" -master = "127.0.0.1:2013" +harbor = 0 start = "cluster1" bootstrap = "snlua bootstrap" -- The service for bootstrap -standalone = "0.0.0.0:2013" luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" diff --git a/examples/config.c2 b/examples/config.c2 index 080ca137..2e8cbb0b 100644 --- a/examples/config.c2 +++ b/examples/config.c2 @@ -1,11 +1,8 @@ thread = 8 logger = nil -harbor = 1 -address = "127.0.0.1:2527" -master = "127.0.0.1:2014" +harbor = 0 start = "cluster2" bootstrap = "snlua bootstrap" -- The service for bootstrap -standalone = "0.0.0.0:2014" luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" diff --git a/service-src/service_dummy.c b/service-src/service_dummy.c new file mode 100644 index 00000000..006a276f --- /dev/null +++ b/service-src/service_dummy.c @@ -0,0 +1,292 @@ +#include "skynet.h" +#include "skynet_harbor.h" + +#include +#include + +#define HASH_SIZE 4096 +#define DEFAULT_QUEUE_SIZE 1024 + +struct msg { + uint8_t * buffer; + size_t size; +}; + +struct msg_queue { + int size; + int head; + int tail; + struct msg * data; +}; + +struct keyvalue { + struct keyvalue * next; + char key[GLOBALNAME_LENGTH]; + uint32_t hash; + uint32_t value; + struct msg_queue * queue; +}; + +struct hashmap { + struct keyvalue *node[HASH_SIZE]; +}; + +/* + message type (8bits) is in destination high 8bits + harbor id (8bits) is also in that place , but remote message doesn't need harbor id. + */ +struct remote_message_header { + uint32_t source; + uint32_t destination; + uint32_t session; +}; + +// 12 is sizeof(struct remote_message_header) +#define HEADER_COOKIE_LENGTH 12 + +struct dummy { + struct skynet_context *ctx; + struct hashmap * map; +}; + +// hash table + +static void +_push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct remote_message_header * header) { + // If there is only 1 free slot which is reserved to distinguish full/empty + // of circular buffer, expand it. + if (((queue->tail + 1) % queue->size) == queue->head) { + struct msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct msg)); + int i; + for (i=0;isize-1;i++) { + new_buffer[i] = queue->data[(i+queue->head) % queue->size]; + } + skynet_free(queue->data); + queue->data = new_buffer; + queue->head = 0; + queue->tail = queue->size - 1; + queue->size *= 2; + } + struct msg * slot = &queue->data[queue->tail]; + queue->tail = (queue->tail + 1) % queue->size; + + slot->buffer = skynet_malloc(sz + sizeof(*header)); + memcpy(slot->buffer, buffer, sz); + memcpy(slot->buffer + sz, header, sizeof(*header)); + slot->size = sz + sizeof(*header); +} + +static struct msg * +_pop_queue(struct msg_queue * queue) { + if (queue->head == queue->tail) { + return NULL; + } + struct msg * slot = &queue->data[queue->head]; + queue->head = (queue->head + 1) % queue->size; + return slot; +} + +static struct msg_queue * +_new_queue() { + struct msg_queue * queue = skynet_malloc(sizeof(*queue)); + queue->size = DEFAULT_QUEUE_SIZE; + queue->head = 0; + queue->tail = 0; + queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct msg)); + + return queue; +} + +static void +_release_queue(struct msg_queue *queue) { + if (queue == NULL) + return; + struct msg * m = _pop_queue(queue); + while (m) { + skynet_free(m->buffer); + m = _pop_queue(queue); + } + skynet_free(queue->data); + skynet_free(queue); +} + +static struct keyvalue * +_hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { + uint32_t *ptr = (uint32_t*) name; + uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; + struct keyvalue * node = hash->node[h % HASH_SIZE]; + while (node) { + if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) { + return node; + } + node = node->next; + } + return NULL; +} + +static struct keyvalue * +_hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { + uint32_t *ptr = (uint32_t *)name; + uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; + struct keyvalue ** pkv = &hash->node[h % HASH_SIZE]; + struct keyvalue * node = skynet_malloc(sizeof(*node)); + memcpy(node->key, name, GLOBALNAME_LENGTH); + node->next = *pkv; + node->queue = NULL; + node->hash = h; + node->value = 0; + *pkv = node; + + return node; +} + +static struct hashmap * +_hash_new() { + struct hashmap * h = skynet_malloc(sizeof(struct hashmap)); + memset(h,0,sizeof(*h)); + return h; +} + +static void +_hash_delete(struct hashmap *hash) { + int i; + for (i=0;inode[i]; + while (node) { + struct keyvalue * next = node->next; + _release_queue(node->queue); + skynet_free(node); + node = next; + } + } + skynet_free(hash); +} + +/////////////// + +struct dummy * +dummy_create(void) { + struct dummy * d = skynet_malloc(sizeof(*d)); + d->map = _hash_new(); + return d; +} + +void +dummy_release(struct dummy *d) { + _hash_delete(d->map); + skynet_free(d); +} + +static inline void +to_bigendian(uint8_t *buffer, uint32_t n) { + buffer[0] = (n >> 24) & 0xff; + buffer[1] = (n >> 16) & 0xff; + buffer[2] = (n >> 8) & 0xff; + buffer[3] = n & 0xff; +} + +static inline void +_header_to_message(const struct remote_message_header * header, uint8_t * message) { + to_bigendian(message , header->source); + to_bigendian(message+4 , header->destination); + to_bigendian(message+8 , header->session); +} + +static inline uint32_t +from_bigendian(uint32_t n) { + union { + uint32_t big; + uint8_t bytes[4]; + } u; + u.big = n; + return u.bytes[0] << 24 | u.bytes[1] << 16 | u.bytes[2] << 8 | u.bytes[3]; +} + +static inline void +_message_to_header(const uint32_t *message, struct remote_message_header *header) { + header->source = from_bigendian(message[0]); + header->destination = from_bigendian(message[1]); + header->session = from_bigendian(message[2]); +} + +static void +_dispatch_queue(struct dummy *h, struct msg_queue * queue, uint32_t handle, const char name[GLOBALNAME_LENGTH] ) { + struct msg * m = _pop_queue(queue); + while (m) { + struct remote_message_header cookie; + uint8_t *ptr = m->buffer + m->size - sizeof(cookie); + memcpy(&cookie, ptr, sizeof(cookie)); + int type = cookie.destination >> HANDLE_REMOTE_SHIFT; + skynet_send(h->ctx, cookie.source, handle , type | PTYPE_TAG_DONTCOPY, cookie.session, m->buffer, m->size - sizeof(cookie)); + m = _pop_queue(queue); + } +} + +static void +_update_name(struct dummy *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { + struct keyvalue * node = _hash_search(h->map, name); + if (node == NULL) { + node = _hash_insert(h->map, name); + } + node->value = handle; + if (node->queue) { + _dispatch_queue(h, node->queue, handle, name); + _release_queue(node->queue); + node->queue = NULL; + } +} + +static void +_send_name(struct dummy *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) { + struct keyvalue * node = _hash_search(h->map, name); + if (node == NULL) { + node = _hash_insert(h->map, name); + } + if (node->value == 0) { + if (node->queue == NULL) { + node->queue = _new_queue(); + } + struct remote_message_header header; + header.source = source; + header.destination = type << HANDLE_REMOTE_SHIFT; + header.session = (uint32_t)session; + _push_queue(node->queue, msg, sz, &header); + } else { + // local message + skynet_send(h->ctx, source, node->value , type | PTYPE_TAG_DONTCOPY, session, (void *)msg, sz); + } +} + +static int +_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { + struct dummy * h = ud; + switch (type) { + case PTYPE_SYSTEM: { + // register name message + const struct remote_message *rmsg = msg; + assert (sz == sizeof(rmsg->destination)); + _update_name(h, rmsg->destination.name, rmsg->destination.handle); + return 0; + } + default: { + // remote message out + const struct remote_message *rmsg = msg; + if (rmsg->destination.handle == 0) { + _send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz); + } else { + // local message + skynet_send(context, source, rmsg->destination.handle , type | PTYPE_TAG_DONTCOPY, session, (void *)rmsg->message, rmsg->sz); + } + return 0; + } + } +} + +int +dummy_init(struct dummy *d, struct skynet_context *ctx, const char * args) { + d->ctx = ctx; + skynet_harbor_start(ctx); + skynet_callback(ctx, d, _mainloop); + + return 0; +} diff --git a/service-src/service_logger.c b/service-src/service_logger.c index c78cf4d6..122cc77f 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -28,7 +28,7 @@ logger_release(struct logger * inst) { static int _logger(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct logger * inst = ud; - fprintf(inst->handle, "[:%x] ",source); + fprintf(inst->handle, "[:%08x] ",source); fwrite(msg, sz , 1, inst->handle); fprintf(inst->handle, "\n"); fflush(inst->handle); diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 51a57999..7c92b419 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -4,17 +4,24 @@ skynet.start(function() assert(skynet.launch("logger", skynet.getenv "logger")) local standalone = skynet.getenv "standalone" - local master_addr = skynet.getenv "master" + local harbor_id = tonumber(skynet.getenv "harbor") + if harbor_id == 0 then + assert(standalone == nil) + standalone = true + skynet.setenv("standalone", "true") + assert(skynet.launch("dummy")) + else + local master_addr = skynet.getenv "master" - if standalone then - assert(skynet.launch("master", master_addr)) + if standalone then + assert(skynet.launch("master", master_addr)) + end + + local local_addr = skynet.getenv "address" + + assert(skynet.launch("harbor",master_addr, local_addr, harbor_id)) end - local local_addr = skynet.getenv "address" - local harbor_id = skynet.getenv "harbor" - - assert(skynet.launch("harbor",master_addr, local_addr, harbor_id)) - local launcher = assert(skynet.launch("snlua","launcher")) skynet.name(".launcher", launcher) diff --git a/skynet-src/skynet_harbor.c b/skynet-src/skynet_harbor.c index ae47cf45..160b1994 100644 --- a/skynet-src/skynet_harbor.c +++ b/skynet-src/skynet_harbor.c @@ -7,18 +7,20 @@ #include static struct skynet_context * REMOTE = 0; -static unsigned int HARBOR = 0; +static unsigned int HARBOR = ~0; void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) { int type = rmsg->sz >> HANDLE_REMOTE_SHIFT; rmsg->sz &= HANDLE_MASK; - assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR); + assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR && REMOTE); skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, type , session); } void skynet_harbor_register(struct remote_name *rname) { + if (REMOTE == NULL) + return; int i; int number = 1; for (i=0;i Date: Fri, 23 May 2014 10:54:54 +0800 Subject: [PATCH 004/729] daemon mode --- HISTORY.md | 3 ++- examples/config | 1 + skynet-src/skynet_imp.h | 1 + skynet-src/skynet_main.c | 10 ++-------- skynet-src/skynet_start.c | 6 ++++++ 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index aa8ebfa0..27927ea5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,7 +1,8 @@ Dev version ----------- * Bugfix: update lua-bson (signed 32bit int bug) -* cluster support +* Add cluster support +* Add daemon mode v0.2.1 (2014-5-19) ----------- diff --git a/examples/config b/examples/config index f89af28b..f759d57f 100644 --- a/examples/config +++ b/examples/config @@ -12,3 +12,4 @@ lualoader = "lualib/loader.lua" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so" +daemon = false diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index 81fad4c8..afad0305 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -4,6 +4,7 @@ struct skynet_config { int thread; int harbor; + int daemon; const char * module_path; const char * bootstrap; }; diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 3b8cf1b2..49f5ed3c 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -24,7 +24,6 @@ optint(const char *key, int opt) { return strtol(str, NULL, 10); } -/* static int optboolean(const char *key, int opt) { const char * str = skynet_getenv(key); @@ -34,7 +33,7 @@ optboolean(const char *key, int opt) { } return strcmp(str,"true")==0; } -*/ + static const char * optstring(const char *key,const char * opt) { const char * str = skynet_getenv(key); @@ -109,21 +108,16 @@ main(int argc, char *argv[]) { } _init_env(L); -#ifdef LUA_CACHELIB - printf("Skynet lua code cache enable\n"); -#endif - config.thread = optint("thread",8); config.module_path = optstring("cpath","./cservice/?.so"); config.harbor = optint("harbor", 1); config.bootstrap = optstring("bootstrap","snlua bootstrap"); + config.daemon = optboolean("daemon", 0); lua_close(L); skynet_start(&config); skynet_globalexit(); - printf("skynet exit\n"); - return 0; } diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index d1f80e10..91b4f224 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -195,6 +195,12 @@ bootstrap(const char * cmdline) { void skynet_start(struct skynet_config * config) { + if (config->daemon) { + if (daemon(1,0)) { + fprintf(stderr, "daemon error"); + exit(1); + } + } skynet_harbor_init(config->harbor); skynet_handle_init(config->harbor); skynet_mq_init(); From 8005b55ef5356b2c4b12274f2b5a061d708630b1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 23 May 2014 13:24:02 +0800 Subject: [PATCH 005/729] add pidfile for daemon --- .gitignore | 1 + Makefile | 2 +- examples/config | 2 +- skynet-src/skynet_daemon.c | 97 ++++++++++++++++++++++++++++++++++++++ skynet-src/skynet_daemon.h | 7 +++ skynet-src/skynet_imp.h | 2 +- skynet-src/skynet_main.c | 4 +- skynet-src/skynet_start.c | 7 ++- 8 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 skynet-src/skynet_daemon.c create mode 100644 skynet-src/skynet_daemon.h diff --git a/.gitignore b/.gitignore index 9c452a72..2cbccc98 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.o *.a skynet +skynet.pid 3rd/lua/lua 3rd/lua/luac cservice diff --git a/Makefile b/Makefile index 35f6626d..9c84351d 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \ - malloc_hook.c + malloc_hook.c skynet_daemon.c all : \ $(SKYNET_BUILD_PATH)/skynet \ diff --git a/examples/config b/examples/config index f759d57f..2f621b83 100644 --- a/examples/config +++ b/examples/config @@ -12,4 +12,4 @@ lualoader = "lualib/loader.lua" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so" -daemon = false +daemon = "./skynet.pid" diff --git a/skynet-src/skynet_daemon.c b/skynet-src/skynet_daemon.c new file mode 100644 index 00000000..6a557b76 --- /dev/null +++ b/skynet-src/skynet_daemon.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +#include "skynet_daemon.h" + +static int +check_pid(const char *pidfile) { + int pid = 0; + FILE *f = fopen(pidfile,"r"); + if (f == NULL) + return 0; + int n = fscanf(f,"%d", &pid); + fclose(f); + + if (n !=1 || pid == 0 || pid == getpid()) { + return 0; + } + + if (kill(pid, 0) && errno == ESRCH) + return 0; + + return pid; +} + +static int +write_pid(const char *pidfile) { + FILE *f; + int pid = 0; + int fd = open(pidfile, O_RDWR|O_CREAT, 0644); + if (fd == -1) { + fprintf(stderr, "Can't create %s.\n", pidfile); + return 0; + } + f = fdopen(fd, "r+"); + if (f == NULL) { + fprintf(stderr, "Can't open %s.\n", pidfile); + return 0; + } + + if (flock(fd, LOCK_EX|LOCK_NB) == -1) { + int n = fscanf(f, "%d", &pid); + fclose(f); + if (n != 1) { + fprintf(stderr, "Can't lock and read pidfile.\n"); + } else { + fprintf(stderr, "Can't lock pidfile, lock is held by pid %d.\n", pid); + } + return 0; + } + + pid = getpid(); + if (!fprintf(f,"%d\n", pid)) { + fprintf(stderr, "Can't write pid.\n"); + close(fd); + return 0; + } + fflush(f); + + if (flock(fd, LOCK_UN) == -1) { + fprintf(stderr, "Can't unlock pidfile %s.\n", pidfile); + close(fd); + return 0; + } + close(fd); + + return pid; +} + +int +daemon_init(const char *pidfile) { + int pid = check_pid(pidfile); + if (pid) { + fprintf(stderr, "Skynet is already running, pid = %d.\n", pid); + return 1; + } + + if (daemon(1,0)) { + fprintf(stderr, "Can't daemonize.\n"); + return 1; + } + + pid = write_pid(pidfile); + if (pid == 0) { + return 1; + } + + return 0; +} + +int +daemon_exit(const char *pidfile) { + return unlink (pidfile); +} diff --git a/skynet-src/skynet_daemon.h b/skynet-src/skynet_daemon.h new file mode 100644 index 00000000..0d99562a --- /dev/null +++ b/skynet-src/skynet_daemon.h @@ -0,0 +1,7 @@ +#ifndef skynet_daemon_h +#define skynet_daemon_h + +int daemon_init(const char *pidfile); +int daemon_exit(const char *pidfile); + +#endif diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index afad0305..11b12ee1 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -4,7 +4,7 @@ struct skynet_config { int thread; int harbor; - int daemon; + const char * daemon; const char * module_path; const char * bootstrap; }; diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 49f5ed3c..8596c778 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -24,6 +24,7 @@ optint(const char *key, int opt) { return strtol(str, NULL, 10); } +/* static int optboolean(const char *key, int opt) { const char * str = skynet_getenv(key); @@ -33,6 +34,7 @@ optboolean(const char *key, int opt) { } return strcmp(str,"true")==0; } +*/ static const char * optstring(const char *key,const char * opt) { @@ -112,7 +114,7 @@ main(int argc, char *argv[]) { config.module_path = optstring("cpath","./cservice/?.so"); config.harbor = optint("harbor", 1); config.bootstrap = optstring("bootstrap","snlua bootstrap"); - config.daemon = optboolean("daemon", 0); + config.daemon = optstring("daemon", NULL); lua_close(L); diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 91b4f224..09ab7711 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -7,6 +7,7 @@ #include "skynet_timer.h" #include "skynet_monitor.h" #include "skynet_socket.h" +#include "skynet_daemon.h" #include #include @@ -196,8 +197,7 @@ bootstrap(const char * cmdline) { void skynet_start(struct skynet_config * config) { if (config->daemon) { - if (daemon(1,0)) { - fprintf(stderr, "daemon error"); + if (daemon_init(config->daemon)) { exit(1); } } @@ -212,4 +212,7 @@ skynet_start(struct skynet_config * config) { _start(config->thread); skynet_socket_free(); + if (config->daemon) { + daemon_exit(config->daemon); + } } From e765d3c3052b65fec071cf6f0625f43b9b68965c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 23 May 2014 14:06:41 +0800 Subject: [PATCH 006/729] don't unlock pidfile after write pid --- skynet-src/skynet_daemon.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/skynet-src/skynet_daemon.c b/skynet-src/skynet_daemon.c index 6a557b76..d77a67a2 100644 --- a/skynet-src/skynet_daemon.c +++ b/skynet-src/skynet_daemon.c @@ -60,19 +60,13 @@ write_pid(const char *pidfile) { } fflush(f); - if (flock(fd, LOCK_UN) == -1) { - fprintf(stderr, "Can't unlock pidfile %s.\n", pidfile); - close(fd); - return 0; - } - close(fd); - return pid; } int daemon_init(const char *pidfile) { int pid = check_pid(pidfile); + if (pid) { fprintf(stderr, "Skynet is already running, pid = %d.\n", pid); return 1; @@ -93,5 +87,5 @@ daemon_init(const char *pidfile) { int daemon_exit(const char *pidfile) { - return unlink (pidfile); + return unlink(pidfile); } From 0fcab96431712c5f8fe0cf73526aa524253bc263 Mon Sep 17 00:00:00 2001 From: Cloud Date: Fri, 23 May 2014 16:52:24 +0800 Subject: [PATCH 007/729] daemon is deprecarted in macosx --- skynet-src/skynet_daemon.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/skynet-src/skynet_daemon.c b/skynet-src/skynet_daemon.c index d77a67a2..4c5e3882 100644 --- a/skynet-src/skynet_daemon.c +++ b/skynet-src/skynet_daemon.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "skynet_daemon.h" @@ -72,10 +73,14 @@ daemon_init(const char *pidfile) { return 1; } +#ifdef __APPLE__ + fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n"); +#else if (daemon(1,0)) { fprintf(stderr, "Can't daemonize.\n"); return 1; } +#endif pid = write_pid(pidfile); if (pid == 0) { From f3b1f5be510259880c35691ba873564e1ccb6bc2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 26 May 2014 13:18:09 +0800 Subject: [PATCH 008/729] bugfix: check globalmq push is complete --- skynet-src/skynet_mq.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index d146fd56..f4814450 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -88,6 +88,10 @@ skynet_globalmq_pop() { } struct message_queue * mq = q->queue[head_ptr]; + if (mq == NULL) { + // globalmq push not complete + return NULL; + } if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) { return NULL; } From 866e59811eeb327544b9996155fe5e45fd8067e8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 26 May 2014 13:23:57 +0800 Subject: [PATCH 009/729] remove unused mq api --- examples/config | 2 +- skynet-src/skynet_mq.c | 19 ++----------------- skynet-src/skynet_mq.h | 4 +--- skynet-src/skynet_server.c | 4 ++-- 4 files changed, 6 insertions(+), 23 deletions(-) diff --git a/examples/config b/examples/config index 2f621b83..087a9a14 100644 --- a/examples/config +++ b/examples/config @@ -12,4 +12,4 @@ lualoader = "lualib/loader.lua" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so" -daemon = "./skynet.pid" +-- daemon = "./skynet.pid" diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index d146fd56..c912257a 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -44,7 +44,7 @@ static struct global_queue *Q = NULL; #define GP(p) ((p) % MAX_GLOBAL_MQ) -static void +void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; @@ -214,21 +214,6 @@ skynet_mq_init() { Q=q; } -void -skynet_mq_force_push(struct message_queue * queue) { - assert(queue->in_global); - skynet_globalmq_push(queue); -} - -void -skynet_mq_pushglobal(struct message_queue *queue) { - LOCK(queue) - assert(queue->in_global); - skynet_globalmq_push(queue); - queue->in_global = MQ_IN_GLOBAL; - UNLOCK(queue) -} - void skynet_mq_mark_release(struct message_queue *q) { LOCK(q) @@ -257,7 +242,7 @@ skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud) { UNLOCK(q) _drop_queue(q, drop_func, ud); } else { - skynet_mq_force_push(q); + skynet_globalmq_push(q); UNLOCK(q) } } diff --git a/skynet-src/skynet_mq.h b/skynet-src/skynet_mq.h index 5ac582a1..df1a9998 100644 --- a/skynet-src/skynet_mq.h +++ b/skynet-src/skynet_mq.h @@ -13,6 +13,7 @@ struct skynet_message { struct message_queue; +void skynet_globalmq_push(struct message_queue * queue); struct message_queue * skynet_globalmq_pop(void); struct message_queue * skynet_mq_create(uint32_t handle); @@ -30,9 +31,6 @@ void skynet_mq_push(struct message_queue *q, struct skynet_message *message); // return the length of message queue, for debug int skynet_mq_length(struct message_queue *q); -void skynet_mq_force_push(struct message_queue *q); -void skynet_mq_pushglobal(struct message_queue *q); - void skynet_mq_init(); #endif diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 4bc7b66a..4ec7ea54 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -139,7 +139,7 @@ skynet_context_new(const char * name, const char *param) { if (ret) { ctx->init = true; } - skynet_mq_force_push(queue); + skynet_globalmq_push(queue); if (ret) { skynet_error(ret, "LAUNCH %s %s", name, param ? param : ""); } @@ -258,7 +258,7 @@ skynet_context_message_dispatch(struct skynet_monitor *sm) { } assert(q == ctx->queue); - skynet_mq_pushglobal(q); + skynet_globalmq_push(q); skynet_context_release(ctx); skynet_monitor_trigger(sm, 0,0); From e1a72bd820da9b109e5de5c3827188d0f2ca6600 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 26 May 2014 13:40:49 +0800 Subject: [PATCH 010/729] optimize message dispatch --- skynet-src/skynet_server.c | 26 +++++++++++++++++--------- skynet-src/skynet_server.h | 2 +- skynet-src/skynet_start.c | 4 +++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 4ec7ea54..ab7094c8 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -228,11 +228,13 @@ _dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { CHECKCALLING_END(ctx) } -int -skynet_context_message_dispatch(struct skynet_monitor *sm) { - struct message_queue * q = skynet_globalmq_pop(); - if (q==NULL) - return 1; +struct message_queue * +skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q) { + if (q == NULL) { + q = skynet_globalmq_pop(); + if (q==NULL) + return NULL; + } uint32_t handle = skynet_mq_handle(q); @@ -240,13 +242,13 @@ skynet_context_message_dispatch(struct skynet_monitor *sm) { if (ctx == NULL) { struct drop_t d = { handle }; skynet_mq_release(q, drop_message, &d); - return 0; + return skynet_globalmq_pop(); } struct skynet_message msg; if (skynet_mq_pop(q,&msg)) { skynet_context_release(ctx); - return 0; + return skynet_globalmq_pop(); } skynet_monitor_trigger(sm, msg.source , handle); @@ -258,12 +260,18 @@ skynet_context_message_dispatch(struct skynet_monitor *sm) { } assert(q == ctx->queue); - skynet_globalmq_push(q); + struct message_queue *nq = skynet_globalmq_pop(); + if (nq) { + // If global mq is not empty , push q back, and return next queue (nq) + // Else (global mq is empty or block, don't push q back, and return q again (for next dispatch) + skynet_globalmq_push(q); + q = nq; + } skynet_context_release(ctx); skynet_monitor_trigger(sm, 0,0); - return 0; + return q; } static void diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 7adc1292..51b8d1b2 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -16,7 +16,7 @@ void skynet_context_init(struct skynet_context *, uint32_t handle); int skynet_context_push(uint32_t handle, struct skynet_message *message); void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session); int skynet_context_newsession(struct skynet_context *); -int skynet_context_message_dispatch(struct skynet_monitor *); // return 1 when block +struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *); // return next queue int skynet_context_total(); void skynet_context_endless(uint32_t handle); // for monitor diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 09ab7711..d45ded49 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -121,8 +121,10 @@ _worker(void *p) { struct monitor *m = wp->m; struct skynet_monitor *sm = m->m[id]; skynet_initthread(THREAD_WORKER); + struct message_queue * q = NULL; for (;;) { - if (skynet_context_message_dispatch(sm)) { + q = skynet_context_message_dispatch(sm, q); + if (q == NULL) { CHECK_ABORT if (pthread_mutex_lock(&m->mutex) == 0) { ++ m->sleep; From 3597bb6d7529c28868d1cd41fd838892af4c058a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 27 May 2014 21:04:22 +0800 Subject: [PATCH 011/729] use jemalloc release 3.6.0 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 3541a904..46c0af68 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 3541a904d6fb949f3f0aea05418ccce7cbd4b705 +Subproject commit 46c0af68bd248b04df75e4f92d5fb804c3d75340 From cd0a1a495ed512f5ab496d46e80839443624af28 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 28 May 2014 11:30:39 +0800 Subject: [PATCH 012/729] optimize timer --- skynet-src/skynet_timer.c | 74 +++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 489f8d09..32b71f77 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -16,6 +16,9 @@ typedef void (*timer_execute_func)(void *ud,void *arg); +#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} +#define UNLOCK(q) __sync_lock_release(&(q)->lock); + #define TIME_NEAR_SHIFT 8 #define TIME_NEAR (1 << TIME_NEAR_SHIFT) #define TIME_LEVEL_SHIFT 6 @@ -50,8 +53,7 @@ struct timer { static struct timer * TI = NULL; static inline struct timer_node * -link_clear(struct link_list *list) -{ +link_clear(struct link_list *list) { struct timer_node * ret = list->head.next; list->head.next = 0; list->tail = &(list->head); @@ -60,23 +62,20 @@ link_clear(struct link_list *list) } static inline void -link(struct link_list *list,struct timer_node *node) -{ +link(struct link_list *list,struct timer_node *node) { list->tail->next = node; list->tail = node; node->next=0; } static void -add_node(struct timer *T,struct timer_node *node) -{ +add_node(struct timer *T,struct timer_node *node) { int time=node->expire; int current_time=T->time; if ((time|TIME_NEAR_MASK)==(current_time|TIME_NEAR_MASK)) { link(&T->near[time&TIME_NEAR_MASK],node); - } - else { + } else { int i; int mask=TIME_NEAR << TIME_LEVEL_SHIFT; for (i=0;i<3;i++) { @@ -90,21 +89,21 @@ add_node(struct timer *T,struct timer_node *node) } static void -timer_add(struct timer *T,void *arg,size_t sz,int time) -{ +timer_add(struct timer *T,void *arg,size_t sz,int time) { struct timer_node *node = (struct timer_node *)skynet_malloc(sizeof(*node)+sz); memcpy(node+1,arg,sz); - while (__sync_lock_test_and_set(&T->lock,1)) {}; + LOCK(T); node->expire=time+T->time; add_node(T,node); - __sync_lock_release(&T->lock); + UNLOCK(T); } static void timer_shift(struct timer *T) { + LOCK(T); int mask = TIME_NEAR; int time = (++T->time) >> TIME_NEAR_SHIFT; int i=0; @@ -125,50 +124,57 @@ timer_shift(struct timer *T) { time >>= TIME_LEVEL_SHIFT; ++i; } + UNLOCK(T); +} + +static inline void +dispatch_list(struct timer_node *current) { + do { + struct timer_event * event = (struct timer_event *)(current+1); + struct skynet_message message; + message.source = 0; + message.session = event->session; + message.data = NULL; + message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT; + + skynet_context_push(event->handle, &message); + + struct timer_node * temp = current; + current=current->next; + skynet_free(temp); + } while (current); } static inline void timer_execute(struct timer *T) { + LOCK(T); int idx = T->time & TIME_NEAR_MASK; while (T->near[idx].head.next) { struct timer_node *current = link_clear(&T->near[idx]); - - do { - struct timer_event * event = (struct timer_event *)(current+1); - struct skynet_message message; - message.source = 0; - message.session = event->session; - message.data = NULL; - message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT; - - skynet_context_push(event->handle, &message); - - struct timer_node * temp = current; - current=current->next; - skynet_free(temp); - } while (current); + UNLOCK(T); + // dispatch_list don't need lock T + dispatch_list(current); + LOCK(T); } + + UNLOCK(T); } static void -timer_update(struct timer *T) -{ - while (__sync_lock_test_and_set(&T->lock,1)) {}; - +timer_update(struct timer *T) { // try to dispatch timeout 0 (rare condition) timer_execute(T); // shift time first, and then dispatch timer message timer_shift(T); + timer_execute(T); - __sync_lock_release(&T->lock); } static struct timer * -timer_create_timer() -{ +timer_create_timer() { struct timer *r=(struct timer *)skynet_malloc(sizeof(struct timer)); memset(r,0,sizeof(*r)); From c130a93a91fe04ffc439b0f5883251e6b4e31039 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 2 Jun 2014 11:53:55 +0800 Subject: [PATCH 013/729] bson : check string length --- lualib-src/lua-bson.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 077e538e..ade3cc4e 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -532,6 +532,9 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { break; case BSON_STRING: { int sz = read_int32(L, &t); + if (sz == 0) { + luaL_error(L, "Invalid bson string , length = 0"); + } lua_pushlstring(L, read_bytes(L, &t, sz), sz-1); break; } @@ -804,6 +807,9 @@ lreplace(lua_State *L) { write_int64(&b, i); break; } + default: + luaL_error(L, "Can't replace type %d", type); + break; } return 0; } From 7f3ba76a417215f3de557ac62200328e75d24bac Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 2 Jun 2014 21:10:58 +0800 Subject: [PATCH 014/729] update history --- 3rd/jemalloc | 2 +- HISTORY.md | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 46c0af68..3541a904 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 46c0af68bd248b04df75e4f92d5fb804c3d75340 +Subproject commit 3541a904d6fb949f3f0aea05418ccce7cbd4b705 diff --git a/HISTORY.md b/HISTORY.md index 27927ea5..e498068a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,8 +1,12 @@ -Dev version +v0.3.0 (2014-6-2) ----------- -* Bugfix: update lua-bson (signed 32bit int bug) * Add cluster support +* Add single node mode * Add daemon mode +* Bugfix: update lua-bson (signed 32bit int bug / check string length) +* Optimize timer +* Simplify message queue and optimize message dispatch +* Use jemalloc release 3.6.0 v0.2.1 (2014-5-19) ----------- From 9477cc8171740ec2b3724f827e74cb3b9961cf01 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 3 Jun 2014 12:35:00 +0000 Subject: [PATCH 015/729] turn off memory hook in freeBSD --- README.md | 4 +++- platform.mk | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2b5e625d..08eaf877 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Build -Install autoconf first for jemalloc +For linux, install autoconf first for jemalloc ``` git clone git@github.com:cloudwu/skynet.git @@ -15,6 +15,8 @@ export PLAT=linux make ``` +For freeBSD , use gmake instead of make . + ## Test Run these in different console diff --git a/platform.mk b/platform.mk index a112f0f2..37291271 100644 --- a/platform.mk +++ b/platform.mk @@ -31,10 +31,10 @@ macosx : EXPORT := macosx linux : SKYNET_LIBS += -ldl linux freebsd : SKYNET_LIBS += -lrt -# Turn off jemalloc and malloc hook on macosx +# Turn off jemalloc and malloc hook on macosx and freebsd -macosx : MALLOC_STATICLIB := -macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC +freebsd macosx : MALLOC_STATICLIB := +freebsd macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC linux macosx freebsd : $(MAKE) all PLAT=$@ SKYNET_LIBS="$(SKYNET_LIBS)" SHARED="$(SHARED)" EXPORT="$(EXPORT)" MALLOC_STATICLIB="$(MALLOC_STATICLIB)" SKYNET_DEFINES="$(SKYNET_DEFINES)" From c10e5412c0d60d73f1212b83fd58494d5864348f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Jun 2014 10:23:49 +0800 Subject: [PATCH 016/729] invalid length may nagtive --- lualib-src/lua-bson.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index ade3cc4e..ca73bae0 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -532,8 +532,8 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { break; case BSON_STRING: { int sz = read_int32(L, &t); - if (sz == 0) { - luaL_error(L, "Invalid bson string , length = 0"); + if (sz <= 0) { + luaL_error(L, "Invalid bson string , length = %d", sz); } lua_pushlstring(L, read_bytes(L, &t, sz), sz-1); break; From f92fc153e4adbe7e1e3bcf9092e3d1f453d40c0e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Jun 2014 02:51:29 +0000 Subject: [PATCH 017/729] bugfix: in freebsd, libthr may call calloc before globalinit --- platform.mk | 4 ++-- skynet-src/skynet_server.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/platform.mk b/platform.mk index 37291271..345acabe 100644 --- a/platform.mk +++ b/platform.mk @@ -33,8 +33,8 @@ linux freebsd : SKYNET_LIBS += -lrt # Turn off jemalloc and malloc hook on macosx and freebsd -freebsd macosx : MALLOC_STATICLIB := -freebsd macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC +macosx : MALLOC_STATICLIB := +macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC linux macosx freebsd : $(MAKE) all PLAT=$@ SKYNET_LIBS="$(SKYNET_LIBS)" SHARED="$(SHARED)" EXPORT="$(EXPORT)" MALLOC_STATICLIB="$(MALLOC_STATICLIB)" SKYNET_DEFINES="$(SKYNET_DEFINES)" diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index ab7094c8..98fcfcd5 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -52,6 +52,7 @@ struct skynet_context { struct skynet_node { int total; + int init; uint32_t monitor_exit; pthread_key_t handle_key; }; @@ -75,8 +76,13 @@ context_dec() { uint32_t skynet_current_handle(void) { - void * handle = pthread_getspecific(G_NODE.handle_key); - return (uint32_t)(uintptr_t)handle; + if (G_NODE.init) { + void * handle = pthread_getspecific(G_NODE.handle_key); + return (uint32_t)(uintptr_t)handle; + } else { + uintptr_t v = (uint32_t)(-THREAD_MAIN); + return v; + } } static void @@ -650,6 +656,7 @@ void skynet_globalinit(void) { G_NODE.total = 0; G_NODE.monitor_exit = 0; + G_NODE.init = 1; if (pthread_key_create(&G_NODE.handle_key, NULL)) { fprintf(stderr, "pthread_key_create failed"); exit(1); From bf6501f2ee754bf696c27674b8750384ce661c1b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 Jun 2014 17:43:40 +0800 Subject: [PATCH 018/729] check mongo reply data stream --- lualib-src/lua-bson.c | 13 +++++++++++-- lualib-src/lua-mongo.c | 7 +++++++ lualib/mongo.lua | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index ca73bae0..1799d7bf 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -68,6 +68,13 @@ struct bson_reader { int size; }; +static inline int32_t +get_length(const uint8_t * data) { + const uint8_t * b = (const uint8_t *)data; + int32_t len = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24; + return len; +} + static inline void bson_destroy(struct bson *b) { if (b->ptr != b->buffer) { @@ -644,7 +651,7 @@ static int lmakeindex(lua_State *L) { int32_t *bson = luaL_checkudata(L,1,"bson"); const uint8_t * start = (const uint8_t *)bson; - struct bson_reader br = { start+4, *bson - 5 }; + struct bson_reader br = { start+4, get_length(start) - 5 }; lua_newtable(L); for (;;) { @@ -820,7 +827,9 @@ ldecode(lua_State *L) { if (data == NULL) { return 0; } - struct bson_reader br = { (const uint8_t *)data , *data }; + const uint8_t * b = (const uint8_t *)data; + int32_t len = get_length(b); + struct bson_reader br = { b , len }; unpack_dict(L, &br, false); diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index a0d312fd..3e4666dc 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -260,6 +260,13 @@ op_reply(lua_State *L) { lua_pushnil(L); lua_rawseti(L, 2, i); } + } else { + if (sz >= 4) { + sz -= get_length((document)doc); + } + } + if (sz != 0) { + return luaL_error(L, "Invalid result bson document"); } lua_pushboolean(L,1); lua_pushinteger(L, id); diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 7b1c6f69..b04f7b46 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -98,7 +98,7 @@ function mongo.client( conf ) host = obj.host, port = obj.port, response = dispatch_reply, - auth = mongo_auth(conf), + auth = mongo_auth(obj), } setmetatable(obj, client_meta) obj.__sock:connect(true) -- try connect only once From 4bc2e800fdb14c07ab78687c5a088fa86547f1be Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 Jun 2014 18:08:23 +0800 Subject: [PATCH 019/729] bugfix: lua mongo result --- lualib/mongo.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index b04f7b46..f9eece35 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -167,7 +167,9 @@ function mongo_db:runCommand(cmd,cmd_v,...) bson_cmd = bson_encode_order(cmd,cmd_v,...) end local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd) - local doc = sock:request(pack, request_id).document + -- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data) + local req = sock:request(pack, request_id) + local doc = req.document return bson_decode(doc) end @@ -224,7 +226,9 @@ function mongo_collection:findOne(query, selector) local request_id = conn:genId() local sock = conn.__sock local pack = driver.query(request_id, 0, self.full_name, 0, 1, query and bson_encode(query) or empty_bson, selector and bson_encode(selector)) - local doc = sock:request(pack, request_id).document + -- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data) + local req = sock:request(pack, request_id) + local doc = req.document return bson_decode(doc) end From 0f64e909fa10784f1a8b6d6b9fea634af76a75d2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Jun 2014 15:11:18 +0800 Subject: [PATCH 020/729] big-endian encoding bson objectid --- HISTORY.md | 6 ++++++ lualib-src/lua-bson.c | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index e498068a..828a8a00 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +Dev version +----------- +* Bugfix: lua mongo driver . Hold reply string before decode bson data. +* More check in bson decoding. +* Use big-endian for encoding bson objectid. + v0.3.0 (2014-6-2) ----------- * Add cluster support diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 1799d7bf..07fb77e2 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1149,14 +1149,14 @@ lobjectid(lua_State *L) { } } else { time_t ti = time(NULL); - oid[2] = ti & 0xff; - oid[3] = (ti>>8) & 0xff; - oid[4] = (ti>>16) & 0xff; - oid[5] = (ti>>24) & 0xff; + oid[2] = (ti>>24) & 0xff; + oid[3] = (ti>>16) & 0xff; + oid[4] = (ti>>8) & 0xff; + oid[5] = ti & 0xff; memcpy(oid+6 , oid_header, 5); - oid[11] = oid_counter & 0xff; + oid[11] = (oid_counter>>16) & 0xff; oid[12] = (oid_counter>>8) & 0xff; - oid[13] = (oid_counter>>16) & 0xff; + oid[13] = oid_counter & 0xff; ++oid_counter; } lua_pushlstring( L, (const char *)oid, 14); From 87d276caf6e85da4255a174b3773bce7a5c98cb0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 12 Jun 2014 17:28:54 +0800 Subject: [PATCH 021/729] typo fix --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9c84351d..3c5fd446 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,7 @@ $(CSERVICE_PATH) : define CSERVICE_TEMP $$(CSERVICE_PATH)/$(1).so : service-src/service_$(1).c | $$(CSERVICE_PATH) - $(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Iskynet-src + $$(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Iskynet-src endef $(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v)))) From 4fbd433199a5bbacd0d7865425e085aecdb94818 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 16 Jun 2014 10:10:18 +0800 Subject: [PATCH 022/729] release v0.3.1 --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 828a8a00..9d39efaf 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -Dev version +v0.3.1 (2014-6-16) ----------- * Bugfix: lua mongo driver . Hold reply string before decode bson data. * More check in bson decoding. From 078ac2bcd2d917ccd19c2e22dddeb6673b2d863a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 18 Jun 2014 15:09:15 +0800 Subject: [PATCH 023/729] bugfix: double free --- lualib-src/lua-cluster.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index b3b8a9d1..92134ced 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -153,6 +153,8 @@ lunpackrequest(lua_State *L) { static int lpackresponse(lua_State *L) { uint32_t session = luaL_checkunsigned(L,1); + // clusterd.lua:command.socket call lpackresponse, + // and the msg/sz is return by skynet.rawcall , so don't free(msg) void * msg = lua_touserdata(L,2); size_t sz = luaL_checkunsigned(L, 3); @@ -161,8 +163,6 @@ lpackresponse(lua_State *L) { fill_uint32(buf+2, session); memcpy(buf+6,msg,sz); - skynet_free(msg); - lua_pushlstring(L, (const char *)buf, sz+6); return 1; From 44bef99f64ee4596bd27675e4e574a80c47c1ac9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 17 Jun 2014 16:32:32 +0800 Subject: [PATCH 024/729] use skynet.error instead of print --- lualib/skynet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 1d936052..430881fe 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -502,7 +502,7 @@ end local function init_service(start) local ok, err = xpcall(init_template, debug.traceback, start) if not ok then - print("init service failed:", err) + skynet.error("init service failed: " .. tostring(err)) skynet.send(".launcher","lua", "ERROR") skynet.exit() else From 942c2d3a67facb264da4e48068cb813016dbf5ef Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 18 Jun 2014 10:25:15 +0800 Subject: [PATCH 025/729] freebsd can use malloc hook now --- platform.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform.mk b/platform.mk index 345acabe..a112f0f2 100644 --- a/platform.mk +++ b/platform.mk @@ -31,7 +31,7 @@ macosx : EXPORT := macosx linux : SKYNET_LIBS += -ldl linux freebsd : SKYNET_LIBS += -lrt -# Turn off jemalloc and malloc hook on macosx and freebsd +# Turn off jemalloc and malloc hook on macosx macosx : MALLOC_STATICLIB := macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC From 4ccb677e8464d27bd9ae6cb3669ad3b225fcce72 Mon Sep 17 00:00:00 2001 From: xjdrew Date: Wed, 18 Jun 2014 20:20:45 +0800 Subject: [PATCH 026/729] optimize redis compose_message, see redistest for changes test case: start redistest --- examples/redistest.lua | 53 ++++++++++++++++++++++++++++++ lualib/redis.lua | 74 ++++++++++++++++++++++++++++-------------- 2 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 examples/redistest.lua diff --git a/examples/redistest.lua b/examples/redistest.lua new file mode 100644 index 00000000..f12d8100 --- /dev/null +++ b/examples/redistest.lua @@ -0,0 +1,53 @@ +local skynet = require "skynet" +local redis = require "redis" + +local db + +function add1(key, count) + local t = {} + for i = 1, count do + t[2*i -1] = "key" ..i + t[2*i] = "value" .. i + end + db:hmset(key, table.unpack(t)) +end + +function add2(key, count) + local t = {} + for i = 1, count do + t[2*i -1] = "key" ..i + t[2*i] = "value" .. i + end + table.insert(t, 1, key) + db:hmset(t) +end + +function __init__() + db = redis.connect { + host = "127.0.0.1", + port = 6300, + db = 0, + auth = "foobared" + } + print("dbsize:", db:dbsize()) + local ok, msg = xpcall(add1, debug.traceback, "test1", 250000) + if not ok then + print("add1 failed", msg) + else + print("add1 succeed") + + end + + local ok, msg = xpcall(add2, debug.traceback, "test2", 250000) + if not ok then + print("add2 failed", msg) + else + print("add2 succeed") + end + print("dbsize:", db:dbsize()) + + print("redistest launched") +end + +skynet.start(__init__) + diff --git a/lualib/redis.lua b/lualib/redis.lua index d28278dc..cd9f43f4 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" local socket = require "socket" local socketchannel = require "socketchannel" +local int64 = require "int64" local table = table local string = string @@ -94,31 +95,56 @@ function command:disconnect() setmetatable(self, nil) end -local function compose_message(msg) - if #msg == 1 then - return msg[1] .. "\r\n" +-- msg could be any type of value +local function pack_value(lines, v) + if v == nil then + return end - local lines = { "*" .. #msg } - for _,v in ipairs(msg) do - local t = type(v) - if t == "number" then - v = tostring(v) - elseif t == "userdata" then - v = int64.tostring(int64.new(v),10) - end - table.insert(lines,"$"..#v) - table.insert(lines,v) - end - table.insert(lines,"") - local cmd = table.concat(lines,"\r\n") - return cmd + local t = type(v) + if t == "number" then + v = tostring(v) + elseif t == "userdata" then + v = int64.tostring(int64.new(v),10) + end + table.insert(lines,"$"..#v) + table.insert(lines,v) +end + +local function compose_message(cmd, msg) + local len = 1 + local t = type(msg) + + if t == "table" then + len = len + #msg + elseif t ~= nil then + len = len + 1 + end + + local lines = {"*" .. len} + pack_value(lines, cmd) + + if t == "table" then + for _,v in ipairs(msg) do + pack_value(lines, v) + end + else + pack_value(lines, msg) + end + table.insert(lines, "") + + local chunk = table.concat(lines,"\r\n") + return chunk end setmetatable(command, { __index = function(t,k) local cmd = string.upper(k) - local f = function (self, ...) - return self[1]:request(compose_message { cmd, ... }, read_response) + local f = function (self, v, ...) + if type(v) == "table" then + return self[1]:request(compose_message(cmd, v), read_response) + else + return self[1]:request(compose_message(cmd, {v, ...}), read_response) + end end t[k] = f return f @@ -131,12 +157,12 @@ end function command:exists(key) local fd = self[1] - return fd:request(compose_message { "EXISTS", key }, read_boolean) + return fd:request(compose_message ("EXISTS", key), read_boolean) end function command:sismember(key, value) local fd = self[1] - return fd:request(compose_message { "SISMEMBER", key, value }, read_boolean) + return fd:request(compose_message ("SISMEMBER", {key, value}), read_boolean) end --- watch mode @@ -156,10 +182,10 @@ local function watch_login(obj, auth) so:request("AUTH "..auth.."\r\n", read_response) end for k in pairs(obj.__psubscribe) do - so:request(compose_message { "PSUBSCRIBE", k }) + so:request(compose_message ("PSUBSCRIBE", k)) end for k in pairs(obj.__subscribe) do - so:request(compose_message { "SUBSCRIBE", k }) + so:request(compose_message("SUBSCRIBE", k)) end end end @@ -192,7 +218,7 @@ local function watch_func( name ) local so = self.__sock for i = 1, select("#", ...) do local v = select(i, ...) - so:request(compose_message { NAME, v }) + so:request(compose_message(NAME, v)) end end end From ad9898a209e5ee38629e9c6b5f991eca9118f1c7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 Jun 2014 15:43:41 +0800 Subject: [PATCH 027/729] add module skynet.harbor --- .gitignore | 8 +- HISTORY.md | 5 + lualib-src/lua-skynet.c | 2 +- lualib/skynet.lua | 25 +++- lualib/skynet/harbor.lua | 40 +++++ service-src/service_dummy.c | 35 ++++- service-src/service_harbor.c | 137 +++++++++++++++++- service/bootstrap.lua | 7 +- skynet-src/skynet_harbor.c | 17 --- skynet-src/skynet_harbor.h | 1 - skynet-src/skynet_server.c | 11 +- test/testharborlink.lua | 11 ++ examples/redistest.lua => test/testredis2.lua | 0 13 files changed, 253 insertions(+), 46 deletions(-) create mode 100644 lualib/skynet/harbor.lua create mode 100644 test/testharborlink.lua rename examples/redistest.lua => test/testredis2.lua (100%) diff --git a/.gitignore b/.gitignore index 2cbccc98..9ee6cde3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ *.o *.a -skynet -skynet.pid +./skynet +./skynet.pid 3rd/lua/lua 3rd/lua/luac -cservice -luaclib +./cservice +./luaclib *.so *.dSYM diff --git a/HISTORY.md b/HISTORY.md index 9d39efaf..addc8955 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +Dev version +----------- +* Optimize redis driver `compose_message`. +* Add module skynet.harbor for monitor harbor connect/disconnect, see test/testharborlink.lua . + v0.3.1 (2014-6-16) ----------- * Bugfix: lua mongo driver . Hold reply string before decode bson data. diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 0025dd52..db49e997 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -221,7 +221,7 @@ _send(lua_State *L) { } if (session < 0) { // send to invalid address - // todo: maybe throw error is better + // todo: maybe throw error whould be better return 0; } lua_pushinteger(L,session); diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 430881fe..93b67d65 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -190,12 +190,33 @@ function skynet.wait() session_id_coroutine[session] = nil end +local function globalname(name, handle) + local c = string.sub(name,1,1) + assert(c ~= ':') + if c == '.' then + return false + end + + assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h + assert(tonumber(name) == nil) -- global name can't be number + + local harbor = require "skynet.harbor" + + harbor.globalname(name, handle) + + return true +end + function skynet.register(name) - c.command("REG", name) + if not globalname(name) then + c.command("REG", name) + end end function skynet.name(name, handle) - c.command("NAME", name .. " " .. skynet.address(handle)) + if not globalname(name, handle) then + c.command("NAME", name .. " " .. skynet.address(handle)) + end end local self_handle diff --git a/lualib/skynet/harbor.lua b/lualib/skynet/harbor.lua new file mode 100644 index 00000000..06f47bb6 --- /dev/null +++ b/lualib/skynet/harbor.lua @@ -0,0 +1,40 @@ +local skynet = require "skynet" + +local harbor = {} + +local HARBOR = skynet.getenv "harbor_address" + +if HARBOR then + HARBOR = tonumber("0x" .. string.sub(HARBOR , 2)) +end + +function harbor.globalname(name, handle) + assert(HARBOR) + handle = handle or skynet.self() + skynet.redirect(HARBOR, handle, "system", 0, "R " .. name) +end + +function harbor.init(h) + assert(HARBOR == nil) + HARBOR = h + skynet.setenv("harbor_address", skynet.address(h)) +end + +function harbor.link(id) + assert(HARBOR) + skynet.call(HARBOR, "system", "M " .. tostring(id)) +end + +function harbor.connect(id) + assert(HARBOR) + skynet.call(HARBOR, "system", "C " .. tostring(id)) +end + +skynet.register_protocol { + name = "system", + id = skynet.PTYPE_SYSTEM, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +return harbor diff --git a/service-src/service_dummy.c b/service-src/service_dummy.c index 006a276f..a1f19d4c 100644 --- a/service-src/service_dummy.c +++ b/service-src/service_dummy.c @@ -257,15 +257,42 @@ _send_name(struct dummy *h, uint32_t source, const char name[GLOBALNAME_LENGTH], } } +static void +dummy_command(struct dummy * h, const char * msg, size_t sz, int session, uint32_t source) { + switch(msg[0]) { + case 'R' : { + // register global name + const char * name = msg + 2; + int s = (int)sz; + s -= 2; + if (s <=0 || s>= GLOBALNAME_LENGTH) { + skynet_error(h->ctx, "Invalid global name %s", name); + return; + } + struct remote_name rn; + memset(&rn, 0, sizeof(rn)); + memcpy(rn.name, name, s); + rn.handle = source; + _update_name(h, rn.name, rn.handle); + break; + } + case 'C' : + case 'M' : + skynet_error(h->ctx, "Don't support harbor monitor in cluster dummy mode"); + skynet_send(h->ctx, 0, source, PTYPE_ERROR, session, NULL, 0); + break; + default: + skynet_error(h->ctx, "Unknown command %s", msg); + return; + } +} + static int _mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct dummy * h = ud; switch (type) { case PTYPE_SYSTEM: { - // register name message - const struct remote_message *rmsg = msg; - assert (sz == sizeof(rmsg->destination)); - _update_name(h, rmsg->destination.name, rmsg->destination.handle); + dummy_command(h, msg, sz, session, source); return 0; } default: { diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 926ed46f..aa6763a7 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -47,6 +47,17 @@ struct remote_message_header { uint32_t session; }; +struct monitor_response { + uint32_t addr; + int session; +}; + +struct monitor_set { + int cap; + int n; + struct monitor_response * resp; +}; + // 12 is sizeof(struct remote_message_header) #define HEADER_COOKIE_LENGTH 12 @@ -60,8 +71,61 @@ struct harbor { int remote_fd[REMOTE_MAX]; bool connected[REMOTE_MAX]; char * remote_addr[REMOTE_MAX]; + struct monitor_set * monitor[REMOTE_MAX]; }; +static void +monitor_free(struct harbor *h) { + int i; + for (i=0;imonitor[i]; + if (m) { + skynet_free(m->resp); + skynet_free(m); + h->monitor[i] = NULL; + } + } +} + +static void +monitor_add(struct harbor *h, int id, uint32_t addr, int session) { + struct monitor_set * m = h->monitor[id]; + if (m == NULL) { + m = skynet_malloc(sizeof(*m)); + m->cap = 4; + m->n = 0; + m->resp = skynet_malloc(m->cap * sizeof(struct monitor_response)); + h->monitor[id] = m; + } + if (m->n >= m->cap) { + assert(m->n == m->cap); + struct monitor_response * resp = skynet_malloc(m->cap * 2 * sizeof(struct monitor_response)); + int i; + for (i=0;in;i++) { + resp[i] = m->resp[i]; + } + m->cap *= 2; + skynet_free(m->resp); + m->resp = resp; + } + struct monitor_response * resp = &m->resp[m->n++]; + resp->addr = addr; + resp->session = session; +} + +static void +monitor_clear(struct harbor *h, int id) { + struct monitor_set * m = h->monitor[id]; + if (m) { + int i; + for (i=0;in;i++) { + struct monitor_response * resp = &m->resp[i]; + skynet_send(h->ctx, 0, resp->addr, PTYPE_RESPONSE, resp->session, NULL, 0); + } + m->n = 0; + } +} + // hash table static void @@ -211,6 +275,7 @@ harbor_create(void) { h->remote_fd[i] = -1; h->connected[i] = false; h->remote_addr[i] = NULL; + h->monitor[i] = NULL; } h->map = _hash_new(); return h; @@ -232,6 +297,7 @@ harbor_release(struct harbor *h) { } } _hash_delete(h->map); + monitor_free(h); skynet_free(h); } @@ -313,6 +379,14 @@ _send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz } } +static void +response_close(struct harbor *h, int id) { + if (h->connected[id]) { + monitor_clear(h, id); + } + h->connected[id] = false; +} + static void _update_remote_address(struct harbor *h, int harbor_id, const char * ipaddr) { if (harbor_id == h->id) { @@ -326,7 +400,7 @@ _update_remote_address(struct harbor *h, int harbor_id, const char * ipaddr) { h->remote_addr[harbor_id] = NULL; } h->remote_fd[harbor_id] = _connect_to(h, ipaddr, false); - h->connected[harbor_id] = false; + response_close(h, harbor_id); } static void @@ -466,7 +540,7 @@ close_harbor(struct harbor *h, int fd) { skynet_error(h->ctx, "Harbor %d closed",id); skynet_socket_close(h->ctx, fd); h->remote_fd[id] = -1; - h->connected[id] = false; + response_close(h,id); } static void @@ -475,9 +549,63 @@ open_harbor(struct harbor *h, int fd) { if (id == 0) return; assert(h->connected[id] == false); + monitor_clear(h, id); h->connected[id] = true; } +static void +harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint32_t source) { + const char * name = msg + 2; + int s = (int)sz; + s -= 2; + switch(msg[0]) { + case 'R' : { + // register global name + if (s <=0 || s>= GLOBALNAME_LENGTH) { + skynet_error(h->ctx, "Invalid global name %s", name); + return; + } + struct remote_name rn; + memset(&rn, 0, sizeof(rn)); + memcpy(rn.name, name, s); + rn.handle = source; + _remote_register_name(h, rn.name, rn.handle); + break; + } + case 'C' : + case 'M' : { + if (s <= 0) { + skynet_error(h->ctx, "Invalid harbor montior"); + skynet_send(h->ctx, 0, source, PTYPE_ERROR, session, NULL, 0); + return; + } + int hid = strtol(name, NULL, 10); + if (hid <= 0 || hid >= REMOTE_MAX) { + skynet_error(h->ctx, "Invalid harbor montior id : %s", name); + skynet_send(h->ctx, 0, source, PTYPE_ERROR, session, NULL, 0); + return; + } + if (msg[0] == 'M') { + if (!h->connected[hid]) { + skynet_send(h->ctx, 0, source, PTYPE_RESPONSE, session, NULL, 0); + return; + } + } else { + assert(msg[0] == 'C'); + if (h->connected[hid]) { + skynet_send(h->ctx, 0, source, PTYPE_RESPONSE, session, NULL, 0); + return; + } + } + monitor_add(h, hid, source, session); + break; + } + default: + skynet_error(h->ctx, "Unknown command %s", msg); + return; + } +} + static int _mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct harbor * h = ud; @@ -536,10 +664,7 @@ _mainloop(struct skynet_context * context, void * ud, int type, int session, uin return 0; } case PTYPE_SYSTEM: { - // register name message - const struct remote_message *rmsg = msg; - assert (sz == sizeof(rmsg->destination)); - _remote_register_name(h, rmsg->destination.name, rmsg->destination.handle); + harbor_command(h, msg,sz,session,source); return 0; } default: { diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 7c92b419..3d66115b 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +local harbor = require "skynet.harbor" skynet.start(function() assert(skynet.launch("logger", skynet.getenv "logger")) @@ -9,7 +10,8 @@ skynet.start(function() assert(standalone == nil) standalone = true skynet.setenv("standalone", "true") - assert(skynet.launch("dummy")) + local dummy = assert(skynet.launch("dummy")) + harbor.init(dummy) else local master_addr = skynet.getenv "master" @@ -19,7 +21,8 @@ skynet.start(function() local local_addr = skynet.getenv "address" - assert(skynet.launch("harbor",master_addr, local_addr, harbor_id)) + local h = assert(skynet.launch("harbor",master_addr, local_addr, harbor_id)) + harbor.init(h) end local launcher = assert(skynet.launch("snlua","launcher")) diff --git a/skynet-src/skynet_harbor.c b/skynet-src/skynet_harbor.c index 160b1994..48814c83 100644 --- a/skynet-src/skynet_harbor.c +++ b/skynet-src/skynet_harbor.c @@ -17,23 +17,6 @@ skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) { skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, type , session); } -void -skynet_harbor_register(struct remote_name *rname) { - if (REMOTE == NULL) - return; - int i; - int number = 1; - for (i=0;iname[i]; - if (!(c >= '0' && c <='9')) { - number = 0; - break; - } - } - assert(number == 0); - skynet_context_send(REMOTE, rname, sizeof(*rname), 0, PTYPE_SYSTEM , 0); -} - int skynet_harbor_message_isremote(uint32_t handle) { assert(HARBOR != ~0); diff --git a/skynet-src/skynet_harbor.h b/skynet-src/skynet_harbor.h index 0116a91a..b699f625 100644 --- a/skynet-src/skynet_harbor.h +++ b/skynet-src/skynet_harbor.h @@ -23,7 +23,6 @@ struct remote_message { }; void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session); -void skynet_harbor_register(struct remote_name *rname); int skynet_harbor_message_isremote(uint32_t handle); void skynet_harbor_init(int harbor); void skynet_harbor_start(void * ctx); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 98fcfcd5..e20f8102 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -342,11 +342,7 @@ cmd_reg(struct skynet_context * context, const char * param) { } else if (param[0] == '.') { return skynet_handle_namehandle(context->handle, param + 1); } else { - assert(context->handle!=0); - struct remote_name *rname = skynet_malloc(sizeof(*rname)); - copy_name(rname->name, param); - rname->handle = context->handle; - skynet_harbor_register(rname); + skynet_error(context, "Can't register global name %s in C", param); return NULL; } } @@ -377,10 +373,7 @@ cmd_name(struct skynet_context * context, const char * param) { if (name[0] == '.') { return skynet_handle_namehandle(handle_id, name + 1); } else { - struct remote_name *rname = skynet_malloc(sizeof(*rname)); - copy_name(rname->name, name); - rname->handle = handle_id; - skynet_harbor_register(rname); + skynet_error(context, "Can't set global name %s in C", name); } return NULL; } diff --git a/test/testharborlink.lua b/test/testharborlink.lua new file mode 100644 index 00000000..031e38ec --- /dev/null +++ b/test/testharborlink.lua @@ -0,0 +1,11 @@ +local skynet = require "skynet" +local harbor = require "skynet.harbor" + +skynet.start(function() + print("wait for harbor 2") + print("run skynet examples/config_log please") + harbor.connect(2) + print("harbor 2 connected") + harbor.link(2) + print("disconnected") +end) diff --git a/examples/redistest.lua b/test/testredis2.lua similarity index 100% rename from examples/redistest.lua rename to test/testredis2.lua From d0468a39f4bb33eafea2e0972b9c8dad34793fde Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 Jun 2014 16:25:50 +0800 Subject: [PATCH 028/729] cluster.open support cluster name --- HISTORY.md | 1 + examples/cluster1.lua | 2 +- lualib/cluster.lua | 6 +++++- service/clusterd.lua | 7 ++++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index addc8955..e4b2b7f7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,7 @@ Dev version ----------- * Optimize redis driver `compose_message`. * Add module skynet.harbor for monitor harbor connect/disconnect, see test/testharborlink.lua . +* cluster.open support cluster name. v0.3.1 (2014-6-16) ----------- diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 73fad761..551b0f38 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -5,5 +5,5 @@ skynet.start(function() skynet.newservice("simpledb") print(skynet.call("SIMPLEDB", "lua", "SET", "a", "foobar")) print(skynet.call("SIMPLEDB", "lua", "GET", "a")) - cluster.open(2528) + cluster.open "db" end) diff --git a/lualib/cluster.lua b/lualib/cluster.lua index e4f6cfa2..bc4312b3 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -9,7 +9,11 @@ function cluster.call(node, address, ...) end function cluster.open(port) - skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) + if type(port) == "string" then + skynet.call(clusterd, "lua", "listen", port) + else + skynet.call(clusterd, "lua", "listen", "0.0.0.0", port) + end end skynet.init(function() diff --git a/service/clusterd.lua b/service/clusterd.lua index ef6bff68..51494971 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -32,7 +32,12 @@ local node_channel = setmetatable({}, { __index = open_channel }) function command.listen(source, addr, port) local gate = skynet.newservice("gate") - skynet.call(gate, "lua", "open", { address = addr, port = port }) + if port == nil then + local host, port = string.match(node_address[addr], "([^:]+):(.*)$") + skynet.call(gate, "lua", "open", { address = host, port = port }) + else + skynet.call(gate, "lua", "open", { address = addr, port = port }) + end skynet.ret(skynet.pack(nil)) end From e4197daa5469d561f8b2ab293f342e9a8a1aea37 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 Jun 2014 16:29:54 +0800 Subject: [PATCH 029/729] minor optimize --- service/clusterd.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 51494971..f607e6f5 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -33,11 +33,9 @@ local node_channel = setmetatable({}, { __index = open_channel }) function command.listen(source, addr, port) local gate = skynet.newservice("gate") if port == nil then - local host, port = string.match(node_address[addr], "([^:]+):(.*)$") - skynet.call(gate, "lua", "open", { address = host, port = port }) - else - skynet.call(gate, "lua", "open", { address = addr, port = port }) + addr, port = string.match(node_address[addr], "([^:]+):(.*)$") end + skynet.call(gate, "lua", "open", { address = addr, port = port }) skynet.ret(skynet.pack(nil)) end From 1a44bfb09ab404ba93c85f6f8b606565b1f369a0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 Jun 2014 22:12:29 +0800 Subject: [PATCH 030/729] launch logger in C --- service/bootstrap.lua | 2 -- skynet-src/skynet_imp.h | 1 + skynet-src/skynet_main.c | 1 + skynet-src/skynet_server.c | 10 ++++++++++ skynet-src/skynet_server.h | 1 + skynet-src/skynet_start.c | 11 +++++++++-- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 3d66115b..4e2377e0 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -2,8 +2,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" skynet.start(function() - assert(skynet.launch("logger", skynet.getenv "logger")) - local standalone = skynet.getenv "standalone" local harbor_id = tonumber(skynet.getenv "harbor") if harbor_id == 0 then diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index 11b12ee1..c5a144a4 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -7,6 +7,7 @@ struct skynet_config { const char * daemon; const char * module_path; const char * bootstrap; + const char * logger; }; #define THREAD_WORKER 0 diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 8596c778..9c38fd25 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -115,6 +115,7 @@ main(int argc, char *argv[]) { config.harbor = optint("harbor", 1); config.bootstrap = optstring("bootstrap","snlua bootstrap"); config.daemon = optstring("daemon", NULL); + config.logger = optstring("logger", NULL); lua_close(L); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index e20f8102..b7b809ef 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -234,6 +234,16 @@ _dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { CHECKCALLING_END(ctx) } +void +skynet_context_dispatchall(struct skynet_context * ctx) { + // for skynet_error + struct skynet_message msg; + struct message_queue *q = ctx->queue; + while (!skynet_mq_pop(q,&msg)) { + _dispatch_message(ctx, &msg); + } +} + struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q) { if (q == NULL) { diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 51b8d1b2..be4e301f 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -18,6 +18,7 @@ void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, int skynet_context_newsession(struct skynet_context *); struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *); // return next queue int skynet_context_total(); +void skynet_context_dispatchall(struct skynet_context * context); // for skynet_error output before exit void skynet_context_endless(uint32_t handle); // for monitor diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index d45ded49..c101da30 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -184,7 +184,7 @@ _start(int thread) { } static void -bootstrap(const char * cmdline) { +bootstrap(struct skynet_context * logger, const char * cmdline) { int sz = strlen(cmdline); char name[sz+1]; char args[sz+1]; @@ -192,6 +192,7 @@ bootstrap(const char * cmdline) { struct skynet_context *ctx = skynet_context_new(name, args); if (ctx == NULL) { skynet_error(NULL, "Bootstrap error : %s\n", cmdline); + skynet_context_dispatchall(logger); exit(1); } } @@ -210,7 +211,13 @@ skynet_start(struct skynet_config * config) { skynet_timer_init(); skynet_socket_init(); - bootstrap(config->bootstrap); + struct skynet_context *ctx = skynet_context_new("logger", config->logger); + if (ctx == NULL) { + fprintf(stderr, "Can't launch logger service\n"); + exit(1); + } + + bootstrap(ctx, config->bootstrap); _start(config->thread); skynet_socket_free(); From c3eb5cd20235f643ac61ad7e5cf67965dd9d5beb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 20 Jun 2014 02:49:48 +0800 Subject: [PATCH 031/729] After connecting, socket can send before connected. remove block connect api --- examples/config_log | 6 ++++-- examples/globallog.lua | 16 ++++++++++++++++ examples/main_log.lua | 1 - service-src/service_harbor.c | 24 +++++++++++++++--------- service-src/service_master.c | 3 ++- service/bootstrap.lua | 2 +- skynet-src/skynet_socket.c | 6 ------ skynet-src/skynet_socket.h | 1 - skynet-src/socket_server.c | 35 ++++++++++------------------------- skynet-src/socket_server.h | 2 -- 10 files changed, 48 insertions(+), 48 deletions(-) diff --git a/examples/config_log b/examples/config_log index 9261e435..42a8692b 100644 --- a/examples/config_log +++ b/examples/config_log @@ -3,8 +3,10 @@ mqueue = 256 cpath = "./cservice/?.so" logger = nil harbor = 2 -address = "127.0.0.1:2527" -master = "127.0.0.1:2013" +--address = "127.0.0.1:2527" +--master = "127.0.0.1:2013" +address = "172.16.100.201:2527" +master = "172.16.100.209:2013" start = "main_log" luaservice ="./service/?.lua;./test/?.lua;./examples/?.lua" snax = "./examples/?.lua;./test/?.lua" diff --git a/examples/globallog.lua b/examples/globallog.lua index ec431cce..9b4dce4c 100644 --- a/examples/globallog.lua +++ b/examples/globallog.lua @@ -1,5 +1,21 @@ local skynet = require "skynet" +skynet.register_protocol { + name = "text", + id = skynet.PTYPE_TEXT, + pack = function (...) + local n = select ("#" , ...) + if n == 0 then + return "" + elseif n == 1 then + return tostring(...) + else + return table.concat({...}," ") + end + end, + unpack = skynet.tostring +} + skynet.start(function() skynet.dispatch("text", function(session, address, text) print("[GLOBALLOG]", skynet.address(address),text) diff --git a/examples/main_log.lua b/examples/main_log.lua index 8a5194a0..f74b563f 100644 --- a/examples/main_log.lua +++ b/examples/main_log.lua @@ -2,7 +2,6 @@ local skynet = require "skynet" skynet.start(function() print("Log server start") - local service = skynet.newservice("service_mgr") skynet.monitor "simplemonitor" local log = skynet.newservice("globallog") skynet.exit() diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index aa6763a7..a9cfaec4 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -302,7 +302,7 @@ harbor_release(struct harbor *h) { } static int -_connect_to(struct harbor *h, const char *ipaddress, bool blocking) { +_connect_to(struct harbor *h, const char *ipaddress) { char * port = strchr(ipaddress,':'); if (port==NULL) { return -1; @@ -316,11 +316,7 @@ _connect_to(struct harbor *h, const char *ipaddress, bool blocking) { skynet_error(h->ctx, "Harbor(%d) connect to %s:%d", h->id, tmp, portid); - if (blocking) { - return skynet_socket_block_connect(h->ctx, tmp, portid); - } else { - return skynet_socket_connect(h->ctx, tmp, portid); - } + return skynet_socket_connect(h->ctx, tmp, portid); } static inline void @@ -399,7 +395,7 @@ _update_remote_address(struct harbor *h, int harbor_id, const char * ipaddr) { skynet_free(h->remote_addr[harbor_id]); h->remote_addr[harbor_id] = NULL; } - h->remote_fd[harbor_id] = _connect_to(h, ipaddr, false); + h->remote_fd[harbor_id] = _connect_to(h, ipaddr); response_close(h, harbor_id); } @@ -448,7 +444,9 @@ _request_master(struct harbor *h, const char name[GLOBALNAME_LENGTH], size_t i, to_bigendian(buffer, handle); memcpy(buffer+4,name,i); - _send_package(h->ctx, h->master_fd, buffer, 4+i); + if (h->master_fd >= 0) { + _send_package(h->ctx, h->master_fd, buffer, 4+i); + } } /* @@ -534,7 +532,14 @@ harbor_id(struct harbor *h, int fd) { static void close_harbor(struct harbor *h, int fd) { + if (fd == h->master_fd) { + skynet_socket_close(h->ctx, fd); + skynet_error(h->ctx, "Master disconnected"); + h->master_fd = -1; + return; + } int id = harbor_id(h,fd); + if (id == 0) return; skynet_error(h->ctx, "Harbor %d closed",id); @@ -551,6 +556,7 @@ open_harbor(struct harbor *h, int fd) { assert(h->connected[id] == false); monitor_clear(h, id); h->connected[id] = true; + skynet_error(h->ctx, "Harbor(%d) connected", id); } static void @@ -715,7 +721,7 @@ harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) { sscanf(args,"%s %s %d",master_addr, local_addr, &harbor_id); h->master_addr = skynet_strdup(master_addr); h->id = harbor_id; - h->master_fd = _connect_to(h, master_addr, true); + h->master_fd = _connect_to(h, master_addr); if (h->master_fd == -1) { fprintf(stderr, "Harbor: Connect to master failed\n"); exit(1); diff --git a/service-src/service_master.c b/service-src/service_master.c index b5d8a560..a4fb8b1b 100644 --- a/service-src/service_master.c +++ b/service-src/service_master.c @@ -121,6 +121,7 @@ _connect_to(struct master *m, int id) { int port = strtol(portstr+1,NULL,10); skynet_error(ctx, "Master connect to harbor(%d) %s:%d", id, tmp, port); m->remote_fd[id] = skynet_socket_connect(ctx, tmp, port); + m->connected[id] = true; } static inline void @@ -217,7 +218,7 @@ socket_id(struct master *m, int id) { static void on_connected(struct master *m, int id) { _broadcast(m, m->remote_addr[id], strlen(m->remote_addr[id]), id); - m->connected[id] = true; +// m->connected[id] = true; int i; for (i=1;islot[id % MAX_SOCKET]; if (s->type == SOCKET_TYPE_INVALID) { if (__sync_bool_compare_and_swap(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) { + s->id = id; + s->fd = -1; return id; } else { // retry @@ -287,7 +289,7 @@ new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) { // return -1 when connecting static int -open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result, bool blocking) { +open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result) { int id = request->id; result->opaque = request->opaque; result->id = id; @@ -316,18 +318,14 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock continue; } socket_keepalive(sock); - if (!blocking) { - sp_nonblocking(sock); - } + sp_nonblocking(sock); status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen); if ( status != 0 && errno != EINPROGRESS) { close(sock); sock = -1; continue; } - if (blocking) { - sp_nonblocking(sock); - } + sp_nonblocking(sock); break; } @@ -512,7 +510,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock return -1; } assert(s->type != SOCKET_TYPE_PLISTEN && s->type != SOCKET_TYPE_LISTEN); - if (send_buffer_empty(s)) { + if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) { int n = write(s->fd, request->buffer, request->sz); if (n<0) { switch(errno) { @@ -687,7 +685,7 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { case 'K': return close_socket(ss,(struct request_close *)buffer, result); case 'O': - return open_socket(ss, (struct request_open *)buffer, result, false); + return open_socket(ss, (struct request_open *)buffer, result); case 'X': result->opaque = 0; result->id = 0; @@ -765,7 +763,9 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message result->opaque = s->opaque; result->id = s->id; result->ud = 0; - sp_write(ss->event_fd, s->fd, s, false); + if (send_buffer_empty(s)) { + sp_write(ss->event_fd, s->fd, s, false); + } union sockaddr_all u; socklen_t slen = sizeof(u); if (getpeername(s->fd, &u.s, &slen) == 0) { @@ -939,19 +939,6 @@ socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * a return request.u.open.id; } -int -socket_server_block_connect(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { - struct request_package request; - struct socket_message result; - open_request(ss, &request, opaque, addr, port); - int ret = open_socket(ss, &request.u.open, &result, true); - if (ret == SOCKET_OPEN) { - return result.id; - } else { - return -1; - } -} - // return -1 when error int64_t socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) { @@ -959,7 +946,6 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz if (s->id != id || s->type == SOCKET_TYPE_INVALID) { return -1; } - assert(s->type != SOCKET_TYPE_RESERVE); struct request_package request; request.u.send.id = id; @@ -976,7 +962,6 @@ socket_server_send_lowpriority(struct socket_server *ss, int id, const void * bu if (s->id != id || s->type == SOCKET_TYPE_INVALID) { return; } - assert(s->type != SOCKET_TYPE_RESERVE); struct request_package request; request.u.send.id = id; diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 2fbb6939..ea15c0e1 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -36,6 +36,4 @@ int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port); int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd); -int socket_server_block_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port); - #endif From 7137850eb2eff0448a0e5d321317c2a45f65c834 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 20 Jun 2014 03:02:51 +0800 Subject: [PATCH 032/729] restore config_log --- examples/config_log | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/config_log b/examples/config_log index 42a8692b..9261e435 100644 --- a/examples/config_log +++ b/examples/config_log @@ -3,10 +3,8 @@ mqueue = 256 cpath = "./cservice/?.so" logger = nil harbor = 2 ---address = "127.0.0.1:2527" ---master = "127.0.0.1:2013" -address = "172.16.100.201:2527" -master = "172.16.100.209:2013" +address = "127.0.0.1:2527" +master = "127.0.0.1:2013" start = "main_log" luaservice ="./service/?.lua;./test/?.lua;./examples/?.lua" snax = "./examples/?.lua;./test/?.lua" From 9937081854c7b65d0a0557c3630ecbf1d62622d8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 21 Jun 2014 17:01:59 +0800 Subject: [PATCH 033/729] Redesign harbor/master/dummy service --- HISTORY.md | 4 + Makefile | 2 +- examples/globallog.lua | 20 +- lualib-src/lua-seri.c | 12 +- lualib-src/lua-skynet.c | 11 + lualib/skynet.lua | 7 +- lualib/skynet/harbor.lua | 28 +- lualib/socket.lua | 8 +- service-src/service_dummy.c | 319 --------------- service-src/service_harbor.c | 727 ++++++++++++++++------------------- service-src/service_master.c | 314 --------------- service/bootstrap.lua | 31 +- service/cdummy.lua | 47 +++ service/cmaster.lua | 123 ++++++ service/cslave.lua | 222 +++++++++++ service/debug_console.lua | 2 +- 16 files changed, 793 insertions(+), 1084 deletions(-) delete mode 100644 service-src/service_dummy.c delete mode 100644 service-src/service_master.c create mode 100644 service/cdummy.lua create mode 100644 service/cmaster.lua create mode 100644 service/cslave.lua diff --git a/HISTORY.md b/HISTORY.md index e4b2b7f7..a7ce9057 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,10 @@ Dev version * Optimize redis driver `compose_message`. * Add module skynet.harbor for monitor harbor connect/disconnect, see test/testharborlink.lua . * cluster.open support cluster name. +* Add new api skynet.packstring , and skynet.unpack support lua string +* socket.listen support put port into address. (address:port) +* Redesign harbor/master/dummy, remove lots of C code and rewite in lua. +* Remove block connect api, queue sending message during connecting now. v0.3.1 (2014-6-16) ----------- diff --git a/Makefile b/Makefile index 3c5fd446..45631905 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ jemalloc : $(MALLOC_STATICLIB) # skynet -CSERVICE = snlua logger gate master harbor dummy +CSERVICE = snlua logger gate harbor LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ cjson clientsocket memory profile multicast \ cluster diff --git a/examples/globallog.lua b/examples/globallog.lua index 9b4dce4c..ffd0a0f3 100644 --- a/examples/globallog.lua +++ b/examples/globallog.lua @@ -1,24 +1,8 @@ local skynet = require "skynet" -skynet.register_protocol { - name = "text", - id = skynet.PTYPE_TEXT, - pack = function (...) - local n = select ("#" , ...) - if n == 0 then - return "" - elseif n == 1 then - return tostring(...) - else - return table.concat({...}," ") - end - end, - unpack = skynet.tostring -} - skynet.start(function() - skynet.dispatch("text", function(session, address, text) - print("[GLOBALLOG]", skynet.address(address),text) + skynet.dispatch("lua", function(session, address, ...) + print("[GLOBALLOG]", skynet.address(address), ...) end) skynet.register "LOG" end) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 69e1c2af..c6544462 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -581,8 +581,16 @@ _luaseri_unpack(lua_State *L) { if (lua_isnoneornil(L,1)) { return 0; } - void * buffer = lua_touserdata(L,1); - int len = luaL_checkinteger(L,2); + void * buffer; + int len; + if (lua_type(L,1) == LUA_TSTRING) { + size_t sz; + buffer = (void *)lua_tolstring(L,1,&sz); + len = (int)sz; + } else { + buffer = lua_touserdata(L,1); + len = luaL_checkinteger(L,2); + } if (len == 0) { return 0; } diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index db49e997..931de581 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -289,6 +289,16 @@ _harbor(lua_State *L) { return 2; } +static int +lpackstring(lua_State *L) { + _luaseri_pack(L); + char * str = (char *)lua_touserdata(L, -2); + int sz = lua_tointeger(L, -1); + lua_pushlstring(L, str, sz); + skynet_free(str); + return 1; +} + int luaopen_skynet_c(lua_State *L) { luaL_checkversion(L); @@ -303,6 +313,7 @@ luaopen_skynet_c(lua_State *L) { { "harbor", _harbor }, { "pack", _luaseri_pack }, { "unpack", _luaseri_unpack }, + { "packstring", lpackstring }, { "callback", _callback }, { NULL, NULL }, }; diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 93b67d65..385a6d38 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -288,6 +288,7 @@ skynet.redirect = function(dest,source,typename,...) end skynet.pack = assert(c.pack) +skynet.packstring = assert(c.packstring) skynet.unpack = assert(c.unpack) skynet.tostring = assert(c.tostring) @@ -339,8 +340,8 @@ function skynet.dispatch(typename, func) p.dispatch = func end -local function unknown_request(session, address, msg, sz) - print("Unknown request :" , c.tostring(msg,sz)) +local function unknown_request(session, address, msg, sz, prototype) + skynet.error(string.format("Unknown request (%s): %s", prototype, c.tostring(msg,sz))) error(string.format("Unknown session : %d from %x", session, address)) end @@ -394,7 +395,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) session_coroutine_address[co] = source suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...))) else - unknown_request(session, source, msg, sz) + unknown_request(session, source, msg, sz, proto[prototype]) end end end diff --git a/lualib/skynet/harbor.lua b/lualib/skynet/harbor.lua index 06f47bb6..5f470be8 100644 --- a/lualib/skynet/harbor.lua +++ b/lualib/skynet/harbor.lua @@ -2,39 +2,17 @@ local skynet = require "skynet" local harbor = {} -local HARBOR = skynet.getenv "harbor_address" - -if HARBOR then - HARBOR = tonumber("0x" .. string.sub(HARBOR , 2)) -end - function harbor.globalname(name, handle) - assert(HARBOR) handle = handle or skynet.self() - skynet.redirect(HARBOR, handle, "system", 0, "R " .. name) -end - -function harbor.init(h) - assert(HARBOR == nil) - HARBOR = h - skynet.setenv("harbor_address", skynet.address(h)) + skynet.send(".slave", "lua", "REGISTER", name, handle) end function harbor.link(id) - assert(HARBOR) - skynet.call(HARBOR, "system", "M " .. tostring(id)) + skynet.call(".slave", "lua", "LINK", id) end function harbor.connect(id) - assert(HARBOR) - skynet.call(HARBOR, "system", "C " .. tostring(id)) + skynet.call(".slave", "lua", "CONNECT", id) end -skynet.register_protocol { - name = "system", - id = skynet.PTYPE_SYSTEM, - pack = function(...) return ... end, - unpack = skynet.tostring, -} - return harbor diff --git a/lualib/socket.lua b/lualib/socket.lua index 93b9bf4c..240d6ed7 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -271,7 +271,13 @@ function socket.invalid(id) return socket_pool[id] == nil end -socket.listen = assert(driver.listen) +function socket.listen(host, port, backlog) + if port == nil then + host, port = string.match(host, "([^:]+):(.+)$") + port = tonumber(port) + end + return driver.listen(host, port, backlog) +end function socket.lock(id) local s = socket_pool[id] diff --git a/service-src/service_dummy.c b/service-src/service_dummy.c deleted file mode 100644 index a1f19d4c..00000000 --- a/service-src/service_dummy.c +++ /dev/null @@ -1,319 +0,0 @@ -#include "skynet.h" -#include "skynet_harbor.h" - -#include -#include - -#define HASH_SIZE 4096 -#define DEFAULT_QUEUE_SIZE 1024 - -struct msg { - uint8_t * buffer; - size_t size; -}; - -struct msg_queue { - int size; - int head; - int tail; - struct msg * data; -}; - -struct keyvalue { - struct keyvalue * next; - char key[GLOBALNAME_LENGTH]; - uint32_t hash; - uint32_t value; - struct msg_queue * queue; -}; - -struct hashmap { - struct keyvalue *node[HASH_SIZE]; -}; - -/* - message type (8bits) is in destination high 8bits - harbor id (8bits) is also in that place , but remote message doesn't need harbor id. - */ -struct remote_message_header { - uint32_t source; - uint32_t destination; - uint32_t session; -}; - -// 12 is sizeof(struct remote_message_header) -#define HEADER_COOKIE_LENGTH 12 - -struct dummy { - struct skynet_context *ctx; - struct hashmap * map; -}; - -// hash table - -static void -_push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct remote_message_header * header) { - // If there is only 1 free slot which is reserved to distinguish full/empty - // of circular buffer, expand it. - if (((queue->tail + 1) % queue->size) == queue->head) { - struct msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct msg)); - int i; - for (i=0;isize-1;i++) { - new_buffer[i] = queue->data[(i+queue->head) % queue->size]; - } - skynet_free(queue->data); - queue->data = new_buffer; - queue->head = 0; - queue->tail = queue->size - 1; - queue->size *= 2; - } - struct msg * slot = &queue->data[queue->tail]; - queue->tail = (queue->tail + 1) % queue->size; - - slot->buffer = skynet_malloc(sz + sizeof(*header)); - memcpy(slot->buffer, buffer, sz); - memcpy(slot->buffer + sz, header, sizeof(*header)); - slot->size = sz + sizeof(*header); -} - -static struct msg * -_pop_queue(struct msg_queue * queue) { - if (queue->head == queue->tail) { - return NULL; - } - struct msg * slot = &queue->data[queue->head]; - queue->head = (queue->head + 1) % queue->size; - return slot; -} - -static struct msg_queue * -_new_queue() { - struct msg_queue * queue = skynet_malloc(sizeof(*queue)); - queue->size = DEFAULT_QUEUE_SIZE; - queue->head = 0; - queue->tail = 0; - queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct msg)); - - return queue; -} - -static void -_release_queue(struct msg_queue *queue) { - if (queue == NULL) - return; - struct msg * m = _pop_queue(queue); - while (m) { - skynet_free(m->buffer); - m = _pop_queue(queue); - } - skynet_free(queue->data); - skynet_free(queue); -} - -static struct keyvalue * -_hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { - uint32_t *ptr = (uint32_t*) name; - uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; - struct keyvalue * node = hash->node[h % HASH_SIZE]; - while (node) { - if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) { - return node; - } - node = node->next; - } - return NULL; -} - -static struct keyvalue * -_hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { - uint32_t *ptr = (uint32_t *)name; - uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; - struct keyvalue ** pkv = &hash->node[h % HASH_SIZE]; - struct keyvalue * node = skynet_malloc(sizeof(*node)); - memcpy(node->key, name, GLOBALNAME_LENGTH); - node->next = *pkv; - node->queue = NULL; - node->hash = h; - node->value = 0; - *pkv = node; - - return node; -} - -static struct hashmap * -_hash_new() { - struct hashmap * h = skynet_malloc(sizeof(struct hashmap)); - memset(h,0,sizeof(*h)); - return h; -} - -static void -_hash_delete(struct hashmap *hash) { - int i; - for (i=0;inode[i]; - while (node) { - struct keyvalue * next = node->next; - _release_queue(node->queue); - skynet_free(node); - node = next; - } - } - skynet_free(hash); -} - -/////////////// - -struct dummy * -dummy_create(void) { - struct dummy * d = skynet_malloc(sizeof(*d)); - d->map = _hash_new(); - return d; -} - -void -dummy_release(struct dummy *d) { - _hash_delete(d->map); - skynet_free(d); -} - -static inline void -to_bigendian(uint8_t *buffer, uint32_t n) { - buffer[0] = (n >> 24) & 0xff; - buffer[1] = (n >> 16) & 0xff; - buffer[2] = (n >> 8) & 0xff; - buffer[3] = n & 0xff; -} - -static inline void -_header_to_message(const struct remote_message_header * header, uint8_t * message) { - to_bigendian(message , header->source); - to_bigendian(message+4 , header->destination); - to_bigendian(message+8 , header->session); -} - -static inline uint32_t -from_bigendian(uint32_t n) { - union { - uint32_t big; - uint8_t bytes[4]; - } u; - u.big = n; - return u.bytes[0] << 24 | u.bytes[1] << 16 | u.bytes[2] << 8 | u.bytes[3]; -} - -static inline void -_message_to_header(const uint32_t *message, struct remote_message_header *header) { - header->source = from_bigendian(message[0]); - header->destination = from_bigendian(message[1]); - header->session = from_bigendian(message[2]); -} - -static void -_dispatch_queue(struct dummy *h, struct msg_queue * queue, uint32_t handle, const char name[GLOBALNAME_LENGTH] ) { - struct msg * m = _pop_queue(queue); - while (m) { - struct remote_message_header cookie; - uint8_t *ptr = m->buffer + m->size - sizeof(cookie); - memcpy(&cookie, ptr, sizeof(cookie)); - int type = cookie.destination >> HANDLE_REMOTE_SHIFT; - skynet_send(h->ctx, cookie.source, handle , type | PTYPE_TAG_DONTCOPY, cookie.session, m->buffer, m->size - sizeof(cookie)); - m = _pop_queue(queue); - } -} - -static void -_update_name(struct dummy *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { - struct keyvalue * node = _hash_search(h->map, name); - if (node == NULL) { - node = _hash_insert(h->map, name); - } - node->value = handle; - if (node->queue) { - _dispatch_queue(h, node->queue, handle, name); - _release_queue(node->queue); - node->queue = NULL; - } -} - -static void -_send_name(struct dummy *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) { - struct keyvalue * node = _hash_search(h->map, name); - if (node == NULL) { - node = _hash_insert(h->map, name); - } - if (node->value == 0) { - if (node->queue == NULL) { - node->queue = _new_queue(); - } - struct remote_message_header header; - header.source = source; - header.destination = type << HANDLE_REMOTE_SHIFT; - header.session = (uint32_t)session; - _push_queue(node->queue, msg, sz, &header); - } else { - // local message - skynet_send(h->ctx, source, node->value , type | PTYPE_TAG_DONTCOPY, session, (void *)msg, sz); - } -} - -static void -dummy_command(struct dummy * h, const char * msg, size_t sz, int session, uint32_t source) { - switch(msg[0]) { - case 'R' : { - // register global name - const char * name = msg + 2; - int s = (int)sz; - s -= 2; - if (s <=0 || s>= GLOBALNAME_LENGTH) { - skynet_error(h->ctx, "Invalid global name %s", name); - return; - } - struct remote_name rn; - memset(&rn, 0, sizeof(rn)); - memcpy(rn.name, name, s); - rn.handle = source; - _update_name(h, rn.name, rn.handle); - break; - } - case 'C' : - case 'M' : - skynet_error(h->ctx, "Don't support harbor monitor in cluster dummy mode"); - skynet_send(h->ctx, 0, source, PTYPE_ERROR, session, NULL, 0); - break; - default: - skynet_error(h->ctx, "Unknown command %s", msg); - return; - } -} - -static int -_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { - struct dummy * h = ud; - switch (type) { - case PTYPE_SYSTEM: { - dummy_command(h, msg, sz, session, source); - return 0; - } - default: { - // remote message out - const struct remote_message *rmsg = msg; - if (rmsg->destination.handle == 0) { - _send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz); - } else { - // local message - skynet_send(context, source, rmsg->destination.handle , type | PTYPE_TAG_DONTCOPY, session, (void *)rmsg->message, rmsg->sz); - } - return 0; - } - } -} - -int -dummy_init(struct dummy *d, struct skynet_context *ctx, const char * args) { - d->ctx = ctx; - skynet_harbor_start(ctx); - skynet_callback(ctx, d, _mainloop); - - return 0; -} diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index a9cfaec4..8dbf7db4 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -2,6 +2,16 @@ #include "skynet_harbor.h" #include "skynet_socket.h" +/* + harbor listen the PTYPE_HARBOR (in text) + N name : update the global name + S fd id: connect to new harbor , we should send self_id to fd first , and then recv a id (check it), and at last send queue. + A fd id: accept new harbor , we should send self_id to fd , and then send queue. + + If the fd is disconnected, send message to slave in PTYPE_TEXT. D id + If we don't known a globalname, send message to slave in PTYPE_TEXT. Q name + */ + #include #include #include @@ -13,8 +23,22 @@ #define HASH_SIZE 4096 #define DEFAULT_QUEUE_SIZE 1024 +// 12 is sizeof(struct remote_message_header) +#define HEADER_COOKIE_LENGTH 12 + +/* + message type (8bits) is in destination high 8bits + harbor id (8bits) is also in that place , but remote message doesn't need harbor id. + */ +struct remote_message_header { + uint32_t source; + uint32_t destination; + uint32_t session; +}; + struct msg { - uint8_t * buffer; + struct remote_message_header header; + void * buffer; size_t size; }; @@ -37,99 +61,34 @@ struct hashmap { struct keyvalue *node[HASH_SIZE]; }; -/* - message type (8bits) is in destination high 8bits - harbor id (8bits) is also in that place , but remote message doesn't need harbor id. - */ -struct remote_message_header { - uint32_t source; - uint32_t destination; - uint32_t session; -}; +#define STATUS_WAIT 0 +#define STATUS_HANDSHAKE 1 +#define STATUS_HEADER 2 +#define STATUS_CONTENT 3 +#define STATUS_DOWN 4 -struct monitor_response { - uint32_t addr; - int session; +struct slave { + int fd; + struct msg_queue *queue; + int status; + int length; + int read; + uint8_t size[4]; + char * recv_buffer; }; -struct monitor_set { - int cap; - int n; - struct monitor_response * resp; -}; - -// 12 is sizeof(struct remote_message_header) -#define HEADER_COOKIE_LENGTH 12 - struct harbor { struct skynet_context *ctx; - char * local_addr; int id; + uint32_t slave; struct hashmap * map; - int master_fd; - char * master_addr; - int remote_fd[REMOTE_MAX]; - bool connected[REMOTE_MAX]; - char * remote_addr[REMOTE_MAX]; - struct monitor_set * monitor[REMOTE_MAX]; + struct slave s[REMOTE_MAX]; }; -static void -monitor_free(struct harbor *h) { - int i; - for (i=0;imonitor[i]; - if (m) { - skynet_free(m->resp); - skynet_free(m); - h->monitor[i] = NULL; - } - } -} - -static void -monitor_add(struct harbor *h, int id, uint32_t addr, int session) { - struct monitor_set * m = h->monitor[id]; - if (m == NULL) { - m = skynet_malloc(sizeof(*m)); - m->cap = 4; - m->n = 0; - m->resp = skynet_malloc(m->cap * sizeof(struct monitor_response)); - h->monitor[id] = m; - } - if (m->n >= m->cap) { - assert(m->n == m->cap); - struct monitor_response * resp = skynet_malloc(m->cap * 2 * sizeof(struct monitor_response)); - int i; - for (i=0;in;i++) { - resp[i] = m->resp[i]; - } - m->cap *= 2; - skynet_free(m->resp); - m->resp = resp; - } - struct monitor_response * resp = &m->resp[m->n++]; - resp->addr = addr; - resp->session = session; -} - -static void -monitor_clear(struct harbor *h, int id) { - struct monitor_set * m = h->monitor[id]; - if (m) { - int i; - for (i=0;in;i++) { - struct monitor_response * resp = &m->resp[i]; - skynet_send(h->ctx, 0, resp->addr, PTYPE_RESPONSE, resp->session, NULL, 0); - } - m->n = 0; - } -} - // hash table static void -_push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct remote_message_header * header) { +push_queue_msg(struct msg_queue * queue, struct msg * m) { // If there is only 1 free slot which is reserved to distinguish full/empty // of circular buffer, expand it. if (((queue->tail + 1) % queue->size) == queue->head) { @@ -145,16 +104,21 @@ _push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct rem queue->size *= 2; } struct msg * slot = &queue->data[queue->tail]; + *slot = *m; queue->tail = (queue->tail + 1) % queue->size; +} - slot->buffer = skynet_malloc(sz + sizeof(*header)); - memcpy(slot->buffer, buffer, sz); - memcpy(slot->buffer + sz, header, sizeof(*header)); - slot->size = sz + sizeof(*header); +static void +push_queue(struct msg_queue * queue, void * buffer, size_t sz, struct remote_message_header * header) { + struct msg m; + m.header = *header; + m.buffer = buffer; + m.size = sz; + push_queue_msg(queue, &m); } static struct msg * -_pop_queue(struct msg_queue * queue) { +pop_queue(struct msg_queue * queue) { if (queue->head == queue->tail) { return NULL; } @@ -164,7 +128,7 @@ _pop_queue(struct msg_queue * queue) { } static struct msg_queue * -_new_queue() { +new_queue() { struct msg_queue * queue = skynet_malloc(sizeof(*queue)); queue->size = DEFAULT_QUEUE_SIZE; queue->head = 0; @@ -175,20 +139,19 @@ _new_queue() { } static void -_release_queue(struct msg_queue *queue) { +release_queue(struct msg_queue *queue) { if (queue == NULL) return; - struct msg * m = _pop_queue(queue); - while (m) { + struct msg * m; + while ((m=pop_queue(queue)) != NULL) { skynet_free(m->buffer); - m = _pop_queue(queue); } skynet_free(queue->data); skynet_free(queue); } static struct keyvalue * -_hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { +hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { uint32_t *ptr = (uint32_t*) name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue * node = hash->node[h % HASH_SIZE]; @@ -206,7 +169,7 @@ _hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { // Don't support erase name yet static struct void -_hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) { +hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) { uint32_t *ptr = name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue ** ptr = &hash->node[h % HASH_SIZE]; @@ -224,7 +187,7 @@ _hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) { */ static struct keyvalue * -_hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { +hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { uint32_t *ptr = (uint32_t *)name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue ** pkv = &hash->node[h % HASH_SIZE]; @@ -240,20 +203,20 @@ _hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { } static struct hashmap * -_hash_new() { +hash_new() { struct hashmap * h = skynet_malloc(sizeof(struct hashmap)); memset(h,0,sizeof(*h)); return h; } static void -_hash_delete(struct hashmap *hash) { +hash_delete(struct hashmap *hash) { int i; for (i=0;inode[i]; while (node) { struct keyvalue * next = node->next; - _release_queue(node->queue); + release_queue(node->queue); skynet_free(node); node = next; } @@ -263,62 +226,49 @@ _hash_delete(struct hashmap *hash) { /////////////// +static void +close_harbor(struct harbor *h, int id) { + struct slave *s = &h->s[id]; + s->status = STATUS_DOWN; + if (s->fd) { + skynet_socket_close(h->ctx, s->fd); + } + if (s->queue) { + release_queue(s->queue); + s->queue = NULL; + } +} + +static void +report_harbor_down(struct harbor *h, int id) { + char down[64]; + int n = sprintf(down, "D %d",id); + + skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, down, n); +} + struct harbor * harbor_create(void) { struct harbor * h = skynet_malloc(sizeof(*h)); - h->ctx = NULL; - h->id = 0; - h->master_fd = -1; - h->master_addr = NULL; - int i; - for (i=0;iremote_fd[i] = -1; - h->connected[i] = false; - h->remote_addr[i] = NULL; - h->monitor[i] = NULL; - } - h->map = _hash_new(); + memset(h,0,sizeof(*h)); + h->map = hash_new(); return h; } void harbor_release(struct harbor *h) { - struct skynet_context *ctx = h->ctx; - if (h->master_fd >= 0) { - skynet_socket_close(ctx, h->master_fd); - } - skynet_free(h->master_addr); - skynet_free(h->local_addr); int i; - for (i=0;iremote_fd[i] >= 0) { - skynet_socket_close(ctx, h->remote_fd[i]); - skynet_free(h->remote_addr[i]); + for (i=1;is[i]; + if (s->fd && s->status != STATUS_DOWN) { + close_harbor(h,i); + report_harbor_down(h,i); } } - _hash_delete(h->map); - monitor_free(h); + hash_delete(h->map); skynet_free(h); } -static int -_connect_to(struct harbor *h, const char *ipaddress) { - char * port = strchr(ipaddress,':'); - if (port==NULL) { - return -1; - } - int sz = port - ipaddress; - char tmp[sz + 1]; - memcpy(tmp,ipaddress,sz); - tmp[sz] = '\0'; - - int portid = strtol(port+1, NULL,10); - - skynet_error(h->ctx, "Harbor(%d) connect to %s:%d", h->id, tmp, portid); - - return skynet_socket_connect(h->ctx, tmp, portid); -} - static inline void to_bigendian(uint8_t *buffer, uint32_t n) { buffer[0] = (n >> 24) & 0xff; @@ -328,7 +278,7 @@ to_bigendian(uint8_t *buffer, uint32_t n) { } static inline void -_header_to_message(const struct remote_message_header * header, uint8_t * message) { +header_to_message(const struct remote_message_header * header, uint8_t * message) { to_bigendian(message , header->source); to_bigendian(message+4 , header->destination); to_bigendian(message+8 , header->session); @@ -345,122 +295,200 @@ from_bigendian(uint32_t n) { } static inline void -_message_to_header(const uint32_t *message, struct remote_message_header *header) { +message_to_header(const uint32_t *message, struct remote_message_header *header) { header->source = from_bigendian(message[0]); header->destination = from_bigendian(message[1]); header->session = from_bigendian(message[2]); } -static void -_send_package(struct skynet_context *ctx, int fd, const void * buffer, size_t sz) { - uint8_t * sendbuf = skynet_malloc(sz+4); - to_bigendian(sendbuf, sz); - memcpy(sendbuf+4, buffer, sz); +// socket package - if (skynet_socket_send(ctx, fd, sendbuf, sz+4)) { - skynet_error(ctx, "Send to %d error", fd); - } +static void +forward_local_messsage(struct harbor *h, void *msg, int sz) { + const char * cookie = msg; + cookie += sz - HEADER_COOKIE_LENGTH; + struct remote_message_header header; + message_to_header((const uint32_t *)cookie, &header); + + uint32_t destination = header.destination; + int type = (destination >> HANDLE_REMOTE_SHIFT) | PTYPE_TAG_DONTCOPY; + destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); + + skynet_send(h->ctx, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH); } static void -_send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) { +send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) { uint32_t sz_header = sz+sizeof(*cookie); uint8_t * sendbuf = skynet_malloc(sz_header+4); to_bigendian(sendbuf, sz_header); memcpy(sendbuf+4, buffer, sz); - _header_to_message(cookie, sendbuf+4+sz); + header_to_message(cookie, sendbuf+4+sz); - if (skynet_socket_send(ctx, fd, sendbuf, sz_header+4)) { - skynet_error(ctx, "Remote send to %d error", fd); - } + // ignore send error, because if the connection is broken, the mainloop will recv a message. + skynet_socket_send(ctx, fd, sendbuf, sz_header+4); } static void -response_close(struct harbor *h, int id) { - if (h->connected[id]) { - monitor_clear(h, id); - } - h->connected[id] = false; -} - -static void -_update_remote_address(struct harbor *h, int harbor_id, const char * ipaddr) { - if (harbor_id == h->id) { - return; - } - assert(harbor_id > 0 && harbor_id< REMOTE_MAX); - struct skynet_context * context = h->ctx; - if (h->remote_fd[harbor_id] >=0) { - skynet_socket_close(context, h->remote_fd[harbor_id]); - skynet_free(h->remote_addr[harbor_id]); - h->remote_addr[harbor_id] = NULL; - } - h->remote_fd[harbor_id] = _connect_to(h, ipaddr); - response_close(h, harbor_id); -} - -static void -_dispatch_queue(struct harbor *h, struct msg_queue * queue, uint32_t handle, const char name[GLOBALNAME_LENGTH] ) { +dispatch_name_queue(struct harbor *h, struct keyvalue * node) { + struct msg_queue * queue = node->queue; + uint32_t handle = node->value; int harbor_id = handle >> HANDLE_REMOTE_SHIFT; assert(harbor_id != 0); struct skynet_context * context = h->ctx; - int fd = h->remote_fd[harbor_id]; - if (fd < 0) { - char tmp [GLOBALNAME_LENGTH+1]; - memcpy(tmp, name , GLOBALNAME_LENGTH); - tmp[GLOBALNAME_LENGTH] = '\0'; - skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id); + struct slave *s = &h->s[harbor_id]; + int fd = s->fd; + if (fd == 0) { + if (s->status == STATUS_DOWN) { + char tmp [GLOBALNAME_LENGTH+1]; + memcpy(tmp, node->key, GLOBALNAME_LENGTH); + tmp[GLOBALNAME_LENGTH] = '\0'; + skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id); + } else { + if (s->queue == NULL) { + s->queue = node->queue; + node->queue = NULL; + } else { + struct msg * m; + while ((m = pop_queue(queue))!=NULL) { + push_queue_msg(s->queue, m); + } + } + } return; } - struct msg * m = _pop_queue(queue); - while (m) { - struct remote_message_header cookie; - uint8_t *ptr = m->buffer + m->size - sizeof(cookie); - memcpy(&cookie, ptr, sizeof(cookie)); - cookie.destination |= (handle & HANDLE_MASK); - _header_to_message(&cookie, ptr); - _send_package(context, fd, m->buffer, m->size); - m = _pop_queue(queue); + struct msg * m; + while ((m = pop_queue(queue)) != NULL) { + m->header.destination |= (handle & HANDLE_MASK); + send_remote(context, fd, m->buffer, m->size, &m->header); } } static void -_update_remote_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { - struct keyvalue * node = _hash_search(h->map, name); +dispatch_queue(struct harbor *h, int id) { + struct slave *s = &h->s[id]; + int fd = s->fd; + assert(fd != 0); + + struct msg_queue *queue = s->queue; + if (queue == NULL) + return; + + struct msg * m; + while ((m = pop_queue(queue)) != NULL) { + send_remote(h->ctx, fd, m->buffer, m->size, &m->header); + } + release_queue(queue); + s->queue = NULL; +} + +static void +push_socket_data(struct harbor *h, const struct skynet_socket_message * message) { + assert(message->type == SKYNET_SOCKET_TYPE_DATA); + int fd = message->id; + int i; + int id = 0; + struct slave * s = NULL; + for (i=1;is[i].fd == fd) { + s = &h->s[i]; + id = i; + break; + } + } + if (s == NULL) { + skynet_free(message->buffer); + skynet_error(h->ctx, "Invalid socket fd (%d) data", fd); + return; + } + uint8_t * buffer = (uint8_t *)message->buffer; + int size = message->ud; + + for (;;) { + switch(s->status) { + case STATUS_HANDSHAKE: { + // check id + uint8_t remote_id = buffer[0]; + if (remote_id != id) { + skynet_error(h->ctx, "Invalid shakehand id (%d) from fd = %d , harbor = %d", id, fd, remote_id); + close_harbor(h,id); + return; + } + ++buffer; + --size; + s->status = STATUS_HEADER; + + dispatch_queue(h, id); + + if (size == 0) { + break; + } + // go though + } + case STATUS_HEADER: + if (size < 4) { + s->length = size; + memcpy(s->size, buffer, size); + return; + } else { + // big endian 4 bytes length + if (buffer[0] != 0) { + skynet_error(h->ctx, "Message is too long from harbor %d", id); + close_harbor(h,id); + return; + } + s->length = buffer[1] << 16 | buffer[2] << 8 | buffer[3]; + s->read = 0; + s->recv_buffer = skynet_malloc(s->length); + s->status = STATUS_CONTENT; + buffer += 4; + size -= 4; + if (size == 0) { + return; + } + } + // go though + case STATUS_CONTENT: + if (size < s->length - s->read) { + memcpy(s->recv_buffer + s->read, buffer, size); + s->read += size; + return; + } + int need = s->length - s->read; + memcpy(s->recv_buffer + s->read, buffer, need); + forward_local_messsage(h, s->recv_buffer, s->length); + s->length = 0; + s->read = 0; + s->recv_buffer = NULL; + size -= need; + buffer += need; + s->status = STATUS_HEADER; + if (size == 0) + return; + break; + default: + return; + } + } +} + +static void +update_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { + struct keyvalue * node = hash_search(h->map, name); if (node == NULL) { - node = _hash_insert(h->map, name); + node = hash_insert(h->map, name); } node->value = handle; if (node->queue) { - _dispatch_queue(h, node->queue, handle, name); - _release_queue(node->queue); + dispatch_name_queue(h, node); + release_queue(node->queue); node->queue = NULL; } } -static void -_request_master(struct harbor *h, const char name[GLOBALNAME_LENGTH], size_t i, uint32_t handle) { - uint8_t buffer[4+i]; - to_bigendian(buffer, handle); - memcpy(buffer+4,name,i); - - if (h->master_fd >= 0) { - _send_package(h->ctx, h->master_fd, buffer, 4+i); - } -} - -/* - update global name to master - - 2 bytes (size) - 4 bytes (handle) (handle == 0 for request) - n bytes string (name) - */ - static int -_remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) { +remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) { int harbor_id = destination >> HANDLE_REMOTE_SHIFT; - assert(harbor_id != 0); struct skynet_context * context = h->ctx; if (harbor_id == h->id) { // local message @@ -468,95 +496,63 @@ _remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int return 1; } - int fd = h->remote_fd[harbor_id]; - if (fd >= 0 && h->connected[harbor_id]) { + struct slave * s = &h->s[harbor_id]; + if (s->fd == 0 || s->status == STATUS_HANDSHAKE) { + if (s->status == STATUS_DOWN) { + // throw an error return to source + // report the destination is dead + skynet_send(context, destination, source, PTYPE_ERROR, 0 , NULL, 0); + skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz); + } else { + struct remote_message_header header; + header.source = source; + header.destination = type << HANDLE_REMOTE_SHIFT; + header.session = (uint32_t)session; + push_queue(s->queue, (void *)msg, sz, &header); + return 1; + } + } else { struct remote_message_header cookie; cookie.source = source; cookie.destination = (destination & HANDLE_MASK) | ((uint32_t)type << HANDLE_REMOTE_SHIFT); cookie.session = (uint32_t)session; - _send_remote(context, fd, msg,sz,&cookie); - } else { - // throw an error return to source - // report the destination is dead - skynet_send(context, destination, source, PTYPE_ERROR, 0 , NULL, 0); - skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz); + send_remote(context, s->fd, msg,sz,&cookie); } + return 0; } -static void -_remote_register_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { - int i; - for (i=0;imap, name); +remote_send_name(struct harbor *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) { + struct keyvalue * node = hash_search(h->map, name); if (node == NULL) { - node = _hash_insert(h->map, name); + node = hash_insert(h->map, name); } if (node->value == 0) { if (node->queue == NULL) { - node->queue = _new_queue(); + node->queue = new_queue(); } struct remote_message_header header; header.source = source; header.destination = type << HANDLE_REMOTE_SHIFT; header.session = (uint32_t)session; - _push_queue(node->queue, msg, sz, &header); - // 0 for request - _remote_register_name(h, name, 0); + push_queue(node->queue, (void *)msg, sz, &header); + char query[2+GLOBALNAME_LENGTH+1] = "Q "; + query[2+GLOBALNAME_LENGTH] = 0; + memcpy(query+2, name, GLOBALNAME_LENGTH); + skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, query, strlen(query)); return 1; } else { - return _remote_send_handle(h, source, node->value, type, session, msg, sz); + return remote_send_handle(h, source, node->value, type, session, msg, sz); } } -static int -harbor_id(struct harbor *h, int fd) { - int i; - for (i=1;iremote_fd[i] == fd) - return i; - } - return 0; -} - static void -close_harbor(struct harbor *h, int fd) { - if (fd == h->master_fd) { - skynet_socket_close(h->ctx, fd); - skynet_error(h->ctx, "Master disconnected"); - h->master_fd = -1; - return; - } - int id = harbor_id(h,fd); - - if (id == 0) - return; - skynet_error(h->ctx, "Harbor %d closed",id); - skynet_socket_close(h->ctx, fd); - h->remote_fd[id] = -1; - response_close(h,id); -} - -static void -open_harbor(struct harbor *h, int fd) { - int id = harbor_id(h,fd); - if (id == 0) - return; - assert(h->connected[id] == false); - monitor_clear(h, id); - h->connected[id] = true; - skynet_error(h->ctx, "Harbor(%d) connected", id); +handshake(struct harbor *h, int id) { + struct slave *s = &h->s[id]; + uint8_t * handshake = skynet_malloc(1); + handshake[0] = (uint8_t)h->id; + skynet_socket_send(h->ctx, s->fd, handshake, 1); } static void @@ -565,8 +561,7 @@ harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint int s = (int)sz; s -= 2; switch(msg[0]) { - case 'R' : { - // register global name + case 'N' : { if (s <=0 || s>= GLOBALNAME_LENGTH) { skynet_error(h->ctx, "Invalid global name %s", name); return; @@ -575,35 +570,35 @@ harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint memset(&rn, 0, sizeof(rn)); memcpy(rn.name, name, s); rn.handle = source; - _remote_register_name(h, rn.name, rn.handle); + update_name(h, rn.name, rn.handle); break; } - case 'C' : - case 'M' : { - if (s <= 0) { - skynet_error(h->ctx, "Invalid harbor montior"); - skynet_send(h->ctx, 0, source, PTYPE_ERROR, session, NULL, 0); + case 'S' : + case 'A' : { + char buffer[s+1]; + memcpy(buffer, name, s); + buffer[s] = 0; + int fd=0, id=0; + sscanf(buffer, "%d %d",&fd,&id); + if (fd == 0 || id <= 0 || id>=REMOTE_MAX) { + skynet_error(h->ctx, "Invalid command %c %s", msg[0], buffer); return; } - int hid = strtol(name, NULL, 10); - if (hid <= 0 || hid >= REMOTE_MAX) { - skynet_error(h->ctx, "Invalid harbor montior id : %s", name); - skynet_send(h->ctx, 0, source, PTYPE_ERROR, session, NULL, 0); + struct slave * slave = &h->s[id]; + if (slave->fd != 0) { + skynet_error(h->ctx, "Harbor %d alreay exist", id); return; } - if (msg[0] == 'M') { - if (!h->connected[hid]) { - skynet_send(h->ctx, 0, source, PTYPE_RESPONSE, session, NULL, 0); - return; - } + slave->fd = fd; + + skynet_socket_start(h->ctx, fd); + handshake(h, id); + if (msg[0] == 'S') { + slave->status = STATUS_HANDSHAKE; } else { - assert(msg[0] == 'C'); - if (h->connected[hid]) { - skynet_send(h->ctx, 0, source, PTYPE_RESPONSE, session, NULL, 0); - return; - } + slave->status = STATUS_HEADER; + dispatch_queue(h,id); } - monitor_add(h, hid, source, session); break; } default: @@ -613,63 +608,48 @@ harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint } static int -_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { +harbor_id(struct harbor *h, int fd) { + int i; + for (i=1;is[i]; + if (s->fd == fd) { + return i; + } + } + return 0; +} + +static int +mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct harbor * h = ud; switch (type) { case PTYPE_SOCKET: { const struct skynet_socket_message * message = msg; switch(message->type) { case SKYNET_SOCKET_TYPE_DATA: + push_socket_data(h, message); skynet_free(message->buffer); - skynet_error(context, "recv invalid socket message (size=%d)", message->ud); - break; - case SKYNET_SOCKET_TYPE_ACCEPT: - skynet_error(context, "recv invalid socket accept message"); break; case SKYNET_SOCKET_TYPE_ERROR: - case SKYNET_SOCKET_TYPE_CLOSE: - close_harbor(h, message->id); + case SKYNET_SOCKET_TYPE_CLOSE: { + int id = harbor_id(h, message->id); + if (id) { + report_harbor_down(h,id); + } else { + skynet_error(context, "Unkown fd (%d) closed", message->id); + } break; + } case SKYNET_SOCKET_TYPE_CONNECT: - open_harbor(h, message->id); + // fd forward to this service + break; + default: + skynet_error(context, "recv invalid socket message type %d", type); break; } return 0; } case PTYPE_HARBOR: { - // remote message in - const char * cookie = msg; - cookie += sz - HEADER_COOKIE_LENGTH; - struct remote_message_header header; - _message_to_header((const uint32_t *)cookie, &header); - if (header.source == 0) { - if (header.destination < REMOTE_MAX) { - // 1 byte harbor id (0~255) - // update remote harbor address - char ip [sz - HEADER_COOKIE_LENGTH + 1]; - memcpy(ip, msg, sz-HEADER_COOKIE_LENGTH); - ip[sz-HEADER_COOKIE_LENGTH] = '\0'; - _update_remote_address(h, header.destination, ip); - } else { - // update global name - if (sz - HEADER_COOKIE_LENGTH > GLOBALNAME_LENGTH) { - char name[sz-HEADER_COOKIE_LENGTH+1]; - memcpy(name, msg, sz-HEADER_COOKIE_LENGTH); - name[sz-HEADER_COOKIE_LENGTH] = '\0'; - skynet_error(context, "Global name is too long %s", name); - } - _update_remote_name(h, msg, header.destination); - } - } else { - uint32_t destination = header.destination; - int type = (destination >> HANDLE_REMOTE_SHIFT) | PTYPE_TAG_DONTCOPY; - destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); - skynet_send(context, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH); - return 1; - } - return 0; - } - case PTYPE_SYSTEM: { harbor_command(h, msg,sz,session,source); return 0; } @@ -677,11 +657,11 @@ _mainloop(struct skynet_context * context, void * ud, int type, int session, uin // remote message out const struct remote_message *rmsg = msg; if (rmsg->destination.handle == 0) { - if (_remote_send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz)) { + if (remote_send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz)) { return 0; } } else { - if (_remote_send_handle(h, source , rmsg->destination.handle, type, session, rmsg->message, rmsg->sz)) { + if (remote_send_handle(h, source , rmsg->destination.handle, type, session, rmsg->message, rmsg->sz)) { return 0; } } @@ -691,48 +671,19 @@ _mainloop(struct skynet_context * context, void * ud, int type, int session, uin } } -static void -_launch_gate(struct skynet_context * ctx, const char * local_addr) { - char tmp[128]; - sprintf(tmp,"gate L ! %s %d %d 0",local_addr, PTYPE_HARBOR, REMOTE_MAX); - const char * gate_addr = skynet_command(ctx, "LAUNCH", tmp); - if (gate_addr == NULL) { - fprintf(stderr, "Harbor : launch gate failed\n"); - exit(1); - } - uint32_t gate = strtoul(gate_addr+1 , NULL, 16); - if (gate == 0) { - fprintf(stderr, "Harbor : launch gate invalid %s", gate_addr); - exit(1); - } - const char * self_addr = skynet_command(ctx, "REG", NULL); - int n = sprintf(tmp,"broker %s",self_addr); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, tmp, n); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, "start", 5); -} - int harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) { h->ctx = ctx; - int sz = strlen(args)+1; - char master_addr[sz]; - char local_addr[sz]; int harbor_id = 0; - sscanf(args,"%s %s %d",master_addr, local_addr, &harbor_id); - h->master_addr = skynet_strdup(master_addr); - h->id = harbor_id; - h->master_fd = _connect_to(h, master_addr); - if (h->master_fd == -1) { - fprintf(stderr, "Harbor: Connect to master failed\n"); - exit(1); + uint32_t slave = 0; + sscanf(args,"%d %u", &harbor_id, &slave); + if (slave == 0) { + return 1; } + h->id = harbor_id; + h->slave = slave; + skynet_callback(ctx, h, mainloop); skynet_harbor_start(ctx); - h->local_addr = skynet_strdup(local_addr); - - _launch_gate(ctx, local_addr); - skynet_callback(ctx, h, _mainloop); - _request_master(h, local_addr, strlen(local_addr), harbor_id); - return 0; } diff --git a/service-src/service_master.c b/service-src/service_master.c deleted file mode 100644 index a4fb8b1b..00000000 --- a/service-src/service_master.c +++ /dev/null @@ -1,314 +0,0 @@ -#include "skynet.h" -#include "skynet_harbor.h" -#include "skynet_socket.h" - -#include -#include -#include -#include -#include -#include - -#define HASH_SIZE 4096 - -struct name { - struct name * next; - char key[GLOBALNAME_LENGTH]; - uint32_t hash; - uint32_t value; -}; - -struct namemap { - struct name *node[HASH_SIZE]; -}; - -struct master { - struct skynet_context *ctx; - int remote_fd[REMOTE_MAX]; - bool connected[REMOTE_MAX]; - char * remote_addr[REMOTE_MAX]; - struct namemap map; -}; - -struct master * -master_create() { - struct master *m = skynet_malloc(sizeof(*m)); - int i; - for (i=0;iremote_fd[i] = -1; - m->remote_addr[i] = NULL; - m->connected[i] = false; - } - memset(&m->map, 0, sizeof(m->map)); - return m; -} - -void -master_release(struct master * m) { - int i; - struct skynet_context *ctx = m->ctx; - for (i=0;iremote_fd[i]; - if (fd >= 0) { - assert(ctx); - skynet_socket_close(ctx, fd); - } - skynet_free(m->remote_addr[i]); - } - for (i=0;imap.node[i]; - while (node) { - struct name * next = node->next; - skynet_free(node); - node = next; - } - } - skynet_free(m); -} - -static struct name * -_search_name(struct master *m, char name[GLOBALNAME_LENGTH]) { - uint32_t *ptr = (uint32_t *) name; - uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; - struct name * node = m->map.node[h % HASH_SIZE]; - while (node) { - if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) { - return node; - } - node = node->next; - } - return NULL; -} - -static struct name * -_insert_name(struct master *m, char name[GLOBALNAME_LENGTH]) { - uint32_t *ptr = (uint32_t *)name; - uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; - struct name **pname = &m->map.node[h % HASH_SIZE]; - struct name * node = skynet_malloc(sizeof(*node)); - memcpy(node->key, name, GLOBALNAME_LENGTH); - node->next = *pname; - node->hash = h; - node->value = 0; - *pname = node; - return node; -} - -static void -_copy_name(char *name, const char * buffer, size_t sz) { - if (sz < GLOBALNAME_LENGTH) { - memcpy(name, buffer, sz); - memset(name+sz, 0 , GLOBALNAME_LENGTH - sz); - } else { - memcpy(name, buffer, GLOBALNAME_LENGTH); - } -} - -static void -_connect_to(struct master *m, int id) { - assert(m->connected[id] == false); - struct skynet_context * ctx = m->ctx; - const char *ipaddress = m->remote_addr[id]; - char * portstr = strchr(ipaddress,':'); - if (portstr==NULL) { - skynet_error(ctx, "Harbor %d : address invalid (%s)",id, ipaddress); - return; - } - int sz = portstr - ipaddress; - char tmp[sz + 1]; - memcpy(tmp,ipaddress,sz); - tmp[sz] = '\0'; - int port = strtol(portstr+1,NULL,10); - skynet_error(ctx, "Master connect to harbor(%d) %s:%d", id, tmp, port); - m->remote_fd[id] = skynet_socket_connect(ctx, tmp, port); - m->connected[id] = true; -} - -static inline void -to_bigendian(uint8_t *buffer, uint32_t n) { - buffer[0] = (n >> 24) & 0xff; - buffer[1] = (n >> 16) & 0xff; - buffer[2] = (n >> 8) & 0xff; - buffer[3] = n & 0xff; -} - -static void -_send_to(struct master *m, int id, const void * buf, int sz, uint32_t handle) { - uint8_t * buffer= (uint8_t *)skynet_malloc(4 + sz + 12); - to_bigendian(buffer, sz+12); - memcpy(buffer+4, buf, sz); - to_bigendian(buffer+4+sz, 0); - to_bigendian(buffer+4+sz+4, handle); - to_bigendian(buffer+4+sz+8, 0); - - sz += 4 + 12; - - if (skynet_socket_send(m->ctx, m->remote_fd[id], buffer, sz)) { - skynet_error(m->ctx, "Harbor %d : send error", id); - } -} - -static void -_broadcast(struct master *m, const char *name, size_t sz, uint32_t handle) { - int i; - for (i=1;iremote_fd[i]; - if (fd < 0 || m->connected[i]==false) - continue; - _send_to(m, i , name, sz, handle); - } -} - -static void -_request_name(struct master *m, const char * buffer, size_t sz) { - char name[GLOBALNAME_LENGTH]; - _copy_name(name, buffer, sz); - struct name * n = _search_name(m, name); - if (n == NULL) { - return; - } - _broadcast(m, name, GLOBALNAME_LENGTH, n->value); -} - -static void -_update_name(struct master *m, uint32_t handle, const char * buffer, size_t sz) { - char name[GLOBALNAME_LENGTH]; - _copy_name(name, buffer, sz); - struct name * n = _search_name(m, name); - if (n==NULL) { - n = _insert_name(m,name); - } - n->value = handle; - _broadcast(m,name,GLOBALNAME_LENGTH, handle); -} - -static void -close_harbor(struct master *m, int harbor_id) { - if (m->connected[harbor_id]) { - struct skynet_context * context = m->ctx; - skynet_socket_close(context, m->remote_fd[harbor_id]); - m->remote_fd[harbor_id] = -1; - m->connected[harbor_id] = false; - } -} - -static void -_update_address(struct master *m, int harbor_id, const char * buffer, size_t sz) { - if (m->remote_fd[harbor_id] >= 0) { - close_harbor(m, harbor_id); - } - skynet_free(m->remote_addr[harbor_id]); - char * addr = skynet_malloc(sz+1); - memcpy(addr, buffer, sz); - addr[sz] = '\0'; - m->remote_addr[harbor_id] = addr; - _connect_to(m, harbor_id); -} - -static int -socket_id(struct master *m, int id) { - int i; - for (i=1;iremote_fd[i] == id) - return i; - } - return 0; -} - -static void -on_connected(struct master *m, int id) { - _broadcast(m, m->remote_addr[id], strlen(m->remote_addr[id]), id); -// m->connected[id] = true; - int i; - for (i=1;iremote_addr[i]; - if (addr == NULL || m->connected[i] == false) { - continue; - } - _send_to(m, id , addr, strlen(addr), i); - } -} - -static void -dispatch_socket(struct master *m, const struct skynet_socket_message *msg, int sz) { - int id = socket_id(m, msg->id); - switch(msg->type) { - case SKYNET_SOCKET_TYPE_CONNECT: - assert(id); - on_connected(m, id); - break; - case SKYNET_SOCKET_TYPE_ERROR: - skynet_error(m->ctx, "socket error on harbor %d", id); - // go though, close socket - case SKYNET_SOCKET_TYPE_CLOSE: - close_harbor(m, id); - break; - default: - skynet_error(m->ctx, "Invalid socket message type %d", msg->type); - break; - } -} - - -/* - update global name to master - - 4 bytes (handle) (handle == 0 for request) - n bytes string (name) - */ - -static int -_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { - if (type == PTYPE_SOCKET) { - dispatch_socket(ud, msg, (int)sz); - return 0; - } - if (type != PTYPE_HARBOR) { - skynet_error(context, "None harbor message recv from %x (type = %d)", source, type); - return 0; - } - assert(sz >= 4); - struct master *m = ud; - const uint8_t *handlen = msg; - uint32_t handle = handlen[0]<<24 | handlen[1]<<16 | handlen[2]<<8 | handlen[3]; - sz -= 4; - const char * name = msg; - name += 4; - - if (handle == 0) { - _request_name(m , name, sz); - } else if (handle < REMOTE_MAX) { - _update_address(m , handle, name, sz); - } else { - _update_name(m , handle, name, sz); - } - - return 0; -} - -int -master_init(struct master *m, struct skynet_context *ctx, const char * args) { - char tmp[strlen(args) + 32]; - sprintf(tmp,"gate L ! %s %d %d 0",args,PTYPE_HARBOR,REMOTE_MAX); - const char * gate_addr = skynet_command(ctx, "LAUNCH", tmp); - if (gate_addr == NULL) { - skynet_error(ctx, "Master : launch gate failed"); - return 1; - } - uint32_t gate = strtoul(gate_addr+1, NULL, 16); - if (gate == 0) { - skynet_error(ctx, "Master : launch gate invalid %s", gate_addr); - return 1; - } - const char * self_addr = skynet_command(ctx, "REG", NULL); - int n = sprintf(tmp,"broker %s",self_addr); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, tmp, n); - skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, "start", 5); - - skynet_callback(ctx, m, _mainloop); - - m->ctx = ctx; - return 0; -} diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 78f98930..11303577 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -3,29 +3,36 @@ local harbor = require "skynet.harbor" skynet.start(function() local standalone = skynet.getenv "standalone" + + local launcher = assert(skynet.launch("snlua","launcher")) + skynet.name(".launcher", launcher) + local harbor_id = tonumber(skynet.getenv "harbor") if harbor_id == 0 then assert(standalone == nil) standalone = true skynet.setenv("standalone", "true") - local dummy = assert(skynet.launch("dummy")) - harbor.init(dummy) - else - local master_addr = skynet.getenv "master" + local slave = skynet.newservice "cdummy" + if slave == nil then + skynet.abort() + end + skynet.name(".slave", slave) + + else if standalone then - assert(skynet.launch("master", standalone)) + if not skynet.newservice "cmaster" then + skynet.abort() + end end - local local_addr = skynet.getenv "address" - - local h = assert(skynet.launch("harbor",master_addr, local_addr, harbor_id)) - harbor.init(h) + local slave = skynet.newservice "cslave" + if slave == nil then + skynet.abort() + end + skynet.name(".slave", slave) end - local launcher = assert(skynet.launch("snlua","launcher")) - skynet.name(".launcher", launcher) - if standalone then local datacenter = assert(skynet.newservice "datacenterd") skynet.name("DATACENTER", datacenter) diff --git a/service/cdummy.lua b/service/cdummy.lua new file mode 100644 index 00000000..fddb2c01 --- /dev/null +++ b/service/cdummy.lua @@ -0,0 +1,47 @@ +local skynet = require "skynet" + +local globalname = {} +local harbor = {} + +skynet.register_protocol { + name = "harbor", + id = skynet.PTYPE_HARBOR, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +skynet.register_protocol { + name = "text", + id = skynet.PTYPE_TEXT, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +function harbor.REGISTER(name, handle) + assert(globalname[name] == nil) + globalname[name] = handle + skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) +end + +function harbor.LINK(id) + skynet.ret() +end + +function harbor.CONNECT(id) + skynet.error("Can't connect to other harbor in single node mode") +end + +skynet.start(function() + local harbor_id = tonumber(skynet.getenv "harbor") + assert(harbor_id == 0) + + skynet.dispatch("lua", function (session,source,command,...) + local f = assert(harbor[command]) + f(...) + end) + skynet.dispatch("text", function(session,source,command) + -- ignore all the command + end) + + harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self())) +end) diff --git a/service/cmaster.lua b/service/cmaster.lua new file mode 100644 index 00000000..9c38515b --- /dev/null +++ b/service/cmaster.lua @@ -0,0 +1,123 @@ +local skynet = require "skynet" +local socket = require "socket" + +--[[ + master manage data : + 1. all the slaves address : id -> ipaddr:port + 2. all the global names : name -> address + + master hold connections from slaves . + + protocol slave->master : + package size 1 byte + type 1 byte : + 'H' : HANDSHAKE, report slave id, and address. + 'R' : REGISTER name address + 'Q' : QUERY name + + + protocol master->slave: + package size 1 byte + type 1 byte : + 'W' : WAIT n + 'C' : CONNECT slave_id slave_address + 'N' : NAME globalname address + 'D' : DISCONNECT slave_id +]] + +local slave_node = {} +local global_name = {} + +local function read_package(fd) + local sz = socket.read(fd, 1) + assert(sz, "closed") + sz = string.byte(sz) + local content = socket.read(fd, sz) + return skynet.unpack(content) +end + +local function pack_package(...) + local message = skynet.packstring(...) + local size = #message + assert(size <= 255 , "too long") + return string.char(size) .. message +end + +local function report_slave(fd, slave_id, slave_addr) + local message = pack_package("C", slave_id, slave_addr) + local n = 0 + for k,v in pairs(slave_node) do + socket.write(v.fd, message) + n = n + 1 + end + socket.write(fd, pack_package("W", n)) +end + +local function handshake(fd) + local t, slave_id, slave_addr = read_package(fd) + assert(t=='H', "Invalid handshake type " .. t) + assert(slave_id ~= 0 , "Invalid slave id 0") + if slave_node[slave_id] then + error(string.format("Slave %d already register on %s", slave_id, slave_node[slave_id].addr)) + end + report_slave(fd, slave_id, slave_addr) + slave_node[slave_id] = { + fd = fd, + id = slave_id, + addr = slave_addr, + } + return slave_id , slave_addr +end + +local function dispatch_slave(fd) + local t, name, address = read_package(fd) + if t == 'R' then + -- register name + assert(type(address)=="number", "Invalid request") + if not global_name[name] then + global_name[name] = address + end + local message = pack_package("N", name, address) + for k,v in pairs(slave_node) do + socket.write(v.fd, message) + end + elseif t == 'Q' then + -- query name + local address = global_name[name] + if address then + socket.write(fd, pack_package("N", name, address)) + end + else + skynet.error("Invalid slave message type " .. t) + end +end + +local function monitor_slave(slave_id, slave_address) + local fd = slave_node[slave_id].fd + skynet.error(string.format("Harbor %d (fd=%d) report %s", slave_id, fd, slave_address)) + while pcall(dispatch_slave, fd) do end + skynet.error("slave " ..slave_id .. " is down") + local message = pack_package("D", slave_id) + slave_node[slave_id].fd = 0 + for k,v in pairs(slave_node) do + socket.write(v.fd, message) + end + socket.close(fd) +end + +skynet.start(function() + local master_addr = skynet.getenv "standalone" + skynet.error("master listen socket " .. tostring(master_addr)) + local fd = socket.listen(master_addr) + socket.start(fd , function(id, addr) + skynet.error("connect from " .. addr .. " " .. id) + socket.start(id) + local ok, slave, slave_addr = pcall(handshake, id) + if ok then + skynet.fork(monitor_slave, slave, slave_addr) + else + skynet.error(string.format("disconnect fd = %d, error = %s", id, slave)) + socket.close(id) + end + end) +end) diff --git a/service/cslave.lua b/service/cslave.lua new file mode 100644 index 00000000..e03c1b83 --- /dev/null +++ b/service/cslave.lua @@ -0,0 +1,222 @@ +local skynet = require "skynet" +local socket = require "socket" + +local slaves = {} +local connect_queue = {} +local globalname = {} +local harbor = {} +local harbor_service +local monitor = {} + +local function read_package(fd) + local sz = socket.read(fd, 1) + assert(sz, "closed") + sz = string.byte(sz) + local content = socket.read(fd, sz) + return skynet.unpack(content) +end + +local function pack_package(...) + local message = skynet.packstring(...) + local size = #message + assert(size <= 255 , "too long") + return string.char(size) .. message +end + +local function monitor_clear(id) + local v = monitor[id] + if v then + monitor[id] = nil + for _, v in ipairs(v) do + skynet.redirect(v.address, 0, "response", v.session, "") + end + end +end + +local function connect_slave(slave_id, address) + local ok, err = pcall(function() + if slaves[slave_id] == nil then + local fd = socket.open(address) + skynet.error(string.format("Connect to harbor %d (fd=%d), %s", slave_id, fd, address)) + slaves[slave_id] = fd + monitor_clear(slave_id) + socket.abandon(fd) + skynet.send(harbor_service, "harbor", string.format("S %d %d",fd,slave_id)) + end + end) + if not ok then + skynet.error(err) + end +end + +local function ready() + local queue = connect_queue + connect_queue = nil + for k,v in pairs(queue) do + connect_slave(k,v) + end + for name,address in pairs(globalname) do + skynet.redirect(harbor_service, address, "harbor", "N " .. name) + end +end + +local function monitor_master(master_fd) + while true do + local ok, t, id_name, address = pcall(read_package,master_fd) + if ok then + if t == 'C' then + if connect_queue then + connect_queue[id_name] = address + else + connect_slave(id_name, address) + end + elseif t == 'N' then + globalname[id_name] = address + if connect_queue == nil then + skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name) + end + elseif t == 'D' then + local fd = slaves[id_name] + slaves[id_name] = false + if fd then + socket.close(fd) + end + end + else + skynet.error("Master disconnect") + socket.close(master_fd) + break + end + end +end + +local function accept_slave(fd) + socket.start(fd) + local id = socket.read(fd, 1) + if not id then + skynet.error(string.format("Connection (fd =%d) closed", fd)) + socket.close(fd) + return + end + id = string.byte(id) + if slaves[id] ~= nil then + skynet.error(string.format("Slave %d exist (fd =%d)", id, fd)) + socket.close(fd) + return + end + slaves[id] = fd + monitor_clear(id) + socket.abandon(fd) + skynet.error(string.format("Harbor %d connected (fd = %d)", id, fd)) + skynet.send(harbor_service, "harbor", string.format("A %d %d", fd, id)) +end + +skynet.register_protocol { + name = "harbor", + id = skynet.PTYPE_HARBOR, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +skynet.register_protocol { + name = "text", + id = skynet.PTYPE_TEXT, + pack = function(...) return ... end, + unpack = skynet.tostring, +} + +local function monitor_harbor(master_fd) + return function(session, source, command) + local t = string.sub(command, 1, 1) + local arg = string.sub(command, 3) + if t == "Q" then + -- query name + if globalname[arg] then + skynet.redirect(harbor_service, globalname[arg], "harbor", "N " .. arg) + else + socket.write(master_fd, pack_package("Q", arg)) + end + elseif t == "D" then + -- harbor down + local id = tonumber(arg) + if slaves[id] then + monitor_clear(id) + end + slaves[id] = false + else + skynet.error("Unknown command ", command) + end + end +end + +function harbor.REGISTER(_,_, fd, name, handle) + assert(globalname[name] == nil) + globalname[name] = handle + socket.write(fd, pack_package("R", name, handle)) + skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) +end + +function harbor.LINK(session, source, fd, id) + if slaves[id] then + if monitor[id] == nil then + monitor[id] = {} + end + table.insert(monitor[id], { address = source, session = session }) + else + skynet.ret() + end +end + +function harbor.CONNECT(session, source, fd, id) + if not slaves[id] then + if monitor[id] == nil then + monitor[id] = {} + end + table.insert(monitor[id], { address = source, session = session }) + else + skynet.ret() + end +end + +skynet.start(function() + local master_addr = skynet.getenv "master" + local harbor_id = tonumber(skynet.getenv "harbor") + local slave_address = assert(skynet.getenv "address") + local slave_fd = socket.listen(slave_address) + skynet.error("slave connect to master " .. tostring(master_addr)) + local master_fd = socket.open(master_addr) + + skynet.dispatch("lua", function (session,source,command,...) + local f = assert(harbor[command]) + f(session, source, master_fd, ...) + end) + skynet.dispatch("text", monitor_harbor(master_fd)) + + harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self())) + + local hs_message = pack_package("H", harbor_id, slave_address) + socket.write(master_fd, hs_message) + local t, n = read_package(master_fd) + assert(t == "W" and type(n) == "number", "slave shakehand failed") + skynet.error(string.format("Waiting for %d harbors", n)) + skynet.fork(monitor_master, master_fd) + if n > 0 then + local co = coroutine.running() + socket.start(slave_fd, function(fd, addr) + skynet.error(string.format("New connection (fd = %d, %s)",fd, addr)) + if pcall(accept_slave,fd) then + local s = 0 + for k,v in pairs(slaves) do + s = s + 1 + end + if s >= n then + skynet.wakeup(co) + end + end + end) + skynet.wait() + end + socket.close(slave_fd) + skynet.error("Shakehand ready") + skynet.fork(ready) +end) diff --git a/service/debug_console.lua b/service/debug_console.lua index d49dd809..b15c3889 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -88,7 +88,7 @@ end skynet.start(function() local listen_socket = socket.listen ("127.0.0.1", port) - print("Start debug console at 127.0.0.1",port) + skynet.error("Start debug console at 127.0.0.1 " .. port) socket.start(listen_socket , function(id, addr) local function print(...) local t = { ... } From d910686cb8a08e0228c7bde3fe9c09ccaef2c5fb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 21 Jun 2014 18:03:08 +0800 Subject: [PATCH 034/729] cluster bugfix : add socket.header to decode bigendian header --- lualib-src/lua-socket.c | 40 ++++++++++++++++++++++------------------ lualib/socket.lua | 1 + service/clusterd.lua | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 2428914e..ad717f94 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -188,10 +188,29 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) { luaL_pushresult(&b); } +static int +lheader(lua_State *L) { + size_t len; + const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 1, &len); + if (len > 4 || len < 1) { + return luaL_error(L, "Invalid read %s", s); + } + int i; + size_t sz = 0; + for (i=0;i<(int)len;i++) { + sz <<= 8; + sz |= s[i]; + } + + lua_pushunsigned(L, sz); + + return 1; +} + /* userdata send_buffer table pool - integer sz or string (big endian) + integer sz */ static int lpopbuffer(lua_State *L) { @@ -200,23 +219,7 @@ lpopbuffer(lua_State *L) { return luaL_error(L, "Need buffer object at param 1"); } luaL_checktype(L,2,LUA_TTABLE); - int type = lua_type(L,3); - int sz; - if (type == LUA_TNUMBER) { - sz = lua_tointeger(L,3); - } else { - size_t len; - const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 3, &len); - if (len > 4 || len < 1) { - return luaL_error(L, "Invalid read %s", s); - } - int i; - sz = 0; - for (i=0;i<(int)len;i++) { - sz <<= 8; - sz |= s[i]; - } - } + int sz = luaL_checkinteger(L,3); if (sb->size < sz || sz == 0) { lua_pushnil(L); } else { @@ -485,6 +488,7 @@ luaopen_socketdriver(lua_State *L) { { "clear", lclearbuffer }, { "readline", lreadline }, { "str2p", lstr2p }, + { "header", lheader }, { "unpack", lunpack }, { NULL, NULL }, diff --git a/lualib/socket.lua b/lualib/socket.lua index 93b9bf4c..f5319ccf 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -266,6 +266,7 @@ end socket.write = assert(driver.send) socket.lwrite = assert(driver.lsend) +socket.header = assert(driver.header) function socket.invalid(id) return socket_pool[id] == nil diff --git a/service/clusterd.lua b/service/clusterd.lua index ef6bff68..563869ac 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -10,7 +10,7 @@ local node_session = {} local command = {} local function read_response(sock) - local sz = sock:read(2) + local sz = socket.header(sock:read(2)) local msg = sock:read(sz) return cluster.unpackresponse(msg) -- session, ok, data end From 2128a82571eb138430d46dfaf39357ac71223ce4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 23 Jun 2014 11:01:20 +0800 Subject: [PATCH 035/729] update history, and release v0.3.2 --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 9d39efaf..89dd918e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +v0.3.2 (2014-6-23) +---------- +* Bugifx : mongo driver and lua-bson . (objectid encoding, and gc problem when mongdo driver reply bson object). +* Bugfix : cluster (double free). +* Add socket.header() to decode big-endian package header (and fix the bug in cluster). + v0.3.1 (2014-6-16) ----------- * Bugfix: lua mongo driver . Hold reply string before decode bson data. From cc2cffacb72c0aa304f2b074416108b0593f3610 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 24 Jun 2014 11:33:29 +0800 Subject: [PATCH 036/729] bugfix: master, don't report offline slave --- service/cmaster.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/service/cmaster.lua b/service/cmaster.lua index 9c38515b..59bc61f1 100644 --- a/service/cmaster.lua +++ b/service/cmaster.lua @@ -47,8 +47,10 @@ local function report_slave(fd, slave_id, slave_addr) local message = pack_package("C", slave_id, slave_addr) local n = 0 for k,v in pairs(slave_node) do - socket.write(v.fd, message) - n = n + 1 + if v.fd ~= 0 then + socket.write(v.fd, message) + n = n + 1 + end end socket.write(fd, pack_package("W", n)) end From d30a206a15831bdd3933ddda1f1fc96311beae1f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 24 Jun 2014 11:51:52 +0800 Subject: [PATCH 037/729] bugfix: typo --- service/cslave.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/cslave.lua b/service/cslave.lua index e03c1b83..71625818 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -132,7 +132,7 @@ local function monitor_harbor(master_fd) if t == "Q" then -- query name if globalname[arg] then - skynet.redirect(harbor_service, globalname[arg], "harbor", "N " .. arg) + skynet.redirect(harbor_service, globalname[arg], "harbor", 0, "N " .. arg) else socket.write(master_fd, pack_package("Q", arg)) end From 6733134ad47783b6e2d01b388d8456e039fe1c77 Mon Sep 17 00:00:00 2001 From: "87414772@qq.com" <87414772@qq.com> Date: Tue, 24 Jun 2014 13:11:05 +0800 Subject: [PATCH 038/729] mysql lib --- 3rd/lua-mysqlaux/lua_mysqlaux.c | 372 +++++++++++++++ Makefile | 5 +- examples/config.mysql | 11 + examples/main_mysql.lua | 10 + lualib/mysql.lua | 801 ++++++++++++++++++++++++++++++++ test/testmysql.lua | 32 ++ 6 files changed, 1230 insertions(+), 1 deletion(-) create mode 100755 3rd/lua-mysqlaux/lua_mysqlaux.c create mode 100644 examples/config.mysql create mode 100644 examples/main_mysql.lua create mode 100755 lualib/mysql.lua create mode 100644 test/testmysql.lua diff --git a/3rd/lua-mysqlaux/lua_mysqlaux.c b/3rd/lua-mysqlaux/lua_mysqlaux.c new file mode 100755 index 00000000..6b639d7c --- /dev/null +++ b/3rd/lua-mysqlaux/lua_mysqlaux.c @@ -0,0 +1,372 @@ +// +// lua_mysqlaux.c +// +// Created by changfeng on 6/17/14. +// Copyright (c) 2014 changfeng. All rights reserved. +// +#include +#include +#include + +#include +#include + +#define SHA1SIZE 20 +#define ROTL(bits,word) (((word) << (bits)) | ((word) >> (32-(bits)))) +typedef unsigned int uint32_t; + +struct sha +{ + uint32_t digest[5]; + uint32_t w[80]; + uint32_t a,b,c,d,e,f; + int err; +}; + + +static uint32_t padded_length_in_bits(uint32_t len) +{ + if(len%64 == 56) + { + len++; + } + while((len%64)!=56) + { + len++; + } + return len*8; +} + + +static int calculate_sha1(struct sha *sha1, unsigned char *text, uint32_t length) +{ + unsigned int i,j; + unsigned char *buffer=NULL, *pbuffer=NULL; + uint32_t bits=0; + uint32_t temp=0,k=0; + uint32_t lb = length*8; + + if (!sha1) + { + return 0; + } + // initialize the default digest values + sha1->digest[0] = 0x67452301; + sha1->digest[1] = 0xEFCDAB89; + sha1->digest[2] = 0x98BADCFE; + sha1->digest[3] = 0x10325476; + sha1->digest[4] = 0xC3D2E1F0; + sha1->a=sha1->b=sha1->c=sha1->d=sha1->e=sha1->f=0; + if (!text || !length) + { + return 0; + } + + bits = padded_length_in_bits(length); + buffer = (unsigned char *) malloc((bits/8)+8); + memset(buffer,0,(bits/8)+8); + if(buffer == NULL) + { + return 1; + } + pbuffer = buffer; + memcpy(buffer, text, length); + + + //add 1 on the last of the message.. + *(buffer+length) = 0x80; + for(i=length+1; i<(bits/8); i++) + { + *(buffer+i) = 0x00; + } + + *(buffer +(bits/8)+4+0) = (lb>>24) & 0xFF; + *(buffer +(bits/8)+4+1) = (lb>>16) & 0xFF; + *(buffer +(bits/8)+4+2) = (lb>>8) & 0xFF; + *(buffer +(bits/8)+4+3) = (lb>>0) & 0xFF; + + + //main loop + for(i=0; i<((bits+64)/512); i++) + { + //first empty the block for each pass.. + for(j=0; j<80; j++) + { + sha1->w[j] = 0x00; + } + + + //fill the first 16 words with the characters read directly from the buffer. + for(j=0; j<16; j++) + { + sha1->w[j] =buffer[j*4+0]; + sha1->w[j] = sha1->w[j]<<8; + sha1->w[j] |= buffer[j*4+1]; + sha1->w[j] = sha1->w[j]<<8; + sha1->w[j] |= buffer[j*4+2]; + sha1->w[j] = sha1->w[j]<<8; + sha1->w[j] |= buffer[j*4+3]; + } + + //fill the rest 64 words using the formula + for(j=16; j<80; j++) + { + sha1->w[j] = (ROTL(1,(sha1->w[j-3] ^ sha1->w[j-8] ^ sha1->w[j-14] ^ sha1->w[j-16]))); + } + + + //initialize hash for this chunck reading that has been stored in the structure digest + sha1->a = sha1->digest[0]; + sha1->b = sha1->digest[1]; + sha1->c = sha1->digest[2]; + sha1->d = sha1->digest[3]; + sha1->e = sha1->digest[4]; + + //for all the 80 32bit blocks calculate f and use k accordingly per specification. + for(j=0; j<80; j++) + { + if((j>=0) && (j<20)) + { + sha1->f = ((sha1->b)&(sha1->c)) | ((~(sha1->b))&(sha1->d)); + k = 0x5A827999; + + } + else if((j>=20) && (j<40)) + { + sha1->f = (sha1->b)^(sha1->c)^(sha1->d); + k = 0x6ED9EBA1; + } + else if((j>=40) && (j<60)) + { + sha1->f = ((sha1->b)&(sha1->c)) | ((sha1->b)&(sha1->d)) | ((sha1->c)&(sha1->d)); + k = 0x8F1BBCDC; + } + else if((j>=60) && (j<80)) + { + sha1->f = (sha1->b)^(sha1->c)^(sha1->d); + k = 0xCA62C1D6; + } + + temp = ROTL(5,(sha1->a)) + (sha1->f) + (sha1->e) + k + sha1->w[j]; + sha1->e = (sha1->d); + sha1->d = (sha1->c); + sha1->c = ROTL(30,(sha1->b)); + sha1->b = (sha1->a); + sha1->a = temp; + + //reset temp to 0 to be in safe side only, not mandatory. + temp =0x00; + + + } + + // append to total hash. + sha1->digest[0] += sha1->a; + sha1->digest[1] += sha1->b; + sha1->digest[2] += sha1->c; + sha1->digest[3] += sha1->d; + sha1->digest[4] += sha1->e; + + + //since we used 512bit size block per each pass, let us update the buffer pointer accordingly. + buffer = buffer+64; + + } + free(pbuffer); + return 0; +} + +static void int2ch4(int intVal,unsigned char *result) +{ + result[0]= (unsigned char)((intVal>>24) & 0x000000ff); + result[1]= (unsigned char)((intVal>>16) & 0x000000ff); + result[2]= (unsigned char)((intVal>> 8) & 0x000000ff); + result[3]= (unsigned char)((intVal>> 0) & 0x000000ff); +} + + +static int sha1_bin (lua_State *L) { + void * msg = NULL; + size_t len =0; + + if( lua_gettop(L) != 1 ){ + return 0; + } + if( lua_isnil(L,1) ) { + msg = NULL; + len =0; + }else{ + msg=luaL_checklstring(L,1,&len); + } + struct sha tmpsha; + calculate_sha1( &tmpsha, msg, (uint32_t)len); + unsigned char tmpret[SHA1SIZE+8]; + memset(tmpret,0,SHA1SIZE+8); + int i=0; + for ( i=0; i<5; i++) + { + int2ch4(tmpsha.digest[i], tmpret+i*4); + } + + lua_pushlstring(L, (char *)tmpret, SHA1SIZE); + return 1; +} + +static unsigned int num_escape_sql_str(unsigned char *dst, unsigned char *src, size_t size) +{ + unsigned int n =0; + while (size) { + /* the highest bit of all the UTF-8 chars + * is always 1 */ + if ((*src & 0x80) == 0) { + switch (*src) { + case '\0': + case '\b': + case '\n': + case '\r': + case '\t': + case 26: /* \z */ + case '\\': + case '\'': + case '"': + n++; + break; + default: + break; + } + } + src++; + size--; + } + return n; +} +static unsigned char* +escape_sql_str(unsigned char *dst, unsigned char *src, size_t size) +{ + + while (size) { + if ((*src & 0x80) == 0) { + switch (*src) { + case '\0': + *dst++ = '\\'; + *dst++ = '0'; + break; + + case '\b': + *dst++ = '\\'; + *dst++ = 'b'; + break; + + case '\n': + *dst++ = '\\'; + *dst++ = 'n'; + break; + + case '\r': + *dst++ = '\\'; + *dst++ = 'r'; + break; + + case '\t': + *dst++ = '\\'; + *dst++ = 't'; + break; + + case 26: + *dst++ = '\\'; + *dst++ = 'z'; + break; + + case '\\': + *dst++ = '\\'; + *dst++ = '\\'; + break; + + case '\'': + *dst++ = '\\'; + *dst++ = '\''; + break; + + case '"': + *dst++ = '\\'; + *dst++ = '"'; + break; + + default: + *dst++ = *src; + break; + } + } else { + *dst++ = *src; + } + src++; + size--; + } /* while (size) */ + + return dst; +} + + + + +static int +quote_sql_str(lua_State *L) +{ + size_t len, dlen, escape; + unsigned char *p; + unsigned char *src, *dst; + + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one argument"); + } + + src = (unsigned char *) luaL_checklstring(L, 1, &len); + + if (len == 0) { + dst = (unsigned char *) "''"; + dlen = sizeof("''") - 1; + lua_pushlstring(L, (char *) dst, dlen); + return 1; + } + + escape = num_escape_sql_str(NULL, src, len); + + dlen = sizeof("''") - 1 + len + escape; + p = lua_newuserdata(L, dlen); + + dst = p; + + *p++ = '\''; + + if (escape == 0) { + memcpy(p, src, len); + p+=len; + } else { + p = (unsigned char *) escape_sql_str(p, src, len); + } + + *p++ = '\''; + + if (p != dst + dlen) { + return luaL_error(L, "quote sql string error"); + } + + lua_pushlstring(L, (char *) dst, p - dst); + + return 1; +} + + +static struct luaL_Reg mysqlauxlib[] = { + {"sha1_bin", sha1_bin}, + {"quote_sql_str",quote_sql_str}, + {NULL, NULL} +}; + + +int luaopen_mysqlaux_c (lua_State *L) { + lua_newtable(L); + luaL_setfuncs(L, mysqlauxlib, 0); + return 1; +} + diff --git a/Makefile b/Makefile index 3c5fd446..03a4903f 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ jemalloc : $(MALLOC_STATICLIB) CSERVICE = snlua logger gate master harbor dummy LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ cjson clientsocket memory profile multicast \ - cluster + cluster mysqlaux SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -110,6 +110,9 @@ $(LUA_CLIB_PATH)/multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ +$(LUA_CLIB_PATH)/mysqlaux.so : 3rd/lua-mysqlaux/lua_mysqlaux.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so diff --git a/examples/config.mysql b/examples/config.mysql new file mode 100644 index 00000000..8f23a02d --- /dev/null +++ b/examples/config.mysql @@ -0,0 +1,11 @@ +root = "./" +thread = 8 +logger = nil +harbor = 0 +start = "main_mysql" -- main script +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +lualoader = "lualib/loader.lua" +snax = root.."examples/?.lua;"..root.."test/?.lua" +cpath = root.."cservice/?.so" +-- daemon = "./skynet.pid" diff --git a/examples/main_mysql.lua b/examples/main_mysql.lua new file mode 100644 index 00000000..8a92b2cd --- /dev/null +++ b/examples/main_mysql.lua @@ -0,0 +1,10 @@ +local skynet = require "skynet" + + +skynet.start(function() + print("Main Server start") + local console = skynet.newservice("testmysql") + + print("Main Server exit") + skynet.exit() +end) diff --git a/lualib/mysql.lua b/lualib/mysql.lua new file mode 100755 index 00000000..137a12fa --- /dev/null +++ b/lualib/mysql.lua @@ -0,0 +1,801 @@ +-- Copyright (C) 2012 Yichun Zhang (agentzh) +-- Copyright (C) 2014 Chang Feng +local socketchannel = require "socketchannel" +local bit = require "bit32" +local mysqlaux = require "mysqlaux.c" + + + +local sub = string.sub +local strbyte = string.byte +local strchar = string.char +local strfind = string.find +local strrep = string.rep +local null = nil +local band = bit.band +local bxor = bit.bxor +local bor = bit.bor +local lshift = bit.lshift +local rshift = bit.rshift +local sha1= mysqlaux.sha1_bin +local concat = table.concat +local unpack = unpack +local setmetatable = setmetatable +local error = error +local tonumber = tonumber +local new_tab = function (narr, nrec) return {} end + + +local _M = { _VERSION = '0.13' } +-- constants + +local STATE_CONNECTED = 1 +local STATE_COMMAND_SENT = 2 + +local COM_QUERY = 0x03 + +local SERVER_MORE_RESULTS_EXISTS = 8 + +-- 16MB - 1, the default max allowed packet size used by libmysqlclient +local FULL_PACKET_SIZE = 16777215 + + +local mt = { __index = _M } + + +-- mysql field value type converters +local converters = new_tab(0, 8) + +for i = 0x01, 0x05 do + -- tiny, short, long, float, double + converters[i] = tonumber +end +-- converters[0x08] = tonumber -- long long +converters[0x09] = tonumber -- int24 +converters[0x0d] = tonumber -- year +converters[0xf6] = tonumber -- newdecimal + + +local function _get_byte2(data, i) + local a, b = strbyte(data, i, i + 1) + return bor(a, lshift(b, 8)), i + 2 +end + + +local function _get_byte3(data, i) + local a, b, c = strbyte(data, i, i + 2) + return bor(a, lshift(b, 8), lshift(c, 16)), i + 3 +end + + +local function _get_byte4(data, i) + local a, b, c, d = strbyte(data, i, i + 3) + return bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24)), i + 4 +end + + +local function _get_byte8(data, i) + local a, b, c, d, e, f, g, h = strbyte(data, i, i + 7) + + -- XXX workaround for the lack of 64-bit support in bitop: + local lo = bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24)) + local hi = bor(e, lshift(f, 8), lshift(g, 16), lshift(h, 24)) + return lo + hi * 4294967296, i + 8 + + -- return bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24), lshift(e, 32), + -- lshift(f, 40), lshift(g, 48), lshift(h, 56)), i + 8 +end + + +local function _set_byte2(n) + return strchar(band(n, 0xff), band(rshift(n, 8), 0xff)) +end + + +local function _set_byte3(n) + return strchar(band(n, 0xff), + band(rshift(n, 8), 0xff), + band(rshift(n, 16), 0xff)) +end + + +local function _set_byte4(n) + return strchar(band(n, 0xff), + band(rshift(n, 8), 0xff), + band(rshift(n, 16), 0xff), + band(rshift(n, 24), 0xff)) +end + + +local function _from_cstring(data, i) + local last = strfind(data, "\0", i, true) + if not last then + return nil, nil + end + + return sub(data, i, last), last + 1 +end + + +local function _to_cstring(data) + return data .. "\0" +end + + +local function _to_binary_coded_string(data) + return strchar(#data) .. data +end + + +local function _dump(data) + local len = #data + local bytes = new_tab(len, 0) + for i = 1, len do + bytes[i] = strbyte(data, i) + end + return concat(bytes, " ") +end + + + +local function _dumphex(bytes) + local result ={} + + for i = 1, string.len(bytes) do + local charcode = tonumber(strbyte(bytes, i, i)) + local hexstr = string.format("%02X", charcode) + result[i]=hexstr + end + + local res=table.concat(result, " ") + return res +end + + +local function _compute_token(password, scramble) + if password == "" then + return "" + end + --_dump(scramble) + --print("password=",password) + --print("password:", password, "scramble: ", _dumphex(scramble) ) + local stage1 = sha1(password) + --print("stage1:", _dumphex(stage1) ) + local stage2 = sha1(stage1) + --print("stage2:", _dumphex(stage2) ) + local stage3 = sha1(scramble .. stage2) + --print("stage3:", _dumphex(stage3) ) + local n = #stage1 + local bytes = new_tab(n, 0) + for i = 1, n do + bytes[i] = strchar(bxor(strbyte(stage3, i), strbyte(stage1, i))) + end + + return concat(bytes) +end + +local function _compose_packet(self, req, size) + self.packet_no = self.packet_no + 1 + + local packet = _set_byte3(size) .. strchar(self.packet_no) .. req + return packet +end + + +local function _send_packet(self, req, size) + local sock = self.sock + + self.packet_no = self.packet_no + 1 + + --print("packet no: ", self.packet_no) + + local packet = _set_byte3(size) .. strchar(self.packet_no) .. req + + --print("sending packet...") + + --return sock:send(packet) + return socket.write(self.sock,packet) +end + + +local function _recv_packet(self,sock) + + + local data = sock:read( 4) + if not data then + return nil, nil, "failed to receive packet header: " + end + --print("_recv_packet data type:" ,type(data) ) + --print("packet header: ", _dump(data)) + + local len, pos = _get_byte3(data, 1) + + --print("recv_packet packet length: ", len) + + if len == 0 then + return nil, nil, "empty packet" + end + + if len > self._max_packet_size then + return nil, nil, "packet size too big: " .. len + end + + local num = strbyte(data, pos) + + --print("recv packet: packet no: ", num) + + self.packet_no = num + + --data, err = sock:receive(len) + + data = sock:read(len) + + + if not data then + return nil, nil, "failed to read packet content: " + end + + --print("packet content: ", _dump(data)) + --print("packet content (ascii): ", data) + + local field_count = strbyte(data, 1) + --print("field count:",field_count) + local typ + if field_count == 0x00 then + typ = "OK" + elseif field_count == 0xff then + typ = "ERR" + elseif field_count == 0xfe then + typ = "EOF" + elseif field_count <= 250 then + typ = "DATA" + end + + --print("recv packet: typ= ", typ) + return data, typ +end + + +local function _from_length_coded_bin(data, pos) + local first = strbyte(data, pos) + + --print("LCB: first: ", first) + + if not first then + return nil, pos + end + + if first >= 0 and first <= 250 then + return first, pos + 1 + end + + if first == 251 then + return null, pos + 1 + end + + if first == 252 then + pos = pos + 1 + return _get_byte2(data, pos) + end + + if first == 253 then + pos = pos + 1 + return _get_byte3(data, pos) + end + + if first == 254 then + pos = pos + 1 + return _get_byte8(data, pos) + end + + return false, pos + 1 +end + + +local function _from_length_coded_str(data, pos) + local len + len, pos = _from_length_coded_bin(data, pos) + if len == nil or len == null then + return null, pos + end + + return sub(data, pos, pos + len - 1), pos + len +end + + +local function _parse_ok_packet(packet) + local res = new_tab(0, 5) + local pos + + res.affected_rows, pos = _from_length_coded_bin(packet, 2) + + --print("affected rows: ", res.affected_rows, ", pos:", pos) + + res.insert_id, pos = _from_length_coded_bin(packet, pos) + + --print("insert id: ", res.insert_id, ", pos:", pos) + + res.server_status, pos = _get_byte2(packet, pos) + + --print("server status: ", res.server_status, ", pos:", pos) + + res.warning_count, pos = _get_byte2(packet, pos) + + --print("warning count: ", res.warning_count, ", pos: ", pos) + + local message = sub(packet, pos) + if message and message ~= "" then + res.message = message + end + + --print("message: ", res.message, ", pos:", pos) + + return res +end + + +local function _parse_eof_packet(packet) + local pos = 2 + + local warning_count, pos = _get_byte2(packet, pos) + local status_flags = _get_byte2(packet, pos) + + return warning_count, status_flags +end + + +local function _parse_err_packet(packet) + local errno, pos = _get_byte2(packet, 2) + local marker = sub(packet, pos, pos) + local sqlstate + if marker == '#' then + -- with sqlstate + pos = pos + 1 + sqlstate = sub(packet, pos, pos + 5 - 1) + pos = pos + 5 + end + + local message = sub(packet, pos) + return errno, message, sqlstate +end + + +local function _parse_result_set_header_packet(packet) + local field_count, pos = _from_length_coded_bin(packet, 1) + + local extra + extra = _from_length_coded_bin(packet, pos) + + return field_count, extra +end + + +local function _parse_field_packet(data) + local col = new_tab(0, 2) + local catalog, db, table, orig_table, orig_name, charsetnr, length + local pos + catalog, pos = _from_length_coded_str(data, 1) + + --print("catalog: ", col.catalog, ", pos:", pos) + + db, pos = _from_length_coded_str(data, pos) + table, pos = _from_length_coded_str(data, pos) + orig_table, pos = _from_length_coded_str(data, pos) + col.name, pos = _from_length_coded_str(data, pos) + + orig_name, pos = _from_length_coded_str(data, pos) + + pos = pos + 1 -- ignore the filler + + charsetnr, pos = _get_byte2(data, pos) + + length, pos = _get_byte4(data, pos) + + col.type = strbyte(data, pos) + + --[[ + pos = pos + 1 + + col.flags, pos = _get_byte2(data, pos) + + col.decimals = strbyte(data, pos) + pos = pos + 1 + + local default = sub(data, pos + 2) + if default and default ~= "" then + col.default = default + end + --]] + + return col +end + + +local function _parse_row_data_packet(data, cols, compact) + local pos = 1 + local ncols = #cols + local row + if compact then + row = new_tab(ncols, 0) + else + row = new_tab(0, ncols) + end + for i = 1, ncols do + local value + value, pos = _from_length_coded_str(data, pos) + local col = cols[i] + local typ = col.type + local name = col.name + + --print("row field value: ", value, ", type: ", typ) + + if value ~= null then + local conv = converters[typ] + if conv then + value = conv(value) + end + end + + if compact then + row[i] = value + + else + row[name] = value + end + end + + return row +end + + +local function _recv_field_packet(self, sock) + local packet, typ, err = _recv_packet(self, sock) + if not packet then + return nil, err + end + + if typ == "ERR" then + local errno, msg, sqlstate = _parse_err_packet(packet) + return nil, msg, errno, sqlstate + end + + if typ ~= 'DATA' then + return nil, "bad field packet type: " .. typ + end + + -- typ == 'DATA' + + return _parse_field_packet(packet) +end + +local function _recv_decode_packet_resp(self) + return function(sock) + return true, _recv_packet(self,sock) + end +end + +local function _recv_auth_resp(self) + return function(sock) + --print("recv auth resp") + local packet, typ, err = _recv_packet(self,sock) + if not packet then + --print("recv auth resp : failed to receive the result packet") + error ("failed to receive the result packet"..err) + end + + --print("receive auth response packet type: ",typ) + if typ == 'ERR' then + local errno, msg, sqlstate = _parse_err_packet(packet) + error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + end + + if typ == 'EOF' then + error "old pre-4.1 authentication protocol not supported" + end + + if typ ~= 'OK' then + error "bad packet type: " + end + return true, true + end +end + + +local function _mysql_login(self,user,password,database) + + return function(sockchannel) + local packet, typ, err = sockchannel:response( _recv_decode_packet_resp(self) ) + --local aat={} + if not packet then + error( err ) + end + + if typ == "ERR" then + local errno, msg, sqlstate = _parse_err_packet(packet) + error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + end + + self.protocol_ver = strbyte(packet) + + --print("protocol version: ", self.protocol_ver) + + local server_ver, pos = _from_cstring(packet, 2) + if not server_ver then + error "bad handshake initialization packet: bad server version" + end + + --print("server version: ", server_ver) + + self._server_ver = server_ver + + + local thread_id, pos = _get_byte4(packet, pos) + + --print("thread id: ", thread_id) + + local scramble1 = sub(packet, pos, pos + 8 - 1) + --print("scramble1:",_dump(scramble1), "pos:",pos) + if not scramble1 then + error "1st part of scramble not found" + end + + pos = pos + 9 -- skip filler + + -- two lower bytes + self._server_capabilities, pos = _get_byte2(packet, pos) + + --print("server capabilities: ", self._server_capabilities) + + self._server_lang = strbyte(packet, pos) + pos = pos + 1 + + --print("server lang: ", self._server_lang) + + self._server_status, pos = _get_byte2(packet, pos) + + --print("server status: ", self._server_status) + + local more_capabilities + more_capabilities, pos = _get_byte2(packet, pos) + + self._server_capabilities = bor(self._server_capabilities, + lshift(more_capabilities, 16)) + + --print("server capabilities: ", self._server_capabilities) + + + -- local len = strbyte(packet, pos) + local len = 21 - 8 - 1 + + --print("scramble len: ", len) + + pos = pos + 1 + 10 + + local scramble_part2 = sub(packet, pos, pos + len - 1) + if not scramble_part2 then + error "2nd part of scramble not found" + end + + + local scramble = scramble1..scramble_part2 + --print("scramble:",_dump(scramble) ) + local token = _compute_token(password, scramble) + + -- local client_flags = self._server_capabilities + local client_flags = 260047; + + --print("token: ", _dump(token)) + + local req = _set_byte4(client_flags) + .. _set_byte4(self._max_packet_size) + .. "\0" -- TODO: add support for charset encoding + .. strrep("\0", 23) + .. _to_cstring(user) + .. _to_binary_coded_string(token) + .. _to_cstring(database) + + local packet_len = 4 + 4 + 1 + 23 + #user + 1 + + #token + 1 + #database + 1 + + --print("packet content length: ", packet_len) + --print("packet content: ", _dump(concat(req, ""))) + + local authpacket=_compose_packet(self,req,packet_len) + --print("mysql login authpacket len=",#authpacket) + return sockchannel:request(authpacket,_recv_auth_resp(self)) + end +end +function _M.connect( opts) + + local self = setmetatable( {}, mt) + + local max_packet_size = opts.max_packet_size + if not max_packet_size then + max_packet_size = 1024 * 1024 -- default 1 MB + end + self._max_packet_size = max_packet_size + self.compact = opts.compact_arrays + + + local database = opts.database or "" + local user = opts.user or "" + local password = opts.password or "" + + local channel = socketchannel.channel { + host = opts.host, + port = opts.port or 3306, + auth = _mysql_login(self,user,password,database ), + } + -- try connect first only once + channel:connect(true) + self.sockchannel = channel + + + return self +end + + + +function _M.close(self) + self.sockchannel:close() + setmetatable(self, nil) +end + + +function _M.server_ver(self) + return self._server_ver +end + +local function _compose_query(self, query) + + self.packet_no = -1 + + local cmd_packet = strchar(COM_QUERY) .. query + local packet_len = 1 + #query + + local querypacket = _compose_packet(self, cmd_packet, packet_len) + --print("compose query packet, len= ", #querypacket) + return querypacket +end + + + +local function read_result(self, sock) + --print("read_result") + local packet, typ, err = _recv_packet(self, sock) + if not packet then + --print("read result", err) + error( err ) + end + + if typ == "ERR" then + local errno, msg, sqlstate = _parse_err_packet(packet) + --print("read result ", msg, errno, sqlstate) + --return nil, msg, errno, sqlstate + error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + end + + if typ == 'OK' then + local res = _parse_ok_packet(packet) + if res and band(res.server_status, SERVER_MORE_RESULTS_EXISTS) ~= 0 then + --print("read result ", res, "again") + return res, "again" + end + --print("parse ok packet res=",res) + return res + end + + if typ ~= 'DATA' then + --print("read result", "packet type " ,typ , " not supported") + error( "packet type " .. typ .. " not supported" ) + end + + -- typ == 'DATA' + + --print("read the result set header packet") + + local field_count, extra = _parse_result_set_header_packet(packet) + + --print("field count: ", field_count) + + local cols = new_tab(field_count, 0) + for i = 1, field_count do + local col, err, errno, sqlstate = _recv_field_packet(self, sock) + if not col then + --return nil, err, errno, sqlstate + error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + end + + cols[i] = col + end + + local packet, typ, err = _recv_packet(self, sock) + if not packet then + error( err) + end + + if typ ~= 'EOF' then + error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) + end + + -- typ == 'EOF' + + local compact = self.compact + + local rows = new_tab( 4, 0) + local i = 0 + while true do + --print("reading a row") + + packet, typ, err = _recv_packet(self, sock) + if not packet then + error (err) + end + + if typ == 'EOF' then + local warning_count, status_flags = _parse_eof_packet(packet) + + --print("status flags: ", status_flags) + + if band(status_flags, SERVER_MORE_RESULTS_EXISTS) ~= 0 then + return rows, "again" + end + + break + end + + -- if typ ~= 'DATA' then + -- return nil, 'bad row packet type: ' .. typ + -- end + + -- typ == 'DATA' + + local row = _parse_row_data_packet(packet, cols, compact) + i = i + 1 + rows[i] = row + end + + return rows +end + +local function _query_resp(self) + return function(sock) + --return true ,read_result(self,sock) + local res, more = read_result(self,sock) + if more ~= "again" then + return true, res + end + local mulitresultset = {res} + mulitresultset.mulitresultset = true + local i =2 + while more =="again" do + res, more = read_result(self,sock) + if not res then + return true, mulitresultset + end + mulitresultset[i]=res + i=i+1 + end + return true, mulitresultset + end +end +function _M.query(self, query) + local querypacket = _compose_query(self, query) + local sockchannel = self.sockchannel + if not self.query_resp then + self.query_resp = _query_resp(self) + end + return sockchannel:request( querypacket, self.query_resp ) +end + + +function _M.set_compact_arrays(self, value) + self.compact = value +end + + +function _M.quote_sql_str( str) + return mysqlaux.quote_sql_str(str) +end + +return _M diff --git a/test/testmysql.lua b/test/testmysql.lua new file mode 100644 index 00000000..b5621404 --- /dev/null +++ b/test/testmysql.lua @@ -0,0 +1,32 @@ +local skynet = require "skynet" +local mysql = require "mysql" + +skynet.start(function() + + local db=mysql.connect{ + host="192.168.1.218", + port=3306, + database="Battle_Data", + user="root", + password="1" + } + if not db then + print("failed to connect") + end + print("testmysql success to connect to mysql server") + + --local res=db:query("select * from test1;select * from test1") + local res=db:query("select * from G_BuildData_0 limit 10") + print(res) + for k,v in pairs(res) do + print("k=",k,"v=",v) + if type(v)=="table" then + for kk, vv in pairs(v) do + print("kk=",kk,"vv=",v) + end + end + end + + skynet.exit() +end) + From 79b6b806765c3ee462d6c291c9381f95fd2e8cde Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 24 Jun 2014 18:19:04 +0800 Subject: [PATCH 039/729] bugfix: harbor socket package split --- service-src/service_harbor.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 8dbf7db4..3df45cd0 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -425,36 +425,40 @@ push_socket_data(struct harbor *h, const struct skynet_socket_message * message) } // go though } - case STATUS_HEADER: - if (size < 4) { - s->length = size; - memcpy(s->size, buffer, size); + case STATUS_HEADER: { + // big endian 4 bytes length, the first one must be 0. + int need = 4 - s->length; + if (size < need) { + s->length += size; + memcpy(s->size + s->length, buffer, size); return; } else { - // big endian 4 bytes length - if (buffer[0] != 0) { + memcpy(s->size + s->length, buffer, need); + buffer += need; + size -= need; + + if (s->size[0] != 0) { skynet_error(h->ctx, "Message is too long from harbor %d", id); close_harbor(h,id); return; } - s->length = buffer[1] << 16 | buffer[2] << 8 | buffer[3]; + s->length = s->size[1] << 16 | s->size[2] << 8 | s->size[3]; s->read = 0; s->recv_buffer = skynet_malloc(s->length); s->status = STATUS_CONTENT; - buffer += 4; - size -= 4; if (size == 0) { return; } } - // go though - case STATUS_CONTENT: - if (size < s->length - s->read) { + } + // go though + case STATUS_CONTENT: { + int need = s->length - s->read; + if (size < need) { memcpy(s->recv_buffer + s->read, buffer, size); s->read += size; return; } - int need = s->length - s->read; memcpy(s->recv_buffer + s->read, buffer, need); forward_local_messsage(h, s->recv_buffer, s->length); s->length = 0; @@ -466,6 +470,7 @@ push_socket_data(struct harbor *h, const struct skynet_socket_message * message) if (size == 0) return; break; + } default: return; } From 813e6c764d9a9c84e2c2152083baf3a0ef1988c8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 24 Jun 2014 18:30:20 +0800 Subject: [PATCH 040/729] add assert --- service/cslave.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/cslave.lua b/service/cslave.lua index 71625818..6f005b2f 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -36,7 +36,7 @@ end local function connect_slave(slave_id, address) local ok, err = pcall(function() if slaves[slave_id] == nil then - local fd = socket.open(address) + local fd = assert(socket.open(address), "Can't connect to "..address) skynet.error(string.format("Connect to harbor %d (fd=%d), %s", slave_id, fd, address)) slaves[slave_id] = fd monitor_clear(slave_id) From db952dc658d468b893db577c0d849d80f2fc9cc0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 24 Jun 2014 19:22:18 +0800 Subject: [PATCH 041/729] report too long message error, instead of assert --- skynet-src/skynet_server.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index b7b809ef..e2baa1ce 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -562,12 +562,19 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da *data = msg; } - assert((*sz & HANDLE_MASK) == *sz); *sz |= type << HANDLE_REMOTE_SHIFT; } int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) { + if ((sz & HANDLE_MASK) != sz) { + skynet_error(context, "The message to %x is too large (sz = %lu)", destination, sz); + if (session != 0) { + skynet_send(context, destination, source, PTYPE_ERROR, session, NULL, 0); + } + skynet_free(data); + return -1; + } _filter_args(context, type, &session, (void **)&data, &sz); if (source == 0) { From 3cadd297965a19545c6a786b086f1dc08f090831 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 24 Jun 2014 19:32:15 +0800 Subject: [PATCH 042/729] don't throw error when destination address is invalid --- lualib-src/lua-skynet.c | 2 +- skynet-src/skynet_server.c | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 931de581..0189b61b 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -149,7 +149,7 @@ _sendname(lua_State *L, struct skynet_context * context, const char * dest) { luaL_error(L, "skynet.send invalid param %s", lua_typename(L,lua_type(L,4))); } if (session < 0) { - luaL_error(L, "skynet.send session (%d) < 0", session); + return 0; } lua_pushinteger(L,session); return 1; diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index e2baa1ce..017d7206 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -569,9 +569,6 @@ int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) { if ((sz & HANDLE_MASK) != sz) { skynet_error(context, "The message to %x is too large (sz = %lu)", destination, sz); - if (session != 0) { - skynet_send(context, destination, source, PTYPE_ERROR, session, NULL, 0); - } skynet_free(data); return -1; } From f849c522bb12267adbf5736078c77f393d4b2913 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 24 Jun 2014 21:06:40 +0800 Subject: [PATCH 043/729] bugfix: silly bug --- service-src/service_harbor.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 3df45cd0..f7a88f2b 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -427,13 +427,13 @@ push_socket_data(struct harbor *h, const struct skynet_socket_message * message) } case STATUS_HEADER: { // big endian 4 bytes length, the first one must be 0. - int need = 4 - s->length; + int need = 4 - s->read; if (size < need) { - s->length += size; - memcpy(s->size + s->length, buffer, size); + memcpy(s->size + s->read, buffer, size); + s->read += size; return; } else { - memcpy(s->size + s->length, buffer, need); + memcpy(s->size + s->read, buffer, need); buffer += need; size -= need; From 1cbe6391867371a54183b2b3afbc358e2f8e9579 Mon Sep 17 00:00:00 2001 From: xjdrew Date: Wed, 25 Jun 2014 00:43:21 +0800 Subject: [PATCH 044/729] bugfix: time rewind --- skynet-src/skynet_timer.c | 51 +++++++++++++++++++++------------------ test/time.lua | 5 ++++ 2 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 test/time.lua diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 32b71f77..4ee0b70d 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -26,6 +26,8 @@ typedef void (*timer_execute_func)(void *ud,void *arg); #define TIME_NEAR_MASK (TIME_NEAR-1) #define TIME_LEVEL_MASK (TIME_LEVEL-1) +#define CENTISECOND_MASK 0x1ffffff + struct timer_event { uint32_t handle; int session; @@ -218,28 +220,41 @@ skynet_timeout(uint32_t handle, int time, int session) { return session; } -static uint32_t -_gettime(void) { - uint32_t t; +// centisecond: 1/100 second +static void +_systime(uint32_t *sec, uint32_t *cs) { #if !defined(__APPLE__) struct timespec ti; - clock_gettime(CLOCK_MONOTONIC, &ti); - t = (uint32_t)(ti.tv_sec & 0xffffff) * 100; - t += ti.tv_nsec / 10000000; + clock_gettime(CLOCK_REALTIME, &ti); + *sec = (uint32_t)ti.tv_sec; + *cs = (uint32_t)(ti.tv_nsec / 10000000); #else struct timeval tv; gettimeofday(&tv, NULL); - t = (uint32_t)(tv.tv_sec & 0xffffff) * 100; - t += tv.tv_usec / 10000; + *sec = tv.tv_sec; + *cs = tv.tv_usec / 10000; #endif - return t; +} + +static uint32_t +_gettime() { + uint32_t sec, cs; + _systime(&sec, &cs); + // if sec < TI->starttime, need to update TI->starttime? + // if(sec < TI->starttime) { + // skynet_error(NULL, "time diff error, change from %u -> %u", TI->starttime, sec); + // TI->starttime = sec; + // TI->current = cs; + // } + uint32_t diff = (sec - TI->starttime) & CENTISECOND_MASK; + return diff * 100 + cs; } void skynet_updatetime(void) { uint32_t ct = _gettime(); if (ct != TI->current) { - int diff = ct>=TI->current?ct-TI->current:(0xffffff+1)*100-TI->current+ct; + uint32_t diff = ct>=TI->current?ct-TI->current:(uint32_t)(CENTISECOND_MASK+1)*100-TI->current+ct; TI->current = ct; int i; for (i=0;icurrent = _gettime(); - -#if !defined(__APPLE__) - struct timespec ti; - clock_gettime(CLOCK_REALTIME, &ti); - uint32_t sec = (uint32_t)ti.tv_sec; -#else - struct timeval tv; - gettimeofday(&tv, NULL); - uint32_t sec = (uint32_t)tv.tv_sec; -#endif - uint32_t mono = _gettime() / 100; - - TI->starttime = sec - mono; + _systime(&TI->starttime, &TI->current); } + diff --git a/test/time.lua b/test/time.lua new file mode 100644 index 00000000..90784211 --- /dev/null +++ b/test/time.lua @@ -0,0 +1,5 @@ +local skynet = require "skynet" +skynet.start(function() + print(skynet.starttime()) + print(skynet.now()) +end) From e0d8b002260419279feea5775d44f3ad5047cf8d Mon Sep 17 00:00:00 2001 From: xjdrew Date: Wed, 25 Jun 2014 10:43:01 +0800 Subject: [PATCH 045/729] =?UTF-8?q?bugfix:=20ntp=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E6=97=B6=E6=8A=8A=E6=97=B6=E9=97=B4=E5=BE=80=E5=90=8E=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skynet-src/skynet_timer.c | 45 ++++++++++++++++++++++++--------------- test/time.lua | 17 +++++++++++++++ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 4ee0b70d..43eb97e7 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -26,8 +26,6 @@ typedef void (*timer_execute_func)(void *ud,void *arg); #define TIME_NEAR_MASK (TIME_NEAR-1) #define TIME_LEVEL_MASK (TIME_LEVEL-1) -#define CENTISECOND_MASK 0x1ffffff - struct timer_event { uint32_t handle; int session; @@ -50,6 +48,8 @@ struct timer { int time; uint32_t current; uint32_t starttime; + uint64_t current_point; + uint64_t origin_point; }; static struct timer * TI = NULL; @@ -236,26 +236,34 @@ _systime(uint32_t *sec, uint32_t *cs) { #endif } -static uint32_t +static uint64_t _gettime() { - uint32_t sec, cs; - _systime(&sec, &cs); - // if sec < TI->starttime, need to update TI->starttime? - // if(sec < TI->starttime) { - // skynet_error(NULL, "time diff error, change from %u -> %u", TI->starttime, sec); - // TI->starttime = sec; - // TI->current = cs; - // } - uint32_t diff = (sec - TI->starttime) & CENTISECOND_MASK; - return diff * 100 + cs; + uint64_t t; +#if !defined(__APPLE__) + struct timespec ti; + clock_gettime(CLOCK_MONOTONIC, &ti); + t = ti.tv_sec * 100; + t += (uint32_t)(ti.tv_nsec / 10000000); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + t = ti.tv_sec * 100; + t += (uint32_t)(tv.tv_usec / 10000); +#endif + return t; } void skynet_updatetime(void) { - uint32_t ct = _gettime(); - if (ct != TI->current) { - uint32_t diff = ct>=TI->current?ct-TI->current:(uint32_t)(CENTISECOND_MASK+1)*100-TI->current+ct; - TI->current = ct; + uint64_t cp = _gettime(); + if(cp < TI->current_point) { + skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point); + } else if (cp != TI->current_point) { + uint32_t diff = (uint32_t)(cp - TI->current_point); + TI->current_point = cp; + + // when cs > 0xffffffff(about 49710), time rewind + TI->current += diff; int i; for (i=0;istarttime, &TI->current); + uint64_t point = _gettime(); + TI->current_point = point; + TI->origin_point = point; } diff --git a/test/time.lua b/test/time.lua index 90784211..be02805c 100644 --- a/test/time.lua +++ b/test/time.lua @@ -2,4 +2,21 @@ local skynet = require "skynet" skynet.start(function() print(skynet.starttime()) print(skynet.now()) + + skynet.timeout(1, function() + print("in 1", skynet.now()) + end) + skynet.timeout(2, function() + print("in 2", skynet.now()) + end) + skynet.timeout(3, function() + print("in 3", skynet.now()) + end) + + skynet.timeout(4, function() + print("in 4", skynet.now()) + end) + skynet.timeout(100, function() + print("in 100", skynet.now()) + end) end) From 36cab8e06071f498f28ee04a6ae70ac6da51d7b5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 25 Jun 2014 11:15:28 +0800 Subject: [PATCH 046/729] add skynet.time() --- HISTORY.md | 1 + lualib/skynet.lua | 4 ++++ skynet-src/skynet_timer.c | 33 ++++++++++++++++++++++----------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index b5641bde..d4066410 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,6 +7,7 @@ Dev version * socket.listen support put port into address. (address:port) * Redesign harbor/master/dummy, remove lots of C code and rewite in lua. * Remove block connect api, queue sending message during connecting now. +* Add skynet.time() v0.3.2 (2014-6-23) ---------- diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 385a6d38..68800d33 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -247,6 +247,10 @@ function skynet.starttime() return tonumber(c.command("STARTTIME")) end +function skynet.time() + return skynet.starttime() + skynet.now()/100 +end + function skynet.exit() skynet.send(".launcher","lua","REMOVE",skynet.self()) c.command("EXIT") diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 43eb97e7..73f1ab7f 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -222,7 +222,7 @@ skynet_timeout(uint32_t handle, int time, int session) { // centisecond: 1/100 second static void -_systime(uint32_t *sec, uint32_t *cs) { +systime(uint32_t *sec, uint32_t *cs) { #if !defined(__APPLE__) struct timespec ti; clock_gettime(CLOCK_REALTIME, &ti); @@ -237,33 +237,44 @@ _systime(uint32_t *sec, uint32_t *cs) { } static uint64_t -_gettime() { +gettime() { uint64_t t; #if !defined(__APPLE__) + +#ifdef CLOCK_MONOTONIC_RAW +#define CLOCK_TIMER CLOCK_MONOTONIC_RAW +#else +#define CLOCK_TIMER CLOCK_MONOTONIC +#endif + struct timespec ti; - clock_gettime(CLOCK_MONOTONIC, &ti); - t = ti.tv_sec * 100; - t += (uint32_t)(ti.tv_nsec / 10000000); + clock_gettime(CLOCK_TIMER, &ti); + t = (uint64_t)ti.tv_sec * 100; + t += ti.tv_nsec / 10000000; #else struct timeval tv; gettimeofday(&tv, NULL); - t = ti.tv_sec * 100; - t += (uint32_t)(tv.tv_usec / 10000); + t = (uint64_t)ti.tv_sec * 100; + t += tv.tv_usec / 10000; #endif return t; } void skynet_updatetime(void) { - uint64_t cp = _gettime(); + uint64_t cp = gettime(); if(cp < TI->current_point) { skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point); } else if (cp != TI->current_point) { uint32_t diff = (uint32_t)(cp - TI->current_point); TI->current_point = cp; - // when cs > 0xffffffff(about 49710), time rewind + uint32_t oc = TI->current; TI->current += diff; + if (TI->current < oc) { + // when cs > 0xffffffff(about 497 days), time rewind + TI->starttime += 0xffffffff / 100; + } int i; for (i=0;istarttime, &TI->current); - uint64_t point = _gettime(); + systime(&TI->starttime, &TI->current); + uint64_t point = gettime(); TI->current_point = point; TI->origin_point = point; } From be1db23ea310dab108940857007572ce1c27daef Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 25 Jun 2014 11:59:07 +0800 Subject: [PATCH 047/729] get now before starttime would be better --- examples/main.lua | 2 ++ lualib/skynet.lua | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/main.lua b/examples/main.lua index 8019c2bf..fa1355e2 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -13,5 +13,7 @@ skynet.start(function() maxclient = max_client, }) + print(skynet.time()) + skynet.exit() end) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 68800d33..2019f530 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -248,7 +248,7 @@ function skynet.starttime() end function skynet.time() - return skynet.starttime() + skynet.now()/100 + return skynet.now()/100 + skynet.starttime() -- get now first would be better end function skynet.exit() From bf686da72304f2945e188a49c908da8c1188a902 Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Wed, 25 Jun 2014 20:55:20 +0800 Subject: [PATCH 048/729] improve mysql lib test --- test/testmysql.lua | 69 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/test/testmysql.lua b/test/testmysql.lua index b5621404..8944d15c 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -1,12 +1,56 @@ local skynet = require "skynet" local mysql = require "mysql" +local function dump(obj) + local getIndent, quoteStr, wrapKey, wrapVal, dumpObj + getIndent = function(level) + return string.rep("\t", level) + end + quoteStr = function(str) + return '"' .. string.gsub(str, '"', '\\"') .. '"' + end + wrapKey = function(val) + if type(val) == "number" then + return "[" .. val .. "]" + elseif type(val) == "string" then + return "[" .. quoteStr(val) .. "]" + else + return "[" .. tostring(val) .. "]" + end + end + wrapVal = function(val, level) + if type(val) == "table" then + return dumpObj(val, level) + elseif type(val) == "number" then + return val + elseif type(val) == "string" then + return quoteStr(val) + else + return tostring(val) + end + end + dumpObj = function(obj, level) + if type(obj) ~= "table" then + return wrapVal(obj) + end + level = level + 1 + local tokens = {} + tokens[#tokens + 1] = "{" + for k, v in pairs(obj) do + tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. "," + end + tokens[#tokens + 1] = getIndent(level - 1) .. "}" + return table.concat(tokens, "\n") + end + return dumpObj(obj, 0) +end + skynet.start(function() local db=mysql.connect{ - host="192.168.1.218", + host="127.0.0.1", port=3306, - database="Battle_Data", + database="skynet", user="root", password="1" } @@ -15,17 +59,16 @@ skynet.start(function() end print("testmysql success to connect to mysql server") - --local res=db:query("select * from test1;select * from test1") - local res=db:query("select * from G_BuildData_0 limit 10") - print(res) - for k,v in pairs(res) do - print("k=",k,"v=",v) - if type(v)=="table" then - for kk, vv in pairs(v) do - print("kk=",kk,"vv=",v) - end - end - end + local res = db:query("drop table if exists cats") + res = db:query("create table cats " + .."(id serial primary key, ".. "name varchar(5))") + print( dump( res ) ) + res = db:query("insert into cats (name) " + .. "values (\'Bob\'),(\'\'),(null)") + print ( dump( res ) ) + res = db:query("select * from cats order by id asc") + print ( dump( res ) ) + skynet.exit() end) From 315945b2bd374356e174857a884cc244f104ccc3 Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Wed, 25 Jun 2014 22:15:06 +0800 Subject: [PATCH 049/729] no message --- test/testmysql.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/testmysql.lua b/test/testmysql.lua index 8944d15c..fe4bcb7e 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -69,7 +69,15 @@ skynet.start(function() res = db:query("select * from cats order by id asc") print ( dump( res ) ) - + -- multiresultset test + res = db:query("select * from cats order by id asc ; select * from cats") + print ( dump( res ) ) + + print ( mysql.quote_sql_str([[\mysql escape %string test'test"]]) ) + + -- bad sql statement + res = pcall( db.query, db, "select * from notexisttable" ) + print( dump(res) ) skynet.exit() end) From 984f727385d92236baf9f4d2e7bb8b25f93e5410 Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Wed, 25 Jun 2014 22:38:38 +0800 Subject: [PATCH 050/729] imporve mysql lib test --- test/testmysql.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/testmysql.lua b/test/testmysql.lua index fe4bcb7e..0debe1f9 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -63,9 +63,11 @@ skynet.start(function() res = db:query("create table cats " .."(id serial primary key, ".. "name varchar(5))") print( dump( res ) ) + res = db:query("insert into cats (name) " .. "values (\'Bob\'),(\'\'),(null)") print ( dump( res ) ) + res = db:query("select * from cats order by id asc") print ( dump( res ) ) @@ -76,8 +78,12 @@ skynet.start(function() print ( mysql.quote_sql_str([[\mysql escape %string test'test"]]) ) -- bad sql statement - res = pcall( db.query, db, "select * from notexisttable" ) + local ok, res = pcall( db.query, db, "select * from notexisttable" ) print( dump(res) ) + + res = db:query("select * from cats order by id asc") + print ( dump( res ) ) + skynet.exit() end) From c0e1365dc25fb69f5e944e0d225dad923ee5528f Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Wed, 25 Jun 2014 23:11:00 +0800 Subject: [PATCH 051/729] modify error process in socket channel --- lualib/socketchannel.lua | 5 ++++- test/testmysql.lua | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 32615ac0..c2170cbc 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -132,9 +132,12 @@ local function dispatch_by_order(self) else close_channel_socket(self) local errmsg - if result ~= socket_error then + if result_ok ~= socket_error then errmsg = result_ok end + self.__result[co] = socket_error + self.__result_data[co] = errmsg + skynet.wakeup(co) wakeup_all(self, errmsg) end end diff --git a/test/testmysql.lua b/test/testmysql.lua index 0debe1f9..05819273 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -79,11 +79,11 @@ skynet.start(function() -- bad sql statement local ok, res = pcall( db.query, db, "select * from notexisttable" ) - print( dump(res) ) + print( "ok= ",ok, dump(res) ) res = db:query("select * from cats order by id asc") print ( dump( res ) ) - + skynet.exit() end) From ad5c37200b09d5332025f71f146dc17fb2b7efbb Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Thu, 26 Jun 2014 00:25:10 +0800 Subject: [PATCH 052/729] improve mysql lib --- lualib/mysql.lua | 128 ++++++++++++++++++++++++++------------------- test/testmysql.lua | 5 +- 2 files changed, 76 insertions(+), 57 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index 137a12fa..1d4d54a5 100755 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -605,46 +605,7 @@ local function _mysql_login(self,user,password,database) return sockchannel:request(authpacket,_recv_auth_resp(self)) end end -function _M.connect( opts) - local self = setmetatable( {}, mt) - - local max_packet_size = opts.max_packet_size - if not max_packet_size then - max_packet_size = 1024 * 1024 -- default 1 MB - end - self._max_packet_size = max_packet_size - self.compact = opts.compact_arrays - - - local database = opts.database or "" - local user = opts.user or "" - local password = opts.password or "" - - local channel = socketchannel.channel { - host = opts.host, - port = opts.port or 3306, - auth = _mysql_login(self,user,password,database ), - } - -- try connect first only once - channel:connect(true) - self.sockchannel = channel - - - return self -end - - - -function _M.close(self) - self.sockchannel:close() - setmetatable(self, nil) -end - - -function _M.server_ver(self) - return self._server_ver -end local function _compose_query(self, query) @@ -665,14 +626,15 @@ local function read_result(self, sock) local packet, typ, err = _recv_packet(self, sock) if not packet then --print("read result", err) - error( err ) + return nil, err + --error( err ) end if typ == "ERR" then local errno, msg, sqlstate = _parse_err_packet(packet) --print("read result ", msg, errno, sqlstate) - --return nil, msg, errno, sqlstate - error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + return nil, msg, errno, sqlstate + --error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end if typ == 'OK' then @@ -687,7 +649,8 @@ local function read_result(self, sock) if typ ~= 'DATA' then --print("read result", "packet type " ,typ , " not supported") - error( "packet type " .. typ .. " not supported" ) + return nil, "packet type " .. typ .. " not supported" + --error( "packet type " .. typ .. " not supported" ) end -- typ == 'DATA' @@ -702,8 +665,8 @@ local function read_result(self, sock) for i = 1, field_count do local col, err, errno, sqlstate = _recv_field_packet(self, sock) if not col then - --return nil, err, errno, sqlstate - error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + return nil, err, errno, sqlstate + --error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end cols[i] = col @@ -711,11 +674,13 @@ local function read_result(self, sock) local packet, typ, err = _recv_packet(self, sock) if not packet then - error( err) + --error( err) + return nil, err end if typ ~= 'EOF' then - error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) + --error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) + return nil, "unexpected packet type " .. typ .. " while eof packet is ".. "expected" end -- typ == 'EOF' @@ -729,7 +694,8 @@ local function read_result(self, sock) packet, typ, err = _recv_packet(self, sock) if not packet then - error (err) + --error (err) + return nil, err end if typ == 'EOF' then @@ -761,15 +727,25 @@ end local function _query_resp(self) return function(sock) --return true ,read_result(self,sock) - local res, more = read_result(self,sock) - if more ~= "again" then + --local res, more = read_result(self,sock) + local res, err, errno, sqlstate = read_result(self,sock) + if not res then + local badresult ={} + badresult.badresult = true + badresult.err = err + badresult.errno = errno + badresult.sqlstate = sqlstate + return true , badresult + end + if err ~= "again" then return true, res end local mulitresultset = {res} mulitresultset.mulitresultset = true local i =2 - while more =="again" do - res, more = read_result(self,sock) + while err =="again" do + --res, more = read_result(self,sock) + res, err, errno, sqlstate = read_result(self,sock) if not res then return true, mulitresultset end @@ -779,6 +755,44 @@ local function _query_resp(self) return true, mulitresultset end end + +function _M.connect( opts) + + local self = setmetatable( {}, mt) + + local max_packet_size = opts.max_packet_size + if not max_packet_size then + max_packet_size = 1024 * 1024 -- default 1 MB + end + self._max_packet_size = max_packet_size + self.compact = opts.compact_arrays + + + local database = opts.database or "" + local user = opts.user or "" + local password = opts.password or "" + + local channel = socketchannel.channel { + host = opts.host, + port = opts.port or 3306, + auth = _mysql_login(self,user,password,database ), + } + -- try connect first only once + channel:connect(true) + self.sockchannel = channel + + + return self +end + + + +function _M.disconnect(self) + self.sockchannel:close() + setmetatable(self, nil) +end + + function _M.query(self, query) local querypacket = _compose_query(self, query) local sockchannel = self.sockchannel @@ -788,9 +802,8 @@ function _M.query(self, query) return sockchannel:request( querypacket, self.query_resp ) end - -function _M.set_compact_arrays(self, value) - self.compact = value +function _M.server_ver(self) + return self._server_ver end @@ -798,4 +811,9 @@ function _M.quote_sql_str( str) return mysqlaux.quote_sql_str(str) end +function _M.set_compact_arrays(self, value) + self.compact = value +end + + return _M diff --git a/test/testmysql.lua b/test/testmysql.lua index 05819273..99202109 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -78,12 +78,13 @@ skynet.start(function() print ( mysql.quote_sql_str([[\mysql escape %string test'test"]]) ) -- bad sql statement - local ok, res = pcall( db.query, db, "select * from notexisttable" ) - print( "ok= ",ok, dump(res) ) + local res = db:query("select * from notexisttable" ) + print( dump(res) ) res = db:query("select * from cats order by id asc") print ( dump( res ) ) + db:disconnect() skynet.exit() end) From 4cce476dfc58d624a609d1b59c99901250fd72c8 Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Thu, 26 Jun 2014 00:28:08 +0800 Subject: [PATCH 053/729] no message --- test/testmysql.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/testmysql.lua b/test/testmysql.lua index 99202109..87ffea67 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -52,7 +52,8 @@ skynet.start(function() port=3306, database="skynet", user="root", - password="1" + password="1", + max_packet_size = 1024 * 1024 } if not db then print("failed to connect") From fce05f0cfc3d2b8b1e742d4f5fdd99b2afdf8bee Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Thu, 26 Jun 2014 01:04:11 +0800 Subject: [PATCH 054/729] no message --- lualib/mysql.lua | 2 ++ test/testmysql.lua | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index 1d4d54a5..adaf08e6 100755 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -481,12 +481,14 @@ local function _recv_auth_resp(self) if not packet then --print("recv auth resp : failed to receive the result packet") error ("failed to receive the result packet"..err) + --return nil,err end --print("receive auth response packet type: ",typ) if typ == 'ERR' then local errno, msg, sqlstate = _parse_err_packet(packet) error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) + --return nil, errno,msg, sqlstate end if typ == 'EOF' then diff --git a/test/testmysql.lua b/test/testmysql.lua index 87ffea67..1423adbd 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -45,6 +45,24 @@ local function dump(obj) return dumpObj(obj, 0) end +local function test2( db) + local i=1 + while true do + local res = db:query("select * from cats order by id asc") + print ( "test2 i=" ,i,"\n",dump( res ) ) + skynet.sleep(1000) + i=i+1 + end +end +local function test3( db) + local i=1 + while true do + local res = db:query("select * from cats order by id asc") + print ( "test3 i=" ,i,"\n",dump( res ) ) + skynet.sleep(1000) + i=i+1 + end +end skynet.start(function() local db=mysql.connect{ @@ -71,7 +89,10 @@ skynet.start(function() res = db:query("select * from cats order by id asc") print ( dump( res ) ) - + + -- test in another coroutine + skynet.fork( test2, db) + skynet.fork( test3, db) -- multiresultset test res = db:query("select * from cats order by id asc ; select * from cats") print ( dump( res ) ) @@ -82,10 +103,15 @@ skynet.start(function() local res = db:query("select * from notexisttable" ) print( dump(res) ) - res = db:query("select * from cats order by id asc") - print ( dump( res ) ) + local i=1 + while true do + local res = db:query("select * from cats order by id asc") + print ( "test1 i=" ,i,"\n",dump( res ) ) + skynet.sleep(1000) + i=i+1 + end - db:disconnect() - skynet.exit() + --db:disconnect() + --skynet.exit() end) From e9d4075e435c5564fd88c9d8f21d87380035f02a Mon Sep 17 00:00:00 2001 From: changfeng <87414772@qq.com> Date: Thu, 26 Jun 2014 07:34:16 +0800 Subject: [PATCH 055/729] remove commented out code, improve test code --- lualib/mysql.lua | 81 ++-------------------------------------------- test/testmysql.lua | 22 +++++++++---- 2 files changed, 18 insertions(+), 85 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index adaf08e6..0c5bdbbb 100755 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -157,14 +157,11 @@ local function _compute_token(password, scramble) return "" end --_dump(scramble) - --print("password=",password) - --print("password:", password, "scramble: ", _dumphex(scramble) ) + local stage1 = sha1(password) --print("stage1:", _dumphex(stage1) ) local stage2 = sha1(stage1) - --print("stage2:", _dumphex(stage2) ) local stage3 = sha1(scramble .. stage2) - --print("stage3:", _dumphex(stage3) ) local n = #stage1 local bytes = new_tab(n, 0) for i = 1, n do @@ -187,13 +184,9 @@ local function _send_packet(self, req, size) self.packet_no = self.packet_no + 1 - --print("packet no: ", self.packet_no) local packet = _set_byte3(size) .. strchar(self.packet_no) .. req - --print("sending packet...") - - --return sock:send(packet) return socket.write(self.sock,packet) end @@ -205,12 +198,10 @@ local function _recv_packet(self,sock) if not data then return nil, nil, "failed to receive packet header: " end - --print("_recv_packet data type:" ,type(data) ) - --print("packet header: ", _dump(data)) + local len, pos = _get_byte3(data, 1) - --print("recv_packet packet length: ", len) if len == 0 then return nil, nil, "empty packet" @@ -222,24 +213,16 @@ local function _recv_packet(self,sock) local num = strbyte(data, pos) - --print("recv packet: packet no: ", num) - self.packet_no = num - --data, err = sock:receive(len) - data = sock:read(len) - if not data then return nil, nil, "failed to read packet content: " end - --print("packet content: ", _dump(data)) - --print("packet content (ascii): ", data) local field_count = strbyte(data, 1) - --print("field count:",field_count) local typ if field_count == 0x00 then typ = "OK" @@ -251,7 +234,6 @@ local function _recv_packet(self,sock) typ = "DATA" end - --print("recv packet: typ= ", typ) return data, typ end @@ -259,8 +241,6 @@ end local function _from_length_coded_bin(data, pos) local first = strbyte(data, pos) - --print("LCB: first: ", first) - if not first then return nil, pos end @@ -309,26 +289,18 @@ local function _parse_ok_packet(packet) res.affected_rows, pos = _from_length_coded_bin(packet, 2) - --print("affected rows: ", res.affected_rows, ", pos:", pos) - res.insert_id, pos = _from_length_coded_bin(packet, pos) - --print("insert id: ", res.insert_id, ", pos:", pos) - res.server_status, pos = _get_byte2(packet, pos) - --print("server status: ", res.server_status, ", pos:", pos) - res.warning_count, pos = _get_byte2(packet, pos) - --print("warning count: ", res.warning_count, ", pos: ", pos) local message = sub(packet, pos) if message and message ~= "" then res.message = message end - --print("message: ", res.message, ", pos:", pos) return res end @@ -376,7 +348,6 @@ local function _parse_field_packet(data) local pos catalog, pos = _from_length_coded_str(data, 1) - --print("catalog: ", col.catalog, ", pos:", pos) db, pos = _from_length_coded_str(data, pos) table, pos = _from_length_coded_str(data, pos) @@ -427,8 +398,6 @@ local function _parse_row_data_packet(data, cols, compact) local typ = col.type local name = col.name - --print("row field value: ", value, ", type: ", typ) - if value ~= null then local conv = converters[typ] if conv then @@ -476,7 +445,6 @@ end local function _recv_auth_resp(self) return function(sock) - --print("recv auth resp") local packet, typ, err = _recv_packet(self,sock) if not packet then --print("recv auth resp : failed to receive the result packet") @@ -484,7 +452,6 @@ local function _recv_auth_resp(self) --return nil,err end - --print("receive auth response packet type: ",typ) if typ == 'ERR' then local errno, msg, sqlstate = _parse_err_packet(packet) error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) @@ -519,24 +486,17 @@ local function _mysql_login(self,user,password,database) self.protocol_ver = strbyte(packet) - --print("protocol version: ", self.protocol_ver) - local server_ver, pos = _from_cstring(packet, 2) if not server_ver then error "bad handshake initialization packet: bad server version" end - --print("server version: ", server_ver) - self._server_ver = server_ver local thread_id, pos = _get_byte4(packet, pos) - --print("thread id: ", thread_id) - local scramble1 = sub(packet, pos, pos + 8 - 1) - --print("scramble1:",_dump(scramble1), "pos:",pos) if not scramble1 then error "1st part of scramble not found" end @@ -546,31 +506,20 @@ local function _mysql_login(self,user,password,database) -- two lower bytes self._server_capabilities, pos = _get_byte2(packet, pos) - --print("server capabilities: ", self._server_capabilities) - self._server_lang = strbyte(packet, pos) pos = pos + 1 - --print("server lang: ", self._server_lang) - self._server_status, pos = _get_byte2(packet, pos) - --print("server status: ", self._server_status) - local more_capabilities more_capabilities, pos = _get_byte2(packet, pos) self._server_capabilities = bor(self._server_capabilities, lshift(more_capabilities, 16)) - --print("server capabilities: ", self._server_capabilities) - - -- local len = strbyte(packet, pos) local len = 21 - 8 - 1 - --print("scramble len: ", len) - pos = pos + 1 + 10 local scramble_part2 = sub(packet, pos, pos + len - 1) @@ -580,14 +529,10 @@ local function _mysql_login(self,user,password,database) local scramble = scramble1..scramble_part2 - --print("scramble:",_dump(scramble) ) local token = _compute_token(password, scramble) - -- local client_flags = self._server_capabilities local client_flags = 260047; - --print("token: ", _dump(token)) - local req = _set_byte4(client_flags) .. _set_byte4(self._max_packet_size) .. "\0" -- TODO: add support for charset encoding @@ -599,11 +544,7 @@ local function _mysql_login(self,user,password,database) local packet_len = 4 + 4 + 1 + 23 + #user + 1 + #token + 1 + #database + 1 - --print("packet content length: ", packet_len) - --print("packet content: ", _dump(concat(req, ""))) - local authpacket=_compose_packet(self,req,packet_len) - --print("mysql login authpacket len=",#authpacket) return sockchannel:request(authpacket,_recv_auth_resp(self)) end end @@ -617,24 +558,20 @@ local function _compose_query(self, query) local packet_len = 1 + #query local querypacket = _compose_packet(self, cmd_packet, packet_len) - --print("compose query packet, len= ", #querypacket) return querypacket end local function read_result(self, sock) - --print("read_result") local packet, typ, err = _recv_packet(self, sock) if not packet then - --print("read result", err) return nil, err --error( err ) end if typ == "ERR" then local errno, msg, sqlstate = _parse_err_packet(packet) - --print("read result ", msg, errno, sqlstate) return nil, msg, errno, sqlstate --error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end @@ -642,27 +579,20 @@ local function read_result(self, sock) if typ == 'OK' then local res = _parse_ok_packet(packet) if res and band(res.server_status, SERVER_MORE_RESULTS_EXISTS) ~= 0 then - --print("read result ", res, "again") return res, "again" end - --print("parse ok packet res=",res) return res end if typ ~= 'DATA' then - --print("read result", "packet type " ,typ , " not supported") return nil, "packet type " .. typ .. " not supported" --error( "packet type " .. typ .. " not supported" ) end -- typ == 'DATA' - --print("read the result set header packet") - local field_count, extra = _parse_result_set_header_packet(packet) - --print("field count: ", field_count) - local cols = new_tab(field_count, 0) for i = 1, field_count do local col, err, errno, sqlstate = _recv_field_packet(self, sock) @@ -692,8 +622,6 @@ local function read_result(self, sock) local rows = new_tab( 4, 0) local i = 0 while true do - --print("reading a row") - packet, typ, err = _recv_packet(self, sock) if not packet then --error (err) @@ -703,8 +631,6 @@ local function read_result(self, sock) if typ == 'EOF' then local warning_count, status_flags = _parse_eof_packet(packet) - --print("status flags: ", status_flags) - if band(status_flags, SERVER_MORE_RESULTS_EXISTS) ~= 0 then return rows, "again" end @@ -728,8 +654,6 @@ end local function _query_resp(self) return function(sock) - --return true ,read_result(self,sock) - --local res, more = read_result(self,sock) local res, err, errno, sqlstate = read_result(self,sock) if not res then local badresult ={} @@ -746,7 +670,6 @@ local function _query_resp(self) mulitresultset.mulitresultset = true local i =2 while err =="again" do - --res, more = read_result(self,sock) res, err, errno, sqlstate = read_result(self,sock) if not res then return true, mulitresultset diff --git a/test/testmysql.lua b/test/testmysql.lua index 1423adbd..510625e0 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -49,7 +49,10 @@ local function test2( db) local i=1 while true do local res = db:query("select * from cats order by id asc") - print ( "test2 i=" ,i,"\n",dump( res ) ) + print ( "test2 loop times=" ,i,"\n","query result=",dump( res ) ) + res = db:query("select * from cats order by id asc") + print ( "test2 loop times=" ,i,"\n","query result=",dump( res ) ) + skynet.sleep(1000) i=i+1 end @@ -58,7 +61,9 @@ local function test3( db) local i=1 while true do local res = db:query("select * from cats order by id asc") - print ( "test3 i=" ,i,"\n",dump( res ) ) + print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) + res = db:query("select * from cats order by id asc") + print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) skynet.sleep(1000) i=i+1 end @@ -95,18 +100,23 @@ skynet.start(function() skynet.fork( test3, db) -- multiresultset test res = db:query("select * from cats order by id asc ; select * from cats") - print ( dump( res ) ) + print ("multiresultset test result=", dump( res ) ) - print ( mysql.quote_sql_str([[\mysql escape %string test'test"]]) ) + print ("escape string test result=", mysql.quote_sql_str([[\mysql escape %string test'test"]]) ) -- bad sql statement local res = db:query("select * from notexisttable" ) - print( dump(res) ) + print( "bad query test result=" ,dump(res) ) local i=1 while true do local res = db:query("select * from cats order by id asc") - print ( "test1 i=" ,i,"\n",dump( res ) ) + print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) + + res = db:query("select * from cats order by id asc") + print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) + + skynet.sleep(1000) i=i+1 end From ceeb9912bf229adbd7bb7317815cde52dac3651e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 26 Jun 2014 14:04:57 +0800 Subject: [PATCH 056/729] typo --- examples/main.lua | 2 -- skynet-src/skynet_timer.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index fa1355e2..8019c2bf 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -13,7 +13,5 @@ skynet.start(function() maxclient = max_client, }) - print(skynet.time()) - skynet.exit() end) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 73f1ab7f..3cd0c51d 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -254,7 +254,7 @@ gettime() { #else struct timeval tv; gettimeofday(&tv, NULL); - t = (uint64_t)ti.tv_sec * 100; + t = (uint64_t)tv.tv_sec * 100; t += tv.tv_usec / 10000; #endif return t; From d343f3b374507ae5941a4ebad3e1188cba7dbb92 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 26 Jun 2014 15:06:47 +0800 Subject: [PATCH 057/729] use atomic operation --- skynet-src/skynet_start.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index c101da30..fc34a077 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -127,11 +127,11 @@ _worker(void *p) { if (q == NULL) { CHECK_ABORT if (pthread_mutex_lock(&m->mutex) == 0) { - ++ m->sleep; + __sync_fetch_and_add(&m->sleep, 1); // "spurious wakeup" is harmless, // because skynet_context_message_dispatch() can be call at any time. pthread_cond_wait(&m->cond, &m->mutex); - -- m->sleep; + __sync_fetch_and_sub(&m->sleep, 1); if (pthread_mutex_unlock(&m->mutex)) { fprintf(stderr, "unlock mutex error"); exit(1); From 2b9efcec3f570e069dd6cc4928bfd5c4ff5a1f12 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 26 Jun 2014 15:12:16 +0800 Subject: [PATCH 058/729] revert --- skynet-src/skynet_start.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index fc34a077..c101da30 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -127,11 +127,11 @@ _worker(void *p) { if (q == NULL) { CHECK_ABORT if (pthread_mutex_lock(&m->mutex) == 0) { - __sync_fetch_and_add(&m->sleep, 1); + ++ m->sleep; // "spurious wakeup" is harmless, // because skynet_context_message_dispatch() can be call at any time. pthread_cond_wait(&m->cond, &m->mutex); - __sync_fetch_and_sub(&m->sleep, 1); + -- m->sleep; if (pthread_mutex_unlock(&m->mutex)) { fprintf(stderr, "unlock mutex error"); exit(1); From f4d865a882f0c73f20483d5bb53801c02c8181a0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 30 Jun 2014 10:07:55 +0800 Subject: [PATCH 059/729] release v0.4.0 --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index d4066410..3f4d5d9d 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -Dev version +v0.4.0 (2014-6-30) ----------- * Optimize redis driver `compose_message`. * Add module skynet.harbor for monitor harbor connect/disconnect, see test/testharborlink.lua . From 1e0189962be9d40afae7299d880b2f7247183217 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 30 Jun 2014 11:31:24 +0800 Subject: [PATCH 060/729] bugfix: dead lock when service_harbor exit --- service-src/service_harbor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index f7a88f2b..36b2789c 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -262,7 +262,8 @@ harbor_release(struct harbor *h) { struct slave *s = &h->s[i]; if (s->fd && s->status != STATUS_DOWN) { close_harbor(h,i); - report_harbor_down(h,i); + // don't call report_harbor_down. + // never call skynet_send during module exit, because of dead lock } } hash_delete(h->map); From 711c04e6a94ce599fbb4afca55010c9621e694a9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 3 Jul 2014 17:13:47 +0800 Subject: [PATCH 061/729] bugfix: redirect should pass session (0) --- examples/simplemonitor.lua | 2 +- lualib/skynet.lua | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/simplemonitor.lua b/examples/simplemonitor.lua index 1528bb50..ec294d21 100644 --- a/examples/simplemonitor.lua +++ b/examples/simplemonitor.lua @@ -12,7 +12,7 @@ skynet.register_protocol { local w = service_map[address] if w then for watcher in pairs(w) do - skynet.redirect(watcher, address, "error", "") + skynet.redirect(watcher, address, "error", 0, "") end service_map[address] = false end diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 2019f530..e2e274af 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -569,6 +569,7 @@ function skynet.monitor(service, query) end assert(monitor, "Monitor launch failed") c.command("MONITOR", string.format(":%08x", monitor)) + return monitor end function skynet.mqlen() From f874fdc61877869919225b615f5d761dcce72bff Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 3 Jul 2014 17:40:07 +0800 Subject: [PATCH 062/729] throw error when skynet.exit --- HISTORY.md | 5 +++++ lualib/loader.lua | 4 +++- lualib/skynet.lua | 7 +++++++ test/testdeadcall.lua | 2 +- test/testmulticast.lua | 2 +- 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 3f4d5d9d..650cee24 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +Dev version +----------- +* add SERVICE_NAME in loader +* throw error back when skynet.error + v0.4.0 (2014-6-30) ----------- * Optimize redis driver `compose_message`. diff --git a/lualib/loader.lua b/lualib/loader.lua index 05224b92..1c4ee58a 100644 --- a/lualib/loader.lua +++ b/lualib/loader.lua @@ -3,11 +3,13 @@ for word in string.gmatch(..., "%S+") do table.insert(args, word) end +SERVICE_NAME = args[1] + local main, pattern local err = {} for pat in string.gmatch(LUA_SERVICE, "([^;]+);*") do - local filename = string.gsub(pat, "?", args[1]) + local filename = string.gsub(pat, "?", SERVICE_NAME) local f, msg = loadfile(filename) if not f then table.insert(err, msg) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index e2e274af..7719d2ae 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -253,6 +253,13 @@ end function skynet.exit() skynet.send(".launcher","lua","REMOVE",skynet.self()) + for co, session in pairs(session_coroutine_id) do + local address = session_coroutine_address[co] + local self = skynet.self() + if session~=0 and address then + skynet.redirect(self, address, "error", session, "") + end + end c.command("EXIT") end diff --git a/test/testdeadcall.lua b/test/testdeadcall.lua index d348a411..c5d67b8c 100644 --- a/test/testdeadcall.lua +++ b/test/testdeadcall.lua @@ -14,7 +14,7 @@ end) else skynet.start(function() - local test = skynet.newservice("testdeadcall", "test") -- launch self in test mode + local test = skynet.newservice(SERVICE_NAME, "test") -- launch self in test mode print(pcall(function() skynet.send(test,"lua", "hello world") diff --git a/test/testmulticast.lua b/test/testmulticast.lua index 88f79ccb..8f084abf 100644 --- a/test/testmulticast.lua +++ b/test/testmulticast.lua @@ -27,7 +27,7 @@ skynet.start(function() local channel = mc.new() print("New channel", channel) for i=1,10 do - local sub = skynet.newservice("testmulticast", "sub") + local sub = skynet.newservice(SERVICE_NAME, "sub") skynet.call(sub, "lua", "init", channel.channel) end From ece89a1b4965114fab75d6d7ab070a5d2fed679b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 7 Jul 2014 18:29:27 +0800 Subject: [PATCH 063/729] add new api skynet.task() --- lualib/skynet.lua | 11 +++++++++++ lualib/skynet/debug.lua | 7 +++++++ service/debug_console.lua | 1 + service/launcher.lua | 10 ++++++++++ 4 files changed, 29 insertions(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 7719d2ae..7e5115cb 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -583,6 +583,17 @@ function skynet.mqlen() return tonumber(c.command "MQLEN") end +function skynet.task(f) + local t = 0 + for _,co in pairs(session_id_coroutine) do + if f then + f(debug.traceback(co)) + end + t = t + 1 + end + return t +end + -- Inject internal debug framework local debug = require "skynet.debug" debug(skynet) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 807ee2ae..bb5877fe 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -21,9 +21,16 @@ end function dbgcmd.STAT() local stat = {} stat.mqlen = skynet.mqlen() + stat.task = skynet.task() skynet.ret(skynet.pack(stat)) end +function dbgcmd.TASK() + local task = {} + skynet.task(function(info) table.insert(task, info) end) + skynet.ret(skynet.pack(task)) +end + function dbgcmd.INFO() if internal_info_func then skynet.ret(skynet.pack(internal_info_func())) diff --git a/service/debug_console.lua b/service/debug_console.lua index b15c3889..a587f69d 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -113,6 +113,7 @@ function COMMAND.help() snax = "lanuch a new snax service", clearcache = "clear lua code cache", service = "List unique service", + task = "task address : show service task detail", } end diff --git a/service/launcher.lua b/service/launcher.lua index b80eadc9..5c1c1a8b 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -38,6 +38,16 @@ function command.INFO(_, _, handle) end end +function command.TASK(_, _, handle) + handle = handle_to_address(handle) + if services[handle] == nil then + return + else + local result = skynet.call(handle,"debug","TASK") + return result + end +end + function command.KILL(_, _, handle) handle = handle_to_address(handle) skynet.kill(handle) From 4967dc2fce7ee3f7b0dac14ab6231f975924e2a8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 7 Jul 2014 19:00:22 +0800 Subject: [PATCH 064/729] skynet.task return session:traceback --- lualib/skynet.lua | 8 ++++---- lualib/skynet/debug.lua | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 7e5115cb..f9d934c3 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -583,11 +583,11 @@ function skynet.mqlen() return tonumber(c.command "MQLEN") end -function skynet.task(f) +function skynet.task(ret) local t = 0 - for _,co in pairs(session_id_coroutine) do - if f then - f(debug.traceback(co)) + for session,co in pairs(session_id_coroutine) do + if ret then + ret[session] = debug.traceback(co) end t = t + 1 end diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index bb5877fe..c8f51f0b 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -27,7 +27,7 @@ end function dbgcmd.TASK() local task = {} - skynet.task(function(info) table.insert(task, info) end) + skynet.task(task) skynet.ret(skynet.pack(task)) end From 54f4d94ba2187fdf58244bce43c69c4f0f1dc049 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 7 Jul 2014 19:50:31 +0800 Subject: [PATCH 065/729] bugfix: create queue first --- service-src/service_harbor.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 36b2789c..fbb6406d 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -510,6 +510,9 @@ remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int skynet_send(context, destination, source, PTYPE_ERROR, 0 , NULL, 0); skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz); } else { + if (s->queue == NULL) { + s->queue = new_queue(); + } struct remote_message_header header; header.source = source; header.destination = type << HANDLE_REMOTE_SHIFT; From 8f8b844bdec65abf996dd780399084f16ca1a1d6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 7 Jul 2014 20:52:34 +0800 Subject: [PATCH 066/729] ready for release --- HISTORY.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 650cee24..0ca75332 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,7 +1,9 @@ -Dev version +v0.4.1 (2014-7-7) ----------- -* add SERVICE_NAME in loader -* throw error back when skynet.error +* Add SERVICE_NAME in loader +* Throw error back when skynet.error +* Add skynet.task +* Bugfix for last version (harbor service bugs) v0.4.0 (2014-6-30) ----------- From e60fb1d7221a6a8a772d2b37113c5d73b5c6eaf9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 8 Jul 2014 11:01:26 +0800 Subject: [PATCH 067/729] bugfix: remote send handle destination --- service-src/service_harbor.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index fbb6406d..78588867 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -315,7 +315,9 @@ forward_local_messsage(struct harbor *h, void *msg, int sz) { int type = (destination >> HANDLE_REMOTE_SHIFT) | PTYPE_TAG_DONTCOPY; destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); - skynet_send(h->ctx, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH); + if (skynet_send(h->ctx, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) { + skynet_error(h->ctx, "Unknown destination :%x from :%x", destination, header.source); + } } static void @@ -515,7 +517,7 @@ remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int } struct remote_message_header header; header.source = source; - header.destination = type << HANDLE_REMOTE_SHIFT; + header.destination = (type << HANDLE_REMOTE_SHIFT) | (destination & HANDLE_MASK); header.session = (uint32_t)session; push_queue(s->queue, (void *)msg, sz, &header); return 1; From 35775685c306395db67112c0d2aaa4729a67569f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 8 Jul 2014 19:56:30 +0800 Subject: [PATCH 068/729] add worker dispatch weight --- skynet-src/skynet_server.c | 31 +++++++++++++++++++------------ skynet-src/skynet_server.h | 2 +- skynet-src/skynet_start.c | 14 +++++++++++++- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 017d7206..8f619ad4 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -245,7 +245,7 @@ skynet_context_dispatchall(struct skynet_context * ctx) { } struct message_queue * -skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q) { +skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q, int weight) { if (q == NULL) { q = skynet_globalmq_pop(); if (q==NULL) @@ -261,18 +261,27 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue return skynet_globalmq_pop(); } + int i,n=1; struct skynet_message msg; - if (skynet_mq_pop(q,&msg)) { - skynet_context_release(ctx); - return skynet_globalmq_pop(); - } - skynet_monitor_trigger(sm, msg.source , handle); + for (i=0;i= 0) { + n = skynet_mq_length(q); + n >>= weight; + } - if (ctx->cb == NULL) { - skynet_free(msg.data); - } else { - _dispatch_message(ctx, &msg); + skynet_monitor_trigger(sm, msg.source , handle); + + if (ctx->cb == NULL) { + skynet_free(msg.data); + } else { + _dispatch_message(ctx, &msg); + } + + skynet_monitor_trigger(sm, 0,0); } assert(q == ctx->queue); @@ -285,8 +294,6 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue } skynet_context_release(ctx); - skynet_monitor_trigger(sm, 0,0); - return q; } diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index be4e301f..5d07ba70 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -16,7 +16,7 @@ void skynet_context_init(struct skynet_context *, uint32_t handle); int skynet_context_push(uint32_t handle, struct skynet_message *message); void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session); int skynet_context_newsession(struct skynet_context *); -struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *); // return next queue +struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *, int weight); // return next queue int skynet_context_total(); void skynet_context_dispatchall(struct skynet_context * context); // for skynet_error output before exit diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index c101da30..bef06b4c 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -27,6 +27,7 @@ struct monitor { struct worker_parm { struct monitor *m; int id; + int weight; }; #define CHECK_ABORT if (skynet_context_total()==0) break; @@ -118,12 +119,13 @@ static void * _worker(void *p) { struct worker_parm *wp = p; int id = wp->id; + int weight = wp->weight; struct monitor *m = wp->m; struct skynet_monitor *sm = m->m[id]; skynet_initthread(THREAD_WORKER); struct message_queue * q = NULL; for (;;) { - q = skynet_context_message_dispatch(sm, q); + q = skynet_context_message_dispatch(sm, q, weight); if (q == NULL) { CHECK_ABORT if (pthread_mutex_lock(&m->mutex) == 0) { @@ -169,10 +171,20 @@ _start(int thread) { create_thread(&pid[1], _timer, m); create_thread(&pid[2], _socket, m); + static int weight[] = { + -1, -1, -1, -1, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, }; struct worker_parm wp[thread]; for (i=0;i Date: Wed, 9 Jul 2014 11:25:28 +0800 Subject: [PATCH 069/729] socket id can't be less then 0 --- skynet-src/socket_server.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index aa105a16..76a995a4 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -942,6 +942,8 @@ socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * a // return -1 when error int64_t socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) { + if (id < 0) + return -1; struct socket * s = &ss->slot[id % MAX_SOCKET]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { return -1; @@ -958,6 +960,8 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz void socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) { + if (id < 0) + return; struct socket * s = &ss->slot[id % MAX_SOCKET]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { return; From d05859b766dfecd5c4f25790a6caef2e941e6fd3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 9 Jul 2014 12:03:44 +0800 Subject: [PATCH 070/729] gate support TCP_NODELAY --- examples/main.lua | 1 + examples/watchdog.lua | 1 + lualib-src/lua-socket.c | 9 +++++++ service/gate.lua | 11 ++++++++ skynet-src/skynet_socket.c | 5 ++++ skynet-src/skynet_socket.h | 1 + skynet-src/socket_server.c | 55 ++++++++++++++++++++++++++++---------- skynet-src/socket_server.h | 2 ++ 8 files changed, 71 insertions(+), 14 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index 8019c2bf..abbd2751 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -12,6 +12,7 @@ skynet.start(function() port = 8888, maxclient = max_client, }) + print("Watchdog listen on ", 8888) skynet.exit() end) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index 8ead2253..41b1eb09 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -34,6 +34,7 @@ end function CMD.start(conf) skynet.call(gate, "lua", "open" , conf) + skynet.call(gate, "lua", "nodelay", true) end skynet.start(function() diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index ad717f94..5ae8b046 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -476,6 +476,14 @@ lstart(lua_State *L) { return 0; } +static int +lnodelay(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + int id = luaL_checkinteger(L, 1); + skynet_socket_nodelay(ctx,id); + return 0; +} + int luaopen_socketdriver(lua_State *L) { luaL_checkversion(L); @@ -502,6 +510,7 @@ luaopen_socketdriver(lua_State *L) { { "lsend", lsendlow }, { "bind", lbind }, { "start", lstart }, + { "nodelay", lnodelay }, { NULL, NULL }, }; lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); diff --git a/service/gate.lua b/service/gate.lua index 54bb3e91..772f135a 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -8,6 +8,7 @@ local watchdog local maxclient local client_number = 0 local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end }) +local nodelay = false local connection = {} -- fd -> connection : { fd , client, agent , ip, mode } local forwarding = {} -- agent -> connection @@ -22,6 +23,13 @@ function CMD.open( source , conf ) socketdriver.start(socket) end +function CMD.nodelay(source, v) + if v ~= false then + v = true + end + nodelay = v +end + function CMD.close() assert(socket) socketdriver.close(socket) @@ -106,6 +114,9 @@ function MSG.open(fd, msg) } connection[fd] = c client_number = client_number + 1 + if nodelay then + socketdriver.nodelay(fd) + end skynet.send(watchdog, "lua", "socket", "open", fd, msg) end diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 610bf2fc..989f88e0 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -150,3 +150,8 @@ skynet_socket_start(struct skynet_context *ctx, int id) { uint32_t source = skynet_context_handle(ctx); socket_server_start(SOCKET_SERVER, source, id); } + +void +skynet_socket_nodelay(struct skynet_context *ctx, int id) { + socket_server_nodelay(SOCKET_SERVER, id); +} diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 2c0eb93b..7f0b4643 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -28,5 +28,6 @@ int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port int skynet_socket_bind(struct skynet_context *ctx, int fd); void skynet_socket_close(struct skynet_context *ctx, int id); void skynet_socket_start(struct skynet_context *ctx, int id); +void skynet_socket_nodelay(struct skynet_context *ctx, int id); #endif diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 76a995a4..9821bd71 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -34,6 +35,8 @@ #define PRIORITY_HIGH 0 #define PRIORITY_LOW 1 +#define HASH_ID(id) (((unsigned)id) % MAX_SOCKET) + struct write_buffer { struct write_buffer * next; char *ptr; @@ -107,6 +110,12 @@ struct request_start { uintptr_t opaque; }; +struct request_setopt { + int id; + int what; + int value; +}; + struct request_package { uint8_t header[8]; // 6 bytes dummy union { @@ -117,6 +126,7 @@ struct request_package { struct request_listen listen; struct request_bind bind; struct request_start start; + struct request_setopt setopt; } u; uint8_t dummy[256]; }; @@ -144,7 +154,7 @@ reserve_id(struct socket_server *ss) { if (id < 0) { id = __sync_and_and_fetch(&(ss->alloc_id), 0x7fffffff); } - struct socket *s = &ss->slot[id % MAX_SOCKET]; + struct socket *s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID) { if (__sync_bool_compare_and_swap(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) { s->id = id; @@ -267,7 +277,7 @@ check_wb_list(struct wb_list *s) { static struct socket * new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) { - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; assert(s->type == SOCKET_TYPE_RESERVE); if (add) { @@ -357,7 +367,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock return -1; _failed: freeaddrinfo( ai_list ); - ss->slot[id % MAX_SOCKET].type = SOCKET_TYPE_INVALID; + ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return SOCKET_ERROR; } @@ -502,7 +512,7 @@ send_buffer_empty(struct socket *s) { static int send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority) { int id = request->id; - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID || s->id != id || s->type == SOCKET_TYPE_HALFCLOSE || s->type == SOCKET_TYPE_PACCEPT) { @@ -556,7 +566,7 @@ _failed: result->id = id; result->ud = 0; result->data = NULL; - ss->slot[id % MAX_SOCKET].type = SOCKET_TYPE_INVALID; + ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return SOCKET_ERROR; } @@ -564,7 +574,7 @@ _failed: static int close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) { int id = request->id; - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID || s->id != id) { result->id = id; result->opaque = request->opaque; @@ -612,7 +622,7 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc result->opaque = request->opaque; result->ud = 0; result->data = NULL; - struct socket *s = &ss->slot[id % MAX_SOCKET]; + struct socket *s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { return SOCKET_ERROR; } @@ -633,6 +643,17 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc return -1; } +static void +setopt_socket(struct socket_server *ss, struct request_setopt *request) { + int id = request->id; + struct socket *s = &ss->slot[HASH_ID(id)]; + if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { + return; + } + int v = request->value; + setsockopt(s->fd, IPPROTO_TCP, request->what, &v, sizeof(v)); +} + static void block_readpipe(int pipefd, void *buffer, int sz) { for (;;) { @@ -696,6 +717,9 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_HIGH); case 'P': return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_LOW); + case 'T': + setopt_socket(ss, (struct request_setopt *)buffer); + return -1; default: fprintf(stderr, "socket-server: Unknown ctrl %c.\n",type); return -1; @@ -942,9 +966,7 @@ socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * a // return -1 when error int64_t socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) { - if (id < 0) - return -1; - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { return -1; } @@ -960,9 +982,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz void socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) { - if (id < 0) - return; - struct socket * s = &ss->slot[id % MAX_SOCKET]; + struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { return; } @@ -1057,4 +1077,11 @@ socket_server_start(struct socket_server *ss, uintptr_t opaque, int id) { send_request(ss, &request, 'S', sizeof(request.u.start)); } - +void +socket_server_nodelay(struct socket_server *ss, int id) { + struct request_package request; + request.u.setopt.id = id; + request.u.setopt.what = TCP_NODELAY; + request.u.setopt.value = 1; + send_request(ss, &request, 'T', sizeof(request.u.setopt)); +} diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index ea15c0e1..66648719 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -36,4 +36,6 @@ int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port); int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd); +void socket_server_nodelay(struct socket_server *, int id); + #endif From 0855bf30d998311781504e8eb5042794f4475642 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 9 Jul 2014 14:33:53 +0800 Subject: [PATCH 071/729] update history --- .gitignore | 8 ++++---- HISTORY.md | 6 ++++++ examples/watchdog.lua | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 9ee6cde3..f40078bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ *.o *.a -./skynet -./skynet.pid +/skynet +/skynet.pid 3rd/lua/lua 3rd/lua/luac -./cservice -./luaclib +/cservice +/luaclib *.so *.dSYM diff --git a/HISTORY.md b/HISTORY.md index 0ca75332..076fe91d 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +Dev version +----------- +* Bugfix : invalid negative socket id +* Add optional TCP_NODELAY support +* Add worker thread weight + v0.4.1 (2014-7-7) ----------- * Add SERVICE_NAME in loader diff --git a/examples/watchdog.lua b/examples/watchdog.lua index 41b1eb09..e28ab9fb 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -33,8 +33,8 @@ function SOCKET.data(fd, msg) end function CMD.start(conf) - skynet.call(gate, "lua", "open" , conf) skynet.call(gate, "lua", "nodelay", true) + skynet.call(gate, "lua", "open" , conf) end skynet.start(function() From b5e10b8f9f7a3fc78dcbbcdab8e1e7a4dbbd0a2a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 9 Jul 2014 15:26:34 +0800 Subject: [PATCH 072/729] add skynet.queue --- HISTORY.md | 1 + lualib/mqueue.lua | 2 ++ lualib/skynet.lua | 2 +- lualib/skynet/queue.lua | 33 +++++++++++++++++++++++++++++++++ test/pingserver.lua | 21 +++++++++++++++++++++ test/testqueue.lua | 18 ++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 lualib/skynet/queue.lua create mode 100644 test/testqueue.lua diff --git a/HISTORY.md b/HISTORY.md index 076fe91d..7a2ecf4a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,7 @@ Dev version * Bugfix : invalid negative socket id * Add optional TCP_NODELAY support * Add worker thread weight +* Add skynet.queue v0.4.1 (2014-7-7) ----------- diff --git a/lualib/mqueue.lua b/lualib/mqueue.lua index 11931200..f21921ca 100644 --- a/lualib/mqueue.lua +++ b/lualib/mqueue.lua @@ -1,3 +1,5 @@ +-- This is a deprecated module, use skynet.queue instead. + local skynet = require "skynet" local c = require "skynet.c" diff --git a/lualib/skynet.lua b/lualib/skynet.lua index f9d934c3..7eba476c 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -22,7 +22,7 @@ local skynet = { PTYPE_HARBOR = 5, PTYPE_SOCKET = 6, PTYPE_ERROR = 7, - PTYPE_QUEUE = 8, + PTYPE_QUEUE = 8, -- use in deprecated mqueue, use skynet.queue instead PTYPE_DEBUG = 9, PTYPE_LUA = 10, PTYPE_SNAX = 11, diff --git a/lualib/skynet/queue.lua b/lualib/skynet/queue.lua new file mode 100644 index 00000000..9f40e247 --- /dev/null +++ b/lualib/skynet/queue.lua @@ -0,0 +1,33 @@ +local skynet = require "skynet" +local coroutine = coroutine +local pcall = pcall +local table = table + +function skynet.queue() + local current_thread + local ref = 0 + local thread_queue = {} + return function(f, ...) + local thread = coroutine.running() + if ref == 0 then + current_thread = thread + elseif current_thread ~= thread then + table.insert(thread_queue, thread) + skynet.wait() + assert(ref == 0) + end + ref = ref + 1 + local ok, err = pcall(f, ...) + ref = ref - 1 + if ref == 0 then + current_thread = nil + local co = table.remove(thread_queue,1) + if co then + skynet.wakeup(co) + end + end + assert(ok,err) + end +end + +return skynet.queue \ No newline at end of file diff --git a/test/pingserver.lua b/test/pingserver.lua index 635cfeab..2ddefd7f 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +local queue = require "skynet.queue" local i = 0 local hello = "hello" @@ -8,9 +9,27 @@ function response.ping(hello) return hello end +-- response.sleep and accept.hello share one lock +local lock + +function accept.sleep(queue, n) + if queue then + lock( + function() + print("queue=",queue, n) + skynet.sleep(n) + end) + else + print("queue=",queue, n) + skynet.sleep(n) + end +end + function accept.hello() + lock(function() i = i + 1 print (i, hello) + end) end function response.error() @@ -19,6 +38,8 @@ end function init( ... ) print ("ping server start:", ...) + -- init queue + lock = queue() -- You can return "queue" for queue service mode -- return "queue" diff --git a/test/testqueue.lua b/test/testqueue.lua new file mode 100644 index 00000000..2076531e --- /dev/null +++ b/test/testqueue.lua @@ -0,0 +1,18 @@ +local skynet = require "skynet" +local snax = require "snax" + +skynet.start(function() + local ps = snax.uniqueservice ("pingserver", "test queue") + for i=1, 10 do + ps.post.sleep(true,i*10) + ps.post.hello() + end + for i=1, 10 do + ps.post.sleep(false,i*10) + ps.post.hello() + end + + skynet.exit() +end) + + From b7c12846a0dc0a71b4c2159308f0a0bf7a8d8540 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 9 Jul 2014 15:59:06 +0800 Subject: [PATCH 073/729] bugfix: socket channel --- HISTORY.md | 1 + lualib/socketchannel.lua | 3 +++ 2 files changed, 4 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 7a2ecf4a..1deddf51 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,6 +4,7 @@ Dev version * Add optional TCP_NODELAY support * Add worker thread weight * Add skynet.queue +* Bugfix: socketchannel v0.4.1 (2014-7-7) ----------- diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 32615ac0..e6a061f0 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -135,6 +135,9 @@ local function dispatch_by_order(self) if result ~= socket_error then errmsg = result_ok end + self.__result[co] = socket_error + self.__result_data[co] = errmsg + skynet.wakeup(co) wakeup_all(self, errmsg) end end From 83289b761261678018a73a8f0d65f77fde116cde Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 9 Jul 2014 21:07:26 +0800 Subject: [PATCH 074/729] bugfix: Issue #133 --- lualib-src/lua-mongo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index 3e4666dc..855cc385 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -506,7 +506,7 @@ op_insert(lua_State *L) { int i; for (i=1;i<=s;i++) { lua_rawgeti(L,3,i); - document doc = lua_touserdata(L,3); + document doc = lua_touserdata(L,-1); luaL_addlstring(&b, (const char *)doc, get_length(doc)); lua_pop(L,1); } From 54e6d03d3627b4e04245e31aca48b4848e2e9392 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 10 Jul 2014 16:57:41 +0800 Subject: [PATCH 075/729] update readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 08eaf877..e2d604f1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ For linux, install autoconf first for jemalloc ``` -git clone git@github.com:cloudwu/skynet.git +git clone https://github.com/cloudwu/skynet.git cd skynet make 'PLATFORM' # PLATFORM can be linux, macosx, freebsd now ``` @@ -38,5 +38,4 @@ You can also use the offical lua version , edit the makefile by yourself . * http://blog.codingnow.com/2012/09/the_design_of_skynet.html * http://blog.codingnow.com/2012/08/skynet.html -* http://blog.codingnow.com/2012/08/skynet_harbor_rpc.html * http://blog.codingnow.com/eo/skynet/ From c3e758dc87baf4bfc5803527a976a4314275499b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Jul 2014 20:28:36 +0800 Subject: [PATCH 076/729] add crypt lib --- Makefile | 5 +- lualib-src/lua-crypt.c | 859 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 863 insertions(+), 1 deletion(-) create mode 100644 lualib-src/lua-crypt.c diff --git a/Makefile b/Makefile index 45631905..63d9085d 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ jemalloc : $(MALLOC_STATICLIB) CSERVICE = snlua logger gate harbor LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ cjson clientsocket memory profile multicast \ - cluster + cluster crypt SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -110,6 +110,9 @@ $(LUA_CLIB_PATH)/multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ +$(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c new file mode 100644 index 00000000..34a190e7 --- /dev/null +++ b/lualib-src/lua-crypt.c @@ -0,0 +1,859 @@ +#include +#include + +#include +#include +#include +#include + +#define SMALL_CHUNK 256 + +/* the eight DES S-boxes */ + +uint32_t SB1[64] = { + 0x01010400, 0x00000000, 0x00010000, 0x01010404, + 0x01010004, 0x00010404, 0x00000004, 0x00010000, + 0x00000400, 0x01010400, 0x01010404, 0x00000400, + 0x01000404, 0x01010004, 0x01000000, 0x00000004, + 0x00000404, 0x01000400, 0x01000400, 0x00010400, + 0x00010400, 0x01010000, 0x01010000, 0x01000404, + 0x00010004, 0x01000004, 0x01000004, 0x00010004, + 0x00000000, 0x00000404, 0x00010404, 0x01000000, + 0x00010000, 0x01010404, 0x00000004, 0x01010000, + 0x01010400, 0x01000000, 0x01000000, 0x00000400, + 0x01010004, 0x00010000, 0x00010400, 0x01000004, + 0x00000400, 0x00000004, 0x01000404, 0x00010404, + 0x01010404, 0x00010004, 0x01010000, 0x01000404, + 0x01000004, 0x00000404, 0x00010404, 0x01010400, + 0x00000404, 0x01000400, 0x01000400, 0x00000000, + 0x00010004, 0x00010400, 0x00000000, 0x01010004 +}; + +static uint32_t SB2[64] = { + 0x80108020, 0x80008000, 0x00008000, 0x00108020, + 0x00100000, 0x00000020, 0x80100020, 0x80008020, + 0x80000020, 0x80108020, 0x80108000, 0x80000000, + 0x80008000, 0x00100000, 0x00000020, 0x80100020, + 0x00108000, 0x00100020, 0x80008020, 0x00000000, + 0x80000000, 0x00008000, 0x00108020, 0x80100000, + 0x00100020, 0x80000020, 0x00000000, 0x00108000, + 0x00008020, 0x80108000, 0x80100000, 0x00008020, + 0x00000000, 0x00108020, 0x80100020, 0x00100000, + 0x80008020, 0x80100000, 0x80108000, 0x00008000, + 0x80100000, 0x80008000, 0x00000020, 0x80108020, + 0x00108020, 0x00000020, 0x00008000, 0x80000000, + 0x00008020, 0x80108000, 0x00100000, 0x80000020, + 0x00100020, 0x80008020, 0x80000020, 0x00100020, + 0x00108000, 0x00000000, 0x80008000, 0x00008020, + 0x80000000, 0x80100020, 0x80108020, 0x00108000 +}; + +static uint32_t SB3[64] = { + 0x00000208, 0x08020200, 0x00000000, 0x08020008, + 0x08000200, 0x00000000, 0x00020208, 0x08000200, + 0x00020008, 0x08000008, 0x08000008, 0x00020000, + 0x08020208, 0x00020008, 0x08020000, 0x00000208, + 0x08000000, 0x00000008, 0x08020200, 0x00000200, + 0x00020200, 0x08020000, 0x08020008, 0x00020208, + 0x08000208, 0x00020200, 0x00020000, 0x08000208, + 0x00000008, 0x08020208, 0x00000200, 0x08000000, + 0x08020200, 0x08000000, 0x00020008, 0x00000208, + 0x00020000, 0x08020200, 0x08000200, 0x00000000, + 0x00000200, 0x00020008, 0x08020208, 0x08000200, + 0x08000008, 0x00000200, 0x00000000, 0x08020008, + 0x08000208, 0x00020000, 0x08000000, 0x08020208, + 0x00000008, 0x00020208, 0x00020200, 0x08000008, + 0x08020000, 0x08000208, 0x00000208, 0x08020000, + 0x00020208, 0x00000008, 0x08020008, 0x00020200 +}; + +static uint32_t SB4[64] = { + 0x00802001, 0x00002081, 0x00002081, 0x00000080, + 0x00802080, 0x00800081, 0x00800001, 0x00002001, + 0x00000000, 0x00802000, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00800080, 0x00800001, + 0x00000001, 0x00002000, 0x00800000, 0x00802001, + 0x00000080, 0x00800000, 0x00002001, 0x00002080, + 0x00800081, 0x00000001, 0x00002080, 0x00800080, + 0x00002000, 0x00802080, 0x00802081, 0x00000081, + 0x00800080, 0x00800001, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00000000, 0x00802000, + 0x00002080, 0x00800080, 0x00800081, 0x00000001, + 0x00802001, 0x00002081, 0x00002081, 0x00000080, + 0x00802081, 0x00000081, 0x00000001, 0x00002000, + 0x00800001, 0x00002001, 0x00802080, 0x00800081, + 0x00002001, 0x00002080, 0x00800000, 0x00802001, + 0x00000080, 0x00800000, 0x00002000, 0x00802080 +}; + +static uint32_t SB5[64] = { + 0x00000100, 0x02080100, 0x02080000, 0x42000100, + 0x00080000, 0x00000100, 0x40000000, 0x02080000, + 0x40080100, 0x00080000, 0x02000100, 0x40080100, + 0x42000100, 0x42080000, 0x00080100, 0x40000000, + 0x02000000, 0x40080000, 0x40080000, 0x00000000, + 0x40000100, 0x42080100, 0x42080100, 0x02000100, + 0x42080000, 0x40000100, 0x00000000, 0x42000000, + 0x02080100, 0x02000000, 0x42000000, 0x00080100, + 0x00080000, 0x42000100, 0x00000100, 0x02000000, + 0x40000000, 0x02080000, 0x42000100, 0x40080100, + 0x02000100, 0x40000000, 0x42080000, 0x02080100, + 0x40080100, 0x00000100, 0x02000000, 0x42080000, + 0x42080100, 0x00080100, 0x42000000, 0x42080100, + 0x02080000, 0x00000000, 0x40080000, 0x42000000, + 0x00080100, 0x02000100, 0x40000100, 0x00080000, + 0x00000000, 0x40080000, 0x02080100, 0x40000100 +}; + +static uint32_t SB6[64] = { + 0x20000010, 0x20400000, 0x00004000, 0x20404010, + 0x20400000, 0x00000010, 0x20404010, 0x00400000, + 0x20004000, 0x00404010, 0x00400000, 0x20000010, + 0x00400010, 0x20004000, 0x20000000, 0x00004010, + 0x00000000, 0x00400010, 0x20004010, 0x00004000, + 0x00404000, 0x20004010, 0x00000010, 0x20400010, + 0x20400010, 0x00000000, 0x00404010, 0x20404000, + 0x00004010, 0x00404000, 0x20404000, 0x20000000, + 0x20004000, 0x00000010, 0x20400010, 0x00404000, + 0x20404010, 0x00400000, 0x00004010, 0x20000010, + 0x00400000, 0x20004000, 0x20000000, 0x00004010, + 0x20000010, 0x20404010, 0x00404000, 0x20400000, + 0x00404010, 0x20404000, 0x00000000, 0x20400010, + 0x00000010, 0x00004000, 0x20400000, 0x00404010, + 0x00004000, 0x00400010, 0x20004010, 0x00000000, + 0x20404000, 0x20000000, 0x00400010, 0x20004010 +}; + +static uint32_t SB7[64] = { + 0x00200000, 0x04200002, 0x04000802, 0x00000000, + 0x00000800, 0x04000802, 0x00200802, 0x04200800, + 0x04200802, 0x00200000, 0x00000000, 0x04000002, + 0x00000002, 0x04000000, 0x04200002, 0x00000802, + 0x04000800, 0x00200802, 0x00200002, 0x04000800, + 0x04000002, 0x04200000, 0x04200800, 0x00200002, + 0x04200000, 0x00000800, 0x00000802, 0x04200802, + 0x00200800, 0x00000002, 0x04000000, 0x00200800, + 0x04000000, 0x00200800, 0x00200000, 0x04000802, + 0x04000802, 0x04200002, 0x04200002, 0x00000002, + 0x00200002, 0x04000000, 0x04000800, 0x00200000, + 0x04200800, 0x00000802, 0x00200802, 0x04200800, + 0x00000802, 0x04000002, 0x04200802, 0x04200000, + 0x00200800, 0x00000000, 0x00000002, 0x04200802, + 0x00000000, 0x00200802, 0x04200000, 0x00000800, + 0x04000002, 0x04000800, 0x00000800, 0x00200002 +}; + +static uint32_t SB8[64] = { + 0x10001040, 0x00001000, 0x00040000, 0x10041040, + 0x10000000, 0x10001040, 0x00000040, 0x10000000, + 0x00040040, 0x10040000, 0x10041040, 0x00041000, + 0x10041000, 0x00041040, 0x00001000, 0x00000040, + 0x10040000, 0x10000040, 0x10001000, 0x00001040, + 0x00041000, 0x00040040, 0x10040040, 0x10041000, + 0x00001040, 0x00000000, 0x00000000, 0x10040040, + 0x10000040, 0x10001000, 0x00041040, 0x00040000, + 0x00041040, 0x00040000, 0x10041000, 0x00001000, + 0x00000040, 0x10040040, 0x00001000, 0x00041040, + 0x10001000, 0x00000040, 0x10000040, 0x10040000, + 0x10040040, 0x10000000, 0x00040000, 0x10001040, + 0x00000000, 0x10041040, 0x00040040, 0x10000040, + 0x10040000, 0x10001000, 0x10001040, 0x00000000, + 0x10041040, 0x00041000, 0x00041000, 0x00001040, + 0x00001040, 0x00040040, 0x10000000, 0x10041000 +}; + +/* PC1: left and right halves bit-swap */ + +static uint32_t LHs[16] = { + 0x00000000, 0x00000001, 0x00000100, 0x00000101, + 0x00010000, 0x00010001, 0x00010100, 0x00010101, + 0x01000000, 0x01000001, 0x01000100, 0x01000101, + 0x01010000, 0x01010001, 0x01010100, 0x01010101 +}; + +static uint32_t RHs[16] = { + 0x00000000, 0x01000000, 0x00010000, 0x01010000, + 0x00000100, 0x01000100, 0x00010100, 0x01010100, + 0x00000001, 0x01000001, 0x00010001, 0x01010001, + 0x00000101, 0x01000101, 0x00010101, 0x01010101, +}; + +/* platform-independant 32-bit integer manipulation macros */ + +#define GET_UINT32(n,b,i) \ +{ \ + (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ + | ( (uint32_t) (b)[(i) + 1] << 16 ) \ + | ( (uint32_t) (b)[(i) + 2] << 8 ) \ + | ( (uint32_t) (b)[(i) + 3] ); \ +} + +#define PUT_UINT32(n,b,i) \ +{ \ + (b)[(i) ] = (uint8_t) ( (n) >> 24 ); \ + (b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \ + (b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \ + (b)[(i) + 3] = (uint8_t) ( (n) ); \ +} + +/* Initial Permutation macro */ + +#define DES_IP(X,Y) \ +{ \ + T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ + T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ + T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ + T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ + Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \ + T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \ + X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \ +} + +/* Final Permutation macro */ + +#define DES_FP(X,Y) \ +{ \ + X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \ + T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \ + Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \ + T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ + T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ + T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ + T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ +} + +/* DES round macro */ + +#define DES_ROUND(X,Y) \ +{ \ + T = *SK++ ^ X; \ + Y ^= SB8[ (T ) & 0x3F ] ^ \ + SB6[ (T >> 8) & 0x3F ] ^ \ + SB4[ (T >> 16) & 0x3F ] ^ \ + SB2[ (T >> 24) & 0x3F ]; \ + \ + T = *SK++ ^ ((X << 28) | (X >> 4)); \ + Y ^= SB7[ (T ) & 0x3F ] ^ \ + SB5[ (T >> 8) & 0x3F ] ^ \ + SB3[ (T >> 16) & 0x3F ] ^ \ + SB1[ (T >> 24) & 0x3F ]; \ +} + +/* DES key schedule */ + +static void +des_main_ks( uint32_t SK[32], const uint8_t key[8] ) { + int i; + uint32_t X, Y, T; + + GET_UINT32( X, key, 0 ); + GET_UINT32( Y, key, 4 ); + + /* Permuted Choice 1 */ + + T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4); + T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T ); + + X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2) + | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] ) + | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6) + | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4); + + Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2) + | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] ) + | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6) + | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4); + + X &= 0x0FFFFFFF; + Y &= 0x0FFFFFFF; + + /* calculate subkeys */ + + for( i = 0; i < 16; i++ ) + { + if( i < 2 || i == 8 || i == 15 ) + { + X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF; + Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF; + } + else + { + X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF; + Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF; + } + + *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000) + | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000) + | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000) + | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000) + | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000) + | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000) + | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400) + | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100) + | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010) + | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004) + | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001); + + *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000) + | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000) + | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000) + | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000) + | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000) + | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000) + | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000) + | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400) + | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100) + | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011) + | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002); + } +} + +/* DES 64-bit block encryption/decryption */ + +static void +des_crypt( const uint32_t SK[32], const uint8_t input[8], uint8_t output[8] ) { + uint32_t X, Y, T; + + GET_UINT32( X, input, 0 ); + GET_UINT32( Y, input, 4 ); + + DES_IP( X, Y ); + + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + DES_ROUND( Y, X ); DES_ROUND( X, Y ); + + DES_FP( Y, X ); + + PUT_UINT32( Y, output, 0 ); + PUT_UINT32( X, output, 4 ); +} + +static int +lrandomkey(lua_State *L) { + char tmp[8]; + int i; + for (i=0;i<8;i++) { + tmp[i] = random() & 0xff; + } + lua_pushlstring(L, tmp, 8); + return 1; +} + +static void +des_key(lua_State *L, uint32_t SK[32]) { + size_t keysz = 0; + const void * key = luaL_checklstring(L, 1, &keysz); + if (keysz != 8) { + luaL_error(L, "Invalid key size %d, need 8 bytes", (int)keysz); + } + des_main_ks(SK, key); +} + +static int +ldesencode(lua_State *L) { + uint32_t SK[32]; + des_key(L, SK); + + size_t textsz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 2, &textsz); + size_t chunksz = (textsz + 8) & ~7; + uint8_t tmp[SMALL_CHUNK]; + uint8_t *buffer = tmp; + if (chunksz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, chunksz); + } + int i; + for (i=0;i<(int)textsz-7;i+=8) { + des_crypt(SK, text+i, buffer+i); + } + int bytes = textsz - i; + uint8_t tail[8]; + int j; + for (j=0;j<8;j++) { + if (j < bytes) { + tail[j] = text[i+j]; + } else if (j==bytes) { + tail[j] = 0x80; + } else { + tail[j] = 0; + } + } + des_crypt(SK, tail, buffer+i); + lua_pushlstring(L, (const char *)buffer, chunksz); + + return 1; +} + +static int +ldesdecode(lua_State *L) { + uint32_t ESK[32]; + des_key(L, ESK); + uint32_t SK[32]; + int i; + for( i = 0; i < 32; i += 2 ) { + SK[i] = ESK[30 - i]; + SK[i + 1] = ESK[31 - i]; + } + size_t textsz = 0; + const uint8_t *text = (const uint8_t *)luaL_checklstring(L, 2, &textsz); + if ((textsz & 7) || textsz == 0) { + return luaL_error(L, "Invalid des crypt text length %d", (int)textsz); + } + uint8_t tmp[SMALL_CHUNK]; + uint8_t *buffer = tmp; + if (textsz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, textsz); + } + for (i=0;i=textsz-8;i--) { + if (buffer[i] == 0) { + padding++; + } else if (buffer[i] == 0x80) { + break; + } else { + return luaL_error(L, "Invalid des crypt text"); + } + } + if (padding > 8) { + return luaL_error(L, "Invalid des crypt text"); + } + lua_pushlstring(L, (const char *)buffer, textsz - padding); + return 1; +} + + +static void +Hash(const char * str, int sz, uint8_t key[8]) { + uint32_t djb_hash = 5381L; + uint32_t js_hash = 1315423911L; + + int i; + for (i=0;i> 2)); + } + + key[0] = djb_hash & 0xff; + key[1] = (djb_hash >> 8) & 0xff; + key[2] = (djb_hash >> 16) & 0xff; + key[3] = (djb_hash >> 24) & 0xff; + + key[4] = js_hash & 0xff; + key[5] = (js_hash >> 8) & 0xff; + key[6] = (js_hash >> 16) & 0xff; + key[7] = (js_hash >> 24) & 0xff; +} + +static int +lhashkey(lua_State *L) { + size_t sz = 0; + const char * key = luaL_checklstring(L, 1, &sz); + uint8_t realkey[8]; + Hash(key,(int)sz,realkey); + lua_pushlstring(L, (const char *)realkey, 8); + return 1; +} + +static int +ltohex(lua_State *L) { + static char hex[] = "0123456789abcdef"; + size_t sz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (sz > SMALL_CHUNK/2) { + buffer = lua_newuserdata(L, sz * 2); + } + int i; + for (i=0;i> 4]; + buffer[i*2+1] = hex[text[i] & 0xf]; + } + lua_pushlstring(L, buffer, sz * 2); + return 1; +} + +#define HEX(v,c) { char tmp = (char) c; if (tmp >= '0' && tmp <= '9') { v = tmp-'0'; } else { v = tmp - 'a' + 10; } } + +static int +lfromhex(lua_State *L) { + size_t sz = 0; + const char * text = luaL_checklstring(L, 1, &sz); + if (sz & 2) { + return luaL_error(L, "Invalid hex text size %d", (int)sz); + } + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (sz > SMALL_CHUNK*2) { + buffer = lua_newuserdata(L, sz / 2); + } + int i; + for (i=0;i 16 || low > 16) { + return luaL_error(L, "Invalid hex text", text); + } + buffer[i/2] = hi<<4 | low; + } + lua_pushlstring(L, buffer, i/2); + return 1; +} + +// Constants are the integer part of the sines of integers (in radians) * 2^32. +const uint32_t k[64] = { +0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee , +0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 , +0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be , +0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 , +0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa , +0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 , +0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed , +0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a , +0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c , +0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 , +0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 , +0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 , +0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 , +0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 , +0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 , +0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; + +// r specifies the per-round shift amounts +const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, + 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, + 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, + 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; + +// leftrotate function definition +#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) + +static void +hmac(uint32_t x[2], uint32_t y[2], uint32_t result[2]) { + uint32_t w[16]; + uint32_t a, b, c, d, f, g, temp; + int i; + + a = 0x67452301u; + b = 0xefcdab89u; + c = 0x98badcfeu; + d = 0x10325476u; + + for (i=0;i<16;i+=4) { + w[i] = x[1]; + w[i+1] = x[0]; + w[i+2] = y[1]; + w[i+3] = y[0]; + } + + for(i = 0; i<64; i++) { + if (i < 16) { + f = (b & c) | ((~b) & d); + g = i; + } else if (i < 32) { + f = (d & b) | ((~d) & c); + g = (5*i + 1) % 16; + } else if (i < 48) { + f = b ^ c ^ d; + g = (3*i + 5) % 16; + } else { + f = c ^ (b | (~d)); + g = (7*i) % 16; + } + + temp = d; + d = c; + c = b; + b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]); + a = temp; + + } + + result[0] = c^d; + result[1] = a^b; +} + +static void +read64(lua_State *L, uint32_t xx[2], uint32_t yy[2]) { + size_t sz = 0; + const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); + if (sz != 8) { + luaL_error(L, "Invalid hmac x"); + } + const uint8_t *y = (const uint8_t *)luaL_checklstring(L, 2, &sz); + if (sz != 8) { + luaL_error(L, "Invalid hmac y"); + } + xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; + xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; + yy[0] = y[0] | y[1]<<8 | y[2]<<16 | y[3]<<24; + yy[1] = y[4] | y[5]<<8 | y[6]<<16 | y[7]<<24; +} + +static int +lhmac64(lua_State *L) { + uint32_t x[2], y[2]; + read64(L, x, y); + uint32_t result[2]; + hmac(x,y,result); + uint8_t tmp[8]; + tmp[0] = result[0] & 0xff; + tmp[1] = (result[0] >> 8 )& 0xff; + tmp[2] = (result[0] >> 16 )& 0xff; + tmp[3] = (result[0] >> 24 )& 0xff; + tmp[4] = result[1] & 0xff; + tmp[5] = (result[1] >> 8 )& 0xff; + tmp[6] = (result[1] >> 16 )& 0xff; + tmp[7] = (result[1] >> 24 )& 0xff; + + lua_pushlstring(L, (const char *)tmp, 8); + return 1; +} + +// powmodp64 for DH-key exchange + +// The biggest 64bit prime +#define P 0xffffffffffffffc5ull + +static inline uint64_t +mul_mod_p(uint64_t a, uint64_t b) { + uint64_t m = 0; + while(b) { + if(b&1) { + uint64_t t = P-a; + if ( m >= t) { + m -= t; + } else { + m += a; + } + } + if (a >= P - a) { + a = a * 2 - P; + } else { + a = a * 2; + } + b>>=1; + } + return m; +} + +static inline uint64_t +pow_mod_p(uint64_t a, uint64_t b) { + if (b==1) { + return a; + } + uint64_t t = pow_mod_p(a, b>>1); + t = mul_mod_p(t,t); + if (b % 2) { + t = mul_mod_p(t, a); + } + return t; +} + +// calc a^b % p +static uint64_t +powmodp(uint64_t a, uint64_t b) { + if (a > P) + a%=P; + return pow_mod_p(a,b); +} + +static void +push64(lua_State *L, uint64_t r) { + uint8_t tmp[8]; + tmp[0] = r & 0xff; + tmp[1] = (r >> 8 )& 0xff; + tmp[2] = (r >> 16 )& 0xff; + tmp[3] = (r >> 24 )& 0xff; + tmp[4] = (r >> 32 )& 0xff; + tmp[5] = (r >> 40 )& 0xff; + tmp[6] = (r >> 48 )& 0xff; + tmp[7] = (r >> 56 )& 0xff; + + lua_pushlstring(L, (const char *)tmp, 8); +} + +static int +ldhsecret(lua_State *L) { + uint32_t x[2], y[2]; + read64(L, x, y); + uint64_t r = powmodp((uint64_t)x[0] | (uint64_t)x[1]<<32, + (uint64_t)y[0] | (uint64_t)y[1]<<32); + + push64(L, r); + + return 1; +} + +#define G 5 + +static int +ldhexchange(lua_State *L) { + size_t sz = 0; + const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); + if (sz != 8) { + luaL_error(L, "Invalid hmac x"); + } + uint32_t xx[2]; + xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; + xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; + + uint64_t r = powmodp(5, (uint64_t)xx[0] | (uint64_t)xx[1]<<32); + push64(L, r); + return 1; +} + +// base64 + +static int +lb64encode(lua_State *L) { + static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + size_t sz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); + int encode_sz = (sz + 2)/3*4; + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (encode_sz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, encode_sz); + } + int i,j; + j=0; + for (i=0;i> 18]; + buffer[j+1] = encoding[(v >> 12) & 0x3f]; + buffer[j+2] = encoding[(v >> 6) & 0x3f]; + buffer[j+3] = encoding[(v) & 0x3f]; + j+=4; + } + int padding = sz-i; + uint32_t v; + switch(padding) { + case 1 : + v = text[i]; + buffer[j] = encoding[v >> 2]; + buffer[j+1] = encoding[(v & 3) << 4]; + buffer[j+2] = '='; + buffer[j+3] = '='; + break; + case 2 : + v = text[i] << 8 | text[i+1]; + buffer[j] = encoding[v >> 10]; + buffer[j+1] = encoding[(v >> 4) & 0x3f]; + buffer[j+2] = encoding[(v & 0xf) << 2]; + buffer[j+3] = '='; + break; + } + lua_pushlstring(L, buffer, encode_sz); + return 1; +} + +static inline int +b64index(uint8_t c) { + static const int decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; + int decoding_size = sizeof(decoding)/sizeof(decoding[0]); + if (c<43) { + return -1; + } + c -= 43; + if (c>=decoding_size) + return -1; + return decoding[c]; +} + +static int +lb64decode(lua_State *L) { + size_t sz = 0; + const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); + int decode_sz = (sz+3)/4*3; + char tmp[SMALL_CHUNK]; + char *buffer = tmp; + if (decode_sz > SMALL_CHUNK) { + buffer = lua_newuserdata(L, decode_sz); + } + int i,j; + int output = 0; + for (i=0;i=sz) { + return luaL_error(L, "Invalid base64 text"); + } + c[j] = b64index(text[i]); + if (c[j] == -1) { + ++i; + continue; + } + if (c[j] == -2) { + ++padding; + } + ++i; + ++j; + } + uint32_t v; + switch (padding) { + case 0: + v = (unsigned)c[0] << 18 | c[1] << 12 | c[2] << 6 | c[3]; + buffer[output] = v >> 16; + buffer[output+1] = (v >> 8) & 0xff; + buffer[output+2] = v & 0xff; + output += 3; + break; + case 1: + if (c[3] != -2 || (c[2] & 3)!=0) { + return luaL_error(L, "Invalid base64 text"); + } + v = (unsigned)c[0] << 10 | c[1] << 4 | c[2] >> 2 ; + buffer[output] = v >> 8; + buffer[output+1] = v & 0xff; + output += 2; + break; + case 2: + if (c[3] != -2 || c[2] != -2 || (c[1] & 0xf) !=0) { + return luaL_error(L, "Invalid base64 text"); + } + v = (unsigned)c[0] << 2 | c[1] >> 4; + buffer[output] = v; + ++ output; + break; + default: + return luaL_error(L, "Invalid base64 text"); + } + } + lua_pushlstring(L, buffer, output); + return 1; +} + +int +luaopen_crypt(lua_State *L) { + luaL_checkversion(L); + srandom(time(NULL)); + luaL_Reg l[] = { + { "hashkey", lhashkey }, + { "randomkey", lrandomkey }, + { "desencode", ldesencode }, + { "desdecode", ldesdecode }, + { "hexencode", ltohex }, + { "hexdecode", lfromhex }, + { "hmac64", lhmac64 }, + { "dhexchange", ldhexchange }, + { "dhsecret", ldhsecret }, + { "base64encode", lb64encode }, + { "base64decode", lb64decode }, + { NULL, NULL }, + }; + luaL_newlib(L,l); + return 1; +} From 3a5de32ad0347120eec1758fbcd247e2113e9b3d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Jul 2014 20:30:02 +0800 Subject: [PATCH 077/729] add socket.limit for defence --- lualib-src/lua-socket.c | 6 ++++++ lualib/socket.lua | 15 +++++++++++++-- test/testsocket.lua | 5 ++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 5ae8b046..8bfefc0e 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -14,6 +14,7 @@ #define BACKLOG 32 // 2 ** 12 == 4096 #define LARGE_PAGE_NODE 12 +#define BUFFER_LIMIT (256 * 1024) struct buffer_node { char * msg; @@ -22,6 +23,7 @@ struct buffer_node { }; struct socket_buffer { + int limit; int size; int offset; struct buffer_node *head; @@ -64,6 +66,7 @@ lnewpool(lua_State *L, int sz) { static int lnewbuffer(lua_State *L) { struct socket_buffer * sb = lua_newuserdata(L, sizeof(*sb)); + sb->limit = luaL_optint(L,1,BUFFER_LIMIT); sb->size = 0; sb->offset = 0; sb->head = NULL; @@ -126,6 +129,9 @@ lpushbuffer(lua_State *L) { sb->size += sz; lua_pushinteger(L, sb->size); + if (sb->limit > 0 && sb->size > sb->limit) { + return luaL_error(L, "buffer overflow (limit = %d, size = %d)", sb->limit, sb->size); + } return 1; } diff --git a/lualib/socket.lua b/lualib/socket.lua index d7bc49d5..28268747 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -2,6 +2,7 @@ local driver = require "socketdriver" local skynet = require "skynet" local assert = assert +local buffer_limit = -1 local socket = {} -- api local buffer_pool = {} -- store all message buffer object local socket_pool = setmetatable( -- store all socket object @@ -47,7 +48,13 @@ socket_message[1] = function(id, size, data) return end - local sz = driver.push(s.buffer, buffer_pool, data, size) + local ok , sz = pcall(driver.push, s.buffer, buffer_pool, data, size) + if not ok then + skynet.error("socket: error on ", id , sz) + driver.clear(s.buffer,buffer_pool) + driver.close(id) + return + end local rr = s.read_required local rrt = type(rr) if rrt == "number" then @@ -123,7 +130,7 @@ skynet.register_protocol { local function connect(id, func) local newbuffer if func == nil then - newbuffer = driver.buffer() + newbuffer = driver.buffer(buffer_limit) end local s = { id = id, @@ -318,4 +325,8 @@ function socket.abandon(id) socket_pool[id] = nil end +function socket.limit(limit) + buffer_limit = limit +end + return socket diff --git a/test/testsocket.lua b/test/testsocket.lua index c465079c..05313a11 100644 --- a/test/testsocket.lua +++ b/test/testsocket.lua @@ -21,6 +21,9 @@ if mode == "agent" then id = tonumber(id) skynet.start(function() + -- A small limit, if socket buffer overflow, close the client + socket.limit(64) + skynet.fork(function() echo(id) skynet.exit() @@ -30,7 +33,7 @@ else local function accept(id) socket.start(id) socket.write(id, "Hello Skynet\n") - skynet.newservice("testsocket", "agent", id) + skynet.newservice(SERVICE_NAME, "agent", id) -- notice: Some data on this connection(id) may lost before new service start. -- So, be careful when you want to use start / abandon / start . socket.abandon(id) From a090899bce1a4ec00ccbce393c1738c7f514deb2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Jul 2014 23:14:05 +0800 Subject: [PATCH 078/729] improve cluster --- HISTORY.md | 3 ++ examples/client.lua | 2 +- lualib-src/lua-clientsocket.c | 87 ++++++++++++++++++++++++++++++++--- lualib-src/lua-cluster.c | 31 +++++++++---- lualib/cluster.lua | 4 ++ lualib/skynet.lua | 3 ++ service/clusterd.lua | 27 +++++++++-- 7 files changed, 136 insertions(+), 21 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 1deddf51..52e1d4b0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,9 @@ Dev version * Add worker thread weight * Add skynet.queue * Bugfix: socketchannel +* cluster can throw error +* Add readline and writeline to clientsocket lib +* Add cluster.reload to reload config file v0.4.1 (2014-7-7) ----------- diff --git a/examples/client.lua b/examples/client.lua index 9197a1c3..278922e6 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -39,7 +39,7 @@ end while true do dispatch() - local cmd = socket.readline() + local cmd = socket.readstdin() if cmd then local args = {} string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end ) diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index 1e499f2d..2676c267 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -125,14 +125,19 @@ _block: boolean (true: data, false: block, nil: close) string last */ + +struct socket_buffer { + void * buffer; + int sz; +}; + static int -lrecv(lua_State *L) { +recv_socket(lua_State *L, char *tmp, struct socket_buffer *result) { int fd = luaL_checkinteger(L,1); size_t sz = 0; const char * last = lua_tolstring(L,2,&sz); luaL_checktype(L, 3, LUA_TTABLE); - char tmp[CACHE_SIZE]; char * buffer; int r = recv(fd, tmp, CACHE_SIZE, 0); if (r == 0) { @@ -163,10 +168,64 @@ lrecv(lua_State *L) { lua_pushnil(L); lua_rawseti(L, 3, i); } - - return unpack(L, (uint8_t *)buffer, r+sz, 0); + result->buffer = buffer; + result->sz = r + sz; + return -1; } +static int +lrecv(lua_State *L) { + struct socket_buffer sb; + char tmp[CACHE_SIZE]; + int ret = recv_socket(L, tmp, &sb); + if (ret < 0) { + return unpack(L, sb.buffer, sb.sz, 0); + } else { + return ret; + } +} + +static int +unpack_line(lua_State *L, uint8_t *buffer, int sz, int n) { + if (sz == 0) + goto _block; + if (buffer[0] == '\n') { + return unpack_line(L, buffer+1, sz-1, n); + } + int i; + for (i=1;ihead == q->tail) { @@ -236,6 +295,18 @@ lreadline(lua_State *L) { return 1; } +static int +lwriteline(lua_State *L) { + size_t sz = 0; + int fd = luaL_checkinteger(L,1); + const char * msg = luaL_checklstring(L, 2, &sz); + block_send(L, fd, msg, sz); + char nl[1] = { '\n' }; + block_send(L, fd, nl, 1); + + return 0; +} + int luaopen_clientsocket(lua_State *L) { luaL_checkversion(L); @@ -245,14 +316,16 @@ luaopen_clientsocket(lua_State *L) { { "send", lsend }, { "close", lclose }, { "usleep", lusleep }, + { "readline", lreadline }, + { "writeline", lwriteline }, { NULL, NULL }, }; luaL_newlib(L, l); struct queue * q = lua_newuserdata(L, sizeof(*q)); memset(q, 0, sizeof(*q)); - lua_pushcclosure(L, lreadline, 1); - lua_setfield(L, -2, "readline"); + lua_pushcclosure(L, lreadstdin, 1); + lua_setfield(L, -2, "readstdin"); pthread_t pid ; pthread_create(&pid, NULL, readline_stdin, q); diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 92134ced..29ebb08a 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -15,7 +15,7 @@ uint32_t next_session */ -#define TEMP_LENGTH 0x10002 +#define TEMP_LENGTH 0x10007 static void fill_uint32(uint8_t * buf, uint32_t n) { @@ -146,6 +146,7 @@ lunpackrequest(lua_State *L) { /* int session + boolean ok lightuserdata msg int sz return string response @@ -155,15 +156,27 @@ lpackresponse(lua_State *L) { uint32_t session = luaL_checkunsigned(L,1); // clusterd.lua:command.socket call lpackresponse, // and the msg/sz is return by skynet.rawcall , so don't free(msg) - void * msg = lua_touserdata(L,2); - size_t sz = luaL_checkunsigned(L, 3); + int ok = lua_toboolean(L,2); + void * msg; + size_t sz; + + if (lua_type(L,3) == LUA_TSTRING) { + msg = (void *)lua_tolstring(L, 3, &sz); + if (sz > 0x1000) { + sz = 0x1000; + } + } else { + msg = lua_touserdata(L,3); + sz = luaL_checkunsigned(L, 4); + } uint8_t buf[TEMP_LENGTH]; - fill_header(L, buf, sz+4, msg); + fill_header(L, buf, sz+5, msg); fill_uint32(buf+2, session); - memcpy(buf+6,msg,sz); + buf[6] = ok; + memcpy(buf+7,msg,sz); - lua_pushlstring(L, (const char *)buf, sz+6); + lua_pushlstring(L, (const char *)buf, sz+7); return 1; } @@ -178,13 +191,13 @@ static int lunpackresponse(lua_State *L) { size_t sz; const char * buf = luaL_checklstring(L, 1, &sz); - if (sz < 4) { + if (sz < 5) { return 0; } uint32_t session = unpack_uint32((const uint8_t *)buf); lua_pushunsigned(L, session); - lua_pushboolean(L, 1); - lua_pushlstring(L, buf+4, sz-4); + lua_pushboolean(L, buf[4]); + lua_pushlstring(L, buf+5, sz-5); return 3; } diff --git a/lualib/cluster.lua b/lualib/cluster.lua index bc4312b3..d53e6d9b 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -16,6 +16,10 @@ function cluster.open(port) end end +function cluster.reload() + skynet.call(clusterd, "lua", "reload") +end + skynet.init(function() clusterd = skynet.uniqueservice("clusterd") end) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 7eba476c..d9c93616 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -325,6 +325,9 @@ end function skynet.rawcall(addr, typename, msg, sz) local p = proto[typename] + if watching_service[addr] == false then + error("Service is dead") + end local session = assert(c.send(addr, p.id , nil , msg, sz), "call to invalid address") return yield_call(addr, session) end diff --git a/service/clusterd.lua b/service/clusterd.lua index 405e28ab..039f7b77 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -5,7 +5,14 @@ local cluster = require "cluster.c" local config_name = skynet.getenv "cluster" local node_address = {} -assert(loadfile(config_name, "t", node_address))() + +local function loadconfig() + local f = assert(io.open(config_name)) + local source = f:read "*a" + f:close() + assert(load(source, "@"..config_name, "t", node_address))() +end + local node_session = {} local command = {} @@ -30,6 +37,11 @@ end local node_channel = setmetatable({}, { __index = open_channel }) +function command.reload() + loadconfig() + skynet.ret(skynet.pack(nil)) +end + function command.listen(source, addr, port) local gate = skynet.newservice("gate") if port == nil then @@ -45,6 +57,7 @@ function command.req(source, node, addr, msg, sz) local session = node_session[node] -- msg is a local pointer, cluster.packrequest will free it request, node_session[node] = cluster.packrequest(addr, session , msg, sz) + local ok, result = pcall(c.request, c, request, session) skynet.ret(c:request(request, session)) end @@ -53,8 +66,13 @@ local request_fd = {} function command.socket(source, subcmd, fd, msg) if subcmd == "data" then local addr, session, msg = cluster.unpackrequest(msg) - local msg, sz = skynet.rawcall(addr, "lua", msg) - local response = cluster.packresponse(session, msg, sz) + local ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg) + local response + if ok then + response = cluster.packresponse(session, true, msg, sz) + else + response = cluster.packresponse(session, false, msg) + end socket.write(fd, response) elseif subcmd == "open" then skynet.error(string.format("socket accept from %s", msg)) @@ -65,7 +83,8 @@ function command.socket(source, subcmd, fd, msg) end skynet.start(function() - skynet.dispatch("lua", function(_, source, cmd, ...) + loadconfig() + skynet.dispatch("lua", function(session , source, cmd, ...) local f = assert(command[cmd]) f(source, ...) end) From 7b5e62b8960715ac4fafcf627242ae48db6dc95e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 12 Jul 2014 23:24:29 +0800 Subject: [PATCH 079/729] fix typo --- service/clusterd.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 039f7b77..f7a9e90f 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -57,7 +57,6 @@ function command.req(source, node, addr, msg, sz) local session = node_session[node] -- msg is a local pointer, cluster.packrequest will free it request, node_session[node] = cluster.packrequest(addr, session , msg, sz) - local ok, result = pcall(c.request, c, request, session) skynet.ret(c:request(request, session)) end From 4284cfc37206735079d2f733825e05b2b917c039 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 10:32:21 +0800 Subject: [PATCH 080/729] loginserver and example --- examples/config.login | 8 +++ examples/login/client.lua | 59 +++++++++++++++++ examples/login/logind.lua | 25 +++++++ examples/login/main.lua | 5 ++ lualib/loginserver.lua | 133 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 230 insertions(+) create mode 100644 examples/config.login create mode 100644 examples/login/client.lua create mode 100644 examples/login/logind.lua create mode 100644 examples/login/main.lua create mode 100644 lualib/loginserver.lua diff --git a/examples/config.login b/examples/config.login new file mode 100644 index 00000000..c72dcb27 --- /dev/null +++ b/examples/config.login @@ -0,0 +1,8 @@ +thread = 8 +logger = nil +harbor = 0 +start = "main" +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = "./service/?.lua;./examples/login/?.lua" +lualoader = "lualib/loader.lua" +cpath = "./cservice/?.so" diff --git a/examples/login/client.lua b/examples/login/client.lua new file mode 100644 index 00000000..36ee3e2a --- /dev/null +++ b/examples/login/client.lua @@ -0,0 +1,59 @@ +package.cpath = "luaclib/?.so" + +local socket = require "clientsocket" +local cjson = require "cjson" +local crypt = require "crypt" + +local last +local fd = assert(socket.connect("127.0.0.1", 8001)) +local input = {} + +local function readline() + local line = table.remove(input, 1) + if line then + return line + end + + while true do + local status + status, last = socket.readline(fd, last, input) + if status == nil then + error "Server closed" + end + if not status then + socket.usleep(100) + else + local line = table.remove(input, 1) + if line then + return line + end + end + end +end + +local challenge = crypt.base64decode(readline()) + +local clientkey = crypt.randomkey() +socket.writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey))) +local secret = crypt.dhsecret(crypt.base64decode(readline()), clientkey) + +print("sceret is ", crypt.hexencode(secret)) + +local hmac = crypt.hmac64(challenge, secret) +socket.writeline(fd, crypt.base64encode(hmac)) + +local token = { + user = "hello", + pass = "password", +} + +local etoken = crypt.desencode(secret, cjson.encode(token)) +local b = crypt.base64encode(etoken) +socket.writeline(fd, crypt.base64encode(etoken)) + +print(readline()) + + + + + diff --git a/examples/login/logind.lua b/examples/login/logind.lua new file mode 100644 index 00000000..3c8a3835 --- /dev/null +++ b/examples/login/logind.lua @@ -0,0 +1,25 @@ +local login = require "loginserver" +local json = require "cjson" +local crypt = require "crypt" + +local server = { + host = "127.0.0.1", + port = 8001, +} + +function server.auth_handler(token) + token = json.decode(token) + assert(token.user) + assert(token.pass == "password") + return "sample", token.user +end + +function server.login_handler(server, uid, secret) + print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret))) +end + +function server.logout_handler(server, uid) + print(string.format("%s@%s is logout", uid, server)) +end + +login(server) diff --git a/examples/login/main.lua b/examples/login/main.lua new file mode 100644 index 00000000..493b2af4 --- /dev/null +++ b/examples/login/main.lua @@ -0,0 +1,5 @@ +local skynet = require "skynet" + +skynet.start(function() + skynet.newservice "logind" +end) diff --git a/lualib/loginserver.lua b/lualib/loginserver.lua new file mode 100644 index 00000000..658ae1cd --- /dev/null +++ b/lualib/loginserver.lua @@ -0,0 +1,133 @@ +local skynet = require "skynet" +local socket = require "socket" +local crypt = require "crypt" +local datacenter = require "datacenter" + +local function launch_slave(auth_handler) + local cmd = {} + + -- set socket buffer limit (8K) + -- If the attacker send large package, close the socket + socket.limit(8192) + + function cmd.auth(fd, addr) + skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) + socket.start(fd) + local challenge = crypt.randomkey() + socket.write(fd, crypt.base64encode(challenge).."\n") + + local handshake = assert(socket.readline(fd), "socket closed") + local clientkey = crypt.base64decode(handshake) + if #clientkey ~= 8 then + error "Invalid client key" + end + local serverkey = crypt.randomkey() + socket.write(fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n") + + local secret = crypt.dhsecret(clientkey, serverkey) + + local response = assert(socket.readline(fd), "socket closed") + local hmac = crypt.hmac64(challenge, secret) + + if hmac ~= crypt.base64decode(response) then + socket.write(fd, "400 Bad Request\n") + error "challenge failed" + end + + local etoken = assert(socket.readline(fd), "socket closed") + + local token = crypt.desdecode(secret, crypt.base64decode(etoken)) + + local ok, server, uid = pcall(auth_handler,token) + + socket.abandon(fd) + return ok, server, uid, secret + end + + skynet.dispatch("lua", function(_,_,command,...) + local f = assert(cmd[command]) + skynet.ret(skynet.pack(f(...))) + end) +end + +local function accept(conf, s, fd, addr) + local ok, server, uid, secret = skynet.call(s, "lua", "auth", fd, addr) + if not ok then + socket.write(fd, "401 Unauthorized\n") + error(server) + end + + socket.start(fd) + + local ok, err = pcall(conf.login_handler, server, uid, secret) + if ok then + socket.write(fd, "200 OK\n") + else + socket.write(fd, "406 Not Acceptable\n") + error(err) + end +end + +local function launch_master(conf) + local instance = conf.instance or 8 + assert(instance > 0) + local host = conf.host or "0.0.0.0" + local port = assert(tonumber(conf.port)) + local slave = {} + local balance = 1 + local cmd = {} + + function cmd.logout(server, uid) + conf.logout_handler(server, uid) + end + + if conf.init then + conf.init() + conf.init = nil + end + + for i=1,instance do + slave[i] = skynet.newservice(SERVICE_NAME) + end + + skynet.dispatch("lua", function(_,_,command, ...) + local f = assert(cmd[command]) + skynet.ret(skynet.pack(f(...))) + end) + + local id = socket.listen(host, port) + skynet.error(string.format("login server listen at : %s %d", host, port)) + socket.start(id , function(fd, addr) + local s = slave[balance] + balance = balance + 1 + if balance > instance then + balance = 1 + end + local ok, err = pcall(accept, conf, s, fd, addr) + if not ok then + skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err)) + end + socket.close(fd) + end) +end + +local function login (conf) + skynet.start(function() + local loginmaster = datacenter.get "login_master" + if loginmaster then + local auth_handler = assert(conf.auth_handler) + launch_master = nil + conf = nil + launch_slave(auth_handler) + else + launch_slave = nil + conf.auth_handler = nil + assert(conf.login_handler) + assert(conf.logout_handler) + datacenter.set("login_master", skynet.self()) + launch_master(conf) + end + end) +end + +return login From 04cb72d1a882475161acce7d23acef250a5550a7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 11:50:30 +0800 Subject: [PATCH 081/729] config.name --- examples/login/logind.lua | 1 + lualib/loginserver.lua | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/login/logind.lua b/examples/login/logind.lua index 3c8a3835..39a60186 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -5,6 +5,7 @@ local crypt = require "crypt" local server = { host = "127.0.0.1", port = 8001, + name = "login_master", } function server.auth_handler(token) diff --git a/lualib/loginserver.lua b/lualib/loginserver.lua index 658ae1cd..1fff5a66 100644 --- a/lualib/loginserver.lua +++ b/lualib/loginserver.lua @@ -112,8 +112,9 @@ local function launch_master(conf) end local function login (conf) + local name = conf.name or "login_master" skynet.start(function() - local loginmaster = datacenter.get "login_master" + local loginmaster = datacenter.get(name) if loginmaster then local auth_handler = assert(conf.auth_handler) launch_master = nil From 6813fd9ef79dd9a922ddc08d88173a2372406f8a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 11:55:25 +0800 Subject: [PATCH 082/729] bugfix: skynet.exit redirect error, and add datacenter.wait --- lualib/datacenter.lua | 4 ++ lualib/skynet.lua | 2 +- service/datacenterd.lua | 81 ++++++++++++++++++++++++++++++++++++++--- test/testdatacenter.lua | 23 ++++++++++++ test/testdeadcall.lua | 4 +- 5 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 test/testdatacenter.lua diff --git a/lualib/datacenter.lua b/lualib/datacenter.lua index 347c140e..ebf68ba1 100644 --- a/lualib/datacenter.lua +++ b/lualib/datacenter.lua @@ -10,5 +10,9 @@ function datacenter.set(...) return skynet.call("DATACENTER", "lua", "UPDATE", ...) end +function datacenter.wait(...) + return skynet.call("DATACENTER", "lua", "WAIT", ...) +end + return datacenter diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d9c93616..6ccdaea2 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -257,7 +257,7 @@ function skynet.exit() local address = session_coroutine_address[co] local self = skynet.self() if session~=0 and address then - skynet.redirect(self, address, "error", session, "") + skynet.redirect(address, self, "error", session, "") end end c.command("EXIT") diff --git a/service/datacenterd.lua b/service/datacenterd.lua index ad431ae2..1a0fee65 100644 --- a/service/datacenterd.lua +++ b/service/datacenterd.lua @@ -2,6 +2,8 @@ local skynet = require "skynet" local command = {} local database = {} +local wait_queue = {} +local mode = {} local function query(db, key, ...) if key == nil then @@ -22,7 +24,7 @@ local function update(db, key, value, ...) if select("#",...) == 0 then local ret = db[key] db[key] = value - return ret + return ret, value else if db[key] == nil then db[key] = {} @@ -31,13 +33,82 @@ local function update(db, key, value, ...) end end +local function wakeup(db, key1, key2, value, ...) + if key1 == nil then + return + end + local q = db[key1] + if q == nil then + return + end + if q[mode] == "queue" then + db[key1] = nil + if value then + -- throw error because can't wake up a branch + for _,v in ipairs(q) do + local session = v[1] + local source = v[2] + skynet.redirect(source, 0, "error", session, "") + end + else + return q + end + else + -- it's branch + return wakeup(q , key2, value, ...) + end +end + function command.UPDATE(...) - return update(database, ...) + local ret, value = update(database, ...) + if ret or value == nil then + return ret + end + local q = wakeup(wait_queue, ...) + if q then + for _, v in ipairs(q) do + local session = v[1] + local source = v[2] + skynet.redirect(source, 0, "response", session, skynet.pack(value)) + end + end +end + +local function waitfor(session, source, db, key1, key2, ...) + if key2 == nil then + -- push queue + local q = db[key1] + if q == nil then + q = { [mode] = "queue" } + db[key1] = q + else + assert(q[mode] == "queue") + end + table.insert(q, { session, source }) + else + local q = db[key1] + if q == nil then + q = { [mode] = "branch" } + db[key1] = q + else + assert(q[mode] == "branch") + end + return waitfor(session, source, q, key2, ...) + end end skynet.start(function() - skynet.dispatch("lua", function (_, source, cmd, ...) - local f = assert(command[cmd]) - skynet.ret(skynet.pack(f(...))) + skynet.dispatch("lua", function (session, source, cmd, ...) + if cmd == "WAIT" then + local ret = command.QUERY(...) + if ret then + skynet.ret(skynet.pack(ret)) + else + waitfor(session, source, wait_queue, ...) + end + else + local f = assert(command[cmd]) + skynet.ret(skynet.pack(f(...))) + end end) end) diff --git a/test/testdatacenter.lua b/test/testdatacenter.lua new file mode 100644 index 00000000..194d2219 --- /dev/null +++ b/test/testdatacenter.lua @@ -0,0 +1,23 @@ +local skynet = require "skynet" +local datacenter = require "datacenter" + +local function f1() + print("====1==== wait hello") + print("\t1>",datacenter.wait ("hello")) + print("====1==== wait key.foobar") + print("\t1>", pcall(datacenter.wait,"key")) -- will failed, because "key" is a branch + print("\t1>",datacenter.wait ("key", "foobar")) +end + +local function f2() + skynet.sleep(10) + print("====2==== set key.foobar") + datacenter.set("key", "foobar", "bingo") +end + +skynet.start(function() + datacenter.set("hello", "world") + print(datacenter.get "hello") + skynet.fork(f1) + skynet.fork(f2) +end) diff --git a/test/testdeadcall.lua b/test/testdeadcall.lua index c5d67b8c..9017d380 100644 --- a/test/testdeadcall.lua +++ b/test/testdeadcall.lua @@ -17,9 +17,7 @@ else local test = skynet.newservice(SERVICE_NAME, "test") -- launch self in test mode print(pcall(function() - skynet.send(test,"lua", "hello world") - skynet.send(test,"lua", "never get there") - skynet.call(test,"lua", "fake call") + skynet.call(test,"lua", "dead call") end)) skynet.exit() From 2e35f405e75a60136e9b7d23adc3c4d9ff29afff Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 11:56:28 +0800 Subject: [PATCH 083/729] update history --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index 52e1d4b0..47b62280 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -8,6 +8,7 @@ Dev version * cluster can throw error * Add readline and writeline to clientsocket lib * Add cluster.reload to reload config file +* Add datacenter.wait v0.4.1 (2014-7-7) ----------- From e1674f04c3364b6865b8774335b7dc638ca648aa Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 14:54:53 +0800 Subject: [PATCH 084/729] add gateserver --- examples/main.lua | 1 + examples/watchdog.lua | 1 - service/gate.lua | 170 ++++++++++++++---------------------------- 3 files changed, 58 insertions(+), 114 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index abbd2751..4a98d2eb 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -11,6 +11,7 @@ skynet.start(function() skynet.call(watchdog, "lua", "start", { port = 8888, maxclient = max_client, + nodelay = true, }) print("Watchdog listen on ", 8888) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index e28ab9fb..8ead2253 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -33,7 +33,6 @@ function SOCKET.data(fd, msg) end function CMD.start(conf) - skynet.call(gate, "lua", "nodelay", true) skynet.call(gate, "lua", "open" , conf) end diff --git a/service/gate.lua b/service/gate.lua index 772f135a..7758175f 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -1,39 +1,41 @@ local skynet = require "skynet" +local gateserver = require "gateserver" local netpack = require "netpack" -local socketdriver = require "socketdriver" -local socket -local queue local watchdog -local maxclient -local client_number = 0 -local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end }) -local nodelay = false - local connection = {} -- fd -> connection : { fd , client, agent , ip, mode } local forwarding = {} -- agent -> connection -function CMD.open( source , conf ) - assert(not socket) - local address = conf.address or "0.0.0.0" - local port = assert(conf.port) - maxclient = conf.maxclient or 1024 +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, +} + +local handler = {} + +function handler.open(source, conf) watchdog = conf.watchdog or source - socket = socketdriver.listen(address, port) - socketdriver.start(socket) + maxclient = conf.maxclient or 1024 end -function CMD.nodelay(source, v) - if v ~= false then - v = true +function handler.message(fd, msg, sz) + -- recv a package, forward it + local c = connection[fd] + local agent = c.agent + if agent then + skynet.redirect(agent, c.client, "client", 0, msg, sz) + else + skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) end - nodelay = v end -function CMD.close() - assert(socket) - socketdriver.close(socket) - socket = nil +function handler.connect(fd, addr) + local c = { + fd = fd, + ip = msg, + } + connection[fd] = c + skynet.send(watchdog, "lua", "socket", "open", fd, addr) end local function unforward(c) @@ -44,28 +46,47 @@ local function unforward(c) end end -local function start(c) - if not c.mode then - c.mode = "open" - socketdriver.start(c.fd) +local function close_fd(fd) + local c = connection[fd] + if c then + unforward(c) + connection[fd] = nil + end +end + +function handler.close(fd) + close_fd(fd) + skynet.send(watchdog, "lua", "socket", "close", fd) +end + +function handler.error(fd, msg) + close_fd(fd) + skynet.send(watchdog, "lua", "socket", "error", fd, msg) +end + +local CMD = {} + +local function unforward(c) + if c.agent then + forwarding[c.agent] = nil + c.agent = nil + c.client = nil end end function CMD.forward(source, fd, client, address) local c = assert(connection[fd]) unforward(c) - start(c) - c.client = client or 0 c.agent = address or source - forwarding[c.agent] = c + gateserver.openclient(fd) end function CMD.accept(source, fd) local c = assert(connection[fd]) unforward(c) - start(c) + gateserver.openclient(fd) end function CMD.kick(source, fd) @@ -78,89 +99,12 @@ function CMD.kick(source, fd) assert(c) - if c.mode ~= "close" then - c.mode = "close" - socketdriver.close(c.fd) - end + gateserver.closeclient(fd) end -local MSG = {} - -function MSG.data(fd, msg, sz) - -- recv a package, forward it - local c = connection[fd] - local agent = c.agent - if agent then - skynet.redirect(agent, c.client, "client", 0, msg, sz) - else - skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) - end +function handler.command(cmd, source, ...) + local f = assert(CMD[cmd]) + return f(source, ...) end -function MSG.more() - for fd, msg, sz in netpack.pop, queue do - MSG.data(fd, msg, sz) - end -end - -function MSG.open(fd, msg) - if client_number >= maxclient then - socketdriver.close(fd) - return - end - local c = { - fd = fd, - ip = msg, - } - connection[fd] = c - client_number = client_number + 1 - if nodelay then - socketdriver.nodelay(fd) - end - skynet.send(watchdog, "lua", "socket", "open", fd, msg) -end - -local function close_fd(fd, message) - local c = connection[fd] - if c then - unforward(c) - connection[fd] = nil - client_number = client_number - 1 - end -end - -function MSG.close(fd) - close_fd(fd) - skynet.send(watchdog, "lua", "socket", "close", fd) -end - -function MSG.error(fd, msg) - close_fd(fd) - skynet.send(watchdog, "lua", "socket", "error", fd, msg) -end - -skynet.register_protocol { - name = "socket", - id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 - unpack = function ( msg, sz ) - return netpack.filter( queue, msg, sz) - end, - dispatch = function (_, _, q, type, ...) - queue = q - if type then - MSG[type](...) - end - end -} - -skynet.register_protocol { - name = "client", - id = skynet.PTYPE_CLIENT, -} - -skynet.start(function() - skynet.dispatch("lua", function (_, address, cmd, ...) - local f = assert(CMD[cmd]) - skynet.ret(skynet.pack(f(address, ...))) - end) -end) +gateserver.start(handler) From 0cdd71c0c202e4e0994ce5e28354dc5ae91555b9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 19:36:44 +0800 Subject: [PATCH 085/729] msggate example --- examples/login/client.lua | 57 +++++++++++ examples/login/logind.lua | 26 ++++- examples/login/main.lua | 9 +- examples/login/msgagent.lua | 27 ++++++ examples/login/msggate.lua | 187 ++++++++++++++++++++++++++++++++++++ lualib/gateserver.lua | 130 +++++++++++++++++++++++++ lualib/loginserver.lua | 36 +++---- service/gate.lua | 1 - 8 files changed, 448 insertions(+), 25 deletions(-) create mode 100644 examples/login/msgagent.lua create mode 100644 examples/login/msggate.lua create mode 100644 lualib/gateserver.lua diff --git a/examples/login/client.lua b/examples/login/client.lua index 36ee3e2a..85a8ab36 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -1,5 +1,18 @@ package.cpath = "luaclib/?.so" +--[[ Status code + +200 OK + +400 Bad Request (通常是登陆å议错误) +401 Unauthorized (通常是登陆æœåŠ¡å™¨æˆ–æ¸¸æˆæœåŠ¡å™¨éªŒè¯é”™è¯¯) +403 Forbidden (é€šå¸¸æ˜¯è¿žæŽ¥æ¸¸æˆæœåŠ¡å™¨çš„ index å·²ç»è¿‡æœŸ) +404 Not Found (é€šå¸¸æ˜¯æ¸¸æˆæœåŠ¡å™¨æœªèŽ·å¾—ç™»é™†æœåŠ¡å™¨çš„é€šçŸ¥) +406 Not Acceptable (通常是登陆æœåŠ¡å™¨è½¬å‘æ¸¸æˆæœåŠ¡å™¨æ‹’ç»ç™»é™†) +412 Precondition Failed (é€šå¸¸æ˜¯é—æ¼äº†å’Œæ¸¸æˆæœåС噍剿¬¡é€šè®¯çš„请求) + +]] + local socket = require "clientsocket" local cjson = require "cjson" local crypt = require "crypt" @@ -43,6 +56,7 @@ local hmac = crypt.hmac64(challenge, secret) socket.writeline(fd, crypt.base64encode(hmac)) local token = { + server = "sample", user = "hello", pass = "password", } @@ -53,6 +67,49 @@ socket.writeline(fd, crypt.base64encode(etoken)) print(readline()) +socket.close(fd) + +----- connect to game server + +local input = {} +local fd = assert(socket.connect("127.0.0.1", 8888)) + +local function readpackage() + local line = table.remove(input, 1) + if line then + return line + end + + while true do + local status + status, last = socket.recv(fd, last, input) + if status == nil then + error "Server closed" + end + if not status then + socket.usleep(100) + else + local line = table.remove(input, 1) + if line then + return line + end + end + end +end + +local index = 0 +local request = 0 +local handshake = string.format("%s@%s:%d:%d", crypt.base64encode(token.user), crypt.base64encode(token.server) , index, request) +local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) + +socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) + +print(readpackage()) + +socket.send(fd , "echo") +print(readpackage()) + + diff --git a/examples/login/logind.lua b/examples/login/logind.lua index 39a60186..759a046e 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -1,6 +1,7 @@ local login = require "loginserver" local json = require "cjson" local crypt = require "crypt" +local skynet = require "skynet" local server = { host = "127.0.0.1", @@ -8,19 +9,40 @@ local server = { name = "login_master", } +local server_list = {} +local user_online = {} + function server.auth_handler(token) token = json.decode(token) assert(token.user) assert(token.pass == "password") - return "sample", token.user + return token.server, token.user end function server.login_handler(server, uid, secret) print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret))) + local u = user_online[uid] + if u then + local gameserver = server_list[u.server] + skynet.call(gameserver, "lua", "kick", server, uid) + end + local gameserver = assert(server_list[server]) + skynet.call(gameserver, "lua", "login", server, uid, secret) end -function server.logout_handler(server, uid) +local CMD = {} + +function CMD.register_gate(source, name) + server_list[name] = source +end + +function CMD.logout(source, uid, server) print(string.format("%s@%s is logout", uid, server)) end +function server.command_handler(command, source, ...) + local f = assert(CMD[command]) + return f(source, ...) +end + login(server) diff --git a/examples/login/main.lua b/examples/login/main.lua index 493b2af4..6359cb96 100644 --- a/examples/login/main.lua +++ b/examples/login/main.lua @@ -1,5 +1,12 @@ local skynet = require "skynet" skynet.start(function() - skynet.newservice "logind" + local loginserver = skynet.newservice "logind" + local gate = skynet.newservice "msggate" + skynet.call(gate, "lua", "open" , { + port = 8888, + maxclient = 64, + loginserver = loginserver, + servername = "sample", + }) end) diff --git a/examples/login/msgagent.lua b/examples/login/msgagent.lua new file mode 100644 index 00000000..30545c68 --- /dev/null +++ b/examples/login/msgagent.lua @@ -0,0 +1,27 @@ +local skynet = require "skynet" + +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, + unpack = skynet.tostring, +} + +local gate + +local CMD = {} + +function CMD.init(source , uid, server) + gate = source + skynet.error(string.format("%s is coming", uid)) +end + +skynet.start(function() + skynet.dispatch("lua", function(_, source, command, ...) + local f = assert(CMD[command]) + skynet.ret(skynet.pack(f(source, ...))) + end) + + skynet.dispatch("client", function(_,_, msg) + skynet.ret(msg) + end) +end) diff --git a/examples/login/msggate.lua b/examples/login/msggate.lua new file mode 100644 index 00000000..cdda670a --- /dev/null +++ b/examples/login/msggate.lua @@ -0,0 +1,187 @@ +local skynet = require "skynet" +local gateserver = require "gateserver" +local netpack = require "netpack" +local crypt = require "crypt" +local socketdriver = require "socketdriver" +local datacenter = require "datacenter" + +local users = {} + +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, +} + +local handler = {} +local handshake = {} +local connection = {} +local agent = {} +local login_master + +-- launch an agent service to handle message from client +local function launch_agent(c) + local agent = assert(skynet.newservice "msgagent") + skynet.call(agent, "lua", "init", c.uid, c.server) + return agent +end + +function handler.connect(fd, addr) + skynet.error(string.format("new connection from %s (fd=%d)", addr, fd)) + handshake[fd] = true + gateserver.openclient(fd) +end + +local function auth(fd, msg, sz) +-- base64(base64(uid)@base64(server):index:request:base64(hmac) + local message = netpack.tostring(msg, sz) + local username, index, request , hmac = string.match(message, "([^:]*):([^:]*):([^:]*):([^:]*)") + local content = users[username] + if content == nil then + return "404 Not Found" + end + local idx = assert(tonumber(index)) + local req = assert(tonumber(request)) + hmac = crypt.base64decode(hmac) + + if idx < content.index then + return "403 Forbidden" + end + if req > content.request or req < content.reserved then + return "412 Precondition Failed" + end + + local text = string.format("%s:%s:%s", username, index, request) + local v = crypt.hmac64(crypt.hashkey(text), content.secret) + if v ~= hmac then + return "401 Unauthorized" + end + + content.index = idx + for i=content.reserved, request do + content.response[i] = nil + end + content.reserved = request + content.reqest = request + connection[fd] = content + + if content.fd then + local last_fd = content.fd + connection[last_fd] = nil + gateserver.closeclient(last_fd) + end + content.fd = fd + + if content.agent == nil then + content.agent = true + local ok, agent = pcall(launch_agent, content) + if ok then + content.agent = agent + else + content.agent = nil + skynet.error(string.format("Launch agent %s failed : %s", content.uid, agent)) + connection[fd] = nil + gateserver.closeclient(fd) + end + end +end + +function handler.message(fd, msg, sz) + if handshake[fd] then + local ok, result = pcall(auth, fd, msg, sz) + if not ok then + result = "400 Bad Request" + end + + local close = result ~= nil + + if result == nil then + result = "200 OK" + end + + socketdriver.send(fd, netpack.pack(result)) + + if close then + gateserver.closeclient(fd) + end + handshake[fd] = nil + else + local c = connection[fd] + if c == nil or c.agent == nil then + local message = netpack.tostring(msg, sz) + skynet.error(string.format("Unknown fd = %d, message (%s) size = %d", fd, crypt.hexencode(message):sub(1,80), #message)) + return + end + c.request = c.request + 1 + local ret = c.response[c.request] + if ret == nil then + local ok, msg, sz = pcall(skynet.rawcall, c.agent, "client", msg, sz) + if ok then + ret = netpack.pack_string(msg, sz) + else + skynet.error(string.format("%s request error : %s", c.username, msg)) + ret = netpack.pack_string "" + end + c.response[c.request] = ret + end + socketdriver.send(c.fd, ret) + end +end + +function handler.close(fd) + handshake[fd] = nil + local c = connection[fd] + if c then + c.fd = nil + end +end + +handler.error = handler.close + +function handler.open(source, conf) + login_master = assert(conf.loginserver) + local servername = assert(conf.servername) + skynet.call(login_master, "lua", "register_gate", servername) +end + + +local CMD = {} + +function CMD.login(source, server, uid, secret) + local username = crypt.base64encode(uid) .. '@' .. crypt.base64encode(server) + users[username] = { + server = server, + uid = uid, + secret = secret, + index = 0, + request = 0, + reserved = 0, + response = {}, + } +end + +function CMD.logout(source) + local c = agent[source] + if c then + skynet.call(login_master, "lua", "logout", c.server, c.uid) + if c.fd then + gateserver.closeclient(c.fd) + end + end +end + +function CMD.kick(source, server, uid) + local username = crypt.base64encode(uid) .. '@' .. crypt.base64encode(server) + local u = users[username] + if u and u.agent then + skynet.call(u.agent, "logout") + skynet.kill(u.agent) + u.agent = nil + end +end + +function handler.command(cmd, source, ...) + local f = assert(CMD[cmd]) + return f(source, ...) +end + +gateserver.start(handler) diff --git a/lualib/gateserver.lua b/lualib/gateserver.lua new file mode 100644 index 00000000..84f33f88 --- /dev/null +++ b/lualib/gateserver.lua @@ -0,0 +1,130 @@ +local skynet = require "skynet" +local netpack = require "netpack" +local socketdriver = require "socketdriver" + +local gateserver = {} + +local socket -- listen socket +local queue -- message queue +local maxclient -- max client +local client_number = 0 +local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end }) +local nodelay = false + +local connection = {} + +function gateserver.openclient(fd) + if connection[fd] then + socketdriver.start(fd) + end +end + +function gateserver.closeclient(fd) + local c = connection[fd] + if c then + connection[fd] = false + socketdriver.close(fd) + end +end + +function gateserver.start(handler) + assert(handler.message) + assert(handler.connect) + + function CMD.open( source, conf ) + assert(not socket) + local address = conf.address or "0.0.0.0" + local port = assert(conf.port) + maxclient = conf.maxclient or 1024 + nodelay = conf.nodelay + socket = socketdriver.listen(address, port) + socketdriver.start(socket) + if handler.open then + handler.open(source, conf) + end + end + + function CMD.close() + assert(socket) + socketdriver.close(socket) + socket = nil + end + + local MSG = {} + + function MSG.data(fd, msg, sz) + if connection[fd] then + handler.message(fd, msg, sz) + end + end + + function MSG.more() + for fd, msg, sz in netpack.pop, queue do + if connection[fd] then + handler.message(fd, msg, sz) + end + end + end + + function MSG.open(fd, msg) + if client_number >= maxclient then + socketdriver.close(fd) + return + end + if nodelay then + socketdriver.nodelay(fd) + end + connection[fd] = true + client_number = client_number + 1 + handler.connect(fd, msg) + end + + local function close_fd(fd) + local c = connection[fd] + if c ~= nil then + connection[fd] = nil + client_number = client_number - 1 + end + end + + function MSG.close(fd) + if handler.close then + handler.close(fd) + end + close_fd(fd) + end + + function MSG.error(fd, msg) + if handler.error then + handler.error(fd) + end + close_fd(fd) + end + + skynet.register_protocol { + name = "socket", + id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 + unpack = function ( msg, sz ) + return netpack.filter( queue, msg, sz) + end, + dispatch = function (_, _, q, type, ...) + queue = q + if type then + MSG[type](...) + end + end + } + + skynet.start(function() + skynet.dispatch("lua", function (_, address, cmd, ...) + local f = CMD[cmd] + if f then + skynet.ret(skynet.pack(f(address, ...))) + else + skynet.ret(skynet.pack(handler.command(cmd, address, ...))) + end + end) + end) +end + +return gateserver \ No newline at end of file diff --git a/lualib/loginserver.lua b/lualib/loginserver.lua index 1fff5a66..7f022508 100644 --- a/lualib/loginserver.lua +++ b/lualib/loginserver.lua @@ -1,7 +1,6 @@ local skynet = require "skynet" local socket = require "socket" local crypt = require "crypt" -local datacenter = require "datacenter" local function launch_slave(auth_handler) local cmd = {} @@ -75,32 +74,26 @@ local function launch_master(conf) local port = assert(tonumber(conf.port)) local slave = {} local balance = 1 - local cmd = {} - function cmd.logout(server, uid) - conf.logout_handler(server, uid) - end - - if conf.init then - conf.init() - conf.init = nil - end + skynet.dispatch("lua", function(_,source,command, ...) + if command == "register_slave" then + table.insert(slave, source) + skynet.ret(skynet.pack(nil)) + else + skynet.ret(skynet.pack(conf.command_handler(command, source, ...))) + end + end) for i=1,instance do - slave[i] = skynet.newservice(SERVICE_NAME) + skynet.newservice(SERVICE_NAME) end - skynet.dispatch("lua", function(_,_,command, ...) - local f = assert(cmd[command]) - skynet.ret(skynet.pack(f(...))) - end) - local id = socket.listen(host, port) skynet.error(string.format("login server listen at : %s %d", host, port)) socket.start(id , function(fd, addr) local s = slave[balance] balance = balance + 1 - if balance > instance then + if balance > #slave then balance = 1 end local ok, err = pcall(accept, conf, s, fd, addr) @@ -112,10 +105,11 @@ local function launch_master(conf) end local function login (conf) - local name = conf.name or "login_master" + local name = "." .. (conf.name or "login") skynet.start(function() - local loginmaster = datacenter.get(name) + local loginmaster = skynet.localname(name) if loginmaster then + skynet.call(loginmaster, "lua", "register_slave") local auth_handler = assert(conf.auth_handler) launch_master = nil conf = nil @@ -124,8 +118,8 @@ local function login (conf) launch_slave = nil conf.auth_handler = nil assert(conf.login_handler) - assert(conf.logout_handler) - datacenter.set("login_master", skynet.self()) + assert(conf.command_handler) + skynet.register(name) launch_master(conf) end end) diff --git a/service/gate.lua b/service/gate.lua index 7758175f..665be9d0 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -15,7 +15,6 @@ local handler = {} function handler.open(source, conf) watchdog = conf.watchdog or source - maxclient = conf.maxclient or 1024 end function handler.message(fd, msg, sz) From 3bc73046090bf9c7a624fc1660896127db81928c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 19:37:08 +0800 Subject: [PATCH 086/729] skynet.localname return nil when the name not exist --- lualib/skynet.lua | 5 ++++- skynet-src/skynet_server.c | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 6ccdaea2..2b984e2a 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -229,7 +229,10 @@ function skynet.self() end function skynet.localname(name) - return string_to_handle(c.command("QUERY", name)) + local addr = c.command("QUERY", name) + if addr then + return string_to_handle(addr) + end end function skynet.launch(...) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 8f619ad4..3e902cc9 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -368,8 +368,10 @@ static const char * cmd_query(struct skynet_context * context, const char * param) { if (param[0] == '.') { uint32_t handle = skynet_handle_findname(param+1); - sprintf(context->result, ":%x", handle); - return context->result; + if (handle) { + sprintf(context->result, ":%x", handle); + return context->result; + } } return NULL; } From 3611bfe1afa6afe90342ceee7e767349fdf51986 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 19:44:23 +0800 Subject: [PATCH 087/729] create gamefw --- examples/login/logind.lua | 2 +- examples/login/msggate.lua | 2 +- lualib/{ => gamefw}/gateserver.lua | 0 lualib/{ => gamefw}/loginserver.lua | 0 service/gate.lua | 2 +- 5 files changed, 3 insertions(+), 3 deletions(-) rename lualib/{ => gamefw}/gateserver.lua (100%) rename lualib/{ => gamefw}/loginserver.lua (100%) diff --git a/examples/login/logind.lua b/examples/login/logind.lua index 759a046e..2ff79c48 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -1,4 +1,4 @@ -local login = require "loginserver" +local login = require "gamefw.loginserver" local json = require "cjson" local crypt = require "crypt" local skynet = require "skynet" diff --git a/examples/login/msggate.lua b/examples/login/msggate.lua index cdda670a..85c3d4af 100644 --- a/examples/login/msggate.lua +++ b/examples/login/msggate.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local gateserver = require "gateserver" +local gateserver = require "gamefw.gateserver" local netpack = require "netpack" local crypt = require "crypt" local socketdriver = require "socketdriver" diff --git a/lualib/gateserver.lua b/lualib/gamefw/gateserver.lua similarity index 100% rename from lualib/gateserver.lua rename to lualib/gamefw/gateserver.lua diff --git a/lualib/loginserver.lua b/lualib/gamefw/loginserver.lua similarity index 100% rename from lualib/loginserver.lua rename to lualib/gamefw/loginserver.lua diff --git a/service/gate.lua b/service/gate.lua index 665be9d0..e1f6f1d8 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local gateserver = require "gateserver" +local gateserver = require "gamefw.gateserver" local netpack = require "netpack" local watchdog From 68ddeab8fac1bd15d56558625fd9eed061cd76ba Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 19:44:59 +0800 Subject: [PATCH 088/729] bugfix: skynet.localname return nil not 0 --- service/service_mgr.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/service_mgr.lua b/service/service_mgr.lua index f49a79f7..db9499d4 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -176,7 +176,7 @@ skynet.start(function() end end) local handle = skynet.localname ".service" - if handle ~= 0 then + if handle then skynet.error(".service is already register by ", skynet.address(handle)) skynet.exit() else From 0d1c3a64d17eb41b55cd4b8ff31499f612efe88a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 21:02:23 +0800 Subject: [PATCH 089/729] bugfix --- examples/login/client.lua | 10 ++++++-- examples/login/logind.lua | 44 ++++++++++++++++++++++++----------- examples/login/msggate.lua | 4 ++-- lualib-src/lua-crypt.c | 2 +- lualib/gamefw/loginserver.lua | 38 ++++++++++++++++-------------- 5 files changed, 63 insertions(+), 35 deletions(-) diff --git a/examples/login/client.lua b/examples/login/client.lua index 85a8ab36..7a72a036 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -14,7 +14,6 @@ package.cpath = "luaclib/?.so" ]] local socket = require "clientsocket" -local cjson = require "cjson" local crypt = require "crypt" local last @@ -61,7 +60,14 @@ local token = { pass = "password", } -local etoken = crypt.desencode(secret, cjson.encode(token)) +local function encode_token(token) + return string.format("%s@%s:%s", + crypt.base64encode(token.user), + crypt.base64encode(token.server), + crypt.base64encode(token.pass)) +end + +local etoken = crypt.desencode(secret, encode_token(token)) local b = crypt.base64encode(etoken) socket.writeline(fd, crypt.base64encode(etoken)) diff --git a/examples/login/logind.lua b/examples/login/logind.lua index 2ff79c48..b28c6721 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -1,5 +1,4 @@ local login = require "gamefw.loginserver" -local json = require "cjson" local crypt = require "crypt" local skynet = require "skynet" @@ -12,32 +11,51 @@ local server = { local server_list = {} local user_online = {} +local server_mt = {} +server_mt.__index = server_mt + +function server_mt:kick(uid) + skynet.call(self.address, "lua", "kick", self.name, uid) +end + +function server_mt:login(uid, secret) + skynet.call(self.address, "lua", "login", self.name, uid, secret) +end + function server.auth_handler(token) - token = json.decode(token) - assert(token.user) - assert(token.pass == "password") - return token.server, token.user + -- the token is base64(user)@base64(server):base64(password) + local user, server, password = token:match("([^@]+)@([^:]+):(.+)") + user = crypt.base64decode(user) + server = crypt.base64decode(server) + password = crypt.base64decode(password) + assert(password == "password") + return server, user end function server.login_handler(server, uid, secret) print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret))) local u = user_online[uid] if u then - local gameserver = server_list[u.server] - skynet.call(gameserver, "lua", "kick", server, uid) + u:kick(uid) end - local gameserver = assert(server_list[server]) - skynet.call(gameserver, "lua", "login", server, uid, secret) + assert(user_online[uid] == nil, "kick failed") + local gameserver = assert(server_list[server], "Unknown server") + gameserver:login(uid, secret) + user_online[uid] = gameserver end local CMD = {} -function CMD.register_gate(source, name) - server_list[name] = source +function CMD.register_gate(server, address) + server_list[server] = setmetatable( { name = server, address = address }, server_mt ) end -function CMD.logout(source, uid, server) - print(string.format("%s@%s is logout", uid, server)) +function CMD.logout(uid) + local u = user_online[uid] + if u then + print(string.format("%s@%s is logout", uid, u.name)) + user_online[uid] = nil + end end function server.command_handler(command, source, ...) diff --git a/examples/login/msggate.lua b/examples/login/msggate.lua index 85c3d4af..fb74ffc2 100644 --- a/examples/login/msggate.lua +++ b/examples/login/msggate.lua @@ -140,7 +140,7 @@ handler.error = handler.close function handler.open(source, conf) login_master = assert(conf.loginserver) local servername = assert(conf.servername) - skynet.call(login_master, "lua", "register_gate", servername) + skynet.call(login_master, "lua", "register_gate", servername, skynet.self()) end @@ -162,7 +162,7 @@ end function CMD.logout(source) local c = agent[source] if c then - skynet.call(login_master, "lua", "logout", c.server, c.uid) + skynet.call(login_master, "lua", "logout", c.uid) if c.fd then gateserver.closeclient(c.fd) end diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 34a190e7..5d2f12f9 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -729,7 +729,7 @@ lb64encode(lua_State *L) { } int i,j; j=0; - for (i=0;i> 18]; buffer[j+1] = encoding[(v >> 12) & 0x3f]; diff --git a/lualib/gamefw/loginserver.lua b/lualib/gamefw/loginserver.lua index 7f022508..a7384105 100644 --- a/lualib/gamefw/loginserver.lua +++ b/lualib/gamefw/loginserver.lua @@ -2,18 +2,21 @@ local skynet = require "skynet" local socket = require "socket" local crypt = require "crypt" -local function launch_slave(auth_handler) - local cmd = {} +local function write(fd, text) + assert(socket.write(fd, text), "socket error") +end +local function launch_slave(auth_handler) -- set socket buffer limit (8K) -- If the attacker send large package, close the socket socket.limit(8192) - function cmd.auth(fd, addr) + local function auth(fd, addr) + fd = assert(tonumber(fd)) skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) socket.start(fd) local challenge = crypt.randomkey() - socket.write(fd, crypt.base64encode(challenge).."\n") + write(fd, crypt.base64encode(challenge).."\n") local handshake = assert(socket.readline(fd), "socket closed") local clientkey = crypt.base64decode(handshake) @@ -21,7 +24,7 @@ local function launch_slave(auth_handler) error "Invalid client key" end local serverkey = crypt.randomkey() - socket.write(fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n") + write(fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n") local secret = crypt.dhsecret(clientkey, serverkey) @@ -29,7 +32,7 @@ local function launch_slave(auth_handler) local hmac = crypt.hmac64(challenge, secret) if hmac ~= crypt.base64decode(response) then - socket.write(fd, "400 Bad Request\n") + write(fd, "400 Bad Request\n") error "challenge failed" end @@ -43,26 +46,27 @@ local function launch_slave(auth_handler) return ok, server, uid, secret end - skynet.dispatch("lua", function(_,_,command,...) - local f = assert(cmd[command]) - skynet.ret(skynet.pack(f(...))) + skynet.dispatch("lua", function(_,_,...) + skynet.ret(skynet.pack(auth(...))) end) end local function accept(conf, s, fd, addr) - local ok, server, uid, secret = skynet.call(s, "lua", "auth", fd, addr) + -- call slave auth + local ok, server, uid, secret = skynet.call(s, "lua", fd, addr) + socket.start(fd) + if not ok then - socket.write(fd, "401 Unauthorized\n") + write(fd, "401 Unauthorized\n") error(server) end - socket.start(fd) - local ok, err = pcall(conf.login_handler, server, uid, secret) if ok then - socket.write(fd, "200 OK\n") + err = err or "" + write(fd, "200 "..crypt.base64encode(err).."\n") else - socket.write(fd, "406 Not Acceptable\n") + write(fd, "406 Not Acceptable\n") error(err) end end @@ -80,7 +84,7 @@ local function launch_master(conf) table.insert(slave, source) skynet.ret(skynet.pack(nil)) else - skynet.ret(skynet.pack(conf.command_handler(command, source, ...))) + skynet.ret(skynet.pack(conf.command_handler(command, ...))) end end) @@ -104,7 +108,7 @@ local function launch_master(conf) end) end -local function login (conf) +local function login(conf) local name = "." .. (conf.name or "login") skynet.start(function() local loginmaster = skynet.localname(name) From ad58ed8a2692b45351a7f0e16dffbf1509296df0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 13 Jul 2014 21:25:44 +0800 Subject: [PATCH 090/729] add subid --- examples/login/client.lua | 9 ++++++--- examples/login/logind.lua | 7 ++++--- examples/login/msggate.lua | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/examples/login/client.lua b/examples/login/client.lua index 7a72a036..537f614b 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -71,10 +71,13 @@ local etoken = crypt.desencode(secret, encode_token(token)) local b = crypt.base64encode(etoken) socket.writeline(fd, crypt.base64encode(etoken)) -print(readline()) - +local result = readline() +local code = tonumber(string.sub(result, 1, 3)) +assert(code == 200) socket.close(fd) +local subid = crypt.base64decode(string.sub(result, 5)) + ----- connect to game server local input = {} @@ -105,7 +108,7 @@ end local index = 0 local request = 0 -local handshake = string.format("%s@%s:%d:%d", crypt.base64encode(token.user), crypt.base64encode(token.server) , index, request) +local handshake = string.format("%s#%s@%s:%d:%d", crypt.base64encode(token.user), crypt.base64encode(subid),crypt.base64encode(token.server) , index, request) local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) diff --git a/examples/login/logind.lua b/examples/login/logind.lua index b28c6721..df7f5fd8 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -15,11 +15,11 @@ local server_mt = {} server_mt.__index = server_mt function server_mt:kick(uid) - skynet.call(self.address, "lua", "kick", self.name, uid) + return skynet.call(self.address, "lua", "kick", self.name, uid) end function server_mt:login(uid, secret) - skynet.call(self.address, "lua", "login", self.name, uid, secret) + return skynet.call(self.address, "lua", "login", self.name, uid, secret) end function server.auth_handler(token) @@ -40,8 +40,9 @@ function server.login_handler(server, uid, secret) end assert(user_online[uid] == nil, "kick failed") local gameserver = assert(server_list[server], "Unknown server") - gameserver:login(uid, secret) + local subid = gameserver:login(uid, secret) user_online[uid] = gameserver + return tostring(subid) end local CMD = {} diff --git a/examples/login/msggate.lua b/examples/login/msggate.lua index fb74ffc2..a7315f4f 100644 --- a/examples/login/msggate.lua +++ b/examples/login/msggate.lua @@ -32,7 +32,7 @@ function handler.connect(fd, addr) end local function auth(fd, msg, sz) --- base64(base64(uid)@base64(server):index:request:base64(hmac) +-- base64(base64(uid)@base64(server)#base64(subid):index:request:base64(hmac) local message = netpack.tostring(msg, sz) local username, index, request , hmac = string.match(message, "([^:]*):([^:]*):([^:]*):([^:]*)") local content = users[username] @@ -147,7 +147,8 @@ end local CMD = {} function CMD.login(source, server, uid, secret) - local username = crypt.base64encode(uid) .. '@' .. crypt.base64encode(server) + local subid = "1" + local username = crypt.base64encode(uid) .. '#'..crypt.base64encode(subid)..'@' .. crypt.base64encode(server) users[username] = { server = server, uid = uid, @@ -157,6 +158,7 @@ function CMD.login(source, server, uid, secret) reserved = 0, response = {}, } + return subid end function CMD.logout(source) From 9feaf15b3d982259236bab27c481d4720f0f23b8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 14 Jul 2014 14:14:05 +0800 Subject: [PATCH 091/729] add netpack.pack_padding --- examples/login/msggate.lua | 2 +- lualib-src/lua-netpack.c | 43 +++++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/examples/login/msggate.lua b/examples/login/msggate.lua index a7315f4f..32b3518f 100644 --- a/examples/login/msggate.lua +++ b/examples/login/msggate.lua @@ -32,7 +32,7 @@ function handler.connect(fd, addr) end local function auth(fd, msg, sz) --- base64(base64(uid)@base64(server)#base64(subid):index:request:base64(hmac) +-- base64(uid)@base64(server)#base64(subid):index:request:base64(hmac) local message = netpack.tostring(msg, sz) local username, index, request , hmac = string.match(message, "([^:]*):([^:]*):([^:]*):([^:]*)") local content = users[username] diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index a95ed4d7..432818e7 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -393,13 +393,13 @@ lpop(lua_State *L) { */ static const char * -tolstring(lua_State *L, size_t *sz) { +tolstring(lua_State *L, size_t *sz, int index) { const char * ptr; - if (lua_isuserdata(L,1)) { - ptr = (const char *)lua_touserdata(L,1); - *sz = (size_t)luaL_checkinteger(L, 2); + if (lua_isuserdata(L,index)) { + ptr = (const char *)lua_touserdata(L,index); + *sz = (size_t)luaL_checkinteger(L, index+1); } else { - ptr = luaL_checklstring(L, 1, sz); + ptr = luaL_checklstring(L, index, sz); } return ptr; } @@ -413,7 +413,7 @@ write_size(uint8_t * buffer, int len) { static int lpack(lua_State *L) { size_t len; - const char * ptr = tolstring(L, &len); + const char * ptr = tolstring(L, &len, 1); if (len > 0x10000) { return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); } @@ -433,7 +433,7 @@ lpack_string(lua_State *L) { uint8_t tmp[SMALLSTRING+2]; size_t len; uint8_t *buffer; - const char * ptr = tolstring(L, &len); + const char * ptr = tolstring(L, &len, 1); if (len > 0x10000) { return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); } @@ -451,6 +451,34 @@ lpack_string(lua_State *L) { return 1; } +static int +lpack_padding(lua_State *L) { + uint8_t tmp[SMALLSTRING+2]; + size_t content_sz; + uint8_t *buffer; + const char * ptr = tolstring(L, &content_sz, 2); + size_t header_sz = 0; + const char * header = luaL_checklstring(L,1,&header_sz); + size_t len = header_sz + content_sz; + + if (len > 0x10000) { + return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); + } + + if (len <= SMALLSTRING) { + buffer = tmp; + } else { + buffer = lua_newuserdata(L, len + 2); + } + + write_size(buffer, len); + memcpy(buffer+2, header, header_sz); + memcpy(buffer+2+header_sz, ptr, content_sz); + lua_pushlstring(L, (const char *)buffer, len+2); + + return 1; +} + static int ltostring(lua_State *L) { void * ptr = lua_touserdata(L, 1); @@ -471,6 +499,7 @@ luaopen_netpack(lua_State *L) { { "pop", lpop }, { "pack", lpack }, { "pack_string", lpack_string }, + { "pack_padding", lpack_padding }, { "clear", lclear }, { "tostring", ltostring }, { NULL, NULL }, From 4cdf034f15775ef1e2abeeedf314c9f69152a0dc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 14 Jul 2014 14:14:42 +0800 Subject: [PATCH 092/729] release 0.4.2 --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 47b62280..5926bc02 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -Dev version +v0.4.2 (2014-7-14) ----------- * Bugfix : invalid negative socket id * Add optional TCP_NODELAY support From dbf492b76192e2d83595c7c27692594d0d0feeb2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 15 Jul 2014 12:05:58 +0800 Subject: [PATCH 093/729] add snax.gateserver snax.loginserver snax.msgserver --- HISTORY.md | 5 + examples/login/client.lua | 73 +++--- examples/login/gated.lua | 88 +++++++ examples/login/logind.lua | 41 ++-- examples/login/main.lua | 6 +- examples/login/msgagent.lua | 31 ++- examples/login/msggate.lua | 189 --------------- lualib-src/lua-netpack.c | 25 +- lualib/skynet.lua | 7 +- lualib/{gamefw => snax}/gateserver.lua | 6 +- lualib/{gamefw => snax}/loginserver.lua | 47 +++- lualib/snax/msgserver.lua | 299 ++++++++++++++++++++++++ service/gate.lua | 13 +- 13 files changed, 562 insertions(+), 268 deletions(-) create mode 100644 examples/login/gated.lua delete mode 100644 examples/login/msggate.lua rename lualib/{gamefw => snax}/gateserver.lua (96%) rename lualib/{gamefw => snax}/loginserver.lua (74%) create mode 100644 lualib/snax/msgserver.lua diff --git a/HISTORY.md b/HISTORY.md index 5926bc02..5c52c767 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +Dev version +----------- +* skynet.exit will quit service immediately. +* Add snax.gateserver, snax.loginserver, snax.msgserver + v0.4.2 (2014-7-14) ----------- * Bugfix : invalid negative socket id diff --git a/examples/login/client.lua b/examples/login/client.lua index 537f614b..82b0a182 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -1,18 +1,5 @@ package.cpath = "luaclib/?.so" ---[[ Status code - -200 OK - -400 Bad Request (通常是登陆å议错误) -401 Unauthorized (通常是登陆æœåŠ¡å™¨æˆ–æ¸¸æˆæœåŠ¡å™¨éªŒè¯é”™è¯¯) -403 Forbidden (é€šå¸¸æ˜¯è¿žæŽ¥æ¸¸æˆæœåŠ¡å™¨çš„ index å·²ç»è¿‡æœŸ) -404 Not Found (é€šå¸¸æ˜¯æ¸¸æˆæœåŠ¡å™¨æœªèŽ·å¾—ç™»é™†æœåŠ¡å™¨çš„é€šçŸ¥) -406 Not Acceptable (通常是登陆æœåŠ¡å™¨è½¬å‘æ¸¸æˆæœåŠ¡å™¨æ‹’ç»ç™»é™†) -412 Precondition Failed (é€šå¸¸æ˜¯é—æ¼äº†å’Œæ¸¸æˆæœåС噍剿¬¡é€šè®¯çš„请求) - -]] - local socket = require "clientsocket" local crypt = require "crypt" @@ -72,16 +59,38 @@ local b = crypt.base64encode(etoken) socket.writeline(fd, crypt.base64encode(etoken)) local result = readline() +print(result) local code = tonumber(string.sub(result, 1, 3)) assert(code == 200) socket.close(fd) local subid = crypt.base64decode(string.sub(result, 5)) +print("login ok, subid=", subid) + ----- connect to game server +local session = 0 + +local function send_request(v) + session = session + 1 + local s = string.char(bit32.extract(session,24,8), bit32.extract(session,16,8), bit32.extract(session,8,8), bit32.extract(session,0,8)) + socket.send(fd , v..s) + return session, v +end + +local function recv_response(v) + local content = v:sub(1,-6) + local ok = v:sub(-5,-5):byte() + local session = 0 + for i=-4,-1 do + local c = v:byte(i) + session = session + bit32.lshift(c,(-1-i) * 8) + end + return ok ~=0 , session, content +end + local input = {} -local fd = assert(socket.connect("127.0.0.1", 8888)) local function readpackage() local line = table.remove(input, 1) @@ -106,20 +115,32 @@ local function readpackage() end end -local index = 0 -local request = 0 -local handshake = string.format("%s#%s@%s:%d:%d", crypt.base64encode(token.user), crypt.base64encode(subid),crypt.base64encode(token.server) , index, request) -local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) - -socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) - -print(readpackage()) - -socket.send(fd , "echo") -print(readpackage()) - +local index = 1 +local function echo(text) + print("connect") + local fd = assert(socket.connect("127.0.0.1", 8888)) + input = {} + local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) + local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) + socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) + + print(readpackage()) + print("===>",send_request(text)) + print("===>",send_request(text .. " again")) + print("<===",recv_response(readpackage())) + print("<===",recv_response(readpackage())) + + print("disconnect") + socket.close(fd) +end + +echo "hello" + +index = index + 1 + +echo "world" diff --git a/examples/login/gated.lua b/examples/login/gated.lua new file mode 100644 index 00000000..aeaa2013 --- /dev/null +++ b/examples/login/gated.lua @@ -0,0 +1,88 @@ +local msgserver = require "snax.msgserver" +local crypt = require "crypt" +local skynet = require "skynet" + +local loginservice = tonumber(...) + +local server = {} +local users = {} +local username_map = {} +local internal_id = 0 + +-- login server disallow multi login, so login_handler never be reentry +-- call by login server +function server.login_handler(uid, secret) + if users[uid] then + error(string.format("%s is already login", uid)) + end + + internal_id = internal_id + 1 + local username = msgserver.username(uid, internal_id, servername) + + -- you can use a pool to alloc new agent + local agent = skynet.newservice "msgagent" + local u = { + username = username, + agent = agent, + uid = uid, + subid = internal_id, + } + + -- trash subid (no used) + skynet.call(agent, "lua", "login", uid, internal_id, secret) + + users[uid] = u + username_map[username] = u + + msgserver.login(username, secret) + + -- you should return unique subid + return internal_id +end + +-- call by agent +function server.logout_handler(uid, subid) + local u = users[uid] + if u then + local username = msgserver.username(uid, subid, servername) + assert(u.username == username) + msgserver.logout(u.username) + users[uid] = nil + username_map[u.username] = nil + skynet.call(loginservice, "lua", "logout",uid, subid) + end +end + +-- call by login server +function server.kick_handler(uid, subid) + local u = users[uid] + if u then + local username = msgserver.username(uid, subid, servername) + assert(u.username == username) + -- NOTICE: logout may call skynet.exit, so you should use pcall. + pcall(skynet.call, u.agent, "lua", "logout") + end +end + +-- call by self (when socket disconnect) +function server.disconnect_handler(username) + local u = username_map[username] + if u then + skynet.call(u.agent, "lua", "afk") + end +end + +-- call by self (when recv a request from client) +function server.request_handler(username, msg, sz) + local u = username_map[username] + return skynet.tostring(skynet.rawcall(u.agent, "client", msg, sz)) +end + +-- call by self (when gate open) +function server.register_handler(name) + servername = name + skynet.call(loginservice, "lua", "register_gate", servername, skynet.self()) +end + +msgserver.start(server) + diff --git a/examples/login/logind.lua b/examples/login/logind.lua index df7f5fd8..e6e4c10f 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -1,26 +1,17 @@ -local login = require "gamefw.loginserver" +local login = require "snax.loginserver" local crypt = require "crypt" local skynet = require "skynet" local server = { host = "127.0.0.1", port = 8001, + multilogin = false, -- disallow multilogin name = "login_master", } local server_list = {} local user_online = {} - -local server_mt = {} -server_mt.__index = server_mt - -function server_mt:kick(uid) - return skynet.call(self.address, "lua", "kick", self.name, uid) -end - -function server_mt:login(uid, secret) - return skynet.call(self.address, "lua", "login", self.name, uid, secret) -end +local user_login = {} function server.auth_handler(token) -- the token is base64(user)@base64(server):base64(password) @@ -34,27 +25,31 @@ end function server.login_handler(server, uid, secret) print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret))) - local u = user_online[uid] - if u then - u:kick(uid) - end - assert(user_online[uid] == nil, "kick failed") local gameserver = assert(server_list[server], "Unknown server") - local subid = gameserver:login(uid, secret) - user_online[uid] = gameserver - return tostring(subid) + -- only one can login, because disallow multilogin + local last = user_online[uid] + if last then + skynet.call(last.address, "lua", "kick", uid, last.subid) + end + if user_online[uid] then + error(string.format("user %s is already online", uid)) + end + + local subid = tostring(skynet.call(gameserver, "lua", "login", uid, secret)) + user_online[uid] = { address = gameserver, subid = subid , server = server} + return subid end local CMD = {} function CMD.register_gate(server, address) - server_list[server] = setmetatable( { name = server, address = address }, server_mt ) + server_list[server] = address end -function CMD.logout(uid) +function CMD.logout(uid, subid) local u = user_online[uid] if u then - print(string.format("%s@%s is logout", uid, u.name)) + print(string.format("%s@%s is logout", uid, u.server)) user_online[uid] = nil end end diff --git a/examples/login/main.lua b/examples/login/main.lua index 6359cb96..1eef6f4d 100644 --- a/examples/login/main.lua +++ b/examples/login/main.lua @@ -1,12 +1,12 @@ local skynet = require "skynet" skynet.start(function() - local loginserver = skynet.newservice "logind" - local gate = skynet.newservice "msggate" + local loginserver = skynet.newservice("logind") + local gate = skynet.newservice("gated", loginserver) + skynet.call(gate, "lua", "open" , { port = 8888, maxclient = 64, - loginserver = loginserver, servername = "sample", }) end) diff --git a/examples/login/msgagent.lua b/examples/login/msgagent.lua index 30545c68..90b4ac08 100644 --- a/examples/login/msgagent.lua +++ b/examples/login/msgagent.lua @@ -7,21 +7,46 @@ skynet.register_protocol { } local gate +local userid, subid local CMD = {} -function CMD.init(source , uid, server) +function CMD.login(source, uid, sid, secret) + -- you may use secret to make a encrypted data stream + skynet.error(string.format("%s is login", uid)) gate = source - skynet.error(string.format("%s is coming", uid)) + userid = uid + subid = sid + -- you may load user data from database +end + +local function logout() + if gate then + skynet.call(gate, "lua", "logout", userid, subid) + end + skynet.exit() +end + +function CMD.logout(source) + -- NOTICE: The logout MAY be reentry + skynet.error(string.format("%s is logout", userid)) + logout() +end + +function CMD.afk(source) + -- the connection is broken, but the user may back + skynet.error(string.format("AFK")) end skynet.start(function() - skynet.dispatch("lua", function(_, source, command, ...) + -- If you want to fork a work thread , you MUST do it in CMD.login + skynet.dispatch("lua", function(session, source, command, ...) local f = assert(CMD[command]) skynet.ret(skynet.pack(f(source, ...))) end) skynet.dispatch("client", function(_,_, msg) + -- the simple ehco service skynet.ret(msg) end) end) diff --git a/examples/login/msggate.lua b/examples/login/msggate.lua deleted file mode 100644 index 32b3518f..00000000 --- a/examples/login/msggate.lua +++ /dev/null @@ -1,189 +0,0 @@ -local skynet = require "skynet" -local gateserver = require "gamefw.gateserver" -local netpack = require "netpack" -local crypt = require "crypt" -local socketdriver = require "socketdriver" -local datacenter = require "datacenter" - -local users = {} - -skynet.register_protocol { - name = "client", - id = skynet.PTYPE_CLIENT, -} - -local handler = {} -local handshake = {} -local connection = {} -local agent = {} -local login_master - --- launch an agent service to handle message from client -local function launch_agent(c) - local agent = assert(skynet.newservice "msgagent") - skynet.call(agent, "lua", "init", c.uid, c.server) - return agent -end - -function handler.connect(fd, addr) - skynet.error(string.format("new connection from %s (fd=%d)", addr, fd)) - handshake[fd] = true - gateserver.openclient(fd) -end - -local function auth(fd, msg, sz) --- base64(uid)@base64(server)#base64(subid):index:request:base64(hmac) - local message = netpack.tostring(msg, sz) - local username, index, request , hmac = string.match(message, "([^:]*):([^:]*):([^:]*):([^:]*)") - local content = users[username] - if content == nil then - return "404 Not Found" - end - local idx = assert(tonumber(index)) - local req = assert(tonumber(request)) - hmac = crypt.base64decode(hmac) - - if idx < content.index then - return "403 Forbidden" - end - if req > content.request or req < content.reserved then - return "412 Precondition Failed" - end - - local text = string.format("%s:%s:%s", username, index, request) - local v = crypt.hmac64(crypt.hashkey(text), content.secret) - if v ~= hmac then - return "401 Unauthorized" - end - - content.index = idx - for i=content.reserved, request do - content.response[i] = nil - end - content.reserved = request - content.reqest = request - connection[fd] = content - - if content.fd then - local last_fd = content.fd - connection[last_fd] = nil - gateserver.closeclient(last_fd) - end - content.fd = fd - - if content.agent == nil then - content.agent = true - local ok, agent = pcall(launch_agent, content) - if ok then - content.agent = agent - else - content.agent = nil - skynet.error(string.format("Launch agent %s failed : %s", content.uid, agent)) - connection[fd] = nil - gateserver.closeclient(fd) - end - end -end - -function handler.message(fd, msg, sz) - if handshake[fd] then - local ok, result = pcall(auth, fd, msg, sz) - if not ok then - result = "400 Bad Request" - end - - local close = result ~= nil - - if result == nil then - result = "200 OK" - end - - socketdriver.send(fd, netpack.pack(result)) - - if close then - gateserver.closeclient(fd) - end - handshake[fd] = nil - else - local c = connection[fd] - if c == nil or c.agent == nil then - local message = netpack.tostring(msg, sz) - skynet.error(string.format("Unknown fd = %d, message (%s) size = %d", fd, crypt.hexencode(message):sub(1,80), #message)) - return - end - c.request = c.request + 1 - local ret = c.response[c.request] - if ret == nil then - local ok, msg, sz = pcall(skynet.rawcall, c.agent, "client", msg, sz) - if ok then - ret = netpack.pack_string(msg, sz) - else - skynet.error(string.format("%s request error : %s", c.username, msg)) - ret = netpack.pack_string "" - end - c.response[c.request] = ret - end - socketdriver.send(c.fd, ret) - end -end - -function handler.close(fd) - handshake[fd] = nil - local c = connection[fd] - if c then - c.fd = nil - end -end - -handler.error = handler.close - -function handler.open(source, conf) - login_master = assert(conf.loginserver) - local servername = assert(conf.servername) - skynet.call(login_master, "lua", "register_gate", servername, skynet.self()) -end - - -local CMD = {} - -function CMD.login(source, server, uid, secret) - local subid = "1" - local username = crypt.base64encode(uid) .. '#'..crypt.base64encode(subid)..'@' .. crypt.base64encode(server) - users[username] = { - server = server, - uid = uid, - secret = secret, - index = 0, - request = 0, - reserved = 0, - response = {}, - } - return subid -end - -function CMD.logout(source) - local c = agent[source] - if c then - skynet.call(login_master, "lua", "logout", c.uid) - if c.fd then - gateserver.closeclient(c.fd) - end - end -end - -function CMD.kick(source, server, uid) - local username = crypt.base64encode(uid) .. '@' .. crypt.base64encode(server) - local u = users[username] - if u and u.agent then - skynet.call(u.agent, "logout") - skynet.kill(u.agent) - u.agent = nil - end -end - -function handler.command(cmd, source, ...) - local f = assert(CMD[cmd]) - return f(source, ...) -end - -gateserver.start(handler) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 432818e7..4012c9bc 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -457,9 +457,9 @@ lpack_padding(lua_State *L) { size_t content_sz; uint8_t *buffer; const char * ptr = tolstring(L, &content_sz, 2); - size_t header_sz = 0; - const char * header = luaL_checklstring(L,1,&header_sz); - size_t len = header_sz + content_sz; + size_t cookie_sz = 0; + const char * cookie = luaL_checklstring(L,1,&cookie_sz); + size_t len = cookie_sz + content_sz; if (len > 0x10000) { return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); @@ -472,8 +472,8 @@ lpack_padding(lua_State *L) { } write_size(buffer, len); - memcpy(buffer+2, header, header_sz); - memcpy(buffer+2+header_sz, ptr, content_sz); + memcpy(buffer+2, ptr, content_sz); + memcpy(buffer+2+content_sz, cookie, cookie_sz); lua_pushlstring(L, (const char *)buffer, len+2); return 1; @@ -486,8 +486,19 @@ ltostring(lua_State *L) { if (ptr == NULL) { lua_pushliteral(L, ""); } else { - lua_pushlstring(L, (const char *)ptr, size); - skynet_free(ptr); + if (lua_isnumber(L, 3)) { + int offset = lua_tointeger(L, 3); + if (offset < 0) { + return luaL_error(L, "Invalid offset %d", offset); + } + if (offset > size) { + offset = size; + } + lua_pushlstring(L, (const char *)ptr + offset, size-offset); + } else { + lua_pushlstring(L, (const char *)ptr, size); + skynet_free(ptr); + } } return 1; } diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 2b984e2a..5e8e95b0 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -127,7 +127,7 @@ function suspend(co, result, command, param, size) if not result then local session = session_coroutine_id[co] local addr = session_coroutine_address[co] - if session and session ~= 0 then + if session then c.send(addr, skynet.PTYPE_ERROR, session, "") end session_coroutine_id[co] = nil @@ -151,6 +151,9 @@ function suspend(co, result, command, param, size) -- coroutine exit session_coroutine_id[co] = nil session_coroutine_address[co] = nil + elseif command == "QUIT" then + -- service exit + return else error("Unknown command : " .. command .. "\n" .. debug.traceback(co)) end @@ -264,6 +267,8 @@ function skynet.exit() end end c.command("EXIT") + -- quit service + coroutine_yield "QUIT" end function skynet.kill(name) diff --git a/lualib/gamefw/gateserver.lua b/lualib/snax/gateserver.lua similarity index 96% rename from lualib/gamefw/gateserver.lua rename to lualib/snax/gateserver.lua index 84f33f88..fc019143 100644 --- a/lualib/gamefw/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -40,7 +40,7 @@ function gateserver.start(handler) socket = socketdriver.listen(address, port) socketdriver.start(socket) if handler.open then - handler.open(source, conf) + return handler.open(source, conf) end end @@ -88,8 +88,8 @@ function gateserver.start(handler) end function MSG.close(fd) - if handler.close then - handler.close(fd) + if handler.disconnect then + handler.disconnect(fd) end close_fd(fd) end diff --git a/lualib/gamefw/loginserver.lua b/lualib/snax/loginserver.lua similarity index 74% rename from lualib/gamefw/loginserver.lua rename to lualib/snax/loginserver.lua index a7384105..17cb131d 100644 --- a/lualib/gamefw/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -1,6 +1,35 @@ local skynet = require "skynet" local socket = require "socket" local crypt = require "crypt" +local table = table +local string = string +local assert = assert + +--[[ + +Protocol: + + line (\n) based text protocol + + 1. Server->Client : base64(8bytes random challenge) + 2. Client->Server : base64(8bytes handshake client key) + 3. Server: Gen a 8bytes handshake server key + 4. Server->Client : base64(DH-Exchange(server key)) + 5. Server/Client secret := DH-Secret(client key/server key) + 6. Client->Server : base64(HMAC(challenge, secret)) + 7. Client->Server : DES(secret, base64(token)) + 8. Server : call auth_handler(token) -> server, uid (A user defined method) + 9. Server : call login_handler(server, uid, secret) (A user defined method) + +Error Code: + 400 Bad Request . challenge failed + 401 Unauthorized . unauthorized by auth_handler + 403 Forbidden . login_handler failed + 406 Not Acceptable . already in login (disallow multi login) + +Success: + 200 base64(subid) +]] local function write(fd, text) assert(socket.write(fd, text), "socket error") @@ -51,6 +80,8 @@ local function launch_slave(auth_handler) end) end +local user_login = {} + local function accept(conf, s, fd, addr) -- call slave auth local ok, server, uid, secret = skynet.call(s, "lua", fd, addr) @@ -61,12 +92,24 @@ local function accept(conf, s, fd, addr) error(server) end + if not conf.multilogin then + if user_login[uid] then + write(fd, "406 Not Acceptable\n") + error(string.format("User %s is already login", uid)) + end + + user_login[uid] = true + end + local ok, err = pcall(conf.login_handler, server, uid, secret) + -- unlock login + user_login[uid] = nil + if ok then err = err or "" write(fd, "200 "..crypt.base64encode(err).."\n") else - write(fd, "406 Not Acceptable\n") + write(fd, "403 Forbidden\n") error(err) end end @@ -92,8 +135,8 @@ local function launch_master(conf) skynet.newservice(SERVICE_NAME) end - local id = socket.listen(host, port) skynet.error(string.format("login server listen at : %s %d", host, port)) + local id = socket.listen(host, port) socket.start(id , function(fd, addr) local s = slave[balance] balance = balance + 1 diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua new file mode 100644 index 00000000..d1a84f3a --- /dev/null +++ b/lualib/snax/msgserver.lua @@ -0,0 +1,299 @@ +local skynet = require "skynet" +local gateserver = require "snax.gateserver" +local netpack = require "netpack" +local crypt = require "crypt" +local socketdriver = require "socketdriver" +local assert = assert +local b64encode = crypt.base64encode +local b64decode = crypt.base64decode + +--[[ + +Protocol: + + All the number type is big-endian + + Shakehands (The first package) + + Client -> Server : + + base64(uid)@base64(server)#base64(subid):index:base64(hmac) + + Server -> Client + + XXX ErrorCode + 404 User Not Found + 403 Index Expired + 401 Unauthorized + 400 Bad Request + 200 OK + + Req-Resp + + Client -> Server : Request + word size (Not include self) + string content (size-4) + dword session + + Server -> Client : Response + word size (Not include self) + string content (size-5) + byte ok (1 is ok, 0 is error) + dword session + +API: + server.userid(username) + return uid, subid, server + + server.username(uid, subid, server) + return username + + server.login(username, secret) + update user secret + + server.logout(username) + user logout + + server.ip(username) + return ip when connection establish, or nil + + server.start(conf) + start server + +Supported skynet command: + kick username (may used by loginserver) + login username secret (used by loginserver) + logout username (used by agent) + +Config for server.start: + conf.expired_number : the number of the response message cached after sending out (default is 128) + conf.login_handler(uid, secret) -> subid : the function when a new user login, alloc a subid for it. (may call by login server) + conf.logout_handler(uid, subid) : the functon when a user logout. (may call by agent) + conf.kick_handler(uid, subid) : the functon when a user logout. (may call by login server) + conf.request_handler(username, session, msg, sz) : the function when recv a new request. + conf.register_handler(servername) : call when gate open + conf.disconnect_handler(username) : call when a connection disconnect (afk) +]] + +local server = {} + +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, +} + +local user_online = {} +local handshake = {} +local connection = {} + +function server.userid(username) + -- base64(uid)@base64(server)#base64(subid) + local uid, servername, subid = username:match "([^@]*)@([^#]*)#(.*)" + return b64decode(uid), b64decode(subid), b64decode(servername) +end + +function server.username(uid, subid, servername) + return string.format("%s@%s#%s", b64encode(uid), b64encode(servername), b64encode(tostring(subid))) +end + +function server.logout(username) + local u = user_online[username] + user_online[username] = nil + if u.fd then + gateserver.closeclient(u.fd) + connection[u.fd] = nil + end +end + +function server.login(username, secret) + assert(user_online[username] == nil) + user_online[username] = { + secret = secret, + version = 0, + index = 0, + username = username, + response = {}, -- response cache + } +end + +function server.ip(username) + local u = user_online[username] + if u and u.fd then + return u.ip + end +end + +function server.start(conf) + local expired_number = conf.expired_number or 128 + + local handler = {} + + local CMD = { + login = assert(conf.login_handler), + logout = assert(conf.logout_handler), + kick = assert(conf.kick_handler), + } + + function handler.command(cmd, source, ...) + local f = assert(CMD[cmd]) + return f(...) + end + + function handler.open(source, gateconf) + local servername = assert(gateconf.servername) + return conf.register_handler(servername) + end + + function handler.connect(fd, addr) + handshake[fd] = addr + gateserver.openclient(fd) + end + + function handler.disconnect(fd) + handshake[fd] = nil + local c = connection[fd] + if c then + c.fd = nil + connection[fd] = nil + if conf.disconnect_handler then + conf.disconnect_handler(c.username) + end + end + end + + handler.error = handler.disconnect + + -- atomic , no yield + local function do_auth(fd, message, addr) + local username, index, hmac = string.match(message, "([^:]*):([^:]*):([^:]*)") + local u = user_online[username] + if u == nil then + return "404 User Not Found" + end + local idx = assert(tonumber(index)) + hmac = b64decode(hmac) + + if idx <= u.version then + return "403 Index Expired" + end + + local text = string.format("%s:%s", username, index) + local v = crypt.hmac64(crypt.hashkey(text), u.secret) + if v ~= hmac then + return "401 Unauthorized" + end + + u.version = idx + u.fd = fd + u.ip = addr + connection[fd] = u + end + + local function auth(fd, addr, msg, sz) + local message = netpack.tostring(msg, sz) + local ok, result = pcall(do_auth, fd, message, addr) + if not ok then + skynet.error(result) + result = "400 Bad Request" + end + + local close = result ~= nil + + if result == nil then + result = "200 OK" + end + + socketdriver.send(fd, netpack.pack(result)) + + if close then + gateserver.closeclient(fd) + end + end + + local request_handler = assert(conf.request_handler) + + -- u.response is a struct { message, version, index } + local function retire_response(u) + if u.index >= expired_number * 2 then + local max = 0 + local response = u.response + for k,p in pairs(response) do + if p[3] < expired_number then + response[k] = nil + else + p[3] = p[3] - expired_number + if p[3] > max then + max = p[3] + end + end + end + u.index = max + 1 + end + end + + local function do_request(fd, msg, sz) + local u = assert(connection[fd], "invalid fd") + local msg_sz = sz - 4 + local session = netpack.tostring(msg, sz, msg_sz) + local p = u.response[session] + if p then + -- session can be reuse in the same connection + if p[2] == u.version then + u.response[session] = nil + p = nil + end + end + + if p == nil then + local ok, result = pcall(conf.request_handler, u.username, msg, msg_sz) + -- NOTICE: YIELD here, socket may close. + if not ok then + skynet.error(result) + result = "\0" .. session + else + result = result .. '\1' .. session + end + + p = { netpack.pack_string(result), u.version, u.index } + if u.response[session] then + skynet.error(string.format("Conflict session %s", crypt.hexencode(session))) + end + u.response[session] = p + else + netpack.tostring(msg, sz) -- request before, so free msg + -- resend response, and update index p[3]. + p[3] = u.index + end + u.index = u.index + 1 + -- check connect again + if connection[fd] then + socketdriver.send(fd, p[1]) + end + retire_response(u) + end + + local function request(fd, msg, sz) + local ok, err = pcall(do_request, fd, msg, sz) + -- not atomic, may yield + if not ok then + skynet.error(string.format("Invalid package %s : %s", err, netpack.tostring(msg, sz))) + if connection[fd] then + gateserver.closeclient(fd) + end + end + end + + function handler.message(fd, msg, sz) + local addr = handshake[fd] + if addr then + auth(fd,addr,msg,sz) + handshake[fd] = nil + else + request(fd, msg, sz) + end + end + + return gateserver.start(handler) +end + +return server diff --git a/service/gate.lua b/service/gate.lua index e1f6f1d8..911a126a 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local gateserver = require "gamefw.gateserver" +local gateserver = require "snax.gateserver" local netpack = require "netpack" local watchdog @@ -53,7 +53,7 @@ local function close_fd(fd) end end -function handler.close(fd) +function handler.disconnect(fd) close_fd(fd) skynet.send(watchdog, "lua", "socket", "close", fd) end @@ -89,15 +89,6 @@ function CMD.accept(source, fd) end function CMD.kick(source, fd) - local c - if fd then - c = connection[fd] - else - c = forwarding[source] - end - - assert(c) - gateserver.closeclient(fd) end From 691bb281639ce596d4ec7300849806248ed9f17b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Jul 2014 12:08:42 +0800 Subject: [PATCH 094/729] update comment --- lualib/snax/loginserver.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 17cb131d..5d332200 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -19,7 +19,8 @@ Protocol: 6. Client->Server : base64(HMAC(challenge, secret)) 7. Client->Server : DES(secret, base64(token)) 8. Server : call auth_handler(token) -> server, uid (A user defined method) - 9. Server : call login_handler(server, uid, secret) (A user defined method) + 9. Server : call login_handler(server, uid, secret) ->subid (A user defined method) + 10. Server->Client : 200 base64(subid) Error Code: 400 Bad Request . challenge failed From 37a46607ce3d3bfbda65e767a09abe3db0244236 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Jul 2014 12:17:55 +0800 Subject: [PATCH 095/729] add socketchannel:changhost --- lualib/mongo.lua | 1 + lualib/socketchannel.lua | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index f9eece35..9760a545 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -84,6 +84,7 @@ local function mongo_auth(mongoc) end return function() assert(mongoc:auth(user, pass)) + -- todo : If mongoc want to connect to replica set, runCommand ismater here, and call mongoc.__sock:changehost end end diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index e6a061f0..e30114d3 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -175,11 +175,11 @@ local function connect_once(self) end self.__authcoroutine = false - return true + return self.__sock ~= nil end local function try_connect(self , once) - local t = 100 + local t = 0 while not self.__closed do if connect_once(self) then if not once then @@ -292,6 +292,16 @@ function channel:close() end end +function channel:changehost(host, port) + self.__host = host + if port then + self.__port = port + end + if not self.__closed then + close_channel_socket(self) + end +end + channel_meta.__gc = channel.close local function wrapper_socket_function(f) From 0ae1fcf7cd88fd6d3496d5e0e2211a68fea9f1f2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Jul 2014 13:46:44 +0800 Subject: [PATCH 096/729] add socketchannel backup host --- lualib/socketchannel.lua | 60 ++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index e30114d3..34fbff36 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -26,6 +26,7 @@ function socket_channel.channel(desc) local c = { __host = assert(desc.host), __port = assert(desc.port), + __backup = desc.backup, __auth = desc.auth, __response = desc.response, -- It's for session mode __request = {}, -- request seq { response func or session } -- It's for order mode @@ -152,30 +153,51 @@ local function dispatch_function(self) end end +local function connect_backup(self) + if self.__backup then + for _, host in ipairs(self.__backup) do + skynet.error("socket: connect to backup host", host, self.__port) + local fd = socket.open(host, self.__port) + if fd then + self.__host = host + return fd + end + end + end +end + local function connect_once(self) assert(not self.__sock and not self.__authcoroutine) local fd = socket.open(self.__host, self.__port) if not fd then - return false - end - self.__authcoroutine = coroutine.running() - self.__sock = setmetatable( {fd} , channel_socket_meta ) - skynet.fork(dispatch_function(self), self) - - if self.__auth then - local ok , message = pcall(self.__auth, self) - if not ok then - close_channel_socket(self) - if message ~= socket_error then - skynet.error("socket: auth failed", message) - end + fd = connect_backup(self) + if not fd then + return false end - self.__authcoroutine = false - return ok end + while not self.__closed do + self.__authcoroutine = coroutine.running() + self.__sock = setmetatable( {fd} , channel_socket_meta ) + skynet.fork(dispatch_function(self), self) - self.__authcoroutine = false - return self.__sock ~= nil + if self.__auth then + local ok , message = pcall(self.__auth, self) + if not ok then + close_channel_socket(self) + if message ~= socket_error then + skynet.error("socket: auth failed", message) + end + end + self.__authcoroutine = false + return ok + end + + self.__authcoroutine = false + if self.__sock then + return true + end + -- auth may change host, so connect again + end end local function try_connect(self , once) @@ -302,6 +324,10 @@ function channel:changehost(host, port) end end +function channel:changebackup(backup) + self.__backup = backup +end + channel_meta.__gc = channel.close local function wrapper_socket_function(f) From acc175e821ffc385a9576a7706ee3ddd7bad50dd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Jul 2014 17:18:56 +0800 Subject: [PATCH 097/729] socketchannel backup support host/port --- lualib/socketchannel.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 34fbff36..80d2d745 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -155,11 +155,18 @@ end local function connect_backup(self) if self.__backup then - for _, host in ipairs(self.__backup) do - skynet.error("socket: connect to backup host", host, self.__port) - local fd = socket.open(host, self.__port) + for _, addr in ipairs(self.__backup) do + local host, port + if type(host) == "table" then + host, port = addr.host, addr.port + else + host = addr + end + skynet.error("socket: connect to backup host", host, port) + local fd = socket.open(host, port) if fd then self.__host = host + self.__port = port return fd end end From 1181730302c8647aa9bf07acba70f40d0a7a47f8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Jul 2014 17:21:39 +0800 Subject: [PATCH 098/729] socketchannel backup support default port --- lualib/socketchannel.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 80d2d745..e953fe8f 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -161,6 +161,9 @@ local function connect_backup(self) host, port = addr.host, addr.port else host = addr + if not addr:find(":", 1, true) then + port = self.__port + end end skynet.error("socket: connect to backup host", host, port) local fd = socket.open(host, port) From 9421435ac53623180ace845f9e7220c3f5b281f5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Jul 2014 17:53:18 +0800 Subject: [PATCH 099/729] remove dup code --- service/gate.lua | 8 -------- 1 file changed, 8 deletions(-) diff --git a/service/gate.lua b/service/gate.lua index 911a126a..5fb22801 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -65,14 +65,6 @@ end local CMD = {} -local function unforward(c) - if c.agent then - forwarding[c.agent] = nil - c.agent = nil - c.client = nil - end -end - function CMD.forward(source, fd, client, address) local c = assert(connection[fd]) unforward(c) From 75b2feff738fca4f5781d705faff3ecefdd5bd47 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Jul 2014 17:53:53 +0800 Subject: [PATCH 100/729] fix typo --- lualib/socketchannel.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index e953fe8f..4b2f28ad 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -157,7 +157,7 @@ local function connect_backup(self) if self.__backup then for _, addr in ipairs(self.__backup) do local host, port - if type(host) == "table" then + if type(addr) == "table" then host, port = addr.host, addr.port else host = addr From 7d2d107518e753698fb2a1a2b461efd73543e5e9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Jul 2014 17:06:47 +0800 Subject: [PATCH 101/729] bugfix: socketchannel auth --- lualib/socketchannel.lua | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 4b2f28ad..c2886e31 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -177,6 +177,9 @@ local function connect_backup(self) end local function connect_once(self) + if self.__closed then + return false + end assert(not self.__sock and not self.__authcoroutine) local fd = socket.open(self.__host, self.__port) if not fd then @@ -185,29 +188,29 @@ local function connect_once(self) return false end end - while not self.__closed do + + self.__sock = setmetatable( {fd} , channel_socket_meta ) + skynet.fork(dispatch_function(self), self) + + if self.__auth then self.__authcoroutine = coroutine.running() - self.__sock = setmetatable( {fd} , channel_socket_meta ) - skynet.fork(dispatch_function(self), self) - - if self.__auth then - local ok , message = pcall(self.__auth, self) - if not ok then - close_channel_socket(self) - if message ~= socket_error then - skynet.error("socket: auth failed", message) - end + local ok , message = pcall(self.__auth, self) + if not ok then + close_channel_socket(self) + if message ~= socket_error then + skynet.error("socket: auth failed", message) end - self.__authcoroutine = false - return ok end - self.__authcoroutine = false if self.__sock then - return true + return ok + else + -- auth may change host, so connect again + return connect_once(self) end - -- auth may change host, so connect again end + + return true end local function try_connect(self , once) From 6bafd05c658cbce7455399b3cb84e52410fb3253 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Jul 2014 17:35:36 +0800 Subject: [PATCH 102/729] don't support ip:port --- lualib/socketchannel.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index c2886e31..62818613 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -161,9 +161,7 @@ local function connect_backup(self) host, port = addr.host, addr.port else host = addr - if not addr:find(":", 1, true) then - port = self.__port - end + port = self.__port end skynet.error("socket: connect to backup host", host, port) local fd = socket.open(host, port) @@ -198,6 +196,7 @@ local function connect_once(self) if not ok then close_channel_socket(self) if message ~= socket_error then + self.__authcoroutine = false skynet.error("socket: auth failed", message) end end From a0b536718ccd8541917631deab07daa1f69b5cc5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Jul 2014 21:41:08 +0800 Subject: [PATCH 103/729] config can read ENV --- skynet-src/skynet_main.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 9c38fd25..4eb18a59 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -11,6 +11,7 @@ #include #include #include +#include static int optint(const char *key, int opt) { @@ -51,7 +52,6 @@ optstring(const char *key,const char * opt) { static void _init_env(lua_State *L) { - lua_pushglobaltable(L); lua_pushnil(L); /* first key */ while (lua_next(L, -2) != 0) { int keyt = lua_type(L, -2); @@ -83,6 +83,19 @@ int sigign() { return 0; } +static const char * load_config = "\ + local config_name = ...\ + local f = assert(io.open(config_name))\ + local code = assert(f:read \'*a\')\ + local function getenv(name) return assert(os.getenv(name), name) end\ + code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\ + print(code)\ + f:close()\ + local result = {}\ + assert(load(code,\'=(load)\',\'t\',result))()\ + return result\ +"; + int main(int argc, char *argv[]) { const char * config_file = "config"; @@ -98,11 +111,12 @@ main(int argc, char *argv[]) { struct lua_State *L = lua_newstate(skynet_lalloc, NULL); luaL_openlibs(L); // link lua lib - lua_close(L); - L = luaL_newstate(); - - int err = luaL_dofile(L, config_file); + int err = luaL_loadstring(L, load_config); + assert(err == LUA_OK); + lua_pushstring(L, config_file); + + err = lua_pcall(L, 1, 1, 0); if (err) { fprintf(stderr,"%s\n",lua_tostring(L,-1)); lua_close(L); From 8e4e9ed5c183291edbbce2f16497eef8a9f0400f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Jul 2014 21:58:09 +0800 Subject: [PATCH 104/729] bugfix: connect auth --- lualib/socketchannel.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 62818613..1bfa06f5 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -203,10 +203,11 @@ local function connect_once(self) self.__authcoroutine = false if self.__sock then return ok - else + elseif ok then -- auth may change host, so connect again return connect_once(self) end + return ok end return true From e1ca48603ee583a3fc61258e8cd1d9bec889b0ab Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Jul 2014 22:00:48 +0800 Subject: [PATCH 105/729] simplify code --- lualib/socketchannel.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 1bfa06f5..45aeea54 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -201,9 +201,7 @@ local function connect_once(self) end end self.__authcoroutine = false - if self.__sock then - return ok - elseif ok then + if ok and not self.__sock then -- auth may change host, so connect again return connect_once(self) end From d41c9db019ccbc8a70eb221f897184b919d9d8e4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 18 Jul 2014 12:09:05 +0800 Subject: [PATCH 106/729] bugfix: update the response fd, when the client reconnect --- examples/login/client.lua | 57 +++++++++++++++++++++---------------- examples/login/msgagent.lua | 1 + lualib/snax/msgserver.lua | 53 +++++++++++++++++++++++----------- 3 files changed, 70 insertions(+), 41 deletions(-) diff --git a/examples/login/client.lua b/examples/login/client.lua index 82b0a182..0ebed6a7 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -70,13 +70,10 @@ print("login ok, subid=", subid) ----- connect to game server -local session = 0 - -local function send_request(v) - session = session + 1 +local function send_request(v, session) local s = string.char(bit32.extract(session,24,8), bit32.extract(session,16,8), bit32.extract(session,8,8), bit32.extract(session,0,8)) socket.send(fd , v..s) - return session, v + return v, session end local function recv_response(v) @@ -87,7 +84,7 @@ local function recv_response(v) local c = v:byte(i) session = session + bit32.lshift(c,(-1-i) * 8) end - return ok ~=0 , session, content + return ok ~=0 , content, session end local input = {} @@ -115,32 +112,44 @@ local function readpackage() end end +local text = "echo" local index = 1 -local function echo(text) - print("connect") - local fd = assert(socket.connect("127.0.0.1", 8888)) - input = {} +print("connect") +local fd = assert(socket.connect("127.0.0.1", 8888)) +input = {} - local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) - local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) +local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) +local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) - socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) +socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) - print(readpackage()) - print("===>",send_request(text)) - print("===>",send_request(text .. " again")) - print("<===",recv_response(readpackage())) - print("<===",recv_response(readpackage())) +print(readpackage()) +print("===>",send_request(text,0)) +-- don't recv response +-- print("<===",recv_response(readpackage())) - print("disconnect") - socket.close(fd) -end - -echo "hello" +print("disconnect") +socket.close(fd) index = index + 1 -echo "world" +print("connect again") +local fd = assert(socket.connect("127.0.0.1", 8888)) +input = {} + +local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) +local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) + +socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) + +print(readpackage()) +print("===>",send_request("fake",0)) -- request again (use last session 0, so the request message is fake) +print("===>",send_request("again",1)) -- request again (use new session) +print("<===",recv_response(readpackage())) +print("<===",recv_response(readpackage())) +print("disconnect") +socket.close(fd) + diff --git a/examples/login/msgagent.lua b/examples/login/msgagent.lua index 90b4ac08..fc3a2d5b 100644 --- a/examples/login/msgagent.lua +++ b/examples/login/msgagent.lua @@ -47,6 +47,7 @@ skynet.start(function() skynet.dispatch("client", function(_,_, msg) -- the simple ehco service + skynet.sleep(10) -- sleep a while skynet.ret(msg) end) end) diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua index d1a84f3a..f6804a60 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -212,18 +212,21 @@ function server.start(conf) local request_handler = assert(conf.request_handler) - -- u.response is a struct { message, version, index } + -- u.response is a struct { return_fd , response, version, index } local function retire_response(u) if u.index >= expired_number * 2 then local max = 0 local response = u.response for k,p in pairs(response) do - if p[3] < expired_number then - response[k] = nil - else - p[3] = p[3] - expired_number - if p[3] > max then - max = p[3] + if p[1] == nil then + -- request complete, check expired + if p[4] < expired_number then + response[k] = nil + else + p[4] = p[4] - expired_number + if p[4] > max then + max = p[4] + end end end end @@ -238,14 +241,23 @@ function server.start(conf) local p = u.response[session] if p then -- session can be reuse in the same connection - if p[2] == u.version then + if p[3] == u.version then + local last = u.response[session] u.response[session] = nil p = nil + if last[2] == nil then + local error_msg = string.format("Conflict session %s", crypt.hexencode(session)) + skynet.error(error_msg) + error(error_msg) + end end end if p == nil then + p = { fd } + u.response[session] = p local ok, result = pcall(conf.request_handler, u.username, msg, msg_sz) + result = result or "" -- NOTICE: YIELD here, socket may close. if not ok then skynet.error(result) @@ -254,21 +266,28 @@ function server.start(conf) result = result .. '\1' .. session end - p = { netpack.pack_string(result), u.version, u.index } - if u.response[session] then - skynet.error(string.format("Conflict session %s", crypt.hexencode(session))) - end - u.response[session] = p + p[2] = netpack.pack_string(result) + p[3] = u.version + p[4] = u.index else netpack.tostring(msg, sz) -- request before, so free msg - -- resend response, and update index p[3]. - p[3] = u.index + -- update version/index, change return fd. + -- resend response. + p[1] = fd + p[3] = u.version + p[4] = u.index + if p[2] == nil then + -- already request, but response is not ready + return + end end u.index = u.index + 1 - -- check connect again + -- the return fd is p[1] (fd may change by multi request) check connect + fd = p[1] if connection[fd] then - socketdriver.send(fd, p[1]) + socketdriver.send(fd, p[2]) end + p[1] = nil retire_response(u) end From a8b80cd73ee1c09e050a182fb69ca8f163253b4d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 19 Jul 2014 14:22:24 +0800 Subject: [PATCH 107/729] simplify clientsocket lib --- HISTORY.md | 1 + examples/client.lua | 74 ++++++++++++------ examples/login/client.lua | 124 +++++++++++++++++++----------- examples/simpledb.lua | 6 +- lualib-src/lua-clientsocket.c | 141 +++------------------------------- lualib/snax/loginserver.lua | 32 ++++++-- 6 files changed, 171 insertions(+), 207 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 5c52c767..a769e314 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,7 @@ Dev version ----------- * skynet.exit will quit service immediately. * Add snax.gateserver, snax.loginserver, snax.msgserver +* Simplify clientsocket lib v0.4.2 (2014-7-14) ----------- diff --git a/examples/client.lua b/examples/client.lua index 278922e6..d5bace7f 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -2,30 +2,47 @@ package.cpath = "luaclib/?.so" local socket = require "clientsocket" local cjson = require "cjson" +local bit32 = require "bit32" -local fd = socket.connect("127.0.0.1", 8888) +local fd = assert(socket.connect("127.0.0.1", 8888)) -local last -local result = {} +local function send_package(fd, pack) + local size = #pack + local package = string.format("%c%c%s", + bit32.extract(size,8,8), + bit32.extract(size,0,8), + pack) -local function dispatch() - while true do - local status - status, last = socket.recv(fd, last, result) - if status == nil then - error "Server closed" - end - if not status then - break - end - for _, v in ipairs(result) do - local session,t,str = string.match(v, "(%d+)(.)(.*)") - assert(t == '-' or t == '+') - session = tonumber(session) - local result = cjson.decode(str) - print("Response:",session, result[1], result[2]) - end + socket.send(fd, package) +end + +local function unpack_package(text) + local size = #text + if size < 2 then + return nil, text end + local s = text:byte(1) * 256 + text:byte(2) + if size < s+2 then + return nil, text + end + + return text:sub(3,2+s), text:sub(3+s) +end + +local function recv_package(last) + local result + result, last = unpack_package(last) + if result then + return result, last + end + local r = socket.recv(fd) + if not r then + return nil, last + end + if r == "" then + error "Server closed" + end + return unpack_package(last .. r) end local session = 0 @@ -33,12 +50,25 @@ local session = 0 local function send_request(v) session = session + 1 local str = string.format("%d+%s",session, cjson.encode(v)) - socket.send(fd, str) + send_package(fd, str) print("Request:", session) end +local last = "" + while true do - dispatch() + while true do + local v + v, last = recv_package(last) + if not v then + break + end + local session,t,str = string.match(v, "(%d+)(.)(.*)") + assert(t == '-' or t == '+') + session = tonumber(session) + local result = cjson.decode(str) + print("Response:",session, result[1], result[2]) + end local cmd = socket.readstdin() if cmd then local args = {} diff --git a/examples/login/client.lua b/examples/login/client.lua index 0ebed6a7..dfd66267 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -2,44 +2,65 @@ package.cpath = "luaclib/?.so" local socket = require "clientsocket" local crypt = require "crypt" +local bit32 = require "bit32" -local last local fd = assert(socket.connect("127.0.0.1", 8001)) -local input = {} -local function readline() - local line = table.remove(input, 1) - if line then - return line +local function writeline(fd, text) + socket.send(fd, text .. "\n") +end + +local function unpack_line(text) + local from = text:find("\n", 1, true) + if from then + return text:sub(1, from-1), text:sub(from+1) end + return nil, text +end - while true do - local status - status, last = socket.readline(fd, last, input) - if status == nil then +local last = "" + +local function unpack_f(f) + local function try_recv(fd, last) + local result + result, last = f(last) + if result then + return result, last + end + local r = socket.recv(fd) + if not r then + return nil, last + end + if r == "" then error "Server closed" end - if not status then - socket.usleep(100) - else - local line = table.remove(input, 1) - if line then - return line + return f(last .. r) + end + + return function() + while true do + local result + result, last = try_recv(fd, last) + if result then + return result end + socket.usleep(100) end end end +local readline = unpack_f(unpack_line) + local challenge = crypt.base64decode(readline()) local clientkey = crypt.randomkey() -socket.writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey))) +writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey))) local secret = crypt.dhsecret(crypt.base64decode(readline()), clientkey) print("sceret is ", crypt.hexencode(secret)) local hmac = crypt.hmac64(challenge, secret) -socket.writeline(fd, crypt.base64encode(hmac)) +writeline(fd, crypt.base64encode(hmac)) local token = { server = "sample", @@ -56,7 +77,7 @@ end local etoken = crypt.desencode(secret, encode_token(token)) local b = crypt.base64encode(etoken) -socket.writeline(fd, crypt.base64encode(etoken)) +writeline(fd, crypt.base64encode(etoken)) local result = readline() print(result) @@ -71,8 +92,18 @@ print("login ok, subid=", subid) ----- connect to game server local function send_request(v, session) - local s = string.char(bit32.extract(session,24,8), bit32.extract(session,16,8), bit32.extract(session,8,8), bit32.extract(session,0,8)) - socket.send(fd , v..s) + local size = #v + 4 + local package = string.format("%c%c%s%c%c%c%c", + bit32.extract(size,8,8), + bit32.extract(size,0,8), + v, + bit32.extract(session,24,8), + bit32.extract(session,16,8), + bit32.extract(session,8,8), + bit32.extract(session,0,8) + ) + + socket.send(fd, package) return v, session end @@ -87,29 +118,29 @@ local function recv_response(v) return ok ~=0 , content, session end -local input = {} - -local function readpackage() - local line = table.remove(input, 1) - if line then - return line +local function unpack_package(text) + local size = #text + if size < 2 then + return nil, text + end + local s = text:byte(1) * 256 + text:byte(2) + if size < s+2 then + return nil, text end - while true do - local status - status, last = socket.recv(fd, last, input) - if status == nil then - error "Server closed" - end - if not status then - socket.usleep(100) - else - local line = table.remove(input, 1) - if line then - return line - end - end - end + return text:sub(3,2+s), text:sub(3+s) +end + +local readpackage = unpack_f(unpack_package) + +local function send_package(fd, pack) + local size = #pack + local package = string.format("%c%c%s", + bit32.extract(size,8,8), + bit32.extract(size,0,8), + pack) + + socket.send(fd, package) end local text = "echo" @@ -117,12 +148,13 @@ local index = 1 print("connect") local fd = assert(socket.connect("127.0.0.1", 8888)) -input = {} +last = "" local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) -socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) + +send_package(fd, handshake .. ":" .. crypt.base64encode(hmac)) print(readpackage()) print("===>",send_request(text,0)) @@ -136,12 +168,12 @@ index = index + 1 print("connect again") local fd = assert(socket.connect("127.0.0.1", 8888)) -input = {} +last = "" local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) -socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac)) +send_package(fd, handshake .. ":" .. crypt.base64encode(hmac)) print(readpackage()) print("===>",send_request("fake",0)) -- request again (use last session 0, so the request message is fake) diff --git a/examples/simpledb.lua b/examples/simpledb.lua index a11fd8c5..3a7649e6 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -16,7 +16,11 @@ end skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) local f = command[string.upper(cmd)] - skynet.ret(skynet.pack(f(...))) + if f then + skynet.ret(skynet.pack(f(...))) + else + error(string.format("Unknown command %s", tostring(cmd))) + end end) skynet.register "SIMPLEDB" end) diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index 2676c267..89bfe1f8 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -75,47 +75,12 @@ lsend(lua_State *L) { size_t sz = 0; int fd = luaL_checkinteger(L,1); const char * msg = luaL_checklstring(L, 2, &sz); - uint8_t tmp[sz + 2]; - if (sz >= 0x10000) { - return luaL_error(L, "package too long %d (16bit limited)", (int)sz); - } - tmp[0] = (sz >> 8) & 0xff; - tmp[1] = sz & 0xff; - memcpy(tmp+2, msg, sz); - block_send(L, fd, (const char *)tmp, (int)sz+2); + block_send(L, fd, msg, (int)sz); return 0; } - -static int -unpack(lua_State *L, uint8_t *buffer, int sz, int n) { - int size = 0; - if (sz >= 2) { - size = buffer[0] << 8 | buffer[1]; - if (size > sz - 2) { - goto _block; - } - } else { - goto _block; - } - ++n; - lua_pushlstring(L, (const char *)buffer+2, size); - lua_rawseti(L, 3, n); - buffer += size + 2; - sz -= size + 2; - return unpack(L, buffer, sz, n); -_block: - lua_pushboolean(L, n==0 ? 0:1); - if (sz == 0) { - lua_pushnil(L); - } else { - lua_pushlstring(L, (const char *)buffer, sz); - } - return 2; -} - /* intger fd string last @@ -132,100 +97,26 @@ struct socket_buffer { }; static int -recv_socket(lua_State *L, char *tmp, struct socket_buffer *result) { +lrecv(lua_State *L) { int fd = luaL_checkinteger(L,1); - size_t sz = 0; - const char * last = lua_tolstring(L,2,&sz); - luaL_checktype(L, 3, LUA_TTABLE); - char * buffer; - int r = recv(fd, tmp, CACHE_SIZE, 0); + char buffer[CACHE_SIZE]; + int r = recv(fd, buffer, CACHE_SIZE, 0); if (r == 0) { + lua_pushliteral(L, ""); // close - return 0; + return 1; } if (r < 0) { if (errno == EAGAIN || errno == EINTR) { - lua_pushboolean(L, 0); - lua_pushvalue(L, 2); - return 2; + return 0; } luaL_error(L, "socket error: %s", strerror(errno)); } - if (sz + r <= CACHE_SIZE) { - buffer = tmp; - memmove(buffer + sz, buffer, r); - memcpy(buffer, last, sz); - } else { - buffer = lua_newuserdata(L, r + sz); - memcpy(buffer, last, sz); - memcpy(buffer + sz, tmp, r); - } - - int i; - int n = lua_rawlen(L, 3); - for (i=1;i<=n;i++) { - lua_pushnil(L); - lua_rawseti(L, 3, i); - } - result->buffer = buffer; - result->sz = r + sz; - return -1; + lua_pushlstring(L, buffer, r); + return 1; } -static int -lrecv(lua_State *L) { - struct socket_buffer sb; - char tmp[CACHE_SIZE]; - int ret = recv_socket(L, tmp, &sb); - if (ret < 0) { - return unpack(L, sb.buffer, sb.sz, 0); - } else { - return ret; - } -} - -static int -unpack_line(lua_State *L, uint8_t *buffer, int sz, int n) { - if (sz == 0) - goto _block; - if (buffer[0] == '\n') { - return unpack_line(L, buffer+1, sz-1, n); - } - int i; - for (i=1;i Date: Mon, 21 Jul 2014 11:47:38 +0800 Subject: [PATCH 108/729] mongo driver support rs --- lualib/mongo.lua | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 9760a545..aafea055 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -75,23 +75,51 @@ local function dispatch_reply(so) return reply_id, succ, result end +local function __parse_addr(addr) + local host, port = string.match(addr, "([^:]+):(.+)") + return host, tonumber(port) +end + local function mongo_auth(mongoc) local user = rawget(mongoc, "username") local pass = rawget(mongoc, "password") - if user == nil or pass == nil then - return - end return function() - assert(mongoc:auth(user, pass)) - -- todo : If mongoc want to connect to replica set, runCommand ismater here, and call mongoc.__sock:changehost + if user ~= nil and pass ~= nil then + assert(mongoc:auth(user, pass)) + end + local rs_data = mongoc:runCommand("ismaster") + if rs_data.ok == 1 then + if rs_data.hosts then + local backup = {} + for _, v in ipairs(rs_data.hosts) do + local host, port = __parse_addr(v) + table.insert(backup, {host = host, port = port}) + end + mongoc.__sock:changebackup(backup) + end + if rs_data.ismaster then + return + else + local host, port = __parse_addr(rs_data.primary) + mongoc.host = host + mongoc.port = port + mongoc.__sock:changehost(host, port) + end + end end end function mongo.client( conf ) + local first = conf + local backup = nil + if conf.rs then + first = conf.rs[1] + backup = conf.rs + end local obj = { - host = conf.host, - port = conf.port or 27017, + host = first.host, + port = first.port or 27017, } obj.__id = 0 @@ -100,6 +128,7 @@ function mongo.client( conf ) port = obj.port, response = dispatch_reply, auth = mongo_auth(obj), + backup = backup, } setmetatable(obj, client_meta) obj.__sock:connect(true) -- try connect only once From 39ff6eb2a66d052436d6095595debb73ae81593e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 21 Jul 2014 14:27:50 +0800 Subject: [PATCH 109/729] space to tab --- lualib/mongo.lua | 228 +++++++++++++++++++++++------------------------ 1 file changed, 114 insertions(+), 114 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index aafea055..b1e87ca7 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -1,123 +1,123 @@ local bson = require "bson" local socket = require "socket" -local socketchannel = require "socketchannel" +local socketchannel = require "socketchannel" local skynet = require "skynet" local driver = require "mongo.driver" -local md5 = require "md5" +local md5 = require "md5" local rawget = rawget local assert = assert -local bson_encode = bson.encode -local bson_encode_order = bson.encode_order -local bson_decode = bson.decode +local bson_encode = bson.encode +local bson_encode_order = bson.encode_order +local bson_decode = bson.decode local empty_bson = bson_encode {} -local mongo = {} +local mongo = {} mongo.null = assert(bson.null) mongo.maxkey = assert(bson.maxkey) mongo.minkey = assert(bson.minkey) mongo.type = assert(bson.type) local mongo_cursor = {} -local cursor_meta = { - __index = mongo_cursor, +local cursor_meta = { + __index = mongo_cursor, } local mongo_client = {} -local client_meta = { - __index = function(self, key) - return rawget(mongo_client, key) or self:getDB(key) +local client_meta = { + __index = function(self, key) + return rawget(mongo_client, key) or self:getDB(key) end, __tostring = function (self) local port_string if self.port then - port_string = ":" .. tostring(self.port) + port_string = ":" .. tostring(self.port) else - port_string = "" + port_string = "" end - return "[mongo client : " .. self.host .. port_string .."]" + return "[mongo client : " .. self.host .. port_string .."]" end, - -- DO NOT need disconnect, because channel will shutdown during gc + -- DO NOT need disconnect, because channel will shutdown during gc } local mongo_db = {} -local db_meta = { - __index = function (self, key) - return rawget(mongo_db, key) or self:getCollection(key) +local db_meta = { + __index = function (self, key) + return rawget(mongo_db, key) or self:getCollection(key) end, __tostring = function (self) - return "[mongo db : " .. self.name .. "]" + return "[mongo db : " .. self.name .. "]" end } local mongo_collection = {} -local collection_meta = { - __index = function(self, key) - return rawget(mongo_collection, key) or self:getCollection(key) - end , +local collection_meta = { + __index = function(self, key) + return rawget(mongo_collection, key) or self:getCollection(key) + end , __tostring = function (self) - return "[mongo collection : " .. self.full_name .. "]" + return "[mongo collection : " .. self.full_name .. "]" end } local function dispatch_reply(so) - local len_reply = so:read(4) - local reply = so:read(driver.length(len_reply)) - local result = { result = {} } - local succ, reply_id, document, cursor_id, startfrom = driver.reply(reply, result.result) - result.document = document + local len_reply = so:read(4) + local reply = so:read(driver.length(len_reply)) + local result = { result = {} } + local succ, reply_id, document, cursor_id, startfrom = driver.reply(reply, result.result) + result.document = document result.cursor_id = cursor_id result.startfrom = startfrom - result.data = reply + result.data = reply return reply_id, succ, result end local function __parse_addr(addr) - local host, port = string.match(addr, "([^:]+):(.+)") - return host, tonumber(port) + local host, port = string.match(addr, "([^:]+):(.+)") + return host, tonumber(port) end local function mongo_auth(mongoc) - local user = rawget(mongoc, "username") - local pass = rawget(mongoc, "password") + local user = rawget(mongoc, "username") + local pass = rawget(mongoc, "password") return function() - if user ~= nil and pass ~= nil then - assert(mongoc:auth(user, pass)) - end - local rs_data = mongoc:runCommand("ismaster") - if rs_data.ok == 1 then - if rs_data.hosts then - local backup = {} - for _, v in ipairs(rs_data.hosts) do - local host, port = __parse_addr(v) - table.insert(backup, {host = host, port = port}) - end - mongoc.__sock:changebackup(backup) - end - if rs_data.ismaster then - return - else - local host, port = __parse_addr(rs_data.primary) - mongoc.host = host - mongoc.port = port - mongoc.__sock:changehost(host, port) - end - end + if user ~= nil and pass ~= nil then + assert(mongoc:auth(user, pass)) + end + local rs_data = mongoc:runCommand("ismaster") + if rs_data.ok == 1 then + if rs_data.hosts then + local backup = {} + for _, v in ipairs(rs_data.hosts) do + local host, port = __parse_addr(v) + table.insert(backup, {host = host, port = port}) + end + mongoc.__sock:changebackup(backup) + end + if rs_data.ismaster then + return + else + local host, port = __parse_addr(rs_data.primary) + mongoc.host = host + mongoc.port = port + mongoc.__sock:changehost(host, port) + end + end end end -function mongo.client( conf ) - local first = conf - local backup = nil - if conf.rs then - first = conf.rs[1] - backup = conf.rs - end - local obj = { +function mongo.client( conf ) + local first = conf + local backup = nil + if conf.rs then + first = conf.rs[1] + backup = conf.rs + end + local obj = { host = first.host, port = first.port or 27017, } @@ -128,10 +128,10 @@ function mongo.client( conf ) port = obj.port, response = dispatch_reply, auth = mongo_auth(obj), - backup = backup, + backup = backup, } setmetatable(obj, client_meta) - obj.__sock:connect(true) -- try connect only once + obj.__sock:connect(true) -- try connect only once return obj end @@ -139,32 +139,32 @@ function mongo_client:getDB(dbname) local db = { connection = self, name = dbname, - full_name = dbname, + full_name = dbname, database = false, - __cmd = dbname .. "." .. "$cmd", + __cmd = dbname .. "." .. "$cmd", } - db.database = db + db.database = db - return setmetatable(db, db_meta) + return setmetatable(db, db_meta) end function mongo_client:disconnect() if self.__sock then local so = self.__sock - self.__sock = false + self.__sock = false so:close() end end function mongo_client:genId() local id = self.__id + 1 - self.__id = id + self.__id = id return id end function mongo_client:runCommand(...) if not self.admin then - self.admin = self:getDB "admin" + self.admin = self:getDB "admin" end return self.admin:runCommand(...) end @@ -176,14 +176,14 @@ function mongo_client:auth(user,password) return false end - local key = md5.sumhexa(string.format("%s%s%s",result.nonce,user,password)) + local key = md5.sumhexa(string.format("%s%s%s",result.nonce,user,password)) local result= self:runCommand ("authenticate",1,"user",user,"nonce",result.nonce,"key",key) - return result.ok == 1 + return result.ok == 1 end function mongo_client:logout() local result = self:runCommand "logout" - return result.ok == 1 + return result.ok == 1 end function mongo_db:runCommand(cmd,cmd_v,...) @@ -196,18 +196,18 @@ function mongo_db:runCommand(cmd,cmd_v,...) else bson_cmd = bson_encode_order(cmd,cmd_v,...) end - local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd) - -- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data) - local req = sock:request(pack, request_id) - local doc = req.document + local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd) + -- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data) + local req = sock:request(pack, request_id) + local doc = req.document return bson_decode(doc) end function mongo_db:getCollection(collection) - local col = { + local col = { connection = self.connection, name = collection, - full_name = self.full_name .. "." .. collection, + full_name = self.full_name .. "." .. collection, database = self.database, } self[collection] = setmetatable(col, collection_meta) @@ -218,20 +218,20 @@ mongo_collection.getCollection = mongo_db.getCollection function mongo_collection:insert(doc) if doc._id == nil then - doc._id = bson.objectid() + doc._id = bson.objectid() end local sock = self.connection.__sock local pack = driver.insert(0, self.full_name, bson_encode(doc)) - -- flags support 1: ContinueOnError + -- flags support 1: ContinueOnError sock:request(pack) end function mongo_collection:batch_insert(docs) - for i=1,#docs do + for i=1,#docs do if docs[i]._id == nil then - docs[i]._id = bson.objectid() + docs[i]._id = bson.objectid() end - docs[i] = bson_encode(docs[i]) + docs[i] = bson_encode(docs[i]) end local sock = self.connection.__sock local pack = driver.insert(0, self.full_name, docs) @@ -239,7 +239,7 @@ function mongo_collection:batch_insert(docs) end function mongo_collection:update(selector,update,upsert,multi) - local flags = (upsert and 1 or 0) + (multi and 2 or 0) + local flags = (upsert and 1 or 0) + (multi and 2 or 0) local sock = self.connection.__sock local pack = driver.update(self.full_name, flags, bson_encode(selector), bson_encode(update)) sock:request(pack) @@ -255,24 +255,24 @@ function mongo_collection:findOne(query, selector) local conn = self.connection local request_id = conn:genId() local sock = conn.__sock - local pack = driver.query(request_id, 0, self.full_name, 0, 1, query and bson_encode(query) or empty_bson, selector and bson_encode(selector)) - -- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data) - local req = sock:request(pack, request_id) - local doc = req.document + local pack = driver.query(request_id, 0, self.full_name, 0, 1, query and bson_encode(query) or empty_bson, selector and bson_encode(selector)) + -- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data) + local req = sock:request(pack, request_id) + local doc = req.document return bson_decode(doc) end function mongo_collection:find(query, selector) return setmetatable( { __collection = self, - __query = query and bson_encode(query) or empty_bson, + __query = query and bson_encode(query) or empty_bson, __selector = selector and bson_encode(selector), - __ptr = nil, + __ptr = nil, __data = nil, __cursor = nil, __document = {}, - __flags = 0, - } , cursor_meta) + __flags = 0, + } , cursor_meta) end function mongo_cursor:hasNext() @@ -285,42 +285,42 @@ function mongo_cursor:hasNext() local sock = conn.__sock local pack if self.__data == nil then - pack = driver.query(request_id, self.__flags, self.__collection.full_name,0,0,self.__query,self.__selector) + pack = driver.query(request_id, self.__flags, self.__collection.full_name,0,0,self.__query,self.__selector) else if self.__cursor then pack = driver.more(request_id, self.__collection.full_name,0,self.__cursor) else -- no more - self.__document = nil - self.__data = nil + self.__document = nil + self.__data = nil return false end end local ok, result = pcall(sock.request,sock,pack, request_id) - local doc = result.document + local doc = result.document local cursor = result.cursor_id if ok then if doc then - self.__document = result.result - self.__data = result.data + self.__document = result.result + self.__data = result.data self.__ptr = 1 - self.__cursor = cursor + self.__cursor = cursor return true else - self.__document = nil - self.__data = nil - self.__cursor = nil + self.__document = nil + self.__data = nil + self.__cursor = nil return false end else - self.__document = nil - self.__data = nil - self.__cursor = nil + self.__document = nil + self.__data = nil + self.__cursor = nil if doc then - local err = bson_decode(doc) + local err = bson_decode(doc) error(err["$err"]) else error("Reply from mongod error") @@ -333,11 +333,11 @@ end function mongo_cursor:next() if self.__ptr == nil then - error "Call hasNext first" + error "Call hasNext first" end - local r = bson_decode(self.__document[self.__ptr]) - self.__ptr = self.__ptr + 1 - if self.__ptr > #self.__document then + local r = bson_decode(self.__document[self.__ptr]) + self.__ptr = self.__ptr + 1 + if self.__ptr > #self.__document then self.__ptr = nil end From 27ac1642e4cd2bf58734be52acd32d0024c2bfd1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 21 Jul 2014 14:29:11 +0800 Subject: [PATCH 110/729] update history --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index a769e314..3d3c26b7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,7 @@ Dev version * skynet.exit will quit service immediately. * Add snax.gateserver, snax.loginserver, snax.msgserver * Simplify clientsocket lib +* mongo driver support replica set v0.4.2 (2014-7-14) ----------- From 7bdbdcd0544c159ac0e0d3913f29b87cdccc96d6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 21 Jul 2014 14:33:29 +0800 Subject: [PATCH 111/729] update history --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index 3d3c26b7..615bc346 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,6 +4,7 @@ Dev version * Add snax.gateserver, snax.loginserver, snax.msgserver * Simplify clientsocket lib * mongo driver support replica set +* config file support read from ENV v0.4.2 (2014-7-14) ----------- From 362d6822bf6d16ea73b93b0cf34229ddb4492293 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 21 Jul 2014 14:37:22 +0800 Subject: [PATCH 112/729] remove dup line --- lualib-src/lua-bson.c | 1 - 1 file changed, 1 deletion(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 07fb77e2..70619340 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1180,7 +1180,6 @@ luaopen_bson(lua_State *L) { { "timestamp", ltimestamp }, { "regex", lregex }, { "binary", lbinary }, - { "regex", lregex }, { "objectid", lobjectid }, { "decode", ldecode }, { NULL, NULL }, From 932b2943dc270386ce4a5d5d85f4a24bd3c87e0a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 22 Jul 2014 21:38:44 +0800 Subject: [PATCH 113/729] simple httpd --- examples/simpleweb.lua | 50 +++++++++ lualib/http/httpd.lua | 190 +++++++++++++++++++++++++++++++++++ lualib/http/sockethelper.lua | 45 +++++++++ 3 files changed, 285 insertions(+) create mode 100644 examples/simpleweb.lua create mode 100644 lualib/http/httpd.lua create mode 100644 lualib/http/sockethelper.lua diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua new file mode 100644 index 00000000..f0c2855b --- /dev/null +++ b/examples/simpleweb.lua @@ -0,0 +1,50 @@ +local skynet = require "skynet" +local socket = require "socket" +local httpd = require "http.httpd" +local sockethelper = require "http.sockethelper" + +local mode = ... + +if mode == "agent" then + +skynet.start(function() + skynet.dispatch("lua", function (_,_,id) + socket.start(id) + local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id)) + if code then + if code ~= 200 then + httpd.write_response(sockethelper.writefunc(id), code) + else + httpd.write_response(sockethelper.writefunc(id), code , "Hello world") + end + else + if url == sockethelper.socket_error then + skynet.error("socket closed") + else + skynet.error(url) + end + end + socket.close(id) + end) +end) + +else + +skynet.start(function() + local agent = {} + for i= 1, 20 do + agent[i] = skynet.newservice(SERVICE_NAME, "agent") + end + local balance = 1 + local id = socket.listen("0.0.0.0", 8001) + socket.start(id , function(id, addr) + skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance])) + skynet.send(agent[balance], "lua", id) + balance = balance + 1 + if balance > #agent then + balance = 1 + end + end) +end) + +end \ No newline at end of file diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua new file mode 100644 index 00000000..4eb9ab40 --- /dev/null +++ b/lualib/http/httpd.lua @@ -0,0 +1,190 @@ +local table = table + +local httpd = {} +local READLIMIT = 8192 -- limit bytes per read + +local http_status_msg = { + [100] = "Continue", + [101] = "Switching Protocols", + [200] = "OK", + [201] = "Created", + [202] = "Accepted", + [203] = "Non-Authoritative Information", + [204] = "No Content", + [205] = "Reset Content", + [206] = "Partial Content", + [300] = "Multiple Choices", + [301] = "Moved Permanently", + [302] = "Found", + [303] = "See Other", + [304] = "Not Modified", + [305] = "Use Proxy", + [307] = "Temporary Redirect", + [400] = "Bad Request", + [401] = "Unauthorized", + [402] = "Payment Required", + [403] = "Forbidden", + [404] = "Not Found", + [405] = "Method Not Allowed", + [406] = "Not Acceptable", + [407] = "Proxy Authentication Required", + [408] = "Request Time-out", + [409] = "Conflict", + [410] = "Gone", + [411] = "Length Required", + [412] = "Precondition Failed", + [413] = "Request Entity Too Large", + [414] = "Request-URI Too Large", + [415] = "Unsupported Media Type", + [416] = "Requested range not satisfiable", + [417] = "Expectation Failed", + [500] = "Internal Server Error", + [501] = "Not Implemented", + [502] = "Bad Gateway", + [503] = "Service Unavailable", + [504] = "Gateway Time-out", + [505] = "HTTP Version not supported", +} + +local function recvheader(readline, header) + local line = readline() + if line == "" then + return header + end + + header = header or {} + + local name, value + repeat + if line:byte(1) == 9 then -- tab, append last line + header[name] = header[name] .. line:sub(2) + else + name, value = line:match "^(.-):%s*(.*)" + assert(name and value) + name = name:lower() + if header[name] then + header[name] = header[name] .. ", " .. value + else + header[name] = value + end + line = readline() + end + until line == "" + + return header +end + +local function recvbody(readbytes, length) + if length < READLIMIT then + return readbytes(length) + end + local tmp = {} + while true do + if length <= READLIMIT then + table.insert(tmp, readbytes(length)) + break + end + table.insert(tmp, readbytes(READLIMIT)) + length = length - READLIMIT + end + return table.concat(tmp) +end + +local function recvchunkedbody(readline, readbytes, header) + local size = assert(tonumber(readline(),16)) + local body = recvbody(readbytes,size) + assert(readbytes(2) == "\r\n") + size = assert(tonumber(readline(),16)) + if size > 0 then + local bodys = { body } + repeat + table.insert(bodys, recvbody(readbytes,size)) + assert(readbytes(2) == "\r\n") + size = assert(tonumber(readline(),16)) + until size <= 0 + body = table.concat(bodys) + end + assert(readbytes(2) == "\r\n") + header = recvheader(readline, header) + return body, header +end + +local function readall(readline, readbytes) + local request = readline() + local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" + assert(method and url and httpver) + httpver = assert(tonumber(httpver)) + if httpver < 1.0 or httpver > 1.1 then + return 505 -- HTTP Version not supported + end + local header = recvheader(readline) + local length, mode + if header then + length = header["content-length"] + if length then + length = tonumber(length) + end + mode = header["transfer-encoding"] + if mode then + if mode ~= "identity" or mode ~= "chunked" then + return 501 -- Not Implemented + end + end + end + + local body + if mode == "chunked" then + body, header = recvchunkedbody(readline, readbytes, header) + else + -- identity mode + if length then + body = readbody(readbytes, length) + end + end + + return 200, url, method, header, body +end + +function httpd.read_request(readfunc) + local readline = assert(readfunc.readline) + local readbytes = assert(readfunc.readbytes) + local ok, code, url, method, header, body = pcall(readall, readline, readbytes) + if ok then + return code, url, method, header, body + else + return nil, code + end +end + +function httpd.write_response(writefunc, statuscode, bodyfunc, header) + local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode]) + writefunc(statusline) + if header then + for k,v in pairs(header) do + writefunc(string.format("%s: %s\r\n", k,v)) + end + end + local t = type(bodyfunc) + if t == "string" then + writefunc(string.format("content-length: %d\r\n\r\n", #bodyfunc)) + writefunc(bodyfunc) + elseif t == "function" then + writefunc("transfer-encoding: chunked\r\n") + while true do + local s = bodyfunc() + if s then + if s ~= "" then + writefunc(string.format("\r\n%x\r\n", #s)) + writefunc(s) + end + else + writefunc("\r\n0\r\n\r\n") + end + end + else + assert(t == "nil") + writefunc("\r\n") + end +end + +return httpd diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua new file mode 100644 index 00000000..64b82c7e --- /dev/null +++ b/lualib/http/sockethelper.lua @@ -0,0 +1,45 @@ +local socket = require "socket" + +local readline = socket.readline +local readbytes = socket.read +local writebytes = socket.write + +local sockethelper = {} +local socket_error = {} + +sockethelper.socket_error = socket_error + +function sockethelper.readfunc(fd) + local helper = {} + + function helper.readline() + local ret = readline(fd, "\r\n") + if ret then + return ret + else + error(socket_error) + end + end + + function helper.readbytes(sz) + local ret = readbytes(fd, sz) + if ret then + return ret + else + error(socket_error) + end + end + + return helper +end + +function sockethelper.writefunc(fd) + return function(content) + local ok = writebytes(fd, content) + if not ok then + error(socket_error) + end + end +end + +return sockethelper \ No newline at end of file From d2ad63da817f3679cd06e9849344a61e6aeb6dba Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 22 Jul 2014 21:52:18 +0800 Subject: [PATCH 114/729] read_request must return header table --- lualib/http/httpd.lua | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 4eb9ab40..3ced98c6 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -52,8 +52,6 @@ local function recvheader(readline, header) return header end - header = header or {} - local name, value repeat if line:byte(1) == 9 then -- tab, append last line @@ -117,18 +115,15 @@ local function readall(readline, readbytes) if httpver < 1.0 or httpver > 1.1 then return 505 -- HTTP Version not supported end - local header = recvheader(readline) - local length, mode - if header then - length = header["content-length"] - if length then - length = tonumber(length) - end - mode = header["transfer-encoding"] - if mode then - if mode ~= "identity" or mode ~= "chunked" then - return 501 -- Not Implemented - end + local header = recvheader(readline, {}) + local length = header["content-length"] + if length then + length = tonumber(length) + end + local mode = header["transfer-encoding"] + if mode then + if mode ~= "identity" or mode ~= "chunked" then + return 501 -- Not Implemented end end @@ -157,7 +152,7 @@ function httpd.read_request(readfunc) end function httpd.write_response(writefunc, statuscode, bodyfunc, header) - local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode]) + local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode] or "") writefunc(statusline) if header then for k,v in pairs(header) do From 551d5048c406c22c59293289a516e6584c45e4e5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 22 Jul 2014 23:04:37 +0800 Subject: [PATCH 115/729] simple url parser --- examples/simpleweb.lua | 16 +++++++++++++++- lualib/http/url.lua | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lualib/http/url.lua diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index f0c2855b..bfd247eb 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -2,6 +2,7 @@ local skynet = require "skynet" local socket = require "socket" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" +local urllib = require "http.url" local mode = ... @@ -15,7 +16,20 @@ skynet.start(function() if code ~= 200 then httpd.write_response(sockethelper.writefunc(id), code) else - httpd.write_response(sockethelper.writefunc(id), code , "Hello world") + local tmp = {} + if header.host then + table.insert(tmp, string.format("host: %s", header.host)) + end + local path, query = urllib.parse(url) + table.insert(tmp, string.format("path: %s", path)) + if query then + local q = urllib.parse_query(query) + for k, v in pairs(q) do + table.insert(tmp, string.format("query: %s= %s", k,v)) + end + end + + httpd.write_response(sockethelper.writefunc(id), code , table.concat(tmp,"\n")) end else if url == sockethelper.socket_error then diff --git a/lualib/http/url.lua b/lualib/http/url.lua new file mode 100644 index 00000000..ae74b099 --- /dev/null +++ b/lualib/http/url.lua @@ -0,0 +1,28 @@ +local url = {} + +local function decode_func(c) + return string.char(tonumber(c, 16)) +end + +local function decode(str) + local str = str:gsub('+', ' ') + return str:gsub("%%(..)", decode_func) +end + +function url.parse(u) + local path,query = u:match "([^?]*)%??(.*)" + if path then + path = decode(path) + end + return path, query +end + +function url.parse_query(q) + local r = {} + for k,v in q:gmatch "(.-)=([^&]*)&?" do + r[decode(k)] = decode(v) + end + return r +end + +return url From 18f20425a098b969f17903b8ba938fa3d30a1d39 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 23 Jul 2014 12:19:48 +0800 Subject: [PATCH 116/729] httpd don't need readline now --- lualib-src/lua-socket.c | 5 - lualib/http/httpd.lua | 177 +++++++++++++++++++++++++---------- lualib/http/sockethelper.lua | 16 +--- lualib/snax/loginserver.lua | 9 +- lualib/socket.lua | 55 +++++++---- test/testsocket.lua | 7 +- 6 files changed, 176 insertions(+), 93 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 8bfefc0e..916fa7a9 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -23,7 +23,6 @@ struct buffer_node { }; struct socket_buffer { - int limit; int size; int offset; struct buffer_node *head; @@ -66,7 +65,6 @@ lnewpool(lua_State *L, int sz) { static int lnewbuffer(lua_State *L) { struct socket_buffer * sb = lua_newuserdata(L, sizeof(*sb)); - sb->limit = luaL_optint(L,1,BUFFER_LIMIT); sb->size = 0; sb->offset = 0; sb->head = NULL; @@ -129,9 +127,6 @@ lpushbuffer(lua_State *L) { sb->size += sz; lua_pushinteger(L, sb->size); - if (sb->limit > 0 && sb->size > sb->limit) { - return luaL_error(L, "buffer overflow (limit = %d, size = %d)", sb->limit, sb->size); - } return 1; } diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 3ced98c6..5333bfd5 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -1,7 +1,6 @@ local table = table local httpd = {} -local READLIMIT = 8192 -- limit bytes per read local http_status_msg = { [100] = "Continue", @@ -46,76 +45,150 @@ local http_status_msg = { [505] = "HTTP Version not supported", } -local function recvheader(readline, header) - local line = readline() - if line == "" then - return header +local function recvheader(readbytes, limit, lines, last) + local idx = 1 + local len = 0 + local header = last + while true do + header = header .. readbytes() + if #header + len > limit then + return + end + while true do + local f,e = header:find("\r\n",1, true) + if f then + if f == 1 then + for i = idx, #lines do + lines[i] = nil + end + return header:sub(3) + end + lines[idx] = header:sub(1, f-1) + idx = idx + 1 + len = len + f-1 + header = header:sub(e+1) + else + break + end + end end +end +local function parseheader(lines, from, header) local name, value - repeat + for i=from,#lines do + local line = lines[i] if line:byte(1) == 9 then -- tab, append last line + if name == nil then + return + end header[name] = header[name] .. line:sub(2) else name, value = line:match "^(.-):%s*(.*)" - assert(name and value) + if name == nil or value == nil then + return + end name = name:lower() if header[name] then header[name] = header[name] .. ", " .. value else header[name] = value end - line = readline() end - until line == "" - + end return header end -local function recvbody(readbytes, length) - if length < READLIMIT then - return readbytes(length) - end - local tmp = {} +local function chunksize(readbytes, body) while true do - if length <= READLIMIT then - table.insert(tmp, readbytes(length)) + if #body > 128 then + return + end + body = body .. readbytes() + local f,e = body:find("\r\n",1,true) + if f then + return tonumber(body:sub(1,f-1),16), body:sub(e+1) + end + end +end + +local function readcrln(readbytes, body) + if #body > 2 then + if body:sub(1,2) ~= "\r\n" then + return + end + return body:sub(3) + else + body = body .. readbytes(2-#body) + if body ~= "\r\n" then + return + end + return "" + end +end + +local tmpline = {} + +local function recvchunkedbody(readbytes, bodylimit, header, body) + local result = "" + local size = 0 + + while true do + local sz + sz , body = chunksize(readbytes, body) + if not sz then + return + end + if sz == 0 then break end - table.insert(tmp, readbytes(READLIMIT)) - length = length - READLIMIT + size = size + sz + if bodylimit and size > bodylimit then + return + end + if #body >= sz then + result = result .. body:sub(1,sz) + body = body:sub(sz+1) + else + result = result .. body .. readbytes(sz - #body) + body = "" + end + body = readcrln(readbytes, body) + if not body then + return + end end - return table.concat(tmp) + + body = readcrln(readbytes, body) + if not body then + return + end + body = recvheader(readbytes, 8192, tmpline, body) + if not body then + return + end + + header = parseheader(tmpline,1,header) + + return result, header end -local function recvchunkedbody(readline, readbytes, header) - local size = assert(tonumber(readline(),16)) - local body = recvbody(readbytes,size) - assert(readbytes(2) == "\r\n") - size = assert(tonumber(readline(),16)) - if size > 0 then - local bodys = { body } - repeat - table.insert(bodys, recvbody(readbytes,size)) - assert(readbytes(2) == "\r\n") - size = assert(tonumber(readline(),16)) - until size <= 0 - body = table.concat(bodys) +local function readall(readbytes, bodylimit) + local body = recvheader(readbytes, 8192, tmpline, "") + if not body then + return 413 -- Request Entity Too Large end - assert(readbytes(2) == "\r\n") - header = recvheader(readline, header) - return body, header -end - -local function readall(readline, readbytes) - local request = readline() + local request = assert(tmpline[1]) local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" assert(method and url and httpver) httpver = assert(tonumber(httpver)) if httpver < 1.0 or httpver > 1.1 then return 505 -- HTTP Version not supported end - local header = recvheader(readline, {}) + local header = parseheader(tmpline,2,{}) + if not header then + return 400 -- Bad request + end local length = header["content-length"] if length then length = tonumber(length) @@ -127,23 +200,31 @@ local function readall(readline, readbytes) end end - local body if mode == "chunked" then - body, header = recvchunkedbody(readline, readbytes, header) + body, header = recvchunkedbody(readbytes, bodylimit, header, body) + if not body then + return 413 + end else -- identity mode if length then - body = readbody(readbytes, length) + if length > bodylimit then + return 413 + end + if #body >= length then + body = body:sub(1,length) + else + local padding = readbytes(length - #body) + body = body .. padding + end end end return 200, url, method, header, body end -function httpd.read_request(readfunc) - local readline = assert(readfunc.readline) - local readbytes = assert(readfunc.readbytes) - local ok, code, url, method, header, body = pcall(readall, readline, readbytes) +function httpd.read_request(readbytes, bodylimit) + local ok, code, url, method, header, body = pcall(readall, readbytes, bodylimit) if ok then return code, url, method, header, body else diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 64b82c7e..14668c8b 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -1,6 +1,5 @@ local socket = require "socket" -local readline = socket.readline local readbytes = socket.read local writebytes = socket.write @@ -10,18 +9,7 @@ local socket_error = {} sockethelper.socket_error = socket_error function sockethelper.readfunc(fd) - local helper = {} - - function helper.readline() - local ret = readline(fd, "\r\n") - if ret then - return ret - else - error(socket_error) - end - end - - function helper.readbytes(sz) + return function (sz) local ret = readbytes(fd, sz) if ret then return ret @@ -29,8 +17,6 @@ function sockethelper.readfunc(fd) error(socket_error) end end - - return helper end function sockethelper.writefunc(fd) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 1ad5b70e..c5de769f 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -47,14 +47,15 @@ local function write(fd, text) end local function launch_slave(auth_handler) - -- set socket buffer limit (8K) - -- If the attacker send large package, close the socket - socket.limit(8192) - local function auth(fd, addr) fd = assert(tonumber(fd)) skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) socket.start(fd) + + -- set socket buffer limit (8K) + -- If the attacker send large package, close the socket + socket.limit(fd, 8192) + local challenge = crypt.randomkey() write(fd, crypt.base64encode(challenge).."\n") diff --git a/lualib/socket.lua b/lualib/socket.lua index 28268747..f54d2175 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -2,7 +2,6 @@ local driver = require "socketdriver" local skynet = require "skynet" local assert = assert -local buffer_limit = -1 local socket = {} -- api local buffer_pool = {} -- store all message buffer object local socket_pool = setmetatable( -- store all socket object @@ -48,13 +47,7 @@ socket_message[1] = function(id, size, data) return end - local ok , sz = pcall(driver.push, s.buffer, buffer_pool, data, size) - if not ok then - skynet.error("socket: error on ", id , sz) - driver.clear(s.buffer,buffer_pool) - driver.close(id) - return - end + local sz = driver.push(s.buffer, buffer_pool, data, size) local rr = s.read_required local rrt = type(rr) if rrt == "number" then @@ -63,11 +56,19 @@ socket_message[1] = function(id, size, data) s.read_required = nil wakeup(s) end - elseif rrt == "string" then - -- read line - if driver.readline(s.buffer,nil,rr) then - s.read_required = nil - wakeup(s) + else + if s.buffer_limit and sz > s.buffer_limit then + skynet.error(string.format("socket buffer overlow: fd=%d size=%d", id , sz)) + driver.clear(s.buffer,buffer_pool) + driver.close(id) + return + end + if rrt == "string" then + -- read line + if driver.readline(s.buffer,nil,rr) then + s.read_required = nil + wakeup(s) + end end end end @@ -130,7 +131,7 @@ skynet.register_protocol { local function connect(id, func) local newbuffer if func == nil then - newbuffer = driver.buffer(buffer_limit) + newbuffer = driver.buffer() end local s = { id = id, @@ -206,6 +207,27 @@ end function socket.read(id, sz) local s = socket_pool[id] assert(s) + if sz == nil then + -- read some bytes + local ret = driver.readall(s.buffer, buffer_pool) + if ret ~= "" then + return ret + end + + if not s.connected then + return false, ret + end + assert(not s.read_required) + s.read_required = 0 + suspend(s) + ret = driver.readall(s.buffer, buffer_pool) + if ret ~= "" then + return ret + else + return false, ret + end + end + local ret = driver.pop(s.buffer, buffer_pool, sz) if ret then return ret @@ -325,8 +347,9 @@ function socket.abandon(id) socket_pool[id] = nil end -function socket.limit(limit) - buffer_limit = limit +function socket.limit(id, limit) + local s = assert(socket_pool[id]) + s.buffer_limit = limit end return socket diff --git a/test/testsocket.lua b/test/testsocket.lua index 05313a11..3893cd61 100644 --- a/test/testsocket.lua +++ b/test/testsocket.lua @@ -7,9 +7,9 @@ local function echo(id) socket.start(id) while true do - local str = socket.readline(id,"\n") + local str = socket.read(id) if str then - socket.write(id, str .. "\n") + socket.write(id, str) else socket.close(id) return @@ -21,9 +21,6 @@ if mode == "agent" then id = tonumber(id) skynet.start(function() - -- A small limit, if socket buffer overflow, close the client - socket.limit(64) - skynet.fork(function() echo(id) skynet.exit() From 0188e263ddbf839cca5376ec8480b77391bbad13 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 23 Jul 2014 13:34:30 +0800 Subject: [PATCH 117/729] recv header all, and then split with \r\n --- lualib/http/httpd.lua | 48 +++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 5333bfd5..3698bf00 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -45,33 +45,37 @@ local http_status_msg = { [505] = "HTTP Version not supported", } -local function recvheader(readbytes, limit, lines, last) - local idx = 1 - local len = 0 - local header = last - while true do - header = header .. readbytes() - if #header + len > limit then - return - end +local function recvheader(readbytes, limit, lines, header) + local result + local e = header:find("\r\n\r\n", 1, true) + if e then + result = header:sub(e+4) + else while true do - local f,e = header:find("\r\n",1, true) - if f then - if f == 1 then - for i = idx, #lines do - lines[i] = nil - end - return header:sub(3) - end - lines[idx] = header:sub(1, f-1) - idx = idx + 1 - len = len + f-1 - header = header:sub(e+1) - else + local bytes = readbytes() + header = header .. bytes + if #header > limit then + return + end + e = header:find("\r\n\r\n", -#bytes-3, true) + if e then + result = header:sub(e+4) break end end end + local idx = 1 + for v in header:gmatch("(.-)\r\n") do + if v == "" then + break + end + lines[idx] = v + idx = idx + 1 + end + for i = idx, #lines do + lines[i] = nil + end + return result end local function parseheader(lines, from, header) From db25d1acc67342641c991aeabbe668a5c70595d4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 23 Jul 2014 14:04:32 +0800 Subject: [PATCH 118/729] update history --- HISTORY.md | 1 + examples/simpleweb.lua | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 615bc346..c5dcfadc 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,7 @@ Dev version * Simplify clientsocket lib * mongo driver support replica set * config file support read from ENV +* add simple httpd (see examples/simpleweb.lua) v0.4.2 (2014-7-14) ----------- diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index bfd247eb..4bb175d0 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -11,7 +11,8 @@ if mode == "agent" then skynet.start(function() skynet.dispatch("lua", function (_,_,id) socket.start(id) - local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id)) + -- limit request body size to 8192 (you can pass nil to unlimit) + local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192) if code then if code ~= 200 then httpd.write_response(sockethelper.writefunc(id), code) From 19e6462376ed82a102dbacbe4be29c3d3b80e3d2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 23 Jul 2014 14:27:42 +0800 Subject: [PATCH 119/729] httpd.write_response capture socket error --- examples/simpleweb.lua | 13 ++++++++++--- lualib/http/httpd.lua | 10 +++++++--- lualib/http/sockethelper.lua | 4 ++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index 4bb175d0..6e6e6438 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -8,6 +8,14 @@ local mode = ... if mode == "agent" then +local function response(id, ...) + local ok, err = httpd.write_response(sockethelper.writefunc(id), ...) + if not ok then + -- if err == sockethelper.socket_error , that means socket closed. + skynet.error(string.format("fd = %d, %s", id, err)) + end +end + skynet.start(function() skynet.dispatch("lua", function (_,_,id) socket.start(id) @@ -15,7 +23,7 @@ skynet.start(function() local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192) if code then if code ~= 200 then - httpd.write_response(sockethelper.writefunc(id), code) + response(id, code) else local tmp = {} if header.host then @@ -29,8 +37,7 @@ skynet.start(function() table.insert(tmp, string.format("query: %s= %s", k,v)) end end - - httpd.write_response(sockethelper.writefunc(id), code , table.concat(tmp,"\n")) + response(id, code, table.concat(tmp,"\n")) end else if url == sockethelper.socket_error then diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 3698bf00..1fe507de 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -227,8 +227,8 @@ local function readall(readbytes, bodylimit) return 200, url, method, header, body end -function httpd.read_request(readbytes, bodylimit) - local ok, code, url, method, header, body = pcall(readall, readbytes, bodylimit) +function httpd.read_request(...) + local ok, code, url, method, header, body = pcall(readall, ...) if ok then return code, url, method, header, body else @@ -236,7 +236,7 @@ function httpd.read_request(readbytes, bodylimit) end end -function httpd.write_response(writefunc, statuscode, bodyfunc, header) +local function writeall(writefunc, statuscode, bodyfunc, header) local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode] or "") writefunc(statusline) if header then @@ -267,4 +267,8 @@ function httpd.write_response(writefunc, statuscode, bodyfunc, header) end end +function httpd.write_response(...) + return pcall(writeall, ...) +end + return httpd diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 14668c8b..89c7ec93 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -4,7 +4,7 @@ local readbytes = socket.read local writebytes = socket.write local sockethelper = {} -local socket_error = {} +local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end }) sockethelper.socket_error = socket_error @@ -28,4 +28,4 @@ function sockethelper.writefunc(fd) end end -return sockethelper \ No newline at end of file +return sockethelper From 7babdd3a97e441949f9020b735684616598b29f7 Mon Sep 17 00:00:00 2001 From: zeYang Date: Wed, 23 Jul 2014 21:26:25 +0800 Subject: [PATCH 120/729] Update lua-bson.c //8 thx! --- lualib-src/lua-bson.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 07fb77e2..10e3d73a 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1083,7 +1083,7 @@ typeclosure(lua_State *L) { "string", // 5 "binary", // 6 "objectid", // 7 - "timestamp",// 8 + "timestamp", // 8 "date", // 9 "regex", // 10 "minkey", // 11 From a56b6aa4258b782772be9f5dab581c228469b5af Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 25 Jul 2014 21:10:11 +0800 Subject: [PATCH 121/729] bugfix: don't use string.format to concat binary string --- examples/client.lua | 7 +++---- examples/login/client.lua | 23 ++++++++++------------- examples/simpleweb.lua | 2 ++ 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/client.lua b/examples/client.lua index d5bace7f..14342556 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -8,10 +8,9 @@ local fd = assert(socket.connect("127.0.0.1", 8888)) local function send_package(fd, pack) local size = #pack - local package = string.format("%c%c%s", - bit32.extract(size,8,8), - bit32.extract(size,0,8), - pack) + local package = string.char(bit32.extract(size,8,8)) .. + string.char(bit32.extract(size,0,8)).. + pack socket.send(fd, package) end diff --git a/examples/login/client.lua b/examples/login/client.lua index dfd66267..2dfdf25b 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -93,15 +93,13 @@ print("login ok, subid=", subid) local function send_request(v, session) local size = #v + 4 - local package = string.format("%c%c%s%c%c%c%c", - bit32.extract(size,8,8), - bit32.extract(size,0,8), - v, - bit32.extract(session,24,8), - bit32.extract(session,16,8), - bit32.extract(session,8,8), - bit32.extract(session,0,8) - ) + local package = string.char(bit32.extract(size,8,8)).. + string.char(bit32.extract(size,0,8)).. + v.. + string.char(bit32.extract(session,24,8)).. + string.char(bit32.extract(session,16,8)).. + string.char(bit32.extract(session,8,8)).. + string.char(bit32.extract(session,0,8)) socket.send(fd, package) return v, session @@ -135,10 +133,9 @@ local readpackage = unpack_f(unpack_package) local function send_package(fd, pack) local size = #pack - local package = string.format("%c%c%s", - bit32.extract(size,8,8), - bit32.extract(size,0,8), - pack) + local package = string.char(bit32.extract(size,8,8)).. + string.char(bit32.extract(size,0,8)).. + pack socket.send(fd, package) end diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index 6e6e6438..19870aba 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -3,6 +3,8 @@ local socket = require "socket" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" local urllib = require "http.url" +local table = table +local string = string local mode = ... From 25a8b3a179d62e579920d1db3f072b2926cfa97c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 28 Jul 2014 15:45:49 +0800 Subject: [PATCH 122/729] release 0.5.0 --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index c5dcfadc..81718ed3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -Dev version +v0.5.0 (2014-7-28) ----------- * skynet.exit will quit service immediately. * Add snax.gateserver, snax.loginserver, snax.msgserver From 8e09fad233553dbcaca615276fbb42798a5c1a70 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 29 Jul 2014 10:31:45 +0800 Subject: [PATCH 123/729] remove unused skynet_context_init --- skynet-src/skynet_handle.c | 17 ++++++++++++----- skynet-src/skynet_handle.h | 2 +- skynet-src/skynet_server.c | 7 ++----- skynet-src/skynet_server.h | 1 - 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/skynet-src/skynet_handle.c b/skynet-src/skynet_handle.c index 58cabc0b..dc20dcf2 100644 --- a/skynet-src/skynet_handle.c +++ b/skynet-src/skynet_handle.c @@ -49,7 +49,6 @@ skynet_handle_register(struct skynet_context *ctx) { rwlock_wunlock(&s->lock); handle |= s->harbor; - skynet_context_init(ctx, handle); return handle; } } @@ -67,8 +66,9 @@ skynet_handle_register(struct skynet_context *ctx) { } } -void +int skynet_handle_retire(uint32_t handle) { + int ret = 0; struct handle_storage *s = H; rwlock_wlock(&s->lock); @@ -79,6 +79,7 @@ skynet_handle_retire(uint32_t handle) { if (ctx != NULL && skynet_context_handle(ctx) == handle) { skynet_context_release(ctx); s->slot[hash] = NULL; + ret = 1; int i; int j=0, n=s->name_count; for (i=0; ilock); + + return ret; } void @@ -105,10 +108,14 @@ skynet_handle_retireall() { for (i=0;islot_size;i++) { rwlock_rlock(&s->lock); struct skynet_context * ctx = s->slot[i]; + uint32_t handle = 0; + if (ctx) + handle = skynet_context_handle(ctx); rwlock_runlock(&s->lock); - if (ctx != NULL) { - ++n; - skynet_handle_retire(skynet_context_handle(ctx)); + if (handle != 0) { + if (skynet_handle_retire(handle)) { + ++n; + } } } if (n==0) diff --git a/skynet-src/skynet_handle.h b/skynet-src/skynet_handle.h index a39b47bb..e067eed0 100644 --- a/skynet-src/skynet_handle.h +++ b/skynet-src/skynet_handle.h @@ -8,7 +8,7 @@ struct skynet_context; uint32_t skynet_handle_register(struct skynet_context *); -void skynet_handle_retire(uint32_t handle); +int skynet_handle_retire(uint32_t handle); struct skynet_context * skynet_handle_grab(uint32_t handle); void skynet_handle_retireall(); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 3e902cc9..0e5f851f 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -132,6 +132,8 @@ skynet_context_new(const char * name, const char *param) { ctx->init = false; ctx->endless = false; + // Should set to 0 first to avoid skynet_handle_retireall get an uninitialized handle + ctx->handle = 0; ctx->handle = skynet_handle_register(ctx); struct message_queue * queue = ctx->queue = skynet_mq_create(ctx->handle); // init function maybe use ctx->handle, so it must init at last @@ -646,11 +648,6 @@ skynet_context_handle(struct skynet_context *ctx) { return ctx->handle; } -void -skynet_context_init(struct skynet_context *ctx, uint32_t handle) { - ctx->handle = handle; -} - void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb) { context->cb = cb; diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 5d07ba70..27b6b768 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -12,7 +12,6 @@ struct skynet_context * skynet_context_new(const char * name, const char * parm) void skynet_context_grab(struct skynet_context *); struct skynet_context * skynet_context_release(struct skynet_context *); uint32_t skynet_context_handle(struct skynet_context *); -void skynet_context_init(struct skynet_context *, uint32_t handle); int skynet_context_push(uint32_t handle, struct skynet_message *message); void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session); int skynet_context_newsession(struct skynet_context *); From 434cd1ca79140357e95427ee4390bd8573327c35 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 29 Jul 2014 20:56:35 +0800 Subject: [PATCH 124/729] add sharedata --- Makefile | 5 +- examples/share.lua | 63 +++ lualib-src/lua-sharedata.c | 789 +++++++++++++++++++++++++++++++++++ lualib/sharedata.lua | 37 ++ lualib/sharedata/corelib.lua | 134 ++++++ service/sharedatad.lua | 121 ++++++ 6 files changed, 1148 insertions(+), 1 deletion(-) create mode 100644 examples/share.lua create mode 100644 lualib-src/lua-sharedata.c create mode 100644 lualib/sharedata.lua create mode 100644 lualib/sharedata/corelib.lua create mode 100644 service/sharedatad.lua diff --git a/Makefile b/Makefile index 63d9085d..1bcb3603 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ jemalloc : $(MALLOC_STATICLIB) CSERVICE = snlua logger gate harbor LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ cjson clientsocket memory profile multicast \ - cluster crypt + cluster crypt sharedata SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -113,6 +113,9 @@ $(LUA_CLIB_PATH)/cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ +$(LUA_CLIB_PATH)/sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so diff --git a/examples/share.lua b/examples/share.lua new file mode 100644 index 00000000..2c81f790 --- /dev/null +++ b/examples/share.lua @@ -0,0 +1,63 @@ +local skynet = require "skynet" +local sharedata = require "sharedata" + +local mode = ... + +if mode == "host" then + +skynet.start(function() + skynet.error("new foobar") + sharedata.new("foobar", { a=1, b= { "hello", "world" } }) + + skynet.fork(function() + skynet.sleep(300) -- sleep 3s + skynet.error("update foobar") + sharedata.update("foobar", { a =2 }) + end) +end) + +else + + +skynet.start(function() + skynet.newservice(SERVICE_NAME, "host") + + local obj = sharedata.query "foobar" + + local b = obj.b + skynet.error(string.format("a=%d", obj.a)) + + for k,v in ipairs(b) do + skynet.error(string.format("b[%d]=%s", k,v)) + end + + for i = 1, 5 do + skynet.sleep(100) + skynet.error(i) + for k,v in pairs(obj) do + skynet.error(string.format("%s = %s", k , tostring(v))) + end + end + + local ok, err = pcall(function() + local tmp = { b[1], b[2] } -- b is invalid , so pcall should failed + end) + + if not ok then + skynet.error(err) + end + + collectgarbage() + skynet.error("sleep") + skynet.sleep(100) + b = nil + collectgarbage() + skynet.error("sleep") + skynet.sleep(100) + + sharedata.delete "foobar" + + skynet.exit() +end) + +end diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c new file mode 100644 index 00000000..1a0f4f74 --- /dev/null +++ b/lualib-src/lua-sharedata.c @@ -0,0 +1,789 @@ +// build: gcc -O2 -Wall --shared -o conf.so luaconf.c + +#include +#include +#include +#include +#include +#include + +#define KEYTYPE_INTEGER 0 +#define KEYTYPE_STRING 1 + +#define VALUETYPE_NIL 0 +#define VALUETYPE_NUMBER 1 +#define VALUETYPE_STRING 2 +#define VALUETYPE_BOOLEAN 3 +#define VALUETYPE_TABLE 4 + +struct table; + +union value { + lua_Number n; + struct table * tbl; + int string; + int boolean; +}; + +struct node { + union value v; + int key; // integer key or index of string table + int next; // next slot index + uint32_t keyhash; + uint8_t keytype; // key type must be integer or string + uint8_t valuetype; // value type can be number/string/boolean/table + uint8_t nocolliding; // 0 means colliding slot +}; + +struct table; + +struct state { + int dirty; + int ref; + struct table * root; +}; + +struct table { + int sizearray; + int sizehash; + uint8_t *arraytype; + union value * array; + struct node * hash; + lua_State * L; +}; + +struct context { + lua_State * L; + struct table * tbl; + int string_index; +}; + +struct ctrl { + struct table * root; + struct table * update; +}; + +static int +countsize(lua_State *L, int sizearray) { + int n = 0; + lua_pushnil(L); + while (lua_next(L, 1) != 0) { + int type = lua_type(L, -2); + ++n; + if (type == LUA_TNUMBER) { + lua_Number key = lua_tonumber(L, -2); + int nkey = (int)key; + if ((lua_Number)nkey != key) { + luaL_error(L, "Invalid key %f", key); + } + if (nkey > 0 && nkey <= sizearray) { + --n; + } + } else if (type != LUA_TSTRING && type != LUA_TTABLE) { + luaL_error(L, "Invalid key type %s", lua_typename(L, type)); + } + lua_pop(L, 1); + } + + return n; +} + +static uint32_t +calchash(const char * str, size_t l) { + uint32_t h = (uint32_t)l; + size_t l1; + size_t step = (l >> 5) + 1; + for (l1 = l; l1 >= step; l1 -= step) { + h = h ^ ((h<<5) + (h>>2) + (uint8_t)(str[l1 - 1])); + } + return h; +} + +static int +stringindex(struct context *ctx, const char * str, size_t sz) { + lua_State *L = ctx->L; + lua_pushlstring(L, str, sz); + lua_pushvalue(L, -1); + lua_rawget(L, 1); + int index; + // stringmap(1) str index + if (lua_isnil(L, -1)) { + index = ++ctx->string_index; + lua_pop(L, 1); + lua_pushinteger(L, index); + lua_rawset(L, 1); + } else { + index = lua_tointeger(L, -1); + lua_pop(L, 2); + } + return index; +} + +static int convtable(lua_State *L); + +static void +setvalue(struct context * ctx, lua_State *L, int index, struct node *n) { + int vt = lua_type(L, index); + switch(vt) { + case LUA_TNIL: + n->valuetype = VALUETYPE_NIL; + break; + case LUA_TNUMBER: + n->v.n = lua_tonumber(L, index); + n->valuetype = VALUETYPE_NUMBER; + break; + case LUA_TSTRING: { + size_t sz = 0; + const char * str = lua_tolstring(L, index, &sz); + n->v.string = stringindex(ctx, str, sz); + n->valuetype = VALUETYPE_STRING; + break; + } + case LUA_TBOOLEAN: + n->v.boolean = lua_toboolean(L, index); + n->valuetype = VALUETYPE_BOOLEAN; + break; + case LUA_TTABLE: { + struct table *tbl = ctx->tbl; + ctx->tbl = (struct table *)malloc(sizeof(struct table)); + if (ctx->tbl == NULL) { + ctx->tbl = tbl; + luaL_error(L, "memory error"); + // never get here + } + memset(ctx->tbl, 0, sizeof(struct table)); + int absidx = lua_absindex(L, index); + + lua_pushcfunction(L, convtable); + lua_pushvalue(L, absidx); + lua_pushlightuserdata(L, ctx); + + lua_call(L, 2, 0); + + n->v.tbl = ctx->tbl; + n->valuetype = VALUETYPE_TABLE; + + ctx->tbl = tbl; + + break; + } + default: + luaL_error(L, "Unsupport value type %s", lua_typename(L, vt)); + break; + } +} + +static void +setarray(struct context *ctx, lua_State *L, int index, int key) { + struct node n; + setvalue(ctx, L, index, &n); + struct table *tbl = ctx->tbl; + --key; // base 0 + tbl->arraytype[key] = n.valuetype; + tbl->array[key] = n.v; +} + +static int +ishashkey(struct context * ctx, lua_State *L, int index, int *key, uint32_t *keyhash, int *keytype) { + int sizearray = ctx->tbl->sizearray; + int kt = lua_type(L, index); + if (kt == LUA_TNUMBER) { + *key = lua_tointeger(L, index); + if (*key > 0 && *key <= sizearray) { + return 0; + } + *keyhash = (uint32_t)*key; + *keytype = KEYTYPE_INTEGER; + } else { + size_t sz = 0; + const char * s = lua_tolstring(L, index, &sz); + *keyhash = calchash(s, sz); + *key = stringindex(ctx, s, sz); + *keytype = KEYTYPE_STRING; + } + return 1; +} + +static void +fillnocolliding(lua_State *L, struct context *ctx) { + struct table * tbl = ctx->tbl; + lua_pushnil(L); + while (lua_next(L, 1) != 0) { + int key; + int keytype; + uint32_t keyhash; + if (!ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) { + setarray(ctx, L, -1, key); + } else { + struct node * n = &tbl->hash[keyhash % tbl->sizehash]; + if (n->valuetype == VALUETYPE_NIL) { + n->key = key; + n->keytype = keytype; + n->keyhash = keyhash; + n->next = -1; + n->nocolliding = 1; + setvalue(ctx, L, -1, n); // set n->v , n->valuetype + } + } + lua_pop(L,1); + } +} + +static void +fillcolliding(lua_State *L, struct context *ctx) { + struct table * tbl = ctx->tbl; + int sizehash = tbl->sizehash; + int emptyslot = 0; + int i; + lua_pushnil(L); + while (lua_next(L, 1) != 0) { + int key; + int keytype; + uint32_t keyhash; + if (ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) { + struct node * mainpos = &tbl->hash[keyhash % tbl->sizehash]; + if (!(mainpos->keytype == keytype && mainpos->key == key)) { + // the key has not insert + struct node * n = NULL; + for (i=emptyslot;ihash[i].valuetype == VALUETYPE_NIL) { + n = &tbl->hash[i]; + break; + } + } + assert(n); + n->next = mainpos->next; + mainpos->next = n - tbl->hash; + mainpos->nocolliding = 0; + n->key = key; + n->keytype = keytype; + n->keyhash = keyhash; + n->nocolliding = 0; + setvalue(ctx, L, -1, n); // set n->v , n->valuetype + } + } + lua_pop(L,1); + } +} + +// table need convert +// struct context * ctx +static int +convtable(lua_State *L) { + int i; + struct context *ctx = lua_touserdata(L,2); + struct table *tbl = ctx->tbl; + + tbl->L = ctx->L; + + int sizearray = lua_rawlen(L, 1); + if (sizearray) { + tbl->arraytype = (uint8_t *)malloc(sizearray * sizeof(uint8_t)); + if (tbl->arraytype == NULL) { + goto memerror; + } + for (i=0;iarraytype[i] = VALUETYPE_NIL; + } + tbl->array = (union value *)malloc(sizearray * sizeof(union value)); + if (tbl->array == NULL) { + goto memerror; + } + tbl->sizearray = sizearray; + } + int sizehash = countsize(L, sizearray); + if (sizehash) { + tbl->hash = (struct node *)malloc(sizehash * sizeof(struct node)); + if (tbl->hash == NULL) { + goto memerror; + } + for (i=0;ihash[i].valuetype = VALUETYPE_NIL; + tbl->hash[i].nocolliding = 0; + } + tbl->sizehash = sizehash; + + fillnocolliding(L, ctx); + fillcolliding(L, ctx); + } else { + int i; + for (i=1;i<=sizearray;i++) { + lua_rawgeti(L, 1, i); + setarray(ctx, L, -1, i); + lua_pop(L,1); + } + } + + return 0; +memerror: + return luaL_error(L, "memory error"); +} + +static void +delete_tbl(struct table *tbl) { + int i; + for (i=0;isizearray;i++) { + if (tbl->arraytype[i] == VALUETYPE_TABLE) { + delete_tbl(tbl->array[i].tbl); + } + } + for (i=0;isizehash;i++) { + if (tbl->hash[i].valuetype == VALUETYPE_TABLE) { + delete_tbl(tbl->hash[i].v.tbl); + } + } + free(tbl->arraytype); + free(tbl->array); + free(tbl->hash); + free(tbl); +} + +static int +pconv(lua_State *L) { + struct context *ctx = lua_touserdata(L,1); + lua_State * pL = lua_touserdata(L, 2); + int ret; + + lua_settop(L, 0); + + // init L (may throw memory error) + // create a table for string map + lua_newtable(L); + + lua_pushcfunction(pL, convtable); + lua_pushvalue(pL,1); + lua_pushlightuserdata(pL, ctx); + + ret = lua_pcall(pL, 2, 0, 0); + if (ret != LUA_OK) { + size_t sz = 0; + const char * error = lua_tolstring(pL, -1, &sz); + lua_pushlstring(L, error, sz); + lua_error(L); + // never get here + } + + luaL_checkstack(L, ctx->string_index + 3, NULL); + lua_settop(L,1); + + return 1; +} + +static void +convert_stringmap(struct context *ctx, struct table *tbl) { + lua_State *L = ctx->L; + lua_settop(L, ctx->string_index + 1); + lua_pushvalue(L, 1); + struct state * s = lua_newuserdata(L, sizeof(*s)); + s->dirty = 0; + s->ref = 0; + s->root = tbl; + lua_replace(L, 1); + lua_replace(L, -2); + + lua_pushnil(L); + // ... stringmap nil + while (lua_next(L, -2) != 0) { + int idx = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_pushvalue(L, -1); + lua_replace(L, idx); + } + + lua_pop(L, 1); + + lua_gc(L, LUA_GCCOLLECT, 0); +} + +static int +lnewconf(lua_State *L) { + int ret; + struct context ctx; + struct table * tbl = NULL; + luaL_checktype(L,1,LUA_TTABLE); + ctx.L = luaL_newstate(); + ctx.tbl = NULL; + ctx.string_index = 1; // 1 reserved for dirty flag + if (ctx.L == NULL) { + lua_pushliteral(L, "memory error"); + goto error; + } + tbl = (struct table *)malloc(sizeof(struct table)); + if (tbl == NULL) { + // lua_pushliteral may fail because of memory error, close first. + lua_close(ctx.L); + ctx.L = NULL; + lua_pushliteral(L, "memory error"); + goto error; + } + memset(tbl, 0, sizeof(struct table)); + ctx.tbl = tbl; + + lua_pushcfunction(ctx.L, pconv); + lua_pushlightuserdata(ctx.L , &ctx); + lua_pushlightuserdata(ctx.L , L); + + ret = lua_pcall(ctx.L, 2, 1, 0); + + if (ret != LUA_OK) { + size_t sz = 0; + const char * error = lua_tolstring(ctx.L, -1, &sz); + lua_pushlstring(L, error, sz); + goto error; + } + + convert_stringmap(&ctx, tbl); + + lua_pushlightuserdata(L, tbl); + + return 1; +error: + if (ctx.L) { + lua_close(ctx.L); + } + if (tbl) { + delete_tbl(tbl); + } + lua_error(L); + return -1; +} + +static struct table * +get_table(lua_State *L, int index) { + struct table *tbl = lua_touserdata(L,index); + if (tbl == NULL) { + luaL_error(L, "Need a conf object"); + } + return tbl; +} + +static int +ldeleteconf(lua_State *L) { + struct table *tbl = get_table(L,1); + lua_close(tbl->L); + delete_tbl(tbl); + return 0; +} + +static void +pushvalue(lua_State *L, lua_State *sL, uint8_t vt, union value *v) { + switch(vt) { + case VALUETYPE_NUMBER: + lua_pushnumber(L, v->n); + break; + case VALUETYPE_STRING: { + size_t sz = 0; + const char *str = lua_tolstring(sL, v->string, &sz); + lua_pushlstring(L, str, sz); + break; + } + case VALUETYPE_BOOLEAN: + lua_pushboolean(L, v->boolean); + break; + case VALUETYPE_TABLE: + lua_pushlightuserdata(L, v->tbl); + break; + default: + lua_pushnil(L); + break; + } +} + +static struct node * +lookup_key(struct table *tbl, uint32_t keyhash, int key, int keytype, const char *str, size_t sz) { + if (tbl->sizehash == 0) + return NULL; + struct node *n = &tbl->hash[keyhash % tbl->sizehash]; + if (keyhash != n->keyhash && n->nocolliding) + return NULL; + for (;;) { + if (keyhash == n->keyhash) { + if (n->keytype == KEYTYPE_INTEGER) { + if (keytype == KEYTYPE_INTEGER && n->key == key) { + return n; + } + } else { + // n->keytype == KEYTYPE_STRING + if (keytype == KEYTYPE_STRING) { + size_t sz2 = 0; + const char * str2 = lua_tolstring(tbl->L, n->key, &sz2); + if (sz == sz2 && memcmp(str,str2,sz) == 0) { + return n; + } + } + } + } + if (n->next < 0) { + return NULL; + } + n = &tbl->hash[n->next]; + } +} + +static int +lindexconf(lua_State *L) { + struct table *tbl = get_table(L,1); + int kt = lua_type(L,2); + uint32_t keyhash; + int key = 0; + int keytype; + size_t sz = 0; + const char * str = NULL; + if (kt == LUA_TNUMBER) { + lua_Number k = lua_tonumber(L, 2); + key = (int)k; + if ((lua_Number)key != k) { + return luaL_error(L, "Invalid key %f", k); + } + if (key > 0 && key <= tbl->sizearray) { + --key; + pushvalue(L, tbl->L, tbl->arraytype[key], &tbl->array[key]); + return 1; + } + keytype = KEYTYPE_INTEGER; + keyhash = (uint32_t)key; + } else { + str = luaL_checklstring(L, 2, &sz); + keyhash = calchash(str, sz); + keytype = KEYTYPE_STRING; + } + + struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz); + if (n) { + pushvalue(L, tbl->L, n->valuetype, &n->v); + return 1; + } else { + return 0; + } +} + +static void +pushkey(lua_State *L, lua_State *sL, struct node *n) { + if (n->keytype == KEYTYPE_INTEGER) { + lua_pushinteger(L, n->key); + } else { + size_t sz = 0; + const char * str = lua_tolstring(sL, n->key, &sz); + lua_pushlstring(L, str, sz); + } +} + +static int +pushfirsthash(lua_State *L, struct table * tbl) { + if (tbl->sizehash) { + pushkey(L, tbl->L, &tbl->hash[0]); + return 1; + } else { + return 0; + } +} + +static int +lnextkey(lua_State *L) { + struct table *tbl = get_table(L,1); + if (lua_isnoneornil(L,2)) { + if (tbl->sizearray > 0) { + int i; + for (i=0;isizearray;i++) { + if (tbl->arraytype[i] != VALUETYPE_NIL) { + lua_pushinteger(L, i+1); + return 1; + } + } + } + return pushfirsthash(L, tbl); + } + int kt = lua_type(L,2); + uint32_t keyhash; + int key = 0; + int keytype; + size_t sz=0; + const char *str = NULL; + int sizearray = tbl->sizearray; + if (kt == LUA_TNUMBER) { + lua_Number k = lua_tonumber(L,2); + key = (int)k; + if ((lua_Number)key != k) { + return 0; + } + if (key > 0 && key <= sizearray) { + int i; + for (i=key;iarraytype[i] != VALUETYPE_NIL) { + lua_pushinteger(L, i+1); + return 1; + } + } + return pushfirsthash(L, tbl); + } + keyhash = (uint32_t)key; + keytype = KEYTYPE_INTEGER; + } else { + str = luaL_checklstring(L, 2, &sz); + keyhash = calchash(str, sz); + keytype = KEYTYPE_STRING; + } + + struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz); + if (n) { + ++n; + int index = n-tbl->hash; + if (index == tbl->sizehash) { + return 0; + } + pushkey(L, tbl->L, n); + return 1; + } else { + return 0; + } +} + +static int +llen(lua_State *L) { + struct table *tbl = get_table(L,1); + lua_pushinteger(L, tbl->sizearray); + return 1; +} + +static int +lhashlen(lua_State *L) { + struct table *tbl = get_table(L,1); + lua_pushinteger(L, tbl->sizehash); + return 1; +} + +static int +releaseobj(lua_State *L) { + struct ctrl *c = lua_touserdata(L, 1); + struct table *tbl = c->root; + struct state *s = lua_touserdata(tbl->L, 1); + __sync_fetch_and_sub(&s->ref, 1); + c->root = NULL; + c->update = NULL; + + return 0; +} + +static int +lboxconf(lua_State *L) { + struct table * tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + __sync_fetch_and_add(&s->ref, 1); + + struct ctrl * c = lua_newuserdata(L, sizeof(*c)); + c->root = tbl; + c->update = NULL; + if (luaL_newmetatable(L, "confctrl")) { + lua_pushcfunction(L, releaseobj); + lua_setfield(L, -2, "__gc"); + } + lua_setmetatable(L, -2); + + return 1; +} + +static int +lmarkdirty(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + s->dirty = 1; + return 0; +} + +static int +lisdirty(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + int d = s->dirty; + lua_pushboolean(L, d); + + return 1; +} + +static int +lgetref(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + lua_pushinteger(L , s->ref); + + return 1; +} + +static int +lincref(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + int ref = __sync_add_and_fetch(&s->ref, 1); + lua_pushinteger(L , ref); + + return 1; +} + +static int +ldecref(lua_State *L) { + struct table *tbl = get_table(L,1); + struct state * s = lua_touserdata(tbl->L, 1); + int ref = __sync_sub_and_fetch(&s->ref, 1); + lua_pushinteger(L , ref); + + return 1; +} + +static int +lneedupdate(lua_State *L) { + struct ctrl * c = lua_touserdata(L, 1); + if (c->update) { + lua_pushlightuserdata(L, c->update); + lua_getuservalue(L, 1); + return 2; + } + return 0; +} + +static int +lupdate(lua_State *L) { + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TLIGHTUSERDATA); + luaL_checktype(L, 3, LUA_TTABLE); + struct ctrl * c= lua_touserdata(L, 1); + struct table *n = lua_touserdata(L, 2); + if (c->update) { + return luaL_error(L, "can't update more than once"); + } + if (c->root == n) { + return luaL_error(L, "You should update a new object"); + } + lua_settop(L, 3); + lua_setuservalue(L, 1); + c->update = n; + + return 0; +} + +int +luaopen_sharedata_core(lua_State *L) { + luaL_Reg l[] = { + // used by host + { "new", lnewconf }, + { "delete", ldeleteconf }, + { "markdirty", lmarkdirty }, + { "getref", lgetref }, + { "incref", lincref }, + { "decref", ldecref }, + + // used by client + { "box", lboxconf }, + { "index", lindexconf }, + { "nextkey", lnextkey }, + { "len", llen }, + { "hashlen", lhashlen }, + { "isdirty", lisdirty }, + { "needupdate", lneedupdate }, + { "update", lupdate }, + { NULL, NULL }, + }; + luaL_checkversion(L); + luaL_newlib(L, l); + + return 1; +} diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua new file mode 100644 index 00000000..edbf61c5 --- /dev/null +++ b/lualib/sharedata.lua @@ -0,0 +1,37 @@ +local skynet = require "skynet" +local sd = require "sharedata.corelib" + +local service + +skynet.init(function() + service = skynet.uniqueservice "sharedatad" +end) + +local sharedata = {} + +local function monitor(name, obj, cobj) + local newobj = skynet.call(service, "lua", "monitor", name, cobj) + sd.update(obj, newobj) +end + +function sharedata.query(name) + local obj = skynet.call(service, "lua", "query", name) + local r = sd.box(obj) + skynet.send(service, "lua", "confirm" , obj) + skynet.fork(monitor,name, r, obj) + return r +end + +function sharedata.new(name, v) + skynet.call(service, "lua", "new", name, v) +end + +function sharedata.update(name, v) + skynet.call(service, "lua", "update", name, v) +end + +function sharedata.delete(name) + skynet.call(service, "lua", "delete", name) +end + +return sharedata diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua new file mode 100644 index 00000000..d9a5488a --- /dev/null +++ b/lualib/sharedata/corelib.lua @@ -0,0 +1,134 @@ +local core = require "sharedata.core" +local type = type +local next = next +local rawget = rawget + +local conf = {} + +conf.host = { + new = core.new, + delete = core.delete, + getref = core.getref, + markdirty = core.markdirty, + incref = core.incref, + decref = core.decref, +} + +local meta = {} + +local isdirty = core.isdirty +local index = core.index +local needupdate = core.needupdate +local len = core.len + +local function findroot(self) + while self.__parent do + self = self.__parent + end + return self +end + +local function update(root, cobj, gcobj) + root.__obj = cobj + root.__gcobj = gcobj + -- don't use pairs + for k,v in next, root do + if type(v)=="table" and k~="__parent" then + local pointer = index(cobj, k) + if type(pointer) == "userdata" then + update(v, pointer, gcobj) + else + root[k] = nil + end + end + end +end + +local function genkey(self) + local key = tostring(self.__key) + while self.__parent do + self = self.__parent + key = self.__key .. "." .. key + end + return key +end + +function meta:__index(key) + local obj = self.__obj + if isdirty(obj) then + local newobj, newtbl = needupdate(self.__gcobj) + if newobj then + local newgcobj = newtbl.__gcobj + -- todo: update + local root = findroot(self) + update(root, newobj, newgcobj) + if obj == self.__obj then + error ("The key [" .. genkey(self) .. "] doesn't exist after update") + end + end + end + local v = index(self.__obj, key) + if type(v) == "userdata" then + local r = setmetatable({ + __obj = v, + __gcobj = self.__gcobj, + __parent = self, + __key = key, + }, meta) + self[key] = r + return r + else + return v + end +end + +function meta:__len() + return len(self.__obj) +end + +local function conf_ipairs(self, index) + local obj = self.__obj + index = index + 1 + local value = rawget(self, index) + if value then + return index, value + end + local sz = len(obj) + if sz < index then + return + end + return index, self[index] +end + +function meta:__ipairs() + return conf_ipairs, self, 0 +end + +function meta:__pairs() + return conf.next, self, nil +end + +function conf.next(obj, key) + local nextkey = core.nextkey(obj.__obj, key) + if nextkey then + return nextkey, obj[nextkey] + end +end + +function conf.box(obj) + local gcobj = core.box(obj) + return setmetatable({ + __parent = false, + __obj = obj, + __gcobj = gcobj, + __key = "", + } , meta) +end + +function conf.update(self, pointer) + local cobj = self.__obj + assert(isdirty(cobj), "Obly dirty object can be update") + core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) }) +end + +return conf \ No newline at end of file diff --git a/service/sharedatad.lua b/service/sharedatad.lua new file mode 100644 index 00000000..7ce24b89 --- /dev/null +++ b/service/sharedatad.lua @@ -0,0 +1,121 @@ +local skynet = require "skynet" +local sharedata = require "sharedata.corelib" +local table = table + +local NORET = {} +local pool = {} +local objmap = {} + +local function newobj(name, tbl) + assert(pool[name] == nil) + local cobj = sharedata.host.new(tbl) + sharedata.host.incref(cobj) + local v = { value = tbl , obj = cobj, watch = {} } + objmap[cobj] = v + pool[name] = v +end + +local function collectobj() + while true do + skynet.sleep(600 * 100) -- sleep 10 min + collectgarbage() + for obj, v in pairs(objmap) do + if v == true then + if sharedata.host.getref(obj) <= 0 then + objmap[obj] = nil + print("collect", obj) + sharedata.host.delete(obj) + end + end + end + end +end + +local CMD = {} + +function CMD.new(name, t) + local dt = type(t) + local value + if dt == "table" then + value = t + elseif dt == "string" then + value = {} + local f = load(t, "=" .. name, "t", env) + f() + elseif dt == "nil" then + value = {} + else + error ("Unknown data type " .. dt) + end + newobj(name, value) +end + +function CMD.delete(name) + local v = assert(pool[name]) + pool[name] = nil + assert(objmap[v.obj]) + objmap[v.obj] = true + sharedata.host.decref(v.obj) +end + +function CMD.query(name) + local v = assert(pool[name]) + local obj = v.obj + sharedata.host.incref(obj) + return v.obj +end + +function CMD.confirm(cobj) + if objmap[cobj] then + sharedata.host.decref(cobj) + end + return NORET +end + +function CMD.update(name, t) + local v = pool[name] + local watch, oldcobj + if v then + watch = v.watch + oldcobj = v.obj + CMD.delete(name) + end + CMD.new(name, t) + local newobj = pool[name].obj + if watch then + sharedata.host.markdirty(oldcobj) + for _,v in ipairs(watch) do + local session = v[1] + local address = v[2] + skynet.redirect(address, 0, "response", session, skynet.pack(newobj)) + end + end +end + +function CMD.monitor(session, address, name, obj) + local v = assert(pool[name]) + if obj ~= v.obj then + return v.obj + end + + table.insert(v.watch, { session, address }) + + return NORET +end + +skynet.start(function() + skynet.fork(collectobj) + skynet.dispatch("lua", function (session, source ,cmd, ...) + local f = assert(CMD[cmd]) + local r + if cmd == "monitor" then + r = f(session, source, ...) + else + r = f(...) + end + if r ~= NORET then + skynet.ret(skynet.pack(r)) + end + end) +end) + From 68a952ff1739ed1c9bc1347ae294830d3b2e6206 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 29 Jul 2014 21:22:49 +0800 Subject: [PATCH 125/729] bugfix: update until delete --- examples/share.lua | 15 ++++++++++++--- lualib/sharedata.lua | 11 +++++++++-- service/sharedatad.lua | 9 ++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/examples/share.lua b/examples/share.lua index 2c81f790..ec94ead8 100644 --- a/examples/share.lua +++ b/examples/share.lua @@ -10,9 +10,15 @@ skynet.start(function() sharedata.new("foobar", { a=1, b= { "hello", "world" } }) skynet.fork(function() - skynet.sleep(300) -- sleep 3s + skynet.sleep(200) -- sleep 3s skynet.error("update foobar") sharedata.update("foobar", { a =2 }) + skynet.sleep(200) -- sleep 3s + skynet.error("update foobar") + sharedata.update("foobar", { a = 3, b = { "change" } }) + skynet.sleep(100) + skynet.error("delete foobar") + sharedata.delete "foobar" end) end) @@ -47,6 +53,11 @@ skynet.start(function() skynet.error(err) end + -- obj. b is not the same with local b + for k,v in ipairs(obj.b) do + skynet.error(string.format("b[%d] = %s", k , tostring(v))) + end + collectgarbage() skynet.error("sleep") skynet.sleep(100) @@ -55,8 +66,6 @@ skynet.start(function() skynet.error("sleep") skynet.sleep(100) - sharedata.delete "foobar" - skynet.exit() end) diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua index edbf61c5..a5fbc115 100644 --- a/lualib/sharedata.lua +++ b/lualib/sharedata.lua @@ -10,8 +10,15 @@ end) local sharedata = {} local function monitor(name, obj, cobj) - local newobj = skynet.call(service, "lua", "monitor", name, cobj) - sd.update(obj, newobj) + local newobj = cobj + while true do + newobj = skynet.call(service, "lua", "monitor", name, newobj) + if newobj == nil then + return + end + sd.update(obj, newobj) + end + print("name exit") end function sharedata.query(name) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 7ce24b89..8733d59c 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -56,6 +56,11 @@ function CMD.delete(name) assert(objmap[v.obj]) objmap[v.obj] = true sharedata.host.decref(v.obj) + for _,v in ipairs(v.watch) do + local session = v[1] + local address = v[2] + skynet.redirect(address, 0, "response", session, skynet.pack(nil)) + end end function CMD.query(name) @@ -78,7 +83,9 @@ function CMD.update(name, t) if v then watch = v.watch oldcobj = v.obj - CMD.delete(name) + objmap[oldcobj] = true + sharedata.host.decref(oldcobj) + pool[name] = nil end CMD.new(name, t) local newobj = pool[name].obj From deed51d3091ea94c18f5b60b2e258a81de7e73d6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 29 Jul 2014 21:30:18 +0800 Subject: [PATCH 126/729] fix typo --- examples/share.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/share.lua b/examples/share.lua index ec94ead8..21c3bf54 100644 --- a/examples/share.lua +++ b/examples/share.lua @@ -10,11 +10,11 @@ skynet.start(function() sharedata.new("foobar", { a=1, b= { "hello", "world" } }) skynet.fork(function() - skynet.sleep(200) -- sleep 3s - skynet.error("update foobar") + skynet.sleep(200) -- sleep 2s + skynet.error("update foobar a = 2") sharedata.update("foobar", { a =2 }) - skynet.sleep(200) -- sleep 3s - skynet.error("update foobar") + skynet.sleep(200) -- sleep 2s + skynet.error("update foobar a = 3") sharedata.update("foobar", { a = 3, b = { "change" } }) skynet.sleep(100) skynet.error("delete foobar") @@ -39,7 +39,7 @@ skynet.start(function() for i = 1, 5 do skynet.sleep(100) - skynet.error(i) + skynet.error("second " ..i) for k,v in pairs(obj) do skynet.error(string.format("%s = %s", k , tostring(v))) end From c78466b4864e0d17c4e3c3ada70f6a8bf0348ff6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 29 Jul 2014 21:56:36 +0800 Subject: [PATCH 127/729] remove debug print --- lualib/sharedata.lua | 1 - service/sharedatad.lua | 1 - 2 files changed, 2 deletions(-) diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua index a5fbc115..5b83d703 100644 --- a/lualib/sharedata.lua +++ b/lualib/sharedata.lua @@ -18,7 +18,6 @@ local function monitor(name, obj, cobj) end sd.update(obj, newobj) end - print("name exit") end function sharedata.query(name) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 8733d59c..af7e1169 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -23,7 +23,6 @@ local function collectobj() if v == true then if sharedata.host.getref(obj) <= 0 then objmap[obj] = nil - print("collect", obj) sharedata.host.delete(obj) end end From ce5adba5b208d748dc022b421912e437b1c6f513 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 30 Jul 2014 12:10:30 +0800 Subject: [PATCH 128/729] add skynet.response for delay response --- HISTORY.md | 7 +++ lualib-src/lua-skynet.c | 21 +++++++++ lualib/sharedata.lua | 2 +- lualib/skynet.lua | 93 ++++++++++++++++++++++++++++++++------- service/bootstrap.lua | 16 +++---- service/console.lua | 5 +-- service/cslave.lua | 16 +++---- service/datacenterd.lua | 22 ++++----- service/debug_console.lua | 8 ++-- service/launcher.lua | 39 ++++++++++------ service/sharedatad.lua | 23 +++------- test/testblockcall.lua | 10 ----- test/testdeadcall.lua | 16 ++++++- 13 files changed, 182 insertions(+), 96 deletions(-) delete mode 100644 test/testblockcall.lua diff --git a/HISTORY.md b/HISTORY.md index 81718ed3..23303874 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,10 @@ +Dev version +----------- +* add sharedata +* bugfix: service exit before init would not report back +* add skynet.response and check multicall skynet.ret +* skynet.newservice throw error when lanuch faild + v0.5.0 (2014-7-28) ----------- * skynet.exit will quit service immediately. diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 0189b61b..479fcea0 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -299,6 +299,26 @@ lpackstring(lua_State *L) { return 1; } +static int +ltrash(lua_State *L) { + int t = lua_type(L,1); + switch (t) { + case LUA_TSTRING: { + break; + } + case LUA_TLIGHTUSERDATA: { + void * msg = lua_touserdata(L,1); + luaL_checkinteger(L,2); + skynet_free(msg); + break; + } + default: + luaL_error(L, "skynet.trash invalid param %s", lua_typename(L,t)); + } + + return 0; +} + int luaopen_skynet_c(lua_State *L) { luaL_checkversion(L); @@ -314,6 +334,7 @@ luaopen_skynet_c(lua_State *L) { { "pack", _luaseri_pack }, { "unpack", _luaseri_unpack }, { "packstring", lpackstring }, + { "trash" , ltrash }, { "callback", _callback }, { NULL, NULL }, }; diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua index 5b83d703..58ecf28d 100644 --- a/lualib/sharedata.lua +++ b/lualib/sharedata.lua @@ -14,7 +14,7 @@ local function monitor(name, obj, cobj) while true do newobj = skynet.call(service, "lua", "monitor", name, newobj) if newobj == nil then - return + break end sd.update(obj, newobj) end diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 5e8e95b0..a5d2caf5 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -43,12 +43,14 @@ end local session_id_coroutine = {} local session_coroutine_id = {} local session_coroutine_address = {} +local session_response = {} local wakeup_session = {} local sleep_session = {} local watching_service = {} local watching_session = {} +local dead_service = {} local error_queue = {} -- suspend is function @@ -73,7 +75,9 @@ local function _error_dispatch(error_session, error_source) if error_session == 0 then -- service is down -- Don't remove from watching_service , because user may call dead service - watching_service[error_source] = false + if watching_service[error_source] then + dead_service[error_source] = true + end for session, srv in pairs(watching_session) do if srv == error_source then table.insert(error_queue, session) @@ -122,6 +126,18 @@ local function dispatch_wakeup() end end +local function release_watching(address) + local ref = watching_service[address] + if ref then + ref = ref - 1 + if ref > 0 then + watching_service[address] = ref + else + watching_service[address] = nil + end + end +end + -- suspend is local function function suspend(co, result, command, param, size) if not result then @@ -142,15 +158,50 @@ function suspend(co, result, command, param, size) elseif command == "RETURN" then local co_session = session_coroutine_id[co] local co_address = session_coroutine_address[co] - if param == nil then + if param == nil or session_response[co] then error(debug.traceback(co)) end - c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, size) - return suspend(co, coroutine.resume(co)) + session_response[co] = true + local ret + if not dead_service[co_address] then + ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, size) >= 0 + elseif size == nil then + c.trash(param, size) + ret = false + end + return suspend(co, coroutine.resume(co, ret)) + elseif command == "RESPONSE" then + local co_session = session_coroutine_id[co] + local co_address = session_coroutine_address[co] + if session_response[co] then + error(debug.traceback(co)) + end + local f = param + local function response(ok, ...) + local ret + if not dead_service[co_address] then + if ok then + ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, f(...)) >=0 + else + ret = c.send(co_address, skynet.PTYPE_ERROR, co_session, "") >=0 + end + else + ret = false + end + release_watching(co_address) + f = nil + return ret + end + watching_service[co_address] = watching_service[co_address] + 1 + session_response[co] = response + return suspend(co, coroutine.resume(co, response)) elseif command == "EXIT" then -- coroutine exit + local address = session_coroutine_address[co] + release_watching(address) session_coroutine_id[co] = nil session_coroutine_address[co] = nil + session_response[co] = nil elseif command == "QUIT" then -- service exit return @@ -259,13 +310,21 @@ end function skynet.exit() skynet.send(".launcher","lua","REMOVE",skynet.self()) + -- report the sources that call me for co, session in pairs(session_coroutine_id) do local address = session_coroutine_address[co] - local self = skynet.self() if session~=0 and address then - skynet.redirect(address, self, "error", session, "") + c.redirect(address, 0, skynet.PTYPE_ERROR, session, "") end end + -- report the sources I call but haven't return + local tmp = {} + for session, address in pairs(watching_session) do + tmp[address] = true + end + for address in pairs(tmp) do + c.redirect(address, 0, skynet.PTYPE_ERROR, 0, "") + end c.command("EXIT") -- quit service coroutine_yield "QUIT" @@ -294,9 +353,6 @@ end function skynet.send(addr, typename, ...) local p = proto[typename] - if watching_service[addr] == false then - error("Service is dead") - end return c.send(addr, p.id, 0 , p.pack(...)) end @@ -321,9 +377,6 @@ end function skynet.call(addr, typename, ...) local p = proto[typename] - if watching_service[addr] == false then - error("Service is dead") - end local session = c.send(addr, p.id , nil , p.pack(...)) if session == nil then error("call to invalid address " .. skynet.address(addr)) @@ -333,16 +386,18 @@ end function skynet.rawcall(addr, typename, msg, sz) local p = proto[typename] - if watching_service[addr] == false then - error("Service is dead") - end local session = assert(c.send(addr, p.id , nil , msg, sz), "call to invalid address") return yield_call(addr, session) end function skynet.ret(msg, sz) msg = msg or "" - coroutine_yield("RETURN", msg, sz) + return coroutine_yield("RETURN", msg, sz) +end + +function skynet.response(pack) + pack = pack or skynet.pack + return coroutine_yield("RESPONSE", pack) end function skynet.retpack(...) @@ -412,6 +467,12 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) local p = assert(proto[prototype], prototype) local f = p.dispatch if f then + local ref = watching_service[source] + if ref then + watching_service[source] = ref + 1 + else + watching_service[source] = 1 + end local co = co_create(f) session_coroutine_id[co] = session session_coroutine_address[co] = source diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 11303577..06e67d7e 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -13,31 +13,31 @@ skynet.start(function() standalone = true skynet.setenv("standalone", "true") - local slave = skynet.newservice "cdummy" - if slave == nil then + local ok, slave = pcall(skynet.newservice, "cdummy") + if not ok then skynet.abort() end skynet.name(".slave", slave) else if standalone then - if not skynet.newservice "cmaster" then + if not pcall(skynet.newservice,"cmaster") then skynet.abort() end end - local slave = skynet.newservice "cslave" - if slave == nil then + local ok, slave = pcall(skynet.newservice, "cslave") + if not ok then skynet.abort() end skynet.name(".slave", slave) end if standalone then - local datacenter = assert(skynet.newservice "datacenterd") + local datacenter = skynet.newservice "datacenterd" skynet.name("DATACENTER", datacenter) end - assert(skynet.newservice "service_mgr") - assert(skynet.newservice(skynet.getenv "start" or "main")) + skynet.newservice "service_mgr" + pcall(skynet.newservice,skynet.getenv "start" or "main") skynet.exit() end) diff --git a/service/console.lua b/service/console.lua index 86bf6cc1..c0eba879 100644 --- a/service/console.lua +++ b/service/console.lua @@ -7,10 +7,7 @@ local function console_main_loop() while true do local cmdline = socket.readline(stdin, "\n") if cmdline ~= "" then - local handle = skynet.newservice(cmdline) - if handle == nil then - print("Launch error:",cmdline) - end + pcall(skynet.newservice,cmdline) end end socket.unlock(stdin) diff --git a/service/cslave.lua b/service/cslave.lua index 6f005b2f..8a21aa43 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -28,7 +28,7 @@ local function monitor_clear(id) if v then monitor[id] = nil for _, v in ipairs(v) do - skynet.redirect(v.address, 0, "response", v.session, "") + v() end end end @@ -149,30 +149,30 @@ local function monitor_harbor(master_fd) end end -function harbor.REGISTER(_,_, fd, name, handle) +function harbor.REGISTER(fd, name, handle) assert(globalname[name] == nil) globalname[name] = handle socket.write(fd, pack_package("R", name, handle)) skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) end -function harbor.LINK(session, source, fd, id) +function harbor.LINK(fd, id) if slaves[id] then if monitor[id] == nil then monitor[id] = {} end - table.insert(monitor[id], { address = source, session = session }) + table.insert(monitor[id], skynet.response(true)) else skynet.ret() end end -function harbor.CONNECT(session, source, fd, id) +function harbor.CONNECT(fd, id) if not slaves[id] then if monitor[id] == nil then monitor[id] = {} end - table.insert(monitor[id], { address = source, session = session }) + table.insert(monitor[id], skynet.response(true)) else skynet.ret() end @@ -186,9 +186,9 @@ skynet.start(function() skynet.error("slave connect to master " .. tostring(master_addr)) local master_fd = socket.open(master_addr) - skynet.dispatch("lua", function (session,source,command,...) + skynet.dispatch("lua", function (_,_,command,...) local f = assert(harbor[command]) - f(session, source, master_fd, ...) + f(master_fd, ...) end) skynet.dispatch("text", monitor_harbor(master_fd)) diff --git a/service/datacenterd.lua b/service/datacenterd.lua index 1a0fee65..5929b0e3 100644 --- a/service/datacenterd.lua +++ b/service/datacenterd.lua @@ -45,10 +45,8 @@ local function wakeup(db, key1, key2, value, ...) db[key1] = nil if value then -- throw error because can't wake up a branch - for _,v in ipairs(q) do - local session = v[1] - local source = v[2] - skynet.redirect(source, 0, "error", session, "") + for _,response in ipairs(q) do + response(false) end else return q @@ -66,15 +64,13 @@ function command.UPDATE(...) end local q = wakeup(wait_queue, ...) if q then - for _, v in ipairs(q) do - local session = v[1] - local source = v[2] - skynet.redirect(source, 0, "response", session, skynet.pack(value)) + for _, response in ipairs(q) do + response(true,value) end end end -local function waitfor(session, source, db, key1, key2, ...) +local function waitfor(db, key1, key2, ...) if key2 == nil then -- push queue local q = db[key1] @@ -84,7 +80,7 @@ local function waitfor(session, source, db, key1, key2, ...) else assert(q[mode] == "queue") end - table.insert(q, { session, source }) + table.insert(q, skynet.response()) else local q = db[key1] if q == nil then @@ -93,18 +89,18 @@ local function waitfor(session, source, db, key1, key2, ...) else assert(q[mode] == "branch") end - return waitfor(session, source, q, key2, ...) + return waitfor(q, key2, ...) end end skynet.start(function() - skynet.dispatch("lua", function (session, source, cmd, ...) + skynet.dispatch("lua", function (_, _, cmd, ...) if cmd == "WAIT" then local ret = command.QUERY(...) if ret then skynet.ret(skynet.pack(ret)) else - waitfor(session, source, wait_queue, ...) + waitfor(wait_queue, ...) end else local f = assert(command[cmd]) diff --git a/service/debug_console.lua b/service/debug_console.lua index a587f69d..aa552ceb 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -122,8 +122,8 @@ function COMMAND.clearcache() end function COMMAND.start(...) - local addr = skynet.newservice(...) - if addr then + local ok, addr = pcall(skynet.newservice, ...) + if ok then return { [skynet.address(addr)] = ... } else return "Failed" @@ -131,8 +131,8 @@ function COMMAND.start(...) end function COMMAND.snax(...) - local s = snax.newservice(...) - if s then + local ok, s = pcall(snax.newservice, ...) + if ok then local addr = s.handle return { [skynet.address(addr)] = ... } else diff --git a/service/launcher.lua b/service/launcher.lua index 5c1c1a8b..9cb194b4 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -28,7 +28,7 @@ function command.STAT() return list end -function command.INFO(_, _, handle) +function command.INFO(_, handle) handle = handle_to_address(handle) if services[handle] == nil then return @@ -38,7 +38,7 @@ function command.INFO(_, _, handle) end end -function command.TASK(_, _, handle) +function command.TASK(_, handle) handle = handle_to_address(handle) if services[handle] == nil then return @@ -48,7 +48,7 @@ function command.TASK(_, _, handle) end end -function command.KILL(_, _, handle) +function command.KILL(_, handle) handle = handle_to_address(handle) skynet.kill(handle) local ret = { [skynet.address(handle)] = tostring(services[handle]) } @@ -72,18 +72,29 @@ function command.GC() return command.MEM() end -function command.REMOVE(_,_, handle) +function command.REMOVE(_, handle) services[handle] = nil + local response = instance[handle] + if response then + -- instance is dead + response(false) + instance[handle] = nil + end + -- don't return (skynet.ret) because the handle may exit return NORET end -function command.LAUNCH(address, session, service, ...) +local function return_string(str) + return str +end + +function command.LAUNCH(_, service, ...) local param = table.concat({...}, " ") local inst = skynet.launch(service, param) if inst then services[inst] = service .. " " .. param - instance[inst] = { session = session, address = address } + instance[inst] = skynet.response(return_string) else skynet.ret("") -- launch failed end @@ -93,9 +104,9 @@ end function command.ERROR(address) -- see serivce-src/service_lua.c -- init failed - local reply = instance[address] - if reply then - skynet.redirect(reply.address , 0, "response", reply.session, "") + local response = instance[address] + if response then + response(false) instance[address] = nil end services[address] = nil @@ -104,9 +115,9 @@ end function command.LAUNCHOK(address) -- init notice - local reply = instance[address] - if reply then - skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address)) + local response = instance[address] + if response then + response(true, skynet.address(address)) instance[address] = nil end @@ -127,7 +138,7 @@ skynet.register_protocol { else -- launch request local service, param = string.match(cmd,"([^ ]+) (.*)") - command.LAUNCH(address, session, service, param) + command.LAUNCH(_, service, param) end end, } @@ -136,7 +147,7 @@ skynet.dispatch("lua", function(session, address, cmd , ...) cmd = string.upper(cmd) local f = command[cmd] if f then - local ret = f(address, session, ...) + local ret = f(address, ...) if ret ~= NORET then skynet.ret(skynet.pack(ret)) end diff --git a/service/sharedatad.lua b/service/sharedatad.lua index af7e1169..eb3346c4 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -55,10 +55,8 @@ function CMD.delete(name) assert(objmap[v.obj]) objmap[v.obj] = true sharedata.host.decref(v.obj) - for _,v in ipairs(v.watch) do - local session = v[1] - local address = v[2] - skynet.redirect(address, 0, "response", session, skynet.pack(nil)) + for _,response in ipairs(v.watch) do + response(true) end end @@ -90,21 +88,19 @@ function CMD.update(name, t) local newobj = pool[name].obj if watch then sharedata.host.markdirty(oldcobj) - for _,v in ipairs(watch) do - local session = v[1] - local address = v[2] - skynet.redirect(address, 0, "response", session, skynet.pack(newobj)) + for _,response in ipairs(watch) do + response(true, newobj) end end end -function CMD.monitor(session, address, name, obj) +function CMD.monitor(name, obj) local v = assert(pool[name]) if obj ~= v.obj then return v.obj end - table.insert(v.watch, { session, address }) + table.insert(v.watch, skynet.response()) return NORET end @@ -113,12 +109,7 @@ skynet.start(function() skynet.fork(collectobj) skynet.dispatch("lua", function (session, source ,cmd, ...) local f = assert(CMD[cmd]) - local r - if cmd == "monitor" then - r = f(session, source, ...) - else - r = f(...) - end + local r = f(...) if r ~= NORET then skynet.ret(skynet.pack(r)) end diff --git a/test/testblockcall.lua b/test/testblockcall.lua deleted file mode 100644 index b0d7e76f..00000000 --- a/test/testblockcall.lua +++ /dev/null @@ -1,10 +0,0 @@ -local skynet = require "skynet" - -skynet.start(function() - local ping = skynet.newservice("pingserver") - skynet.timeout(0,function() - print(skynet.call(ping,"lua","PING","ping")) - end) - - print(skynet.blockcall(ping,"lua","HELLO")) -end) diff --git a/test/testdeadcall.lua b/test/testdeadcall.lua index 9017d380..b34dccb8 100644 --- a/test/testdeadcall.lua +++ b/test/testdeadcall.lua @@ -11,6 +11,15 @@ skynet.start(function() end) end) +elseif mode == "dead" then + +skynet.start(function() + skynet.dispatch("lua", function (...) + skynet.sleep(100) + print("return", skynet.ret "") + end) +end) + else skynet.start(function() @@ -20,6 +29,9 @@ else skynet.call(test,"lua", "dead call") end)) - skynet.exit() + local dead = skynet.newservice(SERVICE_NAME, "dead") -- launch self in dead mode + + skynet.timeout(0, skynet.exit) -- exit after a while, so the call never return + skynet.call(dead, "lua", "whould not return") end) -end \ No newline at end of file +end From 40519e9ee147044c09e50bad10b6d21ada61416e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 30 Jul 2014 14:54:18 +0800 Subject: [PATCH 129/729] add response test mode --- lualib/skynet.lua | 17 +++++++++++++++++ test/testecho.lua | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/testecho.lua diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a5d2caf5..f0f7d74c 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -178,6 +178,23 @@ function suspend(co, result, command, param, size) end local f = param local function response(ok, ...) + if ok == "TEST" then + if dead_service[co_address] then + release_watching(co_address) + f = false + return false + else + return true + end + end + if not f then + if f == false then + f = nil + return false + end + error "Can't response more than once" + end + local ret if not dead_service[co_address] then if ok then diff --git a/test/testecho.lua b/test/testecho.lua new file mode 100644 index 00000000..a6b265ef --- /dev/null +++ b/test/testecho.lua @@ -0,0 +1,44 @@ +local skynet = require "skynet" + +local mode = ... + +if mode == "slave" then + +skynet.start(function() + skynet.dispatch("lua", function(_,_, ...) + skynet.ret(skynet.pack(...)) + end) +end) + +else + +skynet.start(function() + local slave = skynet.newservice(SERVICE_NAME, "slave") + local n = 100000 + local start = skynet.now() + print("call salve", n, "times in queue") + for i=1,n do + skynet.call(slave, "lua") + end + print("qps = ", n/ (skynet.now() - start) * 100) + + start = skynet.now() + + local worker = 10 + local task = n/worker + print("call salve", n, "times in parallel, worker = ", worker) + + for i=1,worker do + skynet.fork(function() + for i=1,task do + skynet.call(slave, "lua") + end + worker = worker -1 + if worker == 0 then + print("qps = ", n/ (skynet.now() - start) * 100) + end + end) + end +end) + +end From 11c528987c2f4a53c066716597b14fe9c3f8a602 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 31 Jul 2014 17:22:59 +0800 Subject: [PATCH 130/729] bugfix: socket.read(fd, nil) should clear buffer size to 0 --- lualib-src/lua-socket.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 916fa7a9..81322d17 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -246,6 +246,7 @@ lclearbuffer(lua_State *L) { while(sb->head) { return_free_node(L,2,sb); } + sb->size = 0; return 0; } @@ -264,6 +265,7 @@ lreadall(lua_State *L) { return_free_node(L,2,sb); } luaL_pushresult(&b); + sb->size = 0; return 1; } From 0747a7d7e1ec1832b23fb10ba81bbcd1fc38d2a1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 31 Jul 2014 20:30:22 +0800 Subject: [PATCH 131/729] remove register_slave --- lualib/snax/loginserver.lua | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index c5de769f..c099ddb8 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -143,16 +143,11 @@ local function launch_master(conf) local balance = 1 skynet.dispatch("lua", function(_,source,command, ...) - if command == "register_slave" then - table.insert(slave, source) - skynet.ret(skynet.pack(nil)) - else - skynet.ret(skynet.pack(conf.command_handler(command, ...))) - end + skynet.ret(skynet.pack(conf.command_handler(command, ...))) end) for i=1,instance do - skynet.newservice(SERVICE_NAME) + table.insert(slave, skynet.newservice(SERVICE_NAME)) end skynet.error(string.format("login server listen at : %s %d", host, port)) @@ -178,7 +173,6 @@ local function login(conf) skynet.start(function() local loginmaster = skynet.localname(name) if loginmaster then - skynet.call(loginmaster, "lua", "register_slave") local auth_handler = assert(conf.auth_handler) launch_master = nil conf = nil From b49fc291a7e159da52609ed9c735d8b1714f3a8f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 2 Aug 2014 18:32:54 +0800 Subject: [PATCH 132/729] bugfix: delete local channel --- service/multicastd.lua | 6 ++++-- test/testmulticast2.lua | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/service/multicastd.lua b/service/multicastd.lua index 66e4f1c4..dba56359 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -50,8 +50,10 @@ function command.DEL(source, c) channel[c] = nil channel_n[c] = nil channel_remote[c] = nil - for node in pairs(remote) do - skynet.send(node_address[node], "lua", "DELR", c) + if remote then + for node in pairs(remote) do + skynet.send(node_address[node], "lua", "DELR", c) + end end return NORET end diff --git a/test/testmulticast2.lua b/test/testmulticast2.lua index 0f55b77d..ff1342c2 100644 --- a/test/testmulticast2.lua +++ b/test/testmulticast2.lua @@ -4,9 +4,13 @@ local mc = require "multicast" skynet.start(function() print("remote start") - skynet.monitor("simplemonitor", true) local console = skynet.newservice("console") local channel = dc.get "MCCHANNEL" + if channel then + print("remote channel", channel) + else + print("create local channel") + end for i=1,10 do local sub = skynet.newservice("testmulticast", "sub") skynet.call(sub, "lua", "init", channel) From 4673344e1e8f2d8f0e07b8232098e61c4aa443ec Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 2 Aug 2014 22:02:17 +0800 Subject: [PATCH 133/729] update readme, add wiki link --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e2d604f1..ceaa4d56 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,6 @@ Each lua file only load once and cache it in memory during skynet start . so if You can also use the offical lua version , edit the makefile by yourself . -## Blog (in Chinese) +## How To (in Chinese) -* http://blog.codingnow.com/2012/09/the_design_of_skynet.html -* http://blog.codingnow.com/2012/08/skynet.html -* http://blog.codingnow.com/eo/skynet/ +* Read Wiki https://github.com/cloudwu/skynet/wiki From 7beed39b1d60854f305a19320f05e366e58fc61c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 4 Aug 2014 16:34:29 +0800 Subject: [PATCH 134/729] bugfix: use temp array for each request --- lualib/http/httpd.lua | 11 +++-------- lualib/http/sockethelper.lua | 12 ++++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 1fe507de..190b6b93 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -64,16 +64,11 @@ local function recvheader(readbytes, limit, lines, header) end end end - local idx = 1 for v in header:gmatch("(.-)\r\n") do if v == "" then break end - lines[idx] = v - idx = idx + 1 - end - for i = idx, #lines do - lines[i] = nil + table.insert(lines, v) end return result end @@ -131,8 +126,6 @@ local function readcrln(readbytes, body) end end -local tmpline = {} - local function recvchunkedbody(readbytes, bodylimit, header, body) local result = "" local size = 0 @@ -167,6 +160,7 @@ local function recvchunkedbody(readbytes, bodylimit, header, body) if not body then return end + local tmpline = {} body = recvheader(readbytes, 8192, tmpline, body) if not body then return @@ -178,6 +172,7 @@ local function recvchunkedbody(readbytes, bodylimit, header, body) end local function readall(readbytes, bodylimit) + local tmpline = {} local body = recvheader(readbytes, 8192, tmpline, "") if not body then return 413 -- Request Entity Too Large diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 89c7ec93..febe4cea 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -28,4 +28,16 @@ function sockethelper.writefunc(fd) end end +function sockethelper.connect(host, port) + local fd = socket.open(host, port) + if fd then + return fd + end + error(socket_error) +end + +function sockethelper.close(fd) + socket.close(fd) +end + return sockethelper From dec2e6fe4c1fd0d21a1bfe82f9649b4073395d5d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 4 Aug 2014 18:06:16 +0800 Subject: [PATCH 135/729] add httpc --- examples/simpleweb.lua | 5 ++ lualib/http/httpc.lua | 114 ++++++++++++++++++++++++++++++++ lualib/http/httpd.lua | 136 ++------------------------------------- lualib/http/internal.lua | 135 ++++++++++++++++++++++++++++++++++++++ test/testhttp.lua | 16 +++++ 5 files changed, 276 insertions(+), 130 deletions(-) create mode 100644 lualib/http/httpc.lua create mode 100644 lualib/http/internal.lua create mode 100644 test/testhttp.lua diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index 19870aba..234c2d08 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -39,6 +39,11 @@ skynet.start(function() table.insert(tmp, string.format("query: %s= %s", k,v)) end end + table.insert(tmp, "-----header----") + for k,v in pairs(header) do + table.insert(tmp, string.format("%s = %s",k,v)) + end + table.insert(tmp, "-----body----\n" .. body) response(id, code, table.concat(tmp,"\n")) end else diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua new file mode 100644 index 00000000..784b186b --- /dev/null +++ b/lualib/http/httpc.lua @@ -0,0 +1,114 @@ +local socket = require "http.sockethelper" +local url = require "http.url" +local internal = require "http.internal" +local string = string +local table = table + +local httpc = {} + +local function request(fd, method, host, url, recvheader, header, content) + local read = socket.readfunc(fd) + local write = socket.writefunc(fd) + local header_content = "" + if header then + for k,v in pairs(header) do + header_content = string.format("%s%s:%s\r\n", header_content, k, v) + end + end + + if content then + local data = string.format("%s %s HTTP/1.1\r\nhost:%s\r\ncontent-length:%d\r\n%s\r\n%s", method, url, host, #content, header_content, content) + write(data) + else + local request_header = string.format("%s %s HTTP/1.1\r\nhost:%s\r\ncontent-length:0\r\n%s\r\n", method, url, host, header_content) + write(request_header) + end + + local tmpline = {} + local body = internal.recvheader(read, tmpline, "") + if not body then + error(socket.socket_error) + end + + local statusline = tmpline[1] + local code, info = statusline:match "HTTP/[%d%.]+%s+([%d]+)%s+(.*)$" + code = assert(tonumber(code)) + + local header = internal.parseheader(tmpline,2,recvheader or {}) + if not header then + error("Invalid HTTP response header") + end + + local length = header["content-length"] + if length then + length = tonumber(length) + end + local mode = header["transfer-encoding"] + if mode then + if mode ~= "identity" and mode ~= "chunked" then + error ("Unsupport transfer-encoding") + end + end + + if mode == "chunked" then + body, header = internal.recvchunkedbody(read, nil, header, body) + if not body then + error("Invalid response body") + end + else + -- identity mode + if length then + if #body >= length then + body = body:sub(1,length) + else + local padding = read(length - #body) + body = body .. padding + end + else + body = nil + end + end + + return code, body +end + +function httpc.request(method, host, url, recvheader, header, content) + local hostname, port = host:match"([^:]+):?(%d*)$" + if port == "" then + port = 80 + else + port = tonumber(port) + end + local fd = socket.connect(hostname, port) + local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content) + if ok then + return statuscode, body + else + socket.close(fd) + error(statuscode) + end +end + +function httpc.get(...) + return httpc.request("GET", ...) +end + +local function escape(s) + return (string.gsub(s, "([^A-Za-z0-9_])", function(c) + return string.format("%%%02X", string.byte(c)) + end)) +end + +function httpc.post(host, url, form, recvheader) + local header = { + ["content-type"] = "application/x-www-form-urlencoded" + } + local body = {} + for k,v in pairs(form) do + table.insert(body, string.format("%s=%s",escape(k),escape(v))) + end + + return httpc.request("POST", host, url, recvheader, header, table.concat(body , "&")) +end + +return httpc diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 190b6b93..5f8f6de8 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -1,3 +1,5 @@ +local internal = require "http.internal" + local table = table local httpd = {} @@ -45,135 +47,9 @@ local http_status_msg = { [505] = "HTTP Version not supported", } -local function recvheader(readbytes, limit, lines, header) - local result - local e = header:find("\r\n\r\n", 1, true) - if e then - result = header:sub(e+4) - else - while true do - local bytes = readbytes() - header = header .. bytes - if #header > limit then - return - end - e = header:find("\r\n\r\n", -#bytes-3, true) - if e then - result = header:sub(e+4) - break - end - end - end - for v in header:gmatch("(.-)\r\n") do - if v == "" then - break - end - table.insert(lines, v) - end - return result -end - -local function parseheader(lines, from, header) - local name, value - for i=from,#lines do - local line = lines[i] - if line:byte(1) == 9 then -- tab, append last line - if name == nil then - return - end - header[name] = header[name] .. line:sub(2) - else - name, value = line:match "^(.-):%s*(.*)" - if name == nil or value == nil then - return - end - name = name:lower() - if header[name] then - header[name] = header[name] .. ", " .. value - else - header[name] = value - end - end - end - return header -end - -local function chunksize(readbytes, body) - while true do - if #body > 128 then - return - end - body = body .. readbytes() - local f,e = body:find("\r\n",1,true) - if f then - return tonumber(body:sub(1,f-1),16), body:sub(e+1) - end - end -end - -local function readcrln(readbytes, body) - if #body > 2 then - if body:sub(1,2) ~= "\r\n" then - return - end - return body:sub(3) - else - body = body .. readbytes(2-#body) - if body ~= "\r\n" then - return - end - return "" - end -end - -local function recvchunkedbody(readbytes, bodylimit, header, body) - local result = "" - local size = 0 - - while true do - local sz - sz , body = chunksize(readbytes, body) - if not sz then - return - end - if sz == 0 then - break - end - size = size + sz - if bodylimit and size > bodylimit then - return - end - if #body >= sz then - result = result .. body:sub(1,sz) - body = body:sub(sz+1) - else - result = result .. body .. readbytes(sz - #body) - body = "" - end - body = readcrln(readbytes, body) - if not body then - return - end - end - - body = readcrln(readbytes, body) - if not body then - return - end - local tmpline = {} - body = recvheader(readbytes, 8192, tmpline, body) - if not body then - return - end - - header = parseheader(tmpline,1,header) - - return result, header -end - local function readall(readbytes, bodylimit) local tmpline = {} - local body = recvheader(readbytes, 8192, tmpline, "") + local body = internal.recvheader(readbytes, tmpline, "") if not body then return 413 -- Request Entity Too Large end @@ -184,7 +60,7 @@ local function readall(readbytes, bodylimit) if httpver < 1.0 or httpver > 1.1 then return 505 -- HTTP Version not supported end - local header = parseheader(tmpline,2,{}) + local header = internal.parseheader(tmpline,2,{}) if not header then return 400 -- Bad request end @@ -194,13 +70,13 @@ local function readall(readbytes, bodylimit) end local mode = header["transfer-encoding"] if mode then - if mode ~= "identity" or mode ~= "chunked" then + if mode ~= "identity" and mode ~= "chunked" then return 501 -- Not Implemented end end if mode == "chunked" then - body, header = recvchunkedbody(readbytes, bodylimit, header, body) + body, header = internal.recvchunkedbody(readbytes, bodylimit, header, body) if not body then return 413 end diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua new file mode 100644 index 00000000..067152ad --- /dev/null +++ b/lualib/http/internal.lua @@ -0,0 +1,135 @@ +local M = {} + +local LIMIT = 8192 + +local function chunksize(readbytes, body) + while true do + if #body > 128 then + return + end + body = body .. readbytes() + local f,e = body:find("\r\n",1,true) + if f then + return tonumber(body:sub(1,f-1),16), body:sub(e+1) + end + end +end + +local function readcrln(readbytes, body) + if #body >= 2 then + if body:sub(1,2) ~= "\r\n" then + return + end + return body:sub(3) + else + body = body .. readbytes(2-#body) + if body ~= "\r\n" then + return + end + return "" + end +end + +function M.recvheader(readbytes, lines, header) + if #header >= 2 then + if header:find "^\r\n" then + return header:sub(3) + end + end + local result + local e = header:find("\r\n\r\n", 1, true) + if e then + result = header:sub(e+4) + else + while true do + local bytes = readbytes() + header = header .. bytes + if #header > LIMIT then + return + end + e = header:find("\r\n\r\n", -#bytes-3, true) + if e then + result = header:sub(e+4) + break + end + if header:find "^\r\n" then + return header:sub(3) + end + end + end + for v in header:gmatch("(.-)\r\n") do + if v == "" then + break + end + table.insert(lines, v) + end + return result +end + +function M.parseheader(lines, from, header) + local name, value + for i=from,#lines do + local line = lines[i] + if line:byte(1) == 9 then -- tab, append last line + if name == nil then + return + end + header[name] = header[name] .. line:sub(2) + else + name, value = line:match "^(.-):%s*(.*)" + if name == nil or value == nil then + return + end + name = name:lower() + if header[name] then + header[name] = header[name] .. ", " .. value + else + header[name] = value + end + end + end + return header +end + +function M.recvchunkedbody(readbytes, bodylimit, header, body) + local result = "" + local size = 0 + + while true do + local sz + sz , body = chunksize(readbytes, body) + if not sz then + return + end + if sz == 0 then + break + end + size = size + sz + if bodylimit and size > bodylimit then + return + end + if #body >= sz then + result = result .. body:sub(1,sz) + body = body:sub(sz+1) + else + result = result .. body .. readbytes(sz - #body) + body = "" + end + body = readcrln(readbytes, body) + if not body then + return + end + end + + local tmpline = {} + body = M.recvheader(readbytes, tmpline, body) + if not body then + return + end + + header = M.parseheader(tmpline,1,header) + + return result, header +end + +return M diff --git a/test/testhttp.lua b/test/testhttp.lua new file mode 100644 index 00000000..52abbe4b --- /dev/null +++ b/test/testhttp.lua @@ -0,0 +1,16 @@ +local skynet = require "skynet" +local httpc = require "http.httpc" + +skynet.start(function() + print("GET www.baidu.com") + local header = {} + local status, body = httpc.get("baidu.com", "/", header) + print("[header] =====>") + for k,v in pairs(header) do + print(k,v) + end + print("[body] =====>", status) + print(body) + + skynet.exit() +end) From b18962929b6f54d342c84e0ea9be38c29b60a513 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 4 Aug 2014 18:08:05 +0800 Subject: [PATCH 136/729] bugfix: chunked mode --- lualib/http/httpd.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 190b6b93..a0c027f2 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -46,6 +46,11 @@ local http_status_msg = { } local function recvheader(readbytes, limit, lines, header) + if #header >= 2 then + if header:find "^\r\n" then + return header:sub(3) + end + end local result local e = header:find("\r\n\r\n", 1, true) if e then @@ -62,6 +67,9 @@ local function recvheader(readbytes, limit, lines, header) result = header:sub(e+4) break end + if header:find "^\r\n" then + return header:sub(3) + end end end for v in header:gmatch("(.-)\r\n") do @@ -156,10 +164,6 @@ local function recvchunkedbody(readbytes, bodylimit, header, body) end end - body = readcrln(readbytes, body) - if not body then - return - end local tmpline = {} body = recvheader(readbytes, 8192, tmpline, body) if not body then @@ -194,7 +198,7 @@ local function readall(readbytes, bodylimit) end local mode = header["transfer-encoding"] if mode then - if mode ~= "identity" or mode ~= "chunked" then + if mode ~= "identity" and mode ~= "chunked" then return 501 -- Not Implemented end end From 87a02ffe72be68ded1addf879f1cc4199a64a7bc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Aug 2014 18:06:08 +0800 Subject: [PATCH 137/729] don't check imported function in snax.hotfix --- HISTORY.md | 1 + lualib/snax/hotfix.lua | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 23303874..63dcced0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,6 +4,7 @@ Dev version * bugfix: service exit before init would not report back * add skynet.response and check multicall skynet.ret * skynet.newservice throw error when lanuch faild +* Don't check imported function in snax.hotfix v0.5.0 (2014-7-28) ----------- diff --git a/lualib/snax/hotfix.lua b/lualib/snax/hotfix.lua index e0cc2a7f..1a60383a 100644 --- a/lualib/snax/hotfix.lua +++ b/lualib/snax/hotfix.lua @@ -3,7 +3,21 @@ local io = io local hotfix = {} -local function collect_uv(f , uv) +local function envid(f) + local i = 1 + while true do + local name, value = debug.getupvalue(f, i) + if name == nil then + return + end + if name == "_ENV" then + return debug.upvalueid(f, i) + end + i = i + 1 + end +end + +local function collect_uv(f , uv, env) local i = 1 while true do local name, value = debug.getupvalue(f, i) @@ -18,7 +32,9 @@ local function collect_uv(f , uv) uv[name] = { func = f, index = i, id = id } if type(value) == "function" then - collect_uv(value, uv) + if envid(value) == env then + collect_uv(value, uv, env) + end end end @@ -30,7 +46,7 @@ local function collect_all_uv(funcs) local global = {} for _, v in pairs(funcs) do if v[4] then - collect_uv(v[4], global) + collect_uv(v[4], global, envid(v[4])) end end From cab10edd1d40d19b2ea25982faabb2c6f38d5f79 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Aug 2014 19:34:48 +0800 Subject: [PATCH 138/729] snaxd change add package.path --- HISTORY.md | 1 + lualib/snax/interface.lua | 5 ++++- service/snaxd.lua | 9 ++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 63dcced0..a161f546 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,7 @@ Dev version * add skynet.response and check multicall skynet.ret * skynet.newservice throw error when lanuch faild * Don't check imported function in snax.hotfix +* snax service add change SERVICE_PATH and add it to package.path v0.5.0 (2014-7-28) ----------- diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index 24d52c08..a2f8aa64 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -61,6 +61,8 @@ return function (name , G, loader) setmetatable(G, { __index = env , __newindex = init_system }) + local pattern + do local path = skynet.getenv "snax" @@ -70,6 +72,7 @@ return function (name , G, loader) local filename = string.gsub(pat, "?", name) local f , err = loader(filename, "bt", G) if f then + pattern = pat mainfunc = f break else @@ -90,5 +93,5 @@ return function (name , G, loader) G[k] = v end - return func + return func, pattern end diff --git a/service/snaxd.lua b/service/snaxd.lua index de41d6c3..28caefce 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -4,7 +4,14 @@ local snax_interface = require "snax.interface" local profile = require "profile" local snax = require "snax" -local func = snax_interface(tostring(...), _ENV) +local snax_name = tostring(...) +local func, pattern = snax_interface(snax_name, _ENV) +local snax_path = pattern:sub(1,pattern:find("?", 1, true)-1) .. snax_name .. "/" +package.path = snax_path .. "?.lua;" .. package.path + +SERVICE_NAME = snax_name +SERVICE_PATH = snax_path + local mode local thread_id local message_queue = {} From 023e4abd87324b31f8efc0c83fa02e61e746b6d2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 8 Aug 2014 15:38:06 +0800 Subject: [PATCH 139/729] reset current_point when time error --- skynet-src/skynet_timer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 3cd0c51d..2856f389 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -265,6 +265,7 @@ skynet_updatetime(void) { uint64_t cp = gettime(); if(cp < TI->current_point) { skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point); + TI->current_point = cp; } else if (cp != TI->current_point) { uint32_t diff = (uint32_t)(cp - TI->current_point); TI->current_point = cp; From f6fa6c7dedbfba6160067e2c0e2cc176026a3e1e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 8 Aug 2014 20:33:38 +0800 Subject: [PATCH 140/729] timer support more than 497 days --- HISTORY.md | 6 ++++ skynet-src/skynet_main.c | 1 - skynet-src/skynet_timer.c | 62 +++++++++++++++++++++++---------------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 81718ed3..8ad3a0f8 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +v0.5.1 (2014-8-4) +----------- +* Bugfix : http module +* Bugfix : multicast local channel delete +* Bugfix : socket.read(fd) + v0.5.0 (2014-7-28) ----------- * skynet.exit will quit service immediately. diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 4eb18a59..fce5debb 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -89,7 +89,6 @@ static const char * load_config = "\ local code = assert(f:read \'*a\')\ local function getenv(name) return assert(os.getenv(name), name) end\ code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\ - print(code)\ f:close()\ local result = {}\ assert(load(code,\'=(load)\',\'t\',result))()\ diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 3cd0c51d..3d0421a9 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -9,6 +9,7 @@ #include #include #include +#include #if defined(__APPLE__) #include @@ -33,7 +34,7 @@ struct timer_event { struct timer_node { struct timer_node *next; - int expire; + uint32_t expire; }; struct link_list { @@ -43,9 +44,9 @@ struct link_list { struct timer { struct link_list near[TIME_NEAR]; - struct link_list t[4][TIME_LEVEL-1]; + struct link_list t[4][TIME_LEVEL]; int lock; - int time; + uint32_t time; uint32_t current; uint32_t starttime; uint64_t current_point; @@ -72,21 +73,22 @@ link(struct link_list *list,struct timer_node *node) { static void add_node(struct timer *T,struct timer_node *node) { - int time=node->expire; - int current_time=T->time; + uint32_t time=node->expire; + uint32_t current_time=T->time; if ((time|TIME_NEAR_MASK)==(current_time|TIME_NEAR_MASK)) { link(&T->near[time&TIME_NEAR_MASK],node); } else { int i; - int mask=TIME_NEAR << TIME_LEVEL_SHIFT; + uint32_t mask=TIME_NEAR << TIME_LEVEL_SHIFT; for (i=0;i<3;i++) { if ((time|(mask-1))==(current_time|(mask-1))) { break; } mask <<= TIME_LEVEL_SHIFT; } - link(&T->t[i][((time>>(TIME_NEAR_SHIFT + i*TIME_LEVEL_SHIFT)) & TIME_LEVEL_MASK)-1],node); + + link(&T->t[i][((time>>(TIME_NEAR_SHIFT + i*TIME_LEVEL_SHIFT)) & TIME_LEVEL_MASK)],node); } } @@ -103,29 +105,38 @@ timer_add(struct timer *T,void *arg,size_t sz,int time) { UNLOCK(T); } +static void +move_list(struct timer *T, int level, int idx) { + struct timer_node *current = link_clear(&T->t[level][idx]); + while (current) { + struct timer_node *temp=current->next; + add_node(T,current); + current=temp; + } +} + static void timer_shift(struct timer *T) { LOCK(T); int mask = TIME_NEAR; - int time = (++T->time) >> TIME_NEAR_SHIFT; - int i=0; - - while ((T->time & (mask-1))==0) { - int idx=time & TIME_LEVEL_MASK; - if (idx!=0) { - --idx; - struct timer_node *current = link_clear(&T->t[i][idx]); - while (current) { - struct timer_node *temp=current->next; - add_node(T,current); - current=temp; + uint32_t ct = ++T->time; + if (ct == 0) { + move_list(T, 3, 0); + } else { + uint32_t time = ct >> TIME_NEAR_SHIFT; + int i=0; + + while ((ct & (mask-1))==0) { + int idx=time & TIME_LEVEL_MASK; + if (idx!=0) { + move_list(T, i, idx); + break; } - break; + mask <<= TIME_LEVEL_SHIFT; + time >>= TIME_LEVEL_SHIFT; + ++i; } - mask <<= TIME_LEVEL_SHIFT; - time >>= TIME_LEVEL_SHIFT; - ++i; - } + } UNLOCK(T); } @@ -187,7 +198,7 @@ timer_create_timer() { } for (i=0;i<4;i++) { - for (j=0;jt[i][j]); } } @@ -265,6 +276,7 @@ skynet_updatetime(void) { uint64_t cp = gettime(); if(cp < TI->current_point) { skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point); + TI->current_point = cp; } else if (cp != TI->current_point) { uint32_t diff = (uint32_t)(cp - TI->current_point); TI->current_point = cp; From 77d53cf5f1a37d3c11ab8f338b4b98ac49d40970 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Aug 2014 09:45:37 +0800 Subject: [PATCH 141/729] release v0.5.2 --- HISTORY.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 8ad3a0f8..375687fd 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,10 @@ +v0.5.2 (2014-8-11) +----------- +* Bugfix : httpd request +* Bugifx : http chunked mode +* Add : httpc +* timer support more than 497 days + v0.5.1 (2014-8-4) ----------- * Bugfix : http module From d7b228f34230d057ed328ebc3478bf35dc5d6076 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Aug 2014 15:45:16 +0800 Subject: [PATCH 142/729] use .core for lua c module name --- lualib-src/lua-cluster.c | 2 +- lualib-src/lua-multicast.c | 2 +- lualib/mqueue.lua | 2 +- lualib/multicast.lua | 2 +- service/clusterd.lua | 2 +- service/multicastd.lua | 2 +- service/snaxd.lua | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 29ebb08a..8ba1b7d6 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -203,7 +203,7 @@ lunpackresponse(lua_State *L) { } int -luaopen_cluster_c(lua_State *L) { +luaopen_cluster_core(lua_State *L) { luaL_Reg l[] = { { "packrequest", lpackrequest }, { "unpackrequest", lunpackrequest }, diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 25b9d936..866821d4 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -149,7 +149,7 @@ mc_nextid(lua_State *L) { } int -luaopen_multicast_c(lua_State *L) { +luaopen_multicast_core(lua_State *L) { luaL_Reg l[] = { { "pack", mc_packlocal }, { "unpack", mc_unpacklocal }, diff --git a/lualib/mqueue.lua b/lualib/mqueue.lua index f21921ca..13c2f327 100644 --- a/lualib/mqueue.lua +++ b/lualib/mqueue.lua @@ -1,7 +1,7 @@ -- This is a deprecated module, use skynet.queue instead. local skynet = require "skynet" -local c = require "skynet.c" +local c = require "skynet.core" local mqueue = {} local init_once diff --git a/lualib/multicast.lua b/lualib/multicast.lua index 502f8d21..1f5fbd15 100644 --- a/lualib/multicast.lua +++ b/lualib/multicast.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local mc = require "multicast.c" +local mc = require "multicast.core" local multicastd local multicast = {} diff --git a/service/clusterd.lua b/service/clusterd.lua index f7a9e90f..3032784e 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" local sc = require "socketchannel" local socket = require "socket" -local cluster = require "cluster.c" +local cluster = require "cluster.core" local config_name = skynet.getenv "cluster" local node_address = {} diff --git a/service/multicastd.lua b/service/multicastd.lua index dba56359..5a7f9d2a 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local mc = require "multicast.c" +local mc = require "multicast.core" local datacenter = require "datacenter" local harbor_id = skynet.harbor(skynet.self()) diff --git a/service/snaxd.lua b/service/snaxd.lua index 28caefce..0ff9baa0 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local c = require "skynet.c" +local c = require "skynet.core" local snax_interface = require "snax.interface" local profile = require "profile" local snax = require "snax" From f69f6e8e704ac75d2a13e1ca7a81f93679e890c6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 12 Aug 2014 14:33:36 +0800 Subject: [PATCH 143/729] add stm object support. (STM : Software transactional memory) --- Makefile | 5 +- lualib-src/lua-stm.c | 244 +++++++++++++++++++++++++++++++++++++++++++ test/teststm.lua | 36 +++++++ 3 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 lualib-src/lua-stm.c create mode 100644 test/teststm.lua diff --git a/Makefile b/Makefile index 1bcb3603..ed8d1c7d 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ jemalloc : $(MALLOC_STATICLIB) CSERVICE = snlua logger gate harbor LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ cjson clientsocket memory profile multicast \ - cluster crypt sharedata + cluster crypt sharedata stm SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -116,6 +116,9 @@ $(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ +$(LUA_CLIB_PATH)/stm.so : lualib-src/lua-stm.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ + clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c new file mode 100644 index 00000000..afe26f07 --- /dev/null +++ b/lualib-src/lua-stm.c @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include + +#include "rwlock.h" +#include "skynet_malloc.h" + +struct stm_object { + struct rwlock lock; + int reference; + struct stm_copy * copy; +}; + +struct stm_copy { + int reference; + uint32_t sz; + void * msg; +}; + +// msg should alloc by skynet_malloc +static struct stm_copy * +stm_newcopy(void * msg, int32_t sz) { + struct stm_copy * copy = skynet_malloc(sizeof(*copy)); + copy->reference = 1; + copy->sz = sz; + copy->msg = msg; + + return copy; +} + +static struct stm_object * +stm_new(void * msg, int32_t sz) { + struct stm_object * obj = skynet_malloc(sizeof(*obj)); + rwlock_init(&obj->lock); + obj->reference = 1; + obj->copy = stm_newcopy(msg, sz); + + return obj; +} + +static void +stm_releasecopy(struct stm_copy *copy) { + if (copy == NULL) + return; + if (__sync_sub_and_fetch(©->reference, 1) == 0) { + skynet_free(copy->msg); + skynet_free(copy); + } +} + +static void +stm_release(struct stm_object *obj) { + assert(obj->copy); + rwlock_wlock(&obj->lock); + // writer release the stm object, so release the last copy . + stm_releasecopy(obj->copy); + obj->copy = NULL; + if (--obj->reference > 0) { + // stm object grab by readers, reset the copy to NULL. + rwlock_wunlock(&obj->lock); + return; + } + // no one grab the stm object, no need to unlock wlock. + skynet_free(obj); +} + +static void +stm_releasereader(struct stm_object *obj) { + rwlock_rlock(&obj->lock); + if (__sync_sub_and_fetch(&obj->reference,1) == 0) { + // last reader, no writer. so no need to unlock + assert(obj->copy == NULL); + skynet_free(obj); + return; + } + rwlock_runlock(&obj->lock); +} + +static void +stm_grab(struct stm_object *obj) { + rwlock_rlock(&obj->lock); + int ref = __sync_fetch_and_add(&obj->reference,1); + rwlock_runlock(&obj->lock); + assert(ref > 0); +} + +static struct stm_copy * +stm_copy(struct stm_object *obj) { + rwlock_rlock(&obj->lock); + struct stm_copy * ret = obj->copy; + if (ret) { + int ref = __sync_fetch_and_add(&ret->reference,1); + assert(ref > 0); + } + rwlock_runlock(&obj->lock); + + return ret; +} + +static void +stm_update(struct stm_object *obj, void *msg, int32_t sz) { + struct stm_copy *copy = stm_newcopy(msg, sz); + rwlock_wlock(&obj->lock); + struct stm_copy *oldcopy = obj->copy; + obj->copy = copy; + rwlock_wunlock(&obj->lock); + + stm_releasecopy(oldcopy); +} + +// lua binding + +struct boxstm { + struct stm_object * obj; +}; + +static int +lcopy(lua_State *L) { + struct boxstm * box = lua_touserdata(L, 1); + stm_grab(box->obj); + lua_pushlightuserdata(L, box->obj); + return 1; +} + +static int +lnewwriter(lua_State *L) { + void * msg = lua_touserdata(L, 1); + uint32_t sz = luaL_checkunsigned(L, 2); + struct boxstm * box = lua_newuserdata(L, sizeof(*box)); + box->obj = stm_new(msg,sz); + lua_pushvalue(L, lua_upvalueindex(1)); + lua_setmetatable(L, -2); + + return 1; +} + +static int +ldeletewriter(lua_State *L) { + struct boxstm * box = lua_touserdata(L, 1); + stm_release(box->obj); + box->obj = NULL; + + return 0; +} + +static int +lupdate(lua_State *L) { + struct boxstm * box = lua_touserdata(L, 1); + void * msg = lua_touserdata(L, 2); + uint32_t sz = luaL_checkunsigned(L, 3); + stm_update(box->obj, msg, sz); + + return 0; +} + +struct boxreader { + struct stm_object *obj; + struct stm_copy *lastcopy; +}; + +static int +lnewreader(lua_State *L) { + struct boxreader * box = lua_newuserdata(L, sizeof(*box)); + box->obj = lua_touserdata(L, 1); + box->lastcopy = NULL; + lua_pushvalue(L, lua_upvalueindex(1)); + lua_setmetatable(L, -2); + + return 1; +} + +static int +ldeletereader(lua_State *L) { + struct boxreader * box = lua_touserdata(L, 1); + stm_releasereader(box->obj); + box->obj = NULL; + stm_releasecopy(box->lastcopy); + box->lastcopy = NULL; + + return 0; +} + +static int +lread(lua_State *L) { + struct boxreader * box = lua_touserdata(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + struct stm_copy * copy = stm_copy(box->obj); + if (copy == box->lastcopy) { + // not update + stm_releasecopy(copy); + lua_pushboolean(L, 0); + return 1; + } + + stm_releasecopy(box->lastcopy); + box->lastcopy = copy; + if (copy) { + lua_settop(L, 2); + lua_pushlightuserdata(L, copy->msg); + lua_pushunsigned(L, copy->sz); + lua_call(L, 2, LUA_MULTRET); + lua_pushboolean(L, 1); + lua_replace(L, 1); + return lua_gettop(L); + } else { + lua_pushboolean(L, 0); + return 1; + } +} + +int +luaopen_stm(lua_State *L) { + luaL_checkversion(L); + lua_createtable(L, 0, 3); + + lua_pushcfunction(L, lcopy); + lua_setfield(L, -2, "copy"); + + luaL_Reg writer[] = { + { "new", lnewwriter }, + { NULL, NULL }, + }; + lua_createtable(L, 0, 2); + lua_pushcfunction(L, ldeletewriter), + lua_setfield(L, -2, "__gc"); + lua_pushcfunction(L, lupdate), + lua_setfield(L, -2, "__call"); + luaL_setfuncs(L, writer, 1); + + luaL_Reg reader[] = { + { "newcopy", lnewreader }, + { NULL, NULL }, + }; + lua_createtable(L, 0, 2); + lua_pushcfunction(L, ldeletereader), + lua_setfield(L, -2, "__gc"); + lua_pushcfunction(L, lread), + lua_setfield(L, -2, "__call"); + luaL_setfuncs(L, reader, 1); + + return 1; +} diff --git a/test/teststm.lua b/test/teststm.lua new file mode 100644 index 00000000..7416a9f2 --- /dev/null +++ b/test/teststm.lua @@ -0,0 +1,36 @@ +local skynet = require "skynet" +local stm = require "stm" + +local mode = ... + +if mode == "slave" then + +skynet.start(function() + skynet.dispatch("lua", function (_,_, obj) + local obj = stm.newcopy(obj) + print("read:", obj(skynet.unpack)) + skynet.ret() + skynet.error("sleep and read") + for i=1,10 do + skynet.sleep(10) + print("read:", obj(skynet.unpack)) + end + skynet.exit() + end) +end) + +else + +skynet.start(function() + local slave = skynet.newservice(SERVICE_NAME, "slave") + local obj = stm.new(skynet.pack(1,2,3,4,5)) + local copy = stm.copy(obj) + skynet.call(slave, "lua", copy) + for i=1,5 do + skynet.sleep(20) + print("write", i) + obj(skynet.pack("hello world", i)) + end + skynet.exit() +end) +end From cfe8b19c8ad8144d1d2a419e0d2b052d252e860f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 12 Aug 2014 21:41:28 +0800 Subject: [PATCH 144/729] change .slave to .cslave --- lualib/skynet/harbor.lua | 6 +++--- service/bootstrap.lua | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/skynet/harbor.lua b/lualib/skynet/harbor.lua index 5f470be8..a84d94ef 100644 --- a/lualib/skynet/harbor.lua +++ b/lualib/skynet/harbor.lua @@ -4,15 +4,15 @@ local harbor = {} function harbor.globalname(name, handle) handle = handle or skynet.self() - skynet.send(".slave", "lua", "REGISTER", name, handle) + skynet.send(".cslave", "lua", "REGISTER", name, handle) end function harbor.link(id) - skynet.call(".slave", "lua", "LINK", id) + skynet.call(".cslave", "lua", "LINK", id) end function harbor.connect(id) - skynet.call(".slave", "lua", "CONNECT", id) + skynet.call(".cslave", "lua", "CONNECT", id) end return harbor diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 06e67d7e..e1df7170 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -17,7 +17,7 @@ skynet.start(function() if not ok then skynet.abort() end - skynet.name(".slave", slave) + skynet.name(".cslave", slave) else if standalone then @@ -30,7 +30,7 @@ skynet.start(function() if not ok then skynet.abort() end - skynet.name(".slave", slave) + skynet.name(".cslave", slave) end if standalone then From cde423cb736f13a74b31519142d66f4a96112da1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 14:32:28 +0800 Subject: [PATCH 145/729] add skynet.harbor.queryname --- lualib/skynet/harbor.lua | 4 ++++ service/cdummy.lua | 28 ++++++++++++++++++++++++++++ service/cslave.lua | 38 ++++++++++++++++++++++++++++++++++---- test/testharborlink.lua | 1 + 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/harbor.lua b/lualib/skynet/harbor.lua index a84d94ef..b5863921 100644 --- a/lualib/skynet/harbor.lua +++ b/lualib/skynet/harbor.lua @@ -7,6 +7,10 @@ function harbor.globalname(name, handle) skynet.send(".cslave", "lua", "REGISTER", name, handle) end +function harbor.queryname(name) + return skynet.call(".cslave", "lua", "QUERYNAME", name) +end + function harbor.link(id) skynet.call(".cslave", "lua", "LINK", id) end diff --git a/service/cdummy.lua b/service/cdummy.lua index fddb2c01..107c626c 100644 --- a/service/cdummy.lua +++ b/service/cdummy.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" local globalname = {} +local queryname = {} local harbor = {} skynet.register_protocol { @@ -17,12 +18,39 @@ skynet.register_protocol { unpack = skynet.tostring, } +local function response_name(name) + local address = globalname[name] + if queryname[name] then + local tmp = queryname[name] + queryname[name] = nil + for _,resp in ipairs(tmp) do + resp(true, address) + end + end +end + function harbor.REGISTER(name, handle) assert(globalname[name] == nil) globalname[name] = handle + response_name(name) skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) end +function harbor.QUERYNAME(fd, name) + local result = globalname[name] + if result then + skynet.ret(skynet.pack(result)) + return + end + local queue = queryname[name] + if queue == nil then + queue = { skynet.response() } + queryname[name] = queue + else + table.insert(queue, skynet.response()) + end +end + function harbor.LINK(id) skynet.ret() end diff --git a/service/cslave.lua b/service/cslave.lua index 8a21aa43..a75e9f36 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -1,9 +1,11 @@ local skynet = require "skynet" local socket = require "socket" +local table = table local slaves = {} local connect_queue = {} local globalname = {} +local queryname = {} local harbor = {} local harbor_service local monitor = {} @@ -28,7 +30,7 @@ local function monitor_clear(id) if v then monitor[id] = nil for _, v in ipairs(v) do - v() + v(true) end end end @@ -60,6 +62,17 @@ local function ready() end end +local function response_name(name) + local address = globalname[name] + if queryname[name] then + local tmp = queryname[name] + queryname[name] = nil + for _,resp in ipairs(tmp) do + resp(true, address) + end + end +end + local function monitor_master(master_fd) while true do local ok, t, id_name, address = pcall(read_package,master_fd) @@ -72,6 +85,7 @@ local function monitor_master(master_fd) end elseif t == 'N' then globalname[id_name] = address + response_name(id_name) if connect_queue == nil then skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name) end @@ -152,6 +166,7 @@ end function harbor.REGISTER(fd, name, handle) assert(globalname[name] == nil) globalname[name] = handle + response_name(name) socket.write(fd, pack_package("R", name, handle)) skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) end @@ -161,7 +176,7 @@ function harbor.LINK(fd, id) if monitor[id] == nil then monitor[id] = {} end - table.insert(monitor[id], skynet.response(true)) + table.insert(monitor[id], skynet.response()) else skynet.ret() end @@ -172,19 +187,34 @@ function harbor.CONNECT(fd, id) if monitor[id] == nil then monitor[id] = {} end - table.insert(monitor[id], skynet.response(true)) + table.insert(monitor[id], skynet.response()) else skynet.ret() end end +function harbor.QUERYNAME(fd, name) + local result = globalname[name] + if result then + skynet.ret(skynet.pack(result)) + return + end + local queue = queryname[name] + if queue == nil then + queue = { skynet.response() } + queryname[name] = queue + else + table.insert(queue, skynet.response()) + end +end + skynet.start(function() local master_addr = skynet.getenv "master" local harbor_id = tonumber(skynet.getenv "harbor") local slave_address = assert(skynet.getenv "address") local slave_fd = socket.listen(slave_address) skynet.error("slave connect to master " .. tostring(master_addr)) - local master_fd = socket.open(master_addr) + local master_fd = assert(socket.open(master_addr), "Can't connect to master") skynet.dispatch("lua", function (_,_,command,...) local f = assert(harbor[command]) diff --git a/test/testharborlink.lua b/test/testharborlink.lua index 031e38ec..dfdb8613 100644 --- a/test/testharborlink.lua +++ b/test/testharborlink.lua @@ -6,6 +6,7 @@ skynet.start(function() print("run skynet examples/config_log please") harbor.connect(2) print("harbor 2 connected") + print("LOG =", skynet.address(harbor.queryname "LOG")) harbor.link(2) print("disconnected") end) From 978e010e67f6efed9fb33ecf033ff23d85007771 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 17:49:30 +0800 Subject: [PATCH 146/729] add cluster.proxy and cluster.ncall --- HISTORY.md | 2 ++ examples/cluster2.lua | 5 ++++- examples/globallog.lua | 1 + examples/simpledb.lua | 1 + lualib-src/lua-skynet.c | 15 +++++++++++++-- lualib/cluster.lua | 19 ++++++++++++++++++- lualib/skynet.lua | 18 +++++++++++++++++- service/cdummy.lua | 6 +++++- service/clusterd.lua | 10 ++++++++++ service/cslave.lua | 9 +++++++-- skynet-src/skynet_server.c | 2 +- 11 files changed, 79 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 6bf2437c..cb29fd78 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,6 +7,8 @@ Dev version * Don't check imported function in snax.hotfix * snax service add change SERVICE_PATH and add it to package.path * skynet.redirect support string address +* add skynet.harbor.queryname to query globalname +* add cluster.proxy and cluster.ncall v0.5.2 (2014-8-11) ----------- diff --git a/examples/cluster2.lua b/examples/cluster2.lua index a61ff5c4..d45605f8 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -2,5 +2,8 @@ local skynet = require "skynet" local cluster = require "cluster" skynet.start(function() - print(cluster.call("db", "SIMPLEDB", "GET", "a")) + local proxy = cluster.proxy("db", ".simpledb") + print(skynet.call(proxy, "lua", "GET", "a")) + print(cluster.ncall("db.simpledb","GET", "a")) + print(cluster.call("db", ".simpledb", "GET", "a")) end) diff --git a/examples/globallog.lua b/examples/globallog.lua index ffd0a0f3..2939756c 100644 --- a/examples/globallog.lua +++ b/examples/globallog.lua @@ -4,5 +4,6 @@ skynet.start(function() skynet.dispatch("lua", function(session, address, ...) print("[GLOBALLOG]", skynet.address(address), ...) end) + skynet.register ".log" skynet.register "LOG" end) diff --git a/examples/simpledb.lua b/examples/simpledb.lua index 3a7649e6..e70d2f86 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -22,5 +22,6 @@ skynet.start(function() error(string.format("Unknown command %s", tostring(cmd))) end end) + skynet.register ".simpledb" skynet.register "SIMPLEDB" end) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 7fe3bd02..644c1b2e 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -73,10 +73,17 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t return 0; } +static int +forward_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { + _cb(context, ud, type, session, source, msg, sz); + // don't delete msg in forward mode. + return 1; +} + static int _callback(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - + int forward = lua_toboolean(L, 2); luaL_checktype(L,1,LUA_TFUNCTION); lua_settop(L,1); lua_rawsetp(L, LUA_REGISTRYINDEX, _cb); @@ -84,7 +91,11 @@ _callback(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *gL = lua_tothread(L,-1); - skynet_callback(context, gL, _cb); + if (forward) { + skynet_callback(context, gL, forward_cb); + } else { + skynet_callback(context, gL, _cb); + } return 0; } diff --git a/lualib/cluster.lua b/lualib/cluster.lua index d53e6d9b..ec3f6abf 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -4,7 +4,7 @@ local clusterd local cluster = {} function cluster.call(node, address, ...) - -- skynet.pack(...) will free by cluster.c.packrequest + -- skynet.pack(...) will free by cluster.core.packrequest return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...)) end @@ -20,6 +20,23 @@ function cluster.reload() skynet.call(clusterd, "lua", "reload") end +function cluster.proxy(node, name) + return skynet.call(clusterd, "lua", "proxy", node, name) +end + +local namecache = {} + +function cluster.ncall(name, ...) + local s = namecache[name] + if not s then + local node , lname = name:match "(.-)(%..+)" + assert(node and lname) + s = cluster.proxy(node, lname) + namecache[name] = assert(s) + end + return skynet.call(s, "lua", ...) +end + skynet.init(function() clusterd = skynet.uniqueservice("clusterd") end) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index e1dcdc1f..563b0f26 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -99,6 +99,7 @@ local coroutine_yield = coroutine.yield local function co_create(f) local co = table.remove(coroutine_pool) if co == nil then + local print = print co = coroutine.create(function(...) f(...) while true do @@ -548,7 +549,7 @@ end function skynet.address(addr) if type(addr) == "number" then - return string.format(":%x",addr) + return string.format(":%08x",addr) else return tostring(addr) end @@ -648,6 +649,21 @@ function skynet.filter(f ,start_func) end) end +function skynet.forward_type(map, start_func) + c.callback(function(ptype, msg, sz, ...) + local prototype = map[ptype] + if prototype then + dispatch_message(prototype, msg, sz, ...) + else + dispatch_message(ptype, msg, sz, ...) + c.trash(msg, sz) + end + end, true) + skynet.timeout(0, function() + init_service(start_func) + end) +end + function skynet.endless() return c.command("ENDLESS")~=nil end diff --git a/service/cdummy.lua b/service/cdummy.lua index 107c626c..52b70d7c 100644 --- a/service/cdummy.lua +++ b/service/cdummy.lua @@ -36,7 +36,11 @@ function harbor.REGISTER(name, handle) skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) end -function harbor.QUERYNAME(fd, name) +function harbor.QUERYNAME(name) + if name:byte() == 46 then -- "." , local name + skynet.ret(skynet.pack(skynet.localname(name))) + return + end local result = globalname[name] if result then skynet.ret(skynet.pack(result)) diff --git a/service/clusterd.lua b/service/clusterd.lua index 3032784e..23fa5b0f 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -60,6 +60,16 @@ function command.req(source, node, addr, msg, sz) skynet.ret(c:request(request, session)) end +local proxy = {} + +function command.proxy(source, node, name) + local fullname = node .. "." .. name + if proxy[fullname] == nil then + proxy[fullname] = skynet.newservice("clusterproxy", node, name) + end + skynet.ret(skynet.pack(proxy[fullname])) +end + local request_fd = {} function command.socket(source, subcmd, fd, msg) diff --git a/service/cslave.lua b/service/cslave.lua index a75e9f36..fdbe9088 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -93,6 +93,7 @@ local function monitor_master(master_fd) local fd = slaves[id_name] slaves[id_name] = false if fd then + monitor_clear(id_name) socket.close(fd) end end @@ -143,14 +144,14 @@ local function monitor_harbor(master_fd) return function(session, source, command) local t = string.sub(command, 1, 1) local arg = string.sub(command, 3) - if t == "Q" then + if t == 'Q' then -- query name if globalname[arg] then skynet.redirect(harbor_service, globalname[arg], "harbor", 0, "N " .. arg) else socket.write(master_fd, pack_package("Q", arg)) end - elseif t == "D" then + elseif t == 'D' then -- harbor down local id = tonumber(arg) if slaves[id] then @@ -194,6 +195,10 @@ function harbor.CONNECT(fd, id) end function harbor.QUERYNAME(fd, name) + if name:byte() == 46 then -- "." , local name + skynet.ret(skynet.pack(skynet.localname(name))) + return + end local result = globalname[name] if result then skynet.ret(skynet.pack(result)) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index fa426ac9..eaf6e14d 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -232,7 +232,7 @@ _dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { size_t sz = msg->sz & HANDLE_MASK; if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) { skynet_free(msg->data); - } + } CHECKCALLING_END(ctx) } From 2cf69a6ef089a0473a3b47c15d308136f2a1c904 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 18:05:11 +0800 Subject: [PATCH 147/729] use localname .simpledb --- examples/cluster1.lua | 7 ++++--- examples/simpledb.lua | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 551b0f38..6bb3b68d 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -2,8 +2,9 @@ local skynet = require "skynet" local cluster = require "cluster" skynet.start(function() - skynet.newservice("simpledb") - print(skynet.call("SIMPLEDB", "lua", "SET", "a", "foobar")) - print(skynet.call("SIMPLEDB", "lua", "GET", "a")) + local sdb = skynet.newservice("simpledb") + skynet.name(".simpledb", sdb) + print(skynet.call(".simpledb", "lua", "SET", "a", "foobar")) + print(skynet.call(".simpledb", "lua", "GET", "a")) cluster.open "db" end) diff --git a/examples/simpledb.lua b/examples/simpledb.lua index e70d2f86..3a7649e6 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -22,6 +22,5 @@ skynet.start(function() error(string.format("Unknown command %s", tostring(cmd))) end end) - skynet.register ".simpledb" skynet.register "SIMPLEDB" end) From 845acd33147eaca1587c735bc5bd23d455209c02 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 19:10:51 +0800 Subject: [PATCH 148/729] cluster servicename is not stable, so don't cache address --- HISTORY.md | 2 +- examples/cluster2.lua | 1 - lualib/cluster.lua | 13 ------------- 3 files changed, 1 insertion(+), 15 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index cb29fd78..7d140bd0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -8,7 +8,7 @@ Dev version * snax service add change SERVICE_PATH and add it to package.path * skynet.redirect support string address * add skynet.harbor.queryname to query globalname -* add cluster.proxy and cluster.ncall +* add cluster.proxy v0.5.2 (2014-8-11) ----------- diff --git a/examples/cluster2.lua b/examples/cluster2.lua index d45605f8..a49aa718 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -4,6 +4,5 @@ local cluster = require "cluster" skynet.start(function() local proxy = cluster.proxy("db", ".simpledb") print(skynet.call(proxy, "lua", "GET", "a")) - print(cluster.ncall("db.simpledb","GET", "a")) print(cluster.call("db", ".simpledb", "GET", "a")) end) diff --git a/lualib/cluster.lua b/lualib/cluster.lua index ec3f6abf..ddb00d13 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -24,19 +24,6 @@ function cluster.proxy(node, name) return skynet.call(clusterd, "lua", "proxy", node, name) end -local namecache = {} - -function cluster.ncall(name, ...) - local s = namecache[name] - if not s then - local node , lname = name:match "(.-)(%..+)" - assert(node and lname) - s = cluster.proxy(node, lname) - namecache[name] = assert(s) - end - return skynet.call(s, "lua", ...) -end - skynet.init(function() clusterd = skynet.uniqueservice("clusterd") end) From 809e6c3a89d87e99c863a65b32cb285b764fe15a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 19:37:26 +0800 Subject: [PATCH 149/729] add debug command: exit --- HISTORY.md | 2 ++ lualib/skynet/debug.lua | 6 +++++- service/debug_console.lua | 1 + service/launcher.lua | 5 +++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 7d140bd0..8f1a5ac2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,8 +7,10 @@ Dev version * Don't check imported function in snax.hotfix * snax service add change SERVICE_PATH and add it to package.path * skynet.redirect support string address +* bugfix: skynet.harbor.link may block * add skynet.harbor.queryname to query globalname * add cluster.proxy +* add DEBUG command exit (send a message to lua service by DEBUG) v0.5.2 (2014-8-11) ----------- diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index c8f51f0b..7e7ffa78 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -39,6 +39,10 @@ function dbgcmd.INFO() end end +function dbgcmd.EXIT() + skynet.exit() +end + local function _debug_dispatch(session, address, cmd, ...) local f = dbgcmd[cmd] assert(f, cmd) @@ -53,4 +57,4 @@ skynet.register_protocol { dispatch = _debug_dispatch, } -end \ No newline at end of file +end diff --git a/service/debug_console.lua b/service/debug_console.lua index aa552ceb..22cb7274 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -106,6 +106,7 @@ function COMMAND.help() list = "List all the service", stat = "Dump all stats", info = "Info address : get service infomation", + exit = "exit address : kill a lua service", kill = "kill address : kill service", mem = "mem : show memory status", gc = "gc : force every lua service do garbage collect", diff --git a/service/launcher.lua b/service/launcher.lua index 9cb194b4..58ae5f11 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -56,6 +56,11 @@ function command.KILL(_, handle) return ret end +function command.EXIT(_, handle) + handle = handle_to_address(handle) + skynet.send(handle, "debug", "EXIT") +end + function command.MEM() local list = {} for k,v in pairs(services) do From 6bf8dd9a31fa3249915980007746a6d07a1c0bbd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 21:02:56 +0800 Subject: [PATCH 150/729] add debug command inject --- HISTORY.md | 1 + lualib/skynet.lua | 2 +- lualib/skynet/debug.lua | 62 ++++++++++++++++++++++++++++++++++++++- service/debug_console.lua | 37 +++++++++++++++++++++++ service/launcher.lua | 25 ---------------- 5 files changed, 100 insertions(+), 27 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 8f1a5ac2..af2d93c9 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -11,6 +11,7 @@ Dev version * add skynet.harbor.queryname to query globalname * add cluster.proxy * add DEBUG command exit (send a message to lua service by DEBUG) +* add DEBUG command run (debug_console command inject) v0.5.2 (2014-8-11) ----------- diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 563b0f26..6dd32a46 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -701,6 +701,6 @@ end -- Inject internal debug framework local debug = require "skynet.debug" -debug(skynet) +debug(skynet, dispatch_message) return skynet diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 7e7ffa78..0f68dd1b 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -1,4 +1,8 @@ -return function (skynet) +local io = io +local table = table +local debug = debug + +return function (skynet, dispatch_func) local internal_info_func @@ -43,6 +47,62 @@ function dbgcmd.EXIT() skynet.exit() end +local function getupvaluetable(u, func, unique) + i = 1 + while true do + local name, value = debug.getupvalue(func, i) + if name == nil then + return + end + local t = type(value) + if t == "table" then + u[name] = value + elseif t == "function" then + if not unique[value] then + unique[value] = true + getupvaluetable(u, value, unique) + end + end + i=i+1 + end +end + +function dbgcmd.RUN(source, filename) + if filename then + filename = "@" .. filename + else + filename = "=(load)" + end + local output = {} + do + local function print(...) + local value = { ... } + for k,v in ipairs(value) do + value[k] = tostring(v) + end + table.insert(output, table.concat(value, "\t")) + end + local u = {} + local unique = {} + getupvaluetable(u, dispatch_func, unique) + getupvaluetable(u, skynet.register_protocol, unique) + local env = setmetatable( { print = print , _U = u }, { __index = _ENV }) + local func, err = load(source, filename, "bt", env) + if not func then + skynet.ret(skynet.pack(err)) + return + end + local ok, err = pcall(func) + if not ok then + table.insert(output, err) + end + + source, filename = nil + end + collectgarbage "collect" + skynet.ret(skynet.pack(table.concat(output, "\n"))) +end + local function _debug_dispatch(session, address, cmd, ...) local f = dbgcmd[cmd] assert(f, cmd) diff --git a/service/debug_console.lua b/service/debug_console.lua index 22cb7274..b627eb7a 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -36,6 +36,7 @@ local function dump_list(print, list) for _,v in ipairs(index) do dump_line(print, v, list[v]) end + print("OK") end local function split_cmdline(cmdline) @@ -92,6 +93,9 @@ skynet.start(function() socket.start(listen_socket , function(id, addr) local function print(...) local t = { ... } + for k,v in ipairs(t) do + t[k] = tostring(v) + end socket.write(id, table.concat(t,"\t")) socket.write(id, "\n") end @@ -115,6 +119,7 @@ function COMMAND.help() clearcache = "clear lua code cache", service = "List unique service", task = "task address : show service task detail", + inject = "inject address luascript.lua", } end @@ -144,3 +149,35 @@ end function COMMAND.service() return skynet.call("SERVICE", "lua", "LIST") end + +local function adjust_address(address) + if address:sub(1,1) ~= ":" then + address = bit32.replace( tonumber("0x" .. address), skynet.harbor(skynet.self()), 24, 8) + end + return address +end + +function COMMAND.exit(address) + skynet.send(adjust_address(address), "debug", "EXIT") +end + +function COMMAND.inject(address, filename) + address = adjust_address(address) + local f = io.open(filename, "rb") + if not f then + return "Can't open " .. filename + end + local source = f:read "*a" + f:close() + return skynet.call(address, "debug", "RUN", source, filename) +end + +function COMMAND.task(address) + address = adjust_address(address) + return skynet.call(address,"debug","TASK") +end + +function COMMAND.info(address) + address = adjust_address(address) + return skynet.call(address,"debug","INFO") +end diff --git a/service/launcher.lua b/service/launcher.lua index 58ae5f11..e4197035 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -28,26 +28,6 @@ function command.STAT() return list end -function command.INFO(_, handle) - handle = handle_to_address(handle) - if services[handle] == nil then - return - else - local result = skynet.call(handle,"debug","INFO") - return result - end -end - -function command.TASK(_, handle) - handle = handle_to_address(handle) - if services[handle] == nil then - return - else - local result = skynet.call(handle,"debug","TASK") - return result - end -end - function command.KILL(_, handle) handle = handle_to_address(handle) skynet.kill(handle) @@ -56,11 +36,6 @@ function command.KILL(_, handle) return ret end -function command.EXIT(_, handle) - handle = handle_to_address(handle) - skynet.send(handle, "debug", "EXIT") -end - function command.MEM() local list = {} for k,v in pairs(services) do From 939ef564ceb1d97c8328842187bf47340702715e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 21:12:07 +0800 Subject: [PATCH 151/729] debug run extract proto's upvalues --- lualib/skynet/debug.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 0f68dd1b..6f30d4e5 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -86,7 +86,19 @@ function dbgcmd.RUN(source, filename) local unique = {} getupvaluetable(u, dispatch_func, unique) getupvaluetable(u, skynet.register_protocol, unique) - local env = setmetatable( { print = print , _U = u }, { __index = _ENV }) + local p = {} + local proto = u.proto + if proto then + for k,v in pairs(proto) do + local name, dispatch = v.name, v.dispatch + if name and dispatch then + local pp = {} + p[name] = pp + getupvaluetable(pp, dispatch, unique) + end + end + end + local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) local func, err = load(source, filename, "bt", env) if not func then skynet.ret(skynet.pack(err)) From 8078d777ab1424fab9eae5e1a42dc278d68dfc52 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Aug 2014 21:24:48 +0800 Subject: [PATCH 152/729] split skynet.debug to skynet.inject --- lualib/skynet/debug.lua | 65 ++-------------------------------------- lualib/skynet/inject.lua | 65 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 63 deletions(-) create mode 100644 lualib/skynet/inject.lua diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 6f30d4e5..5b7b5afa 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -47,70 +47,9 @@ function dbgcmd.EXIT() skynet.exit() end -local function getupvaluetable(u, func, unique) - i = 1 - while true do - local name, value = debug.getupvalue(func, i) - if name == nil then - return - end - local t = type(value) - if t == "table" then - u[name] = value - elseif t == "function" then - if not unique[value] then - unique[value] = true - getupvaluetable(u, value, unique) - end - end - i=i+1 - end -end - function dbgcmd.RUN(source, filename) - if filename then - filename = "@" .. filename - else - filename = "=(load)" - end - local output = {} - do - local function print(...) - local value = { ... } - for k,v in ipairs(value) do - value[k] = tostring(v) - end - table.insert(output, table.concat(value, "\t")) - end - local u = {} - local unique = {} - getupvaluetable(u, dispatch_func, unique) - getupvaluetable(u, skynet.register_protocol, unique) - local p = {} - local proto = u.proto - if proto then - for k,v in pairs(proto) do - local name, dispatch = v.name, v.dispatch - if name and dispatch then - local pp = {} - p[name] = pp - getupvaluetable(pp, dispatch, unique) - end - end - end - local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) - local func, err = load(source, filename, "bt", env) - if not func then - skynet.ret(skynet.pack(err)) - return - end - local ok, err = pcall(func) - if not ok then - table.insert(output, err) - end - - source, filename = nil - end + local inject = require "skynet.inject" + local output = inject(source, filename , dispatch_func, skynet.register_protocol) collectgarbage "collect" skynet.ret(skynet.pack(table.concat(output, "\n"))) end diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua new file mode 100644 index 00000000..68e3954b --- /dev/null +++ b/lualib/skynet/inject.lua @@ -0,0 +1,65 @@ +local function getupvaluetable(u, func, unique) + i = 1 + while true do + local name, value = debug.getupvalue(func, i) + if name == nil then + return + end + local t = type(value) + if t == "table" then + u[name] = value + elseif t == "function" then + if not unique[value] then + unique[value] = true + getupvaluetable(u, value, unique) + end + end + i=i+1 + end +end + +return function(source, filename , ...) + if filename then + filename = "@" .. filename + else + filename = "=(load)" + end + local output = {} + + local function print(...) + local value = { ... } + for k,v in ipairs(value) do + value[k] = tostring(v) + end + table.insert(output, table.concat(value, "\t")) + end + local u = {} + local unique = {} + local funcs = { ... } + for k, func in ipairs(funcs) do + getupvaluetable(u, func, unique) + end + local p = {} + local proto = u.proto + if proto then + for k,v in pairs(proto) do + local name, dispatch = v.name, v.dispatch + if name and dispatch then + local pp = {} + p[name] = pp + getupvaluetable(pp, dispatch, unique) + end + end + end + local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) + local func, err = load(source, filename, "bt", env) + if not func then + return { err } + end + local ok, err = xpcall(func, debug.traceback) + if not ok then + table.insert(output, err) + end + + return output +end From 7e24d460470ca1daae31f377411c1f539fa5a3a9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 14 Aug 2014 11:11:01 +0800 Subject: [PATCH 153/729] clusterproxy --- service/clusterproxy.lua | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 service/clusterproxy.lua diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua new file mode 100644 index 00000000..6edcb674 --- /dev/null +++ b/service/clusterproxy.lua @@ -0,0 +1,27 @@ +local skynet = require "skynet" +local cluster = require "cluster" + +local node, address = ... + +skynet.register_protocol { + name = "system", + id = skynet.PTYPE_SYSTEM, + unpack = function (...) return ... end, +} + +local forward_map = { + [skynet.PTYPE_LUA] = skynet.PTYPE_SYSTEM, + [skynet.PTYPE_RESPONSE] = skynet.PTYPE_RESPONSE, -- don't free response message +} + +skynet.forward_type( forward_map ,function() + local clusterd = skynet.uniqueservice("clusterd") + local n = tonumber(address) + if n then + address = n + end + skynet.dispatch("system", function (session, source, msg, sz) + local m,s = skynet.rawcall(clusterd, "lua", skynet.pack("req", node, address, msg, sz)) + skynet.ret(skynet.rawcall(clusterd, "lua", skynet.pack("req", node, address, msg, sz))) + end) +end) From d2cea9b70f983a38c75e245ceb2bef15a6f5ebfe Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 14 Aug 2014 13:49:49 +0800 Subject: [PATCH 154/729] socketchannel request only try connect once --- lualib/socketchannel.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 45aeea54..89f1f1ec 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -296,7 +296,7 @@ local function wait_for_response(self, response) end function channel:request(request, response) - assert(block_connect(self)) + assert(block_connect(self, true)) -- connect once if not socket.write(self.__sock[1], request) then close_channel_socket(self) From bd52c5fece258e2ee444e7c99101fd0e9e1068e2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 14 Aug 2014 17:03:09 +0800 Subject: [PATCH 155/729] bugfix: socketchannel connect once --- HISTORY.md | 1 + lualib/socketchannel.lua | 40 +++++++++++++++++++++++++--------------- service/clusterd.lua | 15 +++++++++++++-- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index af2d93c9..60609957 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -12,6 +12,7 @@ Dev version * add cluster.proxy * add DEBUG command exit (send a message to lua service by DEBUG) * add DEBUG command run (debug_console command inject) +* bugfix : socketchannel connect once v0.5.2 (2014-8-11) ----------- diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 89f1f1ec..1086cd79 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -218,9 +218,9 @@ local function try_connect(self , once) if not once then skynet.error("socket: connect to", self.__host, self.__port) end - return + return true elseif once then - error(string.format("Connect to %s:%d failed", self.__host, self.__port)) + return false end if t > 1000 then skynet.error("socket: try to reconnect", self.__host, self.__port) @@ -233,7 +233,7 @@ local function try_connect(self , once) end end -local function block_connect(self, once) +local function check_connection(self) if self.__sock then local authco = self.__authcoroutine if not authco then @@ -247,26 +247,36 @@ local function block_connect(self, once) if self.__closed then return false end +end + +local function block_connect(self, once) + local r = check_connection(self) + if r ~= nil then + return r + end if #self.__connecting > 0 then -- connecting in other coroutine local co = coroutine.running() table.insert(self.__connecting, co) skynet.wait() - -- check connection again - return block_connect(self, once) - end - self.__connecting[1] = true - try_connect(self, once) - self.__connecting[1] = nil - for i=2, #self.__connecting do - local co = self.__connecting[i] - self.__connecting[i] = nil - skynet.wakeup(co) + else + self.__connecting[1] = true + try_connect(self, once) + self.__connecting[1] = nil + for i=2, #self.__connecting do + local co = self.__connecting[i] + self.__connecting[i] = nil + skynet.wakeup(co) + end end - -- check again - return block_connect(self, once) + r = check_connection(self) + if r == nil then + error(string.format("Connect to %s:%d failed", self.__host, self.__port)) + else + return r + end end function channel:connect(once) diff --git a/service/clusterd.lua b/service/clusterd.lua index 23fa5b0f..677f84fa 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -51,13 +51,24 @@ function command.listen(source, addr, port) skynet.ret(skynet.pack(nil)) end -function command.req(source, node, addr, msg, sz) +local function send_request(source, node, addr, msg, sz) local request local c = node_channel[node] local session = node_session[node] -- msg is a local pointer, cluster.packrequest will free it request, node_session[node] = cluster.packrequest(addr, session , msg, sz) - skynet.ret(c:request(request, session)) + + return c:request(request, session) +end + +function command.req(...) + local ok, msg, sz = pcall(send_request, ...) + if ok then + skynet.ret(msg, sz) + else + skynet.error(msg) + skynet.response()(false) + end end local proxy = {} From b8a0e35f21f80598e798d8038fe86ec619f79a1d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 16 Aug 2014 15:19:01 +0800 Subject: [PATCH 156/729] bugfix: mongo driver --- HISTORY.md | 1 + lualib-src/lua-mongo.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 60609957..aca45755 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -13,6 +13,7 @@ Dev version * add DEBUG command exit (send a message to lua service by DEBUG) * add DEBUG command run (debug_console command inject) * bugfix : socketchannel connect once +* bugfix : mongo driver v0.5.2 (2014-8-11) ----------- diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index 855cc385..af63d0b0 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -507,8 +507,8 @@ op_insert(lua_State *L) { for (i=1;i<=s;i++) { lua_rawgeti(L,3,i); document doc = lua_touserdata(L,-1); + lua_pop(L,1); // must call lua_pop before luaL_addlstring, because addlstring may change stack top luaL_addlstring(&b, (const char *)doc, get_length(doc)); - lua_pop(L,1); } } From 0390818a02ff847b9ba4312b35fdb6a5e190a569 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Aug 2014 11:31:05 +0800 Subject: [PATCH 157/729] release v0.6.0 --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index aca45755..e6a1917a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -Dev version +v0.6.0 (2014-8-18) ----------- * add sharedata * bugfix: service exit before init would not report back From 0fb23f9ab8e6d57f5e8effc3a2ceb37ff99bef96 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Aug 2014 16:23:49 +0800 Subject: [PATCH 158/729] remove default config name --- skynet-src/skynet_main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index fce5debb..56d571f1 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -97,9 +97,13 @@ static const char * load_config = "\ int main(int argc, char *argv[]) { - const char * config_file = "config"; + const char * config_file = NULL ; if (argc > 1) { config_file = argv[1]; + } else { + fprintf(stderr, "Need a config file. Please read skynet wiki : https://github.com/cloudwu/skynet/wiki/Config\n" + "usage: skynet configfilename\n"); + return 1; } skynet_globalinit(); skynet_env_init(); From f9438461eb6e664569a00031a2855a30dbae7f6c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Aug 2014 16:44:19 +0800 Subject: [PATCH 159/729] check dest type --- lualib-src/lua-skynet.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 644c1b2e..d7096b2a 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -126,6 +126,15 @@ _genid(lua_State *L) { return 1; } +static const char * +get_dest_string(lua_State *L, int index) { + const char * dest_string = lua_tostring(L, index); + if (dest_string == NULL) { + luaL_error(L, "dest address type (%s) must be a string or number.", lua_typename(L, lua_type(L,index))); + } + return dest_string; +} + /* unsigned address string address @@ -141,7 +150,7 @@ _send(lua_State *L) { uint32_t dest = lua_tounsigned(L, 1); const char * dest_string = NULL; if (dest == 0) { - dest_string = lua_tostring(L, 1); + dest_string = get_dest_string(L, 1); } int type = luaL_checkinteger(L, 2); @@ -195,7 +204,7 @@ _redirect(lua_State *L) { uint32_t dest = lua_tounsigned(L,1); const char * dest_string = NULL; if (dest == 0) { - dest_string = lua_tostring(L,1); + dest_string = get_dest_string(L, 1); } uint32_t source = luaL_checkunsigned(L,2); int type = luaL_checkinteger(L,3); From 9ea756ce4fe8f77b7ee7034915363574e676e677 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Aug 2014 16:49:14 +0800 Subject: [PATCH 160/729] fix typo --- service/gate.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/gate.lua b/service/gate.lua index 5fb22801..9fe3a491 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -31,7 +31,7 @@ end function handler.connect(fd, addr) local c = { fd = fd, - ip = msg, + ip = addr, } connection[fd] = c skynet.send(watchdog, "lua", "socket", "open", fd, addr) From f393b6412476cac803ba88b2208aa4da562e7b7c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Aug 2014 17:40:18 +0800 Subject: [PATCH 161/729] remove watch from service_mgr --- service/service_mgr.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/service/service_mgr.lua b/service/service_mgr.lua index db9499d4..4d9e4119 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -117,7 +117,6 @@ local function register_global() function cmd.REPORT(m) mgr[m] = true - skynet.watch(m) end local function add_list(all, m) From 4803e509774cf3e92eb8116ddc1e5b020d1e1d6e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 19 Aug 2014 20:52:43 +0800 Subject: [PATCH 162/729] fix error log --- lualib-src/lua-crypt.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 5d2f12f9..01f9ac0a 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -589,11 +589,11 @@ read64(lua_State *L, uint32_t xx[2], uint32_t yy[2]) { size_t sz = 0; const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); if (sz != 8) { - luaL_error(L, "Invalid hmac x"); + luaL_error(L, "Invalid uint64 x"); } const uint8_t *y = (const uint8_t *)luaL_checklstring(L, 2, &sz); if (sz != 8) { - luaL_error(L, "Invalid hmac y"); + luaL_error(L, "Invalid uint64 y"); } xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; @@ -703,7 +703,7 @@ ldhexchange(lua_State *L) { size_t sz = 0; const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); if (sz != 8) { - luaL_error(L, "Invalid hmac x"); + luaL_error(L, "Invalid dh uint64 key"); } uint32_t xx[2]; xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; From 55ea55606074e6f08c6499c5fa14e9e05e115d92 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 19 Aug 2014 23:26:42 +0800 Subject: [PATCH 163/729] avoid issue #154 --- service-src/service_harbor.c | 46 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 78588867..3fd834b4 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -36,17 +36,17 @@ struct remote_message_header { uint32_t session; }; -struct msg { +struct harbor_msg { struct remote_message_header header; void * buffer; size_t size; }; -struct msg_queue { +struct harbor_msg_queue { int size; int head; int tail; - struct msg * data; + struct harbor_msg * data; }; struct keyvalue { @@ -54,7 +54,7 @@ struct keyvalue { char key[GLOBALNAME_LENGTH]; uint32_t hash; uint32_t value; - struct msg_queue * queue; + struct harbor_msg_queue * queue; }; struct hashmap { @@ -69,7 +69,7 @@ struct hashmap { struct slave { int fd; - struct msg_queue *queue; + struct harbor_msg_queue *queue; int status; int length; int read; @@ -88,11 +88,11 @@ struct harbor { // hash table static void -push_queue_msg(struct msg_queue * queue, struct msg * m) { +push_queue_msg(struct harbor_msg_queue * queue, struct harbor_msg * m) { // If there is only 1 free slot which is reserved to distinguish full/empty // of circular buffer, expand it. if (((queue->tail + 1) % queue->size) == queue->head) { - struct msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct msg)); + struct harbor_msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct harbor_msg)); int i; for (i=0;isize-1;i++) { new_buffer[i] = queue->data[(i+queue->head) % queue->size]; @@ -103,46 +103,46 @@ push_queue_msg(struct msg_queue * queue, struct msg * m) { queue->tail = queue->size - 1; queue->size *= 2; } - struct msg * slot = &queue->data[queue->tail]; + struct harbor_msg * slot = &queue->data[queue->tail]; *slot = *m; queue->tail = (queue->tail + 1) % queue->size; } static void -push_queue(struct msg_queue * queue, void * buffer, size_t sz, struct remote_message_header * header) { - struct msg m; +push_queue(struct harbor_msg_queue * queue, void * buffer, size_t sz, struct remote_message_header * header) { + struct harbor_msg m; m.header = *header; m.buffer = buffer; m.size = sz; push_queue_msg(queue, &m); } -static struct msg * -pop_queue(struct msg_queue * queue) { +static struct harbor_msg * +pop_queue(struct harbor_msg_queue * queue) { if (queue->head == queue->tail) { return NULL; } - struct msg * slot = &queue->data[queue->head]; + struct harbor_msg * slot = &queue->data[queue->head]; queue->head = (queue->head + 1) % queue->size; return slot; } -static struct msg_queue * +static struct harbor_msg_queue * new_queue() { - struct msg_queue * queue = skynet_malloc(sizeof(*queue)); + struct harbor_msg_queue * queue = skynet_malloc(sizeof(*queue)); queue->size = DEFAULT_QUEUE_SIZE; queue->head = 0; queue->tail = 0; - queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct msg)); + queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct harbor_msg)); return queue; } static void -release_queue(struct msg_queue *queue) { +release_queue(struct harbor_msg_queue *queue) { if (queue == NULL) return; - struct msg * m; + struct harbor_msg * m; while ((m=pop_queue(queue)) != NULL) { skynet_free(m->buffer); } @@ -334,7 +334,7 @@ send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, static void dispatch_name_queue(struct harbor *h, struct keyvalue * node) { - struct msg_queue * queue = node->queue; + struct harbor_msg_queue * queue = node->queue; uint32_t handle = node->value; int harbor_id = handle >> HANDLE_REMOTE_SHIFT; assert(harbor_id != 0); @@ -352,7 +352,7 @@ dispatch_name_queue(struct harbor *h, struct keyvalue * node) { s->queue = node->queue; node->queue = NULL; } else { - struct msg * m; + struct harbor_msg * m; while ((m = pop_queue(queue))!=NULL) { push_queue_msg(s->queue, m); } @@ -360,7 +360,7 @@ dispatch_name_queue(struct harbor *h, struct keyvalue * node) { } return; } - struct msg * m; + struct harbor_msg * m; while ((m = pop_queue(queue)) != NULL) { m->header.destination |= (handle & HANDLE_MASK); send_remote(context, fd, m->buffer, m->size, &m->header); @@ -373,11 +373,11 @@ dispatch_queue(struct harbor *h, int id) { int fd = s->fd; assert(fd != 0); - struct msg_queue *queue = s->queue; + struct harbor_msg_queue *queue = s->queue; if (queue == NULL) return; - struct msg * m; + struct harbor_msg * m; while ((m = pop_queue(queue)) != NULL) { send_remote(h->ctx, fd, m->buffer, m->size, &m->header); } From e0feea99953ae0f30fedac5912d1761af479a65d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 19 Aug 2014 23:44:07 +0800 Subject: [PATCH 164/729] fix issue #156 --- service/datacenterd.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/service/datacenterd.lua b/service/datacenterd.lua index 5929b0e3..abc1eea6 100644 --- a/service/datacenterd.lua +++ b/service/datacenterd.lua @@ -33,7 +33,7 @@ local function update(db, key, value, ...) end end -local function wakeup(db, key1, key2, value, ...) +local function wakeup(db, key1, key2, ...) if key1 == nil then return end @@ -43,7 +43,7 @@ local function wakeup(db, key1, key2, value, ...) end if q[mode] == "queue" then db[key1] = nil - if value then + if key2 then -- throw error because can't wake up a branch for _,response in ipairs(q) do response(false) @@ -53,7 +53,7 @@ local function wakeup(db, key1, key2, value, ...) end else -- it's branch - return wakeup(q , key2, value, ...) + return wakeup(q , key2, ...) end end From 04688e98a7988c31a9d4a17efb2977c2c7698e20 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 20 Aug 2014 23:34:23 +0800 Subject: [PATCH 165/729] improve seri lib --- lualib-src/lua-seri.c | 236 +++++++++++++----------------------------- 1 file changed, 71 insertions(+), 165 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index c6544462..1acfb974 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -1,5 +1,5 @@ /* - https://github.com/cloudwu/lua-serialize + modify from https://github.com/cloudwu/lua-serialize */ #include "skynet_malloc.h" @@ -35,14 +35,13 @@ struct block { struct write_block { struct block * head; - int len; struct block * current; + int len; int ptr; }; struct read_block { char * buffer; - struct block * current; int len; int ptr; }; @@ -78,38 +77,17 @@ _again: static void wb_init(struct write_block *wb , struct block *b) { - if (b==NULL) { - wb->head = blk_alloc(); - wb->len = 0; - wb->current = wb->head; - wb->ptr = 0; - wb_push(wb, &wb->len, sizeof(wb->len)); - } else { - wb->head = b; - int * plen = (int *)b->buffer; - int sz = *plen; - wb->len = sz; - while (b->next) { - sz -= BLOCK_SIZE; - b = b->next; - } - wb->current = b; - wb->ptr = sz; - } -} - -static struct block * -wb_close(struct write_block *b) { - b->current = b->head; - b->ptr = 0; - wb_push(b, &b->len, sizeof(b->len)); - b->current = NULL; - return b->head; + wb->head = b; + assert(b->next == NULL); + wb->len = 0; + wb->current = wb->head; + wb->ptr = 0; } static void wb_free(struct write_block *wb) { struct block *blk = wb->head; + blk = blk->next; // the first block is on stack while (blk) { struct block * next = blk->next; skynet_free(blk); @@ -124,7 +102,6 @@ wb_free(struct write_block *wb) { static void rball_init(struct read_block * rb, char * buffer, int size) { rb->buffer = buffer; - rb->current = NULL; rb->len = size; rb->ptr = 0; } @@ -135,63 +112,10 @@ rb_read(struct read_block *rb, void *buffer, int sz) { return NULL; } - if (rb->buffer) { - int ptr = rb->ptr; - rb->ptr += sz; - rb->len -= sz; - return rb->buffer + ptr; - } - - if (rb->ptr == BLOCK_SIZE) { - struct block * next = rb->current->next; - skynet_free(rb->current); - rb->current = next; - rb->ptr = 0; - } - - int copy = BLOCK_SIZE - rb->ptr; - - if (sz <= copy) { - void * ret = rb->current->buffer + rb->ptr; - rb->ptr += sz; - rb->len -= sz; - return ret; - } - - char * tmp = buffer; - - memcpy(tmp, rb->current->buffer + rb->ptr, copy); - sz -= copy; - tmp += copy; - rb->len -= copy; - - for (;;) { - struct block * next = rb->current->next; - skynet_free(rb->current); - rb->current = next; - - if (sz < BLOCK_SIZE) { - memcpy(tmp, rb->current->buffer, sz); - rb->ptr = sz; - rb->len -= sz; - return buffer; - } - memcpy(tmp, rb->current->buffer, BLOCK_SIZE); - sz -= BLOCK_SIZE; - tmp += BLOCK_SIZE; - rb->len -= BLOCK_SIZE; - } -} - -static void -rb_close(struct read_block *rb) { - while (rb->current) { - struct block * next = rb->current->next; - skynet_free(rb->current); - rb->current = next; - } - rb->len = 0; - rb->ptr = 0; + int ptr = rb->ptr; + rb->ptr += sz; + rb->len -= sz; + return rb->buffer + ptr; } static inline void @@ -271,7 +195,7 @@ wb_string(struct write_block *wb, const char *str, int len) { } } -static void _pack_one(lua_State *L, struct write_block *b, int index, int depth); +static void pack_one(lua_State *L, struct write_block *b, int index, int depth); static int wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) { @@ -288,7 +212,7 @@ wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) { int i; for (i=1;i<=array_size;i++) { lua_rawgeti(L,index,i); - _pack_one(L, wb, -1, depth); + pack_one(L, wb, -1, depth); lua_pop(L,1); } @@ -307,8 +231,8 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a continue; } } - _pack_one(L,wb,-2,depth); - _pack_one(L,wb,-1,depth); + pack_one(L,wb,-2,depth); + pack_one(L,wb,-1,depth); lua_pop(L, 1); } wb_nil(wb); @@ -324,7 +248,7 @@ wb_table(lua_State *L, struct write_block *wb, int index, int depth) { } static void -_pack_one(lua_State *L, struct write_block *b, int index, int depth) { +pack_one(lua_State *L, struct write_block *b, int index, int depth) { if (depth > MAX_DEPTH) { wb_free(b); luaL_error(L, "serialize can't pack too depth table"); @@ -370,27 +294,24 @@ _pack_one(lua_State *L, struct write_block *b, int index, int depth) { } static void -_pack_from(lua_State *L, struct write_block *b, int from) { +pack_from(lua_State *L, struct write_block *b, int from) { int n = lua_gettop(L) - from; int i; for (i=1;i<=n;i++) { - _pack_one(L, b , from + i, 0); + pack_one(L, b , from + i, 0); } } static inline void -__invalid_stream(lua_State *L, struct read_block *rb, int line) { +invalid_stream_line(lua_State *L, struct read_block *rb, int line) { int len = rb->len; - if (rb->buffer == NULL) { - rb_close(rb); - } luaL_error(L, "Invalid serialize stream %d (line:%d)", len, line); } -#define _invalid_stream(L,rb) __invalid_stream(L,rb,__LINE__) +#define invalid_stream(L,rb) invalid_stream_line(L,rb,__LINE__) static int -_get_integer(lua_State *L, struct read_block *rb, int cookie) { +get_integer(lua_State *L, struct read_block *rb, int cookie) { switch (cookie) { case 0: return 0; @@ -398,93 +319,93 @@ _get_integer(lua_State *L, struct read_block *rb, int cookie) { uint8_t n = 0; uint8_t * pn = rb_read(rb,&n,1); if (pn == NULL) - _invalid_stream(L,rb); + invalid_stream(L,rb); return *pn; } case 2: { uint16_t n = 0; uint16_t * pn = rb_read(rb,&n,2); if (pn == NULL) - _invalid_stream(L,rb); + invalid_stream(L,rb); return *pn; } case 4: { int n = 0; int * pn = rb_read(rb,&n,4); if (pn == NULL) - _invalid_stream(L,rb); + invalid_stream(L,rb); return *pn; } default: - _invalid_stream(L,rb); + invalid_stream(L,rb); return 0; } } static double -_get_number(lua_State *L, struct read_block *rb, int cookie) { +get_number(lua_State *L, struct read_block *rb, int cookie) { if (cookie == 8) { double n = 0; double * pn = rb_read(rb,&n,8); if (pn == NULL) - _invalid_stream(L,rb); + invalid_stream(L,rb); return *pn; } else { - return (double)_get_integer(L,rb,cookie); + return (double)get_integer(L,rb,cookie); } } static void * -_get_pointer(lua_State *L, struct read_block *rb) { +get_pointer(lua_State *L, struct read_block *rb) { void * userdata = 0; void ** v = (void **)rb_read(rb,&userdata,sizeof(userdata)); if (v == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } return *v; } static void -_get_buffer(lua_State *L, struct read_block *rb, int len) { +get_buffer(lua_State *L, struct read_block *rb, int len) { char tmp[len]; char * p = rb_read(rb,tmp,len); if (p == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } lua_pushlstring(L,p,len); } -static void _unpack_one(lua_State *L, struct read_block *rb); +static void unpack_one(lua_State *L, struct read_block *rb); static void -_unpack_table(lua_State *L, struct read_block *rb, int array_size) { +unpack_table(lua_State *L, struct read_block *rb, int array_size) { if (array_size == MAX_COOKIE-1) { uint8_t type = 0; uint8_t *t = rb_read(rb, &type, 1); if (t==NULL || (*t & 7) != TYPE_NUMBER) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } - array_size = (int)_get_number(L,rb,*t >> 3); + array_size = (int)get_number(L,rb,*t >> 3); } lua_createtable(L,array_size,0); int i; for (i=1;i<=array_size;i++) { - _unpack_one(L,rb); + unpack_one(L,rb); lua_rawseti(L,-2,i); } for (;;) { - _unpack_one(L,rb); + unpack_one(L,rb); if (lua_isnil(L,-1)) { lua_pop(L,1); return; } - _unpack_one(L,rb); + unpack_one(L,rb); lua_rawset(L,-3); } } static void -_push_value(lua_State *L, struct read_block *rb, int type, int cookie) { +push_value(lua_State *L, struct read_block *rb, int type, int cookie) { switch(type) { case TYPE_NIL: lua_pushnil(L); @@ -493,82 +414,69 @@ _push_value(lua_State *L, struct read_block *rb, int type, int cookie) { lua_pushboolean(L,cookie); break; case TYPE_NUMBER: - lua_pushnumber(L,_get_number(L,rb,cookie)); + lua_pushnumber(L,get_number(L,rb,cookie)); break; case TYPE_USERDATA: - lua_pushlightuserdata(L,_get_pointer(L,rb)); + lua_pushlightuserdata(L,get_pointer(L,rb)); break; case TYPE_SHORT_STRING: - _get_buffer(L,rb,cookie); + get_buffer(L,rb,cookie); break; case TYPE_LONG_STRING: { uint32_t len; if (cookie == 2) { uint16_t *plen = rb_read(rb, &len, 2); if (plen == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } - _get_buffer(L,rb,(int)*plen); + get_buffer(L,rb,(int)*plen); } else { if (cookie != 4) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } uint32_t *plen = rb_read(rb, &len, 4); if (plen == NULL) { - _invalid_stream(L,rb); + invalid_stream(L,rb); } - _get_buffer(L,rb,(int)*plen); + get_buffer(L,rb,(int)*plen); } break; } case TYPE_TABLE: { - _unpack_table(L,rb,cookie); + unpack_table(L,rb,cookie); break; } default: { - _invalid_stream(L,rb); + invalid_stream(L,rb); break; } } } static void -_unpack_one(lua_State *L, struct read_block *rb) { +unpack_one(lua_State *L, struct read_block *rb) { uint8_t type = 0; uint8_t *t = rb_read(rb, &type, 1); if (t==NULL) { - _invalid_stream(L, rb); + invalid_stream(L, rb); } - _push_value(L, rb, *t & 0x7, *t>>3); + push_value(L, rb, *t & 0x7, *t>>3); } static void -_seri(lua_State *L, struct block *b) { - uint32_t len = 0; - memcpy(&len, b->buffer ,sizeof(len)); - - len -= 4; +seri(lua_State *L, struct block *b, int len) { uint8_t * buffer = skynet_malloc(len); uint8_t * ptr = buffer; int sz = len; - if (len < BLOCK_SIZE - 4) { - memcpy(ptr, b->buffer+4, len); - } else { - memcpy(ptr, b->buffer+4, BLOCK_SIZE-4); - ptr += BLOCK_SIZE-4; - len -= BLOCK_SIZE-4; - b = b->next; - - while(len>0) { - if (len >= BLOCK_SIZE) { - memcpy(ptr, b->buffer, BLOCK_SIZE); - ptr += BLOCK_SIZE; - len -= BLOCK_SIZE; - } else { - memcpy(ptr, b->buffer, len); - break; - } + while(len>0) { + if (len >= BLOCK_SIZE) { + memcpy(ptr, b->buffer, BLOCK_SIZE); + ptr += BLOCK_SIZE; + len -= BLOCK_SIZE; b = b->next; + } else { + memcpy(ptr, b->buffer, len); + break; } } @@ -611,7 +519,7 @@ _luaseri_unpack(lua_State *L) { uint8_t *t = rb_read(&rb, &type, 1); if (t==NULL) break; - _push_value(L, &rb, *t & 0x7, *t>>3); + push_value(L, &rb, *t & 0x7, *t>>3); } // Need not free buffer @@ -621,17 +529,15 @@ _luaseri_unpack(lua_State *L) { int _luaseri_pack(lua_State *L) { + struct block temp; + temp.next = NULL; struct write_block wb; - wb_init(&wb, NULL); - _pack_from(L,&wb,0); - struct block * b = wb_close(&wb); - _seri(L,b); + wb_init(&wb, &temp); + pack_from(L,&wb,0); + assert(wb.head == &temp); + seri(L, &temp, wb.len); - while (b) { - struct block * next = b->next; - skynet_free(b); - b = next; - } + wb_free(&wb); return 2; } From 16f63df0c3fba1cf4f309720587683b247362e44 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 22 Aug 2014 15:30:35 +0800 Subject: [PATCH 166/729] add message log --- Makefile | 2 +- examples/config | 1 + lualib/skynet.lua | 7 +-- service/debug_console.lua | 23 ++++++++++ service/launcher.lua | 34 +++++++++----- skynet-src/skynet_log.c | 77 ++++++++++++++++++++++++++++++++ skynet-src/skynet_log.h | 14 ++++++ skynet-src/skynet_server.c | 90 ++++++++++++++++++++++++++++++-------- 8 files changed, 210 insertions(+), 38 deletions(-) create mode 100644 skynet-src/skynet_log.c create mode 100644 skynet-src/skynet_log.h diff --git a/Makefile b/Makefile index ed8d1c7d..2b06c01d 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \ - malloc_hook.c skynet_daemon.c + malloc_hook.c skynet_daemon.c skynet_log.c all : \ $(SKYNET_BUILD_PATH)/skynet \ diff --git a/examples/config b/examples/config index 087a9a14..393e83f7 100644 --- a/examples/config +++ b/examples/config @@ -1,6 +1,7 @@ root = "./" thread = 8 logger = nil +logpath = "." harbor = 1 address = "127.0.0.1:2526" master = "127.0.0.1:2013" diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 6dd32a46..683de17e 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -523,12 +523,7 @@ local function dispatch_message(...) end function skynet.newservice(name, ...) - local handle = skynet.tostring(skynet.rawcall(".launcher", "lua" , skynet.pack("LAUNCH", "snlua", name, ...))) - if handle == "" then - return nil - else - return string_to_handle(handle) - end + return skynet.call(".launcher", "lua" , "LAUNCH", "snlua", name, ...) end function skynet.uniqueservice(global, ...) diff --git a/service/debug_console.lua b/service/debug_console.lua index b627eb7a..050fa2de 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local codecache = require "skynet.codecache" +local core = require "skynet.core" local socket = require "socket" local snax = require "snax" @@ -120,6 +121,9 @@ function COMMAND.help() service = "List unique service", task = "task address : show service task detail", inject = "inject address luascript.lua", + logon = "logon address", + logoff = "logoff address", + log = "launch a new lua service with log", } end @@ -136,6 +140,15 @@ function COMMAND.start(...) end end +function COMMAND.log(...) + local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...) + if ok then + return { [skynet.address(addr)] = ... } + else + return "Failed" + end +end + function COMMAND.snax(...) local ok, s = pcall(snax.newservice, ...) if ok then @@ -181,3 +194,13 @@ function COMMAND.info(address) address = adjust_address(address) return skynet.call(address,"debug","INFO") end + +function COMMAND.logon(address) + address = adjust_address(address) + core.command("LOGON", skynet.address(address)) +end + +function COMMAND.logoff(address) + address = adjust_address(address) + core.command("LOGOFF", skynet.address(address)) +end diff --git a/service/launcher.lua b/service/launcher.lua index e4197035..bfe96fbd 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +local core = require "skynet.core" local string = string local services = {} @@ -65,18 +66,29 @@ function command.REMOVE(_, handle) return NORET end -local function return_string(str) - return str +local function launch_service(service, ...) + local param = table.concat({...}, " ") + local inst = skynet.launch(service, param) + local response = skynet.response() + if inst then + services[inst] = service .. " " .. param + instance[inst] = response + else + response(false) + return + end + return inst end function command.LAUNCH(_, service, ...) - local param = table.concat({...}, " ") - local inst = skynet.launch(service, param) + launch_service(service, ...) + return NORET +end + +function command.LOGLAUNCH(_, service, ...) + local inst = launch_service(service, ...) if inst then - services[inst] = service .. " " .. param - instance[inst] = skynet.response(return_string) - else - skynet.ret("") -- launch failed + core.command("LOGON", skynet.address(inst)) end return NORET end @@ -97,7 +109,7 @@ function command.LAUNCHOK(address) -- init notice local response = instance[address] if response then - response(true, skynet.address(address)) + response(true, address) instance[address] = nil end @@ -116,9 +128,7 @@ skynet.register_protocol { elseif cmd == "ERROR" then command.ERROR(address) else - -- launch request - local service, param = string.match(cmd,"([^ ]+) (.*)") - command.LAUNCH(_, service, param) + error ("Invalid text command " .. cmd) end end, } diff --git a/skynet-src/skynet_log.c b/skynet-src/skynet_log.c new file mode 100644 index 00000000..65b97c30 --- /dev/null +++ b/skynet-src/skynet_log.c @@ -0,0 +1,77 @@ +#include "skynet_log.h" +#include "skynet_timer.h" +#include "skynet.h" +#include "skynet_socket.h" +#include +#include + +FILE * +skynet_log_open(struct skynet_context * ctx, uint32_t handle) { + const char * logpath = skynet_getenv("logpath"); + if (logpath == NULL) + return NULL; + size_t sz = strlen(logpath); + char tmp[sz + 16]; + sprintf(tmp, "%s/%08x.log", logpath, handle); + FILE *f = fopen(tmp, "ab"); + if (f) { + uint32_t starttime = skynet_gettime_fixsec(); + uint32_t currenttime = skynet_gettime(); + time_t ti = starttime + currenttime/100; + skynet_error(ctx, "Open log file %s", tmp); + fprintf(f, "open time: %u %s", currenttime, ctime(&ti)); + fflush(f); + } else { + skynet_error(ctx, "Open log file %s fail", tmp); + } + return f; +} + +void +skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) { + skynet_error(ctx, "Close log file :%08x", handle); + fprintf(f, "close time: %u\n", skynet_gettime()); + fclose(f); +} + +static void +log_blob(FILE *f, void * buffer, size_t sz) { + size_t i; + uint8_t * buf = buffer; + for (i=0;i!=sz;i++) { + fprintf(f, "%02x", buf[i]); + } +} + +static void +log_socket(FILE * f, struct skynet_socket_message * message, size_t sz) { + fprintf(f, "[socket] %d %d %d ", message->type, message->id, message->ud); + + if (message->buffer == NULL) { + const char *buffer = (const char *)(message + 1); + sz -= sizeof(*message); + const char * eol = memchr(buffer, '\0', sz); + if (eol) { + sz = eol - buffer; + } + fprintf(f, "[%*s]", (int)sz, (const char *)buffer); + } else { + sz = message->ud; + log_blob(f, message->buffer, sz); + } + fprintf(f, "\n"); + fflush(f); +} + +void +skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz) { + if (type == PTYPE_SOCKET) { + log_socket(f, buffer, sz); + } else { + uint32_t ti = skynet_gettime(); + fprintf(f, ":%08x %d %d %u ", source, type, session, ti); + log_blob(f, buffer, sz); + fprintf(f,"\n"); + fflush(f); + } +} diff --git a/skynet-src/skynet_log.h b/skynet-src/skynet_log.h new file mode 100644 index 00000000..41ddeda0 --- /dev/null +++ b/skynet-src/skynet_log.h @@ -0,0 +1,14 @@ +#ifndef skynet_log_h +#define skynet_log_h + +#include "skynet_env.h" +#include "skynet.h" + +#include +#include + +FILE * skynet_log_open(struct skynet_context * ctx, uint32_t handle); +void skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle); +void skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz); + +#endif \ No newline at end of file diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index eaf6e14d..bdf0dcc1 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -9,6 +9,7 @@ #include "skynet_env.h" #include "skynet_monitor.h" #include "skynet_imp.h" +#include "skynet_log.h" #include @@ -37,13 +38,14 @@ struct skynet_context { void * instance; struct skynet_module * mod; - uint32_t handle; - int ref; - char result[32]; void * cb_ud; skynet_cb cb; - int session_id; struct message_queue *queue; + FILE * logfile; + char result[32]; + uint32_t handle; + int session_id; + int ref; bool init; bool endless; @@ -129,6 +131,7 @@ skynet_context_new(const char * name, const char *param) { ctx->cb = NULL; ctx->cb_ud = NULL; ctx->session_id = 0; + ctx->logfile = NULL; ctx->init = false; ctx->endless = false; @@ -177,6 +180,9 @@ skynet_context_grab(struct skynet_context *ctx) { static void delete_context(struct skynet_context *ctx) { + if (ctx->logfile) { + fclose(ctx->logfile); + } skynet_module_instance_release(ctx->mod, ctx->instance); skynet_mq_mark_release(ctx->queue); skynet_free(ctx); @@ -224,12 +230,15 @@ skynet_isremote(struct skynet_context * ctx, uint32_t handle, int * harbor) { } static void -_dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { +dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { assert(ctx->init); CHECKCALLING_BEGIN(ctx) pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle)); int type = msg->sz >> HANDLE_REMOTE_SHIFT; size_t sz = msg->sz & HANDLE_MASK; + if (ctx->logfile) { + skynet_log_output(ctx->logfile, msg->source, type, msg->session, msg->data, sz); + } if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) { skynet_free(msg->data); } @@ -242,7 +251,7 @@ skynet_context_dispatchall(struct skynet_context * ctx) { struct skynet_message msg; struct message_queue *q = ctx->queue; while (!skynet_mq_pop(q,&msg)) { - _dispatch_message(ctx, &msg); + dispatch_message(ctx, &msg); } } @@ -280,7 +289,7 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue if (ctx->cb == NULL) { skynet_free(msg.data); } else { - _dispatch_message(ctx, &msg); + dispatch_message(ctx, &msg); } skynet_monitor_trigger(sm, 0,0); @@ -412,17 +421,23 @@ cmd_exit(struct skynet_context * context, const char * param) { return NULL; } -static const char * -cmd_kill(struct skynet_context * context, const char * param) { +static uint32_t +tohandle(struct skynet_context * context, const char * param) { uint32_t handle = 0; if (param[0] == ':') { handle = strtoul(param+1, NULL, 16); } else if (param[0] == '.') { handle = skynet_handle_findname(param+1); } else { - skynet_error(context, "Can't kill %s",param); - // todo : kill global service + skynet_error(context, "Can't convert %s to handle",param); } + + return handle; +} + +static const char * +cmd_kill(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); if (handle) { handle_exit(context, handle); } @@ -503,14 +518,7 @@ cmd_monitor(struct skynet_context * context, const char * param) { } return NULL; } else { - if (param[0] == ':') { - handle = strtoul(param+1, NULL, 16); - } else if (param[0] == '.') { - handle = skynet_handle_findname(param+1); - } else { - skynet_error(context, "Can't monitor %s",param); - // todo : monitor global service - } + handle = tohandle(context, param); } G_NODE.monitor_exit = handle; return NULL; @@ -523,6 +531,48 @@ cmd_mqlen(struct skynet_context * context, const char * param) { return context->result; } +static const char * +cmd_logon(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); + if (handle == 0) + return NULL; + struct skynet_context * ctx = skynet_handle_grab(handle); + if (ctx == NULL) + return NULL; + FILE *f = NULL; + FILE * lastf = ctx->logfile; + if (lastf == NULL) { + f = skynet_log_open(context, handle); + if (f) { + if (!__sync_bool_compare_and_swap(&ctx->logfile, NULL, f)) { + // logfile opens in other thread, close this one. + fclose(f); + } + } + } + skynet_context_release(ctx); + return NULL; +} + +static const char * +cmd_logoff(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); + if (handle == 0) + return NULL; + struct skynet_context * ctx = skynet_handle_grab(handle); + if (ctx == NULL) + return NULL; + FILE * f = ctx->logfile; + if (f) { + // logfile may close in other thread + if (__sync_bool_compare_and_swap(&ctx->logfile, f, NULL)) { + skynet_log_close(context, f, handle); + } + } + skynet_context_release(ctx); + return NULL; +} + static struct command_func cmd_funcs[] = { { "TIMEOUT", cmd_timeout }, { "REG", cmd_reg }, @@ -539,6 +589,8 @@ static struct command_func cmd_funcs[] = { { "ABORT", cmd_abort }, { "MONITOR", cmd_monitor }, { "MQLEN", cmd_mqlen }, + { "LOGON", cmd_logon }, + { "LOGOFF", cmd_logoff }, { NULL, NULL }, }; From d0c4a4012a23de9d1bd8837ef634b8b8e62dd1e3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 22 Aug 2014 15:41:05 +0800 Subject: [PATCH 167/729] clear socket when connect failed --- lualib/socket.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/socket.lua b/lualib/socket.lua index f54d2175..efc2ff0b 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -145,6 +145,8 @@ local function connect(id, func) suspend(s) if s.connected then return id + else + socket_pool[id] = nil end end From 98630badd517669323b6554b044aef7677c0c18c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 25 Aug 2014 10:13:04 +0800 Subject: [PATCH 168/729] update history for release v0.6.1 --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index e6a1917a..f3da837b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +v0.6.1 (2014-8-25) +----------- +* bugfix: datacenter.wakeup +* change struct msg name to avoid conflict in mac +* improve seri library + v0.6.0 (2014-8-18) ----------- * add sharedata From 0fde2ea5713578b3b5ac80f0a1b1fd811583a9f6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 26 Aug 2014 11:30:07 +0800 Subject: [PATCH 169/729] better error message --- service/cmaster.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/cmaster.lua b/service/cmaster.lua index 59bc61f1..eeb9461e 100644 --- a/service/cmaster.lua +++ b/service/cmaster.lua @@ -32,7 +32,7 @@ local function read_package(fd) local sz = socket.read(fd, 1) assert(sz, "closed") sz = string.byte(sz) - local content = socket.read(fd, sz) + local content = assert(socket.read(fd, sz), "closed") return skynet.unpack(content) end From 0dd7b2e959a237b10b65705bef5c32b26e9fea6b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 26 Aug 2014 11:34:16 +0800 Subject: [PATCH 170/729] better error message --- service/cslave.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/cslave.lua b/service/cslave.lua index fdbe9088..c380f371 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -14,7 +14,7 @@ local function read_package(fd) local sz = socket.read(fd, 1) assert(sz, "closed") sz = string.byte(sz) - local content = socket.read(fd, sz) + local content = assert(socket.read(fd, sz), "closed") return skynet.unpack(content) end From 165bed46a80fe3aa82f5b1bb710021b6ff241da3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 27 Aug 2014 18:54:46 +0800 Subject: [PATCH 171/729] only skynet.call response error --- lualib/skynet.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 6dd32a46..9e703ecd 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -144,7 +144,8 @@ function suspend(co, result, command, param, size) if not result then local session = session_coroutine_id[co] local addr = session_coroutine_address[co] - if session then + if session ~= 0 then + -- only call response error c.send(addr, skynet.PTYPE_ERROR, session, "") end session_coroutine_id[co] = nil From 8fa51dfc33cd16772b5c7cfa5f674ec5cd207175 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 28 Aug 2014 18:43:25 +0800 Subject: [PATCH 172/729] add sha and hmac-sha1 --- Makefile | 2 +- lualib-src/lsha1.c | 311 +++++++++++++++++++++++++++++++++++++++++ lualib-src/lua-crypt.c | 6 + test/testsha.lua | 217 ++++++++++++++++++++++++++++ 4 files changed, 535 insertions(+), 1 deletion(-) create mode 100644 lualib-src/lsha1.c create mode 100644 test/testsha.lua diff --git a/Makefile b/Makefile index 2b06c01d..543c6c28 100644 --- a/Makefile +++ b/Makefile @@ -110,7 +110,7 @@ $(LUA_CLIB_PATH)/multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -$(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ $(LUA_CLIB_PATH)/sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) diff --git a/lualib-src/lsha1.c b/lualib-src/lsha1.c new file mode 100644 index 00000000..c421303c --- /dev/null +++ b/lualib-src/lsha1.c @@ -0,0 +1,311 @@ +/* +SHA-1 in C +By Steve Reid +100% Public Domain + +----------------- +Modified 7/98 +By James H. Brown +Still 100% Public Domain + +Corrected a problem which generated improper hash values on 16 bit machines +Routine SHA1Update changed from + void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int +len) +to + void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned +long len) + +The 'len' parameter was declared an int which works fine on 32 bit machines. +However, on 16 bit machines an int is too small for the shifts being done +against +it. This caused the hash function to generate incorrect values if len was +greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update(). + +Since the file IO in main() reads 16K at a time, any file 8K or larger would +be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million +"a"s). + +I also changed the declaration of variables i & j in SHA1Update to +unsigned long from unsigned int for the same reason. + +These changes should make no difference to any 32 bit implementations since +an +int and a long are the same size in those environments. + +-- +I also corrected a few compiler warnings generated by Borland C. +1. Added #include for exit() prototype +2. Removed unused variable 'j' in SHA1Final +3. Changed exit(0) to return(0) at end of main. + +ALL changes I made can be located by searching for comments containing 'JHB' +----------------- +Modified 8/98 +By Steve Reid +Still 100% public domain + +1- Removed #include and used return() instead of exit() +2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall) +3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net + +----------------- +Modified 4/01 +By Saul Kravitz +Still 100% PD +Modified to run on Compaq Alpha hardware. + +----------------- +Modified 07/2002 +By Ralph Giles +Still 100% public domain +modified for use with stdint types, autoconf +code cleanup, removed attribution comments +switched SHA1Final() argument order for consistency +use SHA1_ prefix for public api +move public api to sha1.h + +----------------- +Modufiled 08/2014 +By Cloud Wu +Still 100% PD +Lua binding +*/ + +/* +Test Vectors (from FIPS PUB 180-1) +"abc" + A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D +"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 +A million repetitions of "a" + 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F +*/ + +#include +#include +#include + +typedef struct { + uint32_t state[5]; + uint32_t count[2]; + uint8_t buffer[64]; +} SHA1_CTX; + +#define SHA1_DIGEST_SIZE 20 + +static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +/* blk0() and blk() perform the initial expand. */ +/* I got the idea of expanding during the round function from SSLeay */ +/* FIXME: can we do this in an endian-proof way? */ +#ifdef WORDS_BIGENDIAN +#define blk0(i) block.l[i] +#else +#define blk0(i) (block.l[i] = (rol(block.l[i],24)&0xFF00FF00) \ + |(rol(block.l[i],8)&0x00FF00FF)) +#endif +#define blk(i) (block.l[i&15] = rol(block.l[(i+13)&15]^block.l[(i+8)&15] \ + ^block.l[(i+2)&15]^block.l[i&15],1)) + +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); + + +/* Hash a single 512-bit block. This is the core of the algorithm. */ +static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) +{ + uint32_t a, b, c, d, e; + typedef union { + uint8_t c[64]; + uint32_t l[16]; + } CHAR64LONG16; + CHAR64LONG16 block; + + memcpy(&block, buffer, 64); + + /* Copy context->state[] to working vars */ + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + + /* Wipe variables */ + a = b = c = d = e = 0; +} + + +/* SHA1Init - Initialize new context */ +static void sat_SHA1_Init(SHA1_CTX* context) +{ + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + + +/* Run your data through this. */ +static void sat_SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) +{ + size_t i, j; + +#ifdef VERBOSE + SHAPrintContext(context, "before"); +#endif + + j = (context->count[0] >> 3) & 63; + if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; + context->count[1] += (len >> 29); + if ((j + len) > 63) { + memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1_Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) { + SHA1_Transform(context->state, data + i); + } + j = 0; + } + else i = 0; + memcpy(&context->buffer[j], &data[i], len - i); + +#ifdef VERBOSE + SHAPrintContext(context, "after "); +#endif +} + + +/* Add padding and return the message digest. */ +static void sat_SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) +{ + uint32_t i; + uint8_t finalcount[8]; + + for (i = 0; i < 8; i++) { + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + sat_SHA1_Update(context, (uint8_t *)"\200", 1); + while ((context->count[0] & 504) != 448) { + sat_SHA1_Update(context, (uint8_t *)"\0", 1); + } + sat_SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ + for (i = 0; i < SHA1_DIGEST_SIZE; i++) { + digest[i] = (uint8_t) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } + + /* Wipe variables */ + i = 0; + memset(context->buffer, 0, 64); + memset(context->state, 0, 20); + memset(context->count, 0, 8); + memset(finalcount, 0, 8); /* SWR */ +} + +#include +#include + +int +lsha1(lua_State *L) { + size_t sz = 0; + const uint8_t * buffer = (const uint8_t *)luaL_checklstring(L, 1, &sz); + uint8_t digest[SHA1_DIGEST_SIZE]; + SHA1_CTX ctx; + sat_SHA1_Init(&ctx); + sat_SHA1_Update(&ctx, buffer, sz); + sat_SHA1_Final(&ctx, digest); + lua_pushlstring(L, (const char *)digest, SHA1_DIGEST_SIZE); + + return 1; +} + +#define BLOCKSIZE 64 + +static inline void +xor_key(uint8_t key[BLOCKSIZE], uint32_t xor) { + int i; + for (i=0;i BLOCKSIZE) { + SHA1_CTX ctx; + sat_SHA1_Init(&ctx); + sat_SHA1_Update(&ctx, key, key_sz); + sat_SHA1_Final(&ctx, rkey); + key_sz = SHA1_DIGEST_SIZE; + } else { + memcpy(rkey, key, key_sz); + } + + xor_key(rkey, 0x5c5c5c5c); + sat_SHA1_Init(&ctx1); + sat_SHA1_Update(&ctx1, rkey, BLOCKSIZE); + + xor_key(rkey, 0x5c5c5c5c ^ 0x36363636); + sat_SHA1_Init(&ctx2); + sat_SHA1_Update(&ctx2, rkey, BLOCKSIZE); + sat_SHA1_Update(&ctx2, text, text_sz); + sat_SHA1_Final(&ctx2, digest2); + + sat_SHA1_Update(&ctx1, digest2, SHA1_DIGEST_SIZE); + sat_SHA1_Final(&ctx1, digest1); + + lua_pushlstring(L, (const char *)digest1, SHA1_DIGEST_SIZE); + + return 1; +} + diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 01f9ac0a..ca4bd430 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -836,6 +836,10 @@ lb64decode(lua_State *L) { return 1; } +// defined in lsha1.c +int lsha1(lua_State *L); +int lhmac_sha1(lua_State *L); + int luaopen_crypt(lua_State *L) { luaL_checkversion(L); @@ -852,6 +856,8 @@ luaopen_crypt(lua_State *L) { { "dhsecret", ldhsecret }, { "base64encode", lb64encode }, { "base64decode", lb64decode }, + { "sha1", lsha1 }, + { "hmac_sha1", lhmac_sha1 }, { NULL, NULL }, }; luaL_newlib(L,l); diff --git a/test/testsha.lua b/test/testsha.lua new file mode 100644 index 00000000..d0ed1997 --- /dev/null +++ b/test/testsha.lua @@ -0,0 +1,217 @@ +local skynet = require "skynet" +local crypt = require "crypt" + +local function sha1(text) + local c = crypt.sha1(text) + return crypt.hexencode(c) +end + +local function hmac_sha1(key, text) + local c = crypt.hmac_sha1(key, text) + return crypt.hexencode(c) +end + +-- test case from http://regex.info/code/sha1.lua + +print(1) assert(sha1 "http://regex.info/blog/" == "7f103bf600de51dfe91062300c14738b32725db5", 1) +print(2) assert(sha1(string.rep("a", 10000)) == "a080cbda64850abb7b7f67ee875ba068074ff6fe", 2) +print(3) assert(sha1 "abc" == "a9993e364706816aba3e25717850c26c9cd0d89d", 3) +print(4) assert(sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" == "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 4) +print(5) assert(sha1 "The quick brown fox jumps over the lazy dog" == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", 5) +print(6) assert(sha1 "The quick brown fox jumps over the lazy cog" == "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3", 6) +print(7) assert("efb750130b6cc9adf4be219435e575442ec68b7c" == sha1(string.char(136,43,218,202,158,86,64,140,154,173,20,184,170,125,37,54,208,68,171,24,164,89,142,111,148,235,187,181,122):rep(76)), 7) +print(8) assert("432dff9d4023e13194170287103d0377ed182d96" == sha1(string.char(20,174):rep(407)), 8) +print(9) assert("ccba5c47946530726bb86034dbee1dbf0c203e99" == sha1(string.char(20,54,149,252,176,4,96,100,223):rep(753)), 9) +print(10) assert("4d6fea4f8576cd6648ae2d2ee4dc5df0a8309115" == sha1(string.char(118,171,221,33,54,209,223,152,35,67,88,50):rep(985)), 10) +print(11) assert("f560412aabf813d01f15fdc6650489584aabd266" == sha1(string.char(23,85,29,13,146,55,164,14,206,196,109,183,53,92,97,123,242,220,112,15,43,113,22,246,114,29,209,219,190):rep(177)), 11) +print(12) assert("b56795e12f3857b3ba1cbbcedb4d92dd9c419328" == sha1(string.char(21,216):rep(131)), 12) +print(13) assert("54f292ecb7561e8ce27984685b427234c9465095" == sha1(string.char(15,205,12,181,4,114,128,118,219):rep(818)), 13) +print(14) assert("fe265c1b5a848e5f3ada94a7e1bb98a8ce319835" == sha1(string.char(136,165,10,46,167,184,86,255,58,206,237,21,255,15,198,211,145,112,228,146,26,69,92,158,182,165,244,39,152):rep(605)), 14) +print(15) assert("96f186998825075d528646059edadb55fdd96659" == sha1(string.char(100,226,28,248,132,70,221,54,92,181,82,128,191,12,250,244):rep(94)), 15) +print(16) assert("e29c68e4d6ffd3998b2180015be9caee59dd8c8a" == sha1(string.char(247,14,15,163,0,53,50,113,84,121):rep(967)), 16) +print(17) assert("6d2332a82b3600cbc5d2417f944c38be9f1081ae" == sha1(string.char(93,98,119,201,41,27,89,144,25,141,117,26,111,132):rep(632)), 17) +print(18) assert("d84a91da8fb3aa7cd59b99f347113939406ef8eb" == sha1(string.char(28,252,0,4,150,164,91):rep(568)), 18) +print(19) assert("8edf1b92ad5a90ed762def9a873a799b4bda97f9" == sha1(string.char(166,67,113,111,161,253,169,195,158,97,96,150,49,219,103,16,186,184,37,109,228,111):rep(135)), 19) +print(20) assert("d5b2c7019f9ff2f75c38dc040c827ab9d1a42157" == sha1(string.char(38,252,110,224,168,60,2,133,8,153,200,0,199,104,191,62,28,168,73,48,199,217,83):rep(168)), 20) +print(21) assert("5aeb57041bfada3b72e3514f493d7b9f4ca96620" == sha1(string.char(57):rep(738)), 21) +print(22) assert("4548238c8c2124c6398427ed447ae8abbb8ead27" == sha1(string.char(221,131,171):rep(230)), 22) +print(23) assert("ed0960b87a790a24eb2890d8ea8b18043f1a87d5" == sha1(string.char(151,113,144,19,249,148,75,51,164,233,102,232,3,58,81,99,101,255,93,231,147,150,212,216,109,62):rep(110)), 23) +print(24) assert("d7ac6233298783af55901907bb99c13b2afbca99" == sha1(string.char(44,239,189,203,196,79,82,143,99,21,125,75,167,26,108,161,9,193,72):rep(919)), 24) +print(25) assert("43600b41a3c5267e625bbdfde95027429c330c60" == sha1(string.char(122,70,129,24,192,213,205,224,62,79,81,129,22,171):rep(578)), 25) +print(26) assert("5816df09a78e4594c1b02b170aa57333162def38" == sha1(string.char(76,103,48,150,115,161,86,42,247,82,197,213,155,108,215,18,119):rep(480)), 26) +print(27) assert("ef7903b1e811a086a9a5a5142242132e1367ae1d" == sha1(string.char(143,70):rep(65)), 27) +print(28) assert("6e16b2dac71e338a4bd26f182fdd5a2de3c30e6c" == sha1(string.char(130,114,144,219,245,72,205,44,149,68,150,169,243):rep(197)), 28) +print(29) assert("6cc772f978ca5ef257273f046030f84b170f90c9" == sha1(string.char(26,49,141,64,30,61,12):rep(362)), 29) +print(30) assert("54231fc19a04a64fc1aa3dce0882678b04062012" == sha1(string.char(76,252,160,253,253,167,27,179,237,15,219,46,141,255,23,53,184,190,233,125,211,11):rep(741)), 30) +print(31) assert("5511a993b808e572999e508c3ce27d5f12bb4730" == sha1(string.char(197,139,184,188,200,31,171,236,252,147,123,75,7,138,111,167,68,114,73,80,51,233,241,233,91):rep(528)), 31) +print(32) assert("64c4577d4763c95f47ac5d21a292836a34b8b124" == sha1(string.char(177,114,100,216,18,57,2,110,108,60,81,80,253,144,179,47,228,42,105,72,86,18,30,167):rep(901)), 32) +print(33) assert("bb0467117e3b630c2b3d9cdf063a7e6766a3eae1" == sha1(string.char(249,99,174,228,15,211,121,152,203,115,197,198,66,17,196,6,159,170,116):rep(800)), 33) +print(34) assert("1f9c66fca93bc33a071eef7d25cf0b492861e679" == sha1(string.char(205,64,237,65,171,0,176,17,104,6,101,29,128,200,214,24,32,91,115,71,26,11,226,69,141,83,249,129):rep(288)), 34) +print(35) assert("55d228d9bfd522105fe1e1f1b5b09a1e8ee9f782" == sha1(string.char(96,37,252,185,137,194,215,191,190,235,73,224,125,18,146,74,32,82,58,95,49,102,85,57,241,54,55):rep(352)), 35) +print(36) assert("58c3167e666bf3b4062315a84a72172688ad08b1" == sha1(string.char(65,91,96,147,212,18,32,144,138,187,70,26,105,42,71,13,229,137,185,10,86,124,171,204,104,42,2,172):rep(413)), 36) +print(37) assert("f97936ca990d1c11a9967fd12fc717dcd10b8e9e" == sha1(string.char(253,159,59,76,230,153,22,198,15,9,223,3,31):rep(518)), 37) +print(38) assert("5d15229ad10d2276d45c54b83fc0879579c2828e" == sha1(string.char(149,20,176,144,39,216,82,80,56,38,152,49,167,120,222,20,26,51,157,131,160,52,6):rep(895)), 38) +print(39) assert("3757d3e98d46205a6b129e09b0beefaa0e453e64" == sha1(string.char(120,131,113,78,7,19,59,120,210,220,73,118,36,240,64,46,149,3,120,223,80,232,255,212,250,76,109,108,133):rep(724)), 39) +print(40) assert("5e62539caa6c16752739f4f9fd33ca9032fff7e1" == sha1(string.char(216,240,166,165,2,203,2,189,137,219,231,229):rep(61)), 40) +print(41) assert("3ff1c031417e7e9a34ce21be6d26033f66cb72c9" == sha1(string.char(4,178,215,183,17,198,184,253,137,108,178,74,244,126,32):rep(942)), 41) +print(42) assert("8c20831fc3c652e5ce53b9612878e0478ab11ee6" == sha1(string.char(115,157,59,188,221,67,52,151,147,233,84,30,243,250,109,103,101,0,219,13,176,38,21):rep(767)), 42) +print(43) assert("09c7c977cb39893c096449770e1ed75eebb9e5a1" == sha1(string.char(184,131,17,61,201,164,19,25,36,141,173,74,134,132,104,23,104,136,121,232,12,203,115,111,54,114,251,223,61,126):rep(458)), 43) +print(44) assert("9534d690768bc85d2919a059b05561ec94547fc2" == sha1(string.char(49,93,136,112,92,42,117,28,31):rep(187)), 44) +print(45) assert("7dfca0671de92a62de78f63c0921ff087f2ba61d" == sha1(string.char(194,78,252,112,175,6,26,103,4,47,195,99,78,130,40,58,84,175,240,180,255,108,3,42,51,111,35,49,217,160):rep(72)), 45) +print(46) assert("62bf20c51473c6a0f23752e369cabd6c167c9415" == sha1(string.char(28,126,243,196,155,31,158,50):rep(166)), 46) +print(47) assert("2ece95e43aba523cdbf248d07c05f569ecd0bd12" == sha1(string.char(76,230,117,248,231,228):rep(294)), 47) +print(48) assert("722752e863386b737f29a08f23a0ec21c4313519" == sha1(string.char(61,102,1,118):rep(470)), 48) +print(49) assert("a638db01b5af10a828c6e5b73f4ca881974124a0" == sha1(string.char(130,8,4):rep(768)), 49) +print(50) assert("54c7f932548703cc75877195276bc2aa9643cf9b" == sha1(string.char(193,232,122,142,213,243,224,29,201,6,127,45,4,36,92,200,148,111,106,110,221,235,197,51,66,221,123,155,222,186):rep(290)), 50) +print(51) assert("ecfc29397445d85c0f6dd6dc50a1272accba0920" == sha1(string.char(221,76,21,148,12,109,232,113,230,110,96,82,36):rep(196)), 51) +print(52) assert("31d966d9540f77b49598fa22be4b599c3ba307aa" == sha1(string.char(148,237,212,223,44,133,153):rep(53)), 52) +print(53) assert("9f97c8ace98db9f61d173bf2b705404eb2e9e283" == sha1(string.char(190,233,29,208,161,231,248,214,210):rep(451)), 53) +print(54) assert("63449cfce29849d882d9552947ebf82920392aea" == sha1(string.char(216,12,113,137,33,99,200,140,6,222,170,2,115,50,138,134,211,244,176,250,42,95):rep(721)), 54) +print(55) assert("15dc62f4469fb9eae76cd86a84d576905c4bbfe7" == sha1(string.char(50,194,13,88,156,226,39,135,165,204):rep(417)), 55) +print(56) assert("abbc342bcfdb67944466e7086d284500e25aa103" == sha1(string.char(35,39,227,31,206,163,148,163,172,253,98,21,215,43,226,227,130,151,236,177,176,63,30,47,74):rep(960)), 56) +print(57) assert("77ae3a7d3948eaa2c60d6bc165ba2812122f0cce" == sha1(string.char(166,83,82,49,153,89,58,79,131,163,125,18,43,135,120,17,48,94,136):rep(599)), 57) +print(58) assert("d62e1c4395c8657ab1b1c776b924b29a8e009de1" == sha1(string.char(196,199,71,80,10,233,9,229,91,72,73,205,75,77,122,243,219,152):rep(964)), 58) +print(59) assert("15d98d5279651f4a2566eda6eec573812d050ff7" == sha1(string.char(78,70,7,229,21,78,164,158,114,232,100,102,92,18,133,160,56,177,160,49,168,149,13,39,249,214,54,41):rep(618)), 59) +print(60) assert("c3d9bc6535736f09fbb018427c994e58bbcb98f6" == sha1(string.char(129,208,110,40,135,3):rep(618)), 60) +print(61) assert("7dc6b3f309cbe9fa3b2947c2526870a39bf96dc4" == sha1(string.char(126,184,110,39,55,177,108,179,214,200,175,118,125,212,19,147,137,133,89,209,89,189,233,164,71,81,156,215,152):rep(908)), 61) +print(62) assert("b2d4ca3e787a1e475f6608136e89134ae279be57" == sha1(string.char(182,80,145,53,128,194,228,155,53):rep(475)), 62) +print(63) assert("fbe6b9c3a7442806c5b9491642c69f8e56fdd576" == sha1(string.char(9,208,72,179,222,245,140,143,123,111,236,241,40,36,49,68,61,16,169,124,104,42,136,82,172):rep(189)), 63) +print(64) assert("f87d96380201954801004f6b82af1953427dfdcb" == sha1(string.char(153,133,37,2,24,150,93,242,223,68,202,54,118,141,76,35,100,137,13):rep(307)), 64) +print(65) assert("a953e2d054dd77f75337dd9dfa58ec4d3978cfb4" == sha1(string.char(112,215,41,50,221,94):rep(155)), 65) +print(66) assert("e5c3047f9abfdd60f0c386b9a820f11d7028bc70" == sha1(string.char(247,177,124,213,47,175,139,203,81,21,85):rep(766)), 66) +print(67) assert("ee6fe88911a13abfc0006f8809f51b9de7f5920f" == sha1(string.char(81,84,151,242,186,133,39,245,175,79,66,170,246,216,0,88,100,190,137,2,146,58):rep(10)), 67) +print(68) assert("091db84d93bb68349eecf6bfa9378251ecd85500" == sha1(string.char(180,71,122,187):rep(129)), 68) +print(69) assert("d8041c7d65201cc5a77056a5c7eb94a524494754" == sha1(string.char(188,99,87,126,152,214,159,151,234,223,199,247,213,107,63,59,12,146,175,57,79,200,132,202):rep(81)), 69) +print(70) assert("cca1d77faf56f70c82fcb7bc2c0ac9daf553adff" == sha1(string.char(199,1,184,22,113,25,95,87,169,114,130,205,125,159,170,99):rep(865)), 70) +print(71) assert("c009245d9767d4f56464a7b3d6b8ad62eba5ddeb" == sha1(string.char(39,168,16,191,95,82,184,102,242,224,15,108):rep(175)), 71) +print(72) assert("8ce115679600324b58dc8745e38d9bea0a9fb4d6" == sha1(string.char(164,247,250,142,139,131,158,241,27,36,81,239,26,233,105,64,38,249,151,75,142,17,56,217,125,74,132,213,213):rep(426)), 72) +print(73) assert("75f1e55718fd7a3c27792634e70760c6a390d40f" == sha1(string.char(32,145,170,90,188,97,251,159,244,153,21,126,2,67,36,110,31,251,66):rep(659)), 73) +print(74) assert("615722261d6ec8cef4a4e7698200582958193f26" == sha1(string.char(51,24,1,69,81,157,34,185,26,159,231,119):rep(994)), 74) +print(75) assert("4f61d2c1b60d342c51bc47641e0993d42e89c0f9" == sha1(string.char(175,25,99,122,247,217,204,100,0,35,57,65,150,182,51,79,169,99,9,33,113,113,113):rep(211)), 75) +print(76) assert("7d9842e115fc2af17a90a6d85416dbadb663cef3" == sha1(string.char(136,42,39,219,103,95,119,125,205,72,174,15,210,213,23,75,120,56,104,31,63,255,253,100,61,55):rep(668)), 76) +print(77) assert("3eccab72b375de3ba95a9f0fa715ae13cae82c08" == sha1(string.char(190,254,173,227,195,41,49,122,135,57,100):rep(729)), 77) +print(78) assert("7dd68f741731211e0442ce7251e56950e0d05f96" == sha1(string.char(101,155,117,8,143,40,192,100,162,121,142,191,92):rep(250)), 78) +print(79) assert("5e98cdb7f6d7e4e2466f9be23ec20d9177c5ddff" == sha1(string.char(84,67,182,136,89):rep(551)), 79) +print(80) assert("dba1c76e22e2c391bde336c50319fbff2d66c3bb" == sha1(string.char(99,226,185,1,192):rep(702)), 80) +print(81) assert("4b160b8bfe24147b01a247bfdc7a5296b6354e38" == sha1(string.char(144,141,254,1,166,144,129,232,203,31,192,75,145,12):rep(724)), 81) +print(82) assert("27625ad9833144f6b818ef1cf54245dd4897d8aa" == sha1(string.char(31,187,82,156,224,133,116,251,180,165,246,8):rep(661)), 82) +print(83) assert("0ce5e059d22a7ba5e2af9f0c6551d010b08ba197" == sha1(string.char(228):rep(672)), 83) +print(84) assert("290982513a7f67a876c043d3c7819facb9082ea6" == sha1(string.char(150,44,52,144,68,76,207,114,106,153,99,39,219,81,73,140,71,4,228,220,55,244,210,225,221,32):rep(881)), 84) +print(85) assert("14cf924aafceea393a8eb5dd06616c1fe1ccd735" == sha1(string.char(140,194,247,253,117,121,184,216,249,84,41,12,199):rep(738)), 85) +print(86) assert("91e9cc620573db9a692bb0171c5c11b5946ad5c3" == sha1(string.char(48,77,182,152,26,145,231,116,179,95,21,248,120,55,73,66):rep(971)), 86) +print(87) assert("3eb85ec3db474d01acafcbc5ec6e942b3a04a382" == sha1(string.char(68,211):rep(184)), 87) +print(88) assert("f9bd0e886c74d730cb2457c484d30ce6a47f7afa" == sha1(string.char(168,73,19,144,110,93,7,216,40,111,212,192,33,9,136,28,210,175,140,47,125,243,206,157,151,252,26):rep(511)), 88) +print(89) assert("64461476eb8bba70e322e4b83db2beaee5b495d4" == sha1(string.char(33,141):rep(359)), 89) +print(90) assert("761e44ffa4df3b4e28ca22020dee1e0018107d21" == sha1(string.char(254,172,185,30,245,135,14,5,186,42,47,22):rep(715)), 90) +print(91) assert("41161168b99104087bae0f5287b10a15c805596f" == sha1(string.char(79):rep(625)), 91) +print(92) assert("7088f4d88146e6e7784172c2ad1f59ec39fa7768" == sha1(string.char(20,138,80,102,138,182,54,210,38,214,125,123,157,209,215,37):rep(315)), 92) +print(93) assert("2e498e938cb3126ac1291cee8c483a91479900c1" == sha1(string.char(140,197,97,112,205,97,134,190):rep(552)), 93) +print(94) assert("81a2491b727ef2b46fb84e4da2ced84d43587f4e" == sha1(string.char(109,44,17,199,17,107,170,54,113,153,212,161,174):rep(136)), 94) +print(95) assert("0e4f8a07072968fbc4fe32deccbb95f113b32df7" == sha1(string.char(181,247,203,166,34,61,180,48,46,77,98,251,72,210,217,242,135,133,38,12,177,163,249,31,1,162):rep(282)), 95) +print(96) assert("8d15dddd48575a1a0330976b57e2104629afe559" == sha1(string.char(15,60,105,249,158,45,14,208,202,232,255,181,234,217):rep(769)), 96) +print(97) assert("98a9dd7b57418937cbd42f758baac4754d5a4a4b" == sha1(string.char(115,121,91,76,175,110,149,190,56,178,191,157,101,220,190,251,62,41,190,37):rep(879)), 97) +print(98) assert("578487979de082f69e657d165df5031f1fa84030" == sha1(string.char(189,240,198,207,102,142,241,154):rep(684)), 98) +print(99) assert("3e6667b40afb6bcc052654dd64a653ad4b4f9689" == sha1(string.char(85,82,55,80,43,17,57,20,157,10,148,85,154,58,254,254,221,132,53,105,43,234,251,110,111):rep(712)), 99) + +print(100) assert("31285f3fa3c6a086d030cf0f06b07e7a96b5cbd0" == hmac_sha1("63xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 100) +print(101) assert("2d183212abc09247e21282d366eeb14d0bc41fb4" == hmac_sha1("64xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 101) +print(102) assert("ff825333e64e696fc13d82c19071fa46dc94a066" == hmac_sha1("65xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 102) +print(103) assert("ecc8b5d3abb5182d3d570a6f060fef2ee6c11a44" == hmac_sha1(string.char(220,58,17,206,77,234,240,187,69,25,179,182,186,38,57,83,120,107,198,148,234,246,46,96,83,28,231,89,3,169,42,62,125,235,137), string.char(1,225,98,83,100,71,237,241,239,170,244,215,3,254,14,24,216,66,69,30,124,126,96,177,241,20,44,3,92,111,243,169,100,119,198,167,146,242,30,124,7,22,251,52,235,95,211,145,56,204,236,37,107,139,17,184,65,207,245,101,241,12,50,149,19,118,208,133,198,33,80,94,87,133,146,202,27,89,201,218,171,206,21,191,43,77,127,30,187,194,166,39,191,208,42,167,77,202,186,225,4,86,218,237,157,117,175,106,63,166,132,136,153,243,187)), 103) +print(104) assert("bd1577a9417d804ee2969636fa9dde838beb967d" == hmac_sha1(string.char(216,76,38,50,235,99,92,110,245,5,134,195,113,7), string.char(144,3,250,84,145,227,206,87,188,42,169,182,106,31,207,205,33,76,52,158,255,49,129,169,9,145,203,225,90,228,163,33,49,99,29,135,60,112,152,5,200,121,35,77,56,116,68,109,190,136,184,248,144,172,47,107,30,16,105,232,146,137,24,81,245,94,28,76,27,82,105,146,252,219,119,164,21,14,74,192,209,208,156,56,172,124,89,218,51,108,44,174,193,161,228,147,219,129,172,210,248,239,22,11,62,128,1,50,98,233,141,224,102,152,44,68,66,46,210,114,138,113,121,90,7,70,125,191,192,222,225,200,217,48,22,10,132,29,236,71,108,140,102,96,51,142,51,220,4)), 104) +print(105) assert("e8ddf90f0c117ee61b33db4df49d7fc31beca7f7" == hmac_sha1(string.char(189,213,184,86,24,160,198,82,138,223,71,149,249,70,183,47,0,106,27,39,85,102,57,190,237,182,2,10,21,253,252,3,68,73,154,75,79,247,28,132,229,50,2,197,204,213,170,213,255,83,100,213,60,67,202,61,137,125,16,215,254,157,106,5), string.char(78,103,249,15,43,214,87,62,52,57,135,84,44,92,142,209,120,139,76,216,33,223,203,96,218,12,6,126,241,195,47,203,196,22,113,138,228,44,122,37,215,166,184,195,91,97,175,153,115,243,37,225,82,250,54,240,20,237,149,183,5,43,142,113,214,130,100,149,83,232,70,106,152,110,25,74,46,60,159,239,193,173,235,146,173,142,110,71,1,97,54,217,52,228,250,42,223,53,105,24,87,98,117,245,101,189,147,238,48,111,68,52,169,78,151,38,21,249)), 105) +print(106) assert("e33c987c31bb45b842868d49e7636bd840a1ffd3" == hmac_sha1(string.char(154,60,91,199,157,45,32,99,248,5,163,234,114,223,234,48,238,82,43,242,52,176,243,5,135,26,141,49,105,120,220,135,192,96,219,152,126,74,58,110,200,56,108,1,68,175,82,235,149,191,67,72,195,46,35,131,237,188,177,223,145,122,26,234,136,93,34,96,71,214,55,27,13,116,235,109,58,83,175,226,59,13,218,93,5,132,43,235), string.char(182,10,154)), 106) +print(107) assert("f6039d217c16afadfcfae7c5e3d1859801a0fe22" == hmac_sha1(string.char(73,120,173,165,190,159,174,100,255,56,217,98,249,11,166,25,66,95,96,99,70,73,223,132,231,147,220,151,79,102,111,98), string.char(134,93,229,103)), 107) +print(108) assert("f6056b19cf6b070e62a0917a46f4b3aefcf6262d" == hmac_sha1(string.char(49,40,62,214,208,180,147,74,162,196,193,9,118,47,100,9,235,94,3,122,226,163,145,175,233,148,251,88,49,216,208,110,13,97,255,189,120,252,223,241,55,210,77,4), string.char(92,185,37,240,36,97,21,132,188,80,103,36,162,245,63,104,19,106,242,44,96,28,197,26,46,168,73,46,76,105,30,167,101,64,67,119,65,140,177,226,223,108,206,60,59,0,182,125,42,200,101,159,109,6,62,85,67,66,88,137,92,234,61,19,22,177,144,127,129,129,195,230,180,149,128,148,45,18,94,163,196,55,70,184,251,3,200,162,16,210,188,61,186,218,173,227,212,8,125,20,138,82,68,170,24,158,90,98,228,166,246,96,74,24,217,93,91,102,246,221,121,115,157,243,45,158,45,90,186,11,127,179,59,72,37,71,148,123,62,150,114,167,248,197,18,251,92,164,158,83,129,58,127,162,181,92,85,121,13,118,250,221,220,150,231,5,230,28,213,25,251,17,71,68,234,173,10,206,111,72,123,205,49,73,62,209,173,46,94,91,151,122)), 108) +print(109) assert("15ddfa1baa0a3723d8aff00aeefa27b5b02ede95" == hmac_sha1(string.char(91,117,129,150,117,169,221,182,193,130,126,206,129,177,184,171,174,224,91,234,60,197,243,158,59,0,122,240,145,106,192,179,96,84,46,239,170,123,120,51,142,101,45,34), string.char(27,71,38,183,232,93,23,174,151,181,121,91,142,138,180,125,75,243,160,220,225,205,205,72,13,162,104,218,47,162,77,23,16,154,31,156,122,67,227,25,190,102,200,57,116,190,131,8,208,76,174,53,204,88,227,239,71,67,208,32,57,72,165,121,80,139,80,29,90,32,229,208,115,169,238,189,206,82,180,68,157,124,119,1,75,27,122,246,197,178,90,225,138,244,73,12,226,104,191,70,53,210,245,250,239,238,3,229,196,22,28,199,122,158,9,33,109,182,109,87,25,159,159,146,217,77,37,25,173,211,38,173,70,252,18,193,7,167,160,135,63,190,149,14,221,143,173,152,184,143,157,245,137,219,74,239,185,40,14,0,29,87,169,84,67,75,255,252,201,131,75,108,43,129,210,193,108,139,60,228,29,36,150,15,86)), 109) +print(110) assert("d7cba04970a79f5d04c772fbe74bcc0951ff9c67" == hmac_sha1(string.char(159,179,206,236,86,25,53,102,163,243,239,139,134,14,243), string.char(106,160,249,31,114,205,9,66,139,182,209,218,31,151,57,132,182,85,218,220,103,15,210,218,101,244,240,227,12,184,127,137,40,127,232,195,234,182,0,214,125,122,141,207,61,244,143,202,159,229,100,76,165,44,226,137,100,61,1,107,132,142,144,158,223,249,151,78,186,194,189,83,45,66,178,41,23,172,195,137,170,216,208,27,149,112,68,188,0)), 110) +print(111) assert("84625a20dc9ada78ec9de910d37734a299f01c5d" == hmac_sha1(string.char(85,239,250,237,210,126,142,84,119,107,100,163,15,179,132,206,112,85,119,101,44,163,240,10,14,69,169,158,170,190,95,66,129,69,218,229), string.char(210,103,131,185,91,224,115,108,129,11,50,16,90,98,64,124,157,177,50,21,30,201,244,101,136,104,149,102,34,166,25,62,1,79,216,4,221,113,168,169,5,151,172,22,166,28,130,137,251,164,220,189,253,45,149,80,247,84,130,208,69,49,120,8,90,154,100,191,121,81,230,207,23,189,7,164,112,123,158,192,224,255,218,200,70,238,211,161,35,251,150,125,24,10,131,220,57,178,196,38,231,196,206,94,118,32,56,197,136,148,145,247,188,64,56,53,195,140,92,202,22,122,229,105,115,14,42,27,107,223,105)), 111) +print(112) assert("c463318ac1878cd770ec93b2710b706296316894" == hmac_sha1(string.char(162,21,153,195,254,135,203,75,152,144,200,187,226,4,248,93,161,180,219,181,99,130,122,28,179,140,152,9,21,115,34,140,162,45,88,4,33,238,179,125,58,23,108,194,158,48,191,40,3,50,81,247,114,241,0,88,147,93,57,178,73,187,11,195,254,171,106,167,245,190,117,160,1,219,200,249,107,233,58,19,122), string.char(246,179,198,163,52,106,45,38,131,22,142,185,149,121,79,211,0,16,102,52,46,176,83,7,32,42,103,184,234,107,180,128,128,130,21,27,236)), 112) +print(113) assert("e2d37df980593b3e52aadb0d99d61114fb2c1182" == hmac_sha1(string.char(43,84,125,158,182,211,26,238,222,247,6,171,184,54,70,44,169,4,74,34,98,71,118,189,138,53,8,164,117,22,76,171,57,255,122,230,110,122,228,22,252,123,174,218,222,77,80,150,159,43,236,137,234,48,122,138,100,137,112), string.char(249,234,226,86,109,2,157,76,229,42,178,223,196,247,42,194,17,17,117,3,45,6,80,202,22,105,44,242,84,25,21,189,5,216,35,200,220,192,110,81,215,145,109,179,48,44,40,35,216,240,48,240,33,210,79,35,64,189,81,15,135,228,83,14,254,32,211,229,158,79,188,230,84,106,78,126,226,106,203,59,67,134,186,52,21,48,2,142,231,116,241,167,177,175,74,188,232,234,56,41,181,118,232,190,184,76,64,109,167,178,123,118,3,50,46,254,253,83,156,116,220,247,69,27,160,167,210,205,79,60,28,253,17,219,32,44,217,223,77,153,229,55,113,75,234,154,247,13,133,49,220,200,241,111,136,205,14,78,222,55,181,250,160,143,224,37,63,227,155,12,39,173,209,45,171,93,93,70,36,129,111,173,183,112,31,231,22,146,129,171,75,128,45)), 113) +print(114) assert("666f9c282cf544c9c28006713a7461c7efbbdbda" == hmac_sha1(string.char(71,33,203,83,173,199,175,245,206,13,237,187,54,61,85,15,153,125,168,223,231,56,46,250,173,192,247,189,45,166,225,223,109,254,15,79,144,188,71,201,70,25,218,205,13,184,204,219,221,82,133,189,144,179,242,125,211,108,100,1,132,110,231,87,107,91,169,101,241,105,30), string.char(98,122,51,157,136,38,149,9,82,27,218,155,76,61,254,154,43,172,59,105,123,45,97,146,191,58,50,153,139,40,37,116)), 114) +print(115) assert("6904a4ce463002b03923c71cdac6c38f57315b63" == hmac_sha1(string.char(15,40,41,76,105,201,153,223,174,12,100,114,234,95,204,95,84,31,26,28,69,85,48,111,40,173,162,6,198,36,252,179,244,9,112,213,64,87,58,186,4,147,229,211,161,30,226,159,75,38,91,89,224,245,156,110,229,50,210), string.char(213,155,72,86,75,185,138,211,199,57,75,9,1,141,184,89,82,180,105,17,193,63,161,202,25,60,16,201,72,179,74,129,73,172,84,39,140,33,25,122,136,143,116,100,100,234,246,108,135,180,85,12,85,151,176,154,172,147,249,242,180,207,126,126,235,203,55,8,251,29,135,134,166,152,80,76,242,15,69,225,199,221,133,118,194,227,9,185,190,125,120,116,182,15,241,209,113,253,241,58,145,106,136,25,60,47,174,209,251,54,159,98,103,88,245,61,108,91,12,245,119,191,232,36)), 115) +print(116) assert("c57c742c772f92cce60cf3a1ddc263b8df0950f3" == hmac_sha1(string.char(193,178,151,175,44,234,140,18,10,183,92,17,232,95,94,198,229,188,188,131,247,236,215,129,243,171,223,83,42,173,88,157,29,46,221,57,80,179,139,80), string.char(214,95,30,179,51,95,183,198,185,9,76,215,255,122,91,103,133,117,42,33,0,145,60,129,129,237,203,59,141,214,108,79,247,244,187,250,157,177,245,72,191,63,176,211)), 116) +print(117) assert("092336f7f1767874098a54d5f27092fbb92b7c94" == hmac_sha1(string.char(208,10,217,108,72,232,56,67,6,179,160,172,29,67,115,171,118,82,124,118,61,111,21,132,209,92,212,166,181,129,43,55,198,96,169,112,86,212,68,214,81,239,122,216,104,73,114,209,60,182,248,118,74,100,26,1,176,3,166,188), string.char(42,4,235,56,198,119,175,244,93,77)), 117) +print(118) assert("c1d58ad2b611c4d9d59e339da7952bdae4a70737" == hmac_sha1(string.char(100,129,37,207,188,116,232,140,204,90,90,93,124,132,140,81,102,156,67,254,228,192,150,161,10,62,143,218,237,111,151,98,78,168,188,87,170,190,35,228,169,228,143,252,213,85,11,124,92,91,18,27,122,141,98,6,77,106,205,209,2,185,249), string.char(58,190,56,255,176,80,220,228,0,251,108,108,220,197,51,131,164,162,60,181,21,54,122,174,31,13,4,84,198,203,105,11,64,230,1,30,218,208,252,219,44,147,2,108,227,66,49,71,21,95,248,93,193,180,165,240,224,226,13,9,208,186,12,118,243,28,113,49,122,192,212,164,43,41,151,183,187,126,115,85,174,40,125,9,54,193,164,95,21,77,200,226,50,115,187,122,34,141,255,224,239,12,132,191,48,102,205,248,128,164,116,48,39,191,98,53,169,230,249,215,231,152,45,27,226,10,143,15,209,157,208,181,15,210,195,5,29,48,29,125,62,59,191,216,170,179,226,110,40,46,32,130,235,48,234,233,17)), 118) +print(119) assert("3de84c915278a7d5e7d5629ec88193fb5bbadd15" == hmac_sha1(string.char(89,175,111,33,154,12,173,230,28,117), string.char(188,233,41,180,142,157,96,76,105,212,92,202,155,167,179,28,12,156,64,73,32,253,253,166,9,240,7,0,248,43,101,135,226,231,173,221,180,43,160,217,6,38,183,186,214,217,137,83,148,148,40,141,217,98,209,12,167,102,95,166,136,231,232,84,59,112,148,201,166,104,135,124,189,85,160,183,143,122,200,190,144,205,25,254,180,188,108,225,171,131,240,185,86,243,192,173,130,50,150,57,242,180,132,193,11,110,247,121,25,24,110,199,156,121,233,149,79,103,15,173,184,143,45,125,164,242,125,10,183,189,189,135,121,59,148,106,77,9,16,123,176,100,142,246,156,180,202,87,43,102,113,123)), 119) +print(120) assert("e8cc5a50f80e7d52edd7ae4ec037414b70798598" == hmac_sha1(string.char(139,243,17,238,11,156,138,122,212,86,201,213,100,167,65,199,92,182,255,36,221,82,192,213,199,162,69,42,222,95,65,170,146,48,39,185,147,18,140,122,168,141), string.char(141,216,25,29,235,207,123,188,85,53,131,180,125,226,97,244,250,138,64,39,30,250,146,101,219,171,213,158,164,172,137,22,136,86,46,77,95,213,66,198,15,24,164,148,29,166,169,195,196,111,122,147,247,215,148,9,129,40,133,178,69,242,171,235,181,83,241,208,237,17,37,30,188,152,47,214,69,229,114,225,75,172,163,184,38,158,230,177,187,124,33,240,238,148,197,235,237,98,33,237,59,205,147,128,108,254,95,5,6,49,10,224,78,139,164,235,220,78,224,15,213,136,198,217,114,156,130,247,167,64,36,10,183,221,193,220,195,80,125,143,248,228,254,6,221,137,170,211,66)), 120) +print(121) assert("80d9a0bc9600e6a0130681158a1787ef6b9c91d6" == hmac_sha1(string.char(152,28,190,131,179,120,137,63,214,85,213,89,116,246,171,92,0,47,44,102,120,206,185,116,71,106,195), string.char(250,9,181,163,46,88,166,238,164,40,207,236,152,104,201,42,14,255,125,57,173,176,230,171,63,6,254,130,140,201,45,152,164,216,171,27,172,105,19,103,128,33,255,93,64,155,55,210,140,2,94,168,168,164,5,218,249,227,221,133,72,112,97,243,140,149,193,80)), 121) +print(122) assert("cb4182de586a30d606a057c948fe33733145790a" == hmac_sha1(string.char(193,229,65,81,159,93,162,173,74,64,195,41,80,189,104,238,119,67,255,63,209,247,146,247,210,208,86,14,102,153,246,175,242,209,42,4,243,138,217,58,206,147,19,163,152,239,154,78,5,1,175,97,251,24,185,10,67,107,174,145,178,121,36,238,108,85,214,162,78,195,107,135,114,76,180,37,99,103,78,232,29,244,11,60,236,112,18,241,39,164,107,18), string.char(222,85,11,36,12,191,252,103,180,161,84,168,21,125,50,76,4,148,195,230,210,114,35,116,225,176,16,64,44,20,17,224,227,243,175,251,204,177,41,223,76,216,228,221,54,226,150,209,145,175,142,137,140,25,196,99,252,175,125,15,39,76,47,127,188,51,88,227,194,171,249,141,12,249,225,199,241,112,153,26,107,204,191,23,241,46,223,143,9,9,46,64,197,209,23,18,208,139,148,45,146,94,80,86,49,12,122,218,14,210,133,164,39,227,102,60,129,6,43)), 122) +print(123) assert("864e291ec69124c3a43976bae4ba1788f181f027" == hmac_sha1(string.char(231,211,73,154,64,125,224,253,200,234,208,210,83,9), string.char(33,248,155,139,159,173,90,40,200,95,96,12,81,103,8,139,211,189,87,75,8,6,36,103,116,204,137,181,81,233,97,171,47,27,143,164,63,3,223,107,80,232,182,154,7,255,190,171,209,115,219,6,34,109,1,254,214,73,2)), 123) +print(124) assert("bee434914e65818f81bd07e4e0bbb58328be3ca1" == hmac_sha1(string.char(222,123,148,175,157,117,104,212,169,13,252,113,231,104,37,8,147,119,92,27,100,132,250,105,63,13,195,90,251,122,185,97,159,68,36,7,75,163,179,3,6,170,164,152,86,30,49,239,201,245,43,220,78,62,154,206,95,24,149,138,138,15,24,68,58,255,99,88,143,192,12), string.char(97,76,73,171,140,99,57,217,56,73,182,189,58,19,177,104,85,85,45,31,170,132,53,51,155,143,70,169,189,5,41,231,34,132,235,228,114,58,100,105,146,27,67,89,32,34,78,133,155,222,88,26,83,198,128,183,19,243,27,186,192,77,184,54,142,57,24)), 124) +print(125) assert("0268f79ef55c3651a66a64a21f47495c5beeb212" == hmac_sha1(string.char(181,166,229,31,47,25,64,129,95,242,221,96,73,42,243,170,15,33,14,198,78,194,235,139,177,112,191,190,52,224,93,95,107,125,245,17,170,161,53,148,19,180,83,154,45,98,76,170,51,178,114,71,34,242,184,75,1,130,199,40,197,88,203,86,169,109,206,67,86,175,201,135,101,76,130,144,248,80,212,14,119,186), string.char(53,208,22,216,93,88,59,64,135,112,49,253,34,11,210,160,117,40,219,36,226,45,207,163,134,133,199,101)), 125) +print(126) assert("bcb2473c745d7dee1ec17207d1d804d401828035" == hmac_sha1(string.char(143,117,182,35), string.char(110,95,240,139,124,96,238,255,165,146,205,18,155,244,252,176,24,19,253,39,33,248,90,95,34,115,70,63,195,91,124,144,243,91,110,80,173,47,46,231,73,228,207,37,183,28,42,144,66,248,178,255,129,52,55,232,1,33,193,185,184,237,8)), 126) +print(127) assert("402ed75be595f59519e3dd52323817a65e9ff95b" == hmac_sha1(string.char(247,77,247,124,27,156,148,227,12,21,17,94,56,14,118,47,140,239,200,249,216,139,7,172,16,211,237,94,62,201,227,42,250,8,179,21,39,85,186,145,147,106,127,67,111,250,163,89,152,115,166,254,8,246,141,238,43,45,194,131,191,202), string.char(138,227,90,40,221,37,181,54,26,20,24,125,19,101,12,120,111,20,104,10,225,140,64,218,8,33,179,95,215,142,203,127,243,231,166,76,234,159,59,160,132,56,215,143,103,75,155,158,56,126,71,252,229,54,34,240,30,133,110,67,146,213,116,73,14,160,186,169,155,196,84,132,171,200,171,56,92,38,136,90,57,155,145,67,190,198,235,112,242,120,150,191,216,41,21,36,205,42,68,145,226,246,178,70,60,157,254,206,70,84,189,36,197,48,208,249,144,223,6,72,17,253,236,106,205,245,30,1,80,121,165,103,12,202,115,227,192,64,42,223,113,200,195,156,120,202,110,131,130,39,64,217,108,23,133,140,56,144,167,77,45,236,145,161,150,229,85,231,203,159,203,85,125,221,226)), 127) +print(128) assert("20231b6abcdd7e5d5e22f83706652acf1ae5255e" == hmac_sha1(string.char(132,122,252,170,107,28,195,210,121,194,245,252,42,64,42,103,220,18,61,68,19,78,182,234,93,130,175,20,4,199,66,91,247,247,87,107,89,205,68,125,19,254,59,47,228,134,200,145,148,78,52,236,51,255,152,231,47,168,213,124,228,34,48), string.char(61,232,69,199,207,49,72,159,37,230,105,207,63,141,245,127,142,77,168,111,126,92,145,26,202,215,116,19,125,81,203,11,86,36,19,218,90,255,4,10,21,185,151,111,188,125,123,2,137,151,171,178,195,199,106,55,42,42,20,164,35,177,237,41,207,24,102,218,213,223,204,186,212,30,59,121,52,34,84,76,142,179,75,6,84,8,72,210,72,177,45,103,218,70,165,69,36,152,50,228,147,192,61,104,209,76,155,147,169,162,151,178,72,109,241,41,76,0,60,251,107,99,92,37,95,215,172,154,172,93,254,65,176,43,99,242,151,78,48,58,35,49,23,174,115,224,227,106,49,30,9)), 128) +print(129) assert("b5a24d7aadc1e8fb71f914e1fa581085a8114b27" == hmac_sha1(string.char(233,21,254,12,70,129,236,10,187,36,119,197,152,242,131,11,10,243,169,217,128,247,196,111,183,109,146,155,92,91,71,83,0,136,109,1,130,143,0,8,6,22,160,153,190,164,199,56,152,229), string.char(152,112,218,132,38,125,94,106,64,9,137,105)), 129) +print(130) assert("7fd9169b441711bef04ea08ed87b1cd8f245aeb2" == hmac_sha1(string.char(108,226,105,159), string.char(103,218,254,85,50,58,111,74,108,60,174,27,113,65,100,217,97,208,125,38,170,151,72,125,82,114,185,58,4,143,39,223,250,168,66,182,37,216,130,111,103,157,59,0,245,222,8,65,240)), 130) +print(131) assert("98038102f8476b28dd11b535b6c1b93eb8089360" == hmac_sha1(string.char(77,121,52,138,71,149,161,87,106,75,232,247,8,148,175,24,23,60,151,104,176,208,148,227,226,191,73,123,164,36,177,235,101,190,128,202,139,116,201,125,61,41,204,187,48,107,63,231,12,137,11,228,93,136,178,243,90), string.char(90,6,232,110,12,66,45,86,141,121,2,6,177,135,64,208,45,178,129,37,253,246,74,48,71,211,243,121,31,209,138,147,185,141,65,185,245,9,137,134,148,123,181,128,204,74,222,150,239,169,179,36,236,104,147,255,251,204,5,191,198,185,153,195,26,112,179,170,232,101,238,143,143)), 131) +print(132) assert("ba43c9b8e98b4b7176a1bb63fcbed5b004dc4fcd" == hmac_sha1(string.char(133,107,178,183,204,2,203,106,242,180,87,58,134), string.char(211,208,255,34,198,99,119,185,171,52,118,94,65,241,45,24,40,6,2,203,126,216,21,51,245,154,92,198,201,220,190,135,182,137,113,68,250,94,126,233,140,94,118,244,244,98,252,97,217,204,239,218,178,240,65,150,246,217,231,142,157,33,113,135,135,214,137,103,211,213,48,132,171,43,96,51,241,45,42,237,53,247,134,102,163,214,96,147,131,74,180,19,7,12,174,60,37,60,78,85,92,186,121,27,234,128,132,64,47,106,127,218,52,30,59,230,85,240,164,54,168,232,131,110,145,125,255,238,145,237,134,196,197,138,105,74,34,134,29,249,222,119,194,104,230)), 132) +print(133) assert("0c1ad0332f0c6c1f61e6cc86f0a343065635ecb3" == hmac_sha1(string.char(230,111,251,74,216,102,12,134,90,44,121,175,56,221,225,235,180,246,56,12,236,119,119,109,97,59,224,121,27,37,72,219,138,193,50,219,193,152,1,229,224,197,233,76,188,62,120,17,63,158,186,198,87,135,34,60,164,139,3,200,20,188,203,110,212,137,91,70,35,255,146,213,216,71,73,143,55,219,54,142,105,83,175,193,32,226,150,51,59), string.char(185,250,11,8,121,214,91,27,1,50,67,204,219,252,212,198,117,234,57,127,214,12,220,104,44,177,225,238,103,138,144,162,28,69,143,108,192,4,160,63,175,185,123,46,151,221,127,145,55,32,85,245,251,178,125,223,107,192,206,207,232,231,190,44,221,173,1,59,12,178,112,22,253,222,14,40,18,141,128,144,196,6,112,206,183,144,215,156,159,79,132,152,170,22,68,106,137,248,12,242,231,98,96,144,148,20,212,30,242,109,36,112,50,51,57,212,181,191,80,73,215,119,244,209,57,108,147,182,67,204,154,48,216,116)), 133) +print(134) assert("466442888b6989fd380f918d0476a16ae26279cf" == hmac_sha1(string.char(250,60,248,215,154,159,2,121), string.char(236,249,8,10,180,146,36,144,83,196,49,166,17,81,51,72,28)), 134) +print(135) assert("f9febfeda402b930ec1b95c1325788305cb9fde8" == hmac_sha1(string.char(124,195,165,216,158,253,119,86,162,36,185,72,162,141,160,225,183,25,54,123,73,61,40,165,101,154,157,200,113,14,117,144,185,64,236,219,3,87,90,49), string.char(73,1,223,35,5,159,39,85,95,170,227,61,14,227,142,222,255,253,152,123,104,35,229,6,195,106,30,59,5,36,2,173,71,161,217,189,47,193,3,216,34,10,25,30,212,255,141,94,25,72,52,58,50,56,180,99,173,155)), 135) +print(136) assert("53fe70b7e825d017ef0bdecbacdda4e8a76cbc6f" == hmac_sha1(string.char(144,144,130,150,207,183,143,100,188,177,90,5,4,95,116,105,252,118,187,251,35,113,251,86,36,234,176,165,55,165,158,4,182,85,62,255,18,146,128,67,221,183,78,71,158,184,140,14,84,44,36,79,244,158,37,1,122,43,103,228,217,253,72,113,228,121,160,29,87,5,158,64,38,253,179,122,187,157,106,99,161,79,97,176,119,86,101,40,142,241,217,85), string.char(12,89,157,112,207,206,171,254,87,106,42,113,70,72,222,239,88,94,14,41,14,175,206,56,219,244,230,40,33,154,107,249,45,70,55,104,151,193,246,133,99,59,244,179,8,145,225,100,146,142,128,74,40,155,99,93,204,253,249,222,64,99,156,121,142,49,166,62,171,223,135,55,212,183,144,218,56,12,211,99,254,58,249,219,197,189,173,141,60,196,241,157,67,151,251,172,49,105,200,88,224,175,9,79,65,40,5,255,222,14,152,149,228,83,154,207,104,132,106,96,40,229,182,120,100,207,223,81,171,141,1,171)), 136) +print(137) assert("81b1df7265954a8aee4e119800169660ff9e75c8" == hmac_sha1(string.char(232,167,4,53,90,111,79,91,163,125,230,202,52,242,160,135,66,211,246,17,131,109,109,235,91), string.char(111,177,251,180,63,13,33,205,231,130,203,167,177,156,87,70,33,148,229,163,158,224,123,37,18,204,185,89,235,2,30,252,229,202,170,157,175,157,208,254,135,190,34,14,42,211,154,243,31,100,71,53,169,76,36)), 137) +print(138) assert("caf986508402b3feb70c5b44d902f4a9428a44d2" == hmac_sha1(string.char(127,2,162,53,6,172,189,169,176,254,107,46,57,207,23,25,182,72,34,228,164,113,12,179,149,45,169,19,3,204,202,10,126,153,130,41,143,200,198,47,215,15,58,124,225,60,171,98,8,211,72,235,211,187,172,37,119,96,55,243,98,198,225,191,79,207,94,215,255,21,101,128,153,233,88,101,57,195,70,194), string.char(97,24,129,203,10,176,242,39,165,95,51,30,187,106,207,160,18,154,16,130,178,80,206,128,148,56,213,55,46,85,76,100,50,131,41,167,130,12,81,161,158,55,181,12,12,38,89,193,136,220,81,114,191,157,20,221,171,245)), 138) +print(139) assert("83bd91a9e8072010f3b54d215c56ada0cd810bb6" == hmac_sha1(string.char(215,33,57,193,51,126,38,114,7,29,93,101,78,152,222,121,42,0,236,169,48,137,202,48,90,249,235,46,44,126,78,251,15,84,200,235,43,108,8,196,210,57,152,86,254), string.char(153,35,68,230,52,95,243,220,32,101,148,76,80,168,187,172,170,254,10,70,31,117,40,131,61,155,200,189,25,198,120,41,181,53,14,175,64,89,126,94,242,61,52,1,188,255,121,254,74,88,19,10,126,225,89,31,21,104,199,82,149,60,110,194,123,236,13,38,7,213,176,182,202,211,178,185,99,8,173,220,168,166,108,208,71,152,174,5,134,167,220,47,74,177,231,90,114,208,168,47,112,74,58,73,94,224,101,64,128,255,186,179,119,159,117,110,2,188,38,73,230,39,50,73,162,22,117,185,182,168,70,252,11,242,243,165,32,104,220,211)), 139) +print(140) assert("c123d92ee67689922dfb3b68a45584e7e5dc5dc3" == hmac_sha1(string.char(167,32,181,181,249,210), string.char(249,7,96,196,48,157,109,129,192,227,82,84,196,167,224,145,61,177,60,74,217,117,199,147,147,54,198,138,5,51,135,112,29,184,49,16,0,240,172,21,214,114,146,57,2,154,205,60,113,32,113,255,165,5,178,15,159)), 140) +print(141) assert("454d9ba00ac4e0e90a2866ba2abefba40dac9aab" == hmac_sha1(string.char(116,2,170,84,236,37), string.char(219,144,86,39,237,166,110,90,213,70)), 141) +print(142) assert("e919cce3908786205511fee0283e1eb41c0c45a1" == hmac_sha1(string.char(180,76,140,201,56,232,48,183,173,36,111,94,208,153,234,255,52,18,121,118,117,77,92,74,241,103,193,164,169,1,60,46,101,73,61,221,26,253,209,124,99,154,177,218,59,238,159,191,0,65,117,78,15,12,92,35,254,40,11,112,112,91,49,248,198,229,161,247,130,170,76,146,8,220,134,96,155,68,50,168,186,135,188,186,36), string.char(26,47,201,201,197,232,196,239,204,197,162,14,53,254,88,75,43,254,11,20,52,61,136,206,162,29,223,55,125,50,200,239,135,51,154,54,78,191,188,0,137,191,149,162,191,103,0,168,52,178,147,251,253,86,98,17,15,202,231,0,122,10,131,25,15,129,48,190,60,205,185,114,193,122,19,47,52,142,124,107,45,174,221,196,18,32,79,12,129,84,40)), 142) +print(143) assert("fa770f6b76984edb0f7fa514a19d3c7ef9c82c51" == hmac_sha1(string.char(70,55,200,82,238,241,84,180,129,122,103,199,170,177), string.char(88,236,14,60,249,2,197,242,76,152,248,241,158,232,113,242,209,93,232,113,147,210,80,180,44,124,186,150,172,177,138,131,117,151,174,231,93,248,244,200,191,169,159,206,232,246,2,121,106,183,107,79,210,73,145,244,254,248,85,217,156,221,238,120,224,176,31,56,246,26,3,186,189,86,41,17,34,8,208,58,46,130,226,139,209,194,3,34,55,213,154,12,250,229,201,122,89,47,177,229,80,165,183,77,178,139,3,241,165,196,239,93,98,101,99,210,99,5,135,132,151,197,4,154,87,130,145,175,243,238,132,0,146,14,6,205,227,78,150,59,191,223,74,145,57,82,30,223,90,205,248,47,195,220,215,167,112,216,20,130,26,88,170,101,26,14,216,62,169,217,1,135,193,147,218)), 143) +print(144) assert("d3418e6e6d0a96f8ea7c511273423651a63590c8" == hmac_sha1(string.char(61,175,211,89,77,91,143,76,18,30,252,16,181,45,211,37,207,204,174,174,60,208,36,85,23,106,141,215,62,8,158,98,209,49,96,143,129,99,11,159,79,116,98,244,51,237,227,250,73,196,36,216,181,157,159,87,248,108,29,22,181,175,72,92,160,127,46,204,202,175,61,108,172,50,225,48,84,167,116,67,183), string.char(174,62,218,81,85,89,4,35,26)), 144) +print(145) assert("f2fbe13b442bff3c09dcdaeaf375cb2f5214f821" == hmac_sha1(string.char(188,80,129,208,85,53,8,122,92,99,56,251,59,108,97,145,1,97,157,181,156,243,30,204,227,88,205,151), string.char(137,30,19,71,50,239,11,212,91,234,79,248,244,118,108,245,251,78,53,136,249,55,210,231,109,207,153,83,10,63,98,225,79,113,124,64,100,16,195,9,136,28,50,215,93,93,153,25,97,77,4,13,46,34,164,59,33,206,62,125,8,79,219,42,155,39,241,96,18)), 145) +print(146) assert("aeacb03e8284a01b93e51e483df58a9492035668" == hmac_sha1(string.char(62,67,97,44,112,68,107,91,105,4,183,154,9,14,164,180,87,140,195,231,62,49,250,154,193,186,219,18,100,156,176,124,223,164,68,64,105,214,144,200,65), string.char(221,20,36,192,59,234,90,174,232,184,75,15,92,229,59,191,36,51,4,190,226,35,237,189,21,100,135,138,171,202,163,227,176,231,72,185,43,197,134,157,44,86,26,42,230,251,2,124,220,245,242,116,77,35,197,154,237,73,117,131,126,242,167,242,8,72,163,171,60,29,77,152,142,69,25,234,195,2,159,49,178,63,48,56,131,143,165,22,142,116,109,11,57,71,101,99,84,204,56,233,173,65,180,228,211,10,197,204,26,70,198,81,217,83,130,70,79,54,93,96,116,32,21,219,193,70,55,84,161,89,47,206,13,167,46,104,205,218)), 146) +print(147) assert("e163c334e08bd3fa537c1ea309ce99cfbcd97930" == hmac_sha1(string.char(238,187,29,13,100,125,10,129,46,251,49,62,207,74,243,25,25,73,196,100,114,64,141,173,143,144,70,13,162,227,33,255,83,58,254,138,23,146,90,86,17,33,152,227,15,100,200,147,81,114,166,234,47,60,70,190,208,28,16,44,71,150,101,197,182,57,143,79,29,135,45,211,194,62,234,28,244,74,56,104,187,94,146,174,190,198,55,143,134,60,186,144,190,100,102,0,171,151), string.char(177,90,20,162,64,145,130,87,137,70,153,237,51,138,248,29,166,134,168,68,133,121,161,26,228,137,145,116,243,224,201,229,61,107,250,198,214,208,72,169,248,155,247,54,163,167,71,248,69,106,105,139,224,138,85,94,31,143,4,215,83,239,21,198,28,1,120,83,91,92,58,150,110,77,111,166,222,236,170,129,2,121,86,66,42,129,146,232,46,248,136,175,130,118,126,205,184,136,6,35,180,174,194,59,89,243,199,38,97,48,80,98,45,111)), 147) +print(148) assert("6a82b3a54ed409f612a934caf96e6c4799286f19" == hmac_sha1(string.char(11,59,108,100,113,56,239,211,2,138,219,101,50,50,111,31,171,212,228,55,89,196,44,158,77,252,212,134,106,7,250,223,219,46,231,87,9,62,213,104,169,49,1,75,3,10,50,238,5,150,47,14,47,45,171,183,40,245,194,249,228,216,85), string.char(97,131,91,189,9,52,88,203,250,245,46,96,7,95,176,61,57,192,91,231,193,32,41,169,96,101,39,240,29,143,24,56,57,177,70,135,225,90,42,192,134,103,239,175,243,246,76,137,14,237,82,215,7,76,246,219,132,50,175,25,164,4,216,65,102,0,114,216,159,80,58,32,4,100,22,35,71,140,49,16,216,182,91,80,181,129,151,55,130,7,77,84,211,111,45,51,192,123,213,192,11,81,138,60,225,21,37,182,180,223,170,163,110,9,47,214,200,249,164,98,215)), 148) +print(149) assert("79caa9946fcf0ccdd882bc409abe77d3470d28f7" == hmac_sha1(string.char(202,142,21,201,45,72,11,211,67,134,42,192,96,255,210,211,252,6,101,131,25,195,101,76,250,161,148,242,30,129,241,85,68,173,236,140,120,80,71,62,55,4,33,104,52,160,143,118,252,72), string.char(100,188,93,165,75,34,224,223,6,130,52,160,83,157,253,27,200,125,117,249,41,203,19,242,4,188,209,213,211,20,162,252,215,238,221,30,219,252,246,110,117,77,89,204,221,246,69,62,160,186,37,144,43,151,39,133,254,134,24,223)), 149) +print(150) assert("bd1e60a3df42e2687746766d7d67d57e262f3c9b" == hmac_sha1(string.char(57,212,172,224,206,230,13,50,80,54,19,87,147,166,245,67,118,37,101,214,207,60,66,29,197,236,146,140,192,15,36,155,108,121,215,211,210,118,22,20,56,245,42,153,150,32,224,201,171,2,238,41,70,140,73,60,215,243,86,20,169,152,220,104,95,215,187), string.char(245,123,43,109,29,108,197,27,55,167,255,177,226,205,111,126,146,75,93,70,180,90,139,218,243,232,221,247,129,77,115,101,144,42,13,249,223,127,200,213,140,113,23,10,149,107,244,86,41,133,147,26,167,85,189,76,176,12,190,52,90,54,78,29,97,204,161,224,59,243,128,64,133,95,192,19,137,220,96,228,97,161,51,21,157,172,127,76,53,38,83,126,178,26,203,206,165,241,101,130,79,122,75,29,205,240,1,68,222,48,101,7,29,6,82,240,133,112)), 150) +print(151) assert("fe249fa66eb1e6228e1e5166d7862d119ffa0dcf" == hmac_sha1(string.char(152,13,170,129,7,65,32,194,85,196,85,12,170,227,139,215,70,137,246,105,100,111,252,221,54,174,90,169,59,11,198,33,110,94,246,195,174,13,212,47,18,94,17,67,68,56,251,213,92,67,243,114,181,166,24,182,238,146,48,196,11,238,91,213,46,184,187,199,15,221,193,36,73,244,30,222,175,132,165,177,162,54,215,130,171,58,26,144,218,62,172,120,188,20,182,242,47,178,120,175), string.char(129,126,32,58,251,104,186,25,18,204,5,240,65,194,32,205,194,18,151,173,185,249,237,55,145,8,41,18,171,28,59,234,62,163,32,173,197,176,206,224,95,176,218,168,183,82,53,172,205,6,223,190,189,16,3,34,212,201,54,152,89,231,194,95,8,238,133,56,139,157,115,35,74,58,21,162,111,36,105,135,151,30,207,33,198,186,122,221,98,36,196,251,27)), 151) +print(152) assert("88aef9bb450b75bf96e8b5fd7831ed0d16d7fe7a" == hmac_sha1(string.char(162,225,9,204,87,95,132,152,211,133,93,120,105,156,131,55,245,224,33,115,182,10,28,49,80,175,241,67,66,52,21,55,174,108,66,100,77,20), string.char(55,61,250,187,157,98,212,245,5,54,170,60,235,228,197,182,230,24,195,215,202,55,56,153,9,94,164,107,67,93,143,1,235,195,133,201,103,57,130,177,71,73,169,80,21,251,130,247,21,61,228,39,166,173,211,203,149,55,11,224,70,52,248,118,112,219,39,188,147,5,203,101,158,134,53,250,103,73,154,249,71,181,53,171,227,26,79,200,145,178,24,80,102,26,90,101,70,143,233,17,150,181,170)), 152) +print(153) assert("a057dd823478540bbe9a6fcdf2d4dcfcac663545" == hmac_sha1(string.char(83,17,204,150,211,96,88,52,225,90,61,59,28,127,135), string.char(49,226,129,162,111,75,221,186,24,219,219,178,226,132,19,126,192,69,124,114,214,31,7,127,194,134,202,147,45,176,215,141,41,94,69,38,199,231,185,223,10,104,108,50,237,20,76,194,33,43,117,117,176,3,117,117,55,68,112,207,74,72,163)), 153) +print(154) assert("b11757ccfafc8feadc4e9402c820f4903f20032b" == hmac_sha1(string.char(34,11,53,219), string.char(225,194,251,106,223,229,212,105,164,172,25,25,224,199,225,172,197,42,167,1,227,165,17,247,75,250,10,208,99,124,254,158,103,74,89,60,78,141,201,44,253,87,248,239,74,86,75,64,249,189,21,170,249,18,139,21,130,226,66,200,231,35,227,147,95,213,133,35,47,236,52,64,122,115,80,170,27,50,182,151,180,106,120,85,103,255,143,27,233,43,217,152,70,82,8,229,103,42,153,38,130,184,108,160,199,69,38,184)), 154) +print(155) assert("bfd0994350b2e1e0d6a1faed059f67f1dd8b361f" == hmac_sha1(string.char(221,156,128,63,215,109,55,123,41,27,110,127,209,144,178,143,120,12,226,47,56,212,131,228,3,40,186,208,233,135,33,206,92,214,153,77,12,220,72,240,176,53,22,37,130,58,180,4,111,124,135,31,192,71,117,87,11,41,231,101,37,164,112,3,55,141,250,131,191,117,90,159,224,244,9,251,154), string.char(35,88,113,69,231,5,150,40,123,13,197,27,49,72,161,1,212,222,252,74,197,170,137,72,231,245,117,236,29,12,36,59,225,103,44,119,136,34,241,17,142,239,46,152,129,163,107,17,165,112,187,15,122,217,67,13,215,157,212,26,103,226,237,161,213,3,152,88,247,236,130,133,84,200,132,191,166,8,96,247,4,142,14,81,179,64,88,202,156,242,181,95,162,19,97,223,51,76,176,218,22,116,24,238,204,128,241,236,204,2,56,86,77,211,4,52,221,82,94,76,9,21,182,112,199,161,29,39,183,120,13,0,124,136,95,182,241,3,73,167,51,237,152,149,84,147,41,42,241,174,12,30,226,245,139,230,127,34,205,41,166,228,242,105,251,4,150,29,106,42,27,239,89,249,230,246,242,149,4,60,240,93,13,166,102,239,81,216,137,196)), 155) +print(156) assert("15c4527be05987a9b5c33b920be4a4402f7cf941" == hmac_sha1(string.char(132,158,227,135,167,32,19,192,144,48,232,68,79,78,135,122,136,140,97,67,34,91,225,48,126,67,77,115,154,189,13,45,8,234,59,233,245,77,34,76,95,78,3,250,179,224,20,25,6,202,214,115,165,230,85,115,250,236,135,34,32,158,196,45,207,61,71,51,93,27,187,232,175,233,147,68,231,110,12,198,233,165,24,209,206,25,16,252,127,72), string.char(210,162,121,23,181,21,75,54,110,47,15,166,133,206,100,217,57,133,228,32,112,208,186,28,140,8,207,74,185,17,128,29,181,172,20,156,64,192,112,8,207,131,53,10,182,147,224,71,218,94,71,86,215,2,133,46,160,27,51,189,38,130,224,120,185,144,253,117,193,24,154,229,247,132,62,103,65,163,106,172,22,5,2,32,129,38,213,118,110,125,156,14,48,240,170,225,158,89,92,190,254,231,51,220,8,174,188,233,226,106,224,99,218,82,64,219,206,75,166,111,25,7,87,248,145,251,109,106,243,241,89,200,85,184,155,183,249,61,70,87,115,182,81,80,155,24,245,123,184,102,214,255,122,83,61,214,88,235,169,28,147,196,247,97,28,82,193,72,71,96,243,176,35,189,236,44,184,52,151,249,186,189,150,184,142,238,88,113,120,68,234,50,136,104,80,145,179,57,6,186)), 156) +print(157) assert("b9f87f5f23583bdd21b1ea18e7e647513b7b0596" == hmac_sha1(string.char(216,79,247,98,215,219,128,232,135,111,179,168,80,76,36,250,224,157,229,209,238,186,212,188,200,52,208,200,210,79,182,186,22,22,37,34,86,107,89,238,168,90,53,30,151,191,89,163,253,24,204,152,118,1,158,25,168,2,123,91,111,39,101,239,247,37,200,51,215,31,146,211,163,136,208), string.char(153,134,211,81,255,219,50,91,67,48,102,63,244,170,129,66,3,151,110,167,142,150,153,95,89,90,23,87,12,85,42,209,197,36,41,189,199,136,196,159,125,53,253,192,135,234,94,34,87,79,143,213,237,149,212,170,231,181,22,72,81,233,151,243,134,71,160,155,121,32,233,251,187,179,139,48,254,206,172,165,202,105,222,31,223,95,203,87,114,52,218,59,187,41,43,135,200,108,102,201,85,199,8,176,249,44,96,104,160,85,149,177,25,88,55,231,85,120,227,6,180,24)), 157) +print(158) assert("0b586895674ab11d3b395c07ddb01151a6e562f5" == hmac_sha1(string.char(26,110,222,147,168,18,74,206,186,238,154,250,229,207,138,175,126,82,236,148,74,180,27,168,159,89,211,34,39,115,227,239,22,199,252,96,100,16,193,117,111,102,75,70,2,114,214,196,29,26,43,189,183,0,194,217,204), string.char(90,15,17,198,223,30,20,138,203,132,6,170,107,40,167,58,194,212,244,49,174,60,209,164,26,87,174,177,41,166,95,173,40,61,47,136,147,239,125,167,146,180,97,167,33,185,5,110,128,36,61,52,140,20,189,16,216,83,164,46,74,16,251,250,72,50,47,5,86,60,56,77,101,64,11,120,124,194,212,199,136,87,157,160,90,172,101,222,235,23,111,11,36,11,68,20,43,251,65,212,206,150,0,50,146,170,183,93,89,142,161,50,110,237,95,191,22,184,62,194,81)), 158) +print(159) assert("38b25a1d9d927ba5e6b294d2aa69c0ab8ab0a0c5" == hmac_sha1(string.char(93,218,103,160,235,71,107,150,18,170,193,9,63,245,77,150,71,242,137,52,243,12,207,183,222,252,20,215,210,194,241,79), string.char(87,97,35,201,134,196,180)), 159) +print(160) assert("a72ce76565c2876e78f86ce9e137a9881328fddc" == hmac_sha1(string.char(48,215,222,182,164,136,31,53,160,28,61,171,220,159,243,154,169,101,191,193,99,28,140,59,60,215,77,170,51,136,50,145,159,135,237,149,83,154,219,223,29,200,85,193,173,201,66,142,100,21,89,144,111,5,24,229,228,154,160,32,210,71,19,63,244,230,61,185,221,158,151,51), string.char(196,237,178,172,24,232,144,251,253,31,8,63,69,58,202,88,120,219,39,34,223,173,196,164,135,93,188,69,126,58,153,37,72,219,50,152,76,235,244,223,205,60,74,12,109,203,134,239,40,181,207,99,118,149,179,84,14,53,189,201,55,110,67,81,116,140,31,247,163,135,53,254,82,230,55,162,255,230,149,144,217,62,164,59,124,59,78,177,115,47,26,169,228,18,167,232,89,161,56,105,127,111,230,93,230,181,222,212,254,85,32,117,140,80,246,117,21,140,221,198,11,196,112,69,122,125,156)), 160) +print(161) assert("cd324faf8204be01296bea85a22d991533fd353e" == hmac_sha1(string.char(25,136,140,2,222,149,164,20,148,15,90,33,106,111,10,252,67,120,57,49,251,171,99,173,114,16,102,240,206,188,231,174,51,124,38,1,217,142,4,15,78,4,5,128,207,82), string.char(84,0,42,233,157,150,105,1,216,32,170,11,100,98,103,220,137,102,32,185,55,211,207,179,252,190,248,117,253,189,196,71,112,32,4,198,87,5,133,125,238,107,210,227,149,206,12,124,31,59,213,66,105,100,240,237,149,232,174,140,127,9,65,204,162,243,100,120,6,229,71,56,140,128,228,102,121,14,43,166,252,230,172,93,96,25,214,174,151,2,50,64,152,164,132,115,109,176,25,144,171,252,231,72)), 161) +print(162) assert("fb6e40ddbf3df49d4b44ccecf9e9bc74567df49e" == hmac_sha1(string.char(31,41,38,118,207,197,83,231,45,187,105,1,246,6,34,16,214,148,97,64,139,27,73,129,71,129,183,182,228,12,74,242,94,43,121,163,188,242,92,43,154,103,20,208,89,213,63,46,81,60,69,217,238,237,97,18), string.char(126,99,64,121,143,219,76,227,119,32,135,246,48,55,214,38,179,255,33,43,32,140,228,44,218,40,187,117,172,131,225,222,72,61,213,132,167,121,190,167,66,213,199,59,212,61,69,242,46,169,89,191,74,0,228,208,46,112,7,81,88,203,95,180,179,75,116,222,115,239,1,132,178,73,139,252,77,1,151,222,108,84,196,161,79,220,80,44,223,44,131,252,58,28,201,3,77,211,33,208,237,47,251,79,134,223,48,8,0,236,139,11,232,186,188,134,249,29,208,154,137,169,218,228,203,254,106,88,35,252,155,111,119,14,147,161,19,60,145,157,120,236,108,122,218,168,105,59,10,44,99,208,86)), 162) +print(163) assert("373f21bf8fe4e855f882cc976ebed31717f4c791" == hmac_sha1(string.char(198,41,45,129,159,48,37,183,170,144,24,223,108,176,68,60,197,245,254,165,24,152,14,25,243,46,6,25,142,99,64,10,145,229,74,232,162,201,72,215,116,26,179,127,107,45,193,232,96,170,168,153,79,47), string.char(79,250,224,177,254,56,111,120,135,79,55,200,199,71,65,132,222,7,106,170,26,179,235,237,47,172,88,28,244,88,137,177,92,178,147,129,147,85,88,157,30,207,235,246,132,98,232,18,201,57,151,90,174,148,126,22,38,118,133,49,83,156,56,195,10,95,180,250,215,220,251,5,131,243,70,2,162,239,197,153,196,28,181,167,26,14,247,86,244,69,190,100,255,158,217,222,58,116,126,245,240,139,255,213,7,28,222,99,12,127,57,2,212,33,136,220,203,251,87,205,213,160,146,162,73,55,112,203,60,243,139,167,45,91,68,62,204,245,206,221,52,253,5,193,69,19,190,131,64,250,12,127,77,212,53,83,102,212,11,215,253,143,111,201)), 163) +print(164) assert("df709285c8a1917aaa6d570bd1d4225ca916b110" == hmac_sha1(string.char(193,124,34,185,160,71,134,56,178,152,165,219,223,89,174,116,11,237), string.char(47,110,24,9,42,187,179,71,114,43,155,129,158,24,130,32,208,3,46,63,16,1,192,210,72,220,200,89,120,80,82,65,199,119,167,86,57,33,105,118,215,60,18,155,17,154,63,59,189,153,236,78,219,89,4,116,208,122,56,65,253,220,57,147,162,242,193,86,45,145,151,61,30,138,105,139,99,89)), 164) +print(165) assert("66be81c24c87feeecfd805872c6cde41cb1dd732" == hmac_sha1(string.char(128,186,31,231,128,48,206,237,59), string.char(56,228,42,58,98,45,202,132,30,78,80,52,36,128,7,55,209,148,181,52,185,220,137,85,111,128,220,109,55,70,185,242,253,155,165,193,240,63,144,253,240,147,18,161,7,46,40,148,201,0,127,1,33,145,180,247,90,206,178,96,62,137,130,99,248,248,227,135,213,21,230,40,88,162,106,240,218,93,226,108,9,121,173,70,255,106,137,222,135,206,104,153,171,1,52,156,166,27,98,7,74,180,206,11,193,129,77,194,86,224,175,79,77,95,134,62,58,252,180,100,191,53,28,59,121,123,146,107,121,249,168,51,204,170,141,26,84,126,38,77,203,11,58,123,155,167,60,176,151,7,39,75,217,254,32,110,79,123,188,133,158,178,132,198,169,23,5,104,124,44,206,191,239,139,152,110,207,42)), 165) +print(166) assert("2d1dd80c3f0fc323ca46ebd0f73c628caa03f88b" == hmac_sha1(string.char(142,87,50,47,146,180,251,164,35,75,53,50,101,115,90,97,66,12), string.char(174,244,41,143,173,27,117,79,53,73,0,189,83,91,13,71,117,85,96,32,199,168,244,72,218,249,207,107,244,1,113,203,160,156,89,244,132,5,4,220,254,112,77,156,158,105,180,98,40,99,198,236,134,209,13,237,93,220,177,70,97,76,178,106,60,60,92,248)), 166) +print(167) assert("444ef9725523ad1aac3d1c20f4954cdf1550d706" == hmac_sha1(string.char(242,169,63,243,133,158,118,205,250,154,66,127,178,170,214,241,96,22,173,138,6,189,90,45,108,70,236,108,93,58,54,204,85,241,194,55,55,184), string.char(30,52,200,164,245,80,75)), 167) +print(168) assert("2170bcfb79e4ab2164287a30ee26535681e34505" == hmac_sha1(string.char(121,111,11,221,34,216,169,179,73,247,222,122,96,168,61,168,201,230,139,158,110,154,74,83,118,254,237,255,99,212,46,218,83,69,185,10,249,78,46,76,206,206,137,133,118,57,241,114,228,54,51,68,71,224,188,188,0,119,173,94,120,1,25,13,237,4,181,170,222,92,106,28,9,111,128,137,228,2,110,4,137,171,219,70), string.char(108,95,232,156,70,235,149,211,52,84,86,25,251,127,119,231,109,144,54,177,96,118,213,3,8,119,95,192,187,221,115,83,67,39,207,82,215,85,156,198,179,246,177,202,115,103,79,97,222,133,113,201,27,92,185,48,227,223,142,96,143,181,175,238,115,112,29,30,126,59,109,252,136,153,28,112,50,138,155,249,223,114,93,107,122,114,163,226,53,219,44,15,172,233,76,98,175,107,246,181,249,112,230,173,152,211,183,120,227,165,187,10,240,94)), 168) +print(169) assert("2bcd1eb6df83aed8bcdfbb36474651f466b1fb22" == hmac_sha1(string.char(178,135,218,237,192,188,60,157,46,65,76,100,66,248,152,23,200,194,67,107,95,127,67,111,149,64,60,158,199,115,159,64,45,192,212,179,46,13,171,104,139,49,185,254,235,183,65,52,140,30,89), string.char(106,32,155,133,229,130,33,200,31,51,242)), 169) +print(170) assert("505910401632c487cd9482ecd6ad16928f21d6f4" == hmac_sha1(string.char(170,34,244,62,31,230,158,102,235,63,93,33,111,253,232,211,132,160,120,156,107), string.char(43,16,194,149,208,76,252,14,73,11,1,172,79,41,132,217,125,96,221,110,199,248,129,122,45,63,77,145,21,98,40,149,154,9,127,27,168,224,204,167,203,74,218,196,236,230,47,29,122,138,65,239,123,120,29,68,236,137,130,95,114,230,185,146,32,69,201,48,251,233,103,61,132,167,245,42,68,249,145,166,137,204,186)), 170) +print(171) assert("6d5f9ae7e8a51f2e615ae3aa8525a41b0bf77282" == hmac_sha1(string.char(166,8,119,187,177,166,23,183,147,37,177,162,102,16,93,204,247,119,248,179,23,89,48,145,188,37,197,176,238,218,173,6,216,229,224,108,58,78,40,199,9,241,191,154,88,124,237,142,228,7,235,102,142,123,4,124,38,46,170,229,116,243,104,106,26,163,94,2,118,71,173,171,104), string.char(41,12,123,197,199,117,129,177,132,149,175,83,168,79,76,58,236,204,99,121,155,207,228,89,236,154,103,137,183,221,208,93,22,222,28,237,140,21,25,149,188,111,6,85,251,31,73,79,239,246,66,46,215,157,184,5,207,4)), 171) +print(172) assert("8c20120cd131e07d9fa46467602d57c9017ca22d" == hmac_sha1(string.char(12,24,169,75,70,190,156,115,112,233,245,216,85,248,62,24,91,179,204,185,0,129,209,137,116,97,242,183,35,151,91,170,41,4,81,12,120,47,78,145,193,50,237,224,62,203,171,169,6,141,126,75,29,40), string.char(47,165,0,107,170,77,37,219,45,135,102,68,144,218,213,74,35,58,246,179,171,3,148,55,216,32,29,102,83,58,29,11,166,75,243,60,21,9,202,179,236,129,212,62,217,203,6,193,18,176,156,198,86,140,135,222,109,48,195,112,167,218,92,76,71,40,128,221,235,43,81,109,198,51,77,122,183,111,173,195,8,249,40,159,212,136,180,215,171,70,60,108,26,185,11,222,67,47,142,179,158,131,135,153,168,251,199,21,44,7,164,68,212,209,54,75,18,200,117,213,16,175,152,29,146,154,151,236,143,173,86,70,224,138,71,33,33,151,122,205,128,223,166,51,149,125,178,225,234,164,180)), 172) +print(173) assert("1ef2a94a2e620a164e07590f006c5a46b722fe7e" == hmac_sha1(string.char(78,109,133,245,198,165,112,63,135,53,124,251,110,183,41,34,153,68,22,253,66,201,9,41,15,99,105,14,252,172,212,39,100,14,128,16,64,47,104,37,33,27,203,120,163,182,52,37,27,224,212,40,134,81,70,29,155,101,246,141,238,41,253,124,222,216,49,49,85,46,51,51,122,39,221,156,104,7,14,52,71,93,31,219), string.char(191,94,78,159,209,12,118,97,214,190,238,108,175,252,192,244,8,104,145,30,236,242,45,49,215,185,251,73,46,101,156,221,136,245,45,240,138,174,187,151,0,122,113,154,195,94,245,186,218,211,203,189,37,251,21,33,225,66,168,152,102,200,245,101,102,217,193,215,194,214,48,131,153,140,75,100,237,240,32,89,113,17,125,177,189,188,212,101,211,212,102,57,251,213,216,14,86,204,198,147,122,97,203,127,100,107,126,107,116,34,220,122,154,130,135,199,9,243,9,194,214)), 173) +print(174) assert("ad924bedf84061c998029fb2f168877f0b3939bb" == hmac_sha1(string.char(174,37,249,22,190,230,152,74,215,14,1,180,201,164,238,160,59,157,213,35,36,43,116,160,158,144,131,30,213,232,29,183,205,87,35,85,252,120,126,5,54,113,176), string.char(25,233,197,224,170,150,207,83,58,255,63,236,20,13,179,143,48,248,212,16,220,143,14,123,207,59,28,124,19)), 174) +print(175) assert("75a6cfbedde0f1b196209a282b25f5f8b9fef3d1" == hmac_sha1(string.char(131,192,35,70,146,214,224,190,220,240,195,81,129,33,207,253,47,230,111,169,187,136,52,244,217,178,143,140,225,154,105,95,112,201,136,90,221,255,44,31,48,178,90,178,218,122,228,165,90,189,107,45,217,249,89,213,136,139,91,187,202,204,26,250,112), string.char(41,13,202,37,1,104,116,166,245,50,221,59,115,68,187,115,80,93,202,147,10,96,209,213,76,103,143,237,217,21,124,152,82,54,147,207,164,43,89,83,118,67,43,179,79,64,127,252,9,26,125,101,80,192,111,224,104,243,45,67,54,251,238,146,154,212,193,181,138,168,231,171,203,62,93,97,46,55,109,253,214,79,60,62,126,183,235,63,121,46,99,3,223,174,223,2,208,78,7,15,161,56,160,59,205,122,138,77,47,250,107,136,65,199,98,86,131,217,96,226,11,166,185,131,231,76,28,83,140,7,146,87,158,20,102,224,86,5,232,96,34,129,172,236,146,1,139,63,252,1,48,196,242,41,145,97,254,174,88,107,169,70,158,165,246,160,4,16,212,109,236)), 175) +print(176) assert("c6f51cc53b0a341dafa4607ee834cffaa8c7c2a2" == hmac_sha1(string.char(85,193,15,80,156,124,171,95,40,75,229,181,147,237,153,83,232,67,210,131,57,69,7,226,226,195,164,46,185,83,120,177,67,77,119,201,9,123,117,111,148,176,12,145,14,80,225,95,71,136,179,94,136,6,78,230,149,135,176,131,19,125,185,48,200,244,111,8,28,170,82,121,242,200,208,145,73,59,234,19,87,61,79,137,245,148,212,94,96,253,227,60,162,170,49,102), string.char(200,235,229,185,225,227,209,209,199,11,112,58,19,202,246,237,94,60,90,176,145,87,191,138,171,88,242,239,21,146,3,92,255,188,99,146,74,134,252,19,201,111,239,76,249,231,71,109,230,240,192,234,253,52,58,173,153,24,131,161,97,222,0,203,177,179,186,160,46,206,73,176,154,39,248,22,66,164,232,120,188,5,73,245,68,8,40,157,155,93,50,207)), 176) +print(177) assert("a07ef6f8799ef46062ffea5a43bb3edd30c1fdde" == hmac_sha1(string.char(81,90,37,124,211,22,19,34,50,56,90,45,17,230,233,38,76,24,250), string.char(142,185,46,191,103,207,110,48,71,10,217,115,193,52,131,87,106,39,9,80,216,15,235,169,30,143,3,7,188,78,34,136,18,200,181,140,22,253,141,59,235,25,59,204,147,80,162,108,237,101,167,249,7,168,24,123,215,220,198,38,133,253,72,168,10,139,84,22,195,36,241,128,70,132,28,242,56,40,159,113,184,187,89,38,198,255,176,88,112,125,68,12,57,207,3,251,72,198,135,48,118,244,199,192,205,164,181,243,40,33,195,204,9,78,108,253,114,211,173,121,245,104,13,116,143,160,241,139,210,162,78,62,69,232,40,248,254,177,6,148,177,168,51,253,177,84,152,29,107,91,186,68,118,30,125,62,196,24,14,87,158,83,39,240,192,79,78,61,133,109,106,98,120,232,144,18,49,96,17,202,208,189,152,171,219,19,41,132,179,219,83,220,201,36,191)), 177) +print(178) assert("63585793821f635534879a8576194f385f4a551a" == hmac_sha1(string.char(78,213,189,105,102,116,246,215), string.char(162,53,102,158,78,125,120,189,186,214,237,16,219,220,44,24,30,62,32,177,104,217,225,27,92,128,95,202,50,177,239,152,151,161,31,152,93,108,29,125,23,63,55,243,160,169,99,102,15,188,196,129,217,239,132,180,177,251,132,173,62,172,21,139,16,137,231,6,243,185,218,60,60,156,166,153,99,149,144,192,151,249,141,165,222,254,4,111,58,46,106,78,160,215,49,128,252,4,114,236,132,108,114,189,143,45,42,84,134,81,47,140,247,146,247,179,63,14,92,136,120,139)), 178) +print(179) assert("95bb1229d26df63839ba4846a41505d48ff98290" == hmac_sha1(string.char(121,190,21,20,39,101,53,249,44,132,19,132,4,114,199,22,32,143,38,33,184,181,66,55,183,240,31,204,225,230,125,186,87,25,90,229,11,55,239,62,101,67,238,1,104,178,191,144,148,105,6,26,127,154,135,247,248,171,41,44,57,83,186,220,42,40,68,153,189,221), string.char(99,150,114,100,37,53,229,202,20,171,246,91,188,188,46,232,70,26,151,35,223,28,97,132,9,242,55,238,98,75,108,91,226,48,236,209,133,92,68,194,241,199,188,86,59,255,230,128,252,193,94,113,134,27,32,50,191,176,159,185,89,209,255,192,9,214,252,63,147,8,92,162,114,72,50,101,136,180,95,39,55,143,137,118,40,14,104,3,149,153,230,135,98,93,72,180,72,86,185,80,107,69,68,20,73)), 179) +print(180) assert("9e7c9a258a32e6b3667549cef7a61f307f49b4b9" == hmac_sha1(string.char(105,200,69,23,156,169,151,107,30,139,196,145,128,108,52,7,181,248,255,83,176,169,152,51,158,236,49,191,65,208,155,204,105,179,148,226,142,194,63,253,217,184,69,11,135,185,203,150,146,57,129,80,87,84,231,168,63,186,149,85,128,236,135,217,246,255,121,70,251,68,139,215,199,143,87,13,196,20,50,92,55,116,34,141,117,89,228,94,63,142,250,182,58,91,68,214), string.char(38,52,86,63,109,91,84,11,33,250,194,192,178,94,37,17,65,111,173,17,37,54,163,168,142,91,191,197,161,229,44,163,163,125,81,204,167,185,244,119,75,28,211,87,159)), 180) +print(181) assert("7fb01c88e7e519265c18e714efd38bc66f831071" == hmac_sha1(string.char(138,214,212,108,83,152,165,123), string.char(148,223,186,162,237,166,230,68,59,78,220,164,231,42,46,211,239,38,39,150,174,50,58,4,0,135,87,123,123,5,33,252,197,6,53,83,27,178,189,228,166,252,91,141,67,44,68,106,222,17,46,84,214,252,110,181,216,132,14,246,209,57,183,155,238,64,215,121,8,169,157,175,182,191,169,94,116,105,91,49,203,30,155,202,39,140,146,6,115,162,112,130,175,86,143,128,126,249,148,185,39,174,63,83,19,76,164,34,228,97,198,250,65,3,168,158,61,130,167,166,144,185,146,184,160,39,189,11,243,125,255,60,217,46)), 181) +print(182) assert("15ab3da2a97592c5f2e22586b4c8a8653411e756" == hmac_sha1(string.char(6,242,91,101,37,118,105,53,170,56,114,144,117,49,53,203,169,216,9,232,9,170,204,129,82,41,210,45,86,176,139,80,152,168,1,154,32,111,89,165,143,205,21,236,105,62,231,106,232,205,7,189,245,52,145,100,78,140,200,183,209,232,196,88,109,175,70,153,150,231,252,7,189,119,97,176,216,201,186,245,24,128,33,57,113,188,184,0,146,248,69,255,77,144,101), string.char(186,246,72,178,189,127,22,42,22,217,81,156,253,142,152,86,42,41,164,77,150,11,208,155,154,185,18,81,156,185,140,224,215,98,1,97,33,194,137,206,144,219,207,210,52,203,198,1,109,182,65,138,122,227,168,207,213,110,206,102,12,181,198,195,133,218,23,187,117,190,56,140,154,239,105,62,96,148,126,187,47,24,139,194,18,211,225,252,195,49,102,138,165,160,90,153,100,140,105,154,242,59,133,226,19,68,125,59,83,140,48,240,226,129,151,79,130,59,96,111,27,73,198)), 182) +print(183) assert("7b06792805f4e8062ee9dfbbaffccd787c6f98e1" == hmac_sha1(string.char(83,132,152,16,245,136,91,241,37,158,80,92,65,223,79,3,189,213,89,253,159,93,194,52,218), string.char(212,242,37,98,229,190,238,23,210,197,147,253,82,105,146,168,198,253,2,160,165,188,165,221,179,112,136,72,149,79,138,70,101,95,210,73,157,14,211,92,27,230,177,84,170,183,15,40,0,92,222,252,166,48,1,139,40,16,186,178,231,109,100,125,253,125,123,94,115,94,150,157,186,230,115,163,194,164,30,131,162,90,28,61,44,248,137,8,246,173,31,177,236,39,8,10,192,148,211,36,215,57)), 183) +print(184) assert("a28fe22ba0845ae3d57273febbbf1d13e8fde928" == hmac_sha1(string.char(149), string.char(11,139,249,120,214,252,67,75)), 184) +print(185) assert("87a9daa85402f6c4b02f1e9fd6edd7e5d178bb88" == hmac_sha1(string.char(114,92,75,169,228,153,182,206,125,139,162,232,77,38,72,129,132,81,11,153,91,92,152,253,10,79,138,142,17,191,161,194,147,213,43,214,39,32,146,46,132,40,77,192,82,54,55,239,50,47), string.char(114,8,110,153,77,155,178,113,203,30,21,41,136,111,176,255,74,214,117,12,189,194,198,37,73,152,196,108,207,188,187,31,227,237,17,1,66,200,73,83,31,61,161,61,31,149,1,152,140,202,163,175,140,136,51,59,121,28,125,192,238,96,192,65,33,177,136,135,173,13,52,101,42,55,189,201,157,244,104,235,148,114,84,154,10,99,153,41,208,213,41,83,193,129,216,46,4,226,81,184,1,249,70,93,183,173,9,29,57,75,209,67,227,49,253,132,129,13,157,55,66,191,136,67,95,108,155,116,17,125,151,217,242,204,110,143,189,201,118)), 185) +print(186) assert("6300b2f6236b83de0c84ff1816d246231a5d3b79" == hmac_sha1(string.char(215,10,19,7,73,236,63,23,34,49,11,240,186,195,32,171,67,85,7,95,190,237,77,216,115,36,230,204,139,95,229,37,201,115,81,96,217,205,49,22,169,32,90,2,90,93,137,247,83,229,39,88,235,175,191,246,80,40,228,114,188,115,14,252,35,40,130,240,20,25,202,80,184,27,227,175,149,64,110,223,25,64,57,189,153,176,143,30,76,2,68), string.char(13,245,250,139,223,128,3,6,189,59,12,139,38,153,215,76,250,198,91,117,242,118,34,26,133,244,143,5,56,182,213,167,144,71,250,40,202,81,22,14,72,201,143,10,177,4,166,75,160,156,100,5,147,138,104,98,9,125,81,202,44,120,241,221,181,0,169,155,96,1,4,205,145,105,150,122,135,231,42,165,179,83,39,122,159,193,139,28,4,201,178,41,168,11,177,61,39,14,196,60,29,131,201,91,46,35,128,63,167,48,132,134,105,224,246,244,81,144,209,123,222,190,167,138,76,194,42,152,23,125,52,156,18,217,101,207,239,63,235,3,3,49,147,172,158,97,179,86,233,85,155,245,206,205,165,195,180,223,20,62,197,143,149,126,152)), 186) +print(187) assert("35fe4e0cf2417a783d5bdbc17bbc0ab77d2699e7" == hmac_sha1(string.char(15,157,117,128,204,42,12,183,229,129,132,234,228,150,235,100,100,77,160,26,154,230,246,138,150,120,252,20,188,138,171,189,208,62,201,58,158,152,167,165,98,249,52,143,7,26,109,227,144,81,213,157,31,155,26,12,206,103,193,29,142,126,205,123,83,111,179,166,31,32,183,183,100,54,51,205,180,186,160,220,176,233,42,94,62,241,88,74,31,24,22,115,179,124,136,206,78,83), string.char(248,157,191,249,76,86,187,243,10,116,1,227,141,21,104,154,167,72,30,245,6,57,40,170,253,15,4,192,237,237,142,141,196,243,231,245,121,219,150,237,212,118,78,25,201,249,174,21,76,98,187,155,82,243,218,142,239,101,235,240,134,70,212,205,136,165,170,71,121,137,44,8,110,14,167,162,233,92,51,191,178,227,85,134,168,222,121,62,35,127,57,92,80,142,91,112,9,248,129,127,236,2,190,165,125,236,188,117,158,241,181,134,54,133,16,64,100,156,59,83,249,105,235,187,160,186,158,173,52,25,129,20,156,209,102,170,209,10,3,233,12,228,205,138,16,140,108,74,75,65,238,155,251,129,73,236,118,93,202,217,46,180,165,27,84,120,61,186,173,148,131,161,187,30,175,21,249,12,245,145,89,93,61,151,254,29,247,51,198,238,111,52,30,254)), 187) +print(188) assert("83c2a380e55ecbc6190b9817bb291a00c36de5e8" == hmac_sha1(string.char(61,128,92,30,94,101,187,67,197,177,98,100,191,221,190,196,86,62,8,238,225,3,93,127,28,126,217,206,56,192,90,9,74,34,43,228,190,162,57,99,170,238,230,196,99,213,31,6,184,11,79,82,100,68,255,40,63), string.char(78,173,248,128,215,226,77,57,233,135,163,11,241,74,59,78,169,219,125,37,182,56,139,89,32,52,80,165,233,52,179,54,216,45,205,209,173,33,12,148,231,66,126,155,191,91,155,174,23,15,14,28,77,186,139,113,48,141,153,177,252,194,23,61,181,189,66,151,45,11,119,74,236,107,206,244,41,117,161,182,233,138,57,196,90,118,28,99,186,150,54,180,105,230,2,41,246,19,80,211,173,102,72,78,82,253,5,183,248,101,173,204,2,145,105,4,160,44,110,211,63,45,46,78)), 188) +print(189) assert("bd59d0dcf812107a613a2d892322f532c16ec210" == hmac_sha1(string.char(161,183,101,160,169,208,82,58,88,198,190,85,53,240,56,6,20,136,86,18,216,63,190), string.char(39,227,142,194,37,34,121,168,239,99,70,39,179,128,26,113,47,95,70,129,255,219,63,233,44,33,19,127,22,24,208,50,19,141,185,19,54,82,147,138,44,69)), 189) +print(190) assert("6aa92cb64b2e390fbf04dc7edfe3af068109ffba" == hmac_sha1(string.char(202,230,55,100,136,80,156,240,98,73,97,72,191,195,185,105,150,97,148,33,167,140,212,100,247,154,5,152,117,24,128,221,150,49,143,86,12,211,161,75,177,75,145,46,112,143,37,181,84,50,230,81), string.char(131,200,0,161,117,217,5,10,64,79,178,147,64,248,35,147,135,103,157,181,194,123,76,13,150,118,204,63,32,203,155,222,227,148,31,28,218,83,245,17,208,200,104,78,85,31,114,194,67,159,184,167,42,199,241,6,182,195,3,79,22,236,124)), 190) +print(191) assert("a42cf08fa6d8ad6ae00251aeed0357dca80645c7" == hmac_sha1(string.char(170,52,245,172,122,225,164,248,23,35,54,243,118,35,165,16,6), string.char(245,115,59,162,179,146,39,213,64,240,80,108,190,127,116,16,154,211,33,171,150,46,77,213,99,88,160,101,236,202,70,26,61,83,79,110,127,74,97,34,18,243,140,34,244,249,225,88,132,102,7,99,220,140,46,111,96,48,93,44,47,186,86,246,244,127,236,150,35,69,111,238,54,24,71,157,254,21,2,194,69,47,190,87,72,235,60,243,40,153,111,55,220,25,91,17,156,195,57,207,107,145,6,82,104,232,209)), 191) +print(192) assert("8dd62dcf68ef2d193005921161341839b8cac487" == hmac_sha1(string.char(136,5,195,241,18,208,11,46,252,182,96,183,164,87,248,13), string.char(78,49,162,9,47,79,26,139,66,194,248,90,171,207,167,43,201,223,102,192,49,145,83,42,1,52,250,116,216,133,224,224,17,143,41,153,65,214,231,109,160,5,31,85,17,186,199,226,214,92,21,194,148,98,68,209,50,74,26,150,97,59,223,8,131,173,145,6,31,126,248,86,226,2,94,15,3,202,239,99,177,237,61,99,14,253,26,246,0,151,2,7,159,40,249,166)), 192) +print(193) assert("eff66e7a69ae7d2a90b2d80c64ce7c41fe560a19" == hmac_sha1(string.char(123,54,89,205,103,116,21,97,164,41,23,224,151,195,22,196,90,52,253,92), string.char(141,12,168,171,49,30,165,57,107,214,157,224,36,163,182,184,103,186,133,22,2,215,52,220,95,205,231,180,221,139,67,184,21,46,172,95,183,220,31,27,227,122,183,196,114,206,132,162,51,164,84,212,172,63,218,236,127,215,218,77,119,105,14,19,164,53,149,11,13,1,29,246,69,200,208,239,154,60,245,31,172,82,158,165,197,250,151,83,12,156,215,201,66,45,238,228,77,125,157,35,235,162,78,220,132,14,137,168,238,127,10,24,41,132,181,43,95,73,33,32,129,149,145,55,80,156,171,73,90,183,166,182,163,154,31,104,14,56,59,102,72,210,121,191,251,239,1,5,76,116,158,89,40,69,220,107,23,168,106,93,92,95,21,91,118,118,71,29,64,142,84,15,153,193,47,214,30,117,236,217)), 193) +print(194) assert("0f1adc518afcca2ed7ad0adaaedd544835ddb76e" == hmac_sha1(string.char(142,36,199,16,52,54,62,146,153,25,162,85,251,247), string.char(110,70,53,237,120,53,50,196,24,82,199,210,156,178,110,124,7,24,229,89,39,1,10,96,74,211,36,79,166,4,33,238,64,220,239,129,48,232,244,172,142,79,154,73,36,107,182,208,191,25,201,19,233,23,60,236,180,76,116,53,232,181,101,62,160,252,213,171,166,113,193,73,75,13,209,63,76,92,95,176,119,0,125,174,42,138,77,23,212,81,180,10,193,118,244,242,163,246,206,150,162,92,20,208,51,71,254,197,11,222,203,153,208,251,243,193,124,141,215,171,55,244,230,78,158,32,223,138,156,43,98,39,191,108,111,6,117,130,195,100,221,233,17,98,180,49,132,139,171,20,180,7,206,35,54,17,48,253,130,185,249,2,52,17,207,73,24,33)), 194) +print(195) assert("5e87f8d9a653312fd7b070a33a5d9e67b28b9a84" == hmac_sha1(string.char(255,237,33,255,4,170,95,90,88,77,217,10,94,118,134,20,33,208,17,136,77,18,169,205,112,36,203,231,67,47,186,91,7,17,29), string.char(97,211,98,53,135,238,42,101,144,12,75,185,119,223,114,81,201,94,21,118,16,152,74,215,56,61,131,151,83,96,83,59,49,199,255,207,153,90,231,39,181,4,59,9,2,150,207,124,209,120,37,236,195,66,189,209,137,9,165,78,172,142,209,243,237,145,204,210,222,153,52,127,39,174,133,17,186,219,87,142,209,212,223,60,249,57,244,251,44,254,73,238,52,99,94,237,189,185,237,1,101,166,220,170,103,209)), 195) +print(196) assert("837c7cc92e0d2c725b1d1d2f02ef787be37896fa" == hmac_sha1(string.char(150,241,64,89,79,72,88,157,113,139,190,43,211,214,202,52,124,91,111,198,47,161,120,17,120,157,112,248,245,154,254,25,233,96,216), string.char(214,251,73,193,181,158,253,28,23,48,186,168,162,26,124,103,22,195,165,63,189,196,162,229,69,1,208,85,249,24,73,101,243,149,68,236,124,147,108,67,37,75,202,143,57,124,71,176,27,216,208,183,164,173,219,170,79,10,198,1,232,35,9,19,70,131,211,60,37,94,146,98,92,31,150,95,89,189,46,38,156,118,171,137,86,87,32,173,77,14,135,233,233,114,162,136,57,113,73,115,134,87,184)), 196) +print(197) assert("64534553f76e55b890101380ad9149d3cacb5b76" == hmac_sha1(string.char(231,24,192,54,17,14,98,122,119,170,71,54,206,207,223,109,164,181,124,186,122,111,122,34,157,24,237,130,207,206,117,76,183,162,174,124,60,60,77,114,139,170,194,143,232,74,160,141,161,171,51,178,162,159,234,208,55), string.char(141,184,241,185,112,45,53,192,72,152,139,145,146,149,145,151,12,140,111,79,158,172,212,175,75,225,127,4,252,104,5,94,105,24,141,183,79,208,240,227,63,126,213,4,13,109,216,244,237,24,196,52,212,245,147,109,18,63,69,107,122,245,101,67,212,232,92,25,135,67,127,247,187,143,224,198,105,38,147,12,238,6,57,53,105,246,0,127,222,69,163,44,11,245,82,220,32,108,33,70,93,177,89,75,192,214,104,236,84,28,221,209,27,12,80,239,111,249,57,39,236,193,17,69,66,180,63,193,242,248,72,22,225,56,198,235,111,102,33,171,159,124,220,143,108,10,71,186,219,213,28,209,145,188,36,70,59,9,235,154,57,12,211,146,6,87,83)), 197) +print(198) assert("b7efbbc21ac1a746e22368e814ef5921056331ac" == hmac_sha1(string.char(137,153,151,252,88,36,165,92,194,50,19,117), string.char(155,113,35,47,22,52,144,77,130,20,178,133,75,207,168,146,132,209,160,7,123,190,117,196,147,212,142,25,182,222,56,249,192,228,6,224,250,221,89,7,176,27,37,49,215,192,74,132,127,101,32,23,34,131,23,74,37,226,208,205,162,242,102)), 198) +print(199) assert("950ad3222f4917f868d09feab237a909fb6d50b7" == hmac_sha1(string.char(78,46,85,132,231,4,243,255,22,45,240,155,151,119,94,213,50,111,10,83,40,204,49,52,17,69,132,44,213,83,54,251,211,159,123,55,17,58,162,170,210,3,35,237,165,181,217,27,7,249,158,22,158,207,77,121,37,63,37,39,204,68,99,158,78,175,73,183,47,99,134,65,74,234,154,33,14,117,126,98,167,242,106,112,145,82), string.char(144,133,184,16,9,8,227,98,190,60,141,255,87,69,63,214,12,67,14,206,32,120,59,232,176,82,32,194,115,52,148,143,126,86,82,101,167,249,17,169,9,105,228)), 199) + +skynet.start(skynet.exit) \ No newline at end of file From a77cec2aea7d7f3c345eb26e84c9eeefdd8d9ae0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 29 Aug 2014 12:07:09 +0800 Subject: [PATCH 173/729] add example injectlaunch to show how to inject code --- examples/injectlaunch.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/injectlaunch.lua diff --git a/examples/injectlaunch.lua b/examples/injectlaunch.lua new file mode 100644 index 00000000..4a0028c0 --- /dev/null +++ b/examples/injectlaunch.lua @@ -0,0 +1,19 @@ +if not _P then + print[[ +This file is examples to show how to inject code into lua service. +It is used to inject into launcher service to change the command.LAUNCH to command.LOGLAUNCH. +telnet the debug_console service (nc 127.0.0.1 8000), and run: +inject 3 examples/injectlaunch.lua -- 3 means launcher service +]] + return +end +local command = _P.lua.command + +if command.RAWLAUNCH then + command.LAUNCH, command.RAWLAUNCH = command.RAWLAUNCH + print "restore command.LAUNCH" +else + command.RAWLAUNCH = command.LAUNCH + command.LAUNCH = command.LOGLAUNCH + print "replace command.LAUNCH" +end From 8b7ba7bdbeebe365bceede5c836aab775c62a4b9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 1 Sep 2014 11:17:16 +0800 Subject: [PATCH 174/729] add testresponse --- HISTORY.md | 4 ++++ test/testresponse.lua | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test/testresponse.lua diff --git a/HISTORY.md b/HISTORY.md index f3da837b..5098f75c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +v0.6.2 (2014-9-1) +----------- +* bugfix: only skynet.call response PTYPE_ERROR + v0.6.1 (2014-8-25) ----------- * bugfix: datacenter.wakeup diff --git a/test/testresponse.lua b/test/testresponse.lua new file mode 100644 index 00000000..aabbbaba --- /dev/null +++ b/test/testresponse.lua @@ -0,0 +1,44 @@ +local skynet = require "skynet" + +local mode = ... + +if mode == "TICK" then +-- this service whould response the request every 1s. + +local response_queue = {} + +local function response() + while true do + skynet.sleep(100) -- sleep 1s + for k,v in ipairs(response_queue) do + v(true, skynet.now()) -- true means succ, false means error + response_queue[k] = nil + end + end +end + +skynet.start(function() + skynet.fork(response) + skynet.dispatch("lua", function() + table.insert(response_queue, skynet.response()) + end) +end) + +else + +local function request(tick, i) + print(i, "call", skynet.now()) + print(i, "response", skynet.call(tick, "lua")) + print(i, "end", skynet.now()) +end + +skynet.start(function() + local tick = skynet.newservice(SERVICE_NAME, "TICK") + + for i=1,5 do + skynet.fork(request, tick, i) + skynet.sleep(10) + end +end) + +end \ No newline at end of file From 3b23f3d27b9df4cc5aa81667e8166d38ebf192fe Mon Sep 17 00:00:00 2001 From: allon Date: Tue, 2 Sep 2014 11:31:59 +0800 Subject: [PATCH 175/729] Ignore DS_Store file for Mac OS X users --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f40078bf..56919127 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /luaclib *.so *.dSYM +.DS_Store From 7f6767b298612da0d8ae4da708f77a0f77601264 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 6 Sep 2014 12:08:45 +0800 Subject: [PATCH 176/729] remove duplicated line to fix issue #166 --- service/clusterproxy.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index 6edcb674..ad81bc3d 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -21,7 +21,6 @@ skynet.forward_type( forward_map ,function() address = n end skynet.dispatch("system", function (session, source, msg, sz) - local m,s = skynet.rawcall(clusterd, "lua", skynet.pack("req", node, address, msg, sz)) skynet.ret(skynet.rawcall(clusterd, "lua", skynet.pack("req", node, address, msg, sz))) end) end) From db9ab88a6c607cc902d384d83896d9dc05d6d676 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 7 Sep 2014 17:42:10 +0800 Subject: [PATCH 177/729] use sproto instead of cjson --- 3rd/lpeg/HISTORY | 90 + 3rd/lpeg/lpcap.c | 537 +++ 3rd/lpeg/lpcap.h | 43 + 3rd/lpeg/lpcode.c | 963 +++++ 3rd/lpeg/lpcode.h | 34 + 3rd/lpeg/lpeg-128.gif | Bin 0 -> 4923 bytes 3rd/lpeg/lpeg.html | 1429 +++++++ 3rd/lpeg/lpprint.c | 244 ++ 3rd/lpeg/lpprint.h | 35 + 3rd/lpeg/lptree.c | 1232 ++++++ 3rd/lpeg/lptree.h | 77 + 3rd/lpeg/lptypes.h | 147 + 3rd/lpeg/lpvm.c | 355 ++ 3rd/lpeg/lpvm.h | 63 + 3rd/lpeg/makefile | 55 + 3rd/lpeg/re.html | 498 +++ 3rd/lpeg/re.lua | 259 ++ 3rd/lpeg/test.lua | 1386 +++++++ 3rd/lua-cjson/CMakeLists.txt | 76 - 3rd/lua-cjson/LICENSE | 20 - 3rd/lua-cjson/Makefile | 119 - 3rd/lua-cjson/NEWS | 44 - 3rd/lua-cjson/THANKS | 9 - 3rd/lua-cjson/dtoa.c | 4358 ---------------------- 3rd/lua-cjson/dtoa_config.h | 74 - 3rd/lua-cjson/fpconv.c | 205 - 3rd/lua-cjson/fpconv.h | 22 - 3rd/lua-cjson/g_fmt.c | 111 - 3rd/lua-cjson/lua-cjson-2.1.0-1.rockspec | 56 - 3rd/lua-cjson/lua-cjson.spec | 80 - 3rd/lua-cjson/lua/cjson/util.lua | 271 -- 3rd/lua-cjson/lua/json2lua.lua | 14 - 3rd/lua-cjson/lua/lua2json.lua | 20 - 3rd/lua-cjson/lua_cjson.c | 1425 ------- 3rd/lua-cjson/manual.html | 1332 ------- 3rd/lua-cjson/manual.txt | 611 --- 3rd/lua-cjson/performance.html | 617 --- 3rd/lua-cjson/performance.txt | 89 - 3rd/lua-cjson/rfc4627.txt | 563 --- 3rd/lua-cjson/runtests.sh | 92 - 3rd/lua-cjson/strbuf.c | 251 -- 3rd/lua-cjson/strbuf.h | 154 - 3rd/lua-cjson/tests/README | 4 - 3rd/lua-cjson/tests/bench.lua | 131 - 3rd/lua-cjson/tests/example1.json | 22 - 3rd/lua-cjson/tests/example2.json | 11 - 3rd/lua-cjson/tests/example3.json | 26 - 3rd/lua-cjson/tests/example4.json | 88 - 3rd/lua-cjson/tests/example5.json | 27 - 3rd/lua-cjson/tests/genutf8.pl | 23 - 3rd/lua-cjson/tests/numbers.json | 7 - 3rd/lua-cjson/tests/octets-escaped.dat | 1 - 3rd/lua-cjson/tests/rfc-example1.json | 13 - 3rd/lua-cjson/tests/rfc-example2.json | 22 - 3rd/lua-cjson/tests/test.lua | 423 --- 3rd/lua-cjson/tests/types.json | 1 - Makefile | 14 +- examples/agent.lua | 63 +- examples/client.lua | 35 +- examples/proto.lua | 37 + examples/watchdog.lua | 5 +- lualib-src/sproto/README | 1 + lualib-src/sproto/lsproto.c | 455 +++ lualib-src/sproto/msvcint.h | 32 + lualib-src/sproto/sproto.c | 1185 ++++++ lualib-src/sproto/sproto.h | 39 + lualib/jsonpack.lua | 19 - lualib/sproto.lua | 144 + lualib/sprotoparser.lua | 382 ++ 69 files changed, 9805 insertions(+), 11465 deletions(-) create mode 100644 3rd/lpeg/HISTORY create mode 100644 3rd/lpeg/lpcap.c create mode 100644 3rd/lpeg/lpcap.h create mode 100644 3rd/lpeg/lpcode.c create mode 100644 3rd/lpeg/lpcode.h create mode 100644 3rd/lpeg/lpeg-128.gif create mode 100644 3rd/lpeg/lpeg.html create mode 100644 3rd/lpeg/lpprint.c create mode 100644 3rd/lpeg/lpprint.h create mode 100644 3rd/lpeg/lptree.c create mode 100644 3rd/lpeg/lptree.h create mode 100644 3rd/lpeg/lptypes.h create mode 100644 3rd/lpeg/lpvm.c create mode 100644 3rd/lpeg/lpvm.h create mode 100644 3rd/lpeg/makefile create mode 100644 3rd/lpeg/re.html create mode 100644 3rd/lpeg/re.lua create mode 100755 3rd/lpeg/test.lua delete mode 100644 3rd/lua-cjson/CMakeLists.txt delete mode 100644 3rd/lua-cjson/LICENSE delete mode 100644 3rd/lua-cjson/Makefile delete mode 100644 3rd/lua-cjson/NEWS delete mode 100644 3rd/lua-cjson/THANKS delete mode 100644 3rd/lua-cjson/dtoa.c delete mode 100644 3rd/lua-cjson/dtoa_config.h delete mode 100644 3rd/lua-cjson/fpconv.c delete mode 100644 3rd/lua-cjson/fpconv.h delete mode 100644 3rd/lua-cjson/g_fmt.c delete mode 100644 3rd/lua-cjson/lua-cjson-2.1.0-1.rockspec delete mode 100644 3rd/lua-cjson/lua-cjson.spec delete mode 100644 3rd/lua-cjson/lua/cjson/util.lua delete mode 100755 3rd/lua-cjson/lua/json2lua.lua delete mode 100755 3rd/lua-cjson/lua/lua2json.lua delete mode 100644 3rd/lua-cjson/lua_cjson.c delete mode 100644 3rd/lua-cjson/manual.html delete mode 100644 3rd/lua-cjson/manual.txt delete mode 100644 3rd/lua-cjson/performance.html delete mode 100644 3rd/lua-cjson/performance.txt delete mode 100644 3rd/lua-cjson/rfc4627.txt delete mode 100755 3rd/lua-cjson/runtests.sh delete mode 100644 3rd/lua-cjson/strbuf.c delete mode 100644 3rd/lua-cjson/strbuf.h delete mode 100644 3rd/lua-cjson/tests/README delete mode 100755 3rd/lua-cjson/tests/bench.lua delete mode 100644 3rd/lua-cjson/tests/example1.json delete mode 100644 3rd/lua-cjson/tests/example2.json delete mode 100644 3rd/lua-cjson/tests/example3.json delete mode 100644 3rd/lua-cjson/tests/example4.json delete mode 100644 3rd/lua-cjson/tests/example5.json delete mode 100755 3rd/lua-cjson/tests/genutf8.pl delete mode 100644 3rd/lua-cjson/tests/numbers.json delete mode 100644 3rd/lua-cjson/tests/octets-escaped.dat delete mode 100644 3rd/lua-cjson/tests/rfc-example1.json delete mode 100644 3rd/lua-cjson/tests/rfc-example2.json delete mode 100755 3rd/lua-cjson/tests/test.lua delete mode 100644 3rd/lua-cjson/tests/types.json create mode 100644 examples/proto.lua create mode 100644 lualib-src/sproto/README create mode 100644 lualib-src/sproto/lsproto.c create mode 100644 lualib-src/sproto/msvcint.h create mode 100644 lualib-src/sproto/sproto.c create mode 100644 lualib-src/sproto/sproto.h delete mode 100644 lualib/jsonpack.lua create mode 100644 lualib/sproto.lua create mode 100644 lualib/sprotoparser.lua diff --git a/3rd/lpeg/HISTORY b/3rd/lpeg/HISTORY new file mode 100644 index 00000000..8ada7743 --- /dev/null +++ b/3rd/lpeg/HISTORY @@ -0,0 +1,90 @@ +HISTORY for LPeg 0.12 + +* Changes from version 0.11 to 0.12 + --------------------------------- + + no "unsigned short" limit for pattern sizes + + mathtime captures considered nullable + + some bugs fixed + +* Changes from version 0.10 to 0.11 + ------------------------------- + + complete reimplementation of the code generator + + new syntax for table captures + + new functions in module 're' + + other small improvements + +* Changes from version 0.9 to 0.10 + ------------------------------- + + backtrack stack has configurable size + + better error messages + + Notation for non-terminals in 're' back to A instead o + + experimental look-behind pattern + + support for external extensions + + works with Lua 5.2 + + consumes less C stack + + - "and" predicates do not keep captures + +* Changes from version 0.8 to 0.9 + ------------------------------- + + The accumulator capture was replaced by a fold capture; + programs that used the old 'lpeg.Ca' will need small changes. + + Some support for character classes from old C locales. + + A new named-group capture. + +* Changes from version 0.7 to 0.8 + ------------------------------- + + New "match-time" capture. + + New "argument capture" that allows passing arguments into the pattern. + + Better documentation for 're'. + + Several small improvements for 're'. + + The 're' module has an incompatibility with previous versions: + now, any use of a non-terminal must be enclosed in angle brackets + (like ). + +* Changes from version 0.6 to 0.7 + ------------------------------- + + Several improvements in module 're': + - better documentation; + - support for most captures (all but accumulator); + - limited repetitions p{n,m}. + + Small improvements in efficiency. + + Several small bugs corrected (special thanks to Hans Hagen + and Taco Hoekwater). + +* Changes from version 0.5 to 0.6 + ------------------------------- + + Support for non-numeric indices in grammars. + + Some bug fixes (thanks to the luatex team). + + Some new optimizations; (thanks to Mike Pall). + + A new page layout (thanks to Andre Carregal). + + Minimal documentation for module 're'. + +* Changes from version 0.4 to 0.5 + ------------------------------- + + Several optimizations. + + lpeg.P now accepts booleans. + + Some new examples. + + A proper license. + + Several small improvements. + +* Changes from version 0.3 to 0.4 + ------------------------------- + + Static check for loops in repetitions and grammars. + + Removed label option in captures. + + The implementation of captures uses less memory. + +* Changes from version 0.2 to 0.3 + ------------------------------- + + User-defined patterns in Lua. + + Several new captures. + +* Changes from version 0.1 to 0.2 + ------------------------------- + + Several small corrections. + + Handles embedded zeros like any other character. + + Capture "name" can be any Lua value. + + Unlimited number of captures. + + Match gets an optional initial position. + +(end of HISTORY) diff --git a/3rd/lpeg/lpcap.c b/3rd/lpeg/lpcap.c new file mode 100644 index 00000000..d90b935d --- /dev/null +++ b/3rd/lpeg/lpcap.c @@ -0,0 +1,537 @@ +/* +** $Id: lpcap.c,v 1.4 2013/03/21 20:25:12 roberto Exp $ +** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +*/ + +#include "lua.h" +#include "lauxlib.h" + +#include "lpcap.h" +#include "lptypes.h" + + +#define captype(cap) ((cap)->kind) + +#define isclosecap(cap) (captype(cap) == Cclose) + +#define closeaddr(c) ((c)->s + (c)->siz - 1) + +#define isfullcap(cap) ((cap)->siz != 0) + +#define getfromktable(cs,v) lua_rawgeti((cs)->L, ktableidx((cs)->ptop), v) + +#define pushluaval(cs) getfromktable(cs, (cs)->cap->idx) + + + +/* +** Put at the cache for Lua values the value indexed by 'v' in ktable +** of the running pattern (if it is not there yet); returns its index. +*/ +static int updatecache (CapState *cs, int v) { + int idx = cs->ptop + 1; /* stack index of cache for Lua values */ + if (v != cs->valuecached) { /* not there? */ + getfromktable(cs, v); /* get value from 'ktable' */ + lua_replace(cs->L, idx); /* put it at reserved stack position */ + cs->valuecached = v; /* keep track of what is there */ + } + return idx; +} + + +static int pushcapture (CapState *cs); + + +/* +** Goes back in a list of captures looking for an open capture +** corresponding to a close +*/ +static Capture *findopen (Capture *cap) { + int n = 0; /* number of closes waiting an open */ + for (;;) { + cap--; + if (isclosecap(cap)) n++; /* one more open to skip */ + else if (!isfullcap(cap)) + if (n-- == 0) return cap; + } +} + + +/* +** Go to the next capture +*/ +static void nextcap (CapState *cs) { + Capture *cap = cs->cap; + if (!isfullcap(cap)) { /* not a single capture? */ + int n = 0; /* number of opens waiting a close */ + for (;;) { /* look for corresponding close */ + cap++; + if (isclosecap(cap)) { + if (n-- == 0) break; + } + else if (!isfullcap(cap)) n++; + } + } + cs->cap = cap + 1; /* + 1 to skip last close (or entire single capture) */ +} + + +/* +** Push on the Lua stack all values generated by nested captures inside +** the current capture. Returns number of values pushed. 'addextra' +** makes it push the entire match after all captured values. The +** entire match is pushed also if there are no other nested values, +** so the function never returns zero. +*/ +static int pushnestedvalues (CapState *cs, int addextra) { + Capture *co = cs->cap; + if (isfullcap(cs->cap++)) { /* no nested captures? */ + lua_pushlstring(cs->L, co->s, co->siz - 1); /* push whole match */ + return 1; /* that is it */ + } + else { + int n = 0; + while (!isclosecap(cs->cap)) /* repeat for all nested patterns */ + n += pushcapture(cs); + if (addextra || n == 0) { /* need extra? */ + lua_pushlstring(cs->L, co->s, cs->cap->s - co->s); /* push whole match */ + n++; + } + cs->cap++; /* skip close entry */ + return n; + } +} + + +/* +** Push only the first value generated by nested captures +*/ +static void pushonenestedvalue (CapState *cs) { + int n = pushnestedvalues(cs, 0); + if (n > 1) + lua_pop(cs->L, n - 1); /* pop extra values */ +} + + +/* +** Try to find a named group capture with the name given at the top of +** the stack; goes backward from 'cap'. +*/ +static Capture *findback (CapState *cs, Capture *cap) { + lua_State *L = cs->L; + while (cap-- > cs->ocap) { /* repeat until end of list */ + if (isclosecap(cap)) + cap = findopen(cap); /* skip nested captures */ + else if (!isfullcap(cap)) + continue; /* opening an enclosing capture: skip and get previous */ + if (captype(cap) == Cgroup) { + getfromktable(cs, cap->idx); /* get group name */ + if (lua_equal(L, -2, -1)) { /* right group? */ + lua_pop(L, 2); /* remove reference name and group name */ + return cap; + } + else lua_pop(L, 1); /* remove group name */ + } + } + luaL_error(L, "back reference '%s' not found", lua_tostring(L, -1)); + return NULL; /* to avoid warnings */ +} + + +/* +** Back-reference capture. Return number of values pushed. +*/ +static int backrefcap (CapState *cs) { + int n; + Capture *curr = cs->cap; + pushluaval(cs); /* reference name */ + cs->cap = findback(cs, curr); /* find corresponding group */ + n = pushnestedvalues(cs, 0); /* push group's values */ + cs->cap = curr + 1; + return n; +} + + +/* +** Table capture: creates a new table and populates it with nested +** captures. +*/ +static int tablecap (CapState *cs) { + lua_State *L = cs->L; + int n = 0; + lua_newtable(L); + if (isfullcap(cs->cap++)) + return 1; /* table is empty */ + while (!isclosecap(cs->cap)) { + if (captype(cs->cap) == Cgroup && cs->cap->idx != 0) { /* named group? */ + pushluaval(cs); /* push group name */ + pushonenestedvalue(cs); + lua_settable(L, -3); + } + else { /* not a named group */ + int i; + int k = pushcapture(cs); + for (i = k; i > 0; i--) /* store all values into table */ + lua_rawseti(L, -(i + 1), n + i); + n += k; + } + } + cs->cap++; /* skip close entry */ + return 1; /* number of values pushed (only the table) */ +} + + +/* +** Table-query capture +*/ +static int querycap (CapState *cs) { + int idx = cs->cap->idx; + pushonenestedvalue(cs); /* get nested capture */ + lua_gettable(cs->L, updatecache(cs, idx)); /* query cap. value at table */ + if (!lua_isnil(cs->L, -1)) + return 1; + else { /* no value */ + lua_pop(cs->L, 1); /* remove nil */ + return 0; + } +} + + +/* +** Fold capture +*/ +static int foldcap (CapState *cs) { + int n; + lua_State *L = cs->L; + int idx = cs->cap->idx; + if (isfullcap(cs->cap++) || /* no nested captures? */ + isclosecap(cs->cap) || /* no nested captures (large subject)? */ + (n = pushcapture(cs)) == 0) /* nested captures with no values? */ + return luaL_error(L, "no initial value for fold capture"); + if (n > 1) + lua_pop(L, n - 1); /* leave only one result for accumulator */ + while (!isclosecap(cs->cap)) { + lua_pushvalue(L, updatecache(cs, idx)); /* get folding function */ + lua_insert(L, -2); /* put it before accumulator */ + n = pushcapture(cs); /* get next capture's values */ + lua_call(L, n + 1, 1); /* call folding function */ + } + cs->cap++; /* skip close entry */ + return 1; /* only accumulator left on the stack */ +} + + +/* +** Function capture +*/ +static int functioncap (CapState *cs) { + int n; + int top = lua_gettop(cs->L); + pushluaval(cs); /* push function */ + n = pushnestedvalues(cs, 0); /* push nested captures */ + lua_call(cs->L, n, LUA_MULTRET); /* call function */ + return lua_gettop(cs->L) - top; /* return function's results */ +} + + +/* +** Select capture +*/ +static int numcap (CapState *cs) { + int idx = cs->cap->idx; /* value to select */ + if (idx == 0) { /* no values? */ + nextcap(cs); /* skip entire capture */ + return 0; /* no value produced */ + } + else { + int n = pushnestedvalues(cs, 0); + if (n < idx) /* invalid index? */ + return luaL_error(cs->L, "no capture '%d'", idx); + else { + lua_pushvalue(cs->L, -(n - idx + 1)); /* get selected capture */ + lua_replace(cs->L, -(n + 1)); /* put it in place of 1st capture */ + lua_pop(cs->L, n - 1); /* remove other captures */ + return 1; + } + } +} + + +/* +** Return the stack index of the first runtime capture in the given +** list of captures (or zero if no runtime captures) +*/ +int finddyncap (Capture *cap, Capture *last) { + for (; cap < last; cap++) { + if (cap->kind == Cruntime) + return cap->idx; /* stack position of first capture */ + } + return 0; /* no dynamic captures in this segment */ +} + + +/* +** Calls a runtime capture. Returns number of captures removed by +** the call, including the initial Cgroup. (Captures to be added are +** on the Lua stack.) +*/ +int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) { + int n, id; + lua_State *L = cs->L; + int otop = lua_gettop(L); + Capture *open = findopen(close); + assert(captype(open) == Cgroup); + id = finddyncap(open, close); /* get first dynamic capture argument */ + close->kind = Cclose; /* closes the group */ + close->s = s; + cs->cap = open; cs->valuecached = 0; /* prepare capture state */ + luaL_checkstack(L, 4, "too many runtime captures"); + pushluaval(cs); /* push function to be called */ + lua_pushvalue(L, SUBJIDX); /* push original subject */ + lua_pushinteger(L, s - cs->s + 1); /* push current position */ + n = pushnestedvalues(cs, 0); /* push nested captures */ + lua_call(L, n + 2, LUA_MULTRET); /* call dynamic function */ + if (id > 0) { /* are there old dynamic captures to be removed? */ + int i; + for (i = id; i <= otop; i++) + lua_remove(L, id); /* remove old dynamic captures */ + *rem = otop - id + 1; /* total number of dynamic captures removed */ + } + else + *rem = 0; /* no dynamic captures removed */ + return close - open; /* number of captures of all kinds removed */ +} + + +/* +** Auxiliary structure for substitution and string captures: keep +** information about nested captures for future use, avoiding to push +** string results into Lua +*/ +typedef struct StrAux { + int isstring; /* whether capture is a string */ + union { + Capture *cp; /* if not a string, respective capture */ + struct { /* if it is a string... */ + const char *s; /* ... starts here */ + const char *e; /* ... ends here */ + } s; + } u; +} StrAux; + +#define MAXSTRCAPS 10 + +/* +** Collect values from current capture into array 'cps'. Current +** capture must be Cstring (first call) or Csimple (recursive calls). +** (In first call, fills %0 with whole match for Cstring.) +** Returns number of elements in the array that were filled. +*/ +static int getstrcaps (CapState *cs, StrAux *cps, int n) { + int k = n++; + cps[k].isstring = 1; /* get string value */ + cps[k].u.s.s = cs->cap->s; /* starts here */ + if (!isfullcap(cs->cap++)) { /* nested captures? */ + while (!isclosecap(cs->cap)) { /* traverse them */ + if (n >= MAXSTRCAPS) /* too many captures? */ + nextcap(cs); /* skip extra captures (will not need them) */ + else if (captype(cs->cap) == Csimple) /* string? */ + n = getstrcaps(cs, cps, n); /* put info. into array */ + else { + cps[n].isstring = 0; /* not a string */ + cps[n].u.cp = cs->cap; /* keep original capture */ + nextcap(cs); + n++; + } + } + cs->cap++; /* skip close */ + } + cps[k].u.s.e = closeaddr(cs->cap - 1); /* ends here */ + return n; +} + + +/* +** add next capture value (which should be a string) to buffer 'b' +*/ +static int addonestring (luaL_Buffer *b, CapState *cs, const char *what); + + +/* +** String capture: add result to buffer 'b' (instead of pushing +** it into the stack) +*/ +static void stringcap (luaL_Buffer *b, CapState *cs) { + StrAux cps[MAXSTRCAPS]; + int n; + size_t len, i; + const char *fmt; /* format string */ + fmt = lua_tolstring(cs->L, updatecache(cs, cs->cap->idx), &len); + n = getstrcaps(cs, cps, 0) - 1; /* collect nested captures */ + for (i = 0; i < len; i++) { /* traverse them */ + if (fmt[i] != '%') /* not an escape? */ + luaL_addchar(b, fmt[i]); /* add it to buffer */ + else if (fmt[++i] < '0' || fmt[i] > '9') /* not followed by a digit? */ + luaL_addchar(b, fmt[i]); /* add to buffer */ + else { + int l = fmt[i] - '0'; /* capture index */ + if (l > n) + luaL_error(cs->L, "invalid capture index (%d)", l); + else if (cps[l].isstring) + luaL_addlstring(b, cps[l].u.s.s, cps[l].u.s.e - cps[l].u.s.s); + else { + Capture *curr = cs->cap; + cs->cap = cps[l].u.cp; /* go back to evaluate that nested capture */ + if (!addonestring(b, cs, "capture")) + luaL_error(cs->L, "no values in capture index %d", l); + cs->cap = curr; /* continue from where it stopped */ + } + } + } +} + + +/* +** Substitution capture: add result to buffer 'b' +*/ +static void substcap (luaL_Buffer *b, CapState *cs) { + const char *curr = cs->cap->s; + if (isfullcap(cs->cap)) /* no nested captures? */ + luaL_addlstring(b, curr, cs->cap->siz - 1); /* keep original text */ + else { + cs->cap++; /* skip open entry */ + while (!isclosecap(cs->cap)) { /* traverse nested captures */ + const char *next = cs->cap->s; + luaL_addlstring(b, curr, next - curr); /* add text up to capture */ + if (addonestring(b, cs, "replacement")) + curr = closeaddr(cs->cap - 1); /* continue after match */ + else /* no capture value */ + curr = next; /* keep original text in final result */ + } + luaL_addlstring(b, curr, cs->cap->s - curr); /* add last piece of text */ + } + cs->cap++; /* go to next capture */ +} + + +/* +** Evaluates a capture and adds its first value to buffer 'b'; returns +** whether there was a value +*/ +static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) { + switch (captype(cs->cap)) { + case Cstring: + stringcap(b, cs); /* add capture directly to buffer */ + return 1; + case Csubst: + substcap(b, cs); /* add capture directly to buffer */ + return 1; + default: { + lua_State *L = cs->L; + int n = pushcapture(cs); + if (n > 0) { + if (n > 1) lua_pop(L, n - 1); /* only one result */ + if (!lua_isstring(L, -1)) + luaL_error(L, "invalid %s value (a %s)", what, luaL_typename(L, -1)); + luaL_addvalue(b); + } + return n; + } + } +} + + +/* +** Push all values of the current capture into the stack; returns +** number of values pushed +*/ +static int pushcapture (CapState *cs) { + lua_State *L = cs->L; + luaL_checkstack(L, 4, "too many captures"); + switch (captype(cs->cap)) { + case Cposition: { + lua_pushinteger(L, cs->cap->s - cs->s + 1); + cs->cap++; + return 1; + } + case Cconst: { + pushluaval(cs); + cs->cap++; + return 1; + } + case Carg: { + int arg = (cs->cap++)->idx; + if (arg + FIXEDARGS > cs->ptop) + return luaL_error(L, "reference to absent argument #%d", arg); + lua_pushvalue(L, arg + FIXEDARGS); + return 1; + } + case Csimple: { + int k = pushnestedvalues(cs, 1); + lua_insert(L, -k); /* make whole match be first result */ + return k; + } + case Cruntime: { + lua_pushvalue(L, (cs->cap++)->idx); /* value is in the stack */ + return 1; + } + case Cstring: { + luaL_Buffer b; + luaL_buffinit(L, &b); + stringcap(&b, cs); + luaL_pushresult(&b); + return 1; + } + case Csubst: { + luaL_Buffer b; + luaL_buffinit(L, &b); + substcap(&b, cs); + luaL_pushresult(&b); + return 1; + } + case Cgroup: { + if (cs->cap->idx == 0) /* anonymous group? */ + return pushnestedvalues(cs, 0); /* add all nested values */ + else { /* named group: add no values */ + nextcap(cs); /* skip capture */ + return 0; + } + } + case Cbackref: return backrefcap(cs); + case Ctable: return tablecap(cs); + case Cfunction: return functioncap(cs); + case Cnum: return numcap(cs); + case Cquery: return querycap(cs); + case Cfold: return foldcap(cs); + default: assert(0); return 0; + } +} + + +/* +** Prepare a CapState structure and traverse the entire list of +** captures in the stack pushing its results. 's' is the subject +** string, 'r' is the final position of the match, and 'ptop' +** the index in the stack where some useful values were pushed. +** Returns the number of results pushed. (If the list produces no +** results, push the final position of the match.) +*/ +int getcaptures (lua_State *L, const char *s, const char *r, int ptop) { + Capture *capture = (Capture *)lua_touserdata(L, caplistidx(ptop)); + int n = 0; + if (!isclosecap(capture)) { /* is there any capture? */ + CapState cs; + cs.ocap = cs.cap = capture; cs.L = L; + cs.s = s; cs.valuecached = 0; cs.ptop = ptop; + do { /* collect their values */ + n += pushcapture(&cs); + } while (!isclosecap(cs.cap)); + } + if (n == 0) { /* no capture values? */ + lua_pushinteger(L, r - s + 1); /* return only end position */ + n = 1; + } + return n; +} + + diff --git a/3rd/lpeg/lpcap.h b/3rd/lpeg/lpcap.h new file mode 100644 index 00000000..c0a0e382 --- /dev/null +++ b/3rd/lpeg/lpcap.h @@ -0,0 +1,43 @@ +/* +** $Id: lpcap.h,v 1.1 2013/03/21 20:25:12 roberto Exp $ +*/ + +#if !defined(lpcap_h) +#define lpcap_h + + +#include "lptypes.h" + + +/* kinds of captures */ +typedef enum CapKind { + Cclose, Cposition, Cconst, Cbackref, Carg, Csimple, Ctable, Cfunction, + Cquery, Cstring, Cnum, Csubst, Cfold, Cruntime, Cgroup +} CapKind; + + +typedef struct Capture { + const char *s; /* subject position */ + short idx; /* extra info about capture (group name, arg index, etc.) */ + byte kind; /* kind of capture */ + byte siz; /* size of full capture + 1 (0 = not a full capture) */ +} Capture; + + +typedef struct CapState { + Capture *cap; /* current capture */ + Capture *ocap; /* (original) capture list */ + lua_State *L; + int ptop; /* index of last argument to 'match' */ + const char *s; /* original string */ + int valuecached; /* value stored in cache slot */ +} CapState; + + +int runtimecap (CapState *cs, Capture *close, const char *s, int *rem); +int getcaptures (lua_State *L, const char *s, const char *r, int ptop); +int finddyncap (Capture *cap, Capture *last); + +#endif + + diff --git a/3rd/lpeg/lpcode.c b/3rd/lpeg/lpcode.c new file mode 100644 index 00000000..2cc0e0d7 --- /dev/null +++ b/3rd/lpeg/lpcode.c @@ -0,0 +1,963 @@ +/* +** $Id: lpcode.c,v 1.18 2013/04/12 16:30:33 roberto Exp $ +** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +*/ + +#include + + +#include "lua.h" +#include "lauxlib.h" + +#include "lptypes.h" +#include "lpcode.h" + + +/* signals a "no-instruction */ +#define NOINST -1 + + + +static const Charset fullset_ = + {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; + +static const Charset *fullset = &fullset_; + +/* +** {====================================================== +** Analysis and some optimizations +** ======================================================= +*/ + +/* +** Check whether a charset is empty (IFail), singleton (IChar), +** full (IAny), or none of those (ISet). +*/ +static Opcode charsettype (const byte *cs, int *c) { + int count = 0; + int i; + int candidate = -1; /* candidate position for a char */ + for (i = 0; i < CHARSETSIZE; i++) { + int b = cs[i]; + if (b == 0) { + if (count > 1) return ISet; /* else set is still empty */ + } + else if (b == 0xFF) { + if (count < (i * BITSPERCHAR)) + return ISet; + else count += BITSPERCHAR; /* set is still full */ + } + else if ((b & (b - 1)) == 0) { /* byte has only one bit? */ + if (count > 0) + return ISet; /* set is neither full nor empty */ + else { /* set has only one char till now; track it */ + count++; + candidate = i; + } + } + else return ISet; /* byte is neither empty, full, nor singleton */ + } + switch (count) { + case 0: return IFail; /* empty set */ + case 1: { /* singleton; find character bit inside byte */ + int b = cs[candidate]; + *c = candidate * BITSPERCHAR; + if ((b & 0xF0) != 0) { *c += 4; b >>= 4; } + if ((b & 0x0C) != 0) { *c += 2; b >>= 2; } + if ((b & 0x02) != 0) { *c += 1; } + return IChar; + } + default: { + assert(count == CHARSETSIZE * BITSPERCHAR); /* full set */ + return IAny; + } + } +} + +/* +** A few basic operations on Charsets +*/ +static void cs_complement (Charset *cs) { + loopset(i, cs->cs[i] = ~cs->cs[i]); +} + + +static int cs_equal (const byte *cs1, const byte *cs2) { + loopset(i, if (cs1[i] != cs2[i]) return 0); + return 1; +} + + +/* +** computes whether sets cs1 and cs2 are disjoint +*/ +static int cs_disjoint (const Charset *cs1, const Charset *cs2) { + loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;) + return 1; +} + + +/* +** Convert a 'char' pattern (TSet, TChar, TAny) to a charset +*/ +int tocharset (TTree *tree, Charset *cs) { + switch (tree->tag) { + case TSet: { /* copy set */ + loopset(i, cs->cs[i] = treebuffer(tree)[i]); + return 1; + } + case TChar: { /* only one char */ + assert(0 <= tree->u.n && tree->u.n <= UCHAR_MAX); + loopset(i, cs->cs[i] = 0); /* erase all chars */ + setchar(cs->cs, tree->u.n); /* add that one */ + return 1; + } + case TAny: { + loopset(i, cs->cs[i] = 0xFF); /* add all to the set */ + return 1; + } + default: return 0; + } +} + + +/* +** Checks whether a pattern has captures +*/ +int hascaptures (TTree *tree) { + tailcall: + switch (tree->tag) { + case TCapture: case TRunTime: + return 1; + default: { + switch (numsiblings[tree->tag]) { + case 1: /* return hascaptures(sib1(tree)); */ + tree = sib1(tree); goto tailcall; + case 2: + if (hascaptures(sib1(tree))) return 1; + /* else return hascaptures(sib2(tree)); */ + tree = sib2(tree); goto tailcall; + default: assert(numsiblings[tree->tag] == 0); return 0; + } + } + } +} + + +/* +** Checks how a pattern behaves regarding the empty string, +** in one of two different ways: +** A pattern is *nullable* if it can match without consuming any character; +** A pattern is *nofail* if it never fails for any string +** (including the empty string). +** The difference is only for predicates and run-time captures; +** for other patterns, the two properties are equivalent. +** (With predicates, &'a' is nullable but not nofail. Of course, +** nofail => nullable.) +** These functions are all convervative in the following way: +** p is nullable => nullable(p) +** nofail(p) => p cannot fail +** The function assumes that TOpenCall is not nullable; +** this will be checked again when the grammar is fixed.) +** Run-time captures can do whatever they want, so the result +** is conservative. +*/ +int checkaux (TTree *tree, int pred) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: + case TFalse: case TOpenCall: + return 0; /* not nullable */ + case TRep: case TTrue: + return 1; /* no fail */ + case TNot: case TBehind: /* can match empty, but can fail */ + if (pred == PEnofail) return 0; + else return 1; /* PEnullable */ + case TAnd: /* can match empty; fail iff body does */ + if (pred == PEnullable) return 1; + /* else return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TRunTime: /* can fail; match empty iff body does */ + if (pred == PEnofail) return 0; + /* else return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TSeq: + if (!checkaux(sib1(tree), pred)) return 0; + /* else return checkaux(sib2(tree), pred); */ + tree = sib2(tree); goto tailcall; + case TChoice: + if (checkaux(sib2(tree), pred)) return 1; + /* else return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TCapture: case TGrammar: case TRule: + /* return checkaux(sib1(tree), pred); */ + tree = sib1(tree); goto tailcall; + case TCall: /* return checkaux(sib2(tree), pred); */ + tree = sib2(tree); goto tailcall; + default: assert(0); return 0; + }; +} + + +/* +** number of characters to match a pattern (or -1 if variable) +** ('count' avoids infinite loops for grammars) +*/ +int fixedlenx (TTree *tree, int count, int len) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: + return len + 1; + case TFalse: case TTrue: case TNot: case TAnd: case TBehind: + return len; + case TRep: case TRunTime: case TOpenCall: + return -1; + case TCapture: case TRule: case TGrammar: + /* return fixedlenx(sib1(tree), count); */ + tree = sib1(tree); goto tailcall; + case TCall: + if (count++ >= MAXRULES) + return -1; /* may be a loop */ + /* else return fixedlenx(sib2(tree), count); */ + tree = sib2(tree); goto tailcall; + case TSeq: { + len = fixedlenx(sib1(tree), count, len); + if (len < 0) return -1; + /* else return fixedlenx(sib2(tree), count, len); */ + tree = sib2(tree); goto tailcall; + } + case TChoice: { + int n1, n2; + n1 = fixedlenx(sib1(tree), count, len); + if (n1 < 0) return -1; + n2 = fixedlenx(sib2(tree), count, len); + if (n1 == n2) return n1; + else return -1; + } + default: assert(0); return 0; + }; +} + + +/* +** Computes the 'first set' of a pattern. +** The result is a conservative aproximation: +** match p ax -> x' for some x ==> a in first(p). +** The set 'follow' is the first set of what follows the +** pattern (full set if nothing follows it). +** The function returns 0 when this set can be used for +** tests that avoid the pattern altogether. +** A non-zero return can happen for two reasons: +** 1) match p '' -> '' ==> returns 1. +** (tests cannot be used because they always fail for an empty input) +** 2) there is a match-time capture ==> returns 2. +** (match-time captures should not be avoided by optimizations) +*/ +static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: { + tocharset(tree, firstset); + return 0; + } + case TTrue: { + loopset(i, firstset->cs[i] = follow->cs[i]); + return 1; + } + case TFalse: { + loopset(i, firstset->cs[i] = 0); + return 0; + } + case TChoice: { + Charset csaux; + int e1 = getfirst(sib1(tree), follow, firstset); + int e2 = getfirst(sib2(tree), follow, &csaux); + loopset(i, firstset->cs[i] |= csaux.cs[i]); + return e1 | e2; + } + case TSeq: { + if (!nullable(sib1(tree))) { + /* return getfirst(sib1(tree), fullset, firstset); */ + tree = sib1(tree); follow = fullset; goto tailcall; + } + else { /* FIRST(p1 p2, fl) = FIRST(p1, FIRST(p2, fl)) */ + Charset csaux; + int e2 = getfirst(sib2(tree), follow, &csaux); + int e1 = getfirst(sib1(tree), &csaux, firstset); + if (e1 == 0) return 0; /* 'e1' ensures that first can be used */ + else if ((e1 | e2) & 2) /* one of the children has a matchtime? */ + return 2; /* pattern has a matchtime capture */ + else return e2; /* else depends on 'e2' */ + } + } + case TRep: { + getfirst(sib1(tree), follow, firstset); + loopset(i, firstset->cs[i] |= follow->cs[i]); + return 1; /* accept the empty string */ + } + case TCapture: case TGrammar: case TRule: { + /* return getfirst(sib1(tree), follow, firstset); */ + tree = sib1(tree); goto tailcall; + } + case TRunTime: { /* function invalidates any follow info. */ + int e = getfirst(sib1(tree), fullset, firstset); + if (e) return 2; /* function is not "protected"? */ + else return 0; /* pattern inside capture ensures first can be used */ + } + case TCall: { + /* return getfirst(sib2(tree), follow, firstset); */ + tree = sib2(tree); goto tailcall; + } + case TAnd: { + int e = getfirst(sib1(tree), follow, firstset); + loopset(i, firstset->cs[i] &= follow->cs[i]); + return e; + } + case TNot: { + if (tocharset(sib1(tree), firstset)) { + cs_complement(firstset); + return 1; + } + /* else go through */ + } + case TBehind: { /* instruction gives no new information */ + /* call 'getfirst' to check for math-time captures */ + int e = getfirst(sib1(tree), follow, firstset); + loopset(i, firstset->cs[i] = follow->cs[i]); /* uses follow */ + return e | 1; /* always can accept the empty string */ + } + default: assert(0); return 0; + } +} + + +/* +** If it returns true, then pattern can fail only depending on the next +** character of the subject +*/ +static int headfail (TTree *tree) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: case TFalse: + return 1; + case TTrue: case TRep: case TRunTime: case TNot: + case TBehind: + return 0; + case TCapture: case TGrammar: case TRule: case TAnd: + tree = sib1(tree); goto tailcall; /* return headfail(sib1(tree)); */ + case TCall: + tree = sib2(tree); goto tailcall; /* return headfail(sib2(tree)); */ + case TSeq: + if (!nofail(sib2(tree))) return 0; + /* else return headfail(sib1(tree)); */ + tree = sib1(tree); goto tailcall; + case TChoice: + if (!headfail(sib1(tree))) return 0; + /* else return headfail(sib2(tree)); */ + tree = sib2(tree); goto tailcall; + default: assert(0); return 0; + } +} + + +/* +** Check whether the code generation for the given tree can benefit +** from a follow set (to avoid computing the follow set when it is +** not needed) +*/ +static int needfollow (TTree *tree) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: + case TFalse: case TTrue: case TAnd: case TNot: + case TRunTime: case TGrammar: case TCall: case TBehind: + return 0; + case TChoice: case TRep: + return 1; + case TCapture: + tree = sib1(tree); goto tailcall; + case TSeq: + tree = sib2(tree); goto tailcall; + default: assert(0); return 0; + } +} + +/* }====================================================== */ + + + +/* +** {====================================================== +** Code generation +** ======================================================= +*/ + + +/* +** size of an instruction +*/ +int sizei (const Instruction *i) { + switch((Opcode)i->i.code) { + case ISet: case ISpan: return CHARSETINSTSIZE; + case ITestSet: return CHARSETINSTSIZE + 1; + case ITestChar: case ITestAny: case IChoice: case IJmp: + case ICall: case IOpenCall: case ICommit: case IPartialCommit: + case IBackCommit: return 2; + default: return 1; + } +} + + +/* +** state for the compiler +*/ +typedef struct CompileState { + Pattern *p; /* pattern being compiled */ + int ncode; /* next position in p->code to be filled */ + lua_State *L; +} CompileState; + + +/* +** code generation is recursive; 'opt' indicates that the code is +** being generated under a 'IChoice' operator jumping to its end. +** 'tt' points to a previous test protecting this code. 'fl' is +** the follow set of the pattern. +*/ +static void codegen (CompileState *compst, TTree *tree, int opt, int tt, + const Charset *fl); + + +void reallocprog (lua_State *L, Pattern *p, int nsize) { + void *ud; + lua_Alloc f = lua_getallocf(L, &ud); + void *newblock = f(ud, p->code, p->codesize * sizeof(Instruction), + nsize * sizeof(Instruction)); + if (newblock == NULL && nsize > 0) + luaL_error(L, "not enough memory"); + p->code = (Instruction *)newblock; + p->codesize = nsize; +} + + +static int nextinstruction (CompileState *compst) { + int size = compst->p->codesize; + if (compst->ncode >= size) + reallocprog(compst->L, compst->p, size * 2); + return compst->ncode++; +} + + +#define getinstr(cs,i) ((cs)->p->code[i]) + + +static int addinstruction (CompileState *compst, Opcode op, int aux) { + int i = nextinstruction(compst); + getinstr(compst, i).i.code = op; + getinstr(compst, i).i.aux = aux; + return i; +} + + +static int addoffsetinst (CompileState *compst, Opcode op) { + int i = addinstruction(compst, op, 0); /* instruction */ + addinstruction(compst, (Opcode)0, 0); /* open space for offset */ + assert(op == ITestSet || sizei(&getinstr(compst, i)) == 2); + return i; +} + + +static void setoffset (CompileState *compst, int instruction, int offset) { + getinstr(compst, instruction + 1).offset = offset; +} + + +/* +** Add a capture instruction: +** 'op' is the capture instruction; 'cap' the capture kind; +** 'key' the key into ktable; 'aux' is optional offset +** +*/ +static int addinstcap (CompileState *compst, Opcode op, int cap, int key, + int aux) { + int i = addinstruction(compst, op, joinkindoff(cap, aux)); + getinstr(compst, i).i.key = key; + return i; +} + + +#define gethere(compst) ((compst)->ncode) + +#define target(code,i) ((i) + code[i + 1].offset) + + +static void jumptothere (CompileState *compst, int instruction, int target) { + if (instruction >= 0) + setoffset(compst, instruction, target - instruction); +} + + +static void jumptohere (CompileState *compst, int instruction) { + jumptothere(compst, instruction, gethere(compst)); +} + + +/* +** Code an IChar instruction, or IAny if there is an equivalent +** test dominating it +*/ +static void codechar (CompileState *compst, int c, int tt) { + if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar && + getinstr(compst, tt).i.aux == c) + addinstruction(compst, IAny, 0); + else + addinstruction(compst, IChar, c); +} + + +/* +** Add a charset posfix to an instruction +*/ +static void addcharset (CompileState *compst, const byte *cs) { + int p = gethere(compst); + int i; + for (i = 0; i < (int)CHARSETINSTSIZE - 1; i++) + nextinstruction(compst); /* space for buffer */ + /* fill buffer with charset */ + loopset(j, getinstr(compst, p).buff[j] = cs[j]); +} + + +/* +** code a char set, optimizing unit sets for IChar, "complete" +** sets for IAny, and empty sets for IFail; also use an IAny +** when instruction is dominated by an equivalent test. +*/ +static void codecharset (CompileState *compst, const byte *cs, int tt) { + int c = 0; /* (=) to avoid warnings */ + Opcode op = charsettype(cs, &c); + switch (op) { + case IChar: codechar(compst, c, tt); break; + case ISet: { /* non-trivial set? */ + if (tt >= 0 && getinstr(compst, tt).i.code == ITestSet && + cs_equal(cs, getinstr(compst, tt + 2).buff)) + addinstruction(compst, IAny, 0); + else { + addinstruction(compst, ISet, 0); + addcharset(compst, cs); + } + break; + } + default: addinstruction(compst, op, c); break; + } +} + + +/* +** code a test set, optimizing unit sets for ITestChar, "complete" +** sets for ITestAny, and empty sets for IJmp (always fails). +** 'e' is true iff test should accept the empty string. (Test +** instructions in the current VM never accept the empty string.) +*/ +static int codetestset (CompileState *compst, Charset *cs, int e) { + if (e) return NOINST; /* no test */ + else { + int c = 0; + Opcode op = charsettype(cs->cs, &c); + switch (op) { + case IFail: return addoffsetinst(compst, IJmp); /* always jump */ + case IAny: return addoffsetinst(compst, ITestAny); + case IChar: { + int i = addoffsetinst(compst, ITestChar); + getinstr(compst, i).i.aux = c; + return i; + } + case ISet: { + int i = addoffsetinst(compst, ITestSet); + addcharset(compst, cs->cs); + return i; + } + default: assert(0); return 0; + } + } +} + + +/* +** Find the final destination of a sequence of jumps +*/ +static int finaltarget (Instruction *code, int i) { + while (code[i].i.code == IJmp) + i = target(code, i); + return i; +} + + +/* +** final label (after traversing any jumps) +*/ +static int finallabel (Instruction *code, int i) { + return finaltarget(code, target(code, i)); +} + + +/* +** == behind n;

(where n = fixedlen(p)) +*/ +static void codebehind (CompileState *compst, TTree *tree) { + if (tree->u.n > 0) + addinstruction(compst, IBehind, tree->u.n); + codegen(compst, sib1(tree), 0, NOINST, fullset); +} + + +/* +** Choice; optimizations: +** - when p1 is headfail +** - when first(p1) and first(p2) are disjoint; than +** a character not in first(p1) cannot go to p1, and a character +** in first(p1) cannot go to p2 (at it is not in first(p2)). +** (The optimization is not valid if p1 accepts the empty string, +** as then there is no character at all...) +** - when p2 is empty and opt is true; a IPartialCommit can resuse +** the Choice already active in the stack. +*/ +static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt, + const Charset *fl) { + int emptyp2 = (p2->tag == TTrue); + Charset cs1, cs2; + int e1 = getfirst(p1, fullset, &cs1); + if (headfail(p1) || + (!e1 && (getfirst(p2, fl, &cs2), cs_disjoint(&cs1, &cs2)))) { + /* == test (fail(p1)) -> L1 ; p1 ; jmp L2; L1: p2; L2: */ + int test = codetestset(compst, &cs1, 0); + int jmp = NOINST; + codegen(compst, p1, 0, test, fl); + if (!emptyp2) + jmp = addoffsetinst(compst, IJmp); + jumptohere(compst, test); + codegen(compst, p2, opt, NOINST, fl); + jumptohere(compst, jmp); + } + else if (opt && emptyp2) { + /* p1? == IPartialCommit; p1 */ + jumptohere(compst, addoffsetinst(compst, IPartialCommit)); + codegen(compst, p1, 1, NOINST, fullset); + } + else { + /* == + test(fail(p1)) -> L1; choice L1; ; commit L2; L1: ; L2: */ + int pcommit; + int test = codetestset(compst, &cs1, e1); + int pchoice = addoffsetinst(compst, IChoice); + codegen(compst, p1, emptyp2, test, fullset); + pcommit = addoffsetinst(compst, ICommit); + jumptohere(compst, pchoice); + jumptohere(compst, test); + codegen(compst, p2, opt, NOINST, fl); + jumptohere(compst, pcommit); + } +} + + +/* +** And predicate +** optimization: fixedlen(p) = n ==> <&p> ==

; behind n +** (valid only when 'p' has no captures) +*/ +static void codeand (CompileState *compst, TTree *tree, int tt) { + int n = fixedlen(tree); + if (n >= 0 && n <= MAXBEHIND && !hascaptures(tree)) { + codegen(compst, tree, 0, tt, fullset); + if (n > 0) + addinstruction(compst, IBehind, n); + } + else { /* default: Choice L1; p1; BackCommit L2; L1: Fail; L2: */ + int pcommit; + int pchoice = addoffsetinst(compst, IChoice); + codegen(compst, tree, 0, tt, fullset); + pcommit = addoffsetinst(compst, IBackCommit); + jumptohere(compst, pchoice); + addinstruction(compst, IFail, 0); + jumptohere(compst, pcommit); + } +} + + +/* +** Captures: if pattern has fixed (and not too big) length, use +** a single IFullCapture instruction after the match; otherwise, +** enclose the pattern with OpenCapture - CloseCapture. +*/ +static void codecapture (CompileState *compst, TTree *tree, int tt, + const Charset *fl) { + int len = fixedlen(sib1(tree)); + if (len >= 0 && len <= MAXOFF && !hascaptures(sib1(tree))) { + codegen(compst, sib1(tree), 0, tt, fl); + addinstcap(compst, IFullCapture, tree->cap, tree->key, len); + } + else { + addinstcap(compst, IOpenCapture, tree->cap, tree->key, 0); + codegen(compst, sib1(tree), 0, tt, fl); + addinstcap(compst, ICloseCapture, Cclose, 0, 0); + } +} + + +static void coderuntime (CompileState *compst, TTree *tree, int tt) { + addinstcap(compst, IOpenCapture, Cgroup, tree->key, 0); + codegen(compst, sib1(tree), 0, tt, fullset); + addinstcap(compst, ICloseRunTime, Cclose, 0, 0); +} + + +/* +** Repetion; optimizations: +** When pattern is a charset, can use special instruction ISpan. +** When pattern is head fail, or if it starts with characters that +** are disjoint from what follows the repetions, a simple test +** is enough (a fail inside the repetition would backtrack to fail +** again in the following pattern, so there is no need for a choice). +** When 'opt' is true, the repetion can reuse the Choice already +** active in the stack. +*/ +static void coderep (CompileState *compst, TTree *tree, int opt, + const Charset *fl) { + Charset st; + if (tocharset(tree, &st)) { + addinstruction(compst, ISpan, 0); + addcharset(compst, st.cs); + } + else { + int e1 = getfirst(tree, fullset, &st); + if (headfail(tree) || (!e1 && cs_disjoint(&st, fl))) { + /* L1: test (fail(p1)) -> L2;

; jmp L1; L2: */ + int jmp; + int test = codetestset(compst, &st, 0); + codegen(compst, tree, opt, test, fullset); + jmp = addoffsetinst(compst, IJmp); + jumptohere(compst, test); + jumptothere(compst, jmp, test); + } + else { + /* test(fail(p1)) -> L2; choice L2; L1:

; partialcommit L1; L2: */ + /* or (if 'opt'): partialcommit L1; L1:

; partialcommit L1; */ + int commit, l2; + int test = codetestset(compst, &st, e1); + int pchoice = NOINST; + if (opt) + jumptohere(compst, addoffsetinst(compst, IPartialCommit)); + else + pchoice = addoffsetinst(compst, IChoice); + l2 = gethere(compst); + codegen(compst, tree, 0, NOINST, fullset); + commit = addoffsetinst(compst, IPartialCommit); + jumptothere(compst, commit, l2); + jumptohere(compst, pchoice); + jumptohere(compst, test); + } + } +} + + +/* +** Not predicate; optimizations: +** In any case, if first test fails, 'not' succeeds, so it can jump to +** the end. If pattern is headfail, that is all (it cannot fail +** in other parts); this case includes 'not' of simple sets. Otherwise, +** use the default code (a choice plus a failtwice). +*/ +static void codenot (CompileState *compst, TTree *tree) { + Charset st; + int e = getfirst(tree, fullset, &st); + int test = codetestset(compst, &st, e); + if (headfail(tree)) /* test (fail(p1)) -> L1; fail; L1: */ + addinstruction(compst, IFail, 0); + else { + /* test(fail(p))-> L1; choice L1;

; failtwice; L1: */ + int pchoice = addoffsetinst(compst, IChoice); + codegen(compst, tree, 0, NOINST, fullset); + addinstruction(compst, IFailTwice, 0); + jumptohere(compst, pchoice); + } + jumptohere(compst, test); +} + + +/* +** change open calls to calls, using list 'positions' to find +** correct offsets; also optimize tail calls +*/ +static void correctcalls (CompileState *compst, int *positions, + int from, int to) { + int i; + Instruction *code = compst->p->code; + for (i = from; i < to; i += sizei(&code[i])) { + if (code[i].i.code == IOpenCall) { + int n = code[i].i.key; /* rule number */ + int rule = positions[n]; /* rule position */ + assert(rule == from || code[rule - 1].i.code == IRet); + if (code[finaltarget(code, i + 2)].i.code == IRet) /* call; ret ? */ + code[i].i.code = IJmp; /* tail call */ + else + code[i].i.code = ICall; + jumptothere(compst, i, rule); /* call jumps to respective rule */ + } + } + assert(i == to); +} + + +/* +** Code for a grammar: +** call L1; jmp L2; L1: rule 1; ret; rule 2; ret; ...; L2: +*/ +static void codegrammar (CompileState *compst, TTree *grammar) { + int positions[MAXRULES]; + int rulenumber = 0; + TTree *rule; + int firstcall = addoffsetinst(compst, ICall); /* call initial rule */ + int jumptoend = addoffsetinst(compst, IJmp); /* jump to the end */ + int start = gethere(compst); /* here starts the initial rule */ + jumptohere(compst, firstcall); + for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { + positions[rulenumber++] = gethere(compst); /* save rule position */ + codegen(compst, sib1(rule), 0, NOINST, fullset); /* code rule */ + addinstruction(compst, IRet, 0); + } + assert(rule->tag == TTrue); + jumptohere(compst, jumptoend); + correctcalls(compst, positions, start, gethere(compst)); +} + + +static void codecall (CompileState *compst, TTree *call) { + int c = addoffsetinst(compst, IOpenCall); /* to be corrected later */ + getinstr(compst, c).i.key = sib2(call)->cap; /* rule number */ + assert(sib2(call)->tag == TRule); +} + + +/* +** Code first child of a sequence +** (second child is called in-place to allow tail call) +** Return 'tt' for second child +*/ +static int codeseq1 (CompileState *compst, TTree *p1, TTree *p2, + int tt, const Charset *fl) { + if (needfollow(p1)) { + Charset fl1; + getfirst(p2, fl, &fl1); /* p1 follow is p2 first */ + codegen(compst, p1, 0, tt, &fl1); + } + else /* use 'fullset' as follow */ + codegen(compst, p1, 0, tt, fullset); + if (fixedlen(p1) != 0) /* can 'p1' consume anything? */ + return NOINST; /* invalidate test */ + else return tt; /* else 'tt' still protects sib2 */ +} + + +/* +** Main code-generation function: dispatch to auxiliar functions +** according to kind of tree +*/ +static void codegen (CompileState *compst, TTree *tree, int opt, int tt, + const Charset *fl) { + tailcall: + switch (tree->tag) { + case TChar: codechar(compst, tree->u.n, tt); break; + case TAny: addinstruction(compst, IAny, 0); break; + case TSet: codecharset(compst, treebuffer(tree), tt); break; + case TTrue: break; + case TFalse: addinstruction(compst, IFail, 0); break; + case TChoice: codechoice(compst, sib1(tree), sib2(tree), opt, fl); break; + case TRep: coderep(compst, sib1(tree), opt, fl); break; + case TBehind: codebehind(compst, tree); break; + case TNot: codenot(compst, sib1(tree)); break; + case TAnd: codeand(compst, sib1(tree), tt); break; + case TCapture: codecapture(compst, tree, tt, fl); break; + case TRunTime: coderuntime(compst, tree, tt); break; + case TGrammar: codegrammar(compst, tree); break; + case TCall: codecall(compst, tree); break; + case TSeq: { + tt = codeseq1(compst, sib1(tree), sib2(tree), tt, fl); /* code 'p1' */ + /* codegen(compst, p2, opt, tt, fl); */ + tree = sib2(tree); goto tailcall; + } + default: assert(0); + } +} + + +/* +** Optimize jumps and other jump-like instructions. +** * Update labels of instructions with labels to their final +** destinations (e.g., choice L1; ... L1: jmp L2: becomes +** choice L2) +** * Jumps to other instructions that do jumps become those +** instructions (e.g., jump to return becomes a return; jump +** to commit becomes a commit) +*/ +static void peephole (CompileState *compst) { + Instruction *code = compst->p->code; + int i; + for (i = 0; i < compst->ncode; i += sizei(&code[i])) { + switch (code[i].i.code) { + case IChoice: case ICall: case ICommit: case IPartialCommit: + case IBackCommit: case ITestChar: case ITestSet: + case ITestAny: { /* instructions with labels */ + jumptothere(compst, i, finallabel(code, i)); /* optimize label */ + break; + } + case IJmp: { + int ft = finaltarget(code, i); + switch (code[ft].i.code) { /* jumping to what? */ + case IRet: case IFail: case IFailTwice: + case IEnd: { /* instructions with unconditional implicit jumps */ + code[i] = code[ft]; /* jump becomes that instruction */ + code[i + 1].i.code = IAny; /* 'no-op' for target position */ + break; + } + case ICommit: case IPartialCommit: + case IBackCommit: { /* inst. with unconditional explicit jumps */ + int fft = finallabel(code, ft); + code[i] = code[ft]; /* jump becomes that instruction... */ + jumptothere(compst, i, fft); /* but must correct its offset */ + i--; /* reoptimize its label */ + break; + } + default: { + jumptothere(compst, i, ft); /* optimize label */ + break; + } + } + break; + } + default: break; + } + } + assert(code[i - 1].i.code == IEnd); +} + + +/* +** Compile a pattern +*/ +Instruction *compile (lua_State *L, Pattern *p) { + CompileState compst; + compst.p = p; compst.ncode = 0; compst.L = L; + reallocprog(L, p, 2); /* minimum initial size */ + codegen(&compst, p->tree, 0, NOINST, fullset); + addinstruction(&compst, IEnd, 0); + reallocprog(L, p, compst.ncode); /* set final size */ + peephole(&compst); + return p->code; +} + + +/* }====================================================== */ + diff --git a/3rd/lpeg/lpcode.h b/3rd/lpeg/lpcode.h new file mode 100644 index 00000000..5c9d54f9 --- /dev/null +++ b/3rd/lpeg/lpcode.h @@ -0,0 +1,34 @@ +/* +** $Id: lpcode.h,v 1.5 2013/04/04 21:24:45 roberto Exp $ +*/ + +#if !defined(lpcode_h) +#define lpcode_h + +#include "lua.h" + +#include "lptypes.h" +#include "lptree.h" +#include "lpvm.h" + +int tocharset (TTree *tree, Charset *cs); +int checkaux (TTree *tree, int pred); +int fixedlenx (TTree *tree, int count, int len); +int hascaptures (TTree *tree); +int lp_gc (lua_State *L); +Instruction *compile (lua_State *L, Pattern *p); +void reallocprog (lua_State *L, Pattern *p, int nsize); +int sizei (const Instruction *i); + + +#define PEnullable 0 +#define PEnofail 1 + +#define nofail(t) checkaux(t, PEnofail) +#define nullable(t) checkaux(t, PEnullable) + +#define fixedlen(t) fixedlenx(t, 0, 0) + + + +#endif diff --git a/3rd/lpeg/lpeg-128.gif b/3rd/lpeg/lpeg-128.gif new file mode 100644 index 0000000000000000000000000000000000000000..bbf5e78b88e71044bd341a2476dcec756c252ae4 GIT binary patch literal 4923 zcmV-B6U6LCNk%w1VSoUD0O$Vz004pj0D}Plga8JE00)Es2!#L%h6@FN4hDe`2Z9p_ zgBS~j4iktF6p0fRixn1(7#EEV9grItj}#!2B_EC`A(0;@k|8FOBPWz3D3vEFmoh4o zEH0HaE0iuTmM}1wGBTPiH=Q&zn>I9^I5?R*I-5N^oj*OFK|Y~7LZduGq((%cN=Kwi zNTp0krbkVwPfn#$P^VK-s8Cm}SXil9S*luEt6N*FU0kkTUa(+av0-4cV`8*qWwB*u zvSw$qXK1u&X|-!>xNL2?b8fYCZ?|u9xN>&8et>;?c)EXqet?31e0sZqgMfW|yn=** zetf-yg@S~JgMfa&g@=TJf53)_g@S;>eS^Y?iiduL#EFZDij0Ybg};S{!G?#zjgO3u zkd26m#E+4Vk&}>%jK`9ck&TVXl$Mi{j>MIhl$My4l#s@knULz#>wW(&B@2g=gP|A&Ckos%;V0`%+1Z^ z&(Y4%($CP(=h4&9(a`A9)X~$`($doD)YjA0*VNS1>(Dk-Y+}P^d z-P+yR>)qen-`nlp;N9Qh-r?Qv+~Dxw;@{oj^5Nv*gVX`^Xcp8>g?(2>-FpI>g??I?eFXD?fCBS?e6dS^6&KU^6&HU_3`uY z^7Qfb^Y`=h^7i!j_4oAn_4@ht`}g_w`uF_!`uF?z{rUU&{QCa-{Q39&|NH&=`2PR- z|Ns2|`~Ls@A^sIZa%Ew3Wn>_CX>@2HRA^-&M@dak04x9i004jhfB*mp{s8|897wRB z!Gj1BDqP60p~Hs|BTAe|v7*I`7&B_z$g!ixk03*e97(dI$&)Bks$9vkrAvX8`q*is z=H9?{l}7E#R?lNQWVLv4P~jVh?hctKNEB_v)Q`AA0{He1AFMltl|F;_};VY9{( zl?>#^V#;+GAcx6`MS^o5VWY+-eH4NZB-lh!2s*%^Q%D=Wpb`p8b@@WsJ{k14+ee}#bO3Jby=YfFj5SY4>pSIg$+7yAlZ_3|IG4ZYXE{+9&0^>gij@c?1K+BVN7L+ zBE8Uo%|6)(0!cRW6;xLjK86S4czYfI0f`*plLsohjDgNS_Sl1qC+O^h7nVis14DTp z_UB)a2aE$!UFv{w2Rn||WsjqsJvIKEv42Q>pl%atELe@uN%~I9w1O zV#cKwqF5Br5C<|5?UMqf)N;9JfW`!>inerBLQX{d@Zv-T1n>!#kN^gl*Z>n8VQoY3 z(2(b=$RW((0nJ{E&n0%)GnFrb*aOi!dZ>EwmKPuA-(diH5QjPo?Sn_Ot)`4{2&4u| zi5>X#vc@|Pm2*V{Hk> zUea4{!V6#lG+X(=U@xX82l*yQ_Mq|()&=on3I1y8a!FpoM(dfd;ENeIK_h^&Gs~rB ze+Z%xxoV7eLF3GDu*IvosxqH_3Q1lJ+7QIgKJX+#E#Y5=#eg~BqQnUWDt8{D=Uq;V z^2y3UfJs5~SVO|AMgOh9Et8kT&lkZ@Og7=1v+8-hPU!OwKSbrD&ZN!j>DnnC<>!z* zG)ODE?Sb1ae)*}z&<;WKbYq0zw1-6kG&yU8&KTf0qz(!|(!R3e%v)Z)l0!8SIgKdz zAl)pm2fqVY01xT-NFUTtyn%Ql1cS4g-;P#54oZsx9Dr8riUWWSgl-_DQ%5fFKm!tx zfCDf{!YSZkrq)%Y7fYDXLewFG+4=8p{#C;r5dUL@Q)B}kNUR4m@IZpgsfz)P9XKO zA#YG24~Fam2W9i0hvI+ zxvPeBB zAhiK7fs~m51VyG|reVGVb`)i)4;g1Z07-MK`%smD0KkAzBE*$Xh(}27fd_t)5lAb= zC^LOTii^Mlc7p^!92R1aC+uQV`1oDm{#LVS<>`m4S?pN58{!Sg z3Yf$bC|+;1>5vMx59`A$vQ)O9%0#-0JDTX}~|6we0k1su%wk%kT$H+31jtRN*_Gd{y0P{w};~v8-U2rC7tb7gCRr(~?620NOrIO7IL?U>1y))@n!)idpEHy-C1^JMKXa zE4pJFr&z(+hC72{ZsH9b*@q9@`@D5u+{v`$06C_3G!Cr*i|QQ@19qX0hr`1izpV0j zPP|P@TxeV;(fuIL1lvk9=ffYQ>oWzl|X^V`!%xrtkwFG@+?~+yfjw zHuiELw@pP90wUt@CPM1_JgNae9izK?pQ=oPeC*>MdItauY=R9R(O@8qfB<5`Qh+1~ zgda0Ya?!frJ+@>5I7Q9`)Dr@aLlii6c85oBQ*TBQ)Fm4nUd+AgF;hJ;f+)zCy?_S( z;doLsSY?EmoMwuQ3oA@+AGKJr=LTT)=u`Uue8)#VHhzG6@1ymtIR!uTfraKOMhMfh zIIR*mZ2u(ObWz8~bogYPA~dClKvcHtz?2Q;`qwWr6>2f4&w-sqtZ1;(;GR0eUnM_^=7# zM10|MJB4F+CN+Q*2LOEc58E($#$kU9CPc2m03<*IUocSh;C(z89!;kZ*$@j4;TgrV z55_k@E(m(;CW9{rg*7-p^_PQThbc6G2ea@Ek`x>J@MQyb01$Q%_%I4)0C)Zip%1vA zgi8oCcn5(Qb^sy}g6MEQFn|yD01f>E1m3U@Y?MF((GH~WU0)c6%AsF(B?y;yEuE1M zEJ$^q7kc8uJTw@BWn_Q&w;V~}4+HTGeqaPJkO9P@0|N17pR^bjuu*K01=P_!zeIP4 z0W|IqRo*sPlQ<8R7(_J?g3xd=9$*>BpaL00jPO7Tx1=0jr4ZPFQxl;G`gRbblYyQ0 zc{$i?Bv>{h5`wi<9JZ%}uJLN;z*VV1DL7D!9P}S_h7j@q49!MLJEIW$;0`8pe0DdD zU6(unFp2qK4*e7s10V|ASRT@}Pw@tVv_&!H;d~VoaoZFOO%PHCq5g*RxEk#TSemmK zBa%Q|g)|f(Nv#(i(!>a~6SPm)1`ScBfzkz>&gLs$`YK?#|FHKjxpQPp|th7G4sm!v=nr*M~extB?J z0Njv+>M(I)!2p(k4~?f@hY?$|!U%gp0&jJHwImiv(0d0l4U`FDEa`aG5S1xocbsK< z^?*d82@gdF59y$l0B{5XQ4ilR3vmz!mN0bsa02qj4B~+R&rnMBunm@g2aEs>Q$Y`u z_795F1yLCw3=j@NrUc0#5~WmMmm?%@*bw_b1ph$@cr*`4{zMexzybnLD+o0J9FQ5K zlY=>k572M|3g8Vn7@8Bg7`(R-qre4-C=uPjHw~x)#liyo*`NODpApa{H}DGUG!Alr zSS+vtN~bUYumESEL-?QzMer2^-~#OV59$Cd0n(EYH3t4A64g_d6?J?tM{xb202?3z z6o3E#ggeCs02AN=9l!uDDi#Uw0UW@YV!;Rvi5I)@1UYpP!=MJ2Nf7lQ4$}s6GdYi{ z<%0f4b%#+LsODL$@uDtB0|_!rOH!Y@p$<=gPj)gr`OpnJ*=;1cY7nSWYUdiomRN(u z04c~8T*?-ZUwjM}J67H^OmHO8{QqTiMR`O^^SKnEsRUcs;gzMv3`G&<>E zVI4_6bT~CA1*rc41L`1K@@G?*UU>__s+(~MR1l}8G7$d24&Cs8(JHh4>Q{8t1@GV}i_}V` z&;-B$Q!CL74RIEy_YKAwi5InW3>X$&@C_1y4TdT*EOCEJun7@?5AZ+@Ohjtt%33r- z0(fw&)HAnpiVQD z;$RFcG*N(8HXeru&`=Ne@VT=^s-{vQ$50WT7@A154YR-qX6HyFSpawx2y#yf+&3m>5 zA-ff!3QZ6RZDzhq@dj$(3>ty6M2HT);9~NN7w7;8OP~mLB)>h|zjhJ~9vZ#2V1@>~ zQ1(!*v*`|<=<;JBG7!h4Gs!GHu(kO!Hdc@Q%Vv4j_w z013X}F-EME>aYut@CLcS8Iw>5ia-gT;62^&23w#6N)QQmAu2e0#n9EWO@Ro4kOz0b t87`~}*f6ke{1|$%8+P%%7keDYf;`BCT*!ue$cUWCioD2-9K8Vn06SzG6$t + + + LPeg - Parsing Expression Grammars For Lua + + + + + + + +

+ +
+ +
LPeg
+
+ Parsing Expression Grammars For Lua, version 0.12 +
+
+ +
+ + + +
+ + +

Introduction

+ +

+LPeg is a new pattern-matching library for Lua, +based on + +Parsing Expression Grammars (PEGs). +This text is a reference manual for the library. +For a more formal treatment of LPeg, +as well as some discussion about its implementation, +see + +A Text Pattern-Matching Tool based on Parsing Expression Grammars. +(You may also be interested in my +talk about LPeg +given at the III Lua Workshop.) +

+ +

+Following the Snobol tradition, +LPeg defines patterns as first-class objects. +That is, patterns are regular Lua values +(represented by userdata). +The library offers several functions to create +and compose patterns. +With the use of metamethods, +several of these functions are provided as infix or prefix +operators. +On the one hand, +the result is usually much more verbose than the typical +encoding of patterns using the so called +regular expressions +(which typically are not regular expressions in the formal sense). +On the other hand, +first-class patterns allow much better documentation +(as it is easy to comment the code, +to break complex definitions in smaller parts, etc.) +and are extensible, +as we can define new functions to create and compose patterns. +

+ +

+For a quick glance of the library, +the following table summarizes its basic operations +for creating patterns: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
OperatorDescription
lpeg.P(string)Matches string literally
lpeg.P(n)Matches exactly n characters
lpeg.S(string)Matches any character in string (Set)
lpeg.R("xy")Matches any character between x and y (Range)
patt^nMatches at least n repetitions of patt
patt^-nMatches at most n repetitions of patt
patt1 * patt2Matches patt1 followed by patt2
patt1 + patt2Matches patt1 or patt2 + (ordered choice)
patt1 - patt2Matches patt1 if patt2 does not match
-pattEquivalent to ("" - patt)
#pattMatches patt but consumes no input
lpeg.B(patt)Matches patt behind the current position, + consuming no input
+ +

As a very simple example, +lpeg.R("09")^1 creates a pattern that +matches a non-empty sequence of digits. +As a not so simple example, +-lpeg.P(1) +(which can be written as lpeg.P(-1), +or simply -1 for operations expecting a pattern) +matches an empty string only if it cannot match a single character; +so, it succeeds only at the end of the subject. +

+ +

+LPeg also offers the re module, +which implements patterns following a regular-expression style +(e.g., [09]+). +(This module is 260 lines of Lua code, +and of course it uses LPeg to parse regular expressions and +translate them to regular LPeg patterns.) +

+ + +

Functions

+ + +

lpeg.match (pattern, subject [, init])

+

+The matching function. +It attempts to match the given pattern against the subject string. +If the match succeeds, +returns the index in the subject of the first character after the match, +or the captured values +(if the pattern captured any value). +

+ +

+An optional numeric argument init makes the match +start at that position in the subject string. +As usual in Lua libraries, +a negative value counts from the end. +

+ +

+Unlike typical pattern-matching functions, +match works only in anchored mode; +that is, it tries to match the pattern with a prefix of +the given subject string (at position init), +not with an arbitrary substring of the subject. +So, if we want to find a pattern anywhere in a string, +we must either write a loop in Lua or write a pattern that +matches anywhere. +This second approach is easy and quite efficient; +see examples. +

+ +

lpeg.type (value)

+

+If the given value is a pattern, +returns the string "pattern". +Otherwise returns nil. +

+ +

lpeg.version ()

+

+Returns a string with the running version of LPeg. +

+ +

lpeg.setmaxstack (max)

+

+Sets the maximum size for the backtrack stack used by LPeg to +track calls and choices. +Most well-written patterns need little backtrack levels and +therefore you seldom need to change this maximum; +but a few useful patterns may need more space. +Before changing this maximum you should try to rewrite your +pattern to avoid the need for extra space. +

+ + +

Basic Constructions

+ +

+The following operations build patterns. +All operations that expect a pattern as an argument +may receive also strings, tables, numbers, booleans, or functions, +which are translated to patterns according to +the rules of function lpeg.P. +

+ + + +

lpeg.P (value)

+

+Converts the given value into a proper pattern, +according to the following rules: +

+
    + +
  • +If the argument is a pattern, +it is returned unmodified. +

  • + +
  • +If the argument is a string, +it is translated to a pattern that matches the string literally. +

  • + +
  • +If the argument is a non-negative number n, +the result is a pattern that matches exactly n characters. +

  • + +
  • +If the argument is a negative number -n, +the result is a pattern that +succeeds only if the input string has less than n characters left: +lpeg.P(-n) +is equivalent to -lpeg.P(n) +(see the unary minus operation). +

  • + +
  • +If the argument is a boolean, +the result is a pattern that always succeeds or always fails +(according to the boolean value), +without consuming any input. +

  • + +
  • +If the argument is a table, +it is interpreted as a grammar +(see Grammars). +

  • + +
  • +If the argument is a function, +returns a pattern equivalent to a +match-time capture over the empty string. +

  • + +
+ + +

lpeg.B(patt)

+

+Returns a pattern that +matches only if the input string at the current position +is preceded by patt. +Pattern patt must match only strings +with some fixed length, +and it cannot contain captures. +

+ +

+Like the and predicate, +this pattern never consumes any input, +independently of success or failure. +

+ + +

lpeg.R ({range})

+

+Returns a pattern that matches any single character +belonging to one of the given ranges. +Each range is a string xy of length 2, +representing all characters with code +between the codes of x and y +(both inclusive). +

+ +

+As an example, the pattern +lpeg.R("09") matches any digit, +and lpeg.R("az", "AZ") matches any ASCII letter. +

+ + +

lpeg.S (string)

+

+Returns a pattern that matches any single character that +appears in the given string. +(The S stands for Set.) +

+ +

+As an example, the pattern +lpeg.S("+-*/") matches any arithmetic operator. +

+ +

+Note that, if s is a character +(that is, a string of length 1), +then lpeg.P(s) is equivalent to lpeg.S(s) +which is equivalent to lpeg.R(s..s). +Note also that both lpeg.S("") and lpeg.R() +are patterns that always fail. +

+ + +

lpeg.V (v)

+

+This operation creates a non-terminal (a variable) +for a grammar. +The created non-terminal refers to the rule indexed by v +in the enclosing grammar. +(See Grammars for details.) +

+ + +

lpeg.locale ([table])

+

+Returns a table with patterns for matching some character classes +according to the current locale. +The table has fields named +alnum, +alpha, +cntrl, +digit, +graph, +lower, +print, +punct, +space, +upper, and +xdigit, +each one containing a correspondent pattern. +Each pattern matches any single character that belongs to its class. +

+ +

+If called with an argument table, +then it creates those fields inside the given table and +returns that table. +

+ + +

#patt

+

+Returns a pattern that +matches only if the input string matches patt, +but without consuming any input, +independently of success or failure. +(This pattern is called an and predicate +and it is equivalent to +&patt in the original PEG notation.) +

+ + +

+This pattern never produces any capture. +

+ + +

-patt

+

+Returns a pattern that +matches only if the input string does not match patt. +It does not consume any input, +independently of success or failure. +(This pattern is equivalent to +!patt in the original PEG notation.) +

+ +

+As an example, the pattern +-lpeg.P(1) matches only the end of string. +

+ +

+This pattern never produces any captures, +because either patt fails +or -patt fails. +(A failing pattern never produces captures.) +

+ + +

patt1 + patt2

+

+Returns a pattern equivalent to an ordered choice +of patt1 and patt2. +(This is denoted by patt1 / patt2 in the original PEG notation, +not to be confused with the / operation in LPeg.) +It matches either patt1 or patt2, +with no backtracking once one of them succeeds. +The identity element for this operation is the pattern +lpeg.P(false), +which always fails. +

+ +

+If both patt1 and patt2 are +character sets, +this operation is equivalent to set union. +

+
+lower = lpeg.R("az")
+upper = lpeg.R("AZ")
+letter = lower + upper
+
+ + +

patt1 - patt2

+

+Returns a pattern equivalent to !patt2 patt1. +This pattern asserts that the input does not match +patt2 and then matches patt1. +

+ +

+When successful, +this pattern produces all captures from patt1. +It never produces any capture from patt2 +(as either patt2 fails or +patt1 - patt2 fails). +

+ +

+If both patt1 and patt2 are +character sets, +this operation is equivalent to set difference. +Note that -patt is equivalent to "" - patt +(or 0 - patt). +If patt is a character set, +1 - patt is its complement. +

+ + +

patt1 * patt2

+

+Returns a pattern that matches patt1 +and then matches patt2, +starting where patt1 finished. +The identity element for this operation is the +pattern lpeg.P(true), +which always succeeds. +

+ +

+(LPeg uses the * operator +[instead of the more obvious ..] +both because it has +the right priority and because in formal languages it is +common to use a dot for denoting concatenation.) +

+ + +

patt^n

+

+If n is nonnegative, +this pattern is +equivalent to pattn patt*: +It matches n or more occurrences of patt. +

+ +

+Otherwise, when n is negative, +this pattern is equivalent to (patt?)-n: +It matches at most |n| +occurrences of patt. +

+ +

+In particular, patt^0 is equivalent to patt*, +patt^1 is equivalent to patt+, +and patt^-1 is equivalent to patt? +in the original PEG notation. +

+ +

+In all cases, +the resulting pattern is greedy with no backtracking +(also called a possessive repetition). +That is, it matches only the longest possible sequence +of matches for patt. +

+ + + +

Grammars

+ +

+With the use of Lua variables, +it is possible to define patterns incrementally, +with each new pattern using previously defined ones. +However, this technique does not allow the definition of +recursive patterns. +For recursive patterns, +we need real grammars. +

+ +

+LPeg represents grammars with tables, +where each entry is a rule. +

+ +

+The call lpeg.V(v) +creates a pattern that represents the nonterminal +(or variable) with index v in a grammar. +Because the grammar still does not exist when +this function is evaluated, +the result is an open reference to the respective rule. +

+ +

+A table is fixed when it is converted to a pattern +(either by calling lpeg.P or by using it wherein a +pattern is expected). +Then every open reference created by lpeg.V(v) +is corrected to refer to the rule indexed by v in the table. +

+ +

+When a table is fixed, +the result is a pattern that matches its initial rule. +The entry with index 1 in the table defines its initial rule. +If that entry is a string, +it is assumed to be the name of the initial rule. +Otherwise, LPeg assumes that the entry 1 itself is the initial rule. +

+ +

+As an example, +the following grammar matches strings of a's and b's that +have the same number of a's and b's: +

+
+equalcount = lpeg.P{
+  "S";   -- initial rule name
+  S = "a" * lpeg.V"B" + "b" * lpeg.V"A" + "",
+  A = "a" * lpeg.V"S" + "b" * lpeg.V"A" * lpeg.V"A",
+  B = "b" * lpeg.V"S" + "a" * lpeg.V"B" * lpeg.V"B",
+} * -1
+
+

+It is equivalent to the following grammar in standard PEG notation: +

+
+  S <- 'a' B / 'b' A / ''
+  A <- 'a' S / 'b' A A
+  B <- 'b' S / 'a' B B
+
+ + +

Captures

+ +

+A capture is a pattern that creates values +(the so called semantic information) when it matches. +LPeg offers several kinds of captures, +which produces values based on matches and combine these values to +produce new values. +Each capture may produce zero or more values. +

+ +

+The following table summarizes the basic captures: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OperationWhat it Produces
lpeg.C(patt)the match for patt plus all captures + made by patt
lpeg.Carg(n)the value of the nth extra argument to + lpeg.match (matches the empty string)
lpeg.Cb(name)the values produced by the previous + group capture named name + (matches the empty string)
lpeg.Cc(values)the given values (matches the empty string)
lpeg.Cf(patt, func)a folding of the captures from patt
lpeg.Cg(patt [, name])the values produced by patt, + optionally tagged with name
lpeg.Cp()the current position (matches the empty string)
lpeg.Cs(patt)the match for patt + with the values from nested captures replacing their matches
lpeg.Ct(patt)a table with all captures from patt
patt / stringstring, with some marks replaced by captures + of patt
patt / numberthe n-th value captured by patt, +or no value when number is zero.
patt / tabletable[c], where c is the (first) + capture of patt
patt / functionthe returns of function applied to the captures + of patt
lpeg.Cmt(patt, function)the returns of function applied to the captures + of patt; the application is done at match time
+ +

+A capture pattern produces its values every time it succeeds. +For instance, +a capture inside a loop produces as many values as matched by the loop. +A capture produces a value only when it succeeds. +For instance, +the pattern lpeg.C(lpeg.P"a"^-1) +produces the empty string when there is no "a" +(because the pattern "a"? succeeds), +while the pattern lpeg.C("a")^-1 +does not produce any value when there is no "a" +(because the pattern "a" fails). +

+ +

+Usually, +LPeg evaluates all captures only after (and if) the entire match succeeds. +During match time it only gathers enough information +to produce the capture values later. +As a particularly important consequence, +most captures cannot affect the way a pattern matches a subject. +The only exception to this rule is the +so-called match-time capture. +When a match-time capture matches, +it forces the immediate evaluation of all its nested captures +and then calls its corresponding function, +which defines whether the match succeeds and also +what values are produced. +

+ +

lpeg.C (patt)

+

+Creates a simple capture, +which captures the substring of the subject that matches patt. +The captured value is a string. +If patt has other captures, +their values are returned after this one. +

+ + +

lpeg.Carg (n)

+

+Creates an argument capture. +This pattern matches the empty string and +produces the value given as the nth extra +argument given in the call to lpeg.match. +

+ + +

lpeg.Cb (name)

+

+Creates a back capture. +This pattern matches the empty string and +produces the values produced by the most recent +group capture named name. +

+ +

+Most recent means the last +complete +outermost +group capture with the given name. +A Complete capture means that the entire pattern +corresponding to the capture has matched. +An Outermost capture means that the capture is not inside +another complete capture. +

+ + +

lpeg.Cc ([value, ...])

+

+Creates a constant capture. +This pattern matches the empty string and +produces all given values as its captured values. +

+ + +

lpeg.Cf (patt, func)

+

+Creates a fold capture. +If patt produces a list of captures +C1 C2 ... Cn, +this capture will produce the value +func(...func(func(C1, C2), C3)..., + Cn), +that is, it will fold +(or accumulate, or reduce) +the captures from patt using function func. +

+ +

+This capture assumes that patt should produce +at least one capture with at least one value (of any type), +which becomes the initial value of an accumulator. +(If you need a specific initial value, +you may prefix a constant capture to patt.) +For each subsequent capture, +LPeg calls func +with this accumulator as the first argument and all values produced +by the capture as extra arguments; +the first result from this call +becomes the new value for the accumulator. +The final value of the accumulator becomes the captured value. +

+ +

+As an example, +the following pattern matches a list of numbers separated +by commas and returns their addition: +

+
+-- matches a numeral and captures its numerical value
+number = lpeg.R"09"^1 / tonumber
+
+-- matches a list of numbers, capturing their values
+list = number * ("," * number)^0
+
+-- auxiliary function to add two numbers
+function add (acc, newvalue) return acc + newvalue end
+
+-- folds the list of numbers adding them
+sum = lpeg.Cf(list, add)
+
+-- example of use
+print(sum:match("10,30,43"))   --> 83
+
+ + +

lpeg.Cg (patt [, name])

+

+Creates a group capture. +It groups all values returned by patt +into a single capture. +The group may be anonymous (if no name is given) +or named with the given name. +

+ +

+An anonymous group serves to join values from several captures into +a single capture. +A named group has a different behavior. +In most situations, a named group returns no values at all. +Its values are only relevant for a following +back capture or when used +inside a table capture. +

+ + +

lpeg.Cp ()

+

+Creates a position capture. +It matches the empty string and +captures the position in the subject where the match occurs. +The captured value is a number. +

+ + +

lpeg.Cs (patt)

+

+Creates a substitution capture, +which captures the substring of the subject that matches patt, +with substitutions. +For any capture inside patt with a value, +the substring that matched the capture is replaced by the capture value +(which should be a string). +The final captured value is the string resulting from +all replacements. +

+ + +

lpeg.Ct (patt)

+

+Creates a table capture. +This capture creates a table and puts all values from all anonymous captures +made by patt inside this table in successive integer keys, +starting at 1. +Moreover, +for each named capture group created by patt, +the first value of the group is put into the table +with the group name as its key. +The captured value is only the table. +

+ + +

patt / string

+

+Creates a string capture. +It creates a capture string based on string. +The captured value is a copy of string, +except that the character % works as an escape character: +any sequence in string of the form %n, +with n between 1 and 9, +stands for the match of the n-th capture in patt. +The sequence %0 stands for the whole match. +The sequence %% stands for a single %. +

+ + +

patt / number

+

+Creates a numbered capture. +For a non-zero number, +the captured value is the n-th value +captured by patt. +When number is zero, +there are no captured values. +

+ + +

patt / table

+

+Creates a query capture. +It indexes the given table using as key the first value captured by +patt, +or the whole match if patt produced no value. +The value at that index is the final value of the capture. +If the table does not have that key, +there is no captured value. +

+ + +

patt / function

+

+Creates a function capture. +It calls the given function passing all captures made by +patt as arguments, +or the whole match if patt made no capture. +The values returned by the function +are the final values of the capture. +In particular, +if function returns no value, +there is no captured value. +

+ + +

lpeg.Cmt(patt, function)

+

+Creates a match-time capture. +Unlike all other captures, +this one is evaluated immediately when a match occurs. +It forces the immediate evaluation of all its nested captures +and then calls function. +

+ +

+The given function gets as arguments the entire subject, +the current position (after the match of patt), +plus any capture values produced by patt. +

+ +

+The first value returned by function +defines how the match happens. +If the call returns a number, +the match succeeds +and the returned number becomes the new current position. +(Assuming a subject s and current position i, +the returned number must be in the range [i, len(s) + 1].) +If the call returns true, +the match succeeds without consuming any input. +(So, to return true is equivalent to return i.) +If the call returns false, nil, or no value, +the match fails. +

+ +

+Any extra values returned by the function become the +values produced by the capture. +

+ + + + +

Some Examples

+ +

Using a Pattern

+

+This example shows a very simple but complete program +that builds and uses a pattern: +

+
+local lpeg = require "lpeg"
+
+-- matches a word followed by end-of-string
+p = lpeg.R"az"^1 * -1
+
+print(p:match("hello"))        --> 6
+print(lpeg.match(p, "hello"))  --> 6
+print(p:match("1 hello"))      --> nil
+
+

+The pattern is simply a sequence of one or more lower-case letters +followed by the end of string (-1). +The program calls match both as a method +and as a function. +In both sucessful cases, +the match returns +the index of the first character after the match, +which is the string length plus one. +

+ + +

Name-value lists

+

+This example parses a list of name-value pairs and returns a table +with those pairs: +

+
+lpeg.locale(lpeg)   -- adds locale entries into 'lpeg' table
+
+local space = lpeg.space^0
+local name = lpeg.C(lpeg.alpha^1) * space
+local sep = lpeg.S(",;") * space
+local pair = lpeg.Cg(name * "=" * space * name) * sep^-1
+local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset)
+t = list:match("a=b, c = hi; next = pi")  --> { a = "b", c = "hi", next = "pi" }
+
+

+Each pair has the format name = name followed by +an optional separator (a comma or a semicolon). +The pair pattern encloses the pair in a group pattern, +so that the names become the values of a single capture. +The list pattern then folds these captures. +It starts with an empty table, +created by a table capture matching an empty string; +then for each capture (a pair of names) it applies rawset +over the accumulator (the table) and the capture values (the pair of names). +rawset returns the table itself, +so the accumulator is always the table. +

+ +

Splitting a string

+

+The following code builds a pattern that +splits a string using a given pattern +sep as a separator: +

+
+function split (s, sep)
+  sep = lpeg.P(sep)
+  local elem = lpeg.C((1 - sep)^0)
+  local p = elem * (sep * elem)^0
+  return lpeg.match(p, s)
+end
+
+

+First the function ensures that sep is a proper pattern. +The pattern elem is a repetition of zero of more +arbitrary characters as long as there is not a match against +the separator. +It also captures its match. +The pattern p matches a list of elements separated +by sep. +

+ +

+If the split results in too many values, +it may overflow the maximum number of values +that can be returned by a Lua function. +In this case, +we can collect these values in a table: +

+
+function split (s, sep)
+  sep = lpeg.P(sep)
+  local elem = lpeg.C((1 - sep)^0)
+  local p = lpeg.Ct(elem * (sep * elem)^0)   -- make a table capture
+  return lpeg.match(p, s)
+end
+
+ + +

Searching for a pattern

+

+The primitive match works only in anchored mode. +If we want to find a pattern anywhere in a string, +we must write a pattern that matches anywhere. +

+ +

+Because patterns are composable, +we can write a function that, +given any arbitrary pattern p, +returns a new pattern that searches for p +anywhere in a string. +There are several ways to do the search. +One way is like this: +

+
+function anywhere (p)
+  return lpeg.P{ p + 1 * lpeg.V(1) }
+end
+
+

+This grammar has a straight reading: +it matches p or skips one character and tries again. +

+ +

+If we want to know where the pattern is in the string +(instead of knowing only that it is there somewhere), +we can add position captures to the pattern: +

+
+local I = lpeg.Cp()
+function anywhere (p)
+  return lpeg.P{ I * p * I + 1 * lpeg.V(1) }
+end
+
+print(anywhere("world"):match("hello world!"))   -> 7   12
+
+ +

+Another option for the search is like this: +

+
+local I = lpeg.Cp()
+function anywhere (p)
+  return (1 - lpeg.P(p))^0 * I * p * I
+end
+
+

+Again the pattern has a straight reading: +it skips as many characters as possible while not matching p, +and then matches p (plus appropriate captures). +

+ +

+If we want to look for a pattern only at word boundaries, +we can use the following transformer: +

+ +
+local t = lpeg.locale()
+
+function atwordboundary (p)
+  return lpeg.P{
+    [1] = p + t.alpha^0 * (1 - t.alpha)^1 * lpeg.V(1)
+  }
+end
+
+ + +

Balanced parentheses

+

+The following pattern matches only strings with balanced parentheses: +

+
+b = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
+
+

+Reading the first (and only) rule of the given grammar, +we have that a balanced string is +an open parenthesis, +followed by zero or more repetitions of either +a non-parenthesis character or +a balanced string (lpeg.V(1)), +followed by a closing parenthesis. +

+ + +

Global substitution

+

+The next example does a job somewhat similar to string.gsub. +It receives a pattern and a replacement value, +and substitutes the replacement value for all occurrences of the pattern +in a given string: +

+
+function gsub (s, patt, repl)
+  patt = lpeg.P(patt)
+  patt = lpeg.Cs((patt / repl + 1)^0)
+  return lpeg.match(patt, s)
+end
+
+

+As in string.gsub, +the replacement value can be a string, +a function, or a table. +

+ + +

Comma-Separated Values (CSV)

+

+This example breaks a string into comma-separated values, +returning all fields: +

+
+local field = '"' * lpeg.Cs(((lpeg.P(1) - '"') + lpeg.P'""' / '"')^0) * '"' +
+                    lpeg.C((1 - lpeg.S',\n"')^0)
+
+local record = field * (',' * field)^0 * (lpeg.P'\n' + -1)
+
+function csv (s)
+  return lpeg.match(record, s)
+end
+
+

+A field is either a quoted field +(which may contain any character except an individual quote, +which may be written as two quotes that are replaced by one) +or an unquoted field +(which cannot contain commas, newlines, or quotes). +A record is a list of fields separated by commas, +ending with a newline or the string end (-1). +

+ +

+As it is, +the previous pattern returns each field as a separated result. +If we add a table capture in the definition of record, +the pattern will return instead a single table +containing all fields: +

+
+local record = lpeg.Ct(field * (',' * field)^0) * (lpeg.P'\n' + -1)
+
+ + +

UTF-8 and Latin 1

+

+It is not difficult to use LPeg to convert a string from +UTF-8 encoding to Latin 1 (ISO 8859-1): +

+ +
+-- convert a two-byte UTF-8 sequence to a Latin 1 character
+local function f2 (s)
+  local c1, c2 = string.byte(s, 1, 2)
+  return string.char(c1 * 64 + c2 - 12416)
+end
+
+local utf8 = lpeg.R("\0\127")
+           + lpeg.R("\194\195") * lpeg.R("\128\191") / f2
+
+local decode_pattern = lpeg.Cs(utf8^0) * -1
+
+

+In this code, +the definition of UTF-8 is already restricted to the +Latin 1 range (from 0 to 255). +Any encoding outside this range (as well as any invalid encoding) +will not match that pattern. +

+ +

+As the definition of decode_pattern demands that +the pattern matches the whole input (because of the -1 at its end), +any invalid string will simply fail to match, +without any useful information about the problem. +We can improve this situation redefining decode_pattern +as follows: +

+
+local function er (_, i) error("invalid encoding at position " .. i) end
+
+local decode_pattern = lpeg.Cs(utf8^0) * (-1 + lpeg.P(er))
+
+

+Now, if the pattern utf8^0 stops +before the end of the string, +an appropriate error function is called. +

+ + +

UTF-8 and Unicode

+

+We can extend the previous patterns to handle all Unicode code points. +Of course, +we cannot translate them to Latin 1 or any other one-byte encoding. +Instead, our translation results in a array with the code points +represented as numbers. +The full code is here: +

+
+-- decode a two-byte UTF-8 sequence
+local function f2 (s)
+  local c1, c2 = string.byte(s, 1, 2)
+  return c1 * 64 + c2 - 12416
+end
+
+-- decode a three-byte UTF-8 sequence
+local function f3 (s)
+  local c1, c2, c3 = string.byte(s, 1, 3)
+  return (c1 * 64 + c2) * 64 + c3 - 925824
+end
+
+-- decode a four-byte UTF-8 sequence
+local function f4 (s)
+  local c1, c2, c3, c4 = string.byte(s, 1, 4)
+  return ((c1 * 64 + c2) * 64 + c3) * 64 + c4 - 63447168
+end
+
+local cont = lpeg.R("\128\191")   -- continuation byte
+
+local utf8 = lpeg.R("\0\127") / string.byte
+           + lpeg.R("\194\223") * cont / f2
+           + lpeg.R("\224\239") * cont * cont / f3
+           + lpeg.R("\240\244") * cont * cont * cont / f4
+
+local decode_pattern = lpeg.Ct(utf8^0) * -1
+
+ + +

Lua's long strings

+

+A long string in Lua starts with the pattern [=*[ +and ends at the first occurrence of ]=*] with +exactly the same number of equal signs. +If the opening brackets are followed by a newline, +this newline is discarded +(that is, it is not part of the string). +

+ +

+To match a long string in Lua, +the pattern must capture the first repetition of equal signs and then, +whenever it finds a candidate for closing the string, +check whether it has the same number of equal signs. +

+ +
+equals = lpeg.P"="^0
+open = "[" * lpeg.Cg(equals, "init") * "[" * lpeg.P"\n"^-1
+close = "]" * lpeg.C(equals) * "]"
+closeeq = lpeg.Cmt(close * lpeg.Cb("init"), function (s, i, a, b) return a == b end)
+string = open * lpeg.C((lpeg.P(1) - closeeq)^0) * close / 1
+
+ +

+The open pattern matches [=*[, +capturing the repetitions of equal signs in a group named init; +it also discharges an optional newline, if present. +The close pattern matches ]=*], +also capturing the repetitions of equal signs. +The closeeq pattern first matches close; +then it uses a back capture to recover the capture made +by the previous open, +which is named init; +finally it uses a match-time capture to check +whether both captures are equal. +The string pattern starts with an open, +then it goes as far as possible until matching closeeq, +and then matches the final close. +The final numbered capture simply discards +the capture made by close. +

+ + +

Arithmetic expressions

+

+This example is a complete parser and evaluator for simple +arithmetic expressions. +We write it in two styles. +The first approach first builds a syntax tree and then +traverses this tree to compute the expression value: +

+
+-- Lexical Elements
+local Space = lpeg.S(" \n\t")^0
+local Number = lpeg.C(lpeg.P"-"^-1 * lpeg.R("09")^1) * Space
+local TermOp = lpeg.C(lpeg.S("+-")) * Space
+local FactorOp = lpeg.C(lpeg.S("*/")) * Space
+local Open = "(" * Space
+local Close = ")" * Space
+
+-- Grammar
+local Exp, Term, Factor = lpeg.V"Exp", lpeg.V"Term", lpeg.V"Factor"
+G = lpeg.P{ Exp,
+  Exp = lpeg.Ct(Term * (TermOp * Term)^0);
+  Term = lpeg.Ct(Factor * (FactorOp * Factor)^0);
+  Factor = Number + Open * Exp * Close;
+}
+
+G = Space * G * -1
+
+-- Evaluator
+function eval (x)
+  if type(x) == "string" then
+    return tonumber(x)
+  else
+    local op1 = eval(x[1])
+    for i = 2, #x, 2 do
+      local op = x[i]
+      local op2 = eval(x[i + 1])
+      if (op == "+") then op1 = op1 + op2
+      elseif (op == "-") then op1 = op1 - op2
+      elseif (op == "*") then op1 = op1 * op2
+      elseif (op == "/") then op1 = op1 / op2
+      end
+    end
+    return op1
+  end
+end
+
+-- Parser/Evaluator
+function evalExp (s)
+  local t = lpeg.match(G, s)
+  if not t then error("syntax error", 2) end
+  return eval(t)
+end
+
+-- small example
+print(evalExp"3 + 5*9 / (1+1) - 12")   --> 13.5
+
+ +

+The second style computes the expression value on the fly, +without building the syntax tree. +The following grammar takes this approach. +(It assumes the same lexical elements as before.) +

+
+-- Auxiliary function
+function eval (v1, op, v2)
+  if (op == "+") then return v1 + v2
+  elseif (op == "-") then return v1 - v2
+  elseif (op == "*") then return v1 * v2
+  elseif (op == "/") then return v1 / v2
+  end
+end
+
+-- Grammar
+local V = lpeg.V
+G = lpeg.P{ "Exp",
+  Exp = lpeg.Cf(V"Term" * lpeg.Cg(TermOp * V"Term")^0, eval);
+  Term = lpeg.Cf(V"Factor" * lpeg.Cg(FactorOp * V"Factor")^0, eval);
+  Factor = Number / tonumber + Open * V"Exp" * Close;
+}
+
+-- small example
+print(lpeg.match(G, "3 + 5*9 / (1+1) - 12"))   --> 13.5
+
+

+Note the use of the fold (accumulator) capture. +To compute the value of an expression, +the accumulator starts with the value of the first term, +and then applies eval over +the accumulator, the operator, +and the new term for each repetition. +

+ + + +

Download

+ +

LPeg +source code.

+ + +

License

+ +

+Copyright © 2013 Lua.org, PUC-Rio. +

+

+Permission is hereby granted, free of charge, +to any person obtaining a copy of this software and +associated documentation files (the "Software"), +to deal in the Software without restriction, +including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, +and to permit persons to whom the Software is +furnished to do so, +subject to the following conditions: +

+ +

+The above copyright notice and this permission notice +shall be included in all copies or substantial portions of the Software. +

+ +

+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +

+ +
+ +
+ +
+

+$Id: lpeg.html,v 1.71 2013/04/11 19:17:41 roberto Exp $ +

+
+ +
+ + + diff --git a/3rd/lpeg/lpprint.c b/3rd/lpeg/lpprint.c new file mode 100644 index 00000000..05fa6488 --- /dev/null +++ b/3rd/lpeg/lpprint.c @@ -0,0 +1,244 @@ +/* +** $Id: lpprint.c,v 1.7 2013/04/12 16:29:49 roberto Exp $ +** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +*/ + +#include +#include +#include + + +#include "lptypes.h" +#include "lpprint.h" +#include "lpcode.h" + + +#if defined(LPEG_DEBUG) + +/* +** {====================================================== +** Printing patterns (for debugging) +** ======================================================= +*/ + + +void printcharset (const byte *st) { + int i; + printf("["); + for (i = 0; i <= UCHAR_MAX; i++) { + int first = i; + while (testchar(st, i) && i <= UCHAR_MAX) i++; + if (i - 1 == first) /* unary range? */ + printf("(%02x)", first); + else if (i - 1 > first) /* non-empty range? */ + printf("(%02x-%02x)", first, i - 1); + } + printf("]"); +} + + +static void printcapkind (int kind) { + const char *const modes[] = { + "close", "position", "constant", "backref", + "argument", "simple", "table", "function", + "query", "string", "num", "substitution", "fold", + "runtime", "group"}; + printf("%s", modes[kind]); +} + + +static void printjmp (const Instruction *op, const Instruction *p) { + printf("-> %d", (int)(p + (p + 1)->offset - op)); +} + + +static void printinst (const Instruction *op, const Instruction *p) { + const char *const names[] = { + "any", "char", "set", + "testany", "testchar", "testset", + "span", "behind", + "ret", "end", + "choice", "jmp", "call", "open_call", + "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", + "fullcapture", "opencapture", "closecapture", "closeruntime" + }; + printf("%02ld: %s ", (long)(p - op), names[p->i.code]); + switch ((Opcode)p->i.code) { + case IChar: { + printf("'%c'", p->i.aux); + break; + } + case ITestChar: { + printf("'%c'", p->i.aux); printjmp(op, p); + break; + } + case IFullCapture: { + printcapkind(getkind(p)); + printf(" (size = %d) (idx = %d)", getoff(p), p->i.key); + break; + } + case IOpenCapture: { + printcapkind(getkind(p)); + printf(" (idx = %d)", p->i.key); + break; + } + case ISet: { + printcharset((p+1)->buff); + break; + } + case ITestSet: { + printcharset((p+2)->buff); printjmp(op, p); + break; + } + case ISpan: { + printcharset((p+1)->buff); + break; + } + case IOpenCall: { + printf("-> %d", (p + 1)->offset); + break; + } + case IBehind: { + printf("%d", p->i.aux); + break; + } + case IJmp: case ICall: case ICommit: case IChoice: + case IPartialCommit: case IBackCommit: case ITestAny: { + printjmp(op, p); + break; + } + default: break; + } + printf("\n"); +} + + +void printpatt (Instruction *p, int n) { + Instruction *op = p; + while (p < op + n) { + printinst(op, p); + p += sizei(p); + } +} + + +#if defined(LPEG_DEBUG) +static void printcap (Capture *cap) { + printcapkind(cap->kind); + printf(" (idx: %d - size: %d) -> %p\n", cap->idx, cap->siz, cap->s); +} + + +void printcaplist (Capture *cap, Capture *limit) { + printf(">======\n"); + for (; cap->s && (limit == NULL || cap < limit); cap++) + printcap(cap); + printf("=======\n"); +} +#endif + +/* }====================================================== */ + + +/* +** {====================================================== +** Printing trees (for debugging) +** ======================================================= +*/ + +static const char *tagnames[] = { + "char", "set", "any", + "true", "false", + "rep", + "seq", "choice", + "not", "and", + "call", "opencall", "rule", "grammar", + "behind", + "capture", "run-time" +}; + + +void printtree (TTree *tree, int ident) { + int i; + for (i = 0; i < ident; i++) printf(" "); + printf("%s", tagnames[tree->tag]); + switch (tree->tag) { + case TChar: { + int c = tree->u.n; + if (isprint(c)) + printf(" '%c'\n", c); + else + printf(" (%02X)\n", c); + break; + } + case TSet: { + printcharset(treebuffer(tree)); + printf("\n"); + break; + } + case TOpenCall: case TCall: { + printf(" key: %d\n", tree->key); + break; + } + case TBehind: { + printf(" %d\n", tree->u.n); + printtree(sib1(tree), ident + 2); + break; + } + case TCapture: { + printf(" cap: %d key: %d n: %d\n", tree->cap, tree->key, tree->u.n); + printtree(sib1(tree), ident + 2); + break; + } + case TRule: { + printf(" n: %d key: %d\n", tree->cap, tree->key); + printtree(sib1(tree), ident + 2); + break; /* do not print next rule as a sibling */ + } + case TGrammar: { + TTree *rule = sib1(tree); + printf(" %d\n", tree->u.n); /* number of rules */ + for (i = 0; i < tree->u.n; i++) { + printtree(rule, ident + 2); + rule = sib2(rule); + } + assert(rule->tag == TTrue); /* sentinel */ + break; + } + default: { + int sibs = numsiblings[tree->tag]; + printf("\n"); + if (sibs >= 1) { + printtree(sib1(tree), ident + 2); + if (sibs >= 2) + printtree(sib2(tree), ident + 2); + } + break; + } + } +} + + +void printktable (lua_State *L, int idx) { + int n, i; + lua_getfenv(L, idx); + if (lua_isnil(L, -1)) /* no ktable? */ + return; + n = lua_objlen(L, -1); + printf("["); + for (i = 1; i <= n; i++) { + printf("%d = ", i); + lua_rawgeti(L, -1, i); + if (lua_isstring(L, -1)) + printf("%s ", lua_tostring(L, -1)); + else + printf("%s ", lua_typename(L, lua_type(L, -1))); + lua_pop(L, 1); + } + printf("]\n"); + /* leave ktable at the stack */ +} + +/* }====================================================== */ + +#endif diff --git a/3rd/lpeg/lpprint.h b/3rd/lpeg/lpprint.h new file mode 100644 index 00000000..e640f744 --- /dev/null +++ b/3rd/lpeg/lpprint.h @@ -0,0 +1,35 @@ +/* +** $Id: lpprint.h,v 1.1 2013/03/21 20:25:12 roberto Exp $ +*/ + + +#if !defined(lpprint_h) +#define lpprint_h + + +#include "lptree.h" +#include "lpvm.h" + + +#if defined(LPEG_DEBUG) + +void printpatt (Instruction *p, int n); +void printtree (TTree *tree, int ident); +void printktable (lua_State *L, int idx); +void printcharset (const byte *st); +void printcaplist (Capture *cap, Capture *limit); + +#else + +#define printktable(L,idx) \ + luaL_error(L, "function only implemented in debug mode") +#define printtree(tree,i) \ + luaL_error(L, "function only implemented in debug mode") +#define printpatt(p,n) \ + luaL_error(L, "function only implemented in debug mode") + +#endif + + +#endif + diff --git a/3rd/lpeg/lptree.c b/3rd/lpeg/lptree.c new file mode 100644 index 00000000..a5dfeb4a --- /dev/null +++ b/3rd/lpeg/lptree.c @@ -0,0 +1,1232 @@ +/* +** $Id: lptree.c,v 1.10 2013/04/12 16:30:33 roberto Exp $ +** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license) +*/ + +#include +#include +#include + + +#include "lua.h" +#include "lauxlib.h" + +#include "lptypes.h" +#include "lpcap.h" +#include "lpcode.h" +#include "lpprint.h" +#include "lptree.h" + + +/* number of siblings for each tree */ +const byte numsiblings[] = { + 0, 0, 0, /* char, set, any */ + 0, 0, /* true, false */ + 1, /* rep */ + 2, 2, /* seq, choice */ + 1, 1, /* not, and */ + 0, 0, 2, 1, /* call, opencall, rule, grammar */ + 1, /* behind */ + 1, 1 /* capture, runtime capture */ +}; + + +static TTree *newgrammar (lua_State *L, int arg); + + +/* +** returns a reasonable name for value at index 'idx' on the stack +*/ +static const char *val2str (lua_State *L, int idx) { + const char *k = lua_tostring(L, idx); + if (k != NULL) + return lua_pushfstring(L, "%s", k); + else + return lua_pushfstring(L, "(a %s)", luaL_typename(L, idx)); +} + + +/* +** Fix a TOpenCall into a TCall node, using table 'postable' to +** translate a key to its rule address in the tree. Raises an +** error if key does not exist. +*/ +static void fixonecall (lua_State *L, int postable, TTree *g, TTree *t) { + int n; + lua_rawgeti(L, -1, t->key); /* get rule's name */ + lua_gettable(L, postable); /* query name in position table */ + n = lua_tonumber(L, -1); /* get (absolute) position */ + lua_pop(L, 1); /* remove position */ + if (n == 0) { /* no position? */ + lua_rawgeti(L, -1, t->key); /* get rule's name again */ + luaL_error(L, "rule '%s' undefined in given grammar", val2str(L, -1)); + } + t->tag = TCall; + t->u.ps = n - (t - g); /* position relative to node */ + assert(sib2(t)->tag == TRule); + sib2(t)->key = t->key; +} + + +/* +** Transform left associative constructions into right +** associative ones, for sequence and choice; that is: +** (t11 + t12) + t2 => t11 + (t12 + t2) +** (t11 * t12) * t2 => t11 * (t12 * t2) +** (that is, Op (Op t11 t12) t2 => Op t11 (Op t12 t2)) +*/ +static void correctassociativity (TTree *tree) { + TTree *t1 = sib1(tree); + assert(tree->tag == TChoice || tree->tag == TSeq); + while (t1->tag == tree->tag) { + int n1size = tree->u.ps - 1; /* t1 == Op t11 t12 */ + int n11size = t1->u.ps - 1; + int n12size = n1size - n11size - 1; + memmove(sib1(tree), sib1(t1), n11size * sizeof(TTree)); /* move t11 */ + tree->u.ps = n11size + 1; + sib2(tree)->tag = tree->tag; + sib2(tree)->u.ps = n12size + 1; + } +} + + +/* +** Make final adjustments in a tree. Fix open calls in tree 't', +** making them refer to their respective rules or raising appropriate +** errors (if not inside a grammar). Correct associativity of associative +** constructions (making them right associative). Assume that tree's +** ktable is at the top of the stack (for error messages). +*/ +static void finalfix (lua_State *L, int postable, TTree *g, TTree *t) { + tailcall: + switch (t->tag) { + case TGrammar: /* subgrammars were already fixed */ + return; + case TOpenCall: { + if (g != NULL) /* inside a grammar? */ + fixonecall(L, postable, g, t); + else { /* open call outside grammar */ + lua_rawgeti(L, -1, t->key); + luaL_error(L, "rule '%s' used outside a grammar", val2str(L, -1)); + } + break; + } + case TSeq: case TChoice: + correctassociativity(t); + break; + } + switch (numsiblings[t->tag]) { + case 1: /* finalfix(L, postable, g, sib1(t)); */ + t = sib1(t); goto tailcall; + case 2: + finalfix(L, postable, g, sib1(t)); + t = sib2(t); goto tailcall; /* finalfix(L, postable, g, sib2(t)); */ + default: assert(numsiblings[t->tag] == 0); break; + } +} + + +/* +** {====================================================== +** Tree generation +** ======================================================= +*/ + +/* +** In 5.2, could use 'luaL_testudata'... +*/ +static int testpattern (lua_State *L, int idx) { + if (lua_touserdata(L, idx)) { /* value is a userdata? */ + if (lua_getmetatable(L, idx)) { /* does it have a metatable? */ + luaL_getmetatable(L, PATTERN_T); + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ + lua_pop(L, 2); /* remove both metatables */ + return 1; + } + } + } + return 0; +} + + +static Pattern *getpattern (lua_State *L, int idx) { + return (Pattern *)luaL_checkudata(L, idx, PATTERN_T); +} + + +static int getsize (lua_State *L, int idx) { + return (lua_objlen(L, idx) - sizeof(Pattern)) / sizeof(TTree) + 1; +} + + +static TTree *gettree (lua_State *L, int idx, int *len) { + Pattern *p = getpattern(L, idx); + if (len) + *len = getsize(L, idx); + return p->tree; +} + + +/* +** create a pattern +*/ +static TTree *newtree (lua_State *L, int len) { + size_t size = (len - 1) * sizeof(TTree) + sizeof(Pattern); + Pattern *p = (Pattern *)lua_newuserdata(L, size); + luaL_getmetatable(L, PATTERN_T); + lua_setmetatable(L, -2); + p->code = NULL; p->codesize = 0; + return p->tree; +} + + +static TTree *newleaf (lua_State *L, int tag) { + TTree *tree = newtree(L, 1); + tree->tag = tag; + return tree; +} + + +static TTree *newcharset (lua_State *L) { + TTree *tree = newtree(L, bytes2slots(CHARSETSIZE) + 1); + tree->tag = TSet; + loopset(i, treebuffer(tree)[i] = 0); + return tree; +} + + +/* +** add to tree a sequence where first sibling is 'sib' (with size +** 'sibsize'); returns position for second sibling +*/ +static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { + tree->tag = TSeq; tree->u.ps = sibsize + 1; + memcpy(sib1(tree), sib, sibsize * sizeof(TTree)); + return sib2(tree); +} + + +/* +** Add element 'idx' to 'ktable' of pattern at the top of the stack; +** create new 'ktable' if necessary. Return index of new element. +*/ +static int addtoktable (lua_State *L, int idx) { + if (idx == 0 || lua_isnil(L, idx)) /* no actual value to insert? */ + return 0; + else { + int n; + lua_getfenv(L, -1); /* get ktable from pattern */ + n = lua_objlen(L, -1); + if (n == 0) { /* is it empty/non-existent? */ + lua_pop(L, 1); /* remove it */ + lua_createtable(L, 1, 0); /* create a fresh table */ + } + lua_pushvalue(L, idx); /* element to be added */ + lua_rawseti(L, -2, n + 1); + lua_setfenv(L, -2); /* set it as ktable for pattern */ + return n + 1; + } +} + + +/* +** Build a sequence of 'n' nodes, each with tag 'tag' and 'u.n' got +** from the array 's' (or 0 if array is NULL). (TSeq is binary, so it +** must build a sequence of sequence of sequence...) +*/ +static void fillseq (TTree *tree, int tag, int n, const char *s) { + int i; + for (i = 0; i < n - 1; i++) { /* initial n-1 copies of Seq tag; Seq ... */ + tree->tag = TSeq; tree->u.ps = 2; + sib1(tree)->tag = tag; + sib1(tree)->u.n = s ? (byte)s[i] : 0; + tree = sib2(tree); + } + tree->tag = tag; /* last one does not need TSeq */ + tree->u.n = s ? (byte)s[i] : 0; +} + + +/* +** Numbers as patterns: +** 0 == true (always match); n == TAny repeated 'n' times; +** -n == not (TAny repeated 'n' times) +*/ +static TTree *numtree (lua_State *L, int n) { + if (n == 0) + return newleaf(L, TTrue); + else { + TTree *tree, *nd; + if (n > 0) + tree = nd = newtree(L, 2 * n - 1); + else { /* negative: code it as !(-n) */ + n = -n; + tree = newtree(L, 2 * n); + tree->tag = TNot; + nd = sib1(tree); + } + fillseq(nd, TAny, n, NULL); /* sequence of 'n' any's */ + return tree; + } +} + + +/* +** Convert value at index 'idx' to a pattern +*/ +static TTree *getpatt (lua_State *L, int idx, int *len) { + TTree *tree; + switch (lua_type(L, idx)) { + case LUA_TSTRING: { + size_t slen; + const char *s = lua_tolstring(L, idx, &slen); /* get string */ + if (slen == 0) /* empty? */ + tree = newleaf(L, TTrue); /* always match */ + else { + tree = newtree(L, 2 * (slen - 1) + 1); + fillseq(tree, TChar, slen, s); /* sequence of 'slen' chars */ + } + break; + } + case LUA_TNUMBER: { + int n = lua_tointeger(L, idx); + tree = numtree(L, n); + break; + } + case LUA_TBOOLEAN: { + tree = (lua_toboolean(L, idx) ? newleaf(L, TTrue) : newleaf(L, TFalse)); + break; + } + case LUA_TTABLE: { + tree = newgrammar(L, idx); + break; + } + case LUA_TFUNCTION: { + tree = newtree(L, 2); + tree->tag = TRunTime; + tree->key = addtoktable(L, idx); + sib1(tree)->tag = TTrue; + break; + } + default: { + return gettree(L, idx, len); + } + } + lua_replace(L, idx); /* put new tree into 'idx' slot */ + if (len) + *len = getsize(L, idx); + return tree; +} + + +/* +** Return the number of elements in the ktable of pattern at 'idx'. +** In Lua 5.2, default "environment" for patterns is nil, not +** a table. Treat it as an empty table. In Lua 5.1, assumes that +** the environment has no numeric indices (len == 0) +*/ +static int ktablelen (lua_State *L, int idx) { + if (!lua_istable(L, idx)) return 0; + else return lua_objlen(L, idx); +} + + +/* +** Concatentate the contents of table 'idx1' into table 'idx2'. +** (Assume that both indices are negative.) +** Return the original length of table 'idx2' +*/ +static int concattable (lua_State *L, int idx1, int idx2) { + int i; + int n1 = ktablelen(L, idx1); + int n2 = ktablelen(L, idx2); + if (n1 == 0) return 0; /* nothing to correct */ + for (i = 1; i <= n1; i++) { + lua_rawgeti(L, idx1, i); + lua_rawseti(L, idx2 - 1, n2 + i); /* correct 'idx2' */ + } + return n2; +} + + +/* +** Make a merge of ktables from p1 and p2 the ktable for the new +** pattern at the top of the stack. +*/ +static int joinktables (lua_State *L, int p1, int p2) { + int n1, n2; + lua_getfenv(L, p1); /* get ktables */ + lua_getfenv(L, p2); + n1 = ktablelen(L, -2); + n2 = ktablelen(L, -1); + if (n1 == 0 && n2 == 0) { /* are both tables empty? */ + lua_pop(L, 2); /* nothing to be done; pop tables */ + return 0; /* nothing to correct */ + } + if (n2 == 0 || lua_equal(L, -2, -1)) { /* second table is empty or equal? */ + lua_pop(L, 1); /* pop 2nd table */ + lua_setfenv(L, -2); /* set 1st ktable into new pattern */ + return 0; /* nothing to correct */ + } + if (n1 == 0) { /* first table is empty? */ + lua_setfenv(L, -3); /* set 2nd table into new pattern */ + lua_pop(L, 1); /* pop 1st table */ + return 0; /* nothing to correct */ + } + else { + lua_createtable(L, n1 + n2, 0); /* create ktable for new pattern */ + /* stack: new p; ktable p1; ktable p2; new ktable */ + concattable(L, -3, -1); /* from p1 into new ktable */ + concattable(L, -2, -1); /* from p2 into new ktable */ + lua_setfenv(L, -4); /* new ktable becomes p env */ + lua_pop(L, 2); /* pop other ktables */ + return n1; /* correction for indices from p2 */ + } +} + + +static void correctkeys (TTree *tree, int n) { + if (n == 0) return; /* no correction? */ + tailcall: + switch (tree->tag) { + case TOpenCall: case TCall: case TRunTime: case TRule: { + if (tree->key > 0) + tree->key += n; + break; + } + case TCapture: { + if (tree->key > 0 && tree->cap != Carg && tree->cap != Cnum) + tree->key += n; + break; + } + default: break; + } + switch (numsiblings[tree->tag]) { + case 1: /* correctkeys(sib1(tree), n); */ + tree = sib1(tree); goto tailcall; + case 2: + correctkeys(sib1(tree), n); + tree = sib2(tree); goto tailcall; /* correctkeys(sib2(tree), n); */ + default: assert(numsiblings[tree->tag] == 0); break; + } +} + + +/* +** copy 'ktable' of element 'idx' to new tree (on top of stack) +*/ +static void copyktable (lua_State *L, int idx) { + lua_getfenv(L, idx); + lua_setfenv(L, -2); +} + + +/* +** merge 'ktable' from rule at stack index 'idx' into 'ktable' +** from tree at the top of the stack, and correct corresponding +** tree. +*/ +static void mergektable (lua_State *L, int idx, TTree *rule) { + int n; + lua_getfenv(L, -1); /* get ktables */ + lua_getfenv(L, idx); + n = concattable(L, -1, -2); + lua_pop(L, 2); /* remove both ktables */ + correctkeys(rule, n); +} + + +/* +** create a new tree, whith a new root and one sibling. +** Sibling must be on the Lua stack, at index 1. +*/ +static TTree *newroot1sib (lua_State *L, int tag) { + int s1; + TTree *tree1 = getpatt(L, 1, &s1); + TTree *tree = newtree(L, 1 + s1); /* create new tree */ + tree->tag = tag; + memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); + copyktable(L, 1); + return tree; +} + + +/* +** create a new tree, whith a new root and 2 siblings. +** Siblings must be on the Lua stack, first one at index 1. +*/ +static TTree *newroot2sib (lua_State *L, int tag) { + int s1, s2; + TTree *tree1 = getpatt(L, 1, &s1); + TTree *tree2 = getpatt(L, 2, &s2); + TTree *tree = newtree(L, 1 + s1 + s2); /* create new tree */ + tree->tag = tag; + tree->u.ps = 1 + s1; + memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); + memcpy(sib2(tree), tree2, s2 * sizeof(TTree)); + correctkeys(sib2(tree), joinktables(L, 1, 2)); + return tree; +} + + +static int lp_P (lua_State *L) { + luaL_checkany(L, 1); + getpatt(L, 1, NULL); + lua_settop(L, 1); + return 1; +} + + +/* +** sequence operator; optimizations: +** false x => false, x true => x, true x => x +** (cannot do x . false => false because x may have runtime captures) +*/ +static int lp_seq (lua_State *L) { + TTree *tree1 = getpatt(L, 1, NULL); + TTree *tree2 = getpatt(L, 2, NULL); + if (tree1->tag == TFalse || tree2->tag == TTrue) + lua_pushvalue(L, 1); /* false . x == false, x . true = x */ + else if (tree1->tag == TTrue) + lua_pushvalue(L, 2); /* true . x = x */ + else + newroot2sib(L, TSeq); + return 1; +} + + +/* +** choice operator; optimizations: +** charset / charset => charset +** true / x => true, x / false => x, false / x => x +** (x / true is not equivalent to true) +*/ +static int lp_choice (lua_State *L) { + Charset st1, st2; + TTree *t1 = getpatt(L, 1, NULL); + TTree *t2 = getpatt(L, 2, NULL); + if (tocharset(t1, &st1) && tocharset(t2, &st2)) { + TTree *t = newcharset(L); + loopset(i, treebuffer(t)[i] = st1.cs[i] | st2.cs[i]); + } + else if (nofail(t1) || t2->tag == TFalse) + lua_pushvalue(L, 1); /* true / x => true, x / false => x */ + else if (t1->tag == TFalse) + lua_pushvalue(L, 2); /* false / x => x */ + else + newroot2sib(L, TChoice); + return 1; +} + + +/* +** p^n +*/ +static int lp_star (lua_State *L) { + int size1; + int n = luaL_checkint(L, 2); + TTree *tree1 = gettree(L, 1, &size1); + if (n >= 0) { /* seq tree1 (seq tree1 ... (seq tree1 (rep tree1))) */ + TTree *tree = newtree(L, (n + 1) * (size1 + 1)); + if (nullable(tree1)) + luaL_error(L, "loop body may accept empty string"); + while (n--) /* repeat 'n' times */ + tree = seqaux(tree, tree1, size1); + tree->tag = TRep; + memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); + } + else { /* choice (seq tree1 ... choice tree1 true ...) true */ + TTree *tree; + n = -n; + /* size = (choice + seq + tree1 + true) * n, but the last has no seq */ + tree = newtree(L, n * (size1 + 3) - 1); + for (; n > 1; n--) { /* repeat (n - 1) times */ + tree->tag = TChoice; tree->u.ps = n * (size1 + 3) - 2; + sib2(tree)->tag = TTrue; + tree = sib1(tree); + tree = seqaux(tree, tree1, size1); + } + tree->tag = TChoice; tree->u.ps = size1 + 1; + sib2(tree)->tag = TTrue; + memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); + } + copyktable(L, 1); + return 1; +} + + +/* +** #p == &p +*/ +static int lp_and (lua_State *L) { + newroot1sib(L, TAnd); + return 1; +} + + +/* +** -p == !p +*/ +static int lp_not (lua_State *L) { + newroot1sib(L, TNot); + return 1; +} + + +/* +** [t1 - t2] == Seq (Not t2) t1 +** If t1 and t2 are charsets, make their difference. +*/ +static int lp_sub (lua_State *L) { + Charset st1, st2; + int s1, s2; + TTree *t1 = getpatt(L, 1, &s1); + TTree *t2 = getpatt(L, 2, &s2); + if (tocharset(t1, &st1) && tocharset(t2, &st2)) { + TTree *t = newcharset(L); + loopset(i, treebuffer(t)[i] = st1.cs[i] & ~st2.cs[i]); + } + else { + TTree *tree = newtree(L, 2 + s1 + s2); + tree->tag = TSeq; /* sequence of... */ + tree->u.ps = 2 + s2; + sib1(tree)->tag = TNot; /* ...not... */ + memcpy(sib1(sib1(tree)), t2, s2 * sizeof(TTree)); /* ...t2 */ + memcpy(sib2(tree), t1, s1 * sizeof(TTree)); /* ... and t1 */ + correctkeys(sib1(tree), joinktables(L, 1, 2)); + } + return 1; +} + + +static int lp_set (lua_State *L) { + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + TTree *tree = newcharset(L); + while (l--) { + setchar(treebuffer(tree), (byte)(*s)); + s++; + } + return 1; +} + + +static int lp_range (lua_State *L) { + int arg; + int top = lua_gettop(L); + TTree *tree = newcharset(L); + for (arg = 1; arg <= top; arg++) { + int c; + size_t l; + const char *r = luaL_checklstring(L, arg, &l); + luaL_argcheck(L, l == 2, arg, "range must have two characters"); + for (c = (byte)r[0]; c <= (byte)r[1]; c++) + setchar(treebuffer(tree), c); + } + return 1; +} + + +/* +** Look-behind predicate +*/ +static int lp_behind (lua_State *L) { + TTree *tree; + TTree *tree1 = getpatt(L, 1, NULL); + int n = fixedlen(tree1); + luaL_argcheck(L, !hascaptures(tree1), 1, "pattern have captures"); + luaL_argcheck(L, n > 0, 1, "pattern may not have fixed length"); + luaL_argcheck(L, n <= MAXBEHIND, 1, "pattern too long to look behind"); + tree = newroot1sib(L, TBehind); + tree->u.n = n; + return 1; +} + + +/* +** Create a non-terminal +*/ +static int lp_V (lua_State *L) { + TTree *tree = newleaf(L, TOpenCall); + luaL_argcheck(L, !lua_isnoneornil(L, 1), 1, "non-nil value expected"); + tree->key = addtoktable(L, 1); + return 1; +} + + +/* +** Create a tree for a non-empty capture, with a body and +** optionally with an associated Lua value (at index 'labelidx' in the +** stack) +*/ +static int capture_aux (lua_State *L, int cap, int labelidx) { + TTree *tree = newroot1sib(L, TCapture); + tree->cap = cap; + tree->key = addtoktable(L, labelidx); + return 1; +} + + +/* +** Fill a tree with an empty capture, using an empty (TTrue) sibling. +*/ +static TTree *auxemptycap (lua_State *L, TTree *tree, int cap, int idx) { + tree->tag = TCapture; + tree->cap = cap; + tree->key = addtoktable(L, idx); + sib1(tree)->tag = TTrue; + return tree; +} + + +/* +** Create a tree for an empty capture +*/ +static TTree *newemptycap (lua_State *L, int cap, int idx) { + return auxemptycap(L, newtree(L, 2), cap, idx); +} + + +/* +** Captures with syntax p / v +** (function capture, query capture, string capture, or number capture) +*/ +static int lp_divcapture (lua_State *L) { + switch (lua_type(L, 2)) { + case LUA_TFUNCTION: return capture_aux(L, Cfunction, 2); + case LUA_TTABLE: return capture_aux(L, Cquery, 2); + case LUA_TSTRING: return capture_aux(L, Cstring, 2); + case LUA_TNUMBER: { + int n = lua_tointeger(L, 2); + TTree *tree = newroot1sib(L, TCapture); + luaL_argcheck(L, 0 <= n && n <= SHRT_MAX, 1, "invalid number"); + tree->cap = Cnum; + tree->key = n; + return 1; + } + default: return luaL_argerror(L, 2, "invalid replacement value"); + } +} + + +static int lp_substcapture (lua_State *L) { + return capture_aux(L, Csubst, 0); +} + + +static int lp_tablecapture (lua_State *L) { + return capture_aux(L, Ctable, 0); +} + + +static int lp_groupcapture (lua_State *L) { + if (lua_isnoneornil(L, 2)) + return capture_aux(L, Cgroup, 0); + else { + luaL_checkstring(L, 2); + return capture_aux(L, Cgroup, 2); + } +} + + +static int lp_foldcapture (lua_State *L) { + luaL_checktype(L, 2, LUA_TFUNCTION); + return capture_aux(L, Cfold, 2); +} + + +static int lp_simplecapture (lua_State *L) { + return capture_aux(L, Csimple, 0); +} + + +static int lp_poscapture (lua_State *L) { + newemptycap(L, Cposition, 0); + return 1; +} + + +static int lp_argcapture (lua_State *L) { + int n = luaL_checkint(L, 1); + TTree *tree = newemptycap(L, Carg, 0); + tree->key = n; + luaL_argcheck(L, 0 < n && n <= SHRT_MAX, 1, "invalid argument index"); + return 1; +} + + +static int lp_backref (lua_State *L) { + luaL_checkstring(L, 1); + newemptycap(L, Cbackref, 1); + return 1; +} + + +/* +** Constant capture +*/ +static int lp_constcapture (lua_State *L) { + int i; + int n = lua_gettop(L); /* number of values */ + if (n == 0) /* no values? */ + newleaf(L, TTrue); /* no capture */ + else if (n == 1) + newemptycap(L, Cconst, 1); /* single constant capture */ + else { /* create a group capture with all values */ + TTree *tree = newtree(L, 1 + 3 * (n - 1) + 2); + tree->tag = TCapture; + tree->cap = Cgroup; + tree->key = 0; + tree = sib1(tree); + for (i = 1; i <= n - 1; i++) { + tree->tag = TSeq; + tree->u.ps = 3; /* skip TCapture and its sibling */ + auxemptycap(L, sib1(tree), Cconst, i); + tree = sib2(tree); + } + auxemptycap(L, tree, Cconst, i); + } + return 1; +} + + +static int lp_matchtime (lua_State *L) { + TTree *tree; + luaL_checktype(L, 2, LUA_TFUNCTION); + tree = newroot1sib(L, TRunTime); + tree->key = addtoktable(L, 2); + return 1; +} + +/* }====================================================== */ + + +/* +** {====================================================== +** Grammar - Tree generation +** ======================================================= +*/ + +/* +** push on the stack the index and the pattern for the +** initial rule of grammar at index 'arg' in the stack; +** also add that index into position table. +*/ +static void getfirstrule (lua_State *L, int arg, int postab) { + lua_rawgeti(L, arg, 1); /* access first element */ + if (lua_isstring(L, -1)) { /* is it the name of initial rule? */ + lua_pushvalue(L, -1); /* duplicate it to use as key */ + lua_gettable(L, arg); /* get associated rule */ + } + else { + lua_pushinteger(L, 1); /* key for initial rule */ + lua_insert(L, -2); /* put it before rule */ + } + if (!testpattern(L, -1)) { /* initial rule not a pattern? */ + if (lua_isnil(L, -1)) + luaL_error(L, "grammar has no initial rule"); + else + luaL_error(L, "initial rule '%s' is not a pattern", lua_tostring(L, -2)); + } + lua_pushvalue(L, -2); /* push key */ + lua_pushinteger(L, 1); /* push rule position (after TGrammar) */ + lua_settable(L, postab); /* insert pair at position table */ +} + +/* +** traverse grammar at index 'arg', pushing all its keys and patterns +** into the stack. Create a new table (before all pairs key-pattern) to +** collect all keys and their associated positions in the final tree +** (the "position table"). +** Return the number of rules and (in 'totalsize') the total size +** for the new tree. +*/ +static int collectrules (lua_State *L, int arg, int *totalsize) { + int n = 1; /* to count number of rules */ + int postab = lua_gettop(L) + 1; /* index of position table */ + int size; /* accumulator for total size */ + lua_newtable(L); /* create position table */ + getfirstrule(L, arg, postab); + size = 2 + getsize(L, postab + 2); /* TGrammar + TRule + rule */ + lua_pushnil(L); /* prepare to traverse grammar table */ + while (lua_next(L, arg) != 0) { + if (lua_tonumber(L, -2) == 1 || + lua_equal(L, -2, postab + 1)) { /* initial rule? */ + lua_pop(L, 1); /* remove value (keep key for lua_next) */ + continue; + } + if (!testpattern(L, -1)) /* value is not a pattern? */ + luaL_error(L, "rule '%s' is not a pattern", val2str(L, -2)); + luaL_checkstack(L, LUA_MINSTACK, "grammar has too many rules"); + lua_pushvalue(L, -2); /* push key (to insert into position table) */ + lua_pushinteger(L, size); + lua_settable(L, postab); + size += 1 + getsize(L, -1); /* update size */ + lua_pushvalue(L, -2); /* push key (for next lua_next) */ + n++; + } + *totalsize = size + 1; /* TTrue to finish list of rules */ + return n; +} + + +static void buildgrammar (lua_State *L, TTree *grammar, int frule, int n) { + int i; + TTree *nd = sib1(grammar); /* auxiliary pointer to traverse the tree */ + for (i = 0; i < n; i++) { /* add each rule into new tree */ + int ridx = frule + 2*i + 1; /* index of i-th rule */ + int rulesize; + TTree *rn = gettree(L, ridx, &rulesize); + nd->tag = TRule; + nd->key = 0; + nd->cap = i; /* rule number */ + nd->u.ps = rulesize + 1; /* point to next rule */ + memcpy(sib1(nd), rn, rulesize * sizeof(TTree)); /* copy rule */ + mergektable(L, ridx, sib1(nd)); /* merge its ktable into new one */ + nd = sib2(nd); /* move to next rule */ + } + nd->tag = TTrue; /* finish list of rules */ +} + + +/* +** Check whether a tree has potential infinite loops +*/ +static int checkloops (TTree *tree) { + tailcall: + if (tree->tag == TRep && nullable(sib1(tree))) + return 1; + else if (tree->tag == TGrammar) + return 0; /* sub-grammars already checked */ + else { + switch (numsiblings[tree->tag]) { + case 1: /* return checkloops(sib1(tree)); */ + tree = sib1(tree); goto tailcall; + case 2: + if (checkloops(sib1(tree))) return 1; + /* else return checkloops(sib2(tree)); */ + tree = sib2(tree); goto tailcall; + default: assert(numsiblings[tree->tag] == 0); return 0; + } + } +} + + +static int verifyerror (lua_State *L, int *passed, int npassed) { + int i, j; + for (i = npassed - 1; i >= 0; i--) { /* search for a repetition */ + for (j = i - 1; j >= 0; j--) { + if (passed[i] == passed[j]) { + lua_rawgeti(L, -1, passed[i]); /* get rule's key */ + return luaL_error(L, "rule '%s' may be left recursive", val2str(L, -1)); + } + } + } + return luaL_error(L, "too many left calls in grammar"); +} + + +/* +** Check whether a rule can be left recursive; raise an error in that +** case; otherwise return 1 iff pattern is nullable. Assume ktable at +** the top of the stack. +*/ +static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed, + int nullable) { + tailcall: + switch (tree->tag) { + case TChar: case TSet: case TAny: + case TFalse: + return nullable; /* cannot pass from here */ + case TTrue: + case TBehind: /* look-behind cannot have calls */ + return 1; + case TNot: case TAnd: case TRep: + /* return verifyrule(L, sib1(tree), passed, npassed, 1); */ + tree = sib1(tree); nullable = 1; goto tailcall; + case TCapture: case TRunTime: + /* return verifyrule(L, sib1(tree), passed, npassed); */ + tree = sib1(tree); goto tailcall; + case TCall: + /* return verifyrule(L, sib2(tree), passed, npassed); */ + tree = sib2(tree); goto tailcall; + case TSeq: /* only check 2nd child if first is nullable */ + if (!verifyrule(L, sib1(tree), passed, npassed, 0)) + return nullable; + /* else return verifyrule(L, sib2(tree), passed, npassed); */ + tree = sib2(tree); goto tailcall; + case TChoice: /* must check both children */ + nullable = verifyrule(L, sib1(tree), passed, npassed, nullable); + /* return verifyrule(L, sib2(tree), passed, npassed, nullable); */ + tree = sib2(tree); goto tailcall; + case TRule: + if (npassed >= MAXRULES) + return verifyerror(L, passed, npassed); + else { + passed[npassed++] = tree->key; + /* return verifyrule(L, sib1(tree), passed, npassed); */ + tree = sib1(tree); goto tailcall; + } + case TGrammar: + return nullable(tree); /* sub-grammar cannot be left recursive */ + default: assert(0); return 0; + } +} + + +static void verifygrammar (lua_State *L, TTree *grammar) { + int passed[MAXRULES]; + TTree *rule; + /* check left-recursive rules */ + for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { + if (rule->key == 0) continue; /* unused rule */ + verifyrule(L, sib1(rule), passed, 0, 0); + } + assert(rule->tag == TTrue); + /* check infinite loops inside rules */ + for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { + if (rule->key == 0) continue; /* unused rule */ + if (checkloops(sib1(rule))) { + lua_rawgeti(L, -1, rule->key); /* get rule's key */ + luaL_error(L, "empty loop in rule '%s'", val2str(L, -1)); + } + } + assert(rule->tag == TTrue); +} + + +/* +** Give a name for the initial rule if it is not referenced +*/ +static void initialrulename (lua_State *L, TTree *grammar, int frule) { + if (sib1(grammar)->key == 0) { /* initial rule is not referenced? */ + int n = lua_objlen(L, -1) + 1; /* index for name */ + lua_pushvalue(L, frule); /* rule's name */ + lua_rawseti(L, -2, n); /* ktable was on the top of the stack */ + sib1(grammar)->key = n; + } +} + + +static TTree *newgrammar (lua_State *L, int arg) { + int treesize; + int frule = lua_gettop(L) + 2; /* position of first rule's key */ + int n = collectrules(L, arg, &treesize); + TTree *g = newtree(L, treesize); + luaL_argcheck(L, n <= MAXRULES, arg, "grammar has too many rules"); + g->tag = TGrammar; g->u.n = n; + lua_newtable(L); /* create 'ktable' */ + lua_setfenv(L, -2); + buildgrammar(L, g, frule, n); + lua_getfenv(L, -1); /* get 'ktable' for new tree */ + finalfix(L, frule - 1, g, sib1(g)); + initialrulename(L, g, frule); + verifygrammar(L, g); + lua_pop(L, 1); /* remove 'ktable' */ + lua_insert(L, -(n * 2 + 2)); /* move new table to proper position */ + lua_pop(L, n * 2 + 1); /* remove position table + rule pairs */ + return g; /* new table at the top of the stack */ +} + +/* }====================================================== */ + + +static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { + lua_getfenv(L, idx); /* push 'ktable' (may be used by 'finalfix') */ + finalfix(L, 0, NULL, p->tree); + lua_pop(L, 1); /* remove 'ktable' */ + return compile(L, p); +} + + +static int lp_printtree (lua_State *L) { + TTree *tree = getpatt(L, 1, NULL); + int c = lua_toboolean(L, 2); + if (c) { + lua_getfenv(L, 1); /* push 'ktable' (may be used by 'finalfix') */ + finalfix(L, 0, NULL, tree); + lua_pop(L, 1); /* remove 'ktable' */ + } + printktable(L, 1); + printtree(tree, 0); + return 0; +} + + +static int lp_printcode (lua_State *L) { + Pattern *p = getpattern(L, 1); + printktable(L, 1); + if (p->code == NULL) /* not compiled yet? */ + prepcompile(L, p, 1); + printpatt(p->code, p->codesize); + return 0; +} + + +/* +** Get the initial position for the match, interpreting negative +** values from the end of the subject +*/ +static size_t initposition (lua_State *L, size_t len) { + lua_Integer ii = luaL_optinteger(L, 3, 1); + if (ii > 0) { /* positive index? */ + if ((size_t)ii <= len) /* inside the string? */ + return (size_t)ii - 1; /* return it (corrected to 0-base) */ + else return len; /* crop at the end */ + } + else { /* negative index */ + if ((size_t)(-ii) <= len) /* inside the string? */ + return len - ((size_t)(-ii)); /* return position from the end */ + else return 0; /* crop at the beginning */ + } +} + + +/* +** Main match function +*/ +static int lp_match (lua_State *L) { + Capture capture[INITCAPSIZE]; + const char *r; + size_t l; + Pattern *p = (getpatt(L, 1, NULL), getpattern(L, 1)); + Instruction *code = (p->code != NULL) ? p->code : prepcompile(L, p, 1); + const char *s = luaL_checklstring(L, SUBJIDX, &l); + size_t i = initposition(L, l); + int ptop = lua_gettop(L); + lua_pushnil(L); /* initialize subscache */ + lua_pushlightuserdata(L, capture); /* initialize caplistidx */ + lua_getfenv(L, 1); /* initialize penvidx */ + r = match(L, s, s + i, s + l, code, capture, ptop); + if (r == NULL) { + lua_pushnil(L); + return 1; + } + return getcaptures(L, s, r, ptop); +} + + + +/* +** {====================================================== +** Library creation and functions not related to matching +** ======================================================= +*/ + +static int lp_setmax (lua_State *L) { + luaL_optinteger(L, 1, -1); + lua_settop(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); + return 0; +} + + +static int lp_version (lua_State *L) { + lua_pushstring(L, VERSION); + return 1; +} + + +static int lp_type (lua_State *L) { + if (testpattern(L, 1)) + lua_pushliteral(L, "pattern"); + else + lua_pushnil(L); + return 1; +} + + +int lp_gc (lua_State *L) { + Pattern *p = getpattern(L, 1); + if (p->codesize > 0) + reallocprog(L, p, 0); + return 0; +} + + +static void createcat (lua_State *L, const char *catname, int (catf) (int)) { + TTree *t = newcharset(L); + int i; + for (i = 0; i <= UCHAR_MAX; i++) + if (catf(i)) setchar(treebuffer(t), i); + lua_setfield(L, -2, catname); +} + + +static int lp_locale (lua_State *L) { + if (lua_isnoneornil(L, 1)) { + lua_settop(L, 0); + lua_createtable(L, 0, 12); + } + else { + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 1); + } + createcat(L, "alnum", isalnum); + createcat(L, "alpha", isalpha); + createcat(L, "cntrl", iscntrl); + createcat(L, "digit", isdigit); + createcat(L, "graph", isgraph); + createcat(L, "lower", islower); + createcat(L, "print", isprint); + createcat(L, "punct", ispunct); + createcat(L, "space", isspace); + createcat(L, "upper", isupper); + createcat(L, "xdigit", isxdigit); + return 1; +} + + +static struct luaL_Reg pattreg[] = { + {"ptree", lp_printtree}, + {"pcode", lp_printcode}, + {"match", lp_match}, + {"B", lp_behind}, + {"V", lp_V}, + {"C", lp_simplecapture}, + {"Cc", lp_constcapture}, + {"Cmt", lp_matchtime}, + {"Cb", lp_backref}, + {"Carg", lp_argcapture}, + {"Cp", lp_poscapture}, + {"Cs", lp_substcapture}, + {"Ct", lp_tablecapture}, + {"Cf", lp_foldcapture}, + {"Cg", lp_groupcapture}, + {"P", lp_P}, + {"S", lp_set}, + {"R", lp_range}, + {"locale", lp_locale}, + {"version", lp_version}, + {"setmaxstack", lp_setmax}, + {"type", lp_type}, + {NULL, NULL} +}; + + +static struct luaL_Reg metareg[] = { + {"__mul", lp_seq}, + {"__add", lp_choice}, + {"__pow", lp_star}, + {"__gc", lp_gc}, + {"__len", lp_and}, + {"__div", lp_divcapture}, + {"__unm", lp_not}, + {"__sub", lp_sub}, + {NULL, NULL} +}; + + +int luaopen_lpeg (lua_State *L); +int luaopen_lpeg (lua_State *L) { + luaL_newmetatable(L, PATTERN_T); + lua_pushnumber(L, MAXBACK); /* initialize maximum backtracking */ + lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); + luaL_register(L, NULL, metareg); + luaL_register(L, "lpeg", pattreg); + lua_pushvalue(L, -1); + lua_setfield(L, -3, "__index"); + return 1; +} + +/* }====================================================== */ diff --git a/3rd/lpeg/lptree.h b/3rd/lpeg/lptree.h new file mode 100644 index 00000000..b69528a6 --- /dev/null +++ b/3rd/lpeg/lptree.h @@ -0,0 +1,77 @@ +/* +** $Id: lptree.h,v 1.2 2013/03/24 13:51:12 roberto Exp $ +*/ + +#if !defined(lptree_h) +#define lptree_h + + +#include "lptypes.h" + + +/* +** types of trees +*/ +typedef enum TTag { + TChar = 0, TSet, TAny, /* standard PEG elements */ + TTrue, TFalse, + TRep, + TSeq, TChoice, + TNot, TAnd, + TCall, + TOpenCall, + TRule, /* sib1 is rule's pattern, sib2 is 'next' rule */ + TGrammar, /* sib1 is initial (and first) rule */ + TBehind, /* match behind */ + TCapture, /* regular capture */ + TRunTime /* run-time capture */ +} TTag; + +/* number of siblings for each tree */ +extern const byte numsiblings[]; + + +/* +** Tree trees +** The first sibling of a tree (if there is one) is immediately after +** the tree. A reference to a second sibling (ps) is its position +** relative to the position of the tree itself. A key in ktable +** uses the (unique) address of the original tree that created that +** entry. NULL means no data. +*/ +typedef struct TTree { + byte tag; + byte cap; /* kind of capture (if it is a capture) */ + unsigned short key; /* key in ktable for Lua data (0 if no key) */ + union { + int ps; /* occasional second sibling */ + int n; /* occasional counter */ + } u; +} TTree; + + +/* +** A complete pattern has its tree plus, if already compiled, +** its corresponding code +*/ +typedef struct Pattern { + union Instruction *code; + int codesize; + TTree tree[1]; +} Pattern; + + +/* number of siblings for each tree */ +extern const byte numsiblings[]; + +/* access to siblings */ +#define sib1(t) ((t) + 1) +#define sib2(t) ((t) + (t)->u.ps) + + + + + + +#endif + diff --git a/3rd/lpeg/lptypes.h b/3rd/lpeg/lptypes.h new file mode 100644 index 00000000..7ace5451 --- /dev/null +++ b/3rd/lpeg/lptypes.h @@ -0,0 +1,147 @@ +/* +** $Id: lptypes.h,v 1.8 2013/04/12 16:26:38 roberto Exp $ +** LPeg - PEG pattern matching for Lua +** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +** written by Roberto Ierusalimschy +*/ + +#if !defined(lptypes_h) +#define lptypes_h + + +#if !defined(LPEG_DEBUG) +#define NDEBUG +#endif + +#include +#include + +#include "lua.h" + + +#define VERSION "0.12" + + +#define PATTERN_T "lpeg-pattern" +#define MAXSTACKIDX "lpeg-maxstack" + + +/* +** compatibility with Lua 5.2 +*/ +#if (LUA_VERSION_NUM == 502) + +#undef lua_equal +#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) + +#undef lua_getfenv +#define lua_getfenv lua_getuservalue +#undef lua_setfenv +#define lua_setfenv lua_setuservalue + +#undef lua_objlen +#define lua_objlen lua_rawlen + +#undef luaL_register +#define luaL_register(L,n,f) \ + { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } + +#endif + + +/* default maximum size for call/backtrack stack */ +#if !defined(MAXBACK) +#define MAXBACK 100 +#endif + + +/* maximum number of rules in a grammar */ +#define MAXRULES 200 + + + +/* initial size for capture's list */ +#define INITCAPSIZE 32 + + +/* index, on Lua stack, for subject */ +#define SUBJIDX 2 + +/* number of fixed arguments to 'match' (before capture arguments) */ +#define FIXEDARGS 3 + +/* index, on Lua stack, for capture list */ +#define caplistidx(ptop) ((ptop) + 2) + +/* index, on Lua stack, for pattern's ktable */ +#define ktableidx(ptop) ((ptop) + 3) + +/* index, on Lua stack, for backtracking stack */ +#define stackidx(ptop) ((ptop) + 4) + + + +typedef unsigned char byte; + + +#define BITSPERCHAR 8 + +#define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1) + + + +typedef struct Charset { + byte cs[CHARSETSIZE]; +} Charset; + + + +#define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} } + +/* access to charset */ +#define treebuffer(t) ((byte *)((t) + 1)) + +/* number of slots needed for 'n' bytes */ +#define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1) + +/* set 'b' bit in charset 'cs' */ +#define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7))) + + +/* +** in capture instructions, 'kind' of capture and its offset are +** packed in field 'aux', 4 bits for each +*/ +#define getkind(op) ((op)->i.aux & 0xF) +#define getoff(op) (((op)->i.aux >> 4) & 0xF) +#define joinkindoff(k,o) ((k) | ((o) << 4)) + +#define MAXOFF 0xF +#define MAXAUX 0xFF + + +/* maximum number of bytes to look behind */ +#define MAXBEHIND MAXAUX + + +/* maximum size (in elements) for a pattern */ +#define MAXPATTSIZE (SHRT_MAX - 10) + + +/* size (in elements) for an instruction plus extra l bytes */ +#define instsize(l) (((l) + sizeof(Instruction) - 1)/sizeof(Instruction) + 1) + + +/* size (in elements) for a ISet instruction */ +#define CHARSETINSTSIZE instsize(CHARSETSIZE) + +/* size (in elements) for a IFunc instruction */ +#define funcinstsize(p) ((p)->i.aux + 2) + + + +#define testchar(st,c) (((int)(st)[((c) >> 3)] & (1 << ((c) & 7)))) + + +#endif + diff --git a/3rd/lpeg/lpvm.c b/3rd/lpeg/lpvm.c new file mode 100644 index 00000000..cd893ed8 --- /dev/null +++ b/3rd/lpeg/lpvm.c @@ -0,0 +1,355 @@ +/* +** $Id: lpvm.c,v 1.5 2013/04/12 16:29:49 roberto Exp $ +** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +*/ + +#include +#include + + +#include "lua.h" +#include "lauxlib.h" + +#include "lpcap.h" +#include "lptypes.h" +#include "lpvm.h" +#include "lpprint.h" + + +/* initial size for call/backtrack stack */ +#if !defined(INITBACK) +#define INITBACK 100 +#endif + + +#define getoffset(p) (((p) + 1)->offset) + +static const Instruction giveup = {{IGiveup, 0, 0}}; + + +/* +** {====================================================== +** Virtual Machine +** ======================================================= +*/ + + +typedef struct Stack { + const char *s; /* saved position (or NULL for calls) */ + const Instruction *p; /* next instruction */ + int caplevel; +} Stack; + + +#define getstackbase(L, ptop) ((Stack *)lua_touserdata(L, stackidx(ptop))) + + +/* +** Double the size of the array of captures +*/ +static Capture *doublecap (lua_State *L, Capture *cap, int captop, int ptop) { + Capture *newc; + if (captop >= INT_MAX/((int)sizeof(Capture) * 2)) + luaL_error(L, "too many captures"); + newc = (Capture *)lua_newuserdata(L, captop * 2 * sizeof(Capture)); + memcpy(newc, cap, captop * sizeof(Capture)); + lua_replace(L, caplistidx(ptop)); + return newc; +} + + +/* +** Double the size of the stack +*/ +static Stack *doublestack (lua_State *L, Stack **stacklimit, int ptop) { + Stack *stack = getstackbase(L, ptop); + Stack *newstack; + int n = *stacklimit - stack; /* current stack size */ + int max, newn; + lua_getfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); + max = lua_tointeger(L, -1); /* maximum allowed size */ + lua_pop(L, 1); + if (n >= max) /* already at maximum size? */ + luaL_error(L, "too many pending calls/choices"); + newn = 2 * n; /* new size */ + if (newn > max) newn = max; + newstack = (Stack *)lua_newuserdata(L, newn * sizeof(Stack)); + memcpy(newstack, stack, n * sizeof(Stack)); + lua_replace(L, stackidx(ptop)); + *stacklimit = newstack + newn; + return newstack + n; /* return next position */ +} + + +/* +** Interpret the result of a dynamic capture: false -> fail; +** true -> keep current position; number -> next position. +** Return new subject position. 'fr' is stack index where +** is the result; 'curr' is current subject position; 'limit' +** is subject's size. +*/ +static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { + lua_Integer res; + if (!lua_toboolean(L, fr)) { /* false value? */ + lua_settop(L, fr - 1); /* remove results */ + return -1; /* and fail */ + } + else if (lua_isboolean(L, fr)) /* true? */ + res = curr; /* keep current position */ + else { + res = lua_tointeger(L, fr) - 1; /* new position */ + if (res < curr || res > limit) + luaL_error(L, "invalid position returned by match-time capture"); + } + lua_remove(L, fr); /* remove first result (offset) */ + return res; +} + + +/* +** Add capture values returned by a dynamic capture to the capture list +** 'base', nested inside a group capture. 'fd' indexes the first capture +** value, 'n' is the number of values (at least 1). +*/ +static void adddyncaptures (const char *s, Capture *base, int n, int fd) { + int i; + /* Cgroup capture is already there */ + assert(base[0].kind == Cgroup && base[0].siz == 0); + base[0].idx = 0; /* make it an anonymous group */ + for (i = 1; i <= n; i++) { /* add runtime captures */ + base[i].kind = Cruntime; + base[i].siz = 1; /* mark it as closed */ + base[i].idx = fd + i - 1; /* stack index of capture value */ + base[i].s = s; + } + base[i].kind = Cclose; /* close group */ + base[i].siz = 1; + base[i].s = s; +} + + +/* +** Remove dynamic captures from the Lua stack (called in case of failure) +*/ +static int removedyncap (lua_State *L, Capture *capture, + int level, int last) { + int id = finddyncap(capture + level, capture + last); /* index of 1st cap. */ + int top = lua_gettop(L); + if (id == 0) return 0; /* no dynamic captures? */ + lua_settop(L, id - 1); /* remove captures */ + return top - id + 1; /* number of values removed */ +} + + +/* +** Opcode interpreter +*/ +const char *match (lua_State *L, const char *o, const char *s, const char *e, + Instruction *op, Capture *capture, int ptop) { + Stack stackbase[INITBACK]; + Stack *stacklimit = stackbase + INITBACK; + Stack *stack = stackbase; /* point to first empty slot in stack */ + int capsize = INITCAPSIZE; + int captop = 0; /* point to first empty slot in captures */ + int ndyncap = 0; /* number of dynamic captures (in Lua stack) */ + const Instruction *p = op; /* current instruction */ + stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++; + lua_pushlightuserdata(L, stackbase); + for (;;) { +#if defined(DEBUG) + printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", + s, stack - getstackbase(L, ptop), ndyncap, captop); + printinst(op, p); + printcaplist(capture, capture + captop); +#endif + assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); + switch ((Opcode)p->i.code) { + case IEnd: { + assert(stack == getstackbase(L, ptop) + 1); + capture[captop].kind = Cclose; + capture[captop].s = NULL; + return s; + } + case IGiveup: { + assert(stack == getstackbase(L, ptop)); + return NULL; + } + case IRet: { + assert(stack > getstackbase(L, ptop) && (stack - 1)->s == NULL); + p = (--stack)->p; + continue; + } + case IAny: { + if (s < e) { p++; s++; } + else goto fail; + continue; + } + case ITestAny: { + if (s < e) p += 2; + else p += getoffset(p); + continue; + } + case IChar: { + if ((byte)*s == p->i.aux && s < e) { p++; s++; } + else goto fail; + continue; + } + case ITestChar: { + if ((byte)*s == p->i.aux && s < e) p += 2; + else p += getoffset(p); + continue; + } + case ISet: { + int c = (byte)*s; + if (testchar((p+1)->buff, c) && s < e) + { p += CHARSETINSTSIZE; s++; } + else goto fail; + continue; + } + case ITestSet: { + int c = (byte)*s; + if (testchar((p + 2)->buff, c) && s < e) + p += 1 + CHARSETINSTSIZE; + else p += getoffset(p); + continue; + } + case IBehind: { + int n = p->i.aux; + if (n > s - o) goto fail; + s -= n; p++; + continue; + } + case ISpan: { + for (; s < e; s++) { + int c = (byte)*s; + if (!testchar((p+1)->buff, c)) break; + } + p += CHARSETINSTSIZE; + continue; + } + case IJmp: { + p += getoffset(p); + continue; + } + case IChoice: { + if (stack == stacklimit) + stack = doublestack(L, &stacklimit, ptop); + stack->p = p + getoffset(p); + stack->s = s; + stack->caplevel = captop; + stack++; + p += 2; + continue; + } + case ICall: { + if (stack == stacklimit) + stack = doublestack(L, &stacklimit, ptop); + stack->s = NULL; + stack->p = p + 2; /* save return address */ + stack++; + p += getoffset(p); + continue; + } + case ICommit: { + assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); + stack--; + p += getoffset(p); + continue; + } + case IPartialCommit: { + assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); + (stack - 1)->s = s; + (stack - 1)->caplevel = captop; + p += getoffset(p); + continue; + } + case IBackCommit: { + assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); + s = (--stack)->s; + captop = stack->caplevel; + p += getoffset(p); + continue; + } + case IFailTwice: + assert(stack > getstackbase(L, ptop)); + stack--; + /* go through */ + case IFail: + fail: { /* pattern failed: try to backtrack */ + do { /* remove pending calls */ + assert(stack > getstackbase(L, ptop)); + s = (--stack)->s; + } while (s == NULL); + if (ndyncap > 0) /* is there matchtime captures? */ + ndyncap -= removedyncap(L, capture, stack->caplevel, captop); + captop = stack->caplevel; + p = stack->p; + continue; + } + case ICloseRunTime: { + CapState cs; + int rem, res, n; + int fr = lua_gettop(L) + 1; /* stack index of first result */ + cs.s = o; cs.L = L; cs.ocap = capture; cs.ptop = ptop; + n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ + captop -= n; /* remove nested captures */ + fr -= rem; /* 'rem' items were popped from Lua stack */ + res = resdyncaptures(L, fr, s - o, e - o); /* get result */ + if (res == -1) /* fail? */ + goto fail; + s = o + res; /* else update current position */ + n = lua_gettop(L) - fr + 1; /* number of new captures */ + ndyncap += n - rem; /* update number of dynamic captures */ + if (n > 0) { /* any new capture? */ + if ((captop += n + 2) >= capsize) { + capture = doublecap(L, capture, captop, ptop); + capsize = 2 * captop; + } + /* add new captures to 'capture' list */ + adddyncaptures(s, capture + captop - n - 2, n, fr); + } + p++; + continue; + } + case ICloseCapture: { + const char *s1 = s; + assert(captop > 0); + /* if possible, turn capture into a full capture */ + if (capture[captop - 1].siz == 0 && + s1 - capture[captop - 1].s < UCHAR_MAX) { + capture[captop - 1].siz = s1 - capture[captop - 1].s + 1; + p++; + continue; + } + else { + capture[captop].siz = 1; /* mark entry as closed */ + capture[captop].s = s; + goto pushcapture; + } + } + case IOpenCapture: + capture[captop].siz = 0; /* mark entry as open */ + capture[captop].s = s; + goto pushcapture; + case IFullCapture: + capture[captop].siz = getoff(p) + 1; /* save capture size */ + capture[captop].s = s - getoff(p); + /* goto pushcapture; */ + pushcapture: { + capture[captop].idx = p->i.key; + capture[captop].kind = getkind(p); + if (++captop >= capsize) { + capture = doublecap(L, capture, captop, ptop); + capsize = 2 * captop; + } + p++; + continue; + } + default: assert(0); return NULL; + } + } +} + +/* }====================================================== */ + + diff --git a/3rd/lpeg/lpvm.h b/3rd/lpeg/lpvm.h new file mode 100644 index 00000000..6a2a558d --- /dev/null +++ b/3rd/lpeg/lpvm.h @@ -0,0 +1,63 @@ +/* +** $Id: lpvm.h,v 1.2 2013/04/03 20:37:18 roberto Exp $ +*/ + +#if !defined(lpvm_h) +#define lpvm_h + +#include "lpcap.h" + + +/* Virtual Machine's instructions */ +typedef enum Opcode { + IAny, /* if no char, fail */ + IChar, /* if char != aux, fail */ + ISet, /* if char not in buff, fail */ + ITestAny, /* in no char, jump to 'offset' */ + ITestChar, /* if char != aux, jump to 'offset' */ + ITestSet, /* if char not in buff, jump to 'offset' */ + ISpan, /* read a span of chars in buff */ + IBehind, /* walk back 'aux' characters (fail if not possible) */ + IRet, /* return from a rule */ + IEnd, /* end of pattern */ + IChoice, /* stack a choice; next fail will jump to 'offset' */ + IJmp, /* jump to 'offset' */ + ICall, /* call rule at 'offset' */ + IOpenCall, /* call rule number 'key' (must be closed to a ICall) */ + ICommit, /* pop choice and jump to 'offset' */ + IPartialCommit, /* update top choice to current position and jump */ + IBackCommit, /* "fails" but jump to its own 'offset' */ + IFailTwice, /* pop one choice and then fail */ + IFail, /* go back to saved state on choice and jump to saved offset */ + IGiveup, /* internal use */ + IFullCapture, /* complete capture of last 'off' chars */ + IOpenCapture, /* start a capture */ + ICloseCapture, + ICloseRunTime +} Opcode; + + + +typedef union Instruction { + struct Inst { + byte code; + byte aux; + short key; + } i; + int offset; + byte buff[1]; +} Instruction; + + +int getposition (lua_State *L, int t, int i); +void printpatt (Instruction *p, int n); +const char *match (lua_State *L, const char *o, const char *s, const char *e, + Instruction *op, Capture *capture, int ptop); +int verify (lua_State *L, Instruction *op, const Instruction *p, + Instruction *e, int postable, int rule); +void checkrule (lua_State *L, Instruction *op, int from, int to, + int postable, int rule); + + +#endif + diff --git a/3rd/lpeg/makefile b/3rd/lpeg/makefile new file mode 100644 index 00000000..57a18fb3 --- /dev/null +++ b/3rd/lpeg/makefile @@ -0,0 +1,55 @@ +LIBNAME = lpeg +LUADIR = /usr/include/lua5.1/ + +COPT = -O2 +# COPT = -DLPEG_DEBUG -g + +CWARNS = -Wall -Wextra -pedantic \ + -Waggregate-return \ + -Wcast-align \ + -Wcast-qual \ + -Wdisabled-optimization \ + -Wpointer-arith \ + -Wshadow \ + -Wsign-compare \ + -Wundef \ + -Wwrite-strings \ + -Wbad-function-cast \ + -Wdeclaration-after-statement \ + -Wmissing-prototypes \ + -Wnested-externs \ + -Wstrict-prototypes \ +# -Wunreachable-code \ + + +CFLAGS = $(CWARNS) $(COPT) -ansi -I$(LUADIR) -fPIC +CC = gcc + +FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o + +# For Linux +linux: + make lpeg.so "DLLFLAGS = -shared -fPIC" + +# For Mac OS +macosx: + make lpeg.so "DLLFLAGS = -bundle -undefined dynamic_lookup" + +lpeg.so: $(FILES) + env $(CC) $(DLLFLAGS) $(FILES) -o lpeg.so + +$(FILES): makefile + +test: test.lua re.lua lpeg.so + ./test.lua + +clean: + rm -f $(FILES) lpeg.so + + +lpcap.o: lpcap.c lpcap.h lptypes.h +lpcode.o: lpcode.c lptypes.h lpcode.h lptree.h lpvm.h lpcap.h +lpprint.o: lpprint.c lptypes.h lpprint.h lptree.h lpvm.h lpcap.h +lptree.o: lptree.c lptypes.h lpcap.h lpcode.h lptree.h lpvm.h lpprint.h +lpvm.o: lpvm.c lpcap.h lptypes.h lpvm.h lpprint.h lptree.h + diff --git a/3rd/lpeg/re.html b/3rd/lpeg/re.html new file mode 100644 index 00000000..4717ec2b --- /dev/null +++ b/3rd/lpeg/re.html @@ -0,0 +1,498 @@ + + + + LPeg.re - Regex syntax for LPEG + + + + + + + +
+ +
+ +
LPeg.re
+
+ Regex syntax for LPEG +
+
+ +
+ + + +
+ +

The re Module

+ +

+The re module +(provided by file re.lua in the distribution) +supports a somewhat conventional regex syntax +for pattern usage within LPeg. +

+ +

+The next table summarizes re's syntax. +A p represents an arbitrary pattern; +num represents a number ([0-9]+); +name represents an identifier +([a-zA-Z][a-zA-Z0-9_]*). +Constructions are listed in order of decreasing precedence. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SyntaxDescription
( p ) grouping
'string' literal string
"string" literal string
[class] character class
. any character
%namepattern defs[name] or a pre-defined pattern
namenon terminal
<name>non terminal
{} position capture
{ p } simple capture
{: p :} anonymous group capture
{:name: p :} named group capture
{~ p ~} substitution capture
{| p |} table capture
=name back reference +
p ? optional match
p * zero or more repetitions
p + one or more repetitions
p^num exactly n repetitions
p^+numat least n repetitions
p^-numat most n repetitions
p -> 'string' string capture
p -> "string" string capture
p -> num numbered capture
p -> name function/query/string capture +equivalent to p / defs[name]
p => name match-time capture +equivalent to lpeg.Cmt(p, defs[name])
& p and predicate
! p not predicate
p1 p2 concatenation
p1 / p2 ordered choice
(name <- p)+ grammar
+

+Any space appearing in a syntax description can be +replaced by zero or more space characters and Lua-style comments +(-- until end of line). +

+ +

+Character classes define sets of characters. +An initial ^ complements the resulting set. +A range x-y includes in the set +all characters with codes between the codes of x and y. +A pre-defined class %name includes all +characters of that class. +A simple character includes itself in the set. +The only special characters inside a class are ^ +(special only if it is the first character); +] +(can be included in the set as the first character, +after the optional ^); +% (special only if followed by a letter); +and - +(can be included in the set as the first or the last character). +

+ +

+Currently the pre-defined classes are similar to those from the +Lua's string library +(%a for letters, +%A for non letters, etc.). +There is also a class %nl +containing only the newline character, +which is particularly handy for grammars written inside long strings, +as long strings do not interpret escape sequences like \n. +

+ + +

Functions

+ +

re.compile (string, [, defs])

+

+Compiles the given string and +returns an equivalent LPeg pattern. +The given string may define either an expression or a grammar. +The optional defs table provides extra Lua values +to be used by the pattern. +

+ +

re.find (subject, pattern [, init])

+

+Searches the given pattern in the given subject. +If it finds a match, +returns the index where this occurrence starts and +the index where it ends. +Otherwise, returns nil. +

+ +

+An optional numeric argument init makes the search +starts at that position in the subject string. +As usual in Lua libraries, +a negative value counts from the end. +

+ +

re.gsub (subject, pattern, replacement)

+

+Does a global substitution, +replacing all occurrences of pattern +in the given subject by replacement. + +

re.match (subject, pattern)

+

+Matches the given pattern against the given subject, +returning all captures. +

+ +

re.updatelocale ()

+

+Updates the pre-defined character classes to the current locale. +

+ + +

Some Examples

+ +

A complete simple program

+

+The next code shows a simple complete Lua program using +the re module: +

+
+local re = require"re"
+
+-- find the position of the first numeral in a string
+print(re.find("the number 423 is odd", "[0-9]+"))  --> 12    14
+
+-- returns all words in a string
+print(re.match("the number 423 is odd", "({%a+} / .)*"))
+--> the    number    is    odd
+
+-- returns the first numeral in a string
+print(re.match("the number 423 is odd", "s <- {%d+} / . s"))
+--> 423
+
+print(re.gsub("hello World", "[aeiou]", "."))
+--> h.ll. W.rld
+
+ + +

Balanced parentheses

+

+The following call will produce the same pattern produced by the +Lua expression in the +balanced parentheses example: +

+
+b = re.compile[[  balanced <- "(" ([^()] / balanced)* ")"  ]]
+
+ +

String reversal

+

+The next example reverses a string: +

+
+rev = re.compile[[ R <- (!.) -> '' / ({.} R) -> '%2%1']]
+print(rev:match"0123456789")   --> 9876543210
+
+ +

CSV decoder

+

+The next example replicates the CSV decoder: +

+
+record = re.compile[[
+  record <- {| field (',' field)* |} (%nl / !.)
+  field <- escaped / nonescaped
+  nonescaped <- { [^,"%nl]* }
+  escaped <- '"' {~ ([^"] / '""' -> '"')* ~} '"'
+]]
+
+ +

Lua's long strings

+

+The next example matches Lua long strings: +

+
+c = re.compile([[
+  longstring <- ('[' {:eq: '='* :} '[' close)
+  close <- ']' =eq ']' / . close
+]])
+
+print(c:match'[==[]]===]]]]==]===[]')   --> 17
+
+ +

Abstract Syntax Trees

+

+This example shows a simple way to build an +abstract syntax tree (AST) for a given grammar. +To keep our example simple, +let us consider the following grammar +for lists of names: +

+
+p = re.compile[[
+      listname <- (name s)*
+      name <- [a-z][a-z]*
+      s <- %s*
+]]
+
+

+Now, we will add captures to build a corresponding AST. +As a first step, the pattern will build a table to +represent each non terminal; +terminals will be represented by their corresponding strings: +

+
+c = re.compile[[
+      listname <- {| (name s)* |}
+      name <- {| {[a-z][a-z]*} |}
+      s <- %s*
+]]
+
+

+Now, a match against "hi hello bye" +results in the table +{{"hi"}, {"hello"}, {"bye"}}. +

+

+For such a simple grammar, +this AST is more than enough; +actually, the tables around each single name +are already overkilling. +More complex grammars, +however, may need some more structure. +Specifically, +it would be useful if each table had +a tag field telling what non terminal +that table represents. +We can add such a tag using +named group captures: +

+
+x = re.compile[[
+      listname <- {| {:tag: '' -> 'list':} (name s)* |}
+      name <- {| {:tag: '' -> 'id':} {[a-z][a-z]*} |}
+      s <- ' '*
+]]
+
+

+With these group captures, +a match against "hi hello bye" +results in the following table: +

+
+{tag="list",
+  {tag="id", "hi"},
+  {tag="id", "hello"},
+  {tag="id", "bye"}
+}
+
+ + +

Indented blocks

+

+This example breaks indented blocks into tables, +respecting the indentation: +

+
+p = re.compile[[
+  block <- {| {:ident:' '*:} line
+           ((=ident !' ' line) / &(=ident ' ') block)* |}
+  line <- {[^%nl]*} %nl
+]]
+
+

+As an example, +consider the following text: +

+
+t = p:match[[
+first line
+  subline 1
+  subline 2
+second line
+third line
+  subline 3.1
+    subline 3.1.1
+  subline 3.2
+]]
+
+

+The resulting table t will be like this: +

+
+   {'first line'; {'subline 1'; 'subline 2'; ident = '  '};
+    'second line';
+    'third line'; { 'subline 3.1'; {'subline 3.1.1'; ident = '    '};
+                    'subline 3.2'; ident = '  '};
+    ident = ''}
+
+ +

Macro expander

+

+This example implements a simple macro expander. +Macros must be defined as part of the pattern, +following some simple rules: +

+
+p = re.compile[[
+      text <- {~ item* ~}
+      item <- macro / [^()] / '(' item* ')'
+      arg <- ' '* {~ (!',' item)* ~}
+      args <- '(' arg (',' arg)* ')'
+      -- now we define some macros
+      macro <- ('apply' args) -> '%1(%2)'
+             / ('add' args) -> '%1 + %2'
+             / ('mul' args) -> '%1 * %2'
+]]
+
+print(p:match"add(mul(a,b), apply(f,x))")   --> a * b + f(x)
+
+

+A text is a sequence of items, +wherein we apply a substitution capture to expand any macros. +An item is either a macro, +any character different from parentheses, +or a parenthesized expression. +A macro argument (arg) is a sequence +of items different from a comma. +(Note that a comma may appear inside an item, +e.g., inside a parenthesized expression.) +Again we do a substitution capture to expand any macro +in the argument before expanding the outer macro. +args is a list of arguments separated by commas. +Finally we define the macros. +Each macro is a string substitution; +it replaces the macro name and its arguments by its corresponding string, +with each %n replaced by the n-th argument. +

+ +

Patterns

+

+This example shows the complete syntax +of patterns accepted by re. +

+
+p = [=[
+
+pattern         <- exp !.
+exp             <- S (alternative / grammar)
+
+alternative     <- seq ('/' S seq)*
+seq             <- prefix*
+prefix          <- '&' S prefix / '!' S prefix / suffix
+suffix          <- primary S (([+*?]
+                            / '^' [+-]? num
+                            / '->' S (string / '{}' / name)
+                            / '=>' S name) S)*
+
+primary         <- '(' exp ')' / string / class / defined
+                 / '{:' (name ':')? exp ':}'
+                 / '=' name
+                 / '{}'
+                 / '{~' exp '~}'
+                 / '{' exp '}'
+                 / '.'
+                 / name S !arrow
+                 / '<' name '>'          -- old-style non terminals
+
+grammar         <- definition+
+definition      <- name S arrow exp
+
+class           <- '[' '^'? item (!']' item)* ']'
+item            <- defined / range / .
+range           <- . '-' [^]]
+
+S               <- (%s / '--' [^%nl]*)*   -- spaces and comments
+name            <- [A-Za-z][A-Za-z0-9_]*
+arrow           <- '<-'
+num             <- [0-9]+
+string          <- '"' [^"]* '"' / "'" [^']* "'"
+defined         <- '%' name
+
+]=]
+
+print(re.match(p, p))   -- a self description must match itself
+
+ + + +

License

+ +

+Copyright © 2008-2010 Lua.org, PUC-Rio. +

+

+Permission is hereby granted, free of charge, +to any person obtaining a copy of this software and +associated documentation files (the "Software"), +to deal in the Software without restriction, +including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, +and to permit persons to whom the Software is +furnished to do so, +subject to the following conditions: +

+ +

+The above copyright notice and this permission notice +shall be included in all copies or substantial portions of the Software. +

+ +

+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +

+ +
+ +
+ +
+

+$Id: re.html,v 1.21 2013/03/28 20:43:30 roberto Exp $ +

+
+ +
+ + + diff --git a/3rd/lpeg/re.lua b/3rd/lpeg/re.lua new file mode 100644 index 00000000..3b9974fd --- /dev/null +++ b/3rd/lpeg/re.lua @@ -0,0 +1,259 @@ +-- $Id: re.lua,v 1.44 2013/03/26 20:11:40 roberto Exp $ + +-- imported functions and modules +local tonumber, type, print, error = tonumber, type, print, error +local setmetatable = setmetatable +local m = require"lpeg" + +-- 'm' will be used to parse expressions, and 'mm' will be used to +-- create expressions; that is, 're' runs on 'm', creating patterns +-- on 'mm' +local mm = m + +-- pattern's metatable +local mt = getmetatable(mm.P(0)) + + + +-- No more global accesses after this point +local version = _VERSION +if version == "Lua 5.2" then _ENV = nil end + + +local any = m.P(1) + + +-- Pre-defined names +local Predef = { nl = m.P"\n" } + + +local mem +local fmem +local gmem + + +local function updatelocale () + mm.locale(Predef) + Predef.a = Predef.alpha + Predef.c = Predef.cntrl + Predef.d = Predef.digit + Predef.g = Predef.graph + Predef.l = Predef.lower + Predef.p = Predef.punct + Predef.s = Predef.space + Predef.u = Predef.upper + Predef.w = Predef.alnum + Predef.x = Predef.xdigit + Predef.A = any - Predef.a + Predef.C = any - Predef.c + Predef.D = any - Predef.d + Predef.G = any - Predef.g + Predef.L = any - Predef.l + Predef.P = any - Predef.p + Predef.S = any - Predef.s + Predef.U = any - Predef.u + Predef.W = any - Predef.w + Predef.X = any - Predef.x + mem = {} -- restart memoization + fmem = {} + gmem = {} + local mt = {__mode = "v"} + setmetatable(mem, mt) + setmetatable(fmem, mt) + setmetatable(gmem, mt) +end + + +updatelocale() + + + +local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end) + + +local function getdef (id, defs) + local c = defs and defs[id] + if not c then error("undefined name: " .. id) end + return c +end + + +local function patt_error (s, i) + local msg = (#s < i + 20) and s:sub(i) + or s:sub(i,i+20) .. "..." + msg = ("pattern error near '%s'"):format(msg) + error(msg, 2) +end + +local function mult (p, n) + local np = mm.P(true) + while n >= 1 do + if n%2 >= 1 then np = np * p end + p = p * p + n = n/2 + end + return np +end + +local function equalcap (s, i, c) + if type(c) ~= "string" then return nil end + local e = #c + i + if s:sub(i, e - 1) == c then return e else return nil end +end + + +local S = (Predef.space + "--" * (any - Predef.nl)^0)^0 + +local name = m.R("AZ", "az", "__") * m.R("AZ", "az", "__", "09")^0 + +local arrow = S * "<-" + +local seq_follow = m.P"/" + ")" + "}" + ":}" + "~}" + "|}" + (name * arrow) + -1 + +name = m.C(name) + + +-- a defined name only have meaning in a given environment +local Def = name * m.Carg(1) + +local num = m.C(m.R"09"^1) * S / tonumber + +local String = "'" * m.C((any - "'")^0) * "'" + + '"' * m.C((any - '"')^0) * '"' + + +local defined = "%" * Def / function (c,Defs) + local cat = Defs and Defs[c] or Predef[c] + if not cat then error ("name '" .. c .. "' undefined") end + return cat +end + +local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R + +local item = defined + Range + m.C(any) + +local Class = + "[" + * (m.C(m.P"^"^-1)) -- optional complement symbol + * m.Cf(item * (item - "]")^0, mt.__add) / + function (c, p) return c == "^" and any - p or p end + * "]" + +local function adddef (t, k, exp) + if t[k] then + error("'"..k.."' already defined as a rule") + else + t[k] = exp + end + return t +end + +local function firstdef (n, r) return adddef({n}, n, r) end + + +local function NT (n, b) + if not b then + error("rule '"..n.."' used outside a grammar") + else return mm.V(n) + end +end + + +local exp = m.P{ "Exp", + Exp = S * ( m.V"Grammar" + + m.Cf(m.V"Seq" * ("/" * S * m.V"Seq")^0, mt.__add) ); + Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix"^0 , mt.__mul) + * (#seq_follow + patt_error); + Prefix = "&" * S * m.V"Prefix" / mt.__len + + "!" * S * m.V"Prefix" / mt.__unm + + m.V"Suffix"; + Suffix = m.Cf(m.V"Primary" * S * + ( ( m.P"+" * m.Cc(1, mt.__pow) + + m.P"*" * m.Cc(0, mt.__pow) + + m.P"?" * m.Cc(-1, mt.__pow) + + "^" * ( m.Cg(num * m.Cc(mult)) + + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow)) + ) + + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div)) + + m.P"{}" * m.Cc(nil, m.Ct) + + m.Cg(Def / getdef * m.Cc(mt.__div)) + ) + + "=>" * S * m.Cg(Def / getdef * m.Cc(m.Cmt)) + ) * S + )^0, function (a,b,f) return f(a,b) end ); + Primary = "(" * m.V"Exp" * ")" + + String / mm.P + + Class + + defined + + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" / + function (n, p) return mm.Cg(p, n) end + + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end + + m.P"{}" / mm.Cp + + "{~" * m.V"Exp" * "~}" / mm.Cs + + "{|" * m.V"Exp" * "|}" / mm.Ct + + "{" * m.V"Exp" * "}" / mm.C + + m.P"." * m.Cc(any) + + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT; + Definition = name * arrow * m.V"Exp"; + Grammar = m.Cg(m.Cc(true), "G") * + m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0, + adddef) / mm.P +} + +local pattern = S * m.Cg(m.Cc(false), "G") * exp / mm.P * (-any + patt_error) + + +local function compile (p, defs) + if mm.type(p) == "pattern" then return p end -- already compiled + local cp = pattern:match(p, 1, defs) + if not cp then error("incorrect pattern", 3) end + return cp +end + +local function match (s, p, i) + local cp = mem[p] + if not cp then + cp = compile(p) + mem[p] = cp + end + return cp:match(s, i or 1) +end + +local function find (s, p, i) + local cp = fmem[p] + if not cp then + cp = compile(p) / 0 + cp = mm.P{ mm.Cp() * cp * mm.Cp() + 1 * mm.V(1) } + fmem[p] = cp + end + local i, e = cp:match(s, i or 1) + if i then return i, e - 1 + else return i + end +end + +local function gsub (s, p, rep) + local g = gmem[p] or {} -- ensure gmem[p] is not collected while here + gmem[p] = g + local cp = g[rep] + if not cp then + cp = compile(p) + cp = mm.Cs((cp / rep + 1)^0) + g[rep] = cp + end + return cp:match(s) +end + + +-- exported names +local re = { + compile = compile, + match = match, + find = find, + gsub = gsub, + updatelocale = updatelocale, +} + +if version == "Lua 5.1" then _G.re = re end + +return re diff --git a/3rd/lpeg/test.lua b/3rd/lpeg/test.lua new file mode 100755 index 00000000..1d107ca5 --- /dev/null +++ b/3rd/lpeg/test.lua @@ -0,0 +1,1386 @@ +#!/usr/bin/env lua5.1 + +-- $Id: test.lua,v 1.101 2013/04/12 16:30:33 roberto Exp $ + +-- require"strict" -- just to be pedantic + +local m = require"lpeg" + + +-- for general use +local a, b, c, d, e, f, g, p, t + + +-- compatibility with Lua 5.2 +local unpack = rawget(table, "unpack") or unpack +local loadstring = rawget(_G, "loadstring") or load + + +-- most tests here do not need much stack space +m.setmaxstack(5) + +local any = m.P(1) +local space = m.S" \t\n"^0 + +local function checkeq (x, y, p) +if p then print(x,y) end + if type(x) ~= "table" then assert(x == y) + else + for k,v in pairs(x) do checkeq(v, y[k], p) end + for k,v in pairs(y) do checkeq(v, x[k], p) end + end +end + + +local mt = getmetatable(m.P(1)) + + +local allchar = {} +for i=0,255 do allchar[i + 1] = i end +allchar = string.char(unpack(allchar)) +assert(#allchar == 256) + +local function cs2str (c) + return m.match(m.Cs((c + m.P(1)/"")^0), allchar) +end + +local function eqcharset (c1, c2) + assert(cs2str(c1) == cs2str(c2)) +end + + +print"General tests for LPeg library" + +assert(type(m.version()) == "string") +print("version " .. m.version()) +assert(m.type("alo") ~= "pattern") +assert(m.type(io.input) ~= "pattern") +assert(m.type(m.P"alo") == "pattern") + +-- tests for some basic optimizations +assert(m.match(m.P(false) + "a", "a") == 2) +assert(m.match(m.P(true) + "a", "a") == 1) +assert(m.match("a" + m.P(false), "b") == nil) +assert(m.match("a" + m.P(true), "b") == 1) + +assert(m.match(m.P(false) * "a", "a") == nil) +assert(m.match(m.P(true) * "a", "a") == 2) +assert(m.match("a" * m.P(false), "a") == nil) +assert(m.match("a" * m.P(true), "a") == 2) + +assert(m.match(#m.P(false) * "a", "a") == nil) +assert(m.match(#m.P(true) * "a", "a") == 2) +assert(m.match("a" * #m.P(false), "a") == nil) +assert(m.match("a" * #m.P(true), "a") == 2) + + +-- tests for locale +do + assert(m.locale(m) == m) + local t = {} + assert(m.locale(t, m) == t) + local x = m.locale() + for n,v in pairs(x) do + assert(type(n) == "string") + eqcharset(v, m[n]) + end +end + + +assert(m.match(3, "aaaa")) +assert(m.match(4, "aaaa")) +assert(not m.match(5, "aaaa")) +assert(m.match(-3, "aa")) +assert(not m.match(-3, "aaa")) +assert(not m.match(-3, "aaaa")) +assert(not m.match(-4, "aaaa")) +assert(m.P(-5):match"aaaa") + +assert(m.match("a", "alo") == 2) +assert(m.match("al", "alo") == 3) +assert(not m.match("alu", "alo")) +assert(m.match(true, "") == 1) + +local digit = m.S"0123456789" +local upper = m.S"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +local lower = m.S"abcdefghijklmnopqrstuvwxyz" +local letter = m.S"" + upper + lower +local alpha = letter + digit + m.R() + +eqcharset(m.S"", m.P(false)) +eqcharset(upper, m.R("AZ")) +eqcharset(lower, m.R("az")) +eqcharset(upper + lower, m.R("AZ", "az")) +eqcharset(upper + lower, m.R("AZ", "cz", "aa", "bb", "90")) +eqcharset(digit, m.S"01234567" + "8" + "9") +eqcharset(upper, letter - lower) +eqcharset(m.S(""), m.R()) +assert(cs2str(m.S("")) == "") + +eqcharset(m.S"\0", "\0") +eqcharset(m.S"\1\0\2", m.R"\0\2") +eqcharset(m.S"\1\0\2", m.R"\1\2" + "\0") +eqcharset(m.S"\1\0\2" - "\0", m.R"\1\2") + +local word = alpha^1 * (1 - alpha)^0 + +assert((word^0 * -1):match"alo alo") +assert(m.match(word^1 * -1, "alo alo")) +assert(m.match(word^2 * -1, "alo alo")) +assert(not m.match(word^3 * -1, "alo alo")) + +assert(not m.match(word^-1 * -1, "alo alo")) +assert(m.match(word^-2 * -1, "alo alo")) +assert(m.match(word^-3 * -1, "alo alo")) + +local eos = m.P(-1) + +assert(m.match(digit^0 * letter * digit * eos, "1298a1")) +assert(not m.match(digit^0 * letter * eos, "1257a1")) + +b = { + [1] = "(" * (((1 - m.S"()") + #m.P"(" * m.V(1))^0) * ")" +} + +assert(m.match(b, "(al())()")) +assert(not m.match(b * eos, "(al())()")) +assert(m.match(b * eos, "((al())()(é))")) +assert(not m.match(b, "(al()()")) + +assert(not m.match(letter^1 - "for", "foreach")) +assert(m.match(letter^1 - ("for" * eos), "foreach")) +assert(not m.match(letter^1 - ("for" * eos), "for")) + +function basiclookfor (p) + return m.P { + [1] = p + (1 * m.V(1)) + } +end + +function caplookfor (p) + return basiclookfor(p:C()) +end + +assert(m.match(caplookfor(letter^1), " 4achou123...") == "achou") +a = {m.match(caplookfor(letter^1)^0, " two words, one more ")} +checkeq(a, {"two", "words", "one", "more"}) + +assert(m.match( basiclookfor((#m.P(b) * 1) * m.Cp()), " ( (a)") == 7) + +a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "123")} +checkeq(a, {"123", "d"}) + +a = {m.match(m.C(digit^1) * "d" * -1 + m.C(letter^1 * m.Cc"l"), "123d")} +checkeq(a, {"123"}) + +a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "abcd")} +checkeq(a, {"abcd", "l"}) + +a = {m.match(m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} +checkeq(a, {10,20,30,2}) +a = {m.match(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} +checkeq(a, {1,10,20,30,2}) +a = m.match(m.Ct(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') +checkeq(a, {1,10,20,30,2}) +a = m.match(m.Ct(m.Cp() * m.Cc(7,8) * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') +checkeq(a, {1,7,8,10,20,30,2}) +a = {m.match(m.Cc() * m.Cc() * m.Cc(1) * m.Cc(2,3,4) * m.Cc() * 'a', 'aaa')} +checkeq(a, {1,2,3,4}) + +a = {m.match(m.Cp() * letter^1 * m.Cp(), "abcd")} +checkeq(a, {1, 5}) + + +t = {m.match({[1] = m.C(m.C(1) * m.V(1) + -1)}, "abc")} +checkeq(t, {"abc", "a", "bc", "b", "c", "c", ""}) + + +-- test for small capture boundary +for i = 250,260 do + assert(#m.match(m.C(i), string.rep('a', i)) == i) + assert(#m.match(m.C(m.C(i)), string.rep('a', i)) == i) +end + + +-- tests for any*n and any*-n +for n = 1, 550 do + local x_1 = string.rep('x', n - 1) + local x = x_1 .. 'a' + assert(not m.P(n):match(x_1)) + assert(m.P(n):match(x) == n + 1) + assert(n < 4 or m.match(m.P(n) + "xxx", x_1) == 4) + assert(m.C(n):match(x) == x) + assert(m.C(m.C(n)):match(x) == x) + assert(m.P(-n):match(x_1) == 1) + assert(not m.P(-n):match(x)) + assert(n < 13 or m.match(m.Cc(20) * ((n - 13) * m.P(10)) * 3, x) == 20) + local n3 = math.floor(n/3) + assert(m.match(n3 * m.Cp() * n3 * n3, x) == n3 + 1) +end + +-- true values +assert(m.P(0):match("x") == 1) +assert(m.P(0):match("") == 1) +assert(m.C(0):match("x") == "") + +assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxu") == 1) +assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxuxuxuxu") == 0) +assert(m.match(m.C(m.P(2)^1), "abcde") == "abcd") +p = m.Cc(0) * 1 + m.Cc(1) * 2 + m.Cc(2) * 3 + m.Cc(3) * 4 + + +-- test for alternation optimization +assert(m.match(m.P"a"^1 + "ab" + m.P"x"^0, "ab") == 2) +assert(m.match((m.P"a"^1 + "ab" + m.P"x"^0 * 1)^0, "ab") == 3) +assert(m.match(m.P"ab" + "cd" + "" + "cy" + "ak", "98") == 1) +assert(m.match(m.P"ab" + "cd" + "ax" + "cy", "ax") == 3) +assert(m.match("a" * m.P"b"^0 * "c" + "cd" + "ax" + "cy", "ax") == 3) +assert(m.match((m.P"ab" + "cd" + "ax" + "cy")^0, "ax") == 3) +assert(m.match(m.P(1) * "x" + m.S"" * "xu" + "ay", "ay") == 3) +assert(m.match(m.P"abc" + "cde" + "aka", "aka") == 4) +assert(m.match(m.S"abc" * "x" + "cde" + "aka", "ax") == 3) +assert(m.match(m.S"abc" * "x" + "cde" + "aka", "aka") == 4) +assert(m.match(m.S"abc" * "x" + "cde" + "aka", "cde") == 4) +assert(m.match(m.S"abc" * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) +assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "ax") == 3) +assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "aka") == 4) +assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "cde") == 4) +assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) +assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "ax") == 3) +assert(m.match(m.P(1) * "x" + "cde" + m.S"ab" * "ka", "aka") == 4) +assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "aka") == 4) +assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "cde") == 4) +assert(m.match(m.P"eb" + "cd" + m.P"e"^0 + "x", "ee") == 3) +assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "abcd") == 3) +assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "eeex") == 4) +assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "cd") == 3) +assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "x") == 1) +assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x" + "", "zee") == 1) +assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "abcd") == 3) +assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "eeex") == 4) +assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "cd") == 3) +assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "x") == 2) +assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x" + "", "zee") == 1) +assert(not m.match(("aa" * m.P"bc"^-1 + "aab") * "e", "aabe")) + +assert(m.match("alo" * (m.P"\n" + -1), "alo") == 4) + + +-- bug in 0.12 (rc1) +assert(m.match((m.P"\128\187\191" + m.S"abc")^0, "\128\187\191") == 4) + +assert(m.match(m.S"\0\128\255\127"^0, string.rep("\0\128\255\127", 10)) == + 4*10 + 1) + +-- optimizations with optional parts +assert(m.match(("ab" * -m.P"c")^-1, "abc") == 1) +assert(m.match(("ab" * #m.P"c")^-1, "abd") == 1) +assert(m.match(("ab" * m.B"c")^-1, "ab") == 1) +assert(m.match(("ab" * m.P"cd"^0)^-1, "abcdcdc") == 7) + +assert(m.match(m.P"ab"^-1 - "c", "abcd") == 3) + +p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 +assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) + + +pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" +assert(m.match(m.Cs((m.P"1" / "a" + m.P"5" / "b" + m.P"9" / "c" + 1)^0), pi) == + m.match(m.Cs((m.P(1) / {["1"] = "a", ["5"] = "b", ["9"] = "c"})^0), pi)) +print"+" + + +-- tests for capture optimizations +assert(m.match((m.P(3) + 4 * m.Cp()) * "a", "abca") == 5) +t = {m.match(((m.P"a" + m.Cp()) * m.P"x")^0, "axxaxx")} +checkeq(t, {3, 6}) + + +-- tests for numbered captures +p = m.C(1) +assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 3, "abcdefgh") == "a") +assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 1, "abcdefgh") == "abcdef") +assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 4, "abcdefgh") == "bc") +assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 0, "abcdefgh") == 7) + +a, b, c = m.match(p * (m.C(p * m.C(2)) * m.C(3) / 4) * p, "abcdefgh") +assert(a == "a" and b == "efg" and c == "h") + +-- test for table captures +t = m.match(m.Ct(letter^1), "alo") +checkeq(t, {}) + +t, n = m.match(m.Ct(m.C(letter)^1) * m.Cc"t", "alo") +assert(n == "t" and table.concat(t) == "alo") + +t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") +assert(table.concat(t, ";") == "alo;a;l;o") + +t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") +assert(table.concat(t, ";") == "alo;a;l;o") + +t = m.match(m.Ct(m.Ct((m.Cp() * letter * m.Cp())^1)), "alo") +assert(table.concat(t[1], ";") == "1;2;2;3;3;4") + +t = m.match(m.Ct(m.C(m.C(1) * 1 * m.C(1))), "alo") +checkeq(t, {"alo", "a", "o"}) + + +-- tests for groups +p = m.Cg(1) -- no capture +assert(p:match('x') == 'x') +p = m.Cg(m.P(true)/function () end * 1) -- no value +assert(p:match('x') == 'x') +p = m.Cg(m.Cg(m.Cg(m.C(1)))) +assert(p:match('x') == 'x') +p = m.Cg(m.Cg(m.Cg(m.C(1))^0) * m.Cg(m.Cc(1) * m.Cc(2))) +t = {p:match'abc'} +checkeq(t, {'a', 'b', 'c', 1, 2}) + +p = m.Ct(m.Cg(m.Cc(10), "hi") * m.C(1)^0 * m.Cg(m.Cc(20), "ho")) +t = p:match'' +checkeq(t, {hi = 10, ho = 20}) +t = p:match'abc' +checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) + + +-- test for error messages +local function checkerr (msg, ...) + assert(m.match({ m.P(msg) + 1 * m.V(1) }, select(2, pcall(...)))) +end + +checkerr("rule '1' may be left recursive", m.match, { m.V(1) * 'a' }, "a") +checkerr("rule '1' used outside a grammar", m.match, m.V(1), "") +checkerr("rule 'hiii' used outside a grammar", m.match, m.V('hiii'), "") +checkerr("rule 'hiii' undefined in given grammar", m.match, { m.V('hiii') }, "") +checkerr("undefined in given grammar", m.match, { m.V{} }, "") + +checkerr("rule 'A' is not a pattern", m.P, { m.P(1), A = {} }) +checkerr("grammar has no initial rule", m.P, { [print] = {} }) + +-- grammar with a long call chain before left recursion +p = {'a', + a = m.V'b' * m.V'c' * m.V'd' * m.V'a', + b = m.V'c', + c = m.V'd', + d = m.V'e', + e = m.V'f', + f = m.V'g', + g = m.P'' +} +checkerr("rule 'a' may be left recursive", m.match, p, "a") + + +-- tests for non-pattern as arguments to pattern functions + +p = { ('a' * m.V(1))^-1 } * m.P'b' * { 'a' * m.V(2); m.V(1)^-1 } +assert(m.match(p, "aaabaac") == 7) + +p = m.P'abc' * 2 * -5 * true * 'de' -- mix of numbers and strings and booleans + +assert(p:match("abc01de") == 8) +assert(p:match("abc01de3456") == nil) + +p = 'abc' * (2 * (-5 * (true * m.P'de'))) + +assert(p:match("abc01de") == 8) +assert(p:match("abc01de3456") == nil) + +p = { m.V(2), m.P"abc" } * + (m.P{ "xx", xx = m.P"xx" } + { "x", x = m.P"a" * m.V"x" + "" }) +assert(p:match("abcaaaxx") == 7) +assert(p:match("abcxx") == 6) + + +-- a large table capture +t = m.match(m.Ct(m.C('a')^0), string.rep("a", 10000)) +assert(#t == 10000 and t[1] == 'a' and t[#t] == 'a') + +print('+') + + +-- bug in 0.10 (rechecking a grammar, after tail-call optimization) +m.P{ m.P { (m.P(3) + "xuxu")^0 * m.V"xuxu", xuxu = m.P(1) } } + +local V = m.V + +local Space = m.S(" \n\t")^0 +local Number = m.C(m.R("09")^1) * Space +local FactorOp = m.C(m.S("+-")) * Space +local TermOp = m.C(m.S("*/")) * Space +local Open = "(" * Space +local Close = ")" * Space + + +local function f_factor (v1, op, v2, d) + assert(d == nil) + if op == "+" then return v1 + v2 + else return v1 - v2 + end +end + + +local function f_term (v1, op, v2, d) + assert(d == nil) + if op == "*" then return v1 * v2 + else return v1 / v2 + end +end + +G = m.P{ "Exp", + Exp = m.Cf(V"Factor" * m.Cg(FactorOp * V"Factor")^0, f_factor); + Factor = m.Cf(V"Term" * m.Cg(TermOp * V"Term")^0, f_term); + Term = Number / tonumber + Open * V"Exp" * Close; +} + +G = Space * G * -1 + +for _, s in ipairs{" 3 + 5*9 / (1+1) ", "3+4/2", "3+3-3- 9*2+3*9/1- 8"} do + assert(m.match(G, s) == loadstring("return "..s)()) +end + + +-- test for grammars (errors deep in calling non-terminals) +g = m.P{ + [1] = m.V(2) + "a", + [2] = "a" * m.V(3) * "x", + [3] = "b" * m.V(3) + "c" +} + +assert(m.match(g, "abbbcx") == 7) +assert(m.match(g, "abbbbx") == 2) + + +-- tests for \0 +assert(m.match(m.R("\0\1")^1, "\0\1\0") == 4) +assert(m.match(m.S("\0\1ab")^1, "\0\1\0a") == 5) +assert(m.match(m.P(1)^3, "\0\1\0a") == 5) +assert(not m.match(-4, "\0\1\0a")) +assert(m.match("\0\1\0a", "\0\1\0a") == 5) +assert(m.match("\0\0\0", "\0\0\0") == 4) +assert(not m.match("\0\0\0", "\0\0")) + + +-- tests for predicates +assert(not m.match(-m.P("a") * 2, "alo")) +assert(m.match(- -m.P("a") * 2, "alo") == 3) +assert(m.match(#m.P("a") * 2, "alo") == 3) +assert(m.match(##m.P("a") * 2, "alo") == 3) +assert(not m.match(##m.P("c") * 2, "alo")) +assert(m.match(m.Cs((##m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") +assert(m.match(m.Cs((#((#m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") +assert(m.match(m.Cs((- -m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") +assert(m.match(m.Cs((-((-m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") + +p = -m.P'a' * m.Cc(1) + -m.P'b' * m.Cc(2) + -m.P'c' * m.Cc(3) +assert(p:match('a') == 2 and p:match('') == 1 and p:match('b') == 1) + +p = -m.P'a' * m.Cc(10) + #m.P'a' * m.Cc(20) +assert(p:match('a') == 20 and p:match('') == 10 and p:match('b') == 10) + + + +-- look-behind predicate +assert(not m.match(m.B'a', 'a')) +assert(m.match(1 * m.B'a', 'a') == 2) +assert(not m.match(m.B(1), 'a')) +assert(m.match(1 * m.B(1), 'a') == 2) +assert(m.match(-m.B(1), 'a') == 1) +assert(m.match(m.B(250), string.rep('a', 250)) == nil) +assert(m.match(250 * m.B(250), string.rep('a', 250)) == 251) +assert(not pcall(m.B, 260)) + +B = #letter * -m.B(letter) + -letter * m.B(letter) +x = m.Ct({ (B * m.Cp())^-1 * (1 * m.V(1) + m.P(true)) }) +checkeq(m.match(x, 'ar cal c'), {1,3,4,7,9,10}) +checkeq(m.match(x, ' ar cal '), {2,4,5,8}) +checkeq(m.match(x, ' '), {}) +checkeq(m.match(x, 'aloalo'), {1,7}) + +assert(m.match(B, "a") == 1) +assert(m.match(1 * B, "a") == 2) +assert(not m.B(1 - letter):match("")) +assert((-m.B(letter)):match("") == 1) + +assert((4 * m.B(letter, 4)):match("aaaaaaaa") == 5) +assert(not (4 * m.B(#letter * 5)):match("aaaaaaaa")) +assert((4 * -m.B(#letter * 5)):match("aaaaaaaa") == 5) + +-- look-behind with grammars +assert(m.match('a' * m.B{'x', x = m.P(3)}, 'aaa') == nil) +assert(m.match('aa' * m.B{'x', x = m.P('aaa')}, 'aaaa') == nil) +assert(m.match('aaa' * m.B{'x', x = m.P('aaa')}, 'aaaaa') == 4) + + + +-- bug in 0.9 +assert(m.match(('a' * #m.P'b'), "ab") == 2) +assert(not m.match(('a' * #m.P'b'), "a")) + +assert(not m.match(#m.S'567', "")) +assert(m.match(#m.S'567' * 1, "6") == 2) + + +-- tests for Tail Calls + +p = m.P{ 'a' * m.V(1) + '' } +assert(p:match(string.rep('a', 1000)) == 1001) + +-- create a grammar for a simple DFA for even number of 0s and 1s +-- +-- ->1 <---0---> 2 +-- ^ ^ +-- | | +-- 1 1 +-- | | +-- V V +-- 3 <---0---> 4 +-- +-- this grammar should keep no backtracking information + +p = m.P{ + [1] = '0' * m.V(2) + '1' * m.V(3) + -1, + [2] = '0' * m.V(1) + '1' * m.V(4), + [3] = '0' * m.V(4) + '1' * m.V(1), + [4] = '0' * m.V(3) + '1' * m.V(2), +} + +assert(p:match(string.rep("00", 10000))) +assert(p:match(string.rep("01", 10000))) +assert(p:match(string.rep("011", 10000))) +assert(not p:match(string.rep("011", 10000) .. "1")) +assert(not p:match(string.rep("011", 10001))) + + +-- this grammar does need backtracking info. +local lim = 10000 +p = m.P{ '0' * m.V(1) + '0' } +assert(not pcall(m.match, p, string.rep("0", lim))) +m.setmaxstack(2*lim) +assert(not pcall(m.match, p, string.rep("0", lim))) +m.setmaxstack(2*lim + 4) +assert(pcall(m.match, p, string.rep("0", lim))) + +-- this repetition should not need stack space (only the call does) +p = m.P{ ('a' * m.V(1))^0 * 'b' + 'c' } +m.setmaxstack(200) +assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) + +m.setmaxstack(5) -- restore original limit + +-- tests for optional start position +assert(m.match("a", "abc", 1)) +assert(m.match("b", "abc", 2)) +assert(m.match("c", "abc", 3)) +assert(not m.match(1, "abc", 4)) +assert(m.match("a", "abc", -3)) +assert(m.match("b", "abc", -2)) +assert(m.match("c", "abc", -1)) +assert(m.match("abc", "abc", -4)) -- truncate to position 1 + +assert(m.match("", "abc", 10)) -- empty string is everywhere! +assert(m.match("", "", 10)) +assert(not m.match(1, "", 1)) +assert(not m.match(1, "", -1)) +assert(not m.match(1, "", 0)) + +print("+") + + +-- tests for argument captures +assert(not pcall(m.Carg, 0)) +assert(not pcall(m.Carg, -1)) +assert(not pcall(m.Carg, 2^18)) +assert(not pcall(m.match, m.Carg(1), 'a', 1)) +assert(m.match(m.Carg(1), 'a', 1, print) == print) +x = {m.match(m.Carg(1) * m.Carg(2), '', 1, 10, 20)} +checkeq(x, {10, 20}) + +assert(m.match(m.Cmt(m.Cg(m.Carg(3), "a") * + m.Cmt(m.Cb("a"), function (s,i,x) + assert(s == "a" and i == 1); + return i, x+1 + end) * + m.Carg(2), function (s,i,a,b,c) + assert(s == "a" and i == 1 and c == nil); + return i, 2*a + 3*b + end) * "a", + "a", 1, false, 100, 1000) == 2*1001 + 3*100) + + +-- tests for Lua functions + +t = {} +s = "" +p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; return nil end) * false +s = "hi, this is a test" +assert(m.match(((p - m.P(-1)) + 2)^0, s) == string.len(s) + 1) +assert(#t == string.len(s)/2 and t[1] == 1 and t[2] == 3) + +assert(not m.match(p, s)) + +p = mt.__add(function (s, i) return i end, function (s, i) return nil end) +assert(m.match(p, "alo")) + +p = mt.__mul(function (s, i) return i end, function (s, i) return nil end) +assert(not m.match(p, "alo")) + + +t = {} +p = function (s1, i) assert(s == s1); t[#t + 1] = i; return i end +s = "hi, this is a test" +assert(m.match((m.P(1) * p)^0, s) == string.len(s) + 1) +assert(#t == string.len(s) and t[1] == 2 and t[2] == 3) + +t = {} +p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; + return i <= s1:len() and i end) * 1 +s = "hi, this is a test" +assert(m.match(p^0, s) == string.len(s) + 1) +assert(#t == string.len(s) + 1 and t[1] == 1 and t[2] == 2) + +p = function (s1, i) return m.match(m.P"a"^1, s1, i) end +assert(m.match(p, "aaaa") == 5) +assert(m.match(p, "abaa") == 2) +assert(not m.match(p, "baaa")) + +assert(not pcall(m.match, function () return 2^20 end, s)) +assert(not pcall(m.match, function () return 0 end, s)) +assert(not pcall(m.match, function (s, i) return i - 1 end, s)) +assert(not pcall(m.match, m.P(1)^0 * function (_, i) return i - 1 end, s)) +assert(m.match(m.P(1)^0 * function (_, i) return i end * -1, s)) +assert(not pcall(m.match, m.P(1)^0 * function (_, i) return i + 1 end, s)) +assert(m.match(m.P(function (s, i) return s:len() + 1 end) * -1, s)) +assert(not pcall(m.match, m.P(function (s, i) return s:len() + 2 end) * -1, s)) +assert(not m.match(m.P(function (s, i) return s:len() end) * -1, s)) +assert(m.match(m.P(1)^0 * function (_, i) return true end, s) == + string.len(s) + 1) +for i = 1, string.len(s) + 1 do + assert(m.match(function (_, _) return i end, s) == i) +end + +p = (m.P(function (s, i) return i%2 == 0 and i end) * 1 + + m.P(function (s, i) return i%2 ~= 0 and i + 2 <= s:len() and i end) * 3)^0 + * -1 +assert(p:match(string.rep('a', 14000))) + +-- tests for Function Replacements +f = function (a, ...) if a ~= "x" then return {a, ...} end end + +t = m.match(m.C(1)^0/f, "abc") +checkeq(t, {"a", "b", "c"}) + +t = m.match(m.C(1)^0/f/f, "abc") +checkeq(t, {{"a", "b", "c"}}) + +t = m.match(m.P(1)^0/f/f, "abc") -- no capture +checkeq(t, {{"abc"}}) + +t = m.match((m.P(1)^0/f * m.Cp())/f, "abc") +checkeq(t, {{"abc"}, 4}) + +t = m.match((m.C(1)^0/f * m.Cp())/f, "abc") +checkeq(t, {{"a", "b", "c"}, 4}) + +t = m.match((m.C(1)^0/f * m.Cp())/f, "xbc") +checkeq(t, {4}) + +t = m.match(m.C(m.C(1)^0)/f, "abc") +checkeq(t, {"abc", "a", "b", "c"}) + +g = function (...) return 1, ... end +t = {m.match(m.C(1)^0/g/g, "abc")} +checkeq(t, {1, 1, "a", "b", "c"}) + +t = {m.match(m.Cc(nil,nil,4) * m.Cc(nil,3) * m.Cc(nil, nil) / g / g, "")} +t1 = {1,1,nil,nil,4,nil,3,nil,nil} +for i=1,10 do assert(t[i] == t1[i]) end + +t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} +checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) + +t = m.match(m.Ct((m.C(1) / function (x,y) return y, x end * m.Cc(1))^0), "abc") +checkeq(t, {nil, "a", 1, nil, "b", 1, nil, "c", 1}) + +-- tests for Query Replacements + +assert(m.match(m.C(m.C(1)^0)/{abc = 10}, "abc") == 10) +assert(m.match(m.C(1)^0/{a = 10}, "abc") == 10) +assert(m.match(m.S("ba")^0/{ab = 40}, "abc") == 40) +t = m.match(m.Ct((m.S("ba")/{a = 40})^0), "abc") +checkeq(t, {40}) + +assert(m.match(m.Cs((m.C(1)/{a=".", d=".."})^0), "abcdde") == ".bc....e") +assert(m.match(m.Cs((m.C(1)/{f="."})^0), "abcdde") == "abcdde") +assert(m.match(m.Cs((m.C(1)/{d="."})^0), "abcdde") == "abc..e") +assert(m.match(m.Cs((m.C(1)/{e="."})^0), "abcdde") == "abcdd.") +assert(m.match(m.Cs((m.C(1)/{e=".", f="+"})^0), "eefef") == "..+.+") +assert(m.match(m.Cs((m.C(1))^0), "abcdde") == "abcdde") +assert(m.match(m.Cs(m.C(m.C(1)^0)), "abcdde") == "abcdde") +assert(m.match(1 * m.Cs(m.P(1)^0), "abcdde") == "bcdde") +assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "abcdde") == "abcdde") +assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "0ab0b0") == "xabxbx") +assert(m.match(m.Cs((m.C('0')/'x' + m.P(1)/{b=3})^0), "b0a0b") == "3xax3") +assert(m.match(m.P(1)/'%0%0'/{aa = -3} * 'x', 'ax') == -3) +assert(m.match(m.C(1)/'%0%1'/{aa = 'z'}/{z = -3} * 'x', 'ax') == -3) + +assert(m.match(m.Cs(m.Cc(0) * (m.P(1)/"")), "4321") == "0") + +assert(m.match(m.Cs((m.P(1) / "%0")^0), "abcd") == "abcd") +assert(m.match(m.Cs((m.P(1) / "%0.%0")^0), "abcd") == "a.ab.bc.cd.d") +assert(m.match(m.Cs((m.P("a") / "%0.%0" + 1)^0), "abcad") == "a.abca.ad") +assert(m.match(m.C("a") / "%1%%%0", "a") == "a%a") +assert(m.match(m.Cs((m.P(1) / ".xx")^0), "abcd") == ".xx.xx.xx.xx") +assert(m.match(m.Cp() * m.P(3) * m.Cp()/"%2%1%1 - %0 ", "abcde") == + "411 - abc ") + +assert(pcall(m.match, m.P(1)/"%0", "abc")) +assert(not pcall(m.match, m.P(1)/"%1", "abc")) -- out of range +assert(not pcall(m.match, m.P(1)/"%9", "abc")) -- out of range + +p = m.C(1) +p = p * p; p = p * p; p = p * p * m.C(1) / "%9 - %1" +assert(p:match("1234567890") == "9 - 1") + +assert(m.match(m.Cc(print), "") == print) + +-- too many captures (just ignore extra ones) +p = m.C(1)^0 / "%2-%9-%0-%9" +assert(p:match"01234567890123456789" == "1-8-01234567890123456789-8") +s = string.rep("12345678901234567890", 20) +assert(m.match(m.C(1)^0 / "%9-%1-%0-%3", s) == "9-1-" .. s .. "-3") + +-- string captures with non-string subcaptures +p = m.Cc('alo') * m.C(1) / "%1 - %2 - %1" +assert(p:match'x' == 'alo - x - alo') + +assert(not pcall(m.match, m.Cc(true) / "%1", "a")) + +-- long strings for string capture +l = 10000 +s = string.rep('a', l) .. string.rep('b', l) .. string.rep('c', l) + +p = (m.C(m.P'a'^1) * m.C(m.P'b'^1) * m.C(m.P'c'^1)) / '%3%2%1' + +assert(p:match(s) == string.rep('c', l) .. + string.rep('b', l) .. + string.rep('a', l)) + +print"+" + +-- accumulator capture +function f (x) return x + 1 end +assert(m.match(m.Cf(m.Cc(0) * m.C(1)^0, f), "alo alo") == 7) + +t = {m.match(m.Cf(m.Cc(1,2,3), error), "")} +checkeq(t, {1}) +p = m.Cf(m.Ct(true) * m.Cg(m.C(m.R"az"^1) * "=" * m.C(m.R"az"^1) * ";")^0, + rawset) +t = p:match("a=b;c=du;xux=yuy;") +checkeq(t, {a="b", c="du", xux="yuy"}) + + +-- errors in accumulator capture + +-- very long match (forces fold to be a pair open-close) producing with +-- no initial capture +assert(not pcall(m.match, m.Cf(m.P(500), print), string.rep('a', 600))) + +-- nested capture produces no initial value +assert(not pcall(m.match, m.Cf(m.P(1) / {}, print), "alo")) + + +-- tests for loop checker + +local function haveloop (p) + assert(not pcall(function (p) return p^0 end, m.P(p))) +end + +haveloop(m.P("x")^-4) +assert(m.match(((m.P(0) + 1) * m.S"al")^0, "alo") == 3) +assert(m.match((("x" + #m.P(1))^-4 * m.S"al")^0, "alo") == 3) +haveloop("") +haveloop(m.P("x")^0) +haveloop(m.P("x")^-1) +haveloop(m.P("x") + 1 + 2 + m.P("a")^-1) +haveloop(-m.P("ab")) +haveloop(- -m.P("ab")) +haveloop(# #(m.P("ab") + "xy")) +haveloop(- #m.P("ab")^0) +haveloop(# -m.P("ab")^1) +haveloop(#m.V(3)) +haveloop(m.V(3) + m.V(1) + m.P('a')^-1) +haveloop({[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(0)}) +assert(m.match(m.P{[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(1)}^0, "abc") + == 3) +assert(m.match(m.P""^-3, "a") == 1) + +local function find (p, s) + return m.match(basiclookfor(p), s) +end + + +local function badgrammar (g, expected) + local stat, msg = pcall(m.P, g) + assert(not stat) + if expected then assert(find(expected, msg)) end +end + +badgrammar({[1] = m.V(1)}, "rule '1'") +badgrammar({[1] = m.V(2)}, "rule '2'") -- invalid non-terminal +badgrammar({[1] = m.V"x"}, "rule 'x'") -- invalid non-terminal +badgrammar({[1] = m.V{}}, "rule '(a table)'") -- invalid non-terminal +badgrammar({[1] = #m.P("a") * m.V(1)}, "rule '1'") -- left-recursive +badgrammar({[1] = -m.P("a") * m.V(1)}, "rule '1'") -- left-recursive +badgrammar({[1] = -1 * m.V(1)}, "rule '1'") -- left-recursive +badgrammar({[1] = -1 + m.V(1)}, "rule '1'") -- left-recursive +badgrammar({[1] = 1 * m.V(2), [2] = m.V(2)}, "rule '2'") -- left-recursive +badgrammar({[1] = 1 * m.V(2)^0, [2] = m.P(0)}, "rule '1'") -- inf. loop +badgrammar({ m.V(2), m.V(3)^0, m.P"" }, "rule '2'") -- inf. loop +badgrammar({ m.V(2) * m.V(3)^0, m.V(3)^0, m.P"" }, "rule '1'") -- inf. loop +badgrammar({"x", x = #(m.V(1) * 'a') }, "rule '1'") -- inf. loop +badgrammar({ -(m.V(1) * 'a') }, "rule '1'") -- inf. loop +badgrammar({"x", x = m.P'a'^-1 * m.V"x"}, "rule 'x'") -- left recursive +badgrammar({"x", x = m.P'a' * m.V"y"^1, y = #m.P(1)}, "rule 'x'") + +assert(m.match({'a' * -m.V(1)}, "aaa") == 2) +assert(m.match({'a' * -m.V(1)}, "aaaa") == nil) + + +-- good x bad grammars +m.P{ ('a' * m.V(1))^-1 } +m.P{ -('a' * m.V(1)) } +m.P{ ('abc' * m.V(1))^-1 } +m.P{ -('abc' * m.V(1)) } +badgrammar{ #m.P('abc') * m.V(1) } +badgrammar{ -('a' + m.V(1)) } +m.P{ #('a' * m.V(1)) } +badgrammar{ #('a' + m.V(1)) } +m.P{ m.B{ m.P'abc' } * 'a' * m.V(1) } +badgrammar{ m.B{ m.P'abc' } * m.V(1) } +badgrammar{ ('a' + m.P'bcd')^-1 * m.V(1) } + + +-- simple tests for maximum sizes: +local p = m.P"a" +for i=1,14 do p = p * p end + +p = {} +for i=1,100 do p[i] = m.P"a" end +p = m.P(p) + + +-- strange values for rule labels + +p = m.P{ "print", + print = m.V(print), + [print] = m.V(_G), + [_G] = m.P"a", + } + +assert(p:match("a")) + +-- initial rule +g = {} +for i = 1, 10 do g["i"..i] = "a" * m.V("i"..i+1) end +g.i11 = m.P"" +for i = 1, 10 do + g[1] = "i"..i + local p = m.P(g) + assert(p:match("aaaaaaaaaaa") == 11 - i + 1) +end + +print"+" + + +-- tests for back references +assert(not pcall(m.match, m.Cb('x'), '')) +assert(not pcall(m.match, m.Cg(1, 'a') * m.Cb('b'), 'a')) + +p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) +t = p:match("ab") +checkeq(t, {"a", "b"}) + + +t = {} +function foo (p) t[#t + 1] = p; return p .. "x" end + +p = m.Cg(m.C(2) / foo, "x") * m.Cb"x" * + m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * + m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * + m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" +x = {p:match'ab'} +checkeq(x, {'abx', 'abxx', 'abxxx', 'abxxxx'}) +checkeq(t, {'ab', + 'ab', 'abx', + 'ab', 'abx', 'abxx', + 'ab', 'abx', 'abxx', 'abxxx'}) + + + +-- tests for match-time captures + +p = m.P'a' * (function (s, i) return (s:sub(i, i) == 'b') and i + 1 end) + + 'acd' + +assert(p:match('abc') == 3) +assert(p:match('acd') == 4) + +local function id (s, i, ...) + return true, ... +end + +assert(m.Cmt(m.Cs((m.Cmt(m.S'abc' / { a = 'x', c = 'y' }, id) + + m.R'09'^1 / string.char + + m.P(1))^0), id):match"acb98+68c" == "xyb\98+\68y") + +p = m.P{'S', + S = m.V'atom' * space + + m.Cmt(m.Ct("(" * space * (m.Cmt(m.V'S'^1, id) + m.P(true)) * ")" * space), id), + atom = m.Cmt(m.C(m.R("AZ", "az", "09")^1), id) +} +x = p:match"(a g () ((b) c) (d (e)))" +checkeq(x, {'a', 'g', {}, {{'b'}, 'c'}, {'d', {'e'}}}); + +x = {(m.Cmt(1, id)^0):match(string.rep('a', 500))} +assert(#x == 500) + +local function id(s, i, x) + if x == 'a' then return i, 1, 3, 7 + else return nil, 2, 4, 6, 8 + end +end + +p = ((m.P(id) * 1 + m.Cmt(2, id) * 1 + m.Cmt(1, id) * 1))^0 +assert(table.concat{p:match('abababab')} == string.rep('137', 4)) + +local function ref (s, i, x) + return m.match(x, s, i - x:len()) +end + +assert(m.Cmt(m.P(1)^0, ref):match('alo') == 4) +assert((m.P(1) * m.Cmt(m.P(1)^0, ref)):match('alo') == 4) +assert(not (m.P(1) * m.Cmt(m.C(1)^0, ref)):match('alo')) + +ref = function (s,i,x) return i == tonumber(x) and i, 'xuxu' end + +assert(m.Cmt(1, ref):match'2') +assert(not m.Cmt(1, ref):match'1') +assert(m.Cmt(m.P(1)^0, ref):match'03') + +function ref (s, i, a, b) + if a == b then return i, a:upper() end +end + +p = m.Cmt(m.C(m.R"az"^1) * "-" * m.C(m.R"az"^1), ref) +p = (any - p)^0 * p * any^0 * -1 + +assert(p:match'abbbc-bc ddaa' == 'BC') + +do -- match-time captures cannot be optimized away + local touch = 0 + f = m.P(function () touch = touch + 1; return true end) + + local function check(n) n = n or 1; assert(touch == n); touch = 0 end + + assert(m.match(f * false + 'b', 'a') == nil); check() + assert(m.match(f * false + 'b', '') == nil); check() + assert(m.match( (f * 'a')^0 * 'b', 'b') == 2); check() + assert(m.match( (f * 'a')^0 * 'b', '') == nil); check() + assert(m.match( (f * 'a')^-1 * 'b', 'b') == 2); check() + assert(m.match( (f * 'a')^-1 * 'b', '') == nil); check() + assert(m.match( ('b' + f * 'a')^-1 * 'b', '') == nil); check() + assert(m.match( (m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); check() + assert(m.match( (-m.P(1) * m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); + check() + assert(m.match( (f * 'a' + 'b')^-1 * 'b', '') == nil); check() + assert(m.match(f * 'a' + f * 'b', 'b') == 2); check(2) + assert(m.match(f * 'a' + f * 'b', 'a') == 2); check(1) + assert(m.match(-f * 'a' + 'b', 'b') == 2); check(1) + assert(m.match(-f * 'a' + 'b', '') == nil); check(1) +end + +c = '[' * m.Cg(m.P'='^0, "init") * '[' * + { m.Cmt(']' * m.C(m.P'='^0) * ']' * m.Cb("init"), function (_, _, s1, s2) + return s1 == s2 end) + + 1 * m.V(1) } / 0 + +assert(c:match'[==[]]====]]]]==]===[]' == 18) +assert(c:match'[[]=]====]=]]]==]===[]' == 14) +assert(not c:match'[[]=]====]=]=]==]===[]') + + +-- old bug: optimization of concat with fail removed match-time capture +p = m.Cmt(0, function (s) p = s end) * m.P(false) +assert(not p:match('alo')) +assert(p == 'alo') + + +-- ensure that failed match-time captures are not kept on Lua stack +do + local t = {__mode = "kv"}; setmetatable(t,t) + local c = 0 + + local function foo (s,i) + collectgarbage(); + assert(next(t) == "__mode" and next(t, "__mode") == nil) + local x = {} + t[x] = true + c = c + 1 + return i, x + end + + local p = m.P{ m.Cmt(0, foo) * m.P(false) + m.P(1) * m.V(1) + m.P"" } + p:match(string.rep('1', 10)) + assert(c == 11) +end + +p = (m.P(function () return true, "a" end) * 'a' + + m.P(function (s, i) return i, "aa", 20 end) * 'b' + + m.P(function (s,i) if i <= #s then return i, "aaa" end end) * 1)^0 + +t = {p:match('abacc')} +checkeq(t, {'a', 'aa', 20, 'a', 'aaa', 'aaa'}) + + +------------------------------------------------------------------- +-- Tests for 're' module +------------------------------------------------------------------- + +local re = require "re" + +local match, compile = re.match, re.compile + + + +assert(match("a", ".") == 2) +assert(match("a", "''") == 1) +assert(match("", " ! . ") == 1) +assert(not match("a", " ! . ")) +assert(match("abcde", " ( . . ) * ") == 5) +assert(match("abbcde", " [a-c] +") == 5) +assert(match("0abbc1de", "'0' [a-c]+ '1'") == 7) +assert(match("0zz1dda", "'0' [^a-c]+ 'a'") == 8) +assert(match("abbc--", " [a-c] + +") == 5) +assert(match("abbc--", " [ac-] +") == 2) +assert(match("abbc--", " [-acb] + ") == 7) +assert(not match("abbcde", " [b-z] + ")) +assert(match("abb\"de", '"abb"["]"de"') == 7) +assert(match("abceeef", "'ac' ? 'ab' * 'c' { 'e' * } / 'abceeef' ") == "eee") +assert(match("abceeef", "'ac'? 'ab'* 'c' { 'f'+ } / 'abceeef' ") == 8) +local t = {match("abceefe", "( ( & 'e' {} ) ? . ) * ")} +checkeq(t, {4, 5, 7}) +local t = {match("abceefe", "((&&'e' {})? .)*")} +checkeq(t, {4, 5, 7}) +local t = {match("abceefe", "( ( ! ! 'e' {} ) ? . ) *")} +checkeq(t, {4, 5, 7}) +local t = {match("abceefe", "(( & ! & ! 'e' {})? .)*")} +checkeq(t, {4, 5, 7}) + +assert(match("cccx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 5) +assert(match("cdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 4) +assert(match("abcdcdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 8) + +assert(match("abc", "a <- (. a)?") == 4) +b = "balanced <- '(' ([^()] / balanced)* ')'" +assert(match("(abc)", b)) +assert(match("(a(b)((c) (d)))", b)) +assert(not match("(a(b ((c) (d)))", b)) + +b = compile[[ balanced <- "(" ([^()] / balanced)* ")" ]] +assert(b == m.P(b)) +assert(b:match"((((a))(b)))") + +local g = [[ + S <- "0" B / "1" A / "" -- balanced strings + A <- "0" S / "1" A A -- one more 0 + B <- "1" S / "0" B B -- one more 1 +]] +assert(match("00011011", g) == 9) + +local g = [[ + S <- ("0" B / "1" A)* + A <- "0" / "1" A A + B <- "1" / "0" B B +]] +assert(match("00011011", g) == 9) +assert(match("000110110", g) == 9) +assert(match("011110110", g) == 3) +assert(match("000110010", g) == 1) + +s = "aaaaaaaaaaaaaaaaaaaaaaaa" +assert(match(s, "'a'^3") == 4) +assert(match(s, "'a'^0") == 1) +assert(match(s, "'a'^+3") == s:len() + 1) +assert(not match(s, "'a'^+30")) +assert(match(s, "'a'^-30") == s:len() + 1) +assert(match(s, "'a'^-5") == 6) +for i = 1, s:len() do + assert(match(s, string.format("'a'^+%d", i)) >= i + 1) + assert(match(s, string.format("'a'^-%d", i)) <= i + 1) + assert(match(s, string.format("'a'^%d", i)) == i + 1) +end +assert(match("01234567890123456789", "[0-9]^3+") == 19) + + +assert(match("01234567890123456789", "({....}{...}) -> '%2%1'") == "4560123") +t = match("0123456789", "{| {.}* |}") +checkeq(t, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}) +assert(match("012345", "{| (..) -> '%0%0' |}")[1] == "0101") + +assert(match("abcdef", "( {.} {.} {.} {.} {.} ) -> 3") == "c") +assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 3") == "d") +assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 0") == 6) + +assert(not match("abcdef", "{:x: ({.} {.} {.}) -> 2 :} =x")) +assert(match("abcbef", "{:x: ({.} {.} {.}) -> 2 :} =x")) + +eqcharset(compile"[]]", "]") +eqcharset(compile"[][]", m.S"[]") +eqcharset(compile"[]-]", m.S"-]") +eqcharset(compile"[-]", m.S"-") +eqcharset(compile"[az-]", m.S"a-z") +eqcharset(compile"[-az]", m.S"a-z") +eqcharset(compile"[a-z]", m.R"az") +eqcharset(compile"[]['\"]", m.S[[]['"]]) + +eqcharset(compile"[^]]", any - "]") +eqcharset(compile"[^][]", any - m.S"[]") +eqcharset(compile"[^]-]", any - m.S"-]") +eqcharset(compile"[^]-]", any - m.S"-]") +eqcharset(compile"[^-]", any - m.S"-") +eqcharset(compile"[^az-]", any - m.S"a-z") +eqcharset(compile"[^-az]", any - m.S"a-z") +eqcharset(compile"[^a-z]", any - m.R"az") +eqcharset(compile"[^]['\"]", any - m.S[[]['"]]) + +-- tests for comments in 're' +e = compile[[ +A <- _B -- \t \n %nl .<> <- -> -- +_B <- 'x' --]] +assert(e:match'xy' == 2) + +-- tests for 're' with pre-definitions +defs = {digits = m.R"09", letters = m.R"az", _=m.P"__"} +e = compile("%letters (%letters / %digits)*", defs) +assert(e:match"x123" == 5) +e = compile("%_", defs) +assert(e:match"__" == 3) + +e = compile([[ + S <- A+ + A <- %letters+ B + B <- %digits+ +]], defs) + +e = compile("{[0-9]+'.'?[0-9]*} -> sin", math) +assert(e:match("2.34") == math.sin(2.34)) + + +function eq (_, _, a, b) return a == b end + +c = re.compile([[ + longstring <- '[' {:init: '='* :} '[' close + close <- ']' =init ']' / . close +]]) + +assert(c:match'[==[]]===]]]]==]===[]' == 17) +assert(c:match'[[]=]====]=]]]==]===[]' == 14) +assert(not c:match'[[]=]====]=]=]==]===[]') + +c = re.compile" '[' {:init: '='* :} '[' (!(']' =init ']') .)* ']' =init ']' !. " + +assert(c:match'[==[]]===]]]]==]') +assert(c:match'[[]=]====]=][]==]===[]]') +assert(not c:match'[[]=]====]=]=]==]===[]') + +assert(re.find("hi alalo", "{:x:..:} =x") == 4) +assert(re.find("hi alalo", "{:x:..:} =x", 4) == 4) +assert(not re.find("hi alalo", "{:x:..:} =x", 5)) +assert(re.find("hi alalo", "{'al'}", 5) == 6) +assert(re.find("hi aloalolo", "{:x:..:} =x") == 8) +assert(re.find("alo alohi x x", "{:word:%w+:}%W*(=word)!%w") == 11) + +-- re.find discards any captures +local a,b,c = re.find("alo", "{.}{'o'}") +assert(a == 2 and b == 3 and c == nil) + +local function match (s,p) + local i,e = re.find(s,p) + if i then return s:sub(i, e) end +end +assert(match("alo alo", '[a-z]+') == "alo") +assert(match("alo alo", '{:x: [a-z]+ :} =x') == nil) +assert(match("alo alo", "{:x: [a-z]+ :} ' ' =x") == "alo alo") + +assert(re.gsub("alo alo", "[abc]", "x") == "xlo xlo") +assert(re.gsub("alo alo", "%w+", ".") == ". .") +assert(re.gsub("hi, how are you", "[aeiou]", string.upper) == + "hI, hOw ArE yOU") + +s = 'hi [[a comment[=]=] ending here]] and [=[another]]=]]' +c = re.compile" '[' {:i: '='* :} '[' (!(']' =i ']') .)* ']' { =i } ']' " +assert(re.gsub(s, c, "%2") == 'hi and =]') +assert(re.gsub(s, c, "%0") == s) +assert(re.gsub('[=[hi]=]', c, "%2") == '=') + +assert(re.find("", "!.") == 1) +assert(re.find("alo", "!.") == 4) + +function addtag (s, i, t, tag) t.tag = tag; return i, t end + +c = re.compile([[ + doc <- block !. + block <- (start {| (block / { [^<]+ })* |} end?) => addtag + start <- '<' {:tag: [a-z]+ :} '>' + end <- '' +]], {addtag = addtag}) + +x = c:match[[ +hihellobuttotheend]] +checkeq(x, {tag='x', 'hi', {tag = 'b', 'hello'}, 'but', + {'totheend'}}) + + +-- tests for look-ahead captures +x = {re.match("alo", "&(&{.}) !{'b'} {&(...)} &{..} {...} {!.}")} +checkeq(x, {"", "alo", ""}) + +assert(re.match("aloalo", + "{~ (((&'al' {.}) -> 'A%1' / (&%l {.}) -> '%1%1') / .)* ~}") + == "AallooAalloo") + +-- bug in 0.9 (and older versions), due to captures in look-aheads +x = re.compile[[ {~ (&(. ([a-z]* -> '*')) ([a-z]+ -> '+') ' '*)* ~} ]] +assert(x:match"alo alo" == "+ +") + +-- valid capture in look-ahead (used inside the look-ahead itself) +x = re.compile[[ + S <- &({:two: .. :} . =two) {[a-z]+} / . S +]] +assert(x:match("hello aloaLo aloalo xuxu") == "aloalo") + + +p = re.compile[[ + block <- {| {:ident:space*:} line + ((=ident !space line) / &(=ident space) block)* |} + line <- {[^%nl]*} %nl + space <- '_' -- should be ' ', but '_' is simpler for editors +]] + +t= p:match[[ +1 +__1.1 +__1.2 +____1.2.1 +____ +2 +__2.1 +]] +checkeq(t, {"1", {"1.1", "1.2", {"1.2.1", "", ident = "____"}, ident = "__"}, + "2", {"2.1", ident = "__"}, ident = ""}) + + +-- nested grammars +p = re.compile[[ + s <- a b !. + b <- ( x <- ('b' x)? ) + a <- ( x <- 'a' x? ) +]] + +assert(p:match'aaabbb') +assert(p:match'aaa') +assert(not p:match'bbb') +assert(not p:match'aaabbba') + +-- testing groups +t = {re.match("abc", "{:S <- {:.:} {S} / '':}")} +checkeq(t, {"a", "bc", "b", "c", "c", ""}) + +t = re.match("1234", "{| {:a:.:} {:b:.:} {:c:.{.}:} |}") +checkeq(t, {a="1", b="2", c="4"}) +t = re.match("1234", "{|{:a:.:} {:b:{.}{.}:} {:c:{.}:}|}") +checkeq(t, {a="1", b="2", c="4"}) +t = re.match("12345", "{| {:.:} {:b:{.}{.}:} {:{.}{.}:} |}") +checkeq(t, {"1", b="2", "4", "5"}) +t = re.match("12345", "{| {:.:} {:{:b:{.}{.}:}:} {:{.}{.}:} |}") +checkeq(t, {"1", "23", "4", "5"}) +t = re.match("12345", "{| {:.:} {{:b:{.}{.}:}} {:{.}{.}:} |}") +checkeq(t, {"1", "23", "4", "5"}) + + +-- testing pre-defined names +assert(os.setlocale("C") == "C") + +function eqlpeggsub (p1, p2) + local s1 = cs2str(re.compile(p1)) + local s2 = string.gsub(allchar, "[^" .. p2 .. "]", "") + -- if s1 ~= s2 then print(#s1,#s2) end + assert(s1 == s2) +end + + +eqlpeggsub("%w", "%w") +eqlpeggsub("%a", "%a") +eqlpeggsub("%l", "%l") +eqlpeggsub("%u", "%u") +eqlpeggsub("%p", "%p") +eqlpeggsub("%d", "%d") +eqlpeggsub("%x", "%x") +eqlpeggsub("%s", "%s") +eqlpeggsub("%c", "%c") + +eqlpeggsub("%W", "%W") +eqlpeggsub("%A", "%A") +eqlpeggsub("%L", "%L") +eqlpeggsub("%U", "%U") +eqlpeggsub("%P", "%P") +eqlpeggsub("%D", "%D") +eqlpeggsub("%X", "%X") +eqlpeggsub("%S", "%S") +eqlpeggsub("%C", "%C") + +eqlpeggsub("[%w]", "%w") +eqlpeggsub("[_%w]", "_%w") +eqlpeggsub("[^%w]", "%W") +eqlpeggsub("[%W%S]", "%W%S") + +re.updatelocale() + + +-- testing nested substitutions x string captures + +p = re.compile[[ + text <- {~ item* ~} + item <- macro / [^()] / '(' item* ')' + arg <- ' '* {~ (!',' item)* ~} + args <- '(' arg (',' arg)* ')' + macro <- ('apply' args) -> '%1(%2)' + / ('add' args) -> '%1 + %2' + / ('mul' args) -> '%1 * %2' +]] + +assert(p:match"add(mul(a,b), apply(f,x))" == "a * b + f(x)") + +rev = re.compile[[ R <- (!.) -> '' / ({.} R) -> '%2%1']] + +assert(rev:match"0123456789" == "9876543210") + + +-- testing error messages in re + +local function errmsg (p, err) + local s, msg = pcall(re.compile, p) + assert(not s and string.find(msg, err)) +end + +errmsg('aaaa', "rule 'aaaa'") +errmsg('a', 'outside') +errmsg('b <- a', 'undefined') +errmsg("x <- 'a' x <- 'b'", 'already defined') +errmsg("'a' -", "near '-'") + + +print"OK" + + diff --git a/3rd/lua-cjson/CMakeLists.txt b/3rd/lua-cjson/CMakeLists.txt deleted file mode 100644 index c17239b2..00000000 --- a/3rd/lua-cjson/CMakeLists.txt +++ /dev/null @@ -1,76 +0,0 @@ -# If Lua is installed in a non-standard location, please set the LUA_DIR -# environment variable to point to prefix for the install. Eg: -# Unix: export LUA_DIR=/home/user/pkg -# Windows: set LUA_DIR=c:\lua51 - -project(lua-cjson C) -cmake_minimum_required(VERSION 2.6) - -option(USE_INTERNAL_FPCONV "Use internal strtod() / g_fmt() code for performance") -option(MULTIPLE_THREADS "Support multi-threaded apps with internal fpconv - recommended" ON) - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING - "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." - FORCE) -endif() - -find_package(Lua51 REQUIRED) -include_directories(${LUA_INCLUDE_DIR}) - -if(NOT USE_INTERNAL_FPCONV) - # Use libc number conversion routines (strtod(), sprintf()) - set(FPCONV_SOURCES fpconv.c) -else() - # Use internal number conversion routines - add_definitions(-DUSE_INTERNAL_FPCONV) - set(FPCONV_SOURCES g_fmt.c dtoa.c) - - include(TestBigEndian) - TEST_BIG_ENDIAN(IEEE_BIG_ENDIAN) - if(IEEE_BIG_ENDIAN) - add_definitions(-DIEEE_BIG_ENDIAN) - endif() - - if(MULTIPLE_THREADS) - set(CMAKE_THREAD_PREFER_PTHREAD TRUE) - find_package(Threads REQUIRED) - if(NOT CMAKE_USE_PTHREADS_INIT) - message(FATAL_ERROR - "Pthreads not found - required by MULTIPLE_THREADS option") - endif() - add_definitions(-DMULTIPLE_THREADS) - endif() -endif() - -# Handle platforms missing isinf() macro (Eg, some Solaris systems). -include(CheckSymbolExists) -CHECK_SYMBOL_EXISTS(isinf math.h HAVE_ISINF) -if(NOT HAVE_ISINF) - add_definitions(-DUSE_INTERNAL_ISINF) -endif() - -set(_MODULE_LINK "${CMAKE_THREAD_LIBS_INIT}") -get_filename_component(_lua_lib_dir ${LUA_LIBRARY} PATH) - -if(APPLE) - set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS - "${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -undefined dynamic_lookup") -endif() - -if(WIN32) - # Win32 modules need to be linked to the Lua library. - set(_MODULE_LINK ${LUA_LIBRARY} ${_MODULE_LINK}) - set(_lua_module_dir "${_lua_lib_dir}") - # Windows sprintf()/strtod() handle NaN/inf differently. Not supported. - add_definitions(-DDISABLE_INVALID_NUMBERS) -else() - set(_lua_module_dir "${_lua_lib_dir}/lua/5.1") -endif() - -add_library(cjson MODULE lua_cjson.c strbuf.c ${FPCONV_SOURCES}) -set_target_properties(cjson PROPERTIES PREFIX "") -target_link_libraries(cjson ${_MODULE_LINK}) -install(TARGETS cjson DESTINATION "${_lua_module_dir}") - -# vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/LICENSE b/3rd/lua-cjson/LICENSE deleted file mode 100644 index 747a8bff..00000000 --- a/3rd/lua-cjson/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2010-2012 Mark Pulford - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/3rd/lua-cjson/Makefile b/3rd/lua-cjson/Makefile deleted file mode 100644 index 377952ff..00000000 --- a/3rd/lua-cjson/Makefile +++ /dev/null @@ -1,119 +0,0 @@ -##### Available defines for CJSON_CFLAGS ##### -## -## USE_INTERNAL_ISINF: Workaround for Solaris platforms missing isinf(). -## DISABLE_INVALID_NUMBERS: Permanently disable invalid JSON numbers: -## NaN, Infinity, hex. -## -## Optional built-in number conversion uses the following defines: -## USE_INTERNAL_FPCONV: Use builtin strtod/dtoa for numeric conversions. -## IEEE_BIG_ENDIAN: Required on big endian architectures. -## MULTIPLE_THREADS: Must be set when Lua CJSON may be used in a -## multi-threaded application. Requries _pthreads_. - -##### Build defaults ##### -LUA_VERSION = 5.1 -TARGET = cjson.so -PREFIX = /usr/local -#CFLAGS = -g -Wall -pedantic -fno-inline -CFLAGS = -O3 -Wall -pedantic -DNDEBUG -CJSON_CFLAGS = -fpic -CJSON_LDFLAGS = -shared -LUA_INCLUDE_DIR = $(PREFIX)/include -LUA_CMODULE_DIR = $(PREFIX)/lib/lua/$(LUA_VERSION) -LUA_MODULE_DIR = $(PREFIX)/share/lua/$(LUA_VERSION) -LUA_BIN_DIR = $(PREFIX)/bin - -##### Platform overrides ##### -## -## Tweak one of the platform sections below to suit your situation. -## -## See http://lua-users.org/wiki/BuildingModules for further platform -## specific details. - -## Linux - -## FreeBSD -#LUA_INCLUDE_DIR = $(PREFIX)/include/lua51 - -## MacOSX (Macports) -#PREFIX = /opt/local -#CJSON_LDFLAGS = -bundle -undefined dynamic_lookup - -## Solaris -#CC = gcc -#CJSON_CFLAGS = -fpic -DUSE_INTERNAL_ISINF - -## Windows (MinGW) -#TARGET = cjson.dll -#PREFIX = /home/user/opt -#CJSON_CFLAGS = -DDISABLE_INVALID_NUMBERS -#CJSON_LDFLAGS = -shared -L$(PREFIX)/lib -llua51 -#LUA_BIN_SUFFIX = .lua - -##### Number conversion configuration ##### - -## Use Libc support for number conversion (default) -FPCONV_OBJS = fpconv.o - -## Use built in number conversion -#FPCONV_OBJS = g_fmt.o dtoa.o -#CJSON_CFLAGS += -DUSE_INTERNAL_FPCONV - -## Compile built in number conversion for big endian architectures -#CJSON_CFLAGS += -DIEEE_BIG_ENDIAN - -## Compile built in number conversion to support multi-threaded -## applications (recommended) -#CJSON_CFLAGS += -pthread -DMULTIPLE_THREADS -#CJSON_LDFLAGS += -pthread - -##### End customisable sections ##### - -TEST_FILES = README bench.lua genutf8.pl test.lua octets-escaped.dat \ - example1.json example2.json example3.json example4.json \ - example5.json numbers.json rfc-example1.json \ - rfc-example2.json types.json -DATAPERM = 644 -EXECPERM = 755 - -ASCIIDOC = asciidoc - -BUILD_CFLAGS = -I$(LUA_INCLUDE_DIR) $(CJSON_CFLAGS) -OBJS = lua_cjson.o strbuf.o $(FPCONV_OBJS) - -.PHONY: all clean install install-extra doc - -.SUFFIXES: .html .txt - -.c.o: - $(CC) -c $(CFLAGS) $(CPPFLAGS) $(BUILD_CFLAGS) -o $@ $< - -.txt.html: - $(ASCIIDOC) -n -a toc $< - -all: $(TARGET) - -doc: manual.html performance.html - -$(TARGET): $(OBJS) - $(CC) $(LDFLAGS) $(CJSON_LDFLAGS) -o $@ $(OBJS) - -install: $(TARGET) - mkdir -p $(DESTDIR)/$(LUA_CMODULE_DIR) - cp $(TARGET) $(DESTDIR)/$(LUA_CMODULE_DIR) - chmod $(EXECPERM) $(DESTDIR)/$(LUA_CMODULE_DIR)/$(TARGET) - -install-extra: - mkdir -p $(DESTDIR)/$(LUA_MODULE_DIR)/cjson/tests \ - $(DESTDIR)/$(LUA_BIN_DIR) - cp lua/cjson/util.lua $(DESTDIR)/$(LUA_MODULE_DIR)/cjson - chmod $(DATAPERM) $(DESTDIR)/$(LUA_MODULE_DIR)/cjson/util.lua - cp lua/lua2json.lua $(DESTDIR)/$(LUA_BIN_DIR)/lua2json$(LUA_BIN_SUFFIX) - chmod $(EXECPERM) $(DESTDIR)/$(LUA_BIN_DIR)/lua2json$(LUA_BIN_SUFFIX) - cp lua/json2lua.lua $(DESTDIR)/$(LUA_BIN_DIR)/json2lua$(LUA_BIN_SUFFIX) - chmod $(EXECPERM) $(DESTDIR)/$(LUA_BIN_DIR)/json2lua$(LUA_BIN_SUFFIX) - cd tests; cp $(TEST_FILES) $(DESTDIR)/$(LUA_MODULE_DIR)/cjson/tests - cd tests; chmod $(DATAPERM) $(TEST_FILES); chmod $(EXECPERM) *.lua *.pl - -clean: - rm -f *.o $(TARGET) diff --git a/3rd/lua-cjson/NEWS b/3rd/lua-cjson/NEWS deleted file mode 100644 index 8927d6e5..00000000 --- a/3rd/lua-cjson/NEWS +++ /dev/null @@ -1,44 +0,0 @@ -Version 2.1.0 (Mar 1 2012) -* Added cjson.safe module interface which returns nil after an error -* Improved Makefile compatibility with Solaris make - -Version 2.0.0 (Jan 22 2012) -* Improved platform compatibility for strtod/sprintf locale workaround -* Added option to build with David Gay's dtoa.c for improved performance -* Added support for Lua 5.2 -* Added option to encode infinity/NaN as JSON null -* Fixed encode bug with a raised default limit and deeply nested tables -* Updated Makefile for compatibility with non-GNU make implementations -* Added CMake build support -* Added HTML manual -* Increased default nesting limit to 1000 -* Added support for re-entrant use of encode and decode -* Added support for installing lua2json and json2lua utilities -* Added encode_invalid_numbers() and decode_invalid_numbers() -* Added decode_max_depth() -* Removed registration of global cjson module table -* Removed refuse_invalid_numbers() - -Version 1.0.4 (Nov 30 2011) -* Fixed numeric conversion under locales with a comma decimal separator - -Version 1.0.3 (Sep 15 2011) -* Fixed detection of objects with numeric string keys -* Provided work around for missing isinf() on Solaris - -Version 1.0.2 (May 30 2011) -* Portability improvements for Windows - - No longer links with -lm - - Use "socket" instead of "posix" for sub-second timing -* Removed UTF-8 test dependency on Perl Text::Iconv -* Added simple CLI commands for testing Lua <-> JSON conversions -* Added cjson.encode_number_precision() - -Version 1.0.1 (May 10 2011) -* Added build support for OSX -* Removed unnecessary whitespace from JSON output -* Added cjson.encode_keep_buffer() -* Fixed memory leak on Lua stack overflow exception - -Version 1.0 (May 9 2011) -* Initial release diff --git a/3rd/lua-cjson/THANKS b/3rd/lua-cjson/THANKS deleted file mode 100644 index 4aade136..00000000 --- a/3rd/lua-cjson/THANKS +++ /dev/null @@ -1,9 +0,0 @@ -The following people have helped with bug reports, testing and/or -suggestions: - -- Louis-Philippe Perron (@loopole) -- Ondřej Jirman -- Steve Donovan -- Zhang "agentzh" Yichun - -Thanks! diff --git a/3rd/lua-cjson/dtoa.c b/3rd/lua-cjson/dtoa.c deleted file mode 100644 index 520926cc..00000000 --- a/3rd/lua-cjson/dtoa.c +++ /dev/null @@ -1,4358 +0,0 @@ -/**************************************************************** - * - * The author of this software is David M. Gay. - * - * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY - * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - * - ***************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - -/* On a machine with IEEE extended-precision registers, it is - * necessary to specify double-precision (53-bit) rounding precision - * before invoking strtod or dtoa. If the machine uses (the equivalent - * of) Intel 80x87 arithmetic, the call - * _control87(PC_53, MCW_PC); - * does this with many compilers. Whether this or another call is - * appropriate depends on the compiler; for this to work, it may be - * necessary to #include "float.h" or another system-dependent header - * file. - */ - -/* strtod for IEEE-, VAX-, and IBM-arithmetic machines. - * - * This strtod returns a nearest machine number to the input decimal - * string (or sets errno to ERANGE). With IEEE arithmetic, ties are - * broken by the IEEE round-even rule. Otherwise ties are broken by - * biased rounding (add half and chop). - * - * Inspired loosely by William D. Clinger's paper "How to Read Floating - * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. - * - * Modifications: - * - * 1. We only require IEEE, IBM, or VAX double-precision - * arithmetic (not IEEE double-extended). - * 2. We get by with floating-point arithmetic in a case that - * Clinger missed -- when we're computing d * 10^n - * for a small integer d and the integer n is not too - * much larger than 22 (the maximum integer k for which - * we can represent 10^k exactly), we may be able to - * compute (d*10^k) * 10^(e-k) with just one roundoff. - * 3. Rather than a bit-at-a-time adjustment of the binary - * result in the hard case, we use floating-point - * arithmetic to determine the adjustment to within - * one bit; only in really hard cases do we need to - * compute a second residual. - * 4. Because of 3., we don't need a large table of powers of 10 - * for ten-to-e (just some small tables, e.g. of 10^k - * for 0 <= k <= 22). - */ - -/* - * #define IEEE_8087 for IEEE-arithmetic machines where the least - * significant byte has the lowest address. - * #define IEEE_MC68k for IEEE-arithmetic machines where the most - * significant byte has the lowest address. - * #define Long int on machines with 32-bit ints and 64-bit longs. - * #define IBM for IBM mainframe-style floating-point arithmetic. - * #define VAX for VAX-style floating-point arithmetic (D_floating). - * #define No_leftright to omit left-right logic in fast floating-point - * computation of dtoa. This will cause dtoa modes 4 and 5 to be - * treated the same as modes 2 and 3 for some inputs. - * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 - * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS - * is also #defined, fegetround() will be queried for the rounding mode. - * Note that both FLT_ROUNDS and fegetround() are specified by the C99 - * standard (and are specified to be consistent, with fesetround() - * affecting the value of FLT_ROUNDS), but that some (Linux) systems - * do not work correctly in this regard, so using fegetround() is more - * portable than using FLT_ROUNDS directly. - * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 - * and Honor_FLT_ROUNDS is not #defined. - * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines - * that use extended-precision instructions to compute rounded - * products and quotients) with IBM. - * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic - * that rounds toward +Infinity. - * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased - * rounding when the underlying floating-point arithmetic uses - * unbiased rounding. This prevent using ordinary floating-point - * arithmetic when the result could be computed with one rounding error. - * #define Inaccurate_Divide for IEEE-format with correctly rounded - * products but inaccurate quotients, e.g., for Intel i860. - * #define NO_LONG_LONG on machines that do not have a "long long" - * integer type (of >= 64 bits). On such machines, you can - * #define Just_16 to store 16 bits per 32-bit Long when doing - * high-precision integer arithmetic. Whether this speeds things - * up or slows things down depends on the machine and the number - * being converted. If long long is available and the name is - * something other than "long long", #define Llong to be the name, - * and if "unsigned Llong" does not work as an unsigned version of - * Llong, #define #ULLong to be the corresponding unsigned type. - * #define KR_headers for old-style C function headers. - * #define Bad_float_h if your system lacks a float.h or if it does not - * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, - * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. - * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) - * if memory is available and otherwise does something you deem - * appropriate. If MALLOC is undefined, malloc will be invoked - * directly -- and assumed always to succeed. Similarly, if you - * want something other than the system's free() to be called to - * recycle memory acquired from MALLOC, #define FREE to be the - * name of the alternate routine. (FREE or free is only called in - * pathological cases, e.g., in a dtoa call after a dtoa return in - * mode 3 with thousands of digits requested.) - * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making - * memory allocations from a private pool of memory when possible. - * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes, - * unless #defined to be a different length. This default length - * suffices to get rid of MALLOC calls except for unusual cases, - * such as decimal-to-binary conversion of a very long string of - * digits. The longest string dtoa can return is about 751 bytes - * long. For conversions by strtod of strings of 800 digits and - * all dtoa conversions in single-threaded executions with 8-byte - * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte - * pointers, PRIVATE_MEM >= 7112 appears adequate. - * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK - * #defined automatically on IEEE systems. On such systems, - * when INFNAN_CHECK is #defined, strtod checks - * for Infinity and NaN (case insensitively). On some systems - * (e.g., some HP systems), it may be necessary to #define NAN_WORD0 - * appropriately -- to the most significant word of a quiet NaN. - * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.) - * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined, - * strtod also accepts (case insensitively) strings of the form - * NaN(x), where x is a string of hexadecimal digits and spaces; - * if there is only one string of hexadecimal digits, it is taken - * for the 52 fraction bits of the resulting NaN; if there are two - * or more strings of hex digits, the first is for the high 20 bits, - * the second and subsequent for the low 32 bits, with intervening - * white space ignored; but if this results in none of the 52 - * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0 - * and NAN_WORD1 are used instead. - * #define MULTIPLE_THREADS if the system offers preemptively scheduled - * multiple threads. In this case, you must provide (or suitably - * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed - * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed - * in pow5mult, ensures lazy evaluation of only one copy of high - * powers of 5; omitting this lock would introduce a small - * probability of wasting memory, but would otherwise be harmless.) - * You must also invoke freedtoa(s) to free the value s returned by - * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. - * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that - * avoids underflows on inputs whose result does not underflow. - * If you #define NO_IEEE_Scale on a machine that uses IEEE-format - * floating-point numbers and flushes underflows to zero rather - * than implementing gradual underflow, then you must also #define - * Sudden_Underflow. - * #define USE_LOCALE to use the current locale's decimal_point value. - * #define SET_INEXACT if IEEE arithmetic is being used and extra - * computation should be done to set the inexact flag when the - * result is inexact and avoid setting inexact when the result - * is exact. In this case, dtoa.c must be compiled in - * an environment, perhaps provided by #include "dtoa.c" in a - * suitable wrapper, that defines two functions, - * int get_inexact(void); - * void clear_inexact(void); - * such that get_inexact() returns a nonzero value if the - * inexact bit is already set, and clear_inexact() sets the - * inexact bit to 0. When SET_INEXACT is #defined, strtod - * also does extra computations to set the underflow and overflow - * flags when appropriate (i.e., when the result is tiny and - * inexact or when it is a numeric value rounded to +-infinity). - * #define NO_ERRNO if strtod should not assign errno = ERANGE when - * the result overflows to +-Infinity or underflows to 0. - * #define NO_HEX_FP to omit recognition of hexadecimal floating-point - * values by strtod. - * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now) - * to disable logic for "fast" testing of very long input strings - * to strtod. This testing proceeds by initially truncating the - * input string, then if necessary comparing the whole string with - * a decimal expansion to decide close cases. This logic is only - * used for input more than STRTOD_DIGLIM digits long (default 40). - */ - -#include "dtoa_config.h" - -#ifndef Long -#define Long long -#endif -#ifndef ULong -typedef unsigned Long ULong; -#endif - -#ifdef DEBUG -#include "stdio.h" -#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} -#endif - -#include "stdlib.h" -#include "string.h" - -#ifdef USE_LOCALE -#include "locale.h" -#endif - -#ifdef Honor_FLT_ROUNDS -#ifndef Trust_FLT_ROUNDS -#include -#endif -#endif - -#ifdef MALLOC -#ifdef KR_headers -extern char *MALLOC(); -#else -extern void *MALLOC(size_t); -#endif -#else -#define MALLOC malloc -#endif - -#ifndef Omit_Private_Memory -#ifndef PRIVATE_MEM -#define PRIVATE_MEM 2304 -#endif -#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) -static double private_mem[PRIVATE_mem], *pmem_next = private_mem; -#endif - -#undef IEEE_Arith -#undef Avoid_Underflow -#ifdef IEEE_MC68k -#define IEEE_Arith -#endif -#ifdef IEEE_8087 -#define IEEE_Arith -#endif - -#ifdef IEEE_Arith -#ifndef NO_INFNAN_CHECK -#undef INFNAN_CHECK -#define INFNAN_CHECK -#endif -#else -#undef INFNAN_CHECK -#define NO_STRTOD_BIGCOMP -#endif - -#include "errno.h" - -#ifdef Bad_float_h - -#ifdef IEEE_Arith -#define DBL_DIG 15 -#define DBL_MAX_10_EXP 308 -#define DBL_MAX_EXP 1024 -#define FLT_RADIX 2 -#endif /*IEEE_Arith*/ - -#ifdef IBM -#define DBL_DIG 16 -#define DBL_MAX_10_EXP 75 -#define DBL_MAX_EXP 63 -#define FLT_RADIX 16 -#define DBL_MAX 7.2370055773322621e+75 -#endif - -#ifdef VAX -#define DBL_DIG 16 -#define DBL_MAX_10_EXP 38 -#define DBL_MAX_EXP 127 -#define FLT_RADIX 2 -#define DBL_MAX 1.7014118346046923e+38 -#endif - -#ifndef LONG_MAX -#define LONG_MAX 2147483647 -#endif - -#else /* ifndef Bad_float_h */ -#include "float.h" -#endif /* Bad_float_h */ - -#ifndef __MATH_H__ -#include "math.h" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef CONST -#ifdef KR_headers -#define CONST /* blank */ -#else -#define CONST const -#endif -#endif - -#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1 -Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined. -#endif - -typedef union { double d; ULong L[2]; } U; - -#ifdef IEEE_8087 -#define word0(x) (x)->L[1] -#define word1(x) (x)->L[0] -#else -#define word0(x) (x)->L[0] -#define word1(x) (x)->L[1] -#endif -#define dval(x) (x)->d - -#ifndef STRTOD_DIGLIM -#define STRTOD_DIGLIM 40 -#endif - -#ifdef DIGLIM_DEBUG -extern int strtod_diglim; -#else -#define strtod_diglim STRTOD_DIGLIM -#endif - -/* The following definition of Storeinc is appropriate for MIPS processors. - * An alternative that might be better on some machines is - * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) - */ -#if defined(IEEE_8087) + defined(VAX) -#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ -((unsigned short *)a)[0] = (unsigned short)c, a++) -#else -#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ -((unsigned short *)a)[1] = (unsigned short)c, a++) -#endif - -/* #define P DBL_MANT_DIG */ -/* Ten_pmax = floor(P*log(2)/log(5)) */ -/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ -/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ -/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ - -#ifdef IEEE_Arith -#define Exp_shift 20 -#define Exp_shift1 20 -#define Exp_msk1 0x100000 -#define Exp_msk11 0x100000 -#define Exp_mask 0x7ff00000 -#define P 53 -#define Nbits 53 -#define Bias 1023 -#define Emax 1023 -#define Emin (-1022) -#define Exp_1 0x3ff00000 -#define Exp_11 0x3ff00000 -#define Ebits 11 -#define Frac_mask 0xfffff -#define Frac_mask1 0xfffff -#define Ten_pmax 22 -#define Bletch 0x10 -#define Bndry_mask 0xfffff -#define Bndry_mask1 0xfffff -#define LSB 1 -#define Sign_bit 0x80000000 -#define Log2P 1 -#define Tiny0 0 -#define Tiny1 1 -#define Quick_max 14 -#define Int_max 14 -#ifndef NO_IEEE_Scale -#define Avoid_Underflow -#ifdef Flush_Denorm /* debugging option */ -#undef Sudden_Underflow -#endif -#endif - -#ifndef Flt_Rounds -#ifdef FLT_ROUNDS -#define Flt_Rounds FLT_ROUNDS -#else -#define Flt_Rounds 1 -#endif -#endif /*Flt_Rounds*/ - -#ifdef Honor_FLT_ROUNDS -#undef Check_FLT_ROUNDS -#define Check_FLT_ROUNDS -#else -#define Rounding Flt_Rounds -#endif - -#else /* ifndef IEEE_Arith */ -#undef Check_FLT_ROUNDS -#undef Honor_FLT_ROUNDS -#undef SET_INEXACT -#undef Sudden_Underflow -#define Sudden_Underflow -#ifdef IBM -#undef Flt_Rounds -#define Flt_Rounds 0 -#define Exp_shift 24 -#define Exp_shift1 24 -#define Exp_msk1 0x1000000 -#define Exp_msk11 0x1000000 -#define Exp_mask 0x7f000000 -#define P 14 -#define Nbits 56 -#define Bias 65 -#define Emax 248 -#define Emin (-260) -#define Exp_1 0x41000000 -#define Exp_11 0x41000000 -#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ -#define Frac_mask 0xffffff -#define Frac_mask1 0xffffff -#define Bletch 4 -#define Ten_pmax 22 -#define Bndry_mask 0xefffff -#define Bndry_mask1 0xffffff -#define LSB 1 -#define Sign_bit 0x80000000 -#define Log2P 4 -#define Tiny0 0x100000 -#define Tiny1 0 -#define Quick_max 14 -#define Int_max 15 -#else /* VAX */ -#undef Flt_Rounds -#define Flt_Rounds 1 -#define Exp_shift 23 -#define Exp_shift1 7 -#define Exp_msk1 0x80 -#define Exp_msk11 0x800000 -#define Exp_mask 0x7f80 -#define P 56 -#define Nbits 56 -#define Bias 129 -#define Emax 126 -#define Emin (-129) -#define Exp_1 0x40800000 -#define Exp_11 0x4080 -#define Ebits 8 -#define Frac_mask 0x7fffff -#define Frac_mask1 0xffff007f -#define Ten_pmax 24 -#define Bletch 2 -#define Bndry_mask 0xffff007f -#define Bndry_mask1 0xffff007f -#define LSB 0x10000 -#define Sign_bit 0x8000 -#define Log2P 1 -#define Tiny0 0x80 -#define Tiny1 0 -#define Quick_max 15 -#define Int_max 15 -#endif /* IBM, VAX */ -#endif /* IEEE_Arith */ - -#ifndef IEEE_Arith -#define ROUND_BIASED -#else -#ifdef ROUND_BIASED_without_Round_Up -#undef ROUND_BIASED -#define ROUND_BIASED -#endif -#endif - -#ifdef RND_PRODQUOT -#define rounded_product(a,b) a = rnd_prod(a, b) -#define rounded_quotient(a,b) a = rnd_quot(a, b) -#ifdef KR_headers -extern double rnd_prod(), rnd_quot(); -#else -extern double rnd_prod(double, double), rnd_quot(double, double); -#endif -#else -#define rounded_product(a,b) a *= b -#define rounded_quotient(a,b) a /= b -#endif - -#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) -#define Big1 0xffffffff - -#ifndef Pack_32 -#define Pack_32 -#endif - -typedef struct BCinfo BCinfo; - struct -BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflchk; }; - -#ifdef KR_headers -#define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff) -#else -#define FFFFFFFF 0xffffffffUL -#endif - -#ifdef NO_LONG_LONG -#undef ULLong -#ifdef Just_16 -#undef Pack_32 -/* When Pack_32 is not defined, we store 16 bits per 32-bit Long. - * This makes some inner loops simpler and sometimes saves work - * during multiplications, but it often seems to make things slightly - * slower. Hence the default is now to store 32 bits per Long. - */ -#endif -#else /* long long available */ -#ifndef Llong -#define Llong long long -#endif -#ifndef ULLong -#define ULLong unsigned Llong -#endif -#endif /* NO_LONG_LONG */ - -#ifndef MULTIPLE_THREADS -#define ACQUIRE_DTOA_LOCK(n) /*nothing*/ -#define FREE_DTOA_LOCK(n) /*nothing*/ -#endif - -#define Kmax 7 - -#ifdef __cplusplus -extern "C" double fpconv_strtod(const char *s00, char **se); -extern "C" char *dtoa(double d, int mode, int ndigits, - int *decpt, int *sign, char **rve); -#endif - - struct -Bigint { - struct Bigint *next; - int k, maxwds, sign, wds; - ULong x[1]; - }; - - typedef struct Bigint Bigint; - - static Bigint *freelist[Kmax+1]; - - static Bigint * -Balloc -#ifdef KR_headers - (k) int k; -#else - (int k) -#endif -{ - int x; - Bigint *rv; -#ifndef Omit_Private_Memory - unsigned int len; -#endif - - ACQUIRE_DTOA_LOCK(0); - /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */ - /* but this case seems very unlikely. */ - if (k <= Kmax && (rv = freelist[k])) - freelist[k] = rv->next; - else { - x = 1 << k; -#ifdef Omit_Private_Memory - rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong)); -#else - len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) - /sizeof(double); - if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) { - rv = (Bigint*)pmem_next; - pmem_next += len; - } - else - rv = (Bigint*)MALLOC(len*sizeof(double)); -#endif - rv->k = k; - rv->maxwds = x; - } - FREE_DTOA_LOCK(0); - rv->sign = rv->wds = 0; - return rv; - } - - static void -Bfree -#ifdef KR_headers - (v) Bigint *v; -#else - (Bigint *v) -#endif -{ - if (v) { - if (v->k > Kmax) -#ifdef FREE - FREE((void*)v); -#else - free((void*)v); -#endif - else { - ACQUIRE_DTOA_LOCK(0); - v->next = freelist[v->k]; - freelist[v->k] = v; - FREE_DTOA_LOCK(0); - } - } - } - -#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ -y->wds*sizeof(Long) + 2*sizeof(int)) - - static Bigint * -multadd -#ifdef KR_headers - (b, m, a) Bigint *b; int m, a; -#else - (Bigint *b, int m, int a) /* multiply by m and add a */ -#endif -{ - int i, wds; -#ifdef ULLong - ULong *x; - ULLong carry, y; -#else - ULong carry, *x, y; -#ifdef Pack_32 - ULong xi, z; -#endif -#endif - Bigint *b1; - - wds = b->wds; - x = b->x; - i = 0; - carry = a; - do { -#ifdef ULLong - y = *x * (ULLong)m + carry; - carry = y >> 32; - *x++ = y & FFFFFFFF; -#else -#ifdef Pack_32 - xi = *x; - y = (xi & 0xffff) * m + carry; - z = (xi >> 16) * m + (y >> 16); - carry = z >> 16; - *x++ = (z << 16) + (y & 0xffff); -#else - y = *x * m + carry; - carry = y >> 16; - *x++ = y & 0xffff; -#endif -#endif - } - while(++i < wds); - if (carry) { - if (wds >= b->maxwds) { - b1 = Balloc(b->k+1); - Bcopy(b1, b); - Bfree(b); - b = b1; - } - b->x[wds++] = carry; - b->wds = wds; - } - return b; - } - - static Bigint * -s2b -#ifdef KR_headers - (s, nd0, nd, y9, dplen) CONST char *s; int nd0, nd, dplen; ULong y9; -#else - (const char *s, int nd0, int nd, ULong y9, int dplen) -#endif -{ - Bigint *b; - int i, k; - Long x, y; - - x = (nd + 8) / 9; - for(k = 0, y = 1; x > y; y <<= 1, k++) ; -#ifdef Pack_32 - b = Balloc(k); - b->x[0] = y9; - b->wds = 1; -#else - b = Balloc(k+1); - b->x[0] = y9 & 0xffff; - b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; -#endif - - i = 9; - if (9 < nd0) { - s += 9; - do b = multadd(b, 10, *s++ - '0'); - while(++i < nd0); - s += dplen; - } - else - s += dplen + 9; - for(; i < nd; i++) - b = multadd(b, 10, *s++ - '0'); - return b; - } - - static int -hi0bits -#ifdef KR_headers - (x) ULong x; -#else - (ULong x) -#endif -{ - int k = 0; - - if (!(x & 0xffff0000)) { - k = 16; - x <<= 16; - } - if (!(x & 0xff000000)) { - k += 8; - x <<= 8; - } - if (!(x & 0xf0000000)) { - k += 4; - x <<= 4; - } - if (!(x & 0xc0000000)) { - k += 2; - x <<= 2; - } - if (!(x & 0x80000000)) { - k++; - if (!(x & 0x40000000)) - return 32; - } - return k; - } - - static int -lo0bits -#ifdef KR_headers - (y) ULong *y; -#else - (ULong *y) -#endif -{ - int k; - ULong x = *y; - - if (x & 7) { - if (x & 1) - return 0; - if (x & 2) { - *y = x >> 1; - return 1; - } - *y = x >> 2; - return 2; - } - k = 0; - if (!(x & 0xffff)) { - k = 16; - x >>= 16; - } - if (!(x & 0xff)) { - k += 8; - x >>= 8; - } - if (!(x & 0xf)) { - k += 4; - x >>= 4; - } - if (!(x & 0x3)) { - k += 2; - x >>= 2; - } - if (!(x & 1)) { - k++; - x >>= 1; - if (!x) - return 32; - } - *y = x; - return k; - } - - static Bigint * -i2b -#ifdef KR_headers - (i) int i; -#else - (int i) -#endif -{ - Bigint *b; - - b = Balloc(1); - b->x[0] = i; - b->wds = 1; - return b; - } - - static Bigint * -mult -#ifdef KR_headers - (a, b) Bigint *a, *b; -#else - (Bigint *a, Bigint *b) -#endif -{ - Bigint *c; - int k, wa, wb, wc; - ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; - ULong y; -#ifdef ULLong - ULLong carry, z; -#else - ULong carry, z; -#ifdef Pack_32 - ULong z2; -#endif -#endif - - if (a->wds < b->wds) { - c = a; - a = b; - b = c; - } - k = a->k; - wa = a->wds; - wb = b->wds; - wc = wa + wb; - if (wc > a->maxwds) - k++; - c = Balloc(k); - for(x = c->x, xa = x + wc; x < xa; x++) - *x = 0; - xa = a->x; - xae = xa + wa; - xb = b->x; - xbe = xb + wb; - xc0 = c->x; -#ifdef ULLong - for(; xb < xbe; xc0++) { - if ((y = *xb++)) { - x = xa; - xc = xc0; - carry = 0; - do { - z = *x++ * (ULLong)y + *xc + carry; - carry = z >> 32; - *xc++ = z & FFFFFFFF; - } - while(x < xae); - *xc = carry; - } - } -#else -#ifdef Pack_32 - for(; xb < xbe; xb++, xc0++) { - if (y = *xb & 0xffff) { - x = xa; - xc = xc0; - carry = 0; - do { - z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; - carry = z >> 16; - z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; - carry = z2 >> 16; - Storeinc(xc, z2, z); - } - while(x < xae); - *xc = carry; - } - if (y = *xb >> 16) { - x = xa; - xc = xc0; - carry = 0; - z2 = *xc; - do { - z = (*x & 0xffff) * y + (*xc >> 16) + carry; - carry = z >> 16; - Storeinc(xc, z, z2); - z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; - carry = z2 >> 16; - } - while(x < xae); - *xc = z2; - } - } -#else - for(; xb < xbe; xc0++) { - if (y = *xb++) { - x = xa; - xc = xc0; - carry = 0; - do { - z = *x++ * y + *xc + carry; - carry = z >> 16; - *xc++ = z & 0xffff; - } - while(x < xae); - *xc = carry; - } - } -#endif -#endif - for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; - c->wds = wc; - return c; - } - - static Bigint *p5s; - - static Bigint * -pow5mult -#ifdef KR_headers - (b, k) Bigint *b; int k; -#else - (Bigint *b, int k) -#endif -{ - Bigint *b1, *p5, *p51; - int i; - static int p05[3] = { 5, 25, 125 }; - - if ((i = k & 3)) - b = multadd(b, p05[i-1], 0); - - if (!(k >>= 2)) - return b; - if (!(p5 = p5s)) { - /* first time */ -#ifdef MULTIPLE_THREADS - ACQUIRE_DTOA_LOCK(1); - if (!(p5 = p5s)) { - p5 = p5s = i2b(625); - p5->next = 0; - } - FREE_DTOA_LOCK(1); -#else - p5 = p5s = i2b(625); - p5->next = 0; -#endif - } - for(;;) { - if (k & 1) { - b1 = mult(b, p5); - Bfree(b); - b = b1; - } - if (!(k >>= 1)) - break; - if (!(p51 = p5->next)) { -#ifdef MULTIPLE_THREADS - ACQUIRE_DTOA_LOCK(1); - if (!(p51 = p5->next)) { - p51 = p5->next = mult(p5,p5); - p51->next = 0; - } - FREE_DTOA_LOCK(1); -#else - p51 = p5->next = mult(p5,p5); - p51->next = 0; -#endif - } - p5 = p51; - } - return b; - } - - static Bigint * -lshift -#ifdef KR_headers - (b, k) Bigint *b; int k; -#else - (Bigint *b, int k) -#endif -{ - int i, k1, n, n1; - Bigint *b1; - ULong *x, *x1, *xe, z; - -#ifdef Pack_32 - n = k >> 5; -#else - n = k >> 4; -#endif - k1 = b->k; - n1 = n + b->wds + 1; - for(i = b->maxwds; n1 > i; i <<= 1) - k1++; - b1 = Balloc(k1); - x1 = b1->x; - for(i = 0; i < n; i++) - *x1++ = 0; - x = b->x; - xe = x + b->wds; -#ifdef Pack_32 - if (k &= 0x1f) { - k1 = 32 - k; - z = 0; - do { - *x1++ = *x << k | z; - z = *x++ >> k1; - } - while(x < xe); - if ((*x1 = z)) - ++n1; - } -#else - if (k &= 0xf) { - k1 = 16 - k; - z = 0; - do { - *x1++ = *x << k & 0xffff | z; - z = *x++ >> k1; - } - while(x < xe); - if (*x1 = z) - ++n1; - } -#endif - else do - *x1++ = *x++; - while(x < xe); - b1->wds = n1 - 1; - Bfree(b); - return b1; - } - - static int -cmp -#ifdef KR_headers - (a, b) Bigint *a, *b; -#else - (Bigint *a, Bigint *b) -#endif -{ - ULong *xa, *xa0, *xb, *xb0; - int i, j; - - i = a->wds; - j = b->wds; -#ifdef DEBUG - if (i > 1 && !a->x[i-1]) - Bug("cmp called with a->x[a->wds-1] == 0"); - if (j > 1 && !b->x[j-1]) - Bug("cmp called with b->x[b->wds-1] == 0"); -#endif - if (i -= j) - return i; - xa0 = a->x; - xa = xa0 + j; - xb0 = b->x; - xb = xb0 + j; - for(;;) { - if (*--xa != *--xb) - return *xa < *xb ? -1 : 1; - if (xa <= xa0) - break; - } - return 0; - } - - static Bigint * -diff -#ifdef KR_headers - (a, b) Bigint *a, *b; -#else - (Bigint *a, Bigint *b) -#endif -{ - Bigint *c; - int i, wa, wb; - ULong *xa, *xae, *xb, *xbe, *xc; -#ifdef ULLong - ULLong borrow, y; -#else - ULong borrow, y; -#ifdef Pack_32 - ULong z; -#endif -#endif - - i = cmp(a,b); - if (!i) { - c = Balloc(0); - c->wds = 1; - c->x[0] = 0; - return c; - } - if (i < 0) { - c = a; - a = b; - b = c; - i = 1; - } - else - i = 0; - c = Balloc(a->k); - c->sign = i; - wa = a->wds; - xa = a->x; - xae = xa + wa; - wb = b->wds; - xb = b->x; - xbe = xb + wb; - xc = c->x; - borrow = 0; -#ifdef ULLong - do { - y = (ULLong)*xa++ - *xb++ - borrow; - borrow = y >> 32 & (ULong)1; - *xc++ = y & FFFFFFFF; - } - while(xb < xbe); - while(xa < xae) { - y = *xa++ - borrow; - borrow = y >> 32 & (ULong)1; - *xc++ = y & FFFFFFFF; - } -#else -#ifdef Pack_32 - do { - y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(xc, z, y); - } - while(xb < xbe); - while(xa < xae) { - y = (*xa & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*xa++ >> 16) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(xc, z, y); - } -#else - do { - y = *xa++ - *xb++ - borrow; - borrow = (y & 0x10000) >> 16; - *xc++ = y & 0xffff; - } - while(xb < xbe); - while(xa < xae) { - y = *xa++ - borrow; - borrow = (y & 0x10000) >> 16; - *xc++ = y & 0xffff; - } -#endif -#endif - while(!*--xc) - wa--; - c->wds = wa; - return c; - } - - static double -ulp -#ifdef KR_headers - (x) U *x; -#else - (U *x) -#endif -{ - Long L; - U u; - - L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; -#ifndef Avoid_Underflow -#ifndef Sudden_Underflow - if (L > 0) { -#endif -#endif -#ifdef IBM - L |= Exp_msk1 >> 4; -#endif - word0(&u) = L; - word1(&u) = 0; -#ifndef Avoid_Underflow -#ifndef Sudden_Underflow - } - else { - L = -L >> Exp_shift; - if (L < Exp_shift) { - word0(&u) = 0x80000 >> L; - word1(&u) = 0; - } - else { - word0(&u) = 0; - L -= Exp_shift; - word1(&u) = L >= 31 ? 1 : 1 << 31 - L; - } - } -#endif -#endif - return dval(&u); - } - - static double -b2d -#ifdef KR_headers - (a, e) Bigint *a; int *e; -#else - (Bigint *a, int *e) -#endif -{ - ULong *xa, *xa0, w, y, z; - int k; - U d; -#ifdef VAX - ULong d0, d1; -#else -#define d0 word0(&d) -#define d1 word1(&d) -#endif - - xa0 = a->x; - xa = xa0 + a->wds; - y = *--xa; -#ifdef DEBUG - if (!y) Bug("zero y in b2d"); -#endif - k = hi0bits(y); - *e = 32 - k; -#ifdef Pack_32 - if (k < Ebits) { - d0 = Exp_1 | y >> (Ebits - k); - w = xa > xa0 ? *--xa : 0; - d1 = y << ((32-Ebits) + k) | w >> (Ebits - k); - goto ret_d; - } - z = xa > xa0 ? *--xa : 0; - if (k -= Ebits) { - d0 = Exp_1 | y << k | z >> (32 - k); - y = xa > xa0 ? *--xa : 0; - d1 = z << k | y >> (32 - k); - } - else { - d0 = Exp_1 | y; - d1 = z; - } -#else - if (k < Ebits + 16) { - z = xa > xa0 ? *--xa : 0; - d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; - w = xa > xa0 ? *--xa : 0; - y = xa > xa0 ? *--xa : 0; - d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; - goto ret_d; - } - z = xa > xa0 ? *--xa : 0; - w = xa > xa0 ? *--xa : 0; - k -= Ebits + 16; - d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; - y = xa > xa0 ? *--xa : 0; - d1 = w << k + 16 | y << k; -#endif - ret_d: -#ifdef VAX - word0(&d) = d0 >> 16 | d0 << 16; - word1(&d) = d1 >> 16 | d1 << 16; -#else -#undef d0 -#undef d1 -#endif - return dval(&d); - } - - static Bigint * -d2b -#ifdef KR_headers - (d, e, bits) U *d; int *e, *bits; -#else - (U *d, int *e, int *bits) -#endif -{ - Bigint *b; - int de, k; - ULong *x, y, z; -#ifndef Sudden_Underflow - int i; -#endif -#ifdef VAX - ULong d0, d1; - d0 = word0(d) >> 16 | word0(d) << 16; - d1 = word1(d) >> 16 | word1(d) << 16; -#else -#define d0 word0(d) -#define d1 word1(d) -#endif - -#ifdef Pack_32 - b = Balloc(1); -#else - b = Balloc(2); -#endif - x = b->x; - - z = d0 & Frac_mask; - d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ -#ifdef Sudden_Underflow - de = (int)(d0 >> Exp_shift); -#ifndef IBM - z |= Exp_msk11; -#endif -#else - if ((de = (int)(d0 >> Exp_shift))) - z |= Exp_msk1; -#endif -#ifdef Pack_32 - if ((y = d1)) { - if ((k = lo0bits(&y))) { - x[0] = y | z << (32 - k); - z >>= k; - } - else - x[0] = y; -#ifndef Sudden_Underflow - i = -#endif - b->wds = (x[1] = z) ? 2 : 1; - } - else { - k = lo0bits(&z); - x[0] = z; -#ifndef Sudden_Underflow - i = -#endif - b->wds = 1; - k += 32; - } -#else - if (y = d1) { - if (k = lo0bits(&y)) - if (k >= 16) { - x[0] = y | z << 32 - k & 0xffff; - x[1] = z >> k - 16 & 0xffff; - x[2] = z >> k; - i = 2; - } - else { - x[0] = y & 0xffff; - x[1] = y >> 16 | z << 16 - k & 0xffff; - x[2] = z >> k & 0xffff; - x[3] = z >> k+16; - i = 3; - } - else { - x[0] = y & 0xffff; - x[1] = y >> 16; - x[2] = z & 0xffff; - x[3] = z >> 16; - i = 3; - } - } - else { -#ifdef DEBUG - if (!z) - Bug("Zero passed to d2b"); -#endif - k = lo0bits(&z); - if (k >= 16) { - x[0] = z; - i = 0; - } - else { - x[0] = z & 0xffff; - x[1] = z >> 16; - i = 1; - } - k += 32; - } - while(!x[i]) - --i; - b->wds = i + 1; -#endif -#ifndef Sudden_Underflow - if (de) { -#endif -#ifdef IBM - *e = (de - Bias - (P-1) << 2) + k; - *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); -#else - *e = de - Bias - (P-1) + k; - *bits = P - k; -#endif -#ifndef Sudden_Underflow - } - else { - *e = de - Bias - (P-1) + 1 + k; -#ifdef Pack_32 - *bits = 32*i - hi0bits(x[i-1]); -#else - *bits = (i+2)*16 - hi0bits(x[i]); -#endif - } -#endif - return b; - } -#undef d0 -#undef d1 - - static double -ratio -#ifdef KR_headers - (a, b) Bigint *a, *b; -#else - (Bigint *a, Bigint *b) -#endif -{ - U da, db; - int k, ka, kb; - - dval(&da) = b2d(a, &ka); - dval(&db) = b2d(b, &kb); -#ifdef Pack_32 - k = ka - kb + 32*(a->wds - b->wds); -#else - k = ka - kb + 16*(a->wds - b->wds); -#endif -#ifdef IBM - if (k > 0) { - word0(&da) += (k >> 2)*Exp_msk1; - if (k &= 3) - dval(&da) *= 1 << k; - } - else { - k = -k; - word0(&db) += (k >> 2)*Exp_msk1; - if (k &= 3) - dval(&db) *= 1 << k; - } -#else - if (k > 0) - word0(&da) += k*Exp_msk1; - else { - k = -k; - word0(&db) += k*Exp_msk1; - } -#endif - return dval(&da) / dval(&db); - } - - static CONST double -tens[] = { - 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, - 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, - 1e20, 1e21, 1e22 -#ifdef VAX - , 1e23, 1e24 -#endif - }; - - static CONST double -#ifdef IEEE_Arith -bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; -static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, -#ifdef Avoid_Underflow - 9007199254740992.*9007199254740992.e-256 - /* = 2^106 * 1e-256 */ -#else - 1e-256 -#endif - }; -/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */ -/* flag unnecessarily. It leads to a song and dance at the end of strtod. */ -#define Scale_Bit 0x10 -#define n_bigtens 5 -#else -#ifdef IBM -bigtens[] = { 1e16, 1e32, 1e64 }; -static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; -#define n_bigtens 3 -#else -bigtens[] = { 1e16, 1e32 }; -static CONST double tinytens[] = { 1e-16, 1e-32 }; -#define n_bigtens 2 -#endif -#endif - -#undef Need_Hexdig -#ifdef INFNAN_CHECK -#ifndef No_Hex_NaN -#define Need_Hexdig -#endif -#endif - -#ifndef Need_Hexdig -#ifndef NO_HEX_FP -#define Need_Hexdig -#endif -#endif - -#ifdef Need_Hexdig /*{*/ -static unsigned char hexdig[256]; - - static void -#ifdef KR_headers -htinit(h, s, inc) unsigned char *h; unsigned char *s; int inc; -#else -htinit(unsigned char *h, unsigned char *s, int inc) -#endif -{ - int i, j; - for(i = 0; (j = s[i]) !=0; i++) - h[j] = i + inc; - } - - static void -#ifdef KR_headers -hexdig_init() -#else -hexdig_init(void) -#endif -{ -#define USC (unsigned char *) - htinit(hexdig, USC "0123456789", 0x10); - htinit(hexdig, USC "abcdef", 0x10 + 10); - htinit(hexdig, USC "ABCDEF", 0x10 + 10); - } -#endif /* } Need_Hexdig */ - -#ifdef INFNAN_CHECK - -#ifndef NAN_WORD0 -#define NAN_WORD0 0x7ff80000 -#endif - -#ifndef NAN_WORD1 -#define NAN_WORD1 0 -#endif - - static int -match -#ifdef KR_headers - (sp, t) char **sp, *t; -#else - (const char **sp, const char *t) -#endif -{ - int c, d; - CONST char *s = *sp; - - while((d = *t++)) { - if ((c = *++s) >= 'A' && c <= 'Z') - c += 'a' - 'A'; - if (c != d) - return 0; - } - *sp = s + 1; - return 1; - } - -#ifndef No_Hex_NaN - static void -hexnan -#ifdef KR_headers - (rvp, sp) U *rvp; CONST char **sp; -#else - (U *rvp, const char **sp) -#endif -{ - ULong c, x[2]; - CONST char *s; - int c1, havedig, udx0, xshift; - - if (!hexdig['0']) - hexdig_init(); - x[0] = x[1] = 0; - havedig = xshift = 0; - udx0 = 1; - s = *sp; - /* allow optional initial 0x or 0X */ - while((c = *(CONST unsigned char*)(s+1)) && c <= ' ') - ++s; - if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')) - s += 2; - while((c = *(CONST unsigned char*)++s)) { - if ((c1 = hexdig[c])) - c = c1 & 0xf; - else if (c <= ' ') { - if (udx0 && havedig) { - udx0 = 0; - xshift = 1; - } - continue; - } -#ifdef GDTOA_NON_PEDANTIC_NANCHECK - else if (/*(*/ c == ')' && havedig) { - *sp = s + 1; - break; - } - else - return; /* invalid form: don't change *sp */ -#else - else { - do { - if (/*(*/ c == ')') { - *sp = s + 1; - break; - } - } while((c = *++s)); - break; - } -#endif - havedig = 1; - if (xshift) { - xshift = 0; - x[0] = x[1]; - x[1] = 0; - } - if (udx0) - x[0] = (x[0] << 4) | (x[1] >> 28); - x[1] = (x[1] << 4) | c; - } - if ((x[0] &= 0xfffff) || x[1]) { - word0(rvp) = Exp_mask | x[0]; - word1(rvp) = x[1]; - } - } -#endif /*No_Hex_NaN*/ -#endif /* INFNAN_CHECK */ - -#ifdef Pack_32 -#define ULbits 32 -#define kshift 5 -#define kmask 31 -#else -#define ULbits 16 -#define kshift 4 -#define kmask 15 -#endif - -#if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/ - static Bigint * -#ifdef KR_headers -increment(b) Bigint *b; -#else -increment(Bigint *b) -#endif -{ - ULong *x, *xe; - Bigint *b1; - - x = b->x; - xe = x + b->wds; - do { - if (*x < (ULong)0xffffffffL) { - ++*x; - return b; - } - *x++ = 0; - } while(x < xe); - { - if (b->wds >= b->maxwds) { - b1 = Balloc(b->k+1); - Bcopy(b1,b); - Bfree(b); - b = b1; - } - b->x[b->wds++] = 1; - } - return b; - } - -#endif /*}*/ - -#ifndef NO_HEX_FP /*{*/ - - static void -#ifdef KR_headers -rshift(b, k) Bigint *b; int k; -#else -rshift(Bigint *b, int k) -#endif -{ - ULong *x, *x1, *xe, y; - int n; - - x = x1 = b->x; - n = k >> kshift; - if (n < b->wds) { - xe = x + b->wds; - x += n; - if (k &= kmask) { - n = 32 - k; - y = *x++ >> k; - while(x < xe) { - *x1++ = (y | (*x << n)) & 0xffffffff; - y = *x++ >> k; - } - if ((*x1 = y) !=0) - x1++; - } - else - while(x < xe) - *x1++ = *x++; - } - if ((b->wds = x1 - b->x) == 0) - b->x[0] = 0; - } - - static ULong -#ifdef KR_headers -any_on(b, k) Bigint *b; int k; -#else -any_on(Bigint *b, int k) -#endif -{ - int n, nwds; - ULong *x, *x0, x1, x2; - - x = b->x; - nwds = b->wds; - n = k >> kshift; - if (n > nwds) - n = nwds; - else if (n < nwds && (k &= kmask)) { - x1 = x2 = x[n]; - x1 >>= k; - x1 <<= k; - if (x1 != x2) - return 1; - } - x0 = x; - x += n; - while(x > x0) - if (*--x) - return 1; - return 0; - } - -enum { /* rounding values: same as FLT_ROUNDS */ - Round_zero = 0, - Round_near = 1, - Round_up = 2, - Round_down = 3 - }; - - void -#ifdef KR_headers -gethex(sp, rvp, rounding, sign) - CONST char **sp; U *rvp; int rounding, sign; -#else -gethex( CONST char **sp, U *rvp, int rounding, int sign) -#endif -{ - Bigint *b; - CONST unsigned char *decpt, *s0, *s, *s1; - Long e, e1; - ULong L, lostbits, *x; - int big, denorm, esign, havedig, k, n, nbits, up, zret; -#ifdef IBM - int j; -#endif - enum { -#ifdef IEEE_Arith /*{{*/ - emax = 0x7fe - Bias - P + 1, - emin = Emin - P + 1 -#else /*}{*/ - emin = Emin - P, -#ifdef VAX - emax = 0x7ff - Bias - P + 1 -#endif -#ifdef IBM - emax = 0x7f - Bias - P -#endif -#endif /*}}*/ - }; -#ifdef USE_LOCALE - int i; -#ifdef NO_LOCALE_CACHE - const unsigned char *decimalpoint = (unsigned char*) - localeconv()->decimal_point; -#else - const unsigned char *decimalpoint; - static unsigned char *decimalpoint_cache; - if (!(s0 = decimalpoint_cache)) { - s0 = (unsigned char*)localeconv()->decimal_point; - if ((decimalpoint_cache = (unsigned char*) - MALLOC(strlen((CONST char*)s0) + 1))) { - strcpy((char*)decimalpoint_cache, (CONST char*)s0); - s0 = decimalpoint_cache; - } - } - decimalpoint = s0; -#endif -#endif - - if (!hexdig['0']) - hexdig_init(); - havedig = 0; - s0 = *(CONST unsigned char **)sp + 2; - while(s0[havedig] == '0') - havedig++; - s0 += havedig; - s = s0; - decpt = 0; - zret = 0; - e = 0; - if (hexdig[*s]) - havedig++; - else { - zret = 1; -#ifdef USE_LOCALE - for(i = 0; decimalpoint[i]; ++i) { - if (s[i] != decimalpoint[i]) - goto pcheck; - } - decpt = s += i; -#else - if (*s != '.') - goto pcheck; - decpt = ++s; -#endif - if (!hexdig[*s]) - goto pcheck; - while(*s == '0') - s++; - if (hexdig[*s]) - zret = 0; - havedig = 1; - s0 = s; - } - while(hexdig[*s]) - s++; -#ifdef USE_LOCALE - if (*s == *decimalpoint && !decpt) { - for(i = 1; decimalpoint[i]; ++i) { - if (s[i] != decimalpoint[i]) - goto pcheck; - } - decpt = s += i; -#else - if (*s == '.' && !decpt) { - decpt = ++s; -#endif - while(hexdig[*s]) - s++; - }/*}*/ - if (decpt) - e = -(((Long)(s-decpt)) << 2); - pcheck: - s1 = s; - big = esign = 0; - switch(*s) { - case 'p': - case 'P': - switch(*++s) { - case '-': - esign = 1; - /* no break */ - case '+': - s++; - } - if ((n = hexdig[*s]) == 0 || n > 0x19) { - s = s1; - break; - } - e1 = n - 0x10; - while((n = hexdig[*++s]) !=0 && n <= 0x19) { - if (e1 & 0xf8000000) - big = 1; - e1 = 10*e1 + n - 0x10; - } - if (esign) - e1 = -e1; - e += e1; - } - *sp = (char*)s; - if (!havedig) - *sp = (char*)s0 - 1; - if (zret) - goto retz1; - if (big) { - if (esign) { -#ifdef IEEE_Arith - switch(rounding) { - case Round_up: - if (sign) - break; - goto ret_tiny; - case Round_down: - if (!sign) - break; - goto ret_tiny; - } -#endif - goto retz; -#ifdef IEEE_Arith - ret_tiny: -#ifndef NO_ERRNO - errno = ERANGE; -#endif - word0(rvp) = 0; - word1(rvp) = 1; - return; -#endif /* IEEE_Arith */ - } - switch(rounding) { - case Round_near: - goto ovfl1; - case Round_up: - if (!sign) - goto ovfl1; - goto ret_big; - case Round_down: - if (sign) - goto ovfl1; - goto ret_big; - } - ret_big: - word0(rvp) = Big0; - word1(rvp) = Big1; - return; - } - n = s1 - s0 - 1; - for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1) - k++; - b = Balloc(k); - x = b->x; - n = 0; - L = 0; -#ifdef USE_LOCALE - for(i = 0; decimalpoint[i+1]; ++i); -#endif - while(s1 > s0) { -#ifdef USE_LOCALE - if (*--s1 == decimalpoint[i]) { - s1 -= i; - continue; - } -#else - if (*--s1 == '.') - continue; -#endif - if (n == ULbits) { - *x++ = L; - L = 0; - n = 0; - } - L |= (hexdig[*s1] & 0x0f) << n; - n += 4; - } - *x++ = L; - b->wds = n = x - b->x; - n = ULbits*n - hi0bits(L); - nbits = Nbits; - lostbits = 0; - x = b->x; - if (n > nbits) { - n -= nbits; - if (any_on(b,n)) { - lostbits = 1; - k = n - 1; - if (x[k>>kshift] & 1 << (k & kmask)) { - lostbits = 2; - if (k > 0 && any_on(b,k)) - lostbits = 3; - } - } - rshift(b, n); - e += n; - } - else if (n < nbits) { - n = nbits - n; - b = lshift(b, n); - e -= n; - x = b->x; - } - if (e > Emax) { - ovfl: - Bfree(b); - ovfl1: -#ifndef NO_ERRNO - errno = ERANGE; -#endif - word0(rvp) = Exp_mask; - word1(rvp) = 0; - return; - } - denorm = 0; - if (e < emin) { - denorm = 1; - n = emin - e; - if (n >= nbits) { -#ifdef IEEE_Arith /*{*/ - switch (rounding) { - case Round_near: - if (n == nbits && (n < 2 || any_on(b,n-1))) - goto ret_tiny; - break; - case Round_up: - if (!sign) - goto ret_tiny; - break; - case Round_down: - if (sign) - goto ret_tiny; - } -#endif /* } IEEE_Arith */ - Bfree(b); - retz: -#ifndef NO_ERRNO - errno = ERANGE; -#endif - retz1: - rvp->d = 0.; - return; - } - k = n - 1; - if (lostbits) - lostbits = 1; - else if (k > 0) - lostbits = any_on(b,k); - if (x[k>>kshift] & 1 << (k & kmask)) - lostbits |= 2; - nbits -= n; - rshift(b,n); - e = emin; - } - if (lostbits) { - up = 0; - switch(rounding) { - case Round_zero: - break; - case Round_near: - if (lostbits & 2 - && (lostbits & 1) | (x[0] & 1)) - up = 1; - break; - case Round_up: - up = 1 - sign; - break; - case Round_down: - up = sign; - } - if (up) { - k = b->wds; - b = increment(b); - x = b->x; - if (denorm) { -#if 0 - if (nbits == Nbits - 1 - && x[nbits >> kshift] & 1 << (nbits & kmask)) - denorm = 0; /* not currently used */ -#endif - } - else if (b->wds > k - || ((n = nbits & kmask) !=0 - && hi0bits(x[k-1]) < 32-n)) { - rshift(b,1); - if (++e > Emax) - goto ovfl; - } - } - } -#ifdef IEEE_Arith - if (denorm) - word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0; - else - word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20); - word1(rvp) = b->x[0]; -#endif -#ifdef IBM - if ((j = e & 3)) { - k = b->x[0] & ((1 << j) - 1); - rshift(b,j); - if (k) { - switch(rounding) { - case Round_up: - if (!sign) - increment(b); - break; - case Round_down: - if (sign) - increment(b); - break; - case Round_near: - j = 1 << (j-1); - if (k & j && ((k & (j-1)) | lostbits)) - increment(b); - } - } - } - e >>= 2; - word0(rvp) = b->x[1] | ((e + 65 + 13) << 24); - word1(rvp) = b->x[0]; -#endif -#ifdef VAX - /* The next two lines ignore swap of low- and high-order 2 bytes. */ - /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */ - /* word1(rvp) = b->x[0]; */ - word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16); - word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16); -#endif - Bfree(b); - } -#endif /*!NO_HEX_FP}*/ - - static int -#ifdef KR_headers -dshift(b, p2) Bigint *b; int p2; -#else -dshift(Bigint *b, int p2) -#endif -{ - int rv = hi0bits(b->x[b->wds-1]) - 4; - if (p2 > 0) - rv -= p2; - return rv & kmask; - } - - static int -quorem -#ifdef KR_headers - (b, S) Bigint *b, *S; -#else - (Bigint *b, Bigint *S) -#endif -{ - int n; - ULong *bx, *bxe, q, *sx, *sxe; -#ifdef ULLong - ULLong borrow, carry, y, ys; -#else - ULong borrow, carry, y, ys; -#ifdef Pack_32 - ULong si, z, zs; -#endif -#endif - - n = S->wds; -#ifdef DEBUG - /*debug*/ if (b->wds > n) - /*debug*/ Bug("oversize b in quorem"); -#endif - if (b->wds < n) - return 0; - sx = S->x; - sxe = sx + --n; - bx = b->x; - bxe = bx + n; - q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ -#ifdef DEBUG -#ifdef NO_STRTOD_BIGCOMP - /*debug*/ if (q > 9) -#else - /* An oversized q is possible when quorem is called from bigcomp and */ - /* the input is near, e.g., twice the smallest denormalized number. */ - /*debug*/ if (q > 15) -#endif - /*debug*/ Bug("oversized quotient in quorem"); -#endif - if (q) { - borrow = 0; - carry = 0; - do { -#ifdef ULLong - ys = *sx++ * (ULLong)q + carry; - carry = ys >> 32; - y = *bx - (ys & FFFFFFFF) - borrow; - borrow = y >> 32 & (ULong)1; - *bx++ = y & FFFFFFFF; -#else -#ifdef Pack_32 - si = *sx++; - ys = (si & 0xffff) * q + carry; - zs = (si >> 16) * q + (ys >> 16); - carry = zs >> 16; - y = (*bx & 0xffff) - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*bx >> 16) - (zs & 0xffff) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(bx, z, y); -#else - ys = *sx++ * q + carry; - carry = ys >> 16; - y = *bx - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - *bx++ = y & 0xffff; -#endif -#endif - } - while(sx <= sxe); - if (!*bxe) { - bx = b->x; - while(--bxe > bx && !*bxe) - --n; - b->wds = n; - } - } - if (cmp(b, S) >= 0) { - q++; - borrow = 0; - carry = 0; - bx = b->x; - sx = S->x; - do { -#ifdef ULLong - ys = *sx++ + carry; - carry = ys >> 32; - y = *bx - (ys & FFFFFFFF) - borrow; - borrow = y >> 32 & (ULong)1; - *bx++ = y & FFFFFFFF; -#else -#ifdef Pack_32 - si = *sx++; - ys = (si & 0xffff) + carry; - zs = (si >> 16) + (ys >> 16); - carry = zs >> 16; - y = (*bx & 0xffff) - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*bx >> 16) - (zs & 0xffff) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(bx, z, y); -#else - ys = *sx++ + carry; - carry = ys >> 16; - y = *bx - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - *bx++ = y & 0xffff; -#endif -#endif - } - while(sx <= sxe); - bx = b->x; - bxe = bx + n; - if (!*bxe) { - while(--bxe > bx && !*bxe) - --n; - b->wds = n; - } - } - return q; - } - -#if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/ - static double -sulp -#ifdef KR_headers - (x, bc) U *x; BCinfo *bc; -#else - (U *x, BCinfo *bc) -#endif -{ - U u; - double rv; - int i; - - rv = ulp(x); - if (!bc->scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0) - return rv; /* Is there an example where i <= 0 ? */ - word0(&u) = Exp_1 + (i << Exp_shift); - word1(&u) = 0; - return rv * u.d; - } -#endif /*}*/ - -#ifndef NO_STRTOD_BIGCOMP - static void -bigcomp -#ifdef KR_headers - (rv, s0, bc) - U *rv; CONST char *s0; BCinfo *bc; -#else - (U *rv, const char *s0, BCinfo *bc) -#endif -{ - Bigint *b, *d; - int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase; - - dsign = bc->dsign; - nd = bc->nd; - nd0 = bc->nd0; - p5 = nd + bc->e0 - 1; - speccase = 0; -#ifndef Sudden_Underflow - if (rv->d == 0.) { /* special case: value near underflow-to-zero */ - /* threshold was rounded to zero */ - b = i2b(1); - p2 = Emin - P + 1; - bbits = 1; -#ifdef Avoid_Underflow - word0(rv) = (P+2) << Exp_shift; -#else - word1(rv) = 1; -#endif - i = 0; -#ifdef Honor_FLT_ROUNDS - if (bc->rounding == 1) -#endif - { - speccase = 1; - --p2; - dsign = 0; - goto have_i; - } - } - else -#endif - b = d2b(rv, &p2, &bbits); -#ifdef Avoid_Underflow - p2 -= bc->scale; -#endif - /* floor(log2(rv)) == bbits - 1 + p2 */ - /* Check for denormal case. */ - i = P - bbits; - if (i > (j = P - Emin - 1 + p2)) { -#ifdef Sudden_Underflow - Bfree(b); - b = i2b(1); - p2 = Emin; - i = P - 1; -#ifdef Avoid_Underflow - word0(rv) = (1 + bc->scale) << Exp_shift; -#else - word0(rv) = Exp_msk1; -#endif - word1(rv) = 0; -#else - i = j; -#endif - } -#ifdef Honor_FLT_ROUNDS - if (bc->rounding != 1) { - if (i > 0) - b = lshift(b, i); - if (dsign) - b = increment(b); - } - else -#endif - { - b = lshift(b, ++i); - b->x[0] |= 1; - } -#ifndef Sudden_Underflow - have_i: -#endif - p2 -= p5 + i; - d = i2b(1); - /* Arrange for convenient computation of quotients: - * shift left if necessary so divisor has 4 leading 0 bits. - */ - if (p5 > 0) - d = pow5mult(d, p5); - else if (p5 < 0) - b = pow5mult(b, -p5); - if (p2 > 0) { - b2 = p2; - d2 = 0; - } - else { - b2 = 0; - d2 = -p2; - } - i = dshift(d, d2); - if ((b2 += i) > 0) - b = lshift(b, b2); - if ((d2 += i) > 0) - d = lshift(d, d2); - - /* Now b/d = exactly half-way between the two floating-point values */ - /* on either side of the input string. Compute first digit of b/d. */ - - if (!(dig = quorem(b,d))) { - b = multadd(b, 10, 0); /* very unlikely */ - dig = quorem(b,d); - } - - /* Compare b/d with s0 */ - - for(i = 0; i < nd0; ) { - if ((dd = s0[i++] - '0' - dig)) - goto ret; - if (!b->x[0] && b->wds == 1) { - if (i < nd) - dd = 1; - goto ret; - } - b = multadd(b, 10, 0); - dig = quorem(b,d); - } - for(j = bc->dp1; i++ < nd;) { - if ((dd = s0[j++] - '0' - dig)) - goto ret; - if (!b->x[0] && b->wds == 1) { - if (i < nd) - dd = 1; - goto ret; - } - b = multadd(b, 10, 0); - dig = quorem(b,d); - } - if (b->x[0] || b->wds > 1) - dd = -1; - ret: - Bfree(b); - Bfree(d); -#ifdef Honor_FLT_ROUNDS - if (bc->rounding != 1) { - if (dd < 0) { - if (bc->rounding == 0) { - if (!dsign) - goto retlow1; - } - else if (dsign) - goto rethi1; - } - else if (dd > 0) { - if (bc->rounding == 0) { - if (dsign) - goto rethi1; - goto ret1; - } - if (!dsign) - goto rethi1; - dval(rv) += 2.*sulp(rv,bc); - } - else { - bc->inexact = 0; - if (dsign) - goto rethi1; - } - } - else -#endif - if (speccase) { - if (dd <= 0) - rv->d = 0.; - } - else if (dd < 0) { - if (!dsign) /* does not happen for round-near */ -retlow1: - dval(rv) -= sulp(rv,bc); - } - else if (dd > 0) { - if (dsign) { - rethi1: - dval(rv) += sulp(rv,bc); - } - } - else { - /* Exact half-way case: apply round-even rule. */ - if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) { - i = 1 - j; - if (i <= 31) { - if (word1(rv) & (0x1 << i)) - goto odd; - } - else if (word0(rv) & (0x1 << (i-32))) - goto odd; - } - else if (word1(rv) & 1) { - odd: - if (dsign) - goto rethi1; - goto retlow1; - } - } - -#ifdef Honor_FLT_ROUNDS - ret1: -#endif - return; - } -#endif /* NO_STRTOD_BIGCOMP */ - - double -fpconv_strtod -#ifdef KR_headers - (s00, se) CONST char *s00; char **se; -#else - (const char *s00, char **se) -#endif -{ - int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1; - int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign; - CONST char *s, *s0, *s1; - double aadj, aadj1; - Long L; - U aadj2, adj, rv, rv0; - ULong y, z; - BCinfo bc; - Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; -#ifdef Avoid_Underflow - ULong Lsb, Lsb1; -#endif -#ifdef SET_INEXACT - int oldinexact; -#endif -#ifndef NO_STRTOD_BIGCOMP - int req_bigcomp = 0; -#endif -#ifdef Honor_FLT_ROUNDS /*{*/ -#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ - bc.rounding = Flt_Rounds; -#else /*}{*/ - bc.rounding = 1; - switch(fegetround()) { - case FE_TOWARDZERO: bc.rounding = 0; break; - case FE_UPWARD: bc.rounding = 2; break; - case FE_DOWNWARD: bc.rounding = 3; - } -#endif /*}}*/ -#endif /*}*/ -#ifdef USE_LOCALE - CONST char *s2; -#endif - - sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0; - dval(&rv) = 0.; - for(s = s00;;s++) switch(*s) { - case '-': - sign = 1; - /* no break */ - case '+': - if (*++s) - goto break2; - /* no break */ - case 0: - goto ret0; - case '\t': - case '\n': - case '\v': - case '\f': - case '\r': - case ' ': - continue; - default: - goto break2; - } - break2: - if (*s == '0') { -#ifndef NO_HEX_FP /*{*/ - switch(s[1]) { - case 'x': - case 'X': -#ifdef Honor_FLT_ROUNDS - gethex(&s, &rv, bc.rounding, sign); -#else - gethex(&s, &rv, 1, sign); -#endif - goto ret; - } -#endif /*}*/ - nz0 = 1; - while(*++s == '0') ; - if (!*s) - goto ret; - } - s0 = s; - y = z = 0; - for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) - if (nd < 9) - y = 10*y + c - '0'; - else if (nd < 16) - z = 10*z + c - '0'; - nd0 = nd; - bc.dp0 = bc.dp1 = s - s0; - for(s1 = s; s1 > s0 && *--s1 == '0'; ) - ++nz1; -#ifdef USE_LOCALE - s1 = localeconv()->decimal_point; - if (c == *s1) { - c = '.'; - if (*++s1) { - s2 = s; - for(;;) { - if (*++s2 != *s1) { - c = 0; - break; - } - if (!*++s1) { - s = s2; - break; - } - } - } - } -#endif - if (c == '.') { - c = *++s; - bc.dp1 = s - s0; - bc.dplen = bc.dp1 - bc.dp0; - if (!nd) { - for(; c == '0'; c = *++s) - nz++; - if (c > '0' && c <= '9') { - bc.dp0 = s0 - s; - bc.dp1 = bc.dp0 + bc.dplen; - s0 = s; - nf += nz; - nz = 0; - goto have_dig; - } - goto dig_done; - } - for(; c >= '0' && c <= '9'; c = *++s) { - have_dig: - nz++; - if (c -= '0') { - nf += nz; - for(i = 1; i < nz; i++) - if (nd++ < 9) - y *= 10; - else if (nd <= DBL_DIG + 1) - z *= 10; - if (nd++ < 9) - y = 10*y + c; - else if (nd <= DBL_DIG + 1) - z = 10*z + c; - nz = nz1 = 0; - } - } - } - dig_done: - e = 0; - if (c == 'e' || c == 'E') { - if (!nd && !nz && !nz0) { - goto ret0; - } - s00 = s; - esign = 0; - switch(c = *++s) { - case '-': - esign = 1; - case '+': - c = *++s; - } - if (c >= '0' && c <= '9') { - while(c == '0') - c = *++s; - if (c > '0' && c <= '9') { - L = c - '0'; - s1 = s; - while((c = *++s) >= '0' && c <= '9') - L = 10*L + c - '0'; - if (s - s1 > 8 || L > 19999) - /* Avoid confusion from exponents - * so large that e might overflow. - */ - e = 19999; /* safe for 16 bit ints */ - else - e = (int)L; - if (esign) - e = -e; - } - else - e = 0; - } - else - s = s00; - } - if (!nd) { - if (!nz && !nz0) { -#ifdef INFNAN_CHECK - /* Check for Nan and Infinity */ - if (!bc.dplen) - switch(c) { - case 'i': - case 'I': - if (match(&s,"nf")) { - --s; - if (!match(&s,"inity")) - ++s; - word0(&rv) = 0x7ff00000; - word1(&rv) = 0; - goto ret; - } - break; - case 'n': - case 'N': - if (match(&s, "an")) { - word0(&rv) = NAN_WORD0; - word1(&rv) = NAN_WORD1; -#ifndef No_Hex_NaN - if (*s == '(') /*)*/ - hexnan(&rv, &s); -#endif - goto ret; - } - } -#endif /* INFNAN_CHECK */ - ret0: - s = s00; - sign = 0; - } - goto ret; - } - bc.e0 = e1 = e -= nf; - - /* Now we have nd0 digits, starting at s0, followed by a - * decimal point, followed by nd-nd0 digits. The number we're - * after is the integer represented by those digits times - * 10**e */ - - if (!nd0) - nd0 = nd; - k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; - dval(&rv) = y; - if (k > 9) { -#ifdef SET_INEXACT - if (k > DBL_DIG) - oldinexact = get_inexact(); -#endif - dval(&rv) = tens[k - 9] * dval(&rv) + z; - } - bd0 = 0; - if (nd <= DBL_DIG -#ifndef RND_PRODQUOT -#ifndef Honor_FLT_ROUNDS - && Flt_Rounds == 1 -#endif -#endif - ) { - if (!e) - goto ret; -#ifndef ROUND_BIASED_without_Round_Up - if (e > 0) { - if (e <= Ten_pmax) { -#ifdef VAX - goto vax_ovfl_check; -#else -#ifdef Honor_FLT_ROUNDS - /* round correctly FLT_ROUNDS = 2 or 3 */ - if (sign) { - rv.d = -rv.d; - sign = 0; - } -#endif - /* rv = */ rounded_product(dval(&rv), tens[e]); - goto ret; -#endif - } - i = DBL_DIG - nd; - if (e <= Ten_pmax + i) { - /* A fancier test would sometimes let us do - * this for larger i values. - */ -#ifdef Honor_FLT_ROUNDS - /* round correctly FLT_ROUNDS = 2 or 3 */ - if (sign) { - rv.d = -rv.d; - sign = 0; - } -#endif - e -= i; - dval(&rv) *= tens[i]; -#ifdef VAX - /* VAX exponent range is so narrow we must - * worry about overflow here... - */ - vax_ovfl_check: - word0(&rv) -= P*Exp_msk1; - /* rv = */ rounded_product(dval(&rv), tens[e]); - if ((word0(&rv) & Exp_mask) - > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) - goto ovfl; - word0(&rv) += P*Exp_msk1; -#else - /* rv = */ rounded_product(dval(&rv), tens[e]); -#endif - goto ret; - } - } -#ifndef Inaccurate_Divide - else if (e >= -Ten_pmax) { -#ifdef Honor_FLT_ROUNDS - /* round correctly FLT_ROUNDS = 2 or 3 */ - if (sign) { - rv.d = -rv.d; - sign = 0; - } -#endif - /* rv = */ rounded_quotient(dval(&rv), tens[-e]); - goto ret; - } -#endif -#endif /* ROUND_BIASED_without_Round_Up */ - } - e1 += nd - k; - -#ifdef IEEE_Arith -#ifdef SET_INEXACT - bc.inexact = 1; - if (k <= DBL_DIG) - oldinexact = get_inexact(); -#endif -#ifdef Avoid_Underflow - bc.scale = 0; -#endif -#ifdef Honor_FLT_ROUNDS - if (bc.rounding >= 2) { - if (sign) - bc.rounding = bc.rounding == 2 ? 0 : 2; - else - if (bc.rounding != 2) - bc.rounding = 0; - } -#endif -#endif /*IEEE_Arith*/ - - /* Get starting approximation = rv * 10**e1 */ - - if (e1 > 0) { - if ((i = e1 & 15)) - dval(&rv) *= tens[i]; - if (e1 &= ~15) { - if (e1 > DBL_MAX_10_EXP) { - ovfl: - /* Can't trust HUGE_VAL */ -#ifdef IEEE_Arith -#ifdef Honor_FLT_ROUNDS - switch(bc.rounding) { - case 0: /* toward 0 */ - case 3: /* toward -infinity */ - word0(&rv) = Big0; - word1(&rv) = Big1; - break; - default: - word0(&rv) = Exp_mask; - word1(&rv) = 0; - } -#else /*Honor_FLT_ROUNDS*/ - word0(&rv) = Exp_mask; - word1(&rv) = 0; -#endif /*Honor_FLT_ROUNDS*/ -#ifdef SET_INEXACT - /* set overflow bit */ - dval(&rv0) = 1e300; - dval(&rv0) *= dval(&rv0); -#endif -#else /*IEEE_Arith*/ - word0(&rv) = Big0; - word1(&rv) = Big1; -#endif /*IEEE_Arith*/ - range_err: - if (bd0) { - Bfree(bb); - Bfree(bd); - Bfree(bs); - Bfree(bd0); - Bfree(delta); - } -#ifndef NO_ERRNO - errno = ERANGE; -#endif - goto ret; - } - e1 >>= 4; - for(j = 0; e1 > 1; j++, e1 >>= 1) - if (e1 & 1) - dval(&rv) *= bigtens[j]; - /* The last multiplication could overflow. */ - word0(&rv) -= P*Exp_msk1; - dval(&rv) *= bigtens[j]; - if ((z = word0(&rv) & Exp_mask) - > Exp_msk1*(DBL_MAX_EXP+Bias-P)) - goto ovfl; - if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { - /* set to largest number */ - /* (Can't trust DBL_MAX) */ - word0(&rv) = Big0; - word1(&rv) = Big1; - } - else - word0(&rv) += P*Exp_msk1; - } - } - else if (e1 < 0) { - e1 = -e1; - if ((i = e1 & 15)) - dval(&rv) /= tens[i]; - if (e1 >>= 4) { - if (e1 >= 1 << n_bigtens) - goto undfl; -#ifdef Avoid_Underflow - if (e1 & Scale_Bit) - bc.scale = 2*P; - for(j = 0; e1 > 0; j++, e1 >>= 1) - if (e1 & 1) - dval(&rv) *= tinytens[j]; - if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask) - >> Exp_shift)) > 0) { - /* scaled rv is denormal; clear j low bits */ - if (j >= 32) { - if (j > 54) - goto undfl; - word1(&rv) = 0; - if (j >= 53) - word0(&rv) = (P+2)*Exp_msk1; - else - word0(&rv) &= 0xffffffff << (j-32); - } - else - word1(&rv) &= 0xffffffff << j; - } -#else - for(j = 0; e1 > 1; j++, e1 >>= 1) - if (e1 & 1) - dval(&rv) *= tinytens[j]; - /* The last multiplication could underflow. */ - dval(&rv0) = dval(&rv); - dval(&rv) *= tinytens[j]; - if (!dval(&rv)) { - dval(&rv) = 2.*dval(&rv0); - dval(&rv) *= tinytens[j]; -#endif - if (!dval(&rv)) { - undfl: - dval(&rv) = 0.; - goto range_err; - } -#ifndef Avoid_Underflow - word0(&rv) = Tiny0; - word1(&rv) = Tiny1; - /* The refinement below will clean - * this approximation up. - */ - } -#endif - } - } - - /* Now the hard part -- adjusting rv to the correct value.*/ - - /* Put digits into bd: true value = bd * 10^e */ - - bc.nd = nd - nz1; -#ifndef NO_STRTOD_BIGCOMP - bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */ - /* to silence an erroneous warning about bc.nd0 */ - /* possibly not being initialized. */ - if (nd > strtod_diglim) { - /* ASSERT(strtod_diglim >= 18); 18 == one more than the */ - /* minimum number of decimal digits to distinguish double values */ - /* in IEEE arithmetic. */ - i = j = 18; - if (i > nd0) - j += bc.dplen; - for(;;) { - if (--j < bc.dp1 && j >= bc.dp0) - j = bc.dp0 - 1; - if (s0[j] != '0') - break; - --i; - } - e += nd - i; - nd = i; - if (nd0 > nd) - nd0 = nd; - if (nd < 9) { /* must recompute y */ - y = 0; - for(i = 0; i < nd0; ++i) - y = 10*y + s0[i] - '0'; - for(j = bc.dp1; i < nd; ++i) - y = 10*y + s0[j++] - '0'; - } - } -#endif - bd0 = s2b(s0, nd0, nd, y, bc.dplen); - - for(;;) { - bd = Balloc(bd0->k); - Bcopy(bd, bd0); - bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ - bs = i2b(1); - - if (e >= 0) { - bb2 = bb5 = 0; - bd2 = bd5 = e; - } - else { - bb2 = bb5 = -e; - bd2 = bd5 = 0; - } - if (bbe >= 0) - bb2 += bbe; - else - bd2 -= bbe; - bs2 = bb2; -#ifdef Honor_FLT_ROUNDS - if (bc.rounding != 1) - bs2++; -#endif -#ifdef Avoid_Underflow - Lsb = LSB; - Lsb1 = 0; - j = bbe - bc.scale; - i = j + bbbits - 1; /* logb(rv) */ - j = P + 1 - bbbits; - if (i < Emin) { /* denormal */ - i = Emin - i; - j -= i; - if (i < 32) - Lsb <<= i; - else if (i < 52) - Lsb1 = Lsb << (i-32); - else - Lsb1 = Exp_mask; - } -#else /*Avoid_Underflow*/ -#ifdef Sudden_Underflow -#ifdef IBM - j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); -#else - j = P + 1 - bbbits; -#endif -#else /*Sudden_Underflow*/ - j = bbe; - i = j + bbbits - 1; /* logb(rv) */ - if (i < Emin) /* denormal */ - j += P - Emin; - else - j = P + 1 - bbbits; -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow*/ - bb2 += j; - bd2 += j; -#ifdef Avoid_Underflow - bd2 += bc.scale; -#endif - i = bb2 < bd2 ? bb2 : bd2; - if (i > bs2) - i = bs2; - if (i > 0) { - bb2 -= i; - bd2 -= i; - bs2 -= i; - } - if (bb5 > 0) { - bs = pow5mult(bs, bb5); - bb1 = mult(bs, bb); - Bfree(bb); - bb = bb1; - } - if (bb2 > 0) - bb = lshift(bb, bb2); - if (bd5 > 0) - bd = pow5mult(bd, bd5); - if (bd2 > 0) - bd = lshift(bd, bd2); - if (bs2 > 0) - bs = lshift(bs, bs2); - delta = diff(bb, bd); - bc.dsign = delta->sign; - delta->sign = 0; - i = cmp(delta, bs); -#ifndef NO_STRTOD_BIGCOMP /*{*/ - if (bc.nd > nd && i <= 0) { - if (bc.dsign) { - /* Must use bigcomp(). */ - req_bigcomp = 1; - break; - } -#ifdef Honor_FLT_ROUNDS - if (bc.rounding != 1) { - if (i < 0) { - req_bigcomp = 1; - break; - } - } - else -#endif - i = -1; /* Discarded digits make delta smaller. */ - } -#endif /*}*/ -#ifdef Honor_FLT_ROUNDS /*{*/ - if (bc.rounding != 1) { - if (i < 0) { - /* Error is less than an ulp */ - if (!delta->x[0] && delta->wds <= 1) { - /* exact */ -#ifdef SET_INEXACT - bc.inexact = 0; -#endif - break; - } - if (bc.rounding) { - if (bc.dsign) { - adj.d = 1.; - goto apply_adj; - } - } - else if (!bc.dsign) { - adj.d = -1.; - if (!word1(&rv) - && !(word0(&rv) & Frac_mask)) { - y = word0(&rv) & Exp_mask; -#ifdef Avoid_Underflow - if (!bc.scale || y > 2*P*Exp_msk1) -#else - if (y) -#endif - { - delta = lshift(delta,Log2P); - if (cmp(delta, bs) <= 0) - adj.d = -0.5; - } - } - apply_adj: -#ifdef Avoid_Underflow /*{*/ - if (bc.scale && (y = word0(&rv) & Exp_mask) - <= 2*P*Exp_msk1) - word0(&adj) += (2*P+1)*Exp_msk1 - y; -#else -#ifdef Sudden_Underflow - if ((word0(&rv) & Exp_mask) <= - P*Exp_msk1) { - word0(&rv) += P*Exp_msk1; - dval(&rv) += adj.d*ulp(dval(&rv)); - word0(&rv) -= P*Exp_msk1; - } - else -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow}*/ - dval(&rv) += adj.d*ulp(&rv); - } - break; - } - adj.d = ratio(delta, bs); - if (adj.d < 1.) - adj.d = 1.; - if (adj.d <= 0x7ffffffe) { - /* adj = rounding ? ceil(adj) : floor(adj); */ - y = adj.d; - if (y != adj.d) { - if (!((bc.rounding>>1) ^ bc.dsign)) - y++; - adj.d = y; - } - } -#ifdef Avoid_Underflow /*{*/ - if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) - word0(&adj) += (2*P+1)*Exp_msk1 - y; -#else -#ifdef Sudden_Underflow - if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) { - word0(&rv) += P*Exp_msk1; - adj.d *= ulp(dval(&rv)); - if (bc.dsign) - dval(&rv) += adj.d; - else - dval(&rv) -= adj.d; - word0(&rv) -= P*Exp_msk1; - goto cont; - } -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow}*/ - adj.d *= ulp(&rv); - if (bc.dsign) { - if (word0(&rv) == Big0 && word1(&rv) == Big1) - goto ovfl; - dval(&rv) += adj.d; - } - else - dval(&rv) -= adj.d; - goto cont; - } -#endif /*}Honor_FLT_ROUNDS*/ - - if (i < 0) { - /* Error is less than half an ulp -- check for - * special case of mantissa a power of two. - */ - if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask -#ifdef IEEE_Arith /*{*/ -#ifdef Avoid_Underflow - || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1 -#else - || (word0(&rv) & Exp_mask) <= Exp_msk1 -#endif -#endif /*}*/ - ) { -#ifdef SET_INEXACT - if (!delta->x[0] && delta->wds <= 1) - bc.inexact = 0; -#endif - break; - } - if (!delta->x[0] && delta->wds <= 1) { - /* exact result */ -#ifdef SET_INEXACT - bc.inexact = 0; -#endif - break; - } - delta = lshift(delta,Log2P); - if (cmp(delta, bs) > 0) - goto drop_down; - break; - } - if (i == 0) { - /* exactly half-way between */ - if (bc.dsign) { - if ((word0(&rv) & Bndry_mask1) == Bndry_mask1 - && word1(&rv) == ( -#ifdef Avoid_Underflow - (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) - ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : -#endif - 0xffffffff)) { - /*boundary case -- increment exponent*/ - if (word0(&rv) == Big0 && word1(&rv) == Big1) - goto ovfl; - word0(&rv) = (word0(&rv) & Exp_mask) - + Exp_msk1 -#ifdef IBM - | Exp_msk1 >> 4 -#endif - ; - word1(&rv) = 0; -#ifdef Avoid_Underflow - bc.dsign = 0; -#endif - break; - } - } - else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) { - drop_down: - /* boundary case -- decrement exponent */ -#ifdef Sudden_Underflow /*{{*/ - L = word0(&rv) & Exp_mask; -#ifdef IBM - if (L < Exp_msk1) -#else -#ifdef Avoid_Underflow - if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1)) -#else - if (L <= Exp_msk1) -#endif /*Avoid_Underflow*/ -#endif /*IBM*/ - { - if (bc.nd >nd) { - bc.uflchk = 1; - break; - } - goto undfl; - } - L -= Exp_msk1; -#else /*Sudden_Underflow}{*/ -#ifdef Avoid_Underflow - if (bc.scale) { - L = word0(&rv) & Exp_mask; - if (L <= (2*P+1)*Exp_msk1) { - if (L > (P+2)*Exp_msk1) - /* round even ==> */ - /* accept rv */ - break; - /* rv = smallest denormal */ - if (bc.nd >nd) { - bc.uflchk = 1; - break; - } - goto undfl; - } - } -#endif /*Avoid_Underflow*/ - L = (word0(&rv) & Exp_mask) - Exp_msk1; -#endif /*Sudden_Underflow}}*/ - word0(&rv) = L | Bndry_mask1; - word1(&rv) = 0xffffffff; -#ifdef IBM - goto cont; -#else -#ifndef NO_STRTOD_BIGCOMP - if (bc.nd > nd) - goto cont; -#endif - break; -#endif - } -#ifndef ROUND_BIASED -#ifdef Avoid_Underflow - if (Lsb1) { - if (!(word0(&rv) & Lsb1)) - break; - } - else if (!(word1(&rv) & Lsb)) - break; -#else - if (!(word1(&rv) & LSB)) - break; -#endif -#endif - if (bc.dsign) -#ifdef Avoid_Underflow - dval(&rv) += sulp(&rv, &bc); -#else - dval(&rv) += ulp(&rv); -#endif -#ifndef ROUND_BIASED - else { -#ifdef Avoid_Underflow - dval(&rv) -= sulp(&rv, &bc); -#else - dval(&rv) -= ulp(&rv); -#endif -#ifndef Sudden_Underflow - if (!dval(&rv)) { - if (bc.nd >nd) { - bc.uflchk = 1; - break; - } - goto undfl; - } -#endif - } -#ifdef Avoid_Underflow - bc.dsign = 1 - bc.dsign; -#endif -#endif - break; - } - if ((aadj = ratio(delta, bs)) <= 2.) { - if (bc.dsign) - aadj = aadj1 = 1.; - else if (word1(&rv) || word0(&rv) & Bndry_mask) { -#ifndef Sudden_Underflow - if (word1(&rv) == Tiny1 && !word0(&rv)) { - if (bc.nd >nd) { - bc.uflchk = 1; - break; - } - goto undfl; - } -#endif - aadj = 1.; - aadj1 = -1.; - } - else { - /* special case -- power of FLT_RADIX to be */ - /* rounded down... */ - - if (aadj < 2./FLT_RADIX) - aadj = 1./FLT_RADIX; - else - aadj *= 0.5; - aadj1 = -aadj; - } - } - else { - aadj *= 0.5; - aadj1 = bc.dsign ? aadj : -aadj; -#ifdef Check_FLT_ROUNDS - switch(bc.rounding) { - case 2: /* towards +infinity */ - aadj1 -= 0.5; - break; - case 0: /* towards 0 */ - case 3: /* towards -infinity */ - aadj1 += 0.5; - } -#else - if (Flt_Rounds == 0) - aadj1 += 0.5; -#endif /*Check_FLT_ROUNDS*/ - } - y = word0(&rv) & Exp_mask; - - /* Check for overflow */ - - if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { - dval(&rv0) = dval(&rv); - word0(&rv) -= P*Exp_msk1; - adj.d = aadj1 * ulp(&rv); - dval(&rv) += adj.d; - if ((word0(&rv) & Exp_mask) >= - Exp_msk1*(DBL_MAX_EXP+Bias-P)) { - if (word0(&rv0) == Big0 && word1(&rv0) == Big1) - goto ovfl; - word0(&rv) = Big0; - word1(&rv) = Big1; - goto cont; - } - else - word0(&rv) += P*Exp_msk1; - } - else { -#ifdef Avoid_Underflow - if (bc.scale && y <= 2*P*Exp_msk1) { - if (aadj <= 0x7fffffff) { - if ((z = aadj) <= 0) - z = 1; - aadj = z; - aadj1 = bc.dsign ? aadj : -aadj; - } - dval(&aadj2) = aadj1; - word0(&aadj2) += (2*P+1)*Exp_msk1 - y; - aadj1 = dval(&aadj2); - adj.d = aadj1 * ulp(&rv); - dval(&rv) += adj.d; - if (rv.d == 0.) -#ifdef NO_STRTOD_BIGCOMP - goto undfl; -#else - { - if (bc.nd > nd) - bc.dsign = 1; - break; - } -#endif - } - else { - adj.d = aadj1 * ulp(&rv); - dval(&rv) += adj.d; - } -#else -#ifdef Sudden_Underflow - if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) { - dval(&rv0) = dval(&rv); - word0(&rv) += P*Exp_msk1; - adj.d = aadj1 * ulp(&rv); - dval(&rv) += adj.d; -#ifdef IBM - if ((word0(&rv) & Exp_mask) < P*Exp_msk1) -#else - if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) -#endif - { - if (word0(&rv0) == Tiny0 - && word1(&rv0) == Tiny1) { - if (bc.nd >nd) { - bc.uflchk = 1; - break; - } - goto undfl; - } - word0(&rv) = Tiny0; - word1(&rv) = Tiny1; - goto cont; - } - else - word0(&rv) -= P*Exp_msk1; - } - else { - adj.d = aadj1 * ulp(&rv); - dval(&rv) += adj.d; - } -#else /*Sudden_Underflow*/ - /* Compute adj so that the IEEE rounding rules will - * correctly round rv + adj in some half-way cases. - * If rv * ulp(rv) is denormalized (i.e., - * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid - * trouble from bits lost to denormalization; - * example: 1.2e-307 . - */ - if (y <= (P-1)*Exp_msk1 && aadj > 1.) { - aadj1 = (double)(int)(aadj + 0.5); - if (!bc.dsign) - aadj1 = -aadj1; - } - adj.d = aadj1 * ulp(&rv); - dval(&rv) += adj.d; -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow*/ - } - z = word0(&rv) & Exp_mask; -#ifndef SET_INEXACT - if (bc.nd == nd) { -#ifdef Avoid_Underflow - if (!bc.scale) -#endif - if (y == z) { - /* Can we stop now? */ - L = (Long)aadj; - aadj -= L; - /* The tolerances below are conservative. */ - if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) { - if (aadj < .4999999 || aadj > .5000001) - break; - } - else if (aadj < .4999999/FLT_RADIX) - break; - } - } -#endif - cont: - Bfree(bb); - Bfree(bd); - Bfree(bs); - Bfree(delta); - } - Bfree(bb); - Bfree(bd); - Bfree(bs); - Bfree(bd0); - Bfree(delta); -#ifndef NO_STRTOD_BIGCOMP - if (req_bigcomp) { - bd0 = 0; - bc.e0 += nz1; - bigcomp(&rv, s0, &bc); - y = word0(&rv) & Exp_mask; - if (y == Exp_mask) - goto ovfl; - if (y == 0 && rv.d == 0.) - goto undfl; - } -#endif -#ifdef SET_INEXACT - if (bc.inexact) { - if (!oldinexact) { - word0(&rv0) = Exp_1 + (70 << Exp_shift); - word1(&rv0) = 0; - dval(&rv0) += 1.; - } - } - else if (!oldinexact) - clear_inexact(); -#endif -#ifdef Avoid_Underflow - if (bc.scale) { - word0(&rv0) = Exp_1 - 2*P*Exp_msk1; - word1(&rv0) = 0; - dval(&rv) *= dval(&rv0); -#ifndef NO_ERRNO - /* try to avoid the bug of testing an 8087 register value */ -#ifdef IEEE_Arith - if (!(word0(&rv) & Exp_mask)) -#else - if (word0(&rv) == 0 && word1(&rv) == 0) -#endif - errno = ERANGE; -#endif - } -#endif /* Avoid_Underflow */ -#ifdef SET_INEXACT - if (bc.inexact && !(word0(&rv) & Exp_mask)) { - /* set underflow bit */ - dval(&rv0) = 1e-300; - dval(&rv0) *= dval(&rv0); - } -#endif - ret: - if (se) - *se = (char *)s; - return sign ? -dval(&rv) : dval(&rv); - } - -#ifndef MULTIPLE_THREADS - static char *dtoa_result; -#endif - - static char * -#ifdef KR_headers -rv_alloc(i) int i; -#else -rv_alloc(int i) -#endif -{ - int j, k, *r; - - j = sizeof(ULong); - for(k = 0; - sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i; - j <<= 1) - k++; - r = (int*)Balloc(k); - *r = k; - return -#ifndef MULTIPLE_THREADS - dtoa_result = -#endif - (char *)(r+1); - } - - static char * -#ifdef KR_headers -nrv_alloc(s, rve, n) char *s, **rve; int n; -#else -nrv_alloc(const char *s, char **rve, int n) -#endif -{ - char *rv, *t; - - t = rv = rv_alloc(n); - while((*t = *s++)) t++; - if (rve) - *rve = t; - return rv; - } - -/* freedtoa(s) must be used to free values s returned by dtoa - * when MULTIPLE_THREADS is #defined. It should be used in all cases, - * but for consistency with earlier versions of dtoa, it is optional - * when MULTIPLE_THREADS is not defined. - */ - - void -#ifdef KR_headers -freedtoa(s) char *s; -#else -freedtoa(char *s) -#endif -{ - Bigint *b = (Bigint *)((int *)s - 1); - b->maxwds = 1 << (b->k = *(int*)b); - Bfree(b); -#ifndef MULTIPLE_THREADS - if (s == dtoa_result) - dtoa_result = 0; -#endif - } - -/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. - * - * Inspired by "How to Print Floating-Point Numbers Accurately" by - * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. - * - * Modifications: - * 1. Rather than iterating, we use a simple numeric overestimate - * to determine k = floor(log10(d)). We scale relevant - * quantities using O(log2(k)) rather than O(k) multiplications. - * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't - * try to generate digits strictly left to right. Instead, we - * compute with fewer bits and propagate the carry if necessary - * when rounding the final digit up. This is often faster. - * 3. Under the assumption that input will be rounded nearest, - * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. - * That is, we allow equality in stopping tests when the - * round-nearest rule will give the same floating-point value - * as would satisfaction of the stopping test with strict - * inequality. - * 4. We remove common factors of powers of 2 from relevant - * quantities. - * 5. When converting floating-point integers less than 1e16, - * we use floating-point arithmetic rather than resorting - * to multiple-precision integers. - * 6. When asked to produce fewer than 15 digits, we first try - * to get by with floating-point arithmetic; we resort to - * multiple-precision integer arithmetic only if we cannot - * guarantee that the floating-point calculation has given - * the correctly rounded result. For k requested digits and - * "uniformly" distributed input, the probability is - * something like 10^(k-15) that we must resort to the Long - * calculation. - */ - - char * -dtoa -#ifdef KR_headers - (dd, mode, ndigits, decpt, sign, rve) - double dd; int mode, ndigits, *decpt, *sign; char **rve; -#else - (double dd, int mode, int ndigits, int *decpt, int *sign, char **rve) -#endif -{ - /* Arguments ndigits, decpt, sign are similar to those - of ecvt and fcvt; trailing zeros are suppressed from - the returned string. If not null, *rve is set to point - to the end of the return value. If d is +-Infinity or NaN, - then *decpt is set to 9999. - - mode: - 0 ==> shortest string that yields d when read in - and rounded to nearest. - 1 ==> like 0, but with Steele & White stopping rule; - e.g. with IEEE P754 arithmetic , mode 0 gives - 1e23 whereas mode 1 gives 9.999999999999999e22. - 2 ==> max(1,ndigits) significant digits. This gives a - return value similar to that of ecvt, except - that trailing zeros are suppressed. - 3 ==> through ndigits past the decimal point. This - gives a return value similar to that from fcvt, - except that trailing zeros are suppressed, and - ndigits can be negative. - 4,5 ==> similar to 2 and 3, respectively, but (in - round-nearest mode) with the tests of mode 0 to - possibly return a shorter string that rounds to d. - With IEEE arithmetic and compilation with - -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same - as modes 2 and 3 when FLT_ROUNDS != 1. - 6-9 ==> Debugging modes similar to mode - 4: don't try - fast floating-point estimate (if applicable). - - Values of mode other than 0-9 are treated as mode 0. - - Sufficient space is allocated to the return value - to hold the suppressed trailing zeros. - */ - - int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, - j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, - spec_case, try_quick; - Long L; -#ifndef Sudden_Underflow - int denorm; - ULong x; -#endif - Bigint *b, *b1, *delta, *mlo, *mhi, *S; - U d2, eps, u; - double ds; - char *s, *s0; -#ifndef No_leftright -#ifdef IEEE_Arith - U eps1; -#endif -#endif -#ifdef SET_INEXACT - int inexact, oldinexact; -#endif -#ifdef Honor_FLT_ROUNDS /*{*/ - int Rounding; -#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ - Rounding = Flt_Rounds; -#else /*}{*/ - Rounding = 1; - switch(fegetround()) { - case FE_TOWARDZERO: Rounding = 0; break; - case FE_UPWARD: Rounding = 2; break; - case FE_DOWNWARD: Rounding = 3; - } -#endif /*}}*/ -#endif /*}*/ - -#ifndef MULTIPLE_THREADS - if (dtoa_result) { - freedtoa(dtoa_result); - dtoa_result = 0; - } -#endif - - u.d = dd; - if (word0(&u) & Sign_bit) { - /* set sign for everything, including 0's and NaNs */ - *sign = 1; - word0(&u) &= ~Sign_bit; /* clear sign bit */ - } - else - *sign = 0; - -#if defined(IEEE_Arith) + defined(VAX) -#ifdef IEEE_Arith - if ((word0(&u) & Exp_mask) == Exp_mask) -#else - if (word0(&u) == 0x8000) -#endif - { - /* Infinity or NaN */ - *decpt = 9999; -#ifdef IEEE_Arith - if (!word1(&u) && !(word0(&u) & 0xfffff)) - return nrv_alloc("inf", rve, 8); -#endif - return nrv_alloc("nan", rve, 3); - } -#endif -#ifdef IBM - dval(&u) += 0; /* normalize */ -#endif - if (!dval(&u)) { - *decpt = 1; - return nrv_alloc("0", rve, 1); - } - -#ifdef SET_INEXACT - try_quick = oldinexact = get_inexact(); - inexact = 1; -#endif -#ifdef Honor_FLT_ROUNDS - if (Rounding >= 2) { - if (*sign) - Rounding = Rounding == 2 ? 0 : 2; - else - if (Rounding != 2) - Rounding = 0; - } -#endif - - b = d2b(&u, &be, &bbits); -#ifdef Sudden_Underflow - i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); -#else - if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) { -#endif - dval(&d2) = dval(&u); - word0(&d2) &= Frac_mask1; - word0(&d2) |= Exp_11; -#ifdef IBM - if (j = 11 - hi0bits(word0(&d2) & Frac_mask)) - dval(&d2) /= 1 << j; -#endif - - /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 - * log10(x) = log(x) / log(10) - * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) - * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) - * - * This suggests computing an approximation k to log10(d) by - * - * k = (i - Bias)*0.301029995663981 - * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); - * - * We want k to be too large rather than too small. - * The error in the first-order Taylor series approximation - * is in our favor, so we just round up the constant enough - * to compensate for any error in the multiplication of - * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, - * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, - * adding 1e-13 to the constant term more than suffices. - * Hence we adjust the constant term to 0.1760912590558. - * (We could get a more accurate k by invoking log10, - * but this is probably not worthwhile.) - */ - - i -= Bias; -#ifdef IBM - i <<= 2; - i += j; -#endif -#ifndef Sudden_Underflow - denorm = 0; - } - else { - /* d is denormalized */ - - i = bbits + be + (Bias + (P-1) - 1); - x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32) - : word1(&u) << (32 - i); - dval(&d2) = x; - word0(&d2) -= 31*Exp_msk1; /* adjust exponent */ - i -= (Bias + (P-1) - 1) + 1; - denorm = 1; - } -#endif - ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; - k = (int)ds; - if (ds < 0. && ds != k) - k--; /* want k = floor(ds) */ - k_check = 1; - if (k >= 0 && k <= Ten_pmax) { - if (dval(&u) < tens[k]) - k--; - k_check = 0; - } - j = bbits - i - 1; - if (j >= 0) { - b2 = 0; - s2 = j; - } - else { - b2 = -j; - s2 = 0; - } - if (k >= 0) { - b5 = 0; - s5 = k; - s2 += k; - } - else { - b2 -= k; - b5 = -k; - s5 = 0; - } - if (mode < 0 || mode > 9) - mode = 0; - -#ifndef SET_INEXACT -#ifdef Check_FLT_ROUNDS - try_quick = Rounding == 1; -#else - try_quick = 1; -#endif -#endif /*SET_INEXACT*/ - - if (mode > 5) { - mode -= 4; - try_quick = 0; - } - leftright = 1; - ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */ - /* silence erroneous "gcc -Wall" warning. */ - switch(mode) { - case 0: - case 1: - i = 18; - ndigits = 0; - break; - case 2: - leftright = 0; - /* no break */ - case 4: - if (ndigits <= 0) - ndigits = 1; - ilim = ilim1 = i = ndigits; - break; - case 3: - leftright = 0; - /* no break */ - case 5: - i = ndigits + k + 1; - ilim = i; - ilim1 = i - 1; - if (i <= 0) - i = 1; - } - s = s0 = rv_alloc(i); - -#ifdef Honor_FLT_ROUNDS - if (mode > 1 && Rounding != 1) - leftright = 0; -#endif - - if (ilim >= 0 && ilim <= Quick_max && try_quick) { - - /* Try to get by with floating-point arithmetic. */ - - i = 0; - dval(&d2) = dval(&u); - k0 = k; - ilim0 = ilim; - ieps = 2; /* conservative */ - if (k > 0) { - ds = tens[k&0xf]; - j = k >> 4; - if (j & Bletch) { - /* prevent overflows */ - j &= Bletch - 1; - dval(&u) /= bigtens[n_bigtens-1]; - ieps++; - } - for(; j; j >>= 1, i++) - if (j & 1) { - ieps++; - ds *= bigtens[i]; - } - dval(&u) /= ds; - } - else if ((j1 = -k)) { - dval(&u) *= tens[j1 & 0xf]; - for(j = j1 >> 4; j; j >>= 1, i++) - if (j & 1) { - ieps++; - dval(&u) *= bigtens[i]; - } - } - if (k_check && dval(&u) < 1. && ilim > 0) { - if (ilim1 <= 0) - goto fast_failed; - ilim = ilim1; - k--; - dval(&u) *= 10.; - ieps++; - } - dval(&eps) = ieps*dval(&u) + 7.; - word0(&eps) -= (P-1)*Exp_msk1; - if (ilim == 0) { - S = mhi = 0; - dval(&u) -= 5.; - if (dval(&u) > dval(&eps)) - goto one_digit; - if (dval(&u) < -dval(&eps)) - goto no_digits; - goto fast_failed; - } -#ifndef No_leftright - if (leftright) { - /* Use Steele & White method of only - * generating digits needed. - */ - dval(&eps) = 0.5/tens[ilim-1] - dval(&eps); -#ifdef IEEE_Arith - if (k0 < 0 && j1 >= 307) { - eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */ - word0(&eps1) -= Exp_msk1 * (Bias+P-1); - dval(&eps1) *= tens[j1 & 0xf]; - for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++) - if (j & 1) - dval(&eps1) *= bigtens[i]; - if (eps.d < eps1.d) - eps.d = eps1.d; - } -#endif - for(i = 0;;) { - L = dval(&u); - dval(&u) -= L; - *s++ = '0' + (int)L; - if (1. - dval(&u) < dval(&eps)) - goto bump_up; - if (dval(&u) < dval(&eps)) - goto ret1; - if (++i >= ilim) - break; - dval(&eps) *= 10.; - dval(&u) *= 10.; - } - } - else { -#endif - /* Generate ilim digits, then fix them up. */ - dval(&eps) *= tens[ilim-1]; - for(i = 1;; i++, dval(&u) *= 10.) { - L = (Long)(dval(&u)); - if (!(dval(&u) -= L)) - ilim = i; - *s++ = '0' + (int)L; - if (i == ilim) { - if (dval(&u) > 0.5 + dval(&eps)) - goto bump_up; - else if (dval(&u) < 0.5 - dval(&eps)) { - while(*--s == '0'); - s++; - goto ret1; - } - break; - } - } -#ifndef No_leftright - } -#endif - fast_failed: - s = s0; - dval(&u) = dval(&d2); - k = k0; - ilim = ilim0; - } - - /* Do we have a "small" integer? */ - - if (be >= 0 && k <= Int_max) { - /* Yes. */ - ds = tens[k]; - if (ndigits < 0 && ilim <= 0) { - S = mhi = 0; - if (ilim < 0 || dval(&u) <= 5*ds) - goto no_digits; - goto one_digit; - } - for(i = 1;; i++, dval(&u) *= 10.) { - L = (Long)(dval(&u) / ds); - dval(&u) -= L*ds; -#ifdef Check_FLT_ROUNDS - /* If FLT_ROUNDS == 2, L will usually be high by 1 */ - if (dval(&u) < 0) { - L--; - dval(&u) += ds; - } -#endif - *s++ = '0' + (int)L; - if (!dval(&u)) { -#ifdef SET_INEXACT - inexact = 0; -#endif - break; - } - if (i == ilim) { -#ifdef Honor_FLT_ROUNDS - if (mode > 1) - switch(Rounding) { - case 0: goto ret1; - case 2: goto bump_up; - } -#endif - dval(&u) += dval(&u); -#ifdef ROUND_BIASED - if (dval(&u) >= ds) -#else - if (dval(&u) > ds || (dval(&u) == ds && L & 1)) -#endif - { - bump_up: - while(*--s == '9') - if (s == s0) { - k++; - *s = '0'; - break; - } - ++*s++; - } - break; - } - } - goto ret1; - } - - m2 = b2; - m5 = b5; - mhi = mlo = 0; - if (leftright) { - i = -#ifndef Sudden_Underflow - denorm ? be + (Bias + (P-1) - 1 + 1) : -#endif -#ifdef IBM - 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); -#else - 1 + P - bbits; -#endif - b2 += i; - s2 += i; - mhi = i2b(1); - } - if (m2 > 0 && s2 > 0) { - i = m2 < s2 ? m2 : s2; - b2 -= i; - m2 -= i; - s2 -= i; - } - if (b5 > 0) { - if (leftright) { - if (m5 > 0) { - mhi = pow5mult(mhi, m5); - b1 = mult(mhi, b); - Bfree(b); - b = b1; - } - if ((j = b5 - m5)) - b = pow5mult(b, j); - } - else - b = pow5mult(b, b5); - } - S = i2b(1); - if (s5 > 0) - S = pow5mult(S, s5); - - /* Check for special case that d is a normalized power of 2. */ - - spec_case = 0; - if ((mode < 2 || leftright) -#ifdef Honor_FLT_ROUNDS - && Rounding == 1 -#endif - ) { - if (!word1(&u) && !(word0(&u) & Bndry_mask) -#ifndef Sudden_Underflow - && word0(&u) & (Exp_mask & ~Exp_msk1) -#endif - ) { - /* The special case */ - b2 += Log2P; - s2 += Log2P; - spec_case = 1; - } - } - - /* Arrange for convenient computation of quotients: - * shift left if necessary so divisor has 4 leading 0 bits. - * - * Perhaps we should just compute leading 28 bits of S once - * and for all and pass them and a shift to quorem, so it - * can do shifts and ors to compute the numerator for q. - */ - i = dshift(S, s2); - b2 += i; - m2 += i; - s2 += i; - if (b2 > 0) - b = lshift(b, b2); - if (s2 > 0) - S = lshift(S, s2); - if (k_check) { - if (cmp(b,S) < 0) { - k--; - b = multadd(b, 10, 0); /* we botched the k estimate */ - if (leftright) - mhi = multadd(mhi, 10, 0); - ilim = ilim1; - } - } - if (ilim <= 0 && (mode == 3 || mode == 5)) { - if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { - /* no digits, fcvt style */ - no_digits: - k = -1 - ndigits; - goto ret; - } - one_digit: - *s++ = '1'; - k++; - goto ret; - } - if (leftright) { - if (m2 > 0) - mhi = lshift(mhi, m2); - - /* Compute mlo -- check for special case - * that d is a normalized power of 2. - */ - - mlo = mhi; - if (spec_case) { - mhi = Balloc(mhi->k); - Bcopy(mhi, mlo); - mhi = lshift(mhi, Log2P); - } - - for(i = 1;;i++) { - dig = quorem(b,S) + '0'; - /* Do we yet have the shortest decimal string - * that will round to d? - */ - j = cmp(b, mlo); - delta = diff(S, mhi); - j1 = delta->sign ? 1 : cmp(b, delta); - Bfree(delta); -#ifndef ROUND_BIASED - if (j1 == 0 && mode != 1 && !(word1(&u) & 1) -#ifdef Honor_FLT_ROUNDS - && Rounding >= 1 -#endif - ) { - if (dig == '9') - goto round_9_up; - if (j > 0) - dig++; -#ifdef SET_INEXACT - else if (!b->x[0] && b->wds <= 1) - inexact = 0; -#endif - *s++ = dig; - goto ret; - } -#endif - if (j < 0 || (j == 0 && mode != 1 -#ifndef ROUND_BIASED - && !(word1(&u) & 1) -#endif - )) { - if (!b->x[0] && b->wds <= 1) { -#ifdef SET_INEXACT - inexact = 0; -#endif - goto accept_dig; - } -#ifdef Honor_FLT_ROUNDS - if (mode > 1) - switch(Rounding) { - case 0: goto accept_dig; - case 2: goto keep_dig; - } -#endif /*Honor_FLT_ROUNDS*/ - if (j1 > 0) { - b = lshift(b, 1); - j1 = cmp(b, S); -#ifdef ROUND_BIASED - if (j1 >= 0 /*)*/ -#else - if ((j1 > 0 || (j1 == 0 && dig & 1)) -#endif - && dig++ == '9') - goto round_9_up; - } - accept_dig: - *s++ = dig; - goto ret; - } - if (j1 > 0) { -#ifdef Honor_FLT_ROUNDS - if (!Rounding) - goto accept_dig; -#endif - if (dig == '9') { /* possible if i == 1 */ - round_9_up: - *s++ = '9'; - goto roundoff; - } - *s++ = dig + 1; - goto ret; - } -#ifdef Honor_FLT_ROUNDS - keep_dig: -#endif - *s++ = dig; - if (i == ilim) - break; - b = multadd(b, 10, 0); - if (mlo == mhi) - mlo = mhi = multadd(mhi, 10, 0); - else { - mlo = multadd(mlo, 10, 0); - mhi = multadd(mhi, 10, 0); - } - } - } - else - for(i = 1;; i++) { - *s++ = dig = quorem(b,S) + '0'; - if (!b->x[0] && b->wds <= 1) { -#ifdef SET_INEXACT - inexact = 0; -#endif - goto ret; - } - if (i >= ilim) - break; - b = multadd(b, 10, 0); - } - - /* Round off last digit */ - -#ifdef Honor_FLT_ROUNDS - switch(Rounding) { - case 0: goto trimzeros; - case 2: goto roundoff; - } -#endif - b = lshift(b, 1); - j = cmp(b, S); -#ifdef ROUND_BIASED - if (j >= 0) -#else - if (j > 0 || (j == 0 && dig & 1)) -#endif - { - roundoff: - while(*--s == '9') - if (s == s0) { - k++; - *s++ = '1'; - goto ret; - } - ++*s++; - } - else { -#ifdef Honor_FLT_ROUNDS - trimzeros: -#endif - while(*--s == '0'); - s++; - } - ret: - Bfree(S); - if (mhi) { - if (mlo && mlo != mhi) - Bfree(mlo); - Bfree(mhi); - } - ret1: -#ifdef SET_INEXACT - if (inexact) { - if (!oldinexact) { - word0(&u) = Exp_1 + (70 << Exp_shift); - word1(&u) = 0; - dval(&u) += 1.; - } - } - else if (!oldinexact) - clear_inexact(); -#endif - Bfree(b); - *s = 0; - *decpt = k + 1; - if (rve) - *rve = s; - return s0; - } -#ifdef __cplusplus -} -#endif diff --git a/3rd/lua-cjson/dtoa_config.h b/3rd/lua-cjson/dtoa_config.h deleted file mode 100644 index 380e83b0..00000000 --- a/3rd/lua-cjson/dtoa_config.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef _DTOA_CONFIG_H -#define _DTOA_CONFIG_H - -#include -#include -#include - -/* Ensure dtoa.c does not USE_LOCALE. Lua CJSON must not use locale - * aware conversion routines. */ -#undef USE_LOCALE - -/* dtoa.c should not touch errno, Lua CJSON does not use it, and it - * may not be threadsafe */ -#define NO_ERRNO - -#define Long int32_t -#define ULong uint32_t -#define Llong int64_t -#define ULLong uint64_t - -#ifdef IEEE_BIG_ENDIAN -#define IEEE_MC68k -#else -#define IEEE_8087 -#endif - -#define MALLOC(n) xmalloc(n) - -static void *xmalloc(size_t size) -{ - void *p; - - p = malloc(size); - if (!p) { - fprintf(stderr, "Out of memory"); - abort(); - } - - return p; -} - -#ifdef MULTIPLE_THREADS - -/* Enable locking to support multi-threaded applications */ - -#include - -static pthread_mutex_t private_dtoa_lock[2] = { - PTHREAD_MUTEX_INITIALIZER, - PTHREAD_MUTEX_INITIALIZER -}; - -#define ACQUIRE_DTOA_LOCK(n) do { \ - int r = pthread_mutex_lock(&private_dtoa_lock[n]); \ - if (r) { \ - fprintf(stderr, "pthread_mutex_lock failed with %d\n", r); \ - abort(); \ - } \ -} while (0) - -#define FREE_DTOA_LOCK(n) do { \ - int r = pthread_mutex_unlock(&private_dtoa_lock[n]); \ - if (r) { \ - fprintf(stderr, "pthread_mutex_unlock failed with %d\n", r);\ - abort(); \ - } \ -} while (0) - -#endif /* MULTIPLE_THREADS */ - -#endif /* _DTOA_CONFIG_H */ - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/fpconv.c b/3rd/lua-cjson/fpconv.c deleted file mode 100644 index 79908317..00000000 --- a/3rd/lua-cjson/fpconv.c +++ /dev/null @@ -1,205 +0,0 @@ -/* fpconv - Floating point conversion routines - * - * Copyright (c) 2011-2012 Mark Pulford - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* JSON uses a '.' decimal separator. strtod() / sprintf() under C libraries - * with locale support will break when the decimal separator is a comma. - * - * fpconv_* will around these issues with a translation buffer if required. - */ - -#include -#include -#include -#include - -#include "fpconv.h" - -/* Lua CJSON assumes the locale is the same for all threads within a - * process and doesn't change after initialisation. - * - * This avoids the need for per thread storage or expensive checks - * for call. */ -static char locale_decimal_point = '.'; - -/* In theory multibyte decimal_points are possible, but - * Lua CJSON only supports UTF-8 and known locales only have - * single byte decimal points ([.,]). - * - * localconv() may not be thread safe (=>crash), and nl_langinfo() is - * not supported on some platforms. Use sprintf() instead - if the - * locale does change, at least Lua CJSON won't crash. */ -static void fpconv_update_locale() -{ - char buf[8]; - - snprintf(buf, sizeof(buf), "%g", 0.5); - - /* Failing this test might imply the platform has a buggy dtoa - * implementation or wide characters */ - if (buf[0] != '0' || buf[2] != '5' || buf[3] != 0) { - fprintf(stderr, "Error: wide characters found or printf() bug."); - abort(); - } - - locale_decimal_point = buf[1]; -} - -/* Check for a valid number character: [-+0-9a-yA-Y.] - * Eg: -0.6e+5, infinity, 0xF0.F0pF0 - * - * Used to find the probable end of a number. It doesn't matter if - * invalid characters are counted - strtod() will find the valid - * number if it exists. The risk is that slightly more memory might - * be allocated before a parse error occurs. */ -static inline int valid_number_character(char ch) -{ - char lower_ch; - - if ('0' <= ch && ch <= '9') - return 1; - if (ch == '-' || ch == '+' || ch == '.') - return 1; - - /* Hex digits, exponent (e), base (p), "infinity",.. */ - lower_ch = ch | 0x20; - if ('a' <= lower_ch && lower_ch <= 'y') - return 1; - - return 0; -} - -/* Calculate the size of the buffer required for a strtod locale - * conversion. */ -static int strtod_buffer_size(const char *s) -{ - const char *p = s; - - while (valid_number_character(*p)) - p++; - - return p - s; -} - -/* Similar to strtod(), but must be passed the current locale's decimal point - * character. Guaranteed to be called at the start of any valid number in a string */ -double fpconv_strtod(const char *nptr, char **endptr) -{ - char localbuf[FPCONV_G_FMT_BUFSIZE]; - char *buf, *endbuf, *dp; - int buflen; - double value; - - /* System strtod() is fine when decimal point is '.' */ - if (locale_decimal_point == '.') - return strtod(nptr, endptr); - - buflen = strtod_buffer_size(nptr); - if (!buflen) { - /* No valid characters found, standard strtod() return */ - *endptr = (char *)nptr; - return 0; - } - - /* Duplicate number into buffer */ - if (buflen >= FPCONV_G_FMT_BUFSIZE) { - /* Handle unusually large numbers */ - buf = malloc(buflen + 1); - if (!buf) { - fprintf(stderr, "Out of memory"); - abort(); - } - } else { - /* This is the common case.. */ - buf = localbuf; - } - memcpy(buf, nptr, buflen); - buf[buflen] = 0; - - /* Update decimal point character if found */ - dp = strchr(buf, '.'); - if (dp) - *dp = locale_decimal_point; - - value = strtod(buf, &endbuf); - *endptr = (char *)&nptr[endbuf - buf]; - if (buflen >= FPCONV_G_FMT_BUFSIZE) - free(buf); - - return value; -} - -/* "fmt" must point to a buffer of at least 6 characters */ -static void set_number_format(char *fmt, int precision) -{ - int d1, d2, i; - - assert(1 <= precision && precision <= 14); - - /* Create printf format (%.14g) from precision */ - d1 = precision / 10; - d2 = precision % 10; - fmt[0] = '%'; - fmt[1] = '.'; - i = 2; - if (d1) { - fmt[i++] = '0' + d1; - } - fmt[i++] = '0' + d2; - fmt[i++] = 'g'; - fmt[i] = 0; -} - -/* Assumes there is always at least 32 characters available in the target buffer */ -int fpconv_g_fmt(char *str, double num, int precision) -{ - char buf[FPCONV_G_FMT_BUFSIZE]; - char fmt[6]; - int len; - char *b; - - set_number_format(fmt, precision); - - /* Pass through when decimal point character is dot. */ - if (locale_decimal_point == '.') - return snprintf(str, FPCONV_G_FMT_BUFSIZE, fmt, num); - - /* snprintf() to a buffer then translate for other decimal point characters */ - len = snprintf(buf, FPCONV_G_FMT_BUFSIZE, fmt, num); - - /* Copy into target location. Translate decimal point if required */ - b = buf; - do { - *str++ = (*b == locale_decimal_point ? '.' : *b); - } while(*b++); - - return len; -} - -void fpconv_init() -{ - fpconv_update_locale(); -} - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/fpconv.h b/3rd/lua-cjson/fpconv.h deleted file mode 100644 index 01249088..00000000 --- a/3rd/lua-cjson/fpconv.h +++ /dev/null @@ -1,22 +0,0 @@ -/* Lua CJSON floating point conversion routines */ - -/* Buffer required to store the largest string representation of a double. - * - * Longest double printed with %.14g is 21 characters long: - * -1.7976931348623e+308 */ -# define FPCONV_G_FMT_BUFSIZE 32 - -#ifdef USE_INTERNAL_FPCONV -static inline void fpconv_init() -{ - /* Do nothing - not required */ -} -#else -extern inline void fpconv_init(); -#endif - -extern int fpconv_g_fmt(char*, double, int); -extern double fpconv_strtod(const char*, char**); - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/g_fmt.c b/3rd/lua-cjson/g_fmt.c deleted file mode 100644 index 50d6a1d3..00000000 --- a/3rd/lua-cjson/g_fmt.c +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************** - * - * The author of this software is David M. Gay. - * - * Copyright (c) 1991, 1996 by Lucent Technologies. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY - * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - * - ***************************************************************/ - -/* g_fmt(buf,x) stores the closest decimal approximation to x in buf; - * it suffices to declare buf - * char buf[32]; - */ - -#ifdef __cplusplus -extern "C" { -#endif - extern char *dtoa(double, int, int, int *, int *, char **); - extern int g_fmt(char *, double, int); - extern void freedtoa(char*); -#ifdef __cplusplus - } -#endif - -int -fpconv_g_fmt(char *b, double x, int precision) -{ - register int i, k; - register char *s; - int decpt, j, sign; - char *b0, *s0, *se; - - b0 = b; -#ifdef IGNORE_ZERO_SIGN - if (!x) { - *b++ = '0'; - *b = 0; - goto done; - } -#endif - s = s0 = dtoa(x, 2, precision, &decpt, &sign, &se); - if (sign) - *b++ = '-'; - if (decpt == 9999) /* Infinity or Nan */ { - while((*b++ = *s++)); - /* "b" is used to calculate the return length. Decrement to exclude the - * Null terminator from the length */ - b--; - goto done0; - } - if (decpt <= -4 || decpt > precision) { - *b++ = *s++; - if (*s) { - *b++ = '.'; - while((*b = *s++)) - b++; - } - *b++ = 'e'; - /* sprintf(b, "%+.2d", decpt - 1); */ - if (--decpt < 0) { - *b++ = '-'; - decpt = -decpt; - } - else - *b++ = '+'; - for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10); - for(;;) { - i = decpt / k; - *b++ = i + '0'; - if (--j <= 0) - break; - decpt -= i*k; - decpt *= 10; - } - *b = 0; - } - else if (decpt <= 0) { - *b++ = '0'; - *b++ = '.'; - for(; decpt < 0; decpt++) - *b++ = '0'; - while((*b++ = *s++)); - b--; - } - else { - while((*b = *s++)) { - b++; - if (--decpt == 0 && *s) - *b++ = '.'; - } - for(; decpt > 0; decpt--) - *b++ = '0'; - *b = 0; - } - done0: - freedtoa(s0); -#ifdef IGNORE_ZERO_SIGN - done: -#endif - return b - b0; - } diff --git a/3rd/lua-cjson/lua-cjson-2.1.0-1.rockspec b/3rd/lua-cjson/lua-cjson-2.1.0-1.rockspec deleted file mode 100644 index 2d5ff7be..00000000 --- a/3rd/lua-cjson/lua-cjson-2.1.0-1.rockspec +++ /dev/null @@ -1,56 +0,0 @@ -package = "lua-cjson" -version = "2.1.0-1" - -source = { - url = "http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.zip", -} - -description = { - summary = "A fast JSON encoding/parsing module", - detailed = [[ - The Lua CJSON module provides JSON support for Lua. It features: - - Fast, standards compliant encoding/parsing routines - - Full support for JSON with UTF-8, including decoding surrogate pairs - - Optional run-time support for common exceptions to the JSON specification - (infinity, NaN,..) - - No dependencies on other libraries - ]], - homepage = "http://www.kyne.com.au/~mark/software/lua-cjson.php", - license = "MIT" -} - -dependencies = { - "lua >= 5.1" -} - -build = { - type = "builtin", - modules = { - cjson = { - sources = { "lua_cjson.c", "strbuf.c", "fpconv.c" }, - defines = { --- LuaRocks does not support platform specific configuration for Solaris. --- Uncomment the line below on Solaris platforms if required. --- "USE_INTERNAL_ISINF" - } - } - }, - install = { - lua = { - ["cjson.util"] = "lua/cjson/util.lua" - }, - bin = { - json2lua = "lua/json2lua.lua", - lua2json = "lua/lua2json.lua" - } - }, - -- Override default build options (per platform) - platforms = { - win32 = { modules = { cjson = { defines = { - "DISABLE_INVALID_NUMBERS" - } } } } - }, - copy_directories = { "tests" } -} - --- vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/lua-cjson.spec b/3rd/lua-cjson/lua-cjson.spec deleted file mode 100644 index 59f31493..00000000 --- a/3rd/lua-cjson/lua-cjson.spec +++ /dev/null @@ -1,80 +0,0 @@ -%define luaver 5.1 -%define lualibdir %{_libdir}/lua/%{luaver} -%define luadatadir %{_datadir}/lua/%{luaver} - -Name: lua-cjson -Version: 2.1.0 -Release: 1%{?dist} -Summary: A fast JSON encoding/parsing module for Lua - -Group: Development/Libraries -License: MIT -URL: http://www.kyne.com.au/~mark/software/lua-cjson/ -Source0: http://www.kyne.com.au/~mark/software/lua-cjson/download/lua-cjson-%{version}.tar.gz -BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) - -BuildRequires: lua >= %{luaver}, lua-devel >= %{luaver} -Requires: lua >= %{luaver} - -%description -The Lua CJSON module provides JSON support for Lua. It features: -- Fast, standards compliant encoding/parsing routines -- Full support for JSON with UTF-8, including decoding surrogate pairs -- Optional run-time support for common exceptions to the JSON specification - (infinity, NaN,..) -- No dependencies on other libraries - - -%prep -%setup -q - - -%build -make %{?_smp_mflags} CFLAGS="%{optflags}" LUA_INCLUDE_DIR="%{_includedir}" - - -%install -rm -rf "$RPM_BUILD_ROOT" -make install DESTDIR="$RPM_BUILD_ROOT" LUA_CMODULE_DIR="%{lualibdir}" -make install-extra DESTDIR="$RPM_BUILD_ROOT" LUA_MODULE_DIR="%{luadatadir}" \ - LUA_BIN_DIR="%{_bindir}" - - -%clean -rm -rf "$RPM_BUILD_ROOT" - - -%preun -/bin/rm -f "%{luadatadir}/cjson/tests/utf8.dat" - - -%files -%defattr(-,root,root,-) -%doc LICENSE NEWS performance.html performance.txt manual.html manual.txt rfc4627.txt THANKS -%{lualibdir}/* -%{luadatadir}/* -%{_bindir}/* - - -%changelog -* Thu Mar 1 2012 Mark Pulford - 2.1.0-1 -- Update for 2.1.0 - -* Sun Jan 22 2012 Mark Pulford - 2.0.0-1 -- Update for 2.0.0 -- Install lua2json / json2lua utilities - -* Wed Nov 27 2011 Mark Pulford - 1.0.4-1 -- Update for 1.0.4 - -* Wed Sep 15 2011 Mark Pulford - 1.0.3-1 -- Update for 1.0.3 - -* Sun May 29 2011 Mark Pulford - 1.0.2-1 -- Update for 1.0.2 - -* Sun May 10 2011 Mark Pulford - 1.0.1-1 -- Update for 1.0.1 - -* Sun May 1 2011 Mark Pulford - 1.0-1 -- Initial package diff --git a/3rd/lua-cjson/lua/cjson/util.lua b/3rd/lua-cjson/lua/cjson/util.lua deleted file mode 100644 index 6916dad0..00000000 --- a/3rd/lua-cjson/lua/cjson/util.lua +++ /dev/null @@ -1,271 +0,0 @@ -local json = require "cjson" - --- Various common routines used by the Lua CJSON package --- --- Mark Pulford - --- Determine with a Lua table can be treated as an array. --- Explicitly returns "not an array" for very sparse arrays. --- Returns: --- -1 Not an array --- 0 Empty table --- >0 Highest index in the array -local function is_array(table) - local max = 0 - local count = 0 - for k, v in pairs(table) do - if type(k) == "number" then - if k > max then max = k end - count = count + 1 - else - return -1 - end - end - if max > count * 2 then - return -1 - end - - return max -end - -local serialise_value - -local function serialise_table(value, indent, depth) - local spacing, spacing2, indent2 - if indent then - spacing = "\n" .. indent - spacing2 = spacing .. " " - indent2 = indent .. " " - else - spacing, spacing2, indent2 = " ", " ", false - end - depth = depth + 1 - if depth > 50 then - return "Cannot serialise any further: too many nested tables" - end - - local max = is_array(value) - - local comma = false - local fragment = { "{" .. spacing2 } - if max > 0 then - -- Serialise array - for i = 1, max do - if comma then - table.insert(fragment, "," .. spacing2) - end - table.insert(fragment, serialise_value(value[i], indent2, depth)) - comma = true - end - elseif max < 0 then - -- Serialise table - for k, v in pairs(value) do - if comma then - table.insert(fragment, "," .. spacing2) - end - table.insert(fragment, - ("[%s] = %s"):format(serialise_value(k, indent2, depth), - serialise_value(v, indent2, depth))) - comma = true - end - end - table.insert(fragment, spacing .. "}") - - return table.concat(fragment) -end - -function serialise_value(value, indent, depth) - if indent == nil then indent = "" end - if depth == nil then depth = 0 end - - if value == json.null then - return "json.null" - elseif type(value) == "string" then - return ("%q"):format(value) - elseif type(value) == "nil" or type(value) == "number" or - type(value) == "boolean" then - return tostring(value) - elseif type(value) == "table" then - return serialise_table(value, indent, depth) - else - return "\"<" .. type(value) .. ">\"" - end -end - -local function file_load(filename) - local file - if filename == nil then - file = io.stdin - else - local err - file, err = io.open(filename, "rb") - if file == nil then - error(("Unable to read '%s': %s"):format(filename, err)) - end - end - local data = file:read("*a") - - if filename ~= nil then - file:close() - end - - if data == nil then - error("Failed to read " .. filename) - end - - return data -end - -local function file_save(filename, data) - local file - if filename == nil then - file = io.stdout - else - local err - file, err = io.open(filename, "wb") - if file == nil then - error(("Unable to write '%s': %s"):format(filename, err)) - end - end - file:write(data) - if filename ~= nil then - file:close() - end -end - -local function compare_values(val1, val2) - local type1 = type(val1) - local type2 = type(val2) - if type1 ~= type2 then - return false - end - - -- Check for NaN - if type1 == "number" and val1 ~= val1 and val2 ~= val2 then - return true - end - - if type1 ~= "table" then - return val1 == val2 - end - - -- check_keys stores all the keys that must be checked in val2 - local check_keys = {} - for k, _ in pairs(val1) do - check_keys[k] = true - end - - for k, v in pairs(val2) do - if not check_keys[k] then - return false - end - - if not compare_values(val1[k], val2[k]) then - return false - end - - check_keys[k] = nil - end - for k, _ in pairs(check_keys) do - -- Not the same if any keys from val1 were not found in val2 - return false - end - return true -end - -local test_count_pass = 0 -local test_count_total = 0 - -local function run_test_summary() - return test_count_pass, test_count_total -end - -local function run_test(testname, func, input, should_work, output) - local function status_line(name, status, value) - local statusmap = { [true] = ":success", [false] = ":error" } - if status ~= nil then - name = name .. statusmap[status] - end - print(("[%s] %s"):format(name, serialise_value(value, false))) - end - - local result = { pcall(func, unpack(input)) } - local success = table.remove(result, 1) - - local correct = false - if success == should_work and compare_values(result, output) then - correct = true - test_count_pass = test_count_pass + 1 - end - test_count_total = test_count_total + 1 - - local teststatus = { [true] = "PASS", [false] = "FAIL" } - print(("==> Test [%d] %s: %s"):format(test_count_total, testname, - teststatus[correct])) - - status_line("Input", nil, input) - if not correct then - status_line("Expected", should_work, output) - end - status_line("Received", success, result) - print() - - return correct, result -end - -local function run_test_group(tests) - local function run_helper(name, func, input) - if type(name) == "string" and #name > 0 then - print("==> " .. name) - end - -- Not a protected call, these functions should never generate errors. - func(unpack(input or {})) - print() - end - - for _, v in ipairs(tests) do - -- Run the helper if "should_work" is missing - if v[4] == nil then - run_helper(unpack(v)) - else - run_test(unpack(v)) - end - end -end - --- Run a Lua script in a separate environment -local function run_script(script, env) - local env = env or {} - local func - - -- Use setfenv() if it exists, otherwise assume Lua 5.2 load() exists - if _G.setfenv then - func = loadstring(script) - if func then - setfenv(func, env) - end - else - func = load(script, nil, nil, env) - end - - if func == nil then - error("Invalid syntax.") - end - func() - - return env -end - --- Export functions -return { - serialise_value = serialise_value, - file_load = file_load, - file_save = file_save, - compare_values = compare_values, - run_test_summary = run_test_summary, - run_test = run_test, - run_test_group = run_test_group, - run_script = run_script -} - --- vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/lua/json2lua.lua b/3rd/lua-cjson/lua/json2lua.lua deleted file mode 100755 index 014416d2..00000000 --- a/3rd/lua-cjson/lua/json2lua.lua +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env lua - --- usage: json2lua.lua [json_file] --- --- Eg: --- echo '[ "testing" ]' | ./json2lua.lua --- ./json2lua.lua test.json - -local json = require "cjson" -local util = require "cjson.util" - -local json_text = util.file_load(arg[1]) -local t = json.decode(json_text) -print(util.serialise_value(t)) diff --git a/3rd/lua-cjson/lua/lua2json.lua b/3rd/lua-cjson/lua/lua2json.lua deleted file mode 100755 index aee8869a..00000000 --- a/3rd/lua-cjson/lua/lua2json.lua +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env lua - --- usage: lua2json.lua [lua_file] --- --- Eg: --- echo '{ "testing" }' | ./lua2json.lua --- ./lua2json.lua test.lua - -local json = require "cjson" -local util = require "cjson.util" - -local env = { - json = { null = json.null }, - null = json.null -} - -local t = util.run_script("data = " .. util.file_load(arg[1]), env) -print(json.encode(t.data)) - --- vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/lua_cjson.c b/3rd/lua-cjson/lua_cjson.c deleted file mode 100644 index f91b7e38..00000000 --- a/3rd/lua-cjson/lua_cjson.c +++ /dev/null @@ -1,1425 +0,0 @@ -/* Lua CJSON - JSON support for Lua - * - * Copyright (c) 2010-2012 Mark Pulford - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* Caveats: - * - JSON "null" values are represented as lightuserdata since Lua - * tables cannot contain "nil". Compare with cjson.null. - * - Invalid UTF-8 characters are not detected and will be passed - * untouched. If required, UTF-8 error checking should be done - * outside this library. - * - Javascript comments are not part of the JSON spec, and are not - * currently supported. - * - * Note: Decoding is slower than encoding. Lua spends significant - * time (30%) managing tables when parsing JSON since it is - * difficult to know object/array sizes ahead of time. - */ - -#include -#include -#include -#include -#include -#include - -#include "strbuf.h" -#include "fpconv.h" - -#ifndef CJSON_MODNAME -#define CJSON_MODNAME "cjson" -#endif - -#ifndef CJSON_VERSION -#define CJSON_VERSION "2.1.0" -#endif - -/* Workaround for Solaris platforms missing isinf() */ -#if !defined(isinf) && (defined(USE_INTERNAL_ISINF) || defined(MISSING_ISINF)) -#define isinf(x) (!isnan(x) && isnan((x) - (x))) -#endif - -#define DEFAULT_SPARSE_CONVERT 0 -#define DEFAULT_SPARSE_RATIO 2 -#define DEFAULT_SPARSE_SAFE 10 -#define DEFAULT_ENCODE_MAX_DEPTH 1000 -#define DEFAULT_DECODE_MAX_DEPTH 1000 -#define DEFAULT_ENCODE_INVALID_NUMBERS 0 -#define DEFAULT_DECODE_INVALID_NUMBERS 1 -#define DEFAULT_ENCODE_KEEP_BUFFER 1 -#define DEFAULT_ENCODE_NUMBER_PRECISION 14 - -#ifdef DISABLE_INVALID_NUMBERS -#undef DEFAULT_DECODE_INVALID_NUMBERS -#define DEFAULT_DECODE_INVALID_NUMBERS 0 -#endif - -typedef enum { - T_OBJ_BEGIN, - T_OBJ_END, - T_ARR_BEGIN, - T_ARR_END, - T_STRING, - T_NUMBER, - T_BOOLEAN, - T_NULL, - T_COLON, - T_COMMA, - T_END, - T_WHITESPACE, - T_ERROR, - T_UNKNOWN -} json_token_type_t; - -static const char *json_token_type_name[] = { - "T_OBJ_BEGIN", - "T_OBJ_END", - "T_ARR_BEGIN", - "T_ARR_END", - "T_STRING", - "T_NUMBER", - "T_BOOLEAN", - "T_NULL", - "T_COLON", - "T_COMMA", - "T_END", - "T_WHITESPACE", - "T_ERROR", - "T_UNKNOWN", - NULL -}; - -typedef struct { - json_token_type_t ch2token[256]; - char escape2char[256]; /* Decoding */ - - /* encode_buf is only allocated and used when - * encode_keep_buffer is set */ - strbuf_t encode_buf; - - int encode_sparse_convert; - int encode_sparse_ratio; - int encode_sparse_safe; - int encode_max_depth; - int encode_invalid_numbers; /* 2 => Encode as "null" */ - int encode_number_precision; - int encode_keep_buffer; - - int decode_invalid_numbers; - int decode_max_depth; -} json_config_t; - -typedef struct { - const char *data; - const char *ptr; - strbuf_t *tmp; /* Temporary storage for strings */ - json_config_t *cfg; - int current_depth; -} json_parse_t; - -typedef struct { - json_token_type_t type; - int index; - union { - const char *string; - double number; - int boolean; - } value; - int string_len; -} json_token_t; - -static const char *char2escape[256] = { - "\\u0000", "\\u0001", "\\u0002", "\\u0003", - "\\u0004", "\\u0005", "\\u0006", "\\u0007", - "\\b", "\\t", "\\n", "\\u000b", - "\\f", "\\r", "\\u000e", "\\u000f", - "\\u0010", "\\u0011", "\\u0012", "\\u0013", - "\\u0014", "\\u0015", "\\u0016", "\\u0017", - "\\u0018", "\\u0019", "\\u001a", "\\u001b", - "\\u001c", "\\u001d", "\\u001e", "\\u001f", - NULL, NULL, "\\\"", NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\/", - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, "\\\\", NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\u007f", - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, -}; - -/* ===== CONFIGURATION ===== */ - -static json_config_t *json_fetch_config(lua_State *l) -{ - json_config_t *cfg; - - cfg = lua_touserdata(l, lua_upvalueindex(1)); - if (!cfg) - luaL_error(l, "BUG: Unable to fetch CJSON configuration"); - - return cfg; -} - -/* Ensure the correct number of arguments have been provided. - * Pad with nil to allow other functions to simply check arg[i] - * to find whether an argument was provided */ -static json_config_t *json_arg_init(lua_State *l, int args) -{ - luaL_argcheck(l, lua_gettop(l) <= args, args + 1, - "found too many arguments"); - - while (lua_gettop(l) < args) - lua_pushnil(l); - - return json_fetch_config(l); -} - -/* Process integer options for configuration functions */ -static int json_integer_option(lua_State *l, int optindex, int *setting, - int min, int max) -{ - char errmsg[64]; - int value; - - if (!lua_isnil(l, optindex)) { - value = luaL_checkinteger(l, optindex); - snprintf(errmsg, sizeof(errmsg), "expected integer between %d and %d", min, max); - luaL_argcheck(l, min <= value && value <= max, 1, errmsg); - *setting = value; - } - - lua_pushinteger(l, *setting); - - return 1; -} - -/* Process enumerated arguments for a configuration function */ -static int json_enum_option(lua_State *l, int optindex, int *setting, - const char **options, int bool_true) -{ - static const char *bool_options[] = { "off", "on", NULL }; - - if (!options) { - options = bool_options; - bool_true = 1; - } - - if (!lua_isnil(l, optindex)) { - if (bool_true && lua_isboolean(l, optindex)) - *setting = lua_toboolean(l, optindex) * bool_true; - else - *setting = luaL_checkoption(l, optindex, NULL, options); - } - - if (bool_true && (*setting == 0 || *setting == bool_true)) - lua_pushboolean(l, *setting); - else - lua_pushstring(l, options[*setting]); - - return 1; -} - -/* Configures handling of extremely sparse arrays: - * convert: Convert extremely sparse arrays into objects? Otherwise error. - * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio - * safe: Always use an array when the max index <= safe */ -static int json_cfg_encode_sparse_array(lua_State *l) -{ - json_config_t *cfg = json_arg_init(l, 3); - - json_enum_option(l, 1, &cfg->encode_sparse_convert, NULL, 1); - json_integer_option(l, 2, &cfg->encode_sparse_ratio, 0, INT_MAX); - json_integer_option(l, 3, &cfg->encode_sparse_safe, 0, INT_MAX); - - return 3; -} - -/* Configures the maximum number of nested arrays/objects allowed when - * encoding */ -static int json_cfg_encode_max_depth(lua_State *l) -{ - json_config_t *cfg = json_arg_init(l, 1); - - return json_integer_option(l, 1, &cfg->encode_max_depth, 1, INT_MAX); -} - -/* Configures the maximum number of nested arrays/objects allowed when - * encoding */ -static int json_cfg_decode_max_depth(lua_State *l) -{ - json_config_t *cfg = json_arg_init(l, 1); - - return json_integer_option(l, 1, &cfg->decode_max_depth, 1, INT_MAX); -} - -/* Configures number precision when converting doubles to text */ -static int json_cfg_encode_number_precision(lua_State *l) -{ - json_config_t *cfg = json_arg_init(l, 1); - - return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14); -} - -/* Configures JSON encoding buffer persistence */ -static int json_cfg_encode_keep_buffer(lua_State *l) -{ - json_config_t *cfg = json_arg_init(l, 1); - int old_value; - - old_value = cfg->encode_keep_buffer; - - json_enum_option(l, 1, &cfg->encode_keep_buffer, NULL, 1); - - /* Init / free the buffer if the setting has changed */ - if (old_value ^ cfg->encode_keep_buffer) { - if (cfg->encode_keep_buffer) - strbuf_init(&cfg->encode_buf, 0); - else - strbuf_free(&cfg->encode_buf); - } - - return 1; -} - -#if defined(DISABLE_INVALID_NUMBERS) && !defined(USE_INTERNAL_FPCONV) -void json_verify_invalid_number_setting(lua_State *l, int *setting) -{ - if (*setting == 1) { - *setting = 0; - luaL_error(l, "Infinity, NaN, and/or hexadecimal numbers are not supported."); - } -} -#else -#define json_verify_invalid_number_setting(l, s) do { } while(0) -#endif - -static int json_cfg_encode_invalid_numbers(lua_State *l) -{ - static const char *options[] = { "off", "on", "null", NULL }; - json_config_t *cfg = json_arg_init(l, 1); - - json_enum_option(l, 1, &cfg->encode_invalid_numbers, options, 1); - - json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers); - - return 1; -} - -static int json_cfg_decode_invalid_numbers(lua_State *l) -{ - json_config_t *cfg = json_arg_init(l, 1); - - json_enum_option(l, 1, &cfg->decode_invalid_numbers, NULL, 1); - - json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers); - - return 1; -} - -static int json_destroy_config(lua_State *l) -{ - json_config_t *cfg; - - cfg = lua_touserdata(l, 1); - if (cfg) - strbuf_free(&cfg->encode_buf); - cfg = NULL; - - return 0; -} - -static void json_create_config(lua_State *l) -{ - json_config_t *cfg; - int i; - - cfg = lua_newuserdata(l, sizeof(*cfg)); - - /* Create GC method to clean up strbuf */ - lua_newtable(l); - lua_pushcfunction(l, json_destroy_config); - lua_setfield(l, -2, "__gc"); - lua_setmetatable(l, -2); - - cfg->encode_sparse_convert = DEFAULT_SPARSE_CONVERT; - cfg->encode_sparse_ratio = DEFAULT_SPARSE_RATIO; - cfg->encode_sparse_safe = DEFAULT_SPARSE_SAFE; - cfg->encode_max_depth = DEFAULT_ENCODE_MAX_DEPTH; - cfg->decode_max_depth = DEFAULT_DECODE_MAX_DEPTH; - cfg->encode_invalid_numbers = DEFAULT_ENCODE_INVALID_NUMBERS; - cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS; - cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER; - cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; - -#if DEFAULT_ENCODE_KEEP_BUFFER > 0 - strbuf_init(&cfg->encode_buf, 0); -#endif - - /* Decoding init */ - - /* Tag all characters as an error */ - for (i = 0; i < 256; i++) - cfg->ch2token[i] = T_ERROR; - - /* Set tokens that require no further processing */ - cfg->ch2token['{'] = T_OBJ_BEGIN; - cfg->ch2token['}'] = T_OBJ_END; - cfg->ch2token['['] = T_ARR_BEGIN; - cfg->ch2token[']'] = T_ARR_END; - cfg->ch2token[','] = T_COMMA; - cfg->ch2token[':'] = T_COLON; - cfg->ch2token['\0'] = T_END; - cfg->ch2token[' '] = T_WHITESPACE; - cfg->ch2token['\t'] = T_WHITESPACE; - cfg->ch2token['\n'] = T_WHITESPACE; - cfg->ch2token['\r'] = T_WHITESPACE; - - /* Update characters that require further processing */ - cfg->ch2token['f'] = T_UNKNOWN; /* false? */ - cfg->ch2token['i'] = T_UNKNOWN; /* inf, ininity? */ - cfg->ch2token['I'] = T_UNKNOWN; - cfg->ch2token['n'] = T_UNKNOWN; /* null, nan? */ - cfg->ch2token['N'] = T_UNKNOWN; - cfg->ch2token['t'] = T_UNKNOWN; /* true? */ - cfg->ch2token['"'] = T_UNKNOWN; /* string? */ - cfg->ch2token['+'] = T_UNKNOWN; /* number? */ - cfg->ch2token['-'] = T_UNKNOWN; - for (i = 0; i < 10; i++) - cfg->ch2token['0' + i] = T_UNKNOWN; - - /* Lookup table for parsing escape characters */ - for (i = 0; i < 256; i++) - cfg->escape2char[i] = 0; /* String error */ - cfg->escape2char['"'] = '"'; - cfg->escape2char['\\'] = '\\'; - cfg->escape2char['/'] = '/'; - cfg->escape2char['b'] = '\b'; - cfg->escape2char['t'] = '\t'; - cfg->escape2char['n'] = '\n'; - cfg->escape2char['f'] = '\f'; - cfg->escape2char['r'] = '\r'; - cfg->escape2char['u'] = 'u'; /* Unicode parsing required */ -} - -/* ===== ENCODING ===== */ - -static void json_encode_exception(lua_State *l, json_config_t *cfg, strbuf_t *json, int lindex, - const char *reason) -{ - if (!cfg->encode_keep_buffer) - strbuf_free(json); - luaL_error(l, "Cannot serialise %s: %s", - lua_typename(l, lua_type(l, lindex)), reason); -} - -/* json_append_string args: - * - lua_State - * - JSON strbuf - * - String (Lua stack index) - * - * Returns nothing. Doesn't remove string from Lua stack */ -static void json_append_string(lua_State *l, strbuf_t *json, int lindex) -{ - const char *escstr; - int i; - const char *str; - size_t len; - - str = lua_tolstring(l, lindex, &len); - - /* Worst case is len * 6 (all unicode escapes). - * This buffer is reused constantly for small strings - * If there are any excess pages, they won't be hit anyway. - * This gains ~5% speedup. */ - strbuf_ensure_empty_length(json, len * 6 + 2); - - strbuf_append_char_unsafe(json, '\"'); - for (i = 0; i < len; i++) { - escstr = char2escape[(unsigned char)str[i]]; - if (escstr) - strbuf_append_string(json, escstr); - else - strbuf_append_char_unsafe(json, str[i]); - } - strbuf_append_char_unsafe(json, '\"'); -} - -/* Find the size of the array on the top of the Lua stack - * -1 object (not a pure array) - * >=0 elements in array - */ -static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json) -{ - double k; - int max; - int items; - - max = 0; - items = 0; - - lua_pushnil(l); - /* table, startkey */ - while (lua_next(l, -2) != 0) { - /* table, key, value */ - if (lua_type(l, -2) == LUA_TNUMBER && - (k = lua_tonumber(l, -2))) { - /* Integer >= 1 ? */ - if (floor(k) == k && k >= 1) { - if (k > max) - max = k; - items++; - lua_pop(l, 1); - continue; - } - } - - /* Must not be an array (non integer key) */ - lua_pop(l, 2); - return -1; - } - - /* Encode excessively sparse arrays as objects (if enabled) */ - if (cfg->encode_sparse_ratio > 0 && - max > items * cfg->encode_sparse_ratio && - max > cfg->encode_sparse_safe) { - if (!cfg->encode_sparse_convert) - json_encode_exception(l, cfg, json, -1, "excessively sparse array"); - - return -1; - } - - return max; -} - -static void json_check_encode_depth(lua_State *l, json_config_t *cfg, - int current_depth, strbuf_t *json) -{ - /* Ensure there are enough slots free to traverse a table (key, - * value) and push a string for a potential error message. - * - * Unlike "decode", the key and value are still on the stack when - * lua_checkstack() is called. Hence an extra slot for luaL_error() - * below is required just in case the next check to lua_checkstack() - * fails. - * - * While this won't cause a crash due to the EXTRA_STACK reserve - * slots, it would still be an improper use of the API. */ - if (current_depth <= cfg->encode_max_depth && lua_checkstack(l, 3)) - return; - - if (!cfg->encode_keep_buffer) - strbuf_free(json); - - luaL_error(l, "Cannot serialise, excessive nesting (%d)", - current_depth); -} - -static void json_append_data(lua_State *l, json_config_t *cfg, - int current_depth, strbuf_t *json); - -/* json_append_array args: - * - lua_State - * - JSON strbuf - * - Size of passwd Lua array (top of stack) */ -static void json_append_array(lua_State *l, json_config_t *cfg, int current_depth, - strbuf_t *json, int array_length) -{ - int comma, i; - - strbuf_append_char(json, '['); - - comma = 0; - for (i = 1; i <= array_length; i++) { - if (comma) - strbuf_append_char(json, ','); - else - comma = 1; - - lua_rawgeti(l, -1, i); - json_append_data(l, cfg, current_depth, json); - lua_pop(l, 1); - } - - strbuf_append_char(json, ']'); -} - -static void json_append_number(lua_State *l, json_config_t *cfg, - strbuf_t *json, int lindex) -{ - double num = lua_tonumber(l, lindex); - int len; - - if (cfg->encode_invalid_numbers == 0) { - /* Prevent encoding invalid numbers */ - if (isinf(num) || isnan(num)) - json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf"); - } else if (cfg->encode_invalid_numbers == 1) { - /* Encode invalid numbers, but handle "nan" separately - * since some platforms may encode as "-nan". */ - if (isnan(num)) { - strbuf_append_mem(json, "nan", 3); - return; - } - } else { - /* Encode invalid numbers as "null" */ - if (isinf(num) || isnan(num)) { - strbuf_append_mem(json, "null", 4); - return; - } - } - - strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); - len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision); - strbuf_extend_length(json, len); -} - -static void json_append_object(lua_State *l, json_config_t *cfg, - int current_depth, strbuf_t *json) -{ - int comma, keytype; - - /* Object */ - strbuf_append_char(json, '{'); - - lua_pushnil(l); - /* table, startkey */ - comma = 0; - while (lua_next(l, -2) != 0) { - if (comma) - strbuf_append_char(json, ','); - else - comma = 1; - - /* table, key, value */ - keytype = lua_type(l, -2); - if (keytype == LUA_TNUMBER) { - strbuf_append_char(json, '"'); - json_append_number(l, cfg, json, -2); - strbuf_append_mem(json, "\":", 2); - } else if (keytype == LUA_TSTRING) { - json_append_string(l, json, -2); - strbuf_append_char(json, ':'); - } else { - json_encode_exception(l, cfg, json, -2, - "table key must be a number or string"); - /* never returns */ - } - - /* table, key, value */ - json_append_data(l, cfg, current_depth, json); - lua_pop(l, 1); - /* table, key */ - } - - strbuf_append_char(json, '}'); -} - -/* Serialise Lua data into JSON string. */ -static void json_append_data(lua_State *l, json_config_t *cfg, - int current_depth, strbuf_t *json) -{ - int len; - - switch (lua_type(l, -1)) { - case LUA_TSTRING: - json_append_string(l, json, -1); - break; - case LUA_TNUMBER: - json_append_number(l, cfg, json, -1); - break; - case LUA_TBOOLEAN: - if (lua_toboolean(l, -1)) - strbuf_append_mem(json, "true", 4); - else - strbuf_append_mem(json, "false", 5); - break; - case LUA_TTABLE: - current_depth++; - json_check_encode_depth(l, cfg, current_depth, json); - len = lua_array_length(l, cfg, json); - if (len > 0) - json_append_array(l, cfg, current_depth, json, len); - else - json_append_object(l, cfg, current_depth, json); - break; - case LUA_TNIL: - strbuf_append_mem(json, "null", 4); - break; - case LUA_TLIGHTUSERDATA: - if (lua_touserdata(l, -1) == NULL) { - strbuf_append_mem(json, "null", 4); - break; - } - default: - /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD, - * and LUA_TLIGHTUSERDATA) cannot be serialised */ - json_encode_exception(l, cfg, json, -1, "type not supported"); - /* never returns */ - } -} - -static int json_encode(lua_State *l) -{ - json_config_t *cfg = json_fetch_config(l); - strbuf_t local_encode_buf; - strbuf_t *encode_buf; - char *json; - int len; - - luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); - - if (!cfg->encode_keep_buffer) { - /* Use private buffer */ - encode_buf = &local_encode_buf; - strbuf_init(encode_buf, 0); - } else { - /* Reuse existing buffer */ - encode_buf = &cfg->encode_buf; - strbuf_reset(encode_buf); - } - - json_append_data(l, cfg, 0, encode_buf); - json = strbuf_string(encode_buf, &len); - - lua_pushlstring(l, json, len); - - if (!cfg->encode_keep_buffer) - strbuf_free(encode_buf); - - return 1; -} - -/* ===== DECODING ===== */ - -static void json_process_value(lua_State *l, json_parse_t *json, - json_token_t *token); - -static int hexdigit2int(char hex) -{ - if ('0' <= hex && hex <= '9') - return hex - '0'; - - /* Force lowercase */ - hex |= 0x20; - if ('a' <= hex && hex <= 'f') - return 10 + hex - 'a'; - - return -1; -} - -static int decode_hex4(const char *hex) -{ - int digit[4]; - int i; - - /* Convert ASCII hex digit to numeric digit - * Note: this returns an error for invalid hex digits, including - * NULL */ - for (i = 0; i < 4; i++) { - digit[i] = hexdigit2int(hex[i]); - if (digit[i] < 0) { - return -1; - } - } - - return (digit[0] << 12) + - (digit[1] << 8) + - (digit[2] << 4) + - digit[3]; -} - -/* Converts a Unicode codepoint to UTF-8. - * Returns UTF-8 string length, and up to 4 bytes in *utf8 */ -static int codepoint_to_utf8(char *utf8, int codepoint) -{ - /* 0xxxxxxx */ - if (codepoint <= 0x7F) { - utf8[0] = codepoint; - return 1; - } - - /* 110xxxxx 10xxxxxx */ - if (codepoint <= 0x7FF) { - utf8[0] = (codepoint >> 6) | 0xC0; - utf8[1] = (codepoint & 0x3F) | 0x80; - return 2; - } - - /* 1110xxxx 10xxxxxx 10xxxxxx */ - if (codepoint <= 0xFFFF) { - utf8[0] = (codepoint >> 12) | 0xE0; - utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80; - utf8[2] = (codepoint & 0x3F) | 0x80; - return 3; - } - - /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ - if (codepoint <= 0x1FFFFF) { - utf8[0] = (codepoint >> 18) | 0xF0; - utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80; - utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80; - utf8[3] = (codepoint & 0x3F) | 0x80; - return 4; - } - - return 0; -} - - -/* Called when index pointing to beginning of UTF-16 code escape: \uXXXX - * \u is guaranteed to exist, but the remaining hex characters may be - * missing. - * Translate to UTF-8 and append to temporary token string. - * Must advance index to the next character to be processed. - * Returns: 0 success - * -1 error - */ -static int json_append_unicode_escape(json_parse_t *json) -{ - char utf8[4]; /* Surrogate pairs require 4 UTF-8 bytes */ - int codepoint; - int surrogate_low; - int len; - int escape_len = 6; - - /* Fetch UTF-16 code unit */ - codepoint = decode_hex4(json->ptr + 2); - if (codepoint < 0) - return -1; - - /* UTF-16 surrogate pairs take the following 2 byte form: - * 11011 x yyyyyyyyyy - * When x = 0: y is the high 10 bits of the codepoint - * x = 1: y is the low 10 bits of the codepoint - * - * Check for a surrogate pair (high or low) */ - if ((codepoint & 0xF800) == 0xD800) { - /* Error if the 1st surrogate is not high */ - if (codepoint & 0x400) - return -1; - - /* Ensure the next code is a unicode escape */ - if (*(json->ptr + escape_len) != '\\' || - *(json->ptr + escape_len + 1) != 'u') { - return -1; - } - - /* Fetch the next codepoint */ - surrogate_low = decode_hex4(json->ptr + 2 + escape_len); - if (surrogate_low < 0) - return -1; - - /* Error if the 2nd code is not a low surrogate */ - if ((surrogate_low & 0xFC00) != 0xDC00) - return -1; - - /* Calculate Unicode codepoint */ - codepoint = (codepoint & 0x3FF) << 10; - surrogate_low &= 0x3FF; - codepoint = (codepoint | surrogate_low) + 0x10000; - escape_len = 12; - } - - /* Convert codepoint to UTF-8 */ - len = codepoint_to_utf8(utf8, codepoint); - if (!len) - return -1; - - /* Append bytes and advance parse index */ - strbuf_append_mem_unsafe(json->tmp, utf8, len); - json->ptr += escape_len; - - return 0; -} - -static void json_set_token_error(json_token_t *token, json_parse_t *json, - const char *errtype) -{ - token->type = T_ERROR; - token->index = json->ptr - json->data; - token->value.string = errtype; -} - -static void json_next_string_token(json_parse_t *json, json_token_t *token) -{ - char *escape2char = json->cfg->escape2char; - char ch; - - /* Caller must ensure a string is next */ - assert(*json->ptr == '"'); - - /* Skip " */ - json->ptr++; - - /* json->tmp is the temporary strbuf used to accumulate the - * decoded string value. - * json->tmp is sized to handle JSON containing only a string value. - */ - strbuf_reset(json->tmp); - - while ((ch = *json->ptr) != '"') { - if (!ch) { - /* Premature end of the string */ - json_set_token_error(token, json, "unexpected end of string"); - return; - } - - /* Handle escapes */ - if (ch == '\\') { - /* Fetch escape character */ - ch = *(json->ptr + 1); - - /* Translate escape code and append to tmp string */ - ch = escape2char[(unsigned char)ch]; - if (ch == 'u') { - if (json_append_unicode_escape(json) == 0) - continue; - - json_set_token_error(token, json, - "invalid unicode escape code"); - return; - } - if (!ch) { - json_set_token_error(token, json, "invalid escape code"); - return; - } - - /* Skip '\' */ - json->ptr++; - } - /* Append normal character or translated single character - * Unicode escapes are handled above */ - strbuf_append_char_unsafe(json->tmp, ch); - json->ptr++; - } - json->ptr++; /* Eat final quote (") */ - - strbuf_ensure_null(json->tmp); - - token->type = T_STRING; - token->value.string = strbuf_string(json->tmp, &token->string_len); -} - -/* JSON numbers should take the following form: - * -?(0|[1-9]|[1-9][0-9]+)(.[0-9]+)?([eE][-+]?[0-9]+)? - * - * json_next_number_token() uses strtod() which allows other forms: - * - numbers starting with '+' - * - NaN, -NaN, infinity, -infinity - * - hexadecimal numbers - * - numbers with leading zeros - * - * json_is_invalid_number() detects "numbers" which may pass strtod()'s - * error checking, but should not be allowed with strict JSON. - * - * json_is_invalid_number() may pass numbers which cause strtod() - * to generate an error. - */ -static int json_is_invalid_number(json_parse_t *json) -{ - const char *p = json->ptr; - - /* Reject numbers starting with + */ - if (*p == '+') - return 1; - - /* Skip minus sign if it exists */ - if (*p == '-') - p++; - - /* Reject numbers starting with 0x, or leading zeros */ - if (*p == '0') { - int ch2 = *(p + 1); - - if ((ch2 | 0x20) == 'x' || /* Hex */ - ('0' <= ch2 && ch2 <= '9')) /* Leading zero */ - return 1; - - return 0; - } else if (*p <= '9') { - return 0; /* Ordinary number */ - } - - /* Reject inf/nan */ - if (!strncasecmp(p, "inf", 3)) - return 1; - if (!strncasecmp(p, "nan", 3)) - return 1; - - /* Pass all other numbers which may still be invalid, but - * strtod() will catch them. */ - return 0; -} - -static void json_next_number_token(json_parse_t *json, json_token_t *token) -{ - char *endptr; - - token->type = T_NUMBER; - token->value.number = fpconv_strtod(json->ptr, &endptr); - if (json->ptr == endptr) - json_set_token_error(token, json, "invalid number"); - else - json->ptr = endptr; /* Skip the processed number */ - - return; -} - -/* Fills in the token struct. - * T_STRING will return a pointer to the json_parse_t temporary string - * T_ERROR will leave the json->ptr pointer at the error. - */ -static void json_next_token(json_parse_t *json, json_token_t *token) -{ - const json_token_type_t *ch2token = json->cfg->ch2token; - int ch; - - /* Eat whitespace. */ - while (1) { - ch = (unsigned char)*(json->ptr); - token->type = ch2token[ch]; - if (token->type != T_WHITESPACE) - break; - json->ptr++; - } - - /* Store location of new token. Required when throwing errors - * for unexpected tokens (syntax errors). */ - token->index = json->ptr - json->data; - - /* Don't advance the pointer for an error or the end */ - if (token->type == T_ERROR) { - json_set_token_error(token, json, "invalid token"); - return; - } - - if (token->type == T_END) { - return; - } - - /* Found a known single character token, advance index and return */ - if (token->type != T_UNKNOWN) { - json->ptr++; - return; - } - - /* Process characters which triggered T_UNKNOWN - * - * Must use strncmp() to match the front of the JSON string. - * JSON identifier must be lowercase. - * When strict_numbers if disabled, either case is allowed for - * Infinity/NaN (since we are no longer following the spec..) */ - if (ch == '"') { - json_next_string_token(json, token); - return; - } else if (ch == '-' || ('0' <= ch && ch <= '9')) { - if (!json->cfg->decode_invalid_numbers && json_is_invalid_number(json)) { - json_set_token_error(token, json, "invalid number"); - return; - } - json_next_number_token(json, token); - return; - } else if (!strncmp(json->ptr, "true", 4)) { - token->type = T_BOOLEAN; - token->value.boolean = 1; - json->ptr += 4; - return; - } else if (!strncmp(json->ptr, "false", 5)) { - token->type = T_BOOLEAN; - token->value.boolean = 0; - json->ptr += 5; - return; - } else if (!strncmp(json->ptr, "null", 4)) { - token->type = T_NULL; - json->ptr += 4; - return; - } else if (json->cfg->decode_invalid_numbers && - json_is_invalid_number(json)) { - /* When decode_invalid_numbers is enabled, only attempt to process - * numbers we know are invalid JSON (Inf, NaN, hex) - * This is required to generate an appropriate token error, - * otherwise all bad tokens will register as "invalid number" - */ - json_next_number_token(json, token); - return; - } - - /* Token starts with t/f/n but isn't recognised above. */ - json_set_token_error(token, json, "invalid token"); -} - -/* This function does not return. - * DO NOT CALL WITH DYNAMIC MEMORY ALLOCATED. - * The only supported exception is the temporary parser string - * json->tmp struct. - * json and token should exist on the stack somewhere. - * luaL_error() will long_jmp and release the stack */ -static void json_throw_parse_error(lua_State *l, json_parse_t *json, - const char *exp, json_token_t *token) -{ - const char *found; - - strbuf_free(json->tmp); - - if (token->type == T_ERROR) - found = token->value.string; - else - found = json_token_type_name[token->type]; - - /* Note: token->index is 0 based, display starting from 1 */ - luaL_error(l, "Expected %s but found %s at character %d", - exp, found, token->index + 1); -} - -static inline void json_decode_ascend(json_parse_t *json) -{ - json->current_depth--; -} - -static void json_decode_descend(lua_State *l, json_parse_t *json, int slots) -{ - json->current_depth++; - - if (json->current_depth <= json->cfg->decode_max_depth && - lua_checkstack(l, slots)) { - return; - } - - strbuf_free(json->tmp); - luaL_error(l, "Found too many nested data structures (%d) at character %d", - json->current_depth, json->ptr - json->data); -} - -static void json_parse_object_context(lua_State *l, json_parse_t *json) -{ - json_token_t token; - - /* 3 slots required: - * .., table, key, value */ - json_decode_descend(l, json, 3); - - lua_newtable(l); - - json_next_token(json, &token); - - /* Handle empty objects */ - if (token.type == T_OBJ_END) { - json_decode_ascend(json); - return; - } - - while (1) { - if (token.type != T_STRING) - json_throw_parse_error(l, json, "object key string", &token); - - /* Push key */ - lua_pushlstring(l, token.value.string, token.string_len); - - json_next_token(json, &token); - if (token.type != T_COLON) - json_throw_parse_error(l, json, "colon", &token); - - /* Fetch value */ - json_next_token(json, &token); - json_process_value(l, json, &token); - - /* Set key = value */ - lua_rawset(l, -3); - - json_next_token(json, &token); - - if (token.type == T_OBJ_END) { - json_decode_ascend(json); - return; - } - - if (token.type != T_COMMA) - json_throw_parse_error(l, json, "comma or object end", &token); - - json_next_token(json, &token); - } -} - -/* Handle the array context */ -static void json_parse_array_context(lua_State *l, json_parse_t *json) -{ - json_token_t token; - int i; - - /* 2 slots required: - * .., table, value */ - json_decode_descend(l, json, 2); - - lua_newtable(l); - - json_next_token(json, &token); - - /* Handle empty arrays */ - if (token.type == T_ARR_END) { - json_decode_ascend(json); - return; - } - - for (i = 1; ; i++) { - json_process_value(l, json, &token); - lua_rawseti(l, -2, i); /* arr[i] = value */ - - json_next_token(json, &token); - - if (token.type == T_ARR_END) { - json_decode_ascend(json); - return; - } - - if (token.type != T_COMMA) - json_throw_parse_error(l, json, "comma or array end", &token); - - json_next_token(json, &token); - } -} - -/* Handle the "value" context */ -static void json_process_value(lua_State *l, json_parse_t *json, - json_token_t *token) -{ - switch (token->type) { - case T_STRING: - lua_pushlstring(l, token->value.string, token->string_len); - break;; - case T_NUMBER: - lua_pushnumber(l, token->value.number); - break;; - case T_BOOLEAN: - lua_pushboolean(l, token->value.boolean); - break;; - case T_OBJ_BEGIN: - json_parse_object_context(l, json); - break;; - case T_ARR_BEGIN: - json_parse_array_context(l, json); - break;; - case T_NULL: - /* In Lua, setting "t[k] = nil" will delete k from the table. - * Hence a NULL pointer lightuserdata object is used instead */ - lua_pushlightuserdata(l, NULL); - break;; - default: - json_throw_parse_error(l, json, "value", token); - } -} - -static int json_decode(lua_State *l) -{ - json_parse_t json; - json_token_t token; - size_t json_len; - - luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); - - json.cfg = json_fetch_config(l); - json.data = luaL_checklstring(l, 1, &json_len); - json.current_depth = 0; - json.ptr = json.data; - - /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) - * - * CJSON can support any simple data type, hence only the first - * character is guaranteed to be ASCII (at worst: '"'). This is - * still enough to detect whether the wrong encoding is in use. */ - if (json_len >= 2 && (!json.data[0] || !json.data[1])) - luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); - - /* Ensure the temporary buffer can hold the entire string. - * This means we no longer need to do length checks since the decoded - * string must be smaller than the entire json string */ - json.tmp = strbuf_new(json_len); - - json_next_token(&json, &token); - json_process_value(l, &json, &token); - - /* Ensure there is no more input left */ - json_next_token(&json, &token); - - if (token.type != T_END) - json_throw_parse_error(l, &json, "the end", &token); - - strbuf_free(json.tmp); - - return 1; -} - -/* ===== INITIALISATION ===== */ - -#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502 -/* Compatibility for Lua 5.1. - * - * luaL_setfuncs() is used to create a module table where the functions have - * json_config_t as their first upvalue. Code borrowed from Lua 5.2 source. */ -static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup) -{ - int i; - - luaL_checkstack(l, nup, "too many upvalues"); - for (; reg->name != NULL; reg++) { /* fill the table with given functions */ - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(l, -nup); - lua_pushcclosure(l, reg->func, nup); /* closure with those upvalues */ - lua_setfield(l, -(nup + 2), reg->name); - } - lua_pop(l, nup); /* remove upvalues */ -} -#endif - -/* Call target function in protected mode with all supplied args. - * Assumes target function only returns a single non-nil value. - * Convert and return thrown errors as: nil, "error message" */ -static int json_protect_conversion(lua_State *l) -{ - int err; - - /* Deliberately throw an error for invalid arguments */ - luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); - - /* pcall() the function stored as upvalue(1) */ - lua_pushvalue(l, lua_upvalueindex(1)); - lua_insert(l, 1); - err = lua_pcall(l, 1, 1, 0); - if (!err) - return 1; - - if (err == LUA_ERRRUN) { - lua_pushnil(l); - lua_insert(l, -2); - return 2; - } - - /* Since we are not using a custom error handler, the only remaining - * errors are memory related */ - return luaL_error(l, "Memory allocation error in CJSON protected call"); -} - -/* Return cjson module table */ -static int lua_cjson_new(lua_State *l) -{ - luaL_Reg reg[] = { - { "encode", json_encode }, - { "decode", json_decode }, - { "encode_sparse_array", json_cfg_encode_sparse_array }, - { "encode_max_depth", json_cfg_encode_max_depth }, - { "decode_max_depth", json_cfg_decode_max_depth }, - { "encode_number_precision", json_cfg_encode_number_precision }, - { "encode_keep_buffer", json_cfg_encode_keep_buffer }, - { "encode_invalid_numbers", json_cfg_encode_invalid_numbers }, - { "decode_invalid_numbers", json_cfg_decode_invalid_numbers }, - { "new", lua_cjson_new }, - { NULL, NULL } - }; - - /* Initialise number conversions */ - fpconv_init(); - - /* cjson module table */ - lua_newtable(l); - - /* Register functions with config data as upvalue */ - json_create_config(l); - luaL_setfuncs(l, reg, 1); - - /* Set cjson.null */ - lua_pushlightuserdata(l, NULL); - lua_setfield(l, -2, "null"); - - /* Set module name / version fields */ - lua_pushliteral(l, CJSON_MODNAME); - lua_setfield(l, -2, "_NAME"); - lua_pushliteral(l, CJSON_VERSION); - lua_setfield(l, -2, "_VERSION"); - - return 1; -} - -/* Return cjson.safe module table */ -static int lua_cjson_safe_new(lua_State *l) -{ - const char *func[] = { "decode", "encode", NULL }; - int i; - - lua_cjson_new(l); - - /* Fix new() method */ - lua_pushcfunction(l, lua_cjson_safe_new); - lua_setfield(l, -2, "new"); - - for (i = 0; func[i]; i++) { - lua_getfield(l, -1, func[i]); - lua_pushcclosure(l, json_protect_conversion, 1); - lua_setfield(l, -2, func[i]); - } - - return 1; -} - -int luaopen_cjson(lua_State *l) -{ - lua_cjson_new(l); - -#ifdef ENABLE_CJSON_GLOBAL - /* Register a global "cjson" table. */ - lua_pushvalue(l, -1); - lua_setglobal(l, CJSON_MODNAME); -#endif - - /* Return cjson table */ - return 1; -} - -int luaopen_cjson_safe(lua_State *l) -{ - lua_cjson_safe_new(l); - - /* Return cjson.safe table */ - return 1; -} - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/manual.html b/3rd/lua-cjson/manual.html deleted file mode 100644 index 7a1f2788..00000000 --- a/3rd/lua-cjson/manual.html +++ /dev/null @@ -1,1332 +0,0 @@ - - - - - -Lua CJSON 2.1.0 Manual - - - - - -

1. Overview

-
-

The Lua CJSON module provides JSON support for Lua.

-
-
-Features -
-
-
    -
  • -

    -Fast, standards compliant encoding/parsing routines -

    -
  • -
  • -

    -Full support for JSON with UTF-8, including decoding surrogate pairs -

    -
  • -
  • -

    -Optional run-time support for common exceptions to the JSON - specification (infinity, NaN,..) -

    -
  • -
  • -

    -No dependencies on other libraries -

    -
  • -
-
-
-Caveats -
-
-
    -
  • -

    -UTF-16 and UTF-32 are not supported -

    -
  • -
-
-
-

Lua CJSON is covered by the MIT license. Review the file LICENSE for -details.

-

The latest version of this software is available from the -Lua CJSON website.

-

Feel free to email me if you have any patches, suggestions, or comments.

-
-

2. Installation

-
-

Lua CJSON requires either Lua 5.1, Lua 5.2, or -LuaJIT to build.

-

The build method can be selected from 4 options:

-
-
-Make -
-
-

-Unix (including Linux, BSD, Mac OSX & Solaris), Windows -

-
-
-CMake -
-
-

-Unix, Windows -

-
-
-RPM -
-
-

-Linux -

-
-
-LuaRocks -
-
-

-Unix, Windows -

-
-
-

2.1. Make

-

The included Makefile has generic settings.

-

First, review and update the included makefile to suit your platform (if -required).

-

Next, build and install the module:

-
-
-
make install
-

Or install manually into your Lua module directory:

-
-
-
make
-cp cjson.so $LUA_MODULE_DIRECTORY
-

2.2. CMake

-

CMake can generate build configuration for many -different platforms (including Unix and Windows).

-

First, generate the makefile for your platform using CMake. If CMake is -unable to find Lua, manually set the LUA_DIR environment variable to -the base prefix of your Lua 5.1 installation.

-

While cmake is used in the example below, ccmake or cmake-gui may -be used to present an interface for changing the default build options.

-
-
-
mkdir build
-cd build
-# Optional: export LUA_DIR=$LUA51_PREFIX
-cmake ..
-

Next, build and install the module:

-
-
-
make install
-# Or:
-make
-cp cjson.so $LUA_MODULE_DIRECTORY
-

Review the -CMake documentation -for further details.

-

2.3. RPM

-

Linux distributions using RPM can create a package via -the included RPM spec file. Ensure the rpm-build package (or similar) -has been installed.

-

Build and install the module via RPM:

-
-
-
rpmbuild -tb lua-cjson-2.1.0.tar.gz
-rpm -Uvh $LUA_CJSON_RPM
-

2.4. LuaRocks

-

LuaRocks can be used to install and manage Lua -modules on a wide range of platforms (including Windows).

-

First, extract the Lua CJSON source package.

-

Next, install the module:

-
-
-
cd lua-cjson-2.1.0
-luarocks make
-
- - - -
-
Note
-
LuaRocks does not support platform specific configuration for Solaris. -On Solaris, you may need to manually uncomment USE_INTERNAL_ISINF in -the rockspec before building this module.
-
-

Review the LuaRocks documentation -for further details.

-

2.5. Build Options (#define)

-

Lua CJSON offers several #define build options to address portability -issues, and enable non-default features. Some build methods may -automatically set platform specific options if required. Other features -should be enabled manually.

-
-
-USE_INTERNAL_ISINF -
-
-

-Workaround for Solaris platforms missing isinf. -

-
-
-DISABLE_INVALID_NUMBERS -
-
-

-Recommended on platforms where strtod / - sprintf are not POSIX compliant (eg, Windows MinGW). Prevents - cjson.encode_invalid_numbers and cjson.decode_invalid_numbers from - being enabled. However, cjson.encode_invalid_numbers may still be - set to "null". When using the Lua CJSON built-in floating point - conversion this option is unnecessary and is ignored. -

-
-
-

2.5.1. Built-in floating point conversion

-

Lua CJSON may be built with David Gay’s -floating point conversion routines. This can -increase overall performance by up to 50% on some platforms when -converting a large amount of numeric data. However, this option reduces -portability and is disabled by default.

-
-
-USE_INTERNAL_FPCONV -
-
-

-Enable internal number conversion routines. -

-
-
-IEEE_BIG_ENDIAN -
-
-

-Must be set on big endian architectures. -

-
-
-MULTIPLE_THREADS -
-
-

-Must be set if Lua CJSON may be used in a - multi-threaded application. Requires the pthreads library. -

-
-
-
-

3. API (Functions)

-
-

3.1. Synopsis

-
-
-
-- Module instantiation
-local cjson = require "cjson"
-local cjson2 = cjson.new()
-local cjson_safe = require "cjson.safe"
-
--- Translate Lua value to/from JSON
-text = cjson.encode(value)
-value = cjson.decode(text)
-
--- Get and/or set Lua CJSON configuration
-setting = cjson.decode_invalid_numbers([setting])
-setting = cjson.encode_invalid_numbers([setting])
-keep = cjson.encode_keep_buffer([keep])
-depth = cjson.encode_max_depth([depth])
-depth = cjson.decode_max_depth([depth])
-convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
-

3.2. Module Instantiation

-
-
-
local cjson = require "cjson"
-local cjson2 = cjson.new()
-local cjson_safe = require "cjson.safe"
-

Import Lua CJSON via the Lua require function. Lua CJSON does not -register a global module table.

-

The cjson module will throw an error during JSON conversion if any -invalid data is encountered. Refer to cjson.encode -and cjson.decode for details.

-

The cjson.safe module behaves identically to the cjson module, -except when errors are encountered during JSON conversion. On error, the -cjson_safe.encode and cjson_safe.decode functions will return -nil followed by the error message.

-

cjson.new can be used to instantiate an independent copy of the Lua -CJSON module. The new module has a separate persistent encoding buffer, -and default settings.

-

Lua CJSON can support Lua implementations using multiple preemptive -threads within a single Lua state provided the persistent encoding -buffer is not shared. This can be achieved by one of the following -methods:

-
    -
  • -

    -Disabling the persistent encoding buffer with - cjson.encode_keep_buffer -

    -
  • -
  • -

    -Ensuring each thread calls cjson.encode separately (ie, - treat cjson.encode as non-reentrant). -

    -
  • -
  • -

    -Using a separate cjson module table per preemptive thread - (cjson.new) -

    -
  • -
-
- - - -
-
Note
-
Lua CJSON uses strtod and snprintf to perform numeric conversion as -they are usually well supported, fast and bug free. However, these -functions require a workaround for JSON encoding/parsing under locales -using a comma decimal separator. Lua CJSON detects the current locale -during instantiation to determine and automatically implement the -workaround if required. Lua CJSON should be reinitialised via -cjson.new if the locale of the current process changes. Using a -different locale per thread is not supported.
-
-

3.3. decode

-
-
-
value = cjson.decode(json_text)
-

cjson.decode will deserialise any UTF-8 JSON string into a Lua value -or table.

-

UTF-16 and UTF-32 JSON strings are not supported.

-

cjson.decode requires that any NULL (ASCII 0) and double quote (ASCII -34) characters are escaped within strings. All escape codes will be -decoded and other bytes will be passed transparently. UTF-8 characters -are not validated during decoding and should be checked elsewhere if -required.

-

JSON null will be converted to a NULL lightuserdata value. This can -be compared with cjson.null for convenience.

-

By default, numbers incompatible with the JSON specification (infinity, -NaN, hexadecimal) can be decoded. This default can be changed with -cjson.decode_invalid_numbers.

-
-
Example: Decoding
-
-
json_text = '[ true, { "foo": "bar" } ]'
-value = cjson.decode(json_text)
--- Returns: { true, { foo = "bar" } }
-
- - - -
-
Caution
-
Care must be taken after decoding JSON objects with numeric keys. Each -numeric key will be stored as a Lua string. Any subsequent code -assuming type number may break.
-
-

3.4. decode_invalid_numbers

-
-
-
setting = cjson.decode_invalid_numbers([setting])
--- "setting" must be a boolean. Default: true.
-

Lua CJSON may generate an error when trying to decode numbers not -supported by the JSON specification. Invalid numbers are defined as:

-
    -
  • -

    -infinity -

    -
  • -
  • -

    -not-a-number (NaN) -

    -
  • -
  • -

    -hexadecimal -

    -
  • -
-

Available settings:

-
-
-true -
-
-

-Accept and decode invalid numbers. This is the default - setting. -

-
-
-false -
-
-

-Throw an error when invalid numbers are encountered. -

-
-
-

The current setting is always returned, and is only updated when an -argument is provided.

-

3.5. decode_max_depth

-
-
-
depth = cjson.decode_max_depth([depth])
--- "depth" must be a positive integer. Default: 1000.
-

Lua CJSON will generate an error when parsing deeply nested JSON once -the maximum array/object depth has been exceeded. This check prevents -unnecessarily complicated JSON from slowing down the application, or -crashing the application due to lack of process stack space.

-

An error may be generated before the depth limit is hit if Lua is unable -to allocate more objects on the Lua stack.

-

By default, Lua CJSON will reject JSON with arrays and/or objects nested -more than 1000 levels deep.

-

The current setting is always returned, and is only updated when an -argument is provided.

-

3.6. encode

-
-
-
json_text = cjson.encode(value)
-

cjson.encode will serialise a Lua value into a string containing the -JSON representation.

-

cjson.encode supports the following types:

-
    -
  • -

    -boolean -

    -
  • -
  • -

    -lightuserdata (NULL value only) -

    -
  • -
  • -

    -nil -

    -
  • -
  • -

    -number -

    -
  • -
  • -

    -string -

    -
  • -
  • -

    -table -

    -
  • -
-

The remaining Lua types will generate an error:

-
    -
  • -

    -function -

    -
  • -
  • -

    -lightuserdata (non-NULL values) -

    -
  • -
  • -

    -thread -

    -
  • -
  • -

    -userdata -

    -
  • -
-

By default, numbers are encoded with 14 significant digits. Refer to -cjson.encode_number_precision for details.

-

Lua CJSON will escape the following characters within each UTF-8 string:

-
    -
  • -

    -Control characters (ASCII 0 - 31) -

    -
  • -
  • -

    -Double quote (ASCII 34) -

    -
  • -
  • -

    -Forward slash (ASCII 47) -

    -
  • -
  • -

    -Blackslash (ASCII 92) -

    -
  • -
  • -

    -Delete (ASCII 127) -

    -
  • -
-

All other bytes are passed transparently.

-
- - - -
-
Caution
-
-

Lua CJSON will successfully encode/decode binary strings, but this is -technically not supported by JSON and may not be compatible with other -JSON libraries. To ensure the output is valid JSON, applications should -ensure all Lua strings passed to cjson.encode are UTF-8.

-

Base64 is commonly used to encode binary data as the most efficient -encoding under UTF-8 can only reduce the encoded size by a further -~8%. Lua Base64 routines can be found in the -LuaSocket and -lbase64 packages.

-
-
-

Lua CJSON uses a heuristic to determine whether to encode a Lua table as -a JSON array or an object. A Lua table with only positive integer keys -of type number will be encoded as a JSON array. All other tables will -be encoded as a JSON object.

-

Lua CJSON does not use metamethods when serialising tables.

-
    -
  • -

    -rawget is used to iterate over Lua arrays -

    -
  • -
  • -

    -next is used to iterate over Lua objects -

    -
  • -
-

Lua arrays with missing entries (sparse arrays) may optionally be -encoded in several different ways. Refer to -cjson.encode_sparse_array for details.

-

JSON object keys are always strings. Hence cjson.encode only supports -table keys which are type number or string. All other types will -generate an error.

-
- - - -
-
Note
-
Standards compliant JSON must be encapsulated in either an object ({}) -or an array ([]). If strictly standards compliant JSON is desired, a -table must be passed to cjson.encode.
-
-

By default, encoding the following Lua values will generate errors:

-
    -
  • -

    -Numbers incompatible with the JSON specification (infinity, NaN) -

    -
  • -
  • -

    -Tables nested more than 1000 levels deep -

    -
  • -
  • -

    -Excessively sparse Lua arrays -

    -
  • -
-

These defaults can be changed with:

- -
-
Example: Encoding
-
-
value = { true, { foo = "bar" } }
-json_text = cjson.encode(value)
--- Returns: '[true,{"foo":"bar"}]'
-

3.7. encode_invalid_numbers

-
-
-
setting = cjson.encode_invalid_numbers([setting])
--- "setting" must a boolean or "null". Default: false.
-

Lua CJSON may generate an error when encoding floating point numbers not -supported by the JSON specification (invalid numbers):

-
    -
  • -

    -infinity -

    -
  • -
  • -

    -not-a-number (NaN) -

    -
  • -
-

Available settings:

-
-
-true -
-
-

-Allow invalid numbers to be encoded. This will generate - non-standard JSON, but this output is supported by some libraries. -

-
-
-"null" -
-
-

-Encode invalid numbers as a JSON null value. This allows - infinity and NaN to be encoded into valid JSON. -

-
-
-false -
-
-

-Throw an error when attempting to encode invalid numbers. - This is the default setting. -

-
-
-

The current setting is always returned, and is only updated when an -argument is provided.

-

3.8. encode_keep_buffer

-
-
-
keep = cjson.encode_keep_buffer([keep])
--- "keep" must be a boolean. Default: true.
-

Lua CJSON can reuse the JSON encoding buffer to improve performance.

-

Available settings:

-
-
-true -
-
-

-The buffer will grow to the largest size required and is not - freed until the Lua CJSON module is garbage collected. This is the - default setting. -

-
-
-false -
-
-

-Free the encode buffer after each call to cjson.encode. -

-
-
-

The current setting is always returned, and is only updated when an -argument is provided.

-

3.9. encode_max_depth

-
-
-
depth = cjson.encode_max_depth([depth])
--- "depth" must be a positive integer. Default: 1000.
-

Once the maximum table depth has been exceeded Lua CJSON will generate -an error. This prevents a deeply nested or recursive data structure from -crashing the application.

-

By default, Lua CJSON will generate an error when trying to encode data -structures with more than 1000 nested tables.

-

The current setting is always returned, and is only updated when an -argument is provided.

-
-
Example: Recursive Lua table
-
-
a = {}; a[1] = a
-

3.10. encode_number_precision

-
-
-
precision = cjson.encode_number_precision([precision])
--- "precision" must be an integer between 1 and 14. Default: 14.
-

The amount of significant digits returned by Lua CJSON when encoding -numbers can be changed to balance accuracy versus performance. For data -structures containing many numbers, setting -cjson.encode_number_precision to a smaller integer, for example 3, -can improve encoding performance by up to 50%.

-

By default, Lua CJSON will output 14 significant digits when converting -a number to text.

-

The current setting is always returned, and is only updated when an -argument is provided.

-

3.11. encode_sparse_array

-
-
-
convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
--- "convert" must be a boolean. Default: false.
--- "ratio" must be a positive integer. Default: 2.
--- "safe" must be a positive integer. Default: 10.
-

Lua CJSON classifies a Lua table into one of three kinds when encoding a -JSON array. This is determined by the number of values missing from the -Lua array as follows:

-
-
-Normal -
-
-

-All values are available. -

-
-
-Sparse -
-
-

-At least 1 value is missing. -

-
-
-Excessively sparse -
-
-

-The number of values missing exceeds the configured - ratio. -

-
-
-

Lua CJSON encodes sparse Lua arrays as JSON arrays using JSON null for -the missing entries.

-

An array is excessively sparse when all the following conditions are -met:

-
    -
  • -

    -ratio > 0 -

    -
  • -
  • -

    -maximum_index > safe -

    -
  • -
  • -

    -maximum_index > item_count * ratio -

    -
  • -
-

Lua CJSON will never consider an array to be excessively sparse when -ratio = 0. The safe limit ensures that small Lua arrays are always -encoded as sparse arrays.

-

By default, attempting to encode an excessively sparse array will -generate an error. If convert is set to true, excessively sparse -arrays will be converted to a JSON object.

-

The current settings are always returned. A particular setting is only -changed when the argument is provided (non-nil).

-
-
Example: Encoding a sparse array
-
-
cjson.encode({ [3] = "data" })
--- Returns: '[null,null,"data"]'
-
-
Example: Enabling conversion to a JSON object
-
-
cjson.encode_sparse_array(true)
-cjson.encode({ [1000] = "excessively sparse" })
--- Returns: '{"1000":"excessively sparse"}'
-
-

4. API (Variables)

-
-

4.1. _NAME

-

The name of the Lua CJSON module ("cjson").

-

4.2. _VERSION

-

The version number of the Lua CJSON module ("2.1.0").

-

4.3. null

-

Lua CJSON decodes JSON null as a Lua lightuserdata NULL pointer. -cjson.null is provided for comparison.

-
-

5. References

-
-
-
- - - diff --git a/3rd/lua-cjson/manual.txt b/3rd/lua-cjson/manual.txt deleted file mode 100644 index 8c11f4be..00000000 --- a/3rd/lua-cjson/manual.txt +++ /dev/null @@ -1,611 +0,0 @@ -= Lua CJSON 2.1.0 Manual = -Mark Pulford -:revdate: 1st March 2012 - -Overview --------- - -The Lua CJSON module provides JSON support for Lua. - -*Features*:: -- Fast, standards compliant encoding/parsing routines -- Full support for JSON with UTF-8, including decoding surrogate pairs -- Optional run-time support for common exceptions to the JSON - specification (infinity, NaN,..) -- No dependencies on other libraries - -*Caveats*:: -- UTF-16 and UTF-32 are not supported - -Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for -details. - -The latest version of this software is available from the -http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CJSON website]. - -Feel free to email me if you have any patches, suggestions, or comments. - - -Installation ------------- - -Lua CJSON requires either http://www.lua.org[Lua] 5.1, Lua 5.2, or -http://www.luajit.org[LuaJIT] to build. - -The build method can be selected from 4 options: - -Make:: Unix (including Linux, BSD, Mac OSX & Solaris), Windows -CMake:: Unix, Windows -RPM:: Linux -LuaRocks:: Unix, Windows - - -Make -~~~~ - -The included +Makefile+ has generic settings. - -First, review and update the included makefile to suit your platform (if -required). - -Next, build and install the module: - -[source,sh] -make install - -Or install manually into your Lua module directory: - -[source,sh] -make -cp cjson.so $LUA_MODULE_DIRECTORY - - -CMake -~~~~~ - -http://www.cmake.org[CMake] can generate build configuration for many -different platforms (including Unix and Windows). - -First, generate the makefile for your platform using CMake. If CMake is -unable to find Lua, manually set the +LUA_DIR+ environment variable to -the base prefix of your Lua 5.1 installation. - -While +cmake+ is used in the example below, +ccmake+ or +cmake-gui+ may -be used to present an interface for changing the default build options. - -[source,sh] -mkdir build -cd build -# Optional: export LUA_DIR=$LUA51_PREFIX -cmake .. - -Next, build and install the module: - -[source,sh] -make install -# Or: -make -cp cjson.so $LUA_MODULE_DIRECTORY - -Review the -http://www.cmake.org/cmake/help/documentation.html[CMake documentation] -for further details. - - -RPM -~~~ - -Linux distributions using http://rpm.org[RPM] can create a package via -the included RPM spec file. Ensure the +rpm-build+ package (or similar) -has been installed. - -Build and install the module via RPM: - -[source,sh] -rpmbuild -tb lua-cjson-2.1.0.tar.gz -rpm -Uvh $LUA_CJSON_RPM - - -LuaRocks -~~~~~~~~ - -http://luarocks.org[LuaRocks] can be used to install and manage Lua -modules on a wide range of platforms (including Windows). - -First, extract the Lua CJSON source package. - -Next, install the module: - -[source,sh] -cd lua-cjson-2.1.0 -luarocks make - -[NOTE] -LuaRocks does not support platform specific configuration for Solaris. -On Solaris, you may need to manually uncomment +USE_INTERNAL_ISINF+ in -the rockspec before building this module. - -Review the http://luarocks.org/en/Documentation[LuaRocks documentation] -for further details. - - -[[build_options]] -Build Options (#define) -~~~~~~~~~~~~~~~~~~~~~~~ - -Lua CJSON offers several +#define+ build options to address portability -issues, and enable non-default features. Some build methods may -automatically set platform specific options if required. Other features -should be enabled manually. - -USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing +isinf+. -DISABLE_INVALID_NUMBERS:: Recommended on platforms where +strtod+ / - +sprintf+ are not POSIX compliant (eg, Windows MinGW). Prevents - +cjson.encode_invalid_numbers+ and +cjson.decode_invalid_numbers+ from - being enabled. However, +cjson.encode_invalid_numbers+ may still be - set to +"null"+. When using the Lua CJSON built-in floating point - conversion this option is unnecessary and is ignored. - - -Built-in floating point conversion -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Lua CJSON may be built with David Gay's -http://www.netlib.org/fp/[floating point conversion routines]. This can -increase overall performance by up to 50% on some platforms when -converting a large amount of numeric data. However, this option reduces -portability and is disabled by default. - -USE_INTERNAL_FPCONV:: Enable internal number conversion routines. -IEEE_BIG_ENDIAN:: Must be set on big endian architectures. -MULTIPLE_THREADS:: Must be set if Lua CJSON may be used in a - multi-threaded application. Requires the _pthreads_ library. - - -API (Functions) ---------------- - -Synopsis -~~~~~~~~ - -[source,lua] ------------- --- Module instantiation -local cjson = require "cjson" -local cjson2 = cjson.new() -local cjson_safe = require "cjson.safe" - --- Translate Lua value to/from JSON -text = cjson.encode(value) -value = cjson.decode(text) - --- Get and/or set Lua CJSON configuration -setting = cjson.decode_invalid_numbers([setting]) -setting = cjson.encode_invalid_numbers([setting]) -keep = cjson.encode_keep_buffer([keep]) -depth = cjson.encode_max_depth([depth]) -depth = cjson.decode_max_depth([depth]) -convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) ------------- - - -Module Instantiation -~~~~~~~~~~~~~~~~~~~~ - -[source,lua] ------------- -local cjson = require "cjson" -local cjson2 = cjson.new() -local cjson_safe = require "cjson.safe" ------------- - -Import Lua CJSON via the Lua +require+ function. Lua CJSON does not -register a global module table. - -The +cjson+ module will throw an error during JSON conversion if any -invalid data is encountered. Refer to <> -and <> for details. - -The +cjson.safe+ module behaves identically to the +cjson+ module, -except when errors are encountered during JSON conversion. On error, the -+cjson_safe.encode+ and +cjson_safe.decode+ functions will return -+nil+ followed by the error message. - -+cjson.new+ can be used to instantiate an independent copy of the Lua -CJSON module. The new module has a separate persistent encoding buffer, -and default settings. - -Lua CJSON can support Lua implementations using multiple preemptive -threads within a single Lua state provided the persistent encoding -buffer is not shared. This can be achieved by one of the following -methods: - -- Disabling the persistent encoding buffer with - <> -- Ensuring each thread calls <> separately (ie, - treat +cjson.encode+ as non-reentrant). -- Using a separate +cjson+ module table per preemptive thread - (+cjson.new+) - -[NOTE] -Lua CJSON uses +strtod+ and +snprintf+ to perform numeric conversion as -they are usually well supported, fast and bug free. However, these -functions require a workaround for JSON encoding/parsing under locales -using a comma decimal separator. Lua CJSON detects the current locale -during instantiation to determine and automatically implement the -workaround if required. Lua CJSON should be reinitialised via -+cjson.new+ if the locale of the current process changes. Using a -different locale per thread is not supported. - - -decode -~~~~~~ - -[source,lua] ------------- -value = cjson.decode(json_text) ------------- - -+cjson.decode+ will deserialise any UTF-8 JSON string into a Lua value -or table. - -UTF-16 and UTF-32 JSON strings are not supported. - -+cjson.decode+ requires that any NULL (ASCII 0) and double quote (ASCII -34) characters are escaped within strings. All escape codes will be -decoded and other bytes will be passed transparently. UTF-8 characters -are not validated during decoding and should be checked elsewhere if -required. - -JSON +null+ will be converted to a NULL +lightuserdata+ value. This can -be compared with +cjson.null+ for convenience. - -By default, numbers incompatible with the JSON specification (infinity, -NaN, hexadecimal) can be decoded. This default can be changed with -<>. - -.Example: Decoding -[source,lua] -json_text = '[ true, { "foo": "bar" } ]' -value = cjson.decode(json_text) --- Returns: { true, { foo = "bar" } } - -[CAUTION] -Care must be taken after decoding JSON objects with numeric keys. Each -numeric key will be stored as a Lua +string+. Any subsequent code -assuming type +number+ may break. - - -[[decode_invalid_numbers]] -decode_invalid_numbers -~~~~~~~~~~~~~~~~~~~~~~ - -[source,lua] ------------- -setting = cjson.decode_invalid_numbers([setting]) --- "setting" must be a boolean. Default: true. ------------- - -Lua CJSON may generate an error when trying to decode numbers not -supported by the JSON specification. _Invalid numbers_ are defined as: - -- infinity -- not-a-number (NaN) -- hexadecimal - -Available settings: - -+true+:: Accept and decode _invalid numbers_. This is the default - setting. -+false+:: Throw an error when _invalid numbers_ are encountered. - -The current setting is always returned, and is only updated when an -argument is provided. - - -[[decode_max_depth]] -decode_max_depth -~~~~~~~~~~~~~~~~ - -[source,lua] ------------- -depth = cjson.decode_max_depth([depth]) --- "depth" must be a positive integer. Default: 1000. ------------- - -Lua CJSON will generate an error when parsing deeply nested JSON once -the maximum array/object depth has been exceeded. This check prevents -unnecessarily complicated JSON from slowing down the application, or -crashing the application due to lack of process stack space. - -An error may be generated before the depth limit is hit if Lua is unable -to allocate more objects on the Lua stack. - -By default, Lua CJSON will reject JSON with arrays and/or objects nested -more than 1000 levels deep. - -The current setting is always returned, and is only updated when an -argument is provided. - - -[[encode]] -encode -~~~~~~ - -[source,lua] ------------- -json_text = cjson.encode(value) ------------- - -+cjson.encode+ will serialise a Lua value into a string containing the -JSON representation. - -+cjson.encode+ supports the following types: - -- +boolean+ -- +lightuserdata+ (NULL value only) -- +nil+ -- +number+ -- +string+ -- +table+ - -The remaining Lua types will generate an error: - -- +function+ -- +lightuserdata+ (non-NULL values) -- +thread+ -- +userdata+ - -By default, numbers are encoded with 14 significant digits. Refer to -<> for details. - -Lua CJSON will escape the following characters within each UTF-8 string: - -- Control characters (ASCII 0 - 31) -- Double quote (ASCII 34) -- Forward slash (ASCII 47) -- Blackslash (ASCII 92) -- Delete (ASCII 127) - -All other bytes are passed transparently. - -[CAUTION] -========= -Lua CJSON will successfully encode/decode binary strings, but this is -technically not supported by JSON and may not be compatible with other -JSON libraries. To ensure the output is valid JSON, applications should -ensure all Lua strings passed to +cjson.encode+ are UTF-8. - -Base64 is commonly used to encode binary data as the most efficient -encoding under UTF-8 can only reduce the encoded size by a further -~8%. Lua Base64 routines can be found in the -http://w3.impa.br/%7Ediego/software/luasocket/[LuaSocket] and -http://www.tecgraf.puc-rio.br/%7Elhf/ftp/lua/#lbase64[lbase64] packages. -========= - -Lua CJSON uses a heuristic to determine whether to encode a Lua table as -a JSON array or an object. A Lua table with only positive integer keys -of type +number+ will be encoded as a JSON array. All other tables will -be encoded as a JSON object. - -Lua CJSON does not use metamethods when serialising tables. - -- +rawget+ is used to iterate over Lua arrays -- +next+ is used to iterate over Lua objects - -Lua arrays with missing entries (_sparse arrays_) may optionally be -encoded in several different ways. Refer to -<> for details. - -JSON object keys are always strings. Hence +cjson.encode+ only supports -table keys which are type +number+ or +string+. All other types will -generate an error. - -[NOTE] -Standards compliant JSON must be encapsulated in either an object (+{}+) -or an array (+[]+). If strictly standards compliant JSON is desired, a -table must be passed to +cjson.encode+. - -By default, encoding the following Lua values will generate errors: - -- Numbers incompatible with the JSON specification (infinity, NaN) -- Tables nested more than 1000 levels deep -- Excessively sparse Lua arrays - -These defaults can be changed with: - -- <> -- <> -- <> - -.Example: Encoding -[source,lua] -value = { true, { foo = "bar" } } -json_text = cjson.encode(value) --- Returns: '[true,{"foo":"bar"}]' - - -[[encode_invalid_numbers]] -encode_invalid_numbers -~~~~~~~~~~~~~~~~~~~~~~ -[source,lua] ------------- -setting = cjson.encode_invalid_numbers([setting]) --- "setting" must a boolean or "null". Default: false. ------------- - -Lua CJSON may generate an error when encoding floating point numbers not -supported by the JSON specification (_invalid numbers_): - -- infinity -- not-a-number (NaN) - -Available settings: - -+true+:: Allow _invalid numbers_ to be encoded. This will generate - non-standard JSON, but this output is supported by some libraries. -+"null"+:: Encode _invalid numbers_ as a JSON +null+ value. This allows - infinity and NaN to be encoded into valid JSON. -+false+:: Throw an error when attempting to encode _invalid numbers_. - This is the default setting. - -The current setting is always returned, and is only updated when an -argument is provided. - - -[[encode_keep_buffer]] -encode_keep_buffer -~~~~~~~~~~~~~~~~~~ - -[source,lua] ------------- -keep = cjson.encode_keep_buffer([keep]) --- "keep" must be a boolean. Default: true. ------------- - -Lua CJSON can reuse the JSON encoding buffer to improve performance. - -Available settings: - -+true+:: The buffer will grow to the largest size required and is not - freed until the Lua CJSON module is garbage collected. This is the - default setting. -+false+:: Free the encode buffer after each call to +cjson.encode+. - -The current setting is always returned, and is only updated when an -argument is provided. - - -[[encode_max_depth]] -encode_max_depth -~~~~~~~~~~~~~~~~ - -[source,lua] ------------- -depth = cjson.encode_max_depth([depth]) --- "depth" must be a positive integer. Default: 1000. ------------- - -Once the maximum table depth has been exceeded Lua CJSON will generate -an error. This prevents a deeply nested or recursive data structure from -crashing the application. - -By default, Lua CJSON will generate an error when trying to encode data -structures with more than 1000 nested tables. - -The current setting is always returned, and is only updated when an -argument is provided. - -.Example: Recursive Lua table -[source,lua] -a = {}; a[1] = a - - -[[encode_number_precision]] -encode_number_precision -~~~~~~~~~~~~~~~~~~~~~~~ - -[source,lua] ------------- -precision = cjson.encode_number_precision([precision]) --- "precision" must be an integer between 1 and 14. Default: 14. ------------- - -The amount of significant digits returned by Lua CJSON when encoding -numbers can be changed to balance accuracy versus performance. For data -structures containing many numbers, setting -+cjson.encode_number_precision+ to a smaller integer, for example +3+, -can improve encoding performance by up to 50%. - -By default, Lua CJSON will output 14 significant digits when converting -a number to text. - -The current setting is always returned, and is only updated when an -argument is provided. - - -[[encode_sparse_array]] -encode_sparse_array -~~~~~~~~~~~~~~~~~~~ - -[source,lua] ------------- -convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) --- "convert" must be a boolean. Default: false. --- "ratio" must be a positive integer. Default: 2. --- "safe" must be a positive integer. Default: 10. ------------- - -Lua CJSON classifies a Lua table into one of three kinds when encoding a -JSON array. This is determined by the number of values missing from the -Lua array as follows: - -Normal:: All values are available. -Sparse:: At least 1 value is missing. -Excessively sparse:: The number of values missing exceeds the configured - ratio. - -Lua CJSON encodes sparse Lua arrays as JSON arrays using JSON +null+ for -the missing entries. - -An array is excessively sparse when all the following conditions are -met: - -- +ratio+ > +0+ -- _maximum_index_ > +safe+ -- _maximum_index_ > _item_count_ * +ratio+ - -Lua CJSON will never consider an array to be _excessively sparse_ when -+ratio+ = +0+. The +safe+ limit ensures that small Lua arrays are always -encoded as sparse arrays. - -By default, attempting to encode an _excessively sparse_ array will -generate an error. If +convert+ is set to +true+, _excessively sparse_ -arrays will be converted to a JSON object. - -The current settings are always returned. A particular setting is only -changed when the argument is provided (non-++nil++). - -.Example: Encoding a sparse array -[source,lua] -cjson.encode({ [3] = "data" }) --- Returns: '[null,null,"data"]' - -.Example: Enabling conversion to a JSON object -[source,lua] -cjson.encode_sparse_array(true) -cjson.encode({ [1000] = "excessively sparse" }) --- Returns: '{"1000":"excessively sparse"}' - - -API (Variables) ---------------- - -_NAME -~~~~~ - -The name of the Lua CJSON module (+"cjson"+). - - -_VERSION -~~~~~~~~ - -The version number of the Lua CJSON module (+"2.1.0"+). - - -null -~~~~ - -Lua CJSON decodes JSON +null+ as a Lua +lightuserdata+ NULL pointer. -+cjson.null+ is provided for comparison. - - -[sect1] -References ----------- - -- http://tools.ietf.org/html/rfc4627[RFC 4627] -- http://www.json.org/[JSON website] - - -// vi:ft=asciidoc tw=72: diff --git a/3rd/lua-cjson/performance.html b/3rd/lua-cjson/performance.html deleted file mode 100644 index 19edf72d..00000000 --- a/3rd/lua-cjson/performance.html +++ /dev/null @@ -1,617 +0,0 @@ - - - - - -JSON module performance comparison under Lua - - - - - -
-
-

This performance comparison aims to provide a guide of relative -performance between several fast and popular JSON modules.

-

The examples used in this comparison were mostly sourced from the -JSON website and -RFC 4627.

-

Performance will vary widely between platforms and data sets. These -results should only be used as an approximation.

-
-
-

1. Modules

-
-

The following JSON modules for Lua were tested:

-
-
-DKJSON 2.1 -
-
-
    -
  • -

    -Lua implementation with no dependencies on other libraries -

    -
  • -
  • -

    -Supports LPeg to improve decode performance -

    -
  • -
-
-
-Lua YAJL 2.0 -
-
-
    -
  • -

    -C wrapper for the YAJL library -

    -
  • -
-
-
-Lua CSJON 2.0.0 -
-
-
    -
  • -

    -C implementation with no dependencies on other libraries -

    -
  • -
-
-
-
-

2. Summary

-
-

All modules were built and tested as follows:

-
-
-DKJSON -
-
-

-Tested with/without LPeg 10.2. -

-
-
-Lua YAJL -
-
-

-Tested with YAJL 2.0.4. -

-
-
-Lua CJSON -
-
-

-Tested with Libc and internal floating point conversion - routines. -

-
-
-

The following Lua implementations were used for this comparison:

-
-

These results show the number of JSON operations per second sustained by -each module. All results have been normalised against the pure Lua -DKJSON implementation.

-
-
Decoding performance
-
-
             | DKJSON                  | Lua YAJL   | Lua CJSON
-             | No LPeg     With LPeg   |            | Libc         Internal
-             | Lua  JIT    Lua    JIT  | Lua   JIT  | Lua   JIT    Lua   JIT
-example1     | 1x   2x     2.6x   3.4x | 7.1x  10x  | 14x   20x    14x   20x
-example2     | 1x   2.2x   2.9x   4.4x | 6.7x  9.9x | 14x   22x    14x   22x
-example3     | 1x   2.1x   3x     4.3x | 6.9x  9.3x | 14x   21x    15x   22x
-example4     | 1x   2x     2.5x   3.7x | 7.3x  10x  | 12x   19x    12x   20x
-example5     | 1x   2.2x   3x     4.5x | 7.8x  11x  | 16x   24x    16x   24x
-numbers      | 1x   2.2x   2.3x   4x   | 4.6x  5.5x | 8.9x  10x    13x   17x
-rfc-example1 | 1x   2.1x   2.8x   4.3x | 6.1x  8.1x | 13x   19x    14x   21x
-rfc-example2 | 1x   2.1x   3.1x   4.2x | 7.1x  9.2x | 15x   21x    17x   24x
-types        | 1x   2.2x   2.6x   4.3x | 5.3x  7.4x | 12x   20x    13x   21x
--------------|-------------------------|------------|-----------------------
-= Average => | 1x   2.1x   2.7x   4.1x | 6.5x  9x   | 13x   20x    14x   21x
-
-
-
Encoding performance
-
-
             | DKJSON                  | Lua YAJL   | Lua CJSON
-             | No LPeg     With LPeg   |            | Libc         Internal
-             | Lua  JIT    Lua    JIT  | Lua   JIT  | Lua   JIT    Lua   JIT
-example1     | 1x   1.8x   0.97x  1.6x | 3.1x  5.2x | 23x   29x    23x   29x
-example2     | 1x   2x     0.97x  1.7x | 2.6x  4.3x | 22x   28x    22x   28x
-example3     | 1x   1.9x   0.98x  1.6x | 2.8x  4.3x | 13x   15x    16x   18x
-example4     | 1x   1.7x   0.96x  1.3x | 3.9x  6.1x | 15x   19x    17x   21x
-example5     | 1x   2x     0.98x  1.7x | 2.7x  4.5x | 20x   23x    20x   23x
-numbers      | 1x   2.3x   1x     2.2x | 1.3x  1.9x | 3.8x  4.1x   4.2x  4.6x
-rfc-example1 | 1x   1.9x   0.97x  1.6x | 2.2x  3.2x | 8.5x  9.3x   11x   12x
-rfc-example2 | 1x   1.9x   0.98x  1.6x | 2.6x  3.9x | 10x   11x    17x   19x
-types        | 1x   2.2x   0.97x  2x   | 1.2x  1.9x | 11x   13x    12x   14x
--------------|-------------------------|------------|-----------------------
-= Average => | 1x   1.9x   0.98x  1.7x | 2.5x  3.9x | 14x   17x    16x   19x
-
-
- - - diff --git a/3rd/lua-cjson/performance.txt b/3rd/lua-cjson/performance.txt deleted file mode 100644 index fc3a5bb5..00000000 --- a/3rd/lua-cjson/performance.txt +++ /dev/null @@ -1,89 +0,0 @@ -JSON module performance comparison under Lua -============================================ -Mark Pulford -:revdate: January 22, 2012 - -This performance comparison aims to provide a guide of relative -performance between several fast and popular JSON modules. - -The examples used in this comparison were mostly sourced from the -http://json.org[JSON website] and -http://tools.ietf.org/html/rfc4627[RFC 4627]. - -Performance will vary widely between platforms and data sets. These -results should only be used as an approximation. - - -Modules -------- - -The following JSON modules for Lua were tested: - -http://chiselapp.com/user/dhkolf/repository/dkjson/[DKJSON 2.1]:: - - Lua implementation with no dependencies on other libraries - - Supports LPeg to improve decode performance - -https://github.com/brimworks/lua-yajl[Lua YAJL 2.0]:: - - C wrapper for the YAJL library - -http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CSJON 2.0.0]:: - - C implementation with no dependencies on other libraries - - -Summary -------- - -All modules were built and tested as follows: - -DKJSON:: Tested with/without LPeg 10.2. -Lua YAJL:: Tested with YAJL 2.0.4. -Lua CJSON:: Tested with Libc and internal floating point conversion - routines. - -The following Lua implementations were used for this comparison: - -- http://www.lua.org[Lua 5.1.4] (_Lua_) -- http://www.luajit.org[LuaJIT 2.0.0-beta9] (_JIT_) - -These results show the number of JSON operations per second sustained by -each module. All results have been normalised against the pure Lua -DKJSON implementation. - -.Decoding performance -............................................................................ - | DKJSON | Lua YAJL | Lua CJSON - | No LPeg With LPeg | | Libc Internal - | Lua JIT Lua JIT | Lua JIT | Lua JIT Lua JIT -example1 | 1x 2x 2.6x 3.4x | 7.1x 10x | 14x 20x 14x 20x -example2 | 1x 2.2x 2.9x 4.4x | 6.7x 9.9x | 14x 22x 14x 22x -example3 | 1x 2.1x 3x 4.3x | 6.9x 9.3x | 14x 21x 15x 22x -example4 | 1x 2x 2.5x 3.7x | 7.3x 10x | 12x 19x 12x 20x -example5 | 1x 2.2x 3x 4.5x | 7.8x 11x | 16x 24x 16x 24x -numbers | 1x 2.2x 2.3x 4x | 4.6x 5.5x | 8.9x 10x 13x 17x -rfc-example1 | 1x 2.1x 2.8x 4.3x | 6.1x 8.1x | 13x 19x 14x 21x -rfc-example2 | 1x 2.1x 3.1x 4.2x | 7.1x 9.2x | 15x 21x 17x 24x -types | 1x 2.2x 2.6x 4.3x | 5.3x 7.4x | 12x 20x 13x 21x --------------|-------------------------|------------|----------------------- -= Average => | 1x 2.1x 2.7x 4.1x | 6.5x 9x | 13x 20x 14x 21x -............................................................................ - -.Encoding performance -............................................................................. - | DKJSON | Lua YAJL | Lua CJSON - | No LPeg With LPeg | | Libc Internal - | Lua JIT Lua JIT | Lua JIT | Lua JIT Lua JIT -example1 | 1x 1.8x 0.97x 1.6x | 3.1x 5.2x | 23x 29x 23x 29x -example2 | 1x 2x 0.97x 1.7x | 2.6x 4.3x | 22x 28x 22x 28x -example3 | 1x 1.9x 0.98x 1.6x | 2.8x 4.3x | 13x 15x 16x 18x -example4 | 1x 1.7x 0.96x 1.3x | 3.9x 6.1x | 15x 19x 17x 21x -example5 | 1x 2x 0.98x 1.7x | 2.7x 4.5x | 20x 23x 20x 23x -numbers | 1x 2.3x 1x 2.2x | 1.3x 1.9x | 3.8x 4.1x 4.2x 4.6x -rfc-example1 | 1x 1.9x 0.97x 1.6x | 2.2x 3.2x | 8.5x 9.3x 11x 12x -rfc-example2 | 1x 1.9x 0.98x 1.6x | 2.6x 3.9x | 10x 11x 17x 19x -types | 1x 2.2x 0.97x 2x | 1.2x 1.9x | 11x 13x 12x 14x --------------|-------------------------|------------|----------------------- -= Average => | 1x 1.9x 0.98x 1.7x | 2.5x 3.9x | 14x 17x 16x 19x -............................................................................. - - -// vi:ft=asciidoc tw=72: diff --git a/3rd/lua-cjson/rfc4627.txt b/3rd/lua-cjson/rfc4627.txt deleted file mode 100644 index 67b89092..00000000 --- a/3rd/lua-cjson/rfc4627.txt +++ /dev/null @@ -1,563 +0,0 @@ - - - - - - -Network Working Group D. Crockford -Request for Comments: 4627 JSON.org -Category: Informational July 2006 - - - The application/json Media Type for JavaScript Object Notation (JSON) - -Status of This Memo - - This memo provides information for the Internet community. It does - not specify an Internet standard of any kind. Distribution of this - memo is unlimited. - -Copyright Notice - - Copyright (C) The Internet Society (2006). - -Abstract - - JavaScript Object Notation (JSON) is a lightweight, text-based, - language-independent data interchange format. It was derived from - the ECMAScript Programming Language Standard. JSON defines a small - set of formatting rules for the portable representation of structured - data. - -1. Introduction - - JavaScript Object Notation (JSON) is a text format for the - serialization of structured data. It is derived from the object - literals of JavaScript, as defined in the ECMAScript Programming - Language Standard, Third Edition [ECMA]. - - JSON can represent four primitive types (strings, numbers, booleans, - and null) and two structured types (objects and arrays). - - A string is a sequence of zero or more Unicode characters [UNICODE]. - - An object is an unordered collection of zero or more name/value - pairs, where a name is a string and a value is a string, number, - boolean, null, object, or array. - - An array is an ordered sequence of zero or more values. - - The terms "object" and "array" come from the conventions of - JavaScript. - - JSON's design goals were for it to be minimal, portable, textual, and - a subset of JavaScript. - - - -Crockford Informational [Page 1] - -RFC 4627 JSON July 2006 - - -1.1. Conventions Used in This Document - - The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", - "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this - document are to be interpreted as described in [RFC2119]. - - The grammatical rules in this document are to be interpreted as - described in [RFC4234]. - -2. JSON Grammar - - A JSON text is a sequence of tokens. The set of tokens includes six - structural characters, strings, numbers, and three literal names. - - A JSON text is a serialized object or array. - - JSON-text = object / array - - These are the six structural characters: - - begin-array = ws %x5B ws ; [ left square bracket - - begin-object = ws %x7B ws ; { left curly bracket - - end-array = ws %x5D ws ; ] right square bracket - - end-object = ws %x7D ws ; } right curly bracket - - name-separator = ws %x3A ws ; : colon - - value-separator = ws %x2C ws ; , comma - - Insignificant whitespace is allowed before or after any of the six - structural characters. - - ws = *( - %x20 / ; Space - %x09 / ; Horizontal tab - %x0A / ; Line feed or New line - %x0D ; Carriage return - ) - -2.1. Values - - A JSON value MUST be an object, array, number, or string, or one of - the following three literal names: - - false null true - - - -Crockford Informational [Page 2] - -RFC 4627 JSON July 2006 - - - The literal names MUST be lowercase. No other literal names are - allowed. - - value = false / null / true / object / array / number / string - - false = %x66.61.6c.73.65 ; false - - null = %x6e.75.6c.6c ; null - - true = %x74.72.75.65 ; true - -2.2. Objects - - An object structure is represented as a pair of curly brackets - surrounding zero or more name/value pairs (or members). A name is a - string. A single colon comes after each name, separating the name - from the value. A single comma separates a value from a following - name. The names within an object SHOULD be unique. - - object = begin-object [ member *( value-separator member ) ] - end-object - - member = string name-separator value - -2.3. Arrays - - An array structure is represented as square brackets surrounding zero - or more values (or elements). Elements are separated by commas. - - array = begin-array [ value *( value-separator value ) ] end-array - -2.4. Numbers - - The representation of numbers is similar to that used in most - programming languages. A number contains an integer component that - may be prefixed with an optional minus sign, which may be followed by - a fraction part and/or an exponent part. - - Octal and hex forms are not allowed. Leading zeros are not allowed. - - A fraction part is a decimal point followed by one or more digits. - - An exponent part begins with the letter E in upper or lowercase, - which may be followed by a plus or minus sign. The E and optional - sign are followed by one or more digits. - - Numeric values that cannot be represented as sequences of digits - (such as Infinity and NaN) are not permitted. - - - -Crockford Informational [Page 3] - -RFC 4627 JSON July 2006 - - - number = [ minus ] int [ frac ] [ exp ] - - decimal-point = %x2E ; . - - digit1-9 = %x31-39 ; 1-9 - - e = %x65 / %x45 ; e E - - exp = e [ minus / plus ] 1*DIGIT - - frac = decimal-point 1*DIGIT - - int = zero / ( digit1-9 *DIGIT ) - - minus = %x2D ; - - - plus = %x2B ; + - - zero = %x30 ; 0 - -2.5. Strings - - The representation of strings is similar to conventions used in the C - family of programming languages. A string begins and ends with - quotation marks. All Unicode characters may be placed within the - quotation marks except for the characters that must be escaped: - quotation mark, reverse solidus, and the control characters (U+0000 - through U+001F). - - Any character may be escaped. If the character is in the Basic - Multilingual Plane (U+0000 through U+FFFF), then it may be - represented as a six-character sequence: a reverse solidus, followed - by the lowercase letter u, followed by four hexadecimal digits that - encode the character's code point. The hexadecimal letters A though - F can be upper or lowercase. So, for example, a string containing - only a single reverse solidus character may be represented as - "\u005C". - - Alternatively, there are two-character sequence escape - representations of some popular characters. So, for example, a - string containing only a single reverse solidus character may be - represented more compactly as "\\". - - To escape an extended character that is not in the Basic Multilingual - Plane, the character is represented as a twelve-character sequence, - encoding the UTF-16 surrogate pair. So, for example, a string - containing only the G clef character (U+1D11E) may be represented as - "\uD834\uDD1E". - - - -Crockford Informational [Page 4] - -RFC 4627 JSON July 2006 - - - string = quotation-mark *char quotation-mark - - char = unescaped / - escape ( - %x22 / ; " quotation mark U+0022 - %x5C / ; \ reverse solidus U+005C - %x2F / ; / solidus U+002F - %x62 / ; b backspace U+0008 - %x66 / ; f form feed U+000C - %x6E / ; n line feed U+000A - %x72 / ; r carriage return U+000D - %x74 / ; t tab U+0009 - %x75 4HEXDIG ) ; uXXXX U+XXXX - - escape = %x5C ; \ - - quotation-mark = %x22 ; " - - unescaped = %x20-21 / %x23-5B / %x5D-10FFFF - -3. Encoding - - JSON text SHALL be encoded in Unicode. The default encoding is - UTF-8. - - Since the first two characters of a JSON text will always be ASCII - characters [RFC0020], it is possible to determine whether an octet - stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking - at the pattern of nulls in the first four octets. - - 00 00 00 xx UTF-32BE - 00 xx 00 xx UTF-16BE - xx 00 00 00 UTF-32LE - xx 00 xx 00 UTF-16LE - xx xx xx xx UTF-8 - -4. Parsers - - A JSON parser transforms a JSON text into another representation. A - JSON parser MUST accept all texts that conform to the JSON grammar. - A JSON parser MAY accept non-JSON forms or extensions. - - An implementation may set limits on the size of texts that it - accepts. An implementation may set limits on the maximum depth of - nesting. An implementation may set limits on the range of numbers. - An implementation may set limits on the length and character contents - of strings. - - - - -Crockford Informational [Page 5] - -RFC 4627 JSON July 2006 - - -5. Generators - - A JSON generator produces JSON text. The resulting text MUST - strictly conform to the JSON grammar. - -6. IANA Considerations - - The MIME media type for JSON text is application/json. - - Type name: application - - Subtype name: json - - Required parameters: n/a - - Optional parameters: n/a - - Encoding considerations: 8bit if UTF-8; binary if UTF-16 or UTF-32 - - JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON - is written in UTF-8, JSON is 8bit compatible. When JSON is - written in UTF-16 or UTF-32, the binary content-transfer-encoding - must be used. - - Security considerations: - - Generally there are security issues with scripting languages. JSON - is a subset of JavaScript, but it is a safe subset that excludes - assignment and invocation. - - A JSON text can be safely passed into JavaScript's eval() function - (which compiles and executes a string) if all the characters not - enclosed in strings are in the set of characters that form JSON - tokens. This can be quickly determined in JavaScript with two - regular expressions and calls to the test and replace methods. - - var my_JSON_object = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( - text.replace(/"(\\.|[^"\\])*"/g, ''))) && - eval('(' + text + ')'); - - Interoperability considerations: n/a - - Published specification: RFC 4627 - - - - - - - - -Crockford Informational [Page 6] - -RFC 4627 JSON July 2006 - - - Applications that use this media type: - - JSON has been used to exchange data between applications written - in all of these programming languages: ActionScript, C, C#, - ColdFusion, Common Lisp, E, Erlang, Java, JavaScript, Lua, - Objective CAML, Perl, PHP, Python, Rebol, Ruby, and Scheme. - - Additional information: - - Magic number(s): n/a - File extension(s): .json - Macintosh file type code(s): TEXT - - Person & email address to contact for further information: - Douglas Crockford - douglas@crockford.com - - Intended usage: COMMON - - Restrictions on usage: none - - Author: - Douglas Crockford - douglas@crockford.com - - Change controller: - Douglas Crockford - douglas@crockford.com - -7. Security Considerations - - See Security Considerations in Section 6. - -8. Examples - - This is a JSON object: - - { - "Image": { - "Width": 800, - "Height": 600, - "Title": "View from 15th Floor", - "Thumbnail": { - "Url": "http://www.example.com/image/481989943", - "Height": 125, - "Width": "100" - }, - "IDs": [116, 943, 234, 38793] - - - -Crockford Informational [Page 7] - -RFC 4627 JSON July 2006 - - - } - } - - Its Image member is an object whose Thumbnail member is an object - and whose IDs member is an array of numbers. - - This is a JSON array containing two objects: - - [ - { - "precision": "zip", - "Latitude": 37.7668, - "Longitude": -122.3959, - "Address": "", - "City": "SAN FRANCISCO", - "State": "CA", - "Zip": "94107", - "Country": "US" - }, - { - "precision": "zip", - "Latitude": 37.371991, - "Longitude": -122.026020, - "Address": "", - "City": "SUNNYVALE", - "State": "CA", - "Zip": "94085", - "Country": "US" - } - ] - -9. References - -9.1. Normative References - - [ECMA] European Computer Manufacturers Association, "ECMAScript - Language Specification 3rd Edition", December 1999, - . - - [RFC0020] Cerf, V., "ASCII format for network interchange", RFC 20, - October 1969. - - [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate - Requirement Levels", BCP 14, RFC 2119, March 1997. - - [RFC4234] Crocker, D. and P. Overell, "Augmented BNF for Syntax - Specifications: ABNF", RFC 4234, October 2005. - - - -Crockford Informational [Page 8] - -RFC 4627 JSON July 2006 - - - [UNICODE] The Unicode Consortium, "The Unicode Standard Version 4.0", - 2003, . - -Author's Address - - Douglas Crockford - JSON.org - EMail: douglas@crockford.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Crockford Informational [Page 9] - -RFC 4627 JSON July 2006 - - -Full Copyright Statement - - Copyright (C) The Internet Society (2006). - - This document is subject to the rights, licenses and restrictions - contained in BCP 78, and except as set forth therein, the authors - retain all their rights. - - This document and the information contained herein are provided on an - "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS - OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET - ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, - INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE - INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED - WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. - -Intellectual Property - - The IETF takes no position regarding the validity or scope of any - Intellectual Property Rights or other rights that might be claimed to - pertain to the implementation or use of the technology described in - this document or the extent to which any license under such rights - might or might not be available; nor does it represent that it has - made any independent effort to identify any such rights. Information - on the procedures with respect to rights in RFC documents can be - found in BCP 78 and BCP 79. - - Copies of IPR disclosures made to the IETF Secretariat and any - assurances of licenses to be made available, or the result of an - attempt made to obtain a general license or permission for the use of - such proprietary rights by implementers or users of this - specification can be obtained from the IETF on-line IPR repository at - http://www.ietf.org/ipr. - - The IETF invites any interested party to bring to its attention any - copyrights, patents or patent applications, or other proprietary - rights that may cover technology that may be required to implement - this standard. Please address the information to the IETF at - ietf-ipr@ietf.org. - -Acknowledgement - - Funding for the RFC Editor function is provided by the IETF - Administrative Support Activity (IASA). - - - - - - - -Crockford Informational [Page 10] - diff --git a/3rd/lua-cjson/runtests.sh b/3rd/lua-cjson/runtests.sh deleted file mode 100755 index c87a8ee0..00000000 --- a/3rd/lua-cjson/runtests.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/sh - -PLATFORM="`uname -s`" -[ "$1" ] && VERSION="$1" || VERSION="2.1.0" - -set -e - -# Portable "ggrep -A" replacement. -# Work around Solaris awk record limit of 2559 bytes. -# contextgrep PATTERN POST_MATCH_LINES -contextgrep() { - cut -c -2500 | awk "/$1/ { count = ($2 + 1) } count > 0 { count--; print }" -} - -do_tests() { - echo - cd tests - lua -e 'print("Testing Lua CJSON version " .. require("cjson")._VERSION)' - ./test.lua | contextgrep 'FAIL|Summary' 3 | grep -v PASS | cut -c -150 - cd .. -} - -echo "===== Setting LuaRocks PATH =====" -eval "`luarocks path`" - -echo "===== Building UTF-8 test data =====" -( cd tests && ./genutf8.pl; ) - -echo "===== Cleaning old build data =====" -make clean -rm -f tests/cjson.so - -echo "===== Verifying cjson.so is not installed =====" - -cd tests -if lua -e 'require "cjson"' 2>/dev/null -then - cat < "$LOG" - RPM="`awk '/^Wrote: / && ! /debuginfo/ { print $2}' < "$LOG"`" - sudo -- rpm -Uvh \"$RPM\" - do_tests - sudo -- rpm -e lua-cjson - rm -f "$LOG" - else - echo "==> skipping, $TGZ not found" - fi -fi - -# vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/strbuf.c b/3rd/lua-cjson/strbuf.c deleted file mode 100644 index f0f7f4b9..00000000 --- a/3rd/lua-cjson/strbuf.c +++ /dev/null @@ -1,251 +0,0 @@ -/* strbuf - String buffer routines - * - * Copyright (c) 2010-2012 Mark Pulford - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include -#include -#include - -#include "strbuf.h" - -static void die(const char *fmt, ...) -{ - va_list arg; - - va_start(arg, fmt); - vfprintf(stderr, fmt, arg); - va_end(arg); - fprintf(stderr, "\n"); - - exit(-1); -} - -void strbuf_init(strbuf_t *s, int len) -{ - int size; - - if (len <= 0) - size = STRBUF_DEFAULT_SIZE; - else - size = len + 1; /* \0 terminator */ - - s->buf = NULL; - s->size = size; - s->length = 0; - s->increment = STRBUF_DEFAULT_INCREMENT; - s->dynamic = 0; - s->reallocs = 0; - s->debug = 0; - - s->buf = malloc(size); - if (!s->buf) - die("Out of memory"); - - strbuf_ensure_null(s); -} - -strbuf_t *strbuf_new(int len) -{ - strbuf_t *s; - - s = malloc(sizeof(strbuf_t)); - if (!s) - die("Out of memory"); - - strbuf_init(s, len); - - /* Dynamic strbuf allocation / deallocation */ - s->dynamic = 1; - - return s; -} - -void strbuf_set_increment(strbuf_t *s, int increment) -{ - /* Increment > 0: Linear buffer growth rate - * Increment < -1: Exponential buffer growth rate */ - if (increment == 0 || increment == -1) - die("BUG: Invalid string increment"); - - s->increment = increment; -} - -static inline void debug_stats(strbuf_t *s) -{ - if (s->debug) { - fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %d, size: %d\n", - (long)s, s->reallocs, s->length, s->size); - } -} - -/* If strbuf_t has not been dynamically allocated, strbuf_free() can - * be called any number of times strbuf_init() */ -void strbuf_free(strbuf_t *s) -{ - debug_stats(s); - - if (s->buf) { - free(s->buf); - s->buf = NULL; - } - if (s->dynamic) - free(s); -} - -char *strbuf_free_to_string(strbuf_t *s, int *len) -{ - char *buf; - - debug_stats(s); - - strbuf_ensure_null(s); - - buf = s->buf; - if (len) - *len = s->length; - - if (s->dynamic) - free(s); - - return buf; -} - -static int calculate_new_size(strbuf_t *s, int len) -{ - int reqsize, newsize; - - if (len <= 0) - die("BUG: Invalid strbuf length requested"); - - /* Ensure there is room for optional NULL termination */ - reqsize = len + 1; - - /* If the user has requested to shrink the buffer, do it exactly */ - if (s->size > reqsize) - return reqsize; - - newsize = s->size; - if (s->increment < 0) { - /* Exponential sizing */ - while (newsize < reqsize) - newsize *= -s->increment; - } else { - /* Linear sizing */ - newsize = ((newsize + s->increment - 1) / s->increment) * s->increment; - } - - return newsize; -} - - -/* Ensure strbuf can handle a string length bytes long (ignoring NULL - * optional termination). */ -void strbuf_resize(strbuf_t *s, int len) -{ - int newsize; - - newsize = calculate_new_size(s, len); - - if (s->debug > 1) { - fprintf(stderr, "strbuf(%lx) resize: %d => %d\n", - (long)s, s->size, newsize); - } - - s->size = newsize; - s->buf = realloc(s->buf, s->size); - if (!s->buf) - die("Out of memory"); - s->reallocs++; -} - -void strbuf_append_string(strbuf_t *s, const char *str) -{ - int space, i; - - space = strbuf_empty_length(s); - - for (i = 0; str[i]; i++) { - if (space < 1) { - strbuf_resize(s, s->length + 1); - space = strbuf_empty_length(s); - } - - s->buf[s->length] = str[i]; - s->length++; - space--; - } -} - -/* strbuf_append_fmt() should only be used when an upper bound - * is known for the output string. */ -void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...) -{ - va_list arg; - int fmt_len; - - strbuf_ensure_empty_length(s, len); - - va_start(arg, fmt); - fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg); - va_end(arg); - - if (fmt_len < 0) - die("BUG: Unable to convert number"); /* This should never happen.. */ - - s->length += fmt_len; -} - -/* strbuf_append_fmt_retry() can be used when the there is no known - * upper bound for the output string. */ -void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...) -{ - va_list arg; - int fmt_len, try; - int empty_len; - - /* If the first attempt to append fails, resize the buffer appropriately - * and try again */ - for (try = 0; ; try++) { - va_start(arg, fmt); - /* Append the new formatted string */ - /* fmt_len is the length of the string required, excluding the - * trailing NULL */ - empty_len = strbuf_empty_length(s); - /* Add 1 since there is also space to store the terminating NULL. */ - fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg); - va_end(arg); - - if (fmt_len <= empty_len) - break; /* SUCCESS */ - if (try > 0) - die("BUG: length of formatted string changed"); - - strbuf_resize(s, s->length + fmt_len); - } - - s->length += fmt_len; -} - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/strbuf.h b/3rd/lua-cjson/strbuf.h deleted file mode 100644 index d861108c..00000000 --- a/3rd/lua-cjson/strbuf.h +++ /dev/null @@ -1,154 +0,0 @@ -/* strbuf - String buffer routines - * - * Copyright (c) 2010-2012 Mark Pulford - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include - -/* Size: Total bytes allocated to *buf - * Length: String length, excluding optional NULL terminator. - * Increment: Allocation increments when resizing the string buffer. - * Dynamic: True if created via strbuf_new() - */ - -typedef struct { - char *buf; - int size; - int length; - int increment; - int dynamic; - int reallocs; - int debug; -} strbuf_t; - -#ifndef STRBUF_DEFAULT_SIZE -#define STRBUF_DEFAULT_SIZE 1023 -#endif -#ifndef STRBUF_DEFAULT_INCREMENT -#define STRBUF_DEFAULT_INCREMENT -2 -#endif - -/* Initialise */ -extern strbuf_t *strbuf_new(int len); -extern void strbuf_init(strbuf_t *s, int len); -extern void strbuf_set_increment(strbuf_t *s, int increment); - -/* Release */ -extern void strbuf_free(strbuf_t *s); -extern char *strbuf_free_to_string(strbuf_t *s, int *len); - -/* Management */ -extern void strbuf_resize(strbuf_t *s, int len); -static int strbuf_empty_length(strbuf_t *s); -static int strbuf_length(strbuf_t *s); -static char *strbuf_string(strbuf_t *s, int *len); -static void strbuf_ensure_empty_length(strbuf_t *s, int len); -static char *strbuf_empty_ptr(strbuf_t *s); -static void strbuf_extend_length(strbuf_t *s, int len); - -/* Update */ -extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...); -extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...); -static void strbuf_append_mem(strbuf_t *s, const char *c, int len); -extern void strbuf_append_string(strbuf_t *s, const char *str); -static void strbuf_append_char(strbuf_t *s, const char c); -static void strbuf_ensure_null(strbuf_t *s); - -/* Reset string for before use */ -static inline void strbuf_reset(strbuf_t *s) -{ - s->length = 0; -} - -static inline int strbuf_allocated(strbuf_t *s) -{ - return s->buf != NULL; -} - -/* Return bytes remaining in the string buffer - * Ensure there is space for a NULL terminator. */ -static inline int strbuf_empty_length(strbuf_t *s) -{ - return s->size - s->length - 1; -} - -static inline void strbuf_ensure_empty_length(strbuf_t *s, int len) -{ - if (len > strbuf_empty_length(s)) - strbuf_resize(s, s->length + len); -} - -static inline char *strbuf_empty_ptr(strbuf_t *s) -{ - return s->buf + s->length; -} - -static inline void strbuf_extend_length(strbuf_t *s, int len) -{ - s->length += len; -} - -static inline int strbuf_length(strbuf_t *s) -{ - return s->length; -} - -static inline void strbuf_append_char(strbuf_t *s, const char c) -{ - strbuf_ensure_empty_length(s, 1); - s->buf[s->length++] = c; -} - -static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c) -{ - s->buf[s->length++] = c; -} - -static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len) -{ - strbuf_ensure_empty_length(s, len); - memcpy(s->buf + s->length, c, len); - s->length += len; -} - -static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len) -{ - memcpy(s->buf + s->length, c, len); - s->length += len; -} - -static inline void strbuf_ensure_null(strbuf_t *s) -{ - s->buf[s->length] = 0; -} - -static inline char *strbuf_string(strbuf_t *s, int *len) -{ - if (len) - *len = s->length; - - return s->buf; -} - -/* vi:ai et sw=4 ts=4: - */ diff --git a/3rd/lua-cjson/tests/README b/3rd/lua-cjson/tests/README deleted file mode 100644 index 39e8bd45..00000000 --- a/3rd/lua-cjson/tests/README +++ /dev/null @@ -1,4 +0,0 @@ -These JSON examples were taken from the JSON website -(http://json.org/example.html) and RFC 4627. - -Used with permission. diff --git a/3rd/lua-cjson/tests/bench.lua b/3rd/lua-cjson/tests/bench.lua deleted file mode 100755 index 648020b1..00000000 --- a/3rd/lua-cjson/tests/bench.lua +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env lua - --- This benchmark script measures wall clock time and should be --- run on an unloaded system. --- --- Your Mileage May Vary. --- --- Mark Pulford - -local json_module = os.getenv("JSON_MODULE") or "cjson" - -require "socket" -local json = require(json_module) -local util = require "cjson.util" - -local function find_func(mod, funcnames) - for _, v in ipairs(funcnames) do - if mod[v] then - return mod[v] - end - end - - return nil -end - -local json_encode = find_func(json, { "encode", "Encode", "to_string", "stringify", "json" }) -local json_decode = find_func(json, { "decode", "Decode", "to_value", "parse" }) - -local function average(t) - local total = 0 - for _, v in ipairs(t) do - total = total + v - end - return total / #t -end - -function benchmark(tests, seconds, rep) - local function bench(func, iter) - -- Use socket.gettime() to measure microsecond resolution - -- wall clock time. - local t = socket.gettime() - for i = 1, iter do - func(i) - end - t = socket.gettime() - t - - -- Don't trust any results when the run lasted for less than a - -- millisecond - return nil. - if t < 0.001 then - return nil - end - - return (iter / t) - end - - -- Roughly calculate the number of interations required - -- to obtain a particular time period. - local function calc_iter(func, seconds) - local iter = 1 - local rate - -- Warm up the bench function first. - func() - while not rate do - rate = bench(func, iter) - iter = iter * 10 - end - return math.ceil(seconds * rate) - end - - local test_results = {} - for name, func in pairs(tests) do - -- k(number), v(string) - -- k(string), v(function) - -- k(number), v(function) - if type(func) == "string" then - name = func - func = _G[name] - end - - local iter = calc_iter(func, seconds) - - local result = {} - for i = 1, rep do - result[i] = bench(func, iter) - end - - -- Remove the slowest half (round down) of the result set - table.sort(result) - for i = 1, math.floor(#result / 2) do - table.remove(result, 1) - end - - test_results[name] = average(result) - end - - return test_results -end - -function bench_file(filename) - local data_json = util.file_load(filename) - local data_obj = json_decode(data_json) - - local function test_encode() - json_encode(data_obj) - end - local function test_decode() - json_decode(data_json) - end - - local tests = {} - if json_encode then tests.encode = test_encode end - if json_decode then tests.decode = test_decode end - - return benchmark(tests, 0.1, 5) -end - --- Optionally load any custom configuration required for this module -local success, data = pcall(util.file_load, ("bench-%s.lua"):format(json_module)) -if success then - util.run_script(data, _G) - configure(json) -end - -for i = 1, #arg do - local results = bench_file(arg[i]) - for k, v in pairs(results) do - print(("%s\t%s\t%d"):format(arg[i], k, v)) - end -end - --- vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/tests/example1.json b/3rd/lua-cjson/tests/example1.json deleted file mode 100644 index 42486cec..00000000 --- a/3rd/lua-cjson/tests/example1.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "glossary": { - "title": "example glossary", - "GlossDiv": { - "title": "S", - "GlossList": { - "GlossEntry": { - "ID": "SGML", - "SortAs": "SGML", - "GlossTerm": "Standard Generalized Mark up Language", - "Acronym": "SGML", - "Abbrev": "ISO 8879:1986", - "GlossDef": { - "para": "A meta-markup language, used to create markup languages such as DocBook.", - "GlossSeeAlso": ["GML", "XML"] - }, - "GlossSee": "markup" - } - } - } - } -} diff --git a/3rd/lua-cjson/tests/example2.json b/3rd/lua-cjson/tests/example2.json deleted file mode 100644 index 5600991a..00000000 --- a/3rd/lua-cjson/tests/example2.json +++ /dev/null @@ -1,11 +0,0 @@ -{"menu": { - "id": "file", - "value": "File", - "popup": { - "menuitem": [ - {"value": "New", "onclick": "CreateNewDoc()"}, - {"value": "Open", "onclick": "OpenDoc()"}, - {"value": "Close", "onclick": "CloseDoc()"} - ] - } -}} diff --git a/3rd/lua-cjson/tests/example3.json b/3rd/lua-cjson/tests/example3.json deleted file mode 100644 index d7237a5a..00000000 --- a/3rd/lua-cjson/tests/example3.json +++ /dev/null @@ -1,26 +0,0 @@ -{"widget": { - "debug": "on", - "window": { - "title": "Sample Konfabulator Widget", - "name": "main_window", - "width": 500, - "height": 500 - }, - "image": { - "src": "Images/Sun.png", - "name": "sun1", - "hOffset": 250, - "vOffset": 250, - "alignment": "center" - }, - "text": { - "data": "Click Here", - "size": 36, - "style": "bold", - "name": "text1", - "hOffset": 250, - "vOffset": 100, - "alignment": "center", - "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" - } -}} diff --git a/3rd/lua-cjson/tests/example4.json b/3rd/lua-cjson/tests/example4.json deleted file mode 100644 index d31a395b..00000000 --- a/3rd/lua-cjson/tests/example4.json +++ /dev/null @@ -1,88 +0,0 @@ -{"web-app": { - "servlet": [ - { - "servlet-name": "cofaxCDS", - "servlet-class": "org.cofax.cds.CDSServlet", - "init-param": { - "configGlossary:installationAt": "Philadelphia, PA", - "configGlossary:adminEmail": "ksm@pobox.com", - "configGlossary:poweredBy": "Cofax", - "configGlossary:poweredByIcon": "/images/cofax.gif", - "configGlossary:staticPath": "/content/static", - "templateProcessorClass": "org.cofax.WysiwygTemplate", - "templateLoaderClass": "org.cofax.FilesTemplateLoader", - "templatePath": "templates", - "templateOverridePath": "", - "defaultListTemplate": "listTemplate.htm", - "defaultFileTemplate": "articleTemplate.htm", - "useJSP": false, - "jspListTemplate": "listTemplate.jsp", - "jspFileTemplate": "articleTemplate.jsp", - "cachePackageTagsTrack": 200, - "cachePackageTagsStore": 200, - "cachePackageTagsRefresh": 60, - "cacheTemplatesTrack": 100, - "cacheTemplatesStore": 50, - "cacheTemplatesRefresh": 15, - "cachePagesTrack": 200, - "cachePagesStore": 100, - "cachePagesRefresh": 10, - "cachePagesDirtyRead": 10, - "searchEngineListTemplate": "forSearchEnginesList.htm", - "searchEngineFileTemplate": "forSearchEngines.htm", - "searchEngineRobotsDb": "WEB-INF/robots.db", - "useDataStore": true, - "dataStoreClass": "org.cofax.SqlDataStore", - "redirectionClass": "org.cofax.SqlRedirection", - "dataStoreName": "cofax", - "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", - "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", - "dataStoreUser": "sa", - "dataStorePassword": "dataStoreTestQuery", - "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", - "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", - "dataStoreInitConns": 10, - "dataStoreMaxConns": 100, - "dataStoreConnUsageLimit": 100, - "dataStoreLogLevel": "debug", - "maxUrlLength": 500}}, - { - "servlet-name": "cofaxEmail", - "servlet-class": "org.cofax.cds.EmailServlet", - "init-param": { - "mailHost": "mail1", - "mailHostOverride": "mail2"}}, - { - "servlet-name": "cofaxAdmin", - "servlet-class": "org.cofax.cds.AdminServlet"}, - - { - "servlet-name": "fileServlet", - "servlet-class": "org.cofax.cds.FileServlet"}, - { - "servlet-name": "cofaxTools", - "servlet-class": "org.cofax.cms.CofaxToolsServlet", - "init-param": { - "templatePath": "toolstemplates/", - "log": 1, - "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", - "logMaxSize": "", - "dataLog": 1, - "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", - "dataLogMaxSize": "", - "removePageCache": "/content/admin/remove?cache=pages&id=", - "removeTemplateCache": "/content/admin/remove?cache=templates&id=", - "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", - "lookInContext": 1, - "adminGroupID": 4, - "betaServer": true}}], - "servlet-mapping": { - "cofaxCDS": "/", - "cofaxEmail": "/cofaxutil/aemail/*", - "cofaxAdmin": "/admin/*", - "fileServlet": "/static/*", - "cofaxTools": "/tools/*"}, - - "taglib": { - "taglib-uri": "cofax.tld", - "taglib-location": "/WEB-INF/tlds/cofax.tld"}}} diff --git a/3rd/lua-cjson/tests/example5.json b/3rd/lua-cjson/tests/example5.json deleted file mode 100644 index 49980ca2..00000000 --- a/3rd/lua-cjson/tests/example5.json +++ /dev/null @@ -1,27 +0,0 @@ -{"menu": { - "header": "SVG Viewer", - "items": [ - {"id": "Open"}, - {"id": "OpenNew", "label": "Open New"}, - null, - {"id": "ZoomIn", "label": "Zoom In"}, - {"id": "ZoomOut", "label": "Zoom Out"}, - {"id": "OriginalView", "label": "Original View"}, - null, - {"id": "Quality"}, - {"id": "Pause"}, - {"id": "Mute"}, - null, - {"id": "Find", "label": "Find..."}, - {"id": "FindAgain", "label": "Find Again"}, - {"id": "Copy"}, - {"id": "CopyAgain", "label": "Copy Again"}, - {"id": "CopySVG", "label": "Copy SVG"}, - {"id": "ViewSVG", "label": "View SVG"}, - {"id": "ViewSource", "label": "View Source"}, - {"id": "SaveAs", "label": "Save As"}, - null, - {"id": "Help"}, - {"id": "About", "label": "About Adobe CVG Viewer..."} - ] -}} diff --git a/3rd/lua-cjson/tests/genutf8.pl b/3rd/lua-cjson/tests/genutf8.pl deleted file mode 100755 index db661a19..00000000 --- a/3rd/lua-cjson/tests/genutf8.pl +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env perl - -# Create test comparison data using a different UTF-8 implementation. - -# The generated utf8.dat file must have the following MD5 sum: -# cff03b039d850f370a7362f3313e5268 - -use strict; - -# 0xD800 - 0xDFFF are used to encode supplementary codepoints -# 0x10000 - 0x10FFFF are supplementary codepoints -my (@codepoints) = (0 .. 0xD7FF, 0xE000 .. 0x10FFFF); - -my $utf8 = pack("U*", @codepoints); -defined($utf8) or die "Unable create UTF-8 string\n"; - -open(FH, ">:utf8", "utf8.dat") - or die "Unable to open utf8.dat: $!\n"; -print FH $utf8 - or die "Unable to write utf8.dat\n"; -close(FH); - -# vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/tests/numbers.json b/3rd/lua-cjson/tests/numbers.json deleted file mode 100644 index 4f981ff2..00000000 --- a/3rd/lua-cjson/tests/numbers.json +++ /dev/null @@ -1,7 +0,0 @@ -[ 0.110001, - 0.12345678910111, - 0.412454033640, - 2.6651441426902, - 2.718281828459, - 3.1415926535898, - 2.1406926327793 ] diff --git a/3rd/lua-cjson/tests/octets-escaped.dat b/3rd/lua-cjson/tests/octets-escaped.dat deleted file mode 100644 index ee99a6bf..00000000 --- a/3rd/lua-cjson/tests/octets-escaped.dat +++ /dev/null @@ -1 +0,0 @@ -"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ" \ No newline at end of file diff --git a/3rd/lua-cjson/tests/rfc-example1.json b/3rd/lua-cjson/tests/rfc-example1.json deleted file mode 100644 index 73532fa9..00000000 --- a/3rd/lua-cjson/tests/rfc-example1.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "Image": { - "Width": 800, - "Height": 600, - "Title": "View from 15th Floor", - "Thumbnail": { - "Url": "http://www.example.com/image/481989943", - "Height": 125, - "Width": "100" - }, - "IDs": [116, 943, 234, 38793] - } -} diff --git a/3rd/lua-cjson/tests/rfc-example2.json b/3rd/lua-cjson/tests/rfc-example2.json deleted file mode 100644 index 2a0cb681..00000000 --- a/3rd/lua-cjson/tests/rfc-example2.json +++ /dev/null @@ -1,22 +0,0 @@ -[ - { - "precision": "zip", - "Latitude": 37.7668, - "Longitude": -122.3959, - "Address": "", - "City": "SAN FRANCISCO", - "State": "CA", - "Zip": "94107", - "Country": "US" - }, - { - "precision": "zip", - "Latitude": 37.371991, - "Longitude": -122.026020, - "Address": "", - "City": "SUNNYVALE", - "State": "CA", - "Zip": "94085", - "Country": "US" - } -] diff --git a/3rd/lua-cjson/tests/test.lua b/3rd/lua-cjson/tests/test.lua deleted file mode 100755 index c8f3c441..00000000 --- a/3rd/lua-cjson/tests/test.lua +++ /dev/null @@ -1,423 +0,0 @@ -#!/usr/bin/env lua - --- Lua CJSON tests --- --- Mark Pulford --- --- Note: The output of this script is easier to read with "less -S" - -local json = require "cjson" -local json_safe = require "cjson.safe" -local util = require "cjson.util" - -local function gen_raw_octets() - local chars = {} - for i = 0, 255 do chars[i + 1] = string.char(i) end - return table.concat(chars) -end - --- Generate every UTF-16 codepoint, including supplementary codes -local function gen_utf16_escaped() - -- Create raw table escapes - local utf16_escaped = {} - local count = 0 - - local function append_escape(code) - local esc = ('\\u%04X'):format(code) - table.insert(utf16_escaped, esc) - end - - table.insert(utf16_escaped, '"') - for i = 0, 0xD7FF do - append_escape(i) - end - -- Skip 0xD800 - 0xDFFF since they are used to encode supplementary - -- codepoints - for i = 0xE000, 0xFFFF do - append_escape(i) - end - -- Append surrogate pair for each supplementary codepoint - for high = 0xD800, 0xDBFF do - for low = 0xDC00, 0xDFFF do - append_escape(high) - append_escape(low) - end - end - table.insert(utf16_escaped, '"') - - return table.concat(utf16_escaped) -end - -function load_testdata() - local data = {} - - -- Data for 8bit raw <-> escaped octets tests - data.octets_raw = gen_raw_octets() - data.octets_escaped = util.file_load("octets-escaped.dat") - - -- Data for \uXXXX -> UTF-8 test - data.utf16_escaped = gen_utf16_escaped() - - -- Load matching data for utf16_escaped - local utf8_loaded - utf8_loaded, data.utf8_raw = pcall(util.file_load, "utf8.dat") - if not utf8_loaded then - data.utf8_raw = "Failed to load utf8.dat - please run genutf8.pl" - end - - data.table_cycle = {} - data.table_cycle[1] = data.table_cycle - - local big = {} - for i = 1, 1100 do - big = { { 10, false, true, json.null }, "string", a = big } - end - data.deeply_nested_data = big - - return data -end - -function test_decode_cycle(filename) - local obj1 = json.decode(util.file_load(filename)) - local obj2 = json.decode(json.encode(obj1)) - return util.compare_values(obj1, obj2) -end - --- Set up data used in tests -local Inf = math.huge; -local NaN = math.huge * 0; - -local testdata = load_testdata() - -local cjson_tests = { - -- Test API variables - { "Check module name, version", - function () return json._NAME, json._VERSION end, { }, - true, { "cjson", "2.1.0" } }, - - -- Test decoding simple types - { "Decode string", - json.decode, { '"test string"' }, true, { "test string" } }, - { "Decode numbers", - json.decode, { '[ 0.0, -5e3, -1, 0.3e-3, 1023.2, 0e10 ]' }, - true, { { 0.0, -5000, -1, 0.0003, 1023.2, 0 } } }, - { "Decode null", - json.decode, { 'null' }, true, { json.null } }, - { "Decode true", - json.decode, { 'true' }, true, { true } }, - { "Decode false", - json.decode, { 'false' }, true, { false } }, - { "Decode object with numeric keys", - json.decode, { '{ "1": "one", "3": "three" }' }, - true, { { ["1"] = "one", ["3"] = "three" } } }, - { "Decode object with string keys", - json.decode, { '{ "a": "a", "b": "b" }' }, - true, { { a = "a", b = "b" } } }, - { "Decode array", - json.decode, { '[ "one", null, "three" ]' }, - true, { { "one", json.null, "three" } } }, - - -- Test decoding errors - { "Decode UTF-16BE [throw error]", - json.decode, { '\0"\0"' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode UTF-16LE [throw error]", - json.decode, { '"\0"\0' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode UTF-32BE [throw error]", - json.decode, { '\0\0\0"' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode UTF-32LE [throw error]", - json.decode, { '"\0\0\0' }, - false, { "JSON parser does not support UTF-16 or UTF-32" } }, - { "Decode partial JSON [throw error]", - json.decode, { '{ "unexpected eof": ' }, - false, { "Expected value but found T_END at character 21" } }, - { "Decode with extra comma [throw error]", - json.decode, { '{ "extra data": true }, false' }, - false, { "Expected the end but found T_COMMA at character 23" } }, - { "Decode invalid escape code [throw error]", - json.decode, { [[ { "bad escape \q code" } ]] }, - false, { "Expected object key string but found invalid escape code at character 16" } }, - { "Decode invalid unicode escape [throw error]", - json.decode, { [[ { "bad unicode \u0f6 escape" } ]] }, - false, { "Expected object key string but found invalid unicode escape code at character 17" } }, - { "Decode invalid keyword [throw error]", - json.decode, { ' [ "bad barewood", test ] ' }, - false, { "Expected value but found invalid token at character 20" } }, - { "Decode invalid number #1 [throw error]", - json.decode, { '[ -+12 ]' }, - false, { "Expected value but found invalid number at character 3" } }, - { "Decode invalid number #2 [throw error]", - json.decode, { '-v' }, - false, { "Expected value but found invalid number at character 1" } }, - { "Decode invalid number exponent [throw error]", - json.decode, { '[ 0.4eg10 ]' }, - false, { "Expected comma or array end but found invalid token at character 6" } }, - - -- Test decoding nested arrays / objects - { "Set decode_max_depth(5)", - json.decode_max_depth, { 5 }, true, { 5 } }, - { "Decode array at nested limit", - json.decode, { '[[[[[ "nested" ]]]]]' }, - true, { {{{{{ "nested" }}}}} } }, - { "Decode array over nested limit [throw error]", - json.decode, { '[[[[[[ "nested" ]]]]]]' }, - false, { "Found too many nested data structures (6) at character 6" } }, - { "Decode object at nested limit", - json.decode, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' }, - true, { {a={b={c={d={e="nested"}}}}} } }, - { "Decode object over nested limit [throw error]", - json.decode, { '{"a":{"b":{"c":{"d":{"e":{"f":"nested"}}}}}}' }, - false, { "Found too many nested data structures (6) at character 26" } }, - { "Set decode_max_depth(1000)", - json.decode_max_depth, { 1000 }, true, { 1000 } }, - { "Decode deeply nested array [throw error]", - json.decode, { string.rep("[", 1100) .. '1100' .. string.rep("]", 1100)}, - false, { "Found too many nested data structures (1001) at character 1001" } }, - - -- Test encoding nested tables - { "Set encode_max_depth(5)", - json.encode_max_depth, { 5 }, true, { 5 } }, - { "Encode nested table as array at nested limit", - json.encode, { {{{{{"nested"}}}}} }, true, { '[[[[["nested"]]]]]' } }, - { "Encode nested table as array after nested limit [throw error]", - json.encode, { { {{{{{"nested"}}}}} } }, - false, { "Cannot serialise, excessive nesting (6)" } }, - { "Encode nested table as object at nested limit", - json.encode, { {a={b={c={d={e="nested"}}}}} }, - true, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' } }, - { "Encode nested table as object over nested limit [throw error]", - json.encode, { {a={b={c={d={e={f="nested"}}}}}} }, - false, { "Cannot serialise, excessive nesting (6)" } }, - { "Encode table with cycle [throw error]", - json.encode, { testdata.table_cycle }, - false, { "Cannot serialise, excessive nesting (6)" } }, - { "Set encode_max_depth(1000)", - json.encode_max_depth, { 1000 }, true, { 1000 } }, - { "Encode deeply nested data [throw error]", - json.encode, { testdata.deeply_nested_data }, - false, { "Cannot serialise, excessive nesting (1001)" } }, - - -- Test encoding simple types - { "Encode null", - json.encode, { json.null }, true, { 'null' } }, - { "Encode true", - json.encode, { true }, true, { 'true' } }, - { "Encode false", - json.encode, { false }, true, { 'false' } }, - { "Encode empty object", - json.encode, { { } }, true, { '{}' } }, - { "Encode integer", - json.encode, { 10 }, true, { '10' } }, - { "Encode string", - json.encode, { "hello" }, true, { '"hello"' } }, - { "Encode Lua function [throw error]", - json.encode, { function () end }, - false, { "Cannot serialise function: type not supported" } }, - - -- Test decoding invalid numbers - { "Set decode_invalid_numbers(true)", - json.decode_invalid_numbers, { true }, true, { true } }, - { "Decode hexadecimal", - json.decode, { '0x6.ffp1' }, true, { 13.9921875 } }, - { "Decode numbers with leading zero", - json.decode, { '[ 0123, 00.33 ]' }, true, { { 123, 0.33 } } }, - { "Decode +-Inf", - json.decode, { '[ +Inf, Inf, -Inf ]' }, true, { { Inf, Inf, -Inf } } }, - { "Decode +-Infinity", - json.decode, { '[ +Infinity, Infinity, -Infinity ]' }, - true, { { Inf, Inf, -Inf } } }, - { "Decode +-NaN", - json.decode, { '[ +NaN, NaN, -NaN ]' }, true, { { NaN, NaN, NaN } } }, - { "Decode Infrared (not infinity) [throw error]", - json.decode, { 'Infrared' }, - false, { "Expected the end but found invalid token at character 4" } }, - { "Decode Noodle (not NaN) [throw error]", - json.decode, { 'Noodle' }, - false, { "Expected value but found invalid token at character 1" } }, - { "Set decode_invalid_numbers(false)", - json.decode_invalid_numbers, { false }, true, { false } }, - { "Decode hexadecimal [throw error]", - json.decode, { '0x6' }, - false, { "Expected value but found invalid number at character 1" } }, - { "Decode numbers with leading zero [throw error]", - json.decode, { '[ 0123, 00.33 ]' }, - false, { "Expected value but found invalid number at character 3" } }, - { "Decode +-Inf [throw error]", - json.decode, { '[ +Inf, Inf, -Inf ]' }, - false, { "Expected value but found invalid token at character 3" } }, - { "Decode +-Infinity [throw error]", - json.decode, { '[ +Infinity, Infinity, -Infinity ]' }, - false, { "Expected value but found invalid token at character 3" } }, - { "Decode +-NaN [throw error]", - json.decode, { '[ +NaN, NaN, -NaN ]' }, - false, { "Expected value but found invalid token at character 3" } }, - { 'Set decode_invalid_numbers("on")', - json.decode_invalid_numbers, { "on" }, true, { true } }, - - -- Test encoding invalid numbers - { "Set encode_invalid_numbers(false)", - json.encode_invalid_numbers, { false }, true, { false } }, - { "Encode NaN [throw error]", - json.encode, { NaN }, - false, { "Cannot serialise number: must not be NaN or Inf" } }, - { "Encode Infinity [throw error]", - json.encode, { Inf }, - false, { "Cannot serialise number: must not be NaN or Inf" } }, - { "Set encode_invalid_numbers(\"null\")", - json.encode_invalid_numbers, { "null" }, true, { "null" } }, - { "Encode NaN as null", - json.encode, { NaN }, true, { "null" } }, - { "Encode Infinity as null", - json.encode, { Inf }, true, { "null" } }, - { "Set encode_invalid_numbers(true)", - json.encode_invalid_numbers, { true }, true, { true } }, - { "Encode NaN", - json.encode, { NaN }, true, { "nan" } }, - { "Encode Infinity", - json.encode, { Inf }, true, { "inf" } }, - { 'Set encode_invalid_numbers("off")', - json.encode_invalid_numbers, { "off" }, true, { false } }, - - -- Test encoding tables - { "Set encode_sparse_array(true, 2, 3)", - json.encode_sparse_array, { true, 2, 3 }, true, { true, 2, 3 } }, - { "Encode sparse table as array #1", - json.encode, { { [3] = "sparse test" } }, - true, { '[null,null,"sparse test"]' } }, - { "Encode sparse table as array #2", - json.encode, { { [1] = "one", [4] = "sparse test" } }, - true, { '["one",null,null,"sparse test"]' } }, - { "Encode sparse array as object", - json.encode, { { [1] = "one", [5] = "sparse test" } }, - true, { '{"1":"one","5":"sparse test"}' } }, - { "Encode table with numeric string key as object", - json.encode, { { ["2"] = "numeric string key test" } }, - true, { '{"2":"numeric string key test"}' } }, - { "Set encode_sparse_array(false)", - json.encode_sparse_array, { false }, true, { false, 2, 3 } }, - { "Encode table with incompatible key [throw error]", - json.encode, { { [false] = "wrong" } }, - false, { "Cannot serialise boolean: table key must be a number or string" } }, - - -- Test escaping - { "Encode all octets (8-bit clean)", - json.encode, { testdata.octets_raw }, true, { testdata.octets_escaped } }, - { "Decode all escaped octets", - json.decode, { testdata.octets_escaped }, true, { testdata.octets_raw } }, - { "Decode single UTF-16 escape", - json.decode, { [["\uF800"]] }, true, { "\239\160\128" } }, - { "Decode all UTF-16 escapes (including surrogate combinations)", - json.decode, { testdata.utf16_escaped }, true, { testdata.utf8_raw } }, - { "Decode swapped surrogate pair [throw error]", - json.decode, { [["\uDC00\uD800"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode duplicate high surrogate [throw error]", - json.decode, { [["\uDB00\uDB00"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode duplicate low surrogate [throw error]", - json.decode, { [["\uDB00\uDB00"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode missing low surrogate [throw error]", - json.decode, { [["\uDB00"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - { "Decode invalid low surrogate [throw error]", - json.decode, { [["\uDB00\uD"]] }, - false, { "Expected value but found invalid unicode escape code at character 2" } }, - - -- Test locale support - -- - -- The standard Lua interpreter is ANSI C online doesn't support locales - -- by default. Force a known problematic locale to test strtod()/sprintf(). - { "Set locale to cs_CZ (comma separator)", function () - os.setlocale("cs_CZ") - json.new() - end }, - { "Encode number under comma locale", - json.encode, { 1.5 }, true, { '1.5' } }, - { "Decode number in array under comma locale", - json.decode, { '[ 10, "test" ]' }, true, { { 10, "test" } } }, - { "Revert locale to POSIX", function () - os.setlocale("C") - json.new() - end }, - - -- Test encode_keep_buffer() and enable_number_precision() - { "Set encode_keep_buffer(false)", - json.encode_keep_buffer, { false }, true, { false } }, - { "Set encode_number_precision(3)", - json.encode_number_precision, { 3 }, true, { 3 } }, - { "Encode number with precision 3", - json.encode, { 1/3 }, true, { "0.333" } }, - { "Set encode_number_precision(14)", - json.encode_number_precision, { 14 }, true, { 14 } }, - { "Set encode_keep_buffer(true)", - json.encode_keep_buffer, { true }, true, { true } }, - - -- Test config API errors - -- Function is listed as '?' due to pcall - { "Set encode_number_precision(0) [throw error]", - json.encode_number_precision, { 0 }, - false, { "bad argument #1 to '?' (expected integer between 1 and 14)" } }, - { "Set encode_number_precision(\"five\") [throw error]", - json.encode_number_precision, { "five" }, - false, { "bad argument #1 to '?' (number expected, got string)" } }, - { "Set encode_keep_buffer(nil, true) [throw error]", - json.encode_keep_buffer, { nil, true }, - false, { "bad argument #2 to '?' (found too many arguments)" } }, - { "Set encode_max_depth(\"wrong\") [throw error]", - json.encode_max_depth, { "wrong" }, - false, { "bad argument #1 to '?' (number expected, got string)" } }, - { "Set decode_max_depth(0) [throw error]", - json.decode_max_depth, { "0" }, - false, { "bad argument #1 to '?' (expected integer between 1 and 2147483647)" } }, - { "Set encode_invalid_numbers(-2) [throw error]", - json.encode_invalid_numbers, { -2 }, - false, { "bad argument #1 to '?' (invalid option '-2')" } }, - { "Set decode_invalid_numbers(true, false) [throw error]", - json.decode_invalid_numbers, { true, false }, - false, { "bad argument #2 to '?' (found too many arguments)" } }, - { "Set encode_sparse_array(\"not quite on\") [throw error]", - json.encode_sparse_array, { "not quite on" }, - false, { "bad argument #1 to '?' (invalid option 'not quite on')" } }, - - { "Reset Lua CJSON configuration", function () json = json.new() end }, - -- Wrap in a function to ensure the table returned by json.new() is used - { "Check encode_sparse_array()", - function (...) return json.encode_sparse_array(...) end, { }, - true, { false, 2, 10 } }, - - { "Encode (safe) simple value", - json_safe.encode, { true }, - true, { "true" } }, - { "Encode (safe) argument validation [throw error]", - json_safe.encode, { "arg1", "arg2" }, - false, { "bad argument #1 to '?' (expected 1 argument)" } }, - { "Decode (safe) error generation", - json_safe.decode, { "Oops" }, - true, { nil, "Expected value but found invalid token at character 1" } }, - { "Decode (safe) error generation after new()", - function(...) return json_safe.new().decode(...) end, { "Oops" }, - true, { nil, "Expected value but found invalid token at character 1" } }, -} - -print(("==> Testing Lua CJSON version %s\n"):format(json._VERSION)) - -util.run_test_group(cjson_tests) - -for _, filename in ipairs(arg) do - util.run_test("Decode cycle " .. filename, test_decode_cycle, { filename }, - true, { true }) -end - -local pass, total = util.run_test_summary() - -if pass == total then - print("==> Summary: all tests succeeded") -else - print(("==> Summary: %d/%d tests failed"):format(total - pass, total)) - os.exit(1) -end - --- vi:ai et sw=4 ts=4: diff --git a/3rd/lua-cjson/tests/types.json b/3rd/lua-cjson/tests/types.json deleted file mode 100644 index c01e7d20..00000000 --- a/3rd/lua-cjson/tests/types.json +++ /dev/null @@ -1 +0,0 @@ -{ "array": [ 10, true, null ] } diff --git a/Makefile b/Makefile index 543c6c28..eb29a629 100644 --- a/Makefile +++ b/Makefile @@ -42,8 +42,8 @@ jemalloc : $(MALLOC_STATICLIB) CSERVICE = snlua logger gate harbor LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ - cjson clientsocket memory profile multicast \ - cluster crypt sharedata stm + clientsocket memory profile multicast \ + cluster crypt sharedata stm sproto lpeg SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -92,9 +92,6 @@ $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/com $(LUA_CLIB_PATH)/netpack.so : lualib-src/lua-netpack.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -Iskynet-src -o $@ -$(LUA_CLIB_PATH)/cjson.so : | $(LUA_CLIB_PATH) - cd 3rd/lua-cjson && $(MAKE) LUA_INCLUDE_DIR=../../$(LUA_INC) CC=$(CC) CJSON_LDFLAGS="$(SHARED)" && cd ../.. && cp 3rd/lua-cjson/cjson.so $@ - $(LUA_CLIB_PATH)/clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread @@ -119,11 +116,16 @@ $(LUA_CLIB_PATH)/sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/stm.so : lualib-src/lua-stm.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ +$(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@ + +$(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ + clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so cleanall: clean - cd 3rd/lua-cjson && $(MAKE) clean cd 3rd/jemalloc && $(MAKE) clean rm -f $(LUA_STATICLIB) diff --git a/examples/agent.lua b/examples/agent.lua index 2f610063..a2a0a774 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -1,40 +1,75 @@ local skynet = require "skynet" -local jsonpack = require "jsonpack" local netpack = require "netpack" local socket = require "socket" +local sproto = require "sproto" +local bit32 = require "bit32" + +local rpc local CMD = {} - +local REQUEST = {} local client_fd -local function send_client(v) - socket.write(client_fd, netpack.pack(jsonpack.pack(0, {true, v}))) +function REQUEST:get() + print("get", self.what) + local r = skynet.call("SIMPLEDB", "lua", "get", self.what) + return { result = r } end -local function response_client(session,v) - socket.write(client_fd, netpack.pack(jsonpack.response(session,v))) +function REQUEST:set() + print("set", self.what, self.value) + local r = skynet.call("SIMPLEDB", "lua", "set", self.what, self.value) + return { ok = true } +end + +function REQUEST:handshake() + return { msg = "Welcome to skynet" } +end + +local function request(name, args, response) + local f = assert(REQUEST[name]) + local r = f(args) + if response then + return response(r) + end +end + +local function send_package(pack) + local size = #pack + local package = string.char(bit32.extract(size,8,8)) .. + string.char(bit32.extract(size,0,8)).. + pack + + socket.write(client_fd, package) end skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = function (msg, sz) - return jsonpack.unpack(skynet.tostring(msg,sz)) + return rpc:dispatch(msg, sz) end, - dispatch = function (_, _, session, args) - local ok, result = pcall(skynet.call,"SIMPLEDB", "lua", table.unpack(args)) - if ok then - response_client(session, { true, result }) + dispatch = function (_, _, type, ...) + if type == "REQUEST" then + local ok, result = pcall(request, ...) + if ok then + if result then + send_package(result) + end + else + skynet.error(result) + end else - response_client(session, { false, "Invalid command" }) + assert(type == "RESPONSE") + error "This example doesn't support request client" end end } -function CMD.start(gate , fd) +function CMD.start(gate, fd, proto) + rpc = sproto.new(proto):rpc "package" client_fd = fd skynet.call(gate, "lua", "forward", fd) - send_client "Welcome to skynet" end skynet.start(function() diff --git a/examples/client.lua b/examples/client.lua index 14342556..a909ee2e 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -1,8 +1,12 @@ package.cpath = "luaclib/?.so" +package.path = "lualib/?.lua;examples/?.lua" local socket = require "clientsocket" -local cjson = require "cjson" local bit32 = require "bit32" +local proto = require "proto" +local sproto = require "sproto" + +local rpc = sproto.new(proto):rpc "package" local fd = assert(socket.connect("127.0.0.1", 8888)) @@ -46,34 +50,39 @@ end local session = 0 -local function send_request(v) +local function send_request(name, args) session = session + 1 - local str = string.format("%d+%s",session, cjson.encode(v)) + local str = rpc:request(name, args, session) send_package(fd, str) print("Request:", session) end local last = "" -while true do +local function dispatch_package() while true do local v v, last = recv_package(last) if not v then break end - local session,t,str = string.match(v, "(%d+)(.)(.*)") - assert(t == '-' or t == '+') - session = tonumber(session) - local result = cjson.decode(str) - print("Response:",session, result[1], result[2]) + + local t, session, response = rpc:dispatch(v) + assert(t == "RESPONSE" , "This example only support request , so here must be RESPONSE") + print("response session", session) + for k,v in pairs(response) do + print(k,v) + end end +end + +send_request("handshake") +while true do + dispatch_package() local cmd = socket.readstdin() if cmd then - local args = {} - string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end ) - send_request(args) + send_request("get", { what = cmd }) else socket.usleep(100) end -end \ No newline at end of file +end diff --git a/examples/proto.lua b/examples/proto.lua new file mode 100644 index 00000000..bbc7ee94 --- /dev/null +++ b/examples/proto.lua @@ -0,0 +1,37 @@ +local sprotoparser = require "sprotoparser" + +local proto = sprotoparser.parse [[ +.package { + type 0 : integer + session 1 : integer +} + +handshake 1 { + request {} + response { + msg 0 : string + } +} + +get 2 { + request { + what 0 : string + } + response { + result 0 : boolean + } +} + +set 3 { + request { + what 0 : string + value 1 : string + } + response { + ok 0 : boolean + } +} + +]] + +return proto diff --git a/examples/watchdog.lua b/examples/watchdog.lua index 8ead2253..f37de5bf 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -1,5 +1,8 @@ +package.path = "./examples/?.lua;" .. package.path + local skynet = require "skynet" local netpack = require "netpack" +local proto = require "proto" local CMD = {} local SOCKET = {} @@ -8,7 +11,7 @@ local agent = {} function SOCKET.open(fd, addr) agent[fd] = skynet.newservice("agent") - skynet.call(agent[fd], "lua", "start", gate, fd) + skynet.call(agent[fd], "lua", "start", gate, fd, proto) end local function close_agent(fd) diff --git a/lualib-src/sproto/README b/lualib-src/sproto/README new file mode 100644 index 00000000..4421d78a --- /dev/null +++ b/lualib-src/sproto/README @@ -0,0 +1 @@ +Check https://github.com/cloudwu/sproto for more diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c new file mode 100644 index 00000000..c2ef29e7 --- /dev/null +++ b/lualib-src/sproto/lsproto.c @@ -0,0 +1,455 @@ +#include +#include "msvcint.h" + +#include "lua.h" +#include "lauxlib.h" +#include "sproto.h" + +#define ENCODE_BUFFERSIZE 2050 + +//#define ENCODE_BUFFERSIZE 2050 +#define ENCODE_MAXSIZE 0x1000000 +#define ENCODE_DEEPLEVEL 64 + +#ifndef luaL_newlib /* using LuaJIT */ +/* +** set functions from list 'l' into table at top - 'nup'; each +** function gets the 'nup' elements at the top as upvalues. +** Returns with only the table at the stack. +*/ +LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { +#ifdef luaL_checkversion + luaL_checkversion(L); +#endif + luaL_checkstack(L, nup, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -nup); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_setfield(L, -(nup + 2), l->name); + } + lua_pop(L, nup); /* remove upvalues */ +} + +#define luaL_newlibtable(L,l) \ + lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) + +#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) +#endif + +static int +lnewproto(lua_State *L) { + size_t sz = 0; + void * buffer = (void *)luaL_checklstring(L,1,&sz); + struct sproto * sp = sproto_create(buffer, sz); + if (sp) { + lua_pushlightuserdata(L, sp); + return 1; + } + return 0; +} + +static int +ldeleteproto(lua_State *L) { + struct sproto * sp = lua_touserdata(L,1); + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto object"); + } + sproto_release(sp); + return 0; +} + +static int +lquerytype(lua_State *L) { + struct sproto *sp = lua_touserdata(L,1); + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto object"); + } + const char * typename = luaL_checkstring(L,2); + struct sproto_type *st = sproto_type(sp, typename); + if (st) { + lua_pushlightuserdata(L, st); + return 1; + } + + return luaL_error(L, "type %s not found", typename); +} + +struct encode_ud { + lua_State *L; + struct sproto_type *st; + int tbl_index; + const char * array_tag; + int array_index; + int deep; +}; + +static int +encode(void *ud, const char *tagname, int type, int index, struct sproto_type *st, void *value, int length) { + struct encode_ud *self = ud; + lua_State *L = self->L; + if (self->deep >= ENCODE_DEEPLEVEL) + return luaL_error(L, "The table is too deep"); + if (index > 0) { + if (tagname != self->array_tag) { + self->array_tag = tagname; + lua_getfield(L, self->tbl_index, tagname); + if (lua_isnil(L, -1)) { + if (self->array_index) { + lua_replace(L, self->array_index); + } + self->array_index = 0; + return 0; + } + if (self->array_index) { + lua_replace(L, self->array_index); + } else { + self->array_index = lua_gettop(L); + } + } + lua_rawgeti(L, self->array_index, index); + } else { + lua_getfield(L, self->tbl_index, tagname); + } + if (lua_isnil(L, -1)) { + lua_pop(L,1); + return 0; + } + switch (type) { + case SPROTO_TINTEGER: { + lua_Integer v = luaL_checkinteger(L, -1); + lua_pop(L,1); + // notice: in lua 5.2, lua_Integer maybe 52bit + lua_Integer vh = v >> 31; + if (vh == 0 || vh == -1) { + *(uint32_t *)value = (uint32_t)v; + return 4; + } + else { + *(uint64_t *)value = (uint64_t)v; + return 8; + } + } + case SPROTO_TBOOLEAN: { + int v = lua_toboolean(L, -1); + *(int *)value = v; + lua_pop(L,1); + return 4; + } + case SPROTO_TSTRING: { + size_t sz = 0; + const char * str = luaL_checklstring(L, -1, &sz); + if (sz > length) + return -1; + memcpy(value, str, sz); + lua_pop(L,1); + return sz; + } + case SPROTO_TSTRUCT: { + struct encode_ud sub; + sub.L = L; + sub.st = st; + sub.tbl_index = lua_gettop(L); + sub.array_tag = NULL; + sub.array_index = 0; + sub.deep = self->deep + 1; + int r = sproto_encode(st, value, length, encode, &sub); + lua_pop(L,1); + return r; + } + default: + return luaL_error(L, "Invalid field type %d", type); + } +} + +static void * +expand_buffer(lua_State *L, int osz, int nsz) { + do { + osz *= 2; + } while (osz < nsz); + if (osz > ENCODE_MAXSIZE) { + luaL_error(L, "object is too large (>%d)", ENCODE_MAXSIZE); + return NULL; + } + void *output = lua_newuserdata(L, osz); + lua_replace(L, lua_upvalueindex(1)); + lua_pushinteger(L, osz); + lua_replace(L, lua_upvalueindex(2)); + + return output; +} + +/* + lightuserdata sproto_type + table source + + return string + */ +static int +lencode(lua_State *L) { + void * buffer = lua_touserdata(L, lua_upvalueindex(1)); + int sz = lua_tointeger(L, lua_upvalueindex(2)); + + struct sproto_type * st = lua_touserdata(L, 1); + if (st == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + luaL_checktype(L, 2, LUA_TTABLE); + luaL_checkstack(L, ENCODE_DEEPLEVEL + 8, NULL); + struct encode_ud self; + self.L = L; + self.st = st; + self.tbl_index = 2; + self.array_tag = NULL; + self.array_index = 0; + self.deep = 0; + for (;;) { + int r = sproto_encode(st, buffer, sz, encode, &self); + if (r<0) { + buffer = expand_buffer(L, sz, sz*2); + sz *= 2; + } else { + lua_pushlstring(L, buffer, r); + return 1; + } + } +} + +struct decode_ud { + lua_State *L; + const char * array_tag; + int array_index; + int result_index; + int deep; +}; + +static int +decode(void *ud, const char *tagname, int type, int index, struct sproto_type *st, void *value, int length) { + struct decode_ud * self = ud; + lua_State *L = self->L; + if (self->deep >= ENCODE_DEEPLEVEL) + return luaL_error(L, "The table is too deep"); + if (index > 0) { + // It's array + if (tagname != self->array_tag) { + self->array_tag = tagname; + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, self->result_index, tagname); + if (self->array_index) { + lua_replace(L, self->array_index); + } else { + self->array_index = lua_gettop(L); + } + } + } + switch (type) { + case SPROTO_TINTEGER: { + // notice: in lua 5.2, 52bit integer support (not 64) + lua_Integer v = *(lua_Integer *)value; + lua_pushinteger(L, v); + break; + } + case SPROTO_TBOOLEAN: { + int v = *(lua_Integer*)value; + lua_pushboolean(L,v); + break; + } + case SPROTO_TSTRING: { + lua_pushlstring(L, value, length); + break; + } + case SPROTO_TSTRUCT: { + lua_newtable(L); + struct decode_ud sub; + sub.L = L; + sub.result_index = lua_gettop(L); + sub.deep = self->deep + 1; + sub.array_index = 0; + sub.array_tag = NULL; + + int r = sproto_decode(st, value, length, decode, &sub); + if (r < 0 || r != length) + return r; + lua_settop(L, sub.result_index); + break; + } + default: + luaL_error(L, "Invalid type"); + } + if (index > 0) { + lua_rawseti(L, self->array_index, index); + } else { + lua_setfield(L, self->result_index, tagname); + } + + return 0; +} + +static const void * +getbuffer(lua_State *L, int index, size_t *sz) { + const void * buffer = NULL; + int t = lua_type(L, index); + if (t == LUA_TSTRING) { + buffer = lua_tolstring(L, index, sz); + } else { + if (t != LUA_TUSERDATA && t != LUA_TLIGHTUSERDATA) { + luaL_argerror(L, index, "Need a string or userdata"); + return NULL; + } + buffer = lua_touserdata(L, index); + *sz = luaL_checkinteger(L, index+1); + } + return buffer; +} + +/* + lightuserdata sproto_type + string source / (lightuserdata , integer) + return table + */ +static int +ldecode(lua_State *L) { + struct sproto_type * st = lua_touserdata(L, 1); + if (st == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + size_t sz=0; + const void * buffer = getbuffer(L, 2, &sz); + if (!lua_istable(L, -1)) { + lua_newtable(L); + } + luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); + struct decode_ud self; + self.L = L; + self.result_index = lua_gettop(L); + self.array_index = 0; + self.array_tag = NULL; + self.deep = 0; + int r = sproto_decode(st, buffer, (int)sz, decode, &self); + if (r < 0) { + return luaL_error(L, "decode error"); + } + lua_settop(L, self.result_index); + lua_pushinteger(L, r); + return 2; +} + +static int +ldumpproto(lua_State *L) { + struct sproto * sp = lua_touserdata(L, 1); + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + sproto_dump(sp); + + return 0; +} + + +/* + string source / (lightuserdata , integer) + return string + */ +static int +lpack(lua_State *L) { + size_t sz=0; + const void * buffer = getbuffer(L, 1, &sz); + // the worst-case space overhead of packing is 2 bytes per 2 KiB of input (256 words = 2KiB). + size_t maxsz = (sz + 2047) / 2048 * 2 + sz; + void * output = lua_touserdata(L, lua_upvalueindex(1)); + int osz = lua_tointeger(L, lua_upvalueindex(2)); + if (osz < maxsz) { + output = expand_buffer(L, osz, maxsz); + } + int bytes = sproto_pack(buffer, sz, output, maxsz); + if (bytes > maxsz) { + return luaL_error(L, "packing error, return size = %d", bytes); + } + lua_pushlstring(L, output, bytes); + + return 1; +} + +static int +lunpack(lua_State *L) { + size_t sz=0; + const void * buffer = getbuffer(L, 1, &sz); + void * output = lua_touserdata(L, lua_upvalueindex(1)); + int osz = lua_tointeger(L, lua_upvalueindex(2)); + int r = sproto_unpack(buffer, sz, output, osz); + if (r < 0) + return luaL_error(L, "Invalid unpack stream"); + if (r > osz) { + output = expand_buffer(L, osz, r); + } + r = sproto_unpack(buffer, sz, output, r); + if (r < 0) + return luaL_error(L, "Invalid unpack stream"); + lua_pushlstring(L, output, r); + return 1; +} + +static void +pushfunction_withbuffer(lua_State *L, const char * name, lua_CFunction func) { + lua_newuserdata(L, ENCODE_BUFFERSIZE); + lua_pushinteger(L, ENCODE_BUFFERSIZE); + lua_pushcclosure(L, func, 2); + lua_setfield(L, -2, name); +} + +static int +lprotocol(lua_State *L) { + struct sproto * sp = lua_touserdata(L, 1); + if (sp == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + int t = lua_type(L,2); + int tag; + if (t == LUA_TNUMBER) { + tag = lua_tointeger(L, 2); + const char * name = sproto_protoname(sp, tag); + if (name == NULL) + return 0; + lua_pushstring(L, name); + } else { + const char * name = lua_tostring(L, 2); + tag = sproto_prototag(sp, name); + if (tag < 0) + return 0; + lua_pushinteger(L, tag); + } + struct sproto_type * request = sproto_protoquery(sp, tag, SPROTO_REQUEST); + if (request == NULL) { + return 0; + } + lua_pushlightuserdata(L, request); + struct sproto_type * response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); + if (response == NULL) { + return 2; + } + lua_pushlightuserdata(L, response); + return 3; +} + +int +luaopen_sproto_core(lua_State *L) { +#ifdef luaL_checkversion + luaL_checkversion(L); +#endif + luaL_Reg l[] = { + { "newproto", lnewproto }, + { "deleteproto", ldeleteproto }, + { "dumpproto", ldumpproto }, + { "querytype", lquerytype }, + { "decode", ldecode }, + { "protocol", lprotocol }, + { NULL, NULL }, + }; + luaL_newlib(L,l); + pushfunction_withbuffer(L, "encode", lencode); + pushfunction_withbuffer(L, "pack", lpack); + pushfunction_withbuffer(L, "unpack", lunpack); + return 1; +} diff --git a/lualib-src/sproto/msvcint.h b/lualib-src/sproto/msvcint.h new file mode 100644 index 00000000..a0caee97 --- /dev/null +++ b/lualib-src/sproto/msvcint.h @@ -0,0 +1,32 @@ +#ifndef msvc_int_h +#define msvc_int_h + +#ifdef _MSC_VER +# define inline __inline +# ifndef _MSC_STDINT_H_ +# if (_MSC_VER < 1300) +typedef signed char int8_t; +typedef signed short int16_t; +typedef signed int int32_t; +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +# else +typedef signed __int8 int8_t; +typedef signed __int16 int16_t; +typedef signed __int32 int32_t; +typedef unsigned __int8 uint8_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int32 uint32_t; +# endif +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; +# endif + +#else + +#include + +#endif + +#endif diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c new file mode 100644 index 00000000..c701d092 --- /dev/null +++ b/lualib-src/sproto/sproto.c @@ -0,0 +1,1185 @@ +#include +#include +#include +#include +#include "msvcint.h" + +#include "sproto.h" + +#define SPROTO_TARRAY 0x80 +#define CHUNK_SIZE 1000 +#define SIZEOF_LENGTH 4 +#define SIZEOF_HEADER 2 +#define SIZEOF_FIELD 2 + +struct field { + int tag; + int type; + const char * name; + struct sproto_type * st; +}; + +struct sproto_type { + const char * name; + int n; + int base; + int maxn; + struct field *f; +}; + +struct protocol { + const char *name; + int tag; + struct sproto_type * p[2]; +}; + +struct chunk { + struct chunk * next; +}; + +struct pool { + struct chunk * header; + struct chunk * current; + int current_used; +}; + +struct sproto { + struct pool memory; + int type_n; + int protocol_n; + struct sproto_type * type; + struct protocol * proto; +}; + +static void +pool_init(struct pool *p) { + p->header = NULL; + p->current = NULL; + p->current_used = 0; +} + +static void +pool_release(struct pool *p) { + struct chunk * tmp = p->header; + while (tmp) { + struct chunk * n = tmp->next; + free(tmp); + tmp = n; + } +} + +static void * +pool_newchunk(struct pool *p, size_t sz) { + struct chunk * t = malloc(sz + sizeof(struct chunk)); + if (t == NULL) + return NULL; + t->next = p->header; + p->header = t; + return t+1; +} + +static void * +pool_alloc(struct pool *p, size_t sz) { + // align by 8 + sz = (sz + 7) & ~7; + if (sz > CHUNK_SIZE) { + return pool_newchunk(p, sz); + } + if (p->current == NULL) { + if (pool_newchunk(p, CHUNK_SIZE) == NULL) + return NULL; + p->current = p->header; + } + if (sz + p->current_used <= CHUNK_SIZE) { + void * ret = (char *)(p->current+1) + p->current_used; + p->current_used += sz; + return ret; + } + + if (sz > p->current_used) { + return pool_newchunk(p, sz); + } else { + void * ret = pool_newchunk(p, CHUNK_SIZE); + p->current = p->header; + p->current_used = sz; + return ret; + } +} + +static inline int +toword(const uint8_t * p) { + return p[0] | p[1]<<8; +} + +static inline uint32_t +todword(const uint8_t *p) { + return p[0] | p[1]<<8 | p[2]<<16 | p[3]<<24; +} + +static int +count_array(const uint8_t * stream) { + uint32_t length = todword(stream); + stream += SIZEOF_LENGTH; + int n = 0; + while (length > 0) { + uint32_t nsz; + if (length < SIZEOF_LENGTH) + return -1; + nsz = todword(stream); + nsz += SIZEOF_LENGTH; + if (nsz > length) + return -1; + ++n; + stream += nsz; + length -= nsz; + } + + return n; +} + +static int +struct_field(const uint8_t * stream, size_t sz) { + const uint8_t * field; + int fn, header, i; + if (sz < SIZEOF_LENGTH) + return -1; + fn = toword(stream); + header = SIZEOF_HEADER + SIZEOF_FIELD * fn; + if (sz < header) + return -1; + field = stream + SIZEOF_HEADER; + sz -= header; + stream += header; + for (i=0;imemory, sz+1); + memcpy(buffer, stream+SIZEOF_LENGTH, sz); + buffer[sz] = '\0'; + return buffer; +} + +static const uint8_t * +import_field(struct sproto *s, struct field *f, const uint8_t * stream) { + uint32_t sz; + const uint8_t * result; + f->tag = -1; + f->type = -1; + f->name = NULL; + f->st = NULL; + + sz = todword(stream); + stream += SIZEOF_LENGTH; + result = stream + sz; + int fn = struct_field(stream, sz); + if (fn < 0) + return NULL; + stream += SIZEOF_HEADER; + int i; + int array = 0; + int tag = -1; + for (i=0;iname = import_string(s, stream + fn * SIZEOF_FIELD); + continue; + } + if (value == 0) + return NULL; + value = value/2 - 1; + switch(tag) { + case 1: // buildin + if (value >= SPROTO_TSTRUCT) + return NULL; // invalid buildin type + f->type = value; + break; + case 2: // type index + if (value >= s->type_n) + return NULL; // invalid type index + if (f->type >= 0) + return NULL; + f->type = SPROTO_TSTRUCT; + f->st = &s->type[value]; + break; + case 3: // tag + f->tag = value; + break; + case 4: // array + if (value) + array = SPROTO_TARRAY; + break; + default: + return NULL; + } + } + if (f->tag < 0 || f->type < 0 || f->name == NULL) + return NULL; + f->type |= array; + + return result; +} + +/* +.type { + .field { + name 0 : string + buildin 1 : integer + type 2 : integer + tag 3 : integer + array 4 : boolean + } + name 0 : string + fields 1 : *field +} +*/ +static const uint8_t * +import_type(struct sproto *s, struct sproto_type *t, const uint8_t * stream) { + uint32_t sz = todword(stream); + int i; + stream += SIZEOF_LENGTH; + const uint8_t * result = stream + sz; + int fn = struct_field(stream, sz); + if (fn <= 0 || fn > 2) + return NULL; + for (i=0;iname = import_string(s, stream); + if (fn == 1) { + return result; + } + stream += todword(stream)+SIZEOF_LENGTH; // second data + int n = count_array(stream); + if (n<0) + return NULL; + stream += SIZEOF_LENGTH; + int maxn = n; + int last = -1; + t->n = n; + t->f = pool_alloc(&s->memory, sizeof(struct field) * n); + for (i=0;if[i]; + stream = import_field(s, f, stream); + if (stream == NULL) + return NULL; + int tag = f->tag; + if (tag < last) + return NULL; // tag must in ascending order + if (tag > last+1) { + ++maxn; + } + last = tag; + } + t->maxn = maxn; + t->base = t->f[0].tag; + n = t->f[n-1].tag - t->base + 1; + if (n != t->n) { + t->base = -1; + } + return result; +} + +/* +.protocol { + name 0 : string + tag 1 : integer + request 2 : integer + response 3 : integer +} +*/ +static const uint8_t * +import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { + uint32_t sz = todword(stream); + stream += SIZEOF_LENGTH; + const uint8_t * result = stream + sz; + int fn = struct_field(stream, sz); + stream += SIZEOF_HEADER; + p->name = NULL; + p->tag = -1; + p->p[SPROTO_REQUEST] = NULL; + p->p[SPROTO_RESPONSE] = NULL; + int i; + for (i=0;iname = import_string(s, stream + SIZEOF_FIELD *fn); + break; + case 1: // tag + if (value < 0) { + return NULL; + } + p->tag = value; + break; + case 2: // request + if (value < 0 || value>=s->type_n) + return NULL; + p->p[SPROTO_REQUEST] = &s->type[value]; + break; + case 3: // response + if (value < 0 || value>=s->type_n) + return NULL; + p->p[SPROTO_RESPONSE] = &s->type[value]; + break; + default: + return NULL; + } + } + + if (p->name == NULL || p->tag<0 || p->p[SPROTO_REQUEST] == NULL) { + return NULL; + } + + return result; +} + +static struct sproto * +create_from_bundle(struct sproto *s, const uint8_t * stream, size_t sz) { + int fn = struct_field(stream, sz); + if (fn < 0) + return NULL; + + stream += SIZEOF_HEADER; + + const uint8_t * content = stream + fn*SIZEOF_FIELD; + const uint8_t * typedata = NULL; + const uint8_t * protocoldata = NULL; + + int i; + for (i=0;itype_n = n; + s->type = pool_alloc(&s->memory, n * sizeof(*s->type)); + } else { + protocoldata = content+SIZEOF_LENGTH; + s->protocol_n = n; + s->proto = pool_alloc(&s->memory, n * sizeof(*s->proto)); + } + content += todword(content) + SIZEOF_LENGTH; + } + + for (i=0;itype_n;i++) { + typedata = import_type(s, &s->type[i], typedata); + if (typedata == NULL) { + return NULL; + } + } + for (i=0;iprotocol_n;i++) { + protocoldata = import_protocol(s, &s->proto[i], protocoldata); + if (protocoldata == NULL) { + return NULL; + } + } + + return s; +} + +struct sproto * +sproto_create(const void * proto, size_t sz) { + struct pool mem; + pool_init(&mem); + struct sproto * s = pool_alloc(&mem, sizeof(*s)); + if (s == NULL) + return NULL; + memset(s, 0, sizeof(*s)); + s->memory = mem; + if (create_from_bundle(s, proto, sz) == NULL) { + pool_release(&s->memory); + return NULL; + } + return s; +} + +void +sproto_release(struct sproto * s) { + if (s == NULL) + return; + pool_release(&s->memory); +} + +void +sproto_dump(struct sproto *s) { + static const char * buildin[] = { + "integer", + "boolean", + "string", + }; + printf("=== %d types ===\n", s->type_n); + int i,j; + for (i=0;itype_n;i++) { + struct sproto_type *t = &s->type[i]; + printf("%s\n", t->name); + for (j=0;jn;j++) { + char array[2] = { 0, 0 }; + const char * typename = NULL; + struct field *f = &t->f[j]; + if (f->type & SPROTO_TARRAY) { + array[0] = '*'; + } else { + array[0] = 0; + } + int t = f->type & ~SPROTO_TARRAY; + if (t == SPROTO_TSTRUCT) { + typename = f->st->name; + } else { + assert(tname, f->tag, array, typename); + } + } + printf("=== %d protocol ===\n", s->protocol_n); + for (i=0;iprotocol_n;i++) { + struct protocol *p = &s->proto[i]; + printf("\t%s (%d) request:%s", p->name, p->tag, p->p[SPROTO_REQUEST]->name); + if (p->p[SPROTO_RESPONSE]) { + printf(" response:%s", p->p[SPROTO_RESPONSE]->name); + } + printf("\n"); + } +} + +// query +int +sproto_prototag(struct sproto *sp, const char * name) { + int i; + for (i=0;iprotocol_n;i++) { + if (strcmp(name, sp->proto[i].name) == 0) { + return sp->proto[i].tag; + } + } + return -1; +} + +static struct protocol * +query_proto(struct sproto *sp, int tag) { + int begin = 0, end = sp->protocol_n; + while(beginproto[mid].tag; + if (t==tag) { + return &sp->proto[mid]; + } + if (tag > t) { + begin = mid+1; + } else { + end = mid; + } + } + return NULL; +} + +struct sproto_type * +sproto_protoquery(struct sproto *sp, int proto, int what) { + if (what <0 || what >1) { + return NULL; + } + struct protocol * p = query_proto(sp, proto); + if (p) { + return p->p[what]; + } + return NULL; +} + +const char * +sproto_protoname(struct sproto *sp, int proto) { + struct protocol * p = query_proto(sp, proto); + if (p) { + return p->name; + } + return NULL; +} + +struct sproto_type * +sproto_type(struct sproto *sp, const char * type_name) { + int i; + for (i=0;itype_n;i++) { + if (strcmp(type_name, sp->type[i].name) == 0) { + return &sp->type[i]; + } + } + return NULL; +} + +const char * +sproto_name(struct sproto_type * st) { + return st->name; +} + +static struct field * +findtag(struct sproto_type *st, int tag) { + if (st->base >=0 ) { + tag -= st->base; + if (tag < 0 || tag >= st->n) + return NULL; + return &st->f[tag]; + } + int begin = 0, end = st->n; + while (begin < end) { + int mid = (begin+end)/2; + struct field *f = &st->f[mid]; + int t = f->tag; + if (t == tag) { + return f; + } + if (tag > t) { + begin = mid + 1; + } else { + end = mid; + } + } + return NULL; +} + +// encode & decode +// sproto_callback(void *ud, int tag, int type, struct sproto_type *, void *value, int length) +// return size, -1 means error + +static inline int +fill_size(uint8_t * data, int sz) { + if (sz < 0) + return -1; + if (sz == 0) + return 0; + data[0] = sz & 0xff; + data[1] = (sz >> 8) & 0xff; + data[2] = (sz >> 16) & 0xff; + data[3] = (sz >> 24) & 0xff; + return sz + SIZEOF_LENGTH; +} + +static int +encode_integer(uint32_t v, uint8_t * data, int size) { + if (size < SIZEOF_LENGTH + sizeof(v)) + return -1; + data[4] = v & 0xff; + data[5] = (v >> 8) & 0xff; + data[6] = (v >> 16) & 0xff; + data[7] = (v >> 24) & 0xff; + return fill_size(data, sizeof(v)); +} + +static int +encode_uint64(uint64_t v, uint8_t * data, int size) { + if (size < SIZEOF_LENGTH + sizeof(v)) + return -1; + data[4] = v & 0xff; + data[5] = (v >> 8) & 0xff; + data[6] = (v >> 16) & 0xff; + data[7] = (v >> 24) & 0xff; + data[8] = (v >> 32) & 0xff; + data[9] = (v >> 40) & 0xff; + data[10] = (v >> 48) & 0xff; + data[11] = (v >> 56) & 0xff; + return fill_size(data, sizeof(v)); +} + +static int +encode_string(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { + if (size < SIZEOF_LENGTH) + return -1; + int sz = cb(ud, f->name, SPROTO_TSTRING, 0, NULL, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); + return fill_size(data, sz); +} + +static int +encode_struct(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { + if (size < SIZEOF_LENGTH) { + return -1; + } + int sz = cb(ud, f->name, SPROTO_TSTRUCT, 0, f->st, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); + return fill_size(data, sz); +} + +static inline void +uint32_to_uint64(int negative, uint8_t *buffer) { + if (negative) { + buffer[4] = 0xff; + buffer[5] = 0xff; + buffer[6] = 0xff; + buffer[7] = 0xff; + } else { + buffer[4] = 0; + buffer[5] = 0; + buffer[6] = 0; + buffer[7] = 0; + } +} + +static uint8_t * +encode_integer_array(sproto_callback cb, void *ud, struct field *f, uint8_t *buffer, int size) { + uint8_t * header = buffer; + if (size < 1) + return NULL; + buffer++; + size--; + int intlen = sizeof(uint32_t); + int index = 1; + for (;;) { + union { + uint64_t u64; + uint32_t u32; + } u; + int sz = cb(ud, f->name, SPROTO_TINTEGER, index, f->st, &u, sizeof(u)); + if (sz < 0) + return NULL; + if (sz == 0) + break; + if (size < sizeof(uint64_t)) + return NULL; + if (sz == sizeof(uint32_t)) { + uint32_t v = u.u32; + buffer[0] = v & 0xff; + buffer[1] = (v >> 8) & 0xff; + buffer[2] = (v >> 16) & 0xff; + buffer[3] = (v >> 24) & 0xff; + + if (intlen == sizeof(uint64_t)) { + uint32_to_uint64(v & 0x80000000, buffer); + } + } else { + if (sz != sizeof(uint64_t)) + return NULL; + if (intlen == sizeof(uint32_t)) { + // rearrange + size -= (index-1) * sizeof(uint32_t); + if (size < sizeof(uint64_t)) + return NULL; + buffer += (index-1) * sizeof(uint32_t); + int i; + for (i=index-2;i>=0;i--) { + memcpy(header+1+i*sizeof(uint64_t), header+1+i*sizeof(uint32_t), sizeof(uint32_t)); + int negative = header[1+i*sizeof(uint64_t)+3] & 0x80; + uint32_to_uint64(negative, header+1+i*sizeof(uint64_t)); + } + intlen = sizeof(uint64_t); + } + + uint64_t v = u.u64; + buffer[0] = v & 0xff; + buffer[1] = (v >> 8) & 0xff; + buffer[2] = (v >> 16) & 0xff; + buffer[3] = (v >> 24) & 0xff; + buffer[4] = (v >> 32) & 0xff; + buffer[5] = (v >> 40) & 0xff; + buffer[6] = (v >> 48) & 0xff; + buffer[7] = (v >> 56) & 0xff; + } + + size -= intlen; + buffer += intlen; + index++; + } + + if (buffer == header + 1) { + return header; + } + *header = (uint8_t)intlen; + return buffer; +} + +static int +encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { + if (size < SIZEOF_LENGTH) + return -1; + size -= SIZEOF_LENGTH; + int index = 1; + uint8_t * buffer = data + SIZEOF_LENGTH; + int type = f->type & ~SPROTO_TARRAY; + switch (type) { + case SPROTO_TINTEGER: + buffer = encode_integer_array(cb,ud,f,buffer,size); + if (buffer == NULL) + return -1; + break; + case SPROTO_TBOOLEAN: + for (;;) { + int v = 0; + int sz = cb(ud, f->name, type, index, f->st, &v, sizeof(v)); + if (sz < 0) + return -1; + if (sz == 0) + break; + if (size < 1) + return -1; + buffer[0] = v ? 1: 0; + size -= 1; + buffer += 1; + index++; + } + break; + default: + for (;;) { + if (size < SIZEOF_LENGTH) + return -1; + size -= SIZEOF_LENGTH; + int sz = cb(ud, f->name, type, index, f->st, buffer+SIZEOF_LENGTH, size); + if (sz < 0) + return -1; + if (sz == 0) + break; + fill_size(buffer, sz); + buffer += SIZEOF_LENGTH+sz; + size -=sz; + index ++; + } + break; + } + int sz = buffer - (data + SIZEOF_LENGTH); + if (sz == 0) + return 0; + return fill_size(data, sz); +} + +int +sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) { + uint8_t * header = buffer; + uint8_t * data; + int header_sz = SIZEOF_HEADER + st->maxn * SIZEOF_FIELD; + if (size < header_sz) + return -1; + data = header + header_sz; + size -= header_sz; + int i; + int index = 0; + int lasttag = -1; + for (i=0;in;i++) { + struct field *f = &st->f[i]; + int type = f->type; + int value = 0; + int sz = -1; + if (type & SPROTO_TARRAY) { + sz = encode_array(cb,ud, f, data, size); + } else { + switch(type) { + case SPROTO_TINTEGER: + case SPROTO_TBOOLEAN: { + union { + uint64_t u64; + uint32_t u32; + } u; + sz = cb(ud, f->name, type, 0, NULL, &u, sizeof(u)); + if (sz < 0) + return -1; + if (sz == 0) + continue; + if (sz == sizeof(uint32_t)) { + if (u.u32 < 0x7fff) { + value = (u.u32+1) * 2; + sz = 2; // sz can be any number > 0 + } else { + sz = encode_integer(u.u32, data, size); + } + } else if (sz == sizeof(uint64_t)) { + sz= encode_uint64(u.u64, data, size); + } else { + return -1; + } + break; + } + case SPROTO_TSTRING: { + sz = encode_string(cb, ud, f, data, size); + break; + } + case SPROTO_TSTRUCT: { + sz = encode_struct(cb, ud, f, data, size); + break; + } + } + } + if (sz < 0) + return -1; + if (sz > 0) { + if (value == 0) { + data += sz; + size -= sz; + } + uint8_t * record = header+SIZEOF_HEADER+SIZEOF_FIELD*index; + int tag = f->tag - lasttag - 1; + if (tag > 0) { + // skip tag + tag = (tag - 1) * 2 + 1; + if (tag > 0xffff) + return -1; + record[0] = tag & 0xff; + record[1] = (tag >> 8) & 0xff; + ++index; + record += SIZEOF_FIELD; + } + ++index; + record[0] = value & 0xff; + record[1] = (value >> 8) & 0xff; + lasttag = f->tag; + } + } + header[0] = index & 0xff; + header[1] = (index >> 8) & 0xff; + + int datasz = data - (header + header_sz); + data = header + header_sz; + if (index != st->maxn) { + memmove(header + SIZEOF_HEADER + index * SIZEOF_FIELD, data, datasz); + } + return SIZEOF_HEADER + index * SIZEOF_FIELD + datasz; +} + +static int +decode_array_object(sproto_callback cb, void *ud, struct field *f, uint8_t * stream, int sz) { + uint32_t hsz; + int type = f->type & ~SPROTO_TARRAY; + int index = 1; + while (sz > 0) { + if (sz < SIZEOF_LENGTH) + return -1; + hsz = todword(stream); + stream += SIZEOF_LENGTH; + sz -= SIZEOF_LENGTH; + if (hsz > sz) + return -1; + if (cb(ud, f->name, type, index, f->st, stream, hsz)) + return -1; + sz -= hsz; + stream += hsz; + ++index; + } + return 0; +} + +static inline uint64_t +expand64(uint32_t v) { + uint64_t value = v; + if (value & 0x80000000) { + value |= (uint64_t)~0 << 32 ; + } + return value; +} + +static int +decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { + uint32_t sz = todword(stream); + int type = f->type & ~SPROTO_TARRAY; + int i; + stream += SIZEOF_LENGTH; + switch (type) { + case SPROTO_TINTEGER: { + if (sz < 1) + return -1; + int len = *stream; + ++stream; + --sz; + if (len == sizeof(uint32_t)) { + if (sz % sizeof(uint32_t) != 0) + return -1; + for (i=0;iname, SPROTO_TINTEGER, i+1, NULL, &value, sizeof(value)); + } + } else if (len == sizeof(uint64_t)) { + if (sz % sizeof(uint64_t) != 0) + return -1; + for (i=0;iname, SPROTO_TINTEGER, i+1, NULL, &value, sizeof(value)); + } + } else { + return -1; + } + break; + } + case SPROTO_TBOOLEAN: + for (i=0;iname, SPROTO_TBOOLEAN, i+1, NULL, &value, sizeof(value)); + } + break; + case SPROTO_TSTRING: + case SPROTO_TSTRUCT: + return decode_array_object(cb, ud, f, stream, sz); + default: + return -1; + } + return 0; +} + +int +sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callback cb, void *ud) { + int total = size; + uint8_t * stream; + uint8_t * datastream; + int fn; + if (size < SIZEOF_HEADER) + return -1; + stream = (void *)data; + fn = toword(stream); + stream += SIZEOF_HEADER; + size -= SIZEOF_HEADER ; + if (size < fn * SIZEOF_FIELD) + return -1; + datastream = stream + fn * SIZEOF_FIELD; + size -= fn * SIZEOF_FIELD ; + + int i; + int tag = -1; + for (i=0;itype & SPROTO_TARRAY) { + if (decode_array(cb, ud, f, currentdata)) { + return -1; + } + } else { + switch (f->type) { + case SPROTO_TINTEGER: { + uint32_t sz = todword(currentdata); + if (sz == sizeof(uint32_t)) { + uint64_t v = expand64(todword(currentdata + SIZEOF_LENGTH)); + cb(ud, f->name, SPROTO_TINTEGER, 0, NULL, &v, sizeof(v)); + } else if (sz != sizeof(uint64_t)) { + return -1; + } else { + uint32_t low = todword(currentdata + SIZEOF_LENGTH); + uint32_t hi = todword(currentdata + SIZEOF_LENGTH + sizeof(uint32_t)); + uint64_t v = (uint64_t)low | (uint64_t) hi << 32; + cb(ud, f->name, SPROTO_TINTEGER, 0, NULL, &v, sizeof(v)); + } + break; + } + case SPROTO_TSTRING: + case SPROTO_TSTRUCT: { + uint32_t sz = todword(currentdata); + if (cb(ud, f->name, f->type, 0, f->st, currentdata+SIZEOF_LENGTH, sz)) + return -1; + break; + } + default: + return -1; + } + } + } else if (f->type != SPROTO_TINTEGER && f->type != SPROTO_TBOOLEAN) { + return -1; + } else { + uint64_t v = value; + cb(ud, f->name, f->type, 0, NULL, &v, sizeof(v)); + } + } + return total - size; +} + +// 0 pack + +static int +pack_seg(const uint8_t *src, uint8_t * buffer, int sz, int n) { + uint8_t header = 0; + int notzero = 0; + int i; + uint8_t * obuffer = buffer; + ++buffer; + --sz; + if (sz < 0) + obuffer = NULL; + + for (i=0;i<8;i++) { + if (src[i] != 0) { + notzero++; + header |= 1< 0) { + *buffer = src[i]; + ++buffer; + --sz; + } + } + } + if ((notzero == 7 || notzero == 6) && n > 0) { + notzero = 8; + } + if (notzero == 8) { + if (n > 0) { + return 8; + } else { + return 10; + } + } + if (obuffer) { + *obuffer = header; + } + return notzero + 1; +} + +static inline void +write_ff(const uint8_t * src, uint8_t * des, int n) { + des[0] = 0xff; + des[1] = n-1; + memcpy(des+2, src, n * 8); +} + +int +sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { + uint8_t tmp[8]; + int i; + const uint8_t * ff_srcstart = NULL; + uint8_t * ff_desstart = NULL; + int ff_n = 0; + int size = 0; + const uint8_t * src = srcv; + uint8_t * buffer = bufferv; + for (i=0;i 0) { + int j; + memcpy(tmp, src, 8-padding); + for (j=0;j0) { + ++ff_n; + if (ff_n == 256) { + if (bufsz >= 0) { + write_ff(ff_srcstart, ff_desstart, 256); + } + ff_n = 0; + } + } else { + if (ff_n > 0) { + if (bufsz >= 0) { + write_ff(ff_srcstart, ff_desstart, ff_n); + } + ff_n = 0; + } + } + src += 8; + buffer += n; + size += n; + } + if (ff_n > 0 && bufsz >= 0) { + write_ff(ff_srcstart, ff_desstart, ff_n); + } + return size; +} + +int +sproto_unpack(const void * srcv, int srcsz, void * bufferv, int bufsz) { + const uint8_t * src = srcv; + uint8_t * buffer = bufferv; + int size = 0; + while (srcsz > 0) { + uint8_t header = src[0]; + --srcsz; + ++src; + if (header == 0xff) { + if (srcsz < 0) { + return -1; + } + int n = (src[0] + 1) * 8; + if (srcsz < n + 1) + return -1; + srcsz -= n + 1; + ++src; + if (bufsz >= n) { + memcpy(buffer, src, n); + } + bufsz -= n; + buffer += n; + src += n; + size += n; + } else { + int i; + for (i=0;i<8;i++) { + int nz = (header >> i) & 1; + if (nz) { + if (srcsz < 0) + return -1; + if (bufsz > 0) { + *buffer = *src; + --bufsz; + ++buffer; + } + ++src; + --srcsz; + } else { + if (bufsz > 0) { + *buffer = 0; + --bufsz; + ++buffer; + } + } + ++size; + } + } + } + return size; +} diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h new file mode 100644 index 00000000..3286a423 --- /dev/null +++ b/lualib-src/sproto/sproto.h @@ -0,0 +1,39 @@ +#ifndef sproto_h +#define sproto_h + +#include + +struct sproto; +struct sproto_type; + +#define SPROTO_REQUEST 0 +#define SPROTO_RESPONSE 1 + +#define SPROTO_TINTEGER 0 +#define SPROTO_TBOOLEAN 1 +#define SPROTO_TSTRING 2 +#define SPROTO_TSTRUCT 3 + +struct sproto * sproto_create(const void * proto, size_t sz); +void sproto_release(struct sproto *); + +int sproto_prototag(struct sproto *, const char * name); +const char * sproto_protoname(struct sproto *, int proto); +// SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response +struct sproto_type * sproto_protoquery(struct sproto *, int proto, int what); + +struct sproto_type * sproto_type(struct sproto *, const char * type_name); + +int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); +int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); + +typedef int (*sproto_callback)(void *ud, const char *tagname, int type, int index, struct sproto_type *, void *value, int length); + +int sproto_decode(struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); +int sproto_encode(struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); + +// for debug use +void sproto_dump(struct sproto *); +const char * sproto_name(struct sproto_type *); + +#endif diff --git a/lualib/jsonpack.lua b/lualib/jsonpack.lua deleted file mode 100644 index b1da80cd..00000000 --- a/lualib/jsonpack.lua +++ /dev/null @@ -1,19 +0,0 @@ -local cjson = require "cjson" - -local jsonpack = {} - -function jsonpack.pack(session, v) - return string.format("%d+%s", session, cjson.encode(v)) -end - -function jsonpack.response(session, v) - return string.format("%d-%s",session, cjson.encode(v)) -end - -function jsonpack.unpack(msg) - local session,t,str = string.match(msg, "(%d+)(.)(.*)") - assert(t == '+') - return tonumber(session) , cjson.decode(str) -end - -return jsonpack \ No newline at end of file diff --git a/lualib/sproto.lua b/lualib/sproto.lua new file mode 100644 index 00000000..423a5611 --- /dev/null +++ b/lualib/sproto.lua @@ -0,0 +1,144 @@ +local core = require "sproto.core" + +local sproto = {} +local rpc = {} + +local weak_mt = { __mode = "kv" } +local sproto_mt = { __index = sproto } +local rpc_mt = { __index = rpc } + +function sproto_mt:__gc() + core.deleteproto(self.__cobj) +end + +function sproto.new(pbin) + local cobj = assert(core.newproto(pbin)) + local self = { + __cobj = cobj, + __tcache = setmetatable( {} , weak_mt ), + __pcache = setmetatable( {} , weak_mt ), + } + return setmetatable(self, sproto_mt) +end + +function sproto.parse(ptext) + local parser = require "sprotoparser" + local pbin = parser.parse(ptext) + return sproto.new(pbin) +end + +function sproto:rpc( packagename ) + packagename = packagename or "package" + local obj = { + __proto = self, + __package = core.querytype(self.__cobj, packagename), + __session = {}, + } + return setmetatable(obj, rpc_mt) +end + +local function querytype(self, typename) + local v = self.__tcache[typename] + if not v then + v = core.querytype(self.__cobj, typename) + self.__tcache[typename] = v + end + + return v +end + +function sproto:encode(typename, tbl) + local st = querytype(self, typename) + return core.encode(st, tbl) +end + +function sproto:decode(typename, bin) + local st = querytype(self, typename) + return core.decode(st, bin) +end + +function sproto:pencode(typename, tbl) + local st = querytype(self, typename) + return core.pack(core.encode(st, tbl)) +end + +function sproto:pdecode(typename, bin) + local st = querytype(self, typename) + return core.decode(st, core.unpack(bin)) +end + +local function queryproto(self, pname) + local v = self.__pcache[pname] + if not v then + local tag, req, resp = core.protocol(self.__cobj, pname) + assert(tag, pname .. " not found") + if tonumber(pname) then + pname, tag = tag, pname + end + v = { + request = req, + response =resp, + name = pname, + tag = tag, + } + self.__pcache[pname] = v + self.__pcache[tag] = v + end + + return v +end + +local header_tmp = {} +function rpc:request(name, args, session) + local proto = queryproto(self.__proto, name) + header_tmp.type = proto.tag + header_tmp.session = session + local header = core.encode(self.__package, header_tmp) + + if session then + self.__session[session] = assert(proto.response) + end + + if args then + local content = core.encode(proto.request, args) + return core.pack(header .. content) + else + return core.pack(header) + end +end + +local function gen_response(self, response, session) + return function(args) + header_tmp.type = nil + header_tmp.session = session + local header = core.encode(self.__package, header_tmp) + local content = core.encode(response, args) + return core.pack(header .. content) + end +end + +function rpc:dispatch(...) + local bin = core.unpack(...) + header_tmp.type = nil + header_tmp.session = nil + local header, size = core.decode(self.__package, bin, header_tmp) + local content = bin:sub(size + 1) + if header.type then + -- request + local proto = queryproto(self.__proto, header.type) + local result = core.decode(proto.request, content) + if header_tmp.session then + return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) + else + return "REQUEST", proto.name, result + end + else + -- response + local session = assert(header_tmp.session, "session not found") + local response = assert(self.__session[session], "Unknown session") + self.__session[session] = nil + return "RESPONSE", session, core.decode(response, content) + end +end + +return sproto diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua new file mode 100644 index 00000000..1b9dcfcd --- /dev/null +++ b/lualib/sprotoparser.lua @@ -0,0 +1,382 @@ +local lpeg = require "lpeg" +local bit32 = require "bit32" +local table = require "table" + +local P = lpeg.P +local S = lpeg.S +local R = lpeg.R +local C = lpeg.C +local Ct = lpeg.Ct +local Cg = lpeg.Cg +local Cc = lpeg.Cc +local V = lpeg.V + +local function count_lines(_,pos, parser_state) + if parser_state.pos < pos then + parser_state.line = parser_state.line + 1 + parser_state.pos = pos + end + return pos +end + +local exception = lpeg.Cmt( lpeg.Carg(1) , function ( _ , pos, parser_state) + error(string.format("syntax error at [%s] line (%d)", parser_state.file or "", parser_state.line)) + return pos +end) + +local eof = P(-1) +local newline = lpeg.Cmt((P"\n" + "\r\n") * lpeg.Carg(1) ,count_lines) +local line_comment = "#" * (1 - newline) ^0 * (newline + eof) +local blank = S" \t" + newline + line_comment +local blank0 = blank ^ 0 +local blanks = blank ^ 1 +local alpha = R"az" + R"AZ" + "_" +local alnum = alpha + R"09" +local word = alpha * alnum ^ 0 +local name = C(word) +local typename = C(word * ("." * word) ^ 0) +local tag = R"09" ^ 1 / tonumber + +local function multipat(pat) + return Ct(blank0 * (pat * blanks) ^ 0 * pat^0 * blank0) +end + +local function namedpat(name, pat) + return Ct(Cg(Cc(name), "type") * Cg(pat)) +end + +local typedef = P { + "ALL", + FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^0 * typename)), + STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}", + TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ), + SUBPROTO = Ct((C"request" + C"response") * blanks * (name + V"STRUCT")), + PROTOCOL = namedpat("protocol", name * blanks * tag * blank0 * P"{" * multipat(V"SUBPROTO") * P"}"), + ALL = multipat(V"TYPE" + V"PROTOCOL"), +} + +local proto = blank0 * typedef * blank0 + +local convert = {} + +function convert.protocol(all, obj) + local result = { tag = obj[2] } + for _, p in ipairs(obj[3]) do + assert(result[p[1]] == nil) + local typename = p[2] + if type(typename) == "table" then + local struct = typename + typename = obj[1] .. "." .. p[1] + all.type[typename] = convert.type(all, { typename, struct }) + end + result[p[1]] = typename + end + return result +end + +function convert.type(all, obj) + local result = {} + local typename = obj[1] + local tags = {} + local names = {} + for _, f in ipairs(obj[2]) do + if f.type == "field" then + local name = f[1] + if names[name] then + error(string.format("redefine %s in type %s", name, typename)) + end + names[name] = true + local tag = f[2] + if tags[tag] then + error(string.format("redefine tag %d in type %s", tag, typename)) + end + tags[tag] = true + local field = { name = name, tag = tag } + table.insert(result, field) + local fieldtype = f[3] + if fieldtype == "*" then + field.array = true + fieldtype = f[4] + end + field.typename = fieldtype + else + assert(f.type == "type") -- nest type + local nesttypename = typename .. "." .. f[1] + f[1] = nesttypename + assert(all.type[nesttypename] == nil, "redefined " .. nesttypename) + all.type[nesttypename] = convert.type(all, f) + end + end + table.sort(result, function(a,b) return a.tag < b.tag end) + return result +end + +local function adjust(r) + local result = { type = {} , protocol = {} } + + for _, obj in ipairs(r) do + local set = result[obj.type] + local name = obj[1] + assert(set[name] == nil , "redefined " .. name) + set[name] = convert[obj.type](result,obj) + end + + return result +end + +local buildin_types = { + integer = 0, + boolean = 1, + string = 2, +} + +local function checktype(types, ptype, t) + if buildin_types[t] then + return t + end + local fullname = ptype .. "." .. t + if types[fullname] then + return fullname + else + ptype = ptype:match "(.+)%..+$" + if ptype then + return checktype(types, ptype, t) + elseif types[t] then + return t + end + end +end + +local function flattypename(r) + for typename, t in pairs(r.type) do + for _, f in pairs(t) do + local ftype = f.typename + local fullname = checktype(r.type, typename, ftype) + if fullname == nil then + error(string.format("Undefined type %s in type %s", ftype, typename)) + end + f.typename = fullname + end + end + + return r +end + +local function parser(text,filename) + local state = { file = filename, pos = 0, line = 1 } + local r = lpeg.match(proto * -1 + exception , text , 1, state ) + return flattypename(adjust(r)) +end + +--[[ +-- The protocol of sproto +.type { + .field { + name 0 : string + buildin 1 : integer + type 2 : integer + tag 3 : integer + array 4 : boolean + } + name 0 : string + fields 1 : *field +} + +.protocol { + name 0 : string + tag 1 : integer + request 2 : integer # index + response 3 : integer # index +} + +.group { + type 0 : *type + protocol 1 : *protocol +} +]] + +local function packbytes(str) + local size = #str + return string.char(bit32.extract(size,0,8)).. + string.char(bit32.extract(size,8,8)).. + string.char(bit32.extract(size,16,8)).. + string.char(bit32.extract(size,24,8)).. + str +end + +local function packvalue(id) + id = (id + 1) * 2 + assert(id >=0 and id < 65536) + return string.char(bit32.extract(id, 0, 8)) .. string.char(bit32.extract(id, 8, 8)) +end + +local function packfield(f) + local strtbl = {} + if f.array then + table.insert(strtbl, "\5\0") -- 5 fields + else + table.insert(strtbl, "\4\0") -- 4 fields + end + table.insert(strtbl, "\0\0") -- name (tag = 0, ref =0) + if f.buildin then + table.insert(strtbl, packvalue(f.buildin)) -- buildin (tag = 1) + table.insert(strtbl, "\1\0") -- skip (tag = 2) + table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3) + else + table.insert(strtbl, "\1\0") -- skip (tag = 1) + table.insert(strtbl, packvalue(f.type)) -- type (tag = 2) + table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3) + end + if f.array then + table.insert(strtbl, packvalue(1)) -- array = true (tag = 4) + end + table.insert(strtbl, packbytes(f.name)) + return packbytes(table.concat(strtbl)) +end + +local function packtype(name, t, alltypes) + local fields = {} + local tmp = {} + for _, f in ipairs(t) do + tmp.array = f.array + tmp.name = f.name + tmp.tag = f.tag + + tmp.buildin = buildin_types[f.typename] + if not tmp.buildin then + tmp.type = assert(alltypes[f.typename]) + else + tmp.type = nil + end + table.insert(fields, packfield(tmp)) + end + local data + if #fields == 0 then + data = { + "\1\0", -- 1 fields + "\0\0", -- name (id = 0, ref = 0) + packbytes(name), + } + else + data = { + "\2\0", -- 2 fields + "\0\0", -- name (tag = 0, ref = 0) + "\0\0", -- field[] (tag = 1, ref = 1) + packbytes(name), + packbytes(table.concat(fields)), + } + end + + return packbytes(table.concat(data)) +end + +local function packproto(name, p, alltypes) + if p.request == nil then + error(string.format("Protocol %s need request", name)) + end + local request = alltypes[p.request] + if request == nil then + error(string.format("Protocol %s request type %s not found", name, p.request)) + end + local tmp + if p.response then + tmp = { + "\4\0", -- 4 fields + "\0\0", -- name (id=0, ref=0) + packvalue(p.tag), -- tag (tag=1) + packvalue(alltypes[p.request]), -- request typename (tag=2) + packvalue(alltypes[p.response]), -- response typename (tag=3) + } + else + tmp = { + "\3\0", -- 3 fields + "\0\0", -- name (id=0, ref=0) + packvalue(p.tag), -- tag (tag=1) + packvalue(alltypes[p.request]), -- request typename (tag=2) + } + end + table.insert(tmp, packbytes(name)) + + return packbytes(table.concat(tmp)) +end + +local function packgroup(t,p) + if next(t) == nil then + assert(next(p) == nil) + return "\0\0" + end + local tt, tp + local alltypes = {} + for name in pairs(t) do + alltypes[name] = #alltypes + table.insert(alltypes, name) + end + tt = {} + for _,name in ipairs(alltypes) do + table.insert(tt, packtype(name, t[name], alltypes)) + end + tt = packbytes(table.concat(tt)) + if next(p) then + local tmp = {} + for name, tbl in pairs(p) do + table.insert(tmp, tbl) + tbl.name = name + end + table.sort(tmp, function(a,b) return a.tag < b.tag end) + + tp = {} + for _, tbl in ipairs(tmp) do + table.insert(tp, packproto(tbl.name, tbl, alltypes)) + end + tp = packbytes(table.concat(tp)) + end + local result + if tp == nil then + result = { + "\1\0", -- 1 field + "\0\0", -- type[] (id = 0, ref = 0) + tt, + } + else + result = { + "\2\0", -- 2fields + "\0\0", -- type array (id = 0, ref = 0) + "\0\0", -- protocol array (id = 1, ref =1) + + tt, + tp, + } + end + + return table.concat(result) +end + +local function encodeall(r) + return packgroup(r.type, r.protocol) +end + +local sparser = {} + +function sparser.dump(str) + local tmp = "" + for i=1,#str do + tmp = tmp .. string.format("%02X ", string.byte(str,i)) + if i % 8 == 0 then + if i % 16 == 0 then + print(tmp) + tmp = "" + else + tmp = tmp .. "- " + end + end + end + print(tmp) +end + +function sparser.parse(text, name) + local r = parser(text, name or "=text") + local data = encodeall(r) + return data +end + +return sparser From 01269c88ffff238b8d13ca55d152489da4db369e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 7 Sep 2014 18:44:04 +0800 Subject: [PATCH 178/729] update sproto to support empty request --- examples/proto.lua | 1 - lualib-src/sproto/lsproto.c | 10 ++++---- lualib-src/sproto/sproto.c | 11 +++++---- lualib/sproto.lua | 16 +++++++++---- lualib/sprotoparser.lua | 47 ++++++++++++++++++++----------------- 5 files changed, 51 insertions(+), 34 deletions(-) diff --git a/examples/proto.lua b/examples/proto.lua index bbc7ee94..5cf85b11 100644 --- a/examples/proto.lua +++ b/examples/proto.lua @@ -7,7 +7,6 @@ local proto = sprotoparser.parse [[ } handshake 1 { - request {} response { msg 0 : string } diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c2ef29e7..602f86e9 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -422,14 +422,16 @@ lprotocol(lua_State *L) { } struct sproto_type * request = sproto_protoquery(sp, tag, SPROTO_REQUEST); if (request == NULL) { - return 0; + lua_pushnil(L); + } else { + lua_pushlightuserdata(L, request); } - lua_pushlightuserdata(L, request); struct sproto_type * response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); if (response == NULL) { - return 2; + lua_pushnil(L); + } else { + lua_pushlightuserdata(L, response); } - lua_pushlightuserdata(L, response); return 3; } diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index c701d092..0f519d14 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -329,10 +329,13 @@ import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { p->p[SPROTO_REQUEST] = NULL; p->p[SPROTO_RESPONSE] = NULL; int i; - for (i=0;iname == NULL || p->tag<0 || p->p[SPROTO_REQUEST] == NULL) { + if (p->name == NULL || p->tag<0) { return NULL; } diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 423a5611..a6180b0b 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -126,11 +126,19 @@ function rpc:dispatch(...) if header.type then -- request local proto = queryproto(self.__proto, header.type) - local result = core.decode(proto.request, content) - if header_tmp.session then - return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) + if proto.request then + local result = core.decode(proto.request, content) + if header_tmp.session then + return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) + else + return "REQUEST", proto.name, result + end else - return "REQUEST", proto.name, result + if header_tmp.session then + return "REQUEST", proto.name, nil, gen_response(self, proto.response, header_tmp.session) + else + return "REQUEST", proto.name + end end else -- response diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 1b9dcfcd..f6b24399 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -271,30 +271,35 @@ local function packtype(name, t, alltypes) end local function packproto(name, p, alltypes) - if p.request == nil then - error(string.format("Protocol %s need request", name)) +-- if p.request == nil then +-- error(string.format("Protocol %s need request", name)) +-- end + if p.request then + local request = alltypes[p.request] + if request == nil then + error(string.format("Protocol %s request type %s not found", name, p.request)) + end end - local request = alltypes[p.request] - if request == nil then - error(string.format("Protocol %s request type %s not found", name, p.request)) - end - local tmp - if p.response then - tmp = { - "\4\0", -- 4 fields - "\0\0", -- name (id=0, ref=0) - packvalue(p.tag), -- tag (tag=1) - packvalue(alltypes[p.request]), -- request typename (tag=2) - packvalue(alltypes[p.response]), -- response typename (tag=3) - } + local tmp = { + "\4\0", -- 4 fields + "\0\0", -- name (id=0, ref=0) + packvalue(p.tag), -- tag (tag=1) + } + if p.request == nil and p.response == nil then + tmp[1] = "\2\0" else - tmp = { - "\3\0", -- 3 fields - "\0\0", -- name (id=0, ref=0) - packvalue(p.tag), -- tag (tag=1) - packvalue(alltypes[p.request]), -- request typename (tag=2) - } + if p.request then + table.insert(tmp, packvalue(alltypes[p.request])) -- request typename (tag=2) + else + table.insert(tmp, "\1\0") + end + if p.response then + table.insert(tmp, packvalue(alltypes[p.response])) -- request typename (tag=3) + else + tmp[1] = "\3\0" + end end + table.insert(tmp, packbytes(name)) return packbytes(table.concat(tmp)) From 872b3c2743c484d5af4b10bcf51107941194a768 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 7 Sep 2014 21:29:53 +0800 Subject: [PATCH 179/729] example for sproto --- examples/agent.lua | 18 +++++++--- examples/client.lua | 40 ++++++++++++++++++----- examples/proto.lua | 18 +++++++--- lualib/sproto.lua | 80 +++++++++++++++++++++++++-------------------- 4 files changed, 102 insertions(+), 54 deletions(-) diff --git a/examples/agent.lua b/examples/agent.lua index a2a0a774..fc41802e 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -4,7 +4,8 @@ local socket = require "socket" local sproto = require "sproto" local bit32 = require "bit32" -local rpc +local host +local send_request local CMD = {} local REQUEST = {} @@ -19,11 +20,10 @@ end function REQUEST:set() print("set", self.what, self.value) local r = skynet.call("SIMPLEDB", "lua", "set", self.what, self.value) - return { ok = true } end function REQUEST:handshake() - return { msg = "Welcome to skynet" } + return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." } end local function request(name, args, response) @@ -47,7 +47,7 @@ skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = function (msg, sz) - return rpc:dispatch(msg, sz) + return host:dispatch(msg, sz) end, dispatch = function (_, _, type, ...) if type == "REQUEST" then @@ -67,7 +67,15 @@ skynet.register_protocol { } function CMD.start(gate, fd, proto) - rpc = sproto.new(proto):rpc "package" + host = sproto.new(proto.c2s):host "package" + send_request = host:attach(sproto.new(proto.s2c)) + skynet.fork(function() + while true do + send_package(send_request "heartbeat") + skynet.sleep(500) + end + end) + client_fd = fd skynet.call(gate, "lua", "forward", fd) end diff --git a/examples/client.lua b/examples/client.lua index a909ee2e..e9efc160 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -6,7 +6,8 @@ local bit32 = require "bit32" local proto = require "proto" local sproto = require "sproto" -local rpc = sproto.new(proto):rpc "package" +local host = sproto.new(proto.s2c):host "package" +local request = host:attach(sproto.new(proto.c2s)) local fd = assert(socket.connect("127.0.0.1", 8888)) @@ -52,13 +53,40 @@ local session = 0 local function send_request(name, args) session = session + 1 - local str = rpc:request(name, args, session) + local str = request(name, args, session) send_package(fd, str) print("Request:", session) end local last = "" +local function print_request(name, args) + print("REQUEST", name) + if args then + for k,v in pairs(args) do + print(k,v) + end + end +end + +local function print_response(session, args) + print("RESPONSE", session) + if args then + for k,v in pairs(args) do + print(k,v) + end + end +end + +local function print_package(t, ...) + if t == "REQUEST" then + print_request(...) + else + assert(t == "RESPONSE") + print_response(...) + end +end + local function dispatch_package() while true do local v @@ -67,16 +95,12 @@ local function dispatch_package() break end - local t, session, response = rpc:dispatch(v) - assert(t == "RESPONSE" , "This example only support request , so here must be RESPONSE") - print("response session", session) - for k,v in pairs(response) do - print(k,v) - end + print_package(host:dispatch(v)) end end send_request("handshake") +send_request("set", { what = "hello", value = "world" }) while true do dispatch_package() local cmd = socket.readstdin() diff --git a/examples/proto.lua b/examples/proto.lua index 5cf85b11..31b0c52d 100644 --- a/examples/proto.lua +++ b/examples/proto.lua @@ -1,6 +1,8 @@ local sprotoparser = require "sprotoparser" -local proto = sprotoparser.parse [[ +local proto = {} + +proto.c2s = sprotoparser.parse [[ .package { type 0 : integer session 1 : integer @@ -17,7 +19,7 @@ get 2 { what 0 : string } response { - result 0 : boolean + result 0 : string } } @@ -26,11 +28,17 @@ set 3 { what 0 : string value 1 : string } - response { - ok 0 : boolean - } } ]] +proto.s2c = sprotoparser.parse [[ +.package { + type 0 : integer + session 1 : integer +} + +heartbeat 1 {} +]] + return proto diff --git a/lualib/sproto.lua b/lualib/sproto.lua index a6180b0b..e6acd784 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -1,11 +1,12 @@ local core = require "sproto.core" +local assert = assert local sproto = {} -local rpc = {} +local host = {} local weak_mt = { __mode = "kv" } local sproto_mt = { __index = sproto } -local rpc_mt = { __index = rpc } +local host_mt = { __index = host } function sproto_mt:__gc() core.deleteproto(self.__cobj) @@ -27,14 +28,14 @@ function sproto.parse(ptext) return sproto.new(pbin) end -function sproto:rpc( packagename ) +function sproto:host( packagename ) packagename = packagename or "package" local obj = { __proto = self, __package = core.querytype(self.__cobj, packagename), __session = {}, } - return setmetatable(obj, rpc_mt) + return setmetatable(obj, host_mt) end local function querytype(self, typename) @@ -89,35 +90,22 @@ local function queryproto(self, pname) end local header_tmp = {} -function rpc:request(name, args, session) - local proto = queryproto(self.__proto, name) - header_tmp.type = proto.tag - header_tmp.session = session - local header = core.encode(self.__package, header_tmp) - - if session then - self.__session[session] = assert(proto.response) - end - - if args then - local content = core.encode(proto.request, args) - return core.pack(header .. content) - else - return core.pack(header) - end -end local function gen_response(self, response, session) return function(args) header_tmp.type = nil header_tmp.session = session local header = core.encode(self.__package, header_tmp) - local content = core.encode(response, args) - return core.pack(header .. content) + if response then + local content = core.encode(response, args) + return core.pack(header .. content) + else + return core.pack(header) + end end end -function rpc:dispatch(...) +function host:dispatch(...) local bin = core.unpack(...) header_tmp.type = nil header_tmp.session = nil @@ -126,26 +114,46 @@ function rpc:dispatch(...) if header.type then -- request local proto = queryproto(self.__proto, header.type) + local result if proto.request then - local result = core.decode(proto.request, content) - if header_tmp.session then - return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) - else - return "REQUEST", proto.name, result - end + result = core.decode(proto.request, content) + end + if header_tmp.session then + return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) else - if header_tmp.session then - return "REQUEST", proto.name, nil, gen_response(self, proto.response, header_tmp.session) - else - return "REQUEST", proto.name - end + return "REQUEST", proto.name, result end else -- response local session = assert(header_tmp.session, "session not found") local response = assert(self.__session[session], "Unknown session") self.__session[session] = nil - return "RESPONSE", session, core.decode(response, content) + if response == true then + return "RESPONSE", session + else + local result = core.decode(response, content) + return "RESPONSE", session, result + end + end +end + +function host:attach(sp) + return function(name, args, session) + local proto = queryproto(sp, name) + header_tmp.type = proto.tag + header_tmp.session = session + local header = core.encode(self.__package, header_tmp) + + if session then + self.__session[session] = proto.response or true + end + + if args then + local content = core.encode(proto.request, args) + return core.pack(header .. content) + else + return core.pack(header) + end end end From b52cce90560040c04bebd231535be83eb65313dd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Sep 2014 11:50:15 +0800 Subject: [PATCH 180/729] update history for 0.7.0 --- HISTORY.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 5098f75c..a63d2b4b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,10 @@ +v0.7.0 (2014-9-8) +----------- +* Use sproto instead of cjson +* Add message logger +* Add hmac-sha1 +* Some minor bugfix + v0.6.2 (2014-9-1) ----------- * bugfix: only skynet.call response PTYPE_ERROR From fe4122e5792b2c4ad834abcd0d77d056300ebf5e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Sep 2014 21:28:24 +0800 Subject: [PATCH 181/729] fix issue #171 --- lualib/snax/gateserver.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index fc019143..bc5f6547 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -96,7 +96,7 @@ function gateserver.start(handler) function MSG.error(fd, msg) if handler.error then - handler.error(fd) + handler.error(fd, msg) end close_fd(fd) end From 3074dbea0b4556163cc3c41be8c1eaa96e1b1f0c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Sep 2014 11:44:12 +0800 Subject: [PATCH 182/729] bugfix: wakeup sleep coroutine should return BREAK --- lualib/skynet.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index f700ba45..78f5bef9 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -122,7 +122,7 @@ local function dispatch_wakeup() local session = sleep_session[co] if session then session_id_coroutine[session] = "BREAK" - return suspend(co, coroutine.resume(co, true)) + return suspend(co, coroutine.resume(co, true, "BREAK")) end end end @@ -246,7 +246,8 @@ function skynet.sleep(ti) session = tonumber(session) local succ, ret = coroutine_yield("SLEEP", session) sleep_session[coroutine.running()] = nil - if ret == true then + assert(succ, ret) + if ret == "BREAK" then return "BREAK" end end From bac9348db91115454739997ef1b8b789d55b43d7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Sep 2014 16:55:26 +0800 Subject: [PATCH 183/729] bugfix: sharedatad load name --- service/sharedatad.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index eb3346c4..c5c0c925 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -39,7 +39,7 @@ function CMD.new(name, t) value = t elseif dt == "string" then value = {} - local f = load(t, "=" .. name, "t", env) + local f = load(t, "=" .. name, "t", value) f() elseif dt == "nil" then value = {} From f8bf05513c8c7158f8ce7dd16407e24c75291f0e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Sep 2014 18:27:57 +0800 Subject: [PATCH 184/729] set _ENV to metatable when shadredatad read file --- service/sharedatad.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index c5c0c925..40c183c4 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -32,15 +32,18 @@ end local CMD = {} +local env_mt = { __index = _ENV } + function CMD.new(name, t) local dt = type(t) local value if dt == "table" then value = t elseif dt == "string" then - value = {} + value = setmetatable({}, env_mt) local f = load(t, "=" .. name, "t", value) f() + setmetatable(value, nil) elseif dt == "nil" then value = {} else From 9b901c6a3844587ce726fd72b3194e64d9f00f5c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 22 Sep 2014 13:36:20 +0800 Subject: [PATCH 185/729] release 0.7.1 --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index a63d2b4b..d476d18d 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +v0.7.1 (2014-9-22) +----------- +* bugfix: wakeup sleep should return BREAK +* bugfix: sharedatad load string +* bugfix: dataserver forward error msg + v0.7.0 (2014-9-8) ----------- * Use sproto instead of cjson From 228ad16d74525fde72506c162ac6963a337cbb72 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 22 Sep 2014 18:21:54 +0800 Subject: [PATCH 186/729] remove unneeded line --- skynet-src/socket_server.c | 1 - 1 file changed, 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 9821bd71..e1df3f67 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -335,7 +335,6 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock sock = -1; continue; } - sp_nonblocking(sock); break; } From f47e4275b6ef8a0b1d4806e2c182edbe83b75e88 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 22 Sep 2014 20:18:55 +0800 Subject: [PATCH 187/729] check new stream after finding cr --- lualib/http/internal.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 067152ad..0e83bc72 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -4,14 +4,15 @@ local LIMIT = 8192 local function chunksize(readbytes, body) while true do - if #body > 128 then - return - end - body = body .. readbytes() local f,e = body:find("\r\n",1,true) if f then return tonumber(body:sub(1,f-1),16), body:sub(e+1) end + if #body > 128 then + -- pervent the attacker send very long stream without \r\n + return + end + body = body .. readbytes() end end From 320a608c36b551edd16a488d3761719d5ef8aeb1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Sep 2014 16:26:31 +0800 Subject: [PATCH 188/729] bugfix: datacenter.wait --- service/datacenterd.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/service/datacenterd.lua b/service/datacenterd.lua index abc1eea6..8a7b65a0 100644 --- a/service/datacenterd.lua +++ b/service/datacenterd.lua @@ -33,7 +33,7 @@ local function update(db, key, value, ...) end end -local function wakeup(db, key1, key2, ...) +local function wakeup(db, key1, ...) if key1 == nil then return end @@ -43,7 +43,7 @@ local function wakeup(db, key1, key2, ...) end if q[mode] == "queue" then db[key1] = nil - if key2 then + if select("#", ...) ~= 1 then -- throw error because can't wake up a branch for _,response in ipairs(q) do response(false) @@ -53,7 +53,7 @@ local function wakeup(db, key1, key2, ...) end else -- it's branch - return wakeup(q , key2, ...) + return wakeup(q , ...) end end From ce6efc535c3f201c52f5567084f0f65a539d8e14 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Sep 2014 17:36:38 +0800 Subject: [PATCH 189/729] bugfix: error in forked coroutine. and add skynet.term() --- lualib/skynet.lua | 18 ++++++++++++------ lualib/skynet/debug.lua | 4 ++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 78f5bef9..13843199 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -143,13 +143,15 @@ end function suspend(co, result, command, param, size) if not result then local session = session_coroutine_id[co] - local addr = session_coroutine_address[co] - if session ~= 0 then - -- only call response error - c.send(addr, skynet.PTYPE_ERROR, session, "") + if session then -- coroutine may fork by others (session is nil) + local addr = session_coroutine_address[co] + if session ~= 0 then + -- only call response error + c.send(addr, skynet.PTYPE_ERROR, session, "") + end + session_coroutine_id[co] = nil + session_coroutine_address[co] = nil end - session_coroutine_id[co] = nil - session_coroutine_address[co] = nil error(debug.traceback(co,tostring(command))) end if command == "CALL" then @@ -696,6 +698,10 @@ function skynet.task(ret) return t end +function skynet.term(service) + return _error_dispatch(0, service) +end + -- Inject internal debug framework local debug = require "skynet.debug" debug(skynet, dispatch_message) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 5b7b5afa..4105ddcb 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -54,6 +54,10 @@ function dbgcmd.RUN(source, filename) skynet.ret(skynet.pack(table.concat(output, "\n"))) end +function dbgcmd.TERM(service) + skynet.term(service) +end + local function _debug_dispatch(session, address, cmd, ...) local f = dbgcmd[cmd] assert(f, cmd) From b923f93aee9ded0185f73df633741df37b5ad4ba Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Sep 2014 17:37:57 +0800 Subject: [PATCH 190/729] add testterm --- test/testterm.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 test/testterm.lua diff --git a/test/testterm.lua b/test/testterm.lua new file mode 100644 index 00000000..6b341ab1 --- /dev/null +++ b/test/testterm.lua @@ -0,0 +1,15 @@ +local skynet = require "skynet" + +local function term() + skynet.error("Sleep one second, and term the call to UNEXIST") + skynet.sleep(100) + local self = skynet.self() + skynet.send(skynet.self(), "debug", "TERM", "UNEXIST") +end + +skynet.start(function() + skynet.fork(term) + skynet.error("call an unexist named service UNEXIST, may block") + pcall(skynet.call, "UNEXIST", "lua", "test") + skynet.error("unblock the unexisted service call") +end) From 885c5b56bb1191db7e5bc4de45b31e4efc5b84b8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Sep 2014 17:50:17 +0800 Subject: [PATCH 191/729] bugfix: c.send returns nil when dest not exist --- lualib/skynet.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 13843199..746c3b3b 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -168,7 +168,7 @@ function suspend(co, result, command, param, size) session_response[co] = true local ret if not dead_service[co_address] then - ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, size) >= 0 + ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, size) ~= nil elseif size == nil then c.trash(param, size) ret = false @@ -202,9 +202,9 @@ function suspend(co, result, command, param, size) local ret if not dead_service[co_address] then if ok then - ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, f(...)) >=0 + ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, f(...)) ~= nil else - ret = c.send(co_address, skynet.PTYPE_ERROR, co_session, "") >=0 + ret = c.send(co_address, skynet.PTYPE_ERROR, co_session, "") ~= nil end else ret = false From f32f613ac02eedccc035127828289175aadebd68 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Sep 2014 18:16:24 +0800 Subject: [PATCH 192/729] add skynet.harbor.linkmaster --- examples/main_log.lua | 9 ++++++++- lualib/skynet/harbor.lua | 4 ++++ service/cslave.lua | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/examples/main_log.lua b/examples/main_log.lua index f74b563f..2a3fbd30 100644 --- a/examples/main_log.lua +++ b/examples/main_log.lua @@ -1,9 +1,16 @@ local skynet = require "skynet" +local harbor = require "skynet.harbor" + +local function monitor_master() + harbor.linkmaster() + print("master is down") + skynet.exit() +end skynet.start(function() print("Log server start") skynet.monitor "simplemonitor" local log = skynet.newservice("globallog") - skynet.exit() + skynet.fork(monitor_master) end) diff --git a/lualib/skynet/harbor.lua b/lualib/skynet/harbor.lua index b5863921..a8873f1d 100644 --- a/lualib/skynet/harbor.lua +++ b/lualib/skynet/harbor.lua @@ -19,4 +19,8 @@ function harbor.connect(id) skynet.call(".cslave", "lua", "CONNECT", id) end +function harbor.linkmaster() + skynet.call(".cslave", "lua", "LINKMASTER") +end + return harbor diff --git a/service/cslave.lua b/service/cslave.lua index c380f371..f06f1452 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -9,6 +9,7 @@ local queryname = {} local harbor = {} local harbor_service local monitor = {} +local monitor_master_set = {} local function read_package(fd) local sz = socket.read(fd, 1) @@ -99,6 +100,9 @@ local function monitor_master(master_fd) end else skynet.error("Master disconnect") + for _, v in ipairs(monitor_master_set) do + v(true) + end socket.close(master_fd) break end @@ -183,6 +187,10 @@ function harbor.LINK(fd, id) end end +function harbor.LINKMASTER() + table.insert(monitor_master_set, skynet.response()) +end + function harbor.CONNECT(fd, id) if not slaves[id] then if monitor[id] == nil then From 36d77aaa2bb4e622ca1b5925ad366c2b2b55a565 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 27 Sep 2014 01:00:37 +0800 Subject: [PATCH 193/729] bugfix: redirect need session --- service/cslave.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/cslave.lua b/service/cslave.lua index f06f1452..d1717bb8 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -59,7 +59,7 @@ local function ready() connect_slave(k,v) end for name,address in pairs(globalname) do - skynet.redirect(harbor_service, address, "harbor", "N " .. name) + skynet.redirect(harbor_service, address, "harbor", 0, "N " .. name) end end From 016b4dc58fdff175cd39a6be3a940d667c680754 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 27 Sep 2014 03:02:18 +0800 Subject: [PATCH 194/729] bugfix: sharedata can be update more than once --- lualib-src/lua-sharedata.c | 3 --- lualib/sharedata/corelib.lua | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 1a0f4f74..950d3f50 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -747,9 +747,6 @@ lupdate(lua_State *L) { luaL_checktype(L, 3, LUA_TTABLE); struct ctrl * c= lua_touserdata(L, 1); struct table *n = lua_touserdata(L, 2); - if (c->update) { - return luaL_error(L, "can't update more than once"); - } if (c->root == n) { return luaL_error(L, "You should update a new object"); } diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index d9a5488a..06b37fd6 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -59,15 +59,15 @@ function meta:__index(key) local newobj, newtbl = needupdate(self.__gcobj) if newobj then local newgcobj = newtbl.__gcobj - -- todo: update local root = findroot(self) update(root, newobj, newgcobj) if obj == self.__obj then error ("The key [" .. genkey(self) .. "] doesn't exist after update") end + obj = self.__obj end end - local v = index(self.__obj, key) + local v = index(obj, key) if type(v) == "userdata" then local r = setmetatable({ __obj = v, @@ -127,7 +127,7 @@ end function conf.update(self, pointer) local cobj = self.__obj - assert(isdirty(cobj), "Obly dirty object can be update") + assert(isdirty(cobj), "Only dirty object can be update") core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) }) end From c8775ee6f52d8958969a78fb1cf186eb02a463f3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 27 Sep 2014 14:55:14 +0800 Subject: [PATCH 195/729] clear dead response in sharedatad --- lualib/skynet.lua | 1 + service/sharedatad.lua | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 746c3b3b..11daa1e8 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -331,6 +331,7 @@ function skynet.time() end function skynet.exit() + fork_queue = {} -- no fork coroutine can be execute after skynet.exit skynet.send(".launcher","lua","REMOVE",skynet.self()) -- report the sources that call me for co, session in pairs(session_coroutine_id) do diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 40c183c4..191bab5d 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -4,6 +4,7 @@ local table = table local NORET = {} local pool = {} +local pool_count = {} local objmap = {} local function newobj(name, tbl) @@ -13,6 +14,7 @@ local function newobj(name, tbl) local v = { value = tbl , obj = cobj, watch = {} } objmap[cobj] = v pool[name] = v + pool_count[name] = { n = 0, threshold = 16 } end local function collectobj() @@ -55,10 +57,11 @@ end function CMD.delete(name) local v = assert(pool[name]) pool[name] = nil + pool_count[name] = nil assert(objmap[v.obj]) objmap[v.obj] = true sharedata.host.decref(v.obj) - for _,response in ipairs(v.watch) do + for _,response in pairs(v.watch) do response(true) end end @@ -86,23 +89,42 @@ function CMD.update(name, t) objmap[oldcobj] = true sharedata.host.decref(oldcobj) pool[name] = nil + pool_count[name] = nil end CMD.new(name, t) local newobj = pool[name].obj if watch then sharedata.host.markdirty(oldcobj) - for _,response in ipairs(watch) do + for _,response in pairs(watch) do response(true, newobj) end end end +local function check_watch(queue) + local n = 0 + for k,response in pairs(queue) do + if not response "TEST" then + queue[k] = nil + n = n + 1 + end + end + return n +end + function CMD.monitor(name, obj) local v = assert(pool[name]) if obj ~= v.obj then return v.obj end + local n = pool_count[name].n + pool_count[name].n = n + 1 + if n > pool_count[name].threshold then + n = n - check_watch(v.watch) + pool_count[name].threshold = n * 2 + end + table.insert(v.watch, skynet.response()) return NORET From aab65a27287e8052ec96b9c0390849cb5e948308 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 29 Sep 2014 14:42:42 +0800 Subject: [PATCH 196/729] socket accept report addr:port --- examples/watchdog.lua | 1 + skynet-src/socket_server.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index f37de5bf..eaedd5db 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -10,6 +10,7 @@ local gate local agent = {} function SOCKET.open(fd, addr) + skynet.error("New client from : " .. addr) agent[fd] = skynet.newservice("agent") skynet.call(agent[fd], "lua", "start", gate, fd, proto) end diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index e1df3f67..11ba4b0b 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -831,7 +831,10 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message result->data = NULL; void * sin_addr = (u.s.sa_family == AF_INET) ? (void*)&u.v4.sin_addr : (void *)&u.v6.sin6_addr; - if (inet_ntop(u.s.sa_family, sin_addr, ss->buffer, sizeof(ss->buffer))) { + int sin_port = ntohs((u.s.sa_family == AF_INET) ? u.v4.sin_port : u.v6.sin6_port); + char tmp[INET6_ADDRSTRLEN]; + if (inet_ntop(u.s.sa_family, sin_addr, tmp, sizeof(tmp))) { + snprintf(ss->buffer, sizeof(ss->buffer), "%s:%d", tmp, sin_port); result->data = ss->buffer; } From 7dee9a97c5b73a12e5181cee7d7fb19157a6c8f9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 29 Sep 2014 18:33:19 +0800 Subject: [PATCH 197/729] ready for 0.7.2 --- HISTORY.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index d476d18d..cd34e1f2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,11 @@ +v0.7.2 (2014-9-29) +----------- +* Bugfix : datacenter.wait +* Bugfix : error in forker coroutine +* Add skynet.term +* Accept socket report port +* sharedata can be update more than once + v0.7.1 (2014-9-22) ----------- * bugfix: wakeup sleep should return BREAK From 535bbc721954f45dd75bc7fa2ae029f1b9f9a54f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 30 Sep 2014 06:16:16 +0800 Subject: [PATCH 198/729] add log when listen --- lualib/snax/gateserver.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index bc5f6547..6cce5266 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -37,6 +37,7 @@ function gateserver.start(handler) local port = assert(conf.port) maxclient = conf.maxclient or 1024 nodelay = conf.nodelay + skynet.error(string.format("Listen on %s:%d", address, port)) socket = socketdriver.listen(address, port) socketdriver.start(socket) if handler.open then From 2f6bfe91047da77aa17bb50920251f36fa7a492d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 8 Oct 2014 22:42:01 +0800 Subject: [PATCH 199/729] add message overload warning --- lualib/skynet.lua | 6 ++++++ skynet-src/skynet_mq.c | 36 ++++++++++++++++++++++++++++--- skynet-src/skynet_mq.h | 1 + skynet-src/skynet_server.c | 4 ++++ test/testoverload.lua | 44 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 test/testoverload.lua diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 11daa1e8..03699f95 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -95,6 +95,7 @@ end local coroutine_pool = {} local coroutine_yield = coroutine.yield +local coroutine_count = 0 local function co_create(f) local co = table.remove(coroutine_pool) @@ -109,6 +110,11 @@ local function co_create(f) f(coroutine_yield()) end end) + coroutine_count = coroutine_count + 1 + if coroutine_count > 1024 then + skynet.error("May overload, create 1024 task") + coroutine_count = 0 + end else coroutine.resume(co, f) end diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index 027c4dc0..8cc8f9da 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -15,6 +15,7 @@ // 1 means mq is in global mq , or the message is dispatching. #define MQ_IN_GLOBAL 1 +#define MQ_OVERLOAD 1024 struct message_queue { uint32_t handle; @@ -24,6 +25,8 @@ struct message_queue { int lock; int release; int in_global; + int overload; + int overload_threshold; struct skynet_message *queue; struct message_queue *next; }; @@ -116,6 +119,8 @@ skynet_mq_create(uint32_t handle) { // If the service init success, skynet_context_new will call skynet_mq_force_push to push it to global queue. q->in_global = MQ_IN_GLOBAL; q->release = 0; + q->overload = 0; + q->overload_threshold = MQ_OVERLOAD; q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap); q->next = NULL; @@ -150,17 +155,42 @@ skynet_mq_length(struct message_queue *q) { return tail + cap - head; } +int +skynet_mq_overload(struct message_queue *q) { + if (q->overload) { + int overload = q->overload; + q->overload = 0; + return overload; + } + return 0; +} + int skynet_mq_pop(struct message_queue *q, struct skynet_message *message) { int ret = 1; LOCK(q) if (q->head != q->tail) { - *message = q->queue[q->head]; + *message = q->queue[q->head++]; ret = 0; - if ( ++ q->head >= q->cap) { - q->head = 0; + int head = q->head; + int tail = q->tail; + int cap = q->cap; + + if (head >= cap) { + q->head = head = 0; } + int length = tail - head; + if (length < 0) { + length += cap; + } + while (length > q->overload_threshold) { + q->overload = length; + q->overload_threshold *= 2; + } + } else { + // reset overload_threshold when queue is empty + q->overload_threshold = MQ_OVERLOAD; } if (ret) { diff --git a/skynet-src/skynet_mq.h b/skynet-src/skynet_mq.h index df1a9998..17178b4f 100644 --- a/skynet-src/skynet_mq.h +++ b/skynet-src/skynet_mq.h @@ -30,6 +30,7 @@ void skynet_mq_push(struct message_queue *q, struct skynet_message *message); // return the length of message queue, for debug int skynet_mq_length(struct message_queue *q); +int skynet_mq_overload(struct message_queue *q); void skynet_mq_init(); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index bdf0dcc1..50978b10 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -283,6 +283,10 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue n = skynet_mq_length(q); n >>= weight; } + int overload = skynet_mq_overload(q); + if (overload) { + skynet_error(ctx, "May overload, message queue length = %d", overload); + } skynet_monitor_trigger(sm, msg.source , handle); diff --git a/test/testoverload.lua b/test/testoverload.lua new file mode 100644 index 00000000..10fe39a8 --- /dev/null +++ b/test/testoverload.lua @@ -0,0 +1,44 @@ +local skynet = require "skynet" + +local mode = ... + +if mode == "slave" then + +local CMD = {} + +function CMD.sum(n) + skynet.error("for loop begin") + local s = 0 + for i = 1, n do + s = s + i + end + skynet.error("for loop end") +end + +function CMD.blackhole() +end + +skynet.start(function() + skynet.dispatch("lua", function(_,_, cmd, ...) + local f = CMD[cmd] + f(...) + end) +end) + +else + +skynet.start(function() + local slave = skynet.newservice(SERVICE_NAME, "slave") + for step = 1, 20 do + skynet.error("overload test ".. step) + for i = 1, 512 * step do + skynet.send(slave, "lua", "blackhole") + end + skynet.sleep(step) + end + local n = 1000000000 + skynet.error(string.format("endless test n=%d", n)) + skynet.send(slave, "lua", "sum", n) +end) + +end From 4a8bf4f39afd6012e9f53c7ae81eebca5e6a4416 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 10 Oct 2014 20:13:53 +0800 Subject: [PATCH 200/729] bugfix: harbor service should not release before others --- skynet-src/skynet_harbor.c | 12 ++++++++++++ skynet-src/skynet_harbor.h | 1 + skynet-src/skynet_server.c | 8 ++++++++ skynet-src/skynet_server.h | 1 + skynet-src/skynet_start.c | 1 + 5 files changed, 23 insertions(+) diff --git a/skynet-src/skynet_harbor.c b/skynet-src/skynet_harbor.c index 48814c83..379fce80 100644 --- a/skynet-src/skynet_harbor.c +++ b/skynet-src/skynet_harbor.c @@ -31,5 +31,17 @@ skynet_harbor_init(int harbor) { void skynet_harbor_start(void *ctx) { + // the HARBOR must be reserved to ensure the pointer is valid. + // It will be released at last by calling skynet_harbor_exit + skynet_context_reserve(ctx); REMOTE = ctx; } + +void +skynet_harbor_exit() { + struct skynet_context * ctx = REMOTE; + REMOTE= NULL; + if (ctx) { + skynet_context_release(ctx); + } +} diff --git a/skynet-src/skynet_harbor.h b/skynet-src/skynet_harbor.h index b699f625..62982455 100644 --- a/skynet-src/skynet_harbor.h +++ b/skynet-src/skynet_harbor.h @@ -26,5 +26,6 @@ void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int sessio int skynet_harbor_message_isremote(uint32_t handle); void skynet_harbor_init(int harbor); void skynet_harbor_start(void * ctx); +void skynet_harbor_exit(); #endif diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 50978b10..0a3a6b00 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -178,6 +178,14 @@ skynet_context_grab(struct skynet_context *ctx) { __sync_add_and_fetch(&ctx->ref,1); } +void +skynet_context_reserve(struct skynet_context *ctx) { + skynet_context_grab(ctx); + // don't count the context reserved, because skynet abort (the worker threads terminate) only when the total context is 0 . + // the reserved context will be release at last. + context_dec(); +} + static void delete_context(struct skynet_context *ctx) { if (ctx->logfile) { diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 27b6b768..92e125b0 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -10,6 +10,7 @@ struct skynet_monitor; struct skynet_context * skynet_context_new(const char * name, const char * parm); void skynet_context_grab(struct skynet_context *); +void skynet_context_reserve(struct skynet_context *ctx); struct skynet_context * skynet_context_release(struct skynet_context *); uint32_t skynet_context_handle(struct skynet_context *); int skynet_context_push(uint32_t handle, struct skynet_message *message); diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index bef06b4c..93ef4485 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -236,4 +236,5 @@ skynet_start(struct skynet_config * config) { if (config->daemon) { daemon_exit(config->daemon); } + skynet_harbor_exit(); } From 1c62785d714f4bcdff98becf51dc890b27d5e181 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 11 Oct 2014 13:10:39 +0800 Subject: [PATCH 201/729] CHECK ABORT every message dispatch --- skynet-src/skynet_start.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 93ef4485..cc582a68 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -125,9 +125,9 @@ _worker(void *p) { skynet_initthread(THREAD_WORKER); struct message_queue * q = NULL; for (;;) { + CHECK_ABORT q = skynet_context_message_dispatch(sm, q, weight); if (q == NULL) { - CHECK_ABORT if (pthread_mutex_lock(&m->mutex) == 0) { ++ m->sleep; // "spurious wakeup" is harmless, From c8b8dc12764bf2902bc2cfc82c99256046c8f222 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 11 Oct 2014 13:11:00 +0800 Subject: [PATCH 202/729] CHECK ABORT every message dispatch --- skynet-src/skynet_start.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index cc582a68..a6a63e8b 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -125,7 +125,6 @@ _worker(void *p) { skynet_initthread(THREAD_WORKER); struct message_queue * q = NULL; for (;;) { - CHECK_ABORT q = skynet_context_message_dispatch(sm, q, weight); if (q == NULL) { if (pthread_mutex_lock(&m->mutex) == 0) { @@ -140,6 +139,7 @@ _worker(void *p) { } } } + CHECK_ABORT } return NULL; } From 05f508fb42b2267a4512185b815a7a692d348530 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 11 Oct 2014 13:37:40 +0800 Subject: [PATCH 203/729] exit harbor before free socket --- skynet-src/skynet_start.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index a6a63e8b..5f511d29 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -232,9 +232,11 @@ skynet_start(struct skynet_config * config) { bootstrap(ctx, config->bootstrap); _start(config->thread); + + // harbor_exit may call socket send, so it should exit before socket_free + skynet_harbor_exit(); skynet_socket_free(); if (config->daemon) { daemon_exit(config->daemon); } - skynet_harbor_exit(); } From bfc3fd425a3a0eb23a0dbf62cdbe1f66272ef386 Mon Sep 17 00:00:00 2001 From: Nan Li Date: Sun, 12 Oct 2014 03:20:18 +0800 Subject: [PATCH 204/729] fix a minor bug when unpacking string from skynet_socket_message --- lualib-src/lua-socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 81322d17..094c958f 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -368,7 +368,7 @@ lunpack(lua_State *L) { lua_pushinteger(L, message->id); lua_pushinteger(L, message->ud); if (message->buffer == NULL) { - lua_pushlstring(L, (char *)(message+1),size - sizeof(*message)); + lua_pushlstring(L, (char *)(message+1),size - sizeof(*message) - 1); } else { lua_pushlightuserdata(L, message->buffer); } From d649d0adf1efb3028922329f1565f0c52f7ebb33 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Oct 2014 10:59:33 +0800 Subject: [PATCH 205/729] delete duplicate line --- service-src/service_snlua.c | 1 - 1 file changed, 1 deletion(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 50530e33..0063c8c6 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include From c458eba6b46c01e7930b8fc81cc3c3a5510c12a8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Oct 2014 11:03:30 +0800 Subject: [PATCH 206/729] update history for release --- HISTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index cd34e1f2..690b7680 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +v0.7.3 (2014-10-13) +----------- +* Add some logs (warning) when overload +* Bugfix: crash on exit + v0.7.2 (2014-9-29) ----------- * Bugfix : datacenter.wait From 44e6693a400641d21cc4d8ae2974a0e310b91440 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Oct 2014 16:47:40 +0800 Subject: [PATCH 207/729] clear coroutine_pool when gc --- lualib/skynet.lua | 9 ++++++++- lualib/skynet/debug.lua | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 03699f95..ec9258b1 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -709,8 +709,15 @@ function skynet.term(service) return _error_dispatch(0, service) end +local function clear_pool() + coroutine_pool = {} +end + -- Inject internal debug framework local debug = require "skynet.debug" -debug(skynet, dispatch_message) +debug(skynet, { + dispatch = dispatch_message, + clear = clear_pool, +}) return skynet diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 4105ddcb..d834bf23 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -2,7 +2,7 @@ local io = io local table = table local debug = debug -return function (skynet, dispatch_func) +return function (skynet, export) local internal_info_func @@ -18,7 +18,7 @@ function dbgcmd.MEM() end function dbgcmd.GC() - coroutine_pool = {} + export.clear() collectgarbage "collect" end @@ -49,7 +49,7 @@ end function dbgcmd.RUN(source, filename) local inject = require "skynet.inject" - local output = inject(source, filename , dispatch_func, skynet.register_protocol) + local output = inject(source, filename , export.dispatch, skynet.register_protocol) collectgarbage "collect" skynet.ret(skynet.pack(table.concat(output, "\n"))) end From 31d7b648b4fc2a7f0a3770b83cd2991cbe63d64a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Oct 2014 17:55:54 +0800 Subject: [PATCH 208/729] bugfix: not padding 0 --- lualib-src/lua-socket.c | 2 +- skynet-src/skynet_socket.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 094c958f..81322d17 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -368,7 +368,7 @@ lunpack(lua_State *L) { lua_pushinteger(L, message->id); lua_pushinteger(L, message->ud); if (message->buffer == NULL) { - lua_pushlstring(L, (char *)(message+1),size - sizeof(*message) - 1); + lua_pushlstring(L, (char *)(message+1),size - sizeof(*message)); } else { lua_pushlightuserdata(L, message->buffer); } diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 989f88e0..e9af5d26 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -36,10 +36,9 @@ forward_message(int type, bool padding, struct socket_message * result) { int sz = sizeof(*sm); if (padding) { if (result->data) { - sz += strlen(result->data) + 1; + sz += strlen(result->data); } else { result->data = ""; - sz += 1; } } sm = (struct skynet_socket_message *)skynet_malloc(sz); From 883757b936536c7221147d7c930ac2684e0e0b5d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Oct 2014 18:07:34 +0800 Subject: [PATCH 209/729] hotfix the bug introduce by last release --- HISTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 690b7680..e7771ed5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +v0.7.4 (2014-10-13) +----------- +* Bugfix : clear coroutine pool when GC +* hotfix : A bug introduce by 0.7.3 + v0.7.3 (2014-10-13) ----------- * Add some logs (warning) when overload From a074cd79bd8ee52083242877d27a9ce69a59d55b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Oct 2014 20:58:41 +0800 Subject: [PATCH 210/729] add nodelay option in socketchannel --- lualib/mongo.lua | 1 + lualib/redis.lua | 2 ++ lualib/socketchannel.lua | 5 +++++ 3 files changed, 8 insertions(+) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index b1e87ca7..458e1a6c 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -129,6 +129,7 @@ function mongo.client( conf ) response = dispatch_reply, auth = mongo_auth(obj), backup = backup, + nodelay = true, } setmetatable(obj, client_meta) obj.__sock:connect(true) -- try connect only once diff --git a/lualib/redis.lua b/lualib/redis.lua index cd9f43f4..87069e28 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -84,6 +84,7 @@ function redis.connect(db_conf) host = db_conf.host, port = db_conf.port or 6379, auth = redis_login(db_conf.auth, db_conf.db), + nodelay = true, } -- try connect first only once channel:connect(true) @@ -199,6 +200,7 @@ function redis.watch(db_conf) host = db_conf.host, port = db_conf.port or 6379, auth = watch_login(obj, db_conf.auth), + nodelay = true, } obj.__sock = channel diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 1086cd79..e8207489 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local socket = require "socket" +local socketdriver = require "socketdriver" -- channel support auto reconnect , and capture socket error in request/response transaction -- { host = "", port = , auth = function(so) , response = function(so) session, data } @@ -37,6 +38,7 @@ function socket_channel.channel(desc) __sock = false, __closed = false, __authcoroutine = false, + __nodelay = desc.nodelay, } return setmetatable(c, channel_meta) @@ -186,6 +188,9 @@ local function connect_once(self) return false end end + if self.__nodelay then + socketdriver.nodelay(fd) + end self.__sock = setmetatable( {fd} , channel_socket_meta ) skynet.fork(dispatch_function(self), self) From b39fc6ee23f26a4e2f0183ef8493c42e54a73e24 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 14 Oct 2014 18:34:04 +0800 Subject: [PATCH 211/729] update sproto for bugfix --- lualib-src/sproto/sproto.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 0f519d14..ca43cd9b 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -1076,9 +1076,15 @@ pack_seg(const uint8_t *src, uint8_t * buffer, int sz, int n) { static inline void write_ff(const uint8_t * src, uint8_t * des, int n) { + int i; + int align8_n = (n+7)&(~7); + des[0] = 0xff; - des[1] = n-1; - memcpy(des+2, src, n * 8); + des[1] = align8_n/8 - 1; + memcpy(des+2, src, n); + for(i=0; i< align8_n-n; i++){ + des[n+2+i] = 0; + } } int @@ -1092,6 +1098,7 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { const uint8_t * src = srcv; uint8_t * buffer = bufferv; for (i=0;i 0) { int j; @@ -1101,7 +1108,7 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { } src = tmp; } - int n = pack_seg(src, buffer, bufsz, ff_n); + n = pack_seg(src, buffer, bufsz, ff_n); bufsz -= n; if (n == 10) { // first FF @@ -1112,14 +1119,14 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { ++ff_n; if (ff_n == 256) { if (bufsz >= 0) { - write_ff(ff_srcstart, ff_desstart, 256); + write_ff(ff_srcstart, ff_desstart, 256*8); } ff_n = 0; } } else { if (ff_n > 0) { if (bufsz >= 0) { - write_ff(ff_srcstart, ff_desstart, ff_n); + write_ff(ff_srcstart, ff_desstart, ff_n*8); } ff_n = 0; } @@ -1128,8 +1135,11 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { buffer += n; size += n; } - if (ff_n > 0 && bufsz >= 0) { - write_ff(ff_srcstart, ff_desstart, ff_n); + if(bufsz >= 0){ + if(ff_n == 1) + write_ff(ff_srcstart, ff_desstart, 8); + else if (ff_n > 1) + write_ff(ff_srcstart, ff_desstart, srcsz - (intptr_t)(ff_srcstart - (const uint8_t*)srcv)); } return size; } @@ -1144,10 +1154,11 @@ sproto_unpack(const void * srcv, int srcsz, void * bufferv, int bufsz) { --srcsz; ++src; if (header == 0xff) { + int n; if (srcsz < 0) { return -1; } - int n = (src[0] + 1) * 8; + n = (src[0] + 1) * 8; if (srcsz < n + 1) return -1; srcsz -= n + 1; From 1ef5430e0ad0ec45ce1d7e9d9b40bf3eb25c21eb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 15 Oct 2014 10:16:28 +0800 Subject: [PATCH 212/729] should not copy the ending 0 --- skynet-src/skynet_socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index e9af5d26..573b9f0e 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -47,7 +47,7 @@ forward_message(int type, bool padding, struct socket_message * result) { sm->ud = result->ud; if (padding) { sm->buffer = NULL; - strcpy((char*)(sm+1), result->data); + memcpy(sm+1, result->data, sz - sizeof(*sm)); } else { sm->buffer = result->data; } From 3b95ecd9b22d736dbb65009c4a0d764f4e163aaf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 17 Oct 2014 11:22:49 +0800 Subject: [PATCH 213/729] bugfix: skynet.queue --- lualib/skynet/queue.lua | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lualib/skynet/queue.lua b/lualib/skynet/queue.lua index 9f40e247..b427da6b 100644 --- a/lualib/skynet/queue.lua +++ b/lualib/skynet/queue.lua @@ -9,21 +9,20 @@ function skynet.queue() local thread_queue = {} return function(f, ...) local thread = coroutine.running() - if ref == 0 then - current_thread = thread - elseif current_thread ~= thread then + if current_thread and current_thread ~= thread then table.insert(thread_queue, thread) skynet.wait() - assert(ref == 0) + assert(ref == 0) -- current_thread == thread end + current_thread = thread + ref = ref + 1 local ok, err = pcall(f, ...) ref = ref - 1 if ref == 0 then - current_thread = nil - local co = table.remove(thread_queue,1) - if co then - skynet.wakeup(co) + current_thread = table.remove(thread_queue,1) + if current_thread then + skynet.wakeup(current_thread) end end assert(ok,err) From 1d8ca31d55bd64a4650f6df1546e180a5e60a35c Mon Sep 17 00:00:00 2001 From: niuys Date: Tue, 21 Oct 2014 16:34:25 +0800 Subject: [PATCH 214/729] message sequence wrong for queue When the lua state yield in "more queue" proceeding, "data" comes, it should wait in queue, not immediatly do message process. --- lualib-src/lua-netpack.c | 41 +++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 4012c9bc..1e2ad632 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -240,12 +240,19 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) { buffer += need; size -= need; if (size == 0) { - lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); - lua_pushinteger(L, fd); - lua_pushlightuserdata(L, uc->pack.buffer); - lua_pushinteger(L, uc->pack.size); - skynet_free(uc); - return 5; + if (q == NULL || q->head == q->tail ) { + lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); + lua_pushinteger(L, fd); + lua_pushlightuserdata(L, uc->pack.buffer); + lua_pushinteger(L, uc->pack.size); + skynet_free(uc); + return 5; + } + else{ + push_data(L, fd, uc->pack.buffer, uc->pack.size, 0); + skynet_free(uc); + return 1; + } } // more data push_data(L, fd, uc->pack.buffer, uc->pack.size, 0); @@ -274,13 +281,21 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) { } if (size == pack_size) { // just one package - lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); - lua_pushinteger(L, fd); - void * result = skynet_malloc(pack_size); - memcpy(result, buffer, size); - lua_pushlightuserdata(L, result); - lua_pushinteger(L, size); - return 5; + if ( q == NULL || q->head == q->tail) { + lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); + lua_pushinteger(L, fd); + void * result = skynet_malloc(pack_size); + memcpy(result, buffer, size); + lua_pushlightuserdata(L, result); + lua_pushinteger(L, size); + return 5; + } + else{ + push_data(L, fd, buffer, pack_size, 1); + buffer += pack_size; + size -= pack_size; + return 1; + } } // more data push_data(L, fd, buffer, pack_size, 1); From 9bdc36526ef5e5094b629d800421509e2638a6ee Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 22 Oct 2014 11:29:03 +0800 Subject: [PATCH 215/729] close socket after request --- lualib/http/httpc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 784b186b..a73d5710 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -81,10 +81,10 @@ function httpc.request(method, host, url, recvheader, header, content) end local fd = socket.connect(hostname, port) local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content) + socket.close(fd) if ok then return statuscode, body else - socket.close(fd) error(statuscode) end end From fc8983227d861b341c03c7f0e531dee220cfae49 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 22 Oct 2014 20:32:23 +0800 Subject: [PATCH 216/729] bugfix issue #185 --- lualib-src/lua-seri.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 1acfb974..fe4bfa46 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -120,37 +120,37 @@ rb_read(struct read_block *rb, void *buffer, int sz) { static inline void wb_nil(struct write_block *wb) { - int n = TYPE_NIL; + uint8_t n = TYPE_NIL; wb_push(wb, &n, 1); } static inline void wb_boolean(struct write_block *wb, int boolean) { - int n = COMBINE_TYPE(TYPE_BOOLEAN , boolean ? 1 : 0); + uint8_t n = COMBINE_TYPE(TYPE_BOOLEAN , boolean ? 1 : 0); wb_push(wb, &n, 1); } static inline void wb_integer(struct write_block *wb, int v, int type) { if (v == 0) { - int n = COMBINE_TYPE(type , 0); + uint8_t n = COMBINE_TYPE(type , 0); wb_push(wb, &n, 1); } else if (v<0) { - int n = COMBINE_TYPE(type , 4); + uint8_t n = COMBINE_TYPE(type , 4); wb_push(wb, &n, 1); wb_push(wb, &v, 4); } else if (v<0x100) { - int n = COMBINE_TYPE(type , 1); + uint8_t n = COMBINE_TYPE(type , 1); wb_push(wb, &n, 1); uint8_t byte = (uint8_t)v; wb_push(wb, &byte, 1); } else if (v<0x10000) { - int n = COMBINE_TYPE(type , 2); + uint8_t n = COMBINE_TYPE(type , 2); wb_push(wb, &n, 1); uint16_t word = (uint16_t)v; wb_push(wb, &word, 2); } else { - int n = COMBINE_TYPE(type , 4); + uint8_t n = COMBINE_TYPE(type , 4); wb_push(wb, &n, 1); wb_push(wb, &v, 4); } @@ -158,14 +158,14 @@ wb_integer(struct write_block *wb, int v, int type) { static inline void wb_number(struct write_block *wb, double v) { - int n = COMBINE_TYPE(TYPE_NUMBER , 8); + uint8_t n = COMBINE_TYPE(TYPE_NUMBER , 8); wb_push(wb, &n, 1); wb_push(wb, &v, 8); } static inline void wb_pointer(struct write_block *wb, void *v) { - int n = TYPE_USERDATA; + uint8_t n = TYPE_USERDATA; wb_push(wb, &n, 1); wb_push(wb, &v, sizeof(v)); } @@ -173,7 +173,7 @@ wb_pointer(struct write_block *wb, void *v) { static inline void wb_string(struct write_block *wb, const char *str, int len) { if (len < MAX_COOKIE) { - int n = COMBINE_TYPE(TYPE_SHORT_STRING, len); + uint8_t n = COMBINE_TYPE(TYPE_SHORT_STRING, len); wb_push(wb, &n, 1); if (len > 0) { wb_push(wb, str, len); @@ -201,11 +201,11 @@ static int wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) { int array_size = lua_rawlen(L,index); if (array_size >= MAX_COOKIE-1) { - int n = COMBINE_TYPE(TYPE_TABLE, MAX_COOKIE-1); + uint8_t n = COMBINE_TYPE(TYPE_TABLE, MAX_COOKIE-1); wb_push(wb, &n, 1); wb_integer(wb, array_size,TYPE_NUMBER); } else { - int n = COMBINE_TYPE(TYPE_TABLE, array_size); + uint8_t n = COMBINE_TYPE(TYPE_TABLE, array_size); wb_push(wb, &n, 1); } From 82bddd9cb8f0388a5e55d1b47c9f04f182d36676 Mon Sep 17 00:00:00 2001 From: snail Date: Fri, 24 Oct 2014 19:14:01 +0800 Subject: [PATCH 217/729] use xpall easy for debug use xpall to show more debug message --- lualib/skynet/queue.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/queue.lua b/lualib/skynet/queue.lua index 9f40e247..1b679902 100644 --- a/lualib/skynet/queue.lua +++ b/lualib/skynet/queue.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" local coroutine = coroutine -local pcall = pcall +local xpcall = xpcall +local traceback = debug.traceback local table = table function skynet.queue() @@ -17,7 +18,7 @@ function skynet.queue() assert(ref == 0) end ref = ref + 1 - local ok, err = pcall(f, ...) + local ok, err = xpcall(f, traceback, ...) ref = ref - 1 if ref == 0 then current_thread = nil @@ -30,4 +31,4 @@ function skynet.queue() end end -return skynet.queue \ No newline at end of file +return skynet.queue From 73bd788a6ca3e8554900077e5abd6dbe4fc104cb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 27 Oct 2014 10:43:54 +0800 Subject: [PATCH 218/729] ready for v0.8.0 --- HISTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index e7771ed5..64c30744 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +v0.8.0 (2014-10-27) +----------- +* Add mysql client driver +* Bugfix : skynet.queue + v0.7.4 (2014-10-13) ----------- * Bugfix : clear coroutine pool when GC From 01e1a6aafada7cb92159a9b9f7751682fdecd894 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Oct 2014 16:04:01 +0800 Subject: [PATCH 219/729] fix issue #189 --- skynet-src/skynet_server.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 0a3a6b00..5464c12b 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -169,7 +169,11 @@ skynet_context_new(const char * name, const char *param) { int skynet_context_newsession(struct skynet_context *ctx) { // session always be a positive number - int session = (++ctx->session_id) & 0x7fffffff; + int session = ++ctx->session_id; + if (session <= 0) { + ctx->session_id = 1; + return 1; + } return session; } From 9850ca1b94701673c479ede71c4fde5b84ac2ab6 Mon Sep 17 00:00:00 2001 From: czlc Date: Tue, 28 Oct 2014 16:43:29 +0800 Subject: [PATCH 220/729] free buffer --- skynet-src/skynet_socket.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 573b9f0e..b617418a 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -61,6 +61,8 @@ forward_message(int type, bool padding, struct socket_message * result) { if (skynet_context_push((uint32_t)result->opaque, &message)) { // todo: report somewhere to close socket // don't call skynet_socket_close here (It will block mainloop) + if (sm->buffer) + skynet_free(sm->buffer); skynet_free(sm); } } From 954625a53058ef2b7cc9ab8a1bc927c37b15746c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Oct 2014 16:48:35 +0800 Subject: [PATCH 221/729] fix issue #190 --- skynet-src/skynet_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 5464c12b..a1961f13 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -695,7 +695,7 @@ skynet_sendname(struct skynet_context * context, uint32_t source, const char * a if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } - return session; + return -1; } } else { _filter_args(context, type, &session, (void **)&data, &sz); From b1ef8a33d9c09b68f6761c91454c92da3ac26f93 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Oct 2014 17:00:24 +0800 Subject: [PATCH 222/729] harbor can post error message back when the destination is not exist --- service-src/service_harbor.c | 1 + 1 file changed, 1 insertion(+) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 3fd834b4..b78953d1 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -316,6 +316,7 @@ forward_local_messsage(struct harbor *h, void *msg, int sz) { destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); if (skynet_send(h->ctx, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) { + skynet_send(h->ctx, destination, header.source , PTYPE_ERROR, (int)header.session, NULL, 0); skynet_error(h->ctx, "Unknown destination :%x from :%x", destination, header.source); } } From aaf8617dd53676ea734a17661885504faabded10 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Oct 2014 17:14:30 +0800 Subject: [PATCH 223/729] skynet_free(NULL) is ok --- skynet-src/skynet_socket.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index b617418a..38ba4ea9 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -61,8 +61,7 @@ forward_message(int type, bool padding, struct socket_message * result) { if (skynet_context_push((uint32_t)result->opaque, &message)) { // todo: report somewhere to close socket // don't call skynet_socket_close here (It will block mainloop) - if (sm->buffer) - skynet_free(sm->buffer); + skynet_free(sm->buffer); skynet_free(sm); } } From af3ca3bb2f06652508ef7ed95c3022cfea93e884 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Oct 2014 19:40:27 +0800 Subject: [PATCH 224/729] merge lua bugfix, read http://www.lua.org/bugs.html --- 3rd/lua/lgc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index c0f2858c..51c11f5b 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -403,7 +403,7 @@ static int traverseephemeron (global_State *g, Table *h) { reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ } } - if (prop) + if (g->gcstate != GCSatomic || prop) linktable(h, &g->ephemeron); /* have to propagate again */ else if (hasclears) /* does table have white keys? */ linktable(h, &g->allweak); /* may have to clean white keys */ From f8a50929a8554f74fb02c8b1ffcb7b2ccdb6e27f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Oct 2014 11:04:35 +0800 Subject: [PATCH 225/729] bugfix: break when write all --- lualib/http/httpd.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 5f8f6de8..944c9584 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -130,6 +130,7 @@ local function writeall(writefunc, statuscode, bodyfunc, header) end else writefunc("\r\n0\r\n\r\n") + break end end else From 2ab689f7c1f95a0d7222d3c242d7206b1efd9635 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Oct 2014 11:18:47 +0800 Subject: [PATCH 226/729] remove sha1 from lua_mysqlaux, use lua-crypt instead --- 3rd/lua-mysqlaux/lua_mysqlaux.c | 372 -------------------------------- Makefile | 2 +- lualib-src/lua_mysqlaux.c | 170 +++++++++++++++ lualib/mysql.lua | 4 +- 4 files changed, 173 insertions(+), 375 deletions(-) delete mode 100755 3rd/lua-mysqlaux/lua_mysqlaux.c create mode 100755 lualib-src/lua_mysqlaux.c diff --git a/3rd/lua-mysqlaux/lua_mysqlaux.c b/3rd/lua-mysqlaux/lua_mysqlaux.c deleted file mode 100755 index ba170ddb..00000000 --- a/3rd/lua-mysqlaux/lua_mysqlaux.c +++ /dev/null @@ -1,372 +0,0 @@ -// -// lua_mysqlaux.c -// -// Created by changfeng on 6/17/14. -// Copyright (c) 2014 changfeng. All rights reserved. -// -#include -#include -#include - -#include -#include - -#define SHA1SIZE 20 -#define ROTL(bits,word) (((word) << (bits)) | ((word) >> (32-(bits)))) -typedef unsigned int uint32_t; - -struct sha -{ - uint32_t digest[5]; - uint32_t w[80]; - uint32_t a,b,c,d,e,f; - int err; -}; - - -static uint32_t padded_length_in_bits(uint32_t len) -{ - if(len%64 == 56) - { - len++; - } - while((len%64)!=56) - { - len++; - } - return len*8; -} - - -static int calculate_sha1(struct sha *sha1, const unsigned char *text, uint32_t length) -{ - unsigned int i,j; - unsigned char *buffer=NULL, *pbuffer=NULL; - uint32_t bits=0; - uint32_t temp=0,k=0; - uint32_t lb = length*8; - - if (!sha1) - { - return 0; - } - // initialize the default digest values - sha1->digest[0] = 0x67452301; - sha1->digest[1] = 0xEFCDAB89; - sha1->digest[2] = 0x98BADCFE; - sha1->digest[3] = 0x10325476; - sha1->digest[4] = 0xC3D2E1F0; - sha1->a=sha1->b=sha1->c=sha1->d=sha1->e=sha1->f=0; - if (!text || !length) - { - return 0; - } - - bits = padded_length_in_bits(length); - buffer = (unsigned char *) malloc((bits/8)+8); - memset(buffer,0,(bits/8)+8); - if(buffer == NULL) - { - return 1; - } - pbuffer = buffer; - memcpy(buffer, text, length); - - - //add 1 on the last of the message.. - *(buffer+length) = 0x80; - for(i=length+1; i<(bits/8); i++) - { - *(buffer+i) = 0x00; - } - - *(buffer +(bits/8)+4+0) = (lb>>24) & 0xFF; - *(buffer +(bits/8)+4+1) = (lb>>16) & 0xFF; - *(buffer +(bits/8)+4+2) = (lb>>8) & 0xFF; - *(buffer +(bits/8)+4+3) = (lb>>0) & 0xFF; - - - //main loop - for(i=0; i<((bits+64)/512); i++) - { - //first empty the block for each pass.. - for(j=0; j<80; j++) - { - sha1->w[j] = 0x00; - } - - - //fill the first 16 words with the characters read directly from the buffer. - for(j=0; j<16; j++) - { - sha1->w[j] =buffer[j*4+0]; - sha1->w[j] = sha1->w[j]<<8; - sha1->w[j] |= buffer[j*4+1]; - sha1->w[j] = sha1->w[j]<<8; - sha1->w[j] |= buffer[j*4+2]; - sha1->w[j] = sha1->w[j]<<8; - sha1->w[j] |= buffer[j*4+3]; - } - - //fill the rest 64 words using the formula - for(j=16; j<80; j++) - { - sha1->w[j] = (ROTL(1,(sha1->w[j-3] ^ sha1->w[j-8] ^ sha1->w[j-14] ^ sha1->w[j-16]))); - } - - - //initialize hash for this chunck reading that has been stored in the structure digest - sha1->a = sha1->digest[0]; - sha1->b = sha1->digest[1]; - sha1->c = sha1->digest[2]; - sha1->d = sha1->digest[3]; - sha1->e = sha1->digest[4]; - - //for all the 80 32bit blocks calculate f and use k accordingly per specification. - for(j=0; j<80; j++) - { - if((j>=0) && (j<20)) - { - sha1->f = ((sha1->b)&(sha1->c)) | ((~(sha1->b))&(sha1->d)); - k = 0x5A827999; - - } - else if((j>=20) && (j<40)) - { - sha1->f = (sha1->b)^(sha1->c)^(sha1->d); - k = 0x6ED9EBA1; - } - else if((j>=40) && (j<60)) - { - sha1->f = ((sha1->b)&(sha1->c)) | ((sha1->b)&(sha1->d)) | ((sha1->c)&(sha1->d)); - k = 0x8F1BBCDC; - } - else if((j>=60) && (j<80)) - { - sha1->f = (sha1->b)^(sha1->c)^(sha1->d); - k = 0xCA62C1D6; - } - - temp = ROTL(5,(sha1->a)) + (sha1->f) + (sha1->e) + k + sha1->w[j]; - sha1->e = (sha1->d); - sha1->d = (sha1->c); - sha1->c = ROTL(30,(sha1->b)); - sha1->b = (sha1->a); - sha1->a = temp; - - //reset temp to 0 to be in safe side only, not mandatory. - temp =0x00; - - - } - - // append to total hash. - sha1->digest[0] += sha1->a; - sha1->digest[1] += sha1->b; - sha1->digest[2] += sha1->c; - sha1->digest[3] += sha1->d; - sha1->digest[4] += sha1->e; - - - //since we used 512bit size block per each pass, let us update the buffer pointer accordingly. - buffer = buffer+64; - - } - free(pbuffer); - return 0; -} - -static void int2ch4(int intVal,unsigned char *result) -{ - result[0]= (unsigned char)((intVal>>24) & 0x000000ff); - result[1]= (unsigned char)((intVal>>16) & 0x000000ff); - result[2]= (unsigned char)((intVal>> 8) & 0x000000ff); - result[3]= (unsigned char)((intVal>> 0) & 0x000000ff); -} - - -static int sha1_bin (lua_State *L) { - const void * msg = NULL; - size_t len =0; - - if( lua_gettop(L) != 1 ){ - return 0; - } - if( lua_isnil(L,1) ) { - msg = NULL; - len =0; - }else{ - msg=luaL_checklstring(L,1,&len); - } - struct sha tmpsha; - calculate_sha1( &tmpsha, msg, (uint32_t)len); - unsigned char tmpret[SHA1SIZE+8]; - memset(tmpret,0,SHA1SIZE+8); - int i=0; - for ( i=0; i<5; i++) - { - int2ch4(tmpsha.digest[i], tmpret+i*4); - } - - lua_pushlstring(L, (char *)tmpret, SHA1SIZE); - return 1; -} - -static unsigned int num_escape_sql_str(unsigned char *dst, unsigned char *src, size_t size) -{ - unsigned int n =0; - while (size) { - /* the highest bit of all the UTF-8 chars - * is always 1 */ - if ((*src & 0x80) == 0) { - switch (*src) { - case '\0': - case '\b': - case '\n': - case '\r': - case '\t': - case 26: /* \z */ - case '\\': - case '\'': - case '"': - n++; - break; - default: - break; - } - } - src++; - size--; - } - return n; -} -static unsigned char* -escape_sql_str(unsigned char *dst, unsigned char *src, size_t size) -{ - - while (size) { - if ((*src & 0x80) == 0) { - switch (*src) { - case '\0': - *dst++ = '\\'; - *dst++ = '0'; - break; - - case '\b': - *dst++ = '\\'; - *dst++ = 'b'; - break; - - case '\n': - *dst++ = '\\'; - *dst++ = 'n'; - break; - - case '\r': - *dst++ = '\\'; - *dst++ = 'r'; - break; - - case '\t': - *dst++ = '\\'; - *dst++ = 't'; - break; - - case 26: - *dst++ = '\\'; - *dst++ = 'z'; - break; - - case '\\': - *dst++ = '\\'; - *dst++ = '\\'; - break; - - case '\'': - *dst++ = '\\'; - *dst++ = '\''; - break; - - case '"': - *dst++ = '\\'; - *dst++ = '"'; - break; - - default: - *dst++ = *src; - break; - } - } else { - *dst++ = *src; - } - src++; - size--; - } /* while (size) */ - - return dst; -} - - - - -static int -quote_sql_str(lua_State *L) -{ - size_t len, dlen, escape; - unsigned char *p; - unsigned char *src, *dst; - - if (lua_gettop(L) != 1) { - return luaL_error(L, "expecting one argument"); - } - - src = (unsigned char *) luaL_checklstring(L, 1, &len); - - if (len == 0) { - dst = (unsigned char *) "''"; - dlen = sizeof("''") - 1; - lua_pushlstring(L, (char *) dst, dlen); - return 1; - } - - escape = num_escape_sql_str(NULL, src, len); - - dlen = sizeof("''") - 1 + len + escape; - p = lua_newuserdata(L, dlen); - - dst = p; - - *p++ = '\''; - - if (escape == 0) { - memcpy(p, src, len); - p+=len; - } else { - p = (unsigned char *) escape_sql_str(p, src, len); - } - - *p++ = '\''; - - if (p != dst + dlen) { - return luaL_error(L, "quote sql string error"); - } - - lua_pushlstring(L, (char *) dst, p - dst); - - return 1; -} - - -static struct luaL_Reg mysqlauxlib[] = { - {"sha1_bin", sha1_bin}, - {"quote_sql_str",quote_sql_str}, - {NULL, NULL} -}; - - -int luaopen_mysqlaux_c (lua_State *L) { - lua_newtable(L); - luaL_setfuncs(L, mysqlauxlib, 0); - return 1; -} - diff --git a/Makefile b/Makefile index ec52fcd3..0d0cd855 100644 --- a/Makefile +++ b/Makefile @@ -123,7 +123,7 @@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsprot $(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ -$(LUA_CLIB_PATH)/mysqlaux.so : 3rd/lua-mysqlaux/lua_mysqlaux.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/mysqlaux.so : lualib-src/lua_mysqlaux.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ clean : diff --git a/lualib-src/lua_mysqlaux.c b/lualib-src/lua_mysqlaux.c new file mode 100755 index 00000000..31fabad8 --- /dev/null +++ b/lualib-src/lua_mysqlaux.c @@ -0,0 +1,170 @@ +// +// lua_mysqlaux.c +// +// Created by changfeng on 6/17/14. +// Copyright (c) 2014 changfeng. All rights reserved. +// +#include +#include +#include + +#include +#include + +static unsigned int num_escape_sql_str(unsigned char *dst, unsigned char *src, size_t size) +{ + unsigned int n =0; + while (size) { + /* the highest bit of all the UTF-8 chars + * is always 1 */ + if ((*src & 0x80) == 0) { + switch (*src) { + case '\0': + case '\b': + case '\n': + case '\r': + case '\t': + case 26: /* \z */ + case '\\': + case '\'': + case '"': + n++; + break; + default: + break; + } + } + src++; + size--; + } + return n; +} +static unsigned char* +escape_sql_str(unsigned char *dst, unsigned char *src, size_t size) +{ + + while (size) { + if ((*src & 0x80) == 0) { + switch (*src) { + case '\0': + *dst++ = '\\'; + *dst++ = '0'; + break; + + case '\b': + *dst++ = '\\'; + *dst++ = 'b'; + break; + + case '\n': + *dst++ = '\\'; + *dst++ = 'n'; + break; + + case '\r': + *dst++ = '\\'; + *dst++ = 'r'; + break; + + case '\t': + *dst++ = '\\'; + *dst++ = 't'; + break; + + case 26: + *dst++ = '\\'; + *dst++ = 'z'; + break; + + case '\\': + *dst++ = '\\'; + *dst++ = '\\'; + break; + + case '\'': + *dst++ = '\\'; + *dst++ = '\''; + break; + + case '"': + *dst++ = '\\'; + *dst++ = '"'; + break; + + default: + *dst++ = *src; + break; + } + } else { + *dst++ = *src; + } + src++; + size--; + } /* while (size) */ + + return dst; +} + + + + +static int +quote_sql_str(lua_State *L) +{ + size_t len, dlen, escape; + unsigned char *p; + unsigned char *src, *dst; + + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one argument"); + } + + src = (unsigned char *) luaL_checklstring(L, 1, &len); + + if (len == 0) { + dst = (unsigned char *) "''"; + dlen = sizeof("''") - 1; + lua_pushlstring(L, (char *) dst, dlen); + return 1; + } + + escape = num_escape_sql_str(NULL, src, len); + + dlen = sizeof("''") - 1 + len + escape; + p = lua_newuserdata(L, dlen); + + dst = p; + + *p++ = '\''; + + if (escape == 0) { + memcpy(p, src, len); + p+=len; + } else { + p = (unsigned char *) escape_sql_str(p, src, len); + } + + *p++ = '\''; + + if (p != dst + dlen) { + return luaL_error(L, "quote sql string error"); + } + + lua_pushlstring(L, (char *) dst, p - dst); + + return 1; +} + + +static struct luaL_Reg mysqlauxlib[] = { + {"quote_sql_str",quote_sql_str}, + {NULL, NULL} +}; + + +int luaopen_mysqlaux_c (lua_State *L) { + lua_newtable(L); + luaL_setfuncs(L, mysqlauxlib, 0); + return 1; +} + diff --git a/lualib/mysql.lua b/lualib/mysql.lua index b81e2ecc..58843e68 100755 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -6,7 +6,7 @@ local socketchannel = require "socketchannel" local bit = require "bit32" local mysqlaux = require "mysqlaux.c" - +local crypt = require "crypt" local sub = string.sub @@ -20,7 +20,7 @@ local bxor = bit.bxor local bor = bit.bor local lshift = bit.lshift local rshift = bit.rshift -local sha1= mysqlaux.sha1_bin +local sha1= crypt.sha1 local concat = table.concat local unpack = unpack local setmetatable = setmetatable From 6c9ad16077521fd814e2c2a1c82afe9e3207e61c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Oct 2014 16:51:39 +0800 Subject: [PATCH 227/729] bugfix: socket open address, and httpd bodylimit --- examples/simpleweb.lua | 1 + lualib-src/lua-netpack.c | 8 ++++---- lualib/http/httpd.lua | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index 234c2d08..5d38dfb1 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -66,6 +66,7 @@ skynet.start(function() end local balance = 1 local id = socket.listen("0.0.0.0", 8001) + skynet.error("Listen web port 8001") socket.start(id , function(id, addr) skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance])) skynet.send(agent[balance], "lua", id) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 1e2ad632..e424f90c 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -317,9 +317,9 @@ filter_data(lua_State *L, int fd, uint8_t * buffer, int size) { } static void -pushstring(lua_State *L, const char * msg) { +pushstring(lua_State *L, const char * msg, int size) { if (msg) { - lua_pushstring(L, msg); + lua_pushlstring(L, msg, size); } else { lua_pushliteral(L, ""); } @@ -365,12 +365,12 @@ lfilter(lua_State *L) { lua_pushvalue(L, lua_upvalueindex(TYPE_OPEN)); // ignore listen id (message->id); lua_pushinteger(L, message->ud); - pushstring(L, buffer); + pushstring(L, buffer, size); return 4; case SKYNET_SOCKET_TYPE_ERROR: lua_pushvalue(L, lua_upvalueindex(TYPE_ERROR)); lua_pushinteger(L, message->id); - pushstring(L, buffer); + pushstring(L, buffer, size); return 4; default: // never get here diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 944c9584..66431894 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -83,7 +83,7 @@ local function readall(readbytes, bodylimit) else -- identity mode if length then - if length > bodylimit then + if bodylimit and length > bodylimit then return 413 end if #body >= length then From 803a79059a6f2df344907c46735aec9a229f576d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 31 Oct 2014 16:49:54 +0800 Subject: [PATCH 228/729] update sproto lib --- lualib-src/sproto/lsproto.c | 72 ++++++++------ lualib-src/sproto/sproto.c | 187 +++++++++++++++++++++--------------- 2 files changed, 155 insertions(+), 104 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 602f86e9..af86e389 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -19,17 +19,17 @@ */ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { #ifdef luaL_checkversion - luaL_checkversion(L); + luaL_checkversion(L); #endif - luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_setfield(L, -(nup + 2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ + luaL_checkstack(L, nup, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -nup); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_setfield(L, -(nup + 2), l->name); + } + lua_pop(L, nup); /* remove upvalues */ } #define luaL_newlibtable(L,l) \ @@ -62,18 +62,20 @@ ldeleteproto(lua_State *L) { static int lquerytype(lua_State *L) { + const char * type_name; struct sproto *sp = lua_touserdata(L,1); + struct sproto_type *st; if (sp == NULL) { return luaL_argerror(L, 1, "Need a sproto object"); } - const char * typename = luaL_checkstring(L,2); - struct sproto_type *st = sproto_type(sp, typename); + type_name = luaL_checkstring(L,2); + st = sproto_type(sp, type_name); if (st) { lua_pushlightuserdata(L, st); return 1; } - return luaL_error(L, "type %s not found", typename); + return luaL_error(L, "type %s not found", type_name); } struct encode_ud { @@ -119,9 +121,10 @@ encode(void *ud, const char *tagname, int type, int index, struct sproto_type *s switch (type) { case SPROTO_TINTEGER: { lua_Integer v = luaL_checkinteger(L, -1); + lua_Integer vh; lua_pop(L,1); // notice: in lua 5.2, lua_Integer maybe 52bit - lua_Integer vh = v >> 31; + vh = v >> 31; if (vh == 0 || vh == -1) { *(uint32_t *)value = (uint32_t)v; return 4; @@ -148,13 +151,14 @@ encode(void *ud, const char *tagname, int type, int index, struct sproto_type *s } case SPROTO_TSTRUCT: { struct encode_ud sub; + int r; sub.L = L; sub.st = st; sub.tbl_index = lua_gettop(L); sub.array_tag = NULL; sub.array_index = 0; sub.deep = self->deep + 1; - int r = sproto_encode(st, value, length, encode, &sub); + r = sproto_encode(st, value, length, encode, &sub); lua_pop(L,1); return r; } @@ -165,6 +169,7 @@ encode(void *ud, const char *tagname, int type, int index, struct sproto_type *s static void * expand_buffer(lua_State *L, int osz, int nsz) { + void *output; do { osz *= 2; } while (osz < nsz); @@ -172,7 +177,7 @@ expand_buffer(lua_State *L, int osz, int nsz) { luaL_error(L, "object is too large (>%d)", ENCODE_MAXSIZE); return NULL; } - void *output = lua_newuserdata(L, osz); + output = lua_newuserdata(L, osz); lua_replace(L, lua_upvalueindex(1)); lua_pushinteger(L, osz); lua_replace(L, lua_upvalueindex(2)); @@ -188,6 +193,7 @@ expand_buffer(lua_State *L, int osz, int nsz) { */ static int lencode(lua_State *L) { + struct encode_ud self; void * buffer = lua_touserdata(L, lua_upvalueindex(1)); int sz = lua_tointeger(L, lua_upvalueindex(2)); @@ -197,7 +203,6 @@ lencode(lua_State *L) { } luaL_checktype(L, 2, LUA_TTABLE); luaL_checkstack(L, ENCODE_DEEPLEVEL + 8, NULL); - struct encode_ud self; self.L = L; self.st = st; self.tbl_index = 2; @@ -261,15 +266,16 @@ decode(void *ud, const char *tagname, int type, int index, struct sproto_type *s break; } case SPROTO_TSTRUCT: { - lua_newtable(L); struct decode_ud sub; + int r; + lua_newtable(L); sub.L = L; sub.result_index = lua_gettop(L); sub.deep = self->deep + 1; sub.array_index = 0; sub.array_tag = NULL; - int r = sproto_decode(st, value, length, decode, &sub); + r = sproto_decode(st, value, length, decode, &sub); if (r < 0 || r != length) return r; lua_settop(L, sub.result_index); @@ -312,22 +318,25 @@ getbuffer(lua_State *L, int index, size_t *sz) { static int ldecode(lua_State *L) { struct sproto_type * st = lua_touserdata(L, 1); + const void * buffer; + struct decode_ud self; + size_t sz; + int r; if (st == NULL) { return luaL_argerror(L, 1, "Need a sproto_type object"); } - size_t sz=0; - const void * buffer = getbuffer(L, 2, &sz); + sz = 0; + buffer = getbuffer(L, 2, &sz); if (!lua_istable(L, -1)) { lua_newtable(L); } luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); - struct decode_ud self; self.L = L; self.result_index = lua_gettop(L); self.array_index = 0; self.array_tag = NULL; self.deep = 0; - int r = sproto_decode(st, buffer, (int)sz, decode, &self); + r = sproto_decode(st, buffer, (int)sz, decode, &self); if (r < 0) { return luaL_error(L, "decode error"); } @@ -359,11 +368,12 @@ lpack(lua_State *L) { // the worst-case space overhead of packing is 2 bytes per 2 KiB of input (256 words = 2KiB). size_t maxsz = (sz + 2047) / 2048 * 2 + sz; void * output = lua_touserdata(L, lua_upvalueindex(1)); + int bytes; int osz = lua_tointeger(L, lua_upvalueindex(2)); if (osz < maxsz) { output = expand_buffer(L, osz, maxsz); } - int bytes = sproto_pack(buffer, sz, output, maxsz); + bytes = sproto_pack(buffer, sz, output, maxsz); if (bytes > maxsz) { return luaL_error(L, "packing error, return size = %d", bytes); } @@ -402,14 +412,18 @@ pushfunction_withbuffer(lua_State *L, const char * name, lua_CFunction func) { static int lprotocol(lua_State *L) { struct sproto * sp = lua_touserdata(L, 1); + struct sproto_type * request; + struct sproto_type * response; + int t; + int tag; if (sp == NULL) { return luaL_argerror(L, 1, "Need a sproto_type object"); } - int t = lua_type(L,2); - int tag; + t = lua_type(L,2); if (t == LUA_TNUMBER) { + const char * name; tag = lua_tointeger(L, 2); - const char * name = sproto_protoname(sp, tag); + name = sproto_protoname(sp, tag); if (name == NULL) return 0; lua_pushstring(L, name); @@ -420,13 +434,13 @@ lprotocol(lua_State *L) { return 0; lua_pushinteger(L, tag); } - struct sproto_type * request = sproto_protoquery(sp, tag, SPROTO_REQUEST); + request = sproto_protoquery(sp, tag, SPROTO_REQUEST); if (request == NULL) { lua_pushnil(L); } else { lua_pushlightuserdata(L, request); } - struct sproto_type * response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); + response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); if (response == NULL) { lua_pushnil(L); } else { diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index ca43cd9b..24d36492 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -119,8 +119,8 @@ todword(const uint8_t *p) { static int count_array(const uint8_t * stream) { uint32_t length = todword(stream); - stream += SIZEOF_LENGTH; int n = 0; + stream += SIZEOF_LENGTH; while (length > 0) { uint32_t nsz; if (length < SIZEOF_LENGTH) @@ -180,6 +180,10 @@ static const uint8_t * import_field(struct sproto *s, struct field *f, const uint8_t * stream) { uint32_t sz; const uint8_t * result; + int fn; + int i; + int array = 0; + int tag = -1; f->tag = -1; f->type = -1; f->name = NULL; @@ -188,13 +192,10 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { sz = todword(stream); stream += SIZEOF_LENGTH; result = stream + sz; - int fn = struct_field(stream, sz); + fn = struct_field(stream, sz); if (fn < 0) return NULL; stream += SIZEOF_HEADER; - int i; - int array = 0; - int tag = -1; for (i=0;iname = import_string(s, stream + fn * SIZEOF_FIELD); @@ -213,12 +214,12 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { return NULL; value = value/2 - 1; switch(tag) { - case 1: // buildin + case 1: // buildin if (value >= SPROTO_TSTRUCT) return NULL; // invalid buildin type f->type = value; break; - case 2: // type index + case 2: // type index if (value >= s->type_n) return NULL; // invalid type index if (f->type >= 0) @@ -226,10 +227,10 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { f->type = SPROTO_TSTRUCT; f->st = &s->type[value]; break; - case 3: // tag + case 3: // tag f->tag = value; break; - case 4: // array + case 4: // array if (value) array = SPROTO_TARRAY; break; @@ -248,10 +249,10 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { .type { .field { name 0 : string - buildin 1 : integer + buildin 1 : integer type 2 : integer - tag 3 : integer - array 4 : boolean + tag 3 : integer + array 4 : boolean } name 0 : string fields 1 : *field @@ -259,11 +260,16 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { */ static const uint8_t * import_type(struct sproto *s, struct sproto_type *t, const uint8_t * stream) { + const uint8_t * result; uint32_t sz = todword(stream); int i; + int fn; + int n; + int maxn; + int last; stream += SIZEOF_LENGTH; - const uint8_t * result = stream + sz; - int fn = struct_field(stream, sz); + result = stream + sz; + fn = struct_field(stream, sz); if (fn <= 0 || fn > 2) return NULL; for (i=0;in = n; t->f = pool_alloc(&s->memory, sizeof(struct field) * n); for (i=0;if[i]; stream = import_field(s, f, stream); if (stream == NULL) return NULL; - int tag = f->tag; + tag = f->tag; if (tag < last) return NULL; // tag must in ascending order if (tag > last+1) { @@ -312,24 +319,27 @@ import_type(struct sproto *s, struct sproto_type *t, const uint8_t * stream) { /* .protocol { name 0 : string - tag 1 : integer - request 2 : integer + tag 1 : integer + request 2 : integer response 3 : integer } */ static const uint8_t * import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { + const uint8_t * result; uint32_t sz = todword(stream); + int fn; + int i; + int tag; stream += SIZEOF_LENGTH; - const uint8_t * result = stream + sz; - int fn = struct_field(stream, sz); + result = stream + sz; + fn = struct_field(stream, sz); stream += SIZEOF_HEADER; p->name = NULL; p->tag = -1; p->p[SPROTO_REQUEST] = NULL; p->p[SPROTO_RESPONSE] = NULL; - int i; - int tag = 0; + tag = 0; for (i=0;iname = import_string(s, stream + SIZEOF_FIELD *fn); break; - case 1: // tag + case 1: // tag if (value < 0) { return NULL; } p->tag = value; break; - case 2: // request + case 2: // request if (value < 0 || value>=s->type_n) return NULL; p->p[SPROTO_REQUEST] = &s->type[value]; break; - case 3: // response + case 3: // response if (value < 0 || value>=s->type_n) return NULL; p->p[SPROTO_RESPONSE] = &s->type[value]; @@ -374,22 +384,23 @@ import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { static struct sproto * create_from_bundle(struct sproto *s, const uint8_t * stream, size_t sz) { + const uint8_t * content; + const uint8_t * typedata = NULL; + const uint8_t * protocoldata = NULL; int fn = struct_field(stream, sz); + int i; if (fn < 0) return NULL; stream += SIZEOF_HEADER; + content = stream + fn*SIZEOF_FIELD; - const uint8_t * content = stream + fn*SIZEOF_FIELD; - const uint8_t * typedata = NULL; - const uint8_t * protocoldata = NULL; - - int i; for (i=0;itype_n); - int i,j; for (i=0;itype_n;i++) { struct sproto_type *t = &s->type[i]; printf("%s\n", t->name); for (j=0;jn;j++) { char array[2] = { 0, 0 }; - const char * typename = NULL; + const char * type_name = NULL; struct field *f = &t->f[j]; if (f->type & SPROTO_TARRAY) { array[0] = '*'; } else { array[0] = 0; } - int t = f->type & ~SPROTO_TARRAY; - if (t == SPROTO_TSTRUCT) { - typename = f->st->name; - } else { - assert(ttype & ~SPROTO_TARRAY; + if (t == SPROTO_TSTRUCT) { + type_name = f->st->name; + } else { + assert(tname, f->tag, array, typename); + printf("\t%s (%d) %s%s\n", f->name, f->tag, array, type_name); } } printf("=== %d protocol ===\n", s->protocol_n); @@ -518,10 +531,11 @@ query_proto(struct sproto *sp, int tag) { struct sproto_type * sproto_protoquery(struct sproto *sp, int proto, int what) { + struct protocol * p; if (what <0 || what >1) { return NULL; } - struct protocol * p = query_proto(sp, proto); + p = query_proto(sp, proto); if (p) { return p->p[what]; } @@ -555,13 +569,15 @@ sproto_name(struct sproto_type * st) { static struct field * findtag(struct sproto_type *st, int tag) { + int begin, end; if (st->base >=0 ) { tag -= st->base; if (tag < 0 || tag >= st->n) return NULL; return &st->f[tag]; } - int begin = 0, end = st->n; + begin = 0; + end = st->n; while (begin < end) { int mid = (begin+end)/2; struct field *f = &st->f[mid]; @@ -580,7 +596,7 @@ findtag(struct sproto_type *st, int tag) { // encode & decode // sproto_callback(void *ud, int tag, int type, struct sproto_type *, void *value, int length) -// return size, -1 means error +// return size, -1 means error static inline int fill_size(uint8_t * data, int sz) { @@ -623,18 +639,20 @@ encode_uint64(uint64_t v, uint8_t * data, int size) { static int encode_string(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { + int sz; if (size < SIZEOF_LENGTH) return -1; - int sz = cb(ud, f->name, SPROTO_TSTRING, 0, NULL, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); + sz = cb(ud, f->name, SPROTO_TSTRING, 0, NULL, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); return fill_size(data, sz); } static int encode_struct(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { + int sz; if (size < SIZEOF_LENGTH) { return -1; } - int sz = cb(ud, f->name, SPROTO_TSTRUCT, 0, f->st, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); + sz = cb(ud, f->name, SPROTO_TSTRUCT, 0, f->st, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); return fill_size(data, sz); } @@ -656,18 +674,21 @@ uint32_to_uint64(int negative, uint8_t *buffer) { static uint8_t * encode_integer_array(sproto_callback cb, void *ud, struct field *f, uint8_t *buffer, int size) { uint8_t * header = buffer; + int intlen; + int index; if (size < 1) return NULL; buffer++; size--; - int intlen = sizeof(uint32_t); - int index = 1; + intlen = sizeof(uint32_t); + index = 1; for (;;) { + int sz; union { uint64_t u64; uint32_t u32; } u; - int sz = cb(ud, f->name, SPROTO_TINTEGER, index, f->st, &u, sizeof(u)); + sz = cb(ud, f->name, SPROTO_TINTEGER, index, f->st, &u, sizeof(u)); if (sz < 0) return NULL; if (sz == 0) @@ -685,24 +706,26 @@ encode_integer_array(sproto_callback cb, void *ud, struct field *f, uint8_t *buf uint32_to_uint64(v & 0x80000000, buffer); } } else { + uint64_t v; if (sz != sizeof(uint64_t)) return NULL; if (intlen == sizeof(uint32_t)) { + int i; // rearrange size -= (index-1) * sizeof(uint32_t); if (size < sizeof(uint64_t)) return NULL; buffer += (index-1) * sizeof(uint32_t); - int i; for (i=index-2;i>=0;i--) { + int negative; memcpy(header+1+i*sizeof(uint64_t), header+1+i*sizeof(uint32_t), sizeof(uint32_t)); - int negative = header[1+i*sizeof(uint64_t)+3] & 0x80; + negative = header[1+i*sizeof(uint64_t)+3] & 0x80; uint32_to_uint64(negative, header+1+i*sizeof(uint64_t)); } intlen = sizeof(uint64_t); } - uint64_t v = u.u64; + v = u.u64; buffer[0] = v & 0xff; buffer[1] = (v >> 8) & 0xff; buffer[2] = (v >> 16) & 0xff; @@ -727,12 +750,16 @@ encode_integer_array(sproto_callback cb, void *ud, struct field *f, uint8_t *buf static int encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { + uint8_t * buffer; + int index; + int type; + int sz; if (size < SIZEOF_LENGTH) return -1; size -= SIZEOF_LENGTH; - int index = 1; - uint8_t * buffer = data + SIZEOF_LENGTH; - int type = f->type & ~SPROTO_TARRAY; + index = 1; + buffer = data + SIZEOF_LENGTH; + type = f->type & ~SPROTO_TARRAY; switch (type) { case SPROTO_TINTEGER: buffer = encode_integer_array(cb,ud,f,buffer,size); @@ -757,10 +784,11 @@ encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int s break; default: for (;;) { + int sz; if (size < SIZEOF_LENGTH) return -1; size -= SIZEOF_LENGTH; - int sz = cb(ud, f->name, type, index, f->st, buffer+SIZEOF_LENGTH, size); + sz = cb(ud, f->name, type, index, f->st, buffer+SIZEOF_LENGTH, size); if (sz < 0) return -1; if (sz == 0) @@ -772,7 +800,7 @@ encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int s } break; } - int sz = buffer - (data + SIZEOF_LENGTH); + sz = buffer - (data + SIZEOF_LENGTH); if (sz == 0) return 0; return fill_size(data, sz); @@ -783,13 +811,16 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c uint8_t * header = buffer; uint8_t * data; int header_sz = SIZEOF_HEADER + st->maxn * SIZEOF_FIELD; + int i; + int index; + int lasttag; + int datasz; if (size < header_sz) return -1; data = header + header_sz; size -= header_sz; - int i; - int index = 0; - int lasttag = -1; + index = 0; + lasttag = -1; for (i=0;in;i++) { struct field *f = &st->f[i]; int type = f->type; @@ -813,7 +844,7 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c if (sz == sizeof(uint32_t)) { if (u.u32 < 0x7fff) { value = (u.u32+1) * 2; - sz = 2; // sz can be any number > 0 + sz = 2; // sz can be any number > 0 } else { sz = encode_integer(u.u32, data, size); } @@ -837,12 +868,14 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c if (sz < 0) return -1; if (sz > 0) { + uint8_t * record; + int tag; if (value == 0) { data += sz; size -= sz; } - uint8_t * record = header+SIZEOF_HEADER+SIZEOF_FIELD*index; - int tag = f->tag - lasttag - 1; + record = header+SIZEOF_HEADER+SIZEOF_FIELD*index; + tag = f->tag - lasttag - 1; if (tag > 0) { // skip tag tag = (tag - 1) * 2 + 1; @@ -862,7 +895,7 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c header[0] = index & 0xff; header[1] = (index >> 8) & 0xff; - int datasz = data - (header + header_sz); + datasz = data - (header + header_sz); data = header + header_sz; if (index != st->maxn) { memmove(header + SIZEOF_HEADER + index * SIZEOF_FIELD, data, datasz); @@ -909,9 +942,10 @@ decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { stream += SIZEOF_LENGTH; switch (type) { case SPROTO_TINTEGER: { + int len; if (sz < 1) return -1; - int len = *stream; + len = *stream; ++stream; --sz; if (len == sizeof(uint32_t)) { @@ -956,6 +990,8 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba uint8_t * stream; uint8_t * datastream; int fn; + int i; + int tag; if (size < SIZEOF_HEADER) return -1; stream = (void *)data; @@ -967,17 +1003,18 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba datastream = stream + fn * SIZEOF_FIELD; size -= fn * SIZEOF_FIELD ; - int i; - int tag = -1; + tag = -1; for (i=0;i Date: Mon, 3 Nov 2014 15:18:00 +0800 Subject: [PATCH 229/729] release v0.8.1 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 64c30744..f15ef86c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v0.8.1 (2014-11-3) +----------- +* Send to an invalid remote service will raise an error +* Bugifx: socket open address string +* Remove sha1 from mysqlaux +* merge lua and sproto bugfix , use crypt lib instead +* Fix a memory leak in socket +* minor bugfix in http module + v0.8.0 (2014-10-27) ----------- * Add mysql client driver From 5fad5d63ae939f1ac3639c0eb011c358162a884f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 4 Nov 2014 18:12:25 +0800 Subject: [PATCH 230/729] socket close fd when auth failed --- lualib/snax/loginserver.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index c099ddb8..3eafa674 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -87,16 +87,17 @@ local function launch_slave(auth_handler) return ok, server, uid, secret end - local function ret_pack(ok, err, ...) + local function ret_pack(fd, ok, err, ...) if ok then skynet.ret(skynet.pack(err, ...)) elseif err ~= socket_error then + socket.close(fd) error(err) end end - skynet.dispatch("lua", function(_,_,...) - ret_pack(pcall(auth, ...)) + skynet.dispatch("lua", function(_,_,fd,...) + ret_pack(fd,pcall(auth,fd,...)) end) end From 765749f6080c144ec3bf14c202b7b67c7219c986 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 4 Nov 2014 18:25:15 +0800 Subject: [PATCH 231/729] accept may not start fd, so it can't close at last --- lualib/snax/loginserver.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 3eafa674..2605a028 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -87,17 +87,16 @@ local function launch_slave(auth_handler) return ok, server, uid, secret end - local function ret_pack(fd, ok, err, ...) + local function ret_pack(ok, err, ...) if ok then skynet.ret(skynet.pack(err, ...)) - elseif err ~= socket_error then - socket.close(fd) + else error(err) end end - skynet.dispatch("lua", function(_,_,fd,...) - ret_pack(fd,pcall(auth,fd,...)) + skynet.dispatch("lua", function(_,_,...) + ret_pack(pcall(auth, ...)) end) end @@ -164,6 +163,7 @@ local function launch_master(conf) if err ~= socket_error then skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err)) end + socket.start(fd) end socket.close(fd) end) From 929ce385a3b9e2295c4a4c4d2900c08af3df891c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Nov 2014 10:53:29 +0800 Subject: [PATCH 232/729] fprintf need cr --- skynet-src/socket_server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 11ba4b0b..98248134 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -528,7 +528,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock n = 0; break; default: - fprintf(stderr, "socket-server: write to %d (fd=%d) error.",id,s->fd); + fprintf(stderr, "socket-server: write to %d (fd=%d) error :%s.\n",id,s->fd,strerror(errno)); force_close(ss,s,result); return SOCKET_CLOSE; } @@ -660,7 +660,7 @@ block_readpipe(int pipefd, void *buffer, int sz) { if (n<0) { if (errno == EINTR) continue; - fprintf(stderr, "socket-server : read pipe error %s.",strerror(errno)); + fprintf(stderr, "socket-server : read pipe error %s.\n",strerror(errno)); return; } // must atomic read from a pipe From c87dea3d9b6ba089ede598d1dfe87c60afd2f51c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Nov 2014 20:58:10 +0800 Subject: [PATCH 233/729] fix issue #200 --- lualib-src/lua-seri.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index fe4bfa46..c41456be 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -179,7 +179,7 @@ wb_string(struct write_block *wb, const char *str, int len) { wb_push(wb, str, len); } } else { - int n; + uint8_t n; if (len < 0x10000) { n = COMBINE_TYPE(TYPE_LONG_STRING, 2); wb_push(wb, &n, 1); From e06a9e3701a25da87e74537ae01624c0d65fd936 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Nov 2014 11:33:57 +0800 Subject: [PATCH 234/729] dispatch read before write, and try to dispath both --- skynet-src/socket_server.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 98248134..6ed9efad 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -904,15 +904,20 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int fprintf(stderr, "socket-server: invalid socket\n"); break; default: - if (e->write) { - int type = send_buffer(ss, s, result); + if (e->read) { + int type = forward_message(ss, s, result); + if (e->write) { + // Try to dispatch write message next step if write flag set. + e->read = false; + --ss->event_index; + } if (type == -1) break; clear_closed_event(ss, result, type); return type; } - if (e->read) { - int type = forward_message(ss, s, result); + if (e->write) { + int type = send_buffer(ss, s, result); if (type == -1) break; clear_closed_event(ss, result, type); From fa78623b1cec717e3e7a6aed047fbd620148c787 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Nov 2014 13:54:43 +0800 Subject: [PATCH 235/729] support user defined send object --- skynet-src/socket_server.c | 86 ++++++++++++++++++++++++++++---------- skynet-src/socket_server.h | 9 ++++ 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 6ed9efad..da167658 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -40,8 +40,9 @@ struct write_buffer { struct write_buffer * next; char *ptr; - int sz; void *buffer; + int sz; + bool userobject; }; struct wb_list { @@ -68,6 +69,7 @@ struct socket_server { int alloc_id; int event_n; int event_index; + struct socket_object_interface soi; struct event ev[MAX_EVENT]; struct socket slot[MAX_SOCKET]; char buffer[MAX_INFO]; @@ -137,9 +139,40 @@ union sockaddr_all { struct sockaddr_in6 v6; }; +struct send_object { + void * buffer; + int sz; + void (*free_func)(void *); +}; + #define MALLOC skynet_malloc #define FREE skynet_free +static inline bool +send_object_init(struct socket_server *ss, struct send_object *so, void *object, int sz) { + if (sz < 0) { + so->buffer = ss->soi.buffer(object); + so->sz = ss->soi.size(object); + so->free_func = ss->soi.free; + return true; + } else { + so->buffer = object; + so->sz = sz; + so->free_func = FREE; + return false; + } +} + +static inline void +write_buffer_free(struct socket_server *ss, struct write_buffer *wb) { + if (wb->userobject) { + ss->soi.free(wb->buffer); + } else { + FREE(wb->buffer); + } + FREE(wb); +} + static void socket_keepalive(int fd) { int keepalive = 1; @@ -213,6 +246,7 @@ socket_server_create() { ss->alloc_id = 0; ss->event_n = 0; ss->event_index = 0; + memset(&ss->soi, 0, sizeof(ss->soi)); FD_ZERO(&ss->rfds); assert(ss->recvctrl_fd < FD_SETSIZE); @@ -220,13 +254,12 @@ socket_server_create() { } static void -free_wb_list(struct wb_list *list) { +free_wb_list(struct socket_server *ss, struct wb_list *list) { struct write_buffer *wb = list->head; while (wb) { struct write_buffer *tmp = wb; wb = wb->next; - FREE(tmp->buffer); - FREE(tmp); + write_buffer_free(ss, tmp); } list->head = NULL; list->tail = NULL; @@ -242,8 +275,8 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_message *r return; } assert(s->type != SOCKET_TYPE_RESERVE); - free_wb_list(&s->high); - free_wb_list(&s->low); + free_wb_list(ss,&s->high); + free_wb_list(ss,&s->low); if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PLISTEN) { sp_del(ss->event_fd, s->fd); } @@ -395,8 +428,7 @@ send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, stru break; } list->head = tmp->next; - FREE(tmp->buffer); - FREE(tmp); + write_buffer_free(ss,tmp); } list->tail = NULL; @@ -469,10 +501,12 @@ send_buffer(struct socket_server *ss, struct socket *s, struct socket_message *r } static int -append_sendbuffer_(struct wb_list *s, struct request_send * request, int n) { +append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_send * request, int n) { struct write_buffer * buf = MALLOC(sizeof(*buf)); - buf->ptr = request->buffer+n; - buf->sz = request->sz - n; + struct send_object so; + buf->userobject = send_object_init(ss, &so, request->buffer, request->sz); + buf->ptr = so.buffer+n; + buf->sz = so.sz - n; buf->buffer = request->buffer; buf->next = NULL; if (s->head == NULL) { @@ -487,13 +521,13 @@ append_sendbuffer_(struct wb_list *s, struct request_send * request, int n) { } static inline void -append_sendbuffer(struct socket *s, struct request_send * request, int n) { - s->wb_size += append_sendbuffer_(&s->high, request, n); +append_sendbuffer(struct socket_server *ss, struct socket *s, struct request_send * request, int n) { + s->wb_size += append_sendbuffer_(ss, &s->high, request, n); } static inline void -append_sendbuffer_low(struct socket *s, struct request_send * request) { - s->wb_size += append_sendbuffer_(&s->low, request, 0); +append_sendbuffer_low(struct socket_server *ss,struct socket *s, struct request_send * request) { + s->wb_size += append_sendbuffer_(ss, &s->low, request, 0); } static inline int @@ -512,15 +546,17 @@ static int send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority) { int id = request->id; struct socket * s = &ss->slot[HASH_ID(id)]; + struct send_object so; + send_object_init(ss, &so, request->buffer, request->sz); if (s->type == SOCKET_TYPE_INVALID || s->id != id || s->type == SOCKET_TYPE_HALFCLOSE || s->type == SOCKET_TYPE_PACCEPT) { - FREE(request->buffer); + so.free_func(request->buffer); return -1; } assert(s->type != SOCKET_TYPE_PLISTEN && s->type != SOCKET_TYPE_LISTEN); if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) { - int n = write(s->fd, request->buffer, request->sz); + int n = write(s->fd, so.buffer, so.sz); if (n<0) { switch(errno) { case EINTR: @@ -533,17 +569,17 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock return SOCKET_CLOSE; } } - if (n == request->sz) { - FREE(request->buffer); + if (n == so.sz) { + so.free_func(request->buffer); return -1; } - append_sendbuffer(s, request, n); // add to high priority list, even priority == PRIORITY_LOW + append_sendbuffer(ss, s, request, n); // add to high priority list, even priority == PRIORITY_LOW sp_write(ss->event_fd, s->fd, s, true); } else { if (priority == PRIORITY_LOW) { - append_sendbuffer_low(s, request); + append_sendbuffer_low(ss, s, request); } else { - append_sendbuffer(s, request, 0); + append_sendbuffer(ss, s, request, 0); } } return -1; @@ -1092,3 +1128,9 @@ socket_server_nodelay(struct socket_server *ss, int id) { request.u.setopt.value = 1; send_request(ss, &request, 'T', sizeof(request.u.setopt)); } + +void +socket_server_userobject(struct socket_server *ss, struct socket_object_interface *soi) { + ss->soi = *soi; +} + diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 66648719..a4dcf71e 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -38,4 +38,13 @@ int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd); void socket_server_nodelay(struct socket_server *, int id); +struct socket_object_interface { + void * (*buffer)(void *); + int (*size)(void *); + void (*free)(void *); +}; + +// if you send package sz == -1, use soi. +void socket_server_userobject(struct socket_server *, struct socket_object_interface *soi); + #endif From cfe9506a5a06ff9a51b101af69a0cd63c5ec78d7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Nov 2014 23:00:44 +0800 Subject: [PATCH 236/729] add UDP support, and TCP listen support ipv6 now --- lualib-src/lua-socket.c | 168 +++++++++-- lualib/socket.lua | 53 +++- skynet-src/skynet_socket.c | 44 ++- skynet-src/skynet_socket.h | 6 + skynet-src/socket_server.c | 562 ++++++++++++++++++++++++++++++++----- skynet-src/socket_server.h | 15 + test/testudp.lua | 23 ++ 7 files changed, 773 insertions(+), 98 deletions(-) create mode 100644 test/testudp.lua diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 81322d17..c99d85d3 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -9,6 +9,8 @@ #include #include +#include + #include "skynet_socket.h" #define BACKLOG 32 @@ -372,28 +374,64 @@ lunpack(lua_State *L) { } else { lua_pushlightuserdata(L, message->buffer); } + if (message->type == SKYNET_SOCKET_TYPE_UDP) { + int addrsz = 0; + const char * addrstring = skynet_socket_udp_address(message, &addrsz); + if (addrstring) { + lua_pushlstring(L, addrstring, addrsz); + return 5; + } + } return 4; } +static const char * +address_port(lua_State *L, char *tmp, const char * addr, int port_index, int *port) { + const char * host; + if (lua_isnoneornil(L,port_index)) { + host = strchr(addr, '['); + if (host) { + // is ipv6 + ++host; + const char * sep = strchr(addr,']'); + if (sep == NULL) { + luaL_error(L, "Invalid address %s.",addr); + } + memcpy(tmp, host, sep-host); + tmp[sep-host] = '\0'; + host = tmp; + sep = strchr(sep + 1, ':'); + if (sep == NULL) { + luaL_error(L, "Invalid address %s.",addr); + } + *port = strtoul(sep+1,NULL,10); + } else { + // is ipv4 + const char * sep = strchr(addr,':'); + if (sep == NULL) { + luaL_error(L, "Invalid address %s.",addr); + } + memcpy(tmp, addr, sep-addr); + tmp[sep-addr] = '\0'; + host = tmp; + *port = strtoul(sep+1,NULL,10); + } + } else { + host = addr; + *port = luaL_optinteger(L,port_index, 0); + } + return host; +} + static int lconnect(lua_State *L) { size_t sz = 0; const char * addr = luaL_checklstring(L,1,&sz); char tmp[sz]; - int port; - const char * host; - if (lua_isnoneornil(L,2)) { - const char * sep = strchr(addr,':'); - if (sep == NULL) { - return luaL_error(L, "Connect to invalid address %s.",addr); - } - memcpy(tmp, addr, sep-addr); - tmp[sep-addr] = '\0'; - host = tmp; - port = strtoul(sep+1,NULL,10); - } else { - host = addr; - port = luaL_checkinteger(L,2); + int port = 0; + const char * host = address_port(L, tmp, addr, 2, &port); + if (port == 0) { + return luaL_error(L, "Invalid port"); } struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = skynet_socket_connect(ctx, host, port); @@ -426,14 +464,14 @@ llisten(lua_State *L) { } static void * -get_buffer(lua_State *L, int *sz) { +get_buffer(lua_State *L, int index, int *sz) { void *buffer; - if (lua_isuserdata(L,2)) { - buffer = lua_touserdata(L,2); - *sz = luaL_checkinteger(L,3); + if (lua_isuserdata(L,index)) { + buffer = lua_touserdata(L,index); + *sz = luaL_checkinteger(L,index+1); } else { size_t len = 0; - const char * str = luaL_checklstring(L, 2, &len); + const char * str = luaL_checklstring(L, index, &len); buffer = skynet_malloc(len); memcpy(buffer, str, len); *sz = (int)len; @@ -446,7 +484,7 @@ lsend(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); int sz = 0; - void *buffer = get_buffer(L, &sz); + void *buffer = get_buffer(L, 2, &sz); int err = skynet_socket_send(ctx, id, buffer, sz); lua_pushboolean(L, !err); return 1; @@ -457,7 +495,7 @@ lsendlow(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); int sz = 0; - void *buffer = get_buffer(L, &sz); + void *buffer = get_buffer(L, 2, &sz); skynet_socket_send_lowpriority(ctx, id, buffer, sz); return 0; } @@ -486,6 +524,90 @@ lnodelay(lua_State *L) { skynet_socket_nodelay(ctx,id); return 0; } +/* +int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port); +int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port); +int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz); +const char * skynet_socket_udp_address(struct skynet_context *ctx, struct skynet_socket_message *, int *addrsz); +*/ + +static int +ludp(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + size_t sz = 0; + const char * addr = lua_tolstring(L,1,&sz); + char tmp[sz]; + int port = 0; + const char * host = NULL; + if (addr) { + host = address_port(L, tmp, addr, 2, &port); + } + + int id = skynet_socket_udp(ctx, host, port); + if (id < 0) { + return luaL_error(L, "udp init failed"); + } + lua_pushinteger(L, id); + return 1; +} + +static int +ludp_connect(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + int id = luaL_checkinteger(L, 1); + size_t sz = 0; + const char * addr = luaL_checklstring(L,2,&sz); + char tmp[sz]; + int port = 0; + const char * host = NULL; + if (addr) { + host = address_port(L, tmp, addr, 3, &port); + } + + if (skynet_socket_udp_connect(ctx, id, host, port)) { + return luaL_error(L, "udp connect failed"); + } + + return 0; +} + +static int +ludp_send(lua_State *L) { + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + int id = luaL_checkinteger(L, 1); + const char * address = luaL_checkstring(L, 2); + int sz = 0; + void *buffer = get_buffer(L, 3, &sz); + int err = skynet_socket_udp_send(ctx, id, address, buffer, sz); + + lua_pushboolean(L, !err); + + return 1; +} + +static int +ludp_address(lua_State *L) { + size_t sz = 0; + const uint8_t * addr = (const uint8_t *)luaL_checklstring(L, 1, &sz); + int port = addr[1] * 256 + addr[2]; + const void * src = addr+3; + char tmp[256]; + int family; + if (sz == 1+2+4) { + family = AF_INET; + } else { + if (sz != 1+2+16) { + return luaL_error(L, "Invalid udp address"); + } + family = AF_INET6; + } + if (inet_ntop(family, src, tmp, sizeof(tmp)) == NULL) { + return luaL_error(L, "Invalid udp address"); + } + lua_pushstring(L, tmp); + lua_pushinteger(L, port); + return 2; +} int luaopen_socketdriver(lua_State *L) { @@ -514,6 +636,10 @@ luaopen_socketdriver(lua_State *L) { { "bind", lbind }, { "start", lstart }, { "nodelay", lnodelay }, + { "udp", ludp }, + { "udp_connect", ludp_connect }, + { "udp_send", ludp_send }, + { "udp_address", ludp_address }, { NULL, NULL }, }; lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); diff --git a/lualib/socket.lua b/lualib/socket.lua index efc2ff0b..43d59299 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -119,12 +119,23 @@ socket_message[5] = function(id) wakeup(s) end +-- SKYNET_SOCKET_TYPE_UDP = 6 +socket_message[6] = function(id, size, data, address) + local s = socket_pool[id] + if s == nil or s.callback == nil then + skynet.error("socket: drop udp package from " .. id) + driver.drop(data, size) + return + end + s.callback(data, size, address) +end + skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 unpack = driver.unpack, - dispatch = function (_, _, t, n1, n2, data) - socket_message[t](n1,n2,data) + dispatch = function (_, _, t, ...) + socket_message[t](...) end } @@ -140,6 +151,7 @@ local function connect(id, func) read_require = false, co = false, callback = func, + protocol = "TCP", } socket_pool[id] = s suspend(s) @@ -354,4 +366,41 @@ function socket.limit(id, limit) s.buffer_limit = limit end +---------------------- UDP + +local udp_socket = {} + +local function create_udp_object(id, cb) + socket_pool[id] = { + id = id, + read_require = false, + co = false, + connected = true, + protocol = "UDP", + callback = cb, + } +end + +function socket.udp(callback, host, port) + local id = driver.udp(host, port) + create_udp_object(id, callback) + return id +end + +function socket.udp_connect(id, addr, port, callback) + local obj = socket_pool[id] + if obj then + assert(obj.protocol == "UDP") + if callback then + obj.callback = callback + end + else + create_udp_object(id, callback) + end + driver.udp_connect(id, addr, port) +end + +socket.sendto = assert(driver.udp_send) +socket.udp_address = assert(driver.udp_address) + return socket diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 38ba4ea9..f2dff1cc 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -91,6 +91,9 @@ skynet_socket_poll() { case SOCKET_ACCEPT: forward_message(SKYNET_SOCKET_TYPE_ACCEPT, true, &result); break; + case SOCKET_UDP: + forward_message(SKYNET_SOCKET_TYPE_UDP, false, &result); + break; default: skynet_error(NULL, "Unknown socket message type %d.",type); return -1; @@ -101,9 +104,8 @@ skynet_socket_poll() { return 1; } -int -skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { - int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz); +static int +check_wsz(struct skynet_context *ctx, int id, void *buffer, int64_t wsz) { if (wsz < 0) { skynet_free(buffer); return -1; @@ -116,6 +118,12 @@ skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { return 0; } +int +skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { + int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz); + return check_wsz(ctx, id, buffer, wsz); +} + void skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) { socket_server_send_lowpriority(SOCKET_SERVER, id, buffer, sz); @@ -155,3 +163,33 @@ void skynet_socket_nodelay(struct skynet_context *ctx, int id) { socket_server_nodelay(SOCKET_SERVER, id); } + +int +skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) { + uint32_t source = skynet_context_handle(ctx); + return socket_server_udp(SOCKET_SERVER, source, addr, port); +} + +int +skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port) { + return socket_server_udp_connect(SOCKET_SERVER, id, addr, port); +} + +int +skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz) { + int64_t wsz = socket_server_udp_send(SOCKET_SERVER, id, (const struct socket_udp_address *)address, buffer, sz); + return check_wsz(ctx, id, (void *)buffer, wsz); +} + +const char * +skynet_socket_udp_address(struct skynet_socket_message *msg, int *addrsz) { + if (msg->type != SKYNET_SOCKET_TYPE_UDP) { + return NULL; + } + struct socket_message sm; + sm.id = msg->id; + sm.opaque = 0; + sm.ud = msg->ud; + sm.data = msg->buffer; + return (const char *)socket_server_udp_address(SOCKET_SERVER, &sm, addrsz); +} diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 7f0b4643..5327f09f 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -8,6 +8,7 @@ struct skynet_context; #define SKYNET_SOCKET_TYPE_CLOSE 3 #define SKYNET_SOCKET_TYPE_ACCEPT 4 #define SKYNET_SOCKET_TYPE_ERROR 5 +#define SKYNET_SOCKET_TYPE_UDP 6 struct skynet_socket_message { int type; @@ -30,4 +31,9 @@ void skynet_socket_close(struct skynet_context *ctx, int id); void skynet_socket_start(struct skynet_context *ctx, int id); void skynet_socket_nodelay(struct skynet_context *ctx, int id); +int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port); +int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port); +int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz); +const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); + #endif diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index da167658..630f86eb 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -37,28 +37,44 @@ #define HASH_ID(id) (((unsigned)id) % MAX_SOCKET) +#define PROTOCOL_TCP 0 +#define PROTOCOL_UDP 1 +#define PROTOCOL_UDPv6 2 + +#define UDP_ADDRESS_SIZE 19 // ipv6 128bit + port 16bit + 1 byte type + +#define MAX_UDP_PACKAGE 65535 + struct write_buffer { struct write_buffer * next; - char *ptr; void *buffer; + char *ptr; int sz; bool userobject; + uint8_t udp_address[UDP_ADDRESS_SIZE]; }; +#define SIZEOF_TCPBUFFER (offsetof(struct write_buffer, udp_address[0])) +#define SIZEOF_UDPBUFFER (sizeof(struct write_buffer)) + struct wb_list { struct write_buffer * head; struct write_buffer * tail; }; struct socket { - int fd; - int id; - int type; - int size; - int64_t wb_size; uintptr_t opaque; struct wb_list high; struct wb_list low; + int64_t wb_size; + int fd; + int id; + uint16_t protocol; + uint16_t type; + union { + int size; + uint8_t udp_address[UDP_ADDRESS_SIZE]; + } p; }; struct socket_server { @@ -73,6 +89,7 @@ struct socket_server { struct event ev[MAX_EVENT]; struct socket slot[MAX_SOCKET]; char buffer[MAX_INFO]; + uint8_t udpbuffer[MAX_UDP_PACKAGE]; fd_set rfds; }; @@ -89,6 +106,16 @@ struct request_send { char * buffer; }; +struct request_send_udp { + struct request_send send; + uint8_t address[UDP_ADDRESS_SIZE]; +}; + +struct request_setudp { + int id; + uint8_t address[UDP_ADDRESS_SIZE]; +}; + struct request_close { int id; uintptr_t opaque; @@ -118,17 +145,44 @@ struct request_setopt { int value; }; +struct request_udp { + int id; + int fd; + int family; + uintptr_t opaque; +}; + +/* + The first byte is TYPE + + S Start socket + B Bind socket + L Listen socket + K Close socket + O Connect to (Open) + X Exit + D Send package (high) + P Send package (low) + A Send UDP package + T Set opt + U Create UDP socket + C set udp address + */ + struct request_package { uint8_t header[8]; // 6 bytes dummy union { char buffer[256]; struct request_open open; struct request_send send; + struct request_send_udp send_udp; struct request_close close; struct request_listen listen; struct request_bind bind; struct request_start start; struct request_setopt setopt; + struct request_udp udp; + struct request_setudp set_udp; } u; uint8_t dummy[256]; }; @@ -309,7 +363,7 @@ check_wb_list(struct wb_list *s) { } static struct socket * -new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) { +new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool add) { struct socket * s = &ss->slot[HASH_ID(id)]; assert(s->type == SOCKET_TYPE_RESERVE); @@ -322,7 +376,8 @@ new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) { s->id = id; s->fd = fd; - s->size = MIN_READ_BUFFER; + s->protocol = protocol; + s->p.size = MIN_READ_BUFFER; s->opaque = opaque; s->wb_size = 0; check_wb_list(&s->high); @@ -345,7 +400,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock struct addrinfo *ai_ptr = NULL; char port[16]; sprintf(port, "%d", request->port); - memset( &ai_hints, 0, sizeof( ai_hints ) ); + memset(&ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; ai_hints.ai_socktype = SOCK_STREAM; ai_hints.ai_protocol = IPPROTO_TCP; @@ -375,7 +430,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock goto _failed; } - ns = new_fd(ss, id, sock, request->opaque, true); + ns = new_fd(ss, id, sock, PROTOCOL_TCP, request->opaque, true); if (ns == NULL) { close(sock); goto _failed; @@ -404,7 +459,7 @@ _failed: } static int -send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { +send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { while (list->head) { struct write_buffer * tmp = list->head; for (;;) { @@ -435,6 +490,74 @@ send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, stru return -1; } +static socklen_t +udp_socket_address(struct socket *s, const uint8_t udp_address[UDP_ADDRESS_SIZE], union sockaddr_all *sa) { + int type = (uint8_t)udp_address[0]; + if (type != s->protocol) + return 0; + uint16_t port = 0; + memcpy(&port, udp_address+1, sizeof(uint16_t)); + switch (s->protocol) { + case PROTOCOL_UDP: + memset(&sa->v4, 0, sizeof(sa->v4)); + sa->s.sa_family = AF_INET; + sa->v4.sin_port = port; + memcpy(&sa->v4.sin_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v4.sin_addr)); // ipv4 address is 32 bits + return sizeof(sa->v4); + case PROTOCOL_UDPv6: + memset(&sa->v6, 0, sizeof(sa->v6)); + sa->s.sa_family = AF_INET6; + sa->v6.sin6_port = port; + memcpy(&sa->v6.sin6_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v6.sin6_addr)); // ipv4 address is 128 bits + return sizeof(sa->v6); + } + return 0; +} + +static int +send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { + while (list->head) { + struct write_buffer * tmp = list->head; + union sockaddr_all sa; + socklen_t sasz = udp_socket_address(s, tmp->udp_address, &sa); + int err = sendto(s->fd, tmp->ptr, tmp->sz, 0, &sa.s, sasz); + if (err < 0) { + switch(errno) { + case EINTR: + case EAGAIN: + return -1; + } + fprintf(stderr, "socket-server : udp (%d) sendto error %s.\n",s->id, strerror(errno)); + return -1; +/* // ignore udp sendto error + + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = NULL; + + return SOCKET_ERROR; +*/ + } + + s->wb_size -= tmp->sz; + list->head = tmp->next; + write_buffer_free(ss,tmp); + } + list->tail = NULL; + + return -1; +} + +static int +send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { + if (s->protocol == PROTOCOL_TCP) { + return send_list_tcp(ss, s, list, result); + } else { + return send_list_udp(ss, s, list, result); + } +} + static inline int list_uncomplete(struct wb_list *s) { struct write_buffer *wb = s->head; @@ -500,9 +623,9 @@ send_buffer(struct socket_server *ss, struct socket *s, struct socket_message *r return -1; } -static int -append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_send * request, int n) { - struct write_buffer * buf = MALLOC(sizeof(*buf)); +static struct write_buffer * +append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_send * request, int size, int n) { + struct write_buffer * buf = MALLOC(size); struct send_object so; buf->userobject = send_object_init(ss, &so, request->buffer, request->sz); buf->ptr = so.buffer+n; @@ -517,17 +640,27 @@ append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_s s->tail->next = buf; s->tail = buf; } - return buf->sz; + return buf; +} + +static inline void +append_sendbuffer_udp(struct socket_server *ss, struct socket *s, int priority, struct request_send * request, const uint8_t udp_address[UDP_ADDRESS_SIZE]) { + struct wb_list *wl = (priority == PRIORITY_HIGH) ? &s->high : &s->low; + struct write_buffer *buf = append_sendbuffer_(ss, wl, request, SIZEOF_UDPBUFFER, 0); + memcpy(buf->udp_address, udp_address, UDP_ADDRESS_SIZE); + s->wb_size += buf->sz; } static inline void append_sendbuffer(struct socket_server *ss, struct socket *s, struct request_send * request, int n) { - s->wb_size += append_sendbuffer_(ss, &s->high, request, n); + struct write_buffer *buf = append_sendbuffer_(ss, &s->high, request, SIZEOF_TCPBUFFER, n); + s->wb_size += buf->sz; } static inline void append_sendbuffer_low(struct socket_server *ss,struct socket *s, struct request_send * request) { - s->wb_size += append_sendbuffer_(ss, &s->low, request, 0); + struct write_buffer *buf = append_sendbuffer_(ss, &s->low, request, SIZEOF_TCPBUFFER, 0); + s->wb_size += buf->sz; } static inline int @@ -543,7 +676,7 @@ send_buffer_empty(struct socket *s) { Else append package to high (PRIORITY_HIGH) or low (PRIORITY_LOW) list. */ static int -send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority) { +send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority, const uint8_t *udp_address) { int id = request->id; struct socket * s = &ss->slot[HASH_ID(id)]; struct send_object so; @@ -556,30 +689,52 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock } assert(s->type != SOCKET_TYPE_PLISTEN && s->type != SOCKET_TYPE_LISTEN); if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) { - int n = write(s->fd, so.buffer, so.sz); - if (n<0) { - switch(errno) { - case EINTR: - case EAGAIN: - n = 0; - break; - default: - fprintf(stderr, "socket-server: write to %d (fd=%d) error :%s.\n",id,s->fd,strerror(errno)); - force_close(ss,s,result); - return SOCKET_CLOSE; + if (s->protocol == PROTOCOL_TCP) { + int n = write(s->fd, so.buffer, so.sz); + if (n<0) { + switch(errno) { + case EINTR: + case EAGAIN: + n = 0; + break; + default: + fprintf(stderr, "socket-server: write to %d (fd=%d) error :%s.\n",id,s->fd,strerror(errno)); + force_close(ss,s,result); + return SOCKET_CLOSE; + } + } + if (n == so.sz) { + so.free_func(request->buffer); + return -1; + } + append_sendbuffer(ss, s, request, n); // add to high priority list, even priority == PRIORITY_LOW + } else { + // udp + if (udp_address == NULL) { + udp_address = s->p.udp_address; + } + union sockaddr_all sa; + socklen_t sasz = udp_socket_address(s, udp_address, &sa); + int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); + if (n != so.sz) { + append_sendbuffer_udp(ss,s,priority,request,udp_address); + } else { + so.free_func(request->buffer); } } - if (n == so.sz) { - so.free_func(request->buffer); - return -1; - } - append_sendbuffer(ss, s, request, n); // add to high priority list, even priority == PRIORITY_LOW sp_write(ss->event_fd, s->fd, s, true); } else { - if (priority == PRIORITY_LOW) { - append_sendbuffer_low(ss, s, request); + if (s->protocol == PROTOCOL_TCP) { + if (priority == PRIORITY_LOW) { + append_sendbuffer_low(ss, s, request); + } else { + append_sendbuffer(ss, s, request, 0); + } } else { - append_sendbuffer(ss, s, request, 0); + if (udp_address == NULL) { + udp_address = s->p.udp_address; + } + append_sendbuffer_udp(ss,s,priority,request,udp_address); } } return -1; @@ -589,7 +744,7 @@ static int listen_socket(struct socket_server *ss, struct request_listen * request, struct socket_message *result) { int id = request->id; int listen_fd = request->fd; - struct socket *s = new_fd(ss, id, listen_fd, request->opaque, false); + struct socket *s = new_fd(ss, id, listen_fd, PROTOCOL_TCP, request->opaque, false); if (s == NULL) { goto _failed; } @@ -639,7 +794,7 @@ bind_socket(struct socket_server *ss, struct request_bind *request, struct socke result->id = id; result->opaque = request->opaque; result->ud = 0; - struct socket *s = new_fd(ss, id, request->fd, request->opaque, true); + struct socket *s = new_fd(ss, id, request->fd, PROTOCOL_TCP, request->opaque, true); if (s == NULL) { result->data = NULL; return SOCKET_ERROR; @@ -719,6 +874,49 @@ has_cmd(struct socket_server *ss) { return 0; } +static void +add_udp_socket(struct socket_server *ss, struct request_udp *udp) { + int id = udp->id; + int protocol; + if (udp->family == AF_INET6) { + protocol = PROTOCOL_UDPv6; + } else { + protocol = PROTOCOL_UDP; + } + struct socket *ns = new_fd(ss, id, udp->fd, protocol, udp->opaque, true); + if (ns == NULL) { + close(udp->fd); + ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; + } + ns->type = SOCKET_TYPE_CONNECTED; + memset(ns->p.udp_address, 0, sizeof(ns->p.udp_address)); +} + +static int +set_udp_address(struct socket_server *ss, struct request_setudp *request, struct socket_message *result) { + int id = request->id; + struct socket *s = &ss->slot[HASH_ID(id)]; + if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { + return -1; + } + int type = request->address[0]; + if (type != s->protocol) { + // protocol mismatch + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = NULL; + + return SOCKET_ERROR; + } + if (type == PROTOCOL_UDP) { + memcpy(s->p.udp_address, request->address, 1+2+4); // 1 type, 2 port, 4 ipv4 + } else { + memcpy(s->p.udp_address, request->address, 1+2+16); // 1 type, 2 port, 16 ipv6 + } + return -1; +} + // return type static int ctrl_cmd(struct socket_server *ss, struct socket_message *result) { @@ -749,12 +947,21 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { result->data = NULL; return SOCKET_EXIT; case 'D': - return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_HIGH); + return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_HIGH, NULL); case 'P': - return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_LOW); + return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_LOW, NULL); + case 'A': { + struct request_send_udp * rsu = (struct request_send_udp *)buffer; + return send_socket(ss, &rsu->send, result, PRIORITY_HIGH, rsu->address); + } + case 'C': + return set_udp_address(ss, (struct request_setudp *)buffer, result); case 'T': setopt_socket(ss, (struct request_setopt *)buffer); return -1; + case 'U': + add_udp_socket(ss, (struct request_udp *)buffer); + return -1; default: fprintf(stderr, "socket-server: Unknown ctrl %c.\n",type); return -1; @@ -765,8 +972,8 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) { // return -1 (ignore) when error static int -forward_message(struct socket_server *ss, struct socket *s, struct socket_message * result) { - int sz = s->size; +forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_message * result) { + int sz = s->p.size; char * buffer = MALLOC(sz); int n = (int)read(s->fd, buffer, sz); if (n<0) { @@ -797,9 +1004,9 @@ forward_message(struct socket_server *ss, struct socket *s, struct socket_messag } if (n == sz) { - s->size *= 2; + s->p.size *= 2; } else if (sz > MIN_READ_BUFFER && n*2 < sz) { - s->size /= 2; + s->p.size /= 2; } result->opaque = s->opaque; @@ -809,6 +1016,63 @@ forward_message(struct socket_server *ss, struct socket *s, struct socket_messag return SOCKET_DATA; } +static int +gen_udp_address(int protocol, union sockaddr_all *sa, uint8_t * udp_address) { + int addrsz = 1; + udp_address[0] = (uint8_t)protocol; + if (protocol == PROTOCOL_UDP) { + memcpy(udp_address+addrsz, &sa->v4.sin_port, sizeof(sa->v4.sin_port)); + addrsz += sizeof(sa->v4.sin_port); + memcpy(udp_address+addrsz, &sa->v4.sin_addr, sizeof(sa->v4.sin_addr)); + addrsz += sizeof(sa->v4.sin_addr); + } else { + memcpy(udp_address+addrsz, &sa->v6.sin6_port, sizeof(sa->v6.sin6_port)); + addrsz += sizeof(sa->v6.sin6_port); + memcpy(udp_address+addrsz, &sa->v6.sin6_addr, sizeof(sa->v6.sin6_addr)); + addrsz += sizeof(sa->v6.sin6_addr); + } + return addrsz; +} + +static int +forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_message * result) { + union sockaddr_all sa; + socklen_t slen = sizeof(sa); + int n = recvfrom(s->fd, ss->udpbuffer,MAX_UDP_PACKAGE,0,&sa.s,&slen); + if (n<0) { + switch(errno) { + case EINTR: + case EAGAIN: + break; + default: + // close when error + force_close(ss, s, result); + return SOCKET_ERROR; + } + return -1; + } + uint8_t * data; + if (slen == sizeof(sa.v4)) { + if (s->protocol != PROTOCOL_UDP) + return -1; + data = MALLOC(n + 1 + 2 + 4); + gen_udp_address(PROTOCOL_UDP, &sa, data + n); + } else { + if (s->protocol != PROTOCOL_UDPv6) + return -1; + data = MALLOC(n + 1 + 2 + 16); + gen_udp_address(PROTOCOL_UDPv6, &sa, data + n); + } + memcpy(data, ss->udpbuffer, n); + + result->opaque = s->opaque; + result->id = s->id; + result->ud = n; + result->data = (char *)data; + + return SOCKET_UDP; +} + static int report_connect(struct socket_server *ss, struct socket *s, struct socket_message *result) { int error; @@ -855,7 +1119,7 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message } socket_keepalive(client_fd); sp_nonblocking(client_fd); - struct socket *ns = new_fd(ss, id, client_fd, s->opaque, false); + struct socket *ns = new_fd(ss, id, client_fd, PROTOCOL_TCP, s->opaque, false); if (ns == NULL) { close(client_fd); return 0; @@ -941,7 +1205,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int break; default: if (e->read) { - int type = forward_message(ss, s, result); + int type = (s->protocol == PROTOCOL_TCP ? forward_message_tcp : forward_message_udp)(ss, s, result); if (e->write) { // Try to dispatch write message next step if write flag set. e->read = false; @@ -986,9 +1250,11 @@ open_request(struct socket_server *ss, struct request_package *req, uintptr_t op int len = strlen(addr); if (len + sizeof(req->u.open) > 256) { fprintf(stderr, "socket-server : Invalid addr %s.\n",addr); - return 0; + return -1; } int id = reserve_id(ss); + if (id < 0) + return -1; req->u.open.opaque = opaque; req->u.open.id = id; req->u.open.port = port; @@ -1002,6 +1268,8 @@ int socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { struct request_package request; int len = open_request(ss, &request, opaque, addr, port); + if (len < 0) + return -1; send_request(ss, &request, 'O', sizeof(request.u.open) + len); return request.u.open.id; } @@ -1052,38 +1320,63 @@ socket_server_close(struct socket_server *ss, uintptr_t opaque, int id) { send_request(ss, &request, 'K', sizeof(request.u.close)); } +// return -1 means failed +// or return AF_INET or AF_INET6 +static int +do_bind(const char *host, int port, int protocol, int *family) { + int fd; + int status; + int reuse = 1; + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + char portstr[16]; + if (host == NULL || host[0] == 0) { + host = "0.0.0.0"; // INADDR_ANY + } + sprintf(portstr, "%d", port); + memset( &ai_hints, 0, sizeof( ai_hints ) ); + ai_hints.ai_family = AF_UNSPEC; + if (protocol == IPPROTO_TCP) { + ai_hints.ai_socktype = SOCK_STREAM; + } else { + assert(protocol == IPPROTO_UDP); + ai_hints.ai_socktype = SOCK_DGRAM; + } + ai_hints.ai_protocol = protocol; + + status = getaddrinfo( host, portstr, &ai_hints, &ai_list ); + if ( status != 0 ) { + return -1; + } + ai_hints = *ai_list; + freeaddrinfo( ai_list ); + *family = ai_hints.ai_family; + fd = socket(*family, ai_hints.ai_socktype, 0); + if (fd < 0) { + return -1; + } + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int))==-1) { + return -1; + } + status = bind(fd, (struct sockaddr *)ai_hints.ai_addr, ai_hints.ai_addrlen); + if (status != 0) + return -1; + + return fd; +} + static int do_listen(const char * host, int port, int backlog) { - // only support ipv4 - // todo: support ipv6 by getaddrinfo - uint32_t addr = INADDR_ANY; - if (host[0]) { - addr=inet_addr(host); - } - int listen_fd = socket(AF_INET, SOCK_STREAM, 0); + int family = 0; + int listen_fd = do_bind(host, port, IPPROTO_TCP, &family); if (listen_fd < 0) { return -1; } - int reuse = 1; - if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int))==-1) { - goto _failed; - } - - struct sockaddr_in my_addr; - memset(&my_addr, 0, sizeof(struct sockaddr_in)); - my_addr.sin_family = AF_INET; - my_addr.sin_port = htons(port); - my_addr.sin_addr.s_addr = addr; - if (bind(listen_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { - goto _failed; - } if (listen(listen_fd, backlog) == -1) { - goto _failed; + close(listen_fd); + return -1; } return listen_fd; -_failed: - close(listen_fd); - return -1; } int @@ -1094,6 +1387,10 @@ socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * ad } struct request_package request; int id = reserve_id(ss); + if (id < 0) { + close(fd); + return id; + } request.u.listen.opaque = opaque; request.u.listen.id = id; request.u.listen.fd = fd; @@ -1105,6 +1402,8 @@ int socket_server_bind(struct socket_server *ss, uintptr_t opaque, int fd) { struct request_package request; int id = reserve_id(ss); + if (id < 0) + return -1; request.u.bind.opaque = opaque; request.u.bind.id = id; request.u.bind.fd = fd; @@ -1134,3 +1433,122 @@ socket_server_userobject(struct socket_server *ss, struct socket_object_interfac ss->soi = *soi; } +// UDP + +int +socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { + int fd; + int family; + if (port != 0 || addr != NULL) { + // bind + fd = do_bind(addr, port, IPPROTO_UDP, &family); + if (fd < 0) { + return -1; + } + } else { + family = AF_INET; + fd = socket(family, SOCK_DGRAM, 0); + if (fd < 0) { + return -1; + } + } + int id = reserve_id(ss); + if (id < 0) { + close(fd); + return -1; + } + struct request_package request; + request.u.udp.id = id; + request.u.udp.fd = fd; + request.u.udp.opaque = opaque; + request.u.udp.family = family; + + send_request(ss, &request, 'U', sizeof(request.u.udp)); + return id; +} + +int64_t +socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp_address *addr, const void *buffer, int sz) { + struct socket * s = &ss->slot[HASH_ID(id)]; + if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + return -1; + } + + struct request_package request; + request.u.send_udp.send.id = id; + request.u.send_udp.send.sz = sz; + request.u.send_udp.send.buffer = (char *)buffer; + + const uint8_t *udp_address = (const uint8_t *)addr; + int addrsz; + switch (udp_address[0]) { + case PROTOCOL_UDP: + addrsz = 1+2+4; // 1 type, 2 port, 4 ipv4 + break; + case PROTOCOL_UDPv6: + addrsz = 1+2+16; // 1 type, 2 port, 16 ipv6 + break; + default: + return -1; + } + + memcpy(request.u.send_udp.address, udp_address, addrsz); + + send_request(ss, &request, 'A', sizeof(request.u.send_udp.send)+addrsz); + return s->wb_size; +} + +int +socket_server_udp_connect(struct socket_server *ss, int id, const char * addr, int port) { + int status; + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + char portstr[16]; + sprintf(portstr, "%d", port); + memset( &ai_hints, 0, sizeof( ai_hints ) ); + ai_hints.ai_family = AF_UNSPEC; + ai_hints.ai_socktype = SOCK_DGRAM; + ai_hints.ai_protocol = IPPROTO_UDP; + + status = getaddrinfo(addr, portstr, &ai_hints, &ai_list ); + if ( status != 0 ) { + return -1; + } + struct request_package request; + request.u.set_udp.id = id; + int protocol; + + if (ai_list->ai_family == AF_INET) { + protocol = PROTOCOL_UDP; + } else if (ai_list->ai_family == AF_INET6) { + protocol = PROTOCOL_UDPv6; + } else { + freeaddrinfo( ai_list ); + return -1; + } + + int addrsz = gen_udp_address(protocol, (union sockaddr_all *)ai_list->ai_addr, request.u.set_udp.address); + + freeaddrinfo( ai_list ); + + send_request(ss, &request, 'C', sizeof(request.u.set_udp) - sizeof(request.u.set_udp.address) +addrsz); + + return 0; +} + +const struct socket_udp_address * +socket_server_udp_address(struct socket_server *ss, struct socket_message *msg, int *addrsz) { + uint8_t * address = (uint8_t *)(msg->data + msg->ud); + int type = address[0]; + switch(type) { + case PROTOCOL_UDP: + *addrsz = 1+2+4; + break; + case PROTOCOL_UDPv6: + *addrsz = 1+2+16; + break; + default: + return NULL; + } + return (const struct socket_udp_address *)address; +} diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index a4dcf71e..b6f0f5fb 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -9,6 +9,7 @@ #define SOCKET_ACCEPT 3 #define SOCKET_ERROR 4 #define SOCKET_EXIT 5 +#define SOCKET_UDP 6 struct socket_server; @@ -36,8 +37,22 @@ int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port); int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd); +// for tcp void socket_server_nodelay(struct socket_server *, int id); +struct socket_udp_address; + +// create an udp socket handle, attach opaque with it . udp socket don't need call socket_server_start to recv message +// if port != 0, bind the socket . if addr == NULL, bind ipv4 0.0.0.0 . If you want to use ipv6, addr can be "::" and port 0. +int socket_server_udp(struct socket_server *, uintptr_t opaque, const char * addr, int port); +// set default dest address, return 0 when success +int socket_server_udp_connect(struct socket_server *, int id, const char * addr, int port); +// If the socket_udp_address is NULL, use last call socket_server_udp_connect address instead +// You can also use socket_server_send +int64_t socket_server_udp_send(struct socket_server *, int id, const struct socket_udp_address *, const void *buffer, int sz); +// extract the address of the message, struct socket_message * should be SOCKET_UDP +const struct socket_udp_address * socket_server_udp_address(struct socket_server *, struct socket_message *, int *addrsz); + struct socket_object_interface { void * (*buffer)(void *); int (*size)(void *); diff --git a/test/testudp.lua b/test/testudp.lua new file mode 100644 index 00000000..63ca8d7a --- /dev/null +++ b/test/testudp.lua @@ -0,0 +1,23 @@ +local skynet = require "skynet" +local socket = require "socket" + +local function server() + local host + host = socket.udp(function(data, sz, from) + print("server recv", skynet.tostring(data,sz), socket.udp_address(from)) + socket.sendto(host, from, "OK") + end , "127.0.0.1", 8765) -- bind an address +end + +local function client() + local c = socket.udp(function(data, sz, from) + print("client recv", skynet.tostring(data,sz), socket.udp_address(from)) + end) + socket.udp_connect(c, "127.0.0.1", 8765) + socket.write(c, "hello") -- write to the address by udp_connect binding +end + +skynet.start(function() + skynet.fork(server) + skynet.fork(client) +end) From 8e4a1751550da9dad68c588a741afe6b973e36af Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Nov 2014 11:19:58 +0800 Subject: [PATCH 237/729] use fork to dispatch more socket message --- lualib-src/lua-netpack.c | 41 ++++++++++++-------------------------- lualib/snax/gateserver.lua | 24 ++++++++++++++++------ 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index e424f90c..f42e21ca 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -240,19 +240,12 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) { buffer += need; size -= need; if (size == 0) { - if (q == NULL || q->head == q->tail ) { - lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); - lua_pushinteger(L, fd); - lua_pushlightuserdata(L, uc->pack.buffer); - lua_pushinteger(L, uc->pack.size); - skynet_free(uc); - return 5; - } - else{ - push_data(L, fd, uc->pack.buffer, uc->pack.size, 0); - skynet_free(uc); - return 1; - } + lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); + lua_pushinteger(L, fd); + lua_pushlightuserdata(L, uc->pack.buffer); + lua_pushinteger(L, uc->pack.size); + skynet_free(uc); + return 5; } // more data push_data(L, fd, uc->pack.buffer, uc->pack.size, 0); @@ -281,21 +274,13 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) { } if (size == pack_size) { // just one package - if ( q == NULL || q->head == q->tail) { - lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); - lua_pushinteger(L, fd); - void * result = skynet_malloc(pack_size); - memcpy(result, buffer, size); - lua_pushlightuserdata(L, result); - lua_pushinteger(L, size); - return 5; - } - else{ - push_data(L, fd, buffer, pack_size, 1); - buffer += pack_size; - size -= pack_size; - return 1; - } + lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); + lua_pushinteger(L, fd); + void * result = skynet_malloc(pack_size); + memcpy(result, buffer, size); + lua_pushlightuserdata(L, result); + lua_pushinteger(L, size); + return 5; } // more data push_data(L, fd, buffer, pack_size, 1); diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 6cce5266..e68efc12 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -53,20 +53,32 @@ function gateserver.start(handler) local MSG = {} - function MSG.data(fd, msg, sz) + local function dispatch_msg(fd, msg, sz) if connection[fd] then handler.message(fd, msg, sz) + else + skynet.error(string.format("Drop message from fd (%d) : %s", fd, netpack.tostring(msg,sz))) end end - function MSG.more() - for fd, msg, sz in netpack.pop, queue do - if connection[fd] then - handler.message(fd, msg, sz) + MSG.data = dispatch_msg + + local function dispatch_queue() + local fd, msg, sz = netpack.pop(queue) + if fd then + -- may dispatch even the handler.message blocked + -- If the handler.message never block, the queue should be empty, so only fork once and then exit. + skynet.fork(dispatch_queue) + dispatch_msg(fd, msg, sz) + + for fd, msg, sz in netpack.pop, queue do + dispatch_msg(fd, msg, sz) end end end + MSG.more = dispatch_queue + function MSG.open(fd, msg) if client_number >= maxclient then socketdriver.close(fd) @@ -128,4 +140,4 @@ function gateserver.start(handler) end) end -return gateserver \ No newline at end of file +return gateserver From 20b181c56d324d6db6b26eb3a16e383d45ba4f5b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Nov 2014 11:44:46 +0800 Subject: [PATCH 238/729] If recv one udp message, try read next immediately rather than until next epoll_wait --- skynet-src/socket_server.c | 14 +++++++++++++- test/testudp.lua | 9 ++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 630f86eb..c24b0745 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1205,7 +1205,17 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int break; default: if (e->read) { - int type = (s->protocol == PROTOCOL_TCP ? forward_message_tcp : forward_message_udp)(ss, s, result); + int type; + if (s->protocol == PROTOCOL_TCP) { + type = forward_message_tcp(ss, s, result); + } else { + type = forward_message_udp(ss, s, result); + if (type == SOCKET_UDP) { + // try read again + --ss->event_index; + return SOCKET_UDP; + } + } if (e->write) { // Try to dispatch write message next step if write flag set. e->read = false; @@ -1452,6 +1462,8 @@ socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, return -1; } } + sp_nonblocking(fd); + int id = reserve_id(ss); if (id < 0) { close(fd); diff --git a/test/testudp.lua b/test/testudp.lua index 63ca8d7a..d8387e29 100644 --- a/test/testudp.lua +++ b/test/testudp.lua @@ -4,8 +4,9 @@ local socket = require "socket" local function server() local host host = socket.udp(function(data, sz, from) - print("server recv", skynet.tostring(data,sz), socket.udp_address(from)) - socket.sendto(host, from, "OK") + local str = skynet.tostring(data,sz) -- skynet.tostring should call only once, because it will free the pointer data + print("server recv", str, socket.udp_address(from)) + socket.sendto(host, from, "OK " .. str) end , "127.0.0.1", 8765) -- bind an address end @@ -14,7 +15,9 @@ local function client() print("client recv", skynet.tostring(data,sz), socket.udp_address(from)) end) socket.udp_connect(c, "127.0.0.1", 8765) - socket.write(c, "hello") -- write to the address by udp_connect binding + for i=1,20 do + socket.write(c, "hello " .. i) -- write to the address by udp_connect binding + end end skynet.start(function() From b3cf1bc625f0b4c0caf196e64a6361387c753386 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Nov 2014 12:14:02 +0800 Subject: [PATCH 239/729] remove unused code --- lualib/socket.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lualib/socket.lua b/lualib/socket.lua index 43d59299..c00f8b32 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -148,7 +148,7 @@ local function connect(id, func) id = id, buffer = newbuffer, connected = false, - read_require = false, + read_required = false, co = false, callback = func, protocol = "TCP", @@ -373,8 +373,6 @@ local udp_socket = {} local function create_udp_object(id, cb) socket_pool[id] = { id = id, - read_require = false, - co = false, connected = true, protocol = "UDP", callback = cb, From b9e06dc0656e7be9e039201bc2b9efa175e53ac4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 14 Nov 2014 15:11:05 +0800 Subject: [PATCH 240/729] add snax.self and snax.exit --- lualib/snax.lua | 8 ++++++++ test/pingserver.lua | 5 +++++ test/testping.lua | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lualib/snax.lua b/lualib/snax.lua index f810e392..6a01460f 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -132,6 +132,14 @@ function snax.kill(obj, ...) skynet_call(obj.handle, "snax", t.system.exit, ...) end +function snax.self() + return snax.bind(skynet.self(), SERVICE_NAME) +end + +function snax.exit(...) + snax.kill(snax.self(), ...) +end + local function test_result(ok, ...) if ok then return ... diff --git a/test/pingserver.lua b/test/pingserver.lua index 2ddefd7f..5c44a21a 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local queue = require "skynet.queue" +local snax = require "snax" local i = 0 local hello = "hello" @@ -32,6 +33,10 @@ function accept.hello() end) end +function accept.exit(...) + snax.exit(...) +end + function response.error() error "throw an error" end diff --git a/test/testping.lua b/test/testping.lua index c9f05e63..fb0514e8 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -31,6 +31,6 @@ end print(string.format("%s\tcount:%d time:%f", name, v.count, v.time)) end - print(snax.kill(ps,"exit")) + print(ps.post.exit("exit")) -- == snax.kill(ps, "exit") skynet.exit() end) From 6df72e58420a5688f80f12bef359bf2606473506 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 14 Nov 2014 16:13:48 +0800 Subject: [PATCH 241/729] remove snax queue mode --- service/snaxd.lua | 69 ++------------------------------------------- test/pingserver.lua | 3 -- 2 files changed, 3 insertions(+), 69 deletions(-) diff --git a/service/snaxd.lua b/service/snaxd.lua index 0ff9baa0..8be269e9 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -12,10 +12,6 @@ package.path = snax_path .. "?.lua;" .. package.path SERVICE_NAME = snax_name SERVICE_PATH = snax_path -local mode -local thread_id -local message_queue = {} -local init = false local profile_table = {} local function update_stat(name, ti) @@ -38,57 +34,6 @@ local function dispatch(f, ...) return skynet.pack(f(...)) end -local function message_dispatch() - while true do - if #message_queue==0 then - thread_id = coroutine.running() - skynet.wait() - else - local msg = table.remove(message_queue,1) - local method = msg.method - local f = method[4] - if f then - if method[2] == "accept" then - -- no return - profile.start() - local ok, data = xpcall(f, traceback, table.unpack(msg)) - local ti = profile.stop() - update_stat(method[3], ti) - if not ok then - print(string.format("Error on [:%x] to [:%x] %s", msg.source, skynet.self(), tostring(data))) - end - else - profile.start() - local ok, data, size = xpcall(dispatch, traceback, f, table.unpack(msg)) - local ti = profile.stop() - update_stat(method[3], ti) - if ok then - -- skynet.PTYPE_RESPONSE == 1 - c.send(msg.source, 1, msg.session, data, size) - if method[2] == "system" then - init = false - skynet.exit() - break - end - else - -- Can't throw error, so print it directly - print(string.format("Error on [:%x] to [:%x] %s", msg.source, skynet.self(), tostring(data))) - c.send(msg.source, skynet.PTYPE_ERROR, msg.session, "") - end - end - end - end - end -end - -local function queue( session, source, method, ...) - table.insert(message_queue, {session = session, source = source, method = method, ... }) - if thread_id then - skynet.wakeup(thread_id) - thread_id = nil - end -end - local function return_f(f, ...) return skynet.ret(skynet.pack(f(...))) end @@ -109,6 +54,7 @@ end skynet.start(function() skynet.dispatch("snax", function ( session , source , id, ...) + local init = false local method = func[id] if method[2] == "system" then @@ -119,17 +65,12 @@ skynet.start(function() elseif command == "init" then assert(not init, "Already init") local initfunc = method[4] or function() end - mode = initfunc(...) - if mode == "queue" then - skynet.fork(message_dispatch) - end + initfunc(...) skynet.ret() skynet.info_func(function() return profile_table end) init = true - elseif mode == "queue" then - queue( session, source, method , ...) else assert(init, "Never init") assert(command == "exit") @@ -141,11 +82,7 @@ skynet.start(function() end else assert(init, "Init first") - if mode == "queue" then - queue(session, source, method , ...) - else - timing(method, ...) - end + timing(method, ...) end end) end) diff --git a/test/pingserver.lua b/test/pingserver.lua index 5c44a21a..65a25405 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -45,9 +45,6 @@ function init( ... ) print ("ping server start:", ...) -- init queue lock = queue() - --- You can return "queue" for queue service mode --- return "queue" end function exit(...) From ae041c97b3b61509ee3825c5ac6c10a1aa199f98 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 14 Nov 2014 16:27:26 +0800 Subject: [PATCH 242/729] bugfix: use ai_list instead of ai_hints --- skynet-src/socket_server.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index c24b0745..4010055e 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1358,21 +1358,25 @@ do_bind(const char *host, int port, int protocol, int *family) { if ( status != 0 ) { return -1; } - ai_hints = *ai_list; - freeaddrinfo( ai_list ); - *family = ai_hints.ai_family; - fd = socket(*family, ai_hints.ai_socktype, 0); + *family = ai_list->ai_family; + fd = socket(*family, ai_list->ai_socktype, 0); if (fd < 0) { - return -1; + goto _failed_fd; } if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int))==-1) { - return -1; + goto _failed; } - status = bind(fd, (struct sockaddr *)ai_hints.ai_addr, ai_hints.ai_addrlen); + status = bind(fd, (struct sockaddr *)ai_list->ai_addr, ai_list->ai_addrlen); if (status != 0) - return -1; + goto _failed; + freeaddrinfo( ai_list ); return fd; +_failed: + close(fd); +_failed_fd: + freeaddrinfo( ai_list ); + return -1; } static int From 4f21696387ad28ddd776af372727f9529c5b2275 Mon Sep 17 00:00:00 2001 From: snail Date: Fri, 14 Nov 2014 18:11:01 +0800 Subject: [PATCH 243/729] ecsape char '\Z' wrong In mysql-connector, it's '\Z', not '\z'.Now, this bug will change oringinal data --- lualib-src/lua_mysqlaux.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua_mysqlaux.c b/lualib-src/lua_mysqlaux.c index 31fabad8..e6a3edc3 100755 --- a/lualib-src/lua_mysqlaux.c +++ b/lualib-src/lua_mysqlaux.c @@ -24,7 +24,7 @@ static unsigned int num_escape_sql_str(unsigned char *dst, unsigned char *src, s case '\n': case '\r': case '\t': - case 26: /* \z */ + case 26: /* \Z */ case '\\': case '\'': case '"': @@ -73,7 +73,7 @@ escape_sql_str(unsigned char *dst, unsigned char *src, size_t size) case 26: *dst++ = '\\'; - *dst++ = 'z'; + *dst++ = 'Z'; break; case '\\': From 6ee014dd99323fedebe2e8e511265920f40550ac Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 15 Nov 2014 21:28:03 +0800 Subject: [PATCH 244/729] bugfix: init should be out --- service/snaxd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/snaxd.lua b/service/snaxd.lua index 8be269e9..02e3738c 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -53,8 +53,8 @@ local function timing( method, ... ) end skynet.start(function() + local init = false skynet.dispatch("snax", function ( session , source , id, ...) - local init = false local method = func[id] if method[2] == "system" then From 3be0f507221617f68b27b81e1ef6cc0dafc062df Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 15 Nov 2014 21:33:25 +0800 Subject: [PATCH 245/729] use snax.newservice would be better --- test/testping.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testping.lua b/test/testping.lua index fb0514e8..c40392cf 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -2,7 +2,7 @@ local skynet = require "skynet" local snax = require "snax" skynet.start(function() - local ps = snax.uniqueservice ("pingserver", "hello world") + local ps = snax.newservice ("pingserver", "hello world") print(ps.req.ping("foobar")) print(ps.post.hello()) print(pcall(ps.req.error)) From ee61e6063152c40ee2c054b6ad1ab9c90acbbac5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 Nov 2014 14:55:21 +0800 Subject: [PATCH 246/729] ready for 0.9.0 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index f15ef86c..e8835119 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v0.9.0 (2014-11-17) +----------- +* Add UDP support +* Add IPv6 support +* socket send package can define a release method +* dispatch read before write in epoll +* remove snax queue mode +* Fix a bug in big-endian architecture + v0.8.1 (2014-11-3) ----------- * Send to an invalid remote service will raise an error From ff6c92b9752045361dcaa08f635cbbeee03d1716 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 20 Nov 2014 11:33:16 +0800 Subject: [PATCH 247/729] add comment --- lualib-src/lua-socket.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index c99d85d3..df81ce17 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -82,6 +82,15 @@ lnewbuffer(lua_State *L) { int size return size + + Comment: The table pool record all the buffers chunk, + and the first index [1] is a lightuserdata : free_node. We can always use this pointer for struct buffer_node . + The following ([2] ...) userdatas in table pool is the buffer chunk (for struct buffer_node), + we never free them until the VM closed. The size of first chunk ([2]) is 8 struct buffer_node, + and the second size is 16 ... The largest size of chunk is LARGE_PAGE_NODE (4096) + + lpushbbuffer will get a free struct buffer_node from table pool, and then put the msg/size in it. + lpopbuffer return the struct buffer_node back to table pool (By calling return_free_node). */ static int lpushbuffer(lua_State *L) { From 84cb4bb4c18b55d3a3ca1d8b4269f40fb9150477 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 25 Nov 2014 16:04:26 +0800 Subject: [PATCH 248/729] use explicit conversion --- skynet-src/socket_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 4010055e..5622e4a8 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -628,7 +628,7 @@ append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_s struct write_buffer * buf = MALLOC(size); struct send_object so; buf->userobject = send_object_init(ss, &so, request->buffer, request->sz); - buf->ptr = so.buffer+n; + buf->ptr = (char*)so.buffer+n; buf->sz = so.sz - n; buf->buffer = request->buffer; buf->next = NULL; From 2b13eb250dacb6ac4c9cb88e92b6b6d2778f0b5c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 26 Nov 2014 21:18:49 +0800 Subject: [PATCH 249/729] update sproto for big-endian --- lualib-src/sproto/lsproto.c | 10 +++++----- lualib-src/sproto/sproto.c | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index af86e389..e5378713 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -87,7 +87,7 @@ struct encode_ud { int deep; }; -static int +static int encode(void *ud, const char *tagname, int type, int index, struct sproto_type *st, void *value, int length) { struct encode_ud *self = ud; lua_State *L = self->L; @@ -189,7 +189,7 @@ expand_buffer(lua_State *L, int osz, int nsz) { lightuserdata sproto_type table source - return string + return string */ static int lencode(lua_State *L) { @@ -229,7 +229,7 @@ struct decode_ud { int deep; }; -static int +static int decode(void *ud, const char *tagname, int type, int index, struct sproto_type *st, void *value, int length) { struct decode_ud * self = ud; lua_State *L = self->L; @@ -252,12 +252,12 @@ decode(void *ud, const char *tagname, int type, int index, struct sproto_type *s switch (type) { case SPROTO_TINTEGER: { // notice: in lua 5.2, 52bit integer support (not 64) - lua_Integer v = *(lua_Integer *)value; + lua_Integer v = *(uint64_t*)value; lua_pushinteger(L, v); break; } case SPROTO_TBOOLEAN: { - int v = *(lua_Integer*)value; + int v = *(uint64_t*)value; lua_pushboolean(L,v); break; } diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 24d36492..67e0d0d0 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -500,7 +500,7 @@ sproto_dump(struct sproto *s) { } // query -int +int sproto_prototag(struct sproto *sp, const char * name) { int i; for (i=0;iprotocol_n;i++) { @@ -529,7 +529,7 @@ query_proto(struct sproto *sp, int tag) { return NULL; } -struct sproto_type * +struct sproto_type * sproto_protoquery(struct sproto *sp, int proto, int what) { struct protocol * p; if (what <0 || what >1) { @@ -542,7 +542,7 @@ sproto_protoquery(struct sproto *sp, int proto, int what) { return NULL; } -const char * +const char * sproto_protoname(struct sproto *sp, int proto) { struct protocol * p = query_proto(sp, proto); if (p) { @@ -551,7 +551,7 @@ sproto_protoname(struct sproto *sp, int proto) { return NULL; } -struct sproto_type * +struct sproto_type * sproto_type(struct sproto *sp, const char * type_name) { int i; for (i=0;itype_n;i++) { @@ -806,7 +806,7 @@ encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int s return fill_size(data, sz); } -int +int sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) { uint8_t * header = buffer; uint8_t * data; @@ -830,7 +830,7 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c sz = encode_array(cb,ud, f, data, size); } else { switch(type) { - case SPROTO_TINTEGER: + case SPROTO_TINTEGER: case SPROTO_TBOOLEAN: { union { uint64_t u64; @@ -971,7 +971,7 @@ decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { } case SPROTO_TBOOLEAN: for (i=0;iname, SPROTO_TBOOLEAN, i+1, NULL, &value, sizeof(value)); } break; @@ -1050,7 +1050,7 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba } break; } - case SPROTO_TSTRING: + case SPROTO_TSTRING: case SPROTO_TSTRUCT: { uint32_t sz = todword(currentdata); if (cb(ud, f->name, f->type, 0, f->st, currentdata+SIZEOF_LENGTH, sz)) @@ -1124,7 +1124,7 @@ write_ff(const uint8_t * src, uint8_t * des, int n) { } } -int +int sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { uint8_t tmp[8]; int i; @@ -1181,7 +1181,7 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { return size; } -int +int sproto_unpack(const void * srcv, int srcsz, void * bufferv, int bufsz) { const uint8_t * src = srcv; uint8_t * buffer = bufferv; From 6f6039c13601d244e12a2190ed6200d248838879 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 Dec 2014 11:06:09 +0800 Subject: [PATCH 250/729] include sys/socket.h for freebsd --- lualib-src/lua-socket.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index df81ce17..eabb8620 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -9,6 +9,7 @@ #include #include +#include #include #include "skynet_socket.h" From 175529b11468846c4740f7240eb302547b06ae91 Mon Sep 17 00:00:00 2001 From: lmess <260281543@qq.com> Date: Wed, 3 Dec 2014 15:31:32 +0800 Subject: [PATCH 251/729] Update mongo.lua mongo auth config --- lualib/mongo.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 458e1a6c..87595c71 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -120,6 +120,8 @@ function mongo.client( conf ) local obj = { host = first.host, port = first.port or 27017, + username = first.username, + password = first.password, } obj.__id = 0 From a0d2c7172cf0bd9bb71037a9cb2a76be978ac315 Mon Sep 17 00:00:00 2001 From: xjdrew Date: Thu, 4 Dec 2014 15:50:19 +0800 Subject: [PATCH 252/729] =?UTF-8?q?=E4=BD=BF=E7=94=A8spinlock=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8Dcas=E4=B8=8D=E8=83=BD=E5=85=85=E5=88=86?= =?UTF-8?q?=E8=B0=83=E5=BA=A6testdeadloop=E8=BF=99=E6=A0=B7=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skynet-src/skynet_mq.c | 69 ++++++++++++------------------------------ test/testdeadloop.lua | 10 ++++++ 2 files changed, 30 insertions(+), 49 deletions(-) create mode 100644 test/testdeadloop.lua diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index 8cc8f9da..51f47ef1 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -32,12 +32,9 @@ struct message_queue { }; struct global_queue { - uint32_t head; - uint32_t tail; - struct message_queue ** queue; - // We use a separated flag array to ensure the mq is pushed. - // See the comments below. - struct message_queue *list; + struct message_queue *head; + struct message_queue *tail; + int lock; }; static struct global_queue *Q = NULL; @@ -51,57 +48,33 @@ void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; - uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1)); - // only one thread can set the slot (change q->queue[tail] from NULL to queue) - if (!__sync_bool_compare_and_swap(&q->queue[tail], NULL, queue)) { - // The queue may full seldom, save queue in list - assert(queue->next == NULL); - struct message_queue * last; - do { - last = q->list; - queue->next = last; - } while(!__sync_bool_compare_and_swap(&q->list, last, queue)); - - return; + LOCK(q) + assert(queue->next == NULL); + if(q->tail) { + q->tail->next = queue; + q->tail = queue; + } else { + q->head = q->tail = queue; } + UNLOCK(q) } struct message_queue * skynet_globalmq_pop() { struct global_queue *q = Q; - uint32_t head = q->head; - if (head == q->tail) { - // The queue is empty. - return NULL; - } - - uint32_t head_ptr = GP(head); - - struct message_queue * list = q->list; - if (list) { - // If q->list is not empty, try to load it back to the queue - struct message_queue *newhead = list->next; - if (__sync_bool_compare_and_swap(&q->list, list, newhead)) { - // try load list only once, if success , push it back to the queue. - list->next = NULL; - skynet_globalmq_push(list); + LOCK(q) + struct message_queue *mq = q->head; + if(mq) { + q->head = mq->next; + if(q->head == NULL) { + assert(mq == q->tail); + q->tail = NULL; } + mq->next = NULL; } - - struct message_queue * mq = q->queue[head_ptr]; - if (mq == NULL) { - // globalmq push not complete - return NULL; - } - if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) { - return NULL; - } - // only one thread can get the slot (change q->queue[head_ptr] to NULL) - if (!__sync_bool_compare_and_swap(&q->queue[head_ptr], mq, NULL)) { - return NULL; - } + UNLOCK(q) return mq; } @@ -243,8 +216,6 @@ void skynet_mq_init() { struct global_queue *q = skynet_malloc(sizeof(*q)); memset(q,0,sizeof(*q)); - q->queue = skynet_malloc(MAX_GLOBAL_MQ * sizeof(struct message_queue *)); - memset(q->queue, 0, sizeof(struct message_queue *) * MAX_GLOBAL_MQ); Q=q; } diff --git a/test/testdeadloop.lua b/test/testdeadloop.lua new file mode 100644 index 00000000..215700e2 --- /dev/null +++ b/test/testdeadloop.lua @@ -0,0 +1,10 @@ +local skynet = require "skynet" +local function dead_loop() + while true do + skynet.sleep(0) + end +end + +skynet.start(function() + skynet.fork(dead_loop) +end) From 2da2f121c8f8524984792775769f38c36b735aa0 Mon Sep 17 00:00:00 2001 From: xjdrew Date: Thu, 4 Dec 2014 16:16:17 +0800 Subject: [PATCH 253/729] delete unneccessary comment --- skynet-src/skynet_mq.c | 1 - 1 file changed, 1 deletion(-) diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index 51f47ef1..098cb210 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -48,7 +48,6 @@ void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; - // only one thread can set the slot (change q->queue[tail] from NULL to queue) LOCK(q) assert(queue->next == NULL); if(q->tail) { From fe9640e2dd6224b1f7257b08e806a5db3e019b5c Mon Sep 17 00:00:00 2001 From: dpull Date: Thu, 4 Dec 2014 20:10:24 +0800 Subject: [PATCH 254/729] =?UTF-8?q?1=E3=80=81mongo=5Fcollection:createInde?= =?UTF-8?q?x=20=E5=88=9B=E5=BB=BA=E7=B4=A2=E5=BC=95=202=E3=80=81mongo=5Fco?= =?UTF-8?q?llection:safe=5Finsert=20=E5=8F=AF=E4=BB=A5=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E7=9A=84insert=203=E3=80=81mongo=5F?= =?UTF-8?q?collection:findAndModify=20=E6=9F=A5=E8=AF=A2=E5=B9=B6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/mongo.lua | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 458e1a6c..38ac20dd 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -227,7 +227,11 @@ function mongo_collection:insert(doc) sock:request(pack) end -function mongo_collection:batch_insert(docs) +function mongo_collection:safe_insert(doc) + return self.database:runCommand("insert", self.name, "documents", {bson_encode(doc)}) +end + +function mongo_collection:batch_insert(docs) for i=1,#docs do if docs[i]._id == nil then docs[i]._id = bson.objectid() @@ -276,6 +280,48 @@ function mongo_collection:find(query, selector) } , cursor_meta) end +-- collection:createIndex({username = 1}, {unique = true}) +function mongo_collection:createIndex(keys, option) + local name + for k, v in pairs(keys) do + assert(v == 1) + name = (name == nil) and k or (name .. "_" .. k) + end + + local doc = {}; + doc.name = name + doc.key = keys + for k, v in pairs(option) do + if v then + doc[k] = true + end + end + return self.database:runCommand("createIndexes", self.name, "indexes", {doc}) +end + +mongo_collection.ensureIndex = mongo_collection.createIndex; + +-- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, }) +-- keys, value type +-- query, table +-- sort, table +-- remove, bool +-- update, table +-- new, bool +-- fields, bool +-- upsert, boolean +function mongo_collection:findAndModify(doc) + assert(doc.query) + assert(doc.update or doc.remove) + + local cmd = {"findAndModify", self.name}; + for k, v in pairs(doc) do + table.insert(cmd, k) + table.insert(cmd, v) + end + return self.database:runCommand(unpack(cmd)) +end + function mongo_cursor:hasNext() if self.__ptr == nil then if self.__document == nil then From 176e4df90c4265ec85833eab5f4f6dd47bf08126 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Dec 2014 10:53:14 +0800 Subject: [PATCH 255/729] ready for v0.9.2 --- HISTORY.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index e8835119..4fefeb98 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,10 @@ -v0.9.0 (2014-11-17) +v0.9.2 (2014-12-8) +----------- +* Simplify the message queue +* Add create_index in mongo driver +* Fix a bug in big-endian architecture (sproto) + +v0.9.0 / v0.9.1 (2014-11-17) ----------- * Add UDP support * Add IPv6 support From f1c9f9b9dc5b4daa1c017ce2533513a826abd23b Mon Sep 17 00:00:00 2001 From: dpull Date: Thu, 11 Dec 2014 10:41:59 +0800 Subject: [PATCH 256/729] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9mongo=5Fcol?= =?UTF-8?q?lection:createIndex=E7=9A=84bug=202=E3=80=81=E5=A2=9E=E5=8A=A0m?= =?UTF-8?q?ongo=5Fcollection:drop=E5=92=8Cmongo=5Fcollection:dropIndex?= =?UTF-8?q?=EF=BC=88=E6=96=B9=E4=BE=BF=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B8=85=E7=90=86=E7=8E=AF=E5=A2=83=EF=BC=89=203=E3=80=81?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=87=AA=E5=B7=B1=E9=A1=B9=E7=9B=AE=E7=94=A8?= =?UTF-8?q?=E5=88=B0=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/mongo.lua | 28 ++++++++++---- test/testmongodb.lua | 90 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 test/testmongodb.lua diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 38ac20dd..6399465d 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -282,25 +282,39 @@ end -- collection:createIndex({username = 1}, {unique = true}) function mongo_collection:createIndex(keys, option) - local name - for k, v in pairs(keys) do - assert(v == 1) - name = (name == nil) and k or (name .. "_" .. k) + local name = option.name + option.name = nil + + if not name then + for k, v in pairs(keys) do + name = (name == nil) and k or (name .. "_" .. k) + name = name .. "_" .. v + end end + local doc = {}; doc.name = name doc.key = keys for k, v in pairs(option) do - if v then - doc[k] = true - end + doc[k] = v end return self.database:runCommand("createIndexes", self.name, "indexes", {doc}) end mongo_collection.ensureIndex = mongo_collection.createIndex; + +function mongo_collection:drop() + return self.database:runCommand("drop", self.name) +end + +-- collection:dropIndex("age_1") +-- collection:dropIndex("*") +function mongo_collection:dropIndex(indexName) + return self.database:runCommand("dropIndexes", self.name, "index", indexName) +end + -- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, }) -- keys, value type -- query, table diff --git a/test/testmongodb.lua b/test/testmongodb.lua new file mode 100644 index 00000000..f1fa81e0 --- /dev/null +++ b/test/testmongodb.lua @@ -0,0 +1,90 @@ +local skynet = require "skynet" +local mongo = require "mongo" +local bson = require "bson" + +local host, db_name = ... + +function test_insert_without_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) +end + +function test_insert_with_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 0) +end + +function test_find_and_remove() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret and ret.test_key == 1) + + db[db_name].testdb:delete({test_key = 1}) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret == nil) +end + + +function test_expire_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, }) + db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, }) + + local ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret and ret.test_key == 1) + + for i = 1, 1000 do + skynet.sleep(11); + + local ret = db[db_name].testdb:findOne({test_key = 1}) + if ret == nil then + return + end + end + + assert(false, "test expire index failed"); +end + +skynet.start(function() + test_insert_without_index() + test_insert_with_index() + test_find_and_remove() + test_expire_index() + + print("mongodb test finish."); +end) From 978d6af359785aa731886b696da2b66651d9a3e7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 11 Dec 2014 11:01:02 +0800 Subject: [PATCH 257/729] set utf-8 --- test/testmysql.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/testmysql.lua b/test/testmysql.lua index 510625e0..7bd54855 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -63,14 +63,14 @@ local function test3( db) local res = db:query("select * from cats order by id asc") print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) res = db:query("select * from cats order by id asc") - print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) + print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) skynet.sleep(1000) i=i+1 end end skynet.start(function() - local db=mysql.connect{ + local db=mysql.connect{ host="127.0.0.1", port=3306, database="skynet", @@ -83,8 +83,10 @@ skynet.start(function() end print("testmysql success to connect to mysql server") + db:query("set names utf8") + local res = db:query("drop table if exists cats") - res = db:query("create table cats " + res = db:query("create table cats " .."(id serial primary key, ".. "name varchar(5))") print( dump( res ) ) @@ -112,7 +114,7 @@ skynet.start(function() while true do local res = db:query("select * from cats order by id asc") print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) - + res = db:query("select * from cats order by id asc") print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) From 0e20fe5b4eb6028a1c5b9c788ae36df9760485dd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 15 Dec 2014 18:05:38 +0800 Subject: [PATCH 258/729] fix typo --- lualib/socket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/socket.lua b/lualib/socket.lua index c00f8b32..dd2df1c1 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -58,7 +58,7 @@ socket_message[1] = function(id, size, data) end else if s.buffer_limit and sz > s.buffer_limit then - skynet.error(string.format("socket buffer overlow: fd=%d size=%d", id , sz)) + skynet.error(string.format("socket buffer overflow: fd=%d size=%d", id , sz)) driver.clear(s.buffer,buffer_pool) driver.close(id) return From ce6fa906d3847c4b7cfc5ff0cf7631a0b385e15d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 15 Dec 2014 18:08:00 +0800 Subject: [PATCH 259/729] remove unused macro --- skynet-src/skynet_mq.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index 098cb210..ba0c61e6 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -42,8 +42,6 @@ static struct global_queue *Q = NULL; #define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} #define UNLOCK(q) __sync_lock_release(&(q)->lock); -#define GP(p) ((p) % MAX_GLOBAL_MQ) - void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; From 23587d0b0fdcc9176ec7201c38781aa7e0dfd951 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Dec 2014 13:58:32 +0800 Subject: [PATCH 260/729] make sproto parser result more stable --- lualib/sprotoparser.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index f6b24399..4728368e 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -316,6 +316,7 @@ local function packgroup(t,p) alltypes[name] = #alltypes table.insert(alltypes, name) end + table.sort(alltypes) -- make result stable tt = {} for _,name in ipairs(alltypes) do table.insert(tt, packtype(name, t[name], alltypes)) From 21cd5c9882f6d63da97d21ecbd799e49ec655150 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Dec 2014 17:39:59 +0800 Subject: [PATCH 261/729] bugfix last commit --- lualib/sprotoparser.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 4728368e..ff6960c2 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -313,10 +313,12 @@ local function packgroup(t,p) local tt, tp local alltypes = {} for name in pairs(t) do - alltypes[name] = #alltypes table.insert(alltypes, name) end table.sort(alltypes) -- make result stable + for idx, name in ipairs(alltypes) do + alltypes[name] = idx - 1 + end tt = {} for _,name in ipairs(alltypes) do table.insert(tt, packtype(name, t[name], alltypes)) From 52134f9e576fa4e7a82459b7895a7fce56e02285 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 28 Dec 2014 11:39:25 +0800 Subject: [PATCH 262/729] check c obj dirty when len, pairs and ipairs. Fix issue #219 --- lualib/sharedata/corelib.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index 06b37fd6..789e77ad 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -53,7 +53,7 @@ local function genkey(self) return key end -function meta:__index(key) +local function getcobj(self) local obj = self.__obj if isdirty(obj) then local newobj, newtbl = needupdate(self.__gcobj) @@ -67,6 +67,11 @@ function meta:__index(key) obj = self.__obj end end + return obj +end + +function meta:__index(key) + local obj = getcobj(self) local v = index(obj, key) if type(v) == "userdata" then local r = setmetatable({ @@ -83,11 +88,11 @@ function meta:__index(key) end function meta:__len() - return len(self.__obj) + return len(getcobj(self)) end local function conf_ipairs(self, index) - local obj = self.__obj + local obj = getcobj(self) index = index + 1 local value = rawget(self, index) if value then @@ -109,7 +114,8 @@ function meta:__pairs() end function conf.next(obj, key) - local nextkey = core.nextkey(obj.__obj, key) + local cobj = getcobj(obj) + local nextkey = core.nextkey(cobj, key) if nextkey then return nextkey, obj[nextkey] end From ee3f9c6ecdfbbabd5bf7ee46172f2a8ecc484181 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 12:17:57 +0800 Subject: [PATCH 263/729] Bugfix: mc.close may reference an invalid pointer. See pr #220 --- lualib-src/lua-multicast.c | 13 ++++++++----- service/multicastd.lua | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 866821d4..609169d4 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -83,7 +83,7 @@ mc_unpacklocal(lua_State *L) { if (sz != sizeof(*pack)) { return luaL_error(L, "Invalid multicast package size %d", sz); } - lua_settop(L, 1); + lua_pushlightuserdata(L, *pack); lua_pushlightuserdata(L, (*pack)->data); lua_pushunsigned(L, (*pack)->size); return 3; @@ -92,6 +92,8 @@ mc_unpacklocal(lua_State *L) { /* lightuserdata struct mc_package ** integer reference + + return mc_package * */ static int mc_bindrefer(lua_State *L) { @@ -102,16 +104,17 @@ mc_bindrefer(lua_State *L) { } (*pack)->reference = ref; - return 0; + lua_pushlightuserdata(L, *pack); + + return 1; } /* - lightuserdata struct mc_package ** + lightuserdata struct mc_package * */ static int mc_closelocal(lua_State *L) { - struct mc_package **ptr = lua_touserdata(L,1); - struct mc_package *pack = *ptr; + struct mc_package *pack = lua_touserdata(L,1); int ref = __sync_sub_and_fetch(&pack->reference, 1); if (ref <= 0) { diff --git a/service/multicastd.lua b/service/multicastd.lua index 5a7f9d2a..7bafab78 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -68,18 +68,20 @@ end local function publish(c , source, pack, size) local group = channel[c] if group == nil then - -- dead channel, delete the pack - mc.bind(pack, 1) + -- dead channel, delete the pack. mc.bind returns the pointer in pack + local pack = mc.bind(pack, 1) mc.close(pack) return end mc.bind(pack, channel_n[c]) local msg = skynet.tostring(pack, size) for k in pairs(group) do + -- the msg is a pointer to the real message, publish pointer in local is ok. skynet.redirect(k, source, "multicast", c , msg) end local remote = channel_remote[c] if remote then + -- remote publish should unpack the pack, because we should not publish the pointer out. local _, msg, sz = mc.unpack(pack, size) local msg = skynet.tostring(msg,sz) for node in pairs(remote) do From f65ecdf7673bf6e7accb969cbb6d8d301ca5f5bf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 14:06:31 +0800 Subject: [PATCH 264/729] ready for 0.9.3 --- HISTORY.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 4fefeb98..ac7426ae 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,10 @@ +v0.9.3 (2014-1-5) +----------- +* Add : mongo createIndex +* Update : sproto +* bugfix : sharedata check dirty flag when len/pairs metamethod +* bugfix : multicast + v0.9.2 (2014-12-8) ----------- * Simplify the message queue From c7b5015e104bf9f6b6a8b9c84f7237a6b72e3d50 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 16:25:42 +0800 Subject: [PATCH 265/729] update to lua 5.3, remove lua unsigned api --- 3rd/lpeg/lptree.c | 4 +- 3rd/lpeg/lptypes.h | 2 +- 3rd/lua-int64/Makefile | 6 - 3rd/lua-int64/README | 2 - 3rd/lua-int64/int64.c | 317 ------------ 3rd/lua-int64/test.lua | 17 - 3rd/lua/Makefile | 146 +++--- 3rd/lua/README | 3 - 3rd/lua/lapi.c | 571 ++++++++++------------ 3rd/lua/lapi.h | 6 +- 3rd/lua/lauxlib.c | 383 ++++++--------- 3rd/lua/lauxlib.h | 94 +++- 3rd/lua/lbaselib.c | 246 ++++++---- 3rd/lua/lbitlib.c | 78 +-- 3rd/lua/lcode.c | 259 ++++++---- 3rd/lua/lcode.h | 14 +- 3rd/lua/lcorolib.c | 33 +- 3rd/lua/lctype.c | 5 +- 3rd/lua/lctype.h | 2 +- 3rd/lua/ldblib.c | 213 +++++---- 3rd/lua/ldebug.c | 186 +++++--- 3rd/lua/ldebug.h | 16 +- 3rd/lua/ldo.c | 196 ++++---- 3rd/lua/ldo.h | 4 +- 3rd/lua/ldump.c | 316 ++++++------ 3rd/lua/lfunc.c | 178 +++---- 3rd/lua/lfunc.h | 33 +- 3rd/lua/lgc.c | 907 ++++++++++++++++------------------- 3rd/lua/lgc.h | 93 ++-- 3rd/lua/linit.c | 43 +- 3rd/lua/liolib.c | 275 +++++++---- 3rd/lua/llex.c | 282 +++++++---- 3rd/lua/llex.h | 16 +- 3rd/lua/llimits.h | 203 +++----- 3rd/lua/lmathlib.c | 367 +++++++++----- 3rd/lua/lmem.c | 26 +- 3rd/lua/lmem.h | 30 +- 3rd/lua/loadlib.c | 245 ++++++---- 3rd/lua/lobject.c | 291 ++++++++--- 3rd/lua/lobject.h | 370 ++++++-------- 3rd/lua/lopcodes.c | 23 +- 3rd/lua/lopcodes.h | 39 +- 3rd/lua/loslib.c | 91 ++-- 3rd/lua/lparser.c | 252 +++++----- 3rd/lua/lparser.h | 27 +- 3rd/lua/lprefix.h | 45 ++ 3rd/lua/lstate.c | 97 ++-- 3rd/lua/lstate.h | 147 +++--- 3rd/lua/lstring.c | 139 +++--- 3rd/lua/lstring.h | 18 +- 3rd/lua/lstrlib.c | 637 ++++++++++++++++++++----- 3rd/lua/ltable.c | 344 +++++++------ 3rd/lua/ltable.h | 20 +- 3rd/lua/ltablib.c | 226 ++++++--- 3rd/lua/ltm.c | 86 +++- 3rd/lua/ltm.h | 28 +- 3rd/lua/lua.c | 510 ++++++++++++-------- 3rd/lua/lua.h | 170 ++++--- 3rd/lua/luac.c | 66 +-- 3rd/lua/luaconf.h | 774 ++++++++++++++++++------------ 3rd/lua/lualib.h | 7 +- 3rd/lua/lundump.c | 430 +++++++++-------- 3rd/lua/lundump.h | 29 +- 3rd/lua/lutf8lib.c | 255 ++++++++++ 3rd/lua/lvm.c | 955 ++++++++++++++++++++++++------------- 3rd/lua/lvm.h | 42 +- 3rd/lua/lzio.c | 10 +- 3rd/lua/lzio.h | 10 +- Makefile | 5 +- lualib-src/lua-bson.c | 8 +- lualib-src/lua-cluster.c | 16 +- lualib-src/lua-memory.c | 4 +- lualib-src/lua-multicast.c | 12 +- lualib-src/lua-skynet.c | 10 +- lualib-src/lua-socket.c | 2 +- lualib-src/lua-stm.c | 6 +- 76 files changed, 6960 insertions(+), 5058 deletions(-) delete mode 100644 3rd/lua-int64/Makefile delete mode 100644 3rd/lua-int64/README delete mode 100644 3rd/lua-int64/int64.c delete mode 100644 3rd/lua-int64/test.lua delete mode 100644 3rd/lua/README create mode 100644 3rd/lua/lprefix.h create mode 100644 3rd/lua/lutf8lib.c diff --git a/3rd/lpeg/lptree.c b/3rd/lpeg/lptree.c index a5dfeb4a..4f68906b 100644 --- a/3rd/lpeg/lptree.c +++ b/3rd/lpeg/lptree.c @@ -524,7 +524,7 @@ static int lp_choice (lua_State *L) { */ static int lp_star (lua_State *L) { int size1; - int n = luaL_checkint(L, 2); + int n = (int)luaL_checkinteger(L, 2); TTree *tree1 = gettree(L, 1, &size1); if (n >= 0) { /* seq tree1 (seq tree1 ... (seq tree1 (rep tree1))) */ TTree *tree = newtree(L, (n + 1) * (size1 + 1)); @@ -747,7 +747,7 @@ static int lp_poscapture (lua_State *L) { static int lp_argcapture (lua_State *L) { - int n = luaL_checkint(L, 1); + int n = (int)luaL_checkinteger(L, 1); TTree *tree = newemptycap(L, Carg, 0); tree->key = n; luaL_argcheck(L, 0 < n && n <= SHRT_MAX, 1, "invalid argument index"); diff --git a/3rd/lpeg/lptypes.h b/3rd/lpeg/lptypes.h index 7ace5451..d96554d0 100644 --- a/3rd/lpeg/lptypes.h +++ b/3rd/lpeg/lptypes.h @@ -29,7 +29,7 @@ /* ** compatibility with Lua 5.2 */ -#if (LUA_VERSION_NUM == 502) +#if (LUA_VERSION_NUM >= 502) #undef lua_equal #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) diff --git a/3rd/lua-int64/Makefile b/3rd/lua-int64/Makefile deleted file mode 100644 index 82eacaa6..00000000 --- a/3rd/lua-int64/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -install : - gcc -g -Wall -fPIC --shared -o int64.so int64.c - -clean : - rm int64.so - diff --git a/3rd/lua-int64/README b/3rd/lua-int64/README deleted file mode 100644 index 7996553d..00000000 --- a/3rd/lua-int64/README +++ /dev/null @@ -1,2 +0,0 @@ -See https://github.com/cloudwu/lua-int64 -It will remove when Lua upgrade to 5.3 . diff --git a/3rd/lua-int64/int64.c b/3rd/lua-int64/int64.c deleted file mode 100644 index f261b21e..00000000 --- a/3rd/lua-int64/int64.c +++ /dev/null @@ -1,317 +0,0 @@ -#include -#include -#include -#include -#include -#include - -static int64_t -_int64(lua_State *L, int index) { - int type = lua_type(L,index); - int64_t n = 0; - switch(type) { - case LUA_TNUMBER: { - lua_Number d = lua_tonumber(L,index); - n = (int64_t)d; - break; - } - case LUA_TSTRING: { - size_t len = 0; - const uint8_t * str = (const uint8_t *)lua_tolstring(L, index, &len); - if (len>8) { - return luaL_error(L, "The string (length = %d) is not an int64 string", len); - } - int i = 0; - uint64_t n64 = 0; - for (i=0;i<(int)len;i++) { - n64 |= (uint64_t)str[i] << (i*8); - } - n = (int64_t)n64; - break; - } - case LUA_TLIGHTUSERDATA: { - void * p = lua_touserdata(L,index); - n = (intptr_t)p; - break; - } - default: - return luaL_error(L, "argument %d error type %s", index, lua_typename(L,type)); - } - return n; -} - -static inline void -_pushint64(lua_State *L, int64_t n) { - void * p = (void *)(intptr_t)n; - lua_pushlightuserdata(L,p); -} - -static int -int64_add(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - _pushint64(L, a+b); - - return 1; -} - -static int -int64_sub(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - _pushint64(L, a-b); - - return 1; -} - -static int -int64_mul(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - _pushint64(L, a * b); - - return 1; -} - -static int -int64_div(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - if (b == 0) { - return luaL_error(L, "div by zero"); - } - _pushint64(L, a / b); - - return 1; -} - -static int -int64_mod(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - if (b == 0) { - return luaL_error(L, "mod by zero"); - } - _pushint64(L, a % b); - - return 1; -} - -static int64_t -_pow64(int64_t a, int64_t b) { - if (b == 1) { - return a; - } - int64_t a2 = a * a; - if (b % 2 == 1) { - return _pow64(a2, b/2) * a; - } else { - return _pow64(a2, b/2); - } -} - -static int -int64_pow(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - int64_t p; - if (b > 0) { - p = _pow64(a,b); - } else if (b == 0) { - p = 1; - } else { - return luaL_error(L, "pow by nagtive number %d",(int)b); - } - _pushint64(L, p); - - return 1; -} - -static int -int64_unm(lua_State *L) { - int64_t a = _int64(L,1); - _pushint64(L, -a); - return 1; -} - -static int -int64_new(lua_State *L) { - int top = lua_gettop(L); - int64_t n; - switch(top) { - case 0 : - lua_pushlightuserdata(L,NULL); - break; - case 1 : - n = _int64(L,1); - _pushint64(L,n); - break; - default: { - int base = luaL_checkinteger(L,2); - if (base < 2) { - luaL_error(L, "base must be >= 2"); - } - const char * str = luaL_checkstring(L, 1); - n = strtoll(str, NULL, base); - _pushint64(L,n); - break; - } - } - return 1; -} - -static int -int64_eq(lua_State *L) { - // __eq metamethod can't be invoke by lightuserdata - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - lua_pushboolean(L,a == b); - return 1; -} - -static int -int64_lt(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - lua_pushboolean(L,a < b); - return 1; -} - -static int -int64_le(lua_State *L) { - int64_t a = _int64(L,1); - int64_t b = _int64(L,2); - lua_pushboolean(L,a <= b); - return 1; -} - -static int -int64_len(lua_State *L) { - int64_t a = _int64(L,1); - lua_pushnumber(L,(lua_Number)a); - return 1; -} - -static int -tostring(lua_State *L) { - static char hex[16] = "0123456789ABCDEF"; - uintptr_t n = (uintptr_t)lua_touserdata(L,1); - if (lua_gettop(L) == 1) { - luaL_Buffer b; - luaL_buffinit(L , &b); - luaL_addstring(&b, "int64: 0x"); - int i; - bool strip = true; - for (i=15;i>=0;i--) { - int c = (n >> (i*4)) & 0xf; - if (strip && c ==0) { - continue; - } - strip = false; - luaL_addchar(&b, hex[c]); - } - if (strip) { - luaL_addchar(&b , '0'); - } - luaL_pushresult(&b); - } else { - int base = luaL_checkinteger(L,2); - int shift =0, mask =0; - switch(base) { - case 0: { - unsigned char buffer[8]; - int i; - for (i=0;i<8;i++) { - buffer[i] = (n >> (i*8)) & 0xff; - } - lua_pushlstring(L,(const char *)buffer, 8); - return 1; - } - case 10: { - int64_t dec = (int64_t)n; - luaL_Buffer b; - luaL_buffinit(L , &b); - if (dec<0) { - luaL_addchar(&b, '-'); - dec = -dec; - } - int buffer[32]; - int i; - for (i=0;i<32;i++) { - buffer[i] = dec%10; - dec /= 10; - if (dec == 0) - break; - } - while (i>=0) { - luaL_addchar(&b, hex[buffer[i]]); - --i; - } - luaL_pushresult(&b); - return 1; - } - case 2: - shift = 1; - mask = 1; - break; - case 8: - shift = 3; - mask = 7; - break; - case 16: - shift = 4; - mask = 0xf; - break; - default: - luaL_error(L, "Unsupport base %d",base); - break; - } - int i; - char buffer[64]; - for (i=0;i<64;i+=shift) { - buffer[i/shift] = hex[(n>>(64-shift-i)) & mask]; - } - lua_pushlstring(L, buffer, 64 / shift); - } - return 1; -} - -static void -make_mt(lua_State *L) { - luaL_Reg lib[] = { - { "__add", int64_add }, - { "__sub", int64_sub }, - { "__mul", int64_mul }, - { "__div", int64_div }, - { "__mod", int64_mod }, - { "__unm", int64_unm }, - { "__pow", int64_pow }, - { "__eq", int64_eq }, - { "__lt", int64_lt }, - { "__le", int64_le }, - { "__len", int64_len }, - { "__tostring", tostring }, - { NULL, NULL }, - }; - luaL_newlib(L,lib); -} - -int -luaopen_int64(lua_State *L) { - if (sizeof(intptr_t)!=sizeof(int64_t)) { - return luaL_error(L, "Only support 64bit architecture"); - } - lua_pushlightuserdata(L,NULL); - make_mt(L); - lua_setmetatable(L,-2); - lua_pop(L,1); - - lua_newtable(L); - lua_pushcfunction(L, int64_new); - lua_setfield(L, -2, "new"); - lua_pushcfunction(L, tostring); - lua_setfield(L, -2, "tostring"); - - return 1; -} - diff --git a/3rd/lua-int64/test.lua b/3rd/lua-int64/test.lua deleted file mode 100644 index 8aedf561..00000000 --- a/3rd/lua-int64/test.lua +++ /dev/null @@ -1,17 +0,0 @@ -lib = require "int64" - -local int64 = lib.new -print(lib.tostring(int64 "\1\2\3\4\5\6\7\8")) - -a = 1 + int64(1) -b = int64 "\16" + int64("9",10) -print(lib.tostring(a,10), lib.tostring(b,2)) -print("+", a+b) -print("-", lib.tostring(a-b,10)) -print("*", a*b) -print("/", a/b) -print("%", a%b) -print("^", a^b) -print("==", a == b) -print(">", a > b) -print("#", #a) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 7b4b2b75..4be6dc13 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -6,8 +6,8 @@ # Your platform. See PLATS for possible values. PLAT= none -CC= gcc -CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS) +CC= gcc -std=c99 +CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= -lm $(SYSLIBS) $(MYLIBS) @@ -26,14 +26,14 @@ MYOBJS= # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= -PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris +PLATS= aix bsd c89 freebsd generic linux macosx mingw posix solaris LUA_A= liblua.a CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \ lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \ ltm.o lundump.o lvm.o lzio.o LIB_O= lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o \ - lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o + lmathlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o loadlib.o linit.o BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) LUA_T= lua @@ -91,12 +91,16 @@ none: aix: $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" -ansi: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_ANSI" - bsd: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" +c89: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89" + @echo '' + @echo '*** C89 does not guarantee 64-bit integers for Lua.' + @echo '' + + freebsd: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline" @@ -109,7 +113,7 @@ macosx: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc mingw: - $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \ + $(MAKE) "LUA_A=lua53.dll" "LUA_T=lua.exe" \ "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe $(MAKE) "LUAC_T=luac.exe" luac.exe @@ -118,70 +122,76 @@ posix: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" solaris: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" # list targets that do not create files (but not all makes understand .PHONY) .PHONY: all $(PLATS) default o a clean depend echo none # DO NOT DELETE -lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ - lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \ - lvm.h -lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h -lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h -lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h -lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ - lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \ - lstring.h ltable.h lvm.h -lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h -lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h -ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h -ldebug.o: ldebug.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ - ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h ldebug.h ldo.h \ - lfunc.h lstring.h lgc.h ltable.h lvm.h -ldo.o: ldo.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ - lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h \ - lstring.h ltable.h lundump.h lvm.h -ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \ - lzio.h lmem.h lundump.h -lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \ - lstate.h ltm.h lzio.h lmem.h -lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ - lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h -linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h -liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h -llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \ - lstate.h ltm.h lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h -lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h -lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ - ltm.h lzio.h lmem.h ldo.h lgc.h -loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h -lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h -lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h -loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h -lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ - lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lfunc.h \ - lstring.h lgc.h ltable.h -lstate.o: lstate.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ - ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h lstring.h \ - ltable.h -lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ - ltm.h lzio.h lstring.h lgc.h -lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h -ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ - ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h -ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ - lmem.h lstring.h lgc.h ltable.h -lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h -luac.o: luac.c lua.h luaconf.h lauxlib.h lobject.h llimits.h lstate.h \ - ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h -lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h -lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ - lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h -lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ - lzio.h +lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ + ltable.h lundump.h lvm.h +lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h +lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lbitlib.o: lbitlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lgc.h lstring.h ltable.h lvm.h +lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h +ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ + ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h +ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ + lparser.h lstring.h ltable.h lundump.h lvm.h +ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ + ltm.h lzio.h lmem.h lundump.h +lfunc.o: lfunc.c lprefix.h lua.h luaconf.h lfunc.h lobject.h llimits.h \ + lgc.h lstate.h ltm.h lzio.h lmem.h +lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h +linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h +liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldo.h \ + lobject.h lstate.h ltm.h lzio.h lmem.h lgc.h llex.h lparser.h lstring.h \ + ltable.h +lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h +loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ + ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ + lvm.h +lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h +loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lfunc.h lstring.h lgc.h ltable.h +lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ + lstring.h ltable.h +lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h +lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h +ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h +lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h lobject.h llimits.h \ + lstate.h ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h +lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ + lundump.h +lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ + ltable.h lvm.h +lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ + lobject.h ltm.h lzio.h +# (end of Makefile) diff --git a/3rd/lua/README b/3rd/lua/README deleted file mode 100644 index 9e1aea2f..00000000 --- a/3rd/lua/README +++ /dev/null @@ -1,3 +0,0 @@ -This is a modify version of lua 5.2.3 (http://www.lua.org/ftp/lua-5.2.3.tar.gz) . - -For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index a4fce7f2..fbfafa30 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1,16 +1,18 @@ /* -** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lapi.c,v 2.244 2014/12/26 14:43:45 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ +#define lapi_c +#define LUA_CORE + +#include "lprefix.h" + #include #include -#define lapi_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -43,32 +45,35 @@ const char lua_ident[] = /* test for pseudo index */ #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) +/* test for upvalue */ +#define isupvalue(i) ((i) < LUA_REGISTRYINDEX) + /* test for valid but not pseudo index */ #define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) -#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index") +#define api_checkvalidindex(o) api_check(isvalid(o), "invalid index") -#define api_checkstackindex(L, i, o) \ - api_check(L, isstackindex(i, o), "index not in the stack") +#define api_checkstackindex(i, o) \ + api_check(isstackindex(i, o), "index not in the stack") static TValue *index2addr (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { TValue *o = ci->func + idx; - api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); + api_check(idx <= ci->top - (ci->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return o; } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); return L->top + idx; } else if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; - api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); + api_check(idx <= MAXUPVAL + 1, "upvalue index too large"); if (ttislcf(ci->func)) /* light C function? */ return NONVALIDVALUE; /* it has no upvalues */ else { @@ -89,21 +94,22 @@ static void growstack (lua_State *L, void *ud) { } -LUA_API int lua_checkstack (lua_State *L, int size) { +LUA_API int lua_checkstack (lua_State *L, int n) { int res; CallInfo *ci = L->ci; lua_lock(L); - if (L->stack_last - L->top > size) /* stack large enough? */ + api_check(n >= 0, "negative 'n'"); + if (L->stack_last - L->top > n) /* stack large enough? */ res = 1; /* yes; check is OK */ else { /* no; need to grow stack */ int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; - if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */ + if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ res = 0; /* no */ else /* try to grow stack */ - res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK); + res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); } - if (res && ci->top < L->top + size) - ci->top = L->top + size; /* adjust frame top */ + if (res && ci->top < L->top + n) + ci->top = L->top + n; /* adjust frame top */ lua_unlock(L); return res; } @@ -114,8 +120,8 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { if (from == to) return; lua_lock(to); api_checknelems(from, n); - api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "not enough elements to move"); + api_check(G(from) == G(to), "moving among independent states"); + api_check(to->ci->top - to->top >= n, "not enough elements to move"); from->top -= n; for (i = 0; i < n; i++) { setobj2s(to, to->top++, from->top + i); @@ -166,68 +172,63 @@ LUA_API void lua_settop (lua_State *L, int idx) { StkId func = L->ci->func; lua_lock(L); if (idx >= 0) { - api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); + api_check(idx <= L->stack_last - (func + 1), "new top too large"); while (L->top < (func + 1) + idx) setnilvalue(L->top++); L->top = (func + 1) + idx; } else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - L->top += idx+1; /* `subtract' index (index is negative) */ + api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top"); + L->top += idx+1; /* 'subtract' index (index is negative) */ } lua_unlock(L); } -LUA_API void lua_remove (lua_State *L, int idx) { - StkId p; - lua_lock(L); - p = index2addr(L, idx); - api_checkstackindex(L, idx, p); - while (++p < L->top) setobjs2s(L, p-1, p); - L->top--; - lua_unlock(L); +/* +** Reverse the stack segment from 'from' to 'to' +** (auxiliary to 'lua_rotate') +*/ +static void reverse (lua_State *L, StkId from, StkId to) { + for (; from < to; from++, to--) { + TValue temp; + setobj(L, &temp, from); + setobjs2s(L, from, to); + setobj2s(L, to, &temp); + } } -LUA_API void lua_insert (lua_State *L, int idx) { - StkId p; - StkId q; +/* +** Let x = AB, where A is a prefix of length 'n'. Then, +** rotate x n == BA. But BA == (A^r . B^r)^r. +*/ +LUA_API void lua_rotate (lua_State *L, int idx, int n) { + StkId p, t, m; lua_lock(L); - p = index2addr(L, idx); - api_checkstackindex(L, idx, p); - for (q = L->top; q > p; q--) /* use L->top as a temporary */ - setobjs2s(L, q, q - 1); - setobjs2s(L, p, L->top); - lua_unlock(L); -} - - -static void moveto (lua_State *L, TValue *fr, int idx) { - TValue *to = index2addr(L, idx); - api_checkvalidindex(L, to); - setobj(L, to, fr); - if (idx < LUA_REGISTRYINDEX) /* function upvalue? */ - luaC_barrier(L, clCvalue(L->ci->func), fr); - /* LUA_REGISTRYINDEX does not need gc barrier - (collector revisits it before finishing collection) */ -} - - -LUA_API void lua_replace (lua_State *L, int idx) { - lua_lock(L); - api_checknelems(L, 1); - moveto(L, L->top - 1, idx); - L->top--; + t = L->top - 1; /* end of stack segment being rotated */ + p = index2addr(L, idx); /* start of segment */ + api_checkstackindex(idx, p); + api_check((n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); + m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ + reverse(L, p, m); /* reverse the prefix with length 'n' */ + reverse(L, m + 1, t); /* reverse the suffix */ + reverse(L, p, t); /* reverse the entire segment */ lua_unlock(L); } LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { - TValue *fr; + TValue *fr, *to; lua_lock(L); fr = index2addr(L, fromidx); - moveto(L, fr, toidx); + to = index2addr(L, toidx); + api_checkvalidindex(to); + setobj(L, to, fr); + if (isupvalue(toidx)) /* function upvalue? */ + luaC_barrier(L, clCvalue(L->ci->func), fr); + /* LUA_REGISTRYINDEX does not need gc barrier + (collector revisits it before finishing collection) */ lua_unlock(L); } @@ -248,12 +249,13 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) { LUA_API int lua_type (lua_State *L, int idx) { StkId o = index2addr(L, idx); - return (isvalid(o) ? ttypenv(o) : LUA_TNONE); + return (isvalid(o) ? ttnov(o) : LUA_TNONE); } LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); + api_check(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); return ttypename(t); } @@ -264,22 +266,28 @@ LUA_API int lua_iscfunction (lua_State *L, int idx) { } +LUA_API int lua_isinteger (lua_State *L, int idx) { + StkId o = index2addr(L, idx); + return ttisinteger(o); +} + + LUA_API int lua_isnumber (lua_State *L, int idx) { - TValue n; + lua_Number n; const TValue *o = index2addr(L, idx); return tonumber(o, &n); } LUA_API int lua_isstring (lua_State *L, int idx) { - int t = lua_type(L, idx); - return (t == LUA_TSTRING || t == LUA_TNUMBER); + const TValue *o = index2addr(L, idx); + return (ttisstring(o) || cvt2str(o)); } LUA_API int lua_isuserdata (lua_State *L, int idx) { const TValue *o = index2addr(L, idx); - return (ttisuserdata(o) || ttislightuserdata(o)); + return (ttisfulluserdata(o) || ttislightuserdata(o)); } @@ -291,24 +299,17 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { LUA_API void lua_arith (lua_State *L, int op) { - StkId o1; /* 1st operand */ - StkId o2; /* 2nd operand */ lua_lock(L); - if (op != LUA_OPUNM) /* all other operations expect two operands */ - api_checknelems(L, 2); - else { /* for unary minus, add fake 2nd operand */ + if (op != LUA_OPUNM && op != LUA_OPBNOT) + api_checknelems(L, 2); /* all other operations expect two operands */ + else { /* for unary operations, add fake 2nd operand */ api_checknelems(L, 1); setobjs2s(L, L->top, L->top - 1); L->top++; } - o1 = L->top - 2; - o2 = L->top - 1; - if (ttisnumber(o1) && ttisnumber(o2)) { - setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2))); - } - else - luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD)); - L->top--; + /* first operand at top - 2, second at top - 1; result go to top - 2 */ + luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2); + L->top--; /* remove second operand */ lua_unlock(L); } @@ -321,10 +322,10 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { o2 = index2addr(L, index2); if (isvalid(o1) && isvalid(o2)) { switch (op) { - case LUA_OPEQ: i = equalobj(L, o1, o2); break; + case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; - default: api_check(L, 0, "invalid option"); + default: api_check(0, "invalid option"); } } lua_unlock(L); @@ -332,51 +333,33 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { } -LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) { - TValue n; - const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - if (isnum) *isnum = 1; - return nvalue(o); - } - else { - if (isnum) *isnum = 0; - return 0; - } +LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { + size_t sz = luaO_str2num(s, L->top); + if (sz != 0) + api_incr_top(L); + return sz; } -LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { - TValue n; +LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { + lua_Number n; const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - lua_Integer res; - lua_Number num = nvalue(o); - lua_number2integer(res, num); - if (isnum) *isnum = 1; - return res; - } - else { - if (isnum) *isnum = 0; - return 0; - } + int isnum = tonumber(o, &n); + if (!isnum) + n = 0; /* call to 'tonumber' may change 'n' even if it fails */ + if (pisnum) *pisnum = isnum; + return n; } -LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { - TValue n; +LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { + lua_Integer res; const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - lua_Unsigned res; - lua_Number num = nvalue(o); - lua_number2unsigned(res, num); - if (isnum) *isnum = 1; - return res; - } - else { - if (isnum) *isnum = 0; - return 0; - } + int isnum = tointeger(o, &res); + if (!isnum) + res = 0; /* call to 'tointeger' may change 'n' even if it fails */ + if (pisnum) *pisnum = isnum; + return res; } @@ -389,14 +372,14 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { StkId o = index2addr(L, idx); if (!ttisstring(o)) { - lua_lock(L); /* `luaV_tostring' may create a new string */ - if (!luaV_tostring(L, o)) { /* conversion failed? */ + if (!cvt2str(o)) { /* not convertible? */ if (len != NULL) *len = 0; - lua_unlock(L); return NULL; } + lua_lock(L); /* 'luaO_tostring' may create a new string */ luaC_checkGC(L); o = index2addr(L, idx); /* previous call may reallocate the stack */ + luaO_tostring(L, o); lua_unlock(L); } if (len != NULL) *len = tsvalue(o)->len; @@ -406,7 +389,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { LUA_API size_t lua_rawlen (lua_State *L, int idx) { StkId o = index2addr(L, idx); - switch (ttypenv(o)) { + switch (ttnov(o)) { case LUA_TSTRING: return tsvalue(o)->len; case LUA_TUSERDATA: return uvalue(o)->len; case LUA_TTABLE: return luaH_getn(hvalue(o)); @@ -426,8 +409,8 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { LUA_API void *lua_touserdata (lua_State *L, int idx) { StkId o = index2addr(L, idx); - switch (ttypenv(o)) { - case LUA_TUSERDATA: return (rawuvalue(o) + 1); + switch (ttnov(o)) { + case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } @@ -472,9 +455,7 @@ LUA_API void lua_pushnil (lua_State *L) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); - setnvalue(L->top, n); - luai_checknum(L, L->top, - luaG_runerror(L, "C API - attempt to push a signaling NaN")); + setfltvalue(L->top, n); api_incr_top(L); lua_unlock(L); } @@ -482,17 +463,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { lua_lock(L); - setnvalue(L->top, cast_num(n)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { - lua_Number n; - lua_lock(L); - n = lua_unsigned2number(u); - setnvalue(L->top, n); + setivalue(L->top, n); api_incr_top(L); lua_unlock(L); } @@ -558,15 +529,17 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { setfvalue(L->top, fn); } else { - Closure *cl; + CClosure *cl; api_checknelems(L, n); - api_check(L, n <= MAXUPVAL, "upvalue index too large"); + api_check(n <= MAXUPVAL, "upvalue index too large"); luaC_checkGC(L); cl = luaF_newCclosure(L, n); - cl->c.f = fn; + cl->f = fn; L->top -= n; - while (n--) - setobj2n(L, &cl->c.upvalue[n], L->top + n); + while (n--) { + setobj2n(L, &cl->upvalue[n], L->top + n); + /* does not need barrier because closure is white */ + } setclCvalue(L, L->top, cl); } api_incr_top(L); @@ -605,27 +578,29 @@ LUA_API int lua_pushthread (lua_State *L) { */ -LUA_API void lua_getglobal (lua_State *L, const char *var) { +LUA_API int lua_getglobal (lua_State *L, const char *name) { Table *reg = hvalue(&G(L)->l_registry); const TValue *gt; /* global table */ lua_lock(L); gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, var)); + setsvalue2s(L, L->top++, luaS_new(L, name)); luaV_gettable(L, gt, L->top - 1, L->top - 1); lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_gettable (lua_State *L, int idx) { +LUA_API int lua_gettable (lua_State *L, int idx) { StkId t; lua_lock(L); t = index2addr(L, idx); luaV_gettable(L, t, L->top - 1, L->top - 1); lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { +LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { StkId t; lua_lock(L); t = index2addr(L, idx); @@ -633,40 +608,56 @@ LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { api_incr_top(L); luaV_gettable(L, t, L->top - 1, L->top - 1); lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_rawget (lua_State *L, int idx) { +LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { StkId t; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + setivalue(L->top, n); + api_incr_top(L); + luaV_gettable(L, t, L->top - 1, L->top - 1); + lua_unlock(L); + return ttnov(L->top - 1); +} + + +LUA_API int lua_rawget (lua_State *L, int idx) { + StkId t; + lua_lock(L); + t = index2addr(L, idx); + api_check(ttistable(t), "table expected"); setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { +LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { StkId t; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + api_check(ttistable(t), "table expected"); setobj2s(L, L->top, luaH_getint(hvalue(t), n)); api_incr_top(L); lua_unlock(L); + return ttnov(L->top - 1); } -LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) { +LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { StkId t; TValue k; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + api_check(ttistable(t), "table expected"); setpvalue(&k, cast(void *, p)); setobj2s(L, L->top, luaH_get(hvalue(t), &k)); api_incr_top(L); lua_unlock(L); + return ttnov(L->top - 1); } @@ -685,11 +676,11 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { LUA_API int lua_getmetatable (lua_State *L, int objindex) { const TValue *obj; - Table *mt = NULL; - int res; + Table *mt; + int res = 0; lua_lock(L); obj = index2addr(L, objindex); - switch (ttypenv(obj)) { + switch (ttnov(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; break; @@ -697,12 +688,10 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { mt = uvalue(obj)->metatable; break; default: - mt = G(L)->mt[ttypenv(obj)]; + mt = G(L)->mt[ttnov(obj)]; break; } - if (mt == NULL) - res = 0; - else { + if (mt != NULL) { sethvalue(L, L->top, mt); api_incr_top(L); res = 1; @@ -712,17 +701,15 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { } -LUA_API void lua_getuservalue (lua_State *L, int idx) { +LUA_API int lua_getuservalue (lua_State *L, int idx) { StkId o; lua_lock(L); o = index2addr(L, idx); - api_check(L, ttisuserdata(o), "userdata expected"); - if (uvalue(o)->env) { - sethvalue(L, L->top, uvalue(o)->env); - } else - setnilvalue(L->top); + api_check(ttisfulluserdata(o), "full userdata expected"); + getuservalue(L, uvalue(o), L->top); api_incr_top(L); lua_unlock(L); + return ttnov(L->top - 1); } @@ -731,13 +718,13 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) { */ -LUA_API void lua_setglobal (lua_State *L, const char *var) { +LUA_API void lua_setglobal (lua_State *L, const char *name) { Table *reg = hvalue(&G(L)->l_registry); const TValue *gt; /* global table */ lua_lock(L); api_checknelems(L, 1); gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, var)); + setsvalue2s(L, L->top++, luaS_new(L, name)); luaV_settable(L, gt, L->top - 1, L->top - 2); L->top -= 2; /* pop value and key */ lua_unlock(L); @@ -767,43 +754,61 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { } -LUA_API void lua_rawset (lua_State *L, int idx) { +LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { StkId t; lua_lock(L); - api_checknelems(L, 2); + api_checknelems(L, 1); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); - invalidateTMcache(hvalue(t)); - luaC_barrierback(L, gcvalue(t), L->top-1); + setivalue(L->top++, n); + luaV_settable(L, t, L->top - 1, L->top - 2); + L->top -= 2; /* pop value and key */ + lua_unlock(L); +} + + +LUA_API void lua_rawset (lua_State *L, int idx) { + StkId o; + Table *t; + lua_lock(L); + api_checknelems(L, 2); + o = index2addr(L, idx); + api_check(ttistable(o), "table expected"); + t = hvalue(o); + setobj2t(L, luaH_set(L, t, L->top-2), L->top-1); + invalidateTMcache(t); + luaC_barrierback(L, t, L->top-1); L->top -= 2; lua_unlock(L); } -LUA_API void lua_rawseti (lua_State *L, int idx, int n) { - StkId t; +LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { + StkId o; + Table *t; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - luaH_setint(L, hvalue(t), n, L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top-1); + o = index2addr(L, idx); + api_check(ttistable(o), "table expected"); + t = hvalue(o); + luaH_setint(L, t, n, L->top - 1); + luaC_barrierback(L, t, L->top-1); L->top--; lua_unlock(L); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - StkId t; + StkId o; + Table *t; TValue k; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + o = index2addr(L, idx); + api_check(ttistable(o), "table expected"); + t = hvalue(o); setpvalue(&k, cast(void *, p)); - setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top - 1); + setobj2t(L, luaH_set(L, t, &k), L->top - 1); + luaC_barrierback(L, t, L->top - 1); L->top--; lua_unlock(L); } @@ -818,14 +823,14 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { if (ttisnil(L->top - 1)) mt = NULL; else { - api_check(L, ttistable(L->top - 1), "table expected"); + api_check(ttistable(L->top - 1), "table expected"); mt = hvalue(L->top - 1); } - switch (ttypenv(obj)) { + switch (ttnov(obj)) { case LUA_TTABLE: { hvalue(obj)->metatable = mt; if (mt) { - luaC_objbarrierback(L, gcvalue(obj), mt); + luaC_objbarrier(L, gcvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; @@ -833,13 +838,13 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { case LUA_TUSERDATA: { uvalue(obj)->metatable = mt; if (mt) { - luaC_objbarrier(L, rawuvalue(obj), mt); + luaC_objbarrier(L, uvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; } default: { - G(L)->mt[ttypenv(obj)] = mt; + G(L)->mt[ttnov(obj)] = mt; break; } } @@ -854,46 +859,32 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); - api_check(L, ttisuserdata(o), "userdata expected"); - if (ttisnil(L->top - 1)) - uvalue(o)->env = NULL; - else { - api_check(L, ttistable(L->top - 1), "table expected"); - uvalue(o)->env = hvalue(L->top - 1); - luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); - } + api_check(ttisfulluserdata(o), "full userdata expected"); + setuservalue(L, uvalue(o), L->top - 1); + luaC_barrier(L, gcvalue(o), L->top - 1); L->top--; lua_unlock(L); } /* -** `load' and `call' functions (run Lua code) +** 'load' and 'call' functions (run Lua code) */ #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ + api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ "results from function overflow current stack size") -LUA_API int lua_getctx (lua_State *L, int *ctx) { - if (L->ci->callstatus & CIST_YIELDED) { - if (ctx) *ctx = L->ci->u.c.ctx; - return L->ci->u.c.status; - } - else return LUA_OK; -} - - -LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, - lua_CFunction k) { +LUA_API void lua_callk (lua_State *L, int nargs, int nresults, + lua_KContext ctx, lua_KFunction k) { StkId func; lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), + api_check(k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); + api_check(L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); func = L->top - (nargs+1); if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ @@ -912,7 +903,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, /* ** Execute a protected call. */ -struct CallS { /* data to `f_call' */ +struct CallS { /* data to 'f_call' */ StkId func; int nresults; }; @@ -926,21 +917,21 @@ static void f_call (lua_State *L, void *ud) { LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, - int ctx, lua_CFunction k) { + lua_KContext ctx, lua_KFunction k) { struct CallS c; int status; ptrdiff_t func; lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), + api_check(k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); + api_check(L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) func = 0; else { StkId o = index2addr(L, errfunc); - api_checkstackindex(L, errfunc, o); + api_checkstackindex(errfunc, o); func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ @@ -954,11 +945,10 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ ci->extra = savestack(L, c.func); - ci->u.c.old_allowhook = L->allowhook; ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; - /* mark that function may do error recovery */ - ci->callstatus |= CIST_YPCALL; + setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ + ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ luaD_call(L, c.func, nresults, 1); /* do the call */ ci->callstatus &= ~CIST_YPCALL; L->errfunc = ci->u.c.old_errfunc; @@ -980,85 +970,28 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, status = luaD_protectedparser(L, &z, chunkname, mode); if (status == LUA_OK) { /* no errors? */ LClosure *f = clLvalue(L->top - 1); /* get newly created function */ - if (f->nupvalues == 1) { /* does it have one upvalue? */ + if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ Table *reg = hvalue(&G(L)->l_registry); const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v, gt); - luaC_barrier(L, f->upvals[0], gt); + luaC_upvalbarrier(L, f->upvals[0]); } } lua_unlock(L); return status; } -static Proto * -cloneproto (lua_State *L, const Proto *src) { - /* copy constants and nested proto */ - int i,n; - Proto *f = luaF_newproto (L, src->sp); - n = src->sp->sizek; - f->k=luaM_newvector(L,n,TValue); - for (i=0; ik[i]); - for (i=0; ik[i]; - TValue *o=&f->k[i]; - if (ttisstring(s)) { - TString * str = luaS_newlstr(L,svalue(s),tsvalue(s)->len); - setsvalue2n(L,o,str); - } else { - setobj(L,o,s); - } - } - n = src->sp->sizep; - f->p=luaM_newvector(L,n,struct Proto *); - for (i=0; ip[i]=NULL; - for (i=0; ip[i]=cloneproto(L, src->p[i]); - } - return f; -} -LUA_API void lua_clonefunction (lua_State *L, const void * fp) { - int i; - Closure *cl; - const LClosure *f = cast(const LClosure *, fp); - lua_lock(L); - if (f->p->sp->l_G == G(L)) { - setclLvalue(L,L->top,f); - api_incr_top(L); - lua_unlock(L); - return; - } - cl = luaF_newLclosure(L,f->nupvalues); - cl->l.p = cloneproto(L, f->p); - setclLvalue(L,L->top,cl); - api_incr_top(L); - for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */ - UpVal *up = luaF_newupval(L); - cl->l.upvals[i] = up; - luaC_objbarrier(L, cl, up); - } - if (f->nupvalues == 1) { /* does it have one upvalue? */ - /* get global table from registry */ - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - /* set global table as 1st upvalue of 'cl' (may be LUA_ENV) */ - setobj(L, cl->l.upvals[0]->v, gt); - luaC_barrier(L, cl->l.upvals[0], gt); - } - lua_unlock(L); -} - -LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { +LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; TValue *o; lua_lock(L); api_checknelems(L, 1); o = L->top - 1; if (isLfunction(o)) - status = luaU_dump(L, getproto(o), writer, data, 0); + status = luaU_dump(L, getproto(o), writer, data, strip); else status = 1; lua_unlock(L); @@ -1104,19 +1037,21 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSTEP: { - if (g->gckind == KGC_GEN) { /* generational mode? */ - res = (g->GCestimate == 0); /* true if it will do major collection */ - luaC_forcestep(L); /* do a single step */ + l_mem debt = 1; /* =1 to signal that it did an actual step */ + int oldrunning = g->gcrunning; + g->gcrunning = 1; /* allow GC to run */ + if (data == 0) { + luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ + luaC_step(L); } - else { - lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE; - if (g->gcrunning) - debt += g->GCdebt; /* include current debt */ - luaE_setdebt(g, debt); - luaC_forcestep(L); - if (g->gcstate == GCSpause) /* end of cycle? */ - res = 1; /* signal it */ + else { /* add 'data' to total debt */ + debt = cast(l_mem, data) * 1024 + g->GCdebt; + luaE_setdebt(g, debt); + luaC_checkGC(L); } + g->gcrunning = oldrunning; /* restore previous state */ + if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ + res = 1; /* signal it */ break; } case LUA_GCSETPAUSE: { @@ -1124,13 +1059,9 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { g->gcpause = data; break; } - case LUA_GCSETMAJORINC: { - res = g->gcmajorinc; - g->gcmajorinc = data; - break; - } case LUA_GCSETSTEPMUL: { res = g->gcstepmul; + if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ g->gcstepmul = data; break; } @@ -1138,14 +1069,6 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { res = g->gcrunning; break; } - case LUA_GCGEN: { /* change collector to generational mode */ - luaC_changemode(L, KGC_GEN); - break; - } - case LUA_GCINC: { /* change collector to incremental mode */ - luaC_changemode(L, KGC_NORMAL); - break; - } default: res = -1; /* invalid option */ } lua_unlock(L); @@ -1173,7 +1096,7 @@ LUA_API int lua_next (lua_State *L, int idx) { int more; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + api_check(ttistable(t), "table expected"); more = luaH_next(L, hvalue(t), L->top - 1); if (more) { api_incr_top(L); @@ -1233,34 +1156,34 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { Udata *u; lua_lock(L); luaC_checkGC(L); - u = luaS_newudata(L, size, NULL); + u = luaS_newudata(L, size); setuvalue(L, L->top, u); api_incr_top(L); lua_unlock(L); - return u + 1; + return getudatamem(u); } static const char *aux_upvalue (StkId fi, int n, TValue **val, - GCObject **owner) { + CClosure **owner, UpVal **uv) { switch (ttype(fi)) { case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (!(1 <= n && n <= f->nupvalues)) return NULL; *val = &f->upvalue[n-1]; - if (owner) *owner = obj2gco(f); + if (owner) *owner = f; return ""; } case LUA_TLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; - SharedProto *p = f->p->sp; + Proto *p = f->p; if (!(1 <= n && n <= p->sizeupvalues)) return NULL; *val = f->upvals[n-1]->v; - if (owner) *owner = obj2gco(f->upvals[n - 1]); + if (uv) *uv = f->upvals[n - 1]; name = p->upvalues[n-1].name; - return (name == NULL) ? "" : getstr(name); + return (name == NULL) ? "(*no name)" : getstr(name); } default: return NULL; /* not a closure */ } @@ -1271,7 +1194,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ lua_lock(L); - name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL); + name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL); if (name) { setobj2s(L, L->top, val); api_incr_top(L); @@ -1284,16 +1207,18 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ - GCObject *owner = NULL; /* to avoid warnings */ + CClosure *owner = NULL; + UpVal *uv = NULL; StkId fi; lua_lock(L); fi = index2addr(L, funcindex); api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val, &owner); + name = aux_upvalue(fi, n, &val, &owner, &uv); if (name) { L->top--; setobj(L, val, L->top); - luaC_barrier(L, owner, L->top); + if (owner) { luaC_barrier(L, owner, L->top); } + else if (uv) { luaC_upvalbarrier(L, uv); } } lua_unlock(L); return name; @@ -1303,9 +1228,9 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LClosure *f; StkId fi = index2addr(L, fidx); - api_check(L, ttisLclosure(fi), "Lua function expected"); + api_check(ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); + api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; return &f->upvals[n - 1]; /* get its upvalue pointer */ } @@ -1319,11 +1244,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { } case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); + api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index"); return &f->upvalue[n - 1]; } default: { - api_check(L, 0, "closure expected"); + api_check(0, "closure expected"); return NULL; } } @@ -1335,7 +1260,11 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, LClosure *f1; UpVal **up1 = getupvalref(L, fidx1, n1, &f1); UpVal **up2 = getupvalref(L, fidx2, n2, NULL); + luaC_upvdeccount(L, *up1); *up1 = *up2; - luaC_objbarrier(L, f1, *up2); + (*up1)->refcount++; + if (upisopen(*up1)) (*up1)->u.open.touched = 1; + luaC_upvalbarrier(L, *up1); } + diff --git a/3rd/lua/lapi.h b/3rd/lua/lapi.h index c7d34ad8..092f5e97 100644 --- a/3rd/lua/lapi.h +++ b/3rd/lua/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 roberto Exp $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ @@ -11,13 +11,13 @@ #include "llimits.h" #include "lstate.h" -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ +#define api_incr_top(L) {L->top++; api_check(L->top <= L->ci->top, \ "stack overflow");} #define adjustresults(L,nres) \ { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ +#define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \ "not enough elements in the stack") diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 03975d2c..1c41d6a8 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,9 +1,14 @@ /* -** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lauxlib.c,v 1.279 2014/12/14 18:32:26 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ +#define lauxlib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include @@ -16,9 +21,6 @@ ** Any function declared here could be written as an application function. */ -#define lauxlib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -64,11 +66,20 @@ static int findfield (lua_State *L, int objidx, int level) { } +/* +** Search for a name for a function in all loaded modules +** (registry._LOADED). +*/ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ - lua_pushglobaltable(L); + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); if (findfield(L, top + 1, 2)) { + const char *name = lua_tostring(L, -1); + if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ + lua_pushstring(L, name + 3); /* push name without prefix */ + lua_remove(L, -2); /* remove original name */ + } lua_copy(L, -1, top + 1); /* move name to proper place */ lua_pop(L, 2); /* remove pushed values */ return 1; @@ -81,20 +92,18 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { static void pushfuncname (lua_State *L, lua_Debug *ar) { - if (*ar->namewhat != '\0') /* is there a name? */ - lua_pushfstring(L, "function " LUA_QS, ar->name); + if (pushglobalfuncname(L, ar)) { /* try first a global name */ + lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); + lua_remove(L, -2); /* remove name */ + } + else if (*ar->namewhat != '\0') /* is there a name from code? */ + lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ else if (*ar->what == 'm') /* main? */ lua_pushliteral(L, "main chunk"); - else if (*ar->what == 'C') { - if (pushglobalfuncname(L, ar)) { - lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else - lua_pushliteral(L, "?"); - } - else + else if (*ar->what != 'C') /* for Lua functions, use */ lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); + else /* nothing left... */ + lua_pushliteral(L, "?"); } @@ -150,33 +159,40 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, ** ======================================================= */ -LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { +LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { lua_Debug ar; if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ - return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); + return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); lua_getinfo(L, "n", &ar); if (strcmp(ar.namewhat, "method") == 0) { - narg--; /* do not count `self' */ - if (narg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling " LUA_QS " on bad self (%s)", + arg--; /* do not count 'self' */ + if (arg == 0) /* error is in the self argument itself? */ + return luaL_error(L, "calling '%s' on bad self (%s)", ar.name, extramsg); } if (ar.name == NULL) ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; - return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", - narg, ar.name, extramsg); + return luaL_error(L, "bad argument #%d to '%s' (%s)", + arg, ar.name, extramsg); } -static int typeerror (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); +static int typeerror (lua_State *L, int arg, const char *tname) { + const char *msg; + const char *typearg; /* name for the type of the actual argument */ + if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) + typearg = lua_tostring(L, -1); /* use the given type name */ + else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) + typearg = "light userdata"; /* special name for messages */ + else + typearg = luaL_typename(L, arg); /* standard name */ + msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); + return luaL_argerror(L, arg, msg); } -static void tag_error (lua_State *L, int narg, int tag) { - typeerror(L, narg, lua_typename(L, tag)); +static void tag_error (lua_State *L, int arg, int tag) { + typeerror(L, arg, lua_typename(L, tag)); } @@ -222,7 +238,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { } -#if !defined(inspectstat) /* { */ +#if !defined(l_inspectstat) /* { */ #if defined(LUA_USE_POSIX) @@ -231,13 +247,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { /* ** use appropriate macros to interpret 'pclose' return status */ -#define inspectstat(stat,what) \ +#define l_inspectstat(stat,what) \ if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } #else -#define inspectstat(stat,what) /* no op */ +#define l_inspectstat(stat,what) /* no op */ #endif @@ -249,7 +265,7 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { if (stat == -1) /* error? */ return luaL_fileresult(L, 0, NULL); else { - inspectstat(stat, what); /* interpret result */ + l_inspectstat(stat, what); /* interpret result */ if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); else @@ -270,11 +286,12 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { */ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { - luaL_getmetatable(L, tname); /* try to get metatable */ - if (!lua_isnil(L, -1)) /* name already in use? */ + if (luaL_getmetatable(L, tname)) /* name already in use? */ return 0; /* leave previous value on top, but return 0 */ lua_pop(L, 1); lua_newtable(L); /* create metatable */ + lua_pushstring(L, tname); + lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ return 1; @@ -317,16 +334,16 @@ LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { ** ======================================================= */ -LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, +LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, const char *const lst[]) { - const char *name = (def) ? luaL_optstring(L, narg, def) : - luaL_checkstring(L, narg); + const char *name = (def) ? luaL_optstring(L, arg, def) : + luaL_checkstring(L, arg); int i; for (i=0; lst[i]; i++) if (strcmp(lst[i], name) == 0) return i; - return luaL_argerror(L, narg, - lua_pushfstring(L, "invalid option " LUA_QS, name)); + return luaL_argerror(L, arg, + lua_pushfstring(L, "invalid option '%s'", name)); } @@ -342,77 +359,71 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { } -LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { - if (lua_type(L, narg) != t) - tag_error(L, narg, t); +LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { + if (lua_type(L, arg) != t) + tag_error(L, arg, t); } -LUALIB_API void luaL_checkany (lua_State *L, int narg) { - if (lua_type(L, narg) == LUA_TNONE) - luaL_argerror(L, narg, "value expected"); +LUALIB_API void luaL_checkany (lua_State *L, int arg) { + if (lua_type(L, arg) == LUA_TNONE) + luaL_argerror(L, arg, "value expected"); } -LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { - const char *s = lua_tolstring(L, narg, len); - if (!s) tag_error(L, narg, LUA_TSTRING); +LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { + const char *s = lua_tolstring(L, arg, len); + if (!s) tag_error(L, arg, LUA_TSTRING); return s; } -LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, +LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, const char *def, size_t *len) { - if (lua_isnoneornil(L, narg)) { + if (lua_isnoneornil(L, arg)) { if (len) *len = (def ? strlen(def) : 0); return def; } - else return luaL_checklstring(L, narg, len); + else return luaL_checklstring(L, arg, len); } -LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { int isnum; - lua_Number d = lua_tonumberx(L, narg, &isnum); + lua_Number d = lua_tonumberx(L, arg, &isnum); if (!isnum) - tag_error(L, narg, LUA_TNUMBER); + tag_error(L, arg, LUA_TNUMBER); return d; } -LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { - return luaL_opt(L, luaL_checknumber, narg, def); +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { + return luaL_opt(L, luaL_checknumber, arg, def); } -LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { +static void interror (lua_State *L, int arg) { + if (lua_isnumber(L, arg)) + luaL_argerror(L, arg, "number has no integer representation"); + else + tag_error(L, arg, LUA_TNUMBER); +} + + +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { int isnum; - lua_Integer d = lua_tointegerx(L, narg, &isnum); - if (!isnum) - tag_error(L, narg, LUA_TNUMBER); + lua_Integer d = lua_tointegerx(L, arg, &isnum); + if (!isnum) { + interror(L, arg); + } return d; } -LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) { - int isnum; - lua_Unsigned d = lua_tounsignedx(L, narg, &isnum); - if (!isnum) - tag_error(L, narg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, lua_Integer def) { - return luaL_opt(L, luaL_checkinteger, narg, def); -} - - -LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg, - lua_Unsigned def) { - return luaL_opt(L, luaL_checkunsigned, narg, def); + return luaL_opt(L, luaL_checkinteger, arg, def); } /* }====================================================== */ @@ -523,7 +534,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; if (lua_isnil(L, -1)) { lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* `nil' has a unique fixed reference */ + return LUA_REFNIL; /* 'nil' has a unique fixed reference */ } t = lua_absindex(L, t); lua_rawgeti(L, t, freelist); /* get first free element */ @@ -562,7 +573,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { typedef struct LoadF { int n; /* number of pre-read characters */ FILE *f; /* file being read */ - char buff[LUAL_BUFFERSIZE]; /* area for reading file */ + char buff[BUFSIZ]; /* area for reading file */ } LoadF; @@ -627,7 +638,7 @@ static int skipcomment (LoadF *lf, int *cp) { } -static int luaL_loadfilex_ (lua_State *L, const char *filename, +LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { LoadF lf; int status, readstatus; @@ -655,7 +666,7 @@ static int luaL_loadfilex_ (lua_State *L, const char *filename, readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { - lua_settop(L, fnameindex); /* ignore results from `lua_load' */ + lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); @@ -698,23 +709,23 @@ LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { if (!lua_getmetatable(L, obj)) /* no metatable? */ - return 0; - lua_pushstring(L, event); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { - lua_pop(L, 2); /* remove metatable and metafield */ - return 0; - } + return LUA_TNIL; else { - lua_remove(L, -2); /* remove only metatable */ - return 1; + int tt; + lua_pushstring(L, event); + tt = lua_rawget(L, -2); + if (tt == LUA_TNIL) /* is metafield nil? */ + lua_pop(L, 2); /* remove metatable and metafield */ + else + lua_remove(L, -2); /* remove only metatable */ + return tt; /* return metafield type */ } } LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { obj = lua_absindex(L, obj); - if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ + if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ return 0; lua_pushvalue(L, obj); lua_call(L, 1, 1); @@ -722,13 +733,13 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { } -LUALIB_API int luaL_len (lua_State *L, int idx) { - int l; +LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { + lua_Integer l; int isnum; lua_len(L, idx); - l = (int)lua_tointegerx(L, -1, &isnum); + l = lua_tointegerx(L, -1, &isnum); if (!isnum) - luaL_error(L, "object length is not a number"); + luaL_error(L, "object length is not an integer"); lua_pop(L, 1); /* remove object */ return l; } @@ -737,7 +748,13 @@ LUALIB_API int luaL_len (lua_State *L, int idx) { LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ switch (lua_type(L, idx)) { - case LUA_TNUMBER: + case LUA_TNUMBER: { + if (lua_isinteger(L, idx)) + lua_pushfstring(L, "%I", lua_tointeger(L, idx)); + else + lua_pushfstring(L, "%f", lua_tonumber(L, idx)); + break; + } case LUA_TSTRING: lua_pushvalue(L, idx); break; @@ -772,8 +789,7 @@ static const char *luaL_findtable (lua_State *L, int idx, e = strchr(fname, '.'); if (e == NULL) e = fname + strlen(fname); lua_pushlstring(L, fname, e - fname); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { /* no such field? */ + if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */ lua_pop(L, 1); /* remove this nil */ lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ lua_pushlstring(L, fname, e - fname); @@ -810,13 +826,12 @@ static int libsize (const luaL_Reg *l) { LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, int sizehint) { luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ - lua_getfield(L, -1, modname); /* get _LOADED[modname] */ - if (!lua_istable(L, -1)) { /* not found? */ + if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */ lua_pop(L, 1); /* remove previous result */ /* try global variable (and create one if it does not exist) */ lua_pushglobaltable(L); if (luaL_findtable(L, 0, modname, sizehint) != NULL) - luaL_error(L, "name conflict for module " LUA_QS, modname); + luaL_error(L, "name conflict for module '%s'", modname); lua_pushvalue(L, -1); lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ } @@ -846,7 +861,6 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, ** Returns with only the table at the stack. */ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkversion(L); luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; @@ -864,8 +878,8 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { ** into the stack */ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { - lua_getfield(L, idx, fname); - if (lua_istable(L, -1)) return 1; /* table already there */ + if (lua_getfield(L, idx, fname) == LUA_TTABLE) + return 1; /* table already there */ else { lua_pop(L, 1); /* remove previous result */ idx = lua_absindex(L, idx); @@ -878,22 +892,26 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { /* -** stripped-down 'require'. Calls 'openf' to open a module, -** registers the result in 'package.loaded' table and, if 'glb' -** is true, also registers the result in the global table. +** Stripped-down 'require': After checking "loaded" table, calls 'openf' +** to open a module, registers the result in 'package.loaded' table and, +** if 'glb' is true, also registers the result in the global table. ** Leaves resulting module on the top. */ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) { - lua_pushcfunction(L, openf); - lua_pushstring(L, modname); /* argument to open function */ - lua_call(L, 1, 1); /* open module */ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_pushvalue(L, -2); /* make copy of module (call result) */ - lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ - lua_pop(L, 1); /* remove _LOADED table */ + lua_getfield(L, -1, modname); /* _LOADED[modname] */ + if (!lua_toboolean(L, -1)) { /* package not already loaded? */ + lua_pop(L, 1); /* remove field */ + lua_pushcfunction(L, openf); + lua_pushstring(L, modname); /* argument to open function */ + lua_call(L, 1, 1); /* call 'openf' to open module */ + lua_pushvalue(L, -1); /* make copy of module (call result) */ + lua_setfield(L, -3, modname); /* _LOADED[modname] = module */ + } + lua_remove(L, -2); /* remove _LOADED table */ if (glb) { - lua_pushvalue(L, -1); /* copy of 'mod' */ + lua_pushvalue(L, -1); /* copy of module */ lua_setglobal(L, modname); /* _G[modname] = module */ } } @@ -908,7 +926,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, while ((wild = strstr(s, p)) != NULL) { luaL_addlstring(&b, s, wild - s); /* push prefix */ luaL_addstring(&b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after `p' */ + s = wild + l; /* continue after 'p' */ } luaL_addstring(&b, s); /* push last suffix */ luaL_pushresult(&b); @@ -928,8 +946,8 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { - luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); + lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", + lua_tostring(L, -1)); return 0; /* return to Lua to abort */ } @@ -941,137 +959,14 @@ LUALIB_API lua_State *luaL_newstate (void) { } -LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) { +LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { const lua_Number *v = lua_version(L); + if (sz != LUAL_NUMSIZES) /* check numeric types */ + luaL_error(L, "core and library have incompatible numeric types"); if (v != lua_version(NULL)) luaL_error(L, "multiple Lua VMs detected"); else if (*v != ver) luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", ver, *v); - /* check conversions number -> integer types */ - lua_pushnumber(L, -(lua_Number)0x1234); - if (lua_tointeger(L, -1) != -0x1234 || - lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234) - luaL_error(L, "bad conversion number->int;" - " must recompile Lua with proper settings"); - lua_pop(L, 1); } -// use clonefunction - -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - -struct codecache { - int lock; - lua_State *L; -}; - -static struct codecache CC = { 0 , NULL }; - -static void -clearcache() { - if (CC.L == NULL) - return; - LOCK(&CC) - lua_close(CC.L); - CC.L = luaL_newstate(); - UNLOCK(&CC) -} - -static void -init() { - CC.lock = 0; - CC.L = luaL_newstate(); -} - -static const void * -load(const char *key) { - if (CC.L == NULL) - return NULL; - LOCK(&CC) - lua_State *L = CC.L; - lua_pushstring(L, key); - lua_rawget(L, LUA_REGISTRYINDEX); - const void * result = lua_touserdata(L, -1); - lua_pop(L, 1); - UNLOCK(&CC) - - return result; -} - -static const void * -save(const char *key, const void * proto) { - lua_State *L; - const void * result = NULL; - - LOCK(&CC) - if (CC.L == NULL) { - init(); - L = CC.L; - } else { - L = CC.L; - lua_pushstring(L, key); - lua_pushvalue(L, -1); - lua_rawget(L, LUA_REGISTRYINDEX); - result = lua_touserdata(L, -1); /* stack: key oldvalue */ - if (result == NULL) { - lua_pop(L,1); - lua_pushlightuserdata(L, (void *)proto); - lua_rawset(L, LUA_REGISTRYINDEX); - } else { - lua_pop(L,2); - } - } - UNLOCK(&CC) - return result; -} - -LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, - const char *mode) { - const void * proto = load(filename); - if (proto) { - lua_clonefunction(L, proto); - return LUA_OK; - } - lua_State * eL = luaL_newstate(); - if (eL == NULL) { - lua_pushliteral(L, "New state failed"); - return LUA_ERRMEM; - } - int err = luaL_loadfilex_(eL, filename, mode); - if (err != LUA_OK) { - size_t sz = 0; - const char * msg = lua_tolstring(eL, -1, &sz); - lua_pushlstring(L, msg, sz); - lua_close(eL); - return err; - } - proto = lua_topointer(eL, -1); - const void * oldv = save(filename, proto); - if (oldv) { - lua_close(eL); - lua_clonefunction(L, oldv); - } else { - lua_clonefunction(L, proto); - /* Never close it. notice: memory leak */ - } - return LUA_OK; -} - -static int -cache_clear(lua_State *L) { - clearcache(); - return 0; -} - -LUAMOD_API int luaopen_cache(lua_State *L) { - luaL_Reg l[] = { - { "clear", cache_clear }, - { NULL, NULL }, - }; - luaL_newlib(L,l); - lua_getglobal(L, "loadfile"); - lua_setfield(L, -2, "loadfile"); - return 1; -} diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 0fb023b8..0bac2467 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -16,7 +16,7 @@ -/* extra error code for `luaL_load' */ +/* extra error code for 'luaL_load' */ #define LUA_ERRFILE (LUA_ERRERR+1) @@ -26,30 +26,30 @@ typedef struct luaL_Reg { } luaL_Reg; -LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); -#define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) +#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) + +LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); +#define luaL_checkversion(L) \ + luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); -LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, +LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, lua_Integer def); -LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); -LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, - lua_Unsigned def); LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int narg); +LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int arg); LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); @@ -59,7 +59,7 @@ LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); LUALIB_API void (luaL_where) (lua_State *L, int lvl); LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); -LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, +LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, const char *const lst[]); LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); @@ -83,7 +83,7 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); -LUALIB_API int (luaL_len) (lua_State *L, int idx); +LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r); @@ -108,16 +108,13 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) -#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) +#define luaL_newlib(L,l) \ + (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) -#define luaL_argcheck(L, cond,numarg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_argcheck(L, cond,arg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) @@ -207,6 +204,53 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, #endif +/* +** {================================================================== +** "Abstraction Layer" for basic report of messages and errors +** =================================================================== +*/ + +/* print a string */ +#if !defined(lua_writestring) +#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) +#endif + +/* print a newline and flush the output */ +#if !defined(lua_writeline) +#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) +#endif + +/* print an error message */ +#if !defined(lua_writestringerror) +#define lua_writestringerror(s,p) \ + (fprintf(stderr, (s), (p)), fflush(stderr)) +#endif + +/* }================================================================== */ + + +/* +** {============================================================ +** Compatibility with deprecated conversions +** ============================================================= +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) +#define luaL_optunsigned(L,a,d) \ + ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) + +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) + +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#endif +/* }============================================================ */ + + + #endif diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 5255b3cd..a2403952 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,9 +1,13 @@ /* -** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lbaselib.c,v 1.309 2014/12/10 12:26:42 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ +#define lbaselib_c +#define LUA_LIB + +#include "lprefix.h" #include @@ -11,9 +15,6 @@ #include #include -#define lbaselib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -32,62 +33,74 @@ static int luaB_print (lua_State *L) { lua_call(L, 1, 1); s = lua_tolstring(L, -1, &l); /* get result */ if (s == NULL) - return luaL_error(L, - LUA_QL("tostring") " must return a string to " LUA_QL("print")); - if (i>1) luai_writestring("\t", 1); - luai_writestring(s, l); + return luaL_error(L, "'tostring' must return a string to 'print'"); + if (i>1) lua_writestring("\t", 1); + lua_writestring(s, l); lua_pop(L, 1); /* pop result */ } - luai_writeline(); + lua_writeline(); return 0; } #define SPACECHARS " \f\n\r\t\v" +static const char *b_str2int (const char *s, int base, lua_Integer *pn) { + lua_Unsigned n = 0; + int neg = 0; + s += strspn(s, SPACECHARS); /* skip initial spaces */ + if (*s == '-') { s++; neg = 1; } /* handle signal */ + else if (*s == '+') s++; + if (!isalnum((unsigned char)*s)) /* no digit? */ + return NULL; + do { + int digit = (isdigit((unsigned char)*s)) ? *s - '0' + : toupper((unsigned char)*s) - 'A' + 10; + if (digit >= base) return NULL; /* invalid numeral */ + n = n * base + digit; + s++; + } while (isalnum((unsigned char)*s)); + s += strspn(s, SPACECHARS); /* skip trailing spaces */ + *pn = (lua_Integer)((neg) ? (0u - n) : n); + return s; +} + + static int luaB_tonumber (lua_State *L) { - if (lua_isnoneornil(L, 2)) { /* standard conversion */ - int isnum; - lua_Number n = lua_tonumberx(L, 1, &isnum); - if (isnum) { - lua_pushnumber(L, n); - return 1; - } /* else not a number; must be something */ + if (lua_isnoneornil(L, 2)) { /* standard conversion? */ luaL_checkany(L, 1); + if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ + lua_settop(L, 1); /* yes; return it */ + return 1; + } + else { + size_t l; + const char *s = lua_tolstring(L, 1, &l); + if (s != NULL && lua_stringtonumber(L, s) == l + 1) + return 1; /* successful conversion to number */ + /* else not a number */ + } } else { size_t l; - const char *s = luaL_checklstring(L, 1, &l); - const char *e = s + l; /* end point for 's' */ - int base = luaL_checkint(L, 2); - int neg = 0; + const char *s; + lua_Integer n = 0; /* to avoid warnings */ + lua_Integer base = luaL_checkinteger(L, 2); + luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ + s = luaL_checklstring(L, 1, &l); luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - s += strspn(s, SPACECHARS); /* skip initial spaces */ - if (*s == '-') { s++; neg = 1; } /* handle signal */ - else if (*s == '+') s++; - if (isalnum((unsigned char)*s)) { - lua_Number n = 0; - do { - int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : toupper((unsigned char)*s) - 'A' + 10; - if (digit >= base) break; /* invalid numeral; force a fail */ - n = n * (lua_Number)base + (lua_Number)digit; - s++; - } while (isalnum((unsigned char)*s)); - s += strspn(s, SPACECHARS); /* skip trailing spaces */ - if (s == e) { /* no invalid trailing characters? */ - lua_pushnumber(L, (neg) ? -n : n); - return 1; - } /* else not a number */ + if (b_str2int(s, (int)base, &n) == s + l) { + lua_pushinteger(L, n); + return 1; } /* else not a number */ - } + } /* else not a number */ lua_pushnil(L); /* not a number */ return 1; } static int luaB_error (lua_State *L) { - int level = luaL_optint(L, 2, 1); + int level = (int)luaL_optinteger(L, 2, 1); lua_settop(L, 1); if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ luaL_where(L, level); @@ -114,7 +127,7 @@ static int luaB_setmetatable (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table expected"); - if (luaL_getmetafield(L, 1, "__metatable")) + if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); lua_setmetatable(L, 1); @@ -160,19 +173,18 @@ static int luaB_rawset (lua_State *L) { static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "setpause", "setstepmul", - "setmajorinc", "isrunning", "generational", "incremental", NULL}; + "isrunning", NULL}; static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; + LUA_GCISRUNNING}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; - int ex = luaL_optint(L, 2, 0); + int ex = (int)luaL_optinteger(L, 2, 0); int res = lua_gc(L, o, ex); switch (o) { case LUA_GCCOUNT: { int b = lua_gc(L, LUA_GCCOUNTB, 0); - lua_pushnumber(L, res + ((lua_Number)b/1024)); - lua_pushinteger(L, b); - return 2; + lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024)); + return 1; } case LUA_GCSTEP: case LUA_GCISRUNNING: { lua_pushboolean(L, res); @@ -186,16 +198,19 @@ static int luaB_collectgarbage (lua_State *L) { } +/* +** This function has all type names as upvalues, to maximize performance. +*/ static int luaB_type (lua_State *L) { luaL_checkany(L, 1); - lua_pushstring(L, luaL_typename(L, 1)); + lua_pushvalue(L, lua_upvalueindex(lua_type(L, 1) + 1)); return 1; } static int pairsmeta (lua_State *L, const char *method, int iszero, lua_CFunction iter) { - if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ + if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */ luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ lua_pushcfunction(L, iter); /* will return generator, */ lua_pushvalue(L, 1); /* state, */ @@ -227,18 +242,44 @@ static int luaB_pairs (lua_State *L) { } -static int ipairsaux (lua_State *L) { - int i = luaL_checkint(L, 2); +/* +** Traversal function for 'ipairs' for raw tables +*/ +static int ipairsaux_raw (lua_State *L) { + lua_Integer i = luaL_checkinteger(L, 2) + 1; luaL_checktype(L, 1, LUA_TTABLE); - i++; /* next value */ lua_pushinteger(L, i); - lua_rawgeti(L, 1, i); - return (lua_isnil(L, -1)) ? 1 : 2; + return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2; } +/* +** Traversal function for 'ipairs' for tables with metamethods +*/ +static int ipairsaux (lua_State *L) { + lua_Integer i = luaL_checkinteger(L, 2) + 1; + lua_pushinteger(L, i); + return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; +} + + +/* +** This function will use either 'ipairsaux' or 'ipairsaux_raw' to +** traverse a table, depending on whether the table has metamethods +** that can affect the traversal. +*/ static int luaB_ipairs (lua_State *L) { - return pairsmeta(L, "__ipairs", 1, ipairsaux); + lua_CFunction iter = (luaL_getmetafield(L, 1, "__index") != LUA_TNIL) + ? ipairsaux : ipairsaux_raw; +#if defined(LUA_COMPAT_IPAIRS) + return pairsmeta(L, "__ipairs", 1, iter); +#else + luaL_checkany(L, 1); + lua_pushcfunction(L, iter); /* iteration function */ + lua_pushvalue(L, 1); /* state */ + lua_pushinteger(L, 0); /* initial value */ + return 3; +#endif } @@ -284,7 +325,7 @@ static int luaB_loadfile (lua_State *L) { /* -** Reader for generic `load' function: `lua_load' uses the +** Reader for generic 'load' function: 'lua_load' uses the ** stack for internal stuff, so the reader cannot change the ** stack top. Instead, it keeps its resulting string in a ** reserved slot inside the stack. @@ -328,7 +369,8 @@ static int luaB_load (lua_State *L) { /* }====================================================== */ -static int dofilecont (lua_State *L) { +static int dofilecont (lua_State *L, int d1, lua_KContext d2) { + (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ return lua_gettop(L) - 1; } @@ -339,14 +381,20 @@ static int luaB_dofile (lua_State *L) { if (luaL_loadfile(L, fname) != LUA_OK) return lua_error(L); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); - return dofilecont(L); + return dofilecont(L, 0, 0); } static int luaB_assert (lua_State *L) { - if (!lua_toboolean(L, 1)) - return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); - return lua_gettop(L); + if (lua_toboolean(L, 1)) /* condition is true? */ + return lua_gettop(L); /* return all arguments */ + else { /* error */ + luaL_checkany(L, 1); /* there must be a condition */ + lua_remove(L, 1); /* remove it */ + lua_pushliteral(L, "assertion failed!"); /* default message */ + lua_settop(L, 1); /* leave only message (default if no other one) */ + return luaB_error(L); /* call 'error' */ + } } @@ -357,53 +405,57 @@ static int luaB_select (lua_State *L) { return 1; } else { - int i = luaL_checkint(L, 1); + lua_Integer i = luaL_checkinteger(L, 1); if (i < 0) i = n + i; else if (i > n) i = n; luaL_argcheck(L, 1 <= i, 1, "index out of range"); - return n - i; + return n - (int)i; } } -static int finishpcall (lua_State *L, int status) { - if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ - lua_settop(L, 0); /* create space for return values */ - lua_pushboolean(L, 0); - lua_pushstring(L, "stack overflow"); +/* +** Continuation function for 'pcall' and 'xpcall'. Both functions +** already pushed a 'true' before doing the call, so in case of success +** 'finishpcall' only has to return everything in the stack minus +** 'extra' values (where 'extra' is exactly the number of items to be +** ignored). +*/ +static int finishpcall (lua_State *L, int status, lua_KContext extra) { + if (status != LUA_OK && status != LUA_YIELD) { /* error? */ + lua_pushboolean(L, 0); /* first result (false) */ + lua_pushvalue(L, -2); /* error message */ return 2; /* return false, msg */ } - lua_pushboolean(L, status); /* first result (status) */ - lua_replace(L, 1); /* put first result in first slot */ - return lua_gettop(L); -} - - -static int pcallcont (lua_State *L) { - int status = lua_getctx(L, NULL); - return finishpcall(L, (status == LUA_YIELD)); + else + return lua_gettop(L) - (int)extra; /* return all results */ } static int luaB_pcall (lua_State *L) { int status; luaL_checkany(L, 1); - lua_pushnil(L); - lua_insert(L, 1); /* create space for status result */ - status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); - return finishpcall(L, (status == LUA_OK)); + lua_pushboolean(L, 1); /* first result if no errors */ + lua_insert(L, 1); /* put it in place */ + status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall); + return finishpcall(L, status, 0); } +/* +** Do a protected call with error handling. After 'lua_rotate', the +** stack will have ; so, the function passes +** 2 to 'finishpcall' to skip the 2 first values when returning results. +*/ static int luaB_xpcall (lua_State *L) { int status; int n = lua_gettop(L); - luaL_argcheck(L, n >= 2, 2, "value expected"); - lua_pushvalue(L, 1); /* exchange function... */ - lua_copy(L, 2, 1); /* ...and error handler */ - lua_replace(L, 2); - status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont); - return finishpcall(L, (status == LUA_OK)); + luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */ + lua_pushboolean(L, 1); /* first result */ + lua_pushvalue(L, 1); /* function */ + lua_rotate(L, 3, 2); /* move them below function's arguments */ + status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall); + return finishpcall(L, status, 2); } @@ -438,21 +490,31 @@ static const luaL_Reg base_funcs[] = { {"setmetatable", luaB_setmetatable}, {"tonumber", luaB_tonumber}, {"tostring", luaB_tostring}, - {"type", luaB_type}, {"xpcall", luaB_xpcall}, + /* placeholders */ + {"type", NULL}, + {"_G", NULL}, + {"_VERSION", NULL}, {NULL, NULL} }; LUAMOD_API int luaopen_base (lua_State *L) { - /* set global _G */ - lua_pushglobaltable(L); - lua_pushglobaltable(L); - lua_setfield(L, -2, "_G"); + int i; /* open lib into global table */ + lua_pushglobaltable(L); luaL_setfuncs(L, base_funcs, 0); + /* set global _G */ + lua_pushvalue(L, -1); + lua_setfield(L, -2, "_G"); + /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); - lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ + lua_setfield(L, -2, "_VERSION"); + /* set function 'type' with proper upvalues */ + for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */ + lua_pushstring(L, lua_typename(L, i)); + lua_pushcclosure(L, luaB_type, LUA_NUMTAGS); + lua_setfield(L, -2, "type"); return 1; } diff --git a/3rd/lua/lbitlib.c b/3rd/lua/lbitlib.c index 31c7b66f..15d5f0cd 100644 --- a/3rd/lua/lbitlib.c +++ b/3rd/lua/lbitlib.c @@ -1,5 +1,5 @@ /* -** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ +** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp $ ** Standard library for bitwise operations ** See Copyright Notice in lua.h */ @@ -7,20 +7,32 @@ #define lbitlib_c #define LUA_LIB +#include "lprefix.h" + + #include "lua.h" #include "lauxlib.h" #include "lualib.h" +#if defined(LUA_COMPAT_BITLIB) /* { */ + + /* number of bits to consider in a number */ #if !defined(LUA_NBITS) #define LUA_NBITS 32 #endif +/* +** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must +** be made in two parts to avoid problems when LUA_NBITS is equal to the +** number of bits in a lua_Unsigned.) +*/ #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) + /* macro to trim extra bits */ #define trim(x) ((x) & ALLONES) @@ -29,13 +41,10 @@ #define mask(n) (~((ALLONES << 1) << ((n) - 1))) -typedef lua_Unsigned b_uint; - - -static b_uint andaux (lua_State *L) { +static lua_Unsigned andaux (lua_State *L) { int i, n = lua_gettop(L); - b_uint r = ~(b_uint)0; + lua_Unsigned r = ~(lua_Unsigned)0; for (i = 1; i <= n; i++) r &= luaL_checkunsigned(L, i); return trim(r); @@ -43,14 +52,14 @@ static b_uint andaux (lua_State *L) { static int b_and (lua_State *L) { - b_uint r = andaux(L); + lua_Unsigned r = andaux(L); lua_pushunsigned(L, r); return 1; } static int b_test (lua_State *L) { - b_uint r = andaux(L); + lua_Unsigned r = andaux(L); lua_pushboolean(L, r != 0); return 1; } @@ -58,7 +67,7 @@ static int b_test (lua_State *L) { static int b_or (lua_State *L) { int i, n = lua_gettop(L); - b_uint r = 0; + lua_Unsigned r = 0; for (i = 1; i <= n; i++) r |= luaL_checkunsigned(L, i); lua_pushunsigned(L, trim(r)); @@ -68,7 +77,7 @@ static int b_or (lua_State *L) { static int b_xor (lua_State *L) { int i, n = lua_gettop(L); - b_uint r = 0; + lua_Unsigned r = 0; for (i = 1; i <= n; i++) r ^= luaL_checkunsigned(L, i); lua_pushunsigned(L, trim(r)); @@ -77,13 +86,13 @@ static int b_xor (lua_State *L) { static int b_not (lua_State *L) { - b_uint r = ~luaL_checkunsigned(L, 1); + lua_Unsigned r = ~luaL_checkunsigned(L, 1); lua_pushunsigned(L, trim(r)); return 1; } -static int b_shift (lua_State *L, b_uint r, int i) { +static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { if (i < 0) { /* shift right? */ i = -i; r = trim(r); @@ -101,33 +110,33 @@ static int b_shift (lua_State *L, b_uint r, int i) { static int b_lshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); + return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2)); } static int b_rshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); + return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2)); } static int b_arshift (lua_State *L) { - b_uint r = luaL_checkunsigned(L, 1); - int i = luaL_checkint(L, 2); - if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) + lua_Unsigned r = luaL_checkunsigned(L, 1); + lua_Integer i = luaL_checkinteger(L, 2); + if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) return b_shift(L, r, -i); else { /* arithmetic shift for 'negative' number */ if (i >= LUA_NBITS) r = ALLONES; else - r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ + r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ lua_pushunsigned(L, r); return 1; } } -static int b_rot (lua_State *L, int i) { - b_uint r = luaL_checkunsigned(L, 1); - i &= (LUA_NBITS - 1); /* i = i % NBITS */ +static int b_rot (lua_State *L, lua_Integer d) { + lua_Unsigned r = luaL_checkunsigned(L, 1); + int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ r = trim(r); if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ r = (r << i) | (r >> (LUA_NBITS - i)); @@ -137,12 +146,12 @@ static int b_rot (lua_State *L, int i) { static int b_lrot (lua_State *L) { - return b_rot(L, luaL_checkint(L, 2)); + return b_rot(L, luaL_checkinteger(L, 2)); } static int b_rrot (lua_State *L) { - return b_rot(L, -luaL_checkint(L, 2)); + return b_rot(L, -luaL_checkinteger(L, 2)); } @@ -153,20 +162,20 @@ static int b_rrot (lua_State *L) { ** 'width' being used uninitialized.) */ static int fieldargs (lua_State *L, int farg, int *width) { - int f = luaL_checkint(L, farg); - int w = luaL_optint(L, farg + 1, 1); + lua_Integer f = luaL_checkinteger(L, farg); + lua_Integer w = luaL_optinteger(L, farg + 1, 1); luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); if (f + w > LUA_NBITS) luaL_error(L, "trying to access non-existent bits"); - *width = w; - return f; + *width = (int)w; + return (int)f; } static int b_extract (lua_State *L) { int w; - b_uint r = luaL_checkunsigned(L, 1); + lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); int f = fieldargs(L, 2, &w); r = (r >> f) & mask(w); lua_pushunsigned(L, r); @@ -176,8 +185,8 @@ static int b_extract (lua_State *L) { static int b_replace (lua_State *L) { int w; - b_uint r = luaL_checkunsigned(L, 1); - b_uint v = luaL_checkunsigned(L, 2); + lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); + lua_Unsigned v = luaL_checkunsigned(L, 2); int f = fieldargs(L, 3, &w); int m = mask(w); v &= m; /* erase bits outside given width */ @@ -210,3 +219,12 @@ LUAMOD_API int luaopen_bit32 (lua_State *L) { return 1; } + +#else /* }{ */ + + +LUAMOD_API int luaopen_bit32 (lua_State *L) { + return luaL_error(L, "library 'bit32' has been deprecated"); +} + +#endif /* } */ diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 5ba570e0..5e34624b 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,15 +1,18 @@ /* -** $Id: lcode.c,v 2.62.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lcode.c,v 2.99 2014/12/29 16:49:25 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ - -#include - #define lcode_c #define LUA_CORE +#include "lprefix.h" + + +#include +#include + #include "lua.h" #include "lcode.h" @@ -26,11 +29,25 @@ #include "lvm.h" +/* Maximum number of registers in a Lua function */ +#define MAXREGS 250 + + #define hasjumps(e) ((e)->t != (e)->f) -static int isnumeral(expdesc *e) { - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); +static int tonumeral(expdesc *e, TValue *v) { + if (e->t != NO_JUMP || e->f != NO_JUMP) + return 0; /* not a numeral */ + switch (e->k) { + case VKINT: + if (v) setivalue(v, e->u.ival); + return 1; + case VKFLT: + if (v) setfltvalue(v, e->u.nval); + return 1; + default: return 0; + } } @@ -38,7 +55,7 @@ void luaK_nil (FuncState *fs, int from, int n) { Instruction *previous; int l = from + n - 1; /* last register to set nil */ if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - previous = &fs->f->sp->code[fs->pc-1]; + previous = &fs->f->code[fs->pc-1]; if (GET_OPCODE(*previous) == OP_LOADNIL) { int pfrom = GETARG_A(*previous); int pl = pfrom + GETARG_B(*previous); @@ -78,7 +95,7 @@ static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->sp->code[pc]; + Instruction *jmp = &fs->f->code[pc]; int offset = dest-(pc+1); lua_assert(dest != NO_JUMP); if (abs(offset) > MAXARG_sBx) @@ -88,7 +105,7 @@ static void fixjump (FuncState *fs, int pc, int dest) { /* -** returns current `pc' and marks it as a jump target (to avoid wrong +** returns current 'pc' and marks it as a jump target (to avoid wrong ** optimizations with consecutive instructions not in the same basic block). */ int luaK_getlabel (FuncState *fs) { @@ -98,7 +115,7 @@ int luaK_getlabel (FuncState *fs) { static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->sp->code[pc]); + int offset = GETARG_sBx(fs->f->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ return NO_JUMP; /* end of list */ else @@ -107,7 +124,7 @@ static int getjump (FuncState *fs, int pc) { static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->sp->code[pc]; + Instruction *pi = &fs->f->code[pc]; if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) return pi-1; else @@ -176,14 +193,14 @@ void luaK_patchlist (FuncState *fs, int list, int target) { } -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) { +void luaK_patchclose (FuncState *fs, int list, int level) { level++; /* argument is +1 to reserve 0 as non-op */ while (list != NO_JUMP) { int next = getjump(fs, list); - lua_assert(GET_OPCODE(fs->f->sp->code[list]) == OP_JMP && - (GETARG_A(fs->f->sp->code[list]) == 0 || - GETARG_A(fs->f->sp->code[list]) >= level)); - SETARG_A(fs->f->sp->code[list], level); + lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && + (GETARG_A(fs->f->code[list]) == 0 || + GETARG_A(fs->f->code[list]) >= level)); + SETARG_A(fs->f->code[list], level); list = next; } } @@ -210,8 +227,8 @@ void luaK_concat (FuncState *fs, int *l1, int l2) { static int luaK_code (FuncState *fs, Instruction i) { - SharedProto *f = fs->f->sp; - dischargejpc(fs); /* `pc' will change */ + Proto *f = fs->f; + dischargejpc(fs); /* 'pc' will change */ /* put new instruction in code array */ luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, MAX_INT, "opcodes"); @@ -260,10 +277,10 @@ int luaK_codek (FuncState *fs, int reg, int k) { void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; - if (newstack > fs->f->sp->maxstacksize) { - if (newstack >= MAXSTACK) + if (newstack > fs->f->maxstacksize) { + if (newstack >= MAXREGS) luaX_syntaxerror(fs->ls, "function or expression too complex"); - fs->f->sp->maxstacksize = cast_byte(newstack); + fs->f->maxstacksize = cast_byte(newstack); } } @@ -288,27 +305,30 @@ static void freeexp (FuncState *fs, expdesc *e) { } +/* +** Use scanner's table to cache position of constants in constant list +** and try to reuse constants +*/ static int addk (FuncState *fs, TValue *key, TValue *v) { lua_State *L = fs->ls->L; - TValue *idx = luaH_set(L, fs->h, key); Proto *f = fs->f; + TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ int k, oldsize; - if (ttisnumber(idx)) { - lua_Number n = nvalue(idx); - lua_number2int(k, n); - if (luaV_rawequalobj(&f->k[k], v)) - return k; - /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0"); - go through and create a new entry for this value */ + if (ttisinteger(idx)) { /* is there an index there? */ + k = cast_int(ivalue(idx)); + /* correct value? (warning: must distinguish floats from integers!) */ + if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && + luaV_rawequalobj(&f->k[k], v)) + return k; /* reuse index */ } /* constant not found; create a new entry */ - oldsize = f->sp->sizek; + oldsize = f->sizek; k = fs->nk; /* numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ - setnvalue(idx, cast_num(k)); - luaM_growvector(L, f->k, k, f->sp->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sp->sizek) setnilvalue(&f->k[oldsize++]); + setivalue(idx, k); + luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); + while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); fs->nk++; luaC_barrier(L, f, v); @@ -323,20 +343,23 @@ int luaK_stringK (FuncState *fs, TString *s) { } -int luaK_numberK (FuncState *fs, lua_Number r) { - int n; - lua_State *L = fs->ls->L; +/* +** Integers use userdata as keys to avoid collision with floats with same +** value; conversion to 'void*' used only for hashing, no "precision" +** problems +*/ +int luaK_intK (FuncState *fs, lua_Integer n) { + TValue k, o; + setpvalue(&k, cast(void*, cast(size_t, n))); + setivalue(&o, n); + return addk(fs, &k, &o); +} + + +static int luaK_numberK (FuncState *fs, lua_Number r) { TValue o; - setnvalue(&o, r); - if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ - /* use raw representation as key to avoid numeric problems */ - setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r))); - n = addk(fs, L->top - 1, &o); - L->top--; - } - else - n = addk(fs, &o, &o); /* regular case */ - return n; + setfltvalue(&o, r); + return addk(fs, &o, &o); } @@ -351,7 +374,7 @@ static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->ls->L, &k, fs->h); + sethvalue(fs->ls->L, &k, fs->ls->h); return addk(fs, &k, &v); } @@ -433,10 +456,14 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_codek(fs, reg, e->u.info); break; } - case VKNUM: { + case VKFLT: { luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); break; } + case VKINT: { + luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); + break; + } case VRELOCABLE: { Instruction *pc = &getcode(fs, e); SETARG_A(*pc, reg); @@ -468,7 +495,7 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { static void exp2reg (FuncState *fs, expdesc *e, int reg) { discharge2reg(fs, e, reg); if (e->k == VJMP) - luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */ + luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ if (hasjumps(e)) { int final; /* position after whole expression */ int p_f = NO_JUMP; /* position of an eventual LOAD false */ @@ -538,13 +565,19 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { } else break; } - case VKNUM: { + case VKINT: { + e->u.info = luaK_intK(fs, e->u.ival); + e->k = VK; + goto vk; + } + case VKFLT: { e->u.info = luaK_numberK(fs, e->u.nval); e->k = VK; /* go through */ } case VK: { - if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */ + vk: + if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ return RKASK(e->u.info); else break; } @@ -627,7 +660,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { pc = e->u.info; break; } - case VK: case VKNUM: case VTRUE: { + case VK: case VKFLT: case VKINT: case VTRUE: { pc = NO_JUMP; /* always true; do nothing */ break; } @@ -636,7 +669,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { break; } } - luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ + luaK_concat(fs, &e->f, pc); /* insert last jump in 'f' list */ luaK_patchtohere(fs, e->t); e->t = NO_JUMP; } @@ -659,7 +692,7 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) { break; } } - luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ + luaK_concat(fs, &e->t, pc); /* insert last jump in 't' list */ luaK_patchtohere(fs, e->f); e->f = NO_JUMP; } @@ -672,7 +705,7 @@ static void codenot (FuncState *fs, expdesc *e) { e->k = VTRUE; break; } - case VK: case VKNUM: case VTRUE: { + case VK: case VKFLT: case VKINT: case VTRUE: { e->k = VFALSE; break; } @@ -710,25 +743,70 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { } -static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { - lua_Number r; - if (!isnumeral(e1) || !isnumeral(e2)) return 0; - if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0) - return 0; /* do not attempt to divide by 0 */ - r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval); - e1->u.nval = r; +/* +** return false if folding can raise an error +*/ +static int validop (int op, TValue *v1, TValue *v2) { + switch (op) { + case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: + case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ + lua_Integer i; + return (tointeger(v1, &i) && tointeger(v2, &i)); + } + case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ + return (nvalue(v2) != 0); + default: return 1; /* everything else is valid */ + } +} + + +/* +** Try to "constant-fold" an operation; return 1 iff successful +*/ +static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { + TValue v1, v2, res; + if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) + return 0; /* non-numeric operands or not safe to fold */ + luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ + if (ttisinteger(&res)) { + e1->k = VKINT; + e1->u.ival = ivalue(&res); + } + else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */ + lua_Number n = fltvalue(&res); + if (luai_numisnan(n) || n == 0) + return 0; + e1->k = VKFLT; + e1->u.nval = n; + } return 1; } -static void codearith (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - if (constfolding(op, e1, e2)) - return; +/* +** Code for binary and unary expressions that "produce values" +** (arithmetic operations, bitwise operations, concat, length). First +** try to do constant folding (only for numeric [arithmetic and +** bitwise] operations, which is what 'lua_arith' accepts). +** Expression to produce final result will be encoded in 'e1'. +*/ +static void codeexpval (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int line) { + lua_assert(op >= OP_ADD); + if (op <= OP_BNOT && constfolding(fs, op - OP_ADD + LUA_OPADD, e1, e2)) + return; /* result has been folded */ else { - int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; - int o1 = luaK_exp2RK(fs, e1); - if (o1 > o2) { + int o1, o2; + /* move operands to registers (if needed) */ + if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) { /* unary op? */ + o2 = 0; /* no second expression */ + o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */ + } + else { /* regular case (binary operators) */ + o2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ + o1 = luaK_exp2RK(fs, e1); + } + if (o1 > o2) { /* free registers in proper order */ freeexp(fs, e1); freeexp(fs, e2); } @@ -736,8 +814,8 @@ static void codearith (FuncState *fs, OpCode op, freeexp(fs, e2); freeexp(fs, e1); } - e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); - e1->k = VRELOCABLE; + e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */ + e1->k = VRELOCABLE; /* all those operations are relocable */ luaK_fixline(fs, line); } } @@ -750,7 +828,7 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, freeexp(fs, e2); freeexp(fs, e1); if (cond == 0 && op != OP_EQ) { - int temp; /* exchange args to replace by `<' or `<=' */ + int temp; /* exchange args to replace by '<' or '<=' */ temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ cond = 1; } @@ -761,23 +839,13 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { expdesc e2; - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; + e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0; switch (op) { - case OPR_MINUS: { - if (isnumeral(e)) /* minus constant? */ - e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */ - else { - luaK_exp2anyreg(fs, e); - codearith(fs, OP_UNM, e, &e2, line); - } + case OPR_MINUS: case OPR_BNOT: case OPR_LEN: { + codeexpval(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line); break; } case OPR_NOT: codenot(fs, e); break; - case OPR_LEN: { - luaK_exp2anyreg(fs, e); /* cannot operate on constants */ - codearith(fs, OP_LEN, e, &e2, line); - break; - } default: lua_assert(0); } } @@ -794,12 +862,15 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { break; } case OPR_CONCAT: { - luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ + luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */ break; } - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_MOD: case OPR_POW: { - if (!isnumeral(v)) luaK_exp2RK(fs, v); + case OPR_ADD: case OPR_SUB: + case OPR_MUL: case OPR_DIV: case OPR_IDIV: + case OPR_MOD: case OPR_POW: + case OPR_BAND: case OPR_BOR: case OPR_BXOR: + case OPR_SHL: case OPR_SHR: { + if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); break; } default: { @@ -837,13 +908,15 @@ void luaK_posfix (FuncState *fs, BinOpr op, } else { luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codearith(fs, OP_CONCAT, e1, e2, line); + codeexpval(fs, OP_CONCAT, e1, e2, line); } break; } case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_MOD: case OPR_POW: { - codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line); + case OPR_IDIV: case OPR_MOD: case OPR_POW: + case OPR_BAND: case OPR_BOR: case OPR_BXOR: + case OPR_SHL: case OPR_SHR: { + codeexpval(fs, cast(OpCode, (op - OPR_ADD) + OP_ADD), e1, e2, line); break; } case OPR_EQ: case OPR_LT: case OPR_LE: { @@ -860,7 +933,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, void luaK_fixline (FuncState *fs, int line) { - fs->f->sp->lineinfo[fs->pc - 1] = line; + fs->f->lineinfo[fs->pc - 1] = line; } diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 5a8e0b80..43ab86db 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -24,7 +24,11 @@ ** grep "ORDER OPR" if you change these enums (ORDER OP) */ typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, + OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, + OPR_DIV, + OPR_IDIV, + OPR_BAND, OPR_BOR, OPR_BXOR, + OPR_SHL, OPR_SHR, OPR_CONCAT, OPR_EQ, OPR_LT, OPR_LE, OPR_NE, OPR_GT, OPR_GE, @@ -33,10 +37,10 @@ typedef enum BinOpr { } BinOpr; -typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; +typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; -#define getcode(fs,e) ((fs)->f->sp->code[(e)->u.info]) +#define getcode(fs,e) ((fs)->f->code[(e)->u.info]) #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) @@ -52,7 +56,7 @@ LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); -LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); +LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index ce4f6ad4..0c0b7fa6 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -1,22 +1,30 @@ /* -** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp $ ** Coroutine Library ** See Copyright Notice in lua.h */ - -#include - - #define lcorolib_c #define LUA_LIB +#include "lprefix.h" + + +#include + #include "lua.h" #include "lauxlib.h" #include "lualib.h" +static lua_State *getco (lua_State *L) { + lua_State *co = lua_tothread(L, 1); + luaL_argcheck(L, co, 1, "thread expected"); + return co; +} + + static int auxresume (lua_State *L, lua_State *co, int narg) { int status; if (!lua_checkstack(co, narg)) { @@ -47,9 +55,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { static int luaB_coresume (lua_State *L) { - lua_State *co = lua_tothread(L, 1); + lua_State *co = getco(L); int r; - luaL_argcheck(L, co, 1, "coroutine expected"); r = auxresume(L, co, lua_gettop(L) - 1); if (r < 0) { lua_pushboolean(L, 0); @@ -59,7 +66,7 @@ static int luaB_coresume (lua_State *L) { else { lua_pushboolean(L, 1); lua_insert(L, -(r + 1)); - return r + 1; /* return true + `resume' returns */ + return r + 1; /* return true + 'resume' returns */ } } @@ -102,8 +109,7 @@ static int luaB_yield (lua_State *L) { static int luaB_costatus (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "coroutine expected"); + lua_State *co = getco(L); if (L == co) lua_pushliteral(L, "running"); else { switch (lua_status(co)) { @@ -129,6 +135,12 @@ static int luaB_costatus (lua_State *L) { } +static int luaB_yieldable (lua_State *L) { + lua_pushboolean(L, lua_isyieldable(L)); + return 1; +} + + static int luaB_corunning (lua_State *L) { int ismain = lua_pushthread(L); lua_pushboolean(L, ismain); @@ -143,6 +155,7 @@ static const luaL_Reg co_funcs[] = { {"status", luaB_costatus}, {"wrap", luaB_cowrap}, {"yield", luaB_yield}, + {"isyieldable", luaB_yieldable}, {NULL, NULL} }; diff --git a/3rd/lua/lctype.c b/3rd/lua/lctype.c index 93f8cadc..ae9367e6 100644 --- a/3rd/lua/lctype.c +++ b/3rd/lua/lctype.c @@ -1,5 +1,5 @@ /* -** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ @@ -7,6 +7,9 @@ #define lctype_c #define LUA_CORE +#include "lprefix.h" + + #include "lctype.h" #if !LUA_USE_CTYPE /* { */ diff --git a/3rd/lua/lctype.h b/3rd/lua/lctype.h index b09b21a3..99c7d122 100644 --- a/3rd/lua/lctype.h +++ b/3rd/lua/lctype.h @@ -1,5 +1,5 @@ /* -** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ diff --git a/3rd/lua/ldblib.c b/3rd/lua/ldblib.c index 84fe3c7d..24a11b53 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -1,25 +1,30 @@ /* -** $Id: ldblib.c,v 1.132.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldblib.c,v 1.148 2015/01/02 12:52:22 roberto Exp $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ +#define ldblib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include #include -#define ldblib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#define HOOKKEY "_HKEY" - +/* +** The hook table at registry[&HOOKKEY] maps threads to their current +** hook function. (We only need the unique address of 'HOOKKEY'.) +*/ +static const int HOOKKEY = 0; static int db_getregistry (lua_State *L) { @@ -57,35 +62,20 @@ static int db_getuservalue (lua_State *L) { static int db_setuservalue (lua_State *L) { - if (lua_type(L, 1) == LUA_TLIGHTUSERDATA) - luaL_argerror(L, 1, "full userdata expected, got light userdata"); luaL_checktype(L, 1, LUA_TUSERDATA); - if (!lua_isnoneornil(L, 2)) - luaL_checktype(L, 2, LUA_TTABLE); + luaL_checkany(L, 2); lua_settop(L, 2); lua_setuservalue(L, 1); return 1; } -static void settabss (lua_State *L, const char *i, const char *v) { - lua_pushstring(L, v); - lua_setfield(L, -2, i); -} - - -static void settabsi (lua_State *L, const char *i, int v) { - lua_pushinteger(L, v); - lua_setfield(L, -2, i); -} - - -static void settabsb (lua_State *L, const char *i, int v) { - lua_pushboolean(L, v); - lua_setfield(L, -2, i); -} - - +/* +** Auxiliary function used by several library functions: check for +** an optional thread as function's first argument and set 'arg' with +** 1 if this argument is present (so that functions can skip it to +** access their other arguments) +*/ static lua_State *getthread (lua_State *L, int *arg) { if (lua_isthread(L, 1)) { *arg = 1; @@ -93,44 +83,70 @@ static lua_State *getthread (lua_State *L, int *arg) { } else { *arg = 0; - return L; + return L; /* function will operate over current thread */ } } +/* +** Variations of 'lua_settable', used by 'db_getinfo' to put results +** from 'lua_getinfo' into result table. Key is always a string; +** value can be a string, an int, or a boolean. +*/ +static void settabss (lua_State *L, const char *k, const char *v) { + lua_pushstring(L, v); + lua_setfield(L, -2, k); +} + +static void settabsi (lua_State *L, const char *k, int v) { + lua_pushinteger(L, v); + lua_setfield(L, -2, k); +} + +static void settabsb (lua_State *L, const char *k, int v) { + lua_pushboolean(L, v); + lua_setfield(L, -2, k); +} + + +/* +** In function 'db_getinfo', the call to 'lua_getinfo' may push +** results on the stack; later it creates the result table to put +** these objects. Function 'treatstackoption' puts the result from +** 'lua_getinfo' on top of the result table so that it can call +** 'lua_setfield'. +*/ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { - if (L == L1) { - lua_pushvalue(L, -2); - lua_remove(L, -3); - } + if (L == L1) + lua_rotate(L, -2, 1); /* exchange object and table */ else - lua_xmove(L1, L, 1); - lua_setfield(L, -2, fname); + lua_xmove(L1, L, 1); /* move object to the "main" stack */ + lua_setfield(L, -2, fname); /* put object into table */ } +/* +** Calls 'lua_getinfo' and collects all results in a new table. +*/ static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnStu"); - if (lua_isnumber(L, arg+1)) { - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { + if (lua_isfunction(L, arg + 1)) { /* info about a function? */ + options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ + lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ + lua_xmove(L, L1, 1); + } + else { /* stack level */ + if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { lua_pushnil(L); /* level out of range */ return 1; } } - else if (lua_isfunction(L, arg+1)) { - lua_pushfstring(L, ">%s", options); - options = lua_tostring(L, -1); - lua_pushvalue(L, arg+1); - lua_xmove(L, L1, 1); - } - else - return luaL_argerror(L, arg+1, "function or level expected"); if (!lua_getinfo(L1, options, &ar)) return luaL_argerror(L, arg+2, "invalid option"); - lua_createtable(L, 0, 2); + lua_newtable(L); /* table to collect results */ if (strchr(options, 'S')) { settabss(L, "source", ar.source); settabss(L, "short_src", ar.short_src); @@ -164,20 +180,21 @@ static int db_getlocal (lua_State *L) { lua_State *L1 = getthread(L, &arg); lua_Debug ar; const char *name; - int nvar = luaL_checkint(L, arg+2); /* local-variable index */ + int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ if (lua_isfunction(L, arg + 1)) { /* function argument? */ lua_pushvalue(L, arg + 1); /* push function */ lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ - return 1; + return 1; /* return only name (there is no value) */ } else { /* stack-level argument */ - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + int level = (int)luaL_checkinteger(L, arg + 1); + if (!lua_getstack(L1, level, &ar)) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); name = lua_getlocal(L1, &ar, nvar); if (name) { - lua_xmove(L1, L, 1); /* push local value */ + lua_xmove(L1, L, 1); /* move local value */ lua_pushstring(L, name); /* push name */ - lua_pushvalue(L, -2); /* re-order */ + lua_rotate(L, -2, 1); /* re-order */ return 2; } else { @@ -190,26 +207,35 @@ static int db_getlocal (lua_State *L) { static int db_setlocal (lua_State *L) { int arg; + const char *name; lua_State *L1 = getthread(L, &arg); lua_Debug ar; - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + int level = (int)luaL_checkinteger(L, arg + 1); + int nvar = (int)luaL_checkinteger(L, arg + 2); + if (!lua_getstack(L1, level, &ar)) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); luaL_checkany(L, arg+3); lua_settop(L, arg+3); lua_xmove(L, L1, 1); - lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); + name = lua_setlocal(L1, &ar, nvar); + if (name == NULL) + lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */ + lua_pushstring(L, name); return 1; } +/* +** get (if 'get' is true) or set an upvalue from a closure +*/ static int auxupvalue (lua_State *L, int get) { const char *name; - int n = luaL_checkint(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); + int n = (int)luaL_checkinteger(L, 2); /* upvalue index */ + luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */ name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); if (name == NULL) return 0; lua_pushstring(L, name); - lua_insert(L, -(get+1)); + lua_insert(L, -(get+1)); /* no-op if get is false */ return get + 1; } @@ -225,13 +251,15 @@ static int db_setupvalue (lua_State *L) { } +/* +** Check whether a given upvalue from a given closure exists and +** returns its index +*/ static int checkupval (lua_State *L, int argf, int argnup) { - lua_Debug ar; - int nup = luaL_checkint(L, argnup); - luaL_checktype(L, argf, LUA_TFUNCTION); - lua_pushvalue(L, argf); - lua_getinfo(L, ">u", &ar); - luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index"); + int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ + luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ + luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, + "invalid upvalue index"); return nup; } @@ -253,26 +281,29 @@ static int db_upvaluejoin (lua_State *L) { } -#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) - - +/* +** Call hook function registered at hook table for the current +** thread (if there is one) +*/ static void hookf (lua_State *L, lua_Debug *ar) { static const char *const hooknames[] = {"call", "return", "line", "count", "tail call"}; - gethooktable(L); + lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); lua_pushthread(L); - lua_rawget(L, -2); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); + if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ + lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); + lua_pushinteger(L, ar->currentline); /* push current line */ else lua_pushnil(L); lua_assert(lua_getinfo(L, "lS", ar)); - lua_call(L, 2, 0); + lua_call(L, 2, 0); /* call hook function */ } } +/* +** Convert a string mask (for 'sethook') into a bit mask +*/ static int makemask (const char *smask, int count) { int mask = 0; if (strchr(smask, 'c')) mask |= LUA_MASKCALL; @@ -283,6 +314,9 @@ static int makemask (const char *smask, int count) { } +/* +** Convert a bit mask (for 'gethook') into a string mask +*/ static char *unmakemask (int mask, char *smask) { int i = 0; if (mask & LUA_MASKCALL) smask[i++] = 'c'; @@ -297,26 +331,29 @@ static int db_sethook (lua_State *L) { int arg, mask, count; lua_Hook func; lua_State *L1 = getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { + if (lua_isnoneornil(L, arg+1)) { /* no hook? */ lua_settop(L, arg+1); func = NULL; mask = 0; count = 0; /* turn off hooks */ } else { const char *smask = luaL_checkstring(L, arg+2); luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = luaL_optint(L, arg+3, 0); + count = (int)luaL_optinteger(L, arg + 3, 0); func = hookf; mask = makemask(smask, count); } - if (gethooktable(L) == 0) { /* creating hook table? */ + if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { + lua_createtable(L, 0, 2); /* create a hook table */ + lua_pushvalue(L, -1); + lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ lua_pushstring(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ } - lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_pushvalue(L, arg+1); - lua_rawset(L, -3); /* set new hook */ - lua_sethook(L1, func, mask, count); /* set hooks */ + lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ + lua_pushvalue(L, arg + 1); /* value (hook function) */ + lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ + lua_sethook(L1, func, mask, count); return 0; } @@ -327,16 +364,18 @@ static int db_gethook (lua_State *L) { char buff[5]; int mask = lua_gethookmask(L1); lua_Hook hook = lua_gethook(L1); - if (hook != NULL && hook != hookf) /* external hook? */ + if (hook == NULL) /* no hook? */ + lua_pushnil(L); + else if (hook != hookf) /* external hook? */ lua_pushliteral(L, "external hook"); - else { - gethooktable(L); + else { /* hook table must exist */ + lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_rawget(L, -2); /* get hook */ + lua_rawget(L, -2); /* 1st result = hooktable[L1] */ lua_remove(L, -2); /* remove hook table */ } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L1)); + lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ + lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */ return 3; } @@ -344,13 +383,13 @@ static int db_gethook (lua_State *L) { static int db_debug (lua_State *L) { for (;;) { char buffer[250]; - luai_writestringerror("%s", "lua_debug> "); + lua_writestringerror("%s", "lua_debug> "); if (fgets(buffer, sizeof(buffer), stdin) == 0 || strcmp(buffer, "cont\n") == 0) return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || lua_pcall(L, 0, 0, 0)) - luai_writestringerror("%s\n", lua_tostring(L, -1)); + lua_writestringerror("%s\n", lua_tostring(L, -1)); lua_settop(L, 0); /* remove eventual returns */ } } @@ -363,7 +402,7 @@ static int db_traceback (lua_State *L) { if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ lua_pushvalue(L, arg + 1); /* return it untouched */ else { - int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0); + int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0); luaL_traceback(L, L1, msg, level); } return 1; diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 9097f671..6986bf0f 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,18 +1,19 @@ /* -** $Id: ldebug.c,v 2.90.1.3 2013/05/16 16:04:15 roberto Exp $ +** $Id: ldebug.c,v 2.110 2015/01/02 12:52:22 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ +#define ldebug_c +#define LUA_CORE + +#include "lprefix.h" + #include #include #include - -#define ldebug_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -50,7 +51,7 @@ static int currentline (CallInfo *ci) { /* ** this function can be called asynchronous (e.g. during a signal) */ -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { +LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { if (func == NULL || mask == 0) { /* turn off hooks? */ mask = 0; func = NULL; @@ -61,7 +62,6 @@ LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { L->basehookcount = count; resethookcount(L); L->hookmask = cast_byte(mask); - return 1; } @@ -98,14 +98,14 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { static const char *upvalname (Proto *p, int uv) { - TString *s = check_exp(uv < p->sp->sizeupvalues, p->sp->upvalues[uv].name); + TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); } static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - int nparams = clLvalue(ci->func)->p->sp->numparams; + int nparams = clLvalue(ci->func)->p->numparams; if (n >= ci->u.l.base - ci->func - nparams) return NULL; /* no such vararg */ else { @@ -167,9 +167,10 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = 0; /* to avoid warnings */ const char *name = findlocal(L, ar->i_ci, n, &pos); lua_lock(L); - if (name) + if (name) { setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ + L->top--; /* pop value */ + } lua_unlock(L); return name; } @@ -183,7 +184,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { ar->what = "C"; } else { - SharedProto *p = cl->l.p->sp; + Proto *p = cl->l.p; ar->source = p->source ? getstr(p->source) : "=?"; ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; @@ -201,12 +202,12 @@ static void collectvalidlines (lua_State *L, Closure *f) { else { int i; TValue v; - int *lineinfo = f->l.p->sp->lineinfo; + int *lineinfo = f->l.p->lineinfo; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue(L, L->top, t); /* push it on stack */ api_incr_top(L); setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < f->l.p->sp->sizelineinfo; i++) /* for all lines with code */ + for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ } } @@ -232,8 +233,8 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, ar->nparams = 0; } else { - ar->isvararg = f->l.p->sp->is_vararg; - ar->nparams = f->l.p->sp->numparams; + ar->isvararg = f->l.p->is_vararg; + ar->nparams = f->l.p->numparams; } break; } @@ -272,7 +273,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { if (*what == '>') { ci = NULL; func = L->top - 1; - api_check(L, ttisfunction(func), "function expected"); + api_check(ttisfunction(func), "function expected"); what++; /* skip the '>' */ L->top--; /* pop function */ } @@ -342,7 +343,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) { int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->sp->code[pc]; + Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); int a = GETARG_A(i); switch (op) { @@ -366,18 +367,13 @@ static int findsetreg (Proto *p, int lastpc, int reg) { case OP_JMP: { int b = GETARG_sBx(i); int dest = pc + 1 + b; - /* jump is forward and do not skip `lastpc'? */ + /* jump is forward and do not skip 'lastpc'? */ if (pc < dest && dest <= lastpc) { if (dest > jmptarget) jmptarget = dest; /* update 'jmptarget' */ } break; } - case OP_TEST: { - if (reg == a) /* jumped code can change 'a' */ - setreg = filterpc(pc, jmptarget); - break; - } default: if (testAMode(op) && reg == a) /* any instruction that set A */ setreg = filterpc(pc, jmptarget); @@ -397,7 +393,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, /* else try symbolic execution */ pc = findsetreg(p, lastpc, reg); if (pc != -1) { /* could find instruction? */ - Instruction i = p->sp->code[pc]; + Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); switch (op) { case OP_MOVE: { @@ -423,7 +419,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, case OP_LOADK: case OP_LOADKX: { int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->sp->code[pc + 1]); + : GETARG_Ax(p->code[pc + 1]); if (ttisstring(&p->k[b])) { *name = svalue(&p->k[b]); return "constant"; @@ -443,10 +439,14 @@ static const char *getobjname (Proto *p, int lastpc, int reg, static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - TMS tm; + TMS tm = (TMS)0; /* to avoid warnings */ Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ - Instruction i = p->sp->code[pc]; /* calling instruction */ + Instruction i = p->code[pc]; /* calling instruction */ + if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ + *name = "?"; + return "hook"; + } switch (GET_OPCODE(i)) { case OP_CALL: case OP_TAILCALL: /* get function name */ @@ -456,25 +456,27 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { return "for iterator"; } /* all other instructions can call only through metamethods */ - case OP_SELF: - case OP_GETTABUP: - case OP_GETTABLE: tm = TM_INDEX; break; - case OP_SETTABUP: - case OP_SETTABLE: tm = TM_NEWINDEX; break; - case OP_EQ: tm = TM_EQ; break; - case OP_ADD: tm = TM_ADD; break; - case OP_SUB: tm = TM_SUB; break; - case OP_MUL: tm = TM_MUL; break; - case OP_DIV: tm = TM_DIV; break; - case OP_MOD: tm = TM_MOD; break; - case OP_POW: tm = TM_POW; break; + case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: + tm = TM_INDEX; + break; + case OP_SETTABUP: case OP_SETTABLE: + tm = TM_NEWINDEX; + break; + case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: + case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: + case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { + int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */ + tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */ + break; + } case OP_UNM: tm = TM_UNM; break; + case OP_BNOT: tm = TM_BNOT; break; case OP_LEN: tm = TM_LEN; break; + case OP_CONCAT: tm = TM_CONCAT; break; + case OP_EQ: tm = TM_EQ; break; case OP_LT: tm = TM_LT; break; case OP_LE: tm = TM_LE; break; - case OP_CONCAT: tm = TM_CONCAT; break; - default: - return NULL; /* else no useful name can be found */ + default: lua_assert(0); /* other instructions cannot call a function */ } *name = getstr(G(L)->tmname[tm]); return "metamethod"; @@ -485,17 +487,21 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { /* -** only ANSI way to check whether a pointer points to an array -** (used only for error messages, so efficiency is not a big concern) +** The subtraction of two potentially unrelated pointers is +** not ISO C, but it should not crash a program; the subsequent +** checks are ISO C and ensure a correct result. */ static int isinstack (CallInfo *ci, const TValue *o) { - StkId p; - for (p = ci->u.l.base; p < ci->top; p++) - if (o == p) return 1; - return 0; + ptrdiff_t i = o - ci->u.l.base; + return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o); } +/* +** Checks whether value 'o' came from an upvalue. (That can only happen +** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on +** upvalues.) +*/ static const char *getupvalname (CallInfo *ci, const TValue *o, const char **name) { LClosure *c = ci_func(ci); @@ -510,10 +516,9 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, } -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { +static const char *varinfo (lua_State *L, const TValue *o) { + const char *name = NULL; /* to avoid warnings */ CallInfo *ci = L->ci; - const char *name = NULL; - const char *t = objtypename(o); const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ @@ -521,26 +526,39 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { kind = getobjname(ci_func(ci)->p, currentpc(ci), cast_int(o - ci->u.l.base), &name); } - if (kind) - luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", - op, kind, name, t); - else - luaG_runerror(L, "attempt to %s a %s value", op, t); + return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } -l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { - if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; - lua_assert(!ttisstring(p1) && !ttisnumber(p1)); +l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { + const char *t = objtypename(o); + luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); +} + + +l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { + if (ttisstring(p1) || cvt2str(p1)) p1 = p2; luaG_typeerror(L, p1, "concatenate"); } -l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { - TValue temp; - if (luaV_tonumber(p1, &temp) == NULL) - p2 = p1; /* first operand is wrong */ - luaG_typeerror(L, p2, "perform arithmetic on"); +l_noret luaG_opinterror (lua_State *L, const TValue *p1, + const TValue *p2, const char *msg) { + lua_Number temp; + if (!tonumber(p1, &temp)) /* first operand is wrong? */ + p2 = p1; /* now second is wrong */ + luaG_typeerror(L, p2, msg); +} + + +/* +** Error when both values are convertible to numbers, but not to integers +*/ +l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { + lua_Integer temp; + if (!tointeger(p1, &temp)) + p2 = p1; + luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } @@ -559,7 +577,7 @@ static void addinfo (lua_State *L, const char *msg) { if (isLua(ci)) { /* is Lua code? */ char buff[LUA_IDSIZE]; /* add file:line information */ int line = currentline(ci); - TString *src = ci_func(ci)->p->sp->source; + TString *src = ci_func(ci)->p->source; if (src) luaO_chunkid(buff, getstr(src), LUA_IDSIZE); else { /* no source available; use "?" instead */ @@ -573,10 +591,9 @@ static void addinfo (lua_State *L, const char *msg) { l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); - if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; + L->top++; /* assume EXTRA_STACK */ luaD_call(L, L->top - 2, 1, 0); /* call it */ } luaD_throw(L, LUA_ERRRUN); @@ -591,3 +608,36 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { luaG_errormsg(L); } + +void luaG_traceexec (lua_State *L) { + CallInfo *ci = L->ci; + lu_byte mask = L->hookmask; + int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0); + if (counthook) + resethookcount(L); /* reset count */ + if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ + ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ + return; /* do not call hook again (VM yielded, so it did not move) */ + } + if (counthook) + luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ + if (mask & LUA_MASKLINE) { + Proto *p = ci_func(ci)->p; + int npc = pcRel(ci->u.l.savedpc, p); + int newline = getfuncline(p, npc); + if (npc == 0 || /* call linehook when enter a new function, */ + ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ + newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ + luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ + } + L->oldpc = ci->u.l.savedpc; + if (L->status == LUA_YIELD) { /* did hook yield? */ + if (counthook) + L->hookcount = 1; /* undo decrement to zero */ + ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ + ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ + ci->func = L->top - 1; /* protect stack below results */ + luaD_throw(L, LUA_YIELD); + } +} + diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 04d646df..0d8966ca 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldebug.h,v 2.12 2014/11/10 14:46:05 roberto Exp $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -11,9 +11,9 @@ #include "lstate.h" -#define pcRel(pc, p) (cast(int, (pc) - (p)->sp->code) - 1) +#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) -#define getfuncline(f,pc) (((f)->sp->lineinfo) ? (f)->sp->lineinfo[pc] : 0) +#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) #define resethookcount(L) (L->hookcount = L->basehookcount) @@ -23,12 +23,18 @@ LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); -LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); -LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, +LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, + const TValue *p2); +LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, + const TValue *p2, + const char *msg); +LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); LUAI_FUNC l_noret luaG_errormsg (lua_State *L); +LUAI_FUNC void luaG_traceexec (lua_State *L); + #endif diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index ee5509f6..6159e51d 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,17 +1,19 @@ /* -** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $ +** $Id: ldo.c,v 2.135 2014/11/11 17:13:39 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ +#define ldo_c +#define LUA_CORE + +#include "lprefix.h" + #include #include #include -#define ldo_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -33,6 +35,8 @@ +#define errorstatus(s) ((s) > LUA_YIELD) + /* ** {====================================================== @@ -46,30 +50,33 @@ ** C++ code, with _longjmp/_setjmp when asked to use them, and with ** longjmp/setjmp otherwise. */ -#if !defined(LUAI_THROW) +#if !defined(LUAI_THROW) /* { */ + +#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */ -#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* C++ exceptions */ #define LUAI_THROW(L,c) throw(c) #define LUAI_TRY(L,c,a) \ try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } #define luai_jmpbuf int /* dummy variable */ -#elif defined(LUA_USE_ULONGJMP) -/* in Unix, try _longjmp/_setjmp (more efficient) */ +#elif defined(LUA_USE_POSIX) /* }{ */ + +/* in POSIX, try _longjmp/_setjmp (more efficient) */ #define LUAI_THROW(L,c) _longjmp((c)->b, 1) #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } #define luai_jmpbuf jmp_buf -#else -/* default handling with long jumps */ +#else /* }{ */ + +/* ISO C handling with long jumps */ #define LUAI_THROW(L,c) longjmp((c)->b, 1) #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } #define luai_jmpbuf jmp_buf -#endif +#endif /* } */ -#endif +#endif /* } */ @@ -106,15 +113,19 @@ l_noret luaD_throw (lua_State *L, int errcode) { LUAI_THROW(L, L->errorJmp); /* jump to it */ } else { /* thread has no error handler */ + global_State *g = G(L); L->status = cast_byte(errcode); /* mark it as dead */ - if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ - luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ + if (g->mainthread->errorJmp) { /* main thread has a handler? */ + setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ + luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ - if (G(L)->panic) { /* panic function? */ + if (g->panic) { /* panic function? */ + seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ + if (L->ci->top < L->top) + L->ci->top = L->top; /* pushing msg. can break this invariant */ lua_unlock(L); - G(L)->panic(L); /* call it (last chance to jump out) */ + g->panic(L); /* call panic function (last chance to jump out) */ } abort(); } @@ -141,10 +152,10 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { static void correctstack (lua_State *L, TValue *oldstack) { CallInfo *ci; - GCObject *up; + UpVal *up; L->top = (L->top - oldstack) + L->stack; - for (up = L->openupval; up != NULL; up = up->gch.next) - gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; + for (up = L->openupval; up != NULL; up = up->u.open.next) + up->v = (up->v - oldstack) + L->stack; for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top = (ci->top - oldstack) + L->stack; ci->func = (ci->func - oldstack) + L->stack; @@ -206,7 +217,11 @@ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; - if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */ + if (L->stacksize > LUAI_MAXSTACK) /* was handling stack overflow? */ + luaE_freeCI(L); /* free all CIs (list grew because of an error) */ + else + luaE_shrinkCI(L); /* shrink list */ + if (inuse > LUAI_MAXSTACK || /* still handling stack overflow? */ goodsize >= L->stacksize) /* would grow instead of shrink? */ condmovestack(L); /* don't change stack (change only for debugging) */ else @@ -254,7 +269,7 @@ static void callhook (lua_State *L, CallInfo *ci) { } -static StkId adjust_varargs (lua_State *L, SharedProto *p, int actual) { +static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { int i; int nfixargs = p->numparams; StkId base, fixed; @@ -271,18 +286,21 @@ static StkId adjust_varargs (lua_State *L, SharedProto *p, int actual) { } -static StkId tryfuncTM (lua_State *L, StkId func) { +/* +** Check whether __call metafield of 'func' is a function. If so, put +** it in stack below original 'func' so that 'luaD_precall' can call +** it. Raise an error if __call metafield is not a function. +*/ +static void tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); StkId p; - ptrdiff_t funcr = savestack(L, func); if (!ttisfunction(tm)) luaG_typeerror(L, func, "call"); - /* Open a hole inside the stack at `func' */ - for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); - incr_top(L); - func = restorestack(L, funcr); /* previous call may change stack */ + /* Open a hole inside the stack at 'func' */ + for (p = L->top; p > func; p--) + setobjs2s(L, p, p-1); + L->top++; /* slot ensured by caller */ setobj2s(L, func, tm); /* tag method is the new function to be called */ - return func; } @@ -324,7 +342,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { } case LUA_TLCL: { /* Lua function: prepare its call */ StkId base; - SharedProto *p = clLvalue(func)->p->sp; + Proto *p = clLvalue(func)->p; n = cast_int(L->top - func) - 1; /* number of real arguments */ luaD_checkstack(L, p->maxstacksize); for (; n < p->numparams; n++) @@ -352,7 +370,9 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { return 0; } default: { /* not a function */ - func = tryfuncTM(L, func); /* retry with 'function' tag method */ + luaD_checkstack(L, 1); /* ensure space for metamethod */ + func = restorestack(L, funcr); /* previous call may change stack */ + tryfuncTM(L, func); /* try to get '__call' metamethod */ return luaD_precall(L, func, nresults); /* now it must be a function */ } } @@ -405,24 +425,27 @@ void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { } -static void finishCcall (lua_State *L) { +/* +** Completes the execution of an interrupted C function, calling its +** continuation function. +*/ +static void finishCcall (lua_State *L, int status) { CallInfo *ci = L->ci; int n; - lua_assert(ci->u.c.k != NULL); /* must have a continuation */ - lua_assert(L->nny == 0); + /* must have a continuation and must be able to call it */ + lua_assert(ci->u.c.k != NULL && L->nny == 0); + /* error status can only happen in a protected call */ + lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ L->errfunc = ci->u.c.old_errfunc; } - /* finish 'lua_callk'/'lua_pcall' */ + /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already + handled */ adjustresults(L, ci->nresults); /* call continuation function */ - if (!(ci->callstatus & CIST_STAT)) /* no call status? */ - ci->u.c.status = LUA_YIELD; /* 'default' status */ - lua_assert(ci->u.c.status != LUA_OK); - ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; lua_unlock(L); - n = (*ci->u.c.k)(L); + n = (*ci->u.c.k)(L, status, ci->u.c.ctx); lua_lock(L); api_checknelems(L, n); /* finish 'luaD_precall' */ @@ -430,13 +453,20 @@ static void finishCcall (lua_State *L) { } +/* +** Executes "full continuation" (everything in the stack) of a +** previously interrupted coroutine until the stack is empty (or another +** interruption long-jumps out of the loop). If the coroutine is +** recovering from an error, 'ud' points to the error status, which must +** be passed to the first continuation function (otherwise the default +** status is LUA_YIELD). +*/ static void unroll (lua_State *L, void *ud) { - UNUSED(ud); - for (;;) { - if (L->ci == &L->base_ci) /* stack is empty? */ - return; /* coroutine finished normally */ + if (ud != NULL) /* error status? */ + finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ + while (L->ci != &L->base_ci) { /* something in the stack */ if (!isLua(L->ci)) /* C function? */ - finishCcall(L); + finishCcall(L, LUA_YIELD); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ luaV_execute(L); /* execute down to higher C 'boundary' */ @@ -446,7 +476,8 @@ static void unroll (lua_State *L, void *ud) { /* -** check whether thread has a suspended protected call +** Try to find a suspended protected call (a "recover point") for the +** given thread. */ static CallInfo *findpcall (lua_State *L) { CallInfo *ci; @@ -458,6 +489,11 @@ static CallInfo *findpcall (lua_State *L) { } +/* +** Recovers from an error in a coroutine. Finds a recover point (if +** there is one) and completes the execution of the interrupted +** 'luaD_pcall'. If there is no recover point, returns zero. +*/ static int recover (lua_State *L, int status) { StkId oldtop; CallInfo *ci = findpcall(L); @@ -467,12 +503,10 @@ static int recover (lua_State *L, int status) { luaF_close(L, oldtop); seterrorobj(L, status, oldtop); L->ci = ci; - L->allowhook = ci->u.c.old_allowhook; + L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); L->errfunc = ci->u.c.old_errfunc; - ci->callstatus |= CIST_STAT; /* call has error status */ - ci->u.c.status = status; /* (here it is) */ return 1; /* continue running the coroutine */ } @@ -491,7 +525,11 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { /* -** do the work for 'lua_resume' in protected mode +** Do the work for 'lua_resume' in protected mode. Most of the work +** depends on the status of the coroutine: initial state, suspended +** inside a hook, or regularly suspended (optionally with a continuation +** function), plus erroneous cases: non-suspended coroutine or dead +** coroutine. */ static void resume (lua_State *L, void *ud) { int nCcalls = L->nCcalls; @@ -509,24 +547,22 @@ static void resume (lua_State *L, void *ud) { else if (L->status != LUA_YIELD) resume_error(L, "cannot resume dead coroutine", firstArg); else { /* resuming from previous yield */ - L->status = LUA_OK; + L->status = LUA_OK; /* mark that it is running (again) */ ci->func = restorestack(L, ci->extra); if (isLua(ci)) /* yielded inside a hook? */ luaV_execute(L); /* just continue running Lua code */ else { /* 'common' yield */ - if (ci->u.c.k != NULL) { /* does it have a continuation? */ + if (ci->u.c.k != NULL) { /* does it have a continuation function? */ int n; - ci->u.c.status = LUA_YIELD; /* 'default' status */ - ci->callstatus |= CIST_YIELDED; lua_unlock(L); - n = (*ci->u.c.k)(L); /* call continuation */ + n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ } luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ } - unroll(L, NULL); + unroll(L, NULL); /* run continuation */ } lua_assert(nCcalls == L->nCcalls); } @@ -534,7 +570,7 @@ static void resume (lua_State *L, void *ud) { LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { int status; - int oldnny = L->nny; /* save 'nny' */ + int oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); luai_userstateresume(L, nargs); L->nCcalls = (from) ? from->nCcalls + 1 : 1; @@ -543,18 +579,17 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { status = luaD_rawrunprotected(L, resume, L->top - nargs); if (status == -1) /* error calling 'lua_resume'? */ status = LUA_ERRRUN; - else { /* yield or regular error */ - while (status != LUA_OK && status != LUA_YIELD) { /* error? */ - if (recover(L, status)) /* recover point? */ - status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ - else { /* unrecoverable error */ - L->status = cast_byte(status); /* mark thread as `dead' */ - seterrorobj(L, status, L->top); - L->ci->top = L->top; - break; - } + else { /* continue running after recoverable errors */ + while (errorstatus(status) && recover(L, status)) { + /* unroll continuation */ + status = luaD_rawrunprotected(L, unroll, &status); } - lua_assert(status == L->status); + if (errorstatus(status)) { /* unrecoverable error? */ + L->status = cast_byte(status); /* mark thread as 'dead' */ + seterrorobj(L, status, L->top); /* push error message */ + L->ci->top = L->top; + } + else lua_assert(status == L->status); /* normal end or yield */ } L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; @@ -564,7 +599,13 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { } -LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { +LUA_API int lua_isyieldable (lua_State *L) { + return (L->nny == 0); +} + + +LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, + lua_KFunction k) { CallInfo *ci = L->ci; luai_userstateyield(L, nresults); lua_lock(L); @@ -578,7 +619,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { L->status = LUA_YIELD; ci->extra = savestack(L, ci->func); /* save current 'func' */ if (isLua(ci)) { /* inside a hook? */ - api_check(L, k == NULL, "hooks cannot continue after yielding"); + api_check(k == NULL, "hooks cannot continue after yielding"); } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ @@ -619,7 +660,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, /* ** Execute a protected parser. */ -struct SParser { /* data to `f_parser' */ +struct SParser { /* data to 'f_parser' */ ZIO *z; Mbuffer buff; /* dynamic structure used by the scanner */ Dyndata dyd; /* dynamic structures used by the parser */ @@ -631,15 +672,14 @@ struct SParser { /* data to `f_parser' */ static void checkmode (lua_State *L, const char *mode, const char *x) { if (mode && strchr(mode, x[0]) == NULL) { luaO_pushfstring(L, - "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); + "attempt to load a %s chunk (mode is '%s')", x, mode); luaD_throw(L, LUA_ERRSYNTAX); } } static void f_parser (lua_State *L, void *ud) { - int i; - Closure *cl; + LClosure *cl; struct SParser *p = cast(struct SParser *, ud); int c = zgetc(p->z); /* read first character */ if (c == LUA_SIGNATURE[0]) { @@ -650,12 +690,8 @@ static void f_parser (lua_State *L, void *ud) { checkmode(L, p->mode, "text"); cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); } - lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); - for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */ - UpVal *up = luaF_newupval(L); - cl->l.upvals[i] = up; - luaC_objbarrier(L, cl, up); - } + lua_assert(cl->nupvalues == cl->p->sizeupvalues); + luaF_initupvals(L, cl); } diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index d3d3082c..05745c8a 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldo.h,v 2.21 2014/10/25 11:50:46 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -23,7 +23,7 @@ #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) -/* type of protected functions, to be ran by `runprotected' */ +/* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index dd232ecf..b6c7114f 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -1,174 +1,214 @@ /* -** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ldump.c,v 2.34 2014/11/02 19:19:04 roberto Exp $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ -#include - #define ldump_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "lobject.h" #include "lstate.h" #include "lundump.h" + typedef struct { - lua_State* L; - lua_Writer writer; - void* data; - int strip; - int status; + lua_State *L; + lua_Writer writer; + void *data; + int strip; + int status; } DumpState; -#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) -#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) -static void DumpBlock(const void* b, size_t size, DumpState* D) -{ - if (D->status==0) - { - lua_unlock(D->L); - D->status=(*D->writer)(D->L,b,size,D->data); - lua_lock(D->L); - } -} +/* +** All high-level dumps go through DumpVector; you can change it to +** change the endianness of the result +*/ +#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) -static void DumpChar(int y, DumpState* D) -{ - char x=(char)y; - DumpVar(x,D); -} +#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) -static void DumpInt(int x, DumpState* D) -{ - DumpVar(x,D); -} -static void DumpNumber(lua_Number x, DumpState* D) -{ - DumpVar(x,D); -} - -static void DumpVector(const void* b, int n, size_t size, DumpState* D) -{ - DumpInt(n,D); - DumpMem(b,n,size,D); -} - -static void DumpString(const TString* s, DumpState* D) -{ - if (s==NULL) - { - size_t size=0; - DumpVar(size,D); - } - else - { - size_t size=s->tsv.len+1; /* include trailing '\0' */ - DumpVar(size,D); - DumpBlock(getstr(s),size*sizeof(char),D); - } -} - -#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) - -static void DumpFunction(const Proto* f, DumpState* D); - -static void DumpConstants(const Proto* f, DumpState* D) -{ - int i,n=f->sp->sizek; - DumpInt(n,D); - for (i=0; ik[i]; - DumpChar(ttypenv(o),D); - switch (ttypenv(o)) - { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpChar(bvalue(o),D); - break; - case LUA_TNUMBER: - DumpNumber(nvalue(o),D); - break; - case LUA_TSTRING: - DumpString(rawtsvalue(o),D); - break; - default: lua_assert(0); +static void DumpBlock (const void *b, size_t size, DumpState *D) { + if (D->status == 0) { + lua_unlock(D->L); + D->status = (*D->writer)(D->L, b, size, D->data); + lua_lock(D->L); } - } - n=f->sp->sizep; - DumpInt(n,D); - for (i=0; ip[i],D); } -static void DumpUpvalues(const Proto* f, DumpState* D) -{ - int i,n=f->sp->sizeupvalues; - DumpInt(n,D); - for (i=0; isp->upvalues[i].instack,D); - DumpChar(f->sp->upvalues[i].idx,D); - } + +#define DumpVar(x,D) DumpVector(&x,1,D) + + +static void DumpByte (int y, DumpState *D) { + lu_byte x = (lu_byte)y; + DumpVar(x, D); } -static void DumpDebug(const SharedProto* f, DumpState* D) -{ - int i,n; - DumpString((D->strip) ? NULL : f->source,D); - n= (D->strip) ? 0 : f->sizelineinfo; - DumpVector(f->lineinfo,n,sizeof(int),D); - n= (D->strip) ? 0 : f->sizelocvars; - DumpInt(n,D); - for (i=0; ilocvars[i].varname,D); - DumpInt(f->locvars[i].startpc,D); - DumpInt(f->locvars[i].endpc,D); - } - n= (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n,D); - for (i=0; iupvalues[i].name,D); + +static void DumpInt (int x, DumpState *D) { + DumpVar(x, D); } -static void DumpFunction(const Proto* f, DumpState* D) -{ - const SharedProto *sp = f->sp; - DumpInt(sp->linedefined,D); - DumpInt(sp->lastlinedefined,D); - DumpChar(sp->numparams,D); - DumpChar(sp->is_vararg,D); - DumpChar(sp->maxstacksize,D); - DumpCode(sp,D); - DumpConstants(f,D); - DumpUpvalues(f,D); - DumpDebug(sp,D); + +static void DumpNumber (lua_Number x, DumpState *D) { + DumpVar(x, D); } -static void DumpHeader(DumpState* D) -{ - lu_byte h[LUAC_HEADERSIZE]; - luaU_header(h); - DumpBlock(h,LUAC_HEADERSIZE,D); + +static void DumpInteger (lua_Integer x, DumpState *D) { + DumpVar(x, D); } + +static void DumpString (const TString *s, DumpState *D) { + if (s == NULL) + DumpByte(0, D); + else { + size_t size = s->len + 1; /* include trailing '\0' */ + if (size < 0xFF) + DumpByte(cast_int(size), D); + else { + DumpByte(0xFF, D); + DumpVar(size, D); + } + DumpVector(getstr(s), size - 1, D); /* no need to save '\0' */ + } +} + + +static void DumpCode (const Proto *f, DumpState *D) { + DumpInt(f->sizecode, D); + DumpVector(f->code, f->sizecode, D); +} + + +static void DumpFunction(const Proto *f, TString *psource, DumpState *D); + +static void DumpConstants (const Proto *f, DumpState *D) { + int i; + int n = f->sizek; + DumpInt(n, D); + for (i = 0; i < n; i++) { + const TValue *o = &f->k[i]; + DumpByte(ttype(o), D); + switch (ttype(o)) { + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + DumpByte(bvalue(o), D); + break; + case LUA_TNUMFLT: + DumpNumber(fltvalue(o), D); + break; + case LUA_TNUMINT: + DumpInteger(ivalue(o), D); + break; + case LUA_TSHRSTR: + case LUA_TLNGSTR: + DumpString(tsvalue(o), D); + break; + default: + lua_assert(0); + } + } +} + + +static void DumpProtos (const Proto *f, DumpState *D) { + int i; + int n = f->sizep; + DumpInt(n, D); + for (i = 0; i < n; i++) + DumpFunction(f->p[i], f->source, D); +} + + +static void DumpUpvalues (const Proto *f, DumpState *D) { + int i, n = f->sizeupvalues; + DumpInt(n, D); + for (i = 0; i < n; i++) { + DumpByte(f->upvalues[i].instack, D); + DumpByte(f->upvalues[i].idx, D); + } +} + + +static void DumpDebug (const Proto *f, DumpState *D) { + int i, n; + n = (D->strip) ? 0 : f->sizelineinfo; + DumpInt(n, D); + DumpVector(f->lineinfo, n, D); + n = (D->strip) ? 0 : f->sizelocvars; + DumpInt(n, D); + for (i = 0; i < n; i++) { + DumpString(f->locvars[i].varname, D); + DumpInt(f->locvars[i].startpc, D); + DumpInt(f->locvars[i].endpc, D); + } + n = (D->strip) ? 0 : f->sizeupvalues; + DumpInt(n, D); + for (i = 0; i < n; i++) + DumpString(f->upvalues[i].name, D); +} + + +static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { + if (D->strip || f->source == psource) + DumpString(NULL, D); /* no debug info or same source as its parent */ + else + DumpString(f->source, D); + DumpInt(f->linedefined, D); + DumpInt(f->lastlinedefined, D); + DumpByte(f->numparams, D); + DumpByte(f->is_vararg, D); + DumpByte(f->maxstacksize, D); + DumpCode(f, D); + DumpConstants(f, D); + DumpUpvalues(f, D); + DumpProtos(f, D); + DumpDebug(f, D); +} + + +static void DumpHeader (DumpState *D) { + DumpLiteral(LUA_SIGNATURE, D); + DumpByte(LUAC_VERSION, D); + DumpByte(LUAC_FORMAT, D); + DumpLiteral(LUAC_DATA, D); + DumpByte(sizeof(int), D); + DumpByte(sizeof(size_t), D); + DumpByte(sizeof(Instruction), D); + DumpByte(sizeof(lua_Integer), D); + DumpByte(sizeof(lua_Number), D); + DumpInteger(LUAC_INT, D); + DumpNumber(LUAC_NUM, D); +} + + /* ** dump Lua function as precompiled chunk */ -int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) -{ - DumpState D; - D.L=L; - D.writer=w; - D.data=data; - D.strip=strip; - D.status=0; - DumpHeader(&D); - DumpFunction(f,&D); - return D.status; +int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, + int strip) { + DumpState D; + D.L = L; + D.writer = w; + D.data = data; + D.strip = strip; + D.status = 0; + DumpHeader(&D); + DumpByte(f->sizeupvalues, &D); + DumpFunction(f, NULL, &D); + return D.status; } + diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 9dbb034e..67967dab 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -1,15 +1,17 @@ /* -** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ - -#include - #define lfunc_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "lfunc.h" @@ -20,151 +22,123 @@ -Closure *luaF_newCclosure (lua_State *L, int n) { - Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; - c->c.nupvalues = cast_byte(n); +CClosure *luaF_newCclosure (lua_State *L, int n) { + GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); + CClosure *c = gco2ccl(o); + c->nupvalues = cast_byte(n); return c; } -Closure *luaF_newLclosure (lua_State *L, int n) { - Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; - c->l.p = NULL; - c->l.nupvalues = cast_byte(n); - while (n--) c->l.upvals[n] = NULL; +LClosure *luaF_newLclosure (lua_State *L, int n) { + GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); + LClosure *c = gco2lcl(o); + c->p = NULL; + c->nupvalues = cast_byte(n); + while (n--) c->upvals[n] = NULL; return c; } - -UpVal *luaF_newupval (lua_State *L) { - UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; - uv->v = &uv->u.value; - setnilvalue(uv->v); - return uv; +/* +** fill a closure with new closed upvalues +*/ +void luaF_initupvals (lua_State *L, LClosure *cl) { + int i; + for (i = 0; i < cl->nupvalues; i++) { + UpVal *uv = luaM_new(L, UpVal); + uv->refcount = 1; + uv->v = &uv->u.value; /* make it closed */ + setnilvalue(uv->v); + cl->upvals[i] = uv; + } } UpVal *luaF_findupval (lua_State *L, StkId level) { - global_State *g = G(L); - GCObject **pp = &L->openupval; + UpVal **pp = &L->openupval; UpVal *p; UpVal *uv; - while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { - GCObject *o = obj2gco(p); - lua_assert(p->v != &p->u.value); - lua_assert(!isold(o) || isold(obj2gco(L))); - if (p->v == level) { /* found a corresponding upvalue? */ - if (isdead(g, o)) /* is it dead? */ - changewhite(o); /* resurrect it */ - return p; - } - pp = &p->next; + lua_assert(isintwups(L) || L->openupval == NULL); + while (*pp != NULL && (p = *pp)->v >= level) { + lua_assert(upisopen(p)); + if (p->v == level) /* found a corresponding upvalue? */ + return p; /* return it */ + pp = &p->u.open.next; } - /* not found: create a new one */ - uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; + /* not found: create a new upvalue */ + uv = luaM_new(L, UpVal); + uv->refcount = 0; + uv->u.open.next = *pp; /* link it to list of open upvalues */ + uv->u.open.touched = 1; + *pp = uv; uv->v = level; /* current value lives in the stack */ - uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ - uv->u.l.next = g->uvhead.u.l.next; - uv->u.l.next->u.l.prev = uv; - g->uvhead.u.l.next = uv; - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ + L->twups = G(L)->twups; /* link it to the list */ + G(L)->twups = L; + } return uv; } -static void unlinkupval (UpVal *uv) { - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); - uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ - uv->u.l.prev->u.l.next = uv->u.l.next; -} - - -void luaF_freeupval (lua_State *L, UpVal *uv) { - if (uv->v != &uv->u.value) /* is it open? */ - unlinkupval(uv); /* remove from open list */ - luaM_free(L, uv); /* free upvalue */ -} - - void luaF_close (lua_State *L, StkId level) { UpVal *uv; - global_State *g = G(L); - while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { - GCObject *o = obj2gco(uv); - lua_assert(!isblack(o) && uv->v != &uv->u.value); - L->openupval = uv->next; /* remove from `open' list */ - if (isdead(g, o)) - luaF_freeupval(L, uv); /* free upvalue */ + while (L->openupval != NULL && (uv = L->openupval)->v >= level) { + lua_assert(upisopen(uv)); + L->openupval = uv->u.open.next; /* remove from 'open' list */ + if (uv->refcount == 0) /* no references? */ + luaM_free(L, uv); /* free upvalue */ else { - unlinkupval(uv); /* remove upvalue from 'uvhead' list */ setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ uv->v = &uv->u.value; /* now current value lives here */ - gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ - g->allgc = o; - luaC_checkupvalcolor(g, uv); + luaC_upvalbarrier(L, uv); } } } -Proto *luaF_newproto (lua_State *L, SharedProto *sp) { - Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; +Proto *luaF_newproto (lua_State *L) { + GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); + Proto *f = gco2p(o); f->k = NULL; - f->sp = NULL; + f->sizek = 0; f->p = NULL; + f->sizep = 0; + f->code = NULL; f->cache = NULL; - - if (sp == NULL) { - sp = luaM_new(L, SharedProto); - sp->l_G = G(L); - sp->sizek = 0; - sp->sizep = 0; - sp->code = NULL; - sp->sizecode = 0; - sp->lineinfo = NULL; - sp->sizelineinfo = 0; - sp->upvalues = NULL; - sp->sizeupvalues = 0; - sp->numparams = 0; - sp->is_vararg = 0; - sp->maxstacksize = 0; - sp->locvars = NULL; - sp->sizelocvars = 0; - sp->linedefined = 0; - sp->lastlinedefined = 0; - sp->source = NULL; - } - f->sp = sp; - + f->sizecode = 0; + f->lineinfo = NULL; + f->sizelineinfo = 0; + f->upvalues = NULL; + f->sizeupvalues = 0; + f->numparams = 0; + f->is_vararg = 0; + f->maxstacksize = 0; + f->locvars = NULL; + f->sizelocvars = 0; + f->linedefined = 0; + f->lastlinedefined = 0; + f->source = NULL; return f; } -static void -luaF_freesharedproto (lua_State *L, SharedProto *f) { - if (f == NULL || G(L) != f->l_G) - return; + +void luaF_freeproto (lua_State *L, Proto *f) { luaM_freearray(L, f->code, f->sizecode); + luaM_freearray(L, f->p, f->sizep); + luaM_freearray(L, f->k, f->sizek); luaM_freearray(L, f->lineinfo, f->sizelineinfo); luaM_freearray(L, f->locvars, f->sizelocvars); luaM_freearray(L, f->upvalues, f->sizeupvalues); luaM_free(L, f); } -void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->k, f->sp->sizek); - luaM_freearray(L, f->p, f->sp->sizep); - luaF_freesharedproto(L, f->sp); - luaM_free(L, f); -} - /* -** Look for n-th local variable at line `line' in function `func'. +** Look for n-th local variable at line 'line' in function 'func'. ** Returns NULL if not found. */ -const char *luaF_getlocalname (const Proto *fp, int local_number, int pc) { +const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { int i; - const SharedProto *f = fp->sp; for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { if (pc < f->locvars[i].endpc) { /* is variable active? */ local_number--; diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index ba315966..256d3cf9 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.8.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 roberto Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -18,14 +18,35 @@ cast(int, sizeof(TValue *)*((n)-1))) -LUAI_FUNC Proto *luaF_newproto (lua_State *L, SharedProto *sp); -LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); -LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); -LUAI_FUNC UpVal *luaF_newupval (lua_State *L); +/* test whether thread is in 'twups' list */ +#define isintwups(L) (L->twups != L) + + +/* +** Upvalues for Lua closures +*/ +struct UpVal { + TValue *v; /* points to stack or to its own value */ + lu_mem refcount; /* reference counter */ + union { + struct { /* (when open) */ + UpVal *next; /* linked list */ + int touched; /* mark to avoid cycles with dead threads */ + } open; + TValue value; /* the value (when closed) */ + } u; +}; + +#define upisopen(up) ((up)->v != &(up)->u.value) + + +LUAI_FUNC Proto *luaF_newproto (lua_State *L); +LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); +LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); +LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_close (lua_State *L, StkId level); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); -LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 51c11f5b..8b95fb67 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,14 +1,17 @@ /* -** $Id: lgc.c,v 2.140.1.2 2013/04/26 18:22:05 roberto Exp $ +** $Id: lgc.c,v 2.201 2014/12/20 13:58:15 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ -#include - #define lgc_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "ldebug.h" @@ -23,6 +26,11 @@ #include "ltm.h" +/* +** internal state for collector while inside the atomic phase. The +** collector should never be in this state while running regular code. +*/ +#define GCSinsideatomic (GCSpause + 1) /* ** cost of sweeping one element (the size of a small object divided @@ -33,8 +41,8 @@ /* maximum number of elements to sweep in each single step */ #define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) -/* maximum number of finalizers to call in each GC step */ -#define GCFINALIZENUM 4 +/* cost of calling one finalizer */ +#define GCFINALIZECOST GCSWEEPCOST /* @@ -52,18 +60,18 @@ /* -** 'makewhite' erases all color bits plus the old bit and then -** sets only the current white bit +** 'makewhite' erases all color bits then sets only the current white +** bit */ -#define maskcolors (~(bit2mask(BLACKBIT, OLDBIT) | WHITEBITS)) +#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) #define makewhite(g,x) \ - (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g))) + (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) -#define white2gray(x) resetbits(gch(x)->marked, WHITEBITS) -#define black2gray(x) resetbit(gch(x)->marked, BLACKBIT) +#define white2gray(x) resetbits(x->marked, WHITEBITS) +#define black2gray(x) resetbit(x->marked, BLACKBIT) -#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT) +#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) #define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n))) @@ -75,8 +83,8 @@ #define markvalue(g,o) { checkconsistency(o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } -#define markobject(g,t) { if ((t) && iswhite(obj2gco(t))) \ - reallymarkobject(g, obj2gco(t)); } +#define markobject(g,t) \ + { if ((t) && iswhite(t)) reallymarkobject(g, obj2gco(t)); } static void reallymarkobject (global_State *g, GCObject *o); @@ -95,9 +103,9 @@ static void reallymarkobject (global_State *g, GCObject *o); /* -** link table 'h' into list pointed by 'p' +** link collectable object 'o' into list pointed by 'p' */ -#define linktable(h,p) ((h)->gclist = *(p), *(p) = obj2gco(h)) +#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) /* @@ -107,21 +115,21 @@ static void reallymarkobject (global_State *g, GCObject *o); static void removeentry (Node *n) { lua_assert(ttisnil(gval(n))); if (valiswhite(gkey(n))) - setdeadvalue(gkey(n)); /* unused and unmarked key; remove it */ + setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */ } /* ** tells whether a key or value can be cleared from a weak ** table. Non-collectable objects are never removed from weak -** tables. Strings behave as `values', so are never removed too. for +** tables. Strings behave as 'values', so are never removed too. for ** other objects: if really collected, cannot keep them; for objects ** being finalized, keep them in keys, but not in values */ static int iscleared (global_State *g, const TValue *o) { if (!iscollectable(o)) return 0; else if (ttisstring(o)) { - markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */ + markobject(g, tsvalue(o)); /* strings are 'values', so are never weak */ return 0; } else return iswhite(gcvalue(o)); @@ -130,14 +138,14 @@ static int iscleared (global_State *g, const TValue *o) { /* ** barrier that moves collector forward, that is, mark the white object -** being pointed by a black object. +** being pointed by a black object. (If in sweep phase, clear the black +** object to white [sweep it] to avoid other barrier calls for this +** same object.) */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - lua_assert(g->gcstate != GCSpause); - lua_assert(gch(o)->tt != LUA_TTABLE); - if (keepinvariantout(g)) /* must keep invariant? */ + if (keepinvariant(g)) /* must keep invariant? */ reallymarkobject(g, v); /* restore invariant */ else { /* sweep phase */ lua_assert(issweepphase(g)); @@ -148,78 +156,52 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { /* ** barrier that moves collector backward, that is, mark the black object -** pointing to a white object as gray again. (Current implementation -** only works for tables; access to 'gclist' is not uniform across -** different types.) +** pointing to a white object as gray again. */ -void luaC_barrierback_ (lua_State *L, GCObject *o) { +void luaC_barrierback_ (lua_State *L, Table *t) { global_State *g = G(L); - lua_assert(isblack(o) && !isdead(g, o) && gch(o)->tt == LUA_TTABLE); - black2gray(o); /* make object gray (again) */ - gco2t(o)->gclist = g->grayagain; - g->grayagain = o; + lua_assert(isblack(t) && !isdead(g, t)); + black2gray(t); /* make table gray (again) */ + linkgclist(t, g->grayagain); } /* -** barrier for prototypes. When creating first closure (cache is -** NULL), use a forward barrier; this may be the only closure of the -** prototype (if it is a "regular" function, with a single instance) -** and the prototype may be big, so it is better to avoid traversing -** it again. Otherwise, use a backward barrier, to avoid marking all -** possible instances. +** barrier for assignments to closed upvalues. Because upvalues are +** shared among closures, it is impossible to know the color of all +** closures pointing to it. So, we assume that the object being assigned +** must be marked. */ -LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c) { +void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { global_State *g = G(L); - lua_assert(isblack(obj2gco(p))); - if (p->cache == NULL) { /* first time? */ - luaC_objbarrier(L, p, c); - } - else { /* use a backward barrier */ - black2gray(obj2gco(p)); /* make prototype gray (again) */ - p->gclist = g->grayagain; - g->grayagain = obj2gco(p); - } + GCObject *o = gcvalue(uv->v); + lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */ + if (keepinvariant(g)) + markobject(g, o); } -/* -** check color (and invariants) for an upvalue that was closed, -** i.e., moved into the 'allgc' list -*/ -void luaC_checkupvalcolor (global_State *g, UpVal *uv) { - GCObject *o = obj2gco(uv); - lua_assert(!isblack(o)); /* open upvalues are never black */ - if (isgray(o)) { - if (keepinvariant(g)) { - resetoldbit(o); /* see MOVE OLD rule */ - gray2black(o); /* it is being visited now */ - markvalue(g, uv->v); - } - else { - lua_assert(issweepphase(g)); - makewhite(g, o); - } - } +void luaC_fix (lua_State *L, GCObject *o) { + global_State *g = G(L); + lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ + white2gray(o); /* they will be gray forever */ + g->allgc = o->next; /* remove object from 'allgc' list */ + o->next = g->fixedgc; /* link it to 'fixedgc' list */ + g->fixedgc = o; } /* ** create a new collectable object (with given type and size) and link -** it to '*list'. 'offset' tells how many bytes to allocate before the -** object itself (used only by states). +** it to 'allgc' list. */ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, - int offset) { +GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { global_State *g = G(L); - char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz)); - GCObject *o = obj2gco(raw + offset); - if (list == NULL) - list = &g->allgc; /* standard list for collectable objects */ - gch(o)->marked = luaC_white(g); - gch(o)->tt = tt; - gch(o)->next = *list; - *list = o; + GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz)); + o->marked = luaC_white(g); + o->tt = tt; + o->next = g->allgc; + g->allgc = o; return o; } @@ -241,57 +223,49 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, ** upvalues are already linked in 'headuv' list.) */ static void reallymarkobject (global_State *g, GCObject *o) { - lu_mem size; + reentry: white2gray(o); - switch (gch(o)->tt) { + switch (o->tt) { case LUA_TSHRSTR: case LUA_TLNGSTR: { - size = sizestring(gco2ts(o)); - break; /* nothing else to mark; make it black */ - } - case LUA_TUSERDATA: { - Table *mt = gco2u(o)->metatable; - markobject(g, mt); - markobject(g, gco2u(o)->env); - size = sizeudata(gco2u(o)); + gray2black(o); + g->GCmemtrav += sizestring(gco2ts(o)); break; } - case LUA_TUPVAL: { - UpVal *uv = gco2uv(o); - markvalue(g, uv->v); - if (uv->v != &uv->u.value) /* open? */ - return; /* open upvalues remain gray */ - size = sizeof(UpVal); + case LUA_TUSERDATA: { + TValue uvalue; + markobject(g, gco2u(o)->metatable); /* mark its metatable */ + gray2black(o); + g->GCmemtrav += sizeudata(gco2u(o)); + getuservalue(g->mainthread, gco2u(o), &uvalue); + if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */ + o = gcvalue(&uvalue); + goto reentry; + } break; } case LUA_TLCL: { - gco2lcl(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2lcl(o), g->gray); + break; } case LUA_TCCL: { - gco2ccl(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2ccl(o), g->gray); + break; } case LUA_TTABLE: { - linktable(gco2t(o), &g->gray); - return; + linkgclist(gco2t(o), g->gray); + break; } case LUA_TTHREAD: { - gco2th(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2th(o), g->gray); + break; } case LUA_TPROTO: { - gco2p(o)->gclist = g->gray; - g->gray = o; - return; + linkgclist(gco2p(o), g->gray); + break; } - default: lua_assert(0); return; + default: lua_assert(0); break; } - gray2black(o); - g->GCmemtrav += size; } @@ -310,29 +284,41 @@ static void markmt (global_State *g) { */ static void markbeingfnz (global_State *g) { GCObject *o; - for (o = g->tobefnz; o != NULL; o = gch(o)->next) { - makewhite(g, o); - reallymarkobject(g, o); - } + for (o = g->tobefnz; o != NULL; o = o->next) + markobject(g, o); } /* -** mark all values stored in marked open upvalues. (See comment in -** 'lstate.h'.) +** Mark all values stored in marked open upvalues from non-marked threads. +** (Values from marked threads were already marked when traversing the +** thread.) Remove from the list threads that no longer have upvalues and +** not-marked threads. */ static void remarkupvals (global_State *g) { - UpVal *uv; - for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { - if (isgray(obj2gco(uv))) - markvalue(g, uv->v); + lua_State *thread; + lua_State **p = &g->twups; + while ((thread = *p) != NULL) { + lua_assert(!isblack(thread)); /* threads are never black */ + if (isgray(thread) && thread->openupval != NULL) + p = &thread->twups; /* keep marked thread with upvalues in the list */ + else { /* thread is not marked or without upvalues */ + UpVal *uv; + *p = thread->twups; /* remove thread from the list */ + thread->twups = thread; /* mark that it is out of list */ + for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { + if (uv->u.open.touched) { + markvalue(g, uv->v); /* remark upvalue's value */ + uv->u.open.touched = 0; + } + } + } } } /* -** mark root set and reset all gray lists, to start a new -** incremental (or full) collection +** mark root set and reset all gray lists, to start a new collection */ static void restartcollection (global_State *g) { g->gray = g->grayagain = NULL; @@ -352,12 +338,18 @@ static void restartcollection (global_State *g) { ** ======================================================= */ +/* +** Traverse a table with weak values and link it to proper list. During +** propagate phase, keep it in 'grayagain' list, to be revisited in the +** atomic phase. In the atomic phase, if table has any white value, +** put it in 'weak' list, to be cleared. +*/ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); - /* if there is array part, assume it may have white values (do not - traverse it just to check) */ + /* if there is array part, assume it may have white values (it is not + worth traversing it now just to check) */ int hasclears = (h->sizearray > 0); - for (n = gnode(h, 0); n < limit; n++) { + for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ checkdeadkey(n); if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ @@ -368,20 +360,30 @@ static void traverseweakvalue (global_State *g, Table *h) { hasclears = 1; /* table will have to be cleared */ } } - if (hasclears) - linktable(h, &g->weak); /* has to be cleared later */ - else /* no white values */ - linktable(h, &g->grayagain); /* no need to clean */ + if (g->gcstate == GCSpropagate) + linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ + else if (hasclears) + linkgclist(h, g->weak); /* has to be cleared later */ } +/* +** Traverse an ephemeron table and link it to proper list. Returns true +** iff any object was marked during this traversal (which implies that +** convergence has to continue). During propagation phase, keep table +** in 'grayagain' list, to be visited again in the atomic phase. In +** the atomic phase, if table has any white->white entry, it has to +** be revisited during ephemeron convergence (as that key may turn +** black). Otherwise, if it has any white key, table has to be cleared +** (in the atomic phase). +*/ static int traverseephemeron (global_State *g, Table *h) { int marked = 0; /* true if an object is marked in this traversal */ int hasclears = 0; /* true if table has white keys */ - int prop = 0; /* true if table has entry "white-key -> white-value" */ + int hasww = 0; /* true if table has entry "white-key -> white-value" */ Node *n, *limit = gnodelast(h); - int i; - /* traverse array part (numeric keys are 'strong') */ + unsigned int i; + /* traverse array part */ for (i = 0; i < h->sizearray; i++) { if (valiswhite(&h->array[i])) { marked = 1; @@ -396,26 +398,27 @@ static int traverseephemeron (global_State *g, Table *h) { else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */ hasclears = 1; /* table must be cleared */ if (valiswhite(gval(n))) /* value not marked yet? */ - prop = 1; /* must propagate again */ + hasww = 1; /* white-white entry */ } else if (valiswhite(gval(n))) { /* value not marked yet? */ marked = 1; reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ } } - if (g->gcstate != GCSatomic || prop) - linktable(h, &g->ephemeron); /* have to propagate again */ - else if (hasclears) /* does table have white keys? */ - linktable(h, &g->allweak); /* may have to clean white keys */ - else /* no white keys */ - linktable(h, &g->grayagain); /* no need to clean */ + /* link table into proper list */ + if (g->gcstate == GCSpropagate) + linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ + else if (hasww) /* table has white->white entries? */ + linkgclist(h, g->ephemeron); /* have to propagate again */ + else if (hasclears) /* table has white keys? */ + linkgclist(h, g->allweak); /* may have to clean white keys */ return marked; } static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); - int i; + unsigned int i; for (i = 0; i < h->sizearray; i++) /* traverse array part */ markvalue(g, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ @@ -439,13 +442,13 @@ static lu_mem traversetable (global_State *g, Table *h) { ((weakkey = strchr(svalue(mode), 'k')), (weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ - black2gray(obj2gco(h)); /* keep table gray */ + black2gray(h); /* keep table gray */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ traverseephemeron(g, h); else /* all weak */ - linktable(h, &g->allweak); /* nothing to traverse now */ + linkgclist(h, g->allweak); /* nothing to traverse now */ } else /* not weak */ traversestrongtable(g, h); @@ -453,34 +456,26 @@ static lu_mem traversetable (global_State *g, Table *h) { sizeof(Node) * cast(size_t, sizenode(h)); } -static int -marksharedproto (global_State *g, SharedProto *f) { - int i; - if (g != f->l_G) - return 0; - markobject(g, f->source); - for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobject(g, f->upvalues[i].name); - for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobject(g, f->locvars[i].varname); - return sizeof(Instruction) * f->sizecode + - sizeof(int) * f->sizelineinfo + - sizeof(LocVar) * f->sizelocvars + - sizeof(Upvaldesc) * f->sizeupvalues; -} - static int traverseproto (global_State *g, Proto *f) { int i; - if (f->cache && iswhite(obj2gco(f->cache))) + if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */ - for (i = 0; i < f->sp->sizek; i++) /* mark literals */ + markobject(g, f->source); + for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); - for (i = 0; i < f->sp->sizep; i++) /* mark nested protos */ + for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ + markobject(g, f->upvalues[i].name); + for (i = 0; i < f->sizep; i++) /* mark nested protos */ markobject(g, f->p[i]); - return sizeof(Proto) + sizeof(Proto *) * f->sp->sizep + - sizeof(TValue) * f->sp->sizek + - marksharedproto(g, f->sp); + for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ + markobject(g, f->locvars[i].varname); + return sizeof(Proto) + sizeof(Instruction) * f->sizecode + + sizeof(Proto *) * f->sizep + + sizeof(TValue) * f->sizek + + sizeof(int) * f->sizelineinfo + + sizeof(LocVar) * f->sizelocvars + + sizeof(Upvaldesc) * f->sizeupvalues; } @@ -491,34 +486,49 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) { return sizeCclosure(cl->nupvalues); } +/* +** open upvalues point to values in a thread, so those values should +** be marked when the thread is traversed except in the atomic phase +** (because then the value cannot be changed by the thread and the +** thread may not be traversed again) +*/ static lu_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobject(g, cl->p); /* mark its prototype */ - for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ - markobject(g, cl->upvals[i]); + for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ + UpVal *uv = cl->upvals[i]; + if (uv != NULL) { + if (upisopen(uv) && g->gcstate != GCSinsideatomic) + uv->u.open.touched = 1; /* can be marked in 'remarkupvals' */ + else + markvalue(g, uv->v); + } + } return sizeLclosure(cl->nupvalues); } -static lu_mem traversestack (global_State *g, lua_State *th) { - int n = 0; +static lu_mem traversethread (global_State *g, lua_State *th) { StkId o = th->stack; if (o == NULL) return 1; /* stack not completely built yet */ + lua_assert(g->gcstate == GCSinsideatomic || + th->openupval == NULL || isintwups(th)); for (; o < th->top; o++) /* mark live elements in the stack */ markvalue(g, o); - if (g->gcstate == GCSatomic) { /* final traversal? */ + if (g->gcstate == GCSinsideatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ setnilvalue(o); + /* 'remarkupvals' may have removed thread from 'twups' list */ + if (!isintwups(th) && th->openupval != NULL) { + th->twups = g->twups; /* link it back to the list */ + g->twups = th; + } } - else { /* count call infos to compute size */ - CallInfo *ci; - for (ci = &th->base_ci; ci != th->ci; ci = ci->next) - n++; - } - return sizeof(lua_State) + sizeof(TValue) * th->stacksize + - sizeof(CallInfo) * n; + else if (g->gckind != KGC_EMERGENCY) + luaD_shrinkstack(th); /* do not change stack in emergency cycle */ + return (sizeof(lua_State) + sizeof(TValue) * th->stacksize); } @@ -531,7 +541,7 @@ static void propagatemark (global_State *g) { GCObject *o = g->gray; lua_assert(isgray(o)); gray2black(o); - switch (gch(o)->tt) { + switch (o->tt) { case LUA_TTABLE: { Table *h = gco2t(o); g->gray = h->gclist; /* remove from 'gray' list */ @@ -553,10 +563,9 @@ static void propagatemark (global_State *g) { case LUA_TTHREAD: { lua_State *th = gco2th(o); g->gray = th->gclist; /* remove from 'gray' list */ - th->gclist = g->grayagain; - g->grayagain = o; /* insert into 'grayagain' list */ + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ black2gray(o); - size = traversestack(g, th); + size = traversethread(g, th); break; } case LUA_TPROTO: { @@ -576,35 +585,12 @@ static void propagateall (global_State *g) { } -static void propagatelist (global_State *g, GCObject *l) { - lua_assert(g->gray == NULL); /* no grays left */ - g->gray = l; - propagateall(g); /* traverse all elements from 'l' */ -} - -/* -** retraverse all gray lists. Because tables may be reinserted in other -** lists when traversed, traverse the original lists to avoid traversing -** twice the same table (which is not wrong, but inefficient) -*/ -static void retraversegrays (global_State *g) { - GCObject *weak = g->weak; /* save original lists */ - GCObject *grayagain = g->grayagain; - GCObject *ephemeron = g->ephemeron; - g->weak = g->grayagain = g->ephemeron = NULL; - propagateall(g); /* traverse main gray list */ - propagatelist(g, grayagain); - propagatelist(g, weak); - propagatelist(g, ephemeron); -} - - static void convergeephemerons (global_State *g) { int changed; do { GCObject *w; GCObject *next = g->ephemeron; /* get ephemeron list */ - g->ephemeron = NULL; /* tables will return to this list when traversed */ + g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; while ((w = next) != NULL) { next = gco2t(w)->gclist; @@ -652,7 +638,7 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); - int i; + unsigned int i; for (i = 0; i < h->sizearray; i++) { TValue *o = &h->array[i]; if (iscleared(g, o)) /* value was collected? */ @@ -668,23 +654,41 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { } +void luaC_upvdeccount (lua_State *L, UpVal *uv) { + lua_assert(uv->refcount > 0); + uv->refcount--; + if (uv->refcount == 0 && !upisopen(uv)) + luaM_free(L, uv); +} + + +static void freeLclosure (lua_State *L, LClosure *cl) { + int i; + for (i = 0; i < cl->nupvalues; i++) { + UpVal *uv = cl->upvals[i]; + if (uv) + luaC_upvdeccount(L, uv); + } + luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); +} + + static void freeobj (lua_State *L, GCObject *o) { - switch (gch(o)->tt) { + switch (o->tt) { case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; case LUA_TLCL: { - luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); + freeLclosure(L, gco2lcl(o)); break; } case LUA_TCCL: { luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); break; } - case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; case LUA_TTABLE: luaH_free(L, gco2t(o)); break; case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; case LUA_TSHRSTR: - G(L)->strt.nuse--; + luaS_remove(L, gco2ts(o)); /* remove it from hash table */ /* go through */ case LUA_TLNGSTR: { luaM_freemem(L, o, sizestring(gco2ts(o))); @@ -699,61 +703,27 @@ static void freeobj (lua_State *L, GCObject *o) { static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); -/* -** sweep the (open) upvalues of a thread and resize its stack and -** list of call-info structures. -*/ -static void sweepthread (lua_State *L, lua_State *L1) { - if (L1->stack == NULL) return; /* stack not completely built yet */ - sweepwholelist(L, &L1->openupval); /* sweep open upvalues */ - luaE_freeCI(L1); /* free extra CallInfo slots */ - /* should not change the stack during an emergency gc cycle */ - if (G(L)->gckind != KGC_EMERGENCY) - luaD_shrinkstack(L1); -} - - /* ** sweep at most 'count' elements from a list of GCObjects erasing dead -** objects, where a dead (not alive) object is one marked with the "old" -** (non current) white and not fixed. -** In non-generational mode, change all non-dead objects back to white, -** preparing for next collection cycle. -** In generational mode, keep black objects black, and also mark them as -** old; stop when hitting an old object, as all objects after that -** one will be old too. -** When object is a thread, sweep its list of open upvalues too. +** objects, where a dead object is one marked with the old (non current) +** white; change all non-dead objects back to white, preparing for next +** collection cycle. Return where to continue the traversal or NULL if +** list is finished. */ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { global_State *g = G(L); int ow = otherwhite(g); - int toclear, toset; /* bits to clear and to set in all live objects */ - int tostop; /* stop sweep when this is true */ - if (isgenerational(g)) { /* generational mode? */ - toclear = ~0; /* clear nothing */ - toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */ - tostop = bitmask(OLDBIT); /* do not sweep old generation */ - } - else { /* normal mode */ - toclear = maskcolors; /* clear all color bits + old bit */ - toset = luaC_white(g); /* make object white */ - tostop = 0; /* do not stop */ - } + int white = luaC_white(g); /* current white */ while (*p != NULL && count-- > 0) { GCObject *curr = *p; - int marked = gch(curr)->marked; + int marked = curr->marked; if (isdeadm(ow, marked)) { /* is 'curr' dead? */ - *p = gch(curr)->next; /* remove 'curr' from list */ + *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } - else { - if (testbits(marked, tostop)) - return NULL; /* stop sweeping this list */ - if (gch(curr)->tt == LUA_TTHREAD) - sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */ - /* update marks */ - gch(curr)->marked = cast_byte((marked & toclear) | toset); - p = &gch(curr)->next; /* go to next element */ + else { /* change mark to 'white' */ + curr->marked = cast_byte((marked & maskcolors) | white); + p = &curr->next; /* go to next element */ } } return (*p == NULL) ? NULL : p; @@ -764,7 +734,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { ** sweep a list until a live object (or end of list) */ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { - GCObject ** old = p; + GCObject **old = p; int i = 0; do { i++; @@ -783,26 +753,28 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { ** ======================================================= */ -static void checkSizes (lua_State *L) { - global_State *g = G(L); - if (g->gckind != KGC_EMERGENCY) { /* do not change sizes in emergency */ - int hs = g->strt.size / 2; /* half the size of the string table */ - if (g->strt.nuse < cast(lu_int32, hs)) /* using less than that half? */ - luaS_resize(L, hs); /* halve its size */ +/* +** If possible, free concatenation buffer and shrink string table +*/ +static void checkSizes (lua_State *L, global_State *g) { + if (g->gckind != KGC_EMERGENCY) { + l_mem olddebt = g->GCdebt; luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ + if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ + luaS_resize(L, g->strt.size / 2); /* shrink it a little */ + g->GCestimate += g->GCdebt - olddebt; /* update estimate */ } } static GCObject *udata2finalize (global_State *g) { GCObject *o = g->tobefnz; /* get first element */ - lua_assert(isfinalized(o)); - g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */ - gch(o)->next = g->allgc; /* return it to 'allgc' list */ + lua_assert(tofinalize(o)); + g->tobefnz = o->next; /* remove it from 'tobefnz' list */ + o->next = g->allgc; /* return it to 'allgc' list */ g->allgc = o; - resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */ - lua_assert(!isold(o)); /* see MOVE OLD rule */ - if (!keepinvariantout(g)) /* not keeping invariant? */ + resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ + if (issweepphase(g)) makewhite(g, o); /* "sweep" object */ return o; } @@ -846,29 +818,58 @@ static void GCTM (lua_State *L, int propagateerrors) { } +/* +** call a few (up to 'g->gcfinnum') finalizers +*/ +static int runafewfinalizers (lua_State *L) { + global_State *g = G(L); + unsigned int i; + lua_assert(!g->tobefnz || g->gcfinnum > 0); + for (i = 0; g->tobefnz && i < g->gcfinnum; i++) + GCTM(L, 1); /* call one finalizer */ + g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */ + : g->gcfinnum * 2; /* else call a few more next time */ + return i; +} + + +/* +** call all pending finalizers +*/ +static void callallpendingfinalizers (lua_State *L, int propagateerrors) { + global_State *g = G(L); + while (g->tobefnz) + GCTM(L, propagateerrors); +} + + +/* +** find last 'next' field in list 'p' list (to add elements in its end) +*/ +static GCObject **findlast (GCObject **p) { + while (*p != NULL) + p = &(*p)->next; + return p; +} + + /* ** move all unreachable objects (or 'all' objects) that need ** finalization from list 'finobj' to list 'tobefnz' (to be finalized) */ -static void separatetobefnz (lua_State *L, int all) { - global_State *g = G(L); - GCObject **p = &g->finobj; +static void separatetobefnz (global_State *g, int all) { GCObject *curr; - GCObject **lastnext = &g->tobefnz; - /* find last 'next' field in 'tobefnz' list (to add elements in its end) */ - while (*lastnext != NULL) - lastnext = &gch(*lastnext)->next; + GCObject **p = &g->finobj; + GCObject **lastnext = findlast(&g->tobefnz); while ((curr = *p) != NULL) { /* traverse all finalizable objects */ - lua_assert(!isfinalized(curr)); - lua_assert(testbit(gch(curr)->marked, SEPARATED)); + lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ - p = &gch(curr)->next; /* don't bother with it */ + p = &curr->next; /* don't bother with it */ else { - l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */ - *p = gch(curr)->next; /* remove 'curr' from 'finobj' list */ - gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */ + *p = curr->next; /* remove 'curr' from 'finobj' list */ + curr->next = *lastnext; /* link at the end of 'tobefnz' list */ *lastnext = curr; - lastnext = &gch(curr)->next; + lastnext = &curr->next; } } } @@ -880,33 +881,29 @@ static void separatetobefnz (lua_State *L, int all) { */ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { global_State *g = G(L); - if (testbit(gch(o)->marked, SEPARATED) || /* obj. is already separated... */ - isfinalized(o) || /* ... or is finalized... */ - gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ + if (tofinalize(o) || /* obj. is already marked... */ + gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ return; /* nothing to be done */ else { /* move 'o' to 'finobj' list */ GCObject **p; - GCheader *ho = gch(o); - if (g->sweepgc == &ho->next) { /* avoid removing current sweep object */ - lua_assert(issweepphase(g)); - g->sweepgc = sweeptolive(L, g->sweepgc, NULL); + if (issweepphase(g)) { + makewhite(g, o); /* "sweep" object 'o' */ + if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ + g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */ } /* search for pointer pointing to 'o' */ - for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ } - *p = ho->next; /* remove 'o' from root list */ - ho->next = g->finobj; /* link it in list 'finobj' */ + for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } + *p = o->next; /* remove 'o' from 'allgc' list */ + o->next = g->finobj; /* link it in 'finobj' list */ g->finobj = o; - l_setbit(ho->marked, SEPARATED); /* mark it as such */ - if (!keepinvariantout(g)) /* not keeping invariant? */ - makewhite(g, o); /* "sweep" object */ - else - resetoldbit(o); /* see MOVE OLD rule */ + l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */ } } /* }====================================================== */ + /* ** {====================================================== ** GC control @@ -915,127 +912,93 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* -** set a reasonable "time" to wait before starting a new GC cycle; -** cycle will start when memory use hits threshold +** Set a reasonable "time" to wait before starting a new GC cycle; cycle +** will start when memory use hits threshold. (Division by 'estimate' +** should be OK: it cannot be zero (because Lua cannot even start with +** less than PAUSEADJ bytes). */ -static void setpause (global_State *g, l_mem estimate) { - l_mem debt, threshold; - estimate = estimate / PAUSEADJ; /* adjust 'estimate' */ +static void setpause (global_State *g) { + l_mem threshold, debt; + l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ + lua_assert(estimate > 0); threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ ? estimate * g->gcpause /* no overflow */ : MAX_LMEM; /* overflow; truncate to maximum */ - debt = -cast(l_mem, threshold - gettotalbytes(g)); + debt = gettotalbytes(g) - threshold; luaE_setdebt(g, debt); } -#define sweepphases \ - (bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep)) - - /* -** enter first sweep phase (strings) and prepare pointers for other -** sweep phases. The calls to 'sweeptolive' make pointers point to an -** object inside the list (instead of to the header), so that the real -** sweep do not need to skip objects created between "now" and the start -** of the real sweep. +** Enter first sweep phase. +** The call to 'sweeptolive' makes pointer point to an object inside +** the list (instead of to the header), so that the real sweep do not +** need to skip objects created between "now" and the start of the real +** sweep. ** Returns how many objects it swept. */ static int entersweep (lua_State *L) { global_State *g = G(L); int n = 0; - g->gcstate = GCSsweepstring; - lua_assert(g->sweepgc == NULL && g->sweepfin == NULL); - /* prepare to sweep strings, finalizable objects, and regular objects */ - g->sweepstrgc = 0; - g->sweepfin = sweeptolive(L, &g->finobj, &n); + g->gcstate = GCSswpallgc; + lua_assert(g->sweepgc == NULL); g->sweepgc = sweeptolive(L, &g->allgc, &n); return n; } -/* -** change GC mode -*/ -void luaC_changemode (lua_State *L, int mode) { - global_State *g = G(L); - if (mode == g->gckind) return; /* nothing to change */ - if (mode == KGC_GEN) { /* change to generational mode */ - /* make sure gray lists are consistent */ - luaC_runtilstate(L, bitmask(GCSpropagate)); - g->GCestimate = gettotalbytes(g); - g->gckind = KGC_GEN; - } - else { /* change to incremental mode */ - /* sweep all objects to turn them back to white - (as white has not changed, nothing extra will be collected) */ - g->gckind = KGC_NORMAL; - entersweep(L); - luaC_runtilstate(L, ~sweepphases); - } -} - - -/* -** call all pending finalizers -*/ -static void callallpendingfinalizers (lua_State *L, int propagateerrors) { - global_State *g = G(L); - while (g->tobefnz) { - resetoldbit(g->tobefnz); - GCTM(L, propagateerrors); - } -} - - void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); - int i; - separatetobefnz(L, 1); /* separate all objects with finalizers */ + separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L, 0); + lua_assert(g->tobefnz == NULL); g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ g->gckind = KGC_NORMAL; - sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */ + sweepwholelist(L, &g->finobj); sweepwholelist(L, &g->allgc); - for (i = 0; i < g->strt.size; i++) /* free all string lists */ - sweepwholelist(L, &g->strt.hash[i]); + sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } static l_mem atomic (lua_State *L) { global_State *g = G(L); - l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */ + l_mem work; GCObject *origweak, *origall; - lua_assert(!iswhite(obj2gco(g->mainthread))); + GCObject *grayagain = g->grayagain; /* save original list */ + lua_assert(g->ephemeron == NULL && g->weak == NULL); + lua_assert(!iswhite(g->mainthread)); + g->gcstate = GCSinsideatomic; + g->GCmemtrav = 0; /* start counting work */ markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); - markmt(g); /* mark basic metatables */ + markmt(g); /* mark global metatables */ /* remark occasional upvalues of (maybe) dead threads */ remarkupvals(g); propagateall(g); /* propagate changes */ - work += g->GCmemtrav; /* stop counting (do not (re)count grays) */ - /* traverse objects caught by write barrier and by 'remarkupvals' */ - retraversegrays(g); - work -= g->GCmemtrav; /* restart counting */ + work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */ + g->gray = grayagain; + propagateall(g); /* traverse 'grayagain' list */ + g->GCmemtrav = 0; /* restart counting */ convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ - /* clear values from weak tables, before checking finalizers */ + /* Clear values from weak tables, before checking finalizers */ clearvalues(g, g->weak, NULL); clearvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; work += g->GCmemtrav; /* stop counting (objects being finalized) */ - separatetobefnz(L, 0); /* separate objects to be finalized */ + separatetobefnz(g, 0); /* separate objects to be finalized */ + g->gcfinnum = 1; /* there may be objects to be finalized */ markbeingfnz(g); /* mark objects that will be finalized */ - propagateall(g); /* remark, to propagate `preserveness' */ - work -= g->GCmemtrav; /* restart counting */ + propagateall(g); /* remark, to propagate 'resurrection' */ + g->GCmemtrav = 0; /* restart counting */ convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */ - clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */ + clearkeys(g, g->allweak, NULL); /* clear keys from all 'allweak' tables */ /* clear values from resurrected weak tables */ clearvalues(g, g->weak, origweak); clearvalues(g, g->allweak, origall); @@ -1045,65 +1008,71 @@ static l_mem atomic (lua_State *L) { } +static lu_mem sweepstep (lua_State *L, global_State *g, + int nextstate, GCObject **nextlist) { + if (g->sweepgc) { + l_mem olddebt = g->GCdebt; + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); + g->GCestimate += g->GCdebt - olddebt; /* update estimate */ + if (g->sweepgc) /* is there still something to sweep? */ + return (GCSWEEPMAX * GCSWEEPCOST); + } + /* else enter next state */ + g->gcstate = nextstate; + g->sweepgc = nextlist; + return 0; +} + + static lu_mem singlestep (lua_State *L) { global_State *g = G(L); switch (g->gcstate) { case GCSpause: { - /* start to count memory traversed */ g->GCmemtrav = g->strt.size * sizeof(GCObject*); - lua_assert(!isgenerational(g)); restartcollection(g); g->gcstate = GCSpropagate; return g->GCmemtrav; } case GCSpropagate: { - if (g->gray) { - lu_mem oldtrav = g->GCmemtrav; - propagatemark(g); - return g->GCmemtrav - oldtrav; /* memory traversed in this step */ - } - else { /* no more `gray' objects */ - lu_mem work; - int sw; - g->gcstate = GCSatomic; /* finish mark phase */ - g->GCestimate = g->GCmemtrav; /* save what was counted */; - work = atomic(L); /* add what was traversed by 'atomic' */ - g->GCestimate += work; /* estimate of total memory traversed */ - sw = entersweep(L); - return work + sw * GCSWEEPCOST; - } + g->GCmemtrav = 0; + lua_assert(g->gray); + propagatemark(g); + if (g->gray == NULL) /* no more gray objects? */ + g->gcstate = GCSatomic; /* finish propagate phase */ + return g->GCmemtrav; /* memory traversed in this step */ } - case GCSsweepstring: { - int i; - for (i = 0; i < GCSWEEPMAX && g->sweepstrgc + i < g->strt.size; i++) - sweepwholelist(L, &g->strt.hash[g->sweepstrgc + i]); - g->sweepstrgc += i; - if (g->sweepstrgc >= g->strt.size) /* no more strings to sweep? */ - g->gcstate = GCSsweepudata; - return i * GCSWEEPCOST; + case GCSatomic: { + lu_mem work; + int sw; + propagateall(g); /* make sure gray list is empty */ + work = atomic(L); /* work is what was traversed by 'atomic' */ + sw = entersweep(L); + g->GCestimate = gettotalbytes(g); /* first estimate */; + return work + sw * GCSWEEPCOST; } - case GCSsweepudata: { - if (g->sweepfin) { - g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX); - return GCSWEEPMAX*GCSWEEPCOST; - } - else { - g->gcstate = GCSsweep; - return 0; - } + case GCSswpallgc: { /* sweep "regular" objects */ + return sweepstep(L, g, GCSswpfinobj, &g->finobj); } - case GCSsweep: { - if (g->sweepgc) { - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); - return GCSWEEPMAX*GCSWEEPCOST; + case GCSswpfinobj: { /* sweep objects with finalizers */ + return sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + } + case GCSswptobefnz: { /* sweep objects to be finalized */ + return sweepstep(L, g, GCSswpend, NULL); + } + case GCSswpend: { /* finish sweeps */ + makewhite(g, g->mainthread); /* sweep main thread */ + checkSizes(L, g); + g->gcstate = GCScallfin; + return 0; + } + case GCScallfin: { /* call remaining finalizers */ + if (g->tobefnz && g->gckind != KGC_EMERGENCY) { + int n = runafewfinalizers(L); + return (n * GCFINALIZECOST); } - else { - /* sweep main thread */ - GCObject *mt = obj2gco(g->mainthread); - sweeplist(L, &mt, 1); - checkSizes(L); + else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ - return GCSWEEPCOST; + return 0; } } default: lua_assert(0); return 0; @@ -1122,105 +1091,67 @@ void luaC_runtilstate (lua_State *L, int statesmask) { } -static void generationalcollection (lua_State *L) { - global_State *g = G(L); - lua_assert(g->gcstate == GCSpropagate); - if (g->GCestimate == 0) { /* signal for another major collection? */ - luaC_fullgc(L, 0); /* perform a full regular collection */ - g->GCestimate = gettotalbytes(g); /* update control */ - } - else { - lu_mem estimate = g->GCestimate; - luaC_runtilstate(L, bitmask(GCSpause)); /* run complete (minor) cycle */ - g->gcstate = GCSpropagate; /* skip restart */ - if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc) - g->GCestimate = 0; /* signal for a major collection */ - else - g->GCestimate = estimate; /* keep estimate from last major coll. */ - - } - setpause(g, gettotalbytes(g)); - lua_assert(g->gcstate == GCSpropagate); -} - - -static void incstep (lua_State *L) { - global_State *g = G(L); +/* +** get GC debt and convert it from Kb to 'work units' (avoid zero debt +** and overflows) +*/ +static l_mem getdebt (global_State *g) { l_mem debt = g->GCdebt; int stepmul = g->gcstepmul; - if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values (and 0) */ - /* convert debt from Kb to 'work units' (avoid zero debt and overflows) */ debt = (debt / STEPMULADJ) + 1; debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; - do { /* always perform at least one single step */ - lu_mem work = singlestep(L); /* do some work */ - debt -= work; - } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); - if (g->gcstate == GCSpause) - setpause(g, g->GCestimate); /* pause until next cycle */ - else { - debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */ - luaE_setdebt(g, debt); - } + return debt; } - /* -** performs a basic GC step -*/ -void luaC_forcestep (lua_State *L) { - global_State *g = G(L); - int i; - if (isgenerational(g)) generationalcollection(L); - else incstep(L); - /* run a few finalizers (or all of them at the end of a collect cycle) */ - for (i = 0; g->tobefnz && (i < GCFINALIZENUM || g->gcstate == GCSpause); i++) - GCTM(L, 1); /* call one finalizer */ -} - - -/* -** performs a basic GC step only if collector is running +** performs a basic GC step when collector is running */ void luaC_step (lua_State *L) { global_State *g = G(L); - if (g->gcrunning) luaC_forcestep(L); - else luaE_setdebt(g, -GCSTEPSIZE); /* avoid being called too often */ + l_mem debt = getdebt(g); /* GC deficit (be paid now) */ + if (!g->gcrunning) { /* not running? */ + luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ + return; + } + do { /* repeat until pause or enough "credit" (negative debt) */ + lu_mem work = singlestep(L); /* perform one single step */ + debt -= work; + } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); + if (g->gcstate == GCSpause) + setpause(g); /* pause until next cycle */ + else { + debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */ + luaE_setdebt(g, debt); + runafewfinalizers(L); + } } - /* -** performs a full GC cycle; if "isemergency", does not call -** finalizers (which could change stack positions) +** Performs a full GC cycle; if 'isemergency', set a flag to avoid +** some operations which could change the interpreter state in some +** unexpected ways (running finalizers and shrinking some structures). +** Before running the collection, check 'keepinvariant'; if it is true, +** there may be some objects marked as black, so the collector has +** to sweep all objects to turn them back to white (as white has not +** changed, nothing will be collected). */ void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); - int origkind = g->gckind; - lua_assert(origkind != KGC_EMERGENCY); - if (isemergency) /* do not run finalizers during emergency GC */ - g->gckind = KGC_EMERGENCY; - else { - g->gckind = KGC_NORMAL; - callallpendingfinalizers(L, 1); - } - if (keepinvariant(g)) { /* may there be some black objects? */ - /* must sweep all objects to turn them back to white - (as white has not changed, nothing will be collected) */ - entersweep(L); + lua_assert(g->gckind == KGC_NORMAL); + if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */ + if (keepinvariant(g)) { /* black objects? */ + entersweep(L); /* sweep everything to turn them back to white */ } /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */ - luaC_runtilstate(L, bitmask(GCSpause)); /* run entire collection */ - if (origkind == KGC_GEN) { /* generational mode? */ - /* generational mode must be kept in propagate phase */ - luaC_runtilstate(L, bitmask(GCSpropagate)); - } - g->gckind = origkind; - setpause(g, gettotalbytes(g)); - if (!isemergency) /* do not run finalizers during emergency GC */ - callallpendingfinalizers(L, 1); + luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ + /* estimate must be correct after a full GC cycle */ + lua_assert(g->GCestimate == gettotalbytes(g)); + luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ + g->gckind = KGC_NORMAL; + setpause(g); } /* }====================================================== */ diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 84bb1cdf..0eedf842 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -38,36 +38,27 @@ */ #define GCSpropagate 0 #define GCSatomic 1 -#define GCSsweepstring 2 -#define GCSsweepudata 3 -#define GCSsweep 4 -#define GCSpause 5 +#define GCSswpallgc 2 +#define GCSswpfinobj 3 +#define GCSswptobefnz 4 +#define GCSswpend 5 +#define GCScallfin 6 +#define GCSpause 7 #define issweepphase(g) \ - (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) + (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) -#define isgenerational(g) ((g)->gckind == KGC_GEN) /* -** macros to tell when main invariant (white objects cannot point to black -** ones) must be kept. During a non-generational collection, the sweep +** macro to tell when main invariant (white objects cannot point to black +** ones) must be kept. During a collection, the sweep ** phase may break the invariant, as objects turned white may point to ** still-black objects. The invariant is restored when sweep ends and -** all objects are white again. During a generational collection, the -** invariant must be kept all times. +** all objects are white again. */ -#define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) - - -/* -** Outside the collector, the state in generational mode is kept in -** 'propagate', so 'keepinvariant' is always true. -*/ -#define keepinvariantout(g) \ - check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \ - g->gcstate <= GCSatomic) +#define keepinvariant(g) ((g)->gcstate <= GCSatomic) /* @@ -83,38 +74,29 @@ #define testbit(x,b) testbits(x, bitmask(b)) -/* Layout for bit use in `marked' field: */ +/* Layout for bit use in 'marked' field: */ #define WHITE0BIT 0 /* object is white (type 0) */ #define WHITE1BIT 1 /* object is white (type 1) */ #define BLACKBIT 2 /* object is black */ -#define FINALIZEDBIT 3 /* object has been separated for finalization */ -#define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ -#define FIXEDBIT 5 /* object is fixed (should not be collected) */ -#define OLDBIT 6 /* object is old (only in generational mode) */ +#define FINALIZEDBIT 3 /* object has been marked for finalization */ /* bit 7 is currently used by tests (luaL_checkmemory) */ #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) -#define iswhite(x) testbits((x)->gch.marked, WHITEBITS) -#define isblack(x) testbit((x)->gch.marked, BLACKBIT) +#define iswhite(x) testbits((x)->marked, WHITEBITS) +#define isblack(x) testbit((x)->marked, BLACKBIT) #define isgray(x) /* neither white nor black */ \ - (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) + (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) -#define isold(x) testbit((x)->gch.marked, OLDBIT) +#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) -/* MOVE OLD rule: whenever an object is moved to the beginning of - a GC list, its old bit must be cleared */ -#define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) - -#define otherwhite(g) (g->currentwhite ^ WHITEBITS) +#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) -#define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) +#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) -#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) - -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) +#define changewhite(x) ((x)->marked ^= WHITEBITS) +#define gray2black(x) l_setbit((x)->marked, BLACKBIT) #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) @@ -124,34 +106,33 @@ #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) -#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ +#define luaC_barrier(L,p,v) { \ + if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ luaC_barrier_(L,obj2gco(p),gcvalue(v)); } -#define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ +#define luaC_barrierback(L,p,v) { \ + if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ luaC_barrierback_(L,p); } -#define luaC_objbarrier(L,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ +#define luaC_objbarrier(L,p,o) { \ + if (isblack(p) && iswhite(o)) \ luaC_barrier_(L,obj2gco(p),obj2gco(o)); } -#define luaC_objbarrierback(L,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } - -#define luaC_barrierproto(L,p,c) \ - { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } +#define luaC_upvalbarrier(L,uv) \ + { if (iscollectable((uv)->v) && !upisopen(uv)) \ + luaC_upvalbarrier_(L,uv); } +LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_forcestep (lua_State *L); LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); -LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, - GCObject **list, int offset); +LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); -LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); +LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); +LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); -LUAI_FUNC void luaC_changemode (lua_State *L, int mode); +LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); + #endif diff --git a/3rd/lua/linit.c b/3rd/lua/linit.c index c1a38304..ca9d100d 100644 --- a/3rd/lua/linit.c +++ b/3rd/lua/linit.c @@ -1,21 +1,32 @@ /* -** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: linit.c,v 1.37 2014/12/09 15:00:17 roberto Exp $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ +#define linit_c +#define LUA_LIB + +#include "lprefix.h" + + /* ** If you embed Lua in your program and need to open the standard ** libraries, call luaL_openlibs in your program. If you need a ** different set of libraries, copy this file to your project and edit ** it to suit your needs. +** +** You can also *preload* libraries, so that a later 'require' can +** open the library, which is already linked to the application. +** For that, do the following code: +** +** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); +** lua_pushcfunction(L, luaopen_modname); +** lua_setfield(L, -2, modname); +** lua_pop(L, 1); // remove _PRELOAD table */ - -#define linit_c -#define LUA_LIB - #include "lua.h" #include "lualib.h" @@ -34,34 +45,22 @@ static const luaL_Reg loadedlibs[] = { {LUA_IOLIBNAME, luaopen_io}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, - {LUA_BITLIBNAME, luaopen_bit32}, {LUA_MATHLIBNAME, luaopen_math}, + {LUA_UTF8LIBNAME, luaopen_utf8}, {LUA_DBLIBNAME, luaopen_debug}, - {NULL, NULL} -}; - - -/* -** these libs are preloaded and must be required before used -*/ -static const luaL_Reg preloadedlibs[] = { +#if defined(LUA_COMPAT_BITLIB) + {LUA_BITLIBNAME, luaopen_bit32}, +#endif {NULL, NULL} }; LUALIB_API void luaL_openlibs (lua_State *L) { const luaL_Reg *lib; - /* call open functions from 'loadedlibs' and set results to global table */ + /* "require" functions from 'loadedlibs' and set results to global table */ for (lib = loadedlibs; lib->func; lib++) { luaL_requiref(L, lib->name, lib->func, 1); lua_pop(L, 1); /* remove lib */ } - /* add open functions from 'preloadedlibs' into 'package.preload' table */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); - for (lib = preloadedlibs; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_setfield(L, -2, lib->name); - } - lua_pop(L, 1); /* remove _PRELOAD table */ } diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index 2a4ec4aa..4dea3968 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,42 +1,36 @@ /* -** $Id: liolib.c,v 2.112.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: liolib.c,v 2.142 2015/01/02 12:50:28 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ +#define liolib_c +#define LUA_LIB -/* -** This definition must come before the inclusion of 'stdio.h'; it -** should not affect non-POSIX systems -*/ -#if !defined(_FILE_OFFSET_BITS) -#define _LARGEFILE_SOURCE 1 -#define _FILE_OFFSET_BITS 64 -#endif +#include "lprefix.h" +#include #include +#include #include #include #include -#define liolib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#if !defined(lua_checkmode) +#if !defined(l_checkmode) /* ** Check whether 'mode' matches '[rwa]%+?b?'. ** Change this macro to accept other modes for 'fopen' besides ** the standard ones. */ -#define lua_checkmode(mode) \ +#define l_checkmode(mode) \ (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \ (*mode != '+' || ++mode) && /* skip if char is '+' */ \ (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \ @@ -46,75 +40,94 @@ /* ** {====================================================== -** lua_popen spawns a new process connected to the current +** l_popen spawns a new process connected to the current ** one through the file streams. ** ======================================================= */ -#if !defined(lua_popen) /* { */ +#if !defined(l_popen) /* { */ -#if defined(LUA_USE_POPEN) /* { */ +#if defined(LUA_USE_POSIX) /* { */ -#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) -#define lua_pclose(L,file) ((void)L, pclose(file)) +#define l_popen(L,c,m) (fflush(NULL), popen(c,m)) +#define l_pclose(L,file) (pclose(file)) -#elif defined(LUA_WIN) /* }{ */ - -#define lua_popen(L,c,m) ((void)L, _popen(c,m)) -#define lua_pclose(L,file) ((void)L, _pclose(file)) +#elif defined(LUA_USE_WINDOWS) /* }{ */ +#define l_popen(L,c,m) (_popen(c,m)) +#define l_pclose(L,file) (_pclose(file)) #else /* }{ */ -#define lua_popen(L,c,m) ((void)((void)c, m), \ - luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) -#define lua_pclose(L,file) ((void)((void)L, file), -1) - +/* ISO C definitions */ +#define l_popen(L,c,m) \ + ((void)((void)c, m), \ + luaL_error(L, "'popen' not supported"), \ + (FILE*)0) +#define l_pclose(L,file) ((void)L, (void)file, -1) #endif /* } */ -#endif /* } */ +#endif /* } */ /* }====================================================== */ +#if !defined(l_getc) /* { */ + +#if defined(LUA_USE_POSIX) +#define l_getc(f) getc_unlocked(f) +#define l_lockfile(f) flockfile(f) +#define l_unlockfile(f) funlockfile(f) +#else +#define l_getc(f) getc(f) +#define l_lockfile(f) ((void)0) +#define l_unlockfile(f) ((void)0) +#endif + +#endif /* } */ + + /* ** {====================================================== -** lua_fseek: configuration for longer offsets +** l_fseek: configuration for longer offsets ** ======================================================= */ -#if !defined(lua_fseek) && !defined(LUA_ANSI) /* { */ +#if !defined(l_fseek) /* { */ #if defined(LUA_USE_POSIX) /* { */ +#include + #define l_fseek(f,o,w) fseeko(f,o,w) #define l_ftell(f) ftello(f) #define l_seeknum off_t -#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \ +#elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ -/* Windows (but not DDK) and Visual C++ 2005 or higher */ +/* Windows (but not DDK) and Visual C++ 2005 or higher */ #define l_fseek(f,o,w) _fseeki64(f,o,w) #define l_ftell(f) _ftelli64(f) #define l_seeknum __int64 -#endif /* } */ +#else /* }{ */ -#endif /* } */ - - -#if !defined(l_fseek) /* default definitions */ +/* ISO C definitions */ #define l_fseek(f,o,w) fseek(f,o,w) #define l_ftell(f) ftell(f) #define l_seeknum long -#endif + +#endif /* } */ + +#endif /* } */ /* }====================================================== */ #define IO_PREFIX "_IO_" +#define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) #define IO_INPUT (IO_PREFIX "input") #define IO_OUTPUT (IO_PREFIX "output") @@ -161,7 +174,7 @@ static FILE *tofile (lua_State *L) { /* -** When creating file handles, always creates a `closed' file handle +** When creating file handles, always creates a 'closed' file handle ** before opening the actual file; so, if there is a memory error, the ** file is not left opened. */ @@ -173,9 +186,14 @@ static LStream *newprefile (lua_State *L) { } +/* +** Calls the 'close' function from a file handle. The 'volatile' avoids +** a bug in some versions of the Clang compiler (e.g., clang 3.0 for +** 32 bits). +*/ static int aux_close (lua_State *L) { LStream *p = tolstream(L); - lua_CFunction cf = p->closef; + volatile lua_CFunction cf = p->closef; p->closef = NULL; /* mark stream as closed */ return (*cf)(L); /* close it */ } @@ -219,7 +237,7 @@ static void opencheck (lua_State *L, const char *fname, const char *mode) { LStream *p = newfile(L); p->f = fopen(fname, mode); if (p->f == NULL) - luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno)); + luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } @@ -228,7 +246,7 @@ static int io_open (lua_State *L) { const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ - luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode"); + luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -239,7 +257,7 @@ static int io_open (lua_State *L) { */ static int io_pclose (lua_State *L) { LStream *p = tolstream(L); - return luaL_execresult(L, lua_pclose(L, p->f)); + return luaL_execresult(L, l_pclose(L, p->f)); } @@ -247,7 +265,7 @@ static int io_popen (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); - p->f = lua_popen(L, filename, mode); + p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -265,7 +283,7 @@ static FILE *getiofile (lua_State *L, const char *findex) { lua_getfield(L, LUA_REGISTRYINDEX, findex); p = (LStream *)lua_touserdata(L, -1); if (isclosed(p)) - luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX)); + luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN); return p->f; } @@ -301,14 +319,10 @@ static int io_readline (lua_State *L); static void aux_lines (lua_State *L, int toclose) { - int i; int n = lua_gettop(L) - 1; /* number of arguments to read */ - /* ensure that arguments will fit here and into 'io_readline' stack */ - luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); - lua_pushvalue(L, 1); /* file handle */ lua_pushinteger(L, n); /* number of arguments to read */ lua_pushboolean(L, toclose); /* close/not close file when finished */ - for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */ + lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ lua_pushcclosure(L, io_readline, 3 + n); } @@ -347,13 +361,93 @@ static int io_lines (lua_State *L) { */ -static int read_number (lua_State *L, FILE *f) { - lua_Number d; - if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { - lua_pushnumber(L, d); - return 1; +/* maximum length of a numeral */ +#define MAXRN 200 + +/* auxiliary structure used by 'read_number' */ +typedef struct { + FILE *f; /* file being read */ + int c; /* current character (look ahead) */ + int n; /* number of elements in buffer 'buff' */ + char buff[MAXRN + 1]; /* +1 for ending '\0' */ +} RN; + + +/* +** Add current char to buffer (if not out of space) and read next one +*/ +static int nextc (RN *rn) { + if (rn->n >= MAXRN) { /* buffer overflow? */ + rn->buff[0] = '\0'; /* invalidate result */ + return 0; /* fail */ } else { + rn->buff[rn->n++] = rn->c; /* save current char */ + rn->c = l_getc(rn->f); /* read next one */ + return 1; + } +} + + +/* +** Accept current char if it is in 'set' (of size 1 or 2) +*/ +static int test2 (RN *rn, const char *set) { + if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0')) + return nextc(rn); + else return 0; +} + + +/* +** Read a sequence of (hex)digits +*/ +static int readdigits (RN *rn, int hex) { + int count = 0; + while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) + count++; + return count; +} + + +/* access to locale "radix character" (decimal point) */ +#if !defined(l_getlocaledecpoint) +#define l_getlocaledecpoint() (localeconv()->decimal_point[0]) +#endif + + +/* +** Read a number: first reads a valid prefix of a numeral into a buffer. +** Then it calls 'lua_stringtonumber' to check whether the format is +** correct and to convert it to a Lua number +*/ +static int read_number (lua_State *L, FILE *f) { + RN rn; + int count = 0; + int hex = 0; + char decp[2] = "."; + rn.f = f; rn.n = 0; + decp[0] = l_getlocaledecpoint(); /* get decimal point from locale */ + l_lockfile(rn.f); + do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ + test2(&rn, "-+"); /* optional signal */ + if (test2(&rn, "0")) { + if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ + else count = 1; /* count initial '0' as a valid digit */ + } + count += readdigits(&rn, hex); /* integral part */ + if (test2(&rn, decp)) /* decimal point? */ + count += readdigits(&rn, hex); /* fractional part */ + if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ + test2(&rn, "-+"); /* exponent signal */ + readdigits(&rn, 0); /* exponent digits */ + } + ungetc(rn.c, rn.f); /* unread look-ahead char */ + l_unlockfile(rn.f); + rn.buff[rn.n] = '\0'; /* finish string */ + if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ + return 1; /* ok */ + else { /* invalid format */ lua_pushnil(L); /* "result" to be removed */ return 0; /* read fails */ } @@ -362,7 +456,7 @@ static int read_number (lua_State *L, FILE *f) { static int test_eof (lua_State *L, FILE *f) { int c = getc(f); - ungetc(c, f); + ungetc(c, f); /* no-op when c == EOF */ lua_pushlstring(L, NULL, 0); return (c != EOF); } @@ -370,40 +464,34 @@ static int test_eof (lua_State *L, FILE *f) { static int read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; + int c = '\0'; luaL_buffinit(L, &b); - for (;;) { - size_t l; - char *p = luaL_prepbuffer(&b); - if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ - luaL_pushresult(&b); /* close buffer */ - return (lua_rawlen(L, -1) > 0); /* check whether read something */ - } - l = strlen(p); - if (l == 0 || p[l-1] != '\n') - luaL_addsize(&b, l); - else { - luaL_addsize(&b, l - chop); /* chop 'eol' if needed */ - luaL_pushresult(&b); /* close buffer */ - return 1; /* read at least an `eol' */ - } + while (c != EOF && c != '\n') { /* repeat until end of line */ + char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */ + int i = 0; + l_lockfile(f); /* no memory errors can happen inside the lock */ + while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') + buff[i++] = c; + l_unlockfile(f); + luaL_addsize(&b, i); } + if (!chop && c == '\n') /* want a newline and have one? */ + luaL_addchar(&b, c); /* add ending newline to result */ + luaL_pushresult(&b); /* close buffer */ + /* return ok if read something (either a newline or something else) */ + return (c == '\n' || lua_rawlen(L, -1) > 0); } -#define MAX_SIZE_T (~(size_t)0) - static void read_all (lua_State *L, FILE *f) { - size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ + size_t nr; luaL_Buffer b; luaL_buffinit(L, &b); - for (;;) { - char *p = luaL_prepbuffsize(&b, rlen); - size_t nr = fread(p, sizeof(char), rlen, f); + do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ + char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE); + nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); luaL_addsize(&b, nr); - if (nr < rlen) break; /* eof? */ - else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ - rlen *= 2; /* double buffer size at each iteration */ - } + } while (nr == LUAL_BUFFERSIZE); luaL_pushresult(&b); /* close buffer */ } @@ -435,13 +523,13 @@ static int g_read (lua_State *L, FILE *f, int first) { success = 1; for (n = first; nargs-- && success; n++) { if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)lua_tointeger(L, n); + size_t l = (size_t)luaL_checkinteger(L, n); success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); } else { - const char *p = lua_tostring(L, n); - luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); - switch (p[1]) { + const char *p = luaL_checkstring(L, n); + if (*p == '*') p++; /* skip optional '*' (for compatibility) */ + switch (*p) { case 'n': /* number */ success = read_number(L, f); break; @@ -488,11 +576,12 @@ static int io_readline (lua_State *L) { if (isclosed(p)) /* file is already closed? */ return luaL_error(L, "file is already closed"); lua_settop(L , 1); + luaL_checkstack(L, n, "too many arguments"); for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ lua_pushvalue(L, lua_upvalueindex(3 + i)); n = g_read(L, p->f, 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ - if (!lua_isnil(L, -n)) /* read at least one value? */ + if (lua_toboolean(L, -n)) /* read at least one value? */ return n; /* return them */ else { /* first result is nil: EOF or error */ if (n > 1) { /* is there error information? */ @@ -517,8 +606,10 @@ static int g_write (lua_State *L, FILE *f, int arg) { for (; nargs--; arg++) { if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ - status = status && - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; + int len = lua_isinteger(L, arg) + ? fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg)) + : fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)); + status = status && (len > 0); } else { size_t l; @@ -548,15 +639,15 @@ static int f_seek (lua_State *L) { static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L); int op = luaL_checkoption(L, 2, "cur", modenames); - lua_Number p3 = luaL_optnumber(L, 3, 0); + lua_Integer p3 = luaL_optinteger(L, 3, 0); l_seeknum offset = (l_seeknum)p3; - luaL_argcheck(L, (lua_Number)offset == p3, 3, + luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); op = l_fseek(f, offset, mode[op]); if (op) return luaL_fileresult(L, 0, NULL); /* error */ else { - lua_pushnumber(L, (lua_Number)l_ftell(f)); + lua_pushinteger(L, (lua_Integer)l_ftell(f)); return 1; } } @@ -568,7 +659,7 @@ static int f_setvbuf (lua_State *L) { FILE *f = tofile(L); int op = luaL_checkoption(L, 2, NULL, modenames); lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], sz); + int res = setvbuf(f, NULL, mode[op], (size_t)sz); return luaL_fileresult(L, res == 0, NULL); } diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index c4b820e8..6e4a457a 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -1,20 +1,23 @@ /* -** $Id: llex.c,v 2.63.1.2 2013/08/30 15:49:41 roberto Exp $ +** $Id: llex.c,v 2.89 2014/11/14 16:06:09 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ +#define llex_c +#define LUA_CORE + +#include "lprefix.h" + #include #include -#define llex_c -#define LUA_CORE - #include "lua.h" #include "lctype.h" #include "ldo.h" +#include "lgc.h" #include "llex.h" #include "lobject.h" #include "lparser.h" @@ -38,8 +41,9 @@ static const char *const luaX_tokens [] = { "end", "false", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", - "..", "...", "==", ">=", "<=", "~=", "::", "", - "", "", "" + "//", "..", "...", "==", ">=", "<=", "~=", + "<<", ">>", "::", "", + "", "", "", "" }; @@ -53,7 +57,7 @@ static void save (LexState *ls, int c) { Mbuffer *b = ls->buff; if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZET/2) + if (luaZ_sizebuffer(b) >= MAX_SIZE/2) lexerror(ls, "lexical element too long", 0); newsize = luaZ_sizebuffer(b) * 2; luaZ_resizebuffer(ls->L, b, newsize); @@ -64,24 +68,25 @@ static void save (LexState *ls, int c) { void luaX_init (lua_State *L) { int i; + TString *e = luaS_new(L, LUA_ENV); /* create env name */ + luaC_fix(L, obj2gco(e)); /* never collect this name */ for (i=0; itsv.extra = cast_byte(i+1); /* reserved word */ + luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */ + ts->extra = cast_byte(i+1); /* reserved word */ } } const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ - lua_assert(token == cast(unsigned char, token)); - return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) : - luaO_pushfstring(ls->L, "char(%d)", token); + lua_assert(token == cast_uchar(token)); + return luaO_pushfstring(ls->L, "'%c'", token); } else { const char *s = luaX_tokens[token - FIRST_RESERVED]; if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ - return luaO_pushfstring(ls->L, LUA_QS, s); + return luaO_pushfstring(ls->L, "'%s'", s); else /* names, strings, and numerals */ return s; } @@ -90,11 +95,10 @@ const char *luaX_token2str (LexState *ls, int token) { static const char *txtToken (LexState *ls, int token) { switch (token) { - case TK_NAME: - case TK_STRING: - case TK_NUMBER: + case TK_NAME: case TK_STRING: + case TK_FLT: case TK_INT: save(ls, '\0'); - return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); + return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff)); default: return luaX_token2str(ls, token); } @@ -117,24 +121,24 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { /* -** creates a new string and anchors it in function's table so that -** it will not be collected until the end of the function's compilation -** (by that time it should be anchored in function's prototype) +** creates a new string and anchors it in scanner's table so that +** it will not be collected until the end of the compilation +** (by that time it should be anchored somewhere) */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; - TValue *o; /* entry for `str' */ + TValue *o; /* entry for 'str' */ TString *ts = luaS_newlstr(L, str, l); /* create new string */ setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ - o = luaH_set(L, ls->fs->h, L->top - 1); - if (ttisnil(o)) { /* not in use yet? (see 'addK') */ + o = luaH_set(L, ls->h, L->top - 1); + if (ttisnil(o)) { /* not in use yet? */ /* boolean value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setbvalue(o, 1); /* t[string] = true */ luaC_checkGC(L); } else { /* string already present */ - ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ + ts = tsvalue(keyfromval(o)); /* re-use value previously stored */ } L->top--; /* remove string from stack */ return ts; @@ -148,16 +152,17 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { static void inclinenumber (LexState *ls) { int old = ls->current; lua_assert(currIsNewline(ls)); - next(ls); /* skip `\n' or `\r' */ + next(ls); /* skip '\n' or '\r' */ if (currIsNewline(ls) && ls->current != old) - next(ls); /* skip `\n\r' or `\r\n' */ + next(ls); /* skip '\n\r' or '\r\n' */ if (++ls->linenumber >= MAX_INT) - luaX_syntaxerror(ls, "chunk has too many lines"); + lexerror(ls, "chunk has too many lines", 0); } void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, int firstchar) { + ls->t.token = 0; ls->decpoint = '.'; ls->L = L; ls->current = firstchar; @@ -167,8 +172,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, ls->linenumber = 1; ls->lastline = 1; ls->source = source; - ls->envn = luaS_new(L, LUA_ENV); /* create env name */ - luaS_fix(ls->envn); /* never collect this name */ + ls->envn = luaS_new(L, LUA_ENV); /* get env name */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } @@ -181,12 +185,26 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, */ +static int check_next1 (LexState *ls, int c) { + if (ls->current == c) { + next(ls); + return 1; + } + else return 0; +} -static int check_next (LexState *ls, const char *set) { - if (ls->current == '\0' || !strchr(set, ls->current)) - return 0; - save_and_next(ls); - return 1; + +/* +** Check whether current char is in set 'set' (with two chars) and +** saves it +*/ +static int check_next2 (LexState *ls, const char *set) { + lua_assert(set[2] == '\0'); + if (ls->current == set[0] || ls->current == set[1]) { + save_and_next(ls); + return 1; + } + else return 0; } @@ -194,59 +212,73 @@ static int check_next (LexState *ls, const char *set) { ** change all characters 'from' in buffer to 'to' */ static void buffreplace (LexState *ls, char from, char to) { - size_t n = luaZ_bufflen(ls->buff); - char *p = luaZ_buffer(ls->buff); - while (n--) - if (p[n] == from) p[n] = to; + if (from != to) { + size_t n = luaZ_bufflen(ls->buff); + char *p = luaZ_buffer(ls->buff); + while (n--) + if (p[n] == from) p[n] = to; + } } -#if !defined(getlocaledecpoint) -#define getlocaledecpoint() (localeconv()->decimal_point[0]) +#if !defined(l_getlocaledecpoint) +#define l_getlocaledecpoint() (localeconv()->decimal_point[0]) #endif -#define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e) +#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0) /* ** in case of format error, try to change decimal point separator to ** the one defined in the current locale and check again */ -static void trydecpoint (LexState *ls, SemInfo *seminfo) { +static void trydecpoint (LexState *ls, TValue *o) { char old = ls->decpoint; - ls->decpoint = getlocaledecpoint(); + ls->decpoint = l_getlocaledecpoint(); buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ - if (!buff2d(ls->buff, &seminfo->r)) { + if (!buff2num(ls->buff, o)) { /* format error with correct decimal point: no more options */ buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ - lexerror(ls, "malformed number", TK_NUMBER); + lexerror(ls, "malformed number", TK_FLT); } } /* LUA_NUMBER */ /* -** this function is quite liberal in what it accepts, as 'luaO_str2d' +** this function is quite liberal in what it accepts, as 'luaO_str2num' ** will reject ill-formed numerals. */ -static void read_numeral (LexState *ls, SemInfo *seminfo) { +static int read_numeral (LexState *ls, SemInfo *seminfo) { + TValue obj; const char *expo = "Ee"; int first = ls->current; lua_assert(lisdigit(ls->current)); save_and_next(ls); - if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ + if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ expo = "Pp"; for (;;) { - if (check_next(ls, expo)) /* exponent part? */ - check_next(ls, "+-"); /* optional exponent sign */ - if (lisxdigit(ls->current) || ls->current == '.') + if (check_next2(ls, expo)) /* exponent part? */ + check_next2(ls, "-+"); /* optional exponent sign */ + if (lisxdigit(ls->current)) save_and_next(ls); - else break; + else if (ls->current == '.') + save_and_next(ls); + else break; } save(ls, '\0'); buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ - trydecpoint(ls, seminfo); /* try to update decimal point separator */ + if (!buff2num(ls->buff, &obj)) /* format error? */ + trydecpoint(ls, &obj); /* try to update decimal point separator */ + if (ttisinteger(&obj)) { + seminfo->i = ivalue(&obj); + return TK_INT; + } + else { + lua_assert(ttisfloat(&obj)); + seminfo->r = fltvalue(&obj); + return TK_FLT; + } } @@ -268,18 +300,22 @@ static int skip_sep (LexState *ls) { static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { - save_and_next(ls); /* skip 2nd `[' */ + int line = ls->linenumber; /* initial line (for error message) */ + save_and_next(ls); /* skip 2nd '[' */ if (currIsNewline(ls)) /* string starts with a newline? */ inclinenumber(ls); /* skip it */ for (;;) { switch (ls->current) { - case EOZ: - lexerror(ls, (seminfo) ? "unfinished long string" : - "unfinished long comment", TK_EOS); + case EOZ: { /* error */ + const char *what = (seminfo ? "string" : "comment"); + const char *msg = luaO_pushfstring(ls->L, + "unfinished long %s (starting at line %d)", what, line); + lexerror(ls, msg, TK_EOS); break; /* to avoid warnings */ + } case ']': { if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd `]' */ + save_and_next(ls); /* skip 2nd ']' */ goto endloop; } break; @@ -302,40 +338,65 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { } -static void escerror (LexState *ls, int *c, int n, const char *msg) { - int i; - luaZ_resetbuffer(ls->buff); /* prepare error message */ - save(ls, '\\'); - for (i = 0; i < n && c[i] != EOZ; i++) - save(ls, c[i]); - lexerror(ls, msg, TK_STRING); +static void esccheck (LexState *ls, int c, const char *msg) { + if (!c) { + if (ls->current != EOZ) + save_and_next(ls); /* add current to buffer for error message */ + lexerror(ls, msg, TK_STRING); + } +} + + +static int gethexa (LexState *ls) { + save_and_next(ls); + esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); + return luaO_hexavalue(ls->current); } static int readhexaesc (LexState *ls) { - int c[3], i; /* keep input for error message */ - int r = 0; /* result accumulator */ - c[0] = 'x'; /* for error message */ - for (i = 1; i < 3; i++) { /* read two hexadecimal digits */ - c[i] = next(ls); - if (!lisxdigit(c[i])) - escerror(ls, c, i + 1, "hexadecimal digit expected"); - r = (r << 4) + luaO_hexavalue(c[i]); - } + int r = gethexa(ls); + r = (r << 4) + gethexa(ls); + luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */ return r; } +static unsigned long readutf8esc (LexState *ls) { + unsigned long r; + int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ + save_and_next(ls); /* skip 'u' */ + esccheck(ls, ls->current == '{', "missing '{'"); + r = gethexa(ls); /* must have at least one digit */ + while ((save_and_next(ls), lisxdigit(ls->current))) { + i++; + r = (r << 4) + luaO_hexavalue(ls->current); + esccheck(ls, r <= 0x10FFFF, "UTF-8 value too large"); + } + esccheck(ls, ls->current == '}', "missing '}'"); + next(ls); /* skip '}' */ + luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */ + return r; +} + + +static void utf8esc (LexState *ls) { + char buff[UTF8BUFFSZ]; + int n = luaO_utf8esc(buff, readutf8esc(ls)); + for (; n > 0; n--) /* add 'buff' to string */ + save(ls, buff[UTF8BUFFSZ - n]); +} + + static int readdecesc (LexState *ls) { - int c[3], i; + int i; int r = 0; /* result accumulator */ for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ - c[i] = ls->current; - r = 10*r + c[i] - '0'; - next(ls); + r = 10*r + ls->current - '0'; + save_and_next(ls); } - if (r > UCHAR_MAX) - escerror(ls, c, i, "decimal escape too large"); + esccheck(ls, r <= UCHAR_MAX, "decimal escape too large"); + luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */ return r; } @@ -353,7 +414,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) { break; /* to avoid warnings */ case '\\': { /* escape sequences */ int c; /* final character to be saved */ - next(ls); /* do not save the `\' */ + save_and_next(ls); /* keep '\\' for error messages */ switch (ls->current) { case 'a': c = '\a'; goto read_save; case 'b': c = '\b'; goto read_save; @@ -363,12 +424,14 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) { case 't': c = '\t'; goto read_save; case 'v': c = '\v'; goto read_save; case 'x': c = readhexaesc(ls); goto read_save; + case 'u': utf8esc(ls); goto no_save; case '\n': case '\r': inclinenumber(ls); c = '\n'; goto only_save; case '\\': case '\"': case '\'': c = ls->current; goto read_save; case EOZ: goto no_save; /* will raise an error next loop */ case 'z': { /* zap following span of spaces */ + luaZ_buffremove(ls->buff, 1); /* remove '\\' */ next(ls); /* skip the 'z' */ while (lisspace(ls->current)) { if (currIsNewline(ls)) inclinenumber(ls); @@ -377,15 +440,18 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) { goto no_save; } default: { - if (!lisdigit(ls->current)) - escerror(ls, &ls->current, 1, "invalid escape sequence"); - /* digital escape \ddd */ - c = readdecesc(ls); + esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); + c = readdecesc(ls); /* digital escape '\ddd' */ goto only_save; } } - read_save: next(ls); /* read next character */ - only_save: save(ls, c); /* save 'c' */ + read_save: + next(ls); + /* go through */ + only_save: + luaZ_buffremove(ls->buff, 1); /* remove '\\' */ + save(ls, c); + /* go through */ no_save: break; } default: @@ -417,7 +483,7 @@ static int llex (LexState *ls, SemInfo *seminfo) { next(ls); if (ls->current == '[') { /* long comment? */ int sep = skip_sep(ls); - luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ + luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ if (sep >= 0) { read_long_string(ls, NULL, sep); /* skip long comment */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ @@ -440,28 +506,35 @@ static int llex (LexState *ls, SemInfo *seminfo) { } case '=': { next(ls); - if (ls->current != '=') return '='; - else { next(ls); return TK_EQ; } + if (check_next1(ls, '=')) return TK_EQ; + else return '='; } case '<': { next(ls); - if (ls->current != '=') return '<'; - else { next(ls); return TK_LE; } + if (check_next1(ls, '=')) return TK_LE; + else if (check_next1(ls, '<')) return TK_SHL; + else return '<'; } case '>': { next(ls); - if (ls->current != '=') return '>'; - else { next(ls); return TK_GE; } + if (check_next1(ls, '=')) return TK_GE; + else if (check_next1(ls, '>')) return TK_SHR; + else return '>'; + } + case '/': { + next(ls); + if (check_next1(ls, '/')) return TK_IDIV; + else return '/'; } case '~': { next(ls); - if (ls->current != '=') return '~'; - else { next(ls); return TK_NE; } + if (check_next1(ls, '=')) return TK_NE; + else return '~'; } case ':': { next(ls); - if (ls->current != ':') return ':'; - else { next(ls); return TK_DBCOLON; } + if (check_next1(ls, ':')) return TK_DBCOLON; + else return ':'; } case '"': case '\'': { /* short literal strings */ read_string(ls, ls->current, seminfo); @@ -469,18 +542,17 @@ static int llex (LexState *ls, SemInfo *seminfo) { } case '.': { /* '.', '..', '...', or number */ save_and_next(ls); - if (check_next(ls, ".")) { - if (check_next(ls, ".")) + if (check_next1(ls, '.')) { + if (check_next1(ls, '.')) return TK_DOTS; /* '...' */ else return TK_CONCAT; /* '..' */ } else if (!lisdigit(ls->current)) return '.'; - /* else go through */ + else return read_numeral(ls, seminfo); } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { - read_numeral(ls, seminfo); - return TK_NUMBER; + return read_numeral(ls, seminfo); } case EOZ: { return TK_EOS; @@ -495,7 +567,7 @@ static int llex (LexState *ls, SemInfo *seminfo) { luaZ_bufflen(ls->buff)); seminfo->ts = ts; if (isreserved(ts)) /* reserved word? */ - return ts->tsv.extra - 1 + FIRST_RESERVED; + return ts->extra - 1 + FIRST_RESERVED; else { return TK_NAME; } diff --git a/3rd/lua/llex.h b/3rd/lua/llex.h index a4acdd30..afb40b56 100644 --- a/3rd/lua/llex.h +++ b/3rd/lua/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -14,6 +14,10 @@ #define FIRST_RESERVED 257 +#if !defined(LUA_ENV) +#define LUA_ENV "_ENV" +#endif + /* * WARNING: if you change the order of this enumeration, @@ -26,8 +30,10 @@ enum RESERVED { TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ - TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, - TK_NUMBER, TK_NAME, TK_STRING + TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, + TK_SHL, TK_SHR, + TK_DBCOLON, TK_EOS, + TK_FLT, TK_INT, TK_NAME, TK_STRING }; /* number of reserved words */ @@ -36,6 +42,7 @@ enum RESERVED { typedef union { lua_Number r; + lua_Integer i; TString *ts; } SemInfo; /* semantics information */ @@ -51,13 +58,14 @@ typedef struct Token { typedef struct LexState { int current; /* current character (charint) */ int linenumber; /* input line counter */ - int lastline; /* line of last token `consumed' */ + int lastline; /* line of last token 'consumed' */ Token t; /* current token */ Token lookahead; /* look ahead token */ struct FuncState *fs; /* current function (parser) */ struct lua_State *L; ZIO *z; /* input stream */ Mbuffer *buff; /* buffer for tokens */ + Table *h; /* to avoid collection/reuse strings */ struct Dyndata *dyd; /* dynamic structures used by the parser */ TString *source; /* current source name */ TString *envn; /* environment variable name */ diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 152dd055..8f71a6ff 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -1,6 +1,6 @@ /* -** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 roberto Exp $ -** Limits, basic types, and some other `installation-dependent' definitions +** $Id: llimits.h,v 1.125 2014/12/19 13:30:23 roberto Exp $ +** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -14,47 +14,64 @@ #include "lua.h" - -typedef unsigned LUA_INT32 lu_int32; - +/* +** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count +** the total memory used by Lua (in bytes). Usually, 'size_t' and +** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. +*/ +#if defined(LUAI_MEM) /* { external definitions? */ typedef LUAI_UMEM lu_mem; - typedef LUAI_MEM l_mem; +#elif LUAI_BITSINT >= 32 /* }{ */ +typedef size_t lu_mem; +typedef ptrdiff_t l_mem; +#else /* 16-bit ints */ /* }{ */ +typedef unsigned long lu_mem; +typedef long l_mem; +#endif /* } */ - -/* chars used as small naturals (so that `char' is reserved for characters) */ +/* chars used as small naturals (so that 'char' is reserved for characters) */ typedef unsigned char lu_byte; -#define MAX_SIZET ((size_t)(~(size_t)0)-2) +/* maximum value for size_t */ +#define MAX_SIZET ((size_t)(~(size_t)0)) -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) - -#define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) +/* maximum size visible for Lua (must be representable in a lua_Integer */ +#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ + : (size_t)(LUA_MAXINTEGER)) -#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ +#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) + +#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) + + +#define MAX_INT INT_MAX /* maximum value of an int */ + /* -** conversion of pointer to integer +** conversion of pointer to integer: ** this is for hashing only; there is no problem if the integer ** cannot hold the whole pointer value */ -#define IntPoint(p) ((unsigned int)(lu_mem)(p)) +#define point2int(p) ((unsigned int)((size_t)(p) & UINT_MAX)) /* type to ensure maximum alignment */ -#if !defined(LUAI_USER_ALIGNMENT_T) -#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } +#if defined(LUAI_USER_ALIGNMENT_T) +typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; +#else +typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign; #endif -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; -/* result of a `usual argument conversion' over lua_Number */ +/* types of 'usual argument conversions' for lua_Number and lua_Integer */ typedef LUAI_UACNUMBER l_uacNumber; +typedef LUAI_UACINT l_uacInt; /* internal assertions for in-house debugging */ @@ -71,18 +88,15 @@ typedef LUAI_UACNUMBER l_uacNumber; /* ** assertion for checking API calls */ -#if !defined(luai_apicheck) - #if defined(LUA_USE_APICHECK) #include -#define luai_apicheck(L,e) assert(e) +#define luai_apicheck(e) assert(e) #else -#define luai_apicheck(L,e) lua_assert(e) +#define luai_apicheck(e) lua_assert(e) #endif -#endif -#define api_check(l,e,msg) luai_apicheck(l,(e) && msg) +#define api_check(e,msg) luai_apicheck((e) && msg) #if !defined(UNUSED) @@ -92,18 +106,34 @@ typedef LUAI_UACNUMBER l_uacNumber; #define cast(t, exp) ((t)(exp)) +#define cast_void(i) cast(void, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) #define cast_uchar(i) cast(unsigned char, (i)) +/* cast a signed lua_Integer to lua_Unsigned */ +#if !defined(l_castS2U) +#define l_castS2U(i) ((lua_Unsigned)(i)) +#endif + +/* +** cast a lua_Unsigned to a signed lua_Integer; this cast is +** not strict ISO C, but two-complement architectures should +** work fine. +*/ +#if !defined(l_castU2S) +#define l_castU2S(i) ((lua_Integer)(i)) +#endif + + /* ** non-return type */ #if defined(__GNUC__) #define l_noret void __attribute__((noreturn)) -#elif defined(_MSC_VER) +#elif defined(_MSC_VER) && _MSC_VER >= 1200 #define l_noret void __declspec(noreturn) #else #define l_noret void @@ -127,21 +157,21 @@ typedef LUAI_UACNUMBER l_uacNumber; /* -** type for virtual-machine instructions +** type for virtual-machine instructions; ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) */ -typedef lu_int32 Instruction; +#if LUAI_BITSINT >= 32 +typedef unsigned int Instruction; +#else +typedef unsigned long Instruction; +#endif -/* maximum stack for a Lua function */ -#define MAXSTACK 250 - - /* minimum size for the string table (must be power of 2) */ #if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 32 +#define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */ #endif @@ -152,12 +182,12 @@ typedef lu_int32 Instruction; #if !defined(lua_lock) -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) #endif #if !defined(luai_threadyield) -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} +#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} #endif @@ -183,108 +213,11 @@ typedef lu_int32 Instruction; #endif #if !defined(luai_userstateresume) -#define luai_userstateresume(L,n) ((void)L) +#define luai_userstateresume(L,n) ((void)L) #endif #if !defined(luai_userstateyield) -#define luai_userstateyield(L,n) ((void)L) -#endif - -/* -** lua_number2int is a macro to convert lua_Number to int. -** lua_number2integer is a macro to convert lua_Number to lua_Integer. -** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. -** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. -** luai_hashnum is a macro to hash a lua_Number value into an integer. -** The hash must be deterministic and give reasonable values for -** both small and large values (outside the range of integers). -*/ - -#if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ -/* trick with Microsoft assembler for X86 */ - -#define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} -#define lua_number2integer(i,n) lua_number2int(i, n) -#define lua_number2unsigned(i,n) \ - {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} - - -#elif defined(LUA_IEEE754TRICK) /* }{ */ -/* the next trick should work on any machine using IEEE754 with - a 32-bit int type */ - -union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; - -#if !defined(LUA_IEEEENDIAN) /* { */ -#define LUAI_EXTRAIEEE \ - static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; -#define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) -#else -#define LUA_IEEEENDIANLOC LUA_IEEEENDIAN -#define LUAI_EXTRAIEEE /* empty */ -#endif /* } */ - -#define lua_number2int32(i,n,t) \ - { LUAI_EXTRAIEEE \ - volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ - (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } - -#define luai_hashnum(i,n) \ - { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ - (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ - -#define lua_number2int(i,n) lua_number2int32(i, n, int) -#define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) - -/* the trick can be expanded to lua_Integer when it is a 32-bit value */ -#if defined(LUA_IEEELL) -#define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) -#endif - -#endif /* } */ - - -/* the following definitions always work, but may be slow */ - -#if !defined(lua_number2int) -#define lua_number2int(i,n) ((i)=(int)(n)) -#endif - -#if !defined(lua_number2integer) -#define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) -#endif - -#if !defined(lua_number2unsigned) /* { */ -/* the following definition assures proper modulo behavior */ -#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) -#include -#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) -#define lua_number2unsigned(i,n) \ - ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) -#else -#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) -#endif -#endif /* } */ - - -#if !defined(lua_unsigned2number) -/* on several machines, coercion from unsigned to double is slow, - so it may be worth to avoid */ -#define lua_unsigned2number(u) \ - (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) -#endif - - - -#if defined(ltable_c) && !defined(luai_hashnum) - -#include -#include - -#define luai_hashnum(i,n) { int e; \ - n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ - lua_number2int(i, n); i += e; } - +#define luai_userstateyield(L,n) ((void)L) #endif diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index fe9fc542..002c508b 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -1,16 +1,18 @@ /* -** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lmathlib.c,v 1.114 2014/12/27 20:32:26 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ +#define lmathlib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include -#define lmathlib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -18,13 +20,30 @@ #undef PI -#define PI ((lua_Number)(3.1415926535897932384626433832795)) -#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) +#define PI (l_mathop(3.141592653589793238462643383279502884)) +#if !defined(l_rand) /* { */ +#if defined(LUA_USE_POSIX) +#define l_rand() random() +#define l_srand(x) srandom(x) +#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */ +#else +#define l_rand() rand() +#define l_srand(x) srand(x) +#define L_RANDMAX RAND_MAX +#endif +#endif /* } */ + static int math_abs (lua_State *L) { - lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); + if (lua_isinteger(L, 1)) { + lua_Integer n = lua_tointeger(L, 1); + if (n < 0) n = (lua_Integer)(0u - n); + lua_pushinteger(L, n); + } + else + lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); return 1; } @@ -33,31 +52,16 @@ static int math_sin (lua_State *L) { return 1; } -static int math_sinh (lua_State *L) { - lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); - return 1; -} - static int math_cos (lua_State *L) { lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); return 1; } -static int math_cosh (lua_State *L) { - lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); - return 1; -} - static int math_tan (lua_State *L) { lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); return 1; } -static int math_tanh (lua_State *L) { - lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); - return 1; -} - static int math_asin (lua_State *L) { lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); return 1; @@ -69,49 +73,106 @@ static int math_acos (lua_State *L) { } static int math_atan (lua_State *L) { - lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); + lua_Number y = luaL_checknumber(L, 1); + lua_Number x = luaL_optnumber(L, 2, 1); + lua_pushnumber(L, l_mathop(atan2)(y, x)); return 1; } -static int math_atan2 (lua_State *L) { - lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); + +static int math_toint (lua_State *L) { + int valid; + lua_Integer n = lua_tointegerx(L, 1, &valid); + if (valid) + lua_pushinteger(L, n); + else { + luaL_checkany(L, 1); + lua_pushnil(L); /* value is not convertible to integer */ + } return 1; } -static int math_ceil (lua_State *L) { - lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); - return 1; + +static void pushnumint (lua_State *L, lua_Number d) { + lua_Integer n; + if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */ + lua_pushinteger(L, n); /* result is integer */ + else + lua_pushnumber(L, d); /* result is float */ } + static int math_floor (lua_State *L) { - lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); + if (lua_isinteger(L, 1)) + lua_settop(L, 1); /* integer is its own floor */ + else { + lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1)); + pushnumint(L, d); + } return 1; } + +static int math_ceil (lua_State *L) { + if (lua_isinteger(L, 1)) + lua_settop(L, 1); /* integer is its own ceil */ + else { + lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); + pushnumint(L, d); + } + return 1; +} + + static int math_fmod (lua_State *L) { - lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); + if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { + lua_Integer d = lua_tointeger(L, 2); + if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */ + luaL_argcheck(L, d != 0, 2, "zero"); + lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */ + } + else + lua_pushinteger(L, lua_tointeger(L, 1) % d); + } + else + lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), + luaL_checknumber(L, 2))); return 1; } + +/* +** next function does not use 'modf', avoiding problems with 'double*' +** (which is not compatible with 'float*') when lua_Number is not +** 'double'. +*/ static int math_modf (lua_State *L) { - lua_Number ip; - lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); - lua_pushnumber(L, ip); - lua_pushnumber(L, fp); + if (lua_isinteger(L ,1)) { + lua_settop(L, 1); /* number is its own integer part */ + lua_pushnumber(L, 0); /* no fractional part */ + } + else { + lua_Number n = luaL_checknumber(L, 1); + /* integer part (rounds toward zero) */ + lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); + pushnumint(L, ip); + /* fractional part (test needed for inf/-inf) */ + lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip)); + } return 2; } + static int math_sqrt (lua_State *L) { lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); return 1; } -static int math_pow (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number y = luaL_checknumber(L, 2); - lua_pushnumber(L, l_mathop(pow)(x, y)); + +static int math_ult (lua_State *L) { + lua_Integer a = luaL_checkinteger(L, 1); + lua_Integer b = luaL_checkinteger(L, 2); + lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b); return 1; } @@ -122,32 +183,139 @@ static int math_log (lua_State *L) { res = l_mathop(log)(x); else { lua_Number base = luaL_checknumber(L, 2); - if (base == (lua_Number)10.0) res = l_mathop(log10)(x); + if (base == 10.0) res = l_mathop(log10)(x); else res = l_mathop(log)(x)/l_mathop(log)(base); } lua_pushnumber(L, res); return 1; } -#if defined(LUA_COMPAT_LOG10) -static int math_log10 (lua_State *L) { - lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); - return 1; -} -#endif - static int math_exp (lua_State *L) { lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); return 1; } static int math_deg (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); + lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); return 1; } static int math_rad (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); + lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); + return 1; +} + + +static int math_min (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int imin = 1; /* index of current minimum value */ + int i; + luaL_argcheck(L, n >= 1, 1, "value expected"); + for (i = 2; i <= n; i++) { + if (lua_compare(L, i, imin, LUA_OPLT)) + imin = i; + } + lua_pushvalue(L, imin); + return 1; +} + + +static int math_max (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int imax = 1; /* index of current maximum value */ + int i; + luaL_argcheck(L, n >= 1, 1, "value expected"); + for (i = 2; i <= n; i++) { + if (lua_compare(L, imax, i, LUA_OPLT)) + imax = i; + } + lua_pushvalue(L, imax); + return 1; +} + +/* +** This function uses 'double' (instead of 'lua_Number') to ensure that +** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0' +** will keep full precision (ensuring that 'r' is always less than 1.0.) +*/ +static int math_random (lua_State *L) { + lua_Integer low, up; + double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0)); + switch (lua_gettop(L)) { /* check number of arguments */ + case 0: { /* no arguments */ + lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */ + return 1; + } + case 1: { /* only upper limit */ + low = 1; + up = luaL_checkinteger(L, 1); + break; + } + case 2: { /* lower and upper limits */ + low = luaL_checkinteger(L, 1); + up = luaL_checkinteger(L, 2); + break; + } + default: return luaL_error(L, "wrong number of arguments"); + } + /* random integer in the interval [low, up] */ + luaL_argcheck(L, low <= up, 1, "interval is empty"); + luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1, + "interval too large"); + r *= (double)(up - low) + 1.0; + lua_pushinteger(L, (lua_Integer)r + low); + return 1; +} + + +static int math_randomseed (lua_State *L) { + l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); + (void)rand(); /* discard first value to avoid undesirable correlations */ + return 0; +} + + +static int math_type (lua_State *L) { + if (lua_type(L, 1) == LUA_TNUMBER) { + if (lua_isinteger(L, 1)) + lua_pushliteral(L, "integer"); + else + lua_pushliteral(L, "float"); + } + else { + luaL_checkany(L, 1); + lua_pushnil(L); + } + return 1; +} + + +/* +** {================================================================== +** Deprecated functions (for compatibility only) +** =================================================================== +*/ +#if defined(LUA_COMPAT_MATHLIB) + +static int math_cosh (lua_State *L) { + lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); + return 1; +} + +static int math_sinh (lua_State *L) { + lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); + return 1; +} + +static int math_tanh (lua_State *L) { + lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); + return 1; +} + +static int math_pow (lua_State *L) { + lua_Number x = luaL_checknumber(L, 1); + lua_Number y = luaL_checknumber(L, 2); + lua_pushnumber(L, l_mathop(pow)(x, y)); return 1; } @@ -160,107 +328,60 @@ static int math_frexp (lua_State *L) { static int math_ldexp (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); - int ep = luaL_checkint(L, 2); + int ep = (int)luaL_checkinteger(L, 2); lua_pushnumber(L, l_mathop(ldexp)(x, ep)); return 1; } - - -static int math_min (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmin = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d < dmin) - dmin = d; - } - lua_pushnumber(L, dmin); +static int math_log10 (lua_State *L) { + lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); return 1; } +#endif +/* }================================================================== */ -static int math_max (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmax = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d > dmax) - dmax = d; - } - lua_pushnumber(L, dmax); - return 1; -} - - -static int math_random (lua_State *L) { - /* the `%' avoids the (rare) case of r==1, and is needed also because on - some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ - lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; - switch (lua_gettop(L)) { /* check number of arguments */ - case 0: { /* no arguments */ - lua_pushnumber(L, r); /* Number between 0 and 1 */ - break; - } - case 1: { /* only upper limit */ - lua_Number u = luaL_checknumber(L, 1); - luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); - lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ - break; - } - case 2: { /* lower and upper limits */ - lua_Number l = luaL_checknumber(L, 1); - lua_Number u = luaL_checknumber(L, 2); - luaL_argcheck(L, l <= u, 2, "interval is empty"); - lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ - break; - } - default: return luaL_error(L, "wrong number of arguments"); - } - return 1; -} - - -static int math_randomseed (lua_State *L) { - srand(luaL_checkunsigned(L, 1)); - (void)rand(); /* discard first value to avoid undesirable correlations */ - return 0; -} static const luaL_Reg mathlib[] = { {"abs", math_abs}, {"acos", math_acos}, {"asin", math_asin}, - {"atan2", math_atan2}, {"atan", math_atan}, {"ceil", math_ceil}, - {"cosh", math_cosh}, {"cos", math_cos}, {"deg", math_deg}, {"exp", math_exp}, + {"tointeger", math_toint}, {"floor", math_floor}, {"fmod", math_fmod}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, -#if defined(LUA_COMPAT_LOG10) - {"log10", math_log10}, -#endif + {"ult", math_ult}, {"log", math_log}, {"max", math_max}, {"min", math_min}, {"modf", math_modf}, - {"pow", math_pow}, {"rad", math_rad}, {"random", math_random}, {"randomseed", math_randomseed}, - {"sinh", math_sinh}, {"sin", math_sin}, {"sqrt", math_sqrt}, - {"tanh", math_tanh}, {"tan", math_tan}, + {"type", math_type}, +#if defined(LUA_COMPAT_MATHLIB) + {"atan2", math_atan}, + {"cosh", math_cosh}, + {"sinh", math_sinh}, + {"tanh", math_tanh}, + {"pow", math_pow}, + {"frexp", math_frexp}, + {"ldexp", math_ldexp}, + {"log10", math_log10}, +#endif + /* placeholders */ + {"pi", NULL}, + {"huge", NULL}, + {"maxinteger", NULL}, + {"mininteger", NULL}, {NULL, NULL} }; @@ -272,8 +393,12 @@ LUAMOD_API int luaopen_math (lua_State *L) { luaL_newlib(L, mathlib); lua_pushnumber(L, PI); lua_setfield(L, -2, "pi"); - lua_pushnumber(L, HUGE_VAL); + lua_pushnumber(L, (lua_Number)HUGE_VAL); lua_setfield(L, -2, "huge"); + lua_pushinteger(L, LUA_MAXINTEGER); + lua_setfield(L, -2, "maxinteger"); + lua_pushinteger(L, LUA_MININTEGER); + lua_setfield(L, -2, "mininteger"); return 1; } diff --git a/3rd/lua/lmem.c b/3rd/lua/lmem.c index ee343e3e..4feaf036 100644 --- a/3rd/lua/lmem.c +++ b/3rd/lua/lmem.c @@ -1,15 +1,17 @@ /* -** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lmem.c,v 1.89 2014/11/02 19:33:33 roberto Exp $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ - -#include - #define lmem_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "ldebug.h" @@ -24,15 +26,15 @@ /* ** About the realloc function: ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); -** (`osize' is the old size, `nsize' is the new size) +** ('osize' is the old size, 'nsize' is the new size) ** -** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no +** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no ** matter 'x'). ** -** * frealloc(ud, p, x, 0) frees the block `p' +** * frealloc(ud, p, x, 0) frees the block 'p' ** (in this specific case, frealloc must return NULL); ** particularly, frealloc(ud, NULL, 0, 0) does nothing -** (which is equivalent to free(NULL) in ANSI C) +** (which is equivalent to free(NULL) in ISO C) ** ** frealloc returns NULL if it cannot create or reallocate the area ** (any reallocation to an equal or smaller size cannot fail!) @@ -83,12 +85,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { #endif newblock = (*g->frealloc)(g->ud, block, osize, nsize); if (newblock == NULL && nsize > 0) { - api_check(L, nsize > realosize, + api_check( nsize > realosize, "realloc cannot fail when shrinking a block"); - if (g->gcrunning) { - luaC_fullgc(L, 1); /* try to free some memory... */ - newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ - } + luaC_fullgc(L, 1); /* try to free some memory... */ + newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ if (newblock == NULL) luaD_throw(L, LUA_ERRMEM); } diff --git a/3rd/lua/lmem.h b/3rd/lua/lmem.h index bd4f4e07..30f48489 100644 --- a/3rd/lua/lmem.h +++ b/3rd/lua/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -15,20 +15,32 @@ /* -** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is -** always constant. -** The macro is somewhat complex to avoid warnings: -** +1 avoids warnings of "comparison has constant result"; -** cast to 'void' avoids warnings of "value unused". +** This macro reallocs a vector 'b' from 'on' to 'n' elements, where +** each element has size 'e'. In case of arithmetic overflow of the +** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because +** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). +** +** (The macro is somewhat complex to avoid warnings: The 'sizeof' +** comparison avoids a runtime comparison when overflow cannot occur. +** The compiler should be able to optimize the real test by itself, but +** when it does it, it may give a warning about "comparison is always +** false due to limited range of data type"; the +1 tricks the compiler, +** avoiding this warning but also this optimization.) */ #define luaM_reallocv(L,b,on,n,e) \ - (cast(void, \ - (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ + (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ + ? luaM_toobig(L) : cast_void(0)) , \ luaM_realloc_(L, (b), (on)*(e), (n)*(e))) +/* +** Arrays of chars do not need any test +*/ +#define luaM_reallocvchar(L,b,on,n) \ + cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) + #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) -#define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) +#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index bedbea3e..1dab9bd0 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.111.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: loadlib.c,v 1.123 2014/11/12 13:31:51 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -8,22 +8,15 @@ ** systems. */ +#define loadlib_c +#define LUA_LIB -/* -** if needed, includes windows header before everything else -*/ -#if defined(_WIN32) -#include -#endif +#include "lprefix.h" #include #include - -#define loadlib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -31,21 +24,21 @@ /* -** LUA_PATH and LUA_CPATH are the names of the environment +** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment ** variables that Lua check to set its paths. */ -#if !defined(LUA_PATH) -#define LUA_PATH "LUA_PATH" +#if !defined(LUA_PATH_VAR) +#define LUA_PATH_VAR "LUA_PATH" #endif -#if !defined(LUA_CPATH) -#define LUA_CPATH "LUA_CPATH" +#if !defined(LUA_CPATH_VAR) +#define LUA_CPATH_VAR "LUA_CPATH" #endif #define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR -#define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX -#define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX +#define LUA_PATHVARVERSION LUA_PATH_VAR LUA_PATHSUFFIX +#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_PATHSUFFIX /* ** LUA_PATH_SEP is the character that separates templates in a path. @@ -92,29 +85,45 @@ #define LUA_OFSEP "_" -/* table (in the registry) that keeps handles for all loaded C libraries */ -#define CLIBS "_CLIBS" +/* +** unique key for table in the registry that keeps handles +** for all loaded C libraries +*/ +static const int CLIBS = 0; #define LIB_FAIL "open" - -/* error codes for ll_loadfunc */ -#define ERRLIB 1 -#define ERRFUNC 2 - #define setprogdir(L) ((void)0) /* ** system-dependent functions */ -static void ll_unloadlib (void *lib); -static void *ll_load (lua_State *L, const char *path, int seeglb); -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); + +/* +** unload library 'lib' +*/ +static void lsys_unloadlib (void *lib); + +/* +** load C library in file 'path'. If 'seeglb', load with all names in +** the library global. +** Returns the library; in case of error, returns NULL plus an +** error string in the stack. +*/ +static void *lsys_load (lua_State *L, const char *path, int seeglb); + +/* +** Try to find a function named 'sym' in library 'lib'. +** Returns the function; in case of error, returns NULL plus an +** error string in the stack. +*/ +static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); -#if defined(LUA_USE_DLOPEN) + +#if defined(LUA_USE_DLOPEN) /* { */ /* ** {======================================================================== ** This is an implementation of loadlib based on the dlfcn interface. @@ -126,19 +135,19 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); #include -static void ll_unloadlib (void *lib) { +static void lsys_unloadlib (void *lib) { dlclose(lib); } -static void *ll_load (lua_State *L, const char *path, int seeglb) { +static void *lsys_load (lua_State *L, const char *path, int seeglb) { void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); if (lib == NULL) lua_pushstring(L, dlerror()); return lib; } -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { +static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = (lua_CFunction)dlsym(lib, sym); if (f == NULL) lua_pushstring(L, dlerror()); return f; @@ -148,13 +157,15 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { -#elif defined(LUA_DL_DLL) +#elif defined(LUA_DL_DLL) /* }{ */ /* ** {====================================================================== ** This is an implementation of loadlib for Windows using native functions. ** ======================================================================= */ +#include + #undef setprogdir /* @@ -190,12 +201,12 @@ static void pusherror (lua_State *L) { lua_pushfstring(L, "system error %d\n", error); } -static void ll_unloadlib (void *lib) { +static void lsys_unloadlib (void *lib) { FreeLibrary((HMODULE)lib); } -static void *ll_load (lua_State *L, const char *path, int seeglb) { +static void *lsys_load (lua_State *L, const char *path, int seeglb) { HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS); (void)(seeglb); /* not used: symbols are 'global' by default */ if (lib == NULL) pusherror(L); @@ -203,7 +214,7 @@ static void *ll_load (lua_State *L, const char *path, int seeglb) { } -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { +static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym); if (f == NULL) pusherror(L); return f; @@ -212,7 +223,7 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { /* }====================================================== */ -#else +#else /* }{ */ /* ** {====================================================== ** Fallback for other systems @@ -226,31 +237,34 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { #define DLMSG "dynamic libraries not enabled; check your Lua installation" -static void ll_unloadlib (void *lib) { +static void lsys_unloadlib (void *lib) { (void)(lib); /* not used */ } -static void *ll_load (lua_State *L, const char *path, int seeglb) { +static void *lsys_load (lua_State *L, const char *path, int seeglb) { (void)(path); (void)(seeglb); /* not used */ lua_pushliteral(L, DLMSG); return NULL; } -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { +static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { (void)(lib); (void)(sym); /* not used */ lua_pushliteral(L, DLMSG); return NULL; } /* }====================================================== */ -#endif +#endif /* } */ -static void *ll_checkclib (lua_State *L, const char *path) { +/* +** return registry.CLIBS[path] +*/ +static void *checkclib (lua_State *L, const char *path) { void *plib; - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); + lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); lua_getfield(L, -1, path); plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ lua_pop(L, 2); /* pop CLIBS table and 'plib' */ @@ -258,8 +272,12 @@ static void *ll_checkclib (lua_State *L, const char *path) { } -static void ll_addtoclib (lua_State *L, const char *path, void *plib) { - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); +/* +** registry.CLIBS[path] = plib -- for queries +** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries +*/ +static void addtoclib (lua_State *L, const char *path, void *plib) { + lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); lua_pushlightuserdata(L, plib); lua_pushvalue(L, -1); lua_setfield(L, -3, path); /* CLIBS[path] = plib */ @@ -269,33 +287,49 @@ static void ll_addtoclib (lua_State *L, const char *path, void *plib) { /* -** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib +** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib ** handles in list CLIBS */ static int gctm (lua_State *L) { - int n = luaL_len(L, 1); + lua_Integer n = luaL_len(L, 1); for (; n >= 1; n--) { /* for each handle, in reverse order */ lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ - ll_unloadlib(lua_touserdata(L, -1)); + lsys_unloadlib(lua_touserdata(L, -1)); lua_pop(L, 1); /* pop handle */ } return 0; } -static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { - void *reg = ll_checkclib(L, path); /* check loaded C libraries */ + +/* error codes for 'lookforfunc' */ +#define ERRLIB 1 +#define ERRFUNC 2 + +/* +** Look for a C function named 'sym' in a dynamically loaded library +** 'path'. +** First, check whether the library is already loaded; if not, try +** to load it. +** Then, if 'sym' is '*', return true (as library has been loaded). +** Otherwise, look for symbol 'sym' in the library and push a +** C function with that symbol. +** Return 0 and 'true' or a function in the stack; in case of +** errors, return an error code and an error message in the stack. +*/ +static int lookforfunc (lua_State *L, const char *path, const char *sym) { + void *reg = checkclib(L, path); /* check loaded C libraries */ if (reg == NULL) { /* must load library? */ - reg = ll_load(L, path, *sym == '*'); + reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ if (reg == NULL) return ERRLIB; /* unable to load library */ - ll_addtoclib(L, path, reg); + addtoclib(L, path, reg); } if (*sym == '*') { /* loading only library (no function)? */ lua_pushboolean(L, 1); /* return 'true' */ return 0; /* no errors */ } else { - lua_CFunction f = ll_sym(L, reg, sym); + lua_CFunction f = lsys_sym(L, reg, sym); if (f == NULL) return ERRFUNC; /* unable to find function */ lua_pushcfunction(L, f); /* else create new function */ @@ -307,7 +341,7 @@ static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { static int ll_loadlib (lua_State *L) { const char *path = luaL_checkstring(L, 1); const char *init = luaL_checkstring(L, 2); - int stat = ll_loadfunc(L, path, init); + int stat = lookforfunc(L, path, init); if (stat == 0) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ @@ -360,7 +394,7 @@ static const char *searchpath (lua_State *L, const char *name, lua_remove(L, -2); /* remove path template */ if (readable(filename)) /* does file exist and is readable? */ return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file " LUA_QS, filename); + lua_pushfstring(L, "\n\tno file '%s'", filename); lua_remove(L, -2); /* remove file name */ luaL_addvalue(&msg); /* concatenate error msg. entry */ } @@ -390,7 +424,7 @@ static const char *findfile (lua_State *L, const char *name, lua_getfield(L, lua_upvalueindex(1), pname); path = lua_tostring(L, -1); if (path == NULL) - luaL_error(L, LUA_QL("package.%s") " must be a string", pname); + luaL_error(L, "'package.%s' must be a string", pname); return searchpath(L, name, path, ".", dirsep); } @@ -401,8 +435,7 @@ static int checkload (lua_State *L, int stat, const char *filename) { return 2; /* return open function and file name */ } else - return luaL_error(L, "error loading module " LUA_QS - " from file " LUA_QS ":\n\t%s", + return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", lua_tostring(L, 1), filename, lua_tostring(L, -1)); } @@ -416,21 +449,29 @@ static int searcher_Lua (lua_State *L) { } +/* +** Try to find a load function for module 'modname' at file 'filename'. +** First, change '.' to '_' in 'modname'; then, if 'modname' has +** the form X-Y (that is, it has an "ignore mark"), build a function +** name "luaopen_X" and look for it. (For compatibility, if that +** fails, it also tries "luaopen_Y".) If there is no ignore mark, +** look for a function named "luaopen_modname". +*/ static int loadfunc (lua_State *L, const char *filename, const char *modname) { - const char *funcname; + const char *openfunc; const char *mark; modname = luaL_gsub(L, modname, ".", LUA_OFSEP); mark = strchr(modname, *LUA_IGMARK); if (mark) { int stat; - funcname = lua_pushlstring(L, modname, mark - modname); - funcname = lua_pushfstring(L, LUA_POF"%s", funcname); - stat = ll_loadfunc(L, filename, funcname); + openfunc = lua_pushlstring(L, modname, mark - modname); + openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); + stat = lookforfunc(L, filename, openfunc); if (stat != ERRFUNC) return stat; modname = mark + 1; /* else go ahead and try old-style name */ } - funcname = lua_pushfstring(L, LUA_POF"%s", modname); - return ll_loadfunc(L, filename, funcname); + openfunc = lua_pushfstring(L, LUA_POF"%s", modname); + return lookforfunc(L, filename, openfunc); } @@ -455,8 +496,7 @@ static int searcher_Croot (lua_State *L) { if (stat != ERRFUNC) return checkload(L, 0, filename); /* real error */ else { /* open function not found */ - lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, - name, filename); + lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename); return 1; } } @@ -468,8 +508,7 @@ static int searcher_Croot (lua_State *L) { static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); - lua_getfield(L, -1, name); - if (lua_isnil(L, -1)) /* not found? */ + if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ lua_pushfstring(L, "\n\tno field package.preload['%s']", name); return 1; } @@ -479,17 +518,15 @@ static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ luaL_buffinit(L, &msg); - lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */ - if (!lua_istable(L, 3)) - luaL_error(L, LUA_QL("package.searchers") " must be a table"); + /* push 'package.searchers' to index 3 in the stack */ + if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) + luaL_error(L, "'package.searchers' must be a table"); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { - lua_rawgeti(L, 3, i); /* get a searcher */ - if (lua_isnil(L, -1)) { /* no more searchers? */ + if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ luaL_pushresult(&msg); /* create error message */ - luaL_error(L, "module " LUA_QS " not found:%s", - name, lua_tostring(L, -1)); + luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } lua_pushstring(L, name); lua_call(L, 1, 2); /* call it */ @@ -520,8 +557,7 @@ static int ll_require (lua_State *L) { lua_call(L, 2, 1); /* run loader to load module */ if (!lua_isnil(L, -1)) /* non-nil return? */ lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ - lua_getfield(L, 2, name); - if (lua_isnil(L, -1)) { /* module did not set a value? */ + if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ lua_pushboolean(L, 1); /* use true as result */ lua_pushvalue(L, -1); /* extra copy to be returned */ lua_setfield(L, 2, name); /* _LOADED[name] = true */ @@ -548,7 +584,7 @@ static void set_env (lua_State *L) { if (lua_getstack(L, 1, &ar) == 0 || lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ lua_iscfunction(L, -1)) - luaL_error(L, LUA_QL("module") " not called from a Lua function"); + luaL_error(L, "'module' not called from a Lua function"); lua_pushvalue(L, -2); /* copy new environment table to top */ lua_setupvalue(L, -2, 1); lua_pop(L, 1); /* remove function */ @@ -587,9 +623,8 @@ static int ll_module (lua_State *L) { int lastarg = lua_gettop(L); /* last parameter */ luaL_pushmodule(L, modname, 1); /* get/create module table */ /* check whether table already has a _NAME field */ - lua_getfield(L, -1, "_NAME"); - if (!lua_isnil(L, -1)) /* is table an initialized module? */ - lua_pop(L, 1); + if (lua_getfield(L, -1, "_NAME") != LUA_TNIL) + lua_pop(L, 1); /* table is an initialized module */ else { /* no; initialize it */ lua_pop(L, 1); modinit(L, modname); @@ -659,6 +694,12 @@ static const luaL_Reg pk_funcs[] = { #if defined(LUA_COMPAT_MODULE) {"seeall", ll_seeall}, #endif + /* placeholders */ + {"preload", NULL}, + {"cpath", NULL}, + {"path", NULL}, + {"searchers", NULL}, + {"loaded", NULL}, {NULL, NULL} }; @@ -684,36 +725,44 @@ static void createsearcherstable (lua_State *L) { lua_pushcclosure(L, searchers[i], 1); lua_rawseti(L, -2, i+1); } +#if defined(LUA_COMPAT_LOADERS) + lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ + lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */ +#endif + lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ +} + + +/* +** create table CLIBS to keep track of loaded C libraries, +** setting a finalizer to close all libraries when closing state. +*/ +static void createclibstable (lua_State *L) { + lua_newtable(L); /* create CLIBS table */ + lua_createtable(L, 0, 1); /* create metatable for CLIBS */ + lua_pushcfunction(L, gctm); + lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ + lua_setmetatable(L, -2); + lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */ } LUAMOD_API int luaopen_package (lua_State *L) { - /* create table CLIBS to keep track of loaded C libraries */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); - lua_createtable(L, 0, 1); /* metatable for CLIBS */ - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ - lua_setmetatable(L, -2); - /* create `package' table */ - luaL_newlib(L, pk_funcs); + createclibstable(L); + luaL_newlib(L, pk_funcs); /* create 'package' table */ createsearcherstable(L); -#if defined(LUA_COMPAT_LOADERS) - lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ - lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */ -#endif - lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ /* set field 'path' */ - setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT); + setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT); /* set field 'cpath' */ - setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT); + setpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, LUA_CPATH_DEFAULT); /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); lua_setfield(L, -2, "config"); - /* set field `loaded' */ + /* set field 'loaded' */ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); lua_setfield(L, -2, "loaded"); - /* set field `preload' */ + /* set field 'preload' */ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); lua_setfield(L, -2, "preload"); lua_pushglobaltable(L); diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 882d994d..6a24aff9 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,17 +1,20 @@ /* -** $Id: lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lobject.c,v 2.101 2014/12/26 14:43:45 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ +#define lobject_c +#define LUA_CORE + +#include "lprefix.h" + + #include #include #include #include -#define lobject_c -#define LUA_CORE - #include "lua.h" #include "lctype.h" @@ -70,31 +73,92 @@ int luaO_ceillog2 (unsigned int x) { } -lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { +static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, + lua_Integer v2) { switch (op) { - case LUA_OPADD: return luai_numadd(NULL, v1, v2); - case LUA_OPSUB: return luai_numsub(NULL, v1, v2); - case LUA_OPMUL: return luai_nummul(NULL, v1, v2); - case LUA_OPDIV: return luai_numdiv(NULL, v1, v2); - case LUA_OPMOD: return luai_nummod(NULL, v1, v2); - case LUA_OPPOW: return luai_numpow(NULL, v1, v2); - case LUA_OPUNM: return luai_numunm(NULL, v1); + case LUA_OPADD: return intop(+, v1, v2); + case LUA_OPSUB:return intop(-, v1, v2); + case LUA_OPMUL:return intop(*, v1, v2); + case LUA_OPMOD: return luaV_mod(L, v1, v2); + case LUA_OPIDIV: return luaV_div(L, v1, v2); + case LUA_OPBAND: return intop(&, v1, v2); + case LUA_OPBOR: return intop(|, v1, v2); + case LUA_OPBXOR: return intop(^, v1, v2); + case LUA_OPSHL: return luaV_shiftl(v1, v2); + case LUA_OPSHR: return luaV_shiftl(v1, -v2); + case LUA_OPUNM: return intop(-, 0, v1); + case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); default: lua_assert(0); return 0; } } +static lua_Number numarith (lua_State *L, int op, lua_Number v1, + lua_Number v2) { + switch (op) { + case LUA_OPADD: return luai_numadd(L, v1, v2); + case LUA_OPSUB: return luai_numsub(L, v1, v2); + case LUA_OPMUL: return luai_nummul(L, v1, v2); + case LUA_OPDIV: return luai_numdiv(L, v1, v2); + case LUA_OPPOW: return luai_numpow(L, v1, v2); + case LUA_OPIDIV: return luai_numidiv(L, v1, v2); + case LUA_OPUNM: return luai_numunm(L, v1); + case LUA_OPMOD: { + lua_Number m; + luai_nummod(L, v1, v2, m); + return m; + } + default: lua_assert(0); return 0; + } +} + + +void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, + TValue *res) { + switch (op) { + case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: + case LUA_OPSHL: case LUA_OPSHR: + case LUA_OPBNOT: { /* operate only on integers */ + lua_Integer i1; lua_Integer i2; + if (tointeger(p1, &i1) && tointeger(p2, &i2)) { + setivalue(res, intarith(L, op, i1, i2)); + return; + } + else break; /* go to the end */ + } + case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ + lua_Number n1; lua_Number n2; + if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + setfltvalue(res, numarith(L, op, n1, n2)); + return; + } + else break; /* go to the end */ + } + default: { /* other operations */ + lua_Number n1; lua_Number n2; + if (ttisinteger(p1) && ttisinteger(p2)) { + setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); + return; + } + else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + setfltvalue(res, numarith(L, op, n1, n2)); + return; + } + else break; /* go to the end */ + } + } + /* could not perform raw operation; try metamethod */ + lua_assert(L != NULL); /* should not fail when folding (compile time) */ + luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD)); +} + + int luaO_hexavalue (int c) { if (lisdigit(c)) return c - '0'; else return ltolower(c) - 'a' + 10; } -#if !defined(lua_strx2number) - -#include - - static int isneg (const char **s) { if (**s == '-') { (*s)++; return 1; } else if (**s == '+') (*s)++; @@ -102,80 +166,185 @@ static int isneg (const char **s) { } -static lua_Number readhexa (const char **s, lua_Number r, int *count) { - for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ - r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s))); - (*count)++; - } - return r; -} +/* +** {================================================================== +** Lua's implementation for 'lua_strx2number' +** =================================================================== +*/ +#if !defined(lua_strx2number) + +#include + +/* maximum number of significant digits to read (to avoid overflows + even with single floats) */ +#define MAXSIGDIG 30 /* ** convert an hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ static lua_Number lua_strx2number (const char *s, char **endptr) { - lua_Number r = 0.0; - int e = 0, i = 0; - int neg = 0; /* 1 if number is negative */ + lua_Number r = 0.0; /* result (accumulator) */ + int sigdig = 0; /* number of significant digits */ + int nosigdig = 0; /* number of non-significant digits */ + int e = 0; /* exponent correction */ + int neg; /* 1 if number is negative */ + int dot = 0; /* true after seen a dot */ *endptr = cast(char *, s); /* nothing is valid yet */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check signal */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ return 0.0; /* invalid format (no '0x') */ - s += 2; /* skip '0x' */ - r = readhexa(&s, r, &i); /* read integer part */ - if (*s == '.') { - s++; /* skip dot */ - r = readhexa(&s, r, &e); /* read fractional part */ + for (s += 2; ; s++) { /* skip '0x' and read numeral */ + if (*s == '.') { + if (dot) break; /* second dot? stop loop */ + else dot = 1; + } + else if (lisxdigit(cast_uchar(*s))) { + if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ + nosigdig++; + else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ + r = (r * cast_num(16.0)) + luaO_hexavalue(*s); + else e++; /* too many digits; ignore, but still count for exponent */ + if (dot) e--; /* decimal digit? correct exponent */ + } + else break; /* neither a dot nor a digit */ } - if (i == 0 && e == 0) - return 0.0; /* invalid format (no digit) */ - e *= -4; /* each fractional digit divides value by 2^-4 */ + if (nosigdig + sigdig == 0) /* no digits? */ + return 0.0; /* invalid format */ *endptr = cast(char *, s); /* valid up to here */ + e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ - int exp1 = 0; - int neg1; + int exp1 = 0; /* exponent value */ + int neg1; /* exponent signal */ s++; /* skip 'p' */ neg1 = isneg(&s); /* signal */ if (!lisdigit(cast_uchar(*s))) - goto ret; /* must have at least one digit */ + return 0.0; /* invalid; must have at least one digit */ while (lisdigit(cast_uchar(*s))) /* read exponent */ exp1 = exp1 * 10 + *(s++) - '0'; if (neg1) exp1 = -exp1; e += exp1; + *endptr = cast(char *, s); /* valid up to here */ } - *endptr = cast(char *, s); /* valid up to here */ - ret: if (neg) r = -r; return l_mathop(ldexp)(r, e); } #endif +/* }====================================================== */ -int luaO_str2d (const char *s, size_t len, lua_Number *result) { +static const char *l_str2d (const char *s, lua_Number *result) { char *endptr; if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ - return 0; - else if (strpbrk(s, "xX")) /* hexa? */ + return NULL; + else if (strpbrk(s, "xX")) /* hex? */ *result = lua_strx2number(s, &endptr); else *result = lua_str2number(s, &endptr); if (endptr == s) return 0; /* nothing recognized */ while (lisspace(cast_uchar(*endptr))) endptr++; - return (endptr == s + len); /* OK if no trailing characters */ + return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */ } +static const char *l_str2int (const char *s, lua_Integer *result) { + lua_Unsigned a = 0; + int empty = 1; + int neg; + while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ + neg = isneg(&s); + if (s[0] == '0' && + (s[1] == 'x' || s[1] == 'X')) { /* hex? */ + s += 2; /* skip '0x' */ + for (; lisxdigit(cast_uchar(*s)); s++) { + a = a * 16 + luaO_hexavalue(*s); + empty = 0; + } + } + else { /* decimal */ + for (; lisdigit(cast_uchar(*s)); s++) { + a = a * 10 + *s - '0'; + empty = 0; + } + } + while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ + if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ + else { + *result = l_castU2S((neg) ? 0u - a : a); + return s; + } +} + + +size_t luaO_str2num (const char *s, TValue *o) { + lua_Integer i; lua_Number n; + const char *e; + if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ + setivalue(o, i); + } + else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ + setfltvalue(o, n); + } + else + return 0; /* conversion failed */ + return (e - s + 1); /* success; return string size */ +} + + +int luaO_utf8esc (char *buff, unsigned long x) { + int n = 1; /* number of bytes put in buffer (backwards) */ + lua_assert(x <= 0x10FFFF); + if (x < 0x80) /* ascii? */ + buff[UTF8BUFFSZ - 1] = cast(char, x); + else { /* need continuation bytes */ + unsigned int mfb = 0x3f; /* maximum that fits in first byte */ + do { /* add continuation bytes */ + buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f)); + x >>= 6; /* remove added bits */ + mfb >>= 1; /* now there is one less bit available in first byte */ + } while (x > mfb); /* still needs continuation byte? */ + buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */ + } + return n; +} + + +/* maximum length of the conversion of a number to a string */ +#define MAXNUMBER2STR 50 + + +/* +** Convert a number object to a string +*/ +void luaO_tostring (lua_State *L, StkId obj) { + char buff[MAXNUMBER2STR]; + size_t len; + lua_assert(ttisnumber(obj)); + if (ttisinteger(obj)) + len = lua_integer2str(buff, ivalue(obj)); + else { + len = lua_number2str(buff, fltvalue(obj)); +#if !defined(LUA_COMPAT_FLOATSTRING) + if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ + buff[len++] = '.'; + buff[len++] = '0'; /* adds '.0' to result */ + } +#endif + } + setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); +} + static void pushstr (lua_State *L, const char *str, size_t l) { setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); } -/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ +/* this function handles only '%d', '%c', '%f', '%p', and '%s' + conventional formats, plus Lua-specific '%I' and '%U' */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { int n = 0; for (;;) { @@ -191,33 +360,47 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'c': { - char buff; - buff = cast(char, va_arg(argp, int)); - pushstr(L, &buff, 1); + char buff = cast(char, va_arg(argp, int)); + if (lisprint(cast_uchar(buff))) + pushstr(L, &buff, 1); + else /* non-printable character; print its code */ + luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); break; } case 'd': { - setnvalue(L->top++, cast_num(va_arg(argp, int))); + setivalue(L->top++, va_arg(argp, int)); + luaO_tostring(L, L->top - 1); + break; + } + case 'I': { + setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt))); + luaO_tostring(L, L->top - 1); break; } case 'f': { - setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); + setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); + luaO_tostring(L, L->top - 1); break; } case 'p': { - char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ + char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ int l = sprintf(buff, "%p", va_arg(argp, void *)); pushstr(L, buff, l); break; } + case 'U': { + char buff[UTF8BUFFSZ]; + int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); + pushstr(L, buff + UTF8BUFFSZ - l, l); + break; + } case '%': { pushstr(L, "%", 1); break; } default: { - luaG_runerror(L, - "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"), - *(e + 1)); + luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", + *(e + 1)); } } n += 2; diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 1428725b..77b4e470 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.71.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lobject.h,v 2.105 2014/12/19 13:36:32 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -20,13 +20,12 @@ ** Extra tags for non-values */ #define LUA_TPROTO LUA_NUMTAGS -#define LUA_TUPVAL (LUA_NUMTAGS+1) -#define LUA_TDEADKEY (LUA_NUMTAGS+2) +#define LUA_TDEADKEY (LUA_NUMTAGS+1) /* ** number of all possible tags (including LUA_TNONE but excluding DEADKEY) */ -#define LUA_TOTALTAGS (LUA_TUPVAL+2) +#define LUA_TOTALTAGS (LUA_TPROTO + 2) /* @@ -57,6 +56,11 @@ #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ +/* Variant tags for numbers */ +#define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ +#define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ + + /* Bit mark for collectable types */ #define BIT_ISCOLLECTABLE (1 << 6) @@ -65,9 +69,9 @@ /* -** Union of all collectable objects +** Common type for all collectable objects */ -typedef union GCObject GCObject; +typedef struct GCObject GCObject; /* @@ -78,11 +82,11 @@ typedef union GCObject GCObject; /* -** Common header in struct form +** Common type has only the common header */ -typedef struct GCheader { +struct GCObject { CommonHeader; -} GCheader; +}; @@ -92,8 +96,6 @@ typedef struct GCheader { typedef union Value Value; -#define numfield lua_Number n; /* numbers */ - /* @@ -111,7 +113,6 @@ typedef struct lua_TValue TValue; #define val_(o) ((o)->value_) -#define num_(o) (val_(o).n) /* raw type tag of a TValue */ @@ -124,13 +125,15 @@ typedef struct lua_TValue TValue; #define ttype(o) (rttype(o) & 0x3F) /* type tag of a TValue with no variants (bits 0-3) */ -#define ttypenv(o) (novariant(rttype(o))) +#define ttnov(o) (novariant(rttype(o))) /* Macros to test type */ #define checktag(o,t) (rttype(o) == (t)) -#define checktype(o,t) (ttypenv(o) == (t)) -#define ttisnumber(o) checktag((o), LUA_TNUMBER) +#define checktype(o,t) (ttnov(o) == (t)) +#define ttisnumber(o) checktype((o), LUA_TNUMBER) +#define ttisfloat(o) checktag((o), LUA_TNUMFLT) +#define ttisinteger(o) checktag((o), LUA_TNUMINT) #define ttisnil(o) checktag((o), LUA_TNIL) #define ttisboolean(o) checktag((o), LUA_TBOOLEAN) #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) @@ -143,27 +146,27 @@ typedef struct lua_TValue TValue; #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) #define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA)) +#define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) -#define ttisequal(o1,o2) (rttype(o1) == rttype(o2)) /* Macros to access values */ -#define nvalue(o) check_exp(ttisnumber(o), num_(o)) +#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) +#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) +#define nvalue(o) check_exp(ttisnumber(o), \ + (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) -#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts) -#define tsvalue(o) (&rawtsvalue(o)->tsv) -#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u) -#define uvalue(o) (&rawuvalue(o)->uv) -#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl) -#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l) -#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c) +#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) +#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) +#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) +#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) +#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) #define fvalue(o) check_exp(ttislcf(o), val_(o).f) -#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h) +#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) #define bvalue(o) check_exp(ttisboolean(o), val_(o).b) -#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th) +#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) /* a dead value may get the 'gc' field, but cannot access its contents */ #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc)) @@ -174,7 +177,7 @@ typedef struct lua_TValue TValue; /* Macros for internal tests */ -#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt) +#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) #define checkliveness(g,obj) \ lua_longassert(!iscollectable(obj) || \ @@ -184,8 +187,11 @@ typedef struct lua_TValue TValue; /* Macros to set values */ #define settt_(o,t) ((o)->tt_=(t)) -#define setnvalue(obj,x) \ - { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); } +#define setfltvalue(obj,x) \ + { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } + +#define setivalue(obj,x) \ + { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } #define setnilvalue(obj) settt_(obj, LUA_TNIL) @@ -199,38 +205,37 @@ typedef struct lua_TValue TValue; { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } #define setgcovalue(L,obj,x) \ - { TValue *io=(obj); GCObject *i_g=(x); \ - val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); } + { TValue *io = (obj); GCObject *i_g=(x); \ + val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } #define setsvalue(L,obj,x) \ - { TValue *io=(obj); \ - TString *x_ = (x); \ - val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \ + { TValue *io = (obj); TString *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ checkliveness(G(L),io); } #define setuvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \ + { TValue *io = (obj); Udata *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ checkliveness(G(L),io); } #define setthvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \ + { TValue *io = (obj); lua_State *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ checkliveness(G(L),io); } #define setclLvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \ + { TValue *io = (obj); LClosure *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ checkliveness(G(L),io); } #define setclCvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \ + { TValue *io = (obj); CClosure *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ checkliveness(G(L),io); } #define sethvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \ + { TValue *io = (obj); Table *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ checkliveness(G(L),io); } #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) @@ -238,9 +243,8 @@ typedef struct lua_TValue TValue; #define setobj(L,obj1,obj2) \ - { const TValue *io2=(obj2); TValue *io1=(obj1); \ - io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - checkliveness(G(L),io1); } + { TValue *io1=(obj1); *io1 = *(obj2); \ + (void)L; checkliveness(G(L),io1); } /* @@ -263,119 +267,6 @@ typedef struct lua_TValue TValue; #define setsvalue2n setsvalue -/* check whether a number is valid (useful only for NaN trick) */ -#define luai_checknum(L,o,c) { /* empty */ } - - -/* -** {====================================================== -** NaN Trick -** ======================================================= -*/ -#if defined(LUA_NANTRICK) - -/* -** numbers are represented in the 'd_' field. All other values have the -** value (NNMARK | tag) in 'tt__'. A number with such pattern would be -** a "signaled NaN", which is never generated by regular operations by -** the CPU (nor by 'strtod') -*/ - -/* allows for external implementation for part of the trick */ -#if !defined(NNMARK) /* { */ - - -#if !defined(LUA_IEEEENDIAN) -#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN' -#endif - - -#define NNMARK 0x7FF7A500 -#define NNMASK 0x7FFFFF00 - -#undef TValuefields -#undef NILCONSTANT - -#if (LUA_IEEEENDIAN == 0) /* { */ - -/* little endian */ -#define TValuefields \ - union { struct { Value v__; int tt__; } i; double d__; } u -#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}} -/* field-access macros */ -#define v_(o) ((o)->u.i.v__) -#define d_(o) ((o)->u.d__) -#define tt_(o) ((o)->u.i.tt__) - -#else /* }{ */ - -/* big endian */ -#define TValuefields \ - union { struct { int tt__; Value v__; } i; double d__; } u -#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}} -/* field-access macros */ -#define v_(o) ((o)->u.i.v__) -#define d_(o) ((o)->u.d__) -#define tt_(o) ((o)->u.i.tt__) - -#endif /* } */ - -#endif /* } */ - - -/* correspondence with standard representation */ -#undef val_ -#define val_(o) v_(o) -#undef num_ -#define num_(o) d_(o) - - -#undef numfield -#define numfield /* no such field; numbers are the entire struct */ - -/* basic check to distinguish numbers from non-numbers */ -#undef ttisnumber -#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK) - -#define tag2tt(t) (NNMARK | (t)) - -#undef rttype -#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff) - -#undef settt_ -#define settt_(o,t) (tt_(o) = tag2tt(t)) - -#undef setnvalue -#define setnvalue(obj,x) \ - { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); } - -#undef setobj -#define setobj(L,obj1,obj2) \ - { const TValue *o2_=(obj2); TValue *o1_=(obj1); \ - o1_->u = o2_->u; \ - checkliveness(G(L),o1_); } - - -/* -** these redefinitions are not mandatory, but these forms are more efficient -*/ - -#undef checktag -#undef checktype -#define checktag(o,t) (tt_(o) == tag2tt(t)) -#define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS)) - -#undef ttisequal -#define ttisequal(o1,o2) \ - (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2))) - - -#undef luai_checknum -#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; } - -#endif -/* }====================================================== */ - /* @@ -390,7 +281,8 @@ union Value { void *p; /* light userdata */ int b; /* booleans */ lua_CFunction f; /* light C functions */ - numfield /* numbers */ + lua_Integer i; /* integer numbers */ + lua_Number n; /* float numbers */ }; @@ -406,39 +298,78 @@ typedef TValue *StkId; /* index to stack elements */ /* ** Header for string value; string bytes follow the end of this structure +** (aligned according to 'UTString'; see next). */ -typedef union TString { - L_Umaxalign dummy; /* ensures maximum alignment for strings */ - struct { - CommonHeader; - lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - unsigned int hash; - size_t len; /* number of characters in string */ - } tsv; +typedef struct TString { + CommonHeader; + lu_byte extra; /* reserved words for short strings; "has hash" for longs */ + unsigned int hash; + size_t len; /* number of characters in string */ + struct TString *hnext; /* linked list for hash table */ } TString; -/* get the actual string (array of bytes) from a TString */ -#define getstr(ts) cast(const char *, (ts) + 1) +/* +** Ensures that address after this type is always fully aligned. +*/ +typedef union UTString { + L_Umaxalign dummy; /* ensures maximum alignment for strings */ + TString tsv; +} UTString; + + +/* +** Get the actual string (array of bytes) from a 'TString'. +** (Access to 'extra' ensures that value is really a 'TString'.) +*/ +#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString)) +#define getstr(ts) \ + check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts))) /* get the actual string (array of bytes) from a Lua value */ -#define svalue(o) getstr(rawtsvalue(o)) +#define svalue(o) getstr(tsvalue(o)) /* ** Header for userdata; memory area follows the end of this structure +** (aligned according to 'UUdata'; see next). */ -typedef union Udata { - L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ - struct { - CommonHeader; - struct Table *metatable; - struct Table *env; - size_t len; /* number of bytes */ - } uv; +typedef struct Udata { + CommonHeader; + lu_byte ttuv_; /* user value's tag */ + struct Table *metatable; + size_t len; /* number of bytes */ + union Value user_; /* user value */ } Udata; +/* +** Ensures that address after this type is always fully aligned. +*/ +typedef union UUdata { + L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */ + Udata uv; +} UUdata; + + +/* +** Get the address of memory block inside 'Udata'. +** (Access to 'ttuv_' ensures that value is really a 'Udata'.) +*/ +#define getudatamem(u) \ + check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata))) + +#define setuservalue(L,u,o) \ + { const TValue *io=(o); Udata *iu = (u); \ + iu->user_ = io->value_; iu->ttuv_ = io->tt_; \ + checkliveness(G(L),io); } + + +#define getuservalue(L,u,o) \ + { TValue *io=(o); const Udata *iu = (u); \ + io->value_ = iu->user_; io->tt_ = iu->ttuv_; \ + checkliveness(G(L),io); } + /* ** Description of an upvalue for function prototypes @@ -461,35 +392,30 @@ typedef struct LocVar { } LocVar; -typedef struct SharedProto { - void *l_G; /* global state belongs to */ - Instruction *code; - int *lineinfo; /* map from opcodes to source lines (debug information) */ - LocVar *locvars; /* information about local variables (debug information) */ - Upvaldesc *upvalues; /* upvalue information */ - TString *source; /* used for debug information */ - int sizeupvalues; /* size of 'upvalues' */ - int sizek; /* size of `k' */ - int sizecode; - int sizelineinfo; - int sizep; /* size of `p' */ - int sizelocvars; - int linedefined; - int lastlinedefined; - lu_byte numparams; /* number of fixed parameters */ - lu_byte is_vararg; - lu_byte maxstacksize; /* maximum stack used by this function */ -} SharedProto; - /* ** Function Prototypes */ typedef struct Proto { CommonHeader; + lu_byte numparams; /* number of fixed parameters */ + lu_byte is_vararg; + lu_byte maxstacksize; /* maximum stack used by this function */ + int sizeupvalues; /* size of 'upvalues' */ + int sizek; /* size of 'k' */ + int sizecode; + int sizelineinfo; + int sizep; /* size of 'p' */ + int sizelocvars; + int linedefined; + int lastlinedefined; TValue *k; /* constants used by the function */ - struct SharedProto *sp; + Instruction *code; struct Proto **p; /* functions defined inside the function */ - union Closure *cache; /* last created closure with this prototype */ + int *lineinfo; /* map from opcodes to source lines (debug information) */ + LocVar *locvars; /* information about local variables (debug information) */ + Upvaldesc *upvalues; /* upvalue information */ + struct LClosure *cache; /* last created closure with this prototype */ + TString *source; /* used for debug information */ GCObject *gclist; } Proto; @@ -498,17 +424,7 @@ typedef struct Proto { /* ** Lua Upvalues */ -typedef struct UpVal { - CommonHeader; - TValue *v; /* points to stack or to its own value */ - union { - TValue value; /* the value (when closed) */ - struct { /* double linked list (when open) */ - struct UpVal *prev; - struct UpVal *next; - } l; - } u; -} UpVal; +typedef struct UpVal UpVal; /* @@ -550,12 +466,19 @@ typedef union Closure { typedef union TKey { struct { TValuefields; - struct Node *next; /* for chaining */ + int next; /* for chaining (offset for next node) */ } nk; TValue tvk; } TKey; +/* copy a value into a key without messing up field 'next' */ +#define setkey(L,key,obj) \ + { TKey *k_=(key); const TValue *io_=(obj); \ + k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \ + (void)L; checkliveness(G(L),io_); } + + typedef struct Node { TValue i_val; TKey i_key; @@ -565,19 +488,19 @@ typedef struct Node { typedef struct Table { CommonHeader; lu_byte flags; /* 1<

>1) /* `sBx' is signed */ +#define MAXARG_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */ #else #define MAXARG_Bx MAX_INT #define MAXARG_sBx MAX_INT @@ -76,10 +76,10 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define MAXARG_C ((1<> RK(C) */ OP_UNM,/* A B R(A) := -R(B) */ +OP_BNOT,/* A B R(A) := ~R(B) */ OP_NOT,/* A B R(A) := not R(B) */ OP_LEN,/* A B R(A) := length of R(B) */ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ -OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */ +OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */ OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ @@ -231,16 +238,16 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ /*=========================================================================== Notes: - (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is + (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is set to last_result+1, so next open instruction (OP_CALL, OP_RETURN, - OP_SETLIST) may use `top'. + OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (B == 0) then use actual number of varargs and set top (like in OP_CALL with C == 0). - (*) In OP_RETURN, if (B == 0) then return up to `top'. + (*) In OP_RETURN, if (B == 0) then return up to 'top'. - (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next + (*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next 'instruction' is EXTRAARG(real C). (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. @@ -248,7 +255,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) For comparisons, A specifies what condition the test should accept (true or false). - (*) All `skips' (pc++) assume that next instruction is a jump. + (*) All 'skips' (pc++) assume that next instruction is a jump. ===========================================================================*/ diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 052ba174..20359b24 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,9 +1,14 @@ /* -** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: loslib.c,v 1.54 2014/12/26 14:46:07 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ +#define loslib_c +#define LUA_LIB + +#include "lprefix.h" + #include #include @@ -11,69 +16,96 @@ #include #include -#define loslib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" #include "lualib.h" +#if !defined(LUA_STRFTIMEOPTIONS) /* { */ /* ** list of valid conversion specifiers for the 'strftime' function */ -#if !defined(LUA_STRFTIMEOPTIONS) -#if !defined(LUA_USE_POSIX) +#if defined(LUA_USE_C89) #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } -#else +#else /* C99 specification */ #define LUA_STRFTIMEOPTIONS \ - { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \ - "", "E", "cCxXyY", \ + { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \ + "E", "cCxXyY", \ "O", "deHImMSuUVwWy" } #endif -#endif +#endif /* } */ +#if !defined(l_time_t) /* { */ +/* +** type to represent time_t in Lua +*/ +#define l_timet lua_Integer +#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) +#define l_checktime(L,a) ((time_t)luaL_checkinteger(L,a)) + +#endif /* } */ + + + +#if !defined(lua_tmpnam) /* { */ /* ** By default, Lua uses tmpnam except when POSIX is available, where it ** uses mkstemp. */ -#if defined(LUA_USE_MKSTEMP) + +#if defined(LUA_USE_POSIX) /* { */ + #include + #define LUA_TMPNAMBUFSIZE 32 + +#if !defined(LUA_TMPNAMTEMPLATE) +#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" +#endif + #define lua_tmpnam(b,e) { \ - strcpy(b, "/tmp/lua_XXXXXX"); \ + strcpy(b, LUA_TMPNAMTEMPLATE); \ e = mkstemp(b); \ if (e != -1) close(e); \ e = (e == -1); } -#elif !defined(lua_tmpnam) +#else /* }{ */ +/* ISO C definitions */ #define LUA_TMPNAMBUFSIZE L_tmpnam #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } -#endif +#endif /* } */ + +#endif /* } */ + +#if !defined(l_gmtime) /* { */ /* ** By default, Lua uses gmtime/localtime, except when POSIX is available, ** where it uses gmtime_r/localtime_r */ -#if defined(LUA_USE_GMTIME_R) + +#if defined(LUA_USE_POSIX) /* { */ #define l_gmtime(t,r) gmtime_r(t,r) #define l_localtime(t,r) localtime_r(t,r) -#elif !defined(l_gmtime) +#else /* }{ */ -#define l_gmtime(t,r) ((void)r, gmtime(t)) -#define l_localtime(t,r) ((void)r, localtime(t)) +/* ISO C definitions */ +#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) +#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) -#endif +#endif /* } */ + +#endif /* } */ @@ -147,8 +179,7 @@ static void setboolfield (lua_State *L, const char *key, int value) { static int getboolfield (lua_State *L, const char *key) { int res; - lua_getfield(L, -1, key); - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); + res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); lua_pop(L, 1); return res; } @@ -160,7 +191,7 @@ static int getfield (lua_State *L, const char *key, int d) { res = (int)lua_tointegerx(L, -1, &isnum); if (!isnum) { if (d < 0) - return luaL_error(L, "field " LUA_QS " missing in date table", key); + return luaL_error(L, "field '%s' missing in date table", key); res = d; } lua_pop(L, 1); @@ -194,11 +225,11 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { static int os_date (lua_State *L) { const char *s = luaL_optstring(L, 1, "%c"); - time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); + time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); - s++; /* skip `!' */ + s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); @@ -255,17 +286,19 @@ static int os_time (lua_State *L) { ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); } - if (t == (time_t)(-1)) + if (t != (time_t)(l_timet)t) + luaL_error(L, "time result cannot be represented in this Lua instalation"); + else if (t == (time_t)(-1)) lua_pushnil(L); else - lua_pushnumber(L, (lua_Number)t); + l_pushtime(L, t); return 1; } static int os_difftime (lua_State *L) { - lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), - (time_t)(luaL_optnumber(L, 2, 0)))); + double res = difftime((l_checktime(L, 1)), (l_checktime(L, 2))); + lua_pushnumber(L, (lua_Number)res); return 1; } @@ -289,7 +322,7 @@ static int os_exit (lua_State *L) { if (lua_isboolean(L, 1)) status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); else - status = luaL_optint(L, 1, EXIT_SUCCESS); + status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS); if (lua_toboolean(L, 2)) lua_close(L); if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index a8e37fdd..9a54dfc9 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,15 +1,17 @@ /* -** $Id: lparser.c,v 2.130.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lparser.c,v 2.147 2014/12/27 20:31:43 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ - -#include - #define lparser_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "lcode.h" @@ -35,17 +37,21 @@ #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) +/* because all strings are unified by the scanner, the parser + can use pointer equality for string equality */ +#define eqstr(a,b) ((a) == (b)) + /* ** nodes for block list (list of active blocks) */ typedef struct BlockCnt { struct BlockCnt *previous; /* chain */ - short firstlabel; /* index of first label in this block */ - short firstgoto; /* index of first pending goto in this block */ + int firstlabel; /* index of first label in this block */ + int firstgoto; /* index of first pending goto in this block */ lu_byte nactvar; /* # active locals outside the block */ lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isloop; /* true if `block' is a loop */ + lu_byte isloop; /* true if 'block' is a loop */ } BlockCnt; @@ -57,19 +63,9 @@ static void statement (LexState *ls); static void expr (LexState *ls, expdesc *v); -static void anchor_token (LexState *ls) { - /* last token from outer function must be EOS */ - lua_assert(ls->fs != NULL || ls->t.token == TK_EOS); - if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { - TString *ts = ls->t.seminfo.ts; - luaX_newstring(ls, getstr(ts), ts->tsv.len); - } -} - - /* semantic error */ static l_noret semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove 'near to' from final message */ + ls->t.token = 0; /* remove "near " from final message */ luaX_syntaxerror(ls, msg); } @@ -83,7 +79,7 @@ static l_noret error_expected (LexState *ls, int token) { static l_noret errorlimit (FuncState *fs, int limit, const char *what) { lua_State *L = fs->ls->L; const char *msg; - int line = fs->f->sp->linedefined; + int line = fs->f->linedefined; const char *where = (line == 0) ? "main function" : luaO_pushfstring(L, "function at line %d", line); @@ -164,14 +160,13 @@ static void checkname (LexState *ls, expdesc *e) { static int registerlocalvar (LexState *ls, TString *varname) { FuncState *fs = ls->fs; - Proto *fp = fs->f; - SharedProto *f = fp->sp; + Proto *f = fs->f; int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, fp, varname); + luaC_objbarrier(ls->L, f, varname); return fs->nlocvars++; } @@ -199,7 +194,7 @@ static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { static LocVar *getlocvar (FuncState *fs, int i) { int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; lua_assert(idx < fs->nlocvars); - return &fs->f->sp->locvars[idx]; + return &fs->f->locvars[idx]; } @@ -221,17 +216,16 @@ static void removevars (FuncState *fs, int tolevel) { static int searchupvalue (FuncState *fs, TString *name) { int i; - Upvaldesc *up = fs->f->sp->upvalues; + Upvaldesc *up = fs->f->upvalues; for (i = 0; i < fs->nups; i++) { - if (luaS_eqstr(up[i].name, name)) return i; + if (eqstr(up[i].name, name)) return i; } return -1; /* not found */ } static int newupvalue (FuncState *fs, TString *name, expdesc *v) { - Proto *fp = fs->f; - SharedProto *f = fp->sp; + Proto *f = fs->f; int oldsize = f->sizeupvalues; checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, @@ -240,7 +234,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { f->upvalues[fs->nups].instack = (v->k == VLOCAL); f->upvalues[fs->nups].idx = cast_byte(v->u.info); f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, fp, name); + luaC_objbarrier(fs->ls->L, f, name); return fs->nups++; } @@ -248,7 +242,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { static int searchvar (FuncState *fs, TString *n) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (luaS_eqstr(n, getlocvar(fs, i)->varname)) + if (eqstr(n, getlocvar(fs, i)->varname)) return i; } return -1; /* not found */ @@ -344,11 +338,11 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { FuncState *fs = ls->fs; Labellist *gl = &ls->dyd->gt; Labeldesc *gt = &gl->arr[g]; - lua_assert(luaS_eqstr(gt->name, label->name)); + lua_assert(eqstr(gt->name, label->name)); if (gt->nactvar < label->nactvar) { TString *vname = getlocvar(fs, gt->nactvar)->varname; const char *msg = luaO_pushfstring(ls->L, - " at line %d jumps into the scope of local " LUA_QS, + " at line %d jumps into the scope of local '%s'", getstr(gt->name), gt->line, getstr(vname)); semerror(ls, msg); } @@ -371,7 +365,7 @@ static int findlabel (LexState *ls, int g) { /* check labels in current block for a match */ for (i = bl->firstlabel; i < dyd->label.n; i++) { Labeldesc *lb = &dyd->label.arr[i]; - if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */ + if (eqstr(lb->name, gt->name)) { /* correct label? */ if (gt->nactvar > lb->nactvar && (bl->upval || dyd->label.n > bl->firstlabel)) luaK_patchclose(ls->fs, gt->pc, lb->nactvar); @@ -392,7 +386,7 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, l->arr[n].line = line; l->arr[n].nactvar = ls->fs->nactvar; l->arr[n].pc = pc; - l->n++; + l->n = n + 1; return n; } @@ -405,7 +399,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) { Labellist *gl = &ls->dyd->gt; int i = ls->fs->bl->firstgoto; while (i < gl->n) { - if (luaS_eqstr(gl->arr[i].name, lb->name)) + if (eqstr(gl->arr[i].name, lb->name)) closegoto(ls, i, lb); else i++; @@ -414,7 +408,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) { /* -** "export" pending gotos to outer level, to check them against +** export pending gotos to outer level, to check them against ** outer labels; if the block being exited has upvalues, and ** the goto exits the scope of any variable (which can be the ** upvalue), close those variables being exited. @@ -450,7 +444,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { /* -** create a label named "break" to resolve break statements +** create a label named 'break' to resolve break statements */ static void breaklabel (LexState *ls) { TString *n = luaS_new(ls->L, "break"); @@ -465,7 +459,7 @@ static void breaklabel (LexState *ls) { static l_noret undefgoto (LexState *ls, Labeldesc *gt) { const char *msg = isreserved(gt->name) ? "<%s> at line %d not inside a loop" - : "no visible label " LUA_QS " for at line %d"; + : "no visible label '%s' for at line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); semerror(ls, msg); } @@ -502,12 +496,12 @@ static Proto *addprototype (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; /* prototype of current function */ - if (fs->np >= f->sp->sizep) { - int oldsize = f->sp->sizep; - luaM_growvector(L, f->p, fs->np, f->sp->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sp->sizep) f->p[oldsize++] = NULL; + if (fs->np >= f->sizep) { + int oldsize = f->sizep; + luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); + while (oldsize < f->sizep) f->p[oldsize++] = NULL; } - f->p[fs->np++] = clp = luaF_newproto(L, NULL); + f->p[fs->np++] = clp = luaF_newproto(L); luaC_objbarrier(L, f, clp); return clp; } @@ -527,8 +521,7 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - lua_State *L = ls->L; - SharedProto *f; + Proto *f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; ls->fs = fs; @@ -543,13 +536,9 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nactvar = 0; fs->firstlocal = ls->dyd->actvar.n; fs->bl = NULL; - f = fs->f->sp; + f = fs->f; f->source = ls->source; f->maxstacksize = 2; /* registers 0/1 are always valid */ - fs->h = luaH_new(L); - /* anchor table of constants (to avoid being collected) */ - sethvalue2s(L, L->top, fs->h); - incr_top(L); enterblock(fs, bl, 0); } @@ -558,26 +547,22 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; - SharedProto *sp = f->sp; luaK_ret(fs, 0, 0); /* final return */ leaveblock(fs); - luaM_reallocvector(L, sp->code, sp->sizecode, fs->pc, Instruction); - sp->sizecode = fs->pc; - luaM_reallocvector(L, sp->lineinfo, sp->sizelineinfo, fs->pc, int); - sp->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->k, sp->sizek, fs->nk, TValue); - sp->sizek = fs->nk; - luaM_reallocvector(L, f->p, sp->sizep, fs->np, Proto *); - sp->sizep = fs->np; - luaM_reallocvector(L, sp->locvars, sp->sizelocvars, fs->nlocvars, LocVar); - sp->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, sp->upvalues, sp->sizeupvalues, fs->nups, Upvaldesc); - sp->sizeupvalues = fs->nups; + luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); + f->sizecode = fs->pc; + luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); + f->sizelineinfo = fs->pc; + luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); + f->sizek = fs->nk; + luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); + f->sizep = fs->np; + luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); + f->sizelocvars = fs->nlocvars; + luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); + f->sizeupvalues = fs->nups; lua_assert(fs->bl == NULL); ls->fs = fs->prev; - /* last token read was anchored in defunct function; must re-anchor it */ - anchor_token(ls); - L->top--; /* pop table of constants */ luaC_checkGC(L); } @@ -591,7 +576,7 @@ static void close_func (LexState *ls) { /* ** check whether current token is in the follow set of a block. ** 'until' closes syntactical blocks, but do not close scope, -** so it handled in separate. +** so it is handled in separate. */ static int block_follow (LexState *ls, int withuntil) { switch (ls->t.token) { @@ -605,7 +590,7 @@ static int block_follow (LexState *ls, int withuntil) { static void statlist (LexState *ls) { - /* statlist -> { stat [`;'] } */ + /* statlist -> { stat [';'] } */ while (!block_follow(ls, 1)) { if (ls->t.token == TK_RETURN) { statement(ls); @@ -646,14 +631,14 @@ static void yindex (LexState *ls, expdesc *v) { struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ - int nh; /* total number of `record' elements */ + int nh; /* total number of 'record' elements */ int na; /* total number of array elements */ int tostore; /* number of array elements pending to be stored */ }; static void recfield (LexState *ls, struct ConsControl *cc) { - /* recfield -> (NAME | `['exp1`]') = exp1 */ + /* recfield -> (NAME | '['exp1']') = exp1 */ FuncState *fs = ls->fs; int reg = ls->fs->freereg; expdesc key, val; @@ -751,8 +736,8 @@ static void constructor (LexState *ls, expdesc *t) { } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, '}', '{', line); lastlistfield(fs, &cc); - SETARG_B(fs->f->sp->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->sp->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ + SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ + SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ } /* }====================================================================== */ @@ -760,12 +745,12 @@ static void constructor (LexState *ls, expdesc *t) { static void parlist (LexState *ls) { - /* parlist -> [ param { `,' param } ] */ + /* parlist -> [ param { ',' param } ] */ FuncState *fs = ls->fs; - SharedProto *f = fs->f->sp; + Proto *f = fs->f; int nparams = 0; f->is_vararg = 0; - if (ls->t.token != ')') { /* is `parlist' not empty? */ + if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { case TK_NAME: { /* param -> NAME */ @@ -773,12 +758,12 @@ static void parlist (LexState *ls) { nparams++; break; } - case TK_DOTS: { /* param -> `...' */ + case TK_DOTS: { /* param -> '...' */ luaX_next(ls); f->is_vararg = 1; break; } - default: luaX_syntaxerror(ls, " or " LUA_QL("...") " expected"); + default: luaX_syntaxerror(ls, " or '...' expected"); } } while (!f->is_vararg && testnext(ls, ',')); } @@ -789,11 +774,11 @@ static void parlist (LexState *ls) { static void body (LexState *ls, expdesc *e, int ismethod, int line) { - /* body -> `(' parlist `)' block END */ + /* body -> '(' parlist ')' block END */ FuncState new_fs; BlockCnt bl; new_fs.f = addprototype(ls); - new_fs.f->sp->linedefined = line; + new_fs.f->linedefined = line; open_func(ls, &new_fs, &bl); checknext(ls, '('); if (ismethod) { @@ -803,7 +788,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { parlist(ls); checknext(ls, ')'); statlist(ls); - new_fs.f->sp->lastlinedefined = ls->linenumber; + new_fs.f->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); codeclosure(ls, e); close_func(ls); @@ -811,7 +796,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { static int explist (LexState *ls, expdesc *v) { - /* explist -> expr { `,' expr } */ + /* explist -> expr { ',' expr } */ int n = 1; /* at least one expression */ expr(ls, v); while (testnext(ls, ',')) { @@ -828,7 +813,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) { expdesc args; int base, nparams; switch (ls->t.token) { - case '(': { /* funcargs -> `(' [ explist ] `)' */ + case '(': { /* funcargs -> '(' [ explist ] ')' */ luaX_next(ls); if (ls->t.token == ')') /* arg list is empty? */ args.k = VVOID; @@ -845,7 +830,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) { } case TK_STRING: { /* funcargs -> STRING */ codestring(ls, &args, ls->t.seminfo.ts); - luaX_next(ls); /* must use `seminfo' before `next' */ + luaX_next(ls); /* must use 'seminfo' before 'next' */ break; } default: { @@ -911,14 +896,14 @@ static void suffixedexp (LexState *ls, expdesc *v) { fieldsel(ls, v); break; } - case '[': { /* `[' exp1 `]' */ + case '[': { /* '[' exp1 ']' */ expdesc key; luaK_exp2anyregup(fs, v); yindex(ls, &key); luaK_indexed(fs, v, &key); break; } - case ':': { /* `:' NAME funcargs */ + case ':': { /* ':' NAME funcargs */ expdesc key; luaX_next(ls); checkname(ls, &key); @@ -938,14 +923,19 @@ static void suffixedexp (LexState *ls, expdesc *v) { static void simpleexp (LexState *ls, expdesc *v) { - /* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | + /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | constructor | FUNCTION body | suffixedexp */ switch (ls->t.token) { - case TK_NUMBER: { - init_exp(v, VKNUM, 0); + case TK_FLT: { + init_exp(v, VKFLT, 0); v->u.nval = ls->t.seminfo.r; break; } + case TK_INT: { + init_exp(v, VKINT, 0); + v->u.ival = ls->t.seminfo.i; + break; + } case TK_STRING: { codestring(ls, v, ls->t.seminfo.ts); break; @@ -964,8 +954,8 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - check_condition(ls, fs->f->sp->is_vararg, - "cannot use " LUA_QL("...") " outside a vararg function"); + check_condition(ls, fs->f->is_vararg, + "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); break; } @@ -991,6 +981,7 @@ static UnOpr getunopr (int op) { switch (op) { case TK_NOT: return OPR_NOT; case '-': return OPR_MINUS; + case '~': return OPR_BNOT; case '#': return OPR_LEN; default: return OPR_NOUNOPR; } @@ -1002,9 +993,15 @@ static BinOpr getbinopr (int op) { case '+': return OPR_ADD; case '-': return OPR_SUB; case '*': return OPR_MUL; - case '/': return OPR_DIV; case '%': return OPR_MOD; case '^': return OPR_POW; + case '/': return OPR_DIV; + case TK_IDIV: return OPR_IDIV; + case '&': return OPR_BAND; + case '|': return OPR_BOR; + case '~': return OPR_BXOR; + case TK_SHL: return OPR_SHL; + case TK_SHR: return OPR_SHR; case TK_CONCAT: return OPR_CONCAT; case TK_NE: return OPR_NE; case TK_EQ: return OPR_EQ; @@ -1023,19 +1020,24 @@ static const struct { lu_byte left; /* left priority for each binary operator */ lu_byte right; /* right priority */ } priority[] = { /* ORDER OPR */ - {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `*' `/' `%' */ - {10, 9}, {5, 4}, /* ^, .. (right associative) */ - {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ - {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ - {2, 2}, {1, 1} /* and, or */ + {10, 10}, {10, 10}, /* '+' '-' */ + {11, 11}, {11, 11}, /* '*' '%' */ + {14, 13}, /* '^' (right associative) */ + {11, 11}, {11, 11}, /* '/' '//' */ + {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */ + {7, 7}, {7, 7}, /* '<<' '>>' */ + {9, 8}, /* '..' (right associative) */ + {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ + {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ + {2, 2}, {1, 1} /* and, or */ }; -#define UNARY_PRIORITY 8 /* priority for unary operators */ +#define UNARY_PRIORITY 12 /* priority for unary operators */ /* ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } -** where `binop' is any binary operator with a priority higher than `limit' +** where 'binop' is any binary operator with a priority higher than 'limit' */ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { BinOpr op; @@ -1049,7 +1051,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { luaK_prefix(ls->fs, uop, v, line); } else simpleexp(ls, v); - /* expand while operators have priorities higher than `limit' */ + /* expand while operators have priorities higher than 'limit' */ op = getbinopr(ls->t.token); while (op != OPR_NOBINOPR && priority[op].left > limit) { expdesc v2; @@ -1149,7 +1151,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { "C levels"); assignment(ls, &nv, nvars+1); } - else { /* assignment -> `=' explist */ + else { /* assignment -> '=' explist */ int nexps; checknext(ls, '='); nexps = explist(ls, &e); @@ -1173,7 +1175,7 @@ static int cond (LexState *ls) { /* cond -> exp */ expdesc v; expr(ls, &v); /* read condition */ - if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ + if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ luaK_goiftrue(ls->fs, &v); return v.f; } @@ -1198,9 +1200,9 @@ static void gotostat (LexState *ls, int pc) { static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { int i; for (i = fs->bl->firstlabel; i < ll->n; i++) { - if (luaS_eqstr(label, ll->arr[i].name)) { + if (eqstr(label, ll->arr[i].name)) { const char *msg = luaO_pushfstring(fs->ls->L, - "label " LUA_QS " already defined on line %d", + "label '%s' already defined on line %d", getstr(label), ll->arr[i].line); semerror(fs->ls, msg); } @@ -1324,7 +1326,7 @@ static void fornum (LexState *ls, TString *varname, int line) { if (testnext(ls, ',')) exp1(ls); /* optional step */ else { /* default step = 1 */ - luaK_codek(fs, fs->freereg, luaK_numberK(fs, 1)); + luaK_codek(fs, fs->freereg, luaK_intK(fs, 1)); luaK_reserveregs(fs, 1); } forbody(ls, base, line, 1, 1); @@ -1362,15 +1364,15 @@ static void forstat (LexState *ls, int line) { TString *varname; BlockCnt bl; enterblock(fs, &bl, 1); /* scope for loop and control variables */ - luaX_next(ls); /* skip `for' */ + luaX_next(ls); /* skip 'for' */ varname = str_checkname(ls); /* first variable name */ switch (ls->t.token) { case '=': fornum(ls, varname, line); break; case ',': case TK_IN: forlist(ls, varname); break; - default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); + default: luaX_syntaxerror(ls, "'=' or 'in' expected"); } check_match(ls, TK_END, TK_FOR, line); - leaveblock(fs); /* loop scope (`break' jumps to this point) */ + leaveblock(fs); /* loop scope ('break' jumps to this point) */ } @@ -1400,7 +1402,7 @@ static void test_then_block (LexState *ls, int *escapelist) { enterblock(fs, &bl, 0); jf = v.f; } - statlist(ls); /* `then' part */ + statlist(ls); /* 'then' part */ leaveblock(fs); if (ls->t.token == TK_ELSE || ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ @@ -1417,7 +1419,7 @@ static void ifstat (LexState *ls, int line) { while (ls->t.token == TK_ELSEIF) test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ if (testnext(ls, TK_ELSE)) - block(ls); /* `else' part */ + block(ls); /* 'else' part */ check_match(ls, TK_END, TK_IF, line); luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ } @@ -1435,7 +1437,7 @@ static void localfunc (LexState *ls) { static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {`,' NAME} [`=' explist] */ + /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ int nvars = 0; int nexps; expdesc e; @@ -1455,7 +1457,7 @@ static void localstat (LexState *ls) { static int funcname (LexState *ls, expdesc *v) { - /* funcname -> NAME {fieldsel} [`:' NAME] */ + /* funcname -> NAME {fieldsel} [':' NAME] */ int ismethod = 0; singlevar(ls, v); while (ls->t.token == '.') @@ -1476,7 +1478,7 @@ static void funcstat (LexState *ls, int line) { ismethod = funcname(ls, &v); body(ls, &b, ismethod, line); luaK_storevar(ls->fs, &v, &b); - luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ + luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ } @@ -1518,8 +1520,8 @@ static void retstat (LexState *ls) { if (nret == 1) /* only one single value? */ first = luaK_exp2anyreg(fs, &e); else { - luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ - first = fs->nactvar; /* return all `active' values */ + luaK_exp2nextreg(fs, &e); /* values must go to the stack */ + first = fs->nactvar; /* return all active values */ lua_assert(nret == fs->freereg - first); } } @@ -1608,7 +1610,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->sp->is_vararg = 1; /* main function is always vararg */ + fs->f->is_vararg = 1; /* main function is always vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ @@ -1618,24 +1620,28 @@ static void mainfunc (LexState *ls, FuncState *fs) { } -Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar) { +LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, + Dyndata *dyd, const char *name, int firstchar) { LexState lexstate; FuncState funcstate; - Closure *cl = luaF_newLclosure(L, 1); /* create main closure */ - /* anchor closure (to avoid being collected) */ - setclLvalue(L, L->top, cl); + LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ + setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ incr_top(L); - funcstate.f = cl->l.p = luaF_newproto(L, NULL); - funcstate.f->sp->source = luaS_new(L, name); /* create and anchor TString */ + lexstate.h = luaH_new(L); /* create table for scanner */ + sethvalue(L, L->top, lexstate.h); /* anchor it */ + incr_top(L); + funcstate.f = cl->p = luaF_newproto(L); + funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ + lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; - luaX_setinput(L, &lexstate, z, funcstate.f->sp->source, firstchar); + luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); mainfunc(&lexstate, &funcstate); lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); - return cl; /* it's on the stack too */ + L->top--; /* remove scanner's table */ + return cl; /* closure is on the stack, too */ } diff --git a/3rd/lua/lparser.h b/3rd/lua/lparser.h index 0346e3c4..62c50cac 100644 --- a/3rd/lua/lparser.h +++ b/3rd/lua/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lparser.h,v 1.74 2014/10/25 11:50:46 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -21,8 +21,9 @@ typedef enum { VNIL, VTRUE, VFALSE, - VK, /* info = index of constant in `k' */ - VKNUM, /* nval = numerical value */ + VK, /* info = index of constant in 'k' */ + VKFLT, /* nval = numerical float value */ + VKINT, /* nval = numerical integer value */ VNONRELOC, /* info = result register */ VLOCAL, /* info = local register */ VUPVAL, /* info = index of upvalue in 'upvalues' */ @@ -46,10 +47,11 @@ typedef struct expdesc { lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ } ind; int info; /* for generic use */ - lua_Number nval; /* for VKNUM */ + lua_Number nval; /* for VKFLT */ + lua_Integer ival; /* for VKINT */ } u; - int t; /* patch list of `exit when true' */ - int f; /* patch list of `exit when false' */ + int t; /* patch list of 'exit when true' */ + int f; /* patch list of 'exit when false' */ } expdesc; @@ -95,15 +97,14 @@ struct BlockCnt; /* defined in lparser.c */ /* state needed to generate code for a given function */ typedef struct FuncState { Proto *f; /* current function header */ - Table *h; /* table to find (and reuse) elements in `k' */ struct FuncState *prev; /* enclosing function */ struct LexState *ls; /* lexical state */ struct BlockCnt *bl; /* chain of current blocks */ - int pc; /* next position to code (equivalent to `ncode') */ + int pc; /* next position to code (equivalent to 'ncode') */ int lasttarget; /* 'label' of last 'jump label' */ - int jpc; /* list of pending jumps to `pc' */ - int nk; /* number of elements in `k' */ - int np; /* number of elements in `p' */ + int jpc; /* list of pending jumps to 'pc' */ + int nk; /* number of elements in 'k' */ + int np; /* number of elements in 'p' */ int firstlocal; /* index of first local var (in Dyndata array) */ short nlocvars; /* number of elements in 'f->locvars' */ lu_byte nactvar; /* number of active local variables */ @@ -112,8 +113,8 @@ typedef struct FuncState { } FuncState; -LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar); +LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, + Dyndata *dyd, const char *name, int firstchar); #endif diff --git a/3rd/lua/lprefix.h b/3rd/lua/lprefix.h new file mode 100644 index 00000000..02daa837 --- /dev/null +++ b/3rd/lua/lprefix.h @@ -0,0 +1,45 @@ +/* +** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ +** Definitions for Lua code that must come before any other header file +** See Copyright Notice in lua.h +*/ + +#ifndef lprefix_h +#define lprefix_h + + +/* +** Allows POSIX/XSI stuff +*/ +#if !defined(LUA_USE_C89) /* { */ + +#if !defined(_XOPEN_SOURCE) +#define _XOPEN_SOURCE 600 +#elif _XOPEN_SOURCE == 0 +#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ +#endif + +/* +** Allows manipulation of large files in gcc and some other compilers +*/ +#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) +#define _LARGEFILE_SOURCE 1 +#define _FILE_OFFSET_BITS 64 +#endif + +#endif /* } */ + + +/* +** Windows stuff +*/ +#if defined(_WIN32) /* { */ + +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ +#endif + +#endif /* } */ + +#endif + diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index c7f2672b..ff6b02d3 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -1,16 +1,18 @@ /* -** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $ +** $Id: lstate.c,v 2.127 2014/11/02 19:33:33 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ +#define lstate_c +#define LUA_CORE + +#include "lprefix.h" + #include #include -#define lstate_c -#define LUA_CORE - #include "lua.h" #include "lapi.h" @@ -30,10 +32,6 @@ #define LUAI_GCPAUSE 200 /* 200% */ #endif -#if !defined(LUAI_GCMAJOR) -#define LUAI_GCMAJOR 200 /* 200% */ -#endif - #if !defined(LUAI_GCMUL) #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ #endif @@ -57,9 +55,7 @@ ** thread state + extra space */ typedef struct LX { -#if defined(LUAI_EXTRASPACE) - char buff[LUAI_EXTRASPACE]; -#endif + lu_byte extra_[LUA_EXTRASPACE]; lua_State l; } LX; @@ -78,9 +74,8 @@ typedef struct LG { /* -** Compute an initial seed as random as possible. In ANSI, rely on -** Address Space Layout Randomization (if present) to increase -** randomness.. +** Compute an initial seed as random as possible. Rely on Address Space +** Layout Randomization (if present) to increase randomness.. */ #define addbuff(b,p,e) \ { size_t t = cast(size_t, e); \ @@ -119,6 +114,9 @@ CallInfo *luaE_extendCI (lua_State *L) { } +/* +** free all CallInfo structures not in use by a thread +*/ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; @@ -130,6 +128,22 @@ void luaE_freeCI (lua_State *L) { } +/* +** free half of the CallInfo structures not in use by a thread +*/ +void luaE_shrinkCI (lua_State *L) { + CallInfo *ci = L->ci; + while (ci->next != NULL) { /* while there is 'next' */ + CallInfo *next2 = ci->next->next; /* next's next */ + if (next2 == NULL) break; + luaM_free(L, ci->next); /* remove next */ + ci->next = next2; /* remove 'next' from the list */ + next2->previous = ci; + ci = next2; + } +} + + static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ @@ -163,22 +177,23 @@ static void freestack (lua_State *L) { ** Create registry table and its predefined values */ static void init_registry (lua_State *L, global_State *g) { - TValue mt; + TValue temp; /* create registry */ Table *registry = luaH_new(L); sethvalue(L, &g->l_registry, registry); luaH_resize(L, registry, LUA_RIDX_LAST, 0); /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, &mt, L); - luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); + setthvalue(L, &temp, L); /* temp = L */ + luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp); /* registry[LUA_RIDX_GLOBALS] = table of globals */ - sethvalue(L, &mt, luaH_new(L)); - luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt); + sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */ + luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp); } /* -** open parts of the state that may cause memory-allocation errors +** open parts of the state that may cause memory-allocation errors. +** ('g->version' != NULL flags that the state was completely build) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); @@ -190,7 +205,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaX_init(L); /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaS_fix(g->memerrmsg); /* it should never be collected */ + luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); @@ -198,14 +213,15 @@ static void f_luaopen (lua_State *L, void *ud) { /* -** preinitialize a state with consistent values without allocating +** preinitialize a thread with consistent values without allocating ** any memory (to avoid errors) */ -static void preinit_state (lua_State *L, global_State *g) { +static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; L->stack = NULL; L->ci = NULL; L->stacksize = 0; + L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; L->nCcalls = 0; L->hook = NULL; @@ -235,17 +251,28 @@ static void close_state (lua_State *L) { LUA_API lua_State *lua_newthread (lua_State *L) { + global_State *g = G(L); lua_State *L1; lua_lock(L); luaC_checkGC(L); - L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; + /* create new thread */ + L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; + L1->marked = luaC_white(g); + L1->tt = LUA_TTHREAD; + /* link it on list 'allgc' */ + L1->next = g->allgc; + g->allgc = obj2gco(L1); + /* anchor it on L stack */ setthvalue(L, L->top, L1); api_incr_top(L); - preinit_state(L1, G(L)); + preinit_thread(L1, g); L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; resethookcount(L1); + /* initialize L1 extra space */ + memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), + LUA_EXTRASPACE); luai_userstatethread(L, L1); stack_init(L1, L); /* init stack */ lua_unlock(L); @@ -273,36 +300,32 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g = &l->g; L->next = NULL; L->tt = LUA_TTHREAD; - g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); + g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); - g->gckind = KGC_NORMAL; - preinit_state(L, g); + preinit_thread(L, g); g->frealloc = f; g->ud = ud; g->mainthread = L; g->seed = makeseed(L); - g->uvhead.u.l.prev = &g->uvhead; - g->uvhead.u.l.next = &g->uvhead; g->gcrunning = 0; /* no GC while building state */ g->GCestimate = 0; - g->strt.size = 0; - g->strt.nuse = 0; + g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); luaZ_initbuffer(L, &g->buff); g->panic = NULL; g->version = NULL; g->gcstate = GCSpause; - g->allgc = NULL; - g->finobj = NULL; - g->tobefnz = NULL; - g->sweepgc = g->sweepfin = NULL; + g->gckind = KGC_NORMAL; + g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; + g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; + g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; + g->gcfinnum = 0; g->gcpause = LUAI_GCPAUSE; - g->gcmajorinc = LUAI_GCMAJOR; g->gcstepmul = LUAI_GCMUL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index daffd9aa..81e12c40 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstate.h,v 2.119 2014/10/30 18:53:28 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -16,25 +16,16 @@ /* -** Some notes about garbage-collected objects: All objects in Lua must -** be kept somehow accessible until being freed. +** Some notes about garbage-collected objects: All objects in Lua must +** be kept somehow accessible until being freed, so all objects always +** belong to one (and only one) of these lists, using field 'next' of +** the 'CommonHeader' for the link: ** -** Lua keeps most objects linked in list g->allgc. The link uses field -** 'next' of the CommonHeader. -** -** Strings are kept in several lists headed by the array g->strt.hash. -** -** Open upvalues are not subject to independent garbage collection. They -** are collected together with their respective threads. Lua keeps a -** double-linked list with all open upvalues (g->uvhead) so that it can -** mark objects referred by them. (They are always gray, so they must -** be remarked in the atomic step. Usually their contents would be marked -** when traversing the respective threads, but the thread may already be -** dead, while the upvalue is still accessible through closures.) -** -** Objects with finalizers are kept in the list g->finobj. -** -** The list g->tobefnz links all objects being finalized. +** 'allgc': all objects not marked for finalization; +** 'finobj': all objects marked for finalization; +** 'tobefnz': all objects ready to be finalized; +** 'fixedgc': all objects that are not to be collected (currently +** only small strings, such as reserved words). */ @@ -53,65 +44,70 @@ struct lua_longjmp; /* defined in ldo.c */ /* kinds of Garbage Collection */ #define KGC_NORMAL 0 #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ -#define KGC_GEN 2 /* generational collection */ typedef struct stringtable { - GCObject **hash; - lu_int32 nuse; /* number of elements */ + TString **hash; + int nuse; /* number of elements */ int size; } stringtable; /* -** information about a call +** Information about a call. +** When a thread yields, 'func' is adjusted to pretend that the +** top function has only the yielded values in its stack; in that +** case, the actual 'func' value is saved in field 'extra'. +** When a function calls another with a continuation, 'extra' keeps +** the function index so that, in case of errors, the continuation +** function can be called with the correct top. */ typedef struct CallInfo { StkId func; /* function index in the stack */ StkId top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ - short nresults; /* expected number of results from this function */ - lu_byte callstatus; - ptrdiff_t extra; union { struct { /* only for Lua functions */ StkId base; /* base for this function */ const Instruction *savedpc; } l; struct { /* only for C functions */ - int ctx; /* context info. in case of yields */ - lua_CFunction k; /* continuation in case of yields */ + lua_KFunction k; /* continuation in case of yields */ ptrdiff_t old_errfunc; - lu_byte old_allowhook; - lu_byte status; + lua_KContext ctx; /* context info. in case of yields */ } c; } u; + ptrdiff_t extra; + short nresults; /* expected number of results from this function */ + lu_byte callstatus; } CallInfo; /* ** Bits in CallInfo status */ -#define CIST_LUA (1<<0) /* call is running a Lua function */ -#define CIST_HOOKED (1<<1) /* call is running a debug hook */ -#define CIST_REENTRY (1<<2) /* call is running on same invocation of +#define CIST_OAH (1<<0) /* original value of 'allowhook' */ +#define CIST_LUA (1<<1) /* call is running a Lua function */ +#define CIST_HOOKED (1<<2) /* call is running a debug hook */ +#define CIST_REENTRY (1<<3) /* call is running on same invocation of luaV_execute of previous call */ -#define CIST_YIELDED (1<<3) /* call reentered after suspension */ #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ -#define CIST_STAT (1<<5) /* call has an error status (pcall) */ -#define CIST_TAIL (1<<6) /* call was tail called */ -#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ - +#define CIST_TAIL (1<<5) /* call was tail called */ +#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ #define isLua(ci) ((ci)->callstatus & CIST_LUA) +/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ +#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) +#define getoah(st) ((st) & CIST_OAH) + /* -** `global state', shared by all threads of this state +** 'global state', shared by all threads of this state */ typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ - void *ud; /* auxiliary data to `frealloc' */ + void *ud; /* auxiliary data to 'frealloc' */ lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ lu_mem GCmemtrav; /* memory traversed by the GC */ @@ -123,22 +119,21 @@ typedef struct global_State { lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ lu_byte gcrunning; /* true if GC is running */ - int sweepstrgc; /* position of sweep in `strt' */ GCObject *allgc; /* list of all collectable objects */ + GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ - GCObject **sweepgc; /* current position of sweep in list 'allgc' */ - GCObject **sweepfin; /* current position of sweep in list 'finobj' */ GCObject *gray; /* list of gray objects */ GCObject *grayagain; /* list of objects to be traversed atomically */ GCObject *weak; /* list of tables with weak values */ GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ GCObject *allweak; /* list of all-weak tables */ GCObject *tobefnz; /* list of userdata to be GC */ - UpVal uvhead; /* head of double-linked list of all open upvalues */ + GCObject *fixedgc; /* list of objects not to be collected */ + struct lua_State *twups; /* list of threads with open upvalues */ Mbuffer buff; /* temporary buffer for string concatenation */ + unsigned int gcfinnum; /* number of finalizers to call in each GC step */ int gcpause; /* size of pause between successive GCs */ - int gcmajorinc; /* pause between major collections (only in gen. mode) */ - int gcstepmul; /* GC `granularity' */ + int gcstepmul; /* GC 'granularity' */ lua_CFunction panic; /* to be called in unprotected errors */ struct lua_State *mainthread; const lua_Number *version; /* pointer to version number */ @@ -149,7 +144,7 @@ typedef struct global_State { /* -** `per thread' state +** 'per thread' state */ struct lua_State { CommonHeader; @@ -160,19 +155,20 @@ struct lua_State { const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ + UpVal *openupval; /* list of open upvalues in this stack */ + GCObject *gclist; + struct lua_State *twups; /* list of threads with open upvalues */ + struct lua_longjmp *errorJmp; /* current error recover point */ + CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ + lua_Hook hook; + ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; + int basehookcount; + int hookcount; unsigned short nny; /* number of non-yieldable calls in stack */ unsigned short nCcalls; /* number of nested C calls */ lu_byte hookmask; lu_byte allowhook; - int basehookcount; - int hookcount; - lua_Hook hook; - GCObject *openupval; /* list of open upvalues in this stack */ - GCObject *gclist; - struct lua_longjmp *errorJmp; /* current error recover point */ - ptrdiff_t errfunc; /* current error handling function (stack index) */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ }; @@ -180,39 +176,37 @@ struct lua_State { /* -** Union of all collectable objects +** Union of all collectable objects (only for conversions) */ -union GCObject { - GCheader gch; /* common header */ - union TString ts; - union Udata u; +union GCUnion { + GCObject gc; /* common header */ + struct TString ts; + struct Udata u; union Closure cl; struct Table h; struct Proto p; - struct UpVal uv; struct lua_State th; /* thread */ }; -#define gch(o) (&(o)->gch) +#define cast_u(o) cast(union GCUnion *, (o)) /* macros to convert a GCObject into a specific value */ -#define rawgco2ts(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) -#define gco2ts(o) (&rawgco2ts(o)->tsv) -#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) -#define gco2u(o) (&rawgco2u(o)->uv) -#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) -#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) +#define gco2ts(o) \ + check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) +#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) +#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) +#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) #define gco2cl(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) -#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) -#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) -#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) -#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) + check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) +#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) +#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) +#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) -/* macro to convert any Lua object into a GCObject */ -#define obj2gco(v) (cast(GCObject *, (v))) + +/* macro to convert a Lua object into a GCObject */ +#define obj2gco(v) \ + check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) /* actual number of total bytes allocated */ @@ -222,6 +216,7 @@ LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); +LUAI_FUNC void luaE_shrinkCI (lua_State *L); #endif diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index af96c89c..2947113c 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -1,23 +1,28 @@ /* -** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ - -#include - #define lstring_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" +#include "ldebug.h" +#include "ldo.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" + /* ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to ** compute its hash @@ -31,23 +36,14 @@ ** equality for long strings */ int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->tsv.len; - lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); + size_t len = a->len; + lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); return (a == b) || /* same instance or... */ - ((len == b->tsv.len) && /* equal length and ... */ + ((len == b->len) && /* equal length and ... */ (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ } -/* -** equality for strings -*/ -int luaS_eqstr (TString *a, TString *b) { - return (a->tsv.tt == b->tsv.tt) && - (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); -} - - unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int h = seed ^ cast(unsigned int, l); size_t l1; @@ -64,66 +60,59 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { void luaS_resize (lua_State *L, int newsize) { int i; stringtable *tb = &G(L)->strt; - /* cannot resize while GC is traversing strings */ - luaC_runtilstate(L, ~bitmask(GCSsweepstring)); - if (newsize > tb->size) { - luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); - for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; + if (newsize > tb->size) { /* grow table if needed */ + luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); + for (i = tb->size; i < newsize; i++) + tb->hash[i] = NULL; } - /* rehash */ - for (i=0; isize; i++) { - GCObject *p = tb->hash[i]; + for (i = 0; i < tb->size; i++) { /* rehash */ + TString *p = tb->hash[i]; tb->hash[i] = NULL; while (p) { /* for each node in the list */ - GCObject *next = gch(p)->next; /* save next */ - unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ - gch(p)->next = tb->hash[h]; /* chain it */ + TString *hnext = p->hnext; /* save next */ + unsigned int h = lmod(p->hash, newsize); /* new position */ + p->hnext = tb->hash[h]; /* chain it */ tb->hash[h] = p; - resetoldbit(p); /* see MOVE OLD rule */ - p = next; + p = hnext; } } - if (newsize < tb->size) { - /* shrinking slice must be empty */ + if (newsize < tb->size) { /* shrink table if needed */ + /* vanishing slice should be empty */ lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); - luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); + luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); } tb->size = newsize; } + /* ** creates a new string object */ static TString *createstrobj (lua_State *L, const char *str, size_t l, - int tag, unsigned int h, GCObject **list) { + int tag, unsigned int h) { TString *ts; + GCObject *o; size_t totalsize; /* total size of TString object */ - totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); - ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; - ts->tsv.len = l; - ts->tsv.hash = h; - ts->tsv.extra = 0; - memcpy(ts+1, str, l*sizeof(char)); - ((char *)(ts+1))[l] = '\0'; /* ending 0 */ + totalsize = sizelstring(l); + o = luaC_newobj(L, tag, totalsize); + ts = gco2ts(o); + ts->len = l; + ts->hash = h; + ts->extra = 0; + memcpy(getaddrstr(ts), str, l * sizeof(char)); + getaddrstr(ts)[l] = '\0'; /* ending 0 */ return ts; } -/* -** creates a new short string, inserting it into string table -*/ -static TString *newshrstr (lua_State *L, const char *str, size_t l, - unsigned int h) { - GCObject **list; /* (pointer to) list where it will be inserted */ +void luaS_remove (lua_State *L, TString *ts) { stringtable *tb = &G(L)->strt; - TString *s; - if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) - luaS_resize(L, tb->size*2); /* too crowded */ - list = &tb->hash[lmod(h, tb->size)]; - s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); - tb->nuse++; - return s; + TString **p = &tb->hash[lmod(ts->hash, tb->size)]; + while (*p != ts) /* find previous element */ + p = &(*p)->hnext; + *p = (*p)->hnext; /* remove element from its list */ + tb->nuse--; } @@ -131,22 +120,28 @@ static TString *newshrstr (lua_State *L, const char *str, size_t l, ** checks whether short string exists and reuses it or creates a new one */ static TString *internshrstr (lua_State *L, const char *str, size_t l) { - GCObject *o; + TString *ts; global_State *g = G(L); unsigned int h = luaS_hash(str, l, g->seed); - for (o = g->strt.hash[lmod(h, g->strt.size)]; - o != NULL; - o = gch(o)->next) { - TString *ts = rawgco2ts(o); - if (h == ts->tsv.hash && - l == ts->tsv.len && + TString **list = &g->strt.hash[lmod(h, g->strt.size)]; + for (ts = *list; ts != NULL; ts = ts->hnext) { + if (l == ts->len && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { - if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ - changewhite(o); /* resurrect it */ + /* found! */ + if (isdead(g, ts)) /* dead (but not collected yet)? */ + changewhite(ts); /* resurrect it */ return ts; } } - return newshrstr(L, str, l, h); /* not found; create a new string */ + if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { + luaS_resize(L, g->strt.size * 2); + list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ + } + ts = createstrobj(L, str, l, LUA_TSHRSTR, h); + ts->hnext = *list; + *list = ts; + g->strt.nuse++; + return ts; } @@ -157,9 +152,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { if (l <= LUAI_MAXSHORTLEN) /* short string? */ return internshrstr(L, str, l); else { - if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) + if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) luaM_toobig(L); - return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); + return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); } } @@ -172,14 +167,16 @@ TString *luaS_new (lua_State *L, const char *str) { } -Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { +Udata *luaS_newudata (lua_State *L, size_t s) { Udata *u; - if (s > MAX_SIZET - sizeof(Udata)) + GCObject *o; + if (s > MAX_SIZE - sizeof(Udata)) luaM_toobig(L); - u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; - u->uv.len = s; - u->uv.metatable = NULL; - u->uv.env = e; + o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); + u = gco2u(o); + u->len = s; + u->metatable = NULL; + setuservalue(L, u, luaO_nilobject); return u; } diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index 260e7f16..d3f04caf 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstring.h,v 1.56 2014/07/18 14:46:47 roberto Exp $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -12,33 +12,33 @@ #include "lstate.h" -#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) +#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) +#define sizestring(s) sizelstring((s)->len) -#define sizeudata(u) (sizeof(union Udata)+(u)->len) +#define sizeludata(l) (sizeof(union UUdata) + (l)) +#define sizeudata(u) sizeludata((u)->len) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) -#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) - /* ** test whether a string is a reserved word */ -#define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) +#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) /* ** equality for short strings, which are always internalized */ -#define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) +#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); -LUAI_FUNC int luaS_eqstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); +LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); +LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 9261fd22..a650b768 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,19 +1,22 @@ /* -** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lstrlib.c,v 1.221 2014/12/11 14:03:07 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ +#define lstrlib_c +#define LUA_LIB + +#include "lprefix.h" + #include +#include #include #include #include #include -#define lstrlib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" @@ -29,10 +32,19 @@ #endif -/* macro to `unsign' a character */ +/* macro to 'unsign' a character */ #define uchar(c) ((unsigned char)(c)) +/* +** Some sizes are better limited to fit in 'int', but must also fit in +** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) +*/ +#define MAXSIZE \ + (sizeof(size_t) < sizeof(int) ? (~(size_t)0) : (size_t)(INT_MAX)) + + + static int str_len (lua_State *L) { size_t l; @@ -43,22 +55,22 @@ static int str_len (lua_State *L) { /* translate a relative string position: negative means back from end */ -static size_t posrelat (ptrdiff_t pos, size_t len) { - if (pos >= 0) return (size_t)pos; +static lua_Integer posrelat (lua_Integer pos, size_t len) { + if (pos >= 0) return pos; else if (0u - (size_t)pos > len) return 0; - else return len - ((size_t)-pos) + 1; + else return (lua_Integer)len + pos + 1; } static int str_sub (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); - size_t start = posrelat(luaL_checkinteger(L, 2), l); - size_t end = posrelat(luaL_optinteger(L, 3, -1), l); + lua_Integer start = posrelat(luaL_checkinteger(L, 2), l); + lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l); if (start < 1) start = 1; - if (end > l) end = l; + if (end > (lua_Integer)l) end = l; if (start <= end) - lua_pushlstring(L, s + start - 1, end - start + 1); + lua_pushlstring(L, s + start - 1, (size_t)(end - start + 1)); else lua_pushliteral(L, ""); return 1; } @@ -102,25 +114,23 @@ static int str_upper (lua_State *L) { } -/* reasonable limit to avoid arithmetic overflow */ -#define MAXSIZE ((~(size_t)0) >> 1) - static int str_rep (lua_State *L) { size_t l, lsep; const char *s = luaL_checklstring(L, 1, &l); - int n = luaL_checkint(L, 2); + lua_Integer n = luaL_checkinteger(L, 2); const char *sep = luaL_optlstring(L, 3, "", &lsep); if (n <= 0) lua_pushliteral(L, ""); - else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ + else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */ return luaL_error(L, "resulting string too large"); else { - size_t totallen = n * l + (n - 1) * lsep; + size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ memcpy(p, s, l * sizeof(char)); p += l; - if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */ - memcpy(p, sep, lsep * sizeof(char)); p += lsep; + if (lsep > 0) { /* empty 'memcpy' is not that cheap */ + memcpy(p, sep, lsep * sizeof(char)); + p += lsep; } } memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ @@ -133,14 +143,14 @@ static int str_rep (lua_State *L) { static int str_byte (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); - size_t posi = posrelat(luaL_optinteger(L, 2, 1), l); - size_t pose = posrelat(luaL_optinteger(L, 3, posi), l); + lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l); + lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l); int n, i; if (posi < 1) posi = 1; - if (pose > l) pose = l; + if (pose > (lua_Integer)l) pose = l; if (posi > pose) return 0; /* empty interval; return no values */ n = (int)(pose - posi + 1); - if (posi + n <= pose) /* (size_t -> int) overflow? */ + if (posi + n <= pose) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); luaL_checkstack(L, n, "string slice too long"); for (i=0; ip_end) - luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); + luaL_error(ms->L, "malformed pattern (ends with '%%')"); return p+1; } case '[': { if (*p == '^') p++; - do { /* look for a `]' */ + do { /* look for a ']' */ if (p == ms->p_end) - luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); + luaL_error(ms->L, "malformed pattern (missing ']')"); if (*(p++) == L_ESC && p < ms->p_end) - p++; /* skip escapes (e.g. `%]') */ + p++; /* skip escapes (e.g. '%]') */ } while (*p != ']'); return p+1; } @@ -287,7 +298,7 @@ static int matchbracketclass (int c, const char *p, const char *ec) { int sig = 1; if (*(p+1) == '^') { sig = 0; - p++; /* skip the `^' */ + p++; /* skip the '^' */ } while (++p < ec) { if (*p == L_ESC) { @@ -325,8 +336,7 @@ static int singlematch (MatchState *ms, const char *s, const char *p, static const char *matchbalance (MatchState *ms, const char *s, const char *p) { if (p >= ms->p_end - 1) - luaL_error(ms->L, "malformed pattern " - "(missing arguments to " LUA_QL("%%b") ")"); + luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); if (*s != *p) return NULL; else { int b = *p; @@ -425,7 +435,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { break; } case '$': { - if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */ + if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ goto dflt; /* no; go to default */ s = (s == ms->src_end) ? s : NULL; /* check end of string */ break; @@ -443,8 +453,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { const char *ep; char previous; p += 2; if (*p != '[') - luaL_error(ms->L, "missing " LUA_QL("[") " after " - LUA_QL("%%f") " in pattern"); + luaL_error(ms->L, "missing '[' after '%%f' in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms->src_init) ? '\0' : *(s - 1); if (!matchbracketclass(uchar(previous), p, ep - 1) && @@ -514,16 +523,16 @@ static const char *match (MatchState *ms, const char *s, const char *p) { static const char *lmemfind (const char *s1, size_t l1, const char *s2, size_t l2) { if (l2 == 0) return s1; /* empty strings are everywhere */ - else if (l2 > l1) return NULL; /* avoids a negative `l1' */ + else if (l2 > l1) return NULL; /* avoids a negative 'l1' */ else { - const char *init; /* to search for a `*s2' inside `s1' */ - l2--; /* 1st char will be checked by `memchr' */ - l1 = l1-l2; /* `s2' cannot be found after that */ + const char *init; /* to search for a '*s2' inside 's1' */ + l2--; /* 1st char will be checked by 'memchr' */ + l1 = l1-l2; /* 's2' cannot be found after that */ while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { init++; /* 1st char is already checked */ if (memcmp(init, s2+1, l2) == 0) return init-1; - else { /* correct `l1' and `s1' to try again */ + else { /* correct 'l1' and 's1' to try again */ l1 -= init-s1; s1 = init; } @@ -539,7 +548,7 @@ static void push_onecapture (MatchState *ms, int i, const char *s, if (i == 0) /* ms->level == 0, too */ lua_pushlstring(ms->L, s, e - s); /* add whole match */ else - luaL_error(ms->L, "invalid capture index"); + luaL_error(ms->L, "invalid capture index %%%d", i + 1); } else { ptrdiff_t l = ms->capture[i].len; @@ -578,16 +587,16 @@ static int str_find_aux (lua_State *L, int find) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); - size_t init = posrelat(luaL_optinteger(L, 3, 1), ls); + lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); if (init < 1) init = 1; - else if (init > ls + 1) { /* start after string's end? */ + else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ lua_pushnil(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ - const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); + const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); if (s2) { lua_pushinteger(L, s2 - s + 1); lua_pushinteger(L, s2 - s + lp); @@ -678,7 +687,8 @@ static int gmatch (lua_State *L) { static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { size_t l, i; - const char *news = lua_tolstring(ms->L, 3, &l); + lua_State *L = ms->L; + const char *news = lua_tolstring(L, 3, &l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) luaL_addchar(b, news[i]); @@ -686,14 +696,15 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, i++; /* skip ESC */ if (!isdigit(uchar(news[i]))) { if (news[i] != L_ESC) - luaL_error(ms->L, "invalid use of " LUA_QL("%c") - " in replacement string", L_ESC); + luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); luaL_addchar(b, news[i]); } else if (news[i] == '0') luaL_addlstring(b, s, e - s); else { push_onecapture(ms, news[i] - '1', s, e); + luaL_tolstring(L, -1, NULL); /* if number, convert it to string */ + lua_remove(L, -2); /* remove original value */ luaL_addvalue(b); /* add capture to accumulated result */ } } @@ -737,9 +748,9 @@ static int str_gsub (lua_State *L) { const char *src = luaL_checklstring(L, 1, &srcl); const char *p = luaL_checklstring(L, 2, &lp); int tr = lua_type(L, 3); - size_t max_s = luaL_optinteger(L, 4, srcl+1); + lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); int anchor = (*p == '^'); - size_t n = 0; + lua_Integer n = 0; MatchState ms; luaL_Buffer b; luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || @@ -786,48 +797,17 @@ static int str_gsub (lua_State *L) { ** ======================================================= */ -/* -** LUA_INTFRMLEN is the length modifier for integer conversions in -** 'string.format'; LUA_INTFRM_T is the integer type corresponding to -** the previous length -*/ -#if !defined(LUA_INTFRMLEN) /* { */ -#if defined(LUA_USE_LONGLONG) - -#define LUA_INTFRMLEN "ll" -#define LUA_INTFRM_T long long - -#else - -#define LUA_INTFRMLEN "l" -#define LUA_INTFRM_T long - -#endif -#endif /* } */ - - -/* -** LUA_FLTFRMLEN is the length modifier for float conversions in -** 'string.format'; LUA_FLTFRM_T is the float type corresponding to -** the previous length -*/ -#if !defined(LUA_FLTFRMLEN) - -#define LUA_FLTFRMLEN "" -#define LUA_FLTFRM_T double - -#endif - - /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ #define MAX_ITEM 512 + /* valid flags in a format specification */ #define FLAGS "-+ #0" + /* -** maximum size of each format specification (such as '%-099.99d') -** (+10 accounts for %99.99x plus margin of error) +** maximum size of each format specification (such as "%-099.99d") +** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error) */ -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) +#define MAX_FORMAT (sizeof(FLAGS) + 2 + 10) static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { @@ -903,7 +883,7 @@ static int str_format (lua_State *L) { else if (*++strfrmt == L_ESC) luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ - char form[MAX_FORMAT]; /* to store the format (`%...') */ + char form[MAX_FORMAT]; /* to store the format ('%...') */ char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) @@ -911,36 +891,23 @@ static int str_format (lua_State *L) { strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { - nb = sprintf(buff, form, luaL_checkint(L, arg)); - break; - } - case 'd': case 'i': { - lua_Number n = luaL_checknumber(L, arg); - LUA_INTFRM_T ni = (LUA_INTFRM_T)n; - lua_Number diff = n - (lua_Number)ni; - luaL_argcheck(L, -1 < diff && diff < 1, arg, - "not a number in proper range"); - addlenmod(form, LUA_INTFRMLEN); - nb = sprintf(buff, form, ni); + nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg)); break; } + case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { - lua_Number n = luaL_checknumber(L, arg); - unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n; - lua_Number diff = n - (lua_Number)ni; - luaL_argcheck(L, -1 < diff && diff < 1, arg, - "not a non-negative number in proper range"); - addlenmod(form, LUA_INTFRMLEN); - nb = sprintf(buff, form, ni); + lua_Integer n = luaL_checkinteger(L, arg); + addlenmod(form, LUA_INTEGER_FRMLEN); + nb = sprintf(buff, form, n); break; } - case 'e': case 'E': case 'f': #if defined(LUA_USE_AFORMAT) case 'a': case 'A': #endif + case 'e': case 'E': case 'f': case 'g': case 'G': { - addlenmod(form, LUA_FLTFRMLEN); - nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); + addlenmod(form, LUA_NUMBER_FRMLEN); + nb = sprintf(buff, form, luaL_checknumber(L, arg)); break; } case 'q': { @@ -962,9 +929,9 @@ static int str_format (lua_State *L) { break; } } - default: { /* also treat cases `pnLlh' */ - return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " - LUA_QL("format"), *(strfrmt - 1)); + default: { /* also treat cases 'pnLlh' */ + return luaL_error(L, "invalid option '%%%c' to 'format'", + *(strfrmt - 1)); } } luaL_addsize(&b, nb); @@ -977,6 +944,447 @@ static int str_format (lua_State *L) { /* }====================================================== */ +/* +** {====================================================== +** PACK/UNPACK +** ======================================================= +*/ + + +/* value used for padding */ +#if !defined(LUA_PACKPADBYTE) +#define LUA_PACKPADBYTE 0x00 +#endif + +/* maximum size for the binary representation of an integer */ +#define MAXINTSIZE 16 + +/* number of bits in a character */ +#define NB CHAR_BIT + +/* mask for one character (NB 1's) */ +#define MC ((1 << NB) - 1) + +/* size of a lua_Integer */ +#define SZINT ((int)sizeof(lua_Integer)) + + +/* dummy union to get native endianness */ +static const union { + int dummy; + char little; /* true iff machine is little endian */ +} nativeendian = {1}; + + +/* dummy structure to get native alignment requirements */ +struct cD { + char c; + union { double d; void *p; lua_Integer i; lua_Number n; } u; +}; + +#define MAXALIGN (offsetof(struct cD, u)) + + +/* +** Union for serializing floats +*/ +typedef union Ftypes { + float f; + double d; + lua_Number n; + char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ +} Ftypes; + + +/* +** information to pack/unpack stuff +*/ +typedef struct Header { + lua_State *L; + int islittle; + int maxalign; +} Header; + + +/* +** options for pack/unpack +*/ +typedef enum KOption { + Kint, /* signed integers */ + Kuint, /* unsigned integers */ + Kfloat, /* floating-point numbers */ + Kchar, /* fixed-length strings */ + Kstring, /* strings with prefixed length */ + Kzstr, /* zero-terminated strings */ + Kpadding, /* padding */ + Kpaddalign, /* padding for alignment */ + Knop /* no-op (configuration or spaces) */ +} KOption; + + +/* +** Read an integer numeral from string 'fmt' or return 'df' if +** there is no numeral +*/ +static int digit (int c) { return '0' <= c && c <= '9'; } + +static int getnum (const char **fmt, int df) { + if (!digit(**fmt)) /* no number? */ + return df; /* return default value */ + else { + int a = 0; + do { + a = a*10 + (*((*fmt)++) - '0'); + } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10); + return a; + } +} + + +/* +** Read an integer numeral and raises an error if it is larger +** than the maximum size for integers. +*/ +static int getnumlimit (Header *h, const char **fmt, int df) { + int sz = getnum(fmt, df); + if (sz > MAXINTSIZE || sz <= 0) + luaL_error(h->L, "integral size (%d) out of limits [1,%d]", + sz, MAXINTSIZE); + return sz; +} + + +/* +** Initialize Header +*/ +static void initheader (lua_State *L, Header *h) { + h->L = L; + h->islittle = nativeendian.little; + h->maxalign = 1; +} + + +/* +** Read and classify next option. 'size' is filled with option's size. +*/ +static KOption getoption (Header *h, const char **fmt, int *size) { + int opt = *((*fmt)++); + *size = 0; /* default */ + switch (opt) { + case 'b': *size = sizeof(char); return Kint; + case 'B': *size = sizeof(char); return Kuint; + case 'h': *size = sizeof(short); return Kint; + case 'H': *size = sizeof(short); return Kuint; + case 'l': *size = sizeof(long); return Kint; + case 'L': *size = sizeof(long); return Kuint; + case 'j': *size = sizeof(lua_Integer); return Kint; + case 'J': *size = sizeof(lua_Integer); return Kuint; + case 'T': *size = sizeof(size_t); return Kuint; + case 'f': *size = sizeof(float); return Kfloat; + case 'd': *size = sizeof(double); return Kfloat; + case 'n': *size = sizeof(lua_Number); return Kfloat; + case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; + case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; + case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; + case 'c': + *size = getnum(fmt, -1); + if (*size == -1) + luaL_error(h->L, "missing size for format option 'c'"); + return Kchar; + case 'z': return Kzstr; + case 'x': *size = 1; return Kpadding; + case 'X': return Kpaddalign; + case ' ': break; + case '<': h->islittle = 1; break; + case '>': h->islittle = 0; break; + case '=': h->islittle = nativeendian.little; break; + case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; + default: luaL_error(h->L, "invalid format option '%c'", opt); + } + return Knop; +} + + +/* +** Read, classify, and fill other details about the next option. +** 'psize' is filled with option's size, 'notoalign' with its +** alignment requirements. +** Local variable 'size' gets the size to be aligned. (Kpadal option +** always gets its full alignment, other options are limited by +** the maximum alignment ('maxalign'). Kchar option needs no alignment +** despite its size. +*/ +static KOption getdetails (Header *h, size_t totalsize, + const char **fmt, int *psize, int *ntoalign) { + KOption opt = getoption(h, fmt, psize); + int align = *psize; /* usually, alignment follows size */ + if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ + if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) + luaL_argerror(h->L, 1, "invalid next option for option 'X'"); + } + if (align <= 1 || opt == Kchar) /* need no alignment? */ + *ntoalign = 0; + else { + if (align > h->maxalign) /* enforce maximum alignment */ + align = h->maxalign; + if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ + luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); + *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); + } + return opt; +} + + +/* +** Pack integer 'n' with 'size' bytes and 'islittle' endianness. +** The final 'if' handles the case when 'size' is larger than +** the size of a Lua integer, correcting the extra sign-extension +** bytes if necessary (by default they would be zeros). +*/ +static void packint (luaL_Buffer *b, lua_Unsigned n, + int islittle, int size, int neg) { + char *buff = luaL_prepbuffsize(b, size); + int i; + buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ + for (i = 1; i < size; i++) { + n >>= NB; + buff[islittle ? i : size - 1 - i] = (char)(n & MC); + } + if (neg && size > SZINT) { /* negative number need sign extension? */ + for (i = SZINT; i < size; i++) /* correct extra bytes */ + buff[islittle ? i : size - 1 - i] = (char)MC; + } + luaL_addsize(b, size); /* add result to buffer */ +} + + +/* +** Copy 'size' bytes from 'src' to 'dest', correcting endianness if +** given 'islittle' is different from native endianness. +*/ +static void copywithendian (volatile char *dest, volatile const char *src, + int size, int islittle) { + if (islittle == nativeendian.little) { + while (size-- != 0) + *(dest++) = *(src++); + } + else { + dest += size - 1; + while (size-- != 0) + *(dest--) = *(src++); + } +} + + +static int str_pack (lua_State *L) { + luaL_Buffer b; + Header h; + const char *fmt = luaL_checkstring(L, 1); /* format string */ + int arg = 1; /* current argument to pack */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); + lua_pushnil(L); /* mark to separate arguments from string buffer */ + luaL_buffinit(L, &b); + while (*fmt != '\0') { + int size, ntoalign; + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + totalsize += ntoalign + size; + while (ntoalign-- > 0) + luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ + arg++; + switch (opt) { + case Kint: { /* signed integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) { /* need overflow check? */ + lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); + luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); + } + packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0)); + break; + } + case Kuint: { /* unsigned integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) /* need overflow check? */ + luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), + arg, "unsigned overflow"); + packint(&b, (lua_Unsigned)n, h.islittle, size, 0); + break; + } + case Kfloat: { /* floating-point options */ + volatile Ftypes u; + char *buff = luaL_prepbuffsize(&b, size); + lua_Number n = luaL_checknumber(L, arg); /* get argument */ + if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ + else if (size == sizeof(u.d)) u.d = (double)n; + else u.n = n; + /* move 'u' to final result, correcting endianness if needed */ + copywithendian(buff, u.buff, size, h.islittle); + luaL_addsize(&b, size); + break; + } + case Kchar: { /* fixed-size string */ + size_t len; + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, len == (size_t)size, arg, "wrong length"); + luaL_addlstring(&b, s, size); + break; + } + case Kstring: { /* strings with length count */ + size_t len; + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, size >= (int)sizeof(size_t) || + len < ((size_t)1 << (size * NB)), + arg, "string length does not fit in given size"); + packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ + luaL_addlstring(&b, s, len); + totalsize += len; + break; + } + case Kzstr: { /* zero-terminated string */ + size_t len; + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros"); + luaL_addlstring(&b, s, len); + luaL_addchar(&b, '\0'); /* add zero at the end */ + totalsize += len + 1; + break; + } + case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* go through */ + case Kpaddalign: case Knop: + arg--; /* undo increment */ + break; + } + } + luaL_pushresult(&b); + return 1; +} + + +static int str_packsize (lua_State *L) { + Header h; + const char *fmt = luaL_checkstring(L, 1); /* format string */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); + while (*fmt != '\0') { + int size, ntoalign; + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + size += ntoalign; /* total space used by option */ + luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, + "format result too large"); + totalsize += size; + switch (opt) { + case Kstring: /* strings with length count */ + case Kzstr: /* zero-terminated string */ + luaL_argerror(L, 1, "variable-length format"); + break; + default: break; + } + } + lua_pushinteger(L, (lua_Integer)totalsize); + return 1; +} + + +/* +** Unpack an integer with 'size' bytes and 'islittle' endianness. +** If size is smaller than the size of a Lua integer and integer +** is signed, must do sign extension (propagating the sign to the +** higher bits); if size is larger than the size of a Lua integer, +** it must check the unread bytes to see whether they do not cause an +** overflow. +*/ +static lua_Integer unpackint (lua_State *L, const char *str, + int islittle, int size, int issigned) { + lua_Unsigned res = 0; + int i; + int limit = (size <= SZINT) ? size : SZINT; + for (i = limit - 1; i >= 0; i--) { + res <<= NB; + res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i]; + } + if (size < SZINT) { /* real size smaller than lua_Integer? */ + if (issigned) { /* needs sign extension? */ + lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); + res = ((res ^ mask) - mask); /* do sign extension */ + } + } + else if (size > SZINT) { /* must check unread bytes */ + int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; + for (i = limit; i < size; i++) { + if ((unsigned char)str[islittle ? i : size - 1 - i] != mask) + luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); + } + } + return (lua_Integer)res; +} + + +static int str_unpack (lua_State *L) { + Header h; + const char *fmt = luaL_checkstring(L, 1); + size_t ld; + const char *data = luaL_checklstring(L, 2, &ld); + size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1; + int n = 0; /* number of results */ + luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); + initheader(L, &h); + while (*fmt != '\0') { + int size, ntoalign; + KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); + if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld) + luaL_argerror(L, 2, "data string too short"); + pos += ntoalign; /* skip alignment */ + /* stack space for item + next position */ + luaL_checkstack(L, 2, "too many results"); + n++; + switch (opt) { + case Kint: + case Kuint: { + lua_Integer res = unpackint(L, data + pos, h.islittle, size, + (opt == Kint)); + lua_pushinteger(L, res); + break; + } + case Kfloat: { + volatile Ftypes u; + lua_Number num; + copywithendian(u.buff, data + pos, size, h.islittle); + if (size == sizeof(u.f)) num = (lua_Number)u.f; + else if (size == sizeof(u.d)) num = (lua_Number)u.d; + else num = u.n; + lua_pushnumber(L, num); + break; + } + case Kchar: { + lua_pushlstring(L, data + pos, size); + break; + } + case Kstring: { + size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); + luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short"); + lua_pushlstring(L, data + pos + size, len); + pos += len; /* skip string */ + break; + } + case Kzstr: { + size_t len = (int)strlen(data + pos); + lua_pushlstring(L, data + pos, len); + pos += len + 1; /* skip string plus final '\0' */ + break; + } + case Kpaddalign: case Kpadding: case Knop: + n--; /* undo increment */ + break; + } + pos += size; + } + lua_pushinteger(L, pos + 1); /* next position */ + return n + 1; +} + +/* }====================================================== */ + + static const luaL_Reg strlib[] = { {"byte", str_byte}, {"char", str_char}, @@ -992,6 +1400,9 @@ static const luaL_Reg strlib[] = { {"reverse", str_reverse}, {"sub", str_sub}, {"upper", str_upper}, + {"pack", str_pack}, + {"packsize", str_packsize}, + {"unpack", str_unpack}, {NULL, NULL} }; diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 5d76f97e..e8ef146c 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,27 +1,32 @@ /* -** $Id: ltable.c,v 2.72.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltable.c,v 2.99 2014/11/02 19:19:04 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ +#define ltable_c +#define LUA_CORE + +#include "lprefix.h" + /* ** Implementation of tables (aka arrays, objects, or hash tables). ** Tables keep its elements in two parts: an array part and a hash part. ** Non-negative integer keys are all candidates to be kept in the array -** part. The actual size of the array is the largest `n' such that at +** part. The actual size of the array is the largest 'n' such that at ** least half the slots between 0 and n are in use. ** Hash uses a mix of chained scatter table with Brent's variation. ** A main invariant of these tables is that, if an element is not -** in its main position (i.e. the `original' position that its hash gives +** in its main position (i.e. the 'original' position that its hash gives ** to it), then the colliding element is in its own main position. ** Hence even when the load factor reaches 100%, performance remains good. */ +#include +#include #include - -#define ltable_c -#define LUA_CORE +#include #include "lua.h" @@ -37,21 +42,26 @@ /* -** max size of array part is 2^MAXBITS +** Maximum size of array part (MAXASIZE) is 2^MAXABITS. MAXABITS is +** the largest integer such that MAXASIZE fits in an unsigned int. */ -#if LUAI_BITSINT >= 32 -#define MAXBITS 30 -#else -#define MAXBITS (LUAI_BITSINT-2) -#endif +#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) +#define MAXASIZE (1u << MAXABITS) -#define MAXASIZE (1 << MAXBITS) +/* +** Maximum size of hash part is 2^MAXHBITS. MAXHBITS is the largest +** integer such that 2^MAXHBITS fits in a signed int. (Note that the +** maximum number of elements in a table, 2^MAXABITS + 2^MAXHBITS, still +** fits comfortably in an unsigned int.) +*/ +#define MAXHBITS (MAXABITS - 1) #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) -#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) +#define hashstr(t,str) hashpow2(t, (str)->hash) #define hashboolean(t,p) hashpow2(t, p) +#define hashint(t,i) hashpow2(t, i) /* @@ -61,7 +71,7 @@ #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) -#define hashpointer(t,p) hashmod(t, IntPoint(p)) +#define hashpointer(t,p) hashmod(t, point2int(p)) #define dummynode (&dummynode_) @@ -70,16 +80,28 @@ static const Node dummynode_ = { {NILCONSTANT}, /* value */ - {{NILCONSTANT, NULL}} /* key */ + {{NILCONSTANT, 0}} /* key */ }; /* -** hash for lua_Numbers +** Checks whether a float has a value representable as a lua_Integer +** (and does the conversion if so) */ -static Node *hashnum (const Table *t, lua_Number n) { +static int numisinteger (lua_Number x, lua_Integer *p) { + if ((x) == l_floor(x)) /* integral value? */ + return lua_numbertointeger(x, p); /* try as an integer */ + else return 0; +} + + +/* +** hash for floating-point numbers +*/ +static Node *hashfloat (const Table *t, lua_Number n) { int i; - luai_hashnum(i, n); + n = l_mathop(frexp)(n, &i) * cast_num(INT_MAX - DBL_MAX_EXP); + i += cast_int(n); if (i < 0) { if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */ i = 0; /* handle INT_MIN */ @@ -91,23 +113,25 @@ static Node *hashnum (const Table *t, lua_Number n) { /* -** returns the `main' position of an element in a table (that is, the index +** returns the 'main' position of an element in a table (that is, the index ** of its hash value) */ static Node *mainposition (const Table *t, const TValue *key) { switch (ttype(key)) { - case LUA_TNUMBER: - return hashnum(t, nvalue(key)); - case LUA_TLNGSTR: { - TString *s = rawtsvalue(key); - if (s->tsv.extra == 0) { /* no hash? */ - s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash); - s->tsv.extra = 1; /* now it has its hash */ - } - return hashstr(t, rawtsvalue(key)); - } + case LUA_TNUMINT: + return hashint(t, ivalue(key)); + case LUA_TNUMFLT: + return hashfloat(t, fltvalue(key)); case LUA_TSHRSTR: - return hashstr(t, rawtsvalue(key)); + return hashstr(t, tsvalue(key)); + case LUA_TLNGSTR: { + TString *s = tsvalue(key); + if (s->extra == 0) { /* no hash? */ + s->hash = luaS_hash(getstr(s), s->len, s->hash); + s->extra = 1; /* now it has its hash */ + } + return hashstr(t, tsvalue(key)); + } case LUA_TBOOLEAN: return hashboolean(t, bvalue(key)); case LUA_TLIGHTUSERDATA: @@ -121,61 +145,61 @@ static Node *mainposition (const Table *t, const TValue *key) { /* -** returns the index for `key' if `key' is an appropriate key to live in -** the array part of the table, -1 otherwise. +** returns the index for 'key' if 'key' is an appropriate key to live in +** the array part of the table, 0 otherwise. */ -static int arrayindex (const TValue *key) { - if (ttisnumber(key)) { - lua_Number n = nvalue(key); - int k; - lua_number2int(k, n); - if (luai_numeq(cast_num(k), n)) - return k; +static unsigned int arrayindex (const TValue *key) { + if (ttisinteger(key)) { + lua_Integer k = ivalue(key); + if (0 < k && (lua_Unsigned)k <= MAXASIZE) + return cast(unsigned int, k); /* 'key' is an appropriate array index */ } - return -1; /* `key' did not match some condition */ + return 0; /* 'key' did not match some condition */ } /* -** returns the index of a `key' for table traversals. First goes all +** returns the index of a 'key' for table traversals. First goes all ** elements in the array part, then elements in the hash part. The -** beginning of a traversal is signaled by -1. +** beginning of a traversal is signaled by 0. */ -static int findindex (lua_State *L, Table *t, StkId key) { - int i; - if (ttisnil(key)) return -1; /* first iteration */ +static unsigned int findindex (lua_State *L, Table *t, StkId key) { + unsigned int i; + if (ttisnil(key)) return 0; /* first iteration */ i = arrayindex(key); - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ - return i-1; /* yes; that's the index (corrected to C) */ + if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */ + return i; /* yes; that's the index */ else { + int nx; Node *n = mainposition(t, key); - for (;;) { /* check whether `key' is somewhere in the chain */ - /* key may be dead already, but it is ok to use it in `next' */ + for (;;) { /* check whether 'key' is somewhere in the chain */ + /* key may be dead already, but it is ok to use it in 'next' */ if (luaV_rawequalobj(gkey(n), key) || (ttisdeadkey(gkey(n)) && iscollectable(key) && deadvalue(gkey(n)) == gcvalue(key))) { i = cast_int(n - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ - return i + t->sizearray; + return (i + 1) + t->sizearray; } - else n = gnext(n); - if (n == NULL) - luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ + nx = gnext(n); + if (nx == 0) + luaG_runerror(L, "invalid key to 'next'"); /* key not found */ + else n += nx; } } } int luaH_next (lua_State *L, Table *t, StkId key) { - int i = findindex(L, t, key); /* find original element */ - for (i++; i < t->sizearray; i++) { /* try first array part */ + unsigned int i = findindex(L, t, key); /* find original element */ + for (; i < t->sizearray; i++) { /* try first array part */ if (!ttisnil(&t->array[i])) { /* a non-nil value? */ - setnvalue(key, cast_num(i+1)); + setivalue(key, i + 1); setobj2s(L, key+1, &t->array[i]); return 1; } } - for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ + for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */ if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ setobj2s(L, key, gkey(gnode(t, i))); setobj2s(L, key+1, gval(gnode(t, i))); @@ -192,19 +216,24 @@ int luaH_next (lua_State *L, Table *t, StkId key) { ** ============================================================== */ - -static int computesizes (int nums[], int *narray) { +/* +** Compute the optimal size for the array part of table 't'. 'nums' is a +** "count array" where 'nums[i]' is the number of integers in the table +** between 2^(i - 1) + 1 and 2^i. Put in '*narray' the optimal size, and +** return the number of elements that will go to that part. +*/ +static unsigned int computesizes (unsigned int nums[], unsigned int *narray) { int i; - int twotoi; /* 2^i */ - int a = 0; /* number of elements smaller than 2^i */ - int na = 0; /* number of elements to go to array part */ - int n = 0; /* optimal size for array part */ + unsigned int twotoi; /* 2^i */ + unsigned int a = 0; /* number of elements smaller than 2^i */ + unsigned int na = 0; /* number of elements to go to array part */ + unsigned int n = 0; /* optimal size for array part */ for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { if (nums[i] > 0) { a += nums[i]; if (a > twotoi/2) { /* more than half elements present? */ n = twotoi; /* optimal size (till now) */ - na = a; /* all elements smaller than n will go to array part */ + na = a; /* all elements up to 'n' will go to array part */ } } if (a == *narray) break; /* all elements already counted */ @@ -215,9 +244,9 @@ static int computesizes (int nums[], int *narray) { } -static int countint (const TValue *key, int *nums) { - int k = arrayindex(key); - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ +static int countint (const TValue *key, unsigned int *nums) { + unsigned int k = arrayindex(key); + if (k != 0) { /* is 'key' an appropriate array index? */ nums[luaO_ceillog2(k)]++; /* count as such */ return 1; } @@ -226,20 +255,21 @@ static int countint (const TValue *key, int *nums) { } -static int numusearray (const Table *t, int *nums) { +static unsigned int numusearray (const Table *t, unsigned int *nums) { int lg; - int ttlg; /* 2^lg */ - int ause = 0; /* summation of `nums' */ - int i = 1; /* count to traverse all array keys */ - for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ - int lc = 0; /* counter */ - int lim = ttlg; + unsigned int ttlg; /* 2^lg */ + unsigned int ause = 0; /* summation of 'nums' */ + unsigned int i = 1; /* count to traverse all array keys */ + /* traverse each slice */ + for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { + unsigned int lc = 0; /* counter */ + unsigned int lim = ttlg; if (lim > t->sizearray) { lim = t->sizearray; /* adjust upper limit */ if (i > lim) break; /* no more elements to count */ } - /* count elements in range (2^(lg-1), 2^lg] */ + /* count elements in range (2^(lg - 1), 2^lg] */ for (; i <= lim; i++) { if (!ttisnil(&t->array[i-1])) lc++; @@ -251,9 +281,10 @@ static int numusearray (const Table *t, int *nums) { } -static int numusehash (const Table *t, int *nums, int *pnasize) { +static int numusehash (const Table *t, unsigned int *nums, + unsigned int *pnasize) { int totaluse = 0; /* total number of elements */ - int ause = 0; /* summation of `nums' */ + int ause = 0; /* elements added to 'nums' (can go to array part) */ int i = sizenode(t); while (i--) { Node *n = &t->node[i]; @@ -267,8 +298,8 @@ static int numusehash (const Table *t, int *nums, int *pnasize) { } -static void setarrayvector (lua_State *L, Table *t, int size) { - int i; +static void setarrayvector (lua_State *L, Table *t, unsigned int size) { + unsigned int i; luaM_reallocvector(L, t->array, t->sizearray, size, TValue); for (i=t->sizearray; iarray[i]); @@ -276,23 +307,23 @@ static void setarrayvector (lua_State *L, Table *t, int size) { } -static void setnodevector (lua_State *L, Table *t, int size) { +static void setnodevector (lua_State *L, Table *t, unsigned int size) { int lsize; if (size == 0) { /* no elements to hash part? */ - t->node = cast(Node *, dummynode); /* use common `dummynode' */ + t->node = cast(Node *, dummynode); /* use common 'dummynode' */ lsize = 0; } else { int i; lsize = luaO_ceillog2(size); - if (lsize > MAXBITS) + if (lsize > MAXHBITS) luaG_runerror(L, "table overflow"); size = twoto(lsize); t->node = luaM_newvector(L, size, Node); - for (i=0; isizearray; +void luaH_resize (lua_State *L, Table *t, unsigned int nasize, + unsigned int nhsize) { + unsigned int i; + int j; + unsigned int oldasize = t->sizearray; int oldhsize = t->lsizenode; Node *nold = t->node; /* save old hash ... */ if (nasize > oldasize) /* array part must grow? */ @@ -321,8 +354,8 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { luaM_reallocvector(L, t->array, oldasize, nasize, TValue); } /* re-insert elements from hash part */ - for (i = twoto(oldhsize) - 1; i >= 0; i--) { - Node *old = nold+i; + for (j = twoto(oldhsize) - 1; j >= 0; j--) { + Node *old = nold + j; if (!ttisnil(gval(old))) { /* doesn't need barrier/invalidate cache, as entry was already present in the table */ @@ -334,18 +367,20 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { } -void luaH_resizearray (lua_State *L, Table *t, int nasize) { +void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { int nsize = isdummy(t->node) ? 0 : sizenode(t); luaH_resize(L, t, nasize, nsize); } - +/* +** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i +*/ static void rehash (lua_State *L, Table *t, const TValue *ek) { - int nasize, na; - int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */ + unsigned int nasize, na; + unsigned int nums[MAXABITS + 1]; int i; int totaluse; - for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ + for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ nasize = numusearray(t, nums); /* count keys in array part */ totaluse = nasize; /* all those keys are integer keys */ totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ @@ -366,7 +401,8 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { Table *luaH_new (lua_State *L) { - Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h; + GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table)); + Table *t = gco2t(o); t->metatable = NULL; t->flags = cast_byte(~0); t->array = NULL; @@ -404,37 +440,52 @@ static Node *getfreepos (Table *t) { */ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { Node *mp; + TValue aux; if (ttisnil(key)) luaG_runerror(L, "table index is nil"); - else if (ttisnumber(key) && luai_numisnan(L, nvalue(key))) - luaG_runerror(L, "table index is NaN"); + else if (ttisfloat(key)) { + lua_Number n = fltvalue(key); + lua_Integer k; + if (luai_numisnan(n)) + luaG_runerror(L, "table index is NaN"); + if (numisinteger(n, &k)) { /* index is int? */ + setivalue(&aux, k); + key = &aux; /* insert it as an integer */ + } + } mp = mainposition(t, key); if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ Node *othern; - Node *n = getfreepos(t); /* get a free place */ - if (n == NULL) { /* cannot find a free place? */ + Node *f = getfreepos(t); /* get a free place */ + if (f == NULL) { /* cannot find a free place? */ rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' take care of TM cache and GC barrier */ + /* whatever called 'newkey' takes care of TM cache and GC barrier */ return luaH_set(L, t, key); /* insert key into grown table */ } - lua_assert(!isdummy(n)); + lua_assert(!isdummy(f)); othern = mainposition(t, gkey(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ - gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ - *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ - gnext(mp) = NULL; /* now `mp' is free */ + while (othern + gnext(othern) != mp) /* find previous */ + othern += gnext(othern); + gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */ + *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ + if (gnext(mp) != 0) { + gnext(f) += cast_int(mp - f); /* correct 'next' */ + gnext(mp) = 0; /* now 'mp' is free */ + } setnilvalue(gval(mp)); } else { /* colliding node is in its own main position */ /* new node will go into free position */ - gnext(n) = gnext(mp); /* chain new position */ - gnext(mp) = n; - mp = n; + if (gnext(mp) != 0) + gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */ + else lua_assert(gnext(f) == 0); + gnext(mp) = cast_int(f - mp); + mp = f; } } - setobj2t(L, gkey(mp), key); - luaC_barrierback(L, obj2gco(t), key); + setkey(L, &mp->i_key, key); + luaC_barrierback(L, t, key); lua_assert(ttisnil(gval(mp))); return gval(mp); } @@ -443,18 +494,21 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { /* ** search function for integers */ -const TValue *luaH_getint (Table *t, int key) { +const TValue *luaH_getint (Table *t, lua_Integer key) { /* (1 <= key && key <= t->sizearray) */ - if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) - return &t->array[key-1]; + if (l_castS2U(key - 1) < t->sizearray) + return &t->array[key - 1]; else { - lua_Number nk = cast_num(key); - Node *n = hashnum(t, nk); - do { /* check whether `key' is somewhere in the chain */ - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) + Node *n = hashint(t, key); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key) return gval(n); /* that's it */ - else n = gnext(n); - } while (n); + else { + int nx = gnext(n); + if (nx == 0) break; + n += nx; + } + }; return luaO_nilobject; } } @@ -465,12 +519,17 @@ const TValue *luaH_getint (Table *t, int key) { */ const TValue *luaH_getstr (Table *t, TString *key) { Node *n = hashstr(t, key); - lua_assert(key->tsv.tt == LUA_TSHRSTR); - do { /* check whether `key' is somewhere in the chain */ - if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key)) + lua_assert(key->tt == LUA_TSHRSTR); + for (;;) { /* check whether 'key' is somewhere in the chain */ + const TValue *k = gkey(n); + if (ttisshrstring(k) && eqshrstr(tsvalue(k), key)) return gval(n); /* that's it */ - else n = gnext(n); - } while (n); + else { + int nx = gnext(n); + if (nx == 0) break; + n += nx; + } + }; return luaO_nilobject; } @@ -480,23 +539,26 @@ const TValue *luaH_getstr (Table *t, TString *key) { */ const TValue *luaH_get (Table *t, const TValue *key) { switch (ttype(key)) { - case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key)); + case LUA_TSHRSTR: return luaH_getstr(t, tsvalue(key)); + case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); case LUA_TNIL: return luaO_nilobject; - case LUA_TNUMBER: { - int k; - lua_Number n = nvalue(key); - lua_number2int(k, n); - if (luai_numeq(cast_num(k), n)) /* index is int? */ + case LUA_TNUMFLT: { + lua_Integer k; + if (numisinteger(fltvalue(key), &k)) /* index is int? */ return luaH_getint(t, k); /* use specialized version */ /* else go through */ } default: { Node *n = mainposition(t, key); - do { /* check whether `key' is somewhere in the chain */ + for (;;) { /* check whether 'key' is somewhere in the chain */ if (luaV_rawequalobj(gkey(n), key)) return gval(n); /* that's it */ - else n = gnext(n); - } while (n); + else { + int nx = gnext(n); + if (nx == 0) break; + n += nx; + } + }; return luaO_nilobject; } } @@ -515,14 +577,14 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { } -void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { +void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { const TValue *p = luaH_getint(t, key); TValue *cell; if (p != luaO_nilobject) cell = cast(TValue *, p); else { TValue k; - setnvalue(&k, cast_num(key)); + setivalue(&k, key); cell = luaH_newkey(L, t, &k); } setobj2t(L, cell, value); @@ -532,16 +594,16 @@ void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { static int unbound_search (Table *t, unsigned int j) { unsigned int i = j; /* i is zero or a present index */ j++; - /* find `i' and `j' such that i is present and j is not */ + /* find 'i' and 'j' such that i is present and j is not */ while (!ttisnil(luaH_getint(t, j))) { i = j; - j *= 2; - if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ + if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ /* table was built with bad purposes: resort to linear search */ i = 1; while (!ttisnil(luaH_getint(t, i))) i++; return i - 1; } + j *= 2; } /* now do a binary search between them */ while (j - i > 1) { @@ -554,7 +616,7 @@ static int unbound_search (Table *t, unsigned int j) { /* -** Try to find a boundary in table `t'. A `boundary' is an integer index +** Try to find a boundary in table 't'. A 'boundary' is an integer index ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). */ int luaH_getn (Table *t) { diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index d69449b2..53d25511 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $ +** $Id: ltable.h,v 2.20 2014/09/04 18:15:29 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -11,26 +11,34 @@ #define gnode(t,i) (&(t)->node[i]) -#define gkey(n) (&(n)->i_key.tvk) #define gval(n) (&(n)->i_val) #define gnext(n) ((n)->i_key.nk.next) + +/* 'const' to avoid wrong writings that can mess up field 'next' */ +#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) + +#define wgkey(n) (&(n)->i_key.nk) + #define invalidateTMcache(t) ((t)->flags = 0) + /* returns the key, given the value of a table entry */ #define keyfromval(v) \ (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) -LUAI_FUNC const TValue *luaH_getint (Table *t, int key); -LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); +LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); +LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, + TValue *value); LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); LUAI_FUNC Table *luaH_new (lua_State *L); -LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); +LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, + unsigned int nhsize); +LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC int luaH_getn (Table *t); diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index 6001224e..8f78afb7 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -1,23 +1,58 @@ /* -** $Id: ltablib.c,v 1.65.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltablib.c,v 1.79 2014/11/02 19:19:04 roberto Exp $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ - -#include - #define ltablib_c #define LUA_LIB +#include "lprefix.h" + + +#include +#include + #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) +/* +** Structure with table-access functions +*/ +typedef struct { + int (*geti) (lua_State *L, int idx, lua_Integer n); + void (*seti) (lua_State *L, int idx, lua_Integer n); +} TabA; + + +/* +** Check that 'arg' has a table and set access functions in 'ta' to raw +** or non-raw according to the presence of corresponding metamethods. +*/ +static void checktab (lua_State *L, int arg, TabA *ta) { + ta->geti = NULL; ta->seti = NULL; + if (lua_getmetatable(L, arg)) { + lua_pushliteral(L, "__index"); /* 'index' metamethod */ + if (lua_rawget(L, -2) != LUA_TNIL) + ta->geti = lua_geti; + lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */ + if (lua_rawget(L, -3) != LUA_TNIL) + ta->seti = lua_seti; + lua_pop(L, 3); /* pop metatable plus both metamethods */ + } + if (ta->geti == NULL || ta->seti == NULL) { + luaL_checktype(L, arg, LUA_TTABLE); /* must be table for raw methods */ + if (ta->geti == NULL) ta->geti = lua_rawgeti; + if (ta->seti == NULL) ta->seti = lua_rawseti; + } +} + + +#define aux_getn(L,n,ta) (checktab(L, n, ta), luaL_len(L, n)) #if defined(LUA_COMPAT_MAXN) @@ -39,72 +74,110 @@ static int maxn (lua_State *L) { static int tinsert (lua_State *L) { - int e = aux_getn(L, 1) + 1; /* first empty element */ - int pos; /* where to insert new element */ + TabA ta; + lua_Integer e = aux_getn(L, 1, &ta) + 1; /* first empty element */ + lua_Integer pos; /* where to insert new element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ pos = e; /* insert new element at the end */ break; } case 3: { - int i; - pos = luaL_checkint(L, 2); /* 2nd argument is the position */ + lua_Integer i; + pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); for (i = e; i > pos; i--) { /* move up elements */ - lua_rawgeti(L, 1, i-1); - lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ + (*ta.geti)(L, 1, i - 1); + (*ta.seti)(L, 1, i); /* t[i] = t[i - 1] */ } break; } default: { - return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); + return luaL_error(L, "wrong number of arguments to 'insert'"); } } - lua_rawseti(L, 1, pos); /* t[pos] = v */ + (*ta.seti)(L, 1, pos); /* t[pos] = v */ return 0; } static int tremove (lua_State *L) { - int size = aux_getn(L, 1); - int pos = luaL_optint(L, 2, size); + TabA ta; + lua_Integer size = aux_getn(L, 1, &ta); + lua_Integer pos = luaL_optinteger(L, 2, size); if (pos != size) /* validate 'pos' if given */ luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); - lua_rawgeti(L, 1, pos); /* result = t[pos] */ + (*ta.geti)(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { - lua_rawgeti(L, 1, pos+1); - lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ + (*ta.geti)(L, 1, pos + 1); + (*ta.seti)(L, 1, pos); /* t[pos] = t[pos + 1] */ } lua_pushnil(L); - lua_rawseti(L, 1, pos); /* t[pos] = nil */ + (*ta.seti)(L, 1, pos); /* t[pos] = nil */ return 1; } -static void addfield (lua_State *L, luaL_Buffer *b, int i) { - lua_rawgeti(L, 1, i); +static int tmove (lua_State *L) { + TabA ta; + lua_Integer f = luaL_checkinteger(L, 2); + lua_Integer e = luaL_checkinteger(L, 3); + lua_Integer t = luaL_checkinteger(L, 4); + int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ + /* the following restriction avoids several problems with overflows */ + luaL_argcheck(L, f > 0, 2, "initial position must be positive"); + if (e >= f) { /* otherwise, nothing to move */ + lua_Integer n, i; + ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL) + ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti) + : lua_geti; + ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL) + ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) + : lua_seti; + n = e - f + 1; /* number of elements to move */ + if (t > f) { + for (i = n - 1; i >= 0; i--) { + (*ta.geti)(L, 1, f + i); + (*ta.seti)(L, tt, t + i); + } + } + else { + for (i = 0; i < n; i++) { + (*ta.geti)(L, 1, f + i); + (*ta.seti)(L, tt, t + i); + } + } + } + lua_pushvalue(L, tt); /* return "to table" */ + return 1; +} + + +static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { + (*ta->geti)(L, 1, i); if (!lua_isstring(L, -1)) - luaL_error(L, "invalid value (%s) at index %d in table for " - LUA_QL("concat"), luaL_typename(L, -1), i); + luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", + luaL_typename(L, -1), i); luaL_addvalue(b); } static int tconcat (lua_State *L) { + TabA ta; luaL_Buffer b; size_t lsep; - int i, last; + lua_Integer i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 3, 1); - last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); + checktab(L, 1, &ta); + i = luaL_optinteger(L, 3, 1); + last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); luaL_buffinit(L, &b); for (; i < last; i++) { - addfield(L, &b, i); + addfield(L, &b, &ta, i); luaL_addlstring(&b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ - addfield(L, &b, i); + addfield(L, &b, &ta, i); luaL_pushresult(&b); return 1; } @@ -117,35 +190,34 @@ static int tconcat (lua_State *L) { */ static int pack (lua_State *L) { + int i; int n = lua_gettop(L); /* number of elements to pack */ lua_createtable(L, n, 1); /* create result table */ + lua_insert(L, 1); /* put it at index 1 */ + for (i = n; i >= 1; i--) /* assign elements */ + lua_rawseti(L, 1, i); lua_pushinteger(L, n); - lua_setfield(L, -2, "n"); /* t.n = number of elements */ - if (n > 0) { /* at least one element? */ - int i; - lua_pushvalue(L, 1); - lua_rawseti(L, -2, 1); /* insert first element */ - lua_replace(L, 1); /* move table into index 1 */ - for (i = n; i >= 2; i--) /* assign other elements */ - lua_rawseti(L, 1, i); - } + lua_setfield(L, 1, "n"); /* t.n = number of elements */ return 1; /* return table */ } static int unpack (lua_State *L) { - int i, e, n; - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); + TabA ta; + lua_Integer i, e; + lua_Unsigned n; + checktab(L, 1, &ta); + i = luaL_optinteger(L, 2, 1); + e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ - n = e - i + 1; /* number of elements */ - if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ + n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ + if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) return luaL_error(L, "too many results to unpack"); - lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ - while (i++ < e) /* push arg[i + 1...e] */ - lua_rawgeti(L, 1, i); - return n; + do { /* must have at least one element */ + (*ta.geti)(L, 1, i); /* push arg[i..e] */ + } while (i++ < e); + + return (int)n; } /* }====================================================== */ @@ -155,15 +227,15 @@ static int unpack (lua_State *L) { /* ** {====================================================== ** Quicksort -** (based on `Algorithms in MODULA-3', Robert Sedgewick; +** (based on 'Algorithms in MODULA-3', Robert Sedgewick; ** Addison-Wesley, 1993.) ** ======================================================= */ -static void set2 (lua_State *L, int i, int j) { - lua_rawseti(L, 1, i); - lua_rawseti(L, 1, j); +static void set2 (lua_State *L, TabA *ta, int i, int j) { + (*ta->seti)(L, 1, i); + (*ta->seti)(L, 1, j); } static int sort_comp (lua_State *L, int a, int b) { @@ -171,7 +243,7 @@ static int sort_comp (lua_State *L, int a, int b) { int res; lua_pushvalue(L, 2); lua_pushvalue(L, a-1); /* -1 to compensate function */ - lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ + lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ lua_call(L, 2, 1); res = lua_toboolean(L, -1); lua_pop(L, 1); @@ -181,45 +253,45 @@ static int sort_comp (lua_State *L, int a, int b) { return lua_compare(L, a, b, LUA_OPLT); } -static void auxsort (lua_State *L, int l, int u) { +static void auxsort (lua_State *L, TabA *ta, int l, int u) { while (l < u) { /* for tail recursion */ int i, j; /* sort elements a[l], a[(l+u)/2] and a[u] */ - lua_rawgeti(L, 1, l); - lua_rawgeti(L, 1, u); + (*ta->geti)(L, 1, l); + (*ta->geti)(L, 1, u); if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ - set2(L, l, u); /* swap a[l] - a[u] */ + set2(L, ta, l, u); /* swap a[l] - a[u] */ else lua_pop(L, 2); if (u-l == 1) break; /* only 2 elements */ i = (l+u)/2; - lua_rawgeti(L, 1, i); - lua_rawgeti(L, 1, l); + (*ta->geti)(L, 1, i); + (*ta->geti)(L, 1, l); if (sort_comp(L, -2, -1)) /* a[i]geti)(L, 1, u); if (sort_comp(L, -1, -2)) /* a[u]geti)(L, 1, i); /* Pivot */ lua_pushvalue(L, -1); - lua_rawgeti(L, 1, u-1); - set2(L, i, u-1); + (*ta->geti)(L, 1, u-1); + set2(L, ta, i, u-1); /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ i = l; j = u-1; for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ /* repeat ++i until a[i] >= P */ - while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { + while ((*ta->geti)(L, 1, ++i), sort_comp(L, -1, -2)) { if (i>=u) luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } /* repeat --j until a[j] <= P */ - while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { + while ((*ta->geti)(L, 1, --j), sort_comp(L, -3, -1)) { if (j<=l) luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ } @@ -227,11 +299,11 @@ static void auxsort (lua_State *L, int l, int u) { lua_pop(L, 3); /* pop pivot, a[i], a[j] */ break; } - set2(L, i, j); + set2(L, ta, i, j); } - lua_rawgeti(L, 1, u-1); - lua_rawgeti(L, 1, i); - set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ + (*ta->geti)(L, 1, u-1); + (*ta->geti)(L, 1, i); + set2(L, ta, u-1, i); /* swap pivot (a[u-1]) with a[i] */ /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ if (i-l < u-i) { @@ -240,17 +312,18 @@ static void auxsort (lua_State *L, int l, int u) { else { j=i+1; i=u; u=j-2; } - auxsort(L, j, i); /* call recursively the smaller one */ + auxsort(L, ta, j, i); /* call recursively the smaller one */ } /* repeat the routine for the larger one */ } static int sort (lua_State *L) { - int n = aux_getn(L, 1); - luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ + TabA ta; + int n = (int)aux_getn(L, 1, &ta); + luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */ if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ luaL_checktype(L, 2, LUA_TFUNCTION); - lua_settop(L, 2); /* make sure there is two arguments */ - auxsort(L, 1, n); + lua_settop(L, 2); /* make sure there are two arguments */ + auxsort(L, &ta, 1, n); return 0; } @@ -266,6 +339,7 @@ static const luaL_Reg tab_funcs[] = { {"pack", pack}, {"unpack", unpack}, {"remove", tremove}, + {"move", tmove}, {"sort", sort}, {NULL, NULL} }; diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 69b4ed77..25b46b17 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,22 +1,27 @@ /* -** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltm.c,v 2.33 2014/11/21 12:15:57 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ - -#include - #define ltm_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" +#include "ldebug.h" +#include "ldo.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" +#include "lvm.h" static const char udatatypename[] = "userdata"; @@ -25,7 +30,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { "no value", "nil", "boolean", udatatypename, "number", "string", "table", "function", udatatypename, "thread", - "proto", "upval" /* these last two cases are used for tests only */ + "proto" /* this last case is used for tests only */ }; @@ -33,14 +38,16 @@ void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", "__gc", "__mode", "__len", "__eq", - "__add", "__sub", "__mul", "__div", "__mod", - "__pow", "__unm", "__lt", "__le", + "__add", "__sub", "__mul", "__mod", "__pow", + "__div", "__idiv", + "__band", "__bor", "__bxor", "__shl", "__shr", + "__unm", "__bnot", "__lt", "__le", "__concat", "__call" }; int i; for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); - luaS_fix(G(L)->tmname[i]); /* never collect these names */ + luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ } } @@ -62,7 +69,7 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { Table *mt; - switch (ttypenv(o)) { + switch (ttnov(o)) { case LUA_TTABLE: mt = hvalue(o)->metatable; break; @@ -70,8 +77,67 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { mt = uvalue(o)->metatable; break; default: - mt = G(L)->mt[ttypenv(o)]; + mt = G(L)->mt[ttnov(o)]; } return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); } + +void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, TValue *p3, int hasres) { + ptrdiff_t result = savestack(L, p3); + setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */ + setobj2s(L, L->top++, p1); /* 1st argument */ + setobj2s(L, L->top++, p2); /* 2nd argument */ + if (!hasres) /* no result? 'p3' is third argument */ + setobj2s(L, L->top++, p3); /* 3rd argument */ + /* metamethod may yield only when called from Lua code */ + luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); + if (hasres) { /* if has result, move it to its place */ + p3 = restorestack(L, result); + setobjs2s(L, p3, --L->top); + } +} + + +int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event) { + const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ + if (ttisnil(tm)) + tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ + if (ttisnil(tm)) return 0; + luaT_callTM(L, tm, p1, p2, res, 1); + return 1; +} + + +void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event) { + if (!luaT_callbinTM(L, p1, p2, res, event)) { + switch (event) { + case TM_CONCAT: + luaG_concaterror(L, p1, p2); + case TM_BAND: case TM_BOR: case TM_BXOR: + case TM_SHL: case TM_SHR: case TM_BNOT: { + lua_Number dummy; + if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) + luaG_tointerror(L, p1, p2); + else + luaG_opinterror(L, p1, p2, "perform bitwise operation on"); + /* else go through */ + } + default: + luaG_opinterror(L, p1, p2, "perform arithmetic on"); + } + } +} + + +int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, + TMS event) { + if (!luaT_callbinTM(L, p1, p2, L->top, event)) + return -1; /* no metamethod */ + else + return !l_isfalse(L->top); +} + diff --git a/3rd/lua/ltm.h b/3rd/lua/ltm.h index 7f89c841..180179ce 100644 --- a/3rd/lua/ltm.h +++ b/3rd/lua/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -13,7 +13,7 @@ /* * WARNING: if you change the order of this enumeration, -* grep "ORDER TM" +* grep "ORDER TM" and "ORDER OP" */ typedef enum { TM_INDEX, @@ -21,14 +21,21 @@ typedef enum { TM_GC, TM_MODE, TM_LEN, - TM_EQ, /* last tag method with `fast' access */ + TM_EQ, /* last tag method with fast access */ TM_ADD, TM_SUB, TM_MUL, - TM_DIV, TM_MOD, TM_POW, + TM_DIV, + TM_IDIV, + TM_BAND, + TM_BOR, + TM_BXOR, + TM_SHL, + TM_SHR, TM_UNM, + TM_BNOT, TM_LT, TM_LE, TM_CONCAT, @@ -44,7 +51,7 @@ typedef enum { #define fasttm(l,et,e) gfasttm(G(l), et, e) #define ttypename(x) luaT_typenames_[(x) + 1] -#define objtypename(x) ttypename(ttypenv(x)) +#define objtypename(x) ttypename(ttnov(x)) LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; @@ -54,4 +61,15 @@ LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event); LUAI_FUNC void luaT_init (lua_State *L); +LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, TValue *p3, int hasres); +LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event); +LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event); +LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, + const TValue *p2, TMS event); + + + #endif diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 4345e554..34a3900a 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,17 +1,19 @@ /* -** $Id: lua.c,v 1.206.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lua.c,v 1.222 2014/11/11 19:41:27 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ +#define lua_c + +#include "lprefix.h" + #include #include #include #include -#define lua_c - #include "lua.h" #include "lauxlib.h" @@ -31,28 +33,38 @@ #define LUA_MAXINPUT 512 #endif -#if !defined(LUA_INIT) -#define LUA_INIT "LUA_INIT" +#if !defined(LUA_INIT_VAR) +#define LUA_INIT_VAR "LUA_INIT" #endif -#define LUA_INITVERSION \ - LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR +#define LUA_INITVARVERSION \ + LUA_INIT_VAR "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR /* ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that ** is, whether we're running lua interactively). */ -#if defined(LUA_USE_ISATTY) +#if !defined(lua_stdin_is_tty) /* { */ + +#if defined(LUA_USE_POSIX) /* { */ + #include #define lua_stdin_is_tty() isatty(0) -#elif defined(LUA_WIN) + +#elif defined(LUA_USE_WINDOWS) /* }{ */ + #include -#include #define lua_stdin_is_tty() _isatty(_fileno(stdin)) -#else + +#else /* }{ */ + +/* ISO C definition */ #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ -#endif + +#endif /* } */ + +#endif /* } */ /* @@ -61,9 +73,10 @@ ** lua_saveline defines how to "save" a read line in a "history". ** lua_freeline defines how to free a line read by lua_readline. */ -#if defined(LUA_USE_READLINE) +#if !defined(lua_readline) /* { */ + +#if defined(LUA_USE_READLINE) /* { */ -#include #include #include #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) @@ -72,7 +85,7 @@ add_history(lua_tostring(L, idx)); /* add it to history */ #define lua_freeline(L,b) ((void)L, free(b)) -#elif !defined(lua_readline) +#else /* }{ */ #define lua_readline(L,b,p) \ ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ @@ -80,7 +93,9 @@ #define lua_saveline(L,idx) { (void)L; (void)idx; } #define lua_freeline(L,b) { (void)L; (void)b; } -#endif +#endif /* } */ + +#endif /* } */ @@ -90,33 +105,40 @@ static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; - +/* +** Hook set by signal function to stop the interpreter. +*/ static void lstop (lua_State *L, lua_Debug *ar) { (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); + lua_sethook(L, NULL, 0, 0); /* reset hook */ luaL_error(L, "interrupted!"); } +/* +** Function to be called at a C signal. Because a C signal cannot +** just change a Lua state (as there is no proper synchronization), +** this function only sets a hook that, when called, will stop the +** interpreter. +*/ static void laction (int i) { - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ + signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); } static void print_usage (const char *badoption) { - luai_writestringerror("%s: ", progname); + lua_writestringerror("%s: ", progname); if (badoption[1] == 'e' || badoption[1] == 'l') - luai_writestringerror("'%s' needs argument\n", badoption); + lua_writestringerror("'%s' needs argument\n", badoption); else - luai_writestringerror("unrecognized option '%s'\n", badoption); - luai_writestringerror( + lua_writestringerror("unrecognized option '%s'\n", badoption); + lua_writestringerror( "usage: %s [options] [script [args]]\n" "Available options are:\n" - " -e stat execute string " LUA_QL("stat") "\n" - " -i enter interactive mode after executing " LUA_QL("script") "\n" - " -l name require library " LUA_QL("name") "\n" + " -e stat execute string 'stat'\n" + " -i enter interactive mode after executing 'script'\n" + " -l name require library 'name'\n" " -v show version information\n" " -E ignore environment variables\n" " -- stop handling options\n" @@ -126,101 +148,114 @@ static void print_usage (const char *badoption) { } +/* +** Prints an error message, adding the program name in front of it +** (if present) +*/ static void l_message (const char *pname, const char *msg) { - if (pname) luai_writestringerror("%s: ", pname); - luai_writestringerror("%s\n", msg); + if (pname) lua_writestringerror("%s: ", pname); + lua_writestringerror("%s\n", msg); } +/* +** Check whether 'status' is not OK and, if so, prints the error +** message on the top of the stack. It assumes that the error object +** is a string, as it was either generated by Lua or by 'msghandler'. +*/ static int report (lua_State *L, int status) { - if (status != LUA_OK && !lua_isnil(L, -1)) { + if (status != LUA_OK) { const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; l_message(progname, msg); - lua_pop(L, 1); - /* force a complete garbage collection in case of errors */ - lua_gc(L, LUA_GCCOLLECT, 0); + lua_pop(L, 1); /* remove message */ } return status; } -/* the next function is called unprotected, so it must avoid errors */ -static void finalreport (lua_State *L, int status) { - if (status != LUA_OK) { - const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) - : NULL; - if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); - lua_pop(L, 1); - } -} - - -static int traceback (lua_State *L) { +/* +** Message handler used to run all chunks +*/ +static int msghandler (lua_State *L) { const char *msg = lua_tostring(L, 1); - if (msg) - luaL_traceback(L, L, msg, 1); - else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */ - if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */ - lua_pushliteral(L, "(no error message)"); + if (msg == NULL) { /* is error object not a string? */ + if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */ + lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */ + return 1; /* that is the message */ + else + msg = lua_pushfstring(L, "(error object is a %s value)", + luaL_typename(L, 1)); } - return 1; + luaL_traceback(L, L, msg, 1); /* append a standard traceback */ + return 1; /* return the traceback */ } +/* +** Interface to 'lua_pcall', which sets appropriate message function +** and C-signal handler. Used to run all chunks. +*/ static int docall (lua_State *L, int narg, int nres) { int status; int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, traceback); /* push traceback function */ - lua_insert(L, base); /* put it under chunk and args */ + lua_pushcfunction(L, msghandler); /* push message handler */ + lua_insert(L, base); /* put it under function and args */ globalL = L; /* to be available to 'laction' */ - signal(SIGINT, laction); + signal(SIGINT, laction); /* set C-signal handler */ status = lua_pcall(L, narg, nres, base); - signal(SIGINT, SIG_DFL); - lua_remove(L, base); /* remove traceback function */ + signal(SIGINT, SIG_DFL); /* reset C-signal handler */ + lua_remove(L, base); /* remove message handler from the stack */ return status; } static void print_version (void) { - luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); - luai_writeline(); + lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); + lua_writeline(); } -static int getargs (lua_State *L, char **argv, int n) { - int narg; - int i; - int argc = 0; - while (argv[argc]) argc++; /* count total number of arguments */ - narg = argc - (n + 1); /* number of arguments to the script */ - luaL_checkstack(L, narg + 3, "too many arguments to script"); - for (i=n+1; i < argc; i++) +/* +** Create the 'arg' table, which stores all arguments from the +** command line ('argv'). It should be aligned so that, at index 0, +** it has 'argv[script]', which is the script name. The arguments +** to the script (everything after 'script') go to positive indices; +** other arguments (before the script name) go to negative indices. +** If there is no script name, assume interpreter's name as base. +*/ +static void createargtable (lua_State *L, char **argv, int argc, int script) { + int i, narg; + if (script == argc) script = 0; /* no script name? */ + narg = argc - (script + 1); /* number of positive indices */ + lua_createtable(L, narg, script + 1); + for (i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); - lua_createtable(L, narg, n + 1); - for (i=0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - n); + lua_rawseti(L, -2, i - script); } - return narg; + lua_setglobal(L, "arg"); +} + + +static int dochunk (lua_State *L, int status) { + if (status == LUA_OK) status = docall(L, 0, 0); + return report(L, status); } static int dofile (lua_State *L, const char *name) { - int status = luaL_loadfile(L, name); - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); + return dochunk(L, luaL_loadfile(L, name)); } static int dostring (lua_State *L, const char *s, const char *name) { - int status = luaL_loadbuffer(L, s, strlen(s), name); - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); + return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); } +/* +** Calls 'require(name)' and stores the result in a global variable +** with the given name. +*/ static int dolibrary (lua_State *L, const char *name) { int status; lua_getglobal(L, "require"); @@ -232,6 +267,9 @@ static int dolibrary (lua_State *L, const char *name) { } +/* +** Returns the string to be used as a prompt by the interpreter. +*/ static const char *get_prompt (lua_State *L, int firstline) { const char *p; lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); @@ -244,6 +282,12 @@ static const char *get_prompt (lua_State *L, int firstline) { #define EOFMARK "" #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) + +/* +** Check whether 'status' signals a syntax error and the error +** message at the top of the stack ends with the above mark for +** incomplete statements. +*/ static int incomplete (lua_State *L, int status) { if (status == LUA_ERRSYNTAX) { size_t lmsg; @@ -257,20 +301,23 @@ static int incomplete (lua_State *L, int status) { } +/* +** Prompt the user, read a line, and push it into the Lua stack. +*/ static int pushline (lua_State *L, int firstline) { char buffer[LUA_MAXINPUT]; char *b = buffer; size_t l; const char *prmt = get_prompt(L, firstline); int readstatus = lua_readline(L, b, prmt); - lua_pop(L, 1); /* remove result from 'get_prompt' */ if (readstatus == 0) - return 0; /* no input */ + return 0; /* no input (prompt will be popped by caller) */ + lua_pop(L, 1); /* remove prompt */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ b[l-1] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* first line starts with `=' ? */ - lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ + if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ + lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ else lua_pushstring(L, b); lua_freeline(L, b); @@ -278,142 +325,206 @@ static int pushline (lua_State *L, int firstline) { } +/* +** Try to compile line on the stack as 'return '; on return, stack +** has either compiled chunk or original line (if compilation failed). +*/ +static int addreturn (lua_State *L) { + int status; + size_t len; const char *line; + lua_pushliteral(L, "return "); + lua_pushvalue(L, -2); /* duplicate line */ + lua_concat(L, 2); /* new line is "return ..." */ + line = lua_tolstring(L, -1, &len); + if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) + lua_remove(L, -3); /* remove original line */ + else + lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */ + return status; +} + + +/* +** Read multiple lines until a complete Lua statement +*/ +static int multiline (lua_State *L) { + for (;;) { /* repeat until gets a complete statement */ + size_t len; + const char *line = lua_tolstring(L, 1, &len); /* get what it has */ + int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ + if (!incomplete(L, status) || !pushline(L, 0)) + return status; /* cannot or should not try to add continuation line */ + lua_pushliteral(L, "\n"); /* add newline... */ + lua_insert(L, -2); /* ...between the two lines */ + lua_concat(L, 3); /* join them */ + } +} + + +/* +** Read a line and try to load (compile) it first as an expression (by +** adding "return " in front of it) and second as a statement. Return +** the final status of load/call with the resulting function (if any) +** in the top of the stack. +*/ static int loadline (lua_State *L) { int status; lua_settop(L, 0); if (!pushline(L, 1)) return -1; /* no input */ - for (;;) { /* repeat until gets a complete line */ - size_t l; - const char *line = lua_tolstring(L, 1, &l); - status = luaL_loadbuffer(L, line, l, "=stdin"); - if (!incomplete(L, status)) break; /* cannot try to add lines? */ - if (!pushline(L, 0)) /* no more input? */ - return -1; - lua_pushliteral(L, "\n"); /* add a new line... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } - lua_saveline(L, 1); - lua_remove(L, 1); /* remove line */ + if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ + status = multiline(L); /* try as command, maybe with continuation lines */ + lua_saveline(L, 1); /* keep history */ + lua_remove(L, 1); /* remove line from the stack */ + lua_assert(lua_gettop(L) == 1); return status; } -static void dotty (lua_State *L) { +/* +** Prints (calling the Lua 'print' function) any values on the stack +*/ +static void l_print (lua_State *L) { + int n = lua_gettop(L); + if (n > 0) { /* any result to be printed? */ + luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); + lua_getglobal(L, "print"); + lua_insert(L, 1); + if (lua_pcall(L, n, 0, 0) != LUA_OK) + l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)", + lua_tostring(L, -1))); + } +} + + +/* +** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and +** print any results. +*/ +static void doREPL (lua_State *L) { int status; const char *oldprogname = progname; - progname = NULL; + progname = NULL; /* no 'progname' on errors in interactive mode */ while ((status = loadline(L)) != -1) { - if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); - report(L, status); - if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ - luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) - l_message(progname, lua_pushfstring(L, - "error calling " LUA_QL("print") " (%s)", - lua_tostring(L, -1))); - } + if (status == LUA_OK) + status = docall(L, 0, LUA_MULTRET); + if (status == LUA_OK) l_print(L); + else report(L, status); } lua_settop(L, 0); /* clear stack */ - luai_writeline(); + lua_writeline(); progname = oldprogname; } -static int handle_script (lua_State *L, char **argv, int n) { +/* +** Push on the stack the contents of table 'arg' from 1 to #arg +*/ +static int pushargs (lua_State *L) { + int i, n; + if (lua_getglobal(L, "arg") != LUA_TTABLE) + luaL_error(L, "'arg' is not a table"); + n = (int)luaL_len(L, -1); + luaL_checkstack(L, n + 3, "too many arguments to script"); + for (i = 1; i <= n; i++) + lua_rawgeti(L, -i, i); + lua_remove(L, -i); /* remove table from the stack */ + return n; +} + + +static int handle_script (lua_State *L, char **argv) { int status; - const char *fname; - int narg = getargs(L, argv, n); /* collect arguments */ - lua_setglobal(L, "arg"); - fname = argv[n]; - if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) + const char *fname = argv[0]; + if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) fname = NULL; /* stdin */ status = luaL_loadfile(L, fname); - lua_insert(L, -(narg+1)); - if (status == LUA_OK) - status = docall(L, narg, LUA_MULTRET); - else - lua_pop(L, narg); + if (status == LUA_OK) { + int n = pushargs(L); /* push arguments to script */ + status = docall(L, n, LUA_MULTRET); + } return report(L, status); } -/* check that argument has no extra characters at the end */ -#define noextrachars(x) {if ((x)[2] != '\0') return -1;} +/* bits of various argument indicators in 'args' */ +#define has_error 1 /* bad option */ +#define has_i 2 /* -i */ +#define has_v 4 /* -v */ +#define has_e 8 /* -e */ +#define has_E 16 /* -E */ -/* indices of various argument indicators in array args */ -#define has_i 0 /* -i */ -#define has_v 1 /* -v */ -#define has_e 2 /* -e */ -#define has_E 3 /* -E */ - -#define num_has 4 /* number of 'has_*' */ - - -static int collectargs (char **argv, int *args) { +/* +** Traverses all arguments from 'argv', returning a mask with those +** needed before running any Lua code (or an error code if it finds +** any invalid argument). 'first' returns the first not-handled argument +** (either the script name or a bad argument in case of error). +*/ +static int collectargs (char **argv, int *first) { + int args = 0; int i; for (i = 1; argv[i] != NULL; i++) { + *first = i; if (argv[i][0] != '-') /* not an option? */ - return i; - switch (argv[i][1]) { /* option */ - case '-': - noextrachars(argv[i]); - return (argv[i+1] != NULL ? i+1 : 0); - case '\0': - return i; + return args; /* stop handling options */ + switch (argv[i][1]) { /* else check option */ + case '-': /* '--' */ + if (argv[i][2] != '\0') /* extra characters after '--'? */ + return has_error; /* invalid option */ + *first = i + 1; + return args; + case '\0': /* '-' */ + return args; /* script "name" is '-' */ case 'E': - args[has_E] = 1; + if (argv[i][2] != '\0') /* extra characters after 1st? */ + return has_error; /* invalid option */ + args |= has_E; break; case 'i': - noextrachars(argv[i]); - args[has_i] = 1; /* go through */ + args |= has_i; /* goes through (-i implies -v) */ case 'v': - noextrachars(argv[i]); - args[has_v] = 1; + if (argv[i][2] != '\0') /* extra characters after 1st? */ + return has_error; /* invalid option */ + args |= has_v; break; case 'e': - args[has_e] = 1; /* go through */ + args |= has_e; /* go through */ case 'l': /* both options need an argument */ if (argv[i][2] == '\0') { /* no concatenated argument? */ i++; /* try next 'argv' */ if (argv[i] == NULL || argv[i][0] == '-') - return -(i - 1); /* no next argument or it is another option */ + return has_error; /* no next argument or it is another option */ } break; - default: /* invalid option; return its index... */ - return -i; /* ...as a negative value */ + default: /* invalid option */ + return has_error; } } - return 0; + *first = i; /* no script name */ + return args; } +/* +** Processes options 'e' and 'l', which involve running Lua code. +** Returns 0 if some code raises an error. +*/ static int runargs (lua_State *L, char **argv, int n) { int i; for (i = 1; i < n; i++) { - lua_assert(argv[i][0] == '-'); - switch (argv[i][1]) { /* option */ - case 'e': { - const char *chunk = argv[i] + 2; - if (*chunk == '\0') chunk = argv[++i]; - lua_assert(chunk != NULL); - if (dostring(L, chunk, "=(command line)") != LUA_OK) - return 0; - break; - } - case 'l': { - const char *filename = argv[i] + 2; - if (*filename == '\0') filename = argv[++i]; - lua_assert(filename != NULL); - if (dolibrary(L, filename) != LUA_OK) - return 0; /* stop if file fails */ - break; - } - default: break; + int status; + int option = argv[i][1]; + lua_assert(argv[i][0] == '-'); /* already checked */ + if (option == 'e' || option == 'l') { + const char *extra = argv[i] + 2; /* both options need an argument */ + if (*extra == '\0') extra = argv[++i]; + lua_assert(extra != NULL); + if (option == 'e') + status = dostring(L, extra, "=(command line)"); + else + status = dolibrary(L, extra); + if (status != LUA_OK) return 0; } } return 1; @@ -421,10 +532,10 @@ static int runargs (lua_State *L, char **argv, int n) { static int handle_luainit (lua_State *L) { - const char *name = "=" LUA_INITVERSION; + const char *name = "=" LUA_INITVARVERSION; const char *init = getenv(name + 1); if (init == NULL) { - name = "=" LUA_INIT; + name = "=" LUA_INIT_VAR; init = getenv(name + 1); /* try alternative name */ } if (init == NULL) return LUA_OK; @@ -435,40 +546,44 @@ static int handle_luainit (lua_State *L) { } +/* +** Main body of stand-alone interpreter (to be called in protected mode). +** Reads the options and handles them all. +*/ static int pmain (lua_State *L) { int argc = (int)lua_tointeger(L, 1); char **argv = (char **)lua_touserdata(L, 2); int script; - int args[num_has]; - args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0; + int args = collectargs(argv, &script); + luaL_checkversion(L); /* check that interpreter has correct version */ if (argv[0] && argv[0][0]) progname = argv[0]; - script = collectargs(argv, args); - if (script < 0) { /* invalid arg? */ - print_usage(argv[-script]); + if (args == has_error) { /* bad arg? */ + print_usage(argv[script]); /* 'script' has index of bad arg. */ return 0; } - if (args[has_v]) print_version(); - if (args[has_E]) { /* option '-E'? */ + if (args & has_v) /* option '-v'? */ + print_version(); + if (args & has_E) { /* option '-E'? */ lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); } - /* open standard libraries */ - luaL_checkversion(L); - lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ - luaL_openlibs(L); /* open libraries */ - lua_gc(L, LUA_GCRESTART, 0); - if (!args[has_E] && handle_luainit(L) != LUA_OK) - return 0; /* error running LUA_INIT */ - /* execute arguments -e and -l */ - if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; - /* execute main script (if there is one) */ - if (script && handle_script(L, argv, script) != LUA_OK) return 0; - if (args[has_i]) /* -i option? */ - dotty(L); - else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */ - if (lua_stdin_is_tty()) { + luaL_openlibs(L); /* open standard libraries */ + createargtable(L, argv, argc, script); /* create table 'arg' */ + if (!(args & has_E)) { /* no option '-E'? */ + if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ + return 0; /* error running LUA_INIT */ + } + if (!runargs(L, argv, script)) /* execute arguments -e and -l */ + return 0; /* something failed */ + if (script < argc && /* execute main script (if there is one) */ + handle_script(L, argv + script) != LUA_OK) + return 0; + if (args & has_i) /* -i option? */ + doREPL(L); /* do read-eval-print loop */ + else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ + if (lua_stdin_is_tty()) { /* running in interactive mode? */ print_version(); - dotty(L); + doREPL(L); /* do read-eval-print loop */ } else dofile(L, NULL); /* executes stdin as a file */ } @@ -484,13 +599,12 @@ int main (int argc, char **argv) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } - /* call 'pmain' in protected mode */ - lua_pushcfunction(L, &pmain); + lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ - status = lua_pcall(L, 2, 1, 0); + status = lua_pcall(L, 2, 1, 0); /* do the call */ result = lua_toboolean(L, -1); /* get result */ - finalreport(L, status); + report(L, status); lua_close(L); return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 1fceb4a5..09a4ccaf 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.285.1.2 2013/11/11 12:09:16 roberto Exp $ +** $Id: lua.h,v 1.325 2014/12/26 17:24:27 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -17,18 +17,18 @@ #define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "2" -#define LUA_VERSION_NUM 502 -#define LUA_VERSION_RELEASE "3" +#define LUA_VERSION_MINOR "3" +#define LUA_VERSION_NUM 503 +#define LUA_VERSION_RELEASE "0" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2013 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" /* mark for precompiled code ('Lua') */ -#define LUA_SIGNATURE "\033Lua" +#define LUA_SIGNATURE "\x1bLua" /* option for multiple returns in 'lua_pcall' and 'lua_call' */ #define LUA_MULTRET (-1) @@ -53,22 +53,6 @@ typedef struct lua_State lua_State; -typedef int (*lua_CFunction) (lua_State *L); - - -/* -** functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); - - -/* -** prototype for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - /* ** basic types @@ -109,6 +93,34 @@ typedef LUA_INTEGER lua_Integer; /* unsigned integer type */ typedef LUA_UNSIGNED lua_Unsigned; +/* type for continuation-function contexts */ +typedef LUA_KCONTEXT lua_KContext; + + +/* +** Type for C functions registered with Lua +*/ +typedef int (*lua_CFunction) (lua_State *L); + +/* +** Type for continuation functions +*/ +typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); + + +/* +** Type for functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); + + +/* +** Type for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + /* @@ -145,11 +157,9 @@ LUA_API int (lua_absindex) (lua_State *L, int idx); LUA_API int (lua_gettop) (lua_State *L); LUA_API void (lua_settop) (lua_State *L, int idx); LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_remove) (lua_State *L, int idx); -LUA_API void (lua_insert) (lua_State *L, int idx); -LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API void (lua_rotate) (lua_State *L, int idx, int n); LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); -LUA_API int (lua_checkstack) (lua_State *L, int sz); +LUA_API int (lua_checkstack) (lua_State *L, int n); LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); @@ -161,13 +171,13 @@ LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); LUA_API int (lua_isnumber) (lua_State *L, int idx); LUA_API int (lua_isstring) (lua_State *L, int idx); LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isinteger) (lua_State *L, int idx); LUA_API int (lua_isuserdata) (lua_State *L, int idx); LUA_API int (lua_type) (lua_State *L, int idx); LUA_API const char *(lua_typename) (lua_State *L, int tp); LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); -LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum); LUA_API int (lua_toboolean) (lua_State *L, int idx); LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); LUA_API size_t (lua_rawlen) (lua_State *L, int idx); @@ -181,13 +191,20 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx); ** Comparison and arithmetic functions */ -#define LUA_OPADD 0 /* ORDER TM */ +#define LUA_OPADD 0 /* ORDER TM, ORDER OP */ #define LUA_OPSUB 1 #define LUA_OPMUL 2 -#define LUA_OPDIV 3 -#define LUA_OPMOD 4 -#define LUA_OPPOW 5 -#define LUA_OPUNM 6 +#define LUA_OPMOD 3 +#define LUA_OPPOW 4 +#define LUA_OPDIV 5 +#define LUA_OPIDIV 6 +#define LUA_OPBAND 7 +#define LUA_OPBOR 8 +#define LUA_OPBXOR 9 +#define LUA_OPSHL 10 +#define LUA_OPSHR 11 +#define LUA_OPUNM 12 +#define LUA_OPBNOT 13 LUA_API void (lua_arith) (lua_State *L, int op); @@ -205,8 +222,7 @@ LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); LUA_API void (lua_pushnil) (lua_State *L); LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n); -LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, va_list argp); @@ -220,26 +236,29 @@ LUA_API int (lua_pushthread) (lua_State *L); /* ** get functions (Lua -> stack) */ -LUA_API void (lua_getglobal) (lua_State *L, const char *var); -LUA_API void (lua_gettable) (lua_State *L, int idx); -LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawget) (lua_State *L, int idx); -LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); -LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p); +LUA_API int (lua_getglobal) (lua_State *L, const char *name); +LUA_API int (lua_gettable) (lua_State *L, int idx); +LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawget) (lua_State *L, int idx); +LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); +LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); + LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API void (lua_getuservalue) (lua_State *L, int idx); +LUA_API int (lua_getuservalue) (lua_State *L, int idx); /* ** set functions (stack -> Lua) */ -LUA_API void (lua_setglobal) (lua_State *L, const char *var); +LUA_API void (lua_setglobal) (lua_State *L, const char *name); LUA_API void (lua_settable) (lua_State *L, int idx); LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); LUA_API int (lua_setmetatable) (lua_State *L, int objindex); LUA_API void (lua_setuservalue) (lua_State *L, int idx); @@ -248,32 +267,31 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx); /* ** 'load' and 'call' functions (load and run Lua code) */ -LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx, - lua_CFunction k); +LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, + lua_KContext ctx, lua_KFunction k); #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) -LUA_API int (lua_getctx) (lua_State *L, int *ctx); - LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, - int ctx, lua_CFunction k); + lua_KContext ctx, lua_KFunction k); #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname, - const char *mode); + const char *chunkname, const char *mode); -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); -LUA_API void (lua_clonefunction) (lua_State *L, const void *eL); /* ** coroutine functions */ -LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, - lua_CFunction k); +LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, + lua_KFunction k); +LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); +LUA_API int (lua_status) (lua_State *L); +LUA_API int (lua_isyieldable) (lua_State *L); + #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) -LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); -LUA_API int (lua_status) (lua_State *L); + /* ** garbage-collection function and options @@ -287,10 +305,7 @@ LUA_API int (lua_status) (lua_State *L); #define LUA_GCSTEP 5 #define LUA_GCSETPAUSE 6 #define LUA_GCSETSTEPMUL 7 -#define LUA_GCSETMAJORINC 8 #define LUA_GCISRUNNING 9 -#define LUA_GCGEN 10 -#define LUA_GCINC 11 LUA_API int (lua_gc) (lua_State *L, int what, int data); @@ -306,20 +321,23 @@ LUA_API int (lua_next) (lua_State *L, int idx); LUA_API void (lua_concat) (lua_State *L, int n); LUA_API void (lua_len) (lua_State *L, int idx); +LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); + LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); /* -** =============================================================== +** {============================================================== ** some useful macros ** =============================================================== */ -#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) -#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) -#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL) +#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) + +#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) +#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) #define lua_pop(L,n) lua_settop(L, -(n)-1) @@ -347,6 +365,28 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) +#define lua_insert(L,idx) lua_rotate(L, (idx), 1) + +#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) + +#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) + +/* }============================================================== */ + + +/* +** {============================================================== +** compatibility macros for unsigned conversions +** =============================================================== +*/ +#if defined(LUA_COMPAT_APIINTCASTS) + +#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) +#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) +#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) + +#endif +/* }============================================================== */ /* ** {====================================================================== @@ -391,7 +431,7 @@ LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, int fidx2, int n2); -LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); +LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); LUA_API lua_Hook (lua_gethook) (lua_State *L); LUA_API int (lua_gethookmask) (lua_State *L); LUA_API int (lua_gethookcount) (lua_State *L); @@ -419,7 +459,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2013 Lua.org, PUC-Rio. +* Copyright (C) 1994-2015 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index 20594048..a8d2a31b 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -1,17 +1,20 @@ /* -** $Id: luac.c,v 1.69 2011/11/29 17:46:33 lhf Exp $ -** Lua compiler (saves bytecodes to files; also list bytecodes) +** $Id: luac.c,v 1.71 2014/11/26 12:08:59 lhf Exp $ +** Lua compiler (saves bytecodes to files; also lists bytecodes) ** See Copyright Notice in lua.h */ +#define luac_c +#define LUA_CORE + +#include "lprefix.h" + +#include #include #include #include #include -#define luac_c -#define LUA_CORE - #include "lua.h" #include "lauxlib.h" @@ -146,9 +149,9 @@ static const Proto* combine(lua_State* L, int n) for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sp->sizeupvalues>0) f->p[i]->sp->upvalues[0].instack=0; + if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; } - f->sp->sizelineinfo=0; + f->sizelineinfo=0; return f; } } @@ -203,7 +206,7 @@ int main(int argc, char* argv[]) } /* -** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $ +** $Id: print.c,v 1.74 2014/07/21 01:41:45 lhf Exp $ ** print bytecodes ** See Copyright Notice in lua.h */ @@ -223,7 +226,7 @@ int main(int argc, char* argv[]) static void PrintString(const TString* ts) { const char* s=getstr(ts); - size_t i,n=ts->tsv.len; + size_t i,n=ts->len; printf("%c",'"'); for (i=0; ik[i]; - switch (ttypenv(o)) + switch (ttype(o)) { case LUA_TNIL: printf("nil"); @@ -259,11 +262,14 @@ static void PrintConstant(const Proto* f, int i) case LUA_TBOOLEAN: printf(bvalue(o) ? "true" : "false"); break; - case LUA_TNUMBER: - printf(LUA_NUMBER_FMT,nvalue(o)); + case LUA_TNUMFLT: + printf(LUA_NUMBER_FMT,fltvalue(o)); break; - case LUA_TSTRING: - PrintString(rawtsvalue(o)); + case LUA_TNUMINT: + printf(LUA_INTEGER_FMT,ivalue(o)); + break; + case LUA_TSHRSTR: case LUA_TLNGSTR: + PrintString(tsvalue(o)); break; default: /* cannot happen */ printf("? type=%d",ttype(o)); @@ -271,14 +277,13 @@ static void PrintConstant(const Proto* f, int i) } } -#define UPVALNAME(x) ((f->sp->upvalues[x].name) ? getstr(f->sp->upvalues[x].name) : "-") +#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") #define MYK(x) (-1-(x)) static void PrintCode(const Proto* f) { - const SharedProto *sp = f->sp; - const Instruction* code=sp->code; - int pc,n=sp->sizecode; + const Instruction* code=f->code; + int pc,n=f->sizecode; for (pc=0; pcsource ? getstr(f->source) : "=?"; if (*s=='@' || *s=='=') @@ -398,9 +409,8 @@ static void PrintHeader(const SharedProto* f) static void PrintDebug(const Proto* f) { - const SharedProto *sp = f->sp; int i,n; - n=sp->sizek; + n=f->sizek; printf("constants (%d) for %p:\n",n,VOID(f)); for (i=0; isizelocvars; + n=f->sizelocvars; printf("locals (%d) for %p:\n",n,VOID(f)); for (i=0; ilocvars[i].varname),sp->locvars[i].startpc+1,sp->locvars[i].endpc+1); + i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); } - n=sp->sizeupvalues; + n=f->sizeupvalues; printf("upvalues (%d) for %p:\n",n,VOID(f)); for (i=0; iupvalues[i].instack,sp->upvalues[i].idx); + i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx); } } static void PrintFunction(const Proto* f, int full) { - int i,n=f->sp->sizep; - PrintHeader(f->sp); + int i,n=f->sizep; + PrintHeader(f); PrintCode(f); if (full) PrintDebug(f); for (i=0; ip[i],full); diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index 18be9a9e..fd28d21a 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -1,111 +1,187 @@ /* -** $Id: luaconf.h,v 1.176.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: luaconf.h,v 1.238 2014/12/29 13:27:55 roberto Exp $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ -#ifndef lconfig_h -#define lconfig_h +#ifndef luaconf_h +#define luaconf_h #include #include /* -** ================================================================== +** =================================================================== ** Search for "@@" to find all configurable definitions. ** =================================================================== */ /* -@@ LUA_ANSI controls the use of non-ansi features. -** CHANGE it (define it) if you want Lua to avoid the use of any -** non-ansi feature or library. +** {==================================================================== +** System Configuration: macros to adapt (if needed) Lua to some +** particular platform, for instance compiling it with 32-bit numbers or +** restricting it to C89. +** ===================================================================== */ -#if !defined(LUA_ANSI) && defined(__STRICT_ANSI__) -#define LUA_ANSI + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You +** can also define LUA_32BITS in the make file, but changing here you +** ensure that all software connected to Lua will be compiled with the +** same configuration. +*/ +/* #define LUA_32BITS */ + + +/* +@@ LUA_USE_C89 controls the use of non-ISO-C89 features. +** Define it if you want Lua to avoid the use of a few C99 features +** or Windows-specific features on Windows. +*/ +/* #define LUA_USE_C89 */ + + +/* +** By default, Lua on Windows use (some) specific Windows features +*/ +#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ #endif -#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE) -#define LUA_WIN /* enable goodies for regular Windows platforms */ +#if defined(LUA_USE_WINDOWS) +#define LUA_DL_DLL /* enable support for DLL */ +#define LUA_USE_C89 /* broadly, Windows is C89 */ #endif -#if defined(LUA_WIN) -#define LUA_DL_DLL -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#endif - - #if defined(LUA_USE_LINUX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ #define LUA_USE_READLINE /* needs some extra libraries */ -#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#define LUA_USE_LONGLONG /* assume support for long long */ #endif + #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* does not need -ldl */ +#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ #define LUA_USE_READLINE /* needs an extra library: -lreadline */ -#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#define LUA_USE_LONGLONG /* assume support for long long */ +#endif + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS #endif /* -@@ LUA_USE_POSIX includes all functionality listed as X/Open System -@* Interfaces Extension (XSI). -** CHANGE it (define it) if your system is XSI compatible. +@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. */ -#if defined(LUA_USE_POSIX) -#define LUA_USE_MKSTEMP -#define LUA_USE_ISATTY -#define LUA_USE_POPEN -#define LUA_USE_ULONGJMP -#define LUA_USE_GMTIME_R +/* avoid undefined shifts */ +#if ((INT_MAX >> 15) >> 15) >= 1 +#define LUAI_BITSINT 32 +#else +/* 'int' always must have at least 16 bits */ +#define LUAI_BITSINT 16 #endif +/* +@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines the type for +** Lua integers. +@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines +** the type for Lua floats. +** Lua should work fine with any mix of these options (if supported +** by your C compiler). The usual configurations are 64-bit integers +** and 'double' (the default), 32-bit integers and 'float' (for +** restricted platforms), and 'long'/'double' (for C compilers not +** compliant with C99, which may not have support for 'long long'). +*/ + +#if defined(LUA_32BITS) /* { */ +/* +** 32-bit integers and 'float' +*/ +#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ +#define LUA_INT_INT +#else /* otherwise use 'long' */ +#define LUA_INT_LONG +#endif +#define LUA_REAL_FLOAT + +#elif defined(LUA_C89_NUMBERS) /* }{ */ +/* +** largest types available for C89 ('long' and 'double') +*/ +#define LUA_INT_LONG +#define LUA_REAL_DOUBLE + +#else /* }{ */ +/* +** default configuration for 64-bit Lua ('long long' and 'double') +*/ +#define LUA_INT_LONGLONG +#define LUA_REAL_DOUBLE + +#endif /* } */ + +/* }================================================================== */ + + + + +/* +** {================================================================== +** Configuration for Paths. +** =================================================================== +*/ /* @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for -@* Lua libraries. +** Lua libraries. @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for -@* C libraries. +** C libraries. ** CHANGE them if your machine has a non-conventional directory ** hierarchy or if you want to install your libraries in ** non-conventional directories. */ -#if defined(_WIN32) /* { */ +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#if defined(_WIN32) /* { */ /* ** In Windows, any exclamation mark ('!') in the path is replaced by the ** path of the directory of the executable file of the current process. */ #define LUA_LDIR "!\\lua\\" #define LUA_CDIR "!\\" +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" ".\\?.lua" + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + ".\\?.lua;" ".\\?\\init.lua" #define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll" + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" #else /* }{ */ -#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/" #define LUA_ROOT "/usr/local/" -#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR -#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" "./?.lua" + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + "./?.lua;" "./?/init.lua" #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" #endif /* } */ @@ -122,14 +198,14 @@ #define LUA_DIRSEP "/" #endif +/* }================================================================== */ + /* -@@ LUA_ENV is the name of the variable that holds the current -@@ environment, used to access global names. -** CHANGE it if you do not like this name. +** {================================================================== +** Marks for exported symbols in the C code +** =================================================================== */ -#define LUA_ENV "_ENV" - /* @@ LUA_API is a mark for all core API functions. @@ -162,10 +238,10 @@ /* @@ LUAI_FUNC is a mark for all extern functions that are not to be -@* exported to outside modules. +** exported to outside modules. @@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables -@* that are not to be exported to outside modules (LUAI_DDEF for -@* definitions and LUAI_DDEC for declarations). +** that are not to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). ** CHANGE them if you need to mark them in some special way. Elf/gcc ** (versions 3.2 and later) mark them as "hidden" to optimize access ** when Lua is compiled as a shared library. Not all elf targets support @@ -177,60 +253,14 @@ #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ defined(__ELF__) /* { */ #define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + #define LUAI_DDEC LUAI_FUNC #define LUAI_DDEF /* empty */ -#else /* }{ */ -#define LUAI_FUNC extern -#define LUAI_DDEC extern -#define LUAI_DDEF /* empty */ -#endif /* } */ - - - -/* -@@ LUA_QL describes how error messages quote program elements. -** CHANGE it if you want a different appearance. -*/ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - - -/* -@@ LUA_IDSIZE gives the maximum size for the description of the source -@* of a function in debug information. -** CHANGE it if you want a different size. -*/ -#define LUA_IDSIZE 60 - - -/* -@@ luai_writestring/luai_writeline define how 'print' prints its results. -** They are only used in libraries and the stand-alone program. (The #if -** avoids including 'stdio.h' everywhere.) -*/ -#if defined(LUA_LIB) || defined(lua_c) -#include -#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout)) -#endif - -/* -@@ luai_writestringerror defines how to print error messages. -** (A format string with one argument is enough for Lua...) -*/ -#define luai_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) - - -/* -@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is, -** strings that are internalized. (Cannot be smaller than reserved words -** or tags for metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#define LUAI_MAXSHORTLEN 40 - +/* }================================================================== */ /* @@ -240,11 +270,49 @@ */ /* -@@ LUA_COMPAT_ALL controls all compatibility options. +@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. +@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. ** You can define it to get all options, or change specific options ** to fit your specific needs. */ -#if defined(LUA_COMPAT_ALL) /* { */ +#if defined(LUA_COMPAT_5_2) /* { */ + +/* +@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated +** functions in the mathematical library. +*/ +#define LUA_COMPAT_MATHLIB + +/* +@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. +*/ +#define LUA_COMPAT_BITLIB + +/* +@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. +*/ +#define LUA_COMPAT_IPAIRS + +/* +@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for +** manipulating other integer types (lua_pushunsigned, lua_tounsigned, +** luaL_checkint, luaL_checklong, etc.) +*/ +#define LUA_COMPAT_APIINTCASTS + + +/* +@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a +@@ a float mark ('.0'). +** This macro is not on by default even in compatibility mode, +** because this is not really an incompatibility. +*/ +/* #define LUA_COMPAT_FLOATSTRING */ + +#endif /* } */ + + +#if defined(LUA_COMPAT_5_1) /* { */ /* @@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. @@ -310,47 +378,291 @@ /* -@@ LUAI_BITSINT defines the number of bits in an int. -** CHANGE here if Lua cannot automatically detect the number of bits of -** your machine. Probably you do not need to change this. +** {================================================================== +** Configuration for Numbers. +** Change these definitions if no predefined LUA_REAL_* / LUA_INT_* +** satisfy your needs. +** =================================================================== */ -/* avoid overflows in comparison */ -#if INT_MAX-20 < 32760 /* { */ -#define LUAI_BITSINT 16 -#elif INT_MAX > 2147483640L /* }{ */ -/* int has at least 32 bits */ -#define LUAI_BITSINT 32 -#else /* }{ */ -#error "you must define LUA_BITSINT with number of bits in an integer" -#endif /* } */ + +/* +@@ LUA_NUMBER is the floating-point type used by Lua. +** +@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' +@@ over a floating number. +** +@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. +@@ LUA_NUMBER_FMT is the format for writing floats. +@@ lua_number2str converts a float to a string. +** +@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. +** +@@ lua_str2number converts a decimal numeric string to a number. +*/ + +#if defined(LUA_REAL_FLOAT) /* { single float */ + +#define LUA_NUMBER float + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.7g" + +#define l_mathop(op) op##f + +#define lua_str2number(s,p) strtof((s), (p)) + + +#elif defined(LUA_REAL_LONGDOUBLE) /* }{ long double */ + +#define LUA_NUMBER long double + +#define LUAI_UACNUMBER long double + +#define LUA_NUMBER_FRMLEN "L" +#define LUA_NUMBER_FMT "%.19Lg" + +#define l_mathop(op) op##l + +#define lua_str2number(s,p) strtold((s), (p)) + +#elif defined(LUA_REAL_DOUBLE) /* }{ double */ + +#define LUA_NUMBER double + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.14g" + +#define l_mathop(op) op + +#define lua_str2number(s,p) strtod((s), (p)) + +#else /* }{ */ + +#error "numeric real type not defined" + +#endif /* } */ + + +#define l_floor(x) (l_mathop(floor)(x)) + +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) /* -@@ LUA_INT32 is an signed integer with exactly 32 bits. -@@ LUAI_UMEM is an unsigned integer big enough to count the total -@* memory used by Lua. -@@ LUAI_MEM is a signed integer big enough to count the total memory -@* used by Lua. -** CHANGE here if for some weird reason the default definitions are not -** good enough for your machine. Probably you do not need to change -** this. +@@ lua_numbertointeger converts a float number to an integer, or +** returns 0 if float is not within the range of a lua_Integer. +** (The range comparisons are tricky because of rounding. The tests +** here assume a two-complement representation, where MININTEGER always +** has an exact representation as a float; MAXINTEGER may not have one, +** and therefore its conversion to float may have an ill-defined value.) */ -#if LUAI_BITSINT >= 32 /* { */ -#define LUA_INT32 int -#define LUAI_UMEM size_t -#define LUAI_MEM ptrdiff_t +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + +/* +@@ The luai_num* macros define the primitive operations over numbers. +** They should work for any size of floating numbers. +*/ + +/* the following operations need the math library */ +#if defined(lobject_c) || defined(lvm_c) +#include + +/* floor division (defined as 'floor(a/b)') */ +#define luai_numidiv(L,a,b) ((void)L, l_mathop(floor)(luai_numdiv(L,a,b))) + +/* +** module: defined as 'a - floor(a/b)*b'; the previous definition gives +** NaN when 'b' is huge, but the result should be 'a'. 'fmod' gives the +** result of 'a - trunc(a/b)*b', and therefore must be corrected when +** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a +** non-integer negative result, which is equivalent to the test below +*/ +#define luai_nummod(L,a,b,m) \ + { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } + +/* exponentiation */ +#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) + +#endif + +/* these are quite standard operations */ +#if defined(LUA_CORE) +#define luai_numadd(L,a,b) ((a)+(b)) +#define luai_numsub(L,a,b) ((a)-(b)) +#define luai_nummul(L,a,b) ((a)*(b)) +#define luai_numdiv(L,a,b) ((a)/(b)) +#define luai_numunm(L,a) (-(a)) +#define luai_numeq(a,b) ((a)==(b)) +#define luai_numlt(a,b) ((a)<(b)) +#define luai_numle(a,b) ((a)<=(b)) +#define luai_numisnan(a) (!luai_numeq((a), (a))) +#endif + + +/* +@@ LUA_INTEGER is the integer type used by Lua. +** +@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. +** +@@ LUAI_UACINT is the result of an 'usual argument conversion' +@@ over a lUA_INTEGER. +@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. +@@ LUA_INTEGER_FMT is the format for writing integers. +@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. +@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. +@@ lua_integer2str converts an integer to a string. +*/ + + +/* The following definitions are good for most cases here */ + +#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" +#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n)) + +#define LUAI_UACINT LUA_INTEGER + +/* +** use LUAI_UACINT here to avoid problems with promotions (which +** can turn a comparison between unsigneds into a signed comparison) +*/ +#define LUA_UNSIGNED unsigned LUAI_UACINT + + +/* now the variable definitions */ + +#if defined(LUA_INT_INT) /* { int */ + +#define LUA_INTEGER int +#define LUA_INTEGER_FRMLEN "" + +#define LUA_MAXINTEGER INT_MAX +#define LUA_MININTEGER INT_MIN + +#elif defined(LUA_INT_LONG) /* }{ long */ + +#define LUA_INTEGER long +#define LUA_INTEGER_FRMLEN "l" + +#define LUA_MAXINTEGER LONG_MAX +#define LUA_MININTEGER LONG_MIN + +#elif defined(LUA_INT_LONGLONG) /* }{ long long */ + +#if defined(LLONG_MAX) /* { */ +/* use ISO C99 stuff */ + +#define LUA_INTEGER long long +#define LUA_INTEGER_FRMLEN "ll" + +#define LUA_MAXINTEGER LLONG_MAX +#define LUA_MININTEGER LLONG_MIN + +#elif defined(LUA_USE_WINDOWS) /* }{ */ +/* in Windows, can use specific Windows types */ + +#define LUA_INTEGER __int64 +#define LUA_INTEGER_FRMLEN "I64" + +#define LUA_MAXINTEGER _I64_MAX +#define LUA_MININTEGER _I64_MIN + #else /* }{ */ -/* 16-bit ints */ -#define LUA_INT32 long -#define LUAI_UMEM unsigned long -#define LUAI_MEM long + +#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ + or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" + #endif /* } */ +#else /* }{ */ + +#error "numeric integer type not defined" + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Dependencies with C99 +** =================================================================== +*/ + +/* +@@ lua_strx2number converts an hexadecimal numeric string to a number. +** In C99, 'strtod' does both conversions. Otherwise, you can +** leave 'lua_strx2number' undefined and Lua will provide its own +** implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_strx2number(s,p) lua_str2number(s,p) +#endif + + +/* +@@ LUA_USE_AFORMAT allows '%a'/'%A' specifiers in 'string.format' +** Enable it if the C function 'printf' supports these specifiers. +** (C99 demands it and Windows also supports it.) +*/ +#if !defined(LUA_USE_C89) || defined(LUA_USE_WINDOWS) +#define LUA_USE_AFORMAT +#endif + + +/* +** 'strtof' and 'opf' variants for math functions are not valid in +** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the +** availability of these variants. ('math.h' is already included in +** all files that use these macros.) +*/ +#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) +#undef l_mathop /* variants not available */ +#undef lua_str2number +#define l_mathop(op) (lua_Number)op /* no variant */ +#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) +#endif + + +/* +@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation +** functions. It must be a numerical type; Lua will use 'intptr_t' if +** available, otherwise it will use 'ptrdiff_t' (the nearest thing to +** 'intptr_t' in C89) +*/ +#define LUA_KCONTEXT ptrdiff_t + +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined (INTPTR_MAX) /* even in C99 this type is optional */ +#undef LUA_KCONTEXT +#define LUA_KCONTEXT intptr_t +#endif +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Macros that affect the API and must be stable (that is, must be the +** same when you compile Lua and when you compile code that links to +** Lua). You probably do not want/need to change them. +** ===================================================================== +*/ /* @@ LUAI_MAXSTACK limits the size of the Lua stack. ** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua to consume unlimited stack +** its only purpose is to stop Lua from consuming unlimited stack ** space (and to reserve some numbers for pseudo-indices). */ #if LUAI_BITSINT >= 32 @@ -363,179 +675,49 @@ #define LUAI_FIRSTPSEUDOIDX (-LUAI_MAXSTACK - 1000) +/* +@@ LUA_EXTRASPACE defines the size of a raw memory area associated with +** a Lua state with very fast access. +** CHANGE it if you need a different size. +*/ +#define LUA_EXTRASPACE (sizeof(void *)) + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@@ of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is, +** strings that are internalized. (Cannot be smaller than reserved words +** or tags for metamethods, as these strings must be internalized; +** #("function") = 8, #("__newindex") = 10.) +*/ +#define LUAI_MAXSHORTLEN 40 /* @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. ** CHANGE it if it uses too much C-stack space. */ -#define LUAL_BUFFERSIZE BUFSIZ - - - - -/* -** {================================================================== -@@ LUA_NUMBER is the type of numbers in Lua. -** CHANGE the following definitions only if you want to build Lua -** with a number type different from double. You may also need to -** change lua_number2int & lua_number2integer. -** =================================================================== -*/ - -#define LUA_NUMBER_DOUBLE -#define LUA_NUMBER double - -/* -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' -@* over a number. -*/ -#define LUAI_UACNUMBER double - - -/* -@@ LUA_NUMBER_SCAN is the format for reading numbers. -@@ LUA_NUMBER_FMT is the format for writing numbers. -@@ lua_number2str converts a number to a string. -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. -*/ -#define LUA_NUMBER_SCAN "%lf" -#define LUA_NUMBER_FMT "%.14g" -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ - - -/* -@@ l_mathop allows the addition of an 'l' or 'f' to all math operations -*/ -#define l_mathop(x) (x) - - -/* -@@ lua_str2number converts a decimal numeric string to a number. -@@ lua_strx2number converts an hexadecimal numeric string to a number. -** In C99, 'strtod' does both conversions. C89, however, has no function -** to convert floating hexadecimal strings to numbers. For these -** systems, you can leave 'lua_strx2number' undefined and Lua will -** provide its own implementation. -*/ -#define lua_str2number(s,p) strtod((s), (p)) - -#if defined(LUA_USE_STRTODHEX) -#define lua_strx2number(s,p) strtod((s), (p)) -#endif - - -/* -@@ The luai_num* macros define the primitive operations over numbers. -*/ - -/* the following operations need the math library */ -#if defined(lobject_c) || defined(lvm_c) -#include -#define luai_nummod(L,a,b) ((a) - l_mathop(floor)((a)/(b))*(b)) -#define luai_numpow(L,a,b) (l_mathop(pow)(a,b)) -#endif - -/* these are quite standard operations */ -#if defined(LUA_CORE) -#define luai_numadd(L,a,b) ((a)+(b)) -#define luai_numsub(L,a,b) ((a)-(b)) -#define luai_nummul(L,a,b) ((a)*(b)) -#define luai_numdiv(L,a,b) ((a)/(b)) -#define luai_numunm(L,a) (-(a)) -#define luai_numeq(a,b) ((a)==(b)) -#define luai_numlt(L,a,b) ((a)<(b)) -#define luai_numle(L,a,b) ((a)<=(b)) -#define luai_numisnan(L,a) (!luai_numeq((a), (a))) -#endif - - - -/* -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most -** machines, ptrdiff_t gives a good choice between int or long.) -*/ -#define LUA_INTEGER ptrdiff_t - -/* -@@ LUA_UNSIGNED is the integral type used by lua_pushunsigned/lua_tounsigned. -** It must have at least 32 bits. -*/ -#define LUA_UNSIGNED unsigned LUA_INT32 - - - -/* -** Some tricks with doubles -*/ - -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ -/* -** The next definitions activate some tricks to speed up the -** conversion from doubles to integer types, mainly to LUA_UNSIGNED. -** -@@ LUA_MSASMTRICK uses Microsoft assembler to avoid clashes with a -** DirectX idiosyncrasy. -** -@@ LUA_IEEE754TRICK uses a trick that should work on any machine -** using IEEE754 with a 32-bit integer type. -** -@@ LUA_IEEELL extends the trick to LUA_INTEGER; should only be -** defined when LUA_INTEGER is a 32-bit integer. -** -@@ LUA_IEEEENDIAN is the endianness of doubles in your machine -** (0 for little endian, 1 for big endian); if not defined, Lua will -** check it dynamically for LUA_IEEE754TRICK (but not for LUA_NANTRICK). -** -@@ LUA_NANTRICK controls the use of a trick to pack all types into -** a single double value, using NaN values to represent non-number -** values. The trick only works on 32-bit machines (ints and pointers -** are 32-bit values) with numbers represented as IEEE 754-2008 doubles -** with conventional endianess (12345678 or 87654321), in CPUs that do -** not produce signaling NaN values (all NaNs are quiet). -*/ - -/* Microsoft compiler on a Pentium (32 bit) ? */ -#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */ - -#define LUA_MSASMTRICK -#define LUA_IEEEENDIAN 0 -#define LUA_NANTRICK - - -/* pentium 32 bits? */ -#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEELL -#define LUA_IEEEENDIAN 0 -#define LUA_NANTRICK - -/* pentium 64 bits? */ -#elif defined(__x86_64) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEEENDIAN 0 - -#elif defined(__POWERPC__) || defined(__ppc__) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEEENDIAN 1 - -#else /* }{ */ - -/* assume IEEE754 and a 32-bit integer type */ -#define LUA_IEEE754TRICK - -#endif /* } */ - -#endif /* } */ +#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) /* }================================================================== */ +/* +@@ LUA_QL describes how error messages quote program elements. +** Lua does not use these macros anymore; they are here for +** compatibility only. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + /* =================================================================== */ @@ -547,5 +729,7 @@ + + #endif diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index 9be47e58..5165c0fb 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp $ ** Lua standard libraries ** See Copyright Notice in lua.h */ @@ -29,6 +29,9 @@ LUAMOD_API int (luaopen_os) (lua_State *L); #define LUA_STRLIBNAME "string" LUAMOD_API int (luaopen_string) (lua_State *L); +#define LUA_UTF8LIBNAME "utf8" +LUAMOD_API int (luaopen_utf8) (lua_State *L); + #define LUA_BITLIBNAME "bit32" LUAMOD_API int (luaopen_bit32) (lua_State *L); @@ -41,8 +44,6 @@ LUAMOD_API int (luaopen_debug) (lua_State *L); #define LUA_LOADLIBNAME "package" LUAMOD_API int (luaopen_package) (lua_State *L); -#define LUA_CACHELIB -LUAMOD_API int (luaopen_cache) (lua_State *L); /* open all previous libraries */ LUALIB_API void (luaL_openlibs) (lua_State *L); diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index a404594e..510f3258 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -1,14 +1,17 @@ /* -** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lundump.c,v 2.41 2014/11/02 19:19:04 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ -#include - #define lundump_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "ldebug.h" @@ -20,240 +23,255 @@ #include "lundump.h" #include "lzio.h" -typedef struct { - lua_State* L; - ZIO* Z; - Mbuffer* b; - const char* name; -} LoadState; - -static l_noret error(LoadState* S, const char* why) -{ - luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); - luaD_throw(S->L,LUA_ERRSYNTAX); -} - -#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) -#define LoadByte(S) (lu_byte)LoadChar(S) -#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) -#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) #if !defined(luai_verifycode) -#define luai_verifycode(L,b,f) /* empty */ +#define luai_verifycode(L,b,f) /* empty */ #endif -static void LoadBlock(LoadState* S, void* b, size_t size) -{ - if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); + +typedef struct { + lua_State *L; + ZIO *Z; + Mbuffer *b; + const char *name; +} LoadState; + + +static l_noret error(LoadState *S, const char *why) { + luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); + luaD_throw(S->L, LUA_ERRSYNTAX); } -static int LoadChar(LoadState* S) -{ - char x; - LoadVar(S,x); - return x; + +/* +** All high-level loads go through LoadVector; you can change it to +** adapt to the endianness of the input +*/ +#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) + +static void LoadBlock (LoadState *S, void *b, size_t size) { + if (luaZ_read(S->Z, b, size) != 0) + error(S, "truncated"); } -static int LoadInt(LoadState* S) -{ - int x; - LoadVar(S,x); - if (x<0) error(S,"corrupted"); - return x; + +#define LoadVar(S,x) LoadVector(S,&x,1) + + +static lu_byte LoadByte (LoadState *S) { + lu_byte x; + LoadVar(S, x); + return x; } -static lua_Number LoadNumber(LoadState* S) -{ - lua_Number x; - LoadVar(S,x); - return x; + +static int LoadInt (LoadState *S) { + int x; + LoadVar(S, x); + return x; } -static TString* LoadString(LoadState* S) -{ - size_t size; - LoadVar(S,size); - if (size==0) - return NULL; - else - { - char* s=luaZ_openspace(S->L,S->b,size); - LoadBlock(S,s,size*sizeof(char)); - return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ - } + +static lua_Number LoadNumber (LoadState *S) { + lua_Number x; + LoadVar(S, x); + return x; } -static void LoadCode(LoadState* S, SharedProto* f) -{ - int n=LoadInt(S); - f->code=luaM_newvector(S->L,n,Instruction); - f->sizecode=n; - LoadVector(S,f->code,n,sizeof(Instruction)); + +static lua_Integer LoadInteger (LoadState *S) { + lua_Integer x; + LoadVar(S, x); + return x; } -static void LoadFunction(LoadState* S, Proto* f); -static void LoadConstants(LoadState* S, Proto* f) -{ - int i,n; - n=LoadInt(S); - f->k=luaM_newvector(S->L,n,TValue); - f->sp->sizek=n; - for (i=0; ik[i]); - for (i=0; ik[i]; - int t=LoadChar(S); - switch (t) - { - case LUA_TNIL: - setnilvalue(o); - break; - case LUA_TBOOLEAN: - setbvalue(o,LoadChar(S)); - break; - case LUA_TNUMBER: - setnvalue(o,LoadNumber(S)); - break; - case LUA_TSTRING: - setsvalue2n(S->L,o,LoadString(S)); - break; - default: lua_assert(0); +static TString *LoadString (LoadState *S) { + size_t size = LoadByte(S); + if (size == 0xFF) + LoadVar(S, size); + if (size == 0) + return NULL; + else { + char *s = luaZ_openspace(S->L, S->b, --size); + LoadVector(S, s, size); + return luaS_newlstr(S->L, s, size); } - } - n=LoadInt(S); - f->p=luaM_newvector(S->L,n,Proto*); - f->sp->sizep=n; - for (i=0; ip[i]=NULL; - for (i=0; ip[i]=luaF_newproto(S->L, NULL); - LoadFunction(S,f->p[i]); - } } -static void LoadUpvalues(LoadState* S, SharedProto* f) -{ - int i,n; - n=LoadInt(S); - f->upvalues=luaM_newvector(S->L,n,Upvaldesc); - f->sizeupvalues=n; - for (i=0; iupvalues[i].name=NULL; - for (i=0; iupvalues[i].instack=LoadByte(S); - f->upvalues[i].idx=LoadByte(S); - } + +static void LoadCode (LoadState *S, Proto *f) { + int n = LoadInt(S); + f->code = luaM_newvector(S->L, n, Instruction); + f->sizecode = n; + LoadVector(S, f->code, n); } -static void LoadDebug(LoadState* S, SharedProto* f) -{ - int i,n; - f->source=LoadString(S); - n=LoadInt(S); - f->lineinfo=luaM_newvector(S->L,n,int); - f->sizelineinfo=n; - LoadVector(S,f->lineinfo,n,sizeof(int)); - n=LoadInt(S); - f->locvars=luaM_newvector(S->L,n,LocVar); - f->sizelocvars=n; - for (i=0; ilocvars[i].varname=NULL; - for (i=0; ilocvars[i].varname=LoadString(S); - f->locvars[i].startpc=LoadInt(S); - f->locvars[i].endpc=LoadInt(S); - } - n=LoadInt(S); - for (i=0; iupvalues[i].name=LoadString(S); + +static void LoadFunction(LoadState *S, Proto *f, TString *psource); + + +static void LoadConstants (LoadState *S, Proto *f) { + int i; + int n = LoadInt(S); + f->k = luaM_newvector(S->L, n, TValue); + f->sizek = n; + for (i = 0; i < n; i++) + setnilvalue(&f->k[i]); + for (i = 0; i < n; i++) { + TValue *o = &f->k[i]; + int t = LoadByte(S); + switch (t) { + case LUA_TNIL: + setnilvalue(o); + break; + case LUA_TBOOLEAN: + setbvalue(o, LoadByte(S)); + break; + case LUA_TNUMFLT: + setfltvalue(o, LoadNumber(S)); + break; + case LUA_TNUMINT: + setivalue(o, LoadInteger(S)); + break; + case LUA_TSHRSTR: + case LUA_TLNGSTR: + setsvalue2n(S->L, o, LoadString(S)); + break; + default: + lua_assert(0); + } + } } -static void LoadFunction(LoadState* S, Proto* f) -{ - SharedProto *sp = f->sp; - sp->linedefined=LoadInt(S); - sp->lastlinedefined=LoadInt(S); - sp->numparams=LoadByte(S); - sp->is_vararg=LoadByte(S); - sp->maxstacksize=LoadByte(S); - LoadCode(S,sp); - LoadConstants(S,f); - LoadUpvalues(S,sp); - LoadDebug(S,sp); + +static void LoadProtos (LoadState *S, Proto *f) { + int i; + int n = LoadInt(S); + f->p = luaM_newvector(S->L, n, Proto *); + f->sizep = n; + for (i = 0; i < n; i++) + f->p[i] = NULL; + for (i = 0; i < n; i++) { + f->p[i] = luaF_newproto(S->L); + LoadFunction(S, f->p[i], f->source); + } } -/* the code below must be consistent with the code in luaU_header */ -#define N0 LUAC_HEADERSIZE -#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) -#define N2 N1+2 -#define N3 N2+6 -static void LoadHeader(LoadState* S) -{ - lu_byte h[LUAC_HEADERSIZE]; - lu_byte s[LUAC_HEADERSIZE]; - luaU_header(h); - memcpy(s,h,sizeof(char)); /* first char already read */ - LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); - if (memcmp(h,s,N0)==0) return; - if (memcmp(h,s,N1)!=0) error(S,"not a"); - if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); - if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); +static void LoadUpvalues (LoadState *S, Proto *f) { + int i, n; + n = LoadInt(S); + f->upvalues = luaM_newvector(S->L, n, Upvaldesc); + f->sizeupvalues = n; + for (i = 0; i < n; i++) + f->upvalues[i].name = NULL; + for (i = 0; i < n; i++) { + f->upvalues[i].instack = LoadByte(S); + f->upvalues[i].idx = LoadByte(S); + } } + +static void LoadDebug (LoadState *S, Proto *f) { + int i, n; + n = LoadInt(S); + f->lineinfo = luaM_newvector(S->L, n, int); + f->sizelineinfo = n; + LoadVector(S, f->lineinfo, n); + n = LoadInt(S); + f->locvars = luaM_newvector(S->L, n, LocVar); + f->sizelocvars = n; + for (i = 0; i < n; i++) + f->locvars[i].varname = NULL; + for (i = 0; i < n; i++) { + f->locvars[i].varname = LoadString(S); + f->locvars[i].startpc = LoadInt(S); + f->locvars[i].endpc = LoadInt(S); + } + n = LoadInt(S); + for (i = 0; i < n; i++) + f->upvalues[i].name = LoadString(S); +} + + +static void LoadFunction (LoadState *S, Proto *f, TString *psource) { + f->source = LoadString(S); + if (f->source == NULL) /* no source in dump? */ + f->source = psource; /* reuse parent's source */ + f->linedefined = LoadInt(S); + f->lastlinedefined = LoadInt(S); + f->numparams = LoadByte(S); + f->is_vararg = LoadByte(S); + f->maxstacksize = LoadByte(S); + LoadCode(S, f); + LoadConstants(S, f); + LoadUpvalues(S, f); + LoadProtos(S, f); + LoadDebug(S, f); +} + + +static void checkliteral (LoadState *S, const char *s, const char *msg) { + char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ + size_t len = strlen(s); + LoadVector(S, buff, len); + if (memcmp(s, buff, len) != 0) + error(S, msg); +} + + +static void fchecksize (LoadState *S, size_t size, const char *tname) { + if (LoadByte(S) != size) + error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); +} + + +#define checksize(S,t) fchecksize(S,sizeof(t),#t) + +static void checkHeader (LoadState *S) { + checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ + if (LoadByte(S) != LUAC_VERSION) + error(S, "version mismatch in"); + if (LoadByte(S) != LUAC_FORMAT) + error(S, "format mismatch in"); + checkliteral(S, LUAC_DATA, "corrupted"); + checksize(S, int); + checksize(S, size_t); + checksize(S, Instruction); + checksize(S, lua_Integer); + checksize(S, lua_Number); + if (LoadInteger(S) != LUAC_INT) + error(S, "endianness mismatch in"); + if (LoadNumber(S) != LUAC_NUM) + error(S, "float format mismatch in"); +} + + /* ** load precompiled chunk */ -Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) -{ - LoadState S; - Closure* cl; - if (*name=='@' || *name=='=') - S.name=name+1; - else if (*name==LUA_SIGNATURE[0]) - S.name="binary string"; - else - S.name=name; - S.L=L; - S.Z=Z; - S.b=buff; - LoadHeader(&S); - cl=luaF_newLclosure(L,1); - setclLvalue(L,L->top,cl); incr_top(L); - cl->l.p=luaF_newproto(L, NULL); - LoadFunction(&S,cl->l.p); - if (cl->l.p->sp->sizeupvalues != 1) - { - Proto* p=cl->l.p; - cl=luaF_newLclosure(L,cl->l.p->sp->sizeupvalues); - cl->l.p=p; - setclLvalue(L,L->top-1,cl); - } - luai_verifycode(L,buff,cl->l.p); - return cl; +LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, + const char *name) { + LoadState S; + LClosure *cl; + if (*name == '@' || *name == '=') + S.name = name + 1; + else if (*name == LUA_SIGNATURE[0]) + S.name = "binary string"; + else + S.name = name; + S.L = L; + S.Z = Z; + S.b = buff; + checkHeader(&S); + cl = luaF_newLclosure(L, LoadByte(&S)); + setclLvalue(L, L->top, cl); + incr_top(L); + cl->p = luaF_newproto(L); + LoadFunction(&S, cl->p, NULL); + lua_assert(cl->nupvalues == cl->p->sizeupvalues); + luai_verifycode(L, buff, cl->p); + return cl; } -#define MYINT(s) (s[0]-'0') -#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) -#define FORMAT 0 /* this is the official format */ - -/* -* make header for precompiled chunks -* if you change the code below be sure to update LoadHeader and FORMAT above -* and LUAC_HEADERSIZE in lundump.h -*/ -void luaU_header (lu_byte* h) -{ - int x=1; - memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); - h+=sizeof(LUA_SIGNATURE)-sizeof(char); - *h++=cast_byte(VERSION); - *h++=cast_byte(FORMAT); - *h++=cast_byte(*(char*)&x); /* endianness */ - *h++=cast_byte(sizeof(int)); - *h++=cast_byte(sizeof(size_t)); - *h++=cast_byte(sizeof(Instruction)); - *h++=cast_byte(sizeof(lua_Number)); - *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ - memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); -} diff --git a/3rd/lua/lundump.h b/3rd/lua/lundump.h index 5255db25..ef43d512 100644 --- a/3rd/lua/lundump.h +++ b/3rd/lua/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -7,22 +7,27 @@ #ifndef lundump_h #define lundump_h +#include "llimits.h" #include "lobject.h" #include "lzio.h" -/* load one chunk; from lundump.c */ -LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); - -/* make header; from lundump.c */ -LUAI_FUNC void luaU_header (lu_byte* h); - -/* dump one chunk; from ldump.c */ -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); /* data to catch conversion errors */ -#define LUAC_TAIL "\x19\x93\r\n\x1a\n" +#define LUAC_DATA "\x19\x93\r\n\x1a\n" -/* size in bytes of header of binary files */ -#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) +#define LUAC_INT 0x5678 +#define LUAC_NUM cast_num(370.5) + +#define MYINT(s) (s[0]-'0') +#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) +#define LUAC_FORMAT 0 /* this is the official format */ + +/* load one chunk; from lundump.c */ +LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, + const char* name); + +/* dump one chunk; from ldump.c */ +LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, + void* data, int strip); #endif diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c new file mode 100644 index 00000000..be4f087f --- /dev/null +++ b/3rd/lua/lutf8lib.c @@ -0,0 +1,255 @@ +/* +** $Id: lutf8lib.c,v 1.13 2014/11/02 19:19:04 roberto Exp $ +** Standard library for UTF-8 manipulation +** See Copyright Notice in lua.h +*/ + +#define lutf8lib_c +#define LUA_LIB + +#include "lprefix.h" + + +#include +#include +#include + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + +#define MAXUNICODE 0x10FFFF + +#define iscont(p) ((*(p) & 0xC0) == 0x80) + + +/* from strlib */ +/* translate a relative string position: negative means back from end */ +static lua_Integer u_posrelat (lua_Integer pos, size_t len) { + if (pos >= 0) return pos; + else if (0u - (size_t)pos > len) return 0; + else return (lua_Integer)len + pos + 1; +} + + +/* +** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. +*/ +static const char *utf8_decode (const char *o, int *val) { + static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; + const unsigned char *s = (const unsigned char *)o; + unsigned int c = s[0]; + unsigned int res = 0; /* final result */ + if (c < 0x80) /* ascii? */ + res = c; + else { + int count = 0; /* to count number of continuation bytes */ + while (c & 0x40) { /* still have continuation bytes? */ + int cc = s[++count]; /* read next byte */ + if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ + return NULL; /* invalid byte sequence */ + res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ + c <<= 1; /* to test next bit */ + } + res |= ((c & 0x7F) << (count * 5)); /* add first byte */ + if (count > 3 || res > MAXUNICODE || res <= limits[count]) + return NULL; /* invalid byte sequence */ + s += count; /* skip continuation bytes read */ + } + if (val) *val = res; + return (const char *)s + 1; /* +1 to include first byte */ +} + + +/* +** utf8len(s [, i [, j]]) --> number of characters that start in the +** range [i,j], or nil + current position if 's' is not well formed in +** that interval +*/ +static int utflen (lua_State *L) { + int n = 0; + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); + lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); + luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, + "initial position out of string"); + luaL_argcheck(L, --posj < (lua_Integer)len, 3, + "final position out of string"); + while (posi <= posj) { + const char *s1 = utf8_decode(s + posi, NULL); + if (s1 == NULL) { /* conversion error? */ + lua_pushnil(L); /* return nil ... */ + lua_pushinteger(L, posi + 1); /* ... and current position */ + return 2; + } + posi = s1 - s; + n++; + } + lua_pushinteger(L, n); + return 1; +} + + +/* +** codepoint(s, [i, [j]]) -> returns codepoints for all characters +** that start in the range [i,j] +*/ +static int codepoint (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); + lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); + int n; + const char *se; + luaL_argcheck(L, posi >= 1, 2, "out of range"); + luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); + if (posi > pose) return 0; /* empty interval; return no values */ + n = (int)(pose - posi + 1); + if (posi + n <= pose) /* (lua_Integer -> int) overflow? */ + return luaL_error(L, "string slice too long"); + luaL_checkstack(L, n, "string slice too long"); + n = 0; + se = s + pose; + for (s += posi - 1; s < se;) { + int code; + s = utf8_decode(s, &code); + if (s == NULL) + return luaL_error(L, "invalid UTF-8 code"); + lua_pushinteger(L, code); + n++; + } + return n; +} + + +static void pushutfchar (lua_State *L, int arg) { + lua_Integer code = luaL_checkinteger(L, arg); + luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); + lua_pushfstring(L, "%U", (long)code); +} + + +/* +** utfchar(n1, n2, ...) -> char(n1)..char(n2)... +*/ +static int utfchar (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + if (n == 1) /* optimize common case of single char */ + pushutfchar(L, 1); + else { + int i; + luaL_Buffer b; + luaL_buffinit(L, &b); + for (i = 1; i <= n; i++) { + pushutfchar(L, i); + luaL_addvalue(&b); + } + luaL_pushresult(&b); + } + return 1; +} + + +/* +** offset(s, n, [i]) -> index where n-th character counting from +** position 'i' starts; 0 means character at 'i'. +*/ +static int byteoffset (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer n = luaL_checkinteger(L, 2); + lua_Integer posi = (n >= 0) ? 1 : len + 1; + posi = u_posrelat(luaL_optinteger(L, 3, posi), len); + luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, + "position out of range"); + if (n == 0) { + /* find beginning of current byte sequence */ + while (posi > 0 && iscont(s + posi)) posi--; + } + else { + if (iscont(s + posi)) + luaL_error(L, "initial position is a continuation byte"); + if (n < 0) { + while (n < 0 && posi > 0) { /* move back */ + do { /* find beginning of previous character */ + posi--; + } while (posi > 0 && iscont(s + posi)); + n++; + } + } + else { + n--; /* do not move for 1st character */ + while (n > 0 && posi < (lua_Integer)len) { + do { /* find beginning of next character */ + posi++; + } while (iscont(s + posi)); /* (cannot pass final '\0') */ + n--; + } + } + } + if (n == 0) /* did it find given character? */ + lua_pushinteger(L, posi + 1); + else /* no such character */ + lua_pushnil(L); + return 1; +} + + +static int iter_aux (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_Integer n = lua_tointeger(L, 2) - 1; + if (n < 0) /* first iteration? */ + n = 0; /* start from here */ + else if (n < (lua_Integer)len) { + n++; /* skip current byte */ + while (iscont(s + n)) n++; /* and its continuations */ + } + if (n >= (lua_Integer)len) + return 0; /* no more codepoints */ + else { + int code; + const char *next = utf8_decode(s + n, &code); + if (next == NULL || iscont(next)) + return luaL_error(L, "invalid UTF-8 code"); + lua_pushinteger(L, n + 1); + lua_pushinteger(L, code); + return 2; + } +} + + +static int iter_codes (lua_State *L) { + luaL_checkstring(L, 1); + lua_pushcfunction(L, iter_aux); + lua_pushvalue(L, 1); + lua_pushinteger(L, 0); + return 3; +} + + +/* pattern to match a single UTF-8 character */ +#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" + + +static struct luaL_Reg funcs[] = { + {"offset", byteoffset}, + {"codepoint", codepoint}, + {"char", utfchar}, + {"len", utflen}, + {"codes", iter_codes}, + /* placeholders */ + {"charpattern", NULL}, + {NULL, NULL} +}; + + +LUAMOD_API int luaopen_utf8 (lua_State *L) { + luaL_newlib(L, funcs); + lua_pushliteral(L, UTF8PATT); + lua_setfield(L, -2, "charpattern"); + return 1; +} + diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 43019d43..2ac1b02d 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1,17 +1,20 @@ /* -** $Id: lvm.c,v 2.155.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lvm.c,v 2.232 2014/12/27 20:30:38 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ +#define lvm_c +#define LUA_CORE +#include "lprefix.h" + + +#include #include #include #include -#define lvm_c -#define LUA_CORE - #include "lua.h" #include "ldebug.h" @@ -27,121 +30,176 @@ #include "lvm.h" +/* +** You can define LUA_FLOORN2I if you want to convert floats to integers +** by flooring them (instead of raising an error if they are not +** integral values) +*/ +#if !defined(LUA_FLOORN2I) +#define LUA_FLOORN2I 0 +#endif + /* limit for table tag-method chains (to avoid loops) */ -#define MAXTAGLOOP 100 +#define MAXTAGLOOP 2000 -const TValue *luaV_tonumber (const TValue *obj, TValue *n) { - lua_Number num; - if (ttisnumber(obj)) return obj; - if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) { - setnvalue(n, num); - return n; +/* +** Similar to 'tonumber', but does not attempt to convert strings and +** ensure correct precision (no extra bits). Used in comparisons. +*/ +static int tofloat (const TValue *obj, lua_Number *n) { + if (ttisfloat(obj)) *n = fltvalue(obj); + else if (ttisinteger(obj)) { + volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */ + *n = x; } - else - return NULL; + else { + *n = 0; /* to avoid warnings */ + return 0; + } + return 1; } -int luaV_tostring (lua_State *L, StkId obj) { - if (!ttisnumber(obj)) - return 0; - else { - char s[LUAI_MAXNUMBER2STR]; - lua_Number n = nvalue(obj); - int l = lua_number2str(s, n); - setsvalue2s(L, obj, luaS_newlstr(L, s, l)); +/* +** Try to convert a value to a float. The float case is already handled +** by the macro 'tonumber'. +*/ +int luaV_tonumber_ (const TValue *obj, lua_Number *n) { + TValue v; + if (ttisinteger(obj)) { + *n = cast_num(ivalue(obj)); return 1; } + else if (cvt2num(obj) && /* string convertible to number? */ + luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { + *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ + return 1; + } + else + return 0; /* conversion failed */ } -static void traceexec (lua_State *L) { - CallInfo *ci = L->ci; - lu_byte mask = L->hookmask; - int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0); - if (counthook) - resethookcount(L); /* reset count */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ - ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ - return; /* do not call hook again (VM yielded, so it did not move) */ +/* +** try to convert a value to an integer, rounding according to 'mode': +** mode == 0: accepts only integral values +** mode == 1: takes the floor of the number +** mode == 2: takes the ceil of the number +*/ +static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { + TValue v; + again: + if (ttisfloat(obj)) { + lua_Number n = fltvalue(obj); + lua_Number f = l_floor(n); + if (n != f) { /* not an integral value? */ + if (mode == 0) return 0; /* fails if mode demands integral value */ + else if (mode > 1) /* needs ceil? */ + f += 1; /* convert floor to ceil (remember: n != f) */ + } + return lua_numbertointeger(f, p); } - if (counthook) - luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ - if (mask & LUA_MASKLINE) { - Proto *p = ci_func(ci)->p; - int npc = pcRel(ci->u.l.savedpc, p); - int newline = getfuncline(p, npc); - if (npc == 0 || /* call linehook when enter a new function, */ - ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ - newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ - luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ + else if (ttisinteger(obj)) { + *p = ivalue(obj); + return 1; } - L->oldpc = ci->u.l.savedpc; - if (L->status == LUA_YIELD) { /* did hook yield? */ - if (counthook) - L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ - ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ - ci->func = L->top - 1; /* protect stack below results */ - luaD_throw(L, LUA_YIELD); + else if (cvt2num(obj) && + luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { + obj = &v; + goto again; /* convert result from 'luaO_str2num' to an integer */ } + return 0; /* conversion failed */ } -static void callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, TValue *p3, int hasres) { - ptrdiff_t result = savestack(L, p3); - setobj2s(L, L->top++, f); /* push function */ - setobj2s(L, L->top++, p1); /* 1st argument */ - setobj2s(L, L->top++, p2); /* 2nd argument */ - if (!hasres) /* no result? 'p3' is third argument */ - setobj2s(L, L->top++, p3); /* 3rd argument */ - /* metamethod may yield only when called from Lua code */ - luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); - if (hasres) { /* if has result, move it to its place */ - p3 = restorestack(L, result); - setobjs2s(L, p3, --L->top); - } +/* +** try to convert a value to an integer +*/ +int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { + return tointeger_aux(obj, p, LUA_FLOORN2I); } +/* +** Try to convert a 'for' limit to an integer, preserving the +** semantics of the loop. +** (The following explanation assumes a non-negative step; it is valid +** for negative steps mutatis mutandis.) +** If the limit can be converted to an integer, rounding down, that is +** it. +** Otherwise, check whether the limit can be converted to a number. If +** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, +** which means no limit. If the number is too negative, the loop +** should not run, because any initial integer value is larger than the +** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects +** the extreme case when the initial value is LUA_MININTEGER, in which +** case the LUA_MININTEGER limit would still run the loop once. +*/ +static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, + int *stopnow) { + *stopnow = 0; /* usually, let loops run */ + if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ + lua_Number n; /* try to convert to float */ + if (!tonumber(obj, &n)) /* cannot convert to float? */ + return 0; /* not a number */ + if (n > 0) { /* if true, float is larger than max integer */ + *p = LUA_MAXINTEGER; + if (step < 0) *stopnow = 1; + } + else { /* float is smaller than min integer */ + *p = LUA_MININTEGER; + if (step >= 0) *stopnow = 1; + } + } + return 1; +} + + +/* +** Main function for table access (invoking metamethods if needed). +** Compute 'val = t[key]' +*/ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; + int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ + if (ttistable(t)) { /* 't' is a table? */ Table *h = hvalue(t); const TValue *res = luaH_get(h, key); /* do a primitive get */ if (!ttisnil(res) || /* result is not nil? */ (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ - setobj2s(L, val, res); + setobj2s(L, val, res); /* result is the raw get */ return; } - /* else will try the tag method */ + /* else will try metamethod */ } else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) - luaG_typeerror(L, t, "index"); - if (ttisfunction(tm)) { - callTM(L, tm, t, key, val, 1); + luaG_typeerror(L, t, "index"); /* no metamethod */ + if (ttisfunction(tm)) { /* metamethod is a function */ + luaT_callTM(L, tm, t, key, val, 1); return; } - t = tm; /* else repeat with 'tm' */ + t = tm; /* else repeat access over 'tm' */ } - luaG_runerror(L, "loop in gettable"); + luaG_runerror(L, "gettable chain too long; possible loop"); } +/* +** Main function for table assignment (invoking metamethods if needed). +** Compute 't[key] = val' +*/ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; + int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ + if (ttistable(t)) { /* 't' is a table? */ Table *h = hvalue(t); TValue *oldval = cast(TValue *, luaH_get(h, key)); /* if previous value is not nil, there must be a previous entry - in the table; moreover, a metamethod has no relevance */ + in the table; a metamethod has no relevance */ if (!ttisnil(oldval) || /* previous value is nil; must check the metamethod */ ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && @@ -153,7 +211,7 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { /* no metamethod and (now) there is an entry with given key */ setobj2t(L, oldval, val); /* assign new value to that entry */ invalidateTMcache(h); - luaC_barrierback(L, obj2gco(h), val); + luaC_barrierback(L, h, val); return; } /* else will try the metamethod */ @@ -161,66 +219,40 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { else /* not a table; check metamethod */ if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) luaG_typeerror(L, t, "index"); - /* there is a metamethod */ + /* try the metamethod */ if (ttisfunction(tm)) { - callTM(L, tm, t, key, val, 0); + luaT_callTM(L, tm, t, key, val, 0); return; } - t = tm; /* else repeat with 'tm' */ + t = tm; /* else repeat assignment over 'tm' */ } - luaG_runerror(L, "loop in settable"); -} - - -static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ - if (ttisnil(tm)) - tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (ttisnil(tm)) return 0; - callTM(L, tm, p1, p2, res, 1); - return 1; -} - - -static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2, - TMS event) { - const TValue *tm1 = fasttm(L, mt1, event); - const TValue *tm2; - if (tm1 == NULL) return NULL; /* no metamethod */ - if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ - tm2 = fasttm(L, mt2, event); - if (tm2 == NULL) return NULL; /* no metamethod */ - if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */ - return tm1; - return NULL; -} - - -static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, - TMS event) { - if (!call_binTM(L, p1, p2, L->top, event)) - return -1; /* no metamethod */ - else - return !l_isfalse(L->top); + luaG_runerror(L, "settable chain too long; possible loop"); } +/* +** Compare two strings 'ls' x 'rs', returning an integer smaller-equal- +** -larger than zero if 'ls' is smaller-equal-larger than 'rs'. +** The code is a little tricky because it allows '\0' in the strings +** and it uses 'strcoll' (to respect locales) for each segments +** of the strings. +*/ static int l_strcmp (const TString *ls, const TString *rs) { const char *l = getstr(ls); - size_t ll = ls->tsv.len; + size_t ll = ls->len; const char *r = getstr(rs); - size_t lr = rs->tsv.len; - for (;;) { + size_t lr = rs->len; + for (;;) { /* for each segment */ int temp = strcoll(l, r); - if (temp != 0) return temp; - else { /* strings are equal up to a `\0' */ - size_t len = strlen(l); /* index of first `\0' in both strings */ - if (len == lr) /* r is finished? */ - return (len == ll) ? 0 : 1; - else if (len == ll) /* l is finished? */ - return -1; /* l is smaller than r (because r is not finished) */ - /* both strings longer than `len'; go on comparing (after the `\0') */ + if (temp != 0) /* not equal? */ + return temp; /* done */ + else { /* strings are equal up to a '\0' */ + size_t len = strlen(l); /* index of first '\0' in both strings */ + if (len == lr) /* 'rs' is finished? */ + return (len == ll) ? 0 : 1; /* check 'ls' */ + else if (len == ll) /* 'ls' is finished? */ + return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */ + /* both strings longer than 'len'; go on comparing after the '\0' */ len++; l += len; ll -= len; r += len; lr -= len; } @@ -228,79 +260,113 @@ static int l_strcmp (const TString *ls, const TString *rs) { } +/* +** Main operation less than; return 'l < r'. +*/ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { int res; - if (ttisnumber(l) && ttisnumber(r)) - return luai_numlt(L, nvalue(l), nvalue(r)); - else if (ttisstring(l) && ttisstring(r)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; - else if ((res = call_orderTM(L, l, r, TM_LT)) < 0) - luaG_ordererror(L, l, r); + lua_Number nl, nr; + if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ + return (ivalue(l) < ivalue(r)); + else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */ + return luai_numlt(nl, nr); + else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ + return l_strcmp(tsvalue(l), tsvalue(r)) < 0; + else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ + luaG_ordererror(L, l, r); /* error */ return res; } +/* +** Main operation less than or equal to; return 'l <= r'. +*/ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { int res; - if (ttisnumber(l) && ttisnumber(r)) - return luai_numle(L, nvalue(l), nvalue(r)); - else if (ttisstring(l) && ttisstring(r)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; - else if ((res = call_orderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */ + lua_Number nl, nr; + if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ + return (ivalue(l) <= ivalue(r)); + else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */ + return luai_numle(nl, nr); + else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ + return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; + else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try 'le' */ return res; - else if ((res = call_orderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */ + else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try 'lt' */ luaG_ordererror(L, l, r); return !res; } /* -** equality of Lua values. L == NULL means raw equality (no metamethods) +** Main operation for equality of Lua values; return 't1 == t2'. +** L == NULL means raw equality (no metamethods) */ -int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) { +int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; - lua_assert(ttisequal(t1, t2)); + if (ttype(t1) != ttype(t2)) { /* not the same variant? */ + if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) + return 0; /* only numbers can be equal with different variants */ + else { /* two numbers with different variants */ + lua_Number n1, n2; /* compare them as floats */ + lua_assert(ttisnumber(t1) && ttisnumber(t2)); + cast_void(tofloat(t1, &n1)); cast_void(tofloat(t2, &n2)); + return luai_numeq(n1, n2); + } + } + /* values have same type and same variant */ switch (ttype(t1)) { case LUA_TNIL: return 1; - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); + case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); + case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TLCF: return fvalue(t1) == fvalue(t2); - case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2)); - case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2)); + case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); + case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); case LUA_TUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; else if (L == NULL) return 0; - tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ); + tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } case LUA_TTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; else if (L == NULL) return 0; - tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); + tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } default: - lua_assert(iscollectable(t1)); return gcvalue(t1) == gcvalue(t2); } - if (tm == NULL) return 0; /* no TM? */ - callTM(L, tm, t1, t2, L->top, 1); /* call TM */ + if (tm == NULL) /* no TM? */ + return 0; /* objects are different */ + luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ return !l_isfalse(L->top); } +/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ +#define tostring(L,o) \ + (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) + +/* +** Main operation for concatenation: concat 'total' values in the stack, +** from 'L->top - total' up to 'L->top - 1'. +*/ void luaV_concat (lua_State *L, int total) { lua_assert(total >= 2); do { StkId top = L->top; int n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) - luaG_concaterror(L, top-2, top-1); - } + if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) + luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); else if (tsvalue(top-1)->len == 0) /* second operand is empty? */ - (void)tostring(L, top - 2); /* result is first operand */ + cast_void(tostring(L, top - 2)); /* result is first operand */ else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) { setobjs2s(L, top - 2, top - 1); /* result is second op. */ } @@ -312,19 +378,19 @@ void luaV_concat (lua_State *L, int total) { /* collect total length */ for (i = 1; i < total && tostring(L, top-i-1); i++) { size_t l = tsvalue(top-i-1)->len; - if (l >= (MAX_SIZET/sizeof(char)) - tl) + if (l >= (MAX_SIZE/sizeof(char)) - tl) luaG_runerror(L, "string length overflow"); tl += l; } buffer = luaZ_openspace(L, &G(L)->buff, tl); tl = 0; n = i; - do { /* concat all strings */ + do { /* copy all strings to buffer */ size_t l = tsvalue(top-i)->len; memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); tl += l; } while (--i > 0); - setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); + setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); /* create result */ } total -= n-1; /* got 'n' strings to create 1 new */ L->top -= n-1; /* popped 'n' strings and pushed one */ @@ -332,18 +398,21 @@ void luaV_concat (lua_State *L, int total) { } +/* +** Main operation 'ra' = #rb'. +*/ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; - switch (ttypenv(rb)) { + switch (ttnov(rb)) { case LUA_TTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ - setnvalue(ra, cast_num(luaH_getn(h))); /* else primitive len */ + setivalue(ra, luaH_getn(h)); /* else primitive len */ return; } case LUA_TSTRING: { - setnvalue(ra, cast_num(tsvalue(rb)->len)); + setivalue(ra, tsvalue(rb)->len); return; } default: { /* try metamethod */ @@ -353,21 +422,66 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { break; } } - callTM(L, tm, rb, rb, ra, 1); + luaT_callTM(L, tm, rb, rb, ra, 1); } -void luaV_arith (lua_State *L, StkId ra, const TValue *rb, - const TValue *rc, TMS op) { - TValue tempb, tempc; - const TValue *b, *c; - if ((b = luaV_tonumber(rb, &tempb)) != NULL && - (c = luaV_tonumber(rc, &tempc)) != NULL) { - lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c)); - setnvalue(ra, res); +/* +** Integer division; return 'm // n', that is, floor(m/n). +** C division truncates its result (rounds towards zero). +** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, +** otherwise 'floor(q) == trunc(q) - 1'. +*/ +lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { + if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (n == 0) + luaG_runerror(L, "attempt to divide by zero"); + return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ + } + else { + lua_Integer q = m / n; /* perform C division */ + if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ + q -= 1; /* correct result for different rounding */ + return q; + } +} + + +/* +** Integer modulus; return 'm % n'. (Assume that C '%' with +** negative operands follows C99 behavior. See previous comment +** about luaV_div.) +*/ +lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { + if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (n == 0) + luaG_runerror(L, "attempt to perform 'n%%0'"); + return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ + } + else { + lua_Integer r = m % n; + if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */ + r += n; /* correct result for different rounding */ + return r; + } +} + + +/* number of bits in an integer */ +#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) + +/* +** Shift left operation. (Shift right just negates 'y'.) +*/ +lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { + if (y < 0) { /* shift right? */ + if (y <= -NBITS) return 0; + else return intop(>>, x, -y); + } + else { /* shift left */ + if (y >= NBITS) return 0; + else return intop(<<, x, y); } - else if (!call_binTM(L, rb, rc, ra, op)) - luaG_aritherror(L, rb, rc); } @@ -376,15 +490,15 @@ void luaV_arith (lua_State *L, StkId ra, const TValue *rb, ** whether there is a cached closure with the same upvalues needed by ** new closure to be created. */ -static Closure *getcached (Proto *p, UpVal **encup, StkId base) { - Closure *c = p->cache; +static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { + LClosure *c = p->cache; if (c != NULL) { /* is there a cached closure? */ - int nup = p->sp->sizeupvalues; - Upvaldesc *uv = p->sp->upvalues; + int nup = p->sizeupvalues; + Upvaldesc *uv = p->upvalues; int i; for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; - if (c->l.upvals[i]->v != v) + if (c->upvals[i]->v != v) return NULL; /* wrong upvalue; cannot reuse closure */ } } @@ -394,26 +508,28 @@ static Closure *getcached (Proto *p, UpVal **encup, StkId base) { /* ** create a new Lua closure, push it in the stack, and initialize -** its upvalues. Note that the call to 'luaC_barrierproto' must come -** before the assignment to 'p->cache', as the function needs the -** original value of that field. +** its upvalues. Note that the closure is not cached if prototype is +** already black (which means that 'cache' was already cleared by the +** GC). */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { - int nup = p->sp->sizeupvalues; - Upvaldesc *uv = p->sp->upvalues; + int nup = p->sizeupvalues; + Upvaldesc *uv = p->upvalues; int i; - Closure *ncl = luaF_newLclosure(L, nup); - ncl->l.p = p; + LClosure *ncl = luaF_newLclosure(L, nup); + ncl->p = p; setclLvalue(L, ra, ncl); /* anchor new closure in stack */ for (i = 0; i < nup; i++) { /* fill in its upvalues */ if (uv[i].instack) /* upvalue refers to local variable? */ - ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx); + ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); else /* get upvalue from enclosing function */ - ncl->l.upvals[i] = encup[uv[i].idx]; + ncl->upvals[i] = encup[uv[i].idx]; + ncl->upvals[i]->refcount++; + /* new closure is white, so we do not need a barrier here */ } - luaC_barrierproto(L, p, ncl); - p->cache = ncl; /* save it on cache for reuse */ + if (!isblack(p)) /* cache will not break GC invariant? */ + p->cache = ncl; /* save it on cache for reuse */ } @@ -426,8 +542,10 @@ void luaV_finishOp (lua_State *L) { Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ - case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: - case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN: + case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: + case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: + case OP_MOD: case OP_POW: + case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { setobjs2s(L, base + GETARG_A(inst), --L->top); break; @@ -446,7 +564,7 @@ void luaV_finishOp (lua_State *L) { break; } case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'call_binTM' was called */ + StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ int b = GETARG_B(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ setobj2s(L, top - 2, top); /* put TM result in proper position */ @@ -477,8 +595,16 @@ void luaV_finishOp (lua_State *L) { + /* -** some macros for common tasks in `luaV_execute' +** {================================================================== +** Function 'luaV_execute': main interpreter loop +** =================================================================== +*/ + + +/* +** some macros for common tasks in 'luaV_execute' */ #if !defined luai_runtimecheck @@ -517,19 +643,9 @@ void luaV_finishOp (lua_State *L) { luai_threadyield(L); ) -#define arith_op(op,tm) { \ - TValue *rb = RKB(i); \ - TValue *rc = RKC(i); \ - if (ttisnumber(rb) && ttisnumber(rc)) { \ - lua_Number nb = nvalue(rb), nc = nvalue(rc); \ - setnvalue(ra, op(L, nb, nc)); \ - } \ - else { Protect(luaV_arith(L, ra, rb, rc, tm)); } } - - #define vmdispatch(o) switch(o) -#define vmcase(l,b) case l: {b} break; -#define vmcasenb(l,b) case l: {b} /* nb = no break */ +#define vmcase(l) case l: +#define vmbreak break void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; @@ -547,60 +663,71 @@ void luaV_execute (lua_State *L) { StkId ra; if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { - Protect(traceexec(L)); + Protect(luaG_traceexec(L)); } - /* WARNING: several calls may realloc the stack and invalidate `ra' */ + /* WARNING: several calls may realloc the stack and invalidate 'ra' */ ra = RA(i); lua_assert(base == ci->u.l.base); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); vmdispatch (GET_OPCODE(i)) { - vmcase(OP_MOVE, + vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); - ) - vmcase(OP_LOADK, + vmbreak; + } + vmcase(OP_LOADK) { TValue *rb = k + GETARG_Bx(i); setobj2s(L, ra, rb); - ) - vmcase(OP_LOADKX, + vmbreak; + } + vmcase(OP_LOADKX) { TValue *rb; lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); rb = k + GETARG_Ax(*ci->u.l.savedpc++); setobj2s(L, ra, rb); - ) - vmcase(OP_LOADBOOL, + vmbreak; + } + vmcase(OP_LOADBOOL) { setbvalue(ra, GETARG_B(i)); if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ - ) - vmcase(OP_LOADNIL, + vmbreak; + } + vmcase(OP_LOADNIL) { int b = GETARG_B(i); do { setnilvalue(ra++); } while (b--); - ) - vmcase(OP_GETUPVAL, + vmbreak; + } + vmcase(OP_GETUPVAL) { int b = GETARG_B(i); setobj2s(L, ra, cl->upvals[b]->v); - ) - vmcase(OP_GETTABUP, + vmbreak; + } + vmcase(OP_GETTABUP) { int b = GETARG_B(i); Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra)); - ) - vmcase(OP_GETTABLE, + vmbreak; + } + vmcase(OP_GETTABLE) { Protect(luaV_gettable(L, RB(i), RKC(i), ra)); - ) - vmcase(OP_SETTABUP, + vmbreak; + } + vmcase(OP_SETTABUP) { int a = GETARG_A(i); Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i))); - ) - vmcase(OP_SETUPVAL, + vmbreak; + } + vmcase(OP_SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, ra); - luaC_barrier(L, uv, ra); - ) - vmcase(OP_SETTABLE, + luaC_upvalbarrier(L, uv); + vmbreak; + } + vmcase(OP_SETTABLE) { Protect(luaV_settable(L, ra, RKB(i), RKC(i))); - ) - vmcase(OP_NEWTABLE, + vmbreak; + } + vmcase(OP_NEWTABLE) { int b = GETARG_B(i); int c = GETARG_C(i); Table *t = luaH_new(L); @@ -608,49 +735,193 @@ void luaV_execute (lua_State *L) { if (b != 0 || c != 0) luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); checkGC(L, ra + 1); - ) - vmcase(OP_SELF, + vmbreak; + } + vmcase(OP_SELF) { StkId rb = RB(i); setobjs2s(L, ra+1, rb); Protect(luaV_gettable(L, rb, RKC(i), ra)); - ) - vmcase(OP_ADD, - arith_op(luai_numadd, TM_ADD); - ) - vmcase(OP_SUB, - arith_op(luai_numsub, TM_SUB); - ) - vmcase(OP_MUL, - arith_op(luai_nummul, TM_MUL); - ) - vmcase(OP_DIV, - arith_op(luai_numdiv, TM_DIV); - ) - vmcase(OP_MOD, - arith_op(luai_nummod, TM_MOD); - ) - vmcase(OP_POW, - arith_op(luai_numpow, TM_POW); - ) - vmcase(OP_UNM, + vmbreak; + } + vmcase(OP_ADD) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, intop(+, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numadd(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } + vmbreak; + } + vmcase(OP_SUB) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, intop(-, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numsub(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } + vmbreak; + } + vmcase(OP_MUL) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, intop(*, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_nummul(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } + vmbreak; + } + vmcase(OP_DIV) { /* float division (always with floats) */ + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numdiv(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } + vmbreak; + } + vmcase(OP_BAND) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, intop(&, ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } + vmbreak; + } + vmcase(OP_BOR) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, intop(|, ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } + vmbreak; + } + vmcase(OP_BXOR) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, intop(^, ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } + vmbreak; + } + vmcase(OP_SHL) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, luaV_shiftl(ib, ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } + vmbreak; + } + vmcase(OP_SHR) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Integer ib; lua_Integer ic; + if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + setivalue(ra, luaV_shiftl(ib, -ic)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } + vmbreak; + } + vmcase(OP_MOD) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, luaV_mod(L, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + lua_Number m; + luai_nummod(L, nb, nc, m); + setfltvalue(ra, m); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); } + vmbreak; + } + vmcase(OP_IDIV) { /* floor division */ + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (ttisinteger(rb) && ttisinteger(rc)) { + lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); + setivalue(ra, luaV_div(L, ib, ic)); + } + else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numidiv(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } + vmbreak; + } + vmcase(OP_POW) { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + lua_Number nb; lua_Number nc; + if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + setfltvalue(ra, luai_numpow(L, nb, nc)); + } + else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } + vmbreak; + } + vmcase(OP_UNM) { TValue *rb = RB(i); - if (ttisnumber(rb)) { - lua_Number nb = nvalue(rb); - setnvalue(ra, luai_numunm(L, nb)); + lua_Number nb; + if (ttisinteger(rb)) { + lua_Integer ib = ivalue(rb); + setivalue(ra, intop(-, 0, ib)); + } + else if (tonumber(rb, &nb)) { + setfltvalue(ra, luai_numunm(L, nb)); } else { - Protect(luaV_arith(L, ra, rb, rb, TM_UNM)); + Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); } - ) - vmcase(OP_NOT, + vmbreak; + } + vmcase(OP_BNOT) { + TValue *rb = RB(i); + lua_Integer ib; + if (tointeger(rb, &ib)) { + setivalue(ra, intop(^, ~l_castS2U(0), ib)); + } + else { + Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); + } + vmbreak; + } + vmcase(OP_NOT) { TValue *rb = RB(i); int res = l_isfalse(rb); /* next assignment may change this value */ setbvalue(ra, res); - ) - vmcase(OP_LEN, + vmbreak; + } + vmcase(OP_LEN) { Protect(luaV_objlen(L, ra, RB(i))); - ) - vmcase(OP_CONCAT, + vmbreak; + } + vmcase(OP_CONCAT) { int b = GETARG_B(i); int c = GETARG_C(i); StkId rb; @@ -661,43 +932,49 @@ void luaV_execute (lua_State *L) { setobjs2s(L, ra, rb); checkGC(L, (ra >= rb ? ra + 1 : rb)); L->top = ci->top; /* restore top */ - ) - vmcase(OP_JMP, + vmbreak; + } + vmcase(OP_JMP) { dojump(ci, i, 0); - ) - vmcase(OP_EQ, + vmbreak; + } + vmcase(OP_EQ) { TValue *rb = RKB(i); TValue *rc = RKC(i); Protect( - if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i)) + if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i)) ci->u.l.savedpc++; else donextjump(ci); ) - ) - vmcase(OP_LT, + vmbreak; + } + vmcase(OP_LT) { Protect( if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) ci->u.l.savedpc++; else donextjump(ci); ) - ) - vmcase(OP_LE, + vmbreak; + } + vmcase(OP_LE) { Protect( if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) ci->u.l.savedpc++; else donextjump(ci); ) - ) - vmcase(OP_TEST, + vmbreak; + } + vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) ci->u.l.savedpc++; else donextjump(ci); - ) - vmcase(OP_TESTSET, + vmbreak; + } + vmcase(OP_TESTSET) { TValue *rb = RB(i); if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) ci->u.l.savedpc++; @@ -705,8 +982,9 @@ void luaV_execute (lua_State *L) { setobjs2s(L, ra, rb); donextjump(ci); } - ) - vmcase(OP_CALL, + vmbreak; + } + vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) L->top = ra+b; /* else previous instruction set top */ @@ -719,8 +997,9 @@ void luaV_execute (lua_State *L) { ci->callstatus |= CIST_REENTRY; goto newframe; /* restart luaV_execute over new Lua function */ } - ) - vmcase(OP_TAILCALL, + vmbreak; + } + vmcase(OP_TAILCALL) { int b = GETARG_B(i); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); @@ -733,10 +1012,10 @@ void luaV_execute (lua_State *L) { StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ - StkId lim = nci->u.l.base + getproto(nfunc)->sp->numparams; + StkId lim = nci->u.l.base + getproto(nfunc)->numparams; int aux; /* close all upvalues from previous call */ - if (cl->p->sp->sizep > 0) luaF_close(L, oci->u.l.base); + if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); @@ -748,11 +1027,12 @@ void luaV_execute (lua_State *L) { lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } - ) - vmcasenb(OP_RETURN, + vmbreak; + } + vmcase(OP_RETURN) { int b = GETARG_B(i); if (b != 0) L->top = ra+b-1; - if (cl->p->sp->sizep > 0) luaF_close(L, base); + if (cl->p->sizep > 0) luaF_close(L, base); b = luaD_poscall(L, ra); if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ return; /* external invocation: return */ @@ -763,32 +1043,60 @@ void luaV_execute (lua_State *L) { lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); goto newframe; /* restart luaV_execute over new Lua function */ } - ) - vmcase(OP_FORLOOP, - lua_Number step = nvalue(ra+2); - lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */ - lua_Number limit = nvalue(ra+1); - if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit) - : luai_numle(L, limit, idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - setnvalue(ra, idx); /* update internal index... */ - setnvalue(ra+3, idx); /* ...and external index */ + } + vmcase(OP_FORLOOP) { + if (ttisinteger(ra)) { /* integer loop? */ + lua_Integer step = ivalue(ra + 2); + lua_Integer idx = ivalue(ra) + step; /* increment index */ + lua_Integer limit = ivalue(ra + 1); + if ((0 < step) ? (idx <= limit) : (limit <= idx)) { + ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ + setivalue(ra, idx); /* update internal index... */ + setivalue(ra + 3, idx); /* ...and external index */ + } + } + else { /* floating loop */ + lua_Number step = fltvalue(ra + 2); + lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */ + lua_Number limit = fltvalue(ra + 1); + if (luai_numlt(0, step) ? luai_numle(idx, limit) + : luai_numle(limit, idx)) { + ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ + setfltvalue(ra, idx); /* update internal index... */ + setfltvalue(ra + 3, idx); /* ...and external index */ + } + } + vmbreak; + } + vmcase(OP_FORPREP) { + TValue *init = ra; + TValue *plimit = ra + 1; + TValue *pstep = ra + 2; + lua_Integer ilimit; + int stopnow; + if (ttisinteger(init) && ttisinteger(pstep) && + forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { + /* all values are integer */ + lua_Integer initv = (stopnow ? 0 : ivalue(init)); + setivalue(plimit, ilimit); + setivalue(init, initv - ivalue(pstep)); + } + else { /* try making all values floats */ + lua_Number ninit; lua_Number nlimit; lua_Number nstep; + if (!tonumber(plimit, &nlimit)) + luaG_runerror(L, "'for' limit must be a number"); + setfltvalue(plimit, nlimit); + if (!tonumber(pstep, &nstep)) + luaG_runerror(L, "'for' step must be a number"); + setfltvalue(pstep, nstep); + if (!tonumber(init, &ninit)) + luaG_runerror(L, "'for' initial value must be a number"); + setfltvalue(init, luai_numsub(L, ninit, nstep)); } - ) - vmcase(OP_FORPREP, - const TValue *init = ra; - const TValue *plimit = ra+1; - const TValue *pstep = ra+2; - if (!tonumber(init, ra)) - luaG_runerror(L, LUA_QL("for") " initial value must be a number"); - else if (!tonumber(plimit, ra+1)) - luaG_runerror(L, LUA_QL("for") " limit must be a number"); - else if (!tonumber(pstep, ra+2)) - luaG_runerror(L, LUA_QL("for") " step must be a number"); - setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep))); ci->u.l.savedpc += GETARG_sBx(i); - ) - vmcasenb(OP_TFORCALL, + vmbreak; + } + vmcase(OP_TFORCALL) { StkId cb = ra + 3; /* call base */ setobjs2s(L, cb+2, ra+2); setobjs2s(L, cb+1, ra+1); @@ -800,18 +1108,19 @@ void luaV_execute (lua_State *L) { ra = RA(i); lua_assert(GET_OPCODE(i) == OP_TFORLOOP); goto l_tforloop; - ) - vmcase(OP_TFORLOOP, + } + vmcase(OP_TFORLOOP) { l_tforloop: if (!ttisnil(ra + 1)) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ } - ) - vmcase(OP_SETLIST, + vmbreak; + } + vmcase(OP_SETLIST) { int n = GETARG_B(i); int c = GETARG_C(i); - int last; + unsigned int last; Table *h; if (n == 0) n = cast_int(L->top - ra) - 1; if (c == 0) { @@ -826,23 +1135,25 @@ void luaV_execute (lua_State *L) { for (; n > 0; n--) { TValue *val = ra+n; luaH_setint(L, h, last--, val); - luaC_barrierback(L, obj2gco(h), val); + luaC_barrierback(L, h, val); } L->top = ci->top; /* correct top (in case of previous open call) */ - ) - vmcase(OP_CLOSURE, + vmbreak; + } + vmcase(OP_CLOSURE) { Proto *p = cl->p->p[GETARG_Bx(i)]; - Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */ + LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ if (ncl == NULL) /* no match? */ pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ else setclLvalue(L, ra, ncl); /* push cashed closure */ checkGC(L, ra + 1); - ) - vmcase(OP_VARARG, + vmbreak; + } + vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; int j; - int n = cast_int(base - ci->func) - cl->p->sp->numparams - 1; + int n = cast_int(base - ci->func) - cl->p->numparams - 1; if (b < 0) { /* B == 0? */ b = n; /* get all var. arguments */ Protect(luaD_checkstack(L, n)); @@ -857,11 +1168,15 @@ void luaV_execute (lua_State *L) { setnilvalue(ra + j); } } - ) - vmcase(OP_EXTRAARG, + vmbreak; + } + vmcase(OP_EXTRAARG) { lua_assert(0); - ) + vmbreak; + } } } } +/* }================================================================== */ + diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index 5380270d..5c5e18c3 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -13,23 +13,36 @@ #include "ltm.h" -#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) - -#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) - -#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) - -#define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) +#if !defined(LUA_NOCVTN2S) +#define cvt2str(o) ttisnumber(o) +#else +#define cvt2str(o) 0 /* no conversion from numbers to strings */ +#endif -/* not to called directly */ -LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); +#if !defined(LUA_NOCVTS2N) +#define cvt2num(o) ttisstring(o) +#else +#define cvt2num(o) 0 /* no conversion from strings to numbers */ +#endif +#define tonumber(o,n) \ + (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) + +#define tointeger(o,i) \ + (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) + +#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) + +#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) + + +LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); -LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); +LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); +LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val); LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, @@ -37,8 +50,9 @@ LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L); LUAI_FUNC void luaV_concat (lua_State *L, int total); -LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, - const TValue *rc, TMS op); +LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); +LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); +LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); #endif diff --git a/3rd/lua/lzio.c b/3rd/lua/lzio.c index 20efea98..46493920 100644 --- a/3rd/lua/lzio.c +++ b/3rd/lua/lzio.c @@ -1,15 +1,17 @@ /* -** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ - -#include - #define lzio_c #define LUA_CORE +#include "lprefix.h" + + +#include + #include "lua.h" #include "llimits.h" diff --git a/3rd/lua/lzio.h b/3rd/lua/lzio.h index 441f7479..b2e56bcd 100644 --- a/3rd/lua/lzio.h +++ b/3rd/lua/lzio.h @@ -1,5 +1,5 @@ /* -** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ @@ -32,11 +32,13 @@ typedef struct Mbuffer { #define luaZ_sizebuffer(buff) ((buff)->buffsize) #define luaZ_bufflen(buff) ((buff)->n) +#define luaZ_buffremove(buff,i) ((buff)->n -= (i)) #define luaZ_resetbuffer(buff) ((buff)->n = 0) #define luaZ_resizebuffer(L, buff, size) \ - (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ + ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ + (buff)->buffsize, size), \ (buff)->buffsize = size) #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) @@ -45,7 +47,7 @@ typedef struct Mbuffer { LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data); -LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ +LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ @@ -55,7 +57,7 @@ struct Zio { size_t n; /* bytes still unread */ const char *p; /* current position in buffer */ lua_Reader reader; /* reader function */ - void* data; /* additional data */ + void *data; /* additional data */ lua_State *L; /* Lua state (for reader) */ }; diff --git a/Makefile b/Makefile index 0d0cd855..89f3a730 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ jemalloc : $(MALLOC_STATICLIB) # skynet CSERVICE = snlua logger gate harbor -LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \ +LUA_CLIB = skynet socketdriver bson mongo md5 netpack \ clientsocket memory profile multicast \ cluster crypt sharedata stm sproto lpeg \ mysqlaux @@ -78,9 +78,6 @@ $(LUA_CLIB_PATH)/skynet.so : lualib-src/lua-skynet.c lualib-src/lua-seri.c | $(L $(LUA_CLIB_PATH)/socketdriver.so : lualib-src/lua-socket.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src -$(LUA_CLIB_PATH)/int64.so : 3rd/lua-int64/int64.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ - $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index e4dd4ab7..2e93cbb5 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -433,7 +433,7 @@ pack_dict(lua_State *L, struct bson *b, bool isarray) { luaL_error(L, "Invalid array key type : %s", lua_typename(L, kt)); return; } - sz = bson_numstr(numberkey, lua_tounsigned(L,-2)-1); + sz = bson_numstr(numberkey, (unsigned int)lua_tointeger(L,-2)-1); key = numberkey; append_one(b, L, key, sz); @@ -912,7 +912,7 @@ ltimestamp(lua_State *L) { luaL_addlstring(&b, (const char *)&inc, sizeof(inc)); ++inc; } else { - uint32_t i = lua_tounsigned(L,2); + uint32_t i = (uint32_t)lua_tointeger(L,2); luaL_addlstring(&b, (const char *)&i, sizeof(i)); } luaL_addlstring(&b, (const char *)&d, sizeof(d)); @@ -996,8 +996,8 @@ lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) { } const uint32_t * ts = (const uint32_t *)buf; lua_pushvalue(L, lua_upvalueindex(8)); - lua_pushunsigned(L, ts[1]); - lua_pushunsigned(L, ts[0]); + lua_pushinteger(L, (lua_Integer)ts[1]); + lua_pushinteger(L, (lua_Integer)ts[0]); return 3; } case BSON_REGEX: { diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 8ba1b7d6..c4875070 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -37,7 +37,7 @@ fill_header(lua_State *L, uint8_t *buf, int sz, void *msg) { static void packreq_number(lua_State *L, int session, void * msg, size_t sz) { - uint32_t addr = lua_tounsigned(L,1); + uint32_t addr = (uint32_t)lua_tointeger(L,1); uint8_t buf[TEMP_LENGTH]; fill_header(L, buf, sz+9, msg); buf[2] = 0; @@ -73,7 +73,7 @@ lpackrequest(lua_State *L) { if (msg == NULL) { return luaL_error(L, "Invalid request message"); } - size_t sz = luaL_checkunsigned(L,4); + size_t sz = (size_t)luaL_checkinteger(L,4); int session = luaL_checkinteger(L,2); if (session <= 0) { return luaL_error(L, "Invalid request session %d", session); @@ -112,8 +112,8 @@ unpackreq_number(lua_State *L, const uint8_t * buf, size_t sz) { } uint32_t address = unpack_uint32(buf+1); uint32_t session = unpack_uint32(buf+5); - lua_pushunsigned(L, address); - lua_pushunsigned(L, session); + lua_pushinteger(L, (uint32_t)address); + lua_pushinteger(L, (uint32_t)session); lua_pushlstring(L, (const char *)buf+9, sz-9); return 3; @@ -127,7 +127,7 @@ unpackreq_string(lua_State *L, const uint8_t * buf, size_t sz) { } lua_pushlstring(L, (const char *)buf+1, namesz); uint32_t session = unpack_uint32(buf + namesz + 1); - lua_pushunsigned(L, session); + lua_pushinteger(L, (uint32_t)session); lua_pushlstring(L, (const char *)buf+1+namesz+4, sz - namesz - 5); return 3; @@ -153,7 +153,7 @@ lunpackrequest(lua_State *L) { */ static int lpackresponse(lua_State *L) { - uint32_t session = luaL_checkunsigned(L,1); + uint32_t session = (uint32_t)luaL_checkinteger(L,1); // clusterd.lua:command.socket call lpackresponse, // and the msg/sz is return by skynet.rawcall , so don't free(msg) int ok = lua_toboolean(L,2); @@ -167,7 +167,7 @@ lpackresponse(lua_State *L) { } } else { msg = lua_touserdata(L,3); - sz = luaL_checkunsigned(L, 4); + sz = (size_t)luaL_checkinteger(L, 4); } uint8_t buf[TEMP_LENGTH]; @@ -195,7 +195,7 @@ lunpackresponse(lua_State *L) { return 0; } uint32_t session = unpack_uint32((const uint8_t *)buf); - lua_pushunsigned(L, session); + lua_pushinteger(L, (lua_Integer)session); lua_pushboolean(L, buf[4]); lua_pushlstring(L, buf+5, sz-5); diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index fa7c026c..17e7cd40 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -6,7 +6,7 @@ static int ltotal(lua_State *L) { size_t t = malloc_used_memory(); - lua_pushunsigned(L, t); + lua_pushinteger(L, (lua_Integer)t); return 1; } @@ -14,7 +14,7 @@ ltotal(lua_State *L) { static int lblock(lua_State *L) { size_t t = malloc_memory_block(); - lua_pushunsigned(L, t); + lua_pushinteger(L, (lua_Integer)t); return 1; } diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 609169d4..599616f9 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -33,7 +33,7 @@ pack(lua_State *L, void *data, size_t size) { static int mc_packlocal(lua_State *L) { void * data = lua_touserdata(L, 1); - size_t size = luaL_checkunsigned(L, 2); + size_t size = (size_t)luaL_checkinteger(L, 2); if (size != (uint32_t)size) { return luaL_error(L, "Size should be 32bit integer"); } @@ -49,7 +49,7 @@ mc_packlocal(lua_State *L) { static int mc_packremote(lua_State *L) { void * data = lua_touserdata(L, 1); - size_t size = luaL_checkunsigned(L, 2); + size_t size = (size_t)luaL_checkinteger(L, 2); if (size != (uint32_t)size) { return luaL_error(L, "Size should be 32bit integer"); } @@ -85,7 +85,7 @@ mc_unpacklocal(lua_State *L) { } lua_pushlightuserdata(L, *pack); lua_pushlightuserdata(L, (*pack)->data); - lua_pushunsigned(L, (*pack)->size); + lua_pushinteger(L, (lua_Integer)((*pack)->size)); return 3; } @@ -137,16 +137,16 @@ mc_remote(lua_State *L) { struct mc_package **ptr = lua_touserdata(L,1); struct mc_package *pack = *ptr; lua_pushlightuserdata(L, pack->data); - lua_pushunsigned(L, pack->size); + lua_pushinteger(L, (lua_Integer)(pack->size)); skynet_free(pack); return 2; } static int mc_nextid(lua_State *L) { - uint32_t id = luaL_checkunsigned(L, 1); + uint32_t id = (uint32_t)luaL_checkinteger(L, 1); id += 256; - lua_pushunsigned(L, id); + lua_pushinteger(L, (uint32_t)id); return 1; } diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index d7096b2a..9dc658ad 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -136,7 +136,7 @@ get_dest_string(lua_State *L, int index) { } /* - unsigned address + uint32 address string address integer type integer session @@ -147,7 +147,7 @@ get_dest_string(lua_State *L, int index) { static int _send(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - uint32_t dest = lua_tounsigned(L, 1); + uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; if (dest == 0) { dest_string = get_dest_string(L, 1); @@ -201,12 +201,12 @@ _send(lua_State *L) { static int _redirect(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - uint32_t dest = lua_tounsigned(L,1); + uint32_t dest = (uint32_t)lua_tointeger(L,1); const char * dest_string = NULL; if (dest == 0) { dest_string = get_dest_string(L, 1); } - uint32_t source = luaL_checkunsigned(L,2); + uint32_t source = (uint32_t)luaL_checkinteger(L,2); int type = luaL_checkinteger(L,3); int session = luaL_checkinteger(L,4); @@ -262,7 +262,7 @@ _tostring(lua_State *L) { static int _harbor(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - uint32_t handle = luaL_checkunsigned(L,1); + uint32_t handle = (uint32_t)luaL_checkinteger(L,1); int harbor = 0; int remote = skynet_isremote(context, handle, &harbor); lua_pushinteger(L,harbor); diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index eabb8620..8ee60eb0 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -215,7 +215,7 @@ lheader(lua_State *L) { sz |= s[i]; } - lua_pushunsigned(L, sz); + lua_pushinteger(L, (lua_Integer)sz); return 1; } diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index afe26f07..b7bb5795 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -127,7 +127,7 @@ lcopy(lua_State *L) { static int lnewwriter(lua_State *L) { void * msg = lua_touserdata(L, 1); - uint32_t sz = luaL_checkunsigned(L, 2); + uint32_t sz = (uint32_t)luaL_checkinteger(L, 2); struct boxstm * box = lua_newuserdata(L, sizeof(*box)); box->obj = stm_new(msg,sz); lua_pushvalue(L, lua_upvalueindex(1)); @@ -149,7 +149,7 @@ static int lupdate(lua_State *L) { struct boxstm * box = lua_touserdata(L, 1); void * msg = lua_touserdata(L, 2); - uint32_t sz = luaL_checkunsigned(L, 3); + uint32_t sz = (uint32_t)luaL_checkinteger(L, 3); stm_update(box->obj, msg, sz); return 0; @@ -199,7 +199,7 @@ lread(lua_State *L) { if (copy) { lua_settop(L, 2); lua_pushlightuserdata(L, copy->msg); - lua_pushunsigned(L, copy->sz); + lua_pushinteger(L, copy->sz); lua_call(L, 2, LUA_MULTRET); lua_pushboolean(L, 1); lua_replace(L, 1); From 5c07652f6b1178d17d1965516e2587eb3992bdbc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 16:29:15 +0800 Subject: [PATCH 266/729] add readme for lua 5.3.0 rc3 --- 3rd/lua/README | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 3rd/lua/README diff --git a/3rd/lua/README b/3rd/lua/README new file mode 100644 index 00000000..0da6217c --- /dev/null +++ b/3rd/lua/README @@ -0,0 +1,3 @@ +Lua 5.3.0 rc3 http://www.lua.org/work/lua-5.3.0-rc3.tar.gz + +It will be update to final version when lua 5.3.0 released. From da9e8fec2091a658bf1a2e0ccdae0c42525d5af6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 17:05:41 +0800 Subject: [PATCH 267/729] use lua 5.3 lua_isinteger --- lualib-src/lua-bson.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 2e93cbb5..0a0e833f 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -263,12 +263,8 @@ append_key(struct bson *bs, int type, const char *key, size_t sz) { static void append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { - int64_t i = lua_tointeger(L, -1); - lua_Number d = lua_tonumber(L,-1); - if (i != d) { - append_key(bs, BSON_REAL, key, sz); - write_double(bs, d); - } else { + if (lua_isinteger(L, -1)) { + int64_t i = lua_tointeger(L, -1); int si = i >> 31; if (si == 0 || si == -1) { append_key(bs, BSON_INT32, key, sz); @@ -277,6 +273,10 @@ append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { append_key(bs, BSON_INT64, key, sz); write_int64(bs, i); } + } else { + lua_Number d = lua_tonumber(L,-1); + append_key(bs, BSON_REAL, key, sz); + write_double(bs, d); } } @@ -797,20 +797,18 @@ lreplace(lua_State *L) { replace_object(L, type, &b); break; case BSON_INT32: { - double d = luaL_checknumber(L,3); - int32_t i = lua_tointeger(L,3); - if ((int32_t)d != i) { - luaL_error(L, "%f must be a 32bit integer ", d); + if (!lua_isinteger(L, 3)) { + luaL_error(L, "%f must be a 32bit integer ", lua_tonumber(L, 3)); } + int32_t i = lua_tointeger(L,3); write_int32(&b, i); break; } case BSON_INT64: { - double d = luaL_checknumber(L,3); - lua_Integer i = lua_tointeger(L,3); - if ((lua_Integer)d != i) { - luaL_error(L, "%f must be a 64bit integer ", d); + if (!lua_isinteger(L, 3)) { + luaL_error(L, "%f must be a 64bit integer ", lua_tonumber(L, 3)); } + int64_t i = lua_tointeger(L,3); write_int64(&b, i); break; } From d06aa9b27df08302f79a0bc8811c5abd4185376c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 18:17:52 +0800 Subject: [PATCH 268/729] support lua 5.3 lua_Integer --- lualib-src/lua-seri.c | 178 ++++++++++++++++++++++--------------- lualib-src/lua-sharedata.c | 43 +++++---- 2 files changed, 132 insertions(+), 89 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index c41456be..9b2af465 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -15,7 +15,14 @@ #define TYPE_BOOLEAN 1 // hibits 0 false 1 true #define TYPE_NUMBER 2 -// hibits 0 : 0 , 1: byte, 2:word, 4: dword, 8 : double +// hibits 0 : 0 , 1: byte, 2:word, 4: dword, 6: qword, 8 : double +#define TYPE_NUMBER_ZERO 0 +#define TYPE_NUMBER_BYTE 1 +#define TYPE_NUMBER_WORD 2 +#define TYPE_NUMBER_DWORD 4 +#define TYPE_NUMBER_QWORD 6 +#define TYPE_NUMBER_REAL 8 + #define TYPE_USERDATA 3 #define TYPE_SHORT_STRING 4 // hibits 0~31 : len @@ -107,7 +114,7 @@ rball_init(struct read_block * rb, char * buffer, int size) { } static void * -rb_read(struct read_block *rb, void *buffer, int sz) { +rb_read(struct read_block *rb, int sz) { if (rb->len < sz) { return NULL; } @@ -131,36 +138,44 @@ wb_boolean(struct write_block *wb, int boolean) { } static inline void -wb_integer(struct write_block *wb, int v, int type) { +wb_integer(struct write_block *wb, lua_Integer v) { + int type = TYPE_NUMBER; if (v == 0) { - uint8_t n = COMBINE_TYPE(type , 0); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_ZERO); wb_push(wb, &n, 1); - } else if (v<0) { - uint8_t n = COMBINE_TYPE(type , 4); + } else if (v != (int32_t)v) { + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_QWORD); + int64_t v64 = v; wb_push(wb, &n, 1); - wb_push(wb, &v, 4); + wb_push(wb, &v64, sizeof(v64)); + } else if (v < 0) { + int32_t v32 = (int32_t)v; + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD); + wb_push(wb, &n, 1); + wb_push(wb, &v32, sizeof(v32)); } else if (v<0x100) { - uint8_t n = COMBINE_TYPE(type , 1); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_BYTE); wb_push(wb, &n, 1); uint8_t byte = (uint8_t)v; - wb_push(wb, &byte, 1); + wb_push(wb, &byte, sizeof(byte)); } else if (v<0x10000) { - uint8_t n = COMBINE_TYPE(type , 2); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_WORD); wb_push(wb, &n, 1); uint16_t word = (uint16_t)v; - wb_push(wb, &word, 2); + wb_push(wb, &word, sizeof(word)); } else { - uint8_t n = COMBINE_TYPE(type , 4); + uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD); wb_push(wb, &n, 1); - wb_push(wb, &v, 4); + uint32_t v32 = (uint32_t)v; + wb_push(wb, &v32, sizeof(v32)); } } static inline void -wb_number(struct write_block *wb, double v) { - uint8_t n = COMBINE_TYPE(TYPE_NUMBER , 8); +wb_real(struct write_block *wb, double v) { + uint8_t n = COMBINE_TYPE(TYPE_NUMBER , TYPE_NUMBER_REAL); wb_push(wb, &n, 1); - wb_push(wb, &v, 8); + wb_push(wb, &v, sizeof(v)); } static inline void @@ -203,7 +218,7 @@ wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) { if (array_size >= MAX_COOKIE-1) { uint8_t n = COMBINE_TYPE(TYPE_TABLE, MAX_COOKIE-1); wb_push(wb, &n, 1); - wb_integer(wb, array_size,TYPE_NUMBER); + wb_integer(wb, array_size); } else { uint8_t n = COMBINE_TYPE(TYPE_TABLE, array_size); wb_push(wb, &n, 1); @@ -224,11 +239,12 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a lua_pushnil(L); while (lua_next(L, index) != 0) { if (lua_type(L,-2) == LUA_TNUMBER) { - lua_Number k = lua_tonumber(L,-2); - int32_t x = (int32_t)lua_tointeger(L,-2); - if (k == (lua_Number)x && x>0 && x<=array_size) { - lua_pop(L,1); - continue; + if (lua_isinteger(L, -2)) { + lua_Integer x = lua_tointeger(L,-2); + if (x>0 && x<=array_size) { + lua_pop(L,1); + continue; + } } } pack_one(L,wb,-2,depth); @@ -259,12 +275,12 @@ pack_one(lua_State *L, struct write_block *b, int index, int depth) { wb_nil(b); break; case LUA_TNUMBER: { - int32_t x = (int32_t)lua_tointeger(L,index); - lua_Number n = lua_tonumber(L,index); - if ((lua_Number)x==n) { - wb_integer(b, x, TYPE_NUMBER); + if (lua_isinteger(L, index)) { + lua_Integer x = lua_tointeger(L,index); + wb_integer(b, x); } else { - wb_number(b,n); + lua_Number n = lua_tonumber(L,index); + wb_real(b,n); } break; } @@ -310,31 +326,42 @@ invalid_stream_line(lua_State *L, struct read_block *rb, int line) { #define invalid_stream(L,rb) invalid_stream_line(L,rb,__LINE__) -static int +static lua_Integer get_integer(lua_State *L, struct read_block *rb, int cookie) { switch (cookie) { - case 0: + case TYPE_NUMBER_ZERO: return 0; - case 1: { - uint8_t n = 0; - uint8_t * pn = rb_read(rb,&n,1); + case TYPE_NUMBER_BYTE: { + uint8_t n; + uint8_t * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); - return *pn; + n = *pn; + return n; } - case 2: { - uint16_t n = 0; - uint16_t * pn = rb_read(rb,&n,2); + case TYPE_NUMBER_WORD: { + uint16_t n; + uint16_t * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); - return *pn; + memcpy(&n, pn, sizeof(n)); + return n; } - case 4: { - int n = 0; - int * pn = rb_read(rb,&n,4); + case TYPE_NUMBER_DWORD: { + int32_t n; + int32_t * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); - return *pn; + memcpy(&n, pn, sizeof(n)); + return n; + } + case TYPE_NUMBER_QWORD: { + int64_t n; + int64_t * pn = rb_read(rb,sizeof(n)); + if (pn == NULL) + invalid_stream(L,rb); + memcpy(&n, pn, sizeof(n)); + return n; } default: invalid_stream(L,rb); @@ -343,32 +370,29 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) { } static double -get_number(lua_State *L, struct read_block *rb, int cookie) { - if (cookie == 8) { - double n = 0; - double * pn = rb_read(rb,&n,8); - if (pn == NULL) - invalid_stream(L,rb); - return *pn; - } else { - return (double)get_integer(L,rb,cookie); - } +get_real(lua_State *L, struct read_block *rb) { + double n; + double * pn = rb_read(rb,sizeof(n)); + if (pn == NULL) + invalid_stream(L,rb); + memcpy(&n, pn, sizeof(n)); + return n; } static void * get_pointer(lua_State *L, struct read_block *rb) { void * userdata = 0; - void ** v = (void **)rb_read(rb,&userdata,sizeof(userdata)); + void ** v = (void **)rb_read(rb,sizeof(userdata)); if (v == NULL) { invalid_stream(L,rb); } - return *v; + memcpy(&userdata, v, sizeof(userdata)); + return userdata; } static void get_buffer(lua_State *L, struct read_block *rb, int len) { - char tmp[len]; - char * p = rb_read(rb,tmp,len); + char * p = rb_read(rb,len); if (p == NULL) { invalid_stream(L,rb); } @@ -380,12 +404,17 @@ static void unpack_one(lua_State *L, struct read_block *rb); static void unpack_table(lua_State *L, struct read_block *rb, int array_size) { if (array_size == MAX_COOKIE-1) { - uint8_t type = 0; - uint8_t *t = rb_read(rb, &type, 1); - if (t==NULL || (*t & 7) != TYPE_NUMBER) { + uint8_t type; + uint8_t *t = rb_read(rb, sizeof(type)); + if (t==NULL) { invalid_stream(L,rb); } - array_size = (int)get_number(L,rb,*t >> 3); + type = *t; + int cookie = type >> 3; + if ((type & 7) != TYPE_NUMBER || cookie == TYPE_NUMBER_REAL) { + invalid_stream(L,rb); + } + array_size = get_integer(L,rb,cookie); } lua_createtable(L,array_size,0); int i; @@ -414,7 +443,11 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) { lua_pushboolean(L,cookie); break; case TYPE_NUMBER: - lua_pushnumber(L,get_number(L,rb,cookie)); + if (cookie == TYPE_NUMBER_REAL) { + lua_pushnumber(L,get_real(L,rb)); + } else { + lua_pushinteger(L, get_integer(L, rb, cookie)); + } break; case TYPE_USERDATA: lua_pushlightuserdata(L,get_pointer(L,rb)); @@ -423,22 +456,25 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) { get_buffer(L,rb,cookie); break; case TYPE_LONG_STRING: { - uint32_t len; if (cookie == 2) { - uint16_t *plen = rb_read(rb, &len, 2); + uint16_t *plen = rb_read(rb, 2); if (plen == NULL) { invalid_stream(L,rb); } - get_buffer(L,rb,(int)*plen); + uint16_t n; + memcpy(&n, plen, sizeof(n)); + get_buffer(L,rb,n); } else { if (cookie != 4) { invalid_stream(L,rb); } - uint32_t *plen = rb_read(rb, &len, 4); + uint32_t *plen = rb_read(rb, 4); if (plen == NULL) { invalid_stream(L,rb); } - get_buffer(L,rb,(int)*plen); + uint32_t n; + memcpy(&n, plen, sizeof(n)); + get_buffer(L,rb,n); } break; } @@ -455,12 +491,13 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) { static void unpack_one(lua_State *L, struct read_block *rb) { - uint8_t type = 0; - uint8_t *t = rb_read(rb, &type, 1); + uint8_t type; + uint8_t *t = rb_read(rb, sizeof(type)); if (t==NULL) { invalid_stream(L, rb); } - push_value(L, rb, *t & 0x7, *t>>3); + type = *t; + push_value(L, rb, type & 0x7, type>>3); } static void @@ -516,10 +553,11 @@ _luaseri_unpack(lua_State *L) { lua_checkstack(L,i); } uint8_t type = 0; - uint8_t *t = rb_read(&rb, &type, 1); + uint8_t *t = rb_read(&rb, sizeof(type)); if (t==NULL) break; - push_value(L, &rb, *t & 0x7, *t>>3); + type = *t; + push_value(L, &rb, type & 0x7, type>>3); } // Need not free buffer diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 950d3f50..f0eb046f 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -1,5 +1,3 @@ -// build: gcc -O2 -Wall --shared -o conf.so luaconf.c - #include #include #include @@ -11,15 +9,17 @@ #define KEYTYPE_STRING 1 #define VALUETYPE_NIL 0 -#define VALUETYPE_NUMBER 1 +#define VALUETYPE_REAL 1 #define VALUETYPE_STRING 2 #define VALUETYPE_BOOLEAN 3 #define VALUETYPE_TABLE 4 +#define VALUETYPE_INTEGER 5 struct table; union value { lua_Number n; + lua_Integer d; struct table * tbl; int string; int boolean; @@ -71,11 +71,10 @@ countsize(lua_State *L, int sizearray) { int type = lua_type(L, -2); ++n; if (type == LUA_TNUMBER) { - lua_Number key = lua_tonumber(L, -2); - int nkey = (int)key; - if ((lua_Number)nkey != key) { - luaL_error(L, "Invalid key %f", key); + if (!lua_isinteger(L, -2)) { + luaL_error(L, "Invalid key %f", lua_tonumber(L, -2)); } + lua_Integer nkey = lua_tointeger(L, -2); if (nkey > 0 && nkey <= sizearray) { --n; } @@ -129,8 +128,13 @@ setvalue(struct context * ctx, lua_State *L, int index, struct node *n) { n->valuetype = VALUETYPE_NIL; break; case LUA_TNUMBER: - n->v.n = lua_tonumber(L, index); - n->valuetype = VALUETYPE_NUMBER; + if (lua_isinteger(L, index)) { + n->v.d = lua_tointeger(L, index); + n->valuetype = VALUETYPE_INTEGER; + } else { + n->v.n = lua_tonumber(L, index); + n->valuetype = VALUETYPE_REAL; + } break; case LUA_TSTRING: { size_t sz = 0; @@ -468,9 +472,12 @@ ldeleteconf(lua_State *L) { static void pushvalue(lua_State *L, lua_State *sL, uint8_t vt, union value *v) { switch(vt) { - case VALUETYPE_NUMBER: + case VALUETYPE_REAL: lua_pushnumber(L, v->n); break; + case VALUETYPE_INTEGER: + lua_pushinteger(L, v->d); + break; case VALUETYPE_STRING: { size_t sz = 0; const char *str = lua_tolstring(sL, v->string, &sz); @@ -530,16 +537,15 @@ lindexconf(lua_State *L) { size_t sz = 0; const char * str = NULL; if (kt == LUA_TNUMBER) { - lua_Number k = lua_tonumber(L, 2); - key = (int)k; - if ((lua_Number)key != k) { - return luaL_error(L, "Invalid key %f", k); + if (!lua_isinteger(L, 2)) { + return luaL_error(L, "Invalid key %f", lua_tonumber(L, 2)); } + lua_Integer key = lua_tointeger(L, 2); if (key > 0 && key <= tbl->sizearray) { --key; pushvalue(L, tbl->L, tbl->arraytype[key], &tbl->array[key]); return 1; - } + } keytype = KEYTYPE_INTEGER; keyhash = (uint32_t)key; } else { @@ -601,13 +607,12 @@ lnextkey(lua_State *L) { const char *str = NULL; int sizearray = tbl->sizearray; if (kt == LUA_TNUMBER) { - lua_Number k = lua_tonumber(L,2); - key = (int)k; - if ((lua_Number)key != k) { + if (!lua_isinteger(L, 2)) { return 0; } + lua_Integer key = lua_tointeger(L, 2); if (key > 0 && key <= sizearray) { - int i; + lua_Integer i; for (i=key;iarraytype[i] != VALUETYPE_NIL) { lua_pushinteger(L, i+1); From 92150dd2e9cc67710b5a9b1ca3127e558b813861 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 18:23:46 +0800 Subject: [PATCH 269/729] int64 library is removed --- lualib/redis.lua | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lualib/redis.lua b/lualib/redis.lua index 87069e28..b1e44185 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -1,7 +1,6 @@ local skynet = require "skynet" local socket = require "socket" local socketchannel = require "socketchannel" -local int64 = require "int64" local table = table local string = string @@ -102,12 +101,8 @@ local function pack_value(lines, v) return end - local t = type(v) - if t == "number" then - v = tostring(v) - elseif t == "userdata" then - v = int64.tostring(int64.new(v),10) - end + v = tostring(v) + table.insert(lines,"$"..#v) table.insert(lines,v) end From cb4129332006356ece02293745efb8a4d1759845 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 21:33:06 +0800 Subject: [PATCH 270/729] update mysql driver for lua 5.3 --- lualib/mysql.lua | 135 ++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 96 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index 58843e68..f78f529b 100755 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -2,27 +2,22 @@ -- Copyright (C) 2014 Chang Feng -- This file is modified version from https://github.com/openresty/lua-resty-mysql -- The license is under the BSD license. +-- Modified by Cloud Wu (remove bit32 for lua 5.3) local socketchannel = require "socketchannel" -local bit = require "bit32" local mysqlaux = require "mysqlaux.c" local crypt = require "crypt" local sub = string.sub +local strgsub = string.gsub +local strformat = string.format local strbyte = string.byte local strchar = string.char -local strfind = string.find local strrep = string.rep -local null = nil -local band = bit.band -local bxor = bit.bxor -local bor = bit.bor -local lshift = bit.lshift -local rshift = bit.rshift +local strunpack = string.unpack +local strpack = string.pack local sha1= crypt.sha1 -local concat = table.concat -local unpack = unpack local setmetatable = setmetatable local error = error local tonumber = tonumber @@ -53,105 +48,53 @@ for i = 0x01, 0x05 do -- tiny, short, long, float, double converters[i] = tonumber end --- converters[0x08] = tonumber -- long long +converters[0x08] = tonumber -- long long converters[0x09] = tonumber -- int24 converters[0x0d] = tonumber -- year converters[0xf6] = tonumber -- newdecimal local function _get_byte2(data, i) - local a, b = strbyte(data, i, i + 1) - return bor(a, lshift(b, 8)), i + 2 + return strunpack(" Date: Mon, 5 Jan 2015 21:38:41 +0800 Subject: [PATCH 271/729] update sprotoparser for lua 5.3 --- lualib/sprotoparser.lua | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index ff6960c2..cf65e536 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -1,5 +1,4 @@ local lpeg = require "lpeg" -local bit32 = require "bit32" local table = require "table" local P = lpeg.P @@ -196,18 +195,14 @@ end ]] local function packbytes(str) - local size = #str - return string.char(bit32.extract(size,0,8)).. - string.char(bit32.extract(size,8,8)).. - string.char(bit32.extract(size,16,8)).. - string.char(bit32.extract(size,24,8)).. - str + str = string.pack("=0 and id < 65536) - return string.char(bit32.extract(id, 0, 8)) .. string.char(bit32.extract(id, 8, 8)) + local str = string.pack(" Date: Mon, 5 Jan 2015 21:55:26 +0800 Subject: [PATCH 272/729] use lua 5.3 in examples client --- README.md | 8 +++----- examples/client.lua | 10 +++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ceaa4d56..d438b558 100644 --- a/README.md +++ b/README.md @@ -23,16 +23,14 @@ Run these in different console ``` ./skynet examples/config # Launch first skynet node (Gate server) and a skynet-master (see config for standalone option) -lua examples/client.lua # Launch a client, and try to input some words. +./3rd/lua/lua examples/client.lua # Launch a client, and try to input hello. ``` ## About Lua -Skynet put a modified version of lua 5.2.3 in 3rd/lua , it can share proto type between lua state (http://lua-users.org/lists/lua-l/2014-03/msg00489.html) . +Skynet now use lua 5.3.0-rc3 , http://www.lua.org/work/lua-5.3.0-rc3.tar.gz -Each lua file only load once and cache it in memory during skynet start . so if you want to reflush the cache , call skynet.cache.clear() . - -You can also use the offical lua version , edit the makefile by yourself . +You can also use the other offical lua version , edit the makefile by yourself . ## How To (in Chinese) diff --git a/examples/client.lua b/examples/client.lua index e9efc160..a17b1926 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -1,8 +1,11 @@ package.cpath = "luaclib/?.so" package.path = "lualib/?.lua;examples/?.lua" +if _VERSION ~= "Lua 5.3" then + error "Use lua 5.3" +end + local socket = require "clientsocket" -local bit32 = require "bit32" local proto = require "proto" local sproto = require "sproto" @@ -13,10 +16,7 @@ local fd = assert(socket.connect("127.0.0.1", 8888)) local function send_package(fd, pack) local size = #pack - local package = string.char(bit32.extract(size,8,8)) .. - string.char(bit32.extract(size,0,8)).. - pack - + local package = string.pack(">s2", pack) socket.send(fd, package) end From 309e26ba7ae2cd6d24d6f222490c7da92508398c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 21:58:50 +0800 Subject: [PATCH 273/729] we don't need __ipairs in lua 5.3 --- lualib/sharedata/corelib.lua | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index 789e77ad..90dc1f94 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -91,24 +91,6 @@ function meta:__len() return len(getcobj(self)) end -local function conf_ipairs(self, index) - local obj = getcobj(self) - index = index + 1 - local value = rawget(self, index) - if value then - return index, value - end - local sz = len(obj) - if sz < index then - return - end - return index, self[index] -end - -function meta:__ipairs() - return conf_ipairs, self, 0 -end - function meta:__pairs() return conf.next, self, nil end From 8737531fbed4a5d718e6eaa266d7ed92d3ac3837 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jan 2015 22:12:26 +0800 Subject: [PATCH 274/729] remove unused line --- examples/client.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/client.lua b/examples/client.lua index a17b1926..effee9c7 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -15,7 +15,6 @@ local request = host:attach(sproto.new(proto.c2s)) local fd = assert(socket.connect("127.0.0.1", 8888)) local function send_package(fd, pack) - local size = #pack local package = string.pack(">s2", pack) socket.send(fd, package) end From 29822f14bd6b78bbf648724b2554c33a5215c79d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Jan 2015 15:38:34 +0800 Subject: [PATCH 275/729] update to lua 5.3 rc4 --- 3rd/lua/Makefile | 2 +- 3rd/lua/README | 2 +- 3rd/lua/linit.c | 10 ++++++---- 3rd/lua/loadlib.c | 16 ++++++++++++++-- 3rd/lua/lobject.h | 4 ++-- 3rd/lua/lopcodes.c | 4 +++- 3rd/lua/ltable.c | 4 ++-- 3rd/lua/luac.c | 17 +++++++++++------ 8 files changed, 40 insertions(+), 19 deletions(-) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 4be6dc13..2e7a4120 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -6,7 +6,7 @@ # Your platform. See PLATS for possible values. PLAT= none -CC= gcc -std=c99 +CC= gcc -std=gnu99 CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= -lm $(SYSLIBS) $(MYLIBS) diff --git a/3rd/lua/README b/3rd/lua/README index 0da6217c..4ed252ff 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,3 +1,3 @@ -Lua 5.3.0 rc3 http://www.lua.org/work/lua-5.3.0-rc3.tar.gz +Lua 5.3.0 rc4 http://www.lua.org/work/lua-5.3.0-rc4.tar.gz It will be update to final version when lua 5.3.0 released. diff --git a/3rd/lua/linit.c b/3rd/lua/linit.c index ca9d100d..8ce94ccb 100644 --- a/3rd/lua/linit.c +++ b/3rd/lua/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.37 2014/12/09 15:00:17 roberto Exp $ +** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ @@ -8,9 +8,6 @@ #define linit_c #define LUA_LIB -#include "lprefix.h" - - /* ** If you embed Lua in your program and need to open the standard ** libraries, call luaL_openlibs in your program. If you need a @@ -27,6 +24,11 @@ ** lua_pop(L, 1); // remove _PRELOAD table */ +#include "lprefix.h" + + +#include + #include "lua.h" #include "lualib.h" diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index 1dab9bd0..7f8d9902 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.123 2014/11/12 13:31:51 roberto Exp $ +** $Id: loadlib.c,v 1.124 2015/01/05 13:51:39 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -135,6 +135,18 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); #include +/* +** Macro to covert pointer to void* to pointer to function. This cast +** is undefined according to ISO C, but POSIX assumes that it must work. +** (The '__extension__' in gnu compilers is only to avoid warnings.) +*/ +#if defined(__GNUC__) +#define cast_func(p) (__extension__ (lua_CFunction)(p)) +#else +#define cast_func(p) ((lua_CFunction)(p)) +#endif + + static void lsys_unloadlib (void *lib) { dlclose(lib); } @@ -148,7 +160,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) { static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)dlsym(lib, sym); + lua_CFunction f = cast_func(dlsym(lib, sym)); if (f == NULL) lua_pushstring(L, dlerror()); return f; } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 77b4e470..d7d0ebf3 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.105 2014/12/19 13:36:32 roberto Exp $ +** $Id: lobject.h,v 2.106 2015/01/05 13:52:37 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -473,7 +473,7 @@ typedef union TKey { /* copy a value into a key without messing up field 'next' */ -#define setkey(L,key,obj) \ +#define setnodekey(L,key,obj) \ { TKey *k_=(key); const TValue *io_=(obj); \ k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \ (void)L; checkliveness(G(L),io_); } diff --git a/3rd/lua/lopcodes.c b/3rd/lua/lopcodes.c index 8e2e6da3..a1cbef85 100644 --- a/3rd/lua/lopcodes.c +++ b/3rd/lua/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.54 2014/11/02 19:19:04 roberto Exp $ +** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -10,6 +10,8 @@ #include "lprefix.h" +#include + #include "lopcodes.h" diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index e8ef146c..38be0051 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.99 2014/11/02 19:19:04 roberto Exp $ +** $Id: ltable.c,v 2.100 2015/01/05 13:52:37 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -484,7 +484,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { mp = f; } } - setkey(L, &mp->i_key, key); + setnodekey(L, &mp->i_key, key); luaC_barrierback(L, t, key); lua_assert(ttisnil(gval(mp))); return gval(mp); diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index a8d2a31b..c565f46a 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -1,5 +1,5 @@ /* -** $Id: luac.c,v 1.71 2014/11/26 12:08:59 lhf Exp $ +** $Id: luac.c,v 1.72 2015/01/06 03:09:13 lhf Exp $ ** Lua compiler (saves bytecodes to files; also lists bytecodes) ** See Copyright Notice in lua.h */ @@ -50,14 +50,14 @@ static void cannot(const char* what) static void usage(const char* message) { if (*message=='-') - fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); + fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message); else fprintf(stderr,"%s: %s\n",progname,message); fprintf(stderr, "usage: %s [options] [filenames]\n" "Available options are:\n" " -l list (use -l -l for full listing)\n" - " -o name output to file " LUA_QL("name") " (default is \"%s\")\n" + " -o name output to file 'name' (default is \"%s\")\n" " -p parse only\n" " -s strip debug information\n" " -v show version information\n" @@ -92,7 +92,7 @@ static int doargs(int argc, char* argv[]) { output=argv[++i]; if (output==NULL || *output==0 || (*output=='-' && output[1]!=0)) - usage(LUA_QL("-o") " needs argument"); + usage("'-o' needs argument"); if (IS("-")) output=NULL; } else if (IS("-p")) /* parse only */ @@ -206,7 +206,7 @@ int main(int argc, char* argv[]) } /* -** $Id: print.c,v 1.74 2014/07/21 01:41:45 lhf Exp $ +** $Id: print.c,v 1.76 2015/01/05 16:12:50 lhf Exp $ ** print bytecodes ** See Copyright Notice in lua.h */ @@ -263,8 +263,13 @@ static void PrintConstant(const Proto* f, int i) printf(bvalue(o) ? "true" : "false"); break; case LUA_TNUMFLT: - printf(LUA_NUMBER_FMT,fltvalue(o)); + { + char buff[100]; + sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); + printf("%s",buff); + if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); break; + } case LUA_TNUMINT: printf(LUA_INTEGER_FMT,ivalue(o)); break; From 4156933e0379c6cf95b65f565b5cf426aac086b6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 13 Jan 2015 11:09:16 +0800 Subject: [PATCH 276/729] update lua 5.3 final --- 3rd/lua/README | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/3rd/lua/README b/3rd/lua/README index 4ed252ff..1daa6438 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,3 +1,2 @@ -Lua 5.3.0 rc4 http://www.lua.org/work/lua-5.3.0-rc4.tar.gz +Copy from Lua 5.3.0 final http://www.lua.org/ftp/lua-5.3.0.tar.gz -It will be update to final version when lua 5.3.0 released. From 72d665ca44a8350f3d6b7f149ab99929f99d34a5 Mon Sep 17 00:00:00 2001 From: apppur Date: Tue, 20 Jan 2015 16:26:18 +0800 Subject: [PATCH 277/729] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d438b558..26bb18c2 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Run these in different console ## About Lua -Skynet now use lua 5.3.0-rc3 , http://www.lua.org/work/lua-5.3.0-rc3.tar.gz +Skynet now use lua 5.3.0 final, http://www.lua.org/ftp/lua-5.3.0.tar.gz You can also use the other offical lua version , edit the makefile by yourself . From 9e8d4a453b00d524f290c347ec390664041d44a2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 20 Jan 2015 21:59:08 +0800 Subject: [PATCH 278/729] lua 5.3 returns type for lua_rawget --- lualib-src/lua-bson.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 0a0e833f..ba97f841 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -775,8 +775,7 @@ lreplace(lua_State *L) { return luaL_error(L, "call makeindex first"); } lua_pushvalue(L,2); - lua_rawget(L, -2); - if (!lua_isnumber(L,-1)) { + if (lua_rawget(L, -2) != LUA_TNUMBER) { return luaL_error(L, "Can't replace key : %s", lua_tostring(L,2)); } int id = lua_tointeger(L, -1); From 93da2dbfc9702314b2f39c96b43f4e1126057e51 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 21 Jan 2015 18:54:05 +0800 Subject: [PATCH 279/729] use integer for service address --- lualib-src/lua-skynet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 9dc658ad..ca3476d1 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -45,7 +45,7 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t lua_pushlightuserdata(L, (void *)msg); lua_pushinteger(L,sz); lua_pushinteger(L, session); - lua_pushnumber(L, source); + lua_pushinteger(L, source); r = lua_pcall(L, 5, 0 , trace); From 9f0274b5fe3fe976218a1d2d632a95198ae06108 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 21 Jan 2015 20:01:26 +0800 Subject: [PATCH 280/729] typo --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index ac7426ae..bf2a6929 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -v0.9.3 (2014-1-5) +v0.9.3 (2015-1-5) ----------- * Add : mongo createIndex * Update : sproto From b891146397369e892cc896fa3fc0252611a7d1ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 22 Jan 2015 14:13:26 +0800 Subject: [PATCH 281/729] add lua_clonefunction for lua 5.3 --- 3rd/lua/lapi.c | 56 ++++++++++++++++++++++++++++++++++- 3rd/lua/lcode.c | 36 +++++++++++------------ 3rd/lua/lcode.h | 2 +- 3rd/lua/ldebug.c | 24 +++++++-------- 3rd/lua/ldebug.h | 4 +-- 3rd/lua/ldo.c | 4 +-- 3rd/lua/ldump.c | 21 ++++++------- 3rd/lua/lfunc.c | 62 ++++++++++++++++++++++++--------------- 3rd/lua/lfunc.h | 2 +- 3rd/lua/lgc.c | 32 ++++++++++++-------- 3rd/lua/lobject.h | 21 ++++++++----- 3rd/lua/lparser.c | 75 ++++++++++++++++++++++++----------------------- 3rd/lua/luac.c | 28 ++++++++++-------- 3rd/lua/lundump.c | 23 ++++++++------- 3rd/lua/lvm.c | 16 +++++----- 15 files changed, 246 insertions(+), 160 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index fbfafa30..bf5e829f 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -28,6 +28,7 @@ #include "ltm.h" #include "lundump.h" #include "lvm.h" +#include "lfunc.h" @@ -984,6 +985,59 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, } +static Proto * cloneproto (lua_State *L, const Proto *src) { + /* copy constants and nested proto */ + int i,n; + Proto *f = luaF_newproto(L, src->sp); + n = src->sp->sizek; + f->k=luaM_newvector(L,n,TValue); + for (i=0; ik[i]); + for (i=0; ik[i]; + TValue *o=&f->k[i]; + if (ttisstring(s)) { + TString * str = luaS_newlstr(L,svalue(s),tsvalue(s)->len); + setsvalue2n(L,o,str); + } else { + setobj(L,o,s); + } + } + n = src->sp->sizep; + f->p=luaM_newvector(L,n,struct Proto *); + for (i=0; ip[i]=NULL; + for (i=0; ip[i]=cloneproto(L, src->p[i]); + } + return f; +} + +LUA_API void lua_clonefunction (lua_State *L, void * fp) { + LClosure *cl; + LClosure *f = cast(LClosure *, fp); + lua_lock(L); + if (f->p->sp->l_G == G(L)) { + setclLvalue(L,L->top,f); + api_incr_top(L); + lua_unlock(L); + return; + } + cl = luaF_newLclosure(L,f->nupvalues); + cl->p = cloneproto(L, f->p); + setclLvalue(L,L->top,cl); + api_incr_top(L); + luaF_initupvals(L, cl); + + if (f->nupvalues >= 1) { /* does it have an upvalue? */ + /* get global table from registry */ + Table *reg = hvalue(&G(L)->l_registry); + const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); + /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ + setobj(L, f->upvals[0]->v, gt); + luaC_upvalbarrier(L, f->upvals[0]); + } + lua_unlock(L); +} + LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; TValue *o; @@ -1178,7 +1232,7 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val, case LUA_TLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; - Proto *p = f->p; + SharedProto *p = f->p->sp; if (!(1 <= n && n <= p->sizeupvalues)) return NULL; *val = f->upvals[n-1]->v; if (uv) *uv = f->upvals[n - 1]; diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 5e34624b..0f0a6752 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -55,7 +55,7 @@ void luaK_nil (FuncState *fs, int from, int n) { Instruction *previous; int l = from + n - 1; /* last register to set nil */ if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - previous = &fs->f->code[fs->pc-1]; + previous = &fs->f->sp->code[fs->pc-1]; if (GET_OPCODE(*previous) == OP_LOADNIL) { int pfrom = GETARG_A(*previous); int pl = pfrom + GETARG_B(*previous); @@ -95,7 +95,7 @@ static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->code[pc]; + Instruction *jmp = &fs->f->sp->code[pc]; int offset = dest-(pc+1); lua_assert(dest != NO_JUMP); if (abs(offset) > MAXARG_sBx) @@ -115,7 +115,7 @@ int luaK_getlabel (FuncState *fs) { static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->code[pc]); + int offset = GETARG_sBx(fs->f->sp->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ return NO_JUMP; /* end of list */ else @@ -124,7 +124,7 @@ static int getjump (FuncState *fs, int pc) { static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->code[pc]; + Instruction *pi = &fs->f->sp->code[pc]; if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) return pi-1; else @@ -197,10 +197,10 @@ void luaK_patchclose (FuncState *fs, int list, int level) { level++; /* argument is +1 to reserve 0 as non-op */ while (list != NO_JUMP) { int next = getjump(fs, list); - lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && - (GETARG_A(fs->f->code[list]) == 0 || - GETARG_A(fs->f->code[list]) >= level)); - SETARG_A(fs->f->code[list], level); + lua_assert(GET_OPCODE(fs->f->sp->code[list]) == OP_JMP && + (GETARG_A(fs->f->sp->code[list]) == 0 || + GETARG_A(fs->f->sp->code[list]) >= level)); + SETARG_A(fs->f->sp->code[list], level); list = next; } } @@ -230,13 +230,13 @@ static int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; dischargejpc(fs); /* 'pc' will change */ /* put new instruction in code array */ - luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, + luaM_growvector(fs->ls->L, f->sp->code, fs->pc, f->sp->sizecode, Instruction, MAX_INT, "opcodes"); - f->code[fs->pc] = i; + f->sp->code[fs->pc] = i; /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, + luaM_growvector(fs->ls->L, f->sp->lineinfo, fs->pc, f->sp->sizelineinfo, int, MAX_INT, "opcodes"); - f->lineinfo[fs->pc] = fs->ls->lastline; + f->sp->lineinfo[fs->pc] = fs->ls->lastline; return fs->pc++; } @@ -277,10 +277,10 @@ int luaK_codek (FuncState *fs, int reg, int k) { void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; - if (newstack > fs->f->maxstacksize) { + if (newstack > fs->f->sp->maxstacksize) { if (newstack >= MAXREGS) luaX_syntaxerror(fs->ls, "function or expression too complex"); - fs->f->maxstacksize = cast_byte(newstack); + fs->f->sp->maxstacksize = cast_byte(newstack); } } @@ -322,13 +322,13 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { return k; /* reuse index */ } /* constant not found; create a new entry */ - oldsize = f->sizek; + oldsize = f->sp->sizek; k = fs->nk; /* numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setivalue(idx, k); - luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); + luaM_growvector(L, f->k, k, f->sp->sizek, TValue, MAXARG_Ax, "constants"); + while (oldsize < f->sp->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); fs->nk++; luaC_barrier(L, f, v); @@ -933,7 +933,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, void luaK_fixline (FuncState *fs, int line) { - fs->f->lineinfo[fs->pc - 1] = line; + fs->f->sp->lineinfo[fs->pc - 1] = line; } diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 43ab86db..b4c865f9 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -40,7 +40,7 @@ typedef enum BinOpr { typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; -#define getcode(fs,e) ((fs)->f->code[(e)->u.info]) +#define getcode(fs,e) ((fs)->f->sp->code[(e)->u.info]) #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 6986bf0f..ab10dd99 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -98,14 +98,14 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { static const char *upvalname (Proto *p, int uv) { - TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); + TString *s = check_exp(uv < p->sizeupvalues, p->sp->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); } static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - int nparams = clLvalue(ci->func)->p->numparams; + int nparams = clLvalue(ci->func)->p->sp->numparams; if (n >= ci->u.l.base - ci->func - nparams) return NULL; /* no such vararg */ else { @@ -184,7 +184,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { ar->what = "C"; } else { - Proto *p = cl->l.p; + SharedProto *p = cl->l.p->sp; ar->source = p->source ? getstr(p->source) : "=?"; ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; @@ -202,12 +202,12 @@ static void collectvalidlines (lua_State *L, Closure *f) { else { int i; TValue v; - int *lineinfo = f->l.p->lineinfo; + int *lineinfo = f->l.p->sp->lineinfo; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue(L, L->top, t); /* push it on stack */ api_incr_top(L); setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ + for (i = 0; i < f->l.p->sp->sizelineinfo; i++) /* for all lines with code */ luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ } } @@ -233,8 +233,8 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, ar->nparams = 0; } else { - ar->isvararg = f->l.p->is_vararg; - ar->nparams = f->l.p->numparams; + ar->isvararg = f->l.p->sp->is_vararg; + ar->nparams = f->l.p->sp->numparams; } break; } @@ -343,7 +343,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) { int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->code[pc]; + Instruction i = p->sp->code[pc]; OpCode op = GET_OPCODE(i); int a = GETARG_A(i); switch (op) { @@ -393,7 +393,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, /* else try symbolic execution */ pc = findsetreg(p, lastpc, reg); if (pc != -1) { /* could find instruction? */ - Instruction i = p->code[pc]; + Instruction i = p->sp->code[pc]; OpCode op = GET_OPCODE(i); switch (op) { case OP_MOVE: { @@ -419,7 +419,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, case OP_LOADK: case OP_LOADKX: { int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); + : GETARG_Ax(p->sp->code[pc + 1]); if (ttisstring(&p->k[b])) { *name = svalue(&p->k[b]); return "constant"; @@ -442,7 +442,7 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { TMS tm = (TMS)0; /* to avoid warnings */ Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ - Instruction i = p->code[pc]; /* calling instruction */ + Instruction i = p->sp->code[pc]; /* calling instruction */ if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; @@ -577,7 +577,7 @@ static void addinfo (lua_State *L, const char *msg) { if (isLua(ci)) { /* is Lua code? */ char buff[LUA_IDSIZE]; /* add file:line information */ int line = currentline(ci); - TString *src = ci_func(ci)->p->source; + TString *src = ci_func(ci)->p->sp->source; if (src) luaO_chunkid(buff, getstr(src), LUA_IDSIZE); else { /* no source available; use "?" instead */ diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 0d8966ca..916ce308 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -11,9 +11,9 @@ #include "lstate.h" -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) +#define pcRel(pc, p) (cast(int, (pc) - (p)->sp->code) - 1) -#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) +#define getfuncline(f,pc) (((f)->sp->lineinfo) ? (f)->sp->lineinfo[pc] : -1) #define resethookcount(L) (L->hookcount = L->basehookcount) diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 6159e51d..227a8b82 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -269,7 +269,7 @@ static void callhook (lua_State *L, CallInfo *ci) { } -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { +static StkId adjust_varargs (lua_State *L, SharedProto *p, int actual) { int i; int nfixargs = p->numparams; StkId base, fixed; @@ -342,7 +342,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { } case LUA_TLCL: { /* Lua function: prepare its call */ StkId base; - Proto *p = clLvalue(func)->p; + SharedProto *p = clLvalue(func)->p->sp; n = cast_int(L->top - func) - 1; /* number of real arguments */ luaD_checkstack(L, p->maxstacksize); for (; n < p->numparams; n++) diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index b6c7114f..c4efac16 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -86,7 +86,7 @@ static void DumpString (const TString *s, DumpState *D) { } -static void DumpCode (const Proto *f, DumpState *D) { +static void DumpCode (const SharedProto *f, DumpState *D) { DumpInt(f->sizecode, D); DumpVector(f->code, f->sizecode, D); } @@ -96,7 +96,7 @@ static void DumpFunction(const Proto *f, TString *psource, DumpState *D); static void DumpConstants (const Proto *f, DumpState *D) { int i; - int n = f->sizek; + int n = f->sp->sizek; DumpInt(n, D); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; @@ -126,14 +126,14 @@ static void DumpConstants (const Proto *f, DumpState *D) { static void DumpProtos (const Proto *f, DumpState *D) { int i; - int n = f->sizep; + int n = f->sp->sizep; DumpInt(n, D); for (i = 0; i < n; i++) - DumpFunction(f->p[i], f->source, D); + DumpFunction(f->p[i], f->sp->source, D); } -static void DumpUpvalues (const Proto *f, DumpState *D) { +static void DumpUpvalues (const SharedProto *f, DumpState *D) { int i, n = f->sizeupvalues; DumpInt(n, D); for (i = 0; i < n; i++) { @@ -143,7 +143,7 @@ static void DumpUpvalues (const Proto *f, DumpState *D) { } -static void DumpDebug (const Proto *f, DumpState *D) { +static void DumpDebug (const SharedProto *f, DumpState *D) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; DumpInt(n, D); @@ -162,7 +162,8 @@ static void DumpDebug (const Proto *f, DumpState *D) { } -static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { +static void DumpFunction (const Proto *fp, TString *psource, DumpState *D) { + const SharedProto *f = fp->sp; if (D->strip || f->source == psource) DumpString(NULL, D); /* no debug info or same source as its parent */ else @@ -173,9 +174,9 @@ static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { DumpByte(f->is_vararg, D); DumpByte(f->maxstacksize, D); DumpCode(f, D); - DumpConstants(f, D); + DumpConstants(fp, D); DumpUpvalues(f, D); - DumpProtos(f, D); + DumpProtos(fp, D); DumpDebug(f, D); } @@ -207,7 +208,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, D.strip = strip; D.status = 0; DumpHeader(&D); - DumpByte(f->sizeupvalues, &D); + DumpByte(f->sp->sizeupvalues, &D); DumpFunction(f, NULL, &D); return D.status; } diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 67967dab..c1f2daf7 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -96,39 +96,52 @@ void luaF_close (lua_State *L, StkId level) { } -Proto *luaF_newproto (lua_State *L) { +Proto *luaF_newproto (lua_State *L, SharedProto *sp) { GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); Proto *f = gco2p(o); + f->sp = NULL; f->k = NULL; - f->sizek = 0; f->p = NULL; - f->sizep = 0; - f->code = NULL; f->cache = NULL; - f->sizecode = 0; - f->lineinfo = NULL; - f->sizelineinfo = 0; - f->upvalues = NULL; - f->sizeupvalues = 0; - f->numparams = 0; - f->is_vararg = 0; - f->maxstacksize = 0; - f->locvars = NULL; - f->sizelocvars = 0; - f->linedefined = 0; - f->lastlinedefined = 0; - f->source = NULL; + if (sp == NULL) { + sp = luaM_new(L, SharedProto); + sp->l_G = G(L); + sp->sizek = 0; + sp->sizep = 0; + sp->code = NULL; + sp->sizecode = 0; + sp->lineinfo = NULL; + sp->sizelineinfo = 0; + sp->upvalues = NULL; + sp->sizeupvalues = 0; + sp->numparams = 0; + sp->is_vararg = 0; + sp->maxstacksize = 0; + sp->locvars = NULL; + sp->sizelocvars = 0; + sp->linedefined = 0; + sp->lastlinedefined = 0; + sp->source = NULL; + } + f->sp = sp; return f; } +static void freesharedproto (lua_State *L, SharedProto *f) { + if (f == NULL || G(L) != f->l_G) + return; + luaM_freearray(L, f->code, f->sizecode); + luaM_freearray(L, f->lineinfo, f->sizelineinfo); + luaM_freearray(L, f->locvars, f->sizelocvars); + luaM_freearray(L, f->upvalues, f->sizeupvalues); + luaM_free(L, f); +} + void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->p, f->sizep); - luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); - luaM_freearray(L, f->locvars, f->sizelocvars); - luaM_freearray(L, f->upvalues, f->sizeupvalues); + luaM_freearray(L, f->p, f->sp->sizep); + luaM_freearray(L, f->k, f->sp->sizek); + freesharedproto(L, f->sp); luaM_free(L, f); } @@ -137,8 +150,9 @@ void luaF_freeproto (lua_State *L, Proto *f) { ** Look for n-th local variable at line 'line' in function 'func'. ** Returns NULL if not found. */ -const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { +const char *luaF_getlocalname (const Proto *fp, int local_number, int pc) { int i; + const SharedProto *f = fp->sp; for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { if (pc < f->locvars[i].endpc) { /* is variable active? */ local_number--; diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 256d3cf9..9d02a4b3 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -40,7 +40,7 @@ struct UpVal { #define upisopen(up) ((up)->v != &(up)->u.value) -LUAI_FUNC Proto *luaF_newproto (lua_State *L); +LUAI_FUNC Proto *luaF_newproto (lua_State *L, SharedProto *sp); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 8b95fb67..0be4866a 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -456,26 +456,32 @@ static lu_mem traversetable (global_State *g, Table *h) { sizeof(Node) * cast(size_t, sizenode(h)); } +static int marksharedproto (global_State *g, SharedProto *f) { + int i; + if (g != f->l_G) + return 0; + markobject(g, f->source); + for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ + markobject(g, f->upvalues[i].name); + for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ + markobject(g, f->locvars[i].varname); + return sizeof(Instruction) * f->sizecode + + sizeof(int) * f->sizelineinfo + + sizeof(LocVar) * f->sizelocvars + + sizeof(Upvaldesc) * f->sizeupvalues; +} static int traverseproto (global_State *g, Proto *f) { int i; if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */ - markobject(g, f->source); - for (i = 0; i < f->sizek; i++) /* mark literals */ + for (i = 0; i < f->sp->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); - for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobject(g, f->upvalues[i].name); - for (i = 0; i < f->sizep; i++) /* mark nested protos */ + for (i = 0; i < f->sp->sizep; i++) /* mark nested protos */ markobject(g, f->p[i]); - for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobject(g, f->locvars[i].varname); - return sizeof(Proto) + sizeof(Instruction) * f->sizecode + - sizeof(Proto *) * f->sizep + - sizeof(TValue) * f->sizek + - sizeof(int) * f->sizelineinfo + - sizeof(LocVar) * f->sizelocvars + - sizeof(Upvaldesc) * f->sizeupvalues; + return sizeof(Proto) + sizeof(Proto *) * f->sp->sizep + + sizeof(TValue) * f->sp->sizek + + marksharedproto(g, f->sp); } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index d7d0ebf3..01f29139 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -392,11 +392,7 @@ typedef struct LocVar { } LocVar; -/* -** Function Prototypes -*/ -typedef struct Proto { - CommonHeader; +typedef struct SharedProto { lu_byte numparams; /* number of fixed parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* maximum stack used by this function */ @@ -408,14 +404,23 @@ typedef struct Proto { int sizelocvars; int linedefined; int lastlinedefined; - TValue *k; /* constants used by the function */ + void *l_G; /* global state belongs to */ Instruction *code; - struct Proto **p; /* functions defined inside the function */ int *lineinfo; /* map from opcodes to source lines (debug information) */ LocVar *locvars; /* information about local variables (debug information) */ Upvaldesc *upvalues; /* upvalue information */ - struct LClosure *cache; /* last created closure with this prototype */ TString *source; /* used for debug information */ +} SharedProto; + +/* +** Function Prototypes +*/ +typedef struct Proto { + CommonHeader; + struct SharedProto *sp; + TValue *k; /* constants used by the function */ + struct Proto **p; /* functions defined inside the function */ + struct LClosure *cache; /* last created closure with this prototype */ GCObject *gclist; } Proto; diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 9a54dfc9..0bed0dd3 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -79,7 +79,7 @@ static l_noret error_expected (LexState *ls, int token) { static l_noret errorlimit (FuncState *fs, int limit, const char *what) { lua_State *L = fs->ls->L; const char *msg; - int line = fs->f->linedefined; + int line = fs->f->sp->linedefined; const char *where = (line == 0) ? "main function" : luaO_pushfstring(L, "function at line %d", line); @@ -160,13 +160,14 @@ static void checkname (LexState *ls, expdesc *e) { static int registerlocalvar (LexState *ls, TString *varname) { FuncState *fs = ls->fs; - Proto *f = fs->f; + Proto *fp = fs->f; + SharedProto *f = fp->sp; int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, f, varname); + luaC_objbarrier(ls->L, fp, varname); return fs->nlocvars++; } @@ -194,7 +195,7 @@ static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { static LocVar *getlocvar (FuncState *fs, int i) { int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; lua_assert(idx < fs->nlocvars); - return &fs->f->locvars[idx]; + return &fs->f->sp->locvars[idx]; } @@ -216,7 +217,7 @@ static void removevars (FuncState *fs, int tolevel) { static int searchupvalue (FuncState *fs, TString *name) { int i; - Upvaldesc *up = fs->f->upvalues; + Upvaldesc *up = fs->f->sp->upvalues; for (i = 0; i < fs->nups; i++) { if (eqstr(up[i].name, name)) return i; } @@ -225,7 +226,8 @@ static int searchupvalue (FuncState *fs, TString *name) { static int newupvalue (FuncState *fs, TString *name, expdesc *v) { - Proto *f = fs->f; + Proto *fp = fs->f; + SharedProto *f = fp->sp; int oldsize = f->sizeupvalues; checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, @@ -234,7 +236,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { f->upvalues[fs->nups].instack = (v->k == VLOCAL); f->upvalues[fs->nups].idx = cast_byte(v->u.info); f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, f, name); + luaC_objbarrier(fs->ls->L, fp, name); return fs->nups++; } @@ -496,12 +498,12 @@ static Proto *addprototype (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; /* prototype of current function */ - if (fs->np >= f->sizep) { - int oldsize = f->sizep; - luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sizep) f->p[oldsize++] = NULL; + if (fs->np >= f->sp->sizep) { + int oldsize = f->sp->sizep; + luaM_growvector(L, f->p, fs->np, f->sp->sizep, Proto *, MAXARG_Bx, "functions"); + while (oldsize < f->sp->sizep) f->p[oldsize++] = NULL; } - f->p[fs->np++] = clp = luaF_newproto(L); + f->p[fs->np++] = clp = luaF_newproto(L, NULL); luaC_objbarrier(L, f, clp); return clp; } @@ -521,7 +523,7 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - Proto *f; + SharedProto *f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; ls->fs = fs; @@ -536,7 +538,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nactvar = 0; fs->firstlocal = ls->dyd->actvar.n; fs->bl = NULL; - f = fs->f; + f = fs->f->sp; f->source = ls->source; f->maxstacksize = 2; /* registers 0/1 are always valid */ enterblock(fs, bl, 0); @@ -547,20 +549,21 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; + SharedProto *sp = f->sp; luaK_ret(fs, 0, 0); /* final return */ leaveblock(fs); - luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); - f->sizecode = fs->pc; - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); - f->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); - f->sizek = fs->nk; - luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); - f->sizep = fs->np; - luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); - f->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - f->sizeupvalues = fs->nups; + luaM_reallocvector(L, sp->code, sp->sizecode, fs->pc, Instruction); + sp->sizecode = fs->pc; + luaM_reallocvector(L, sp->lineinfo, sp->sizelineinfo, fs->pc, int); + sp->sizelineinfo = fs->pc; + luaM_reallocvector(L, f->k, sp->sizek, fs->nk, TValue); + sp->sizek = fs->nk; + luaM_reallocvector(L, f->p, sp->sizep, fs->np, Proto *); + sp->sizep = fs->np; + luaM_reallocvector(L, sp->locvars, sp->sizelocvars, fs->nlocvars, LocVar); + sp->sizelocvars = fs->nlocvars; + luaM_reallocvector(L, sp->upvalues, sp->sizeupvalues, fs->nups, Upvaldesc); + sp->sizeupvalues = fs->nups; lua_assert(fs->bl == NULL); ls->fs = fs->prev; luaC_checkGC(L); @@ -736,8 +739,8 @@ static void constructor (LexState *ls, expdesc *t) { } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, '}', '{', line); lastlistfield(fs, &cc); - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ + SETARG_B(fs->f->sp->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ + SETARG_C(fs->f->sp->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ } /* }====================================================================== */ @@ -747,7 +750,7 @@ static void constructor (LexState *ls, expdesc *t) { static void parlist (LexState *ls) { /* parlist -> [ param { ',' param } ] */ FuncState *fs = ls->fs; - Proto *f = fs->f; + SharedProto *f = fs->f->sp; int nparams = 0; f->is_vararg = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ @@ -778,7 +781,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { FuncState new_fs; BlockCnt bl; new_fs.f = addprototype(ls); - new_fs.f->linedefined = line; + new_fs.f->sp->linedefined = line; open_func(ls, &new_fs, &bl); checknext(ls, '('); if (ismethod) { @@ -788,7 +791,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { parlist(ls); checknext(ls, ')'); statlist(ls); - new_fs.f->lastlinedefined = ls->linenumber; + new_fs.f->sp->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); codeclosure(ls, e); close_func(ls); @@ -954,7 +957,7 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, + check_condition(ls, fs->f->sp->is_vararg, "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); break; @@ -1610,7 +1613,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->is_vararg = 1; /* main function is always vararg */ + fs->f->sp->is_vararg = 1; /* main function is always vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ @@ -1630,13 +1633,13 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, lexstate.h = luaH_new(L); /* create table for scanner */ sethvalue(L, L->top, lexstate.h); /* anchor it */ incr_top(L); - funcstate.f = cl->p = luaF_newproto(L); - funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ + funcstate.f = cl->p = luaF_newproto(L, NULL); + funcstate.f->sp->source = luaS_new(L, name); /* create and anchor TString */ lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; - luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); + luaX_setinput(L, &lexstate, z, funcstate.f->sp->source, firstchar); mainfunc(&lexstate, &funcstate); lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index c565f46a..ea2f1c8f 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -149,9 +149,9 @@ static const Proto* combine(lua_State* L, int n) for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; + if (f->p[i]->sp->sizeupvalues>0) f->p[i]->sp->upvalues[0].instack=0; } - f->sizelineinfo=0; + f->sp->sizelineinfo=0; return f; } } @@ -282,13 +282,14 @@ static void PrintConstant(const Proto* f, int i) } } -#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") +#define UPVALNAME(x) ((f->sp->upvalues[x].name) ? getstr(f->sp->upvalues[x].name) : "-") #define MYK(x) (-1-(x)) static void PrintCode(const Proto* f) { - const Instruction* code=f->code; - int pc,n=f->sizecode; + const SharedProto *sp = f->sp; + const Instruction* code=sp->code; + int pc,n=sp->sizecode; for (pc=0; pcsource ? getstr(f->source) : "=?"; if (*s=='@' || *s=='=') @@ -414,8 +415,9 @@ static void PrintHeader(const Proto* f) static void PrintDebug(const Proto* f) { + const SharedProto *sp = f->sp; int i,n; - n=f->sizek; + n=sp->sizek; printf("constants (%d) for %p:\n",n,VOID(f)); for (i=0; isizelocvars; + n=sp->sizelocvars; printf("locals (%d) for %p:\n",n,VOID(f)); for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); + i,getstr(sp->locvars[i].varname),sp->locvars[i].startpc+1,sp->locvars[i].endpc+1); } - n=f->sizeupvalues; + n=f->sp->sizeupvalues; printf("upvalues (%d) for %p:\n",n,VOID(f)); for (i=0; iupvalues[i].instack,f->upvalues[i].idx); + i,UPVALNAME(i),sp->upvalues[i].instack,sp->upvalues[i].idx); } } static void PrintFunction(const Proto* f, int full) { - int i,n=f->sizep; - PrintHeader(f); + int i,n=f->sp->sizep; + PrintHeader(f->sp); PrintCode(f); if (full) PrintDebug(f); for (i=0; ip[i],full); diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 510f3258..451c9097 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -100,7 +100,7 @@ static TString *LoadString (LoadState *S) { } -static void LoadCode (LoadState *S, Proto *f) { +static void LoadCode (LoadState *S, SharedProto *f) { int n = LoadInt(S); f->code = luaM_newvector(S->L, n, Instruction); f->sizecode = n; @@ -115,7 +115,7 @@ static void LoadConstants (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->k = luaM_newvector(S->L, n, TValue); - f->sizek = n; + f->sp->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { @@ -149,17 +149,17 @@ static void LoadProtos (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->p = luaM_newvector(S->L, n, Proto *); - f->sizep = n; + f->sp->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { - f->p[i] = luaF_newproto(S->L); - LoadFunction(S, f->p[i], f->source); + f->p[i] = luaF_newproto(S->L, NULL); + LoadFunction(S, f->p[i], f->sp->source); } } -static void LoadUpvalues (LoadState *S, Proto *f) { +static void LoadUpvalues (LoadState *S, SharedProto *f) { int i, n; n = LoadInt(S); f->upvalues = luaM_newvector(S->L, n, Upvaldesc); @@ -173,7 +173,7 @@ static void LoadUpvalues (LoadState *S, Proto *f) { } -static void LoadDebug (LoadState *S, Proto *f) { +static void LoadDebug (LoadState *S, SharedProto *f) { int i, n; n = LoadInt(S); f->lineinfo = luaM_newvector(S->L, n, int); @@ -195,7 +195,8 @@ static void LoadDebug (LoadState *S, Proto *f) { } -static void LoadFunction (LoadState *S, Proto *f, TString *psource) { +static void LoadFunction (LoadState *S, Proto *fp, TString *psource) { + SharedProto *f = fp->sp; f->source = LoadString(S); if (f->source == NULL) /* no source in dump? */ f->source = psource; /* reuse parent's source */ @@ -205,9 +206,9 @@ static void LoadFunction (LoadState *S, Proto *f, TString *psource) { f->is_vararg = LoadByte(S); f->maxstacksize = LoadByte(S); LoadCode(S, f); - LoadConstants(S, f); + LoadConstants(S, fp); LoadUpvalues(S, f); - LoadProtos(S, f); + LoadProtos(S, fp); LoadDebug(S, f); } @@ -268,7 +269,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, cl = luaF_newLclosure(L, LoadByte(&S)); setclLvalue(L, L->top, cl); incr_top(L); - cl->p = luaF_newproto(L); + cl->p = luaF_newproto(L, NULL); LoadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, buff, cl->p); diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 2ac1b02d..e7348d8b 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -493,8 +493,8 @@ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { LClosure *c = p->cache; if (c != NULL) { /* is there a cached closure? */ - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; + int nup = p->sp->sizeupvalues; + Upvaldesc *uv = p->sp->upvalues; int i; for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; @@ -514,8 +514,8 @@ static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; + int nup = p->sp->sizeupvalues; + Upvaldesc *uv = p->sp->upvalues; int i; LClosure *ncl = luaF_newLclosure(L, nup); ncl->p = p; @@ -1012,10 +1012,10 @@ void luaV_execute (lua_State *L) { StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ - StkId lim = nci->u.l.base + getproto(nfunc)->numparams; + StkId lim = nci->u.l.base + getproto(nfunc)->sp->numparams; int aux; /* close all upvalues from previous call */ - if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); + if (cl->p->sp->sizep > 0) luaF_close(L, oci->u.l.base); /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); @@ -1032,7 +1032,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_RETURN) { int b = GETARG_B(i); if (b != 0) L->top = ra+b-1; - if (cl->p->sizep > 0) luaF_close(L, base); + if (cl->p->sp->sizep > 0) luaF_close(L, base); b = luaD_poscall(L, ra); if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ return; /* external invocation: return */ @@ -1153,7 +1153,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; int j; - int n = cast_int(base - ci->func) - cl->p->numparams - 1; + int n = cast_int(base - ci->func) - cl->p->sp->numparams - 1; if (b < 0) { /* B == 0? */ b = n; /* get all var. arguments */ Protect(luaD_checkstack(L, n)); From 459347e24dd66133775b94defcee4b76b89c98c1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 22 Jan 2015 15:23:58 +0800 Subject: [PATCH 282/729] use lua_clonefunction for load lua file --- 3rd/lua/README | 5 +- 3rd/lua/lapi.c | 8 +-- 3rd/lua/lauxlib.c | 122 +++++++++++++++++++++++++++++++++++++++++++++- 3rd/lua/lua.h | 1 + 4 files changed, 130 insertions(+), 6 deletions(-) diff --git a/3rd/lua/README b/3rd/lua/README index 1daa6438..d658c4fd 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,2 +1,5 @@ -Copy from Lua 5.3.0 final http://www.lua.org/ftp/lua-5.3.0.tar.gz +This is a modify version of lua 5.3.0 (http://www.lua.org/ftp/lua-5.3.0.tar.gz) . + +For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html + diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index bf5e829f..8d9c3765 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1011,7 +1011,7 @@ static Proto * cloneproto (lua_State *L, const Proto *src) { return f; } -LUA_API void lua_clonefunction (lua_State *L, void * fp) { +LUA_API void lua_clonefunction (lua_State *L, const void * fp) { LClosure *cl; LClosure *f = cast(LClosure *, fp); lua_lock(L); @@ -1027,13 +1027,13 @@ LUA_API void lua_clonefunction (lua_State *L, void * fp) { api_incr_top(L); luaF_initupvals(L, cl); - if (f->nupvalues >= 1) { /* does it have an upvalue? */ + if (cl->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ Table *reg = hvalue(&G(L)->l_registry); const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v, gt); - luaC_upvalbarrier(L, f->upvals[0]); + setobj(L, cl->upvals[0]->v, gt); + luaC_upvalbarrier(L, cl->upvals[0]); } lua_unlock(L); } diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 1c41d6a8..72ef5d95 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -638,7 +638,7 @@ static int skipcomment (LoadF *lf, int *cp) { } -LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, +static int luaL_loadfilex_ (lua_State *L, const char *filename, const char *mode) { LoadF lf; int status, readstatus; @@ -970,3 +970,123 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { ver, *v); } +// use clonefunction + +#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} +#define UNLOCK(q) __sync_lock_release(&(q)->lock); + +struct codecache { + int lock; + lua_State *L; +}; + +static struct codecache CC = { 0 , NULL }; + +static void +clearcache() { + if (CC.L == NULL) + return; + LOCK(&CC) + lua_close(CC.L); + CC.L = luaL_newstate(); + UNLOCK(&CC) +} + +static void +init() { + CC.lock = 0; + CC.L = luaL_newstate(); +} + +static const void * +load(const char *key) { + if (CC.L == NULL) + return NULL; + LOCK(&CC) + lua_State *L = CC.L; + lua_pushstring(L, key); + lua_rawget(L, LUA_REGISTRYINDEX); + const void * result = lua_touserdata(L, -1); + lua_pop(L, 1); + UNLOCK(&CC) + + return result; +} + +static const void * +save(const char *key, const void * proto) { + lua_State *L; + const void * result = NULL; + + LOCK(&CC) + if (CC.L == NULL) { + init(); + L = CC.L; + } else { + L = CC.L; + lua_pushstring(L, key); + lua_pushvalue(L, -1); + lua_rawget(L, LUA_REGISTRYINDEX); + result = lua_touserdata(L, -1); /* stack: key oldvalue */ + if (result == NULL) { + lua_pop(L,1); + lua_pushlightuserdata(L, (void *)proto); + lua_rawset(L, LUA_REGISTRYINDEX); + } else { + lua_pop(L,2); + } + } + UNLOCK(&CC) + return result; +} + +LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, + const char *mode) { + const void * proto = load(filename); + if (proto) { + lua_clonefunction(L, proto); + return LUA_OK; + } + lua_State * eL = luaL_newstate(); + if (eL == NULL) { + lua_pushliteral(L, "New state failed"); + return LUA_ERRMEM; + } + int err = luaL_loadfilex_(eL, filename, mode); + if (err != LUA_OK) { + size_t sz = 0; + const char * msg = lua_tolstring(eL, -1, &sz); + lua_pushlstring(L, msg, sz); + lua_close(eL); + return err; + } + proto = lua_topointer(eL, -1); + const void * oldv = save(filename, proto); + if (oldv) { + lua_close(eL); + lua_clonefunction(L, oldv); + } else { + lua_clonefunction(L, proto); + /* Never close it. notice: memory leak */ + } + + return LUA_OK; +} + +static int +cache_clear(lua_State *L) { + (void)(L); + clearcache(); + return 0; +} + +LUAMOD_API int luaopen_cache(lua_State *L) { + luaL_Reg l[] = { + { "clear", cache_clear }, + { NULL, NULL }, + }; + luaL_newlib(L,l); + lua_getglobal(L, "loadfile"); + lua_setfield(L, -2, "loadfile"); + return 1; +} diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 09a4ccaf..579a5e17 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -280,6 +280,7 @@ LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); +LUA_API void (lua_clonefunction) (lua_State *L, const void *eL); /* ** coroutine functions From 40794a90b8f61f5e28b9a628a8b5852b53b44cfd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 23 Jan 2015 11:08:49 +0800 Subject: [PATCH 283/729] remove compat52 --- 3rd/lua/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 2e7a4120..7430722c 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -7,7 +7,7 @@ PLAT= none CC= gcc -std=gnu99 -CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS) +CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= -lm $(SYSLIBS) $(MYLIBS) From e44f589c5388bc24f6b54fe1dd6f216f9588bd7c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 23 Jan 2015 17:15:30 +0800 Subject: [PATCH 284/729] unpack to table.unpack --- lualib/mongo.lua | 20 ++++++++++---------- service/dbg.lua | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 143f2a5e..7a8d6eb6 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -230,10 +230,10 @@ function mongo_collection:insert(doc) end function mongo_collection:safe_insert(doc) - return self.database:runCommand("insert", self.name, "documents", {bson_encode(doc)}) + return self.database:runCommand("insert", self.name, "documents", {bson_encode(doc)}) end -function mongo_collection:batch_insert(docs) +function mongo_collection:batch_insert(docs) for i=1,#docs do if docs[i]._id == nil then docs[i]._id = bson.objectid() @@ -291,7 +291,7 @@ function mongo_collection:createIndex(keys, option) for k, v in pairs(keys) do name = (name == nil) and k or (name .. "_" .. k) name = name .. "_" .. v - end + end end @@ -319,13 +319,13 @@ end -- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, }) -- keys, value type --- query, table +-- query, table -- sort, table --- remove, bool --- update, table --- new, bool --- fields, bool --- upsert, boolean +-- remove, bool +-- update, table +-- new, bool +-- fields, bool +-- upsert, boolean function mongo_collection:findAndModify(doc) assert(doc.query) assert(doc.update or doc.remove) @@ -335,7 +335,7 @@ function mongo_collection:findAndModify(doc) table.insert(cmd, k) table.insert(cmd, v) end - return self.database:runCommand(unpack(cmd)) + return self.database:runCommand(table.unpack(cmd)) end function mongo_cursor:hasNext() diff --git a/service/dbg.lua b/service/dbg.lua index 25b630ef..5d5b7bf8 100644 --- a/service/dbg.lua +++ b/service/dbg.lua @@ -35,7 +35,7 @@ local function dump_list(list) end skynet.start(function() - local list = skynet.call(".launcher","lua", unpack(cmd)) + local list = skynet.call(".launcher","lua", table.unpack(cmd)) if list then dump_list(list) end From 2de52cee4d8ee4cf6cf5b1fd84b33d85d3811809 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 27 Jan 2015 18:03:05 +0800 Subject: [PATCH 285/729] bugfix: don't use new local var here --- lualib-src/lua-sharedata.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index f0eb046f..6ef10f0a 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -540,7 +540,7 @@ lindexconf(lua_State *L) { if (!lua_isinteger(L, 2)) { return luaL_error(L, "Invalid key %f", lua_tonumber(L, 2)); } - lua_Integer key = lua_tointeger(L, 2); + key = (int)lua_tointeger(L, 2); if (key > 0 && key <= tbl->sizearray) { --key; pushvalue(L, tbl->L, tbl->arraytype[key], &tbl->array[key]); @@ -610,7 +610,7 @@ lnextkey(lua_State *L) { if (!lua_isinteger(L, 2)) { return 0; } - lua_Integer key = lua_tointeger(L, 2); + key = (int)lua_tointeger(L, 2); if (key > 0 && key <= sizearray) { lua_Integer i; for (i=key;i Date: Tue, 27 Jan 2015 22:00:24 +0800 Subject: [PATCH 286/729] varable delcared in closure but used for global fork_queue is used in skynet.exit,but declared after it. --- lualib/skynet.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index ec9258b1..c4dd3578 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -52,6 +52,7 @@ local watching_service = {} local watching_session = {} local dead_service = {} local error_queue = {} +local fork_queue = {} -- suspend is function local suspend @@ -468,8 +469,6 @@ function skynet.dispatch_unknown_response(unknown) return prev end -local fork_queue = {} - local tunpack = table.unpack function skynet.fork(func,...) From e2683a46c39ff43b7f9bd48a67713e0e3a0d627d Mon Sep 17 00:00:00 2001 From: snail Date: Fri, 30 Jan 2015 11:15:43 +0800 Subject: [PATCH 287/729] remove needless 'debug.traceback' call in assert Though the result is not used, 'debug.traceback' is called when 'succ' is true --- lualib/skynet.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index c4dd3578..63857259 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -401,7 +401,9 @@ local function yield_call(service, session) watching_session[session] = service local succ, msg, sz = coroutine_yield("CALL", session) watching_session[session] = nil - assert(succ, debug.traceback()) + if not succ then + error(debug.traceback()) + end return msg,sz end From 05c61ca2ce49d36849dd1bafa304e1a2f5db319c Mon Sep 17 00:00:00 2001 From: snail Date: Mon, 2 Feb 2015 19:35:45 +0800 Subject: [PATCH 288/729] needless 'string.format', accurate error info remove needless 'string.format' as param of assert, add detail error info for not exist interface. --- lualib/snax.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lualib/snax.lua b/lualib/snax.lua index 6a01460f..13c79f95 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -22,6 +22,7 @@ function snax.interface(name) local si = snax_interface(name, G) local ret = { + name = name, accept = {}, response = {}, system = {}, @@ -44,7 +45,10 @@ local skynet_call = skynet.call local function gen_post(type, handle) return setmetatable({} , { __index = function( t, k ) - local id = assert(type.accept[k] , string.format("post %s no exist", k)) + local id = type.accept[k] + if not id then + error(string.format("post %s:%s no exist", type.name, k)) + end return function(...) skynet_send(handle, "snax", id, ...) end @@ -54,7 +58,10 @@ end local function gen_req(type, handle) return setmetatable({} , { __index = function( t, k ) - local id = assert(type.response[k] , string.format("request %s no exist", k)) + local id = type.response[k] + if not id then + error(string.format("request %s:%s no exist", type.name, k)) + end return function(...) return skynet_call(handle, "snax", id, ...) end From 05ae0550975176745a4826b1bff97f9dc1a54883 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 2 Feb 2015 22:45:44 +0800 Subject: [PATCH 289/729] update lpeg to 0.12.1 --- 3rd/lpeg/lpcap.c | 4 +- 3rd/lpeg/lpcode.c | 119 +++++++++++++++++++++++++++------------------ 3rd/lpeg/lpcode.h | 4 +- 3rd/lpeg/lpeg.html | 8 +-- 3rd/lpeg/lptree.c | 24 +++++---- 3rd/lpeg/lptypes.h | 10 ++-- 3rd/lpeg/lpvm.h | 7 +-- 3rd/lpeg/makefile | 4 +- 3rd/lpeg/test.lua | 117 ++++++++++++++++++++++++++------------------ 9 files changed, 173 insertions(+), 124 deletions(-) diff --git a/3rd/lpeg/lpcap.c b/3rd/lpeg/lpcap.c index d90b935d..b6911cb1 100644 --- a/3rd/lpeg/lpcap.c +++ b/3rd/lpeg/lpcap.c @@ -1,5 +1,5 @@ /* -** $Id: lpcap.c,v 1.4 2013/03/21 20:25:12 roberto Exp $ +** $Id: lpcap.c,v 1.5 2014/12/12 16:58:47 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -462,7 +462,7 @@ static int pushcapture (CapState *cs) { case Carg: { int arg = (cs->cap++)->idx; if (arg + FIXEDARGS > cs->ptop) - return luaL_error(L, "reference to absent argument #%d", arg); + return luaL_error(L, "reference to absent extra argument #%d", arg); lua_pushvalue(L, arg + FIXEDARGS); return 1; } diff --git a/3rd/lpeg/lpcode.c b/3rd/lpeg/lpcode.c index 2cc0e0d7..93c0d2aa 100644 --- a/3rd/lpeg/lpcode.c +++ b/3rd/lpeg/lpcode.c @@ -1,5 +1,5 @@ /* -** $Id: lpcode.c,v 1.18 2013/04/12 16:30:33 roberto Exp $ +** $Id: lpcode.c,v 1.21 2014/12/12 17:01:29 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -33,26 +33,30 @@ static const Charset *fullset = &fullset_; */ /* -** Check whether a charset is empty (IFail), singleton (IChar), -** full (IAny), or none of those (ISet). +** Check whether a charset is empty (returns IFail), singleton (IChar), +** full (IAny), or none of those (ISet). When singleton, '*c' returns +** which character it is. (When generic set, the set was the input, +** so there is no need to return it.) */ static Opcode charsettype (const byte *cs, int *c) { - int count = 0; + int count = 0; /* number of characters in the set */ int i; - int candidate = -1; /* candidate position for a char */ - for (i = 0; i < CHARSETSIZE; i++) { + int candidate = -1; /* candidate position for the singleton char */ + for (i = 0; i < CHARSETSIZE; i++) { /* for each byte */ int b = cs[i]; - if (b == 0) { - if (count > 1) return ISet; /* else set is still empty */ + if (b == 0) { /* is byte empty? */ + if (count > 1) /* was set neither empty nor singleton? */ + return ISet; /* neither full nor empty nor singleton */ + /* else set is still empty or singleton */ } - else if (b == 0xFF) { - if (count < (i * BITSPERCHAR)) - return ISet; + else if (b == 0xFF) { /* is byte full? */ + if (count < (i * BITSPERCHAR)) /* was set not full? */ + return ISet; /* neither full nor empty nor singleton */ else count += BITSPERCHAR; /* set is still full */ } - else if ((b & (b - 1)) == 0) { /* byte has only one bit? */ - if (count > 0) - return ISet; /* set is neither full nor empty */ + else if ((b & (b - 1)) == 0) { /* has byte only one bit? */ + if (count > 0) /* was set not empty? */ + return ISet; /* neither full nor empty nor singleton */ else { /* set has only one char till now; track it */ count++; candidate = i; @@ -77,6 +81,7 @@ static Opcode charsettype (const byte *cs, int *c) { } } + /* ** A few basic operations on Charsets */ @@ -84,16 +89,11 @@ static void cs_complement (Charset *cs) { loopset(i, cs->cs[i] = ~cs->cs[i]); } - static int cs_equal (const byte *cs1, const byte *cs2) { loopset(i, if (cs1[i] != cs2[i]) return 0); return 1; } - -/* -** computes whether sets cs1 and cs2 are disjoint -*/ static int cs_disjoint (const Charset *cs1, const Charset *cs2) { loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;) return 1; @@ -101,7 +101,8 @@ static int cs_disjoint (const Charset *cs1, const Charset *cs2) { /* -** Convert a 'char' pattern (TSet, TChar, TAny) to a charset +** If 'tree' is a 'char' pattern (TSet, TChar, TAny), convert it into a +** charset and return 1; else return 0. */ int tocharset (TTree *tree, Charset *cs) { switch (tree->tag) { @@ -116,7 +117,7 @@ int tocharset (TTree *tree, Charset *cs) { return 1; } case TAny: { - loopset(i, cs->cs[i] = 0xFF); /* add all to the set */ + loopset(i, cs->cs[i] = 0xFF); /* add all characters to the set */ return 1; } default: return 0; @@ -125,13 +126,16 @@ int tocharset (TTree *tree, Charset *cs) { /* -** Checks whether a pattern has captures +** Check whether a pattern tree has captures */ int hascaptures (TTree *tree) { tailcall: switch (tree->tag) { case TCapture: case TRunTime: return 1; + case TCall: + tree = sib2(tree); goto tailcall; /* return hascaptures(sib2(tree)); */ + case TOpenCall: assert(0); default: { switch (numsiblings[tree->tag]) { case 1: /* return hascaptures(sib1(tree)); */ @@ -161,7 +165,7 @@ int hascaptures (TTree *tree) { ** p is nullable => nullable(p) ** nofail(p) => p cannot fail ** The function assumes that TOpenCall is not nullable; -** this will be checked again when the grammar is fixed.) +** this will be checked again when the grammar is fixed. ** Run-time captures can do whatever they want, so the result ** is conservative. */ @@ -198,7 +202,7 @@ int checkaux (TTree *tree, int pred) { case TCall: /* return checkaux(sib2(tree), pred); */ tree = sib2(tree); goto tailcall; default: assert(0); return 0; - }; + } } @@ -245,16 +249,20 @@ int fixedlenx (TTree *tree, int count, int len) { /* ** Computes the 'first set' of a pattern. ** The result is a conservative aproximation: -** match p ax -> x' for some x ==> a in first(p). +** match p ax -> x (for some x) ==> a belongs to first(p) +** or +** a not in first(p) ==> match p ax -> fail (for all x) +** ** The set 'follow' is the first set of what follows the ** pattern (full set if nothing follows it). -** The function returns 0 when this set can be used for -** tests that avoid the pattern altogether. +** +** The function returns 0 when this resulting set can be used for +** test instructions that avoid the pattern altogether. ** A non-zero return can happen for two reasons: -** 1) match p '' -> '' ==> returns 1. -** (tests cannot be used because they always fail for an empty input) -** 2) there is a match-time capture ==> returns 2. -** (match-time captures should not be avoided by optimizations) +** 1) match p '' -> '' ==> return has bit 1 set +** (tests cannot be used because they would always fail for an empty input); +** 2) there is a match-time capture ==> return has bit 2 set +** (optimizations should not bypass match-time captures). */ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { tailcall: @@ -265,7 +273,7 @@ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { } case TTrue: { loopset(i, firstset->cs[i] = follow->cs[i]); - return 1; + return 1; /* accepts the empty string */ } case TFalse: { loopset(i, firstset->cs[i] = 0); @@ -280,7 +288,8 @@ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { } case TSeq: { if (!nullable(sib1(tree))) { - /* return getfirst(sib1(tree), fullset, firstset); */ + /* when p1 is not nullable, p2 has nothing to contribute; + return getfirst(sib1(tree), fullset, firstset); */ tree = sib1(tree); follow = fullset; goto tailcall; } else { /* FIRST(p1 p2, fl) = FIRST(p1, FIRST(p2, fl)) */ @@ -324,7 +333,7 @@ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { /* else go through */ } case TBehind: { /* instruction gives no new information */ - /* call 'getfirst' to check for math-time captures */ + /* call 'getfirst' only to check for math-time captures */ int e = getfirst(sib1(tree), follow, firstset); loopset(i, firstset->cs[i] = follow->cs[i]); /* uses follow */ return e | 1; /* always can accept the empty string */ @@ -335,8 +344,8 @@ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { /* -** If it returns true, then pattern can fail only depending on the next -** character of the subject +** If 'headfail(tree)' true, then 'tree' can fail only depending on the +** next character of the subject. */ static int headfail (TTree *tree) { tailcall: @@ -403,9 +412,9 @@ int sizei (const Instruction *i) { switch((Opcode)i->i.code) { case ISet: case ISpan: return CHARSETINSTSIZE; case ITestSet: return CHARSETINSTSIZE + 1; - case ITestChar: case ITestAny: case IChoice: case IJmp: - case ICall: case IOpenCall: case ICommit: case IPartialCommit: - case IBackCommit: return 2; + case ITestChar: case ITestAny: case IChoice: case IJmp: case ICall: + case IOpenCall: case ICommit: case IPartialCommit: case IBackCommit: + return 2; default: return 1; } } @@ -423,7 +432,8 @@ typedef struct CompileState { /* ** code generation is recursive; 'opt' indicates that the code is -** being generated under a 'IChoice' operator jumping to its end. +** being generated under a 'IChoice' operator jumping to its end +** (that is, the match is "optional"). ** 'tt' points to a previous test protecting this code. 'fl' is ** the follow set of the pattern. */ @@ -431,7 +441,7 @@ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, const Charset *fl); -void reallocprog (lua_State *L, Pattern *p, int nsize) { +void realloccode (lua_State *L, Pattern *p, int nsize) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); void *newblock = f(ud, p->code, p->codesize * sizeof(Instruction), @@ -446,7 +456,7 @@ void reallocprog (lua_State *L, Pattern *p, int nsize) { static int nextinstruction (CompileState *compst) { int size = compst->p->codesize; if (compst->ncode >= size) - reallocprog(compst->L, compst->p, size * 2); + realloccode(compst->L, compst->p, size * 2); return compst->ncode++; } @@ -462,6 +472,9 @@ static int addinstruction (CompileState *compst, Opcode op, int aux) { } +/* +** Add an instruction followed by space for an offset (to be set later) +*/ static int addoffsetinst (CompileState *compst, Opcode op) { int i = addinstruction(compst, op, 0); /* instruction */ addinstruction(compst, (Opcode)0, 0); /* open space for offset */ @@ -470,6 +483,9 @@ static int addoffsetinst (CompileState *compst, Opcode op) { } +/* +** Set the offset of an instruction +*/ static void setoffset (CompileState *compst, int instruction, int offset) { getinstr(compst, instruction + 1).offset = offset; } @@ -478,7 +494,7 @@ static void setoffset (CompileState *compst, int instruction, int offset) { /* ** Add a capture instruction: ** 'op' is the capture instruction; 'cap' the capture kind; -** 'key' the key into ktable; 'aux' is optional offset +** 'key' the key into ktable; 'aux' is the optional capture offset ** */ static int addinstcap (CompileState *compst, Opcode op, int cap, int key, @@ -494,12 +510,18 @@ static int addinstcap (CompileState *compst, Opcode op, int cap, int key, #define target(code,i) ((i) + code[i + 1].offset) +/* +** Patch 'instruction' to jump to 'target' +*/ static void jumptothere (CompileState *compst, int instruction, int target) { if (instruction >= 0) setoffset(compst, instruction, target - instruction); } +/* +** Patch 'instruction' to jump to current position +*/ static void jumptohere (CompileState *compst, int instruction) { jumptothere(compst, instruction, gethere(compst)); } @@ -863,7 +885,8 @@ static int codeseq1 (CompileState *compst, TTree *p1, TTree *p2, /* ** Main code-generation function: dispatch to auxiliar functions -** according to kind of tree +** according to kind of tree. ('needfollow' should return true +** only for consructions that use 'fl'.) */ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, const Charset *fl) { @@ -906,6 +929,7 @@ static void peephole (CompileState *compst) { Instruction *code = compst->p->code; int i; for (i = 0; i < compst->ncode; i += sizei(&code[i])) { + redo: switch (code[i].i.code) { case IChoice: case ICall: case ICommit: case IPartialCommit: case IBackCommit: case ITestChar: case ITestSet: @@ -927,8 +951,7 @@ static void peephole (CompileState *compst) { int fft = finallabel(code, ft); code[i] = code[ft]; /* jump becomes that instruction... */ jumptothere(compst, i, fft); /* but must correct its offset */ - i--; /* reoptimize its label */ - break; + goto redo; /* reoptimize its label */ } default: { jumptothere(compst, i, ft); /* optimize label */ @@ -950,10 +973,10 @@ static void peephole (CompileState *compst) { Instruction *compile (lua_State *L, Pattern *p) { CompileState compst; compst.p = p; compst.ncode = 0; compst.L = L; - reallocprog(L, p, 2); /* minimum initial size */ + realloccode(L, p, 2); /* minimum initial size */ codegen(&compst, p->tree, 0, NOINST, fullset); addinstruction(&compst, IEnd, 0); - reallocprog(L, p, compst.ncode); /* set final size */ + realloccode(L, p, compst.ncode); /* set final size */ peephole(&compst); return p->code; } diff --git a/3rd/lpeg/lpcode.h b/3rd/lpeg/lpcode.h index 5c9d54f9..72d2bb94 100644 --- a/3rd/lpeg/lpcode.h +++ b/3rd/lpeg/lpcode.h @@ -1,5 +1,5 @@ /* -** $Id: lpcode.h,v 1.5 2013/04/04 21:24:45 roberto Exp $ +** $Id: lpcode.h,v 1.6 2013/11/28 14:56:02 roberto Exp $ */ #if !defined(lpcode_h) @@ -17,7 +17,7 @@ int fixedlenx (TTree *tree, int count, int len); int hascaptures (TTree *tree); int lp_gc (lua_State *L); Instruction *compile (lua_State *L, Pattern *p); -void reallocprog (lua_State *L, Pattern *p, int nsize); +void realloccode (lua_State *L, Pattern *p, int nsize); int sizei (const Instruction *i); diff --git a/3rd/lpeg/lpeg.html b/3rd/lpeg/lpeg.html index 4747e304..0eb1747f 100644 --- a/3rd/lpeg/lpeg.html +++ b/3rd/lpeg/lpeg.html @@ -10,7 +10,7 @@ - +

@@ -1375,13 +1375,13 @@ and the new term for each repetition.

Download

LPeg -source code.

+source code.

License

-Copyright © 2013 Lua.org, PUC-Rio. +Copyright © 2014 Lua.org, PUC-Rio.

Permission is hereby granted, free of charge, @@ -1419,7 +1419,7 @@ THE SOFTWARE.

-$Id: lpeg.html,v 1.71 2013/04/11 19:17:41 roberto Exp $ +$Id: lpeg.html,v 1.72 2014/12/12 17:11:35 roberto Exp $

diff --git a/3rd/lpeg/lptree.c b/3rd/lpeg/lptree.c index 4f68906b..7c5b8200 100644 --- a/3rd/lpeg/lptree.c +++ b/3rd/lpeg/lptree.c @@ -1,5 +1,5 @@ /* -** $Id: lptree.c,v 1.10 2013/04/12 16:30:33 roberto Exp $ +** $Id: lptree.c,v 1.13 2014/12/12 16:59:10 roberto Exp $ ** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -209,9 +209,11 @@ static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { /* ** Add element 'idx' to 'ktable' of pattern at the top of the stack; ** create new 'ktable' if necessary. Return index of new element. +** If new element is nil, does not add it to table (as it would be +** useless) and returns 0, as ktable[0] is always nil. */ static int addtoktable (lua_State *L, int idx) { - if (idx == 0 || lua_isnil(L, idx)) /* no actual value to insert? */ + if (idx == 0) /* no actual value to insert? */ return 0; else { int n; @@ -220,11 +222,15 @@ static int addtoktable (lua_State *L, int idx) { if (n == 0) { /* is it empty/non-existent? */ lua_pop(L, 1); /* remove it */ lua_createtable(L, 1, 0); /* create a fresh table */ + lua_pushvalue(L, -1); /* make a copy */ + lua_setfenv(L, -3); /* set it as 'ktable' for pattern */ } - lua_pushvalue(L, idx); /* element to be added */ - lua_rawseti(L, -2, n + 1); - lua_setfenv(L, -2); /* set it as ktable for pattern */ - return n + 1; + if (!lua_isnil(L, idx)) { /* non-nil value? */ + lua_pushvalue(L, idx); /* element to be added */ + lua_rawseti(L, -2, ++n); + } + lua_pop(L, 1); /* remove 'ktable' */ + return n; } } @@ -525,7 +531,7 @@ static int lp_choice (lua_State *L) { static int lp_star (lua_State *L) { int size1; int n = (int)luaL_checkinteger(L, 2); - TTree *tree1 = gettree(L, 1, &size1); + TTree *tree1 = getpatt(L, 1, &size1); if (n >= 0) { /* seq tree1 (seq tree1 ... (seq tree1 (rep tree1))) */ TTree *tree = newtree(L, (n + 1) * (size1 + 1)); if (nullable(tree1)) @@ -634,8 +640,8 @@ static int lp_behind (lua_State *L) { TTree *tree; TTree *tree1 = getpatt(L, 1, NULL); int n = fixedlen(tree1); - luaL_argcheck(L, !hascaptures(tree1), 1, "pattern have captures"); luaL_argcheck(L, n > 0, 1, "pattern may not have fixed length"); + luaL_argcheck(L, !hascaptures(tree1), 1, "pattern have captures"); luaL_argcheck(L, n <= MAXBEHIND, 1, "pattern too long to look behind"); tree = newroot1sib(L, TBehind); tree->u.n = n; @@ -1139,7 +1145,7 @@ static int lp_type (lua_State *L) { int lp_gc (lua_State *L) { Pattern *p = getpattern(L, 1); if (p->codesize > 0) - reallocprog(L, p, 0); + realloccode(L, p, 0); return 0; } diff --git a/3rd/lpeg/lptypes.h b/3rd/lpeg/lptypes.h index d96554d0..b85c0c97 100644 --- a/3rd/lpeg/lptypes.h +++ b/3rd/lpeg/lptypes.h @@ -1,7 +1,7 @@ /* -** $Id: lptypes.h,v 1.8 2013/04/12 16:26:38 roberto Exp $ +** $Id: lptypes.h,v 1.10 2014/12/12 17:11:35 roberto Exp $ ** LPeg - PEG pattern matching for Lua -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) +** Copyright 2007-2014, Lua.org & PUC-Rio (see 'lpeg.html' for license) ** written by Roberto Ierusalimschy */ @@ -19,7 +19,7 @@ #include "lua.h" -#define VERSION "0.12" +#define VERSION "0.12.1" #define PATTERN_T "lpeg-pattern" @@ -56,7 +56,9 @@ /* maximum number of rules in a grammar */ -#define MAXRULES 200 +#if !defined(MAXRULES) +#define MAXRULES 1000 +#endif diff --git a/3rd/lpeg/lpvm.h b/3rd/lpeg/lpvm.h index 6a2a558d..757b9e13 100644 --- a/3rd/lpeg/lpvm.h +++ b/3rd/lpeg/lpvm.h @@ -1,5 +1,5 @@ /* -** $Id: lpvm.h,v 1.2 2013/04/03 20:37:18 roberto Exp $ +** $Id: lpvm.h,v 1.3 2014/02/21 13:06:41 roberto Exp $ */ #if !defined(lpvm_h) @@ -49,14 +49,9 @@ typedef union Instruction { } Instruction; -int getposition (lua_State *L, int t, int i); void printpatt (Instruction *p, int n); const char *match (lua_State *L, const char *o, const char *s, const char *e, Instruction *op, Capture *capture, int ptop); -int verify (lua_State *L, Instruction *op, const Instruction *p, - Instruction *e, int postable, int rule); -void checkrule (lua_State *L, Instruction *op, int from, int to, - int postable, int rule); #endif diff --git a/3rd/lpeg/makefile b/3rd/lpeg/makefile index 57a18fb3..7a8463e3 100644 --- a/3rd/lpeg/makefile +++ b/3rd/lpeg/makefile @@ -1,5 +1,5 @@ LIBNAME = lpeg -LUADIR = /usr/include/lua5.1/ +LUADIR = ../lua/ COPT = -O2 # COPT = -DLPEG_DEBUG -g @@ -22,7 +22,7 @@ CWARNS = -Wall -Wextra -pedantic \ # -Wunreachable-code \ -CFLAGS = $(CWARNS) $(COPT) -ansi -I$(LUADIR) -fPIC +CFLAGS = $(CWARNS) $(COPT) -std=c99 -I$(LUADIR) -fPIC CC = gcc FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o diff --git a/3rd/lpeg/test.lua b/3rd/lpeg/test.lua index 1d107ca5..fec5a85b 100755 --- a/3rd/lpeg/test.lua +++ b/3rd/lpeg/test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env lua5.1 --- $Id: test.lua,v 1.101 2013/04/12 16:30:33 roberto Exp $ +-- $Id: test.lua,v 1.105 2014/12/12 17:00:39 roberto Exp $ -- require"strict" -- just to be pedantic @@ -170,8 +170,8 @@ assert(m.match( basiclookfor((#m.P(b) * 1) * m.Cp()), " ( (a)") == 7) a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "123")} checkeq(a, {"123", "d"}) -a = {m.match(m.C(digit^1) * "d" * -1 + m.C(letter^1 * m.Cc"l"), "123d")} -checkeq(a, {"123"}) +-- bug in LPeg 0.12 (nil value does not create a 'ktable') +assert(m.match(m.Cc(nil), "") == nil) a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "abcd")} checkeq(a, {"abcd", "l"}) @@ -194,6 +194,16 @@ checkeq(a, {1, 5}) t = {m.match({[1] = m.C(m.C(1) * m.V(1) + -1)}, "abc")} checkeq(t, {"abc", "a", "bc", "b", "c", "c", ""}) +-- bug in 0.12 ('hascapture' did not check for captures inside a rule) +do + local pat = m.P{ + 'S'; + S1 = m.C('abc') + 3, + S = #m.V('S1') -- rule has capture, but '#' must ignore it + } + assert(pat:match'abc' == 1) +end + -- test for small capture boundary for i = 250,260 do @@ -201,9 +211,8 @@ for i = 250,260 do assert(#m.match(m.C(m.C(i)), string.rep('a', i)) == i) end - -- tests for any*n and any*-n -for n = 1, 550 do +for n = 1, 550, 13 do local x_1 = string.rep('x', n - 1) local x = x_1 .. 'a' assert(not m.P(n):match(x_1)) @@ -345,8 +354,9 @@ checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) -- test for error messages -local function checkerr (msg, ...) - assert(m.match({ m.P(msg) + 1 * m.V(1) }, select(2, pcall(...)))) +local function checkerr (msg, f, ...) + local st, err = pcall(f, ...) + assert(not st and m.match({ m.P(msg) + 1 * m.V(1) }, err)) end checkerr("rule '1' may be left recursive", m.match, { m.V(1) * 'a' }, "a") @@ -370,6 +380,13 @@ p = {'a', } checkerr("rule 'a' may be left recursive", m.match, p, "a") +-- Bug in peephole optimization of LPeg 0.12 (IJmp -> ICommit) +-- the next grammar has an original sequence IJmp -> ICommit -> IJmp L1 +-- that is optimized to ICommit L1 + +p = m.P { (m.P {m.P'abc'} + 'ayz') * m.V'y'; y = m.P'x' } +assert(p:match('abcx') == 5 and p:match('ayzx') == 5 and not p:match'abc') + -- tests for non-pattern as arguments to pattern functions @@ -488,7 +505,10 @@ assert(m.match(1 * m.B(1), 'a') == 2) assert(m.match(-m.B(1), 'a') == 1) assert(m.match(m.B(250), string.rep('a', 250)) == nil) assert(m.match(250 * m.B(250), string.rep('a', 250)) == 251) -assert(not pcall(m.B, 260)) + +-- look-behind with an open call +checkerr("pattern may not have fixed length", m.B, m.V'S1') +checkerr("too long to look behind", m.B, 260) B = #letter * -m.B(letter) + -letter * m.B(letter) x = m.Ct({ (B * m.Cp())^-1 * (1 * m.V(1) + m.P(true)) }) @@ -555,11 +575,11 @@ assert(not p:match(string.rep("011", 10001))) -- this grammar does need backtracking info. local lim = 10000 p = m.P{ '0' * m.V(1) + '0' } -assert(not pcall(m.match, p, string.rep("0", lim))) +checkerr("too many pending", m.match, p, string.rep("0", lim)) m.setmaxstack(2*lim) -assert(not pcall(m.match, p, string.rep("0", lim))) +checkerr("too many pending", m.match, p, string.rep("0", lim)) m.setmaxstack(2*lim + 4) -assert(pcall(m.match, p, string.rep("0", lim))) +assert(m.match(p, string.rep("0", lim)) == lim + 1) -- this repetition should not need stack space (only the call does) p = m.P{ ('a' * m.V(1))^0 * 'b' + 'c' } @@ -588,10 +608,10 @@ print("+") -- tests for argument captures -assert(not pcall(m.Carg, 0)) -assert(not pcall(m.Carg, -1)) -assert(not pcall(m.Carg, 2^18)) -assert(not pcall(m.match, m.Carg(1), 'a', 1)) +checkerr("invalid argument", m.Carg, 0) +checkerr("invalid argument", m.Carg, -1) +checkerr("invalid argument", m.Carg, 2^18) +checkerr("absent extra argument #1", m.match, m.Carg(1), 'a', 1) assert(m.match(m.Carg(1), 'a', 1, print) == print) x = {m.match(m.Carg(1) * m.Carg(2), '', 1, 10, 20)} checkeq(x, {10, 20}) @@ -644,14 +664,16 @@ assert(m.match(p, "aaaa") == 5) assert(m.match(p, "abaa") == 2) assert(not m.match(p, "baaa")) -assert(not pcall(m.match, function () return 2^20 end, s)) -assert(not pcall(m.match, function () return 0 end, s)) -assert(not pcall(m.match, function (s, i) return i - 1 end, s)) -assert(not pcall(m.match, m.P(1)^0 * function (_, i) return i - 1 end, s)) +checkerr("invalid position", m.match, function () return 2^20 end, s) +checkerr("invalid position", m.match, function () return 0 end, s) +checkerr("invalid position", m.match, function (s, i) return i - 1 end, s) +checkerr("invalid position", m.match, + m.P(1)^0 * function (_, i) return i - 1 end, s) assert(m.match(m.P(1)^0 * function (_, i) return i end * -1, s)) -assert(not pcall(m.match, m.P(1)^0 * function (_, i) return i + 1 end, s)) +checkerr("invalid position", m.match, + m.P(1)^0 * function (_, i) return i + 1 end, s) assert(m.match(m.P(function (s, i) return s:len() + 1 end) * -1, s)) -assert(not pcall(m.match, m.P(function (s, i) return s:len() + 2 end) * -1, s)) +checkerr("invalid position", m.match, m.P(function (s, i) return s:len() + 2 end) * -1, s) assert(not m.match(m.P(function (s, i) return s:len() end) * -1, s)) assert(m.match(m.P(1)^0 * function (_, i) return true end, s) == string.len(s) + 1) @@ -734,9 +756,9 @@ assert(m.match(m.Cs((m.P(1) / ".xx")^0), "abcd") == ".xx.xx.xx.xx") assert(m.match(m.Cp() * m.P(3) * m.Cp()/"%2%1%1 - %0 ", "abcde") == "411 - abc ") -assert(pcall(m.match, m.P(1)/"%0", "abc")) -assert(not pcall(m.match, m.P(1)/"%1", "abc")) -- out of range -assert(not pcall(m.match, m.P(1)/"%9", "abc")) -- out of range +assert(m.match(m.P(1)/"%0", "abc") == "a") +checkerr("invalid capture index", m.match, m.P(1)/"%1", "abc") +checkerr("invalid capture index", m.match, m.P(1)/"%9", "abc") p = m.C(1) p = p * p; p = p * p; p = p * p * m.C(1) / "%9 - %1" @@ -754,7 +776,7 @@ assert(m.match(m.C(1)^0 / "%9-%1-%0-%3", s) == "9-1-" .. s .. "-3") p = m.Cc('alo') * m.C(1) / "%1 - %2 - %1" assert(p:match'x' == 'alo - x - alo') -assert(not pcall(m.match, m.Cc(true) / "%1", "a")) +checkerr("invalid capture value (a boolean)", m.match, m.Cc(true) / "%1", "a") -- long strings for string capture l = 10000 @@ -782,35 +804,37 @@ checkeq(t, {a="b", c="du", xux="yuy"}) -- errors in accumulator capture --- very long match (forces fold to be a pair open-close) producing with -- no initial capture -assert(not pcall(m.match, m.Cf(m.P(500), print), string.rep('a', 600))) +checkerr("no initial value", m.match, m.Cf(m.P(5), print), 'aaaaaa') +-- no initial capture (very long match forces fold to be a pair open-close) +checkerr("no initial value", m.match, m.Cf(m.P(500), print), + string.rep('a', 600)) -- nested capture produces no initial value -assert(not pcall(m.match, m.Cf(m.P(1) / {}, print), "alo")) +checkerr("no initial value", m.match, m.Cf(m.P(1) / {}, print), "alo") -- tests for loop checker -local function haveloop (p) - assert(not pcall(function (p) return p^0 end, m.P(p))) +local function isnullable (p) + checkerr("may accept empty string", function (p) return p^0 end, m.P(p)) end -haveloop(m.P("x")^-4) +isnullable(m.P("x")^-4) assert(m.match(((m.P(0) + 1) * m.S"al")^0, "alo") == 3) assert(m.match((("x" + #m.P(1))^-4 * m.S"al")^0, "alo") == 3) -haveloop("") -haveloop(m.P("x")^0) -haveloop(m.P("x")^-1) -haveloop(m.P("x") + 1 + 2 + m.P("a")^-1) -haveloop(-m.P("ab")) -haveloop(- -m.P("ab")) -haveloop(# #(m.P("ab") + "xy")) -haveloop(- #m.P("ab")^0) -haveloop(# -m.P("ab")^1) -haveloop(#m.V(3)) -haveloop(m.V(3) + m.V(1) + m.P('a')^-1) -haveloop({[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(0)}) +isnullable("") +isnullable(m.P("x")^0) +isnullable(m.P("x")^-1) +isnullable(m.P("x") + 1 + 2 + m.P("a")^-1) +isnullable(-m.P("ab")) +isnullable(- -m.P("ab")) +isnullable(# #(m.P("ab") + "xy")) +isnullable(- #m.P("ab")^0) +isnullable(# -m.P("ab")^1) +isnullable(#m.V(3)) +isnullable(m.V(3) + m.V(1) + m.P('a')^-1) +isnullable({[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(0)}) assert(m.match(m.P{[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(1)}^0, "abc") == 3) assert(m.match(m.P""^-3, "a") == 1) @@ -894,8 +918,8 @@ print"+" -- tests for back references -assert(not pcall(m.match, m.Cb('x'), '')) -assert(not pcall(m.match, m.Cg(1, 'a') * m.Cb('b'), 'a')) +checkerr("back reference 'x' not found", m.match, m.Cb('x'), '') +checkerr("back reference 'b' not found", m.match, m.Cg(1, 'a') * m.Cb('b'), 'a') p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) t = p:match("ab") @@ -1370,8 +1394,7 @@ assert(rev:match"0123456789" == "9876543210") -- testing error messages in re local function errmsg (p, err) - local s, msg = pcall(re.compile, p) - assert(not s and string.find(msg, err)) + checkerr(err, re.compile, p) end errmsg('aaaa', "rule 'aaaa'") From 7c7d63ea700885d9115a3302d5ed98cc6dadd6fd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 5 Feb 2015 18:16:26 +0800 Subject: [PATCH 290/729] return nil to caller of newservice, while calling skynet.exit in start function --- service/launcher.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/launcher.lua b/service/launcher.lua index bfe96fbd..f4ac16fe 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -58,7 +58,7 @@ function command.REMOVE(_, handle) local response = instance[handle] if response then -- instance is dead - response(false) + response(true) -- return nil to caller of newservice instance[handle] = nil end From 72c2fad82a723e0022d8f013d20c943168e32907 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 5 Feb 2015 18:25:06 +0800 Subject: [PATCH 291/729] skynet.kill would raise error --- lualib/skynet.lua | 4 ++-- service/launcher.lua | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 63857259..d34a7f45 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -339,7 +339,7 @@ end function skynet.exit() fork_queue = {} -- no fork coroutine can be execute after skynet.exit - skynet.send(".launcher","lua","REMOVE",skynet.self()) + skynet.send(".launcher","lua","REMOVE",skynet.self(), false) -- report the sources that call me for co, session in pairs(session_coroutine_id) do local address = session_coroutine_address[co] @@ -362,7 +362,7 @@ end function skynet.kill(name) if type(name) == "number" then - skynet.send(".launcher","lua","REMOVE",name) + skynet.send(".launcher","lua","REMOVE",name, true) name = skynet.address(name) end c.command("KILL",name) diff --git a/service/launcher.lua b/service/launcher.lua index f4ac16fe..28192754 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -53,12 +53,12 @@ function command.GC() return command.MEM() end -function command.REMOVE(_, handle) +function command.REMOVE(_, handle, kill) services[handle] = nil local response = instance[handle] if response then -- instance is dead - response(true) -- return nil to caller of newservice + response(not kill) -- return nil to caller of newservice, when kill == false instance[handle] = nil end From 0447f661e0a9fadf655a6a376258527077830011 Mon Sep 17 00:00:00 2001 From: dpull Date: Fri, 6 Feb 2015 10:01:19 +0800 Subject: [PATCH 292/729] =?UTF-8?q?snax=20=E7=9A=84=20G=20=E5=8F=AF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/config | 1 + lualib/snax.lua | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/config b/examples/config index 393e83f7..9533af81 100644 --- a/examples/config +++ b/examples/config @@ -12,5 +12,6 @@ luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" lualoader = "lualib/loader.lua" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run snax = root.."examples/?.lua;"..root.."test/?.lua" +-- snax_interface_g = "snax_g" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" diff --git a/lualib/snax.lua b/lualib/snax.lua index 6a01460f..524f1c07 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -4,7 +4,9 @@ local snax_interface = require "snax.interface" local snax = {} local typeclass = {} -local G = { require = function() end } +local interface_g = skynet.getenv("snax_interface_g") +local G = interface_g and require (interface_g) or { require = function() end } +interface_g = nil skynet.register_protocol { name = "snax", From d2145162bf7edbcf3315a973519c9b15d12995fa Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 6 Feb 2015 16:06:30 +0800 Subject: [PATCH 293/729] remove bit32 --- examples/agent.lua | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/agent.lua b/examples/agent.lua index fc41802e..98b20b2b 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -2,7 +2,6 @@ local skynet = require "skynet" local netpack = require "netpack" local socket = require "socket" local sproto = require "sproto" -local bit32 = require "bit32" local host local send_request @@ -35,11 +34,7 @@ local function request(name, args, response) end local function send_package(pack) - local size = #pack - local package = string.char(bit32.extract(size,8,8)) .. - string.char(bit32.extract(size,0,8)).. - pack - + local package = string.pack(">s2", pack) socket.write(client_fd, package) end From 62e70ef755791fd1df5a3a3f93429beabbd5266b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 6 Feb 2015 16:09:26 +0800 Subject: [PATCH 294/729] fix for lua 5.3 (PR #232) --- service/launcher.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/launcher.lua b/service/launcher.lua index 28192754..634eeb1e 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -41,7 +41,7 @@ function command.MEM() local list = {} for k,v in pairs(services) do local kb, bytes = skynet.call(k,"debug","MEM") - list[skynet.address(k)] = string.format("%d Kb (%s)",kb,v) + list[skynet.address(k)] = string.format("%.2f Kb (%s)",kb,v) end return list end From 498f634ac5644721eb303a404b21e84dd43f8e1c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 Feb 2015 23:40:54 +0800 Subject: [PATCH 295/729] remote debugger --- Makefile | 7 +- lualib-src/lua-debugchannel.c | 195 ++++++++++ lualib-src/{lua_mysqlaux.c => lua-mysqlaux.c} | 0 lualib-src/sproto/README.md | 350 ++++++++++++++++++ lualib/skynet/debug.lua | 5 + lualib/skynet/remotedebug.lua | 130 +++++++ service/debug_agent.lua | 28 ++ service/debug_console.lua | 50 ++- 8 files changed, 759 insertions(+), 6 deletions(-) create mode 100644 lualib-src/lua-debugchannel.c rename lualib-src/{lua_mysqlaux.c => lua-mysqlaux.c} (100%) mode change 100755 => 100644 create mode 100644 lualib-src/sproto/README.md create mode 100644 lualib/skynet/remotedebug.lua create mode 100644 service/debug_agent.lua diff --git a/Makefile b/Makefile index 89f3a730..09734485 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ CSERVICE = snlua logger gate harbor LUA_CLIB = skynet socketdriver bson mongo md5 netpack \ clientsocket memory profile multicast \ cluster crypt sharedata stm sproto lpeg \ - mysqlaux + mysqlaux debugchannel SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -120,7 +120,10 @@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsprot $(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ -$(LUA_CLIB_PATH)/mysqlaux.so : lualib-src/lua_mysqlaux.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/mysqlaux.so : lualib-src/lua-mysqlaux.c | $(LUA_CLIB_PATH) + $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + +$(LUA_CLIB_PATH)/debugchannel.so : lualib-src/lua-debugchannel.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ clean : diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c new file mode 100644 index 00000000..07c493a8 --- /dev/null +++ b/lualib-src/lua-debugchannel.c @@ -0,0 +1,195 @@ +// only for debug use +#include +#include +#include +#include +#include + +#define METANAME "debugchannel" +#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} +#define UNLOCK(q) __sync_lock_release(&(q)->lock); + +struct command { + struct command * next; + size_t sz; +}; + +struct channel { + int lock; + int ref; + struct command * head; + struct command * tail; +}; + +static struct channel * +channel_new() { + struct channel * c = malloc(sizeof(*c)); + memset(c, 0 , sizeof(*c)); + c->ref = 1; + + return c; +} + +static struct channel * +channel_connect(struct channel *c) { + struct channel * ret = NULL; + LOCK(c) + if (c->ref == 1) { + ++c->ref; + ret = c; + } + UNLOCK(c) + return ret; +} + +static struct channel * +channel_release(struct channel *c) { + LOCK(c) + --c->ref; + if (c->ref > 0) { + UNLOCK(c) + return c; + } + // never unlock while reference is 0 + struct command * p = c->head; + c->head = NULL; + c->tail = NULL; + while(p) { + struct command *next = p->next; + free(p); + p = next; + } + return NULL; +} + +// call free after channel_read +static struct command * +channel_read(struct channel *c, double timeout) { + struct command * ret = NULL; + LOCK(c) + if (c->head == NULL) { + UNLOCK(c) + int ti = (int)(timeout * 100000); + usleep(ti); + return NULL; + } + ret = c->head; + c->head = ret->next; + if (c->head == NULL) { + c->tail = NULL; + } + UNLOCK(c) + + return ret; +} + +static void +channel_write(struct channel *c, const char * s, size_t sz) { + struct command * cmd = malloc(sizeof(*cmd)+ sz); + cmd->sz = sz; + cmd->next = NULL; + memcpy(cmd+1, s, sz); + LOCK(c) + if (c->tail == NULL) { + c->head = c->tail = cmd; + } else { + c->tail->next = cmd; + c->tail = cmd; + } + UNLOCK(c) +} + +struct channel_box { + struct channel *c; +}; + +static int +lread(lua_State *L) { + struct channel_box *cb = luaL_checkudata(L,1, METANAME); + double ti = luaL_optnumber(L, 2, 0); + struct command * c = channel_read(cb->c, ti); + if (c == NULL) + return 0; + lua_pushlstring(L, (const char *)(c+1), c->sz); + free(c); + return 1; +} + +static int +lwrite(lua_State *L) { + struct channel_box *cb = luaL_checkudata(L,1, METANAME); + size_t sz; + const char * str = luaL_checklstring(L, 2, &sz); + channel_write(cb->c, str, sz); + return 0; +} + +static int +lrelease(lua_State *L) { + struct channel_box *cb = lua_touserdata(L, 1); + if (cb) { + if (channel_release(cb->c) == NULL) { + cb->c = NULL; + } + } + + return 0; +} + +static struct channel * +new_channel(lua_State *L, struct channel *c) { + if (c == NULL) { + c = channel_new(); + } else { + c = channel_connect(c); + } + if (c == NULL) { + luaL_error(L, "new channel failed"); + // never go here + } + struct channel_box * cb = lua_newuserdata(L, sizeof(*cb)); + cb->c = c; + if (luaL_newmetatable(L, METANAME)) { + luaL_Reg l[]={ + { "read", lread }, + { "write", lwrite }, + { NULL, NULL }, + }; + luaL_newlib(L,l); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, lrelease); + lua_setfield(L, -2, "__gc"); + } + lua_setmetatable(L, -2); + return c; +} + +static int +lcreate(lua_State *L) { + struct channel *c = new_channel(L, NULL); + lua_pushlightuserdata(L, c); + return 2; +} + +static int +lconnect(lua_State *L) { + struct channel *c = lua_touserdata(L, 1); + if (c == NULL) + return luaL_error(L, "Invalid channel pointer"); + new_channel(L, c); + + return 1; +} + +int +luaopen_debugchannel(lua_State *L) { + luaL_Reg l[] = { + { "create", lcreate }, // for write + { "connect", lconnect }, // for read + { "release", lrelease }, + { NULL, NULL }, + }; + luaL_checkversion(L); + luaL_newlib(L,l); + return 1; +} diff --git a/lualib-src/lua_mysqlaux.c b/lualib-src/lua-mysqlaux.c old mode 100755 new mode 100644 similarity index 100% rename from lualib-src/lua_mysqlaux.c rename to lualib-src/lua-mysqlaux.c diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md new file mode 100644 index 00000000..8d9efd94 --- /dev/null +++ b/lualib-src/sproto/README.md @@ -0,0 +1,350 @@ +Introduction +====== + +Sproto is an efficient serialization library for C, and focuses on lua binding. It's like Google protocol buffers, but much faster. + +The design is simple. It only supports a few types that lua supports. It can be easily bound to other dynamic languages, or be used directly in C. + +In my i5-2500 @3.3GHz CPU, the benchmark is below: + +The schema in sproto: + +``` +.Person { + name 0 : string + id 1 : integer + email 2 : string + + .PhoneNumber { + number 0 : string + type 1 : integer + } + + phone 3 : *PhoneNumber +} + +.AddressBook { + person 0 : *Person +} +``` + +It's equal to: + +``` +message Person { + required string name = 1; + required int32 id = 2; + optional string email = 3; + + message PhoneNumber { + required string number = 1; + optional int32 type = 2 ; + } + + repeated PhoneNumber phone = 4; +} + +message AddressBook { + repeated Person person = 1; +} +``` + +Use the data: +```lua +local ab = { + person = { + { + name = "Alice", + id = 10000, + phone = { + { number = "123456789" , type = 1 }, + { number = "87654321" , type = 2 }, + } + }, + { + name = "Bob", + id = 20000, + phone = { + { number = "01234567890" , type = 3 }, + } + } + } +} +``` + +library| encode 1M times | decode 1M times | size +-------| --------------- | --------------- | ---- +sproto | 2.15s | 7.84s | 83 bytes +sproto (nopack) |1.58s | 6.93s | 130 bytes +pbc-lua | 6.94s | 16.9s | 69 bytes +lua-cjson | 4.92s | 8.30s | 183 bytes + +* pbc-lua is a google protocol buffers library https://github.com/cloudwu/pbc +* lua-cjson is a json library https://github.com/efelix/lua-cjson + +Lua API +======= + +```lua +local parser = require "sprotoparser" +``` + +* `parser.parse` parses a sproto schema to a binary string. + +The parser is needed for parsing the sproto schema. You can use it to generate binary string offline. The schema text and the parser is not needed when your program is running. + +```lua +local sproto = require "sproto.core" +``` + +* `sproto.newproto(sp)` creates a sproto object by a schema string (generates by parser). +* `sproto.querytype(sp, typename)` queries a type object from a sproto object by typename. +* `sproto.encode(st, luatable)` encodes a lua table by a type object, and generates a string message. +* `sproto.decode(st, message)` decodes a message string generated by sproto.encode with type. +* `sproto.pack(sprotomessage)` packs a string encoded by sproto.encode to reduce the size. +* `sproto.unpack(packedmessage)` unpacks the string packed by sproto.pack. + +The sproto supports protocol tag for RPC. Use `sproto.protocol(tagorname)` to convert the protocol name to the tag id, or convert back from tag id to the name, and returns the request/response message type objects of this protocol. + +RPC API +======= + +There is a lua wrapper for the core API for RPC . + +Read testrpc.lua for detail. + +Schema Language +========== + +Like Protocol Buffers (but unlike json), sproto messages are strongly-typed and are not self-describing. You must define your message structure in a special language. + +You can use sprotoparser library to parse the schema text to a binary string, so that the sproto library can use it. +You can parse them offline and save the string, or you can parse them during your program running. + +The schema text is like this: + +``` +# This is a comment. + +.Person { # . means a user defined type + name 0 : string # string is a build-in type. + id 1 : integer + email 2 : string + + .PhoneNumber { # user defined type can be nest. + number 0 : string + type 1 : integer + } + + phone 3 : *PhoneNumber # *PhoneNumber means an array of PhoneNumber. +} + +.AddressBook { + person 0 : *Person +} + +foobar 1 { # define a new protocol (for RPC used) with tag 1 + request person # Associate the type person with foobar.request + response { # define the foobar.response type + ok 0 : boolean + } +} + +``` + +A schema text can be self-described by the sproto schema language. + +``` +.type { + .field { + name 0 : string + type 1 : string + id 2 : integer + array 3 : boolean + } + name 0 : string + fields 1 : *field +} + +.protocol { + name 0 : string + id 1 : integer + request 2 : string + response 3 : string +} + +.group { + type 0 : *type + protocol 1 : *protocol +} +``` + +Types +======= + +* **string** : binary string +* **integer** : integer, the max length of a integer is signed 64bit. +* **boolean** : true or false + +You can add * before the typename to declare an array. + +User defined type can be any name in alphanumeric characters except the build-in typenames, and nested types are supported. + +* Where are double or real types? + +I have been using Google protocol buffers for many years in many projects, and I found the real types were seldom used. If you really need it, you can use string to serialize the double numbers. + +* Where is enum? + +In lua, enum types are not very useful. You can use integer to define an enum table in lua. + +Wire protocol +======== + +Each integer number must be serialized in little-endian format. + +The sproto message must be a user defined type struct, and a struct is encoded in three parts. The header, the field part, and the data part. +The tag and small integer or boolean will be encoded in field part, and others are in data part. + +All the fields must be encoded in ascending order (by tag). The tags of fields can be discontinuous, if a field is nil. (default value in lua), don't encode it in message. + +The header is a 16bit integer. It is the number of fields. + +Each field in field part is a 16bit integer (n). If n is zero, that means the field data is encoded in data part ; + +If n is even (and not zero), the value of this field is n/2-1 ; + +If n is odd, that means the tags is not continuous, and we should add current tag by (n+1)/2 . + +Read the examples below to see more details. + +Notice: If the tag is not declared in schema, the decoder will simply ignore the field for protocol version compatibility. + +Example 1: + +``` +person { name = "Alice" , age = 13, marital = false } + +03 00 (fn = 3) +00 00 (id = 0, value in data part) +1C 00 (id = 1, value = 13) +02 00 (id = 2, value = false) +05 00 00 00 (sizeof "Alice") +41 6C 69 63 65 ("Alice") +``` + +Example 2: + +``` +person { + name = "Bob", + age = 40, + children = { + { name = "Alice" , age = 13, marital = false }, + } +} + +04 00 (fn = 4) +00 00 (id = 0, value in data part) +52 00 (id = 1, value = 40) +01 00 (skip id = 2) +00 00 (id = 3, value in data part) + +03 00 00 00 (sizeof "Bob") +42 6F 62 ("Bob") + +11 00 00 00 (sizeof struct) +03 00 (fn = 3) +00 00 (id = 0, value in data part) +1C 00 (id = 1, value = 13) +02 00 (id = 2, value = false) +05 00 00 00 (sizeof "Alice") +41 6C 69 63 65 ("Alice") +``` + +0 Packing +======= + +The algorithm is very similar to [Cap'n proto](http://kentonv.github.io/capnproto/), but 0x00 is not treated specially. + +In packed format, the message if padding to 8. Each 8 byte is reduced to a tag byte followed by zero to eight content bytes. +The bits of the tag byte correspond to the bytes of the unpacked word, with the least-significant bit corresponding to the first byte. +Each zero bit indicates that the corresponding byte is zero. The non-zero bytes are packed following the tag. + +For example: + +``` +unpacked (hex): 08 00 00 00 03 00 02 00 19 00 00 00 aa 01 00 00 +packed (hex): 51 08 03 02 31 19 aa 01 +``` + +Tag 0xff is treated specially. A number N is following the 0xff tag. N means (N+1)*8 bytes should be copied directly. +The bytes may or may not contain zeros. Because of this rule, the worst-case space overhead of packing is 2 bytes per 2 KiB of input. + +For example: + +``` +unpacked (hex): 8a (x 30 bytes) +packed (hex): ff 03 8a (x 30 bytes) 00 00 +``` + +C API +===== + +```C +struct sproto * sproto_create(const void * proto, size_t sz); +``` + +Create a sproto object with a schema string encoded by sprotoparser: + +```C +void sproto_release(struct sproto *); +``` + +Release the sproto object: + +```C +int sproto_prototag(struct sproto *, const char * name); +const char * sproto_protoname(struct sproto *, int proto); +// SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response +struct sproto_type * sproto_protoquery(struct sproto *, int proto, int what); +``` + +Convert between tag and name of a protocol, and query the type object of it: + +```C +struct sproto_type * sproto_type(struct sproto *, const char * typename); +``` + +Query the type object from a sproto object: + +```C +typedef int (*sproto_callback)(void *ud, const char *tagname, int type, int index, struct sproto_type *, void *value, int length); + +int sproto_decode(struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); +int sproto_encode(struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); +``` + +encode and decode the sproto message with a user defined callback function. Read the implementation of lsproto.c for more details. + +```C +int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); +int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); +``` + +pack and unpack the message with the 0 packing algorithm. + +JIT +===== +You may also interest in https://github.com/lvzixun/sproto-JIT + +C# version +===== +https://github.com/lvzixun/sproto-Csharp + +Question? +========== + +* Send me an email: http://www.codingnow.com/2000/gmail.gif +* My Blog: http://blog.codingnow.com +* Design: http://blog.codingnow.com/2014/07/ejoyproto.html (in Chinese) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index d834bf23..9fd83ce2 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -58,6 +58,11 @@ function dbgcmd.TERM(service) skynet.term(service) end +function dbgcmd.REMOTEDEBUG(...) + local remotedebug = require "skynet.remotedebug" + remotedebug.start(export.dispatch, ...) +end + local function _debug_dispatch(session, address, cmd, ...) local f = dbgcmd[cmd] assert(f, cmd) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua new file mode 100644 index 00000000..8e0c86c1 --- /dev/null +++ b/lualib/skynet/remotedebug.lua @@ -0,0 +1,130 @@ +local skynet = require "skynet" +local debugchannel = require "debugchannel" +local socket = require "socket" + +local M = {} + +local HOOK_FUNC = "raw_dispatch_message" +local raw_dispatcher + +local function remove_hook(dispatcher) + assert(raw_dispatcher, "Not in debug mode") + for i=1,8 do + local name, func = debug.getupvalue(dispatcher, i) + if name == HOOK_FUNC then + debug.setupvalue(dispatcher, i, raw_dispatcher) + break + end + end + raw_dispatcher = nil + + skynet.error "Leave debug mode" +end + +local function idle() + skynet.timeout(10,idle) -- idle every 0.1s +end + +local function hook_dispatch(dispatcher, resp, fd, channel) + local address = skynet.self() + local prompt = string.format(":%08x>", address) + local newline = true + + local function print(...) + local tmp = table.pack(...) + for i=1,tmp.n do + tmp[i] = tostring(tmp[i]) + end + table.insert(tmp, "\n") + socket.write(fd, table.concat(tmp, "\t")) + end + + local message = {} + local cond + + local function breakpoint(f) + cond = f + end + + local env = setmetatable({ skynet = skynet, print = print, hook = breakpoint, m = message }, { __index = _ENV }) + + local function debug_hook(proto, msg, sz, session, source) + message.proto = proto + message.session = session + message.address = source + message.message = msg + message.size = sz + local sleep = nil + while true do + if newline then + socket.write(fd, prompt) + newline = false + end + local cmd = channel:read(sleep) + if cmd then + if cmd == "cont" then + break + end + if sleep then + if cmd == "c" then + print "continue..." + prompt = string.format(":%08x>", address) + newline = true + return + end + end + + local f = load("return "..cmd, "=(debug)", "t", env) + if not f then + local err + f,err = load(cmd, "=(debug)", "t", env) + if not f then + socket.write(fd, err .. "\n") + end + end + if f then + print(select(2,pcall(f))) + end + newline = true + else + -- no input + if sleep == nil then + if cond and cond(message) then + -- cond break point + prompt = "break>" + sleep = 0.1 + print() + newline = true + else + return + end + end + end + end + -- exit debug mode + remove_hook(dispatcher) + resp(true) + end + + for i=1,8 do + local name, func = debug.getupvalue(dispatcher, i) + if name == HOOK_FUNC then + local function hook(...) + debug_hook(...) + return func(...) + end + debug.setupvalue(dispatcher, i, hook) + skynet.timeout(0, idle) + return func + end + end +end + +function M.start(dispatcher, fd, handle) + assert(raw_dispatcher == nil, "Already in debug mode") + skynet.error "Enter debug mode" + local channel = debugchannel.connect(handle) + raw_dispatcher = hook_dispatch(dispatcher, skynet.response(), fd, channel) +end + +return M diff --git a/service/debug_agent.lua b/service/debug_agent.lua new file mode 100644 index 00000000..b4c4667d --- /dev/null +++ b/service/debug_agent.lua @@ -0,0 +1,28 @@ +local skynet = require "skynet" +local debugchannel = require "debugchannel" + +local CMD = {} + +local channel + +function CMD.start(address, fd) + assert(channel == nil, "start more than once") + skynet.error(string.format("Attach to :%08x", address)) + local handle + channel, handle = debugchannel.create() + skynet.call(address, "debug", "REMOTEDEBUG", fd, handle) + -- todo hook + skynet.ret(skynet.pack(nil)) + skynet.exit() +end + +function CMD.cmd(cmdline) + channel:write(cmdline) +end + +skynet.start(function() + skynet.dispatch("lua", function(_,_,cmd,...) + local f = CMD[cmd] + f(...) + end) +end) diff --git a/service/debug_console.lua b/service/debug_console.lua index 050fa2de..75db9343 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -48,14 +48,15 @@ local function split_cmdline(cmdline) return split end -local function docmd(cmdline, print) +local function docmd(cmdline, print, fd) local split = split_cmdline(cmdline) + table.insert(split, fd) local cmd = COMMAND[split[1]] local ok, list if cmd then ok, list = pcall(cmd, select(2,table.unpack(split))) else - ok, list = pcall(skynet.call,".launcher","lua", table.unpack(split)) + print("Invalid command, type help for command list") end if ok then @@ -82,7 +83,7 @@ local function console_main_loop(stdin, print) break end if cmdline ~= "" then - docmd(cmdline, print) + docmd(cmdline, print, stdin) end end socket.unlock(stdin) @@ -124,6 +125,7 @@ function COMMAND.help() logon = "logon address", logoff = "logoff address", log = "launch a new lua service with log", + debug = "debug address : debug a lua service", } end @@ -165,11 +167,31 @@ end local function adjust_address(address) if address:sub(1,1) ~= ":" then - address = bit32.replace( tonumber("0x" .. address), skynet.harbor(skynet.self()), 24, 8) + address = tonumber("0x" .. address) | (skynet.harbor(skynet.self()) << 24) end return address end +function COMMAND.list() + return skynet.call(".launcher", "lua", "LIST") +end + +function COMMAND.stat() + return skynet.call(".launcher", "lua", "STAT") +end + +function COMMAND.mem() + return skynet.call(".launcher", "lua", "MEM") +end + +function COMMAND.kill(address) + return skynet.call(".launcher", "lua", "KILL", address) +end + +function COMMAND.gc() + return skynet.call(".launcher", "lua", "GC") +end + function COMMAND.exit(address) skynet.send(adjust_address(address), "debug", "EXIT") end @@ -195,6 +217,26 @@ function COMMAND.info(address) return skynet.call(address,"debug","INFO") end +function COMMAND.debug(address, fd) + address = adjust_address(address) + local agent = skynet.newservice "debug_agent" + local stop + skynet.fork(function() + repeat + local cmdline = socket.readline(fd, "\n") + if not cmdline then + skynet.send(agent, "lua", "cmd", "cont") + break + end + if cmdline ~= "" then + skynet.send(agent, "lua", "cmd", cmdline) + end + until stop or cmdline == "cont" + end) + skynet.call(agent, "lua", "start", address, fd) + stop = true +end + function COMMAND.logon(address) address = adjust_address(address) core.command("LOGON", skynet.address(address)) From e031a353305dbf4ba43f626ea9215b9578ca13d7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 Feb 2015 23:48:45 +0800 Subject: [PATCH 296/729] support empty line --- lualib/skynet/remotedebug.lua | 32 +++++++++++++++++--------------- service/debug_console.lua | 4 +--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 8e0c86c1..06dbeb03 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -65,25 +65,27 @@ local function hook_dispatch(dispatcher, resp, fd, channel) if cmd == "cont" then break end - if sleep then - if cmd == "c" then - print "continue..." - prompt = string.format(":%08x>", address) - newline = true - return + if cmd ~= "" then + if sleep then + if cmd == "c" then + print "continue..." + prompt = string.format(":%08x>", address) + newline = true + return + end end - end - local f = load("return "..cmd, "=(debug)", "t", env) - if not f then - local err - f,err = load(cmd, "=(debug)", "t", env) + local f = load("return "..cmd, "=(debug)", "t", env) if not f then - socket.write(fd, err .. "\n") + local err + f,err = load(cmd, "=(debug)", "t", env) + if not f then + socket.write(fd, err .. "\n") + end + end + if f then + print(select(2,pcall(f))) end - end - if f then - print(select(2,pcall(f))) end newline = true else diff --git a/service/debug_console.lua b/service/debug_console.lua index 75db9343..0133d293 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -228,9 +228,7 @@ function COMMAND.debug(address, fd) skynet.send(agent, "lua", "cmd", "cont") break end - if cmdline ~= "" then - skynet.send(agent, "lua", "cmd", cmdline) - end + skynet.send(agent, "lua", "cmd", cmdline) until stop or cmdline == "cont" end) skynet.call(agent, "lua", "start", address, fd) From 8792a40845b7e3fe5a09821d88d30c936d2ca30c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Feb 2015 14:18:47 +0800 Subject: [PATCH 297/729] debug step mode --- 3rd/lua/ldebug.c | 19 ++- 3rd/lua/lstate.h | 1 + lualib-src/lua-debugchannel.c | 85 +++++++++++ lualib/skynet.lua | 4 + lualib/skynet/debug.lua | 2 +- lualib/skynet/injectcode.lua | 133 +++++++++++++++++ lualib/skynet/remotedebug.lua | 272 +++++++++++++++++++++++++--------- 7 files changed, 436 insertions(+), 80 deletions(-) create mode 100644 lualib/skynet/injectcode.lua diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index ab10dd99..679686ed 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -85,18 +85,25 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { CallInfo *ci; if (level < 0) return 0; /* invalid (negative) level */ lua_lock(L); - for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) - level--; - if (level == 0 && ci != &L->base_ci) { /* level found? */ + if (level == 0 && L->status == LUA_YIELD && isLua(L->ci)) { + ci = &(L->temp_ci); + *ci = *(L->ci); + ci->func = restorestack(L, ci->extra); status = 1; ar->i_ci = ci; + } else { + for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) + level--; + if (level == 0 && ci != &L->base_ci) { /* level found? */ + status = 1; + ar->i_ci = ci; + } + else status = 0; /* no such level */ } - else status = 0; /* no such level */ lua_unlock(L); return status; } - static const char *upvalname (Proto *p, int uv) { TString *s = check_exp(uv < p->sizeupvalues, p->sp->upvalues[uv].name); if (s == NULL) return "?"; @@ -130,7 +137,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, else base = ci->func + 1; if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; + StkId limit = (ci == L->ci || ci == &(L->temp_ci)) ? L->top : ci->next->func; if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ name = "(*temporary)"; /* generic name for any valid slot */ else diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 81e12c40..c89b910c 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -160,6 +160,7 @@ struct lua_State { struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ + CallInfo temp_ci; /* CallInfo for yield from hook (debug api use) */ lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c index 07c493a8..102f065a 100644 --- a/lualib-src/lua-debugchannel.c +++ b/lualib-src/lua-debugchannel.c @@ -181,12 +181,97 @@ lconnect(lua_State *L) { return 1; } +static const int HOOKKEY = 0; + +/* +** Auxiliary function used by several library functions: check for +** an optional thread as function's first argument and set 'arg' with +** 1 if this argument is present (so that functions can skip it to +** access their other arguments) +*/ +static lua_State *getthread (lua_State *L, int *arg) { + if (lua_isthread(L, 1)) { + *arg = 1; + return lua_tothread(L, 1); + } + else { + *arg = 0; + return L; /* function will operate over current thread */ + } +} + +/* +** Call hook function registered at hook table for the current +** thread (if there is one) +*/ +static void hookf (lua_State *L, lua_Debug *ar) { + static const char *const hooknames[] = + {"call", "return", "line", "count", "tail call"}; + lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + lua_pushthread(L); + if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ + lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ + if (ar->currentline >= 0) + lua_pushinteger(L, ar->currentline); /* push current line */ + else lua_pushnil(L); + lua_call(L, 2, 1); /* call hook function */ + int yield = lua_toboolean(L, -1); + lua_pop(L,1); + if (yield) { + lua_yield(L, 0); + } + } +} + +/* +** Convert a string mask (for 'sethook') into a bit mask +*/ +static int makemask (const char *smask, int count) { + int mask = 0; + if (strchr(smask, 'c')) mask |= LUA_MASKCALL; + if (strchr(smask, 'r')) mask |= LUA_MASKRET; + if (strchr(smask, 'l')) mask |= LUA_MASKLINE; + if (count > 0) mask |= LUA_MASKCOUNT; + return mask; +} + +static int db_sethook (lua_State *L) { + int arg, mask, count; + lua_Hook func; + lua_State *L1 = getthread(L, &arg); + if (lua_isnoneornil(L, arg+1)) { /* no hook? */ + lua_settop(L, arg+1); + func = NULL; mask = 0; count = 0; /* turn off hooks */ + } + else { + const char *smask = luaL_checkstring(L, arg+2); + luaL_checktype(L, arg+1, LUA_TFUNCTION); + count = (int)luaL_optinteger(L, arg + 3, 0); + func = hookf; mask = makemask(smask, count); + } + if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { + lua_createtable(L, 0, 2); /* create a hook table */ + lua_pushvalue(L, -1); + lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ + lua_pushstring(L, "k"); + lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ + lua_pushvalue(L, -1); + lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ + } + lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ + lua_pushvalue(L, arg + 1); /* value (hook function) */ + lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ + lua_sethook(L1, func, mask, count); + return 0; +} + int luaopen_debugchannel(lua_State *L) { luaL_Reg l[] = { { "create", lcreate }, // for write { "connect", lconnect }, // for read { "release", lrelease }, + { "sethook", db_sethook }, { NULL, NULL }, }; luaL_checkversion(L); diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d34a7f45..e538a37b 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -233,6 +233,9 @@ function suspend(co, result, command, param, size) elseif command == "QUIT" then -- service exit return + elseif command == nil then + -- debug trace + return else error("Unknown command : " .. command .. "\n" .. debug.traceback(co)) end @@ -719,6 +722,7 @@ local debug = require "skynet.debug" debug(skynet, { dispatch = dispatch_message, clear = clear_pool, + suspend = suspend, }) return skynet diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 9fd83ce2..0ef6dcb8 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -60,7 +60,7 @@ end function dbgcmd.REMOTEDEBUG(...) local remotedebug = require "skynet.remotedebug" - remotedebug.start(export.dispatch, ...) + remotedebug.start(export, ...) end local function _debug_dispatch(session, address, cmd, ...) diff --git a/lualib/skynet/injectcode.lua b/lualib/skynet/injectcode.lua new file mode 100644 index 00000000..de85cb6c --- /dev/null +++ b/lualib/skynet/injectcode.lua @@ -0,0 +1,133 @@ +local debug = debug +local table = table + +local FUNC_TEMP=[[ +local $ARGS +return function(...) +$SOURCE +end, +function() +return {$LOCALS} +end +]] + +local temp = {} +local function wrap_locals(co, source, level, ext_funcs) + if co == coroutine.running() then + level = level + 3 + end + local f = debug.getinfo(co, level,"f").func + if f == nil then + return false, "Invalid level" + end + + local uv = {} + local locals = {} + local uv_id = {} + local local_id = {} + + if ext_funcs then + for k,v in pairs(ext_funcs) do + table.insert(uv, k) + end + end + local i = 1 + while true do + local name, value = debug.getlocal(co, level, i) + if name == nil then + break + end + if name:byte() ~= 40 then -- '(' + table.insert(uv, name) + table.insert(locals, ("[%d]=%s,"):format(i,name)) + local_id[name] = value + end + i = i + 1 + end + local i = 1 + while true do + local name = debug.getupvalue(f, i) + if name == nil then + break + end + uv_id[name] = i + table.insert(uv, name) + i = i + 1 + end + temp.ARGS = table.concat(uv, ",") + temp.SOURCE = source + temp.LOCALS = table.concat(locals) + local full_source = FUNC_TEMP:gsub("%$(%w+)",temp) + local loader, err = load(full_source, "=(debug)") + if loader == nil then + return false, err + end + local func, update = loader() + -- join func's upvalues + local i = 1 + while true do + local name = debug.getupvalue(func, i) + if name == nil then + break + end + if ext_funcs then + local v = ext_funcs[name] + if v then + debug.setupvalue(func, i, v) + end + end + + local local_value = local_id[name] + if local_value then + debug.setupvalue(func, i, local_value) + end + local upvalue_id = uv_id[name] + if upvalue_id then + debug.upvaluejoin(func, i, f, upvalue_id) + end + i=i+1 + end + local vararg, v = debug.getlocal(co, level, -1) + if vararg then + local vargs = { v } + local i = 2 + while true do + local vararg,v = debug.getlocal(co, level, -i) + if vararg then + vargs[i] = v + else + break + end + i=i+1 + end + return func, update, table.unpack(vargs) + else + return func, update + end +end + +local function exec(co, level, func, update, ...) + if not func then + return false, update + end + if co == coroutine.running() then + level = level + 2 + end + local rets = table.pack(pcall(func, ...)) + if rets[1] then + local needupdate = update() + for k,v in pairs(needupdate) do + debug.setlocal(co, level,k,v) + end + return table.unpack(rets, 1, rets.n) + else + return false, rets[2] + end +end + +return function (source, co, level, ext_funcs) + co = co or coroutine.running() + level = level or 0 + return exec(co, level, wrap_locals(co, source, level, ext_funcs)) +end + diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 06dbeb03..0bef884c 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -1,36 +1,56 @@ local skynet = require "skynet" local debugchannel = require "debugchannel" local socket = require "socket" +local injectrun = require "skynet.injectcode" +local table = table +local debug = debug +local coroutine = coroutine +local sethook = debugchannel.sethook + local M = {} local HOOK_FUNC = "raw_dispatch_message" local raw_dispatcher +local print = _G.print +local skynet_suspend +local prompt +local newline + +local function change_prompt(s) + newline = true + prompt = s +end + +local function replace_upvalue(func, uvname, value) + local i = 1 + while true do + local name, uv = debug.getupvalue(func, i) + if name == nil then + break + end + if name == uvname then + if value then + debug.setupvalue(func, i, value) + end + return uv + end + i = i + 1 + end +end local function remove_hook(dispatcher) assert(raw_dispatcher, "Not in debug mode") - for i=1,8 do - local name, func = debug.getupvalue(dispatcher, i) - if name == HOOK_FUNC then - debug.setupvalue(dispatcher, i, raw_dispatcher) - break - end - end + replace_upvalue(dispatcher, HOOK_FUNC, raw_dispatcher) raw_dispatcher = nil + print = _G.print skynet.error "Leave debug mode" end -local function idle() - skynet.timeout(10,idle) -- idle every 0.1s -end - -local function hook_dispatch(dispatcher, resp, fd, channel) - local address = skynet.self() - local prompt = string.format(":%08x>", address) - local newline = true - - local function print(...) +local function gen_print(fd) + -- redirect print to socket fd + return function(...) local tmp = table.pack(...) for i=1,tmp.n do tmp[i] = tostring(tmp[i]) @@ -38,91 +58,197 @@ local function hook_dispatch(dispatcher, resp, fd, channel) table.insert(tmp, "\n") socket.write(fd, table.concat(tmp, "\t")) end +end - local message = {} - local cond +local function run_exp(ok, ...) + if ok then + print(...) + end + return ok +end - local function breakpoint(f) - cond = f +local function run_cmd(cmd, env, co, level) + if not run_exp(injectrun("return "..cmd, co, level, env)) then + print(select(2, injectrun(cmd,co, level,env))) + end +end + +local ctx_skynet = debug.getinfo(skynet.start,"S").short_src -- skip when enter this source file +local ctx_term = debug.getinfo(run_cmd, "S").short_src -- term when get here +local ctx_active = {} + +local linehook +local function skip_hook(mode) + local co = coroutine.running() + local ctx = ctx_active[co] + if mode == "return" then + ctx.level = ctx.level - 1 + if ctx.level == 0 then + ctx.needupdate = true + sethook(linehook, "crl") + end + else + ctx.level = ctx.level + 1 + end +end + +function linehook(mode, line) + local co = coroutine.running() + local ctx = ctx_active[co] + if mode ~= "line" then + ctx.needupdate = true + if mode ~= "return" then + if ctx.next_mode or debug.getinfo(2,"S").short_src == ctx_skynet then + ctx.level = 1 + sethook(skip_hook, "cr") + end + end + else + if ctx.needupdate then + ctx.needupdate = false + ctx.filename = debug.getinfo(2, "S").short_src + if ctx.filename == ctx_term then + ctx_active[co] = nil + sethook() + change_prompt(string.format(":%08x>", skynet.self())) + return + end + end + -- Lua 5.3 report currentline seems wrong + change_prompt(string.format("%s(%d)>",ctx.filename, line-1)) + return true -- yield + end +end + +local function add_watch_hook() + local co = coroutine.running() + local ctx = {} + ctx_active[co] = ctx + local level = 3 + sethook(function() + level = level - 1 + if level == 0 then + ctx.needupdate = true + sethook(linehook, "crl") + end + end, "r") +end + +local function watch_proto(protoname) + local proto = assert(replace_upvalue(skynet.register_protocol, "proto"), "Can't find proto table") + local p = proto[protoname] + local dispatch = p.dispatch_origin or p.dispatch + if p == nil or dispatch == nil then + return "No " .. protoname + end + p.dispatch_origin = dispatch + p.dispatch = function(...) + p.dispatch = dispatch -- restore origin dispatch function + add_watch_hook() + dispatch(...) + end +end + +local function remove_watch() + for co in pairs(ctx_active) do + sethook(co) + end + ctx_active = {} +end + +local dbgcmd = {} + +function dbgcmd.s(co) + local ctx = ctx_active[co] + ctx.next_mode = false + skynet_suspend(co, coroutine.resume(co)) +end + +function dbgcmd.n(co) + local ctx = ctx_active[co] + ctx.next_mode = true + skynet_suspend(co, coroutine.resume(co)) +end + +function dbgcmd.c(co) + sethook(co) + ctx_active[co] = nil + change_prompt(string.format(":%08x>", skynet.self())) + skynet_suspend(co, coroutine.resume(co)) +end + +local function hook_dispatch(dispatcher, resp, fd, channel) + change_prompt(string.format(":%08x>", skynet.self())) + + print = gen_print(fd) + local env = { + print = print, + watch = watch_proto + } + + local watch_env = { + print = print + } + + local function watch_cmd(cmd) + local co = next(ctx_active) + if dbgcmd[cmd] then + dbgcmd[cmd](co) + else + run_cmd(cmd, watch_env, co, 0) + end end - local env = setmetatable({ skynet = skynet, print = print, hook = breakpoint, m = message }, { __index = _ENV }) - - local function debug_hook(proto, msg, sz, session, source) - message.proto = proto - message.session = session - message.address = source - message.message = msg - message.size = sz - local sleep = nil + local function debug_hook() while true do if newline then + socket.write(fd, "\n") socket.write(fd, prompt) newline = false end - local cmd = channel:read(sleep) + local cmd = channel:read() if cmd then if cmd == "cont" then + -- leave debug mode break end if cmd ~= "" then - if sleep then - if cmd == "c" then - print "continue..." - prompt = string.format(":%08x>", address) - newline = true - return - end - end - - local f = load("return "..cmd, "=(debug)", "t", env) - if not f then - local err - f,err = load(cmd, "=(debug)", "t", env) - if not f then - socket.write(fd, err .. "\n") - end - end - if f then - print(select(2,pcall(f))) + if next(ctx_active) then + watch_cmd(cmd) + else + run_cmd(cmd, env, coroutine.running(),2) end end newline = true else -- no input - if sleep == nil then - if cond and cond(message) then - -- cond break point - prompt = "break>" - sleep = 0.1 - print() - newline = true - else - return - end - end + return end end -- exit debug mode + remove_watch() remove_hook(dispatcher) resp(true) end - for i=1,8 do - local name, func = debug.getupvalue(dispatcher, i) - if name == HOOK_FUNC then - local function hook(...) - debug_hook(...) - return func(...) - end - debug.setupvalue(dispatcher, i, hook) - skynet.timeout(0, idle) - return func - end + local func + local function hook(...) + debug_hook() + return func(...) end + func = replace_upvalue(dispatcher, HOOK_FUNC, hook) + if func then + local function idle() + skynet.timeout(10,idle) -- idle every 0.1s + end + skynet.timeout(0, idle) + end + return func end -function M.start(dispatcher, fd, handle) +function M.start(import, fd, handle) + local dispatcher = import.dispatch + skynet_suspend = import.suspend assert(raw_dispatcher == nil, "Already in debug mode") skynet.error "Enter debug mode" local channel = debugchannel.connect(handle) From 00efa6cc8a54593f5122a255e8cee9ef9c9a658c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Feb 2015 14:46:46 +0800 Subject: [PATCH 298/729] cond break point --- lualib/skynet/remotedebug.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 0bef884c..338cbff8 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -134,7 +134,7 @@ local function add_watch_hook() end, "r") end -local function watch_proto(protoname) +local function watch_proto(protoname, cond) local proto = assert(replace_upvalue(skynet.register_protocol, "proto"), "Can't find proto table") local p = proto[protoname] local dispatch = p.dispatch_origin or p.dispatch @@ -143,8 +143,10 @@ local function watch_proto(protoname) end p.dispatch_origin = dispatch p.dispatch = function(...) - p.dispatch = dispatch -- restore origin dispatch function - add_watch_hook() + if not cond or cond(...) then + p.dispatch = dispatch -- restore origin dispatch function + add_watch_hook() + end dispatch(...) end end @@ -202,7 +204,6 @@ local function hook_dispatch(dispatcher, resp, fd, channel) local function debug_hook() while true do if newline then - socket.write(fd, "\n") socket.write(fd, prompt) newline = false end From bd790cbdf1e6c87095464a62a068f6c8e3df74ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Feb 2015 17:58:52 +0800 Subject: [PATCH 299/729] add _CO for debug coroutine --- lualib/skynet/remotedebug.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 338cbff8..8b7c89e3 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -194,6 +194,7 @@ local function hook_dispatch(dispatcher, resp, fd, channel) local function watch_cmd(cmd) local co = next(ctx_active) + watch_env._CO = co if dbgcmd[cmd] then dbgcmd[cmd](co) else From 76ab48df3b23669142dc925680cdb00a33192f87 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 12 Feb 2015 14:34:52 +0800 Subject: [PATCH 300/729] bugfix: watch hook begin at next call --- lualib/skynet/remotedebug.lua | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 8b7c89e3..1fa04155 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -114,8 +114,7 @@ function linehook(mode, line) return end end - -- Lua 5.3 report currentline seems wrong - change_prompt(string.format("%s(%d)>",ctx.filename, line-1)) + change_prompt(string.format("%s(%d)>",ctx.filename, line)) return true -- yield end end @@ -124,14 +123,18 @@ local function add_watch_hook() local co = coroutine.running() local ctx = {} ctx_active[co] = ctx - local level = 3 - sethook(function() - level = level - 1 - if level == 0 then - ctx.needupdate = true - sethook(linehook, "crl") + local level = 1 + sethook(function(mode) + if mode == "return" then + level = level - 1 + else + level = level + 1 + if level == 0 then + ctx.needupdate = true + sethook(linehook, "crl") + end end - end, "r") + end, "cr") end local function watch_proto(protoname, cond) From 8645061a661193fd2567fc5cf1175e8419c9f5d8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 14 Feb 2015 11:47:18 +0800 Subject: [PATCH 301/729] use offical patch for lua --- 3rd/lua/ldebug.c | 44 ++++++++++++++++++++++++++++++-------------- 3rd/lua/lstate.h | 1 - 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 679686ed..a8cb5606 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -48,6 +48,22 @@ static int currentline (CallInfo *ci) { } +/* +** If function yielded, its 'func' can be in the 'extra' field. The +** next function restores 'func' to its correct value for debugging +** purposes. (It exchanges 'func' and 'extra'; so, when called again, +** after debugging, it also "re-restores" ** 'func' to its altered value. +*/ +static void swapextra (lua_State *L) { + if (L->status == LUA_YIELD) { + CallInfo *ci = L->ci; /* get function that yielded */ + StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ + ci->func = restorestack(L, ci->extra); + ci->extra = savestack(L, temp); + } +} + + /* ** this function can be called asynchronous (e.g. during a signal) */ @@ -85,25 +101,18 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { CallInfo *ci; if (level < 0) return 0; /* invalid (negative) level */ lua_lock(L); - if (level == 0 && L->status == LUA_YIELD && isLua(L->ci)) { - ci = &(L->temp_ci); - *ci = *(L->ci); - ci->func = restorestack(L, ci->extra); + for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) + level--; + if (level == 0 && ci != &L->base_ci) { /* level found? */ status = 1; ar->i_ci = ci; - } else { - for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) - level--; - if (level == 0 && ci != &L->base_ci) { /* level found? */ - status = 1; - ar->i_ci = ci; - } - else status = 0; /* no such level */ } + else status = 0; /* no such level */ lua_unlock(L); return status; } + static const char *upvalname (Proto *p, int uv) { TString *s = check_exp(uv < p->sizeupvalues, p->sp->upvalues[uv].name); if (s == NULL) return "?"; @@ -137,7 +146,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, else base = ci->func + 1; if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci || ci == &(L->temp_ci)) ? L->top : ci->next->func; + StkId limit = (ci == L->ci) ? L->top : ci->next->func; if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ name = "(*temporary)"; /* generic name for any valid slot */ else @@ -151,6 +160,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { const char *name; lua_lock(L); + swapextra(L); if (ar == NULL) { /* information about non-active function? */ if (!isLfunction(L->top - 1)) /* not a Lua function? */ name = NULL; @@ -165,6 +175,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { api_incr_top(L); } } + swapextra(L); lua_unlock(L); return name; } @@ -172,12 +183,15 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = 0; /* to avoid warnings */ - const char *name = findlocal(L, ar->i_ci, n, &pos); + const char *name; lua_lock(L); + swapextra(L); + name = findlocal(L, ar->i_ci, n, &pos); if (name) { setobjs2s(L, pos, L->top - 1); L->top--; /* pop value */ } + swapextra(L); lua_unlock(L); return name; } @@ -277,6 +291,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { CallInfo *ci; StkId func; lua_lock(L); + swapextra(L); if (*what == '>') { ci = NULL; func = L->top - 1; @@ -295,6 +310,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { setobjs2s(L, L->top, func); api_incr_top(L); } + swapextra(L); /* correct before option 'L', which can raise a mem. error */ if (strchr(what, 'L')) collectvalidlines(L, cl); lua_unlock(L); diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index c89b910c..81e12c40 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -160,7 +160,6 @@ struct lua_State { struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ - CallInfo temp_ci; /* CallInfo for yield from hook (debug api use) */ lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; From e5e2771e271567a247f93dbb20931264c696a1ba Mon Sep 17 00:00:00 2001 From: billchen Date: Sat, 21 Feb 2015 01:52:34 +0800 Subject: [PATCH 302/729] Update mysql.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 3处使“作没改 --- lualib/mysql.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index f78f529b..a83a0986 100755 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -461,9 +461,7 @@ local function _mysql_login(self,user,password,database) local more_capabilities more_capabilities, pos = _get_byte2(packet, pos) - self._server_capabilities = bor(self._server_capabilities, - lshift(more_capabilities, 16)) - + self._server_capabilities = self._server_capabilities|more_capabilities<<16 local len = 21 - 8 - 1 @@ -524,7 +522,7 @@ local function read_result(self, sock) if typ == 'OK' then local res = _parse_ok_packet(packet) - if res and band(res.server_status, SERVER_MORE_RESULTS_EXISTS) ~= 0 then + if res and res.server_status&SERVER_MORE_RESULTS_EXISTS ~= 0 then return res, "again" end return res @@ -577,7 +575,7 @@ local function read_result(self, sock) if typ == 'EOF' then local warning_count, status_flags = _parse_eof_packet(packet) - if band(status_flags, SERVER_MORE_RESULTS_EXISTS) ~= 0 then + if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end From 7602195540a28cc7f1b70ba33894b2a098932e3f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 27 Feb 2015 11:43:39 +0800 Subject: [PATCH 303/729] fix issue #239 --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 09734485..3c2ead00 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,8 @@ LUA_STATICLIB := 3rd/lua/liblua.a LUA_LIB ?= $(LUA_STATICLIB) LUA_INC ?= 3rd/lua -$(LUA_STATICLIB) : - cd 3rd/lua && $(MAKE) CC=$(CC) $(PLAT) +$(LUA_STATICLIB) : + cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) # jemalloc From 318ee300e3d5b6a61bf9e38d323ca0dc70d145cd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 28 Feb 2015 11:13:10 +0800 Subject: [PATCH 304/729] add load/save proto for multi states use --- Makefile | 2 +- lualib-src/sproto/lsproto.c | 39 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 3c2ead00..e553d6c9 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ LUA_STATICLIB := 3rd/lua/liblua.a LUA_LIB ?= $(LUA_STATICLIB) LUA_INC ?= 3rd/lua -$(LUA_STATICLIB) : +$(LUA_STATICLIB) : cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) # jemalloc diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index e5378713..c04f7d28 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -1,4 +1,5 @@ #include +#include #include "msvcint.h" #include "lua.h" @@ -41,8 +42,15 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { static int lnewproto(lua_State *L) { size_t sz = 0; - void * buffer = (void *)luaL_checklstring(L,1,&sz); - struct sproto * sp = sproto_create(buffer, sz); + void * buffer; + struct sproto * sp; + if (lua_isuserdata(L,1)) { + buffer = lua_touserdata(L,1); + sz = luaL_checkinteger(L,2); + } else { + buffer = (void *)luaL_checklstring(L,1,&sz); + } + sp = sproto_create(buffer, sz); if (sp) { lua_pushlightuserdata(L, sp); return 1; @@ -449,6 +457,31 @@ lprotocol(lua_State *L) { return 3; } +/* global sproto pointer for multi states */ +static void * G_sproto = NULL; +static size_t G_sproto_sz = 0; + +static int +lsaveproto(lua_State *L) { + size_t sz; + void * buffer = (void *)luaL_checklstring(L,1,&sz); + void * tmp = malloc(sz); + memcpy(tmp, buffer, sz); + if (G_sproto) { + free(G_sproto); + } + G_sproto = tmp; + G_sproto_sz = sz; + return 0; +} + +static int +lloadproto(lua_State *L) { + lua_pushlightuserdata(L, G_sproto); + lua_pushinteger(L, G_sproto_sz); + return 2; +} + int luaopen_sproto_core(lua_State *L) { #ifdef luaL_checkversion @@ -461,6 +494,8 @@ luaopen_sproto_core(lua_State *L) { { "querytype", lquerytype }, { "decode", ldecode }, { "protocol", lprotocol }, + { "loadproto", lloadproto }, + { "saveproto", lsaveproto }, { NULL, NULL }, }; luaL_newlib(L,l); From 688cc843ced972fb395c04c13c9f9eb334c70144 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 28 Feb 2015 13:33:56 +0800 Subject: [PATCH 305/729] use sproto global slot --- examples/agent.lua | 8 +++++--- examples/main.lua | 2 ++ examples/protoloader.lua | 13 ++++++++++++ examples/watchdog.lua | 5 +---- lualib-src/lua-bson.c | 31 ++++++++++------------------ lualib-src/sproto/lsproto.c | 40 +++++++++++++++++++++++++++---------- lualib/sproto.lua | 4 ++-- lualib/sprotoloader.lua | 23 +++++++++++++++++++++ 8 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 examples/protoloader.lua create mode 100644 lualib/sprotoloader.lua diff --git a/examples/agent.lua b/examples/agent.lua index 98b20b2b..45541f70 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -2,6 +2,7 @@ local skynet = require "skynet" local netpack = require "netpack" local socket = require "socket" local sproto = require "sproto" +local sprotoloader = require "sprotoloader" local host local send_request @@ -61,9 +62,10 @@ skynet.register_protocol { end } -function CMD.start(gate, fd, proto) - host = sproto.new(proto.c2s):host "package" - send_request = host:attach(sproto.new(proto.s2c)) +function CMD.start(gate, fd) + -- slot 1,2 set at main.lua + host = sprotoloader.load(1):host "package" + send_request = host:attach(sprotoloader.load(2)) skynet.fork(function() while true do send_package(send_request "heartbeat") diff --git a/examples/main.lua b/examples/main.lua index 4a98d2eb..9da707d4 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -1,9 +1,11 @@ local skynet = require "skynet" +local sprotoloader = require "sprotoloader" local max_client = 64 skynet.start(function() print("Server start") + skynet.uniqueservice("protoloader") local console = skynet.newservice("console") skynet.newservice("debug_console",8000) skynet.newservice("simpledb") diff --git a/examples/protoloader.lua b/examples/protoloader.lua new file mode 100644 index 00000000..03df570f --- /dev/null +++ b/examples/protoloader.lua @@ -0,0 +1,13 @@ +-- module proto as examples/proto.lua +package.path = "./examples/?.lua;" .. package.path + +local skynet = require "skynet" +local sprotoparser = require "sprotoparser" +local sprotoloader = require "sprotoloader" +local proto = require "proto" + +skynet.start(function() + sprotoloader.save(proto.c2s, 1) + sprotoloader.save(proto.s2c, 2) + -- don't call skynet.exit() , because sproto.core may unload and the global slot become invalid +end) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index eaedd5db..e02af169 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -1,8 +1,5 @@ -package.path = "./examples/?.lua;" .. package.path - local skynet = require "skynet" local netpack = require "netpack" -local proto = require "proto" local CMD = {} local SOCKET = {} @@ -12,7 +9,7 @@ local agent = {} function SOCKET.open(fd, addr) skynet.error("New client from : " .. addr) agent[fd] = skynet.newservice("agent") - skynet.call(agent[fd], "lua", "start", gate, fd, proto) + skynet.call(agent[fd], "lua", "start", gate, fd) end local function close_agent(fd) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index ba97f841..88f1be8e 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -9,24 +9,6 @@ #include #include -#if defined(_WIN32) || defined(_WIN64) - -#include - -static void -init_winsock() { - WSADATA wsaData; - WSAStartup(MAKEWORD(2,2), &wsaData); -} - -#else - -static void -init_winsock() { -} - -#endif - #define DEFAULT_CAP 64 #define MAX_NUMBER 1024 @@ -1100,7 +1082,10 @@ static uint32_t oid_counter; static void init_oid_header() { - init_winsock(); + if (oid_counter) { + // already init + return; + } pid_t pid = getpid(); uint32_t h = 0; char hostname[256]; @@ -1116,8 +1101,12 @@ init_oid_header() { oid_header[2] = (h>>16) & 0xff; oid_header[3] = pid & 0xff; oid_header[4] = (pid >> 8) & 0xff; - - oid_counter = h ^ time(NULL) ^ (uintptr_t)&h; + + uint32_t c = h ^ time(NULL) ^ (uintptr_t)&h; + if (c == 0) { + c = 1; + } + oid_counter = c; } static inline int diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c04f7d28..290eeef4 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -6,6 +6,7 @@ #include "lauxlib.h" #include "sproto.h" +#define MAX_GLOBALSPROTO 16 #define ENCODE_BUFFERSIZE 2050 //#define ENCODE_BUFFERSIZE 2050 @@ -458,27 +459,46 @@ lprotocol(lua_State *L) { } /* global sproto pointer for multi states */ -static void * G_sproto = NULL; -static size_t G_sproto_sz = 0; +struct sproto_bin { + void *ptr; + size_t sz; +}; + +static struct sproto_bin G_sproto[MAX_GLOBALSPROTO]; static int lsaveproto(lua_State *L) { size_t sz; void * buffer = (void *)luaL_checklstring(L,1,&sz); - void * tmp = malloc(sz); - memcpy(tmp, buffer, sz); - if (G_sproto) { - free(G_sproto); + int index = luaL_optinteger(L, 2, 0); + void * tmp; + struct sproto_bin * sbin = &G_sproto[index]; + if (index < 0 || index >= MAX_GLOBALSPROTO) { + return luaL_error(L, "Invalid global slot index %d", index); } - G_sproto = tmp; - G_sproto_sz = sz; + tmp = malloc(sz); + memcpy(tmp, buffer, sz); + if (sbin->ptr) { + free(sbin->ptr); + } + sbin->ptr = tmp; + sbin->sz = sz; return 0; } static int lloadproto(lua_State *L) { - lua_pushlightuserdata(L, G_sproto); - lua_pushinteger(L, G_sproto_sz); + int index = luaL_optinteger(L, 1, 0); + struct sproto_bin * sbin = &G_sproto[index]; + if (index < 0 || index >= MAX_GLOBALSPROTO) { + return luaL_error(L, "Invalid global slot index %d", index); + } + if (sbin->ptr == NULL) { + return luaL_error(L, "nil sproto at index %d", index); + } + + lua_pushlightuserdata(L, sbin->ptr); + lua_pushinteger(L, sbin->sz); return 2; } diff --git a/lualib/sproto.lua b/lualib/sproto.lua index e6acd784..4351ba1d 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -12,8 +12,8 @@ function sproto_mt:__gc() core.deleteproto(self.__cobj) end -function sproto.new(pbin) - local cobj = assert(core.newproto(pbin)) +function sproto.new(...) + local cobj = assert(core.newproto(...)) local self = { __cobj = cobj, __tcache = setmetatable( {} , weak_mt ), diff --git a/lualib/sprotoloader.lua b/lualib/sprotoloader.lua new file mode 100644 index 00000000..28f32eca --- /dev/null +++ b/lualib/sprotoloader.lua @@ -0,0 +1,23 @@ +local parser = require "sprotoparser" +local core = require "sproto.core" +local sproto = require "sproto" + +local loader = {} + +function loader.register(filename, index) + local f = assert(io.open(filename), "Can't open sproto file") + local data = f:read "a" + f:close() + local bin = parser.parse(data) + core.saveproto(bin, index) +end + +loader.save = core.saveproto + +function loader.load(index) + local bin, sz = core.loadproto(index) + return sproto.new(bin,sz) +end + +return loader + From 9a6e26db96f9af805fa1f2f76d3fc85ecd76953c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 28 Feb 2015 16:42:47 +0800 Subject: [PATCH 306/729] add sharemap , an example of stm --- lualib-src/lua-stm.c | 31 ++++++++++++++++---- lualib/sharemap.lua | 69 ++++++++++++++++++++++++++++++++++++++++++++ lualib/sproto.lua | 4 +-- test/sharemap.sp | 5 ++++ test/testsm.lua | 48 ++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 lualib/sharemap.lua create mode 100644 test/sharemap.sp create mode 100644 test/testsm.lua diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index b7bb5795..b38fbcd1 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "rwlock.h" #include "skynet_malloc.h" @@ -126,8 +127,16 @@ lcopy(lua_State *L) { static int lnewwriter(lua_State *L) { - void * msg = lua_touserdata(L, 1); - uint32_t sz = (uint32_t)luaL_checkinteger(L, 2); + void * msg; + size_t sz; + if (lua_isuserdata(L,1)) { + msg = lua_touserdata(L, 1); + sz = (size_t)luaL_checkinteger(L, 2); + } else { + const char * tmp = luaL_checklstring(L,1,&sz); + msg = skynet_malloc(sz); + memcpy(msg, tmp, sz); + } struct boxstm * box = lua_newuserdata(L, sizeof(*box)); box->obj = stm_new(msg,sz); lua_pushvalue(L, lua_upvalueindex(1)); @@ -148,8 +157,16 @@ ldeletewriter(lua_State *L) { static int lupdate(lua_State *L) { struct boxstm * box = lua_touserdata(L, 1); - void * msg = lua_touserdata(L, 2); - uint32_t sz = (uint32_t)luaL_checkinteger(L, 3); + void * msg; + size_t sz; + if (lua_isuserdata(L, 2)) { + msg = lua_touserdata(L, 2); + sz = (size_t)luaL_checkinteger(L, 3); + } else { + const char * tmp = luaL_checklstring(L,2,&sz); + msg = skynet_malloc(sz); + memcpy(msg, tmp, sz); + } stm_update(box->obj, msg, sz); return 0; @@ -186,6 +203,7 @@ static int lread(lua_State *L) { struct boxreader * box = lua_touserdata(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); + struct stm_copy * copy = stm_copy(box->obj); if (copy == box->lastcopy) { // not update @@ -197,10 +215,13 @@ lread(lua_State *L) { stm_releasecopy(box->lastcopy); box->lastcopy = copy; if (copy) { + lua_settop(L, 3); + lua_replace(L, 1); lua_settop(L, 2); lua_pushlightuserdata(L, copy->msg); lua_pushinteger(L, copy->sz); - lua_call(L, 2, LUA_MULTRET); + lua_pushvalue(L, 1); + lua_call(L, 3, LUA_MULTRET); lua_pushboolean(L, 1); lua_replace(L, 1); return lua_gettop(L); diff --git a/lualib/sharemap.lua b/lualib/sharemap.lua new file mode 100644 index 00000000..5b0e5845 --- /dev/null +++ b/lualib/sharemap.lua @@ -0,0 +1,69 @@ +local stm = require "stm" +local sprotoloader = require "sprotoloader" +local sproto = require "sproto" +local setmetatable = setmetatable + +local sharemap = {} + +function sharemap.register(protofile) + -- use global slot 0 for type define + sprotoloader.register(protofile, 0) +end + +local sprotoobj +local function loadsp() + if sprotoobj == nil then + sprotoobj = sprotoloader.load(0) + end + return sprotoobj +end + +function sharemap:commit() + self.__obj(sprotoobj:encode(self.__typename, self.__data)) +end + +function sharemap:copy() + return stm.copy(self.__obj) +end + +function sharemap.writer(typename, obj) + local sp = loadsp() + obj = obj or {} + local stmobj = stm.new(sp:encode(typename,obj)) + obj.__typename = typename + obj.__obj = stmobj + obj.__data = obj + obj.commit = sharemap.commit + obj.copy = sharemap.copy + return setmetatable(obj, { __index = obj.__data, __newindex = obj.__data }) +end + +local function decode(msg, sz, self) + local data = self.__data + for k in pairs(data) do + data[k] = nil + end + return sprotoobj:decode(self.__typename, msg, sz, data) +end + +function sharemap:update() + return self.__obj(decode, self) +end + +function sharemap.reader(typename, stmcpy) + local sp = loadsp() + local stmobj = stm.newcopy(stmcpy) + local _, data = stmobj(function(msg, sz) + return sp:decode(typename, msg, sz) + end) + + local obj = { + __typename = typename, + __obj = stmobj, + __data = data, + update = sharemap.update, + } + return setmetatable(obj, { __index = obj.__data, __newindex = error }) +end + +return sharemap diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 4351ba1d..98f88529 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -53,9 +53,9 @@ function sproto:encode(typename, tbl) return core.encode(st, tbl) end -function sproto:decode(typename, bin) +function sproto:decode(typename, ...) local st = querytype(self, typename) - return core.decode(st, bin) + return core.decode(st, ...) end function sproto:pencode(typename, tbl) diff --git a/test/sharemap.sp b/test/sharemap.sp new file mode 100644 index 00000000..a0a77f51 --- /dev/null +++ b/test/sharemap.sp @@ -0,0 +1,5 @@ +.foobar { + x 0 : integer + y 1 : integer + s 2 : string +} diff --git a/test/testsm.lua b/test/testsm.lua new file mode 100644 index 00000000..626952f8 --- /dev/null +++ b/test/testsm.lua @@ -0,0 +1,48 @@ +local skynet = require "skynet" +local sharemap = require "sharemap" + +local mode = ... + +if mode == "slave" then +--slave + +local function dump(reader) + reader:update() + print("x=", reader.x) + print("y=", reader.y) + print("s=", reader.s) +end + +skynet.start(function() + local reader + skynet.dispatch("lua", function(_,_,cmd,...) + if cmd == "init" then + reader = sharemap.reader(...) + else + assert(cmd == "ping") + dump(reader) + end + skynet.ret() + end) +end) + +else +-- master +skynet.start(function() + -- register share type schema + sharemap.register("./test/sharemap.sp") + local slave = skynet.newservice(SERVICE_NAME, "slave") + local writer = sharemap.writer("foobar", { x=0,y=0,s="hello" }) + skynet.call(slave, "lua", "init", "foobar", writer:copy()) + writer.x = 1 + writer:commit() + skynet.call(slave, "lua", "ping") + writer.y = 2 + writer:commit() + skynet.call(slave, "lua", "ping") + writer.s = "world" + writer:commit() + skynet.call(slave, "lua", "ping") +end) + +end \ No newline at end of file From cc136f996f56e62fd2c845c4ad12db80d6787273 Mon Sep 17 00:00:00 2001 From: David Feng Date: Mon, 2 Mar 2015 16:31:32 +0800 Subject: [PATCH 307/729] error report fix --- skynet-src/skynet_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 56d571f1..98163bd6 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -87,7 +87,7 @@ static const char * load_config = "\ local config_name = ...\ local f = assert(io.open(config_name))\ local code = assert(f:read \'*a\')\ - local function getenv(name) return assert(os.getenv(name), name) end\ + local function getenv(name) return assert(os.getenv(name), \'os.getenv failed\' .. name) end\ code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\ f:close()\ local result = {}\ From 5b469c0a9a832908863f4a707354060cbbd3e136 Mon Sep 17 00:00:00 2001 From: Learno Date: Tue, 3 Mar 2015 16:54:34 +0800 Subject: [PATCH 308/729] =?UTF-8?q?=E6=89=A9=E5=B1=95mongo=E5=BA=93?= =?UTF-8?q?=E5=AF=B9sort,=20skip,=20limit,=20count=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/mongo.lua | 41 +++++++++++++++++++++++++++++++++++++++-- test/testmongodb.lua | 12 ++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 7a8d6eb6..a1a2e11e 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -279,9 +279,45 @@ function mongo_collection:find(query, selector) __cursor = nil, __document = {}, __flags = 0, + __skip = 0, + __sortquery = nil, + __limit = 0, } , cursor_meta) end +function mongo_cursor:sort(key_list) + self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key_list} + return self +end + +function mongo_cursor:skip(amount) + self.__skip = amount + return self +end + +function mongo_cursor:limit(amount) + self.__limit = amount + return self +end + +function mongo_cursor:count(with_limit_and_skip) + local cmd = { + 'count', self.__collection.name, + 'query', self.__query, + } + if with_limit_and_skip then + local len = #cmd + cmd[len+1] = 'limit' + cmd[len+2] = self.__limit + cmd[len+3] = 'skip' + cmd[len+4] = self.__skip + end + local ret = self.__collection.database:runCommand(table.unpack(cmd)) + assert(ret and ret.ok == 1) + return ret.n +end + + -- collection:createIndex({username = 1}, {unique = true}) function mongo_collection:createIndex(keys, option) local name = option.name @@ -348,10 +384,11 @@ function mongo_cursor:hasNext() local sock = conn.__sock local pack if self.__data == nil then - pack = driver.query(request_id, self.__flags, self.__collection.full_name,0,0,self.__query,self.__selector) + local query = self.__sortquery or self.__query + pack = driver.query(request_id, self.__flags, self.__collection.full_name, self.__skip, -self.__limit, query, self.__selector) else if self.__cursor then - pack = driver.more(request_id, self.__collection.full_name,0,self.__cursor) + pack = driver.more(request_id, self.__collection.full_name, -self.__limit, self.__cursor) else -- no more self.__document = nil diff --git a/test/testmongodb.lua b/test/testmongodb.lua index f1fa81e0..d1fed034 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -43,10 +43,22 @@ function test_find_and_remove() local ret = db[db_name].testdb:safe_insert({test_key = 1}) assert(ret and ret.n == 1) + local ret = db[db_name].testdb:safe_insert({test_key = 2}) + assert(ret and ret.n == 1) + local ret = db[db_name].testdb:findOne({test_key = 1}) assert(ret and ret.test_key == 1) + local ret = db[db_name].testdb:find({test_key = {['$gt'] = 0}}):sort({test_key = -1}):skip(1):limit(1) + assert(ret:count() == 2) + assert(ret:count(true) == 1) + if ret:hasNext() then + ret = ret:next() + end + assert(ret and ret.test_key == 1) + db[db_name].testdb:delete({test_key = 1}) + db[db_name].testdb:delete({test_key = 2}) local ret = db[db_name].testdb:findOne({test_key = 1}) assert(ret == nil) From fee96045ee8fcdaf4bbff716bc96cba259091ed8 Mon Sep 17 00:00:00 2001 From: David Feng Date: Tue, 3 Mar 2015 17:00:29 +0800 Subject: [PATCH 309/729] fix error output format --- skynet-src/skynet_main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 98163bd6..81148874 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -87,7 +87,7 @@ static const char * load_config = "\ local config_name = ...\ local f = assert(io.open(config_name))\ local code = assert(f:read \'*a\')\ - local function getenv(name) return assert(os.getenv(name), \'os.getenv failed\' .. name) end\ + local function getenv(name) return assert(os.getenv(name), \'os.getenv() failed: \' .. name) end\ code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\ f:close()\ local result = {}\ @@ -118,13 +118,13 @@ main(int argc, char *argv[]) { int err = luaL_loadstring(L, load_config); assert(err == LUA_OK); lua_pushstring(L, config_file); - + err = lua_pcall(L, 1, 1, 0); if (err) { fprintf(stderr,"%s\n",lua_tostring(L,-1)); lua_close(L); return 1; - } + } _init_env(L); config.thread = optint("thread",8); From a7d79831e697a8d49d725637392855504d070573 Mon Sep 17 00:00:00 2001 From: David Feng Date: Wed, 4 Mar 2015 15:14:35 +0800 Subject: [PATCH 310/729] remove redundant print and use skynet.error --- lualib/skynet.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index e538a37b..21168c25 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -101,7 +101,6 @@ local coroutine_count = 0 local function co_create(f) local co = table.remove(coroutine_pool) if co == nil then - local print = print co = coroutine.create(function(...) f(...) while true do @@ -464,7 +463,7 @@ function skynet.dispatch_unknown_request(unknown) end local function unknown_response(session, address, msg, sz) - print("Response message :" , c.tostring(msg,sz)) + skynet.error(string.format("Response message :" , c.tostring(msg,sz))) error(string.format("Unknown session : %d from %x", session, address)) end From 4718a12668d4d96d6621b0dd54d743d8109e1237 Mon Sep 17 00:00:00 2001 From: David Feng Date: Wed, 4 Mar 2015 15:22:38 +0800 Subject: [PATCH 311/729] typo fix --- lualib-src/lua-skynet.c | 6 +++--- lualib/skynet.lua | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index ca3476d1..f1594f79 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -16,7 +16,7 @@ struct snlua { const char * preload; }; -static int +static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) @@ -191,7 +191,7 @@ _send(lua_State *L) { } if (session < 0) { // send to invalid address - // todo: maybe throw error whould be better + // todo: maybe throw an error would be better return 0; } lua_pushinteger(L,session); @@ -304,7 +304,7 @@ ltrash(lua_State *L) { int luaopen_skynet_core(lua_State *L) { luaL_checkversion(L); - + luaL_Reg l[] = { { "send" , _send }, { "genid", _genid }, diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 21168c25..17f22bd3 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -22,7 +22,7 @@ local skynet = { PTYPE_HARBOR = 5, PTYPE_SOCKET = 6, PTYPE_ERROR = 7, - PTYPE_QUEUE = 8, -- use in deprecated mqueue, use skynet.queue instead + PTYPE_QUEUE = 8, -- used in deprecated mqueue, use skynet.queue instead PTYPE_DEBUG = 9, PTYPE_LUA = 10, PTYPE_SNAX = 11, From e6d348e441682cf69d050378119b47e03fa1e430 Mon Sep 17 00:00:00 2001 From: David Feng Date: Wed, 4 Mar 2015 15:30:19 +0800 Subject: [PATCH 312/729] typo fix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 26bb18c2..d5d471da 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ Run these in different console ## About Lua -Skynet now use lua 5.3.0 final, http://www.lua.org/ftp/lua-5.3.0.tar.gz +Skynet now use Lua 5.3.0 final, http://www.lua.org/ftp/lua-5.3.0.tar.gz -You can also use the other offical lua version , edit the makefile by yourself . +You can also use the other official Lua version , edit the makefile by yourself . ## How To (in Chinese) From d19d4f24431fcb2b3e87ab4ec13c14e628ea9e98 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 4 Mar 2015 16:27:59 +0800 Subject: [PATCH 313/729] bugfix: clear cached sub node --- lualib/sharedata/corelib.lua | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index 90dc1f94..481e85fa 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -31,14 +31,14 @@ end local function update(root, cobj, gcobj) root.__obj = cobj root.__gcobj = gcobj - -- don't use pairs - for k,v in next, root do - if type(v)=="table" and k~="__parent" then + local children = root.__cache + if children then + for k,v in pairs(children) do local pointer = index(cobj, k) if type(pointer) == "userdata" then update(v, pointer, gcobj) else - root[k] = nil + children[k] = nil end end end @@ -74,13 +74,22 @@ function meta:__index(key) local obj = getcobj(self) local v = index(obj, key) if type(v) == "userdata" then - local r = setmetatable({ + local children = self.__cache + if children == nil then + children = {} + self.__cache = children + end + local r = children[key] + if r then + return r + end + r = setmetatable({ __obj = v, __gcobj = self.__gcobj, __parent = self, __key = key, }, meta) - self[key] = r + children[key] = r return r else return v From cbd7a2cd8b69332f130460a41e51eb477f8efebf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 Mar 2015 13:07:56 +0800 Subject: [PATCH 314/729] Release 1.0.0 alpha --- HISTORY.md | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index bf2a6929..779f459f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v1.0.0-alpha (2015-3-9) +----------- +* Update lua from 5.2 to 5.3 +* Add an online lua debugger +* Add sharemap as an example use case of stm +* Improve sproto for multi-state +* Improve mongodb driver +* Fix known bugs + v0.9.3 (2015-1-5) ----------- * Add : mongo createIndex diff --git a/README.md b/README.md index d5d471da..59801892 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,4 @@ You can also use the other official Lua version , edit the makefile by yourself ## How To (in Chinese) * Read Wiki https://github.com/cloudwu/skynet/wiki +* The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ From 4d6d090ff2bcbbbd93e0fa94041cca20fbf011b9 Mon Sep 17 00:00:00 2001 From: zhaodj Date: Tue, 10 Mar 2015 05:02:12 +0800 Subject: [PATCH 315/729] use lua 5.3 in examples/login/client.lua --- examples/login/client.lua | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) mode change 100644 => 100755 examples/login/client.lua diff --git a/examples/login/client.lua b/examples/login/client.lua old mode 100644 new mode 100755 index 2dfdf25b..4180e7a7 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -2,7 +2,10 @@ package.cpath = "luaclib/?.so" local socket = require "clientsocket" local crypt = require "crypt" -local bit32 = require "bit32" + +if _VERSION ~= "Lua 5.3" then + error "Use lua 5.3" +end local fd = assert(socket.connect("127.0.0.1", 8001)) @@ -93,26 +96,14 @@ print("login ok, subid=", subid) local function send_request(v, session) local size = #v + 4 - local package = string.char(bit32.extract(size,8,8)).. - string.char(bit32.extract(size,0,8)).. - v.. - string.char(bit32.extract(session,24,8)).. - string.char(bit32.extract(session,16,8)).. - string.char(bit32.extract(session,8,8)).. - string.char(bit32.extract(session,0,8)) - + local package = string.pack(">I2", size)..v..string.pack(">I4", session) socket.send(fd, package) return v, session end local function recv_response(v) - local content = v:sub(1,-6) - local ok = v:sub(-5,-5):byte() - local session = 0 - for i=-4,-1 do - local c = v:byte(i) - session = session + bit32.lshift(c,(-1-i) * 8) - end + local size = #v - 5 + local content, ok, session = string.unpack("c"..tostring(size).."B>I4", v) return ok ~=0 , content, session end @@ -132,11 +123,7 @@ end local readpackage = unpack_f(unpack_package) local function send_package(fd, pack) - local size = #pack - local package = string.char(bit32.extract(size,8,8)).. - string.char(bit32.extract(size,0,8)).. - pack - + local package = string.pack(">s2", pack) socket.send(fd, package) end From 13cb2b79e202677b1b125724eb330db3809103f4 Mon Sep 17 00:00:00 2001 From: zhaodj Date: Tue, 10 Mar 2015 05:05:39 +0800 Subject: [PATCH 316/729] change file mode --- examples/login/client.lua | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 examples/login/client.lua diff --git a/examples/login/client.lua b/examples/login/client.lua old mode 100755 new mode 100644 From a630eb796df0bc629be6a270cc07b3a42805612c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 10 Mar 2015 19:17:53 +0800 Subject: [PATCH 317/729] remove wrong param, Issue #248 --- examples/login/logind.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/login/logind.lua b/examples/login/logind.lua index e6e4c10f..afaa3496 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -54,9 +54,9 @@ function CMD.logout(uid, subid) end end -function server.command_handler(command, source, ...) +function server.command_handler(command, ...) local f = assert(CMD[command]) - return f(source, ...) + return f(...) end login(server) From a0ffff8ad60a5a992ed04a124f8b0d4555e41546 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Mar 2015 15:10:53 +0800 Subject: [PATCH 318/729] add module signal and hack lua to support it :) --- 3rd/lua/lua.h | 5 +++++ 3rd/lua/lvm.c | 15 +++++++++++++++ service-src/service_snlua.c | 6 ++++++ service/debug_console.lua | 17 +++++++++++++++-- skynet-src/skynet_module.c | 11 ++++++++++- skynet-src/skynet_module.h | 3 +++ skynet-src/skynet_server.c | 21 +++++++++++++++++++++ 7 files changed, 75 insertions(+), 3 deletions(-) diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 579a5e17..747a372a 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -458,6 +458,11 @@ struct lua_Debug { /* }====================================================================== */ +/* Add by skynet */ + +extern lua_State * skynet_sig_L; +LUA_API void (lua_checksig_)(lua_State *L); +#define lua_checksig(L) if (skynet_sig_L) { lua_checksig_(L); } /****************************************************************************** * Copyright (C) 1994-2015 Lua.org, PUC-Rio. diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index e7348d8b..8a51550c 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -43,6 +43,17 @@ /* limit for table tag-method chains (to avoid loops) */ #define MAXTAGLOOP 2000 +/* Add by skynet */ +lua_State * skynet_sig_L = NULL; + +LUA_API void +lua_checksig_(lua_State *L) { + if (skynet_sig_L == G(L)->mainthread) { + skynet_sig_L = NULL; + lua_pushnil(L); + lua_error(L); + } +} /* ** Similar to 'tonumber', but does not attempt to convert strings and @@ -935,6 +946,7 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_JMP) { + lua_checksig(L); dojump(ci, i, 0); vmbreak; } @@ -987,6 +999,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; + lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ if (luaD_precall(L, ra, nresults)) { /* C function? */ if (nresults >= 0) L->top = ci->top; /* adjust results */ @@ -1001,6 +1014,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TAILCALL) { int b = GETARG_B(i); + lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ @@ -1111,6 +1125,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TFORLOOP) { l_tforloop: + lua_checksig(L); if (!ttisnil(ra + 1)) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 0063c8c6..1ab533a2 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -156,3 +156,9 @@ snlua_release(struct snlua *l) { lua_close(l->L); skynet_free(l); } + +void +snlua_signal(struct snlua *l, int signal) { + skynet_error(l->ctx, "recv a signal %d", signal); + skynet_sig_L = l->L; +} diff --git a/service/debug_console.lua b/service/debug_console.lua index 0133d293..142cf601 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -50,8 +50,11 @@ end local function docmd(cmdline, print, fd) local split = split_cmdline(cmdline) - table.insert(split, fd) - local cmd = COMMAND[split[1]] + local command = split[1] + if command == "debug" then + table.insert(split, fd) + end + local cmd = COMMAND[command] local ok, list if cmd then ok, list = pcall(cmd, select(2,table.unpack(split))) @@ -126,6 +129,7 @@ function COMMAND.help() logoff = "logoff address", log = "launch a new lua service with log", debug = "debug address : debug a lua service", + signal = "signal address sig", } end @@ -244,3 +248,12 @@ function COMMAND.logoff(address) address = adjust_address(address) core.command("LOGOFF", skynet.address(address)) end + +function COMMAND.signal(address, sig) + address = skynet.address(adjust_address(address)) + if sig then + core.command("SIGNAL", string.format("%s %d",address,sig)) + else + core.command("SIGNAL", address) + end +end diff --git a/skynet-src/skynet_module.c b/skynet-src/skynet_module.c index 66dd4629..2da34629 100644 --- a/skynet-src/skynet_module.c +++ b/skynet-src/skynet_module.c @@ -75,7 +75,7 @@ _query(const char * name) { static int _open_sym(struct skynet_module *mod) { size_t name_size = strlen(mod->name); - char tmp[name_size + 9]; // create/init/release , longest name is release (7) + char tmp[name_size + 9]; // create/init/release/signal , longest name is release (7) memcpy(tmp, mod->name, name_size); strcpy(tmp+name_size, "_create"); mod->create = dlsym(mod->module, tmp); @@ -83,6 +83,8 @@ _open_sym(struct skynet_module *mod) { mod->init = dlsym(mod->module, tmp); strcpy(tmp+name_size, "_release"); mod->release = dlsym(mod->module, tmp); + strcpy(tmp+name_size, "_signal"); + mod->signal = dlsym(mod->module, tmp); return mod->init == NULL; } @@ -150,6 +152,13 @@ skynet_module_instance_release(struct skynet_module *m, void *inst) { } } +void +skynet_module_instance_signal(struct skynet_module *m, void *inst, int signal) { + if (m->signal) { + m->signal(inst, signal); + } +} + void skynet_module_init(const char *path) { struct modules *m = skynet_malloc(sizeof(*m)); diff --git a/skynet-src/skynet_module.h b/skynet-src/skynet_module.h index a2d96306..52b5e181 100644 --- a/skynet-src/skynet_module.h +++ b/skynet-src/skynet_module.h @@ -6,6 +6,7 @@ struct skynet_context; typedef void * (*skynet_dl_create)(void); typedef int (*skynet_dl_init)(void * inst, struct skynet_context *, const char * parm); typedef void (*skynet_dl_release)(void * inst); +typedef void (*skynet_dl_signal)(void * inst, int signal); struct skynet_module { const char * name; @@ -13,6 +14,7 @@ struct skynet_module { skynet_dl_create create; skynet_dl_init init; skynet_dl_release release; + skynet_dl_signal signal; }; void skynet_module_insert(struct skynet_module *mod); @@ -20,6 +22,7 @@ struct skynet_module * skynet_module_query(const char * name); void * skynet_module_instance_create(struct skynet_module *); int skynet_module_instance_init(struct skynet_module *, void * inst, struct skynet_context *ctx, const char * parm); void skynet_module_instance_release(struct skynet_module *, void *inst); +void skynet_module_instance_signal(struct skynet_module *, void *inst, int signal); void skynet_module_init(const char *path); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index a1961f13..4527529e 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -589,6 +589,26 @@ cmd_logoff(struct skynet_context * context, const char * param) { return NULL; } +static const char * +cmd_signal(struct skynet_context * context, const char * param) { + uint32_t handle = tohandle(context, param); + if (handle == 0) + return NULL; + struct skynet_context * ctx = skynet_handle_grab(handle); + if (ctx == NULL) + return NULL; + param = strchr(param, ' '); + int sig = 0; + if (param) { + sig = strtol(param, NULL, 0); + } + // NOTICE: the signal function should be thread safe. + skynet_module_instance_signal(ctx->mod, ctx->instance, sig); + + skynet_context_release(ctx); + return NULL; +} + static struct command_func cmd_funcs[] = { { "TIMEOUT", cmd_timeout }, { "REG", cmd_reg }, @@ -607,6 +627,7 @@ static struct command_func cmd_funcs[] = { { "MQLEN", cmd_mqlen }, { "LOGON", cmd_logon }, { "LOGOFF", cmd_logoff }, + { "SIGNAL", cmd_signal }, { NULL, NULL }, }; From f95d8db4daa4ca55f5f43b576c68b7aead3a6d8f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Mar 2015 15:17:51 +0800 Subject: [PATCH 319/729] Add marco detect lua version --- service-src/service_snlua.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 1ab533a2..89e607f6 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -160,5 +160,8 @@ snlua_release(struct snlua *l) { void snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); +#ifdef lua_checksig + // If our lua support signal (modified lua version by skynet), trigger it. skynet_sig_L = l->L; +#endif } From 03f7661a4a4433a7192dd9667b49df5820bbc603 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Mar 2015 16:48:43 +0800 Subject: [PATCH 320/729] don't delete sproto object when share it --- lualib/sproto.lua | 7 ++++--- lualib/sprotoloader.lua | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 98f88529..7907f406 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -6,20 +6,21 @@ local host = {} local weak_mt = { __mode = "kv" } local sproto_mt = { __index = sproto } +local sproto_nogc = { __index = sproto } local host_mt = { __index = host } function sproto_mt:__gc() core.deleteproto(self.__cobj) end -function sproto.new(...) - local cobj = assert(core.newproto(...)) +function sproto.new(bin,sz,nogc) + local cobj = assert(core.newproto(bin,sz)) local self = { __cobj = cobj, __tcache = setmetatable( {} , weak_mt ), __pcache = setmetatable( {} , weak_mt ), } - return setmetatable(self, sproto_mt) + return setmetatable(self, nogc and sproto_nogc or sproto_mt) end function sproto.parse(ptext) diff --git a/lualib/sprotoloader.lua b/lualib/sprotoloader.lua index 28f32eca..05d17293 100644 --- a/lualib/sprotoloader.lua +++ b/lualib/sprotoloader.lua @@ -16,7 +16,8 @@ loader.save = core.saveproto function loader.load(index) local bin, sz = core.loadproto(index) - return sproto.new(bin,sz) + -- no __gc in metatable + return sproto.new(bin,sz, true) end return loader From a0eddbdeca16aac689c5e94f8d8ad446f4da5e40 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 12 Mar 2015 12:16:21 +0800 Subject: [PATCH 321/729] sproto:pdecode support userdata --- lualib/sproto.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 7907f406..4c285222 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -64,9 +64,9 @@ function sproto:pencode(typename, tbl) return core.pack(core.encode(st, tbl)) end -function sproto:pdecode(typename, bin) +function sproto:pdecode(typename, ...) local st = querytype(self, typename) - return core.decode(st, core.unpack(bin)) + return core.decode(st, core.unpack(...)) end local function queryproto(self, pname) From 72c43b661424d9e66b698d5b0adb1ba53233c06d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 15 Mar 2015 21:20:56 +0800 Subject: [PATCH 322/729] minor fix --- lualib/http/httpd.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 66431894..c19d4bae 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -1,6 +1,7 @@ local internal = require "http.internal" local table = table +local string = string local httpd = {} From 75bbdeb6d29a00c96afef91ed5073c583595b8cd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 16 Mar 2015 17:24:02 +0800 Subject: [PATCH 323/729] define LUA_CACHELIB --- 3rd/lua/lualib.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index 5165c0fb..c43f8a2d 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -44,6 +44,8 @@ LUAMOD_API int (luaopen_debug) (lua_State *L); #define LUA_LOADLIBNAME "package" LUAMOD_API int (luaopen_package) (lua_State *L); +#define LUA_CACHELIB +LUAMOD_API int (luaopen_cache) (lua_State *L); /* open all previous libraries */ LUALIB_API void (luaL_openlibs) (lua_State *L); From f71beb4018e94b29f652388fc41321b546b9004f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 16 Mar 2015 17:33:24 +0800 Subject: [PATCH 324/729] sproto update --- lualib-src/sproto/README.md | 33 +++++--- lualib-src/sproto/lsproto.c | 131 ++++++++++++++++++++--------- lualib-src/sproto/sproto.c | 163 +++++++++++++++++++++++++----------- lualib-src/sproto/sproto.h | 14 +++- lualib/sprotoparser.lua | 93 +++++++++++++++----- 5 files changed, 315 insertions(+), 119 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index 8d9efd94..e397549d 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -140,11 +140,11 @@ The schema text is like this: } .AddressBook { - person 0 : *Person + person 0 : *Person(id) # (id) is optional, means Person.id is main index. } foobar 1 { # define a new protocol (for RPC used) with tag 1 - request person # Associate the type person with foobar.request + request Person # Associate the type Person with foobar.request response { # define the foobar.response type ok 0 : boolean } @@ -161,6 +161,7 @@ A schema text can be self-described by the sproto schema language. type 1 : string id 2 : integer array 3 : boolean + key 4 : integer # optional tag for map } name 0 : string fields 1 : *field @@ -183,10 +184,12 @@ Types ======= * **string** : binary string -* **integer** : integer, the max length of a integer is signed 64bit. +* **integer** : integer, the max length of an integer is signed 64bit. * **boolean** : true or false -You can add * before the typename to declare an array. +You can add * before the typename to declare an array. + +You can also specify a main index, the array whould be encode as an unordered map. User defined type can be any name in alphanumeric characters except the build-in typenames, and nested types are supported. @@ -319,7 +322,19 @@ struct sproto_type * sproto_type(struct sproto *, const char * typename); Query the type object from a sproto object: ```C -typedef int (*sproto_callback)(void *ud, const char *tagname, int type, int index, struct sproto_type *, void *value, int length); +struct sproto_arg { + void *ud; + const char *tagname; + int tagid; + int type; + struct sproto_type *subtype; + void *value; + int length; + int index; // array base 1 + int mainindex; // for map +}; + +typedef int (*sproto_callback)(const struct sproto_arg *args); int sproto_decode(struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); int sproto_encode(struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); @@ -334,13 +349,9 @@ int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); pack and unpack the message with the 0 packing algorithm. -JIT +Other Implementions and bindings ===== -You may also interest in https://github.com/lvzixun/sproto-JIT - -C# version -===== -https://github.com/lvzixun/sproto-Csharp +See Wiki https://github.com/cloudwu/sproto/wiki Question? ========== diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 290eeef4..a546b51d 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -9,7 +9,6 @@ #define MAX_GLOBALSPROTO 16 #define ENCODE_BUFFERSIZE 2050 -//#define ENCODE_BUFFERSIZE 2050 #define ENCODE_MAXSIZE 0x1000000 #define ENCODE_DEEPLEVEL 64 @@ -94,18 +93,19 @@ struct encode_ud { const char * array_tag; int array_index; int deep; + int iter_index; }; static int -encode(void *ud, const char *tagname, int type, int index, struct sproto_type *st, void *value, int length) { - struct encode_ud *self = ud; +encode(const struct sproto_arg *args) { + struct encode_ud *self = args->ud; lua_State *L = self->L; if (self->deep >= ENCODE_DEEPLEVEL) return luaL_error(L, "The table is too deep"); - if (index > 0) { - if (tagname != self->array_tag) { - self->array_tag = tagname; - lua_getfield(L, self->tbl_index, tagname); + if (args->index > 0) { + if (args->tagname != self->array_tag) { + self->array_tag = args->tagname; + lua_getfield(L, self->tbl_index, args->tagname); if (lua_isnil(L, -1)) { if (self->array_index) { lua_replace(L, self->array_index); @@ -119,15 +119,30 @@ encode(void *ud, const char *tagname, int type, int index, struct sproto_type *s self->array_index = lua_gettop(L); } } - lua_rawgeti(L, self->array_index, index); + if (args->mainindex >= 0) { + // use lua_next to iterate the table + // todo: check the key is equal to mainindex value + + lua_pushvalue(L,self->iter_index); + if (!lua_next(L, self->array_index)) { + // iterate end + lua_pushnil(L); + lua_replace(L, self->iter_index); + return 0; + } + lua_insert(L, -2); + lua_replace(L, self->iter_index); + } else { + lua_rawgeti(L, self->array_index, args->index); + } } else { - lua_getfield(L, self->tbl_index, tagname); + lua_getfield(L, self->tbl_index, args->tagname); } if (lua_isnil(L, -1)) { lua_pop(L,1); return 0; } - switch (type) { + switch (args->type) { case SPROTO_TINTEGER: { lua_Integer v = luaL_checkinteger(L, -1); lua_Integer vh; @@ -135,26 +150,26 @@ encode(void *ud, const char *tagname, int type, int index, struct sproto_type *s // notice: in lua 5.2, lua_Integer maybe 52bit vh = v >> 31; if (vh == 0 || vh == -1) { - *(uint32_t *)value = (uint32_t)v; + *(uint32_t *)args->value = (uint32_t)v; return 4; } else { - *(uint64_t *)value = (uint64_t)v; + *(uint64_t *)args->value = (uint64_t)v; return 8; } } case SPROTO_TBOOLEAN: { int v = lua_toboolean(L, -1); - *(int *)value = v; + *(int *)args->value = v; lua_pop(L,1); return 4; } case SPROTO_TSTRING: { size_t sz = 0; const char * str = luaL_checklstring(L, -1, &sz); - if (sz > length) + if (sz > args->length) return -1; - memcpy(value, str, sz); + memcpy(args->value, str, sz); lua_pop(L,1); return sz; } @@ -162,17 +177,19 @@ encode(void *ud, const char *tagname, int type, int index, struct sproto_type *s struct encode_ud sub; int r; sub.L = L; - sub.st = st; + sub.st = args->subtype; sub.tbl_index = lua_gettop(L); sub.array_tag = NULL; sub.array_index = 0; sub.deep = self->deep + 1; - r = sproto_encode(st, value, length, encode, &sub); - lua_pop(L,1); + lua_pushnil(L); // prepare an iterator slot + sub.iter_index = sub.tbl_index + 1; + r = sproto_encode(args->subtype, args->value, args->length, encode, &sub); + lua_pop(L,2); // pop the value and the iterator slot return r; } default: - return luaL_error(L, "Invalid field type %d", type); + return luaL_error(L, "Invalid field type %d", args->type); } } @@ -211,18 +228,23 @@ lencode(lua_State *L) { return luaL_argerror(L, 1, "Need a sproto_type object"); } luaL_checktype(L, 2, LUA_TTABLE); - luaL_checkstack(L, ENCODE_DEEPLEVEL + 8, NULL); + luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); self.L = L; self.st = st; self.tbl_index = 2; self.array_tag = NULL; self.array_index = 0; self.deep = 0; + lua_pushnil(L); // for iterator (stack 3) + self.iter_index = 3; for (;;) { int r = sproto_encode(st, buffer, sz, encode, &self); if (r<0) { buffer = expand_buffer(L, sz, sz*2); sz *= 2; + // reset iterator slot + lua_pushnil(L); + lua_replace(L, 3); } else { lua_pushlstring(L, buffer, r); return 1; @@ -236,21 +258,23 @@ struct decode_ud { int array_index; int result_index; int deep; + int mainindex_tag; + int key_index; }; static int -decode(void *ud, const char *tagname, int type, int index, struct sproto_type *st, void *value, int length) { - struct decode_ud * self = ud; +decode(const struct sproto_arg *args) { + struct decode_ud * self = args->ud; lua_State *L = self->L; if (self->deep >= ENCODE_DEEPLEVEL) return luaL_error(L, "The table is too deep"); - if (index > 0) { + if (args->index > 0) { // It's array - if (tagname != self->array_tag) { - self->array_tag = tagname; + if (args->tagname != self->array_tag) { + self->array_tag = args->tagname; lua_newtable(L); lua_pushvalue(L, -1); - lua_setfield(L, self->result_index, tagname); + lua_setfield(L, self->result_index, args->tagname); if (self->array_index) { lua_replace(L, self->array_index); } else { @@ -258,20 +282,20 @@ decode(void *ud, const char *tagname, int type, int index, struct sproto_type *s } } } - switch (type) { + switch (args->type) { case SPROTO_TINTEGER: { // notice: in lua 5.2, 52bit integer support (not 64) - lua_Integer v = *(uint64_t*)value; + lua_Integer v = *(uint64_t*)args->value; lua_pushinteger(L, v); break; } case SPROTO_TBOOLEAN: { - int v = *(uint64_t*)value; + int v = *(uint64_t*)args->value; lua_pushboolean(L,v); break; } case SPROTO_TSTRING: { - lua_pushlstring(L, value, length); + lua_pushlstring(L, args->value, args->length); break; } case SPROTO_TSTRUCT: { @@ -283,20 +307,47 @@ decode(void *ud, const char *tagname, int type, int index, struct sproto_type *s sub.deep = self->deep + 1; sub.array_index = 0; sub.array_tag = NULL; + if (args->mainindex >= 0) { + // This struct will set into a map, so mark the main index tag. + sub.mainindex_tag = args->mainindex; + lua_pushnil(L); + sub.key_index = lua_gettop(L); - r = sproto_decode(st, value, length, decode, &sub); - if (r < 0 || r != length) - return r; - lua_settop(L, sub.result_index); - break; + r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); + if (r < 0 || r != args->length) + return r; + // assert(args->index > 0); + lua_pushvalue(L, sub.key_index); + if (lua_isnil(L, -1)) { + luaL_error(L, "Can't find main index (tag=%d) in [%s]", args->mainindex, args->tagname); + } + lua_pushvalue(L, sub.result_index); + lua_rawset(L, self->array_index); + lua_settop(L, sub.result_index-1); + return 0; + } else { + sub.mainindex_tag = -1; + sub.key_index = 0; + r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); + if (r < 0 || r != args->length) + return r; + lua_settop(L, sub.result_index); + break; + } } default: luaL_error(L, "Invalid type"); } - if (index > 0) { - lua_rawseti(L, self->array_index, index); + if (args->index > 0) { + lua_rawseti(L, self->array_index, args->index); } else { - lua_setfield(L, self->result_index, tagname); + if (self->mainindex_tag == args->tagid) { + // This tag is marked, save the value to key_index + // assert(self->key_index > 0); + lua_pushvalue(L,-1); + lua_replace(L, self->key_index); + } + lua_setfield(L, self->result_index, args->tagname); } return 0; @@ -339,12 +390,14 @@ ldecode(lua_State *L) { if (!lua_istable(L, -1)) { lua_newtable(L); } - luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); + luaL_checkstack(L, ENCODE_DEEPLEVEL*3 + 8, NULL); self.L = L; self.result_index = lua_gettop(L); self.array_index = 0; self.array_tag = NULL; self.deep = 0; + self.mainindex_tag = -1; + self.key_index = 0; r = sproto_decode(st, buffer, (int)sz, decode, &self); if (r < 0) { return luaL_error(L, "decode error"); diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 67e0d0d0..910266da 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -17,6 +17,7 @@ struct field { int type; const char * name; struct sproto_type * st; + int key; }; struct sproto_type { @@ -188,6 +189,7 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { f->type = -1; f->name = NULL; f->st = NULL; + f->key = -1; sz = todword(stream); stream += SIZEOF_LENGTH; @@ -234,6 +236,9 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { if (value) array = SPROTO_TARRAY; break; + case 5: // key + f->key = value; + break; default: return NULL; } @@ -485,7 +490,11 @@ sproto_dump(struct sproto *s) { type_name = buildin[t]; } } - printf("\t%s (%d) %s%s\n", f->name, f->tag, array, type_name); + if (f->key >= 0) { + printf("\t%s (%d) %s%s(%d)\n", f->name, f->tag, array, type_name, f->key); + } else { + printf("\t%s (%d) %s%s\n", f->name, f->tag, array, type_name); + } } } printf("=== %d protocol ===\n", s->protocol_n); @@ -637,22 +646,37 @@ encode_uint64(uint64_t v, uint8_t * data, int size) { return fill_size(data, sizeof(v)); } +/* +//#define CB(tagname,type,index,subtype,value,length) cb(ud, tagname,type,index,subtype,value,length) + static int -encode_string(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { +do_cb(sproto_callback cb, void *ud, const char *tagname, int type, int index, struct sproto_type *subtype, void *value, int length) { + if (subtype) { + if (type >= 0) { + printf("callback: tag=%s[%d], subtype[%s]:%d\n",tagname,index, subtype->name, type); + } else { + printf("callback: tag=%s[%d], subtype[%s]\n",tagname,index, subtype->name); + } + } else if (index > 0) { + printf("callback: tag=%s[%d]\n",tagname,index); + } else if (index == 0) { + printf("callback: tag=%s\n",tagname); + } else { + printf("callback: tag=%s [mainkey]\n",tagname); + } + return cb(ud, tagname,type,index,subtype,value,length); +} +#define CB(tagname,type,index,subtype,value,length) do_cb(cb,ud, tagname,type,index,subtype,value,length) +*/ + +static int +encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) { int sz; if (size < SIZEOF_LENGTH) return -1; - sz = cb(ud, f->name, SPROTO_TSTRING, 0, NULL, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); - return fill_size(data, sz); -} - -static int -encode_struct(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { - int sz; - if (size < SIZEOF_LENGTH) { - return -1; - } - sz = cb(ud, f->name, SPROTO_TSTRUCT, 0, f->st, data+SIZEOF_LENGTH, size-SIZEOF_LENGTH); + args->value = data+SIZEOF_LENGTH; + args->length = size-SIZEOF_LENGTH; + sz = cb(args); return fill_size(data, sz); } @@ -672,7 +696,7 @@ uint32_to_uint64(int negative, uint8_t *buffer) { } static uint8_t * -encode_integer_array(sproto_callback cb, void *ud, struct field *f, uint8_t *buffer, int size) { +encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size) { uint8_t * header = buffer; int intlen; int index; @@ -688,7 +712,10 @@ encode_integer_array(sproto_callback cb, void *ud, struct field *f, uint8_t *buf uint64_t u64; uint32_t u32; } u; - sz = cb(ud, f->name, SPROTO_TINTEGER, index, f->st, &u, sizeof(u)); + args->value = &u; + args->length = sizeof(u); + args->index = index; + sz = cb(args); if (sz < 0) return NULL; if (sz == 0) @@ -749,27 +776,26 @@ encode_integer_array(sproto_callback cb, void *ud, struct field *f, uint8_t *buf } static int -encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int size) { +encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) { uint8_t * buffer; - int index; - int type; int sz; if (size < SIZEOF_LENGTH) return -1; size -= SIZEOF_LENGTH; - index = 1; buffer = data + SIZEOF_LENGTH; - type = f->type & ~SPROTO_TARRAY; - switch (type) { + switch (args->type) { case SPROTO_TINTEGER: - buffer = encode_integer_array(cb,ud,f,buffer,size); + buffer = encode_integer_array(cb,args,buffer,size); if (buffer == NULL) return -1; break; case SPROTO_TBOOLEAN: + args->index = 1; for (;;) { int v = 0; - int sz = cb(ud, f->name, type, index, f->st, &v, sizeof(v)); + args->value = &v; + args->length = sizeof(v); + sz = cb(args); if (sz < 0) return -1; if (sz == 0) @@ -779,16 +805,18 @@ encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int s buffer[0] = v ? 1: 0; size -= 1; buffer += 1; - index++; + ++args->index; } break; default: + args->index = 1; for (;;) { - int sz; if (size < SIZEOF_LENGTH) return -1; size -= SIZEOF_LENGTH; - sz = cb(ud, f->name, type, index, f->st, buffer+SIZEOF_LENGTH, size); + args->value = buffer+SIZEOF_LENGTH; + args->length = size; + sz = cb(args); if (sz < 0) return -1; if (sz == 0) @@ -796,7 +824,7 @@ encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int s fill_size(buffer, sz); buffer += SIZEOF_LENGTH+sz; size -=sz; - index ++; + ++args->index; } break; } @@ -808,6 +836,7 @@ encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int s int sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) { + struct sproto_arg args; uint8_t * header = buffer; uint8_t * data; int header_sz = SIZEOF_HEADER + st->maxn * SIZEOF_FIELD; @@ -817,6 +846,7 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c int datasz; if (size < header_sz) return -1; + args.ud = ud; data = header + header_sz; size -= header_sz; index = 0; @@ -826,9 +856,16 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c int type = f->type; int value = 0; int sz = -1; + args.tagname = f->name; + args.tagid = f->tag; + args.subtype = f->st; + args.mainindex = f->key; if (type & SPROTO_TARRAY) { - sz = encode_array(cb,ud, f, data, size); + args.type = type & ~SPROTO_TARRAY; + sz = encode_array(cb, &args, data, size); } else { + args.type = type; + args.index = 0; switch(type) { case SPROTO_TINTEGER: case SPROTO_TBOOLEAN: { @@ -836,7 +873,9 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c uint64_t u64; uint32_t u32; } u; - sz = cb(ud, f->name, type, 0, NULL, &u, sizeof(u)); + args.value = &u; + args.length = sizeof(u); + sz = cb(&args); if (sz < 0) return -1; if (sz == 0) @@ -855,12 +894,9 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c } break; } + case SPROTO_TSTRUCT: case SPROTO_TSTRING: { - sz = encode_string(cb, ud, f, data, size); - break; - } - case SPROTO_TSTRUCT: { - sz = encode_struct(cb, ud, f, data, size); + sz = encode_object(cb, &args, data, size); break; } } @@ -904,9 +940,8 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c } static int -decode_array_object(sproto_callback cb, void *ud, struct field *f, uint8_t * stream, int sz) { +decode_array_object(sproto_callback cb, struct sproto_arg *args, uint8_t * stream, int sz) { uint32_t hsz; - int type = f->type & ~SPROTO_TARRAY; int index = 1; while (sz > 0) { if (sz < SIZEOF_LENGTH) @@ -916,7 +951,10 @@ decode_array_object(sproto_callback cb, void *ud, struct field *f, uint8_t * str sz -= SIZEOF_LENGTH; if (hsz > sz) return -1; - if (cb(ud, f->name, type, index, f->st, stream, hsz)) + args->index = index; + args->value = stream; + args->length = hsz; + if (cb(args)) return -1; sz -= hsz; stream += hsz; @@ -935,9 +973,9 @@ expand64(uint32_t v) { } static int -decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { +decode_array(sproto_callback cb, struct sproto_arg *args, uint8_t * stream) { uint32_t sz = todword(stream); - int type = f->type & ~SPROTO_TARRAY; + int type = args->type; int i; stream += SIZEOF_LENGTH; switch (type) { @@ -953,7 +991,10 @@ decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { return -1; for (i=0;iname, SPROTO_TINTEGER, i+1, NULL, &value, sizeof(value)); + args->index = i+1; + args->value = &value; + args->length = sizeof(value); + cb(args); } } else if (len == sizeof(uint64_t)) { if (sz % sizeof(uint64_t) != 0) @@ -962,7 +1003,10 @@ decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { uint64_t low = todword(stream + i*sizeof(uint64_t)); uint64_t hi = todword(stream + i*sizeof(uint64_t) + sizeof(uint32_t)); uint64_t value = low | hi << 32; - cb(ud, f->name, SPROTO_TINTEGER, i+1, NULL, &value, sizeof(value)); + args->index = i+1; + args->value = &value; + args->length = sizeof(value); + cb(args); } } else { return -1; @@ -972,12 +1016,15 @@ decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { case SPROTO_TBOOLEAN: for (i=0;iname, SPROTO_TBOOLEAN, i+1, NULL, &value, sizeof(value)); + args->index = i+1; + args->value = &value; + args->length = sizeof(value); + cb(args); } break; case SPROTO_TSTRING: case SPROTO_TSTRUCT: - return decode_array_object(cb, ud, f, stream, sz); + return decode_array_object(cb, args, stream, sz); default: return -1; } @@ -986,6 +1033,7 @@ decode_array(sproto_callback cb, void *ud, struct field *f, uint8_t * stream) { int sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callback cb, void *ud) { + struct sproto_arg args; int total = size; uint8_t * stream; uint8_t * datastream; @@ -994,6 +1042,8 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba int tag; if (size < SIZEOF_HEADER) return -1; + // debug print + // printf("sproto_decode[%p] (%s)\n", ud, st->name); stream = (void *)data; fn = toword(stream); stream += SIZEOF_HEADER; @@ -1001,7 +1051,8 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba if (size < fn * SIZEOF_FIELD) return -1; datastream = stream + fn * SIZEOF_FIELD; - size -= fn * SIZEOF_FIELD ; + size -= fn * SIZEOF_FIELD; + args.ud = ud; tag = -1; for (i=0;iname; + args.tagid = f->tag; + args.type = f->type & ~SPROTO_TARRAY; + args.subtype = f->st; + args.index = 0; + args.mainindex = f->key; if (value < 0) { if (f->type & SPROTO_TARRAY) { - if (decode_array(cb, ud, f, currentdata)) { + if (decode_array(cb, &args, currentdata)) { return -1; } } else { @@ -1039,21 +1096,27 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba uint32_t sz = todword(currentdata); if (sz == sizeof(uint32_t)) { uint64_t v = expand64(todword(currentdata + SIZEOF_LENGTH)); - cb(ud, f->name, SPROTO_TINTEGER, 0, NULL, &v, sizeof(v)); + args.value = &v; + args.length = sizeof(v); + cb(&args); } else if (sz != sizeof(uint64_t)) { return -1; } else { uint32_t low = todword(currentdata + SIZEOF_LENGTH); uint32_t hi = todword(currentdata + SIZEOF_LENGTH + sizeof(uint32_t)); uint64_t v = (uint64_t)low | (uint64_t) hi << 32; - cb(ud, f->name, SPROTO_TINTEGER, 0, NULL, &v, sizeof(v)); + args.value = &v; + args.length = sizeof(v); + cb(&args); } break; } case SPROTO_TSTRING: case SPROTO_TSTRUCT: { uint32_t sz = todword(currentdata); - if (cb(ud, f->name, f->type, 0, f->st, currentdata+SIZEOF_LENGTH, sz)) + args.value = currentdata+SIZEOF_LENGTH; + args.length = sz; + if (cb(&args)) return -1; break; } @@ -1065,7 +1128,9 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba return -1; } else { uint64_t v = value; - cb(ud, f->name, f->type, 0, NULL, &v, sizeof(v)); + args.value = &v; + args.length = sizeof(v); + cb(&args); } } return total - size; diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index 3286a423..934e18f8 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -27,7 +27,19 @@ struct sproto_type * sproto_type(struct sproto *, const char * type_name); int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); -typedef int (*sproto_callback)(void *ud, const char *tagname, int type, int index, struct sproto_type *, void *value, int length); +struct sproto_arg { + void *ud; + const char *tagname; + int tagid; + int type; + struct sproto_type *subtype; + void *value; + int length; + int index; // array base 1 + int mainindex; // for map +}; + +typedef int (*sproto_callback)(const struct sproto_arg *args); int sproto_decode(struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); int sproto_encode(struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index cf65e536..4761bcca 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -1,6 +1,40 @@ local lpeg = require "lpeg" local table = require "table" +local packbytes +local packvalue + +if _VERSION == "Lua 5.3" then + function packbytes(str) + return string.pack("=0 and id < 65536) + local a = id % 256 + local b = math.floor(id / 256) + return string.char(a) .. string.char(b) + end +end + local P = lpeg.P local S = lpeg.S local R = lpeg.R @@ -35,6 +69,7 @@ local word = alpha * alnum ^ 0 local name = C(word) local typename = C(word * ("." * word) ^ 0) local tag = R"09" ^ 1 / tonumber +local mainkey = "(" * blank0 * name * blank0 * ")" local function multipat(pat) return Ct(blank0 * (pat * blanks) ^ 0 * pat^0 * blank0) @@ -46,7 +81,7 @@ end local typedef = P { "ALL", - FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^0 * typename)), + FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^0 * typename * mainkey^0)), STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}", TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ), SUBPROTO = Ct((C"request" + C"response") * blanks * (name + V"STRUCT")), @@ -97,6 +132,11 @@ function convert.type(all, obj) field.array = true fieldtype = f[4] end + local mainkey = f[5] + if mainkey then + assert(field.array) + field.key = mainkey + end field.typename = fieldtype else assert(f.type == "type") -- nest type @@ -176,6 +216,7 @@ end type 2 : integer tag 3 : integer array 4 : boolean + key 5 : integer # If key exists, array must be true, and it's a map. } name 0 : string fields 1 : *field @@ -194,25 +235,18 @@ end } ]] -local function packbytes(str) - str = string.pack(" Date: Mon, 16 Mar 2015 17:39:07 +0800 Subject: [PATCH 325/729] release alpha2 --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 779f459f..c4d0ff2c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +v1.0.0-alpha2 (2015-3-16) +----------- +* Update examples client to lua 5.3 +* Patch lua 5.3 to interrupt the dead loop (for debug) +* Update sproto (fix some bugs and support unordered map) + v1.0.0-alpha (2015-3-9) ----------- * Update lua from 5.2 to 5.3 From 92b7b7beffdba8b99b5b21543d60ad5708f92d1e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 16 Mar 2015 20:01:22 +0800 Subject: [PATCH 326/729] use table for multi header key --- lualib/http/httpd.lua | 9 ++++++++- lualib/http/internal.lua | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index c19d4bae..0131db0a 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -2,6 +2,7 @@ local internal = require "http.internal" local table = table local string = string +local type = type local httpd = {} @@ -113,7 +114,13 @@ local function writeall(writefunc, statuscode, bodyfunc, header) writefunc(statusline) if header then for k,v in pairs(header) do - writefunc(string.format("%s: %s\r\n", k,v)) + if type(v) == "table" then + for _,v in ipairs(v) do + writefunc(string.format("%s: %s\r\n", k,v)) + end + else + writefunc(string.format("%s: %s\r\n", k,v)) + end end end local t = type(bodyfunc) diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 0e83bc72..5bb35752 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -1,3 +1,6 @@ +local table = table +local type = type + local M = {} local LIMIT = 8192 @@ -83,7 +86,12 @@ function M.parseheader(lines, from, header) end name = name:lower() if header[name] then - header[name] = header[name] .. ", " .. value + local v = header[name] + if type(v) == "table" then + table.insert(v, value) + else + header[name] = { v , value } + end else header[name] = value end From 7e5d9b73993b8306ab8715943aa6696ee6c4c4da Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 18 Mar 2015 11:07:34 +0800 Subject: [PATCH 327/729] bugfix Issue #249 --- lualib-src/sproto/lsproto.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index a546b51d..fa2310ad 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -104,6 +104,7 @@ encode(const struct sproto_arg *args) { return luaL_error(L, "The table is too deep"); if (args->index > 0) { if (args->tagname != self->array_tag) { + // a new array self->array_tag = args->tagname; lua_getfield(L, self->tbl_index, args->tagname); if (lua_isnil(L, -1)) { @@ -176,6 +177,7 @@ encode(const struct sproto_arg *args) { case SPROTO_TSTRUCT: { struct encode_ud sub; int r; + int top = lua_gettop(L); sub.L = L; sub.st = args->subtype; sub.tbl_index = lua_gettop(L); @@ -185,7 +187,7 @@ encode(const struct sproto_arg *args) { lua_pushnil(L); // prepare an iterator slot sub.iter_index = sub.tbl_index + 1; r = sproto_encode(args->subtype, args->value, args->length, encode, &sub); - lua_pop(L,2); // pop the value and the iterator slot + lua_settop(L, top-1); // pop the value return r; } default: @@ -222,29 +224,30 @@ lencode(lua_State *L) { struct encode_ud self; void * buffer = lua_touserdata(L, lua_upvalueindex(1)); int sz = lua_tointeger(L, lua_upvalueindex(2)); - + int tbl_index = 2; struct sproto_type * st = lua_touserdata(L, 1); if (st == NULL) { return luaL_argerror(L, 1, "Need a sproto_type object"); } - luaL_checktype(L, 2, LUA_TTABLE); + luaL_checktype(L, tbl_index, LUA_TTABLE); luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); self.L = L; self.st = st; - self.tbl_index = 2; - self.array_tag = NULL; - self.array_index = 0; - self.deep = 0; - lua_pushnil(L); // for iterator (stack 3) - self.iter_index = 3; + self.tbl_index = tbl_index; for (;;) { - int r = sproto_encode(st, buffer, sz, encode, &self); + int r; + self.array_tag = NULL; + self.array_index = 0; + self.deep = 0; + + lua_settop(L, tbl_index); + lua_pushnil(L); // for iterator (stack slot 3) + self.iter_index = tbl_index+1; + + r = sproto_encode(st, buffer, sz, encode, &self); if (r<0) { buffer = expand_buffer(L, sz, sz*2); sz *= 2; - // reset iterator slot - lua_pushnil(L); - lua_replace(L, 3); } else { lua_pushlstring(L, buffer, r); return 1; From 65e7da9793a364e4cfa771bb3c8aa92e1d941d11 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 18 Mar 2015 11:50:47 +0800 Subject: [PATCH 328/729] more sproto error message --- lualib-src/sproto/lsproto.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index fa2310ad..5e683566 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -145,8 +145,14 @@ encode(const struct sproto_arg *args) { } switch (args->type) { case SPROTO_TINTEGER: { - lua_Integer v = luaL_checkinteger(L, -1); + lua_Integer v; lua_Integer vh; + if (!lua_isinteger(L, -1)) { + return luaL_error(L, ".%s[%d] is not an integer (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } else { + v = lua_tointeger(L, -1); + } lua_pop(L,1); // notice: in lua 5.2, lua_Integer maybe 52bit vh = v >> 31; @@ -167,7 +173,13 @@ encode(const struct sproto_arg *args) { } case SPROTO_TSTRING: { size_t sz = 0; - const char * str = luaL_checklstring(L, -1, &sz); + const char * str; + if (!lua_isstring(L, -1)) { + return luaL_error(L, ".%s[%d] is not a string (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } else { + str = lua_tolstring(L, -1, &sz); + } if (sz > args->length) return -1; memcpy(args->value, str, sz); From 8d8add37102648618e471555032fe911fcc8a3ce Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 18 Mar 2015 12:17:01 +0800 Subject: [PATCH 329/729] update sproto --- lualib-src/sproto/lsproto.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 5e683566..7ed70d18 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -39,6 +39,13 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { #define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #endif +#if LUA_VERSION_NUM < 503 + +// lua_isinteger is lua 5.3 api +#define lua_isinteger lua_isnumber + +#endif + static int lnewproto(lua_State *L) { size_t sz = 0; From 05b458ddb29eee348130080977e3a06d27ad9d3c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 20 Mar 2015 09:51:00 +0800 Subject: [PATCH 330/729] update sproto --- lualib/sprotoparser.lua | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 4761bcca..e7da7fa0 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -84,7 +84,7 @@ local typedef = P { FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^0 * typename * mainkey^0)), STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}", TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ), - SUBPROTO = Ct((C"request" + C"response") * blanks * (name + V"STRUCT")), + SUBPROTO = Ct((C"request" + C"response") * blanks * (typename + V"STRUCT")), PROTOCOL = namedpat("protocol", name * blanks * tag * blank0 * P"{" * multipat(V"SUBPROTO") * P"}"), ALL = multipat(V"TYPE" + V"PROTOCOL"), } @@ -186,6 +186,32 @@ local function checktype(types, ptype, t) end end +local function check_protocol(r) + local map = {} + local type = r.type + for name, v in pairs(r.protocol) do + local tag = v.tag + local request = v.request + local response = v.response + local p = map[tag] + + if p then + error(string.format("redefined protocol tag %d at %s", tag, name)) + end + + if request and not type[request] then + error(string.format("Undefined request type %s in protocol %s", request, name)) + end + + if response and not type[response] then + error(string.format("Undefined response type %s in protocol %s", response, name)) + end + + map[tag] = v + end + return r +end + local function flattypename(r) for typename, t in pairs(r.type) do for _, f in pairs(t) do @@ -204,7 +230,7 @@ end local function parser(text,filename) local state = { file = filename, pos = 0, line = 1 } local r = lpeg.match(proto * -1 + exception , text , 1, state ) - return flattypename(adjust(r)) + return flattypename(check_protocol(adjust(r))) end --[[ From a9b1993686ae0cb46336352dae2493ff01e2a11a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 29 Mar 2015 14:41:28 +0800 Subject: [PATCH 331/729] Add async dns query --- lualib/dns.lua | 290 +++++++++++++++++++++++++++++++++++++++++++++++ test/testdns.lua | 11 ++ 2 files changed, 301 insertions(+) create mode 100644 lualib/dns.lua create mode 100644 test/testdns.lua diff --git a/lualib/dns.lua b/lualib/dns.lua new file mode 100644 index 00000000..4636bf6d --- /dev/null +++ b/lualib/dns.lua @@ -0,0 +1,290 @@ +--[[ + lua dns resolver library + See https://github.com/xjdrew/levent/blob/master/levent/dns.lua for more detail + +-- resource record type: +-- TYPE value and meaning +-- A 1 a host address +-- NS 2 an authoritative name server +-- MD 3 a mail destination (Obsolete - use MX) +-- MF 4 a mail forwarder (Obsolete - use MX) +-- CNAME 5 the canonical name for an alias +-- SOA 6 marks the start of a zone of authority +-- MB 7 a mailbox domain name (EXPERIMENTAL) +-- MG 8 a mail group member (EXPERIMENTAL) +-- MR 9 a mail rename domain name (EXPERIMENTAL) +-- NULL 10 a null RR (EXPERIMENTAL) +-- WKS 11 a well known service description +-- PTR 12 a domain name pointer +-- HINFO 13 host information +-- MINFO 14 mailbox or mail list information +-- MX 15 mail exchange +-- TXT 16 text strings +-- AAAA 28 a ipv6 host address +-- only appear in the question section: +-- AXFR 252 A request for a transfer of an entire zone +-- MAILB 253 A request for mailbox-related records (MB, MG or MR) +-- MAILA 254 A request for mail agent RRs (Obsolete - see MX) +-- * 255 A request for all records +-- +-- resource recode class: +-- IN 1 the Internet +-- CS 2 the CSNET class (Obsolete - used only for examples in some obsolete RFCs) +-- CH 3 the CHAOS class +-- HS 4 Hesiod [Dyer 87] +-- only appear in the question section: +-- * 255 any class +-- ]] + +--[[ +-- struct header { +-- uint16_t tid # identifier assigned by the program that generates any kind of query. +-- uint16_t flags # flags +-- uint16_t qdcount # the number of entries in the question section. +-- uint16_t ancount # the number of resource records in the answer section. +-- uint16_t nscount # the number of name server resource records in the authority records section. +-- uint16_t arcount # the number of resource records in the additional records section. +-- } +-- +-- request body: +-- struct request { +-- string name +-- uint16_t atype +-- uint16_t class +-- } +-- +-- response body: +-- struct response { +-- string name +-- uint16_t atype +-- uint16_t class +-- uint16_t ttl +-- uint16_t rdlength +-- string rdata +-- } +--]] + +local skynet = require "skynet" +local socket = require "socket" + +local MAX_DOMAIN_LEN = 1024 +local MAX_LABEL_LEN = 63 +local MAX_PACKET_LEN = 2048 +local DNS_HEADER_LEN = 12 + +local QTYPE = { + A = 1, + CNAME = 5, + AAAA = 28, +} + +local QCLASS = { + IN = 1, +} + +local dns = {} + +local function verify_domain_name(name) + if #name > MAX_DOMAIN_LEN then + return false + end + if not name:match("^[%l%d-%.]+$") then + return false + end + for w in name:gmatch("([%w-]+)%.?") do + if #w > MAX_LABEL_LEN then + return false + end + end + return true +end + +local next_tid = 1 +local function gen_tid() + local tid = next_tid + next_tid = next_tid + 1 + return tid +end + +local function pack_header(t) + return string.pack(">HHHHHH", + t.tid, t.flags, t.qdcount, t.ancount or 0, t.nscount or 0, t.arcount or 0) +end + +local function pack_question(name, qtype, qclass) + local labels = {} + for w in name:gmatch("([%w-]+)%.?") do + table.insert(labels, string.pack("s1",w)) + end + table.insert(labels, '\0') + table.insert(labels, string.pack(">HH", qtype, qclass)) + return table.concat(labels) +end + +local function unpack_header(chunk) + local tid, flags, qdcount, ancount, nscount, arcount, left = string.unpack(">HHHHHH", chunk) + return { + tid = tid, + flags = flags, + qdcount = qdcount, + ancount = ancount, + nscount = nscount, + arcount = arcount + }, left +end + +-- unpack a resource name +local function unpack_name(chunk, left) + local t = {} + local jump_pointer + local tag, offset, label + while true do + tag, left = string.unpack("B", chunk, left) + if tag & 0xc0 == 0xc0 then + -- pointer + offset,left = string.unpack(">H", chunk, left - 1) + offset = offset & 0x3fff + if not jump_pointer then + jump_pointer = left + end + -- offset is base 0, need to plus 1 + left = offset + 1 + elseif tag == 0 then + break + else + label, left = string.unpack("s1", chunk, left - 1) + t[#t+1] = label + end + end + return table.concat(t, "."), jump_pointer or left +end + +local function unpack_question(chunk, left) + local name, left = unpack_name(chunk, left) + local atype, class, left = string.unpack(">HH", chunk, left) + return { + name = name, + atype = atype, + class = class + }, left +end + +local function unpack_answer(chunk, left) + local name, left = unpack_name(chunk, left) + local atype, class, ttl, rdata, left = string.unpack(">HHI4s2", chunk, left) + return { + name = name, + atype = atype, + class = class, + ttl = ttl, + rdata = rdata + },left +end + +local function unpack_rdata(qtype, chunk) + if qtype == QTYPE.A then + local a,b,c,d = string.unpack("BBBB", chunk) + return string.format("%d.%d.%d.%d", a,b,c,d) + elseif qtype == QTYPE.AAAA then + local a,b,c,d,e,f,g,h = string.unpack(">HHHHHHHH", chunk) + return string.format("%x:%x:%x:%x:%x:%x:%x:%x", a, b, c, d, e, f, g, h) + else + error("Error qtype " .. qtype) + end +end + +local dns_server +local request_pool = {} + +local function resolve(content) + if #content < DNS_HEADER_LEN then + -- drop + skynet.error("Recv an invalid package when dns query") + return + end + local answer_header,left = unpack_header(content) + -- verify answer + assert(answer_header.qdcount == 1, "malformed packet") + + local resp = request_pool[answer_header.tid] + if not resp then + skynet.error("Recv an invalid tid when dns query") + return + end + + local question,left = unpack_question(content, left) + if question.name ~= resp.name then + skynet.error("Recv an invalid name when dns query") + return + end + + local ttl + local answer + local answers = {} + for i=1, answer_header.ancount do + answer, left = unpack_answer(content, left) + -- only extract qtype address + if answer.atype == resp.qtype then + local ip = unpack_rdata(resp.qtype, answer.rdata) + ttl = ttl and math.min(ttl, answer.ttl) or answer.ttl + answers[#answers+1] = ip + end + end + + if #answers > 0 then + resp.answers = answers + end + + skynet.wakeup(resp.co) +end + +function dns.server(server, port) + if not server then + local f = assert(io.open "/etc/resolv.conf") + for line in f:lines() do + server = line:match("%s*nameserver%s+([^%s]+)") + if server then + break + end + end + assert(server, "Can't get nameserver") + end + dns_server = socket.udp(function(data, sz, from) + resolve(skynet.tostring(data,sz)) + end) + socket.udp_connect(dns_server, server, port or 53) + return server +end + +local function suspend(tid, name, qtype) + local req = { + name = name, + tid = tid, + qtype = qtype, + time = skynet.now(), -- for timeout + co = coroutine.running(), + } + request_pool[tid] = req + skynet.wait() + local answers = request_pool[tid].answers + request_pool[tid] = nil + assert(answers, "no ip") + return answers[1], answers +end + +function dns.resolve(name, ipv6) + local qtype = ipv6 and QTYPE.AAAA or QTYPE.A + local name = name:lower() + assert(verify_domain_name(name) , "illegal name") + local question_header = { + tid = gen_tid(), + flags = 0x100, -- flags: 00000001 00000000, set RD + qdcount = 1, + } + local req = pack_header(question_header) .. pack_question(name, qtype, QCLASS.IN) + assert(dns_server, "Call dns.server fist") + socket.write(dns_server, req) + return suspend(question_header.tid, name, qtype) +end + +return dns diff --git a/test/testdns.lua b/test/testdns.lua new file mode 100644 index 00000000..1d767ce0 --- /dev/null +++ b/test/testdns.lua @@ -0,0 +1,11 @@ +local skynet = require "skynet" +local dns = require "dns" + +skynet.start(function() + print("nameserver:", dns.server()) -- set nameserver + -- you can specify the server like dns.server("8.8.4.4", 53) + local ip, ips = dns.resolve "github.com" + for k,v in ipairs(ips) do + print("github.com",v) + end +end) From 36ed8679b869c17f84e1a46db4f702470385c5ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 30 Mar 2015 09:29:22 +0800 Subject: [PATCH 332/729] you can set host in httpc header --- lualib/http/httpc.lua | 7 ++++++- test/testhttp.lua | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index a73d5710..823d33b5 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -14,10 +14,15 @@ local function request(fd, method, host, url, recvheader, header, content) for k,v in pairs(header) do header_content = string.format("%s%s:%s\r\n", header_content, k, v) end + if header.host then + host = "" + end + else + host = string.format("host:%s\r\n",host) end if content then - local data = string.format("%s %s HTTP/1.1\r\nhost:%s\r\ncontent-length:%d\r\n%s\r\n%s", method, url, host, #content, header_content, content) + local data = string.format("%s %s HTTP/1.1\r\n%scontent-length:%d\r\n%s\r\n%s", method, url, host, #content, header_content, content) write(data) else local request_header = string.format("%s %s HTTP/1.1\r\nhost:%s\r\ncontent-length:0\r\n%s\r\n", method, url, host, header_content) diff --git a/test/testhttp.lua b/test/testhttp.lua index 9fd0804c..cba1488a 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -1,16 +1,24 @@ local skynet = require "skynet" local httpc = require "http.httpc" +local dns = require "dns" skynet.start(function() print("GET baidu.com") - local header = {} - local status, body = httpc.get("baidu.com", "/", header) + local respheader = {} + local status, body = httpc.get("baidu.com", "/", respheader) print("[header] =====>") - for k,v in pairs(header) do + for k,v in pairs(respheader) do print(k,v) end print("[body] =====>", status) print(body) + local respheader = {} + dns.server() + local ip = dns.resolve "baidu.com" + print(string.format("GET %s (baidu.com)", ip)) + local status, body = httpc.get("baidu.com", "/", respheader, { host = "baidu.com" }) + print(status) + skynet.exit() end) From c512b5f59a9ad27793d03a8317c875a64aebc08f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 30 Mar 2015 11:00:51 +0800 Subject: [PATCH 333/729] release alpha 3 --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index c4d0ff2c..5eb08748 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +v1.0.0-alpha3 (2015-3-30) +----------- +* Update sproto (bugfix) +* Add async dns query +* improve httpc + v1.0.0-alpha2 (2015-3-16) ----------- * Update examples client to lua 5.3 From 41b447d41ae47e45d9610b53be68de67535aee36 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 1 Apr 2015 12:18:49 +0800 Subject: [PATCH 334/729] update sproto: check table --- lualib-src/sproto/lsproto.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 7ed70d18..cb655e3e 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -121,6 +121,9 @@ encode(const struct sproto_arg *args) { self->array_index = 0; return 0; } + if (!lua_istable(L, -1)) { + return luaL_error(L, "%s is not a table", args->tagname); + } if (self->array_index) { lua_replace(L, self->array_index); } else { From 6b87f159b42bedae2b7831ba3a8fdca9000b1fc1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 1 Apr 2015 13:22:09 +0800 Subject: [PATCH 335/729] more type check --- lualib-src/sproto/lsproto.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index cb655e3e..7a36c721 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -122,7 +122,8 @@ encode(const struct sproto_arg *args) { return 0; } if (!lua_istable(L, -1)) { - return luaL_error(L, "%s is not a table", args->tagname); + return luaL_error(L, ".*%s(%d) should be a table (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); } if (self->array_index) { lua_replace(L, self->array_index); @@ -177,6 +178,10 @@ encode(const struct sproto_arg *args) { } case SPROTO_TBOOLEAN: { int v = lua_toboolean(L, -1); + if (!lua_isboolean(L,-1)) { + return luaL_error(L, ".%s[%d] is not a boolean (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } *(int *)args->value = v; lua_pop(L,1); return 4; @@ -200,9 +205,13 @@ encode(const struct sproto_arg *args) { struct encode_ud sub; int r; int top = lua_gettop(L); + if (!lua_istable(L, top)) { + return luaL_error(L, ".%s[%d] is not a table (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } sub.L = L; sub.st = args->subtype; - sub.tbl_index = lua_gettop(L); + sub.tbl_index = top; sub.array_tag = NULL; sub.array_index = 0; sub.deep = self->deep + 1; From ec1bca238e2ea6bca3fd641a36d2bc821e0bda95 Mon Sep 17 00:00:00 2001 From: HuaYang Huang Date: Wed, 1 Apr 2015 17:54:22 +0800 Subject: [PATCH 336/729] fix: httpc.post MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调用httpc.post时,如果没有在header里é¢è®¾ç½®host时有问题 --- lualib/http/httpc.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 823d33b5..a8b7d6f8 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -16,6 +16,8 @@ local function request(fd, method, host, url, recvheader, header, content) end if header.host then host = "" + else + host = string.format("host:%s\r\n", host) end else host = string.format("host:%s\r\n",host) From c6f993428ec97de5dd8fa6006e70d88dfe29536b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 3 Apr 2015 11:03:37 +0800 Subject: [PATCH 337/729] bugfix #257 --- lualib/sharemap.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lualib/sharemap.lua b/lualib/sharemap.lua index 5b0e5845..6f9b1bdf 100644 --- a/lualib/sharemap.lua +++ b/lualib/sharemap.lua @@ -30,12 +30,14 @@ function sharemap.writer(typename, obj) local sp = loadsp() obj = obj or {} local stmobj = stm.new(sp:encode(typename,obj)) - obj.__typename = typename - obj.__obj = stmobj - obj.__data = obj - obj.commit = sharemap.commit - obj.copy = sharemap.copy - return setmetatable(obj, { __index = obj.__data, __newindex = obj.__data }) + local ret = { + __typename = typename, + __obj = stmobj, + __data = obj, + commit = sharemap.commit, + copy = sharemap.copy, + } + return setmetatable(ret, { __index = obj, __newindex = obj }) end local function decode(msg, sz, self) @@ -63,7 +65,7 @@ function sharemap.reader(typename, stmcpy) __data = data, update = sharemap.update, } - return setmetatable(obj, { __index = obj.__data, __newindex = error }) + return setmetatable(obj, { __index = data, __newindex = error }) end return sharemap From 7d8c80c9a05d7ecfda59ef1eeb27e4eda1f121ff Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 3 Apr 2015 11:07:11 +0800 Subject: [PATCH 338/729] fix Issue #256 --- skynet-src/malloc_hook.h | 6 +++--- skynet-src/rwlock.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index e7beb1a7..d35551d1 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -1,5 +1,5 @@ -#ifndef __MALLOC_HOOK_H -#define __MALLOC_HOOK_H +#ifndef SKYNET_MALLOC_HOOK_H +#define SKYNET_MALLOC_HOOK_H #include @@ -10,5 +10,5 @@ extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); extern void dump_c_mem(void); -#endif /* __MALLOC_HOOK_H */ +#endif /* SKYNET_MALLOC_HOOK_H */ diff --git a/skynet-src/rwlock.h b/skynet-src/rwlock.h index 46357764..e366f18c 100644 --- a/skynet-src/rwlock.h +++ b/skynet-src/rwlock.h @@ -1,5 +1,5 @@ -#ifndef _RWLOCK_H_ -#define _RWLOCK_H_ +#ifndef SKYNET_RWLOCK_H +#define SKYNET_RWLOCK_H struct rwlock { int write; From ca33bf8b3e03ec43cec833af47b8e043a5056344 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 5 Apr 2015 20:19:30 +0800 Subject: [PATCH 339/729] bugfix sproto : support empty string --- lualib-src/sproto/lsproto.c | 2 +- lualib-src/sproto/sproto.c | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 7a36c721..42eccd8c 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -199,7 +199,7 @@ encode(const struct sproto_arg *args) { return -1; memcpy(args->value, str, sz); lua_pop(L,1); - return sz; + return sz + 1; // The length of empty string is 1. } case SPROTO_TSTRUCT: { struct encode_ud sub; diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 910266da..ac64bcff 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -609,10 +609,6 @@ findtag(struct sproto_type *st, int tag) { static inline int fill_size(uint8_t * data, int sz) { - if (sz < 0) - return -1; - if (sz == 0) - return 0; data[0] = sz & 0xff; data[1] = (sz >> 8) & 0xff; data[2] = (sz >> 16) & 0xff; @@ -677,6 +673,11 @@ encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int si args->value = data+SIZEOF_LENGTH; args->length = size-SIZEOF_LENGTH; sz = cb(args); + if (sz <= 0) + return sz; + if (args->type == SPROTO_TSTRING) { + --sz; // the length of null string is 1 + } return fill_size(data, sz); } @@ -718,7 +719,7 @@ encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffe sz = cb(args); if (sz < 0) return NULL; - if (sz == 0) + if (sz == 0) // nil object, end of array break; if (size < sizeof(uint64_t)) return NULL; @@ -798,7 +799,7 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz sz = cb(args); if (sz < 0) return -1; - if (sz == 0) + if (sz == 0) // nil object , end of array break; if (size < 1) return -1; @@ -817,10 +818,13 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz args->value = buffer+SIZEOF_LENGTH; args->length = size; sz = cb(args); - if (sz < 0) - return -1; if (sz == 0) break; + if (sz < 0) + return -1; + if (args->type == SPROTO_TSTRING) { + --sz; + } fill_size(buffer, sz); buffer += SIZEOF_LENGTH+sz; size -=sz; @@ -829,7 +833,7 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz break; } sz = buffer - (data + SIZEOF_LENGTH); - if (sz == 0) + if (sz == 0) // empty array return 0; return fill_size(data, sz); } @@ -878,7 +882,7 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c sz = cb(&args); if (sz < 0) return -1; - if (sz == 0) + if (sz == 0) // nil object continue; if (sz == sizeof(uint32_t)) { if (u.u32 < 0x7fff) { @@ -895,11 +899,10 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c break; } case SPROTO_TSTRUCT: - case SPROTO_TSTRING: { + case SPROTO_TSTRING: sz = encode_object(cb, &args, data, size); break; } - } } if (sz < 0) return -1; From 5a1132101bf281ffb73fa29a439955fb85178ebd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 8 Apr 2015 11:23:52 +0800 Subject: [PATCH 340/729] share sproto C struct --- lualib-src/sproto/lsproto.c | 46 ++++++++++++------------------------- lualib/sproto.lua | 15 +++++++++--- lualib/sprotoloader.lua | 13 +++++++---- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 42eccd8c..c33118d4 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -48,15 +48,9 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { static int lnewproto(lua_State *L) { - size_t sz = 0; - void * buffer; struct sproto * sp; - if (lua_isuserdata(L,1)) { - buffer = lua_touserdata(L,1); - sz = luaL_checkinteger(L,2); - } else { - buffer = (void *)luaL_checklstring(L,1,&sz); - } + size_t sz; + void * buffer = (void *)luaL_checklstring(L,1,&sz); sp = sproto_create(buffer, sz); if (sp) { lua_pushlightuserdata(L, sp); @@ -545,48 +539,38 @@ lprotocol(lua_State *L) { return 3; } -/* global sproto pointer for multi states */ -struct sproto_bin { - void *ptr; - size_t sz; -}; - -static struct sproto_bin G_sproto[MAX_GLOBALSPROTO]; +/* global sproto pointer for multi states + NOTICE : It is not thread safe + */ +static struct sproto * G_sproto[MAX_GLOBALSPROTO]; static int lsaveproto(lua_State *L) { - size_t sz; - void * buffer = (void *)luaL_checklstring(L,1,&sz); + struct sproto * sp = lua_touserdata(L, 1); int index = luaL_optinteger(L, 2, 0); - void * tmp; - struct sproto_bin * sbin = &G_sproto[index]; if (index < 0 || index >= MAX_GLOBALSPROTO) { return luaL_error(L, "Invalid global slot index %d", index); } - tmp = malloc(sz); - memcpy(tmp, buffer, sz); - if (sbin->ptr) { - free(sbin->ptr); - } - sbin->ptr = tmp; - sbin->sz = sz; + /* TODO : release old object (memory leak now, but thread safe)*/ + G_sproto[index] = sp; return 0; } static int lloadproto(lua_State *L) { int index = luaL_optinteger(L, 1, 0); - struct sproto_bin * sbin = &G_sproto[index]; + struct sproto * sp; if (index < 0 || index >= MAX_GLOBALSPROTO) { return luaL_error(L, "Invalid global slot index %d", index); } - if (sbin->ptr == NULL) { + sp = G_sproto[index]; + if (sp == NULL) { return luaL_error(L, "nil sproto at index %d", index); } - lua_pushlightuserdata(L, sbin->ptr); - lua_pushinteger(L, sbin->sz); - return 2; + lua_pushlightuserdata(L, sp); + + return 1; } int diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 4c285222..7a722f55 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -13,14 +13,23 @@ function sproto_mt:__gc() core.deleteproto(self.__cobj) end -function sproto.new(bin,sz,nogc) - local cobj = assert(core.newproto(bin,sz)) +function sproto.new(bin) + local cobj = assert(core.newproto(bin)) local self = { __cobj = cobj, __tcache = setmetatable( {} , weak_mt ), __pcache = setmetatable( {} , weak_mt ), } - return setmetatable(self, nogc and sproto_nogc or sproto_mt) + return setmetatable(self, sproto_mt) +end + +function sproto.sharenew(cobj) + local self = { + __cobj = cobj, + __tcache = setmetatable( {} , weak_mt ), + __pcache = setmetatable( {} , weak_mt ), + } + return setmetatable(self, sproto_nogc) end function sproto.parse(ptext) diff --git a/lualib/sprotoloader.lua b/lualib/sprotoloader.lua index 05d17293..e88a9260 100644 --- a/lualib/sprotoloader.lua +++ b/lualib/sprotoloader.lua @@ -8,16 +8,19 @@ function loader.register(filename, index) local f = assert(io.open(filename), "Can't open sproto file") local data = f:read "a" f:close() - local bin = parser.parse(data) - core.saveproto(bin, index) + local sp = core.newproto(parser.parse(data)) + core.saveproto(sp, index) end -loader.save = core.saveproto +function loader.save(bin, index) + local sp = core.newproto(bin) + core.saveproto(sp, index) +end function loader.load(index) - local bin, sz = core.loadproto(index) + local sp = core.loadproto(index) -- no __gc in metatable - return sproto.new(bin,sz, true) + return sproto.sharenew(sp) end return loader From c7ed5279116db060f7c466604f98e700dba21c0a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 8 Apr 2015 11:42:06 +0800 Subject: [PATCH 341/729] add const to sproto C api --- lualib-src/sproto/sproto.c | 16 ++++++++-------- lualib-src/sproto/sproto.h | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index ac64bcff..c37fdc1b 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -510,7 +510,7 @@ sproto_dump(struct sproto *s) { // query int -sproto_prototag(struct sproto *sp, const char * name) { +sproto_prototag(const struct sproto *sp, const char * name) { int i; for (i=0;iprotocol_n;i++) { if (strcmp(name, sp->proto[i].name) == 0) { @@ -521,7 +521,7 @@ sproto_prototag(struct sproto *sp, const char * name) { } static struct protocol * -query_proto(struct sproto *sp, int tag) { +query_proto(const struct sproto *sp, int tag) { int begin = 0, end = sp->protocol_n; while(begin1) { return NULL; @@ -552,7 +552,7 @@ sproto_protoquery(struct sproto *sp, int proto, int what) { } const char * -sproto_protoname(struct sproto *sp, int proto) { +sproto_protoname(const struct sproto *sp, int proto) { struct protocol * p = query_proto(sp, proto); if (p) { return p->name; @@ -561,7 +561,7 @@ sproto_protoname(struct sproto *sp, int proto) { } struct sproto_type * -sproto_type(struct sproto *sp, const char * type_name) { +sproto_type(const struct sproto *sp, const char * type_name) { int i; for (i=0;itype_n;i++) { if (strcmp(type_name, sp->type[i].name) == 0) { @@ -577,7 +577,7 @@ sproto_name(struct sproto_type * st) { } static struct field * -findtag(struct sproto_type *st, int tag) { +findtag(const struct sproto_type *st, int tag) { int begin, end; if (st->base >=0 ) { tag -= st->base; @@ -839,7 +839,7 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz } int -sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) { +sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) { struct sproto_arg args; uint8_t * header = buffer; uint8_t * data; @@ -1035,7 +1035,7 @@ decode_array(sproto_callback cb, struct sproto_arg *args, uint8_t * stream) { } int -sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callback cb, void *ud) { +sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_callback cb, void *ud) { struct sproto_arg args; int total = size; uint8_t * stream; diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index 934e18f8..0a419c86 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -17,12 +17,12 @@ struct sproto_type; struct sproto * sproto_create(const void * proto, size_t sz); void sproto_release(struct sproto *); -int sproto_prototag(struct sproto *, const char * name); -const char * sproto_protoname(struct sproto *, int proto); +int sproto_prototag(const struct sproto *, const char * name); +const char * sproto_protoname(const struct sproto *, int proto); // SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response -struct sproto_type * sproto_protoquery(struct sproto *, int proto, int what); +struct sproto_type * sproto_protoquery(const struct sproto *, int proto, int what); -struct sproto_type * sproto_type(struct sproto *, const char * type_name); +struct sproto_type * sproto_type(const struct sproto *, const char * type_name); int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); @@ -41,8 +41,8 @@ struct sproto_arg { typedef int (*sproto_callback)(const struct sproto_arg *args); -int sproto_decode(struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); -int sproto_encode(struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); +int sproto_decode(const struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); +int sproto_encode(const struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); // for debug use void sproto_dump(struct sproto *); From 9188b5451ccb60f37c7e79332806a725b07ac31d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 9 Apr 2015 11:55:07 +0800 Subject: [PATCH 342/729] bugfix 1 , from http://www.lua.org/bugs.html#5.3.0-1 --- 3rd/lua/lstrlib.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index a650b768..43be88ec 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -798,7 +798,8 @@ static int str_gsub (lua_State *L) { */ /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ -#define MAX_ITEM 512 +#define MAX_ITEM \ + (sizeof(lua_Number) <= 4 ? 150 : sizeof(lua_Number) <= 8 ? 450 : 5050) /* valid flags in a format specification */ #define FLAGS "-+ #0" From 22364bac897c3ec01e0040e07fd0f7ea3ece2227 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 9 Apr 2015 12:14:28 +0800 Subject: [PATCH 343/729] bugfix: memory leak --- lualib/dns.lua | 4 +++- lualib/skynet.lua | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib/dns.lua b/lualib/dns.lua index 4636bf6d..26a7bcd5 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -250,7 +250,9 @@ function dns.server(server, port) assert(server, "Can't get nameserver") end dns_server = socket.udp(function(data, sz, from) - resolve(skynet.tostring(data,sz)) + local str = skynet.tostring(data,sz) + skynet.trash(data,sz) + resolve(str) end) socket.udp_connect(dns_server, server, port or 53) return server diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 17f22bd3..481a2321 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -398,6 +398,7 @@ skynet.pack = assert(c.pack) skynet.packstring = assert(c.packstring) skynet.unpack = assert(c.unpack) skynet.tostring = assert(c.tostring) +skynet.trash = assert(c.trash) local function yield_call(service, session) watching_session[session] = service From 03399e86a9f5886e4227cbbf6a5508263b1fa7a3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 9 Apr 2015 12:49:40 +0800 Subject: [PATCH 344/729] udp api changed, callback recv the string --- lualib/dns.lua | 4 +--- lualib/socket.lua | 5 ++++- test/testudp.lua | 7 +++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lualib/dns.lua b/lualib/dns.lua index 26a7bcd5..a38c00e6 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -249,9 +249,7 @@ function dns.server(server, port) end assert(server, "Can't get nameserver") end - dns_server = socket.udp(function(data, sz, from) - local str = skynet.tostring(data,sz) - skynet.trash(data,sz) + dns_server = socket.udp(function(str, from) resolve(str) end) socket.udp_connect(dns_server, server, port or 53) diff --git a/lualib/socket.lua b/lualib/socket.lua index dd2df1c1..8d98e5a3 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -1,5 +1,6 @@ local driver = require "socketdriver" local skynet = require "skynet" +local skynet_core = require "skynet.core" local assert = assert local socket = {} -- api @@ -127,7 +128,9 @@ socket_message[6] = function(id, size, data, address) driver.drop(data, size) return end - s.callback(data, size, address) + local str = skynet.tostring(data, size) + skynet_core.trash(data, size) + s.callback(str, address) end skynet.register_protocol { diff --git a/test/testudp.lua b/test/testudp.lua index d8387e29..fb8f7109 100644 --- a/test/testudp.lua +++ b/test/testudp.lua @@ -3,16 +3,15 @@ local socket = require "socket" local function server() local host - host = socket.udp(function(data, sz, from) - local str = skynet.tostring(data,sz) -- skynet.tostring should call only once, because it will free the pointer data + host = socket.udp(function(str, from) print("server recv", str, socket.udp_address(from)) socket.sendto(host, from, "OK " .. str) end , "127.0.0.1", 8765) -- bind an address end local function client() - local c = socket.udp(function(data, sz, from) - print("client recv", skynet.tostring(data,sz), socket.udp_address(from)) + local c = socket.udp(function(str, from) + print("client recv", str, socket.udp_address(from)) end) socket.udp_connect(c, "127.0.0.1", 8765) for i=1,20 do From 678a94bf71f04dbeadaf42f62b66a19fb3d822a2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 10 Apr 2015 11:35:47 +0800 Subject: [PATCH 345/729] update license --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index a777a3f0..0650949a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2012-2014 codingnow.com +Copyright (c) 2012-2015 codingnow.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in From 0298997d239fa81991ec94b28b8539d69a23bca6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Apr 2015 13:04:39 +0800 Subject: [PATCH 346/729] release alpha 4 --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 5eb08748..b0945ca3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +v1.0.0-alpha4 (2015-4-13) +----------- +* sproto can share c struct between states +* udp api changed (use lua string now) +* fix memory leak in dns module + v1.0.0-alpha3 (2015-3-30) ----------- * Update sproto (bugfix) From 012d98380de3893a4d18177c64c3efd5abe40636 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Apr 2015 17:14:08 +0800 Subject: [PATCH 347/729] merge lua bugfix from lua offical bugs page --- 3rd/lua/lstate.h | 1 + 3rd/lua/lvm.c | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 81e12c40..cdcdd5a4 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -94,6 +94,7 @@ typedef struct CallInfo { #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ +#define CIST_LEQ (1<<7) /* using __lt for __le */ #define isLua(ci) ((ci)->callstatus & CIST_LUA) diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 8a51550c..cb2e2809 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -303,12 +303,18 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try 'le' */ return res; - else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try 'lt' */ - luaG_ordererror(L, l, r); - return !res; + else { /* try 'lt': */ + L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ + res = luaT_callorderTM(L, r, l, TM_LT); + L->ci->callstatus ^= CIST_LEQ; /* clear mark */ + if (res < 0) + luaG_ordererror(L, l, r); + return !res; /* result is negated */ + } } + /* ** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) @@ -564,11 +570,11 @@ void luaV_finishOp (lua_State *L) { case OP_LE: case OP_LT: case OP_EQ: { int res = !l_isfalse(L->top - 1); L->top--; - /* metamethod should not be called when operand is K */ - lua_assert(!ISK(GETARG_B(inst))); - if (op == OP_LE && /* "<=" using "<" instead? */ - ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE))) - res = !res; /* invert result */ + if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ + lua_assert(op == OP_LE); + ci->callstatus ^= CIST_LEQ; /* clear mark */ + res = !res; /* negate result */ + } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); if (res != GETARG_A(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ From a6bad52181e3bd803f833cc01142bbc2449ff41c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 14 Apr 2015 23:38:49 +0800 Subject: [PATCH 348/729] add sproto new api --- lualib/sproto.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 7a722f55..62dd5048 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -99,6 +99,29 @@ local function queryproto(self, pname) return v end +function sproto:request_encode(protoname, tbl) + local p = queryproto(self, protoname) + return core.encode(p.request,tbl) , p.tag +end + +function sproto:response_encode(protoname, tbl) + local p = queryproto(self, protoname) + return core.encode(p.response,tbl) , p.tag +end + +function sproto:request_decode(protoname, ...) + local p = queryproto(self, protoname) + return core.decode(p.request,...) +end + +function sproto:response_decode(protoname, ...) + local p = queryproto(self, protoname) + return core.decode(p.response,...) +end + +sproto.pack = core.pack +sproto.unpack = core.unpack + local header_tmp = {} local function gen_response(self, response, session) From 843095afc67146b7fe6e2e1735cfae6fe02b22a3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 15 Apr 2015 16:52:55 +0800 Subject: [PATCH 349/729] add snax.printf --- lualib/snax.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lualib/snax.lua b/lualib/snax.lua index 99e539de..a84dbb46 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -162,4 +162,8 @@ function snax.hotfix(obj, source, ...) return test_result(skynet_call(obj.handle, "snax", t.system.hotfix, source, ...)) end +function snax.printf(fmt, ...) + skynet.error(string.format(fmt, ...)) +end + return snax From f55a31c24f0d4604b1ea6f23de17b67a811e7e1a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 15 Apr 2015 22:01:01 +0800 Subject: [PATCH 350/729] sproto.request_decode returns name --- lualib/sproto.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 62dd5048..a9466f9c 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -106,12 +106,12 @@ end function sproto:response_encode(protoname, tbl) local p = queryproto(self, protoname) - return core.encode(p.response,tbl) , p.tag + return core.encode(p.response,tbl) end function sproto:request_decode(protoname, ...) local p = queryproto(self, protoname) - return core.decode(p.request,...) + return core.decode(p.request,...) , p.name end function sproto:response_decode(protoname, ...) From 57c0ad694cf61669e4757bf2de00354b46791194 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 16 Apr 2015 10:35:47 +0800 Subject: [PATCH 351/729] add hmac_hash to simplify hmac64(hashkey(d), key) --- lualib-src/lua-crypt.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index ca4bd430..ef02405a 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -602,11 +602,7 @@ read64(lua_State *L, uint32_t xx[2], uint32_t yy[2]) { } static int -lhmac64(lua_State *L) { - uint32_t x[2], y[2]; - read64(L, x, y); - uint32_t result[2]; - hmac(x,y,result); +pushqword(lua_State *L, uint32_t result[2]) { uint8_t tmp[8]; tmp[0] = result[0] & 0xff; tmp[1] = (result[0] >> 8 )& 0xff; @@ -621,6 +617,40 @@ lhmac64(lua_State *L) { return 1; } +static int +lhmac64(lua_State *L) { + uint32_t x[2], y[2]; + read64(L, x, y); + uint32_t result[2]; + hmac(x,y,result); + return pushqword(L, result); +} + +/* + 8bytes key + string text + */ +static int +lhmac_hash(lua_State *L) { + uint32_t key[2]; + size_t sz = 0; + const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); + if (sz != 8) { + luaL_error(L, "Invalid uint64 key"); + } + key[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; + key[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; + const char * text = luaL_checklstring(L, 2, &sz); + uint8_t h[8]; + Hash(text,(int)sz,h); + uint32_t htext[2]; + htext[0] = h[0] | h[1]<<8 | h[2]<<16 | h[3]<<24; + htext[1] = h[4] | h[5]<<8 | h[6]<<16 | h[7]<<24; + uint32_t result[2]; + hmac(htext,key,result); + return pushqword(L, result); +} + // powmodp64 for DH-key exchange // The biggest 64bit prime @@ -858,6 +888,7 @@ luaopen_crypt(lua_State *L) { { "base64decode", lb64decode }, { "sha1", lsha1 }, { "hmac_sha1", lhmac_sha1 }, + { "hmac_hash", lhmac_hash }, { NULL, NULL }, }; luaL_newlib(L,l); From 7b783076e554a82b4970b09e7f93c17956c26adc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 16 Apr 2015 10:41:23 +0800 Subject: [PATCH 352/729] use hmac_hash --- lualib/snax/msgserver.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua index f6804a60..a70ea415 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -178,7 +178,7 @@ function server.start(conf) end local text = string.format("%s:%s", username, index) - local v = crypt.hmac64(crypt.hashkey(text), u.secret) + local v = crypt.hmac_hash(u.secret, text) -- equivalent to crypt.hmac64(crypt.hashkey(text), u.secret) if v ~= hmac then return "401 Unauthorized" end From 86d586545776270c4e57902c780933e6cdc8c804 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 18 Apr 2015 21:11:57 +0800 Subject: [PATCH 353/729] bugfix: wunlock first, and then release context. (may dead lock) --- skynet-src/skynet_handle.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_handle.c b/skynet-src/skynet_handle.c index dc20dcf2..d904288e 100644 --- a/skynet-src/skynet_handle.c +++ b/skynet-src/skynet_handle.c @@ -77,7 +77,6 @@ skynet_handle_retire(uint32_t handle) { struct skynet_context * ctx = s->slot[hash]; if (ctx != NULL && skynet_context_handle(ctx) == handle) { - skynet_context_release(ctx); s->slot[hash] = NULL; ret = 1; int i; @@ -92,10 +91,17 @@ skynet_handle_retire(uint32_t handle) { ++j; } s->name_count = j; + } else { + ctx = NULL; } rwlock_wunlock(&s->lock); + if (ctx) { + // release ctx may call skynet_handle_* , so wunlock first. + skynet_context_release(ctx); + } + return ret; } From e3b7f86075ad340f30dc45b10dea34695b695407 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 21 Apr 2015 15:35:29 +0800 Subject: [PATCH 354/729] cluster reload should reset old connection to update address:port --- service/clusterd.lua | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index 677f84fa..8d340b70 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -5,14 +5,6 @@ local cluster = require "cluster.core" local config_name = skynet.getenv "cluster" local node_address = {} - -local function loadconfig() - local f = assert(io.open(config_name)) - local source = f:read "*a" - f:close() - assert(load(source, "@"..config_name, "t", node_address))() -end - local node_session = {} local command = {} @@ -37,6 +29,24 @@ end local node_channel = setmetatable({}, { __index = open_channel }) +local function loadconfig() + local f = assert(io.open(config_name)) + local source = f:read "*a" + f:close() + local tmp = {} + assert(load(source, "@"..config_name, "t", tmp))() + for name,address in pairs(tmp) do + assert(type(address) == "string") + if node_address[name] ~= address then + -- address changed + if rawget(node_channel, name) then + node_channel[name] = nil -- reset connection + end + node_address[name] = address + end + end +end + function command.reload() loadconfig() skynet.ret(skynet.pack(nil)) From cd3391461cad04b3303a352b3f01e0e3db3475af Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 22 Apr 2015 16:50:20 +0800 Subject: [PATCH 355/729] add skynet.pcall for hotfix use (call a function with require) --- examples/cluster1.lua | 3 +++ examples/cluster2.lua | 1 + examples/clustername.lua | 1 + lualib/skynet.lua | 12 +++++++++--- lualib/skynet/debug.lua | 2 +- lualib/skynet/inject.lua | 4 ++-- service/sharedatad.lua | 2 +- 7 files changed, 18 insertions(+), 7 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 6bb3b68d..27c00371 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -5,6 +5,9 @@ skynet.start(function() local sdb = skynet.newservice("simpledb") skynet.name(".simpledb", sdb) print(skynet.call(".simpledb", "lua", "SET", "a", "foobar")) + print(skynet.call(".simpledb", "lua", "SET", "b", "foobar2")) print(skynet.call(".simpledb", "lua", "GET", "a")) + print(skynet.call(".simpledb", "lua", "GET", "b")) cluster.open "db" + cluster.open "db2" end) diff --git a/examples/cluster2.lua b/examples/cluster2.lua index a49aa718..7b315e1a 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -5,4 +5,5 @@ skynet.start(function() local proxy = cluster.proxy("db", ".simpledb") print(skynet.call(proxy, "lua", "GET", "a")) print(cluster.call("db", ".simpledb", "GET", "a")) + print(cluster.call("db2", ".simpledb", "GET", "b")) end) diff --git a/examples/clustername.lua b/examples/clustername.lua index c5ed5e69..681245a1 100644 --- a/examples/clustername.lua +++ b/examples/clustername.lua @@ -1 +1,2 @@ db = "127.0.0.1:2528" +db2 = "127.0.0.1:2529" diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 481a2321..d3cb623d 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -620,8 +620,10 @@ end local function init_all() local funcs = init_func init_func = nil - for k,v in pairs(funcs) do - v() + if funcs then + for k,v in pairs(funcs) do + v() + end end end @@ -632,8 +634,12 @@ local function init_template(start) init_all() end +function skynet.pcall(start) + return xpcall(init_template, debug.traceback, start) +end + local function init_service(start) - local ok, err = xpcall(init_template, debug.traceback, start) + local ok, err = skynet.pcall(start) if not ok then skynet.error("init service failed: " .. tostring(err)) skynet.send(".launcher","lua", "ERROR") diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 0ef6dcb8..0bcb8edb 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -49,7 +49,7 @@ end function dbgcmd.RUN(source, filename) local inject = require "skynet.inject" - local output = inject(source, filename , export.dispatch, skynet.register_protocol) + local output = inject(skynet, source, filename , export.dispatch, skynet.register_protocol) collectgarbage "collect" skynet.ret(skynet.pack(table.concat(output, "\n"))) end diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua index 68e3954b..3c945ffe 100644 --- a/lualib/skynet/inject.lua +++ b/lualib/skynet/inject.lua @@ -18,7 +18,7 @@ local function getupvaluetable(u, func, unique) end end -return function(source, filename , ...) +return function(skynet, source, filename , ...) if filename then filename = "@" .. filename else @@ -56,7 +56,7 @@ return function(source, filename , ...) if not func then return { err } end - local ok, err = xpcall(func, debug.traceback) + local ok, err = skynet.pcall(func) if not ok then table.insert(output, err) end diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 191bab5d..a7e8d427 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -44,7 +44,7 @@ function CMD.new(name, t) elseif dt == "string" then value = setmetatable({}, env_mt) local f = load(t, "=" .. name, "t", value) - f() + assert(skynet.pcall(f)) setmetatable(value, nil) elseif dt == "nil" then value = {} From 98af25d109103374731bd568e32b520ba4b18066 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 24 Apr 2015 14:31:18 +0800 Subject: [PATCH 356/729] better error log, see pr #260 --- lualib/snax/loginserver.lua | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 2605a028..cfddd85e 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -33,17 +33,17 @@ Success: ]] local socket_error = {} -local function assert_socket(v, fd) +local function assert_socket(service, v, fd) if v then return v else - skynet.error(string.format("auth failed: socket (fd = %d) closed", fd)) + skynet.error(string.format("%s failed: socket (fd = %d) closed", service, fd)) error(socket_error) end end -local function write(fd, text) - assert_socket(socket.write(fd, text), fd) +local function write(service, fd, text) + assert_socket(service, socket.write(fd, text), fd) end local function launch_slave(auth_handler) @@ -57,27 +57,27 @@ local function launch_slave(auth_handler) socket.limit(fd, 8192) local challenge = crypt.randomkey() - write(fd, crypt.base64encode(challenge).."\n") + write("auth", fd, crypt.base64encode(challenge).."\n") - local handshake = assert_socket(socket.readline(fd), fd) + local handshake = assert_socket("auth", socket.readline(fd), fd) local clientkey = crypt.base64decode(handshake) if #clientkey ~= 8 then error "Invalid client key" end local serverkey = crypt.randomkey() - write(fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n") + write("auth", fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n") local secret = crypt.dhsecret(clientkey, serverkey) - local response = assert_socket(socket.readline(fd), fd) + local response = assert_socket("auth", socket.readline(fd), fd) local hmac = crypt.hmac64(challenge, secret) if hmac ~= crypt.base64decode(response) then - write(fd, "400 Bad Request\n") + write("auth", fd, "400 Bad Request\n") error "challenge failed" end - local etoken = assert_socket(socket.readline(fd),fd) + local etoken = assert_socket("auth", socket.readline(fd),fd) local token = crypt.desdecode(secret, crypt.base64decode(etoken)) @@ -91,7 +91,11 @@ local function launch_slave(auth_handler) if ok then skynet.ret(skynet.pack(err, ...)) else - error(err) + if err == socket_error then + skynet.ret(skynet.pack(nil, "socket error")) + else + skynet.ret(skynet.pack(false, err)) + end end end @@ -108,13 +112,15 @@ local function accept(conf, s, fd, addr) socket.start(fd) if not ok then - write(fd, "401 Unauthorized\n") + if ok ~= nil then + write("response 401", fd, "401 Unauthorized\n") + end error(server) end if not conf.multilogin then if user_login[uid] then - write(fd, "406 Not Acceptable\n") + write("response 406", fd, "406 Not Acceptable\n") error(string.format("User %s is already login", uid)) end @@ -127,9 +133,9 @@ local function accept(conf, s, fd, addr) if ok then err = err or "" - write(fd, "200 "..crypt.base64encode(err).."\n") + write("response 200",fd, "200 "..crypt.base64encode(err).."\n") else - write(fd, "403 Forbidden\n") + write("response 403",fd, "403 Forbidden\n") error(err) end end From 35a5b99bed88911cd62a369bb4c6b9d09949c6ab Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 27 Apr 2015 10:45:31 +0800 Subject: [PATCH 357/729] release alpha5 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index b0945ca3..2f6a04e7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v1.0.0-alpha5 (2015-4-27) +----------- +* merge lua 5.3 offical bugfix +* improve sproto rpc api +* fix a deadlock bug when service retire +* improve cluster config reload +* add skynet.pcall for calling a function with `require` +* better error log in loginserver + v1.0.0-alpha4 (2015-4-13) ----------- * sproto can share c struct between states From 8682f7f82f41304ee9d8b194157bfdd5efda7f43 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 27 Apr 2015 16:23:12 +0800 Subject: [PATCH 358/729] response error when return/response package is too large --- lualib/skynet.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d3cb623d..322c1da5 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -175,6 +175,10 @@ function suspend(co, result, command, param, size) local ret if not dead_service[co_address] then ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, param, size) ~= nil + if not ret then + -- If the package is too large, returns nil. so we should report error back + c.send(co_address, skynet.PTYPE_ERROR, co_session, "") + end elseif size == nil then c.trash(param, size) ret = false @@ -209,6 +213,10 @@ function suspend(co, result, command, param, size) if not dead_service[co_address] then if ok then ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, f(...)) ~= nil + if not ret then + -- If the package is too large, returns false. so we should report error back + c.send(co_address, skynet.PTYPE_ERROR, co_session, "") + end else ret = c.send(co_address, skynet.PTYPE_ERROR, co_session, "") ~= nil end @@ -405,7 +413,7 @@ local function yield_call(service, session) local succ, msg, sz = coroutine_yield("CALL", session) watching_session[session] = nil if not succ then - error(debug.traceback()) + error "call failed" end return msg,sz end From 75d9b07158510f4d1315edefe860bcdf7b0fd2f7 Mon Sep 17 00:00:00 2001 From: HuaYang Huang Date: Wed, 29 Apr 2015 20:18:13 +0800 Subject: [PATCH 359/729] fix: httpc.get MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit :-( 此错误会导致httpc.get无法找到正确的虚拟主机 --- lualib/http/httpc.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index a8b7d6f8..ae771dc8 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -11,23 +11,21 @@ local function request(fd, method, host, url, recvheader, header, content) local write = socket.writefunc(fd) local header_content = "" if header then + if not header.host then + header.host = host + end for k,v in pairs(header) do header_content = string.format("%s%s:%s\r\n", header_content, k, v) end - if header.host then - host = "" - else - host = string.format("host:%s\r\n", host) - end else - host = string.format("host:%s\r\n",host) + header_content = string.format("host:%s\r\n",host) end if content then - local data = string.format("%s %s HTTP/1.1\r\n%scontent-length:%d\r\n%s\r\n%s", method, url, host, #content, header_content, content) + local data = string.format("%s %s HTTP/1.1\r\n%scontent-length:%d\r\n\r\n%s", method, url, header_content, #content, content) write(data) else - local request_header = string.format("%s %s HTTP/1.1\r\nhost:%s\r\ncontent-length:0\r\n%s\r\n", method, url, host, header_content) + local request_header = string.format("%s %s HTTP/1.1\r\n%scontent-length:0\r\n\r\n", method, url, header_content) write(request_header) end From f61e3f46e8e512a42d158f933f17a65ae9643cd7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 2 May 2015 20:47:15 +0800 Subject: [PATCH 360/729] fix issue #265 --- lualib-src/lua-seri.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 9b2af465..18b90d0d 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -256,6 +256,7 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a static void wb_table(lua_State *L, struct write_block *wb, int index, int depth) { + luaL_checkstack(L, LUA_MINSTACK, NULL); if (index < 0) { index = lua_gettop(L) + index + 1; } @@ -416,6 +417,7 @@ unpack_table(lua_State *L, struct read_block *rb, int array_size) { } array_size = get_integer(L,rb,cookie); } + luaL_checkstack(L,LUA_MINSTACK,NULL); lua_createtable(L,array_size,0); int i; for (i=1;i<=array_size;i++) { @@ -549,8 +551,8 @@ _luaseri_unpack(lua_State *L) { int i; for (i=0;;i++) { - if (i%16==15) { - lua_checkstack(L,i); + if (i%8==7) { + luaL_checkstack(L,LUA_MINSTACK,NULL); } uint8_t type = 0; uint8_t *t = rb_read(&rb, sizeof(type)); From fa17081012e47763ed6970842624bc80cbdb11cf Mon Sep 17 00:00:00 2001 From: xiyanxiyan10 Date: Wed, 6 May 2015 23:13:06 +0800 Subject: [PATCH 361/729] =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E6=9B=B4=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skynet-src/socket_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 5622e4a8..914bc32b 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -508,7 +508,7 @@ udp_socket_address(struct socket *s, const uint8_t udp_address[UDP_ADDRESS_SIZE] memset(&sa->v6, 0, sizeof(sa->v6)); sa->s.sa_family = AF_INET6; sa->v6.sin6_port = port; - memcpy(&sa->v6.sin6_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v6.sin6_addr)); // ipv4 address is 128 bits + memcpy(&sa->v6.sin6_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v6.sin6_addr)); // ipv6 address is 128 bits return sizeof(sa->v6); } return 0; From f987ff8199f06b98924d1db323e9771e555b1400 Mon Sep 17 00:00:00 2001 From: gaopan Date: Thu, 7 May 2015 15:25:47 +0800 Subject: [PATCH 362/729] fix:udp and udp_send --- skynet-src/socket_server.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 914bc32b..eba07624 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -720,6 +720,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock append_sendbuffer_udp(ss,s,priority,request,udp_address); } else { so.free_func(request->buffer); + return -1; } } sp_write(ss->event_fd, s->fd, s, true); @@ -887,6 +888,7 @@ add_udp_socket(struct socket_server *ss, struct request_udp *udp) { if (ns == NULL) { close(udp->fd); ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; + return; } ns->type = SOCKET_TYPE_CONNECTED; memset(ns->p.udp_address, 0, sizeof(ns->p.udp_address)); From 0eb4754b312ee781af473c064f97cfa87affb784 Mon Sep 17 00:00:00 2001 From: gaopan Date: Thu, 7 May 2015 16:52:48 +0800 Subject: [PATCH 363/729] @fix udp_address, in big-endian machine maybe error --- lualib-src/lua-socket.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 8ee60eb0..f28d8665 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -599,7 +599,9 @@ static int ludp_address(lua_State *L) { size_t sz = 0; const uint8_t * addr = (const uint8_t *)luaL_checklstring(L, 1, &sz); - int port = addr[1] * 256 + addr[2]; + uint16_t port = 0; + memcpy(&port, addr+1, sizeof(uint16_t)); + port = ntohs(port); const void * src = addr+3; char tmp[256]; int family; From 9452169f0a424ce3ca3aae8b57bccb78980b861c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 May 2015 18:17:46 +0800 Subject: [PATCH 364/729] remove unused comment --- lualib-src/lua-socket.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index f28d8665..e12a9c2f 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -534,12 +534,6 @@ lnodelay(lua_State *L) { skynet_socket_nodelay(ctx,id); return 0; } -/* -int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port); -int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port); -int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz); -const char * skynet_socket_udp_address(struct skynet_context *ctx, struct skynet_socket_message *, int *addrsz); -*/ static int ludp(lua_State *L) { From c7f5145e9e4f15e83d282ab12c8d9b9a22959d64 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 May 2015 18:20:17 +0800 Subject: [PATCH 365/729] update sproto , add new api sproto:default --- lualib-src/sproto/lsproto.c | 50 +++++++++++++++++++++++++++++++++++++ lualib/sproto.lua | 15 +++++++++++ 2 files changed, 65 insertions(+) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c33118d4..99448487 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -573,6 +573,55 @@ lloadproto(lua_State *L) { return 1; } +static int +encode_default(const struct sproto_arg *args) { + lua_State *L = args->ud; + lua_pushstring(L, args->tagname); + if (args->index > 0) { + lua_newtable(L); + } else { + switch(args->type) { + case SPROTO_TINTEGER: + lua_pushinteger(L, 0); + break; + case SPROTO_TBOOLEAN: + lua_pushboolean(L, 0); + break; + case SPROTO_TSTRING: + lua_pushliteral(L, ""); + break; + case SPROTO_TSTRUCT: + lua_createtable(L, 0, 1); + lua_pushstring(L, sproto_name(args->subtype)); + lua_setfield(L, -2, "__type"); + break; + } + } + lua_rawset(L, -3); + return 0; +} + +/* + lightuserdata sproto_type + return default table + */ +static int +ldefault(lua_State *L) { + int ret; + // 32 is enough for dummy buffer, because ldefault encode nothing but the header. + char dummy[32]; + struct sproto_type * st = lua_touserdata(L, 1); + if (st == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + lua_newtable(L); + ret = sproto_encode(st, dummy, sizeof(dummy), encode_default, L); + if (ret<0) { + return luaL_error(L, "dummy buffer (%d) is too small", (int)sizeof(dummy)); + } + return 1; +} + int luaopen_sproto_core(lua_State *L) { #ifdef luaL_checkversion @@ -587,6 +636,7 @@ luaopen_sproto_core(lua_State *L) { { "protocol", lprotocol }, { "loadproto", lloadproto }, { "saveproto", lsaveproto }, + { "default", ldefault }, { NULL, NULL }, }; luaL_newlib(L,l); diff --git a/lualib/sproto.lua b/lualib/sproto.lua index a9466f9c..271e692d 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -122,6 +122,21 @@ end sproto.pack = core.pack sproto.unpack = core.unpack +function sproto:default(typename, type) + if type == nil then + return core.default(querytype(self, typename)) + else + local p = queryproto(self, typename) + if type == "REQUEST" then + return core.default(p.request) + elseif type == "RESPONSE" then + return core.default(p.response) + else + error "Invalid type" + end + end +end + local header_tmp = {} local function gen_response(self, response, session) From 35c8c6379358a94f7e3c02a0705da1f2a6fe2860 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 May 2015 21:39:56 +0800 Subject: [PATCH 366/729] update sproto, bugfix sproto_dump --- lualib-src/sproto/sproto.c | 6 +++++- lualib/sprotoparser.lua | 5 +---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index c37fdc1b..11756796 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -500,7 +500,11 @@ sproto_dump(struct sproto *s) { printf("=== %d protocol ===\n", s->protocol_n); for (i=0;iprotocol_n;i++) { struct protocol *p = &s->proto[i]; - printf("\t%s (%d) request:%s", p->name, p->tag, p->p[SPROTO_REQUEST]->name); + if (p->p[SPROTO_REQUEST]) { + printf("\t%s (%d) request:%s", p->name, p->tag, p->p[SPROTO_REQUEST]->name); + } else { + printf("\t%s (%d) request:(null)", p->name, p->tag); + } if (p->p[SPROTO_RESPONSE]) { printf(" response:%s", p->p[SPROTO_RESPONSE]->name); } diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index e7da7fa0..7b0b04e2 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -194,7 +194,7 @@ local function check_protocol(r) local request = v.request local response = v.response local p = map[tag] - + if p then error(string.format("redefined protocol tag %d at %s", tag, name)) end @@ -340,9 +340,6 @@ local function packtype(name, t, alltypes) end local function packproto(name, p, alltypes) --- if p.request == nil then --- error(string.format("Protocol %s need request", name)) --- end if p.request then local request = alltypes[p.request] if request == nil then From 0587400e2d99faa57406f5c2fdcb48ee021fa130 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 12 May 2015 10:20:16 +0800 Subject: [PATCH 367/729] update sproto: handle empty protocol --- lualib/sproto.lua | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 271e692d..911dc877 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -101,22 +101,40 @@ end function sproto:request_encode(protoname, tbl) local p = queryproto(self, protoname) - return core.encode(p.request,tbl) , p.tag + local request = p.request + if request then + return core.encode(request,tbl) , p.tag + else + return "" , p.tag + end end function sproto:response_encode(protoname, tbl) local p = queryproto(self, protoname) - return core.encode(p.response,tbl) + local response = p.response + if response then + return core.encode(response,tbl) + else + return "" + end end function sproto:request_decode(protoname, ...) local p = queryproto(self, protoname) - return core.decode(p.request,...) , p.name + local request = p.request + if request then + return core.decode(request,...) , p.name + else + return nil, p.name + end end function sproto:response_decode(protoname, ...) local p = queryproto(self, protoname) - return core.decode(p.response,...) + local response = p.response + if response then + return core.decode(response,...) + end end sproto.pack = core.pack @@ -128,9 +146,13 @@ function sproto:default(typename, type) else local p = queryproto(self, typename) if type == "REQUEST" then - return core.default(p.request) + if p.request then + return core.default(p.request) + end elseif type == "RESPONSE" then - return core.default(p.response) + if p.response then + return core.default(p.response) + end else error "Invalid type" end From d92adda2c91c2a6d18f3e9aafb1de7e3bf13a4f3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 12 May 2015 22:53:03 +0800 Subject: [PATCH 368/729] If skynet.wakeup a skynet.call, raise an error --- lualib/skynet.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 322c1da5..fa1087d2 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -128,7 +128,7 @@ local function dispatch_wakeup() local session = sleep_session[co] if session then session_id_coroutine[session] = "BREAK" - return suspend(co, coroutine.resume(co, true, "BREAK")) + return suspend(co, coroutine.resume(co, false, "BREAK")) end end end @@ -265,9 +265,13 @@ function skynet.sleep(ti) session = tonumber(session) local succ, ret = coroutine_yield("SLEEP", session) sleep_session[coroutine.running()] = nil - assert(succ, ret) + if succ then + return + end if ret == "BREAK" then return "BREAK" + else + error(ret) end end @@ -277,7 +281,7 @@ end function skynet.wait() local session = c.genid() - coroutine_yield("SLEEP", session) + local ret, msg = coroutine_yield("SLEEP", session) local co = coroutine.running() sleep_session[co] = nil session_id_coroutine[session] = nil From 82fa2f979c7852f6a583e7b970c0486474fb9107 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 12 May 2015 23:15:03 +0800 Subject: [PATCH 369/729] skynet.exit will call every unresponse handle --- lualib/skynet.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index fa1087d2..64aaa603 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -44,6 +44,7 @@ local session_id_coroutine = {} local session_coroutine_id = {} local session_coroutine_address = {} local session_response = {} +local unresponse = {} local wakeup_session = {} local sleep_session = {} @@ -224,11 +225,13 @@ function suspend(co, result, command, param, size) ret = false end release_watching(co_address) + unresponse[response] = nil f = nil return ret end watching_service[co_address] = watching_service[co_address] + 1 session_response[co] = response + unresponse[response] = true return suspend(co, coroutine.resume(co, response)) elseif command == "EXIT" then -- coroutine exit @@ -361,6 +364,9 @@ function skynet.exit() c.redirect(address, 0, skynet.PTYPE_ERROR, session, "") end end + for resp in pairs(unresponse) do + resp(false) + end -- report the sources I call but haven't return local tmp = {} for session, address in pairs(watching_session) do From 9e27f5903345a56a26c79889b6a31e10a26b1af9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 May 2015 09:50:53 +0800 Subject: [PATCH 370/729] remove task overload warning --- lualib/skynet.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 64aaa603..39b867c5 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -97,7 +97,6 @@ end local coroutine_pool = {} local coroutine_yield = coroutine.yield -local coroutine_count = 0 local function co_create(f) local co = table.remove(coroutine_pool) @@ -111,11 +110,6 @@ local function co_create(f) f(coroutine_yield()) end end) - coroutine_count = coroutine_count + 1 - if coroutine_count > 1024 then - skynet.error("May overload, create 1024 task") - coroutine_count = 0 - end else coroutine.resume(co, f) end From 3baeb62b0b1993ad7352d152571dd95d611c87a4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 May 2015 11:04:25 +0800 Subject: [PATCH 371/729] move some api from skynet.lua to skynet/manager.lua --- {service => examples}/abort.lua | 1 + examples/cluster1.lua | 1 + examples/globallog.lua | 1 + examples/main_log.lua | 1 + examples/simpledb.lua | 1 + lualib/skynet.lua | 94 ++------------------------------- lualib/skynet/manager.lua | 90 +++++++++++++++++++++++++++++++ service/bootstrap.lua | 1 + service/cdummy.lua | 1 + service/clusterproxy.lua | 1 + service/cslave.lua | 1 + service/launcher.lua | 1 + service/service_mgr.lua | 1 + 13 files changed, 106 insertions(+), 89 deletions(-) rename {service => examples}/abort.lua (50%) create mode 100644 lualib/skynet/manager.lua diff --git a/service/abort.lua b/examples/abort.lua similarity index 50% rename from service/abort.lua rename to examples/abort.lua index 755675a6..305cfdb5 100644 --- a/service/abort.lua +++ b/examples/abort.lua @@ -1,3 +1,4 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.abort skynet.abort() diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 27c00371..6623aba3 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local cluster = require "cluster" +require "skynet.manager" -- import skynet.name skynet.start(function() local sdb = skynet.newservice("simpledb") diff --git a/examples/globallog.lua b/examples/globallog.lua index 2939756c..e45d33dc 100644 --- a/examples/globallog.lua +++ b/examples/globallog.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register skynet.start(function() skynet.dispatch("lua", function(session, address, ...) diff --git a/examples/main_log.lua b/examples/main_log.lua index 2a3fbd30..4dc700c4 100644 --- a/examples/main_log.lua +++ b/examples/main_log.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" +require "skynet.manager" -- import skynet.monitor local function monitor_master() harbor.linkmaster() diff --git a/examples/simpledb.lua b/examples/simpledb.lua index 3a7649e6..d073dfe6 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register local db = {} local command = {} diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 39b867c5..4e099ba6 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -284,35 +284,6 @@ function skynet.wait() session_id_coroutine[session] = nil end -local function globalname(name, handle) - local c = string.sub(name,1,1) - assert(c ~= ':') - if c == '.' then - return false - end - - assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h - assert(tonumber(name) == nil) -- global name can't be number - - local harbor = require "skynet.harbor" - - harbor.globalname(name, handle) - - return true -end - -function skynet.register(name) - if not globalname(name) then - c.command("REG", name) - end -end - -function skynet.name(name, handle) - if not globalname(name, handle) then - c.command("NAME", name .. " " .. skynet.address(handle)) - end -end - local self_handle function skynet.self() if self_handle then @@ -329,13 +300,6 @@ function skynet.localname(name) end end -function skynet.launch(...) - local addr = c.command("LAUNCH", table.concat({...}," ")) - if addr then - return string_to_handle(addr) - end -end - function skynet.now() return tonumber(c.command("NOW")) end @@ -374,14 +338,6 @@ function skynet.exit() coroutine_yield "QUIT" end -function skynet.kill(name) - if type(name) == "number" then - skynet.send(".launcher","lua","REMOVE",name, true) - name = skynet.address(name) - end - c.command("KILL",name) -end - function skynet.getenv(key) local ret = c.command("GETENV",key) if ret == "" then @@ -528,7 +484,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) end end -local function dispatch_message(...) +function skynet.dispatch_message(...) local succ, err = pcall(raw_dispatch_message,...) while true do local key,co = next(fork_queue) @@ -650,7 +606,7 @@ function skynet.pcall(start) return xpcall(init_template, debug.traceback, start) end -local function init_service(start) +function skynet.init_service(start) local ok, err = skynet.pcall(start) if not ok then skynet.error("init service failed: " .. tostring(err)) @@ -662,33 +618,9 @@ local function init_service(start) end function skynet.start(start_func) - c.callback(dispatch_message) + c.callback(skynet.dispatch_message) skynet.timeout(0, function() - init_service(start_func) - end) -end - -function skynet.filter(f ,start_func) - c.callback(function(...) - dispatch_message(f(...)) - end) - skynet.timeout(0, function() - init_service(start_func) - end) -end - -function skynet.forward_type(map, start_func) - c.callback(function(ptype, msg, sz, ...) - local prototype = map[ptype] - if prototype then - dispatch_message(prototype, msg, sz, ...) - else - dispatch_message(ptype, msg, sz, ...) - c.trash(msg, sz) - end - end, true) - skynet.timeout(0, function() - init_service(start_func) + skynet.init_service(start_func) end) end @@ -696,22 +628,6 @@ function skynet.endless() return c.command("ENDLESS")~=nil end -function skynet.abort() - c.command("ABORT") -end - -function skynet.monitor(service, query) - local monitor - if query then - monitor = skynet.queryservice(true, service) - else - monitor = skynet.uniqueservice(true, service) - end - assert(monitor, "Monitor launch failed") - c.command("MONITOR", string.format(":%08x", monitor)) - return monitor -end - function skynet.mqlen() return tonumber(c.command "MQLEN") end @@ -738,7 +654,7 @@ end -- Inject internal debug framework local debug = require "skynet.debug" debug(skynet, { - dispatch = dispatch_message, + dispatch = skynet.dispatch_message, clear = clear_pool, suspend = suspend, }) diff --git a/lualib/skynet/manager.lua b/lualib/skynet/manager.lua new file mode 100644 index 00000000..4365d2e2 --- /dev/null +++ b/lualib/skynet/manager.lua @@ -0,0 +1,90 @@ +local skynet = require "skynet" +local c = require "skynet.core" + +function skynet.launch(...) + local addr = c.command("LAUNCH", table.concat({...}," ")) + if addr then + return tonumber("0x" .. string.sub(addr , 2)) + end +end + +function skynet.kill(name) + if type(name) == "number" then + skynet.send(".launcher","lua","REMOVE",name, true) + name = skynet.address(name) + end + c.command("KILL",name) +end + +function skynet.abort() + c.command("ABORT") +end + +local function globalname(name, handle) + local c = string.sub(name,1,1) + assert(c ~= ':') + if c == '.' then + return false + end + + assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h + assert(tonumber(name) == nil) -- global name can't be number + + local harbor = require "skynet.harbor" + + harbor.globalname(name, handle) + + return true +end + +function skynet.register(name) + if not globalname(name) then + c.command("REG", name) + end +end + +function skynet.name(name, handle) + if not globalname(name, handle) then + c.command("NAME", name .. " " .. skynet.address(handle)) + end +end + +local dispatch_message = skynet.dispatch_message + +function skynet.forward_type(map, start_func) + c.callback(function(ptype, msg, sz, ...) + local prototype = map[ptype] + if prototype then + dispatch_message(prototype, msg, sz, ...) + else + dispatch_message(ptype, msg, sz, ...) + c.trash(msg, sz) + end + end, true) + skynet.timeout(0, function() + skynet.init_service(start_func) + end) +end + +function skynet.filter(f ,start_func) + c.callback(function(...) + dispatch_message(f(...)) + end) + skynet.timeout(0, function() + skynet.init_service(start_func) + end) +end + +function skynet.monitor(service, query) + local monitor + if query then + monitor = skynet.queryservice(true, service) + else + monitor = skynet.uniqueservice(true, service) + end + assert(monitor, "Monitor launch failed") + c.command("MONITOR", string.format(":%08x", monitor)) + return monitor +end + +return skynet diff --git a/service/bootstrap.lua b/service/bootstrap.lua index e1df7170..269654c3 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" +require "skynet.manager" -- import skynet.launch, ... skynet.start(function() local standalone = skynet.getenv "standalone" diff --git a/service/cdummy.lua b/service/cdummy.lua index 52b70d7c..62865222 100644 --- a/service/cdummy.lua +++ b/service/cdummy.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.launch, ... local globalname = {} local queryname = {} diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index ad81bc3d..abdff0cc 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local cluster = require "cluster" +require "skynet.manager" -- inject skynet.forward_type local node, address = ... diff --git a/service/cslave.lua b/service/cslave.lua index d1717bb8..63d98eea 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local socket = require "socket" +require "skynet.manager" -- import skynet.launch, ... local table = table local slaves = {} diff --git a/service/launcher.lua b/service/launcher.lua index 634eeb1e..82e7d4be 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local core = require "skynet.core" +require "skynet.manager" -- import manager apis local string = string local services = {} diff --git a/service/service_mgr.lua b/service/service_mgr.lua index 4d9e4119..d94b0a3d 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register local snax = require "snax" local cmd = {} From 5cc2ef3ac8d872445d03659d4299569dc005f6d3 Mon Sep 17 00:00:00 2001 From: antsmallant <412288482@qq.com> Date: Fri, 15 May 2015 15:59:47 +0800 Subject: [PATCH 372/729] Update inject.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit å…å¾—å˜é‡i污染全局空间 --- lualib/skynet/inject.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua index 3c945ffe..f3ef65d2 100644 --- a/lualib/skynet/inject.lua +++ b/lualib/skynet/inject.lua @@ -1,5 +1,5 @@ local function getupvaluetable(u, func, unique) - i = 1 + local i = 1 while true do local name, value = debug.getupvalue(func, i) if name == nil then From 6a9080157ff8cbf1bf4e5a8c6a77a9de8fc14cd8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 15 May 2015 16:24:01 +0800 Subject: [PATCH 373/729] need require skynet.manager --- lualib/snax/loginserver.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index cfddd85e..01b2d49b 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" local socket = require "socket" local crypt = require "crypt" local table = table From 6c43e907087312399f91196652bde823ad602ae5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 May 2015 12:02:08 +0800 Subject: [PATCH 374/729] release alpha6 --- HISTORY.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 2f6a04e7..cb7c2e3a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,16 @@ +v1.0.0-alpha6 (2015-5-18) +----------- +* bugfix: httpc.get +* bugfix: seri lib stack overflow +* bugfix: udp send +* bugfix: udp address +* bugfix: sproto dump +* add: sproto default +* improve: skynet.wakeup (can wakeup skynet.call by raise an error) +* improve: skynet.exit (raise error when uncall response) +* remove: task overload warning +* move: some skynet api move into skynet.manager + v1.0.0-alpha5 (2015-4-27) ----------- * merge lua 5.3 offical bugfix From de57064b962e2396e81e24c85fdd6b662ebb39a5 Mon Sep 17 00:00:00 2001 From: HuaYang Huang Date: Mon, 18 May 2015 23:05:24 +0800 Subject: [PATCH 375/729] Update lua-crypt.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 static é¿å…å¯èƒ½å¯¼è‡´çš„é“¾æŽ¥å†²çª --- lualib-src/lua-crypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index ef02405a..0284c48b 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -10,7 +10,7 @@ /* the eight DES S-boxes */ -uint32_t SB1[64] = { +static uint32_t SB1[64] = { 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, From 65fad89794ed128ad2f06467eb54d22e9f5e0be7 Mon Sep 17 00:00:00 2001 From: ximenpo Date: Wed, 20 May 2015 15:14:28 +0800 Subject: [PATCH 376/729] =?UTF-8?q?console=E6=9C=8D=E5=8A=A1=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=90=AF=E5=8A=A8snax=E6=9C=8D=E5=8A=A1=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 输入: svc -> 用snluaå¯åЍsvcæœåŠ¡ snax svc -> 用snaxå¯åЍsvcæœåŠ¡ --- service/console.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/service/console.lua b/service/console.lua index c0eba879..90fe62b0 100644 --- a/service/console.lua +++ b/service/console.lua @@ -1,13 +1,26 @@ local skynet = require "skynet" +local snax = require "snax" local socket = require "socket" +local function split_cmdline(cmdline) + local split = {} + for i in string.gmatch(cmdline, "%S+") do + table.insert(split,i) + end + return split +end + local function console_main_loop() local stdin = socket.stdin() socket.lock(stdin) while true do local cmdline = socket.readline(stdin, "\n") - if cmdline ~= "" then - pcall(skynet.newservice,cmdline) + local split = split_cmdline(cmdline) + local command = split[1] + if command == "snax" then + pcall(snax.newservice, select(2, table.unpack(split))) + elseif cmdline ~= "" then + pcall(skynet.newservice, cmdline) end end socket.unlock(stdin) From 7b471ae27dfbbce89c504e36fa664b237d5e8b75 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 20 May 2015 16:13:11 +0800 Subject: [PATCH 377/729] snax service support cluster --- examples/cluster1.lua | 3 +++ examples/cluster2.lua | 4 ++++ examples/config.c1 | 3 ++- examples/config.c2 | 3 ++- lualib/cluster.lua | 9 +++++++++ lualib/snax/interface.lua | 2 +- service/clusterproxy.lua | 1 + service/snaxd.lua | 10 ++++++++-- test/pingserver.lua | 1 + 9 files changed, 31 insertions(+), 5 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 6623aba3..9ba8fcbf 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" local cluster = require "cluster" require "skynet.manager" -- import skynet.name +local snax = require "snax" skynet.start(function() local sdb = skynet.newservice("simpledb") @@ -11,4 +12,6 @@ skynet.start(function() print(skynet.call(".simpledb", "lua", "GET", "b")) cluster.open "db" cluster.open "db2" + -- unique snax service + snax.uniqueservice "pingserver" end) diff --git a/examples/cluster2.lua b/examples/cluster2.lua index 7b315e1a..09e53076 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -6,4 +6,8 @@ skynet.start(function() print(skynet.call(proxy, "lua", "GET", "a")) print(cluster.call("db", ".simpledb", "GET", "a")) print(cluster.call("db2", ".simpledb", "GET", "b")) + + -- test snax service + local pingserver = cluster.snax("db", "pingserver") + print(pingserver.req.ping "hello") end) diff --git a/examples/config.c1 b/examples/config.c1 index d1fdf165..436859d4 100644 --- a/examples/config.c1 +++ b/examples/config.c1 @@ -6,4 +6,5 @@ bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" -cluster = "./examples/clustername.lua" \ No newline at end of file +cluster = "./examples/clustername.lua" +snax = "./test/?.lua" diff --git a/examples/config.c2 b/examples/config.c2 index 2e8cbb0b..23886105 100644 --- a/examples/config.c2 +++ b/examples/config.c2 @@ -6,4 +6,5 @@ bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" -cluster = "./examples/clustername.lua" \ No newline at end of file +cluster = "./examples/clustername.lua" +snax = "./test/?.lua" diff --git a/lualib/cluster.lua b/lualib/cluster.lua index ddb00d13..e4407f06 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -24,6 +24,15 @@ function cluster.proxy(node, name) return skynet.call(clusterd, "lua", "proxy", node, name) end +function cluster.snax(node, name, address) + local snax = require "snax" + if not address then + address = cluster.call(node, ".service", "QUERY", "snaxd" , name) + end + local handle = skynet.call(clusterd, "lua", "proxy", node, address) + return snax.bind(handle, name) +end + skynet.init(function() clusterd = skynet.uniqueservice("clusterd") end) diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index a2f8aa64..bd996c4d 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -64,7 +64,7 @@ return function (name , G, loader) local pattern do - local path = skynet.getenv "snax" + local path = assert(skynet.getenv "snax" , "please set snax in config file") local errlist = {} diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index abdff0cc..e003da2b 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -11,6 +11,7 @@ skynet.register_protocol { } local forward_map = { + [skynet.PTYPE_SNAX] = skynet.PTYPE_SYSTEM, [skynet.PTYPE_LUA] = skynet.PTYPE_SYSTEM, [skynet.PTYPE_RESPONSE] = skynet.PTYPE_RESPONSE, -- don't free response message } diff --git a/service/snaxd.lua b/service/snaxd.lua index 02e3738c..d27008be 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -54,7 +54,7 @@ end skynet.start(function() local init = false - skynet.dispatch("snax", function ( session , source , id, ...) + local function dispatcher( session , source , id, ...) local method = func[id] if method[2] == "system" then @@ -84,5 +84,11 @@ skynet.start(function() assert(init, "Init first") timing(method, ...) end - end) + end + skynet.dispatch("snax", dispatcher) + + -- set lua dispatcher + function snax.enablecluster() + skynet.dispatch("lua", dispatcher) + end end) diff --git a/test/pingserver.lua b/test/pingserver.lua index 65a25405..5785b61b 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -43,6 +43,7 @@ end function init( ... ) print ("ping server start:", ...) + snax.enablecluster() -- enable cluster call -- init queue lock = queue() end From 250531c9a7442bfd931bcfd6b71ef2039de07b8e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 22 May 2015 20:51:00 +0800 Subject: [PATCH 378/729] bugfix: sproto.default --- lualib-src/sproto/lsproto.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 99448487..3f725b44 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -608,8 +608,8 @@ encode_default(const struct sproto_arg *args) { static int ldefault(lua_State *L) { int ret; - // 32 is enough for dummy buffer, because ldefault encode nothing but the header. - char dummy[32]; + // 64 is always enough for dummy buffer, except the type has many fields ( > 27). + char dummy[64]; struct sproto_type * st = lua_touserdata(L, 1); if (st == NULL) { return luaL_argerror(L, 1, "Need a sproto_type object"); @@ -617,7 +617,18 @@ ldefault(lua_State *L) { lua_newtable(L); ret = sproto_encode(st, dummy, sizeof(dummy), encode_default, L); if (ret<0) { - return luaL_error(L, "dummy buffer (%d) is too small", (int)sizeof(dummy)); + // try again + int sz = sizeof(dummy) * 2; + void * tmp = lua_newuserdata(L, sz); + lua_insert(L, -2); + for (;;) { + ret = sproto_encode(st, tmp, sz, encode_default, L); + if (ret >= 0) + break; + sz *= 2; + tmp = lua_newuserdata(L, sz); + lua_replace(L, -3); + } } return 1; } From 987a90af8bfda8c1c6b16546ea9dbe898dd10291 Mon Sep 17 00:00:00 2001 From: xiyanxiyan10 Date: Sat, 23 May 2015 16:35:15 +0800 Subject: [PATCH 379/729] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service-src/service_gate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 84d11c4d..7c95703d 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -135,7 +135,7 @@ _ctrl(struct gate * g, const void * msg, int sz) { skynet_socket_start(ctx, g->listen_id); return; } - if (memcmp(command, "close", i) == 0) { + if (memcmp(command, "close", i) == 0) { if (g->listen_id >= 0) { skynet_socket_close(ctx, g->listen_id); g->listen_id = -1; From 205824ab1200cd21d9d08faf6598fde63a4f5937 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 26 May 2015 16:05:31 +0800 Subject: [PATCH 380/729] use LUA_API instead of extern --- 3rd/lua/lua.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 747a372a..313e4cd0 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -460,7 +460,7 @@ struct lua_Debug { /* Add by skynet */ -extern lua_State * skynet_sig_L; +LUA_API lua_State * skynet_sig_L; LUA_API void (lua_checksig_)(lua_State *L); #define lua_checksig(L) if (skynet_sig_L) { lua_checksig_(L); } From 3a2c43e9e02d24158ca501fa57ce4cc5667a6d16 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 27 May 2015 14:54:26 +0800 Subject: [PATCH 381/729] set nodelay in clusterd, Issue #278 --- service/clusterd.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/service/clusterd.lua b/service/clusterd.lua index 8d340b70..12c4c4e7 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -20,6 +20,7 @@ local function open_channel(t, key) host = host, port = tonumber(port), response = read_response, + nodelay = true, } assert(c:connect(true)) t[key] = c From b8c54cbac270caf98c57bada2ad02314f9d40c9e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 27 May 2015 21:17:43 +0800 Subject: [PATCH 382/729] skynet.kill move into skynet.manager --- examples/watchdog.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index e02af169..32bceed5 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local netpack = require "netpack" +require "skynet.manager" local CMD = {} local SOCKET = {} @@ -15,6 +16,7 @@ end local function close_agent(fd) local a = agent[fd] if a then + -- The better way is send a message to agent, and let agent exit self. skynet.kill(a) agent[fd] = nil end From ca50a5f5181ff319b7feecacc2fc540736938245 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 28 May 2015 16:10:19 +0800 Subject: [PATCH 383/729] dns support underscore --- lualib/dns.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/dns.lua b/lualib/dns.lua index a38c00e6..6244b314 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -91,7 +91,7 @@ local function verify_domain_name(name) if not name:match("^[%l%d-%.]+$") then return false end - for w in name:gmatch("([%w-]+)%.?") do + for w in name:gmatch("([_%w%-]+)%.?") do if #w > MAX_LABEL_LEN then return false end @@ -113,7 +113,7 @@ end local function pack_question(name, qtype, qclass) local labels = {} - for w in name:gmatch("([%w-]+)%.?") do + for w in name:gmatch("([_%w%-]+)%.?") do table.insert(labels, string.pack("s1",w)) end table.insert(labels, '\0') @@ -282,7 +282,7 @@ function dns.resolve(name, ipv6) qdcount = 1, } local req = pack_header(question_header) .. pack_question(name, qtype, QCLASS.IN) - assert(dns_server, "Call dns.server fist") + assert(dns_server, "Call dns.server first") socket.write(dns_server, req) return suspend(question_header.tid, name, qtype) end From 07dbfd8651d1465df780996a696892e642a7186f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 28 May 2015 16:18:56 +0800 Subject: [PATCH 384/729] add underscore --- lualib/dns.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/dns.lua b/lualib/dns.lua index 6244b314..35e38084 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -88,7 +88,7 @@ local function verify_domain_name(name) if #name > MAX_DOMAIN_LEN then return false end - if not name:match("^[%l%d-%.]+$") then + if not name:match("^[_%l%d%-%.]+$") then return false end for w in name:gmatch("([_%w%-]+)%.?") do From 24f6994b50f36a7077dac3cea39d42975025ef64 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 29 May 2015 21:11:02 +0800 Subject: [PATCH 385/729] close uncomplete when socket disconnect, see Issue #280 --- examples/agent.lua | 15 ++++++++++++++- examples/client.lua | 6 +++++- examples/proto.lua | 2 ++ examples/watchdog.lua | 14 +++++++++----- lualib-src/lua-netpack.c | 14 ++++++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/examples/agent.lua b/examples/agent.lua index 45541f70..06db1108 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -4,6 +4,7 @@ local socket = require "socket" local sproto = require "sproto" local sprotoloader = require "sprotoloader" +local WATCHDOG local host local send_request @@ -26,6 +27,10 @@ function REQUEST:handshake() return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." } end +function REQUEST:quit() + skynet.call(WATCHDOG, "lua", "close", client_fd) +end + local function request(name, args, response) local f = assert(REQUEST[name]) local r = f(args) @@ -62,7 +67,10 @@ skynet.register_protocol { end } -function CMD.start(gate, fd) +function CMD.start(conf) + local fd = conf.client + local gate = conf.gate + WATCHDOG = conf.watchdog -- slot 1,2 set at main.lua host = sprotoloader.load(1):host "package" send_request = host:attach(sprotoloader.load(2)) @@ -77,6 +85,11 @@ function CMD.start(gate, fd) skynet.call(gate, "lua", "forward", fd) end +function CMD.disconnect() + -- todo: do something before exit + skynet.exit() +end + skynet.start(function() skynet.dispatch("lua", function(_,_, command, ...) local f = CMD[command] diff --git a/examples/client.lua b/examples/client.lua index effee9c7..92aaa16e 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -104,7 +104,11 @@ while true do dispatch_package() local cmd = socket.readstdin() if cmd then - send_request("get", { what = cmd }) + if cmd == "quit" then + send_request("quit") + else + send_request("get", { what = cmd }) + end else socket.usleep(100) end diff --git a/examples/proto.lua b/examples/proto.lua index 31b0c52d..af88a4b5 100644 --- a/examples/proto.lua +++ b/examples/proto.lua @@ -30,6 +30,8 @@ set 3 { } } +quit 4 {} + ]] proto.s2c = sprotoparser.parse [[ diff --git a/examples/watchdog.lua b/examples/watchdog.lua index 32bceed5..29111033 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -1,6 +1,5 @@ local skynet = require "skynet" local netpack = require "netpack" -require "skynet.manager" local CMD = {} local SOCKET = {} @@ -10,15 +9,16 @@ local agent = {} function SOCKET.open(fd, addr) skynet.error("New client from : " .. addr) agent[fd] = skynet.newservice("agent") - skynet.call(agent[fd], "lua", "start", gate, fd) + skynet.call(agent[fd], "lua", "start", { gate = gate, client = fd, watchdog = skynet.self() }) end local function close_agent(fd) local a = agent[fd] + agent[fd] = nil if a then - -- The better way is send a message to agent, and let agent exit self. - skynet.kill(a) - agent[fd] = nil + skynet.call(gate, "lua", "kick", fd) + -- disconnect never return + skynet.send(a, "lua", "disconnect") end end @@ -39,6 +39,10 @@ function CMD.start(conf) skynet.call(gate, "lua", "open" , conf) end +function CMD.close(fd) + close_agent(fd) +end + skynet.start(function() skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) if cmd == "socket" then diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index f42e21ca..1839abe3 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -210,6 +210,16 @@ push_more(lua_State *L, int fd, uint8_t *buffer, int size) { } } +static void +close_uncomplete(lua_State *L, int fd) { + struct queue *q = lua_touserdata(L,1); + struct uncomplete * uc = find_uncomplete(q, fd); + if (uc) { + skynet_free(uc->pack.buffer); + skynet_free(uc); + } +} + static int filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) { struct queue *q = lua_touserdata(L,1); @@ -343,6 +353,8 @@ lfilter(lua_State *L) { // ignore listen fd connect return 1; case SKYNET_SOCKET_TYPE_CLOSE: + // no more data in fd (message->id) + close_uncomplete(L, message->id); lua_pushvalue(L, lua_upvalueindex(TYPE_CLOSE)); lua_pushinteger(L, message->id); return 3; @@ -353,6 +365,8 @@ lfilter(lua_State *L) { pushstring(L, buffer, size); return 4; case SKYNET_SOCKET_TYPE_ERROR: + // no more data in fd (message->id) + close_uncomplete(L, message->id); lua_pushvalue(L, lua_upvalueindex(TYPE_ERROR)); lua_pushinteger(L, message->id); pushstring(L, buffer, size); From 69946d75c53eea3ff8c721faf4160a5bf69c125e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 30 May 2015 22:17:53 +0800 Subject: [PATCH 386/729] add config.logservice for user defined log service --- skynet-src/skynet_imp.h | 1 + skynet-src/skynet_main.c | 1 + skynet-src/skynet_start.c | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index c5a144a4..1126d2c9 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -8,6 +8,7 @@ struct skynet_config { const char * module_path; const char * bootstrap; const char * logger; + const char * logservice; }; #define THREAD_WORKER 0 diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 81148874..59e344f8 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -133,6 +133,7 @@ main(int argc, char *argv[]) { config.bootstrap = optstring("bootstrap","snlua bootstrap"); config.daemon = optstring("daemon", NULL); config.logger = optstring("logger", NULL); + config.logservice = optstring("logservice", "logger"); lua_close(L); diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 5f511d29..4b73c26b 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -223,9 +223,9 @@ skynet_start(struct skynet_config * config) { skynet_timer_init(); skynet_socket_init(); - struct skynet_context *ctx = skynet_context_new("logger", config->logger); + struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger); if (ctx == NULL) { - fprintf(stderr, "Can't launch logger service\n"); + fprintf(stderr, "Can't launch %s service\n", config->logservice); exit(1); } From 7f578be6490c48edd9eb42e8f536c180614dd747 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 1 Jun 2015 10:42:29 +0800 Subject: [PATCH 387/729] bugfix: Issue #283 --- lualib/skynet.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 4e099ba6..c6b7b973 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -174,7 +174,7 @@ function suspend(co, result, command, param, size) -- If the package is too large, returns nil. so we should report error back c.send(co_address, skynet.PTYPE_ERROR, co_session, "") end - elseif size == nil then + elseif size ~= nil then c.trash(param, size) ret = false end @@ -432,7 +432,7 @@ function skynet.dispatch_unknown_request(unknown) end local function unknown_response(session, address, msg, sz) - skynet.error(string.format("Response message :" , c.tostring(msg,sz))) + skynet.error(string.format("Response message : %s" , c.tostring(msg,sz))) error(string.format("Unknown session : %d from %x", session, address)) end @@ -479,7 +479,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) session_coroutine_address[co] = source suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...))) else - unknown_request(session, source, msg, sz, proto[prototype]) + unknown_request(session, source, msg, sz, proto[prototype].name) end end end From 7fb109dbb03b955dfeceffb82a4fcc14e87a3126 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 1 Jun 2015 10:57:30 +0800 Subject: [PATCH 388/729] See Issue #284 --- lualib/skynet/inject.lua | 2 +- service/debug_console.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua index f3ef65d2..3c87bbb3 100644 --- a/lualib/skynet/inject.lua +++ b/lualib/skynet/inject.lua @@ -44,7 +44,7 @@ return function(skynet, source, filename , ...) if proto then for k,v in pairs(proto) do local name, dispatch = v.name, v.dispatch - if name and dispatch then + if name and dispatch and not p[name] then local pp = {} p[name] = pp getupvaluetable(pp, dispatch, unique) diff --git a/service/debug_console.lua b/service/debug_console.lua index 142cf601..139e76bb 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -171,7 +171,7 @@ end local function adjust_address(address) if address:sub(1,1) ~= ":" then - address = tonumber("0x" .. address) | (skynet.harbor(skynet.self()) << 24) + address = assert(tonumber("0x" .. address), "Need an address") | (skynet.harbor(skynet.self()) << 24) end return address end From cc4756de35806b50eb9db8ce5ff805d1f8304788 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 Jun 2015 11:52:55 +0800 Subject: [PATCH 389/729] fix Issue #285 --- lualib-src/lua-debugchannel.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c index 102f065a..9e4539d3 100644 --- a/lualib-src/lua-debugchannel.c +++ b/lualib-src/lua-debugchannel.c @@ -59,6 +59,7 @@ channel_release(struct channel *c) { free(p); p = next; } + free(c); return NULL; } From 1fec2e6063e9157f06ec0b3c9e7555d89d309756 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 Jun 2015 09:42:49 +0800 Subject: [PATCH 390/729] sep size may greater than buffer node, See Issue #286 --- lualib-src/lua-socket.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index e12a9c2f..5ac0b359 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -190,7 +190,10 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) { } break; } - luaL_addlstring(&b, current->msg + sb->offset, (sz - skip < bytes) ? sz - skip : bytes); + int real_sz = sz - skip; + if (real_sz > 0) { + luaL_addlstring(&b, current->msg + sb->offset, (real_sz < bytes) ? real_sz : bytes); + } return_free_node(L,2,sb); sz-=bytes; if (sz==0) From 55d0d57d5c9d3d14f6fba31acd4ea92bfbac0aaa Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 4 Jun 2015 16:06:09 +0800 Subject: [PATCH 391/729] skynet.fork returns the coroutine, see Issue #287 --- lualib/skynet.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index c6b7b973..02a966fb 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -450,6 +450,7 @@ function skynet.fork(func,...) func(tunpack(args)) end) table.insert(fork_queue, co) + return co end local function raw_dispatch_message(prototype, msg, sz, session, source, ...) From d3ac522dc67b9c35a689992aa28107fa0a7f1304 Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 5 Jun 2015 19:04:43 +0800 Subject: [PATCH 392/729] typo in type of variable --- skynet-src/skynet_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 4527529e..ea1aadcf 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -82,7 +82,7 @@ skynet_current_handle(void) { void * handle = pthread_getspecific(G_NODE.handle_key); return (uint32_t)(uintptr_t)handle; } else { - uintptr_t v = (uint32_t)(-THREAD_MAIN); + uint32_t v = (uint32_t)(-THREAD_MAIN); return v; } } From ac093c7142f64d65788ec3b80f95e0c84d940e27 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 7 Jun 2015 12:38:55 +0800 Subject: [PATCH 393/729] fix memory leak, see Issue #290 --- service-src/service_harbor.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index b78953d1..5940d62c 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -365,6 +365,7 @@ dispatch_name_queue(struct harbor *h, struct keyvalue * node) { while ((m = pop_queue(queue)) != NULL) { m->header.destination |= (handle & HANDLE_MASK); send_remote(context, fd, m->buffer, m->size, &m->header); + skynet_free(m->buffer); } } @@ -381,6 +382,7 @@ dispatch_queue(struct harbor *h, int id) { struct harbor_msg * m; while ((m = pop_queue(queue)) != NULL) { send_remote(h->ctx, fd, m->buffer, m->size, &m->header); + skynet_free(m->buffer); } release_queue(queue); s->queue = NULL; From 8214c5389112b78ec9477a355927f24908513b29 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Jun 2015 10:31:00 +0800 Subject: [PATCH 394/729] release alpha7 --- HISTORY.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index cb7c2e3a..2410fbe3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,15 @@ +v1.0.0-alpha7 (2015-6-8) +----------- +* console support launch snax service +* Add cluster.snax +* Add nodelay in clusterd +* Merge sproto bugfix patch +* Move some skynet api into skynet.manager +* DNS support underscore +* Add logservice in config file for user defined log service +* skynet.fork returns coroutine +* Fix a few of bugs , see the commits log + v1.0.0-alpha6 (2015-5-18) ----------- * bugfix: httpc.get From 57d43a588e70df567a8fecfa5d26d3b9caf7db36 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 9 Jun 2015 20:00:20 +0800 Subject: [PATCH 395/729] Check quit flag before pthread_cond_wait --- skynet-src/skynet_start.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 4b73c26b..1d9ae6c4 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -22,6 +22,7 @@ struct monitor { pthread_cond_t cond; pthread_mutex_t mutex; int sleep; + int quit; }; struct worker_parm { @@ -49,7 +50,7 @@ wakeup(struct monitor *m, int busy) { } static void * -_socket(void *p) { +thread_socket(void *p) { struct monitor * m = p; skynet_initthread(THREAD_SOCKET); for (;;) { @@ -79,7 +80,7 @@ free_monitor(struct monitor *m) { } static void * -_monitor(void *p) { +thread_monitor(void *p) { struct monitor * m = p; int i; int n = m->count; @@ -99,7 +100,7 @@ _monitor(void *p) { } static void * -_timer(void *p) { +thread_timer(void *p) { struct monitor * m = p; skynet_initthread(THREAD_TIMER); for (;;) { @@ -111,12 +112,14 @@ _timer(void *p) { // wakeup socket thread skynet_socket_exit(); // wakeup all worker thread + // we don't need lock m before set m->quit, because it can only set once. + m->quit = 1; pthread_cond_broadcast(&m->cond); return NULL; } static void * -_worker(void *p) { +thread_worker(void *p) { struct worker_parm *wp = p; int id = wp->id; int weight = wp->weight; @@ -124,28 +127,28 @@ _worker(void *p) { struct skynet_monitor *sm = m->m[id]; skynet_initthread(THREAD_WORKER); struct message_queue * q = NULL; - for (;;) { + while (!m->quit) { q = skynet_context_message_dispatch(sm, q, weight); if (q == NULL) { if (pthread_mutex_lock(&m->mutex) == 0) { ++ m->sleep; // "spurious wakeup" is harmless, // because skynet_context_message_dispatch() can be call at any time. - pthread_cond_wait(&m->cond, &m->mutex); + if (!m->quit) + pthread_cond_wait(&m->cond, &m->mutex); -- m->sleep; if (pthread_mutex_unlock(&m->mutex)) { fprintf(stderr, "unlock mutex error"); exit(1); } } - } - CHECK_ABORT + } } return NULL; } static void -_start(int thread) { +start(int thread) { pthread_t pid[thread+3]; struct monitor *m = skynet_malloc(sizeof(*m)); @@ -167,9 +170,9 @@ _start(int thread) { exit(1); } - create_thread(&pid[0], _monitor, m); - create_thread(&pid[1], _timer, m); - create_thread(&pid[2], _socket, m); + create_thread(&pid[0], thread_monitor, m); + create_thread(&pid[1], thread_timer, m); + create_thread(&pid[2], thread_socket, m); static int weight[] = { -1, -1, -1, -1, 0, 0, 0, 0, @@ -185,7 +188,7 @@ _start(int thread) { } else { wp[i].weight = 0; } - create_thread(&pid[i+3], _worker, &wp[i]); + create_thread(&pid[i+3], thread_worker, &wp[i]); } for (i=0;ibootstrap); - _start(config->thread); + start(config->thread); // harbor_exit may call socket send, so it should exit before socket_free skynet_harbor_exit(); From 039235b3a3ab67a03b163a1e08ae65b3e63080b6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 9 Jun 2015 21:46:37 +0800 Subject: [PATCH 396/729] lock before set quit flag --- skynet-src/skynet_start.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 1d9ae6c4..4123c624 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -112,9 +112,10 @@ thread_timer(void *p) { // wakeup socket thread skynet_socket_exit(); // wakeup all worker thread - // we don't need lock m before set m->quit, because it can only set once. + pthread_mutex_lock(&m->mutex); m->quit = 1; pthread_cond_broadcast(&m->cond); + pthread_mutex_unlock(&m->mutex); return NULL; } From 7fa1d548edc7b84a6528176ec2ad98bfce446548 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 16 Jun 2015 10:56:28 +0800 Subject: [PATCH 397/729] bugfix: don't call skynet_free directly when send failed --- skynet-src/skynet_socket.c | 1 - skynet-src/socket_server.c | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index f2dff1cc..3f2c9a04 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -107,7 +107,6 @@ skynet_socket_poll() { static int check_wsz(struct skynet_context *ctx, int id, void *buffer, int64_t wsz) { if (wsz < 0) { - skynet_free(buffer); return -1; } else if (wsz > 1024 * 1024) { int kb4 = wsz / 1024 / 4; diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index eba07624..7c287b90 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1286,11 +1286,19 @@ socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * a return request.u.open.id; } +static void +free_buffer(struct socket_server *ss, const void * buffer, int sz) { + struct send_object so; + send_object_init(ss, &so, (void *)buffer, sz); + so.free_func((void *)buffer); +} + // return -1 when error int64_t socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) { struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + free_buffer(ss, buffer, sz); return -1; } @@ -1307,6 +1315,7 @@ void socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) { struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + free_buffer(ss, buffer, sz); return; } @@ -1489,6 +1498,7 @@ int64_t socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp_address *addr, const void *buffer, int sz) { struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { + free_buffer(ss, buffer, sz); return -1; } @@ -1507,6 +1517,7 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp addrsz = 1+2+16; // 1 type, 2 port, 16 ipv6 break; default: + free_buffer(ss, buffer, sz); return -1; } From f36b1731553afa033ee6d287bd195bd1209d11dc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Jun 2015 22:06:11 +0800 Subject: [PATCH 398/729] update to lua 5.3.1 --- 3rd/lua/Makefile | 62 +++++------ 3rd/lua/README | 2 +- 3rd/lua/lapi.c | 179 +++++++++++------------------- 3rd/lua/lapi.h | 6 +- 3rd/lua/lauxlib.c | 126 +-------------------- 3rd/lua/lbaselib.c | 4 +- 3rd/lua/lcode.c | 53 ++++----- 3rd/lua/lcode.h | 2 +- 3rd/lua/ldblib.c | 21 +++- 3rd/lua/ldebug.c | 63 ++++++----- 3rd/lua/ldebug.h | 11 +- 3rd/lua/ldo.c | 30 ++--- 3rd/lua/ldo.h | 4 +- 3rd/lua/ldump.c | 28 ++--- 3rd/lua/lfunc.c | 62 ++++------- 3rd/lua/lfunc.h | 11 +- 3rd/lua/lgc.c | 72 ++++++------ 3rd/lua/liolib.c | 15 +-- 3rd/lua/llex.c | 28 ++--- 3rd/lua/llimits.h | 107 +++++++++++++++--- 3rd/lua/lmathlib.c | 5 +- 3rd/lua/lmem.c | 11 +- 3rd/lua/loadlib.c | 7 +- 3rd/lua/lobject.c | 41 ++++--- 3rd/lua/lobject.h | 54 +++++---- 3rd/lua/loslib.c | 76 +++++++------ 3rd/lua/lparser.c | 75 ++++++------- 3rd/lua/lstate.c | 10 +- 3rd/lua/lstate.h | 3 +- 3rd/lua/lstring.c | 72 +++++++++--- 3rd/lua/lstring.h | 5 +- 3rd/lua/lstrlib.c | 125 +++++++++++++++++---- 3rd/lua/ltable.c | 123 +++++++++++---------- 3rd/lua/ltablib.c | 8 +- 3rd/lua/ltm.c | 5 +- 3rd/lua/lua.c | 36 +++--- 3rd/lua/lua.h | 19 ++-- 3rd/lua/luac.c | 34 +++--- 3rd/lua/luaconf.h | 211 +++++++++++++++++++----------------- 3rd/lua/lualib.h | 2 - 3rd/lua/lundump.c | 23 ++-- 3rd/lua/lutf8lib.c | 13 ++- 3rd/lua/lvm.c | 265 ++++++++++++++++++++++++++++----------------- 3rd/lua/lvm.h | 16 ++- 44 files changed, 1149 insertions(+), 976 deletions(-) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 7430722c..d71c75c8 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -7,7 +7,7 @@ PLAT= none CC= gcc -std=gnu99 -CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) +CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= -lm $(SYSLIBS) $(MYLIBS) @@ -130,68 +130,68 @@ solaris: # DO NOT DELETE lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ - ltable.h lundump.h lvm.h + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ + ltable.h lundump.h lvm.h lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lbitlib.o: lbitlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ - llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ - ldo.h lgc.h lstring.h ltable.h lvm.h + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lgc.h lstring.h ltable.h lvm.h lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ - ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h + lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ + ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ - lparser.h lstring.h ltable.h lundump.h lvm.h + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ + lparser.h lstring.h ltable.h lundump.h lvm.h ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ - ltm.h lzio.h lmem.h lundump.h + ltm.h lzio.h lmem.h lundump.h lfunc.o: lfunc.c lprefix.h lua.h luaconf.h lfunc.h lobject.h llimits.h \ - lgc.h lstate.h ltm.h lzio.h lmem.h + lgc.h lstate.h ltm.h lzio.h lmem.h lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldo.h \ - lobject.h lstate.h ltm.h lzio.h lmem.h lgc.h llex.h lparser.h lstring.h \ - ltable.h +llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ + lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ + lstring.h ltable.h lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ - ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ - lvm.h + ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ + lvm.h lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ - llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ - ldo.h lfunc.h lstring.h lgc.h ltable.h + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lfunc.h lstring.h lgc.h ltable.h lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ - lstring.h ltable.h + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ + lstring.h ltable.h lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ - lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h + llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h lobject.h llimits.h \ - lstate.h ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h + lstate.h ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ - lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ - lundump.h + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ + lundump.h lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ - ltable.h lvm.h + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ + ltable.h lvm.h lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ - lobject.h ltm.h lzio.h + lobject.h ltm.h lzio.h # (end of Makefile) diff --git a/3rd/lua/README b/3rd/lua/README index d658c4fd..a77dc6bf 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,4 +1,4 @@ -This is a modify version of lua 5.3.0 (http://www.lua.org/ftp/lua-5.3.0.tar.gz) . +This is a modify version of lua 5.3.1 (http://www.lua.org/ftp/lua-5.3.1.tar.gz) . For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 8d9c3765..fea9eb27 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.244 2014/12/26 14:43:45 roberto Exp $ +** $Id: lapi.c,v 2.249 2015/04/06 12:23:48 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -28,7 +28,6 @@ #include "ltm.h" #include "lundump.h" #include "lvm.h" -#include "lfunc.h" @@ -52,29 +51,29 @@ const char lua_ident[] = /* test for valid but not pseudo index */ #define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) -#define api_checkvalidindex(o) api_check(isvalid(o), "invalid index") +#define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index") -#define api_checkstackindex(i, o) \ - api_check(isstackindex(i, o), "index not in the stack") +#define api_checkstackindex(l, i, o) \ + api_check(l, isstackindex(i, o), "index not in the stack") static TValue *index2addr (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { TValue *o = ci->func + idx; - api_check(idx <= ci->top - (ci->func + 1), "unacceptable index"); + api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return o; } else if (!ispseudo(idx)) { /* negative index */ - api_check(idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); return L->top + idx; } else if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; - api_check(idx <= MAXUPVAL + 1, "upvalue index too large"); + api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); if (ttislcf(ci->func)) /* light C function? */ return NONVALIDVALUE; /* it has no upvalues */ else { @@ -99,7 +98,7 @@ LUA_API int lua_checkstack (lua_State *L, int n) { int res; CallInfo *ci = L->ci; lua_lock(L); - api_check(n >= 0, "negative 'n'"); + api_check(L, n >= 0, "negative 'n'"); if (L->stack_last - L->top > n) /* stack large enough? */ res = 1; /* yes; check is OK */ else { /* no; need to grow stack */ @@ -121,11 +120,12 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { if (from == to) return; lua_lock(to); api_checknelems(from, n); - api_check(G(from) == G(to), "moving among independent states"); - api_check(to->ci->top - to->top >= n, "not enough elements to move"); + api_check(from, G(from) == G(to), "moving among independent states"); + api_check(from, to->ci->top - to->top >= n, "not enough elements to move"); from->top -= n; for (i = 0; i < n; i++) { - setobj2s(to, to->top++, from->top + i); + setobj2s(to, to->top, from->top + i); + api_incr_top(to); } lua_unlock(to); } @@ -160,7 +160,7 @@ LUA_API const lua_Number *lua_version (lua_State *L) { LUA_API int lua_absindex (lua_State *L, int idx) { return (idx > 0 || ispseudo(idx)) ? idx - : cast_int(L->top - L->ci->func + idx); + : cast_int(L->top - L->ci->func) + idx; } @@ -173,13 +173,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { StkId func = L->ci->func; lua_lock(L); if (idx >= 0) { - api_check(idx <= L->stack_last - (func + 1), "new top too large"); + api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); while (L->top < (func + 1) + idx) setnilvalue(L->top++); L->top = (func + 1) + idx; } else { - api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top"); + api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); L->top += idx+1; /* 'subtract' index (index is negative) */ } lua_unlock(L); @@ -209,8 +209,8 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { lua_lock(L); t = L->top - 1; /* end of stack segment being rotated */ p = index2addr(L, idx); /* start of segment */ - api_checkstackindex(idx, p); - api_check((n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); + api_checkstackindex(L, idx, p); + api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ reverse(L, p, m); /* reverse the prefix with length 'n' */ reverse(L, m + 1, t); /* reverse the suffix */ @@ -224,7 +224,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { lua_lock(L); fr = index2addr(L, fromidx); to = index2addr(L, toidx); - api_checkvalidindex(to); + api_checkvalidindex(L, to); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ luaC_barrier(L, clCvalue(L->ci->func), fr); @@ -256,7 +256,7 @@ LUA_API int lua_type (lua_State *L, int idx) { LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); - api_check(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); + api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); return ttypename(t); } @@ -306,7 +306,7 @@ LUA_API void lua_arith (lua_State *L, int op) { else { /* for unary operations, add fake 2nd operand */ api_checknelems(L, 1); setobjs2s(L, L->top, L->top - 1); - L->top++; + api_incr_top(L); } /* first operand at top - 2, second at top - 1; result go to top - 2 */ luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2); @@ -326,7 +326,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; - default: api_check(0, "invalid option"); + default: api_check(L, 0, "invalid option"); } } lua_unlock(L); @@ -383,15 +383,17 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { luaO_tostring(L, o); lua_unlock(L); } - if (len != NULL) *len = tsvalue(o)->len; + if (len != NULL) + *len = vslen(o); return svalue(o); } LUA_API size_t lua_rawlen (lua_State *L, int idx) { StkId o = index2addr(L, idx); - switch (ttnov(o)) { - case LUA_TSTRING: return tsvalue(o)->len; + switch (ttype(o)) { + case LUA_TSHRSTR: return tsvalue(o)->shrlen; + case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; case LUA_TUSERDATA: return uvalue(o)->len; case LUA_TTABLE: return luaH_getn(hvalue(o)); default: return 0; @@ -432,9 +434,8 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { case LUA_TCCL: return clCvalue(o); case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); case LUA_TTHREAD: return thvalue(o); - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - return lua_touserdata(L, idx); + case LUA_TUSERDATA: return getudatamem(uvalue(o)); + case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } } @@ -483,20 +484,19 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { LUA_API const char *lua_pushstring (lua_State *L, const char *s) { - if (s == NULL) { - lua_pushnil(L); - return NULL; - } + lua_lock(L); + if (s == NULL) + setnilvalue(L->top); else { TString *ts; - lua_lock(L); luaC_checkGC(L); ts = luaS_new(L, s); setsvalue2s(L, L->top, ts); - api_incr_top(L); - lua_unlock(L); - return getstr(ts); + s = getstr(ts); /* internal copy's address */ } + api_incr_top(L); + lua_unlock(L); + return s; } @@ -532,7 +532,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { else { CClosure *cl; api_checknelems(L, n); - api_check(n <= MAXUPVAL, "upvalue index too large"); + api_check(L, n <= MAXUPVAL, "upvalue index too large"); luaC_checkGC(L); cl = luaF_newCclosure(L, n); cl->f = fn; @@ -584,7 +584,8 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) { const TValue *gt; /* global table */ lua_lock(L); gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, name)); + setsvalue2s(L, L->top, luaS_new(L, name)); + api_incr_top(L); luaV_gettable(L, gt, L->top - 1, L->top - 1); lua_unlock(L); return ttnov(L->top - 1); @@ -629,7 +630,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) { StkId t; lua_lock(L); t = index2addr(L, idx); - api_check(ttistable(t), "table expected"); + api_check(L, ttistable(t), "table expected"); setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); lua_unlock(L); return ttnov(L->top - 1); @@ -640,7 +641,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { StkId t; lua_lock(L); t = index2addr(L, idx); - api_check(ttistable(t), "table expected"); + api_check(L, ttistable(t), "table expected"); setobj2s(L, L->top, luaH_getint(hvalue(t), n)); api_incr_top(L); lua_unlock(L); @@ -653,7 +654,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { TValue k; lua_lock(L); t = index2addr(L, idx); - api_check(ttistable(t), "table expected"); + api_check(L, ttistable(t), "table expected"); setpvalue(&k, cast(void *, p)); setobj2s(L, L->top, luaH_get(hvalue(t), &k)); api_incr_top(L); @@ -706,7 +707,7 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) { StkId o; lua_lock(L); o = index2addr(L, idx); - api_check(ttisfulluserdata(o), "full userdata expected"); + api_check(L, ttisfulluserdata(o), "full userdata expected"); getuservalue(L, uvalue(o), L->top); api_incr_top(L); lua_unlock(L); @@ -725,7 +726,8 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) { lua_lock(L); api_checknelems(L, 1); gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, name)); + setsvalue2s(L, L->top, luaS_new(L, name)); + api_incr_top(L); luaV_settable(L, gt, L->top - 1, L->top - 2); L->top -= 2; /* pop value and key */ lua_unlock(L); @@ -748,7 +750,8 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); - setsvalue2s(L, L->top++, luaS_new(L, k)); + setsvalue2s(L, L->top, luaS_new(L, k)); + api_incr_top(L); luaV_settable(L, t, L->top - 1, L->top - 2); L->top -= 2; /* pop value and key */ lua_unlock(L); @@ -760,7 +763,8 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); - setivalue(L->top++, n); + setivalue(L->top, n); + api_incr_top(L); luaV_settable(L, t, L->top - 1, L->top - 2); L->top -= 2; /* pop value and key */ lua_unlock(L); @@ -773,7 +777,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 2); o = index2addr(L, idx); - api_check(ttistable(o), "table expected"); + api_check(L, ttistable(o), "table expected"); t = hvalue(o); setobj2t(L, luaH_set(L, t, L->top-2), L->top-1); invalidateTMcache(t); @@ -789,7 +793,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); - api_check(ttistable(o), "table expected"); + api_check(L, ttistable(o), "table expected"); t = hvalue(o); luaH_setint(L, t, n, L->top - 1); luaC_barrierback(L, t, L->top-1); @@ -805,7 +809,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); - api_check(ttistable(o), "table expected"); + api_check(L, ttistable(o), "table expected"); t = hvalue(o); setpvalue(&k, cast(void *, p)); setobj2t(L, luaH_set(L, t, &k), L->top - 1); @@ -824,7 +828,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { if (ttisnil(L->top - 1)) mt = NULL; else { - api_check(ttistable(L->top - 1), "table expected"); + api_check(L, ttistable(L->top - 1), "table expected"); mt = hvalue(L->top - 1); } switch (ttnov(obj)) { @@ -860,7 +864,7 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); - api_check(ttisfulluserdata(o), "full userdata expected"); + api_check(L, ttisfulluserdata(o), "full userdata expected"); setuservalue(L, uvalue(o), L->top - 1); luaC_barrier(L, gcvalue(o), L->top - 1); L->top--; @@ -874,7 +878,7 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { #define checkresults(L,na,nr) \ - api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ + api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ "results from function overflow current stack size") @@ -882,10 +886,10 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k) { StkId func; lua_lock(L); - api_check(k == NULL || !isLua(L->ci), + api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); - api_check(L->status == LUA_OK, "cannot do calls on non-normal thread"); + api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); func = L->top - (nargs+1); if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ @@ -923,16 +927,16 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, int status; ptrdiff_t func; lua_lock(L); - api_check(k == NULL || !isLua(L->ci), + api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); - api_check(L->status == LUA_OK, "cannot do calls on non-normal thread"); + api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) func = 0; else { StkId o = index2addr(L, errfunc); - api_checkstackindex(errfunc, o); + api_checkstackindex(L, errfunc, o); func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ @@ -985,59 +989,6 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, } -static Proto * cloneproto (lua_State *L, const Proto *src) { - /* copy constants and nested proto */ - int i,n; - Proto *f = luaF_newproto(L, src->sp); - n = src->sp->sizek; - f->k=luaM_newvector(L,n,TValue); - for (i=0; ik[i]); - for (i=0; ik[i]; - TValue *o=&f->k[i]; - if (ttisstring(s)) { - TString * str = luaS_newlstr(L,svalue(s),tsvalue(s)->len); - setsvalue2n(L,o,str); - } else { - setobj(L,o,s); - } - } - n = src->sp->sizep; - f->p=luaM_newvector(L,n,struct Proto *); - for (i=0; ip[i]=NULL; - for (i=0; ip[i]=cloneproto(L, src->p[i]); - } - return f; -} - -LUA_API void lua_clonefunction (lua_State *L, const void * fp) { - LClosure *cl; - LClosure *f = cast(LClosure *, fp); - lua_lock(L); - if (f->p->sp->l_G == G(L)) { - setclLvalue(L,L->top,f); - api_incr_top(L); - lua_unlock(L); - return; - } - cl = luaF_newLclosure(L,f->nupvalues); - cl->p = cloneproto(L, f->p); - setclLvalue(L,L->top,cl); - api_incr_top(L); - luaF_initupvals(L, cl); - - if (cl->nupvalues >= 1) { /* does it have an upvalue? */ - /* get global table from registry */ - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, cl->upvals[0]->v, gt); - luaC_upvalbarrier(L, cl->upvals[0]); - } - lua_unlock(L); -} - LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; TValue *o; @@ -1150,7 +1101,7 @@ LUA_API int lua_next (lua_State *L, int idx) { int more; lua_lock(L); t = index2addr(L, idx); - api_check(ttistable(t), "table expected"); + api_check(L, ttistable(t), "table expected"); more = luaH_next(L, hvalue(t), L->top - 1); if (more) { api_incr_top(L); @@ -1232,7 +1183,7 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val, case LUA_TLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; - SharedProto *p = f->p->sp; + Proto *p = f->p; if (!(1 <= n && n <= p->sizeupvalues)) return NULL; *val = f->upvals[n-1]->v; if (uv) *uv = f->upvals[n - 1]; @@ -1282,9 +1233,9 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LClosure *f; StkId fi = index2addr(L, fidx); - api_check(ttisLclosure(fi), "Lua function expected"); + api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); + api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; return &f->upvals[n - 1]; /* get its upvalue pointer */ } @@ -1298,11 +1249,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { } case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); - api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index"); + api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); return &f->upvalue[n - 1]; } default: { - api_check(0, "closure expected"); + api_check(L, 0, "closure expected"); return NULL; } } diff --git a/3rd/lua/lapi.h b/3rd/lua/lapi.h index 092f5e97..6d36dee3 100644 --- a/3rd/lua/lapi.h +++ b/3rd/lua/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 roberto Exp $ +** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ @@ -11,13 +11,13 @@ #include "llimits.h" #include "lstate.h" -#define api_incr_top(L) {L->top++; api_check(L->top <= L->ci->top, \ +#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ "stack overflow");} #define adjustresults(L,nres) \ { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } -#define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \ +#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ "not enough elements in the stack") diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 72ef5d95..b8bace7f 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.279 2014/12/14 18:32:26 roberto Exp $ +** $Id: lauxlib.c,v 1.280 2015/02/03 17:38:24 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -286,7 +286,7 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { */ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { - if (luaL_getmetatable(L, tname)) /* name already in use? */ + if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ return 0; /* leave previous value on top, but return 0 */ lua_pop(L, 1); lua_newtable(L); /* create metatable */ @@ -638,7 +638,7 @@ static int skipcomment (LoadF *lf, int *cp) { } -static int luaL_loadfilex_ (lua_State *L, const char *filename, +LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { LoadF lf; int status, readstatus; @@ -970,123 +970,3 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { ver, *v); } -// use clonefunction - -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - -struct codecache { - int lock; - lua_State *L; -}; - -static struct codecache CC = { 0 , NULL }; - -static void -clearcache() { - if (CC.L == NULL) - return; - LOCK(&CC) - lua_close(CC.L); - CC.L = luaL_newstate(); - UNLOCK(&CC) -} - -static void -init() { - CC.lock = 0; - CC.L = luaL_newstate(); -} - -static const void * -load(const char *key) { - if (CC.L == NULL) - return NULL; - LOCK(&CC) - lua_State *L = CC.L; - lua_pushstring(L, key); - lua_rawget(L, LUA_REGISTRYINDEX); - const void * result = lua_touserdata(L, -1); - lua_pop(L, 1); - UNLOCK(&CC) - - return result; -} - -static const void * -save(const char *key, const void * proto) { - lua_State *L; - const void * result = NULL; - - LOCK(&CC) - if (CC.L == NULL) { - init(); - L = CC.L; - } else { - L = CC.L; - lua_pushstring(L, key); - lua_pushvalue(L, -1); - lua_rawget(L, LUA_REGISTRYINDEX); - result = lua_touserdata(L, -1); /* stack: key oldvalue */ - if (result == NULL) { - lua_pop(L,1); - lua_pushlightuserdata(L, (void *)proto); - lua_rawset(L, LUA_REGISTRYINDEX); - } else { - lua_pop(L,2); - } - } - UNLOCK(&CC) - return result; -} - -LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, - const char *mode) { - const void * proto = load(filename); - if (proto) { - lua_clonefunction(L, proto); - return LUA_OK; - } - lua_State * eL = luaL_newstate(); - if (eL == NULL) { - lua_pushliteral(L, "New state failed"); - return LUA_ERRMEM; - } - int err = luaL_loadfilex_(eL, filename, mode); - if (err != LUA_OK) { - size_t sz = 0; - const char * msg = lua_tolstring(eL, -1, &sz); - lua_pushlstring(L, msg, sz); - lua_close(eL); - return err; - } - proto = lua_topointer(eL, -1); - const void * oldv = save(filename, proto); - if (oldv) { - lua_close(eL); - lua_clonefunction(L, oldv); - } else { - lua_clonefunction(L, proto); - /* Never close it. notice: memory leak */ - } - - return LUA_OK; -} - -static int -cache_clear(lua_State *L) { - (void)(L); - clearcache(); - return 0; -} - -LUAMOD_API int luaopen_cache(lua_State *L) { - luaL_Reg l[] = { - { "clear", cache_clear }, - { NULL, NULL }, - }; - luaL_newlib(L,l); - lua_getglobal(L, "loadfile"); - lua_setfield(L, -2, "loadfile"); - return 1; -} diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index a2403952..9a151245 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.309 2014/12/10 12:26:42 roberto Exp $ +** $Id: lbaselib.c,v 1.310 2015/03/28 19:14:47 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ @@ -55,7 +55,7 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) { return NULL; do { int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : toupper((unsigned char)*s) - 'A' + 10; + : (toupper((unsigned char)*s) - 'A') + 10; if (digit >= base) return NULL; /* invalid numeral */ n = n * base + digit; s++; diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 0f0a6752..d6f0fcd8 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.99 2014/12/29 16:49:25 roberto Exp $ +** $Id: lcode.c,v 2.101 2015/04/29 18:24:11 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -29,8 +29,8 @@ #include "lvm.h" -/* Maximum number of registers in a Lua function */ -#define MAXREGS 250 +/* Maximum number of registers in a Lua function (must fit in 8 bits) */ +#define MAXREGS 255 #define hasjumps(e) ((e)->t != (e)->f) @@ -55,7 +55,7 @@ void luaK_nil (FuncState *fs, int from, int n) { Instruction *previous; int l = from + n - 1; /* last register to set nil */ if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - previous = &fs->f->sp->code[fs->pc-1]; + previous = &fs->f->code[fs->pc-1]; if (GET_OPCODE(*previous) == OP_LOADNIL) { int pfrom = GETARG_A(*previous); int pl = pfrom + GETARG_B(*previous); @@ -95,7 +95,7 @@ static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->sp->code[pc]; + Instruction *jmp = &fs->f->code[pc]; int offset = dest-(pc+1); lua_assert(dest != NO_JUMP); if (abs(offset) > MAXARG_sBx) @@ -115,7 +115,7 @@ int luaK_getlabel (FuncState *fs) { static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->sp->code[pc]); + int offset = GETARG_sBx(fs->f->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ return NO_JUMP; /* end of list */ else @@ -124,7 +124,7 @@ static int getjump (FuncState *fs, int pc) { static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->sp->code[pc]; + Instruction *pi = &fs->f->code[pc]; if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) return pi-1; else @@ -197,10 +197,10 @@ void luaK_patchclose (FuncState *fs, int list, int level) { level++; /* argument is +1 to reserve 0 as non-op */ while (list != NO_JUMP) { int next = getjump(fs, list); - lua_assert(GET_OPCODE(fs->f->sp->code[list]) == OP_JMP && - (GETARG_A(fs->f->sp->code[list]) == 0 || - GETARG_A(fs->f->sp->code[list]) >= level)); - SETARG_A(fs->f->sp->code[list], level); + lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && + (GETARG_A(fs->f->code[list]) == 0 || + GETARG_A(fs->f->code[list]) >= level)); + SETARG_A(fs->f->code[list], level); list = next; } } @@ -230,13 +230,13 @@ static int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; dischargejpc(fs); /* 'pc' will change */ /* put new instruction in code array */ - luaM_growvector(fs->ls->L, f->sp->code, fs->pc, f->sp->sizecode, Instruction, + luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, MAX_INT, "opcodes"); - f->sp->code[fs->pc] = i; + f->code[fs->pc] = i; /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->sp->lineinfo, fs->pc, f->sp->sizelineinfo, int, + luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, MAX_INT, "opcodes"); - f->sp->lineinfo[fs->pc] = fs->ls->lastline; + f->lineinfo[fs->pc] = fs->ls->lastline; return fs->pc++; } @@ -277,10 +277,11 @@ int luaK_codek (FuncState *fs, int reg, int k) { void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; - if (newstack > fs->f->sp->maxstacksize) { + if (newstack > fs->f->maxstacksize) { if (newstack >= MAXREGS) - luaX_syntaxerror(fs->ls, "function or expression too complex"); - fs->f->sp->maxstacksize = cast_byte(newstack); + luaX_syntaxerror(fs->ls, + "function or expression needs too many registers"); + fs->f->maxstacksize = cast_byte(newstack); } } @@ -322,13 +323,13 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { return k; /* reuse index */ } /* constant not found; create a new entry */ - oldsize = f->sp->sizek; + oldsize = f->sizek; k = fs->nk; /* numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setivalue(idx, k); - luaM_growvector(L, f->k, k, f->sp->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sp->sizek) setnilvalue(&f->k[oldsize++]); + luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); + while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); fs->nk++; luaC_barrier(L, f, v); @@ -573,8 +574,8 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { case VKFLT: { e->u.info = luaK_numberK(fs, e->u.nval); e->k = VK; - /* go through */ } + /* FALLTHROUGH */ case VK: { vk: if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ @@ -793,7 +794,7 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { static void codeexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { lua_assert(op >= OP_ADD); - if (op <= OP_BNOT && constfolding(fs, op - OP_ADD + LUA_OPADD, e1, e2)) + if (op <= OP_BNOT && constfolding(fs, (op - OP_ADD) + LUA_OPADD, e1, e2)) return; /* result has been folded */ else { int o1, o2; @@ -920,11 +921,11 @@ void luaK_posfix (FuncState *fs, BinOpr op, break; } case OPR_EQ: case OPR_LT: case OPR_LE: { - codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2); + codecomp(fs, cast(OpCode, (op - OPR_EQ) + OP_EQ), 1, e1, e2); break; } case OPR_NE: case OPR_GT: case OPR_GE: { - codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2); + codecomp(fs, cast(OpCode, (op - OPR_NE) + OP_EQ), 0, e1, e2); break; } default: lua_assert(0); @@ -933,7 +934,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, void luaK_fixline (FuncState *fs, int line) { - fs->f->sp->lineinfo[fs->pc - 1] = line; + fs->f->lineinfo[fs->pc - 1] = line; } diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index b4c865f9..43ab86db 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -40,7 +40,7 @@ typedef enum BinOpr { typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; -#define getcode(fs,e) ((fs)->f->sp->code[(e)->u.info]) +#define getcode(fs,e) ((fs)->f->code[(e)->u.info]) #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) diff --git a/3rd/lua/ldblib.c b/3rd/lua/ldblib.c index 24a11b53..91514584 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.148 2015/01/02 12:52:22 roberto Exp $ +** $Id: ldblib.c,v 1.149 2015/02/19 17:06:21 roberto Exp $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -27,6 +27,17 @@ static const int HOOKKEY = 0; +/* +** If L1 != L, L1 can be in any state, and therefore there is no +** garanties about its stack space; any push in L1 must be +** checked. +*/ +static void checkstack (lua_State *L, lua_State *L1, int n) { + if (L != L1 && !lua_checkstack(L1, n)) + luaL_error(L, "stack overflow"); +} + + static int db_getregistry (lua_State *L) { lua_pushvalue(L, LUA_REGISTRYINDEX); return 1; @@ -127,12 +138,16 @@ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { /* ** Calls 'lua_getinfo' and collects all results in a new table. +** L1 needs stack space for an optional input (function) plus +** two optional outputs (function and line table) from function +** 'lua_getinfo'. */ static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnStu"); + checkstack(L, L1, 3); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ @@ -190,6 +205,7 @@ static int db_getlocal (lua_State *L) { int level = (int)luaL_checkinteger(L, arg + 1); if (!lua_getstack(L1, level, &ar)) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); + checkstack(L, L1, 1); name = lua_getlocal(L1, &ar, nvar); if (name) { lua_xmove(L1, L, 1); /* move local value */ @@ -216,6 +232,7 @@ static int db_setlocal (lua_State *L) { return luaL_argerror(L, arg+1, "level out of range"); luaL_checkany(L, arg+3); lua_settop(L, arg+3); + checkstack(L, L1, 1); lua_xmove(L, L1, 1); name = lua_setlocal(L1, &ar, nvar); if (name == NULL) @@ -350,6 +367,7 @@ static int db_sethook (lua_State *L) { lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ } + checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ lua_pushvalue(L, arg + 1); /* value (hook function) */ lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ @@ -370,6 +388,7 @@ static int db_gethook (lua_State *L) { lua_pushliteral(L, "external hook"); else { /* hook table must exist */ lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); lua_rawget(L, -2); /* 1st result = hooktable[L1] */ lua_remove(L, -2); /* remove hook table */ diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index a8cb5606..f76582c5 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.110 2015/01/02 12:52:22 roberto Exp $ +** $Id: ldebug.c,v 2.115 2015/05/22 17:45:56 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -34,6 +34,10 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) +/* Active Lua function (given call info) */ +#define ci_func(ci) (clLvalue((ci)->func)) + + static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); @@ -114,15 +118,15 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { static const char *upvalname (Proto *p, int uv) { - TString *s = check_exp(uv < p->sizeupvalues, p->sp->upvalues[uv].name); + TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); } static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - int nparams = clLvalue(ci->func)->p->sp->numparams; - if (n >= ci->u.l.base - ci->func - nparams) + int nparams = clLvalue(ci->func)->p->numparams; + if (n >= cast_int(ci->u.l.base - ci->func) - nparams) return NULL; /* no such vararg */ else { *pos = ci->func + nparams + n; @@ -168,7 +172,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); } else { /* active function; get information through 'ar' */ - StkId pos = 0; /* to avoid warnings */ + StkId pos = NULL; /* to avoid warnings */ name = findlocal(L, ar->i_ci, n, &pos); if (name) { setobj2s(L, L->top, pos); @@ -182,7 +186,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - StkId pos = 0; /* to avoid warnings */ + StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); swapextra(L); @@ -205,7 +209,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { ar->what = "C"; } else { - SharedProto *p = cl->l.p->sp; + Proto *p = cl->l.p; ar->source = p->source ? getstr(p->source) : "=?"; ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; @@ -223,12 +227,12 @@ static void collectvalidlines (lua_State *L, Closure *f) { else { int i; TValue v; - int *lineinfo = f->l.p->sp->lineinfo; + int *lineinfo = f->l.p->lineinfo; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue(L, L->top, t); /* push it on stack */ api_incr_top(L); setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < f->l.p->sp->sizelineinfo; i++) /* for all lines with code */ + for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ } } @@ -254,8 +258,8 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, ar->nparams = 0; } else { - ar->isvararg = f->l.p->sp->is_vararg; - ar->nparams = f->l.p->sp->numparams; + ar->isvararg = f->l.p->is_vararg; + ar->nparams = f->l.p->numparams; } break; } @@ -295,7 +299,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { if (*what == '>') { ci = NULL; func = L->top - 1; - api_check(ttisfunction(func), "function expected"); + api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ L->top--; /* pop function */ } @@ -366,7 +370,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) { int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->sp->code[pc]; + Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); int a = GETARG_A(i); switch (op) { @@ -416,7 +420,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, /* else try symbolic execution */ pc = findsetreg(p, lastpc, reg); if (pc != -1) { /* could find instruction? */ - Instruction i = p->sp->code[pc]; + Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); switch (op) { case OP_MOVE: { @@ -442,7 +446,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, case OP_LOADK: case OP_LOADKX: { int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->sp->code[pc + 1]); + : GETARG_Ax(p->code[pc + 1]); if (ttisstring(&p->k[b])) { *name = svalue(&p->k[b]); return "constant"; @@ -465,7 +469,7 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { TMS tm = (TMS)0; /* to avoid warnings */ Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ - Instruction i = p->sp->code[pc]; /* calling instruction */ + Instruction i = p->code[pc]; /* calling instruction */ if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; @@ -595,19 +599,16 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { } -static void addinfo (lua_State *L, const char *msg) { - CallInfo *ci = L->ci; - if (isLua(ci)) { /* is Lua code? */ - char buff[LUA_IDSIZE]; /* add file:line information */ - int line = currentline(ci); - TString *src = ci_func(ci)->p->sp->source; - if (src) - luaO_chunkid(buff, getstr(src), LUA_IDSIZE); - else { /* no source available; use "?" instead */ - buff[0] = '?'; buff[1] = '\0'; - } - luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); +/* add src:line information to 'msg' */ +const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, + int line) { + char buff[LUA_IDSIZE]; + if (src) + luaO_chunkid(buff, getstr(src), LUA_IDSIZE); + else { /* no source available; use "?" instead */ + buff[0] = '?'; buff[1] = '\0'; } + return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); } @@ -624,10 +625,14 @@ l_noret luaG_errormsg (lua_State *L) { l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { + CallInfo *ci = L->ci; + const char *msg; va_list argp; va_start(argp, fmt); - addinfo(L, luaO_pushvfstring(L, fmt, argp)); + msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); + if (isLua(ci)) /* if Lua function, add source:line information */ + luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); luaG_errormsg(L); } diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 916ce308..0e31546b 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.12 2014/11/10 14:46:05 roberto Exp $ +** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -11,15 +11,12 @@ #include "lstate.h" -#define pcRel(pc, p) (cast(int, (pc) - (p)->sp->code) - 1) +#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) -#define getfuncline(f,pc) (((f)->sp->lineinfo) ? (f)->sp->lineinfo[pc] : -1) +#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) #define resethookcount(L) (L->hookcount = L->basehookcount) -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue((ci)->func)) - LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); @@ -33,6 +30,8 @@ LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); +LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, + TString *src, int line); LUAI_FUNC l_noret luaG_errormsg (lua_State *L); LUAI_FUNC void luaG_traceexec (lua_State *L); diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 227a8b82..5c93a259 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.135 2014/11/11 17:13:39 roberto Exp $ +** $Id: ldo.c,v 2.138 2015/05/22 17:48:19 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -269,7 +269,7 @@ static void callhook (lua_State *L, CallInfo *ci) { } -static StkId adjust_varargs (lua_State *L, SharedProto *p, int actual) { +static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { int i; int nfixargs = p->numparams; StkId base, fixed; @@ -323,6 +323,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { case LUA_TCCL: { /* C closure */ f = clCvalue(func)->f; Cfunc: + luaC_checkGC(L); /* stack grow uses memory */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; @@ -330,20 +331,20 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { ci->top = L->top + LUA_MINSTACK; lua_assert(ci->top <= L->stack_last); ci->callstatus = 0; - luaC_checkGC(L); /* stack grow uses memory */ if (L->hookmask & LUA_MASKCALL) luaD_hook(L, LUA_HOOKCALL, -1); lua_unlock(L); n = (*f)(L); /* do the actual call */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, L->top - n); + luaD_poscall(L, L->top - n, n); return 1; } case LUA_TLCL: { /* Lua function: prepare its call */ StkId base; - SharedProto *p = clLvalue(func)->p->sp; + Proto *p = clLvalue(func)->p; n = cast_int(L->top - func) - 1; /* number of real arguments */ + luaC_checkGC(L); /* stack grow uses memory */ luaD_checkstack(L, p->maxstacksize); for (; n < p->numparams; n++) setnilvalue(L->top++); /* complete missing arguments */ @@ -364,7 +365,6 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = CIST_LUA; L->top = ci->top; - luaC_checkGC(L); /* stack grow uses memory */ if (L->hookmask & LUA_MASKCALL) callhook(L, ci); return 0; @@ -379,7 +379,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { } -int luaD_poscall (lua_State *L, StkId firstResult) { +int luaD_poscall (lua_State *L, StkId firstResult, int nres) { StkId res; int wanted, i; CallInfo *ci = L->ci; @@ -393,9 +393,9 @@ int luaD_poscall (lua_State *L, StkId firstResult) { } res = ci->func; /* res == final position of 1st result */ wanted = ci->nresults; - L->ci = ci = ci->previous; /* back to caller */ + L->ci = ci->previous; /* back to caller */ /* move results to correct place */ - for (i = wanted; i != 0 && firstResult < L->top; i--) + for (i = wanted; i != 0 && nres-- > 0; i--) setobjs2s(L, res++, firstResult++); while (i-- > 0) setnilvalue(res++); @@ -449,7 +449,7 @@ static void finishCcall (lua_State *L, int status) { lua_lock(L); api_checknelems(L, n); /* finish 'luaD_precall' */ - luaD_poscall(L, L->top - n); + luaD_poscall(L, L->top - n, n); } @@ -533,7 +533,8 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { */ static void resume (lua_State *L, void *ud) { int nCcalls = L->nCcalls; - StkId firstArg = cast(StkId, ud); + int n = *(cast(int*, ud)); /* number of arguments */ + StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (nCcalls >= LUAI_MAXCCALLS) resume_error(L, "C stack overflow", firstArg); @@ -553,14 +554,13 @@ static void resume (lua_State *L, void *ud) { luaV_execute(L); /* just continue running Lua code */ else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ - int n; lua_unlock(L); n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ } - luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ + luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */ } unroll(L, NULL); /* run continuation */ } @@ -576,7 +576,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { L->nCcalls = (from) ? from->nCcalls + 1 : 1; L->nny = 0; /* allow yields */ api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); - status = luaD_rawrunprotected(L, resume, L->top - nargs); + status = luaD_rawrunprotected(L, resume, &nargs); if (status == -1) /* error calling 'lua_resume'? */ status = LUA_ERRRUN; else { /* continue running after recoverable errors */ @@ -619,7 +619,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, L->status = LUA_YIELD; ci->extra = savestack(L, ci->func); /* save current 'func' */ if (isLua(ci)) { /* inside a hook? */ - api_check(k == NULL, "hooks cannot continue after yielding"); + api_check(L, k == NULL, "hooks cannot continue after yielding"); } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 05745c8a..edade657 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.21 2014/10/25 11:50:46 roberto Exp $ +** $Id: ldo.h,v 2.22 2015/05/22 17:48:19 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -34,7 +34,7 @@ LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, int allowyield); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); +LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_growstack (lua_State *L, int n); LUAI_FUNC void luaD_shrinkstack (lua_State *L); diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index c4efac16..4c04812a 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.34 2014/11/02 19:19:04 roberto Exp $ +** $Id: ldump.c,v 2.36 2015/03/30 15:43:51 roberto Exp $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -74,19 +74,20 @@ static void DumpString (const TString *s, DumpState *D) { if (s == NULL) DumpByte(0, D); else { - size_t size = s->len + 1; /* include trailing '\0' */ + size_t size = tsslen(s) + 1; /* include trailing '\0' */ + const char *str = getstr(s); if (size < 0xFF) DumpByte(cast_int(size), D); else { DumpByte(0xFF, D); DumpVar(size, D); } - DumpVector(getstr(s), size - 1, D); /* no need to save '\0' */ + DumpVector(str, size - 1, D); /* no need to save '\0' */ } } -static void DumpCode (const SharedProto *f, DumpState *D) { +static void DumpCode (const Proto *f, DumpState *D) { DumpInt(f->sizecode, D); DumpVector(f->code, f->sizecode, D); } @@ -96,7 +97,7 @@ static void DumpFunction(const Proto *f, TString *psource, DumpState *D); static void DumpConstants (const Proto *f, DumpState *D) { int i; - int n = f->sp->sizek; + int n = f->sizek; DumpInt(n, D); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; @@ -126,14 +127,14 @@ static void DumpConstants (const Proto *f, DumpState *D) { static void DumpProtos (const Proto *f, DumpState *D) { int i; - int n = f->sp->sizep; + int n = f->sizep; DumpInt(n, D); for (i = 0; i < n; i++) - DumpFunction(f->p[i], f->sp->source, D); + DumpFunction(f->p[i], f->source, D); } -static void DumpUpvalues (const SharedProto *f, DumpState *D) { +static void DumpUpvalues (const Proto *f, DumpState *D) { int i, n = f->sizeupvalues; DumpInt(n, D); for (i = 0; i < n; i++) { @@ -143,7 +144,7 @@ static void DumpUpvalues (const SharedProto *f, DumpState *D) { } -static void DumpDebug (const SharedProto *f, DumpState *D) { +static void DumpDebug (const Proto *f, DumpState *D) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; DumpInt(n, D); @@ -162,8 +163,7 @@ static void DumpDebug (const SharedProto *f, DumpState *D) { } -static void DumpFunction (const Proto *fp, TString *psource, DumpState *D) { - const SharedProto *f = fp->sp; +static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { if (D->strip || f->source == psource) DumpString(NULL, D); /* no debug info or same source as its parent */ else @@ -174,9 +174,9 @@ static void DumpFunction (const Proto *fp, TString *psource, DumpState *D) { DumpByte(f->is_vararg, D); DumpByte(f->maxstacksize, D); DumpCode(f, D); - DumpConstants(fp, D); + DumpConstants(f, D); DumpUpvalues(f, D); - DumpProtos(fp, D); + DumpProtos(f, D); DumpDebug(f, D); } @@ -208,7 +208,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, D.strip = strip; D.status = 0; DumpHeader(&D); - DumpByte(f->sp->sizeupvalues, &D); + DumpByte(f->sizeupvalues, &D); DumpFunction(f, NULL, &D); return D.status; } diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index c1f2daf7..67967dab 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -96,52 +96,39 @@ void luaF_close (lua_State *L, StkId level) { } -Proto *luaF_newproto (lua_State *L, SharedProto *sp) { +Proto *luaF_newproto (lua_State *L) { GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); Proto *f = gco2p(o); - f->sp = NULL; f->k = NULL; + f->sizek = 0; f->p = NULL; + f->sizep = 0; + f->code = NULL; f->cache = NULL; - if (sp == NULL) { - sp = luaM_new(L, SharedProto); - sp->l_G = G(L); - sp->sizek = 0; - sp->sizep = 0; - sp->code = NULL; - sp->sizecode = 0; - sp->lineinfo = NULL; - sp->sizelineinfo = 0; - sp->upvalues = NULL; - sp->sizeupvalues = 0; - sp->numparams = 0; - sp->is_vararg = 0; - sp->maxstacksize = 0; - sp->locvars = NULL; - sp->sizelocvars = 0; - sp->linedefined = 0; - sp->lastlinedefined = 0; - sp->source = NULL; - } - f->sp = sp; + f->sizecode = 0; + f->lineinfo = NULL; + f->sizelineinfo = 0; + f->upvalues = NULL; + f->sizeupvalues = 0; + f->numparams = 0; + f->is_vararg = 0; + f->maxstacksize = 0; + f->locvars = NULL; + f->sizelocvars = 0; + f->linedefined = 0; + f->lastlinedefined = 0; + f->source = NULL; return f; } -static void freesharedproto (lua_State *L, SharedProto *f) { - if (f == NULL || G(L) != f->l_G) - return; - luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); - luaM_freearray(L, f->locvars, f->sizelocvars); - luaM_freearray(L, f->upvalues, f->sizeupvalues); - luaM_free(L, f); -} - void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->p, f->sp->sizep); - luaM_freearray(L, f->k, f->sp->sizek); - freesharedproto(L, f->sp); + luaM_freearray(L, f->code, f->sizecode); + luaM_freearray(L, f->p, f->sizep); + luaM_freearray(L, f->k, f->sizek); + luaM_freearray(L, f->lineinfo, f->sizelineinfo); + luaM_freearray(L, f->locvars, f->sizelocvars); + luaM_freearray(L, f->upvalues, f->sizeupvalues); luaM_free(L, f); } @@ -150,9 +137,8 @@ void luaF_freeproto (lua_State *L, Proto *f) { ** Look for n-th local variable at line 'line' in function 'func'. ** Returns NULL if not found. */ -const char *luaF_getlocalname (const Proto *fp, int local_number, int pc) { +const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { int i; - const SharedProto *f = fp->sp; for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { if (pc < f->locvars[i].endpc) { /* is variable active? */ local_number--; diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 9d02a4b3..2eeb0d5a 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 roberto Exp $ +** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -22,6 +22,13 @@ #define isintwups(L) (L->twups != L) +/* +** maximum number of upvalues in a closure (both C and Lua). (Value +** must fit in a VM register.) +*/ +#define MAXUPVAL 255 + + /* ** Upvalues for Lua closures */ @@ -40,7 +47,7 @@ struct UpVal { #define upisopen(up) ((up)->v != &(up)->u.value) -LUAI_FUNC Proto *luaF_newproto (lua_State *L, SharedProto *sp); +LUAI_FUNC Proto *luaF_newproto (lua_State *L); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 0be4866a..973c269f 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.201 2014/12/20 13:58:15 roberto Exp $ +** $Id: lgc.c,v 2.205 2015/03/25 13:42:19 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -83,8 +83,13 @@ #define markvalue(g,o) { checkconsistency(o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } -#define markobject(g,t) \ - { if ((t) && iswhite(t)) reallymarkobject(g, obj2gco(t)); } +#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); } + +/* +** mark an object that can be NULL (either because it is really optional, +** or it was stripped as debug info, or inside an uncompleted structure) +*/ +#define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); @@ -226,15 +231,19 @@ static void reallymarkobject (global_State *g, GCObject *o) { reentry: white2gray(o); switch (o->tt) { - case LUA_TSHRSTR: + case LUA_TSHRSTR: { + gray2black(o); + g->GCmemtrav += sizelstring(gco2ts(o)->shrlen); + break; + } case LUA_TLNGSTR: { gray2black(o); - g->GCmemtrav += sizestring(gco2ts(o)); + g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); break; } case LUA_TUSERDATA: { TValue uvalue; - markobject(g, gco2u(o)->metatable); /* mark its metatable */ + markobjectN(g, gco2u(o)->metatable); /* mark its metatable */ gray2black(o); g->GCmemtrav += sizeudata(gco2u(o)); getuservalue(g->mainthread, gco2u(o), &uvalue); @@ -275,7 +284,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { static void markmt (global_State *g) { int i; for (i=0; i < LUA_NUMTAGS; i++) - markobject(g, g->mt[i]); + markobjectN(g, g->mt[i]); } @@ -437,7 +446,7 @@ static void traversestrongtable (global_State *g, Table *h) { static lu_mem traversetable (global_State *g, Table *h) { const char *weakkey, *weakvalue; const TValue *mode = gfasttm(g, h->metatable, TM_MODE); - markobject(g, h->metatable); + markobjectN(g, h->metatable); if (mode && ttisstring(mode) && /* is there a weak mode? */ ((weakkey = strchr(svalue(mode), 'k')), (weakvalue = strchr(svalue(mode), 'v')), @@ -456,32 +465,31 @@ static lu_mem traversetable (global_State *g, Table *h) { sizeof(Node) * cast(size_t, sizenode(h)); } -static int marksharedproto (global_State *g, SharedProto *f) { - int i; - if (g != f->l_G) - return 0; - markobject(g, f->source); - for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobject(g, f->upvalues[i].name); - for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobject(g, f->locvars[i].varname); - return sizeof(Instruction) * f->sizecode + - sizeof(int) * f->sizelineinfo + - sizeof(LocVar) * f->sizelocvars + - sizeof(Upvaldesc) * f->sizeupvalues; -} +/* +** Traverse a prototype. (While a prototype is being build, its +** arrays can be larger than needed; the extra slots are filled with +** NULL, so the use of 'markobjectN') +*/ static int traverseproto (global_State *g, Proto *f) { int i; if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */ - for (i = 0; i < f->sp->sizek; i++) /* mark literals */ + markobjectN(g, f->source); + for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); - for (i = 0; i < f->sp->sizep; i++) /* mark nested protos */ - markobject(g, f->p[i]); - return sizeof(Proto) + sizeof(Proto *) * f->sp->sizep + - sizeof(TValue) * f->sp->sizek + - marksharedproto(g, f->sp); + for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ + markobjectN(g, f->upvalues[i].name); + for (i = 0; i < f->sizep; i++) /* mark nested protos */ + markobjectN(g, f->p[i]); + for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ + markobjectN(g, f->locvars[i].varname); + return sizeof(Proto) + sizeof(Instruction) * f->sizecode + + sizeof(Proto *) * f->sizep + + sizeof(TValue) * f->sizek + + sizeof(int) * f->sizelineinfo + + sizeof(LocVar) * f->sizelocvars + + sizeof(Upvaldesc) * f->sizeupvalues; } @@ -500,7 +508,7 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) { */ static lu_mem traverseLclosure (global_State *g, LClosure *cl) { int i; - markobject(g, cl->p); /* mark its prototype */ + markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ UpVal *uv = cl->upvals[i]; if (uv != NULL) { @@ -695,9 +703,10 @@ static void freeobj (lua_State *L, GCObject *o) { case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; case LUA_TSHRSTR: luaS_remove(L, gco2ts(o)); /* remove it from hash table */ - /* go through */ + luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); + break; case LUA_TLNGSTR: { - luaM_freemem(L, o, sizestring(gco2ts(o))); + luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); break; } default: lua_assert(0); @@ -1008,6 +1017,7 @@ static l_mem atomic (lua_State *L) { /* clear values from resurrected weak tables */ clearvalues(g, g->weak, origweak); clearvalues(g, g->allweak, origall); + luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ work += g->GCmemtrav; /* complete counting */ return work; /* estimate of memory marked by 'atomic' */ diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index 4dea3968..193cac67 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.142 2015/01/02 12:50:28 roberto Exp $ +** $Id: liolib.c,v 2.144 2015/04/03 18:41:57 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -410,12 +410,6 @@ static int readdigits (RN *rn, int hex) { } -/* access to locale "radix character" (decimal point) */ -#if !defined(l_getlocaledecpoint) -#define l_getlocaledecpoint() (localeconv()->decimal_point[0]) -#endif - - /* ** Read a number: first reads a valid prefix of a numeral into a buffer. ** Then it calls 'lua_stringtonumber' to check whether the format is @@ -425,9 +419,10 @@ static int read_number (lua_State *L, FILE *f) { RN rn; int count = 0; int hex = 0; - char decp[2] = "."; + char decp[2]; rn.f = f; rn.n = 0; - decp[0] = l_getlocaledecpoint(); /* get decimal point from locale */ + decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ + decp[1] = '\0'; l_lockfile(rn.f); do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ test2(&rn, "-+"); /* optional signal */ @@ -457,7 +452,7 @@ static int read_number (lua_State *L, FILE *f) { static int test_eof (lua_State *L, FILE *f) { int c = getc(f); ungetc(c, f); /* no-op when c == EOF */ - lua_pushlstring(L, NULL, 0); + lua_pushliteral(L, ""); return (c != EOF); } diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index 6e4a457a..c35bd55f 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.89 2014/11/14 16:06:09 roberto Exp $ +** $Id: llex.c,v 2.93 2015/05/22 17:45:56 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -16,6 +16,7 @@ #include "lua.h" #include "lctype.h" +#include "ldebug.h" #include "ldo.h" #include "lgc.h" #include "llex.h" @@ -68,7 +69,7 @@ static void save (LexState *ls, int c) { void luaX_init (lua_State *L) { int i; - TString *e = luaS_new(L, LUA_ENV); /* create env name */ + TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */ luaC_fix(L, obj2gco(e)); /* never collect this name */ for (i=0; isource), LUA_IDSIZE); - msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); + msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber); if (token) luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); luaD_throw(ls->L, LUA_ERRSYNTAX); @@ -172,7 +171,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, ls->linenumber = 1; ls->lastline = 1; ls->source = source; - ls->envn = luaS_new(L, LUA_ENV); /* get env name */ + ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } @@ -221,11 +220,6 @@ static void buffreplace (LexState *ls, char from, char to) { } -#if !defined(l_getlocaledecpoint) -#define l_getlocaledecpoint() (localeconv()->decimal_point[0]) -#endif - - #define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0) /* @@ -234,7 +228,7 @@ static void buffreplace (LexState *ls, char from, char to) { */ static void trydecpoint (LexState *ls, TValue *o) { char old = ls->decpoint; - ls->decpoint = l_getlocaledecpoint(); + ls->decpoint = lua_getlocaledecpoint(); buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ if (!buff2num(ls->buff, o)) { /* format error with correct decimal point: no more options */ @@ -283,8 +277,9 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** skip a sequence '[=*[' or ']=*]' and return its number of '='s or -** -1 if sequence is malformed +** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return +** its number of '='s; otherwise, return a negative number (-1 iff there +** are no '='s after initial bracket) */ static int skip_sep (LexState *ls) { int count = 0; @@ -501,8 +496,9 @@ static int llex (LexState *ls, SemInfo *seminfo) { read_long_string(ls, seminfo, sep); return TK_STRING; } - else if (sep == -1) return '['; - else lexerror(ls, "invalid long string delimiter", TK_STRING); + else if (sep != -1) /* '[=...' missing second bracket */ + lexerror(ls, "invalid long string delimiter", TK_STRING); + return '['; } case '=': { next(ls); diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 8f71a6ff..277c724d 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.125 2014/12/19 13:30:23 roberto Exp $ +** $Id: llimits.h,v 1.135 2015/06/09 14:21:00 roberto Exp $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -52,11 +52,11 @@ typedef unsigned char lu_byte; /* -** conversion of pointer to integer: +** conversion of pointer to unsigned integer: ** this is for hashing only; there is no problem if the integer ** cannot hold the whole pointer value */ -#define point2int(p) ((unsigned int)((size_t)(p) & UINT_MAX)) +#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) @@ -88,22 +88,20 @@ typedef LUAI_UACINT l_uacInt; /* ** assertion for checking API calls */ -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(e) assert(e) -#else -#define luai_apicheck(e) lua_assert(e) +#if !defined(luai_apicheck) +#define luai_apicheck(l,e) lua_assert(e) #endif - -#define api_check(e,msg) luai_apicheck((e) && msg) +#define api_check(l,e,msg) luai_apicheck(l,(e) && msg) +/* macro to avoid warnings about unused variables */ #if !defined(UNUSED) -#define UNUSED(x) ((void)(x)) /* to avoid warnings */ +#define UNUSED(x) ((void)(x)) #endif +/* type casts (a macro highlights casts in the code) */ #define cast(t, exp) ((t)(exp)) #define cast_void(i) cast(void, (i)) @@ -149,11 +147,6 @@ typedef LUAI_UACINT l_uacInt; #define LUAI_MAXCCALLS 200 #endif -/* -** maximum number of upvalues in a closure (both C and Lua). (Value -** must fit in an unsigned char.) -*/ -#define MAXUPVAL UCHAR_MAX /* @@ -168,10 +161,33 @@ typedef unsigned long Instruction; +/* +** Maximum length for short strings, that is, strings that are +** internalized. (Cannot be smaller than reserved words or tags for +** metamethods, as these strings must be internalized; +** #("function") = 8, #("__newindex") = 10.) +*/ +#if !defined(LUAI_MAXSHORTLEN) +#define LUAI_MAXSHORTLEN 40 +#endif -/* minimum size for the string table (must be power of 2) */ + +/* +** Initial size for the string table (must be power of 2). +** The Lua core alone registers ~50 strings (reserved words + +** metaevent keys + a few others). Libraries would typically add +** a few dozens more. +*/ #if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */ +#define MINSTRTABSIZE 128 +#endif + + +/* +** Size of cache for strings in the API (better be a prime) +*/ +#if !defined(STRCACHE_SIZE) +#define STRCACHE_SIZE 127 #endif @@ -181,11 +197,19 @@ typedef unsigned long Instruction; #endif +/* +** macros that are executed whenether program enters the Lua core +** ('lua_lock') and leaves the core ('lua_unlock') +*/ #if !defined(lua_lock) #define lua_lock(L) ((void) 0) #define lua_unlock(L) ((void) 0) #endif +/* +** macro executed during Lua functions at points where the +** function can yield. +*/ #if !defined(luai_threadyield) #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} #endif @@ -222,6 +246,53 @@ typedef unsigned long Instruction; +/* +** The luai_num* macros define the primitive operations over numbers. +*/ + +/* floor division (defined as 'floor(a/b)') */ +#if !defined(luai_numidiv) +#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) +#endif + +/* float division */ +#if !defined(luai_numdiv) +#define luai_numdiv(L,a,b) ((a)/(b)) +#endif + +/* +** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when +** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of +** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b) +** ~= floor(a/b)'. That happens when the division has a non-integer +** negative result, which is equivalent to the test below. +*/ +#if !defined(luai_nummod) +#define luai_nummod(L,a,b,m) \ + { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } +#endif + +/* exponentiation */ +#if !defined(luai_numpow) +#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) +#endif + +/* the others are quite standard operations */ +#if !defined(luai_numadd) +#define luai_numadd(L,a,b) ((a)+(b)) +#define luai_numsub(L,a,b) ((a)-(b)) +#define luai_nummul(L,a,b) ((a)*(b)) +#define luai_numunm(L,a) (-(a)) +#define luai_numeq(a,b) ((a)==(b)) +#define luai_numlt(a,b) ((a)<(b)) +#define luai_numle(a,b) ((a)<=(b)) +#define luai_numisnan(a) (!luai_numeq((a), (a))) +#endif + + + + + /* ** macro to control inclusion of some hard tests on stack reallocation */ diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index 002c508b..4f2ec60a 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.114 2014/12/27 20:32:26 roberto Exp $ +** $Id: lmathlib.c,v 1.115 2015/03/12 14:04:04 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -183,6 +183,9 @@ static int math_log (lua_State *L) { res = l_mathop(log)(x); else { lua_Number base = luaL_checknumber(L, 2); +#if !defined(LUA_USE_C89) + if (base == 2.0) res = l_mathop(log2)(x); else +#endif if (base == 10.0) res = l_mathop(log10)(x); else res = l_mathop(log)(x)/l_mathop(log)(base); } diff --git a/3rd/lua/lmem.c b/3rd/lua/lmem.c index 4feaf036..0a0476cc 100644 --- a/3rd/lua/lmem.c +++ b/3rd/lua/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.89 2014/11/02 19:33:33 roberto Exp $ +** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -85,10 +85,11 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { #endif newblock = (*g->frealloc)(g->ud, block, osize, nsize); if (newblock == NULL && nsize > 0) { - api_check( nsize > realosize, - "realloc cannot fail when shrinking a block"); - luaC_fullgc(L, 1); /* try to free some memory... */ - newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ + lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ + if (g->version) { /* is state fully built? */ + luaC_fullgc(L, 1); /* try to free some memory... */ + newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ + } if (newblock == NULL) luaD_throw(L, LUA_ERRMEM); } diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index 7f8d9902..bbf8f67a 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.124 2015/01/05 13:51:39 roberto Exp $ +** $Id: loadlib.c,v 1.126 2015/02/16 13:14:33 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -14,6 +14,7 @@ #include "lprefix.h" +#include #include #include @@ -136,8 +137,8 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); #include /* -** Macro to covert pointer to void* to pointer to function. This cast -** is undefined according to ISO C, but POSIX assumes that it must work. +** Macro to convert pointer-to-void* to pointer-to-function. This cast +** is undefined according to ISO C, but POSIX assumes that it works. ** (The '__extension__' in gnu compilers is only to avoid warnings.) */ #if defined(__GNUC__) diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 6a24aff9..6c53b981 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.101 2014/12/26 14:43:45 roberto Exp $ +** $Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -10,6 +10,8 @@ #include "lprefix.h" +#include +#include #include #include #include @@ -39,8 +41,12 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; int luaO_int2fb (unsigned int x) { int e = 0; /* exponent */ if (x < 8) return x; - while (x >= 0x10) { - x = (x+1) >> 1; + while (x >= (8 << 4)) { /* coarse steps */ + x = (x + 0xf) >> 4; /* x = ceil(x / 16) */ + e += 4; + } + while (x >= (8 << 1)) { /* fine steps */ + x = (x + 1) >> 1; /* x = ceil(x / 2) */ e++; } return ((e+1) << 3) | (cast_int(x) - 8); @@ -55,8 +61,11 @@ int luaO_fb2int (int x) { } +/* +** Computes ceil(log2(x)) +*/ int luaO_ceillog2 (unsigned int x) { - static const lu_byte log_2[256] = { + static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, @@ -149,13 +158,13 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, } /* could not perform raw operation; try metamethod */ lua_assert(L != NULL); /* should not fail when folding (compile time) */ - luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD)); + luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); } int luaO_hexavalue (int c) { if (lisdigit(c)) return c - '0'; - else return ltolower(c) - 'a' + 10; + else return (ltolower(c) - 'a') + 10; } @@ -172,9 +181,8 @@ static int isneg (const char **s) { ** Lua's implementation for 'lua_strx2number' ** =================================================================== */ -#if !defined(lua_strx2number) -#include +#if !defined(lua_strx2number) /* maximum number of significant digits to read (to avoid overflows even with single floats) */ @@ -185,21 +193,22 @@ static int isneg (const char **s) { ** C99 specification for 'strtod' */ static lua_Number lua_strx2number (const char *s, char **endptr) { + int dot = lua_getlocaledecpoint(); lua_Number r = 0.0; /* result (accumulator) */ int sigdig = 0; /* number of significant digits */ int nosigdig = 0; /* number of non-significant digits */ int e = 0; /* exponent correction */ int neg; /* 1 if number is negative */ - int dot = 0; /* true after seen a dot */ + int hasdot = 0; /* true after seen a dot */ *endptr = cast(char *, s); /* nothing is valid yet */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check signal */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ return 0.0; /* invalid format (no '0x') */ for (s += 2; ; s++) { /* skip '0x' and read numeral */ - if (*s == '.') { - if (dot) break; /* second dot? stop loop */ - else dot = 1; + if (*s == dot) { + if (hasdot) break; /* second dot? stop loop */ + else hasdot = 1; } else if (lisxdigit(cast_uchar(*s))) { if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ @@ -207,7 +216,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ r = (r * cast_num(16.0)) + luaO_hexavalue(*s); else e++; /* too many digits; ignore, but still count for exponent */ - if (dot) e--; /* decimal digit? correct exponent */ + if (hasdot) e--; /* decimal digit? correct exponent */ } else break; /* neither a dot nor a digit */ } @@ -244,7 +253,7 @@ static const char *l_str2d (const char *s, lua_Number *result) { *result = lua_strx2number(s, &endptr); else *result = lua_str2number(s, &endptr); - if (endptr == s) return 0; /* nothing recognized */ + if (endptr == s) return NULL; /* nothing recognized */ while (lisspace(cast_uchar(*endptr))) endptr++; return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */ } @@ -290,7 +299,7 @@ size_t luaO_str2num (const char *s, TValue *o) { } else return 0; /* conversion failed */ - return (e - s + 1); /* success; return string size */ + return (e - s) + 1; /* success; return string size */ } @@ -329,7 +338,7 @@ void luaO_tostring (lua_State *L, StkId obj) { len = lua_number2str(buff, fltvalue(obj)); #if !defined(LUA_COMPAT_FLOATSTRING) if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ - buff[len++] = '.'; + buff[len++] = lua_getlocaledecpoint(); buff[len++] = '0'; /* adds '.0' to result */ } #endif diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 01f29139..9230b7a9 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.106 2015/01/05 13:52:37 roberto Exp $ +** $Id: lobject.h,v 2.111 2015/06/09 14:21:42 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -35,8 +35,6 @@ ** bit 6: whether value is collectable */ -#define VARBITS (3 << 4) - /* ** LUA_TFUNCTION variants: @@ -190,9 +188,15 @@ typedef struct lua_TValue TValue; #define setfltvalue(obj,x) \ { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } +#define chgfltvalue(obj,x) \ + { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } + #define setivalue(obj,x) \ { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } +#define chgivalue(obj,x) \ + { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } + #define setnilvalue(obj) settt_(obj, LUA_TNIL) #define setfvalue(obj,x) \ @@ -303,9 +307,12 @@ typedef TValue *StkId; /* index to stack elements */ typedef struct TString { CommonHeader; lu_byte extra; /* reserved words for short strings; "has hash" for longs */ + lu_byte shrlen; /* length for short strings */ unsigned int hash; - size_t len; /* number of characters in string */ - struct TString *hnext; /* linked list for hash table */ + union { + size_t lnglen; /* length for long strings */ + struct TString *hnext; /* linked list for hash table */ + } u; } TString; @@ -329,6 +336,12 @@ typedef union UTString { /* get the actual string (array of bytes) from a Lua value */ #define svalue(o) getstr(tsvalue(o)) +/* get string length from 'TString *s' */ +#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen) + +/* get string length from 'TValue *o' */ +#define vslen(o) tsslen(tsvalue(o)) + /* ** Header for userdata; memory area follows the end of this structure @@ -361,13 +374,13 @@ typedef union UUdata { #define setuservalue(L,u,o) \ { const TValue *io=(o); Udata *iu = (u); \ - iu->user_ = io->value_; iu->ttuv_ = io->tt_; \ + iu->user_ = io->value_; iu->ttuv_ = rttype(io); \ checkliveness(G(L),io); } #define getuservalue(L,u,o) \ { TValue *io=(o); const Udata *iu = (u); \ - io->value_ = iu->user_; io->tt_ = iu->ttuv_; \ + io->value_ = iu->user_; settt_(io, iu->ttuv_); \ checkliveness(G(L),io); } @@ -376,7 +389,7 @@ typedef union UUdata { */ typedef struct Upvaldesc { TString *name; /* upvalue name (for debug information) */ - lu_byte instack; /* whether it is in stack */ + lu_byte instack; /* whether it is in stack (register) */ lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ } Upvaldesc; @@ -392,10 +405,14 @@ typedef struct LocVar { } LocVar; -typedef struct SharedProto { +/* +** Function Prototypes +*/ +typedef struct Proto { + CommonHeader; lu_byte numparams; /* number of fixed parameters */ lu_byte is_vararg; - lu_byte maxstacksize; /* maximum stack used by this function */ + lu_byte maxstacksize; /* number of registers needed by this function */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ int sizecode; @@ -404,23 +421,14 @@ typedef struct SharedProto { int sizelocvars; int linedefined; int lastlinedefined; - void *l_G; /* global state belongs to */ - Instruction *code; + TValue *k; /* constants used by the function */ + Instruction *code; /* opcodes */ + struct Proto **p; /* functions defined inside the function */ int *lineinfo; /* map from opcodes to source lines (debug information) */ LocVar *locvars; /* information about local variables (debug information) */ Upvaldesc *upvalues; /* upvalue information */ + struct LClosure *cache; /* last-created closure with this prototype */ TString *source; /* used for debug information */ -} SharedProto; - -/* -** Function Prototypes -*/ -typedef struct Proto { - CommonHeader; - struct SharedProto *sp; - TValue *k; /* constants used by the function */ - struct Proto **p; /* functions defined inside the function */ - struct LClosure *cache; /* last created closure with this prototype */ GCObject *gclist; } Proto; diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 20359b24..cb8a3c33 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.54 2014/12/26 14:46:07 roberto Exp $ +** $Id: loslib.c,v 1.57 2015/04/10 17:41:04 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -22,10 +22,12 @@ #include "lualib.h" -#if !defined(LUA_STRFTIMEOPTIONS) /* { */ /* +** {================================================================== ** list of valid conversion specifiers for the 'strftime' function +** =================================================================== */ +#if !defined(LUA_STRFTIMEOPTIONS) /* { */ #if defined(LUA_USE_C89) #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } @@ -37,8 +39,14 @@ #endif #endif /* } */ +/* }================================================================== */ +/* +** {================================================================== +** Configuration for time-related stuff +** =================================================================== +*/ #if !defined(l_time_t) /* { */ /* @@ -51,15 +59,41 @@ #endif /* } */ - -#if !defined(lua_tmpnam) /* { */ +#if !defined(l_gmtime) /* { */ /* -** By default, Lua uses tmpnam except when POSIX is available, where it -** uses mkstemp. +** By default, Lua uses gmtime/localtime, except when POSIX is available, +** where it uses gmtime_r/localtime_r */ #if defined(LUA_USE_POSIX) /* { */ +#define l_gmtime(t,r) gmtime_r(t,r) +#define l_localtime(t,r) localtime_r(t,r) + +#else /* }{ */ + +/* ISO C definitions */ +#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) +#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) + +#endif /* } */ + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Configuration for 'tmpnam': +** By default, Lua uses tmpnam except when POSIX is available, where +** it uses mkstemp. +** =================================================================== +*/ +#if !defined(lua_tmpnam) /* { */ + +#if defined(LUA_USE_POSIX) /* { */ + #include #define LUA_TMPNAMBUFSIZE 32 @@ -83,31 +117,10 @@ #endif /* } */ #endif /* } */ +/* }================================================================== */ -#if !defined(l_gmtime) /* { */ -/* -** By default, Lua uses gmtime/localtime, except when POSIX is available, -** where it uses gmtime_r/localtime_r -*/ - -#if defined(LUA_USE_POSIX) /* { */ - -#define l_gmtime(t,r) gmtime_r(t,r) -#define l_localtime(t,r) localtime_r(t,r) - -#else /* }{ */ - -/* ISO C definitions */ -#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) -#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) - -#endif /* } */ - -#endif /* } */ - - static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); @@ -287,7 +300,7 @@ static int os_time (lua_State *L) { t = mktime(&ts); } if (t != (time_t)(l_timet)t) - luaL_error(L, "time result cannot be represented in this Lua instalation"); + luaL_error(L, "time result cannot be represented in this Lua installation"); else if (t == (time_t)(-1)) lua_pushnil(L); else @@ -297,8 +310,9 @@ static int os_time (lua_State *L) { static int os_difftime (lua_State *L) { - double res = difftime((l_checktime(L, 1)), (l_checktime(L, 2))); - lua_pushnumber(L, (lua_Number)res); + time_t t1 = l_checktime(L, 1); + time_t t2 = l_checktime(L, 2); + lua_pushnumber(L, (lua_Number)difftime(t1, t2)); return 1; } diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 0bed0dd3..9a54dfc9 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -79,7 +79,7 @@ static l_noret error_expected (LexState *ls, int token) { static l_noret errorlimit (FuncState *fs, int limit, const char *what) { lua_State *L = fs->ls->L; const char *msg; - int line = fs->f->sp->linedefined; + int line = fs->f->linedefined; const char *where = (line == 0) ? "main function" : luaO_pushfstring(L, "function at line %d", line); @@ -160,14 +160,13 @@ static void checkname (LexState *ls, expdesc *e) { static int registerlocalvar (LexState *ls, TString *varname) { FuncState *fs = ls->fs; - Proto *fp = fs->f; - SharedProto *f = fp->sp; + Proto *f = fs->f; int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, fp, varname); + luaC_objbarrier(ls->L, f, varname); return fs->nlocvars++; } @@ -195,7 +194,7 @@ static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { static LocVar *getlocvar (FuncState *fs, int i) { int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; lua_assert(idx < fs->nlocvars); - return &fs->f->sp->locvars[idx]; + return &fs->f->locvars[idx]; } @@ -217,7 +216,7 @@ static void removevars (FuncState *fs, int tolevel) { static int searchupvalue (FuncState *fs, TString *name) { int i; - Upvaldesc *up = fs->f->sp->upvalues; + Upvaldesc *up = fs->f->upvalues; for (i = 0; i < fs->nups; i++) { if (eqstr(up[i].name, name)) return i; } @@ -226,8 +225,7 @@ static int searchupvalue (FuncState *fs, TString *name) { static int newupvalue (FuncState *fs, TString *name, expdesc *v) { - Proto *fp = fs->f; - SharedProto *f = fp->sp; + Proto *f = fs->f; int oldsize = f->sizeupvalues; checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, @@ -236,7 +234,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { f->upvalues[fs->nups].instack = (v->k == VLOCAL); f->upvalues[fs->nups].idx = cast_byte(v->u.info); f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, fp, name); + luaC_objbarrier(fs->ls->L, f, name); return fs->nups++; } @@ -498,12 +496,12 @@ static Proto *addprototype (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; /* prototype of current function */ - if (fs->np >= f->sp->sizep) { - int oldsize = f->sp->sizep; - luaM_growvector(L, f->p, fs->np, f->sp->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sp->sizep) f->p[oldsize++] = NULL; + if (fs->np >= f->sizep) { + int oldsize = f->sizep; + luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); + while (oldsize < f->sizep) f->p[oldsize++] = NULL; } - f->p[fs->np++] = clp = luaF_newproto(L, NULL); + f->p[fs->np++] = clp = luaF_newproto(L); luaC_objbarrier(L, f, clp); return clp; } @@ -523,7 +521,7 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - SharedProto *f; + Proto *f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; ls->fs = fs; @@ -538,7 +536,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nactvar = 0; fs->firstlocal = ls->dyd->actvar.n; fs->bl = NULL; - f = fs->f->sp; + f = fs->f; f->source = ls->source; f->maxstacksize = 2; /* registers 0/1 are always valid */ enterblock(fs, bl, 0); @@ -549,21 +547,20 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; - SharedProto *sp = f->sp; luaK_ret(fs, 0, 0); /* final return */ leaveblock(fs); - luaM_reallocvector(L, sp->code, sp->sizecode, fs->pc, Instruction); - sp->sizecode = fs->pc; - luaM_reallocvector(L, sp->lineinfo, sp->sizelineinfo, fs->pc, int); - sp->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->k, sp->sizek, fs->nk, TValue); - sp->sizek = fs->nk; - luaM_reallocvector(L, f->p, sp->sizep, fs->np, Proto *); - sp->sizep = fs->np; - luaM_reallocvector(L, sp->locvars, sp->sizelocvars, fs->nlocvars, LocVar); - sp->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, sp->upvalues, sp->sizeupvalues, fs->nups, Upvaldesc); - sp->sizeupvalues = fs->nups; + luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); + f->sizecode = fs->pc; + luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); + f->sizelineinfo = fs->pc; + luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); + f->sizek = fs->nk; + luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); + f->sizep = fs->np; + luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); + f->sizelocvars = fs->nlocvars; + luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); + f->sizeupvalues = fs->nups; lua_assert(fs->bl == NULL); ls->fs = fs->prev; luaC_checkGC(L); @@ -739,8 +736,8 @@ static void constructor (LexState *ls, expdesc *t) { } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, '}', '{', line); lastlistfield(fs, &cc); - SETARG_B(fs->f->sp->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->sp->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ + SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ + SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ } /* }====================================================================== */ @@ -750,7 +747,7 @@ static void constructor (LexState *ls, expdesc *t) { static void parlist (LexState *ls) { /* parlist -> [ param { ',' param } ] */ FuncState *fs = ls->fs; - SharedProto *f = fs->f->sp; + Proto *f = fs->f; int nparams = 0; f->is_vararg = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ @@ -781,7 +778,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { FuncState new_fs; BlockCnt bl; new_fs.f = addprototype(ls); - new_fs.f->sp->linedefined = line; + new_fs.f->linedefined = line; open_func(ls, &new_fs, &bl); checknext(ls, '('); if (ismethod) { @@ -791,7 +788,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { parlist(ls); checknext(ls, ')'); statlist(ls); - new_fs.f->sp->lastlinedefined = ls->linenumber; + new_fs.f->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); codeclosure(ls, e); close_func(ls); @@ -957,7 +954,7 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - check_condition(ls, fs->f->sp->is_vararg, + check_condition(ls, fs->f->is_vararg, "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); break; @@ -1613,7 +1610,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->sp->is_vararg = 1; /* main function is always vararg */ + fs->f->is_vararg = 1; /* main function is always vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ @@ -1633,13 +1630,13 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, lexstate.h = luaH_new(L); /* create table for scanner */ sethvalue(L, L->top, lexstate.h); /* anchor it */ incr_top(L); - funcstate.f = cl->p = luaF_newproto(L, NULL); - funcstate.f->sp->source = luaS_new(L, name); /* create and anchor TString */ + funcstate.f = cl->p = luaF_newproto(L); + funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; - luaX_setinput(L, &lexstate, z, funcstate.f->sp->source, firstchar); + luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); mainfunc(&lexstate, &funcstate); lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index ff6b02d3..12e51d24 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.127 2014/11/02 19:33:33 roberto Exp $ +** $Id: lstate.c,v 2.128 2015/03/04 13:31:21 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -37,9 +37,6 @@ #endif -#define MEMERRMSG "not enough memory" - - /* ** a macro to help the creation of a unique random seed when a state is ** created; the seed is used to randomize hashes. @@ -200,12 +197,9 @@ static void f_luaopen (lua_State *L, void *ud) { UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + luaS_init(L); luaT_init(L); luaX_init(L); - /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index cdcdd5a4..eefc217d 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.119 2014/10/30 18:53:28 roberto Exp $ +** $Id: lstate.h,v 2.122 2015/06/01 16:34:37 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -141,6 +141,7 @@ typedef struct global_State { TString *memerrmsg; /* memory-error message */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ + TString *strcache[STRCACHE_SIZE][1]; /* cache for strings in API */ } global_State; diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 2947113c..5e0e3c40 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ +** $Id: lstring.c,v 2.49 2015/06/01 16:34:37 roberto Exp $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -22,6 +22,8 @@ #include "lstring.h" +#define MEMERRMSG "not enough memory" + /* ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to @@ -36,10 +38,10 @@ ** equality for long strings */ int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->len; + size_t len = a->u.lnglen; lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); return (a == b) || /* same instance or... */ - ((len == b->len) && /* equal length and ... */ + ((len == b->u.lnglen) && /* equal length and ... */ (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ } @@ -69,9 +71,9 @@ void luaS_resize (lua_State *L, int newsize) { TString *p = tb->hash[i]; tb->hash[i] = NULL; while (p) { /* for each node in the list */ - TString *hnext = p->hnext; /* save next */ + TString *hnext = p->u.hnext; /* save next */ unsigned int h = lmod(p->hash, newsize); /* new position */ - p->hnext = tb->hash[h]; /* chain it */ + p->u.hnext = tb->hash[h]; /* chain it */ tb->hash[h] = p; p = hnext; } @@ -85,6 +87,34 @@ void luaS_resize (lua_State *L, int newsize) { } +/* +** Clear API string cache. (Entries cannot be empty, so fill them with +** a non-collectable string.) +*/ +void luaS_clearcache (global_State *g) { + int i; + for (i = 0; i < STRCACHE_SIZE; i++) { + if (iswhite(g->strcache[i][0])) /* will entry be collected? */ + g->strcache[i][0] = g->memerrmsg; /* replace it with something fixed */ + } +} + + +/* +** Initialize the string table and the string cache +*/ +void luaS_init (lua_State *L) { + global_State *g = G(L); + int i; + luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + /* pre-create memory-error message */ + g->memerrmsg = luaS_newliteral(L, MEMERRMSG); + luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ + for (i = 0; i < STRCACHE_SIZE; i++) /* fill cache with valid strings */ + g->strcache[i][0] = g->memerrmsg; +} + + /* ** creates a new string object @@ -97,7 +127,6 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, totalsize = sizelstring(l); o = luaC_newobj(L, tag, totalsize); ts = gco2ts(o); - ts->len = l; ts->hash = h; ts->extra = 0; memcpy(getaddrstr(ts), str, l * sizeof(char)); @@ -110,8 +139,8 @@ void luaS_remove (lua_State *L, TString *ts) { stringtable *tb = &G(L)->strt; TString **p = &tb->hash[lmod(ts->hash, tb->size)]; while (*p != ts) /* find previous element */ - p = &(*p)->hnext; - *p = (*p)->hnext; /* remove element from its list */ + p = &(*p)->u.hnext; + *p = (*p)->u.hnext; /* remove element from its list */ tb->nuse--; } @@ -124,8 +153,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { global_State *g = G(L); unsigned int h = luaS_hash(str, l, g->seed); TString **list = &g->strt.hash[lmod(h, g->strt.size)]; - for (ts = *list; ts != NULL; ts = ts->hnext) { - if (l == ts->len && + for (ts = *list; ts != NULL; ts = ts->u.hnext) { + if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ @@ -138,7 +167,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ } ts = createstrobj(L, str, l, LUA_TSHRSTR, h); - ts->hnext = *list; + ts->shrlen = cast_byte(l); + ts->u.hnext = *list; *list = ts; g->strt.nuse++; return ts; @@ -152,18 +182,32 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { if (l <= LUAI_MAXSHORTLEN) /* short string? */ return internshrstr(L, str, l); else { + TString *ts; if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) luaM_toobig(L); - return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); + ts = createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); + ts->u.lnglen = l; + return ts; } } /* -** new zero-terminated string +** Create or reuse a zero-terminated string, first checking in the +** cache (using the string address as a key). The cache can contain +** only zero-terminated strings, so it is safe to use 'strcmp' to +** check hits. */ TString *luaS_new (lua_State *L, const char *str) { - return luaS_newlstr(L, str, strlen(str)); + unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */ + TString **p = G(L)->strcache[i]; + if (strcmp(str, getstr(p[0])) == 0) /* hit? */ + return p[0]; /* that it is */ + else { /* normal route */ + TString *s = luaS_newlstr(L, str, strlen(str)); + p[0] = s; + return s; + } } diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index d3f04caf..e746f5fc 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.56 2014/07/18 14:46:47 roberto Exp $ +** $Id: lstring.h,v 1.59 2015/03/25 13:42:19 roberto Exp $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -13,7 +13,6 @@ #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) -#define sizestring(s) sizelstring((s)->len) #define sizeludata(l) (sizeof(union UUdata) + (l)) #define sizeudata(u) sizeludata((u)->len) @@ -37,6 +36,8 @@ LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); +LUAI_FUNC void luaS_clearcache (global_State *g); +LUAI_FUNC void luaS_init (lua_State *L); LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 43be88ec..19c350de 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.221 2014/12/11 14:03:07 roberto Exp $ +** $Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -11,6 +11,7 @@ #include +#include #include #include #include @@ -70,7 +71,7 @@ static int str_sub (lua_State *L) { if (start < 1) start = 1; if (end > (lua_Integer)l) end = l; if (start <= end) - lua_pushlstring(L, s + start - 1, (size_t)(end - start + 1)); + lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1); else lua_pushliteral(L, ""); return 1; } @@ -149,9 +150,9 @@ static int str_byte (lua_State *L) { if (posi < 1) posi = 1; if (pose > (lua_Integer)l) pose = l; if (posi > pose) return 0; /* empty interval; return no values */ - n = (int)(pose - posi + 1); - if (posi + n <= pose) /* arithmetic overflow? */ + if (pose - posi >= INT_MAX) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); + n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); for (i=0; icapture[i].len; if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); if (l == CAP_POSITION) - lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); + lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); else lua_pushlstring(ms->L, ms->capture[i].init, l); } @@ -598,8 +599,8 @@ static int str_find_aux (lua_State *L, int find) { /* do a plain search */ const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); if (s2) { - lua_pushinteger(L, s2 - s + 1); - lua_pushinteger(L, s2 - s + lp); + lua_pushinteger(L, (s2 - s) + 1); + lua_pushinteger(L, (s2 - s) + lp); return 2; } } @@ -621,7 +622,7 @@ static int str_find_aux (lua_State *L, int find) { lua_assert(ms.matchdepth == MAXCCALLS); if ((res=match(&ms, s1, p)) != NULL) { if (find) { - lua_pushinteger(L, s1 - s + 1); /* start */ + lua_pushinteger(L, (s1 - s) + 1); /* start */ lua_pushinteger(L, res - s); /* end */ return push_captures(&ms, NULL, 0) + 2; } @@ -797,18 +798,100 @@ static int str_gsub (lua_State *L) { ** ======================================================= */ -/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ -#define MAX_ITEM \ - (sizeof(lua_Number) <= 4 ? 150 : sizeof(lua_Number) <= 8 ? 450 : 5050) +#if !defined(lua_number2strx) /* { */ + +/* +** Hexadecimal floating-point formatter +*/ + +#include +#include + +#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) + + +/* +** Number of bits that goes into the first digit. It can be any value +** between 1 and 4; the following definition tries to align the number +** to nibble boundaries by making what is left after that first digit a +** multiple of 4. +*/ +#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1) + + +/* +** Add integer part of 'x' to buffer and return new 'x' +*/ +static lua_Number adddigit (char *buff, int n, lua_Number x) { + lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ + int d = (int)dd; + buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ + return x - dd; /* return what is left */ +} + + +static int num2straux (char *buff, lua_Number x) { + if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */ + return sprintf(buff, LUA_NUMBER_FMT, x); /* equal to '%g' */ + else if (x == 0) { /* can be -0... */ + sprintf(buff, LUA_NUMBER_FMT, x); + strcat(buff, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */ + return strlen(buff); + } + else { + int e; + lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ + int n = 0; /* character count */ + if (m < 0) { /* is number negative? */ + buff[n++] = '-'; /* add signal */ + m = -m; /* make it positive */ + } + buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ + m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */ + e -= L_NBFD; /* this digit goes before the radix point */ + if (m > 0) { /* more digits? */ + buff[n++] = lua_getlocaledecpoint(); /* add radix point */ + do { /* add as many digits as needed */ + m = adddigit(buff, n++, m * 16); + } while (m > 0); + } + n += sprintf(buff + n, "p%+d", e); /* add exponent */ + return n; + } +} + + +static int lua_number2strx (lua_State *L, char *buff, const char *fmt, + lua_Number x) { + int n = num2straux(buff, x); + if (fmt[SIZELENMOD] == 'A') { + int i; + for (i = 0; i < n; i++) + buff[i] = toupper(uchar(buff[i])); + } + else if (fmt[SIZELENMOD] != 'a') + luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); + return n; +} + +#endif /* } */ + + +/* +** Maximum size of each formatted item. This maximum size is produced +** by format('%.99f', minfloat), and is equal to 99 + 2 ('-' and '.') + +** number of decimal digits to represent minfloat. +*/ +#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) + /* valid flags in a format specification */ #define FLAGS "-+ #0" /* ** maximum size of each format specification (such as "%-099.99d") -** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error) */ -#define MAX_FORMAT (sizeof(FLAGS) + 2 + 10) +#define MAX_FORMAT 32 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { @@ -850,8 +933,8 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { if (isdigit(uchar(*p))) luaL_error(L, "invalid format (width or precision too long)"); *(form++) = '%'; - memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char)); - form += p - strfrmt + 1; + memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char)); + form += (p - strfrmt) + 1; *form = '\0'; return p; } @@ -902,9 +985,10 @@ static int str_format (lua_State *L) { nb = sprintf(buff, form, n); break; } -#if defined(LUA_USE_AFORMAT) case 'a': case 'A': -#endif + addlenmod(form, LUA_NUMBER_FRMLEN); + nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg)); + break; case 'e': case 'E': case 'f': case 'g': case 'G': { addlenmod(form, LUA_NUMBER_FRMLEN); @@ -922,13 +1006,12 @@ static int str_format (lua_State *L) { /* no precision and string is too long to be formatted; keep original string */ luaL_addvalue(&b); - break; } else { nb = sprintf(buff, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ - break; } + break; } default: { /* also treat cases 'pnLlh' */ return luaL_error(L, "invalid option '%%%c' to 'format'", @@ -1250,7 +1333,7 @@ static int str_pack (lua_State *L) { totalsize += len + 1; break; } - case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* go through */ + case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */ case Kpaddalign: case Knop: arg--; /* undo increment */ break; diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 38be0051..04f2a347 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.100 2015/01/05 13:52:37 roberto Exp $ +** $Id: ltable.c,v 2.111 2015/06/09 14:21:13 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -14,8 +14,8 @@ ** Implementation of tables (aka arrays, objects, or hash tables). ** Tables keep its elements in two parts: an array part and a hash part. ** Non-negative integer keys are all candidates to be kept in the array -** part. The actual size of the array is the largest 'n' such that at -** least half the slots between 0 and n are in use. +** part. The actual size of the array is the largest 'n' such that +** more than half the slots between 1 and n are in use. ** Hash uses a mix of chained scatter table with Brent's variation. ** A main invariant of these tables is that, if an element is not ** in its main position (i.e. the 'original' position that its hash gives @@ -23,9 +23,7 @@ ** Hence even when the load factor reaches 100%, performance remains good. */ -#include #include -#include #include #include "lua.h" @@ -71,7 +69,7 @@ #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) -#define hashpointer(t,p) hashmod(t, point2int(p)) +#define hashpointer(t,p) hashmod(t, point2uint(p)) #define dummynode (&dummynode_) @@ -85,31 +83,33 @@ static const Node dummynode_ = { /* -** Checks whether a float has a value representable as a lua_Integer -** (and does the conversion if so) +** Hash for floating-point numbers. +** The main computation should be just +** n = frepx(n, &i); return (n * INT_MAX) + i +** but there are some numerical subtleties. +** In a two-complement representation, INT_MAX does not has an exact +** representation as a float, but INT_MIN does; because the absolute +** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the +** absolute value of the product 'frexp * -INT_MIN' is smaller or equal +** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when +** adding 'i'; the use of '~u' (instead of '-u') avoids problems with +** INT_MIN. */ -static int numisinteger (lua_Number x, lua_Integer *p) { - if ((x) == l_floor(x)) /* integral value? */ - return lua_numbertointeger(x, p); /* try as an integer */ - else return 0; -} - - -/* -** hash for floating-point numbers -*/ -static Node *hashfloat (const Table *t, lua_Number n) { +#if !defined(l_hashfloat) +static int l_hashfloat (lua_Number n) { int i; - n = l_mathop(frexp)(n, &i) * cast_num(INT_MAX - DBL_MAX_EXP); - i += cast_int(n); - if (i < 0) { - if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */ - i = 0; /* handle INT_MIN */ - i = -i; /* must be a positive value */ + lua_Integer ni; + n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); + if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ + lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == HUGE_VAL); + return 0; + } + else { /* normal case */ + unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni); + return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u); } - return hashmod(t, i); } - +#endif /* @@ -121,13 +121,13 @@ static Node *mainposition (const Table *t, const TValue *key) { case LUA_TNUMINT: return hashint(t, ivalue(key)); case LUA_TNUMFLT: - return hashfloat(t, fltvalue(key)); + return hashmod(t, l_hashfloat(fltvalue(key))); case LUA_TSHRSTR: return hashstr(t, tsvalue(key)); case LUA_TLNGSTR: { TString *s = tsvalue(key); if (s->extra == 0) { /* no hash? */ - s->hash = luaS_hash(getstr(s), s->len, s->hash); + s->hash = luaS_hash(getstr(s), s->u.lnglen, s->hash); s->extra = 1; /* now it has its hash */ } return hashstr(t, tsvalue(key)); @@ -219,28 +219,29 @@ int luaH_next (lua_State *L, Table *t, StkId key) { /* ** Compute the optimal size for the array part of table 't'. 'nums' is a ** "count array" where 'nums[i]' is the number of integers in the table -** between 2^(i - 1) + 1 and 2^i. Put in '*narray' the optimal size, and -** return the number of elements that will go to that part. +** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of +** integer keys in the table and leaves with the number of keys that +** will go to the array part; return the optimal size. */ -static unsigned int computesizes (unsigned int nums[], unsigned int *narray) { +static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { int i; - unsigned int twotoi; /* 2^i */ + unsigned int twotoi; /* 2^i (candidate for optimal size) */ unsigned int a = 0; /* number of elements smaller than 2^i */ unsigned int na = 0; /* number of elements to go to array part */ - unsigned int n = 0; /* optimal size for array part */ - for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { + unsigned int optimal = 0; /* optimal size for array part */ + /* loop while keys can fill more than half of total size */ + for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { if (nums[i] > 0) { a += nums[i]; if (a > twotoi/2) { /* more than half elements present? */ - n = twotoi; /* optimal size (till now) */ - na = a; /* all elements up to 'n' will go to array part */ + optimal = twotoi; /* optimal size (till now) */ + na = a; /* all elements up to 'optimal' will go to array part */ } } - if (a == *narray) break; /* all elements already counted */ } - *narray = n; - lua_assert(*narray/2 <= na && na <= *narray); - return na; + lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); + *pna = na; + return optimal; } @@ -255,6 +256,11 @@ static int countint (const TValue *key, unsigned int *nums) { } +/* +** Count keys in array part of table 't': Fill 'nums[i]' with +** number of keys that will go into corresponding slice and return +** total number of non-nil keys. +*/ static unsigned int numusearray (const Table *t, unsigned int *nums) { int lg; unsigned int ttlg; /* 2^lg */ @@ -281,8 +287,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) { } -static int numusehash (const Table *t, unsigned int *nums, - unsigned int *pnasize) { +static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { int totaluse = 0; /* total number of elements */ int ause = 0; /* elements added to 'nums' (can go to array part) */ int i = sizenode(t); @@ -293,7 +298,7 @@ static int numusehash (const Table *t, unsigned int *nums, totaluse++; } } - *pnasize += ause; + *pna += ause; return totaluse; } @@ -363,7 +368,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize, } } if (!isdummy(nold)) - luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */ + luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old hash */ } @@ -376,21 +381,22 @@ void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i */ static void rehash (lua_State *L, Table *t, const TValue *ek) { - unsigned int nasize, na; + unsigned int asize; /* optimal size for array part */ + unsigned int na; /* number of keys in the array part */ unsigned int nums[MAXABITS + 1]; int i; int totaluse; for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ - nasize = numusearray(t, nums); /* count keys in array part */ - totaluse = nasize; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ + na = numusearray(t, nums); /* count keys in array part */ + totaluse = na; /* all those keys are integer keys */ + totaluse += numusehash(t, nums, &na); /* count keys in hash part */ /* count extra key */ - nasize += countint(ek, nums); + na += countint(ek, nums); totaluse++; /* compute new size for array part */ - na = computesizes(nums, &nasize); + asize = computesizes(nums, &na); /* resize the table to new computed sizes */ - luaH_resize(L, t, nasize, totaluse - na); + luaH_resize(L, t, asize, totaluse - na); } @@ -443,14 +449,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { TValue aux; if (ttisnil(key)) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { - lua_Number n = fltvalue(key); lua_Integer k; - if (luai_numisnan(n)) - luaG_runerror(L, "table index is NaN"); - if (numisinteger(n, &k)) { /* index is int? */ + if (luaV_tointeger(key, &k, 0)) { /* index is int? */ setivalue(&aux, k); key = &aux; /* insert it as an integer */ } + else if (luai_numisnan(fltvalue(key))) + luaG_runerror(L, "table index is NaN"); } mp = mainposition(t, key); if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ @@ -544,10 +549,10 @@ const TValue *luaH_get (Table *t, const TValue *key) { case LUA_TNIL: return luaO_nilobject; case LUA_TNUMFLT: { lua_Integer k; - if (numisinteger(fltvalue(key), &k)) /* index is int? */ + if (luaV_tointeger(key, &k, 0)) /* index is int? */ return luaH_getint(t, k); /* use specialized version */ - /* else go through */ - } + /* else... */ + } /* FALLTHROUGH */ default: { Node *n = mainposition(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index 8f78afb7..a05c885c 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.79 2014/11/02 19:19:04 roberto Exp $ +** $Id: ltablib.c,v 1.80 2015/01/13 16:27:29 roberto Exp $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -124,8 +124,6 @@ static int tmove (lua_State *L) { lua_Integer e = luaL_checkinteger(L, 3); lua_Integer t = luaL_checkinteger(L, 4); int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ - /* the following restriction avoids several problems with overflows */ - luaL_argcheck(L, f > 0, 2, "initial position must be positive"); if (e >= f) { /* otherwise, nothing to move */ lua_Integer n, i; ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL) @@ -134,7 +132,11 @@ static int tmove (lua_State *L) { ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL) ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) : lua_seti; + luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, + "too many elements to move"); n = e - f + 1; /* number of elements to move */ + luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, + "destination wrap around"); if (t > f) { for (i = n - 1; i >= 0; i--) { (*ta.geti)(L, 1, f + i); diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 25b46b17..c38e5c34 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.33 2014/11/21 12:15:57 roberto Exp $ +** $Id: ltm.c,v 2.34 2015/03/30 15:42:27 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -117,6 +117,7 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, switch (event) { case TM_CONCAT: luaG_concaterror(L, p1, p2); + /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { lua_Number dummy; @@ -124,8 +125,8 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, luaG_tointerror(L, p1, p2); else luaG_opinterror(L, p1, p2, "perform bitwise operation on"); - /* else go through */ } + /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ default: luaG_opinterror(L, p1, p2, "perform arithmetic on"); } diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 34a3900a..7a47582c 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.222 2014/11/11 19:41:27 roberto Exp $ +** $Id: lua.c,v 1.225 2015/03/30 15:42:59 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -80,9 +80,7 @@ #include #include #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,idx) \ - if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \ - add_history(lua_tostring(L, idx)); /* add it to history */ +#define lua_saveline(L,line) ((void)L, add_history(line)) #define lua_freeline(L,b) ((void)L, free(b)) #else /* }{ */ @@ -90,7 +88,7 @@ #define lua_readline(L,b,p) \ ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,idx) { (void)L; (void)idx; } +#define lua_saveline(L,line) { (void)L; (void)line; } #define lua_freeline(L,b) { (void)L; (void)b; } #endif /* } */ @@ -315,11 +313,11 @@ static int pushline (lua_State *L, int firstline) { lua_pop(L, 1); /* remove prompt */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ - b[l-1] = '\0'; /* remove it */ + b[--l] = '\0'; /* remove it */ if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ else - lua_pushstring(L, b); + lua_pushlstring(L, b, l); lua_freeline(L, b); return 1; } @@ -336,8 +334,12 @@ static int addreturn (lua_State *L) { lua_pushvalue(L, -2); /* duplicate line */ lua_concat(L, 2); /* new line is "return ..." */ line = lua_tolstring(L, -1, &len); - if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) + if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) { lua_remove(L, -3); /* remove original line */ + line += sizeof("return")/sizeof(char); /* remove 'return' for history */ + if (line[0] != '\0') /* non empty? */ + lua_saveline(L, line); /* keep history */ + } else lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */ return status; @@ -352,8 +354,10 @@ static int multiline (lua_State *L) { size_t len; const char *line = lua_tolstring(L, 1, &len); /* get what it has */ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ - if (!incomplete(L, status) || !pushline(L, 0)) + if (!incomplete(L, status) || !pushline(L, 0)) { + lua_saveline(L, line); /* keep history */ return status; /* cannot or should not try to add continuation line */ + } lua_pushliteral(L, "\n"); /* add newline... */ lua_insert(L, -2); /* ...between the two lines */ lua_concat(L, 3); /* join them */ @@ -374,7 +378,6 @@ static int loadline (lua_State *L) { return -1; /* no input */ if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ status = multiline(L); /* try as command, maybe with continuation lines */ - lua_saveline(L, 1); /* keep history */ lua_remove(L, 1); /* remove line from the stack */ lua_assert(lua_gettop(L) == 1); return status; @@ -482,14 +485,14 @@ static int collectargs (char **argv, int *first) { args |= has_E; break; case 'i': - args |= has_i; /* goes through (-i implies -v) */ + args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ case 'v': if (argv[i][2] != '\0') /* extra characters after 1st? */ return has_error; /* invalid option */ args |= has_v; break; case 'e': - args |= has_e; /* go through */ + args |= has_e; /* FALLTHROUGH */ case 'l': /* both options need an argument */ if (argv[i][2] == '\0') { /* no concatenated argument? */ i++; /* try next 'argv' */ @@ -513,17 +516,16 @@ static int collectargs (char **argv, int *first) { static int runargs (lua_State *L, char **argv, int n) { int i; for (i = 1; i < n; i++) { - int status; int option = argv[i][1]; lua_assert(argv[i][0] == '-'); /* already checked */ if (option == 'e' || option == 'l') { + int status; const char *extra = argv[i] + 2; /* both options need an argument */ if (*extra == '\0') extra = argv[++i]; lua_assert(extra != NULL); - if (option == 'e') - status = dostring(L, extra, "=(command line)"); - else - status = dolibrary(L, extra); + status = (option == 'e') + ? dostring(L, extra, "=(command line)") + : dolibrary(L, extra); if (status != LUA_OK) return 0; } } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 313e4cd0..1c2b95ac 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.325 2014/12/26 17:24:27 roberto Exp $ +** $Id: lua.h,v 1.328 2015/06/03 13:03:38 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -19,7 +19,7 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "3" #define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "0" +#define LUA_VERSION_RELEASE "1" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE @@ -35,9 +35,11 @@ /* -** pseudo-indices +** Pseudo-indices +** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty +** space after that to help overflow detection) */ -#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX +#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) @@ -280,7 +282,6 @@ LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); -LUA_API void (lua_clonefunction) (lua_State *L, const void *eL); /* ** coroutine functions @@ -357,8 +358,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) -#define lua_pushliteral(L, s) \ - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) +#define lua_pushliteral(L, s) lua_pushstring(L, "" s) #define lua_pushglobaltable(L) \ lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) @@ -458,11 +458,6 @@ struct lua_Debug { /* }====================================================================== */ -/* Add by skynet */ - -LUA_API lua_State * skynet_sig_L; -LUA_API void (lua_checksig_)(lua_State *L); -#define lua_checksig(L) if (skynet_sig_L) { lua_checksig_(L); } /****************************************************************************** * Copyright (C) 1994-2015 Lua.org, PUC-Rio. diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index ea2f1c8f..c0c91d01 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -1,5 +1,5 @@ /* -** $Id: luac.c,v 1.72 2015/01/06 03:09:13 lhf Exp $ +** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $ ** Lua compiler (saves bytecodes to files; also lists bytecodes) ** See Copyright Notice in lua.h */ @@ -149,9 +149,9 @@ static const Proto* combine(lua_State* L, int n) for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sp->sizeupvalues>0) f->p[i]->sp->upvalues[0].instack=0; + if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; } - f->sp->sizelineinfo=0; + f->sizelineinfo=0; return f; } } @@ -206,7 +206,7 @@ int main(int argc, char* argv[]) } /* -** $Id: print.c,v 1.76 2015/01/05 16:12:50 lhf Exp $ +** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $ ** print bytecodes ** See Copyright Notice in lua.h */ @@ -226,7 +226,7 @@ int main(int argc, char* argv[]) static void PrintString(const TString* ts) { const char* s=getstr(ts); - size_t i,n=ts->len; + size_t i,n=tsslen(ts); printf("%c",'"'); for (i=0; isp->upvalues[x].name) ? getstr(f->sp->upvalues[x].name) : "-") +#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") #define MYK(x) (-1-(x)) static void PrintCode(const Proto* f) { - const SharedProto *sp = f->sp; - const Instruction* code=sp->code; - int pc,n=sp->sizecode; + const Instruction* code=f->code; + int pc,n=f->sizecode; for (pc=0; pcsource ? getstr(f->source) : "=?"; if (*s=='@' || *s=='=') @@ -415,9 +414,8 @@ static void PrintHeader(const SharedProto* f) static void PrintDebug(const Proto* f) { - const SharedProto *sp = f->sp; int i,n; - n=sp->sizek; + n=f->sizek; printf("constants (%d) for %p:\n",n,VOID(f)); for (i=0; isizelocvars; + n=f->sizelocvars; printf("locals (%d) for %p:\n",n,VOID(f)); for (i=0; ilocvars[i].varname),sp->locvars[i].startpc+1,sp->locvars[i].endpc+1); + i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); } - n=f->sp->sizeupvalues; + n=f->sizeupvalues; printf("upvalues (%d) for %p:\n",n,VOID(f)); for (i=0; iupvalues[i].instack,sp->upvalues[i].idx); + i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx); } } static void PrintFunction(const Proto* f, int full) { - int i,n=f->sp->sizep; - PrintHeader(f->sp); + int i,n=f->sizep; + PrintHeader(f); PrintCode(f); if (full) PrintDebug(f); for (i=0; ip[i],full); diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index fd28d21a..7cfa4faf 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.238 2014/12/29 13:27:55 roberto Exp $ +** $Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -96,10 +96,8 @@ /* -@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines the type for -** Lua integers. -@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines -** the type for Lua floats. +@@ LUA_INT_TYPE defines the type for Lua integers. +@@ LUA_FLOAT_TYPE defines the type for Lua floats. ** Lua should work fine with any mix of these options (if supported ** by your C compiler). The usual configurations are 64-bit integers ** and 'double' (the default), 32-bit integers and 'float' (for @@ -107,31 +105,46 @@ ** compliant with C99, which may not have support for 'long long'). */ +/* predefined options for LUA_INT_TYPE */ +#define LUA_INT_INT 1 +#define LUA_INT_LONG 2 +#define LUA_INT_LONGLONG 3 + +/* predefined options for LUA_FLOAT_TYPE */ +#define LUA_FLOAT_FLOAT 1 +#define LUA_FLOAT_DOUBLE 2 +#define LUA_FLOAT_LONGDOUBLE 3 + #if defined(LUA_32BITS) /* { */ /* ** 32-bit integers and 'float' */ #if LUAI_BITSINT >= 32 /* use 'int' if big enough */ -#define LUA_INT_INT +#define LUA_INT_TYPE LUA_INT_INT #else /* otherwise use 'long' */ -#define LUA_INT_LONG +#define LUA_INT_TYPE LUA_INT_LONG #endif -#define LUA_REAL_FLOAT +#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT #elif defined(LUA_C89_NUMBERS) /* }{ */ /* ** largest types available for C89 ('long' and 'double') */ -#define LUA_INT_LONG -#define LUA_REAL_DOUBLE +#define LUA_INT_TYPE LUA_INT_LONG +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE + +#endif /* } */ + -#else /* }{ */ /* ** default configuration for 64-bit Lua ('long long' and 'double') */ -#define LUA_INT_LONGLONG -#define LUA_REAL_DOUBLE +#if !defined(LUA_INT_TYPE) +#define LUA_INT_TYPE LUA_INT_LONGLONG +#endif +#if !defined(LUA_FLOAT_TYPE) +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE #endif /* } */ /* }================================================================== */ @@ -155,7 +168,7 @@ ** non-conventional directories. */ #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#if defined(_WIN32) /* { */ +#if defined(_WIN32) /* { */ /* ** In Windows, any exclamation mark ('!') in the path is replaced by the ** path of the directory of the executable file of the current process. @@ -300,20 +313,15 @@ */ #define LUA_COMPAT_APIINTCASTS - -/* -@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a -@@ a float mark ('.0'). -** This macro is not on by default even in compatibility mode, -** because this is not really an incompatibility. -*/ -/* #define LUA_COMPAT_FLOATSTRING */ - #endif /* } */ #if defined(LUA_COMPAT_5_1) /* { */ +/* Incompatibilities from 5.2 -> 5.3 */ +#define LUA_COMPAT_MATHLIB +#define LUA_COMPAT_APIINTCASTS + /* @@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. ** You can replace it with 'table.unpack'. @@ -373,6 +381,15 @@ #endif /* } */ + +/* +@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a +@@ a float mark ('.0'). +** This macro is not on by default even in compatibility mode, +** because this is not really an incompatibility. +*/ +/* #define LUA_COMPAT_FLOATSTRING */ + /* }================================================================== */ @@ -380,30 +397,30 @@ /* ** {================================================================== ** Configuration for Numbers. -** Change these definitions if no predefined LUA_REAL_* / LUA_INT_* +** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* ** satisfy your needs. ** =================================================================== */ /* @@ LUA_NUMBER is the floating-point type used by Lua. -** @@ LUAI_UACNUMBER is the result of an 'usual argument conversion' @@ over a floating number. -** +@@ l_mathlim(x) corrects limit name 'x' to the proper float type +** by prefixing it with one of FLT/DBL/LDBL. @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. @@ LUA_NUMBER_FMT is the format for writing floats. @@ lua_number2str converts a float to a string. -** @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. -** @@ lua_str2number converts a decimal numeric string to a number. */ -#if defined(LUA_REAL_FLOAT) /* { single float */ +#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ #define LUA_NUMBER float +#define l_mathlim(n) (FLT_##n) + #define LUAI_UACNUMBER double #define LUA_NUMBER_FRMLEN "" @@ -414,10 +431,12 @@ #define lua_str2number(s,p) strtof((s), (p)) -#elif defined(LUA_REAL_LONGDOUBLE) /* }{ long double */ +#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ #define LUA_NUMBER long double +#define l_mathlim(n) (LDBL_##n) + #define LUAI_UACNUMBER long double #define LUA_NUMBER_FRMLEN "L" @@ -427,10 +446,12 @@ #define lua_str2number(s,p) strtold((s), (p)) -#elif defined(LUA_REAL_DOUBLE) /* }{ double */ +#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ #define LUA_NUMBER double +#define l_mathlim(n) (DBL_##n) + #define LUAI_UACNUMBER double #define LUA_NUMBER_FRMLEN "" @@ -440,9 +461,9 @@ #define lua_str2number(s,p) strtod((s), (p)) -#else /* }{ */ +#else /* }{ */ -#error "numeric real type not defined" +#error "numeric float type not defined" #endif /* } */ @@ -466,46 +487,6 @@ (*(p) = (LUA_INTEGER)(n), 1)) -/* -@@ The luai_num* macros define the primitive operations over numbers. -** They should work for any size of floating numbers. -*/ - -/* the following operations need the math library */ -#if defined(lobject_c) || defined(lvm_c) -#include - -/* floor division (defined as 'floor(a/b)') */ -#define luai_numidiv(L,a,b) ((void)L, l_mathop(floor)(luai_numdiv(L,a,b))) - -/* -** module: defined as 'a - floor(a/b)*b'; the previous definition gives -** NaN when 'b' is huge, but the result should be 'a'. 'fmod' gives the -** result of 'a - trunc(a/b)*b', and therefore must be corrected when -** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a -** non-integer negative result, which is equivalent to the test below -*/ -#define luai_nummod(L,a,b,m) \ - { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } - -/* exponentiation */ -#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) - -#endif - -/* these are quite standard operations */ -#if defined(LUA_CORE) -#define luai_numadd(L,a,b) ((a)+(b)) -#define luai_numsub(L,a,b) ((a)-(b)) -#define luai_nummul(L,a,b) ((a)*(b)) -#define luai_numdiv(L,a,b) ((a)/(b)) -#define luai_numunm(L,a) (-(a)) -#define luai_numeq(a,b) ((a)==(b)) -#define luai_numlt(a,b) ((a)<(b)) -#define luai_numle(a,b) ((a)<=(b)) -#define luai_numisnan(a) (!luai_numeq((a), (a))) -#endif - /* @@ LUA_INTEGER is the integer type used by Lua. @@ -538,7 +519,7 @@ /* now the variable definitions */ -#if defined(LUA_INT_INT) /* { int */ +#if LUA_INT_TYPE == LUA_INT_INT /* { int */ #define LUA_INTEGER int #define LUA_INTEGER_FRMLEN "" @@ -546,7 +527,7 @@ #define LUA_MAXINTEGER INT_MAX #define LUA_MININTEGER INT_MIN -#elif defined(LUA_INT_LONG) /* }{ long */ +#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ #define LUA_INTEGER long #define LUA_INTEGER_FRMLEN "l" @@ -554,7 +535,7 @@ #define LUA_MAXINTEGER LONG_MAX #define LUA_MININTEGER LONG_MIN -#elif defined(LUA_INT_LONGLONG) /* }{ long long */ +#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ #if defined(LLONG_MAX) /* { */ /* use ISO C99 stuff */ @@ -592,13 +573,13 @@ /* ** {================================================================== -** Dependencies with C99 +** Dependencies with C99 and other C details ** =================================================================== */ /* @@ lua_strx2number converts an hexadecimal numeric string to a number. -** In C99, 'strtod' does both conversions. Otherwise, you can +** In C99, 'strtod' does that conversion. Otherwise, you can ** leave 'lua_strx2number' undefined and Lua will provide its own ** implementation. */ @@ -608,12 +589,13 @@ /* -@@ LUA_USE_AFORMAT allows '%a'/'%A' specifiers in 'string.format' -** Enable it if the C function 'printf' supports these specifiers. -** (C99 demands it and Windows also supports it.) +@@ lua_number2strx converts a float to an hexadecimal numeric string. +** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. +** Otherwise, you can leave 'lua_number2strx' undefined and Lua will +** provide its own implementation. */ -#if !defined(LUA_USE_C89) || defined(LUA_USE_WINDOWS) -#define LUA_USE_AFORMAT +#if !defined(LUA_USE_C89) +#define lua_number2strx(L,b,f,n) sprintf(b,f,n) #endif @@ -642,12 +624,50 @@ #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ __STDC_VERSION__ >= 199901L #include -#if defined (INTPTR_MAX) /* even in C99 this type is optional */ +#if defined(INTPTR_MAX) /* even in C99 this type is optional */ #undef LUA_KCONTEXT #define LUA_KCONTEXT intptr_t #endif #endif + +/* +@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). +** Change that if you do not want to use C locales. (Code using this +** macro must include header 'locale.h'.) +*/ +#if !defined(lua_getlocaledecpoint) +#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Language Variations +** ===================================================================== +*/ + +/* +@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some +** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from +** numbers to strings. Define LUA_NOCVTS2N to turn off automatic +** coercion from strings to numbers. +*/ +/* #define LUA_NOCVTN2S */ +/* #define LUA_NOCVTS2N */ + + +/* +@@ LUA_USE_APICHECK turns on several consistency checks on the C API. +** Define it as a help when debugging C code. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(l,e) assert(e) +#endif + /* }================================================================== */ @@ -671,9 +691,6 @@ #define LUAI_MAXSTACK 15000 #endif -/* reserve some space for error handling */ -#define LUAI_FIRSTPSEUDOIDX (-LUAI_MAXSTACK - 1000) - /* @@ LUA_EXTRASPACE defines the size of a raw memory area associated with @@ -691,20 +708,18 @@ #define LUA_IDSIZE 60 -/* -@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is, -** strings that are internalized. (Cannot be smaller than reserved words -** or tags for metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#define LUAI_MAXSHORTLEN 40 - - /* @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -** CHANGE it if it uses too much C-stack space. +** CHANGE it if it uses too much C-stack space. (For long double, +** 'string.format("%.99f", 1e4932)' needs ~5030 bytes, so a +** smaller buffer would force a memory allocation for each call to +** 'string.format'.) */ -#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) +#if defined(LUA_FLOAT_LONGDOUBLE) +#define LUAL_BUFFERSIZE 8192 +#else +#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) +#endif /* }================================================================== */ diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index c43f8a2d..5165c0fb 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -44,8 +44,6 @@ LUAMOD_API int (luaopen_debug) (lua_State *L); #define LUA_LOADLIBNAME "package" LUAMOD_API int (luaopen_package) (lua_State *L); -#define LUA_CACHELIB -LUAMOD_API int (luaopen_cache) (lua_State *L); /* open all previous libraries */ LUALIB_API void (luaL_openlibs) (lua_State *L); diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 451c9097..510f3258 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -100,7 +100,7 @@ static TString *LoadString (LoadState *S) { } -static void LoadCode (LoadState *S, SharedProto *f) { +static void LoadCode (LoadState *S, Proto *f) { int n = LoadInt(S); f->code = luaM_newvector(S->L, n, Instruction); f->sizecode = n; @@ -115,7 +115,7 @@ static void LoadConstants (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->k = luaM_newvector(S->L, n, TValue); - f->sp->sizek = n; + f->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { @@ -149,17 +149,17 @@ static void LoadProtos (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->p = luaM_newvector(S->L, n, Proto *); - f->sp->sizep = n; + f->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { - f->p[i] = luaF_newproto(S->L, NULL); - LoadFunction(S, f->p[i], f->sp->source); + f->p[i] = luaF_newproto(S->L); + LoadFunction(S, f->p[i], f->source); } } -static void LoadUpvalues (LoadState *S, SharedProto *f) { +static void LoadUpvalues (LoadState *S, Proto *f) { int i, n; n = LoadInt(S); f->upvalues = luaM_newvector(S->L, n, Upvaldesc); @@ -173,7 +173,7 @@ static void LoadUpvalues (LoadState *S, SharedProto *f) { } -static void LoadDebug (LoadState *S, SharedProto *f) { +static void LoadDebug (LoadState *S, Proto *f) { int i, n; n = LoadInt(S); f->lineinfo = luaM_newvector(S->L, n, int); @@ -195,8 +195,7 @@ static void LoadDebug (LoadState *S, SharedProto *f) { } -static void LoadFunction (LoadState *S, Proto *fp, TString *psource) { - SharedProto *f = fp->sp; +static void LoadFunction (LoadState *S, Proto *f, TString *psource) { f->source = LoadString(S); if (f->source == NULL) /* no source in dump? */ f->source = psource; /* reuse parent's source */ @@ -206,9 +205,9 @@ static void LoadFunction (LoadState *S, Proto *fp, TString *psource) { f->is_vararg = LoadByte(S); f->maxstacksize = LoadByte(S); LoadCode(S, f); - LoadConstants(S, fp); + LoadConstants(S, f); LoadUpvalues(S, f); - LoadProtos(S, fp); + LoadProtos(S, f); LoadDebug(S, f); } @@ -269,7 +268,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, cl = luaF_newLclosure(L, LoadByte(&S)); setclLvalue(L, L->top, cl); incr_top(L); - cl->p = luaF_newproto(L, NULL); + cl->p = luaF_newproto(L); LoadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, buff, cl->p); diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c index be4f087f..9042582d 100644 --- a/3rd/lua/lutf8lib.c +++ b/3rd/lua/lutf8lib.c @@ -1,5 +1,5 @@ /* -** $Id: lutf8lib.c,v 1.13 2014/11/02 19:19:04 roberto Exp $ +** $Id: lutf8lib.c,v 1.15 2015/03/28 19:16:55 roberto Exp $ ** Standard library for UTF-8 manipulation ** See Copyright Notice in lua.h */ @@ -11,6 +11,7 @@ #include +#include #include #include @@ -37,7 +38,7 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. */ static const char *utf8_decode (const char *o, int *val) { - static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; + static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; const unsigned char *s = (const unsigned char *)o; unsigned int c = s[0]; unsigned int res = 0; /* final result */ @@ -106,9 +107,9 @@ static int codepoint (lua_State *L) { luaL_argcheck(L, posi >= 1, 2, "out of range"); luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); if (posi > pose) return 0; /* empty interval; return no values */ - n = (int)(pose - posi + 1); - if (posi + n <= pose) /* (lua_Integer -> int) overflow? */ + if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ return luaL_error(L, "string slice too long"); + n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); n = 0; se = s + pose; @@ -234,7 +235,7 @@ static int iter_codes (lua_State *L) { #define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" -static struct luaL_Reg funcs[] = { +static const luaL_Reg funcs[] = { {"offset", byteoffset}, {"codepoint", codepoint}, {"char", utfchar}, @@ -248,7 +249,7 @@ static struct luaL_Reg funcs[] = { LUAMOD_API int luaopen_utf8 (lua_State *L) { luaL_newlib(L, funcs); - lua_pushliteral(L, UTF8PATT); + lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); lua_setfield(L, -2, "charpattern"); return 1; } diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index cb2e2809..a8cefc52 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.232 2014/12/27 20:30:38 roberto Exp $ +** $Id: lvm.c,v 2.245 2015/06/09 15:53:35 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -9,8 +9,9 @@ #include "lprefix.h" - +#include #include +#include #include #include #include @@ -30,47 +31,38 @@ #include "lvm.h" -/* -** You can define LUA_FLOORN2I if you want to convert floats to integers -** by flooring them (instead of raising an error if they are not -** integral values) -*/ -#if !defined(LUA_FLOORN2I) -#define LUA_FLOORN2I 0 -#endif - - /* limit for table tag-method chains (to avoid loops) */ #define MAXTAGLOOP 2000 -/* Add by skynet */ -lua_State * skynet_sig_L = NULL; -LUA_API void -lua_checksig_(lua_State *L) { - if (skynet_sig_L == G(L)->mainthread) { - skynet_sig_L = NULL; - lua_pushnil(L); - lua_error(L); - } -} /* -** Similar to 'tonumber', but does not attempt to convert strings and -** ensure correct precision (no extra bits). Used in comparisons. +** 'l_intfitsf' checks whether a given integer can be converted to a +** float without rounding. Used in comparisons. Left undefined if +** all integers fit in a float precisely. */ -static int tofloat (const TValue *obj, lua_Number *n) { - if (ttisfloat(obj)) *n = fltvalue(obj); - else if (ttisinteger(obj)) { - volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */ - *n = x; - } - else { - *n = 0; /* to avoid warnings */ - return 0; - } - return 1; -} +#if !defined(l_intfitsf) + +/* number of bits in the mantissa of a float */ +#define NBM (l_mathlim(MANT_DIG)) + +/* +** Check whether some integers may not fit in a float, that is, whether +** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger). +** (The shifts are done in parts to avoid shifting by more than the size +** of an integer. In a worst case, NBM == 113 for long double and +** sizeof(integer) == 32.) +*/ +#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ + >> (NBM - (3 * (NBM / 4)))) > 0 + +#define l_intfitsf(i) \ + (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) + +#endif + +#endif + /* @@ -84,7 +76,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { return 1; } else if (cvt2num(obj) && /* string convertible to number? */ - luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { + luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ return 1; } @@ -99,7 +91,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { ** mode == 1: takes the floor of the number ** mode == 2: takes the ceil of the number */ -static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { +int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { TValue v; again: if (ttisfloat(obj)) { @@ -117,7 +109,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { return 1; } else if (cvt2num(obj) && - luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { + luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { obj = &v; goto again; /* convert result from 'luaO_str2num' to an integer */ } @@ -125,14 +117,6 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { } -/* -** try to convert a value to an integer -*/ -int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { - return tointeger_aux(obj, p, LUA_FLOORN2I); -} - - /* ** Try to convert a 'for' limit to an integer, preserving the ** semantics of the loop. @@ -151,11 +135,11 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, int *stopnow) { *stopnow = 0; /* usually, let loops run */ - if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ + if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ lua_Number n; /* try to convert to float */ if (!tonumber(obj, &n)) /* cannot convert to float? */ return 0; /* not a number */ - if (n > 0) { /* if true, float is larger than max integer */ + if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ *p = LUA_MAXINTEGER; if (step < 0) *stopnow = 1; } @@ -250,9 +234,9 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { */ static int l_strcmp (const TString *ls, const TString *rs) { const char *l = getstr(ls); - size_t ll = ls->len; + size_t ll = tsslen(ls); const char *r = getstr(rs); - size_t lr = rs->len; + size_t lr = tsslen(rs); for (;;) { /* for each segment */ int temp = strcoll(l, r); if (temp != 0) /* not equal? */ @@ -271,16 +255,103 @@ static int l_strcmp (const TString *ls, const TString *rs) { } +/* +** Check whether integer 'i' is less than float 'f'. If 'i' has an +** exact representation as a float ('l_intfitsf'), compare numbers as +** floats. Otherwise, if 'f' is outside the range for integers, result +** is trivial. Otherwise, compare them as integers. (When 'i' has no +** float representation, either 'f' is "far away" from 'i' or 'f' has +** no precision left for a fractional part; either way, how 'f' is +** truncated is irrelevant.) When 'f' is NaN, comparisons must result +** in false. +*/ +static int LTintfloat (lua_Integer i, lua_Number f) { +#if defined(l_intfitsf) + if (!l_intfitsf(i)) { + if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ + return 1; /* f >= maxint + 1 > i */ + else if (f > cast_num(LUA_MININTEGER)) /* minint < f <= maxint ? */ + return (i < cast(lua_Integer, f)); /* compare them as integers */ + else /* f <= minint <= i (or 'f' is NaN) --> not(i < f) */ + return 0; + } +#endif + return luai_numlt(cast_num(i), f); /* compare them as floats */ +} + + +/* +** Check whether integer 'i' is less than or equal to float 'f'. +** See comments on previous function. +*/ +static int LEintfloat (lua_Integer i, lua_Number f) { +#if defined(l_intfitsf) + if (!l_intfitsf(i)) { + if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ + return 1; /* f >= maxint + 1 > i */ + else if (f >= cast_num(LUA_MININTEGER)) /* minint <= f <= maxint ? */ + return (i <= cast(lua_Integer, f)); /* compare them as integers */ + else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */ + return 0; + } +#endif + return luai_numle(cast_num(i), f); /* compare them as floats */ +} + + +/* +** Return 'l < r', for numbers. +*/ +static int LTnum (const TValue *l, const TValue *r) { + if (ttisinteger(l)) { + lua_Integer li = ivalue(l); + if (ttisinteger(r)) + return li < ivalue(r); /* both are integers */ + else /* 'l' is int and 'r' is float */ + return LTintfloat(li, fltvalue(r)); /* l < r ? */ + } + else { + lua_Number lf = fltvalue(l); /* 'l' must be float */ + if (ttisfloat(r)) + return luai_numlt(lf, fltvalue(r)); /* both are float */ + else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ + return 0; /* NaN < i is always false */ + else /* without NaN, (l < r) <--> not(r <= l) */ + return !LEintfloat(ivalue(r), lf); /* not (r <= l) ? */ + } +} + + +/* +** Return 'l <= r', for numbers. +*/ +static int LEnum (const TValue *l, const TValue *r) { + if (ttisinteger(l)) { + lua_Integer li = ivalue(l); + if (ttisinteger(r)) + return li <= ivalue(r); /* both are integers */ + else /* 'l' is int and 'r' is float */ + return LEintfloat(li, fltvalue(r)); /* l <= r ? */ + } + else { + lua_Number lf = fltvalue(l); /* 'l' must be float */ + if (ttisfloat(r)) + return luai_numle(lf, fltvalue(r)); /* both are float */ + else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ + return 0; /* NaN <= i is always false */ + else /* without NaN, (l <= r) <--> not(r < l) */ + return !LTintfloat(ivalue(r), lf); /* not (r < l) ? */ + } +} + + /* ** Main operation less than; return 'l < r'. */ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { int res; - lua_Number nl, nr; - if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ - return (ivalue(l) < ivalue(r)); - else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */ - return luai_numlt(nl, nr); + if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ + return LTnum(l, r); else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ return l_strcmp(tsvalue(l), tsvalue(r)) < 0; else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ @@ -290,18 +361,20 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { /* -** Main operation less than or equal to; return 'l <= r'. +** Main operation less than or equal to; return 'l <= r'. If it needs +** a metamethod and there is no '__le', try '__lt', based on +** l <= r iff !(r < l) (assuming a total order). If the metamethod +** yields during this substitution, the continuation has to know +** about it (to negate the result of r= 0) /* first try 'le' */ + else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ return res; else { /* try 'lt': */ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ @@ -314,9 +387,8 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { } - /* -** Main operation for equality of Lua values; return 't1 == t2'. +** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) */ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { @@ -325,10 +397,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ - lua_Number n1, n2; /* compare them as floats */ - lua_assert(ttisnumber(t1) && ttisnumber(t2)); - cast_void(tofloat(t1, &n1)); cast_void(tofloat(t2, &n2)); - return luai_numeq(n1, n2); + lua_Integer i1, i2; /* compare them as integers */ + return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); } } /* values have same type and same variant */ @@ -371,6 +441,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { #define tostring(L,o) \ (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) +#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) + /* ** Main operation for concatenation: concat 'total' values in the stack, ** from 'L->top - total' up to 'L->top - 1'. @@ -382,19 +454,19 @@ void luaV_concat (lua_State *L, int total) { int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); - else if (tsvalue(top-1)->len == 0) /* second operand is empty? */ + else if (isemptystr(top - 1)) /* second operand is empty? */ cast_void(tostring(L, top - 2)); /* result is first operand */ - else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) { + else if (isemptystr(top - 2)) { /* first operand is an empty string? */ setobjs2s(L, top - 2, top - 1); /* result is second op. */ } else { /* at least two non-empty string values; get as many as possible */ - size_t tl = tsvalue(top-1)->len; + size_t tl = vslen(top - 1); char *buffer; int i; /* collect total length */ for (i = 1; i < total && tostring(L, top-i-1); i++) { - size_t l = tsvalue(top-i-1)->len; + size_t l = vslen(top - i - 1); if (l >= (MAX_SIZE/sizeof(char)) - tl) luaG_runerror(L, "string length overflow"); tl += l; @@ -403,7 +475,7 @@ void luaV_concat (lua_State *L, int total) { tl = 0; n = i; do { /* copy all strings to buffer */ - size_t l = tsvalue(top-i)->len; + size_t l = vslen(top - i); memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); tl += l; } while (--i > 0); @@ -420,7 +492,7 @@ void luaV_concat (lua_State *L, int total) { */ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; - switch (ttnov(rb)) { + switch (ttype(rb)) { case LUA_TTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); @@ -428,8 +500,12 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { setivalue(ra, luaH_getn(h)); /* else primitive len */ return; } - case LUA_TSTRING: { - setivalue(ra, tsvalue(rb)->len); + case LUA_TSHRSTR: { + setivalue(ra, tsvalue(rb)->shrlen); + return; + } + case LUA_TLNGSTR: { + setivalue(ra, tsvalue(rb)->u.lnglen); return; } default: { /* try metamethod */ @@ -465,7 +541,7 @@ lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { /* -** Integer modulus; return 'm % n'. (Assume that C '%' with +** Integer modulus; return 'm % n'. (Assume that C '%' with ** negative operands follows C99 behavior. See previous comment ** about luaV_div.) */ @@ -510,8 +586,8 @@ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { LClosure *c = p->cache; if (c != NULL) { /* is there a cached closure? */ - int nup = p->sp->sizeupvalues; - Upvaldesc *uv = p->sp->upvalues; + int nup = p->sizeupvalues; + Upvaldesc *uv = p->upvalues; int i; for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; @@ -531,8 +607,8 @@ static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { - int nup = p->sp->sizeupvalues; - Upvaldesc *uv = p->sp->upvalues; + int nup = p->sizeupvalues; + Upvaldesc *uv = p->upvalues; int i; LClosure *ncl = luaF_newLclosure(L, nup); ncl->p = p; @@ -624,7 +700,7 @@ void luaV_finishOp (lua_State *L) { ** some macros for common tasks in 'luaV_execute' */ -#if !defined luai_runtimecheck +#if !defined(luai_runtimecheck) #define luai_runtimecheck(L, c) /* void */ #endif @@ -760,7 +836,7 @@ void luaV_execute (lua_State *L) { Protect(luaV_gettable(L, rb, RKC(i), ra)); vmbreak; } - vmcase(OP_ADD) { + vmcase(OP_ADD) { TValue *rb = RKB(i); TValue *rc = RKC(i); lua_Number nb; lua_Number nc; @@ -945,14 +1021,13 @@ void luaV_execute (lua_State *L) { L->top = base + c + 1; /* mark the end of concat operands */ Protect(luaV_concat(L, c - b + 1)); ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */ - rb = b + base; + rb = base + b; setobjs2s(L, ra, rb); checkGC(L, (ra >= rb ? ra + 1 : rb)); L->top = ci->top; /* restore top */ vmbreak; } vmcase(OP_JMP) { - lua_checksig(L); dojump(ci, i, 0); vmbreak; } @@ -1005,7 +1080,6 @@ void luaV_execute (lua_State *L) { vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; - lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ if (luaD_precall(L, ra, nresults)) { /* C function? */ if (nresults >= 0) L->top = ci->top; /* adjust results */ @@ -1020,7 +1094,6 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TAILCALL) { int b = GETARG_B(i); - lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ @@ -1032,10 +1105,10 @@ void luaV_execute (lua_State *L) { StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ - StkId lim = nci->u.l.base + getproto(nfunc)->sp->numparams; + StkId lim = nci->u.l.base + getproto(nfunc)->numparams; int aux; /* close all upvalues from previous call */ - if (cl->p->sp->sizep > 0) luaF_close(L, oci->u.l.base); + if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); @@ -1051,9 +1124,8 @@ void luaV_execute (lua_State *L) { } vmcase(OP_RETURN) { int b = GETARG_B(i); - if (b != 0) L->top = ra+b-1; - if (cl->p->sp->sizep > 0) luaF_close(L, base); - b = luaD_poscall(L, ra); + if (cl->p->sizep > 0) luaF_close(L, base); + b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra)); if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ return; /* external invocation: return */ else { /* invocation via reentry: continue execution */ @@ -1071,7 +1143,7 @@ void luaV_execute (lua_State *L) { lua_Integer limit = ivalue(ra + 1); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - setivalue(ra, idx); /* update internal index... */ + chgivalue(ra, idx); /* update internal index... */ setivalue(ra + 3, idx); /* ...and external index */ } } @@ -1082,7 +1154,7 @@ void luaV_execute (lua_State *L) { if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - setfltvalue(ra, idx); /* update internal index... */ + chgfltvalue(ra, idx); /* update internal index... */ setfltvalue(ra + 3, idx); /* ...and external index */ } } @@ -1131,7 +1203,6 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TFORLOOP) { l_tforloop: - lua_checksig(L); if (!ttisnil(ra + 1)) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ @@ -1174,7 +1245,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; int j; - int n = cast_int(base - ci->func) - cl->p->sp->numparams - 1; + int n = cast_int(base - ci->func) - cl->p->numparams - 1; if (b < 0) { /* B == 0? */ b = n; /* get all var. arguments */ Protect(luaD_checkstack(L, n)); diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index 5c5e18c3..0613826a 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 roberto Exp $ +** $Id: lvm.h,v 2.35 2015/02/20 14:27:53 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -27,11 +27,21 @@ #endif +/* +** You can define LUA_FLOORN2I if you want to convert floats to integers +** by flooring them (instead of raising an error if they are not +** integral values) +*/ +#if !defined(LUA_FLOORN2I) +#define LUA_FLOORN2I 0 +#endif + + #define tonumber(o,n) \ (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) #define tointeger(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) + (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) @@ -42,7 +52,7 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); -LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); +LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val); LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, From 2086e13a4696792d32001f671821571fd27c6ab2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 17 Jun 2015 22:12:48 +0800 Subject: [PATCH 399/729] hack lua to support signal --- 3rd/lua/lua.h | 5 +++++ 3rd/lua/lvm.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 1c2b95ac..065e72a0 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -458,6 +458,11 @@ struct lua_Debug { /* }====================================================================== */ +/* Add by skynet */ + +LUA_API lua_State * skynet_sig_L; +LUA_API void (lua_checksig_)(lua_State *L); +#define lua_checksig(L) if (skynet_sig_L) { lua_checksig_(L); } /****************************************************************************** * Copyright (C) 1994-2015 Lua.org, PUC-Rio. diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index a8cefc52..a45d6cf2 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -63,7 +63,17 @@ #endif +/* Add by skynet */ +lua_State * skynet_sig_L = NULL; +LUA_API void +lua_checksig_(lua_State *L) { + if (skynet_sig_L == G(L)->mainthread) { + skynet_sig_L = NULL; + lua_pushnil(L); + lua_error(L); + } +} /* ** Try to convert a value to a float. The float case is already handled @@ -1028,6 +1038,7 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_JMP) { + lua_checksig(L); dojump(ci, i, 0); vmbreak; } @@ -1080,6 +1091,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; + lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ if (luaD_precall(L, ra, nresults)) { /* C function? */ if (nresults >= 0) L->top = ci->top; /* adjust results */ @@ -1094,6 +1106,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TAILCALL) { int b = GETARG_B(i); + lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ @@ -1203,6 +1216,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TFORLOOP) { l_tforloop: + lua_checksig(L); if (!ttisnil(ra + 1)) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ From 5d9142110e56f9076ef8c4c8d7c6183556b9c571 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 18 Jun 2015 17:39:37 +0800 Subject: [PATCH 400/729] patch lua_clonefunction --- 3rd/lua/Makefile | 2 +- 3rd/lua/lapi.c | 55 ++++++++++++++++++++- 3rd/lua/lauxlib.c | 122 +++++++++++++++++++++++++++++++++++++++++++++- 3rd/lua/lcode.c | 36 +++++++------- 3rd/lua/lcode.h | 2 +- 3rd/lua/ldebug.c | 24 ++++----- 3rd/lua/ldebug.h | 4 +- 3rd/lua/ldo.c | 4 +- 3rd/lua/ldump.c | 21 ++++---- 3rd/lua/lfunc.c | 55 +++++++++++++-------- 3rd/lua/lfunc.h | 2 +- 3rd/lua/lgc.c | 32 +++++++----- 3rd/lua/lobject.h | 22 +++++---- 3rd/lua/lparser.c | 75 ++++++++++++++-------------- 3rd/lua/lua.h | 2 + 3rd/lua/luac.c | 27 +++++----- 3rd/lua/lundump.c | 23 ++++----- 3rd/lua/lvm.c | 16 +++--- 18 files changed, 364 insertions(+), 160 deletions(-) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index d71c75c8..6c680538 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -7,7 +7,7 @@ PLAT= none CC= gcc -std=gnu99 -CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS) +CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= -lm $(SYSLIBS) $(MYLIBS) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index fea9eb27..953a304b 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -28,6 +28,7 @@ #include "ltm.h" #include "lundump.h" #include "lvm.h" +#include "lfunc.h" @@ -988,6 +989,58 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, return status; } +static Proto * cloneproto (lua_State *L, const Proto *src) { + /* copy constants and nested proto */ + int i,n; + Proto *f = luaF_newproto(L, src->sp); + n = src->sp->sizek; + f->k=luaM_newvector(L,n,TValue); + for (i=0; ik[i]); + for (i=0; ik[i]; + TValue *o=&f->k[i]; + if (ttisstring(s)) { + TString * str = luaS_newlstr(L,svalue(s),vslen(s)); + setsvalue2n(L,o,str); + } else { + setobj(L,o,s); + } + } + n = src->sp->sizep; + f->p=luaM_newvector(L,n,struct Proto *); + for (i=0; ip[i]=NULL; + for (i=0; ip[i]=cloneproto(L, src->p[i]); + } + return f; +} + +LUA_API void lua_clonefunction (lua_State *L, const void * fp) { + LClosure *cl; + LClosure *f = cast(LClosure *, fp); + lua_lock(L); + if (f->p->sp->l_G == G(L)) { + setclLvalue(L,L->top,f); + api_incr_top(L); + lua_unlock(L); + return; + } + cl = luaF_newLclosure(L,f->nupvalues); + cl->p = cloneproto(L, f->p); + setclLvalue(L,L->top,cl); + api_incr_top(L); + luaF_initupvals(L, cl); + + if (cl->nupvalues >= 1) { /* does it have an upvalue? */ + /* get global table from registry */ + Table *reg = hvalue(&G(L)->l_registry); + const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); + /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ + setobj(L, cl->upvals[0]->v, gt); + luaC_upvalbarrier(L, cl->upvals[0]); + } + lua_unlock(L); +} LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; @@ -1183,7 +1236,7 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val, case LUA_TLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; - Proto *p = f->p; + SharedProto *p = f->p->sp; if (!(1 <= n && n <= p->sizeupvalues)) return NULL; *val = f->upvals[n-1]->v; if (uv) *uv = f->upvals[n - 1]; diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index b8bace7f..e4d79955 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -638,7 +638,7 @@ static int skipcomment (LoadF *lf, int *cp) { } -LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, +static int luaL_loadfilex_ (lua_State *L, const char *filename, const char *mode) { LoadF lf; int status, readstatus; @@ -970,3 +970,123 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { ver, *v); } +// use clonefunction + +#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} +#define UNLOCK(q) __sync_lock_release(&(q)->lock); + +struct codecache { + int lock; + lua_State *L; +}; + +static struct codecache CC = { 0 , NULL }; + +static void +clearcache() { + if (CC.L == NULL) + return; + LOCK(&CC) + lua_close(CC.L); + CC.L = luaL_newstate(); + UNLOCK(&CC) +} + +static void +init() { + CC.lock = 0; + CC.L = luaL_newstate(); +} + +static const void * +load(const char *key) { + if (CC.L == NULL) + return NULL; + LOCK(&CC) + lua_State *L = CC.L; + lua_pushstring(L, key); + lua_rawget(L, LUA_REGISTRYINDEX); + const void * result = lua_touserdata(L, -1); + lua_pop(L, 1); + UNLOCK(&CC) + + return result; +} + +static const void * +save(const char *key, const void * proto) { + lua_State *L; + const void * result = NULL; + + LOCK(&CC) + if (CC.L == NULL) { + init(); + L = CC.L; + } else { + L = CC.L; + lua_pushstring(L, key); + lua_pushvalue(L, -1); + lua_rawget(L, LUA_REGISTRYINDEX); + result = lua_touserdata(L, -1); /* stack: key oldvalue */ + if (result == NULL) { + lua_pop(L,1); + lua_pushlightuserdata(L, (void *)proto); + lua_rawset(L, LUA_REGISTRYINDEX); + } else { + lua_pop(L,2); + } + } + UNLOCK(&CC) + return result; +} + +LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, + const char *mode) { + const void * proto = load(filename); + if (proto) { + lua_clonefunction(L, proto); + return LUA_OK; + } + lua_State * eL = luaL_newstate(); + if (eL == NULL) { + lua_pushliteral(L, "New state failed"); + return LUA_ERRMEM; + } + int err = luaL_loadfilex_(eL, filename, mode); + if (err != LUA_OK) { + size_t sz = 0; + const char * msg = lua_tolstring(eL, -1, &sz); + lua_pushlstring(L, msg, sz); + lua_close(eL); + return err; + } + proto = lua_topointer(eL, -1); + const void * oldv = save(filename, proto); + if (oldv) { + lua_close(eL); + lua_clonefunction(L, oldv); + } else { + lua_clonefunction(L, proto); + /* Never close it. notice: memory leak */ + } + + return LUA_OK; +} + +static int +cache_clear(lua_State *L) { + (void)(L); + clearcache(); + return 0; +} + +LUAMOD_API int luaopen_cache(lua_State *L) { + luaL_Reg l[] = { + { "clear", cache_clear }, + { NULL, NULL }, + }; + luaL_newlib(L,l); + lua_getglobal(L, "loadfile"); + lua_setfield(L, -2, "loadfile"); + return 1; +} \ No newline at end of file diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index d6f0fcd8..00043462 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -55,7 +55,7 @@ void luaK_nil (FuncState *fs, int from, int n) { Instruction *previous; int l = from + n - 1; /* last register to set nil */ if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - previous = &fs->f->code[fs->pc-1]; + previous = &fs->f->sp->code[fs->pc-1]; if (GET_OPCODE(*previous) == OP_LOADNIL) { int pfrom = GETARG_A(*previous); int pl = pfrom + GETARG_B(*previous); @@ -95,7 +95,7 @@ static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->code[pc]; + Instruction *jmp = &fs->f->sp->code[pc]; int offset = dest-(pc+1); lua_assert(dest != NO_JUMP); if (abs(offset) > MAXARG_sBx) @@ -115,7 +115,7 @@ int luaK_getlabel (FuncState *fs) { static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->code[pc]); + int offset = GETARG_sBx(fs->f->sp->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ return NO_JUMP; /* end of list */ else @@ -124,7 +124,7 @@ static int getjump (FuncState *fs, int pc) { static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->code[pc]; + Instruction *pi = &fs->f->sp->code[pc]; if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) return pi-1; else @@ -197,10 +197,10 @@ void luaK_patchclose (FuncState *fs, int list, int level) { level++; /* argument is +1 to reserve 0 as non-op */ while (list != NO_JUMP) { int next = getjump(fs, list); - lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && - (GETARG_A(fs->f->code[list]) == 0 || - GETARG_A(fs->f->code[list]) >= level)); - SETARG_A(fs->f->code[list], level); + lua_assert(GET_OPCODE(fs->f->sp->code[list]) == OP_JMP && + (GETARG_A(fs->f->sp->code[list]) == 0 || + GETARG_A(fs->f->sp->code[list]) >= level)); + SETARG_A(fs->f->sp->code[list], level); list = next; } } @@ -230,13 +230,13 @@ static int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; dischargejpc(fs); /* 'pc' will change */ /* put new instruction in code array */ - luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, + luaM_growvector(fs->ls->L, f->sp->code, fs->pc, f->sp->sizecode, Instruction, MAX_INT, "opcodes"); - f->code[fs->pc] = i; + f->sp->code[fs->pc] = i; /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, + luaM_growvector(fs->ls->L, f->sp->lineinfo, fs->pc, f->sp->sizelineinfo, int, MAX_INT, "opcodes"); - f->lineinfo[fs->pc] = fs->ls->lastline; + f->sp->lineinfo[fs->pc] = fs->ls->lastline; return fs->pc++; } @@ -277,11 +277,11 @@ int luaK_codek (FuncState *fs, int reg, int k) { void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; - if (newstack > fs->f->maxstacksize) { + if (newstack > fs->f->sp->maxstacksize) { if (newstack >= MAXREGS) luaX_syntaxerror(fs->ls, "function or expression needs too many registers"); - fs->f->maxstacksize = cast_byte(newstack); + fs->f->sp->maxstacksize = cast_byte(newstack); } } @@ -323,13 +323,13 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { return k; /* reuse index */ } /* constant not found; create a new entry */ - oldsize = f->sizek; + oldsize = f->sp->sizek; k = fs->nk; /* numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setivalue(idx, k); - luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); + luaM_growvector(L, f->k, k, f->sp->sizek, TValue, MAXARG_Ax, "constants"); + while (oldsize < f->sp->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); fs->nk++; luaC_barrier(L, f, v); @@ -934,7 +934,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, void luaK_fixline (FuncState *fs, int line) { - fs->f->lineinfo[fs->pc - 1] = line; + fs->f->sp->lineinfo[fs->pc - 1] = line; } diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index 43ab86db..b4c865f9 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -40,7 +40,7 @@ typedef enum BinOpr { typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; -#define getcode(fs,e) ((fs)->f->code[(e)->u.info]) +#define getcode(fs,e) ((fs)->f->sp->code[(e)->u.info]) #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index f76582c5..2b444fbf 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -118,14 +118,14 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { static const char *upvalname (Proto *p, int uv) { - TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); + TString *s = check_exp(uv < p->sizeupvalues, p->sp->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); } static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - int nparams = clLvalue(ci->func)->p->numparams; + int nparams = clLvalue(ci->func)->p->sp->numparams; if (n >= cast_int(ci->u.l.base - ci->func) - nparams) return NULL; /* no such vararg */ else { @@ -209,7 +209,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { ar->what = "C"; } else { - Proto *p = cl->l.p; + SharedProto *p = cl->l.p->sp; ar->source = p->source ? getstr(p->source) : "=?"; ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; @@ -227,12 +227,12 @@ static void collectvalidlines (lua_State *L, Closure *f) { else { int i; TValue v; - int *lineinfo = f->l.p->lineinfo; + int *lineinfo = f->l.p->sp->lineinfo; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue(L, L->top, t); /* push it on stack */ api_incr_top(L); setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ + for (i = 0; i < f->l.p->sp->sizelineinfo; i++) /* for all lines with code */ luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ } } @@ -258,8 +258,8 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, ar->nparams = 0; } else { - ar->isvararg = f->l.p->is_vararg; - ar->nparams = f->l.p->numparams; + ar->isvararg = f->l.p->sp->is_vararg; + ar->nparams = f->l.p->sp->numparams; } break; } @@ -370,7 +370,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) { int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->code[pc]; + Instruction i = p->sp->code[pc]; OpCode op = GET_OPCODE(i); int a = GETARG_A(i); switch (op) { @@ -420,7 +420,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, /* else try symbolic execution */ pc = findsetreg(p, lastpc, reg); if (pc != -1) { /* could find instruction? */ - Instruction i = p->code[pc]; + Instruction i = p->sp->code[pc]; OpCode op = GET_OPCODE(i); switch (op) { case OP_MOVE: { @@ -446,7 +446,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, case OP_LOADK: case OP_LOADKX: { int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); + : GETARG_Ax(p->sp->code[pc + 1]); if (ttisstring(&p->k[b])) { *name = svalue(&p->k[b]); return "constant"; @@ -469,7 +469,7 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { TMS tm = (TMS)0; /* to avoid warnings */ Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ - Instruction i = p->code[pc]; /* calling instruction */ + Instruction i = p->sp->code[pc]; /* calling instruction */ if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; @@ -632,7 +632,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); if (isLua(ci)) /* if Lua function, add source:line information */ - luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); + luaG_addinfo(L, msg, ci_func(ci)->p->sp->source, currentline(ci)); luaG_errormsg(L); } diff --git a/3rd/lua/ldebug.h b/3rd/lua/ldebug.h index 0e31546b..0579311b 100644 --- a/3rd/lua/ldebug.h +++ b/3rd/lua/ldebug.h @@ -11,9 +11,9 @@ #include "lstate.h" -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) +#define pcRel(pc, p) (cast(int, (pc) - (p)->sp->code) - 1) -#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) +#define getfuncline(f,pc) (((f)->sp->lineinfo) ? (f)->sp->lineinfo[pc] : -1) #define resethookcount(L) (L->hookcount = L->basehookcount) diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 5c93a259..825f8c20 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -269,7 +269,7 @@ static void callhook (lua_State *L, CallInfo *ci) { } -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { +static StkId adjust_varargs (lua_State *L, SharedProto *p, int actual) { int i; int nfixargs = p->numparams; StkId base, fixed; @@ -342,7 +342,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { } case LUA_TLCL: { /* Lua function: prepare its call */ StkId base; - Proto *p = clLvalue(func)->p; + SharedProto *p = clLvalue(func)->p->sp; n = cast_int(L->top - func) - 1; /* number of real arguments */ luaC_checkGC(L); /* stack grow uses memory */ luaD_checkstack(L, p->maxstacksize); diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index 4c04812a..bcdf9b9b 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -87,7 +87,7 @@ static void DumpString (const TString *s, DumpState *D) { } -static void DumpCode (const Proto *f, DumpState *D) { +static void DumpCode (const SharedProto *f, DumpState *D) { DumpInt(f->sizecode, D); DumpVector(f->code, f->sizecode, D); } @@ -97,7 +97,7 @@ static void DumpFunction(const Proto *f, TString *psource, DumpState *D); static void DumpConstants (const Proto *f, DumpState *D) { int i; - int n = f->sizek; + int n = f->sp->sizek; DumpInt(n, D); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; @@ -127,14 +127,14 @@ static void DumpConstants (const Proto *f, DumpState *D) { static void DumpProtos (const Proto *f, DumpState *D) { int i; - int n = f->sizep; + int n = f->sp->sizep; DumpInt(n, D); for (i = 0; i < n; i++) - DumpFunction(f->p[i], f->source, D); + DumpFunction(f->p[i], f->sp->source, D); } -static void DumpUpvalues (const Proto *f, DumpState *D) { +static void DumpUpvalues (const SharedProto *f, DumpState *D) { int i, n = f->sizeupvalues; DumpInt(n, D); for (i = 0; i < n; i++) { @@ -144,7 +144,7 @@ static void DumpUpvalues (const Proto *f, DumpState *D) { } -static void DumpDebug (const Proto *f, DumpState *D) { +static void DumpDebug (const SharedProto *f, DumpState *D) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; DumpInt(n, D); @@ -163,7 +163,8 @@ static void DumpDebug (const Proto *f, DumpState *D) { } -static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { +static void DumpFunction (const Proto *fp, TString *psource, DumpState *D) { + const SharedProto *f = fp->sp; if (D->strip || f->source == psource) DumpString(NULL, D); /* no debug info or same source as its parent */ else @@ -174,9 +175,9 @@ static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { DumpByte(f->is_vararg, D); DumpByte(f->maxstacksize, D); DumpCode(f, D); - DumpConstants(f, D); + DumpConstants(fp, D); DumpUpvalues(f, D); - DumpProtos(f, D); + DumpProtos(fp, D); DumpDebug(f, D); } @@ -208,7 +209,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, D.strip = strip; D.status = 0; DumpHeader(&D); - DumpByte(f->sizeupvalues, &D); + DumpByte(f->sp->sizeupvalues, &D); DumpFunction(f, NULL, &D); return D.status; } diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index 67967dab..ae603c8f 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -96,39 +96,51 @@ void luaF_close (lua_State *L, StkId level) { } -Proto *luaF_newproto (lua_State *L) { +Proto *luaF_newproto (lua_State *L, SharedProto *sp) { GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); Proto *f = gco2p(o); + f->sp = NULL; f->k = NULL; - f->sizek = 0; f->p = NULL; - f->sizep = 0; - f->code = NULL; f->cache = NULL; - f->sizecode = 0; - f->lineinfo = NULL; - f->sizelineinfo = 0; - f->upvalues = NULL; - f->sizeupvalues = 0; - f->numparams = 0; - f->is_vararg = 0; - f->maxstacksize = 0; - f->locvars = NULL; - f->sizelocvars = 0; - f->linedefined = 0; - f->lastlinedefined = 0; - f->source = NULL; + if (sp == NULL) { + sp = luaM_new(L, SharedProto); + sp->l_G = G(L); + sp->sizek = 0; + sp->sizep = 0; + sp->code = NULL; + sp->sizecode = 0; + sp->lineinfo = NULL; + sp->sizelineinfo = 0; + sp->upvalues = NULL; + sp->sizeupvalues = 0; + sp->numparams = 0; + sp->is_vararg = 0; + sp->maxstacksize = 0; + sp->locvars = NULL; + sp->sizelocvars = 0; + sp->linedefined = 0; + sp->lastlinedefined = 0; + sp->source = NULL; + } + f->sp = sp; return f; } -void luaF_freeproto (lua_State *L, Proto *f) { +static void freesharedproto (lua_State *L, SharedProto *f) { + if (f == NULL || G(L) != f->l_G) + return; luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->p, f->sizep); - luaM_freearray(L, f->k, f->sizek); luaM_freearray(L, f->lineinfo, f->sizelineinfo); luaM_freearray(L, f->locvars, f->sizelocvars); luaM_freearray(L, f->upvalues, f->sizeupvalues); +} + +void luaF_freeproto (lua_State *L, Proto *f) { + luaM_freearray(L, f->p, f->sp->sizep); + luaM_freearray(L, f->k, f->sp->sizek); + freesharedproto(L, f->sp); luaM_free(L, f); } @@ -137,8 +149,9 @@ void luaF_freeproto (lua_State *L, Proto *f) { ** Look for n-th local variable at line 'line' in function 'func'. ** Returns NULL if not found. */ -const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { +const char *luaF_getlocalname (const Proto *fp, int local_number, int pc) { int i; + const SharedProto *f = fp->sp; for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { if (pc < f->locvars[i].endpc) { /* is variable active? */ local_number--; diff --git a/3rd/lua/lfunc.h b/3rd/lua/lfunc.h index 2eeb0d5a..21cbe34e 100644 --- a/3rd/lua/lfunc.h +++ b/3rd/lua/lfunc.h @@ -47,7 +47,7 @@ struct UpVal { #define upisopen(up) ((up)->v != &(up)->u.value) -LUAI_FUNC Proto *luaF_newproto (lua_State *L); +LUAI_FUNC Proto *luaF_newproto (lua_State *L, SharedProto *sp); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 973c269f..e73c58bf 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -465,6 +465,20 @@ static lu_mem traversetable (global_State *g, Table *h) { sizeof(Node) * cast(size_t, sizenode(h)); } +static int marksharedproto (global_State *g, SharedProto *f) { + int i; + if (g != f->l_G) + return 0; + markobjectN(g, f->source); + for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ + markobjectN(g, f->upvalues[i].name); + for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ + markobjectN(g, f->locvars[i].varname); + return sizeof(Instruction) * f->sizecode + + sizeof(int) * f->sizelineinfo + + sizeof(LocVar) * f->sizelocvars + + sizeof(Upvaldesc) * f->sizeupvalues; +} /* ** Traverse a prototype. (While a prototype is being build, its @@ -475,21 +489,13 @@ static int traverseproto (global_State *g, Proto *f) { int i; if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */ - markobjectN(g, f->source); - for (i = 0; i < f->sizek; i++) /* mark literals */ + for (i = 0; i < f->sp->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); - for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobjectN(g, f->upvalues[i].name); - for (i = 0; i < f->sizep; i++) /* mark nested protos */ + for (i = 0; i < f->sp->sizep; i++) /* mark nested protos */ markobjectN(g, f->p[i]); - for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobjectN(g, f->locvars[i].varname); - return sizeof(Proto) + sizeof(Instruction) * f->sizecode + - sizeof(Proto *) * f->sizep + - sizeof(TValue) * f->sizek + - sizeof(int) * f->sizelineinfo + - sizeof(LocVar) * f->sizelocvars + - sizeof(Upvaldesc) * f->sizeupvalues; + return sizeof(Proto) + sizeof(Proto *) * f->sp->sizep + + sizeof(TValue) * f->sp->sizek + + marksharedproto(g, f->sp); } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 9230b7a9..ccb778db 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -404,12 +404,7 @@ typedef struct LocVar { int endpc; /* first point where variable is dead */ } LocVar; - -/* -** Function Prototypes -*/ -typedef struct Proto { - CommonHeader; +typedef struct SharedProto { lu_byte numparams; /* number of fixed parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ @@ -421,14 +416,23 @@ typedef struct Proto { int sizelocvars; int linedefined; int lastlinedefined; - TValue *k; /* constants used by the function */ + void *l_G; /* global state belongs to */ Instruction *code; /* opcodes */ - struct Proto **p; /* functions defined inside the function */ int *lineinfo; /* map from opcodes to source lines (debug information) */ LocVar *locvars; /* information about local variables (debug information) */ Upvaldesc *upvalues; /* upvalue information */ - struct LClosure *cache; /* last-created closure with this prototype */ TString *source; /* used for debug information */ +} SharedProto; + +/* +** Function Prototypes +*/ +typedef struct Proto { + CommonHeader; + struct SharedProto *sp; + TValue *k; /* constants used by the function */ + struct Proto **p; /* functions defined inside the function */ + struct LClosure *cache; /* last-created closure with this prototype */ GCObject *gclist; } Proto; diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 9a54dfc9..0bed0dd3 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -79,7 +79,7 @@ static l_noret error_expected (LexState *ls, int token) { static l_noret errorlimit (FuncState *fs, int limit, const char *what) { lua_State *L = fs->ls->L; const char *msg; - int line = fs->f->linedefined; + int line = fs->f->sp->linedefined; const char *where = (line == 0) ? "main function" : luaO_pushfstring(L, "function at line %d", line); @@ -160,13 +160,14 @@ static void checkname (LexState *ls, expdesc *e) { static int registerlocalvar (LexState *ls, TString *varname) { FuncState *fs = ls->fs; - Proto *f = fs->f; + Proto *fp = fs->f; + SharedProto *f = fp->sp; int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, f, varname); + luaC_objbarrier(ls->L, fp, varname); return fs->nlocvars++; } @@ -194,7 +195,7 @@ static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { static LocVar *getlocvar (FuncState *fs, int i) { int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; lua_assert(idx < fs->nlocvars); - return &fs->f->locvars[idx]; + return &fs->f->sp->locvars[idx]; } @@ -216,7 +217,7 @@ static void removevars (FuncState *fs, int tolevel) { static int searchupvalue (FuncState *fs, TString *name) { int i; - Upvaldesc *up = fs->f->upvalues; + Upvaldesc *up = fs->f->sp->upvalues; for (i = 0; i < fs->nups; i++) { if (eqstr(up[i].name, name)) return i; } @@ -225,7 +226,8 @@ static int searchupvalue (FuncState *fs, TString *name) { static int newupvalue (FuncState *fs, TString *name, expdesc *v) { - Proto *f = fs->f; + Proto *fp = fs->f; + SharedProto *f = fp->sp; int oldsize = f->sizeupvalues; checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, @@ -234,7 +236,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { f->upvalues[fs->nups].instack = (v->k == VLOCAL); f->upvalues[fs->nups].idx = cast_byte(v->u.info); f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, f, name); + luaC_objbarrier(fs->ls->L, fp, name); return fs->nups++; } @@ -496,12 +498,12 @@ static Proto *addprototype (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; /* prototype of current function */ - if (fs->np >= f->sizep) { - int oldsize = f->sizep; - luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sizep) f->p[oldsize++] = NULL; + if (fs->np >= f->sp->sizep) { + int oldsize = f->sp->sizep; + luaM_growvector(L, f->p, fs->np, f->sp->sizep, Proto *, MAXARG_Bx, "functions"); + while (oldsize < f->sp->sizep) f->p[oldsize++] = NULL; } - f->p[fs->np++] = clp = luaF_newproto(L); + f->p[fs->np++] = clp = luaF_newproto(L, NULL); luaC_objbarrier(L, f, clp); return clp; } @@ -521,7 +523,7 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - Proto *f; + SharedProto *f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; ls->fs = fs; @@ -536,7 +538,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nactvar = 0; fs->firstlocal = ls->dyd->actvar.n; fs->bl = NULL; - f = fs->f; + f = fs->f->sp; f->source = ls->source; f->maxstacksize = 2; /* registers 0/1 are always valid */ enterblock(fs, bl, 0); @@ -547,20 +549,21 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; + SharedProto *sp = f->sp; luaK_ret(fs, 0, 0); /* final return */ leaveblock(fs); - luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); - f->sizecode = fs->pc; - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); - f->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); - f->sizek = fs->nk; - luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); - f->sizep = fs->np; - luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); - f->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - f->sizeupvalues = fs->nups; + luaM_reallocvector(L, sp->code, sp->sizecode, fs->pc, Instruction); + sp->sizecode = fs->pc; + luaM_reallocvector(L, sp->lineinfo, sp->sizelineinfo, fs->pc, int); + sp->sizelineinfo = fs->pc; + luaM_reallocvector(L, f->k, sp->sizek, fs->nk, TValue); + sp->sizek = fs->nk; + luaM_reallocvector(L, f->p, sp->sizep, fs->np, Proto *); + sp->sizep = fs->np; + luaM_reallocvector(L, sp->locvars, sp->sizelocvars, fs->nlocvars, LocVar); + sp->sizelocvars = fs->nlocvars; + luaM_reallocvector(L, sp->upvalues, sp->sizeupvalues, fs->nups, Upvaldesc); + sp->sizeupvalues = fs->nups; lua_assert(fs->bl == NULL); ls->fs = fs->prev; luaC_checkGC(L); @@ -736,8 +739,8 @@ static void constructor (LexState *ls, expdesc *t) { } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, '}', '{', line); lastlistfield(fs, &cc); - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ + SETARG_B(fs->f->sp->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ + SETARG_C(fs->f->sp->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ } /* }====================================================================== */ @@ -747,7 +750,7 @@ static void constructor (LexState *ls, expdesc *t) { static void parlist (LexState *ls) { /* parlist -> [ param { ',' param } ] */ FuncState *fs = ls->fs; - Proto *f = fs->f; + SharedProto *f = fs->f->sp; int nparams = 0; f->is_vararg = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ @@ -778,7 +781,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { FuncState new_fs; BlockCnt bl; new_fs.f = addprototype(ls); - new_fs.f->linedefined = line; + new_fs.f->sp->linedefined = line; open_func(ls, &new_fs, &bl); checknext(ls, '('); if (ismethod) { @@ -788,7 +791,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) { parlist(ls); checknext(ls, ')'); statlist(ls); - new_fs.f->lastlinedefined = ls->linenumber; + new_fs.f->sp->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); codeclosure(ls, e); close_func(ls); @@ -954,7 +957,7 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, + check_condition(ls, fs->f->sp->is_vararg, "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); break; @@ -1610,7 +1613,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->is_vararg = 1; /* main function is always vararg */ + fs->f->sp->is_vararg = 1; /* main function is always vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ @@ -1630,13 +1633,13 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, lexstate.h = luaH_new(L); /* create table for scanner */ sethvalue(L, L->top, lexstate.h); /* anchor it */ incr_top(L); - funcstate.f = cl->p = luaF_newproto(L); - funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ + funcstate.f = cl->p = luaF_newproto(L, NULL); + funcstate.f->sp->source = luaS_new(L, name); /* create and anchor TString */ lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; - luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); + luaX_setinput(L, &lexstate, z, funcstate.f->sp->source, firstchar); mainfunc(&lexstate, &funcstate); lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 065e72a0..89ab8d38 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -282,6 +282,8 @@ LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); +LUA_API void (lua_clonefunction) (lua_State *L, const void *eL); + /* ** coroutine functions diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index c0c91d01..0ff987b4 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -149,9 +149,9 @@ static const Proto* combine(lua_State* L, int n) for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; + if (f->p[i]->sp->sizeupvalues>0) f->p[i]->sp->upvalues[0].instack=0; } - f->sizelineinfo=0; + f->sp->sizelineinfo=0; return f; } } @@ -282,13 +282,13 @@ static void PrintConstant(const Proto* f, int i) } } -#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") +#define UPVALNAME(x) ((f->sp->upvalues[x].name) ? getstr(f->sp->upvalues[x].name) : "-") #define MYK(x) (-1-(x)) static void PrintCode(const Proto* f) { - const Instruction* code=f->code; - int pc,n=f->sizecode; + const Instruction* code=f->sp->code; + int pc,n=f->sp->sizecode; for (pc=0; pcsource ? getstr(f->source) : "=?"; if (*s=='@' || *s=='=') @@ -414,8 +414,9 @@ static void PrintHeader(const Proto* f) static void PrintDebug(const Proto* f) { + const SharedProto *sp = f->sp; int i,n; - n=f->sizek; + n=sp->sizek; printf("constants (%d) for %p:\n",n,VOID(f)); for (i=0; isizelocvars; + n=sp->sizelocvars; printf("locals (%d) for %p:\n",n,VOID(f)); for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); + i,getstr(sp->locvars[i].varname),sp->locvars[i].startpc+1,sp->locvars[i].endpc+1); } - n=f->sizeupvalues; + n=f->sp->sizeupvalues; printf("upvalues (%d) for %p:\n",n,VOID(f)); for (i=0; iupvalues[i].instack,f->upvalues[i].idx); + i,UPVALNAME(i),sp->upvalues[i].instack,sp->upvalues[i].idx); } } static void PrintFunction(const Proto* f, int full) { - int i,n=f->sizep; - PrintHeader(f); + int i,n=f->sp->sizep; + PrintHeader(f->sp); PrintCode(f); if (full) PrintDebug(f); for (i=0; ip[i],full); diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 510f3258..451c9097 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -100,7 +100,7 @@ static TString *LoadString (LoadState *S) { } -static void LoadCode (LoadState *S, Proto *f) { +static void LoadCode (LoadState *S, SharedProto *f) { int n = LoadInt(S); f->code = luaM_newvector(S->L, n, Instruction); f->sizecode = n; @@ -115,7 +115,7 @@ static void LoadConstants (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->k = luaM_newvector(S->L, n, TValue); - f->sizek = n; + f->sp->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { @@ -149,17 +149,17 @@ static void LoadProtos (LoadState *S, Proto *f) { int i; int n = LoadInt(S); f->p = luaM_newvector(S->L, n, Proto *); - f->sizep = n; + f->sp->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { - f->p[i] = luaF_newproto(S->L); - LoadFunction(S, f->p[i], f->source); + f->p[i] = luaF_newproto(S->L, NULL); + LoadFunction(S, f->p[i], f->sp->source); } } -static void LoadUpvalues (LoadState *S, Proto *f) { +static void LoadUpvalues (LoadState *S, SharedProto *f) { int i, n; n = LoadInt(S); f->upvalues = luaM_newvector(S->L, n, Upvaldesc); @@ -173,7 +173,7 @@ static void LoadUpvalues (LoadState *S, Proto *f) { } -static void LoadDebug (LoadState *S, Proto *f) { +static void LoadDebug (LoadState *S, SharedProto *f) { int i, n; n = LoadInt(S); f->lineinfo = luaM_newvector(S->L, n, int); @@ -195,7 +195,8 @@ static void LoadDebug (LoadState *S, Proto *f) { } -static void LoadFunction (LoadState *S, Proto *f, TString *psource) { +static void LoadFunction (LoadState *S, Proto *fp, TString *psource) { + SharedProto *f = fp->sp; f->source = LoadString(S); if (f->source == NULL) /* no source in dump? */ f->source = psource; /* reuse parent's source */ @@ -205,9 +206,9 @@ static void LoadFunction (LoadState *S, Proto *f, TString *psource) { f->is_vararg = LoadByte(S); f->maxstacksize = LoadByte(S); LoadCode(S, f); - LoadConstants(S, f); + LoadConstants(S, fp); LoadUpvalues(S, f); - LoadProtos(S, f); + LoadProtos(S, fp); LoadDebug(S, f); } @@ -268,7 +269,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, cl = luaF_newLclosure(L, LoadByte(&S)); setclLvalue(L, L->top, cl); incr_top(L); - cl->p = luaF_newproto(L); + cl->p = luaF_newproto(L, NULL); LoadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, buff, cl->p); diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index a45d6cf2..95a0ebe1 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -596,8 +596,8 @@ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { LClosure *c = p->cache; if (c != NULL) { /* is there a cached closure? */ - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; + int nup = p->sp->sizeupvalues; + Upvaldesc *uv = p->sp->upvalues; int i; for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; @@ -617,8 +617,8 @@ static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; + int nup = p->sp->sizeupvalues; + Upvaldesc *uv = p->sp->upvalues; int i; LClosure *ncl = luaF_newLclosure(L, nup); ncl->p = p; @@ -1118,10 +1118,10 @@ void luaV_execute (lua_State *L) { StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ - StkId lim = nci->u.l.base + getproto(nfunc)->numparams; + StkId lim = nci->u.l.base + getproto(nfunc)->sp->numparams; int aux; /* close all upvalues from previous call */ - if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); + if (cl->p->sp->sizep > 0) luaF_close(L, oci->u.l.base); /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); @@ -1137,7 +1137,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_RETURN) { int b = GETARG_B(i); - if (cl->p->sizep > 0) luaF_close(L, base); + if (cl->p->sp->sizep > 0) luaF_close(L, base); b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra)); if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ return; /* external invocation: return */ @@ -1259,7 +1259,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; int j; - int n = cast_int(base - ci->func) - cl->p->numparams - 1; + int n = cast_int(base - ci->func) - cl->p->sp->numparams - 1; if (b < 0) { /* B == 0? */ b = n; /* get all var. arguments */ Protect(luaD_checkstack(L, n)); From 35b35f686c70a3e0fd7a6fb18d4a7bad0fc85e78 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 19 Jun 2015 13:18:27 +0800 Subject: [PATCH 401/729] bugfix: Issue #291 --- skynet-src/skynet_timer.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 3d0421a9..f7d5a1ff 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -117,7 +117,6 @@ move_list(struct timer *T, int level, int idx) { static void timer_shift(struct timer *T) { - LOCK(T); int mask = TIME_NEAR; uint32_t ct = ++T->time; if (ct == 0) { @@ -137,7 +136,6 @@ timer_shift(struct timer *T) { ++i; } } - UNLOCK(T); } static inline void @@ -160,7 +158,6 @@ dispatch_list(struct timer_node *current) { static inline void timer_execute(struct timer *T) { - LOCK(T); int idx = T->time & TIME_NEAR_MASK; while (T->near[idx].head.next) { @@ -170,12 +167,12 @@ timer_execute(struct timer *T) { dispatch_list(current); LOCK(T); } - - UNLOCK(T); } static void timer_update(struct timer *T) { + LOCK(T); + // try to dispatch timeout 0 (rare condition) timer_execute(T); @@ -184,6 +181,7 @@ timer_update(struct timer *T) { timer_execute(T); + UNLOCK(T); } static struct timer * From 57999456ef380cc0add81ba1930f43e877226b34 Mon Sep 17 00:00:00 2001 From: leeonix Date: Sat, 20 Jun 2015 11:29:56 +0800 Subject: [PATCH 402/729] modify Makefile and add lua clean modify service-src/service_gate.c space to tab --- Makefile | 1 + service-src/service_gate.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e553d6c9..2982e62c 100644 --- a/Makefile +++ b/Makefile @@ -131,5 +131,6 @@ clean : cleanall: clean cd 3rd/jemalloc && $(MAKE) clean + cd 3rd/lua && $(MAKE) clean rm -f $(LUA_STATICLIB) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 7c95703d..9f5d98ec 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -135,7 +135,7 @@ _ctrl(struct gate * g, const void * msg, int sz) { skynet_socket_start(ctx, g->listen_id); return; } - if (memcmp(command, "close", i) == 0) { + if (memcmp(command, "close", i) == 0) { if (g->listen_id >= 0) { skynet_socket_close(ctx, g->listen_id); g->listen_id = -1; From f3536bfc53a0d1abdc0fce81cd8157255223f3f6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Jun 2015 10:27:37 +0800 Subject: [PATCH 403/729] not use raw api, fix Issue #293 --- lualib-src/sproto/lsproto.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 3f725b44..50a05f67 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -44,6 +44,10 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { // lua_isinteger is lua 5.3 api #define lua_isinteger lua_isnumber +// work around , use push & lua_gettable may be better +#define lua_geti lua_rawgeti +#define lua_seti lua_rawseti + #endif static int @@ -139,7 +143,7 @@ encode(const struct sproto_arg *args) { lua_insert(L, -2); lua_replace(L, self->iter_index); } else { - lua_rawgeti(L, self->array_index, args->index); + lua_geti(L, self->array_index, args->index); } } else { lua_getfield(L, self->tbl_index, args->tagname); @@ -350,7 +354,7 @@ decode(const struct sproto_arg *args) { luaL_error(L, "Can't find main index (tag=%d) in [%s]", args->mainindex, args->tagname); } lua_pushvalue(L, sub.result_index); - lua_rawset(L, self->array_index); + lua_settable(L, self->array_index); lua_settop(L, sub.result_index-1); return 0; } else { @@ -367,7 +371,7 @@ decode(const struct sproto_arg *args) { luaL_error(L, "Invalid type"); } if (args->index > 0) { - lua_rawseti(L, self->array_index, args->index); + lua_seti(L, self->array_index, args->index); } else { if (self->mainindex_tag == args->tagid) { // This tag is marked, save the value to key_index From 0fb2b21ed558c4a9ecebd12494b49f7e800b7988 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Jun 2015 18:57:47 +0800 Subject: [PATCH 404/729] use atom inc for object id counter --- lualib-src/lua-bson.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 88f1be8e..37b5f7f6 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1135,15 +1135,18 @@ lobjectid(lua_State *L) { } } else { time_t ti = time(NULL); + uint32_t id = oid_counter; + // old_counter is a static var, use atom inc. + __sync_add_and_fetch(&oid_counter,1); + oid[2] = (ti>>24) & 0xff; oid[3] = (ti>>16) & 0xff; oid[4] = (ti>>8) & 0xff; oid[5] = ti & 0xff; memcpy(oid+6 , oid_header, 5); - oid[11] = (oid_counter>>16) & 0xff; - oid[12] = (oid_counter>>8) & 0xff; - oid[13] = oid_counter & 0xff; - ++oid_counter; + oid[11] = (id>>16) & 0xff; + oid[12] = (id>>8) & 0xff; + oid[13] = id & 0xff; } lua_pushlstring( L, (const char *)oid, 14); From 2fa130997f189ce45467c858e69afe802c4bbd6d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 24 Jun 2015 19:50:45 +0800 Subject: [PATCH 405/729] fix atom inc --- lualib-src/lua-bson.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 37b5f7f6..52baa64c 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1135,9 +1135,8 @@ lobjectid(lua_State *L) { } } else { time_t ti = time(NULL); - uint32_t id = oid_counter; // old_counter is a static var, use atom inc. - __sync_add_and_fetch(&oid_counter,1); + uint32_t id = __sync_fetch_and_add(&oid_counter,1); oid[2] = (ti>>24) & 0xff; oid[3] = (ti>>16) & 0xff; From 728abeaeb841ec62cf60e2d9e3ce845ba840e651 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Jun 2015 19:28:42 +0800 Subject: [PATCH 406/729] can't write to listen fd, See issue #294 --- skynet-src/socket_server.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 7c287b90..1b480da7 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -687,7 +687,11 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock so.free_func(request->buffer); return -1; } - assert(s->type != SOCKET_TYPE_PLISTEN && s->type != SOCKET_TYPE_LISTEN); + if (s->type == SOCKET_TYPE_PLISTEN || s->type == SOCKET_TYPE_LISTEN) { + fprintf(stderr, "socket-server: write to listen fd %d.\n", id); + so.free_func(request->buffer); + return -1; + } if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) { if (s->protocol == PROTOCOL_TCP) { int n = write(s->fd, so.buffer, so.sz); @@ -700,6 +704,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock default: fprintf(stderr, "socket-server: write to %d (fd=%d) error :%s.\n",id,s->fd,strerror(errno)); force_close(ss,s,result); + so.free_func(request->buffer); return SOCKET_CLOSE; } } From 6001d9c2ebeea36253a3bdcf7fdcf998f2723d09 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 26 Jun 2015 14:33:33 +0800 Subject: [PATCH 407/729] raise error message from response function, See issue #295 --- lualib/socketchannel.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 4b90193b..282e6283 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -303,7 +303,11 @@ local function wait_for_response(self, response) self.__result_data[co] = nil if result == socket_error then - error(socket_error) + if result_data then + error(result_data) + else + error(socket_error) + end else assert(result, result_data) return result_data From 27ae0a75004dc5b8cb29a4c37418a02cee7c1b38 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 29 Jun 2015 11:11:32 +0800 Subject: [PATCH 408/729] release alpha 8 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 2410fbe3..8f0a1ac4 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v1.0.0-alpha8 (2015-6-29) +----------- +* Update lua 5.3.1 +* Bugfix: skynet exit issue +* Bugfix: timer race condition +* Use atom increment in bson object id +* remove assert when write to a listen fd +* sproto encode doesn't use raw table api + v1.0.0-alpha7 (2015-6-8) ----------- * console support launch snax service From dad66010cd0b3e05208506e8615b664d0bbfd24d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 30 Jun 2015 15:05:49 +0800 Subject: [PATCH 409/729] sproto bugfix, see sproto issue 34 for detail --- lualib-src/sproto/sproto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 11756796..8324d64f 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -152,7 +152,7 @@ struct_field(const uint8_t * stream, size_t sz) { sz -= header; stream += header; for (i=0;i Date: Wed, 1 Jul 2015 11:35:05 +0800 Subject: [PATCH 410/729] update readme --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 59801892..1f2bfb3b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ Run these in different console ## About Lua -Skynet now use Lua 5.3.0 final, http://www.lua.org/ftp/lua-5.3.0.tar.gz +Skynet now use a modify version of lua 5.3.1 (http://www.lua.org/ftp/lua-5.3.1.tar.gz) . + +For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html You can also use the other official Lua version , edit the makefile by yourself . From b646a1b8a34b238340ce4e17756c1b7fc9894490 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 1 Jul 2015 12:51:20 +0800 Subject: [PATCH 411/729] fix login example --- examples/login/gated.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/login/gated.lua b/examples/login/gated.lua index aeaa2013..4a024699 100644 --- a/examples/login/gated.lua +++ b/examples/login/gated.lua @@ -17,7 +17,8 @@ function server.login_handler(uid, secret) end internal_id = internal_id + 1 - local username = msgserver.username(uid, internal_id, servername) + local id = internal_id -- don't use internal_id directly + local username = msgserver.username(uid, id, servername) -- you can use a pool to alloc new agent local agent = skynet.newservice "msgagent" @@ -25,11 +26,11 @@ function server.login_handler(uid, secret) username = username, agent = agent, uid = uid, - subid = internal_id, + subid = id, } -- trash subid (no used) - skynet.call(agent, "lua", "login", uid, internal_id, secret) + skynet.call(agent, "lua", "login", uid, id, secret) users[uid] = u username_map[username] = u @@ -37,7 +38,7 @@ function server.login_handler(uid, secret) msgserver.login(username, secret) -- you should return unique subid - return internal_id + return id end -- call by agent From bd55a58515db02ae1112a22604356d5a7f630aa1 Mon Sep 17 00:00:00 2001 From: cxj Date: Wed, 1 Jul 2015 16:54:20 +0800 Subject: [PATCH 412/729] support sharedata serialization --- lualib-src/lua-seri.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 18b90d0d..6e118230 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -260,8 +260,28 @@ wb_table(lua_State *L, struct write_block *wb, int index, int depth) { if (index < 0) { index = lua_gettop(L) + index + 1; } - int array_size = wb_table_array(L, wb, index, depth); - wb_table_hash(L, wb, index, depth, array_size); + if (luaL_getmetafield(L, index, "__pairs")) { + uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); + wb_push(wb, &n, 1); + lua_pushvalue(L, index); + lua_call(L, 1, 3); + while (1) { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_copy(L, -5, -3); + lua_call(L, 2, 2); + int type = lua_type(L, -2); + if (type == LUA_TNIL) + break; + pack_one(L, wb, -2, depth); + pack_one(L, wb, -1, depth); + lua_pop(L, 1); + } + wb_nil(wb); + } else { + int array_size = wb_table_array(L, wb, index, depth); + wb_table_hash(L, wb, index, depth, array_size); + } } static void From 08dabd51c85a2d3f9b1436d81ec81dfee7516520 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 2 Jul 2015 12:17:18 +0800 Subject: [PATCH 413/729] fix issue #299 --- 3rd/lua/lualib.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index 5165c0fb..c43f8a2d 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -44,6 +44,8 @@ LUAMOD_API int (luaopen_debug) (lua_State *L); #define LUA_LOADLIBNAME "package" LUAMOD_API int (luaopen_package) (lua_State *L); +#define LUA_CACHELIB +LUAMOD_API int (luaopen_cache) (lua_State *L); /* open all previous libraries */ LUALIB_API void (luaL_openlibs) (lua_State *L); From 172356be190bbf8233d6ee722b0d4621172190a9 Mon Sep 17 00:00:00 2001 From: cxj Date: Thu, 2 Jul 2015 13:51:03 +0800 Subject: [PATCH 414/729] fix lua-seric.c wb_table --- lualib-src/lua-seri.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 6e118230..1d12e263 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -260,7 +260,7 @@ wb_table(lua_State *L, struct write_block *wb, int index, int depth) { if (index < 0) { index = lua_gettop(L) + index + 1; } - if (luaL_getmetafield(L, index, "__pairs")) { + if (luaL_getmetafield(L, index, "__pairs") != LUA_TNIL) { uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); wb_push(wb, &n, 1); lua_pushvalue(L, index); From a77d36911dd33f22dc523dac33ba79e5a46ce43d Mon Sep 17 00:00:00 2001 From: cxj Date: Thu, 2 Jul 2015 14:47:50 +0800 Subject: [PATCH 415/729] change wb_table to call wb_table_metapairs for keeping consistent style --- lualib-src/lua-seri.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 1d12e263..0db2267d 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -254,6 +254,27 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a wb_nil(wb); } +static void +wb_table_metapairs(lua_State *L, struct write_block *wb, int index, int depth) { + uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); + wb_push(wb, &n, 1); + lua_pushvalue(L, index); + lua_call(L, 1, 3); + for(;;) { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_copy(L, -5, -3); + lua_call(L, 2, 2); + int type = lua_type(L, -2); + if (type == LUA_TNIL) + break; + pack_one(L, wb, -2, depth); + pack_one(L, wb, -1, depth); + lua_pop(L, 1); + } + wb_nil(wb); +} + static void wb_table(lua_State *L, struct write_block *wb, int index, int depth) { luaL_checkstack(L, LUA_MINSTACK, NULL); @@ -261,23 +282,7 @@ wb_table(lua_State *L, struct write_block *wb, int index, int depth) { index = lua_gettop(L) + index + 1; } if (luaL_getmetafield(L, index, "__pairs") != LUA_TNIL) { - uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); - wb_push(wb, &n, 1); - lua_pushvalue(L, index); - lua_call(L, 1, 3); - while (1) { - lua_pushvalue(L, -2); - lua_pushvalue(L, -2); - lua_copy(L, -5, -3); - lua_call(L, 2, 2); - int type = lua_type(L, -2); - if (type == LUA_TNIL) - break; - pack_one(L, wb, -2, depth); - pack_one(L, wb, -1, depth); - lua_pop(L, 1); - } - wb_nil(wb); + wb_table_metapairs(L, wb, index, depth); } else { int array_size = wb_table_array(L, wb, index, depth); wb_table_hash(L, wb, index, depth, array_size); From d17e6f59fef44510957521842ff8c7ceff4dc4aa Mon Sep 17 00:00:00 2001 From: cxj Date: Thu, 2 Jul 2015 15:02:11 +0800 Subject: [PATCH 416/729] add sharedata lua serialization test --- examples/share.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/share.lua b/examples/share.lua index 21c3bf54..2721d480 100644 --- a/examples/share.lua +++ b/examples/share.lua @@ -37,6 +37,16 @@ skynet.start(function() skynet.error(string.format("b[%d]=%s", k,v)) end + -- test lua serialization + local s = skynet.packstring(obj) + local nobj = skynet.unpack(s) + for k,v in pairs(nobj) do + skynet.error(string.format("nobj[%s]=%s", k,v)) + end + for k,v in ipairs(nobj) do + skynet.error(string.format("nobj.b[%d]=%s", k,v)) + end + for i = 1, 5 do skynet.sleep(100) skynet.error("second " ..i) From d4023c8b6314b4f8017c6bf702875ce16f87b84b Mon Sep 17 00:00:00 2001 From: sanikoyes Date: Thu, 2 Jul 2015 16:25:10 +0800 Subject: [PATCH 417/729] fix invalid fd after reconnected, used in unpack_f --- examples/login/client.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/login/client.lua b/examples/login/client.lua index 4180e7a7..f7c48018 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -131,7 +131,7 @@ local text = "echo" local index = 1 print("connect") -local fd = assert(socket.connect("127.0.0.1", 8888)) +fd = assert(socket.connect("127.0.0.1", 8888)) last = "" local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) @@ -151,7 +151,7 @@ socket.close(fd) index = index + 1 print("connect again") -local fd = assert(socket.connect("127.0.0.1", 8888)) +fd = assert(socket.connect("127.0.0.1", 8888)) last = "" local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) From 55cf9375fc11e816ff1bdc5746a7aaa49591839b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 3 Jul 2015 10:58:40 +0800 Subject: [PATCH 418/729] asterisk(*) should be once or not at all --- lualib/sprotoparser.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 7b0b04e2..03524ac0 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -81,7 +81,7 @@ end local typedef = P { "ALL", - FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^0 * typename * mainkey^0)), + FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^-1 * typename * mainkey^0)), STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}", TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ), SUBPROTO = Ct((C"request" + C"response") * blanks * (typename + V"STRUCT")), From 86bbc9c7011cce8966d192d14ee9c8c8667c35ad Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 7 Jul 2015 12:11:16 +0800 Subject: [PATCH 419/729] off by 1 error --- skynet-src/socket_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 1b480da7..78f9b47b 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1265,7 +1265,7 @@ send_request(struct socket_server *ss, struct request_package *request, char typ static int open_request(struct socket_server *ss, struct request_package *req, uintptr_t opaque, const char *addr, int port) { int len = strlen(addr); - if (len + sizeof(req->u.open) > 256) { + if (len + sizeof(req->u.open) >= 256) { fprintf(stderr, "socket-server : Invalid addr %s.\n",addr); return -1; } From ea2437e138a92a0182f19a3e88c431cf2a985c5b Mon Sep 17 00:00:00 2001 From: sanikoyes Date: Thu, 9 Jul 2015 18:07:32 +0800 Subject: [PATCH 420/729] lunpack optimize, do not unpack twice if r <= osz --- lualib-src/sproto/lsproto.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 50a05f67..16eaa022 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -487,10 +487,10 @@ lunpack(lua_State *L) { return luaL_error(L, "Invalid unpack stream"); if (r > osz) { output = expand_buffer(L, osz, r); + r = sproto_unpack(buffer, sz, output, r); + if (r < 0) + return luaL_error(L, "Invalid unpack stream"); } - r = sproto_unpack(buffer, sz, output, r); - if (r < 0) - return luaL_error(L, "Invalid unpack stream"); lua_pushlstring(L, output, r); return 1; } From d9ad981c8c3f55965327f7869038dad0a026cfa0 Mon Sep 17 00:00:00 2001 From: cxj Date: Fri, 10 Jul 2015 20:21:07 +0800 Subject: [PATCH 421/729] sproto optimize, malloc newchunk and return directly if sz >= CHUNK_SIZE in pool_alloc, do not replace current chunk if sz >= p->current_used in pool_alloc, more safety checking --- lualib-src/sproto/sproto.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 8324d64f..b5d61ea6 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -83,7 +83,7 @@ static void * pool_alloc(struct pool *p, size_t sz) { // align by 8 sz = (sz + 7) & ~7; - if (sz > CHUNK_SIZE) { + if (sz >= CHUNK_SIZE) { return pool_newchunk(p, sz); } if (p->current == NULL) { @@ -97,7 +97,7 @@ pool_alloc(struct pool *p, size_t sz) { return ret; } - if (sz > p->current_used) { + if (sz >= p->current_used) { return pool_newchunk(p, sz); } else { void * ret = pool_newchunk(p, CHUNK_SIZE); @@ -305,7 +305,7 @@ import_type(struct sproto *s, struct sproto_type *t, const uint8_t * stream) { if (stream == NULL) return NULL; tag = f->tag; - if (tag < last) + if (tag <= last) return NULL; // tag must in ascending order if (tag > last+1) { ++maxn; @@ -394,7 +394,7 @@ create_from_bundle(struct sproto *s, const uint8_t * stream, size_t sz) { const uint8_t * protocoldata = NULL; int fn = struct_field(stream, sz); int i; - if (fn < 0) + if (fn < 0 || fn > 2) return NULL; stream += SIZEOF_HEADER; From 538414408d47af80b29e370db6b512807552b0c4 Mon Sep 17 00:00:00 2001 From: cxj Date: Mon, 13 Jul 2015 13:04:09 +0800 Subject: [PATCH 422/729] sharedata serialization bugfix --- examples/share.lua | 2 +- lualib-src/lua-seri.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/share.lua b/examples/share.lua index 2721d480..8bc4b497 100644 --- a/examples/share.lua +++ b/examples/share.lua @@ -43,7 +43,7 @@ skynet.start(function() for k,v in pairs(nobj) do skynet.error(string.format("nobj[%s]=%s", k,v)) end - for k,v in ipairs(nobj) do + for k,v in ipairs(nobj.b) do skynet.error(string.format("nobj.b[%d]=%s", k,v)) end diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 0db2267d..c0761bd5 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -266,8 +266,10 @@ wb_table_metapairs(lua_State *L, struct write_block *wb, int index, int depth) { lua_copy(L, -5, -3); lua_call(L, 2, 2); int type = lua_type(L, -2); - if (type == LUA_TNIL) + if (type == LUA_TNIL) { + lua_pop(L, 4); break; + } pack_one(L, wb, -2, depth); pack_one(L, wb, -1, depth); lua_pop(L, 1); From a0ecd512307f074164f3c0580b444262ea7a48bb Mon Sep 17 00:00:00 2001 From: LiBei <252608386@qq.com> Date: Tue, 21 Jul 2015 11:12:55 +0800 Subject: [PATCH 423/729] Update cdummy.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 误定义全局å˜é‡ --- service/cdummy.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/service/cdummy.lua b/service/cdummy.lua index 62865222..7926e35e 100644 --- a/service/cdummy.lua +++ b/service/cdummy.lua @@ -4,6 +4,7 @@ require "skynet.manager" -- import skynet.launch, ... local globalname = {} local queryname = {} local harbor = {} +local harbor_service skynet.register_protocol { name = "harbor", From 3f91484791e6be1610633f70b72d1725d803faf4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 21 Jul 2015 11:31:27 +0800 Subject: [PATCH 424/729] user log service --- examples/config.userlog | 18 ++++++++++++++++++ examples/userlog.lua | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 examples/config.userlog create mode 100644 examples/userlog.lua diff --git a/examples/config.userlog b/examples/config.userlog new file mode 100644 index 00000000..97bef6ef --- /dev/null +++ b/examples/config.userlog @@ -0,0 +1,18 @@ +root = "./" +thread = 8 +logger = "userlog" +logservice = "snlua" +logpath = "." +harbor = 1 +address = "127.0.0.1:2526" +master = "127.0.0.1:2013" +start = "main" -- main script +bootstrap = "snlua bootstrap" -- The service for bootstrap +standalone = "0.0.0.0:2013" +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +lualoader = "lualib/loader.lua" +-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run +snax = root.."examples/?.lua;"..root.."test/?.lua" +-- snax_interface_g = "snax_g" +cpath = root.."cservice/?.so" +-- daemon = "./skynet.pid" diff --git a/examples/userlog.lua b/examples/userlog.lua new file mode 100644 index 00000000..55aa0952 --- /dev/null +++ b/examples/userlog.lua @@ -0,0 +1,15 @@ +local skynet = require "skynet" +require "skynet.manager" + +skynet.register_protocol { + name = "text", + id = skynet.PTYPE_TEXT, + unpack = skynet.tostring, + dispatch = function(_, address, msg) + print(string.format("%x(%.2f): %s", address, skynet.time(), msg)) + end +} + +skynet.start(function() + skynet.register ".logger" +end) \ No newline at end of file From 4ebb139c2b930cf0054a6c9914efd958e5d2c627 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 22 Jul 2015 15:52:26 +0800 Subject: [PATCH 425/729] bugfix: free message when cluster not connected --- lualib-src/lua-cluster.c | 3 ++- service/clusterd.lua | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index c4875070..586f74bf 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -74,8 +74,9 @@ lpackrequest(lua_State *L) { return luaL_error(L, "Invalid request message"); } size_t sz = (size_t)luaL_checkinteger(L,4); - int session = luaL_checkinteger(L,2); + int session = luaL_optinteger(L,2,1); // new connection start with 1 if (session <= 0) { + skynet_free(msg); return luaL_error(L, "Invalid request session %d", session); } int addr_type = lua_type(L,1); diff --git a/service/clusterd.lua b/service/clusterd.lua index 12c4c4e7..3805be25 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -24,7 +24,6 @@ local function open_channel(t, key) } assert(c:connect(true)) t[key] = c - node_session[key] = 1 return c end @@ -63,11 +62,11 @@ function command.listen(source, addr, port) end local function send_request(source, node, addr, msg, sz) - local request - local c = node_channel[node] local session = node_session[node] -- msg is a local pointer, cluster.packrequest will free it - request, node_session[node] = cluster.packrequest(addr, session , msg, sz) + local request, new_session = cluster.packrequest(addr, session, msg, sz) + local c = node_channel[node] + node_session[node] = new_session return c:request(request, session) end From 588c4a350800b3676e23cbcf6ffef2a34dd000ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 22 Jul 2015 16:49:25 +0800 Subject: [PATCH 426/729] bugfix --- lualib-src/lua-cluster.c | 2 +- service/clusterd.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 586f74bf..e9d9e142 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -74,7 +74,7 @@ lpackrequest(lua_State *L) { return luaL_error(L, "Invalid request message"); } size_t sz = (size_t)luaL_checkinteger(L,4); - int session = luaL_optinteger(L,2,1); // new connection start with 1 + int session = luaL_checkinteger(L,2); if (session <= 0) { skynet_free(msg); return luaL_error(L, "Invalid request session %d", session); diff --git a/service/clusterd.lua b/service/clusterd.lua index 3805be25..b0ee6335 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -62,7 +62,7 @@ function command.listen(source, addr, port) end local function send_request(source, node, addr, msg, sz) - local session = node_session[node] + local session = node_session[node] or 1 -- msg is a local pointer, cluster.packrequest will free it local request, new_session = cluster.packrequest(addr, session, msg, sz) local c = node_channel[node] From 39f9a96b6c3c8276d715d8a921f17767efe1dfb5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 23 Jul 2015 16:47:35 +0800 Subject: [PATCH 427/729] add cmemory.info() returns a table of c memory info --- lualib-src/lua-memory.c | 1 + service/cmemory.lua | 6 +++++- skynet-src/malloc_hook.c | 15 +++++++++++++++ skynet-src/malloc_hook.h | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index 17e7cd40..67a3acf6 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -42,6 +42,7 @@ luaopen_memory(lua_State *L) { { "block", lblock }, { "dumpinfo", ldumpinfo }, { "dump", ldump }, + { "info", dump_mem_lua }, { NULL, NULL }, }; diff --git a/service/cmemory.lua b/service/cmemory.lua index 06627ae3..e33eb42f 100644 --- a/service/cmemory.lua +++ b/service/cmemory.lua @@ -2,7 +2,11 @@ local skynet = require "skynet" local memory = require "memory" memory.dumpinfo() -memory.dump() +--memory.dump() +local info = memory.info() +for k,v in pairs(info) do + print(string.format(":%08x %gK",k,v/1024)) +end print("Total memory:", memory.total()) print("Total block:", memory.block()) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 9b3949a6..d9ad6f32 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "malloc_hook.h" #include "skynet.h" @@ -224,3 +225,17 @@ skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize) { return skynet_realloc(ptr, nsize); } } + +int +dump_mem_lua(lua_State *L) { + int i; + lua_newtable(L); + for(i=0; ihandle != 0 && data->allocated != 0) { + lua_pushinteger(L, data->allocated); + lua_rawseti(L, -2, (lua_Integer)data->handle); + } + } + return 1; +} diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index d35551d1..03339556 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -2,6 +2,7 @@ #define SKYNET_MALLOC_HOOK_H #include +#include extern size_t malloc_used_memory(void); extern size_t malloc_memory_block(void); @@ -9,6 +10,7 @@ extern void memory_info_dump(void); extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); extern void dump_c_mem(void); +extern int dump_mem_lua(lua_State *L); #endif /* SKYNET_MALLOC_HOOK_H */ From b2d82373639cf2b3902f23078559b9945d19c023 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 23 Jul 2015 18:23:47 +0800 Subject: [PATCH 428/729] improve unregister protocol error report --- lualib/skynet.lua | 20 ++++++++++++++++---- lualib/skynet/debug.lua | 4 ++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 02a966fb..cbc16034 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -415,9 +415,13 @@ function skynet.wakeup(co) end function skynet.dispatch(typename, func) - local p = assert(proto[typename],tostring(typename)) - assert(p.dispatch == nil, tostring(typename)) - p.dispatch = func + local p = proto[typename] + if func then + assert(p and (p.dispatch == nil), tostring(typename)) + p.dispatch = func + else + return p and p.dispatch + end end local function unknown_request(session, address, msg, sz, prototype) @@ -466,7 +470,15 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) suspend(co, coroutine.resume(co, true, msg, sz)) end else - local p = assert(proto[prototype], prototype) + local p = proto[prototype] + if p == nil then + if session ~= 0 then + c.send(source, skynet.PTYPE_ERROR, session, "") + else + unknown_request(session, source, msg, sz, prototype) + end + return + end local f = p.dispatch if f then local ref = watching_service[source] diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 0bcb8edb..6c040c24 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -63,6 +63,10 @@ function dbgcmd.REMOTEDEBUG(...) remotedebug.start(export, ...) end +function dbgcmd.SUPPORT(pname) + return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil)) +end + local function _debug_dispatch(session, address, cmd, ...) local f = dbgcmd[cmd] assert(f, cmd) From 1da8a6b38d31b493b64834a52ea3705f6ac3dd0d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 23 Jul 2015 18:35:29 +0800 Subject: [PATCH 429/729] lazy init debug command --- lualib/skynet/debug.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 6c040c24..635bdb31 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -10,7 +10,10 @@ function skynet.info_func(func) internal_info_func = func end -local dbgcmd = {} +local dbgcmd + +local function init_dbgcmd() +dbgcmd = {} function dbgcmd.MEM() local kb, bytes = collectgarbage "count" @@ -67,8 +70,11 @@ function dbgcmd.SUPPORT(pname) return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil)) end +return dbgcmd +end -- function init_dbgcmd + local function _debug_dispatch(session, address, cmd, ...) - local f = dbgcmd[cmd] + local f = (dbgcmd or init_dbgcmd())[cmd] -- lazy init dbgcmd assert(f, cmd) f(...) end From 0ee61de22ceb4c32a76ab26c67daef94e685d094 Mon Sep 17 00:00:00 2001 From: jintiao Date: Fri, 24 Jul 2015 00:48:24 +0800 Subject: [PATCH 430/729] skip jemalloc clean if jemalloc's makefile was missing --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 2982e62c..b92a1a33 100644 --- a/Makefile +++ b/Makefile @@ -130,7 +130,9 @@ clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so cleanall: clean +ifneq (,$(wildcard 3rd/jemalloc/Makefile)) cd 3rd/jemalloc && $(MAKE) clean +endif cd 3rd/lua && $(MAKE) clean rm -f $(LUA_STATICLIB) From e87ac18b84339a0baa3432b9b352530dca00eb14 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 24 Jul 2015 17:50:42 +0800 Subject: [PATCH 431/729] expand lua stack first --- lualib-src/lua-sharedata.c | 1 + service-src/service_snlua.c | 21 ++++++++++++++++++++- skynet-src/malloc_hook.c | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 6ef10f0a..e7a27c57 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -376,6 +376,7 @@ pconv(lua_State *L) { static void convert_stringmap(struct context *ctx, struct table *tbl) { lua_State *L = ctx->L; + lua_checkstack(L, ctx->string_index + LUA_MINSTACK); lua_settop(L, ctx->string_index + 1); lua_pushvalue(L, 1); struct state * s = lua_newuserdata(L, sizeof(*s)); diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 89e607f6..b839b117 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -12,6 +12,7 @@ struct snlua { lua_State * L; struct skynet_context * ctx; + FILE *f; }; // LUA_CACHELIB may defined in patched lua for shared proto @@ -143,17 +144,35 @@ snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) { return 0; } +static void * +lalloc(void *ud, void *ptr, size_t osize, size_t nsize) { + struct snlua * l = ud; + if (l->f) { + fprintf(l->f, "%p %d %d\n", ptr, (int)osize, (int)nsize); + } + if (nsize == 0) { + skynet_free(ptr); + return NULL; + } else { + return skynet_realloc(ptr, nsize); + } +} + struct snlua * snlua_create(void) { struct snlua * l = skynet_malloc(sizeof(*l)); memset(l,0,sizeof(*l)); - l->L = lua_newstate(skynet_lalloc, NULL); + char tmp[L_tmpnam]; + l->f = fopen(tmpnam(tmp),"wb"); +// printf("%s\n", tmp); + l->L = lua_newstate(lalloc, l); return l; } void snlua_release(struct snlua *l) { lua_close(l->L); + fclose(l->f); skynet_free(l); } diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index d9ad6f32..3cff09fa 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -218,6 +218,7 @@ skynet_strdup(const char *str) { void * skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize) { + printf("%p osize = %d nsize = %d\n", ptr, (int)osize, (int)nsize); if (nsize == 0) { skynet_free(ptr); return NULL; From b3f966bcaa00f5e8f3003fc24df7cb705122e356 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Jul 2015 16:10:38 +0800 Subject: [PATCH 432/729] bugfix: multicast chan gc --- lualib/multicast.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/multicast.lua b/lualib/multicast.lua index 1f5fbd15..181979cb 100644 --- a/lualib/multicast.lua +++ b/lualib/multicast.lua @@ -8,7 +8,9 @@ local dispatch = setmetatable({} , {__mode = "kv" }) local chan = {} local chan_meta = { __index = chan, - __gc = unsubscribe, + __gc = function(self) + self:unsubscribe() + end, __tostring = function (self) return string.format("[Multicast:%x]",self.channel) end, From 4cbead28d5ec78db8fd881a21d89291e65b0d813 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Jul 2015 16:12:49 +0800 Subject: [PATCH 433/729] skynet.dispatch can replace old func --- lualib/skynet.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index cbc16034..8906f60d 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -417,8 +417,9 @@ end function skynet.dispatch(typename, func) local p = proto[typename] if func then - assert(p and (p.dispatch == nil), tostring(typename)) + local ret = p.dispatch p.dispatch = func + return ret else return p and p.dispatch end From 3b48beb42808f82264081dc5b577f87e08cc42be Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 30 Jul 2015 15:41:17 +0800 Subject: [PATCH 434/729] bson bugfix --- lualib-src/lua-bson.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 52baa64c..9071f56d 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -243,12 +243,16 @@ append_key(struct bson *bs, int type, const char *key, size_t sz) { write_string(bs, key, sz); } +static inline int +is_32bit(int64_t v) { + return v >= INT32_MIN && v <= INT32_MAX; +} + static void append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { if (lua_isinteger(L, -1)) { int64_t i = lua_tointeger(L, -1); - int si = i >> 31; - if (si == 0 || si == -1) { + if (is_32bit(i)) { append_key(bs, BSON_INT32, key, sz); write_int32(bs, i); } else { From 704b016f2cec9ba7d0799c8bd1408b613fef0e64 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 4 Aug 2015 17:51:35 +0800 Subject: [PATCH 435/729] bugfix: inc session before get channel --- service/clusterd.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/service/clusterd.lua b/service/clusterd.lua index b0ee6335..fc8b4126 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -65,9 +65,11 @@ local function send_request(source, node, addr, msg, sz) local session = node_session[node] or 1 -- msg is a local pointer, cluster.packrequest will free it local request, new_session = cluster.packrequest(addr, session, msg, sz) - local c = node_channel[node] node_session[node] = new_session + -- node_channel[node] may yield or throw error + local c = node_channel[node] + return c:request(request, session) end From b5244b96aa119c2f99f3647dc7c9c63abe84e551 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 4 Aug 2015 16:47:55 +0800 Subject: [PATCH 436/729] cluster rpc support large package --- examples/cluster1.lua | 1 + examples/cluster2.lua | 7 +- lualib-src/lua-cluster.c | 361 ++++++++++++++++++++++++++++++++++----- lualib/socketchannel.lua | 68 ++++++-- service/clusterd.lua | 48 +++++- 5 files changed, 418 insertions(+), 67 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 9ba8fcbf..d44899ba 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -6,6 +6,7 @@ local snax = require "snax" skynet.start(function() local sdb = skynet.newservice("simpledb") skynet.name(".simpledb", sdb) + print(skynet.call(".simpledb", "lua", "SET", "a", "foobar")) print(skynet.call(".simpledb", "lua", "SET", "b", "foobar2")) print(skynet.call(".simpledb", "lua", "GET", "a")) diff --git a/examples/cluster2.lua b/examples/cluster2.lua index 09e53076..0aef412c 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -3,7 +3,12 @@ local cluster = require "cluster" skynet.start(function() local proxy = cluster.proxy("db", ".simpledb") - print(skynet.call(proxy, "lua", "GET", "a")) + local largekey = string.rep("X", 128*1024) + local largevalue = string.rep("R", 100 * 1024) + print(skynet.call(proxy, "lua", "SET", largekey, largevalue)) + local v = skynet.call(proxy, "lua", "GET", largekey) + assert(largevalue == v) + print(cluster.call("db", ".simpledb", "GET", "a")) print(cluster.call("db2", ".simpledb", "GET", "b")) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index e9d9e142..c558fb0f 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -15,7 +15,8 @@ uint32_t next_session */ -#define TEMP_LENGTH 0x10007 +#define TEMP_LENGTH 0x8200 +#define MULTI_PART 0x8000 static void fill_uint32(uint8_t * buf, uint32_t n) { @@ -35,21 +36,69 @@ fill_header(lua_State *L, uint8_t *buf, int sz, void *msg) { buf[1] = sz & 0xff; } -static void -packreq_number(lua_State *L, int session, void * msg, size_t sz) { +/* + The request package : + size <= 0x8000 (32K) and address is id + WORD sz+9 + BYTE 0 + DWORD addr + DWORD session + PADDING msg(sz) + size > 0x8000 and address is id + DWORD 13 + BYTE 1 ; multireq + DWORD addr + DWORD session + DWORD sz + + size <= 0x8000 (32K) and address is string + WORD sz+6+namelen + BYTE 0x80 + BYTE namelen + STRING name + DWORD session + PADDING msg(sz) + size > 0x8000 and address is string + DWORD 10 + namelen + BYTE 0x81 + BYTE namelen + STRING name + DWORD session + DWORD sz + + multi req + WORD sz + 5 + BYTE 2/3 ; 2:multipart, 3:multipart end + DWORD SESSION + PADDING msgpart(sz) + */ +static int +packreq_number(lua_State *L, int session, void * msg, uint32_t sz) { uint32_t addr = (uint32_t)lua_tointeger(L,1); uint8_t buf[TEMP_LENGTH]; - fill_header(L, buf, sz+9, msg); - buf[2] = 0; - fill_uint32(buf+3, addr); - fill_uint32(buf+7, (uint32_t)session); - memcpy(buf+11,msg,sz); + if (sz < MULTI_PART) { + fill_header(L, buf, sz+9, msg); + buf[2] = 0; + fill_uint32(buf+3, addr); + fill_uint32(buf+7, (uint32_t)session); + memcpy(buf+11,msg,sz); - lua_pushlstring(L, (const char *)buf, sz+11); + lua_pushlstring(L, (const char *)buf, sz+11); + return 0; + } else { + int part = (sz - 1) / MULTI_PART + 1; + fill_header(L, buf, 13, msg); + buf[2] = 0x80; + fill_uint32(buf+3, addr); + fill_uint32(buf+7, (uint32_t)session); + fill_uint32(buf+11, sz); + lua_pushlstring(L, (const char *)buf, 15); + return part; + } } -static void -packreq_string(lua_State *L, int session, void * msg, size_t sz) { +static int +packreq_string(lua_State *L, int session, void * msg, uint32_t sz) { size_t namelen = 0; const char *name = lua_tolstring(L, 1, &namelen); if (name == NULL || namelen < 1 || namelen > 255) { @@ -58,13 +107,53 @@ packreq_string(lua_State *L, int session, void * msg, size_t sz) { } uint8_t buf[TEMP_LENGTH]; - fill_header(L, buf, sz+5+namelen, msg); - buf[2] = (uint8_t)namelen; - memcpy(buf+3, name, namelen); - fill_uint32(buf+3+namelen, (uint32_t)session); - memcpy(buf+7+namelen,msg,sz); + if (sz < MULTI_PART) { + fill_header(L, buf, sz+6+namelen, msg); + buf[2] = 0x80; + buf[3] = (uint8_t)namelen; + memcpy(buf+4, name, namelen); + fill_uint32(buf+4+namelen, (uint32_t)session); + memcpy(buf+8+namelen,msg,sz); - lua_pushlstring(L, (const char *)buf, sz+7+namelen); + lua_pushlstring(L, (const char *)buf, sz+8+namelen); + return 0; + } else { + int part = (sz - 1) / MULTI_PART + 1; + fill_header(L, buf, 10+namelen, msg); + buf[2] = 0x81; + buf[3] = (uint8_t)namelen; + memcpy(buf+4, name, namelen); + fill_uint32(buf+4+namelen, (uint32_t)session); + fill_uint32(buf+8+namelen, sz); + + lua_pushlstring(L, (const char *)buf, 12+namelen); + return part; + } +} + +static void +packreq_multi(lua_State *L, int session, void * msg, uint32_t sz) { + uint8_t buf[TEMP_LENGTH]; + int part = (sz - 1) / MULTI_PART + 1; + int i; + char *ptr = msg; + for (i=0;i MULTI_PART) { + s = MULTI_PART; + buf[2] = 2; + } else { + s = sz; + buf[2] = 3; // the last multi part + } + fill_header(L, buf, s+5, msg); + fill_uint32(buf+3, (uint32_t)session); + memcpy(buf+7, ptr, s); + lua_pushlstring(L, (const char *)buf, s+7); + lua_rawseti(L, -2, i+1); + sz -= s; + ptr += s; + } } static int @@ -73,24 +162,33 @@ lpackrequest(lua_State *L) { if (msg == NULL) { return luaL_error(L, "Invalid request message"); } - size_t sz = (size_t)luaL_checkinteger(L,4); + uint32_t sz = (uint32_t)luaL_checkinteger(L,4); int session = luaL_checkinteger(L,2); if (session <= 0) { skynet_free(msg); return luaL_error(L, "Invalid request session %d", session); } int addr_type = lua_type(L,1); + int multipak; if (addr_type == LUA_TNUMBER) { - packreq_number(L, session, msg, sz); + multipak = packreq_number(L, session, msg, sz); } else { - packreq_string(L, session, msg, sz); + multipak = packreq_string(L, session, msg, sz); } + int current_session = session; if (++session < 0) { session = 1; } - skynet_free(msg); lua_pushinteger(L, session); - return 2; + if (multipak) { + lua_createtable(L, multipak, 0); + packreq_multi(L, current_session, msg, sz); + skynet_free(msg); + return 3; + } else { + skynet_free(msg); + return 2; + } } /* @@ -99,6 +197,7 @@ lpackrequest(lua_State *L) { uint32_t or string addr int session string msg + boolean padding */ static inline uint32_t @@ -107,44 +206,122 @@ unpack_uint32(const uint8_t * buf) { } static int -unpackreq_number(lua_State *L, const uint8_t * buf, size_t sz) { +unpackreq_number(lua_State *L, const uint8_t * buf, int sz) { if (sz < 9) { - return luaL_error(L, "Invalid cluster message"); + return luaL_error(L, "Invalid cluster message (size=%d)", sz); } uint32_t address = unpack_uint32(buf+1); uint32_t session = unpack_uint32(buf+5); - lua_pushinteger(L, (uint32_t)address); - lua_pushinteger(L, (uint32_t)session); + lua_pushinteger(L, address); + lua_pushinteger(L, session); lua_pushlstring(L, (const char *)buf+9, sz-9); return 3; } static int -unpackreq_string(lua_State *L, const uint8_t * buf, size_t sz) { - size_t namesz = buf[0]; - if (sz < namesz + 5) { - return luaL_error(L, "Invalid cluster message"); +unpackmreq_number(lua_State *L, const uint8_t * buf, int sz) { + if (sz != 15) { + return luaL_error(L, "Invalid cluster message size %d (multi req must be 15)", sz); } - lua_pushlstring(L, (const char *)buf+1, namesz); - uint32_t session = unpack_uint32(buf + namesz + 1); + uint32_t address = unpack_uint32(buf+1); + uint32_t session = unpack_uint32(buf+5); + uint32_t size = unpack_uint32(buf+9); + lua_pushinteger(L, address); + lua_pushinteger(L, session); + lua_pushinteger(L, size); + lua_pushboolean(L, 1); // padding multi part + + return 4; +} + +static int +unpackmreq_part(lua_State *L, const uint8_t * buf, int sz) { + if (sz < 5) { + return luaL_error(L, "Invalid cluster multi part message"); + } + int padding = (buf[0] == 2); + uint32_t session = unpack_uint32(buf+1); + lua_pushboolean(L, 0); // no address + lua_pushinteger(L, session); + lua_pushlstring(L, (const char *)buf+5, sz-5); + lua_pushboolean(L, padding); + + return 4; +} + +static int +unpackreq_string(lua_State *L, const uint8_t * buf, int sz) { + if (sz < 2) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + size_t namesz = buf[1]; + if (sz < namesz + 6) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + lua_pushlstring(L, (const char *)buf+2, namesz); + uint32_t session = unpack_uint32(buf + namesz + 2); lua_pushinteger(L, (uint32_t)session); - lua_pushlstring(L, (const char *)buf+1+namesz+4, sz - namesz - 5); + lua_pushlstring(L, (const char *)buf+2+namesz+4, sz - namesz - 6); return 3; } +static int +unpackmreq_string(lua_State *L, const uint8_t * buf, int sz) { + if (sz < 2) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + size_t namesz = buf[1]; + if (sz < namesz + 10) { + return luaL_error(L, "Invalid cluster message (size=%d)", sz); + } + lua_pushlstring(L, (const char *)buf+2, namesz); + uint32_t session = unpack_uint32(buf + namesz + 2); + uint32_t size = unpack_uint32(buf + namesz + 6); + lua_pushinteger(L, session); + lua_pushinteger(L, size); + lua_pushboolean(L, 1); // padding multipart + + return 4; +} + static int lunpackrequest(lua_State *L) { - size_t sz; - const char *msg = luaL_checklstring(L,1,&sz); - if (msg[0] == 0) { + size_t ssz; + const char *msg = luaL_checklstring(L,1,&ssz); + int sz = (int)ssz; + switch (msg[0]) { + case 0: return unpackreq_number(L, (const uint8_t *)msg, sz); - } else { + case 1: + return unpackmreq_number(L, (const uint8_t *)msg, sz); + case 2: + case 3: + return unpackmreq_part(L, (const uint8_t *)msg, sz); + case '\x80': return unpackreq_string(L, (const uint8_t *)msg, sz); + case '\x81': + return unpackmreq_string(L, (const uint8_t *)msg, sz); + default: + return luaL_error(L, "Invalid req package type %d", msg[0]); } } +/* + DWORD session + BYTE type + 0: error + 1: ok + 2: multi begin + 3: multi part + 4: multi end + PADDING msg + type = 0, error msg + type = 1, msg + type = 2, DWORD size + type = 3/4, msg + */ /* int session boolean ok @@ -163,14 +340,54 @@ lpackresponse(lua_State *L) { if (lua_type(L,3) == LUA_TSTRING) { msg = (void *)lua_tolstring(L, 3, &sz); - if (sz > 0x1000) { - sz = 0x1000; - } } else { msg = lua_touserdata(L,3); sz = (size_t)luaL_checkinteger(L, 4); } + if (!ok) { + if (sz > MULTI_PART) { + // truncate the error msg if too long + sz = MULTI_PART; + } + } else { + if (sz > MULTI_PART) { + // return + int part = (sz - 1) / MULTI_PART + 1; + lua_createtable(L, part+1, 0); + uint8_t buf[TEMP_LENGTH]; + + // multi part begin + fill_header(L, buf, 9, msg); + fill_uint32(buf+2, session); + buf[6] = 2; + fill_uint32(buf+7, (uint32_t)sz); + lua_pushlstring(L, (const char *)buf, 11); + lua_rawseti(L, -2, 1); + + char * ptr = msg; + int i; + for (i=0;i MULTI_PART) { + s = MULTI_PART; + buf[6] = 3; + } else { + s = sz; + buf[6] = 4; + } + fill_header(L, buf, s+5, msg); + fill_uint32(buf+2, session); + memcpy(buf+7,ptr,s); + lua_pushlstring(L, (const char *)buf, s+7); + lua_rawseti(L, -2, i+2); + sz -= s; + ptr += s; + } + return 1; + } + } + uint8_t buf[TEMP_LENGTH]; fill_header(L, buf, sz+5, msg); fill_uint32(buf+2, session); @@ -187,6 +404,7 @@ lpackresponse(lua_State *L) { return integer session boolean ok string msg + boolean padding */ static int lunpackresponse(lua_State *L) { @@ -197,10 +415,66 @@ lunpackresponse(lua_State *L) { } uint32_t session = unpack_uint32((const uint8_t *)buf); lua_pushinteger(L, (lua_Integer)session); - lua_pushboolean(L, buf[4]); - lua_pushlstring(L, buf+5, sz-5); + switch(buf[4]) { + case 0: // error + lua_pushboolean(L, 0); + lua_pushlstring(L, buf+5, sz-5); + return 3; + case 1: // ok + case 4: // multi end + lua_pushboolean(L, 1); + lua_pushlstring(L, buf+5, sz-5); + return 3; + case 2: // multi begin + if (sz != 9) { + return 0; + } + sz = unpack_uint32((const uint8_t *)buf+5); + lua_pushboolean(L, 1); + lua_pushinteger(L, sz); + lua_pushboolean(L, 1); + return 4; + case 3: // multi part + lua_pushboolean(L, 1); + lua_pushlstring(L, buf+5, sz-5); + lua_pushboolean(L, 1); + return 4; + default: + return 0; + } +} - return 3; +static int +lconcat(lua_State *L) { + if (!lua_istable(L,1)) + return 0; + if (lua_geti(L,1,1) != LUA_TNUMBER) + return 0; + int sz = lua_tointeger(L,-1); + lua_pop(L,1); + char * buff = skynet_malloc(sz); + int idx = 2; + int offset = 0; + while(lua_geti(L,1,idx) == LUA_TSTRING) { + size_t s; + const char * str = lua_tolstring(L, -1, &s); + if (s+offset > sz) { + skynet_free(buff); + return 0; + } + memcpy(buff+offset, str, s); + lua_pop(L,1); + offset += s; + ++idx; + } + if (offset != sz) { + skynet_free(buff); + return 0; + } + // buff/sz will send to other service, See clusterd.lua + lua_pushlightuserdata(L, buff); + lua_pushinteger(L, sz); + return 2; } int @@ -210,6 +484,7 @@ luaopen_cluster_core(lua_State *L) { { "unpackrequest", lunpackrequest }, { "packresponse", lpackresponse }, { "unpackresponse", lunpackresponse }, + { "concat", lconcat }, { NULL, NULL }, }; luaL_checkversion(L); diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 282e6283..9ff5d451 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -81,15 +81,27 @@ local function dispatch_by_session(self) local response = self.__response -- response() return session while self.__sock do - local ok , session, result_ok, result_data = pcall(response, self.__sock) + local ok , session, result_ok, result_data, padding = pcall(response, self.__sock) if ok and session then local co = self.__thread[session] - self.__thread[session] = nil if co then - self.__result[co] = result_ok - self.__result_data[co] = result_data - skynet.wakeup(co) + if padding and result_ok then + -- If padding is true, append result_data to a table (self.__result_data[co]) + local result = self.__result_data[co] or {} + self.__result_data[co] = result + table.insert(result, result_data) + else + self.__thread[session] = nil + self.__result[co] = result_ok + if result_ok and self.__result_data[co] then + table.insert(self.__result_data[co], result_data) + else + self.__result_data[co] = result_data + end + skynet.wakeup(co) + end else + self.__thread[session] = nil skynet.error("socket: unknown session :", session) end else @@ -127,11 +139,23 @@ local function dispatch_by_order(self) wakeup_all(self) end else - local ok, result_ok, result_data = pcall(func, self.__sock) + local ok, result_ok, result_data, padding = pcall(func, self.__sock) if ok then - self.__result[co] = result_ok - self.__result_data[co] = result_data - skynet.wakeup(co) + if padding and result_ok then + -- if padding is true, wait for next result_data + -- self.__result_data[co] is a table + local result = self.__result_data[co] or {} + self.__result_data[co] = result + table.insert(result, result_data) + else + self.__result[co] = result_ok + if result_ok and self.__result_data[co] then + table.insert(self.__result_data[co], result_data) + else + self.__result_data[co] = result_data + end + skynet.wakeup(co) + end else close_channel_socket(self) local errmsg @@ -314,13 +338,27 @@ local function wait_for_response(self, response) end end -function channel:request(request, response) - assert(block_connect(self, true)) -- connect once +local socket_write = socket.write +local socket_lwrite = socket.lwrite - if not socket.write(self.__sock[1], request) then - close_channel_socket(self) - wakeup_all(self) - error(socket_error) +function channel:request(request, response, padding) + assert(block_connect(self, true)) -- connect once + local fd = self.__sock[1] + + if padding then + -- padding may be a table, to support multi part request + -- multi part request use low priority socket write + -- socket_lwrite returns nothing + socket_lwrite(fd , request) + for _,v in ipairs(padding) do + socket_lwrite(fd, v) + end + else + if not socket_write(fd , request) then + close_channel_socket(self) + wakeup_all(self) + error(socket_error) + end end if response == nil then diff --git a/service/clusterd.lua b/service/clusterd.lua index fc8b4126..b3c74722 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -11,7 +11,7 @@ local command = {} local function read_response(sock) local sz = socket.header(sock:read(2)) local msg = sock:read(sz) - return cluster.unpackresponse(msg) -- session, ok, data + return cluster.unpackresponse(msg) -- session, ok, data, padding end local function open_channel(t, key) @@ -64,19 +64,23 @@ end local function send_request(source, node, addr, msg, sz) local session = node_session[node] or 1 -- msg is a local pointer, cluster.packrequest will free it - local request, new_session = cluster.packrequest(addr, session, msg, sz) + local request, new_session, padding = cluster.packrequest(addr, session, msg, sz) node_session[node] = new_session -- node_channel[node] may yield or throw error local c = node_channel[node] - return c:request(request, session) + return c:request(request, session, padding) end function command.req(...) local ok, msg, sz = pcall(send_request, ...) if ok then - skynet.ret(msg, sz) + if type(msg) == "table" then + skynet.ret(cluster.concat(msg)) + else + skynet.ret(msg) + end else skynet.error(msg) skynet.response()(false) @@ -93,23 +97,51 @@ function command.proxy(source, node, name) skynet.ret(skynet.pack(proxy[fullname])) end -local request_fd = {} +local large_request = {} function command.socket(source, subcmd, fd, msg) if subcmd == "data" then - local addr, session, msg = cluster.unpackrequest(msg) - local ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg) + local sz + local addr, session, msg, padding = cluster.unpackrequest(msg) + if padding then + local req = large_request[session] or { addr = addr } + large_request[session] = req + table.insert(req, msg) + return + else + local req = large_request[session] + if req then + large_request[session] = nil + table.insert(req, msg) + msg,sz = cluster.concat(req) + addr = req.addr + end + if not msg then + local response = cluster.packresponse(session, false, "Invalid large req") + socket.write(fd, response) + return + end + end + local ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz) local response if ok then response = cluster.packresponse(session, true, msg, sz) + if type(response) == "table" then + for _, v in ipairs(response) do + socket.lwrite(fd, v) + end + else + socket.write(fd, response) + end else response = cluster.packresponse(session, false, msg) + socket.write(fd, response) end - socket.write(fd, response) elseif subcmd == "open" then skynet.error(string.format("socket accept from %s", msg)) skynet.call(source, "lua", "accept", fd) else + large_request = {} skynet.error(string.format("socket %s %d : %s", subcmd, fd, msg)) end end From aff73cbed7e6cebd796b1f83cac150901dec1172 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 5 Aug 2015 12:23:35 +0800 Subject: [PATCH 437/729] Increase message size 16M(24bit) limit to 56bit on 64bit arch --- examples/config.userlog | 5 +---- service-src/service_harbor.c | 9 +++++++-- skynet-src/skynet_error.c | 2 +- skynet-src/skynet_handle.h | 4 +++- skynet-src/skynet_harbor.c | 6 ++++-- skynet-src/skynet_harbor.h | 4 ---- skynet-src/skynet_mq.h | 4 ++++ skynet-src/skynet_server.c | 12 ++++++------ skynet-src/skynet_socket.c | 4 ++-- skynet-src/skynet_start.c | 1 + skynet-src/skynet_timer.c | 4 ++-- 11 files changed, 31 insertions(+), 24 deletions(-) diff --git a/examples/config.userlog b/examples/config.userlog index 97bef6ef..dcd47b7b 100644 --- a/examples/config.userlog +++ b/examples/config.userlog @@ -3,12 +3,9 @@ thread = 8 logger = "userlog" logservice = "snlua" logpath = "." -harbor = 1 -address = "127.0.0.1:2526" -master = "127.0.0.1:2013" +harbor = 0 start = "main" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap -standalone = "0.0.0.0:2013" luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" lualoader = "lualib/loader.lua" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 5940d62c..90d8bcc6 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -1,6 +1,7 @@ #include "skynet.h" #include "skynet_harbor.h" #include "skynet_socket.h" +#include "skynet_handle.h" /* harbor listen the PTYPE_HARBOR (in text) @@ -323,9 +324,13 @@ forward_local_messsage(struct harbor *h, void *msg, int sz) { static void send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) { - uint32_t sz_header = sz+sizeof(*cookie); + size_t sz_header = sz+sizeof(*cookie); + if (sz_header > UINT32_MAX) { + skynet_error(ctx, "remote message from :%08x to :%08x is too large.", cookie->source, cookie->destination); + return; + } uint8_t * sendbuf = skynet_malloc(sz_header+4); - to_bigendian(sendbuf, sz_header); + to_bigendian(sendbuf, (uint32_t)sz_header); memcpy(sendbuf+4, buffer, sz); header_to_message(cookie, sendbuf+4+sz); diff --git a/skynet-src/skynet_error.c b/skynet-src/skynet_error.c index b9c53efd..962ea597 100644 --- a/skynet-src/skynet_error.c +++ b/skynet-src/skynet_error.c @@ -54,7 +54,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { } smsg.session = 0; smsg.data = data; - smsg.sz = len | (PTYPE_TEXT << HANDLE_REMOTE_SHIFT); + smsg.sz = len | ((size_t)PTYPE_TEXT << MESSAGE_TYPE_SHIFT); skynet_context_push(logger, &smsg); } diff --git a/skynet-src/skynet_handle.h b/skynet-src/skynet_handle.h index e067eed0..293b6571 100644 --- a/skynet-src/skynet_handle.h +++ b/skynet-src/skynet_handle.h @@ -3,7 +3,9 @@ #include -#include "skynet_harbor.h" +// reserve high 8 bits for remote id +#define HANDLE_MASK 0xffffff +#define HANDLE_REMOTE_SHIFT 24 struct skynet_context; diff --git a/skynet-src/skynet_harbor.c b/skynet-src/skynet_harbor.c index 379fce80..5e39ca62 100644 --- a/skynet-src/skynet_harbor.c +++ b/skynet-src/skynet_harbor.c @@ -1,6 +1,8 @@ #include "skynet.h" #include "skynet_harbor.h" #include "skynet_server.h" +#include "skynet_mq.h" +#include "skynet_handle.h" #include #include @@ -11,8 +13,8 @@ static unsigned int HARBOR = ~0; void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) { - int type = rmsg->sz >> HANDLE_REMOTE_SHIFT; - rmsg->sz &= HANDLE_MASK; + int type = rmsg->sz >> MESSAGE_TYPE_SHIFT; + rmsg->sz &= MESSAGE_TYPE_MASK; assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR && REMOTE); skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, type , session); } diff --git a/skynet-src/skynet_harbor.h b/skynet-src/skynet_harbor.h index 62982455..97a76c20 100644 --- a/skynet-src/skynet_harbor.h +++ b/skynet-src/skynet_harbor.h @@ -7,10 +7,6 @@ #define GLOBALNAME_LENGTH 16 #define REMOTE_MAX 256 -// reserve high 8 bits for remote id -#define HANDLE_MASK 0xffffff -#define HANDLE_REMOTE_SHIFT 24 - struct remote_name { char name[GLOBALNAME_LENGTH]; uint32_t handle; diff --git a/skynet-src/skynet_mq.h b/skynet-src/skynet_mq.h index 17178b4f..3721e32b 100644 --- a/skynet-src/skynet_mq.h +++ b/skynet-src/skynet_mq.h @@ -11,6 +11,10 @@ struct skynet_message { size_t sz; }; +// type is encoding in skynet_message.sz high 8bit +#define MESSAGE_TYPE_MASK (SIZE_MAX >> 8) +#define MESSAGE_TYPE_SHIFT ((sizeof(size_t)-1) * 8) + struct message_queue; void skynet_globalmq_push(struct message_queue * queue); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index ea1aadcf..52331193 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -246,8 +246,8 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { assert(ctx->init); CHECKCALLING_BEGIN(ctx) pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle)); - int type = msg->sz >> HANDLE_REMOTE_SHIFT; - size_t sz = msg->sz & HANDLE_MASK; + int type = msg->sz >> MESSAGE_TYPE_SHIFT; + size_t sz = msg->sz & MESSAGE_TYPE_MASK; if (ctx->logfile) { skynet_log_output(ctx->logfile, msg->source, type, msg->session, msg->data, sz); } @@ -662,13 +662,13 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da *data = msg; } - *sz |= type << HANDLE_REMOTE_SHIFT; + *sz |= (size_t)type << MESSAGE_TYPE_SHIFT; } int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) { - if ((sz & HANDLE_MASK) != sz) { - skynet_error(context, "The message to %x is too large (sz = %lu)", destination, sz); + if ((sz & MESSAGE_TYPE_MASK) != sz) { + skynet_error(context, "The message to %x is too large", destination); skynet_free(data); return -1; } @@ -751,7 +751,7 @@ skynet_context_send(struct skynet_context * ctx, void * msg, size_t sz, uint32_t smsg.source = source; smsg.session = session; smsg.data = msg; - smsg.sz = sz | type << HANDLE_REMOTE_SHIFT; + smsg.sz = sz | (size_t)type << MESSAGE_TYPE_SHIFT; skynet_mq_push(ctx->queue, &smsg); } diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 3f2c9a04..2ea26c0a 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -33,7 +33,7 @@ skynet_socket_free() { static void forward_message(int type, bool padding, struct socket_message * result) { struct skynet_socket_message *sm; - int sz = sizeof(*sm); + size_t sz = sizeof(*sm); if (padding) { if (result->data) { sz += strlen(result->data); @@ -56,7 +56,7 @@ forward_message(int type, bool padding, struct socket_message * result) { message.source = 0; message.session = 0; message.data = sm; - message.sz = sz | PTYPE_SOCKET << HANDLE_REMOTE_SHIFT; + message.sz = sz | ((size_t)PTYPE_SOCKET << MESSAGE_TYPE_SHIFT); if (skynet_context_push((uint32_t)result->opaque, &message)) { // todo: report somewhere to close socket diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 4123c624..5345c76c 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -8,6 +8,7 @@ #include "skynet_monitor.h" #include "skynet_socket.h" #include "skynet_daemon.h" +#include "skynet_harbor.h" #include #include diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index f7d5a1ff..a6b4bb87 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -146,7 +146,7 @@ dispatch_list(struct timer_node *current) { message.source = 0; message.session = event->session; message.data = NULL; - message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT; + message.sz = (size_t)MESSAGE_TYPE_SHIFT << MESSAGE_TYPE_SHIFT; skynet_context_push(event->handle, &message); @@ -214,7 +214,7 @@ skynet_timeout(uint32_t handle, int time, int session) { message.source = 0; message.session = session; message.data = NULL; - message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT; + message.sz = (size_t)PTYPE_RESPONSE << MESSAGE_TYPE_SHIFT; if (skynet_context_push(handle, &message)) { return -1; From b4048a8eab2ff01888a931ec7e24541d45c7a2ba Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 5 Aug 2015 16:42:05 +0800 Subject: [PATCH 438/729] bugfix: typo --- skynet-src/skynet_timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index a6b4bb87..f23114dc 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -146,7 +146,7 @@ dispatch_list(struct timer_node *current) { message.source = 0; message.session = event->session; message.data = NULL; - message.sz = (size_t)MESSAGE_TYPE_SHIFT << MESSAGE_TYPE_SHIFT; + message.sz = (size_t)PTYPE_RESPONSE << MESSAGE_TYPE_SHIFT; skynet_context_push(event->handle, &message); From 3156661e0b1fb85dd7e70f7df8c532ba0367e3e3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 6 Aug 2015 20:00:48 +0800 Subject: [PATCH 439/729] bugfix: response function for socket channel --- lualib/mysql.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index a83a0986..305e900b 100755 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -386,7 +386,8 @@ end local function _recv_decode_packet_resp(self) return function(sock) - return true, _recv_packet(self,sock) + -- don't return more than 2 results + return true, (_recv_packet(self,sock)) end end From e9cfdba22a43e4ef9caf29b4f711ab27231426a0 Mon Sep 17 00:00:00 2001 From: snail Date: Fri, 7 Aug 2015 10:38:57 +0800 Subject: [PATCH 440/729] 'hotfix' code cannot use '_ENV' If there's no global variable or function used in any interface of a snax service,the upvalue "_ENV" for 'hotfix' is not set. This modify is not complete, it depend's on the 'collect_uv' method having upvalue '_ENV',maybe other good method to get upvalue index of '_ENV'. --- lualib/snax/hotfix.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/snax/hotfix.lua b/lualib/snax/hotfix.lua index 1a60383a..3a6e4e3e 100644 --- a/lualib/snax/hotfix.lua +++ b/lualib/snax/hotfix.lua @@ -49,7 +49,9 @@ local function collect_all_uv(funcs) collect_uv(v[4], global, envid(v[4])) end end - + if not global["_ENV"] then + global["_ENV"] = {func = collect_uv, index = 1} + end return global end From 5936d3616ccfb0ec49005a19a4c38a758889f6b9 Mon Sep 17 00:00:00 2001 From: BITMAN Date: Fri, 7 Aug 2015 13:51:10 +0800 Subject: [PATCH 441/729] Packing multipart req number first byte is 1 --- lualib-src/lua-cluster.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index c558fb0f..40cc453e 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -88,7 +88,7 @@ packreq_number(lua_State *L, int session, void * msg, uint32_t sz) { } else { int part = (sz - 1) / MULTI_PART + 1; fill_header(L, buf, 13, msg); - buf[2] = 0x80; + buf[2] = 1; fill_uint32(buf+3, addr); fill_uint32(buf+7, (uint32_t)session); fill_uint32(buf+11, sz); From c16ae57639b9568156e7cdb14c27d385d13d838f Mon Sep 17 00:00:00 2001 From: felixdae Date: Fri, 7 Aug 2015 18:36:30 +0800 Subject: [PATCH 442/729] not work with telnet due to \r\n --- service/debug_console.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/service/debug_console.lua b/service/debug_console.lua index 139e76bb..97b35806 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -228,6 +228,7 @@ function COMMAND.debug(address, fd) skynet.fork(function() repeat local cmdline = socket.readline(fd, "\n") + cmdline = cmdline:gsub("(.*)\r$", "%1") if not cmdline then skynet.send(agent, "lua", "cmd", "cont") break From 2935ba3521eed5a8f612bf9bd90538a4e1ec63a9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Aug 2015 16:17:55 +0800 Subject: [PATCH 443/729] Release alpha9 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 8f0a1ac4..a970b7c2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v1.0.0-alpha9 (2015-8-10) +----------- +* Improve lua serialization , support pairs metamethod. +* Bugfix : sproto (See commits log of sproto) +* Add user log service support (In config) +* Remove the size limit of cluster RPC message. +* Remove the size limit of local message. +* Other minor bugfix (See commits log) + v1.0.0-alpha8 (2015-6-29) ----------- * Update lua 5.3.1 From 6fa436e8ff980c05239c8aaa5015d424d0065bdc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Aug 2015 22:06:36 +0800 Subject: [PATCH 444/729] add core.intcommand , See Issue #321 --- lualib-src/lua-skynet.c | 23 +++++++++++++++++++++++ lualib/skynet.lua | 12 +++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index f1594f79..c92ff98d 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -118,6 +118,28 @@ _command(lua_State *L) { return 0; } +static int +_intcommand(lua_State *L) { + struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); + const char * cmd = luaL_checkstring(L,1); + const char * result; + const char * parm = NULL; + char tmp[64]; // for integer parm + if (lua_gettop(L) == 2) { + int32_t n = (int32_t)luaL_checkinteger(L,2); + sprintf(tmp, "%d", n); + parm = tmp; + } + + result = skynet_command(context, cmd, parm); + if (result) { + lua_Integer r = strtoll(result, NULL, 0); + lua_pushinteger(L, r); + return 1; + } + return 0; +} + static int _genid(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); @@ -310,6 +332,7 @@ luaopen_skynet_core(lua_State *L) { { "genid", _genid }, { "redirect", _redirect }, { "command" , _command }, + { "intcommand", _intcommand }, { "error", _error }, { "tostring", _tostring }, { "harbor", _harbor }, diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 8906f60d..31efffcb 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -248,18 +248,16 @@ function suspend(co, result, command, param, size) end function skynet.timeout(ti, func) - local session = c.command("TIMEOUT",tostring(ti)) + local session = c.intcommand("TIMEOUT",ti) assert(session) - session = tonumber(session) local co = co_create(func) assert(session_id_coroutine[session] == nil) session_id_coroutine[session] = co end function skynet.sleep(ti) - local session = c.command("TIMEOUT",tostring(ti)) + local session = c.intcommand("TIMEOUT",ti) assert(session) - session = tonumber(session) local succ, ret = coroutine_yield("SLEEP", session) sleep_session[coroutine.running()] = nil if succ then @@ -301,11 +299,11 @@ function skynet.localname(name) end function skynet.now() - return tonumber(c.command("NOW")) + return c.intcommand("NOW") end function skynet.starttime() - return tonumber(c.command("STARTTIME")) + return c.intcommand("STARTTIME") end function skynet.time() @@ -643,7 +641,7 @@ function skynet.endless() end function skynet.mqlen() - return tonumber(c.command "MQLEN") + return c.intcommand "MQLEN" end function skynet.task(ret) From 947727e33acb7d26e500875cb17537946e481697 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2015 13:21:24 +0800 Subject: [PATCH 445/729] add new socket message WARNING --- examples/watchdog.lua | 5 +++++ lualib-src/lua-netpack.c | 9 ++++++++- lualib/skynet.lua | 2 +- lualib/snax/gateserver.lua | 6 ++++++ lualib/socket.lua | 25 +++++++++++++++++++++++++ service-src/service_gate.c | 3 +++ service-src/service_harbor.c | 7 +++++++ service/gate.lua | 4 ++++ skynet-src/skynet_socket.c | 11 +++++++---- skynet-src/skynet_socket.h | 1 + 10 files changed, 67 insertions(+), 6 deletions(-) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index 29111033..efee5e24 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -32,6 +32,11 @@ function SOCKET.error(fd, msg) close_agent(fd) end +function SOCKET.warning(fd, size) + -- size K bytes havn't send out in fd + print("socket warning", fd, size) +end + function SOCKET.data(fd, msg) end diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 1839abe3..88a54d3d 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -19,6 +19,7 @@ #define TYPE_ERROR 3 #define TYPE_OPEN 4 #define TYPE_CLOSE 5 +#define TYPE_WARNING 6 /* Each package is uint16 + data , uint16 (serialized in big-endian) is the number of bytes comprising the data . @@ -371,6 +372,11 @@ lfilter(lua_State *L) { lua_pushinteger(L, message->id); pushstring(L, buffer, size); return 4; + case SKYNET_SOCKET_TYPE_WARNING: + lua_pushvalue(L, lua_upvalueindex(TYPE_WARNING)); + lua_pushinteger(L, message->id); + lua_pushinteger(L, message->ud); + return 4; default: // never get here return 1; @@ -537,8 +543,9 @@ luaopen_netpack(lua_State *L) { lua_pushliteral(L, "error"); lua_pushliteral(L, "open"); lua_pushliteral(L, "close"); + lua_pushliteral(L, "warning"); - lua_pushcclosure(L, lfilter, 5); + lua_pushcclosure(L, lfilter, 6); lua_setfield(L, -2, "filter"); return 1; diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 31efffcb..e4f46908 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -271,7 +271,7 @@ function skynet.sleep(ti) end function skynet.yield() - return skynet.sleep("0") + return skynet.sleep(0) end function skynet.wait() diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index e68efc12..dfce2fa9 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -114,6 +114,12 @@ function gateserver.start(handler) close_fd(fd) end + function MSG.warning(fd, size) + if handler.warning then + handler.warning(fd, size) + end + end + skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 diff --git a/lualib/socket.lua b/lualib/socket.lua index 8d98e5a3..c5fe7858 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -133,6 +133,25 @@ socket_message[6] = function(id, size, data, address) s.callback(str, address) end +local function default_warning(id, size) + local s = socket_pool[id] + local last = s.warningsize or 0 + if last + 64 < size then -- if size increase 64K + s.warningsize = size + skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id)) + end + s.warningsize = size +end + +-- SKYNET_SOCKET_TYPE_WARNING +socket_message[7] = function(id, size) + local s = socket_pool[id] + if s then + local warning = s.warning or default_warning + warning(id, size) + end +end + skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 @@ -404,4 +423,10 @@ end socket.sendto = assert(driver.udp_send) socket.udp_address = assert(driver.udp_address) +function socket.warning(id, callback) + local obj = socket_pool[id] + assert(obj) + obj.warning = callback +end + return socket diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 9f5d98ec..ec44c722 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -262,6 +262,9 @@ dispatch_socket_message(struct gate *g, const struct skynet_socket_message * mes skynet_socket_start(ctx, message->ud); } break; + case SKYNET_SOCKET_TYPE_WARNING: + skynet_error(ctx, "fd (%d) send buffer (%d)K", message->id, message->ud); + break; } } diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 90d8bcc6..5ff618f1 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -662,6 +662,13 @@ mainloop(struct skynet_context * context, void * ud, int type, int session, uint case SKYNET_SOCKET_TYPE_CONNECT: // fd forward to this service break; + case SKYNET_SOCKET_TYPE_WARNING: { + int id = harbor_id(h, message->id); + if (id) { + skynet_error(context, "message havn't send to Harbor (%d) reach %d K", id, message->ud); + } + break; + } default: skynet_error(context, "recv invalid socket message type %d", type); break; diff --git a/service/gate.lua b/service/gate.lua index 9fe3a491..78fb099f 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -63,6 +63,10 @@ function handler.error(fd, msg) skynet.send(watchdog, "lua", "socket", "error", fd, msg) end +function handler.warning(fd, size) + skynet.send(watchdog, "lua", "socket", "warning", fd, size) +end + local CMD = {} function CMD.forward(source, fd, client, address) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 2ea26c0a..9301419f 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -109,10 +109,13 @@ check_wsz(struct skynet_context *ctx, int id, void *buffer, int64_t wsz) { if (wsz < 0) { return -1; } else if (wsz > 1024 * 1024) { - int kb4 = wsz / 1024 / 4; - if (kb4 % 256 == 0) { - skynet_error(ctx, "%d Mb bytes on socket %d need to send out", (int)(wsz / (1024 * 1024)), id); - } + struct skynet_socket_message tmp; + tmp.type = SKYNET_SOCKET_TYPE_WARNING; + tmp.id = id; + tmp.ud = (int)(wsz / 1024); + tmp.buffer = NULL; + skynet_send(ctx, 0, skynet_context_handle(ctx), PTYPE_SOCKET, 0 , &tmp, sizeof(tmp)); +// skynet_error(ctx, "%d Mb bytes on socket %d need to send out", (int)(wsz / (1024 * 1024)), id); } return 0; } diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 5327f09f..bcdc137c 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -9,6 +9,7 @@ struct skynet_context; #define SKYNET_SOCKET_TYPE_ACCEPT 4 #define SKYNET_SOCKET_TYPE_ERROR 5 #define SKYNET_SOCKET_TYPE_UDP 6 +#define SKYNET_SOCKET_TYPE_WARNING 7 struct skynet_socket_message { int type; From d3771edc9d5a878c34de954cc845d7096a4bcc1d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2015 14:04:15 +0800 Subject: [PATCH 446/729] The cluster message never great than 64K now --- lualib-src/lua-cluster.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 40cc453e..4afe0e6d 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "skynet.h" @@ -28,10 +29,7 @@ fill_uint32(uint8_t * buf, uint32_t n) { static void fill_header(lua_State *L, uint8_t *buf, int sz, void *msg) { - if (sz >= 0x10000) { - skynet_free(msg); - luaL_error(L, "request message is too long %d", sz); - } + assert(sz < 0x10000); buf[0] = (sz >> 8) & 0xff; buf[1] = sz & 0xff; } From 926b44ddf6b8e6a71dfe340e4af5bcac21d8a09d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Aug 2015 14:09:16 +0800 Subject: [PATCH 447/729] remove unused parm --- lualib-src/lua-cluster.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 4afe0e6d..e35615c7 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -28,7 +28,7 @@ fill_uint32(uint8_t * buf, uint32_t n) { } static void -fill_header(lua_State *L, uint8_t *buf, int sz, void *msg) { +fill_header(lua_State *L, uint8_t *buf, int sz) { assert(sz < 0x10000); buf[0] = (sz >> 8) & 0xff; buf[1] = sz & 0xff; @@ -75,7 +75,7 @@ packreq_number(lua_State *L, int session, void * msg, uint32_t sz) { uint32_t addr = (uint32_t)lua_tointeger(L,1); uint8_t buf[TEMP_LENGTH]; if (sz < MULTI_PART) { - fill_header(L, buf, sz+9, msg); + fill_header(L, buf, sz+9); buf[2] = 0; fill_uint32(buf+3, addr); fill_uint32(buf+7, (uint32_t)session); @@ -85,7 +85,7 @@ packreq_number(lua_State *L, int session, void * msg, uint32_t sz) { return 0; } else { int part = (sz - 1) / MULTI_PART + 1; - fill_header(L, buf, 13, msg); + fill_header(L, buf, 13); buf[2] = 1; fill_uint32(buf+3, addr); fill_uint32(buf+7, (uint32_t)session); @@ -106,7 +106,7 @@ packreq_string(lua_State *L, int session, void * msg, uint32_t sz) { uint8_t buf[TEMP_LENGTH]; if (sz < MULTI_PART) { - fill_header(L, buf, sz+6+namelen, msg); + fill_header(L, buf, sz+6+namelen); buf[2] = 0x80; buf[3] = (uint8_t)namelen; memcpy(buf+4, name, namelen); @@ -117,7 +117,7 @@ packreq_string(lua_State *L, int session, void * msg, uint32_t sz) { return 0; } else { int part = (sz - 1) / MULTI_PART + 1; - fill_header(L, buf, 10+namelen, msg); + fill_header(L, buf, 10+namelen); buf[2] = 0x81; buf[3] = (uint8_t)namelen; memcpy(buf+4, name, namelen); @@ -144,7 +144,7 @@ packreq_multi(lua_State *L, int session, void * msg, uint32_t sz) { s = sz; buf[2] = 3; // the last multi part } - fill_header(L, buf, s+5, msg); + fill_header(L, buf, s+5); fill_uint32(buf+3, (uint32_t)session); memcpy(buf+7, ptr, s); lua_pushlstring(L, (const char *)buf, s+7); @@ -356,7 +356,7 @@ lpackresponse(lua_State *L) { uint8_t buf[TEMP_LENGTH]; // multi part begin - fill_header(L, buf, 9, msg); + fill_header(L, buf, 9); fill_uint32(buf+2, session); buf[6] = 2; fill_uint32(buf+7, (uint32_t)sz); @@ -374,7 +374,7 @@ lpackresponse(lua_State *L) { s = sz; buf[6] = 4; } - fill_header(L, buf, s+5, msg); + fill_header(L, buf, s+5); fill_uint32(buf+2, session); memcpy(buf+7,ptr,s); lua_pushlstring(L, (const char *)buf, s+7); @@ -387,7 +387,7 @@ lpackresponse(lua_State *L) { } uint8_t buf[TEMP_LENGTH]; - fill_header(L, buf, sz+5, msg); + fill_header(L, buf, sz+5); fill_uint32(buf+2, session); buf[6] = ok; memcpy(buf+7,msg,sz); From 7547126570000ddce915c58e7590430be55d67c4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Aug 2015 12:03:52 +0800 Subject: [PATCH 448/729] bugfix: memory leak , See Issue #323 --- 3rd/lua/lfunc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/3rd/lua/lfunc.c b/3rd/lua/lfunc.c index ae603c8f..b0d9db4b 100644 --- a/3rd/lua/lfunc.c +++ b/3rd/lua/lfunc.c @@ -135,6 +135,7 @@ static void freesharedproto (lua_State *L, SharedProto *f) { luaM_freearray(L, f->lineinfo, f->sizelineinfo); luaM_freearray(L, f->locvars, f->sizelocvars); luaM_freearray(L, f->upvalues, f->sizeupvalues); + luaM_free(L, f); } void luaF_freeproto (lua_State *L, Proto *f) { From a4f827a48d1d83fd29acc7135267b7d7e6688dc6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Aug 2015 14:53:36 +0800 Subject: [PATCH 449/729] use string for msgserver request --- lualib-src/lua-netpack.c | 15 ++------------- lualib/snax/msgserver.lua | 16 ++++++++-------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 88a54d3d..0ec6f7e9 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -506,19 +506,8 @@ ltostring(lua_State *L) { if (ptr == NULL) { lua_pushliteral(L, ""); } else { - if (lua_isnumber(L, 3)) { - int offset = lua_tointeger(L, 3); - if (offset < 0) { - return luaL_error(L, "Invalid offset %d", offset); - } - if (offset > size) { - offset = size; - } - lua_pushlstring(L, (const char *)ptr + offset, size-offset); - } else { - lua_pushlstring(L, (const char *)ptr, size); - skynet_free(ptr); - } + lua_pushlstring(L, (const char *)ptr, size); + skynet_free(ptr); } return 1; } diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua index a70ea415..c480a8a6 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -70,7 +70,7 @@ Config for server.start: conf.login_handler(uid, secret) -> subid : the function when a new user login, alloc a subid for it. (may call by login server) conf.logout_handler(uid, subid) : the functon when a user logout. (may call by agent) conf.kick_handler(uid, subid) : the functon when a user logout. (may call by login server) - conf.request_handler(username, session, msg, sz) : the function when recv a new request. + conf.request_handler(username, session, msg) : the function when recv a new request. conf.register_handler(servername) : call when gate open conf.disconnect_handler(username) : call when a connection disconnect (afk) ]] @@ -234,10 +234,10 @@ function server.start(conf) end end - local function do_request(fd, msg, sz) + local function do_request(fd, message) local u = assert(connection[fd], "invalid fd") - local msg_sz = sz - 4 - local session = netpack.tostring(msg, sz, msg_sz) + local session = string.unpack("I4", message, -4) + message = message:sub(1,-5) local p = u.response[session] if p then -- session can be reuse in the same connection @@ -256,7 +256,7 @@ function server.start(conf) if p == nil then p = { fd } u.response[session] = p - local ok, result = pcall(conf.request_handler, u.username, msg, msg_sz) + local ok, result = pcall(conf.request_handler, u.username, message) result = result or "" -- NOTICE: YIELD here, socket may close. if not ok then @@ -270,7 +270,6 @@ function server.start(conf) p[3] = u.version p[4] = u.index else - netpack.tostring(msg, sz) -- request before, so free msg -- update version/index, change return fd. -- resend response. p[1] = fd @@ -292,10 +291,11 @@ function server.start(conf) end local function request(fd, msg, sz) - local ok, err = pcall(do_request, fd, msg, sz) + local message = netpack.tostring(msg, sz) + local ok, err = pcall(do_request, fd, message) -- not atomic, may yield if not ok then - skynet.error(string.format("Invalid package %s : %s", err, netpack.tostring(msg, sz))) + skynet.error(string.format("Invalid package %s : %s", err, message)) if connection[fd] then gateserver.closeclient(fd) end From adc39705f392b0a7cc6cadd62b9bf6ed703699f3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Aug 2015 20:56:29 +0800 Subject: [PATCH 450/729] remove unused netpack c api --- lualib-src/lua-netpack.c | 53 --------------------------------------- lualib/snax/msgserver.lua | 10 ++++---- 2 files changed, 5 insertions(+), 58 deletions(-) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 0ec6f7e9..193938b1 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -448,57 +448,6 @@ lpack(lua_State *L) { return 2; } -static int -lpack_string(lua_State *L) { - uint8_t tmp[SMALLSTRING+2]; - size_t len; - uint8_t *buffer; - const char * ptr = tolstring(L, &len, 1); - if (len > 0x10000) { - return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); - } - - if (len <= SMALLSTRING) { - buffer = tmp; - } else { - buffer = lua_newuserdata(L, len + 2); - } - - write_size(buffer, len); - memcpy(buffer+2, ptr, len); - lua_pushlstring(L, (const char *)buffer, len+2); - - return 1; -} - -static int -lpack_padding(lua_State *L) { - uint8_t tmp[SMALLSTRING+2]; - size_t content_sz; - uint8_t *buffer; - const char * ptr = tolstring(L, &content_sz, 2); - size_t cookie_sz = 0; - const char * cookie = luaL_checklstring(L,1,&cookie_sz); - size_t len = cookie_sz + content_sz; - - if (len > 0x10000) { - return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); - } - - if (len <= SMALLSTRING) { - buffer = tmp; - } else { - buffer = lua_newuserdata(L, len + 2); - } - - write_size(buffer, len); - memcpy(buffer+2, ptr, content_sz); - memcpy(buffer+2+content_sz, cookie, cookie_sz); - lua_pushlstring(L, (const char *)buffer, len+2); - - return 1; -} - static int ltostring(lua_State *L) { void * ptr = lua_touserdata(L, 1); @@ -518,8 +467,6 @@ luaopen_netpack(lua_State *L) { luaL_Reg l[] = { { "pop", lpop }, { "pack", lpack }, - { "pack_string", lpack_string }, - { "pack_padding", lpack_padding }, { "clear", lclear }, { "tostring", ltostring }, { NULL, NULL }, diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua index c480a8a6..765387cc 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -236,7 +236,7 @@ function server.start(conf) local function do_request(fd, message) local u = assert(connection[fd], "invalid fd") - local session = string.unpack("I4", message, -4) + local session = string.unpack(">I4", message, -4) message = message:sub(1,-5) local p = u.response[session] if p then @@ -257,16 +257,16 @@ function server.start(conf) p = { fd } u.response[session] = p local ok, result = pcall(conf.request_handler, u.username, message) - result = result or "" -- NOTICE: YIELD here, socket may close. + result = result or "" if not ok then skynet.error(result) - result = "\0" .. session + result = string.pack(">BI4", 0, session) else - result = result .. '\1' .. session + result = result .. string.pack(">BI4", 1, session) end - p[2] = netpack.pack_string(result) + p[2] = string.pack(">s2",result) p[3] = u.version p[4] = u.index else From 0ce9921c251988ed50519d3d43d7143efa5b4bc2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Aug 2015 21:16:02 +0800 Subject: [PATCH 451/729] remove unused sz --- examples/login/gated.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/login/gated.lua b/examples/login/gated.lua index 4a024699..0b8c53b7 100644 --- a/examples/login/gated.lua +++ b/examples/login/gated.lua @@ -74,9 +74,9 @@ function server.disconnect_handler(username) end -- call by self (when recv a request from client) -function server.request_handler(username, msg, sz) +function server.request_handler(username, msg) local u = username_map[username] - return skynet.tostring(skynet.rawcall(u.agent, "client", msg, sz)) + return skynet.tostring(skynet.rawcall(u.agent, "client", msg)) end -- call by self (when gate open) From bf8f9b8654fe5764b5249766b9f7611adb6e8dd0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Aug 2015 21:00:20 +0800 Subject: [PATCH 452/729] add pthread lock --- Makefile | 9 ++-- lualib-src/lua-bson.c | 3 +- lualib-src/lua-clientsocket.c | 16 ++++---- lualib-src/lua-debugchannel.c | 26 ++++++------ lualib-src/lua-multicast.c | 4 +- lualib-src/lua-sharedata.c | 9 ++-- lualib-src/lua-stm.c | 9 ++-- skynet-src/atomic.h | 14 +++++++ skynet-src/malloc_hook.c | 17 ++++---- skynet-src/rwlock.h | 40 ++++++++++++++++++ skynet-src/skynet_env.c | 16 ++++---- skynet-src/skynet_module.c | 15 ++++--- skynet-src/skynet_monitor.c | 3 +- skynet-src/skynet_mq.c | 42 +++++++++---------- skynet-src/skynet_server.c | 25 +++++++----- skynet-src/skynet_timer.c | 21 +++++----- skynet-src/socket_server.c | 7 ++-- skynet-src/spinlock.h | 77 +++++++++++++++++++++++++++++++++++ 18 files changed, 249 insertions(+), 104 deletions(-) create mode 100644 skynet-src/atomic.h create mode 100644 skynet-src/spinlock.h diff --git a/Makefile b/Makefile index b92a1a33..98f7d1c9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ CSERVICE_PATH ?= cservice SKYNET_BUILD_PATH ?= . -CFLAGS = -g -O2 -Wall -I$(LUA_INC) $(MYCFLAGS) +CFLAGS = -g -O2 -Wall -I$(LUA_INC) $(MYCFLAGS) +# CFLAGS += -DUSE_PTHREAD_LOCK # lua @@ -79,7 +80,7 @@ $(LUA_CLIB_PATH)/socketdriver.so : lualib-src/lua-socket.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -Iskynet-src $(LUA_CLIB_PATH)/mongo.so : lualib-src/lua-mongo.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src @@ -109,7 +110,7 @@ $(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CL $(CC) $(CFLAGS) $(SHARED) $^ -o $@ $(LUA_CLIB_PATH)/sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ $(LUA_CLIB_PATH)/stm.so : lualib-src/lua-stm.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ @@ -124,7 +125,7 @@ $(LUA_CLIB_PATH)/mysqlaux.so : lualib-src/lua-mysqlaux.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ $(LUA_CLIB_PATH)/debugchannel.so : lualib-src/lua-debugchannel.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 9071f56d..0fc26985 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -8,6 +8,7 @@ #include #include #include +#include "atomic.h" #define DEFAULT_CAP 64 #define MAX_NUMBER 1024 @@ -1140,7 +1141,7 @@ lobjectid(lua_State *L) { } else { time_t ti = time(NULL); // old_counter is a static var, use atom inc. - uint32_t id = __sync_fetch_and_add(&oid_counter,1); + uint32_t id = ATOM_FINC(&oid_counter); oid[2] = (ti>>24) & 0xff; oid[3] = (ti>>16) & 0xff; diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index 89bfe1f8..fa1a88e8 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -128,11 +128,8 @@ lusleep(lua_State *L) { #define QUEUE_SIZE 1024 -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - struct queue { - int lock; + pthread_mutex_t lock; int head; int tail; char * queue[QUEUE_SIZE]; @@ -153,7 +150,7 @@ readline_stdin(void * arg) { memcpy(str, tmp, n); str[n] = 0; - LOCK(q); + pthread_mutex_lock(&q->lock); q->queue[q->tail] = str; if (++q->tail >= QUEUE_SIZE) { @@ -163,7 +160,7 @@ readline_stdin(void * arg) { // queue overflow exit(1); } - UNLOCK(q); + pthread_mutex_unlock(&q->lock); } return NULL; } @@ -171,16 +168,16 @@ readline_stdin(void * arg) { static int lreadstdin(lua_State *L) { struct queue *q = lua_touserdata(L, lua_upvalueindex(1)); - LOCK(q); + pthread_mutex_lock(&q->lock); if (q->head == q->tail) { - UNLOCK(q); + pthread_mutex_unlock(&q->lock); return 0; } char * str = q->queue[q->head]; if (++q->head >= QUEUE_SIZE) { q->head = 0; } - UNLOCK(q); + pthread_mutex_unlock(&q->lock); lua_pushstring(L, str); free(str); return 1; @@ -201,6 +198,7 @@ luaopen_clientsocket(lua_State *L) { struct queue * q = lua_newuserdata(L, sizeof(*q)); memset(q, 0, sizeof(*q)); + pthread_mutex_init(&q->lock, NULL); lua_pushcclosure(L, lreadstdin, 1); lua_setfield(L, -2, "readstdin"); diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c index 9e4539d3..ad7e1f1b 100644 --- a/lualib-src/lua-debugchannel.c +++ b/lualib-src/lua-debugchannel.c @@ -4,10 +4,9 @@ #include #include #include +#include "spinlock.h" #define METANAME "debugchannel" -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); struct command { struct command * next; @@ -15,7 +14,7 @@ struct command { }; struct channel { - int lock; + struct spinlock lock; int ref; struct command * head; struct command * tail; @@ -26,6 +25,7 @@ channel_new() { struct channel * c = malloc(sizeof(*c)); memset(c, 0 , sizeof(*c)); c->ref = 1; + SPIN_INIT(c) return c; } @@ -33,21 +33,21 @@ channel_new() { static struct channel * channel_connect(struct channel *c) { struct channel * ret = NULL; - LOCK(c) + SPIN_LOCK(c) if (c->ref == 1) { ++c->ref; ret = c; } - UNLOCK(c) + SPIN_UNLOCK(c) return ret; } static struct channel * channel_release(struct channel *c) { - LOCK(c) + SPIN_LOCK(c) --c->ref; if (c->ref > 0) { - UNLOCK(c) + SPIN_UNLOCK(c) return c; } // never unlock while reference is 0 @@ -59,6 +59,8 @@ channel_release(struct channel *c) { free(p); p = next; } + SPIN_UNLOCK(c) + SPIN_DESTROY(c) free(c); return NULL; } @@ -67,9 +69,9 @@ channel_release(struct channel *c) { static struct command * channel_read(struct channel *c, double timeout) { struct command * ret = NULL; - LOCK(c) + SPIN_LOCK(c) if (c->head == NULL) { - UNLOCK(c) + SPIN_UNLOCK(c) int ti = (int)(timeout * 100000); usleep(ti); return NULL; @@ -79,7 +81,7 @@ channel_read(struct channel *c, double timeout) { if (c->head == NULL) { c->tail = NULL; } - UNLOCK(c) + SPIN_UNLOCK(c) return ret; } @@ -90,14 +92,14 @@ channel_write(struct channel *c, const char * s, size_t sz) { cmd->sz = sz; cmd->next = NULL; memcpy(cmd+1, s, sz); - LOCK(c) + SPIN_LOCK(c) if (c->tail == NULL) { c->head = c->tail = cmd; } else { c->tail->next = cmd; c->tail = cmd; } - UNLOCK(c) + SPIN_UNLOCK(c) } struct channel_box { diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 599616f9..cff8d434 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -5,6 +5,8 @@ #include #include +#include "atomic.h" + struct mc_package { int reference; uint32_t size; @@ -116,7 +118,7 @@ static int mc_closelocal(lua_State *L) { struct mc_package *pack = lua_touserdata(L,1); - int ref = __sync_sub_and_fetch(&pack->reference, 1); + int ref = ATOM_DEC(&pack->reference); if (ref <= 0) { skynet_free(pack->data); skynet_free(pack); diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index e7a27c57..99cadd15 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -4,6 +4,7 @@ #include #include #include +#include "atomic.h" #define KEYTYPE_INTEGER 0 #define KEYTYPE_STRING 1 @@ -663,7 +664,7 @@ releaseobj(lua_State *L) { struct ctrl *c = lua_touserdata(L, 1); struct table *tbl = c->root; struct state *s = lua_touserdata(tbl->L, 1); - __sync_fetch_and_sub(&s->ref, 1); + ATOM_DEC(&s->ref); c->root = NULL; c->update = NULL; @@ -674,7 +675,7 @@ static int lboxconf(lua_State *L) { struct table * tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); - __sync_fetch_and_add(&s->ref, 1); + ATOM_INC(&s->ref); struct ctrl * c = lua_newuserdata(L, sizeof(*c)); c->root = tbl; @@ -719,7 +720,7 @@ static int lincref(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); - int ref = __sync_add_and_fetch(&s->ref, 1); + int ref = ATOM_INC(&s->ref); lua_pushinteger(L , ref); return 1; @@ -729,7 +730,7 @@ static int ldecref(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); - int ref = __sync_sub_and_fetch(&s->ref, 1); + int ref = ATOM_DEC(&s->ref); lua_pushinteger(L , ref); return 1; diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index b38fbcd1..e175d100 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -7,6 +7,7 @@ #include "rwlock.h" #include "skynet_malloc.h" +#include "atomic.h" struct stm_object { struct rwlock lock; @@ -45,7 +46,7 @@ static void stm_releasecopy(struct stm_copy *copy) { if (copy == NULL) return; - if (__sync_sub_and_fetch(©->reference, 1) == 0) { + if (ATOM_DEC(©->reference) == 0) { skynet_free(copy->msg); skynet_free(copy); } @@ -70,7 +71,7 @@ stm_release(struct stm_object *obj) { static void stm_releasereader(struct stm_object *obj) { rwlock_rlock(&obj->lock); - if (__sync_sub_and_fetch(&obj->reference,1) == 0) { + if (ATOM_DEC(&obj->reference) == 0) { // last reader, no writer. so no need to unlock assert(obj->copy == NULL); skynet_free(obj); @@ -82,7 +83,7 @@ stm_releasereader(struct stm_object *obj) { static void stm_grab(struct stm_object *obj) { rwlock_rlock(&obj->lock); - int ref = __sync_fetch_and_add(&obj->reference,1); + int ref = ATOM_FINC(&obj->reference); rwlock_runlock(&obj->lock); assert(ref > 0); } @@ -92,7 +93,7 @@ stm_copy(struct stm_object *obj) { rwlock_rlock(&obj->lock); struct stm_copy * ret = obj->copy; if (ret) { - int ref = __sync_fetch_and_add(&ret->reference,1); + int ref = ATOM_FINC(&ret->reference); assert(ref > 0); } rwlock_runlock(&obj->lock); diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h new file mode 100644 index 00000000..f79c7148 --- /dev/null +++ b/skynet-src/atomic.h @@ -0,0 +1,14 @@ +#ifndef SKYNET_ATOMIC_H +#define SKYNET_ATOMIC_H + +#define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) +#define ATOM_CAS_POINTER(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) +#define ATOM_INC(ptr) __sync_add_and_fetch(ptr, 1) +#define ATOM_FINC(ptr) __sync_fetch_and_add(ptr, 1) +#define ATOM_DEC(ptr) __sync_sub_and_fetch(ptr, 1) +#define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1) +#define ATOM_ADD(ptr,n) __sync_add_and_fetch(ptr, n) +#define ATOM_SUB(ptr,n) __sync_sub_and_fetch(ptr, n) +#define ATOM_AND(ptr,n) __sync_and_and_fetch(ptr, n) + +#endif diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index d9ad6f32..3b072168 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -6,6 +6,7 @@ #include "malloc_hook.h" #include "skynet.h" +#include "atomic.h" static size_t _used_memory = 0; static size_t _memory_block = 0; @@ -32,11 +33,11 @@ get_allocated_field(uint32_t handle) { ssize_t old_alloc = data->allocated; if(old_handle == 0 || old_alloc <= 0) { // data->allocated may less than zero, because it may not count at start. - if(!__sync_bool_compare_and_swap(&data->handle, old_handle, handle)) { + if(!ATOM_CAS(&data->handle, old_handle, handle)) { return 0; } if (old_alloc < 0) { - __sync_bool_compare_and_swap(&data->allocated, old_alloc, 0); + ATOM_CAS(&data->allocated, old_alloc, 0); } } if(data->handle != handle) { @@ -47,21 +48,21 @@ get_allocated_field(uint32_t handle) { inline static void update_xmalloc_stat_alloc(uint32_t handle, size_t __n) { - __sync_add_and_fetch(&_used_memory, __n); - __sync_add_and_fetch(&_memory_block, 1); + ATOM_ADD(&_used_memory, __n); + ATOM_INC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { - __sync_add_and_fetch(allocated, __n); + ATOM_ADD(allocated, __n); } } inline static void update_xmalloc_stat_free(uint32_t handle, size_t __n) { - __sync_sub_and_fetch(&_used_memory, __n); - __sync_sub_and_fetch(&_memory_block, 1); + ATOM_SUB(&_used_memory, __n); + ATOM_DEC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { - __sync_sub_and_fetch(allocated, __n); + ATOM_SUB(allocated, __n); } } diff --git a/skynet-src/rwlock.h b/skynet-src/rwlock.h index e366f18c..5a995918 100644 --- a/skynet-src/rwlock.h +++ b/skynet-src/rwlock.h @@ -1,6 +1,8 @@ #ifndef SKYNET_RWLOCK_H #define SKYNET_RWLOCK_H +#ifndef USE_PTHREAD_LOCK + struct rwlock { int write; int read; @@ -45,4 +47,42 @@ rwlock_runlock(struct rwlock *lock) { __sync_sub_and_fetch(&lock->read,1); } +#else + +#include + +// only for some platform doesn't have __sync_* +// todo: check the result of pthread api + +struct rwlock { + pthread_rwlock_t lock; +}; + +static inline void +rwlock_init(struct rwlock *lock) { + pthread_rwlock_init(&lock->lock, NULL); +} + +static inline void +rwlock_rlock(struct rwlock *lock) { + pthread_rwlock_rdlock(&lock->lock); +} + +static inline void +rwlock_wlock(struct rwlock *lock) { + pthread_rwlock_wrlock(&lock->lock); +} + +static inline void +rwlock_wunlock(struct rwlock *lock) { + pthread_rwlock_unlock(&lock->lock); +} + +static inline void +rwlock_runlock(struct rwlock *lock) { + pthread_rwlock_unlock(&lock->lock); +} + +#endif + #endif diff --git a/skynet-src/skynet_env.c b/skynet-src/skynet_env.c index 9dbbcf9c..a5882f21 100644 --- a/skynet-src/skynet_env.c +++ b/skynet-src/skynet_env.c @@ -1,5 +1,6 @@ #include "skynet.h" #include "skynet_env.h" +#include "spinlock.h" #include #include @@ -8,18 +9,15 @@ #include struct skynet_env { - int lock; + struct spinlock lock; lua_State *L; }; static struct skynet_env *E = NULL; -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - const char * skynet_getenv(const char *key) { - LOCK(E) + SPIN_LOCK(E) lua_State *L = E->L; @@ -27,14 +25,14 @@ skynet_getenv(const char *key) { const char * result = lua_tostring(L, -1); lua_pop(L, 1); - UNLOCK(E) + SPIN_UNLOCK(E) return result; } void skynet_setenv(const char *key, const char *value) { - LOCK(E) + SPIN_LOCK(E) lua_State *L = E->L; lua_getglobal(L, key); @@ -43,12 +41,12 @@ skynet_setenv(const char *key, const char *value) { lua_pushstring(L,value); lua_setglobal(L,key); - UNLOCK(E) + SPIN_UNLOCK(E) } void skynet_env_init() { E = skynet_malloc(sizeof(*E)); - E->lock = 0; + SPIN_INIT(E) E->L = luaL_newstate(); } diff --git a/skynet-src/skynet_module.c b/skynet-src/skynet_module.c index 2da34629..d586427f 100644 --- a/skynet-src/skynet_module.c +++ b/skynet-src/skynet_module.c @@ -1,6 +1,7 @@ #include "skynet.h" #include "skynet_module.h" +#include "spinlock.h" #include #include @@ -13,7 +14,7 @@ struct modules { int count; - int lock; + struct spinlock lock; const char * path; struct skynet_module m[MAX_MODULE_TYPE]; }; @@ -95,7 +96,7 @@ skynet_module_query(const char * name) { if (result) return result; - while(__sync_lock_test_and_set(&M->lock,1)) {} + SPIN_LOCK(M) result = _query(name); // double check @@ -114,21 +115,22 @@ skynet_module_query(const char * name) { } } - __sync_lock_release(&M->lock); + SPIN_UNLOCK(M) return result; } void skynet_module_insert(struct skynet_module *mod) { - while(__sync_lock_test_and_set(&M->lock,1)) {} + SPIN_LOCK(M) struct skynet_module * m = _query(mod->name); assert(m == NULL && M->count < MAX_MODULE_TYPE); int index = M->count; M->m[index] = *mod; ++M->count; - __sync_lock_release(&M->lock); + + SPIN_UNLOCK(M) } void * @@ -164,7 +166,8 @@ skynet_module_init(const char *path) { struct modules *m = skynet_malloc(sizeof(*m)); m->count = 0; m->path = skynet_strdup(path); - m->lock = 0; + + SPIN_INIT(m) M = m; } diff --git a/skynet-src/skynet_monitor.c b/skynet-src/skynet_monitor.c index 9ee302e5..8d47fc89 100644 --- a/skynet-src/skynet_monitor.c +++ b/skynet-src/skynet_monitor.c @@ -3,6 +3,7 @@ #include "skynet_monitor.h" #include "skynet_server.h" #include "skynet.h" +#include "atomic.h" #include #include @@ -30,7 +31,7 @@ void skynet_monitor_trigger(struct skynet_monitor *sm, uint32_t source, uint32_t destination) { sm->source = source; sm->destination = destination; - __sync_fetch_and_add(&sm->version , 1); + ATOM_INC(&sm->version); } void diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index ba0c61e6..157f33d8 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -1,6 +1,7 @@ #include "skynet.h" #include "skynet_mq.h" #include "skynet_handle.h" +#include "spinlock.h" #include #include @@ -18,11 +19,11 @@ #define MQ_OVERLOAD 1024 struct message_queue { + struct spinlock lock; uint32_t handle; int cap; int head; int tail; - int lock; int release; int in_global; int overload; @@ -34,19 +35,16 @@ struct message_queue { struct global_queue { struct message_queue *head; struct message_queue *tail; - int lock; + struct spinlock lock; }; static struct global_queue *Q = NULL; -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; - LOCK(q) + SPIN_LOCK(q) assert(queue->next == NULL); if(q->tail) { q->tail->next = queue; @@ -54,14 +52,14 @@ skynet_globalmq_push(struct message_queue * queue) { } else { q->head = q->tail = queue; } - UNLOCK(q) + SPIN_UNLOCK(q) } struct message_queue * skynet_globalmq_pop() { struct global_queue *q = Q; - LOCK(q) + SPIN_LOCK(q) struct message_queue *mq = q->head; if(mq) { q->head = mq->next; @@ -71,7 +69,7 @@ skynet_globalmq_pop() { } mq->next = NULL; } - UNLOCK(q) + SPIN_UNLOCK(q) return mq; } @@ -83,7 +81,7 @@ skynet_mq_create(uint32_t handle) { q->cap = DEFAULT_QUEUE_SIZE; q->head = 0; q->tail = 0; - q->lock = 0; + SPIN_INIT(q) // When the queue is create (always between service create and service init) , // set in_global flag to avoid push it to global queue . // If the service init success, skynet_context_new will call skynet_mq_force_push to push it to global queue. @@ -100,6 +98,7 @@ skynet_mq_create(uint32_t handle) { static void _release(struct message_queue *q) { assert(q->next == NULL); + SPIN_DESTROY(q) skynet_free(q->queue); skynet_free(q); } @@ -113,11 +112,11 @@ int skynet_mq_length(struct message_queue *q) { int head, tail,cap; - LOCK(q) + SPIN_LOCK(q) head = q->head; tail = q->tail; cap = q->cap; - UNLOCK(q) + SPIN_UNLOCK(q) if (head <= tail) { return tail - head; @@ -138,7 +137,7 @@ skynet_mq_overload(struct message_queue *q) { int skynet_mq_pop(struct message_queue *q, struct skynet_message *message) { int ret = 1; - LOCK(q) + SPIN_LOCK(q) if (q->head != q->tail) { *message = q->queue[q->head++]; @@ -167,7 +166,7 @@ skynet_mq_pop(struct message_queue *q, struct skynet_message *message) { q->in_global = 0; } - UNLOCK(q) + SPIN_UNLOCK(q) return ret; } @@ -190,7 +189,7 @@ expand_queue(struct message_queue *q) { void skynet_mq_push(struct message_queue *q, struct skynet_message *message) { assert(message); - LOCK(q) + SPIN_LOCK(q) q->queue[q->tail] = *message; if (++ q->tail >= q->cap) { @@ -206,25 +205,26 @@ skynet_mq_push(struct message_queue *q, struct skynet_message *message) { skynet_globalmq_push(q); } - UNLOCK(q) + SPIN_UNLOCK(q) } void skynet_mq_init() { struct global_queue *q = skynet_malloc(sizeof(*q)); memset(q,0,sizeof(*q)); + SPIN_INIT(q); Q=q; } void skynet_mq_mark_release(struct message_queue *q) { - LOCK(q) + SPIN_LOCK(q) assert(q->release == 0); q->release = 1; if (q->in_global != MQ_IN_GLOBAL) { skynet_globalmq_push(q); } - UNLOCK(q) + SPIN_UNLOCK(q) } static void @@ -238,13 +238,13 @@ _drop_queue(struct message_queue *q, message_drop drop_func, void *ud) { void skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud) { - LOCK(q) + SPIN_LOCK(q) if (q->release) { - UNLOCK(q) + SPIN_UNLOCK(q) _drop_queue(q, drop_func, ud); } else { skynet_globalmq_push(q); - UNLOCK(q) + SPIN_UNLOCK(q) } } diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 52331193..4972e7a4 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -10,6 +10,8 @@ #include "skynet_monitor.h" #include "skynet_imp.h" #include "skynet_log.h" +#include "spinlock.h" +#include "atomic.h" #include @@ -21,16 +23,18 @@ #ifdef CALLING_CHECK -#define CHECKCALLING_BEGIN(ctx) assert(__sync_lock_test_and_set(&ctx->calling,1) == 0); -#define CHECKCALLING_END(ctx) __sync_lock_release(&ctx->calling); -#define CHECKCALLING_INIT(ctx) ctx->calling = 0; -#define CHECKCALLING_DECL int calling; +#define CHECKCALLING_BEGIN(ctx) if (!(spinlock_trylock(&ctx->calling))) { assert(0); } +#define CHECKCALLING_END(ctx) spinlock_unlock(&ctx->calling); +#define CHECKCALLING_INIT(ctx) spinlock_init(&ctx->calling); +#define CHECKCALLING_DESTROY(ctx) spinlock_destroy(&ctx->calling); +#define CHECKCALLING_DECL struct spinlock calling; #else #define CHECKCALLING_BEGIN(ctx) #define CHECKCALLING_END(ctx) #define CHECKCALLING_INIT(ctx) +#define CHECKCALLING_DESTROY(ctx) #define CHECKCALLING_DECL #endif @@ -68,12 +72,12 @@ skynet_context_total() { static void context_inc() { - __sync_fetch_and_add(&G_NODE.total,1); + ATOM_INC(&G_NODE.total); } static void context_dec() { - __sync_fetch_and_sub(&G_NODE.total,1); + ATOM_DEC(&G_NODE.total); } uint32_t @@ -179,7 +183,7 @@ skynet_context_newsession(struct skynet_context *ctx) { void skynet_context_grab(struct skynet_context *ctx) { - __sync_add_and_fetch(&ctx->ref,1); + ATOM_INC(&ctx->ref); } void @@ -197,13 +201,14 @@ delete_context(struct skynet_context *ctx) { } skynet_module_instance_release(ctx->mod, ctx->instance); skynet_mq_mark_release(ctx->queue); + CHECKCALLING_DESTROY(ctx) skynet_free(ctx); context_dec(); } struct skynet_context * skynet_context_release(struct skynet_context *ctx) { - if (__sync_sub_and_fetch(&ctx->ref,1) == 0) { + if (ATOM_DEC(&ctx->ref) == 0) { delete_context(ctx); return NULL; } @@ -560,7 +565,7 @@ cmd_logon(struct skynet_context * context, const char * param) { if (lastf == NULL) { f = skynet_log_open(context, handle); if (f) { - if (!__sync_bool_compare_and_swap(&ctx->logfile, NULL, f)) { + if (!ATOM_CAS_POINTER(&ctx->logfile, NULL, f)) { // logfile opens in other thread, close this one. fclose(f); } @@ -581,7 +586,7 @@ cmd_logoff(struct skynet_context * context, const char * param) { FILE * f = ctx->logfile; if (f) { // logfile may close in other thread - if (__sync_bool_compare_and_swap(&ctx->logfile, f, NULL)) { + if (ATOM_CAS_POINTER(&ctx->logfile, f, NULL)) { skynet_log_close(context, f, handle); } } diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index f23114dc..c9a113ae 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -4,6 +4,7 @@ #include "skynet_mq.h" #include "skynet_server.h" #include "skynet_handle.h" +#include "spinlock.h" #include #include @@ -17,9 +18,6 @@ typedef void (*timer_execute_func)(void *ud,void *arg); -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); - #define TIME_NEAR_SHIFT 8 #define TIME_NEAR (1 << TIME_NEAR_SHIFT) #define TIME_LEVEL_SHIFT 6 @@ -45,7 +43,7 @@ struct link_list { struct timer { struct link_list near[TIME_NEAR]; struct link_list t[4][TIME_LEVEL]; - int lock; + struct spinlock lock; uint32_t time; uint32_t current; uint32_t starttime; @@ -97,12 +95,12 @@ timer_add(struct timer *T,void *arg,size_t sz,int time) { struct timer_node *node = (struct timer_node *)skynet_malloc(sizeof(*node)+sz); memcpy(node+1,arg,sz); - LOCK(T); + SPIN_LOCK(T); node->expire=time+T->time; add_node(T,node); - UNLOCK(T); + SPIN_UNLOCK(T); } static void @@ -162,16 +160,16 @@ timer_execute(struct timer *T) { while (T->near[idx].head.next) { struct timer_node *current = link_clear(&T->near[idx]); - UNLOCK(T); + SPIN_UNLOCK(T); // dispatch_list don't need lock T dispatch_list(current); - LOCK(T); + SPIN_LOCK(T); } } static void timer_update(struct timer *T) { - LOCK(T); + SPIN_LOCK(T); // try to dispatch timeout 0 (rare condition) timer_execute(T); @@ -181,7 +179,7 @@ timer_update(struct timer *T) { timer_execute(T); - UNLOCK(T); + SPIN_UNLOCK(T); } static struct timer * @@ -201,7 +199,8 @@ timer_create_timer() { } } - r->lock = 0; + SPIN_INIT(r) + r->current = 0; return r; diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 78f9b47b..b2454d2e 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -2,6 +2,7 @@ #include "socket_server.h" #include "socket_poll.h" +#include "atomic.h" #include #include @@ -237,13 +238,13 @@ static int reserve_id(struct socket_server *ss) { int i; for (i=0;ialloc_id), 1); + int id = ATOM_INC(&(ss->alloc_id)); if (id < 0) { - id = __sync_and_and_fetch(&(ss->alloc_id), 0x7fffffff); + id = ATOM_AND(&(ss->alloc_id), 0x7fffffff); } struct socket *s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID) { - if (__sync_bool_compare_and_swap(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) { + if (ATOM_CAS(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) { s->id = id; s->fd = -1; return id; diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h new file mode 100644 index 00000000..d9744c3f --- /dev/null +++ b/skynet-src/spinlock.h @@ -0,0 +1,77 @@ +#ifndef SKYNET_SPINLOCK_H +#define SKYNET_SPINLOCK_H + +#define SPIN_INIT(q) spinlock_init(&(q)->lock); +#define SPIN_LOCK(q) spinlock_lock(&(q)->lock); +#define SPIN_UNLOCK(q) spinlock_unlock(&(q)->lock); +#define SPIN_DESTROY(q) spinlock_destroy(&(q)->lock); + +#ifndef USE_PTHREAD_LOCK + +struct spinlock { + int lock; +}; + +static inline void +spinlock_init(struct spinlock *lock) { + lock->lock = 0; +} + +static inline void +spinlock_lock(struct spinlock *lock) { + while (__sync_lock_test_and_set(&lock->lock,1)) {} +} + +static inline int +spinlock_trylock(struct spinlock *lock) { + return __sync_lock_test_and_set(&lock->lock,1) == 0; +} + +static inline void +spinlock_unlock(struct spinlock *lock) { + __sync_lock_release(&lock->lock); +} + +static inline void +spinlock_destroy(struct spinlock *lock) { +} + +#else + +#include + +// we use mutex instead of spinlock for some reason +// you can also replace to pthread_spinlock + +struct spinlock { + pthread_mutex_t lock; +}; + +static inline void +spinlock_init(struct spinlock *lock) { + pthread_mutex_init(&lock->lock, NULL); +} + +static inline void +spinlock_lock(struct spinlock *lock) { + pthread_mutex_lock(&lock->lock); +} + +static inline int +spinlock_trylock(struct spinlock *lock) { + return pthread_mutex_trylock(&lock->lock) == 0; +} + +static inline void +spinlock_unlock(struct spinlock *lock) { + pthread_mutex_unlock(&lock->lock); +} + +static inline void +spinlock_destroy(struct spinlock *lock) { + pthread_mutex_destroy(&lock->lock); +} + +#endif + +#endif From a38a3140dde2e8d20aadbe9dd04b8d4c0a14e37b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 14 Aug 2015 17:32:59 +0800 Subject: [PATCH 453/729] error when send to address 0 --- lualib-src/lua-skynet.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index c92ff98d..7d75d2ce 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -172,6 +172,9 @@ _send(lua_State *L) { uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; if (dest == 0) { + if (lua_type(L,1) == LUA_TNUMBER) { + return luaL_error(L, "Invalid service address 0"); + } dest_string = get_dest_string(L, 1); } From 2583af26d7e5fe58f1f0aef4463d99ae1619a656 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 14 Aug 2015 23:22:00 +0800 Subject: [PATCH 454/729] add cluster.register and cluster.query --- examples/cluster1.lua | 13 +++++++------ examples/cluster2.lua | 9 ++++++--- lualib-src/lua-cluster.c | 4 ++-- lualib/cluster.lua | 10 ++++++++++ service/clusterd.lua | 31 +++++++++++++++++++++++++++++-- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index d44899ba..0eca6176 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -1,16 +1,17 @@ local skynet = require "skynet" local cluster = require "cluster" -require "skynet.manager" -- import skynet.name local snax = require "snax" skynet.start(function() local sdb = skynet.newservice("simpledb") - skynet.name(".simpledb", sdb) + -- register name "sdb" for simpledb, you can use cluster.query() later. + -- See cluster2.lua + cluster.register("sdb", sdb) - print(skynet.call(".simpledb", "lua", "SET", "a", "foobar")) - print(skynet.call(".simpledb", "lua", "SET", "b", "foobar2")) - print(skynet.call(".simpledb", "lua", "GET", "a")) - print(skynet.call(".simpledb", "lua", "GET", "b")) + print(skynet.call(sdb, "lua", "SET", "a", "foobar")) + print(skynet.call(sdb, "lua", "SET", "b", "foobar2")) + print(skynet.call(sdb, "lua", "GET", "a")) + print(skynet.call(sdb, "lua", "GET", "b")) cluster.open "db" cluster.open "db2" -- unique snax service diff --git a/examples/cluster2.lua b/examples/cluster2.lua index 0aef412c..84648567 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -2,15 +2,18 @@ local skynet = require "skynet" local cluster = require "cluster" skynet.start(function() - local proxy = cluster.proxy("db", ".simpledb") + -- query name "sdb" of cluster db. + local sdb = cluster.query("db", "sdb") + print("db.sbd=",sdb) + local proxy = cluster.proxy("db", sdb) local largekey = string.rep("X", 128*1024) local largevalue = string.rep("R", 100 * 1024) print(skynet.call(proxy, "lua", "SET", largekey, largevalue)) local v = skynet.call(proxy, "lua", "GET", largekey) assert(largevalue == v) - print(cluster.call("db", ".simpledb", "GET", "a")) - print(cluster.call("db2", ".simpledb", "GET", "b")) + print(cluster.call("db", sdb, "GET", "a")) + print(cluster.call("db2", sdb, "GET", "b")) -- test snax service local pingserver = cluster.snax("db", "pingserver") diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index e35615c7..6ee1e69b 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -219,8 +219,8 @@ unpackreq_number(lua_State *L, const uint8_t * buf, int sz) { static int unpackmreq_number(lua_State *L, const uint8_t * buf, int sz) { - if (sz != 15) { - return luaL_error(L, "Invalid cluster message size %d (multi req must be 15)", sz); + if (sz != 13) { + return luaL_error(L, "Invalid cluster message size %d (multi req must be 13)", sz); } uint32_t address = unpack_uint32(buf+1); uint32_t session = unpack_uint32(buf+5); diff --git a/lualib/cluster.lua b/lualib/cluster.lua index e4407f06..dba8f318 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -33,6 +33,16 @@ function cluster.snax(node, name, address) return snax.bind(handle, name) end +function cluster.register(name, addr) + assert(type(name) == "string") + assert(addr == nil or type(addr) == "number") + return skynet.call(clusterd, "lua", "register", name, addr) +end + +function cluster.query(node, name) + return skynet.call(clusterd, "lua", "req", node, 0, skynet.pack(name)) +end + skynet.init(function() clusterd = skynet.uniqueservice("clusterd") end) diff --git a/service/clusterd.lua b/service/clusterd.lua index b3c74722..22d5e9f0 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -97,6 +97,21 @@ function command.proxy(source, node, name) skynet.ret(skynet.pack(proxy[fullname])) end +local register_name = {} + +function command.register(source, name, addr) + assert(register_name[name] == nil) + addr = addr or source + local old_name = register_name[addr] + if old_name then + register_name[old_name] = nil + end + register_name[addr] = name + register_name[name] = addr + skynet.ret(nil) + skynet.error(string.format("Register [%s] :%08x", name, addr)) +end + local large_request = {} function command.socket(source, subcmd, fd, msg) @@ -122,8 +137,20 @@ function command.socket(source, subcmd, fd, msg) return end end - local ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz) - local response + local ok, response + if addr == 0 then + local name = skynet.unpack(msg, sz) + local addr = register_name[name] + if addr then + ok = true + msg, sz = skynet.pack(addr) + else + ok = false + msg = "name not found" + end + else + ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz) + end if ok then response = cluster.packresponse(session, true, msg, sz) if type(response) == "table" then From 1b53e6e28de7cda4dd3df8d974525a25e406e486 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 17 Aug 2015 11:21:20 +0800 Subject: [PATCH 455/729] Release alpha 10 --- HISTORY.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index a970b7c2..48c7a069 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,10 +1,20 @@ +v1.0.0-alpha10 (2015-8-17) +----------- +* Remove the size limit of cluster RPC message. +* Remove the size limit of local message. +* Add cluster.query and clsuter.register. +* Add an option of pthread mutex lock. +* Add skynet.core.intcommand to optimize skynet.sleep etc. +* Fix a memory leak bug in lua shared proto. +* snax.msgserver use string instead of lightuserdata/size. +* Remove some unused api in netpack. +* Raise error when skynet.send to 0. + v1.0.0-alpha9 (2015-8-10) ----------- * Improve lua serialization , support pairs metamethod. * Bugfix : sproto (See commits log of sproto) * Add user log service support (In config) -* Remove the size limit of cluster RPC message. -* Remove the size limit of local message. * Other minor bugfix (See commits log) v1.0.0-alpha8 (2015-6-29) From c0862d8445bf6e20f104b0e171e8e03ca98c58e5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 21 Aug 2015 11:41:01 +0800 Subject: [PATCH 456/729] add global share string table --- 3rd/lua/Makefile | 2 +- 3rd/lua/lapi.c | 4 +- 3rd/lua/lgc.c | 3 +- 3rd/lua/lstring.c | 224 +++++++++++++++++++++++++++++++++++++- 3rd/lua/lstring.h | 5 + 3rd/lua/luaconf.h | 1 + lualib-src/lua-memory.c | 10 ++ service/bootstrap.lua | 4 + service/debug_console.lua | 19 ++++ skynet-src/luashrtbl.h | 17 +++ skynet-src/skynet_main.c | 4 + 11 files changed, 287 insertions(+), 6 deletions(-) create mode 100644 skynet-src/luashrtbl.h diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index 6c680538..ddf7b349 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -19,7 +19,7 @@ SYSCFLAGS= SYSLDFLAGS= SYSLIBS= -MYCFLAGS= +MYCFLAGS=-I../../skynet-src MYLDFLAGS= MYLIBS= MYOBJS= diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 953a304b..3f47f7ee 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1000,7 +1000,7 @@ static Proto * cloneproto (lua_State *L, const Proto *src) { const TValue *s=&src->k[i]; TValue *o=&f->k[i]; if (ttisstring(s)) { - TString * str = luaS_newlstr(L,svalue(s),vslen(s)); + TString * str = luaS_clonestring(L,tsvalue(s)); setsvalue2n(L,o,str); } else { setobj(L,o,s); @@ -1288,7 +1288,7 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { StkId fi = index2addr(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); + api_check(L, (1 <= n && n <= f->p->sp->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; return &f->upvals[n - 1]; /* get its upvalue pointer */ } diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index e73c58bf..a1ce0245 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -188,7 +188,8 @@ void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); - lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ + if (g->allgc != o) + return; /* if object is not 1st in 'allgc' list, it is in global short string table */ white2gray(o); /* they will be gray forever */ g->allgc = o->next; /* remove object from 'allgc' list */ o->next = g->fixedgc; /* link it to 'fixedgc' list */ diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 5e0e3c40..dd36eb87 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -148,10 +148,9 @@ void luaS_remove (lua_State *L, TString *ts) { /* ** checks whether short string exists and reuses it or creates a new one */ -static TString *internshrstr (lua_State *L, const char *str, size_t l) { +static TString *queryshrstr (lua_State *L, const char *str, size_t l, unsigned int h) { TString *ts; global_State *g = G(L); - unsigned int h = luaS_hash(str, l, g->seed); TString **list = &g->strt.hash[lmod(h, g->strt.size)]; for (ts = *list; ts != NULL; ts = ts->u.hnext) { if (l == ts->shrlen && @@ -162,6 +161,13 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { return ts; } } + return NULL; +} + +static TString *addshrstr (lua_State *L, const char *str, size_t l, unsigned int h) { + TString *ts; + global_State *g = G(L); + TString **list = &g->strt.hash[lmod(h, g->strt.size)]; if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { luaS_resize(L, g->strt.size * 2); list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ @@ -174,6 +180,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { return ts; } +static TString *internshrstr (lua_State *L, const char *str, size_t l); /* ** new string (with explicit length) @@ -224,3 +231,216 @@ Udata *luaS_newudata (lua_State *L, size_t s) { return u; } +/* + * global shared table + */ + +#include "rwlock.h" +#include "atomic.h" +#include + +#define SHRSTR_SLOT 0x10000 +#define HASH_NODE(h) ((h) % SHRSTR_SLOT) + +struct shrmap_slot { + struct rwlock lock; + TString *str; +}; + +struct shrmap { + struct shrmap_slot h[SHRSTR_SLOT]; + int n; +}; + +static struct shrmap *SSM = NULL; + +LUA_API void +luaS_initshr() { + struct shrmap * s = malloc(sizeof(*s)); + memset(s, 0, sizeof(*s)); + int i; + for (i=0;ih[i].lock); + } + SSM = s; +} + +LUA_API void +luaS_exitshr() { + int i; + for (i=0;ih[i].str; + while (str) { + TString * next = str->u.hnext; + free(str); + str = next; + } + } + free(SSM); +} + +static TString * +query_string(unsigned int h, const char *str, lu_byte l) { + struct shrmap_slot *s = &SSM->h[HASH_NODE(h)]; + rwlock_rlock(&s->lock); + TString *ts = s->str; + while (ts) { + if (ts->hash == h && + ts->shrlen == l && + memcmp(str, ts+1, l) == 0) { + break; + } + ts = ts->u.hnext; + } + rwlock_runlock(&s->lock); + return ts; +} + +static TString * +query_ptr(TString *t) { + unsigned int h = t->hash; + struct shrmap_slot *s = &SSM->h[HASH_NODE(h)]; + rwlock_rlock(&s->lock); + TString *ts = s->str; + while (ts) { + if (ts == t) + break; + ts = ts->u.hnext; + } + rwlock_runlock(&s->lock); + return ts; +} + +static TString * +new_string(unsigned int h, const char *str, lu_byte l) { + size_t sz = sizelstring(l); + TString *ts = malloc(sz); + memset(ts, 0, sz); + ts->tt = LUA_TSHRSTR; + ts->hash = h; + ts->shrlen = l; + memcpy(ts+1, str, l); + return ts; +} + +static TString * +add_string(unsigned int h, const char *str, lu_byte l) { + TString * tmp = new_string(h, str, l); + struct shrmap_slot *s = &SSM->h[HASH_NODE(h)]; + rwlock_wlock(&s->lock); + TString *ts = s->str; + while (ts) { + if (ts->hash == h && + ts->shrlen == l && + memcmp(str, ts+1, l) == 0) { + break; + } + ts = ts->u.hnext; + } + if (ts == NULL) { + ts = tmp; + ts->u.hnext = s->str; + s->str = ts; + tmp = NULL; + } + rwlock_wunlock(&s->lock); + if (tmp) { + // string is create by other thread, so free tmp + free(tmp); + } + return ts; +} + +static TString * +internshrstr (lua_State *L, const char *str, size_t l) { + TString *ts; + global_State *g = G(L); + unsigned int h = luaS_hash(str, l, g->seed); + unsigned int h0; + // lookup global state of this L first + ts = queryshrstr (L, str, l, h); + if (ts) + return ts; + // lookup SSM again + h0 = luaS_hash(str, l, 0); + ts = query_string(h0, str, l); + if (ts) + return ts; + // If SSM->n greate than 0, add it to SSM + if (SSM->n > 0) { + ATOM_DEC(&SSM->n); + return add_string(h0, str, l); + } + // Else add it to global state (local) + return addshrstr (L, str, l, h); +} + +LUA_API void +luaS_expandshr(int n) { + ATOM_ADD(&SSM->n, n); +} + +LUAI_FUNC TString * +luaS_clonestring(lua_State *L, TString *ts) { + unsigned int h; + int l; + const char * str = getaddrstr(ts); + global_State *g = G(L); + TString *result; + if (ts->tt == LUA_TLNGSTR) + return luaS_newlstr(L, str, ts->u.lnglen); + // look up global state of this L first + l = ts->shrlen; + h = luaS_hash(str, l, g->seed); + result = queryshrstr (L, str, l, h); + if (result) + return result; + // look up SSM by ptr + result = query_ptr(ts); + if (result) + return result; + // ts is not in SSM, so recalc hash, and add it to SSM + h = luaS_hash(str, l, 0); + return add_string(h, str, l); +} + +struct slotinfo { + int len; + int size; +}; + +static void +getslot(struct shrmap_slot *s, struct slotinfo *info) { + memset(info, 0, sizeof(*info)); + rwlock_rlock(&s->lock); + TString *ts = s->str; + while (ts) { + ++info->len; + info->size += ts->shrlen; + ts = ts->u.hnext; + } + rwlock_runlock(&s->lock); +} + +LUA_API int +luaS_shrinfo(lua_State *L) { + struct slotinfo total; + struct slotinfo tmp; + memset(&total, 0, sizeof(total)); + int i; + int len = 0; + for (i=0;ih[i]; + getslot(s, &tmp); + len += tmp.len; + if (tmp.len > total.len) { + total.len = tmp.len; + } + total.size += tmp.size; + } + lua_pushinteger(L, len); + lua_pushinteger(L, total.size); + lua_pushinteger(L, total.len); + lua_pushinteger(L, SSM->n); + return 4; +} diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index e746f5fc..b3cfa1b7 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -43,5 +43,10 @@ LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); +LUA_API void luaS_initshr(); +LUA_API void luaS_exitshr(); +LUA_API void luaS_expandshr(int n); +LUAI_FUNC TString *luaS_clonestring(lua_State *L, TString *); +LUA_API int luaS_shrinfo(lua_State *L); #endif diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index 7cfa4faf..5e88dcb0 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -11,6 +11,7 @@ #include #include +#define LUA_USE_APICHECK /* ** =================================================================== diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index 67a3acf6..a3afecfc 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -2,6 +2,7 @@ #include #include "malloc_hook.h" +#include "luashrtbl.h" static int ltotal(lua_State *L) { @@ -33,6 +34,13 @@ ldump(lua_State *L) { return 0; } +static int +lexpandshrtbl(lua_State *L) { + int n = luaL_checkinteger(L, 1); + luaS_expandshr(n); + return 0; +} + int luaopen_memory(lua_State *L) { luaL_checkversion(L); @@ -43,6 +51,8 @@ luaopen_memory(lua_State *L) { { "dumpinfo", ldumpinfo }, { "dump", ldump }, { "info", dump_mem_lua }, + { "ssinfo", luaS_shrinfo }, + { "ssexpand", lexpandshrtbl }, { NULL, NULL }, }; diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 269654c3..db8d8581 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,8 +1,12 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" require "skynet.manager" -- import skynet.launch, ... +local memory = require "memory" skynet.start(function() + local sharestring = tonumber(skynet.getenv "sharestring") + memory.ssexpand(sharestring or 4096) + local standalone = skynet.getenv "standalone" local launcher = assert(skynet.launch("snlua","launcher")) diff --git a/service/debug_console.lua b/service/debug_console.lua index 97b35806..37b20c30 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -3,6 +3,7 @@ local codecache = require "skynet.codecache" local core = require "skynet.core" local socket = require "socket" local snax = require "snax" +local memory = require "memory" local port = tonumber(...) local COMMAND = {} @@ -130,6 +131,8 @@ function COMMAND.help() log = "launch a new lua service with log", debug = "debug address : debug a lua service", signal = "signal address sig", + cmem = "Show C memory info", + shrtbl = "Show shared short string table info", } end @@ -258,3 +261,19 @@ function COMMAND.signal(address, sig) core.command("SIGNAL", address) end end + +function COMMAND.cmem() + local info = memory.info() + local tmp = {} + for k,v in pairs(info) do + tmp[skynet.address(k)] = v + end + return tmp +end + +function COMMAND.shrtbl() + local n, total, longest, space = memory.ssinfo() + return { n = n, total = total, longest = longest, space = space } +end + + diff --git a/skynet-src/luashrtbl.h b/skynet-src/luashrtbl.h new file mode 100644 index 00000000..6f566038 --- /dev/null +++ b/skynet-src/luashrtbl.h @@ -0,0 +1,17 @@ +#ifndef LUA_SHORT_STRING_TABLE_H +#define LUA_SHORT_STRING_TABLE_H + +#ifndef DISABLE_SHORT_STRING + +#include "lstring.h" + +#else + +static inline int luaS_shrinfo(lua_State *L) { return 0; } +static inline void luaS_initshr() {} +static inline void luaS_exitshr() {} +static inline void luaS_expandshr(int n); + +#endif + +#endif diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 59e344f8..eb6f4429 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -3,6 +3,7 @@ #include "skynet_imp.h" #include "skynet_env.h" #include "skynet_server.h" +#include "luashrtbl.h" #include #include @@ -105,6 +106,8 @@ main(int argc, char *argv[]) { "usage: skynet configfilename\n"); return 1; } + + luaS_initshr(); skynet_globalinit(); skynet_env_init(); @@ -139,6 +142,7 @@ main(int argc, char *argv[]) { skynet_start(&config); skynet_globalexit(); + luaS_exitshr(); return 0; } From 9acce94464d162da93f6aea7dd7c31727feaedb3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 21 Aug 2015 13:55:43 +0800 Subject: [PATCH 457/729] disable lua apicheck --- 3rd/lua/luaconf.h | 1 - 1 file changed, 1 deletion(-) diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index 5e88dcb0..7cfa4faf 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -11,7 +11,6 @@ #include #include -#define LUA_USE_APICHECK /* ** =================================================================== From bb6d5d826ec029a025d2bb5525a30d166e098df9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 21 Aug 2015 15:59:51 +0800 Subject: [PATCH 458/729] add TString struct size --- 3rd/lua/lstring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index dd36eb87..3516747a 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -416,7 +416,7 @@ getslot(struct shrmap_slot *s, struct slotinfo *info) { TString *ts = s->str; while (ts) { ++info->len; - info->size += ts->shrlen; + info->size += sizelstring(ts->shrlen); ts = ts->u.hnext; } rwlock_runlock(&s->lock); From eb587c63d3897f5f3a5f14b07e125cea910b79af Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 21 Aug 2015 19:42:09 +0800 Subject: [PATCH 459/729] fix issue #327 --- skynet-src/skynet_server.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 4972e7a4..64258a92 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -674,7 +674,9 @@ int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) { if ((sz & MESSAGE_TYPE_MASK) != sz) { skynet_error(context, "The message to %x is too large", destination); - skynet_free(data); + if (type & PTYPE_TAG_DONTCOPY) { + skynet_free(data); + } return -1; } _filter_args(context, type, &session, (void **)&data, &sz); From 010d1c1d4f9826de9f56868ef13faba1261d59d8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 24 Aug 2015 11:50:40 +0800 Subject: [PATCH 460/729] avoid dead loop --- service-src/service_harbor.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 5ff618f1..ed05190b 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -317,8 +317,11 @@ forward_local_messsage(struct harbor *h, void *msg, int sz) { destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); if (skynet_send(h->ctx, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) { - skynet_send(h->ctx, destination, header.source , PTYPE_ERROR, (int)header.session, NULL, 0); - skynet_error(h->ctx, "Unknown destination :%x from :%x", destination, header.source); + if (type != PTYPE_ERROR) { + // don't need report error when type is error + skynet_send(h->ctx, destination, header.source , PTYPE_ERROR, (int)header.session, NULL, 0); + } + skynet_error(h->ctx, "Unknown destination :%x from :%x type(%d)", destination, header.source, type); } } From 9b0e496afe2781c5f01bbbbd90db9a753fcd38e1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 27 Aug 2015 13:49:31 +0800 Subject: [PATCH 461/729] minor amend --- 3rd/lua/README | 7 ++++--- 3rd/lua/lstring.h | 2 ++ skynet-src/luashrtbl.h | 5 ++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/3rd/lua/README b/3rd/lua/README index a77dc6bf..efb71d70 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,5 +1,6 @@ This is a modify version of lua 5.3.1 (http://www.lua.org/ftp/lua-5.3.1.tar.gz) . -For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html - - +For detail , + Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html + Shared short string table : http://blog.codingnow.com/2015/08/lua_vm_share_string.html + \ No newline at end of file diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index b3cfa1b7..a28ff0a0 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -43,6 +43,8 @@ LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); +#define ENABLE_SHORT_STRING_TABLE + LUA_API void luaS_initshr(); LUA_API void luaS_exitshr(); LUA_API void luaS_expandshr(int n); diff --git a/skynet-src/luashrtbl.h b/skynet-src/luashrtbl.h index 6f566038..824a0c85 100644 --- a/skynet-src/luashrtbl.h +++ b/skynet-src/luashrtbl.h @@ -1,11 +1,10 @@ #ifndef LUA_SHORT_STRING_TABLE_H #define LUA_SHORT_STRING_TABLE_H -#ifndef DISABLE_SHORT_STRING - #include "lstring.h" -#else +// If you use modified lua, this macro would be defined in lstring.h +#ifndef ENABLE_SHORT_STRING_TABLE static inline int luaS_shrinfo(lua_State *L) { return 0; } static inline void luaS_initshr() {} From 1bfddda435e0c7952d1d6732a35c659364d6782d Mon Sep 17 00:00:00 2001 From: snail Date: Thu, 27 Aug 2015 18:04:56 +0800 Subject: [PATCH 462/729] uncomplete buffer not free 'uncomplete' buffer not freed in clear --- lualib-src/lua-netpack.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 193938b1..6e601e07 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -49,6 +49,7 @@ struct queue { static void clear_list(struct uncomplete * uc) { while (uc) { + skynet_free(uc->pack.buffer); void * tmp = uc; uc = uc->next; skynet_free(tmp); From 633e5fbe324fefeb4642ebf650cd01f185bbc91b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 29 Aug 2015 15:24:24 +0800 Subject: [PATCH 463/729] fix issue #331 --- 3rd/lua/lstring.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 3516747a..5046624d 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -252,36 +252,33 @@ struct shrmap { int n; }; -static struct shrmap *SSM = NULL; +static struct shrmap SSM; LUA_API void luaS_initshr() { - struct shrmap * s = malloc(sizeof(*s)); - memset(s, 0, sizeof(*s)); + struct shrmap * s = &SSM; int i; for (i=0;ih[i].lock); } - SSM = s; } LUA_API void luaS_exitshr() { int i; for (i=0;ih[i].str; + TString *str = SSM.h[i].str; while (str) { TString * next = str->u.hnext; free(str); str = next; } } - free(SSM); } static TString * query_string(unsigned int h, const char *str, lu_byte l) { - struct shrmap_slot *s = &SSM->h[HASH_NODE(h)]; + struct shrmap_slot *s = &SSM.h[HASH_NODE(h)]; rwlock_rlock(&s->lock); TString *ts = s->str; while (ts) { @@ -299,7 +296,7 @@ query_string(unsigned int h, const char *str, lu_byte l) { static TString * query_ptr(TString *t) { unsigned int h = t->hash; - struct shrmap_slot *s = &SSM->h[HASH_NODE(h)]; + struct shrmap_slot *s = &SSM.h[HASH_NODE(h)]; rwlock_rlock(&s->lock); TString *ts = s->str; while (ts) { @@ -326,7 +323,7 @@ new_string(unsigned int h, const char *str, lu_byte l) { static TString * add_string(unsigned int h, const char *str, lu_byte l) { TString * tmp = new_string(h, str, l); - struct shrmap_slot *s = &SSM->h[HASH_NODE(h)]; + struct shrmap_slot *s = &SSM.h[HASH_NODE(h)]; rwlock_wlock(&s->lock); TString *ts = s->str; while (ts) { @@ -366,9 +363,9 @@ internshrstr (lua_State *L, const char *str, size_t l) { ts = query_string(h0, str, l); if (ts) return ts; - // If SSM->n greate than 0, add it to SSM - if (SSM->n > 0) { - ATOM_DEC(&SSM->n); + // If SSM.n greate than 0, add it to SSM + if (SSM.n > 0) { + ATOM_DEC(&SSM.n); return add_string(h0, str, l); } // Else add it to global state (local) @@ -377,7 +374,7 @@ internshrstr (lua_State *L, const char *str, size_t l) { LUA_API void luaS_expandshr(int n) { - ATOM_ADD(&SSM->n, n); + ATOM_ADD(&SSM.n, n); } LUAI_FUNC TString * @@ -430,7 +427,7 @@ luaS_shrinfo(lua_State *L) { int i; int len = 0; for (i=0;ih[i]; + struct shrmap_slot *s = &SSM.h[i]; getslot(s, &tmp); len += tmp.len; if (tmp.len > total.len) { @@ -441,6 +438,6 @@ luaS_shrinfo(lua_State *L) { lua_pushinteger(L, len); lua_pushinteger(L, total.size); lua_pushinteger(L, total.len); - lua_pushinteger(L, SSM->n); + lua_pushinteger(L, SSM.n); return 4; } From 4f665d2469e5f278835ea388a76f95e8dadc22d1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 30 Aug 2015 12:13:23 +0800 Subject: [PATCH 464/729] init global string table, See issue #331 --- 3rd/lua/lua.c | 5 ++++- 3rd/lua/luac.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 7a47582c..c6e81a55 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -18,6 +18,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "lstring.h" #if !defined(LUA_PROMPT) @@ -596,7 +597,9 @@ static int pmain (lua_State *L) { int main (int argc, char **argv) { int status, result; - lua_State *L = luaL_newstate(); /* create state */ + lua_State *L; + luaS_initshr(); /* init global short string table */ + L = luaL_newstate(); /* create state */ if (L == NULL) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; diff --git a/3rd/lua/luac.c b/3rd/lua/luac.c index 0ff987b4..86d47518 100644 --- a/3rd/lua/luac.c +++ b/3rd/lua/luac.c @@ -21,6 +21,7 @@ #include "lobject.h" #include "lstate.h" #include "lundump.h" +#include "lstring.h" static void PrintFunction(const Proto* f, int full); #define luaU_print PrintFunction @@ -195,6 +196,7 @@ int main(int argc, char* argv[]) int i=doargs(argc,argv); argc-=i; argv+=i; if (argc<=0) usage("no input files given"); + luaS_initshr(); L=luaL_newstate(); if (L==NULL) fatal("cannot create state: not enough memory"); lua_pushcfunction(L,&pmain); From bad3472124e0dce5ca8ff76691e2bafe18b787ae Mon Sep 17 00:00:00 2001 From: snail Date: Sun, 30 Aug 2015 16:47:34 +0800 Subject: [PATCH 465/729] 'response' not released 'response' not released after src service died, and defined error. --- lualib/skynet.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index e4f46908..56d62e6c 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -186,10 +186,12 @@ function suspend(co, result, command, param, size) error(debug.traceback(co)) end local f = param - local function response(ok, ...) + local response + response = function(ok, ...) if ok == "TEST" then if dead_service[co_address] then release_watching(co_address) + unresponse[response] = nil f = false return false else @@ -224,7 +226,7 @@ function suspend(co, result, command, param, size) return ret end watching_service[co_address] = watching_service[co_address] + 1 - session_response[co] = response + session_response[co] = true unresponse[response] = true return suspend(co, coroutine.resume(co, response)) elseif command == "EXIT" then From 7f9c3c8c9a99b87adff4f3d732b2b9bf905b4d0b Mon Sep 17 00:00:00 2001 From: snail Date: Sun, 30 Aug 2015 18:17:41 +0800 Subject: [PATCH 466/729] local function is better local function is the same as " local a; a = function" --- lualib/skynet.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 56d62e6c..554d8ff8 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -186,8 +186,7 @@ function suspend(co, result, command, param, size) error(debug.traceback(co)) end local f = param - local response - response = function(ok, ...) + local function response(ok, ...) if ok == "TEST" then if dead_service[co_address] then release_watching(co_address) From 7797de85b457cf6f724748804a3801f6b5c5a68a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 31 Aug 2015 16:36:57 +0800 Subject: [PATCH 467/729] bugfix: See PR #332 --- service/sharedatad.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index a7e8d427..363f59ef 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -123,6 +123,7 @@ function CMD.monitor(name, obj) if n > pool_count[name].threshold then n = n - check_watch(v.watch) pool_count[name].threshold = n * 2 + pool_count[name].n = n end table.insert(v.watch, skynet.response()) From 6c33f7bc44d0eafe743e5490af3b1e5d86727c45 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 4 Sep 2015 12:14:09 +0800 Subject: [PATCH 468/729] see pr #332 --- service/sharedatad.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 363f59ef..d76fd115 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -118,13 +118,12 @@ function CMD.monitor(name, obj) return v.obj end - local n = pool_count[name].n - pool_count[name].n = n + 1 + local n = pool_count[name].n + 1 if n > pool_count[name].threshold then n = n - check_watch(v.watch) pool_count[name].threshold = n * 2 - pool_count[name].n = n end + pool_count[name].n = n table.insert(v.watch, skynet.response()) From 846d55eaef0d8e64338ecf323a51718611186d6f Mon Sep 17 00:00:00 2001 From: sanikoyes Date: Mon, 14 Sep 2015 18:03:04 +0800 Subject: [PATCH 469/729] add missing luaS_exitshr call --- 3rd/lua/lua.c | 1 + 1 file changed, 1 insertion(+) diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index c6e81a55..825d7c21 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -611,6 +611,7 @@ int main (int argc, char **argv) { result = lua_toboolean(L, -1); /* get result */ report(L, status); lua_close(L); + luaS_exitshr(); return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; } From b8f419eb691f64bd3c33ff9836549d245d081fb7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 15 Sep 2015 14:52:30 +0800 Subject: [PATCH 470/729] more error message for socket, and close listen socket when reach limit of open files. See Issue #339 --- lualib/snax/gateserver.lua | 19 +++++++++++++------ lualib/socket.lua | 13 ++++++++----- skynet-src/skynet_socket.c | 8 ++++++-- skynet-src/socket_server.c | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index dfce2fa9..68f77b58 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -101,17 +101,24 @@ function gateserver.start(handler) end function MSG.close(fd) - if handler.disconnect then - handler.disconnect(fd) + if fd ~= socket then + if handler.disconnect then + handler.disconnect(fd) + end + close_fd(fd) end - close_fd(fd) end function MSG.error(fd, msg) - if handler.error then - handler.error(fd, msg) + if fd == socket then + socketdriver.close(fd) + skynet.error(msg) + else + if handler.error then + handler.error(fd, msg) + end + close_fd(fd) end - close_fd(fd) end function MSG.warning(fd, size) diff --git a/lualib/socket.lua b/lualib/socket.lua index c5fe7858..7f1d603b 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -106,16 +106,17 @@ socket_message[4] = function(id, newid, addr) end -- SKYNET_SOCKET_TYPE_ERROR = 5 -socket_message[5] = function(id) +socket_message[5] = function(id, _, err) local s = socket_pool[id] if s == nil then - skynet.error("socket: error on unknown", id) + skynet.error("socket: error on unknown", id, err) return end - if s.connected then - skynet.error("socket: error on", id) + if s.connected or s.connecting then + skynet.error("socket: error on", id, err) end s.connected = false + driver.close(id) wakeup(s) end @@ -170,6 +171,7 @@ local function connect(id, func) id = id, buffer = newbuffer, connected = false, + connecting = true, read_required = false, co = false, callback = func, @@ -177,6 +179,7 @@ local function connect(id, func) } socket_pool[id] = s suspend(s) + s.connecting = nil if s.connected then return id else @@ -221,7 +224,7 @@ function socket.close(id) return end if s.connected then - driver.close(s.id) + driver.close(id) -- notice: call socket.close in __gc should be carefully, -- because skynet.wait never return in __gc, so driver.clear may not be called if s.co then diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 9301419f..f6f8ec56 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -36,7 +36,11 @@ forward_message(int type, bool padding, struct socket_message * result) { size_t sz = sizeof(*sm); if (padding) { if (result->data) { - sz += strlen(result->data); + size_t msg_sz = strlen(result->data); + if (msg_sz > 128) { + msg_sz = 128; + } + sz += msg_sz; } else { result->data = ""; } @@ -86,7 +90,7 @@ skynet_socket_poll() { forward_message(SKYNET_SOCKET_TYPE_CONNECT, true, &result); break; case SOCKET_ERROR: - forward_message(SKYNET_SOCKET_TYPE_ERROR, false, &result); + forward_message(SKYNET_SOCKET_TYPE_ERROR, true, &result); break; case SOCKET_ACCEPT: forward_message(SKYNET_SOCKET_TYPE_ACCEPT, true, &result); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index b2454d2e..7724a3c5 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -408,6 +408,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock status = getaddrinfo( request->host, port, &ai_hints, &ai_list ); if ( status != 0 ) { + result->data = (void *)gai_strerror(status); goto _failed; } int sock= -1; @@ -428,12 +429,14 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock } if (sock < 0) { + result->data = strerror(errno); goto _failed; } ns = new_fd(ss, id, sock, PROTOCOL_TCP, request->opaque, true); if (ns == NULL) { close(sock); + result->data = "reach skynet socket number limit"; goto _failed; } @@ -762,7 +765,7 @@ _failed: result->opaque = request->opaque; result->id = id; result->ud = 0; - result->data = NULL; + result->data = "reach skynet socket number limit"; ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return SOCKET_ERROR; @@ -803,7 +806,7 @@ bind_socket(struct socket_server *ss, struct request_bind *request, struct socke result->ud = 0; struct socket *s = new_fd(ss, id, request->fd, PROTOCOL_TCP, request->opaque, true); if (s == NULL) { - result->data = NULL; + result->data = "reach skynet socket number limit"; return SOCKET_ERROR; } sp_nonblocking(request->fd); @@ -821,11 +824,13 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc result->data = NULL; struct socket *s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { + result->data = "invalid socket"; return SOCKET_ERROR; } if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { if (sp_add(ss->event_fd, s->fd, s)) { s->type = SOCKET_TYPE_INVALID; + result->data = strerror(errno); return SOCKET_ERROR; } s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; @@ -913,7 +918,7 @@ set_udp_address(struct socket_server *ss, struct request_setudp *request, struct result->opaque = s->opaque; result->id = s->id; result->ud = 0; - result->data = NULL; + result->data = "protocol mismatch"; return SOCKET_ERROR; } @@ -995,6 +1000,7 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_me default: // close when error force_close(ss, s, result); + result->data = strerror(errno); return SOCKET_ERROR; } return -1; @@ -1055,6 +1061,7 @@ forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_me default: // close when error force_close(ss, s, result); + result->data = strerror(errno); return SOCKET_ERROR; } return -1; @@ -1088,6 +1095,7 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); if (code < 0 || error) { force_close(ss,s, result); + result->data = strerror(errno); return SOCKET_ERROR; } else { s->type = SOCKET_TYPE_CONNECTED; @@ -1111,14 +1119,22 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message } } -// return 0 when failed +// return 0 when failed, or -1 when file limit static int report_accept(struct socket_server *ss, struct socket *s, struct socket_message *result) { union sockaddr_all u; socklen_t len = sizeof(u); int client_fd = accept(s->fd, &u.s, &len); if (client_fd < 0) { - return 0; + if (errno == EMFILE || errno == ENFILE) { + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = strerror(errno); + return -1; + } else { + return 0; + } } int id = reserve_id(ss); if (id < 0) { @@ -1203,11 +1219,16 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int switch (s->type) { case SOCKET_TYPE_CONNECTING: return report_connect(ss, s, result); - case SOCKET_TYPE_LISTEN: - if (report_accept(ss, s, result)) { + case SOCKET_TYPE_LISTEN: { + int ok = report_accept(ss, s, result); + if (ok > 0) { return SOCKET_ACCEPT; - } + } if (ok < 0 ) { + return SOCKET_ERROR; + } + // when ok == 0, retry break; + } case SOCKET_TYPE_INVALID: fprintf(stderr, "socket-server: invalid socket\n"); break; From 00dbf6e9969e7f6ea498cffd3740d991847db710 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 18 Sep 2015 14:10:12 +0800 Subject: [PATCH 471/729] DH key exchange can't be 0 , avoid attack --- lualib-src/lua-crypt.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 0284c48b..2cb11f40 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -338,8 +338,13 @@ static int lrandomkey(lua_State *L) { char tmp[8]; int i; + char x = 0; for (i=0;i<8;i++) { tmp[i] = random() & 0xff; + x ^= tmp[i]; + } + if (x==0) { + tmp[0] |= 1; // avoid 0 } lua_pushlstring(L, tmp, 8); return 1; @@ -718,8 +723,13 @@ static int ldhsecret(lua_State *L) { uint32_t x[2], y[2]; read64(L, x, y); - uint64_t r = powmodp((uint64_t)x[0] | (uint64_t)x[1]<<32, - (uint64_t)y[0] | (uint64_t)y[1]<<32); + uint64_t xx = (uint64_t)x[0] | (uint64_t)x[1]<<32; + uint64_t yy = (uint64_t)y[0] | (uint64_t)y[1]<<32; + if (xx == 0) + xx = 1; + if (yy == 0) + yy = 1; + uint64_t r = powmodp(xx, yy); push64(L, r); @@ -739,7 +749,11 @@ ldhexchange(lua_State *L) { xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; - uint64_t r = powmodp(5, (uint64_t)xx[0] | (uint64_t)xx[1]<<32); + uint64_t x64 = (uint64_t)xx[0] | (uint64_t)xx[1]<<32; + if (x64 == 0) + x64 = 1; + + uint64_t r = powmodp(5, x64); push64(L, r); return 1; } From 008351573a518ab92aea5431363e9d45414ec26a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 18 Sep 2015 14:13:16 +0800 Subject: [PATCH 472/729] raise error when DH param is 0 --- lualib-src/lua-crypt.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 2cb11f40..405679a9 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -725,10 +725,8 @@ ldhsecret(lua_State *L) { read64(L, x, y); uint64_t xx = (uint64_t)x[0] | (uint64_t)x[1]<<32; uint64_t yy = (uint64_t)y[0] | (uint64_t)y[1]<<32; - if (xx == 0) - xx = 1; - if (yy == 0) - yy = 1; + if (xx == 0 || yy == 0) + return luaL_error(L, "Can't be 0"); uint64_t r = powmodp(xx, yy); push64(L, r); @@ -751,7 +749,7 @@ ldhexchange(lua_State *L) { uint64_t x64 = (uint64_t)xx[0] | (uint64_t)xx[1]<<32; if (x64 == 0) - x64 = 1; + return luaL_error(L, "Can't be 0"); uint64_t r = powmodp(5, x64); push64(L, r); From 752a501f681a4a692971a49ebd2d4cc389ddbd75 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 21 Sep 2015 10:40:09 +0800 Subject: [PATCH 473/729] bugfix: remove DONTCOPY tag from type --- service-src/service_harbor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index ed05190b..70abe82b 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -313,10 +313,10 @@ forward_local_messsage(struct harbor *h, void *msg, int sz) { message_to_header((const uint32_t *)cookie, &header); uint32_t destination = header.destination; - int type = (destination >> HANDLE_REMOTE_SHIFT) | PTYPE_TAG_DONTCOPY; + int type = destination >> HANDLE_REMOTE_SHIFT; destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); - if (skynet_send(h->ctx, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) { + if (skynet_send(h->ctx, header.source, destination, type | PTYPE_TAG_DONTCOPY , (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) { if (type != PTYPE_ERROR) { // don't need report error when type is error skynet_send(h->ctx, destination, header.source , PTYPE_ERROR, (int)header.session, NULL, 0); From 325452a242657ac0c87a71a19e625d8ba16bd3b3 Mon Sep 17 00:00:00 2001 From: bingo Date: Mon, 21 Sep 2015 11:21:36 +0800 Subject: [PATCH 474/729] Signed-off-by: bingo --- lualib/snax/gateserver.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 68f77b58..bf62e2ce 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -48,7 +48,6 @@ function gateserver.start(handler) function CMD.close() assert(socket) socketdriver.close(socket) - socket = nil end local MSG = {} @@ -106,6 +105,8 @@ function gateserver.start(handler) handler.disconnect(fd) end close_fd(fd) + else + socket = nil end end From 63942496fdfa7c3b98364e86fab067165da4e270 Mon Sep 17 00:00:00 2001 From: great90 <897346026@qq.com> Date: Mon, 21 Sep 2015 23:50:18 +0800 Subject: [PATCH 475/729] =?UTF-8?q?=E4=BB=8Echeck=5Fwsz=E5=8F=91=E9=80=81?= =?UTF-8?q?=E7=9A=84SKYNET=5FSOCKET=5FTYPE=5FWARNING=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=BC=9Aassert=E5=A4=B1=E8=B4=A5=EF=BC=8C=E5=AF=BC=E8=87=B4cor?= =?UTF-8?q?edump?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service-src/service_gate.c | 1 - 1 file changed, 1 deletion(-) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index ec44c722..d59b4050 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -295,7 +295,6 @@ _cb(struct skynet_context * ctx, void * ud, int type, int session, uint32_t sour } } case PTYPE_SOCKET: - assert(source == 0); // recv socket message from skynet_socket dispatch_socket_message(g, msg, (int)(sz-sizeof(struct skynet_socket_message))); break; From cb9feb34bd263d3c0390c2ca2c55c887026ba945 Mon Sep 17 00:00:00 2001 From: zhengfangxin Date: Sat, 26 Sep 2015 21:03:38 +0800 Subject: [PATCH 476/729] delete two no need call clear_closed_event --- skynet-src/socket_server.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 7724a3c5..4963dd37 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1176,6 +1176,7 @@ clear_closed_event(struct socket_server *ss, struct socket_message * result, int if (s) { if (s->type == SOCKET_TYPE_INVALID && s->id == id) { e->s = NULL; + break; } } } @@ -1245,21 +1246,19 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int return SOCKET_UDP; } } - if (e->write) { + if (e->write && type != SOCKET_CLOSE && type != SOCKET_ERROR) { // Try to dispatch write message next step if write flag set. e->read = false; --ss->event_index; } if (type == -1) - break; - clear_closed_event(ss, result, type); + break; return type; } if (e->write) { int type = send_buffer(ss, s, result); if (type == -1) break; - clear_closed_event(ss, result, type); return type; } break; From 97ff6afc8438a22a630bd0acb102555b21760c1d Mon Sep 17 00:00:00 2001 From: xjdrew Date: Tue, 29 Sep 2015 17:29:33 +0800 Subject: [PATCH 477/729] removte unused code --- service-src/service_gate.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index d59b4050..29eab2ea 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -335,13 +335,12 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) { if (parm == NULL) return 1; int max = 0; - int buffer = 0; int sz = strlen(parm)+1; char watchdog[sz]; char binding[sz]; int client_tag = 0; char header; - int n = sscanf(parm, "%c %s %s %d %d %d",&header,watchdog, binding,&client_tag , &max,&buffer); + int n = sscanf(parm, "%c %s %s %d %d %d", &header, watchdog, binding, &client_tag, &max); if (n<4) { skynet_error(ctx, "Invalid gate parm %s",parm); return 1; From 97fd5053c9c46fbca4990b353e412d49129f68ae Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Oct 2015 09:56:12 +0800 Subject: [PATCH 478/729] update lpeg to 1.0.0 --- 3rd/lpeg/HISTORY | 8 +- 3rd/lpeg/lpcap.c | 4 +- 3rd/lpeg/lpcap.h | 4 +- 3rd/lpeg/lpcode.c | 22 +-- 3rd/lpeg/lpcode.h | 10 +- 3rd/lpeg/lpeg.html | 27 +-- 3rd/lpeg/lpprint.c | 8 +- 3rd/lpeg/lpprint.h | 3 +- 3rd/lpeg/lptree.c | 446 +++++++++++++++++++++++++-------------------- 3rd/lpeg/lptypes.h | 34 ++-- 3rd/lpeg/lpvm.c | 6 +- 3rd/lpeg/re.html | 8 +- 3rd/lpeg/test.lua | 55 +++++- 13 files changed, 376 insertions(+), 259 deletions(-) diff --git a/3rd/lpeg/HISTORY b/3rd/lpeg/HISTORY index 8ada7743..0c10edd0 100644 --- a/3rd/lpeg/HISTORY +++ b/3rd/lpeg/HISTORY @@ -1,4 +1,10 @@ -HISTORY for LPeg 0.12 +HISTORY for LPeg 1.0 + +* Changes from version 0.12 to 1.0 + --------------------------------- + + group "names" can be any Lua value + + some bugs fixed + + other small improvements * Changes from version 0.11 to 0.12 --------------------------------- diff --git a/3rd/lpeg/lpcap.c b/3rd/lpeg/lpcap.c index b6911cb1..c9085de0 100644 --- a/3rd/lpeg/lpcap.c +++ b/3rd/lpeg/lpcap.c @@ -1,5 +1,5 @@ /* -** $Id: lpcap.c,v 1.5 2014/12/12 16:58:47 roberto Exp $ +** $Id: lpcap.c,v 1.6 2015/06/15 16:09:57 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -126,7 +126,7 @@ static Capture *findback (CapState *cs, Capture *cap) { continue; /* opening an enclosing capture: skip and get previous */ if (captype(cap) == Cgroup) { getfromktable(cs, cap->idx); /* get group name */ - if (lua_equal(L, -2, -1)) { /* right group? */ + if (lp_equal(L, -2, -1)) { /* right group? */ lua_pop(L, 2); /* remove reference name and group name */ return cap; } diff --git a/3rd/lpeg/lpcap.h b/3rd/lpeg/lpcap.h index c0a0e382..d762fdcf 100644 --- a/3rd/lpeg/lpcap.h +++ b/3rd/lpeg/lpcap.h @@ -1,5 +1,5 @@ /* -** $Id: lpcap.h,v 1.1 2013/03/21 20:25:12 roberto Exp $ +** $Id: lpcap.h,v 1.2 2015/02/27 17:13:17 roberto Exp $ */ #if !defined(lpcap_h) @@ -18,7 +18,7 @@ typedef enum CapKind { typedef struct Capture { const char *s; /* subject position */ - short idx; /* extra info about capture (group name, arg index, etc.) */ + unsigned short idx; /* extra info (group name, arg index, etc.) */ byte kind; /* kind of capture */ byte siz; /* size of full capture + 1 (0 = not a full capture) */ } Capture; diff --git a/3rd/lpeg/lpcode.c b/3rd/lpeg/lpcode.c index 93c0d2aa..fbf44feb 100644 --- a/3rd/lpeg/lpcode.c +++ b/3rd/lpeg/lpcode.c @@ -1,5 +1,5 @@ /* -** $Id: lpcode.c,v 1.21 2014/12/12 17:01:29 roberto Exp $ +** $Id: lpcode.c,v 1.23 2015/06/12 18:36:47 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -431,11 +431,11 @@ typedef struct CompileState { /* -** code generation is recursive; 'opt' indicates that the code is -** being generated under a 'IChoice' operator jumping to its end -** (that is, the match is "optional"). -** 'tt' points to a previous test protecting this code. 'fl' is -** the follow set of the pattern. +** code generation is recursive; 'opt' indicates that the code is being +** generated as the last thing inside an optional pattern (so, if that +** code is optional too, it can reuse the 'IChoice' already in place for +** the outer pattern). 'tt' points to a previous test protecting this +** code (or NOINST). 'fl' is the follow set of the pattern. */ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, const Charset *fl); @@ -638,13 +638,13 @@ static void codebehind (CompileState *compst, TTree *tree) { /* ** Choice; optimizations: -** - when p1 is headfail -** - when first(p1) and first(p2) are disjoint; than +** - when p1 is headfail or +** when first(p1) and first(p2) are disjoint, than ** a character not in first(p1) cannot go to p1, and a character ** in first(p1) cannot go to p2 (at it is not in first(p2)). ** (The optimization is not valid if p1 accepts the empty string, ** as then there is no character at all...) -** - when p2 is empty and opt is true; a IPartialCommit can resuse +** - when p2 is empty and opt is true; a IPartialCommit can reuse ** the Choice already active in the stack. */ static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt, @@ -671,7 +671,7 @@ static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt, } else { /* == - test(fail(p1)) -> L1; choice L1; ; commit L2; L1: ; L2: */ + test(first(p1)) -> L1; choice L1; ; commit L2; L1: ; L2: */ int pcommit; int test = codetestset(compst, &cs1, e1); int pchoice = addoffsetinst(compst, IChoice); @@ -759,7 +759,7 @@ static void coderep (CompileState *compst, TTree *tree, int opt, /* L1: test (fail(p1)) -> L2;

; jmp L1; L2: */ int jmp; int test = codetestset(compst, &st, 0); - codegen(compst, tree, opt, test, fullset); + codegen(compst, tree, 0, test, fullset); jmp = addoffsetinst(compst, IJmp); jumptohere(compst, test); jumptothere(compst, jmp, test); diff --git a/3rd/lpeg/lpcode.h b/3rd/lpeg/lpcode.h index 72d2bb94..896d3c79 100644 --- a/3rd/lpeg/lpcode.h +++ b/3rd/lpeg/lpcode.h @@ -1,5 +1,5 @@ /* -** $Id: lpcode.h,v 1.6 2013/11/28 14:56:02 roberto Exp $ +** $Id: lpcode.h,v 1.7 2015/06/12 18:24:45 roberto Exp $ */ #if !defined(lpcode_h) @@ -24,7 +24,15 @@ int sizei (const Instruction *i); #define PEnullable 0 #define PEnofail 1 +/* +** nofail(t) implies that 't' cannot fail with any input +*/ #define nofail(t) checkaux(t, PEnofail) + +/* +** (not nullable(t)) implies 't' cannot match without consuming +** something +*/ #define nullable(t) checkaux(t, PEnullable) #define fixedlen(t) fixedlenx(t, 0, 0) diff --git a/3rd/lpeg/lpeg.html b/3rd/lpeg/lpeg.html index 0eb1747f..c0a7f090 100644 --- a/3rd/lpeg/lpeg.html +++ b/3rd/lpeg/lpeg.html @@ -10,7 +10,7 @@ - +

@@ -22,7 +22,7 @@
LPeg
- Parsing Expression Grammars For Lua, version 0.12 + Parsing Expression Grammars For Lua, version 1.0
@@ -195,13 +195,16 @@ Returns a string with the running version of LPeg.

lpeg.setmaxstack (max)

-Sets the maximum size for the backtrack stack used by LPeg to +Sets a limit for the size of the backtrack stack used by LPeg to track calls and choices. +(The default limit is 400.) Most well-written patterns need little backtrack levels and -therefore you seldom need to change this maximum; -but a few useful patterns may need more space. -Before changing this maximum you should try to rewrite your +therefore you seldom need to change this limit; +before changing it you should try to rewrite your pattern to avoid the need for extra space. +Nevertheless, a few useful patterns may overflow. +Also, with recursive grammars, +subjects with deep recursion may also need larger limits.

@@ -682,7 +685,8 @@ argument given in the call to lpeg.match. Creates a back capture. This pattern matches the empty string and produces the values produced by the most recent -group capture named name. +group capture named name +(where name can be any Lua value).

@@ -762,7 +766,8 @@ Creates a group capture. It groups all values returned by patt into a single capture. The group may be anonymous (if no name is given) -or named with the given name. +or named with the given name +(which can be any non-nil Lua value).

@@ -1375,13 +1380,13 @@ and the new term for each repetition.

Download

LPeg -source code.

+source code.

License

-Copyright © 2014 Lua.org, PUC-Rio. +Copyright © 2007-2015 Lua.org, PUC-Rio.

Permission is hereby granted, free of charge, @@ -1419,7 +1424,7 @@ THE SOFTWARE.

-$Id: lpeg.html,v 1.72 2014/12/12 17:11:35 roberto Exp $ +$Id: lpeg.html,v 1.75 2015/09/28 17:17:41 roberto Exp $

diff --git a/3rd/lpeg/lpprint.c b/3rd/lpeg/lpprint.c index 05fa6488..174d1687 100644 --- a/3rd/lpeg/lpprint.c +++ b/3rd/lpeg/lpprint.c @@ -1,5 +1,5 @@ /* -** $Id: lpprint.c,v 1.7 2013/04/12 16:29:49 roberto Exp $ +** $Id: lpprint.c,v 1.9 2015/06/15 16:09:57 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -52,7 +52,7 @@ static void printjmp (const Instruction *op, const Instruction *p) { } -static void printinst (const Instruction *op, const Instruction *p) { +void printinst (const Instruction *op, const Instruction *p) { const char *const names[] = { "any", "char", "set", "testany", "testchar", "testset", @@ -221,10 +221,10 @@ void printtree (TTree *tree, int ident) { void printktable (lua_State *L, int idx) { int n, i; - lua_getfenv(L, idx); + lua_getuservalue(L, idx); if (lua_isnil(L, -1)) /* no ktable? */ return; - n = lua_objlen(L, -1); + n = lua_rawlen(L, -1); printf("["); for (i = 1; i <= n; i++) { printf("%d = ", i); diff --git a/3rd/lpeg/lpprint.h b/3rd/lpeg/lpprint.h index e640f744..63297607 100644 --- a/3rd/lpeg/lpprint.h +++ b/3rd/lpeg/lpprint.h @@ -1,5 +1,5 @@ /* -** $Id: lpprint.h,v 1.1 2013/03/21 20:25:12 roberto Exp $ +** $Id: lpprint.h,v 1.2 2015/06/12 18:18:08 roberto Exp $ */ @@ -18,6 +18,7 @@ void printtree (TTree *tree, int ident); void printktable (lua_State *L, int idx); void printcharset (const byte *st); void printcaplist (Capture *cap, Capture *limit); +void printinst (const Instruction *op, const Instruction *p); #else diff --git a/3rd/lpeg/lptree.c b/3rd/lpeg/lptree.c index 7c5b8200..ac5f5150 100644 --- a/3rd/lpeg/lptree.c +++ b/3rd/lpeg/lptree.c @@ -1,5 +1,5 @@ /* -** $Id: lptree.c,v 1.13 2014/12/12 16:59:10 roberto Exp $ +** $Id: lptree.c,v 1.21 2015/09/28 17:01:25 roberto Exp $ ** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -126,6 +126,189 @@ static void finalfix (lua_State *L, int postable, TTree *g, TTree *t) { } + +/* +** {=================================================================== +** KTable manipulation +** +** - The ktable of a pattern 'p' can be shared by other patterns that +** contain 'p' and no other constants. Because of this sharing, we +** should not add elements to a 'ktable' unless it was freshly created +** for the new pattern. +** +** - The maximum index in a ktable is USHRT_MAX, because trees and +** patterns use unsigned shorts to store those indices. +** ==================================================================== +*/ + +/* +** Create a new 'ktable' to the pattern at the top of the stack. +*/ +static void newktable (lua_State *L, int n) { + lua_createtable(L, n, 0); /* create a fresh table */ + lua_setuservalue(L, -2); /* set it as 'ktable' for pattern */ +} + + +/* +** Add element 'idx' to 'ktable' of pattern at the top of the stack; +** Return index of new element. +** If new element is nil, does not add it to table (as it would be +** useless) and returns 0, as ktable[0] is always nil. +*/ +static int addtoktable (lua_State *L, int idx) { + if (lua_isnil(L, idx)) /* nil value? */ + return 0; + else { + int n; + lua_getuservalue(L, -1); /* get ktable from pattern */ + n = lua_rawlen(L, -1); + if (n >= USHRT_MAX) + luaL_error(L, "too many Lua values in pattern"); + lua_pushvalue(L, idx); /* element to be added */ + lua_rawseti(L, -2, ++n); + lua_pop(L, 1); /* remove 'ktable' */ + return n; + } +} + + +/* +** Return the number of elements in the ktable at 'idx'. +** In Lua 5.2/5.3, default "environment" for patterns is nil, not +** a table. Treat it as an empty table. In Lua 5.1, assumes that +** the environment has no numeric indices (len == 0) +*/ +static int ktablelen (lua_State *L, int idx) { + if (!lua_istable(L, idx)) return 0; + else return lua_rawlen(L, idx); +} + + +/* +** Concatentate the contents of table 'idx1' into table 'idx2'. +** (Assume that both indices are negative.) +** Return the original length of table 'idx2' (or 0, if no +** element was added, as there is no need to correct any index). +*/ +static int concattable (lua_State *L, int idx1, int idx2) { + int i; + int n1 = ktablelen(L, idx1); + int n2 = ktablelen(L, idx2); + if (n1 + n2 > USHRT_MAX) + luaL_error(L, "too many Lua values in pattern"); + if (n1 == 0) return 0; /* nothing to correct */ + for (i = 1; i <= n1; i++) { + lua_rawgeti(L, idx1, i); + lua_rawseti(L, idx2 - 1, n2 + i); /* correct 'idx2' */ + } + return n2; +} + + +/* +** When joining 'ktables', constants from one of the subpatterns must +** be renumbered; 'correctkeys' corrects their indices (adding 'n' +** to each of them) +*/ +static void correctkeys (TTree *tree, int n) { + if (n == 0) return; /* no correction? */ + tailcall: + switch (tree->tag) { + case TOpenCall: case TCall: case TRunTime: case TRule: { + if (tree->key > 0) + tree->key += n; + break; + } + case TCapture: { + if (tree->key > 0 && tree->cap != Carg && tree->cap != Cnum) + tree->key += n; + break; + } + default: break; + } + switch (numsiblings[tree->tag]) { + case 1: /* correctkeys(sib1(tree), n); */ + tree = sib1(tree); goto tailcall; + case 2: + correctkeys(sib1(tree), n); + tree = sib2(tree); goto tailcall; /* correctkeys(sib2(tree), n); */ + default: assert(numsiblings[tree->tag] == 0); break; + } +} + + +/* +** Join the ktables from p1 and p2 the ktable for the new pattern at the +** top of the stack, reusing them when possible. +*/ +static void joinktables (lua_State *L, int p1, TTree *t2, int p2) { + int n1, n2; + lua_getuservalue(L, p1); /* get ktables */ + lua_getuservalue(L, p2); + n1 = ktablelen(L, -2); + n2 = ktablelen(L, -1); + if (n1 == 0 && n2 == 0) /* are both tables empty? */ + lua_pop(L, 2); /* nothing to be done; pop tables */ + else if (n2 == 0 || lp_equal(L, -2, -1)) { /* 2nd table empty or equal? */ + lua_pop(L, 1); /* pop 2nd table */ + lua_setuservalue(L, -2); /* set 1st ktable into new pattern */ + } + else if (n1 == 0) { /* first table is empty? */ + lua_setuservalue(L, -3); /* set 2nd table into new pattern */ + lua_pop(L, 1); /* pop 1st table */ + } + else { + lua_createtable(L, n1 + n2, 0); /* create ktable for new pattern */ + /* stack: new p; ktable p1; ktable p2; new ktable */ + concattable(L, -3, -1); /* from p1 into new ktable */ + concattable(L, -2, -1); /* from p2 into new ktable */ + lua_setuservalue(L, -4); /* new ktable becomes 'p' environment */ + lua_pop(L, 2); /* pop other ktables */ + correctkeys(t2, n1); /* correction for indices from p2 */ + } +} + + +/* +** copy 'ktable' of element 'idx' to new tree (on top of stack) +*/ +static void copyktable (lua_State *L, int idx) { + lua_getuservalue(L, idx); + lua_setuservalue(L, -2); +} + + +/* +** merge 'ktable' from 'stree' at stack index 'idx' into 'ktable' +** from tree at the top of the stack, and correct corresponding +** tree. +*/ +static void mergektable (lua_State *L, int idx, TTree *stree) { + int n; + lua_getuservalue(L, -1); /* get ktables */ + lua_getuservalue(L, idx); + n = concattable(L, -1, -2); + lua_pop(L, 2); /* remove both ktables */ + correctkeys(stree, n); +} + + +/* +** Create a new 'ktable' to the pattern at the top of the stack, adding +** all elements from pattern 'p' (if not 0) plus element 'idx' to it. +** Return index of new element. +*/ +static int addtonewktable (lua_State *L, int p, int idx) { + newktable(L, 1); + if (p) + mergektable(L, p, NULL); + return addtoktable(L, idx); +} + +/* }====================================================== */ + + /* ** {====================================================== ** Tree generation @@ -155,7 +338,7 @@ static Pattern *getpattern (lua_State *L, int idx) { static int getsize (lua_State *L, int idx) { - return (lua_objlen(L, idx) - sizeof(Pattern)) / sizeof(TTree) + 1; + return (lua_rawlen(L, idx) - sizeof(Pattern)) / sizeof(TTree) + 1; } @@ -168,12 +351,16 @@ static TTree *gettree (lua_State *L, int idx, int *len) { /* -** create a pattern +** create a pattern. Set its uservalue (the 'ktable') equal to its +** metatable. (It could be any empty sequence; the metatable is at +** hand here, so we use it.) */ static TTree *newtree (lua_State *L, int len) { size_t size = (len - 1) * sizeof(TTree) + sizeof(Pattern); Pattern *p = (Pattern *)lua_newuserdata(L, size); luaL_getmetatable(L, PATTERN_T); + lua_pushvalue(L, -1); + lua_setuservalue(L, -3); lua_setmetatable(L, -2); p->code = NULL; p->codesize = 0; return p->tree; @@ -206,35 +393,6 @@ static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { } -/* -** Add element 'idx' to 'ktable' of pattern at the top of the stack; -** create new 'ktable' if necessary. Return index of new element. -** If new element is nil, does not add it to table (as it would be -** useless) and returns 0, as ktable[0] is always nil. -*/ -static int addtoktable (lua_State *L, int idx) { - if (idx == 0) /* no actual value to insert? */ - return 0; - else { - int n; - lua_getfenv(L, -1); /* get ktable from pattern */ - n = lua_objlen(L, -1); - if (n == 0) { /* is it empty/non-existent? */ - lua_pop(L, 1); /* remove it */ - lua_createtable(L, 1, 0); /* create a fresh table */ - lua_pushvalue(L, -1); /* make a copy */ - lua_setfenv(L, -3); /* set it as 'ktable' for pattern */ - } - if (!lua_isnil(L, idx)) { /* non-nil value? */ - lua_pushvalue(L, idx); /* element to be added */ - lua_rawseti(L, -2, ++n); - } - lua_pop(L, 1); /* remove 'ktable' */ - return n; - } -} - - /* ** Build a sequence of 'n' nodes, each with tag 'tag' and 'u.n' got ** from the array 's' (or 0 if array is NULL). (TSeq is binary, so it @@ -310,7 +468,7 @@ static TTree *getpatt (lua_State *L, int idx, int *len) { case LUA_TFUNCTION: { tree = newtree(L, 2); tree->tag = TRunTime; - tree->key = addtoktable(L, idx); + tree->key = addtonewktable(L, 0, idx); sib1(tree)->tag = TTrue; break; } @@ -325,123 +483,6 @@ static TTree *getpatt (lua_State *L, int idx, int *len) { } -/* -** Return the number of elements in the ktable of pattern at 'idx'. -** In Lua 5.2, default "environment" for patterns is nil, not -** a table. Treat it as an empty table. In Lua 5.1, assumes that -** the environment has no numeric indices (len == 0) -*/ -static int ktablelen (lua_State *L, int idx) { - if (!lua_istable(L, idx)) return 0; - else return lua_objlen(L, idx); -} - - -/* -** Concatentate the contents of table 'idx1' into table 'idx2'. -** (Assume that both indices are negative.) -** Return the original length of table 'idx2' -*/ -static int concattable (lua_State *L, int idx1, int idx2) { - int i; - int n1 = ktablelen(L, idx1); - int n2 = ktablelen(L, idx2); - if (n1 == 0) return 0; /* nothing to correct */ - for (i = 1; i <= n1; i++) { - lua_rawgeti(L, idx1, i); - lua_rawseti(L, idx2 - 1, n2 + i); /* correct 'idx2' */ - } - return n2; -} - - -/* -** Make a merge of ktables from p1 and p2 the ktable for the new -** pattern at the top of the stack. -*/ -static int joinktables (lua_State *L, int p1, int p2) { - int n1, n2; - lua_getfenv(L, p1); /* get ktables */ - lua_getfenv(L, p2); - n1 = ktablelen(L, -2); - n2 = ktablelen(L, -1); - if (n1 == 0 && n2 == 0) { /* are both tables empty? */ - lua_pop(L, 2); /* nothing to be done; pop tables */ - return 0; /* nothing to correct */ - } - if (n2 == 0 || lua_equal(L, -2, -1)) { /* second table is empty or equal? */ - lua_pop(L, 1); /* pop 2nd table */ - lua_setfenv(L, -2); /* set 1st ktable into new pattern */ - return 0; /* nothing to correct */ - } - if (n1 == 0) { /* first table is empty? */ - lua_setfenv(L, -3); /* set 2nd table into new pattern */ - lua_pop(L, 1); /* pop 1st table */ - return 0; /* nothing to correct */ - } - else { - lua_createtable(L, n1 + n2, 0); /* create ktable for new pattern */ - /* stack: new p; ktable p1; ktable p2; new ktable */ - concattable(L, -3, -1); /* from p1 into new ktable */ - concattable(L, -2, -1); /* from p2 into new ktable */ - lua_setfenv(L, -4); /* new ktable becomes p env */ - lua_pop(L, 2); /* pop other ktables */ - return n1; /* correction for indices from p2 */ - } -} - - -static void correctkeys (TTree *tree, int n) { - if (n == 0) return; /* no correction? */ - tailcall: - switch (tree->tag) { - case TOpenCall: case TCall: case TRunTime: case TRule: { - if (tree->key > 0) - tree->key += n; - break; - } - case TCapture: { - if (tree->key > 0 && tree->cap != Carg && tree->cap != Cnum) - tree->key += n; - break; - } - default: break; - } - switch (numsiblings[tree->tag]) { - case 1: /* correctkeys(sib1(tree), n); */ - tree = sib1(tree); goto tailcall; - case 2: - correctkeys(sib1(tree), n); - tree = sib2(tree); goto tailcall; /* correctkeys(sib2(tree), n); */ - default: assert(numsiblings[tree->tag] == 0); break; - } -} - - -/* -** copy 'ktable' of element 'idx' to new tree (on top of stack) -*/ -static void copyktable (lua_State *L, int idx) { - lua_getfenv(L, idx); - lua_setfenv(L, -2); -} - - -/* -** merge 'ktable' from rule at stack index 'idx' into 'ktable' -** from tree at the top of the stack, and correct corresponding -** tree. -*/ -static void mergektable (lua_State *L, int idx, TTree *rule) { - int n; - lua_getfenv(L, -1); /* get ktables */ - lua_getfenv(L, idx); - n = concattable(L, -1, -2); - lua_pop(L, 2); /* remove both ktables */ - correctkeys(rule, n); -} - - /* ** create a new tree, whith a new root and one sibling. ** Sibling must be on the Lua stack, at index 1. @@ -470,7 +511,7 @@ static TTree *newroot2sib (lua_State *L, int tag) { tree->u.ps = 1 + s1; memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); memcpy(sib2(tree), tree2, s2 * sizeof(TTree)); - correctkeys(sib2(tree), joinktables(L, 1, 2)); + joinktables(L, 1, sib2(tree), 2); return tree; } @@ -599,7 +640,7 @@ static int lp_sub (lua_State *L) { sib1(tree)->tag = TNot; /* ...not... */ memcpy(sib1(sib1(tree)), t2, s2 * sizeof(TTree)); /* ...t2 */ memcpy(sib2(tree), t1, s1 * sizeof(TTree)); /* ... and t1 */ - correctkeys(sib1(tree), joinktables(L, 1, 2)); + joinktables(L, 1, sib1(tree), 2); } return 1; } @@ -640,7 +681,7 @@ static int lp_behind (lua_State *L) { TTree *tree; TTree *tree1 = getpatt(L, 1, NULL); int n = fixedlen(tree1); - luaL_argcheck(L, n > 0, 1, "pattern may not have fixed length"); + luaL_argcheck(L, n >= 0, 1, "pattern may not have fixed length"); luaL_argcheck(L, !hascaptures(tree1), 1, "pattern have captures"); luaL_argcheck(L, n <= MAXBEHIND, 1, "pattern too long to look behind"); tree = newroot1sib(L, TBehind); @@ -655,7 +696,7 @@ static int lp_behind (lua_State *L) { static int lp_V (lua_State *L) { TTree *tree = newleaf(L, TOpenCall); luaL_argcheck(L, !lua_isnoneornil(L, 1), 1, "non-nil value expected"); - tree->key = addtoktable(L, 1); + tree->key = addtonewktable(L, 0, 1); return 1; } @@ -668,7 +709,7 @@ static int lp_V (lua_State *L) { static int capture_aux (lua_State *L, int cap, int labelidx) { TTree *tree = newroot1sib(L, TCapture); tree->cap = cap; - tree->key = addtoktable(L, labelidx); + tree->key = (labelidx == 0) ? 0 : addtonewktable(L, 1, labelidx); return 1; } @@ -676,10 +717,9 @@ static int capture_aux (lua_State *L, int cap, int labelidx) { /* ** Fill a tree with an empty capture, using an empty (TTrue) sibling. */ -static TTree *auxemptycap (lua_State *L, TTree *tree, int cap, int idx) { +static TTree *auxemptycap (TTree *tree, int cap) { tree->tag = TCapture; tree->cap = cap; - tree->key = addtoktable(L, idx); sib1(tree)->tag = TTrue; return tree; } @@ -688,8 +728,18 @@ static TTree *auxemptycap (lua_State *L, TTree *tree, int cap, int idx) { /* ** Create a tree for an empty capture */ -static TTree *newemptycap (lua_State *L, int cap, int idx) { - return auxemptycap(L, newtree(L, 2), cap, idx); +static TTree *newemptycap (lua_State *L, int cap) { + return auxemptycap(newtree(L, 2), cap); +} + + +/* +** Create a tree for an empty capture with an associated Lua value +*/ +static TTree *newemptycapkey (lua_State *L, int cap, int idx) { + TTree *tree = auxemptycap(newtree(L, 2), cap); + tree->key = addtonewktable(L, 0, idx); + return tree; } @@ -728,10 +778,8 @@ static int lp_tablecapture (lua_State *L) { static int lp_groupcapture (lua_State *L) { if (lua_isnoneornil(L, 2)) return capture_aux(L, Cgroup, 0); - else { - luaL_checkstring(L, 2); + else return capture_aux(L, Cgroup, 2); - } } @@ -747,14 +795,14 @@ static int lp_simplecapture (lua_State *L) { static int lp_poscapture (lua_State *L) { - newemptycap(L, Cposition, 0); + newemptycap(L, Cposition); return 1; } static int lp_argcapture (lua_State *L) { int n = (int)luaL_checkinteger(L, 1); - TTree *tree = newemptycap(L, Carg, 0); + TTree *tree = newemptycap(L, Carg); tree->key = n; luaL_argcheck(L, 0 < n && n <= SHRT_MAX, 1, "invalid argument index"); return 1; @@ -762,8 +810,8 @@ static int lp_argcapture (lua_State *L) { static int lp_backref (lua_State *L) { - luaL_checkstring(L, 1); - newemptycap(L, Cbackref, 1); + luaL_checkany(L, 1); + newemptycapkey(L, Cbackref, 1); return 1; } @@ -777,9 +825,10 @@ static int lp_constcapture (lua_State *L) { if (n == 0) /* no values? */ newleaf(L, TTrue); /* no capture */ else if (n == 1) - newemptycap(L, Cconst, 1); /* single constant capture */ + newemptycapkey(L, Cconst, 1); /* single constant capture */ else { /* create a group capture with all values */ TTree *tree = newtree(L, 1 + 3 * (n - 1) + 2); + newktable(L, n); /* create a 'ktable' for new tree */ tree->tag = TCapture; tree->cap = Cgroup; tree->key = 0; @@ -787,10 +836,12 @@ static int lp_constcapture (lua_State *L) { for (i = 1; i <= n - 1; i++) { tree->tag = TSeq; tree->u.ps = 3; /* skip TCapture and its sibling */ - auxemptycap(L, sib1(tree), Cconst, i); + auxemptycap(sib1(tree), Cconst); + sib1(tree)->key = addtoktable(L, i); tree = sib2(tree); } - auxemptycap(L, tree, Cconst, i); + auxemptycap(tree, Cconst); + tree->key = addtoktable(L, i); } return 1; } @@ -800,7 +851,7 @@ static int lp_matchtime (lua_State *L) { TTree *tree; luaL_checktype(L, 2, LUA_TFUNCTION); tree = newroot1sib(L, TRunTime); - tree->key = addtoktable(L, 2); + tree->key = addtonewktable(L, 1, 2); return 1; } @@ -857,7 +908,7 @@ static int collectrules (lua_State *L, int arg, int *totalsize) { lua_pushnil(L); /* prepare to traverse grammar table */ while (lua_next(L, arg) != 0) { if (lua_tonumber(L, -2) == 1 || - lua_equal(L, -2, postab + 1)) { /* initial rule? */ + lp_equal(L, -2, postab + 1)) { /* initial rule? */ lua_pop(L, 1); /* remove value (keep key for lua_next) */ continue; } @@ -934,36 +985,40 @@ static int verifyerror (lua_State *L, int *passed, int npassed) { /* ** Check whether a rule can be left recursive; raise an error in that -** case; otherwise return 1 iff pattern is nullable. Assume ktable at -** the top of the stack. +** case; otherwise return 1 iff pattern is nullable. +** The return value is used to check sequences, where the second pattern +** is only relevant if the first is nullable. +** Parameter 'nb' works as an accumulator, to allow tail calls in +** choices. ('nb' true makes function returns true.) +** Assume ktable at the top of the stack. */ static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed, - int nullable) { + int nb) { tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: case TFalse: - return nullable; /* cannot pass from here */ + return nb; /* cannot pass from here */ case TTrue: case TBehind: /* look-behind cannot have calls */ return 1; case TNot: case TAnd: case TRep: /* return verifyrule(L, sib1(tree), passed, npassed, 1); */ - tree = sib1(tree); nullable = 1; goto tailcall; + tree = sib1(tree); nb = 1; goto tailcall; case TCapture: case TRunTime: - /* return verifyrule(L, sib1(tree), passed, npassed); */ + /* return verifyrule(L, sib1(tree), passed, npassed, nb); */ tree = sib1(tree); goto tailcall; case TCall: - /* return verifyrule(L, sib2(tree), passed, npassed); */ + /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ tree = sib2(tree); goto tailcall; - case TSeq: /* only check 2nd child if first is nullable */ + case TSeq: /* only check 2nd child if first is nb */ if (!verifyrule(L, sib1(tree), passed, npassed, 0)) - return nullable; - /* else return verifyrule(L, sib2(tree), passed, npassed); */ + return nb; + /* else return verifyrule(L, sib2(tree), passed, npassed, nb); */ tree = sib2(tree); goto tailcall; case TChoice: /* must check both children */ - nullable = verifyrule(L, sib1(tree), passed, npassed, nullable); - /* return verifyrule(L, sib2(tree), passed, npassed, nullable); */ + nb = verifyrule(L, sib1(tree), passed, npassed, nb); + /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ tree = sib2(tree); goto tailcall; case TRule: if (npassed >= MAXRULES) @@ -1006,7 +1061,7 @@ static void verifygrammar (lua_State *L, TTree *grammar) { */ static void initialrulename (lua_State *L, TTree *grammar, int frule) { if (sib1(grammar)->key == 0) { /* initial rule is not referenced? */ - int n = lua_objlen(L, -1) + 1; /* index for name */ + int n = lua_rawlen(L, -1) + 1; /* index for name */ lua_pushvalue(L, frule); /* rule's name */ lua_rawseti(L, -2, n); /* ktable was on the top of the stack */ sib1(grammar)->key = n; @@ -1022,9 +1077,9 @@ static TTree *newgrammar (lua_State *L, int arg) { luaL_argcheck(L, n <= MAXRULES, arg, "grammar has too many rules"); g->tag = TGrammar; g->u.n = n; lua_newtable(L); /* create 'ktable' */ - lua_setfenv(L, -2); + lua_setuservalue(L, -2); buildgrammar(L, g, frule, n); - lua_getfenv(L, -1); /* get 'ktable' for new tree */ + lua_getuservalue(L, -1); /* get 'ktable' for new tree */ finalfix(L, frule - 1, g, sib1(g)); initialrulename(L, g, frule); verifygrammar(L, g); @@ -1038,7 +1093,7 @@ static TTree *newgrammar (lua_State *L, int arg) { static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { - lua_getfenv(L, idx); /* push 'ktable' (may be used by 'finalfix') */ + lua_getuservalue(L, idx); /* push 'ktable' (may be used by 'finalfix') */ finalfix(L, 0, NULL, p->tree); lua_pop(L, 1); /* remove 'ktable' */ return compile(L, p); @@ -1049,7 +1104,7 @@ static int lp_printtree (lua_State *L) { TTree *tree = getpatt(L, 1, NULL); int c = lua_toboolean(L, 2); if (c) { - lua_getfenv(L, 1); /* push 'ktable' (may be used by 'finalfix') */ + lua_getuservalue(L, 1); /* push 'ktable' (may be used by 'finalfix') */ finalfix(L, 0, NULL, tree); lua_pop(L, 1); /* remove 'ktable' */ } @@ -1102,7 +1157,7 @@ static int lp_match (lua_State *L) { int ptop = lua_gettop(L); lua_pushnil(L); /* initialize subscache */ lua_pushlightuserdata(L, capture); /* initialize caplistidx */ - lua_getfenv(L, 1); /* initialize penvidx */ + lua_getuservalue(L, 1); /* initialize penvidx */ r = match(L, s, s + i, s + l, code, capture, ptop); if (r == NULL) { lua_pushnil(L); @@ -1119,8 +1174,12 @@ static int lp_match (lua_State *L) { ** ======================================================= */ +/* maximum limit for stack size */ +#define MAXLIM (INT_MAX / 100) + static int lp_setmax (lua_State *L) { - luaL_optinteger(L, 1, -1); + lua_Integer lim = luaL_checkinteger(L, 1); + luaL_argcheck(L, 0 < lim && lim <= MAXLIM, 1, "out of range"); lua_settop(L, 1); lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); return 0; @@ -1144,8 +1203,7 @@ static int lp_type (lua_State *L) { int lp_gc (lua_State *L) { Pattern *p = getpattern(L, 1); - if (p->codesize > 0) - realloccode(L, p, 0); + realloccode(L, p, 0); /* delete code block */ return 0; } @@ -1228,8 +1286,8 @@ int luaopen_lpeg (lua_State *L) { luaL_newmetatable(L, PATTERN_T); lua_pushnumber(L, MAXBACK); /* initialize maximum backtracking */ lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); - luaL_register(L, NULL, metareg); - luaL_register(L, "lpeg", pattreg); + luaL_setfuncs(L, metareg, 0); + luaL_newlib(L, pattreg); lua_pushvalue(L, -1); lua_setfield(L, -3, "__index"); return 1; diff --git a/3rd/lpeg/lptypes.h b/3rd/lpeg/lptypes.h index b85c0c97..5eb7987b 100644 --- a/3rd/lpeg/lptypes.h +++ b/3rd/lpeg/lptypes.h @@ -1,7 +1,7 @@ /* -** $Id: lptypes.h,v 1.10 2014/12/12 17:11:35 roberto Exp $ +** $Id: lptypes.h,v 1.14 2015/09/28 17:17:41 roberto Exp $ ** LPeg - PEG pattern matching for Lua -** Copyright 2007-2014, Lua.org & PUC-Rio (see 'lpeg.html' for license) +** Copyright 2007-2015, Lua.org & PUC-Rio (see 'lpeg.html' for license) ** written by Roberto Ierusalimschy */ @@ -19,7 +19,7 @@ #include "lua.h" -#define VERSION "0.12.1" +#define VERSION "1.0.0" #define PATTERN_T "lpeg-pattern" @@ -27,31 +27,31 @@ /* -** compatibility with Lua 5.2 +** compatibility with Lua 5.1 */ -#if (LUA_VERSION_NUM >= 502) +#if (LUA_VERSION_NUM == 501) -#undef lua_equal -#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#define lp_equal lua_equal -#undef lua_getfenv -#define lua_getfenv lua_getuservalue -#undef lua_setfenv -#define lua_setfenv lua_setuservalue +#define lua_getuservalue lua_getfenv +#define lua_setuservalue lua_setfenv -#undef lua_objlen -#define lua_objlen lua_rawlen +#define lua_rawlen lua_objlen -#undef luaL_register -#define luaL_register(L,n,f) \ - { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } +#define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) +#define luaL_newlib(L,f) luaL_register(L,"lpeg",f) #endif +#if !defined(lp_equal) +#define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#endif + + /* default maximum size for call/backtrack stack */ #if !defined(MAXBACK) -#define MAXBACK 100 +#define MAXBACK 400 #endif diff --git a/3rd/lpeg/lpvm.c b/3rd/lpeg/lpvm.c index cd893ed8..eaf2ebfd 100644 --- a/3rd/lpeg/lpvm.c +++ b/3rd/lpeg/lpvm.c @@ -1,5 +1,5 @@ /* -** $Id: lpvm.c,v 1.5 2013/04/12 16:29:49 roberto Exp $ +** $Id: lpvm.c,v 1.6 2015/09/28 17:01:25 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -18,7 +18,7 @@ /* initial size for call/backtrack stack */ #if !defined(INITBACK) -#define INITBACK 100 +#define INITBACK MAXBACK #endif @@ -70,7 +70,7 @@ static Stack *doublestack (lua_State *L, Stack **stacklimit, int ptop) { max = lua_tointeger(L, -1); /* maximum allowed size */ lua_pop(L, 1); if (n >= max) /* already at maximum size? */ - luaL_error(L, "too many pending calls/choices"); + luaL_error(L, "backtrack stack overflow (current limit is %d)", max); newn = 2 * n; /* new size */ if (newn > max) newn = max; newstack = (Stack *)lua_newuserdata(L, newn * sizeof(Stack)); diff --git a/3rd/lpeg/re.html b/3rd/lpeg/re.html index 4717ec2b..d0d9744f 100644 --- a/3rd/lpeg/re.html +++ b/3rd/lpeg/re.html @@ -10,7 +10,7 @@ - +
@@ -296,7 +296,7 @@ it would be useful if each table had a tag field telling what non terminal that table represents. We can add such a tag using -named group captures: +named group captures:

 x = re.compile[[
@@ -450,7 +450,7 @@ print(re.match(p, p))   -- a self description must match itself
 

License

-Copyright © 2008-2010 Lua.org, PUC-Rio. +Copyright © 2008-2015 Lua.org, PUC-Rio.

Permission is hereby granted, free of charge, @@ -488,7 +488,7 @@ THE SOFTWARE.

-$Id: re.html,v 1.21 2013/03/28 20:43:30 roberto Exp $ +$Id: re.html,v 1.23 2015/09/28 17:17:41 roberto Exp $

diff --git a/3rd/lpeg/test.lua b/3rd/lpeg/test.lua index fec5a85b..017a3abf 100755 --- a/3rd/lpeg/test.lua +++ b/3rd/lpeg/test.lua @@ -1,6 +1,6 @@ -#!/usr/bin/env lua5.1 +#!/usr/bin/env lua --- $Id: test.lua,v 1.105 2014/12/12 17:00:39 roberto Exp $ +-- $Id: test.lua,v 1.109 2015/09/28 17:01:25 roberto Exp $ -- require"strict" -- just to be pedantic @@ -16,9 +16,6 @@ local unpack = rawget(table, "unpack") or unpack local loadstring = rawget(_G, "loadstring") or load --- most tests here do not need much stack space -m.setmaxstack(5) - local any = m.P(1) local space = m.S" \t\n"^0 @@ -291,6 +288,13 @@ assert(m.match(m.P"ab"^-1 - "c", "abcd") == 3) p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) + + +-- bug in 0.12.2 +-- p = { ('ab' ('c' 'ef'?)*)? } +p = m.C(('ab' * ('c' * m.P'ef'^-1)^0)^-1) +s = "abcefccefc" +assert(s == p:match(s)) pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" @@ -352,6 +356,11 @@ checkeq(t, {hi = 10, ho = 20}) t = p:match'abc' checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) +-- non-string group names +p = m.Ct(m.Cg(1, print) * m.Cg(1, 23.5) * m.Cg(1, io)) +t = p:match('abcdefghij') +assert(t[print] == 'a' and t[23.5] == 'b' and t[io] == 'c') + -- test for error messages local function checkerr (msg, f, ...) @@ -388,6 +397,25 @@ p = m.P { (m.P {m.P'abc'} + 'ayz') * m.V'y'; y = m.P'x' } assert(p:match('abcx') == 5 and p:match('ayzx') == 5 and not p:match'abc') +do + -- large dynamic Cc + local lim = 2^16 - 1 + local c = 0 + local function seq (n) + if n == 1 then c = c + 1; return m.Cc(c) + else + local m = math.floor(n / 2) + return seq(m) * seq(n - m) + end + end + p = m.Ct(seq(lim)) + t = p:match('') + assert(t[lim] == lim) + checkerr("too many", function () p = p / print end) + checkerr("too many", seq, lim + 1) +end + + -- tests for non-pattern as arguments to pattern functions p = { ('a' * m.V(1))^-1 } * m.P'b' * { 'a' * m.V(2); m.V(1)^-1 } @@ -575,9 +603,9 @@ assert(not p:match(string.rep("011", 10001))) -- this grammar does need backtracking info. local lim = 10000 p = m.P{ '0' * m.V(1) + '0' } -checkerr("too many pending", m.match, p, string.rep("0", lim)) +checkerr("stack overflow", m.match, p, string.rep("0", lim)) m.setmaxstack(2*lim) -checkerr("too many pending", m.match, p, string.rep("0", lim)) +checkerr("stack overflow", m.match, p, string.rep("0", lim)) m.setmaxstack(2*lim + 4) assert(m.match(p, string.rep("0", lim)) == lim + 1) @@ -586,7 +614,7 @@ p = m.P{ ('a' * m.V(1))^0 * 'b' + 'c' } m.setmaxstack(200) assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) -m.setmaxstack(5) -- restore original limit +m.setmaxstack(100) -- restore low limit -- tests for optional start position assert(m.match("a", "abc", 1)) @@ -718,6 +746,10 @@ t = {m.match(m.Cc(nil,nil,4) * m.Cc(nil,3) * m.Cc(nil, nil) / g / g, "")} t1 = {1,1,nil,nil,4,nil,3,nil,nil} for i=1,10 do assert(t[i] == t1[i]) end +-- bug in 0.12.2: ktable with only nil could be eliminated when joining +-- with a pattern without ktable +assert((m.P"aaa" * m.Cc(nil)):match"aaa" == nil) + t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) @@ -925,6 +957,13 @@ p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) t = p:match("ab") checkeq(t, {"a", "b"}) +p = m.P(true) +for i = 1, 10 do p = p * m.Cg(1, i) end +for i = 1, 10 do + local p = p * m.Cb(i) + assert(p:match('abcdefghij') == string.sub('abcdefghij', i, i)) +end + t = {} function foo (p) t[#t + 1] = p; return p .. "x" end From 3a06dcf534e89a81e5bcf532e3b3cbade985bab9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Oct 2015 10:05:36 +0800 Subject: [PATCH 479/729] update jemalloc to 4.0.3 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 3541a904..e9192eac 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 3541a904d6fb949f3f0aea05418ccce7cbd4b705 +Subproject commit e9192eacf8935e29fc62fddc2701f7942b1cc02c From 79e73d907e566b12fb69d2ad6ead02914bde7916 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Oct 2015 10:09:22 +0800 Subject: [PATCH 480/729] add target update3rd --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 98f7d1c9..e0f0b14d 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ JEMALLOC_INC := 3rd/jemalloc/include/jemalloc all : jemalloc -.PHONY : jemalloc +.PHONY : jemalloc update3rd MALLOC_STATICLIB := $(JEMALLOC_STATICLIB) @@ -39,6 +39,9 @@ $(JEMALLOC_STATICLIB) : 3rd/jemalloc/Makefile jemalloc : $(MALLOC_STATICLIB) +update3rd : + rm -rf 3rd/jemalloc && git submodule update --init + # skynet CSERVICE = snlua logger gate harbor From 8924b86ad7231e52b44d6dc8c06ab3d58d25f77e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 8 Oct 2015 15:15:35 +0800 Subject: [PATCH 481/729] check empty group, See pr #351 --- service/multicastd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/multicastd.lua b/service/multicastd.lua index 7bafab78..2ff1a7f0 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -67,7 +67,7 @@ end -- for remote node, call remote_publish. (call mc.unpack and skynet.tostring to convert message pointer to string) local function publish(c , source, pack, size) local group = channel[c] - if group == nil then + if group == nil or next(group) == nil then -- dead channel, delete the pack. mc.bind returns the pointer in pack local pack = mc.bind(pack, 1) mc.close(pack) From 8c1011c503f6b57b780dd08e0713efa33ee8f7e1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 8 Oct 2015 15:30:15 +0800 Subject: [PATCH 482/729] Add sproto:exist_type and sproto:exist_proto --- lualib/sproto.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 911dc877..d3ae40ad 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -58,6 +58,15 @@ local function querytype(self, typename) return v end +function sproto:exist_type(typename) + local v = self.__tcache[typename] + if not v then + return core.querytype(self.__cobj, typename) ~= nil + else + return true + end +end + function sproto:encode(typename, tbl) local st = querytype(self, typename) return core.encode(st, tbl) @@ -99,6 +108,15 @@ local function queryproto(self, pname) return v end +function sproto:exist_proto(pname) + local v = self.__pcache[pname] + if not v then + return core.protocol(self.__cobj, pname) ~= nil + else + return true + end +end + function sproto:request_encode(protoname, tbl) local p = queryproto(self, protoname) local request = p.request From 8dd9923cf6313a2858f0a5633ff0aa43964d8695 Mon Sep 17 00:00:00 2001 From: xjdrew Date: Thu, 8 Oct 2015 16:55:29 +0800 Subject: [PATCH 483/729] gate: application could control when to start receiving data --- service-src/service_gate.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 29eab2ea..600c4ec7 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -132,7 +132,12 @@ _ctrl(struct gate * g, const void * msg, int sz) { return; } if (memcmp(command,"start",i) == 0) { - skynet_socket_start(ctx, g->listen_id); + _parm(tmp, sz, i); + int uid = strtol(command , NULL, 10); + int id = hashid_lookup(&g->hash, uid); + if (id>=0) { + skynet_socket_start(ctx, uid); + } return; } if (memcmp(command, "close", i) == 0) { @@ -225,10 +230,7 @@ dispatch_socket_message(struct gate *g, const struct skynet_socket_message * mes break; } int id = hashid_lookup(&g->hash, message->id); - if (id>=0) { - struct connection *c = &g->conn[id]; - _report(g, "%d open %d %s:0",message->id,message->id,c->remote_name); - } else { + if (id<0) { skynet_error(ctx, "Close unknown connection %d", message->id); skynet_socket_close(ctx, message->id); } @@ -259,7 +261,8 @@ dispatch_socket_message(struct gate *g, const struct skynet_socket_message * mes c->id = message->ud; memcpy(c->remote_name, message+1, sz); c->remote_name[sz] = '\0'; - skynet_socket_start(ctx, message->ud); + _report(g, "%d open %d %s:0",c->id, c->id, c->remote_name); + skynet_error(ctx, "socket open: %x", c->id); } break; case SKYNET_SOCKET_TYPE_WARNING: @@ -327,6 +330,7 @@ start_listen(struct gate *g, char * listen_addr) { if (g->listen_id < 0) { return 1; } + skynet_socket_start(ctx, g->listen_id); return 0; } From 515326dadcfb1b9c922bbdff4c99110e19f550a6 Mon Sep 17 00:00:00 2001 From: xjdrew Date: Sat, 10 Oct 2015 18:39:42 +0800 Subject: [PATCH 484/729] bugfix: remove Wformat warning --- service-src/service_gate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 600c4ec7..71ab1a64 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -344,7 +344,7 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) { char binding[sz]; int client_tag = 0; char header; - int n = sscanf(parm, "%c %s %s %d %d %d", &header, watchdog, binding, &client_tag, &max); + int n = sscanf(parm, "%c %s %s %d %d", &header, watchdog, binding, &client_tag, &max); if (n<4) { skynet_error(ctx, "Invalid gate parm %s",parm); return 1; From 878110f9f75fe0ddb179077bcf409d9fbc34de44 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 12 Oct 2015 12:29:19 +0800 Subject: [PATCH 485/729] add codecache.mode() --- 3rd/lua/lauxlib.c | 50 +++++++++++++++++++++++++++++++++++++ service-src/service_snlua.c | 1 + 2 files changed, 51 insertions(+) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index e4d79955..49657027 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1040,13 +1040,62 @@ save(const char *key, const void * proto) { return result; } +#define CACHE_OFF 0 +#define CACHE_EXIST 1 +#define CACHE_ON 2 + +static int cache_key = 0; + +static int cache_level(lua_State *L) { + int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); + int r = lua_tointeger(L, -1); + lua_pop(L,1); + if (t == LUA_TNUMBER) { + return r; + } + return CACHE_ON; +} + +static int cache_mode(lua_State *L) { + static const char * lst[] = { + "OFF", + "EXIST", + "ON", + NULL, + }; + if (lua_isnoneornil(L,1)) { + int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); + int r = lua_tointeger(L, -1); + if (t == LUA_TNUMBER) { + if (r < 0 || r >= CACHE_ON) { + r = CACHE_ON; + } + } else { + r = CACHE_ON; + } + lua_pushstring(L, lst[r]); + return 1; + } + int t = luaL_checkoption(L, 1, "OFF" , lst); + lua_pushinteger(L, t); + lua_rawsetp(L, LUA_REGISTRYINDEX, &cache_key); + return 0; +} + LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { + int level = cache_level(L); + if (level == CACHE_OFF) { + return luaL_loadfilex_(L, filename, mode); + } const void * proto = load(filename); if (proto) { lua_clonefunction(L, proto); return LUA_OK; } + if (level == CACHE_EXIST) { + return luaL_loadfilex_(L, filename, mode); + } lua_State * eL = luaL_newstate(); if (eL == NULL) { lua_pushliteral(L, "New state failed"); @@ -1083,6 +1132,7 @@ cache_clear(lua_State *L) { LUAMOD_API int luaopen_cache(lua_State *L) { luaL_Reg l[] = { { "clear", cache_clear }, + { "mode", cache_mode }, { NULL, NULL }, }; luaL_newlib(L,l); diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 89e607f6..20440780 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -30,6 +30,7 @@ static int codecache(lua_State *L) { luaL_Reg l[] = { { "clear", cleardummy }, + { "mode", cleardummy }, { NULL, NULL }, }; luaL_newlib(L,l); From d7e4e43a1b1cd73e6d40369265766084d9deaee3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 12 Oct 2015 13:07:09 +0800 Subject: [PATCH 486/729] use spinlock macro --- 3rd/lua/lauxlib.c | 21 ++++++++++----------- skynet-src/spinlock.h | 1 + 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index 49657027..dec81e8a 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -972,29 +972,28 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { // use clonefunction -#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} -#define UNLOCK(q) __sync_lock_release(&(q)->lock); +#include "spinlock.h" struct codecache { - int lock; + struct spinlock lock; lua_State *L; }; -static struct codecache CC = { 0 , NULL }; +static struct codecache CC; static void clearcache() { if (CC.L == NULL) return; - LOCK(&CC) + SPIN_LOCK(&CC) lua_close(CC.L); CC.L = luaL_newstate(); - UNLOCK(&CC) + SPIN_UNLOCK(&CC) } static void init() { - CC.lock = 0; + SPIN_INIT(&CC); CC.L = luaL_newstate(); } @@ -1002,13 +1001,13 @@ static const void * load(const char *key) { if (CC.L == NULL) return NULL; - LOCK(&CC) + SPIN_LOCK(&CC) lua_State *L = CC.L; lua_pushstring(L, key); lua_rawget(L, LUA_REGISTRYINDEX); const void * result = lua_touserdata(L, -1); lua_pop(L, 1); - UNLOCK(&CC) + SPIN_UNLOCK(&CC) return result; } @@ -1018,7 +1017,7 @@ save(const char *key, const void * proto) { lua_State *L; const void * result = NULL; - LOCK(&CC) + SPIN_LOCK(&CC) if (CC.L == NULL) { init(); L = CC.L; @@ -1036,7 +1035,7 @@ save(const char *key, const void * proto) { lua_pop(L,2); } } - UNLOCK(&CC) + SPIN_UNLOCK(&CC) return result; } diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h index d9744c3f..514546f4 100644 --- a/skynet-src/spinlock.h +++ b/skynet-src/spinlock.h @@ -34,6 +34,7 @@ spinlock_unlock(struct spinlock *lock) { static inline void spinlock_destroy(struct spinlock *lock) { + (void) lock; } #else From d2f2e299d527a0ab19a9c69a58ebfcd73db63e47 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 12 Oct 2015 14:40:42 +0800 Subject: [PATCH 487/729] call socket.abandon while auth error, see issue #355 --- lualib/snax/loginserver.lua | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 01b2d49b..dfaa3ebd 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -49,7 +49,6 @@ end local function launch_slave(auth_handler) local function auth(fd, addr) - fd = assert(tonumber(fd)) skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) socket.start(fd) @@ -84,11 +83,11 @@ local function launch_slave(auth_handler) local ok, server, uid = pcall(auth_handler,token) - socket.abandon(fd) return ok, server, uid, secret end - local function ret_pack(ok, err, ...) + local function ret_pack(fd, ok, err, ...) + socket.abandon(fd) if ok then skynet.ret(skynet.pack(err, ...)) else @@ -100,8 +99,12 @@ local function launch_slave(auth_handler) end end - skynet.dispatch("lua", function(_,_,...) - ret_pack(pcall(auth, ...)) + skynet.dispatch("lua", function(_,_,fd,...) + if type(fd) ~= "number" then + skynet.ret(skynet.pack(false, "invalid fd type")) + else + ret_pack(fd,pcall(auth, fd, ...)) + end end) end From 9aaa35ef142da0d6e9ab0c6a2326ee8442716d6b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Oct 2015 15:24:51 +0800 Subject: [PATCH 488/729] avoid lua stack overflow --- lualib-src/lua-bson.c | 2 ++ skynet-src/skynet_timer.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 0fc26985..bcd44162 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -408,6 +408,7 @@ bson_numstr( char *str, unsigned int i ) { static void pack_dict(lua_State *L, struct bson *b, bool isarray) { + luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table int length = reserve_length(b); lua_pushnil(L); while(lua_next(L,-2) != 0) { @@ -495,6 +496,7 @@ make_object(lua_State *L, int type, const void * ptr, size_t len) { static void unpack_dict(lua_State *L, struct bson_reader *br, bool array) { + luaL_checkstack(L, 16, NULL); // reserve enough stack space to unpack table int sz = read_int32(L, br); const void * bytes = read_bytes(L, br, sz-5); struct bson_reader t = { bytes, sz-5 }; diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index c9a113ae..1f366b85 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -208,7 +208,7 @@ timer_create_timer() { int skynet_timeout(uint32_t handle, int time, int session) { - if (time == 0) { + if (time <= 0) { struct skynet_message message; message.source = 0; message.session = session; From 7486e5323287765b14e04b22412b6aa93e5f047a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 14 Oct 2015 15:44:09 +0800 Subject: [PATCH 489/729] avoid circular reference while encodeing bson --- lualib-src/lua-bson.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index bcd44162..7319a0dd 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -12,6 +12,8 @@ #define DEFAULT_CAP 64 #define MAX_NUMBER 1024 +// avoid circular reference while encodeing +#define MAX_DEPTH 128 #define BSON_REAL 1 #define BSON_STRING 2 @@ -236,7 +238,7 @@ write_double(struct bson *b, lua_Number d) { } } -static void pack_dict(lua_State *L, struct bson *b, bool array); +static void pack_dict(lua_State *L, struct bson *b, bool array, int depth); static inline void append_key(struct bson *bs, int type, const char *key, size_t sz) { @@ -268,7 +270,7 @@ append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { } static void -append_table(struct bson *bs, lua_State *L, const char *key, size_t sz) { +append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { size_t len = lua_rawlen(L, -1); bool isarray = false; if (len > 0) { @@ -284,7 +286,7 @@ append_table(struct bson *bs, lua_State *L, const char *key, size_t sz) { } else { append_key(bs, BSON_DOCUMENT, key, sz); } - pack_dict(L, bs, isarray); + pack_dict(L, bs, isarray, depth); } static void @@ -297,7 +299,7 @@ write_binary(struct bson *b, const void * buffer, size_t sz) { } static void -append_one(struct bson *bs, lua_State *L, const char *key, size_t sz) { +append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { int vt = lua_type(L,-1); switch(vt) { case LUA_TNUMBER: @@ -385,7 +387,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz) { break; } case LUA_TTABLE: - append_table(bs, L, key, sz); + append_table(bs, L, key, sz, depth+1); break; case LUA_TBOOLEAN: append_key(bs, BSON_BOOLEAN, key, sz); @@ -407,7 +409,10 @@ bson_numstr( char *str, unsigned int i ) { } static void -pack_dict(lua_State *L, struct bson *b, bool isarray) { +pack_dict(lua_State *L, struct bson *b, bool isarray, int depth) { + if (depth > MAX_DEPTH) { + luaL_error(L, "Too depth while encoding bson"); + } luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table int length = reserve_length(b); lua_pushnil(L); @@ -424,7 +429,7 @@ pack_dict(lua_State *L, struct bson *b, bool isarray) { sz = bson_numstr(numberkey, (unsigned int)lua_tointeger(L,-2)-1); key = numberkey; - append_one(b, L, key, sz); + append_one(b, L, key, sz, depth); lua_pop(L,1); } else { switch(kt) { @@ -433,12 +438,12 @@ pack_dict(lua_State *L, struct bson *b, bool isarray) { lua_pushvalue(L,-2); lua_insert(L,-2); key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz); + append_one(b, L, key, sz, depth); lua_pop(L,2); break; case LUA_TSTRING: key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz); + append_one(b, L, key, sz, depth); lua_pop(L,1); break; default: @@ -452,7 +457,7 @@ pack_dict(lua_State *L, struct bson *b, bool isarray) { } static void -pack_ordered_dict(lua_State *L, struct bson *b, int n) { +pack_ordered_dict(lua_State *L, struct bson *b, int n, int depth) { int length = reserve_length(b); int i; for (i=0;i Date: Wed, 14 Oct 2015 20:12:30 +0800 Subject: [PATCH 490/729] bugfix: socketchannel order mode may blocked. (used by redis driver) --- lualib/dns.lua | 2 +- lualib/skynet.lua | 4 +-- lualib/socket.lua | 6 ++-- lualib/socketchannel.lua | 74 ++++++++++++++++++++++------------------ 4 files changed, 46 insertions(+), 40 deletions(-) diff --git a/lualib/dns.lua b/lualib/dns.lua index 35e38084..e349699f 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -265,7 +265,7 @@ local function suspend(tid, name, qtype) co = coroutine.running(), } request_pool[tid] = req - skynet.wait() + skynet.wait(req.co) local answers = request_pool[tid].answers request_pool[tid] = nil assert(answers, "no ip") diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 554d8ff8..c20efaa2 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -275,10 +275,10 @@ function skynet.yield() return skynet.sleep(0) end -function skynet.wait() +function skynet.wait(co) local session = c.genid() local ret, msg = coroutine_yield("SLEEP", session) - local co = coroutine.running() + co = co or coroutine.running() sleep_session[co] = nil session_id_coroutine[session] = nil end diff --git a/lualib/socket.lua b/lualib/socket.lua index 7f1d603b..58fe2678 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -30,7 +30,7 @@ end local function suspend(s) assert(not s.co) s.co = coroutine.running() - skynet.wait() + skynet.wait(s.co) -- wakeup closing corouting every time suspend, -- because socket.close() will wait last socket buffer operation before clear the buffer. if s.closing then @@ -232,7 +232,7 @@ function socket.close(id) -- wait reading coroutine read the buffer. assert(not s.closing) s.closing = coroutine.running() - skynet.wait() + skynet.wait(s.closing) else suspend(s) end @@ -361,7 +361,7 @@ function socket.lock(id) else local co = coroutine.running() table.insert(lock_set, co) - skynet.wait() + skynet.wait(co) end end diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 9ff5d451..547b9f87 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -115,8 +115,17 @@ local function dispatch_by_session(self) end end +local wait_response + local function pop_response(self) - return table.remove(self.__request, 1), table.remove(self.__thread, 1) + while true do + local func,co = table.remove(self.__request, 1), table.remove(self.__thread, 1) + if func then + return func, co + end + wait_response = coroutine.running() + skynet.wait(wait_response) + end end local function push_response(self, response, co) @@ -127,46 +136,43 @@ local function push_response(self, response, co) -- response is a function, push it to __request table.insert(self.__request, response) table.insert(self.__thread, co) + if wait_response then + skynet.wakeup(wait_response) + wait_response = nil + end end end local function dispatch_by_order(self) while self.__sock do local func, co = pop_response(self) - if func == nil then - if not socket.block(self.__sock[1]) then - close_channel_socket(self) - wakeup_all(self) + local ok, result_ok, result_data, padding = pcall(func, self.__sock) + if ok then + if padding and result_ok then + -- if padding is true, wait for next result_data + -- self.__result_data[co] is a table + local result = self.__result_data[co] or {} + self.__result_data[co] = result + table.insert(result, result_data) + else + self.__result[co] = result_ok + if result_ok and self.__result_data[co] then + table.insert(self.__result_data[co], result_data) + else + self.__result_data[co] = result_data + end + skynet.wakeup(co) end else - local ok, result_ok, result_data, padding = pcall(func, self.__sock) - if ok then - if padding and result_ok then - -- if padding is true, wait for next result_data - -- self.__result_data[co] is a table - local result = self.__result_data[co] or {} - self.__result_data[co] = result - table.insert(result, result_data) - else - self.__result[co] = result_ok - if result_ok and self.__result_data[co] then - table.insert(self.__result_data[co], result_data) - else - self.__result_data[co] = result_data - end - skynet.wakeup(co) - end - else - close_channel_socket(self) - local errmsg - if result_ok ~= socket_error then - errmsg = result_ok - end - self.__result[co] = socket_error - self.__result_data[co] = errmsg - skynet.wakeup(co) - wakeup_all(self, errmsg) + close_channel_socket(self) + local errmsg + if result_ok ~= socket_error then + errmsg = result_ok end + self.__result[co] = socket_error + self.__result_data[co] = errmsg + skynet.wakeup(co) + wakeup_all(self, errmsg) end end end @@ -288,7 +294,7 @@ local function block_connect(self, once) -- connecting in other coroutine local co = coroutine.running() table.insert(self.__connecting, co) - skynet.wait() + skynet.wait(co) else self.__connecting[1] = true try_connect(self, once) @@ -319,7 +325,7 @@ end local function wait_for_response(self, response) local co = coroutine.running() push_response(self, response, co) - skynet.wait() + skynet.wait(co) local result = self.__result[co] self.__result[co] = nil From bab91a306753174d2e95cfca133b7ff8f78ea642 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 15 Oct 2015 14:01:50 +0800 Subject: [PATCH 491/729] bugfix: don't use global wait_response --- lualib/socketchannel.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 547b9f87..0452cc15 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -115,16 +115,14 @@ local function dispatch_by_session(self) end end -local wait_response - local function pop_response(self) while true do local func,co = table.remove(self.__request, 1), table.remove(self.__thread, 1) if func then return func, co end - wait_response = coroutine.running() - skynet.wait(wait_response) + self.wait_response = coroutine.running() + skynet.wait(self.wait_response) end end @@ -136,9 +134,9 @@ local function push_response(self, response, co) -- response is a function, push it to __request table.insert(self.__request, response) table.insert(self.__thread, co) - if wait_response then - skynet.wakeup(wait_response) - wait_response = nil + if self.wait_response then + skynet.wakeup(self.wait_response) + self.wait_response = nil end end end From 2d27df6f4607d637b4773977b385efeb2db4c2f6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 15 Oct 2015 14:08:01 +0800 Subject: [PATCH 492/729] change name --- lualib/socketchannel.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 0452cc15..7e7a0f6b 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -121,8 +121,8 @@ local function pop_response(self) if func then return func, co end - self.wait_response = coroutine.running() - skynet.wait(self.wait_response) + self.__wait_response = coroutine.running() + skynet.wait(self.__wait_response) end end @@ -134,9 +134,9 @@ local function push_response(self, response, co) -- response is a function, push it to __request table.insert(self.__request, response) table.insert(self.__thread, co) - if self.wait_response then - skynet.wakeup(self.wait_response) - self.wait_response = nil + if self.__wait_response then + skynet.wakeup(self.__wait_response) + self.__wait_response = nil end end end From 26cbebfcfd0051bdb17757a96df7509f96fefeab Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 21 Oct 2015 12:58:57 +0800 Subject: [PATCH 493/729] verify callback overflow --- lualib-src/sproto/sproto.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index b5d61ea6..ec77441b 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -677,6 +677,7 @@ encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int si args->value = data+SIZEOF_LENGTH; args->length = size-SIZEOF_LENGTH; sz = cb(args); + assert(sz <= size-SIZEOF_LENGTH); // verify buffer overflow if (sz <= 0) return sz; if (args->type == SPROTO_TSTRING) { From fe3954fb041c676557acccf9c5148c20d4a771ba Mon Sep 17 00:00:00 2001 From: fztcjjl Date: Sat, 24 Oct 2015 01:35:16 +0800 Subject: [PATCH 494/729] add a callback for mysql auth --- lualib/mysql.lua | 13 ++++++++----- test/testmysql.lua | 6 ++---- 2 files changed, 10 insertions(+), 9 deletions(-) mode change 100755 => 100644 lualib/mysql.lua diff --git a/lualib/mysql.lua b/lualib/mysql.lua old mode 100755 new mode 100644 index 305e900b..331c7942 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -418,7 +418,7 @@ local function _recv_auth_resp(self) end -local function _mysql_login(self,user,password,database) +local function _mysql_login(self,user,password,database,cb) return function(sockchannel) local packet, typ, err = sockchannel:response( _recv_decode_packet_resp(self) ) @@ -490,7 +490,10 @@ local function _mysql_login(self,user,password,database) local packet_len = #req local authpacket=_compose_packet(self,req,packet_len) - return sockchannel:request(authpacket,_recv_auth_resp(self)) + sockchannel:request(authpacket,_recv_auth_resp(self)) + if cb then + cb(self) + end end end @@ -626,7 +629,7 @@ local function _query_resp(self) end end -function _M.connect( opts) +function _M.connect(opts, cb) local self = setmetatable( {}, mt) @@ -645,11 +648,11 @@ function _M.connect( opts) local channel = socketchannel.channel { host = opts.host, port = opts.port or 3306, - auth = _mysql_login(self,user,password,database ), + auth = _mysql_login(self,user,password,database,cb), } + self.sockchannel = channel -- try connect first only once channel:connect(true) - self.sockchannel = channel return self diff --git a/test/testmysql.lua b/test/testmysql.lua index 7bd54855..8679e7bc 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -70,21 +70,19 @@ local function test3( db) end skynet.start(function() - local db=mysql.connect{ + local db=mysql.connect({ host="127.0.0.1", port=3306, database="skynet", user="root", password="1", max_packet_size = 1024 * 1024 - } + }, function(db) db:query("set charset utf8") end) if not db then print("failed to connect") end print("testmysql success to connect to mysql server") - db:query("set names utf8") - local res = db:query("drop table if exists cats") res = db:query("create table cats " .."(id serial primary key, ".. "name varchar(5))") From 1653b3213a8faae7df79438f4c79e4c2157b3352 Mon Sep 17 00:00:00 2001 From: fztcjjl Date: Sat, 24 Oct 2015 06:37:25 +0800 Subject: [PATCH 495/729] add a callback for mysql auth --- lualib/mysql.lua | 10 +++++----- test/testmysql.lua | 8 ++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index 331c7942..c3573ae2 100644 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -418,7 +418,7 @@ local function _recv_auth_resp(self) end -local function _mysql_login(self,user,password,database,cb) +local function _mysql_login(self,user,password,database,on_connect) return function(sockchannel) local packet, typ, err = sockchannel:response( _recv_decode_packet_resp(self) ) @@ -491,8 +491,8 @@ local function _mysql_login(self,user,password,database,cb) local authpacket=_compose_packet(self,req,packet_len) sockchannel:request(authpacket,_recv_auth_resp(self)) - if cb then - cb(self) + if on_connect then + on_connect(self) end end end @@ -629,7 +629,7 @@ local function _query_resp(self) end end -function _M.connect(opts, cb) +function _M.connect(opts) local self = setmetatable( {}, mt) @@ -648,7 +648,7 @@ function _M.connect(opts, cb) local channel = socketchannel.channel { host = opts.host, port = opts.port or 3306, - auth = _mysql_login(self,user,password,database,cb), + auth = _mysql_login(self,user,password,database,opts.on_connect), } self.sockchannel = channel -- try connect first only once diff --git a/test/testmysql.lua b/test/testmysql.lua index 8679e7bc..e8b420aa 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -70,14 +70,18 @@ local function test3( db) end skynet.start(function() + local function on_connect(db) + db:query("set charset utf8"); + end local db=mysql.connect({ host="127.0.0.1", port=3306, database="skynet", user="root", password="1", - max_packet_size = 1024 * 1024 - }, function(db) db:query("set charset utf8") end) + max_packet_size = 1024 * 1024, + on_connect = on_connect + }) if not db then print("failed to connect") end From f5740e00b17d125e0be02510a93b7db6aea59b5a Mon Sep 17 00:00:00 2001 From: nbwk1988 Date: Mon, 26 Oct 2015 16:12:10 +0800 Subject: [PATCH 496/729] add hmac_md5 add hmac_md5 --- lualib/md5.lua | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lualib/md5.lua b/lualib/md5.lua index 26f5cfa6..ceaddb38 100644 --- a/lualib/md5.lua +++ b/lualib/md5.lua @@ -15,4 +15,22 @@ function core.sumhexa (k) end)) end -return core \ No newline at end of file +function core.hmac_md5(data,key) + if #key>64 then + key=core.sum(key) + key=string.sub(key,1,16) + end + + local b=table.pack(string.byte(key,1,#key)) + local ipad_s="" + local opad_s="" + for i=1,64 do + ipad_s=ipad_s..string.char((b[i] or 0)~0x36) + opad_s=opad_s..string.char((b[i] or 0)~0x5c) + end + local istr=core.sum(ipad_s..data) + local ostr=core.sumhexa(opad_s..istr) + return ostr +end + +return core From 1999a03ff7f913fe97eb58c3a7c5baa17f73da49 Mon Sep 17 00:00:00 2001 From: nbwk1988 Date: Tue, 27 Oct 2015 10:44:23 +0800 Subject: [PATCH 497/729] =?UTF-8?q?=E4=BC=98=E5=8C=96hmac?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/md5.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lualib/md5.lua b/lualib/md5.lua index ceaddb38..90e28739 100644 --- a/lualib/md5.lua +++ b/lualib/md5.lua @@ -15,19 +15,21 @@ function core.sumhexa (k) end)) end -function core.hmac_md5(data,key) +local function get_ipad(c) + return string.char(c:byte() ~ 0x36) +end + +local function get_opad(c) + return string.char(c:byte() ~ 0x5c) +end + +function core.hmacmd5(data,key) if #key>64 then key=core.sum(key) - key=string.sub(key,1,16) - end - - local b=table.pack(string.byte(key,1,#key)) - local ipad_s="" - local opad_s="" - for i=1,64 do - ipad_s=ipad_s..string.char((b[i] or 0)~0x36) - opad_s=opad_s..string.char((b[i] or 0)~0x5c) + key=key:sub(1,16) end + local ipad_s=key:gsub(".", get_ipad)..string.rep("6",64-#key) + local opad_s=key:gsub(".", get_opad)..string.rep("\\",64-#key) local istr=core.sum(ipad_s..data) local ostr=core.sumhexa(opad_s..istr) return ostr From a4af35f442ca4779d1320a6dd4a38eba5a7e4e33 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 27 Oct 2015 14:00:25 +0800 Subject: [PATCH 498/729] fix issue #364 --- lualib/snax/interface.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index bd996c4d..740f09d9 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -59,8 +59,6 @@ return function (name , G, loader) end end - setmetatable(G, { __index = env , __newindex = init_system }) - local pattern do @@ -85,9 +83,10 @@ return function (name , G, loader) end end - mainfunc() - + setmetatable(G, { __index = env , __newindex = init_system }) + local ok, err = pcall(mainfunc) setmetatable(G, nil) + assert(ok,err) for k,v in pairs(temp_global) do G[k] = v From 13d920e1852d71584c830dd3b76fc99810699a03 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 27 Oct 2015 15:40:15 +0800 Subject: [PATCH 499/729] body may contain ascii 0, see issue #365 --- lualib/http/httpc.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index ae771dc8..49ccb9b8 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -22,8 +22,9 @@ local function request(fd, method, host, url, recvheader, header, content) end if content then - local data = string.format("%s %s HTTP/1.1\r\n%scontent-length:%d\r\n\r\n%s", method, url, header_content, #content, content) + local data = string.format("%s %s HTTP/1.1\r\n%scontent-length:%d\r\n\r\n", method, url, header_content, #content) write(data) + write(content) else local request_header = string.format("%s %s HTTP/1.1\r\n%scontent-length:0\r\n\r\n", method, url, header_content) write(request_header) From 935863f43455c5e902d1c62a7e9d481b3eda2cb1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 28 Oct 2015 16:12:01 +0800 Subject: [PATCH 500/729] report connect failed reason --- lualib/socket.lua | 6 +++++- lualib/socketchannel.lua | 18 +++++++++++------- skynet-src/socket_server.c | 5 ++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lualib/socket.lua b/lualib/socket.lua index 58fe2678..25a6099f 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -112,8 +112,10 @@ socket_message[5] = function(id, _, err) skynet.error("socket: error on unknown", id, err) return end - if s.connected or s.connecting then + if s.connected then skynet.error("socket: error on", id, err) + elseif s.connecting then + s.connecting = err end s.connected = false driver.close(id) @@ -179,11 +181,13 @@ local function connect(id, func) } socket_pool[id] = s suspend(s) + local err = s.connecting s.connecting = nil if s.connected then return id else socket_pool[id] = nil + return nil, err end end diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 7e7a0f6b..31a339c5 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -209,11 +209,11 @@ local function connect_once(self) return false end assert(not self.__sock and not self.__authcoroutine) - local fd = socket.open(self.__host, self.__port) + local fd,err = socket.open(self.__host, self.__port) if not fd then fd = connect_backup(self) if not fd then - return false + return false, err end end if self.__nodelay then @@ -247,13 +247,16 @@ end local function try_connect(self , once) local t = 0 while not self.__closed do - if connect_once(self) then + local ok, err = connect_once(self) + if ok then if not once then skynet.error("socket: connect to", self.__host, self.__port) end - return true + return elseif once then - return false + return err + else + skynet.error("socket: connect", err) end if t > 1000 then skynet.error("socket: try to reconnect", self.__host, self.__port) @@ -287,6 +290,7 @@ local function block_connect(self, once) if r ~= nil then return r end + local err if #self.__connecting > 0 then -- connecting in other coroutine @@ -295,7 +299,7 @@ local function block_connect(self, once) skynet.wait(co) else self.__connecting[1] = true - try_connect(self, once) + err = try_connect(self, once) self.__connecting[1] = nil for i=2, #self.__connecting do local co = self.__connecting[i] @@ -306,7 +310,7 @@ local function block_connect(self, once) r = check_connection(self) if r == nil then - error(string.format("Connect to %s:%d failed", self.__host, self.__port)) + error(string.format("Connect to %s:%d failed (%s)", self.__host, self.__port, err)) else return r end diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 4963dd37..6ee4d03a 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1095,7 +1095,10 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); if (code < 0 || error) { force_close(ss,s, result); - result->data = strerror(errno); + if (code >= 0) + result->data = strerror(error); + else + result->data = strerror(errno); return SOCKET_ERROR; } else { s->type = SOCKET_TYPE_CONNECTED; From 0199ba1382c1007e9ba9e4f79d9f1b916910abb2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 28 Oct 2015 17:37:06 +0800 Subject: [PATCH 501/729] sharedata support filename string --- lualib/skynet.lua | 8 ++++++-- service/sharedatad.lua | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index c20efaa2..2dd38f8d 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -608,11 +608,15 @@ local function init_all() end end +local function ret(f, ...) + f() + return ... +end + local function init_template(start) init_all() init_func = {} - start() - init_all() + return ret(init_all, start()) end function skynet.pcall(start) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index d76fd115..1ae9022c 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -1,6 +1,8 @@ local skynet = require "skynet" local sharedata = require "sharedata.corelib" local table = table +local cache = require "skynet.codecache" +cache.mode "OFF" -- turn off codecache, because CMD.new may load data file local NORET = {} local pool = {} @@ -43,9 +45,17 @@ function CMD.new(name, t) value = t elseif dt == "string" then value = setmetatable({}, env_mt) - local f = load(t, "=" .. name, "t", value) - assert(skynet.pcall(f)) + local f + if t:sub(1,1) == "@" then + f = assert(loadfile(t:sub(2),"bt",value)) + else + f = assert(load(t, "=" .. name, "bt", value)) + end + local _, ret = assert(skynet.pcall(f)) setmetatable(value, nil) + if type(ret) == "table" then + value = ret + end elseif dt == "nil" then value = {} else From ac4d51b779c3719d6f8723137e343228291d9232 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 29 Oct 2015 11:22:26 +0800 Subject: [PATCH 502/729] bugfix issue #368 --- lualib-src/sproto/sproto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index ec77441b..68ccd047 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -677,12 +677,12 @@ encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int si args->value = data+SIZEOF_LENGTH; args->length = size-SIZEOF_LENGTH; sz = cb(args); - assert(sz <= size-SIZEOF_LENGTH); // verify buffer overflow if (sz <= 0) return sz; if (args->type == SPROTO_TSTRING) { --sz; // the length of null string is 1 } + assert(sz <= size-SIZEOF_LENGTH); // verify buffer overflow return fill_size(data, sz); } From d1b26a60eb016fac567d1422638fa6dbae5f60ec Mon Sep 17 00:00:00 2001 From: kexiao8 Date: Sat, 31 Oct 2015 17:43:33 +0800 Subject: [PATCH 503/729] =?UTF-8?q?=20repeated=20=E2=80=9Cstruct=20table"?= =?UTF-8?q?=20of=20line=2019?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib-src/lua-sharedata.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 99cadd15..abbc175d 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -36,8 +36,6 @@ struct node { uint8_t nocolliding; // 0 means colliding slot }; -struct table; - struct state { int dirty; int ref; From d83fc0c00fe5db22c8e47f3d896ea375a224cba9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 2 Nov 2015 19:50:51 +0800 Subject: [PATCH 504/729] sproto support all integer representation --- lualib-src/sproto/lsproto.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 16eaa022..1d71d887 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -41,8 +41,18 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { #if LUA_VERSION_NUM < 503 -// lua_isinteger is lua 5.3 api -#define lua_isinteger lua_isnumber +#if LUA_VERSION_NUM < 502 +static lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum) { + if (lua_isnumber(L, idx)) { + if (isnum) *isnum = 1; + return lua_tointeger(L, idx); + } + else { + if (isnum) *isnum = 0; + return 0; + } +} +#endif // work around , use push & lua_gettable may be better #define lua_geti lua_rawgeti @@ -156,11 +166,11 @@ encode(const struct sproto_arg *args) { case SPROTO_TINTEGER: { lua_Integer v; lua_Integer vh; - if (!lua_isinteger(L, -1)) { + int isnum; + v = lua_tointegerx(L, -1, &isnum); + if(!isnum) { return luaL_error(L, ".%s[%d] is not an integer (Is a %s)", args->tagname, args->index, lua_typename(L, lua_type(L, -1))); - } else { - v = lua_tointeger(L, -1); } lua_pop(L,1); // notice: in lua 5.2, lua_Integer maybe 52bit From 9382454bcdd1443db501136089e6ce0056392ab3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 10 Nov 2015 12:12:42 +0800 Subject: [PATCH 505/729] update jemalloc to 4.0.4 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index e9192eac..91010a9e 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit e9192eacf8935e29fc62fddc2701f7942b1cc02c +Subproject commit 91010a9e2ebfc84b1ac1ed7fdde3bfed4f65f180 From c28ae25c7020673790c160d30ef7488613aeb458 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 10 Nov 2015 12:19:04 +0800 Subject: [PATCH 506/729] release 1.0.0 beta --- HISTORY.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 48c7a069..23e1ae10 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,22 @@ +v1.0.0-beta (2015-11-10) +----------- +* Improve and fix bug for sproto +* Add global short string pool for lua vm +* Add code cache mode +* Add a callback for mysql auth +* Add hmac_md5 +* Sharedata support filename as a string +* Fix a bug in socket.httpc +* Fix a lua stack overflow bug in lua bson +* Fix a socketchannel bug may block the data steam +* Avoid dead loop when sending message to the service exiting +* Fix memory leak in netpack +* Improve DH key exchange implement +* Minor fix for socket +* Minor fix for multicast +* Update jemalloc to 4.0.4 +* Update lpeg to 1.0.0 + v1.0.0-alpha10 (2015-8-17) ----------- * Remove the size limit of cluster RPC message. From 7521fe05300c117923a57c5cbd6409de355b1f5a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Nov 2015 10:44:50 +0800 Subject: [PATCH 507/729] socket.shutdown abandon unsend buffer now, see issue #371 --- lualib-src/lua-socket.c | 9 +++++++++ lualib/socket.lua | 14 +++++++++----- skynet-src/skynet_socket.c | 6 ++++++ skynet-src/skynet_socket.h | 1 + skynet-src/socket_server.c | 14 +++++++++++++- skynet-src/socket_server.h | 1 + 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 5ac0b359..9d12ab52 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -461,6 +461,14 @@ lclose(lua_State *L) { return 0; } +static int +lshutdown(lua_State *L) { + int id = luaL_checkinteger(L,1); + struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); + skynet_socket_shutdown(ctx, id); + return 0; +} + static int llisten(lua_State *L) { const char * host = luaL_checkstring(L,1); @@ -639,6 +647,7 @@ luaopen_socketdriver(lua_State *L) { luaL_Reg l2[] = { { "connect", lconnect }, { "close", lclose }, + { "shutdown", lshutdown }, { "listen", llisten }, { "send", lsend }, { "lsend", lsendlow }, diff --git a/lualib/socket.lua b/lualib/socket.lua index 25a6099f..1fd9d60b 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -118,7 +118,7 @@ socket_message[5] = function(id, _, err) s.connecting = err end s.connected = false - driver.close(id) + driver.shutdown(id) wakeup(s) end @@ -210,18 +210,22 @@ function socket.start(id, func) return connect(id, func) end -function socket.shutdown(id) +local function close_fd(id, func) local s = socket_pool[id] if s then if s.buffer then driver.clear(s.buffer,buffer_pool) end if s.connected then - driver.close(id) + func(id) end end end +function socket.shutdown(id) + close_fd(id, driver.shutdown) +end + function socket.close(id) local s = socket_pool[id] if s == nil then @@ -232,7 +236,7 @@ function socket.close(id) -- notice: call socket.close in __gc should be carefully, -- because skynet.wait never return in __gc, so driver.clear may not be called if s.co then - -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediatel + -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediately -- wait reading coroutine read the buffer. assert(not s.closing) s.closing = coroutine.running() @@ -242,7 +246,7 @@ function socket.close(id) end s.connected = false end - socket.shutdown(id) + close_fd(id) -- clear the buffer (already close fd) assert(s.lock_set == nil or next(s.lock_set) == nil) socket_pool[id] = nil end diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index f6f8ec56..2f3aec84 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -159,6 +159,12 @@ skynet_socket_close(struct skynet_context *ctx, int id) { socket_server_close(SOCKET_SERVER, source, id); } +void +skynet_socket_shutdown(struct skynet_context *ctx, int id) { + uint32_t source = skynet_context_handle(ctx); + socket_server_shutdown(SOCKET_SERVER, source, id); +} + void skynet_socket_start(struct skynet_context *ctx, int id) { uint32_t source = skynet_context_handle(ctx); diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index bcdc137c..55b98595 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -29,6 +29,7 @@ int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port); int skynet_socket_bind(struct skynet_context *ctx, int fd); void skynet_socket_close(struct skynet_context *ctx, int id); +void skynet_socket_shutdown(struct skynet_context *ctx, int id); void skynet_socket_start(struct skynet_context *ctx, int id); void skynet_socket_nodelay(struct skynet_context *ctx, int id); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 6ee4d03a..2e26e24f 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -119,6 +119,7 @@ struct request_setudp { struct request_close { int id; + int shutdown; uintptr_t opaque; }; @@ -787,7 +788,7 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc if (type != -1) return type; } - if (send_buffer_empty(s)) { + if (request->shutdown || send_buffer_empty(s)) { force_close(ss,s,result); result->id = id; result->opaque = request->opaque; @@ -1366,6 +1367,17 @@ void socket_server_close(struct socket_server *ss, uintptr_t opaque, int id) { struct request_package request; request.u.close.id = id; + request.u.close.shutdown = 0; + request.u.close.opaque = opaque; + send_request(ss, &request, 'K', sizeof(request.u.close)); +} + + +void +socket_server_shutdown(struct socket_server *ss, uintptr_t opaque, int id) { + struct request_package request; + request.u.close.id = id; + request.u.close.shutdown = 1; request.u.close.opaque = opaque; send_request(ss, &request, 'K', sizeof(request.u.close)); } diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index b6f0f5fb..41ef9cca 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -26,6 +26,7 @@ int socket_server_poll(struct socket_server *, struct socket_message *result, in void socket_server_exit(struct socket_server *); void socket_server_close(struct socket_server *, uintptr_t opaque, int id); +void socket_server_shutdown(struct socket_server *, uintptr_t opaque, int id); void socket_server_start(struct socket_server *, uintptr_t opaque, int id); // return -1 when error From 9f3baf2ee3e0157d2f3f37ffdafdce502a17f4ad Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 Nov 2015 15:06:10 +0800 Subject: [PATCH 508/729] fix issue #372 --- skynet-src/skynet_error.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/skynet-src/skynet_error.c b/skynet-src/skynet_error.c index 962ea597..856c4700 100644 --- a/skynet-src/skynet_error.c +++ b/skynet-src/skynet_error.c @@ -28,7 +28,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { va_start(ap,msg); int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap); va_end(ap); - if (len < LOG_MESSAGE_SIZE) { + if (len >=0 && len < LOG_MESSAGE_SIZE) { data = skynet_strdup(tmp); } else { int max_size = LOG_MESSAGE_SIZE; @@ -44,6 +44,11 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { skynet_free(data); } } + if (len < 0) { + skynet_free(data); + perror("vsnprintf error :"); + return; + } struct skynet_message smsg; From fa729d593b2672776ff0e614561a315a44bf0ea2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 12 Nov 2015 10:53:14 +0800 Subject: [PATCH 509/729] lua alloc use raw allocator --- skynet-src/malloc_hook.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 3b072168..06e3bbf7 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -25,6 +25,10 @@ static mem_data mem_stats[SLOT_SIZE]; #include "jemalloc.h" +// for skynet_lalloc use +#define raw_realloc je_realloc +#define raw_free je_free + static ssize_t* get_allocated_field(uint32_t handle) { int h = (int)(handle & (SLOT_SIZE - 1)); @@ -165,6 +169,10 @@ skynet_calloc(size_t nmemb,size_t size) { #else +// for skynet_lalloc use +#define raw_realloc realloc +#define raw_free free + void memory_info_dump(void) { skynet_error(NULL, "No jemalloc"); @@ -220,10 +228,10 @@ skynet_strdup(const char *str) { void * skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize) { if (nsize == 0) { - skynet_free(ptr); + raw_free(ptr); return NULL; } else { - return skynet_realloc(ptr, nsize); + return raw_realloc(ptr, nsize); } } From 7138144a689374399baef55572a4d192d6c0486e Mon Sep 17 00:00:00 2001 From: huanzai <857763401@qq.com> Date: Thu, 12 Nov 2015 11:15:50 +0800 Subject: [PATCH 510/729] Update lua-netpack.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当长度等于0x10000时,read_size读出的值为0 --- lualib-src/lua-netpack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 6e601e07..5fae0406 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -435,7 +435,7 @@ static int lpack(lua_State *L) { size_t len; const char * ptr = tolstring(L, &len, 1); - if (len > 0x10000) { + if (len >= 0x10000) { return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); } From 5edde990d6fd5cd8e8e25cc8e4e23d98ce0a5da4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 12 Nov 2015 15:41:41 +0800 Subject: [PATCH 511/729] bugfix: skynet.fork use table.pack to pack table --- lualib/skynet.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 2dd38f8d..0bc2d6e9 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -446,12 +446,10 @@ function skynet.dispatch_unknown_response(unknown) return prev end -local tunpack = table.unpack - function skynet.fork(func,...) - local args = { ... } + local args = table.pack(...) local co = co_create(function() - func(tunpack(args)) + func(table.unpack(args,1,args.n)) end) table.insert(fork_queue, co) return co From b72f0921d4ea9b9184fbfd675f422eb744309357 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 12 Nov 2015 16:21:44 +0800 Subject: [PATCH 512/729] remove unused local function --- service/snaxd.lua | 8 -------- 1 file changed, 8 deletions(-) diff --git a/service/snaxd.lua b/service/snaxd.lua index d27008be..1b8ddee6 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -26,14 +26,6 @@ end local traceback = debug.traceback -local function do_func(f, msg) - return xpcall(f, traceback, table.unpack(msg)) -end - -local function dispatch(f, ...) - return skynet.pack(f(...)) -end - local function return_f(f, ...) return skynet.ret(skynet.pack(f(...))) end From d283d7fcaab86905f1d621a5ca25ec339f2ca220 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 12 Nov 2015 18:34:50 +0800 Subject: [PATCH 513/729] check close return value --- skynet-src/socket_server.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 2e26e24f..a3326b05 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -337,7 +337,9 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_message *r sp_del(ss->event_fd, s->fd); } if (s->type != SOCKET_TYPE_BIND) { - close(s->fd); + if (close(s->fd) < 0) { + perror("close socket:"); + } } s->type = SOCKET_TYPE_INVALID; } From 471aecd708156f922ef9a08235b96d2c7559fb96 Mon Sep 17 00:00:00 2001 From: xiefan Date: Thu, 12 Nov 2015 20:45:19 +0800 Subject: [PATCH 514/729] redis pipeline redis pipeline --- lualib/redis.lua | 22 ++++++++++++++++++++++ test/testpipeline.lua | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/testpipeline.lua diff --git a/lualib/redis.lua b/lualib/redis.lua index b1e44185..c05e8423 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -4,6 +4,7 @@ local socketchannel = require "socketchannel" local table = table local string = string +local assert = assert local redis = {} local command = {} @@ -161,6 +162,27 @@ function command:sismember(key, value) return fd:request(compose_message ("SISMEMBER", {key, value}), read_boolean) end +function command:pipeline(ops) + assert(#ops > 0, "pipeline is null") + + local fd = self[1] + + local cmds = {} + for _, cmd in ipairs(ops) do + assert(#cmd >= 2, "pipeline error, the params length is less than 2") + table.insert(cmds, compose_message(string.upper(cmd[1]), {cmd[2], cmd[3], cmd[4]})) + end + + return fd:request(table.concat(cmds, "\r\n"), function (fd) + local result = {} + for i=1, #ops do + local ok, out = read_response(fd) + table.insert(result, {ok = ok, out = out}) + end + return true, result + end) +end + --- watch mode local watch = {} diff --git a/test/testpipeline.lua b/test/testpipeline.lua new file mode 100644 index 00000000..ae55b766 --- /dev/null +++ b/test/testpipeline.lua @@ -0,0 +1,37 @@ +local skynet = require "skynet" +local redis = require "redis" + +local conf = { + host = "127.0.0.1", + port = 6379, + db = 0 +} + +local function read_table(t) + local result = { } + for i = 1, #t, 2 do result[t[i]] = t[i + 1] end + return result +end + +skynet.start(function() + local db = redis.connect(conf) + + local ret = db:pipeline { + {"hincrby", "hello", 1, 1}, + {"del", "hello"}, + {"hincrby", "hello", 3, 1}, + {"hgetall", "hello"}, + } + + print(ret[1].out) + print(ret[2].out) + print(ret[3].out) + + for k, v in pairs(read_table(ret[4].out)) do + print(k, v) + end + + db:disconnect() + skynet.exit() +end) + From d2396f9d204e89f95dea2782ce67a613b878a8c6 Mon Sep 17 00:00:00 2001 From: xiefan Date: Fri, 13 Nov 2015 08:10:33 +0800 Subject: [PATCH 515/729] Revert "redis pipeline" This reverts commit 471aecd708156f922ef9a08235b96d2c7559fb96. --- lualib/redis.lua | 22 ---------------------- test/testpipeline.lua | 37 ------------------------------------- 2 files changed, 59 deletions(-) delete mode 100644 test/testpipeline.lua diff --git a/lualib/redis.lua b/lualib/redis.lua index c05e8423..b1e44185 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -4,7 +4,6 @@ local socketchannel = require "socketchannel" local table = table local string = string -local assert = assert local redis = {} local command = {} @@ -162,27 +161,6 @@ function command:sismember(key, value) return fd:request(compose_message ("SISMEMBER", {key, value}), read_boolean) end -function command:pipeline(ops) - assert(#ops > 0, "pipeline is null") - - local fd = self[1] - - local cmds = {} - for _, cmd in ipairs(ops) do - assert(#cmd >= 2, "pipeline error, the params length is less than 2") - table.insert(cmds, compose_message(string.upper(cmd[1]), {cmd[2], cmd[3], cmd[4]})) - end - - return fd:request(table.concat(cmds, "\r\n"), function (fd) - local result = {} - for i=1, #ops do - local ok, out = read_response(fd) - table.insert(result, {ok = ok, out = out}) - end - return true, result - end) -end - --- watch mode local watch = {} diff --git a/test/testpipeline.lua b/test/testpipeline.lua deleted file mode 100644 index ae55b766..00000000 --- a/test/testpipeline.lua +++ /dev/null @@ -1,37 +0,0 @@ -local skynet = require "skynet" -local redis = require "redis" - -local conf = { - host = "127.0.0.1", - port = 6379, - db = 0 -} - -local function read_table(t) - local result = { } - for i = 1, #t, 2 do result[t[i]] = t[i + 1] end - return result -end - -skynet.start(function() - local db = redis.connect(conf) - - local ret = db:pipeline { - {"hincrby", "hello", 1, 1}, - {"del", "hello"}, - {"hincrby", "hello", 3, 1}, - {"hgetall", "hello"}, - } - - print(ret[1].out) - print(ret[2].out) - print(ret[3].out) - - for k, v in pairs(read_table(ret[4].out)) do - print(k, v) - end - - db:disconnect() - skynet.exit() -end) - From 34d9b5555df178456776b6a023c4883dac95fa5e Mon Sep 17 00:00:00 2001 From: xiefan Date: Fri, 13 Nov 2015 08:16:47 +0800 Subject: [PATCH 516/729] reds pipeline v2(support function as pipeline params) fix a bug & add function as pipeline params --- lualib/redis.lua | 22 +++++++++++++ test/testpipeline.lua | 72 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 test/testpipeline.lua diff --git a/lualib/redis.lua b/lualib/redis.lua index b1e44185..2006c335 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -4,6 +4,7 @@ local socketchannel = require "socketchannel" local table = table local string = string +local assert = assert local redis = {} local command = {} @@ -161,6 +162,27 @@ function command:sismember(key, value) return fd:request(compose_message ("SISMEMBER", {key, value}), read_boolean) end +function command:pipeline(ops) + assert(ops and #ops > 0, "pipeline is null") + + local fd = self[1] + + local cmds = {} + for _, cmd in ipairs(ops) do + assert(#cmd >= 2, "pipeline error, the params length is less than 2") + table.insert(cmds, compose_message(string.upper(table.remove(cmd, 1)), cmd)) + end + + return fd:request(table.concat(cmds, "\r\n"), function (fd) + local result = {} + for i=1, #ops do + local ok, out = read_response(fd) + table.insert(result, {ok = ok, out = out}) + end + return true, result + end) +end + --- watch mode local watch = {} diff --git a/test/testpipeline.lua b/test/testpipeline.lua new file mode 100644 index 00000000..4b0ecafb --- /dev/null +++ b/test/testpipeline.lua @@ -0,0 +1,72 @@ +local skynet = require "skynet" +local redis = require "redis" + +local conf = { + host = "127.0.0.1", + port = 6379, + db = 0 +} + +local function read_table(t) + local result = { } + for i = 1, #t, 2 do result[t[i]] = t[i + 1] end + return result +end + +skynet.start(function() + local db = redis.connect(conf) + + db.pipelining = function (self, block) + local ops = {} + + block(setmetatable({}, { + __index = function (_, name) + return function (_, ...) + table.insert(ops, {name, ...}) + end + end + })) + + return self:pipeline(ops) + end + + do + print("test function") + local ret = db:pipelining(function (red) + red:hincrby("hello", 1, 1) + red:del("hello") + red:hmset("hello", 1, 1, 2, 2, 3, 3) + red:hgetall("hello") + end) + + print(ret[1].out) + print(ret[2].out) + print(ret[3].out) + + for k, v in pairs(read_table(ret[4].out)) do + print(k, v) + end + end + + do + print("test table") + local ret = db:pipeline { + {"hincrby", "hello", 1, 1}, + {"del", "hello"}, + {"hmset", "hello", 1, 1, 2, 2, 3, 3}, + {"hgetall", "hello"}, + } + + print(ret[1].out) + print(ret[2].out) + print(ret[3].out) + + for k, v in pairs(read_table(ret[4].out)) do + print(k, v) + end + end + + db:disconnect() + skynet.exit() +end) + From ce50c47c2b3679ea70e617e9ac90c5da71a91330 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 13 Nov 2015 11:04:02 +0800 Subject: [PATCH 517/729] socket.send support strings table --- lualib-src/lua-socket.c | 55 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 9d12ab52..8f08ead0 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -484,18 +484,65 @@ llisten(lua_State *L) { return 1; } +static size_t +count_size(lua_State *L, int index) { + size_t tlen = 0; + int i; + for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) { + size_t len; + luaL_checklstring(L, -1, &len); + tlen += len; + lua_pop(L,1); + } + lua_pop(L,1); + return tlen; +} + +static void +concat_table(lua_State *L, int index, void *buffer, size_t tlen) { + char *ptr = buffer; + int i; + for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) { + size_t len; + const char * str = lua_tolstring(L, -1, &len); + if (str == NULL || tlen < len) { + break; + } + memcpy(ptr, str, len); + ptr += len; + tlen -= len; + lua_pop(L,1); + } + if (tlen != 0) { + skynet_free(buffer); + luaL_error(L, "Invalid strings table"); + } + lua_pop(L,1); +} + static void * get_buffer(lua_State *L, int index, int *sz) { void *buffer; - if (lua_isuserdata(L,index)) { + switch(lua_type(L, index)) { + const char * str; + size_t len; + case LUA_TUSERDATA: buffer = lua_touserdata(L,index); *sz = luaL_checkinteger(L,index+1); - } else { - size_t len = 0; - const char * str = luaL_checklstring(L, index, &len); + break; + case LUA_TTABLE: + // concat the table as a string + len = count_size(L, index); + buffer = skynet_malloc(len); + concat_table(L, index, buffer, len); + *sz = (int)len; + break; + default: + str = luaL_checklstring(L, index, &len); buffer = skynet_malloc(len); memcpy(buffer, str, len); *sz = (int)len; + break; } return buffer; } From 107be7ee8caa1f24b0cb4e4a0d9451805c6022cb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 13 Nov 2015 12:30:46 +0800 Subject: [PATCH 518/729] rewrite redis driver , avoid table.concat --- lualib/redis.lua | 96 ++++++++++++++++++++++++++++--------------- test/testpipeline.lua | 14 +++---- 2 files changed, 68 insertions(+), 42 deletions(-) diff --git a/lualib/redis.lua b/lualib/redis.lua index 2006c335..18cd6af0 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -97,41 +97,48 @@ function command:disconnect() end -- msg could be any type of value -local function pack_value(lines, v) - if v == nil then - return - end - v = tostring(v) +local header_cache = setmetatable({}, { + __mode = "kv", + __index = function(t,k) + local s = "\r\n$" .. k .. "\r\n" + t[k] = s + return s + end}) - table.insert(lines,"$"..#v) - table.insert(lines,v) -end +local command_cache = setmetatable({}, { + __mode = "kv", + __index = function(t,cmd) + local s = "\r\n$"..#cmd.."\r\n"..cmd:upper() + t[cmd] = s + return s + end}) local function compose_message(cmd, msg) - local len = 1 local t = type(msg) + local lines = {} if t == "table" then - len = len + #msg - elseif t ~= nil then - len = len + 1 - end - - local lines = {"*" .. len} - pack_value(lines, cmd) - - if t == "table" then + lines[1] = "*" .. (#msg+1) + lines[2] = command_cache[cmd] + local idx = 3 for _,v in ipairs(msg) do - pack_value(lines, v) + v= tostring(v) + lines[idx] = header_cache[#v] + lines[idx+1] = v + idx = idx + 2 end + lines[idx] = "\r\n" else - pack_value(lines, msg) + msg = tostring(msg) + lines[1] = "*2" + lines[2] = command_cache[cmd] + lines[3] = header_cache[#msg] + lines[4] = msg + lines[5] = "\r\n" end - table.insert(lines, "") - local chunk = table.concat(lines,"\r\n") - return chunk + return lines end setmetatable(command, { __index = function(t,k) @@ -162,25 +169,46 @@ function command:sismember(key, value) return fd:request(compose_message ("SISMEMBER", {key, value}), read_boolean) end -function command:pipeline(ops) +local function compose_table(lines, msg) + local tinsert = table.insert + tinsert(lines, "*" .. #msg) + for _,v in ipairs(msg) do + v = tostring(v) + tinsert(lines,header_cache[#v]) + tinsert(lines,v) + end + tinsert(lines, "\r\n") + return lines +end + +function command:pipeline(ops,resp) assert(ops and #ops > 0, "pipeline is null") local fd = self[1] local cmds = {} for _, cmd in ipairs(ops) do - assert(#cmd >= 2, "pipeline error, the params length is less than 2") - table.insert(cmds, compose_message(string.upper(table.remove(cmd, 1)), cmd)) + compose_table(cmds, cmd) end - return fd:request(table.concat(cmds, "\r\n"), function (fd) - local result = {} - for i=1, #ops do - local ok, out = read_response(fd) - table.insert(result, {ok = ok, out = out}) - end - return true, result - end) + if resp then + return fd:request(cmds, function (fd) + for i=1, #ops do + local ok, out = read_response(fd) + table.insert(resp, {ok = ok, out = out}) + end + return true, resp + end) + else + return fd:request(cmds, function (fd) + local ok, out + for i=1, #ops do + ok, out = read_response(fd) + end + -- return last response + return ok,out + end) + end end --- watch mode diff --git a/test/testpipeline.lua b/test/testpipeline.lua index 4b0ecafb..d5c57c8d 100644 --- a/test/testpipeline.lua +++ b/test/testpipeline.lua @@ -33,29 +33,27 @@ skynet.start(function() do print("test function") local ret = db:pipelining(function (red) + red:multi() red:hincrby("hello", 1, 1) red:del("hello") red:hmset("hello", 1, 1, 2, 2, 3, 3) red:hgetall("hello") + red:exec() end) - - print(ret[1].out) - print(ret[2].out) - print(ret[3].out) - - for k, v in pairs(read_table(ret[4].out)) do + -- ret is the result of red:exec() + for k, v in pairs(read_table(ret[4])) do print(k, v) end end do print("test table") - local ret = db:pipeline { + local ret = db:pipeline({ {"hincrby", "hello", 1, 1}, {"del", "hello"}, {"hmset", "hello", 1, 1, 2, 2, 3, 3}, {"hgetall", "hello"}, - } + }, {}) -- offer a {} for result print(ret[1].out) print(ret[2].out) From 30859a124478577f43ed06adbd0c993bdd1a2fdb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 13 Nov 2015 13:25:33 +0800 Subject: [PATCH 519/729] make cache --- lualib/redis.lua | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lualib/redis.lua b/lualib/redis.lua index 18cd6af0..3fc14a58 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -98,28 +98,37 @@ end -- msg could be any type of value -local header_cache = setmetatable({}, { - __mode = "kv", - __index = function(t,k) +local function make_cache(f) + return setmetatable({}, { + __mode = "kv", + __index = f, + }) +end + +local header_cache = make_cache(function(t,k) local s = "\r\n$" .. k .. "\r\n" t[k] = s return s - end}) + end) -local command_cache = setmetatable({}, { - __mode = "kv", - __index = function(t,cmd) +local command_cache = make_cache(function(t,cmd) local s = "\r\n$"..#cmd.."\r\n"..cmd:upper() t[cmd] = s return s - end}) + end) + +local count_cache = make_cache(function(t,k) + local s = "*" .. k + t[k] = s + return s + end) local function compose_message(cmd, msg) local t = type(msg) local lines = {} if t == "table" then - lines[1] = "*" .. (#msg+1) + lines[1] = count_cache[#msg+1] lines[2] = command_cache[cmd] local idx = 3 for _,v in ipairs(msg) do @@ -171,7 +180,7 @@ end local function compose_table(lines, msg) local tinsert = table.insert - tinsert(lines, "*" .. #msg) + tinsert(lines, count_cache[#msg]) for _,v in ipairs(msg) do v = tostring(v) tinsert(lines,header_cache[#v]) From 87bc0815f67397de82e4893dc5ee8e5e3bda0101 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 13 Nov 2015 17:28:02 +0800 Subject: [PATCH 520/729] bugfix: add lightuserdata --- lualib-src/lua-socket.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 8f08ead0..bc49e568 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -527,6 +527,7 @@ get_buffer(lua_State *L, int index, int *sz) { const char * str; size_t len; case LUA_TUSERDATA: + case LUA_TLIGHTUSERDATA: buffer = lua_touserdata(L,index); *sz = luaL_checkinteger(L,index+1); break; From 2252409eaaa164b8c934d64486be79b013dc006c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 16 Nov 2015 19:48:38 +0800 Subject: [PATCH 521/729] add skynet_now() for 64bit time --- lualib-src/lua-skynet.c | 8 ++++++++ lualib/skynet.lua | 13 ++++++++----- skynet-src/skynet.h | 1 + skynet-src/skynet_log.c | 10 +++++----- skynet-src/skynet_server.c | 10 +--------- skynet-src/skynet_timer.c | 23 ++++++++--------------- skynet-src/skynet_timer.h | 3 +-- 7 files changed, 32 insertions(+), 36 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 7d75d2ce..063320a4 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -326,6 +326,13 @@ ltrash(lua_State *L) { return 0; } +static int +lnow(lua_State *L) { + uint64_t ti = skynet_now(); + lua_pushinteger(L, ti); + return 1; +} + int luaopen_skynet_core(lua_State *L) { luaL_checkversion(L); @@ -344,6 +351,7 @@ luaopen_skynet_core(lua_State *L) { { "packstring", lpackstring }, { "trash" , ltrash }, { "callback", _callback }, + { "now", lnow }, { NULL, NULL }, }; diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 0bc2d6e9..4a101004 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -299,16 +299,19 @@ function skynet.localname(name) end end -function skynet.now() - return c.intcommand("NOW") -end +skynet.now = c.now + +local starttime function skynet.starttime() - return c.intcommand("STARTTIME") + if not starttime then + starttime = c.intcommand("STARTTIME") + end + return starttime end function skynet.time() - return skynet.now()/100 + skynet.starttime() -- get now first would be better + return skynet.now()/100 + starttime or skynet.starttime() end function skynet.exit() diff --git a/skynet-src/skynet.h b/skynet-src/skynet.h index fc8235e0..26df5ac6 100644 --- a/skynet-src/skynet.h +++ b/skynet-src/skynet.h @@ -38,5 +38,6 @@ typedef int (*skynet_cb)(struct skynet_context * context, void *ud, int type, in void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb); uint32_t skynet_current_handle(void); +uint64_t skynet_now(void); #endif diff --git a/skynet-src/skynet_log.c b/skynet-src/skynet_log.c index 65b97c30..4ce106ff 100644 --- a/skynet-src/skynet_log.c +++ b/skynet-src/skynet_log.c @@ -15,11 +15,11 @@ skynet_log_open(struct skynet_context * ctx, uint32_t handle) { sprintf(tmp, "%s/%08x.log", logpath, handle); FILE *f = fopen(tmp, "ab"); if (f) { - uint32_t starttime = skynet_gettime_fixsec(); - uint32_t currenttime = skynet_gettime(); + uint32_t starttime = skynet_starttime(); + uint64_t currenttime = skynet_now(); time_t ti = starttime + currenttime/100; skynet_error(ctx, "Open log file %s", tmp); - fprintf(f, "open time: %u %s", currenttime, ctime(&ti)); + fprintf(f, "open time: %u %s", (uint32_t)currenttime, ctime(&ti)); fflush(f); } else { skynet_error(ctx, "Open log file %s fail", tmp); @@ -30,7 +30,7 @@ skynet_log_open(struct skynet_context * ctx, uint32_t handle) { void skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) { skynet_error(ctx, "Close log file :%08x", handle); - fprintf(f, "close time: %u\n", skynet_gettime()); + fprintf(f, "close time: %u\n", (uint32_t)skynet_now()); fclose(f); } @@ -68,7 +68,7 @@ skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer if (type == PTYPE_SOCKET) { log_socket(f, buffer, sz); } else { - uint32_t ti = skynet_gettime(); + uint32_t ti = (uint32_t)skynet_now(); fprintf(f, ":%08x %d %d %u ", source, type, session, ti); log_blob(f, buffer, sz); fprintf(f,"\n"); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 64258a92..f5d7f6bd 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -429,13 +429,6 @@ cmd_name(struct skynet_context * context, const char * param) { return NULL; } -static const char * -cmd_now(struct skynet_context * context, const char * param) { - uint32_t ti = skynet_gettime(); - sprintf(context->result,"%u",ti); - return context->result; -} - static const char * cmd_exit(struct skynet_context * context, const char * param) { handle_exit(context, 0); @@ -507,7 +500,7 @@ cmd_setenv(struct skynet_context * context, const char * param) { static const char * cmd_starttime(struct skynet_context * context, const char * param) { - uint32_t sec = skynet_gettime_fixsec(); + uint32_t sec = skynet_starttime(); sprintf(context->result,"%u",sec); return context->result; } @@ -619,7 +612,6 @@ static struct command_func cmd_funcs[] = { { "REG", cmd_reg }, { "QUERY", cmd_query }, { "NAME", cmd_name }, - { "NOW", cmd_now }, { "EXIT", cmd_exit }, { "KILL", cmd_kill }, { "LAUNCH", cmd_launch }, diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 1f366b85..182b808d 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -45,10 +45,9 @@ struct timer { struct link_list t[4][TIME_LEVEL]; struct spinlock lock; uint32_t time; - uint32_t current; uint32_t starttime; + uint64_t current; uint64_t current_point; - uint64_t origin_point; }; static struct timer * TI = NULL; @@ -277,13 +276,7 @@ skynet_updatetime(void) { } else if (cp != TI->current_point) { uint32_t diff = (uint32_t)(cp - TI->current_point); TI->current_point = cp; - - uint32_t oc = TI->current; TI->current += diff; - if (TI->current < oc) { - // when cs > 0xffffffff(about 497 days), time rewind - TI->starttime += 0xffffffff / 100; - } int i; for (i=0;istarttime; } -uint32_t -skynet_gettime(void) { +uint64_t +skynet_now(void) { return TI->current; } void skynet_timer_init(void) { TI = timer_create_timer(); - systime(&TI->starttime, &TI->current); - uint64_t point = gettime(); - TI->current_point = point; - TI->origin_point = point; + uint32_t current = 0; + systime(&TI->starttime, ¤t); + TI->current = current; + TI->current_point = gettime(); } diff --git a/skynet-src/skynet_timer.h b/skynet-src/skynet_timer.h index 4d4e2b0f..b278dbcb 100644 --- a/skynet-src/skynet_timer.h +++ b/skynet-src/skynet_timer.h @@ -5,8 +5,7 @@ int skynet_timeout(uint32_t handle, int time, int session); void skynet_updatetime(void); -uint32_t skynet_gettime(void); -uint32_t skynet_gettime_fixsec(void); +uint32_t skynet_starttime(void); void skynet_timer_init(void); From f4437948ca1ce79c0d1fe24558587db92540d8f9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 16 Nov 2015 22:42:44 +0800 Subject: [PATCH 522/729] fix Issue #377 --- lualib/skynet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 4a101004..97a7f6b9 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -311,7 +311,7 @@ function skynet.starttime() end function skynet.time() - return skynet.now()/100 + starttime or skynet.starttime() + return skynet.now()/100 + (starttime or skynet.starttime()) end function skynet.exit() From 8d83881808633220a87275d294cdbee67b1477c7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 18 Nov 2015 10:19:51 +0800 Subject: [PATCH 523/729] use CLOCK_MONOTONIC maybe better --- skynet-src/skynet_timer.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 182b808d..5edff943 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -247,15 +247,8 @@ static uint64_t gettime() { uint64_t t; #if !defined(__APPLE__) - -#ifdef CLOCK_MONOTONIC_RAW -#define CLOCK_TIMER CLOCK_MONOTONIC_RAW -#else -#define CLOCK_TIMER CLOCK_MONOTONIC -#endif - struct timespec ti; - clock_gettime(CLOCK_TIMER, &ti); + clock_gettime(CLOCK_MONOTONIC, &ti); t = (uint64_t)ti.tv_sec * 100; t += ti.tv_nsec / 10000000; #else From 3335ea11d7de33fae11bf2f287947d6237a1bc1a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 Nov 2015 21:02:46 +0800 Subject: [PATCH 524/729] dummy luaS_expandshr --- skynet-src/luashrtbl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/luashrtbl.h b/skynet-src/luashrtbl.h index 824a0c85..a9485ccd 100644 --- a/skynet-src/luashrtbl.h +++ b/skynet-src/luashrtbl.h @@ -9,7 +9,7 @@ static inline int luaS_shrinfo(lua_State *L) { return 0; } static inline void luaS_initshr() {} static inline void luaS_exitshr() {} -static inline void luaS_expandshr(int n); +static inline void luaS_expandshr(int n) {} #endif From 91c720ad6fe34a24e4dcb1c10aaa20c5a5c6be20 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 Nov 2015 22:51:43 +0800 Subject: [PATCH 525/729] add assert for socket fd reuse --- lualib/socket.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/socket.lua b/lualib/socket.lua index 1fd9d60b..bc9701ae 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -179,6 +179,7 @@ local function connect(id, func) callback = func, protocol = "TCP", } + assert(not socket_pool[id], "socket is not closed") socket_pool[id] = s suspend(s) local err = s.connecting @@ -404,6 +405,7 @@ end local udp_socket = {} local function create_udp_object(id, cb) + assert(not socket_pool[id], "socket is not closed") socket_pool[id] = { id = id, connected = true, From fbbda08bb261244e4ee0444e507034c847b47fdc Mon Sep 17 00:00:00 2001 From: David Feng Date: Sat, 21 Nov 2015 15:36:25 +0800 Subject: [PATCH 526/729] fix typo --- examples/login/msgagent.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/login/msgagent.lua b/examples/login/msgagent.lua index fc3a2d5b..d00fd332 100644 --- a/examples/login/msgagent.lua +++ b/examples/login/msgagent.lua @@ -46,7 +46,7 @@ skynet.start(function() end) skynet.dispatch("client", function(_,_, msg) - -- the simple ehco service + -- the simple echo service skynet.sleep(10) -- sleep a while skynet.ret(msg) end) From 2c3f3ac3c67852eccc91e1c68e249c3a34920b95 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 23 Nov 2015 12:47:29 +0800 Subject: [PATCH 527/729] more sproto wire protocol examples --- lualib-src/sproto/README.md | 92 +++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index e397549d..8fcf5e34 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -219,10 +219,28 @@ If n is even (and not zero), the value of this field is n/2-1 ; If n is odd, that means the tags is not continuous, and we should add current tag by (n+1)/2 . +Arrays are always encode in data part, 4 bytes header for the size, and the following bytes is the contents. See the example 2 for the struct array; example 3/4 for the integer array ; example 5 for the boolean array. + +Fot integer array, an additional byte (4 or 8) to indicate the value is 32bit or 64bit. + Read the examples below to see more details. Notice: If the tag is not declared in schema, the decoder will simply ignore the field for protocol version compatibility. +``` +.Person { + name 0 : string + age 1 : integer + marital 2 : boolean + children 3 : *Person +} + +.Data { + numbers 0 : *integer + bools 1 : *boolean +} +``` + Example 1: ``` @@ -243,7 +261,8 @@ person { name = "Bob", age = 40, children = { - { name = "Alice" , age = 13, marital = false }, + { name = "Alice" , age = 13 }, + { name = "Carol" , age = 5 }, } } @@ -256,13 +275,76 @@ person { 03 00 00 00 (sizeof "Bob") 42 6F 62 ("Bob") -11 00 00 00 (sizeof struct) -03 00 (fn = 3) +26 00 00 00 (sizeof children) + +0F 00 00 00 (sizeof child 1) +02 00 (fn = 2) 00 00 (id = 0, value in data part) 1C 00 (id = 1, value = 13) -02 00 (id = 2, value = false) 05 00 00 00 (sizeof "Alice") 41 6C 69 63 65 ("Alice") + +0F 00 00 00 (sizeof child 2) +02 00 (fn = 2) +00 00 (id = 0, value in data part) +0C 00 (id = 1, value = 5) +05 00 00 00 (sizeof "Carol") +43 61 72 6F 6C ("Carol") +``` + +Example 3: + +``` +data { + numbers = { 1,2,3,4,5 } +} + +01 00 (fn = 1) +00 00 (id = 0, value in data part) + +15 00 00 00 (sizeof numbers) +04 ( sizeof int32 ) +01 00 00 00 (1) +02 00 00 00 (2) +03 00 00 00 (3) +04 00 00 00 (4) +05 00 00 00 (5) +``` + +Example 4: +``` +data { + numbers = { + (1<<32)+1, + (1<<32)+2, + (1<<32)+3, + } +} + +01 00 (fn = 1) +00 00 (id = 0, value in data part) + +19 00 00 00 (sizeof numbers) +08 ( sizeof int64 ) +01 00 00 00 01 00 00 00 ( (1<32) + 1) +02 00 00 00 01 00 00 00 ( (1<32) + 2) +03 00 00 00 01 00 00 00 ( (1<32) + 3) +``` + +Example 5: +``` +data { + bools = { false, true, false } +} + +02 00 (fn = 2) +01 00 (skip id) +00 00 (id = 2, value in data part) + +03 00 00 00 (sizeof bools) +00 (false) +01 (true) +00 (false) ``` 0 Packing @@ -270,7 +352,7 @@ person { The algorithm is very similar to [Cap'n proto](http://kentonv.github.io/capnproto/), but 0x00 is not treated specially. -In packed format, the message if padding to 8. Each 8 byte is reduced to a tag byte followed by zero to eight content bytes. +In packed format, the message is padding to 8. Each 8 byte is reduced to a tag byte followed by zero to eight content bytes. The bits of the tag byte correspond to the bytes of the unpacked word, with the least-significant bit corresponding to the first byte. Each zero bit indicates that the corresponding byte is zero. The non-zero bytes are packed following the tag. From d35388581f0747c10f87bf3eae2d178d5bd0ac80 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 23 Nov 2015 16:12:57 +0800 Subject: [PATCH 528/729] big number in sproto --- lualib-src/sproto/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index 8fcf5e34..c38a3162 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -238,6 +238,8 @@ Notice: If the tag is not declared in schema, the decoder will simply ignore the .Data { numbers 0 : *integer bools 1 : *boolean + number 2 : integer + bignumber 3 : integer } ``` @@ -347,6 +349,25 @@ data { 00 (false) ``` +Example 6: +``` +data { + number = 100000, + bignumber = -1000000000, +} + +03 00 (fn = 3) +03 00 (skip id 0/1) +00 00 (id = 2, value in data part) +00 00 (id = 3, value in data part) + +04 00 00 00 (sizeof number, data part) +A0 86 01 00 (100000, 32bit integer) + +08 00 00 00 (sizeof bignumber, data part) +00 1C F4 AB FD FF FF FF (-10000000000, 64bit integer) +``` + 0 Packing ======= From c180036bed76ae7c9f2145efce8cd02da2bd928d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 25 Nov 2015 13:09:41 +0800 Subject: [PATCH 529/729] use async dns resolve in httpc, and dns resolve cache by ttl. see about issue #253 --- lualib/dns.lua | 97 ++++++++++++++++++++++++++++++++++--------- lualib/http/httpc.lua | 11 +++++ test/testhttp.lua | 1 + 3 files changed, 89 insertions(+), 20 deletions(-) diff --git a/lualib/dns.lua b/lualib/dns.lua index e349699f..d6658d63 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -71,6 +71,7 @@ local MAX_DOMAIN_LEN = 1024 local MAX_LABEL_LEN = 63 local MAX_PACKET_LEN = 2048 local DNS_HEADER_LEN = 12 +local TIMEOUT = 30 * 100 -- 30 seconds local QTYPE = { A = 1, @@ -82,8 +83,18 @@ local QCLASS = { IN = 1, } +local weak = {__mode = "kv"} +local CACHE = {} + local dns = {} +function dns.flush() + CACHE[QTYPE.A] = setmetatable({},weak) + CACHE[QTYPE.AAAA] = setmetatable({},weak) +end + +dns.flush() + local function verify_domain_name(name) if #name > MAX_DOMAIN_LEN then return false @@ -206,33 +217,51 @@ local function resolve(content) -- verify answer assert(answer_header.qdcount == 1, "malformed packet") - local resp = request_pool[answer_header.tid] - if not resp then - skynet.error("Recv an invalid tid when dns query") - return - end - local question,left = unpack_question(content, left) - if question.name ~= resp.name then - skynet.error("Recv an invalid name when dns query") - return - end local ttl local answer - local answers = {} + local answers_ipv4 + local answers_ipv6 + for i=1, answer_header.ancount do answer, left = unpack_answer(content, left) - -- only extract qtype address - if answer.atype == resp.qtype then - local ip = unpack_rdata(resp.qtype, answer.rdata) + local answers + if answer.atype == QTYPE.A then + answers_ipv4 = answers_ipv4 or {} + answers = answers_ipv4 + elseif answer.atype == QTYPE.AAAA then + answers_ipv6 = answers_ipv6 or {} + answers = answers_ipv6 + end + if answers then + local ip = unpack_rdata(answer.atype, answer.rdata) ttl = ttl and math.min(ttl, answer.ttl) or answer.ttl answers[#answers+1] = ip end end - if #answers > 0 then - resp.answers = answers + if answers_ipv4 then + CACHE[QTYPE.A][question.name] = { answers = answers_ipv4, ttl = skynet.now() + ttl * 100 } + end + + if answers_ipv6 then + CACHE[QTYPE.AAAA][question.name] = { answers = answers_ipv6, ttl = skynet.now() + ttl * 100 } + end + + local resp = request_pool[answer_header.tid] + if not resp then + -- the resp may be timeout + return + end + + if question.name ~= resp.name then + skynet.error("Recv an invalid name when dns query") + end + + local r = CACHE[resp.qtype][resp.name] + if r then + resp.answers = r.answers end skynet.wakeup(resp.co) @@ -256,26 +285,54 @@ function dns.server(server, port) return server end +local function lookup_cache(name, qtype, ignorettl) + local result = CACHE[qtype][name] + if result then + if ignorettl or (result.ttl > skynet.now()) then + return result.answers + end + end +end + local function suspend(tid, name, qtype) local req = { name = name, tid = tid, qtype = qtype, - time = skynet.now(), -- for timeout co = coroutine.running(), } request_pool[tid] = req + skynet.fork(function() + skynet.sleep(TIMEOUT) + local req = request_pool[tid] + if req then + -- cancel tid + skynet.error(string.format("DNS query %s timeout", name)) + request_pool[tid] = nil + skynet.wakeup(req.co) + end + end) skynet.wait(req.co) - local answers = request_pool[tid].answers + local answers = req.answers request_pool[tid] = nil - assert(answers, "no ip") - return answers[1], answers + if not req.answers then + local answers = lookup_cache(name, qtype, true) + if answers then + return answers[1], answers + end + error "timeout or no answer" + end + return req.answers[1], req.answers end function dns.resolve(name, ipv6) local qtype = ipv6 and QTYPE.AAAA or QTYPE.A local name = name:lower() assert(verify_domain_name(name) , "illegal name") + local answers = lookup_cache(name, qtype) + if answers then + return answers[1], answers + end local question_header = { tid = gen_tid(), flags = 0x100, -- flags: 00000001 00000000, set RD diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 49ccb9b8..09ac9288 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -1,6 +1,7 @@ local socket = require "http.sockethelper" local url = require "http.url" local internal = require "http.internal" +local dns = require "dns" local string = string local table = table @@ -78,6 +79,13 @@ local function request(fd, method, host, url, recvheader, header, content) return code, body end +local async_dns + +function httpc.dns(server,port) + async_dns = true + dns.server(server,port) +end + function httpc.request(method, host, url, recvheader, header, content) local hostname, port = host:match"([^:]+):?(%d*)$" if port == "" then @@ -85,6 +93,9 @@ function httpc.request(method, host, url, recvheader, header, content) else port = tonumber(port) end + if async_dns and not hostname:match(".*%d+$") then + hostname = dns.resolve(hostname) + end local fd = socket.connect(hostname, port) local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content) socket.close(fd) diff --git a/test/testhttp.lua b/test/testhttp.lua index cba1488a..76a5a4da 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -3,6 +3,7 @@ local httpc = require "http.httpc" local dns = require "dns" skynet.start(function() + httpc.dns() -- set dns server print("GET baidu.com") local respheader = {} local status, body = httpc.get("baidu.com", "/", respheader) From e47a4921faaecbb1e2710e48bcb36c64221ad6a8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 27 Nov 2015 18:13:50 +0800 Subject: [PATCH 530/729] bugfix. remote publish first, see issue #391 --- service/multicastd.lua | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/service/multicastd.lua b/service/multicastd.lua index 2ff1a7f0..3010e2c9 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -66,6 +66,16 @@ end -- publish a message, for local node, use the message pointer (call mc.bind to add the reference) -- for remote node, call remote_publish. (call mc.unpack and skynet.tostring to convert message pointer to string) local function publish(c , source, pack, size) + local remote = channel_remote[c] + if remote then + -- remote publish should unpack the pack, because we should not publish the pointer out. + local _, msg, sz = mc.unpack(pack, size) + local msg = skynet.tostring(msg,sz) + for node in pairs(remote) do + remote_publish(node, c, source, msg) + end + end + local group = channel[c] if group == nil or next(group) == nil then -- dead channel, delete the pack. mc.bind returns the pointer in pack @@ -79,15 +89,6 @@ local function publish(c , source, pack, size) -- the msg is a pointer to the real message, publish pointer in local is ok. skynet.redirect(k, source, "multicast", c , msg) end - local remote = channel_remote[c] - if remote then - -- remote publish should unpack the pack, because we should not publish the pointer out. - local _, msg, sz = mc.unpack(pack, size) - local msg = skynet.tostring(msg,sz) - for node in pairs(remote) do - remote_publish(node, c, source, msg) - end - end end skynet.register_protocol { From 3a527f0a66e146b5421b07ac553304acca09fc14 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 30 Nov 2015 20:31:56 +0800 Subject: [PATCH 531/729] update lua to 5.3.2 --- 3rd/lua/README | 6 +- 3rd/lua/lapi.c | 149 ++++++++++-------- 3rd/lua/lauxlib.c | 90 ++++++++--- 3rd/lua/lauxlib.h | 4 +- 3rd/lua/lbaselib.c | 41 ++--- 3rd/lua/lbitlib.c | 51 ++++--- 3rd/lua/lcode.c | 6 +- 3rd/lua/ldblib.c | 6 +- 3rd/lua/ldebug.c | 8 +- 3rd/lua/ldo.c | 194 ++++++++++++++++------- 3rd/lua/ldo.h | 26 +++- 3rd/lua/ldump.c | 4 +- 3rd/lua/lgc.c | 28 ++-- 3rd/lua/lgc.h | 41 +++-- 3rd/lua/liolib.c | 31 ++-- 3rd/lua/llex.c | 10 +- 3rd/lua/llimits.h | 34 +++-- 3rd/lua/lmathlib.c | 6 +- 3rd/lua/loadlib.c | 4 +- 3rd/lua/lobject.c | 30 ++-- 3rd/lua/lobject.h | 86 +++++------ 3rd/lua/loslib.c | 72 +++++---- 3rd/lua/lparser.c | 11 +- 3rd/lua/lstate.c | 29 ++-- 3rd/lua/lstate.h | 14 +- 3rd/lua/lstring.c | 83 ++++++---- 3rd/lua/lstring.h | 4 +- 3rd/lua/lstrlib.c | 181 +++++++++++++--------- 3rd/lua/ltable.c | 78 ++++++---- 3rd/lua/ltable.h | 7 +- 3rd/lua/ltablib.c | 374 ++++++++++++++++++++++++++++----------------- 3rd/lua/ltm.c | 19 ++- 3rd/lua/lua.c | 20 +-- 3rd/lua/lua.h | 4 +- 3rd/lua/luaconf.h | 65 +++++--- 3rd/lua/lundump.c | 22 +-- 3rd/lua/lundump.h | 5 +- 3rd/lua/lvm.c | 219 ++++++++++++++------------ 3rd/lua/lvm.h | 56 ++++++- 3rd/lua/lzio.c | 12 +- 3rd/lua/lzio.h | 3 +- 41 files changed, 1296 insertions(+), 837 deletions(-) diff --git a/3rd/lua/README b/3rd/lua/README index efb71d70..de361c84 100644 --- a/3rd/lua/README +++ b/3rd/lua/README @@ -1,6 +1,6 @@ -This is a modify version of lua 5.3.1 (http://www.lua.org/ftp/lua-5.3.1.tar.gz) . +This is a modify version of lua 5.3.2 (http://www.lua.org/ftp/lua-5.3.2.tar.gz) . -For detail , +For detail , Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html Shared short string table : http://blog.codingnow.com/2015/08/lua_vm_share_string.html - \ No newline at end of file + Signal for debug use : http://blog.codingnow.com/2015/03/skynet_signal.html diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 3f47f7ee..33062dad 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.249 2015/04/06 12:23:48 roberto Exp $ +** $Id: lapi.c,v 2.257 2015/11/02 18:48:07 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -122,11 +122,11 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { lua_lock(to); api_checknelems(from, n); api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "not enough elements to move"); + api_check(from, to->ci->top - to->top >= n, "stack overflow"); from->top -= n; for (i = 0; i < n; i++) { setobj2s(to, to->top, from->top + i); - api_incr_top(to); + to->top++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); } @@ -472,11 +472,16 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { } +/* +** Pushes on the stack a string with given length. Avoid using 's' when +** 'len' == 0 (as 's' can be NULL in that case), due to later use of +** 'memcmp' and 'memcpy'. +*/ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); luaC_checkGC(L); - ts = luaS_newlstr(L, s, len); + ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top, ts); api_incr_top(L); lua_unlock(L); @@ -580,19 +585,30 @@ LUA_API int lua_pushthread (lua_State *L) { */ -LUA_API int lua_getglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt; /* global table */ - lua_lock(L); - gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top, luaS_new(L, name)); - api_incr_top(L); - luaV_gettable(L, gt, L->top - 1, L->top - 1); +static int auxgetstr (lua_State *L, const TValue *t, const char *k) { + const TValue *aux; + TString *str = luaS_new(L, k); + if (luaV_fastget(L, t, str, aux, luaH_getstr)) { + setobj2s(L, L->top, aux); + api_incr_top(L); + } + else { + setsvalue2s(L, L->top, str); + api_incr_top(L); + luaV_finishget(L, t, L->top - 1, L->top - 1, aux); + } lua_unlock(L); return ttnov(L->top - 1); } +LUA_API int lua_getglobal (lua_State *L, const char *name) { + Table *reg = hvalue(&G(L)->l_registry); + lua_lock(L); + return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); +} + + LUA_API int lua_gettable (lua_State *L, int idx) { StkId t; lua_lock(L); @@ -604,24 +620,25 @@ LUA_API int lua_gettable (lua_State *L, int idx) { LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { - StkId t; lua_lock(L); - t = index2addr(L, idx); - setsvalue2s(L, L->top, luaS_new(L, k)); - api_incr_top(L); - luaV_gettable(L, t, L->top - 1, L->top - 1); - lua_unlock(L); - return ttnov(L->top - 1); + return auxgetstr(L, index2addr(L, idx), k); } LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { StkId t; + const TValue *aux; lua_lock(L); t = index2addr(L, idx); - setivalue(L->top, n); - api_incr_top(L); - luaV_gettable(L, t, L->top - 1, L->top - 1); + if (luaV_fastget(L, t, n, aux, luaH_getint)) { + setobj2s(L, L->top, aux); + api_incr_top(L); + } + else { + setivalue(L->top, n); + api_incr_top(L); + luaV_finishget(L, t, L->top - 1, L->top - 1, aux); + } lua_unlock(L); return ttnov(L->top - 1); } @@ -720,18 +737,29 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) { ** set functions (stack -> Lua) */ +/* +** t[k] = value at the top of the stack (where 'k' is a string) +*/ +static void auxsetstr (lua_State *L, const TValue *t, const char *k) { + const TValue *aux; + TString *str = luaS_new(L, k); + api_checknelems(L, 1); + if (luaV_fastset(L, t, str, aux, luaH_getstr, L->top - 1)) + L->top--; /* pop value */ + else { + setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ + api_incr_top(L); + luaV_finishset(L, t, L->top - 1, L->top - 2, aux); + L->top -= 2; /* pop value and key */ + } + lua_unlock(L); /* lock done by caller */ +} + LUA_API void lua_setglobal (lua_State *L, const char *name) { Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt; /* global table */ - lua_lock(L); - api_checknelems(L, 1); - gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top, luaS_new(L, name)); - api_incr_top(L); - luaV_settable(L, gt, L->top - 1, L->top - 2); - L->top -= 2; /* pop value and key */ - lua_unlock(L); + lua_lock(L); /* unlock done in 'auxsetstr' */ + auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); } @@ -747,42 +775,40 @@ LUA_API void lua_settable (lua_State *L, int idx) { LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { - StkId t; - lua_lock(L); - api_checknelems(L, 1); - t = index2addr(L, idx); - setsvalue2s(L, L->top, luaS_new(L, k)); - api_incr_top(L); - luaV_settable(L, t, L->top - 1, L->top - 2); - L->top -= 2; /* pop value and key */ - lua_unlock(L); + lua_lock(L); /* unlock done in 'auxsetstr' */ + auxsetstr(L, index2addr(L, idx), k); } LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { StkId t; + const TValue *aux; lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); - setivalue(L->top, n); - api_incr_top(L); - luaV_settable(L, t, L->top - 1, L->top - 2); - L->top -= 2; /* pop value and key */ + if (luaV_fastset(L, t, n, aux, luaH_getint, L->top - 1)) + L->top--; /* pop value */ + else { + setivalue(L->top, n); + api_incr_top(L); + luaV_finishset(L, t, L->top - 1, L->top - 2, aux); + L->top -= 2; /* pop value and key */ + } lua_unlock(L); } LUA_API void lua_rawset (lua_State *L, int idx) { StkId o; - Table *t; + TValue *slot; lua_lock(L); api_checknelems(L, 2); o = index2addr(L, idx); api_check(L, ttistable(o), "table expected"); - t = hvalue(o); - setobj2t(L, luaH_set(L, t, L->top-2), L->top-1); - invalidateTMcache(t); - luaC_barrierback(L, t, L->top-1); + slot = luaH_set(L, hvalue(o), L->top - 2); + setobj2t(L, slot, L->top - 1); + invalidateTMcache(hvalue(o)); + luaC_barrierback(L, hvalue(o), L->top-1); L->top -= 2; lua_unlock(L); } @@ -790,14 +816,12 @@ LUA_API void lua_rawset (lua_State *L, int idx) { LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { StkId o; - Table *t; lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); api_check(L, ttistable(o), "table expected"); - t = hvalue(o); - luaH_setint(L, t, n, L->top - 1); - luaC_barrierback(L, t, L->top-1); + luaH_setint(L, hvalue(o), n, L->top - 1); + luaC_barrierback(L, hvalue(o), L->top-1); L->top--; lua_unlock(L); } @@ -805,16 +829,15 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { StkId o; - Table *t; - TValue k; + TValue k, *slot; lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); api_check(L, ttistable(o), "table expected"); - t = hvalue(o); setpvalue(&k, cast(void *, p)); - setobj2t(L, luaH_set(L, t, &k), L->top - 1); - luaC_barrierback(L, t, L->top - 1); + slot = luaH_set(L, hvalue(o), &k); + setobj2t(L, slot, L->top - 1); + luaC_barrierback(L, hvalue(o), L->top - 1); L->top--; lua_unlock(L); } @@ -896,10 +919,10 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.ctx = ctx; /* save context */ - luaD_call(L, func, nresults, 1); /* do the call */ + luaD_call(L, func, nresults); /* do the call */ } else /* no continuation or no yieldable */ - luaD_call(L, func, nresults, 0); /* just do the call */ + luaD_callnoyield(L, func, nresults); /* just do the call */ adjustresults(L, nresults); lua_unlock(L); } @@ -917,7 +940,7 @@ struct CallS { /* data to 'f_call' */ static void f_call (lua_State *L, void *ud) { struct CallS *c = cast(struct CallS *, ud); - luaD_call(L, c->func, c->nresults, 0); + luaD_callnoyield(L, c->func, c->nresults); } @@ -955,7 +978,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, L->errfunc = func; setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ - luaD_call(L, c.func, nresults, 1); /* do the call */ + luaD_call(L, c.func, nresults); /* do the call */ ci->callstatus &= ~CIST_YPCALL; L->errfunc = ci->u.c.old_errfunc; status = LUA_OK; /* if it is here, there were no errors */ @@ -1096,7 +1119,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { } case LUA_GCSTEP: { l_mem debt = 1; /* =1 to signal that it did an actual step */ - int oldrunning = g->gcrunning; + lu_byte oldrunning = g->gcrunning; g->gcrunning = 1; /* allow GC to run */ if (data == 0) { luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index dec81e8a..b92e9527 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.280 2015/02/03 17:38:24 roberto Exp $ +** $Id: lauxlib.c,v 1.284 2015/11/19 19:16:22 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -33,8 +33,8 @@ */ -#define LEVELS1 12 /* size of the first part of the stack */ -#define LEVELS2 10 /* size of the second part of the stack */ +#define LEVELS1 10 /* size of the first part of the stack */ +#define LEVELS2 11 /* size of the second part of the stack */ @@ -107,7 +107,7 @@ static void pushfuncname (lua_State *L, lua_Debug *ar) { } -static int countlevels (lua_State *L) { +static int lastlevel (lua_State *L) { lua_Debug ar; int li = 1, le = 1; /* find an upper bound */ @@ -126,14 +126,16 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level) { lua_Debug ar; int top = lua_gettop(L); - int numlevels = countlevels(L1); - int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0; - if (msg) lua_pushfstring(L, "%s\n", msg); + int last = lastlevel(L1); + int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; + if (msg) + lua_pushfstring(L, "%s\n", msg); + luaL_checkstack(L, 10, NULL); lua_pushliteral(L, "stack traceback:"); while (lua_getstack(L1, level++, &ar)) { - if (level == mark) { /* too many levels? */ + if (n1-- == 0) { /* too many levels? */ lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = numlevels - LEVELS2; /* and skip to last ones */ + level = last - LEVELS2 + 1; /* and skip to last ones */ } else { lua_getinfo(L1, "Slnt", &ar); @@ -289,7 +291,7 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ return 0; /* leave previous value on top, but return 0 */ lua_pop(L, 1); - lua_newtable(L); /* create metatable */ + lua_createtable(L, 0, 2); /* create metatable */ lua_pushstring(L, tname); lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ lua_pushvalue(L, -1); @@ -435,6 +437,47 @@ LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, ** ======================================================= */ +/* userdata to box arbitrary data */ +typedef struct UBox { + void *box; + size_t bsize; +} UBox; + + +static void *resizebox (lua_State *L, int idx, size_t newsize) { + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); + UBox *box = (UBox *)lua_touserdata(L, idx); + void *temp = allocf(ud, box->box, box->bsize, newsize); + if (temp == NULL && newsize > 0) { /* allocation error? */ + resizebox(L, idx, 0); /* free buffer */ + luaL_error(L, "not enough memory for buffer allocation"); + } + box->box = temp; + box->bsize = newsize; + return temp; +} + + +static int boxgc (lua_State *L) { + resizebox(L, 1, 0); + return 0; +} + + +static void *newbox (lua_State *L, size_t newsize) { + UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox)); + box->box = NULL; + box->bsize = 0; + if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */ + lua_pushcfunction(L, boxgc); + lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */ + } + lua_setmetatable(L, -2); + return resizebox(L, -1, newsize); +} + + /* ** check whether buffer is using a userdata on the stack as a temporary ** buffer @@ -455,11 +498,12 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { if (newsize < B->n || newsize - B->n < sz) luaL_error(L, "buffer too large"); /* create larger buffer */ - newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char)); - /* move content to new buffer */ - memcpy(newbuff, B->b, B->n * sizeof(char)); if (buffonstack(B)) - lua_remove(L, -2); /* remove old buffer */ + newbuff = (char *)resizebox(L, -1, newsize); + else { /* no buffer yet */ + newbuff = (char *)newbox(L, newsize); + memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ + } B->b = newbuff; B->size = newsize; } @@ -468,9 +512,11 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { - char *b = luaL_prepbuffsize(B, l); - memcpy(b, s, l * sizeof(char)); - luaL_addsize(B, l); + if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ + char *b = luaL_prepbuffsize(B, l); + memcpy(b, s, l * sizeof(char)); + luaL_addsize(B, l); + } } @@ -482,8 +528,10 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) - lua_remove(L, -2); /* remove old buffer */ + if (buffonstack(B)) { + resizebox(L, -2, 0); /* delete old buffer */ + lua_remove(L, -2); /* remove its header from the stack */ + } } @@ -605,7 +653,7 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ + const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ int c; lf->n = 0; do { @@ -1138,4 +1186,4 @@ LUAMOD_API int luaopen_cache(lua_State *L) { lua_getglobal(L, "loadfile"); lua_setfield(L, -2, "loadfile"); return 1; -} \ No newline at end of file +} diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index 0bac2467..ddb7c228 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp $ +** $Id: lauxlib.h,v 1.129 2015/11/23 11:29:43 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -65,7 +65,7 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); -/* pre-defined references */ +/* predefined references */ #define LUA_NOREF (-2) #define LUA_REFNIL (-1) diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 9a151245..861823d8 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.310 2015/03/28 19:14:47 roberto Exp $ +** $Id: lbaselib.c,v 1.312 2015/10/29 15:21:04 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ @@ -86,8 +86,8 @@ static int luaB_tonumber (lua_State *L) { const char *s; lua_Integer n = 0; /* to avoid warnings */ lua_Integer base = luaL_checkinteger(L, 2); - luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ - s = luaL_checklstring(L, 1, &l); + luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ + s = lua_tolstring(L, 1, &l); luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); if (b_str2int(s, (int)base, &n) == s + l) { lua_pushinteger(L, n); @@ -198,12 +198,10 @@ static int luaB_collectgarbage (lua_State *L) { } -/* -** This function has all type names as upvalues, to maximize performance. -*/ static int luaB_type (lua_State *L) { - luaL_checkany(L, 1); - lua_pushvalue(L, lua_upvalueindex(lua_type(L, 1) + 1)); + int t = lua_type(L, 1); + luaL_argcheck(L, t != LUA_TNONE, 1, "value expected"); + lua_pushstring(L, lua_typename(L, t)); return 1; } @@ -243,18 +241,7 @@ static int luaB_pairs (lua_State *L) { /* -** Traversal function for 'ipairs' for raw tables -*/ -static int ipairsaux_raw (lua_State *L) { - lua_Integer i = luaL_checkinteger(L, 2) + 1; - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushinteger(L, i); - return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2; -} - - -/* -** Traversal function for 'ipairs' for tables with metamethods +** Traversal function for 'ipairs' */ static int ipairsaux (lua_State *L) { lua_Integer i = luaL_checkinteger(L, 2) + 1; @@ -269,13 +256,11 @@ static int ipairsaux (lua_State *L) { ** that can affect the traversal. */ static int luaB_ipairs (lua_State *L) { - lua_CFunction iter = (luaL_getmetafield(L, 1, "__index") != LUA_TNIL) - ? ipairsaux : ipairsaux_raw; #if defined(LUA_COMPAT_IPAIRS) - return pairsmeta(L, "__ipairs", 1, iter); + return pairsmeta(L, "__ipairs", 1, ipairsaux); #else luaL_checkany(L, 1); - lua_pushcfunction(L, iter); /* iteration function */ + lua_pushcfunction(L, ipairsaux); /* iteration function */ lua_pushvalue(L, 1); /* state */ lua_pushinteger(L, 0); /* initial value */ return 3; @@ -490,9 +475,9 @@ static const luaL_Reg base_funcs[] = { {"setmetatable", luaB_setmetatable}, {"tonumber", luaB_tonumber}, {"tostring", luaB_tostring}, + {"type", luaB_type}, {"xpcall", luaB_xpcall}, /* placeholders */ - {"type", NULL}, {"_G", NULL}, {"_VERSION", NULL}, {NULL, NULL} @@ -500,7 +485,6 @@ static const luaL_Reg base_funcs[] = { LUAMOD_API int luaopen_base (lua_State *L) { - int i; /* open lib into global table */ lua_pushglobaltable(L); luaL_setfuncs(L, base_funcs, 0); @@ -510,11 +494,6 @@ LUAMOD_API int luaopen_base (lua_State *L) { /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); lua_setfield(L, -2, "_VERSION"); - /* set function 'type' with proper upvalues */ - for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */ - lua_pushstring(L, lua_typename(L, i)); - lua_pushcclosure(L, luaB_type, LUA_NUMTAGS); - lua_setfield(L, -2, "type"); return 1; } diff --git a/3rd/lua/lbitlib.c b/3rd/lua/lbitlib.c index 15d5f0cd..1cb1d5b9 100644 --- a/3rd/lua/lbitlib.c +++ b/3rd/lua/lbitlib.c @@ -1,5 +1,5 @@ /* -** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp $ +** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp $ ** Standard library for bitwise operations ** See Copyright Notice in lua.h */ @@ -19,6 +19,10 @@ #if defined(LUA_COMPAT_BITLIB) /* { */ +#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) +#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) + + /* number of bits to consider in a number */ #if !defined(LUA_NBITS) #define LUA_NBITS 32 @@ -46,14 +50,14 @@ static lua_Unsigned andaux (lua_State *L) { int i, n = lua_gettop(L); lua_Unsigned r = ~(lua_Unsigned)0; for (i = 1; i <= n; i++) - r &= luaL_checkunsigned(L, i); + r &= checkunsigned(L, i); return trim(r); } static int b_and (lua_State *L) { lua_Unsigned r = andaux(L); - lua_pushunsigned(L, r); + pushunsigned(L, r); return 1; } @@ -69,8 +73,8 @@ static int b_or (lua_State *L) { int i, n = lua_gettop(L); lua_Unsigned r = 0; for (i = 1; i <= n; i++) - r |= luaL_checkunsigned(L, i); - lua_pushunsigned(L, trim(r)); + r |= checkunsigned(L, i); + pushunsigned(L, trim(r)); return 1; } @@ -79,15 +83,15 @@ static int b_xor (lua_State *L) { int i, n = lua_gettop(L); lua_Unsigned r = 0; for (i = 1; i <= n; i++) - r ^= luaL_checkunsigned(L, i); - lua_pushunsigned(L, trim(r)); + r ^= checkunsigned(L, i); + pushunsigned(L, trim(r)); return 1; } static int b_not (lua_State *L) { - lua_Unsigned r = ~luaL_checkunsigned(L, 1); - lua_pushunsigned(L, trim(r)); + lua_Unsigned r = ~checkunsigned(L, 1); + pushunsigned(L, trim(r)); return 1; } @@ -104,23 +108,23 @@ static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { else r <<= i; r = trim(r); } - lua_pushunsigned(L, r); + pushunsigned(L, r); return 1; } static int b_lshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2)); + return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); } static int b_rshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2)); + return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); } static int b_arshift (lua_State *L) { - lua_Unsigned r = luaL_checkunsigned(L, 1); + lua_Unsigned r = checkunsigned(L, 1); lua_Integer i = luaL_checkinteger(L, 2); if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) return b_shift(L, r, -i); @@ -128,19 +132,19 @@ static int b_arshift (lua_State *L) { if (i >= LUA_NBITS) r = ALLONES; else r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ - lua_pushunsigned(L, r); + pushunsigned(L, r); return 1; } } static int b_rot (lua_State *L, lua_Integer d) { - lua_Unsigned r = luaL_checkunsigned(L, 1); + lua_Unsigned r = checkunsigned(L, 1); int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ r = trim(r); if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ r = (r << i) | (r >> (LUA_NBITS - i)); - lua_pushunsigned(L, trim(r)); + pushunsigned(L, trim(r)); return 1; } @@ -175,23 +179,22 @@ static int fieldargs (lua_State *L, int farg, int *width) { static int b_extract (lua_State *L) { int w; - lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); + lua_Unsigned r = trim(checkunsigned(L, 1)); int f = fieldargs(L, 2, &w); r = (r >> f) & mask(w); - lua_pushunsigned(L, r); + pushunsigned(L, r); return 1; } static int b_replace (lua_State *L) { int w; - lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); - lua_Unsigned v = luaL_checkunsigned(L, 2); + lua_Unsigned r = trim(checkunsigned(L, 1)); + lua_Unsigned v = trim(checkunsigned(L, 2)); int f = fieldargs(L, 3, &w); - int m = mask(w); - v &= m; /* erase bits outside given width */ - r = (r & ~(m << f)) | (v << f); - lua_pushunsigned(L, r); + lua_Unsigned m = mask(w); + r = (r & ~(m << f)) | ((v & m) << f); + pushunsigned(L, r); return 1; } diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 00043462..d8676e21 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.101 2015/04/29 18:24:11 roberto Exp $ +** $Id: lcode.c,v 2.103 2015/11/19 19:16:22 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -37,7 +37,7 @@ static int tonumeral(expdesc *e, TValue *v) { - if (e->t != NO_JUMP || e->f != NO_JUMP) + if (hasjumps(e)) return 0; /* not a numeral */ switch (e->k) { case VKINT: @@ -816,7 +816,7 @@ static void codeexpval (FuncState *fs, OpCode op, freeexp(fs, e1); } e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocable */ + e1->k = VRELOCABLE; /* all those operations are relocatable */ luaK_fixline(fs, line); } } diff --git a/3rd/lua/ldblib.c b/3rd/lua/ldblib.c index 91514584..786f6cd9 100644 --- a/3rd/lua/ldblib.c +++ b/3rd/lua/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.149 2015/02/19 17:06:21 roberto Exp $ +** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -28,8 +28,8 @@ static const int HOOKKEY = 0; /* -** If L1 != L, L1 can be in any state, and therefore there is no -** garanties about its stack space; any push in L1 must be +** If L1 != L, L1 can be in any state, and therefore there are no +** guarantees about its stack space; any push in L1 must be ** checked. */ static void checkstack (lua_State *L, lua_State *L1, int n) { diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 2b444fbf..a75bceec 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.115 2015/05/22 17:45:56 roberto Exp $ +** $Id: ldebug.c,v 2.117 2015/11/02 18:48:07 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -618,7 +618,7 @@ l_noret luaG_errormsg (lua_State *L) { setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ L->top++; /* assume EXTRA_STACK */ - luaD_call(L, L->top - 2, 1, 0); /* call it */ + luaD_callnoyield(L, L->top - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); } @@ -640,9 +640,11 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { void luaG_traceexec (lua_State *L) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; - int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0); + int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); if (counthook) resethookcount(L); /* reset count */ + else if (!(mask & LUA_MASKLINE)) + return; /* no line hook and count != 0; nothing to be done */ if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return; /* do not call hook again (VM yielded, so it did not move) */ diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index 825f8c20..a141c2fd 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.138 2015/05/22 17:48:19 roberto Exp $ +** $Id: ldo.c,v 2.150 2015/11/19 19:16:22 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -150,6 +150,11 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { /* }====================================================== */ +/* +** {================================================================== +** Stack reallocation +** =================================================================== +*/ static void correctstack (lua_State *L, TValue *oldstack) { CallInfo *ci; UpVal *up; @@ -221,14 +226,22 @@ void luaD_shrinkstack (lua_State *L) { luaE_freeCI(L); /* free all CIs (list grew because of an error) */ else luaE_shrinkCI(L); /* shrink list */ - if (inuse > LUAI_MAXSTACK || /* still handling stack overflow? */ - goodsize >= L->stacksize) /* would grow instead of shrink? */ - condmovestack(L); /* don't change stack (change only for debugging) */ - else + if (inuse <= LUAI_MAXSTACK && /* not handling stack overflow? */ + goodsize < L->stacksize) /* trying to shrink? */ luaD_reallocstack(L, goodsize); /* shrink it */ + else + condmovestack(L,,); /* don't change stack (change only for debugging) */ } +void luaD_inctop (lua_State *L) { + luaD_checkstack(L, 1); + L->top++; +} + +/* }================================================================== */ + + void luaD_hook (lua_State *L, int event, int line) { lua_Hook hook = L->hook; if (hook && L->allowhook) { @@ -273,15 +286,15 @@ static StkId adjust_varargs (lua_State *L, SharedProto *p, int actual) { int i; int nfixargs = p->numparams; StkId base, fixed; - lua_assert(actual >= nfixargs); /* move fixed parameters to final position */ - luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ fixed = L->top - actual; /* first fixed argument */ base = L->top; /* final position of first argument */ - for (i=0; itop++, fixed + i); - setnilvalue(fixed + i); + setnilvalue(fixed + i); /* erase original copy (for GC) */ } + for (; i < nfixargs; i++) + setnilvalue(L->top++); /* complete missing arguments */ return base; } @@ -308,26 +321,36 @@ static void tryfuncTM (lua_State *L, StkId func) { #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) +/* macro to check stack size, preserving 'p' */ +#define checkstackp(L,n,p) \ + luaD_checkstackaux(L, n, \ + ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ + luaC_checkGC(L), /* stack grow uses memory */ \ + p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ + + /* -** returns true if function has been executed (C function) +** Prepares a function call: checks the stack, creates a new CallInfo +** entry, fills in the relevant information, calls hook if needed. +** If function is a C function, does the call, too. (Otherwise, leave +** the execution ('luaV_execute') to the caller, to allow stackless +** calls.) Returns true iff function has been executed (C function). */ int luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; CallInfo *ci; - int n; /* number of arguments (Lua) or returns (C) */ - ptrdiff_t funcr = savestack(L, func); switch (ttype(func)) { + case LUA_TCCL: /* C closure */ + f = clCvalue(func)->f; + goto Cfunc; case LUA_TLCF: /* light C function */ f = fvalue(func); - goto Cfunc; - case LUA_TCCL: { /* C closure */ - f = clCvalue(func)->f; - Cfunc: - luaC_checkGC(L); /* stack grow uses memory */ - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ + Cfunc: { + int n; /* number of returns */ + checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; - ci->func = restorestack(L, funcr); + ci->func = func; ci->top = L->top + LUA_MINSTACK; lua_assert(ci->top <= L->stack_last); ci->callstatus = 0; @@ -337,41 +360,36 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { n = (*f)(L); /* do the actual call */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, L->top - n, n); + luaD_poscall(L, ci, L->top - n, n); return 1; } case LUA_TLCL: { /* Lua function: prepare its call */ StkId base; SharedProto *p = clLvalue(func)->p->sp; - n = cast_int(L->top - func) - 1; /* number of real arguments */ - luaC_checkGC(L); /* stack grow uses memory */ - luaD_checkstack(L, p->maxstacksize); - for (; n < p->numparams; n++) - setnilvalue(L->top++); /* complete missing arguments */ - if (!p->is_vararg) { - func = restorestack(L, funcr); + int n = cast_int(L->top - func) - 1; /* number of real arguments */ + int fsize = p->maxstacksize; /* frame size */ + checkstackp(L, fsize, func); + if (p->is_vararg != 1) { /* do not use vararg? */ + for (; n < p->numparams; n++) + setnilvalue(L->top++); /* complete missing arguments */ base = func + 1; } - else { + else base = adjust_varargs(L, p, n); - func = restorestack(L, funcr); /* previous call can change stack */ - } ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; ci->func = func; ci->u.l.base = base; - ci->top = base + p->maxstacksize; + L->top = ci->top = base + fsize; lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = CIST_LUA; - L->top = ci->top; if (L->hookmask & LUA_MASKCALL) callhook(L, ci); return 0; } default: { /* not a function */ - luaD_checkstack(L, 1); /* ensure space for metamethod */ - func = restorestack(L, funcr); /* previous call may change stack */ + checkstackp(L, 1, func); /* ensure space for metamethod */ tryfuncTM(L, func); /* try to get '__call' metamethod */ return luaD_precall(L, func, nresults); /* now it must be a function */ } @@ -379,10 +397,57 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { } -int luaD_poscall (lua_State *L, StkId firstResult, int nres) { +/* +** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. +** Handle most typical cases (zero results for commands, one result for +** expressions, multiple results for tail calls/single parameters) +** separated. +*/ +static int moveresults (lua_State *L, const TValue *firstResult, StkId res, + int nres, int wanted) { + switch (wanted) { /* handle typical cases separately */ + case 0: break; /* nothing to move */ + case 1: { /* one result needed */ + if (nres == 0) /* no results? */ + firstResult = luaO_nilobject; /* adjust with nil */ + setobjs2s(L, res, firstResult); /* move it to proper place */ + break; + } + case LUA_MULTRET: { + int i; + for (i = 0; i < nres; i++) /* move all results to correct place */ + setobjs2s(L, res + i, firstResult + i); + L->top = res + nres; + return 0; /* wanted == LUA_MULTRET */ + } + default: { + int i; + if (wanted <= nres) { /* enough results? */ + for (i = 0; i < wanted; i++) /* move wanted results to correct place */ + setobjs2s(L, res + i, firstResult + i); + } + else { /* not enough results; use all of them plus nils */ + for (i = 0; i < nres; i++) /* move all results to correct place */ + setobjs2s(L, res + i, firstResult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(res + i); + } + break; + } + } + L->top = res + wanted; /* top points after the last result */ + return 1; +} + + +/* +** Finishes a function call: calls hook if necessary, removes CallInfo, +** moves current number of results to proper place; returns 0 iff call +** wanted multiple (variable number of) results. +*/ +int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { StkId res; - int wanted, i; - CallInfo *ci = L->ci; + int wanted = ci->nresults; if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { if (L->hookmask & LUA_MASKRET) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ @@ -392,15 +457,24 @@ int luaD_poscall (lua_State *L, StkId firstResult, int nres) { L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ } res = ci->func; /* res == final position of 1st result */ - wanted = ci->nresults; L->ci = ci->previous; /* back to caller */ - /* move results to correct place */ - for (i = wanted; i != 0 && nres-- > 0; i--) - setobjs2s(L, res++, firstResult++); - while (i-- > 0) - setnilvalue(res++); - L->top = res; - return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ + /* move results to proper place */ + return moveresults(L, firstResult, res, nres, wanted); +} + + +/* +** Check appropriate error for stack overflow ("regular" overflow or +** overflow while handling stack overflow). If 'nCalls' is larger than +** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but +** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to +** allow overflow handling to work) +*/ +static void stackerror (lua_State *L) { + if (L->nCcalls == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ } @@ -410,21 +484,25 @@ int luaD_poscall (lua_State *L, StkId firstResult, int nres) { ** When returns, all the results are on the stack, starting at the original ** function position. */ -void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { - if (++L->nCcalls >= LUAI_MAXCCALLS) { - if (L->nCcalls == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ - } - if (!allowyield) L->nny++; +void luaD_call (lua_State *L, StkId func, int nResults) { + if (++L->nCcalls >= LUAI_MAXCCALLS) + stackerror(L); if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ luaV_execute(L); /* call it */ - if (!allowyield) L->nny--; L->nCcalls--; } +/* +** Similar to 'luaD_call', but does not allow yields during the call +*/ +void luaD_callnoyield (lua_State *L, StkId func, int nResults) { + L->nny++; + luaD_call(L, func, nResults); + L->nny--; +} + + /* ** Completes the execution of an interrupted C function, calling its ** continuation function. @@ -449,7 +527,7 @@ static void finishCcall (lua_State *L, int status) { lua_lock(L); api_checknelems(L, n); /* finish 'luaD_precall' */ - luaD_poscall(L, L->top - n, n); + luaD_poscall(L, ci, L->top - n, n); } @@ -560,7 +638,7 @@ static void resume (lua_State *L, void *ud) { api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ } - luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ } unroll(L, NULL); /* run continuation */ } @@ -570,7 +648,7 @@ static void resume (lua_State *L, void *ud) { LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { int status; - int oldnny = L->nny; /* save "number of non-yieldable" calls */ + unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); luai_userstateresume(L, nargs); L->nCcalls = (from) ? from->nCcalls + 1 : 1; @@ -684,7 +762,7 @@ static void f_parser (lua_State *L, void *ud) { int c = zgetc(p->z); /* read first character */ if (c == LUA_SIGNATURE[0]) { checkmode(L, p->mode, "binary"); - cl = luaU_undump(L, p->z, &p->buff, p->name); + cl = luaU_undump(L, p->z, p->name); } else { checkmode(L, p->mode, "text"); diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index edade657..80582dc2 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.22 2015/05/22 17:48:19 roberto Exp $ +** $Id: ldo.h,v 2.28 2015/11/23 11:29:43 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -13,11 +13,21 @@ #include "lzio.h" -#define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ - luaD_growstack(L, n); else condmovestack(L); +/* +** Macro to check stack size and grow stack if needed. Parameters +** 'pre'/'pos' allow the macro to preserve a pointer into the +** stack across reallocations, doing the work only when needed. +** 'condmovestack' is used in heavy tests to force a stack reallocation +** at every check. +*/ +#define luaD_checkstackaux(L,n,pre,pos) \ + if (L->stack_last - L->top <= (n)) \ + { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } + +/* In general, 'pre'/'pos' are empty (nothing to save) */ +#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,,) -#define incr_top(L) {L->top++; luaD_checkstack(L,0);} #define savestack(L,p) ((char *)(p) - (char *)L->stack) #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) @@ -30,14 +40,16 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); -LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, - int allowyield); +LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); +LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres); +LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, + int nres); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_growstack (lua_State *L, int n); LUAI_FUNC void luaD_shrinkstack (lua_State *L); +LUAI_FUNC void luaD_inctop (lua_State *L); LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); diff --git a/3rd/lua/ldump.c b/3rd/lua/ldump.c index bcdf9b9b..dc100ca1 100644 --- a/3rd/lua/ldump.c +++ b/3rd/lua/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.36 2015/03/30 15:43:51 roberto Exp $ +** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -38,7 +38,7 @@ typedef struct { static void DumpBlock (const void *b, size_t size, DumpState *D) { - if (D->status == 0) { + if (D->status == 0 && size > 0) { lua_unlock(D->L); D->status = (*D->writer)(D->L, b, size, D->data); lua_lock(D->L); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index a1ce0245..3925f2ca 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.205 2015/03/25 13:42:19 roberto Exp $ +** $Id: lgc.c,v 2.210 2015/11/03 18:10:44 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -114,8 +114,13 @@ static void reallymarkobject (global_State *g, GCObject *o); /* -** if key is not marked, mark its entry as dead (therefore removing it -** from the table) +** If key is not marked, mark its entry as dead. This allows key to be +** collected, but keeps its entry in the table. A dead node is needed +** when Lua looks up for a key (it may be part of a chain) and when +** traversing a weak table (key might be removed from the table during +** traversal). Other places never manipulate dead keys, because its +** associated nil value is enough to signal that the entry is logically +** empty. */ static void removeentry (Node *n) { lua_assert(ttisnil(gval(n))); @@ -549,7 +554,8 @@ static lu_mem traversethread (global_State *g, lua_State *th) { } else if (g->gckind != KGC_EMERGENCY) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return (sizeof(lua_State) + sizeof(TValue) * th->stacksize); + return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + + sizeof(CallInfo) * th->nci); } @@ -776,12 +782,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { */ /* -** If possible, free concatenation buffer and shrink string table +** If possible, shrink string table */ static void checkSizes (lua_State *L, global_State *g) { if (g->gckind != KGC_EMERGENCY) { l_mem olddebt = g->GCdebt; - luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ luaS_resize(L, g->strt.size / 2); /* shrink it a little */ g->GCestimate += g->GCdebt - olddebt; /* update estimate */ @@ -804,7 +809,7 @@ static GCObject *udata2finalize (global_State *g) { static void dothecall (lua_State *L, void *ud) { UNUSED(ud); - luaD_call(L, L->top - 2, 0, 0); + luaD_callnoyield(L, L->top - 2, 0); } @@ -1121,9 +1126,12 @@ void luaC_runtilstate (lua_State *L, int statesmask) { static l_mem getdebt (global_State *g) { l_mem debt = g->GCdebt; int stepmul = g->gcstepmul; - debt = (debt / STEPMULADJ) + 1; - debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; - return debt; + if (debt <= 0) return 0; /* minimal debt */ + else { + debt = (debt / STEPMULADJ) + 1; + debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; + return debt; + } } /* diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 0eedf842..1775ca45 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 roberto Exp $ +** $Id: lgc.h,v 2.90 2015/10/21 18:15:15 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -101,26 +101,35 @@ #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) -#define luaC_condGC(L,c) \ - {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} -#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) +/* +** Does one step of collection when debt becomes positive. 'pre'/'pos' +** allows some adjustments to be done only when needed. macro +** 'condchangemem' is used only for heavy tests (forcing a full +** GC cycle on every opportunity) +*/ +#define luaC_condGC(L,pre,pos) \ + { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ + condchangemem(L,pre,pos); } + +/* more often than not, 'pre'/'pos' are empty */ +#define luaC_checkGC(L) luaC_condGC(L,,) -#define luaC_barrier(L,p,v) { \ - if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)); } +#define luaC_barrier(L,p,v) ( \ + (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ + luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) -#define luaC_barrierback(L,p,v) { \ - if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ - luaC_barrierback_(L,p); } +#define luaC_barrierback(L,p,v) ( \ + (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ + luaC_barrierback_(L,p) : cast_void(0)) -#define luaC_objbarrier(L,p,o) { \ - if (isblack(p) && iswhite(o)) \ - luaC_barrier_(L,obj2gco(p),obj2gco(o)); } +#define luaC_objbarrier(L,p,o) ( \ + (isblack(p) && iswhite(o)) ? \ + luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) -#define luaC_upvalbarrier(L,uv) \ - { if (iscollectable((uv)->v) && !upisopen(uv)) \ - luaC_upvalbarrier_(L,uv); } +#define luaC_upvalbarrier(L,uv) ( \ + (iscollectable((uv)->v) && !upisopen(uv)) ? \ + luaC_upvalbarrier_(L,uv) : cast_void(0)) LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index 193cac67..a91ba391 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.144 2015/04/03 18:41:57 roberto Exp $ +** $Id: liolib.c,v 2.148 2015/11/23 11:36:11 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -23,18 +23,24 @@ #include "lualib.h" -#if !defined(l_checkmode) + /* -** Check whether 'mode' matches '[rwa]%+?b?'. ** Change this macro to accept other modes for 'fopen' besides ** the standard ones. */ +#if !defined(l_checkmode) + +/* accepted extensions to 'mode' in 'fopen' */ +#if !defined(L_MODEEXT) +#define L_MODEEXT "b" +#endif + +/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ #define l_checkmode(mode) \ (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \ - (*mode != '+' || ++mode) && /* skip if char is '+' */ \ - (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \ - (*mode == '\0')) + (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ \ + (strspn(mode, L_MODEEXT) == strlen(mode))) #endif @@ -176,7 +182,7 @@ static FILE *tofile (lua_State *L) { /* ** When creating file handles, always creates a 'closed' file handle ** before opening the actual file; so, if there is a memory error, the -** file is not left opened. +** handle is in a consistent state. */ static LStream *newprefile (lua_State *L) { LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); @@ -318,8 +324,15 @@ static int io_output (lua_State *L) { static int io_readline (lua_State *L); +/* +** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit +** in the limit for upvalues of a closure) +*/ +#define MAXARGLINE 250 + static void aux_lines (lua_State *L, int toclose) { int n = lua_gettop(L) - 1; /* number of arguments to read */ + luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); lua_pushinteger(L, n); /* number of arguments to read */ lua_pushboolean(L, toclose); /* close/not close file when finished */ lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ @@ -462,7 +475,7 @@ static int read_line (lua_State *L, FILE *f, int chop) { int c = '\0'; luaL_buffinit(L, &b); while (c != EOF && c != '\n') { /* repeat until end of line */ - char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */ + char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ int i = 0; l_lockfile(f); /* no memory errors can happen inside the lock */ while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') @@ -483,7 +496,7 @@ static void read_all (lua_State *L, FILE *f) { luaL_Buffer b; luaL_buffinit(L, &b); do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ - char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE); + char *p = luaL_prepbuffer(&b); nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); luaL_addsize(&b, nr); } while (nr == LUAL_BUFFERSIZE); diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index c35bd55f..16ea3ebe 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.93 2015/05/22 17:45:56 roberto Exp $ +** $Id: llex.c,v 2.95 2015/11/19 19:16:22 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -220,8 +220,6 @@ static void buffreplace (LexState *ls, char from, char to) { } -#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0) - /* ** in case of format error, try to change decimal point separator to ** the one defined in the current locale and check again @@ -230,7 +228,7 @@ static void trydecpoint (LexState *ls, TValue *o) { char old = ls->decpoint; ls->decpoint = lua_getlocaledecpoint(); buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ - if (!buff2num(ls->buff, o)) { + if (luaO_str2num(luaZ_buffer(ls->buff), o) == 0) { /* format error with correct decimal point: no more options */ buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ lexerror(ls, "malformed number", TK_FLT); @@ -262,7 +260,7 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { } save(ls, '\0'); buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!buff2num(ls->buff, &obj)) /* format error? */ + if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ trydecpoint(ls, &obj); /* try to update decimal point separator */ if (ttisinteger(&obj)) { seminfo->i = ivalue(&obj); @@ -277,7 +275,7 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return +** skip a sequence '[=*[' or ']=*]'; if sequence is well formed, return ** its number of '='s; otherwise, return a negative number (-1 iff there ** are no '='s after initial bracket) */ diff --git a/3rd/lua/llimits.h b/3rd/lua/llimits.h index 277c724d..f21377fe 100644 --- a/3rd/lua/llimits.h +++ b/3rd/lua/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.135 2015/06/09 14:21:00 roberto Exp $ +** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -64,7 +64,13 @@ typedef unsigned char lu_byte; #if defined(LUAI_USER_ALIGNMENT_T) typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; #else -typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign; +typedef union { + lua_Number n; + double u; + void *s; + lua_Integer i; + long l; +} L_Umaxalign; #endif @@ -78,7 +84,7 @@ typedef LUAI_UACINT l_uacInt; #if defined(lua_assert) #define check_exp(c,e) (lua_assert(c), (e)) /* to avoid problems with conditions too long */ -#define lua_longassert(c) { if (!(c)) lua_assert(0); } +#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) #else #define lua_assert(c) ((void)0) #define check_exp(c,e) (e) @@ -184,10 +190,13 @@ typedef unsigned long Instruction; /* -** Size of cache for strings in the API (better be a prime) +** Size of cache for strings in the API. 'N' is the number of +** sets (better be a prime) and "M" is the size of each set (M == 1 +** makes a direct cache.) */ -#if !defined(STRCACHE_SIZE) -#define STRCACHE_SIZE 127 +#if !defined(STRCACHE_N) +#define STRCACHE_N 53 +#define STRCACHE_M 2 #endif @@ -198,7 +207,7 @@ typedef unsigned long Instruction; /* -** macros that are executed whenether program enters the Lua core +** macros that are executed whenever program enters the Lua core ** ('lua_lock') and leaves the core ('lua_unlock') */ #if !defined(lua_lock) @@ -297,17 +306,18 @@ typedef unsigned long Instruction; ** macro to control inclusion of some hard tests on stack reallocation */ #if !defined(HARDSTACKTESTS) -#define condmovestack(L) ((void)0) +#define condmovestack(L,pre,pos) ((void)0) #else /* realloc stack keeping its size */ -#define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) +#define condmovestack(L,pre,pos) \ + { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; } #endif #if !defined(HARDMEMTESTS) -#define condchangemem(L) condmovestack(L) +#define condchangemem(L,pre,pos) ((void)0) #else -#define condchangemem(L) \ - ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) +#define condchangemem(L,pre,pos) \ + { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } #endif #endif diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index 4f2ec60a..94815f12 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.115 2015/03/12 14:04:04 roberto Exp $ +** $Id: lmathlib.c,v 1.117 2015/10/02 15:39:23 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -39,7 +39,7 @@ static int math_abs (lua_State *L) { if (lua_isinteger(L, 1)) { lua_Integer n = lua_tointeger(L, 1); - if (n < 0) n = (lua_Integer)(0u - n); + if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n); lua_pushinteger(L, n); } else @@ -273,7 +273,7 @@ static int math_random (lua_State *L) { static int math_randomseed (lua_State *L) { l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); - (void)rand(); /* discard first value to avoid undesirable correlations */ + (void)l_rand(); /* discard first value to avoid undesirable correlations */ return 0; } diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index bbf8f67a..79119287 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.126 2015/02/16 13:14:33 roberto Exp $ +** $Id: loadlib.c,v 1.127 2015/11/23 11:30:45 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -732,7 +732,7 @@ static void createsearcherstable (lua_State *L) { int i; /* create 'searchers' table */ lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); - /* fill it with pre-defined searchers */ + /* fill it with predefined searchers */ for (i=0; searchers[i] != NULL; i++) { lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ lua_pushcclosure(L, searchers[i], 1); diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 6c53b981..e24723fe 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp $ +** $Id: lobject.c,v 2.108 2015/11/02 16:09:30 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -55,9 +55,7 @@ int luaO_int2fb (unsigned int x) { /* converts back */ int luaO_fb2int (int x) { - int e = (x >> 3) & 0x1f; - if (e == 0) return x; - else return ((x & 7) + 8) << (e - 1); + return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1); } @@ -333,9 +331,9 @@ void luaO_tostring (lua_State *L, StkId obj) { size_t len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) - len = lua_integer2str(buff, ivalue(obj)); + len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); else { - len = lua_number2str(buff, fltvalue(obj)); + len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); #if !defined(LUA_COMPAT_FLOATSTRING) if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ buff[len++] = lua_getlocaledecpoint(); @@ -348,7 +346,8 @@ void luaO_tostring (lua_State *L, StkId obj) { static void pushstr (lua_State *L, const char *str, size_t l) { - setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); + setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); + luaD_inctop(L); } @@ -359,7 +358,6 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { for (;;) { const char *e = strchr(fmt, '%'); if (e == NULL) break; - luaD_checkstack(L, 2); /* fmt + item */ pushstr(L, fmt, e - fmt); switch (*(e+1)) { case 's': { @@ -377,23 +375,23 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'd': { - setivalue(L->top++, va_arg(argp, int)); - luaO_tostring(L, L->top - 1); - break; + setivalue(L->top, va_arg(argp, int)); + goto top2str; } case 'I': { - setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt))); - luaO_tostring(L, L->top - 1); - break; + setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); + goto top2str; } case 'f': { - setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); + setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); + top2str: + luaD_inctop(L); luaO_tostring(L, L->top - 1); break; } case 'p': { char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ - int l = sprintf(buff, "%p", va_arg(argp, void *)); + int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *)); pushstr(L, buff, l); break; } diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index ccb778db..7521b71e 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.111 2015/06/09 14:21:42 roberto Exp $ +** $Id: lobject.h,v 2.116 2015/11/03 18:33:10 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -19,8 +19,8 @@ /* ** Extra tags for non-values */ -#define LUA_TPROTO LUA_NUMTAGS -#define LUA_TDEADKEY (LUA_NUMTAGS+1) +#define LUA_TPROTO LUA_NUMTAGS /* function prototypes */ +#define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */ /* ** number of all possible tags (including LUA_TNONE but excluding DEADKEY) @@ -88,22 +88,32 @@ struct GCObject { -/* -** Union of all Lua values -*/ -typedef union Value Value; - - - /* ** Tagged Values. This is the basic representation of values in Lua, ** an actual value plus a tag with its type. */ +/* +** Union of all Lua values +*/ +typedef union Value { + GCObject *gc; /* collectable objects */ + void *p; /* light userdata */ + int b; /* booleans */ + lua_CFunction f; /* light C functions */ + lua_Integer i; /* integer numbers */ + lua_Number n; /* float numbers */ +} Value; + + #define TValuefields Value value_; int tt_ -typedef struct lua_TValue TValue; + +typedef struct lua_TValue { + TValuefields; +} TValue; + /* macro defining a nil value */ @@ -177,9 +187,9 @@ typedef struct lua_TValue TValue; /* Macros for internal tests */ #define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) -#define checkliveness(g,obj) \ +#define checkliveness(L,obj) \ lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && !isdead(g,gcvalue(obj)))) + (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) /* Macros to set values */ @@ -215,32 +225,32 @@ typedef struct lua_TValue TValue; #define setsvalue(L,obj,x) \ { TValue *io = (obj); TString *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ - checkliveness(G(L),io); } + checkliveness(L,io); } #define setuvalue(L,obj,x) \ { TValue *io = (obj); Udata *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ - checkliveness(G(L),io); } + checkliveness(L,io); } #define setthvalue(L,obj,x) \ { TValue *io = (obj); lua_State *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ - checkliveness(G(L),io); } + checkliveness(L,io); } #define setclLvalue(L,obj,x) \ { TValue *io = (obj); LClosure *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ - checkliveness(G(L),io); } + checkliveness(L,io); } #define setclCvalue(L,obj,x) \ { TValue *io = (obj); CClosure *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ - checkliveness(G(L),io); } + checkliveness(L,io); } #define sethvalue(L,obj,x) \ { TValue *io = (obj); Table *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ - checkliveness(G(L),io); } + checkliveness(L,io); } #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) @@ -248,7 +258,7 @@ typedef struct lua_TValue TValue; #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); *io1 = *(obj2); \ - (void)L; checkliveness(G(L),io1); } + (void)L; checkliveness(L,io1); } /* @@ -264,12 +274,13 @@ typedef struct lua_TValue TValue; #define setptvalue2s setptvalue /* from table to same table */ #define setobjt2t setobj -/* to table */ -#define setobj2t setobj /* to new object */ #define setobj2n setobj #define setsvalue2n setsvalue +/* to table (define it as an expression to be used in macros) */ +#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1))) + @@ -280,21 +291,6 @@ typedef struct lua_TValue TValue; */ -union Value { - GCObject *gc; /* collectable objects */ - void *p; /* light userdata */ - int b; /* booleans */ - lua_CFunction f; /* light C functions */ - lua_Integer i; /* integer numbers */ - lua_Number n; /* float numbers */ -}; - - -struct lua_TValue { - TValuefields; -}; - - typedef TValue *StkId; /* index to stack elements */ @@ -329,9 +325,9 @@ typedef union UTString { ** Get the actual string (array of bytes) from a 'TString'. ** (Access to 'extra' ensures that value is really a 'TString'.) */ -#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString)) #define getstr(ts) \ - check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts))) + check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString)) + /* get the actual string (array of bytes) from a Lua value */ #define svalue(o) getstr(tsvalue(o)) @@ -375,13 +371,13 @@ typedef union UUdata { #define setuservalue(L,u,o) \ { const TValue *io=(o); Udata *iu = (u); \ iu->user_ = io->value_; iu->ttuv_ = rttype(io); \ - checkliveness(G(L),io); } + checkliveness(L,io); } #define getuservalue(L,u,o) \ { TValue *io=(o); const Udata *iu = (u); \ io->value_ = iu->user_; settt_(io, iu->ttuv_); \ - checkliveness(G(L),io); } + checkliveness(L,io); } /* @@ -406,7 +402,7 @@ typedef struct LocVar { typedef struct SharedProto { lu_byte numparams; /* number of fixed parameters */ - lu_byte is_vararg; + lu_byte is_vararg; /* 2: declared vararg; 1: uses vararg */ lu_byte maxstacksize; /* number of registers needed by this function */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ @@ -414,8 +410,8 @@ typedef struct SharedProto { int sizelineinfo; int sizep; /* size of 'p' */ int sizelocvars; - int linedefined; - int lastlinedefined; + int linedefined; /* debug information */ + int lastlinedefined; /* debug information */ void *l_G; /* global state belongs to */ Instruction *code; /* opcodes */ int *lineinfo; /* map from opcodes to source lines (debug information) */ @@ -493,7 +489,7 @@ typedef union TKey { #define setnodekey(L,key,obj) \ { TKey *k_=(key); const TValue *io_=(obj); \ k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \ - (void)L; checkliveness(G(L),io_); } + (void)L; checkliveness(L,io_); } typedef struct Node { diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index cb8a3c33..7dae5336 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.57 2015/04/10 17:41:04 roberto Exp $ +** $Id: loslib.c,v 1.60 2015/11/19 19:16:22 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -54,7 +54,12 @@ */ #define l_timet lua_Integer #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) -#define l_checktime(L,a) ((time_t)luaL_checkinteger(L,a)) + +static time_t l_checktime (lua_State *L, int arg) { + lua_Integer t = luaL_checkinteger(L, arg); + luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); + return (time_t)t; +} #endif /* } */ @@ -198,17 +203,29 @@ static int getboolfield (lua_State *L, const char *key) { } -static int getfield (lua_State *L, const char *key, int d) { - int res, isnum; - lua_getfield(L, -1, key); - res = (int)lua_tointegerx(L, -1, &isnum); - if (!isnum) { - if (d < 0) +/* maximum value for date fields (to avoid arithmetic overflows with 'int') */ +#if !defined(L_MAXDATEFIELD) +#define L_MAXDATEFIELD (INT_MAX / 2) +#endif + +static int getfield (lua_State *L, const char *key, int d, int delta) { + int isnum; + int t = lua_getfield(L, -1, key); + lua_Integer res = lua_tointegerx(L, -1, &isnum); + if (!isnum) { /* field is not a number? */ + if (t != LUA_TNIL) /* some other value? */ + return luaL_error(L, "field '%s' not an integer", key); + else if (d < 0) /* absent field; no default? */ return luaL_error(L, "field '%s' missing in date table", key); res = d; } + else { + if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) + return luaL_error(L, "field '%s' out-of-bounds", key); + res -= delta; + } lua_pop(L, 1); - return res; + return (int)res; } @@ -236,6 +253,10 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { } +/* maximum size for an individual 'strftime' item */ +#define SIZETIMEFMT 250 + + static int os_date (lua_State *L) { const char *s = luaL_optstring(L, 1, "%c"); time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); @@ -247,8 +268,8 @@ static int os_date (lua_State *L) { else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ - lua_pushnil(L); - else if (strcmp(s, "*t") == 0) { + luaL_error(L, "time result cannot be represented in this installation"); + if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm->tm_sec); setfield(L, "min", stm->tm_min); @@ -266,14 +287,14 @@ static int os_date (lua_State *L) { cc[0] = '%'; luaL_buffinit(L, &b); while (*s) { - if (*s != '%') /* no conversion specifier? */ + if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; - char buff[200]; /* should be big enough for any conversion result */ + char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s = checkoption(L, s + 1, cc); - reslen = strftime(buff, sizeof(buff), cc, stm); - luaL_addlstring(&b, buff, reslen); + reslen = strftime(buff, SIZETIMEFMT, cc, stm); + luaL_addsize(&b, reslen); } } luaL_pushresult(&b); @@ -290,21 +311,18 @@ static int os_time (lua_State *L) { struct tm ts; luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0); - ts.tm_min = getfield(L, "min", 0); - ts.tm_hour = getfield(L, "hour", 12); - ts.tm_mday = getfield(L, "day", -1); - ts.tm_mon = getfield(L, "month", -1) - 1; - ts.tm_year = getfield(L, "year", -1) - 1900; + ts.tm_sec = getfield(L, "sec", 0, 0); + ts.tm_min = getfield(L, "min", 0, 0); + ts.tm_hour = getfield(L, "hour", 12, 0); + ts.tm_mday = getfield(L, "day", -1, 0); + ts.tm_mon = getfield(L, "month", -1, 1); + ts.tm_year = getfield(L, "year", -1, 1900); ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); } - if (t != (time_t)(l_timet)t) - luaL_error(L, "time result cannot be represented in this Lua installation"); - else if (t == (time_t)(-1)) - lua_pushnil(L); - else - l_pushtime(L, t); + if (t != (time_t)(l_timet)t || t == (time_t)(-1)) + luaL_error(L, "time result cannot be represented in this installation"); + l_pushtime(L, t); return 1; } diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 0bed0dd3..60be8aa2 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.147 2014/12/27 20:31:43 roberto Exp $ +** $Id: lparser.c,v 2.149 2015/11/02 16:09:30 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -763,7 +763,7 @@ static void parlist (LexState *ls) { } case TK_DOTS: { /* param -> '...' */ luaX_next(ls); - f->is_vararg = 1; + f->is_vararg = 2; /* declared vararg */ break; } default: luaX_syntaxerror(ls, " or '...' expected"); @@ -959,6 +959,7 @@ static void simpleexp (LexState *ls, expdesc *v) { FuncState *fs = ls->fs; check_condition(ls, fs->f->sp->is_vararg, "cannot use '...' outside a vararg function"); + fs->f->sp->is_vararg = 1; /* function actually uses vararg */ init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); break; } @@ -1613,7 +1614,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->sp->is_vararg = 1; /* main function is always vararg */ + fs->f->sp->is_vararg = 2; /* main function is always declared vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ @@ -1629,10 +1630,10 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, FuncState funcstate; LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ - incr_top(L); + luaD_inctop(L); lexstate.h = luaH_new(L); /* create table for scanner */ sethvalue(L, L->top, lexstate.h); /* anchor it */ - incr_top(L); + luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L, NULL); funcstate.f->sp->source = luaS_new(L, name); /* create and anchor TString */ lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ diff --git a/3rd/lua/lstate.c b/3rd/lua/lstate.c index 12e51d24..9194ac34 100644 --- a/3rd/lua/lstate.c +++ b/3rd/lua/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.128 2015/03/04 13:31:21 roberto Exp $ +** $Id: lstate.c,v 2.133 2015/11/13 12:16:51 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -76,7 +76,7 @@ typedef struct LG { */ #define addbuff(b,p,e) \ { size_t t = cast(size_t, e); \ - memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } + memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } static unsigned int makeseed (lua_State *L) { char buff[4 * sizeof(size_t)]; @@ -93,10 +93,14 @@ static unsigned int makeseed (lua_State *L) { /* ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) -** invariant +** invariant (and avoiding underflows in 'totalbytes') */ void luaE_setdebt (global_State *g, l_mem debt) { - g->totalbytes -= (debt - g->GCdebt); + l_mem tb = gettotalbytes(g); + lua_assert(tb > 0); + if (debt < tb - MAX_LMEM) + debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ + g->totalbytes = tb - debt; g->GCdebt = debt; } @@ -107,6 +111,7 @@ CallInfo *luaE_extendCI (lua_State *L) { L->ci->next = ci; ci->previous = L->ci; ci->next = NULL; + L->nci++; return ci; } @@ -121,6 +126,7 @@ void luaE_freeCI (lua_State *L) { while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); + L->nci--; } } @@ -130,13 +136,14 @@ void luaE_freeCI (lua_State *L) { */ void luaE_shrinkCI (lua_State *L) { CallInfo *ci = L->ci; - while (ci->next != NULL) { /* while there is 'next' */ - CallInfo *next2 = ci->next->next; /* next's next */ - if (next2 == NULL) break; - luaM_free(L, ci->next); /* remove next */ + CallInfo *next2; /* next's next */ + /* while there are two nexts */ + while (ci->next != NULL && (next2 = ci->next->next) != NULL) { + luaM_free(L, ci->next); /* free next */ + L->nci--; ci->next = next2; /* remove 'next' from the list */ next2->previous = ci; - ci = next2; + ci = next2; /* keep next's next */ } } @@ -166,6 +173,7 @@ static void freestack (lua_State *L) { return; /* stack not completely built yet */ L->ci = &L->base_ci; /* free the entire 'ci' list */ luaE_freeCI(L); + lua_assert(L->nci == 0); luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ } @@ -214,6 +222,7 @@ static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; L->stack = NULL; L->ci = NULL; + L->nci = 0; L->stacksize = 0; L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; @@ -237,7 +246,6 @@ static void close_state (lua_State *L) { if (g->version) /* closing a fully built state? */ luai_userstateclose(L); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); - luaZ_freebuffer(L, &g->buff); freestack(L); lua_assert(gettotalbytes(g) == sizeof(LG)); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ @@ -306,7 +314,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); - luaZ_initbuffer(L, &g->buff); g->panic = NULL; g->version = NULL; g->gcstate = GCSpause; diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index eefc217d..65c914d2 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.122 2015/06/01 16:34:37 roberto Exp $ +** $Id: lstate.h,v 2.128 2015/11/13 12:16:51 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -89,8 +89,8 @@ typedef struct CallInfo { #define CIST_OAH (1<<0) /* original value of 'allowhook' */ #define CIST_LUA (1<<1) /* call is running a Lua function */ #define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#define CIST_REENTRY (1<<3) /* call is running on same invocation of - luaV_execute of previous call */ +#define CIST_FRESH (1<<3) /* call is running on a fresh invocation + of luaV_execute */ #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ @@ -109,7 +109,7 @@ typedef struct CallInfo { typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ - lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ + l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ lu_mem GCmemtrav; /* memory traversed by the GC */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ @@ -131,7 +131,6 @@ typedef struct global_State { GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */ struct lua_State *twups; /* list of threads with open upvalues */ - Mbuffer buff; /* temporary buffer for string concatenation */ unsigned int gcfinnum; /* number of finalizers to call in each GC step */ int gcpause; /* size of pause between successive GCs */ int gcstepmul; /* GC 'granularity' */ @@ -141,7 +140,7 @@ typedef struct global_State { TString *memerrmsg; /* memory-error message */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ - TString *strcache[STRCACHE_SIZE][1]; /* cache for strings in API */ + TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ } global_State; @@ -150,6 +149,7 @@ typedef struct global_State { */ struct lua_State { CommonHeader; + unsigned short nci; /* number of items in 'ci' list */ lu_byte status; StkId top; /* first free slot in the stack */ global_State *l_G; @@ -212,7 +212,7 @@ union GCUnion { /* actual number of total bytes allocated */ -#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) +#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 5046624d..79a232df 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.49 2015/06/01 16:34:37 roberto Exp $ +** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -48,14 +48,23 @@ int luaS_eqlngstr (TString *a, TString *b) { unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int h = seed ^ cast(unsigned int, l); - size_t l1; size_t step = (l >> LUAI_HASHLIMIT) + 1; - for (l1 = l; l1 >= step; l1 -= step) - h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); + for (; l >= step; l -= step) + h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; } +unsigned int luaS_hashlongstr (TString *ts) { + lua_assert(ts->tt == LUA_TLNGSTR); + if (ts->extra == 0) { /* no hash? */ + ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); + ts->extra = 1; /* now it has its hash */ + } + return ts->hash; +} + + /* ** resizes the string table */ @@ -92,11 +101,12 @@ void luaS_resize (lua_State *L, int newsize) { ** a non-collectable string.) */ void luaS_clearcache (global_State *g) { - int i; - for (i = 0; i < STRCACHE_SIZE; i++) { - if (iswhite(g->strcache[i][0])) /* will entry be collected? */ - g->strcache[i][0] = g->memerrmsg; /* replace it with something fixed */ - } + int i, j; + for (i = 0; i < STRCACHE_N; i++) + for (j = 0; j < STRCACHE_M; j++) { + if (iswhite(g->strcache[i][j])) /* will entry be collected? */ + g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ + } } @@ -105,13 +115,14 @@ void luaS_clearcache (global_State *g) { */ void luaS_init (lua_State *L) { global_State *g = G(L); - int i; + int i, j; luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ - for (i = 0; i < STRCACHE_SIZE; i++) /* fill cache with valid strings */ - g->strcache[i][0] = g->memerrmsg; + for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ + for (j = 0; j < STRCACHE_M; j++) + g->strcache[i][j] = g->memerrmsg; } @@ -119,8 +130,7 @@ void luaS_init (lua_State *L) { /* ** creates a new string object */ -static TString *createstrobj (lua_State *L, const char *str, size_t l, - int tag, unsigned int h) { +static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { TString *ts; GCObject *o; size_t totalsize; /* total size of TString object */ @@ -129,8 +139,14 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, ts = gco2ts(o); ts->hash = h; ts->extra = 0; - memcpy(getaddrstr(ts), str, l * sizeof(char)); - getaddrstr(ts)[l] = '\0'; /* ending 0 */ + getstr(ts)[l] = '\0'; /* ending 0 */ + return ts; +} + + +TString *luaS_createlngstrobj (lua_State *L, size_t l) { + TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); + ts->u.lnglen = l; return ts; } @@ -152,6 +168,7 @@ static TString *queryshrstr (lua_State *L, const char *str, size_t l, unsigned i TString *ts; global_State *g = G(L); TString **list = &g->strt.hash[lmod(h, g->strt.size)]; + lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { @@ -172,7 +189,8 @@ static TString *addshrstr (lua_State *L, const char *str, size_t l, unsigned int luaS_resize(L, g->strt.size * 2); list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ } - ts = createstrobj(L, str, l, LUA_TSHRSTR, h); + ts = createstrobj(L, l, LUA_TSHRSTR, h); + memcpy(getstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); ts->u.hnext = *list; *list = ts; @@ -190,10 +208,10 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) + if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) luaM_toobig(L); - ts = createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); - ts->u.lnglen = l; + ts = luaS_createlngstrobj(L, l); + memcpy(getstr(ts), str, l * sizeof(char)); return ts; } } @@ -206,15 +224,19 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { ** check hits. */ TString *luaS_new (lua_State *L, const char *str) { - unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */ + unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ + int j; TString **p = G(L)->strcache[i]; - if (strcmp(str, getstr(p[0])) == 0) /* hit? */ - return p[0]; /* that it is */ - else { /* normal route */ - TString *s = luaS_newlstr(L, str, strlen(str)); - p[0] = s; - return s; + for (j = 0; j < STRCACHE_M; j++) { + if (strcmp(str, getstr(p[j])) == 0) /* hit? */ + return p[j]; /* that is it */ } + /* normal route */ + for (j = STRCACHE_M - 1; j > 0; j--) + p[j] = p[j - 1]; /* move out last element */ + /* new element is first in the list */ + p[0] = luaS_newlstr(L, str, strlen(str)); + return p[0]; } @@ -241,6 +263,7 @@ Udata *luaS_newudata (lua_State *L, size_t s) { #define SHRSTR_SLOT 0x10000 #define HASH_NODE(h) ((h) % SHRSTR_SLOT) +#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString)) struct shrmap_slot { struct rwlock lock; @@ -282,7 +305,7 @@ query_string(unsigned int h, const char *str, lu_byte l) { rwlock_rlock(&s->lock); TString *ts = s->str; while (ts) { - if (ts->hash == h && + if (ts->hash == h && ts->shrlen == l && memcmp(str, ts+1, l) == 0) { break; @@ -327,7 +350,7 @@ add_string(unsigned int h, const char *str, lu_byte l) { rwlock_wlock(&s->lock); TString *ts = s->str; while (ts) { - if (ts->hash == h && + if (ts->hash == h && ts->shrlen == l && memcmp(str, ts+1, l) == 0) { break; @@ -384,7 +407,7 @@ luaS_clonestring(lua_State *L, TString *ts) { const char * str = getaddrstr(ts); global_State *g = G(L); TString *result; - if (ts->tt == LUA_TLNGSTR) + if (ts->tt == LUA_TLNGSTR) return luaS_newlstr(L, str, ts->u.lnglen); // look up global state of this L first l = ts->shrlen; diff --git a/3rd/lua/lstring.h b/3rd/lua/lstring.h index a28ff0a0..ca5f0a35 100644 --- a/3rd/lua/lstring.h +++ b/3rd/lua/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.59 2015/03/25 13:42:19 roberto Exp $ +** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -34,6 +34,7 @@ LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); +LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_clearcache (global_State *g); @@ -42,6 +43,7 @@ LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); +LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); #define ENABLE_SHORT_STRING_TABLE diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 19c350de..fe30e34b 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 roberto Exp $ +** $Id: lstrlib.c,v 1.239 2015/11/25 16:28:17 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -41,8 +41,10 @@ ** Some sizes are better limited to fit in 'int', but must also fit in ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) */ +#define MAX_SIZET ((size_t)(~(size_t)0)) + #define MAXSIZE \ - (sizeof(size_t) < sizeof(int) ? (~(size_t)0) : (size_t)(INT_MAX)) + (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) @@ -208,11 +210,12 @@ static int str_dump (lua_State *L) { typedef struct MatchState { - int matchdepth; /* control for recursive depth (to avoid C stack overflow) */ const char *src_init; /* init of source string */ const char *src_end; /* end ('\0') of source string */ const char *p_end; /* end ('\0') of pattern */ lua_State *L; + size_t nrep; /* limit to avoid non-linear complexity */ + int matchdepth; /* control for recursive depth (to avoid C stack overflow) */ int level; /* total number of captures (finished or unfinished) */ struct { const char *init; @@ -231,6 +234,17 @@ static const char *match (MatchState *ms, const char *s, const char *p); #endif +/* +** parameters to control the maximum number of operators handled in +** a match (to avoid non-linear complexity). The maximum will be: +** (subject length) * A_REPS + B_REPS +*/ +#if !defined(A_REPS) +#define A_REPS 4 +#define B_REPS 100000 +#endif + + #define L_ESC '%' #define SPECIALS "^$*+?.([%-" @@ -488,6 +502,8 @@ static const char *match (MatchState *ms, const char *s, const char *p) { s = NULL; /* fail */ } else { /* matched once */ + if (ms->nrep-- == 0) + luaL_error(ms->L, "pattern too complex"); switch (*ep) { /* handle optional suffix */ case '?': { /* optional */ const char *res; @@ -584,6 +600,26 @@ static int nospecials (const char *p, size_t l) { } +static void prepstate (MatchState *ms, lua_State *L, + const char *s, size_t ls, const char *p, size_t lp) { + ms->L = L; + ms->matchdepth = MAXCCALLS; + ms->src_init = s; + ms->src_end = s + ls; + ms->p_end = p + lp; + if (ls < (MAX_SIZET - B_REPS) / A_REPS) + ms->nrep = A_REPS * ls + B_REPS; + else /* overflow (very long subject) */ + ms->nrep = MAX_SIZET; /* no limit */ +} + + +static void reprepstate (MatchState *ms) { + ms->level = 0; + lua_assert(ms->matchdepth == MAXCCALLS); +} + + static int str_find_aux (lua_State *L, int find) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); @@ -611,15 +647,10 @@ static int str_find_aux (lua_State *L, int find) { if (anchor) { p++; lp--; /* skip anchor character */ } - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = s; - ms.src_end = s + ls; - ms.p_end = p + lp; + prepstate(&ms, L, s, ls, p, lp); do { const char *res; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); + reprepstate(&ms); if ((res=match(&ms, s1, p)) != NULL) { if (find) { lua_pushinteger(L, (s1 - s) + 1); /* start */ @@ -646,29 +677,26 @@ static int str_match (lua_State *L) { } +/* state for 'gmatch' */ +typedef struct GMatchState { + const char *src; /* current position */ + const char *p; /* pattern */ + MatchState ms; /* match state */ +} GMatchState; + + static int gmatch_aux (lua_State *L) { - MatchState ms; - size_t ls, lp; - const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); - const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp); + GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); const char *src; - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = s; - ms.src_end = s+ls; - ms.p_end = p + lp; - for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); - src <= ms.src_end; - src++) { + for (src = gm->src; src <= gm->ms.src_end; src++) { const char *e; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); - if ((e = match(&ms, src, p)) != NULL) { - lua_Integer newstart = e-s; - if (e == src) newstart++; /* empty match? go at least one position */ - lua_pushinteger(L, newstart); - lua_replace(L, lua_upvalueindex(3)); - return push_captures(&ms, src, e); + reprepstate(&gm->ms); + if ((e = match(&gm->ms, src, gm->p)) != NULL) { + if (e == src) /* empty match? */ + gm->src =src + 1; /* go at least one position */ + else + gm->src = e; + return push_captures(&gm->ms, src, e); } } return 0; /* not found */ @@ -676,10 +704,14 @@ static int gmatch_aux (lua_State *L) { static int gmatch (lua_State *L) { - luaL_checkstring(L, 1); - luaL_checkstring(L, 2); - lua_settop(L, 2); - lua_pushinteger(L, 0); + size_t ls, lp; + const char *s = luaL_checklstring(L, 1, &ls); + const char *p = luaL_checklstring(L, 2, &lp); + GMatchState *gm; + lua_settop(L, 2); /* keep them on closure to avoid being collected */ + gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState)); + prepstate(&gm->ms, L, s, ls, p, lp); + gm->src = s; gm->p = p; lua_pushcclosure(L, gmatch_aux, 3); return 1; } @@ -761,17 +793,11 @@ static int str_gsub (lua_State *L) { if (anchor) { p++; lp--; /* skip anchor character */ } - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = src; - ms.src_end = src+srcl; - ms.p_end = p + lp; + prepstate(&ms, L, src, srcl, p, lp); while (n < max_s) { const char *e; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); - e = match(&ms, src, p); - if (e) { + reprepstate(&ms); + if ((e = match(&ms, src, p)) != NULL) { n++; add_value(&ms, &b, src, e, tr); } @@ -830,13 +856,12 @@ static lua_Number adddigit (char *buff, int n, lua_Number x) { } -static int num2straux (char *buff, lua_Number x) { +static int num2straux (char *buff, int sz, lua_Number x) { if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */ - return sprintf(buff, LUA_NUMBER_FMT, x); /* equal to '%g' */ + return l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* equal to '%g' */ else if (x == 0) { /* can be -0... */ - sprintf(buff, LUA_NUMBER_FMT, x); - strcat(buff, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */ - return strlen(buff); + /* create "0" or "-0" followed by exponent */ + return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", x); } else { int e; @@ -855,15 +880,16 @@ static int num2straux (char *buff, lua_Number x) { m = adddigit(buff, n++, m * 16); } while (m > 0); } - n += sprintf(buff + n, "p%+d", e); /* add exponent */ + n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ + lua_assert(n < sz); return n; } } -static int lua_number2strx (lua_State *L, char *buff, const char *fmt, - lua_Number x) { - int n = num2straux(buff, x); +static int lua_number2strx (lua_State *L, char *buff, int sz, + const char *fmt, lua_Number x) { + int n = num2straux(buff, sz, x); if (fmt[SIZELENMOD] == 'A') { int i; for (i = 0; i < n; i++) @@ -879,10 +905,12 @@ static int lua_number2strx (lua_State *L, char *buff, const char *fmt, /* ** Maximum size of each formatted item. This maximum size is produced -** by format('%.99f', minfloat), and is equal to 99 + 2 ('-' and '.') + -** number of decimal digits to represent minfloat. +** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.', +** and '\0') + number of decimal digits to represent maxfloat (which +** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra +** expenses", such as locale-dependent stuff) */ -#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) +#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) /* valid flags in a format specification */ @@ -906,9 +934,9 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { else if (*s == '\0' || iscntrl(uchar(*s))) { char buff[10]; if (!isdigit(uchar(*(s+1)))) - sprintf(buff, "\\%d", (int)uchar(*s)); + l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); else - sprintf(buff, "\\%03d", (int)uchar(*s)); + l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); luaL_addstring(b, buff); } else @@ -975,24 +1003,25 @@ static int str_format (lua_State *L) { strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { - nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg)); + nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg)); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { lua_Integer n = luaL_checkinteger(L, arg); addlenmod(form, LUA_INTEGER_FRMLEN); - nb = sprintf(buff, form, n); + nb = l_sprintf(buff, MAX_ITEM, form, n); break; } case 'a': case 'A': addlenmod(form, LUA_NUMBER_FRMLEN); - nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg)); + nb = lua_number2strx(L, buff, MAX_ITEM, form, + luaL_checknumber(L, arg)); break; case 'e': case 'E': case 'f': case 'g': case 'G': { addlenmod(form, LUA_NUMBER_FRMLEN); - nb = sprintf(buff, form, luaL_checknumber(L, arg)); + nb = l_sprintf(buff, MAX_ITEM, form, luaL_checknumber(L, arg)); break; } case 'q': { @@ -1002,14 +1031,18 @@ static int str_format (lua_State *L) { case 's': { size_t l; const char *s = luaL_tolstring(L, arg, &l); - if (!strchr(form, '.') && l >= 100) { - /* no precision and string is too long to be formatted; - keep original string */ - luaL_addvalue(&b); - } + if (form[2] == '\0') /* no modifiers? */ + luaL_addvalue(&b); /* keep entire string */ else { - nb = sprintf(buff, form, s); - lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ + luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); + if (!strchr(form, '.') && l >= 100) { + /* no precision and string is too long to be formatted */ + luaL_addvalue(&b); /* keep entire string */ + } + else { /* format the string into 'buff' */ + nb = l_sprintf(buff, MAX_ITEM, form, s); + lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ + } } break; } @@ -1018,6 +1051,7 @@ static int str_format (lua_State *L) { *(strfrmt - 1)); } } + lua_assert(nb < MAX_ITEM); luaL_addsize(&b, nb); } } @@ -1309,8 +1343,13 @@ static int str_pack (lua_State *L) { case Kchar: { /* fixed-size string */ size_t len; const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, len == (size_t)size, arg, "wrong length"); - luaL_addlstring(&b, s, size); + if ((size_t)size <= len) /* string larger than (or equal to) needed? */ + luaL_addlstring(&b, s, size); /* truncate string to asked size */ + else { /* string smaller than needed */ + luaL_addlstring(&b, s, len); /* add it all */ + while (len++ < (size_t)size) /* pad extra space */ + luaL_addchar(&b, LUA_PACKPADBYTE); + } break; } case Kstring: { /* strings with length count */ @@ -1360,7 +1399,7 @@ static int str_packsize (lua_State *L) { case Kstring: /* strings with length count */ case Kzstr: /* zero-terminated string */ luaL_argerror(L, 1, "variable-length format"); - break; + /* call never return, but to avoid warnings: *//* FALLTHROUGH */ default: break; } } diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 04f2a347..7e15b71b 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.111 2015/06/09 14:21:13 roberto Exp $ +** $Id: ltable.c,v 2.117 2015/11/19 19:16:22 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -85,7 +85,7 @@ static const Node dummynode_ = { /* ** Hash for floating-point numbers. ** The main computation should be just -** n = frepx(n, &i); return (n * INT_MAX) + i +** n = frexp(n, &i); return (n * INT_MAX) + i ** but there are some numerical subtleties. ** In a two-complement representation, INT_MAX does not has an exact ** representation as a float, but INT_MIN does; because the absolute @@ -101,7 +101,7 @@ static int l_hashfloat (lua_Number n) { lua_Integer ni; n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ - lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == HUGE_VAL); + lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL)); return 0; } else { /* normal case */ @@ -124,14 +124,8 @@ static Node *mainposition (const Table *t, const TValue *key) { return hashmod(t, l_hashfloat(fltvalue(key))); case LUA_TSHRSTR: return hashstr(t, tsvalue(key)); - case LUA_TLNGSTR: { - TString *s = tsvalue(key); - if (s->extra == 0) { /* no hash? */ - s->hash = luaS_hash(getstr(s), s->u.lnglen, s->hash); - s->extra = 1; /* now it has its hash */ - } - return hashstr(t, tsvalue(key)); - } + case LUA_TLNGSTR: + return hashpow2(t, luaS_hashlongstr(tsvalue(key))); case LUA_TBOOLEAN: return hashboolean(t, bvalue(key)); case LUA_TLIGHTUSERDATA: @@ -139,6 +133,7 @@ static Node *mainposition (const Table *t, const TValue *key) { case LUA_TLCF: return hashpointer(t, fvalue(key)); default: + lua_assert(!ttisdeadkey(key)); return hashpointer(t, gcvalue(key)); } } @@ -463,7 +458,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { Node *f = getfreepos(t); /* get a free place */ if (f == NULL) { /* cannot find a free place? */ rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' takes care of TM cache and GC barrier */ + /* whatever called 'newkey' takes care of TM cache */ return luaH_set(L, t, key); /* insert key into grown table */ } lua_assert(!isdummy(f)); @@ -501,7 +496,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { */ const TValue *luaH_getint (Table *t, lua_Integer key) { /* (1 <= key && key <= t->sizearray) */ - if (l_castS2U(key - 1) < t->sizearray) + if (l_castS2U(key) - 1 < t->sizearray) return &t->array[key - 1]; else { Node *n = hashint(t, key); @@ -513,7 +508,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { if (nx == 0) break; n += nx; } - }; + } return luaO_nilobject; } } @@ -522,7 +517,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { /* ** search function for short strings */ -const TValue *luaH_getstr (Table *t, TString *key) { +const TValue *luaH_getshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); lua_assert(key->tt == LUA_TSHRSTR); for (;;) { /* check whether 'key' is somewhere in the chain */ @@ -531,11 +526,41 @@ const TValue *luaH_getstr (Table *t, TString *key) { return gval(n); /* that's it */ else { int nx = gnext(n); - if (nx == 0) break; + if (nx == 0) + return luaO_nilobject; /* not found */ n += nx; } - }; - return luaO_nilobject; + } +} + + +/* +** "Generic" get version. (Not that generic: not valid for integers, +** which may be in array part, nor for floats with integral values.) +*/ +static const TValue *getgeneric (Table *t, const TValue *key) { + Node *n = mainposition(t, key); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (luaV_rawequalobj(gkey(n), key)) + return gval(n); /* that's it */ + else { + int nx = gnext(n); + if (nx == 0) + return luaO_nilobject; /* not found */ + n += nx; + } + } +} + + +const TValue *luaH_getstr (Table *t, TString *key) { + if (key->tt == LUA_TSHRSTR) + return luaH_getshortstr(t, key); + else { /* for long strings, use generic case */ + TValue ko; + setsvalue(cast(lua_State *, NULL), &ko, key); + return getgeneric(t, &ko); + } } @@ -544,7 +569,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { */ const TValue *luaH_get (Table *t, const TValue *key) { switch (ttype(key)) { - case LUA_TSHRSTR: return luaH_getstr(t, tsvalue(key)); + case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); case LUA_TNIL: return luaO_nilobject; case LUA_TNUMFLT: { @@ -553,19 +578,8 @@ const TValue *luaH_get (Table *t, const TValue *key) { return luaH_getint(t, k); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ - default: { - Node *n = mainposition(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (luaV_rawequalobj(gkey(n), key)) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) break; - n += nx; - } - }; - return luaO_nilobject; - } + default: + return getgeneric(t, key); } } diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index 53d25511..213cc139 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.20 2014/09/04 18:15:29 roberto Exp $ +** $Id: ltable.h,v 2.21 2015/11/03 15:47:30 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -18,6 +18,10 @@ /* 'const' to avoid wrong writings that can mess up field 'next' */ #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) +/* +** writable version of 'gkey'; allows updates to individual fields, +** but not to the whole (which has incompatible type) +*/ #define wgkey(n) (&(n)->i_key.nk) #define invalidateTMcache(t) ((t)->flags = 0) @@ -31,6 +35,7 @@ LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); +LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index a05c885c..b3c9a7c5 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.80 2015/01/13 16:27:29 roberto Exp $ +** $Id: ltablib.c,v 1.90 2015/11/25 12:48:57 roberto Exp $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -12,6 +12,7 @@ #include #include +#include #include "lua.h" @@ -19,40 +20,42 @@ #include "lualib.h" - /* -** Structure with table-access functions +** Operations that an object must define to mimic a table +** (some functions only need some of them) */ -typedef struct { - int (*geti) (lua_State *L, int idx, lua_Integer n); - void (*seti) (lua_State *L, int idx, lua_Integer n); -} TabA; +#define TAB_R 1 /* read */ +#define TAB_W 2 /* write */ +#define TAB_L 4 /* length */ +#define TAB_RW (TAB_R | TAB_W) /* read/write */ -/* -** Check that 'arg' has a table and set access functions in 'ta' to raw -** or non-raw according to the presence of corresponding metamethods. -*/ -static void checktab (lua_State *L, int arg, TabA *ta) { - ta->geti = NULL; ta->seti = NULL; - if (lua_getmetatable(L, arg)) { - lua_pushliteral(L, "__index"); /* 'index' metamethod */ - if (lua_rawget(L, -2) != LUA_TNIL) - ta->geti = lua_geti; - lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */ - if (lua_rawget(L, -3) != LUA_TNIL) - ta->seti = lua_seti; - lua_pop(L, 3); /* pop metatable plus both metamethods */ - } - if (ta->geti == NULL || ta->seti == NULL) { - luaL_checktype(L, arg, LUA_TTABLE); /* must be table for raw methods */ - if (ta->geti == NULL) ta->geti = lua_rawgeti; - if (ta->seti == NULL) ta->seti = lua_rawseti; - } +#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) + + +static int checkfield (lua_State *L, const char *key, int n) { + lua_pushstring(L, key); + return (lua_rawget(L, -n) != LUA_TNIL); } -#define aux_getn(L,n,ta) (checktab(L, n, ta), luaL_len(L, n)) +/* +** Check that 'arg' either is a table or can behave like one (that is, +** has a metatable with the required metamethods) +*/ +static void checktab (lua_State *L, int arg, int what) { + if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ + int n = 1; /* number of elements to pop */ + if (lua_getmetatable(L, arg) && /* must have metatable */ + (!(what & TAB_R) || checkfield(L, "__index", ++n)) && + (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && + (!(what & TAB_L) || checkfield(L, "__len", ++n))) { + lua_pop(L, n); /* pop metatable and tested metamethods */ + } + else + luaL_argerror(L, arg, "table expected"); /* force an error */ + } +} #if defined(LUA_COMPAT_MAXN) @@ -74,8 +77,7 @@ static int maxn (lua_State *L) { static int tinsert (lua_State *L) { - TabA ta; - lua_Integer e = aux_getn(L, 1, &ta) + 1; /* first empty element */ + lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ lua_Integer pos; /* where to insert new element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ @@ -87,8 +89,8 @@ static int tinsert (lua_State *L) { pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); for (i = e; i > pos; i--) { /* move up elements */ - (*ta.geti)(L, 1, i - 1); - (*ta.seti)(L, 1, i); /* t[i] = t[i - 1] */ + lua_geti(L, 1, i - 1); + lua_seti(L, 1, i); /* t[i] = t[i - 1] */ } break; } @@ -96,57 +98,57 @@ static int tinsert (lua_State *L) { return luaL_error(L, "wrong number of arguments to 'insert'"); } } - (*ta.seti)(L, 1, pos); /* t[pos] = v */ + lua_seti(L, 1, pos); /* t[pos] = v */ return 0; } static int tremove (lua_State *L) { - TabA ta; - lua_Integer size = aux_getn(L, 1, &ta); + lua_Integer size = aux_getn(L, 1, TAB_RW); lua_Integer pos = luaL_optinteger(L, 2, size); if (pos != size) /* validate 'pos' if given */ luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); - (*ta.geti)(L, 1, pos); /* result = t[pos] */ + lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { - (*ta.geti)(L, 1, pos + 1); - (*ta.seti)(L, 1, pos); /* t[pos] = t[pos + 1] */ + lua_geti(L, 1, pos + 1); + lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ } lua_pushnil(L); - (*ta.seti)(L, 1, pos); /* t[pos] = nil */ + lua_seti(L, 1, pos); /* t[pos] = nil */ return 1; } +/* +** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever +** possible, copy in increasing order, which is better for rehashing. +** "possible" means destination after original range, or smaller +** than origin, or copying to another table. +*/ static int tmove (lua_State *L) { - TabA ta; lua_Integer f = luaL_checkinteger(L, 2); lua_Integer e = luaL_checkinteger(L, 3); lua_Integer t = luaL_checkinteger(L, 4); int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ + checktab(L, 1, TAB_R); + checktab(L, tt, TAB_W); if (e >= f) { /* otherwise, nothing to move */ lua_Integer n, i; - ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL) - ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti) - : lua_geti; - ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL) - ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) - : lua_seti; luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, "too many elements to move"); n = e - f + 1; /* number of elements to move */ luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, "destination wrap around"); - if (t > f) { - for (i = n - 1; i >= 0; i--) { - (*ta.geti)(L, 1, f + i); - (*ta.seti)(L, tt, t + i); + if (t > e || t <= f || tt != 1) { + for (i = 0; i < n; i++) { + lua_geti(L, 1, f + i); + lua_seti(L, tt, t + i); } } else { - for (i = 0; i < n; i++) { - (*ta.geti)(L, 1, f + i); - (*ta.seti)(L, tt, t + i); + for (i = n - 1; i >= 0; i--) { + lua_geti(L, 1, f + i); + lua_seti(L, tt, t + i); } } } @@ -155,8 +157,8 @@ static int tmove (lua_State *L) { } -static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { - (*ta->geti)(L, 1, i); +static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { + lua_geti(L, 1, i); if (!lua_isstring(L, -1)) luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", luaL_typename(L, -1), i); @@ -165,21 +167,19 @@ static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { static int tconcat (lua_State *L) { - TabA ta; luaL_Buffer b; + lua_Integer last = aux_getn(L, 1, TAB_R); size_t lsep; - lua_Integer i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); - checktab(L, 1, &ta); - i = luaL_optinteger(L, 3, 1); - last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); + lua_Integer i = luaL_optinteger(L, 3, 1); + last = luaL_opt(L, luaL_checkinteger, 4, last); luaL_buffinit(L, &b); for (; i < last; i++) { - addfield(L, &b, &ta, i); + addfield(L, &b, i); luaL_addlstring(&b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ - addfield(L, &b, &ta, i); + addfield(L, &b, i); luaL_pushresult(&b); return 1; } @@ -197,7 +197,7 @@ static int pack (lua_State *L) { lua_createtable(L, n, 1); /* create result table */ lua_insert(L, 1); /* put it at index 1 */ for (i = n; i >= 1; i--) /* assign elements */ - lua_rawseti(L, 1, i); + lua_seti(L, 1, i); lua_pushinteger(L, n); lua_setfield(L, 1, "n"); /* t.n = number of elements */ return 1; /* return table */ @@ -205,20 +205,17 @@ static int pack (lua_State *L) { static int unpack (lua_State *L) { - TabA ta; - lua_Integer i, e; lua_Unsigned n; - checktab(L, 1, &ta); - i = luaL_optinteger(L, 2, 1); - e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); + lua_Integer i = luaL_optinteger(L, 2, 1); + lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) return luaL_error(L, "too many results to unpack"); - do { /* must have at least one element */ - (*ta.geti)(L, 1, i); /* push arg[i..e] */ - } while (i++ < e); - + for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ + lua_geti(L, 1, i); + } + lua_geti(L, 1, e); /* push last element */ return (int)n; } @@ -235,97 +232,190 @@ static int unpack (lua_State *L) { */ -static void set2 (lua_State *L, TabA *ta, int i, int j) { - (*ta->seti)(L, 1, i); - (*ta->seti)(L, 1, j); +/* +** Produce a "random" 'unsigned int' to randomize pivot choice. This +** macro is used only when 'sort' detects a big imbalance in the result +** of a partition. (If you don't want/need this "randomness", ~0 is a +** good choice.) +*/ +#if !defined(l_randomizePivot) /* { */ + +#include + +/* size of 'e' measured in number of 'unsigned int's */ +#define sof(e) (sizeof(e) / sizeof(unsigned int)) + +/* +** Use 'time' and 'clock' as sources of "randomness". Because we don't +** know the types 'clock_t' and 'time_t', we cannot cast them to +** anything without risking overflows. A safe way to use their values +** is to copy them to an array of a known type and use the array values. +*/ +static unsigned int l_randomizePivot (void) { + clock_t c = clock(); + time_t t = time(NULL); + unsigned int buff[sof(c) + sof(t)]; + unsigned int i, rnd = 0; + memcpy(buff, &c, sof(c) * sizeof(unsigned int)); + memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); + for (i = 0; i < sof(buff); i++) + rnd += buff[i]; + return rnd; } +#endif /* } */ + + +/* arrays larger than 'RANLIMIT' may use randomized pivots */ +#define RANLIMIT 100u + + +static void set2 (lua_State *L, unsigned int i, unsigned int j) { + lua_seti(L, 1, i); + lua_seti(L, 1, j); +} + + +/* +** Return true iff value at stack index 'a' is less than the value at +** index 'b' (according to the order of the sort). +*/ static int sort_comp (lua_State *L, int a, int b) { - if (!lua_isnil(L, 2)) { /* function? */ + if (lua_isnil(L, 2)) /* no function? */ + return lua_compare(L, a, b, LUA_OPLT); /* a < b */ + else { /* function */ int res; - lua_pushvalue(L, 2); + lua_pushvalue(L, 2); /* push function */ lua_pushvalue(L, a-1); /* -1 to compensate function */ lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ - lua_call(L, 2, 1); - res = lua_toboolean(L, -1); - lua_pop(L, 1); + lua_call(L, 2, 1); /* call function */ + res = lua_toboolean(L, -1); /* get result */ + lua_pop(L, 1); /* pop result */ return res; } - else /* a < b? */ - return lua_compare(L, a, b, LUA_OPLT); } -static void auxsort (lua_State *L, TabA *ta, int l, int u) { - while (l < u) { /* for tail recursion */ - int i, j; - /* sort elements a[l], a[(l+u)/2] and a[u] */ - (*ta->geti)(L, 1, l); - (*ta->geti)(L, 1, u); - if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ - set2(L, ta, l, u); /* swap a[l] - a[u] */ + +/* +** Does the partition: Pivot P is at the top of the stack. +** precondition: a[lo] <= P == a[up-1] <= a[up], +** so it only needs to do the partition from lo + 1 to up - 2. +** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] +** returns 'i'. +*/ +static unsigned int partition (lua_State *L, unsigned int lo, + unsigned int up) { + unsigned int i = lo; /* will be incremented before first use */ + unsigned int j = up - 1; /* will be decremented before first use */ + /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ + for (;;) { + /* next loop: repeat ++i while a[i] < P */ + while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { + if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ + luaL_error(L, "invalid order function for sorting"); + lua_pop(L, 1); /* remove a[i] */ + } + /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ + /* next loop: repeat --j while P < a[j] */ + while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { + if (j < i) /* j < i but a[j] > P ?? */ + luaL_error(L, "invalid order function for sorting"); + lua_pop(L, 1); /* remove a[j] */ + } + /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ + if (j < i) { /* no elements out of place? */ + /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ + lua_pop(L, 1); /* pop a[j] */ + /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ + set2(L, up - 1, i); + return i; + } + /* otherwise, swap a[i] - a[j] to restore invariant and repeat */ + set2(L, i, j); + } +} + + +/* +** Choose an element in the middle (2nd-3th quarters) of [lo,up] +** "randomized" by 'rnd' +*/ +static unsigned int choosePivot (unsigned int lo, unsigned int up, + unsigned int rnd) { + unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ + unsigned int p = rnd % (r4 * 2) + (lo + r4); + lua_assert(lo + r4 <= p && p <= up - r4); + return p; +} + + +/* +** QuickSort algorithm (recursive function) +*/ +static void auxsort (lua_State *L, unsigned int lo, unsigned int up, + unsigned int rnd) { + while (lo < up) { /* loop for tail recursion */ + unsigned int p; /* Pivot index */ + unsigned int n; /* to be used later */ + /* sort elements 'lo', 'p', and 'up' */ + lua_geti(L, 1, lo); + lua_geti(L, 1, up); + if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ + set2(L, lo, up); /* swap a[lo] - a[up] */ else - lua_pop(L, 2); - if (u-l == 1) break; /* only 2 elements */ - i = (l+u)/2; - (*ta->geti)(L, 1, i); - (*ta->geti)(L, 1, l); - if (sort_comp(L, -2, -1)) /* a[i]geti)(L, 1, u); - if (sort_comp(L, -1, -2)) /* a[u]geti)(L, 1, i); /* Pivot */ - lua_pushvalue(L, -1); - (*ta->geti)(L, 1, u-1); - set2(L, ta, i, u-1); - /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ - i = l; j = u-1; - for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ - /* repeat ++i until a[i] >= P */ - while ((*ta->geti)(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i>=u) luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[i] */ - } - /* repeat --j until a[j] <= P */ - while ((*ta->geti)(L, 1, --j), sort_comp(L, -3, -1)) { - if (j<=l) luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[j] */ - } - if (jgeti)(L, 1, u-1); - (*ta->geti)(L, 1, i); - set2(L, ta, u-1, i); /* swap pivot (a[u-1]) with a[i] */ - /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ - /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ - if (i-l < u-i) { - j=l; i=i-1; l=i+2; + if (up - lo == 2) /* only 3 elements? */ + return; /* already sorted */ + lua_geti(L, 1, p); /* get middle element (Pivot) */ + lua_pushvalue(L, -1); /* push Pivot */ + lua_geti(L, 1, up - 1); /* push a[up - 1] */ + set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ + p = partition(L, lo, up); + /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ + if (p - lo < up - p) { /* lower interval is smaller? */ + auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ + n = p - lo; /* size of smaller interval */ + lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */ } else { - j=i+1; i=u; u=j-2; + auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ + n = up - p; /* size of smaller interval */ + up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ } - auxsort(L, ta, j, i); /* call recursively the smaller one */ - } /* repeat the routine for the larger one */ + if ((up - lo) / 128u > n) /* partition too imbalanced? */ + rnd = l_randomizePivot(); /* try a new randomization */ + } /* tail call auxsort(L, lo, up, rnd) */ } + static int sort (lua_State *L) { - TabA ta; - int n = (int)aux_getn(L, 1, &ta); - luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */ - if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ - luaL_checktype(L, 2, LUA_TFUNCTION); - lua_settop(L, 2); /* make sure there are two arguments */ - auxsort(L, &ta, 1, n); + lua_Integer n = aux_getn(L, 1, TAB_RW); + if (n > 1) { /* non-trivial interval? */ + luaL_argcheck(L, n < INT_MAX, 1, "array too big"); + luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ + if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ + luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ + lua_settop(L, 2); /* make sure there are two arguments */ + auxsort(L, 1, (unsigned int)n, 0u); + } return 0; } diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index c38e5c34..22b4df39 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.34 2015/03/30 15:42:27 roberto Exp $ +** $Id: ltm.c,v 2.36 2015/11/03 15:47:30 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -57,7 +57,7 @@ void luaT_init (lua_State *L) { ** tag methods */ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getstr(events, ename); + const TValue *tm = luaH_getshortstr(events, ename); lua_assert(event <= TM_EQ); if (ttisnil(tm)) { /* no tag method? */ events->flags |= cast_byte(1u<mt[ttnov(o)]; } - return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); + return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); } void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, TValue *p3, int hasres) { ptrdiff_t result = savestack(L, p3); - setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */ - setobj2s(L, L->top++, p1); /* 1st argument */ - setobj2s(L, L->top++, p2); /* 2nd argument */ + StkId func = L->top; + setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ + setobj2s(L, func + 1, p1); /* 1st argument */ + setobj2s(L, func + 2, p2); /* 2nd argument */ + L->top += 3; if (!hasres) /* no result? 'p3' is third argument */ setobj2s(L, L->top++, p3); /* 3rd argument */ /* metamethod may yield only when called from Lua code */ - luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); + if (isLua(L->ci)) + luaD_call(L, func, hasres); + else + luaD_callnoyield(L, func, hasres); if (hasres) { /* if has result, move it to its place */ p3 = restorestack(L, result); setobjs2s(L, p3, --L->top); diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 825d7c21..ca45e2e1 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.225 2015/03/30 15:42:59 roberto Exp $ +** $Id: lua.c,v 1.226 2015/08/14 19:11:20 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -325,24 +325,20 @@ static int pushline (lua_State *L, int firstline) { /* -** Try to compile line on the stack as 'return '; on return, stack +** Try to compile line on the stack as 'return ;'; on return, stack ** has either compiled chunk or original line (if compilation failed). */ static int addreturn (lua_State *L) { - int status; - size_t len; const char *line; - lua_pushliteral(L, "return "); - lua_pushvalue(L, -2); /* duplicate line */ - lua_concat(L, 2); /* new line is "return ..." */ - line = lua_tolstring(L, -1, &len); - if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) { - lua_remove(L, -3); /* remove original line */ - line += sizeof("return")/sizeof(char); /* remove 'return' for history */ + const char *line = lua_tostring(L, -1); /* original line */ + const char *retline = lua_pushfstring(L, "return %s;", line); + int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); + if (status == LUA_OK) { + lua_remove(L, -2); /* remove modified line */ if (line[0] != '\0') /* non empty? */ lua_saveline(L, line); /* keep history */ } else - lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */ + lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ return status; } diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 89ab8d38..75080b6c 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.328 2015/06/03 13:03:38 roberto Exp $ +** $Id: lua.h,v 1.329 2015/11/13 17:18:42 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -19,7 +19,7 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "3" #define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "1" +#define LUA_VERSION_RELEASE "2" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index 7cfa4faf..b4ed5001 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp $ +** $Id: luaconf.h,v 1.254 2015/10/21 18:17:40 roberto Exp $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -145,7 +145,7 @@ #if !defined(LUA_FLOAT_TYPE) #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif /* } */ +#endif /* }================================================================== */ @@ -412,9 +412,33 @@ @@ LUA_NUMBER_FMT is the format for writing floats. @@ lua_number2str converts a float to a string. @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. +@@ l_floor takes the floor of a float. @@ lua_str2number converts a decimal numeric string to a number. */ + +/* The following definitions are good for most cases here */ + +#define l_floor(x) (l_mathop(floor)(x)) + +#define lua_number2str(s,sz,n) l_sprintf((s), sz, LUA_NUMBER_FMT, (n)) + +/* +@@ lua_numbertointeger converts a float number to an integer, or +** returns 0 if float is not within the range of a lua_Integer. +** (The range comparisons are tricky because of rounding. The tests +** here assume a two-complement representation, where MININTEGER always +** has an exact representation as a float; MAXINTEGER may not have one, +** and therefore its conversion to float may have an ill-defined value.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + +/* now the variable definitions */ + #if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ #define LUA_NUMBER float @@ -468,25 +492,6 @@ #endif /* } */ -#define l_floor(x) (l_mathop(floor)(x)) - -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) - - -/* -@@ lua_numbertointeger converts a float number to an integer, or -** returns 0 if float is not within the range of a lua_Integer. -** (The range comparisons are tricky because of rounding. The tests -** here assume a two-complement representation, where MININTEGER always -** has an exact representation as a float; MAXINTEGER may not have one, -** and therefore its conversion to float may have an ill-defined value.) -*/ -#define lua_numbertointeger(n,p) \ - ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ - (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ - (*(p) = (LUA_INTEGER)(n), 1)) - - /* @@ LUA_INTEGER is the integer type used by Lua. @@ -506,7 +511,7 @@ /* The following definitions are good for most cases here */ #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" -#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n)) +#define lua_integer2str(s,sz,n) l_sprintf((s), sz, LUA_INTEGER_FMT, (n)) #define LUAI_UACINT LUA_INTEGER @@ -537,6 +542,7 @@ #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ +/* use presence of macro LLONG_MAX as proxy for C99 compliance */ #if defined(LLONG_MAX) /* { */ /* use ISO C99 stuff */ @@ -577,6 +583,17 @@ ** =================================================================== */ +/* +@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. +** (All uses in Lua have only one format item.) +*/ +#if !defined(LUA_USE_C89) +#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) +#else +#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) +#endif + + /* @@ lua_strx2number converts an hexadecimal numeric string to a number. ** In C99, 'strtod' does that conversion. Otherwise, you can @@ -584,7 +601,7 @@ ** implementation. */ #if !defined(LUA_USE_C89) -#define lua_strx2number(s,p) lua_str2number(s,p) +#define lua_strx2number(s,p) lua_str2number(s,p) #endif @@ -595,7 +612,7 @@ ** provide its own implementation. */ #if !defined(LUA_USE_C89) -#define lua_number2strx(L,b,f,n) sprintf(b,f,n) +#define lua_number2strx(L,b,sz,f,n) l_sprintf(b,sz,f,n) #endif diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 451c9097..5e0224c5 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.41 2014/11/02 19:19:04 roberto Exp $ +** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -32,7 +32,6 @@ typedef struct { lua_State *L; ZIO *Z; - Mbuffer *b; const char *name; } LoadState; @@ -92,10 +91,15 @@ static TString *LoadString (LoadState *S) { LoadVar(S, size); if (size == 0) return NULL; - else { - char *s = luaZ_openspace(S->L, S->b, --size); - LoadVector(S, s, size); - return luaS_newlstr(S->L, s, size); + else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ + char buff[LUAI_MAXSHORTLEN]; + LoadVector(S, buff, size); + return luaS_newlstr(S->L, buff, size); + } + else { /* long string */ + TString *ts = luaS_createlngstrobj(S->L, size); + LoadVector(S, getstr(ts), size); /* load directly in final place */ + return ts; } } @@ -252,8 +256,7 @@ static void checkHeader (LoadState *S) { /* ** load precompiled chunk */ -LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, - const char *name) { +LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { LoadState S; LClosure *cl; if (*name == '@' || *name == '=') @@ -264,11 +267,10 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, S.name = name; S.L = L; S.Z = Z; - S.b = buff; checkHeader(&S); cl = luaF_newLclosure(L, LoadByte(&S)); setclLvalue(L, L->top, cl); - incr_top(L); + luaD_inctop(L); cl->p = luaF_newproto(L, NULL); LoadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); diff --git a/3rd/lua/lundump.h b/3rd/lua/lundump.h index ef43d512..aa5cc82f 100644 --- a/3rd/lua/lundump.h +++ b/3rd/lua/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 roberto Exp $ +** $Id: lundump.h,v 1.45 2015/09/08 15:41:05 roberto Exp $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -23,8 +23,7 @@ #define LUAC_FORMAT 0 /* this is the official format */ /* load one chunk; from lundump.c */ -LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, - const char* name); +LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); /* dump one chunk; from ldump.c */ LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 95a0ebe1..58ba7f29 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.245 2015/06/09 15:53:35 roberto Exp $ +** $Id: lvm.c,v 2.265 2015/11/23 11:30:45 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -66,7 +66,7 @@ /* Add by skynet */ lua_State * skynet_sig_L = NULL; -LUA_API void +LUA_API void lua_checksig_(lua_State *L) { if (skynet_sig_L == G(L)->mainthread) { skynet_sig_L = NULL; @@ -163,30 +163,28 @@ static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, /* -** Main function for table access (invoking metamethods if needed). -** Compute 'val = t[key]' +** Complete a table access: if 't' is a table, 'tm' has its metamethod; +** otherwise, 'tm' is NULL. */ -void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { +void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, + const TValue *tm) { int loop; /* counter to avoid infinite loops */ + lua_assert(tm != NULL || !ttistable(t)); for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* 't' is a table? */ - Table *h = hvalue(t); - const TValue *res = luaH_get(h, key); /* do a primitive get */ - if (!ttisnil(res) || /* result is not nil? */ - (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ - setobj2s(L, val, res); /* result is the raw get */ - return; - } - /* else will try metamethod */ + if (tm == NULL) { /* no metamethod (from a table)? */ + if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) + luaG_typeerror(L, t, "index"); /* no metamethod */ } - else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) - luaG_typeerror(L, t, "index"); /* no metamethod */ if (ttisfunction(tm)) { /* metamethod is a function */ - luaT_callTM(L, tm, t, key, val, 1); + luaT_callTM(L, tm, t, key, val, 1); /* call it */ return; } t = tm; /* else repeat access over 'tm' */ + if (luaV_fastget(L,t,key,tm,luaH_get)) { /* try fast track */ + setobj2s(L, val, tm); /* done */ + return; + } + /* else repeat */ } luaG_runerror(L, "gettable chain too long; possible loop"); } @@ -196,40 +194,41 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { ** Main function for table assignment (invoking metamethods if needed). ** Compute 't[key] = val' */ -void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { +void luaV_finishset (lua_State *L, const TValue *t, TValue *key, + StkId val, const TValue *oldval) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; - if (ttistable(t)) { /* 't' is a table? */ - Table *h = hvalue(t); - TValue *oldval = cast(TValue *, luaH_get(h, key)); - /* if previous value is not nil, there must be a previous entry - in the table; a metamethod has no relevance */ - if (!ttisnil(oldval) || - /* previous value is nil; must check the metamethod */ - ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && + if (oldval != NULL) { + lua_assert(ttistable(t) && ttisnil(oldval)); + /* must check the metamethod */ + if ((tm = fasttm(L, hvalue(t)->metatable, TM_NEWINDEX)) == NULL && /* no metamethod; is there a previous entry in the table? */ (oldval != luaO_nilobject || /* no previous entry; must create one. (The next test is always true; we only need the assignment.) */ - (oldval = luaH_newkey(L, h, key), 1)))) { + (oldval = luaH_newkey(L, hvalue(t), key), 1))) { /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, oldval, val); /* assign new value to that entry */ - invalidateTMcache(h); - luaC_barrierback(L, h, val); + setobj2t(L, cast(TValue *, oldval), val); + invalidateTMcache(hvalue(t)); + luaC_barrierback(L, hvalue(t), val); return; } /* else will try the metamethod */ } - else /* not a table; check metamethod */ + else { /* not a table; check metamethod */ if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) luaG_typeerror(L, t, "index"); + } /* try the metamethod */ if (ttisfunction(tm)) { luaT_callTM(L, tm, t, key, val, 0); return; } t = tm; /* else repeat assignment over 'tm' */ + if (luaV_fastset(L, t, key, oldval, luaH_get, val)) + return; /* done */ + /* else loop */ } luaG_runerror(L, "settable chain too long; possible loop"); } @@ -453,6 +452,17 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) +/* copy strings in stack from top - n up to top - 1 to buffer */ +static void copy2buff (StkId top, int n, char *buff) { + size_t tl = 0; /* size already copied */ + do { + size_t l = vslen(top - n); /* length of string being copied */ + memcpy(buff + tl, svalue(top - n), l * sizeof(char)); + tl += l; + } while (--n > 0); +} + + /* ** Main operation for concatenation: concat 'total' values in the stack, ** from 'L->top - total' up to 'L->top - 1'. @@ -472,24 +482,24 @@ void luaV_concat (lua_State *L, int total) { else { /* at least two non-empty string values; get as many as possible */ size_t tl = vslen(top - 1); - char *buffer; - int i; - /* collect total length */ - for (i = 1; i < total && tostring(L, top-i-1); i++) { - size_t l = vslen(top - i - 1); + TString *ts; + /* collect total length and number of strings */ + for (n = 1; n < total && tostring(L, top - n - 1); n++) { + size_t l = vslen(top - n - 1); if (l >= (MAX_SIZE/sizeof(char)) - tl) luaG_runerror(L, "string length overflow"); tl += l; } - buffer = luaZ_openspace(L, &G(L)->buff, tl); - tl = 0; - n = i; - do { /* copy all strings to buffer */ - size_t l = vslen(top - i); - memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); - tl += l; - } while (--i > 0); - setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); /* create result */ + if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ + char buff[LUAI_MAXSHORTLEN]; + copy2buff(top, n, buff); /* copy strings to buffer */ + ts = luaS_newlstr(L, buff, tl); + } + else { /* long string; copy strings directly to final result */ + ts = luaS_createlngstrobj(L, tl); + copy2buff(top, n, getstr(ts)); + } + setsvalue2s(L, top - n, ts); /* create result */ } total -= n-1; /* got 'n' strings to create 1 new */ L->top -= n-1; /* popped 'n' strings and pushed one */ @@ -710,27 +720,20 @@ void luaV_finishOp (lua_State *L) { ** some macros for common tasks in 'luaV_execute' */ -#if !defined(luai_runtimecheck) -#define luai_runtimecheck(L, c) /* void */ -#endif - #define RA(i) (base+GETARG_A(i)) -/* to be used after possible stack reallocation */ #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) -#define KBx(i) \ - (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++))) /* execute a jump instruction */ #define dojump(ci,i,e) \ { int a = GETARG_A(i); \ - if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \ + if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \ ci->u.l.savedpc += GETARG_sBx(i) + e; } /* for test instructions, execute the jump instruction that follows it */ @@ -740,34 +743,49 @@ void luaV_finishOp (lua_State *L) { #define Protect(x) { {x;}; base = ci->u.l.base; } #define checkGC(L,c) \ - Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \ - luaC_step(L); \ - L->top = ci->top;}) /* restore top */ \ - luai_threadyield(L); ) + { luaC_condGC(L, L->top = (c), /* limit of live values */ \ + Protect(L->top = ci->top)); /* restore top */ \ + luai_threadyield(L); } #define vmdispatch(o) switch(o) #define vmcase(l) case l: #define vmbreak break + +/* +** copy of 'luaV_gettable', but protecting call to potential metamethod +** (which can reallocate the stack) +*/ +#define gettableProtected(L,t,k,v) { const TValue *aux; \ + if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ + else Protect(luaV_finishget(L,t,k,v,aux)); } + + +/* same for 'luaV_settable' */ +#define settableProtected(L,t,k,v) { const TValue *slot; \ + if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ + Protect(luaV_finishset(L,t,k,v,slot)); } + + + void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; LClosure *cl; TValue *k; StkId base; + ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); - cl = clLvalue(ci->func); - k = cl->p->k; - base = ci->u.l.base; + cl = clLvalue(ci->func); /* local reference to function's closure */ + k = cl->p->k; /* local reference to function's constant table */ + base = ci->u.l.base; /* local copy of function's base */ /* main loop of interpreter */ for (;;) { Instruction i = *(ci->u.l.savedpc++); StkId ra; - if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && - (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { + if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) Protect(luaG_traceexec(L)); - } /* WARNING: several calls may realloc the stack and invalidate 'ra' */ ra = RA(i); lua_assert(base == ci->u.l.base); @@ -807,17 +825,22 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_GETTABUP) { - int b = GETARG_B(i); - Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra)); + TValue *upval = cl->upvals[GETARG_B(i)]->v; + TValue *rc = RKC(i); + gettableProtected(L, upval, rc, ra); vmbreak; } vmcase(OP_GETTABLE) { - Protect(luaV_gettable(L, RB(i), RKC(i), ra)); + StkId rb = RB(i); + TValue *rc = RKC(i); + gettableProtected(L, rb, rc, ra); vmbreak; } vmcase(OP_SETTABUP) { - int a = GETARG_A(i); - Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i))); + TValue *upval = cl->upvals[GETARG_A(i)]->v; + TValue *rb = RKB(i); + TValue *rc = RKC(i); + settableProtected(L, upval, rb, rc); vmbreak; } vmcase(OP_SETUPVAL) { @@ -827,7 +850,9 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_SETTABLE) { - Protect(luaV_settable(L, ra, RKB(i), RKC(i))); + TValue *rb = RKB(i); + TValue *rc = RKC(i); + settableProtected(L, ra, rb, rc); vmbreak; } vmcase(OP_NEWTABLE) { @@ -841,9 +866,15 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_SELF) { + const TValue *aux; StkId rb = RB(i); - setobjs2s(L, ra+1, rb); - Protect(luaV_gettable(L, rb, RKC(i), ra)); + TValue *rc = RKC(i); + TString *key = tsvalue(rc); /* key must be a string */ + setobjs2s(L, ra + 1, rb); + if (luaV_fastget(L, rb, key, aux, luaH_getstr)) { + setobj2s(L, ra, aux); + } + else Protect(luaV_finishget(L, rb, rc, ra, aux)); vmbreak; } vmcase(OP_ADD) { @@ -1030,7 +1061,7 @@ void luaV_execute (lua_State *L) { StkId rb; L->top = base + c + 1; /* mark the end of concat operands */ Protect(luaV_concat(L, c - b + 1)); - ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */ + ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */ rb = base + b; setobjs2s(L, ra, rb); checkGC(L, (ra >= rb ? ra + 1 : rb)); @@ -1046,7 +1077,7 @@ void luaV_execute (lua_State *L) { TValue *rb = RKB(i); TValue *rc = RKC(i); Protect( - if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i)) + if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) ci->u.l.savedpc++; else donextjump(ci); @@ -1094,12 +1125,12 @@ void luaV_execute (lua_State *L) { lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ if (luaD_precall(L, ra, nresults)) { /* C function? */ - if (nresults >= 0) L->top = ci->top; /* adjust results */ - base = ci->u.l.base; + if (nresults >= 0) + L->top = ci->top; /* adjust results */ + Protect((void)0); /* update 'base' */ } else { /* Lua function */ ci = L->ci; - ci->callstatus |= CIST_REENTRY; goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; @@ -1109,8 +1140,9 @@ void luaV_execute (lua_State *L) { lua_checksig(L); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ - base = ci->u.l.base; + if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ + Protect((void)0); /* update 'base' */ + } else { /* tail call: put called frame (n) in place of caller one (o) */ CallInfo *nci = L->ci; /* called frame */ @@ -1138,8 +1170,8 @@ void luaV_execute (lua_State *L) { vmcase(OP_RETURN) { int b = GETARG_B(i); if (cl->p->sp->sizep > 0) luaF_close(L, base); - b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra)); - if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ + b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); + if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ return; /* external invocation: return */ else { /* invocation via reentry: continue execution */ ci = L->ci; @@ -1152,7 +1184,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_FORLOOP) { if (ttisinteger(ra)) { /* integer loop? */ lua_Integer step = ivalue(ra + 2); - lua_Integer idx = ivalue(ra) + step; /* increment index */ + lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */ lua_Integer limit = ivalue(ra + 1); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ @@ -1184,7 +1216,7 @@ void luaV_execute (lua_State *L) { /* all values are integer */ lua_Integer initv = (stopnow ? 0 : ivalue(init)); setivalue(plimit, ilimit); - setivalue(init, initv - ivalue(pstep)); + setivalue(init, intop(-, initv, ivalue(pstep))); } else { /* try making all values floats */ lua_Number ninit; lua_Number nlimit; lua_Number nstep; @@ -1207,7 +1239,7 @@ void luaV_execute (lua_State *L) { setobjs2s(L, cb+1, ra+1); setobjs2s(L, cb, ra); L->top = cb + 3; /* func. + 2 args (state and index) */ - Protect(luaD_call(L, cb, GETARG_C(i), 1)); + Protect(luaD_call(L, cb, GETARG_C(i))); L->top = ci->top; i = *(ci->u.l.savedpc++); /* go to next instruction */ ra = RA(i); @@ -1233,11 +1265,10 @@ void luaV_execute (lua_State *L) { lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); c = GETARG_Ax(*ci->u.l.savedpc++); } - luai_runtimecheck(L, ttistable(ra)); h = hvalue(ra); last = ((c-1)*LFIELDS_PER_FLUSH) + n; if (last > h->sizearray) /* needs more space? */ - luaH_resizearray(L, h, last); /* pre-allocate it at once */ + luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { TValue *val = ra+n; luaH_setint(L, h, last--, val); @@ -1257,23 +1288,21 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_VARARG) { - int b = GETARG_B(i) - 1; + int b = GETARG_B(i) - 1; /* required results */ int j; int n = cast_int(base - ci->func) - cl->p->sp->numparams - 1; + if (n < 0) /* less arguments than parameters? */ + n = 0; /* no vararg arguments */ if (b < 0) { /* B == 0? */ b = n; /* get all var. arguments */ Protect(luaD_checkstack(L, n)); ra = RA(i); /* previous call may change the stack */ L->top = ra + n; } - for (j = 0; j < b; j++) { - if (j < n) { - setobjs2s(L, ra + j, base - n + j); - } - else { - setnilvalue(ra + j); - } - } + for (j = 0; j < b && j < n; j++) + setobjs2s(L, ra + j, base - n + j); + for (; j < b; j++) /* complete required results with nil */ + setnilvalue(ra + j); vmbreak; } vmcase(OP_EXTRAARG) { diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index 0613826a..fd0e748d 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.35 2015/02/20 14:27:53 roberto Exp $ +** $Id: lvm.h,v 2.39 2015/09/09 13:44:07 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -48,15 +48,61 @@ #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) +/* +** fast track for 'gettable': 1 means 'aux' points to resulted value; +** 0 means 'aux' is metamethod (if 't' is a table) or NULL. 'f' is +** the raw get function to use. +*/ +#define luaV_fastget(L,t,k,aux,f) \ + (!ttistable(t) \ + ? (aux = NULL, 0) /* not a table; 'aux' is NULL and result is 0 */ \ + : (aux = f(hvalue(t), k), /* else, do raw access */ \ + !ttisnil(aux) ? 1 /* result not nil? 'aux' has it */ \ + : (aux = fasttm(L, hvalue(t)->metatable, TM_INDEX), /* get metamethod */\ + aux != NULL ? 0 /* has metamethod? must call it */ \ + : (aux = luaO_nilobject, 1)))) /* else, final result is nil */ + +/* +** standard implementation for 'gettable' +*/ +#define luaV_gettable(L,t,k,v) { const TValue *aux; \ + if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ + else luaV_finishget(L,t,k,v,aux); } + + +/* +** Fast track for set table. If 't' is a table and 't[k]' is not nil, +** call GC barrier, do a raw 't[k]=v', and return true; otherwise, +** return false with 'slot' equal to NULL (if 't' is not a table) or +** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro +** returns true, there is no need to 'invalidateTMcache', because the +** call is not creating a new entry. +*/ +#define luaV_fastset(L,t,k,slot,f,v) \ + (!ttistable(t) \ + ? (slot = NULL, 0) \ + : (slot = f(hvalue(t), k), \ + ttisnil(slot) ? 0 \ + : (luaC_barrierback(L, hvalue(t), v), \ + setobj2t(L, cast(TValue *,slot), v), \ + 1))) + + +#define luaV_settable(L,t,k,v) { const TValue *slot; \ + if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ + luaV_finishset(L,t,k,v,slot); } + + + LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); -LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, - StkId val); -LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, - StkId val); +LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, const TValue *tm); +LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, + StkId val, const TValue *oldval); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L); LUAI_FUNC void luaV_concat (lua_State *L, int total); diff --git a/3rd/lua/lzio.c b/3rd/lua/lzio.c index 46493920..c9e1f491 100644 --- a/3rd/lua/lzio.c +++ b/3rd/lua/lzio.c @@ -1,5 +1,5 @@ /* -** $Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp $ +** $Id: lzio.c,v 1.37 2015/09/08 15:41:05 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ @@ -66,13 +66,3 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) { return 0; } -/* ------------------------------------------------------------------------ */ -char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { - if (n > buff->buffsize) { - if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; - luaZ_resizebuffer(L, buff, n); - } - return buff->buffer; -} - - diff --git a/3rd/lua/lzio.h b/3rd/lua/lzio.h index b2e56bcd..e7b6f34b 100644 --- a/3rd/lua/lzio.h +++ b/3rd/lua/lzio.h @@ -1,5 +1,5 @@ /* -** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 roberto Exp $ +** $Id: lzio.h,v 1.31 2015/09/08 15:41:05 roberto Exp $ ** Buffered streams ** See Copyright Notice in lua.h */ @@ -44,7 +44,6 @@ typedef struct Mbuffer { #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) -LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data); LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ From 05605e62a996110354a5615cc18beb79452b6349 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 1 Dec 2015 17:09:32 +0800 Subject: [PATCH 532/729] core.querytype doesn't raise error, see issue #395 --- lualib-src/sproto/README.md | 10 +++++----- lualib-src/sproto/lsproto.c | 3 +-- lualib/sproto.lua | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index c38a3162..af33b331 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -340,8 +340,8 @@ data { } 02 00 (fn = 2) -01 00 (skip id) -00 00 (id = 2, value in data part) +01 00 (skip id = 0) +00 00 (id = 1, value in data part) 03 00 00 00 (sizeof bools) 00 (false) @@ -353,11 +353,11 @@ Example 6: ``` data { number = 100000, - bignumber = -1000000000, + bignumber = -10000000000, } 03 00 (fn = 3) -03 00 (skip id 0/1) +03 00 (skip id = 1) 00 00 (id = 2, value in data part) 00 00 (id = 3, value in data part) @@ -384,7 +384,7 @@ unpacked (hex): 08 00 00 00 03 00 02 00 19 00 00 00 aa 01 00 00 packed (hex): 51 08 03 02 31 19 aa 01 ``` -Tag 0xff is treated specially. A number N is following the 0xff tag. N means (N+1)*8 bytes should be copied directly. +Tag 0xff is treated specially. A number N is following the 0xff tag. N means (N+1)\*8 bytes should be copied directly. The bytes may or may not contain zeros. Because of this rule, the worst-case space overhead of packing is 2 bytes per 2 KiB of input. For example: diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 1d71d887..d1cca74e 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -97,8 +97,7 @@ lquerytype(lua_State *L) { lua_pushlightuserdata(L, st); return 1; } - - return luaL_error(L, "type %s not found", type_name); + return 0; } struct encode_ud { diff --git a/lualib/sproto.lua b/lualib/sproto.lua index d3ae40ad..9c6524b2 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -42,7 +42,7 @@ function sproto:host( packagename ) packagename = packagename or "package" local obj = { __proto = self, - __package = core.querytype(self.__cobj, packagename), + __package = assert(core.querytype(self.__cobj, packagename), "type package not found"), __session = {}, } return setmetatable(obj, host_mt) @@ -51,7 +51,7 @@ end local function querytype(self, typename) local v = self.__tcache[typename] if not v then - v = core.querytype(self.__cobj, typename) + v = assert(core.querytype(self.__cobj, typename), "type not found") self.__tcache[typename] = v end From 940a7ac58ce49e388b0bbc5537b313192c5637a3 Mon Sep 17 00:00:00 2001 From: snail Date: Tue, 1 Dec 2015 20:17:03 +0800 Subject: [PATCH 533/729] close file handle clearly Although 'f' would be closed after collected, maybe it's better to close it clearly --- lualib/dns.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/dns.lua b/lualib/dns.lua index d6658d63..318ba4c1 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -276,6 +276,7 @@ function dns.server(server, port) break end end + f:close() assert(server, "Can't get nameserver") end dns_server = socket.udp(function(str, from) From 1431f55d28e7b0e205d3c8402c20477f1210ac1b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 3 Dec 2015 15:37:43 +0800 Subject: [PATCH 534/729] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1f2bfb3b..16c544f2 100644 --- a/README.md +++ b/README.md @@ -28,13 +28,13 @@ Run these in different console ## About Lua -Skynet now use a modify version of lua 5.3.1 (http://www.lua.org/ftp/lua-5.3.1.tar.gz) . +Skynet now use a modify version of lua 5.3.2 ( http://www.lua.org/ftp/lua-5.3.2.tar.gz ) . For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html You can also use the other official Lua version , edit the makefile by yourself . -## How To (in Chinese) +## How To Use (Sorry, Only in Chinese now) * Read Wiki https://github.com/cloudwu/skynet/wiki * The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ From 3a2766dd0e6c6c58efad1620714b9a76d5bafb27 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 4 Dec 2015 10:47:30 +0800 Subject: [PATCH 535/729] return results, see pr #403 --- lualib/skynet/queue.lua | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lualib/skynet/queue.lua b/lualib/skynet/queue.lua index 3db244e3..1df1a6bb 100644 --- a/lualib/skynet/queue.lua +++ b/lualib/skynet/queue.lua @@ -8,6 +8,19 @@ function skynet.queue() local current_thread local ref = 0 local thread_queue = {} + + local function xpcall_ret(ok, ...) + ref = ref - 1 + if ref == 0 then + current_thread = table.remove(thread_queue,1) + if current_thread then + skynet.wakeup(current_thread) + end + end + assert(ok, (...)) + return ... + end + return function(f, ...) local thread = coroutine.running() if current_thread and current_thread ~= thread then @@ -18,15 +31,7 @@ function skynet.queue() current_thread = thread ref = ref + 1 - local ok, err = xpcall(f, traceback, ...) - ref = ref - 1 - if ref == 0 then - current_thread = table.remove(thread_queue,1) - if current_thread then - skynet.wakeup(current_thread) - end - end - assert(ok,err) + return xpcall_ret(xpcall(f, traceback, ...)) end end From d4d188541fb3975ce74725e7dd5945d69063ffa3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 6 Dec 2015 16:48:28 +0800 Subject: [PATCH 536/729] add socket.close_fd --- examples/login/logind.lua | 2 +- lualib/snax/loginserver.lua | 34 +++++++++++++++++++--------------- lualib/socket.lua | 5 +++++ skynet-src/socket_server.c | 2 ++ 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/examples/login/logind.lua b/examples/login/logind.lua index afaa3496..dd003f2f 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -19,7 +19,7 @@ function server.auth_handler(token) user = crypt.base64decode(user) server = crypt.base64decode(server) password = crypt.base64decode(password) - assert(password == "password") + assert(password == "password", "Invalid password") return server, user end diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index dfaa3ebd..37f335b6 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -49,9 +49,6 @@ end local function launch_slave(auth_handler) local function auth(fd, addr) - skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) - socket.start(fd) - -- set socket buffer limit (8K) -- If the attacker send large package, close the socket socket.limit(fd, 8192) @@ -86,24 +83,32 @@ local function launch_slave(auth_handler) return ok, server, uid, secret end - local function ret_pack(fd, ok, err, ...) - socket.abandon(fd) + local function ret_pack(ok, err, ...) if ok then - skynet.ret(skynet.pack(err, ...)) + return skynet.pack(err, ...) else if err == socket_error then - skynet.ret(skynet.pack(nil, "socket error")) + return skynet.pack(nil, "socket error") else - skynet.ret(skynet.pack(false, err)) + return skynet.pack(false, err) end end end - skynet.dispatch("lua", function(_,_,fd,...) - if type(fd) ~= "number" then - skynet.ret(skynet.pack(false, "invalid fd type")) + local function auth_fd(fd, addr) + skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) + socket.start(fd) -- may raise error here + local msg, len = ret_pack(pcall(auth, fd, addr)) + socket.abandon(fd) -- never raise error here + return msg, len + end + + skynet.dispatch("lua", function(_,_,...) + local ok, msg, len = pcall(auth_fd, ...) + if ok then + return skynet.ret(msg,len) else - ret_pack(fd,pcall(auth, fd, ...)) + return skynet.ret(skynet.pack(false, msg)) end end) end @@ -113,7 +118,7 @@ local user_login = {} local function accept(conf, s, fd, addr) -- call slave auth local ok, server, uid, secret = skynet.call(s, "lua", fd, addr) - socket.start(fd) + -- slave will accept(start) fd, so we can write to fd later if not ok then if ok ~= nil then @@ -173,9 +178,8 @@ local function launch_master(conf) if err ~= socket_error then skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err)) end - socket.start(fd) end - socket.close(fd) + socket.close_fd(fd) -- We haven't call socket.start, so use socket.close_fd rather than socket.close. end) end diff --git a/lualib/socket.lua b/lualib/socket.lua index bc9701ae..b2f2ac15 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -227,6 +227,11 @@ function socket.shutdown(id) close_fd(id, driver.shutdown) end +function socket.close_fd(id) + assert(socket_pool[id] == nil,"Use socket.close instead") + driver.close(id) +end + function socket.close(id) local s = socket_pool[id] if s == nil then diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index a3326b05..89ba11f9 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -841,10 +841,12 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc result->data = "start"; return SOCKET_OPEN; } else if (s->type == SOCKET_TYPE_CONNECTED) { + // todo: maybe we should send a message SOCKET_TRANSFER to s->opaque s->opaque = request->opaque; result->data = "transfer"; return SOCKET_OPEN; } + // if s->type == SOCKET_TYPE_HALFCLOSE , SOCKET_CLOSE message will send later return -1; } From 03617f23a16e0a0cf9e07a5e2661ef57fd99146f Mon Sep 17 00:00:00 2001 From: linse073 Date: Mon, 7 Dec 2015 21:00:19 +0800 Subject: [PATCH 537/729] =?UTF-8?q?loginserver.lua=E4=B8=AD=E7=9A=84launch?= =?UTF-8?q?=5Fslave=E7=9A=84lua=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84return=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之å‰å¯èƒ½æ˜¯æˆ‘没有表述清楚,这两个returnæˆ‘çŒœæƒ³æ˜¯ä¸æ˜¯æ‰‹è¯¯åŠ ä¸ŠåŽ»çš„ï¼Œå› ä¸ºä¹‹å‰éƒ½æ²¡æœ‰è§è¿‡éœ€è¦åœ¨æ¶ˆæ¯å¤„ç†å‡½æ•°é‡Œé¢return skynet.retè°ƒç”¨çš„ç»“æžœï¼Œå®žé™…åŽ»æŽ‰åŽæµ‹è¯•ä¹Ÿæ˜¯æ²¡æœ‰é—®é¢˜çš„ï¼Œå’Œæ˜¯ä¸æ˜¯ä¸¤æ¬¡pcall的调用应该是没有关系的。 --- lualib/snax/loginserver.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 37f335b6..cd74fcb5 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -106,9 +106,9 @@ local function launch_slave(auth_handler) skynet.dispatch("lua", function(_,_,...) local ok, msg, len = pcall(auth_fd, ...) if ok then - return skynet.ret(msg,len) + skynet.ret(msg,len) else - return skynet.ret(skynet.pack(false, msg)) + skynet.ret(skynet.pack(false, msg)) end end) end From ccb9cdbc9d5d74165e994e1dab65c9acd27e954f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 14 Dec 2015 12:25:26 +0800 Subject: [PATCH 538/729] update sproto schema --- lualib-src/sproto/README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index af33b331..ea5bc7be 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -158,10 +158,11 @@ A schema text can be self-described by the sproto schema language. .type { .field { name 0 : string - type 1 : string - id 2 : integer - array 3 : boolean - key 4 : integer # optional tag for map + buildin 1 : integer + type 2 : integer + tag 3 : integer + array 4 : boolean + key 5 : integer # If key exists, array must be true, and it's a map. } name 0 : string fields 1 : *field @@ -169,9 +170,9 @@ A schema text can be self-described by the sproto schema language. .protocol { name 0 : string - id 1 : integer - request 2 : string - response 3 : string + tag 1 : integer + request 2 : integer # index + response 3 : integer # index } .group { From bfe8eeb4b390bf19767b880b5cba0a5475edf495 Mon Sep 17 00:00:00 2001 From: Kezhu Wang Date: Mon, 14 Dec 2015 15:17:40 +0800 Subject: [PATCH 539/729] Fix dangling c pointer to lua string value Lua string object referenced by C pointer from lua_tolstring() was removed from stack by lua_settop(L,0). Lua 5.3 Reference Manual says: > Because Lua has garbage collection, there is no guarantee that the > pointer returned by lua_tolstring will be valid after the > corresponding Lua value is removed from the stack. That is it. Introduced in commit 9937081854c7b65d0a0557c3630ecbf1d62622d8. --- lualib-src/lua-seri.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index c0761bd5..d80452b6 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -572,7 +572,7 @@ _luaseri_unpack(lua_State *L) { return luaL_error(L, "deserialize null pointer"); } - lua_settop(L,0); + lua_settop(L,1); struct read_block rb; rball_init(&rb, buffer, len); @@ -591,7 +591,7 @@ _luaseri_unpack(lua_State *L) { // Need not free buffer - return lua_gettop(L); + return lua_gettop(L) - 1; } int From 4f8427a23fbe0389c5938c6dc567a21d92c58a4d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Dec 2015 10:04:38 +0800 Subject: [PATCH 540/729] close socket when sp_add failed --- skynet-src/socket_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 89ba11f9..8caf499f 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -832,7 +832,7 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc } if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { if (sp_add(ss->event_fd, s->fd, s)) { - s->type = SOCKET_TYPE_INVALID; + force_close(ss, s, result); result->data = strerror(errno); return SOCKET_ERROR; } From a9fc57756099e7bc766ed84d380fc711a8c8d872 Mon Sep 17 00:00:00 2001 From: David Feng Date: Thu, 17 Dec 2015 15:53:39 +0800 Subject: [PATCH 541/729] fix typo fix a little mistake --- lualib/socket.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/socket.lua b/lualib/socket.lua index b2f2ac15..5ea222e0 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -253,7 +253,7 @@ function socket.close(id) s.connected = false end close_fd(id) -- clear the buffer (already close fd) - assert(s.lock_set == nil or next(s.lock_set) == nil) + assert(s.lock == nil or next(s.lock) == nil) socket_pool[id] = nil end From 872491e9680a3d83c8a686e6a4edd7fea71fd440 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Dec 2015 16:25:24 +0800 Subject: [PATCH 542/729] Add skynet.coroutine module --- lualib/skynet.lua | 24 ++++---- lualib/skynet/coroutine.lua | 108 ++++++++++++++++++++++++++++++++++++ test/testcoroutine.lua | 24 ++++++++ 3 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 lualib/skynet/coroutine.lua create mode 100644 test/testcoroutine.lua diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 97a7f6b9..c0a615ee 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -8,8 +8,8 @@ local pcall = pcall local profile = require "profile" -coroutine.resume = profile.resume -coroutine.yield = profile.yield +local coroutine_resume = profile.resume +local coroutine_yield = profile.yield local proto = {} local skynet = { @@ -69,7 +69,7 @@ local function dispatch_error_queue() if session then local co = session_id_coroutine[session] session_id_coroutine[session] = nil - return suspend(co, coroutine.resume(co, false)) + return suspend(co, coroutine_resume(co, false)) end end @@ -96,7 +96,6 @@ end -- coroutine reuse local coroutine_pool = {} -local coroutine_yield = coroutine.yield local function co_create(f) local co = table.remove(coroutine_pool) @@ -111,7 +110,7 @@ local function co_create(f) end end) else - coroutine.resume(co, f) + coroutine_resume(co, f) end return co end @@ -123,7 +122,7 @@ local function dispatch_wakeup() local session = sleep_session[co] if session then session_id_coroutine[session] = "BREAK" - return suspend(co, coroutine.resume(co, false, "BREAK")) + return suspend(co, coroutine_resume(co, false, "BREAK")) end end end @@ -178,7 +177,7 @@ function suspend(co, result, command, param, size) c.trash(param, size) ret = false end - return suspend(co, coroutine.resume(co, ret)) + return suspend(co, coroutine_resume(co, ret)) elseif command == "RESPONSE" then local co_session = session_coroutine_id[co] local co_address = session_coroutine_address[co] @@ -227,7 +226,7 @@ function suspend(co, result, command, param, size) watching_service[co_address] = watching_service[co_address] + 1 session_response[co] = true unresponse[response] = true - return suspend(co, coroutine.resume(co, response)) + return suspend(co, coroutine_resume(co, response)) elseif command == "EXIT" then -- coroutine exit local address = session_coroutine_address[co] @@ -238,6 +237,9 @@ function suspend(co, result, command, param, size) elseif command == "QUIT" then -- service exit return + elseif command == "USER" then + -- See skynet.coutine for detail + error("Call skynet.coroutine.yield out of skynet.coroutine.resume\n" .. debug.traceback(co)) elseif command == nil then -- debug trace return @@ -468,7 +470,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) unknown_response(session, source, msg, sz) else session_id_coroutine[session] = nil - suspend(co, coroutine.resume(co, true, msg, sz)) + suspend(co, coroutine_resume(co, true, msg, sz)) end else local p = proto[prototype] @@ -491,7 +493,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) local co = co_create(f) session_coroutine_id[co] = session session_coroutine_address[co] = source - suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...))) + suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz, ...))) else unknown_request(session, source, msg, sz, proto[prototype].name) end @@ -506,7 +508,7 @@ function skynet.dispatch_message(...) break end fork_queue[key] = nil - local fork_succ, fork_err = pcall(suspend,co,coroutine.resume(co)) + local fork_succ, fork_err = pcall(suspend,co,coroutine_resume(co)) if not fork_succ then if succ then succ = false diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua new file mode 100644 index 00000000..69cd3130 --- /dev/null +++ b/lualib/skynet/coroutine.lua @@ -0,0 +1,108 @@ +-- You should use this module (skynet.coroutine) instead of origin lua coroutine in skynet framework + +local coroutine = coroutine +-- origin lua coroutine module +local coroutine_resume = coroutine.resume +local coroutine_yield = coroutine.yield +local coroutine_status = coroutine.status + +local select = select +local skynetco = {} + +skynetco.create = coroutine.create +skynetco.isyieldable = coroutine.isyieldable +skynetco.running = coroutine.running +skynetco.status = coroutine.status + +local skynet_coroutines = setmetatable({}, { __mode = "kv" }) + +function skynetco.create(f) + local co = coroutine.create(f) + -- mark co as a skynet coroutine + skynet_coroutines[co] = true + return co +end + +function skynetco.isskynetcoroutine(co) + co = co or coroutine.running() + return skynet_coroutines[co] ~= nil +end + +do -- begin skynetco.resume + + local profile = require "profile" + -- skynet use profile.resume/yield instead of coroutine.resume/yield + -- read skynet.lua for detail + local skynet_resume = profile.resume + local skynet_yield = profile.yield + + local function unlock(co, ...) + skynet_coroutines[co] = true + return ... + end + + local function skynet_yielding(co, ...) + skynet_coroutines[co] = false + return unlock(co, skynet_resume(co, skynet_yield(...))) + end + + local function resume(co, ok, ...) + if not ok then + return ok, ... + elseif coroutine_status(co) == "dead" then + -- the main function exit + skynet_coroutines[co] = nil + return true, ... + elseif (...) == "USER" then + return true, select(2, ...) + else + -- blocked in skynet framework, so raise the yielding message + return resume(co, skynet_yielding(co, ...)) + end + end + +function skynetco.resume(co, ...) + local co_status = skynet_coroutines[co] + if not co_status then + if co_status == false then + -- is running + return false, "cannot resume a skynet coroutine suspend by skynet framework" + end + if coroutine_status(co) == "dead" then + -- always return false, "cannot resume dead coroutine" + return coroutine_resume(co, ...) + else + return false, "cannot resume none skynet coroutine" + end + end + return resume(co, coroutine_resume(co, ...)) +end + +end -- end of skynetco.resume + +function skynetco.yield(...) + return coroutine_yield("USER", ...) +end + +do -- begin skynetco.wrap + + local function wrap_co(ok, ...) + if ok then + return ... + else + error(...) + end + end + +function skynetco.wrap(f) + local co = skynetco.create(function(...) + return f(...) + end) + return function(...) + return wrap_co(skynetco.resume(co, ...)) + end +end + +end -- end of skynetco.wrap + +return skynetco diff --git a/test/testcoroutine.lua b/test/testcoroutine.lua new file mode 100644 index 00000000..78a433a6 --- /dev/null +++ b/test/testcoroutine.lua @@ -0,0 +1,24 @@ +local skynet = require "skynet" +-- You should use skynet.coroutine instead of origin coroutine in skynet +local coroutine = require "skynet.coroutine" + +local function test(n) + print ("begin", coroutine.isskynetcoroutine()) + for i=1,n do + skynet.sleep(100) + coroutine.yield(i) + end + print "end" + return false +end + +skynet.start(function() + print("Is the main thead a skynet coroutine ?", coroutine.isskynetcoroutine(coroutine.running())) -- always false + print(coroutine.resume(coroutine.running())) -- always return false + local f = coroutine.wrap(test) + repeat + local n = f(5) + print(n) + until not n + skynet.exit() +end) From 4a80a75fd6196b5ffe30ebb224c335a8f5133e74 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Dec 2015 17:11:54 +0800 Subject: [PATCH 543/729] skynet.coroutine.status may return blocked --- lualib/skynet/coroutine.lua | 13 +++++++++++++ test/testcoroutine.lua | 28 +++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua index 69cd3130..1d30ab30 100644 --- a/lualib/skynet/coroutine.lua +++ b/lualib/skynet/coroutine.lua @@ -80,6 +80,19 @@ end end -- end of skynetco.resume +function skynetco.status(co) + local status = coroutine.status(co) + if status == "suspended" then + if skynet_coroutines[co] == false then + return "blocked" + else + return "suspended" + end + else + return status + end +end + function skynetco.yield(...) return coroutine_yield("USER", ...) end diff --git a/test/testcoroutine.lua b/test/testcoroutine.lua index 78a433a6..1bab5d2c 100644 --- a/test/testcoroutine.lua +++ b/test/testcoroutine.lua @@ -2,23 +2,37 @@ local skynet = require "skynet" -- You should use skynet.coroutine instead of origin coroutine in skynet local coroutine = require "skynet.coroutine" +local function status(co) + repeat + local status = coroutine.status(co) + print("STATUS", status) + skynet.sleep(100) + until status == "suspended" + + repeat + local ok, n = assert(coroutine.resume(co)) + print("status thread", n) + until not n + skynet.exit() +end + local function test(n) - print ("begin", coroutine.isskynetcoroutine()) + local co = coroutine.running() + print ("begin", coroutine.isskynetcoroutine(co)) + skynet.fork(status, co) for i=1,n do skynet.sleep(100) coroutine.yield(i) end print "end" - return false end skynet.start(function() - print("Is the main thead a skynet coroutine ?", coroutine.isskynetcoroutine(coroutine.running())) -- always false + print("Is the main thead a skynet coroutine ?", coroutine.isskynetcoroutine()) -- always false print(coroutine.resume(coroutine.running())) -- always return false local f = coroutine.wrap(test) - repeat + for i=1,3 do local n = f(5) - print(n) - until not n - skynet.exit() + print("main thread",n) + end end) From 9d6bde01a3498de6b4020d4a287b5a3fe065ed2b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Dec 2015 20:41:02 +0800 Subject: [PATCH 544/729] profile support skynet coroutine --- lualib-src/lua-profile.c | 99 ++++++++++++++++++++++++++++++++----- lualib/skynet/coroutine.lua | 39 +++++++++------ test/testcoroutine.lua | 25 ++++++++-- 3 files changed, 133 insertions(+), 30 deletions(-) diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c index f0f11270..0c196106 100644 --- a/lualib-src/lua-profile.c +++ b/lualib-src/lua-profile.c @@ -1,3 +1,4 @@ +#include #include #include @@ -11,6 +12,8 @@ #define NANOSEC 1000000000 #define MICROSEC 1000000 +// #define DEBUG_LOG + static double get_time() { #if !defined(__APPLE__) @@ -47,7 +50,11 @@ diff_time(double start) { static int lstart(lua_State *L) { - lua_pushthread(L); + if (lua_type(L,1) == LUA_TTHREAD) { + lua_settop(L,1); + } else { + lua_pushthread(L); + } lua_rawget(L, lua_upvalueindex(2)); if (!lua_isnil(L, -1)) { return luaL_error(L, "Thread %p start profile more than once", lua_topointer(L, 1)); @@ -57,7 +64,11 @@ lstart(lua_State *L) { lua_rawset(L, lua_upvalueindex(2)); lua_pushthread(L); - lua_pushnumber(L, get_time()); + double ti = get_time(); +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] start\n", L); +#endif + lua_pushnumber(L, ti); lua_rawset(L, lua_upvalueindex(1)); return 0; @@ -65,9 +76,15 @@ lstart(lua_State *L) { static int lstop(lua_State *L) { - lua_pushthread(L); + if (lua_type(L,1) == LUA_TTHREAD) { + lua_settop(L,1); + } else { + lua_pushthread(L); + } lua_rawget(L, lua_upvalueindex(1)); - luaL_checktype(L, -1, LUA_TNUMBER); + if (lua_type(L, -1) != LUA_TNUMBER) { + return luaL_error(L, "Call profile.start() before profile.stop()"); + } double ti = diff_time(lua_tonumber(L, -1)); lua_pushthread(L); lua_rawget(L, lua_upvalueindex(2)); @@ -81,14 +98,20 @@ lstop(lua_State *L) { lua_pushnil(L); lua_rawset(L, lua_upvalueindex(2)); - lua_pushnumber(L, ti + total_time); + total_time += ti; + lua_pushnumber(L, total_time); +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] stop (%lf / %lf)\n", L, ti, total_time); +#endif return 1; } static int -lresume(lua_State *L) { - lua_pushvalue(L,1); +timing_resume(lua_State *L) { +#ifdef DEBUG_LOG + lua_State *from = lua_tothread(L, -1); +#endif lua_rawget(L, lua_upvalueindex(2)); if (lua_isnil(L, -1)) { // check total time lua_pop(L,1); @@ -96,6 +119,9 @@ lresume(lua_State *L) { lua_pop(L,1); lua_pushvalue(L,1); double ti = get_time(); +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] resume\n", from); +#endif lua_pushnumber(L, ti); lua_rawset(L, lua_upvalueindex(1)); // set start time } @@ -106,8 +132,25 @@ lresume(lua_State *L) { } static int -lyield(lua_State *L) { - lua_pushthread(L); +lresume(lua_State *L) { + lua_pushvalue(L,1); + + return timing_resume(L); +} + +static int +lresume_co(lua_State *L) { + luaL_checktype(L, 2, LUA_TTHREAD); + lua_rotate(L, 2, -1); + + return timing_resume(L); +} + +static int +timing_yield(lua_State *L) { +#ifdef DEBUG_LOG + lua_State *from = lua_tothread(L, -1); +#endif lua_rawget(L, lua_upvalueindex(2)); // check total time if (lua_isnil(L, -1)) { lua_pop(L,1); @@ -120,7 +163,11 @@ lyield(lua_State *L) { double starttime = lua_tonumber(L, -1); lua_pop(L,1); - ti += diff_time(starttime); + double diff = diff_time(starttime); + ti += diff; +#ifdef DEBUG_LOG + fprintf(stderr, "PROFILE [%p] yield (%lf/%lf)\n", from, diff, ti); +#endif lua_pushthread(L); lua_pushnumber(L, ti); @@ -132,6 +179,21 @@ lyield(lua_State *L) { return co_yield(L); } +static int +lyield(lua_State *L) { + lua_pushthread(L); + + return timing_yield(L); +} + +static int +lyield_co(lua_State *L) { + luaL_checktype(L, 1, LUA_TTHREAD); + lua_rotate(L, 1, -1); + + return timing_yield(L); +} + int luaopen_profile(lua_State *L) { luaL_checkversion(L); @@ -140,6 +202,8 @@ luaopen_profile(lua_State *L) { { "stop", lstop }, { "resume", lresume }, { "yield", lyield }, + { "resume_co", lresume_co }, + { "yield_co", lyield_co }, { NULL, NULL }, }; luaL_newlibtable(L,l); @@ -154,7 +218,7 @@ luaopen_profile(lua_State *L) { lua_setmetatable(L, -3); lua_setmetatable(L, -3); - lua_pushnil(L); + lua_pushnil(L); // cfunction (coroutine.resume or coroutine.yield) luaL_setfuncs(L,l,3); int libtable = lua_gettop(L); @@ -166,20 +230,33 @@ luaopen_profile(lua_State *L) { if (co_resume == NULL) return luaL_error(L, "Can't get coroutine.resume"); lua_pop(L,1); + lua_getfield(L, libtable, "resume"); lua_pushcfunction(L, co_resume); lua_setupvalue(L, -2, 3); lua_pop(L,1); + lua_getfield(L, libtable, "resume_co"); + lua_pushcfunction(L, co_resume); + lua_setupvalue(L, -2, 3); + lua_pop(L,1); + lua_getfield(L, -1, "yield"); lua_CFunction co_yield = lua_tocfunction(L, -1); if (co_yield == NULL) return luaL_error(L, "Can't get coroutine.yield"); lua_pop(L,1); + lua_getfield(L, libtable, "yield"); lua_pushcfunction(L, co_yield); lua_setupvalue(L, -2, 3); + lua_pop(L,1); + + lua_getfield(L, libtable, "yield_co"); + lua_pushcfunction(L, co_yield); + lua_setupvalue(L, -2, 3); + lua_pop(L,1); lua_settop(L, libtable); diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua index 1d30ab30..af2e1fd2 100644 --- a/lualib/skynet/coroutine.lua +++ b/lualib/skynet/coroutine.lua @@ -5,6 +5,7 @@ local coroutine = coroutine local coroutine_resume = coroutine.resume local coroutine_yield = coroutine.yield local coroutine_status = coroutine.status +local coroutine_running = coroutine.running local select = select local skynetco = {} @@ -23,30 +24,25 @@ function skynetco.create(f) return co end -function skynetco.isskynetcoroutine(co) - co = co or coroutine.running() - return skynet_coroutines[co] ~= nil -end - do -- begin skynetco.resume local profile = require "profile" - -- skynet use profile.resume/yield instead of coroutine.resume/yield - -- read skynet.lua for detail - local skynet_resume = profile.resume - local skynet_yield = profile.yield + -- skynet use profile.resume_co/yield_co instead of coroutine.resume/yield + + local skynet_resume = profile.resume_co + local skynet_yield = profile.yield_co local function unlock(co, ...) skynet_coroutines[co] = true return ... end - local function skynet_yielding(co, ...) + local function skynet_yielding(co, from, ...) skynet_coroutines[co] = false - return unlock(co, skynet_resume(co, skynet_yield(...))) + return unlock(co, skynet_resume(co, from, skynet_yield(from, ...))) end - local function resume(co, ok, ...) + local function resume(co, from, ok, ...) if not ok then return ok, ... elseif coroutine_status(co) == "dead" then @@ -57,10 +53,13 @@ do -- begin skynetco.resume return true, select(2, ...) else -- blocked in skynet framework, so raise the yielding message - return resume(co, skynet_yielding(co, ...)) + return resume(co, from, skynet_yielding(co, from, ...)) end end + -- record the root of coroutine caller (It should be a skynet thread) + local coroutine_caller = setmetatable({} , { __mode = "kv" }) + function skynetco.resume(co, ...) local co_status = skynet_coroutines[co] if not co_status then @@ -75,7 +74,19 @@ function skynetco.resume(co, ...) return false, "cannot resume none skynet coroutine" end end - return resume(co, coroutine_resume(co, ...)) + local from = coroutine_running() + local caller = coroutine_caller[from] or from + coroutine_caller[co] = caller + return resume(co, caller, coroutine_resume(co, ...)) +end + +function skynetco.thread(co) + co = co or coroutine_running() + if skynet_coroutines[co] ~= nil then + return coroutine_caller[co] , false + else + return co, true + end end end -- end of skynetco.resume diff --git a/test/testcoroutine.lua b/test/testcoroutine.lua index 1bab5d2c..5a698575 100644 --- a/test/testcoroutine.lua +++ b/test/testcoroutine.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" -- You should use skynet.coroutine instead of origin coroutine in skynet local coroutine = require "skynet.coroutine" +local profile = require "profile" local function status(co) repeat @@ -18,21 +19,35 @@ end local function test(n) local co = coroutine.running() - print ("begin", coroutine.isskynetcoroutine(co)) + print ("begin", co, coroutine.thread(co)) -- false skynet.fork(status, co) for i=1,n do skynet.sleep(100) coroutine.yield(i) end - print "end" + print ("end", co) end -skynet.start(function() - print("Is the main thead a skynet coroutine ?", coroutine.isskynetcoroutine()) -- always false - print(coroutine.resume(coroutine.running())) -- always return false +local function main() local f = coroutine.wrap(test) + coroutine.yield "begin" for i=1,3 do local n = f(5) print("main thread",n) end + coroutine.yield "end" + print("main thread time:", profile.stop(coroutine.thread())) +end + +skynet.start(function() + print("Main thead :", coroutine.thread()) -- true + print(coroutine.resume(coroutine.running())) -- always return false + + profile.start() + + local f = coroutine.wrap(main) + print("main step", f()) + print("main step", f()) + print("main step", f()) +-- print("main thread time:", profile.stop()) end) From 20610723a10eda9913b563b5fb65a8bac35c473c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Dec 2015 22:13:51 +0800 Subject: [PATCH 545/729] add debug api to show current service c memory --- lualib-src/lua-memory.c | 7 +++++++ skynet-src/malloc_hook.c | 22 ++++++++++++++++++++++ skynet-src/malloc_hook.h | 1 + skynet-src/skynet.h | 1 + 4 files changed, 31 insertions(+) diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index a3afecfc..89e2996f 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -41,6 +41,12 @@ lexpandshrtbl(lua_State *L) { return 0; } +static int +lcurrent(lua_State *L) { + lua_pushinteger(L, malloc_current_memory()); + return 1; +} + int luaopen_memory(lua_State *L) { luaL_checkversion(L); @@ -53,6 +59,7 @@ luaopen_memory(lua_State *L) { { "info", dump_mem_lua }, { "ssinfo", luaS_shrinfo }, { "ssexpand", lexpandshrtbl }, + { "current", lcurrent }, { NULL, NULL }, }; diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 06e3bbf7..6cad2a9a 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "malloc_hook.h" #include "skynet.h" @@ -248,3 +249,24 @@ dump_mem_lua(lua_State *L) { } return 1; } + +size_t +malloc_current_memory(void) { + uint32_t handle = skynet_current_handle(); + int i; + for(i=0; ihandle == (uint32_t)handle && data->allocated != 0) { + return (size_t) data->allocated; + } + } + return 0; +} + +void +skynet_debug_memory(const char *info) { + // for debug use + uint32_t handle = skynet_current_handle(); + size_t mem = malloc_current_memory(); + fprintf(stderr, "[:%08x] %s %p\n", handle, info, (void *)mem); +} diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index 03339556..4da4123a 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -11,6 +11,7 @@ extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); extern void dump_c_mem(void); extern int dump_mem_lua(lua_State *L); +extern size_t malloc_current_memory(void); #endif /* SKYNET_MALLOC_HOOK_H */ diff --git a/skynet-src/skynet.h b/skynet-src/skynet.h index 26df5ac6..7d3e478a 100644 --- a/skynet-src/skynet.h +++ b/skynet-src/skynet.h @@ -39,5 +39,6 @@ void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb); uint32_t skynet_current_handle(void); uint64_t skynet_now(void); +void skynet_debug_memory(const char *info); // for debug use, output current service memory to stderr #endif From 7acf6d8767e246eefee373452d75a421c5071d24 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 18 Dec 2015 23:28:55 +0800 Subject: [PATCH 546/729] remove unused varargs --- lualib/skynet.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index c0a615ee..6d7e3141 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -460,7 +460,7 @@ function skynet.fork(func,...) return co end -local function raw_dispatch_message(prototype, msg, sz, session, source, ...) +local function raw_dispatch_message(prototype, msg, sz, session, source) -- skynet.PTYPE_RESPONSE = 1, read skynet.h if prototype == 1 then local co = session_id_coroutine[session] @@ -493,7 +493,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) local co = co_create(f) session_coroutine_id[co] = session session_coroutine_address[co] = source - suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz, ...))) + suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz))) else unknown_request(session, source, msg, sz, proto[prototype].name) end From 5a1b2e51f723722b2e6774fe8021da4b45df40a2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 23 Dec 2015 19:55:42 +0800 Subject: [PATCH 547/729] exit dispatch_by_order when close channel --- lualib/socketchannel.lua | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 31a339c5..7a9b74fc 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -68,14 +68,21 @@ local function wakeup_all(self, errmsg) for i = 1, #self.__thread do local co = self.__thread[i] self.__thread[i] = nil - self.__result[co] = socket_error - self.__result_data[co] = errmsg - skynet.wakeup(co) + if co then -- ignore the close signal + self.__result[co] = socket_error + self.__result_data[co] = errmsg + skynet.wakeup(co) + end end end end - +local function exit_thread(self) + local co = coroutine.running() + if self.__dispatch_thread == co then + self.__dispatch_thread = nil + end +end local function dispatch_by_session(self) local response = self.__response @@ -113,6 +120,7 @@ local function dispatch_by_session(self) wakeup_all(self, errormsg) end end + exit_thread(self) end local function pop_response(self) @@ -144,6 +152,11 @@ end local function dispatch_by_order(self) while self.__sock do local func, co = pop_response(self) + if not co then + -- close signal + wakeup_all(self, errmsg) + break + end local ok, result_ok, result_data, padding = pcall(func, self.__sock) if ok then if padding and result_ok then @@ -173,6 +186,7 @@ local function dispatch_by_order(self) wakeup_all(self, errmsg) end end + exit_thread(self) end local function dispatch_function(self) @@ -221,7 +235,7 @@ local function connect_once(self) end self.__sock = setmetatable( {fd} , channel_socket_meta ) - skynet.fork(dispatch_function(self), self) + self.__dispatch_thread = skynet.fork(dispatch_function(self), self) if self.__auth then self.__authcoroutine = coroutine.running() @@ -385,8 +399,13 @@ end function channel:close() if not self.__closed then + local thread = assert(self.__dispatch_thread) self.__closed = true close_channel_socket(self) + if not self.__response and self.__dispatch_thread == thread then + -- dispatch by order, send close signal to dispatch thread + push_response(self, true, false) -- (true, false) is close signal + end end end From f0ac66787eb5ade795dd919140390ffb62ef6b36 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 23 Dec 2015 20:22:46 +0800 Subject: [PATCH 548/729] socket channel connect should wait for last closing --- lualib/socketchannel.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 7a9b74fc..cf918645 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -81,6 +81,10 @@ local function exit_thread(self) local co = coroutine.running() if self.__dispatch_thread == co then self.__dispatch_thread = nil + local connecting = self.__connecting_thread + if connecting then + skynet.wakeup(connecting) + end end end @@ -332,6 +336,14 @@ end function channel:connect(once) if self.__closed then + if self.__dispatch_thread then + -- closing, wait + assert(self.__connecting_thread == nil, "already connecting") + local co = coroutine.running() + self.__connecting_thread = co + skynet.wait(co) + self.__connecting_thread = nil + end self.__closed = false end From b9ad559992b68228848270bb0dd5255bab31d38e Mon Sep 17 00:00:00 2001 From: m2q1n9 Date: Fri, 25 Dec 2015 17:24:50 +0800 Subject: [PATCH 549/729] fix a debug_console error --- service/debug_console.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 37b20c30..2ff5adce 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -231,7 +231,7 @@ function COMMAND.debug(address, fd) skynet.fork(function() repeat local cmdline = socket.readline(fd, "\n") - cmdline = cmdline:gsub("(.*)\r$", "%1") + cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") if not cmdline then skynet.send(agent, "lua", "cmd", "cont") break @@ -275,5 +275,3 @@ function COMMAND.shrtbl() local n, total, longest, space = memory.ssinfo() return { n = n, total = total, longest = longest, space = space } end - - From 0c4ea817092f2613f3637503b6f49d8fa722f710 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 27 Dec 2015 19:31:32 +0800 Subject: [PATCH 550/729] EAGAIN and EWOULDBLOCK may be not the same value, see issue #420 --- skynet-src/socket_server.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 8caf499f..d1f55966 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -46,6 +46,13 @@ #define MAX_UDP_PACKAGE 65535 +// EAGAIN and EWOULDBLOCK may be not the same value. +#if (EAGAIN != EWOULDBLOCK) +#define AGAIN_WOULDBLOCK EAGAIN : case EWOULDBLOCK +#else +#define AGAIN_WOULDBLOCK EAGAIN +#endif + struct write_buffer { struct write_buffer * next; void *buffer; @@ -475,7 +482,7 @@ send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, switch(errno) { case EINTR: continue; - case EAGAIN: + case AGAIN_WOULDBLOCK: return -1; } force_close(ss,s, result); @@ -531,7 +538,7 @@ send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, if (err < 0) { switch(errno) { case EINTR: - case EAGAIN: + case AGAIN_WOULDBLOCK: return -1; } fprintf(stderr, "socket-server : udp (%d) sendto error %s.\n",s->id, strerror(errno)); @@ -705,7 +712,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock if (n<0) { switch(errno) { case EINTR: - case EAGAIN: + case AGAIN_WOULDBLOCK: n = 0; break; default: @@ -999,7 +1006,7 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_me switch(errno) { case EINTR: break; - case EAGAIN: + case AGAIN_WOULDBLOCK: fprintf(stderr, "socket-server: EAGAIN capture.\n"); break; default: @@ -1061,7 +1068,7 @@ forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_me if (n<0) { switch(errno) { case EINTR: - case EAGAIN: + case AGAIN_WOULDBLOCK: break; default: // close when error From bcdfcde72f0689e8458a236ae7a43d4270b8e88a Mon Sep 17 00:00:00 2001 From: m2q1n9 Date: Sun, 27 Dec 2015 22:38:58 +0800 Subject: [PATCH 551/729] fix code indent --- service/debug_console.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 2ff5adce..a5e735c0 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -231,7 +231,7 @@ function COMMAND.debug(address, fd) skynet.fork(function() repeat local cmdline = socket.readline(fd, "\n") - cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") + cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") if not cmdline then skynet.send(agent, "lua", "cmd", "cont") break From a3a7d129ab857f82338d08d15e4b535e215d9965 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 28 Dec 2015 13:55:01 +0800 Subject: [PATCH 552/729] V1.0.0 RC --- HISTORY.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 23e1ae10..9067a9ac 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,16 @@ +v1.0.0-rc (2015-12-28) +----------- +* Update to lua 5.3.2 +* Add skynet.coroutine lib +* Add new debug api to show c memory used +* httpc can use async dns query +* Redis driver support pipeline +* socket.send support string table, and rewrite redis driver +* socket.shutdown would abandon unsend buffer +* Improve some sproto api +* c memory doesn't count the memory allocated by lua vm +* some other bugfix (In multicast, socketchannel, etc) + v1.0.0-beta (2015-11-10) ----------- * Improve and fix bug for sproto From 014cbb9676946f0830f10daf78ce4ef2092f882c Mon Sep 17 00:00:00 2001 From: DeanHH <532171255@qq.com> Date: Thu, 31 Dec 2015 17:31:08 +0800 Subject: [PATCH 553/729] Update skynet_mq.c Comment error:-) --- skynet-src/skynet_mq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index 157f33d8..0ddd9ee9 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -84,7 +84,7 @@ skynet_mq_create(uint32_t handle) { SPIN_INIT(q) // When the queue is create (always between service create and service init) , // set in_global flag to avoid push it to global queue . - // If the service init success, skynet_context_new will call skynet_mq_force_push to push it to global queue. + // If the service init success, skynet_context_new will call skynet_mq_push to push it to global queue. q->in_global = MQ_IN_GLOBAL; q->release = 0; q->overload = 0; From 645ce72e7e0bb5aec9ae607059cc44cd121bd1e3 Mon Sep 17 00:00:00 2001 From: fztcjjl Date: Mon, 4 Jan 2016 14:55:28 +0800 Subject: [PATCH 554/729] fix comment error --- skynet-src/socket_server.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 41ef9cca..96e38980 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -16,7 +16,7 @@ struct socket_server; struct socket_message { int id; uintptr_t opaque; - int ud; // for accept, ud is listen id ; for data, ud is size of data + int ud; // for accept, ud is new connection id ; for data, ud is size of data char * data; }; From 141a4d0992dc1a074467a0589f2207583d49767e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 4 Jan 2016 15:57:03 +0800 Subject: [PATCH 555/729] lua 5.3.2 bugfix,see http://lua-users.org/lists/lua-l/2016-01/msg00000.html --- 3rd/lua/lvm.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 58ba7f29..3fa87cb1 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -200,18 +200,19 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; if (oldval != NULL) { - lua_assert(ttistable(t) && ttisnil(oldval)); + Table *h = hvalue(t); + lua_assert(ttisnil(oldval)); /* must check the metamethod */ - if ((tm = fasttm(L, hvalue(t)->metatable, TM_NEWINDEX)) == NULL && + if ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && /* no metamethod; is there a previous entry in the table? */ (oldval != luaO_nilobject || /* no previous entry; must create one. (The next test is always true; we only need the assignment.) */ - (oldval = luaH_newkey(L, hvalue(t), key), 1))) { + (oldval = luaH_newkey(L, h, key), 1))) { /* no metamethod and (now) there is an entry with given key */ setobj2t(L, cast(TValue *, oldval), val); - invalidateTMcache(hvalue(t)); - luaC_barrierback(L, hvalue(t), val); + invalidateTMcache(h); + luaC_barrierback(L, h, val); return; } /* else will try the metamethod */ From 64c2cb1b5165db5a483cc94862842f3c136162a8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 5 Jan 2016 16:06:20 +0800 Subject: [PATCH 556/729] sproto dispatch support ud --- lualib/sproto.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 9c6524b2..f6338008 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -197,6 +197,7 @@ function host:dispatch(...) local bin = core.unpack(...) header_tmp.type = nil header_tmp.session = nil + header_tmp.ud = nil local header, size = core.decode(self.__package, bin, header_tmp) local content = bin:sub(size + 1) if header.type then @@ -207,9 +208,9 @@ function host:dispatch(...) result = core.decode(proto.request, content) end if header_tmp.session then - return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) + return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session), header.ud else - return "REQUEST", proto.name, result + return "REQUEST", proto.name, result, nil, header.ud end else -- response @@ -217,10 +218,10 @@ function host:dispatch(...) local response = assert(self.__session[session], "Unknown session") self.__session[session] = nil if response == true then - return "RESPONSE", session + return "RESPONSE", session, nil, header.ud else local result = core.decode(response, content) - return "RESPONSE", session, result + return "RESPONSE", session, result, header.ud end end end From 05003e3e2abb9e6dc302fdb155fc27cf0b51e3a7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 5 Jan 2016 17:51:48 +0800 Subject: [PATCH 557/729] sproto.attach also need ud --- lualib/sproto.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index f6338008..3ae114bd 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -227,10 +227,11 @@ function host:dispatch(...) end function host:attach(sp) - return function(name, args, session) + return function(name, args, session, ud) local proto = queryproto(sp, name) header_tmp.type = proto.tag header_tmp.session = session + header_tmp.ud = ud local header = core.encode(self.__package, header_tmp) if session then From c175d66e23bb7519925090276e0e8873e6684a9c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 6 Jan 2016 16:55:15 +0800 Subject: [PATCH 558/729] remove unused function --- lualib/mysql.lua | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index c3573ae2..933e0643 100644 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -125,19 +125,6 @@ local function _compose_packet(self, req, size) return packet end - -local function _send_packet(self, req, size) - local sock = self.sock - - self.packet_no = self.packet_no + 1 - - - local packet = _set_byte3(size) .. strchar(self.packet_no) .. req - - return socket.write(self.sock,packet) -end - - local function _recv_packet(self,sock) From d7a76c7c7272b1280bd37916c2d044e36d246991 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Jan 2016 16:01:17 +0800 Subject: [PATCH 559/729] If no content-length, read all. see issue #431 --- lualib/http/httpc.lua | 3 ++- lualib/http/sockethelper.lua | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 09ac9288..68d3ce9c 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -72,7 +72,8 @@ local function request(fd, method, host, url, recvheader, header, content) body = body .. padding end else - body = nil + -- no content-length, read all + body = body .. socket.readall(fd) end end diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index febe4cea..cac3130d 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -19,6 +19,8 @@ function sockethelper.readfunc(fd) end end +sockethelper.readall = socket.readall + function sockethelper.writefunc(fd) return function(content) local ok = writebytes(fd, content) From 24f9ee1560f588812e3749ed375fdd1846c058bb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 8 Jan 2016 18:34:45 +0800 Subject: [PATCH 560/729] work thread for channel may not create if you never connect --- lualib/socketchannel.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index cf918645..350423a4 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -411,10 +411,10 @@ end function channel:close() if not self.__closed then - local thread = assert(self.__dispatch_thread) + local thread = self.__dispatch_thread self.__closed = true close_channel_socket(self) - if not self.__response and self.__dispatch_thread == thread then + if not self.__response and self.__dispatch_thread == thread and thread then -- dispatch by order, send close signal to dispatch thread push_response(self, true, false) -- (true, false) is close signal end From 532f47444bbd8e92a5c30611dbba4fdde610a293 Mon Sep 17 00:00:00 2001 From: snail Date: Mon, 11 Jan 2016 15:06:51 +0800 Subject: [PATCH 561/729] reduce needless 'add_string' call Short strings in lua proto are reused most of the time. The string in SSM may be added by one service and used by other service, there is no need to call `add_string`,which will call 'new_string' first. --- 3rd/lua/lstring.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lstring.c b/3rd/lua/lstring.c index 79a232df..71443a91 100644 --- a/3rd/lua/lstring.c +++ b/3rd/lua/lstring.c @@ -419,8 +419,11 @@ luaS_clonestring(lua_State *L, TString *ts) { result = query_ptr(ts); if (result) return result; - // ts is not in SSM, so recalc hash, and add it to SSM h = luaS_hash(str, l, 0); + result = query_string(h, str, l); + if (result) + return result; + // ts is not in SSM, so recalc hash, and add it to SSM return add_string(h, str, l); } From 7f45b3fd10cb2f2fb39d7c07309a4ce585828002 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Jan 2016 13:13:31 +0800 Subject: [PATCH 562/729] correct maxsz --- lualib-src/sproto/lsproto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index d1cca74e..22e53189 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -469,7 +469,7 @@ lpack(lua_State *L) { size_t sz=0; const void * buffer = getbuffer(L, 1, &sz); // the worst-case space overhead of packing is 2 bytes per 2 KiB of input (256 words = 2KiB). - size_t maxsz = (sz + 2047) / 2048 * 2 + sz; + size_t maxsz = (sz + 2047) / 2048 * 2 + sz + 2; void * output = lua_touserdata(L, lua_upvalueindex(1)); int bytes; int osz = lua_tointeger(L, lua_upvalueindex(2)); From 9747cf9aeebb821acb6e755e5fc01f0d5bf8a257 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 19 Jan 2016 16:16:18 +0800 Subject: [PATCH 563/729] don't assert here --- service-src/service_harbor.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index 70abe82b..d01b33a4 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -346,8 +346,14 @@ dispatch_name_queue(struct harbor *h, struct keyvalue * node) { struct harbor_msg_queue * queue = node->queue; uint32_t handle = node->value; int harbor_id = handle >> HANDLE_REMOTE_SHIFT; - assert(harbor_id != 0); struct skynet_context * context = h->ctx; + if (harbor_id == 0) { + char tmp [GLOBALNAME_LENGTH+1]; + memcpy(tmp, node->key, GLOBALNAME_LENGTH); + tmp[GLOBALNAME_LENGTH] = '\0'; + skynet_error(context, "Invalid name (%s) handle :%08x",tmp,handle); + return; + } struct slave *s = &h->s[harbor_id]; int fd = s->fd; if (fd == 0) { From 1da92850a0dd546963561c3e61383acead813a28 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 19 Jan 2016 17:14:52 +0800 Subject: [PATCH 564/729] bugfix: dispatch local global message queue --- service-src/service_harbor.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index d01b33a4..3e722fa9 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -347,13 +347,6 @@ dispatch_name_queue(struct harbor *h, struct keyvalue * node) { uint32_t handle = node->value; int harbor_id = handle >> HANDLE_REMOTE_SHIFT; struct skynet_context * context = h->ctx; - if (harbor_id == 0) { - char tmp [GLOBALNAME_LENGTH+1]; - memcpy(tmp, node->key, GLOBALNAME_LENGTH); - tmp[GLOBALNAME_LENGTH] = '\0'; - skynet_error(context, "Invalid name (%s) handle :%08x",tmp,handle); - return; - } struct slave *s = &h->s[harbor_id]; int fd = s->fd; if (fd == 0) { @@ -372,6 +365,16 @@ dispatch_name_queue(struct harbor *h, struct keyvalue * node) { push_queue_msg(s->queue, m); } } + if (harbor_id == (h->slave >> HANDLE_REMOTE_SHIFT)) { + // the harbor_id is local + struct harbor_msg * m; + while ((m = pop_queue(s->queue)) != NULL) { + int type = m->header.destination >> HANDLE_REMOTE_SHIFT; + skynet_send(context, m->header.source, handle , type | PTYPE_TAG_DONTCOPY, m->header.session, m->buffer, m->size); + } + release_queue(s->queue); + s->queue = NULL; + } } return; } From 96e0a4e3cceac4f7268775ace7d9f46ea313d228 Mon Sep 17 00:00:00 2001 From: David Feng Date: Wed, 20 Jan 2016 19:24:58 +0800 Subject: [PATCH 565/729] change socket.write to socketdriver.send MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这样改过之åŽå¯ä»¥ç›´æŽ¥è°ƒè¯•使用了gateæœåŠ¡çš„æ¨¡å—。 因为gateæœåŠ¡æ³¨å†Œäº†socket类型的消æ¯ï¼Œæ‰€ä»¥ä¸èƒ½å’Œsocket模å—一å—使用;而debug模å—åªä½¿ç”¨åˆ°äº†socket模å—çš„write函数,所以这样修改åŽå¯¹debug的功能并没有影å“,但改过之åŽå°±å¯ä»¥è°ƒè¯•gateæœåŠ¡äº†ã€‚ --- lualib/skynet/remotedebug.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 1fa04155..5c1ad9b2 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local debugchannel = require "debugchannel" -local socket = require "socket" +local socketdriver = require "socketdriver" local injectrun = require "skynet.injectcode" local table = table local debug = debug @@ -56,7 +56,7 @@ local function gen_print(fd) tmp[i] = tostring(tmp[i]) end table.insert(tmp, "\n") - socket.write(fd, table.concat(tmp, "\t")) + socketdriver.send(fd, table.concat(tmp, "\t")) end end @@ -208,7 +208,7 @@ local function hook_dispatch(dispatcher, resp, fd, channel) local function debug_hook() while true do if newline then - socket.write(fd, prompt) + socketdriver.send(fd, prompt) newline = false end local cmd = channel:read() From 77f407119112d0bd404cdd24a6262f50795209ee Mon Sep 17 00:00:00 2001 From: David Feng Date: Mon, 18 Jan 2016 11:56:25 +0800 Subject: [PATCH 566/729] remove unused local variables --- lualib/skynet/debug.lua | 2 -- lualib/snax/hotfix.lua | 3 --- 2 files changed, 5 deletions(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 635bdb31..79c51ad1 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -1,6 +1,4 @@ -local io = io local table = table -local debug = debug return function (skynet, export) diff --git a/lualib/snax/hotfix.lua b/lualib/snax/hotfix.lua index 3a6e4e3e..f56a5072 100644 --- a/lualib/snax/hotfix.lua +++ b/lualib/snax/hotfix.lua @@ -1,7 +1,4 @@ local si = require "snax.interface" -local io = io - -local hotfix = {} local function envid(f) local i = 1 From d34b092cdd3bf0cfe707c0dcc5829eb09b2a4f99 Mon Sep 17 00:00:00 2001 From: David Feng Date: Fri, 22 Jan 2016 10:28:03 +0800 Subject: [PATCH 567/729] =?UTF-8?q?remotedebug.lua=20call=20skynet.timeout?= =?UTF-8?q?()=20only=20in=20debug=20hook=20=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=9C=A8debug=E7=BB=93=E6=9D=9F=E5=90=8E=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet/remotedebug.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 1fa04155..ccb4f8f9 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -244,7 +244,9 @@ local function hook_dispatch(dispatcher, resp, fd, channel) func = replace_upvalue(dispatcher, HOOK_FUNC, hook) if func then local function idle() - skynet.timeout(10,idle) -- idle every 0.1s + if raw_dispatcher then + skynet.timeout(10,idle) -- idle every 0.1s + end end skynet.timeout(0, idle) end From d982009260a16403f33cace61c2178b67bea6dc2 Mon Sep 17 00:00:00 2001 From: David Feng Date: Wed, 27 Jan 2016 10:35:40 +0800 Subject: [PATCH 568/729] fix --- lualib/skynet/remotedebug.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 7cfe19eb..72a06809 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -140,10 +140,13 @@ end local function watch_proto(protoname, cond) local proto = assert(replace_upvalue(skynet.register_protocol, "proto"), "Can't find proto table") local p = proto[protoname] - local dispatch = p.dispatch_origin or p.dispatch - if p == nil or dispatch == nil then + if p == nil then return "No " .. protoname end + local dispatch = p.dispatch_origin or p.dispatch + if dispatch == nil then + return "No dispatch" + end p.dispatch_origin = dispatch p.dispatch = function(...) if not cond or cond(...) then From ea8557296b63e80c0cedf7635bdc600da4750ee4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 13 Feb 2016 19:20:07 +0800 Subject: [PATCH 569/729] fix issue #450 --- lualib-src/lua-crypt.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 405679a9..5322b2d6 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -885,7 +885,12 @@ int lhmac_sha1(lua_State *L); int luaopen_crypt(lua_State *L) { luaL_checkversion(L); - srandom(time(NULL)); + static int init = 0; + if (!init) { + // Don't need call srandom more than once. + init = 1 ; + srandom(time(NULL)); + } luaL_Reg l[] = { { "hashkey", lhashkey }, { "randomkey", lrandomkey }, From 14ccf96eb042aaf10f1c813798dd4dcaea912d68 Mon Sep 17 00:00:00 2001 From: snail Date: Mon, 22 Feb 2016 11:23:35 +0800 Subject: [PATCH 570/729] error deal for query containing multi statement A query may execute multi sql statements.If an error occurs for any sql, the result should return the error info. --- lualib/mysql.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index 933e0643..afa76425 100644 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -607,6 +607,10 @@ local function _query_resp(self) while err =="again" do res, err, errno, sqlstate = read_result(self,sock) if not res then + mulitresultset.badresult = true + mulitresultset.err = err + mulitresultset.errno = errno + mulitresultset.sqlstate = sqlstate return true, mulitresultset end mulitresultset[i]=res From 8f7fa6a5629a976b65af25b975a0423d2523d968 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Feb 2016 14:40:24 +0800 Subject: [PATCH 571/729] merge sproto --- lualib/sproto.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 3ae114bd..00f5337b 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -180,9 +180,10 @@ end local header_tmp = {} local function gen_response(self, response, session) - return function(args) + return function(args, ud) header_tmp.type = nil header_tmp.session = session + header_tmp.ud = ud local header = core.encode(self.__package, header_tmp) if response then local content = core.encode(response, args) From cbf22ac9f32608d9d97d04b0369d03689297d160 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 7 Mar 2016 11:46:30 +0800 Subject: [PATCH 572/729] release rc2 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 9067a9ac..2f2af7b2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v1.0.0-rc2 (2015-3-7) +----------- +* Fix a bug in lua 5.3.2 +* Update sproto (fix bugs and add ud for package) +* Fix a bug in http +* Fix a bug in harbor +* Fix a bug in socket channel +* Enhance remote debugger + v1.0.0-rc (2015-12-28) ----------- * Update to lua 5.3.2 From 7771e180ec7628d2df4afeb0e97ff006a8dc9b05 Mon Sep 17 00:00:00 2001 From: great90 <897346026@qq.com> Date: Tue, 8 Mar 2016 11:25:59 +0800 Subject: [PATCH 573/729] delete unused local variable delete unused local variable table "udp_socket" --- lualib/socket.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lualib/socket.lua b/lualib/socket.lua index 5ea222e0..d7741b54 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -407,8 +407,6 @@ end ---------------------- UDP -local udp_socket = {} - local function create_udp_object(id, cb) assert(not socket_pool[id], "socket is not closed") socket_pool[id] = { From e5490522ce4632a6b9f94474b3b82f6e29dbeb23 Mon Sep 17 00:00:00 2001 From: dennis Date: Wed, 9 Mar 2016 17:50:33 +0800 Subject: [PATCH 574/729] this line is useless --- lualib/skynet/coroutine.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua index af2e1fd2..b9280894 100644 --- a/lualib/skynet/coroutine.lua +++ b/lualib/skynet/coroutine.lua @@ -10,7 +10,6 @@ local coroutine_running = coroutine.running local select = select local skynetco = {} -skynetco.create = coroutine.create skynetco.isyieldable = coroutine.isyieldable skynetco.running = coroutine.running skynetco.status = coroutine.status From 242ff32bcaae57601b8f11dff2f3f50ad5087cc5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 24 Mar 2016 16:19:06 +0800 Subject: [PATCH 575/729] lua 5.3.2 bugfix 2/3 --- 3rd/lua/lparser.c | 2 +- 3rd/lua/lstrlib.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 60be8aa2..7845ccf1 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1229,7 +1229,7 @@ static void labelstat (LexState *ls, TString *label, int line) { checkrepeated(fs, ll, label); /* check for repeated labels */ checknext(ls, TK_DBCOLON); /* skip double colon */ /* create new entry for this label */ - l = newlabelentry(ls, ll, label, line, fs->pc); + l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); skipnoopstat(ls); /* skip other no-op statements */ if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ /* assume that locals are already out of scope */ diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index fe30e34b..bc6fc4ea 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -688,6 +688,7 @@ typedef struct GMatchState { static int gmatch_aux (lua_State *L) { GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); const char *src; + gm->ms.L = L; for (src = gm->src; src <= gm->ms.src_end; src++) { const char *e; reprepstate(&gm->ms); From 13d1d9fc9d5b3a53aaa931cadb38bbc50ecb7aca Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 24 Mar 2016 16:29:00 +0800 Subject: [PATCH 576/729] update jemalloc to 4.1.0 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 91010a9e..df900dbf 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 91010a9e2ebfc84b1ac1ed7fdde3bfed4f65f180 +Subproject commit df900dbfaf4835d3efc06d771535f3e781544913 From ca5e855c34eb194e16fd8bc2b02fb70958b99d3e Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Thu, 24 Mar 2016 17:48:43 +0800 Subject: [PATCH 577/729] Improve some English in README --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 16c544f2..bb9e3820 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Build -For linux, install autoconf first for jemalloc +For Linux, install autoconf first for jemalloc: ``` git clone https://github.com/cloudwu/skynet.git @@ -8,18 +8,18 @@ cd skynet make 'PLATFORM' # PLATFORM can be linux, macosx, freebsd now ``` -Or you can : +Or you can: ``` export PLAT=linux make ``` -For freeBSD , use gmake instead of make . +For FreeBSD , use gmake instead of make. ## Test -Run these in different console +Run these in different consoles: ``` ./skynet examples/config # Launch first skynet node (Gate server) and a skynet-master (see config for standalone option) @@ -28,11 +28,11 @@ Run these in different console ## About Lua -Skynet now use a modify version of lua 5.3.2 ( http://www.lua.org/ftp/lua-5.3.2.tar.gz ) . +Skynet now uses a modified version of lua 5.3.2 ( http://www.lua.org/ftp/lua-5.3.2.tar.gz ) . -For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html +For details: http://lua-users.org/lists/lua-l/2014-03/msg00489.html -You can also use the other official Lua version , edit the makefile by yourself . +You can also use other official Lua versions, just edit the Makefile by yourself. ## How To Use (Sorry, Only in Chinese now) From c4b45acae326b9d5d74f70070ee3999121fd41e3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 29 Mar 2016 12:34:55 +0800 Subject: [PATCH 578/729] sproto: check args --- lualib-src/sproto/lsproto.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 22e53189..6214fdc9 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -532,6 +532,9 @@ lprotocol(lua_State *L) { lua_pushstring(L, name); } else { const char * name = lua_tostring(L, 2); + if (name == NULL) { + return luaL_argerror(L, 2, "Should be number or string"); + } tag = sproto_prototag(sp, name); if (tag < 0) return 0; From dd152fc9102fda2e6aa85398f7aa885fcce163a0 Mon Sep 17 00:00:00 2001 From: xjdrew Date: Thu, 31 Mar 2016 17:33:47 +0800 Subject: [PATCH 579/729] =?UTF-8?q?skynet.init=E6=8C=89=E7=85=A7=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 6d7e3141..94db8b5e 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -592,9 +592,9 @@ function skynet.init(f, name) if init_func == nil then f() else - if name == nil then - table.insert(init_func, f) - else + table.insert(init_func, f) + if name then + assert(type(name) == "string") assert(init_func[name] == nil) init_func[name] = f end @@ -605,8 +605,8 @@ local function init_all() local funcs = init_func init_func = nil if funcs then - for k,v in pairs(funcs) do - v() + for _,f in ipairs(funcs) do + f() end end end From 8de314948531aee8965dd687bdf127be26c211a4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 1 Apr 2016 10:22:57 +0800 Subject: [PATCH 580/729] request global name from master, see issue #476 --- service/cslave.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/service/cslave.lua b/service/cslave.lua index 63d98eea..559d02b3 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -215,6 +215,7 @@ function harbor.QUERYNAME(fd, name) end local queue = queryname[name] if queue == nil then + socket.write(fd, pack_package("Q", name)) queue = { skynet.response() } queryname[name] = queue else From 5332021086065874fb8080999eb797fb6c46ceb5 Mon Sep 17 00:00:00 2001 From: rickone Date: Fri, 1 Apr 2016 12:39:09 +0800 Subject: [PATCH 581/729] Update redis.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit exec执行时其中一æ¡å‡ºé”™ï¼Œçœ‹ä¸åˆ°é”™è¯¯ä¿¡æ¯ --- lualib/redis.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lualib/redis.lua b/lualib/redis.lua index 3fc14a58..09147817 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -54,11 +54,10 @@ redcmd[42] = function(fd, data) -- '*' local noerr = true for i = 1,n do local ok, v = read_response(fd) - if ok then - bulk[i] = v - else + if not ok then noerr = false end + bulk[i] = v end return noerr, bulk end From 69b5bd8c13c6cd235035c17ddbf6a96e462f2fd3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Apr 2016 10:12:30 +0800 Subject: [PATCH 582/729] update sproto. support encode empty table --- lualib-src/sproto/README.md | 4 +- lualib-src/sproto/lsproto.c | 27 ++++++++---- lualib-src/sproto/sproto.c | 85 +++++++++++++++++++++++-------------- lualib-src/sproto/sproto.h | 4 ++ 4 files changed, 78 insertions(+), 42 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index ea5bc7be..67366a46 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -210,13 +210,13 @@ Each integer number must be serialized in little-endian format. The sproto message must be a user defined type struct, and a struct is encoded in three parts. The header, the field part, and the data part. The tag and small integer or boolean will be encoded in field part, and others are in data part. -All the fields must be encoded in ascending order (by tag). The tags of fields can be discontinuous, if a field is nil. (default value in lua), don't encode it in message. +All the fields must be encoded in ascending order (by tag, base 0). The tags of fields can be discontinuous, if a field is nil. (default value in lua), don't encode it in message. The header is a 16bit integer. It is the number of fields. Each field in field part is a 16bit integer (n). If n is zero, that means the field data is encoded in data part ; -If n is even (and not zero), the value of this field is n/2-1 ; +If n is even (and not zero), the value of this field is n/2-1 , and the tag increases 1; If n is odd, that means the tags is not continuous, and we should add current tag by (n+1)/2 . diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 6214fdc9..3a70a6d4 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -126,7 +126,7 @@ encode(const struct sproto_arg *args) { lua_replace(L, self->array_index); } self->array_index = 0; - return 0; + return SPROTO_CB_NOARRAY; } if (!lua_istable(L, -1)) { return luaL_error(L, ".*%s(%d) should be a table (Is a %s)", @@ -147,7 +147,7 @@ encode(const struct sproto_arg *args) { // iterate end lua_pushnil(L); lua_replace(L, self->iter_index); - return 0; + return SPROTO_CB_NIL; } lua_insert(L, -2); lua_replace(L, self->iter_index); @@ -159,7 +159,7 @@ encode(const struct sproto_arg *args) { } if (lua_isnil(L, -1)) { lua_pop(L,1); - return 0; + return SPROTO_CB_NIL; } switch (args->type) { case SPROTO_TINTEGER: { @@ -203,10 +203,10 @@ encode(const struct sproto_arg *args) { str = lua_tolstring(L, -1, &sz); } if (sz > args->length) - return -1; + return SPROTO_CB_ERROR; memcpy(args->value, str, sz); lua_pop(L,1); - return sz + 1; // The length of empty string is 1. + return sz; } case SPROTO_TSTRUCT: { struct encode_ud sub; @@ -226,6 +226,8 @@ encode(const struct sproto_arg *args) { sub.iter_index = sub.tbl_index + 1; r = sproto_encode(args->subtype, args->value, args->length, encode, &sub); lua_settop(L, top-1); // pop the value + if (r < 0) + return SPROTO_CB_ERROR; return r; } default: @@ -309,7 +311,7 @@ decode(const struct sproto_arg *args) { lua_State *L = self->L; if (self->deep >= ENCODE_DEEPLEVEL) return luaL_error(L, "The table is too deep"); - if (args->index > 0) { + if (args->index != 0) { // It's array if (args->tagname != self->array_tag) { self->array_tag = args->tagname; @@ -321,6 +323,10 @@ decode(const struct sproto_arg *args) { } else { self->array_index = lua_gettop(L); } + if (args->index < 0) { + // It's a empty array, return now. + return 0; + } } } switch (args->type) { @@ -355,9 +361,10 @@ decode(const struct sproto_arg *args) { sub.key_index = lua_gettop(L); r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); - if (r < 0 || r != args->length) + if (r < 0) + return SPROTO_CB_ERROR; + if (r != args->length) return r; - // assert(args->index > 0); lua_pushvalue(L, sub.key_index); if (lua_isnil(L, -1)) { luaL_error(L, "Can't find main index (tag=%d) in [%s]", args->mainindex, args->tagname); @@ -370,7 +377,9 @@ decode(const struct sproto_arg *args) { sub.mainindex_tag = -1; sub.key_index = 0; r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); - if (r < 0 || r != args->length) + if (r < 0) + return SPROTO_CB_ERROR; + if (r != args->length) return r; lua_settop(L, sub.result_index); break; diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 68ccd047..9b23af49 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -677,10 +677,10 @@ encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int si args->value = data+SIZEOF_LENGTH; args->length = size-SIZEOF_LENGTH; sz = cb(args); - if (sz <= 0) - return sz; - if (args->type == SPROTO_TSTRING) { - --sz; // the length of null string is 1 + if (sz < 0) { + if (sz == SPROTO_CB_NIL) + return 0; + return -1; // sz == SPROTO_CB_ERROR } assert(sz <= size-SIZEOF_LENGTH); // verify buffer overflow return fill_size(data, sz); @@ -702,7 +702,7 @@ uint32_to_uint64(int negative, uint8_t *buffer) { } static uint8_t * -encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size) { +encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size, int *noarray) { uint8_t * header = buffer; int intlen; int index; @@ -712,6 +712,8 @@ encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffe size--; intlen = sizeof(uint32_t); index = 1; + *noarray = 0; + for (;;) { int sz; union { @@ -722,10 +724,15 @@ encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffe args->length = sizeof(u); args->index = index; sz = cb(args); - if (sz < 0) - return NULL; - if (sz == 0) // nil object, end of array - break; + if (sz <= 0) { + if (sz == SPROTO_CB_NIL) // nil object, end of array + break; + if (sz == SPROTO_CB_NOARRAY) { // no array, don't encode it + *noarray = 1; + break; + } + return NULL; // sz == SPROTO_CB_ERROR + } if (size < sizeof(uint64_t)) return NULL; if (sz == sizeof(uint32_t)) { @@ -790,11 +797,17 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz size -= SIZEOF_LENGTH; buffer = data + SIZEOF_LENGTH; switch (args->type) { - case SPROTO_TINTEGER: - buffer = encode_integer_array(cb,args,buffer,size); + case SPROTO_TINTEGER: { + int noarray; + buffer = encode_integer_array(cb,args,buffer,size, &noarray); if (buffer == NULL) return -1; + + if (noarray) { + return 0; + } break; + } case SPROTO_TBOOLEAN: args->index = 1; for (;;) { @@ -802,10 +815,13 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz args->value = &v; args->length = sizeof(v); sz = cb(args); - if (sz < 0) - return -1; - if (sz == 0) // nil object , end of array - break; + if (sz < 0) { + if (sz == SPROTO_CB_NIL) // nil object , end of array + break; + if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it + return 0; + return -1; // sz == SPROTO_CB_ERROR + } if (size < 1) return -1; buffer[0] = v ? 1: 0; @@ -823,12 +839,13 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz args->value = buffer+SIZEOF_LENGTH; args->length = size; sz = cb(args); - if (sz == 0) - break; - if (sz < 0) - return -1; - if (args->type == SPROTO_TSTRING) { - --sz; + if (sz < 0) { + if (sz == SPROTO_CB_NIL) { + break; + } + if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it + return 0; + return -1; // sz == SPROTO_CB_ERROR } fill_size(buffer, sz); buffer += SIZEOF_LENGTH+sz; @@ -838,8 +855,6 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz break; } sz = buffer - (data + SIZEOF_LENGTH); - if (sz == 0) // empty array - return 0; return fill_size(data, sz); } @@ -885,10 +900,13 @@ sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_call args.value = &u; args.length = sizeof(u); sz = cb(&args); - if (sz < 0) - return -1; - if (sz == 0) // nil object - continue; + if (sz < 0) { + if (sz == SPROTO_CB_NIL) + continue; + if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it + return 0; + return -1; // sz == SPROTO_CB_ERROR + } if (sz == sizeof(uint32_t)) { if (u.u32 < 0x7fff) { value = (u.u32+1) * 2; @@ -985,13 +1003,18 @@ decode_array(sproto_callback cb, struct sproto_arg *args, uint8_t * stream) { uint32_t sz = todword(stream); int type = args->type; int i; + if (sz == 0) { + // It's empty array, call cb with index == -1 to create the empty array. + args->index = -1; + args->value = NULL; + args->length = 0; + cb(args); + return 0; + } stream += SIZEOF_LENGTH; switch (type) { case SPROTO_TINTEGER: { - int len; - if (sz < 1) - return -1; - len = *stream; + int len = *stream; ++stream; --sz; if (len == sizeof(uint32_t)) { diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index 0a419c86..c38a12c0 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -14,6 +14,10 @@ struct sproto_type; #define SPROTO_TSTRING 2 #define SPROTO_TSTRUCT 3 +#define SPROTO_CB_ERROR -1 +#define SPROTO_CB_NIL -2 +#define SPROTO_CB_NOARRAY -3 + struct sproto * sproto_create(const void * proto, size_t sz); void sproto_release(struct sproto *); From 146f8ea09d44056a169cbeac2ea33a215db56df7 Mon Sep 17 00:00:00 2001 From: caijietao Date: Fri, 8 Apr 2016 10:29:34 +0800 Subject: [PATCH 583/729] bugfix, error when use lua apicheck --- 3rd/lua/ldebug.c | 2 +- 3rd/lua/ldo.c | 2 +- 3rd/lua/lparser.c | 2 +- 3rd/lua/lundump.c | 2 +- 3rd/lua/lvm.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index a75bceec..9d66f955 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -118,7 +118,7 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { static const char *upvalname (Proto *p, int uv) { - TString *s = check_exp(uv < p->sizeupvalues, p->sp->upvalues[uv].name); + TString *s = check_exp(uv < p->sp->sizeupvalues, p->sp->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); } diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index a141c2fd..db222be8 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -768,7 +768,7 @@ static void f_parser (lua_State *L, void *ud) { checkmode(L, p->mode, "text"); cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); } - lua_assert(cl->nupvalues == cl->p->sizeupvalues); + lua_assert(cl->nupvalues == cl->p->sp->sizeupvalues); luaF_initupvals(L, cl); } diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 7845ccf1..53c5cdd5 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1597,7 +1597,7 @@ static void statement (LexState *ls) { break; } } - lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && + lua_assert(ls->fs->f->sp->maxstacksize >= ls->fs->freereg && ls->fs->freereg >= ls->fs->nactvar); ls->fs->freereg = ls->fs->nactvar; /* free registers */ leavelevel(ls); diff --git a/3rd/lua/lundump.c b/3rd/lua/lundump.c index 5e0224c5..39674cd9 100644 --- a/3rd/lua/lundump.c +++ b/3rd/lua/lundump.c @@ -273,7 +273,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { luaD_inctop(L); cl->p = luaF_newproto(L, NULL); LoadFunction(&S, cl->p, NULL); - lua_assert(cl->nupvalues == cl->p->sizeupvalues); + lua_assert(cl->nupvalues == cl->p->sp->sizeupvalues); luai_verifycode(L, buff, cl->p); return cl; } diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 3fa87cb1..9fc24af1 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1163,7 +1163,7 @@ void luaV_execute (lua_State *L) { oci->u.l.savedpc = nci->u.l.savedpc; oci->callstatus |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ - lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); + lua_assert(L->top == oci->u.l.base + getproto(ofunc)->sp->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; From b124d4c1b8a42a728e6bce5ce82a920df8c1c24d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Apr 2016 11:55:39 +0800 Subject: [PATCH 584/729] skynet.getenv can return empty string --- lualib/skynet.lua | 7 +------ service/bootstrap.lua | 6 +++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 94db8b5e..463048fe 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -343,12 +343,7 @@ function skynet.exit() end function skynet.getenv(key) - local ret = c.command("GETENV",key) - if ret == "" then - return - else - return ret - end + return c.command("GETENV",key) end function skynet.setenv(key, value) diff --git a/service/bootstrap.lua b/service/bootstrap.lua index db8d8581..90a542cb 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -4,15 +4,15 @@ require "skynet.manager" -- import skynet.launch, ... local memory = require "memory" skynet.start(function() - local sharestring = tonumber(skynet.getenv "sharestring") - memory.ssexpand(sharestring or 4096) + local sharestring = tonumber(skynet.getenv "sharestring" or 4096) + memory.ssexpand(sharestring) local standalone = skynet.getenv "standalone" local launcher = assert(skynet.launch("snlua","launcher")) skynet.name(".launcher", launcher) - local harbor_id = tonumber(skynet.getenv "harbor") + local harbor_id = tonumber(skynet.getenv "harbor" or 0) if harbor_id == 0 then assert(standalone == nil) standalone = true From f56d777cc2ae7602ae2ae058e64d7bc78218059b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 18 Apr 2016 12:05:46 +0800 Subject: [PATCH 585/729] for compatible --- lualib/skynet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 463048fe..5064c7ae 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -343,7 +343,7 @@ function skynet.exit() end function skynet.getenv(key) - return c.command("GETENV",key) + return (c.command("GETENV",key)) end function skynet.setenv(key, value) From f2381136773a32736882a5ac3a309a653b6e1f8a Mon Sep 17 00:00:00 2001 From: Acai Date: Tue, 19 Apr 2016 21:11:31 +0800 Subject: [PATCH 586/729] =?UTF-8?q?=E7=BB=99dbgcmd=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 我们项目增加了一个dbgcmd的指令,主è¦ç”¨äºŽçƒ­æ›´æ–°ï¼Œä½†åœ¨å‡çº§skynet版本时,需è¦è®°å¾—åˆå¹¶è¿™ä¸ªå·®å¼‚,感觉挺挫的。 但现有的debug.luaå¹¶ä¸åˆ©äºŽæ‰©å±•,于是呢,我想通过给debug.lua增加一个upvalueæ¥è§£å†³è¿™ä¸ªé—®é¢˜ã€‚ 其实也是解决的挺挫的 :) --- lualib/skynet/debug.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 79c51ad1..6e886f02 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -1,4 +1,5 @@ local table = table +local extern_dbgcmd = {}; return function (skynet, export) @@ -11,7 +12,7 @@ end local dbgcmd local function init_dbgcmd() -dbgcmd = {} +dbgcmd = extern_dbgcmd function dbgcmd.MEM() local kb, bytes = collectgarbage "count" From c5fb93823acf1c373d3c3795fcf6ac0d383a5fd0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 20 Apr 2016 10:11:55 +0800 Subject: [PATCH 587/729] update sproto, fix bug in sproto.default --- lualib-src/sproto/lsproto.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 3a70a6d4..c7708fed 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -604,6 +604,8 @@ encode_default(const struct sproto_arg *args) { lua_pushstring(L, args->tagname); if (args->index > 0) { lua_newtable(L); + lua_rawset(L, -3); + return SPROTO_CB_NOARRAY; } else { switch(args->type) { case SPROTO_TINTEGER: @@ -621,9 +623,9 @@ encode_default(const struct sproto_arg *args) { lua_setfield(L, -2, "__type"); break; } + lua_rawset(L, -3); + return SPROTO_CB_NIL; } - lua_rawset(L, -3); - return 0; } /* From 6b2eb8018863296b39e3477a5d8bb011df56048c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 20 Apr 2016 22:35:01 +0800 Subject: [PATCH 588/729] fix #487 --- service/service_mgr.lua | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/service/service_mgr.lua b/service/service_mgr.lua index d94b0a3d..fc63ce25 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -142,14 +142,23 @@ local function register_global() end local function register_local() - function cmd.GLAUNCH(name, ...) + local function waitfor_remote(cmd, name, ...) local global_name = "@" .. name - return waitfor(global_name, skynet.call, "SERVICE", "lua", "LAUNCH", global_name, ...) + local local_name + if name == "snaxd" then + local_name = global_name .. "." .. (...) + else + local_name = global_name + end + return waitfor(local_name, skynet.call, "SERVICE", "lua", cmd, global_name, ...) end - function cmd.GQUERY(name, ...) - local global_name = "@" .. name - return waitfor(global_name, skynet.call, "SERVICE", "lua", "QUERY", global_name, ...) + function cmd.GLAUNCH(...) + return waitfor_remote("LAUNCH", ...) + end + + function cmd.GQUERY(...) + return waitfor_remote("QUERY", ...) end function cmd.LIST() From 45d46b90722e205eb2943c594a0b0217b7130453 Mon Sep 17 00:00:00 2001 From: David Feng Date: Wed, 27 Apr 2016 20:53:05 +0800 Subject: [PATCH 589/729] skynet.pcall support vararg; sharedata loader can use custom args --- lualib/sharedata.lua | 8 ++++---- lualib/skynet.lua | 8 ++++---- service/sharedatad.lua | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua index 58ecf28d..93ead44d 100644 --- a/lualib/sharedata.lua +++ b/lualib/sharedata.lua @@ -28,12 +28,12 @@ function sharedata.query(name) return r end -function sharedata.new(name, v) - skynet.call(service, "lua", "new", name, v) +function sharedata.new(name, v, ...) + skynet.call(service, "lua", "new", name, v, ...) end -function sharedata.update(name, v) - skynet.call(service, "lua", "update", name, v) +function sharedata.update(name, v, ...) + skynet.call(service, "lua", "update", name, v, ...) end function sharedata.delete(name) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 5064c7ae..e7fe27f4 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -611,14 +611,14 @@ local function ret(f, ...) return ... end -local function init_template(start) +local function init_template(start, ...) init_all() init_func = {} - return ret(init_all, start()) + return ret(init_all, start(...)) end -function skynet.pcall(start) - return xpcall(init_template, debug.traceback, start) +function skynet.pcall(start, ...) + return xpcall(init_template, debug.traceback, start, ...) end function skynet.init_service(start) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 1ae9022c..3f86155b 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -38,7 +38,7 @@ local CMD = {} local env_mt = { __index = _ENV } -function CMD.new(name, t) +function CMD.new(name, t, ...) local dt = type(t) local value if dt == "table" then @@ -51,7 +51,7 @@ function CMD.new(name, t) else f = assert(load(t, "=" .. name, "bt", value)) end - local _, ret = assert(skynet.pcall(f)) + local _, ret = assert(skynet.pcall(f, ...)) setmetatable(value, nil) if type(ret) == "table" then value = ret @@ -90,7 +90,7 @@ function CMD.confirm(cobj) return NORET end -function CMD.update(name, t) +function CMD.update(name, t, ...) local v = pool[name] local watch, oldcobj if v then @@ -101,7 +101,7 @@ function CMD.update(name, t) pool[name] = nil pool_count[name] = nil end - CMD.new(name, t) + CMD.new(name, t, ...) local newobj = pool[name].obj if watch then sharedata.host.markdirty(oldcobj) From 2862568ecaeaa5cfc07ff32838bc57000d9b999b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 29 Apr 2016 16:25:12 +0800 Subject: [PATCH 590/729] typo in history --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 2f2af7b2..b1f6ac0e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -v1.0.0-rc2 (2015-3-7) +v1.0.0-rc2 (2016-3-7) ----------- * Fix a bug in lua 5.3.2 * Update sproto (fix bugs and add ud for package) From 4ada09b93c02cecbc59e417b06cc54eddb2aa62f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 6 May 2016 14:06:38 +0800 Subject: [PATCH 591/729] add pcall in launcher --- service/launcher.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/service/launcher.lua b/service/launcher.lua index 82e7d4be..272a459a 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -24,7 +24,10 @@ end function command.STAT() local list = {} for k,v in pairs(services) do - local stat = skynet.call(k,"debug","STAT") + local ok, stat = pcall(skynet.call,k,"debug","STAT") + if not ok then + stat = string.format("ERROR (%s)",v) + end list[skynet.address(k)] = stat end return list @@ -41,8 +44,12 @@ end function command.MEM() local list = {} for k,v in pairs(services) do - local kb, bytes = skynet.call(k,"debug","MEM") - list[skynet.address(k)] = string.format("%.2f Kb (%s)",kb,v) + local ok, kb, bytes = pcall(skynet.call,k,"debug","MEM") + if not ok then + list[skynet.address(k)] = string.format("ERROR (%s)",v) + else + list[skynet.address(k)] = string.format("%.2f Kb (%s)",kb,v) + end end return list end From 84b08bf286549f3a0f2817f5e3252e8a785ecbb9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 6 May 2016 19:37:16 +0800 Subject: [PATCH 592/729] detect console fd closed --- service/debug_console.lua | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index a5e735c0..312cd0e6 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -52,13 +52,10 @@ end local function docmd(cmdline, print, fd) local split = split_cmdline(cmdline) local command = split[1] - if command == "debug" then - table.insert(split, fd) - end local cmd = COMMAND[command] local ok, list if cmd then - ok, list = pcall(cmd, select(2,table.unpack(split))) + ok, list = pcall(cmd, fd, select(2,table.unpack(split))) else print("Invalid command, type help for command list") end @@ -79,18 +76,21 @@ local function docmd(cmdline, print, fd) end local function console_main_loop(stdin, print) - socket.lock(stdin) print("Welcome to skynet console") - while true do - local cmdline = socket.readline(stdin, "\n") - if not cmdline then - break + skynet.error(stdin, "connected") + pcall(function() + while true do + local cmdline = socket.readline(stdin, "\n") + if not cmdline then + break + end + if cmdline ~= "" then + docmd(cmdline, print, stdin) + end end - if cmdline ~= "" then - docmd(cmdline, print, stdin) - end - end - socket.unlock(stdin) + end) + skynet.error(stdin, "disconnected") + socket.close(stdin) end skynet.start(function() @@ -140,7 +140,7 @@ function COMMAND.clearcache() codecache.clear() end -function COMMAND.start(...) +function COMMAND.start(fd, ...) local ok, addr = pcall(skynet.newservice, ...) if ok then return { [skynet.address(addr)] = ... } @@ -149,7 +149,7 @@ function COMMAND.start(...) end end -function COMMAND.log(...) +function COMMAND.log(fd, ...) local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...) if ok then return { [skynet.address(addr)] = ... } @@ -158,7 +158,7 @@ function COMMAND.log(...) end end -function COMMAND.snax(...) +function COMMAND.snax(fd, ...) local ok, s = pcall(snax.newservice, ...) if ok then local addr = s.handle @@ -191,7 +191,7 @@ function COMMAND.mem() return skynet.call(".launcher", "lua", "MEM") end -function COMMAND.kill(address) +function COMMAND.kill(fd, address) return skynet.call(".launcher", "lua", "KILL", address) end @@ -199,11 +199,11 @@ function COMMAND.gc() return skynet.call(".launcher", "lua", "GC") end -function COMMAND.exit(address) +function COMMAND.exit(fd, address) skynet.send(adjust_address(address), "debug", "EXIT") end -function COMMAND.inject(address, filename) +function COMMAND.inject(fd, address, filename) address = adjust_address(address) local f = io.open(filename, "rb") if not f then @@ -214,17 +214,17 @@ function COMMAND.inject(address, filename) return skynet.call(address, "debug", "RUN", source, filename) end -function COMMAND.task(address) +function COMMAND.task(fd, address) address = adjust_address(address) return skynet.call(address,"debug","TASK") end -function COMMAND.info(address) +function COMMAND.info(fd, address) address = adjust_address(address) return skynet.call(address,"debug","INFO") end -function COMMAND.debug(address, fd) +function COMMAND.debug(fd, address) address = adjust_address(address) local agent = skynet.newservice "debug_agent" local stop @@ -243,17 +243,17 @@ function COMMAND.debug(address, fd) stop = true end -function COMMAND.logon(address) +function COMMAND.logon(fd, address) address = adjust_address(address) core.command("LOGON", skynet.address(address)) end -function COMMAND.logoff(address) +function COMMAND.logoff(fd, address) address = adjust_address(address) core.command("LOGOFF", skynet.address(address)) end -function COMMAND.signal(address, sig) +function COMMAND.signal(fd, address, sig) address = skynet.address(adjust_address(address)) if sig then core.command("SIGNAL", string.format("%s %d",address,sig)) From 6cc558bb825be0dddd9f72df12ba83763c74c7e3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 6 May 2016 19:45:50 +0800 Subject: [PATCH 593/729] output Exit when new service exit --- service/debug_console.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 312cd0e6..92d99403 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -143,7 +143,11 @@ end function COMMAND.start(fd, ...) local ok, addr = pcall(skynet.newservice, ...) if ok then - return { [skynet.address(addr)] = ... } + if addr then + return { [skynet.address(addr)] = ... } + else + return "Exit" + end else return "Failed" end @@ -152,7 +156,11 @@ end function COMMAND.log(fd, ...) local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...) if ok then - return { [skynet.address(addr)] = ... } + if addr then + return { [skynet.address(addr)] = ... } + else + return "Failed" + end else return "Failed" end From a6299a0748215ed26dfa61682eab747564b6595b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 6 May 2016 20:10:11 +0800 Subject: [PATCH 594/729] optimize skynet.error --- examples/main.lua | 5 ++-- lualib-src/lua-seri.c | 4 +-- lualib-src/lua-seri.h | 4 +-- lualib-src/lua-skynet.c | 62 ++++++++++++++++++++++++++--------------- lualib/skynet.lua | 8 +----- 5 files changed, 47 insertions(+), 36 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index 9da707d4..cac49737 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -4,7 +4,7 @@ local sprotoloader = require "sprotoloader" local max_client = 64 skynet.start(function() - print("Server start") + skynet.error("Server start") skynet.uniqueservice("protoloader") local console = skynet.newservice("console") skynet.newservice("debug_console",8000) @@ -15,7 +15,6 @@ skynet.start(function() maxclient = max_client, nodelay = true, }) - print("Watchdog listen on ", 8888) - + skynet.error("Watchdog listen on", 8888) skynet.exit() end) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index d80452b6..28056f74 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -551,7 +551,7 @@ seri(lua_State *L, struct block *b, int len) { } int -_luaseri_unpack(lua_State *L) { +luaseri_unpack(lua_State *L) { if (lua_isnoneornil(L,1)) { return 0; } @@ -595,7 +595,7 @@ _luaseri_unpack(lua_State *L) { } int -_luaseri_pack(lua_State *L) { +luaseri_pack(lua_State *L) { struct block temp; temp.next = NULL; struct write_block wb; diff --git a/lualib-src/lua-seri.h b/lualib-src/lua-seri.h index 23eb4cf9..6102239e 100644 --- a/lualib-src/lua-seri.h +++ b/lualib-src/lua-seri.h @@ -3,7 +3,7 @@ #include -int _luaseri_pack(lua_State *L); -int _luaseri_unpack(lua_State *L); +int luaseri_pack(lua_State *L); +int luaseri_unpack(lua_State *L); #endif diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 063320a4..2422f413 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -81,7 +81,7 @@ forward_cb(struct skynet_context * context, void * ud, int type, int session, ui } static int -_callback(lua_State *L) { +lcallback(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); int forward = lua_toboolean(L, 2); luaL_checktype(L,1,LUA_TFUNCTION); @@ -101,7 +101,7 @@ _callback(lua_State *L) { } static int -_command(lua_State *L) { +lcommand(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * cmd = luaL_checkstring(L,1); const char * result; @@ -119,7 +119,7 @@ _command(lua_State *L) { } static int -_intcommand(lua_State *L) { +lintcommand(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * cmd = luaL_checkstring(L,1); const char * result; @@ -141,7 +141,7 @@ _intcommand(lua_State *L) { } static int -_genid(lua_State *L) { +lgenid(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); int session = skynet_send(context, 0, 0, PTYPE_TAG_ALLOCSESSION , 0 , NULL, 0); lua_pushinteger(L, session); @@ -167,7 +167,7 @@ get_dest_string(lua_State *L, int index) { integer len */ static int -_send(lua_State *L) { +lsend(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; @@ -224,7 +224,7 @@ _send(lua_State *L) { } static int -_redirect(lua_State *L) { +lredirect(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); uint32_t dest = (uint32_t)lua_tointeger(L,1); const char * dest_string = NULL; @@ -267,14 +267,32 @@ _redirect(lua_State *L) { } static int -_error(lua_State *L) { +lerror(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - skynet_error(context, "%s", luaL_checkstring(L,1)); + int n = lua_gettop(L); + if (n <= 1) { + lua_settop(L, 1); + const char * s = luaL_tolstring(L, 1, NULL); + skynet_error(context, "%s", s); + return 0; + } + luaL_Buffer b; + luaL_buffinit(L, &b); + int i; + for (i=1; i<=n; i++) { + luaL_tolstring(L, i, NULL); + luaL_addvalue(&b); + if (i Date: Fri, 6 May 2016 20:37:51 +0800 Subject: [PATCH 595/729] memory warning --- service-src/service_snlua.c | 24 +++++++++++++++++++++++- skynet-src/malloc_hook.c | 2 +- skynet-src/skynet_main.c | 2 +- skynet-src/skynet_malloc.h | 2 +- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 20440780..bd2f6b2c 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -9,9 +9,13 @@ #include #include +#define MEMORY_WARNING_REPORT (1024 * 1024 * 32) + struct snlua { lua_State * L; struct skynet_context * ctx; + size_t mem; + size_t mem_report; }; // LUA_CACHELIB may defined in patched lua for shared proto @@ -144,11 +148,25 @@ snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) { return 0; } +static void * +lalloc(void * ud, void *ptr, size_t osize, size_t nsize) { + struct snlua *l = ud; + l->mem += nsize; + if (ptr) + l->mem -= osize; + if (l->mem > l->mem_report) { + l->mem_report *= 2; + skynet_error(l->ctx, "Memory warning %.2f M", (float)l->mem / (1024 * 1024)); + } + return skynet_lalloc(ptr, osize, nsize); +} + struct snlua * snlua_create(void) { struct snlua * l = skynet_malloc(sizeof(*l)); memset(l,0,sizeof(*l)); - l->L = lua_newstate(skynet_lalloc, NULL); + l->mem_report = MEMORY_WARNING_REPORT; + l->L = lua_newstate(lalloc, l); return l; } @@ -161,8 +179,12 @@ snlua_release(struct snlua *l) { void snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); + if (signal == 0) { #ifdef lua_checksig // If our lua support signal (modified lua version by skynet), trigger it. skynet_sig_L = l->L; #endif + } else if (signal == 1) { + skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); + } } diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 6cad2a9a..da8a536b 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -227,7 +227,7 @@ skynet_strdup(const char *str) { } void * -skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize) { +skynet_lalloc(void *ptr, size_t osize, size_t nsize) { if (nsize == 0) { raw_free(ptr); return NULL; diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index eb6f4429..f4296cd9 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -115,7 +115,7 @@ main(int argc, char *argv[]) { struct skynet_config config; - struct lua_State *L = lua_newstate(skynet_lalloc, NULL); + struct lua_State *L = luaL_newstate(); luaL_openlibs(L); // link lua lib int err = luaL_loadstring(L, load_config); diff --git a/skynet-src/skynet_malloc.h b/skynet-src/skynet_malloc.h index f7e4337f..7bafa406 100644 --- a/skynet-src/skynet_malloc.h +++ b/skynet-src/skynet_malloc.h @@ -13,6 +13,6 @@ void * skynet_calloc(size_t nmemb,size_t size); void * skynet_realloc(void *ptr, size_t size); void skynet_free(void *ptr); char * skynet_strdup(const char *str); -void * skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize); // use for lua +void * skynet_lalloc(void *ptr, size_t osize, size_t nsize); // use for lua #endif From 4b96ade66076aab0d47f3c12351c20490c3fe206 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 6 May 2016 23:40:44 +0800 Subject: [PATCH 596/729] support sandbox memory limit --- lualib/skynet/manager.lua | 4 ++++ service-src/service_snlua.c | 29 ++++++++++++++++++++++------- test/testmemlimit.lua | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 test/testmemlimit.lua diff --git a/lualib/skynet/manager.lua b/lualib/skynet/manager.lua index 4365d2e2..627d1e12 100644 --- a/lualib/skynet/manager.lua +++ b/lualib/skynet/manager.lua @@ -20,6 +20,10 @@ function skynet.abort() c.command("ABORT") end +function skynet.memlimit(bytes) + debug.getregistry().memlimit = bytes +end + local function globalname(name, handle) local c = string.sub(name,1,1) assert(c ~= ':') diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index bd2f6b2c..4870baa9 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -16,6 +16,7 @@ struct snlua { struct skynet_context * ctx; size_t mem; size_t mem_report; + size_t mem_limit; }; // LUA_CACHELIB may defined in patched lua for shared proto @@ -57,7 +58,7 @@ traceback (lua_State *L) { } static void -_report_launcher_error(struct skynet_context *ctx) { +report_launcher_error(struct skynet_context *ctx) { // sizeof "ERROR" == 5 skynet_sendname(ctx, 0, ".launcher", PTYPE_TEXT, 0, "ERROR", 5); } @@ -72,7 +73,7 @@ optstring(struct skynet_context *ctx, const char *key, const char * str) { } static int -_init(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) { +init_cb(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) { lua_State *L = l->L; l->ctx = ctx; lua_gc(L, LUA_GCSTOP, 0); @@ -105,17 +106,23 @@ _init(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) int r = luaL_loadfile(L,loader); if (r != LUA_OK) { skynet_error(ctx, "Can't load %s : %s", loader, lua_tostring(L, -1)); - _report_launcher_error(ctx); + report_launcher_error(ctx); return 1; } lua_pushlstring(L, args, sz); r = lua_pcall(L,1,0,1); if (r != LUA_OK) { skynet_error(ctx, "lua loader error : %s", lua_tostring(L, -1)); - _report_launcher_error(ctx); + report_launcher_error(ctx); return 1; } lua_settop(L,0); + if (lua_getfield(L, LUA_REGISTRYINDEX, "memlimit") == LUA_TNUMBER) { + size_t limit = lua_tointeger(L, -1); + l->mem_limit = limit; + skynet_error(ctx, "Set memory limit to %.2f M", (float)limit / (1024 * 1024)); + } + lua_pop(L, 1); lua_gc(L, LUA_GCRESTART, 0); @@ -123,11 +130,11 @@ _init(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) } static int -_launch(struct skynet_context * context, void *ud, int type, int session, uint32_t source , const void * msg, size_t sz) { +launch_cb(struct skynet_context * context, void *ud, int type, int session, uint32_t source , const void * msg, size_t sz) { assert(type == 0 && session == 0); struct snlua *l = ud; skynet_callback(context, NULL, NULL); - int err = _init(l, context, msg, sz); + int err = init_cb(l, context, msg, sz); if (err) { skynet_command(context, "EXIT", NULL); } @@ -140,7 +147,7 @@ snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) { int sz = strlen(args); char * tmp = skynet_malloc(sz); memcpy(tmp, args, sz); - skynet_callback(ctx, l , _launch); + skynet_callback(ctx, l , launch_cb); const char * self = skynet_command(ctx, "REG", NULL); uint32_t handle_id = strtoul(self+1, NULL, 16); // it must be first message @@ -151,9 +158,16 @@ snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) { static void * lalloc(void * ud, void *ptr, size_t osize, size_t nsize) { struct snlua *l = ud; + size_t mem = l->mem; l->mem += nsize; if (ptr) l->mem -= osize; + if (l->mem_limit != 0 && l->mem > l->mem_limit) { + if (ptr == NULL || nsize > osize) { + l->mem = mem; + return NULL; + } + } if (l->mem > l->mem_report) { l->mem_report *= 2; skynet_error(l->ctx, "Memory warning %.2f M", (float)l->mem / (1024 * 1024)); @@ -166,6 +180,7 @@ snlua_create(void) { struct snlua * l = skynet_malloc(sizeof(*l)); memset(l,0,sizeof(*l)); l->mem_report = MEMORY_WARNING_REPORT; + l->mem_limit = 0; l->L = lua_newstate(lalloc, l); return l; } diff --git a/test/testmemlimit.lua b/test/testmemlimit.lua new file mode 100644 index 00000000..532df727 --- /dev/null +++ b/test/testmemlimit.lua @@ -0,0 +1,18 @@ +local skynet = require "skynet" +require "skynet.manager" + +-- set sandbox memory limit to 1M, must set here (at start, out of skynet.start) +skynet.memlimit(1 * 1024 * 1024) + +skynet.start(function() + local a = {} + local limit + local ok, err = pcall(function() + for i=1, 1000000 do + limit = i + table.insert(a, {}) + end + end) + skynet.error(limit, err) + skynet.exit() +end) From a5c79cc4bda2a5d56f0842cb4782ab0c92cfff5f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 7 May 2016 09:23:01 +0800 Subject: [PATCH 597/729] move skynet.memlimit from skynet.manager to skynet --- lualib/skynet.lua | 5 +++++ lualib/skynet/manager.lua | 4 ---- service-src/service_snlua.c | 2 ++ test/testmemlimit.lua | 1 - 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 295696a7..08a3fa13 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -656,6 +656,11 @@ function skynet.term(service) return _error_dispatch(0, service) end +function skynet.memlimit(bytes) + debug.getregistry().memlimit = bytes + skynet.memlimit = nil -- set only once +end + local function clear_pool() coroutine_pool = {} end diff --git a/lualib/skynet/manager.lua b/lualib/skynet/manager.lua index 627d1e12..4365d2e2 100644 --- a/lualib/skynet/manager.lua +++ b/lualib/skynet/manager.lua @@ -20,10 +20,6 @@ function skynet.abort() c.command("ABORT") end -function skynet.memlimit(bytes) - debug.getregistry().memlimit = bytes -end - local function globalname(name, handle) local c = string.sub(name,1,1) assert(c ~= ':') diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 4870baa9..6f2ea6c5 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -121,6 +121,8 @@ init_cb(struct snlua *l, struct skynet_context *ctx, const char * args, size_t s size_t limit = lua_tointeger(L, -1); l->mem_limit = limit; skynet_error(ctx, "Set memory limit to %.2f M", (float)limit / (1024 * 1024)); + lua_pushnil(L); + lua_setfield(L, LUA_REGISTRYINDEX, "memlimit"); } lua_pop(L, 1); diff --git a/test/testmemlimit.lua b/test/testmemlimit.lua index 532df727..41072dcc 100644 --- a/test/testmemlimit.lua +++ b/test/testmemlimit.lua @@ -1,5 +1,4 @@ local skynet = require "skynet" -require "skynet.manager" -- set sandbox memory limit to 1M, must set here (at start, out of skynet.start) skynet.memlimit(1 * 1024 * 1024) From fb232cc43ef3423282b9d7ec0ee7a9edb61f11a3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 May 2016 09:45:40 +0800 Subject: [PATCH 598/729] update lua to 5.3.3 rc1 --- 3rd/lua/lapi.c | 48 ++-- 3rd/lua/lauxlib.c | 29 +- 3rd/lua/lbaselib.c | 11 +- 3rd/lua/lcode.c | 693 ++++++++++++++++++++++++++++++--------------- 3rd/lua/lcode.h | 5 +- 3rd/lua/lcorolib.c | 4 +- 3rd/lua/ldebug.c | 18 +- 3rd/lua/ldo.c | 9 +- 3rd/lua/ldo.h | 4 +- 3rd/lua/lgc.c | 35 +-- 3rd/lua/lgc.h | 4 +- 3rd/lua/liolib.c | 19 +- 3rd/lua/llex.c | 35 +-- 3rd/lua/llex.h | 3 +- 3rd/lua/lobject.c | 78 +++-- 3rd/lua/loslib.c | 91 +++--- 3rd/lua/lparser.c | 20 +- 3rd/lua/lparser.h | 53 ++-- 3rd/lua/lstate.h | 15 +- 3rd/lua/lstrlib.c | 127 +++++---- 3rd/lua/ltablib.c | 41 +-- 3rd/lua/ltm.c | 18 +- 3rd/lua/ltm.h | 5 +- 3rd/lua/lua.h | 8 +- 3rd/lua/luaconf.h | 4 +- 3rd/lua/lvm.c | 102 ++++--- 3rd/lua/lvm.h | 31 +- 27 files changed, 942 insertions(+), 568 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index 33062dad..c8ee2df4 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.257 2015/11/02 18:48:07 roberto Exp $ +** $Id: lapi.c,v 2.259 2016/02/29 14:27:14 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -379,9 +379,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { return NULL; } lua_lock(L); /* 'luaO_tostring' may create a new string */ + luaO_tostring(L, o); luaC_checkGC(L); o = index2addr(L, idx); /* previous call may reallocate the stack */ - luaO_tostring(L, o); lua_unlock(L); } if (len != NULL) @@ -480,10 +480,10 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); - luaC_checkGC(L); ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top, ts); api_incr_top(L); + luaC_checkGC(L); lua_unlock(L); return getstr(ts); } @@ -495,12 +495,12 @@ LUA_API const char *lua_pushstring (lua_State *L, const char *s) { setnilvalue(L->top); else { TString *ts; - luaC_checkGC(L); ts = luaS_new(L, s); setsvalue2s(L, L->top, ts); s = getstr(ts); /* internal copy's address */ } api_incr_top(L); + luaC_checkGC(L); lua_unlock(L); return s; } @@ -510,8 +510,8 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp) { const char *ret; lua_lock(L); - luaC_checkGC(L); ret = luaO_pushvfstring(L, fmt, argp); + luaC_checkGC(L); lua_unlock(L); return ret; } @@ -521,10 +521,10 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { const char *ret; va_list argp; lua_lock(L); - luaC_checkGC(L); va_start(argp, fmt); ret = luaO_pushvfstring(L, fmt, argp); va_end(argp); + luaC_checkGC(L); lua_unlock(L); return ret; } @@ -539,7 +539,6 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { CClosure *cl; api_checknelems(L, n); api_check(L, n <= MAXUPVAL, "upvalue index too large"); - luaC_checkGC(L); cl = luaF_newCclosure(L, n); cl->f = fn; L->top -= n; @@ -550,6 +549,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { setclCvalue(L, L->top, cl); } api_incr_top(L); + luaC_checkGC(L); lua_unlock(L); } @@ -586,16 +586,16 @@ LUA_API int lua_pushthread (lua_State *L) { static int auxgetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *aux; + const TValue *slot; TString *str = luaS_new(L, k); - if (luaV_fastget(L, t, str, aux, luaH_getstr)) { - setobj2s(L, L->top, aux); + if (luaV_fastget(L, t, str, slot, luaH_getstr)) { + setobj2s(L, L->top, slot); api_incr_top(L); } else { setsvalue2s(L, L->top, str); api_incr_top(L); - luaV_finishget(L, t, L->top - 1, L->top - 1, aux); + luaV_finishget(L, t, L->top - 1, L->top - 1, slot); } lua_unlock(L); return ttnov(L->top - 1); @@ -627,17 +627,17 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { StkId t; - const TValue *aux; + const TValue *slot; lua_lock(L); t = index2addr(L, idx); - if (luaV_fastget(L, t, n, aux, luaH_getint)) { - setobj2s(L, L->top, aux); + if (luaV_fastget(L, t, n, slot, luaH_getint)) { + setobj2s(L, L->top, slot); api_incr_top(L); } else { setivalue(L->top, n); api_incr_top(L); - luaV_finishget(L, t, L->top - 1, L->top - 1, aux); + luaV_finishget(L, t, L->top - 1, L->top - 1, slot); } lua_unlock(L); return ttnov(L->top - 1); @@ -684,12 +684,12 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); - luaC_checkGC(L); t = luaH_new(L); sethvalue(L, L->top, t); api_incr_top(L); if (narray > 0 || nrec > 0) luaH_resize(L, t, narray, nrec); + luaC_checkGC(L); lua_unlock(L); } @@ -741,15 +741,15 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) { ** t[k] = value at the top of the stack (where 'k' is a string) */ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *aux; + const TValue *slot; TString *str = luaS_new(L, k); api_checknelems(L, 1); - if (luaV_fastset(L, t, str, aux, luaH_getstr, L->top - 1)) + if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) L->top--; /* pop value */ else { setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); - luaV_finishset(L, t, L->top - 1, L->top - 2, aux); + luaV_finishset(L, t, L->top - 1, L->top - 2, slot); L->top -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ @@ -782,16 +782,16 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { StkId t; - const TValue *aux; + const TValue *slot; lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); - if (luaV_fastset(L, t, n, aux, luaH_getint, L->top - 1)) + if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1)) L->top--; /* pop value */ else { setivalue(L->top, n); api_incr_top(L); - luaV_finishset(L, t, L->top - 1, L->top - 2, aux); + luaV_finishset(L, t, L->top - 1, L->top - 2, slot); L->top -= 2; /* pop value and key */ } lua_unlock(L); @@ -1193,7 +1193,6 @@ LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); if (n >= 2) { - luaC_checkGC(L); luaV_concat(L, n); } else if (n == 0) { /* push empty string */ @@ -1201,6 +1200,7 @@ LUA_API void lua_concat (lua_State *L, int n) { api_incr_top(L); } /* else n == 1; nothing to do */ + luaC_checkGC(L); lua_unlock(L); } @@ -1236,10 +1236,10 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { LUA_API void *lua_newuserdata (lua_State *L, size_t size) { Udata *u; lua_lock(L); - luaC_checkGC(L); u = luaS_newudata(L, size); setuvalue(L, L->top, u); api_incr_top(L); + luaC_checkGC(L); lua_unlock(L); return getudatamem(u); } diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index b92e9527..be061be7 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.284 2015/11/19 19:16:22 roberto Exp $ +** $Id: lauxlib.c,v 1.286 2016/01/08 15:33:09 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -17,7 +17,8 @@ #include -/* This file uses only the official API of Lua. +/* +** This file uses only the official API of Lua. ** Any function declared here could be written as an application function. */ @@ -198,6 +199,10 @@ static void tag_error (lua_State *L, int arg, int tag) { } +/* +** The use of 'lua_pushfstring' ensures this function does not +** need reserved stack space when called. +*/ LUALIB_API void luaL_where (lua_State *L, int level) { lua_Debug ar; if (lua_getstack(L, level, &ar)) { /* check function at level */ @@ -207,10 +212,15 @@ LUALIB_API void luaL_where (lua_State *L, int level) { return; } } - lua_pushliteral(L, ""); /* else, no information available... */ + lua_pushfstring(L, ""); /* else, no information available... */ } +/* +** Again, the use of 'lua_pushvfstring' ensures this function does +** not need reserved stack space when called. (At worst, it generates +** an error with "stack overflow" instead of the given message.) +*/ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); @@ -349,10 +359,15 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, } +/* +** Ensures the stack has at least 'space' extra slots, raising an error +** if it cannot fulfill the request. (The error handling needs a few +** extra slots to format the error message. In case of an error without +** this extra space, Lua will generate the same 'stack overflow' error, +** but without 'msg'.) +*/ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - /* keep some extra space to run error routines, if needed */ - const int extra = LUA_MINSTACK; - if (!lua_checkstack(L, space + extra)) { + if (!lua_checkstack(L, space)) { if (msg) luaL_error(L, "stack overflow (%s)", msg); else @@ -678,7 +693,7 @@ static int skipcomment (LoadF *lf, int *cp) { if (c == '#') { /* first line is a comment (Unix exec. file)? */ do { /* skip first line */ c = getc(lf->f); - } while (c != EOF && c != '\n') ; + } while (c != EOF && c != '\n'); *cp = getc(lf->f); /* skip end-of-line, if present */ return 1; /* there was a comment */ } diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index 861823d8..d481c4e1 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.312 2015/10/29 15:21:04 roberto Exp $ +** $Id: lbaselib.c,v 1.313 2016/04/11 19:18:40 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ @@ -102,8 +102,8 @@ static int luaB_tonumber (lua_State *L) { static int luaB_error (lua_State *L) { int level = (int)luaL_optinteger(L, 2, 1); lua_settop(L, 1); - if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ - luaL_where(L, level); + if (lua_type(L, 1) == LUA_TSTRING && level > 0) { + luaL_where(L, level); /* add extra information */ lua_pushvalue(L, 1); lua_concat(L, 2); } @@ -251,9 +251,8 @@ static int ipairsaux (lua_State *L) { /* -** This function will use either 'ipairsaux' or 'ipairsaux_raw' to -** traverse a table, depending on whether the table has metamethods -** that can affect the traversal. +** 'ipairs' function. Returns 'ipairsaux', given "table", 0. +** (The given "table" may not be a table.) */ static int luaB_ipairs (lua_State *L) { #if defined(LUA_COMPAT_IPAIRS) diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index d8676e21..e0940bbb 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.103 2015/11/19 19:16:22 roberto Exp $ +** $Id: lcode.c,v 2.108 2016/01/05 16:22:37 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -36,6 +36,10 @@ #define hasjumps(e) ((e)->t != (e)->f) +/* +** If expression is a numeric constant, fills 'v' with its value +** and returns 1. Otherwise, returns 0. +*/ static int tonumeral(expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a numeral */ @@ -51,13 +55,19 @@ static int tonumeral(expdesc *e, TValue *v) { } +/* +** Create a OP_LOADNIL instruction, but try to optimize: if the previous +** instruction is also OP_LOADNIL and ranges are compatible, adjust +** range of previous instruction instead of emitting a new one. (For +** instance, 'local a; local b' will generate a single opcode.) +*/ void luaK_nil (FuncState *fs, int from, int n) { Instruction *previous; int l = from + n - 1; /* last register to set nil */ if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ previous = &fs->f->sp->code[fs->pc-1]; - if (GET_OPCODE(*previous) == OP_LOADNIL) { - int pfrom = GETARG_A(*previous); + if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ + int pfrom = GETARG_A(*previous); /* get previous range */ int pl = pfrom + GETARG_B(*previous); if ((pfrom <= from && from <= pl + 1) || (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ @@ -73,37 +83,84 @@ void luaK_nil (FuncState *fs, int from, int n) { } +/* +** Gets the destination address of a jump instruction. Used to traverse +** a list of jumps. +*/ +static int getjump (FuncState *fs, int pc) { + int offset = GETARG_sBx(fs->f->sp->code[pc]); + if (offset == NO_JUMP) /* point to itself represents end of list */ + return NO_JUMP; /* end of list */ + else + return (pc+1)+offset; /* turn offset into absolute position */ +} + + +/* +** Fix jump instruction at position 'pc' to jump to 'dest'. +** (Jump addresses are relative in Lua) +*/ +static void fixjump (FuncState *fs, int pc, int dest) { + Instruction *jmp = &fs->f->sp->code[pc]; + int offset = dest - (pc + 1); + lua_assert(dest != NO_JUMP); + if (abs(offset) > MAXARG_sBx) + luaX_syntaxerror(fs->ls, "control structure too long"); + SETARG_sBx(*jmp, offset); +} + + +/* +** Concatenate jump-list 'l2' into jump-list 'l1' +*/ +void luaK_concat (FuncState *fs, int *l1, int l2) { + if (l2 == NO_JUMP) return; /* nothing to concatenate? */ + else if (*l1 == NO_JUMP) /* no original list? */ + *l1 = l2; /* 'l1' points to 'l2' */ + else { + int list = *l1; + int next; + while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ + list = next; + fixjump(fs, list, l2); /* last element links to 'l2' */ + } +} + + +/* +** Create a jump instruction and return its position, so its destination +** can be fixed later (with 'fixjump'). If there are jumps to +** this position (kept in 'jpc'), link them all together so that +** 'patchlistaux' will fix all them directly to the final destination. +*/ int luaK_jump (FuncState *fs) { int jpc = fs->jpc; /* save list of jumps to here */ int j; - fs->jpc = NO_JUMP; + fs->jpc = NO_JUMP; /* no more jumps to here */ j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); luaK_concat(fs, &j, jpc); /* keep them on hold */ return j; } +/* +** Code a 'return' instruction +*/ void luaK_ret (FuncState *fs, int first, int nret) { luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); } +/* +** Code a "conditional jump", that is, a test or comparison opcode +** followed by a jump. Return jump position. +*/ static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { luaK_codeABC(fs, op, A, B, C); return luaK_jump(fs); } -static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->sp->code[pc]; - int offset = dest-(pc+1); - lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sBx) - luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_sBx(*jmp, offset); -} - - /* ** returns current 'pc' and marks it as a jump target (to avoid wrong ** optimizations with consecutive instructions not in the same basic block). @@ -114,14 +171,11 @@ int luaK_getlabel (FuncState *fs) { } -static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->sp->code[pc]); - if (offset == NO_JUMP) /* point to itself represents end of list */ - return NO_JUMP; /* end of list */ - else - return (pc+1)+offset; /* turn offset into absolute position */ -} - +/* +** Returns the position of the instruction "controlling" a given +** jump (that is, its condition), or the jump itself if it is +** unconditional. +*/ static Instruction *getjumpcontrol (FuncState *fs, int pc) { Instruction *pi = &fs->f->sp->code[pc]; @@ -133,37 +187,41 @@ static Instruction *getjumpcontrol (FuncState *fs, int pc) { /* -** check whether list has any jump that do not produce a value -** (or produce an inverted value) +** Patch destination register for a TESTSET instruction. +** If instruction in position 'node' is not a TESTSET, return 0 ("fails"). +** Otherwise, if 'reg' is not 'NO_REG', set it as the destination +** register. Otherwise, change instruction to a simple 'TEST' (produces +** no register value) */ -static int need_value (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - Instruction i = *getjumpcontrol(fs, list); - if (GET_OPCODE(i) != OP_TESTSET) return 1; - } - return 0; /* not found */ -} - - static int patchtestreg (FuncState *fs, int node, int reg) { Instruction *i = getjumpcontrol(fs, node); if (GET_OPCODE(*i) != OP_TESTSET) return 0; /* cannot patch other instructions */ if (reg != NO_REG && reg != GETARG_B(*i)) SETARG_A(*i, reg); - else /* no register to put value or register already has the value */ + else { + /* no register to put value or register already has the value; + change instruction to simple test */ *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); - + } return 1; } +/* +** Traverse a list of tests ensuring no one produces a value +*/ static void removevalues (FuncState *fs, int list) { for (; list != NO_JUMP; list = getjump(fs, list)) patchtestreg(fs, list, NO_REG); } +/* +** Traverse a list of tests, patching their destination address and +** registers: tests producing values jump to 'vtarget' (and put their +** values in 'reg'), other tests jump to 'dtarget'. +*/ static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, int dtarget) { while (list != NO_JUMP) { @@ -177,15 +235,35 @@ static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, } +/* +** Ensure all pending jumps to current position are fixed (jumping +** to current position with no values) and reset list of pending +** jumps +*/ static void dischargejpc (FuncState *fs) { patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); fs->jpc = NO_JUMP; } +/* +** Add elements in 'list' to list of pending jumps to "here" +** (current position) +*/ +void luaK_patchtohere (FuncState *fs, int list) { + luaK_getlabel(fs); /* mark "here" as a jump target */ + luaK_concat(fs, &fs->jpc, list); +} + + +/* +** Path all jumps in 'list' to jump to 'target'. +** (The assert means that we cannot fix a jump to a forward address +** because we only know addresses once code is generated.) +*/ void luaK_patchlist (FuncState *fs, int list, int target) { - if (target == fs->pc) - luaK_patchtohere(fs, list); + if (target == fs->pc) /* 'target' is current position? */ + luaK_patchtohere(fs, list); /* add list to pending jumps */ else { lua_assert(target < fs->pc); patchlistaux(fs, list, target, NO_REG, target); @@ -193,39 +271,26 @@ void luaK_patchlist (FuncState *fs, int list, int target) { } +/* +** Path all jumps in 'list' to close upvalues up to given 'level' +** (The assertion checks that jumps either were closing nothing +** or were closing higher levels, from inner blocks.) +*/ void luaK_patchclose (FuncState *fs, int list, int level) { level++; /* argument is +1 to reserve 0 as non-op */ - while (list != NO_JUMP) { - int next = getjump(fs, list); + for (; list != NO_JUMP; list = getjump(fs, list)) { lua_assert(GET_OPCODE(fs->f->sp->code[list]) == OP_JMP && (GETARG_A(fs->f->sp->code[list]) == 0 || GETARG_A(fs->f->sp->code[list]) >= level)); SETARG_A(fs->f->sp->code[list], level); - list = next; - } -} - - -void luaK_patchtohere (FuncState *fs, int list) { - luaK_getlabel(fs); - luaK_concat(fs, &fs->jpc, list); -} - - -void luaK_concat (FuncState *fs, int *l1, int l2) { - if (l2 == NO_JUMP) return; - else if (*l1 == NO_JUMP) - *l1 = l2; - else { - int list = *l1; - int next; - while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ - list = next; - fixjump(fs, list, l2); } } +/* +** Emit instruction 'i', checking for array sizes and saving also its +** line information. Return 'i' position. +*/ static int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; dischargejpc(fs); /* 'pc' will change */ @@ -241,6 +306,10 @@ static int luaK_code (FuncState *fs, Instruction i) { } +/* +** Format and emit an 'iABC' instruction. (Assertions check consistency +** of parameters versus opcode.) +*/ int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { lua_assert(getOpMode(o) == iABC); lua_assert(getBMode(o) != OpArgN || b == 0); @@ -250,6 +319,9 @@ int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { } +/* +** Format and emit an 'iABx' instruction. +*/ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); lua_assert(getCMode(o) == OpArgN); @@ -258,12 +330,20 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { } +/* +** Emit an "extra argument" instruction (format 'iAx') +*/ static int codeextraarg (FuncState *fs, int a) { lua_assert(a <= MAXARG_Ax); return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); } +/* +** Emit a "load constant" instruction, using either 'OP_LOADK' +** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' +** instruction with "extra argument". +*/ int luaK_codek (FuncState *fs, int reg, int k) { if (k <= MAXARG_Bx) return luaK_codeABx(fs, OP_LOADK, reg, k); @@ -275,6 +355,10 @@ int luaK_codek (FuncState *fs, int reg, int k) { } +/* +** Check register-stack level, keeping track of its maximum size +** in field 'maxstacksize' +*/ void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; if (newstack > fs->f->sp->maxstacksize) { @@ -286,12 +370,20 @@ void luaK_checkstack (FuncState *fs, int n) { } +/* +** Reserve 'n' registers in register stack +*/ void luaK_reserveregs (FuncState *fs, int n) { luaK_checkstack(fs, n); fs->freereg += n; } +/* +** Free register 'reg', if it is neither a constant index nor +** a local variable. +) +*/ static void freereg (FuncState *fs, int reg) { if (!ISK(reg) && reg >= fs->nactvar) { fs->freereg--; @@ -300,6 +392,9 @@ static void freereg (FuncState *fs, int reg) { } +/* +** Free register used by expression 'e' (if any) +*/ static void freeexp (FuncState *fs, expdesc *e) { if (e->k == VNONRELOC) freereg(fs, e->u.info); @@ -307,8 +402,29 @@ static void freeexp (FuncState *fs, expdesc *e) { /* +** Free registers used by expressions 'e1' and 'e2' (if any) in proper +** order. +*/ +static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { + int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; + int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; + if (r1 > r2) { + freereg(fs, r1); + freereg(fs, r2); + } + else { + freereg(fs, r2); + freereg(fs, r1); + } +} + + +/* +** Add constant 'v' to prototype's list of constants (field 'k'). ** Use scanner's table to cache position of constants in constant list -** and try to reuse constants +** and try to reuse constants. Because some values should not be used +** as keys (nil cannot be a key, integer keys can collapse with float +** keys), the caller must provide a useful 'key' for indexing the cache. */ static int addk (FuncState *fs, TValue *key, TValue *v) { lua_State *L = fs->ls->L; @@ -337,17 +453,21 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { } +/* +** Add a string to list of constants and return its index. +*/ int luaK_stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); - return addk(fs, &o, &o); + return addk(fs, &o, &o); /* use string itself as key */ } /* -** Integers use userdata as keys to avoid collision with floats with same -** value; conversion to 'void*' used only for hashing, no "precision" -** problems +** Add an integer to list of constants and return its index. +** Integers use userdata as keys to avoid collision with floats with +** same value; conversion to 'void*' is used only for hashing, so there +** are no "precision" problems. */ int luaK_intK (FuncState *fs, lua_Integer n) { TValue k, o; @@ -356,21 +476,29 @@ int luaK_intK (FuncState *fs, lua_Integer n) { return addk(fs, &k, &o); } - +/* +** Add a float to list of constants and return its index. +*/ static int luaK_numberK (FuncState *fs, lua_Number r) { TValue o; setfltvalue(&o, r); - return addk(fs, &o, &o); + return addk(fs, &o, &o); /* use number itself as key */ } +/* +** Add a boolean to list of constants and return its index. +*/ static int boolK (FuncState *fs, int b) { TValue o; setbvalue(&o, b); - return addk(fs, &o, &o); + return addk(fs, &o, &o); /* use boolean itself as key */ } +/* +** Add nil to list of constants and return its index. +*/ static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); @@ -380,54 +508,79 @@ static int nilK (FuncState *fs) { } +/* +** Fix an expression to return the number of results 'nresults'. +** Either 'e' is a multi-ret expression (function call or vararg) +** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). +*/ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { if (e->k == VCALL) { /* expression is an open function call? */ - SETARG_C(getcode(fs, e), nresults+1); + SETARG_C(getinstruction(fs, e), nresults + 1); } else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), nresults+1); - SETARG_A(getcode(fs, e), fs->freereg); + Instruction *pc = &getinstruction(fs, e); + SETARG_B(*pc, nresults + 1); + SETARG_A(*pc, fs->freereg); luaK_reserveregs(fs, 1); } + else lua_assert(nresults == LUA_MULTRET); } +/* +** Fix an expression to return one result. +** If expression is not a multi-ret expression (function call or +** vararg), it already returns one result, so nothing needs to be done. +** Function calls become VNONRELOC expressions (as its result comes +** fixed in the base register of the call), while vararg expressions +** become VRELOCABLE (as OP_VARARG puts its results where it wants). +** (Calls are created returning one result, so that does not need +** to be fixed.) +*/ void luaK_setoneret (FuncState *fs, expdesc *e) { if (e->k == VCALL) { /* expression is an open function call? */ - e->k = VNONRELOC; - e->u.info = GETARG_A(getcode(fs, e)); + /* already returns 1 value */ + lua_assert(GETARG_C(getinstruction(fs, e)) == 2); + e->k = VNONRELOC; /* result has fixed position */ + e->u.info = GETARG_A(getinstruction(fs, e)); } else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), 2); + SETARG_B(getinstruction(fs, e), 2); e->k = VRELOCABLE; /* can relocate its simple result */ } } +/* +** Ensure that expression 'e' is not a variable. +*/ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { - case VLOCAL: { - e->k = VNONRELOC; + case VLOCAL: { /* already in a register */ + e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } - case VUPVAL: { + case VUPVAL: { /* move value to some (pending) register */ e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); e->k = VRELOCABLE; break; } case VINDEXED: { - OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */ + OpCode op; freereg(fs, e->u.ind.idx); - if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */ + if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */ freereg(fs, e->u.ind.t); op = OP_GETTABLE; } + else { + lua_assert(e->u.ind.vt == VUPVAL); + op = OP_GETTABUP; /* 't' is in an upvalue */ + } e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOCABLE; break; } - case VVARARG: - case VCALL: { + case VVARARG: case VCALL: { luaK_setoneret(fs, e); break; } @@ -436,12 +589,10 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { } -static int code_label (FuncState *fs, int A, int b, int jump) { - luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); -} - - +/* +** Ensures expression value is in register 'reg' (and therefore +** 'e' will become a non-relocatable expression). +*/ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_dischargevars(fs, e); switch (e->k) { @@ -466,8 +617,8 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { break; } case VRELOCABLE: { - Instruction *pc = &getcode(fs, e); - SETARG_A(*pc, reg); + Instruction *pc = &getinstruction(fs, e); + SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ break; } case VNONRELOC: { @@ -476,7 +627,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { break; } default: { - lua_assert(e->k == VVOID || e->k == VJMP); + lua_assert(e->k == VJMP); return; /* nothing to do... */ } } @@ -485,17 +636,46 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { } +/* +** Ensures expression value is in any register. +*/ static void discharge2anyreg (FuncState *fs, expdesc *e) { - if (e->k != VNONRELOC) { - luaK_reserveregs(fs, 1); - discharge2reg(fs, e, fs->freereg-1); + if (e->k != VNONRELOC) { /* no fixed register yet? */ + luaK_reserveregs(fs, 1); /* get a register */ + discharge2reg(fs, e, fs->freereg-1); /* put value there */ } } +static int code_loadbool (FuncState *fs, int A, int b, int jump) { + luaK_getlabel(fs); /* those instructions may be jump targets */ + return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); +} + + +/* +** check whether list has any jump that do not produce a value +** or produce an inverted value +*/ +static int need_value (FuncState *fs, int list) { + for (; list != NO_JUMP; list = getjump(fs, list)) { + Instruction i = *getjumpcontrol(fs, list); + if (GET_OPCODE(i) != OP_TESTSET) return 1; + } + return 0; /* not found */ +} + + +/* +** Ensures final expression result (including results from its jump +** lists) is in register 'reg'. +** If expression has jumps, need to patch these jumps either to +** its final position or to "load" instructions (for those tests +** that do not produce values). +*/ static void exp2reg (FuncState *fs, expdesc *e, int reg) { discharge2reg(fs, e, reg); - if (e->k == VJMP) + if (e->k == VJMP) /* expression itself is a test? */ luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ if (hasjumps(e)) { int final; /* position after whole expression */ @@ -503,8 +683,8 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { int p_t = NO_JUMP; /* position of an eventual LOAD true */ if (need_value(fs, e->t) || need_value(fs, e->f)) { int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_label(fs, reg, 0, 1); - p_t = code_label(fs, reg, 1, 0); + p_f = code_loadbool(fs, reg, 0, 1); + p_t = code_loadbool(fs, reg, 1, 0); luaK_patchtohere(fs, fj); } final = luaK_getlabel(fs); @@ -517,6 +697,10 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { } +/* +** Ensures final expression result (including results from its jump +** lists) is in next available register. +*/ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); freeexp(fs, e); @@ -525,26 +709,39 @@ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { } +/* +** Ensures final expression result (including results from its jump +** lists) is in some (any) register and return that register. +*/ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); - if (e->k == VNONRELOC) { - if (!hasjumps(e)) return e->u.info; /* exp is already in a register */ + if (e->k == VNONRELOC) { /* expression already has a register? */ + if (!hasjumps(e)) /* no jumps? */ + return e->u.info; /* result is already in a register */ if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ - exp2reg(fs, e, e->u.info); /* put value on it */ + exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } } - luaK_exp2nextreg(fs, e); /* default */ + luaK_exp2nextreg(fs, e); /* otherwise, use next available register */ return e->u.info; } +/* +** Ensures final expression result is either in a register or in an +** upvalue. +*/ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { if (e->k != VUPVAL || hasjumps(e)) luaK_exp2anyreg(fs, e); } +/* +** Ensures final expression result is either in a register or it is +** a constant. +*/ void luaK_exp2val (FuncState *fs, expdesc *e) { if (hasjumps(e)) luaK_exp2anyreg(fs, e); @@ -553,35 +750,26 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { } +/* +** Ensures final expression result is in a valid R/K index +** (that is, it is either in a register or in 'k' with an index +** in the range of R/K indices). +** Returns R/K index. +*/ int luaK_exp2RK (FuncState *fs, expdesc *e) { luaK_exp2val(fs, e); - switch (e->k) { - case VTRUE: - case VFALSE: - case VNIL: { - if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */ - e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE)); - e->k = VK; - return RKASK(e->u.info); - } - else break; - } - case VKINT: { - e->u.info = luaK_intK(fs, e->u.ival); - e->k = VK; - goto vk; - } - case VKFLT: { - e->u.info = luaK_numberK(fs, e->u.nval); - e->k = VK; - } - /* FALLTHROUGH */ - case VK: { + switch (e->k) { /* move constants to 'k' */ + case VTRUE: e->u.info = boolK(fs, 1); goto vk; + case VFALSE: e->u.info = boolK(fs, 0); goto vk; + case VNIL: e->u.info = nilK(fs); goto vk; + case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk; + case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk; + case VK: vk: + e->k = VK; if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ return RKASK(e->u.info); else break; - } default: break; } /* not a constant in the right range: put it in a register */ @@ -589,11 +777,14 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { } +/* +** Generate code to store result of expression 'ex' into variable 'var'. +*/ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); + exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ return; } case VUPVAL: { @@ -607,29 +798,32 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); break; } - default: { - lua_assert(0); /* invalid var kind to store */ - break; - } + default: lua_assert(0); /* invalid var kind to store */ } freeexp(fs, ex); } +/* +** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). +*/ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { int ereg; luaK_exp2anyreg(fs, e); ereg = e->u.info; /* register where 'e' was placed */ freeexp(fs, e); e->u.info = fs->freereg; /* base register for op_self */ - e->k = VNONRELOC; + e->k = VNONRELOC; /* self expression has a fixed register */ luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); freeexp(fs, key); } -static void invertjump (FuncState *fs, expdesc *e) { +/* +** Negate condition 'e' (where 'e' is a comparison). +*/ +static void negatecondition (FuncState *fs, expdesc *e) { Instruction *pc = getjumpcontrol(fs, e->u.info); lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && GET_OPCODE(*pc) != OP_TEST); @@ -637,9 +831,15 @@ static void invertjump (FuncState *fs, expdesc *e) { } +/* +** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond' +** is true, code will jump if 'e' is true.) Return jump position. +** Optimize when 'e' is 'not' something, inverting the condition +** and removing the 'not'. +*/ static int jumponcond (FuncState *fs, expdesc *e, int cond) { if (e->k == VRELOCABLE) { - Instruction ie = getcode(fs, e); + Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { fs->pc--; /* remove previous OP_NOT */ return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); @@ -652,13 +852,16 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { } +/* +** Emit code to go through if 'e' is true, jump otherwise. +*/ void luaK_goiftrue (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ + int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { - case VJMP: { - invertjump(fs, e); - pc = e->u.info; + case VJMP: { /* condition? */ + negatecondition(fs, e); /* jump when it is false */ + pc = e->u.info; /* save jump position */ break; } case VK: case VKFLT: case VKINT: case VTRUE: { @@ -666,22 +869,25 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { break; } default: { - pc = jumponcond(fs, e, 0); + pc = jumponcond(fs, e, 0); /* jump when false */ break; } } - luaK_concat(fs, &e->f, pc); /* insert last jump in 'f' list */ - luaK_patchtohere(fs, e->t); + luaK_concat(fs, &e->f, pc); /* insert new jump in false list */ + luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */ e->t = NO_JUMP; } +/* +** Emit code to go through if 'e' is false, jump otherwise. +*/ void luaK_goiffalse (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ + int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { case VJMP: { - pc = e->u.info; + pc = e->u.info; /* already jump if true */ break; } case VNIL: case VFALSE: { @@ -689,29 +895,32 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) { break; } default: { - pc = jumponcond(fs, e, 1); + pc = jumponcond(fs, e, 1); /* jump if true */ break; } } - luaK_concat(fs, &e->t, pc); /* insert last jump in 't' list */ - luaK_patchtohere(fs, e->f); + luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */ + luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */ e->f = NO_JUMP; } +/* +** Code 'not e', doing constant folding. +*/ static void codenot (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); switch (e->k) { case VNIL: case VFALSE: { - e->k = VTRUE; + e->k = VTRUE; /* true == not nil == not false */ break; } case VK: case VKFLT: case VKINT: case VTRUE: { - e->k = VFALSE; + e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ break; } case VJMP: { - invertjump(fs, e); + negatecondition(fs, e); break; } case VRELOCABLE: @@ -722,30 +931,32 @@ static void codenot (FuncState *fs, expdesc *e) { e->k = VRELOCABLE; break; } - default: { - lua_assert(0); /* cannot happen */ - break; - } + default: lua_assert(0); /* cannot happen */ } /* interchange true and false lists */ { int temp = e->f; e->f = e->t; e->t = temp; } - removevalues(fs, e->f); + removevalues(fs, e->f); /* values are useless when negated */ removevalues(fs, e->t); } +/* +** Create expression 't[k]'. 't' must have its final result already in a +** register or upvalue. +*/ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - lua_assert(!hasjumps(t)); - t->u.ind.t = t->u.info; - t->u.ind.idx = luaK_exp2RK(fs, k); - t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL - : check_exp(vkisinreg(t->k), VLOCAL); + lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); + t->u.ind.t = t->u.info; /* register or upvalue index */ + t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */ + t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL; t->k = VINDEXED; } /* -** return false if folding can raise an error +** Return false if folding can raise an error. +** Bitwise operations need operands convertible to integers; division +** operations cannot have 0 as divisor. */ static int validop (int op, TValue *v1, TValue *v2) { switch (op) { @@ -762,7 +973,8 @@ static int validop (int op, TValue *v1, TValue *v2) { /* -** Try to "constant-fold" an operation; return 1 iff successful +** Try to "constant-fold" an operation; return 1 iff successful. +** (In this case, 'e1' has the final result.) */ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { TValue v1, v2, res; @@ -773,7 +985,7 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { e1->k = VKINT; e1->u.ival = ivalue(&res); } - else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */ + else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */ lua_Number n = fltvalue(&res); if (luai_numisnan(n) || n == 0) return 0; @@ -785,81 +997,97 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { /* -** Code for binary and unary expressions that "produce values" -** (arithmetic operations, bitwise operations, concat, length). First -** try to do constant folding (only for numeric [arithmetic and -** bitwise] operations, which is what 'lua_arith' accepts). -** Expression to produce final result will be encoded in 'e1'. +** Emit code for unary expressions that "produce values" +** (everything but 'not'). +** Expression to produce final result will be encoded in 'e'. */ -static void codeexpval (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - lua_assert(op >= OP_ADD); - if (op <= OP_BNOT && constfolding(fs, (op - OP_ADD) + LUA_OPADD, e1, e2)) - return; /* result has been folded */ - else { - int o1, o2; - /* move operands to registers (if needed) */ - if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) { /* unary op? */ - o2 = 0; /* no second expression */ - o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */ - } - else { /* regular case (binary operators) */ - o2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ - o1 = luaK_exp2RK(fs, e1); - } - if (o1 > o2) { /* free registers in proper order */ - freeexp(fs, e1); - freeexp(fs, e2); - } - else { - freeexp(fs, e2); - freeexp(fs, e1); - } - e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); - } +static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { + int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ + freeexp(fs, e); + e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ + e->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); } -static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, - expdesc *e2) { - int o1 = luaK_exp2RK(fs, e1); - int o2 = luaK_exp2RK(fs, e2); - freeexp(fs, e2); - freeexp(fs, e1); - if (cond == 0 && op != OP_EQ) { - int temp; /* exchange args to replace by '<' or '<=' */ - temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ - cond = 1; +/* +** Emit code for binary expressions that "produce values" +** (everything but logical operators 'and'/'or' and comparison +** operators). +** Expression to produce final result will be encoded in 'e1'. +*/ +static void codebinexpval (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int line) { + int rk1 = luaK_exp2RK(fs, e1); /* both operands are "RK" */ + int rk2 = luaK_exp2RK(fs, e2); + freeexps(fs, e1, e2); + e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ + e1->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); +} + + +/* +** Emit code for comparisons. +** 'e1' was already put in R/K form by 'luaK_infix'. +*/ +static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { + int rk1 = (e1->k == VK) ? RKASK(e1->u.info) + : check_exp(e1->k == VNONRELOC, e1->u.info); + int rk2 = luaK_exp2RK(fs, e2); + freeexps(fs, e1, e2); + switch (opr) { + case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */ + e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2); + break; + } + case OPR_GT: case OPR_GE: { + /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */ + OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); + e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */ + break; + } + default: { /* '==', '<', '<=' use their own opcodes */ + OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); + e1->u.info = condjump(fs, op, 1, rk1, rk2); + break; + } } - e1->u.info = condjump(fs, op, cond, o1, o2); e1->k = VJMP; } +/* +** Aplly prefix operation 'op' to expression 'e'. +*/ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { - expdesc e2; - e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0; + static expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; /* fake 2nd operand */ switch (op) { - case OPR_MINUS: case OPR_BNOT: case OPR_LEN: { - codeexpval(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line); + case OPR_MINUS: case OPR_BNOT: + if (constfolding(fs, op + LUA_OPUNM, e, &ef)) + break; + /* else go through */ + case OPR_LEN: + codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); break; - } case OPR_NOT: codenot(fs, e); break; default: lua_assert(0); } } +/* +** Process 1st operand 'v' of binary operation 'op' before reading +** 2nd operand. +*/ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { switch (op) { case OPR_AND: { - luaK_goiftrue(fs, v); + luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ break; } case OPR_OR: { - luaK_goiffalse(fs, v); + luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */ break; } case OPR_CONCAT: { @@ -871,7 +1099,9 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_MOD: case OPR_POW: case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { - if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); + if (!tonumeral(v, NULL)) + luaK_exp2RK(fs, v); + /* else keep numeral, which may be folded with 2nd operand */ break; } default: { @@ -882,18 +1112,24 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { } +/* +** Finalize code for binary operation, after reading 2nd operand. +** For '(a .. b .. c)' (which is '(a .. (b .. c))', because +** concatenation is right associative), merge second CONCAT into first +** one. +*/ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2, int line) { switch (op) { case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list must be closed */ + lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ luaK_dischargevars(fs, e2); luaK_concat(fs, &e2->f, e1->f); *e1 = *e2; break; } case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list must be closed */ + lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */ luaK_dischargevars(fs, e2); luaK_concat(fs, &e2->t, e1->t); *e1 = *e2; @@ -901,15 +1137,16 @@ void luaK_posfix (FuncState *fs, BinOpr op, } case OPR_CONCAT: { luaK_exp2val(fs, e2); - if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { - lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1); + if (e2->k == VRELOCABLE && + GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) { + lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1); freeexp(fs, e1); - SETARG_B(getcode(fs, e2), e1->u.info); + SETARG_B(getinstruction(fs, e2), e1->u.info); e1->k = VRELOCABLE; e1->u.info = e2->u.info; } else { luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codeexpval(fs, OP_CONCAT, e1, e2, line); + codebinexpval(fs, OP_CONCAT, e1, e2, line); } break; } @@ -917,15 +1154,13 @@ void luaK_posfix (FuncState *fs, BinOpr op, case OPR_IDIV: case OPR_MOD: case OPR_POW: case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { - codeexpval(fs, cast(OpCode, (op - OPR_ADD) + OP_ADD), e1, e2, line); - break; - } - case OPR_EQ: case OPR_LT: case OPR_LE: { - codecomp(fs, cast(OpCode, (op - OPR_EQ) + OP_EQ), 1, e1, e2); + if (!constfolding(fs, op + LUA_OPADD, e1, e2)) + codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line); break; } + case OPR_EQ: case OPR_LT: case OPR_LE: case OPR_NE: case OPR_GT: case OPR_GE: { - codecomp(fs, cast(OpCode, (op - OPR_NE) + OP_EQ), 0, e1, e2); + codecomp(fs, op, e1, e2); break; } default: lua_assert(0); @@ -933,15 +1168,25 @@ void luaK_posfix (FuncState *fs, BinOpr op, } +/* +** Change line information associated with current position. +*/ void luaK_fixline (FuncState *fs, int line) { fs->f->sp->lineinfo[fs->pc - 1] = line; } +/* +** Emit a SETLIST instruction. +** 'base' is register that keeps table; +** 'nelems' is #table plus those to be stored now; +** 'tostore' is number of values (in registers 'base + 1',...) to add to +** table (or LUA_MULTRET to add up to stack top). +*/ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; int b = (tostore == LUA_MULTRET) ? 0 : tostore; - lua_assert(tostore != 0); + lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); if (c <= MAXARG_C) luaK_codeABC(fs, OP_SETLIST, base, b, c); else if (c <= MAXARG_Ax) { diff --git a/3rd/lua/lcode.h b/3rd/lua/lcode.h index b4c865f9..7a867434 100644 --- a/3rd/lua/lcode.h +++ b/3rd/lua/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 roberto Exp $ +** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -40,7 +40,8 @@ typedef enum BinOpr { typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; -#define getcode(fs,e) ((fs)->f->sp->code[(e)->u.info]) +/* get (pointer to) instruction of given 'expdesc' */ +#define getinstruction(fs,e) ((fs)->f->sp->code[(e)->u.info]) #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) diff --git a/3rd/lua/lcorolib.c b/3rd/lua/lcorolib.c index 0c0b7fa6..2303429e 100644 --- a/3rd/lua/lcorolib.c +++ b/3rd/lua/lcorolib.c @@ -1,5 +1,5 @@ /* -** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp $ +** $Id: lcorolib.c,v 1.10 2016/04/11 19:19:55 roberto Exp $ ** Coroutine Library ** See Copyright Notice in lua.h */ @@ -75,7 +75,7 @@ static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { - if (lua_isstring(L, -1)) { /* error object is a string? */ + if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ luaL_where(L, 1); /* add extra info */ lua_insert(L, -2); lua_concat(L, 2); diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index 9d66f955..a5df4d61 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.117 2015/11/02 18:48:07 roberto Exp $ +** $Id: ldebug.c,v 2.120 2016/03/31 19:01:21 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -69,7 +69,13 @@ static void swapextra (lua_State *L) { /* -** this function can be called asynchronous (e.g. during a signal) +** This function can be called asynchronously (e.g. during a signal). +** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by +** 'resethookcount') are for debug only, and it is no problem if they +** get arbitrary values (causes at most one wrong hook call). 'hookmask' +** is an atomic value. We assume that pointers are atomic too (e.g., gcc +** ensures that for all platforms where it runs). Moreover, 'hook' is +** always checked before being called (see 'luaD_hook'). */ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { if (func == NULL || mask == 0) { /* turn off hooks? */ @@ -558,7 +564,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { - const char *t = objtypename(o); + const char *t = luaT_objtypename(L, o); luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); } @@ -590,9 +596,9 @@ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { - const char *t1 = objtypename(p1); - const char *t2 = objtypename(p2); - if (t1 == t2) + const char *t1 = luaT_objtypename(L, p1); + const char *t2 = luaT_objtypename(L, p2); + if (strcmp(t1, t2) == 0) luaG_runerror(L, "attempt to compare two %s values", t1); else luaG_runerror(L, "attempt to compare %s with %s", t1, t2); diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index db222be8..a73e4214 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.150 2015/11/19 19:16:22 roberto Exp $ +** $Id: ldo.c,v 2.151 2015/12/16 16:40:07 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -242,9 +242,14 @@ void luaD_inctop (lua_State *L) { /* }================================================================== */ +/* +** Call a hook for the given event. Make sure there is a hook to be +** called. (Both 'L->hook' and 'L->hookmask', which triggers this +** function, can be changed asynchronously by signals.) +*/ void luaD_hook (lua_State *L, int event, int line) { lua_Hook hook = L->hook; - if (hook && L->allowhook) { + if (hook && L->allowhook) { /* make sure there is a hook */ CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top); ptrdiff_t ci_top = savestack(L, ci->top); diff --git a/3rd/lua/ldo.h b/3rd/lua/ldo.h index 80582dc2..4f5d51c3 100644 --- a/3rd/lua/ldo.h +++ b/3rd/lua/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.28 2015/11/23 11:29:43 roberto Exp $ +** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -25,7 +25,7 @@ { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } /* In general, 'pre'/'pos' are empty (nothing to save) */ -#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,,) +#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 3925f2ca..fd33b166 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.210 2015/11/03 18:10:44 roberto Exp $ +** $Id: lgc.c,v 2.212 2016/03/31 19:02:03 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -761,14 +761,11 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { /* ** sweep a list until a live object (or end of list) */ -static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { +static GCObject **sweeptolive (lua_State *L, GCObject **p) { GCObject **old = p; - int i = 0; do { - i++; p = sweeplist(L, p, 1); } while (p == old); - if (n) *n += i; return p; } @@ -863,10 +860,10 @@ static int runafewfinalizers (lua_State *L) { /* ** call all pending finalizers */ -static void callallpendingfinalizers (lua_State *L, int propagateerrors) { +static void callallpendingfinalizers (lua_State *L) { global_State *g = G(L); while (g->tobefnz) - GCTM(L, propagateerrors); + GCTM(L, 0); } @@ -916,7 +913,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { if (issweepphase(g)) { makewhite(g, o); /* "sweep" object 'o' */ if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ - g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */ + g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } /* search for pointer pointing to 'o' */ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } @@ -958,19 +955,16 @@ static void setpause (global_State *g) { /* ** Enter first sweep phase. -** The call to 'sweeptolive' makes pointer point to an object inside -** the list (instead of to the header), so that the real sweep do not -** need to skip objects created between "now" and the start of the real -** sweep. -** Returns how many objects it swept. +** The call to 'sweeplist' tries to make pointer point to an object +** inside the list (instead of to the header), so that the real sweep do +** not need to skip objects created between "now" and the start of the +** real sweep. */ -static int entersweep (lua_State *L) { +static void entersweep (lua_State *L) { global_State *g = G(L); - int n = 0; g->gcstate = GCSswpallgc; lua_assert(g->sweepgc == NULL); - g->sweepgc = sweeptolive(L, &g->allgc, &n); - return n; + g->sweepgc = sweeplist(L, &g->allgc, 1); } @@ -978,7 +972,7 @@ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); - callallpendingfinalizers(L, 0); + callallpendingfinalizers(L); lua_assert(g->tobefnz == NULL); g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ g->gckind = KGC_NORMAL; @@ -1071,12 +1065,11 @@ static lu_mem singlestep (lua_State *L) { } case GCSatomic: { lu_mem work; - int sw; propagateall(g); /* make sure gray list is empty */ work = atomic(L); /* work is what was traversed by 'atomic' */ - sw = entersweep(L); + entersweep(L); g->GCestimate = gettotalbytes(g); /* first estimate */; - return work + sw * GCSWEEPCOST; + return work; } case GCSswpallgc: { /* sweep "regular" objects */ return sweepstep(L, g, GCSswpfinobj, &g->finobj); diff --git a/3rd/lua/lgc.h b/3rd/lua/lgc.h index 1775ca45..aed3e18a 100644 --- a/3rd/lua/lgc.h +++ b/3rd/lua/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.90 2015/10/21 18:15:15 roberto Exp $ +** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -112,7 +112,7 @@ condchangemem(L,pre,pos); } /* more often than not, 'pre'/'pos' are empty */ -#define luaC_checkGC(L) luaC_condGC(L,,) +#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) #define luaC_barrier(L,p,v) ( \ diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index a91ba391..aa78e593 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.148 2015/11/23 11:36:11 roberto Exp $ +** $Id: liolib.c,v 2.149 2016/05/02 14:03:19 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -375,14 +375,17 @@ static int io_lines (lua_State *L) { /* maximum length of a numeral */ -#define MAXRN 200 +#if !defined (L_MAXLENNUM) +#define L_MAXLENNUM 200 +#endif + /* auxiliary structure used by 'read_number' */ typedef struct { FILE *f; /* file being read */ int c; /* current character (look ahead) */ int n; /* number of elements in buffer 'buff' */ - char buff[MAXRN + 1]; /* +1 for ending '\0' */ + char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ } RN; @@ -390,7 +393,7 @@ typedef struct { ** Add current char to buffer (if not out of space) and read next one */ static int nextc (RN *rn) { - if (rn->n >= MAXRN) { /* buffer overflow? */ + if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ rn->buff[0] = '\0'; /* invalidate result */ return 0; /* fail */ } @@ -403,10 +406,10 @@ static int nextc (RN *rn) { /* -** Accept current char if it is in 'set' (of size 1 or 2) +** Accept current char if it is in 'set' (of size 2) */ static int test2 (RN *rn, const char *set) { - if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0')) + if (rn->c == set[0] || rn->c == set[1]) return nextc(rn); else return 0; } @@ -435,11 +438,11 @@ static int read_number (lua_State *L, FILE *f) { char decp[2]; rn.f = f; rn.n = 0; decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ - decp[1] = '\0'; + decp[1] = '.'; /* always accept a dot */ l_lockfile(rn.f); do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ test2(&rn, "-+"); /* optional signal */ - if (test2(&rn, "0")) { + if (test2(&rn, "00")) { if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ else count = 1; /* count initial '0' as a valid digit */ } diff --git a/3rd/lua/llex.c b/3rd/lua/llex.c index 16ea3ebe..70328273 100644 --- a/3rd/lua/llex.c +++ b/3rd/lua/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.95 2015/11/19 19:16:22 roberto Exp $ +** $Id: llex.c,v 2.96 2016/05/02 14:02:12 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -162,7 +162,6 @@ static void inclinenumber (LexState *ls) { void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, int firstchar) { ls->t.token = 0; - ls->decpoint = '.'; ls->L = L; ls->current = firstchar; ls->lookahead.token = TK_EOS; /* no look-ahead token */ @@ -207,35 +206,6 @@ static int check_next2 (LexState *ls, const char *set) { } -/* -** change all characters 'from' in buffer to 'to' -*/ -static void buffreplace (LexState *ls, char from, char to) { - if (from != to) { - size_t n = luaZ_bufflen(ls->buff); - char *p = luaZ_buffer(ls->buff); - while (n--) - if (p[n] == from) p[n] = to; - } -} - - -/* -** in case of format error, try to change decimal point separator to -** the one defined in the current locale and check again -*/ -static void trydecpoint (LexState *ls, TValue *o) { - char old = ls->decpoint; - ls->decpoint = lua_getlocaledecpoint(); - buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ - if (luaO_str2num(luaZ_buffer(ls->buff), o) == 0) { - /* format error with correct decimal point: no more options */ - buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ - lexerror(ls, "malformed number", TK_FLT); - } -} - - /* LUA_NUMBER */ /* ** this function is quite liberal in what it accepts, as 'luaO_str2num' @@ -259,9 +229,8 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { else break; } save(ls, '\0'); - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ - trydecpoint(ls, &obj); /* try to update decimal point separator */ + lexerror(ls, "malformed number", TK_FLT); if (ttisinteger(&obj)) { seminfo->i = ivalue(&obj); return TK_INT; diff --git a/3rd/lua/llex.h b/3rd/lua/llex.h index afb40b56..2363d87e 100644 --- a/3rd/lua/llex.h +++ b/3rd/lua/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp $ +** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -69,7 +69,6 @@ typedef struct LexState { struct Dyndata *dyd; /* dynamic structures used by the parser */ TString *source; /* current source name */ TString *envn; /* environment variable name */ - char decpoint; /* locale decimal point */ } LexState; diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index e24723fe..04f45da2 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.108 2015/11/02 16:09:30 roberto Exp $ +** $Id: lobject.c,v 2.110 2016/05/02 14:00:32 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -243,17 +243,53 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { /* }====================================================== */ -static const char *l_str2d (const char *s, lua_Number *result) { +/* maximum length of a numeral */ +#if !defined (L_MAXLENNUM) +#define L_MAXLENNUM 200 +#endif + +static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { char *endptr; - if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ + *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ + : lua_str2number(s, &endptr); + if (endptr == s) return NULL; /* nothing recognized? */ + while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ + return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */ +} + + +/* +** Convert string 's' to a Lua number (put in 'result'). Return NULL +** on fail or the address of the ending '\0' on success. +** 'pmode' points to (and 'mode' contains) special things in the string: +** - 'x'/'X' means an hexadecimal numeral +** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) +** - '.' just optimizes the search for the common case (nothing special) +** This function accepts both the current locale or a dot as the radix +** mark. If the convertion fails, it may mean number has a dot but +** locale accepts something else. In that case, the code copies 's' +** to a buffer (because 's' is read-only), changes the dot to the +** current locale radix mark, and tries to convert again. +*/ +static const char *l_str2d (const char *s, lua_Number *result) { + const char *endptr; + const char *pmode = strpbrk(s, ".xXnN"); + int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; + if (mode == 'n') /* reject 'inf' and 'nan' */ return NULL; - else if (strpbrk(s, "xX")) /* hex? */ - *result = lua_strx2number(s, &endptr); - else - *result = lua_str2number(s, &endptr); - if (endptr == s) return NULL; /* nothing recognized */ - while (lisspace(cast_uchar(*endptr))) endptr++; - return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */ + endptr = l_str2dloc(s, result, mode); /* try to convert */ + if (endptr == NULL) { /* failed? may be a different locale */ + char buff[L_MAXLENNUM + 1]; + char *pdot = strchr(s, '.'); + if (strlen(s) > L_MAXLENNUM || pdot == NULL) + return NULL; /* string too long or no dot; fail */ + strcpy(buff, s); /* copy string to buffer */ + buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ + endptr = l_str2dloc(buff, result, mode); /* try again */ + if (endptr != NULL) + endptr = s + (endptr - buff); /* make relative to 's' */ + } + return endptr; } @@ -351,8 +387,10 @@ static void pushstr (lua_State *L, const char *str, size_t l) { } -/* this function handles only '%d', '%c', '%f', '%p', and '%s' - conventional formats, plus Lua-specific '%I' and '%U' */ +/* +** this function handles only '%d', '%c', '%f', '%p', and '%s' + conventional formats, plus Lua-specific '%I' and '%U' +*/ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { int n = 0; for (;;) { @@ -360,13 +398,13 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { if (e == NULL) break; pushstr(L, fmt, e - fmt); switch (*(e+1)) { - case 's': { + case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; pushstr(L, s, strlen(s)); break; } - case 'c': { + case 'c': { /* an 'int' as a character */ char buff = cast(char, va_arg(argp, int)); if (lisprint(cast_uchar(buff))) pushstr(L, &buff, 1); @@ -374,28 +412,28 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); break; } - case 'd': { + case 'd': { /* an 'int' */ setivalue(L->top, va_arg(argp, int)); goto top2str; } - case 'I': { + case 'I': { /* a 'lua_Integer' */ setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); goto top2str; } - case 'f': { + case 'f': { /* a 'lua_Number' */ setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); - top2str: + top2str: /* convert the top element to a string */ luaD_inctop(L); luaO_tostring(L, L->top - 1); break; } - case 'p': { + case 'p': { /* a pointer */ char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *)); pushstr(L, buff, l); break; } - case 'U': { + case 'U': { /* an 'int' as a UTF-8 sequence */ char buff[UTF8BUFFSZ]; int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); pushstr(L, buff + UTF8BUFFSZ - l, l); diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 7dae5336..48106555 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.60 2015/11/19 19:16:22 roberto Exp $ +** $Id: loslib.c,v 1.64 2016/04/18 13:06:55 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -24,18 +24,29 @@ /* ** {================================================================== -** list of valid conversion specifiers for the 'strftime' function +** List of valid conversion specifiers for the 'strftime' function; +** options are grouped by length; group of length 2 start with '||'. ** =================================================================== */ #if !defined(LUA_STRFTIMEOPTIONS) /* { */ -#if defined(LUA_USE_C89) -#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } +/* options for ANSI C 89 */ +#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%" + +/* options for ISO C 99 and POSIX */ +#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ + "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" + +/* options for Windows */ +#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ + "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" + +#if defined(LUA_USE_WINDOWS) +#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN +#elif defined(LUA_USE_C89) +#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89 #else /* C99 specification */ -#define LUA_STRFTIMEOPTIONS \ - { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \ - "E", "cCxXyY", \ - "O", "deHImMSuUVwWy" } +#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 #endif #endif /* } */ @@ -195,6 +206,23 @@ static void setboolfield (lua_State *L, const char *key, int value) { lua_setfield(L, -2, key); } + +/* +** Set all fields from structure 'tm' in the table on top of the stack +*/ +static void setallfields (lua_State *L, struct tm *stm) { + setfield(L, "sec", stm->tm_sec); + setfield(L, "min", stm->tm_min); + setfield(L, "hour", stm->tm_hour); + setfield(L, "day", stm->tm_mday); + setfield(L, "month", stm->tm_mon + 1); + setfield(L, "year", stm->tm_year + 1900); + setfield(L, "wday", stm->tm_wday + 1); + setfield(L, "yday", stm->tm_yday + 1); + setboolfield(L, "isdst", stm->tm_isdst); +} + + static int getboolfield (lua_State *L, const char *key) { int res; res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); @@ -210,18 +238,18 @@ static int getboolfield (lua_State *L, const char *key) { static int getfield (lua_State *L, const char *key, int d, int delta) { int isnum; - int t = lua_getfield(L, -1, key); + int t = lua_getfield(L, -1, key); /* get field and its type */ lua_Integer res = lua_tointegerx(L, -1, &isnum); - if (!isnum) { /* field is not a number? */ + if (!isnum) { /* field is not an integer? */ if (t != LUA_TNIL) /* some other value? */ - return luaL_error(L, "field '%s' not an integer", key); + return luaL_error(L, "field '%s' is not an integer", key); else if (d < 0) /* absent field; no default? */ return luaL_error(L, "field '%s' missing in date table", key); res = d; } else { if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) - return luaL_error(L, "field '%s' out-of-bounds", key); + return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } lua_pop(L, 1); @@ -230,21 +258,15 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { static const char *checkoption (lua_State *L, const char *conv, char *buff) { - static const char *const options[] = LUA_STRFTIMEOPTIONS; - unsigned int i; - for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) { - if (*conv != '\0' && strchr(options[i], *conv) != NULL) { - buff[1] = *conv; - if (*options[i + 1] == '\0') { /* one-char conversion specifier? */ - buff[2] = '\0'; /* end buffer */ - return conv + 1; - } - else if (*(conv + 1) != '\0' && - strchr(options[i + 1], *(conv + 1)) != NULL) { - buff[2] = *(conv + 1); /* valid two-char conversion specifier */ - buff[3] = '\0'; /* end buffer */ - return conv + 2; - } + const char *option; + int oplen = 1; + for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { + if (*option == '|') /* next block? */ + oplen++; /* next length */ + else if (memcmp(conv, option, oplen) == 0) { /* match? */ + memcpy(buff, conv, oplen); /* copy valid option to buffer */ + buff[oplen] = '\0'; + return conv + oplen; /* return next item */ } } luaL_argerror(L, 1, @@ -271,18 +293,10 @@ static int os_date (lua_State *L) { luaL_error(L, "time result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon+1); - setfield(L, "year", stm->tm_year+1900); - setfield(L, "wday", stm->tm_wday+1); - setfield(L, "yday", stm->tm_yday+1); - setboolfield(L, "isdst", stm->tm_isdst); + setallfields(L, stm); } else { - char cc[4]; + char cc[4]; /* buffer for individual conversion specifiers */ luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); @@ -292,7 +306,7 @@ static int os_date (lua_State *L) { else { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); - s = checkoption(L, s + 1, cc); + s = checkoption(L, s + 1, cc + 1); /* copy specifier to 'cc' */ reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } @@ -319,6 +333,7 @@ static int os_time (lua_State *L) { ts.tm_year = getfield(L, "year", -1, 1900); ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); + setallfields(L, &ts); /* update fields with normalized values */ } if (t != (time_t)(l_timet)t || t == (time_t)(-1)) luaL_error(L, "time result cannot be represented in this installation"); diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 53c5cdd5..bdc5f3ed 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.149 2015/11/02 16:09:30 roberto Exp $ +** $Id: lparser.c,v 2.152 2016/03/07 19:25:39 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -165,7 +165,8 @@ static int registerlocalvar (LexState *ls, TString *varname) { int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); - while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; + while (oldsize < f->sizelocvars) + f->locvars[oldsize++].varname = NULL; f->locvars[fs->nlocvars].varname = varname; luaC_objbarrier(ls->L, fp, varname); return fs->nlocvars++; @@ -232,7 +233,8 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, Upvaldesc, MAXUPVAL, "upvalues"); - while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; + while (oldsize < f->sizeupvalues) + f->upvalues[oldsize++].name = NULL; f->upvalues[fs->nups].instack = (v->k == VLOCAL); f->upvalues[fs->nups].idx = cast_byte(v->u.info); f->upvalues[fs->nups].name = name; @@ -257,7 +259,8 @@ static int searchvar (FuncState *fs, TString *n) { */ static void markupval (FuncState *fs, int level) { BlockCnt *bl = fs->bl; - while (bl->nactvar > level) bl = bl->previous; + while (bl->nactvar > level) + bl = bl->previous; bl->upval = 1; } @@ -501,7 +504,8 @@ static Proto *addprototype (LexState *ls) { if (fs->np >= f->sp->sizep) { int oldsize = f->sp->sizep; luaM_growvector(L, f->p, fs->np, f->sp->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sp->sizep) f->p[oldsize++] = NULL; + while (oldsize < f->sp->sizep) + f->p[oldsize++] = NULL; } f->p[fs->np++] = clp = luaF_newproto(L, NULL); luaC_objbarrier(L, f, clp); @@ -1497,7 +1501,7 @@ static void exprstat (LexState *ls) { } else { /* stat -> func */ check_condition(ls, v.v.k == VCALL, "syntax error"); - SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ + SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */ } } @@ -1514,8 +1518,8 @@ static void retstat (LexState *ls) { if (hasmultret(e.k)) { luaK_setmultret(fs, &e); if (e.k == VCALL && nret == 1) { /* tail call? */ - SET_OPCODE(getcode(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); + SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); + lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); } first = fs->nactvar; nret = LUA_MULTRET; /* return all values */ diff --git a/3rd/lua/lparser.h b/3rd/lua/lparser.h index 62c50cac..02e9b03a 100644 --- a/3rd/lua/lparser.h +++ b/3rd/lua/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.74 2014/10/25 11:50:46 roberto Exp $ +** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -13,25 +13,38 @@ /* -** Expression descriptor +** Expression and variable descriptor. +** Code generation for variables and expressions can be delayed to allow +** optimizations; An 'expdesc' structure describes a potentially-delayed +** variable/expression. It has a description of its "main" value plus a +** list of conditional jumps that can also produce its value (generated +** by short-circuit operators 'and'/'or'). */ +/* kinds of variables/expressions */ typedef enum { - VVOID, /* no value */ - VNIL, - VTRUE, - VFALSE, - VK, /* info = index of constant in 'k' */ - VKFLT, /* nval = numerical float value */ - VKINT, /* nval = numerical integer value */ - VNONRELOC, /* info = result register */ - VLOCAL, /* info = local register */ - VUPVAL, /* info = index of upvalue in 'upvalues' */ - VINDEXED, /* t = table register/upvalue; idx = index R/K */ - VJMP, /* info = instruction pc */ - VRELOCABLE, /* info = instruction pc */ - VCALL, /* info = instruction pc */ - VVARARG /* info = instruction pc */ + VVOID, /* when 'expdesc' describes the last expression a list, + this kind means an empty list (so, no expression) */ + VNIL, /* constant nil */ + VTRUE, /* constant true */ + VFALSE, /* constant false */ + VK, /* constant in 'k'; info = index of constant in 'k' */ + VKFLT, /* floating constant; nval = numerical float value */ + VKINT, /* integer constant; nval = numerical integer value */ + VNONRELOC, /* expression has its value in a fixed register; + info = result register */ + VLOCAL, /* local variable; info = local register */ + VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ + VINDEXED, /* indexed variable; + ind.vt = whether 't' is register or upvalue; + ind.t = table register or upvalue; + ind.idx = key's R/K index */ + VJMP, /* expression is a test/comparison; + info = pc of corresponding jump instruction */ + VRELOCABLE, /* expression can put result in any register; + info = instruction pc */ + VCALL, /* expression is a function call; info = instruction pc */ + VVARARG /* vararg expression; info = instruction pc */ } expkind; @@ -41,14 +54,14 @@ typedef enum { typedef struct expdesc { expkind k; union { + lua_Integer ival; /* for VKINT */ + lua_Number nval; /* for VKFLT */ + int info; /* for generic use */ struct { /* for indexed variables (VINDEXED) */ short idx; /* index (R/K) */ lu_byte t; /* table (register or upvalue) */ lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ } ind; - int info; /* for generic use */ - lua_Number nval; /* for VKFLT */ - lua_Integer ival; /* for VKINT */ } u; int t; /* patch list of 'exit when true' */ int f; /* patch list of 'exit when false' */ diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index 65c914d2..b3033bee 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.128 2015/11/13 12:16:51 roberto Exp $ +** $Id: lstate.h,v 2.130 2015/12/16 16:39:38 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -33,6 +33,15 @@ struct lua_longjmp; /* defined in ldo.c */ +/* +** Atomic type (relative to signals) to better ensure that 'lua_sethook' +** is thread safe +*/ +#if !defined(l_signalT) +#include +#define l_signalT sig_atomic_t +#endif + /* extra stack space to handle TM calls and some other extras */ #define EXTRA_STACK 5 @@ -162,14 +171,14 @@ struct lua_State { struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ - lua_Hook hook; + volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; int basehookcount; int hookcount; unsigned short nny; /* number of non-yieldable calls in stack */ unsigned short nCcalls; /* number of nested C calls */ - lu_byte hookmask; + l_signalT hookmask; lu_byte allowhook; }; diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index bc6fc4ea..a610a0d7 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.239 2015/11/25 16:28:17 roberto Exp $ +** $Id: lstrlib.c,v 1.248 2016/05/02 13:58:01 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -26,7 +27,8 @@ /* ** maximum number of captures that a pattern can do during -** pattern-matching. This limit is arbitrary. +** pattern-matching. This limit is arbitrary, but must fit in +** an unsigned char. */ #if !defined(LUA_MAXCAPTURES) #define LUA_MAXCAPTURES 32 @@ -214,9 +216,8 @@ typedef struct MatchState { const char *src_end; /* end ('\0') of source string */ const char *p_end; /* end ('\0') of pattern */ lua_State *L; - size_t nrep; /* limit to avoid non-linear complexity */ int matchdepth; /* control for recursive depth (to avoid C stack overflow) */ - int level; /* total number of captures (finished or unfinished) */ + unsigned char level; /* total number of captures (finished or unfinished) */ struct { const char *init; ptrdiff_t len; @@ -234,17 +235,6 @@ static const char *match (MatchState *ms, const char *s, const char *p); #endif -/* -** parameters to control the maximum number of operators handled in -** a match (to avoid non-linear complexity). The maximum will be: -** (subject length) * A_REPS + B_REPS -*/ -#if !defined(A_REPS) -#define A_REPS 4 -#define B_REPS 100000 -#endif - - #define L_ESC '%' #define SPECIALS "^$*+?.([%-" @@ -502,8 +492,6 @@ static const char *match (MatchState *ms, const char *s, const char *p) { s = NULL; /* fail */ } else { /* matched once */ - if (ms->nrep-- == 0) - luaL_error(ms->L, "pattern too complex"); switch (*ep) { /* handle optional suffix */ case '?': { /* optional */ const char *res; @@ -607,10 +595,6 @@ static void prepstate (MatchState *ms, lua_State *L, ms->src_init = s; ms->src_end = s + ls; ms->p_end = p + lp; - if (ls < (MAX_SIZET - B_REPS) / A_REPS) - ms->nrep = A_REPS * ls + B_REPS; - else /* overflow (very long subject) */ - ms->nrep = MAX_SIZET; /* no limit */ } @@ -681,6 +665,7 @@ static int str_match (lua_State *L) { typedef struct GMatchState { const char *src; /* current position */ const char *p; /* pattern */ + const char *lastmatch; /* end of last match */ MatchState ms; /* match state */ } GMatchState; @@ -692,11 +677,8 @@ static int gmatch_aux (lua_State *L) { for (src = gm->src; src <= gm->ms.src_end; src++) { const char *e; reprepstate(&gm->ms); - if ((e = match(&gm->ms, src, gm->p)) != NULL) { - if (e == src) /* empty match? */ - gm->src =src + 1; /* go at least one position */ - else - gm->src = e; + if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) { + gm->src = gm->lastmatch = e; return push_captures(&gm->ms, src, e); } } @@ -712,7 +694,7 @@ static int gmatch (lua_State *L) { lua_settop(L, 2); /* keep them on closure to avoid being collected */ gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState)); prepstate(&gm->ms, L, s, ls, p, lp); - gm->src = s; gm->p = p; + gm->src = s; gm->p = p; gm->lastmatch = NULL; lua_pushcclosure(L, gmatch_aux, 3); return 1; } @@ -779,12 +761,13 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, static int str_gsub (lua_State *L) { size_t srcl, lp; - const char *src = luaL_checklstring(L, 1, &srcl); - const char *p = luaL_checklstring(L, 2, &lp); - int tr = lua_type(L, 3); - lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); + const char *src = luaL_checklstring(L, 1, &srcl); /* subject */ + const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ + const char *lastmatch = NULL; /* end of last match */ + int tr = lua_type(L, 3); /* replacement type */ + lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ int anchor = (*p == '^'); - lua_Integer n = 0; + lua_Integer n = 0; /* replacement count */ MatchState ms; luaL_Buffer b; luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || @@ -797,16 +780,15 @@ static int str_gsub (lua_State *L) { prepstate(&ms, L, src, srcl, p, lp); while (n < max_s) { const char *e; - reprepstate(&ms); - if ((e = match(&ms, src, p)) != NULL) { + reprepstate(&ms); /* (re)prepare state for new match */ + if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ n++; - add_value(&ms, &b, src, e, tr); + add_value(&ms, &b, src, e, tr); /* add replacement to buffer */ + src = lastmatch = e; } - if (e && e>src) /* non empty match? */ - src = e; /* skip it */ - else if (src < ms.src_end) + else if (src < ms.src_end) /* otherwise, skip one character */ luaL_addchar(&b, *src++); - else break; + else break; /* end of subject */ if (anchor) break; } luaL_addlstring(&b, src, ms.src_end-src); @@ -831,7 +813,6 @@ static int str_gsub (lua_State *L) { ** Hexadecimal floating-point formatter */ -#include #include #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) @@ -923,16 +904,14 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, #define MAX_FORMAT 32 -static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); +static void addquoted (luaL_Buffer *b, const char *s, size_t len) { luaL_addchar(b, '"'); - while (l--) { + while (len--) { if (*s == '"' || *s == '\\' || *s == '\n') { luaL_addchar(b, '\\'); luaL_addchar(b, *s); } - else if (*s == '\0' || iscntrl(uchar(*s))) { + else if (iscntrl(uchar(*s))) { char buff[10]; if (!isdigit(uchar(*(s+1)))) l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); @@ -947,6 +926,52 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { luaL_addchar(b, '"'); } + +/* +** Convert a Lua number to a string in floating-point hexadecimal +** format. Ensures the resulting string uses a dot as the radix +** character. +*/ +static void addliteralnum (lua_State *L, luaL_Buffer *b, lua_Number n) { + char *buff = luaL_prepbuffsize(b, MAX_ITEM); + int nb = lua_number2strx(L, buff, MAX_ITEM, + "%" LUA_NUMBER_FRMLEN "a", n); + if (memchr(buff, '.', nb) == NULL) { /* no dot? */ + char point = lua_getlocaledecpoint(); /* try locale point */ + char *ppoint = memchr(buff, point, nb); + if (ppoint) *ppoint = '.'; /* change it to a dot */ + } + luaL_addsize(b, nb); +} + + +static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { + switch (lua_type(L, arg)) { + case LUA_TSTRING: { + size_t len; + const char *s = lua_tolstring(L, arg, &len); + addquoted(b, s, len); + break; + } + case LUA_TNUMBER: { + if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */ + addliteralnum(L, b, lua_tonumber(L, arg)); + break; + } + /* else integers; write in "native" format *//* FALLTHROUGH */ + } + case LUA_TNIL: case LUA_TBOOLEAN: { + luaL_tolstring(L, arg, NULL); + luaL_addvalue(b); + break; + } + default: { + luaL_argerror(L, arg, "value has no literal form"); + } + } +} + + static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { const char *p = strfrmt; while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ @@ -1026,7 +1051,7 @@ static int str_format (lua_State *L) { break; } case 'q': { - addquoted(L, &b, arg); + addliteral(L, &b, arg); break; } case 's': { @@ -1071,8 +1096,8 @@ static int str_format (lua_State *L) { /* value used for padding */ -#if !defined(LUA_PACKPADBYTE) -#define LUA_PACKPADBYTE 0x00 +#if !defined(LUAL_PACKPADBYTE) +#define LUAL_PACKPADBYTE 0x00 #endif /* maximum size for the binary representation of an integer */ @@ -1309,7 +1334,7 @@ static int str_pack (lua_State *L) { KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); totalsize += ntoalign + size; while (ntoalign-- > 0) - luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ + luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */ arg++; switch (opt) { case Kint: { /* signed integers */ @@ -1349,7 +1374,7 @@ static int str_pack (lua_State *L) { else { /* string smaller than needed */ luaL_addlstring(&b, s, len); /* add it all */ while (len++ < (size_t)size) /* pad extra space */ - luaL_addchar(&b, LUA_PACKPADBYTE); + luaL_addchar(&b, LUAL_PACKPADBYTE); } break; } @@ -1373,7 +1398,7 @@ static int str_pack (lua_State *L) { totalsize += len + 1; break; } - case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */ + case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */ case Kpaddalign: case Knop: arg--; /* undo increment */ break; diff --git a/3rd/lua/ltablib.c b/3rd/lua/ltablib.c index b3c9a7c5..98b2f871 100644 --- a/3rd/lua/ltablib.c +++ b/3rd/lua/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.90 2015/11/25 12:48:57 roberto Exp $ +** $Id: ltablib.c,v 1.93 2016/02/25 19:41:54 roberto Exp $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -53,7 +53,7 @@ static void checktab (lua_State *L, int arg, int what) { lua_pop(L, n); /* pop metatable and tested metamethods */ } else - luaL_argerror(L, arg, "table expected"); /* force an error */ + luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ } } @@ -139,7 +139,7 @@ static int tmove (lua_State *L) { n = e - f + 1; /* number of elements to move */ luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, "destination wrap around"); - if (t > e || t <= f || tt != 1) { + if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { for (i = 0; i < n; i++) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); @@ -152,7 +152,7 @@ static int tmove (lua_State *L) { } } } - lua_pushvalue(L, tt); /* return "to table" */ + lua_pushvalue(L, tt); /* return destination table */ return 1; } @@ -172,7 +172,7 @@ static int tconcat (lua_State *L) { size_t lsep; const char *sep = luaL_optlstring(L, 2, "", &lsep); lua_Integer i = luaL_optinteger(L, 3, 1); - last = luaL_opt(L, luaL_checkinteger, 4, last); + last = luaL_optinteger(L, 4, last); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, i); @@ -232,6 +232,10 @@ static int unpack (lua_State *L) { */ +/* type for array indices */ +typedef unsigned int IdxT; + + /* ** Produce a "random" 'unsigned int' to randomize pivot choice. This ** macro is used only when 'sort' detects a big imbalance in the result @@ -270,7 +274,7 @@ static unsigned int l_randomizePivot (void) { #define RANLIMIT 100u -static void set2 (lua_State *L, unsigned int i, unsigned int j) { +static void set2 (lua_State *L, IdxT i, IdxT j) { lua_seti(L, 1, i); lua_seti(L, 1, j); } @@ -303,10 +307,9 @@ static int sort_comp (lua_State *L, int a, int b) { ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] ** returns 'i'. */ -static unsigned int partition (lua_State *L, unsigned int lo, - unsigned int up) { - unsigned int i = lo; /* will be incremented before first use */ - unsigned int j = up - 1; /* will be decremented before first use */ +static IdxT partition (lua_State *L, IdxT lo, IdxT up) { + IdxT i = lo; /* will be incremented before first use */ + IdxT j = up - 1; /* will be decremented before first use */ /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ for (;;) { /* next loop: repeat ++i while a[i] < P */ @@ -340,10 +343,9 @@ static unsigned int partition (lua_State *L, unsigned int lo, ** Choose an element in the middle (2nd-3th quarters) of [lo,up] ** "randomized" by 'rnd' */ -static unsigned int choosePivot (unsigned int lo, unsigned int up, - unsigned int rnd) { - unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ - unsigned int p = rnd % (r4 * 2) + (lo + r4); +static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { + IdxT r4 = (up - lo) / 4; /* range/4 */ + IdxT p = rnd % (r4 * 2) + (lo + r4); lua_assert(lo + r4 <= p && p <= up - r4); return p; } @@ -352,11 +354,11 @@ static unsigned int choosePivot (unsigned int lo, unsigned int up, /* ** QuickSort algorithm (recursive function) */ -static void auxsort (lua_State *L, unsigned int lo, unsigned int up, +static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned int rnd) { while (lo < up) { /* loop for tail recursion */ - unsigned int p; /* Pivot index */ - unsigned int n; /* to be used later */ + IdxT p; /* Pivot index */ + IdxT n; /* to be used later */ /* sort elements 'lo', 'p', and 'up' */ lua_geti(L, 1, lo); lua_geti(L, 1, up); @@ -400,7 +402,7 @@ static void auxsort (lua_State *L, unsigned int lo, unsigned int up, n = up - p; /* size of smaller interval */ up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ } - if ((up - lo) / 128u > n) /* partition too imbalanced? */ + if ((up - lo) / 128 > n) /* partition too imbalanced? */ rnd = l_randomizePivot(); /* try a new randomization */ } /* tail call auxsort(L, lo, up, rnd) */ } @@ -410,11 +412,10 @@ static int sort (lua_State *L) { lua_Integer n = aux_getn(L, 1, TAB_RW); if (n > 1) { /* non-trivial interval? */ luaL_argcheck(L, n < INT_MAX, 1, "array too big"); - luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ lua_settop(L, 2); /* make sure there are two arguments */ - auxsort(L, 1, (unsigned int)n, 0u); + auxsort(L, 1, (IdxT)n, 0); } return 0; } diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 22b4df39..4650cc29 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.36 2015/11/03 15:47:30 roberto Exp $ +** $Id: ltm.c,v 2.37 2016/02/26 19:20:15 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -83,6 +83,22 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { } +/* +** Return the name of the type of an object. For tables and userdata +** with metatable, use their '__name' metafield, if present. +*/ +const char *luaT_objtypename (lua_State *L, const TValue *o) { + Table *mt; + if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || + (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { + const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); + if (ttisstring(name)) /* is '__name' a string? */ + return getstr(tsvalue(name)); /* use it as type name */ + } + return ttypename(ttnov(o)); /* else use standard type name */ +} + + void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, TValue *p3, int hasres) { ptrdiff_t result = savestack(L, p3); diff --git a/3rd/lua/ltm.h b/3rd/lua/ltm.h index 180179ce..63db7269 100644 --- a/3rd/lua/ltm.h +++ b/3rd/lua/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 roberto Exp $ +** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -51,11 +51,12 @@ typedef enum { #define fasttm(l,et,e) gfasttm(G(l), et, e) #define ttypename(x) luaT_typenames_[(x) + 1] -#define objtypename(x) ttypename(ttnov(x)) LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; +LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); + LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event); diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 75080b6c..e7416e16 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.329 2015/11/13 17:18:42 roberto Exp $ +** $Id: lua.h,v 1.330 2016/01/13 17:55:19 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -19,11 +19,11 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "3" #define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "2" +#define LUA_VERSION_RELEASE "3" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2016 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -467,7 +467,7 @@ LUA_API void (lua_checksig_)(lua_State *L); #define lua_checksig(L) if (skynet_sig_L) { lua_checksig_(L); } /****************************************************************************** -* Copyright (C) 1994-2015 Lua.org, PUC-Rio. +* Copyright (C) 1994-2016 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index b4ed5001..fd447ccb 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.254 2015/10/21 18:17:40 roberto Exp $ +** $Id: luaconf.h,v 1.255 2016/05/01 20:06:09 roberto Exp $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -612,7 +612,7 @@ ** provide its own implementation. */ #if !defined(LUA_USE_C89) -#define lua_number2strx(L,b,sz,f,n) l_sprintf(b,sz,f,n) +#define lua_number2strx(L,b,sz,f,n) ((void)L, l_sprintf(b,sz,f,n)) #endif diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index 9fc24af1..c5fd165b 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.265 2015/11/23 11:30:45 roberto Exp $ +** $Id: lvm.c,v 2.268 2016/02/05 19:59:14 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -163,54 +163,67 @@ static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, /* -** Complete a table access: if 't' is a table, 'tm' has its metamethod; -** otherwise, 'tm' is NULL. +** Finish the table access 'val = t[key]'. +** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to +** t[k] entry (which must be nil). */ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, - const TValue *tm) { + const TValue *slot) { int loop; /* counter to avoid infinite loops */ - lua_assert(tm != NULL || !ttistable(t)); + const TValue *tm; /* metamethod */ for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (tm == NULL) { /* no metamethod (from a table)? */ - if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) + if (slot == NULL) { /* 't' is not a table? */ + lua_assert(!ttistable(t)); + tm = luaT_gettmbyobj(L, t, TM_INDEX); + if (ttisnil(tm)) luaG_typeerror(L, t, "index"); /* no metamethod */ + /* else will try the metamethod */ } - if (ttisfunction(tm)) { /* metamethod is a function */ + else { /* 't' is a table */ + lua_assert(ttisnil(slot)); + tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ + if (tm == NULL) { /* no metamethod? */ + setnilvalue(val); /* result is nil */ + return; + } + /* else will try the metamethod */ + } + if (ttisfunction(tm)) { /* is metamethod a function? */ luaT_callTM(L, tm, t, key, val, 1); /* call it */ return; } - t = tm; /* else repeat access over 'tm' */ - if (luaV_fastget(L,t,key,tm,luaH_get)) { /* try fast track */ - setobj2s(L, val, tm); /* done */ + t = tm; /* else try to access 'tm[key]' */ + if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */ + setobj2s(L, val, slot); /* done */ return; } - /* else repeat */ + /* else repeat (tail call 'luaV_finishget') */ } - luaG_runerror(L, "gettable chain too long; possible loop"); + luaG_runerror(L, "'__index' chain too long; possible loop"); } /* -** Main function for table assignment (invoking metamethods if needed). -** Compute 't[key] = val' +** Finish a table assignment 't[key] = val'. +** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points +** to the entry 't[key]', or to 'luaO_nilobject' if there is no such +** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset' +** would have done the job.) */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *oldval) { + StkId val, const TValue *slot) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (oldval != NULL) { - Table *h = hvalue(t); - lua_assert(ttisnil(oldval)); - /* must check the metamethod */ - if ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && - /* no metamethod; is there a previous entry in the table? */ - (oldval != luaO_nilobject || - /* no previous entry; must create one. (The next test is - always true; we only need the assignment.) */ - (oldval = luaH_newkey(L, h, key), 1))) { + const TValue *tm; /* '__newindex' metamethod */ + if (slot != NULL) { /* is 't' a table? */ + Table *h = hvalue(t); /* save 't' table */ + lua_assert(ttisnil(slot)); /* old value must be nil */ + tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ + if (tm == NULL) { /* no metamethod? */ + if (slot == luaO_nilobject) /* no previous entry? */ + slot = luaH_newkey(L, h, key); /* create one */ /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, cast(TValue *, oldval), val); + setobj2t(L, cast(TValue *, slot), val); /* set its new value */ invalidateTMcache(h); luaC_barrierback(L, h, val); return; @@ -227,11 +240,11 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, return; } t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastset(L, t, key, oldval, luaH_get, val)) + if (luaV_fastset(L, t, key, slot, luaH_get, val)) return; /* done */ /* else loop */ } - luaG_runerror(L, "settable chain too long; possible loop"); + luaG_runerror(L, "'__newindex' chain too long; possible loop"); } @@ -749,18 +762,28 @@ void luaV_finishOp (lua_State *L) { luai_threadyield(L); } +/* fetch an instruction and prepare its execution */ +#define vmfetch() { \ + i = *(ci->u.l.savedpc++); \ + if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) \ + Protect(luaG_traceexec(L)); \ + ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ + lua_assert(base == ci->u.l.base); \ + lua_assert(base <= L->top && L->top < L->stack + L->stacksize); \ +} + #define vmdispatch(o) switch(o) #define vmcase(l) case l: #define vmbreak break /* -** copy of 'luaV_gettable', but protecting call to potential metamethod -** (which can reallocate the stack) +** copy of 'luaV_gettable', but protecting the call to potential +** metamethod (which can reallocate the stack) */ -#define gettableProtected(L,t,k,v) { const TValue *aux; \ - if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ - else Protect(luaV_finishget(L,t,k,v,aux)); } +#define gettableProtected(L,t,k,v) { const TValue *slot; \ + if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ + else Protect(luaV_finishget(L,t,k,v,slot)); } /* same for 'luaV_settable' */ @@ -783,14 +806,9 @@ void luaV_execute (lua_State *L) { base = ci->u.l.base; /* local copy of function's base */ /* main loop of interpreter */ for (;;) { - Instruction i = *(ci->u.l.savedpc++); + Instruction i; StkId ra; - if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) - Protect(luaG_traceexec(L)); - /* WARNING: several calls may realloc the stack and invalidate 'ra' */ - ra = RA(i); - lua_assert(base == ci->u.l.base); - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); + vmfetch(); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index fd0e748d..bcf52d20 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.39 2015/09/09 13:44:07 roberto Exp $ +** $Id: lvm.h,v 2.40 2016/01/05 16:07:21 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -49,25 +49,24 @@ /* -** fast track for 'gettable': 1 means 'aux' points to resulted value; -** 0 means 'aux' is metamethod (if 't' is a table) or NULL. 'f' is -** the raw get function to use. +** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, +** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, +** return 0 (meaning it will have to check metamethod) with 'slot' +** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). +** 'f' is the raw get function to use. */ -#define luaV_fastget(L,t,k,aux,f) \ +#define luaV_fastget(L,t,k,slot,f) \ (!ttistable(t) \ - ? (aux = NULL, 0) /* not a table; 'aux' is NULL and result is 0 */ \ - : (aux = f(hvalue(t), k), /* else, do raw access */ \ - !ttisnil(aux) ? 1 /* result not nil? 'aux' has it */ \ - : (aux = fasttm(L, hvalue(t)->metatable, TM_INDEX), /* get metamethod */\ - aux != NULL ? 0 /* has metamethod? must call it */ \ - : (aux = luaO_nilobject, 1)))) /* else, final result is nil */ + ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ + : (slot = f(hvalue(t), k), /* else, do raw access */ \ + !ttisnil(slot))) /* result not nil? */ /* ** standard implementation for 'gettable' */ -#define luaV_gettable(L,t,k,v) { const TValue *aux; \ - if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ - else luaV_finishget(L,t,k,v,aux); } +#define luaV_gettable(L,t,k,v) { const TValue *slot; \ + if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ + else luaV_finishget(L,t,k,v,slot); } /* @@ -100,9 +99,9 @@ LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *tm); + StkId val, const TValue *slot); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *oldval); + StkId val, const TValue *slot); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L); LUAI_FUNC void luaV_concat (lua_State *L, int total); From 56846b7dda3a553a9eb6795b96a2ee01ebb65b60 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 May 2016 09:47:01 +0800 Subject: [PATCH 599/729] new script to check deadloop service --- examples/checkdeadloop.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/checkdeadloop.lua diff --git a/examples/checkdeadloop.lua b/examples/checkdeadloop.lua new file mode 100644 index 00000000..46f395c1 --- /dev/null +++ b/examples/checkdeadloop.lua @@ -0,0 +1,30 @@ +local skynet = require "skynet" + +local list = {} + +local function timeout_check(ti) + if not next(list) then + return + end + skynet.sleep(ti) -- sleep 10 sec + for k,v in pairs(list) do + skynet.error("timout",ti,k,v) + end +end + +skynet.start(function() + skynet.error("ping all") + local list_ret = skynet.call(".launcher", "lua", "LIST") + for addr, desc in pairs(list_ret) do + list[addr] = desc + skynet.fork(function() + skynet.call(addr,"debug","INFO") + list[addr] = nil + end) + end + skynet.sleep(0) + timeout_check(100) + timeout_check(400) + timeout_check(500) + skynet.exit() +end) From c450b7b5d7cf25cce47bdd5b887b6d6f5352e702 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 May 2016 09:56:16 +0800 Subject: [PATCH 600/729] update jemaloc to 4.1.1 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index df900dbf..e02b83cc 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit df900dbfaf4835d3efc06d771535f3e781544913 +Subproject commit e02b83cc5e3c4d30f93dba945162e3aa58d962d6 From ed1a8313f419316eb54a14777ea0594b73760ce6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 May 2016 10:06:32 +0800 Subject: [PATCH 601/729] 1.0.0 rc3 --- HISTORY.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index b1f6ac0e..2bbb8b19 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,16 @@ +v1.0.0-rc3 (2016-5-9) +----------- +* Update jemalloc 4.1.1 +* Update lua 5.3.3 rc1 +* Update sproto to support encoding empty table +* Make skynet.init stable (keep order) +* skynet.getenv can return empty string +* Add lua VM memory warning +* lua VM support memory limit +* skynet.pcall suport varargs +* Bugfix : Global name query +* Bugfix : snax.queryglobal + v1.0.0-rc2 (2016-3-7) ----------- * Fix a bug in lua 5.3.2 From 4edb586077e75b90a87efe52a01a67ae5c179311 Mon Sep 17 00:00:00 2001 From: chenkete Date: Mon, 9 May 2016 17:16:37 +0800 Subject: [PATCH 602/729] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dredis=20auth=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=B2=A1=E6=9C=89=E8=BF=9B=E8=A1=8Ccompose=5Fmessage?= =?UTF-8?q?=E7=9A=84bug,=E8=AF=A5bug=E5=AF=BC=E8=87=B4skynet=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5twemproxy=E6=97=B6,auth=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/redis.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lualib/redis.lua b/lualib/redis.lua index 09147817..750505df 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -63,6 +63,7 @@ redcmd[42] = function(fd, data) -- '*' end ------------------- +local compose_message local function redis_login(auth, db) if auth == nil and db == nil then @@ -70,10 +71,10 @@ local function redis_login(auth, db) end return function(so) if auth then - so:request("AUTH "..auth.."\r\n", read_response) + so:request(compose_message("AUTH", auth), read_response) end if db then - so:request("SELECT "..db.."\r\n", read_response) + so:request(compose_message("SELECT", db), read_response) end end end @@ -122,7 +123,7 @@ local count_cache = make_cache(function(t,k) return s end) -local function compose_message(cmd, msg) +function compose_message(cmd, msg) local t = type(msg) local lines = {} @@ -233,7 +234,7 @@ local watchmeta = { local function watch_login(obj, auth) return function(so) if auth then - so:request("AUTH "..auth.."\r\n", read_response) + so:request(compose_message("AUTH", auth), read_response) end for k in pairs(obj.__psubscribe) do so:request(compose_message ("PSUBSCRIBE", k)) From 78df0b816144876e0b4dab64d22bb089279dc7ad Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 9 May 2016 19:16:29 +0800 Subject: [PATCH 603/729] refine code --- lualib/redis.lua | 55 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/lualib/redis.lua b/lualib/redis.lua index 750505df..949023a2 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -63,33 +63,6 @@ redcmd[42] = function(fd, data) -- '*' end ------------------- -local compose_message - -local function redis_login(auth, db) - if auth == nil and db == nil then - return - end - return function(so) - if auth then - so:request(compose_message("AUTH", auth), read_response) - end - if db then - so:request(compose_message("SELECT", db), read_response) - end - end -end - -function redis.connect(db_conf) - local channel = socketchannel.channel { - host = db_conf.host, - port = db_conf.port or 6379, - auth = redis_login(db_conf.auth, db_conf.db), - nodelay = true, - } - -- try connect first only once - channel:connect(true) - return setmetatable( { channel }, meta ) -end function command:disconnect() self[1]:close() @@ -123,7 +96,7 @@ local count_cache = make_cache(function(t,k) return s end) -function compose_message(cmd, msg) +local function compose_message(cmd, msg) local t = type(msg) local lines = {} @@ -150,6 +123,32 @@ function compose_message(cmd, msg) return lines end +local function redis_login(auth, db) + if auth == nil and db == nil then + return + end + return function(so) + if auth then + so:request(compose_message("AUTH", auth), read_response) + end + if db then + so:request(compose_message("SELECT", db), read_response) + end + end +end + +function redis.connect(db_conf) + local channel = socketchannel.channel { + host = db_conf.host, + port = db_conf.port or 6379, + auth = redis_login(db_conf.auth, db_conf.db), + nodelay = true, + } + -- try connect first only once + channel:connect(true) + return setmetatable( { channel }, meta ) +end + setmetatable(command, { __index = function(t,k) local cmd = string.upper(k) local f = function (self, v, ...) From e9f397945dfe62093e881333b860e92384e6de96 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 10 May 2016 16:59:34 +0800 Subject: [PATCH 604/729] bugfix issue #494 --- 3rd/lua/lapi.c | 10 +++++----- test/testmemlimit.lua | 26 ++++++++++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index c8ee2df4..fdffdbe1 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1012,10 +1012,9 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, return status; } -static Proto * cloneproto (lua_State *L, const Proto *src) { +void cloneproto (lua_State *L, Proto *f, const Proto *src) { /* copy constants and nested proto */ int i,n; - Proto *f = luaF_newproto(L, src->sp); n = src->sp->sizek; f->k=luaM_newvector(L,n,TValue); for (i=0; ik[i]); @@ -1033,9 +1032,9 @@ static Proto * cloneproto (lua_State *L, const Proto *src) { f->p=luaM_newvector(L,n,struct Proto *); for (i=0; ip[i]=NULL; for (i=0; ip[i]=cloneproto(L, src->p[i]); + f->p[i]= luaF_newproto(L, src->p[i]->sp); + cloneproto(L, f->p[i], src->p[i]); } - return f; } LUA_API void lua_clonefunction (lua_State *L, const void * fp) { @@ -1049,9 +1048,10 @@ LUA_API void lua_clonefunction (lua_State *L, const void * fp) { return; } cl = luaF_newLclosure(L,f->nupvalues); - cl->p = cloneproto(L, f->p); setclLvalue(L,L->top,cl); api_incr_top(L); + cl->p = luaF_newproto(L, f->p->sp); + cloneproto(L, cl->p, f->p); luaF_initupvals(L, cl); if (cl->nupvalues >= 1) { /* does it have an upvalue? */ diff --git a/test/testmemlimit.lua b/test/testmemlimit.lua index 41072dcc..66d972d1 100644 --- a/test/testmemlimit.lua +++ b/test/testmemlimit.lua @@ -1,17 +1,23 @@ local skynet = require "skynet" +local names = {"cluster", "dns", "mongo", "mysql", "redis", "sharedata", "socket", "sproto"} + -- set sandbox memory limit to 1M, must set here (at start, out of skynet.start) skynet.memlimit(1 * 1024 * 1024) skynet.start(function() - local a = {} - local limit - local ok, err = pcall(function() - for i=1, 1000000 do - limit = i - table.insert(a, {}) - end - end) - skynet.error(limit, err) - skynet.exit() + local a = {} + local limit + local ok, err = pcall(function() + for i=1, 12355 do + limit = i + table.insert(a, {}) + end + end) + local libs = {} + for k,v in ipairs(names) do + libs[v] = require(v) + end + skynet.error(limit, err) + skynet.exit() end) From 5be836527c5fa513c5199abd7276879c55f7e52a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 11 May 2016 22:02:10 +0800 Subject: [PATCH 605/729] bugfix issue #494 --- 3rd/lua/lgc.c | 14 +++++++++----- test/testmemlimit.lua | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index fd33b166..44accb59 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -492,15 +492,19 @@ static int marksharedproto (global_State *g, SharedProto *f) { ** NULL, so the use of 'markobjectN') */ static int traverseproto (global_State *g, Proto *f) { - int i; + int i,nk,np; if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */ - for (i = 0; i < f->sp->sizek; i++) /* mark literals */ + if (f->sp == NULL) + return sizeof(Proto); + nk = (f->k == NULL) ? 0 : f->sp->sizek; + np = (f->p == NULL) ? 0 : f->sp->sizep; + for (i = 0; i < nk; i++) /* mark literals */ markvalue(g, &f->k[i]); - for (i = 0; i < f->sp->sizep; i++) /* mark nested protos */ + for (i = 0; i < np; i++) /* mark nested protos */ markobjectN(g, f->p[i]); - return sizeof(Proto) + sizeof(Proto *) * f->sp->sizep + - sizeof(TValue) * f->sp->sizek + + return sizeof(Proto) + sizeof(Proto *) * np + + sizeof(TValue) * nk + marksharedproto(g, f->sp); } diff --git a/test/testmemlimit.lua b/test/testmemlimit.lua index 66d972d1..5c0c10d5 100644 --- a/test/testmemlimit.lua +++ b/test/testmemlimit.lua @@ -16,7 +16,10 @@ skynet.start(function() end) local libs = {} for k,v in ipairs(names) do - libs[v] = require(v) + local ok, m = pcall(require, v) + if ok then + libs[v] = m + end end skynet.error(limit, err) skynet.exit() From 1cf976c4e4e8367728748bcd51991691d2f9f28e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 13 May 2016 09:46:44 +0800 Subject: [PATCH 606/729] add debug console command : ping --- lualib/skynet/debug.lua | 8 ++++++++ service/debug_console.lua | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 6e886f02..6f6186ee 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -69,6 +69,14 @@ function dbgcmd.SUPPORT(pname) return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil)) end +function dbgcmd.PING() + return skynet.ret() +end + +function dbgcmd.LINK() + -- no return, raise error when exit +end + return dbgcmd end -- function init_dbgcmd diff --git a/service/debug_console.lua b/service/debug_console.lua index 92d99403..7b68a866 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -115,7 +115,7 @@ function COMMAND.help() help = "This help message", list = "List all the service", stat = "Dump all stats", - info = "Info address : get service infomation", + info = "info address : get service infomation", exit = "exit address : kill a lua service", kill = "kill address : kill service", mem = "mem : show memory status", @@ -133,6 +133,7 @@ function COMMAND.help() signal = "signal address sig", cmem = "Show C memory info", shrtbl = "Show shared short string table info", + ping = "ping address", } end @@ -283,3 +284,11 @@ function COMMAND.shrtbl() local n, total, longest, space = memory.ssinfo() return { n = n, total = total, longest = longest, space = space } end + +function COMMAND.ping(fd, address) + address = adjust_address(address) + local ti = skynet.now() + skynet.call(address, "debug", "PING") + ti = skynet.now() - ti + return tostring(ti) +end From 58755a677247f919c1d72a5c715bb478889df07a Mon Sep 17 00:00:00 2001 From: David Feng Date: Mon, 16 May 2016 18:57:40 +0800 Subject: [PATCH 607/729] add vararg for INFO debug console command --- lualib/skynet/debug.lua | 4 ++-- service/debug_console.lua | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 6f6186ee..3d6ff33c 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -37,9 +37,9 @@ function dbgcmd.TASK() skynet.ret(skynet.pack(task)) end -function dbgcmd.INFO() +function dbgcmd.INFO(...) if internal_info_func then - skynet.ret(skynet.pack(internal_info_func())) + skynet.ret(skynet.pack(internal_info_func(...))) else skynet.ret(skynet.pack(nil)) end diff --git a/service/debug_console.lua b/service/debug_console.lua index 7b68a866..9e121944 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -228,9 +228,9 @@ function COMMAND.task(fd, address) return skynet.call(address,"debug","TASK") end -function COMMAND.info(fd, address) +function COMMAND.info(fd, address, ...) address = adjust_address(address) - return skynet.call(address,"debug","INFO") + return skynet.call(address,"debug","INFO", ...) end function COMMAND.debug(fd, address) From 3b5f45eea2f01d3d5f85c7949c59a7d18b6ec533 Mon Sep 17 00:00:00 2001 From: LiuYang Date: Thu, 19 May 2016 13:10:33 +0800 Subject: [PATCH 608/729] =?UTF-8?q?lua=5Fbson=E7=9A=84encode=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=A2=9E=E5=8A=A0=E5=AF=B9=E6=9C=89=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=5F=5Fpairs=E7=9A=84table=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib-src/lua-bson.c | 110 +++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 34 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 7319a0dd..2024ed69 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -409,7 +409,44 @@ bson_numstr( char *str, unsigned int i ) { } static void -pack_dict(lua_State *L, struct bson *b, bool isarray, int depth) { +pack_dict_data(lua_State *L, struct bson *b, bool isarray, int depth, int kt) { + char numberkey[32]; + const char * key = NULL; + size_t sz; + if (isarray) { + if (kt != LUA_TNUMBER) { + luaL_error(L, "Invalid array key type : %s", lua_typename(L, kt)); + return; + } + sz = bson_numstr(numberkey, (unsigned int)lua_tointeger(L,-2)-1); + key = numberkey; + + append_one(b, L, key, sz, depth); + lua_pop(L,1); + } else { + switch(kt) { + case LUA_TNUMBER: + // copy key, don't change key type + lua_pushvalue(L,-2); + lua_insert(L,-2); + key = lua_tolstring(L,-2,&sz); + append_one(b, L, key, sz, depth); + lua_pop(L,2); + break; + case LUA_TSTRING: + key = lua_tolstring(L,-2,&sz); + append_one(b, L, key, sz, depth); + lua_pop(L,1); + break; + default: + luaL_error(L, "Invalid key type : %s", lua_typename(L, kt)); + return; + } + } +} + +static void +pack_simple_dict(lua_State *L, struct bson *b, bool isarray, int depth) { if (depth > MAX_DEPTH) { luaL_error(L, "Too depth while encoding bson"); } @@ -418,44 +455,49 @@ pack_dict(lua_State *L, struct bson *b, bool isarray, int depth) { lua_pushnil(L); while(lua_next(L,-2) != 0) { int kt = lua_type(L, -2); - char numberkey[32]; - const char * key = NULL; - size_t sz; - if (isarray) { - if (kt != LUA_TNUMBER) { - luaL_error(L, "Invalid array key type : %s", lua_typename(L, kt)); - return; - } - sz = bson_numstr(numberkey, (unsigned int)lua_tointeger(L,-2)-1); - key = numberkey; - - append_one(b, L, key, sz, depth); - lua_pop(L,1); - } else { - switch(kt) { - case LUA_TNUMBER: - // copy key, don't change key type - lua_pushvalue(L,-2); - lua_insert(L,-2); - key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz, depth); - lua_pop(L,2); - break; - case LUA_TSTRING: - key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz, depth); - lua_pop(L,1); - break; - default: - luaL_error(L, "Invalid key type : %s", lua_typename(L, kt)); - return; - } - } + pack_dict_data(L, b, isarray, depth, kt); } write_byte(b,0); write_length(b, b->size - length, length); } + +static void +pack_meta_dict(lua_State *L, struct bson *b, bool isarray, int depth) { + if (depth > MAX_DEPTH) { + luaL_error(L, "Too depth while encoding bson"); + } + luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table + int length = reserve_length(b); + + lua_pushvalue(L, -2); // push meta_obj + lua_call(L, 1, 3); // call __pairs_func => next_func, t_data, first_k + for(;;) { + lua_pushvalue(L, -2); // copy data + lua_pushvalue(L, -2); // copy k + lua_copy(L, -5, -3); // copy next_func replace old_k + lua_call(L, 2, 2); // call next_func + + int kt = lua_type(L, -2); + if (kt == LUA_TNIL) { + lua_pop(L, 4); // pop all k, v, next_func, obj + break; + } + pack_dict_data(L, b, isarray, depth, kt); + } + write_byte(b,0); + write_length(b, b->size - length, length); +} + +static void +pack_dict(lua_State *L, struct bson *b, bool isarray, int depth) { + if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { + pack_meta_dict(L, b, isarray, depth); + } else { + pack_simple_dict(L, b, isarray, depth); + } +} + static void pack_ordered_dict(lua_State *L, struct bson *b, int n, int depth) { int length = reserve_length(b); From de176b735f81c2204d591c57c6d4378d72549ddf Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 May 2016 13:16:19 +0800 Subject: [PATCH 609/729] update to lua 5.3.3 rc2 --- 3rd/lua/lcode.c | 4 ++-- 3rd/lua/lparser.c | 20 ++++++++++---------- 3rd/lua/lstrlib.c | 18 ++++++++---------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index e0940bbb..0cd5600e 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.108 2016/01/05 16:22:37 roberto Exp $ +** $Id: lcode.c,v 2.109 2016/05/13 19:09:21 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1066,7 +1066,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { case OPR_MINUS: case OPR_BNOT: if (constfolding(fs, op + LUA_OPUNM, e, &ef)) break; - /* else go through */ + /* FALLTHROUGH */ case OPR_LEN: codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); break; diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index bdc5f3ed..99fdc9de 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.152 2016/03/07 19:25:39 roberto Exp $ +** $Id: lparser.c,v 2.153 2016/05/13 19:10:16 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -269,27 +269,26 @@ static void markupval (FuncState *fs, int level) { Find variable with given name 'n'. If it is an upvalue, add this upvalue into all intermediate functions. */ -static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { +static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { if (fs == NULL) /* no more levels? */ - return VVOID; /* default is global */ + init_exp(var, VVOID, 0); /* default is global */ else { int v = searchvar(fs, n); /* look up locals at current level */ if (v >= 0) { /* found? */ init_exp(var, VLOCAL, v); /* variable is local */ if (!base) markupval(fs, v); /* local will be used as an upval */ - return VLOCAL; } else { /* not found as local at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ if (idx < 0) { /* not found? */ - if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */ - return VVOID; /* not found; is a global */ + singlevaraux(fs->prev, n, var, 0); /* try upper levels */ + if (var->k == VVOID) /* not found? */ + return; /* it is a global */ /* else was LOCAL or UPVAL */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ } - init_exp(var, VUPVAL, idx); - return VUPVAL; + init_exp(var, VUPVAL, idx); /* new or old upvalue */ } } } @@ -298,10 +297,11 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { static void singlevar (LexState *ls, expdesc *var) { TString *varname = str_checkname(ls); FuncState *fs = ls->fs; - if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */ + singlevaraux(fs, varname, var, 1); + if (var->k == VVOID) { /* global name? */ expdesc key; singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - lua_assert(var->k == VLOCAL || var->k == VUPVAL); + lua_assert(var->k != VVOID); /* this one must exist */ codestring(ls, &key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ } diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index a610a0d7..99ce452a 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.248 2016/05/02 13:58:01 roberto Exp $ +** $Id: lstrlib.c,v 1.250 2016/05/18 18:19:51 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -958,8 +958,8 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { addliteralnum(L, b, lua_tonumber(L, arg)); break; } - /* else integers; write in "native" format *//* FALLTHROUGH */ - } + /* else integers; write in "native" format */ + } /* FALLTHROUGH */ case LUA_TNIL: case LUA_TBOOLEAN: { luaL_tolstring(L, arg, NULL); luaL_addvalue(b); @@ -1369,13 +1369,11 @@ static int str_pack (lua_State *L) { case Kchar: { /* fixed-size string */ size_t len; const char *s = luaL_checklstring(L, arg, &len); - if ((size_t)size <= len) /* string larger than (or equal to) needed? */ - luaL_addlstring(&b, s, size); /* truncate string to asked size */ - else { /* string smaller than needed */ - luaL_addlstring(&b, s, len); /* add it all */ - while (len++ < (size_t)size) /* pad extra space */ - luaL_addchar(&b, LUAL_PACKPADBYTE); - } + luaL_argcheck(L, len <= (size_t)size, arg, + "string longer than given size"); + luaL_addlstring(&b, s, len); /* add string */ + while (len++ < (size_t)size) /* pad extra space */ + luaL_addchar(&b, LUAL_PACKPADBYTE); break; } case Kstring: { /* strings with length count */ From e00ad7ab79dc6d1bf65016aa7467ddb450395fdc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 31 May 2016 09:30:21 +0800 Subject: [PATCH 610/729] update to lua 5.3.3 rc3 --- 3rd/lua/lobject.c | 10 ++++++++-- 3rd/lua/lstrlib.c | 33 +++++++++++++++++++-------------- 3rd/lua/lua.h | 4 ++-- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index 04f45da2..a44b3850 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.110 2016/05/02 14:00:32 roberto Exp $ +** $Id: lobject.c,v 2.111 2016/05/20 14:07:48 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -293,6 +293,9 @@ static const char *l_str2d (const char *s, lua_Number *result) { } +#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) +#define MAXLASTD cast_int(LUA_MAXINTEGER % 10) + static const char *l_str2int (const char *s, lua_Integer *result) { lua_Unsigned a = 0; int empty = 1; @@ -309,7 +312,10 @@ static const char *l_str2int (const char *s, lua_Integer *result) { } else { /* decimal */ for (; lisdigit(cast_uchar(*s)); s++) { - a = a * 10 + *s - '0'; + int d = *s - '0'; + if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ + return NULL; /* do not accept it (as integer) */ + a = a * 10 + d; empty = 0; } } diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 99ce452a..12264f88 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.250 2016/05/18 18:19:51 roberto Exp $ +** $Id: lstrlib.c,v 1.251 2016/05/20 14:13:21 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -928,20 +928,14 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { /* -** Convert a Lua number to a string in floating-point hexadecimal -** format. Ensures the resulting string uses a dot as the radix -** character. +** Ensures the 'buff' string uses a dot as the radix character. */ -static void addliteralnum (lua_State *L, luaL_Buffer *b, lua_Number n) { - char *buff = luaL_prepbuffsize(b, MAX_ITEM); - int nb = lua_number2strx(L, buff, MAX_ITEM, - "%" LUA_NUMBER_FRMLEN "a", n); +static void checkdp (char *buff, int nb) { if (memchr(buff, '.', nb) == NULL) { /* no dot? */ char point = lua_getlocaledecpoint(); /* try locale point */ char *ppoint = memchr(buff, point, nb); if (ppoint) *ppoint = '.'; /* change it to a dot */ } - luaL_addsize(b, nb); } @@ -954,12 +948,23 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { break; } case LUA_TNUMBER: { - if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */ - addliteralnum(L, b, lua_tonumber(L, arg)); - break; + char *buff = luaL_prepbuffsize(b, MAX_ITEM); + int nb; + if (!lua_isinteger(L, arg)) { /* float? */ + lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */ + nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); + checkdp(buff, nb); /* ensure it uses a dot */ } - /* else integers; write in "native" format */ - } /* FALLTHROUGH */ + else { /* integers */ + lua_Integer n = lua_tointeger(L, arg); + const char *format = (n == LUA_MININTEGER) /* corner case? */ + ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */ + : LUA_INTEGER_FMT; /* else use default format */ + nb = l_sprintf(buff, MAX_ITEM, format, n); + } + luaL_addsize(b, nb); + break; + } case LUA_TNIL: case LUA_TBOOLEAN: { luaL_tolstring(L, arg, NULL); luaL_addvalue(b); diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index e7416e16..2707cb31 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.330 2016/01/13 17:55:19 roberto Exp $ +** $Id: lua.h,v 1.331 2016/05/30 15:53:28 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -363,7 +363,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); #define lua_pushliteral(L, s) lua_pushstring(L, "" s) #define lua_pushglobaltable(L) \ - lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) + ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) From ba264700dc2131b941ed05b317b9c2bc44a77152 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 31 May 2016 09:36:21 +0800 Subject: [PATCH 611/729] update jemalloc to 4.2.0 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index e02b83cc..f70a254d 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit e02b83cc5e3c4d30f93dba945162e3aa58d962d6 +Subproject commit f70a254d44c8d30af2cd5d30531fb18fdabaae6d From 838e63ab62831c4ce04e18534f90a0066ad443b8 Mon Sep 17 00:00:00 2001 From: rickone Date: Sat, 4 Jun 2016 16:26:07 +0800 Subject: [PATCH 612/729] Update socketchannel.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit request的时候å¯èƒ½ä¼šè¿žæŽ¥å¤±è´¥ï¼Œè¿™æ—¶raiseçš„ä¸æ˜¯socket_error,上层处ç†ä¸å¤ªç»Ÿä¸€ã€‚ --- lualib/socketchannel.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 350423a4..fa72b891 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -328,7 +328,8 @@ local function block_connect(self, once) r = check_connection(self) if r == nil then - error(string.format("Connect to %s:%d failed (%s)", self.__host, self.__port, err)) + skynet.error(string.format("Connect to %s:%d failed (%s)", self.__host, self.__port, err)) + error(socket_error) else return r end From b8bb327f98e6d86ddffc5685b6a985f66bec8648 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 6 Jun 2016 16:21:59 +0800 Subject: [PATCH 613/729] make coroutine_pool weak would be better --- lualib/skynet.lua | 7 +------ lualib/skynet/debug.lua | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 08a3fa13..abb568c8 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -95,7 +95,7 @@ end -- coroutine reuse -local coroutine_pool = {} +local coroutine_pool = setmetatable({}, { __mode = "kv" }) local function co_create(f) local co = table.remove(coroutine_pool) @@ -661,15 +661,10 @@ function skynet.memlimit(bytes) skynet.memlimit = nil -- set only once end -local function clear_pool() - coroutine_pool = {} -end - -- Inject internal debug framework local debug = require "skynet.debug" debug(skynet, { dispatch = skynet.dispatch_message, - clear = clear_pool, suspend = suspend, }) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 3d6ff33c..957a2588 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -20,7 +20,6 @@ function dbgcmd.MEM() end function dbgcmd.GC() - export.clear() collectgarbage "collect" end From 0edb839c95f19734ff09ffce8fc2b4d0d591016f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 7 Jun 2016 09:43:43 +0800 Subject: [PATCH 614/729] Lua 5.3.3 released --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bb9e3820..1a65b43a 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,13 @@ Run these in different consoles: ./3rd/lua/lua examples/client.lua # Launch a client, and try to input hello. ``` -## About Lua +## About Lua version -Skynet now uses a modified version of lua 5.3.2 ( http://www.lua.org/ftp/lua-5.3.2.tar.gz ) . +Skynet now uses a modified version of lua 5.3.3 ( http://www.lua.org/ftp/lua-5.3.3.tar.gz ) . For details: http://lua-users.org/lists/lua-l/2014-03/msg00489.html -You can also use other official Lua versions, just edit the Makefile by yourself. +You can also use official Lua versions, just edit the Makefile by yourself. ## How To Use (Sorry, Only in Chinese now) From ea9bc5326117d80855bd083e77130e3263b6ed5d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 12 Jun 2016 13:40:23 +0800 Subject: [PATCH 615/729] reopen log file when recv signal HUP --- service-src/service_logger.c | 28 ++++++++++++++++++++++------ skynet-src/skynet_start.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/service-src/service_logger.c b/service-src/service_logger.c index 122cc77f..e73300e3 100644 --- a/service-src/service_logger.c +++ b/service-src/service_logger.c @@ -3,9 +3,11 @@ #include #include #include +#include struct logger { FILE * handle; + char * filename; int close; }; @@ -14,6 +16,8 @@ logger_create(void) { struct logger * inst = skynet_malloc(sizeof(*inst)); inst->handle = NULL; inst->close = 0; + inst->filename = NULL; + return inst; } @@ -22,16 +26,26 @@ logger_release(struct logger * inst) { if (inst->close) { fclose(inst->handle); } + skynet_free(inst->filename); skynet_free(inst); } static int -_logger(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) { +logger_cb(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct logger * inst = ud; - fprintf(inst->handle, "[:%08x] ",source); - fwrite(msg, sz , 1, inst->handle); - fprintf(inst->handle, "\n"); - fflush(inst->handle); + switch (type) { + case PTYPE_SYSTEM: + if (inst->filename) { + inst->handle = freopen(inst->filename, "a", inst->handle); + } + break; + case PTYPE_TEXT: + fprintf(inst->handle, "[:%08x] ",source); + fwrite(msg, sz , 1, inst->handle); + fprintf(inst->handle, "\n"); + fflush(inst->handle); + break; + } return 0; } @@ -43,12 +57,14 @@ logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) if (inst->handle == NULL) { return 1; } + inst->filename = skynet_malloc(strlen(parm)+1); + strcpy(inst->filename, parm); inst->close = 1; } else { inst->handle = stdout; } if (inst->handle) { - skynet_callback(ctx, inst, _logger); + skynet_callback(ctx, inst, logger_cb); skynet_command(ctx, "REG", ".logger"); return 0; } diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 5345c76c..a7b1d70b 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -16,6 +16,7 @@ #include #include #include +#include struct monitor { int count; @@ -32,6 +33,15 @@ struct worker_parm { int weight; }; +static int SIG = 0; + +static void +handle_hup(int signal) { + if (signal == SIGHUP) { + SIG = 1; + } +} + #define CHECK_ABORT if (skynet_context_total()==0) break; static void @@ -100,6 +110,21 @@ thread_monitor(void *p) { return NULL; } +static void +signal_hup() { + // make log file reopen + + struct skynet_message smsg; + smsg.source = 0; + smsg.session = 0; + smsg.data = NULL; + smsg.sz = (size_t)PTYPE_SYSTEM << MESSAGE_TYPE_SHIFT; + uint32_t logger = skynet_handle_findname("logger"); + if (logger) { + skynet_context_push(logger, &smsg); + } +} + static void * thread_timer(void *p) { struct monitor * m = p; @@ -109,6 +134,10 @@ thread_timer(void *p) { CHECK_ABORT wakeup(m,m->count-1); usleep(2500); + if (SIG) { + signal_hup(); + SIG = 0; + } } // wakeup socket thread skynet_socket_exit(); @@ -216,6 +245,13 @@ bootstrap(struct skynet_context * logger, const char * cmdline) { void skynet_start(struct skynet_config * config) { + // register SIGHUP for log file reopen + struct sigaction sa; + sa.sa_handler = &handle_hup; + sa.sa_flags = SA_RESTART; + sigfillset(&sa.sa_mask); + sigaction(SIGHUP, &sa, NULL); + if (config->daemon) { if (daemon_init(config->daemon)) { exit(1); From f84fe84b964a2d947b5d7b6c0cb6d811d3a6c85a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 12 Jun 2016 13:48:53 +0800 Subject: [PATCH 616/729] update userlog --- examples/userlog.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/userlog.lua b/examples/userlog.lua index 55aa0952..3a8c5d22 100644 --- a/examples/userlog.lua +++ b/examples/userlog.lua @@ -6,7 +6,17 @@ skynet.register_protocol { id = skynet.PTYPE_TEXT, unpack = skynet.tostring, dispatch = function(_, address, msg) - print(string.format("%x(%.2f): %s", address, skynet.time(), msg)) + print(string.format(":%08x(%.2f): %s", address, skynet.time(), msg)) + end +} + +skynet.register_protocol { + name = "SYSTEM", + id = skynet.PTYPE_SYSTEM, + unpack = function(...) return ... end, + dispatch = function() + -- reopen signal + print("SIGHUP") end } From 95465372569a8973a5d7d9a99d803d626a23a413 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 12 Jun 2016 14:51:18 +0800 Subject: [PATCH 617/729] debug console support simple http --- lualib/http/sockethelper.lua | 38 +++++++++++++++++++++++++++++++++++- service/debug_console.lua | 10 ++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index cac3130d..735da45d 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -8,7 +8,43 @@ local socket_error = setmetatable({} , { __tostring = function() return "[Socket sockethelper.socket_error = socket_error -function sockethelper.readfunc(fd) +local function preread(fd, str) + return function (sz) + if str then + if sz == #str or sz == nil then + local ret = str + str = nil + return ret + else + if sz < #str then + local ret = str:sub(1,sz) + str = str:sub(sz + 1) + return ret + else + sz = sz - #str + local ret = readbytes(fd, sz) + if ret then + return str .. ret + else + error(socket_error) + end + end + end + else + local ret = readbytes(fd, sz) + if ret then + return ret + else + error(socket_error) + end + end + end +end + +function sockethelper.readfunc(fd, pre) + if pre then + return preread(fd, pre) + end return function (sz) local ret = readbytes(fd, sz) if ret then diff --git a/service/debug_console.lua b/service/debug_console.lua index 9e121944..4d0edee2 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -4,6 +4,8 @@ local core = require "skynet.core" local socket = require "socket" local snax = require "snax" local memory = require "memory" +local httpd = require "http.httpd" +local sockethelper = require "http.sockethelper" local port = tonumber(...) local COMMAND = {} @@ -84,6 +86,14 @@ local function console_main_loop(stdin, print) if not cmdline then break end + if cmdline:sub(1,4) == "GET " then + -- http + local code, url = httpd.read_request(sockethelper.readfunc(stdin, cmdline.. "\n"), 8192) + local cmdline = url:sub(2):gsub("/"," ") + docmd(cmdline, print, stdin) + socket.close(stdin) + return + end if cmdline ~= "" then docmd(cmdline, print, stdin) end From d9b0347741003d0fa12d4603d0112ea0a5eb581a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 12 Jun 2016 15:07:56 +0800 Subject: [PATCH 618/729] don't close fd twice --- service/debug_console.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 4d0edee2..976e35b5 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -91,8 +91,7 @@ local function console_main_loop(stdin, print) local code, url = httpd.read_request(sockethelper.readfunc(stdin, cmdline.. "\n"), 8192) local cmdline = url:sub(2):gsub("/"," ") docmd(cmdline, print, stdin) - socket.close(stdin) - return + break end if cmdline ~= "" then docmd(cmdline, print, stdin) From bf5b74b6cafb55bd2b30b7e9a7cb1ecb9a6a758e Mon Sep 17 00:00:00 2001 From: dpull Date: Sun, 12 Jun 2016 16:29:47 +0800 Subject: [PATCH 619/729] =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=AE=BE=E8=AE=A1deb?= =?UTF-8?q?ug.lua=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/skynet.lua | 2 +- lualib/skynet/debug.lua | 183 +++++++++++++++++++++------------------- 2 files changed, 97 insertions(+), 88 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index abb568c8..16ad0c24 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -663,7 +663,7 @@ end -- Inject internal debug framework local debug = require "skynet.debug" -debug(skynet, { +debug.init(skynet, { dispatch = skynet.dispatch_message, suspend = suspend, }) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 957a2588..8fb484de 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -1,96 +1,105 @@ local table = table -local extern_dbgcmd = {}; +local extern_dbgcmd = {} -return function (skynet, export) +local function init(skynet, export) + local internal_info_func -local internal_info_func - -function skynet.info_func(func) - internal_info_func = func -end - -local dbgcmd - -local function init_dbgcmd() -dbgcmd = extern_dbgcmd - -function dbgcmd.MEM() - local kb, bytes = collectgarbage "count" - skynet.ret(skynet.pack(kb,bytes)) -end - -function dbgcmd.GC() - collectgarbage "collect" -end - -function dbgcmd.STAT() - local stat = {} - stat.mqlen = skynet.mqlen() - stat.task = skynet.task() - skynet.ret(skynet.pack(stat)) -end - -function dbgcmd.TASK() - local task = {} - skynet.task(task) - skynet.ret(skynet.pack(task)) -end - -function dbgcmd.INFO(...) - if internal_info_func then - skynet.ret(skynet.pack(internal_info_func(...))) - else - skynet.ret(skynet.pack(nil)) + function skynet.info_func(func) + internal_info_func = func end + + local dbgcmd + + local function init_dbgcmd() + dbgcmd = {} + + function dbgcmd.MEM() + local kb, bytes = collectgarbage "count" + skynet.ret(skynet.pack(kb,bytes)) + end + + function dbgcmd.GC() + + collectgarbage "collect" + end + + function dbgcmd.STAT() + local stat = {} + stat.mqlen = skynet.mqlen() + stat.task = skynet.task() + skynet.ret(skynet.pack(stat)) + end + + function dbgcmd.TASK() + local task = {} + skynet.task(task) + skynet.ret(skynet.pack(task)) + end + + function dbgcmd.INFO(...) + if internal_info_func then + skynet.ret(skynet.pack(internal_info_func(...))) + else + skynet.ret(skynet.pack(nil)) + end + end + + function dbgcmd.EXIT() + skynet.exit() + end + + function dbgcmd.RUN(source, filename) + local inject = require "skynet.inject" + local output = inject(skynet, source, filename , export.dispatch, skynet.register_protocol) + collectgarbage "collect" + skynet.ret(skynet.pack(table.concat(output, "\n"))) + end + + function dbgcmd.TERM(service) + skynet.term(service) + end + + function dbgcmd.REMOTEDEBUG(...) + local remotedebug = require "skynet.remotedebug" + remotedebug.start(export, ...) + end + + function dbgcmd.SUPPORT(pname) + return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil)) + end + + function dbgcmd.PING() + return skynet.ret() + end + + function dbgcmd.LINK() + -- no return, raise error when exit + end + + return dbgcmd + end -- function init_dbgcmd + + local function _debug_dispatch(session, address, cmd, ...) + dbgcmd = dbgcmd or init_dbgcmd() -- lazy init dbgcmd + local f = dbgcmd[cmd] or extern_dbgcmd[cmd] + assert(f, cmd) + f(...) + end + + skynet.register_protocol { + name = "debug", + id = assert(skynet.PTYPE_DEBUG), + pack = assert(skynet.pack), + unpack = assert(skynet.unpack), + dispatch = _debug_dispatch, + } end -function dbgcmd.EXIT() - skynet.exit() +local function reg_debugcmd(name, fn) + extern_dbgcmd[name] = fn end -function dbgcmd.RUN(source, filename) - local inject = require "skynet.inject" - local output = inject(skynet, source, filename , export.dispatch, skynet.register_protocol) - collectgarbage "collect" - skynet.ret(skynet.pack(table.concat(output, "\n"))) -end - -function dbgcmd.TERM(service) - skynet.term(service) -end - -function dbgcmd.REMOTEDEBUG(...) - local remotedebug = require "skynet.remotedebug" - remotedebug.start(export, ...) -end - -function dbgcmd.SUPPORT(pname) - return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil)) -end - -function dbgcmd.PING() - return skynet.ret() -end - -function dbgcmd.LINK() - -- no return, raise error when exit -end - -return dbgcmd -end -- function init_dbgcmd - -local function _debug_dispatch(session, address, cmd, ...) - local f = (dbgcmd or init_dbgcmd())[cmd] -- lazy init dbgcmd - assert(f, cmd) - f(...) -end - -skynet.register_protocol { - name = "debug", - id = assert(skynet.PTYPE_DEBUG), - pack = assert(skynet.pack), - unpack = assert(skynet.unpack), - dispatch = _debug_dispatch, +return { + init = init, + reg_debugcmd = reg_debugcmd, } - -end From 84ee3da79a5c5e18d58d83c5db841497b43576c1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 11:55:31 +0800 Subject: [PATCH 620/729] add mongo.createIndexs, and fix the bug in old API createIndex. For detail, see PR #511 --- lualib/mongo.lua | 67 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index a1a2e11e..e6ccc5e8 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -318,30 +318,63 @@ function mongo_cursor:count(with_limit_and_skip) end +-- For compatibility. -- collection:createIndex({username = 1}, {unique = true}) -function mongo_collection:createIndex(keys, option) - local name = option.name - option.name = nil - - if not name then - for k, v in pairs(keys) do - name = (name == nil) and k or (name .. "_" .. k) - name = name .. "_" .. v - end - end - - - local doc = {}; - doc.name = name - doc.key = keys - for k, v in pairs(option) do +local function createIndex_onekey(self, key, option) + local doc = {} + for k,v in pairs(option) do doc[k] = v end + local k,v = next(key) -- support only one key + doc.name = doc.name or (k .. "_" .. v) + doc.key = key + return self.database:runCommand("createIndexes", self.name, "indexes", {doc}) end -mongo_collection.ensureIndex = mongo_collection.createIndex; +local function IndexModel(option) + local doc = {} + for k,v in pairs(option) do + if type(k) == "string" then + doc[k] = v + end + end + + local keys = {} + local name + for _, kv in ipairs(option) do + local k,v = next(kv) + table.insert(keys, k) + table.insert(keys, v) + name = (name == nil) and k or (name .. "_" .. k) + name = name .. "_" .. v + end + + doc.name = doc.name or name + doc.key = bson_encode_order(table.unpack(keys)) + + return doc +end + +-- collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } +function mongo_collection:createIndex(option , onekey) + if onekey then + return createIndex_onekey(self, option, onekey) + end + + return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(option) }) +end + +function mongo_collection:createIndexs(...) + local idx = { ... } + for k,v in ipairs(idx) do + idx[k] = IndexModel(v) + end + return self.database:runCommand("createIndexes", self.name, "indexes", idx) +end + +mongo_collection.ensureIndex = mongo_collection.createIndex function mongo_collection:drop() return self.database:runCommand("drop", self.name) From ee1a21887015761b13cf2163d55711cd33384ab0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:01:54 +0800 Subject: [PATCH 621/729] typo --- lualib/mongo.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index e6ccc5e8..977d4c3a 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -366,7 +366,7 @@ function mongo_collection:createIndex(option , onekey) return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(option) }) end -function mongo_collection:createIndexs(...) +function mongo_collection:createIndexes(...) local idx = { ... } for k,v in ipairs(idx) do idx[k] = IndexModel(v) From bd3d9b70ec297c30985a1c22e17c51329ca16429 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:05:52 +0800 Subject: [PATCH 622/729] change arg name --- lualib/mongo.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 977d4c3a..c33c8d6c 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -358,12 +358,12 @@ local function IndexModel(option) end -- collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } -function mongo_collection:createIndex(option , onekey) - if onekey then - return createIndex_onekey(self, option, onekey) +function mongo_collection:createIndex(arg1 , arg2) + if arg2 then + return createIndex_onekey(self, arg1, arg2) + else + return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(arg1) }) end - - return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(option) }) end function mongo_collection:createIndexes(...) From fd2c814fe04c8b76a1c53bd7a718b39a475fd071 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:13:02 +0800 Subject: [PATCH 623/729] default key ascending --- lualib/mongo.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index c33c8d6c..4806bee0 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -344,7 +344,13 @@ local function IndexModel(option) local keys = {} local name for _, kv in ipairs(option) do - local k,v = next(kv) + local k,v + if type(kv) == "string" then + k = kv + v = 1 + else + k,v = next(kv) + end table.insert(keys, k) table.insert(keys, v) name = (name == nil) and k or (name .. "_" .. k) @@ -358,6 +364,8 @@ local function IndexModel(option) end -- collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } +-- or collection:createIndex { "key1", "key2", unique = true } +-- or collection:createIndex( { key1 = 1} , { unique = true } ) -- For compatibility function mongo_collection:createIndex(arg1 , arg2) if arg2 then return createIndex_onekey(self, arg1, arg2) From bb0143e476866d733d138311eb37743b9f0bd7cd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:28:13 +0800 Subject: [PATCH 624/729] add some assert --- lualib/mongo.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 4806bee0..895adb62 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -326,6 +326,7 @@ local function createIndex_onekey(self, key, option) doc[k] = v end local k,v = next(key) -- support only one key + assert(next(key,k) == nil, "Use new api for multi-keys") doc.name = doc.name or (k .. "_" .. v) doc.key = key @@ -356,6 +357,7 @@ local function IndexModel(option) name = (name == nil) and k or (name .. "_" .. k) name = name .. "_" .. v end + assert(name, "Need keys") doc.name = doc.name or name doc.key = bson_encode_order(table.unpack(keys)) From 2020ce3c9c1fa854593cb1dcef0df397e733f71b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 15:08:33 +0800 Subject: [PATCH 625/729] update jemalloc to 4.2.1 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index f70a254d..3de03533 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit f70a254d44c8d30af2cd5d30531fb18fdabaae6d +Subproject commit 3de035335255d553bdb344c32ffdb603816195d8 From 6a54fe8293a5c93dc48acb348b633dcdb3ccf382 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 15:13:17 +0800 Subject: [PATCH 626/729] 1.0.0 rc4 --- HISTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 2bbb8b19..3ed8a7c3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +v1.0.0-rc4 (2016-6-13) +----------- +* Update lua to 5.3.3 +* Update jemalloc to 4.2.1 +* Add debug console command ping +* Lua bson support __pairs +* Add mongo.createIndexes and fix bug in old mongo.createIndex +* Handle signal HUP to reopen log file (for logrotate) + v1.0.0-rc3 (2016-5-9) ----------- * Update jemalloc 4.1.1 From ffa2bbf1e4ee60e8fb99c4a28c4a4edec9918c95 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 15 Jun 2016 16:48:02 +0800 Subject: [PATCH 627/729] support mongo auth_scram_sha1 --- lualib-src/lua-crypt.c | 20 +++++++++++ lualib/mongo.lua | 81 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 5322b2d6..7910d367 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -878,6 +878,25 @@ lb64decode(lua_State *L) { return 1; } +static int +lxor_str(lua_State *L) { + size_t len1,len2; + const char *s1 = luaL_checklstring(L,1,&len1); + const char *s2 = luaL_checklstring(L,2,&len2); + if (len2 == 0) { + return luaL_error(L, "Can't xor empty string"); + } + luaL_Buffer b; + char * buffer = luaL_buffinitsize(L, &b, len1); + int i; + for (i=0;i Date: Wed, 15 Jun 2016 17:58:58 +0800 Subject: [PATCH 628/729] add authmod in conf --- lualib/mongo.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index a8acee9e..877982f4 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -84,11 +84,15 @@ end local function mongo_auth(mongoc) local user = rawget(mongoc, "username") local pass = rawget(mongoc, "password") + local authmod = rawget(mongoc, "authmod") or "scram_sha1" + authmod = "auth_" .. authmod return function() if user ~= nil and pass ~= nil then --- assert(mongoc:auth(user, pass)) - assert(mongoc:auth_scram_sha1(user, pass)) + -- autmod can be "mongodb_cr" or "scram_sha1" + local auth_func = mongoc[authmod] + assert(auth_func , "Invalid authmod") + assert(auth_func(mongoc,user, pass)) end local rs_data = mongoc:runCommand("ismaster") if rs_data.ok == 1 then @@ -124,6 +128,7 @@ function mongo.client( conf ) port = first.port or 27017, username = first.username, password = first.password, + authmod = first.authmod, } obj.__id = 0 @@ -174,7 +179,7 @@ function mongo_client:runCommand(...) return self.admin:runCommand(...) end -function mongo_client:auth(user,password) +function mongo_client:auth_mongodb_cr(user,password) local password = md5.sumhexa(string.format("%s:mongo:%s",user,password)) local result= self:runCommand "getnonce" if result.ok ~=1 then From 040089639f563d36b377a53ef812bc171ebcb7c2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 17 Jun 2016 12:17:53 +0800 Subject: [PATCH 629/729] memory leak bugfix in multicast, See issue #519 --- lualib-src/lua-multicast.c | 17 +++-------------- service/multicastd.lua | 6 +++--- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index cff8d434..841267a9 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -60,18 +60,6 @@ mc_packremote(lua_State *L) { return pack(L, msg, size); } -static int -mc_packstring(lua_State *L) { - size_t size; - const char * msg = luaL_checklstring(L, 1, &size); - if (size != (uint32_t)size) { - return luaL_error(L, "string is too long"); - } - void * data = skynet_malloc(size); - memcpy(data, msg, size); - return pack(L, data, size); -} - /* lightuserdata struct mc_package ** integer size (must be sizeof(struct mc_package *) @@ -82,7 +70,7 @@ static int mc_unpacklocal(lua_State *L) { struct mc_package ** pack = lua_touserdata(L,1); int sz = luaL_checkinteger(L,2); - if (sz != sizeof(*pack)) { + if (sz != sizeof(pack)) { return luaL_error(L, "Invalid multicast package size %d", sz); } lua_pushlightuserdata(L, *pack); @@ -108,6 +96,8 @@ mc_bindrefer(lua_State *L) { lua_pushlightuserdata(L, *pack); + skynet_free(pack); + return 1; } @@ -161,7 +151,6 @@ luaopen_multicast_core(lua_State *L) { { "bind", mc_bindrefer }, { "close", mc_closelocal }, { "remote", mc_remote }, - { "packstring", mc_packstring }, { "packremote", mc_packremote }, { "nextid", mc_nextid }, { NULL, NULL }, diff --git a/service/multicastd.lua b/service/multicastd.lua index 3010e2c9..7f8506af 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -78,13 +78,13 @@ local function publish(c , source, pack, size) local group = channel[c] if group == nil or next(group) == nil then - -- dead channel, delete the pack. mc.bind returns the pointer in pack + -- dead channel, delete the pack. mc.bind returns the pointer in pack and free the pack (struct mc_package **) local pack = mc.bind(pack, 1) mc.close(pack) return end - mc.bind(pack, channel_n[c]) - local msg = skynet.tostring(pack, size) + local msg = skynet.tostring(pack, size) -- copy (pack,size) to a string + mc.bind(pack, channel_n[c]) -- mc.bind will free the pack(struct mc_package **) for k in pairs(group) do -- the msg is a pointer to the real message, publish pointer in local is ok. skynet.redirect(k, source, "multicast", c , msg) From 0465688403703ee29ba54f67b1da4dfce6c13aad Mon Sep 17 00:00:00 2001 From: ahuang007 <419637330@qq.com> Date: Tue, 21 Jun 2016 15:25:08 +0800 Subject: [PATCH 630/729] Update lua-crypt.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这个地方应该用G --- lualib-src/lua-crypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 7910d367..6ddb246e 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -751,7 +751,7 @@ ldhexchange(lua_State *L) { if (x64 == 0) return luaL_error(L, "Can't be 0"); - uint64_t r = powmodp(5, x64); + uint64_t r = powmodp(G, x64); push64(L, r); return 1; } From 16af94795e28eb4d6a223b08891f9c76cba8383a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 22 Jun 2016 22:28:54 +0800 Subject: [PATCH 631/729] lua 5.3.3 bugfix 1 --- 3rd/lua/lparser.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 99fdc9de..9c9fd5cc 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -325,6 +325,8 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { luaK_nil(fs, reg, extra); } } + if (nexps > nvars) + ls->fs->freereg -= nexps - nvars; /* remove extra values */ } @@ -1163,11 +1165,8 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { int nexps; checknext(ls, '='); nexps = explist(ls, &e); - if (nexps != nvars) { + if (nexps != nvars) adjust_assign(ls, nvars, nexps, &e); - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* remove extra values */ - } else { luaK_setoneret(ls->fs, &e); /* close last expression */ luaK_storevar(ls->fs, &lh->v, &e); From 098d44162b8df9d8001f99213b3ec45668203c55 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 23 Jun 2016 16:14:16 +0800 Subject: [PATCH 632/729] bson support meta array --- lualib-src/lua-bson.c | 165 ++++++++++++++++++++++-------------------- test/testbson.lua | 95 ++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 77 deletions(-) create mode 100644 test/testbson.lua diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 2024ed69..680a4f24 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -238,8 +238,6 @@ write_double(struct bson *b, lua_Number d) { } } -static void pack_dict(lua_State *L, struct bson *b, bool array, int depth); - static inline void append_key(struct bson *bs, int type, const char *key, size_t sz) { write_byte(bs, type); @@ -269,25 +267,7 @@ append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { } } -static void -append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { - size_t len = lua_rawlen(L, -1); - bool isarray = false; - if (len > 0) { - lua_pushinteger(L, len); - if (lua_next(L,-2) == 0) { - isarray = true; - } else { - lua_pop(L,2); - } - } - if (isarray) { - append_key(bs, BSON_ARRAY, key, sz); - } else { - append_key(bs, BSON_DOCUMENT, key, sz); - } - pack_dict(L, bs, isarray, depth); -} +static void append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth); static void write_binary(struct bson *b, const void * buffer, size_t sz) { @@ -409,65 +389,59 @@ bson_numstr( char *str, unsigned int i ) { } static void -pack_dict_data(lua_State *L, struct bson *b, bool isarray, int depth, int kt) { - char numberkey[32]; - const char * key = NULL; - size_t sz; - if (isarray) { - if (kt != LUA_TNUMBER) { - luaL_error(L, "Invalid array key type : %s", lua_typename(L, kt)); - return; - } - sz = bson_numstr(numberkey, (unsigned int)lua_tointeger(L,-2)-1); - key = numberkey; - - append_one(b, L, key, sz, depth); - lua_pop(L,1); - } else { - switch(kt) { - case LUA_TNUMBER: - // copy key, don't change key type - lua_pushvalue(L,-2); - lua_insert(L,-2); - key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz, depth); - lua_pop(L,2); - break; - case LUA_TSTRING: - key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz, depth); - lua_pop(L,1); - break; - default: - luaL_error(L, "Invalid key type : %s", lua_typename(L, kt)); - return; - } - } -} - -static void -pack_simple_dict(lua_State *L, struct bson *b, bool isarray, int depth) { - if (depth > MAX_DEPTH) { - luaL_error(L, "Too depth while encoding bson"); - } - luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table +pack_array(lua_State *L, struct bson *b, int depth, size_t len) { int length = reserve_length(b); - lua_pushnil(L); - while(lua_next(L,-2) != 0) { - int kt = lua_type(L, -2); - pack_dict_data(L, b, isarray, depth, kt); + size_t i; + for (i=1;i<=len;i++) { + char numberkey[32]; + size_t sz = bson_numstr(numberkey, i - 1); + const char * key = numberkey; + lua_geti(L, -1, i); + append_one(b, L, key, sz, depth); + lua_pop(L, 1); } write_byte(b,0); write_length(b, b->size - length, length); } +static void +pack_dict_data(lua_State *L, struct bson *b, int depth, int kt) { + const char * key = NULL; + size_t sz; + switch(kt) { + case LUA_TNUMBER: + // copy key, don't change key type + lua_pushvalue(L,-2); + lua_insert(L,-2); + key = lua_tolstring(L,-2,&sz); + append_one(b, L, key, sz, depth); + lua_pop(L,2); + break; + case LUA_TSTRING: + key = lua_tolstring(L,-2,&sz); + append_one(b, L, key, sz, depth); + lua_pop(L,1); + break; + default: + luaL_error(L, "Invalid key type : %s", lua_typename(L, kt)); + return; + } +} static void -pack_meta_dict(lua_State *L, struct bson *b, bool isarray, int depth) { - if (depth > MAX_DEPTH) { - luaL_error(L, "Too depth while encoding bson"); +pack_simple_dict(lua_State *L, struct bson *b, int depth) { + int length = reserve_length(b); + lua_pushnil(L); + while(lua_next(L,-2) != 0) { + int kt = lua_type(L, -2); + pack_dict_data(L, b, depth, kt); } - luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table + write_byte(b,0); + write_length(b, b->size - length, length); +} + +static void +pack_meta_dict(lua_State *L, struct bson *b, int depth) { int length = reserve_length(b); lua_pushvalue(L, -2); // push meta_obj @@ -483,18 +457,51 @@ pack_meta_dict(lua_State *L, struct bson *b, bool isarray, int depth) { lua_pop(L, 4); // pop all k, v, next_func, obj break; } - pack_dict_data(L, b, isarray, depth, kt); + pack_dict_data(L, b, depth, kt); } write_byte(b,0); write_length(b, b->size - length, length); } +static bool +is_rawarray(lua_State *L) { + size_t len = lua_rawlen(L, -1); + if (len > 0) { + lua_pushinteger(L, len); + if (lua_next(L,-2) == 0) { + return true; + } else { + lua_pop(L,2); + } + } + return false; +} + static void -pack_dict(lua_State *L, struct bson *b, bool isarray, int depth) { - if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { - pack_meta_dict(L, b, isarray, depth); +append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { + if (depth > MAX_DEPTH) { + luaL_error(L, "Too depth while encoding bson"); + } + luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table + if (luaL_getmetafield(L, -1, "__len") != LUA_TNIL) { + lua_pushvalue(L, -2); + lua_call(L, 1, 1); + if (!lua_isinteger(L, -1)) { + luaL_error(L, "__len should return integer"); + } + size_t len = lua_tointeger(L, -1); + lua_pop(L, 1); + append_key(bs, BSON_ARRAY, key, sz); + pack_array(L, bs, depth, len); + } else if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { + append_key(bs, BSON_DOCUMENT, key, sz); + pack_meta_dict(L, bs, depth); + } else if (is_rawarray(L)) { + append_key(bs, BSON_ARRAY, key, sz); + pack_array(L, bs, depth, lua_rawlen(L, -1)); } else { - pack_simple_dict(L, b, isarray, depth); + append_key(bs, BSON_DOCUMENT, key, sz); + pack_simple_dict(L, bs, depth); } } @@ -895,7 +902,11 @@ lencode(lua_State *L) { bson_create(&b); lua_settop(L,1); luaL_checktype(L, 1, LUA_TTABLE); - pack_dict(L, &b, false, 0); + if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { + pack_meta_dict(L, &b, 0); + } else { + pack_simple_dict(L, &b, 0); + } void * ud = lua_newuserdata(L, b.size); memcpy(ud, b.ptr, b.size); bson_destroy(&b); diff --git a/test/testbson.lua b/test/testbson.lua new file mode 100644 index 00000000..d589f01f --- /dev/null +++ b/test/testbson.lua @@ -0,0 +1,95 @@ +local bson = require "bson" + +sub = bson.encode_order( "hello", 1, "world", 2 ) + +local function tbl_next(...) + print("--- next.a", ...) + local k, v = next(...) + print("--- next.b", k, v) + return k, v +end + +local function tbl_pairs(obj) + return tbl_next, obj.__data, nil +end + +local obj_a = { + __data = { + [1] = 2, + [3] = 4, + [5] = 6, + } +} + +setmetatable( + obj_a, + { + __index = obj_a.__data, + __pairs = tbl_pairs, + } +) + +local obj_b = { + __data = { + [7] = 8, + [9] = 10, + [11] = obj_a, + } +} + +setmetatable( + obj_b, + { + __index = obj_b.__data, + __pairs = tbl_pairs, + } +) + +local metaarray = setmetatable({ n = 5 }, { + __len = function(self) return self.n end, + __index = function(self, idx) return tostring(idx) end, +}) + +b = bson.encode { + a = 1, + b = true, + c = bson.null, + d = { 1,2,3,4 }, + e = bson.binary "hello", + f = bson.regex ("*","i"), + g = bson.regex "hello", + h = bson.date (os.time()), + i = bson.timestamp(os.time()), + j = bson.objectid(), + k = { a = false, b = true }, + l = {}, + m = bson.minkey, + n = bson.maxkey, + o = sub, + p = 2^32-1, + q = obj_b, + r = metaarray, +} + +print "\n[before replace]" +t = b:decode() + +for k, v in pairs(t) do + print(k,type(v)) +end + +for k,v in ipairs(t.r) do + print(k,v) +end + +b:makeindex() +b.a = 2 +b.b = false +b.h = bson.date(os.time()) +b.i = bson.timestamp(os.time()) +b.j = bson.objectid() + +print "\n[after replace]" +t = b:decode() + +print("o.hello", bson.type(t.o.hello)) From 14a699cb85765d62a019e1c6a146c15c428e62dc Mon Sep 17 00:00:00 2001 From: zxfishhack Date: Mon, 27 Jun 2016 10:59:50 +0800 Subject: [PATCH 633/729] auto determine primary host --- lualib/mongo.lua | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 877982f4..7393ebbd 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -105,12 +105,38 @@ local function mongo_auth(mongoc) mongoc.__sock:changebackup(backup) end if rs_data.ismaster then + if rawget(mongoc, "__pickserver") then + rawset(mongoc, "__pickserver", nil) + end return else - local host, port = __parse_addr(rs_data.primary) - mongoc.host = host - mongoc.port = port - mongoc.__sock:changehost(host, port) + if rs_data.primary then + local host, port = __parse_addr(rs_data.primary) + mongoc.host = host + mongoc.port = port + mongoc.__sock:changehost(host, port) + else + skynet.error("WARNING: NO PRIMARY RETURN " .. rs_data.me) + -- determine the primary db using hosts + local pickserver = {} + if rawget(mongoc, "__pickserver") == nil then + for _, v in ipairs(rs_data.hosts) do + if v ~= rs_data.me then + table.insert(pickserver, v) + end + rawset(mongoc, "__pickserver", pickserver) + end + end + if #mongoc.__pickserver <= 0 then + error("CAN NOT DETERMINE THE PRIMARY DB") + end + skynet.error("INFO: TRY TO CONNECT " .. mongoc.__pickserver[1]) + local host, port = __parse_addr(mongoc.__pickserver[1]) + table.remove(mongoc.__pickserver, 1) + mongoc.host = host + mongoc.port = port + mongoc.__sock:changehost(host, port) + end end end end From 33321e89a19f86017118ee62a1f10d0a0ec15ee3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Jun 2016 18:31:12 +0800 Subject: [PATCH 634/729] don't launch console service when in daemon mode --- examples/config | 4 +++- examples/main.lua | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/config b/examples/config index 9533af81..368a06f6 100644 --- a/examples/config +++ b/examples/config @@ -9,7 +9,9 @@ start = "main" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap standalone = "0.0.0.0:2013" luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" -lualoader = "lualib/loader.lua" +lualoader = root .. "lualib/loader.lua" +lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua" +lua_cpath = root .. "luaclib/?.so" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run snax = root.."examples/?.lua;"..root.."test/?.lua" -- snax_interface_g = "snax_g" diff --git a/examples/main.lua b/examples/main.lua index cac49737..5a2150db 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -6,7 +6,9 @@ local max_client = 64 skynet.start(function() skynet.error("Server start") skynet.uniqueservice("protoloader") - local console = skynet.newservice("console") + if not skynet.getenv "daemon" then + local console = skynet.newservice("console") + end skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") From fd1b230a54d04086bc0f3ba68f2135f29822f998 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 4 Jul 2016 16:31:58 +0800 Subject: [PATCH 635/729] 1.0.0 rc5 --- HISTORY.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 3ed8a7c3..c41b0e5b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,11 @@ +v1.0.0-rc5 (2016-7-4) +----------- +* MongoDB : Support auth_scram_sha1 +* MongoDB : Auto determine primary host +* Bugfix : memory leak in multicast +* Bugfix : Lua 5.3.3 +* Bson : support meta array + v1.0.0-rc4 (2016-6-13) ----------- * Update lua to 5.3.3 From 97538286fd46b781ef607718d8dc71e7e0f343c6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 Jul 2016 19:39:45 +0800 Subject: [PATCH 636/729] 1.0.0 Released --- HISTORY.md | 4 ++++ README.md | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index c41b0e5b..3b7ea80e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +v1.0.0 (2016-7-11) +----------- +* Version 1.0.0 Released + v1.0.0-rc5 (2016-7-4) ----------- * MongoDB : Support auth_scram_sha1 diff --git a/README.md b/README.md index 1a65b43a..125a8f15 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +## Skynet + +Skynet is a lightweight online game framework, and it can be used in many other fields. + ## Build For Linux, install autoconf first for jemalloc: @@ -28,13 +32,11 @@ Run these in different consoles: ## About Lua version -Skynet now uses a modified version of lua 5.3.3 ( http://www.lua.org/ftp/lua-5.3.3.tar.gz ) . - -For details: http://lua-users.org/lists/lua-l/2014-03/msg00489.html +Skynet now uses a modified version of lua 5.3.3 ( https://github.com/ejoy/lua/tree/skynet ) for multiple lua states. You can also use official Lua versions, just edit the Makefile by yourself. ## How To Use (Sorry, Only in Chinese now) -* Read Wiki https://github.com/cloudwu/skynet/wiki +* Read Wiki for documents https://github.com/cloudwu/skynet/wiki * The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ From b278106336e98d3600d7721e31d289ba8c1c769c Mon Sep 17 00:00:00 2001 From: puXiaoyi <465577676@qq.com> Date: Thu, 14 Jul 2016 13:49:06 +0800 Subject: [PATCH 637/729] fix comment for lpushbuffer The size of first chunk ([2]) is 16 struct buffer_node, and the second size is 32 ... --- lualib-src/lua-socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index bc49e568..ecaa2fcb 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -87,8 +87,8 @@ lnewbuffer(lua_State *L) { Comment: The table pool record all the buffers chunk, and the first index [1] is a lightuserdata : free_node. We can always use this pointer for struct buffer_node . The following ([2] ...) userdatas in table pool is the buffer chunk (for struct buffer_node), - we never free them until the VM closed. The size of first chunk ([2]) is 8 struct buffer_node, - and the second size is 16 ... The largest size of chunk is LARGE_PAGE_NODE (4096) + we never free them until the VM closed. The size of first chunk ([2]) is 16 struct buffer_node, + and the second size is 32 ... The largest size of chunk is LARGE_PAGE_NODE (4096) lpushbbuffer will get a free struct buffer_node from table pool, and then put the msg/size in it. lpopbuffer return the struct buffer_node back to table pool (By calling return_free_node). From e2fa2a1a4d2a675b309f09c43c0bcd711aff08e3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 1 Aug 2016 14:18:13 +0800 Subject: [PATCH 638/729] lua5.3.3 bugfix 2/3 --- 3rd/lua/lcode.c | 4 ++-- 3rd/lua/loslib.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index 0cd5600e..a97da8dc 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1018,8 +1018,8 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { */ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { - int rk1 = luaK_exp2RK(fs, e1); /* both operands are "RK" */ - int rk2 = luaK_exp2RK(fs, e2); + int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ + int rk1 = luaK_exp2RK(fs, e1); freeexps(fs, e1, e2); e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ e1->k = VRELOCABLE; /* all those operations are relocatable */ diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index 48106555..a56877e1 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -260,7 +260,8 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { static const char *checkoption (lua_State *L, const char *conv, char *buff) { const char *option; int oplen = 1; - for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { + int convlen = (int)strlen(conv); + for (option = LUA_STRFTIMEOPTIONS; *option != '\0' && oplen <= convlen; option += oplen) { if (*option == '|') /* next block? */ oplen++; /* next length */ else if (memcmp(conv, option, oplen) == 0) { /* match? */ From c082d71a563d7ae33e38e4065b14fa1455502014 Mon Sep 17 00:00:00 2001 From: Learno Date: Thu, 4 Aug 2016 12:44:11 +0800 Subject: [PATCH 639/729] sort support multi key --- lualib/mongo.lua | 16 ++++++++++++++-- test/testmongodb.lua | 19 +++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 7393ebbd..3be7d18a 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -7,6 +7,7 @@ local md5 = require "md5" local crypt = require "crypt" local rawget = rawget local assert = assert +local table = table local bson_encode = bson.encode local bson_encode_order = bson.encode_order @@ -395,8 +396,19 @@ function mongo_collection:find(query, selector) } , cursor_meta) end -function mongo_cursor:sort(key_list) - self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key_list} +-- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1}) +function mongo_cursor:sort(key, key_v, ...) + if key_v then + local key_list = {} + for _, kp in ipairs {key, key_v, ...} do + local next_func, t = pairs(kp) + local k, v = next_func(t, v) -- The first key pair + table.insert(key_list, k) + table.insert(key_list, v) + end + key = bson_encode_order(table.unpack(key_list)) + end + self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key} return self end diff --git a/test/testmongodb.lua b/test/testmongodb.lua index d1fed034..bce37683 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -38,24 +38,27 @@ function test_find_and_remove() db[db_name].testdb:dropIndex("*") db[db_name].testdb:drop() - db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + db[db_name].testdb:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"}) - local ret = db[db_name].testdb:safe_insert({test_key = 1}) + local ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 1}) assert(ret and ret.n == 1) - local ret = db[db_name].testdb:safe_insert({test_key = 2}) + local ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2}) assert(ret and ret.n == 1) - local ret = db[db_name].testdb:findOne({test_key = 1}) - assert(ret and ret.test_key == 1) + local ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3}) + assert(ret and ret.n == 1) - local ret = db[db_name].testdb:find({test_key = {['$gt'] = 0}}):sort({test_key = -1}):skip(1):limit(1) - assert(ret:count() == 2) + local ret = db[db_name].testdb:findOne({test_key2 = 1}) + assert(ret and ret.test_key2 == 1) + + local ret = db[db_name].testdb:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1) + assert(ret:count() == 3) assert(ret:count(true) == 1) if ret:hasNext() then ret = ret:next() end - assert(ret and ret.test_key == 1) + assert(ret and ret.test_key2 == 1) db[db_name].testdb:delete({test_key = 1}) db[db_name].testdb:delete({test_key = 2}) From 283c3f10fe646b74aa85015d1834251cba6887d9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Aug 2016 17:48:10 +0800 Subject: [PATCH 640/729] bugfix: Issue #531 --- lualib-src/lua-crypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 6ddb246e..400322a5 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -494,7 +494,7 @@ static int lfromhex(lua_State *L) { size_t sz = 0; const char * text = luaL_checklstring(L, 1, &sz); - if (sz & 2) { + if (sz & 1) { return luaL_error(L, "Invalid hex text size %d", (int)sz); } char tmp[SMALL_CHUNK]; From 26ad20aa2b3df395fe480d87818bb2c8d24390eb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Aug 2016 11:55:13 +0800 Subject: [PATCH 641/729] debug cmem print total memory used --- service/debug_console.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/service/debug_console.lua b/service/debug_console.lua index 976e35b5..b5854e51 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -286,6 +286,9 @@ function COMMAND.cmem() for k,v in pairs(info) do tmp[skynet.address(k)] = v end + tmp.total = memory.total() + tmp.block = memory.block() + return tmp end From c91efa513435e71f24fd869e15ef409e0caf6c86 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Sep 2016 17:53:25 +0800 Subject: [PATCH 642/729] debug_console support user defined ip --- service/debug_console.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index b5854e51..4115bbe9 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -7,7 +7,11 @@ local memory = require "memory" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" -local port = tonumber(...) +local arg = table.pack(...) +assert(arg.n <= 2) +local ip = (arg.n == 2 and arg[1] or "127.0.0.1") +local port = tonumber(arg[arg.n]) + local COMMAND = {} local function format_table(t) @@ -103,8 +107,8 @@ local function console_main_loop(stdin, print) end skynet.start(function() - local listen_socket = socket.listen ("127.0.0.1", port) - skynet.error("Start debug console at 127.0.0.1 " .. port) + local listen_socket = socket.listen (ip, port) + skynet.error("Start debug console at " .. ip .. ":" .. port) socket.start(listen_socket , function(id, addr) local function print(...) local t = { ... } From 0782b355a4698c3962a4938c4cef1a004f1d1f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=8E=AE=E5=8D=8E?= Date: Thu, 8 Sep 2016 15:18:35 +0800 Subject: [PATCH 643/729] =?UTF-8?q?[bugfix]=20alpine=20linux=20=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E4=B8=8D=E9=80=9A=E8=BF=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skynet-src/skynet_daemon.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/skynet-src/skynet_daemon.c b/skynet-src/skynet_daemon.c index 4c5e3882..4b6b96e1 100644 --- a/skynet-src/skynet_daemon.c +++ b/skynet-src/skynet_daemon.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "skynet_daemon.h" @@ -27,7 +28,7 @@ check_pid(const char *pidfile) { return pid; } -static int +static int write_pid(const char *pidfile) { FILE *f; int pid = 0; @@ -52,7 +53,7 @@ write_pid(const char *pidfile) { } return 0; } - + pid = getpid(); if (!fprintf(f,"%d\n", pid)) { fprintf(stderr, "Can't write pid.\n"); @@ -90,7 +91,7 @@ daemon_init(const char *pidfile) { return 0; } -int +int daemon_exit(const char *pidfile) { return unlink(pidfile); } From ccb0e1b24c078e675971b2f05846334d4d9e3b3b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 24 Sep 2016 12:28:57 +0800 Subject: [PATCH 644/729] suppot httpc.timeout --- lualib/http/httpc.lua | 17 ++++++++++++++++- test/testhttp.lua | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 68d3ce9c..16a52823 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -1,3 +1,4 @@ +local skynet = require "skynet" local socket = require "http.sockethelper" local url = require "http.url" local internal = require "http.internal" @@ -88,6 +89,7 @@ function httpc.dns(server,port) end function httpc.request(method, host, url, recvheader, header, content) + local timeout = httpc.timeout -- get httpc.timeout before any blocked api local hostname, port = host:match"([^:]+):?(%d*)$" if port == "" then port = 80 @@ -98,8 +100,21 @@ function httpc.request(method, host, url, recvheader, header, content) hostname = dns.resolve(hostname) end local fd = socket.connect(hostname, port) + local finish + if timeout then + skynet.timeout(timeout, function() + if not finish then + local temp = fd + fd = nil + socket.close(temp) + end + end) + end local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content) - socket.close(fd) + finish = true + if fd then -- may close by skynet.timeout + socket.close(fd) + end if ok then return statuscode, body else diff --git a/test/testhttp.lua b/test/testhttp.lua index 76a5a4da..e66d6c19 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -4,6 +4,7 @@ local dns = require "dns" skynet.start(function() httpc.dns() -- set dns server + httpc.timeout = 100 -- set timeout 1 second print("GET baidu.com") local respheader = {} local status, body = httpc.get("baidu.com", "/", respheader) From f48eeca6673465e330347c496d78d142c9f1b7e4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 26 Sep 2016 13:07:55 +0800 Subject: [PATCH 645/729] add debug command call --- service/debug_console.lua | 61 ++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 4115bbe9..b34067ff 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -13,13 +13,14 @@ local ip = (arg.n == 2 and arg[1] or "127.0.0.1") local port = tonumber(arg[arg.n]) local COMMAND = {} +local COMMANDX = {} local function format_table(t) local index = {} for k in pairs(t) do table.insert(index, k) end - table.sort(index) + table.sort(index, function(a, b) return tostring(a) < tostring(b) end) local result = {} for _,v in ipairs(index) do table.insert(result, string.format("%s:%s",v,tostring(t[v]))) @@ -40,7 +41,7 @@ local function dump_list(print, list) for k in pairs(list) do table.insert(index, k) end - table.sort(index) + table.sort(index, function(a, b) return tostring(a) < tostring(b) end) for _,v in ipairs(index) do dump_line(print, v, list[v]) end @@ -61,9 +62,16 @@ local function docmd(cmdline, print, fd) local cmd = COMMAND[command] local ok, list if cmd then - ok, list = pcall(cmd, fd, select(2,table.unpack(split))) + ok, list = pcall(cmd, table.unpack(split,2)) else - print("Invalid command, type help for command list") + cmd = COMMANDX[command] + if cmd then + split.fd = fd + split[1] = cmdline + ok, list = pcall(cmd, split) + else + print("Invalid command, type help for command list") + end end if ok then @@ -147,6 +155,7 @@ function COMMAND.help() cmem = "Show C memory info", shrtbl = "Show shared short string table info", ping = "ping address", + call = "call address ...", } end @@ -154,7 +163,7 @@ function COMMAND.clearcache() codecache.clear() end -function COMMAND.start(fd, ...) +function COMMAND.start(...) local ok, addr = pcall(skynet.newservice, ...) if ok then if addr then @@ -167,7 +176,7 @@ function COMMAND.start(fd, ...) end end -function COMMAND.log(fd, ...) +function COMMAND.log(...) local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...) if ok then if addr then @@ -180,7 +189,7 @@ function COMMAND.log(fd, ...) end end -function COMMAND.snax(fd, ...) +function COMMAND.snax(...) local ok, s = pcall(snax.newservice, ...) if ok then local addr = s.handle @@ -213,7 +222,7 @@ function COMMAND.mem() return skynet.call(".launcher", "lua", "MEM") end -function COMMAND.kill(fd, address) +function COMMAND.kill(address) return skynet.call(".launcher", "lua", "KILL", address) end @@ -221,11 +230,11 @@ function COMMAND.gc() return skynet.call(".launcher", "lua", "GC") end -function COMMAND.exit(fd, address) +function COMMAND.exit(address) skynet.send(adjust_address(address), "debug", "EXIT") end -function COMMAND.inject(fd, address, filename) +function COMMAND.inject(address, filename) address = adjust_address(address) local f = io.open(filename, "rb") if not f then @@ -236,23 +245,23 @@ function COMMAND.inject(fd, address, filename) return skynet.call(address, "debug", "RUN", source, filename) end -function COMMAND.task(fd, address) +function COMMAND.task(address) address = adjust_address(address) return skynet.call(address,"debug","TASK") end -function COMMAND.info(fd, address, ...) +function COMMAND.info(address, ...) address = adjust_address(address) return skynet.call(address,"debug","INFO", ...) end -function COMMAND.debug(fd, address) - address = adjust_address(address) +function COMMANDX.debug(cmd) + local address = adjust_address(cmd[2]) local agent = skynet.newservice "debug_agent" local stop skynet.fork(function() repeat - local cmdline = socket.readline(fd, "\n") + local cmdline = socket.readline(cmd.fd, "\n") cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") if not cmdline then skynet.send(agent, "lua", "cmd", "cont") @@ -261,21 +270,21 @@ function COMMAND.debug(fd, address) skynet.send(agent, "lua", "cmd", cmdline) until stop or cmdline == "cont" end) - skynet.call(agent, "lua", "start", address, fd) + skynet.call(agent, "lua", "start", address, cmd.fd) stop = true end -function COMMAND.logon(fd, address) +function COMMAND.logon(address) address = adjust_address(address) core.command("LOGON", skynet.address(address)) end -function COMMAND.logoff(fd, address) +function COMMAND.logoff(address) address = adjust_address(address) core.command("LOGOFF", skynet.address(address)) end -function COMMAND.signal(fd, address, sig) +function COMMAND.signal(address, sig) address = skynet.address(adjust_address(address)) if sig then core.command("SIGNAL", string.format("%s %d",address,sig)) @@ -301,10 +310,22 @@ function COMMAND.shrtbl() return { n = n, total = total, longest = longest, space = space } end -function COMMAND.ping(fd, address) +function COMMAND.ping(address) address = adjust_address(address) local ti = skynet.now() skynet.call(address, "debug", "PING") ti = skynet.now() - ti return tostring(ti) end + +function COMMANDX.call(cmd) + local address = adjust_address(cmd[2]) + local cmdline = assert(cmd[1]:match("%S+%s+%S+%s(.+)") , "need arguments") + local args_func = assert(load("return " .. cmdline, "debug console", "t", {}), "Invalid arguments") + local args = table.pack(pcall(args_func)) + if not args[1] then + error(args[2]) + end + local rets = table.pack(skynet.call(address, "lua", table.unpack(args, 2, args.n))) + return rets +end From 9c6f8f8b82b35a0e8e489cf83f8ea9c05f36fa6a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Oct 2016 12:06:11 +0800 Subject: [PATCH 646/729] add sharedata.flush() to collect old version immediately --- lualib/sharedata.lua | 15 +++++++++++++++ lualib/sharedata/corelib.lua | 6 +++++- service/sharedatad.lua | 27 ++++++++++++++++++++------- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua index 93ead44d..5526b327 100644 --- a/lualib/sharedata.lua +++ b/lualib/sharedata.lua @@ -8,6 +8,7 @@ skynet.init(function() end) local sharedata = {} +local cache = setmetatable({}, { __mode = "kv" }) local function monitor(name, obj, cobj) local newobj = cobj @@ -18,13 +19,20 @@ local function monitor(name, obj, cobj) end sd.update(obj, newobj) end + if cache[name] == obj then + cache[name] = nil + end end function sharedata.query(name) + if cache[name] then + return cache[name] + end local obj = skynet.call(service, "lua", "query", name) local r = sd.box(obj) skynet.send(service, "lua", "confirm" , obj) skynet.fork(monitor,name, r, obj) + cache[name] = r return r end @@ -40,4 +48,11 @@ function sharedata.delete(name) skynet.call(service, "lua", "delete", name) end +function sharedata.flush() + for name, obj in pairs(cache) do + sd.flush(obj) + end + collectgarbage() +end + return sharedata diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index 481e85fa..08d6dd3d 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -128,4 +128,8 @@ function conf.update(self, pointer) core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) }) end -return conf \ No newline at end of file +function conf.flush(obj) + getcobj(obj) +end + +return conf diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 3f86155b..9888216b 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -8,6 +8,7 @@ local NORET = {} local pool = {} local pool_count = {} local objmap = {} +local collect_tick = 600 local function newobj(name, tbl) assert(pool[name] == nil) @@ -19,17 +20,28 @@ local function newobj(name, tbl) pool_count[name] = { n = 0, threshold = 16 } end +local function collect10sec() + if collect_tick > 10 then + collect_tick = 10 + end +end + local function collectobj() while true do - skynet.sleep(600 * 100) -- sleep 10 min - collectgarbage() - for obj, v in pairs(objmap) do - if v == true then - if sharedata.host.getref(obj) <= 0 then - objmap[obj] = nil - sharedata.host.delete(obj) + skynet.sleep(100) -- sleep 1s + if collect_tick <= 0 then + collect_tick = 600 -- reset tick count to 600 sec + collectgarbage() + for obj, v in pairs(objmap) do + if v == true then + if sharedata.host.getref(obj) <= 0 then + objmap[obj] = nil + sharedata.host.delete(obj) + end end end + else + collect_tick = collect_tick - 1 end end end @@ -109,6 +121,7 @@ function CMD.update(name, t, ...) response(true, newobj) end end + collect10sec() -- collect in 10 sec end local function check_watch(queue) From 07a149988625486377243d140acfe5efe7537513 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 26 Oct 2016 16:28:36 +0800 Subject: [PATCH 647/729] bugfix: profile --- lualib-src/lua-profile.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c index 0c196106..a717bfd0 100644 --- a/lualib-src/lua-profile.c +++ b/lualib-src/lua-profile.c @@ -50,20 +50,22 @@ diff_time(double start) { static int lstart(lua_State *L) { - if (lua_type(L,1) == LUA_TTHREAD) { + if (lua_gettop(L) != 0) { lua_settop(L,1); + luaL_checktype(L, 1, LUA_TTHREAD); } else { lua_pushthread(L); } + lua_pushvalue(L, 1); // push coroutine lua_rawget(L, lua_upvalueindex(2)); if (!lua_isnil(L, -1)) { return luaL_error(L, "Thread %p start profile more than once", lua_topointer(L, 1)); } - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_pushnumber(L, 0); lua_rawset(L, lua_upvalueindex(2)); - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine double ti = get_time(); #ifdef DEBUG_LOG fprintf(stderr, "PROFILE [%p] start\n", L); @@ -76,25 +78,27 @@ lstart(lua_State *L) { static int lstop(lua_State *L) { - if (lua_type(L,1) == LUA_TTHREAD) { + if (lua_gettop(L) != 0) { lua_settop(L,1); + luaL_checktype(L, 1, LUA_TTHREAD); } else { lua_pushthread(L); } + lua_pushvalue(L, 1); // push coroutine lua_rawget(L, lua_upvalueindex(1)); if (lua_type(L, -1) != LUA_TNUMBER) { return luaL_error(L, "Call profile.start() before profile.stop()"); } double ti = diff_time(lua_tonumber(L, -1)); - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_rawget(L, lua_upvalueindex(2)); double total_time = lua_tonumber(L, -1); - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_pushnil(L); lua_rawset(L, lua_upvalueindex(1)); - lua_pushthread(L); + lua_pushvalue(L, 1); // push coroutine lua_pushnil(L); lua_rawset(L, lua_upvalueindex(2)); From 249ffb9362759f72437be95927ef7b06fa57d286 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 26 Oct 2016 17:43:52 +0800 Subject: [PATCH 648/729] add skynet.stat() to get cpu cost and message count --- lualib-src/lua-skynet.c | 25 ++++++++++++--- lualib/skynet.lua | 8 +++-- lualib/skynet/debug.lua | 4 ++- skynet-src/skynet_imp.h | 1 + skynet-src/skynet_main.c | 3 +- skynet-src/skynet_server.c | 62 +++++++++++++++++++++++++++----------- skynet-src/skynet_server.h | 2 ++ skynet-src/skynet_start.c | 1 + skynet-src/skynet_timer.c | 24 +++++++++++++++ skynet-src/skynet_timer.h | 1 + 10 files changed, 104 insertions(+), 27 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 2422f413..ee7bd897 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -126,15 +126,30 @@ lintcommand(lua_State *L) { const char * parm = NULL; char tmp[64]; // for integer parm if (lua_gettop(L) == 2) { - int32_t n = (int32_t)luaL_checkinteger(L,2); - sprintf(tmp, "%d", n); - parm = tmp; + if (lua_isnumber(L, 2)) { + int32_t n = (int32_t)luaL_checkinteger(L,2); + sprintf(tmp, "%d", n); + parm = tmp; + } else { + parm = luaL_checkstring(L,2); + } } result = skynet_command(context, cmd, parm); if (result) { - lua_Integer r = strtoll(result, NULL, 0); - lua_pushinteger(L, r); + char *endptr = NULL; + lua_Integer r = strtoll(result, &endptr, 0); + if (endptr == NULL || *endptr != '\0') { + // may be real number + double n = strtod(result, &endptr); + if (endptr == NULL || *endptr != '\0') { + return luaL_error(L, "Invalid result %s", result); + } else { + lua_pushnumber(L, n); + } + } else { + lua_pushinteger(L, r); + } return 1; } return 0; diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 16ad0c24..a5353a03 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -634,11 +634,15 @@ function skynet.start(start_func) end function skynet.endless() - return c.command("ENDLESS")~=nil + return (c.intcommand("STAT", "endless") == 1) end function skynet.mqlen() - return c.intcommand "MQLEN" + return c.intcommand("STAT", "mqlen") +end + +function skynet.stat(what) + return c.intcommand("STAT", what) end function skynet.task(ret) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 8fb484de..8bc999c6 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -25,8 +25,10 @@ local function init(skynet, export) function dbgcmd.STAT() local stat = {} - stat.mqlen = skynet.mqlen() stat.task = skynet.task() + stat.mqlen = skynet.stat "mqlen" + stat.cpu = skynet.stat "cpu" + stat.message = skynet.stat "message" skynet.ret(skynet.pack(stat)) end diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index 1126d2c9..eef5d509 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -4,6 +4,7 @@ struct skynet_config { int thread; int harbor; + int profile; const char * daemon; const char * module_path; const char * bootstrap; diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index f4296cd9..f3ce56b2 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -26,7 +26,6 @@ optint(const char *key, int opt) { return strtol(str, NULL, 10); } -/* static int optboolean(const char *key, int opt) { const char * str = skynet_getenv(key); @@ -36,7 +35,6 @@ optboolean(const char *key, int opt) { } return strcmp(str,"true")==0; } -*/ static const char * optstring(const char *key,const char * opt) { @@ -137,6 +135,7 @@ main(int argc, char *argv[]) { config.daemon = optstring("daemon", NULL); config.logger = optstring("logger", NULL); config.logservice = optstring("logservice", "logger"); + config.profile = optboolean("profile", 1); lua_close(L); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index f5d7f6bd..736f99af 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -10,6 +10,7 @@ #include "skynet_monitor.h" #include "skynet_imp.h" #include "skynet_log.h" +#include "skynet_timer.h" #include "spinlock.h" #include "atomic.h" @@ -46,12 +47,15 @@ struct skynet_context { skynet_cb cb; struct message_queue *queue; FILE * logfile; + uint64_t cpu_cost; // in microsec char result[32]; uint32_t handle; int session_id; int ref; + int message_count; bool init; bool endless; + bool profile; CHECKCALLING_DECL }; @@ -61,6 +65,7 @@ struct skynet_node { int init; uint32_t monitor_exit; pthread_key_t handle_key; + bool profile; // default is off }; static struct skynet_node G_NODE; @@ -139,6 +144,10 @@ skynet_context_new(const char * name, const char *param) { ctx->init = false; ctx->endless = false; + + ctx->cpu_cost = 0; + ctx->message_count = 0; + ctx->profile = G_NODE.profile; // Should set to 0 first to avoid skynet_handle_retireall get an uninitialized handle ctx->handle = 0; ctx->handle = skynet_handle_register(ctx); @@ -256,9 +265,19 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { if (ctx->logfile) { skynet_log_output(ctx->logfile, msg->source, type, msg->session, msg->data, sz); } - if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) { + ++ctx->message_count; + int reserve_msg; + if (ctx->profile) { + uint64_t start_time = skynet_thread_time(); + reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); + uint64_t cost_time = skynet_thread_time() - start_time; + ctx->cpu_cost += cost_time; + } else { + reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); + } + if (!reserve_msg) { skynet_free(msg->data); - } + } CHECKCALLING_END(ctx) } @@ -505,16 +524,6 @@ cmd_starttime(struct skynet_context * context, const char * param) { return context->result; } -static const char * -cmd_endless(struct skynet_context * context, const char * param) { - if (context->endless) { - strcpy(context->result, "1"); - context->endless = false; - return context->result; - } - return NULL; -} - static const char * cmd_abort(struct skynet_context * context, const char * param) { skynet_handle_retireall(); @@ -539,9 +548,25 @@ cmd_monitor(struct skynet_context * context, const char * param) { } static const char * -cmd_mqlen(struct skynet_context * context, const char * param) { - int len = skynet_mq_length(context->queue); - sprintf(context->result, "%d", len); +cmd_stat(struct skynet_context * context, const char * param) { + if (strcmp(param, "mqlen") == 0) { + int len = skynet_mq_length(context->queue); + sprintf(context->result, "%d", len); + } else if (strcmp(param, "endless") == 0) { + if (context->endless) { + strcpy(context->result, "1"); + context->endless = false; + } else { + strcpy(context->result, "0"); + } + } else if (strcmp(param, "cpu") == 0) { + double t = (double)context->cpu_cost / 1000000.0; // microsec + sprintf(context->result, "%lf", t); + } else if (strcmp(param, "message") == 0) { + sprintf(context->result, "%d", context->message_count); + } else { + context->result[0] = '\0'; + } return context->result; } @@ -618,10 +643,9 @@ static struct command_func cmd_funcs[] = { { "GETENV", cmd_getenv }, { "SETENV", cmd_setenv }, { "STARTTIME", cmd_starttime }, - { "ENDLESS", cmd_endless }, { "ABORT", cmd_abort }, { "MONITOR", cmd_monitor }, - { "MQLEN", cmd_mqlen }, + { "STAT", cmd_stat }, { "LOGON", cmd_logon }, { "LOGOFF", cmd_logoff }, { "SIGNAL", cmd_signal }, @@ -779,3 +803,7 @@ skynet_initthread(int m) { pthread_setspecific(G_NODE.handle_key, (void *)v); } +void +skynet_profile_enable(int enable) { + G_NODE.profile = (bool)enable; +} diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 92e125b0..0644dc4d 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -26,4 +26,6 @@ void skynet_globalinit(void); void skynet_globalexit(void); void skynet_initthread(int m); +void skynet_profile_enable(int enable); + #endif diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index a7b1d70b..2f5ddea1 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -263,6 +263,7 @@ skynet_start(struct skynet_config * config) { skynet_module_init(config->module_path); skynet_timer_init(); skynet_socket_init(); + skynet_profile_enable(config->profile); struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger); if (ctx == NULL) { diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 5edff943..6e1497d3 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -14,6 +14,8 @@ #if defined(__APPLE__) #include +#include +#include #endif typedef void (*timer_execute_func)(void *ud,void *arg); @@ -296,3 +298,25 @@ skynet_timer_init(void) { TI->current_point = gettime(); } +// for profile + +#define NANOSEC 1000000000 +#define MICROSEC 1000000 + +uint64_t +skynet_thread_time(void) { +#if !defined(__APPLE__) + struct timespec ti; + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ti); + + return (uint64_t)ti.tv_sec * MICROSEC + (uint64_t)ti.tv_nsec / (NANOSEC / MICROSEC); +#else + struct task_thread_times_info aTaskInfo; + mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; + if (KERN_SUCCESS != task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)) { + return 0; + } + + return (uint64_t)(aTaskInfo.user_time.seconds) + (uint64_t)aTaskInfo.user_time.microseconds; +#endif +} diff --git a/skynet-src/skynet_timer.h b/skynet-src/skynet_timer.h index b278dbcb..c204fff0 100644 --- a/skynet-src/skynet_timer.h +++ b/skynet-src/skynet_timer.h @@ -6,6 +6,7 @@ int skynet_timeout(uint32_t handle, int time, int session); void skynet_updatetime(void); uint32_t skynet_starttime(void); +uint64_t skynet_thread_time(void); // for profile, in micro second void skynet_timer_init(void); From ffe5de468e9364a215dcbc2aa2425e624b402a50 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 26 Oct 2016 18:01:22 +0800 Subject: [PATCH 649/729] add skynet.stat 'time' --- skynet-src/skynet_server.c | 14 ++++++++++++-- test/testendless.lua | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/testendless.lua diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 736f99af..d730d1dd 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -48,6 +48,7 @@ struct skynet_context { struct message_queue *queue; FILE * logfile; uint64_t cpu_cost; // in microsec + uint64_t cpu_start; // in microsec char result[32]; uint32_t handle; int session_id; @@ -146,6 +147,7 @@ skynet_context_new(const char * name, const char *param) { ctx->endless = false; ctx->cpu_cost = 0; + ctx->cpu_start = 0; ctx->message_count = 0; ctx->profile = G_NODE.profile; // Should set to 0 first to avoid skynet_handle_retireall get an uninitialized handle @@ -268,9 +270,9 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { ++ctx->message_count; int reserve_msg; if (ctx->profile) { - uint64_t start_time = skynet_thread_time(); + ctx->cpu_start = skynet_thread_time(); reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); - uint64_t cost_time = skynet_thread_time() - start_time; + uint64_t cost_time = skynet_thread_time() - ctx->cpu_start; ctx->cpu_cost += cost_time; } else { reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); @@ -562,6 +564,14 @@ cmd_stat(struct skynet_context * context, const char * param) { } else if (strcmp(param, "cpu") == 0) { double t = (double)context->cpu_cost / 1000000.0; // microsec sprintf(context->result, "%lf", t); + } else if (strcmp(param, "time") == 0) { + if (context->profile) { + uint64_t ti = skynet_thread_time() - context->cpu_start; + double t = (double)ti / 1000000.0; // microsec + sprintf(context->result, "%lf", t); + } else { + strcpy(context->result, "0"); + } } else if (strcmp(param, "message") == 0) { sprintf(context->result, "%d", context->message_count); } else { diff --git a/test/testendless.lua b/test/testendless.lua new file mode 100644 index 00000000..928c1707 --- /dev/null +++ b/test/testendless.lua @@ -0,0 +1,11 @@ +local skynet = require "skynet" + +skynet.start(function() + for i = 1, 1000000000 do -- very long loop + if i%100000000 == 0 then + print("Endless = ", skynet.stat "endless") + print("Cost time = ", skynet.stat "time") + end + end + skynet.exit() +end) From c5fb8ef3f7e44aa42a0e193e61fd7509911d7f17 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 14 Nov 2016 13:05:01 +0800 Subject: [PATCH 650/729] add sharedata.deepcopy --- lualib/sharedata.lua | 12 ++++++++++ lualib/sharedata/corelib.lua | 45 +++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua index 5526b327..a17a20ba 100644 --- a/lualib/sharedata.lua +++ b/lualib/sharedata.lua @@ -55,4 +55,16 @@ function sharedata.flush() collectgarbage() end +function sharedata.deepcopy(name, ...) + if cache[name] then + local cobj = cache[name].__obj + return sd.copy(cobj, ...) + end + + local cobj = skynet.call(service, "lua", "query", name) + local ret = sd.copy(cobj, ...) + skynet.send(service, "lua", "confirm" , cobj) + return ret +end + return sharedata diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index 08d6dd3d..bd42cf1b 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -20,6 +20,7 @@ local isdirty = core.isdirty local index = core.index local needupdate = core.needupdate local len = core.len +local core_nextkey = core.nextkey local function findroot(self) while self.__parent do @@ -106,7 +107,7 @@ end function conf.next(obj, key) local cobj = getcobj(obj) - local nextkey = core.nextkey(cobj, key) + local nextkey = core_nextkey(cobj, key) if nextkey then return nextkey, obj[nextkey] end @@ -132,4 +133,46 @@ function conf.flush(obj) getcobj(obj) end +local function clone_table(cobj) + local obj = {} + local key + while true do + key = core_nextkey(cobj, key) + if key == nil then + break + end + local v = index(cobj, key) + if type(v) == "userdata" then + v = clone_table(v) + end + obj[key] = v + end + return obj +end + +local function find_node(cobj, key, ...) + if key == nil then + return cobj + end + local cobj = index(cobj, key) + if cobj == nil then + return nil + end + if type(cobj) == "userdata" then + return find_node(cobj, ...) + end + return cobj +end + +function conf.copy(cobj, ...) + cobj = find_node(cobj, ...) + if cobj then + if type(cobj) == "userdata" then + return clone_table(cobj) + else + return cobj + end + end +end + return conf From 708d57a77e01a11389c7b18621a514e1dac6ab54 Mon Sep 17 00:00:00 2001 From: lpy Date: Tue, 22 Nov 2016 11:14:07 +0800 Subject: [PATCH 651/729] pass the inject code error to debug_console --- lualib/skynet/debug.lua | 4 ++-- lualib/skynet/inject.lua | 5 +++-- service/debug_console.lua | 6 +++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 8bc999c6..e267b9a9 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -52,9 +52,9 @@ local function init(skynet, export) function dbgcmd.RUN(source, filename) local inject = require "skynet.inject" - local output = inject(skynet, source, filename , export.dispatch, skynet.register_protocol) + local ok, output = inject(skynet, source, filename , export.dispatch, skynet.register_protocol) collectgarbage "collect" - skynet.ret(skynet.pack(table.concat(output, "\n"))) + skynet.ret(skynet.pack(ok, table.concat(output, "\n"))) end function dbgcmd.TERM(service) diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua index 3c87bbb3..46c1c8ae 100644 --- a/lualib/skynet/inject.lua +++ b/lualib/skynet/inject.lua @@ -54,12 +54,13 @@ return function(skynet, source, filename , ...) local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) local func, err = load(source, filename, "bt", env) if not func then - return { err } + return false, { err } end local ok, err = skynet.pcall(func) if not ok then table.insert(output, err) + return false, output end - return output + return true, output end diff --git a/service/debug_console.lua b/service/debug_console.lua index b34067ff..7029c802 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -242,7 +242,11 @@ function COMMAND.inject(address, filename) end local source = f:read "*a" f:close() - return skynet.call(address, "debug", "RUN", source, filename) + local ok, output = skynet.call(address, "debug", "RUN", source, filename) + if ok == false then + error(output) + end + return output end function COMMAND.task(address) From bac383441ece81fd9729795cd116ef6caefe417a Mon Sep 17 00:00:00 2001 From: lpy Date: Tue, 22 Nov 2016 19:37:10 +0800 Subject: [PATCH 652/729] modify debug_console result string --- service/debug_console.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 7029c802..d76a1e58 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -45,7 +45,6 @@ local function dump_list(print, list) for _,v in ipairs(index) do dump_line(print, v, list[v]) end - print("OK") end local function split_cmdline(cmdline) @@ -81,11 +80,11 @@ local function docmd(cmdline, print, fd) else dump_list(print, list) end - else - print("OK") end + print("") else - print("Error:", list) + print(list) + print("") end end From 060e5384f536d487d735d9ccc276860fc869b399 Mon Sep 17 00:00:00 2001 From: Wei Yao Date: Fri, 2 Dec 2016 11:43:14 +0800 Subject: [PATCH 653/729] skynet.lua raw_dispatch_message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 似乎少了个session判断 --- lualib/skynet.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a5353a03..9a590d1d 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -489,6 +489,8 @@ local function raw_dispatch_message(prototype, msg, sz, session, source) session_coroutine_id[co] = session session_coroutine_address[co] = source suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz))) + elseif session ~= 0 then + c.send(source, skynet.PTYPE_ERROR, session, "") else unknown_request(session, source, msg, sz, proto[prototype].name) end From ace41d7c2261241c047cb7ef0ead77f6c3cc9b6d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Dec 2016 20:39:49 +0800 Subject: [PATCH 654/729] clusterd support send --- lualib/cluster.lua | 5 +++++ lualib/skynet.lua | 3 +++ 2 files changed, 8 insertions(+) diff --git a/lualib/cluster.lua b/lualib/cluster.lua index dba8f318..9883227f 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -8,6 +8,11 @@ function cluster.call(node, address, ...) return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...)) end +function cluster.send(node, address, ...) + -- skynet.pack(...) will free by cluster.core.packrequest + skynet.send(clusterd, "lua", "req", node, address, skynet.pack(...)) +end + function cluster.open(port) if type(port) == "string" then skynet.call(clusterd, "lua", "listen", port) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 9a590d1d..c3b98942 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -161,6 +161,9 @@ function suspend(co, result, command, param, size) sleep_session[co] = param elseif command == "RETURN" then local co_session = session_coroutine_id[co] + if co_session == 0 then + return suspend(co, coroutine_resume(co, false)) -- send don't need ret + end local co_address = session_coroutine_address[co] if param == nil or session_response[co] then error(debug.traceback(co)) From a3eccf5e371a04f8d88522cc87227fb8d1ab4ca0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Dec 2016 20:49:16 +0800 Subject: [PATCH 655/729] trash message --- lualib/skynet.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index c3b98942..4cefc893 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -162,6 +162,9 @@ function suspend(co, result, command, param, size) elseif command == "RETURN" then local co_session = session_coroutine_id[co] if co_session == 0 then + if size ~= nil then + c.trash(param, size) + end return suspend(co, coroutine_resume(co, false)) -- send don't need ret end local co_address = session_coroutine_address[co] From 7ee9cdb29be686973b72dc0a56f5c92eac902590 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Dec 2016 21:23:12 +0800 Subject: [PATCH 656/729] dummy session is 1 --- service-src/service_gate.c | 6 +++--- service/gate.lua | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/service-src/service_gate.c b/service-src/service_gate.c index 71ab1a64..b8da7441 100644 --- a/service-src/service_gate.c +++ b/service-src/service_gate.c @@ -171,18 +171,18 @@ _forward(struct gate *g, struct connection * c, int size) { if (g->broker) { void * temp = skynet_malloc(size); databuffer_read(&c->buffer,&g->mp,temp, size); - skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, 0, temp, size); + skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, 1, temp, size); return; } if (c->agent) { void * temp = skynet_malloc(size); databuffer_read(&c->buffer,&g->mp,temp, size); - skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, 0 , temp, size); + skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, 1 , temp, size); } else if (g->watchdog) { char * tmp = skynet_malloc(size + 32); int n = snprintf(tmp,32,"%d data ",c->id); databuffer_read(&c->buffer,&g->mp,tmp+n,size); - skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT | PTYPE_TAG_DONTCOPY, 0, tmp, size + n); + skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT | PTYPE_TAG_DONTCOPY, 1, tmp, size + n); } } diff --git a/service/gate.lua b/service/gate.lua index 78fb099f..f61eed6e 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -22,7 +22,7 @@ function handler.message(fd, msg, sz) local c = connection[fd] local agent = c.agent if agent then - skynet.redirect(agent, c.client, "client", 0, msg, sz) + skynet.redirect(agent, c.client, "client", 1, msg, sz) else skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) end From 1e51af2d62499ad9f8463832dbdf0168aa54ab53 Mon Sep 17 00:00:00 2001 From: dmx Date: Thu, 15 Dec 2016 15:59:23 +0800 Subject: [PATCH 657/729] get snax profile_table after rewriting info_func of snax service --- lualib/snax.lua | 5 +++++ lualib/snax/interface.lua | 2 +- service/snaxd.lua | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lualib/snax.lua b/lualib/snax.lua index a84dbb46..c21272b2 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -166,4 +166,9 @@ function snax.printf(fmt, ...) skynet.error(string.format(fmt, ...)) end +function snax.profile_info(obj) + local t = snax.interface(obj.type) + return skynet_call(obj.handle, "snax", t.system.profile) +end + return snax diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index 740f09d9..df650865 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -35,7 +35,7 @@ return function (name , G, loader) local env = setmetatable({} , { __index = temp_global }) local func = {} - local system = { "init", "exit", "hotfix" } + local system = { "init", "exit", "hotfix", "profile"} do for k, v in ipairs(system) do diff --git a/service/snaxd.lua b/service/snaxd.lua index 1b8ddee6..4e2b7216 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -54,6 +54,8 @@ skynet.start(function() if command == "hotfix" then local hotfix = require "snax.hotfix" skynet.ret(skynet.pack(hotfix(func, ...))) + elseif command == "profile" then + skynet.ret(skynet.pack(profile_table)) elseif command == "init" then assert(not init, "Already init") local initfunc = method[4] or function() end From 2dbb0129d3a208bb6e5c36c7299e9b2ab241a54a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 3 Jan 2017 10:44:39 +0800 Subject: [PATCH 658/729] update to lua 5.3.4 --- 3rd/lua/lapi.c | 2 +- 3rd/lua/lauxlib.c | 46 +++++---- 3rd/lua/lauxlib.h | 12 ++- 3rd/lua/lbaselib.c | 4 +- 3rd/lua/lcode.c | 19 ++-- 3rd/lua/ldebug.c | 45 ++++++--- 3rd/lua/ldo.c | 226 +++++++++++++++++++++++---------------------- 3rd/lua/lgc.c | 8 +- 3rd/lua/linit.c | 6 +- 3rd/lua/liolib.c | 17 ++-- 3rd/lua/lmathlib.c | 17 ++-- 3rd/lua/loadlib.c | 110 +++------------------- 3rd/lua/lobject.c | 6 +- 3rd/lua/lobject.h | 4 +- 3rd/lua/lopcodes.h | 4 +- 3rd/lua/loslib.c | 29 +++--- 3rd/lua/lparser.c | 7 +- 3rd/lua/lstate.h | 11 ++- 3rd/lua/lstrlib.c | 20 ++-- 3rd/lua/ltable.c | 46 ++++----- 3rd/lua/ltable.h | 14 ++- 3rd/lua/ltm.c | 4 +- 3rd/lua/lua.c | 104 +++++++++++++++++++-- 3rd/lua/lua.h | 8 +- 3rd/lua/luaconf.h | 34 +++++-- 3rd/lua/lutf8lib.c | 4 +- 3rd/lua/lvm.h | 4 +- 27 files changed, 446 insertions(+), 365 deletions(-) diff --git a/3rd/lua/lapi.c b/3rd/lua/lapi.c index fdffdbe1..d8ee0386 100644 --- a/3rd/lua/lapi.c +++ b/3rd/lua/lapi.c @@ -1012,7 +1012,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, return status; } -void cloneproto (lua_State *L, Proto *f, const Proto *src) { +static void cloneproto (lua_State *L, Proto *f, const Proto *src) { /* copy constants and nested proto */ int i,n; n = src->sp->sizek; diff --git a/3rd/lua/lauxlib.c b/3rd/lua/lauxlib.c index be061be7..5988509e 100644 --- a/3rd/lua/lauxlib.c +++ b/3rd/lua/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.286 2016/01/08 15:33:09 roberto Exp $ +** $Id: lauxlib.c,v 1.289 2016/12/20 18:37:00 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -69,12 +69,11 @@ static int findfield (lua_State *L, int objidx, int level) { /* ** Search for a name for a function in all loaded modules -** (registry._LOADED). */ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); if (findfield(L, top + 1, 2)) { const char *name = lua_tostring(L, -1); if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ @@ -809,13 +808,17 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { - if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ + if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ + if (!lua_isstring(L, -1)) + luaL_error(L, "'__tostring' must return a string"); + } + else { switch (lua_type(L, idx)) { case LUA_TNUMBER: { if (lua_isinteger(L, idx)) - lua_pushfstring(L, "%I", lua_tointeger(L, idx)); + lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); else - lua_pushfstring(L, "%f", lua_tonumber(L, idx)); + lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); break; } case LUA_TSTRING: @@ -827,10 +830,15 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { case LUA_TNIL: lua_pushliteral(L, "nil"); break; - default: - lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), - lua_topointer(L, idx)); + default: { + int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ + const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : + luaL_typename(L, idx); + lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); + if (tt != LUA_TNIL) + lua_remove(L, -2); /* remove '__name' */ break; + } } } return lua_tolstring(L, -1, len); @@ -882,23 +890,23 @@ static int libsize (const luaL_Reg *l) { /* ** Find or create a module table with a given name. The function -** first looks at the _LOADED table and, if that fails, try a +** first looks at the LOADED table and, if that fails, try a ** global variable with that name. In any case, leaves on the stack ** the module table. */ LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, int sizehint) { - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ - if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */ + luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1); + if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */ lua_pop(L, 1); /* remove previous result */ /* try global variable (and create one if it does not exist) */ lua_pushglobaltable(L); if (luaL_findtable(L, 0, modname, sizehint) != NULL) luaL_error(L, "name conflict for module '%s'", modname); lua_pushvalue(L, -1); - lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ + lua_setfield(L, -3, modname); /* LOADED[modname] = new table */ } - lua_remove(L, -2); /* remove _LOADED table */ + lua_remove(L, -2); /* remove LOADED table */ } @@ -962,17 +970,17 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { */ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) { - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, modname); /* _LOADED[modname] */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); + lua_getfield(L, -1, modname); /* LOADED[modname] */ if (!lua_toboolean(L, -1)) { /* package not already loaded? */ lua_pop(L, 1); /* remove field */ lua_pushcfunction(L, openf); lua_pushstring(L, modname); /* argument to open function */ lua_call(L, 1, 1); /* call 'openf' to open module */ lua_pushvalue(L, -1); /* make copy of module (call result) */ - lua_setfield(L, -3, modname); /* _LOADED[modname] = module */ + lua_setfield(L, -3, modname); /* LOADED[modname] = module */ } - lua_remove(L, -2); /* remove _LOADED table */ + lua_remove(L, -2); /* remove LOADED table */ if (glb) { lua_pushvalue(L, -1); /* copy of module */ lua_setglobal(L, modname); /* _G[modname] = module */ @@ -1030,7 +1038,7 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { luaL_error(L, "multiple Lua VMs detected"); else if (*v != ver) luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", - ver, *v); + (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v); } // use clonefunction diff --git a/3rd/lua/lauxlib.h b/3rd/lua/lauxlib.h index ddb7c228..9a2e66aa 100644 --- a/3rd/lua/lauxlib.h +++ b/3rd/lua/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.129 2015/11/23 11:29:43 roberto Exp $ +** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -16,10 +16,18 @@ -/* extra error code for 'luaL_load' */ +/* extra error code for 'luaL_loadfilex' */ #define LUA_ERRFILE (LUA_ERRERR+1) +/* key, in the registry, for table of loaded modules */ +#define LUA_LOADED_TABLE "_LOADED" + + +/* key, in the registry, for table of preloaded loaders */ +#define LUA_PRELOAD_TABLE "_PRELOAD" + + typedef struct luaL_Reg { const char *name; lua_CFunction func; diff --git a/3rd/lua/lbaselib.c b/3rd/lua/lbaselib.c index d481c4e1..08523e6e 100644 --- a/3rd/lua/lbaselib.c +++ b/3rd/lua/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.313 2016/04/11 19:18:40 roberto Exp $ +** $Id: lbaselib.c,v 1.314 2016/09/05 19:06:34 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ @@ -208,8 +208,8 @@ static int luaB_type (lua_State *L) { static int pairsmeta (lua_State *L, const char *method, int iszero, lua_CFunction iter) { + luaL_checkany(L, 1); if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */ - luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ lua_pushcfunction(L, iter); /* will return generator, */ lua_pushvalue(L, 1); /* state, */ if (iszero) lua_pushinteger(L, 0); /* and initial value */ diff --git a/3rd/lua/lcode.c b/3rd/lua/lcode.c index a97da8dc..18116445 100644 --- a/3rd/lua/lcode.c +++ b/3rd/lua/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.109 2016/05/13 19:09:21 roberto Exp $ +** $Id: lcode.c,v 2.112 2016/12/22 13:08:50 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -40,7 +40,7 @@ ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. */ -static int tonumeral(expdesc *e, TValue *v) { +static int tonumeral(const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a numeral */ switch (e->k) { @@ -86,7 +86,7 @@ void luaK_nil (FuncState *fs, int from, int n) { /* ** Gets the destination address of a jump instruction. Used to traverse ** a list of jumps. -*/ +*/ static int getjump (FuncState *fs, int pc) { int offset = GETARG_sBx(fs->f->sp->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ @@ -176,7 +176,6 @@ int luaK_getlabel (FuncState *fs) { ** jump (that is, its condition), or the jump itself if it is ** unconditional. */ - static Instruction *getjumpcontrol (FuncState *fs, int pc) { Instruction *pi = &fs->f->sp->code[pc]; if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) @@ -755,7 +754,7 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { ** (that is, it is either in a register or in 'k' with an index ** in the range of R/K indices). ** Returns R/K index. -*/ +*/ int luaK_exp2RK (FuncState *fs, expdesc *e) { luaK_exp2val(fs, e); switch (e->k) { /* move constants to 'k' */ @@ -976,7 +975,8 @@ static int validop (int op, TValue *v1, TValue *v2) { ** Try to "constant-fold" an operation; return 1 iff successful. ** (In this case, 'e1' has the final result.) */ -static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { +static int constfolding (FuncState *fs, int op, expdesc *e1, + const expdesc *e2) { TValue v1, v2, res; if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) return 0; /* non-numeric operands or not safe to fold */ @@ -1015,6 +1015,9 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { ** (everything but logical operators 'and'/'or' and comparison ** operators). ** Expression to produce final result will be encoded in 'e1'. +** Because 'luaK_exp2RK' can free registers, its calls must be +** in "stack order" (that is, first on 'e2', which may have more +** recent registers to be released). */ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { @@ -1061,9 +1064,9 @@ static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { ** Aplly prefix operation 'op' to expression 'e'. */ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { - static expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; /* fake 2nd operand */ + static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; switch (op) { - case OPR_MINUS: case OPR_BNOT: + case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ if (constfolding(fs, op + LUA_OPUNM, e, &ef)) break; /* FALLTHROUGH */ diff --git a/3rd/lua/ldebug.c b/3rd/lua/ldebug.c index a5df4d61..3e684b0b 100644 --- a/3rd/lua/ldebug.c +++ b/3rd/lua/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.120 2016/03/31 19:01:21 roberto Exp $ +** $Id: ldebug.c,v 2.121 2016/10/19 12:32:10 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -38,7 +38,8 @@ #define ci_func(ci) (clLvalue((ci)->func)) -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); +static const char *funcnamefromcode (lua_State *L, CallInfo *ci, + const char **name); static int currentpc (CallInfo *ci) { @@ -244,6 +245,20 @@ static void collectvalidlines (lua_State *L, Closure *f) { } +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { + if (ci == NULL) /* no 'ci'? */ + return NULL; /* no info */ + else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ + *name = "__gc"; + return "metamethod"; /* report it as such */ + } + /* calling function is a known Lua function? */ + else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) + return funcnamefromcode(L, ci->previous, name); + else return NULL; /* no way to find a name */ +} + + static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, Closure *f, CallInfo *ci) { int status = 1; @@ -274,11 +289,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'n': { - /* calling function is a known Lua function? */ - if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) - ar->namewhat = getfuncname(L, ci->previous, &ar->name); - else - ar->namewhat = NULL; + ar->namewhat = getfuncname(L, ci, &ar->name); if (ar->namewhat == NULL) { ar->namewhat = ""; /* not found */ ar->name = NULL; @@ -471,8 +482,15 @@ static const char *getobjname (Proto *p, int lastpc, int reg, } -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - TMS tm = (TMS)0; /* to avoid warnings */ +/* +** Try to find a name for a function based on the code that called it. +** (Only works when function was called by a Lua function.) +** Returns what the name is (e.g., "for iterator", "method", +** "metamethod") and sets '*name' to point to the name. +*/ +static const char *funcnamefromcode (lua_State *L, CallInfo *ci, + const char **name) { + TMS tm = (TMS)0; /* (initial value avoids warnings) */ Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->sp->code[pc]; /* calling instruction */ @@ -482,13 +500,13 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { } switch (GET_OPCODE(i)) { case OP_CALL: - case OP_TAILCALL: /* get function name */ - return getobjname(p, pc, GETARG_A(i), name); + case OP_TAILCALL: + return getobjname(p, pc, GETARG_A(i), name); /* get function name */ case OP_TFORCALL: { /* for iterator */ *name = "for iterator"; return "for iterator"; } - /* all other instructions can call only through metamethods */ + /* other instructions can do calls through metamethods */ case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: tm = TM_INDEX; break; @@ -509,7 +527,8 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { case OP_EQ: tm = TM_EQ; break; case OP_LT: tm = TM_LT; break; case OP_LE: tm = TM_LE; break; - default: lua_assert(0); /* other instructions cannot call a function */ + default: + return NULL; /* cannot find a reasonable name */ } *name = getstr(G(L)->tmname[tm]); return "metamethod"; diff --git a/3rd/lua/ldo.c b/3rd/lua/ldo.c index a73e4214..955af4ef 100644 --- a/3rd/lua/ldo.c +++ b/3rd/lua/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.151 2015/12/16 16:40:07 roberto Exp $ +** $Id: ldo.c,v 2.157 2016/12/13 15:52:21 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -211,9 +211,9 @@ static int stackinuse (lua_State *L) { CallInfo *ci; StkId lim = L->top; for (ci = L->ci; ci != NULL; ci = ci->previous) { - lua_assert(ci->top <= L->stack_last); if (lim < ci->top) lim = ci->top; } + lua_assert(lim <= L->stack_last); return cast_int(lim - L->stack) + 1; /* part of stack in use */ } @@ -221,16 +221,19 @@ static int stackinuse (lua_State *L) { void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; - if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; - if (L->stacksize > LUAI_MAXSTACK) /* was handling stack overflow? */ + if (goodsize > LUAI_MAXSTACK) + goodsize = LUAI_MAXSTACK; /* respect stack limit */ + if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */ luaE_freeCI(L); /* free all CIs (list grew because of an error) */ else luaE_shrinkCI(L); /* shrink list */ - if (inuse <= LUAI_MAXSTACK && /* not handling stack overflow? */ - goodsize < L->stacksize) /* trying to shrink? */ - luaD_reallocstack(L, goodsize); /* shrink it */ - else - condmovestack(L,,); /* don't change stack (change only for debugging) */ + /* if thread is currently not handling a stack overflow and its + good size is smaller than current size, shrink its stack */ + if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && + goodsize < L->stacksize) + luaD_reallocstack(L, goodsize); + else /* don't change stack */ + condmovestack(L,{},{}); /* (change only for debugging) */ } @@ -322,86 +325,6 @@ static void tryfuncTM (lua_State *L, StkId func) { } - -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) - - -/* macro to check stack size, preserving 'p' */ -#define checkstackp(L,n,p) \ - luaD_checkstackaux(L, n, \ - ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ - luaC_checkGC(L), /* stack grow uses memory */ \ - p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ - - -/* -** Prepares a function call: checks the stack, creates a new CallInfo -** entry, fills in the relevant information, calls hook if needed. -** If function is a C function, does the call, too. (Otherwise, leave -** the execution ('luaV_execute') to the caller, to allow stackless -** calls.) Returns true iff function has been executed (C function). -*/ -int luaD_precall (lua_State *L, StkId func, int nresults) { - lua_CFunction f; - CallInfo *ci; - switch (ttype(func)) { - case LUA_TCCL: /* C closure */ - f = clCvalue(func)->f; - goto Cfunc; - case LUA_TLCF: /* light C function */ - f = fvalue(func); - Cfunc: { - int n; /* number of returns */ - checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); - return 1; - } - case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; - SharedProto *p = clLvalue(func)->p->sp; - int n = cast_int(L->top - func) - 1; /* number of real arguments */ - int fsize = p->maxstacksize; /* frame size */ - checkstackp(L, fsize, func); - if (p->is_vararg != 1) { /* do not use vararg? */ - for (; n < p->numparams; n++) - setnilvalue(L->top++); /* complete missing arguments */ - base = func + 1; - } - else - base = adjust_varargs(L, p, n); - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; - ci->u.l.base = base; - L->top = ci->top = base + fsize; - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; - if (L->hookmask & LUA_MASKCALL) - callhook(L, ci); - return 0; - } - default: { /* not a function */ - checkstackp(L, 1, func); /* ensure space for metamethod */ - tryfuncTM(L, func); /* try to get '__call' metamethod */ - return luaD_precall(L, func, nresults); /* now it must be a function */ - } - } -} - - /* ** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. ** Handle most typical cases (zero results for commands, one result for @@ -468,6 +391,86 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { } + +#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) + + +/* macro to check stack size, preserving 'p' */ +#define checkstackp(L,n,p) \ + luaD_checkstackaux(L, n, \ + ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ + luaC_checkGC(L), /* stack grow uses memory */ \ + p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ + + +/* +** Prepares a function call: checks the stack, creates a new CallInfo +** entry, fills in the relevant information, calls hook if needed. +** If function is a C function, does the call, too. (Otherwise, leave +** the execution ('luaV_execute') to the caller, to allow stackless +** calls.) Returns true iff function has been executed (C function). +*/ +int luaD_precall (lua_State *L, StkId func, int nresults) { + lua_CFunction f; + CallInfo *ci; + switch (ttype(func)) { + case LUA_TCCL: /* C closure */ + f = clCvalue(func)->f; + goto Cfunc; + case LUA_TLCF: /* light C function */ + f = fvalue(func); + Cfunc: { + int n; /* number of returns */ + checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + ci = next_ci(L); /* now 'enter' new function */ + ci->nresults = nresults; + ci->func = func; + ci->top = L->top + LUA_MINSTACK; + lua_assert(ci->top <= L->stack_last); + ci->callstatus = 0; + if (L->hookmask & LUA_MASKCALL) + luaD_hook(L, LUA_HOOKCALL, -1); + lua_unlock(L); + n = (*f)(L); /* do the actual call */ + lua_lock(L); + api_checknelems(L, n); + luaD_poscall(L, ci, L->top - n, n); + return 1; + } + case LUA_TLCL: { /* Lua function: prepare its call */ + StkId base; + SharedProto *p = clLvalue(func)->p->sp; + int n = cast_int(L->top - func) - 1; /* number of real arguments */ + int fsize = p->maxstacksize; /* frame size */ + checkstackp(L, fsize, func); + if (p->is_vararg) + base = adjust_varargs(L, p, n); + else { /* non vararg function */ + for (; n < p->numparams; n++) + setnilvalue(L->top++); /* complete missing arguments */ + base = func + 1; + } + ci = next_ci(L); /* now 'enter' new function */ + ci->nresults = nresults; + ci->func = func; + ci->u.l.base = base; + L->top = ci->top = base + fsize; + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus = CIST_LUA; + if (L->hookmask & LUA_MASKCALL) + callhook(L, ci); + return 0; + } + default: { /* not a function */ + checkstackp(L, 1, func); /* ensure space for metamethod */ + tryfuncTM(L, func); /* try to get '__call' metamethod */ + return luaD_precall(L, func, nresults); /* now it must be a function */ + } + } +} + + /* ** Check appropriate error for stack overflow ("regular" overflow or ** overflow while handling stack overflow). If 'nCalls' is larger than @@ -520,19 +523,17 @@ static void finishCcall (lua_State *L, int status) { /* error status can only happen in a protected call */ lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ - ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ - L->errfunc = ci->u.c.old_errfunc; + ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */ + L->errfunc = ci->u.c.old_errfunc; /* with the same error function */ } /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already handled */ adjustresults(L, ci->nresults); - /* call continuation function */ lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); + n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ lua_lock(L); api_checknelems(L, n); - /* finish 'luaD_precall' */ - luaD_poscall(L, ci, L->top - n, n); + luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ } @@ -595,15 +596,16 @@ static int recover (lua_State *L, int status) { /* -** signal an error in the call to 'resume', not in the execution of the -** coroutine itself. (Such errors should not be handled by any coroutine -** error handler and should not kill the coroutine.) +** Signal an error in the call to 'lua_resume', not in the execution +** of the coroutine itself. (Such errors should not be handled by any +** coroutine error handler and should not kill the coroutine.) */ -static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { - L->top = firstArg; /* remove args from the stack */ +static int resume_error (lua_State *L, const char *msg, int narg) { + L->top -= narg; /* remove args from the stack */ setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ api_incr_top(L); - luaD_throw(L, -1); /* jump back to 'lua_resume' */ + lua_unlock(L); + return LUA_ERRRUN; } @@ -615,22 +617,15 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { ** coroutine. */ static void resume (lua_State *L, void *ud) { - int nCcalls = L->nCcalls; int n = *(cast(int*, ud)); /* number of arguments */ StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; - if (nCcalls >= LUAI_MAXCCALLS) - resume_error(L, "C stack overflow", firstArg); - if (L->status == LUA_OK) { /* may be starting a coroutine */ - if (ci != &L->base_ci) /* not in base level? */ - resume_error(L, "cannot resume non-suspended coroutine", firstArg); - /* coroutine is in base level; start running it */ + if (L->status == LUA_OK) { /* starting a coroutine? */ if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ luaV_execute(L); /* call it */ } - else if (L->status != LUA_YIELD) - resume_error(L, "cannot resume dead coroutine", firstArg); else { /* resuming from previous yield */ + lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ ci->func = restorestack(L, ci->extra); if (isLua(ci)) /* yielded inside a hook? */ @@ -647,7 +642,6 @@ static void resume (lua_State *L, void *ud) { } unroll(L, NULL); /* run continuation */ } - lua_assert(nCcalls == L->nCcalls); } @@ -655,8 +649,16 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { int status; unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); - luai_userstateresume(L, nargs); + if (L->status == LUA_OK) { /* may be starting a coroutine */ + if (L->ci != &L->base_ci) /* not in base level? */ + return resume_error(L, "cannot resume non-suspended coroutine", nargs); + } + else if (L->status != LUA_YIELD) + return resume_error(L, "cannot resume dead coroutine", nargs); L->nCcalls = (from) ? from->nCcalls + 1 : 1; + if (L->nCcalls >= LUAI_MAXCCALLS) + return resume_error(L, "C stack overflow", nargs); + luai_userstateresume(L, nargs); L->nny = 0; /* allow yields */ api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); diff --git a/3rd/lua/lgc.c b/3rd/lua/lgc.c index 44accb59..8a029f79 100644 --- a/3rd/lua/lgc.c +++ b/3rd/lua/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.212 2016/03/31 19:02:03 roberto Exp $ +** $Id: lgc.c,v 2.215 2016/12/22 13:08:50 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -468,7 +468,7 @@ static lu_mem traversetable (global_State *g, Table *h) { else /* not weak */ traversestrongtable(g, h); return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * cast(size_t, sizenode(h)); + sizeof(Node) * cast(size_t, allocsizenode(h)); } static int marksharedproto (global_State *g, SharedProto *f) { @@ -550,7 +550,7 @@ static lu_mem traversethread (global_State *g, lua_State *th) { StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ setnilvalue(o); - /* 'remarkupvals' may have removed thread from 'twups' list */ + /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { th->twups = g->twups; /* link it back to the list */ g->twups = th; @@ -829,7 +829,9 @@ static void GCTM (lua_State *L, int propagateerrors) { setobj2s(L, L->top, tm); /* push finalizer... */ setobj2s(L, L->top + 1, &v); /* ... and its argument */ L->top += 2; /* and (next line) call the finalizer */ + L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); + L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ diff --git a/3rd/lua/linit.c b/3rd/lua/linit.c index 8ce94ccb..afcaf98b 100644 --- a/3rd/lua/linit.c +++ b/3rd/lua/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp $ +** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ @@ -18,10 +18,10 @@ ** open the library, which is already linked to the application. ** For that, do the following code: ** -** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); +** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); ** lua_pushcfunction(L, luaopen_modname); ** lua_setfield(L, -2, modname); -** lua_pop(L, 1); // remove _PRELOAD table +** lua_pop(L, 1); // remove PRELOAD table */ #include "lprefix.h" diff --git a/3rd/lua/liolib.c b/3rd/lua/liolib.c index aa78e593..15684035 100644 --- a/3rd/lua/liolib.c +++ b/3rd/lua/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.149 2016/05/02 14:03:19 roberto Exp $ +** $Id: liolib.c,v 2.151 2016/12/20 18:37:00 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -37,10 +37,11 @@ #endif /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ -#define l_checkmode(mode) \ - (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \ - (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ \ - (strspn(mode, L_MODEEXT) == strlen(mode))) +static int l_checkmode (const char *mode) { + return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && + (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ + (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ +} #endif @@ -618,8 +619,10 @@ static int g_write (lua_State *L, FILE *f, int arg) { if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ int len = lua_isinteger(L, arg) - ? fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg)) - : fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)); + ? fprintf(f, LUA_INTEGER_FMT, + (LUAI_UACINT)lua_tointeger(L, arg)) + : fprintf(f, LUA_NUMBER_FMT, + (LUAI_UACNUMBER)lua_tonumber(L, arg)); status = status && (len > 0); } else { diff --git a/3rd/lua/lmathlib.c b/3rd/lua/lmathlib.c index 94815f12..b7f8baee 100644 --- a/3rd/lua/lmathlib.c +++ b/3rd/lua/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.117 2015/10/02 15:39:23 roberto Exp $ +** $Id: lmathlib.c,v 1.119 2016/12/22 13:08:50 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -184,10 +184,13 @@ static int math_log (lua_State *L) { else { lua_Number base = luaL_checknumber(L, 2); #if !defined(LUA_USE_C89) - if (base == 2.0) res = l_mathop(log2)(x); else + if (base == l_mathop(2.0)) + res = l_mathop(log2)(x); else #endif - if (base == 10.0) res = l_mathop(log10)(x); - else res = l_mathop(log)(x)/l_mathop(log)(base); + if (base == l_mathop(10.0)) + res = l_mathop(log10)(x); + else + res = l_mathop(log)(x)/l_mathop(log)(base); } lua_pushnumber(L, res); return 1; @@ -262,7 +265,7 @@ static int math_random (lua_State *L) { default: return luaL_error(L, "wrong number of arguments"); } /* random integer in the interval [low, up] */ - luaL_argcheck(L, low <= up, 1, "interval is empty"); + luaL_argcheck(L, low <= up, 1, "interval is empty"); luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1, "interval too large"); r *= (double)(up - low) + 1.0; @@ -281,9 +284,9 @@ static int math_randomseed (lua_State *L) { static int math_type (lua_State *L) { if (lua_type(L, 1) == LUA_TNUMBER) { if (lua_isinteger(L, 1)) - lua_pushliteral(L, "integer"); + lua_pushliteral(L, "integer"); else - lua_pushliteral(L, "float"); + lua_pushliteral(L, "float"); } else { luaL_checkany(L, 1); diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index 79119287..930cfa7a 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.127 2015/11/23 11:30:45 roberto Exp $ +** $Id: loadlib.c,v 1.129 2016/12/04 20:17:24 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -25,40 +25,9 @@ /* -** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment -** variables that Lua check to set its paths. -*/ -#if !defined(LUA_PATH_VAR) -#define LUA_PATH_VAR "LUA_PATH" -#endif - -#if !defined(LUA_CPATH_VAR) -#define LUA_CPATH_VAR "LUA_CPATH" -#endif - -#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - -#define LUA_PATHVARVERSION LUA_PATH_VAR LUA_PATHSUFFIX -#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_PATHSUFFIX - -/* -** LUA_PATH_SEP is the character that separates templates in a path. -** LUA_PATH_MARK is the string that marks the substitution points in a -** template. -** LUA_EXEC_DIR in a Windows path is replaced by the executable's -** directory. ** LUA_IGMARK is a mark to ignore all before it when building the ** luaopen_ function name. */ -#if !defined (LUA_PATH_SEP) -#define LUA_PATH_SEP ";" -#endif -#if !defined (LUA_PATH_MARK) -#define LUA_PATH_MARK "?" -#endif -#if !defined (LUA_EXEC_DIR) -#define LUA_EXEC_DIR "!" -#endif #if !defined (LUA_IGMARK) #define LUA_IGMARK "-" #endif @@ -94,8 +63,6 @@ static const int CLIBS = 0; #define LIB_FAIL "open" -#define setprogdir(L) ((void)0) - /* ** system-dependent functions @@ -179,7 +146,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { #include -#undef setprogdir /* ** optional flags for LoadLibraryEx @@ -189,21 +155,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { #endif -static void setprogdir (lua_State *L) { - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff)/sizeof(char); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) - luaL_error(L, "unable to get ModuleFileName"); - else { - *lb = '\0'; - luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - - static void pusherror (lua_State *L) { int error = GetLastError(); char buffer[128]; @@ -520,7 +471,7 @@ static int searcher_Croot (lua_State *L) { static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); + lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ lua_pushfstring(L, "\n\tno field package.preload['%s']", name); return 1; @@ -557,9 +508,9 @@ static void findloader (lua_State *L, const char *name) { static int ll_require (lua_State *L) { const char *name = luaL_checkstring(L, 1); - lua_settop(L, 1); /* _LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, 2, name); /* _LOADED[name] */ + lua_settop(L, 1); /* LOADED table will be at index 2 */ + lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); + lua_getfield(L, 2, name); /* LOADED[name] */ if (lua_toboolean(L, -1)) /* is it there? */ return 1; /* package is already loaded */ /* else must load package */ @@ -569,11 +520,11 @@ static int ll_require (lua_State *L) { lua_insert(L, -2); /* name is 1st argument (before search data) */ lua_call(L, 2, 1); /* run loader to load module */ if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ + lua_setfield(L, 2, name); /* LOADED[name] = returned value */ if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ lua_pushboolean(L, 1); /* use true as result */ lua_pushvalue(L, -1); /* extra copy to be returned */ - lua_setfield(L, 2, name); /* _LOADED[name] = true */ + lua_setfield(L, 2, name); /* LOADED[name] = true */ } return 1; } @@ -666,41 +617,6 @@ static int ll_seeall (lua_State *L) { -/* auxiliary mark (for internal use) */ -#define AUXMARK "\1" - - -/* -** return registry.LUA_NOENV as a boolean -*/ -static int noenv (lua_State *L) { - int b; - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - b = lua_toboolean(L, -1); - lua_pop(L, 1); /* remove value */ - return b; -} - - -static void setpath (lua_State *L, const char *fieldname, const char *envname1, - const char *envname2, const char *def) { - const char *path = getenv(envname1); - if (path == NULL) /* no environment variable? */ - path = getenv(envname2); /* try alternative name */ - if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushstring(L, def); /* use default */ - else { - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, - LUA_PATH_SEP AUXMARK LUA_PATH_SEP); - luaL_gsub(L, path, AUXMARK, def); - lua_remove(L, -2); - } - setprogdir(L); - lua_setfield(L, -2, fieldname); -} - - static const luaL_Reg pk_funcs[] = { {"loadlib", ll_loadlib}, {"searchpath", ll_searchpath}, @@ -764,19 +680,19 @@ LUAMOD_API int luaopen_package (lua_State *L) { createclibstable(L); luaL_newlib(L, pk_funcs); /* create 'package' table */ createsearcherstable(L); - /* set field 'path' */ - setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT); - /* set field 'cpath' */ - setpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, LUA_CPATH_DEFAULT); + lua_pushstring(L, LUA_PATH_DEFAULT); + lua_setfield(L, -2, "path"); /* package.path = default path */ + lua_pushstring(L, LUA_CPATH_DEFAULT); + lua_setfield(L, -2, "cpath"); /* package.cpath = default cpath */ /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); lua_setfield(L, -2, "config"); /* set field 'loaded' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); lua_setfield(L, -2, "loaded"); /* set field 'preload' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); lua_setfield(L, -2, "preload"); lua_pushglobaltable(L); lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ diff --git a/3rd/lua/lobject.c b/3rd/lua/lobject.c index a44b3850..2da76899 100644 --- a/3rd/lua/lobject.c +++ b/3rd/lua/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.111 2016/05/20 14:07:48 roberto Exp $ +** $Id: lobject.c,v 2.113 2016/12/22 13:08:50 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -280,7 +280,7 @@ static const char *l_str2d (const char *s, lua_Number *result) { endptr = l_str2dloc(s, result, mode); /* try to convert */ if (endptr == NULL) { /* failed? may be a different locale */ char buff[L_MAXLENNUM + 1]; - char *pdot = strchr(s, '.'); + const char *pdot = strchr(s, '.'); if (strlen(s) > L_MAXLENNUM || pdot == NULL) return NULL; /* string too long or no dot; fail */ strcpy(buff, s); /* copy string to buffer */ @@ -394,7 +394,7 @@ static void pushstr (lua_State *L, const char *str, size_t l) { /* -** this function handles only '%d', '%c', '%f', '%p', and '%s' +** this function handles only '%d', '%c', '%f', '%p', and '%s' conventional formats, plus Lua-specific '%I' and '%U' */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { diff --git a/3rd/lua/lobject.h b/3rd/lua/lobject.h index 7521b71e..ec6a1f0c 100644 --- a/3rd/lua/lobject.h +++ b/3rd/lua/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.116 2015/11/03 18:33:10 roberto Exp $ +** $Id: lobject.h,v 2.117 2016/08/01 19:51:24 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -402,7 +402,7 @@ typedef struct LocVar { typedef struct SharedProto { lu_byte numparams; /* number of fixed parameters */ - lu_byte is_vararg; /* 2: declared vararg; 1: uses vararg */ + lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ diff --git a/3rd/lua/lopcodes.h b/3rd/lua/lopcodes.h index 864b8e4b..bbc4b619 100644 --- a/3rd/lua/lopcodes.h +++ b/3rd/lua/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.148 2014/10/25 11:50:46 roberto Exp $ +** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -139,7 +139,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ /* gets the index of the constant */ #define INDEXK(r) ((int)(r) & ~BITRK) +#if !defined(MAXINDEXRK) /* (for debugging only) */ #define MAXINDEXRK (BITRK - 1) +#endif /* code a constant index as a RK value */ #define RKASK(x) ((x) | BITRK) diff --git a/3rd/lua/loslib.c b/3rd/lua/loslib.c index a56877e1..5a94eb90 100644 --- a/3rd/lua/loslib.c +++ b/3rd/lua/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.64 2016/04/18 13:06:55 roberto Exp $ +** $Id: loslib.c,v 1.65 2016/07/18 17:58:58 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -30,16 +30,16 @@ */ #if !defined(LUA_STRFTIMEOPTIONS) /* { */ -/* options for ANSI C 89 */ +/* options for ANSI C 89 (only 1-char options) */ #define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%" /* options for ISO C 99 and POSIX */ #define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ - "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" + "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ /* options for Windows */ #define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ - "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" + "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ #if defined(LUA_USE_WINDOWS) #define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN @@ -257,13 +257,13 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { } -static const char *checkoption (lua_State *L, const char *conv, char *buff) { - const char *option; - int oplen = 1; - int convlen = (int)strlen(conv); - for (option = LUA_STRFTIMEOPTIONS; *option != '\0' && oplen <= convlen; option += oplen) { +static const char *checkoption (lua_State *L, const char *conv, + ptrdiff_t convlen, char *buff) { + const char *option = LUA_STRFTIMEOPTIONS; + int oplen = 1; /* length of options being checked */ + for (; *option != '\0' && oplen <= convlen; option += oplen) { if (*option == '|') /* next block? */ - oplen++; /* next length */ + oplen++; /* will check options with next length (+1) */ else if (memcmp(conv, option, oplen) == 0) { /* match? */ memcpy(buff, conv, oplen); /* copy valid option to buffer */ buff[oplen] = '\0'; @@ -281,8 +281,10 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { static int os_date (lua_State *L) { - const char *s = luaL_optstring(L, 1, "%c"); + size_t slen; + const char *s = luaL_optlstring(L, 1, "%c", &slen); time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); + const char *se = s + slen; /* 's' end */ struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); @@ -301,13 +303,14 @@ static int os_date (lua_State *L) { luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); - while (*s) { + while (s < se) { if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); - s = checkoption(L, s + 1, cc + 1); /* copy specifier to 'cc' */ + s++; /* skip '%' */ + s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 9c9fd5cc..83503a06 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.153 2016/05/13 19:10:16 roberto Exp $ +** $Id: lparser.c,v 2.155 2016/08/01 19:51:24 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -769,7 +769,7 @@ static void parlist (LexState *ls) { } case TK_DOTS: { /* param -> '...' */ luaX_next(ls); - f->is_vararg = 2; /* declared vararg */ + f->is_vararg = 1; /* declared vararg */ break; } default: luaX_syntaxerror(ls, " or '...' expected"); @@ -965,7 +965,6 @@ static void simpleexp (LexState *ls, expdesc *v) { FuncState *fs = ls->fs; check_condition(ls, fs->f->sp->is_vararg, "cannot use '...' outside a vararg function"); - fs->f->sp->is_vararg = 1; /* function actually uses vararg */ init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); break; } @@ -1617,7 +1616,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->sp->is_vararg = 2; /* main function is always declared vararg */ + fs->f->sp->is_vararg = 1; /* main function is always declared vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ diff --git a/3rd/lua/lstate.h b/3rd/lua/lstate.h index b3033bee..a469466c 100644 --- a/3rd/lua/lstate.h +++ b/3rd/lua/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.130 2015/12/16 16:39:38 roberto Exp $ +** $Id: lstate.h,v 2.133 2016/12/22 13:08:50 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -23,7 +23,7 @@ ** ** 'allgc': all objects not marked for finalization; ** 'finobj': all objects marked for finalization; -** 'tobefnz': all objects ready to be finalized; +** 'tobefnz': all objects ready to be finalized; ** 'fixedgc': all objects that are not to be collected (currently ** only small strings, such as reserved words). @@ -34,7 +34,7 @@ struct lua_longjmp; /* defined in ldo.c */ /* -** Atomic type (relative to signals) to better ensure that 'lua_sethook' +** Atomic type (relative to signals) to better ensure that 'lua_sethook' ** is thread safe */ #if !defined(l_signalT) @@ -66,7 +66,7 @@ typedef struct stringtable { ** Information about a call. ** When a thread yields, 'func' is adjusted to pretend that the ** top function has only the yielded values in its stack; in that -** case, the actual 'func' value is saved in field 'extra'. +** case, the actual 'func' value is saved in field 'extra'. ** When a function calls another with a continuation, 'extra' keeps ** the function index so that, in case of errors, the continuation ** function can be called with the correct top. @@ -88,7 +88,7 @@ typedef struct CallInfo { } u; ptrdiff_t extra; short nresults; /* expected number of results from this function */ - lu_byte callstatus; + unsigned short callstatus; } CallInfo; @@ -104,6 +104,7 @@ typedef struct CallInfo { #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ #define CIST_LEQ (1<<7) /* using __lt for __le */ +#define CIST_FIN (1<<8) /* call is running a finalizer */ #define isLua(ci) ((ci)->callstatus & CIST_LUA) diff --git a/3rd/lua/lstrlib.c b/3rd/lua/lstrlib.c index 12264f88..c7aa755f 100644 --- a/3rd/lua/lstrlib.c +++ b/3rd/lua/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.251 2016/05/20 14:13:21 roberto Exp $ +** $Id: lstrlib.c,v 1.254 2016/12/22 13:08:50 roberto Exp $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -839,11 +839,12 @@ static lua_Number adddigit (char *buff, int n, lua_Number x) { static int num2straux (char *buff, int sz, lua_Number x) { - if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */ - return l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* equal to '%g' */ + /* if 'inf' or 'NaN', format it like '%g' */ + if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) + return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); else if (x == 0) { /* can be -0... */ /* create "0" or "-0" followed by exponent */ - return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", x); + return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x); } else { int e; @@ -933,7 +934,7 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { static void checkdp (char *buff, int nb) { if (memchr(buff, '.', nb) == NULL) { /* no dot? */ char point = lua_getlocaledecpoint(); /* try locale point */ - char *ppoint = memchr(buff, point, nb); + char *ppoint = (char *)memchr(buff, point, nb); if (ppoint) *ppoint = '.'; /* change it to a dot */ } } @@ -960,7 +961,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { const char *format = (n == LUA_MININTEGER) /* corner case? */ ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */ : LUA_INTEGER_FMT; /* else use default format */ - nb = l_sprintf(buff, MAX_ITEM, format, n); + nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); } luaL_addsize(b, nb); break; @@ -1041,7 +1042,7 @@ static int str_format (lua_State *L) { case 'o': case 'u': case 'x': case 'X': { lua_Integer n = luaL_checkinteger(L, arg); addlenmod(form, LUA_INTEGER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, n); + nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n); break; } case 'a': case 'A': @@ -1051,8 +1052,9 @@ static int str_format (lua_State *L) { break; case 'e': case 'E': case 'f': case 'g': case 'G': { + lua_Number n = luaL_checknumber(L, arg); addlenmod(form, LUA_NUMBER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, luaL_checknumber(L, arg)); + nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n); break; } case 'q': { @@ -1259,7 +1261,7 @@ static KOption getoption (Header *h, const char **fmt, int *size) { ** 'psize' is filled with option's size, 'notoalign' with its ** alignment requirements. ** Local variable 'size' gets the size to be aligned. (Kpadal option -** always gets its full alignment, other options are limited by +** always gets its full alignment, other options are limited by ** the maximum alignment ('maxalign'). Kchar option needs no alignment ** despite its size. */ diff --git a/3rd/lua/ltable.c b/3rd/lua/ltable.c index 7e15b71b..d080189f 100644 --- a/3rd/lua/ltable.c +++ b/3rd/lua/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.117 2015/11/19 19:16:22 roberto Exp $ +** $Id: ltable.c,v 2.118 2016/11/07 12:38:35 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -74,8 +74,6 @@ #define dummynode (&dummynode_) -#define isdummy(n) ((n) == dummynode) - static const Node dummynode_ = { {NILCONSTANT}, /* value */ {{NILCONSTANT, 0}} /* key */ @@ -308,14 +306,14 @@ static void setarrayvector (lua_State *L, Table *t, unsigned int size) { static void setnodevector (lua_State *L, Table *t, unsigned int size) { - int lsize; if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */ - lsize = 0; + t->lsizenode = 0; + t->lastfree = NULL; /* signal that it is using dummy node */ } else { int i; - lsize = luaO_ceillog2(size); + int lsize = luaO_ceillog2(size); if (lsize > MAXHBITS) luaG_runerror(L, "table overflow"); size = twoto(lsize); @@ -326,9 +324,9 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { setnilvalue(wgkey(n)); setnilvalue(gval(n)); } + t->lsizenode = cast_byte(lsize); + t->lastfree = gnode(t, size); /* all positions are free */ } - t->lsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ } @@ -337,7 +335,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize, unsigned int i; int j; unsigned int oldasize = t->sizearray; - int oldhsize = t->lsizenode; + int oldhsize = allocsizenode(t); Node *nold = t->node; /* save old hash ... */ if (nasize > oldasize) /* array part must grow? */ setarrayvector(L, t, nasize); @@ -354,7 +352,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize, luaM_reallocvector(L, t->array, oldasize, nasize, TValue); } /* re-insert elements from hash part */ - for (j = twoto(oldhsize) - 1; j >= 0; j--) { + for (j = oldhsize - 1; j >= 0; j--) { Node *old = nold + j; if (!ttisnil(gval(old))) { /* doesn't need barrier/invalidate cache, as entry was @@ -362,13 +360,13 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize, setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); } } - if (!isdummy(nold)) - luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old hash */ + if (oldhsize > 0) /* not the dummy node? */ + luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */ } void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { - int nsize = isdummy(t->node) ? 0 : sizenode(t); + int nsize = allocsizenode(t); luaH_resize(L, t, nasize, nsize); } @@ -414,7 +412,7 @@ Table *luaH_new (lua_State *L) { void luaH_free (lua_State *L, Table *t) { - if (!isdummy(t->node)) + if (!isdummy(t)) luaM_freearray(L, t->node, cast(size_t, sizenode(t))); luaM_freearray(L, t->array, t->sizearray); luaM_free(L, t); @@ -422,10 +420,12 @@ void luaH_free (lua_State *L, Table *t) { static Node *getfreepos (Table *t) { - while (t->lastfree > t->node) { - t->lastfree--; - if (ttisnil(gkey(t->lastfree))) - return t->lastfree; + if (!isdummy(t)) { + while (t->lastfree > t->node) { + t->lastfree--; + if (ttisnil(gkey(t->lastfree))) + return t->lastfree; + } } return NULL; /* could not find a free place */ } @@ -445,7 +445,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { if (ttisnil(key)) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { lua_Integer k; - if (luaV_tointeger(key, &k, 0)) { /* index is int? */ + if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */ setivalue(&aux, k); key = &aux; /* insert it as an integer */ } @@ -453,7 +453,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { luaG_runerror(L, "table index is NaN"); } mp = mainposition(t, key); - if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ + if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; Node *f = getfreepos(t); /* get a free place */ if (f == NULL) { /* cannot find a free place? */ @@ -461,7 +461,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { /* whatever called 'newkey' takes care of TM cache */ return luaH_set(L, t, key); /* insert key into grown table */ } - lua_assert(!isdummy(f)); + lua_assert(!isdummy(t)); othern = mainposition(t, gkey(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ @@ -651,7 +651,7 @@ int luaH_getn (Table *t) { return i; } /* else must find a boundary in hash part */ - else if (isdummy(t->node)) /* hash part is empty? */ + else if (isdummy(t)) /* hash part is empty? */ return j; /* that is easy... */ else return unbound_search(t, j); } @@ -664,6 +664,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) { return mainposition(t, key); } -int luaH_isdummy (Node *n) { return isdummy(n); } +int luaH_isdummy (const Table *t) { return isdummy(t); } #endif diff --git a/3rd/lua/ltable.h b/3rd/lua/ltable.h index 213cc139..6da9024f 100644 --- a/3rd/lua/ltable.h +++ b/3rd/lua/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.21 2015/11/03 15:47:30 roberto Exp $ +** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -15,7 +15,7 @@ #define gnext(n) ((n)->i_key.nk.next) -/* 'const' to avoid wrong writings that can mess up field 'next' */ +/* 'const' to avoid wrong writings that can mess up field 'next' */ #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) /* @@ -27,6 +27,14 @@ #define invalidateTMcache(t) ((t)->flags = 0) +/* true when 't' is using 'dummynode' as its hash part */ +#define isdummy(t) ((t)->lastfree == NULL) + + +/* allocated size for hash nodes */ +#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) + + /* returns the key, given the value of a table entry */ #define keyfromval(v) \ (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) @@ -51,7 +59,7 @@ LUAI_FUNC int luaH_getn (Table *t); #if defined(LUA_DEBUG) LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (Node *n); +LUAI_FUNC int luaH_isdummy (const Table *t); #endif diff --git a/3rd/lua/ltm.c b/3rd/lua/ltm.c index 4650cc29..14e52578 100644 --- a/3rd/lua/ltm.c +++ b/3rd/lua/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.37 2016/02/26 19:20:15 roberto Exp $ +** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -15,7 +15,7 @@ #include "lua.h" #include "ldebug.h" -#include "ldo.h" +#include "ldo.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index ca45e2e1..9222dd1f 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.226 2015/08/14 19:11:20 roberto Exp $ +** $Id: lua.c,v 1.229 2016/12/22 13:08:50 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -21,6 +21,9 @@ #include "lstring.h" +#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR + + #if !defined(LUA_PROMPT) #define LUA_PROMPT "> " #define LUA_PROMPT2 ">> " @@ -38,8 +41,7 @@ #define LUA_INIT_VAR "LUA_INIT" #endif -#define LUA_INITVARVERSION \ - LUA_INIT_VAR "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR +#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX /* @@ -56,6 +58,8 @@ #elif defined(LUA_USE_WINDOWS) /* }{ */ #include +#include + #define lua_stdin_is_tty() _isatty(_fileno(stdin)) #else /* }{ */ @@ -458,7 +462,7 @@ static int handle_script (lua_State *L, char **argv) { /* ** Traverses all arguments from 'argv', returning a mask with those ** needed before running any Lua code (or an error code if it finds -** any invalid argument). 'first' returns the first not-handled argument +** any invalid argument). 'first' returns the first not-handled argument ** (either the script name or a bad argument in case of error). */ static int collectargs (char **argv, int *first) { @@ -482,7 +486,7 @@ static int collectargs (char **argv, int *first) { args |= has_E; break; case 'i': - args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ + args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ case 'v': if (argv[i][2] != '\0') /* extra characters after 1st? */ return has_error; /* invalid option */ @@ -530,6 +534,89 @@ static int runargs (lua_State *L, char **argv, int n) { } + +/* +** {================================================================== +** Set Paths +** =================================================================== +*/ + +/* +** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment +** variables that Lua check to set its paths. +*/ +#if !defined(LUA_PATH_VAR) +#define LUA_PATH_VAR "LUA_PATH" +#endif + +#if !defined(LUA_CPATH_VAR) +#define LUA_CPATH_VAR "LUA_CPATH" +#endif + +#define LUA_PATHVARVERSION LUA_PATH_VAR LUA_VERSUFFIX +#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_VERSUFFIX + + +#define AUXMARK "\1" /* auxiliary mark */ + + +#if defined(LUA_USE_WINDOWS) + + +/* +** Replace in the path (on the top of the stack) any occurrence +** of LUA_EXEC_DIR with the executable's path. +*/ +static void setprogdir (lua_State *L) { + char buff[MAX_PATH + 1]; + char *lb; + DWORD nsize = sizeof(buff)/sizeof(char); + DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ + if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) + luaL_error(L, "unable to get ModuleFileName"); + else { + *lb = '\0'; /* cut name on the last '\\' to get the path */ + luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); + lua_remove(L, -2); /* remove original string */ + } +} + +#else + +#define setprogdir(L) ((void)0) + +#endif + +/* +** Change a path according to corresponding environment variables +*/ +static void chgpath (lua_State *L, const char *fieldname, + const char *envname1, + const char *envname2, + int noenv) { + const char *path = getenv(envname1); + lua_getglobal(L, LUA_LOADLIBNAME); /* get 'package' table */ + lua_getfield(L, -1, fieldname); /* get original path */ + if (path == NULL) /* no environment variable? */ + path = getenv(envname2); /* try alternative name */ + if (path == NULL || noenv) /* no environment variable? */ + lua_pushvalue(L, -1); /* use original value */ + else { + const char *def = lua_tostring(L, -1); /* default path */ + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ + path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, + LUA_PATH_SEP AUXMARK LUA_PATH_SEP); + luaL_gsub(L, path, AUXMARK, def); + lua_remove(L, -2); /* remove result from 1st 'gsub' */ + } + setprogdir(L); + lua_setfield(L, -3, fieldname); /* set path value */ + lua_pop(L, 2); /* pop 'package' table and original path */ +} + +/* }================================================================== */ + + static int handle_luainit (lua_State *L) { const char *name = "=" LUA_INITVARVERSION; const char *init = getenv(name + 1); @@ -562,11 +649,10 @@ static int pmain (lua_State *L) { } if (args & has_v) /* option '-v'? */ print_version(); - if (args & has_E) { /* option '-E'? */ - lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - } luaL_openlibs(L); /* open standard libraries */ + /* change paths according to env variables */ + chgpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, (args & has_E)); + chgpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, (args & has_E)); createargtable(L, argv, argc, script); /* create table 'arg' */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ diff --git a/3rd/lua/lua.h b/3rd/lua/lua.h index 2707cb31..24a8886d 100644 --- a/3rd/lua/lua.h +++ b/3rd/lua/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.331 2016/05/30 15:53:28 roberto Exp $ +** $Id: lua.h,v 1.332 2016/12/22 15:51:20 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -19,11 +19,11 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "3" #define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "3" +#define LUA_VERSION_RELEASE "4" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2016 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2017 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -467,7 +467,7 @@ LUA_API void (lua_checksig_)(lua_State *L); #define lua_checksig(L) if (skynet_sig_L) { lua_checksig_(L); } /****************************************************************************** -* Copyright (C) 1994-2016 Lua.org, PUC-Rio. +* Copyright (C) 1994-2017 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/3rd/lua/luaconf.h b/3rd/lua/luaconf.h index fd447ccb..f37bea09 100644 --- a/3rd/lua/luaconf.h +++ b/3rd/lua/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.255 2016/05/01 20:06:09 roberto Exp $ +** $Id: luaconf.h,v 1.259 2016/12/22 13:08:50 roberto Exp $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -158,6 +158,18 @@ ** =================================================================== */ +/* +** LUA_PATH_SEP is the character that separates templates in a path. +** LUA_PATH_MARK is the string that marks the substitution points in a +** template. +** LUA_EXEC_DIR in a Windows path is replaced by the executable's +** directory. +*/ +#define LUA_PATH_SEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXEC_DIR "!" + + /* @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for ** Lua libraries. @@ -404,7 +416,7 @@ /* @@ LUA_NUMBER is the floating-point type used by Lua. -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' +@@ LUAI_UACNUMBER is the result of a 'default argument promotion' @@ over a floating number. @@ l_mathlim(x) corrects limit name 'x' to the proper float type ** by prefixing it with one of FLT/DBL/LDBL. @@ -421,7 +433,8 @@ #define l_floor(x) (l_mathop(floor)(x)) -#define lua_number2str(s,sz,n) l_sprintf((s), sz, LUA_NUMBER_FMT, (n)) +#define lua_number2str(s,sz,n) \ + l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) /* @@ lua_numbertointeger converts a float number to an integer, or @@ -498,7 +511,7 @@ ** @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. ** -@@ LUAI_UACINT is the result of an 'usual argument conversion' +@@ LUAI_UACINT is the result of a 'default argument promotion' @@ over a lUA_INTEGER. @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. @@ LUA_INTEGER_FMT is the format for writing integers. @@ -511,10 +524,12 @@ /* The following definitions are good for most cases here */ #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" -#define lua_integer2str(s,sz,n) l_sprintf((s), sz, LUA_INTEGER_FMT, (n)) #define LUAI_UACINT LUA_INTEGER +#define lua_integer2str(s,sz,n) \ + l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) + /* ** use LUAI_UACINT here to avoid problems with promotions (which ** can turn a comparison between unsigneds into a signed comparison) @@ -606,13 +621,14 @@ /* -@@ lua_number2strx converts a float to an hexadecimal numeric string. +@@ lua_number2strx converts a float to an hexadecimal numeric string. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will ** provide its own implementation. */ #if !defined(LUA_USE_C89) -#define lua_number2strx(L,b,sz,f,n) ((void)L, l_sprintf(b,sz,f,n)) +#define lua_number2strx(L,b,sz,f,n) \ + ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) #endif @@ -728,11 +744,11 @@ /* @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. ** CHANGE it if it uses too much C-stack space. (For long double, -** 'string.format("%.99f", 1e4932)' needs ~5030 bytes, so a +** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a ** smaller buffer would force a memory allocation for each call to ** 'string.format'.) */ -#if defined(LUA_FLOAT_LONGDOUBLE) +#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE #define LUAL_BUFFERSIZE 8192 #else #define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) diff --git a/3rd/lua/lutf8lib.c b/3rd/lua/lutf8lib.c index 9042582d..de9e3dcd 100644 --- a/3rd/lua/lutf8lib.c +++ b/3rd/lua/lutf8lib.c @@ -1,5 +1,5 @@ /* -** $Id: lutf8lib.c,v 1.15 2015/03/28 19:16:55 roberto Exp $ +** $Id: lutf8lib.c,v 1.16 2016/12/22 13:08:50 roberto Exp $ ** Standard library for UTF-8 manipulation ** See Copyright Notice in lua.h */ @@ -194,7 +194,7 @@ static int byteoffset (lua_State *L) { lua_pushinteger(L, posi + 1); else /* no such character */ lua_pushnil(L); - return 1; + return 1; } diff --git a/3rd/lua/lvm.h b/3rd/lua/lvm.h index bcf52d20..422f8719 100644 --- a/3rd/lua/lvm.h +++ b/3rd/lua/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.40 2016/01/05 16:07:21 roberto Exp $ +** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -90,7 +90,7 @@ #define luaV_settable(L,t,k,v) { const TValue *slot; \ if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ luaV_finishset(L,t,k,v,slot); } - + LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); From 8bc018fe200768fb0f0135878cfcadf588a7af7d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 3 Jan 2017 10:51:17 +0800 Subject: [PATCH 659/729] update to jemalloc 4.4.0 --- 3rd/jemalloc | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index 3de03533..f1f76357 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit 3de035335255d553bdb344c32ffdb603816195d8 +Subproject commit f1f76357313e7dcad7262f17a48ff0a2e005fcdc diff --git a/Makefile b/Makefile index e0f0b14d..08259a63 100644 --- a/Makefile +++ b/Makefile @@ -135,7 +135,7 @@ clean : cleanall: clean ifneq (,$(wildcard 3rd/jemalloc/Makefile)) - cd 3rd/jemalloc && $(MAKE) clean + cd 3rd/jemalloc && $(MAKE) clean && rm Makefile endif cd 3rd/lua && $(MAKE) clean rm -f $(LUA_STATICLIB) From 9e90ee2dde263b1cfb31a75cdce469d9334bf69c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 13 Jan 2017 10:59:10 +0800 Subject: [PATCH 660/729] update to 5.3.4 rc3 --- 3rd/lua/loadlib.c | 97 ++++++++++++++++++++++++++++++++++++++++++++--- 3rd/lua/lua.c | 93 +++------------------------------------------ 3rd/lua/lualib.h | 5 ++- 3 files changed, 101 insertions(+), 94 deletions(-) diff --git a/3rd/lua/loadlib.c b/3rd/lua/loadlib.c index 930cfa7a..4791e748 100644 --- a/3rd/lua/loadlib.c +++ b/3rd/lua/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.129 2016/12/04 20:17:24 roberto Exp $ +** $Id: loadlib.c,v 1.130 2017/01/12 17:14:26 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -64,6 +64,9 @@ static const int CLIBS = 0; #define LIB_FAIL "open" +#define setprogdir(L) ((void)0) + + /* ** system-dependent functions */ @@ -155,6 +158,30 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { #endif +#undef setprogdir + + +/* +** Replace in the path (on the top of the stack) any occurrence +** of LUA_EXEC_DIR with the executable's path. +*/ +static void setprogdir (lua_State *L) { + char buff[MAX_PATH + 1]; + char *lb; + DWORD nsize = sizeof(buff)/sizeof(char); + DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ + if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) + luaL_error(L, "unable to get ModuleFileName"); + else { + *lb = '\0'; /* cut name on the last '\\' to get the path */ + luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); + lua_remove(L, -2); /* remove original string */ + } +} + + + + static void pusherror (lua_State *L) { int error = GetLastError(); char buffer[128]; @@ -223,6 +250,67 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { #endif /* } */ +/* +** {================================================================== +** Set Paths +** =================================================================== +*/ + +/* +** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment +** variables that Lua check to set its paths. +*/ +#if !defined(LUA_PATH_VAR) +#define LUA_PATH_VAR "LUA_PATH" +#endif + +#if !defined(LUA_CPATH_VAR) +#define LUA_CPATH_VAR "LUA_CPATH" +#endif + + +#define AUXMARK "\1" /* auxiliary mark */ + + +/* +** return registry.LUA_NOENV as a boolean +*/ +static int noenv (lua_State *L) { + int b; + lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); + b = lua_toboolean(L, -1); + lua_pop(L, 1); /* remove value */ + return b; +} + + +/* +** Set a path +*/ +static void setpath (lua_State *L, const char *fieldname, + const char *envname, + const char *dft) { + const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); + const char *path = getenv(nver); /* use versioned name */ + if (path == NULL) /* no environment variable? */ + path = getenv(envname); /* try unversioned name */ + if (path == NULL || noenv(L)) /* no environment variable? */ + lua_pushstring(L, dft); /* use default */ + else { + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ + path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, + LUA_PATH_SEP AUXMARK LUA_PATH_SEP); + luaL_gsub(L, path, AUXMARK, dft); + lua_remove(L, -2); /* remove result from 1st 'gsub' */ + } + setprogdir(L); + lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ + lua_pop(L, 1); /* pop versioned variable name */ +} + +/* }================================================================== */ + + /* ** return registry.CLIBS[path] */ @@ -680,10 +768,9 @@ LUAMOD_API int luaopen_package (lua_State *L) { createclibstable(L); luaL_newlib(L, pk_funcs); /* create 'package' table */ createsearcherstable(L); - lua_pushstring(L, LUA_PATH_DEFAULT); - lua_setfield(L, -2, "path"); /* package.path = default path */ - lua_pushstring(L, LUA_CPATH_DEFAULT); - lua_setfield(L, -2, "cpath"); /* package.cpath = default cpath */ + /* set paths */ + setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); + setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); diff --git a/3rd/lua/lua.c b/3rd/lua/lua.c index 9222dd1f..febd9876 100644 --- a/3rd/lua/lua.c +++ b/3rd/lua/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.229 2016/12/22 13:08:50 roberto Exp $ +** $Id: lua.c,v 1.230 2017/01/12 17:14:26 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -21,8 +21,6 @@ #include "lstring.h" -#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - #if !defined(LUA_PROMPT) #define LUA_PROMPT "> " @@ -535,88 +533,6 @@ static int runargs (lua_State *L, char **argv, int n) { -/* -** {================================================================== -** Set Paths -** =================================================================== -*/ - -/* -** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment -** variables that Lua check to set its paths. -*/ -#if !defined(LUA_PATH_VAR) -#define LUA_PATH_VAR "LUA_PATH" -#endif - -#if !defined(LUA_CPATH_VAR) -#define LUA_CPATH_VAR "LUA_CPATH" -#endif - -#define LUA_PATHVARVERSION LUA_PATH_VAR LUA_VERSUFFIX -#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_VERSUFFIX - - -#define AUXMARK "\1" /* auxiliary mark */ - - -#if defined(LUA_USE_WINDOWS) - - -/* -** Replace in the path (on the top of the stack) any occurrence -** of LUA_EXEC_DIR with the executable's path. -*/ -static void setprogdir (lua_State *L) { - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff)/sizeof(char); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) - luaL_error(L, "unable to get ModuleFileName"); - else { - *lb = '\0'; /* cut name on the last '\\' to get the path */ - luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - -#else - -#define setprogdir(L) ((void)0) - -#endif - -/* -** Change a path according to corresponding environment variables -*/ -static void chgpath (lua_State *L, const char *fieldname, - const char *envname1, - const char *envname2, - int noenv) { - const char *path = getenv(envname1); - lua_getglobal(L, LUA_LOADLIBNAME); /* get 'package' table */ - lua_getfield(L, -1, fieldname); /* get original path */ - if (path == NULL) /* no environment variable? */ - path = getenv(envname2); /* try alternative name */ - if (path == NULL || noenv) /* no environment variable? */ - lua_pushvalue(L, -1); /* use original value */ - else { - const char *def = lua_tostring(L, -1); /* default path */ - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, - LUA_PATH_SEP AUXMARK LUA_PATH_SEP); - luaL_gsub(L, path, AUXMARK, def); - lua_remove(L, -2); /* remove result from 1st 'gsub' */ - } - setprogdir(L); - lua_setfield(L, -3, fieldname); /* set path value */ - lua_pop(L, 2); /* pop 'package' table and original path */ -} - -/* }================================================================== */ - - static int handle_luainit (lua_State *L) { const char *name = "=" LUA_INITVARVERSION; const char *init = getenv(name + 1); @@ -649,10 +565,11 @@ static int pmain (lua_State *L) { } if (args & has_v) /* option '-v'? */ print_version(); + if (args & has_E) { /* option '-E'? */ + lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ + lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); + } luaL_openlibs(L); /* open standard libraries */ - /* change paths according to env variables */ - chgpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, (args & has_E)); - chgpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, (args & has_E)); createargtable(L, argv, argc, script); /* create table 'arg' */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ diff --git a/3rd/lua/lualib.h b/3rd/lua/lualib.h index c43f8a2d..54ae8df7 100644 --- a/3rd/lua/lualib.h +++ b/3rd/lua/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp $ +** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp $ ** Lua standard libraries ** See Copyright Notice in lua.h */ @@ -11,6 +11,9 @@ #include "lua.h" +/* version suffix for environment variable names */ +#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR + LUAMOD_API int (luaopen_base) (lua_State *L); From af6b5609de16ae39e85ecf04b9ff54847483f53c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 23 Jan 2017 16:50:34 +0800 Subject: [PATCH 661/729] add checksig in OP_FORLOOP, see issue #568 --- 3rd/lua/lvm.c | 1 + 1 file changed, 1 insertion(+) diff --git a/3rd/lua/lvm.c b/3rd/lua/lvm.c index c5fd165b..eeaa1008 100644 --- a/3rd/lua/lvm.c +++ b/3rd/lua/lvm.c @@ -1201,6 +1201,7 @@ void luaV_execute (lua_State *L) { } } vmcase(OP_FORLOOP) { + lua_checksig(L); if (ttisinteger(ra)) { /* integer loop? */ lua_Integer step = ivalue(ra + 2); lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */ From 2e919505f18969a88ed6fa07a3b0d3a89e970d78 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 9 Feb 2017 17:26:48 +0800 Subject: [PATCH 662/729] bson check utf8 string --- lualib-src/lua-bson.c | 76 +++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 680a4f24..817df780 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -198,10 +198,50 @@ write_length(struct bson *b, int32_t v, int off) { b->ptr[off++] = (uv >> 24)&0xff; } +#define MAXUNICODE 0x10FFFF + +static int +utf8_copy(const char *s, char *d, size_t limit) { + static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; + unsigned int c = s[0]; + unsigned int res = 0; + if (limit < 1) + return 0; + d[0] = s[0]; + if (c < 0x80) { + return 1; + } else { + int count = 0; + while (c & 0x40) { + int cc = s[++count]; + if (limit <= count || (cc & 0xC0) != 0x80) + return 0; + d[count] = s[count]; + res = (res << 6) | (cc & 0x3F); + c <<= 1; + } + res |= ((c & 0x7F) << (count * 5)); + if (count > 3 || res > MAXUNICODE || res <= limits[count]) + return 0; + return count+1; + } +} + static void -write_string(struct bson *b, const char *key, size_t sz) { +write_string(struct bson *b, lua_State *L, const char *key, size_t sz) { bson_reserve(b,sz+1); - memcpy(b->ptr + b->size, key, sz); + char *dst = (char *)(b->ptr + b->size); + const char *src = key; + size_t n = sz; + while(n > 0) { + int c = utf8_copy(src, dst, n); + if (c == 0) { + luaL_error(L, "Invalid utf8 string"); + } + src += c; + dst += c; + n -= c; + } b->ptr[b->size+sz] = '\0'; b->size+=sz+1; } @@ -239,9 +279,9 @@ write_double(struct bson *b, lua_Number d) { } static inline void -append_key(struct bson *bs, int type, const char *key, size_t sz) { +append_key(struct bson *bs, lua_State *L, int type, const char *key, size_t sz) { write_byte(bs, type); - write_string(bs, key, sz); + write_string(bs, L, key, sz); } static inline int @@ -254,15 +294,15 @@ append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { if (lua_isinteger(L, -1)) { int64_t i = lua_tointeger(L, -1); if (is_32bit(i)) { - append_key(bs, BSON_INT32, key, sz); + append_key(bs, L, BSON_INT32, key, sz); write_int32(bs, i); } else { - append_key(bs, BSON_INT64, key, sz); + append_key(bs, L, BSON_INT64, key, sz); write_int64(bs, i); } } else { lua_Number d = lua_tonumber(L,-1); - append_key(bs, BSON_REAL, key, sz); + append_key(bs, L, BSON_REAL, key, sz); write_double(bs, d); } } @@ -286,7 +326,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) append_number(bs, L, key, sz); break; case LUA_TUSERDATA: { - append_key(bs, BSON_DOCUMENT, key, sz); + append_key(bs, L, BSON_DOCUMENT, key, sz); int32_t * doc = lua_touserdata(L,-1); int32_t sz = *doc; bson_reserve(bs,sz); @@ -299,7 +339,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) const char * str = lua_tolstring(L,-1,&len); if (len > 1 && str[0]==0) { int subt = (uint8_t)str[1]; - append_key(bs, subt, key, sz); + append_key(bs, L, subt, key, sz); switch(subt) { case BSON_BINARY: write_binary(bs, str+2, len-2); @@ -345,8 +385,8 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) break; } } - write_string(bs, str, len-i-1); - write_string(bs, str + len-i, i); + write_string(bs, L, str, len-i-1); + write_string(bs, L, str + len-i, i); break; } case BSON_MINKEY: @@ -359,9 +399,9 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) } else { size_t len; const char * str = lua_tolstring(L,-1,&len); - append_key(bs, BSON_STRING, key, sz); + append_key(bs, L, BSON_STRING, key, sz); int off = reserve_length(bs); - write_string(bs, str, len); + write_string(bs, L, str, len); write_length(bs, len+1, off); } break; @@ -370,7 +410,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) append_table(bs, L, key, sz, depth+1); break; case LUA_TBOOLEAN: - append_key(bs, BSON_BOOLEAN, key, sz); + append_key(bs, L, BSON_BOOLEAN, key, sz); write_byte(bs, lua_toboolean(L,-1)); break; default: @@ -491,16 +531,16 @@ append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int dept } size_t len = lua_tointeger(L, -1); lua_pop(L, 1); - append_key(bs, BSON_ARRAY, key, sz); + append_key(bs, L, BSON_ARRAY, key, sz); pack_array(L, bs, depth, len); } else if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { - append_key(bs, BSON_DOCUMENT, key, sz); + append_key(bs, L, BSON_DOCUMENT, key, sz); pack_meta_dict(L, bs, depth); } else if (is_rawarray(L)) { - append_key(bs, BSON_ARRAY, key, sz); + append_key(bs, L, BSON_ARRAY, key, sz); pack_array(L, bs, depth, lua_rawlen(L, -1)); } else { - append_key(bs, BSON_DOCUMENT, key, sz); + append_key(bs, L, BSON_DOCUMENT, key, sz); pack_simple_dict(L, bs, depth); } } From 5c508e45a7b7f7c717384fc4926f2475993bef41 Mon Sep 17 00:00:00 2001 From: hqwrong Date: Thu, 20 Oct 2016 09:52:52 +0800 Subject: [PATCH 663/729] pcall->xpcall --- lualib/snax/interface.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index 740f09d9..5efb7410 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -84,7 +84,7 @@ return function (name , G, loader) end setmetatable(G, { __index = env , __newindex = init_system }) - local ok, err = pcall(mainfunc) + local ok, err = xpcall(mainfunc, debug.traceback) setmetatable(G, nil) assert(ok,err) From d637d720db90f6ba869c6a1ebbbc04548087d8d9 Mon Sep 17 00:00:00 2001 From: hqwrong Date: Thu, 3 Nov 2016 14:55:42 +0800 Subject: [PATCH 664/729] snax loader --- lualib/snax/interface.lua | 43 ++++++++++++++++++--------------------- service/snaxd.lua | 4 +++- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/lualib/snax/interface.lua b/lualib/snax/interface.lua index 5efb7410..e8c8d596 100644 --- a/lualib/snax/interface.lua +++ b/lualib/snax/interface.lua @@ -1,8 +1,24 @@ local skynet = require "skynet" +local function dft_loader(path, name, G) + local errlist = {} + + for pat in string.gmatch(path,"[^;]+") do + local filename = string.gsub(pat, "?", name) + local f , err = loadfile(filename, "bt", G) + if f then + return f, pat + else + table.insert(errlist, err) + end + end + + error(table.concat(errlist, "\n")) +end + return function (name , G, loader) - loader = loader or loadfile - local mainfunc + loader = loader or dft_loader + local mainfunc local function func_id(id, group) local tmp = {} @@ -61,27 +77,8 @@ return function (name , G, loader) local pattern - do - local path = assert(skynet.getenv "snax" , "please set snax in config file") - - local errlist = {} - - for pat in string.gmatch(path,"[^;]+") do - local filename = string.gsub(pat, "?", name) - local f , err = loader(filename, "bt", G) - if f then - pattern = pat - mainfunc = f - break - else - table.insert(errlist, err) - end - end - - if mainfunc == nil then - error(table.concat(errlist, "\n")) - end - end + local path = assert(skynet.getenv "snax" , "please set snax in config file") + mainfunc, pattern = loader(path, name, G) setmetatable(G, { __index = env , __newindex = init_system }) local ok, err = xpcall(mainfunc, debug.traceback) diff --git a/service/snaxd.lua b/service/snaxd.lua index 1b8ddee6..5aaa3353 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -5,7 +5,9 @@ local profile = require "profile" local snax = require "snax" local snax_name = tostring(...) -local func, pattern = snax_interface(snax_name, _ENV) +local loaderpath = skynet.getenv"snax_loader" +local loader = loaderpath and assert(dofile(loaderpath)) or require"snax.loader" +local func, pattern = snax_interface(snax_name, _ENV, loader) local snax_path = pattern:sub(1,pattern:find("?", 1, true)-1) .. snax_name .. "/" package.path = snax_path .. "?.lua;" .. package.path From 6fd1072fde3f739d1d0a402b093857f1b325c12d Mon Sep 17 00:00:00 2001 From: hqwrong Date: Fri, 30 Dec 2016 15:23:40 +0800 Subject: [PATCH 665/729] fix snax hotfix --- lualib/snax/hotfix.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lualib/snax/hotfix.lua b/lualib/snax/hotfix.lua index f56a5072..3262dafd 100644 --- a/lualib/snax/hotfix.lua +++ b/lualib/snax/hotfix.lua @@ -53,8 +53,8 @@ local function collect_all_uv(funcs) end local function loader(source) - return function (filename, ...) - return load(source, "=patch", ...) + return function (path, name, G) + return load(source, "=patch", "bt", G) end end From f25e396181d06e1b2eea006abaed11db8d49cf5a Mon Sep 17 00:00:00 2001 From: hqwrong Date: Wed, 8 Feb 2017 17:50:31 +0800 Subject: [PATCH 666/729] 1. snax hotfix recursively patch func 2. load hotfix source with _ENV instead of empty table --- lualib/snax/hotfix.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lualib/snax/hotfix.lua b/lualib/snax/hotfix.lua index 3262dafd..a73d680e 100644 --- a/lualib/snax/hotfix.lua +++ b/lualib/snax/hotfix.lua @@ -68,10 +68,10 @@ local function find_func(funcs, group , name) end local dummy_env = {} +for k,v in pairs(_ENV) do dummy_env[k] = v end -local function patch_func(funcs, global, group, name, f) - local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name)) - local i = 1 +local function _patch(global, f) + local i = 1 while true do local name, value = debug.getupvalue(f, i) if name == nil then @@ -81,9 +81,18 @@ local function patch_func(funcs, global, group, name, f) if old_uv then debug.upvaluejoin(f, i, old_uv.func, old_uv.index) end + else + if type(value) == "function" then + _patch(global, value) + end end i = i + 1 end +end + +local function patch_func(funcs, global, group, name, f) + local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name)) + _patch(global, f) desc[4] = f end From 5c831e24659e2cc3f519187716ee1efe5defddaa Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Feb 2017 11:48:31 +0800 Subject: [PATCH 667/729] fix issue #574 --- service/snaxd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/snaxd.lua b/service/snaxd.lua index d69c8118..429d6876 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -6,7 +6,7 @@ local snax = require "snax" local snax_name = tostring(...) local loaderpath = skynet.getenv"snax_loader" -local loader = loaderpath and assert(dofile(loaderpath)) or require"snax.loader" +local loader = loaderpath and assert(dofile(loaderpath)) local func, pattern = snax_interface(snax_name, _ENV, loader) local snax_path = pattern:sub(1,pattern:find("?", 1, true)-1) .. snax_name .. "/" package.path = snax_path .. "?.lua;" .. package.path From 2b55bc57a49ceba419f6bb5f79338a73b124b65b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 2 Mar 2017 14:21:56 +0800 Subject: [PATCH 668/729] do not response send, may fix issue #578 --- lualib/skynet.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 4cefc893..7b4c466a 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -211,7 +211,8 @@ function suspend(co, result, command, param, size) end local ret - if not dead_service[co_address] then + -- do not response when session == 0 (send) + if co_session ~= 0 and not dead_service[co_address] then if ok then ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, f(...)) ~= nil if not ret then From ca290355faa5ef31d293c4646c51c5e0f3bc403f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 3 Mar 2017 10:52:25 +0800 Subject: [PATCH 669/729] add assert --- lualib/skynet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 7b4c466a..202e4964 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -34,7 +34,7 @@ skynet.cache = require "skynet.codecache" function skynet.register_protocol(class) local name = class.name local id = class.id - assert(proto[name] == nil) + assert(proto[name] == nil and proto[id] == nil) assert(type(name) == "string" and type(id) == "number" and id >=0 and id <=255) proto[name] = class proto[id] = class From c6cbc2f6b64e37fadbf9d5afc357afbaf0fdf344 Mon Sep 17 00:00:00 2001 From: footer Date: Mon, 6 Mar 2017 14:47:03 +0800 Subject: [PATCH 670/729] nice error log in skynet coroutine --- lualib/skynet/coroutine.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua index b9280894..33bd1faf 100644 --- a/lualib/skynet/coroutine.lua +++ b/lualib/skynet/coroutine.lua @@ -109,11 +109,11 @@ end do -- begin skynetco.wrap - local function wrap_co(ok, ...) + local function wrap_co(co, ok, ...) if ok then return ... else - error(...) + error(debug.traceback(co), 2) end end @@ -122,7 +122,7 @@ function skynetco.wrap(f) return f(...) end) return function(...) - return wrap_co(skynetco.resume(co, ...)) + return wrap_co(co, skynetco.resume(co, ...)) end end From feab147563e7331868b3d33951ef91abb3218d3e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 10 Mar 2017 14:06:53 +0800 Subject: [PATCH 671/729] revert, see issue #579 --- lualib/skynet/coroutine.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/coroutine.lua b/lualib/skynet/coroutine.lua index 33bd1faf..b9280894 100644 --- a/lualib/skynet/coroutine.lua +++ b/lualib/skynet/coroutine.lua @@ -109,11 +109,11 @@ end do -- begin skynetco.wrap - local function wrap_co(co, ok, ...) + local function wrap_co(ok, ...) if ok then return ... else - error(debug.traceback(co), 2) + error(...) end end @@ -122,7 +122,7 @@ function skynetco.wrap(f) return f(...) end) return function(...) - return wrap_co(co, skynetco.resume(co, ...)) + return wrap_co(skynetco.resume(co, ...)) end end From cb19ab54e928fdd0fd6b47badecb7825942b808e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Mar 2017 17:01:41 +0800 Subject: [PATCH 672/729] keep skynet.wakeup order, see issue #587 --- lualib/skynet.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 202e4964..a40eff36 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -5,6 +5,7 @@ local coroutine = coroutine local assert = assert local pairs = pairs local pcall = pcall +local table = table local profile = require "profile" @@ -46,7 +47,7 @@ local session_coroutine_address = {} local session_response = {} local unresponse = {} -local wakeup_session = {} +local wakeup_queue = {} local sleep_session = {} local watching_service = {} @@ -116,9 +117,8 @@ local function co_create(f) end local function dispatch_wakeup() - local co = next(wakeup_session) + local co = table.remove(wakeup_queue,1) if co then - wakeup_session[co] = nil local session = sleep_session[co] if session then session_id_coroutine[session] = "BREAK" @@ -414,8 +414,8 @@ function skynet.retpack(...) end function skynet.wakeup(co) - if sleep_session[co] and wakeup_session[co] == nil then - wakeup_session[co] = true + if sleep_session[co] then + table.insert(wakeup_queue, co) return true end end From 1cdc0d75589d38b22ba964a0090b1fbf0b881b49 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Mar 2017 17:24:39 +0800 Subject: [PATCH 673/729] add cluster.reload(config) --- examples/cluster1.lua | 5 +++++ examples/config.c1 | 3 ++- lualib/cluster.lua | 4 ++-- service/clusterd.lua | 20 ++++++++++++-------- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 0eca6176..70d20044 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -3,6 +3,11 @@ local cluster = require "cluster" local snax = require "snax" skynet.start(function() + cluster.reload { + db = "127.0.0.1:2528", + db2 = "127.0.0.1:2529", + } + local sdb = skynet.newservice("simpledb") -- register name "sdb" for simpledb, you can use cluster.query() later. -- See cluster2.lua diff --git a/examples/config.c1 b/examples/config.c1 index 436859d4..0ebb4e9c 100644 --- a/examples/config.c1 +++ b/examples/config.c1 @@ -6,5 +6,6 @@ bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" -cluster = "./examples/clustername.lua" +-- use cluster.reload instead, see cluster1.lua +-- cluster = "./examples/clustername.lua" snax = "./test/?.lua" diff --git a/lualib/cluster.lua b/lualib/cluster.lua index 9883227f..3b180753 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -21,8 +21,8 @@ function cluster.open(port) end end -function cluster.reload() - skynet.call(clusterd, "lua", "reload") +function cluster.reload(config) + skynet.call(clusterd, "lua", "reload", config) end function cluster.proxy(node, name) diff --git a/service/clusterd.lua b/service/clusterd.lua index 22d5e9f0..20c43ee7 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -29,12 +29,16 @@ end local node_channel = setmetatable({}, { __index = open_channel }) -local function loadconfig() - local f = assert(io.open(config_name)) - local source = f:read "*a" - f:close() - local tmp = {} - assert(load(source, "@"..config_name, "t", tmp))() +local function loadconfig(tmp) + if tmp == nil then + tmp = {} + if config_name then + local f = assert(io.open(config_name)) + local source = f:read "*a" + f:close() + assert(load(source, "@"..config_name, "t", tmp))() + end + end for name,address in pairs(tmp) do assert(type(address) == "string") if node_address[name] ~= address then @@ -47,8 +51,8 @@ local function loadconfig() end end -function command.reload() - loadconfig() +function command.reload(source, config) + loadconfig(config) skynet.ret(skynet.pack(nil)) end From 48034ae22ed0acbc282a57ccb5d2738881ef4d41 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 14 Mar 2017 20:56:49 +0800 Subject: [PATCH 674/729] merge sproto --- lualib-src/sproto/lsproto.c | 27 +++++++++---- lualib-src/sproto/sproto.c | 81 +++++++++++++++++++++++++++---------- lualib-src/sproto/sproto.h | 6 +++ lualib/sprotoparser.lua | 22 ++++++++-- 4 files changed, 103 insertions(+), 33 deletions(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c7708fed..3eb32163 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -166,10 +166,16 @@ encode(const struct sproto_arg *args) { lua_Integer v; lua_Integer vh; int isnum; - v = lua_tointegerx(L, -1, &isnum); - if(!isnum) { - return luaL_error(L, ".%s[%d] is not an integer (Is a %s)", - args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + if (args->extra) { + // It's decimal. + lua_Number vn = lua_tonumber(L, -1); + v = (lua_Integer)(vn * args->extra + 0.5); + } else { + v = lua_tointegerx(L, -1, &isnum); + if(!isnum) { + return luaL_error(L, ".%s[%d] is not an integer (Is a %s)", + args->tagname, args->index, lua_typename(L, lua_type(L, -1))); + } } lua_pop(L,1); // notice: in lua 5.2, lua_Integer maybe 52bit @@ -332,8 +338,15 @@ decode(const struct sproto_arg *args) { switch (args->type) { case SPROTO_TINTEGER: { // notice: in lua 5.2, 52bit integer support (not 64) - lua_Integer v = *(uint64_t*)args->value; - lua_pushinteger(L, v); + if (args->extra) { + lua_Integer v = *(uint64_t*)args->value; + lua_Number vn = (lua_Number)v; + vn /= args->extra; + lua_pushnumber(L, vn); + } else { + lua_Integer v = *(uint64_t*)args->value; + lua_pushinteger(L, v); + } break; } case SPROTO_TBOOLEAN: { @@ -660,7 +673,7 @@ ldefault(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_sproto_core(lua_State *L) { #ifdef luaL_checkversion luaL_checkversion(L); diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 9b23af49..416f1006 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -18,6 +18,7 @@ struct field { const char * name; struct sproto_type * st; int key; + int extra; }; struct sproto_type { @@ -177,6 +178,18 @@ import_string(struct sproto *s, const uint8_t * stream) { return buffer; } +static int +calc_pow(int base, int n) { + int r; + if (n == 0) + return 1; + r = calc_pow(base * base , n / 2); + if (n&1) { + r *= base; + } + return r; +} + static const uint8_t * import_field(struct sproto *s, struct field *f, const uint8_t * stream) { uint32_t sz; @@ -190,6 +203,7 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { f->name = NULL; f->st = NULL; f->key = -1; + f->extra = 0; sz = todword(stream); stream += SIZEOF_LENGTH; @@ -222,12 +236,18 @@ import_field(struct sproto *s, struct field *f, const uint8_t * stream) { f->type = value; break; case 2: // type index - if (value >= s->type_n) - return NULL; // invalid type index - if (f->type >= 0) - return NULL; - f->type = SPROTO_TSTRUCT; - f->st = &s->type[value]; + if (f->type == SPROTO_TINTEGER) { + f->extra = calc_pow(10, value); + } else if (f->type == SPROTO_TSTRING) { + f->extra = value; // string if 0 ; binary is 1 + } else { + if (value >= s->type_n) + return NULL; // invalid type index + if (f->type >= 0) + return NULL; + f->type = SPROTO_TSTRUCT; + f->st = &s->type[value]; + } break; case 3: // tag f->tag = value; @@ -463,11 +483,6 @@ sproto_release(struct sproto * s) { void sproto_dump(struct sproto *s) { int i,j; - static const char * buildin[] = { - "integer", - "boolean", - "string", - }; printf("=== %d types ===\n", s->type_n); for (i=0;itype_n;i++) { struct sproto_type *t = &s->type[i]; @@ -476,25 +491,45 @@ sproto_dump(struct sproto *s) { char array[2] = { 0, 0 }; const char * type_name = NULL; struct field *f = &t->f[j]; + int type = f->type & ~SPROTO_TARRAY; if (f->type & SPROTO_TARRAY) { array[0] = '*'; } else { array[0] = 0; } - { - int t = f->type & ~SPROTO_TARRAY; - if (t == SPROTO_TSTRUCT) { - type_name = f->st->name; - } else { - assert(tst->name; + } else { + switch(type) { + case SPROTO_TINTEGER: + if (f->extra) { + type_name = "decimal"; + } else { + type_name = "integer"; + } + break; + case SPROTO_TBOOLEAN: + type_name = "boolean"; + break; + case SPROTO_TSTRING: + if (f->extra == SPROTO_TSTRING_BINARY) + type_name = "binary"; + else + type_name = "string"; + break; + default: + type_name = "invalid"; + break; } } - if (f->key >= 0) { - printf("\t%s (%d) %s%s(%d)\n", f->name, f->tag, array, type_name, f->key); - } else { - printf("\t%s (%d) %s%s\n", f->name, f->tag, array, type_name); + printf("\t%s (%d) %s%s", f->name, f->tag, array, type_name); + if (type == SPROTO_TINTEGER && f->extra > 0) { + printf("(%d)", f->extra); } + if (f->key >= 0) { + printf("[%d]", f->key); + } + printf("\n"); } } printf("=== %d protocol ===\n", s->protocol_n); @@ -884,6 +919,7 @@ sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_call args.tagid = f->tag; args.subtype = f->st; args.mainindex = f->key; + args.extra = f->extra; if (type & SPROTO_TARRAY) { args.type = type & ~SPROTO_TARRAY; sz = encode_array(cb, &args, data, size); @@ -1116,6 +1152,7 @@ sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_ args.subtype = f->st; args.index = 0; args.mainindex = f->key; + args.extra = f->extra; if (value < 0) { if (f->type & SPROTO_TARRAY) { if (decode_array(cb, &args, currentdata)) { diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index c38a12c0..cf40497b 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -9,11 +9,16 @@ struct sproto_type; #define SPROTO_REQUEST 0 #define SPROTO_RESPONSE 1 +// type (sproto_arg.type) #define SPROTO_TINTEGER 0 #define SPROTO_TBOOLEAN 1 #define SPROTO_TSTRING 2 #define SPROTO_TSTRUCT 3 +// sub type of string (sproto_arg.extra) +#define SPROTO_TSTRING_STRING 0 +#define SPROTO_TSTRING_BINARY 1 + #define SPROTO_CB_ERROR -1 #define SPROTO_CB_NIL -2 #define SPROTO_CB_NOARRAY -3 @@ -41,6 +46,7 @@ struct sproto_arg { int length; int index; // array base 1 int mainindex; // for map + int extra; // SPROTO_TINTEGER: decimal ; SPROTO_TSTRING 0:utf8 string 1:binary }; typedef int (*sproto_callback)(const struct sproto_arg *args); diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 03524ac0..898a1cbc 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -70,6 +70,7 @@ local name = C(word) local typename = C(word * ("." * word) ^ 0) local tag = R"09" ^ 1 / tonumber local mainkey = "(" * blank0 * name * blank0 * ")" +local decimal = "(" * blank0 * C(tag) * blank0 * ")" local function multipat(pat) return Ct(blank0 * (pat * blanks) ^ 0 * pat^0 * blank0) @@ -81,7 +82,7 @@ end local typedef = P { "ALL", - FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^-1 * typename * mainkey^0)), + FIELD = namedpat("field", (name * blanks * tag * blank0 * ":" * blank0 * (C"*")^-1 * typename * (mainkey + decimal)^0)), STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}", TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ), SUBPROTO = Ct((C"request" + C"response") * blanks * (typename + V"STRUCT")), @@ -134,8 +135,12 @@ function convert.type(all, obj) end local mainkey = f[5] if mainkey then - assert(field.array) - field.key = mainkey + if fieldtype == "integer" then + field.decimal = mainkey + else + assert(field.array) + field.key = mainkey + end end field.typename = fieldtype else @@ -167,6 +172,7 @@ local buildin_types = { integer = 0, boolean = 1, string = 2, + binary = 2, -- binary is a sub type of string } local function checktype(types, ptype, t) @@ -275,7 +281,11 @@ local function packfield(f) table.insert(strtbl, "\0\0") -- name (tag = 0, ref an object) if f.buildin then table.insert(strtbl, packvalue(f.buildin)) -- buildin (tag = 1) - table.insert(strtbl, "\1\0") -- skip (tag = 2) + if f.extra then + table.insert(strtbl, packvalue(f.extra)) -- f.buildin can be integer or string + else + table.insert(strtbl, "\1\0") -- skip (tag = 2) + end table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3) else table.insert(strtbl, "\1\0") -- skip (tag = 1) @@ -299,8 +309,12 @@ local function packtype(name, t, alltypes) tmp.array = f.array tmp.name = f.name tmp.tag = f.tag + tmp.extra = f.decimal tmp.buildin = buildin_types[f.typename] + if f.typename == "binary" then + tmp.extra = 1 -- binary is sub type of string + end local subtype if not tmp.buildin then subtype = assert(alltypes[f.typename]) From b00b006c4a520302309fd4fa198e74cd84e9f6d5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 14 Mar 2017 21:01:26 +0800 Subject: [PATCH 675/729] update jemalloc 4.5.0 --- 3rd/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/jemalloc b/3rd/jemalloc index f1f76357..04380e79 160000 --- a/3rd/jemalloc +++ b/3rd/jemalloc @@ -1 +1 @@ -Subproject commit f1f76357313e7dcad7262f17a48ff0a2e005fcdc +Subproject commit 04380e79f1e2428bd0ad000bbc6e3d2dfc6b66a5 From 6170346da2b3c47cbd6bf586a5ebfb44c0a50720 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 16 Mar 2017 15:24:11 +0800 Subject: [PATCH 676/729] add LUAMOD_API --- lualib-src/lsha1.c | 4 +++- lualib-src/lua-bson.c | 4 +++- lualib-src/lua-clientsocket.c | 4 +++- lualib-src/lua-cluster.c | 4 +++- lualib-src/lua-crypt.c | 4 +++- lualib-src/lua-debugchannel.c | 4 +++- lualib-src/lua-memory.c | 4 +++- lualib-src/lua-mongo.c | 4 +++- lualib-src/lua-multicast.c | 4 +++- lualib-src/lua-mysqlaux.c | 10 +++------- lualib-src/lua-netpack.c | 4 +++- lualib-src/lua-profile.c | 4 +++- lualib-src/lua-seri.c | 4 +++- lualib-src/lua-sharedata.c | 4 +++- lualib-src/lua-skynet.c | 4 +++- lualib-src/lua-socket.c | 4 +++- lualib-src/lua-stm.c | 4 +++- lualib-src/sproto/lsproto.c | 2 ++ 18 files changed, 53 insertions(+), 23 deletions(-) diff --git a/lualib-src/lsha1.c b/lualib-src/lsha1.c index c421303c..8f539844 100644 --- a/lualib-src/lsha1.c +++ b/lualib-src/lsha1.c @@ -82,6 +82,8 @@ A million repetitions of "a" 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F */ +#define LUA_LIB + #include #include #include @@ -269,7 +271,7 @@ xor_key(uint8_t key[BLOCKSIZE], uint32_t xor) { } } -int +LUAMOD_API int lhmac_sha1(lua_State *L) { size_t key_sz = 0; const uint8_t * key = (const uint8_t *)luaL_checklstring(L, 1, &key_sz); diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 817df780..725de81b 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include @@ -1257,7 +1259,7 @@ lobjectid(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_bson(lua_State *L) { luaL_checkversion(L); int i; diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index fa1a88e8..def0bff6 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -2,6 +2,8 @@ // It's only for demo, limited feature. Don't use it in your project. // Rewrite socket library by yourself . +#define LUA_LIB + #include #include #include @@ -183,7 +185,7 @@ lreadstdin(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_clientsocket(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 6ee1e69b..37696562 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include #include @@ -475,7 +477,7 @@ lconcat(lua_State *L) { return 2; } -int +LUAMOD_API int luaopen_cluster_core(lua_State *L) { luaL_Reg l[] = { { "packrequest", lpackrequest }, diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 400322a5..184f5f57 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include @@ -901,7 +903,7 @@ lxor_str(lua_State *L) { int lsha1(lua_State *L); int lhmac_sha1(lua_State *L); -int +LUAMOD_API int luaopen_crypt(lua_State *L) { luaL_checkversion(L); static int init = 0; diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c index ad7e1f1b..a07da360 100644 --- a/lualib-src/lua-debugchannel.c +++ b/lualib-src/lua-debugchannel.c @@ -1,3 +1,5 @@ +#define LUA_LIB + // only for debug use #include #include @@ -268,7 +270,7 @@ static int db_sethook (lua_State *L) { return 0; } -int +LUAMOD_API int luaopen_debugchannel(lua_State *L) { luaL_Reg l[] = { { "create", lcreate }, // for write diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index 89e2996f..df771e2e 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include @@ -47,7 +49,7 @@ lcurrent(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_memory(lua_State *L) { luaL_checkversion(L); diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index af63d0b0..cdaa1521 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet_malloc.h" #include @@ -529,7 +531,7 @@ reply_length(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_mongo_driver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] ={ diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 841267a9..16e5ed08 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet.h" #include @@ -143,7 +145,7 @@ mc_nextid(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_multicast_core(lua_State *L) { luaL_Reg l[] = { { "pack", mc_packlocal }, diff --git a/lualib-src/lua-mysqlaux.c b/lualib-src/lua-mysqlaux.c index e6a3edc3..bee93edf 100644 --- a/lualib-src/lua-mysqlaux.c +++ b/lualib-src/lua-mysqlaux.c @@ -1,9 +1,5 @@ -// -// lua_mysqlaux.c -// -// Created by changfeng on 6/17/14. -// Copyright (c) 2014 changfeng. All rights reserved. -// +#define LUA_LIB + #include #include #include @@ -162,7 +158,7 @@ static struct luaL_Reg mysqlauxlib[] = { }; -int luaopen_mysqlaux_c (lua_State *L) { +LUAMOD_API int luaopen_mysqlaux_c (lua_State *L) { lua_newtable(L); luaL_setfuncs(L, mysqlauxlib, 0); return 1; diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 5fae0406..fb62f221 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet_malloc.h" #include "skynet_socket.h" @@ -462,7 +464,7 @@ ltostring(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_netpack(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c index a717bfd0..1167029a 100644 --- a/lualib-src/lua-profile.c +++ b/lualib-src/lua-profile.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include #include @@ -198,7 +200,7 @@ lyield_co(lua_State *L) { return timing_yield(L); } -int +LUAMOD_API int luaopen_profile(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 28056f74..e6dd531f 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -2,6 +2,8 @@ modify from https://github.com/cloudwu/lua-serialize */ +#define LUA_LIB + #include "skynet_malloc.h" #include @@ -594,7 +596,7 @@ luaseri_unpack(lua_State *L) { return lua_gettop(L) - 1; } -int +LUAMOD_API int luaseri_pack(lua_State *L) { struct block temp; temp.next = NULL; diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index abbc175d..9d49411e 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include #include @@ -762,7 +764,7 @@ lupdate(lua_State *L) { return 0; } -int +LUAMOD_API int luaopen_sharedata_core(lua_State *L) { luaL_Reg l[] = { // used by host diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index ee7bd897..074e293c 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet.h" #include "lua-seri.h" @@ -366,7 +368,7 @@ lnow(lua_State *L) { return 1; } -int +LUAMOD_API int luaopen_skynet_core(lua_State *L) { luaL_checkversion(L); diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index ecaa2fcb..39b8107f 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "skynet_malloc.h" #include @@ -674,7 +676,7 @@ ludp_address(lua_State *L) { return 2; } -int +LUAMOD_API int luaopen_socketdriver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index e175d100..e5a2fe1e 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include #include @@ -232,7 +234,7 @@ lread(lua_State *L) { } } -int +LUAMOD_API int luaopen_stm(lua_State *L) { luaL_checkversion(L); lua_createtable(L, 0, 3); diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 3eb32163..827ba96a 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -1,3 +1,5 @@ +#define LUA_LIB + #include #include #include "msvcint.h" From 673ffcce00acf1ea8d95b80189e3ef42f7677c1d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 19 Mar 2017 11:38:25 +0800 Subject: [PATCH 677/729] close stdfds after write_pid, see issue #590 --- skynet-src/skynet_daemon.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/skynet-src/skynet_daemon.c b/skynet-src/skynet_daemon.c index 4b6b96e1..902086e3 100644 --- a/skynet-src/skynet_daemon.c +++ b/skynet-src/skynet_daemon.c @@ -34,12 +34,12 @@ write_pid(const char *pidfile) { int pid = 0; int fd = open(pidfile, O_RDWR|O_CREAT, 0644); if (fd == -1) { - fprintf(stderr, "Can't create %s.\n", pidfile); + fprintf(stderr, "Can't create pidfile [%s].\n", pidfile); return 0; } f = fdopen(fd, "r+"); if (f == NULL) { - fprintf(stderr, "Can't open %s.\n", pidfile); + fprintf(stderr, "Can't open pidfile [%s].\n", pidfile); return 0; } @@ -65,6 +65,31 @@ write_pid(const char *pidfile) { return pid; } +static int +redirect_fds() { + int nfd = open("/dev/null", O_RDWR); + if (nfd == -1) { + perror("Unable to open /dev/null: "); + return -1; + } + if (dup2(nfd, 0) < 0) { + perror("Unable to dup2 stdin(0): "); + return -1; + } + if (dup2(nfd, 1) < 0) { + perror("Unable to dup2 stdout(1): "); + return -1; + } + if (dup2(nfd, 2) < 0) { + perror("Unable to dup2 stderr(2): "); + return -1; + } + + close(nfd); + + return 0; +} + int daemon_init(const char *pidfile) { int pid = check_pid(pidfile); @@ -77,7 +102,7 @@ daemon_init(const char *pidfile) { #ifdef __APPLE__ fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n"); #else - if (daemon(1,0)) { + if (daemon(1,1)) { fprintf(stderr, "Can't daemonize.\n"); return 1; } @@ -88,6 +113,10 @@ daemon_init(const char *pidfile) { return 1; } + if (redirect_fds()) { + return 1; + } + return 0; } From 80d9ec379f8b729ac0b13a18ea58da1420dd8504 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 20 Mar 2017 14:30:07 +0800 Subject: [PATCH 678/729] config support include --- skynet-src/skynet_main.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index f3ce56b2..141802be 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -83,15 +83,20 @@ int sigign() { } static const char * load_config = "\ - local config_name = ...\ - local f = assert(io.open(config_name))\ - local code = assert(f:read \'*a\')\ - local function getenv(name) return assert(os.getenv(name), \'os.getenv() failed: \' .. name) end\ - code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\ - f:close()\ - local result = {}\ - assert(load(code,\'=(load)\',\'t\',result))()\ - return result\ + local config_name = ...\n\ + local f = assert(io.open(config_name))\n\ + local code = assert(f:read \'*a\')\n\ + local function getenv(name) return assert(os.getenv(name), \'os.getenv() failed: \' .. name) end\n\ + code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\n\ + f:close()\n\ + local result = {}\n\ + local function include(filename)\n\ + assert(loadfile(filename, \'t\', result))()\n\ + end\n\ + setmetatable(result, { __index = { include = include } })\n\ + assert(load(code,\'=(load)\',\'t\',result))()\n\ + setmetatable(result, nil)\n\ + return result\n\ "; int @@ -116,7 +121,7 @@ main(int argc, char *argv[]) { struct lua_State *L = luaL_newstate(); luaL_openlibs(L); // link lua lib - int err = luaL_loadstring(L, load_config); + int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t"); assert(err == LUA_OK); lua_pushstring(L, config_file); From 67b055009db0f56f33e4cd0b7334bb7484dc35be Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 20 Mar 2017 16:25:15 +0800 Subject: [PATCH 679/729] bugfix isse #592 --- service/debug_agent.lua | 14 +++++++++++--- service/debug_console.lua | 22 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/service/debug_agent.lua b/service/debug_agent.lua index b4c4667d..451e2215 100644 --- a/service/debug_agent.lua +++ b/service/debug_agent.lua @@ -10,9 +10,13 @@ function CMD.start(address, fd) skynet.error(string.format("Attach to :%08x", address)) local handle channel, handle = debugchannel.create() - skynet.call(address, "debug", "REMOTEDEBUG", fd, handle) - -- todo hook - skynet.ret(skynet.pack(nil)) + local ok, err = pcall(skynet.call, address, "debug", "REMOTEDEBUG", fd, handle) + if not ok then + skynet.ret(skynet.pack(false, "Debugger attach failed")) + else + -- todo hook + skynet.ret(skynet.pack(true)) + end skynet.exit() end @@ -20,6 +24,10 @@ function CMD.cmd(cmdline) channel:write(cmdline) end +function CMD.ping() + skynet.ret() +end + skynet.start(function() skynet.dispatch("lua", function(_,_,cmd,...) local f = CMD[cmd] diff --git a/service/debug_console.lua b/service/debug_console.lua index d76a1e58..2db38e46 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -91,7 +91,7 @@ end local function console_main_loop(stdin, print) print("Welcome to skynet console") skynet.error(stdin, "connected") - pcall(function() + local ok, err = pcall(function() while true do local cmdline = socket.readline(stdin, "\n") if not cmdline then @@ -109,6 +109,9 @@ local function console_main_loop(stdin, print) end end end) + if not ok then + skynet.error(stdin, err) + end skynet.error(stdin, "disconnected") socket.close(stdin) end @@ -262,8 +265,11 @@ function COMMANDX.debug(cmd) local address = adjust_address(cmd[2]) local agent = skynet.newservice "debug_agent" local stop - skynet.fork(function() + local term_co = coroutine.running() + local function forward_cmd() repeat + -- notice : It's a bad practice to call socket.readline from two threads (this one and console_main_loop), be careful. + skynet.call(agent, "lua", "cmd", "ping") -- detect agent alive, if agent exit, raise error local cmdline = socket.readline(cmd.fd, "\n") cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") if not cmdline then @@ -272,9 +278,19 @@ function COMMANDX.debug(cmd) end skynet.send(agent, "lua", "cmd", cmdline) until stop or cmdline == "cont" + end + skynet.fork(function() + pcall(forward_cmd) + skynet.wakeup(term_co) end) - skynet.call(agent, "lua", "start", address, cmd.fd) + local ok, err = skynet.call(agent, "lua", "start", address, cmd.fd) stop = true + -- wait for fork coroutine exit. + skynet.wait(term_co) + + if not ok then + error(err) + end end function COMMAND.logon(address) From 3a4a673b43dab351a8458c28b2bf41a002bedaf2 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 21 Mar 2017 11:49:04 +0800 Subject: [PATCH 680/729] enhance config include --- examples/config | 10 +++------- examples/config.path | 6 ++++++ skynet-src/skynet_main.c | 30 ++++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 examples/config.path diff --git a/examples/config b/examples/config index 368a06f6..99e7af5f 100644 --- a/examples/config +++ b/examples/config @@ -1,4 +1,6 @@ -root = "./" +include "config.path" + +-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run thread = 8 logger = nil logpath = "." @@ -8,12 +10,6 @@ master = "127.0.0.1:2013" start = "main" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap standalone = "0.0.0.0:2013" -luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" -lualoader = root .. "lualib/loader.lua" -lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua" -lua_cpath = root .. "luaclib/?.so" --- preload = "./examples/preload.lua" -- run preload.lua before every lua service run -snax = root.."examples/?.lua;"..root.."test/?.lua" -- snax_interface_g = "snax_g" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" diff --git a/examples/config.path b/examples/config.path new file mode 100644 index 00000000..579fece5 --- /dev/null +++ b/examples/config.path @@ -0,0 +1,6 @@ +root = "./" +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +lualoader = root .. "lualib/loader.lua" +lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua" +lua_cpath = root .. "luaclib/?.so" +snax = root.."examples/?.lua;"..root.."test/?.lua" diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 141802be..f08feef2 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -83,18 +83,32 @@ int sigign() { } static const char * load_config = "\ - local config_name = ...\n\ - local f = assert(io.open(config_name))\n\ - local code = assert(f:read \'*a\')\n\ - local function getenv(name) return assert(os.getenv(name), \'os.getenv() failed: \' .. name) end\n\ - code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\n\ - f:close()\n\ local result = {}\n\ + local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\ + local sep = package.config:sub(1,1)\n\ + local current_path = [[.]]..sep\n\ local function include(filename)\n\ - assert(loadfile(filename, \'t\', result))()\n\ + local last_path = current_path\n\ + local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\ + if path then\n\ + if path:sub(1,1) == sep then -- root\n\ + current_path = path\n\ + else\n\ + current_path = current_path .. path\n\ + end\n\ + else\n\ + name = filename\n\ + end\n\ + local f = assert(io.open(current_path .. name))\n\ + local code = assert(f:read [[*a]])\n\ + code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\ + f:close()\n\ + assert(load(code,[[@]]..filename,[[t]],result))()\n\ + current_path = last_path\n\ end\n\ setmetatable(result, { __index = { include = include } })\n\ - assert(load(code,\'=(load)\',\'t\',result))()\n\ + local config_name = ...\n\ + include(config_name)\n\ setmetatable(result, nil)\n\ return result\n\ "; From 6289b2b8e4c5333561c2ea69c45fc97b5eeeed73 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 25 Mar 2017 00:10:23 +0800 Subject: [PATCH 681/729] complete cluster.send --- examples/cluster2.lua | 2 ++ examples/simpledb.lua | 12 +++++++- lualib-src/lua-cluster.c | 61 +++++++++++++++++++++++++++++----------- lualib/cluster.lua | 4 +-- lualib/skynet.lua | 5 ++++ service/clusterd.lua | 23 +++++++++++++-- service/clusterproxy.lua | 6 +++- 7 files changed, 90 insertions(+), 23 deletions(-) diff --git a/examples/cluster2.lua b/examples/cluster2.lua index 84648567..22c9cf7f 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -11,9 +11,11 @@ skynet.start(function() print(skynet.call(proxy, "lua", "SET", largekey, largevalue)) local v = skynet.call(proxy, "lua", "GET", largekey) assert(largevalue == v) + skynet.send(proxy, "lua", "PING", "proxy") print(cluster.call("db", sdb, "GET", "a")) print(cluster.call("db2", sdb, "GET", "b")) + cluster.send("db2", sdb, "PING", "db2:longstring" .. largevalue) -- test snax service local pingserver = cluster.snax("db", "pingserver") diff --git a/examples/simpledb.lua b/examples/simpledb.lua index d073dfe6..af6bac5e 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -16,7 +16,17 @@ end skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) - local f = command[string.upper(cmd)] + cmd = cmd:upper() + if cmd == "PING" then + assert(session == 0) + local str = (...) + if #str > 20 then + str = str:sub(1,20) .. "...(" .. #str .. ")" + end + skynet.error(string.format("%s ping %s", skynet.address(address), str)) + return + end + local f = command[cmd] if f then skynet.ret(skynet.pack(f(...))) else diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 37696562..ce54df45 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -46,7 +46,7 @@ fill_header(lua_State *L, uint8_t *buf, int sz) { PADDING msg(sz) size > 0x8000 and address is id DWORD 13 - BYTE 1 ; multireq + BYTE 1 ; multireq , 0x41: multi push DWORD addr DWORD session DWORD sz @@ -60,7 +60,7 @@ fill_header(lua_State *L, uint8_t *buf, int sz) { PADDING msg(sz) size > 0x8000 and address is string DWORD 10 + namelen - BYTE 0x81 + BYTE 0x81 ; 0xc1 : multi push BYTE namelen STRING name DWORD session @@ -73,14 +73,14 @@ fill_header(lua_State *L, uint8_t *buf, int sz) { PADDING msgpart(sz) */ static int -packreq_number(lua_State *L, int session, void * msg, uint32_t sz) { +packreq_number(lua_State *L, int session, void * msg, uint32_t sz, int is_push) { uint32_t addr = (uint32_t)lua_tointeger(L,1); uint8_t buf[TEMP_LENGTH]; if (sz < MULTI_PART) { fill_header(L, buf, sz+9); buf[2] = 0; fill_uint32(buf+3, addr); - fill_uint32(buf+7, (uint32_t)session); + fill_uint32(buf+7, is_push ? 0 : (uint32_t)session); memcpy(buf+11,msg,sz); lua_pushlstring(L, (const char *)buf, sz+11); @@ -88,7 +88,7 @@ packreq_number(lua_State *L, int session, void * msg, uint32_t sz) { } else { int part = (sz - 1) / MULTI_PART + 1; fill_header(L, buf, 13); - buf[2] = 1; + buf[2] = is_push ? 0x41 : 1; // multi push or request fill_uint32(buf+3, addr); fill_uint32(buf+7, (uint32_t)session); fill_uint32(buf+11, sz); @@ -98,7 +98,7 @@ packreq_number(lua_State *L, int session, void * msg, uint32_t sz) { } static int -packreq_string(lua_State *L, int session, void * msg, uint32_t sz) { +packreq_string(lua_State *L, int session, void * msg, uint32_t sz, int is_push) { size_t namelen = 0; const char *name = lua_tolstring(L, 1, &namelen); if (name == NULL || namelen < 1 || namelen > 255) { @@ -112,7 +112,7 @@ packreq_string(lua_State *L, int session, void * msg, uint32_t sz) { buf[2] = 0x80; buf[3] = (uint8_t)namelen; memcpy(buf+4, name, namelen); - fill_uint32(buf+4+namelen, (uint32_t)session); + fill_uint32(buf+4+namelen, is_push ? 0 : (uint32_t)session); memcpy(buf+8+namelen,msg,sz); lua_pushlstring(L, (const char *)buf, sz+8+namelen); @@ -120,7 +120,7 @@ packreq_string(lua_State *L, int session, void * msg, uint32_t sz) { } else { int part = (sz - 1) / MULTI_PART + 1; fill_header(L, buf, 10+namelen); - buf[2] = 0x81; + buf[2] = is_push ? 0xc1 : 0x81; // multi push or request buf[3] = (uint8_t)namelen; memcpy(buf+4, name, namelen); fill_uint32(buf+4+namelen, (uint32_t)session); @@ -157,7 +157,7 @@ packreq_multi(lua_State *L, int session, void * msg, uint32_t sz) { } static int -lpackrequest(lua_State *L) { +packrequest(lua_State *L, int is_push) { void *msg = lua_touserdata(L,3); if (msg == NULL) { return luaL_error(L, "Invalid request message"); @@ -171,9 +171,9 @@ lpackrequest(lua_State *L) { int addr_type = lua_type(L,1); int multipak; if (addr_type == LUA_TNUMBER) { - multipak = packreq_number(L, session, msg, sz); + multipak = packreq_number(L, session, msg, sz, is_push); } else { - multipak = packreq_string(L, session, msg, sz); + multipak = packreq_string(L, session, msg, sz, is_push); } int current_session = session; if (++session < 0) { @@ -191,6 +191,16 @@ lpackrequest(lua_State *L) { } } +static int +lpackrequest(lua_State *L) { + return packrequest(L, 0); +} + +static int +lpackpush(lua_State *L) { + return packrequest(L, 1); +} + /* string packed message return @@ -215,12 +225,17 @@ unpackreq_number(lua_State *L, const uint8_t * buf, int sz) { lua_pushinteger(L, address); lua_pushinteger(L, session); lua_pushlstring(L, (const char *)buf+9, sz-9); + if (session == 0) { + lua_pushnil(L); + lua_pushboolean(L,1); // is_push, no reponse + return 5; + } return 3; } static int -unpackmreq_number(lua_State *L, const uint8_t * buf, int sz) { +unpackmreq_number(lua_State *L, const uint8_t * buf, int sz, int is_push) { if (sz != 13) { return luaL_error(L, "Invalid cluster message size %d (multi req must be 13)", sz); } @@ -231,8 +246,9 @@ unpackmreq_number(lua_State *L, const uint8_t * buf, int sz) { lua_pushinteger(L, session); lua_pushinteger(L, size); lua_pushboolean(L, 1); // padding multi part + lua_pushboolean(L, is_push); - return 4; + return 5; } static int @@ -263,12 +279,17 @@ unpackreq_string(lua_State *L, const uint8_t * buf, int sz) { uint32_t session = unpack_uint32(buf + namesz + 2); lua_pushinteger(L, (uint32_t)session); lua_pushlstring(L, (const char *)buf+2+namesz+4, sz - namesz - 6); + if (session == 0) { + lua_pushnil(L); + lua_pushboolean(L,1); // is_push, no reponse + return 5; + } return 3; } static int -unpackmreq_string(lua_State *L, const uint8_t * buf, int sz) { +unpackmreq_string(lua_State *L, const uint8_t * buf, int sz, int is_push) { if (sz < 2) { return luaL_error(L, "Invalid cluster message (size=%d)", sz); } @@ -282,8 +303,9 @@ unpackmreq_string(lua_State *L, const uint8_t * buf, int sz) { lua_pushinteger(L, session); lua_pushinteger(L, size); lua_pushboolean(L, 1); // padding multipart + lua_pushboolean(L, is_push); - return 4; + return 5; } static int @@ -295,14 +317,18 @@ lunpackrequest(lua_State *L) { case 0: return unpackreq_number(L, (const uint8_t *)msg, sz); case 1: - return unpackmreq_number(L, (const uint8_t *)msg, sz); + return unpackmreq_number(L, (const uint8_t *)msg, sz, 0); // request + case '\x41': + return unpackmreq_number(L, (const uint8_t *)msg, sz, 1); // push case 2: case 3: return unpackmreq_part(L, (const uint8_t *)msg, sz); case '\x80': return unpackreq_string(L, (const uint8_t *)msg, sz); case '\x81': - return unpackmreq_string(L, (const uint8_t *)msg, sz); + return unpackmreq_string(L, (const uint8_t *)msg, sz, 0 ); // request + case '\xc1': + return unpackmreq_string(L, (const uint8_t *)msg, sz, 1 ); // push default: return luaL_error(L, "Invalid req package type %d", msg[0]); } @@ -481,6 +507,7 @@ LUAMOD_API int luaopen_cluster_core(lua_State *L) { luaL_Reg l[] = { { "packrequest", lpackrequest }, + { "packpush", lpackpush }, { "unpackrequest", lunpackrequest }, { "packresponse", lpackresponse }, { "unpackresponse", lunpackresponse }, diff --git a/lualib/cluster.lua b/lualib/cluster.lua index 3b180753..14dee2ac 100644 --- a/lualib/cluster.lua +++ b/lualib/cluster.lua @@ -9,8 +9,8 @@ function cluster.call(node, address, ...) end function cluster.send(node, address, ...) - -- skynet.pack(...) will free by cluster.core.packrequest - skynet.send(clusterd, "lua", "req", node, address, skynet.pack(...)) + -- push is the same with req, but no response + skynet.send(clusterd, "lua", "push", node, address, skynet.pack(...)) end function cluster.open(port) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a40eff36..49c7e9b4 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -362,6 +362,11 @@ function skynet.send(addr, typename, ...) return c.send(addr, p.id, 0 , p.pack(...)) end +function skynet.rawsend(addr, typename, msg, sz) + local p = proto[typename] + return c.send(addr, p.id, 0 , msg, sz) +end + skynet.genid = assert(c.genid) skynet.redirect = function(dest,source,typename,...) diff --git a/service/clusterd.lua b/service/clusterd.lua index 20c43ee7..911b27d1 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -91,6 +91,21 @@ function command.req(...) end end +function command.push(source, node, addr, msg, sz) + local session = node_session[node] or 1 + local request, new_session, padding = cluster.packpush(addr, session, msg, sz) + if padding then -- is multi push + node_session[node] = new_session + end + + -- node_channel[node] may yield or throw error + local c = node_channel[node] + + c:request(request, nil, padding) + + -- notice: push may fail where the channel is disconnected or broken. +end + local proxy = {} function command.proxy(source, node, name) @@ -121,9 +136,9 @@ local large_request = {} function command.socket(source, subcmd, fd, msg) if subcmd == "data" then local sz - local addr, session, msg, padding = cluster.unpackrequest(msg) + local addr, session, msg, padding, is_push = cluster.unpackrequest(msg) if padding then - local req = large_request[session] or { addr = addr } + local req = large_request[session] or { addr = addr , is_push = is_push } large_request[session] = req table.insert(req, msg) return @@ -134,6 +149,7 @@ function command.socket(source, subcmd, fd, msg) table.insert(req, msg) msg,sz = cluster.concat(req) addr = req.addr + is_push = req.is_push end if not msg then local response = cluster.packresponse(session, false, "Invalid large req") @@ -152,6 +168,9 @@ function command.socket(source, subcmd, fd, msg) ok = false msg = "name not found" end + elseif is_push then + skynet.rawsend(addr, "lua", msg, sz) + return -- no response else ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz) end diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index e003da2b..1e55cff6 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -23,6 +23,10 @@ skynet.forward_type( forward_map ,function() address = n end skynet.dispatch("system", function (session, source, msg, sz) - skynet.ret(skynet.rawcall(clusterd, "lua", skynet.pack("req", node, address, msg, sz))) + if session == 0 then + skynet.send(clusterd, "lua", "push", node, address, msg, sz) + else + skynet.ret(skynet.rawcall(clusterd, "lua", skynet.pack("req", node, address, msg, sz))) + end end) end) From 570a8356e608d2c8af6801c9474dcf076e5335c9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 25 Mar 2017 15:05:39 +0800 Subject: [PATCH 682/729] add comment --- lualib-src/lua-cluster.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index ce54df45..7975c020 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -37,7 +37,9 @@ fill_header(lua_State *L, uint8_t *buf, int sz) { } /* - The request package : + The request package : + first WORD is size of the package with big-endian + DWORD in content is small-endian size <= 0x8000 (32K) and address is id WORD sz+9 BYTE 0 @@ -45,7 +47,7 @@ fill_header(lua_State *L, uint8_t *buf, int sz) { DWORD session PADDING msg(sz) size > 0x8000 and address is id - DWORD 13 + WORD 13 BYTE 1 ; multireq , 0x41: multi push DWORD addr DWORD session @@ -59,7 +61,7 @@ fill_header(lua_State *L, uint8_t *buf, int sz) { DWORD session PADDING msg(sz) size > 0x8000 and address is string - DWORD 10 + namelen + WORD 10 + namelen BYTE 0x81 ; 0xc1 : multi push BYTE namelen STRING name @@ -335,6 +337,8 @@ lunpackrequest(lua_State *L) { } /* + The response package : + WORD size (big endian) DWORD session BYTE type 0: error From fd0291845f76304609cbc8e16df8b6af49c81bc0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 28 Mar 2017 18:54:14 +0800 Subject: [PATCH 683/729] proto.request is the condition --- lualib/sproto.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 00f5337b..757140ee 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -239,7 +239,7 @@ function host:attach(sp) self.__session[session] = proto.response or true end - if args then + if proto.request then local content = core.encode(proto.request, args) return core.pack(header .. content) else From 2a79d7bed9e7532de0ea47edcb070783dfea9a49 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 31 Mar 2017 17:51:11 +0800 Subject: [PATCH 684/729] add FAQ in comment --- lualib/skynet.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 49c7e9b4..b2d9ebce 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -1,3 +1,4 @@ +-- read https://github.com/cloudwu/skynet/wiki/FAQ for the module "skynet.core" local c = require "skynet.core" local tostring = tostring local tonumber = tonumber From 6699279d538422b596ccfceef0bcc3d87561e0d0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 5 Apr 2017 14:16:17 +0800 Subject: [PATCH 685/729] update sproto --- lualib-src/sproto/lsproto.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 827ba96a..11b1a67d 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -341,7 +341,8 @@ decode(const struct sproto_arg *args) { case SPROTO_TINTEGER: { // notice: in lua 5.2, 52bit integer support (not 64) if (args->extra) { - lua_Integer v = *(uint64_t*)args->value; + // lua_Integer is 32bit in small lua. + uint64_t v = *(uint64_t*)args->value; lua_Number vn = (lua_Number)v; vn /= args->extra; lua_pushnumber(L, vn); From 1f4a84edc271adb8901dfbefaecc33d2c392ae04 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 6 Apr 2017 11:57:28 +0800 Subject: [PATCH 686/729] fix dns bug , see issue #605 --- examples/main.lua | 2 ++ lualib/dns.lua | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index 5a2150db..1b371707 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -9,6 +9,7 @@ skynet.start(function() if not skynet.getenv "daemon" then local console = skynet.newservice("console") end +--[[ skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") @@ -18,5 +19,6 @@ skynet.start(function() nodelay = true, }) skynet.error("Watchdog listen on", 8888) +]] skynet.exit() end) diff --git a/lualib/dns.lua b/lualib/dns.lua index 318ba4c1..76f363f0 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -87,6 +87,7 @@ local weak = {__mode = "kv"} local CACHE = {} local dns = {} +local request_pool = {} function dns.flush() CACHE[QTYPE.A] = setmetatable({},weak) @@ -113,7 +114,21 @@ end local next_tid = 1 local function gen_tid() local tid = next_tid - next_tid = next_tid + 1 + if request_pool[tid] then + tid = nil + for i = 1, 65535 do + -- find available tid + if not request_pool[i] then + tid = i + break + end + end + assert(tid) + end + next_tid = tid + 1 + if next_tid > 65535 then + next_tid = 1 + end return tid end @@ -205,7 +220,6 @@ local function unpack_rdata(qtype, chunk) end local dns_server -local request_pool = {} local function resolve(content) if #content < DNS_HEADER_LEN then From 6fa241453c6964f1839f7c79b4775f8ed5d6ea65 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 7 Apr 2017 11:11:39 +0800 Subject: [PATCH 687/729] redirect support alloc session --- examples/main.lua | 2 - lualib-src/lua-skynet.c | 114 ++++++++++++++++------------------------ lualib/skynet.lua | 4 +- 3 files changed, 48 insertions(+), 72 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index 1b371707..5a2150db 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -9,7 +9,6 @@ skynet.start(function() if not skynet.getenv "daemon" then local console = skynet.newservice("console") end ---[[ skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") @@ -19,6 +18,5 @@ skynet.start(function() nodelay = true, }) skynet.error("Watchdog listen on", 8888) -]] skynet.exit() end) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 074e293c..daab81ed 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -174,17 +174,8 @@ get_dest_string(lua_State *L, int index) { return dest_string; } -/* - uint32 address - string address - integer type - integer session - string message - lightuserdata message_ptr - integer len - */ static int -lsend(lua_State *L) { +send_message(lua_State *L, int source, int idx_type) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; @@ -195,68 +186,19 @@ lsend(lua_State *L) { dest_string = get_dest_string(L, 1); } - int type = luaL_checkinteger(L, 2); + int type = luaL_checkinteger(L, idx_type+0); int session = 0; - if (lua_isnil(L,3)) { + if (lua_isnil(L,idx_type+1)) { type |= PTYPE_TAG_ALLOCSESSION; } else { - session = luaL_checkinteger(L,3); + session = luaL_checkinteger(L,idx_type+1); } - int mtype = lua_type(L,4); + int mtype = lua_type(L,idx_type+2); switch (mtype) { case LUA_TSTRING: { size_t len = 0; - void * msg = (void *)lua_tolstring(L,4,&len); - if (len == 0) { - msg = NULL; - } - if (dest_string) { - session = skynet_sendname(context, 0, dest_string, type, session , msg, len); - } else { - session = skynet_send(context, 0, dest, type, session , msg, len); - } - break; - } - case LUA_TLIGHTUSERDATA: { - void * msg = lua_touserdata(L,4); - int size = luaL_checkinteger(L,5); - if (dest_string) { - session = skynet_sendname(context, 0, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); - } else { - session = skynet_send(context, 0, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); - } - break; - } - default: - luaL_error(L, "skynet.send invalid param %s", lua_typename(L, lua_type(L,4))); - } - if (session < 0) { - // send to invalid address - // todo: maybe throw an error would be better - return 0; - } - lua_pushinteger(L,session); - return 1; -} - -static int -lredirect(lua_State *L) { - struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - uint32_t dest = (uint32_t)lua_tointeger(L,1); - const char * dest_string = NULL; - if (dest == 0) { - dest_string = get_dest_string(L, 1); - } - uint32_t source = (uint32_t)luaL_checkinteger(L,2); - int type = luaL_checkinteger(L,3); - int session = luaL_checkinteger(L,4); - - int mtype = lua_type(L,5); - switch (mtype) { - case LUA_TSTRING: { - size_t len = 0; - void * msg = (void *)lua_tolstring(L,5,&len); + void * msg = (void *)lua_tolstring(L,idx_type+2,&len); if (len == 0) { msg = NULL; } @@ -268,8 +210,8 @@ lredirect(lua_State *L) { break; } case LUA_TLIGHTUSERDATA: { - void * msg = lua_touserdata(L,5); - int size = luaL_checkinteger(L,6); + void * msg = lua_touserdata(L,idx_type+2); + int size = luaL_checkinteger(L,idx_type+3); if (dest_string) { session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); } else { @@ -278,9 +220,45 @@ lredirect(lua_State *L) { break; } default: - luaL_error(L, "skynet.redirect invalid param %s", lua_typename(L,mtype)); + luaL_error(L, "invalid param %s", lua_typename(L, lua_type(L,idx_type+2))); } - return 0; + if (session < 0) { + // send to invalid address + // todo: maybe throw an error would be better + return 0; + } + lua_pushinteger(L,session); + return 1; +} + +/* + uint32 address + string address + integer type + integer session + string message + lightuserdata message_ptr + integer len + */ +static int +lsend(lua_State *L) { + return send_message(L, 0, 2); +} + +/* + uint32 address + string address + integer source_address + integer type + integer session + string message + lightuserdata message_ptr + integer len + */ +static int +lredirect(lua_State *L) { + uint32_t source = (uint32_t)luaL_checkinteger(L,2); + return send_message(L, source, 3); } static int diff --git a/lualib/skynet.lua b/lualib/skynet.lua index b2d9ebce..900dc5c1 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -331,7 +331,7 @@ function skynet.exit() for co, session in pairs(session_coroutine_id) do local address = session_coroutine_address[co] if session~=0 and address then - c.redirect(address, 0, skynet.PTYPE_ERROR, session, "") + c.send(address, skynet.PTYPE_ERROR, session, "") end end for resp in pairs(unresponse) do @@ -343,7 +343,7 @@ function skynet.exit() tmp[address] = true end for address in pairs(tmp) do - c.redirect(address, 0, skynet.PTYPE_ERROR, 0, "") + c.send(address, skynet.PTYPE_ERROR, 0, "") end c.command("EXIT") -- quit service From 8e7796a67284bb086c18974cc781bd50bfb1a1f7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Apr 2017 13:09:24 +0800 Subject: [PATCH 688/729] lpeg 1.0.1 --- 3rd/lpeg/lpcap.h | 19 ++++++++-- 3rd/lpeg/lpcode.c | 76 ++++++++++++++++++++++++++------------ 3rd/lpeg/lpcode.h | 6 +-- 3rd/lpeg/lpeg.html | 43 +++++++++++++-------- 3rd/lpeg/lpprint.c | 22 +++++------ 3rd/lpeg/lptree.c | 13 +++++-- 3rd/lpeg/lptree.h | 53 ++++++++++++++------------ 3rd/lpeg/lptypes.h | 10 ++--- 3rd/lpeg/lpvm.c | 33 +++++++++++------ 3rd/lpeg/re.html | 6 +-- 3rd/lpeg/test.lua | 57 +++++++++++++++++++++++++++- skynet-src/skynet_module.c | 33 +++++++++++------ 12 files changed, 253 insertions(+), 118 deletions(-) diff --git a/3rd/lpeg/lpcap.h b/3rd/lpeg/lpcap.h index d762fdcf..6133df2a 100644 --- a/3rd/lpeg/lpcap.h +++ b/3rd/lpeg/lpcap.h @@ -1,5 +1,5 @@ /* -** $Id: lpcap.h,v 1.2 2015/02/27 17:13:17 roberto Exp $ +** $Id: lpcap.h,v 1.3 2016/09/13 17:45:58 roberto Exp $ */ #if !defined(lpcap_h) @@ -11,8 +11,21 @@ /* kinds of captures */ typedef enum CapKind { - Cclose, Cposition, Cconst, Cbackref, Carg, Csimple, Ctable, Cfunction, - Cquery, Cstring, Cnum, Csubst, Cfold, Cruntime, Cgroup + Cclose, /* not used in trees */ + Cposition, + Cconst, /* ktable[key] is Lua constant */ + Cbackref, /* ktable[key] is "name" of group to get capture */ + Carg, /* 'key' is arg's number */ + Csimple, /* next node is pattern */ + Ctable, /* next node is pattern */ + Cfunction, /* ktable[key] is function; next node is pattern */ + Cquery, /* ktable[key] is table; next node is pattern */ + Cstring, /* ktable[key] is string; next node is pattern */ + Cnum, /* numbered capture; 'key' is number of value to return */ + Csubst, /* substitution capture; next node is pattern */ + Cfold, /* ktable[key] is function; next node is pattern */ + Cruntime, /* not used in trees (is uses another type for tree) */ + Cgroup /* ktable[key] is group's "name" */ } CapKind; diff --git a/3rd/lpeg/lpcode.c b/3rd/lpeg/lpcode.c index fbf44feb..2722d716 100644 --- a/3rd/lpeg/lpcode.c +++ b/3rd/lpeg/lpcode.c @@ -1,5 +1,5 @@ /* -** $Id: lpcode.c,v 1.23 2015/06/12 18:36:47 roberto Exp $ +** $Id: lpcode.c,v 1.24 2016/09/15 17:46:13 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -125,6 +125,27 @@ int tocharset (TTree *tree, Charset *cs) { } +/* +** Visit a TCall node taking care to stop recursion. If node not yet +** visited, return 'f(sib2(tree))', otherwise return 'def' (default +** value) +*/ +static int callrecursive (TTree *tree, int f (TTree *t), int def) { + int key = tree->key; + assert(tree->tag == TCall); + assert(sib2(tree)->tag == TRule); + if (key == 0) /* node already visited? */ + return def; /* return default value */ + else { /* first visit */ + int result; + tree->key = 0; /* mark call as already visited */ + result = f(sib2(tree)); /* go to called rule */ + tree->key = key; /* restore tree */ + return result; + } +} + + /* ** Check whether a pattern tree has captures */ @@ -134,14 +155,17 @@ int hascaptures (TTree *tree) { case TCapture: case TRunTime: return 1; case TCall: - tree = sib2(tree); goto tailcall; /* return hascaptures(sib2(tree)); */ + return callrecursive(tree, hascaptures, 0); + case TRule: /* do not follow siblings */ + tree = sib1(tree); goto tailcall; case TOpenCall: assert(0); default: { switch (numsiblings[tree->tag]) { case 1: /* return hascaptures(sib1(tree)); */ tree = sib1(tree); goto tailcall; case 2: - if (hascaptures(sib1(tree))) return 1; + if (hascaptures(sib1(tree))) + return 1; /* else return hascaptures(sib2(tree)); */ tree = sib2(tree); goto tailcall; default: assert(numsiblings[tree->tag] == 0); return 0; @@ -208,9 +232,9 @@ int checkaux (TTree *tree, int pred) { /* ** number of characters to match a pattern (or -1 if variable) -** ('count' avoids infinite loops for grammars) */ -int fixedlenx (TTree *tree, int count, int len) { +int fixedlen (TTree *tree) { + int len = 0; /* to accumulate in tail calls */ tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: @@ -220,26 +244,29 @@ int fixedlenx (TTree *tree, int count, int len) { case TRep: case TRunTime: case TOpenCall: return -1; case TCapture: case TRule: case TGrammar: - /* return fixedlenx(sib1(tree), count); */ + /* return fixedlen(sib1(tree)); */ tree = sib1(tree); goto tailcall; - case TCall: - if (count++ >= MAXRULES) - return -1; /* may be a loop */ - /* else return fixedlenx(sib2(tree), count); */ - tree = sib2(tree); goto tailcall; + case TCall: { + int n1 = callrecursive(tree, fixedlen, -1); + if (n1 < 0) + return -1; + else + return len + n1; + } case TSeq: { - len = fixedlenx(sib1(tree), count, len); - if (len < 0) return -1; - /* else return fixedlenx(sib2(tree), count, len); */ - tree = sib2(tree); goto tailcall; + int n1 = fixedlen(sib1(tree)); + if (n1 < 0) + return -1; + /* else return fixedlen(sib2(tree)) + len; */ + len += n1; tree = sib2(tree); goto tailcall; } case TChoice: { - int n1, n2; - n1 = fixedlenx(sib1(tree), count, len); - if (n1 < 0) return -1; - n2 = fixedlenx(sib2(tree), count, len); - if (n1 == n2) return n1; - else return -1; + int n1 = fixedlen(sib1(tree)); + int n2 = fixedlen(sib2(tree)); + if (n1 != n2 || n1 < 0) + return -1; + else + return len + n1; } default: assert(0); return 0; }; @@ -710,9 +737,10 @@ static void codeand (CompileState *compst, TTree *tree, int tt) { /* -** Captures: if pattern has fixed (and not too big) length, use -** a single IFullCapture instruction after the match; otherwise, -** enclose the pattern with OpenCapture - CloseCapture. +** Captures: if pattern has fixed (and not too big) length, and it +** has no nested captures, use a single IFullCapture instruction +** after the match; otherwise, enclose the pattern with OpenCapture - +** CloseCapture. */ static void codecapture (CompileState *compst, TTree *tree, int tt, const Charset *fl) { diff --git a/3rd/lpeg/lpcode.h b/3rd/lpeg/lpcode.h index 896d3c79..2a5861ef 100644 --- a/3rd/lpeg/lpcode.h +++ b/3rd/lpeg/lpcode.h @@ -1,5 +1,5 @@ /* -** $Id: lpcode.h,v 1.7 2015/06/12 18:24:45 roberto Exp $ +** $Id: lpcode.h,v 1.8 2016/09/15 17:46:13 roberto Exp $ */ #if !defined(lpcode_h) @@ -13,7 +13,7 @@ int tocharset (TTree *tree, Charset *cs); int checkaux (TTree *tree, int pred); -int fixedlenx (TTree *tree, int count, int len); +int fixedlen (TTree *tree); int hascaptures (TTree *tree); int lp_gc (lua_State *L); Instruction *compile (lua_State *L, Pattern *p); @@ -35,8 +35,6 @@ int sizei (const Instruction *i); */ #define nullable(t) checkaux(t, PEnullable) -#define fixedlen(t) fixedlenx(t, 0, 0) - #endif diff --git a/3rd/lpeg/lpeg.html b/3rd/lpeg/lpeg.html index c0a7f090..5c9535f7 100644 --- a/3rd/lpeg/lpeg.html +++ b/3rd/lpeg/lpeg.html @@ -10,7 +10,7 @@ - +
@@ -577,8 +577,9 @@ It is equivalent to the following grammar in standard PEG notation:

Captures

-A capture is a pattern that creates values -(the so called semantic information) when it matches. +A capture is a pattern that produces values +(the so called semantic information) +according to what it matches. LPeg offers several kinds of captures, which produces values based on matches and combine these values to produce new values. @@ -632,10 +633,7 @@ or no value when number is zero.

-A capture pattern produces its values every time it succeeds. -For instance, -a capture inside a loop produces as many values as matched by the loop. -A capture produces a value only when it succeeds. +A capture pattern produces its values only when it succeeds. For instance, the pattern lpeg.C(lpeg.P"a"^-1) produces the empty string when there is no "a" @@ -643,14 +641,20 @@ produces the empty string when there is no "a" while the pattern lpeg.C("a")^-1 does not produce any value when there is no "a" (because the pattern "a" fails). +A pattern inside a loop or inside a recursive structure +produces values for each match.

Usually, -LPeg evaluates all captures only after (and if) the entire match succeeds. -During match time it only gathers enough information -to produce the capture values later. -As a particularly important consequence, +LPeg does not specify when (and if) it evaluates its captures. +(As an example, +consider the pattern lpeg.P"a" / func / 0. +Because the "division" by 0 instructs LPeg to throw away the +results from the pattern, +LPeg may or may not call func.) +Therefore, captures should avoid side effects. +Moreover, most captures cannot affect the way a pattern matches a subject. The only exception to this rule is the so-called match-time capture. @@ -700,6 +704,12 @@ An Outermost capture means that the capture is not inside another complete capture.

+

+In the same way that LPeg does not specify when it evaluates captures, +it does not specify whether it reuses +values previously produced by the group +or re-evaluates them. +

lpeg.Cc ([value, ...])

@@ -806,7 +816,7 @@ all replacements.

lpeg.Ct (patt)

Creates a table capture. -This capture creates a table and puts all values from all anonymous captures +This capture returns a table with all values from all anonymous captures made by patt inside this table in successive integer keys, starting at 1. Moreover, @@ -872,7 +882,8 @@ there is no captured value.

Creates a match-time capture. Unlike all other captures, -this one is evaluated immediately when a match occurs. +this one is evaluated immediately when a match occurs +(even if it is part of a larger pattern that fails later). It forces the immediate evaluation of all its nested captures and then calls function.

@@ -1380,13 +1391,13 @@ and the new term for each repetition.

Download

LPeg -source code.

+source code.

License

-Copyright © 2007-2015 Lua.org, PUC-Rio. +Copyright © 2007-2017 Lua.org, PUC-Rio.

Permission is hereby granted, free of charge, @@ -1424,7 +1435,7 @@ THE SOFTWARE.

-$Id: lpeg.html,v 1.75 2015/09/28 17:17:41 roberto Exp $ +$Id: lpeg.html,v 1.77 2017/01/13 13:40:05 roberto Exp $

diff --git a/3rd/lpeg/lpprint.c b/3rd/lpeg/lpprint.c index 174d1687..f7be408f 100644 --- a/3rd/lpeg/lpprint.c +++ b/3rd/lpeg/lpprint.c @@ -1,5 +1,5 @@ /* -** $Id: lpprint.c,v 1.9 2015/06/15 16:09:57 roberto Exp $ +** $Id: lpprint.c,v 1.10 2016/09/13 16:06:03 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -37,13 +37,13 @@ void printcharset (const byte *st) { } -static void printcapkind (int kind) { +static const char *capkind (int kind) { const char *const modes[] = { "close", "position", "constant", "backref", "argument", "simple", "table", "function", "query", "string", "num", "substitution", "fold", "runtime", "group"}; - printf("%s", modes[kind]); + return modes[kind]; } @@ -73,13 +73,12 @@ void printinst (const Instruction *op, const Instruction *p) { break; } case IFullCapture: { - printcapkind(getkind(p)); - printf(" (size = %d) (idx = %d)", getoff(p), p->i.key); + printf("%s (size = %d) (idx = %d)", + capkind(getkind(p)), getoff(p), p->i.key); break; } case IOpenCapture: { - printcapkind(getkind(p)); - printf(" (idx = %d)", p->i.key); + printf("%s (idx = %d)", capkind(getkind(p)), p->i.key); break; } case ISet: { @@ -124,8 +123,8 @@ void printpatt (Instruction *p, int n) { #if defined(LPEG_DEBUG) static void printcap (Capture *cap) { - printcapkind(cap->kind); - printf(" (idx: %d - size: %d) -> %p\n", cap->idx, cap->siz, cap->s); + printf("%s (idx: %d - size: %d) -> %p\n", + capkind(cap->kind), cap->idx, cap->siz, cap->s); } @@ -177,7 +176,8 @@ void printtree (TTree *tree, int ident) { break; } case TOpenCall: case TCall: { - printf(" key: %d\n", tree->key); + assert(sib2(tree)->tag == TRule); + printf(" key: %d (rule: %d)\n", tree->key, sib2(tree)->cap); break; } case TBehind: { @@ -186,7 +186,7 @@ void printtree (TTree *tree, int ident) { break; } case TCapture: { - printf(" cap: %d key: %d n: %d\n", tree->cap, tree->key, tree->u.n); + printf(" kind: '%s' key: %d\n", capkind(tree->cap), tree->key); printtree(sib1(tree), ident + 2); break; } diff --git a/3rd/lpeg/lptree.c b/3rd/lpeg/lptree.c index ac5f5150..bda61b91 100644 --- a/3rd/lpeg/lptree.c +++ b/3rd/lpeg/lptree.c @@ -1,5 +1,5 @@ /* -** $Id: lptree.c,v 1.21 2015/09/28 17:01:25 roberto Exp $ +** $Id: lptree.c,v 1.22 2016/09/13 18:10:22 roberto Exp $ ** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -64,7 +64,7 @@ static void fixonecall (lua_State *L, int postable, TTree *g, TTree *t) { t->tag = TCall; t->u.ps = n - (t - g); /* position relative to node */ assert(sib2(t)->tag == TRule); - sib2(t)->key = t->key; + sib2(t)->key = t->key; /* fix rule's key */ } @@ -935,7 +935,7 @@ static void buildgrammar (lua_State *L, TTree *grammar, int frule, int n) { int rulesize; TTree *rn = gettree(L, ridx, &rulesize); nd->tag = TRule; - nd->key = 0; + nd->key = 0; /* will be fixed when rule is used */ nd->cap = i; /* rule number */ nd->u.ps = rulesize + 1; /* point to next rule */ memcpy(sib1(nd), rn, rulesize * sizeof(TTree)); /* copy rule */ @@ -969,6 +969,11 @@ static int checkloops (TTree *tree) { } +/* +** Give appropriate error message for 'verifyrule'. If a rule appears +** twice in 'passed', there is path from it back to itself without +** advancing the subject. +*/ static int verifyerror (lua_State *L, int *passed, int npassed) { int i, j; for (i = npassed - 1; i >= 0; i--) { /* search for a repetition */ @@ -990,6 +995,8 @@ static int verifyerror (lua_State *L, int *passed, int npassed) { ** is only relevant if the first is nullable. ** Parameter 'nb' works as an accumulator, to allow tail calls in ** choices. ('nb' true makes function returns true.) +** Parameter 'passed' is a list of already visited rules, 'npassed' +** counts the elements in 'passed'. ** Assume ktable at the top of the stack. */ static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed, diff --git a/3rd/lpeg/lptree.h b/3rd/lpeg/lptree.h index b69528a6..34ee15ca 100644 --- a/3rd/lpeg/lptree.h +++ b/3rd/lpeg/lptree.h @@ -1,5 +1,5 @@ /* -** $Id: lptree.h,v 1.2 2013/03/24 13:51:12 roberto Exp $ +** $Id: lptree.h,v 1.3 2016/09/13 18:07:51 roberto Exp $ */ #if !defined(lptree_h) @@ -13,38 +13,43 @@ ** types of trees */ typedef enum TTag { - TChar = 0, TSet, TAny, /* standard PEG elements */ - TTrue, TFalse, - TRep, - TSeq, TChoice, - TNot, TAnd, - TCall, - TOpenCall, - TRule, /* sib1 is rule's pattern, sib2 is 'next' rule */ - TGrammar, /* sib1 is initial (and first) rule */ - TBehind, /* match behind */ - TCapture, /* regular capture */ - TRunTime /* run-time capture */ + TChar = 0, /* 'n' = char */ + TSet, /* the set is stored in next CHARSETSIZE bytes */ + TAny, + TTrue, + TFalse, + TRep, /* 'sib1'* */ + TSeq, /* 'sib1' 'sib2' */ + TChoice, /* 'sib1' / 'sib2' */ + TNot, /* !'sib1' */ + TAnd, /* &'sib1' */ + TCall, /* ktable[key] is rule's key; 'sib2' is rule being called */ + TOpenCall, /* ktable[key] is rule's key */ + TRule, /* ktable[key] is rule's key (but key == 0 for unused rules); + 'sib1' is rule's pattern; + 'sib2' is next rule; 'cap' is rule's sequential number */ + TGrammar, /* 'sib1' is initial (and first) rule */ + TBehind, /* 'sib1' is pattern, 'n' is how much to go back */ + TCapture, /* captures: 'cap' is kind of capture (enum 'CapKind'); + ktable[key] is Lua value associated with capture; + 'sib1' is capture body */ + TRunTime /* run-time capture: 'key' is Lua function; + 'sib1' is capture body */ } TTag; -/* number of siblings for each tree */ -extern const byte numsiblings[]; - /* ** Tree trees -** The first sibling of a tree (if there is one) is immediately after -** the tree. A reference to a second sibling (ps) is its position -** relative to the position of the tree itself. A key in ktable -** uses the (unique) address of the original tree that created that -** entry. NULL means no data. +** The first child of a tree (if there is one) is immediately after +** the tree. A reference to a second child (ps) is its position +** relative to the position of the tree itself. */ typedef struct TTree { byte tag; byte cap; /* kind of capture (if it is a capture) */ unsigned short key; /* key in ktable for Lua data (0 if no key) */ union { - int ps; /* occasional second sibling */ + int ps; /* occasional second child */ int n; /* occasional counter */ } u; } TTree; @@ -61,10 +66,10 @@ typedef struct Pattern { } Pattern; -/* number of siblings for each tree */ +/* number of children for each tree */ extern const byte numsiblings[]; -/* access to siblings */ +/* access to children */ #define sib1(t) ((t) + 1) #define sib2(t) ((t) + (t)->u.ps) diff --git a/3rd/lpeg/lptypes.h b/3rd/lpeg/lptypes.h index 5eb7987b..8e78bc81 100644 --- a/3rd/lpeg/lptypes.h +++ b/3rd/lpeg/lptypes.h @@ -1,7 +1,7 @@ /* -** $Id: lptypes.h,v 1.14 2015/09/28 17:17:41 roberto Exp $ +** $Id: lptypes.h,v 1.16 2017/01/13 13:33:17 roberto Exp $ ** LPeg - PEG pattern matching for Lua -** Copyright 2007-2015, Lua.org & PUC-Rio (see 'lpeg.html' for license) +** Copyright 2007-2017, Lua.org & PUC-Rio (see 'lpeg.html' for license) ** written by Roberto Ierusalimschy */ @@ -19,7 +19,7 @@ #include "lua.h" -#define VERSION "1.0.0" +#define VERSION "1.0.1" #define PATTERN_T "lpeg-pattern" @@ -55,9 +55,9 @@ #endif -/* maximum number of rules in a grammar */ +/* maximum number of rules in a grammar (limited by 'unsigned char') */ #if !defined(MAXRULES) -#define MAXRULES 1000 +#define MAXRULES 250 #endif diff --git a/3rd/lpeg/lpvm.c b/3rd/lpeg/lpvm.c index eaf2ebfd..05a5f68c 100644 --- a/3rd/lpeg/lpvm.c +++ b/3rd/lpeg/lpvm.c @@ -1,5 +1,5 @@ /* -** $Id: lpvm.c,v 1.6 2015/09/28 17:01:25 roberto Exp $ +** $Id: lpvm.c,v 1.9 2016/06/03 20:11:18 roberto Exp $ ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) */ @@ -45,14 +45,16 @@ typedef struct Stack { /* -** Double the size of the array of captures +** Make the size of the array of captures 'cap' twice as large as needed +** (which is 'captop'). ('n' is the number of new elements.) */ -static Capture *doublecap (lua_State *L, Capture *cap, int captop, int ptop) { +static Capture *doublecap (lua_State *L, Capture *cap, int captop, + int n, int ptop) { Capture *newc; if (captop >= INT_MAX/((int)sizeof(Capture) * 2)) luaL_error(L, "too many captures"); newc = (Capture *)lua_newuserdata(L, captop * 2 * sizeof(Capture)); - memcpy(newc, cap, captop * sizeof(Capture)); + memcpy(newc, cap, (captop - n) * sizeof(Capture)); lua_replace(L, caplistidx(ptop)); return newc; } @@ -113,8 +115,8 @@ static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { */ static void adddyncaptures (const char *s, Capture *base, int n, int fd) { int i; - /* Cgroup capture is already there */ - assert(base[0].kind == Cgroup && base[0].siz == 0); + base[0].kind = Cgroup; /* create group capture */ + base[0].siz = 0; base[0].idx = 0; /* make it an anonymous group */ for (i = 1; i <= n; i++) { /* add runtime captures */ base[i].kind = Cruntime; @@ -157,10 +159,11 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, lua_pushlightuserdata(L, stackbase); for (;;) { #if defined(DEBUG) - printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", - s, stack - getstackbase(L, ptop), ndyncap, captop); - printinst(op, p); + printf("-------------------------------------\n"); printcaplist(capture, capture + captop); + printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", + s, (int)(stack - getstackbase(L, ptop)), ndyncap, captop); + printinst(op, p); #endif assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); switch ((Opcode)p->i.code) { @@ -284,6 +287,9 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, ndyncap -= removedyncap(L, capture, stack->caplevel, captop); captop = stack->caplevel; p = stack->p; +#if defined(DEBUG) + printf("**FAIL**\n"); +#endif continue; } case ICloseRunTime: { @@ -293,16 +299,19 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, cs.s = o; cs.L = L; cs.ocap = capture; cs.ptop = ptop; n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ captop -= n; /* remove nested captures */ + ndyncap -= rem; /* update number of dynamic captures */ fr -= rem; /* 'rem' items were popped from Lua stack */ res = resdyncaptures(L, fr, s - o, e - o); /* get result */ if (res == -1) /* fail? */ goto fail; s = o + res; /* else update current position */ n = lua_gettop(L) - fr + 1; /* number of new captures */ - ndyncap += n - rem; /* update number of dynamic captures */ + ndyncap += n; /* update number of dynamic captures */ if (n > 0) { /* any new capture? */ + if (fr + n >= SHRT_MAX) + luaL_error(L, "too many results in match-time capture"); if ((captop += n + 2) >= capsize) { - capture = doublecap(L, capture, captop, ptop); + capture = doublecap(L, capture, captop, n + 2, ptop); capsize = 2 * captop; } /* add new captures to 'capture' list */ @@ -339,7 +348,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, capture[captop].idx = p->i.key; capture[captop].kind = getkind(p); if (++captop >= capsize) { - capture = doublecap(L, capture, captop, ptop); + capture = doublecap(L, capture, captop, 0, ptop); capsize = 2 * captop; } p++; diff --git a/3rd/lpeg/re.html b/3rd/lpeg/re.html index d0d9744f..32f0a458 100644 --- a/3rd/lpeg/re.html +++ b/3rd/lpeg/re.html @@ -10,7 +10,7 @@ - +
@@ -406,7 +406,7 @@ of patterns accepted by re. p = [=[ pattern <- exp !. -exp <- S (alternative / grammar) +exp <- S (grammar / alternative) alternative <- seq ('/' S seq)* seq <- prefix* @@ -488,7 +488,7 @@ THE SOFTWARE.

-$Id: re.html,v 1.23 2015/09/28 17:17:41 roberto Exp $ +$Id: re.html,v 1.24 2016/09/20 17:41:27 roberto Exp $

diff --git a/3rd/lpeg/test.lua b/3rd/lpeg/test.lua index 017a3abf..20ad07f8 100755 --- a/3rd/lpeg/test.lua +++ b/3rd/lpeg/test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env lua --- $Id: test.lua,v 1.109 2015/09/28 17:01:25 roberto Exp $ +-- $Id: test.lua,v 1.112 2017/01/14 18:55:22 roberto Exp $ -- require"strict" -- just to be pedantic @@ -202,6 +202,14 @@ do end +-- bug: loop in 'hascaptures' +do + local p = m.C(-m.P{m.P'x' * m.V(1) + m.P'y'}) + assert(p:match("xxx") == "") +end + + + -- test for small capture boundary for i = 250,260 do assert(#m.match(m.C(i), string.rep('a', i)) == i) @@ -517,6 +525,27 @@ assert(m.match(m.Cs((#((#m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") assert(m.match(m.Cs((- -m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") assert(m.match(m.Cs((-((-m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") + +-- fixed length +do + -- 'and' predicate using fixed length + local p = m.C(#("a" * (m.P("bd") + "cd")) * 2) + assert(p:match("acd") == "ac") + + p = #m.P{ "a" * m.V(2), m.P"b" } * 2 + assert(p:match("abc") == 3) + + p = #(m.P"abc" * m.B"c") + assert(p:match("abc") == 1 and not p:match("ab")) + + p = m.P{ "a" * m.V(2), m.P"b"^1 } + checkerr("pattern may not have fixed length", m.B, p) + + p = "abc" * (m.P"b"^1 + m.P"a"^0) + checkerr("pattern may not have fixed length", m.B, p) +end + + p = -m.P'a' * m.Cc(1) + -m.P'b' * m.Cc(2) + -m.P'c' * m.Cc(3) assert(p:match('a') == 2 and p:match('') == 1 and p:match('b') == 1) @@ -1098,6 +1127,32 @@ do assert(c == 11) end + +-- Return a match-time capture that returns 'n' captures +local function manyCmt (n) + return m.Cmt("a", function () + local a = {}; for i = 1, n do a[i] = n - i end + return true, unpack(a) + end) +end + +-- bug in 1.0: failed match-time that used previous match-time results +do + local x + local function aux (...) x = #{...}; return false end + local res = {m.match(m.Cmt(manyCmt(20), aux) + manyCmt(10), "a")} + assert(#res == 10 and res[1] == 9 and res[10] == 0) +end + + +-- bug in 1.0: problems with math-times returning too many captures +do + local lim = 2^11 - 10 + local res = {m.match(manyCmt(lim), "a")} + assert(#res == lim and res[1] == lim - 1 and res[lim] == 0) + checkerr("too many", m.match, manyCmt(2^15), "a") +end + p = (m.P(function () return true, "a" end) * 'a' + m.P(function (s, i) return i, "aa", 20 end) * 'b' + m.P(function (s,i) if i <= #s then return i, "aaa" end end) * 1)^0 diff --git a/skynet-src/skynet_module.c b/skynet-src/skynet_module.c index d586427f..a473e422 100644 --- a/skynet-src/skynet_module.c +++ b/skynet-src/skynet_module.c @@ -73,19 +73,28 @@ _query(const char * name) { return NULL; } -static int -_open_sym(struct skynet_module *mod) { +static void * +get_api(struct skynet_module *mod, const char *api_name) { size_t name_size = strlen(mod->name); - char tmp[name_size + 9]; // create/init/release/signal , longest name is release (7) + size_t api_size = strlen(api_name); + char tmp[name_size + api_size + 1]; memcpy(tmp, mod->name, name_size); - strcpy(tmp+name_size, "_create"); - mod->create = dlsym(mod->module, tmp); - strcpy(tmp+name_size, "_init"); - mod->init = dlsym(mod->module, tmp); - strcpy(tmp+name_size, "_release"); - mod->release = dlsym(mod->module, tmp); - strcpy(tmp+name_size, "_signal"); - mod->signal = dlsym(mod->module, tmp); + memcpy(tmp+name_size, api_name, api_size+1); + char *ptr = strrchr(tmp, '.'); + if (ptr == NULL) { + ptr = tmp; + } else { + ptr = ptr + 1; + } + return dlsym(mod->module, ptr); +} + +static int +open_sym(struct skynet_module *mod) { + mod->create = get_api(mod, "_create"); + mod->init = get_api(mod, "_init"); + mod->release = get_api(mod, "_release"); + mod->signal = get_api(mod, "_signal"); return mod->init == NULL; } @@ -107,7 +116,7 @@ skynet_module_query(const char * name) { M->m[index].name = name; M->m[index].module = dl; - if (_open_sym(&M->m[index]) == 0) { + if (open_sym(&M->m[index]) == 0) { M->m[index].name = skynet_strdup(name); M->count ++; result = &M->m[index]; From 61d13846cde40395bc2093ff1f9adeeb86f2dea6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Apr 2017 17:04:58 +0800 Subject: [PATCH 689/729] fix a bug , issue #592 --- service/debug_console.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 2db38e46..ec4ea482 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -269,7 +269,7 @@ function COMMANDX.debug(cmd) local function forward_cmd() repeat -- notice : It's a bad practice to call socket.readline from two threads (this one and console_main_loop), be careful. - skynet.call(agent, "lua", "cmd", "ping") -- detect agent alive, if agent exit, raise error + skynet.call(agent, "lua", "ping") -- detect agent alive, if agent exit, raise error local cmdline = socket.readline(cmd.fd, "\n") cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") if not cmdline then From 312943c6a0cff4a20975065bc01251850af5218c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 13 Apr 2017 15:32:54 +0800 Subject: [PATCH 690/729] connect timeout, see #611 --- lualib/http/httpc.lua | 2 +- lualib/http/sockethelper.lua | 24 ++++++++++++++++++++++-- test/testhttp.lua | 5 ++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 16a52823..43a44e19 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -99,7 +99,7 @@ function httpc.request(method, host, url, recvheader, header, content) if async_dns and not hostname:match(".*%d+$") then hostname = dns.resolve(hostname) end - local fd = socket.connect(hostname, port) + local fd = socket.connect(hostname, port, timeout) local finish if timeout then skynet.timeout(timeout, function() diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 735da45d..0d8e80fb 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -1,4 +1,5 @@ local socket = require "socket" +local skynet = require "skynet" local readbytes = socket.read local writebytes = socket.write @@ -66,8 +67,27 @@ function sockethelper.writefunc(fd) end end -function sockethelper.connect(host, port) - local fd = socket.open(host, port) +function sockethelper.connect(host, port, timeout) + local fd + if timeout then + local drop_fd + -- asynchronous connect + skynet.fork(function() + fd = socket.open(host, port) + if drop_fd then + -- sockethelper.connect already return, and raise socket_error + socket.close(fd) + end + end) + skynet.sleep(timeout) + if not fd then + -- not connect yet + drop_fd = true + end + else + -- block connect + fd = socket.open(host, port) + end if fd then return fd end diff --git a/test/testhttp.lua b/test/testhttp.lua index e66d6c19..4098b629 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -2,7 +2,7 @@ local skynet = require "skynet" local httpc = require "http.httpc" local dns = require "dns" -skynet.start(function() +local function main() httpc.dns() -- set dns server httpc.timeout = 100 -- set timeout 1 second print("GET baidu.com") @@ -21,6 +21,9 @@ skynet.start(function() print(string.format("GET %s (baidu.com)", ip)) local status, body = httpc.get("baidu.com", "/", respheader, { host = "baidu.com" }) print(status) +end +skynet.start(function() + print(pcall(main)) skynet.exit() end) From 62a4bcdc713ca591d1b416fb8e07ddb8d1f771f7 Mon Sep 17 00:00:00 2001 From: djx Date: Tue, 18 Apr 2017 16:03:01 +0800 Subject: [PATCH 691/729] =?UTF-8?q?1=E3=80=81remove=20not=20used=20require?= =?UTF-8?q?=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/watchdog.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index efee5e24..dabbd097 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -1,5 +1,4 @@ local skynet = require "skynet" -local netpack = require "netpack" local CMD = {} local SOCKET = {} From 66f8fbd1b718a9b13b1187845854b0b5afbb28b4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 19 Apr 2017 10:50:48 +0800 Subject: [PATCH 692/729] see issue #615 --- skynet-src/skynet_socket.c | 2 +- skynet-src/socket_server.c | 26 +++++++++++++------------- skynet-src/socket_server.h | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 2f3aec84..af793d35 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -89,7 +89,7 @@ skynet_socket_poll() { case SOCKET_OPEN: forward_message(SKYNET_SOCKET_TYPE_CONNECT, true, &result); break; - case SOCKET_ERROR: + case SOCKET_ERR: forward_message(SKYNET_SOCKET_TYPE_ERROR, true, &result); break; case SOCKET_ACCEPT: diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index d1f55966..ea90aab4 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -469,7 +469,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock _failed: freeaddrinfo( ai_list ); ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; - return SOCKET_ERROR; + return SOCKET_ERR; } static int @@ -550,7 +550,7 @@ send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, result->ud = 0; result->data = NULL; - return SOCKET_ERROR; + return SOCKET_ERR; */ } @@ -778,7 +778,7 @@ _failed: result->data = "reach skynet socket number limit"; ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; - return SOCKET_ERROR; + return SOCKET_ERR; } static int @@ -817,7 +817,7 @@ bind_socket(struct socket_server *ss, struct request_bind *request, struct socke struct socket *s = new_fd(ss, id, request->fd, PROTOCOL_TCP, request->opaque, true); if (s == NULL) { result->data = "reach skynet socket number limit"; - return SOCKET_ERROR; + return SOCKET_ERR; } sp_nonblocking(request->fd); s->type = SOCKET_TYPE_BIND; @@ -835,13 +835,13 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc struct socket *s = &ss->slot[HASH_ID(id)]; if (s->type == SOCKET_TYPE_INVALID || s->id !=id) { result->data = "invalid socket"; - return SOCKET_ERROR; + return SOCKET_ERR; } if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { if (sp_add(ss->event_fd, s->fd, s)) { force_close(ss, s, result); result->data = strerror(errno); - return SOCKET_ERROR; + return SOCKET_ERR; } s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; s->opaque = request->opaque; @@ -932,7 +932,7 @@ set_udp_address(struct socket_server *ss, struct request_setudp *request, struct result->ud = 0; result->data = "protocol mismatch"; - return SOCKET_ERROR; + return SOCKET_ERR; } if (type == PROTOCOL_UDP) { memcpy(s->p.udp_address, request->address, 1+2+4); // 1 type, 2 port, 4 ipv4 @@ -1013,7 +1013,7 @@ forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_me // close when error force_close(ss, s, result); result->data = strerror(errno); - return SOCKET_ERROR; + return SOCKET_ERR; } return -1; } @@ -1074,7 +1074,7 @@ forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_me // close when error force_close(ss, s, result); result->data = strerror(errno); - return SOCKET_ERROR; + return SOCKET_ERR; } return -1; } @@ -1111,7 +1111,7 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message result->data = strerror(error); else result->data = strerror(errno); - return SOCKET_ERROR; + return SOCKET_ERR; } else { s->type = SOCKET_TYPE_CONNECTED; result->opaque = s->opaque; @@ -1182,7 +1182,7 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message static inline void clear_closed_event(struct socket_server *ss, struct socket_message * result, int type) { - if (type == SOCKET_CLOSE || type == SOCKET_ERROR) { + if (type == SOCKET_CLOSE || type == SOCKET_ERR) { int id = result->id; int i; for (i=ss->event_index; ievent_n; i++) { @@ -1240,7 +1240,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int if (ok > 0) { return SOCKET_ACCEPT; } if (ok < 0 ) { - return SOCKET_ERROR; + return SOCKET_ERR; } // when ok == 0, retry break; @@ -1261,7 +1261,7 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int return SOCKET_UDP; } } - if (e->write && type != SOCKET_CLOSE && type != SOCKET_ERROR) { + if (e->write && type != SOCKET_CLOSE && type != SOCKET_ERR) { // Try to dispatch write message next step if write flag set. e->read = false; --ss->event_index; diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 96e38980..31b3c9de 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -7,7 +7,7 @@ #define SOCKET_CLOSE 1 #define SOCKET_OPEN 2 #define SOCKET_ACCEPT 3 -#define SOCKET_ERROR 4 +#define SOCKET_ERR 4 #define SOCKET_EXIT 5 #define SOCKET_UDP 6 From ad36be1bd7169e9f683446630d4b0bb4acb91cff Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 20 Apr 2017 11:26:22 +0800 Subject: [PATCH 693/729] bugfix: http://lua-users.org/lists/lua-l/2017-04/msg00045.html --- 3rd/lua/lparser.c | 1 - 1 file changed, 1 deletion(-) diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 83503a06..8ede1268 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1395,7 +1395,6 @@ static void test_then_block (LexState *ls, int *escapelist) { luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ gotostat(ls, v.t); /* handle goto/break */ - skipnoopstat(ls); /* skip other no-op statements */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ leaveblock(fs); return; /* and that is it */ From 999a94111086d3fcc12a40bc17d04e4825539855 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 20 Apr 2017 17:52:59 +0800 Subject: [PATCH 694/729] destroy bson object when encode error --- lualib-src/lua-bson.c | 62 ++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 725de81b..76daef40 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -551,15 +551,17 @@ static void pack_ordered_dict(lua_State *L, struct bson *b, int n, int depth) { int length = reserve_length(b); int i; + size_t sz; + // the first key is at index n + const char * key = lua_tolstring(L, n, &sz); for (i=0;isize - length, length); @@ -938,36 +940,66 @@ bson_meta(lua_State *L) { lua_setmetatable(L, -2); } +static int +encode_bson(lua_State *L) { + struct bson *b = lua_touserdata(L, 2); + lua_settop(L, 1); + if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { + pack_meta_dict(L, b, 0); + } else { + pack_simple_dict(L, b, 0); + } + void * ud = lua_newuserdata(L, b->size); + memcpy(ud, b->ptr, b->size); + return 1; +} + static int lencode(lua_State *L) { struct bson b; - bson_create(&b); lua_settop(L,1); luaL_checktype(L, 1, LUA_TTABLE); - if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { - pack_meta_dict(L, &b, 0); - } else { - pack_simple_dict(L, &b, 0); + bson_create(&b); + lua_pushcfunction(L, encode_bson); + lua_pushvalue(L, 1); + lua_pushlightuserdata(L, &b); + if (lua_pcall(L, 2, 1, 0) != LUA_OK) { + bson_destroy(&b); + return lua_error(L); } - void * ud = lua_newuserdata(L, b.size); - memcpy(ud, b.ptr, b.size); bson_destroy(&b); bson_meta(L); return 1; } +static int +encode_bson_byorder(lua_State *L) { + int n = lua_gettop(L); + struct bson *b = lua_touserdata(L, n); + lua_settop(L, --n); + pack_ordered_dict(L, b, n, 0); + lua_settop(L,0); + void * ud = lua_newuserdata(L, b->size); + memcpy(ud, b->ptr, b->size); + return 1; +} + static int lencode_order(lua_State *L) { struct bson b; - bson_create(&b); int n = lua_gettop(L); if (n%2 != 0) { return luaL_error(L, "Invalid ordered dict"); } - pack_ordered_dict(L, &b, n, 0); - lua_settop(L,1); - void * ud = lua_newuserdata(L, b.size); - memcpy(ud, b.ptr, b.size); + bson_create(&b); + lua_pushvalue(L, 1); // copy the first arg to n + lua_pushcfunction(L, encode_bson_byorder); + lua_replace(L, 1); + lua_pushlightuserdata(L, &b); + if (lua_pcall(L, n+1, 1, 0) != LUA_OK) { + bson_destroy(&b); + return lua_error(L); + } bson_destroy(&b); bson_meta(L); return 1; From dab5a4e3081f5184914ef6fbae82ebed42c009a4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 20 Apr 2017 18:37:27 +0800 Subject: [PATCH 695/729] mongo.sort --- lualib/mongo.lua | 19 ++++++++++++------- lualib/socketchannel.lua | 2 +- service/console.lua | 2 -- test/testmongodb.lua | 10 +++++++++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 3be7d18a..393f4334 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -396,16 +396,21 @@ function mongo_collection:find(query, selector) } , cursor_meta) end +local function unfold(list, key, ...) + if key == nil then + return list + end + local next_func, t = pairs(key) + local k, v = next_func(t) -- The first key pair + table.insert(list, k) + table.insert(list, v) + return unfold(list, ...) +end + -- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1}) function mongo_cursor:sort(key, key_v, ...) if key_v then - local key_list = {} - for _, kp in ipairs {key, key_v, ...} do - local next_func, t = pairs(kp) - local k, v = next_func(t, v) -- The first key pair - table.insert(key_list, k) - table.insert(key_list, v) - end + local key_list = unfold({}, key, key_v , ...) key = bson_encode_order(table.unpack(key_list)) end self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key} diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index fa72b891..89290ec0 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -158,7 +158,7 @@ local function dispatch_by_order(self) local func, co = pop_response(self) if not co then -- close signal - wakeup_all(self, errmsg) + wakeup_all(self, "channel_closed") break end local ok, result_ok, result_data, padding = pcall(func, self.__sock) diff --git a/service/console.lua b/service/console.lua index 90fe62b0..e0ce1453 100644 --- a/service/console.lua +++ b/service/console.lua @@ -12,7 +12,6 @@ end local function console_main_loop() local stdin = socket.stdin() - socket.lock(stdin) while true do local cmdline = socket.readline(stdin, "\n") local split = split_cmdline(cmdline) @@ -23,7 +22,6 @@ local function console_main_loop() pcall(skynet.newservice, cmdline) end end - socket.unlock(stdin) end skynet.start(function() diff --git a/test/testmongodb.lua b/test/testmongodb.lua index bce37683..e8dc5887 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -7,10 +7,18 @@ local host, db_name = ... function test_insert_without_index() local db = mongo.client({host = host}) + + local r = db:runCommand("buildInfo",1) + for k, v in pairs(r) do + print(k,v) + end db[db_name].testdb:dropIndex("*") db[db_name].testdb:drop() local ret = db[db_name].testdb:safe_insert({test_key = 1}); + for k,v in pairs(ret) do + print(k,v) + end assert(ret and ret.n == 1) local ret = db[db_name].testdb:safe_insert({test_key = 1}); @@ -85,7 +93,7 @@ function test_expire_index() for i = 1, 1000 do skynet.sleep(11); - + local ret = db[db_name].testdb:findOne({test_key = 1}) if ret == nil then return From 77ed1bd68b07358d55cf7fc29eaffe1d311a37f7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 20 Apr 2017 18:48:15 +0800 Subject: [PATCH 696/729] add check for testbson --- test/testbson.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/testbson.lua b/test/testbson.lua index d589f01f..815ffdef 100644 --- a/test/testbson.lua +++ b/test/testbson.lua @@ -1,6 +1,13 @@ local bson = require "bson" -sub = bson.encode_order( "hello", 1, "world", 2 ) +local sub = bson.encode_order( "hello", 1, "world", 2 ) + +do + -- check decode encode_order + local d = bson.decode(sub) + assert(d.hello == 1 ) + assert(d.world == 2 ) +end local function tbl_next(...) print("--- next.a", ...) From 89d771929adec128423ee51ff403d99f73f13b15 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 20 Apr 2017 18:52:02 +0800 Subject: [PATCH 697/729] add print in testmongodb --- test/testmongodb.lua | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/test/testmongodb.lua b/test/testmongodb.lua index e8dc5887..06e2691c 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -7,18 +7,10 @@ local host, db_name = ... function test_insert_without_index() local db = mongo.client({host = host}) - - local r = db:runCommand("buildInfo",1) - for k, v in pairs(r) do - print(k,v) - end db[db_name].testdb:dropIndex("*") db[db_name].testdb:drop() local ret = db[db_name].testdb:safe_insert({test_key = 1}); - for k,v in pairs(ret) do - print(k,v) - end assert(ret and ret.n == 1) local ret = db[db_name].testdb:safe_insert({test_key = 1}); @@ -92,7 +84,7 @@ function test_expire_index() assert(ret and ret.test_key == 1) for i = 1, 1000 do - skynet.sleep(11); + skynet.sleep(1); local ret = db[db_name].testdb:findOne({test_key = 1}) if ret == nil then @@ -104,10 +96,13 @@ function test_expire_index() end skynet.start(function() + print("Test insert without index") test_insert_without_index() + print("Test insert index") test_insert_with_index() + print("Test find and remove") test_find_and_remove() + print("Test expire index") test_expire_index() - print("mongodb test finish."); end) From 3b6b31266d3d3948dd8d32f6602ff7c37fbffa8c Mon Sep 17 00:00:00 2001 From: caijietao Date: Mon, 24 Apr 2017 15:23:12 +0800 Subject: [PATCH 698/729] error report on sharedata newindex --- lualib/sharedata/corelib.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index bd42cf1b..05a14b2f 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -71,6 +71,10 @@ local function getcobj(self) return obj end +function meta:__newindex(key, value) + error ("Error newindex, the key [" .. genkey(self) .. "]") +end + function meta:__index(key) local obj = getcobj(self) local v = index(obj, key) @@ -78,7 +82,7 @@ function meta:__index(key) local children = self.__cache if children == nil then children = {} - self.__cache = children + rawset(self, "__cache", children) end local r = children[key] if r then From 1153873321a0ecfaec0015e397682d9353f8db09 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 24 Apr 2017 15:28:15 +0800 Subject: [PATCH 699/729] minor fix --- lualib/sharedata/corelib.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index 05a14b2f..20bd5506 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -1,7 +1,6 @@ local core = require "sharedata.core" local type = type -local next = next -local rawget = rawget +local rawset = rawset local conf = {} From 3fb46241f2f3bc6ffc2e2be3b2c734573a8e5910 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 25 Apr 2017 11:38:29 +0800 Subject: [PATCH 700/729] raise error when you forget response the request, fix issue #625 --- lualib/skynet.lua | 15 +++++++++++---- lualib/skynet/debug.lua | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 900dc5c1..e4dfdc58 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -92,6 +92,7 @@ local function _error_dispatch(error_session, error_source) if watching_session[error_session] then table.insert(error_queue, error_session) end + session_response[coroutine.running()] = true -- error report don't need response end end @@ -238,10 +239,16 @@ function suspend(co, result, command, param, size) elseif command == "EXIT" then -- coroutine exit local address = session_coroutine_address[co] - release_watching(address) - session_coroutine_id[co] = nil - session_coroutine_address[co] = nil - session_response[co] = nil + local session = session_coroutine_id[co] + if address then + release_watching(address) + if session ~= 0 and not session_response[co] then + error "no response" -- need response + end + session_coroutine_id[co] = nil + session_coroutine_address[co] = nil + session_response[co] = nil + end elseif command == "QUIT" then -- service exit return diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index e267b9a9..f776c96e 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -75,7 +75,7 @@ local function init(skynet, export) end function dbgcmd.LINK() - -- no return, raise error when exit + skynet.response() -- get response , but not return. raise error when exit end return dbgcmd From a7be3320230ab5e3b7077fbe19b980e362a9b8df Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 26 Apr 2017 18:01:05 +0800 Subject: [PATCH 701/729] don't check response, because gate use a fake session for PTYPE_CLIENT --- lualib/skynet.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index e4dfdc58..d8212f18 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -92,7 +92,6 @@ local function _error_dispatch(error_session, error_source) if watching_session[error_session] then table.insert(error_queue, error_session) end - session_response[coroutine.running()] = true -- error report don't need response end end @@ -239,12 +238,8 @@ function suspend(co, result, command, param, size) elseif command == "EXIT" then -- coroutine exit local address = session_coroutine_address[co] - local session = session_coroutine_id[co] if address then release_watching(address) - if session ~= 0 and not session_response[co] then - error "no response" -- need response - end session_coroutine_id[co] = nil session_coroutine_address[co] = nil session_response[co] = nil From cce8f0086a89e13642cf357240f358baf9e7ecc3 Mon Sep 17 00:00:00 2001 From: Stocom Date: Fri, 28 Apr 2017 10:49:30 +0800 Subject: [PATCH 702/729] optimize SOCKET_WARNING msg --- lualib-src/lua-socket.c | 5 +-- lualib/snax/gateserver.lua | 2 +- lualib/socket.lua | 14 ++++----- lualib/socketchannel.lua | 21 ++++++++----- skynet-src/skynet_socket.c | 31 +++++-------------- skynet-src/skynet_socket.h | 2 +- skynet-src/socket_server.c | 62 +++++++++++++++++++++++++++----------- skynet-src/socket_server.h | 7 +++-- 8 files changed, 82 insertions(+), 62 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 39b8107f..bd5cc5b9 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -567,8 +567,9 @@ lsendlow(lua_State *L) { int id = luaL_checkinteger(L, 1); int sz = 0; void *buffer = get_buffer(L, 2, &sz); - skynet_socket_send_lowpriority(ctx, id, buffer, sz); - return 0; + int err = skynet_socket_send_lowpriority(ctx, id, buffer, sz); + lua_pushboolean(L, !err); + return 1; } static int diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index bf62e2ce..f22176bc 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -113,7 +113,7 @@ function gateserver.start(handler) function MSG.error(fd, msg) if fd == socket then socketdriver.close(fd) - skynet.error(msg) + skynet.error("gateserver close listen socket, accpet error:",msg) else if handler.error then handler.error(fd, msg) diff --git a/lualib/socket.lua b/lualib/socket.lua index d7741b54..f9644313 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -138,19 +138,17 @@ end local function default_warning(id, size) local s = socket_pool[id] - local last = s.warningsize or 0 - if last + 64 < size then -- if size increase 64K - s.warningsize = size - skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id)) - end - s.warningsize = size + if not s then + return + end + skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id)) end -- SKYNET_SOCKET_TYPE_WARNING socket_message[7] = function(id, size) local s = socket_pool[id] if s then - local warning = s.warning or default_warning + local warning = s.on_warning or default_warning warning(id, size) end end @@ -442,7 +440,7 @@ socket.udp_address = assert(driver.udp_address) function socket.warning(id, callback) local obj = socket_pool[id] assert(obj) - obj.warning = callback + obj.on_warning = callback end return socket diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 89290ec0..4a0929b3 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -383,17 +383,24 @@ function channel:request(request, response, padding) if padding then -- padding may be a table, to support multi part request -- multi part request use low priority socket write - -- socket_lwrite returns nothing - socket_lwrite(fd , request) - for _,v in ipairs(padding) do - socket_lwrite(fd, v) - end - else - if not socket_write(fd , request) then + -- now socket_lwrite returns as socket_write + local function sock_err() close_channel_socket(self) wakeup_all(self) error(socket_error) end + if not socket_lwrite(fd , request) then + sock_err() + end + for _,v in ipairs(padding) do + if not socket_lwrite(fd, v) then + sock_err() + end + end + else + if not socket_write(fd , request) then + sock_err() + end end if response == nil then diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index af793d35..ef255060 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -98,6 +98,9 @@ skynet_socket_poll() { case SOCKET_UDP: forward_message(SKYNET_SOCKET_TYPE_UDP, false, &result); break; + case SOCKET_WARNING: + forward_message(SKYNET_SOCKET_TYPE_WARNING, false, &result); + break; default: skynet_error(NULL, "Unknown socket message type %d.",type); return -1; @@ -108,31 +111,14 @@ skynet_socket_poll() { return 1; } -static int -check_wsz(struct skynet_context *ctx, int id, void *buffer, int64_t wsz) { - if (wsz < 0) { - return -1; - } else if (wsz > 1024 * 1024) { - struct skynet_socket_message tmp; - tmp.type = SKYNET_SOCKET_TYPE_WARNING; - tmp.id = id; - tmp.ud = (int)(wsz / 1024); - tmp.buffer = NULL; - skynet_send(ctx, 0, skynet_context_handle(ctx), PTYPE_SOCKET, 0 , &tmp, sizeof(tmp)); -// skynet_error(ctx, "%d Mb bytes on socket %d need to send out", (int)(wsz / (1024 * 1024)), id); - } - return 0; +int +skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { + return socket_server_send(SOCKET_SERVER, id, buffer, sz); } int -skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { - int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz); - return check_wsz(ctx, id, buffer, wsz); -} - -void skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) { - socket_server_send_lowpriority(SOCKET_SERVER, id, buffer, sz); + return socket_server_send_lowpriority(SOCKET_SERVER, id, buffer, sz); } int @@ -189,8 +175,7 @@ skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz) { - int64_t wsz = socket_server_udp_send(SOCKET_SERVER, id, (const struct socket_udp_address *)address, buffer, sz); - return check_wsz(ctx, id, (void *)buffer, wsz); + return socket_server_udp_send(SOCKET_SERVER, id, (const struct socket_udp_address *)address, buffer, sz); } const char * diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 55b98595..21dfcef4 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -24,7 +24,7 @@ void skynet_socket_free(); int skynet_socket_poll(); int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz); -void skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz); +int skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz); int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog); int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port); int skynet_socket_bind(struct skynet_context *ctx, int fd); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index ea90aab4..4f381280 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -53,6 +53,8 @@ #define AGAIN_WOULDBLOCK EAGAIN #endif +#define WARNING_SIZE (1024*1024) + struct write_buffer { struct write_buffer * next; void *buffer; @@ -79,6 +81,7 @@ struct socket { int id; uint16_t protocol; uint16_t type; + int64_t warn_size; union { int size; uint8_t udp_address[UDP_ADDRESS_SIZE]; @@ -391,6 +394,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, s->p.size = MIN_READ_BUFFER; s->opaque = opaque; s->wb_size = 0; + s->warn_size = 0; check_wb_list(&s->high); check_wb_list(&s->low); return s; @@ -598,6 +602,11 @@ raise_uncomplete(struct socket * s) { high->head = high->tail = tmp; } +static inline int +send_buffer_empty(struct socket *s) { + return (s->high.head == NULL && s->low.head == NULL); +} + /* Each socket has two write buffer list, high priority and low priority. @@ -622,15 +631,24 @@ send_buffer(struct socket_server *ss, struct socket *s, struct socket_message *r // step 3 if (list_uncomplete(&s->low)) { raise_uncomplete(s); + return -1; } - } else { - // step 4 - sp_write(ss->event_fd, s->fd, s, false); + } + // step 4 + assert(send_buffer_empty(s) && s->wb_size == 0); + sp_write(ss->event_fd, s->fd, s, false); - if (s->type == SOCKET_TYPE_HALFCLOSE) { + if (s->type == SOCKET_TYPE_HALFCLOSE) { force_close(ss, s, result); return SOCKET_CLOSE; - } + } + if(s->warn_size > 0){ + s->warn_size = 0; + result->opaque = s->opaque; + result->id = s->id; + result->ud = 0; + result->data = NULL; + return SOCKET_WARNING; } } @@ -677,10 +695,6 @@ append_sendbuffer_low(struct socket_server *ss,struct socket *s, struct request_ s->wb_size += buf->sz; } -static inline int -send_buffer_empty(struct socket *s) { - return (s->high.head == NULL && s->low.head == NULL); -} /* When send a package , we can assign the priority : PRIORITY_HIGH or PRIORITY_LOW @@ -757,6 +771,14 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock append_sendbuffer_udp(ss,s,priority,request,udp_address); } } + if (s->wb_size >= WARNING_SIZE && s->wb_size >= s->warn_size) { + s->warn_size = s->warn_size == 0 ? WARNING_SIZE *2 : s->warn_size*2; + result->opaque = s->opaque; + result->id = s->id; + result->ud = s->wb_size%1024 == 0 ? s->wb_size/1024 : s->wb_size/1024 + 1; + result->data = NULL; + return SOCKET_WARNING; + } return -1; } @@ -794,7 +816,8 @@ close_socket(struct socket_server *ss, struct request_close *request, struct soc } if (!send_buffer_empty(s)) { int type = send_buffer(ss,s,result); - if (type != -1) + // type : -1 or SOCKET_WARNING or SOCKET_CLOSE, SOCKET_WARNING means send_buffer_empty + if (type != -1 && type != SOCKET_WARNING) return type; } if (request->shutdown || send_buffer_empty(s)) { @@ -1223,6 +1246,9 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int ss->event_index = 0; if (ss->event_n <= 0) { ss->event_n = 0; + if (errno == EINTR) { + continue; + } return -1; } } @@ -1334,8 +1360,8 @@ free_buffer(struct socket_server *ss, const void * buffer, int sz) { so.free_func((void *)buffer); } -// return -1 when error -int64_t +// return -1 when error, 0 when success +int socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) { struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { @@ -1349,15 +1375,16 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz request.u.send.buffer = (char *)buffer; send_request(ss, &request, 'D', sizeof(request.u.send)); - return s->wb_size; + return 0; } -void +// return -1 when error, 0 when success +int socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) { struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { free_buffer(ss, buffer, sz); - return; + return -1; } struct request_package request; @@ -1366,6 +1393,7 @@ socket_server_send_lowpriority(struct socket_server *ss, int id, const void * bu request.u.send.buffer = (char *)buffer; send_request(ss, &request, 'P', sizeof(request.u.send)); + return 0; } void @@ -1546,7 +1574,7 @@ socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, return id; } -int64_t +int socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp_address *addr, const void *buffer, int sz) { struct socket * s = &ss->slot[HASH_ID(id)]; if (s->id != id || s->type == SOCKET_TYPE_INVALID) { @@ -1576,7 +1604,7 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp memcpy(request.u.send_udp.address, udp_address, addrsz); send_request(ss, &request, 'A', sizeof(request.u.send_udp.send)+addrsz); - return s->wb_size; + return 0; } int diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index 31b3c9de..622b086f 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -10,6 +10,7 @@ #define SOCKET_ERR 4 #define SOCKET_EXIT 5 #define SOCKET_UDP 6 +#define SOCKET_WARNING 7 struct socket_server; @@ -30,8 +31,8 @@ void socket_server_shutdown(struct socket_server *, uintptr_t opaque, int id); void socket_server_start(struct socket_server *, uintptr_t opaque, int id); // return -1 when error -int64_t socket_server_send(struct socket_server *, int id, const void * buffer, int sz); -void socket_server_send_lowpriority(struct socket_server *, int id, const void * buffer, int sz); +int socket_server_send(struct socket_server *, int id, const void * buffer, int sz); +int socket_server_send_lowpriority(struct socket_server *, int id, const void * buffer, int sz); // ctrl command below returns id int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int port, int backlog); @@ -50,7 +51,7 @@ int socket_server_udp(struct socket_server *, uintptr_t opaque, const char * add int socket_server_udp_connect(struct socket_server *, int id, const char * addr, int port); // If the socket_udp_address is NULL, use last call socket_server_udp_connect address instead // You can also use socket_server_send -int64_t socket_server_udp_send(struct socket_server *, int id, const struct socket_udp_address *, const void *buffer, int sz); +int socket_server_udp_send(struct socket_server *, int id, const struct socket_udp_address *, const void *buffer, int sz); // extract the address of the message, struct socket_message * should be SOCKET_UDP const struct socket_udp_address * socket_server_udp_address(struct socket_server *, struct socket_message *, int *addrsz); From 73e61a9cc6c3d745f9960a1b32d83e872ae60b0e Mon Sep 17 00:00:00 2001 From: silveratom Date: Fri, 28 Apr 2017 15:17:12 +0800 Subject: [PATCH 703/729] Update socketchannel.lua --- lualib/socketchannel.lua | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 4a0929b3..fc3c246e 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -376,6 +376,12 @@ end local socket_write = socket.write local socket_lwrite = socket.lwrite +local function sock_err(self) + close_channel_socket(self) + wakeup_all(self) + error(socket_error) +end + function channel:request(request, response, padding) assert(block_connect(self, true)) -- connect once local fd = self.__sock[1] @@ -384,22 +390,17 @@ function channel:request(request, response, padding) -- padding may be a table, to support multi part request -- multi part request use low priority socket write -- now socket_lwrite returns as socket_write - local function sock_err() - close_channel_socket(self) - wakeup_all(self) - error(socket_error) - end if not socket_lwrite(fd , request) then - sock_err() + sock_err(self) end for _,v in ipairs(padding) do if not socket_lwrite(fd, v) then - sock_err() + sock_err(self) end end else if not socket_write(fd , request) then - sock_err() + sock_err(self) end end From d4eeb1ff28ed20fbb3d81bdeda6588d27207a5c5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 May 2017 15:29:48 +0800 Subject: [PATCH 704/729] sproto support response nil --- lualib-src/sproto/README.md | 59 ++++++++++++++++++++++++++++--------- lualib-src/sproto/lsproto.c | 10 +++++-- lualib-src/sproto/sproto.c | 13 ++++++++ lualib-src/sproto/sproto.h | 1 + lualib/sprotoparser.lua | 21 +++++++++---- 5 files changed, 83 insertions(+), 21 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index 67366a46..9fd4c3ff 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -82,7 +82,7 @@ lua-cjson | 4.92s | 8.30s | 183 bytes * pbc-lua is a google protocol buffers library https://github.com/cloudwu/pbc * lua-cjson is a json library https://github.com/efelix/lua-cjson -Lua API +Parser ======= ```lua @@ -93,24 +93,50 @@ local parser = require "sprotoparser" The parser is needed for parsing the sproto schema. You can use it to generate binary string offline. The schema text and the parser is not needed when your program is running. +Lua API +======= + ```lua -local sproto = require "sproto.core" +local sproto = require "sproto" +local sprotocore = require "sproto.core" -- optional ``` -* `sproto.newproto(sp)` creates a sproto object by a schema string (generates by parser). -* `sproto.querytype(sp, typename)` queries a type object from a sproto object by typename. -* `sproto.encode(st, luatable)` encodes a lua table by a type object, and generates a string message. -* `sproto.decode(st, message)` decodes a message string generated by sproto.encode with type. -* `sproto.pack(sprotomessage)` packs a string encoded by sproto.encode to reduce the size. -* `sproto.unpack(packedmessage)` unpacks the string packed by sproto.pack. - -The sproto supports protocol tag for RPC. Use `sproto.protocol(tagorname)` to convert the protocol name to the tag id, or convert back from tag id to the name, and returns the request/response message type objects of this protocol. +* `sproto.new(spbin)` creates a sproto object by a schema binary string (generates by parser). +* `sprotocore.newproto(spbin)` creates a sproto c object by a schema binary string (generates by parser). +* `sproto.sharenew(spbin)` share a sproto object from a sproto c object (generates by sprotocore.newproto). +* `sproto.parse(schema)` creares a sproto object by a schema text string (by calling parser.parse) +* `sproto:exist_type(typename)` detect whether a type exist in sproto object. +* `sproto:encode(typename, luatable)` encodes a lua table with typename into a binary string. +* `sproto:decode(typename, blob [,sz])` decodes a binary string generated by sproto.encode with typename. If blob is a lightuserdata (C ptr), sz (integer) is needed. +* `sproto:pencode(typename, luatable)` The same with sproto:encode, but pack (compress) the results. +* `sproto:pdecode(typename, blob [,sz])` The same with sproto.decode, but unpack the blob (generated by sproto:pencode) first. +* `sproto:default(typename, type)` Create a table with default values of typename. Type can be nil , "REQUEST", or "RESPONSE". RPC API ======= There is a lua wrapper for the core API for RPC . +`sproto:host([packagename])` creates a host object to deliver the rpc message. + +`host:dispatch(blob [,sz])` unpack and decode (sproto:pdecode) the binary string with type the host created (packagename). + +If .type is exist, it's a REQUEST message with .type , returns "REQUEST", protoname, message, responser, .ud. The responser is a function for encode the response message. The responser will be nil when .session is not exist. + +If .type is not exist, it's a RESPONSE message for .session . Returns "RESPONSE", .session, message, .ud . + +`host:attach(sprotoobj)` creates a function(protoname, message, session, ud) to pack and encode request message with sprotoobj. + +If you don't want to use host object, you can also use these following apis to encode and decode the rpc message: + +`sproto:request_encode(protoname, tbl)` encode a request message with protoname. + +`sproto:response_encode(protoname, tbl)` encode a response message with protoname. + +`sproto:request_decode(protoname, blob [,sz])` decode a request message with protoname. + +`sproto:response_decode(protoname, blob [,sz]` decode a response message with protoname. + Read testrpc.lua for detail. Schema Language @@ -137,6 +163,8 @@ The schema text is like this: } phone 3 : *PhoneNumber # *PhoneNumber means an array of PhoneNumber. + height 4 : integer(2) # (2) means a 1/100 fixed-point number. + data 5 : binary # Some binary data } .AddressBook { @@ -159,7 +187,7 @@ A schema text can be self-described by the sproto schema language. .field { name 0 : string buildin 1 : integer - type 2 : integer + type 2 : integer # type is fixed-point number precision when buildin is SPROTO_TINTEGER; When buildin is SPROTO_TSTRING, it means binary string when type is 1. tag 3 : integer array 4 : boolean key 5 : integer # If key exists, array must be true, and it's a map. @@ -173,6 +201,7 @@ A schema text can be self-described by the sproto schema language. tag 1 : integer request 2 : integer # index response 3 : integer # index + confirm 4 : boolean # response nil where confirm == true } .group { @@ -184,8 +213,9 @@ A schema text can be self-described by the sproto schema language. Types ======= -* **string** : binary string -* **integer** : integer, the max length of an integer is signed 64bit. +* **string** : string +* **binary** : binary string (it's a sub type of string) +* **integer** : integer, the max length of an integer is signed 64bit. It can be a fixed-point number with specified precision. * **boolean** : true or false You can add * before the typename to declare an array. @@ -196,7 +226,7 @@ User defined type can be any name in alphanumeric characters except the build-in * Where are double or real types? -I have been using Google protocol buffers for many years in many projects, and I found the real types were seldom used. If you really need it, you can use string to serialize the double numbers. +I have been using Google protocol buffers for many years in many projects, and I found the real types were seldom used. If you really need it, you can use string to serialize the double numbers. When you need decimal, you can specify the fixed-point presision. * Where is enum? @@ -436,6 +466,7 @@ struct sproto_arg { int length; int index; // array base 1 int mainindex; // for map + int extra; // SPROTO_TINTEGER: fixed-point presision ; SPROTO_TSTRING 0:utf8 string 1:binary }; typedef int (*sproto_callback)(const struct sproto_arg *args); diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 11b1a67d..70437931 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -275,7 +275,9 @@ lencode(lua_State *L) { int tbl_index = 2; struct sproto_type * st = lua_touserdata(L, 1); if (st == NULL) { - return luaL_argerror(L, 1, "Need a sproto_type object"); + luaL_checktype(L, tbl_index, LUA_TNIL); + lua_pushstring(L, ""); + return 1; // response nil } luaL_checktype(L, tbl_index, LUA_TTABLE); luaL_checkstack(L, ENCODE_DEEPLEVEL*2 + 8, NULL); @@ -573,7 +575,11 @@ lprotocol(lua_State *L) { } response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); if (response == NULL) { - lua_pushnil(L); + if (sproto_protoresponse(sp, tag)) { + lua_pushlightuserdata(L, NULL); // response nil + } else { + lua_pushnil(L); + } } else { lua_pushlightuserdata(L, response); } diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 416f1006..713f1569 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -32,6 +32,7 @@ struct sproto_type { struct protocol { const char *name; int tag; + int confirm; // confirm == 1 where response nil struct sproto_type * p[2]; }; @@ -364,6 +365,7 @@ import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { p->tag = -1; p->p[SPROTO_REQUEST] = NULL; p->p[SPROTO_RESPONSE] = NULL; + p->confirm = 0; tag = 0; for (i=0;ip[SPROTO_RESPONSE] = &s->type[value]; break; + case 4: // confirm + p->confirm = value; + break; default: return NULL; } @@ -542,6 +547,8 @@ sproto_dump(struct sproto *s) { } if (p->p[SPROTO_RESPONSE]) { printf(" response:%s", p->p[SPROTO_RESPONSE]->name); + } else if (p->confirm) { + printf(" response nil"); } printf("\n"); } @@ -590,6 +597,12 @@ sproto_protoquery(const struct sproto *sp, int proto, int what) { return NULL; } +int +sproto_protoresponse(const struct sproto * sp, int proto) { + struct protocol * p = query_proto(sp, proto); + return (p!=NULL && (p->p[SPROTO_RESPONSE] || p->confirm)); +} + const char * sproto_protoname(const struct sproto *sp, int proto) { struct protocol * p = query_proto(sp, proto); diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index cf40497b..f70309ec 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -30,6 +30,7 @@ int sproto_prototag(const struct sproto *, const char * name); const char * sproto_protoname(const struct sproto *, int proto); // SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response struct sproto_type * sproto_protoquery(const struct sproto *, int proto, int what); +int sproto_protoresponse(const struct sproto *, int proto); struct sproto_type * sproto_type(const struct sproto *, const char * type_name); diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 898a1cbc..4c9a7d07 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -104,7 +104,13 @@ function convert.protocol(all, obj) typename = obj[1] .. "." .. p[1] all.type[typename] = convert.type(all, { typename, struct }) end - result[p[1]] = typename + if typename == "nil" then + if p[1] == "response" then + result.confirm = true + end + else + result[p[1]] = typename + end end return result end @@ -259,6 +265,7 @@ end tag 1 : integer request 2 : integer # index response 3 : integer # index + confirm 4 : boolean # true means response nil } .group { @@ -366,18 +373,22 @@ local function packproto(name, p, alltypes) "\0\0", -- name (id=0, ref=0) packvalue(p.tag), -- tag (tag=1) } - if p.request == nil and p.response == nil then - tmp[1] = "\2\0" + if p.request == nil and p.response == nil and p.confirm == nil then + tmp[1] = "\2\0" -- only two fields else if p.request then table.insert(tmp, packvalue(alltypes[p.request].id)) -- request typename (tag=2) else - table.insert(tmp, "\1\0") + table.insert(tmp, "\1\0") -- skip this field (request) end if p.response then table.insert(tmp, packvalue(alltypes[p.response].id)) -- request typename (tag=3) + elseif p.confirm then + tmp[1] = "\5\0" -- add confirm field + table.insert(tmp, "\1\0") -- skip this field (response) + table.insert(tmp, packvalue(1)) -- confirm = true else - tmp[1] = "\3\0" + tmp[1] = "\3\0" -- only three fields end end From 72e3973cf939a1a3e9bb91d97f9376a0a572d312 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 2 May 2017 16:44:43 +0800 Subject: [PATCH 705/729] decode response nil --- lualib-src/sproto/lsproto.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index 70437931..d7160a62 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -451,7 +451,8 @@ ldecode(lua_State *L) { size_t sz; int r; if (st == NULL) { - return luaL_argerror(L, 1, "Need a sproto_type object"); + // return nil + return 0; } sz = 0; buffer = getbuffer(L, 2, &sz); From 4b2efd4785edd8ce4b9bd728b66186834fbee774 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 3 May 2017 13:42:13 +0800 Subject: [PATCH 706/729] add debug info to lua --- 3rd/lua/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/lua/Makefile b/3rd/lua/Makefile index ddf7b349..0b7c8781 100644 --- a/3rd/lua/Makefile +++ b/3rd/lua/Makefile @@ -19,7 +19,7 @@ SYSCFLAGS= SYSLDFLAGS= SYSLIBS= -MYCFLAGS=-I../../skynet-src +MYCFLAGS=-I../../skynet-src -g MYLDFLAGS= MYLIBS= MYOBJS= From ea129cd9f6cf9a220b27d565d49f0645b2a177e0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 4 May 2017 00:59:52 +0800 Subject: [PATCH 707/729] bugfix: skynet.coroutine --- lualib-src/lua-profile.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c index 1167029a..bf0ac74b 100644 --- a/lualib-src/lua-profile.c +++ b/lualib-src/lua-profile.c @@ -107,7 +107,7 @@ lstop(lua_State *L) { total_time += ti; lua_pushnumber(L, total_time); #ifdef DEBUG_LOG - fprintf(stderr, "PROFILE [%p] stop (%lf / %lf)\n", L, ti, total_time); + fprintf(stderr, "PROFILE [%p] stop (%lf / %lf)\n", lua_tothread(L,1), ti, total_time); #endif return 1; @@ -157,14 +157,15 @@ timing_yield(lua_State *L) { #ifdef DEBUG_LOG lua_State *from = lua_tothread(L, -1); #endif + lua_pushvalue(L, -1); lua_rawget(L, lua_upvalueindex(2)); // check total time if (lua_isnil(L, -1)) { - lua_pop(L,1); + lua_pop(L,2); } else { double ti = lua_tonumber(L, -1); lua_pop(L,1); - lua_pushthread(L); + lua_pushvalue(L, -1); // push coroutine lua_rawget(L, lua_upvalueindex(1)); double starttime = lua_tonumber(L, -1); lua_pop(L,1); @@ -175,9 +176,10 @@ timing_yield(lua_State *L) { fprintf(stderr, "PROFILE [%p] yield (%lf/%lf)\n", from, diff, ti); #endif - lua_pushthread(L); + lua_pushvalue(L, -1); // push coroutine lua_pushnumber(L, ti); lua_rawset(L, lua_upvalueindex(2)); + lua_pop(L, 1); // pop coroutine } lua_CFunction co_yield = lua_tocfunction(L, lua_upvalueindex(3)); From acb0c57844a0cf3234846d1c781c82cde6d1a01f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 4 May 2017 16:58:24 +0800 Subject: [PATCH 708/729] profile.resume_co bugfix --- lualib-src/lua-profile.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c index bf0ac74b..6522b9fa 100644 --- a/lualib-src/lua-profile.c +++ b/lualib-src/lua-profile.c @@ -107,7 +107,7 @@ lstop(lua_State *L) { total_time += ti; lua_pushnumber(L, total_time); #ifdef DEBUG_LOG - fprintf(stderr, "PROFILE [%p] stop (%lf / %lf)\n", lua_tothread(L,1), ti, total_time); + fprintf(stderr, "PROFILE [%p] stop (%lf/%lf)\n", lua_tothread(L,1), ti, total_time); #endif return 1; @@ -115,18 +115,15 @@ lstop(lua_State *L) { static int timing_resume(lua_State *L) { -#ifdef DEBUG_LOG - lua_State *from = lua_tothread(L, -1); -#endif + lua_pushvalue(L, -1); lua_rawget(L, lua_upvalueindex(2)); if (lua_isnil(L, -1)) { // check total time - lua_pop(L,1); + lua_pop(L,2); // pop from coroutine } else { lua_pop(L,1); - lua_pushvalue(L,1); double ti = get_time(); #ifdef DEBUG_LOG - fprintf(stderr, "PROFILE [%p] resume\n", from); + fprintf(stderr, "PROFILE [%p] resume %lf\n", lua_tothread(L, -1), ti); #endif lua_pushnumber(L, ti); lua_rawset(L, lua_upvalueindex(1)); // set start time @@ -147,7 +144,7 @@ lresume(lua_State *L) { static int lresume_co(lua_State *L) { luaL_checktype(L, 2, LUA_TTHREAD); - lua_rotate(L, 2, -1); + lua_rotate(L, 2, -1); // 'from' coroutine rotate to the top(index -1) return timing_resume(L); } From 648504119ec0e31a1472606eabfb0004bb162ac8 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 May 2017 11:45:50 +0800 Subject: [PATCH 709/729] bugfix: wakeup sleep --- lualib/http/sockethelper.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 0d8e80fb..0ae6445d 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -71,12 +71,16 @@ function sockethelper.connect(host, port, timeout) local fd if timeout then local drop_fd + local co = coroutine.running() -- asynchronous connect skynet.fork(function() fd = socket.open(host, port) if drop_fd then -- sockethelper.connect already return, and raise socket_error socket.close(fd) + else + -- socket.open before sleep, wakeup. + skynet.wakeup(co) end end) skynet.sleep(timeout) From a504c5e6249042200f31d356fe18dd9f3a9df37e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 May 2017 15:07:00 +0800 Subject: [PATCH 710/729] lua 5.3.4 bugfix --- 3rd/lua/lparser.c | 1 + 1 file changed, 1 insertion(+) diff --git a/3rd/lua/lparser.c b/3rd/lua/lparser.c index 8ede1268..4aa3e356 100644 --- a/3rd/lua/lparser.c +++ b/3rd/lua/lparser.c @@ -1395,6 +1395,7 @@ static void test_then_block (LexState *ls, int *escapelist) { luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ gotostat(ls, v.t); /* handle goto/break */ + while (testnext(ls, ';')) {} /* skip colons */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ leaveblock(fs); return; /* and that is it */ From 7400e51451873d8a46a3d6bd90c5db3bb3e47d4f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 27 May 2017 10:41:29 +0800 Subject: [PATCH 711/729] support error event, see #644 --- skynet-src/socket_epoll.h | 1 + skynet-src/socket_kqueue.h | 9 +++++---- skynet-src/socket_poll.h | 1 + skynet-src/socket_server.c | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/skynet-src/socket_epoll.h b/skynet-src/socket_epoll.h index 2fc8903a..62b1614a 100644 --- a/skynet-src/socket_epoll.h +++ b/skynet-src/socket_epoll.h @@ -59,6 +59,7 @@ sp_wait(int efd, struct event *e, int max) { unsigned flag = ev[i].events; e[i].write = (flag & EPOLLOUT) != 0; e[i].read = (flag & EPOLLIN) != 0; + e[i].error = (flag & EPOLLERR) != 0; } return n; diff --git a/skynet-src/socket_kqueue.h b/skynet-src/socket_kqueue.h index 54884b6a..8aa31203 100644 --- a/skynet-src/socket_kqueue.h +++ b/skynet-src/socket_kqueue.h @@ -38,17 +38,17 @@ static int sp_add(int kfd, int sock, void *ud) { struct kevent ke; EV_SET(&ke, sock, EVFILT_READ, EV_ADD, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { return 1; } EV_SET(&ke, sock, EVFILT_WRITE, EV_ADD, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { EV_SET(&ke, sock, EVFILT_READ, EV_DELETE, 0, 0, NULL); kevent(kfd, &ke, 1, NULL, 0, NULL); return 1; } EV_SET(&ke, sock, EVFILT_WRITE, EV_DISABLE, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { sp_del(kfd, sock); return 1; } @@ -59,7 +59,7 @@ static void sp_write(int kfd, int sock, void *ud, bool enable) { struct kevent ke; EV_SET(&ke, sock, EVFILT_WRITE, enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); - if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1) { + if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { // todo: check error } } @@ -75,6 +75,7 @@ sp_wait(int kfd, struct event *e, int max) { unsigned filter = ev[i].filter; e[i].write = (filter == EVFILT_WRITE); e[i].read = (filter == EVFILT_READ); + e[i].error = false; // kevent has not error event } return n; diff --git a/skynet-src/socket_poll.h b/skynet-src/socket_poll.h index 0f36308a..cfd7e870 100644 --- a/skynet-src/socket_poll.h +++ b/skynet-src/socket_poll.h @@ -9,6 +9,7 @@ struct event { void * s; bool read; bool write; + bool error; }; static bool sp_invalid(poll_fd fd); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 4f381280..3841f233 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1302,6 +1302,21 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int break; return type; } + if (e->error) { + // close when error + int error; + socklen_t len = sizeof(error); + int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); + if (code < 0) { + result->data = strerror(errno); + } else if (error != 0) { + result->data = strerror(error); + } else { + result->data = "Unknown error"; + } + force_close(ss, s, result); + return SOCKET_ERR; + } break; } } From 25763fc70b8ba287259c3fdd533edab51a2b95c5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 31 May 2017 11:00:00 +0800 Subject: [PATCH 712/729] consider EPOLLHUP as EPOLLIN, see #644 --- skynet-src/socket_epoll.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skynet-src/socket_epoll.h b/skynet-src/socket_epoll.h index 62b1614a..cd1f9018 100644 --- a/skynet-src/socket_epoll.h +++ b/skynet-src/socket_epoll.h @@ -58,7 +58,7 @@ sp_wait(int efd, struct event *e, int max) { e[i].s = ev[i].data.ptr; unsigned flag = ev[i].events; e[i].write = (flag & EPOLLOUT) != 0; - e[i].read = (flag & EPOLLIN) != 0; + e[i].read = (flag & (EPOLLIN | EPOLLHUP)) != 0; e[i].error = (flag & EPOLLERR) != 0; } From 24537b3001ed85d04c1271d8c44712146777c22a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 10:46:06 +0800 Subject: [PATCH 713/729] move some module into skynet/ --- examples/agent.lua | 2 +- examples/simpleweb.lua | 2 +- lualib/{ => skynet/db}/mongo.lua | 4 ++-- lualib/{ => skynet/db}/mysql.lua | 2 +- lualib/{ => skynet/db}/redis.lua | 4 ++-- lualib/{ => skynet}/dns.lua | 2 +- lualib/{ => skynet}/socket.lua | 0 lualib/{ => skynet}/socketchannel.lua | 2 +- test/testdns.lua | 2 +- test/testhttp.lua | 2 +- test/testmemlimit.lua | 11 ++++++++++- test/testmongodb.lua | 2 +- test/testmysql.lua | 2 +- test/testpipeline.lua | 2 +- test/testredis.lua | 2 +- test/testredis2.lua | 2 +- test/testsocket.lua | 2 +- test/testudp.lua | 2 +- 18 files changed, 28 insertions(+), 19 deletions(-) rename lualib/{ => skynet/db}/mongo.lua (99%) rename lualib/{ => skynet/db}/mysql.lua (99%) rename lualib/{ => skynet/db}/redis.lua (98%) rename lualib/{ => skynet}/dns.lua (99%) rename lualib/{ => skynet}/socket.lua (100%) rename lualib/{ => skynet}/socketchannel.lua (99%) diff --git a/examples/agent.lua b/examples/agent.lua index 06db1108..c4cfaf27 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local netpack = require "netpack" -local socket = require "socket" +local socket = require "skynet.socket" local sproto = require "sproto" local sprotoloader = require "sprotoloader" diff --git a/examples/simpleweb.lua b/examples/simpleweb.lua index 5d38dfb1..283b7e91 100644 --- a/examples/simpleweb.lua +++ b/examples/simpleweb.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" local urllib = require "http.url" diff --git a/lualib/mongo.lua b/lualib/skynet/db/mongo.lua similarity index 99% rename from lualib/mongo.lua rename to lualib/skynet/db/mongo.lua index 393f4334..d25dc8fb 100644 --- a/lualib/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -1,6 +1,6 @@ local bson = require "bson" -local socket = require "socket" -local socketchannel = require "socketchannel" +local socket = require "skynet.socket" +local socketchannel = require "skynet.socketchannel" local skynet = require "skynet" local driver = require "mongo.driver" local md5 = require "md5" diff --git a/lualib/mysql.lua b/lualib/skynet/db/mysql.lua similarity index 99% rename from lualib/mysql.lua rename to lualib/skynet/db/mysql.lua index afa76425..e6f8a347 100644 --- a/lualib/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -4,7 +4,7 @@ -- The license is under the BSD license. -- Modified by Cloud Wu (remove bit32 for lua 5.3) -local socketchannel = require "socketchannel" +local socketchannel = require "skynet.socketchannel" local mysqlaux = require "mysqlaux.c" local crypt = require "crypt" diff --git a/lualib/redis.lua b/lualib/skynet/db/redis.lua similarity index 98% rename from lualib/redis.lua rename to lualib/skynet/db/redis.lua index 949023a2..e838a978 100644 --- a/lualib/redis.lua +++ b/lualib/skynet/db/redis.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local socket = require "socket" -local socketchannel = require "socketchannel" +local socket = require "skynet.socket" +local socketchannel = require "skynet.socketchannel" local table = table local string = string diff --git a/lualib/dns.lua b/lualib/skynet/dns.lua similarity index 99% rename from lualib/dns.lua rename to lualib/skynet/dns.lua index 76f363f0..0b96f214 100644 --- a/lualib/dns.lua +++ b/lualib/skynet/dns.lua @@ -65,7 +65,7 @@ --]] local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" local MAX_DOMAIN_LEN = 1024 local MAX_LABEL_LEN = 63 diff --git a/lualib/socket.lua b/lualib/skynet/socket.lua similarity index 100% rename from lualib/socket.lua rename to lualib/skynet/socket.lua diff --git a/lualib/socketchannel.lua b/lualib/skynet/socketchannel.lua similarity index 99% rename from lualib/socketchannel.lua rename to lualib/skynet/socketchannel.lua index fc3c246e..751a29a6 100644 --- a/lualib/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" local socketdriver = require "socketdriver" -- channel support auto reconnect , and capture socket error in request/response transaction diff --git a/test/testdns.lua b/test/testdns.lua index 1d767ce0..01ac414a 100644 --- a/test/testdns.lua +++ b/test/testdns.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local dns = require "dns" +local dns = require "skynet.dns" skynet.start(function() print("nameserver:", dns.server()) -- set nameserver diff --git a/test/testhttp.lua b/test/testhttp.lua index 4098b629..f9524930 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local httpc = require "http.httpc" -local dns = require "dns" +local dns = require "skynet.dns" local function main() httpc.dns() -- set dns server diff --git a/test/testmemlimit.lua b/test/testmemlimit.lua index 5c0c10d5..c1a7e63e 100644 --- a/test/testmemlimit.lua +++ b/test/testmemlimit.lua @@ -1,6 +1,15 @@ local skynet = require "skynet" -local names = {"cluster", "dns", "mongo", "mysql", "redis", "sharedata", "socket", "sproto"} +local names = { + "cluster", + "skynet.db.dns", + "skynet.db.mongo", + "skynet.db.mysql", + "skynet.db.redis", + "sharedata", + "skynet.socket", + "sproto" +} -- set sandbox memory limit to 1M, must set here (at start, out of skynet.start) skynet.memlimit(1 * 1024 * 1024) diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 06e2691c..138a970b 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local mongo = require "mongo" +local mongo = require "skynet.db.mongo" local bson = require "bson" local host, db_name = ... diff --git a/test/testmysql.lua b/test/testmysql.lua index e8b420aa..0b11aeb7 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local mysql = require "mysql" +local mysql = require "skynet.db.mysql" local function dump(obj) local getIndent, quoteStr, wrapKey, wrapVal, dumpObj diff --git a/test/testpipeline.lua b/test/testpipeline.lua index d5c57c8d..b2eea2f1 100644 --- a/test/testpipeline.lua +++ b/test/testpipeline.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local redis = require "redis" +local redis = require "skynet.db.redis" local conf = { host = "127.0.0.1", diff --git a/test/testredis.lua b/test/testredis.lua index 31a13c5a..1b692e1f 100644 --- a/test/testredis.lua +++ b/test/testredis.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local redis = require "redis" +local redis = require "skynet.db.redis" local conf = { host = "127.0.0.1" , diff --git a/test/testredis2.lua b/test/testredis2.lua index f12d8100..d3d50961 100644 --- a/test/testredis2.lua +++ b/test/testredis2.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local redis = require "redis" +local redis = require "skynet.db.redis" local db diff --git a/test/testsocket.lua b/test/testsocket.lua index 3893cd61..179df1a8 100644 --- a/test/testsocket.lua +++ b/test/testsocket.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" local mode , id = ... diff --git a/test/testudp.lua b/test/testudp.lua index fb8f7109..e62f4067 100644 --- a/test/testudp.lua +++ b/test/testudp.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" local function server() local host From 8a6243d2a04186b2f1310b3fceecab834b3fc1e1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 10:51:02 +0800 Subject: [PATCH 714/729] rename socket to skynet.socket --- lualib/http/sockethelper.lua | 2 +- lualib/snax/loginserver.lua | 2 +- service/clusterd.lua | 4 ++-- service/cmaster.lua | 2 +- service/console.lua | 2 +- service/cslave.lua | 2 +- service/debug_console.lua | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 0ae6445d..f5d6ae80 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -1,4 +1,4 @@ -local socket = require "socket" +local socket = require "skynet.socket" local skynet = require "skynet" local readbytes = socket.read diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index cd74fcb5..9ac39090 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" require "skynet.manager" -local socket = require "socket" +local socket = require "skynet.socket" local crypt = require "crypt" local table = table local string = string diff --git a/service/clusterd.lua b/service/clusterd.lua index 911b27d1..1a34c45f 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local sc = require "socketchannel" -local socket = require "socket" +local sc = require "skynet.socketchannel" +local socket = require "skynet.socket" local cluster = require "cluster.core" local config_name = skynet.getenv "cluster" diff --git a/service/cmaster.lua b/service/cmaster.lua index eeb9461e..91c75b4b 100644 --- a/service/cmaster.lua +++ b/service/cmaster.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" --[[ master manage data : diff --git a/service/console.lua b/service/console.lua index e0ce1453..f7d1c6bf 100644 --- a/service/console.lua +++ b/service/console.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local snax = require "snax" -local socket = require "socket" +local socket = require "skynet.socket" local function split_cmdline(cmdline) local split = {} diff --git a/service/cslave.lua b/service/cslave.lua index 559d02b3..452dbaf2 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local socket = require "socket" +local socket = require "skynet.socket" require "skynet.manager" -- import skynet.launch, ... local table = table diff --git a/service/debug_console.lua b/service/debug_console.lua index ec4ea482..81042286 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" local codecache = require "skynet.codecache" local core = require "skynet.core" -local socket = require "socket" +local socket = require "skynet.socket" local snax = require "snax" local memory = require "memory" local httpd = require "http.httpd" From f1ed4820012e4dabeabe384bcc261ef3743e2019 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 11:29:07 +0800 Subject: [PATCH 715/729] move to skynet module --- Makefile | 45 ++++++++++++++--------- examples/agent.lua | 2 +- examples/client.lua | 2 +- examples/cluster1.lua | 2 +- examples/cluster2.lua | 2 +- lualib/http/httpc.lua | 2 +- lualib/skynet.lua | 2 +- lualib/{ => skynet}/cluster.lua | 0 lualib/{ => skynet}/datacenter.lua | 0 lualib/skynet/db/mongo.lua | 4 +- lualib/skynet/db/mysql.lua | 4 +- lualib/{ => skynet}/mqueue.lua | 0 lualib/{ => skynet}/multicast.lua | 2 +- lualib/skynet/reload.lua | 9 +++++ lualib/skynet/remotedebug.lua | 4 +- lualib/{ => skynet}/sharedata.lua | 2 +- lualib/{ => skynet}/sharedata/corelib.lua | 2 +- lualib/{ => skynet}/sharemap.lua | 2 +- lualib/skynet/socket.lua | 2 +- lualib/skynet/socketchannel.lua | 2 +- service/bootstrap.lua | 2 +- service/clusterd.lua | 2 +- service/clusterproxy.lua | 2 +- service/cmemory.lua | 2 +- service/debug_agent.lua | 2 +- service/debug_console.lua | 2 +- service/gate.lua | 2 +- service/multicastd.lua | 4 +- service/sharedatad.lua | 2 +- service/snaxd.lua | 2 +- test/testcoroutine.lua | 2 +- test/testdatacenter.lua | 2 +- test/testmulticast.lua | 4 +- test/testmulticast2.lua | 4 +- test/testsha.lua | 2 +- test/testsm.lua | 2 +- test/teststm.lua | 2 +- 37 files changed, 75 insertions(+), 55 deletions(-) rename lualib/{ => skynet}/cluster.lua (100%) rename lualib/{ => skynet}/datacenter.lua (100%) rename lualib/{ => skynet}/mqueue.lua (100%) rename lualib/{ => skynet}/multicast.lua (98%) create mode 100644 lualib/skynet/reload.lua rename lualib/{ => skynet}/sharedata.lua (96%) rename lualib/{ => skynet}/sharedata/corelib.lua (98%) rename lualib/{ => skynet}/sharemap.lua (97%) diff --git a/Makefile b/Makefile index 08259a63..de69e05a 100644 --- a/Makefile +++ b/Makefile @@ -45,10 +45,21 @@ update3rd : # skynet CSERVICE = snlua logger gate harbor -LUA_CLIB = skynet socketdriver bson mongo md5 netpack \ - clientsocket memory profile multicast \ - cluster crypt sharedata stm sproto lpeg \ - mysqlaux debugchannel +LUA_CLIB = skynet \ + skynet.socketdriver \ + skynet.mongo \ + skynet.netpack \ + skynet.mysqlaux \ + skynet.debugchannel \ + skynet.profile \ + skynet.cluster \ + skynet.sharedata \ + skynet.multicast \ + skynet.memory \ + skynet.crypt \ + skynet.stm \ + skynet.clientsocket \ + bson md5 sproto lpeg SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ @@ -79,43 +90,43 @@ $(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v)))) $(LUA_CLIB_PATH)/skynet.so : lualib-src/lua-skynet.c lualib-src/lua-seri.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src -Ilualib-src -$(LUA_CLIB_PATH)/socketdriver.so : lualib-src/lua-socket.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.socketdriver.so : lualib-src/lua-socket.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -Iskynet-src -$(LUA_CLIB_PATH)/mongo.so : lualib-src/lua-mongo.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.mongo.so : lualib-src/lua-mongo.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/compat-5.2.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lua-md5 $^ -o $@ -$(LUA_CLIB_PATH)/netpack.so : lualib-src/lua-netpack.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.netpack.so : lualib-src/lua-netpack.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -Iskynet-src -o $@ -$(LUA_CLIB_PATH)/clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread -$(LUA_CLIB_PATH)/memory.so : lualib-src/lua-memory.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.memory.so : lualib-src/lua-memory.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -$(LUA_CLIB_PATH)/profile.so : lualib-src/lua-profile.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.profile.so : lualib-src/lua-profile.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -$(LUA_CLIB_PATH)/multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -$(LUA_CLIB_PATH)/cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -$(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.crypt.so : lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -$(LUA_CLIB_PATH)/sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -$(LUA_CLIB_PATH)/stm.so : lualib-src/lua-stm.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.stm.so : lualib-src/lua-stm.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c | $(LUA_CLIB_PATH) @@ -124,10 +135,10 @@ $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsprot $(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ -$(LUA_CLIB_PATH)/mysqlaux.so : lualib-src/lua-mysqlaux.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.mysqlaux.so : lualib-src/lua-mysqlaux.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -$(LUA_CLIB_PATH)/debugchannel.so : lualib-src/lua-debugchannel.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.debugchannel.so : lualib-src/lua-debugchannel.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ clean : diff --git a/examples/agent.lua b/examples/agent.lua index c4cfaf27..3e164bb8 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local netpack = require "netpack" +local netpack = require "skynet.netpack" local socket = require "skynet.socket" local sproto = require "sproto" local sprotoloader = require "sprotoloader" diff --git a/examples/client.lua b/examples/client.lua index 92aaa16e..52836c5f 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -5,7 +5,7 @@ if _VERSION ~= "Lua 5.3" then error "Use lua 5.3" end -local socket = require "clientsocket" +local socket = require "skynet.clientsocket" local proto = require "proto" local sproto = require "sproto" diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 70d20044..16e01b93 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local cluster = require "cluster" +local cluster = require "skynet.cluster" local snax = require "snax" skynet.start(function() diff --git a/examples/cluster2.lua b/examples/cluster2.lua index 22c9cf7f..f356357c 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local cluster = require "cluster" +local cluster = require "skynet.cluster" skynet.start(function() -- query name "sdb" of cluster db. diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index 43a44e19..79e7db74 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -2,7 +2,7 @@ local skynet = require "skynet" local socket = require "http.sockethelper" local url = require "http.url" local internal = require "http.internal" -local dns = require "dns" +local dns = require "skynet.dns" local string = string local table = table diff --git a/lualib/skynet.lua b/lualib/skynet.lua index d8212f18..caa07db6 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -8,7 +8,7 @@ local pairs = pairs local pcall = pcall local table = table -local profile = require "profile" +local profile = require "skynet.profile" local coroutine_resume = profile.resume local coroutine_yield = profile.yield diff --git a/lualib/cluster.lua b/lualib/skynet/cluster.lua similarity index 100% rename from lualib/cluster.lua rename to lualib/skynet/cluster.lua diff --git a/lualib/datacenter.lua b/lualib/skynet/datacenter.lua similarity index 100% rename from lualib/datacenter.lua rename to lualib/skynet/datacenter.lua diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index d25dc8fb..e77f7499 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -2,9 +2,9 @@ local bson = require "bson" local socket = require "skynet.socket" local socketchannel = require "skynet.socketchannel" local skynet = require "skynet" -local driver = require "mongo.driver" +local driver = require "skynet.mongo.driver" local md5 = require "md5" -local crypt = require "crypt" +local crypt = require "skynet.crypt" local rawget = rawget local assert = assert local table = table diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index e6f8a347..b5d97e2c 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -5,8 +5,8 @@ -- Modified by Cloud Wu (remove bit32 for lua 5.3) local socketchannel = require "skynet.socketchannel" -local mysqlaux = require "mysqlaux.c" -local crypt = require "crypt" +local mysqlaux = require "skynet.mysqlaux.c" +local crypt = require "skynet.crypt" local sub = string.sub diff --git a/lualib/mqueue.lua b/lualib/skynet/mqueue.lua similarity index 100% rename from lualib/mqueue.lua rename to lualib/skynet/mqueue.lua diff --git a/lualib/multicast.lua b/lualib/skynet/multicast.lua similarity index 98% rename from lualib/multicast.lua rename to lualib/skynet/multicast.lua index 181979cb..4b09044f 100644 --- a/lualib/multicast.lua +++ b/lualib/skynet/multicast.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local mc = require "multicast.core" +local mc = require "skynet.multicast.core" local multicastd local multicast = {} diff --git a/lualib/skynet/reload.lua b/lualib/skynet/reload.lua new file mode 100644 index 00000000..33e234b0 --- /dev/null +++ b/lualib/skynet/reload.lua @@ -0,0 +1,9 @@ +local core = require "skynet.reload.core" +local skynet = require "skynet" + +local function reload(...) + local args = SERVICE_NAME .. " " .. table.concat({...}, " ") + print(args) +end + +return reload diff --git a/lualib/skynet/remotedebug.lua b/lualib/skynet/remotedebug.lua index 72a06809..d0c99e27 100644 --- a/lualib/skynet/remotedebug.lua +++ b/lualib/skynet/remotedebug.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local debugchannel = require "debugchannel" -local socketdriver = require "socketdriver" +local debugchannel = require "skynet.debugchannel" +local socketdriver = require "skynet.socketdriver" local injectrun = require "skynet.injectcode" local table = table local debug = debug diff --git a/lualib/sharedata.lua b/lualib/skynet/sharedata.lua similarity index 96% rename from lualib/sharedata.lua rename to lualib/skynet/sharedata.lua index a17a20ba..b955bd90 100644 --- a/lualib/sharedata.lua +++ b/lualib/skynet/sharedata.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local sd = require "sharedata.corelib" +local sd = require "skynet.sharedata.corelib" local service diff --git a/lualib/sharedata/corelib.lua b/lualib/skynet/sharedata/corelib.lua similarity index 98% rename from lualib/sharedata/corelib.lua rename to lualib/skynet/sharedata/corelib.lua index 20bd5506..cc49c2d1 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/skynet/sharedata/corelib.lua @@ -1,4 +1,4 @@ -local core = require "sharedata.core" +local core = require "skynet.sharedata.core" local type = type local rawset = rawset diff --git a/lualib/sharemap.lua b/lualib/skynet/sharemap.lua similarity index 97% rename from lualib/sharemap.lua rename to lualib/skynet/sharemap.lua index 6f9b1bdf..6d6d16b3 100644 --- a/lualib/sharemap.lua +++ b/lualib/skynet/sharemap.lua @@ -1,4 +1,4 @@ -local stm = require "stm" +local stm = require "skynet.stm" local sprotoloader = require "sprotoloader" local sproto = require "sproto" local setmetatable = setmetatable diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index f9644313..26524d90 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -1,4 +1,4 @@ -local driver = require "socketdriver" +local driver = require "skynet.socketdriver" local skynet = require "skynet" local skynet_core = require "skynet.core" local assert = assert diff --git a/lualib/skynet/socketchannel.lua b/lualib/skynet/socketchannel.lua index 751a29a6..a6474a81 100644 --- a/lualib/skynet/socketchannel.lua +++ b/lualib/skynet/socketchannel.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local socket = require "skynet.socket" -local socketdriver = require "socketdriver" +local socketdriver = require "skynet.socketdriver" -- channel support auto reconnect , and capture socket error in request/response transaction -- { host = "", port = , auth = function(so) , response = function(so) session, data } diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 90a542cb..3dabfd8a 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" require "skynet.manager" -- import skynet.launch, ... -local memory = require "memory" +local memory = require "skynet.memory" skynet.start(function() local sharestring = tonumber(skynet.getenv "sharestring" or 4096) diff --git a/service/clusterd.lua b/service/clusterd.lua index 1a34c45f..595b7973 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" local sc = require "skynet.socketchannel" local socket = require "skynet.socket" -local cluster = require "cluster.core" +local cluster = require "skynet.cluster.core" local config_name = skynet.getenv "cluster" local node_address = {} diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index 1e55cff6..e0cc5daa 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local cluster = require "cluster" +local cluster = require "skynet.cluster" require "skynet.manager" -- inject skynet.forward_type local node, address = ... diff --git a/service/cmemory.lua b/service/cmemory.lua index e33eb42f..2d429b3c 100644 --- a/service/cmemory.lua +++ b/service/cmemory.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local memory = require "memory" +local memory = require "skynet.memory" memory.dumpinfo() --memory.dump() diff --git a/service/debug_agent.lua b/service/debug_agent.lua index 451e2215..500a34ee 100644 --- a/service/debug_agent.lua +++ b/service/debug_agent.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local debugchannel = require "debugchannel" +local debugchannel = require "skynet.debugchannel" local CMD = {} diff --git a/service/debug_console.lua b/service/debug_console.lua index 81042286..986efae5 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -3,7 +3,7 @@ local codecache = require "skynet.codecache" local core = require "skynet.core" local socket = require "skynet.socket" local snax = require "snax" -local memory = require "memory" +local memory = require "skynet.memory" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" diff --git a/service/gate.lua b/service/gate.lua index f61eed6e..e3a210cc 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local gateserver = require "snax.gateserver" -local netpack = require "netpack" +local netpack = require "skynet.netpack" local watchdog local connection = {} -- fd -> connection : { fd , client, agent , ip, mode } diff --git a/service/multicastd.lua b/service/multicastd.lua index 7f8506af..bcc5ea97 100644 --- a/service/multicastd.lua +++ b/service/multicastd.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local mc = require "multicast.core" -local datacenter = require "datacenter" +local mc = require "skynet.multicast.core" +local datacenter = require "skynet.datacenter" local harbor_id = skynet.harbor(skynet.self()) diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 9888216b..3877ec39 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local sharedata = require "sharedata.corelib" +local sharedata = require "skynet.sharedata.corelib" local table = table local cache = require "skynet.codecache" cache.mode "OFF" -- turn off codecache, because CMD.new may load data file diff --git a/service/snaxd.lua b/service/snaxd.lua index 429d6876..4c9e98f9 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" local c = require "skynet.core" local snax_interface = require "snax.interface" -local profile = require "profile" +local profile = require "skynet.profile" local snax = require "snax" local snax_name = tostring(...) diff --git a/test/testcoroutine.lua b/test/testcoroutine.lua index 5a698575..910bcc0f 100644 --- a/test/testcoroutine.lua +++ b/test/testcoroutine.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" -- You should use skynet.coroutine instead of origin coroutine in skynet local coroutine = require "skynet.coroutine" -local profile = require "profile" +local profile = require "skynet.profile" local function status(co) repeat diff --git a/test/testdatacenter.lua b/test/testdatacenter.lua index 194d2219..44eb3875 100644 --- a/test/testdatacenter.lua +++ b/test/testdatacenter.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local datacenter = require "datacenter" +local datacenter = require "skynet.datacenter" local function f1() print("====1==== wait hello") diff --git a/test/testmulticast.lua b/test/testmulticast.lua index 8f084abf..21303457 100644 --- a/test/testmulticast.lua +++ b/test/testmulticast.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local mc = require "multicast" -local dc = require "datacenter" +local mc = require "skynet.multicast" +local dc = require "skynet.datacenter" local mode = ... diff --git a/test/testmulticast2.lua b/test/testmulticast2.lua index ff1342c2..545b83f5 100644 --- a/test/testmulticast2.lua +++ b/test/testmulticast2.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local dc = require "datacenter" -local mc = require "multicast" +local dc = require "skynet.datacenter" +local mc = require "skynet.multicast" skynet.start(function() print("remote start") diff --git a/test/testsha.lua b/test/testsha.lua index d0ed1997..3be749ae 100644 --- a/test/testsha.lua +++ b/test/testsha.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local crypt = require "crypt" +local crypt = require "skynet.crypt" local function sha1(text) local c = crypt.sha1(text) diff --git a/test/testsm.lua b/test/testsm.lua index 626952f8..bcc369d1 100644 --- a/test/testsm.lua +++ b/test/testsm.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local sharemap = require "sharemap" +local sharemap = require "skynet.sharemap" local mode = ... diff --git a/test/teststm.lua b/test/teststm.lua index 7416a9f2..1d834b35 100644 --- a/test/teststm.lua +++ b/test/teststm.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local stm = require "stm" +local stm = require "skynet.stm" local mode = ... From f9f009a4c1c8a21efe4b97d416cfca86649db322 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 11:56:16 +0800 Subject: [PATCH 716/729] add prefix skynet to c module --- Makefile | 70 ++++++++++------------------------- lualib-src/lua-cluster.c | 2 +- lualib-src/lua-crypt.c | 2 +- lualib-src/lua-debugchannel.c | 2 +- lualib-src/lua-memory.c | 2 +- lualib-src/lua-mongo.c | 2 +- lualib-src/lua-multicast.c | 2 +- lualib-src/lua-mysqlaux.c | 2 +- lualib-src/lua-netpack.c | 2 +- lualib-src/lua-profile.c | 2 +- lualib-src/lua-sharedata.c | 2 +- lualib-src/lua-socket.c | 2 +- lualib-src/lua-stm.c | 2 +- lualib/snax/gateserver.lua | 4 +- lualib/snax/loginserver.lua | 2 +- lualib/snax/msgserver.lua | 6 +-- 16 files changed, 37 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index de69e05a..b4a94090 100644 --- a/Makefile +++ b/Makefile @@ -46,21 +46,25 @@ update3rd : CSERVICE = snlua logger gate harbor LUA_CLIB = skynet \ - skynet.socketdriver \ - skynet.mongo \ - skynet.netpack \ - skynet.mysqlaux \ - skynet.debugchannel \ - skynet.profile \ - skynet.cluster \ - skynet.sharedata \ - skynet.multicast \ - skynet.memory \ - skynet.crypt \ - skynet.stm \ - skynet.clientsocket \ + clientsocket \ bson md5 sproto lpeg +LUA_CLIB_SKYNET = \ + lua-skynet.c lua-seri.c \ + lua-socket.c \ + lua-mongo.c \ + lua-netpack.c \ + lua-memory.c \ + lua-profile.c \ + lua-multicast.c \ + lua-cluster.c \ + lua-crypt.c lsha1.c \ + lua-sharedata.c \ + lua-stm.c \ + lua-mysqlaux.c \ + lua-debugchannel.c \ + \ + SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \ @@ -87,60 +91,24 @@ endef $(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v)))) -$(LUA_CLIB_PATH)/skynet.so : lualib-src/lua-skynet.c lualib-src/lua-seri.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/skynet.so : $(addprefix lualib-src/,$(LUA_CLIB_SKYNET)) | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src -Ilualib-src -$(LUA_CLIB_PATH)/skynet.socketdriver.so : lualib-src/lua-socket.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src - $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ -Iskynet-src -$(LUA_CLIB_PATH)/skynet.mongo.so : lualib-src/lua-mongo.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src - $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/compat-5.2.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lua-md5 $^ -o $@ -$(LUA_CLIB_PATH)/skynet.netpack.so : lualib-src/lua-netpack.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -Iskynet-src -o $@ - -$(LUA_CLIB_PATH)/skynet.clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread -$(LUA_CLIB_PATH)/skynet.memory.so : lualib-src/lua-memory.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ - -$(LUA_CLIB_PATH)/skynet.profile.so : lualib-src/lua-profile.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ - -$(LUA_CLIB_PATH)/skynet.multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ - -$(LUA_CLIB_PATH)/skynet.cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ - -$(LUA_CLIB_PATH)/skynet.crypt.so : lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ - -$(LUA_CLIB_PATH)/skynet.sharedata.so : lualib-src/lua-sharedata.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ - -$(LUA_CLIB_PATH)/skynet.stm.so : lualib-src/lua-stm.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ - $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@ $(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ -$(LUA_CLIB_PATH)/skynet.mysqlaux.so : lualib-src/lua-mysqlaux.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ - -$(LUA_CLIB_PATH)/skynet.debugchannel.so : lualib-src/lua-debugchannel.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ - clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so diff --git a/lualib-src/lua-cluster.c b/lualib-src/lua-cluster.c index 7975c020..84d6332c 100644 --- a/lualib-src/lua-cluster.c +++ b/lualib-src/lua-cluster.c @@ -508,7 +508,7 @@ lconcat(lua_State *L) { } LUAMOD_API int -luaopen_cluster_core(lua_State *L) { +luaopen_skynet_cluster_core(lua_State *L) { luaL_Reg l[] = { { "packrequest", lpackrequest }, { "packpush", lpackpush }, diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 184f5f57..7a182db7 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -904,7 +904,7 @@ int lsha1(lua_State *L); int lhmac_sha1(lua_State *L); LUAMOD_API int -luaopen_crypt(lua_State *L) { +luaopen_skynet_crypt(lua_State *L) { luaL_checkversion(L); static int init = 0; if (!init) { diff --git a/lualib-src/lua-debugchannel.c b/lualib-src/lua-debugchannel.c index a07da360..7f83c9e9 100644 --- a/lualib-src/lua-debugchannel.c +++ b/lualib-src/lua-debugchannel.c @@ -271,7 +271,7 @@ static int db_sethook (lua_State *L) { } LUAMOD_API int -luaopen_debugchannel(lua_State *L) { +luaopen_skynet_debugchannel(lua_State *L) { luaL_Reg l[] = { { "create", lcreate }, // for write { "connect", lconnect }, // for read diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index df771e2e..5fc75752 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -50,7 +50,7 @@ lcurrent(lua_State *L) { } LUAMOD_API int -luaopen_memory(lua_State *L) { +luaopen_skynet_memory(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index cdaa1521..a8ca0b0c 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -532,7 +532,7 @@ reply_length(lua_State *L) { } LUAMOD_API int -luaopen_mongo_driver(lua_State *L) { +luaopen_skynet_mongo_driver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] ={ { "query", op_query }, diff --git a/lualib-src/lua-multicast.c b/lualib-src/lua-multicast.c index 16e5ed08..8903b36e 100644 --- a/lualib-src/lua-multicast.c +++ b/lualib-src/lua-multicast.c @@ -146,7 +146,7 @@ mc_nextid(lua_State *L) { } LUAMOD_API int -luaopen_multicast_core(lua_State *L) { +luaopen_skynet_multicast_core(lua_State *L) { luaL_Reg l[] = { { "pack", mc_packlocal }, { "unpack", mc_unpacklocal }, diff --git a/lualib-src/lua-mysqlaux.c b/lualib-src/lua-mysqlaux.c index bee93edf..46fbc67d 100644 --- a/lualib-src/lua-mysqlaux.c +++ b/lualib-src/lua-mysqlaux.c @@ -158,7 +158,7 @@ static struct luaL_Reg mysqlauxlib[] = { }; -LUAMOD_API int luaopen_mysqlaux_c (lua_State *L) { +LUAMOD_API int luaopen_skynet_mysqlaux_c (lua_State *L) { lua_newtable(L); luaL_setfuncs(L, mysqlauxlib, 0); return 1; diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index fb62f221..7f9a7b64 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -465,7 +465,7 @@ ltostring(lua_State *L) { } LUAMOD_API int -luaopen_netpack(lua_State *L) { +luaopen_skynet_netpack(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "pop", lpop }, diff --git a/lualib-src/lua-profile.c b/lualib-src/lua-profile.c index 6522b9fa..9d9b261d 100644 --- a/lualib-src/lua-profile.c +++ b/lualib-src/lua-profile.c @@ -200,7 +200,7 @@ lyield_co(lua_State *L) { } LUAMOD_API int -luaopen_profile(lua_State *L) { +luaopen_skynet_profile(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "start", lstart }, diff --git a/lualib-src/lua-sharedata.c b/lualib-src/lua-sharedata.c index 9d49411e..16b20872 100644 --- a/lualib-src/lua-sharedata.c +++ b/lualib-src/lua-sharedata.c @@ -765,7 +765,7 @@ lupdate(lua_State *L) { } LUAMOD_API int -luaopen_sharedata_core(lua_State *L) { +luaopen_skynet_sharedata_core(lua_State *L) { luaL_Reg l[] = { // used by host { "new", lnewconf }, diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index bd5cc5b9..c39f85dd 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -678,7 +678,7 @@ ludp_address(lua_State *L) { } LUAMOD_API int -luaopen_socketdriver(lua_State *L) { +luaopen_skynet_socketdriver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "buffer", lnewbuffer }, diff --git a/lualib-src/lua-stm.c b/lualib-src/lua-stm.c index e5a2fe1e..3089594b 100644 --- a/lualib-src/lua-stm.c +++ b/lualib-src/lua-stm.c @@ -235,7 +235,7 @@ lread(lua_State *L) { } LUAMOD_API int -luaopen_stm(lua_State *L) { +luaopen_skynet_stm(lua_State *L) { luaL_checkversion(L); lua_createtable(L, 0, 3); diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index f22176bc..61e7c97e 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local netpack = require "netpack" -local socketdriver = require "socketdriver" +local netpack = require "skynet.netpack" +local socketdriver = require "skynet.socketdriver" local gateserver = {} diff --git a/lualib/snax/loginserver.lua b/lualib/snax/loginserver.lua index 9ac39090..be88b97a 100644 --- a/lualib/snax/loginserver.lua +++ b/lualib/snax/loginserver.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" require "skynet.manager" local socket = require "skynet.socket" -local crypt = require "crypt" +local crypt = require "skynet.crypt" local table = table local string = string local assert = assert diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua index 765387cc..4c6f1724 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -1,8 +1,8 @@ local skynet = require "skynet" local gateserver = require "snax.gateserver" -local netpack = require "netpack" -local crypt = require "crypt" -local socketdriver = require "socketdriver" +local netpack = require "skynet.netpack" +local crypt = require "skynet.crypt" +local socketdriver = require "skynet.socketdriver" local assert = assert local b64encode = crypt.base64encode local b64decode = crypt.base64decode From 2be915e3c737e420201f4d46231d47dec5f14f2a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 12:10:14 +0800 Subject: [PATCH 717/729] compat skynet 1.0 lua module --- lualib/compat10/cluster.lua | 1 + lualib/compat10/crypt.lua | 1 + lualib/compat10/datacenter.lua | 1 + lualib/compat10/debugchannel.lua | 1 + lualib/compat10/dns.lua | 1 + lualib/compat10/memory.lua | 1 + lualib/compat10/mongo.lua | 1 + lualib/compat10/mqueue.lua | 1 + lualib/compat10/multicast.lua | 1 + lualib/compat10/mysql.lua | 1 + lualib/compat10/netpack.lua | 1 + lualib/compat10/profile.lua | 1 + lualib/compat10/redis.lua | 1 + lualib/compat10/sharedata.lua | 1 + lualib/compat10/sharemap.lua | 1 + lualib/compat10/socket.lua | 1 + lualib/compat10/socketchannel.lua | 1 + lualib/compat10/socketdriver.lua | 1 + lualib/compat10/stm.lua | 1 + 19 files changed, 19 insertions(+) create mode 100644 lualib/compat10/cluster.lua create mode 100644 lualib/compat10/crypt.lua create mode 100644 lualib/compat10/datacenter.lua create mode 100644 lualib/compat10/debugchannel.lua create mode 100644 lualib/compat10/dns.lua create mode 100644 lualib/compat10/memory.lua create mode 100644 lualib/compat10/mongo.lua create mode 100644 lualib/compat10/mqueue.lua create mode 100644 lualib/compat10/multicast.lua create mode 100644 lualib/compat10/mysql.lua create mode 100644 lualib/compat10/netpack.lua create mode 100644 lualib/compat10/profile.lua create mode 100644 lualib/compat10/redis.lua create mode 100644 lualib/compat10/sharedata.lua create mode 100644 lualib/compat10/sharemap.lua create mode 100644 lualib/compat10/socket.lua create mode 100644 lualib/compat10/socketchannel.lua create mode 100644 lualib/compat10/socketdriver.lua create mode 100644 lualib/compat10/stm.lua diff --git a/lualib/compat10/cluster.lua b/lualib/compat10/cluster.lua new file mode 100644 index 00000000..04c5b01c --- /dev/null +++ b/lualib/compat10/cluster.lua @@ -0,0 +1 @@ +return require "skynet.cluster" \ No newline at end of file diff --git a/lualib/compat10/crypt.lua b/lualib/compat10/crypt.lua new file mode 100644 index 00000000..7fcb351a --- /dev/null +++ b/lualib/compat10/crypt.lua @@ -0,0 +1 @@ +return require "skynet.crypt" \ No newline at end of file diff --git a/lualib/compat10/datacenter.lua b/lualib/compat10/datacenter.lua new file mode 100644 index 00000000..1529e7bc --- /dev/null +++ b/lualib/compat10/datacenter.lua @@ -0,0 +1 @@ +return require "skynet.datacenter" \ No newline at end of file diff --git a/lualib/compat10/debugchannel.lua b/lualib/compat10/debugchannel.lua new file mode 100644 index 00000000..3768643d --- /dev/null +++ b/lualib/compat10/debugchannel.lua @@ -0,0 +1 @@ +return require "skynet.debugchannel" \ No newline at end of file diff --git a/lualib/compat10/dns.lua b/lualib/compat10/dns.lua new file mode 100644 index 00000000..fd9e7096 --- /dev/null +++ b/lualib/compat10/dns.lua @@ -0,0 +1 @@ +return require "skynet.dns" \ No newline at end of file diff --git a/lualib/compat10/memory.lua b/lualib/compat10/memory.lua new file mode 100644 index 00000000..07c4e506 --- /dev/null +++ b/lualib/compat10/memory.lua @@ -0,0 +1 @@ +return require "skynet.memory" \ No newline at end of file diff --git a/lualib/compat10/mongo.lua b/lualib/compat10/mongo.lua new file mode 100644 index 00000000..98c5456f --- /dev/null +++ b/lualib/compat10/mongo.lua @@ -0,0 +1 @@ +return require "skynet.mongo" \ No newline at end of file diff --git a/lualib/compat10/mqueue.lua b/lualib/compat10/mqueue.lua new file mode 100644 index 00000000..a1ca6102 --- /dev/null +++ b/lualib/compat10/mqueue.lua @@ -0,0 +1 @@ +return require "skynet.mqueue" \ No newline at end of file diff --git a/lualib/compat10/multicast.lua b/lualib/compat10/multicast.lua new file mode 100644 index 00000000..93a38bc7 --- /dev/null +++ b/lualib/compat10/multicast.lua @@ -0,0 +1 @@ +return require "skynet.multicast" \ No newline at end of file diff --git a/lualib/compat10/mysql.lua b/lualib/compat10/mysql.lua new file mode 100644 index 00000000..f0b9dd27 --- /dev/null +++ b/lualib/compat10/mysql.lua @@ -0,0 +1 @@ +return require "skynet.db.mysql" \ No newline at end of file diff --git a/lualib/compat10/netpack.lua b/lualib/compat10/netpack.lua new file mode 100644 index 00000000..eb1350ab --- /dev/null +++ b/lualib/compat10/netpack.lua @@ -0,0 +1 @@ +return require "skynet.netpack" \ No newline at end of file diff --git a/lualib/compat10/profile.lua b/lualib/compat10/profile.lua new file mode 100644 index 00000000..3d8dc69c --- /dev/null +++ b/lualib/compat10/profile.lua @@ -0,0 +1 @@ +return require "skynet.profile" \ No newline at end of file diff --git a/lualib/compat10/redis.lua b/lualib/compat10/redis.lua new file mode 100644 index 00000000..8ca502fd --- /dev/null +++ b/lualib/compat10/redis.lua @@ -0,0 +1 @@ +return require "skynet.db.redis" \ No newline at end of file diff --git a/lualib/compat10/sharedata.lua b/lualib/compat10/sharedata.lua new file mode 100644 index 00000000..59d9c942 --- /dev/null +++ b/lualib/compat10/sharedata.lua @@ -0,0 +1 @@ +return require "skynet.sharedata" \ No newline at end of file diff --git a/lualib/compat10/sharemap.lua b/lualib/compat10/sharemap.lua new file mode 100644 index 00000000..f377542b --- /dev/null +++ b/lualib/compat10/sharemap.lua @@ -0,0 +1 @@ +return require "skynet.sharemap" \ No newline at end of file diff --git a/lualib/compat10/socket.lua b/lualib/compat10/socket.lua new file mode 100644 index 00000000..99eec22f --- /dev/null +++ b/lualib/compat10/socket.lua @@ -0,0 +1 @@ +return require "skynet.socket" \ No newline at end of file diff --git a/lualib/compat10/socketchannel.lua b/lualib/compat10/socketchannel.lua new file mode 100644 index 00000000..b74f8a4f --- /dev/null +++ b/lualib/compat10/socketchannel.lua @@ -0,0 +1 @@ +return require "skynet.socketchannel" \ No newline at end of file diff --git a/lualib/compat10/socketdriver.lua b/lualib/compat10/socketdriver.lua new file mode 100644 index 00000000..45ddf996 --- /dev/null +++ b/lualib/compat10/socketdriver.lua @@ -0,0 +1 @@ +return require "skynet.socketdriver" \ No newline at end of file diff --git a/lualib/compat10/stm.lua b/lualib/compat10/stm.lua new file mode 100644 index 00000000..933c651d --- /dev/null +++ b/lualib/compat10/stm.lua @@ -0,0 +1 @@ +return require "skynet.stm" \ No newline at end of file From 5a4ebe6a73da0f04feded9b7935f5e89b5c6ea87 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 12:18:42 +0800 Subject: [PATCH 718/729] move snax to skynet --- examples/cluster1.lua | 2 +- examples/share.lua | 2 +- lualib/compat10/snax.lua | 1 + lualib/skynet/cluster.lua | 2 +- lualib/{ => skynet}/snax.lua | 0 service/console.lua | 2 +- service/debug_console.lua | 2 +- service/service_mgr.lua | 2 +- service/snaxd.lua | 2 +- test/pingserver.lua | 2 +- test/testping.lua | 2 +- test/testqueue.lua | 2 +- 12 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 lualib/compat10/snax.lua rename lualib/{ => skynet}/snax.lua (100%) diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 16e01b93..79665826 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local cluster = require "skynet.cluster" -local snax = require "snax" +local snax = require "skynet.snax" skynet.start(function() cluster.reload { diff --git a/examples/share.lua b/examples/share.lua index 8bc4b497..b91e3fc7 100644 --- a/examples/share.lua +++ b/examples/share.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local sharedata = require "sharedata" +local sharedata = require "skynet.sharedata" local mode = ... diff --git a/lualib/compat10/snax.lua b/lualib/compat10/snax.lua new file mode 100644 index 00000000..bdeda17d --- /dev/null +++ b/lualib/compat10/snax.lua @@ -0,0 +1 @@ +return require "skynet.snax" \ No newline at end of file diff --git a/lualib/skynet/cluster.lua b/lualib/skynet/cluster.lua index 14dee2ac..dd3b9dde 100644 --- a/lualib/skynet/cluster.lua +++ b/lualib/skynet/cluster.lua @@ -30,7 +30,7 @@ function cluster.proxy(node, name) end function cluster.snax(node, name, address) - local snax = require "snax" + local snax = require "skynet.snax" if not address then address = cluster.call(node, ".service", "QUERY", "snaxd" , name) end diff --git a/lualib/snax.lua b/lualib/skynet/snax.lua similarity index 100% rename from lualib/snax.lua rename to lualib/skynet/snax.lua diff --git a/service/console.lua b/service/console.lua index f7d1c6bf..1a7bc343 100644 --- a/service/console.lua +++ b/service/console.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local snax = require "snax" +local snax = require "skynet.snax" local socket = require "skynet.socket" local function split_cmdline(cmdline) diff --git a/service/debug_console.lua b/service/debug_console.lua index 986efae5..830a81c0 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -2,7 +2,7 @@ local skynet = require "skynet" local codecache = require "skynet.codecache" local core = require "skynet.core" local socket = require "skynet.socket" -local snax = require "snax" +local snax = require "skynet.snax" local memory = require "skynet.memory" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" diff --git a/service/service_mgr.lua b/service/service_mgr.lua index fc63ce25..dcd901fd 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" require "skynet.manager" -- import skynet.register -local snax = require "snax" +local snax = require "skynet.snax" local cmd = {} local service = {} diff --git a/service/snaxd.lua b/service/snaxd.lua index 4c9e98f9..6b264930 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -2,7 +2,7 @@ local skynet = require "skynet" local c = require "skynet.core" local snax_interface = require "snax.interface" local profile = require "skynet.profile" -local snax = require "snax" +local snax = require "skynet.snax" local snax_name = tostring(...) local loaderpath = skynet.getenv"snax_loader" diff --git a/test/pingserver.lua b/test/pingserver.lua index 5785b61b..e5213716 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" local queue = require "skynet.queue" -local snax = require "snax" +local snax = require "skynet.snax" local i = 0 local hello = "hello" diff --git a/test/testping.lua b/test/testping.lua index c40392cf..084729df 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local snax = require "snax" +local snax = require "skynet.snax" skynet.start(function() local ps = snax.newservice ("pingserver", "hello world") diff --git a/test/testqueue.lua b/test/testqueue.lua index 2076531e..a9053f41 100644 --- a/test/testqueue.lua +++ b/test/testqueue.lua @@ -1,5 +1,5 @@ local skynet = require "skynet" -local snax = require "snax" +local snax = require "skynet.snax" skynet.start(function() local ps = snax.uniqueservice ("pingserver", "test queue") From 1d364b5c2247999998e40e3af1154c0ab0a8c558 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 16:58:51 +0800 Subject: [PATCH 719/729] add skynet.service --- examples/config.path | 2 +- lualib/skynet/service.lua | 43 +++++++++++++ service/service_provider.lua | 113 +++++++++++++++++++++++++++++++++++ test/testservice/init.lua | 7 +++ test/testservice/kvdb.lua | 43 +++++++++++++ 5 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 lualib/skynet/service.lua create mode 100644 service/service_provider.lua create mode 100644 test/testservice/init.lua create mode 100644 test/testservice/kvdb.lua diff --git a/examples/config.path b/examples/config.path index 579fece5..568b345d 100644 --- a/examples/config.path +++ b/examples/config.path @@ -1,5 +1,5 @@ root = "./" -luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua;"..root.."test/?/init.lua" lualoader = root .. "lualib/loader.lua" lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua" lua_cpath = root .. "luaclib/?.so" diff --git a/lualib/skynet/service.lua b/lualib/skynet/service.lua new file mode 100644 index 00000000..32d665cf --- /dev/null +++ b/lualib/skynet/service.lua @@ -0,0 +1,43 @@ +local skynet = require "skynet" + +local service = {} +local cache = {} +local provider + +local function get_provider() + provider = provider or skynet.uniqueservice "service_provider" + return provider +end + +local function check(func) + local info = debug.getinfo(func, "u") + assert(info.nups == 1) + assert(debug.getupvalue(func,1) == "_ENV") +end + +function service.new(name, mainfunc, ...) + local p = get_provider() + local addr, booting = skynet.call(p, "lua", "test", name) + if addr then + service.address = addr + else + if booting then + service.address = skynet.call(p, "lua", "query", name) + else + check(mainfunc) + local code = string.dump(mainfunc) + service.address = skynet.call(p, "lua", "launch", name, code, ...) + end + end + cache[name] = service.address + return service.address +end + +function service.query(name) + if not cache[name] then + cache[name] = skynet.call(get_provider(), "lua", "query", name) + end + return cache[name] +end + +return service diff --git a/service/service_provider.lua b/service/service_provider.lua new file mode 100644 index 00000000..b04067ca --- /dev/null +++ b/service/service_provider.lua @@ -0,0 +1,113 @@ +local skynet = require "skynet" + +local provider = {} + +local function new_service(svr, name) + local s = {} + svr[name] = s + s.queue = {} + return s +end + +local svr = setmetatable({}, { __index = new_service }) + + +function provider.query(name) + local s = svr[name] + if s.queue then + table.insert(s.queue, skynet.response()) + else + if s.address then + return skynet.ret(skynet.pack(s.address)) + else + error(s.error) + end + end +end + +local function boot(addr, name, code, ...) + local s = svr[name] + skynet.call(addr, "lua", "init", code, ...) + local tmp = table.pack( ... ) + for i=1,tmp.n do + tmp[i] = tostring(tmp[i]) + end + + if tmp.n > 0 then + s.init = table.concat(tmp, ",") + end + s.time = skynet.time() +end + +function provider.launch(name, code, ...) + local s = svr[name] + if s.booting then + table.insert(s.queue, skynet.response()) + else + s.booting = true + local err + local ok, addr = pcall(skynet.newservice,"service_cell", name) + if ok then + ok, err = xpcall(boot, debug.traceback, addr, name, code, ...) + else + err = addr + addr = nil + end + s.booting = nil + if ok then + s.address = addr + for _, resp in ipairs(s.queue) do + resp(true, addr) + end + s.queue = nil + skynet.ret(skynet.pack(addr)) + else + if addr then + skynet.send(addr, "debug", "EXIT") + end + s.error = err + for _, resp in ipairs(s.queue) do + resp(false) + end + s.queue = nil + error(err) + end + end +end + +function provider.test(name) + local s = svr[name] + if s.booting then + skynet.ret(skynet.pack(nil, true)) -- booting + elseif s.address then + skynet.ret(skynet.pack(s.address)) + elseif s.error then + error(s.error) + else + skynet.ret() -- nil + end +end + +skynet.start(function() + skynet.dispatch("lua", function(session, address, cmd, ...) + provider[cmd](...) + end) + skynet.info_func(function() + local info = {} + for k,v in pairs(svr) do + local status + if v.booting then + status = "booting" + elseif v.queue then + status = "waiting(" .. #v.queue .. ")" + end + info[skynet.address(v.address)] = { + init = v.init, + name = k, + time = os.date("%Y %b %d %T %z",math.floor(v.time)), + status = status, + } + end + return info + end) +end) diff --git a/test/testservice/init.lua b/test/testservice/init.lua new file mode 100644 index 00000000..d69b8973 --- /dev/null +++ b/test/testservice/init.lua @@ -0,0 +1,7 @@ +local skynet = require "skynet" +local kvdb = require "kvdb" + +skynet.start(function() + kvdb.set("A", 1) + print(kvdb.get "A") +end) diff --git a/test/testservice/kvdb.lua b/test/testservice/kvdb.lua new file mode 100644 index 00000000..5dcd695d --- /dev/null +++ b/test/testservice/kvdb.lua @@ -0,0 +1,43 @@ +local skynet = require "skynet" +local service = require "skynet.service" + +local kvdb = {} + +-- service.address is the default address registered by itself. +function kvdb.get(key) + return skynet.call(service.address, "lua", "get", key) +end + +function kvdb.set(key, value) + skynet.call(service.address, "lua", "set", key , value) +end + +-- this function will be injected into an unique service, so don't refer any upvalues +local function service_mainfunc(...) + local skynet = require "skynet" + + skynet.error(...) -- (...) passed from service.new + + local db = {} + + local command = {} + + function command.get(key) + return db[key] + end + + function command.set(key, value) + db[key] = value + end + + -- skynet.start is compatible + skynet.dispatch("lua", function(session, address, cmd, ...) + skynet.ret(skynet.pack(command[cmd](...))) + end) +end + +skynet.init(function() + service.new("kvdb", service_mainfunc, "Service Init") +end) + +return kvdb From 895c0dae3b09541dff4fac9a29d1c61ca6e42b14 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 1 Jun 2017 17:31:19 +0800 Subject: [PATCH 720/729] skynet.db.mongo --- lualib/compat10/mongo.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/compat10/mongo.lua b/lualib/compat10/mongo.lua index 98c5456f..eee821ae 100644 --- a/lualib/compat10/mongo.lua +++ b/lualib/compat10/mongo.lua @@ -1 +1 @@ -return require "skynet.mongo" \ No newline at end of file +return require "skynet.db.mongo" \ No newline at end of file From c7d2e2cb0e270fe7b23c657c37e504a8a03ef954 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Jun 2017 13:24:24 +0800 Subject: [PATCH 721/729] remove debugchannel proxy --- lualib/compat10/debugchannel.lua | 1 - 1 file changed, 1 deletion(-) delete mode 100644 lualib/compat10/debugchannel.lua diff --git a/lualib/compat10/debugchannel.lua b/lualib/compat10/debugchannel.lua deleted file mode 100644 index 3768643d..00000000 --- a/lualib/compat10/debugchannel.lua +++ /dev/null @@ -1 +0,0 @@ -return require "skynet.debugchannel" \ No newline at end of file From 624fbf6f227575fb9296f44856e5ff98fb7c03f9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Jun 2017 19:16:36 +0800 Subject: [PATCH 722/729] fix issue #648 --- service/debug_console.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/service/debug_console.lua b/service/debug_console.lua index 830a81c0..07bae9fb 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -281,12 +281,17 @@ function COMMANDX.debug(cmd) end skynet.fork(function() pcall(forward_cmd) - skynet.wakeup(term_co) + if not stop then -- block at skynet.call "start" + term_co = nil + skynet.wakeup(term_co) + end end) local ok, err = skynet.call(agent, "lua", "start", address, cmd.fd) stop = true - -- wait for fork coroutine exit. - skynet.wait(term_co) + if term_co then + -- wait for fork coroutine exit. + skynet.wait(term_co) + end if not ok then error(err) From 94374cf06f4e6bb4e36b7d5d6b0fed6637f0c1ab Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Jun 2017 19:36:39 +0800 Subject: [PATCH 723/729] fix #648 --- service/debug_console.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/service/debug_console.lua b/service/debug_console.lua index 07bae9fb..33cc06ee 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -283,6 +283,7 @@ function COMMANDX.debug(cmd) pcall(forward_cmd) if not stop then -- block at skynet.call "start" term_co = nil + else skynet.wakeup(term_co) end end) From 2798c778c71b0e0d43a6e543df852f297da34a34 Mon Sep 17 00:00:00 2001 From: sundream Date: Sat, 3 Jun 2017 12:26:19 +0800 Subject: [PATCH 724/729] redis cluster client --- lualib/skynet/db/redis/cluster.lua | 383 +++++++++++++++++++++++++++++ lualib/skynet/db/redis/crc16.lua | 62 +++++ test/testrediscluster.lua | 114 +++++++++ 3 files changed, 559 insertions(+) create mode 100644 lualib/skynet/db/redis/cluster.lua create mode 100644 lualib/skynet/db/redis/crc16.lua create mode 100644 test/testrediscluster.lua diff --git a/lualib/skynet/db/redis/cluster.lua b/lualib/skynet/db/redis/cluster.lua new file mode 100644 index 00000000..99e37577 --- /dev/null +++ b/lualib/skynet/db/redis/cluster.lua @@ -0,0 +1,383 @@ +-- a simple redis-cluster client +-- rewrite from https://github.com/antirez/redis-rb-cluster + +local skynet = require "skynet" +local redis = require "skynet.db.redis" +local crc16 = require "skynet.db.redis.crc16" + +local RedisClusterHashSlots = 16384 +local RedisClusterRequestTTL = 16 + + +local _M = {} + +local rediscluster = {} +rediscluster.__index = rediscluster +_M.rediscluster = rediscluster + +function _M.new(startup_nodes,opt) + if #startup_nodes == 0 then + startup_nodes = {startup_nodes,} + end + opt = opt or {} + local self = { + startup_nodes = startup_nodes, + max_connections = opt.max_connections or 16, + connections = setmetatable({},{__mode = "kv"}), + opt = opt, + refresh_table_asap = false, + } + setmetatable(self,rediscluster) + self:initialize_slots_cache() + return self +end + +local function nodename(node) + return string.format("%s:%s",node.host,node.port) +end + +function rediscluster:get_redis_link(node) + local conf = { + host = node.host, + port = node.port, + auth = self.opt.auth, + db = self.opt.db or 0, + } + return redis.connect(conf) +end + +-- Given a node (that is just a Ruby hash) give it a name just +-- concatenating the host and port. We use the node name as a key +-- to cache connections to that node. +function rediscluster:set_node_name(node) + if not node.name then + node.name = nodename(node) + end + if not node.slaves then + local oldnode = self.name_node[node.name] + if oldnode then + node.slaves = oldnode.slaves + end + end + self.name_node[node.name] = node +end + +-- Contact the startup nodes and try to fetch the hash slots -> instances +-- map in order to initialize the @slots hash. +function rediscluster:initialize_slots_cache() + self.slots = {} + self.nodes = {} + self.name_node = {} + for _,startup_node in ipairs(self.startup_nodes) do + local ok = pcall(function () + local name = nodename(startup_node) + local conn = self.connections[name] or self:get_redis_link(startup_node) + local list = conn:cluster("slots") + for _,result in ipairs(list) do + local ip,port,runid = table.unpack(result[3]) + local master_node = { + host = ip, + port = port, + runid = runid, + slaves = {}, + } + self:set_node_name(master_node) + for i=4,#result do + local ip,port,runid = table.unpack(result[i]) + local slave_node = { + host = ip, + port = port, + runid = runid, + } + self:set_node_name(slave_node) + table.insert(master_node.slaves,slave_node) + end + for slot=tonumber(result[1]),tonumber(result[2]) do + table.insert(self.nodes,master_node) + self.slots[slot] = master_node + end + end + self.refresh_table_asap = false + if not self.connections[name] then + self.connections[name] = conn + end + end) + -- Exit the loop as long as the first node replies + if ok then + break + end + end +end + +-- Flush the cache, mostly useful for debugging when we want to force +-- redirection. +function rediscluster:flush_slots_cache() + self.slots = {} +end + +-- Return the hash slot from the key. +function rediscluster:keyslot(key) + -- Only hash what is inside {...} if there is such a pattern in the key. + -- Note that the specification requires the content that is between + -- the first { and the first } after the first {. If we found {} without + -- nothing in the middle, the whole key is hashed as usually. + local startpos = string.find(key,"{",1,true) + if startpos then + local endpos = string.find(key,"}",startpos+1,true) + if endpos and endpos ~= startpos + 1 then + key = string.sub(key,startpos+1,endpos-1) + end + end + return crc16(key) % RedisClusterHashSlots +end + +-- Return the first key in the command arguments. +-- +-- Currently we just return argv[1], that is, the first argument +-- after the command name. +-- +-- This is indeed the key for most commands, and when it is not true +-- the cluster redirection will point us to the right node anyway. +-- +-- For commands we want to explicitly bad as they don't make sense +-- in the context of cluster, nil is returned. +function rediscluster:get_key_from_command(argv) + local cmd,key = table.unpack(argv) + cmd = string.lower(cmd) + if cmd == "info" or + cmd == "multi" or + cmd == "exec" or + cmd == "slaveof" or + cmd == "config" or + cmd == "shutdown" then + return nil + end + -- Unknown commands, and all the commands having the key + -- as first argument are handled here: + -- set, get, ... + return key +end + +-- If the current number of connections is already the maximum number +-- allowed, close a random connection. This should be called every time +-- we cache a new connection in the @connections hash. +function rediscluster:close_existing_connection() + local length = 0 + for name,conn in pairs(self.connections) do + length = length + 1 + end + if length >= self.max_connections then + pcall(function () + local name,conn = next(self.connections) + self.connections[name] = nil + conn:disconnect() + end) + end +end + +function rediscluster:close_all_connection() + local connections = self.connections + self.connections = setmetatable({},{__mode = "kv"}) + for name,conn in pairs(connections) do + pcall(conn.disconnect,conn) + end +end + +function rediscluster:get_connection(node) + if type(node) == "string" then + local ip,port = string.match(node,"^([^:]+):([^:]+)$") + node = {host=ip,port=port} + end + local name = node.name or nodename(node) + local conn = self.connections[name] + if not conn then + conn = self:get_redis_link(node) + self.connections[name] = conn + end + return self.connections[name] +end + +-- Return a link to a random node, or raise an error if no node can be +-- contacted. This function is only called when we can't reach the node +-- associated with a given hash slot, or when we don't know the right +-- mapping. +-- The function will try to get a successful reply to the PING command, +-- otherwise the next node is tried. +function rediscluster:get_random_connection() + -- shuffle + local shuffle_idx = {} + local startpos = 1 + local endpos = #self.nodes + for i=startpos,endpos do + shuffle_idx[i] = i + end + for i=startpos,endpos do + local idx = math.random(i,endpos) + local tmp = shuffle_idx[i] + shuffle_idx[i] = shuffle_idx[idx] + shuffle_idx[idx] = tmp + end + for i,idx in ipairs(shuffle_idx) do + local ok,conn = pcall(function () + local node = self.nodes[idx] + local conn = self.connections[node.name] + if not conn then + -- Connect the node if it is not connected + conn = self:get_redis_link(node) + if conn:ping() == "PONG" then + self:close_existing_connection() + self.connections[node.name] = conn + return conn + else + -- If the connection is not good close it ASAP in order + -- to avoid waiting for the GC finalizer. File + -- descriptors are a rare resource. + conn:disconnect() + end + else + -- The node was already connected, test the connection. + if conn:ping() == "PONG" then + return conn + end + end + end) + if ok and conn then + return conn + end + end + error("Can't reach a single startup node.") +end + +-- Given a slot return the link (Redis instance) to the mapped node. +-- Make sure to create a connection with the node if we don't have +-- one. +function rediscluster:get_connection_by_slot(slot) + local node = self.slots[slot] + -- If we don't know what the mapping is, return a random node. + if not node then + return self:get_random_connection() + end + if not self.connections[node.name] then + local ok = pcall(function () + self:close_existing_connection() + self.connections[node.name] = self:get_redis_link(node) + end) + if not ok then + if self.opt.read_slave and node.slaves and #node.slaves > 0 then + local slave_node = node.slaves[math.random(1,#node.slaves)] + local ok2,conn = pcall(self.get_connection,self,slave_node) + if ok2 then + conn:readonly() -- allow this connection read-slave + return conn + end + end + -- This will probably never happen with recent redis-rb + -- versions because the connection is enstablished in a lazy + -- way only when a command is called. However it is wise to + -- handle an instance creation error of some kind. + return self:get_random_connection() + end + end + return self.connections[node.name] +end + +-- Dispatch commands. +function rediscluster:call(...) + local argv = table.pack(...) + if self.refresh_table_asap then + self:initialize_slots_cache() + end + local ttl = RedisClusterRequestTTL -- Max number of redirections + local err + local asking = false + local try_random_node = false + while ttl > 0 do + ttl = ttl - 1 + local key = self:get_key_from_command(argv) + if not key then + error("No way to dispatch this command to Redis Cluster: " .. tostring(argv[1])) + end + local conn + local slot = self:keyslot(key) + if asking then + conn = self:get_connection(asking) + elseif try_random_node then + conn = self:get_random_connection() + try_random_node = false + else + conn = self:get_connection_by_slot(slot) + end + local result = {pcall(function () + -- TODO: use pipelining to send asking and save a rtt. + if asking then + conn:asking() + end + asking = false + local cmd = argv[1] + local func = conn[cmd] + return func(conn,table.unpack(argv,2)) + end)} + local ok = result[1] + if not ok then + err = table.unpack(result,2) + err = tostring(err) + if err == "[Error: socket]" then + -- may be nerver come here? + try_random_node = true + if ttl < RedisClusterRequestTTL/2 then + skynet.sleep(10) + end + else + -- err: ./lualib/skynet/socketchannel.lua:371: xxx + err = string.match(err,".+:%d+:%s(.*)$") or err + local errlist = {} + for e in string.gmatch(err,"([^%s]+)%s?") do + table.insert(errlist,e) + end + if (errlist[1] ~= "MOVED" and errlist[1] ~= "ASK") then + error(err) + else + if errlist[1] == "ASK" then + asking = true + else + -- Serve replied with MOVED. It's better for us to + -- ask for CLUSTER SLOTS the next time. + self.refresh_table_asap = true + end + local newslot = tonumber(errlist[2]) + local node_ip,node_port = string.match(errlist[3],"^([^:]+):([^:]+)$") + local node = { + host = node_ip, + port = tonumber(node_port), + } + if not asking then + self:set_node_name(node) + self.slots[newslot] = node + else + asking = node + end + end + end + else + return table.unpack(result,2) + end + end + error(string.format("Too many Cluster redirections?,maybe node is disconnected (last error: %q)",err)) +end + +-- Currently we handle all the commands using method_missing for +-- simplicity. For a Cluster client actually it will be better to have +-- every single command as a method with the right arity and possibly +-- additional checks (example: RPOPLPUSH with same src/dst key, SORT +-- without GET or BY, and so forth). +setmetatable(rediscluster,{ + __index = function (t,cmd) + t[cmd] = function (self,...) + return self:call(cmd,...) + end + return t[cmd] + end, +}) + + +return _M diff --git a/lualib/skynet/db/redis/crc16.lua b/lualib/skynet/db/redis/crc16.lua new file mode 100644 index 00000000..1acab9f5 --- /dev/null +++ b/lualib/skynet/db/redis/crc16.lua @@ -0,0 +1,62 @@ +--/* +-- This is the CRC16 algorithm used by Redis Cluster to hash keys. +-- Implementation according to CCITT standards. +-- +-- This is actually the XMODEM CRC 16 algorithm, using the +-- following parameters: +-- +-- Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN" +-- Width : 16 bit +-- Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1) +-- Initialization : 0000 +-- Reflect Input byte : False +-- Reflect Output CRC : False +-- Xor constant to output CRC : 0000 +-- Output for "123456789" : 31C3 +--*/ + + + +local XMODEMCRC16Lookup = { + 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, + 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, + 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, + 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, + 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, + 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, + 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, + 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, + 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, + 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, + 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, + 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, + 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, + 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, + 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, + 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, + 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, + 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, + 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, + 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, + 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, + 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, + 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, + 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, + 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, + 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, + 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, + 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, + 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, + 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, + 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, + 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 +} + +return function(bytes) + local crc = 0 + for i=1,#bytes do + local b = string.byte(bytes,i,i) + crc = ((crc<<8) & 0xffff) ~ XMODEMCRC16Lookup[(((crc>>8)~b) & 0xff) + 1] + end + return tonumber(crc) +end diff --git a/test/testrediscluster.lua b/test/testrediscluster.lua new file mode 100644 index 00000000..5c6efdf0 --- /dev/null +++ b/test/testrediscluster.lua @@ -0,0 +1,114 @@ +local skynet = require "skynet" +local rediscluster = require "skynet.db.redis.cluster" + +local test_more = ... + +skynet.start(function () + local db = rediscluster.new({ + {host="127.0.0.1",port=7000}, + {host="127.0.0.1",port=7001},}, + {read_slave=true,auth=nil,db=0,} + ) + db:del("list") + db:del("map") + db:rpush("list",1,2,3) + local list = db:lrange("list",0,-1) + for i,v in ipairs(list) do + print(v) + end + db:hmset("map","key1",1,"key2",2) + local map = db:hgetall("map") + for i=1,#map,2 do + local key = map[i] + local val = map[i+1] + print(key,val) + end + -- test MOVED + db:flush_slots_cache() + print(db:set("A",1)) + print(db:get("A")) + -- reconnect + local cnt = 0 + for name,conn in pairs(db.connections) do + print(name,conn) + cnt = cnt + 1 + end + print("cnt:",cnt) + db:close_all_connection() + print(db:set("A",1)) + print(db:del("A")) + + local slot = db:keyslot("{foo}") + local conn = db:get_connection_by_slot(slot) + -- You must ensure keys at one slot: use same key or hash tags + local ret = conn:pipeline({ + {"hincrby", "{foo}hello", 1, 1}, + {"del", "{foo}hello"}, + {"hmset", "{foo}hello", 1, 1, 2, 2, 3, 3}, + {"hgetall", "{foo}hello"}, + },{}) + print(ret[1].ok,ret[1].out) + print(ret[2].ok,ret[2].out) + print(ret[3].ok,ret[3].out) + print(ret[4].ok) + if ret[4].ok then + for i,v in ipairs(ret[4].out) do + print(v) + end + else + print(ret[4].out) + end + -- dbsize/info/keys + local conn = db:get_random_connection() + print("dbsize:",conn:dbsize()) + print("info:",conn:info()) + local keys = conn:keys("list*") + for i,key in ipairs(keys) do + print(key) + end + print("cluster nodes") + local nodes = db:cluster("nodes") + print(nodes) + print("cluster slots") + local slots = db:cluster("slots") + for i,slot_map in ipairs(slots) do + local start_slot = slot_map[1] + local end_slot = slot_map[2] + local master_node = slot_map[3] + print(start_slot,end_slot) + for i,v in ipairs(master_node) do + print(v) + end + for i=4,#slot_map do + local slave_node = slot_map[i] + for i,v in ipairs(slave_node) do + print(v) + end + end + end + + if not test_more then + skynet.exit() + return + end + local last = false + while not last do + last = db:get("__last__") + if last == nil then + last = 0 + end + last = tonumber(last) + end + for val=last,1000000000 do + local ok,errmsg = pcall(function () + local key = string.format("foo%s",val) + db:set(key,val) + print(key,db:get(key)) + db:set("__last__",val) + end) + if not ok then + print("error:",errmsg) + end + end + skynet.exit() +end) From a0ac8640c730352853930b4b208541a72b3b53ec Mon Sep 17 00:00:00 2001 From: dennis Date: Sun, 4 Jun 2017 23:52:21 +0800 Subject: [PATCH 725/729] remove wrong prefix --- examples/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/client.lua b/examples/client.lua index 52836c5f..92aaa16e 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -5,7 +5,7 @@ if _VERSION ~= "Lua 5.3" then error "Use lua 5.3" end -local socket = require "skynet.clientsocket" +local socket = require "clientsocket" local proto = require "proto" local sproto = require "sproto" From 1328895ff9a30d8c407cf4ba4ee945cd5ae80b5f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 6 Jun 2017 13:54:43 +0800 Subject: [PATCH 726/729] copy crypt to client.crypt, see #655 --- Makefile | 4 ++-- examples/client.lua | 2 +- examples/login/client.lua | 4 ++-- examples/login/gated.lua | 2 +- examples/login/logind.lua | 2 +- lualib-src/lua-clientsocket.c | 2 +- lualib-src/lua-crypt.c | 5 +++++ 7 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index b4a94090..c54703c9 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ update3rd : CSERVICE = snlua logger gate harbor LUA_CLIB = skynet \ - clientsocket \ + client \ bson md5 sproto lpeg LUA_CLIB_SKYNET = \ @@ -100,7 +100,7 @@ $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/compat-5.2.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lua-md5 $^ -o $@ -$(LUA_CLIB_PATH)/clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH) +$(LUA_CLIB_PATH)/client.so : lualib-src/lua-clientsocket.c lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c | $(LUA_CLIB_PATH) diff --git a/examples/client.lua b/examples/client.lua index 92aaa16e..740732f8 100644 --- a/examples/client.lua +++ b/examples/client.lua @@ -5,7 +5,7 @@ if _VERSION ~= "Lua 5.3" then error "Use lua 5.3" end -local socket = require "clientsocket" +local socket = require "client.socket" local proto = require "proto" local sproto = require "sproto" diff --git a/examples/login/client.lua b/examples/login/client.lua index f7c48018..4143f941 100644 --- a/examples/login/client.lua +++ b/examples/login/client.lua @@ -1,7 +1,7 @@ package.cpath = "luaclib/?.so" -local socket = require "clientsocket" -local crypt = require "crypt" +local socket = require "client.socket" +local crypt = require "client.crypt" if _VERSION ~= "Lua 5.3" then error "Use lua 5.3" diff --git a/examples/login/gated.lua b/examples/login/gated.lua index 0b8c53b7..86fa31fc 100644 --- a/examples/login/gated.lua +++ b/examples/login/gated.lua @@ -1,5 +1,5 @@ local msgserver = require "snax.msgserver" -local crypt = require "crypt" +local crypt = require "skynet.crypt" local skynet = require "skynet" local loginservice = tonumber(...) diff --git a/examples/login/logind.lua b/examples/login/logind.lua index dd003f2f..5c739a8a 100644 --- a/examples/login/logind.lua +++ b/examples/login/logind.lua @@ -1,5 +1,5 @@ local login = require "snax.loginserver" -local crypt = require "crypt" +local crypt = require "skynet.crypt" local skynet = require "skynet" local server = { diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index def0bff6..25189032 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -186,7 +186,7 @@ lreadstdin(lua_State *L) { } LUAMOD_API int -luaopen_clientsocket(lua_State *L) { +luaopen_client_socket(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "connect", lconnect }, diff --git a/lualib-src/lua-crypt.c b/lualib-src/lua-crypt.c index 7a182db7..888fe544 100644 --- a/lualib-src/lua-crypt.c +++ b/lualib-src/lua-crypt.c @@ -933,3 +933,8 @@ luaopen_skynet_crypt(lua_State *L) { luaL_newlib(L,l); return 1; } + +LUAMOD_API int +luaopen_client_crypt(lua_State *L) { + return luaopen_skynet_crypt(L); +} From 6e36cf99404139b2269c507597d4773d2b84c894 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 2 Jun 2017 18:12:16 +0800 Subject: [PATCH 727/729] add skynet.datasheet --- Makefile | 1 + lualib-src/lua-datasheet.c | 371 ++++++++++++++++++++++++++++ lualib/skynet/datasheet/builder.lua | 176 +++++++++++++ lualib/skynet/datasheet/dump.lua | 270 ++++++++++++++++++++ lualib/skynet/datasheet/init.lua | 77 ++++++ test/testdatasheet.lua | 28 +++ 6 files changed, 923 insertions(+) create mode 100644 lualib-src/lua-datasheet.c create mode 100644 lualib/skynet/datasheet/builder.lua create mode 100644 lualib/skynet/datasheet/dump.lua create mode 100644 lualib/skynet/datasheet/init.lua create mode 100644 test/testdatasheet.lua diff --git a/Makefile b/Makefile index c54703c9..4f8b17a7 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,7 @@ LUA_CLIB_SKYNET = \ lua-stm.c \ lua-mysqlaux.c \ lua-debugchannel.c \ + lua-datasheet.c \ \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ diff --git a/lualib-src/lua-datasheet.c b/lualib-src/lua-datasheet.c new file mode 100644 index 00000000..e0390f4a --- /dev/null +++ b/lualib-src/lua-datasheet.c @@ -0,0 +1,371 @@ +#include +#include +#include + +#define NODECACHE "_ctable" +#define PROXYCACHE "_proxy" +#define TABLES "_ctables" + +#define VALUE_NIL 0 +#define VALUE_INTEGER 1 +#define VALUE_REAL 2 +#define VALUE_BOOLEAN 3 +#define VALUE_TABLE 4 +#define VALUE_STRING 5 +#define VALUE_INVALID 6 + +#define INVALID_OFFSET 0xffffffff + +struct proxy { + const char * data; + int index; +}; + +struct document { + uint32_t strtbl; + uint32_t n; + uint32_t index[1]; + // table[n] + // strings +}; + +struct table { + uint32_t array; + uint32_t dict; + uint8_t type[1]; + // value[array] + // kvpair[dict] +}; + +static inline const struct table * +gettable(const struct document *doc, int index) { + if (doc->index[index] == INVALID_OFFSET) { + return NULL; + } + return (const struct table *)((const char *)doc + sizeof(uint32_t) + sizeof(uint32_t) + doc->n * sizeof(uint32_t) + doc->index[index]); +} + +static void +create_proxy(lua_State *L, const void *data, int index) { + const struct table * t = gettable(data, index); + if (t == NULL) { + luaL_error(L, "Invalid index %d", index); + } + lua_getfield(L, LUA_REGISTRYINDEX, NODECACHE); + if (lua_rawgetp(L, -1, t) == LUA_TTABLE) { + lua_replace(L, -2); + return; + } + lua_pop(L, 1); + lua_newtable(L); + lua_pushvalue(L, lua_upvalueindex(1)); + lua_setmetatable(L, -2); + lua_pushvalue(L, -1); + // NODECACHE, table, table + lua_rawsetp(L, -3, t); + // NODECACHE, table + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + // NODECACHE, table, PROXYCACHE + lua_pushvalue(L, -2); + // NODECACHE, table, PROXYCACHE, table + struct proxy * p = lua_newuserdata(L, sizeof(struct proxy)); + // NODECACHE, table, PROXYCACHE, table, proxy + p->data = data; + p->index = index; + lua_rawset(L, -3); + // NODECACHE, table, PROXYCACHE + lua_pop(L, 1); + // NODECACHE, table + lua_replace(L, -2); + // table +} + +static void +clear_table(lua_State *L) { + int t = lua_gettop(L); // clear top table + if (lua_type(L, t) != LUA_TTABLE) { + luaL_error(L, "Invalid cache"); + } + lua_pushnil(L); + while (lua_next(L, t) != 0) { + // key value + lua_pop(L, 1); + lua_pushvalue(L, -1); + lua_pushnil(L); + // key key nil + lua_rawset(L, t); + // key + } +} + +static void +update_cache(lua_State *L, const void *data, const void * newdata) { + lua_getfield(L, LUA_REGISTRYINDEX, NODECACHE); + int t = lua_gettop(L); + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + int pt = t + 1; + lua_newtable(L); // temp table + int nt = pt + 1; + lua_pushnil(L); + while (lua_next(L, t) != 0) { + // pointer (-2) -> table (-1) + lua_pushvalue(L, -1); + if (lua_rawget(L, pt) == LUA_TUSERDATA) { + // pointer, table, proxy + struct proxy * p = lua_touserdata(L, -1); + if (p->data == data) { + // update to newdata + p->data = newdata; + const struct table * newt = gettable(newdata, p->index); + lua_pop(L, 1); + // pointer, table + clear_table(L); + lua_pushvalue(L, lua_upvalueindex(1)); + // pointer, table, meta + lua_setmetatable(L, -2); + // pointer, table + if (newt) { + lua_rawsetp(L, nt, newt); + } else { + lua_pop(L, 1); + } + // pointer + lua_pushvalue(L, -1); + lua_pushnil(L); + lua_rawset(L, t); + } else { + lua_pop(L, 2); + } + } else { + lua_pop(L, 2); + // pointer + } + } + // copy nt to t + lua_pushnil(L); + while (lua_next(L, nt) != 0) { + lua_pushvalue(L, -2); + lua_insert(L, -2); + // key key value + lua_rawset(L, t); + } + // NODECACHE PROXYCACHE TEMP + lua_pop(L, 3); +} + +static int +lupdate(lua_State *L) { + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + lua_pushvalue(L, 1); + // PROXYCACHE, table + if (lua_rawget(L, -2) != LUA_TUSERDATA) { + luaL_error(L, "Invalid proxy table %p", lua_topointer(L, 1)); + } + struct proxy * p = lua_touserdata(L, -1); + luaL_checktype(L, 2, LUA_TLIGHTUSERDATA); + const char * newdata = lua_touserdata(L, 2); + update_cache(L, p->data, newdata); + return 1; +} + +static inline uint32_t +getuint32(const void *v) { + union { + uint32_t d; + uint8_t t[4]; + } test = { 1 }; + if (test.t[0] == 0) { + // big endian + test.d = *(const uint32_t *)v; + return test.t[0] | test.t[1] << 4 | test.t[2] << 8 | test.t[3] << 12; + } else { + return *(const uint32_t *)v; + } +} + +static inline float +getfloat(const void *v) { + union { + uint32_t d; + float f; + uint8_t t[4]; + } test = { 1 }; + if (test.t[0] == 0) { + // big endian + test.d = *(const uint32_t *)v; + test.d = test.t[0] | test.t[1] << 4 | test.t[2] << 8 | test.t[3] << 12; + return test.f; + } else { + return *(const float *)v; + } +} + +static void +pushvalue(lua_State *L, const void *v, int type, const struct document * doc) { + switch (type) { + case VALUE_NIL: + lua_pushnil(L); + break; + case VALUE_INTEGER: + lua_pushinteger(L, (int32_t)getuint32(v)); + break; + case VALUE_REAL: + lua_pushnumber(L, getfloat(v)); + break; + case VALUE_BOOLEAN: + lua_pushboolean(L, getuint32(v)); + break; + case VALUE_TABLE: + create_proxy(L, doc, getuint32(v)); + break; + case VALUE_STRING: + lua_pushstring(L, (const char *)doc + doc->strtbl + getuint32(v)); + break; + default: + luaL_error(L, "Invalid type %d at %p", type, v); + } +} + +static void +copytable(lua_State *L, int tbl, struct proxy *p) { + const struct document * doc = (const struct document *)p->data; + if (p->index < 0 || p->index >= doc->n) { + luaL_error(L, "Invalid proxy (index = %d, total = %d)", p->index, (int)doc->n); + } + const struct table * t = gettable(doc, p->index); + if (t == NULL) { + luaL_error(L, "Invalid proxy (index = %d)", p->index); + } + const uint32_t * v = (const uint32_t *)((const char *)t + sizeof(uint32_t) + sizeof(uint32_t) + ((t->array + t->dict + 3) & ~3)); + int i; + for (i=0;iarray;i++) { + pushvalue(L, v++, t->type[i], doc); + lua_rawseti(L, tbl, i+1); + } + for (i=0;idict;i++) { + pushvalue(L, v++, VALUE_STRING, doc); + pushvalue(L, v++, t->type[t->array+i], doc); + lua_rawset(L, tbl); + } +} + +static int +lnew(lua_State *L) { + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + const char * data = lua_touserdata(L, 1); + // hold ref to data + lua_getfield(L, LUA_REGISTRYINDEX, TABLES); + lua_pushvalue(L, 1); + lua_rawsetp(L, -2, data); + + create_proxy(L, data, 0); + return 1; +} + +static void +copyfromdata(lua_State *L) { + lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + lua_pushvalue(L, 1); + // PROXYCACHE, table + if (lua_rawget(L, -2) != LUA_TUSERDATA) { + luaL_error(L, "Invalid proxy table %p", lua_topointer(L, 1)); + } + struct proxy * p = lua_touserdata(L, -1); + lua_pop(L, 2); + copytable(L, 1, p); + lua_pushnil(L); + lua_setmetatable(L, 1); // remove metatable +} + +static int +lindex(lua_State *L) { + copyfromdata(L); + lua_rawget(L, 1); + return 1; +} + +static int +lnext(lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 2); /* create a 2nd argument if there isn't one */ + if (lua_next(L, 1)) + return 2; + else { + lua_pushnil(L); + return 1; + } +} + +static int +lpairs(lua_State *L) { + copyfromdata(L); + lua_pushcfunction(L, lnext); + lua_pushvalue(L, 1); + lua_pushnil(L); + return 3; +} + +static int +llen(lua_State *L) { + copyfromdata(L); + lua_pushinteger(L, lua_rawlen(L, 1)); + return 1; +} + +static void +gen_metatable(lua_State *L) { + lua_createtable(L, 0, 1); // weak meta table + lua_pushstring(L, "kv"); + lua_setfield(L, -2, "__mode"); + + lua_newtable(L); // NODECACHE + lua_pushvalue(L, -2); + lua_setmetatable(L, -2); // make NODECACGE weak + lua_setfield(L, LUA_REGISTRYINDEX, NODECACHE); + + lua_newtable(L); // PROXYCACHE + lua_pushvalue(L, -2); + lua_setmetatable(L, -2); // make NODECACGE weak + lua_setfield(L, LUA_REGISTRYINDEX, PROXYCACHE); + + lua_pop(L, 1); // pop weak meta + + lua_newtable(L); + lua_setfield(L, LUA_REGISTRYINDEX, TABLES); + + lua_createtable(L, 0, 1); // mod table + + lua_createtable(L, 0, 2); // metatable + luaL_Reg l[] = { + { "__index", lindex }, + { "__pairs", lpairs }, + { "__len", llen }, + { NULL, NULL }, + }; + lua_pushvalue(L, -1); + luaL_setfuncs(L, l, 1); +} + +static int +lstringpointer(lua_State *L) { + const char * str = luaL_checkstring(L, 1); + lua_pushlightuserdata(L, (void *)str); + return 1; +} + +LUAMOD_API int +luaopen_skynet_datasheet_core(lua_State *L) { + luaL_checkversion(L); + luaL_Reg l[] = { + { "new", lnew }, + { "update", lupdate }, + { NULL, NULL }, + }; + + luaL_newlibtable(L,l); + gen_metatable(L); + luaL_setfuncs(L, l, 1); + lua_pushcfunction(L, lstringpointer); + lua_setfield(L, -2, "stringpointer"); + return 1; +} diff --git a/lualib/skynet/datasheet/builder.lua b/lualib/skynet/datasheet/builder.lua new file mode 100644 index 00000000..1e8db48a --- /dev/null +++ b/lualib/skynet/datasheet/builder.lua @@ -0,0 +1,176 @@ +local skynet = require "skynet" +local dump = require "skynet.datasheet.dump" +local core = require "skynet.datasheet.core" +local service = require "skynet.service" + +local builder = {} + +local cache = {} +local dataset = {} + +local function monitor(pointer) + skynet.fork(function() + skynet.call(service.address, "lua", "collect", pointer) + for k,v in pairs(cache) do + if v == pointer then + cache[k] = nil + return + end + end + end) +end + +function builder.new(name, v) + assert(dataset[name] == nil) + local datastring + if type(v) == "string" then + datastring = v + else + datastring = dump.dump(v) + end + local pointer = core.stringpointer(datastring) + skynet.call(service.address, "lua", "update", name, pointer) + cache[datastring] = pointer + dataset[name] = datastring + monitor(pointer) +end + +function builder.update(name, v) + local lastversion = assert(dataset[name]) + local diff + if type(v) == "string" then + diff = v + else + local newversion = dump.dump(v) + diff = dump.diff(lastversion, newversion) + end + local pointer = core.stringpointer(diff) + skynet.call(service.address, "lua", "update", name, pointer) + cache[diff] = pointer + local lp = assert(cache[lastversion]) + skynet.send(service.address, "lua", "release", lp) + dataset[name] = diff + monitor(pointer) +end + +function builder.compile(v) + return dump.dump(v) +end + +function builder.diff(v1,v2) + local lastversion = dump.dump(v1) + local newversion = dump.dump(v2) + + return dump.diff(lastversion, newversion) +end + +local function datasheet_service() + +local skynet = require "skynet" + +local datasheet = {} +local handles = {} -- handle:{ ref:count , name:name , collect:resp } +local dataset = {} -- name:{ handle:handle, monitor:{monitors queue} } + +local function releasehandle(handle) + local h = handles[handle] + h.ref = h.ref - 1 + if h.ref == 0 and h.collect then + h.collect(true) + h.collect = nil + handles[handle] = nil + end +end + +-- from builder, create or update handle +function datasheet.update(name, handle) + local t = dataset[name] + if not t then + -- new datasheet + t = { handle = handle, monitor = {} } + dataset[name] = t + handles[handle] = { ref = 1, name = name } + else + t.handle = handle + -- report update to customers + handles[handle] = { ref = 1 + #t.monitor, name = name } + + for k,v in ipairs(t.monitor) do + v(true, handle) + t.monitor[k] = nil + end + end + skynet.ret() +end + +-- from customers +function datasheet.query(name) + local t = assert(dataset[name], "create data first") + local handle = t.handle + local h = handles[handle] + h.ref = h.ref + 1 + skynet.ret(skynet.pack(handle)) +end + +-- from customers, monitor handle change +function datasheet.monitor(handle) + local h = assert(handles[handle], "Invalid data handle") + local t = dataset[h.name] + if t.handle ~= handle then -- already changes + skynet.ret(skynet.pack(t.handle)) + else + h.ref = h.ref + 1 + table.insert(t.monitor, skynet.response()) + end +end + +-- from customers, release handle , ref count - 1 +function datasheet.release(handle) + -- send message, don't ret + releasehandle(handle) +end + +-- from builder, monitor handle release +function datasheet.collect(handle) + local h = assert(handles[handle], "Invalid data handle") + if h.ref == 0 then + handles[handle] = nil + skynet.ret() + else + assert(h.collect == nil, "Only one collect allows") + h.collect = skynet.response() + end +end + +skynet.dispatch("lua", function(_,_,cmd,...) + datasheet[cmd](...) +end) + +skynet.info_func(function() + local info = {} + local tmp = {} + for k,v in pairs(handles) do + tmp[k] = v + end + for k,v in pairs(dataset) do + local h = handles[v.handle] + tmp[v.handle] = nil + info[k] = { + handle = v.handle, + monitors = #v.monitor, + } + end + for k,v in pairs(tmp) do + info[k] = v.ref + end + + return info +end) + +end + +skynet.init(function() + service.new("datasheet", datasheet_service) +end) + +return builder diff --git a/lualib/skynet/datasheet/dump.lua b/lualib/skynet/datasheet/dump.lua new file mode 100644 index 00000000..c8d07a00 --- /dev/null +++ b/lualib/skynet/datasheet/dump.lua @@ -0,0 +1,270 @@ +--[[ file format +document : + int32 strtbloffset + int32 n + int32*n index table + table*n + strings + +table: + int32 array + int32 dict + int8*(array+dict) type (align 4) + value*array + kvpair*dict + +kvpair: + string k + value v + +value: (union) + int32 integer + float real + int32 boolean + int32 table index + int32 string offset + +type: (enum) + 0 nil + 1 integer + 2 real + 3 boolean + 4 table + 5 string +]] + +local ctd = {} +local math = math +local table = table +local string = string + +function ctd.dump(root) + local doc = { + table_n = 0, + table = {}, + strings = {}, + offset = 0, + } + local function dump_table(t) + local index = doc.table_n + 1 + doc.table_n = index + doc.table[index] = false -- place holder + local array_n = 0 + local array = {} + local kvs = {} + local types = {} + local function encode(v) + local t = type(v) + if t == "table" then + local index = dump_table(v) + return '\4', string.pack(" 0 and ik <= array_n) + end + end + -- encode table + local typeset = table.concat(types) + local align = string.rep("\0", (4 - #typeset & 3) & 3) + local tmp = { + string.pack(" Date: Fri, 2 Jun 2017 19:32:37 +0800 Subject: [PATCH 728/729] remove datasheet.builder.diff --- lualib/skynet/datasheet/builder.lua | 31 ++++++++++------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/lualib/skynet/datasheet/builder.lua b/lualib/skynet/datasheet/builder.lua index 1e8db48a..2d14ccd4 100644 --- a/lualib/skynet/datasheet/builder.lua +++ b/lualib/skynet/datasheet/builder.lua @@ -20,14 +20,17 @@ local function monitor(pointer) end) end +local function dumpsheet(v) + if type(v) == "string" then + return v + else + return dump.dump(v) + end +end + function builder.new(name, v) assert(dataset[name] == nil) - local datastring - if type(v) == "string" then - datastring = v - else - datastring = dump.dump(v) - end + local datastring = dumpsheet(v) local pointer = core.stringpointer(datastring) skynet.call(service.address, "lua", "update", name, pointer) cache[datastring] = pointer @@ -37,13 +40,8 @@ end function builder.update(name, v) local lastversion = assert(dataset[name]) - local diff - if type(v) == "string" then - diff = v - else - local newversion = dump.dump(v) - diff = dump.diff(lastversion, newversion) - end + local newversion = dumpsheet(v) + local diff = dump.diff(lastversion, newversion) local pointer = core.stringpointer(diff) skynet.call(service.address, "lua", "update", name, pointer) cache[diff] = pointer @@ -57,13 +55,6 @@ function builder.compile(v) return dump.dump(v) end -function builder.diff(v1,v2) - local lastversion = dump.dump(v1) - local newversion = dump.dump(v2) - - return dump.diff(lastversion, newversion) -end - local function datasheet_service() local skynet = require "skynet" From 7a4189f8309f4b1ea60da8dba4202fde967e4b70 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 5 Jun 2017 19:59:17 +0800 Subject: [PATCH 729/729] PROXYCACHE key weak --- lualib-src/lua-datasheet.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lualib-src/lua-datasheet.c b/lualib-src/lua-datasheet.c index e0390f4a..57e56764 100644 --- a/lualib-src/lua-datasheet.c +++ b/lualib-src/lua-datasheet.c @@ -313,23 +313,24 @@ llen(lua_State *L) { } static void -gen_metatable(lua_State *L) { +new_weak_table(lua_State *L, const char *mode) { + lua_newtable(L); // NODECACHE { pointer:table } + lua_createtable(L, 0, 1); // weak meta table - lua_pushstring(L, "kv"); + lua_pushstring(L, mode); lua_setfield(L, -2, "__mode"); - lua_newtable(L); // NODECACHE - lua_pushvalue(L, -2); - lua_setmetatable(L, -2); // make NODECACGE weak + lua_setmetatable(L, -2); // make NODECACHE weak +} + +static void +gen_metatable(lua_State *L) { + new_weak_table(L, "kv"); // NODECACHE { pointer:table } lua_setfield(L, LUA_REGISTRYINDEX, NODECACHE); - lua_newtable(L); // PROXYCACHE - lua_pushvalue(L, -2); - lua_setmetatable(L, -2); // make NODECACGE weak + new_weak_table(L, "k"); // PROXYCACHE { table:userdata } lua_setfield(L, LUA_REGISTRYINDEX, PROXYCACHE); - lua_pop(L, 1); // pop weak meta - lua_newtable(L); lua_setfield(L, LUA_REGISTRYINDEX, TABLES);